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/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
24 #define AAT1290_MOVIE_MODE_CURRENT_ADDR 17
25 #define AAT1290_MAX_MM_CURR_PERCENT_0 16
26 #define AAT1290_MAX_MM_CURR_PERCENT_100 1
28 #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18
30 #define AAT1290_MOVIE_MODE_CONFIG_ADDR 19
31 #define AAT1290_MOVIE_MODE_OFF 1
32 #define AAT1290_MOVIE_MODE_ON 3
34 #define AAT1290_MM_CURRENT_RATIO_ADDR 20
35 #define AAT1290_MM_TO_FL_1_92 1
37 #define AAT1290_MM_TO_FL_RATIO 1000 / 1920
38 #define AAT1290_MAX_MM_CURRENT(fl_max) (fl_max * AAT1290_MM_TO_FL_RATIO)
40 #define AAT1290_LATCH_TIME_MIN_US 500
41 #define AAT1290_LATCH_TIME_MAX_US 1000
42 #define AAT1290_EN_SET_TICK_TIME_US 1
43 #define AAT1290_FLEN_OFF_DELAY_TIME_US 10
44 #define AAT1290_FLASH_TM_NUM_LEVELS 16
45 #define AAT1290_MM_CURRENT_SCALE_SIZE 15
48 struct aat1290_led_config_data
{
49 /* maximum LED current in movie mode */
51 /* maximum LED current in flash mode */
52 u32 max_flash_current
;
53 /* maximum flash timeout */
55 /* max LED brightness level */
56 enum led_brightness max_brightness
;
60 /* platform device data */
61 struct platform_device
*pdev
;
62 /* secures access to the device */
65 /* corresponding LED Flash class device */
66 struct led_classdev_flash fled_cdev
;
69 struct gpio_desc
*gpio_fl_en
;
71 struct gpio_desc
*gpio_en_set
;
72 /* movie mode current scale */
73 int *mm_current_scale
;
77 /* brightness cache */
78 unsigned int torch_brightness
;
79 /* assures led-triggers compatibility */
80 struct work_struct work_brightness_set
;
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 void aat1290_as2cwire_write(struct aat1290_led
*led
, int addr
, int value
)
93 gpiod_direction_output(led
->gpio_fl_en
, 0);
94 gpiod_direction_output(led
->gpio_en_set
, 0);
96 udelay(AAT1290_FLEN_OFF_DELAY_TIME_US
);
99 for (i
= 0; i
< addr
; ++i
) {
100 udelay(AAT1290_EN_SET_TICK_TIME_US
);
101 gpiod_direction_output(led
->gpio_en_set
, 0);
102 udelay(AAT1290_EN_SET_TICK_TIME_US
);
103 gpiod_direction_output(led
->gpio_en_set
, 1);
106 usleep_range(AAT1290_LATCH_TIME_MIN_US
, AAT1290_LATCH_TIME_MAX_US
);
109 for (i
= 0; i
< value
; ++i
) {
110 udelay(AAT1290_EN_SET_TICK_TIME_US
);
111 gpiod_direction_output(led
->gpio_en_set
, 0);
112 udelay(AAT1290_EN_SET_TICK_TIME_US
);
113 gpiod_direction_output(led
->gpio_en_set
, 1);
116 usleep_range(AAT1290_LATCH_TIME_MIN_US
, AAT1290_LATCH_TIME_MAX_US
);
119 static void aat1290_set_flash_safety_timer(struct aat1290_led
*led
,
120 unsigned int micro_sec
)
122 struct led_classdev_flash
*fled_cdev
= &led
->fled_cdev
;
123 struct led_flash_setting
*flash_tm
= &fled_cdev
->timeout
;
124 int flash_tm_reg
= AAT1290_FLASH_TM_NUM_LEVELS
-
125 (micro_sec
/ flash_tm
->step
) + 1;
127 aat1290_as2cwire_write(led
, AAT1290_FLASH_SAFETY_TIMER_ADDR
,
131 static void aat1290_brightness_set(struct aat1290_led
*led
,
132 enum led_brightness brightness
)
134 mutex_lock(&led
->lock
);
136 if (brightness
== 0) {
137 gpiod_direction_output(led
->gpio_fl_en
, 0);
138 gpiod_direction_output(led
->gpio_en_set
, 0);
139 led
->movie_mode
= false;
141 if (!led
->movie_mode
) {
142 aat1290_as2cwire_write(led
,
143 AAT1290_MM_CURRENT_RATIO_ADDR
,
144 AAT1290_MM_TO_FL_1_92
);
145 led
->movie_mode
= true;
148 aat1290_as2cwire_write(led
, AAT1290_MOVIE_MODE_CURRENT_ADDR
,
149 AAT1290_MAX_MM_CURR_PERCENT_0
- brightness
);
150 aat1290_as2cwire_write(led
, AAT1290_MOVIE_MODE_CONFIG_ADDR
,
151 AAT1290_MOVIE_MODE_ON
);
154 mutex_unlock(&led
->lock
);
157 /* LED subsystem callbacks */
159 static void aat1290_brightness_set_work(struct work_struct
*work
)
161 struct aat1290_led
*led
=
162 container_of(work
, struct aat1290_led
, work_brightness_set
);
164 aat1290_brightness_set(led
, led
->torch_brightness
);
167 static void aat1290_led_brightness_set(struct led_classdev
*led_cdev
,
168 enum led_brightness brightness
)
170 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
171 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
173 led
->torch_brightness
= brightness
;
174 schedule_work(&led
->work_brightness_set
);
177 static int aat1290_led_brightness_set_sync(struct led_classdev
*led_cdev
,
178 enum led_brightness brightness
)
180 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
181 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
183 aat1290_brightness_set(led
, brightness
);
188 static int aat1290_led_flash_strobe_set(struct led_classdev_flash
*fled_cdev
,
192 struct aat1290_led
*led
= fled_cdev_to_led(fled_cdev
);
193 struct led_classdev
*led_cdev
= &fled_cdev
->led_cdev
;
194 struct led_flash_setting
*timeout
= &fled_cdev
->timeout
;
196 mutex_lock(&led
->lock
);
199 aat1290_set_flash_safety_timer(led
, timeout
->val
);
200 gpiod_direction_output(led
->gpio_fl_en
, 1);
202 gpiod_direction_output(led
->gpio_fl_en
, 0);
203 gpiod_direction_output(led
->gpio_en_set
, 0);
207 * To reenter movie mode after a flash event the part must be cycled
208 * off and back on to reset the movie mode and reprogrammed via the
209 * AS2Cwire. Therefore the brightness and movie_mode properties needs
210 * to be updated here to reflect the actual state.
212 led_cdev
->brightness
= 0;
213 led
->movie_mode
= false;
215 mutex_unlock(&led
->lock
);
220 static int aat1290_led_flash_timeout_set(struct led_classdev_flash
*fled_cdev
,
224 * Don't do anything - flash timeout is cached in the led-class-flash
225 * core and will be applied in the strobe_set op, as writing the
226 * safety timer register spuriously turns the torch mode on.
232 static int aat1290_led_parse_dt(struct aat1290_led
*led
,
233 struct aat1290_led_config_data
*cfg
)
235 struct led_classdev
*led_cdev
= &led
->fled_cdev
.led_cdev
;
236 struct device
*dev
= &led
->pdev
->dev
;
237 struct device_node
*child_node
;
240 led
->gpio_fl_en
= devm_gpiod_get(dev
, "flen");
241 if (IS_ERR(led
->gpio_fl_en
)) {
242 ret
= PTR_ERR(led
->gpio_fl_en
);
243 dev_err(dev
, "Unable to claim gpio \"flen\".\n");
247 led
->gpio_en_set
= devm_gpiod_get(dev
, "enset");
248 if (IS_ERR(led
->gpio_en_set
)) {
249 ret
= PTR_ERR(led
->gpio_en_set
);
250 dev_err(dev
, "Unable to claim gpio \"enset\".\n");
254 child_node
= of_get_next_available_child(dev
->of_node
, NULL
);
256 dev_err(dev
, "No DT child node found for connected LED.\n");
260 led_cdev
->name
= of_get_property(child_node
, "label", NULL
) ? :
263 ret
= of_property_read_u32(child_node
, "led-max-microamp",
264 &cfg
->max_mm_current
);
266 * led-max-microamp will default to 1/20 of flash-max-microamp
267 * in case it is missing.
271 "led-max-microamp DT property missing\n");
273 ret
= of_property_read_u32(child_node
, "flash-max-microamp",
274 &cfg
->max_flash_current
);
277 "flash-max-microamp DT property missing\n");
281 ret
= of_property_read_u32(child_node
, "flash-max-timeout-us",
285 "flash-max-timeout-us DT property missing\n");
289 of_node_put(child_node
);
294 static void aat1290_led_validate_mm_current(struct aat1290_led
*led
,
295 struct aat1290_led_config_data
*cfg
)
297 int i
, b
= 0, e
= AAT1290_MM_CURRENT_SCALE_SIZE
;
301 if (cfg
->max_mm_current
< led
->mm_current_scale
[i
])
307 cfg
->max_mm_current
= led
->mm_current_scale
[b
];
308 cfg
->max_brightness
= b
+ 1;
311 int init_mm_current_scale(struct aat1290_led
*led
,
312 struct aat1290_led_config_data
*cfg
)
314 int max_mm_current_percent
[] = { 20, 22, 25, 28, 32, 36, 40, 45, 50, 56,
315 63, 71, 79, 89, 100 };
316 int i
, max_mm_current
=
317 AAT1290_MAX_MM_CURRENT(cfg
->max_flash_current
);
319 led
->mm_current_scale
= kzalloc(sizeof(max_mm_current_percent
),
321 if (!led
->mm_current_scale
)
324 for (i
= 0; i
< AAT1290_MM_CURRENT_SCALE_SIZE
; ++i
)
325 led
->mm_current_scale
[i
] = max_mm_current
*
326 max_mm_current_percent
[i
] / 100;
331 static int aat1290_led_get_configuration(struct aat1290_led
*led
,
332 struct aat1290_led_config_data
*cfg
)
336 ret
= aat1290_led_parse_dt(led
, cfg
);
340 * Init non-linear movie mode current scale basing
341 * on the max flash current from led configuration.
343 ret
= init_mm_current_scale(led
, cfg
);
347 aat1290_led_validate_mm_current(led
, cfg
);
349 kfree(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 static const struct led_flash_ops flash_ops
= {
369 .strobe_set
= aat1290_led_flash_strobe_set
,
370 .timeout_set
= aat1290_led_flash_timeout_set
,
373 static int aat1290_led_probe(struct platform_device
*pdev
)
375 struct device
*dev
= &pdev
->dev
;
376 struct aat1290_led
*led
;
377 struct led_classdev
*led_cdev
;
378 struct led_classdev_flash
*fled_cdev
;
379 struct aat1290_led_config_data led_cfg
= {};
382 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
387 platform_set_drvdata(pdev
, led
);
389 fled_cdev
= &led
->fled_cdev
;
390 fled_cdev
->ops
= &flash_ops
;
391 led_cdev
= &fled_cdev
->led_cdev
;
393 ret
= aat1290_led_get_configuration(led
, &led_cfg
);
397 mutex_init(&led
->lock
);
399 /* Initialize LED Flash class device */
400 led_cdev
->brightness_set
= aat1290_led_brightness_set
;
401 led_cdev
->brightness_set_sync
= aat1290_led_brightness_set_sync
;
402 led_cdev
->max_brightness
= led_cfg
.max_brightness
;
403 led_cdev
->flags
|= LED_DEV_CAP_FLASH
;
404 INIT_WORK(&led
->work_brightness_set
, aat1290_brightness_set_work
);
406 aat1290_init_flash_timeout(led
, &led_cfg
);
408 /* Register LED Flash class device */
409 ret
= led_classdev_flash_register(&pdev
->dev
, fled_cdev
);
411 goto err_flash_register
;
416 mutex_destroy(&led
->lock
);
421 static int aat1290_led_remove(struct platform_device
*pdev
)
423 struct aat1290_led
*led
= platform_get_drvdata(pdev
);
425 led_classdev_flash_unregister(&led
->fled_cdev
);
426 cancel_work_sync(&led
->work_brightness_set
);
428 mutex_destroy(&led
->lock
);
433 static const struct of_device_id aat1290_led_dt_match
[] = {
434 { .compatible
= "skyworks,aat1290" },
438 static struct platform_driver aat1290_led_driver
= {
439 .probe
= aat1290_led_probe
,
440 .remove
= aat1290_led_remove
,
443 .of_match_table
= aat1290_led_dt_match
,
447 module_platform_driver(aat1290_led_driver
);
449 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
450 MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs");
451 MODULE_LICENSE("GPL v2");