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