2 * LED Flash class interface
4 * Copyright (C) 2015 Samsung Electronics Co., Ltd.
5 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/device.h>
13 #include <linux/init.h>
14 #include <linux/led-class-flash.h>
15 #include <linux/leds.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
20 #define has_flash_op(fled_cdev, op) \
21 (fled_cdev && fled_cdev->ops->op)
23 #define call_flash_op(fled_cdev, op, args...) \
24 ((has_flash_op(fled_cdev, op)) ? \
25 (fled_cdev->ops->op(fled_cdev, args)) : \
28 static const char * const led_flash_fault_names
[] = {
30 "flash-timeout-exceeded",
31 "controller-over-temperature",
32 "controller-short-circuit",
33 "led-power-supply-over-current",
34 "indicator-led-fault",
36 "controller-under-voltage",
37 "led-over-temperature",
40 static ssize_t
flash_brightness_store(struct device
*dev
,
41 struct device_attribute
*attr
, const char *buf
, size_t size
)
43 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
44 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
48 mutex_lock(&led_cdev
->led_access
);
50 if (led_sysfs_is_disabled(led_cdev
)) {
55 ret
= kstrtoul(buf
, 10, &state
);
59 ret
= led_set_flash_brightness(fled_cdev
, state
);
65 mutex_unlock(&led_cdev
->led_access
);
69 static ssize_t
flash_brightness_show(struct device
*dev
,
70 struct device_attribute
*attr
, char *buf
)
72 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
73 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
75 /* no lock needed for this */
76 led_update_flash_brightness(fled_cdev
);
78 return sprintf(buf
, "%u\n", fled_cdev
->brightness
.val
);
80 static DEVICE_ATTR_RW(flash_brightness
);
82 static ssize_t
max_flash_brightness_show(struct device
*dev
,
83 struct device_attribute
*attr
, char *buf
)
85 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
86 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
88 return sprintf(buf
, "%u\n", fled_cdev
->brightness
.max
);
90 static DEVICE_ATTR_RO(max_flash_brightness
);
92 static ssize_t
flash_strobe_store(struct device
*dev
,
93 struct device_attribute
*attr
, const char *buf
, size_t size
)
95 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
96 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
98 ssize_t ret
= -EINVAL
;
100 mutex_lock(&led_cdev
->led_access
);
102 if (led_sysfs_is_disabled(led_cdev
)) {
107 ret
= kstrtoul(buf
, 10, &state
);
111 if (state
< 0 || state
> 1) {
116 ret
= led_set_flash_strobe(fled_cdev
, state
);
121 mutex_unlock(&led_cdev
->led_access
);
125 static ssize_t
flash_strobe_show(struct device
*dev
,
126 struct device_attribute
*attr
, char *buf
)
128 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
129 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
133 /* no lock needed for this */
134 ret
= led_get_flash_strobe(fled_cdev
, &state
);
138 return sprintf(buf
, "%u\n", state
);
140 static DEVICE_ATTR_RW(flash_strobe
);
142 static ssize_t
flash_timeout_store(struct device
*dev
,
143 struct device_attribute
*attr
, const char *buf
, size_t size
)
145 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
146 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
147 unsigned long flash_timeout
;
150 mutex_lock(&led_cdev
->led_access
);
152 if (led_sysfs_is_disabled(led_cdev
)) {
157 ret
= kstrtoul(buf
, 10, &flash_timeout
);
161 ret
= led_set_flash_timeout(fled_cdev
, flash_timeout
);
167 mutex_unlock(&led_cdev
->led_access
);
171 static ssize_t
flash_timeout_show(struct device
*dev
,
172 struct device_attribute
*attr
, char *buf
)
174 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
175 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
177 return sprintf(buf
, "%u\n", fled_cdev
->timeout
.val
);
179 static DEVICE_ATTR_RW(flash_timeout
);
181 static ssize_t
max_flash_timeout_show(struct device
*dev
,
182 struct device_attribute
*attr
, char *buf
)
184 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
185 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
187 return sprintf(buf
, "%u\n", fled_cdev
->timeout
.max
);
189 static DEVICE_ATTR_RO(max_flash_timeout
);
191 static ssize_t
flash_fault_show(struct device
*dev
,
192 struct device_attribute
*attr
, char *buf
)
194 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
195 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
196 u32 fault
, mask
= 0x1;
200 ret
= led_get_flash_fault(fled_cdev
, &fault
);
206 for (i
= 0; i
< LED_NUM_FLASH_FAULTS
; ++i
) {
208 buf_len
= sprintf(pbuf
, "%s ",
209 led_flash_fault_names
[i
]);
215 return sprintf(buf
, "%s\n", buf
);
217 static DEVICE_ATTR_RO(flash_fault
);
219 static ssize_t
available_sync_leds_show(struct device
*dev
,
220 struct device_attribute
*attr
, char *buf
)
222 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
223 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
227 buf_len
= sprintf(pbuf
, "[0: none] ");
230 for (i
= 0; i
< fled_cdev
->num_sync_leds
; ++i
) {
231 buf_len
= sprintf(pbuf
, "[%d: %s] ", i
+ 1,
232 fled_cdev
->sync_leds
[i
]->led_cdev
.name
);
236 return sprintf(buf
, "%s\n", buf
);
238 static DEVICE_ATTR_RO(available_sync_leds
);
240 static ssize_t
flash_sync_strobe_store(struct device
*dev
,
241 struct device_attribute
*attr
, const char *buf
, size_t size
)
243 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
244 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
245 unsigned long led_id
;
248 mutex_lock(&led_cdev
->led_access
);
250 if (led_sysfs_is_disabled(led_cdev
)) {
255 ret
= kstrtoul(buf
, 10, &led_id
);
259 if (led_id
> fled_cdev
->num_sync_leds
) {
264 fled_cdev
->sync_led_id
= led_id
;
268 mutex_unlock(&led_cdev
->led_access
);
272 static ssize_t
flash_sync_strobe_show(struct device
*dev
,
273 struct device_attribute
*attr
, char *buf
)
275 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
276 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
277 int sled_id
= fled_cdev
->sync_led_id
;
278 char *sync_led_name
= "none";
280 if (fled_cdev
->sync_led_id
> 0)
281 sync_led_name
= (char *)
282 fled_cdev
->sync_leds
[sled_id
- 1]->led_cdev
.name
;
284 return sprintf(buf
, "[%d: %s]\n", sled_id
, sync_led_name
);
286 static DEVICE_ATTR_RW(flash_sync_strobe
);
288 static struct attribute
*led_flash_strobe_attrs
[] = {
289 &dev_attr_flash_strobe
.attr
,
293 static struct attribute
*led_flash_timeout_attrs
[] = {
294 &dev_attr_flash_timeout
.attr
,
295 &dev_attr_max_flash_timeout
.attr
,
299 static struct attribute
*led_flash_brightness_attrs
[] = {
300 &dev_attr_flash_brightness
.attr
,
301 &dev_attr_max_flash_brightness
.attr
,
305 static struct attribute
*led_flash_fault_attrs
[] = {
306 &dev_attr_flash_fault
.attr
,
310 static struct attribute
*led_flash_sync_strobe_attrs
[] = {
311 &dev_attr_available_sync_leds
.attr
,
312 &dev_attr_flash_sync_strobe
.attr
,
316 static const struct attribute_group led_flash_strobe_group
= {
317 .attrs
= led_flash_strobe_attrs
,
320 static const struct attribute_group led_flash_timeout_group
= {
321 .attrs
= led_flash_timeout_attrs
,
324 static const struct attribute_group led_flash_brightness_group
= {
325 .attrs
= led_flash_brightness_attrs
,
328 static const struct attribute_group led_flash_fault_group
= {
329 .attrs
= led_flash_fault_attrs
,
332 static const struct attribute_group led_flash_sync_strobe_group
= {
333 .attrs
= led_flash_sync_strobe_attrs
,
336 static void led_flash_resume(struct led_classdev
*led_cdev
)
338 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
340 call_flash_op(fled_cdev
, flash_brightness_set
,
341 fled_cdev
->brightness
.val
);
342 call_flash_op(fled_cdev
, timeout_set
, fled_cdev
->timeout
.val
);
345 static void led_flash_init_sysfs_groups(struct led_classdev_flash
*fled_cdev
)
347 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
348 const struct led_flash_ops
*ops
= fled_cdev
->ops
;
349 const struct attribute_group
**flash_groups
= fled_cdev
->sysfs_groups
;
351 int num_sysfs_groups
= 0;
353 flash_groups
[num_sysfs_groups
++] = &led_flash_strobe_group
;
355 if (ops
->flash_brightness_set
)
356 flash_groups
[num_sysfs_groups
++] = &led_flash_brightness_group
;
358 if (ops
->timeout_set
)
359 flash_groups
[num_sysfs_groups
++] = &led_flash_timeout_group
;
362 flash_groups
[num_sysfs_groups
++] = &led_flash_fault_group
;
364 if (led_cdev
->flags
& LED_DEV_CAP_SYNC_STROBE
)
365 flash_groups
[num_sysfs_groups
++] = &led_flash_sync_strobe_group
;
367 led_cdev
->groups
= flash_groups
;
370 int led_classdev_flash_register(struct device
*parent
,
371 struct led_classdev_flash
*fled_cdev
)
373 struct led_classdev
*led_cdev
;
374 const struct led_flash_ops
*ops
;
380 led_cdev
= &fled_cdev
->led_cdev
;
382 if (led_cdev
->flags
& LED_DEV_CAP_FLASH
) {
383 if (!led_cdev
->brightness_set_sync
)
386 ops
= fled_cdev
->ops
;
387 if (!ops
|| !ops
->strobe_set
)
390 led_cdev
->flash_resume
= led_flash_resume
;
392 /* Select the sysfs attributes to be created for the device */
393 led_flash_init_sysfs_groups(fled_cdev
);
396 /* Register led class device */
397 ret
= led_classdev_register(parent
, led_cdev
);
401 /* Setting a torch brightness needs to have immediate effect */
402 led_cdev
->flags
&= ~SET_BRIGHTNESS_ASYNC
;
403 led_cdev
->flags
|= SET_BRIGHTNESS_SYNC
;
407 EXPORT_SYMBOL_GPL(led_classdev_flash_register
);
409 void led_classdev_flash_unregister(struct led_classdev_flash
*fled_cdev
)
414 led_classdev_unregister(&fled_cdev
->led_cdev
);
416 EXPORT_SYMBOL_GPL(led_classdev_flash_unregister
);
418 static void led_clamp_align(struct led_flash_setting
*s
)
422 v
= s
->val
+ s
->step
/ 2;
423 v
= clamp(v
, s
->min
, s
->max
);
425 offset
= s
->step
* (offset
/ s
->step
);
426 s
->val
= s
->min
+ offset
;
429 int led_set_flash_timeout(struct led_classdev_flash
*fled_cdev
, u32 timeout
)
431 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
432 struct led_flash_setting
*s
= &fled_cdev
->timeout
;
437 if (!(led_cdev
->flags
& LED_SUSPENDED
))
438 return call_flash_op(fled_cdev
, timeout_set
, s
->val
);
442 EXPORT_SYMBOL_GPL(led_set_flash_timeout
);
444 int led_get_flash_fault(struct led_classdev_flash
*fled_cdev
, u32
*fault
)
446 return call_flash_op(fled_cdev
, fault_get
, fault
);
448 EXPORT_SYMBOL_GPL(led_get_flash_fault
);
450 int led_set_flash_brightness(struct led_classdev_flash
*fled_cdev
,
453 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
454 struct led_flash_setting
*s
= &fled_cdev
->brightness
;
459 if (!(led_cdev
->flags
& LED_SUSPENDED
))
460 return call_flash_op(fled_cdev
, flash_brightness_set
, s
->val
);
464 EXPORT_SYMBOL_GPL(led_set_flash_brightness
);
466 int led_update_flash_brightness(struct led_classdev_flash
*fled_cdev
)
468 struct led_flash_setting
*s
= &fled_cdev
->brightness
;
471 if (has_flash_op(fled_cdev
, flash_brightness_get
)) {
472 int ret
= call_flash_op(fled_cdev
, flash_brightness_get
,
482 EXPORT_SYMBOL_GPL(led_update_flash_brightness
);
484 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
485 MODULE_DESCRIPTION("LED Flash class interface");
486 MODULE_LICENSE("GPL v2");