/* * LED Flash class interface * * Copyright (C) 2015 Samsung Electronics Co., Ltd. * Author: Jacek Anaszewski * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #include #include #include #include #include #include #include "leds.h" #define has_flash_op(fled_cdev, op) \ (fled_cdev && fled_cdev->ops->op) #define call_flash_op(fled_cdev, op, args...) \ ((has_flash_op(fled_cdev, op)) ? \ (fled_cdev->ops->op(fled_cdev, args)) : \ -EINVAL) static const char * const led_flash_fault_names[] = { "led-over-voltage", "flash-timeout-exceeded", "controller-over-temperature", "controller-short-circuit", "led-power-supply-over-current", "indicator-led-fault", "led-under-voltage", "controller-under-voltage", "led-over-temperature", }; static ssize_t flash_brightness_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); unsigned long state; ssize_t ret; mutex_lock(&led_cdev->led_access); if (led_sysfs_is_disabled(led_cdev)) { ret = -EBUSY; goto unlock; } ret = kstrtoul(buf, 10, &state); if (ret) goto unlock; ret = led_set_flash_brightness(fled_cdev, state); if (ret < 0) goto unlock; ret = size; unlock: mutex_unlock(&led_cdev->led_access); return ret; } static ssize_t flash_brightness_show(struct device *dev, struct device_attribute *attr, char *buf) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); /* no lock needed for this */ led_update_flash_brightness(fled_cdev); return sprintf(buf, "%u\n", fled_cdev->brightness.val); } static DEVICE_ATTR_RW(flash_brightness); static ssize_t max_flash_brightness_show(struct device *dev, struct device_attribute *attr, char *buf) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); return sprintf(buf, "%u\n", fled_cdev->brightness.max); } static DEVICE_ATTR_RO(max_flash_brightness); static ssize_t flash_strobe_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); unsigned long state; ssize_t ret = -EINVAL; mutex_lock(&led_cdev->led_access); if (led_sysfs_is_disabled(led_cdev)) { ret = -EBUSY; goto unlock; } ret = kstrtoul(buf, 10, &state); if (ret) goto unlock; if (state < 0 || state > 1) { ret = -EINVAL; goto unlock; } ret = led_set_flash_strobe(fled_cdev, state); if (ret < 0) goto unlock; ret = size; unlock: mutex_unlock(&led_cdev->led_access); return ret; } static ssize_t flash_strobe_show(struct device *dev, struct device_attribute *attr, char *buf) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); bool state; int ret; /* no lock needed for this */ ret = led_get_flash_strobe(fled_cdev, &state); if (ret < 0) return ret; return sprintf(buf, "%u\n", state); } static DEVICE_ATTR_RW(flash_strobe); static ssize_t flash_timeout_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); unsigned long flash_timeout; ssize_t ret; mutex_lock(&led_cdev->led_access); if (led_sysfs_is_disabled(led_cdev)) { ret = -EBUSY; goto unlock; } ret = kstrtoul(buf, 10, &flash_timeout); if (ret) goto unlock; ret = led_set_flash_timeout(fled_cdev, flash_timeout); if (ret < 0) goto unlock; ret = size; unlock: mutex_unlock(&led_cdev->led_access); return ret; } static ssize_t flash_timeout_show(struct device *dev, struct device_attribute *attr, char *buf) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); return sprintf(buf, "%u\n", fled_cdev->timeout.val); } static DEVICE_ATTR_RW(flash_timeout); static ssize_t max_flash_timeout_show(struct device *dev, struct device_attribute *attr, char *buf) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); return sprintf(buf, "%u\n", fled_cdev->timeout.max); } static DEVICE_ATTR_RO(max_flash_timeout); static ssize_t flash_fault_show(struct device *dev, struct device_attribute *attr, char *buf) { struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev); u32 fault, mask = 0x1; char *pbuf = buf; int i, ret, buf_len; ret = led_get_flash_fault(fled_cdev, &fault); if (ret < 0) return -EINVAL; *buf = '\0'; for (i = 0; i < LED_NUM_FLASH_FAULTS; ++i) { if (fault & mask) { buf_len = sprintf(pbuf, "%s ", led_flash_fault_names[i]); pbuf += buf_len; } mask <<= 1; } return sprintf(buf, "%s\n", buf); } static DEVICE_ATTR_RO(flash_fault); static struct attribute *led_flash_strobe_attrs[] = { &dev_attr_flash_strobe.attr, NULL, }; static struct attribute *led_flash_timeout_attrs[] = { &dev_attr_flash_timeout.attr, &dev_attr_max_flash_timeout.attr, NULL, }; static struct attribute *led_flash_brightness_attrs[] = { &dev_attr_flash_brightness.attr, &dev_attr_m