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>
16 #include <linux/slab.h>
18 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
19 defined(CONFIG_LCD_CLASS_DEVICE_MODULE))
20 /* This callback gets called when something important happens inside a
21 * framebuffer driver. We're looking if that important event is blanking,
22 * and if it is, we're switching lcd power as well ...
24 static int fb_notifier_callback(struct notifier_block
*self
,
25 unsigned long event
, void *data
)
27 struct lcd_device
*ld
;
28 struct fb_event
*evdata
= data
;
30 /* If we aren't interested in this event, skip it immediately ... */
33 case FB_EVENT_MODE_CHANGE
:
34 case FB_EVENT_MODE_CHANGE_ALL
:
40 ld
= container_of(self
, struct lcd_device
, fb_notif
);
44 mutex_lock(&ld
->ops_lock
);
45 if (!ld
->ops
->check_fb
|| ld
->ops
->check_fb(ld
, evdata
->info
)) {
46 if (event
== FB_EVENT_BLANK
) {
47 if (ld
->ops
->set_power
)
48 ld
->ops
->set_power(ld
, *(int *)evdata
->data
);
50 if (ld
->ops
->set_mode
)
51 ld
->ops
->set_mode(ld
, evdata
->data
);
54 mutex_unlock(&ld
->ops_lock
);
58 static int lcd_register_fb(struct lcd_device
*ld
)
60 memset(&ld
->fb_notif
, 0, sizeof(ld
->fb_notif
));
61 ld
->fb_notif
.notifier_call
= fb_notifier_callback
;
62 return fb_register_client(&ld
->fb_notif
);
65 static void lcd_unregister_fb(struct lcd_device
*ld
)
67 fb_unregister_client(&ld
->fb_notif
);
70 static int lcd_register_fb(struct lcd_device
*ld
)
75 static inline void lcd_unregister_fb(struct lcd_device
*ld
)
78 #endif /* CONFIG_FB */
80 static ssize_t
lcd_show_power(struct device
*dev
, struct device_attribute
*attr
,
84 struct lcd_device
*ld
= to_lcd_device(dev
);
86 mutex_lock(&ld
->ops_lock
);
87 if (ld
->ops
&& ld
->ops
->get_power
)
88 rc
= sprintf(buf
, "%d\n", ld
->ops
->get_power(ld
));
91 mutex_unlock(&ld
->ops_lock
);
96 static ssize_t
lcd_store_power(struct device
*dev
,
97 struct device_attribute
*attr
, const char *buf
, size_t count
)
100 struct lcd_device
*ld
= to_lcd_device(dev
);
103 rc
= kstrtoul(buf
, 0, &power
);
107 mutex_lock(&ld
->ops_lock
);
108 if (ld
->ops
&& ld
->ops
->set_power
) {
109 pr_debug("lcd: set power to %lu\n", power
);
110 ld
->ops
->set_power(ld
, power
);
113 mutex_unlock(&ld
->ops_lock
);
118 static ssize_t
lcd_show_contrast(struct device
*dev
,
119 struct device_attribute
*attr
, char *buf
)
122 struct lcd_device
*ld
= to_lcd_device(dev
);
124 mutex_lock(&ld
->ops_lock
);
125 if (ld
->ops
&& ld
->ops
->get_contrast
)
126 rc
= sprintf(buf
, "%d\n", ld
->ops
->get_contrast(ld
));
127 mutex_unlock(&ld
->ops_lock
);
132 static ssize_t
lcd_store_contrast(struct device
*dev
,
133 struct device_attribute
*attr
, const char *buf
, size_t count
)
136 struct lcd_device
*ld
= to_lcd_device(dev
);
137 unsigned long contrast
;
139 rc
= kstrtoul(buf
, 0, &contrast
);
143 mutex_lock(&ld
->ops_lock
);
144 if (ld
->ops
&& ld
->ops
->set_contrast
) {
145 pr_debug("lcd: set contrast to %lu\n", contrast
);
146 ld
->ops
->set_contrast(ld
, contrast
);
149 mutex_unlock(&ld
->ops_lock
);
154 static ssize_t
lcd_show_max_contrast(struct device
*dev
,
155 struct device_attribute
*attr
, char *buf
)
157 struct lcd_device
*ld
= to_lcd_device(dev
);
159 return sprintf(buf
, "%d\n", ld
->props
.max_contrast
);
162 static struct class *lcd_class
;
164 static void lcd_device_release(struct device
*dev
)
166 struct lcd_device
*ld
= to_lcd_device(dev
);
170 static struct device_attribute lcd_device_attributes
[] = {
171 __ATTR(lcd_power
, 0644, lcd_show_power
, lcd_store_power
),
172 __ATTR(contrast
, 0644, lcd_show_contrast
, lcd_store_contrast
),
173 __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 device. The
182 * methods may retrieve it by using lcd_get_data(ld).
183 * @ops: the lcd operations structure.
185 * Creates and registers a new lcd 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
, struct device
*parent
,
189 void *devdata
, struct lcd_ops
*ops
)
191 struct lcd_device
*new_ld
;
194 pr_debug("lcd_device_register: name=%s\n", name
);
196 new_ld
= kzalloc(sizeof(struct lcd_device
), GFP_KERNEL
);
198 return ERR_PTR(-ENOMEM
);
200 mutex_init(&new_ld
->ops_lock
);
201 mutex_init(&new_ld
->update_lock
);
203 new_ld
->dev
.class = lcd_class
;
204 new_ld
->dev
.parent
= parent
;
205 new_ld
->dev
.release
= lcd_device_release
;
206 dev_set_name(&new_ld
->dev
, name
);
207 dev_set_drvdata(&new_ld
->dev
, devdata
);
209 rc
= device_register(&new_ld
->dev
);
215 rc
= lcd_register_fb(new_ld
);
217 device_unregister(&new_ld
->dev
);
225 EXPORT_SYMBOL(lcd_device_register
);
228 * lcd_device_unregister - unregisters a object of lcd_device class.
229 * @ld: the lcd device object to be unregistered and freed.
231 * Unregisters a previously registered via lcd_device_register object.
233 void lcd_device_unregister(struct lcd_device
*ld
)
238 mutex_lock(&ld
->ops_lock
);
240 mutex_unlock(&ld
->ops_lock
);
241 lcd_unregister_fb(ld
);
243 device_unregister(&ld
->dev
);
245 EXPORT_SYMBOL(lcd_device_unregister
);
247 static void __exit
lcd_class_exit(void)
249 class_destroy(lcd_class
);
252 static int __init
lcd_class_init(void)
254 lcd_class
= class_create(THIS_MODULE
, "lcd");
255 if (IS_ERR(lcd_class
)) {
256 printk(KERN_WARNING
"Unable to create backlight class; errno = %ld\n",
258 return PTR_ERR(lcd_class
);
261 lcd_class
->dev_attrs
= lcd_device_attributes
;
266 * if this is compiled into the kernel, we need to ensure that the
267 * class is registered before users of the class try to register lcd's
269 postcore_initcall(lcd_class_init
);
270 module_exit(lcd_class_exit
);
272 MODULE_LICENSE("GPL");
273 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
274 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");