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