lcd: add callbacks for early fb event blank support
[firefly-linux-kernel-4.4.55.git] / drivers / video / backlight / lcd.c
1 /*
2  * LCD Lowlevel Control Abstraction
3  *
4  * Copyright (C) 2003,2004 Hewlett-Packard Company
5  *
6  */
7
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>
16 #include <linux/slab.h>
17
18 #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  */
24 static 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 ... */
31         switch (event) {
32         case FB_EVENT_BLANK:
33         case FB_EVENT_MODE_CHANGE:
34         case FB_EVENT_MODE_CHANGE_ALL:
35         case FB_EARLY_EVENT_BLANK:
36         case FB_R_EARLY_EVENT_BLANK:
37                 break;
38         default:
39                 return 0;
40         }
41
42         ld = container_of(self, struct lcd_device, fb_notif);
43         if (!ld->ops)
44                 return 0;
45
46         mutex_lock(&ld->ops_lock);
47         if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
48                 if (event == FB_EVENT_BLANK) {
49                         if (ld->ops->set_power)
50                                 ld->ops->set_power(ld, *(int *)evdata->data);
51                 } 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);
59                 } else {
60                         if (ld->ops->set_mode)
61                                 ld->ops->set_mode(ld, evdata->data);
62                 }
63         }
64         mutex_unlock(&ld->ops_lock);
65         return 0;
66 }
67
68 static int lcd_register_fb(struct lcd_device *ld)
69 {
70         memset(&ld->fb_notif, 0, sizeof(ld->fb_notif));
71         ld->fb_notif.notifier_call = fb_notifier_callback;
72         return fb_register_client(&ld->fb_notif);
73 }
74
75 static void lcd_unregister_fb(struct lcd_device *ld)
76 {
77         fb_unregister_client(&ld->fb_notif);
78 }
79 #else
80 static int lcd_register_fb(struct lcd_device *ld)
81 {
82         return 0;
83 }
84
85 static inline void lcd_unregister_fb(struct lcd_device *ld)
86 {
87 }
88 #endif /* CONFIG_FB */
89
90 static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
91                 char *buf)
92 {
93         int rc;
94         struct lcd_device *ld = to_lcd_device(dev);
95
96         mutex_lock(&ld->ops_lock);
97         if (ld->ops && ld->ops->get_power)
98                 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
99         else
100                 rc = -ENXIO;
101         mutex_unlock(&ld->ops_lock);
102
103         return rc;
104 }
105
106 static ssize_t lcd_store_power(struct device *dev,
107                 struct device_attribute *attr, const char *buf, size_t count)
108 {
109         int rc = -ENXIO;
110         struct lcd_device *ld = to_lcd_device(dev);
111         unsigned long power;
112
113         rc = kstrtoul(buf, 0, &power);
114         if (rc)
115                 return rc;
116
117         mutex_lock(&ld->ops_lock);
118         if (ld->ops && ld->ops->set_power) {
119                 pr_debug("lcd: set power to %lu\n", power);
120                 ld->ops->set_power(ld, power);
121                 rc = count;
122         }
123         mutex_unlock(&ld->ops_lock);
124
125         return rc;
126 }
127
128 static ssize_t lcd_show_contrast(struct device *dev,
129                 struct device_attribute *attr, char *buf)
130 {
131         int rc = -ENXIO;
132         struct lcd_device *ld = to_lcd_device(dev);
133
134         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);
138
139         return rc;
140 }
141
142 static ssize_t lcd_store_contrast(struct device *dev,
143                 struct device_attribute *attr, const char *buf, size_t count)
144 {
145         int rc = -ENXIO;
146         struct lcd_device *ld = to_lcd_device(dev);
147         unsigned long contrast;
148
149         rc = kstrtoul(buf, 0, &contrast);
150         if (rc)
151                 return rc;
152
153         mutex_lock(&ld->ops_lock);
154         if (ld->ops && ld->ops->set_contrast) {
155                 pr_debug("lcd: set contrast to %lu\n", contrast);
156                 ld->ops->set_contrast(ld, contrast);
157                 rc = count;
158         }
159         mutex_unlock(&ld->ops_lock);
160
161         return rc;
162 }
163
164 static ssize_t lcd_show_max_contrast(struct device *dev,
165                 struct device_attribute *attr, char *buf)
166 {
167         struct lcd_device *ld = to_lcd_device(dev);
168
169         return sprintf(buf, "%d\n", ld->props.max_contrast);
170 }
171
172 static struct class *lcd_class;
173
174 static void lcd_device_release(struct device *dev)
175 {
176         struct lcd_device *ld = to_lcd_device(dev);
177         kfree(ld);
178 }
179
180 static 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,
185 };
186
187 /**
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).
191  * @devdata: an optional pointer to be stored in the device. The
192  *   methods may retrieve it by using lcd_get_data(ld).
193  * @ops: the lcd operations structure.
194  *
195  * Creates and registers a new lcd device. Returns either an ERR_PTR()
196  * or a pointer to the newly allocated device.
197  */
198 struct lcd_device *lcd_device_register(const char *name, struct device *parent,
199                 void *devdata, struct lcd_ops *ops)
200 {
201         struct lcd_device *new_ld;
202         int rc;
203
204         pr_debug("lcd_device_register: name=%s\n", name);
205
206         new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
207         if (!new_ld)
208                 return ERR_PTR(-ENOMEM);
209
210         mutex_init(&new_ld->ops_lock);
211         mutex_init(&new_ld->update_lock);
212
213         new_ld->dev.class = lcd_class;
214         new_ld->dev.parent = parent;
215         new_ld->dev.release = lcd_device_release;
216         dev_set_name(&new_ld->dev, name);
217         dev_set_drvdata(&new_ld->dev, devdata);
218
219         rc = device_register(&new_ld->dev);
220         if (rc) {
221                 kfree(new_ld);
222                 return ERR_PTR(rc);
223         }
224
225         rc = lcd_register_fb(new_ld);
226         if (rc) {
227                 device_unregister(&new_ld->dev);
228                 return ERR_PTR(rc);
229         }
230
231         new_ld->ops = ops;
232
233         return new_ld;
234 }
235 EXPORT_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  */
243 void lcd_device_unregister(struct lcd_device *ld)
244 {
245         if (!ld)
246                 return;
247
248         mutex_lock(&ld->ops_lock);
249         ld->ops = NULL;
250         mutex_unlock(&ld->ops_lock);
251         lcd_unregister_fb(ld);
252
253         device_unregister(&ld->dev);
254 }
255 EXPORT_SYMBOL(lcd_device_unregister);
256
257 static void __exit lcd_class_exit(void)
258 {
259         class_destroy(lcd_class);
260 }
261
262 static int __init lcd_class_init(void)
263 {
264         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;
273 }
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  */
279 postcore_initcall(lcd_class_init);
280 module_exit(lcd_class_exit);
281
282 MODULE_LICENSE("GPL");
283 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
284 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");