blob: 34fb6bd798c84bb82e79b7f651a380b0547955cd [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
Jingoo Han35f96162012-05-29 15:07:16 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
17#include <linux/fb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
James Simmons3d5eead2006-12-08 02:40:47 -080020#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 ...
25 */
26static int fb_notifier_callback(struct notifier_block *self,
27 unsigned long event, void *data)
28{
29 struct lcd_device *ld;
30 struct fb_event *evdata = data;
31
32 /* If we aren't interested in this event, skip it immediately ... */
Eric Miaofaa312d2008-08-29 04:18:43 +080033 switch (event) {
34 case FB_EVENT_BLANK:
35 case FB_EVENT_MODE_CHANGE:
36 case FB_EVENT_MODE_CHANGE_ALL:
Inki Daed54ad832012-05-29 15:07:13 -070037 case FB_EARLY_EVENT_BLANK:
38 case FB_R_EARLY_EVENT_BLANK:
Eric Miaofaa312d2008-08-29 04:18:43 +080039 break;
40 default:
James Simmons3d5eead2006-12-08 02:40:47 -080041 return 0;
Eric Miaofaa312d2008-08-29 04:18:43 +080042 }
James Simmons3d5eead2006-12-08 02:40:47 -080043
44 ld = container_of(self, struct lcd_device, fb_notif);
Eric Miaofaa312d2008-08-29 04:18:43 +080045 if (!ld->ops)
46 return 0;
47
Richard Purdie599a52d2007-02-10 23:07:48 +000048 mutex_lock(&ld->ops_lock);
Eric Miaofaa312d2008-08-29 04:18:43 +080049 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
Ben Dooksb3b4dc82008-11-19 15:36:25 -080050 if (event == FB_EVENT_BLANK) {
51 if (ld->ops->set_power)
52 ld->ops->set_power(ld, *(int *)evdata->data);
Inki Daed54ad832012-05-29 15:07:13 -070053 } 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);
Ben Dooksb3b4dc82008-11-19 15:36:25 -080061 } else {
62 if (ld->ops->set_mode)
63 ld->ops->set_mode(ld, evdata->data);
64 }
Eric Miaofaa312d2008-08-29 04:18:43 +080065 }
Richard Purdie599a52d2007-02-10 23:07:48 +000066 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080067 return 0;
68}
69
70static int lcd_register_fb(struct lcd_device *ld)
71{
Jean Delvare1e0fa6b2009-10-02 11:28:18 +020072 memset(&ld->fb_notif, 0, sizeof(ld->fb_notif));
James Simmons3d5eead2006-12-08 02:40:47 -080073 ld->fb_notif.notifier_call = fb_notifier_callback;
74 return fb_register_client(&ld->fb_notif);
75}
76
77static void lcd_unregister_fb(struct lcd_device *ld)
78{
79 fb_unregister_client(&ld->fb_notif);
80}
81#else
82static int lcd_register_fb(struct lcd_device *ld)
83{
84 return 0;
85}
86
87static inline void lcd_unregister_fb(struct lcd_device *ld)
88{
89}
90#endif /* CONFIG_FB */
91
Richard Purdie655bfd72007-07-09 12:17:24 +010092static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
93 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +010096 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Richard Purdie599a52d2007-02-10 23:07:48 +000098 mutex_lock(&ld->ops_lock);
99 if (ld->ops && ld->ops->get_power)
100 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 else
102 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000103 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 return rc;
106}
107
Richard Purdie655bfd72007-07-09 12:17:24 +0100108static ssize_t lcd_store_power(struct device *dev,
109 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Jingoo Han424e06e2012-12-17 16:01:10 -0800111 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100112 struct lcd_device *ld = to_lcd_device(dev);
Jingoo Han66655762012-01-10 15:09:19 -0800113 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jingoo Han66655762012-01-10 15:09:19 -0800115 rc = kstrtoul(buf, 0, &power);
116 if (rc)
117 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jingoo Han424e06e2012-12-17 16:01:10 -0800119 rc = -ENXIO;
120
Richard Purdie599a52d2007-02-10 23:07:48 +0000121 mutex_lock(&ld->ops_lock);
122 if (ld->ops && ld->ops->set_power) {
Jingoo Han35f96162012-05-29 15:07:16 -0700123 pr_debug("set power to %lu\n", power);
Richard Purdie599a52d2007-02-10 23:07:48 +0000124 ld->ops->set_power(ld, power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700126 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000127 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 return rc;
130}
131
Richard Purdie655bfd72007-07-09 12:17:24 +0100132static ssize_t lcd_show_contrast(struct device *dev,
133 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Richard Purdie68673af2006-05-15 09:44:15 -0700135 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100136 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Richard Purdie599a52d2007-02-10 23:07:48 +0000138 mutex_lock(&ld->ops_lock);
139 if (ld->ops && ld->ops->get_contrast)
140 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
141 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return rc;
144}
145
Richard Purdie655bfd72007-07-09 12:17:24 +0100146static ssize_t lcd_store_contrast(struct device *dev,
147 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Jingoo Han424e06e2012-12-17 16:01:10 -0800149 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100150 struct lcd_device *ld = to_lcd_device(dev);
Jingoo Han66655762012-01-10 15:09:19 -0800151 unsigned long contrast;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jingoo Han66655762012-01-10 15:09:19 -0800153 rc = kstrtoul(buf, 0, &contrast);
154 if (rc)
155 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Jingoo Han424e06e2012-12-17 16:01:10 -0800157 rc = -ENXIO;
158
Richard Purdie599a52d2007-02-10 23:07:48 +0000159 mutex_lock(&ld->ops_lock);
160 if (ld->ops && ld->ops->set_contrast) {
Jingoo Han35f96162012-05-29 15:07:16 -0700161 pr_debug("set contrast to %lu\n", contrast);
Richard Purdie599a52d2007-02-10 23:07:48 +0000162 ld->ops->set_contrast(ld, contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700164 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000165 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return rc;
168}
169
Richard Purdie655bfd72007-07-09 12:17:24 +0100170static ssize_t lcd_show_max_contrast(struct device *dev,
171 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Richard Purdie655bfd72007-07-09 12:17:24 +0100173 struct lcd_device *ld = to_lcd_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Richard Purdie599a52d2007-02-10 23:07:48 +0000175 return sprintf(buf, "%d\n", ld->props.max_contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100178static struct class *lcd_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100179
180static void lcd_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct lcd_device *ld = to_lcd_device(dev);
183 kfree(ld);
184}
185
Richard Purdie655bfd72007-07-09 12:17:24 +0100186static struct device_attribute lcd_device_attributes[] = {
187 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
188 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
189 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
190 __ATTR_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/**
194 * lcd_device_register - register a new object of lcd_device class.
195 * @name: the name of the new object(must be the same as the name of the
196 * respective framebuffer device).
Richard Purdie655bfd72007-07-09 12:17:24 +0100197 * @devdata: an optional pointer to be stored in the device. The
198 * methods may retrieve it by using lcd_get_data(ld).
Richard Purdie599a52d2007-02-10 23:07:48 +0000199 * @ops: the lcd operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100201 * Creates and registers a new lcd device. Returns either an ERR_PTR()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * or a pointer to the newly allocated device.
203 */
Richard Purdie655bfd72007-07-09 12:17:24 +0100204struct lcd_device *lcd_device_register(const char *name, struct device *parent,
205 void *devdata, struct lcd_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct lcd_device *new_ld;
Richard Purdie655bfd72007-07-09 12:17:24 +0100208 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 pr_debug("lcd_device_register: name=%s\n", name);
211
Richard Purdie599a52d2007-02-10 23:07:48 +0000212 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000213 if (!new_ld)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800214 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Richard Purdie599a52d2007-02-10 23:07:48 +0000216 mutex_init(&new_ld->ops_lock);
Richard Purdie28ee0862007-02-08 22:25:09 +0000217 mutex_init(&new_ld->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Richard Purdie655bfd72007-07-09 12:17:24 +0100219 new_ld->dev.class = lcd_class;
220 new_ld->dev.parent = parent;
221 new_ld->dev.release = lcd_device_release;
Kay Sievers64dba9a2009-01-06 10:44:35 -0800222 dev_set_name(&new_ld->dev, name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100223 dev_set_drvdata(&new_ld->dev, devdata);
224
225 rc = device_register(&new_ld->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000226 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000227 kfree(new_ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return ERR_PTR(rc);
229 }
230
James Simmons3d5eead2006-12-08 02:40:47 -0800231 rc = lcd_register_fb(new_ld);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000232 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100233 device_unregister(&new_ld->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000234 return ERR_PTR(rc);
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Richard Purdie655bfd72007-07-09 12:17:24 +0100237 new_ld->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 return new_ld;
240}
241EXPORT_SYMBOL(lcd_device_register);
242
243/**
244 * lcd_device_unregister - unregisters a object of lcd_device class.
245 * @ld: the lcd device object to be unregistered and freed.
246 *
247 * Unregisters a previously registered via lcd_device_register object.
248 */
249void lcd_device_unregister(struct lcd_device *ld)
250{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!ld)
252 return;
253
Richard Purdie599a52d2007-02-10 23:07:48 +0000254 mutex_lock(&ld->ops_lock);
255 ld->ops = NULL;
256 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -0800257 lcd_unregister_fb(ld);
Richard Purdie655bfd72007-07-09 12:17:24 +0100258
259 device_unregister(&ld->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261EXPORT_SYMBOL(lcd_device_unregister);
262
263static void __exit lcd_class_exit(void)
264{
Richard Purdie655bfd72007-07-09 12:17:24 +0100265 class_destroy(lcd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static int __init lcd_class_init(void)
269{
Richard Purdie655bfd72007-07-09 12:17:24 +0100270 lcd_class = class_create(THIS_MODULE, "lcd");
271 if (IS_ERR(lcd_class)) {
Jingoo Han35f96162012-05-29 15:07:16 -0700272 pr_warn("Unable to create backlight class; errno = %ld\n",
273 PTR_ERR(lcd_class));
Richard Purdie655bfd72007-07-09 12:17:24 +0100274 return PTR_ERR(lcd_class);
275 }
276
277 lcd_class->dev_attrs = lcd_device_attributes;
278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * if this is compiled into the kernel, we need to ensure that the
283 * class is registered before users of the class try to register lcd's
284 */
285postcore_initcall(lcd_class_init);
286module_exit(lcd_class_exit);
287
288MODULE_LICENSE("GPL");
289MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
290MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");