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