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>
17 #define has_flash_op(fled_cdev, op) \
18 (fled_cdev && fled_cdev->ops->op)
20 #define call_flash_op(fled_cdev, op, args...) \
21 ((has_flash_op(fled_cdev, op)) ? \
22 (fled_cdev->ops->op(fled_cdev, args)) : \
25 static const char * const led_flash_fault_names[] = {
27 "flash-timeout-exceeded",
28 "controller-over-temperature",
29 "controller-short-circuit",
30 "led-power-supply-over-current",
31 "indicator-led-fault",
33 "controller-under-voltage",
34 "led-over-temperature",
37 static ssize_t flash_brightness_store(struct device *dev,
38 struct device_attribute *attr, const char *buf, size_t size)
40 struct led_classdev *led_cdev = dev_get_drvdata(dev);
41 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
45 mutex_lock(&led_cdev->led_access);
47 if (led_sysfs_is_disabled(led_cdev)) {
52 ret = kstrtoul(buf, 10, &state);
56 ret = led_set_flash_brightness(fled_cdev, state);
62 mutex_unlock(&led_cdev->led_access);
66 static ssize_t flash_brightness_show(struct device *dev,
67 struct device_attribute *attr, char *buf)
69 struct led_classdev *led_cdev = dev_get_drvdata(dev);
70 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
72 /* no lock needed for this */
73 led_update_flash_brightness(fled_cdev);
75 return sprintf(buf, "%u\n", fled_cdev->brightness.val);
77 static DEVICE_ATTR_RW(flash_brightness);
79 static ssize_t max_flash_brightness_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
82 struct led_classdev *led_cdev = dev_get_drvdata(dev);
83 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
85 return sprintf(buf, "%u\n", fled_cdev->brightness.max);
87 static DEVICE_ATTR_RO(max_flash_brightness);
89 static ssize_t flash_strobe_store(struct device *dev,
90 struct device_attribute *attr, const char *buf, size_t size)
92 struct led_classdev *led_cdev = dev_get_drvdata(dev);
93 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
97 mutex_lock(&led_cdev->led_access);
99 if (led_sysfs_is_disabled(led_cdev))
102 ret = kstrtoul(buf, 10, &state);
111 ret = led_set_flash_strobe(fled_cdev, state);
116 mutex_unlock(&led_cdev->led_access);
120 static ssize_t flash_strobe_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct led_classdev *led_cdev = dev_get_drvdata(dev);
124 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
128 /* no lock needed for this */
129 ret = led_get_flash_strobe(fled_cdev, &state);
133 return sprintf(buf, "%u\n", state);
135 static DEVICE_ATTR_RW(flash_strobe);
137 static ssize_t flash_timeout_store(struct device *dev,
138 struct device_attribute *attr, const char *buf, size_t size)
140 struct led_classdev *led_cdev = dev_get_drvdata(dev);
141 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
142 unsigned long flash_timeout;
145 mutex_lock(&led_cdev->led_access);
147 if (led_sysfs_is_disabled(led_cdev)) {
152 ret = kstrtoul(buf, 10, &flash_timeout);
156 ret = led_set_flash_timeout(fled_cdev, flash_timeout);
162 mutex_unlock(&led_cdev->led_access);
166 static ssize_t flash_timeout_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
169 struct led_classdev *led_cdev = dev_get_drvdata(dev);
170 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
172 return sprintf(buf, "%u\n", fled_cdev->timeout.val);
174 static DEVICE_ATTR_RW(flash_timeout);
176 static ssize_t max_flash_timeout_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
179 struct led_classdev *led_cdev = dev_get_drvdata(dev);
180 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
182 return sprintf(buf, "%u\n", fled_cdev->timeout.max);
184 static DEVICE_ATTR_RO(max_flash_timeout);
186 static ssize_t flash_fault_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
189 struct led_classdev *led_cdev = dev_get_drvdata(dev);
190 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
191 u32 fault, mask = 0x1;
195 ret = led_get_flash_fault(fled_cdev, &fault);
201 for (i = 0; i < LED_NUM_FLASH_FAULTS; ++i) {
203 buf_len = sprintf(pbuf, "%s ",
204 led_flash_fault_names[i]);
210 return strlen(strcat(buf, "\n"));
212 static DEVICE_ATTR_RO(flash_fault);
214 static struct attribute *led_flash_strobe_attrs[] = {
215 &dev_attr_flash_strobe.attr,
219 static struct attribute *led_flash_timeout_attrs[] = {
220 &dev_attr_flash_timeout.attr,
221 &dev_attr_max_flash_timeout.attr,
225 static struct attribute *led_flash_brightness_attrs[] = {
226 &dev_attr_flash_brightness.attr,
227 &dev_attr_max_flash_brightness.attr,
231 static struct attribute *led_flash_fault_attrs[] = {
232 &dev_attr_flash_fault.attr,
236 static const struct attribute_group led_flash_strobe_group = {
237 .attrs = led_flash_strobe_attrs,
240 static const struct attribute_group led_flash_timeout_group = {
241 .attrs = led_flash_timeout_attrs,
244 static const struct attribute_group led_flash_brightness_group = {
245 .attrs = led_flash_brightness_attrs,
248 static const struct attribute_group led_flash_fault_group = {
249 .attrs = led_flash_fault_attrs,
252 static void led_flash_resume(struct led_classdev *led_cdev)
254 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
256 call_flash_op(fled_cdev, flash_brightness_set,
257 fled_cdev->brightness.val);
258 call_flash_op(fled_cdev, timeout_set, fled_cdev->timeout.val);
261 static void led_flash_init_sysfs_groups(struct led_classdev_flash *fled_cdev)
263 struct led_classdev *led_cdev = &fled_cdev->led_cdev;
264 const struct led_flash_ops *ops = fled_cdev->ops;
265 const struct attribute_group **flash_groups = fled_cdev->sysfs_groups;
267 int num_sysfs_groups = 0;
269 flash_groups[num_sysfs_groups++] = &led_flash_strobe_group;
271 if (ops->flash_brightness_set)
272 flash_groups[num_sysfs_groups++] = &led_flash_brightness_group;
274 if (ops->timeout_set)
275 flash_groups[num_sysfs_groups++] = &led_flash_timeout_group;
278 flash_groups[num_sysfs_groups++] = &led_flash_fault_group;
280 led_cdev->groups = flash_groups;
283 int led_classdev_flash_register_ext(struct device *parent,
284 struct led_classdev_flash *fled_cdev,
285 struct led_init_data *init_data)
287 struct led_classdev *led_cdev;
288 const struct led_flash_ops *ops;
294 led_cdev = &fled_cdev->led_cdev;
296 if (led_cdev->flags & LED_DEV_CAP_FLASH) {
297 if (!led_cdev->brightness_set_blocking)
300 ops = fled_cdev->ops;
301 if (!ops || !ops->strobe_set)
304 led_cdev->flash_resume = led_flash_resume;
306 /* Select the sysfs attributes to be created for the device */
307 led_flash_init_sysfs_groups(fled_cdev);
310 /* Register led class device */
311 ret = led_classdev_register_ext(parent, led_cdev, init_data);
317 EXPORT_SYMBOL_GPL(led_classdev_flash_register_ext);
319 void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev)
324 led_classdev_unregister(&fled_cdev->led_cdev);
326 EXPORT_SYMBOL_GPL(led_classdev_flash_unregister);
328 static void devm_led_classdev_flash_release(struct device *dev, void *res)
330 led_classdev_flash_unregister(*(struct led_classdev_flash **)res);
333 int devm_led_classdev_flash_register_ext(struct device *parent,
334 struct led_classdev_flash *fled_cdev,
335 struct led_init_data *init_data)
337 struct led_classdev_flash **dr;
340 dr = devres_alloc(devm_led_classdev_flash_release, sizeof(*dr),
345 ret = led_classdev_flash_register_ext(parent, fled_cdev, init_data);
352 devres_add(parent, dr);
356 EXPORT_SYMBOL_GPL(devm_led_classdev_flash_register_ext);
358 static int devm_led_classdev_flash_match(struct device *dev,
359 void *res, void *data)
361 struct led_classdev_flash **p = res;
363 if (WARN_ON(!p || !*p))
369 void devm_led_classdev_flash_unregister(struct device *dev,
370 struct led_classdev_flash *fled_cdev)
372 WARN_ON(devres_release(dev,
373 devm_led_classdev_flash_release,
374 devm_led_classdev_flash_match, fled_cdev));
376 EXPORT_SYMBOL_GPL(devm_led_classdev_flash_unregister);
378 static void led_clamp_align(struct led_flash_setting *s)
382 v = s->val + s->step / 2;
383 v = clamp(v, s->min, s->max);
385 offset = s->step * (offset / s->step);
386 s->val = s->min + offset;
389 int led_set_flash_timeout(struct led_classdev_flash *fled_cdev, u32 timeout)
391 struct led_classdev *led_cdev = &fled_cdev->led_cdev;
392 struct led_flash_setting *s = &fled_cdev->timeout;
397 if (!(led_cdev->flags & LED_SUSPENDED))
398 return call_flash_op(fled_cdev, timeout_set, s->val);
402 EXPORT_SYMBOL_GPL(led_set_flash_timeout);
404 int led_get_flash_fault(struct led_classdev_flash *fled_cdev, u32 *fault)
406 return call_flash_op(fled_cdev, fault_get, fault);
408 EXPORT_SYMBOL_GPL(led_get_flash_fault);
410 int led_set_flash_brightness(struct led_classdev_flash *fled_cdev,
413 struct led_classdev *led_cdev = &fled_cdev->led_cdev;
414 struct led_flash_setting *s = &fled_cdev->brightness;
419 if (!(led_cdev->flags & LED_SUSPENDED))
420 return call_flash_op(fled_cdev, flash_brightness_set, s->val);
424 EXPORT_SYMBOL_GPL(led_set_flash_brightness);
426 int led_update_flash_brightness(struct led_classdev_flash *fled_cdev)
428 struct led_flash_setting *s = &fled_cdev->brightness;
431 if (has_flash_op(fled_cdev, flash_brightness_get)) {
432 int ret = call_flash_op(fled_cdev, flash_brightness_get,
442 EXPORT_SYMBOL_GPL(led_update_flash_brightness);
444 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
445 MODULE_DESCRIPTION("LED Flash class interface");
446 MODULE_LICENSE("GPL v2");