1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2020 Luca Weiss <luca@z3ntu.xyz>
4 #include <linux/gpio/consumer.h>
5 #include <linux/led-class-flash.h>
6 #include <linux/module.h>
7 #include <linux/regulator/consumer.h>
8 #include <linux/platform_device.h>
10 #include <media/v4l2-flash-led-class.h>
12 #define FLASH_TIMEOUT_DEFAULT 250000U /* 250ms */
13 #define FLASH_MAX_TIMEOUT_DEFAULT 300000U /* 300ms */
16 struct led_classdev_flash fled_cdev
;
17 struct v4l2_flash
*v4l2_flash
;
19 struct timer_list powerdown_timer
;
21 struct gpio_desc
*flash_gpio
;
22 struct gpio_desc
*enable_gpio
;
23 struct regulator
*vin_regulator
;
27 /* current timeout in us */
29 /* maximum timeout in us */
33 static struct sgm3140
*flcdev_to_sgm3140(struct led_classdev_flash
*flcdev
)
35 return container_of(flcdev
, struct sgm3140
, fled_cdev
);
38 static int sgm3140_strobe_set(struct led_classdev_flash
*fled_cdev
, bool state
)
40 struct sgm3140
*priv
= flcdev_to_sgm3140(fled_cdev
);
43 if (priv
->enabled
== state
)
47 ret
= regulator_enable(priv
->vin_regulator
);
49 dev_err(fled_cdev
->led_cdev
.dev
,
50 "failed to enable regulator: %d\n", ret
);
53 gpiod_set_value_cansleep(priv
->flash_gpio
, 1);
54 gpiod_set_value_cansleep(priv
->enable_gpio
, 1);
55 mod_timer(&priv
->powerdown_timer
,
56 jiffies
+ usecs_to_jiffies(priv
->timeout
));
58 del_timer_sync(&priv
->powerdown_timer
);
59 gpiod_set_value_cansleep(priv
->enable_gpio
, 0);
60 gpiod_set_value_cansleep(priv
->flash_gpio
, 0);
61 ret
= regulator_disable(priv
->vin_regulator
);
63 dev_err(fled_cdev
->led_cdev
.dev
,
64 "failed to disable regulator: %d\n", ret
);
69 priv
->enabled
= state
;
74 static int sgm3140_strobe_get(struct led_classdev_flash
*fled_cdev
, bool *state
)
76 struct sgm3140
*priv
= flcdev_to_sgm3140(fled_cdev
);
78 *state
= timer_pending(&priv
->powerdown_timer
);
83 static int sgm3140_timeout_set(struct led_classdev_flash
*fled_cdev
,
86 struct sgm3140
*priv
= flcdev_to_sgm3140(fled_cdev
);
88 priv
->timeout
= timeout
;
93 static const struct led_flash_ops sgm3140_flash_ops
= {
94 .strobe_set
= sgm3140_strobe_set
,
95 .strobe_get
= sgm3140_strobe_get
,
96 .timeout_set
= sgm3140_timeout_set
,
99 static int sgm3140_brightness_set(struct led_classdev
*led_cdev
,
100 enum led_brightness brightness
)
102 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
103 struct sgm3140
*priv
= flcdev_to_sgm3140(fled_cdev
);
104 bool enable
= brightness
== LED_ON
;
107 if (priv
->enabled
== enable
)
111 ret
= regulator_enable(priv
->vin_regulator
);
113 dev_err(led_cdev
->dev
,
114 "failed to enable regulator: %d\n", ret
);
117 gpiod_set_value_cansleep(priv
->enable_gpio
, 1);
119 gpiod_set_value_cansleep(priv
->enable_gpio
, 0);
120 ret
= regulator_disable(priv
->vin_regulator
);
122 dev_err(led_cdev
->dev
,
123 "failed to disable regulator: %d\n", ret
);
128 priv
->enabled
= enable
;
133 static void sgm3140_powerdown_timer(struct timer_list
*t
)
135 struct sgm3140
*priv
= from_timer(priv
, t
, powerdown_timer
);
137 gpiod_set_value(priv
->enable_gpio
, 0);
138 gpiod_set_value(priv
->flash_gpio
, 0);
139 regulator_disable(priv
->vin_regulator
);
141 priv
->enabled
= false;
144 static void sgm3140_init_flash_timeout(struct sgm3140
*priv
)
146 struct led_classdev_flash
*fled_cdev
= &priv
->fled_cdev
;
147 struct led_flash_setting
*s
;
149 /* Init flash timeout setting */
150 s
= &fled_cdev
->timeout
;
152 s
->max
= priv
->max_timeout
;
154 s
->val
= FLASH_TIMEOUT_DEFAULT
;
157 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
158 static void sgm3140_init_v4l2_flash_config(struct sgm3140
*priv
,
159 struct v4l2_flash_config
*v4l2_sd_cfg
)
161 struct led_classdev
*led_cdev
= &priv
->fled_cdev
.led_cdev
;
162 struct led_flash_setting
*s
;
164 strscpy(v4l2_sd_cfg
->dev_name
, led_cdev
->dev
->kobj
.name
,
165 sizeof(v4l2_sd_cfg
->dev_name
));
167 /* Init flash intensity setting */
168 s
= &v4l2_sd_cfg
->intensity
;
176 static void sgm3140_init_v4l2_flash_config(struct sgm3140
*priv
,
177 struct v4l2_flash_config
*v4l2_sd_cfg
)
182 static int sgm3140_probe(struct platform_device
*pdev
)
184 struct sgm3140
*priv
;
185 struct led_classdev
*led_cdev
;
186 struct led_classdev_flash
*fled_cdev
;
187 struct led_init_data init_data
= {};
188 struct fwnode_handle
*child_node
;
189 struct v4l2_flash_config v4l2_sd_cfg
= {};
192 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
196 priv
->flash_gpio
= devm_gpiod_get(&pdev
->dev
, "flash", GPIOD_OUT_LOW
);
197 ret
= PTR_ERR_OR_ZERO(priv
->flash_gpio
);
199 return dev_err_probe(&pdev
->dev
, ret
,
200 "Failed to request flash gpio\n");
202 priv
->enable_gpio
= devm_gpiod_get(&pdev
->dev
, "enable", GPIOD_OUT_LOW
);
203 ret
= PTR_ERR_OR_ZERO(priv
->enable_gpio
);
205 return dev_err_probe(&pdev
->dev
, ret
,
206 "Failed to request enable gpio\n");
208 priv
->vin_regulator
= devm_regulator_get(&pdev
->dev
, "vin");
209 ret
= PTR_ERR_OR_ZERO(priv
->vin_regulator
);
211 return dev_err_probe(&pdev
->dev
, ret
,
212 "Failed to request regulator\n");
214 child_node
= fwnode_get_next_available_child_node(pdev
->dev
.fwnode
,
218 "No fwnode child node found for connected LED.\n");
222 ret
= fwnode_property_read_u32(child_node
, "flash-max-timeout-us",
225 priv
->max_timeout
= FLASH_MAX_TIMEOUT_DEFAULT
;
227 "flash-max-timeout-us property missing\n");
231 * Set default timeout to FLASH_DEFAULT_TIMEOUT except if max_timeout
234 priv
->timeout
= min(priv
->max_timeout
, FLASH_TIMEOUT_DEFAULT
);
236 timer_setup(&priv
->powerdown_timer
, sgm3140_powerdown_timer
, 0);
238 fled_cdev
= &priv
->fled_cdev
;
239 led_cdev
= &fled_cdev
->led_cdev
;
241 fled_cdev
->ops
= &sgm3140_flash_ops
;
243 led_cdev
->brightness_set_blocking
= sgm3140_brightness_set
;
244 led_cdev
->max_brightness
= LED_ON
;
245 led_cdev
->flags
|= LED_DEV_CAP_FLASH
;
247 sgm3140_init_flash_timeout(priv
);
249 init_data
.fwnode
= child_node
;
251 platform_set_drvdata(pdev
, priv
);
253 /* Register in the LED subsystem */
254 ret
= devm_led_classdev_flash_register_ext(&pdev
->dev
,
255 fled_cdev
, &init_data
);
257 dev_err(&pdev
->dev
, "Failed to register flash device: %d\n",
262 sgm3140_init_v4l2_flash_config(priv
, &v4l2_sd_cfg
);
264 /* Create V4L2 Flash subdev */
265 priv
->v4l2_flash
= v4l2_flash_init(&pdev
->dev
,
269 if (IS_ERR(priv
->v4l2_flash
)) {
270 ret
= PTR_ERR(priv
->v4l2_flash
);
277 fwnode_handle_put(child_node
);
281 static int sgm3140_remove(struct platform_device
*pdev
)
283 struct sgm3140
*priv
= platform_get_drvdata(pdev
);
285 del_timer_sync(&priv
->powerdown_timer
);
287 v4l2_flash_release(priv
->v4l2_flash
);
292 static const struct of_device_id sgm3140_dt_match
[] = {
293 { .compatible
= "sgmicro,sgm3140" },
296 MODULE_DEVICE_TABLE(of
, sgm3140_dt_match
);
298 static struct platform_driver sgm3140_driver
= {
299 .probe
= sgm3140_probe
,
300 .remove
= sgm3140_remove
,
303 .of_match_table
= sgm3140_dt_match
,
307 module_platform_driver(sgm3140_driver
);
309 MODULE_AUTHOR("Luca Weiss <luca@z3ntu.xyz>");
310 MODULE_DESCRIPTION("SG Micro SGM3140 charge pump LED driver");
311 MODULE_LICENSE("GPL v2");