]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/video/backlight/lcd.c
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/djbw/async_tx
[mirror_ubuntu-bionic-kernel.git] / drivers / video / backlight / lcd.c
CommitLineData
1da177e4
LT
1/*
2 * LCD Lowlevel Control Abstraction
3 *
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
5 *
6 */
7
1da177e4
LT
8#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>
1da177e4 16
3d5eeadd
JS
17#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 ... */
faa312da
EM
30 switch (event) {
31 case FB_EVENT_BLANK:
32 case FB_EVENT_MODE_CHANGE:
33 case FB_EVENT_MODE_CHANGE_ALL:
34 break;
35 default:
3d5eeadd 36 return 0;
faa312da 37 }
3d5eeadd
JS
38
39 ld = container_of(self, struct lcd_device, fb_notif);
faa312da
EM
40 if (!ld->ops)
41 return 0;
42
66c1ca01
AR
43 if (!lock_fb_info(evdata->info))
44 return -ENODEV;
599a52d1 45 mutex_lock(&ld->ops_lock);
faa312da 46 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
b3b4dc88
BD
47 if (event == FB_EVENT_BLANK) {
48 if (ld->ops->set_power)
49 ld->ops->set_power(ld, *(int *)evdata->data);
50 } else {
51 if (ld->ops->set_mode)
52 ld->ops->set_mode(ld, evdata->data);
53 }
faa312da 54 }
599a52d1 55 mutex_unlock(&ld->ops_lock);
66c1ca01 56 unlock_fb_info(evdata->info);
3d5eeadd
JS
57 return 0;
58}
59
60static int lcd_register_fb(struct lcd_device *ld)
61{
62 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
63 ld->fb_notif.notifier_call = fb_notifier_callback;
64 return fb_register_client(&ld->fb_notif);
65}
66
67static void lcd_unregister_fb(struct lcd_device *ld)
68{
69 fb_unregister_client(&ld->fb_notif);
70}
71#else
72static int lcd_register_fb(struct lcd_device *ld)
73{
74 return 0;
75}
76
77static inline void lcd_unregister_fb(struct lcd_device *ld)
78{
79}
80#endif /* CONFIG_FB */
81
655bfd7a
RP
82static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
83 char *buf)
1da177e4
LT
84{
85 int rc;
655bfd7a 86 struct lcd_device *ld = to_lcd_device(dev);
1da177e4 87
599a52d1
RP
88 mutex_lock(&ld->ops_lock);
89 if (ld->ops && ld->ops->get_power)
90 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
1da177e4
LT
91 else
92 rc = -ENXIO;
599a52d1 93 mutex_unlock(&ld->ops_lock);
1da177e4
LT
94
95 return rc;
96}
97
655bfd7a
RP
98static ssize_t lcd_store_power(struct device *dev,
99 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 100{
68673afd 101 int rc = -ENXIO;
1da177e4 102 char *endp;
655bfd7a 103 struct lcd_device *ld = to_lcd_device(dev);
68673afd
RP
104 int power = simple_strtoul(buf, &endp, 0);
105 size_t size = endp - buf;
1da177e4 106
68673afd
RP
107 if (*endp && isspace(*endp))
108 size++;
109 if (size != count)
1da177e4
LT
110 return -EINVAL;
111
599a52d1
RP
112 mutex_lock(&ld->ops_lock);
113 if (ld->ops && ld->ops->set_power) {
1da177e4 114 pr_debug("lcd: set power to %d\n", power);
599a52d1 115 ld->ops->set_power(ld, power);
1da177e4 116 rc = count;
68673afd 117 }
599a52d1 118 mutex_unlock(&ld->ops_lock);
1da177e4
LT
119
120 return rc;
121}
122
655bfd7a
RP
123static ssize_t lcd_show_contrast(struct device *dev,
124 struct device_attribute *attr, char *buf)
1da177e4 125{
68673afd 126 int rc = -ENXIO;
655bfd7a 127 struct lcd_device *ld = to_lcd_device(dev);
1da177e4 128
599a52d1
RP
129 mutex_lock(&ld->ops_lock);
130 if (ld->ops && ld->ops->get_contrast)
131 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
132 mutex_unlock(&ld->ops_lock);
1da177e4
LT
133
134 return rc;
135}
136
655bfd7a
RP
137static ssize_t lcd_store_contrast(struct device *dev,
138 struct device_attribute *attr, const char *buf, size_t count)
1da177e4 139{
68673afd 140 int rc = -ENXIO;
1da177e4 141 char *endp;
655bfd7a 142 struct lcd_device *ld = to_lcd_device(dev);
68673afd
RP
143 int contrast = simple_strtoul(buf, &endp, 0);
144 size_t size = endp - buf;
1da177e4 145
68673afd
RP
146 if (*endp && isspace(*endp))
147 size++;
148 if (size != count)
1da177e4
LT
149 return -EINVAL;
150
599a52d1
RP
151 mutex_lock(&ld->ops_lock);
152 if (ld->ops && ld->ops->set_contrast) {
1da177e4 153 pr_debug("lcd: set contrast to %d\n", contrast);
599a52d1 154 ld->ops->set_contrast(ld, contrast);
1da177e4 155 rc = count;
68673afd 156 }
599a52d1 157 mutex_unlock(&ld->ops_lock);
1da177e4
LT
158
159 return rc;
160}
161
655bfd7a
RP
162static ssize_t lcd_show_max_contrast(struct device *dev,
163 struct device_attribute *attr, char *buf)
1da177e4 164{
655bfd7a 165 struct lcd_device *ld = to_lcd_device(dev);
1da177e4 166
599a52d1 167 return sprintf(buf, "%d\n", ld->props.max_contrast);
1da177e4
LT
168}
169
0ad90efd 170static struct class *lcd_class;
655bfd7a
RP
171
172static void lcd_device_release(struct device *dev)
1da177e4
LT
173{
174 struct lcd_device *ld = to_lcd_device(dev);
175 kfree(ld);
176}
177
655bfd7a
RP
178static struct device_attribute lcd_device_attributes[] = {
179 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
180 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
181 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
182 __ATTR_NULL,
1da177e4
LT
183};
184
1da177e4
LT
185/**
186 * lcd_device_register - register a new object of lcd_device class.
187 * @name: the name of the new object(must be the same as the name of the
188 * respective framebuffer device).
655bfd7a
RP
189 * @devdata: an optional pointer to be stored in the device. The
190 * methods may retrieve it by using lcd_get_data(ld).
599a52d1 191 * @ops: the lcd operations structure.
1da177e4 192 *
655bfd7a 193 * Creates and registers a new lcd device. Returns either an ERR_PTR()
1da177e4
LT
194 * or a pointer to the newly allocated device.
195 */
655bfd7a
RP
196struct lcd_device *lcd_device_register(const char *name, struct device *parent,
197 void *devdata, struct lcd_ops *ops)
1da177e4 198{
1da177e4 199 struct lcd_device *new_ld;
655bfd7a 200 int rc;
1da177e4
LT
201
202 pr_debug("lcd_device_register: name=%s\n", name);
203
599a52d1 204 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
90968e8e 205 if (!new_ld)
10ad1b73 206 return ERR_PTR(-ENOMEM);
1da177e4 207
599a52d1 208 mutex_init(&new_ld->ops_lock);
28ee086d 209 mutex_init(&new_ld->update_lock);
1da177e4 210
655bfd7a
RP
211 new_ld->dev.class = lcd_class;
212 new_ld->dev.parent = parent;
213 new_ld->dev.release = lcd_device_release;
64dba9a9 214 dev_set_name(&new_ld->dev, name);
655bfd7a
RP
215 dev_set_drvdata(&new_ld->dev, devdata);
216
217 rc = device_register(&new_ld->dev);
90968e8e 218 if (rc) {
2fd5a154 219 kfree(new_ld);
1da177e4
LT
220 return ERR_PTR(rc);
221 }
222
3d5eeadd 223 rc = lcd_register_fb(new_ld);
2fd5a154 224 if (rc) {
655bfd7a 225 device_unregister(&new_ld->dev);
2fd5a154
DT
226 return ERR_PTR(rc);
227 }
1da177e4 228
655bfd7a 229 new_ld->ops = ops;
1da177e4
LT
230
231 return new_ld;
232}
233EXPORT_SYMBOL(lcd_device_register);
234
235/**
236 * lcd_device_unregister - unregisters a object of lcd_device class.
237 * @ld: the lcd device object to be unregistered and freed.
238 *
239 * Unregisters a previously registered via lcd_device_register object.
240 */
241void lcd_device_unregister(struct lcd_device *ld)
242{
1da177e4
LT
243 if (!ld)
244 return;
245
599a52d1
RP
246 mutex_lock(&ld->ops_lock);
247 ld->ops = NULL;
248 mutex_unlock(&ld->ops_lock);
3d5eeadd 249 lcd_unregister_fb(ld);
655bfd7a
RP
250
251 device_unregister(&ld->dev);
1da177e4
LT
252}
253EXPORT_SYMBOL(lcd_device_unregister);
254
255static void __exit lcd_class_exit(void)
256{
655bfd7a 257 class_destroy(lcd_class);
1da177e4
LT
258}
259
260static int __init lcd_class_init(void)
261{
655bfd7a
RP
262 lcd_class = class_create(THIS_MODULE, "lcd");
263 if (IS_ERR(lcd_class)) {
264 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
265 PTR_ERR(lcd_class));
266 return PTR_ERR(lcd_class);
267 }
268
269 lcd_class->dev_attrs = lcd_device_attributes;
270 return 0;
1da177e4
LT
271}
272
273/*
274 * if this is compiled into the kernel, we need to ensure that the
275 * class is registered before users of the class try to register lcd's
276 */
277postcore_initcall(lcd_class_init);
278module_exit(lcd_class_exit);
279
280MODULE_LICENSE("GPL");
281MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
282MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");