1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED Flash class interface
5 * Copyright (C) 2015 Samsung Electronics Co., Ltd.
6 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
9 #include <linux/device.h>
10 #include <linux/init.h>
11 #include <linux/led-class-flash.h>
12 #include <linux/leds.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #define has_flash_op(fled_cdev, op) \
17 (fled_cdev && fled_cdev->ops->op)
19 #define call_flash_op(fled_cdev, op, args...) \
20 ((has_flash_op(fled_cdev, op)) ? \
21 (fled_cdev->ops->op(fled_cdev, args)) : \
24 static const char * const led_flash_fault_names
[] = {
26 "flash-timeout-exceeded",
27 "controller-over-temperature",
28 "controller-short-circuit",
29 "led-power-supply-over-current",
30 "indicator-led-fault",
32 "controller-under-voltage",
33 "led-over-temperature",
36 static ssize_t
flash_brightness_store(struct device
*dev
,
37 struct device_attribute
*attr
, const char *buf
, size_t size
)
39 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
40 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
44 mutex_lock(&led_cdev
->led_access
);
46 if (led_sysfs_is_disabled(led_cdev
)) {
51 ret
= kstrtoul(buf
, 10, &state
);
55 ret
= led_set_flash_brightness(fled_cdev
, state
);
61 mutex_unlock(&led_cdev
->led_access
);
65 static ssize_t
flash_brightness_show(struct device
*dev
,
66 struct device_attribute
*attr
, char *buf
)
68 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
69 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
71 /* no lock needed for this */
72 led_update_flash_brightness(fled_cdev
);
74 return sprintf(buf
, "%u\n", fled_cdev
->brightness
.val
);
76 static DEVICE_ATTR_RW(flash_brightness
);
78 static ssize_t
max_flash_brightness_show(struct device
*dev
,
79 struct device_attribute
*attr
, char *buf
)
81 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
82 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
84 return sprintf(buf
, "%u\n", fled_cdev
->brightness
.max
);
86 static DEVICE_ATTR_RO(max_flash_brightness
);
88 static ssize_t
flash_strobe_store(struct device
*dev
,
89 struct device_attribute
*attr
, const char *buf
, size_t size
)
91 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
92 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
96 mutex_lock(&led_cdev
->led_access
);
98 if (led_sysfs_is_disabled(led_cdev
))
101 ret
= kstrtoul(buf
, 10, &state
);
110 ret
= led_set_flash_strobe(fled_cdev
, state
);
115 mutex_unlock(&led_cdev
->led_access
);
119 static ssize_t
flash_strobe_show(struct device
*dev
,
120 struct device_attribute
*attr
, char *buf
)
122 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
123 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
127 /* no lock needed for this */
128 ret
= led_get_flash_strobe(fled_cdev
, &state
);
132 return sprintf(buf
, "%u\n", state
);
134 static DEVICE_ATTR_RW(flash_strobe
);
136 static ssize_t
flash_timeout_store(struct device
*dev
,
137 struct device_attribute
*attr
, const char *buf
, size_t size
)
139 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
140 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
141 unsigned long flash_timeout
;
144 mutex_lock(&led_cdev
->led_access
);
146 if (led_sysfs_is_disabled(led_cdev
)) {
151 ret
= kstrtoul(buf
, 10, &flash_timeout
);
155 ret
= led_set_flash_timeout(fled_cdev
, flash_timeout
);
161 mutex_unlock(&led_cdev
->led_access
);
165 static ssize_t
flash_timeout_show(struct device
*dev
,
166 struct device_attribute
*attr
, char *buf
)
168 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
169 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
171 return sprintf(buf
, "%u\n", fled_cdev
->timeout
.val
);
173 static DEVICE_ATTR_RW(flash_timeout
);
175 static ssize_t
max_flash_timeout_show(struct device
*dev
,
176 struct device_attribute
*attr
, char *buf
)
178 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
179 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
181 return sprintf(buf
, "%u\n", fled_cdev
->timeout
.max
);
183 static DEVICE_ATTR_RO(max_flash_timeout
);
185 static ssize_t
flash_fault_show(struct device
*dev
,
186 struct device_attribute
*attr
, char *buf
)
188 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
189 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
190 u32 fault
, mask
= 0x1;
194 ret
= led_get_flash_fault(fled_cdev
, &fault
);
200 for (i
= 0; i
< LED_NUM_FLASH_FAULTS
; ++i
) {
202 buf_len
= sprintf(pbuf
, "%s ",
203 led_flash_fault_names
[i
]);
209 return strlen(strcat(buf
, "\n"));
211 static DEVICE_ATTR_RO(flash_fault
);
213 static struct attribute
*led_flash_strobe_attrs
[] = {
214 &dev_attr_flash_strobe
.attr
,
218 static struct attribute
*led_flash_timeout_attrs
[] = {
219 &dev_attr_flash_timeout
.attr
,
220 &dev_attr_max_flash_timeout
.attr
,
224 static struct attribute
*led_flash_brightness_attrs
[] = {
225 &dev_attr_flash_brightness
.attr
,
226 &dev_attr_max_flash_brightness
.attr
,
230 static struct attribute
*led_flash_fault_attrs
[] = {
231 &dev_attr_flash_fault
.attr
,
235 static const struct attribute_group led_flash_strobe_group
= {
236 .attrs
= led_flash_strobe_attrs
,
239 static const struct attribute_group led_flash_timeout_group
= {
240 .attrs
= led_flash_timeout_attrs
,
243 static const struct attribute_group led_flash_brightness_group
= {
244 .attrs
= led_flash_brightness_attrs
,
247 static const struct attribute_group led_flash_fault_group
= {
248 .attrs
= led_flash_fault_attrs
,
251 static void led_flash_resume(struct led_classdev
*led_cdev
)
253 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
255 call_flash_op(fled_cdev
, flash_brightness_set
,
256 fled_cdev
->brightness
.val
);
257 call_flash_op(fled_cdev
, timeout_set
, fled_cdev
->timeout
.val
);
260 static void led_flash_init_sysfs_groups(struct led_classdev_flash
*fled_cdev
)
262 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
263 const struct led_flash_ops
*ops
= fled_cdev
->ops
;
264 const struct attribute_group
**flash_groups
= fled_cdev
->sysfs_groups
;
266 int num_sysfs_groups
= 0;
268 flash_groups
[num_sysfs_groups
++] = &led_flash_strobe_group
;
270 if (ops
->flash_brightness_set
)
271 flash_groups
[num_sysfs_groups
++] = &led_flash_brightness_group
;
273 if (ops
->timeout_set
)
274 flash_groups
[num_sysfs_groups
++] = &led_flash_timeout_group
;
277 flash_groups
[num_sysfs_groups
++] = &led_flash_fault_group
;
279 led_cdev
->groups
= flash_groups
;
282 int led_classdev_flash_register_ext(struct device
*parent
,
283 struct led_classdev_flash
*fled_cdev
,
284 struct led_init_data
*init_data
)
286 struct led_classdev
*led_cdev
;
287 const struct led_flash_ops
*ops
;
293 led_cdev
= &fled_cdev
->led_cdev
;
295 if (led_cdev
->flags
& LED_DEV_CAP_FLASH
) {
296 if (!led_cdev
->brightness_set_blocking
)
299 ops
= fled_cdev
->ops
;
300 if (!ops
|| !ops
->strobe_set
)
303 led_cdev
->flash_resume
= led_flash_resume
;
305 /* Select the sysfs attributes to be created for the device */
306 led_flash_init_sysfs_groups(fled_cdev
);
309 /* Register led class device */
310 ret
= led_classdev_register_ext(parent
, led_cdev
, init_data
);
316 EXPORT_SYMBOL_GPL(led_classdev_flash_register_ext
);
318 void led_classdev_flash_unregister(struct led_classdev_flash
*fled_cdev
)
323 led_classdev_unregister(&fled_cdev
->led_cdev
);
325 EXPORT_SYMBOL_GPL(led_classdev_flash_unregister
);
327 static void devm_led_classdev_flash_release(struct device
*dev
, void *res
)
329 led_classdev_flash_unregister(*(struct led_classdev_flash
**)res
);
332 int devm_led_classdev_flash_register_ext(struct device
*parent
,
333 struct led_classdev_flash
*fled_cdev
,
334 struct led_init_data
*init_data
)
336 struct led_classdev_flash
**dr
;
339 dr
= devres_alloc(devm_led_classdev_flash_release
, sizeof(*dr
),
344 ret
= led_classdev_flash_register_ext(parent
, fled_cdev
, init_data
);
351 devres_add(parent
, dr
);
355 EXPORT_SYMBOL_GPL(devm_led_classdev_flash_register_ext
);
357 static int devm_led_classdev_flash_match(struct device
*dev
,
358 void *res
, void *data
)
360 struct led_classdev_flash
**p
= res
;
362 if (WARN_ON(!p
|| !*p
))
368 void devm_led_classdev_flash_unregister(struct device
*dev
,
369 struct led_classdev_flash
*fled_cdev
)
371 WARN_ON(devres_release(dev
,
372 devm_led_classdev_flash_release
,
373 devm_led_classdev_flash_match
, fled_cdev
));
375 EXPORT_SYMBOL_GPL(devm_led_classdev_flash_unregister
);
377 static void led_clamp_align(struct led_flash_setting
*s
)
381 v
= s
->val
+ s
->step
/ 2;
382 v
= clamp(v
, s
->min
, s
->max
);
384 offset
= s
->step
* (offset
/ s
->step
);
385 s
->val
= s
->min
+ offset
;
388 int led_set_flash_timeout(struct led_classdev_flash
*fled_cdev
, u32 timeout
)
390 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
391 struct led_flash_setting
*s
= &fled_cdev
->timeout
;
396 if (!(led_cdev
->flags
& LED_SUSPENDED
))
397 return call_flash_op(fled_cdev
, timeout_set
, s
->val
);
401 EXPORT_SYMBOL_GPL(led_set_flash_timeout
);
403 int led_get_flash_fault(struct led_classdev_flash
*fled_cdev
, u32
*fault
)
405 return call_flash_op(fled_cdev
, fault_get
, fault
);
407 EXPORT_SYMBOL_GPL(led_get_flash_fault
);
409 int led_set_flash_brightness(struct led_classdev_flash
*fled_cdev
,
412 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
413 struct led_flash_setting
*s
= &fled_cdev
->brightness
;
418 if (!(led_cdev
->flags
& LED_SUSPENDED
))
419 return call_flash_op(fled_cdev
, flash_brightness_set
, s
->val
);
423 EXPORT_SYMBOL_GPL(led_set_flash_brightness
);
425 int led_update_flash_brightness(struct led_classdev_flash
*fled_cdev
)
427 struct led_flash_setting
*s
= &fled_cdev
->brightness
;
430 if (has_flash_op(fled_cdev
, flash_brightness_get
)) {
431 int ret
= call_flash_op(fled_cdev
, flash_brightness_get
,
441 EXPORT_SYMBOL_GPL(led_update_flash_brightness
);
443 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
444 MODULE_DESCRIPTION("LED Flash class interface");
445 MODULE_LICENSE("GPL v2");