2 * LED Flash class driver for the AAT1290
3 * 1.5A Step-Up Current Regulator for Flash LEDs
5 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
6 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/led-class-flash.h>
16 #include <linux/leds.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <media/v4l2-flash-led-class.h>
26 #define AAT1290_MOVIE_MODE_CURRENT_ADDR 17
27 #define AAT1290_MAX_MM_CURR_PERCENT_0 16
28 #define AAT1290_MAX_MM_CURR_PERCENT_100 1
30 #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18
32 #define AAT1290_MOVIE_MODE_CONFIG_ADDR 19
33 #define AAT1290_MOVIE_MODE_OFF 1
34 #define AAT1290_MOVIE_MODE_ON 3
36 #define AAT1290_MM_CURRENT_RATIO_ADDR 20
37 #define AAT1290_MM_TO_FL_1_92 1
39 #define AAT1290_MM_TO_FL_RATIO 1000 / 1920
40 #define AAT1290_MAX_MM_CURRENT(fl_max) (fl_max * AAT1290_MM_TO_FL_RATIO)
42 #define AAT1290_LATCH_TIME_MIN_US 500
43 #define AAT1290_LATCH_TIME_MAX_US 1000
44 #define AAT1290_EN_SET_TICK_TIME_US 1
45 #define AAT1290_FLEN_OFF_DELAY_TIME_US 10
46 #define AAT1290_FLASH_TM_NUM_LEVELS 16
47 #define AAT1290_MM_CURRENT_SCALE_SIZE 15
50 struct aat1290_led_config_data
{
51 /* maximum LED current in movie mode */
53 /* maximum LED current in flash mode */
54 u32 max_flash_current
;
55 /* maximum flash timeout */
57 /* external strobe capability */
58 bool has_external_strobe
;
59 /* max LED brightness level */
60 enum led_brightness max_brightness
;
64 /* platform device data */
65 struct platform_device
*pdev
;
66 /* secures access to the device */
69 /* corresponding LED Flash class device */
70 struct led_classdev_flash fled_cdev
;
71 /* V4L2 Flash device */
72 struct v4l2_flash
*v4l2_flash
;
75 struct gpio_desc
*gpio_fl_en
;
77 struct gpio_desc
*gpio_en_set
;
78 /* movie mode current scale */
79 int *mm_current_scale
;
83 /* brightness cache */
84 unsigned int torch_brightness
;
85 /* assures led-triggers compatibility */
86 struct work_struct work_brightness_set
;
89 static struct aat1290_led
*fled_cdev_to_led(
90 struct led_classdev_flash
*fled_cdev
)
92 return container_of(fled_cdev
, struct aat1290_led
, fled_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 static void aat1290_brightness_set(struct aat1290_led
*led
,
138 enum led_brightness brightness
)
140 mutex_lock(&led
->lock
);
142 if (brightness
== 0) {
143 gpiod_direction_output(led
->gpio_fl_en
, 0);
144 gpiod_direction_output(led
->gpio_en_set
, 0);
145 led
->movie_mode
= false;
147 if (!led
->movie_mode
) {
148 aat1290_as2cwire_write(led
,
149 AAT1290_MM_CURRENT_RATIO_ADDR
,
150 AAT1290_MM_TO_FL_1_92
);
151 led
->movie_mode
= true;
154 aat1290_as2cwire_write(led
, AAT1290_MOVIE_MODE_CURRENT_ADDR
,
155 AAT1290_MAX_MM_CURR_PERCENT_0
- brightness
);
156 aat1290_as2cwire_write(led
, AAT1290_MOVIE_MODE_CONFIG_ADDR
,
157 AAT1290_MOVIE_MODE_ON
);
160 mutex_unlock(&led
->lock
);
163 /* LED subsystem callbacks */
165 static void aat1290_brightness_set_work(struct work_struct
*work
)
167 struct aat1290_led
*led
=
168 container_of(work
, struct aat1290_led
, work_brightness_set
);
170 aat1290_brightness_set(led
, led
->torch_brightness
);
173 static void aat1290_led_brightness_set(struct led_classdev
*led_cdev
,
174 enum led_brightness brightness
)
176 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
177 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
179 led
->torch_brightness
= brightness
;
180 schedule_work(&led
->work_brightness_set
);
183 static int aat1290_led_brightness_set_sync(struct led_classdev
*led_cdev
,
184 enum led_brightness brightness
)
186 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
187 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
189 aat1290_brightness_set(led
, brightness
);
194 static int aat1290_led_flash_strobe_set(struct led_classdev_flash
*fled_cdev
,
198 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
199 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
200 struct led_flash_setting
*timeout
= &fled_cdev
->timeout
;
202 mutex_lock(&led
->lock
);
205 aat1290_set_flash_safety_timer(led
, timeout
->val
);
206 gpiod_direction_output(led
->gpio_fl_en
, 1);
208 gpiod_direction_output(led
->gpio_fl_en
, 0);
209 gpiod_direction_output(led
->gpio_en_set
, 0);
213 * To reenter movie mode after a flash event the part must be cycled
214 * off and back on to reset the movie mode and reprogrammed via the
215 * AS2Cwire. Therefore the brightness and movie_mode properties needs
216 * to be updated here to reflect the actual state.
218 led_cdev
->brightness
= 0;
219 led
->movie_mode
= false;
221 mutex_unlock(&led
->lock
);
226 static int aat1290_led_flash_timeout_set(struct led_classdev_flash
*fled_cdev
,
230 * Don't do anything - flash timeout is cached in the led-class-flash
231 * core and will be applied in the strobe_set op, as writing the
232 * safety timer register spuriously turns the torch mode on.
238 static int aat1290_led_parse_dt(struct aat1290_led
*led
,
239 struct aat1290_led_config_data
*cfg
,
240 struct device_node
**sub_node
)
242 struct led_classdev
*led_cdev
= &led
->fled_cdev
.led_cdev
;
243 struct device
*dev
= &led
->pdev
->dev
;
244 struct device_node
*child_node
;
245 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
246 struct pinctrl
*pinctrl
;
250 led
->gpio_fl_en
= devm_gpiod_get(dev
, "flen", GPIOD_ASIS
);
251 if (IS_ERR(led
->gpio_fl_en
)) {
252 ret
= PTR_ERR(led
->gpio_fl_en
);
253 dev_err(dev
, "Unable to claim gpio \"flen\".\n");
257 led
->gpio_en_set
= devm_gpiod_get(dev
, "enset", GPIOD_ASIS
);
258 if (IS_ERR(led
->gpio_en_set
)) {
259 ret
= PTR_ERR(led
->gpio_en_set
);
260 dev_err(dev
, "Unable to claim gpio \"enset\".\n");
264 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
265 pinctrl
= devm_pinctrl_get_select_default(&led
->pdev
->dev
);
266 if (IS_ERR(pinctrl
)) {
267 cfg
->has_external_strobe
= false;
269 "No support for external strobe detected.\n");
271 cfg
->has_external_strobe
= true;
275 child_node
= of_get_next_available_child(dev
->of_node
, NULL
);
277 dev_err(dev
, "No DT child node found for connected LED.\n");
281 led_cdev
->name
= of_get_property(child_node
, "label", NULL
) ? :
284 ret
= of_property_read_u32(child_node
, "led-max-microamp",
285 &cfg
->max_mm_current
);
287 * led-max-microamp will default to 1/20 of flash-max-microamp
288 * in case it is missing.
292 "led-max-microamp DT property missing\n");
294 ret
= of_property_read_u32(child_node
, "flash-max-microamp",
295 &cfg
->max_flash_current
);
298 "flash-max-microamp DT property missing\n");
302 ret
= of_property_read_u32(child_node
, "flash-max-timeout-us",
306 "flash-max-timeout-us DT property missing\n");
310 of_node_put(child_node
);
312 *sub_node
= child_node
;
317 static void aat1290_led_validate_mm_current(struct aat1290_led
*led
,
318 struct aat1290_led_config_data
*cfg
)
320 int i
, b
= 0, e
= AAT1290_MM_CURRENT_SCALE_SIZE
;
324 if (cfg
->max_mm_current
< led
->mm_current_scale
[i
])
330 cfg
->max_mm_current
= led
->mm_current_scale
[b
];
331 cfg
->max_brightness
= b
+ 1;
334 static int init_mm_current_scale(struct aat1290_led
*led
,
335 struct aat1290_led_config_data
*cfg
)
337 int max_mm_current_percent
[] = { 20, 22, 25, 28, 32, 36, 40, 45, 50, 56,
338 63, 71, 79, 89, 100 };
339 int i
, max_mm_current
=
340 AAT1290_MAX_MM_CURRENT(cfg
->max_flash_current
);
342 led
->mm_current_scale
= devm_kzalloc(&led
->pdev
->dev
,
343 sizeof(max_mm_current_percent
),
345 if (!led
->mm_current_scale
)
348 for (i
= 0; i
< AAT1290_MM_CURRENT_SCALE_SIZE
; ++i
)
349 led
->mm_current_scale
[i
] = max_mm_current
*
350 max_mm_current_percent
[i
] / 100;
355 static int aat1290_led_get_configuration(struct aat1290_led
*led
,
356 struct aat1290_led_config_data
*cfg
,
357 struct device_node
**sub_node
)
361 ret
= aat1290_led_parse_dt(led
, cfg
, sub_node
);
365 * Init non-linear movie mode current scale basing
366 * on the max flash current from led configuration.
368 ret
= init_mm_current_scale(led
, cfg
);
372 aat1290_led_validate_mm_current(led
, cfg
);
374 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
376 devm_kfree(&led
->pdev
->dev
, led
->mm_current_scale
);
382 static void aat1290_init_flash_timeout(struct aat1290_led
*led
,
383 struct aat1290_led_config_data
*cfg
)
385 struct led_classdev_flash
*fled_cdev
= &led
->fled_cdev
;
386 struct led_flash_setting
*setting
;
388 /* Init flash timeout setting */
389 setting
= &fled_cdev
->timeout
;
390 setting
->min
= cfg
->max_flash_tm
/ AAT1290_FLASH_TM_NUM_LEVELS
;
391 setting
->max
= cfg
->max_flash_tm
;
392 setting
->step
= setting
->min
;
393 setting
->val
= setting
->max
;
396 #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
397 static enum led_brightness
aat1290_intensity_to_brightness(
398 struct v4l2_flash
*v4l2_flash
,
401 struct led_classdev_flash
*fled_cdev
= v4l2_flash
->fled_cdev
;
402 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
405 for (i
= AAT1290_MM_CURRENT_SCALE_SIZE
- 1; i
>= 0; --i
)
406 if (intensity
>= led
->mm_current_scale
[i
])
412 static s32
aat1290_brightness_to_intensity(struct v4l2_flash
*v4l2_flash
,
413 enum led_brightness brightness
)
415 struct led_classdev_flash
*fled_cdev
= v4l2_flash
->fled_cdev
;
416 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
418 return led
->mm_current_scale
[brightness
- 1];
421 static int aat1290_led_external_strobe_set(struct v4l2_flash
*v4l2_flash
,
424 struct aat1290_led
*led
= fled_cdev_to_led(v4l2_flash
->fled_cdev
);
425 struct led_classdev_flash
*fled_cdev
= v4l2_flash
->fled_cdev
;
426 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
427 struct pinctrl
*pinctrl
;
429 gpiod_direction_output(led
->gpio_fl_en
, 0);
430 gpiod_direction_output(led
->gpio_en_set
, 0);
432 led
->movie_mode
= false;
433 led_cdev
->brightness
= 0;
435 pinctrl
= devm_pinctrl_get_select(&led
->pdev
->dev
,
436 enable
? "isp" : "host");
437 if (IS_ERR(pinctrl
)) {
438 dev_warn(&led
->pdev
->dev
, "Unable to switch strobe source.\n");
439 return PTR_ERR(pinctrl
);
445 static void aat1290_init_v4l2_flash_config(struct aat1290_led
*led
,
446 struct aat1290_led_config_data
*led_cfg
,
447 struct v4l2_flash_config
*v4l2_sd_cfg
)
449 struct led_classdev
*led_cdev
= &led
->fled_cdev
.led_cdev
;
450 struct led_flash_setting
*s
;
452 strlcpy(v4l2_sd_cfg
->dev_name
, led_cdev
->name
,
453 sizeof(v4l2_sd_cfg
->dev_name
));
455 s
= &v4l2_sd_cfg
->torch_intensity
;
456 s
->min
= led
->mm_current_scale
[0];
457 s
->max
= led_cfg
->max_mm_current
;
461 v4l2_sd_cfg
->has_external_strobe
= led_cfg
->has_external_strobe
;
464 static const struct v4l2_flash_ops v4l2_flash_ops
= {
465 .external_strobe_set
= aat1290_led_external_strobe_set
,
466 .intensity_to_led_brightness
= aat1290_intensity_to_brightness
,
467 .led_brightness_to_intensity
= aat1290_brightness_to_intensity
,
470 static inline void aat1290_init_v4l2_flash_config(struct aat1290_led
*led
,
471 struct aat1290_led_config_data
*led_cfg
,
472 struct v4l2_flash_config
*v4l2_sd_cfg
)
475 static const struct v4l2_flash_ops v4l2_flash_ops
;
478 static const struct led_flash_ops flash_ops
= {
479 .strobe_set
= aat1290_led_flash_strobe_set
,
480 .timeout_set
= aat1290_led_flash_timeout_set
,
483 static int aat1290_led_probe(struct platform_device
*pdev
)
485 struct device
*dev
= &pdev
->dev
;
486 struct device_node
*sub_node
= NULL
;
487 struct aat1290_led
*led
;
488 struct led_classdev
*led_cdev
;
489 struct led_classdev_flash
*fled_cdev
;
490 struct aat1290_led_config_data led_cfg
= {};
491 struct v4l2_flash_config v4l2_sd_cfg
= {};
494 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
499 platform_set_drvdata(pdev
, led
);
501 fled_cdev
= &led
->fled_cdev
;
502 fled_cdev
->ops
= &flash_ops
;
503 led_cdev
= &fled_cdev
->led_cdev
;
505 ret
= aat1290_led_get_configuration(led
, &led_cfg
, &sub_node
);
509 mutex_init(&led
->lock
);
511 /* Initialize LED Flash class device */
512 led_cdev
->brightness_set
= aat1290_led_brightness_set
;
513 led_cdev
->brightness_set_sync
= aat1290_led_brightness_set_sync
;
514 led_cdev
->max_brightness
= led_cfg
.max_brightness
;
515 led_cdev
->flags
|= LED_DEV_CAP_FLASH
;
516 INIT_WORK(&led
->work_brightness_set
, aat1290_brightness_set_work
);
518 aat1290_init_flash_timeout(led
, &led_cfg
);
520 /* Register LED Flash class device */
521 ret
= led_classdev_flash_register(&pdev
->dev
, fled_cdev
);
523 goto err_flash_register
;
525 aat1290_init_v4l2_flash_config(led
, &led_cfg
, &v4l2_sd_cfg
);
527 /* Create V4L2 Flash subdev. */
528 led
->v4l2_flash
= v4l2_flash_init(dev
, sub_node
, fled_cdev
, NULL
,
529 &v4l2_flash_ops
, &v4l2_sd_cfg
);
530 if (IS_ERR(led
->v4l2_flash
)) {
531 ret
= PTR_ERR(led
->v4l2_flash
);
532 goto error_v4l2_flash_init
;
537 error_v4l2_flash_init
:
538 led_classdev_flash_unregister(fled_cdev
);
540 mutex_destroy(&led
->lock
);
545 static int aat1290_led_remove(struct platform_device
*pdev
)
547 struct aat1290_led
*led
= platform_get_drvdata(pdev
);
549 v4l2_flash_release(led
->v4l2_flash
);
550 led_classdev_flash_unregister(&led
->fled_cdev
);
551 cancel_work_sync(&led
->work_brightness_set
);
553 mutex_destroy(&led
->lock
);
558 static const struct of_device_id aat1290_led_dt_match
[] = {
559 { .compatible
= "skyworks,aat1290" },
562 MODULE_DEVICE_TABLE(of
, aat1290_led_dt_match
);
564 static struct platform_driver aat1290_led_driver
= {
565 .probe
= aat1290_led_probe
,
566 .remove
= aat1290_led_remove
,
569 .of_match_table
= aat1290_led_dt_match
,
573 module_platform_driver(aat1290_led_driver
);
575 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
576 MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs");
577 MODULE_LICENSE("GPL v2");