blob: b6449470106cd47e8db704edfd074c53dde8ec4b [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)) {
Ben Dooksb3b4dc82008-11-19 15:36:25 -080045 if (event == FB_EVENT_BLANK) {
46 if (ld->ops->set_power)
47 ld->ops->set_power(ld, *(int *)evdata->data);
48 } else {
49 if (ld->ops->set_mode)
50 ld->ops->set_mode(ld, evdata->data);
51 }
Eric Miaofaa312d2008-08-29 04:18:43 +080052 }
Richard Purdie599a52d2007-02-10 23:07:48 +000053 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080054 return 0;
55}
56
57static int lcd_register_fb(struct lcd_device *ld)
58{
59 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
60 ld->fb_notif.notifier_call = fb_notifier_callback;
61 return fb_register_client(&ld->fb_notif);
62}
63
64static void lcd_unregister_fb(struct lcd_device *ld)
65{
66 fb_unregister_client(&ld->fb_notif);
67}
68#else
69static int lcd_register_fb(struct lcd_device *ld)
70{
71 return 0;
72}
73
74static inline void lcd_unregister_fb(struct lcd_device *ld)
75{
76}
77#endif /* CONFIG_FB */
78
Richard Purdie655bfd72007-07-09 12:17:24 +010079static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
80 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +010083 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Richard Purdie599a52d2007-02-10 23:07:48 +000085 mutex_lock(&ld->ops_lock);
86 if (ld->ops && ld->ops->get_power)
87 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 else
89 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +000090 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 return rc;
93}
94
Richard Purdie655bfd72007-07-09 12:17:24 +010095static ssize_t lcd_store_power(struct device *dev,
96 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Richard Purdie68673af2006-05-15 09:44:15 -070098 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 char *endp;
Richard Purdie655bfd72007-07-09 12:17:24 +0100100 struct lcd_device *ld = to_lcd_device(dev);
Richard Purdie68673af2006-05-15 09:44:15 -0700101 int power = simple_strtoul(buf, &endp, 0);
102 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Richard Purdie68673af2006-05-15 09:44:15 -0700104 if (*endp && isspace(*endp))
105 size++;
106 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EINVAL;
108
Richard Purdie599a52d2007-02-10 23:07:48 +0000109 mutex_lock(&ld->ops_lock);
110 if (ld->ops && ld->ops->set_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 pr_debug("lcd: set power to %d\n", power);
Richard Purdie599a52d2007-02-10 23:07:48 +0000112 ld->ops->set_power(ld, power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700114 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000115 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 return rc;
118}
119
Richard Purdie655bfd72007-07-09 12:17:24 +0100120static ssize_t lcd_show_contrast(struct device *dev,
121 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Richard Purdie68673af2006-05-15 09:44:15 -0700123 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100124 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Richard Purdie599a52d2007-02-10 23:07:48 +0000126 mutex_lock(&ld->ops_lock);
127 if (ld->ops && ld->ops->get_contrast)
128 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
129 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return rc;
132}
133
Richard Purdie655bfd72007-07-09 12:17:24 +0100134static ssize_t lcd_store_contrast(struct device *dev,
135 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Richard Purdie68673af2006-05-15 09:44:15 -0700137 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 char *endp;
Richard Purdie655bfd72007-07-09 12:17:24 +0100139 struct lcd_device *ld = to_lcd_device(dev);
Richard Purdie68673af2006-05-15 09:44:15 -0700140 int contrast = simple_strtoul(buf, &endp, 0);
141 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Richard Purdie68673af2006-05-15 09:44:15 -0700143 if (*endp && isspace(*endp))
144 size++;
145 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -EINVAL;
147
Richard Purdie599a52d2007-02-10 23:07:48 +0000148 mutex_lock(&ld->ops_lock);
149 if (ld->ops && ld->ops->set_contrast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 pr_debug("lcd: set contrast to %d\n", contrast);
Richard Purdie599a52d2007-02-10 23:07:48 +0000151 ld->ops->set_contrast(ld, contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700153 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000154 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return rc;
157}
158
Richard Purdie655bfd72007-07-09 12:17:24 +0100159static ssize_t lcd_show_max_contrast(struct device *dev,
160 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Richard Purdie655bfd72007-07-09 12:17:24 +0100162 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Richard Purdie599a52d2007-02-10 23:07:48 +0000164 return sprintf(buf, "%d\n", ld->props.max_contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100167static struct class *lcd_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100168
169static void lcd_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct lcd_device *ld = to_lcd_device(dev);
172 kfree(ld);
173}
174
Richard Purdie655bfd72007-07-09 12:17:24 +0100175static struct device_attribute lcd_device_attributes[] = {
176 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
177 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
178 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
179 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/**
183 * lcd_device_register - register a new object of lcd_device class.
184 * @name: the name of the new object(must be the same as the name of the
185 * respective framebuffer device).
Richard Purdie655bfd72007-07-09 12:17:24 +0100186 * @devdata: an optional pointer to be stored in the device. The
187 * methods may retrieve it by using lcd_get_data(ld).
Richard Purdie599a52d2007-02-10 23:07:48 +0000188 * @ops: the lcd operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100190 * Creates and registers a new lcd device. Returns either an ERR_PTR()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * or a pointer to the newly allocated device.
192 */
Richard Purdie655bfd72007-07-09 12:17:24 +0100193struct lcd_device *lcd_device_register(const char *name, struct device *parent,
194 void *devdata, struct lcd_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct lcd_device *new_ld;
Richard Purdie655bfd72007-07-09 12:17:24 +0100197 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 pr_debug("lcd_device_register: name=%s\n", name);
200
Richard Purdie599a52d2007-02-10 23:07:48 +0000201 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000202 if (!new_ld)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800203 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Richard Purdie599a52d2007-02-10 23:07:48 +0000205 mutex_init(&new_ld->ops_lock);
Richard Purdie28ee0862007-02-08 22:25:09 +0000206 mutex_init(&new_ld->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Richard Purdie655bfd72007-07-09 12:17:24 +0100208 new_ld->dev.class = lcd_class;
209 new_ld->dev.parent = parent;
210 new_ld->dev.release = lcd_device_release;
Kay Sievers64dba9a2009-01-06 10:44:35 -0800211 dev_set_name(&new_ld->dev, name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100212 dev_set_drvdata(&new_ld->dev, devdata);
213
214 rc = device_register(&new_ld->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000215 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000216 kfree(new_ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return ERR_PTR(rc);
218 }
219
James Simmons3d5eead2006-12-08 02:40:47 -0800220 rc = lcd_register_fb(new_ld);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000221 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100222 device_unregister(&new_ld->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000223 return ERR_PTR(rc);
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Richard Purdie655bfd72007-07-09 12:17:24 +0100226 new_ld->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 return new_ld;
229}
230EXPORT_SYMBOL(lcd_device_register);
231
232/**
233 * lcd_device_unregister - unregisters a object of lcd_device class.
234 * @ld: the lcd device object to be unregistered and freed.
235 *
236 * Unregisters a previously registered via lcd_device_register object.
237 */
238void lcd_device_unregister(struct lcd_device *ld)
239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!ld)
241 return;
242
Richard Purdie599a52d2007-02-10 23:07:48 +0000243 mutex_lock(&ld->ops_lock);
244 ld->ops = NULL;
245 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -0800246 lcd_unregister_fb(ld);
Richard Purdie655bfd72007-07-09 12:17:24 +0100247
248 device_unregister(&ld->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250EXPORT_SYMBOL(lcd_device_unregister);
251
252static void __exit lcd_class_exit(void)
253{
Richard Purdie655bfd72007-07-09 12:17:24 +0100254 class_destroy(lcd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static int __init lcd_class_init(void)
258{
Richard Purdie655bfd72007-07-09 12:17:24 +0100259 lcd_class = class_create(THIS_MODULE, "lcd");
260 if (IS_ERR(lcd_class)) {
261 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
262 PTR_ERR(lcd_class));
263 return PTR_ERR(lcd_class);
264 }
265
266 lcd_class->dev_attrs = lcd_device_attributes;
267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*
271 * if this is compiled into the kernel, we need to ensure that the
272 * class is registered before users of the class try to register lcd's
273 */
274postcore_initcall(lcd_class_init);
275module_exit(lcd_class_exit);
276
277MODULE_LICENSE("GPL");
278MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
279MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");