^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // Copyright (C) 2020 Luca Weiss <luca@z3ntu.xyz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/led-class-flash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <media/v4l2-flash-led-class.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define FLASH_TIMEOUT_DEFAULT 250000U /* 250ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define FLASH_MAX_TIMEOUT_DEFAULT 300000U /* 300ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct sgm3140 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct led_classdev_flash fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct v4l2_flash *v4l2_flash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct timer_list powerdown_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct gpio_desc *flash_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct gpio_desc *enable_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct regulator *vin_regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* current timeout in us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u32 timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* maximum timeout in us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u32 max_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static struct sgm3140 *flcdev_to_sgm3140(struct led_classdev_flash *flcdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return container_of(flcdev, struct sgm3140, fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int sgm3140_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct sgm3140 *priv = flcdev_to_sgm3140(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (priv->enabled == state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ret = regulator_enable(priv->vin_regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) dev_err(fled_cdev->led_cdev.dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) "failed to enable regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) gpiod_set_value_cansleep(priv->flash_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) gpiod_set_value_cansleep(priv->enable_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) mod_timer(&priv->powerdown_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) jiffies + usecs_to_jiffies(priv->timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) del_timer_sync(&priv->powerdown_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) gpiod_set_value_cansleep(priv->enable_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) gpiod_set_value_cansleep(priv->flash_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ret = regulator_disable(priv->vin_regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) dev_err(fled_cdev->led_cdev.dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) "failed to disable regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) priv->enabled = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int sgm3140_strobe_get(struct led_classdev_flash *fled_cdev, bool *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct sgm3140 *priv = flcdev_to_sgm3140(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *state = timer_pending(&priv->powerdown_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int sgm3140_timeout_set(struct led_classdev_flash *fled_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) u32 timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct sgm3140 *priv = flcdev_to_sgm3140(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) priv->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static const struct led_flash_ops sgm3140_flash_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .strobe_set = sgm3140_strobe_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .strobe_get = sgm3140_strobe_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .timeout_set = sgm3140_timeout_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static int sgm3140_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct sgm3140 *priv = flcdev_to_sgm3140(fled_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) bool enable = brightness == LED_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (priv->enabled == enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ret = regulator_enable(priv->vin_regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) dev_err(led_cdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "failed to enable regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) gpiod_set_value_cansleep(priv->enable_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) gpiod_set_value_cansleep(priv->enable_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ret = regulator_disable(priv->vin_regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) dev_err(led_cdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "failed to disable regulator: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) priv->enabled = enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void sgm3140_powerdown_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct sgm3140 *priv = from_timer(priv, t, powerdown_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) gpiod_set_value(priv->enable_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) gpiod_set_value(priv->flash_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) regulator_disable(priv->vin_regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) priv->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void sgm3140_init_flash_timeout(struct sgm3140 *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct led_classdev_flash *fled_cdev = &priv->fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct led_flash_setting *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Init flash timeout setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) s = &fled_cdev->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) s->min = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) s->max = priv->max_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) s->step = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) s->val = FLASH_TIMEOUT_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static void sgm3140_init_v4l2_flash_config(struct sgm3140 *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct v4l2_flash_config *v4l2_sd_cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct led_classdev *led_cdev = &priv->fled_cdev.led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct led_flash_setting *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) strscpy(v4l2_sd_cfg->dev_name, led_cdev->dev->kobj.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) sizeof(v4l2_sd_cfg->dev_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Init flash intensity setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) s = &v4l2_sd_cfg->intensity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) s->min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) s->max = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) s->step = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) s->val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void sgm3140_init_v4l2_flash_config(struct sgm3140 *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct v4l2_flash_config *v4l2_sd_cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int sgm3140_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct sgm3140 *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct led_classdev *led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct led_classdev_flash *fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct fwnode_handle *child_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct v4l2_flash_config v4l2_sd_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) priv->flash_gpio = devm_gpiod_get(&pdev->dev, "flash", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret = PTR_ERR_OR_ZERO(priv->flash_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return dev_err_probe(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) "Failed to request flash gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) priv->enable_gpio = devm_gpiod_get(&pdev->dev, "enable", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ret = PTR_ERR_OR_ZERO(priv->enable_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return dev_err_probe(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) "Failed to request enable gpio\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) priv->vin_regulator = devm_regulator_get(&pdev->dev, "vin");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) ret = PTR_ERR_OR_ZERO(priv->vin_regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return dev_err_probe(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) "Failed to request regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) child_node = fwnode_get_next_available_child_node(pdev->dev.fwnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!child_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) "No fwnode child node found for connected LED.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) ret = fwnode_property_read_u32(child_node, "flash-max-timeout-us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) &priv->max_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) priv->max_timeout = FLASH_MAX_TIMEOUT_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev_warn(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) "flash-max-timeout-us property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * Set default timeout to FLASH_DEFAULT_TIMEOUT except if max_timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * from DT is lower.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) priv->timeout = min(priv->max_timeout, FLASH_TIMEOUT_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) timer_setup(&priv->powerdown_timer, sgm3140_powerdown_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) fled_cdev = &priv->fled_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) led_cdev = &fled_cdev->led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) fled_cdev->ops = &sgm3140_flash_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) led_cdev->brightness_set_blocking = sgm3140_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) led_cdev->max_brightness = LED_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) led_cdev->flags |= LED_DEV_CAP_FLASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) sgm3140_init_flash_timeout(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) init_data.fwnode = child_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) platform_set_drvdata(pdev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* Register in the LED subsystem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) ret = devm_led_classdev_flash_register_ext(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) fled_cdev, &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) dev_err(&pdev->dev, "Failed to register flash device: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) sgm3140_init_v4l2_flash_config(priv, &v4l2_sd_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Create V4L2 Flash subdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) priv->v4l2_flash = v4l2_flash_init(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) child_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) fled_cdev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) &v4l2_sd_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (IS_ERR(priv->v4l2_flash)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ret = PTR_ERR(priv->v4l2_flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) fwnode_handle_put(child_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int sgm3140_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct sgm3140 *priv = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) del_timer_sync(&priv->powerdown_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) v4l2_flash_release(priv->v4l2_flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static const struct of_device_id sgm3140_dt_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) { .compatible = "sgmicro,sgm3140" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) MODULE_DEVICE_TABLE(of, sgm3140_dt_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static struct platform_driver sgm3140_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .probe = sgm3140_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) .remove = sgm3140_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .name = "sgm3140",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) .of_match_table = sgm3140_dt_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) module_platform_driver(sgm3140_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) MODULE_AUTHOR("Luca Weiss <luca@z3ntu.xyz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) MODULE_DESCRIPTION("SG Micro SGM3140 charge pump LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) MODULE_LICENSE("GPL v2");