]>
Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * LCD Lowlevel Control Abstraction | |
4 | * | |
5 | * Copyright (C) 2003,2004 Hewlett-Packard Company | |
6 | * | |
7 | */ | |
8 | ||
35f96162 JH |
9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
10 | ||
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/init.h> | |
13 | #include <linux/device.h> | |
14 | #include <linux/lcd.h> | |
15 | #include <linux/notifier.h> | |
16 | #include <linux/ctype.h> | |
17 | #include <linux/err.h> | |
18 | #include <linux/fb.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
1da177e4 | 20 | |
3d5eeadd JS |
21 | #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ |
22 | defined(CONFIG_LCD_CLASS_DEVICE_MODULE)) | |
23 | /* This callback gets called when something important happens inside a | |
24 | * framebuffer driver. We're looking if that important event is blanking, | |
25 | * and if it is, we're switching lcd power as well ... | |
26 | */ | |
27 | static int fb_notifier_callback(struct notifier_block *self, | |
28 | unsigned long event, void *data) | |
29 | { | |
30 | struct lcd_device *ld; | |
31 | struct fb_event *evdata = data; | |
32 | ||
3d5eeadd | 33 | ld = container_of(self, struct lcd_device, fb_notif); |
faa312da EM |
34 | if (!ld->ops) |
35 | return 0; | |
36 | ||
599a52d1 | 37 | mutex_lock(&ld->ops_lock); |
faa312da | 38 | if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) { |
b3b4dc88 BD |
39 | if (event == FB_EVENT_BLANK) { |
40 | if (ld->ops->set_power) | |
41 | ld->ops->set_power(ld, *(int *)evdata->data); | |
42 | } else { | |
43 | if (ld->ops->set_mode) | |
44 | ld->ops->set_mode(ld, evdata->data); | |
45 | } | |
faa312da | 46 | } |
599a52d1 | 47 | mutex_unlock(&ld->ops_lock); |
3d5eeadd JS |
48 | return 0; |
49 | } | |
50 | ||
51 | static int lcd_register_fb(struct lcd_device *ld) | |
52 | { | |
1e0fa6bd | 53 | memset(&ld->fb_notif, 0, sizeof(ld->fb_notif)); |
3d5eeadd JS |
54 | ld->fb_notif.notifier_call = fb_notifier_callback; |
55 | return fb_register_client(&ld->fb_notif); | |
56 | } | |
57 | ||
58 | static void lcd_unregister_fb(struct lcd_device *ld) | |
59 | { | |
60 | fb_unregister_client(&ld->fb_notif); | |
61 | } | |
62 | #else | |
63 | static int lcd_register_fb(struct lcd_device *ld) | |
64 | { | |
65 | return 0; | |
66 | } | |
67 | ||
68 | static inline void lcd_unregister_fb(struct lcd_device *ld) | |
69 | { | |
70 | } | |
71 | #endif /* CONFIG_FB */ | |
72 | ||
d79fd03f | 73 | static ssize_t lcd_power_show(struct device *dev, struct device_attribute *attr, |
655bfd7a | 74 | char *buf) |
1da177e4 LT |
75 | { |
76 | int rc; | |
655bfd7a | 77 | struct lcd_device *ld = to_lcd_device(dev); |
1da177e4 | 78 | |
599a52d1 RP |
79 | mutex_lock(&ld->ops_lock); |
80 | if (ld->ops && ld->ops->get_power) | |
81 | rc = sprintf(buf, "%d\n", ld->ops->get_power(ld)); | |
1da177e4 LT |
82 | else |
83 | rc = -ENXIO; | |
599a52d1 | 84 | mutex_unlock(&ld->ops_lock); |
1da177e4 LT |
85 | |
86 | return rc; | |
87 | } | |
88 | ||
d79fd03f | 89 | static ssize_t lcd_power_store(struct device *dev, |
655bfd7a | 90 | struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 | 91 | { |
424e06e3 | 92 | int rc; |
655bfd7a | 93 | struct lcd_device *ld = to_lcd_device(dev); |
66655760 | 94 | unsigned long power; |
1da177e4 | 95 | |
66655760 JH |
96 | rc = kstrtoul(buf, 0, &power); |
97 | if (rc) | |
98 | return rc; | |
1da177e4 | 99 | |
424e06e3 JH |
100 | rc = -ENXIO; |
101 | ||
599a52d1 RP |
102 | mutex_lock(&ld->ops_lock); |
103 | if (ld->ops && ld->ops->set_power) { | |
35f96162 | 104 | pr_debug("set power to %lu\n", power); |
599a52d1 | 105 | ld->ops->set_power(ld, power); |
1da177e4 | 106 | rc = count; |
68673afd | 107 | } |
599a52d1 | 108 | mutex_unlock(&ld->ops_lock); |
1da177e4 LT |
109 | |
110 | return rc; | |
111 | } | |
d79fd03f | 112 | static DEVICE_ATTR_RW(lcd_power); |
1da177e4 | 113 | |
d79fd03f | 114 | static ssize_t contrast_show(struct device *dev, |
655bfd7a | 115 | struct device_attribute *attr, char *buf) |
1da177e4 | 116 | { |
68673afd | 117 | int rc = -ENXIO; |
655bfd7a | 118 | struct lcd_device *ld = to_lcd_device(dev); |
1da177e4 | 119 | |
599a52d1 RP |
120 | mutex_lock(&ld->ops_lock); |
121 | if (ld->ops && ld->ops->get_contrast) | |
122 | rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld)); | |
123 | mutex_unlock(&ld->ops_lock); | |
1da177e4 LT |
124 | |
125 | return rc; | |
126 | } | |
127 | ||
d79fd03f | 128 | static ssize_t contrast_store(struct device *dev, |
655bfd7a | 129 | struct device_attribute *attr, const char *buf, size_t count) |
1da177e4 | 130 | { |
424e06e3 | 131 | int rc; |
655bfd7a | 132 | struct lcd_device *ld = to_lcd_device(dev); |
66655760 | 133 | unsigned long contrast; |
1da177e4 | 134 | |
66655760 JH |
135 | rc = kstrtoul(buf, 0, &contrast); |
136 | if (rc) | |
137 | return rc; | |
1da177e4 | 138 | |
424e06e3 JH |
139 | rc = -ENXIO; |
140 | ||
599a52d1 RP |
141 | mutex_lock(&ld->ops_lock); |
142 | if (ld->ops && ld->ops->set_contrast) { | |
35f96162 | 143 | pr_debug("set contrast to %lu\n", contrast); |
599a52d1 | 144 | ld->ops->set_contrast(ld, contrast); |
1da177e4 | 145 | rc = count; |
68673afd | 146 | } |
599a52d1 | 147 | mutex_unlock(&ld->ops_lock); |
1da177e4 LT |
148 | |
149 | return rc; | |
150 | } | |
d79fd03f | 151 | static DEVICE_ATTR_RW(contrast); |
1da177e4 | 152 | |
d79fd03f | 153 | static ssize_t max_contrast_show(struct device *dev, |
655bfd7a | 154 | struct device_attribute *attr, char *buf) |
1da177e4 | 155 | { |
655bfd7a | 156 | struct lcd_device *ld = to_lcd_device(dev); |
1da177e4 | 157 | |
599a52d1 | 158 | return sprintf(buf, "%d\n", ld->props.max_contrast); |
1da177e4 | 159 | } |
d79fd03f | 160 | static DEVICE_ATTR_RO(max_contrast); |
1da177e4 | 161 | |
0ad90efd | 162 | static struct class *lcd_class; |
655bfd7a RP |
163 | |
164 | static void lcd_device_release(struct device *dev) | |
1da177e4 LT |
165 | { |
166 | struct lcd_device *ld = to_lcd_device(dev); | |
167 | kfree(ld); | |
168 | } | |
169 | ||
d79fd03f GKH |
170 | static struct attribute *lcd_device_attrs[] = { |
171 | &dev_attr_lcd_power.attr, | |
172 | &dev_attr_contrast.attr, | |
173 | &dev_attr_max_contrast.attr, | |
174 | NULL, | |
1da177e4 | 175 | }; |
d79fd03f | 176 | ATTRIBUTE_GROUPS(lcd_device); |
1da177e4 | 177 | |
1da177e4 LT |
178 | /** |
179 | * lcd_device_register - register a new object of lcd_device class. | |
180 | * @name: the name of the new object(must be the same as the name of the | |
181 | * respective framebuffer device). | |
655bfd7a RP |
182 | * @devdata: an optional pointer to be stored in the device. The |
183 | * methods may retrieve it by using lcd_get_data(ld). | |
599a52d1 | 184 | * @ops: the lcd operations structure. |
1da177e4 | 185 | * |
655bfd7a | 186 | * Creates and registers a new lcd device. Returns either an ERR_PTR() |
1da177e4 LT |
187 | * or a pointer to the newly allocated device. |
188 | */ | |
655bfd7a RP |
189 | struct lcd_device *lcd_device_register(const char *name, struct device *parent, |
190 | void *devdata, struct lcd_ops *ops) | |
1da177e4 | 191 | { |
1da177e4 | 192 | struct lcd_device *new_ld; |
655bfd7a | 193 | int rc; |
1da177e4 LT |
194 | |
195 | pr_debug("lcd_device_register: name=%s\n", name); | |
196 | ||
599a52d1 | 197 | new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL); |
90968e8e | 198 | if (!new_ld) |
10ad1b73 | 199 | return ERR_PTR(-ENOMEM); |
1da177e4 | 200 | |
599a52d1 | 201 | mutex_init(&new_ld->ops_lock); |
28ee086d | 202 | mutex_init(&new_ld->update_lock); |
1da177e4 | 203 | |
655bfd7a RP |
204 | new_ld->dev.class = lcd_class; |
205 | new_ld->dev.parent = parent; | |
206 | new_ld->dev.release = lcd_device_release; | |
02aa2a37 | 207 | dev_set_name(&new_ld->dev, "%s", name); |
655bfd7a RP |
208 | dev_set_drvdata(&new_ld->dev, devdata); |
209 | ||
cc21942b UKK |
210 | new_ld->ops = ops; |
211 | ||
655bfd7a | 212 | rc = device_register(&new_ld->dev); |
90968e8e | 213 | if (rc) { |
54f5968d | 214 | put_device(&new_ld->dev); |
1da177e4 LT |
215 | return ERR_PTR(rc); |
216 | } | |
217 | ||
3d5eeadd | 218 | rc = lcd_register_fb(new_ld); |
2fd5a154 | 219 | if (rc) { |
655bfd7a | 220 | device_unregister(&new_ld->dev); |
2fd5a154 DT |
221 | return ERR_PTR(rc); |
222 | } | |
1da177e4 | 223 | |
1da177e4 LT |
224 | return new_ld; |
225 | } | |
226 | EXPORT_SYMBOL(lcd_device_register); | |
227 | ||
228 | /** | |
229 | * lcd_device_unregister - unregisters a object of lcd_device class. | |
230 | * @ld: the lcd device object to be unregistered and freed. | |
231 | * | |
232 | * Unregisters a previously registered via lcd_device_register object. | |
233 | */ | |
234 | void lcd_device_unregister(struct lcd_device *ld) | |
235 | { | |
1da177e4 LT |
236 | if (!ld) |
237 | return; | |
238 | ||
599a52d1 RP |
239 | mutex_lock(&ld->ops_lock); |
240 | ld->ops = NULL; | |
241 | mutex_unlock(&ld->ops_lock); | |
3d5eeadd | 242 | lcd_unregister_fb(ld); |
655bfd7a RP |
243 | |
244 | device_unregister(&ld->dev); | |
1da177e4 LT |
245 | } |
246 | EXPORT_SYMBOL(lcd_device_unregister); | |
247 | ||
1d0c48e6 JH |
248 | static void devm_lcd_device_release(struct device *dev, void *res) |
249 | { | |
250 | struct lcd_device *lcd = *(struct lcd_device **)res; | |
251 | ||
252 | lcd_device_unregister(lcd); | |
253 | } | |
254 | ||
255 | static int devm_lcd_device_match(struct device *dev, void *res, void *data) | |
256 | { | |
257 | struct lcd_device **r = res; | |
258 | ||
259 | return *r == data; | |
260 | } | |
261 | ||
262 | /** | |
263 | * devm_lcd_device_register - resource managed lcd_device_register() | |
264 | * @dev: the device to register | |
265 | * @name: the name of the device | |
266 | * @parent: a pointer to the parent device | |
267 | * @devdata: an optional pointer to be stored for private driver use | |
268 | * @ops: the lcd operations structure | |
269 | * | |
270 | * @return a struct lcd on success, or an ERR_PTR on error | |
271 | * | |
272 | * Managed lcd_device_register(). The lcd_device returned from this function | |
273 | * are automatically freed on driver detach. See lcd_device_register() | |
274 | * for more information. | |
275 | */ | |
276 | struct lcd_device *devm_lcd_device_register(struct device *dev, | |
277 | const char *name, struct device *parent, | |
278 | void *devdata, struct lcd_ops *ops) | |
279 | { | |
280 | struct lcd_device **ptr, *lcd; | |
281 | ||
282 | ptr = devres_alloc(devm_lcd_device_release, sizeof(*ptr), GFP_KERNEL); | |
283 | if (!ptr) | |
284 | return ERR_PTR(-ENOMEM); | |
285 | ||
286 | lcd = lcd_device_register(name, parent, devdata, ops); | |
287 | if (!IS_ERR(lcd)) { | |
288 | *ptr = lcd; | |
289 | devres_add(dev, ptr); | |
290 | } else { | |
291 | devres_free(ptr); | |
292 | } | |
293 | ||
294 | return lcd; | |
295 | } | |
296 | EXPORT_SYMBOL(devm_lcd_device_register); | |
297 | ||
298 | /** | |
299 | * devm_lcd_device_unregister - resource managed lcd_device_unregister() | |
300 | * @dev: the device to unregister | |
301 | * @ld: the lcd device to unregister | |
302 | * | |
303 | * Deallocated a lcd allocated with devm_lcd_device_register(). Normally | |
304 | * this function will not need to be called and the resource management | |
305 | * code will ensure that the resource is freed. | |
306 | */ | |
307 | void devm_lcd_device_unregister(struct device *dev, struct lcd_device *ld) | |
308 | { | |
309 | int rc; | |
310 | ||
311 | rc = devres_release(dev, devm_lcd_device_release, | |
312 | devm_lcd_device_match, ld); | |
313 | WARN_ON(rc); | |
314 | } | |
315 | EXPORT_SYMBOL(devm_lcd_device_unregister); | |
316 | ||
317 | ||
1da177e4 LT |
318 | static void __exit lcd_class_exit(void) |
319 | { | |
655bfd7a | 320 | class_destroy(lcd_class); |
1da177e4 LT |
321 | } |
322 | ||
323 | static int __init lcd_class_init(void) | |
324 | { | |
655bfd7a RP |
325 | lcd_class = class_create(THIS_MODULE, "lcd"); |
326 | if (IS_ERR(lcd_class)) { | |
35f96162 JH |
327 | pr_warn("Unable to create backlight class; errno = %ld\n", |
328 | PTR_ERR(lcd_class)); | |
655bfd7a RP |
329 | return PTR_ERR(lcd_class); |
330 | } | |
331 | ||
d79fd03f | 332 | lcd_class->dev_groups = lcd_device_groups; |
655bfd7a | 333 | return 0; |
1da177e4 LT |
334 | } |
335 | ||
336 | /* | |
337 | * if this is compiled into the kernel, we need to ensure that the | |
338 | * class is registered before users of the class try to register lcd's | |
339 | */ | |
340 | postcore_initcall(lcd_class_init); | |
341 | module_exit(lcd_class_exit); | |
342 | ||
343 | MODULE_LICENSE("GPL"); | |
344 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); | |
345 | MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction"); |