blob: 6ef8f0a7a137612deb25cbb3377f49e31e8aa93e [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 ... */
30 if (event != FB_EVENT_BLANK)
31 return 0;
32
33 ld = container_of(self, struct lcd_device, fb_notif);
Richard Purdie599a52d2007-02-10 23:07:48 +000034 mutex_lock(&ld->ops_lock);
35 if (ld->ops)
36 if (!ld->ops->check_fb || ld->ops->check_fb(evdata->info))
37 ld->ops->set_power(ld, *(int *)evdata->data);
38 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080039 return 0;
40}
41
42static int lcd_register_fb(struct lcd_device *ld)
43{
44 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
45 ld->fb_notif.notifier_call = fb_notifier_callback;
46 return fb_register_client(&ld->fb_notif);
47}
48
49static void lcd_unregister_fb(struct lcd_device *ld)
50{
51 fb_unregister_client(&ld->fb_notif);
52}
53#else
54static int lcd_register_fb(struct lcd_device *ld)
55{
56 return 0;
57}
58
59static inline void lcd_unregister_fb(struct lcd_device *ld)
60{
61}
62#endif /* CONFIG_FB */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static ssize_t lcd_show_power(struct class_device *cdev, char *buf)
65{
66 int rc;
67 struct lcd_device *ld = to_lcd_device(cdev);
68
Richard Purdie599a52d2007-02-10 23:07:48 +000069 mutex_lock(&ld->ops_lock);
70 if (ld->ops && ld->ops->get_power)
71 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 else
73 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +000074 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return rc;
77}
78
79static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
80{
Richard Purdie68673af2006-05-15 09:44:15 -070081 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 char *endp;
83 struct lcd_device *ld = to_lcd_device(cdev);
Richard Purdie68673af2006-05-15 09:44:15 -070084 int power = simple_strtoul(buf, &endp, 0);
85 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Richard Purdie68673af2006-05-15 09:44:15 -070087 if (*endp && isspace(*endp))
88 size++;
89 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EINVAL;
91
Richard Purdie599a52d2007-02-10 23:07:48 +000092 mutex_lock(&ld->ops_lock);
93 if (ld->ops && ld->ops->set_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 pr_debug("lcd: set power to %d\n", power);
Richard Purdie599a52d2007-02-10 23:07:48 +000095 ld->ops->set_power(ld, power);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -070097 }
Richard Purdie599a52d2007-02-10 23:07:48 +000098 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 return rc;
101}
102
103static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
104{
Richard Purdie68673af2006-05-15 09:44:15 -0700105 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct lcd_device *ld = to_lcd_device(cdev);
107
Richard Purdie599a52d2007-02-10 23:07:48 +0000108 mutex_lock(&ld->ops_lock);
109 if (ld->ops && ld->ops->get_contrast)
110 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
111 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 return rc;
114}
115
116static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
117{
Richard Purdie68673af2006-05-15 09:44:15 -0700118 int rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 char *endp;
120 struct lcd_device *ld = to_lcd_device(cdev);
Richard Purdie68673af2006-05-15 09:44:15 -0700121 int contrast = simple_strtoul(buf, &endp, 0);
122 size_t size = endp - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Richard Purdie68673af2006-05-15 09:44:15 -0700124 if (*endp && isspace(*endp))
125 size++;
126 if (size != count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return -EINVAL;
128
Richard Purdie599a52d2007-02-10 23:07:48 +0000129 mutex_lock(&ld->ops_lock);
130 if (ld->ops && ld->ops->set_contrast) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 pr_debug("lcd: set contrast to %d\n", contrast);
Richard Purdie599a52d2007-02-10 23:07:48 +0000132 ld->ops->set_contrast(ld, contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 rc = count;
Richard Purdie68673af2006-05-15 09:44:15 -0700134 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000135 mutex_unlock(&ld->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 return rc;
138}
139
140static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct lcd_device *ld = to_lcd_device(cdev);
143
Richard Purdie599a52d2007-02-10 23:07:48 +0000144 return sprintf(buf, "%d\n", ld->props.max_contrast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static void lcd_class_release(struct class_device *dev)
148{
149 struct lcd_device *ld = to_lcd_device(dev);
150 kfree(ld);
151}
152
153static struct class lcd_class = {
154 .name = "lcd",
155 .release = lcd_class_release,
156};
157
158#define DECLARE_ATTR(_name,_mode,_show,_store) \
159{ \
160 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
161 .show = _show, \
162 .store = _store, \
163}
164
Helge Dellerd95159c2006-12-08 02:40:28 -0800165static const struct class_device_attribute lcd_class_device_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 DECLARE_ATTR(power, 0644, lcd_show_power, lcd_store_power),
167 DECLARE_ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
168 DECLARE_ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
169};
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/**
172 * lcd_device_register - register a new object of lcd_device class.
173 * @name: the name of the new object(must be the same as the name of the
174 * respective framebuffer device).
175 * @devdata: an optional pointer to be stored in the class_device. The
176 * methods may retrieve it by using class_get_devdata(ld->class_dev).
Richard Purdie599a52d2007-02-10 23:07:48 +0000177 * @ops: the lcd operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *
179 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
180 * or a pointer to the newly allocated device.
181 */
182struct lcd_device *lcd_device_register(const char *name, void *devdata,
Richard Purdie599a52d2007-02-10 23:07:48 +0000183 struct lcd_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int i, rc;
186 struct lcd_device *new_ld;
187
188 pr_debug("lcd_device_register: name=%s\n", name);
189
Richard Purdie599a52d2007-02-10 23:07:48 +0000190 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000191 if (!new_ld)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800192 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Richard Purdie599a52d2007-02-10 23:07:48 +0000194 mutex_init(&new_ld->ops_lock);
Richard Purdie28ee0862007-02-08 22:25:09 +0000195 mutex_init(&new_ld->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000196 new_ld->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 new_ld->class_dev.class = &lcd_class;
198 strlcpy(new_ld->class_dev.class_id, name, KOBJ_NAME_LEN);
199 class_set_devdata(&new_ld->class_dev, devdata);
200
201 rc = class_device_register(&new_ld->class_dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000202 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000203 kfree(new_ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return ERR_PTR(rc);
205 }
206
James Simmons3d5eead2006-12-08 02:40:47 -0800207 rc = lcd_register_fb(new_ld);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000208 if (rc) {
209 class_device_unregister(&new_ld->class_dev);
210 return ERR_PTR(rc);
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++) {
214 rc = class_device_create_file(&new_ld->class_dev,
215 &lcd_class_device_attributes[i]);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000216 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 while (--i >= 0)
218 class_device_remove_file(&new_ld->class_dev,
219 &lcd_class_device_attributes[i]);
220 class_device_unregister(&new_ld->class_dev);
221 /* No need to kfree(new_ld) since release() method was called */
222 return ERR_PTR(rc);
223 }
224 }
225
226 return new_ld;
227}
228EXPORT_SYMBOL(lcd_device_register);
229
230/**
231 * lcd_device_unregister - unregisters a object of lcd_device class.
232 * @ld: the lcd device object to be unregistered and freed.
233 *
234 * Unregisters a previously registered via lcd_device_register object.
235 */
236void lcd_device_unregister(struct lcd_device *ld)
237{
238 int i;
239
240 if (!ld)
241 return;
242
243 pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
244
245 for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++)
246 class_device_remove_file(&ld->class_dev,
247 &lcd_class_device_attributes[i]);
248
Richard Purdie599a52d2007-02-10 23:07:48 +0000249 mutex_lock(&ld->ops_lock);
250 ld->ops = NULL;
251 mutex_unlock(&ld->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -0800252 lcd_unregister_fb(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 class_device_unregister(&ld->class_dev);
254}
255EXPORT_SYMBOL(lcd_device_unregister);
256
257static void __exit lcd_class_exit(void)
258{
259 class_unregister(&lcd_class);
260}
261
262static int __init lcd_class_init(void)
263{
264 return class_register(&lcd_class);
265}
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");