Linux-2.6.12-rc2
[linux-2.6/kvm.git] / drivers / video / backlight / lcd.c
blob470e6f0ee4dd808a21fdee829d5404f6a2ab74e2
1 /*
2 * LCD Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
6 */
8 #include <linux/version.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/device.h>
12 #include <linux/lcd.h>
13 #include <linux/notifier.h>
14 #include <linux/ctype.h>
15 #include <linux/err.h>
16 #include <linux/fb.h>
17 #include <asm/bug.h>
19 static ssize_t lcd_show_power(struct class_device *cdev, char *buf)
21 int rc;
22 struct lcd_device *ld = to_lcd_device(cdev);
24 down(&ld->sem);
25 if (likely(ld->props && ld->props->get_power))
26 rc = sprintf(buf, "%d\n", ld->props->get_power(ld));
27 else
28 rc = -ENXIO;
29 up(&ld->sem);
31 return rc;
34 static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
36 int rc, power;
37 char *endp;
38 struct lcd_device *ld = to_lcd_device(cdev);
40 power = simple_strtoul(buf, &endp, 0);
41 if (*endp && !isspace(*endp))
42 return -EINVAL;
44 down(&ld->sem);
45 if (likely(ld->props && ld->props->set_power)) {
46 pr_debug("lcd: set power to %d\n", power);
47 ld->props->set_power(ld, power);
48 rc = count;
49 } else
50 rc = -ENXIO;
51 up(&ld->sem);
53 return rc;
56 static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
58 int rc;
59 struct lcd_device *ld = to_lcd_device(cdev);
61 down(&ld->sem);
62 if (likely(ld->props && ld->props->get_contrast))
63 rc = sprintf(buf, "%d\n", ld->props->get_contrast(ld));
64 else
65 rc = -ENXIO;
66 up(&ld->sem);
68 return rc;
71 static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
73 int rc, contrast;
74 char *endp;
75 struct lcd_device *ld = to_lcd_device(cdev);
77 contrast = simple_strtoul(buf, &endp, 0);
78 if (*endp && !isspace(*endp))
79 return -EINVAL;
81 down(&ld->sem);
82 if (likely(ld->props && ld->props->set_contrast)) {
83 pr_debug("lcd: set contrast to %d\n", contrast);
84 ld->props->set_contrast(ld, contrast);
85 rc = count;
86 } else
87 rc = -ENXIO;
88 up(&ld->sem);
90 return rc;
93 static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
95 int rc;
96 struct lcd_device *ld = to_lcd_device(cdev);
98 down(&ld->sem);
99 if (likely(ld->props))
100 rc = sprintf(buf, "%d\n", ld->props->max_contrast);
101 else
102 rc = -ENXIO;
103 up(&ld->sem);
105 return rc;
108 static void lcd_class_release(struct class_device *dev)
110 struct lcd_device *ld = to_lcd_device(dev);
111 kfree(ld);
114 static struct class lcd_class = {
115 .name = "lcd",
116 .release = lcd_class_release,
119 #define DECLARE_ATTR(_name,_mode,_show,_store) \
121 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
122 .show = _show, \
123 .store = _store, \
126 static struct class_device_attribute lcd_class_device_attributes[] = {
127 DECLARE_ATTR(power, 0644, lcd_show_power, lcd_store_power),
128 DECLARE_ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
129 DECLARE_ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
132 /* This callback gets called when something important happens inside a
133 * framebuffer driver. We're looking if that important event is blanking,
134 * and if it is, we're switching lcd power as well ...
136 static int fb_notifier_callback(struct notifier_block *self,
137 unsigned long event, void *data)
139 struct lcd_device *ld;
140 struct fb_event *evdata =(struct fb_event *)data;
142 /* If we aren't interested in this event, skip it immediately ... */
143 if (event != FB_EVENT_BLANK)
144 return 0;
146 ld = container_of(self, struct lcd_device, fb_notif);
147 down(&ld->sem);
148 if (ld->props)
149 if (!ld->props->check_fb || ld->props->check_fb(evdata->info))
150 ld->props->set_power(ld, *(int *)evdata->data);
151 up(&ld->sem);
152 return 0;
156 * lcd_device_register - register a new object of lcd_device class.
157 * @name: the name of the new object(must be the same as the name of the
158 * respective framebuffer device).
159 * @devdata: an optional pointer to be stored in the class_device. The
160 * methods may retrieve it by using class_get_devdata(ld->class_dev).
161 * @lp: the lcd properties structure.
163 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
164 * or a pointer to the newly allocated device.
166 struct lcd_device *lcd_device_register(const char *name, void *devdata,
167 struct lcd_properties *lp)
169 int i, rc;
170 struct lcd_device *new_ld;
172 pr_debug("lcd_device_register: name=%s\n", name);
174 new_ld = kmalloc(sizeof(struct lcd_device), GFP_KERNEL);
175 if (unlikely(!new_ld))
176 return ERR_PTR(ENOMEM);
178 init_MUTEX(&new_ld->sem);
179 new_ld->props = lp;
180 memset(&new_ld->class_dev, 0, sizeof(new_ld->class_dev));
181 new_ld->class_dev.class = &lcd_class;
182 strlcpy(new_ld->class_dev.class_id, name, KOBJ_NAME_LEN);
183 class_set_devdata(&new_ld->class_dev, devdata);
185 rc = class_device_register(&new_ld->class_dev);
186 if (unlikely(rc)) {
187 error: kfree(new_ld);
188 return ERR_PTR(rc);
191 memset(&new_ld->fb_notif, 0, sizeof(new_ld->fb_notif));
192 new_ld->fb_notif.notifier_call = fb_notifier_callback;
194 rc = fb_register_client(&new_ld->fb_notif);
195 if (unlikely(rc))
196 goto error;
198 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++) {
199 rc = class_device_create_file(&new_ld->class_dev,
200 &lcd_class_device_attributes[i]);
201 if (unlikely(rc)) {
202 while (--i >= 0)
203 class_device_remove_file(&new_ld->class_dev,
204 &lcd_class_device_attributes[i]);
205 class_device_unregister(&new_ld->class_dev);
206 /* No need to kfree(new_ld) since release() method was called */
207 return ERR_PTR(rc);
211 return new_ld;
213 EXPORT_SYMBOL(lcd_device_register);
216 * lcd_device_unregister - unregisters a object of lcd_device class.
217 * @ld: the lcd device object to be unregistered and freed.
219 * Unregisters a previously registered via lcd_device_register object.
221 void lcd_device_unregister(struct lcd_device *ld)
223 int i;
225 if (!ld)
226 return;
228 pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
230 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++)
231 class_device_remove_file(&ld->class_dev,
232 &lcd_class_device_attributes[i]);
234 down(&ld->sem);
235 ld->props = NULL;
236 up(&ld->sem);
238 fb_unregister_client(&ld->fb_notif);
240 class_device_unregister(&ld->class_dev);
242 EXPORT_SYMBOL(lcd_device_unregister);
244 static void __exit lcd_class_exit(void)
246 class_unregister(&lcd_class);
249 static int __init lcd_class_init(void)
251 return class_register(&lcd_class);
255 * if this is compiled into the kernel, we need to ensure that the
256 * class is registered before users of the class try to register lcd's
258 postcore_initcall(lcd_class_init);
259 module_exit(lcd_class_exit);
261 MODULE_LICENSE("GPL");
262 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
263 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");