blob: 20b276e1f894465a140165c2f90a44f479e37597 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Backlight Lowlevel Control Abstraction
3 *
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
5 *
6 */
7
Jingoo Han35f96162012-05-29 15:07:16 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/device.h>
13#include <linux/backlight.h>
14#include <linux/notifier.h>
15#include <linux/ctype.h>
16#include <linux/err.h>
17#include <linux/fb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Richard Purdie321709c2007-02-10 15:04:08 +000020#ifdef CONFIG_PMAC_BACKLIGHT
21#include <asm/backlight.h>
22#endif
James Simmons3d5eead2006-12-08 02:40:47 -080023
Aaron Lu5915a3d2013-10-11 21:27:43 +080024static struct list_head backlight_dev_list;
25static struct mutex backlight_dev_list_mutex;
26
Bart Van Asschec338bfb2011-09-10 20:13:01 +020027static const char *const backlight_types[] = {
Matthew Garrettbb7ca742011-03-22 16:30:21 -070028 [BACKLIGHT_RAW] = "raw",
29 [BACKLIGHT_PLATFORM] = "platform",
30 [BACKLIGHT_FIRMWARE] = "firmware",
31};
32
James Simmons3d5eead2006-12-08 02:40:47 -080033#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
34 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
35/* This callback gets called when something important happens inside a
36 * framebuffer driver. We're looking if that important event is blanking,
37 * and if it is, we're switching backlight power as well ...
38 */
39static int fb_notifier_callback(struct notifier_block *self,
40 unsigned long event, void *data)
41{
42 struct backlight_device *bd;
43 struct fb_event *evdata = data;
44
45 /* If we aren't interested in this event, skip it immediately ... */
Richard Purdie994efac2007-02-09 09:46:45 +000046 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
James Simmons3d5eead2006-12-08 02:40:47 -080047 return 0;
48
49 bd = container_of(self, struct backlight_device, fb_notif);
Richard Purdie599a52d2007-02-10 23:07:48 +000050 mutex_lock(&bd->ops_lock);
51 if (bd->ops)
52 if (!bd->ops->check_fb ||
Bruno Prémont57e148b2010-02-21 00:20:01 +010053 bd->ops->check_fb(bd, evdata->info)) {
Richard Purdie599a52d2007-02-10 23:07:48 +000054 bd->props.fb_blank = *(int *)evdata->data;
Richard Purdiec835ee72009-01-06 21:00:19 +000055 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
56 bd->props.state &= ~BL_CORE_FBBLANK;
57 else
58 bd->props.state |= BL_CORE_FBBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +000059 backlight_update_status(bd);
James Simmons3d5eead2006-12-08 02:40:47 -080060 }
Richard Purdie599a52d2007-02-10 23:07:48 +000061 mutex_unlock(&bd->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080062 return 0;
63}
64
65static int backlight_register_fb(struct backlight_device *bd)
66{
67 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
68 bd->fb_notif.notifier_call = fb_notifier_callback;
69
70 return fb_register_client(&bd->fb_notif);
71}
72
73static void backlight_unregister_fb(struct backlight_device *bd)
74{
75 fb_unregister_client(&bd->fb_notif);
76}
77#else
78static inline int backlight_register_fb(struct backlight_device *bd)
79{
80 return 0;
81}
82
83static inline void backlight_unregister_fb(struct backlight_device *bd)
84{
85}
86#endif /* CONFIG_FB */
87
Matthew Garrett325253a2009-07-14 17:06:02 +010088static void backlight_generate_event(struct backlight_device *bd,
89 enum backlight_update_reason reason)
90{
91 char *envp[2];
92
93 switch (reason) {
94 case BACKLIGHT_UPDATE_SYSFS:
95 envp[0] = "SOURCE=sysfs";
96 break;
97 case BACKLIGHT_UPDATE_HOTKEY:
98 envp[0] = "SOURCE=hotkey";
99 break;
100 default:
101 envp[0] = "SOURCE=unknown";
102 break;
103 }
104 envp[1] = NULL;
105 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
Henrique de Moraes Holschuh89dfc282009-09-20 14:44:47 -0300106 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
Matthew Garrett325253a2009-07-14 17:06:02 +0100107}
108
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700109static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
110 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Richard Purdie655bfd72007-07-09 12:17:24 +0100112 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Richard Purdie599a52d2007-02-10 23:07:48 +0000114 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700117static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
118 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000120 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100121 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000122 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jingoo Han66655762012-01-10 15:09:19 -0800124 rc = kstrtoul(buf, 0, &power);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000125 if (rc)
126 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000128 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000129 mutex_lock(&bd->ops_lock);
130 if (bd->ops) {
Jingoo Han35f96162012-05-29 15:07:16 -0700131 pr_debug("set power to %lu\n", power);
Helge Deller51552452008-01-13 23:01:13 +0000132 if (bd->props.power != power) {
133 bd->props.power = power;
134 backlight_update_status(bd);
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800137 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000138 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return rc;
141}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700142static DEVICE_ATTR_RW(bl_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700144static ssize_t brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100145 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Richard Purdie655bfd72007-07-09 12:17:24 +0100147 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Richard Purdie599a52d2007-02-10 23:07:48 +0000149 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700152static ssize_t brightness_store(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100153 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000155 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100156 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000157 unsigned long brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jingoo Han66655762012-01-10 15:09:19 -0800159 rc = kstrtoul(buf, 0, &brightness);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000160 if (rc)
161 return rc;
162
163 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Richard Purdie599a52d2007-02-10 23:07:48 +0000165 mutex_lock(&bd->ops_lock);
166 if (bd->ops) {
167 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800168 rc = -EINVAL;
169 else {
Jingoo Han35f96162012-05-29 15:07:16 -0700170 pr_debug("set brightness to %lu\n", brightness);
Zhang Rui9be1df92009-01-08 14:11:30 +0000171 bd->props.brightness = brightness;
172 backlight_update_status(bd);
Richard Purdie6ca01762006-03-31 02:31:49 -0800173 rc = count;
174 }
175 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000176 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Matthew Garrett325253a2009-07-14 17:06:02 +0100178 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return rc;
181}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700182static DEVICE_ATTR_RW(brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700184static ssize_t type_show(struct device *dev, struct device_attribute *attr,
185 char *buf)
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700186{
187 struct backlight_device *bd = to_backlight_device(dev);
188
189 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
190}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700191static DEVICE_ATTR_RO(type);
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700192
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700193static ssize_t max_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100194 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Richard Purdie655bfd72007-07-09 12:17:24 +0100196 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Richard Purdie599a52d2007-02-10 23:07:48 +0000198 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800199}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700200static DEVICE_ATTR_RO(max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800201
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700202static ssize_t actual_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100203 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800204{
205 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100206 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800207
Richard Purdie599a52d2007-02-10 23:07:48 +0000208 mutex_lock(&bd->ops_lock);
209 if (bd->ops && bd->ops->get_brightness)
210 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
211 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 return rc;
214}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700215static DEVICE_ATTR_RO(actual_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100217static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100218
Shuah Khan36017922013-07-03 15:05:16 -0700219#ifdef CONFIG_PM_SLEEP
220static int backlight_suspend(struct device *dev)
Richard Purdiec835ee72009-01-06 21:00:19 +0000221{
222 struct backlight_device *bd = to_backlight_device(dev);
223
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800224 mutex_lock(&bd->ops_lock);
225 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000226 bd->props.state |= BL_CORE_SUSPENDED;
227 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000228 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800229 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000230
231 return 0;
232}
233
234static int backlight_resume(struct device *dev)
235{
236 struct backlight_device *bd = to_backlight_device(dev);
237
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800238 mutex_lock(&bd->ops_lock);
239 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000240 bd->props.state &= ~BL_CORE_SUSPENDED;
241 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000242 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800243 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000244
245 return 0;
246}
Shuah Khan36017922013-07-03 15:05:16 -0700247#endif
248
249static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
250 backlight_resume);
Richard Purdiec835ee72009-01-06 21:00:19 +0000251
Richard Purdie655bfd72007-07-09 12:17:24 +0100252static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct backlight_device *bd = to_backlight_device(dev);
255 kfree(bd);
256}
257
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700258static struct attribute *bl_device_attrs[] = {
259 &dev_attr_bl_power.attr,
260 &dev_attr_brightness.attr,
261 &dev_attr_actual_brightness.attr,
262 &dev_attr_max_brightness.attr,
263 &dev_attr_type.attr,
264 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700266ATTRIBUTE_GROUPS(bl_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/**
Matthew Garrett325253a2009-07-14 17:06:02 +0100269 * backlight_force_update - tell the backlight subsystem that hardware state
270 * has changed
271 * @bd: the backlight device to update
272 *
273 * Updates the internal state of the backlight in response to a hardware event,
274 * and generate a uevent to notify userspace
275 */
276void backlight_force_update(struct backlight_device *bd,
277 enum backlight_update_reason reason)
278{
279 mutex_lock(&bd->ops_lock);
280 if (bd->ops && bd->ops->get_brightness)
281 bd->props.brightness = bd->ops->get_brightness(bd);
282 mutex_unlock(&bd->ops_lock);
283 backlight_generate_event(bd, reason);
284}
285EXPORT_SYMBOL(backlight_force_update);
286
287/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * backlight_device_register - create and register a new object of
289 * backlight_device class.
290 * @name: the name of the new object(must be the same as the name of the
291 * respective framebuffer device).
Sebastian Siewiorf6ec2d92008-07-16 23:05:49 +0100292 * @parent: a pointer to the parent device
Richard Purdie655bfd72007-07-09 12:17:24 +0100293 * @devdata: an optional pointer to be stored for private driver use. The
294 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000295 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100297 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * ERR_PTR() or a pointer to the newly allocated device.
299 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800300struct backlight_device *backlight_device_register(const char *name,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500301 struct device *parent, void *devdata, const struct backlight_ops *ops,
302 const struct backlight_properties *props)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100305 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Richard Purdie655bfd72007-07-09 12:17:24 +0100307 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Richard Purdie599a52d2007-02-10 23:07:48 +0000309 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000310 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800311 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Richard Purdie28ee0862007-02-08 22:25:09 +0000313 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000314 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Richard Purdie655bfd72007-07-09 12:17:24 +0100316 new_bd->dev.class = backlight_class;
317 new_bd->dev.parent = parent;
318 new_bd->dev.release = bl_device_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700319 dev_set_name(&new_bd->dev, "%s", name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100320 dev_set_drvdata(&new_bd->dev, devdata);
321
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500322 /* Set default properties */
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700323 if (props) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500324 memcpy(&new_bd->props, props,
325 sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700326 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
327 WARN(1, "%s: invalid backlight type", name);
328 new_bd->props.type = BACKLIGHT_RAW;
329 }
330 } else {
331 new_bd->props.type = BACKLIGHT_RAW;
332 }
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500333
Richard Purdie655bfd72007-07-09 12:17:24 +0100334 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000335 if (rc) {
Levente Kurusa35762a42014-02-07 09:43:21 +0100336 put_device(&new_bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return ERR_PTR(rc);
338 }
339
James Simmons3d5eead2006-12-08 02:40:47 -0800340 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000341 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100342 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000343 return ERR_PTR(rc);
344 }
345
Richard Purdie655bfd72007-07-09 12:17:24 +0100346 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Richard Purdie321709c2007-02-10 15:04:08 +0000348#ifdef CONFIG_PMAC_BACKLIGHT
349 mutex_lock(&pmac_backlight_mutex);
350 if (!pmac_backlight)
351 pmac_backlight = new_bd;
352 mutex_unlock(&pmac_backlight_mutex);
353#endif
354
Aaron Lu5915a3d2013-10-11 21:27:43 +0800355 mutex_lock(&backlight_dev_list_mutex);
356 list_add(&new_bd->entry, &backlight_dev_list);
357 mutex_unlock(&backlight_dev_list_mutex);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return new_bd;
360}
361EXPORT_SYMBOL(backlight_device_register);
362
Aaron Lu5915a3d2013-10-11 21:27:43 +0800363bool backlight_device_registered(enum backlight_type type)
364{
365 bool found = false;
366 struct backlight_device *bd;
367
368 mutex_lock(&backlight_dev_list_mutex);
369 list_for_each_entry(bd, &backlight_dev_list, entry) {
370 if (bd->props.type == type) {
371 found = true;
372 break;
373 }
374 }
375 mutex_unlock(&backlight_dev_list_mutex);
376
377 return found;
378}
379EXPORT_SYMBOL(backlight_device_registered);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/**
382 * backlight_device_unregister - unregisters a backlight device object.
383 * @bd: the backlight device object to be unregistered and freed.
384 *
385 * Unregisters a previously registered via backlight_device_register object.
386 */
387void backlight_device_unregister(struct backlight_device *bd)
388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!bd)
390 return;
391
Aaron Lu5915a3d2013-10-11 21:27:43 +0800392 mutex_lock(&backlight_dev_list_mutex);
393 list_del(&bd->entry);
394 mutex_unlock(&backlight_dev_list_mutex);
395
Richard Purdie321709c2007-02-10 15:04:08 +0000396#ifdef CONFIG_PMAC_BACKLIGHT
397 mutex_lock(&pmac_backlight_mutex);
398 if (pmac_backlight == bd)
399 pmac_backlight = NULL;
400 mutex_unlock(&pmac_backlight_mutex);
401#endif
Richard Purdie599a52d2007-02-10 23:07:48 +0000402 mutex_lock(&bd->ops_lock);
403 bd->ops = NULL;
404 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
James Simmons3d5eead2006-12-08 02:40:47 -0800406 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100407 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409EXPORT_SYMBOL(backlight_device_unregister);
410
Jingoo Han8318fde42013-07-03 15:05:13 -0700411static void devm_backlight_device_release(struct device *dev, void *res)
412{
413 struct backlight_device *backlight = *(struct backlight_device **)res;
414
415 backlight_device_unregister(backlight);
416}
417
418static int devm_backlight_device_match(struct device *dev, void *res,
419 void *data)
420{
421 struct backlight_device **r = res;
422
423 return *r == data;
424}
425
426/**
427 * devm_backlight_device_register - resource managed backlight_device_register()
428 * @dev: the device to register
429 * @name: the name of the device
430 * @parent: a pointer to the parent device
431 * @devdata: an optional pointer to be stored for private driver use
432 * @ops: the backlight operations structure
433 * @props: the backlight properties
434 *
435 * @return a struct backlight on success, or an ERR_PTR on error
436 *
437 * Managed backlight_device_register(). The backlight_device returned
438 * from this function are automatically freed on driver detach.
439 * See backlight_device_register() for more information.
440 */
441struct backlight_device *devm_backlight_device_register(struct device *dev,
442 const char *name, struct device *parent, void *devdata,
443 const struct backlight_ops *ops,
444 const struct backlight_properties *props)
445{
446 struct backlight_device **ptr, *backlight;
447
448 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
449 GFP_KERNEL);
450 if (!ptr)
451 return ERR_PTR(-ENOMEM);
452
453 backlight = backlight_device_register(name, parent, devdata, ops,
454 props);
455 if (!IS_ERR(backlight)) {
456 *ptr = backlight;
457 devres_add(dev, ptr);
458 } else {
459 devres_free(ptr);
460 }
461
462 return backlight;
463}
464EXPORT_SYMBOL(devm_backlight_device_register);
465
466/**
467 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
468 * @dev: the device to unregister
469 * @bd: the backlight device to unregister
470 *
471 * Deallocated a backlight allocated with devm_backlight_device_register().
472 * Normally this function will not need to be called and the resource management
473 * code will ensure that the resource is freed.
474 */
475void devm_backlight_device_unregister(struct device *dev,
476 struct backlight_device *bd)
477{
478 int rc;
479
480 rc = devres_release(dev, devm_backlight_device_release,
481 devm_backlight_device_match, bd);
482 WARN_ON(rc);
483}
484EXPORT_SYMBOL(devm_backlight_device_unregister);
485
Thierry Reding762a9362012-12-17 16:01:06 -0800486#ifdef CONFIG_OF
Greg Kroah-Hartman3213f632013-02-06 16:43:02 -0800487static int of_parent_match(struct device *dev, const void *data)
Thierry Reding762a9362012-12-17 16:01:06 -0800488{
489 return dev->parent && dev->parent->of_node == data;
490}
491
492/**
493 * of_find_backlight_by_node() - find backlight device by device-tree node
494 * @node: device-tree node of the backlight device
495 *
496 * Returns a pointer to the backlight device corresponding to the given DT
497 * node or NULL if no such backlight device exists or if the device hasn't
498 * been probed yet.
499 *
500 * This function obtains a reference on the backlight device and it is the
501 * caller's responsibility to drop the reference by calling put_device() on
502 * the backlight device's .dev field.
503 */
504struct backlight_device *of_find_backlight_by_node(struct device_node *node)
505{
506 struct device *dev;
507
508 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
509
510 return dev ? to_backlight_device(dev) : NULL;
511}
512EXPORT_SYMBOL(of_find_backlight_by_node);
513#endif
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static void __exit backlight_class_exit(void)
516{
Richard Purdie655bfd72007-07-09 12:17:24 +0100517 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static int __init backlight_class_init(void)
521{
Richard Purdie655bfd72007-07-09 12:17:24 +0100522 backlight_class = class_create(THIS_MODULE, "backlight");
523 if (IS_ERR(backlight_class)) {
Jingoo Han35f96162012-05-29 15:07:16 -0700524 pr_warn("Unable to create backlight class; errno = %ld\n",
525 PTR_ERR(backlight_class));
Richard Purdie655bfd72007-07-09 12:17:24 +0100526 return PTR_ERR(backlight_class);
527 }
528
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700529 backlight_class->dev_groups = bl_device_groups;
Shuah Khan36017922013-07-03 15:05:16 -0700530 backlight_class->pm = &backlight_class_dev_pm_ops;
Aaron Lu5915a3d2013-10-11 21:27:43 +0800531 INIT_LIST_HEAD(&backlight_dev_list);
532 mutex_init(&backlight_dev_list_mutex);
Richard Purdie655bfd72007-07-09 12:17:24 +0100533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * if this is compiled into the kernel, we need to ensure that the
538 * class is registered before users of the class try to register lcd's
539 */
540postcore_initcall(backlight_class_init);
541module_exit(backlight_class_exit);
542
543MODULE_LICENSE("GPL");
544MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
545MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");