1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED Flash class driver for the AAT1290
4 * 1.5A Step-Up Current Regulator for Flash LEDs
6 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
7 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
10 #include <linux/cleanup.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/led-class-flash.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <media/v4l2-flash-led-class.h>
23 #define AAT1290_MOVIE_MODE_CURRENT_ADDR 17
24 #define AAT1290_MAX_MM_CURR_PERCENT_0 16
25 #define AAT1290_MAX_MM_CURR_PERCENT_100 1
27 #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18
29 #define AAT1290_MOVIE_MODE_CONFIG_ADDR 19
30 #define AAT1290_MOVIE_MODE_OFF 1
31 #define AAT1290_MOVIE_MODE_ON 3
33 #define AAT1290_MM_CURRENT_RATIO_ADDR 20
34 #define AAT1290_MM_TO_FL_1_92 1
36 #define AAT1290_MM_TO_FL_RATIO 1000 / 1920
37 #define AAT1290_MAX_MM_CURRENT(fl_max) (fl_max * AAT1290_MM_TO_FL_RATIO)
39 #define AAT1290_LATCH_TIME_MIN_US 500
40 #define AAT1290_LATCH_TIME_MAX_US 1000
41 #define AAT1290_EN_SET_TICK_TIME_US 1
42 #define AAT1290_FLEN_OFF_DELAY_TIME_US 10
43 #define AAT1290_FLASH_TM_NUM_LEVELS 16
44 #define AAT1290_MM_CURRENT_SCALE_SIZE 15
46 #define AAT1290_NAME "aat1290"
49 struct aat1290_led_config_data
{
50 /* maximum LED current in movie mode */
52 /* maximum LED current in flash mode */
53 u32 max_flash_current
;
54 /* maximum flash timeout */
56 /* external strobe capability */
57 bool has_external_strobe
;
58 /* max LED brightness level */
59 enum led_brightness max_brightness
;
63 /* platform device data */
64 struct platform_device
*pdev
;
65 /* secures access to the device */
68 /* corresponding LED Flash class device */
69 struct led_classdev_flash fled_cdev
;
70 /* V4L2 Flash device */
71 struct v4l2_flash
*v4l2_flash
;
74 struct gpio_desc
*gpio_fl_en
;
76 struct gpio_desc
*gpio_en_set
;
77 /* movie mode current scale */
78 int *mm_current_scale
;
83 static struct aat1290_led
*fled_cdev_to_led(
84 struct led_classdev_flash
*fled_cdev
)
86 return container_of(fled_cdev
, struct aat1290_led
, fled_cdev
);
89 static struct led_classdev_flash
*led_cdev_to_fled_cdev(
90 struct led_classdev
*led_cdev
)
92 return container_of(led_cdev
, struct led_classdev_flash
, led_cdev
);
95 static void aat1290_as2cwire_write(struct aat1290_led
*led
, int addr
, int value
)
99 gpiod_direction_output(led
->gpio_fl_en
, 0);
100 gpiod_direction_output(led
->gpio_en_set
, 0);
102 udelay(AAT1290_FLEN_OFF_DELAY_TIME_US
);
105 for (i
= 0; i
< addr
; ++i
) {
106 udelay(AAT1290_EN_SET_TICK_TIME_US
);
107 gpiod_direction_output(led
->gpio_en_set
, 0);
108 udelay(AAT1290_EN_SET_TICK_TIME_US
);
109 gpiod_direction_output(led
->gpio_en_set
, 1);
112 usleep_range(AAT1290_LATCH_TIME_MIN_US
, AAT1290_LATCH_TIME_MAX_US
);
115 for (i
= 0; i
< value
; ++i
) {
116 udelay(AAT1290_EN_SET_TICK_TIME_US
);
117 gpiod_direction_output(led
->gpio_en_set
, 0);
118 udelay(AAT1290_EN_SET_TICK_TIME_US
);
119 gpiod_direction_output(led
->gpio_en_set
, 1);
122 usleep_range(AAT1290_LATCH_TIME_MIN_US
, AAT1290_LATCH_TIME_MAX_US
);
125 static void aat1290_set_flash_safety_timer(struct aat1290_led
*led
,
126 unsigned int micro_sec
)
128 struct led_classdev_flash
*fled_cdev
= &led
->fled_cdev
;
129 struct led_flash_setting
*flash_tm
= &fled_cdev
->timeout
;
130 int flash_tm_reg
= AAT1290_FLASH_TM_NUM_LEVELS
-
131 (micro_sec
/ flash_tm
->step
) + 1;
133 aat1290_as2cwire_write(led
, AAT1290_FLASH_SAFETY_TIMER_ADDR
,
137 /* LED subsystem callbacks */
139 static int aat1290_led_brightness_set(struct led_classdev
*led_cdev
,
140 enum led_brightness brightness
)
142 struct led_classdev_flash
*fled_cdev
= led_cdev_to_fled_cdev(led_cdev
);
143 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
145 mutex_lock(&led
->lock
);
147 if (brightness
== 0) {
148 gpiod_direction_output(led
->gpio_fl_en
, 0);
149 gpiod_direction_output(led
->gpio_en_set
, 0);
150 led
->movie_mode
= false;
152 if (!led
->movie_mode
) {
153 aat1290_as2cwire_write(led
,
154 AAT1290_MM_CURRENT_RATIO_ADDR
,
155 AAT1290_MM_TO_FL_1_92
);
156 led
->movie_mode
= true;
159 aat1290_as2cwire_write(led
, AAT1290_MOVIE_MODE_CURRENT_ADDR
,
160 AAT1290_MAX_MM_CURR_PERCENT_0
- brightness
);
161 aat1290_as2cwire_write(led
, AAT1290_MOVIE_MODE_CONFIG_ADDR
,
162 AAT1290_MOVIE_MODE_ON
);
165 mutex_unlock(&led
->lock
);
170 static int aat1290_led_flash_strobe_set(struct led_classdev_flash
*fled_cdev
,
174 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
175 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
176 struct led_flash_setting
*timeout
= &fled_cdev
->timeout
;
178 mutex_lock(&led
->lock
);
181 aat1290_set_flash_safety_timer(led
, timeout
->val
);
182 gpiod_direction_output(led
->gpio_fl_en
, 1);
184 gpiod_direction_output(led
->gpio_fl_en
, 0);
185 gpiod_direction_output(led
->gpio_en_set
, 0);
189 * To reenter movie mode after a flash event the part must be cycled
190 * off and back on to reset the movie mode and reprogrammed via the
191 * AS2Cwire. Therefore the brightness and movie_mode properties needs
192 * to be updated here to reflect the actual state.
194 led_cdev
->brightness
= 0;
195 led
->movie_mode
= false;
197 mutex_unlock(&led
->lock
);
202 static int aat1290_led_flash_timeout_set(struct led_classdev_flash
*fled_cdev
,
206 * Don't do anything - flash timeout is cached in the led-class-flash
207 * core and will be applied in the strobe_set op, as writing the
208 * safety timer register spuriously turns the torch mode on.
214 static int aat1290_led_parse_dt(struct aat1290_led
*led
,
215 struct aat1290_led_config_data
*cfg
,
216 struct device_node
**sub_node
)
218 struct device
*dev
= &led
->pdev
->dev
;
219 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
220 struct pinctrl
*pinctrl
;
224 led
->gpio_fl_en
= devm_gpiod_get(dev
, "flen", GPIOD_ASIS
);
225 if (IS_ERR(led
->gpio_fl_en
)) {
226 ret
= PTR_ERR(led
->gpio_fl_en
);
227 dev_err(dev
, "Unable to claim gpio \"flen\".\n");
231 led
->gpio_en_set
= devm_gpiod_get(dev
, "enset", GPIOD_ASIS
);
232 if (IS_ERR(led
->gpio_en_set
)) {
233 ret
= PTR_ERR(led
->gpio_en_set
);
234 dev_err(dev
, "Unable to claim gpio \"enset\".\n");
238 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
239 pinctrl
= devm_pinctrl_get_select_default(&led
->pdev
->dev
);
240 if (IS_ERR(pinctrl
)) {
241 cfg
->has_external_strobe
= false;
243 "No support for external strobe detected.\n");
245 cfg
->has_external_strobe
= true;
249 struct device_node
*child_node
__free(device_node
) =
250 of_get_next_available_child(dev_of_node(dev
), NULL
);
252 dev_err(dev
, "No DT child node found for connected LED.\n");
256 ret
= of_property_read_u32(child_node
, "led-max-microamp",
257 &cfg
->max_mm_current
);
259 * led-max-microamp will default to 1/20 of flash-max-microamp
260 * in case it is missing.
264 "led-max-microamp DT property missing\n");
266 ret
= of_property_read_u32(child_node
, "flash-max-microamp",
267 &cfg
->max_flash_current
);
270 "flash-max-microamp DT property missing\n");
274 ret
= of_property_read_u32(child_node
, "flash-max-timeout-us",
278 "flash-max-timeout-us DT property missing\n");
282 *sub_node
= child_node
;
287 static void aat1290_led_validate_mm_current(struct aat1290_led
*led
,
288 struct aat1290_led_config_data
*cfg
)
290 int i
, b
= 0, e
= AAT1290_MM_CURRENT_SCALE_SIZE
;
294 if (cfg
->max_mm_current
< led
->mm_current_scale
[i
])
300 cfg
->max_mm_current
= led
->mm_current_scale
[b
];
301 cfg
->max_brightness
= b
+ 1;
304 static int init_mm_current_scale(struct aat1290_led
*led
,
305 struct aat1290_led_config_data
*cfg
)
307 static const int max_mm_current_percent
[] = {
308 20, 22, 25, 28, 32, 36, 40, 45, 50, 56,
311 int i
, max_mm_current
=
312 AAT1290_MAX_MM_CURRENT(cfg
->max_flash_current
);
314 led
->mm_current_scale
= devm_kzalloc(&led
->pdev
->dev
,
315 sizeof(max_mm_current_percent
),
317 if (!led
->mm_current_scale
)
320 for (i
= 0; i
< AAT1290_MM_CURRENT_SCALE_SIZE
; ++i
)
321 led
->mm_current_scale
[i
] = max_mm_current
*
322 max_mm_current_percent
[i
] / 100;
327 static int aat1290_led_get_configuration(struct aat1290_led
*led
,
328 struct aat1290_led_config_data
*cfg
,
329 struct device_node
**sub_node
)
333 ret
= aat1290_led_parse_dt(led
, cfg
, sub_node
);
337 * Init non-linear movie mode current scale basing
338 * on the max flash current from led configuration.
340 ret
= init_mm_current_scale(led
, cfg
);
344 aat1290_led_validate_mm_current(led
, cfg
);
346 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
348 devm_kfree(&led
->pdev
->dev
, led
->mm_current_scale
);
354 static void aat1290_init_flash_timeout(struct aat1290_led
*led
,
355 struct aat1290_led_config_data
*cfg
)
357 struct led_classdev_flash
*fled_cdev
= &led
->fled_cdev
;
358 struct led_flash_setting
*setting
;
360 /* Init flash timeout setting */
361 setting
= &fled_cdev
->timeout
;
362 setting
->min
= cfg
->max_flash_tm
/ AAT1290_FLASH_TM_NUM_LEVELS
;
363 setting
->max
= cfg
->max_flash_tm
;
364 setting
->step
= setting
->min
;
365 setting
->val
= setting
->max
;
368 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
369 static enum led_brightness
aat1290_intensity_to_brightness(
370 struct v4l2_flash
*v4l2_flash
,
373 struct led_classdev_flash
*fled_cdev
= v4l2_flash
->fled_cdev
;
374 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
377 for (i
= AAT1290_MM_CURRENT_SCALE_SIZE
- 1; i
>= 0; --i
)
378 if (intensity
>= led
->mm_current_scale
[i
])
384 static s32
aat1290_brightness_to_intensity(struct v4l2_flash
*v4l2_flash
,
385 enum led_brightness brightness
)
387 struct led_classdev_flash
*fled_cdev
= v4l2_flash
->fled_cdev
;
388 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
390 return led
->mm_current_scale
[brightness
- 1];
393 static int aat1290_led_external_strobe_set(struct v4l2_flash
*v4l2_flash
,
396 struct aat1290_led
*led
= fled_cdev_to_led(v4l2_flash
->fled_cdev
);
397 struct led_classdev_flash
*fled_cdev
= v4l2_flash
->fled_cdev
;
398 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
399 struct pinctrl
*pinctrl
;
401 gpiod_direction_output(led
->gpio_fl_en
, 0);
402 gpiod_direction_output(led
->gpio_en_set
, 0);
404 led
->movie_mode
= false;
405 led_cdev
->brightness
= 0;
407 pinctrl
= devm_pinctrl_get_select(&led
->pdev
->dev
,
408 enable
? "isp" : "host");
409 if (IS_ERR(pinctrl
)) {
410 dev_warn(&led
->pdev
->dev
, "Unable to switch strobe source.\n");
411 return PTR_ERR(pinctrl
);
417 static void aat1290_init_v4l2_flash_config(struct aat1290_led
*led
,
418 struct aat1290_led_config_data
*led_cfg
,
419 struct v4l2_flash_config
*v4l2_sd_cfg
)
421 struct led_classdev
*led_cdev
= &led
->fled_cdev
.led_cdev
;
422 struct led_flash_setting
*s
;
424 strscpy(v4l2_sd_cfg
->dev_name
, led_cdev
->dev
->kobj
.name
,
425 sizeof(v4l2_sd_cfg
->dev_name
));
427 s
= &v4l2_sd_cfg
->intensity
;
428 s
->min
= led
->mm_current_scale
[0];
429 s
->max
= led_cfg
->max_mm_current
;
433 v4l2_sd_cfg
->has_external_strobe
= led_cfg
->has_external_strobe
;
436 static const struct v4l2_flash_ops v4l2_flash_ops
= {
437 .external_strobe_set
= aat1290_led_external_strobe_set
,
438 .intensity_to_led_brightness
= aat1290_intensity_to_brightness
,
439 .led_brightness_to_intensity
= aat1290_brightness_to_intensity
,
442 static inline void aat1290_init_v4l2_flash_config(struct aat1290_led
*led
,
443 struct aat1290_led_config_data
*led_cfg
,
444 struct v4l2_flash_config
*v4l2_sd_cfg
)
447 static const struct v4l2_flash_ops v4l2_flash_ops
;
450 static const struct led_flash_ops flash_ops
= {
451 .strobe_set
= aat1290_led_flash_strobe_set
,
452 .timeout_set
= aat1290_led_flash_timeout_set
,
455 static int aat1290_led_probe(struct platform_device
*pdev
)
457 struct device
*dev
= &pdev
->dev
;
458 struct device_node
*sub_node
= NULL
;
459 struct aat1290_led
*led
;
460 struct led_classdev
*led_cdev
;
461 struct led_classdev_flash
*fled_cdev
;
462 struct led_init_data init_data
= {};
463 struct aat1290_led_config_data led_cfg
= {};
464 struct v4l2_flash_config v4l2_sd_cfg
= {};
467 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
472 platform_set_drvdata(pdev
, led
);
474 fled_cdev
= &led
->fled_cdev
;
475 fled_cdev
->ops
= &flash_ops
;
476 led_cdev
= &fled_cdev
->led_cdev
;
478 ret
= aat1290_led_get_configuration(led
, &led_cfg
, &sub_node
);
482 mutex_init(&led
->lock
);
484 /* Initialize LED Flash class device */
485 led_cdev
->brightness_set_blocking
= aat1290_led_brightness_set
;
486 led_cdev
->max_brightness
= led_cfg
.max_brightness
;
487 led_cdev
->flags
|= LED_DEV_CAP_FLASH
;
489 aat1290_init_flash_timeout(led
, &led_cfg
);
491 init_data
.fwnode
= of_fwnode_handle(sub_node
);
492 init_data
.devicename
= AAT1290_NAME
;
494 /* Register LED Flash class device */
495 ret
= led_classdev_flash_register_ext(&pdev
->dev
, fled_cdev
,
498 goto err_flash_register
;
500 aat1290_init_v4l2_flash_config(led
, &led_cfg
, &v4l2_sd_cfg
);
502 /* Create V4L2 Flash subdev. */
503 led
->v4l2_flash
= v4l2_flash_init(dev
, of_fwnode_handle(sub_node
),
504 fled_cdev
, &v4l2_flash_ops
,
506 if (IS_ERR(led
->v4l2_flash
)) {
507 ret
= PTR_ERR(led
->v4l2_flash
);
508 goto error_v4l2_flash_init
;
513 error_v4l2_flash_init
:
514 led_classdev_flash_unregister(fled_cdev
);
516 mutex_destroy(&led
->lock
);
521 static void aat1290_led_remove(struct platform_device
*pdev
)
523 struct aat1290_led
*led
= platform_get_drvdata(pdev
);
525 v4l2_flash_release(led
->v4l2_flash
);
526 led_classdev_flash_unregister(&led
->fled_cdev
);
528 mutex_destroy(&led
->lock
);
531 static const struct of_device_id aat1290_led_dt_match
[] = {
532 { .compatible
= "skyworks,aat1290" },
535 MODULE_DEVICE_TABLE(of
, aat1290_led_dt_match
);
537 static struct platform_driver aat1290_led_driver
= {
538 .probe
= aat1290_led_probe
,
539 .remove
= aat1290_led_remove
,
542 .of_match_table
= aat1290_led_dt_match
,
546 module_platform_driver(aat1290_led_driver
);
548 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
549 MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs");
550 MODULE_LICENSE("GPL v2");