blob: 1e1e61a4b5da6cc6651f2e2a55fa7a76cd12dbe7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * LCD Lowlevel Control Abstraction
3 *
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
5 *
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/device.h>
11#include <linux/lcd.h>
12#include <linux/notifier.h>
13#include <linux/ctype.h>
14#include <linux/err.h>
15#include <linux/fb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
James Simmons3d5eead2006-12-08 02:40:47 -080017#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
18 defined(CONFIG_LCD_CLASS_DEVICE_MODULE))
19/* This callback gets called when something important happens inside a
20 * framebuffer driver. We're looking if that important event is blanking,
21 * and if it is, we're switching lcd power as well ...
22 */
23static int fb_notifier_callback(struct notifier_block *self,
24 unsigned long event, void *data)
25{
26 struct lcd_device *ld;
27 struct fb_event *evdata = data;
28
29 /* If we aren't interested in this event, skip it immediately ... */
30 if (event != FB_EVENT_BLANK)
31 return 0;
32
33 ld = container_of(self, struct lcd_device, fb_notif);
34 down(&ld->sem);
35 if (ld->props)
36 if (!ld->props->check_fb || ld->props->check_fb(evdata->info))
37 ld->props->set_power(ld, *(int *)evdata->data);
38 up(&ld->sem);
39 return 0;
40}
41
42static int lcd_register_fb(struct lcd_device *ld)
43{
44 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
45 ld->fb_notif.notifier_call = fb_notifier_callback;
46 return fb_register_client(&ld->fb_notif);
47}
48
49static void lcd_unregister_fb(struct lcd_device *ld)
50{
51 fb_unregister_client(&ld->fb_notif);
52}
53#else
54static int lcd_register_fb(struct lcd_device *ld)
55{
56 return 0;
57}
58
59static inline void lcd_unregister_fb(struct lcd_device *ld)
60{
61}
62#endif /* CONFIG_FB */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static ssize_t lcd_show_power(struct class_device *cdev, char *buf)
65{
66 int rc;
67 struct lcd_device *ld = to_lcd_device(cdev);
68
69 down(&ld->sem);
Dmitry Torokhov90968e82007-02-08 00:12:28 +000070 if (ld->props && ld->props->get_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 rc = sprintf(buf, "%d\n", ld->props->get_power(ld));
72 else
73 rc = -ENXIO;
74 up(&ld->sem);
75
76 return rc;
77}
78
79static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
80{
Richard Purdie68673af2006-05-15 09:44:15 -070081 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 char *endp;
83 struct lcd_device *ld = to_lcd_device(cdev);
Richard Purdie68673af2006-05-15 09:44:15 -070084 int power = simple_strtoul(buf, &endp, 0);
85 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Richard Purdie68673af2006-05-15 09:44:15 -070087 if (*endp && isspace(*endp))
88 size++;
89 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EINVAL;
91
92 down(&ld->sem);
Dmitry Torokhov90968e82007-02-08 00:12:28 +000093 if (ld->props && ld->props->set_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 pr_debug("lcd: set power to %d\n", power);
95 ld->props->set_power(ld, power);
96 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -070097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 up(&ld->sem);
99
100 return rc;
101}
102
103static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
104{
Richard Purdie68673af2006-05-15 09:44:15 -0700105 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct lcd_device *ld = to_lcd_device(cdev);
107
108 down(&ld->sem);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000109 if (ld->props && ld->props->get_contrast)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 rc = sprintf(buf, "%d\n", ld->props->get_contrast(ld));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 up(&ld->sem);
112
113 return rc;
114}
115
116static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
117{
Richard Purdie68673af2006-05-15 09:44:15 -0700118 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 char *endp;
120 struct lcd_device *ld = to_lcd_device(cdev);
Richard Purdie68673af2006-05-15 09:44:15 -0700121 int contrast = simple_strtoul(buf, &endp, 0);
122 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Richard Purdie68673af2006-05-15 09:44:15 -0700124 if (*endp && isspace(*endp))
125 size++;
126 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return -EINVAL;
128
129 down(&ld->sem);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000130 if (ld->props && ld->props->set_contrast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 pr_debug("lcd: set contrast to %d\n", contrast);
132 ld->props->set_contrast(ld, contrast);
133 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 up(&ld->sem);
136
137 return rc;
138}
139
140static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
141{
Richard Purdie68673af2006-05-15 09:44:15 -0700142 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct lcd_device *ld = to_lcd_device(cdev);
144
145 down(&ld->sem);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000146 if (ld->props)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 rc = sprintf(buf, "%d\n", ld->props->max_contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 up(&ld->sem);
149
150 return rc;
151}
152
153static void lcd_class_release(struct class_device *dev)
154{
155 struct lcd_device *ld = to_lcd_device(dev);
156 kfree(ld);
157}
158
159static struct class lcd_class = {
160 .name = "lcd",
161 .release = lcd_class_release,
162};
163
164#define DECLARE_ATTR(_name,_mode,_show,_store) \
165{ \
166 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
167 .show = _show, \
168 .store = _store, \
169}
170
Helge Dellerd95159c2006-12-08 02:40:28 -0800171static const struct class_device_attribute lcd_class_device_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 DECLARE_ATTR(power, 0644, lcd_show_power, lcd_store_power),
173 DECLARE_ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
174 DECLARE_ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
175};
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
178 * lcd_device_register - register a new object of lcd_device class.
179 * @name: the name of the new object(must be the same as the name of the
180 * respective framebuffer device).
181 * @devdata: an optional pointer to be stored in the class_device. The
182 * methods may retrieve it by using class_get_devdata(ld->class_dev).
183 * @lp: the lcd properties structure.
184 *
185 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
186 * or a pointer to the newly allocated device.
187 */
188struct lcd_device *lcd_device_register(const char *name, void *devdata,
189 struct lcd_properties *lp)
190{
191 int i, rc;
192 struct lcd_device *new_ld;
193
194 pr_debug("lcd_device_register: name=%s\n", name);
195
196 new_ld = kmalloc(sizeof(struct lcd_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000197 if (!new_ld)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800198 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 init_MUTEX(&new_ld->sem);
Richard Purdie28ee0862007-02-08 22:25:09 +0000201 mutex_init(&new_ld->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 new_ld->props = lp;
203 memset(&new_ld->class_dev, 0, sizeof(new_ld->class_dev));
204 new_ld->class_dev.class = &lcd_class;
205 strlcpy(new_ld->class_dev.class_id, name, KOBJ_NAME_LEN);
206 class_set_devdata(&new_ld->class_dev, devdata);
207
208 rc = class_device_register(&new_ld->class_dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000209 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000210 kfree(new_ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return ERR_PTR(rc);
212 }
213
James Simmons3d5eead2006-12-08 02:40:47 -0800214 rc = lcd_register_fb(new_ld);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000215 if (rc) {
216 class_device_unregister(&new_ld->class_dev);
217 return ERR_PTR(rc);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++) {
221 rc = class_device_create_file(&new_ld->class_dev,
222 &lcd_class_device_attributes[i]);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000223 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 while (--i >= 0)
225 class_device_remove_file(&new_ld->class_dev,
226 &lcd_class_device_attributes[i]);
227 class_device_unregister(&new_ld->class_dev);
228 /* No need to kfree(new_ld) since release() method was called */
229 return ERR_PTR(rc);
230 }
231 }
232
233 return new_ld;
234}
235EXPORT_SYMBOL(lcd_device_register);
236
237/**
238 * lcd_device_unregister - unregisters a object of lcd_device class.
239 * @ld: the lcd device object to be unregistered and freed.
240 *
241 * Unregisters a previously registered via lcd_device_register object.
242 */
243void lcd_device_unregister(struct lcd_device *ld)
244{
245 int i;
246
247 if (!ld)
248 return;
249
250 pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
251
252 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++)
253 class_device_remove_file(&ld->class_dev,
254 &lcd_class_device_attributes[i]);
255
256 down(&ld->sem);
257 ld->props = NULL;
258 up(&ld->sem);
James Simmons3d5eead2006-12-08 02:40:47 -0800259 lcd_unregister_fb(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 class_device_unregister(&ld->class_dev);
261}
262EXPORT_SYMBOL(lcd_device_unregister);
263
264static void __exit lcd_class_exit(void)
265{
266 class_unregister(&lcd_class);
267}
268
269static int __init lcd_class_init(void)
270{
271 return class_register(&lcd_class);
272}
273
274/*
275 * if this is compiled into the kernel, we need to ensure that the
276 * class is registered before users of the class try to register lcd's
277 */
278postcore_initcall(lcd_class_init);
279module_exit(lcd_class_exit);
280
281MODULE_LICENSE("GPL");
282MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
283MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");