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 ... */
32 case FB_EVENT_MODE_CHANGE:
33 case FB_EVENT_MODE_CHANGE_ALL:
39 ld = container_of(self, struct lcd_device, fb_notif);
43 if (!lock_fb_info(evdata->info))
45 mutex_lock(&ld->ops_lock);
46 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
47 if (event == FB_EVENT_BLANK) {
48 if (ld->ops->set_power)
49 ld->ops->set_power(ld, *(int *)evdata->data);
51 if (ld->ops->set_mode)
52 ld->ops->set_mode(ld, evdata->data);
55 mutex_unlock(&ld->ops_lock);
56 unlock_fb_info(evdata->info);
60 static int lcd_register_fb(struct lcd_device *ld)
62 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
63 ld->fb_notif.notifier_call = fb_notifier_callback;
64 return fb_register_client(&ld->fb_notif);
67 static void lcd_unregister_fb(struct lcd_device *ld)
69 fb_unregister_client(&ld->fb_notif);
72 static int lcd_register_fb(struct lcd_device *ld)
77 static inline void lcd_unregister_fb(struct lcd_device *ld)
80 #endif /* CONFIG_FB */
82 static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
86 struct lcd_device *ld = to_lcd_device(dev);
88 mutex_lock(&ld->ops_lock);
89 if (ld->ops && ld->ops->get_power)
90 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
93 mutex_unlock(&ld->ops_lock);
98 static ssize_t lcd_store_power(struct device *dev,
99 struct device_attribute *attr, const char *buf, size_t count)
103 struct lcd_device *ld = to_lcd_device(dev);
104 int power = simple_strtoul(buf, &endp, 0);
105 size_t size = endp - buf;
107 if (*endp && isspace(*endp))
112 mutex_lock(&ld->ops_lock);
113 if (ld->ops && ld->ops->set_power) {
114 pr_debug("lcd: set power to %d\n", power);
115 ld->ops->set_power(ld, power);
118 mutex_unlock(&ld->ops_lock);
123 static ssize_t lcd_show_contrast(struct device *dev,
124 struct device_attribute *attr, char *buf)
127 struct lcd_device *ld = to_lcd_device(dev);
129 mutex_lock(&ld->ops_lock);
130 if (ld->ops && ld->ops->get_contrast)
131 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
132 mutex_unlock(&ld->ops_lock);
137 static ssize_t lcd_store_contrast(struct device *dev,
138 struct device_attribute *attr, const char *buf, size_t count)
142 struct lcd_device *ld = to_lcd_device(dev);
143 int contrast = simple_strtoul(buf, &endp, 0);
144 size_t size = endp - buf;
146 if (*endp && isspace(*endp))
151 mutex_lock(&ld->ops_lock);
152 if (ld->ops && ld->ops->set_contrast) {
153 pr_debug("lcd: set contrast to %d\n", contrast);
154 ld->ops->set_contrast(ld, contrast);
157 mutex_unlock(&ld->ops_lock);
162 static ssize_t lcd_show_max_contrast(struct device *dev,
163 struct device_attribute *attr, char *buf)
165 struct lcd_device *ld = to_lcd_device(dev);
167 return sprintf(buf, "%d\n", ld->props.max_contrast);
170 static struct class *lcd_class;
172 static void lcd_device_release(struct device *dev)
174 struct lcd_device *ld = to_lcd_device(dev);
178 static struct device_attribute lcd_device_attributes[] = {
179 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
180 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
181 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
186 * lcd_device_register - register a new object of lcd_device class.
187 * @name: the name of the new object(must be the same as the name of the
188 * respective framebuffer device).
189 * @devdata: an optional pointer to be stored in the device. The
190 * methods may retrieve it by using lcd_get_data(ld).
191 * @ops: the lcd operations structure.
193 * Creates and registers a new lcd device. Returns either an ERR_PTR()
194 * or a pointer to the newly allocated device.
196 struct lcd_device *lcd_device_register(const char *name, struct device *parent,
197 void *devdata, struct lcd_ops *ops)
199 struct lcd_device *new_ld;
202 pr_debug("lcd_device_register: name=%s\n", name);
204 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
206 return ERR_PTR(-ENOMEM);
208 mutex_init(&new_ld->ops_lock);
209 mutex_init(&new_ld->update_lock);
211 new_ld->dev.class = lcd_class;
212 new_ld->dev.parent = parent;
213 new_ld->dev.release = lcd_device_release;
214 dev_set_name(&new_ld->dev, name);
215 dev_set_drvdata(&new_ld->dev, devdata);
217 rc = device_register(&new_ld->dev);
223 rc = lcd_register_fb(new_ld);
225 device_unregister(&new_ld->dev);
233 EXPORT_SYMBOL(lcd_device_register);
236 * lcd_device_unregister - unregisters a object of lcd_device class.
237 * @ld: the lcd device object to be unregistered and freed.
239 * Unregisters a previously registered via lcd_device_register object.
241 void lcd_device_unregister(struct lcd_device *ld)
246 mutex_lock(&ld->ops_lock);
248 mutex_unlock(&ld->ops_lock);
249 lcd_unregister_fb(ld);
251 device_unregister(&ld->dev);
253 EXPORT_SYMBOL(lcd_device_unregister);
255 static void __exit lcd_class_exit(void)
257 class_destroy(lcd_class);
260 static int __init lcd_class_init(void)
262 lcd_class = class_create(THIS_MODULE, "lcd");
263 if (IS_ERR(lcd_class)) {
264 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
266 return PTR_ERR(lcd_class);
269 lcd_class->dev_attrs = lcd_device_attributes;
274 * if this is compiled into the kernel, we need to ensure that the
275 * class is registered before users of the class try to register lcd's
277 postcore_initcall(lcd_class_init);
278 module_exit(lcd_class_exit);
280 MODULE_LICENSE("GPL");
281 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
282 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");