^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * LED Flash class interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2015 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/led-class-flash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "leds.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define has_flash_op(fled_cdev, op) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) (fled_cdev && fled_cdev->ops->op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define call_flash_op(fled_cdev, op, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) ((has_flash_op(fled_cdev, op)) ? \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) (fled_cdev->ops->op(fled_cdev, args)) : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static const char * const led_flash_fault_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) "led-over-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) "flash-timeout-exceeded",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) "controller-over-temperature",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) "controller-short-circuit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) "led-power-supply-over-current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) "indicator-led-fault",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) "led-under-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) "controller-under-voltage",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) "led-over-temperature",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static ssize_t flash_brightness_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) mutex_lock(&led_cdev->led_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (led_sysfs_is_disabled(led_cdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ret = kstrtoul(buf, 10, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = led_set_flash_brightness(fled_cdev, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mutex_unlock(&led_cdev->led_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static ssize_t flash_brightness_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* no lock needed for this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) led_update_flash_brightness(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return sprintf(buf, "%u\n", fled_cdev->brightness.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static DEVICE_ATTR_RW(flash_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static ssize_t max_flash_brightness_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return sprintf(buf, "%u\n", fled_cdev->brightness.max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static DEVICE_ATTR_RO(max_flash_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static ssize_t flash_strobe_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ssize_t ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) mutex_lock(&led_cdev->led_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (led_sysfs_is_disabled(led_cdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ret = kstrtoul(buf, 10, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (state > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = led_set_flash_strobe(fled_cdev, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) mutex_unlock(&led_cdev->led_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static ssize_t flash_strobe_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) bool state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* no lock needed for this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ret = led_get_flash_strobe(fled_cdev, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return sprintf(buf, "%u\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static DEVICE_ATTR_RW(flash_strobe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static ssize_t flash_timeout_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned long flash_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) mutex_lock(&led_cdev->led_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (led_sysfs_is_disabled(led_cdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ret = kstrtoul(buf, 10, &flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = led_set_flash_timeout(fled_cdev, flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) mutex_unlock(&led_cdev->led_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static ssize_t flash_timeout_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return sprintf(buf, "%u\n", fled_cdev->timeout.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static DEVICE_ATTR_RW(flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static ssize_t max_flash_timeout_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return sprintf(buf, "%u\n", fled_cdev->timeout.max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static DEVICE_ATTR_RO(max_flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static ssize_t flash_fault_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct led_classdev *led_cdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) u32 fault, mask = 0x1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) char *pbuf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int i, ret, buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret = led_get_flash_fault(fled_cdev, &fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) *buf = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) for (i = 0; i < LED_NUM_FLASH_FAULTS; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (fault & mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) buf_len = sprintf(pbuf, "%s ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) led_flash_fault_names[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) pbuf += buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) mask <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return sprintf(buf, "%s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static DEVICE_ATTR_RO(flash_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static struct attribute *led_flash_strobe_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) &dev_attr_flash_strobe.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static struct attribute *led_flash_timeout_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) &dev_attr_flash_timeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) &dev_attr_max_flash_timeout.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static struct attribute *led_flash_brightness_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) &dev_attr_flash_brightness.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) &dev_attr_max_flash_brightness.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static struct attribute *led_flash_fault_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) &dev_attr_flash_fault.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static const struct attribute_group led_flash_strobe_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) .attrs = led_flash_strobe_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static const struct attribute_group led_flash_timeout_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) .attrs = led_flash_timeout_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static const struct attribute_group led_flash_brightness_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .attrs = led_flash_brightness_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static const struct attribute_group led_flash_fault_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .attrs = led_flash_fault_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static void led_flash_resume(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) call_flash_op(fled_cdev, flash_brightness_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) fled_cdev->brightness.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) call_flash_op(fled_cdev, timeout_set, fled_cdev->timeout.val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void led_flash_init_sysfs_groups(struct led_classdev_flash *fled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct led_classdev *led_cdev = &fled_cdev->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) const struct led_flash_ops *ops = fled_cdev->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) const struct attribute_group **flash_groups = fled_cdev->sysfs_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int num_sysfs_groups = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) flash_groups[num_sysfs_groups++] = &led_flash_strobe_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (ops->flash_brightness_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) flash_groups[num_sysfs_groups++] = &led_flash_brightness_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (ops->timeout_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) flash_groups[num_sysfs_groups++] = &led_flash_timeout_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (ops->fault_get)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) flash_groups[num_sysfs_groups++] = &led_flash_fault_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) led_cdev->groups = flash_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int led_classdev_flash_register_ext(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct led_classdev_flash *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct led_init_data *init_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct led_classdev *led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) const struct led_flash_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!fled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) led_cdev = &fled_cdev->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (led_cdev->flags & LED_DEV_CAP_FLASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!led_cdev->brightness_set_blocking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) ops = fled_cdev->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!ops || !ops->strobe_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) led_cdev->flash_resume = led_flash_resume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* Select the sysfs attributes to be created for the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) led_flash_init_sysfs_groups(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /* Register led class device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ret = led_classdev_register_ext(parent, led_cdev, init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) EXPORT_SYMBOL_GPL(led_classdev_flash_register_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!fled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) led_classdev_unregister(&fled_cdev->led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) EXPORT_SYMBOL_GPL(led_classdev_flash_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static void devm_led_classdev_flash_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) led_classdev_flash_unregister(*(struct led_classdev_flash **)res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int devm_led_classdev_flash_register_ext(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct led_classdev_flash *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct led_init_data *init_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct led_classdev_flash **dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) dr = devres_alloc(devm_led_classdev_flash_release, sizeof(*dr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) ret = led_classdev_flash_register_ext(parent, fled_cdev, init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) devres_free(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) *dr = fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) devres_add(parent, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) EXPORT_SYMBOL_GPL(devm_led_classdev_flash_register_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int devm_led_classdev_flash_match(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct led_classdev_flash **p = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (WARN_ON(!p || !*p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return *p == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) void devm_led_classdev_flash_unregister(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct led_classdev_flash *fled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) WARN_ON(devres_release(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) devm_led_classdev_flash_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) devm_led_classdev_flash_match, fled_cdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) EXPORT_SYMBOL_GPL(devm_led_classdev_flash_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static void led_clamp_align(struct led_flash_setting *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) u32 v, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) v = s->val + s->step / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) v = clamp(v, s->min, s->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) offset = v - s->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) offset = s->step * (offset / s->step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) s->val = s->min + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) int led_set_flash_timeout(struct led_classdev_flash *fled_cdev, u32 timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct led_classdev *led_cdev = &fled_cdev->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct led_flash_setting *s = &fled_cdev->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) s->val = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) led_clamp_align(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (!(led_cdev->flags & LED_SUSPENDED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return call_flash_op(fled_cdev, timeout_set, s->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) EXPORT_SYMBOL_GPL(led_set_flash_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int led_get_flash_fault(struct led_classdev_flash *fled_cdev, u32 *fault)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return call_flash_op(fled_cdev, fault_get, fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) EXPORT_SYMBOL_GPL(led_get_flash_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int led_set_flash_brightness(struct led_classdev_flash *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) u32 brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct led_classdev *led_cdev = &fled_cdev->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) struct led_flash_setting *s = &fled_cdev->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) s->val = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) led_clamp_align(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (!(led_cdev->flags & LED_SUSPENDED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return call_flash_op(fled_cdev, flash_brightness_set, s->val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) EXPORT_SYMBOL_GPL(led_set_flash_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int led_update_flash_brightness(struct led_classdev_flash *fled_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) struct led_flash_setting *s = &fled_cdev->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) u32 brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (has_flash_op(fled_cdev, flash_brightness_get)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) int ret = call_flash_op(fled_cdev, flash_brightness_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) &brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) s->val = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) EXPORT_SYMBOL_GPL(led_update_flash_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) MODULE_DESCRIPTION("LED Flash class interface");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) MODULE_LICENSE("GPL v2");