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>
18 static ssize_t lcd_show_power(struct class_device *cdev, char *buf)
21 struct lcd_device *ld = to_lcd_device(cdev);
24 if (likely(ld->props && ld->props->get_power))
25 rc = sprintf(buf, "%d\n", ld->props->get_power(ld));
33 static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
37 struct lcd_device *ld = to_lcd_device(cdev);
39 power = simple_strtoul(buf, &endp, 0);
40 if (*endp && !isspace(*endp))
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);
55 static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
58 struct lcd_device *ld = to_lcd_device(cdev);
61 if (likely(ld->props && ld->props->get_contrast))
62 rc = sprintf(buf, "%d\n", ld->props->get_contrast(ld));
70 static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
74 struct lcd_device *ld = to_lcd_device(cdev);
76 contrast = simple_strtoul(buf, &endp, 0);
77 if (*endp && !isspace(*endp))
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);
92 static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
95 struct lcd_device *ld = to_lcd_device(cdev);
98 if (likely(ld->props))
99 rc = sprintf(buf, "%d\n", ld->props->max_contrast);
107 static void lcd_class_release(struct class_device *dev)
109 struct lcd_device *ld = to_lcd_device(dev);
113 static struct class lcd_class = {
115 .release = lcd_class_release,
118 #define DECLARE_ATTR(_name,_mode,_show,_store) \
120 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
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),
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 ...
135 static int fb_notifier_callback(struct notifier_block *self,
136 unsigned long event, void *data)
138 struct lcd_device *ld;
139 struct fb_event *evdata =(struct fb_event *)data;
141 /* If we aren't interested in this event, skip it immediately ... */
142 if (event != FB_EVENT_BLANK)
145 ld = container_of(self, struct lcd_device, fb_notif);
148 if (!ld->props->check_fb || ld->props->check_fb(evdata->info))
149 ld->props->set_power(ld, *(int *)evdata->data);
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.
162 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
163 * or a pointer to the newly allocated device.
165 struct lcd_device *lcd_device_register(const char *name, void *devdata,
166 struct lcd_properties *lp)
169 struct lcd_device *new_ld;
171 pr_debug("lcd_device_register: name=%s\n", name);
173 new_ld = kmalloc(sizeof(struct lcd_device), GFP_KERNEL);
174 if (unlikely(!new_ld))
175 return ERR_PTR(ENOMEM);
177 init_MUTEX(&new_ld->sem);
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);
184 rc = class_device_register(&new_ld->class_dev);
186 error: kfree(new_ld);
190 memset(&new_ld->fb_notif, 0, sizeof(new_ld->fb_notif));
191 new_ld->fb_notif.notifier_call = fb_notifier_callback;
193 rc = fb_register_client(&new_ld->fb_notif);
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]);
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 */
212 EXPORT_SYMBOL(lcd_device_register);
215 * lcd_device_unregister - unregisters a object of lcd_device class.
216 * @ld: the lcd device object to be unregistered and freed.
218 * Unregisters a previously registered via lcd_device_register object.
220 void lcd_device_unregister(struct lcd_device *ld)
227 pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
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]);
237 fb_unregister_client(&ld->fb_notif);
239 class_device_unregister(&ld->class_dev);
241 EXPORT_SYMBOL(lcd_device_unregister);
243 static void __exit lcd_class_exit(void)
245 class_unregister(&lcd_class);
248 static int __init lcd_class_init(void)
250 return class_register(&lcd_class);
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
257 postcore_initcall(lcd_class_init);
258 module_exit(lcd_class_exit);
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");