blob: efc9a2da918d1f4398abecb03c7bd47158d8b8a3 [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;
Liu Yinga55944c2014-04-03 14:48:54 -070044 int node = evdata->info->node;
45 int fb_blank = 0;
James Simmons3d5eead2006-12-08 02:40:47 -080046
47 /* If we aren't interested in this event, skip it immediately ... */
Richard Purdie994efac2007-02-09 09:46:45 +000048 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
James Simmons3d5eead2006-12-08 02:40:47 -080049 return 0;
50
51 bd = container_of(self, struct backlight_device, fb_notif);
Richard Purdie599a52d2007-02-10 23:07:48 +000052 mutex_lock(&bd->ops_lock);
53 if (bd->ops)
54 if (!bd->ops->check_fb ||
Bruno Prémont57e148b2010-02-21 00:20:01 +010055 bd->ops->check_fb(bd, evdata->info)) {
Liu Yinga55944c2014-04-03 14:48:54 -070056 fb_blank = *(int *)evdata->data;
57 if (fb_blank == FB_BLANK_UNBLANK &&
58 !bd->fb_bl_on[node]) {
59 bd->fb_bl_on[node] = true;
60 if (!bd->use_count++) {
61 bd->props.state &= ~BL_CORE_FBBLANK;
62 bd->props.fb_blank = FB_BLANK_UNBLANK;
63 }
64 } else if (fb_blank != FB_BLANK_UNBLANK &&
65 bd->fb_bl_on[node]) {
66 bd->fb_bl_on[node] = false;
67 if (!(--bd->use_count)) {
68 bd->props.state |= BL_CORE_FBBLANK;
69 bd->props.fb_blank = fb_blank;
70 }
71 }
Richard Purdie28ee0862007-02-08 22:25:09 +000072 backlight_update_status(bd);
James Simmons3d5eead2006-12-08 02:40:47 -080073 }
Richard Purdie599a52d2007-02-10 23:07:48 +000074 mutex_unlock(&bd->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080075 return 0;
76}
77
78static int backlight_register_fb(struct backlight_device *bd)
79{
80 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
81 bd->fb_notif.notifier_call = fb_notifier_callback;
82
83 return fb_register_client(&bd->fb_notif);
84}
85
86static void backlight_unregister_fb(struct backlight_device *bd)
87{
88 fb_unregister_client(&bd->fb_notif);
89}
90#else
91static inline int backlight_register_fb(struct backlight_device *bd)
92{
93 return 0;
94}
95
96static inline void backlight_unregister_fb(struct backlight_device *bd)
97{
98}
99#endif /* CONFIG_FB */
100
Matthew Garrett325253a2009-07-14 17:06:02 +0100101static void backlight_generate_event(struct backlight_device *bd,
102 enum backlight_update_reason reason)
103{
104 char *envp[2];
105
106 switch (reason) {
107 case BACKLIGHT_UPDATE_SYSFS:
108 envp[0] = "SOURCE=sysfs";
109 break;
110 case BACKLIGHT_UPDATE_HOTKEY:
111 envp[0] = "SOURCE=hotkey";
112 break;
113 default:
114 envp[0] = "SOURCE=unknown";
115 break;
116 }
117 envp[1] = NULL;
118 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
Henrique de Moraes Holschuh89dfc282009-09-20 14:44:47 -0300119 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
Matthew Garrett325253a2009-07-14 17:06:02 +0100120}
121
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700122static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
123 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Richard Purdie655bfd72007-07-09 12:17:24 +0100125 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Richard Purdie599a52d2007-02-10 23:07:48 +0000127 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700130static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
131 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000133 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100134 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000135 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jingoo Han66655762012-01-10 15:09:19 -0800137 rc = kstrtoul(buf, 0, &power);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000138 if (rc)
139 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000141 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000142 mutex_lock(&bd->ops_lock);
143 if (bd->ops) {
Jingoo Han35f96162012-05-29 15:07:16 -0700144 pr_debug("set power to %lu\n", power);
Helge Deller51552452008-01-13 23:01:13 +0000145 if (bd->props.power != power) {
146 bd->props.power = power;
147 backlight_update_status(bd);
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800150 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000151 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 return rc;
154}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700155static DEVICE_ATTR_RW(bl_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700157static ssize_t brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100158 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Richard Purdie655bfd72007-07-09 12:17:24 +0100160 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Richard Purdie599a52d2007-02-10 23:07:48 +0000162 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700165static ssize_t brightness_store(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100166 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000168 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100169 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000170 unsigned long brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jingoo Han66655762012-01-10 15:09:19 -0800172 rc = kstrtoul(buf, 0, &brightness);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000173 if (rc)
174 return rc;
175
176 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Richard Purdie599a52d2007-02-10 23:07:48 +0000178 mutex_lock(&bd->ops_lock);
179 if (bd->ops) {
180 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800181 rc = -EINVAL;
182 else {
Jingoo Han35f96162012-05-29 15:07:16 -0700183 pr_debug("set brightness to %lu\n", brightness);
Zhang Rui9be1df92009-01-08 14:11:30 +0000184 bd->props.brightness = brightness;
185 backlight_update_status(bd);
Richard Purdie6ca01762006-03-31 02:31:49 -0800186 rc = count;
187 }
188 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000189 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Matthew Garrett325253a2009-07-14 17:06:02 +0100191 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return rc;
194}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700195static DEVICE_ATTR_RW(brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700197static ssize_t type_show(struct device *dev, struct device_attribute *attr,
198 char *buf)
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700199{
200 struct backlight_device *bd = to_backlight_device(dev);
201
202 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
203}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700204static DEVICE_ATTR_RO(type);
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700205
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700206static ssize_t max_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100207 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Richard Purdie655bfd72007-07-09 12:17:24 +0100209 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Richard Purdie599a52d2007-02-10 23:07:48 +0000211 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800212}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700213static DEVICE_ATTR_RO(max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800214
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700215static ssize_t actual_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100216 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800217{
218 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100219 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800220
Richard Purdie599a52d2007-02-10 23:07:48 +0000221 mutex_lock(&bd->ops_lock);
222 if (bd->ops && bd->ops->get_brightness)
223 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
224 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return rc;
227}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700228static DEVICE_ATTR_RO(actual_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100230static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100231
Shuah Khan36017922013-07-03 15:05:16 -0700232#ifdef CONFIG_PM_SLEEP
233static int backlight_suspend(struct device *dev)
Richard Purdiec835ee72009-01-06 21:00:19 +0000234{
235 struct backlight_device *bd = to_backlight_device(dev);
236
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800237 mutex_lock(&bd->ops_lock);
238 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000239 bd->props.state |= BL_CORE_SUSPENDED;
240 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000241 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800242 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000243
244 return 0;
245}
246
247static int backlight_resume(struct device *dev)
248{
249 struct backlight_device *bd = to_backlight_device(dev);
250
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800251 mutex_lock(&bd->ops_lock);
252 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000253 bd->props.state &= ~BL_CORE_SUSPENDED;
254 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000255 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800256 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000257
258 return 0;
259}
Shuah Khan36017922013-07-03 15:05:16 -0700260#endif
261
262static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
263 backlight_resume);
Richard Purdiec835ee72009-01-06 21:00:19 +0000264
Richard Purdie655bfd72007-07-09 12:17:24 +0100265static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct backlight_device *bd = to_backlight_device(dev);
268 kfree(bd);
269}
270
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700271static struct attribute *bl_device_attrs[] = {
272 &dev_attr_bl_power.attr,
273 &dev_attr_brightness.attr,
274 &dev_attr_actual_brightness.attr,
275 &dev_attr_max_brightness.attr,
276 &dev_attr_type.attr,
277 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700279ATTRIBUTE_GROUPS(bl_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/**
Matthew Garrett325253a2009-07-14 17:06:02 +0100282 * backlight_force_update - tell the backlight subsystem that hardware state
283 * has changed
284 * @bd: the backlight device to update
285 *
286 * Updates the internal state of the backlight in response to a hardware event,
287 * and generate a uevent to notify userspace
288 */
289void backlight_force_update(struct backlight_device *bd,
290 enum backlight_update_reason reason)
291{
292 mutex_lock(&bd->ops_lock);
293 if (bd->ops && bd->ops->get_brightness)
294 bd->props.brightness = bd->ops->get_brightness(bd);
295 mutex_unlock(&bd->ops_lock);
296 backlight_generate_event(bd, reason);
297}
298EXPORT_SYMBOL(backlight_force_update);
299
300/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * backlight_device_register - create and register a new object of
302 * backlight_device class.
303 * @name: the name of the new object(must be the same as the name of the
304 * respective framebuffer device).
Sebastian Siewiorf6ec2d92008-07-16 23:05:49 +0100305 * @parent: a pointer to the parent device
Richard Purdie655bfd72007-07-09 12:17:24 +0100306 * @devdata: an optional pointer to be stored for private driver use. The
307 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000308 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100310 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * ERR_PTR() or a pointer to the newly allocated device.
312 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800313struct backlight_device *backlight_device_register(const char *name,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500314 struct device *parent, void *devdata, const struct backlight_ops *ops,
315 const struct backlight_properties *props)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100318 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Richard Purdie655bfd72007-07-09 12:17:24 +0100320 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Richard Purdie599a52d2007-02-10 23:07:48 +0000322 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000323 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800324 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Richard Purdie28ee0862007-02-08 22:25:09 +0000326 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000327 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Richard Purdie655bfd72007-07-09 12:17:24 +0100329 new_bd->dev.class = backlight_class;
330 new_bd->dev.parent = parent;
331 new_bd->dev.release = bl_device_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700332 dev_set_name(&new_bd->dev, "%s", name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100333 dev_set_drvdata(&new_bd->dev, devdata);
334
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500335 /* Set default properties */
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700336 if (props) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500337 memcpy(&new_bd->props, props,
338 sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700339 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
340 WARN(1, "%s: invalid backlight type", name);
341 new_bd->props.type = BACKLIGHT_RAW;
342 }
343 } else {
344 new_bd->props.type = BACKLIGHT_RAW;
345 }
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500346
Richard Purdie655bfd72007-07-09 12:17:24 +0100347 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000348 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000349 kfree(new_bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return ERR_PTR(rc);
351 }
352
James Simmons3d5eead2006-12-08 02:40:47 -0800353 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000354 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100355 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000356 return ERR_PTR(rc);
357 }
358
Richard Purdie655bfd72007-07-09 12:17:24 +0100359 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Richard Purdie321709c2007-02-10 15:04:08 +0000361#ifdef CONFIG_PMAC_BACKLIGHT
362 mutex_lock(&pmac_backlight_mutex);
363 if (!pmac_backlight)
364 pmac_backlight = new_bd;
365 mutex_unlock(&pmac_backlight_mutex);
366#endif
367
Aaron Lu5915a3d2013-10-11 21:27:43 +0800368 mutex_lock(&backlight_dev_list_mutex);
369 list_add(&new_bd->entry, &backlight_dev_list);
370 mutex_unlock(&backlight_dev_list_mutex);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return new_bd;
373}
374EXPORT_SYMBOL(backlight_device_register);
375
Aaron Lu5915a3d2013-10-11 21:27:43 +0800376bool backlight_device_registered(enum backlight_type type)
377{
378 bool found = false;
379 struct backlight_device *bd;
380
381 mutex_lock(&backlight_dev_list_mutex);
382 list_for_each_entry(bd, &backlight_dev_list, entry) {
383 if (bd->props.type == type) {
384 found = true;
385 break;
386 }
387 }
388 mutex_unlock(&backlight_dev_list_mutex);
389
390 return found;
391}
392EXPORT_SYMBOL(backlight_device_registered);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/**
395 * backlight_device_unregister - unregisters a backlight device object.
396 * @bd: the backlight device object to be unregistered and freed.
397 *
398 * Unregisters a previously registered via backlight_device_register object.
399 */
400void backlight_device_unregister(struct backlight_device *bd)
401{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!bd)
403 return;
404
Aaron Lu5915a3d2013-10-11 21:27:43 +0800405 mutex_lock(&backlight_dev_list_mutex);
406 list_del(&bd->entry);
407 mutex_unlock(&backlight_dev_list_mutex);
408
Richard Purdie321709c2007-02-10 15:04:08 +0000409#ifdef CONFIG_PMAC_BACKLIGHT
410 mutex_lock(&pmac_backlight_mutex);
411 if (pmac_backlight == bd)
412 pmac_backlight = NULL;
413 mutex_unlock(&pmac_backlight_mutex);
414#endif
Richard Purdie599a52d2007-02-10 23:07:48 +0000415 mutex_lock(&bd->ops_lock);
416 bd->ops = NULL;
417 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
James Simmons3d5eead2006-12-08 02:40:47 -0800419 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100420 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422EXPORT_SYMBOL(backlight_device_unregister);
423
Jingoo Han8318fde42013-07-03 15:05:13 -0700424static void devm_backlight_device_release(struct device *dev, void *res)
425{
426 struct backlight_device *backlight = *(struct backlight_device **)res;
427
428 backlight_device_unregister(backlight);
429}
430
431static int devm_backlight_device_match(struct device *dev, void *res,
432 void *data)
433{
434 struct backlight_device **r = res;
435
436 return *r == data;
437}
438
439/**
440 * devm_backlight_device_register - resource managed backlight_device_register()
441 * @dev: the device to register
442 * @name: the name of the device
443 * @parent: a pointer to the parent device
444 * @devdata: an optional pointer to be stored for private driver use
445 * @ops: the backlight operations structure
446 * @props: the backlight properties
447 *
448 * @return a struct backlight on success, or an ERR_PTR on error
449 *
450 * Managed backlight_device_register(). The backlight_device returned
451 * from this function are automatically freed on driver detach.
452 * See backlight_device_register() for more information.
453 */
454struct backlight_device *devm_backlight_device_register(struct device *dev,
455 const char *name, struct device *parent, void *devdata,
456 const struct backlight_ops *ops,
457 const struct backlight_properties *props)
458{
459 struct backlight_device **ptr, *backlight;
460
461 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
462 GFP_KERNEL);
463 if (!ptr)
464 return ERR_PTR(-ENOMEM);
465
466 backlight = backlight_device_register(name, parent, devdata, ops,
467 props);
468 if (!IS_ERR(backlight)) {
469 *ptr = backlight;
470 devres_add(dev, ptr);
471 } else {
472 devres_free(ptr);
473 }
474
475 return backlight;
476}
477EXPORT_SYMBOL(devm_backlight_device_register);
478
479/**
480 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
481 * @dev: the device to unregister
482 * @bd: the backlight device to unregister
483 *
484 * Deallocated a backlight allocated with devm_backlight_device_register().
485 * Normally this function will not need to be called and the resource management
486 * code will ensure that the resource is freed.
487 */
488void devm_backlight_device_unregister(struct device *dev,
489 struct backlight_device *bd)
490{
491 int rc;
492
493 rc = devres_release(dev, devm_backlight_device_release,
494 devm_backlight_device_match, bd);
495 WARN_ON(rc);
496}
497EXPORT_SYMBOL(devm_backlight_device_unregister);
498
Thierry Reding762a9362012-12-17 16:01:06 -0800499#ifdef CONFIG_OF
Greg Kroah-Hartman3213f632013-02-06 16:43:02 -0800500static int of_parent_match(struct device *dev, const void *data)
Thierry Reding762a9362012-12-17 16:01:06 -0800501{
502 return dev->parent && dev->parent->of_node == data;
503}
504
505/**
506 * of_find_backlight_by_node() - find backlight device by device-tree node
507 * @node: device-tree node of the backlight device
508 *
509 * Returns a pointer to the backlight device corresponding to the given DT
510 * node or NULL if no such backlight device exists or if the device hasn't
511 * been probed yet.
512 *
513 * This function obtains a reference on the backlight device and it is the
514 * caller's responsibility to drop the reference by calling put_device() on
515 * the backlight device's .dev field.
516 */
517struct backlight_device *of_find_backlight_by_node(struct device_node *node)
518{
519 struct device *dev;
520
521 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
522
523 return dev ? to_backlight_device(dev) : NULL;
524}
525EXPORT_SYMBOL(of_find_backlight_by_node);
526#endif
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static void __exit backlight_class_exit(void)
529{
Richard Purdie655bfd72007-07-09 12:17:24 +0100530 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533static int __init backlight_class_init(void)
534{
Richard Purdie655bfd72007-07-09 12:17:24 +0100535 backlight_class = class_create(THIS_MODULE, "backlight");
536 if (IS_ERR(backlight_class)) {
Jingoo Han35f96162012-05-29 15:07:16 -0700537 pr_warn("Unable to create backlight class; errno = %ld\n",
538 PTR_ERR(backlight_class));
Richard Purdie655bfd72007-07-09 12:17:24 +0100539 return PTR_ERR(backlight_class);
540 }
541
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700542 backlight_class->dev_groups = bl_device_groups;
Shuah Khan36017922013-07-03 15:05:16 -0700543 backlight_class->pm = &backlight_class_dev_pm_ops;
Aaron Lu5915a3d2013-10-11 21:27:43 +0800544 INIT_LIST_HEAD(&backlight_dev_list);
545 mutex_init(&backlight_dev_list_mutex);
Richard Purdie655bfd72007-07-09 12:17:24 +0100546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/*
550 * if this is compiled into the kernel, we need to ensure that the
551 * class is registered before users of the class try to register lcd's
552 */
553postcore_initcall(backlight_class_init);
554module_exit(backlight_class_exit);
555
556MODULE_LICENSE("GPL");
557MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
558MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");