2 * LCD Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/lcd.h>
14 #include <linux/notifier.h>
15 #include <linux/ctype.h>
16 #include <linux/err.h>
18 #include <linux/slab.h>
20 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
21 defined(CONFIG_LCD_CLASS_DEVICE_MODULE))
22 /* This callback gets called when something important happens inside a
23 * framebuffer driver. We're looking if that important event is blanking,
24 * and if it is, we're switching lcd power as well ...
26 static int fb_notifier_callback(struct notifier_block
*self
,
27 unsigned long event
, void *data
)
29 struct lcd_device
*ld
;
30 struct fb_event
*evdata
= data
;
32 /* If we aren't interested in this event, skip it immediately ... */
35 case FB_EVENT_MODE_CHANGE
:
36 case FB_EVENT_MODE_CHANGE_ALL
:
37 case FB_EARLY_EVENT_BLANK
:
38 case FB_R_EARLY_EVENT_BLANK
:
44 ld
= container_of(self
, struct lcd_device
, fb_notif
);
48 mutex_lock(&ld
->ops_lock
);
49 if (!ld
->ops
->check_fb
|| ld
->ops
->check_fb(ld
, evdata
->info
)) {
50 if (event
== FB_EVENT_BLANK
) {
51 if (ld
->ops
->set_power
)
52 ld
->ops
->set_power(ld
, *(int *)evdata
->data
);
53 } else if (event
== FB_EARLY_EVENT_BLANK
) {
54 if (ld
->ops
->early_set_power
)
55 ld
->ops
->early_set_power(ld
,
56 *(int *)evdata
->data
);
57 } else if (event
== FB_R_EARLY_EVENT_BLANK
) {
58 if (ld
->ops
->r_early_set_power
)
59 ld
->ops
->r_early_set_power(ld
,
60 *(int *)evdata
->data
);
62 if (ld
->ops
->set_mode
)
63 ld
->ops
->set_mode(ld
, evdata
->data
);
66 mutex_unlock(&ld
->ops_lock
);
70 static int lcd_register_fb(struct lcd_device
*ld
)
72 memset(&ld
->fb_notif
, 0, sizeof(ld
->fb_notif
));
73 ld
->fb_notif
.notifier_call
= fb_notifier_callback
;
74 return fb_register_client(&ld
->fb_notif
);
77 static void lcd_unregister_fb(struct lcd_device
*ld
)
79 fb_unregister_client(&ld
->fb_notif
);
82 static int lcd_register_fb(struct lcd_device
*ld
)
87 static inline void lcd_unregister_fb(struct lcd_device
*ld
)
90 #endif /* CONFIG_FB */
92 static ssize_t
lcd_power_show(struct device
*dev
, struct device_attribute
*attr
,
96 struct lcd_device
*ld
= to_lcd_device(dev
);
98 mutex_lock(&ld
->ops_lock
);
99 if (ld
->ops
&& ld
->ops
->get_power
)
100 rc
= sprintf(buf
, "%d\n", ld
->ops
->get_power(ld
));
103 mutex_unlock(&ld
->ops_lock
);
108 static ssize_t
lcd_power_store(struct device
*dev
,
109 struct device_attribute
*attr
, const char *buf
, size_t count
)
112 struct lcd_device
*ld
= to_lcd_device(dev
);
115 rc
= kstrtoul(buf
, 0, &power
);
121 mutex_lock(&ld
->ops_lock
);
122 if (ld
->ops
&& ld
->ops
->set_power
) {
123 pr_debug("set power to %lu\n", power
);
124 ld
->ops
->set_power(ld
, power
);
127 mutex_unlock(&ld
->ops_lock
);
131 static DEVICE_ATTR_RW(lcd_power
);
133 static ssize_t
contrast_show(struct device
*dev
,
134 struct device_attribute
*attr
, char *buf
)
137 struct lcd_device
*ld
= to_lcd_device(dev
);
139 mutex_lock(&ld
->ops_lock
);
140 if (ld
->ops
&& ld
->ops
->get_contrast
)
141 rc
= sprintf(buf
, "%d\n", ld
->ops
->get_contrast(ld
));
142 mutex_unlock(&ld
->ops_lock
);
147 static ssize_t
contrast_store(struct device
*dev
,
148 struct device_attribute
*attr
, const char *buf
, size_t count
)
151 struct lcd_device
*ld
= to_lcd_device(dev
);
152 unsigned long contrast
;
154 rc
= kstrtoul(buf
, 0, &contrast
);
160 mutex_lock(&ld
->ops_lock
);
161 if (ld
->ops
&& ld
->ops
->set_contrast
) {
162 pr_debug("set contrast to %lu\n", contrast
);
163 ld
->ops
->set_contrast(ld
, contrast
);
166 mutex_unlock(&ld
->ops_lock
);
170 static DEVICE_ATTR_RW(contrast
);
172 static ssize_t
max_contrast_show(struct device
*dev
,
173 struct device_attribute
*attr
, char *buf
)
175 struct lcd_device
*ld
= to_lcd_device(dev
);
177 return sprintf(buf
, "%d\n", ld
->props
.max_contrast
);
179 static DEVICE_ATTR_RO(max_contrast
);
181 static struct class *lcd_class
;
183 static void lcd_device_release(struct device
*dev
)
185 struct lcd_device
*ld
= to_lcd_device(dev
);
189 static struct attribute
*lcd_device_attrs
[] = {
190 &dev_attr_lcd_power
.attr
,
191 &dev_attr_contrast
.attr
,
192 &dev_attr_max_contrast
.attr
,
195 ATTRIBUTE_GROUPS(lcd_device
);
198 * lcd_device_register - register a new object of lcd_device class.
199 * @name: the name of the new object(must be the same as the name of the
200 * respective framebuffer device).
201 * @devdata: an optional pointer to be stored in the device. The
202 * methods may retrieve it by using lcd_get_data(ld).
203 * @ops: the lcd operations structure.
205 * Creates and registers a new lcd device. Returns either an ERR_PTR()
206 * or a pointer to the newly allocated device.
208 struct lcd_device
*lcd_device_register(const char *name
, struct device
*parent
,
209 void *devdata
, struct lcd_ops
*ops
)
211 struct lcd_device
*new_ld
;
214 pr_debug("lcd_device_register: name=%s\n", name
);
216 new_ld
= kzalloc(sizeof(struct lcd_device
), GFP_KERNEL
);
218 return ERR_PTR(-ENOMEM
);
220 mutex_init(&new_ld
->ops_lock
);
221 mutex_init(&new_ld
->update_lock
);
223 new_ld
->dev
.class = lcd_class
;
224 new_ld
->dev
.parent
= parent
;
225 new_ld
->dev
.release
= lcd_device_release
;
226 dev_set_name(&new_ld
->dev
, "%s", name
);
227 dev_set_drvdata(&new_ld
->dev
, devdata
);
229 rc
= device_register(&new_ld
->dev
);
231 put_device(&new_ld
->dev
);
235 rc
= lcd_register_fb(new_ld
);
237 device_unregister(&new_ld
->dev
);
245 EXPORT_SYMBOL(lcd_device_register
);
248 * lcd_device_unregister - unregisters a object of lcd_device class.
249 * @ld: the lcd device object to be unregistered and freed.
251 * Unregisters a previously registered via lcd_device_register object.
253 void lcd_device_unregister(struct lcd_device
*ld
)
258 mutex_lock(&ld
->ops_lock
);
260 mutex_unlock(&ld
->ops_lock
);
261 lcd_unregister_fb(ld
);
263 device_unregister(&ld
->dev
);
265 EXPORT_SYMBOL(lcd_device_unregister
);
267 static void devm_lcd_device_release(struct device
*dev
, void *res
)
269 struct lcd_device
*lcd
= *(struct lcd_device
**)res
;
271 lcd_device_unregister(lcd
);
274 static int devm_lcd_device_match(struct device
*dev
, void *res
, void *data
)
276 struct lcd_device
**r
= res
;
282 * devm_lcd_device_register - resource managed lcd_device_register()
283 * @dev: the device to register
284 * @name: the name of the device
285 * @parent: a pointer to the parent device
286 * @devdata: an optional pointer to be stored for private driver use
287 * @ops: the lcd operations structure
289 * @return a struct lcd on success, or an ERR_PTR on error
291 * Managed lcd_device_register(). The lcd_device returned from this function
292 * are automatically freed on driver detach. See lcd_device_register()
293 * for more information.
295 struct lcd_device
*devm_lcd_device_register(struct device
*dev
,
296 const char *name
, struct device
*parent
,
297 void *devdata
, struct lcd_ops
*ops
)
299 struct lcd_device
**ptr
, *lcd
;
301 ptr
= devres_alloc(devm_lcd_device_release
, sizeof(*ptr
), GFP_KERNEL
);
303 return ERR_PTR(-ENOMEM
);
305 lcd
= lcd_device_register(name
, parent
, devdata
, ops
);
308 devres_add(dev
, ptr
);
315 EXPORT_SYMBOL(devm_lcd_device_register
);
318 * devm_lcd_device_unregister - resource managed lcd_device_unregister()
319 * @dev: the device to unregister
320 * @ld: the lcd device to unregister
322 * Deallocated a lcd allocated with devm_lcd_device_register(). Normally
323 * this function will not need to be called and the resource management
324 * code will ensure that the resource is freed.
326 void devm_lcd_device_unregister(struct device
*dev
, struct lcd_device
*ld
)
330 rc
= devres_release(dev
, devm_lcd_device_release
,
331 devm_lcd_device_match
, ld
);
334 EXPORT_SYMBOL(devm_lcd_device_unregister
);
337 static void __exit
lcd_class_exit(void)
339 class_destroy(lcd_class
);
342 static int __init
lcd_class_init(void)
344 lcd_class
= class_create(THIS_MODULE
, "lcd");
345 if (IS_ERR(lcd_class
)) {
346 pr_warn("Unable to create backlight class; errno = %ld\n",
348 return PTR_ERR(lcd_class
);
351 lcd_class
->dev_groups
= lcd_device_groups
;
356 * if this is compiled into the kernel, we need to ensure that the
357 * class is registered before users of the class try to register lcd's
359 postcore_initcall(lcd_class_init
);
360 module_exit(lcd_class_exit
);
362 MODULE_LICENSE("GPL");
363 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
364 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");