blob: d54c8e4d8954e6b3fa4c2a9be3ac50fde8ccfbb3 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Richard Purdiec72a1d62006-03-31 02:31:04 -08002/*
3 * LED Class Core
4 *
5 * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
Richard Purdief8a7c6f2007-07-08 23:19:31 +01006 * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
Richard Purdiec72a1d62006-03-31 02:31:04 -08007 */
8
Richard Purdie3dc7b822006-05-15 09:44:17 -07009#include <linux/ctype.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070010#include <linux/device.h>
11#include <linux/err.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080014#include <linux/leds.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070015#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/spinlock.h>
19#include <linux/timer.h>
David Lechnereb1ce742016-09-16 14:16:49 -050020#include <uapi/linux/uleds.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080021#include "leds.h"
22
23static struct class *leds_class;
24
Greg Kroah-Hartman5baa7502013-07-24 15:05:13 -070025static ssize_t brightness_show(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010026 struct device_attribute *attr, char *buf)
Richard Purdiec72a1d62006-03-31 02:31:04 -080027{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010028 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdiec72a1d62006-03-31 02:31:04 -080029
30 /* no lock needed for this */
Henrique de Moraes Holschuh29d76df2008-03-18 09:47:48 +000031 led_update_brightness(led_cdev);
Richard Purdiec72a1d62006-03-31 02:31:04 -080032
Sven Wegenerdd8e5a22008-10-13 10:41:39 +010033 return sprintf(buf, "%u\n", led_cdev->brightness);
Richard Purdiec72a1d62006-03-31 02:31:04 -080034}
35
Greg Kroah-Hartman5baa7502013-07-24 15:05:13 -070036static ssize_t brightness_store(struct device *dev,
Richard Purdief8a7c6f2007-07-08 23:19:31 +010037 struct device_attribute *attr, const char *buf, size_t size)
Richard Purdiec72a1d62006-03-31 02:31:04 -080038{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010039 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Shuah Khan872b86b2012-05-29 15:07:26 -070040 unsigned long state;
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -070041 ssize_t ret;
42
43 mutex_lock(&led_cdev->led_access);
44
45 if (led_sysfs_is_disabled(led_cdev)) {
46 ret = -EBUSY;
47 goto unlock;
48 }
Richard Purdiec72a1d62006-03-31 02:31:04 -080049
Shuah Khan872b86b2012-05-29 15:07:26 -070050 ret = kstrtoul(buf, 10, &state);
51 if (ret)
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -070052 goto unlock;
Richard Purdie3dc7b822006-05-15 09:44:17 -070053
Shuah Khan872b86b2012-05-29 15:07:26 -070054 if (state == LED_OFF)
55 led_trigger_remove(led_cdev);
Jacek Anaszewski4d71a4a2014-11-14 02:50:18 -080056 led_set_brightness(led_cdev, state);
Pavel Machek0db37912019-04-29 17:22:59 +020057 flush_work(&led_cdev->set_brightness_work);
Németh Márton0013b232008-03-09 20:54:37 +000058
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -070059 ret = size;
60unlock:
61 mutex_unlock(&led_cdev->led_access);
62 return ret;
Richard Purdiec72a1d62006-03-31 02:31:04 -080063}
Greg Kroah-Hartman5baa7502013-07-24 15:05:13 -070064static DEVICE_ATTR_RW(brightness);
Richard Purdiec72a1d62006-03-31 02:31:04 -080065
Jacek Anaszewski384196122014-08-07 05:10:24 -070066static ssize_t max_brightness_show(struct device *dev,
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +000067 struct device_attribute *attr, char *buf)
68{
69 struct led_classdev *led_cdev = dev_get_drvdata(dev);
70
71 return sprintf(buf, "%u\n", led_cdev->max_brightness);
72}
Jacek Anaszewski384196122014-08-07 05:10:24 -070073static DEVICE_ATTR_RO(max_brightness);
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +000074
Richard Purdiec3bc9952006-03-31 02:31:05 -080075#ifdef CONFIG_LEDS_TRIGGERS
Greg Kroah-Hartman5baa7502013-07-24 15:05:13 -070076static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
77static struct attribute *led_trigger_attrs[] = {
78 &dev_attr_trigger.attr,
79 NULL,
80};
81static const struct attribute_group led_trigger_group = {
82 .attrs = led_trigger_attrs,
83};
Richard Purdiec3bc9952006-03-31 02:31:05 -080084#endif
Greg Kroah-Hartman5baa7502013-07-24 15:05:13 -070085
86static struct attribute *led_class_attrs[] = {
87 &dev_attr_brightness.attr,
88 &dev_attr_max_brightness.attr,
89 NULL,
90};
91
92static const struct attribute_group led_group = {
93 .attrs = led_class_attrs,
94};
95
96static const struct attribute_group *led_groups[] = {
97 &led_group,
98#ifdef CONFIG_LEDS_TRIGGERS
99 &led_trigger_group,
100#endif
101 NULL,
Florian Fainelli14b5d6d2010-03-10 18:32:18 +0100102};
Richard Purdiec72a1d62006-03-31 02:31:04 -0800103
Hans de Goede0cb8eb32017-01-29 14:42:52 +0100104#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
105static ssize_t brightness_hw_changed_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
107{
108 struct led_classdev *led_cdev = dev_get_drvdata(dev);
109
110 if (led_cdev->brightness_hw_changed == -1)
111 return -ENODATA;
112
113 return sprintf(buf, "%u\n", led_cdev->brightness_hw_changed);
114}
115
116static DEVICE_ATTR_RO(brightness_hw_changed);
117
118static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
119{
120 struct device *dev = led_cdev->dev;
121 int ret;
122
123 ret = device_create_file(dev, &dev_attr_brightness_hw_changed);
124 if (ret) {
125 dev_err(dev, "Error creating brightness_hw_changed\n");
126 return ret;
127 }
128
129 led_cdev->brightness_hw_changed_kn =
130 sysfs_get_dirent(dev->kobj.sd, "brightness_hw_changed");
131 if (!led_cdev->brightness_hw_changed_kn) {
132 dev_err(dev, "Error getting brightness_hw_changed kn\n");
133 device_remove_file(dev, &dev_attr_brightness_hw_changed);
134 return -ENXIO;
135 }
136
137 return 0;
138}
139
140static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
141{
142 sysfs_put(led_cdev->brightness_hw_changed_kn);
143 device_remove_file(led_cdev->dev, &dev_attr_brightness_hw_changed);
144}
145
146void led_classdev_notify_brightness_hw_changed(struct led_classdev *led_cdev,
147 enum led_brightness brightness)
148{
149 if (WARN_ON(!led_cdev->brightness_hw_changed_kn))
150 return;
151
152 led_cdev->brightness_hw_changed = brightness;
153 sysfs_notify_dirent(led_cdev->brightness_hw_changed_kn);
154}
155EXPORT_SYMBOL_GPL(led_classdev_notify_brightness_hw_changed);
156#else
157static int led_add_brightness_hw_changed(struct led_classdev *led_cdev)
158{
159 return 0;
160}
161static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev)
162{
163}
164#endif
165
Richard Purdiec72a1d62006-03-31 02:31:04 -0800166/**
167 * led_classdev_suspend - suspend an led_classdev.
168 * @led_cdev: the led_classdev to suspend.
169 */
170void led_classdev_suspend(struct led_classdev *led_cdev)
171{
172 led_cdev->flags |= LED_SUSPENDED;
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200173 led_set_brightness_nopm(led_cdev, 0);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800174}
175EXPORT_SYMBOL_GPL(led_classdev_suspend);
176
177/**
178 * led_classdev_resume - resume an led_classdev.
179 * @led_cdev: the led_classdev to resume.
180 */
181void led_classdev_resume(struct led_classdev *led_cdev)
182{
Jacek Anaszewski81fe8e52015-10-07 11:10:41 +0200183 led_set_brightness_nopm(led_cdev, led_cdev->brightness);
Jacek Anaszewski7aea8382015-01-09 07:22:51 -0800184
185 if (led_cdev->flash_resume)
186 led_cdev->flash_resume(led_cdev);
187
Richard Purdiec72a1d62006-03-31 02:31:04 -0800188 led_cdev->flags &= ~LED_SUSPENDED;
189}
190EXPORT_SYMBOL_GPL(led_classdev_resume);
191
Grygorii Strashko084609b2015-04-24 14:57:10 +0300192#ifdef CONFIG_PM_SLEEP
Shuah Khan73e1ab42013-06-20 12:08:44 -0700193static int led_suspend(struct device *dev)
Richard Purdie859cb7f2009-01-08 17:55:03 +0000194{
195 struct led_classdev *led_cdev = dev_get_drvdata(dev);
196
197 if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
198 led_classdev_suspend(led_cdev);
199
200 return 0;
201}
202
203static int led_resume(struct device *dev)
204{
205 struct led_classdev *led_cdev = dev_get_drvdata(dev);
206
207 if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
208 led_classdev_resume(led_cdev);
209
210 return 0;
211}
Grygorii Strashko084609b2015-04-24 14:57:10 +0300212#endif
Richard Purdie859cb7f2009-01-08 17:55:03 +0000213
Grygorii Strashko084609b2015-04-24 14:57:10 +0300214static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
Shuah Khan73e1ab42013-06-20 12:08:44 -0700215
Ricardo Ribalda Delgadoa96aa642015-03-30 10:45:59 -0700216static int led_classdev_next_name(const char *init_name, char *name,
217 size_t len)
218{
219 unsigned int i = 0;
220 int ret = 0;
Ricardo Ribalda Delgadoe5b5a612015-07-31 13:36:21 +0200221 struct device *dev;
Ricardo Ribalda Delgadoa96aa642015-03-30 10:45:59 -0700222
223 strlcpy(name, init_name, len);
224
Ricardo Ribalda Delgadoe5b5a612015-07-31 13:36:21 +0200225 while ((ret < len) &&
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +0100226 (dev = class_find_device_by_name(leds_class, name))) {
Ricardo Ribalda Delgadoe5b5a612015-07-31 13:36:21 +0200227 put_device(dev);
Ricardo Ribalda Delgadoa96aa642015-03-30 10:45:59 -0700228 ret = snprintf(name, len, "%s_%u", init_name, ++i);
Ricardo Ribalda Delgadoe5b5a612015-07-31 13:36:21 +0200229 }
Ricardo Ribalda Delgadoa96aa642015-03-30 10:45:59 -0700230
231 if (ret >= len)
232 return -ENOMEM;
233
234 return i;
235}
236
Richard Purdiec72a1d62006-03-31 02:31:04 -0800237/**
Rafał Miłecki442c6092017-03-06 06:19:44 +0100238 * of_led_classdev_register - register a new object of led_classdev class.
239 *
240 * @parent: parent of LED device
Richard Purdiec72a1d62006-03-31 02:31:04 -0800241 * @led_cdev: the led_classdev structure for this device.
Rafał Miłecki442c6092017-03-06 06:19:44 +0100242 * @np: DT node describing this LED
Richard Purdiec72a1d62006-03-31 02:31:04 -0800243 */
Rafał Miłecki442c6092017-03-06 06:19:44 +0100244int of_led_classdev_register(struct device *parent, struct device_node *np,
245 struct led_classdev *led_cdev)
Richard Purdiec72a1d62006-03-31 02:31:04 -0800246{
David Lechnereb1ce742016-09-16 14:16:49 -0500247 char name[LED_MAX_NAME_SIZE];
Ricardo Ribalda Delgadoa96aa642015-03-30 10:45:59 -0700248 int ret;
249
250 ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
251 if (ret < 0)
252 return ret;
253
Luis Henriques6d710212018-05-23 23:22:21 +0100254 mutex_init(&led_cdev->led_access);
255 mutex_lock(&led_cdev->led_access);
Johan Hovoldd0d480c2014-06-25 10:08:44 -0700256 led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
Sakari Ailusccdc4502015-03-26 16:42:06 -0700257 led_cdev, led_cdev->groups, "%s", name);
Luis Henriques6d710212018-05-23 23:22:21 +0100258 if (IS_ERR(led_cdev->dev)) {
259 mutex_unlock(&led_cdev->led_access);
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100260 return PTR_ERR(led_cdev->dev);
Luis Henriques6d710212018-05-23 23:22:21 +0100261 }
Rafał Miłecki442c6092017-03-06 06:19:44 +0100262 led_cdev->dev->of_node = np;
Richard Purdiec72a1d62006-03-31 02:31:04 -0800263
Ricardo Ribalda Delgadoa96aa642015-03-30 10:45:59 -0700264 if (ret)
Sakari Ailus6f06c7f2015-03-26 16:42:05 -0700265 dev_warn(parent, "Led %s renamed to %s due to name collision",
Ricardo Ribalda Delgadoa96aa642015-03-30 10:45:59 -0700266 led_cdev->name, dev_name(led_cdev->dev));
267
Hans de Goede0cb8eb32017-01-29 14:42:52 +0100268 if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) {
269 ret = led_add_brightness_hw_changed(led_cdev);
270 if (ret) {
271 device_unregister(led_cdev->dev);
Luis Henriques6d710212018-05-23 23:22:21 +0100272 mutex_unlock(&led_cdev->led_access);
Hans de Goede0cb8eb32017-01-29 14:42:52 +0100273 return ret;
274 }
275 }
276
Hans de Goedea9c6ce52016-11-08 14:38:46 +0100277 led_cdev->work_flags = 0;
Richard Purdie270c3952008-10-20 23:16:17 +0100278#ifdef CONFIG_LEDS_TRIGGERS
279 init_rwsem(&led_cdev->trigger_lock);
280#endif
Hans de Goede0cb8eb32017-01-29 14:42:52 +0100281#ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED
282 led_cdev->brightness_hw_changed = -1;
283#endif
Richard Purdiec72a1d62006-03-31 02:31:04 -0800284 /* add to the list of leds */
Richard Purdie72f8da32007-12-31 23:09:44 +0000285 down_write(&leds_list_lock);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800286 list_add_tail(&led_cdev->node, &leds_list);
Richard Purdie72f8da32007-12-31 23:09:44 +0000287 up_write(&leds_list_lock);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800288
Guennadi Liakhovetski1bd465e2009-01-10 18:54:39 +0000289 if (!led_cdev->max_brightness)
290 led_cdev->max_brightness = LED_FULL;
291
Henrique de Moraes Holschuh29d76df2008-03-18 09:47:48 +0000292 led_update_brightness(led_cdev);
293
Jacek Anaszewski757b06a2015-09-28 14:38:04 +0200294 led_init_core(led_cdev);
Johannes Berg5ada28b2010-11-11 14:05:21 -0800295
Richard Purdiec3bc9952006-03-31 02:31:05 -0800296#ifdef CONFIG_LEDS_TRIGGERS
Jeff Garzik12fda162006-10-17 00:10:20 -0700297 led_trigger_set_default(led_cdev);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800298#endif
299
Luis Henriques6d710212018-05-23 23:22:21 +0100300 mutex_unlock(&led_cdev->led_access);
301
Sachin Kamatd23e7b82012-11-25 10:57:35 +0530302 dev_dbg(parent, "Registered led device: %s\n",
Richard Purdief8a7c6f2007-07-08 23:19:31 +0100303 led_cdev->name);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800304
305 return 0;
306}
Rafał Miłecki442c6092017-03-06 06:19:44 +0100307EXPORT_SYMBOL_GPL(of_led_classdev_register);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800308
309/**
Qinghuang Feng0266a452008-10-20 23:04:36 +0100310 * led_classdev_unregister - unregisters a object of led_properties class.
Henrik Kretzschmar70d63cc2006-10-03 23:31:30 +0200311 * @led_cdev: the led device to unregister
Richard Purdiec72a1d62006-03-31 02:31:04 -0800312 *
313 * Unregisters a previously registered via led_classdev_register object.
314 */
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100315void led_classdev_unregister(struct led_classdev *led_cdev)
Richard Purdiec72a1d62006-03-31 02:31:04 -0800316{
Richard Purdiec3bc9952006-03-31 02:31:05 -0800317#ifdef CONFIG_LEDS_TRIGGERS
Richard Purdiedc472062007-11-10 13:29:04 +0000318 down_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800319 if (led_cdev->trigger)
320 led_trigger_set(led_cdev, NULL);
Richard Purdiedc472062007-11-10 13:29:04 +0000321 up_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800322#endif
Richard Purdiec72a1d62006-03-31 02:31:04 -0800323
Heiner Kallweitd84d80f2016-01-22 21:43:48 +0100324 led_cdev->flags |= LED_UNREGISTERING;
325
Johannes Berg5ada28b2010-11-11 14:05:21 -0800326 /* Stop blinking */
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800327 led_stop_software_blink(led_cdev);
Milo Kimd1aa5772015-11-20 17:03:00 +0900328
Shuah Khan19cd67e2012-06-14 04:34:30 +0800329 led_set_brightness(led_cdev, LED_OFF);
Johannes Berg5ada28b2010-11-11 14:05:21 -0800330
Milo Kimd1aa5772015-11-20 17:03:00 +0900331 flush_work(&led_cdev->set_brightness_work);
332
Hans de Goede0cb8eb32017-01-29 14:42:52 +0100333 if (led_cdev->flags & LED_BRIGHT_HW_CHANGED)
334 led_remove_brightness_hw_changed(led_cdev);
335
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100336 device_unregister(led_cdev->dev);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800337
Richard Purdie72f8da32007-12-31 23:09:44 +0000338 down_write(&leds_list_lock);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800339 list_del(&led_cdev->node);
Richard Purdie72f8da32007-12-31 23:09:44 +0000340 up_write(&leds_list_lock);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700341
342 mutex_destroy(&led_cdev->led_access);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800343}
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100344EXPORT_SYMBOL_GPL(led_classdev_unregister);
Richard Purdiec72a1d62006-03-31 02:31:04 -0800345
Bjorn Anderssonca1bb4e2015-02-23 16:11:41 -0800346static void devm_led_classdev_release(struct device *dev, void *res)
347{
348 led_classdev_unregister(*(struct led_classdev **)res);
349}
350
351/**
Rafał Miłecki442c6092017-03-06 06:19:44 +0100352 * devm_of_led_classdev_register - resource managed led_classdev_register()
353 *
354 * @parent: parent of LED device
Bjorn Anderssonca1bb4e2015-02-23 16:11:41 -0800355 * @led_cdev: the led_classdev structure for this device.
356 */
Rafał Miłecki442c6092017-03-06 06:19:44 +0100357int devm_of_led_classdev_register(struct device *parent,
358 struct device_node *np,
359 struct led_classdev *led_cdev)
Bjorn Anderssonca1bb4e2015-02-23 16:11:41 -0800360{
361 struct led_classdev **dr;
362 int rc;
363
364 dr = devres_alloc(devm_led_classdev_release, sizeof(*dr), GFP_KERNEL);
365 if (!dr)
366 return -ENOMEM;
367
Rafał Miłecki442c6092017-03-06 06:19:44 +0100368 rc = of_led_classdev_register(parent, np, led_cdev);
Bjorn Anderssonca1bb4e2015-02-23 16:11:41 -0800369 if (rc) {
370 devres_free(dr);
371 return rc;
372 }
373
374 *dr = led_cdev;
375 devres_add(parent, dr);
376
377 return 0;
378}
Rafał Miłecki442c6092017-03-06 06:19:44 +0100379EXPORT_SYMBOL_GPL(devm_of_led_classdev_register);
Bjorn Anderssonca1bb4e2015-02-23 16:11:41 -0800380
381static int devm_led_classdev_match(struct device *dev, void *res, void *data)
382{
383 struct led_cdev **p = res;
384
385 if (WARN_ON(!p || !*p))
386 return 0;
387
388 return *p == data;
389}
390
391/**
392 * devm_led_classdev_unregister() - resource managed led_classdev_unregister()
393 * @parent: The device to unregister.
394 * @led_cdev: the led_classdev structure for this device.
395 */
396void devm_led_classdev_unregister(struct device *dev,
397 struct led_classdev *led_cdev)
398{
399 WARN_ON(devres_release(dev,
400 devm_led_classdev_release,
401 devm_led_classdev_match, led_cdev));
402}
403EXPORT_SYMBOL_GPL(devm_led_classdev_unregister);
404
Richard Purdiec72a1d62006-03-31 02:31:04 -0800405static int __init leds_init(void)
406{
407 leds_class = class_create(THIS_MODULE, "leds");
408 if (IS_ERR(leds_class))
409 return PTR_ERR(leds_class);
Shuah Khan73e1ab42013-06-20 12:08:44 -0700410 leds_class->pm = &leds_class_dev_pm_ops;
Greg Kroah-Hartman5baa7502013-07-24 15:05:13 -0700411 leds_class->dev_groups = led_groups;
Richard Purdiec72a1d62006-03-31 02:31:04 -0800412 return 0;
413}
414
415static void __exit leds_exit(void)
416{
417 class_destroy(leds_class);
418}
419
420subsys_initcall(leds_init);
421module_exit(leds_exit);
422
423MODULE_AUTHOR("John Lenz, Richard Purdie");
424MODULE_LICENSE("GPL");
425MODULE_DESCRIPTION("LED Class Interface");