Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[pandora-kernel.git] / drivers / video / backlight / lcd.c
1 /*
2  * LCD Lowlevel Control Abstraction
3  *
4  * Copyright (C) 2003,2004 Hewlett-Packard Company
5  *
6  */
7
8 #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>
16 #include <asm/bug.h>
17
18 static ssize_t lcd_show_power(struct class_device *cdev, char *buf)
19 {
20         int rc;
21         struct lcd_device *ld = to_lcd_device(cdev);
22
23         down(&ld->sem);
24         if (likely(ld->props && ld->props->get_power))
25                 rc = sprintf(buf, "%d\n", ld->props->get_power(ld));
26         else
27                 rc = -ENXIO;
28         up(&ld->sem);
29
30         return rc;
31 }
32
33 static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
34 {
35         int rc, power;
36         char *endp;
37         struct lcd_device *ld = to_lcd_device(cdev);
38
39         power = simple_strtoul(buf, &endp, 0);
40         if (*endp && !isspace(*endp))
41                 return -EINVAL;
42
43         down(&ld->sem);
44         if (likely(ld->props && ld->props->set_power)) {
45                 pr_debug("lcd: set power to %d\n", power);
46                 ld->props->set_power(ld, power);
47                 rc = count;
48         } else
49                 rc = -ENXIO;
50         up(&ld->sem);
51
52         return rc;
53 }
54
55 static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
56 {
57         int rc;
58         struct lcd_device *ld = to_lcd_device(cdev);
59
60         down(&ld->sem);
61         if (likely(ld->props && ld->props->get_contrast))
62                 rc = sprintf(buf, "%d\n", ld->props->get_contrast(ld));
63         else
64                 rc = -ENXIO;
65         up(&ld->sem);
66
67         return rc;
68 }
69
70 static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
71 {
72         int rc, contrast;
73         char *endp;
74         struct lcd_device *ld = to_lcd_device(cdev);
75
76         contrast = simple_strtoul(buf, &endp, 0);
77         if (*endp && !isspace(*endp))
78                 return -EINVAL;
79
80         down(&ld->sem);
81         if (likely(ld->props && ld->props->set_contrast)) {
82                 pr_debug("lcd: set contrast to %d\n", contrast);
83                 ld->props->set_contrast(ld, contrast);
84                 rc = count;
85         } else
86                 rc = -ENXIO;
87         up(&ld->sem);
88
89         return rc;
90 }
91
92 static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
93 {
94         int rc;
95         struct lcd_device *ld = to_lcd_device(cdev);
96
97         down(&ld->sem);
98         if (likely(ld->props))
99                 rc = sprintf(buf, "%d\n", ld->props->max_contrast);
100         else
101                 rc = -ENXIO;
102         up(&ld->sem);
103
104         return rc;
105 }
106
107 static void lcd_class_release(struct class_device *dev)
108 {
109         struct lcd_device *ld = to_lcd_device(dev);
110         kfree(ld);
111 }
112
113 static struct class lcd_class = {
114         .name = "lcd",
115         .release = lcd_class_release,
116 };
117
118 #define DECLARE_ATTR(_name,_mode,_show,_store)                  \
119 {                                                               \
120         .attr   = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },  \
121         .show   = _show,                                        \
122         .store  = _store,                                       \
123 }
124
125 static struct class_device_attribute lcd_class_device_attributes[] = {
126         DECLARE_ATTR(power, 0644, lcd_show_power, lcd_store_power),
127         DECLARE_ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
128         DECLARE_ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
129 };
130
131 /* This callback gets called when something important happens inside a
132  * framebuffer driver. We're looking if that important event is blanking,
133  * and if it is, we're switching lcd power as well ...
134  */
135 static int fb_notifier_callback(struct notifier_block *self,
136                                  unsigned long event, void *data)
137 {
138         struct lcd_device *ld;
139         struct fb_event *evdata =(struct fb_event *)data;
140
141         /* If we aren't interested in this event, skip it immediately ... */
142         if (event != FB_EVENT_BLANK)
143                 return 0;
144
145         ld = container_of(self, struct lcd_device, fb_notif);
146         down(&ld->sem);
147         if (ld->props)
148                 if (!ld->props->check_fb || ld->props->check_fb(evdata->info))
149                         ld->props->set_power(ld, *(int *)evdata->data);
150         up(&ld->sem);
151         return 0;
152 }
153
154 /**
155  * lcd_device_register - register a new object of lcd_device class.
156  * @name: the name of the new object(must be the same as the name of the
157  *   respective framebuffer device).
158  * @devdata: an optional pointer to be stored in the class_device. The
159  *   methods may retrieve it by using class_get_devdata(ld->class_dev).
160  * @lp: the lcd properties structure.
161  *
162  * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
163  * or a pointer to the newly allocated device.
164  */
165 struct lcd_device *lcd_device_register(const char *name, void *devdata,
166                                        struct lcd_properties *lp)
167 {
168         int i, rc;
169         struct lcd_device *new_ld;
170
171         pr_debug("lcd_device_register: name=%s\n", name);
172
173         new_ld = kmalloc(sizeof(struct lcd_device), GFP_KERNEL);
174         if (unlikely(!new_ld))
175                 return ERR_PTR(ENOMEM);
176
177         init_MUTEX(&new_ld->sem);
178         new_ld->props = lp;
179         memset(&new_ld->class_dev, 0, sizeof(new_ld->class_dev));
180         new_ld->class_dev.class = &lcd_class;
181         strlcpy(new_ld->class_dev.class_id, name, KOBJ_NAME_LEN);
182         class_set_devdata(&new_ld->class_dev, devdata);
183
184         rc = class_device_register(&new_ld->class_dev);
185         if (unlikely(rc)) {
186 error:          kfree(new_ld);
187                 return ERR_PTR(rc);
188         }
189
190         memset(&new_ld->fb_notif, 0, sizeof(new_ld->fb_notif));
191         new_ld->fb_notif.notifier_call = fb_notifier_callback;
192
193         rc = fb_register_client(&new_ld->fb_notif);
194         if (unlikely(rc))
195                 goto error;
196
197         for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++) {
198                 rc = class_device_create_file(&new_ld->class_dev,
199                                               &lcd_class_device_attributes[i]);
200                 if (unlikely(rc)) {
201                         while (--i >= 0)
202                                 class_device_remove_file(&new_ld->class_dev,
203                                                          &lcd_class_device_attributes[i]);
204                         class_device_unregister(&new_ld->class_dev);
205                         /* No need to kfree(new_ld) since release() method was called */
206                         return ERR_PTR(rc);
207                 }
208         }
209
210         return new_ld;
211 }
212 EXPORT_SYMBOL(lcd_device_register);
213
214 /**
215  * lcd_device_unregister - unregisters a object of lcd_device class.
216  * @ld: the lcd device object to be unregistered and freed.
217  *
218  * Unregisters a previously registered via lcd_device_register object.
219  */
220 void lcd_device_unregister(struct lcd_device *ld)
221 {
222         int i;
223
224         if (!ld)
225                 return;
226
227         pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
228
229         for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++)
230                 class_device_remove_file(&ld->class_dev,
231                                          &lcd_class_device_attributes[i]);
232
233         down(&ld->sem);
234         ld->props = NULL;
235         up(&ld->sem);
236
237         fb_unregister_client(&ld->fb_notif);
238
239         class_device_unregister(&ld->class_dev);
240 }
241 EXPORT_SYMBOL(lcd_device_unregister);
242
243 static void __exit lcd_class_exit(void)
244 {
245         class_unregister(&lcd_class);
246 }
247
248 static int __init lcd_class_init(void)
249 {
250         return class_register(&lcd_class);
251 }
252
253 /*
254  * if this is compiled into the kernel, we need to ensure that the
255  * class is registered before users of the class try to register lcd's
256  */
257 postcore_initcall(lcd_class_init);
258 module_exit(lcd_class_exit);
259
260 MODULE_LICENSE("GPL");
261 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
262 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");