blob: 8e1731d3b2283e311bcad1d6f10475901467cda2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * LCD Lowlevel Control Abstraction
3 *
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
5 *
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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>
15#include <linux/fb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
James Simmons3d5eead2006-12-08 02:40:47 -080017#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 ...
22 */
23static int fb_notifier_callback(struct notifier_block *self,
24 unsigned long event, void *data)
25{
26 struct lcd_device *ld;
27 struct fb_event *evdata = data;
28
29 /* If we aren't interested in this event, skip it immediately ... */
Eric Miaofaa312d2008-08-29 04:18:43 +080030 switch (event) {
31 case FB_EVENT_BLANK:
32 case FB_EVENT_MODE_CHANGE:
33 case FB_EVENT_MODE_CHANGE_ALL:
34 break;
35 default:
James Simmons3d5eead2006-12-08 02:40:47 -080036 return 0;
Eric Miaofaa312d2008-08-29 04:18:43 +080037 }
James Simmons3d5eead2006-12-08 02:40:47 -080038
39 ld = container_of(self, struct lcd_device, fb_notif);
Eric Miaofaa312d2008-08-29 04:18:43 +080040 if (!ld->ops)
41 return 0;
42
Richard Purdie599a52d2007-02-10 23:07:48 +000043 mutex_lock(&ld->ops_lock);
Eric Miaofaa312d2008-08-29 04:18:43 +080044 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
45 if (event == FB_EVENT_BLANK)
Richard Purdie599a52d2007-02-10 23:07:48 +000046 ld->ops->set_power(ld, *(int *)evdata->data);
Eric Miaofaa312d2008-08-29 04:18:43 +080047 else
48 ld->ops->set_mode(ld, evdata->data);
49 }
Richard Purdie599a52d2007-02-10 23:07:48 +000050 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080051 return 0;
52}
53
54static int lcd_register_fb(struct lcd_device *ld)
55{
56 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
57 ld->fb_notif.notifier_call = fb_notifier_callback;
58 return fb_register_client(&ld->fb_notif);
59}
60
61static void lcd_unregister_fb(struct lcd_device *ld)
62{
63 fb_unregister_client(&ld->fb_notif);
64}
65#else
66static int lcd_register_fb(struct lcd_device *ld)
67{
68 return 0;
69}
70
71static inline void lcd_unregister_fb(struct lcd_device *ld)
72{
73}
74#endif /* CONFIG_FB */
75
Richard Purdie655bfd72007-07-09 12:17:24 +010076static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
77 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +010080 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Richard Purdie599a52d2007-02-10 23:07:48 +000082 mutex_lock(&ld->ops_lock);
83 if (ld->ops && ld->ops->get_power)
84 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 else
86 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +000087 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return rc;
90}
91
Richard Purdie655bfd72007-07-09 12:17:24 +010092static ssize_t lcd_store_power(struct device *dev,
93 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Richard Purdie68673af2006-05-15 09:44:15 -070095 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 char *endp;
Richard Purdie655bfd72007-07-09 12:17:24 +010097 struct lcd_device *ld = to_lcd_device(dev);
Richard Purdie68673af2006-05-15 09:44:15 -070098 int power = simple_strtoul(buf, &endp, 0);
99 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Richard Purdie68673af2006-05-15 09:44:15 -0700101 if (*endp && isspace(*endp))
102 size++;
103 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -EINVAL;
105
Richard Purdie599a52d2007-02-10 23:07:48 +0000106 mutex_lock(&ld->ops_lock);
107 if (ld->ops && ld->ops->set_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 pr_debug("lcd: set power to %d\n", power);
Richard Purdie599a52d2007-02-10 23:07:48 +0000109 ld->ops->set_power(ld, power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700111 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000112 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 return rc;
115}
116
Richard Purdie655bfd72007-07-09 12:17:24 +0100117static ssize_t lcd_show_contrast(struct device *dev,
118 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Richard Purdie68673af2006-05-15 09:44:15 -0700120 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100121 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Richard Purdie599a52d2007-02-10 23:07:48 +0000123 mutex_lock(&ld->ops_lock);
124 if (ld->ops && ld->ops->get_contrast)
125 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
126 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 return rc;
129}
130
Richard Purdie655bfd72007-07-09 12:17:24 +0100131static ssize_t lcd_store_contrast(struct device *dev,
132 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Richard Purdie68673af2006-05-15 09:44:15 -0700134 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 char *endp;
Richard Purdie655bfd72007-07-09 12:17:24 +0100136 struct lcd_device *ld = to_lcd_device(dev);
Richard Purdie68673af2006-05-15 09:44:15 -0700137 int contrast = simple_strtoul(buf, &endp, 0);
138 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Richard Purdie68673af2006-05-15 09:44:15 -0700140 if (*endp && isspace(*endp))
141 size++;
142 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -EINVAL;
144
Richard Purdie599a52d2007-02-10 23:07:48 +0000145 mutex_lock(&ld->ops_lock);
146 if (ld->ops && ld->ops->set_contrast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 pr_debug("lcd: set contrast to %d\n", contrast);
Richard Purdie599a52d2007-02-10 23:07:48 +0000148 ld->ops->set_contrast(ld, contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700150 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000151 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return rc;
154}
155
Richard Purdie655bfd72007-07-09 12:17:24 +0100156static ssize_t lcd_show_max_contrast(struct device *dev,
157 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Richard Purdie655bfd72007-07-09 12:17:24 +0100159 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Richard Purdie599a52d2007-02-10 23:07:48 +0000161 return sprintf(buf, "%d\n", ld->props.max_contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100164static struct class *lcd_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100165
166static void lcd_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct lcd_device *ld = to_lcd_device(dev);
169 kfree(ld);
170}
171
Richard Purdie655bfd72007-07-09 12:17:24 +0100172static struct device_attribute lcd_device_attributes[] = {
173 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
174 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
175 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
176 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/**
180 * lcd_device_register - register a new object of lcd_device class.
181 * @name: the name of the new object(must be the same as the name of the
182 * respective framebuffer device).
Richard Purdie655bfd72007-07-09 12:17:24 +0100183 * @devdata: an optional pointer to be stored in the device. The
184 * methods may retrieve it by using lcd_get_data(ld).
Richard Purdie599a52d2007-02-10 23:07:48 +0000185 * @ops: the lcd operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100187 * Creates and registers a new lcd device. Returns either an ERR_PTR()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * or a pointer to the newly allocated device.
189 */
Richard Purdie655bfd72007-07-09 12:17:24 +0100190struct lcd_device *lcd_device_register(const char *name, struct device *parent,
191 void *devdata, struct lcd_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct lcd_device *new_ld;
Richard Purdie655bfd72007-07-09 12:17:24 +0100194 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 pr_debug("lcd_device_register: name=%s\n", name);
197
Richard Purdie599a52d2007-02-10 23:07:48 +0000198 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000199 if (!new_ld)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800200 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Richard Purdie599a52d2007-02-10 23:07:48 +0000202 mutex_init(&new_ld->ops_lock);
Richard Purdie28ee0862007-02-08 22:25:09 +0000203 mutex_init(&new_ld->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Richard Purdie655bfd72007-07-09 12:17:24 +0100205 new_ld->dev.class = lcd_class;
206 new_ld->dev.parent = parent;
207 new_ld->dev.release = lcd_device_release;
208 strlcpy(new_ld->dev.bus_id, name, BUS_ID_SIZE);
209 dev_set_drvdata(&new_ld->dev, devdata);
210
211 rc = device_register(&new_ld->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000212 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000213 kfree(new_ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return ERR_PTR(rc);
215 }
216
James Simmons3d5eead2006-12-08 02:40:47 -0800217 rc = lcd_register_fb(new_ld);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000218 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100219 device_unregister(&new_ld->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000220 return ERR_PTR(rc);
221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Richard Purdie655bfd72007-07-09 12:17:24 +0100223 new_ld->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 return new_ld;
226}
227EXPORT_SYMBOL(lcd_device_register);
228
229/**
230 * lcd_device_unregister - unregisters a object of lcd_device class.
231 * @ld: the lcd device object to be unregistered and freed.
232 *
233 * Unregisters a previously registered via lcd_device_register object.
234 */
235void lcd_device_unregister(struct lcd_device *ld)
236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!ld)
238 return;
239
Richard Purdie599a52d2007-02-10 23:07:48 +0000240 mutex_lock(&ld->ops_lock);
241 ld->ops = NULL;
242 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -0800243 lcd_unregister_fb(ld);
Richard Purdie655bfd72007-07-09 12:17:24 +0100244
245 device_unregister(&ld->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247EXPORT_SYMBOL(lcd_device_unregister);
248
249static void __exit lcd_class_exit(void)
250{
Richard Purdie655bfd72007-07-09 12:17:24 +0100251 class_destroy(lcd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254static int __init lcd_class_init(void)
255{
Richard Purdie655bfd72007-07-09 12:17:24 +0100256 lcd_class = class_create(THIS_MODULE, "lcd");
257 if (IS_ERR(lcd_class)) {
258 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
259 PTR_ERR(lcd_class));
260 return PTR_ERR(lcd_class);
261 }
262
263 lcd_class->dev_attrs = lcd_device_attributes;
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * if this is compiled into the kernel, we need to ensure that the
269 * class is registered before users of the class try to register lcd's
270 */
271postcore_initcall(lcd_class_init);
272module_exit(lcd_class_exit);
273
274MODULE_LICENSE("GPL");
275MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
276MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");