RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / video / backlight / lcd.c
blob6ef8f0a7a137612deb25cbb3377f49e31e8aa93e
1 /*
2 * LCD Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
6 */
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>
17 #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 ...
23 static int fb_notifier_callback(struct notifier_block *self,
24 unsigned long event, void *data)
26 struct lcd_device *ld;
27 struct fb_event *evdata = data;
29 /* If we aren't interested in this event, skip it immediately ... */
30 if (event != FB_EVENT_BLANK)
31 return 0;
33 ld = container_of(self, struct lcd_device, fb_notif);
34 mutex_lock(&ld->ops_lock);
35 if (ld->ops)
36 if (!ld->ops->check_fb || ld->ops->check_fb(evdata->info))
37 ld->ops->set_power(ld, *(int *)evdata->data);
38 mutex_unlock(&ld->ops_lock);
39 return 0;
42 static int lcd_register_fb(struct lcd_device *ld)
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);
49 static void lcd_unregister_fb(struct lcd_device *ld)
51 fb_unregister_client(&ld->fb_notif);
53 #else
54 static int lcd_register_fb(struct lcd_device *ld)
56 return 0;
59 static inline void lcd_unregister_fb(struct lcd_device *ld)
62 #endif /* CONFIG_FB */
64 static ssize_t lcd_show_power(struct class_device *cdev, char *buf)
66 int rc;
67 struct lcd_device *ld = to_lcd_device(cdev);
69 mutex_lock(&ld->ops_lock);
70 if (ld->ops && ld->ops->get_power)
71 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
72 else
73 rc = -ENXIO;
74 mutex_unlock(&ld->ops_lock);
76 return rc;
79 static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
81 int rc = -ENXIO;
82 char *endp;
83 struct lcd_device *ld = to_lcd_device(cdev);
84 int power = simple_strtoul(buf, &endp, 0);
85 size_t size = endp - buf;
87 if (*endp && isspace(*endp))
88 size++;
89 if (size != count)
90 return -EINVAL;
92 mutex_lock(&ld->ops_lock);
93 if (ld->ops && ld->ops->set_power) {
94 pr_debug("lcd: set power to %d\n", power);
95 ld->ops->set_power(ld, power);
96 rc = count;
98 mutex_unlock(&ld->ops_lock);
100 return rc;
103 static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
105 int rc = -ENXIO;
106 struct lcd_device *ld = to_lcd_device(cdev);
108 mutex_lock(&ld->ops_lock);
109 if (ld->ops && ld->ops->get_contrast)
110 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
111 mutex_unlock(&ld->ops_lock);
113 return rc;
116 static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
118 int rc = -ENXIO;
119 char *endp;
120 struct lcd_device *ld = to_lcd_device(cdev);
121 int contrast = simple_strtoul(buf, &endp, 0);
122 size_t size = endp - buf;
124 if (*endp && isspace(*endp))
125 size++;
126 if (size != count)
127 return -EINVAL;
129 mutex_lock(&ld->ops_lock);
130 if (ld->ops && ld->ops->set_contrast) {
131 pr_debug("lcd: set contrast to %d\n", contrast);
132 ld->ops->set_contrast(ld, contrast);
133 rc = count;
135 mutex_unlock(&ld->ops_lock);
137 return rc;
140 static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
142 struct lcd_device *ld = to_lcd_device(cdev);
144 return sprintf(buf, "%d\n", ld->props.max_contrast);
147 static void lcd_class_release(struct class_device *dev)
149 struct lcd_device *ld = to_lcd_device(dev);
150 kfree(ld);
153 static struct class lcd_class = {
154 .name = "lcd",
155 .release = lcd_class_release,
158 #define DECLARE_ATTR(_name,_mode,_show,_store) \
160 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
161 .show = _show, \
162 .store = _store, \
165 static const struct class_device_attribute lcd_class_device_attributes[] = {
166 DECLARE_ATTR(power, 0644, lcd_show_power, lcd_store_power),
167 DECLARE_ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
168 DECLARE_ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
172 * lcd_device_register - register a new object of lcd_device class.
173 * @name: the name of the new object(must be the same as the name of the
174 * respective framebuffer device).
175 * @devdata: an optional pointer to be stored in the class_device. The
176 * methods may retrieve it by using class_get_devdata(ld->class_dev).
177 * @ops: the lcd operations structure.
179 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
180 * or a pointer to the newly allocated device.
182 struct lcd_device *lcd_device_register(const char *name, void *devdata,
183 struct lcd_ops *ops)
185 int i, rc;
186 struct lcd_device *new_ld;
188 pr_debug("lcd_device_register: name=%s\n", name);
190 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
191 if (!new_ld)
192 return ERR_PTR(-ENOMEM);
194 mutex_init(&new_ld->ops_lock);
195 mutex_init(&new_ld->update_lock);
196 new_ld->ops = ops;
197 new_ld->class_dev.class = &lcd_class;
198 strlcpy(new_ld->class_dev.class_id, name, KOBJ_NAME_LEN);
199 class_set_devdata(&new_ld->class_dev, devdata);
201 rc = class_device_register(&new_ld->class_dev);
202 if (rc) {
203 kfree(new_ld);
204 return ERR_PTR(rc);
207 rc = lcd_register_fb(new_ld);
208 if (rc) {
209 class_device_unregister(&new_ld->class_dev);
210 return ERR_PTR(rc);
213 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++) {
214 rc = class_device_create_file(&new_ld->class_dev,
215 &lcd_class_device_attributes[i]);
216 if (rc) {
217 while (--i >= 0)
218 class_device_remove_file(&new_ld->class_dev,
219 &lcd_class_device_attributes[i]);
220 class_device_unregister(&new_ld->class_dev);
221 /* No need to kfree(new_ld) since release() method was called */
222 return ERR_PTR(rc);
226 return new_ld;
228 EXPORT_SYMBOL(lcd_device_register);
231 * lcd_device_unregister - unregisters a object of lcd_device class.
232 * @ld: the lcd device object to be unregistered and freed.
234 * Unregisters a previously registered via lcd_device_register object.
236 void lcd_device_unregister(struct lcd_device *ld)
238 int i;
240 if (!ld)
241 return;
243 pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
245 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++)
246 class_device_remove_file(&ld->class_dev,
247 &lcd_class_device_attributes[i]);
249 mutex_lock(&ld->ops_lock);
250 ld->ops = NULL;
251 mutex_unlock(&ld->ops_lock);
252 lcd_unregister_fb(ld);
253 class_device_unregister(&ld->class_dev);
255 EXPORT_SYMBOL(lcd_device_unregister);
257 static void __exit lcd_class_exit(void)
259 class_unregister(&lcd_class);
262 static int __init lcd_class_init(void)
264 return class_register(&lcd_class);
268 * if this is compiled into the kernel, we need to ensure that the
269 * class is registered before users of the class try to register lcd's
271 postcore_initcall(lcd_class_init);
272 module_exit(lcd_class_exit);
274 MODULE_LICENSE("GPL");
275 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
276 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");