blob: bddc8b17a4d8a688f978aa82c58ff8c7514b21f0 [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;
Hans de Goede3cc69192014-05-21 15:39:54 +020026static struct blocking_notifier_head backlight_notifier;
Aaron Lu5915a3d2013-10-11 21:27:43 +080027
Bart Van Asschec338bfb2011-09-10 20:13:01 +020028static const char *const backlight_types[] = {
Matthew Garrettbb7ca742011-03-22 16:30:21 -070029 [BACKLIGHT_RAW] = "raw",
30 [BACKLIGHT_PLATFORM] = "platform",
31 [BACKLIGHT_FIRMWARE] = "firmware",
32};
33
James Simmons3d5eead2006-12-08 02:40:47 -080034#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
35 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
36/* This callback gets called when something important happens inside a
37 * framebuffer driver. We're looking if that important event is blanking,
Liu Ying8c16f332014-04-03 14:48:55 -070038 * and if it is and necessary, we're switching backlight power as well ...
James Simmons3d5eead2006-12-08 02:40:47 -080039 */
40static int fb_notifier_callback(struct notifier_block *self,
41 unsigned long event, void *data)
42{
43 struct backlight_device *bd;
44 struct fb_event *evdata = data;
Liu Yinga55944c2014-04-03 14:48:54 -070045 int node = evdata->info->node;
46 int fb_blank = 0;
James Simmons3d5eead2006-12-08 02:40:47 -080047
48 /* If we aren't interested in this event, skip it immediately ... */
Richard Purdie994efac2007-02-09 09:46:45 +000049 if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
James Simmons3d5eead2006-12-08 02:40:47 -080050 return 0;
51
52 bd = container_of(self, struct backlight_device, fb_notif);
Richard Purdie599a52d2007-02-10 23:07:48 +000053 mutex_lock(&bd->ops_lock);
54 if (bd->ops)
55 if (!bd->ops->check_fb ||
Bruno Prémont57e148b2010-02-21 00:20:01 +010056 bd->ops->check_fb(bd, evdata->info)) {
Liu Yinga55944c2014-04-03 14:48:54 -070057 fb_blank = *(int *)evdata->data;
58 if (fb_blank == FB_BLANK_UNBLANK &&
59 !bd->fb_bl_on[node]) {
60 bd->fb_bl_on[node] = true;
61 if (!bd->use_count++) {
62 bd->props.state &= ~BL_CORE_FBBLANK;
63 bd->props.fb_blank = FB_BLANK_UNBLANK;
Liu Ying8c16f332014-04-03 14:48:55 -070064 backlight_update_status(bd);
Liu Yinga55944c2014-04-03 14:48:54 -070065 }
66 } else if (fb_blank != FB_BLANK_UNBLANK &&
67 bd->fb_bl_on[node]) {
68 bd->fb_bl_on[node] = false;
69 if (!(--bd->use_count)) {
70 bd->props.state |= BL_CORE_FBBLANK;
71 bd->props.fb_blank = fb_blank;
Liu Ying8c16f332014-04-03 14:48:55 -070072 backlight_update_status(bd);
Liu Yinga55944c2014-04-03 14:48:54 -070073 }
74 }
James Simmons3d5eead2006-12-08 02:40:47 -080075 }
Richard Purdie599a52d2007-02-10 23:07:48 +000076 mutex_unlock(&bd->ops_lock);
James Simmons3d5eead2006-12-08 02:40:47 -080077 return 0;
78}
79
80static int backlight_register_fb(struct backlight_device *bd)
81{
82 memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
83 bd->fb_notif.notifier_call = fb_notifier_callback;
84
85 return fb_register_client(&bd->fb_notif);
86}
87
88static void backlight_unregister_fb(struct backlight_device *bd)
89{
90 fb_unregister_client(&bd->fb_notif);
91}
92#else
93static inline int backlight_register_fb(struct backlight_device *bd)
94{
95 return 0;
96}
97
98static inline void backlight_unregister_fb(struct backlight_device *bd)
99{
100}
101#endif /* CONFIG_FB */
102
Matthew Garrett325253a2009-07-14 17:06:02 +0100103static void backlight_generate_event(struct backlight_device *bd,
104 enum backlight_update_reason reason)
105{
106 char *envp[2];
107
108 switch (reason) {
109 case BACKLIGHT_UPDATE_SYSFS:
110 envp[0] = "SOURCE=sysfs";
111 break;
112 case BACKLIGHT_UPDATE_HOTKEY:
113 envp[0] = "SOURCE=hotkey";
114 break;
115 default:
116 envp[0] = "SOURCE=unknown";
117 break;
118 }
119 envp[1] = NULL;
120 kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
Henrique de Moraes Holschuh89dfc282009-09-20 14:44:47 -0300121 sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
Matthew Garrett325253a2009-07-14 17:06:02 +0100122}
123
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700124static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
125 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Richard Purdie655bfd72007-07-09 12:17:24 +0100127 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Richard Purdie599a52d2007-02-10 23:07:48 +0000129 return sprintf(buf, "%d\n", bd->props.power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700132static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
133 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000135 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100136 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000137 unsigned long power;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jingoo Han66655762012-01-10 15:09:19 -0800139 rc = kstrtoul(buf, 0, &power);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000140 if (rc)
141 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000143 rc = -ENXIO;
Richard Purdie599a52d2007-02-10 23:07:48 +0000144 mutex_lock(&bd->ops_lock);
145 if (bd->ops) {
Jingoo Han35f96162012-05-29 15:07:16 -0700146 pr_debug("set power to %lu\n", power);
Helge Deller51552452008-01-13 23:01:13 +0000147 if (bd->props.power != power) {
148 bd->props.power = power;
149 backlight_update_status(bd);
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 rc = count;
Richard Purdie6ca01762006-03-31 02:31:49 -0800152 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000153 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return rc;
156}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700157static DEVICE_ATTR_RW(bl_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700159static ssize_t brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100160 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Richard Purdie655bfd72007-07-09 12:17:24 +0100162 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Richard Purdie599a52d2007-02-10 23:07:48 +0000164 return sprintf(buf, "%d\n", bd->props.brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700167static ssize_t brightness_store(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100168 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000170 int rc;
Richard Purdie655bfd72007-07-09 12:17:24 +0100171 struct backlight_device *bd = to_backlight_device(dev);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000172 unsigned long brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jingoo Han66655762012-01-10 15:09:19 -0800174 rc = kstrtoul(buf, 0, &brightness);
Pavel Machek9a2c61a2008-12-03 08:43:48 +0000175 if (rc)
176 return rc;
177
178 rc = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Richard Purdie599a52d2007-02-10 23:07:48 +0000180 mutex_lock(&bd->ops_lock);
181 if (bd->ops) {
182 if (brightness > bd->props.max_brightness)
Richard Purdie6ca01762006-03-31 02:31:49 -0800183 rc = -EINVAL;
184 else {
Jingoo Han35f96162012-05-29 15:07:16 -0700185 pr_debug("set brightness to %lu\n", brightness);
Zhang Rui9be1df92009-01-08 14:11:30 +0000186 bd->props.brightness = brightness;
187 backlight_update_status(bd);
Richard Purdie6ca01762006-03-31 02:31:49 -0800188 rc = count;
189 }
190 }
Richard Purdie599a52d2007-02-10 23:07:48 +0000191 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Matthew Garrett325253a2009-07-14 17:06:02 +0100193 backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return rc;
196}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700197static DEVICE_ATTR_RW(brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700199static ssize_t type_show(struct device *dev, struct device_attribute *attr,
200 char *buf)
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700201{
202 struct backlight_device *bd = to_backlight_device(dev);
203
204 return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
205}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700206static DEVICE_ATTR_RO(type);
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700207
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700208static ssize_t max_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100209 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Richard Purdie655bfd72007-07-09 12:17:24 +0100211 struct backlight_device *bd = to_backlight_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Richard Purdie599a52d2007-02-10 23:07:48 +0000213 return sprintf(buf, "%d\n", bd->props.max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800214}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700215static DEVICE_ATTR_RO(max_brightness);
Richard Purdie6ca01762006-03-31 02:31:49 -0800216
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700217static ssize_t actual_brightness_show(struct device *dev,
Richard Purdie655bfd72007-07-09 12:17:24 +0100218 struct device_attribute *attr, char *buf)
Richard Purdie6ca01762006-03-31 02:31:49 -0800219{
220 int rc = -ENXIO;
Richard Purdie655bfd72007-07-09 12:17:24 +0100221 struct backlight_device *bd = to_backlight_device(dev);
Richard Purdie6ca01762006-03-31 02:31:49 -0800222
Richard Purdie599a52d2007-02-10 23:07:48 +0000223 mutex_lock(&bd->ops_lock);
224 if (bd->ops && bd->ops->get_brightness)
225 rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
Andrzej Hajdab3de3402014-05-30 12:10:49 +0200226 else
227 rc = sprintf(buf, "%d\n", bd->props.brightness);
Richard Purdie599a52d2007-02-10 23:07:48 +0000228 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 return rc;
231}
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700232static DEVICE_ATTR_RO(actual_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Adrian Bunk0ad90ef2007-08-11 10:27:19 +0100234static struct class *backlight_class;
Richard Purdie655bfd72007-07-09 12:17:24 +0100235
Shuah Khan36017922013-07-03 15:05:16 -0700236#ifdef CONFIG_PM_SLEEP
237static int backlight_suspend(struct device *dev)
Richard Purdiec835ee72009-01-06 21:00:19 +0000238{
239 struct backlight_device *bd = to_backlight_device(dev);
240
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800241 mutex_lock(&bd->ops_lock);
242 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000243 bd->props.state |= BL_CORE_SUSPENDED;
244 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000245 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800246 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000247
248 return 0;
249}
250
251static int backlight_resume(struct device *dev)
252{
253 struct backlight_device *bd = to_backlight_device(dev);
254
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800255 mutex_lock(&bd->ops_lock);
256 if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
Richard Purdiec835ee72009-01-06 21:00:19 +0000257 bd->props.state &= ~BL_CORE_SUSPENDED;
258 backlight_update_status(bd);
Richard Purdiec835ee72009-01-06 21:00:19 +0000259 }
Uwe Kleine-Königd1d73572010-11-24 12:57:14 -0800260 mutex_unlock(&bd->ops_lock);
Richard Purdiec835ee72009-01-06 21:00:19 +0000261
262 return 0;
263}
Shuah Khan36017922013-07-03 15:05:16 -0700264#endif
265
266static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
267 backlight_resume);
Richard Purdiec835ee72009-01-06 21:00:19 +0000268
Richard Purdie655bfd72007-07-09 12:17:24 +0100269static void bl_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct backlight_device *bd = to_backlight_device(dev);
272 kfree(bd);
273}
274
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700275static struct attribute *bl_device_attrs[] = {
276 &dev_attr_bl_power.attr,
277 &dev_attr_brightness.attr,
278 &dev_attr_actual_brightness.attr,
279 &dev_attr_max_brightness.attr,
280 &dev_attr_type.attr,
281 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700283ATTRIBUTE_GROUPS(bl_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/**
Matthew Garrett325253a2009-07-14 17:06:02 +0100286 * backlight_force_update - tell the backlight subsystem that hardware state
287 * has changed
288 * @bd: the backlight device to update
289 *
290 * Updates the internal state of the backlight in response to a hardware event,
291 * and generate a uevent to notify userspace
292 */
293void backlight_force_update(struct backlight_device *bd,
294 enum backlight_update_reason reason)
295{
296 mutex_lock(&bd->ops_lock);
297 if (bd->ops && bd->ops->get_brightness)
298 bd->props.brightness = bd->ops->get_brightness(bd);
299 mutex_unlock(&bd->ops_lock);
300 backlight_generate_event(bd, reason);
301}
302EXPORT_SYMBOL(backlight_force_update);
303
304/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * backlight_device_register - create and register a new object of
306 * backlight_device class.
307 * @name: the name of the new object(must be the same as the name of the
308 * respective framebuffer device).
Sebastian Siewiorf6ec2d92008-07-16 23:05:49 +0100309 * @parent: a pointer to the parent device
Richard Purdie655bfd72007-07-09 12:17:24 +0100310 * @devdata: an optional pointer to be stored for private driver use. The
311 * methods may retrieve it by using bl_get_data(bd).
Richard Purdie599a52d2007-02-10 23:07:48 +0000312 * @ops: the backlight operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
Richard Purdie655bfd72007-07-09 12:17:24 +0100314 * Creates and registers new backlight device. Returns either an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * ERR_PTR() or a pointer to the newly allocated device.
316 */
Yu Luming519ab5f2006-12-19 12:56:15 -0800317struct backlight_device *backlight_device_register(const char *name,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500318 struct device *parent, void *devdata, const struct backlight_ops *ops,
319 const struct backlight_properties *props)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct backlight_device *new_bd;
Richard Purdie655bfd72007-07-09 12:17:24 +0100322 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Richard Purdie655bfd72007-07-09 12:17:24 +0100324 pr_debug("backlight_device_register: name=%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Richard Purdie599a52d2007-02-10 23:07:48 +0000326 new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000327 if (!new_bd)
Jean Delvare10ad1b72006-03-09 17:33:36 -0800328 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Richard Purdie28ee0862007-02-08 22:25:09 +0000330 mutex_init(&new_bd->update_lock);
Richard Purdie599a52d2007-02-10 23:07:48 +0000331 mutex_init(&new_bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Richard Purdie655bfd72007-07-09 12:17:24 +0100333 new_bd->dev.class = backlight_class;
334 new_bd->dev.parent = parent;
335 new_bd->dev.release = bl_device_release;
Kees Cook02aa2a32013-07-03 15:04:56 -0700336 dev_set_name(&new_bd->dev, "%s", name);
Richard Purdie655bfd72007-07-09 12:17:24 +0100337 dev_set_drvdata(&new_bd->dev, devdata);
338
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500339 /* Set default properties */
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700340 if (props) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500341 memcpy(&new_bd->props, props,
342 sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700343 if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
344 WARN(1, "%s: invalid backlight type", name);
345 new_bd->props.type = BACKLIGHT_RAW;
346 }
347 } else {
348 new_bd->props.type = BACKLIGHT_RAW;
349 }
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500350
Richard Purdie655bfd72007-07-09 12:17:24 +0100351 rc = device_register(&new_bd->dev);
Dmitry Torokhov90968e82007-02-08 00:12:28 +0000352 if (rc) {
Levente Kurusa35762a42014-02-07 09:43:21 +0100353 put_device(&new_bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return ERR_PTR(rc);
355 }
356
James Simmons3d5eead2006-12-08 02:40:47 -0800357 rc = backlight_register_fb(new_bd);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000358 if (rc) {
Richard Purdie655bfd72007-07-09 12:17:24 +0100359 device_unregister(&new_bd->dev);
Dmitry Torokhov2fd5a152007-02-07 22:25:50 +0000360 return ERR_PTR(rc);
361 }
362
Richard Purdie655bfd72007-07-09 12:17:24 +0100363 new_bd->ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Richard Purdie321709c2007-02-10 15:04:08 +0000365#ifdef CONFIG_PMAC_BACKLIGHT
366 mutex_lock(&pmac_backlight_mutex);
367 if (!pmac_backlight)
368 pmac_backlight = new_bd;
369 mutex_unlock(&pmac_backlight_mutex);
370#endif
371
Aaron Lu5915a3d2013-10-11 21:27:43 +0800372 mutex_lock(&backlight_dev_list_mutex);
373 list_add(&new_bd->entry, &backlight_dev_list);
374 mutex_unlock(&backlight_dev_list_mutex);
375
Hans de Goede3cc69192014-05-21 15:39:54 +0200376 blocking_notifier_call_chain(&backlight_notifier,
377 BACKLIGHT_REGISTERED, new_bd);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return new_bd;
380}
381EXPORT_SYMBOL(backlight_device_register);
382
Aaron Lu5915a3d2013-10-11 21:27:43 +0800383bool backlight_device_registered(enum backlight_type type)
384{
385 bool found = false;
386 struct backlight_device *bd;
387
388 mutex_lock(&backlight_dev_list_mutex);
389 list_for_each_entry(bd, &backlight_dev_list, entry) {
390 if (bd->props.type == type) {
391 found = true;
392 break;
393 }
394 }
395 mutex_unlock(&backlight_dev_list_mutex);
396
397 return found;
398}
399EXPORT_SYMBOL(backlight_device_registered);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/**
402 * backlight_device_unregister - unregisters a backlight device object.
403 * @bd: the backlight device object to be unregistered and freed.
404 *
405 * Unregisters a previously registered via backlight_device_register object.
406 */
407void backlight_device_unregister(struct backlight_device *bd)
408{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!bd)
410 return;
411
Aaron Lu5915a3d2013-10-11 21:27:43 +0800412 mutex_lock(&backlight_dev_list_mutex);
413 list_del(&bd->entry);
414 mutex_unlock(&backlight_dev_list_mutex);
415
Richard Purdie321709c2007-02-10 15:04:08 +0000416#ifdef CONFIG_PMAC_BACKLIGHT
417 mutex_lock(&pmac_backlight_mutex);
418 if (pmac_backlight == bd)
419 pmac_backlight = NULL;
420 mutex_unlock(&pmac_backlight_mutex);
421#endif
Hans de Goede3cc69192014-05-21 15:39:54 +0200422
423 blocking_notifier_call_chain(&backlight_notifier,
424 BACKLIGHT_UNREGISTERED, bd);
425
Richard Purdie599a52d2007-02-10 23:07:48 +0000426 mutex_lock(&bd->ops_lock);
427 bd->ops = NULL;
428 mutex_unlock(&bd->ops_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
James Simmons3d5eead2006-12-08 02:40:47 -0800430 backlight_unregister_fb(bd);
Richard Purdie655bfd72007-07-09 12:17:24 +0100431 device_unregister(&bd->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433EXPORT_SYMBOL(backlight_device_unregister);
434
Jingoo Han8318fde42013-07-03 15:05:13 -0700435static void devm_backlight_device_release(struct device *dev, void *res)
436{
437 struct backlight_device *backlight = *(struct backlight_device **)res;
438
439 backlight_device_unregister(backlight);
440}
441
442static int devm_backlight_device_match(struct device *dev, void *res,
443 void *data)
444{
445 struct backlight_device **r = res;
446
447 return *r == data;
448}
449
450/**
Hans de Goede3cc69192014-05-21 15:39:54 +0200451 * backlight_register_notifier - get notified of backlight (un)registration
452 * @nb: notifier block with the notifier to call on backlight (un)registration
453 *
454 * @return 0 on success, otherwise a negative error code
455 *
456 * Register a notifier to get notified when backlight devices get registered
457 * or unregistered.
458 */
459int backlight_register_notifier(struct notifier_block *nb)
460{
461 return blocking_notifier_chain_register(&backlight_notifier, nb);
462}
463EXPORT_SYMBOL(backlight_register_notifier);
464
465/**
466 * backlight_unregister_notifier - unregister a backlight notifier
467 * @nb: notifier block to unregister
468 *
469 * @return 0 on success, otherwise a negative error code
470 *
471 * Register a notifier to get notified when backlight devices get registered
472 * or unregistered.
473 */
474int backlight_unregister_notifier(struct notifier_block *nb)
475{
476 return blocking_notifier_chain_unregister(&backlight_notifier, nb);
477}
478EXPORT_SYMBOL(backlight_unregister_notifier);
479
480/**
Jingoo Han8318fde42013-07-03 15:05:13 -0700481 * devm_backlight_device_register - resource managed backlight_device_register()
482 * @dev: the device to register
483 * @name: the name of the device
484 * @parent: a pointer to the parent device
485 * @devdata: an optional pointer to be stored for private driver use
486 * @ops: the backlight operations structure
487 * @props: the backlight properties
488 *
489 * @return a struct backlight on success, or an ERR_PTR on error
490 *
491 * Managed backlight_device_register(). The backlight_device returned
492 * from this function are automatically freed on driver detach.
493 * See backlight_device_register() for more information.
494 */
495struct backlight_device *devm_backlight_device_register(struct device *dev,
496 const char *name, struct device *parent, void *devdata,
497 const struct backlight_ops *ops,
498 const struct backlight_properties *props)
499{
500 struct backlight_device **ptr, *backlight;
501
502 ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
503 GFP_KERNEL);
504 if (!ptr)
505 return ERR_PTR(-ENOMEM);
506
507 backlight = backlight_device_register(name, parent, devdata, ops,
508 props);
509 if (!IS_ERR(backlight)) {
510 *ptr = backlight;
511 devres_add(dev, ptr);
512 } else {
513 devres_free(ptr);
514 }
515
516 return backlight;
517}
518EXPORT_SYMBOL(devm_backlight_device_register);
519
520/**
521 * devm_backlight_device_unregister - resource managed backlight_device_unregister()
522 * @dev: the device to unregister
523 * @bd: the backlight device to unregister
524 *
525 * Deallocated a backlight allocated with devm_backlight_device_register().
526 * Normally this function will not need to be called and the resource management
527 * code will ensure that the resource is freed.
528 */
529void devm_backlight_device_unregister(struct device *dev,
530 struct backlight_device *bd)
531{
532 int rc;
533
534 rc = devres_release(dev, devm_backlight_device_release,
535 devm_backlight_device_match, bd);
536 WARN_ON(rc);
537}
538EXPORT_SYMBOL(devm_backlight_device_unregister);
539
Thierry Reding762a9362012-12-17 16:01:06 -0800540#ifdef CONFIG_OF
Greg Kroah-Hartman3213f632013-02-06 16:43:02 -0800541static int of_parent_match(struct device *dev, const void *data)
Thierry Reding762a9362012-12-17 16:01:06 -0800542{
543 return dev->parent && dev->parent->of_node == data;
544}
545
546/**
547 * of_find_backlight_by_node() - find backlight device by device-tree node
548 * @node: device-tree node of the backlight device
549 *
550 * Returns a pointer to the backlight device corresponding to the given DT
551 * node or NULL if no such backlight device exists or if the device hasn't
552 * been probed yet.
553 *
554 * This function obtains a reference on the backlight device and it is the
555 * caller's responsibility to drop the reference by calling put_device() on
556 * the backlight device's .dev field.
557 */
558struct backlight_device *of_find_backlight_by_node(struct device_node *node)
559{
560 struct device *dev;
561
562 dev = class_find_device(backlight_class, NULL, node, of_parent_match);
563
564 return dev ? to_backlight_device(dev) : NULL;
565}
566EXPORT_SYMBOL(of_find_backlight_by_node);
567#endif
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static void __exit backlight_class_exit(void)
570{
Richard Purdie655bfd72007-07-09 12:17:24 +0100571 class_destroy(backlight_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574static int __init backlight_class_init(void)
575{
Richard Purdie655bfd72007-07-09 12:17:24 +0100576 backlight_class = class_create(THIS_MODULE, "backlight");
577 if (IS_ERR(backlight_class)) {
Jingoo Han35f96162012-05-29 15:07:16 -0700578 pr_warn("Unable to create backlight class; errno = %ld\n",
579 PTR_ERR(backlight_class));
Richard Purdie655bfd72007-07-09 12:17:24 +0100580 return PTR_ERR(backlight_class);
581 }
582
Greg Kroah-Hartmanea1bb702013-07-24 15:05:30 -0700583 backlight_class->dev_groups = bl_device_groups;
Shuah Khan36017922013-07-03 15:05:16 -0700584 backlight_class->pm = &backlight_class_dev_pm_ops;
Aaron Lu5915a3d2013-10-11 21:27:43 +0800585 INIT_LIST_HEAD(&backlight_dev_list);
586 mutex_init(&backlight_dev_list_mutex);
Hans de Goede3cc69192014-05-21 15:39:54 +0200587 BLOCKING_INIT_NOTIFIER_HEAD(&backlight_notifier);
588
Richard Purdie655bfd72007-07-09 12:17:24 +0100589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/*
593 * if this is compiled into the kernel, we need to ensure that the
594 * class is registered before users of the class try to register lcd's
595 */
596postcore_initcall(backlight_class_init);
597module_exit(backlight_class_exit);
598
599MODULE_LICENSE("GPL");
600MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
601MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");