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