blob: 1c298d5bf3afbb9ed5bcb2bf62957d21f57b28ec [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
James Simmons3d5eead2006-12-08 02:40:47 -080018#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 ...
23 */
24static int fb_notifier_callback(struct notifier_block *self,
25 unsigned long event, void *data)
26{
27 struct lcd_device *ld;
28 struct fb_event *evdata = data;
29
30 /* If we aren't interested in this event, skip it immediately ... */
Eric Miaofaa312d2008-08-29 04:18:43 +080031 switch (event) {
32 case FB_EVENT_BLANK:
33 case FB_EVENT_MODE_CHANGE:
34 case FB_EVENT_MODE_CHANGE_ALL:
Inki Daed54ad832012-05-29 15:07:13 -070035 case FB_EARLY_EVENT_BLANK:
36 case FB_R_EARLY_EVENT_BLANK:
Eric Miaofaa312d2008-08-29 04:18:43 +080037 break;
38 default:
James Simmons3d5eead2006-12-08 02:40:47 -080039 return 0;
Eric Miaofaa312d2008-08-29 04:18:43 +080040 }
James Simmons3d5eead2006-12-08 02:40:47 -080041
42 ld = container_of(self, struct lcd_device, fb_notif);
Eric Miaofaa312d2008-08-29 04:18:43 +080043 if (!ld->ops)
44 return 0;
45
Richard Purdie599a52d2007-02-10 23:07:48 +000046 mutex_lock(&ld->ops_lock);
Eric Miaofaa312d2008-08-29 04:18:43 +080047 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
Ben Dooksb3b4dc82008-11-19 15:36:25 -080048 if (event == FB_EVENT_BLANK) {
49 if (ld->ops->set_power)
50 ld->ops->set_power(ld, *(int *)evdata->data);
Inki Daed54ad832012-05-29 15:07:13 -070051 } else if (event == FB_EARLY_EVENT_BLANK) {
52 if (ld->ops->early_set_power)
53 ld->ops->early_set_power(ld,
54 *(int *)evdata->data);
55 } else if (event == FB_R_EARLY_EVENT_BLANK) {
56 if (ld->ops->r_early_set_power)
57 ld->ops->r_early_set_power(ld,
58 *(int *)evdata->data);
Ben Dooksb3b4dc82008-11-19 15:36:25 -080059 } else {
60 if (ld->ops->set_mode)
61 ld->ops->set_mode(ld, evdata->data);
62 }
Eric Miaofaa312d2008-08-29 04:18:43 +080063 }
Richard Purdie599a52d2007-02-10 23:07:48 +000064 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080065 return 0;
66}
67
68static int lcd_register_fb(struct lcd_device *ld)
69{
Jean Delvare1e0fa6b2009-10-02 11:28:18 +020070 memset(&ld->fb_notif, 0, sizeof(ld->fb_notif));
James Simmons3d5eead2006-12-08 02:40:47 -080071 ld->fb_notif.notifier_call = fb_notifier_callback;
72 return fb_register_client(&ld->fb_notif);
73}
74
75static void lcd_unregister_fb(struct lcd_device *ld)
76{
77 fb_unregister_client(&ld->fb_notif);
78}
79#else
80static int lcd_register_fb(struct lcd_device *ld)
81{
82 return 0;
83}
84
85static inline void lcd_unregister_fb(struct lcd_device *ld)
86{
87}
88#endif /* CONFIG_FB */
89
Richard Purdie655bfd72007-07-09 12:17:24 +010090static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
91 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +010094 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Richard Purdie599a52d2007-02-10 23:07:48 +000096 mutex_lock(&ld->ops_lock);
97 if (ld->ops && ld->ops->get_power)
98 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 else
100 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000101 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 return rc;
104}
105
Richard Purdie655bfd72007-07-09 12:17:24 +0100106static ssize_t lcd_store_power(struct device *dev,
107 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Richard Purdie68673af2006-05-15 09:44:15 -0700109 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100110 struct lcd_device *ld = to_lcd_device(dev);
Jingoo Han66655762012-01-10 15:09:19 -0800111 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jingoo Han66655762012-01-10 15:09:19 -0800113 rc = kstrtoul(buf, 0, &power);
114 if (rc)
115 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Richard Purdie599a52d2007-02-10 23:07:48 +0000117 mutex_lock(&ld->ops_lock);
118 if (ld->ops && ld->ops->set_power) {
Jingoo Han66655762012-01-10 15:09:19 -0800119 pr_debug("lcd: set power to %lu\n", power);
Richard Purdie599a52d2007-02-10 23:07:48 +0000120 ld->ops->set_power(ld, power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700122 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000123 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return rc;
126}
127
Richard Purdie655bfd72007-07-09 12:17:24 +0100128static ssize_t lcd_show_contrast(struct device *dev,
129 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Richard Purdie68673af2006-05-15 09:44:15 -0700131 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100132 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Richard Purdie599a52d2007-02-10 23:07:48 +0000134 mutex_lock(&ld->ops_lock);
135 if (ld->ops && ld->ops->get_contrast)
136 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
137 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 return rc;
140}
141
Richard Purdie655bfd72007-07-09 12:17:24 +0100142static ssize_t lcd_store_contrast(struct device *dev,
143 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Richard Purdie68673af2006-05-15 09:44:15 -0700145 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100146 struct lcd_device *ld = to_lcd_device(dev);
Jingoo Han66655762012-01-10 15:09:19 -0800147 unsigned long contrast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jingoo Han66655762012-01-10 15:09:19 -0800149 rc = kstrtoul(buf, 0, &contrast);
150 if (rc)
151 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Richard Purdie599a52d2007-02-10 23:07:48 +0000153 mutex_lock(&ld->ops_lock);
154 if (ld->ops && ld->ops->set_contrast) {
Jingoo Han66655762012-01-10 15:09:19 -0800155 pr_debug("lcd: set contrast to %lu\n", contrast);
Richard Purdie599a52d2007-02-10 23:07:48 +0000156 ld->ops->set_contrast(ld, contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700158 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000159 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return rc;
162}
163
Richard Purdie655bfd72007-07-09 12:17:24 +0100164static ssize_t lcd_show_max_contrast(struct device *dev,
165 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Richard Purdie655bfd72007-07-09 12:17:24 +0100167 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Richard Purdie599a52d2007-02-10 23:07:48 +0000169 return sprintf(buf, "%d\n", ld->props.max_contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100172static struct class *lcd_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100173
174static void lcd_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct lcd_device *ld = to_lcd_device(dev);
177 kfree(ld);
178}
179
Richard Purdie655bfd72007-07-09 12:17:24 +0100180static struct device_attribute lcd_device_attributes[] = {
181 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
182 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
183 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
184 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/**
188 * lcd_device_register - register a new object of lcd_device class.
189 * @name: the name of the new object(must be the same as the name of the
190 * respective framebuffer device).
Richard Purdie655bfd72007-07-09 12:17:24 +0100191 * @devdata: an optional pointer to be stored in the device. The
192 * methods may retrieve it by using lcd_get_data(ld).
Richard Purdie599a52d2007-02-10 23:07:48 +0000193 * @ops: the lcd operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100195 * Creates and registers a new lcd device. Returns either an ERR_PTR()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * or a pointer to the newly allocated device.
197 */
Richard Purdie655bfd72007-07-09 12:17:24 +0100198struct lcd_device *lcd_device_register(const char *name, struct device *parent,
199 void *devdata, struct lcd_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct lcd_device *new_ld;
Richard Purdie655bfd72007-07-09 12:17:24 +0100202 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 pr_debug("lcd_device_register: name=%s\n", name);
205
Richard Purdie599a52d2007-02-10 23:07:48 +0000206 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000207 if (!new_ld)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800208 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Richard Purdie599a52d2007-02-10 23:07:48 +0000210 mutex_init(&new_ld->ops_lock);
Richard Purdie28ee0862007-02-08 22:25:09 +0000211 mutex_init(&new_ld->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Richard Purdie655bfd72007-07-09 12:17:24 +0100213 new_ld->dev.class = lcd_class;
214 new_ld->dev.parent = parent;
215 new_ld->dev.release = lcd_device_release;
Kay Sievers64dba9a2009-01-06 10:44:35 -0800216 dev_set_name(&new_ld->dev, name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100217 dev_set_drvdata(&new_ld->dev, devdata);
218
219 rc = device_register(&new_ld->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000220 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000221 kfree(new_ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return ERR_PTR(rc);
223 }
224
James Simmons3d5eead2006-12-08 02:40:47 -0800225 rc = lcd_register_fb(new_ld);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000226 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100227 device_unregister(&new_ld->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000228 return ERR_PTR(rc);
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Richard Purdie655bfd72007-07-09 12:17:24 +0100231 new_ld->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return new_ld;
234}
235EXPORT_SYMBOL(lcd_device_register);
236
237/**
238 * lcd_device_unregister - unregisters a object of lcd_device class.
239 * @ld: the lcd device object to be unregistered and freed.
240 *
241 * Unregisters a previously registered via lcd_device_register object.
242 */
243void lcd_device_unregister(struct lcd_device *ld)
244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (!ld)
246 return;
247
Richard Purdie599a52d2007-02-10 23:07:48 +0000248 mutex_lock(&ld->ops_lock);
249 ld->ops = NULL;
250 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -0800251 lcd_unregister_fb(ld);
Richard Purdie655bfd72007-07-09 12:17:24 +0100252
253 device_unregister(&ld->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255EXPORT_SYMBOL(lcd_device_unregister);
256
257static void __exit lcd_class_exit(void)
258{
Richard Purdie655bfd72007-07-09 12:17:24 +0100259 class_destroy(lcd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262static int __init lcd_class_init(void)
263{
Richard Purdie655bfd72007-07-09 12:17:24 +0100264 lcd_class = class_create(THIS_MODULE, "lcd");
265 if (IS_ERR(lcd_class)) {
266 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
267 PTR_ERR(lcd_class));
268 return PTR_ERR(lcd_class);
269 }
270
271 lcd_class->dev_attrs = lcd_device_attributes;
272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/*
276 * if this is compiled into the kernel, we need to ensure that the
277 * class is registered before users of the class try to register lcd's
278 */
279postcore_initcall(lcd_class_init);
280module_exit(lcd_class_exit);
281
282MODULE_LICENSE("GPL");
283MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
284MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");