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