2 * LCD Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
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>
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)
33 ld = container_of(self, struct lcd_device, fb_notif);
34 mutex_lock(&ld->props_lock);
36 if (!ld->props->check_fb || ld->props->check_fb(evdata->info))
37 ld->props->set_power(ld, *(int *)evdata->data);
38 mutex_unlock(&ld->props_lock);
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);
54 static int lcd_register_fb(struct lcd_device *ld)
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)
67 struct lcd_device *ld = to_lcd_device(cdev);
69 mutex_lock(&ld->props_lock);
70 if (ld->props && ld->props->get_power)
71 rc = sprintf(buf, "%d\n", ld->props->get_power(ld));
74 mutex_unlock(&ld->props_lock);
79 static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
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))
92 mutex_lock(&ld->props_lock);
93 if (ld->props && ld->props->set_power) {
94 pr_debug("lcd: set power to %d\n", power);
95 ld->props->set_power(ld, power);
98 mutex_unlock(&ld->props_lock);
103 static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
106 struct lcd_device *ld = to_lcd_device(cdev);
108 mutex_lock(&ld->props_lock);
109 if (ld->props && ld->props->get_contrast)
110 rc = sprintf(buf, "%d\n", ld->props->get_contrast(ld));
111 mutex_unlock(&ld->props_lock);
116 static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
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))
129 mutex_lock(&ld->props_lock);
130 if (ld->props && ld->props->set_contrast) {
131 pr_debug("lcd: set contrast to %d\n", contrast);
132 ld->props->set_contrast(ld, contrast);
135 mutex_unlock(&ld->props_lock);
140 static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
143 struct lcd_device *ld = to_lcd_device(cdev);
145 mutex_lock(&ld->props_lock);
147 rc = sprintf(buf, "%d\n", ld->props->max_contrast);
148 mutex_unlock(&ld->props_lock);
153 static void lcd_class_release(struct class_device *dev)
155 struct lcd_device *ld = to_lcd_device(dev);
159 static struct class lcd_class = {
161 .release = lcd_class_release,
164 #define DECLARE_ATTR(_name,_mode,_show,_store) \
166 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
171 static const struct class_device_attribute lcd_class_device_attributes[] = {
172 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),
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.
185 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
186 * or a pointer to the newly allocated device.
188 struct lcd_device *lcd_device_register(const char *name, void *devdata,
189 struct lcd_properties *lp)
192 struct lcd_device *new_ld;
194 pr_debug("lcd_device_register: name=%s\n", name);
196 new_ld = kmalloc(sizeof(struct lcd_device), GFP_KERNEL);
198 return ERR_PTR(-ENOMEM);
200 mutex_init(&new_ld->props_lock);
201 mutex_init(&new_ld->update_lock);
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);
208 rc = class_device_register(&new_ld->class_dev);
214 rc = lcd_register_fb(new_ld);
216 class_device_unregister(&new_ld->class_dev);
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]);
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 */
235 EXPORT_SYMBOL(lcd_device_register);
238 * lcd_device_unregister - unregisters a object of lcd_device class.
239 * @ld: the lcd device object to be unregistered and freed.
241 * Unregisters a previously registered via lcd_device_register object.
243 void lcd_device_unregister(struct lcd_device *ld)
250 pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
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]);
256 mutex_lock(&ld->props_lock);
258 mutex_unlock(&ld->props_lock);
259 lcd_unregister_fb(ld);
260 class_device_unregister(&ld->class_dev);
262 EXPORT_SYMBOL(lcd_device_unregister);
264 static void __exit lcd_class_exit(void)
266 class_unregister(&lcd_class);
269 static int __init lcd_class_init(void)
271 return class_register(&lcd_class);
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
278 postcore_initcall(lcd_class_init);
279 module_exit(lcd_class_exit);
281 MODULE_LICENSE("GPL");
282 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
283 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");