Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
c72a1d60 RP |
2 | /* |
3 | * LED Class Core | |
4 | * | |
5 | * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu> | |
f8a7c6fe | 6 | * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com> |
c72a1d60 RP |
7 | */ |
8 | ||
04713306 JA |
9 | #include <linux/ctype.h> |
10 | #include <linux/device.h> | |
11 | #include <linux/err.h> | |
c72a1d60 | 12 | #include <linux/init.h> |
04713306 JA |
13 | #include <linux/kernel.h> |
14 | #include <linux/leds.h> | |
c72a1d60 | 15 | #include <linux/list.h> |
04713306 | 16 | #include <linux/module.h> |
b2b998c0 | 17 | #include <linux/of.h> |
04713306 | 18 | #include <linux/slab.h> |
c72a1d60 | 19 | #include <linux/spinlock.h> |
9067359f | 20 | #include <linux/timer.h> |
eb1ce746 | 21 | #include <uapi/linux/uleds.h> |
c72a1d60 RP |
22 | #include "leds.h" |
23 | ||
24 | static struct class *leds_class; | |
25 | ||
5baa7503 | 26 | static ssize_t brightness_show(struct device *dev, |
f8a7c6fe | 27 | struct device_attribute *attr, char *buf) |
c72a1d60 | 28 | { |
f8a7c6fe | 29 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
c72a1d60 RP |
30 | |
31 | /* no lock needed for this */ | |
29d76dfa | 32 | led_update_brightness(led_cdev); |
c72a1d60 | 33 | |
dd8e5a20 | 34 | return sprintf(buf, "%u\n", led_cdev->brightness); |
c72a1d60 RP |
35 | } |
36 | ||
5baa7503 | 37 | static ssize_t brightness_store(struct device *dev, |
f8a7c6fe | 38 | struct device_attribute *attr, const char *buf, size_t size) |
c72a1d60 | 39 | { |
f8a7c6fe | 40 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
872b86be | 41 | unsigned long state; |
acd899e4 JA |
42 | ssize_t ret; |
43 | ||
44 | mutex_lock(&led_cdev->led_access); | |
45 | ||
46 | if (led_sysfs_is_disabled(led_cdev)) { | |
47 | ret = -EBUSY; | |
48 | goto unlock; | |
49 | } | |
c72a1d60 | 50 | |
872b86be SK |
51 | ret = kstrtoul(buf, 10, &state); |
52 | if (ret) | |
acd899e4 | 53 | goto unlock; |
3dc7b82e | 54 | |
872b86be SK |
55 | if (state == LED_OFF) |
56 | led_trigger_remove(led_cdev); | |
4d71a4a1 | 57 | led_set_brightness(led_cdev, state); |
0db37915 | 58 | flush_work(&led_cdev->set_brightness_work); |
0013b23d | 59 | |
acd899e4 JA |
60 | ret = size; |
61 | unlock: | |
62 | mutex_unlock(&led_cdev->led_access); | |
63 | return ret; | |
c72a1d60 | 64 | } |
5baa7503 | 65 | static DEVICE_ATTR_RW(brightness); |
c72a1d60 | 66 | |
38419612 | 67 | static ssize_t max_brightness_show(struct device *dev, |
1bd465e6 GL |
68 | struct device_attribute *attr, char *buf) |
69 | { | |
70 | struct led_classdev *led_cdev = dev_get_drvdata(dev); | |
71 | ||
72 | return sprintf(buf, "%u\n", led_cdev->max_brightness); | |
73 | } | |
38419612 | 74 | static DEVICE_ATTR_RO(max_brightness); |
1bd465e6 | 75 | |
c3bc9956 | 76 | #ifdef CONFIG_LEDS_TRIGGERS |
5baa7503 GKH |
77 | static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store); |
78 | static struct attribute *led_trigger_attrs[] = { | |
79 | &dev_attr_trigger.attr, | |
80 | NULL, | |
81 | }; | |
82 | static const struct attribute_group led_trigger_group = { | |
83 | .attrs = led_trigger_attrs, | |
84 | }; | |
85 | #endif | |
86 | ||
87 | static struct attribute *led_class_attrs[] = { | |
88 | &dev_attr_brightness.attr, | |
89 | &dev_attr_max_brightness.attr, | |
90 | NULL, | |
91 | }; | |
92 | ||
93 | static const struct attribute_group led_group = { | |
94 | .attrs = led_class_attrs, | |
95 | }; | |
96 | ||
97 | static const struct attribute_group *led_groups[] = { | |
98 | &led_group, | |
99 | #ifdef CONFIG_LEDS_TRIGGERS | |
100 | &led_trigger_group, | |
c3bc9956 | 101 | #endif |
5baa7503 | 102 | NULL, |
14b5d6dd | 103 | }; |
c72a1d60 | 104 | |
0cb8eb30 HG |
105 | #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED |
106 | static ssize_t brightness_hw_changed_show(struct device *dev, | |
107 | struct device_attribute *attr, char *buf) | |
108 | { | |
109 | struct led_classdev *led_cdev = dev_get_drvdata(dev); | |
110 | ||
111 | if (led_cdev->brightness_hw_changed == -1) | |
112 | return -ENODATA; | |
113 | ||
114 | return sprintf(buf, "%u\n", led_cdev->brightness_hw_changed); | |
115 | } | |
116 | ||
117 | static DEVICE_ATTR_RO(brightness_hw_changed); | |
118 | ||
119 | static int led_add_brightness_hw_changed(struct led_classdev *led_cdev) | |
120 | { | |
121 | struct device *dev = led_cdev->dev; | |
122 | int ret; | |
123 | ||
124 | ret = device_create_file(dev, &dev_attr_brightness_hw_changed); | |
125 | if (ret) { | |
126 | dev_err(dev, "Error creating brightness_hw_changed\n"); | |
127 | return ret; | |
128 | } | |
129 | ||
130 | led_cdev->brightness_hw_changed_kn = | |
131 | sysfs_get_dirent(dev->kobj.sd, "brightness_hw_changed"); | |
132 | if (!led_cdev->brightness_hw_changed_kn) { | |
133 | dev_err(dev, "Error getting brightness_hw_changed kn\n"); | |
134 | device_remove_file(dev, &dev_attr_brightness_hw_changed); | |
135 | return -ENXIO; | |
136 | } | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev) | |
142 | { | |
143 | sysfs_put(led_cdev->brightness_hw_changed_kn); | |
144 | device_remove_file(led_cdev->dev, &dev_attr_brightness_hw_changed); | |
145 | } | |
146 | ||
147 | void led_classdev_notify_brightness_hw_changed(struct led_classdev *led_cdev, | |
148 | enum led_brightness brightness) | |
149 | { | |
150 | if (WARN_ON(!led_cdev->brightness_hw_changed_kn)) | |
151 | return; | |
152 | ||
153 | led_cdev->brightness_hw_changed = brightness; | |
154 | sysfs_notify_dirent(led_cdev->brightness_hw_changed_kn); | |
155 | } | |
156 | EXPORT_SYMBOL_GPL(led_classdev_notify_brightness_hw_changed); | |
157 | #else | |
158 | static int led_add_brightness_hw_changed(struct led_classdev *led_cdev) | |
159 | { | |
160 | return 0; | |
161 | } | |
162 | static void led_remove_brightness_hw_changed(struct led_classdev *led_cdev) | |
163 | { | |
164 | } | |
165 | #endif | |
166 | ||
c72a1d60 RP |
167 | /** |
168 | * led_classdev_suspend - suspend an led_classdev. | |
169 | * @led_cdev: the led_classdev to suspend. | |
170 | */ | |
171 | void led_classdev_suspend(struct led_classdev *led_cdev) | |
172 | { | |
173 | led_cdev->flags |= LED_SUSPENDED; | |
81fe8e5b | 174 | led_set_brightness_nopm(led_cdev, 0); |
c72a1d60 RP |
175 | } |
176 | EXPORT_SYMBOL_GPL(led_classdev_suspend); | |
177 | ||
178 | /** | |
179 | * led_classdev_resume - resume an led_classdev. | |
180 | * @led_cdev: the led_classdev to resume. | |
181 | */ | |
182 | void led_classdev_resume(struct led_classdev *led_cdev) | |
183 | { | |
81fe8e5b | 184 | led_set_brightness_nopm(led_cdev, led_cdev->brightness); |
7aea8389 JA |
185 | |
186 | if (led_cdev->flash_resume) | |
187 | led_cdev->flash_resume(led_cdev); | |
188 | ||
c72a1d60 RP |
189 | led_cdev->flags &= ~LED_SUSPENDED; |
190 | } | |
191 | EXPORT_SYMBOL_GPL(led_classdev_resume); | |
192 | ||
084609bf | 193 | #ifdef CONFIG_PM_SLEEP |
73e1ab41 | 194 | static int led_suspend(struct device *dev) |
859cb7f2 RP |
195 | { |
196 | struct led_classdev *led_cdev = dev_get_drvdata(dev); | |
197 | ||
198 | if (led_cdev->flags & LED_CORE_SUSPENDRESUME) | |
199 | led_classdev_suspend(led_cdev); | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | static int led_resume(struct device *dev) | |
205 | { | |
206 | struct led_classdev *led_cdev = dev_get_drvdata(dev); | |
207 | ||
208 | if (led_cdev->flags & LED_CORE_SUSPENDRESUME) | |
209 | led_classdev_resume(led_cdev); | |
210 | ||
211 | return 0; | |
212 | } | |
084609bf | 213 | #endif |
859cb7f2 | 214 | |
084609bf | 215 | static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume); |
73e1ab41 | 216 | |
a96aa64c RRD |
217 | static int match_name(struct device *dev, const void *data) |
218 | { | |
219 | if (!dev_name(dev)) | |
220 | return 0; | |
221 | return !strcmp(dev_name(dev), (char *)data); | |
222 | } | |
223 | ||
224 | static int led_classdev_next_name(const char *init_name, char *name, | |
225 | size_t len) | |
226 | { | |
227 | unsigned int i = 0; | |
228 | int ret = 0; | |
e5b5a61f | 229 | struct device *dev; |
a96aa64c RRD |
230 | |
231 | strlcpy(name, init_name, len); | |
232 | ||
e5b5a61f RRD |
233 | while ((ret < len) && |
234 | (dev = class_find_device(leds_class, NULL, name, match_name))) { | |
235 | put_device(dev); | |
a96aa64c | 236 | ret = snprintf(name, len, "%s_%u", init_name, ++i); |
e5b5a61f | 237 | } |
a96aa64c RRD |
238 | |
239 | if (ret >= len) | |
240 | return -ENOMEM; | |
241 | ||
242 | return i; | |
243 | } | |
244 | ||
c72a1d60 | 245 | /** |
b2b998c0 JA |
246 | * led_classdev_register_ext - register a new object of led_classdev class |
247 | * with init data. | |
442c6098 RM |
248 | * |
249 | * @parent: parent of LED device | |
c72a1d60 | 250 | * @led_cdev: the led_classdev structure for this device. |
b2b998c0 | 251 | * @init_data: LED class device initialization data |
c72a1d60 | 252 | */ |
b2b998c0 JA |
253 | int led_classdev_register_ext(struct device *parent, |
254 | struct led_classdev *led_cdev, | |
255 | struct led_init_data *init_data) | |
c72a1d60 | 256 | { |
eb1ce746 | 257 | char name[LED_MAX_NAME_SIZE]; |
a96aa64c RRD |
258 | int ret; |
259 | ||
260 | ret = led_classdev_next_name(led_cdev->name, name, sizeof(name)); | |
261 | if (ret < 0) | |
262 | return ret; | |
263 | ||
6d71021a LH |
264 | mutex_init(&led_cdev->led_access); |
265 | mutex_lock(&led_cdev->led_access); | |
d0d480cc | 266 | led_cdev->dev = device_create_with_groups(leds_class, parent, 0, |
ccdc4507 | 267 | led_cdev, led_cdev->groups, "%s", name); |
6d71021a LH |
268 | if (IS_ERR(led_cdev->dev)) { |
269 | mutex_unlock(&led_cdev->led_access); | |
f8a7c6fe | 270 | return PTR_ERR(led_cdev->dev); |
6d71021a | 271 | } |
b2b998c0 JA |
272 | if (init_data && init_data->fwnode) |
273 | led_cdev->dev->of_node = to_of_node(init_data->fwnode); | |
c72a1d60 | 274 | |
a96aa64c | 275 | if (ret) |
6f06c7f8 | 276 | dev_warn(parent, "Led %s renamed to %s due to name collision", |
a96aa64c RRD |
277 | led_cdev->name, dev_name(led_cdev->dev)); |
278 | ||
0cb8eb30 HG |
279 | if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) { |
280 | ret = led_add_brightness_hw_changed(led_cdev); | |
281 | if (ret) { | |
282 | device_unregister(led_cdev->dev); | |
6d71021a | 283 | mutex_unlock(&led_cdev->led_access); |
0cb8eb30 HG |
284 | return ret; |
285 | } | |
286 | } | |
287 | ||
a9c6ce57 | 288 | led_cdev->work_flags = 0; |
270c3957 RP |
289 | #ifdef CONFIG_LEDS_TRIGGERS |
290 | init_rwsem(&led_cdev->trigger_lock); | |
0cb8eb30 HG |
291 | #endif |
292 | #ifdef CONFIG_LEDS_BRIGHTNESS_HW_CHANGED | |
293 | led_cdev->brightness_hw_changed = -1; | |
270c3957 | 294 | #endif |
c72a1d60 | 295 | /* add to the list of leds */ |
72f8da32 | 296 | down_write(&leds_list_lock); |
c72a1d60 | 297 | list_add_tail(&led_cdev->node, &leds_list); |
72f8da32 | 298 | up_write(&leds_list_lock); |
c72a1d60 | 299 | |
1bd465e6 GL |
300 | if (!led_cdev->max_brightness) |
301 | led_cdev->max_brightness = LED_FULL; | |
302 | ||
29d76dfa HMH |
303 | led_update_brightness(led_cdev); |
304 | ||
757b06ae | 305 | led_init_core(led_cdev); |
5ada28bf | 306 | |
c3bc9956 | 307 | #ifdef CONFIG_LEDS_TRIGGERS |
12fda168 | 308 | led_trigger_set_default(led_cdev); |
c3bc9956 RP |
309 | #endif |
310 | ||
6d71021a LH |
311 | mutex_unlock(&led_cdev->led_access); |
312 | ||
d23e7b8b | 313 | dev_dbg(parent, "Registered led device: %s\n", |
f8a7c6fe | 314 | led_cdev->name); |
c72a1d60 RP |
315 | |
316 | return 0; | |
317 | } | |
b2b998c0 | 318 | EXPORT_SYMBOL_GPL(led_classdev_register_ext); |
c72a1d60 RP |
319 | |
320 | /** | |
0266a458 | 321 | * led_classdev_unregister - unregisters a object of led_properties class. |
70d63ccc | 322 | * @led_cdev: the led device to unregister |
c72a1d60 RP |
323 | * |
324 | * Unregisters a previously registered via led_classdev_register object. | |
325 | */ | |
b844eba2 | 326 | void led_classdev_unregister(struct led_classdev *led_cdev) |
c72a1d60 | 327 | { |
c3bc9956 | 328 | #ifdef CONFIG_LEDS_TRIGGERS |
dc47206e | 329 | down_write(&led_cdev->trigger_lock); |
c3bc9956 RP |
330 | if (led_cdev->trigger) |
331 | led_trigger_set(led_cdev, NULL); | |
dc47206e | 332 | up_write(&led_cdev->trigger_lock); |
c3bc9956 | 333 | #endif |
c72a1d60 | 334 | |
d84d80f3 HK |
335 | led_cdev->flags |= LED_UNREGISTERING; |
336 | ||
5ada28bf | 337 | /* Stop blinking */ |
d23a22a7 | 338 | led_stop_software_blink(led_cdev); |
d1aa577f | 339 | |
19cd67e2 | 340 | led_set_brightness(led_cdev, LED_OFF); |
5ada28bf | 341 | |
d1aa577f MK |
342 | flush_work(&led_cdev->set_brightness_work); |
343 | ||
0cb8eb30 HG |
344 | if (led_cdev->flags & LED_BRIGHT_HW_CHANGED) |
345 | led_remove_brightness_hw_changed(led_cdev); | |
346 | ||
b844eba2 | 347 | device_unregister(led_cdev->dev); |
c72a1d60 | 348 | |
72f8da32 | 349 | down_write(&leds_list_lock); |
c72a1d60 | 350 | list_del(&led_cdev->node); |
72f8da32 | 351 | up_write(&leds_list_lock); |
acd899e4 JA |
352 | |
353 | mutex_destroy(&led_cdev->led_access); | |
c72a1d60 | 354 | } |
b844eba2 | 355 | EXPORT_SYMBOL_GPL(led_classdev_unregister); |
c72a1d60 | 356 | |
ca1bb4ee BA |
357 | static void devm_led_classdev_release(struct device *dev, void *res) |
358 | { | |
359 | led_classdev_unregister(*(struct led_classdev **)res); | |
360 | } | |
361 | ||
362 | /** | |
b2b998c0 | 363 | * devm_led_classdev_register_ext - resource managed led_classdev_register_ext() |
442c6098 RM |
364 | * |
365 | * @parent: parent of LED device | |
ca1bb4ee | 366 | * @led_cdev: the led_classdev structure for this device. |
b2b998c0 | 367 | * @init_data: LED class device initialization data |
ca1bb4ee | 368 | */ |
b2b998c0 JA |
369 | int devm_led_classdev_register_ext(struct device *parent, |
370 | struct led_classdev *led_cdev, | |
371 | struct led_init_data *init_data) | |
ca1bb4ee BA |
372 | { |
373 | struct led_classdev **dr; | |
374 | int rc; | |
375 | ||
376 | dr = devres_alloc(devm_led_classdev_release, sizeof(*dr), GFP_KERNEL); | |
377 | if (!dr) | |
378 | return -ENOMEM; | |
379 | ||
b2b998c0 | 380 | rc = led_classdev_register_ext(parent, led_cdev, init_data); |
ca1bb4ee BA |
381 | if (rc) { |
382 | devres_free(dr); | |
383 | return rc; | |
384 | } | |
385 | ||
386 | *dr = led_cdev; | |
387 | devres_add(parent, dr); | |
388 | ||
389 | return 0; | |
390 | } | |
b2b998c0 | 391 | EXPORT_SYMBOL_GPL(devm_led_classdev_register_ext); |
ca1bb4ee BA |
392 | |
393 | static int devm_led_classdev_match(struct device *dev, void *res, void *data) | |
394 | { | |
395 | struct led_cdev **p = res; | |
396 | ||
397 | if (WARN_ON(!p || !*p)) | |
398 | return 0; | |
399 | ||
400 | return *p == data; | |
401 | } | |
402 | ||
403 | /** | |
404 | * devm_led_classdev_unregister() - resource managed led_classdev_unregister() | |
405 | * @parent: The device to unregister. | |
406 | * @led_cdev: the led_classdev structure for this device. | |
407 | */ | |
408 | void devm_led_classdev_unregister(struct device *dev, | |
409 | struct led_classdev *led_cdev) | |
410 | { | |
411 | WARN_ON(devres_release(dev, | |
412 | devm_led_classdev_release, | |
413 | devm_led_classdev_match, led_cdev)); | |
414 | } | |
415 | EXPORT_SYMBOL_GPL(devm_led_classdev_unregister); | |
416 | ||
c72a1d60 RP |
417 | static int __init leds_init(void) |
418 | { | |
419 | leds_class = class_create(THIS_MODULE, "leds"); | |
420 | if (IS_ERR(leds_class)) | |
421 | return PTR_ERR(leds_class); | |
73e1ab41 | 422 | leds_class->pm = &leds_class_dev_pm_ops; |
5baa7503 | 423 | leds_class->dev_groups = led_groups; |
c72a1d60 RP |
424 | return 0; |
425 | } | |
426 | ||
427 | static void __exit leds_exit(void) | |
428 | { | |
429 | class_destroy(leds_class); | |
430 | } | |
431 | ||
432 | subsys_initcall(leds_init); | |
433 | module_exit(leds_exit); | |
434 | ||
435 | MODULE_AUTHOR("John Lenz, Richard Purdie"); | |
436 | MODULE_LICENSE("GPL"); | |
437 | MODULE_DESCRIPTION("LED Class Interface"); |