blob: 27d3cf255e78f3045a3190cd0c1451e1a216d0f3 [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,
Liu Ying8c16f332014-04-03 14:48:55 -070037 * and if it is and necessary, we're switching backlight power as well ...
James Simmons3d5eead2006-12-08 02:40:47 -080038 */
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;
Liu Ying8c16f332014-04-03 14:48:55 -070063 backlight_update_status(bd);
Liu Yinga55944c2014-04-03 14:48:54 -070064 }
65 } else if (fb_blank != FB_BLANK_UNBLANK &&
66 bd->fb_bl_on[node]) {
67 bd->fb_bl_on[node] = false;
68 if (!(--bd->use_count)) {
69 bd->props.state |= BL_CORE_FBBLANK;
70 bd->props.fb_blank = fb_blank;
Liu Ying8c16f332014-04-03 14:48:55 -070071 backlight_update_status(bd);
Liu Yinga55944c2014-04-03 14:48:54 -070072 }
73 }
James Simmons3d5eead2006-12-08 02:40:47 -080074 }
Richard Purdie599a52d2007-02-10 23:07:48 +000075 mutex_unlock(&bd->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080076 return 0;
77}
78
79static int backlight_register_fb(struct backlight_device *bd)
80{
81 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
82 bd->fb_notif.notifier_call = fb_notifier_callback;
83
84 return fb_register_client(&bd->fb_notif);
85}
86
87static void backlight_unregister_fb(struct backlight_device *bd)
88{
89 fb_unregister_client(&bd->fb_notif);
90}
91#else
92static inline int backlight_register_fb(struct backlight_device *bd)
93{
94 return 0;
95}
96
97static inline void backlight_unregister_fb(struct backlight_device *bd)
98{
99}
100#endif /* CONFIG_FB */
101
Matthew Garrett325253a2009-07-14 17:06:02 +0100102static void backlight_generate_event(struct backlight_device *bd,
103 enum backlight_update_reason reason)
104{
105 char *envp[2];
106
107 switch (reason) {
108 case BACKLIGHT_UPDATE_SYSFS:
109 envp[0] = "SOURCE=sysfs";
110 break;
111 case BACKLIGHT_UPDATE_HOTKEY:
112 envp[0] = "SOURCE=hotkey";
113 break;
114 default:
115 envp[0] = "SOURCE=unknown";
116 break;
117 }
118 envp[1] = NULL;
119 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
Henrique de Moraes Holschuh89dfc282009-09-20 14:44:47 -0300120 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
Matthew Garrett325253a2009-07-14 17:06:02 +0100121}
122
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700123static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
124 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Richard Purdie655bfd72007-07-09 12:17:24 +0100126 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Richard Purdie599a52d2007-02-10 23:07:48 +0000128 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700131static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
132 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000134 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100135 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000136 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Jingoo Han66655762012-01-10 15:09:19 -0800138 rc = kstrtoul(buf, 0, &power);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000139 if (rc)
140 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000142 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000143 mutex_lock(&bd->ops_lock);
144 if (bd->ops) {
Jingoo Han35f96162012-05-29 15:07:16 -0700145 pr_debug("set power to %lu\n", power);
Helge Deller51552452008-01-13 23:01:13 +0000146 if (bd->props.power != power) {
147 bd->props.power = power;
148 backlight_update_status(bd);
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800151 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000152 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return rc;
155}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700156static DEVICE_ATTR_RW(bl_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700158static ssize_t brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100159 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Richard Purdie655bfd72007-07-09 12:17:24 +0100161 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Richard Purdie599a52d2007-02-10 23:07:48 +0000163 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700166static ssize_t brightness_store(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100167 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000169 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100170 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000171 unsigned long brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jingoo Han66655762012-01-10 15:09:19 -0800173 rc = kstrtoul(buf, 0, &brightness);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000174 if (rc)
175 return rc;
176
177 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Richard Purdie599a52d2007-02-10 23:07:48 +0000179 mutex_lock(&bd->ops_lock);
180 if (bd->ops) {
181 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800182 rc = -EINVAL;
183 else {
Jingoo Han35f96162012-05-29 15:07:16 -0700184 pr_debug("set brightness to %lu\n", brightness);
Zhang Rui9be1df92009-01-08 14:11:30 +0000185 bd->props.brightness = brightness;
186 backlight_update_status(bd);
Richard Purdie6ca01762006-03-31 02:31:49 -0800187 rc = count;
188 }
189 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000190 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Matthew Garrett325253a2009-07-14 17:06:02 +0100192 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return rc;
195}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700196static DEVICE_ATTR_RW(brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700198static ssize_t type_show(struct device *dev, struct device_attribute *attr,
199 char *buf)
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700200{
201 struct backlight_device *bd = to_backlight_device(dev);
202
203 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
204}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700205static DEVICE_ATTR_RO(type);
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700206
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700207static ssize_t max_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100208 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Richard Purdie655bfd72007-07-09 12:17:24 +0100210 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Richard Purdie599a52d2007-02-10 23:07:48 +0000212 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800213}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700214static DEVICE_ATTR_RO(max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800215
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700216static ssize_t actual_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100217 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800218{
219 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100220 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800221
Richard Purdie599a52d2007-02-10 23:07:48 +0000222 mutex_lock(&bd->ops_lock);
223 if (bd->ops && bd->ops->get_brightness)
224 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
225 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 return rc;
228}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700229static DEVICE_ATTR_RO(actual_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100231static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100232
Shuah Khan36017922013-07-03 15:05:16 -0700233#ifdef CONFIG_PM_SLEEP
234static int backlight_suspend(struct device *dev)
Richard Purdiec835ee72009-01-06 21:00:19 +0000235{
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}
247
248static int backlight_resume(struct device *dev)
249{
250 struct backlight_device *bd = to_backlight_device(dev);
251
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800252 mutex_lock(&bd->ops_lock);
253 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000254 bd->props.state &= ~BL_CORE_SUSPENDED;
255 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000256 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800257 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000258
259 return 0;
260}
Shuah Khan36017922013-07-03 15:05:16 -0700261#endif
262
263static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
264 backlight_resume);
Richard Purdiec835ee72009-01-06 21:00:19 +0000265
Richard Purdie655bfd72007-07-09 12:17:24 +0100266static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct backlight_device *bd = to_backlight_device(dev);
269 kfree(bd);
270}
271
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700272static struct attribute *bl_device_attrs[] = {
273 &dev_attr_bl_power.attr,
274 &dev_attr_brightness.attr,
275 &dev_attr_actual_brightness.attr,
276 &dev_attr_max_brightness.attr,
277 &dev_attr_type.attr,
278 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700280ATTRIBUTE_GROUPS(bl_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/**
Matthew Garrett325253a2009-07-14 17:06:02 +0100283 * backlight_force_update - tell the backlight subsystem that hardware state
284 * has changed
285 * @bd: the backlight device to update
286 *
287 * Updates the internal state of the backlight in response to a hardware event,
288 * and generate a uevent to notify userspace
289 */
290void backlight_force_update(struct backlight_device *bd,
291 enum backlight_update_reason reason)
292{
293 mutex_lock(&bd->ops_lock);
294 if (bd->ops && bd->ops->get_brightness)
295 bd->props.brightness = bd->ops->get_brightness(bd);
296 mutex_unlock(&bd->ops_lock);
297 backlight_generate_event(bd, reason);
298}
299EXPORT_SYMBOL(backlight_force_update);
300
301/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * backlight_device_register - create and register a new object of
303 * backlight_device class.
304 * @name: the name of the new object(must be the same as the name of the
305 * respective framebuffer device).
Sebastian Siewiorf6ec2d92008-07-16 23:05:49 +0100306 * @parent: a pointer to the parent device
Richard Purdie655bfd72007-07-09 12:17:24 +0100307 * @devdata: an optional pointer to be stored for private driver use. The
308 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000309 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100311 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * ERR_PTR() or a pointer to the newly allocated device.
313 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800314struct backlight_device *backlight_device_register(const char *name,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500315 struct device *parent, void *devdata, const struct backlight_ops *ops,
316 const struct backlight_properties *props)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100319 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Richard Purdie655bfd72007-07-09 12:17:24 +0100321 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Richard Purdie599a52d2007-02-10 23:07:48 +0000323 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000324 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800325 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Richard Purdie28ee0862007-02-08 22:25:09 +0000327 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000328 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Richard Purdie655bfd72007-07-09 12:17:24 +0100330 new_bd->dev.class = backlight_class;
331 new_bd->dev.parent = parent;
332 new_bd->dev.release = bl_device_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700333 dev_set_name(&new_bd->dev, "%s", name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100334 dev_set_drvdata(&new_bd->dev, devdata);
335
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500336 /* Set default properties */
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700337 if (props) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500338 memcpy(&new_bd->props, props,
339 sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700340 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
341 WARN(1, "%s: invalid backlight type", name);
342 new_bd->props.type = BACKLIGHT_RAW;
343 }
344 } else {
345 new_bd->props.type = BACKLIGHT_RAW;
346 }
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500347
Richard Purdie655bfd72007-07-09 12:17:24 +0100348 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000349 if (rc) {
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000350 kfree(new_bd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return ERR_PTR(rc);
352 }
353
James Simmons3d5eead2006-12-08 02:40:47 -0800354 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000355 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100356 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000357 return ERR_PTR(rc);
358 }
359
Richard Purdie655bfd72007-07-09 12:17:24 +0100360 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Richard Purdie321709c2007-02-10 15:04:08 +0000362#ifdef CONFIG_PMAC_BACKLIGHT
363 mutex_lock(&pmac_backlight_mutex);
364 if (!pmac_backlight)
365 pmac_backlight = new_bd;
366 mutex_unlock(&pmac_backlight_mutex);
367#endif
368
Aaron Lu5915a3d2013-10-11 21:27:43 +0800369 mutex_lock(&backlight_dev_list_mutex);
370 list_add(&new_bd->entry, &backlight_dev_list);
371 mutex_unlock(&backlight_dev_list_mutex);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return new_bd;
374}
375EXPORT_SYMBOL(backlight_device_register);
376
Aaron Lu5915a3d2013-10-11 21:27:43 +0800377bool backlight_device_registered(enum backlight_type type)
378{
379 bool found = false;
380 struct backlight_device *bd;
381
382 mutex_lock(&backlight_dev_list_mutex);
383 list_for_each_entry(bd, &backlight_dev_list, entry) {
384 if (bd->props.type == type) {
385 found = true;
386 break;
387 }
388 }
389 mutex_unlock(&backlight_dev_list_mutex);
390
391 return found;
392}
393EXPORT_SYMBOL(backlight_device_registered);
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/**
396 * backlight_device_unregister - unregisters a backlight device object.
397 * @bd: the backlight device object to be unregistered and freed.
398 *
399 * Unregisters a previously registered via backlight_device_register object.
400 */
401void backlight_device_unregister(struct backlight_device *bd)
402{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!bd)
404 return;
405
Aaron Lu5915a3d2013-10-11 21:27:43 +0800406 mutex_lock(&backlight_dev_list_mutex);
407 list_del(&bd->entry);
408 mutex_unlock(&backlight_dev_list_mutex);
409
Richard Purdie321709c2007-02-10 15:04:08 +0000410#ifdef CONFIG_PMAC_BACKLIGHT
411 mutex_lock(&pmac_backlight_mutex);
412 if (pmac_backlight == bd)
413 pmac_backlight = NULL;
414 mutex_unlock(&pmac_backlight_mutex);
415#endif
Richard Purdie599a52d2007-02-10 23:07:48 +0000416 mutex_lock(&bd->ops_lock);
417 bd->ops = NULL;
418 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
James Simmons3d5eead2006-12-08 02:40:47 -0800420 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100421 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423EXPORT_SYMBOL(backlight_device_unregister);
424
Jingoo Han8318fde42013-07-03 15:05:13 -0700425static void devm_backlight_device_release(struct device *dev, void *res)
426{
427 struct backlight_device *backlight = *(struct backlight_device **)res;
428
429 backlight_device_unregister(backlight);
430}
431
432static int devm_backlight_device_match(struct device *dev, void *res,
433 void *data)
434{
435 struct backlight_device **r = res;
436
437 return *r == data;
438}
439
440/**
441 * devm_backlight_device_register - resource managed backlight_device_register()
442 * @dev: the device to register
443 * @name: the name of the device
444 * @parent: a pointer to the parent device
445 * @devdata: an optional pointer to be stored for private driver use
446 * @ops: the backlight operations structure
447 * @props: the backlight properties
448 *
449 * @return a struct backlight on success, or an ERR_PTR on error
450 *
451 * Managed backlight_device_register(). The backlight_device returned
452 * from this function are automatically freed on driver detach.
453 * See backlight_device_register() for more information.
454 */
455struct backlight_device *devm_backlight_device_register(struct device *dev,
456 const char *name, struct device *parent, void *devdata,
457 const struct backlight_ops *ops,
458 const struct backlight_properties *props)
459{
460 struct backlight_device **ptr, *backlight;
461
462 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
463 GFP_KERNEL);
464 if (!ptr)
465 return ERR_PTR(-ENOMEM);
466
467 backlight = backlight_device_register(name, parent, devdata, ops,
468 props);
469 if (!IS_ERR(backlight)) {
470 *ptr = backlight;
471 devres_add(dev, ptr);
472 } else {
473 devres_free(ptr);
474 }
475
476 return backlight;
477}
478EXPORT_SYMBOL(devm_backlight_device_register);
479
480/**
481 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
482 * @dev: the device to unregister
483 * @bd: the backlight device to unregister
484 *
485 * Deallocated a backlight allocated with devm_backlight_device_register().
486 * Normally this function will not need to be called and the resource management
487 * code will ensure that the resource is freed.
488 */
489void devm_backlight_device_unregister(struct device *dev,
490 struct backlight_device *bd)
491{
492 int rc;
493
494 rc = devres_release(dev, devm_backlight_device_release,
495 devm_backlight_device_match, bd);
496 WARN_ON(rc);
497}
498EXPORT_SYMBOL(devm_backlight_device_unregister);
499
Thierry Reding762a9362012-12-17 16:01:06 -0800500#ifdef CONFIG_OF
Greg Kroah-Hartman3213f632013-02-06 16:43:02 -0800501static int of_parent_match(struct device *dev, const void *data)
Thierry Reding762a9362012-12-17 16:01:06 -0800502{
503 return dev->parent && dev->parent->of_node == data;
504}
505
506/**
507 * of_find_backlight_by_node() - find backlight device by device-tree node
508 * @node: device-tree node of the backlight device
509 *
510 * Returns a pointer to the backlight device corresponding to the given DT
511 * node or NULL if no such backlight device exists or if the device hasn't
512 * been probed yet.
513 *
514 * This function obtains a reference on the backlight device and it is the
515 * caller's responsibility to drop the reference by calling put_device() on
516 * the backlight device's .dev field.
517 */
518struct backlight_device *of_find_backlight_by_node(struct device_node *node)
519{
520 struct device *dev;
521
522 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
523
524 return dev ? to_backlight_device(dev) : NULL;
525}
526EXPORT_SYMBOL(of_find_backlight_by_node);
527#endif
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static void __exit backlight_class_exit(void)
530{
Richard Purdie655bfd72007-07-09 12:17:24 +0100531 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534static int __init backlight_class_init(void)
535{
Richard Purdie655bfd72007-07-09 12:17:24 +0100536 backlight_class = class_create(THIS_MODULE, "backlight");
537 if (IS_ERR(backlight_class)) {
Jingoo Han35f96162012-05-29 15:07:16 -0700538 pr_warn("Unable to create backlight class; errno = %ld\n",
539 PTR_ERR(backlight_class));
Richard Purdie655bfd72007-07-09 12:17:24 +0100540 return PTR_ERR(backlight_class);
541 }
542
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700543 backlight_class->dev_groups = bl_device_groups;
Shuah Khan36017922013-07-03 15:05:16 -0700544 backlight_class->pm = &backlight_class_dev_pm_ops;
Aaron Lu5915a3d2013-10-11 21:27:43 +0800545 INIT_LIST_HEAD(&backlight_dev_list);
546 mutex_init(&backlight_dev_list_mutex);
Richard Purdie655bfd72007-07-09 12:17:24 +0100547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
551 * if this is compiled into the kernel, we need to ensure that the
552 * class is registered before users of the class try to register lcd's
553 */
554postcore_initcall(backlight_class_init);
555module_exit(backlight_class_exit);
556
557MODULE_LICENSE("GPL");
558MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
559MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");