1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED driver : leds-ktd2692.c
5 * Copyright (C) 2015 Samsung Electronics
6 * Ingi Kim <ingi2.kim@samsung.com>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/led-class-flash.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/consumer.h>
19 /* Value related the movie mode */
20 #define KTD2692_MOVIE_MODE_CURRENT_LEVELS 16
21 #define KTD2692_MM_TO_FL_RATIO(x) ((x) / 3)
22 #define KTD2692_MM_MIN_CURR_THRESHOLD_SCALE 8
24 /* Value related the flash mode */
25 #define KTD2692_FLASH_MODE_TIMEOUT_LEVELS 8
26 #define KTD2692_FLASH_MODE_TIMEOUT_DISABLE 0
27 #define KTD2692_FLASH_MODE_CURR_PERCENT(x) (((x) * 16) / 100)
29 /* Macro for getting offset of flash timeout */
30 #define GET_TIMEOUT_OFFSET(timeout, step) ((timeout) / (step))
32 /* Base register address */
33 #define KTD2692_REG_LVP_BASE 0x00
34 #define KTD2692_REG_FLASH_TIMEOUT_BASE 0x20
35 #define KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE 0x40
36 #define KTD2692_REG_MOVIE_CURRENT_BASE 0x60
37 #define KTD2692_REG_FLASH_CURRENT_BASE 0x80
38 #define KTD2692_REG_MODE_BASE 0xA0
40 /* Set bit coding time for expresswire interface */
41 #define KTD2692_TIME_RESET_US 700
42 #define KTD2692_TIME_DATA_START_TIME_US 10
43 #define KTD2692_TIME_HIGH_END_OF_DATA_US 350
44 #define KTD2692_TIME_LOW_END_OF_DATA_US 10
45 #define KTD2692_TIME_SHORT_BITSET_US 4
46 #define KTD2692_TIME_LONG_BITSET_US 12
48 /* KTD2692 default length of name */
49 #define KTD2692_NAME_LENGTH 20
56 /* Movie / Flash Mode Control */
57 enum ktd2692_led_mode
{
58 KTD2692_MODE_DISABLE
= 0, /* default */
63 struct ktd2692_led_config_data
{
64 /* maximum LED current in movie mode */
65 u32 movie_max_microamp
;
66 /* maximum LED current in flash mode */
67 u32 flash_max_microamp
;
68 /* maximum flash timeout */
69 u32 flash_max_timeout
;
70 /* max LED brightness level */
71 enum led_brightness max_brightness
;
74 struct ktd2692_context
{
75 /* Related LED Flash class device */
76 struct led_classdev_flash fled_cdev
;
78 /* secures access to the device */
80 struct regulator
*regulator
;
82 struct gpio_desc
*aux_gpio
;
83 struct gpio_desc
*ctrl_gpio
;
85 enum ktd2692_led_mode mode
;
86 enum led_brightness torch_brightness
;
89 static struct ktd2692_context
*fled_cdev_to_led(
90 struct led_classdev_flash
*fled_cdev
)
92 return container_of(fled_cdev
, struct ktd2692_context
, fled_cdev
);
95 static void ktd2692_expresswire_start(struct ktd2692_context
*led
)
97 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_HIGH
);
98 udelay(KTD2692_TIME_DATA_START_TIME_US
);
101 static void ktd2692_expresswire_reset(struct ktd2692_context
*led
)
103 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_LOW
);
104 udelay(KTD2692_TIME_RESET_US
);
107 static void ktd2692_expresswire_end(struct ktd2692_context
*led
)
109 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_LOW
);
110 udelay(KTD2692_TIME_LOW_END_OF_DATA_US
);
111 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_HIGH
);
112 udelay(KTD2692_TIME_HIGH_END_OF_DATA_US
);
115 static void ktd2692_expresswire_set_bit(struct ktd2692_context
*led
, bool bit
)
118 * The Low Bit(0) and High Bit(1) is based on a time detection
119 * algorithm between time low and time high
120 * Time_(L_LB) : Low time of the Low Bit(0)
121 * Time_(H_LB) : High time of the LOW Bit(0)
122 * Time_(L_HB) : Low time of the High Bit(1)
123 * Time_(H_HB) : High time of the High Bit(1)
125 * It can be simplified to:
126 * Low Bit(0) : 2 * Time_(H_LB) < Time_(L_LB)
127 * High Bit(1) : 2 * Time_(L_HB) < Time_(H_HB)
128 * HIGH ___ ____ _.. _________ ___
129 * |_________| |_.. |____| |__|
130 * LOW <L_LB> <H_LB> <L_HB> <H_HB>
131 * [ Low Bit (0) ] [ High Bit(1) ]
134 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_LOW
);
135 udelay(KTD2692_TIME_SHORT_BITSET_US
);
136 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_HIGH
);
137 udelay(KTD2692_TIME_LONG_BITSET_US
);
139 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_LOW
);
140 udelay(KTD2692_TIME_LONG_BITSET_US
);
141 gpiod_direction_output(led
->ctrl_gpio
, KTD2692_HIGH
);
142 udelay(KTD2692_TIME_SHORT_BITSET_US
);
146 static void ktd2692_expresswire_write(struct ktd2692_context
*led
, u8 value
)
150 ktd2692_expresswire_start(led
);
151 for (i
= 7; i
>= 0; i
--)
152 ktd2692_expresswire_set_bit(led
, value
& BIT(i
));
153 ktd2692_expresswire_end(led
);
156 static int ktd2692_led_brightness_set(struct led_classdev
*led_cdev
,
157 enum led_brightness brightness
)
159 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(led_cdev
);
160 struct ktd2692_context
*led
= fled_cdev_to_led(fled_cdev
);
162 mutex_lock(&led
->lock
);
164 if (brightness
== LED_OFF
) {
165 led
->mode
= KTD2692_MODE_DISABLE
;
166 gpiod_direction_output(led
->aux_gpio
, KTD2692_LOW
);
168 ktd2692_expresswire_write(led
, brightness
|
169 KTD2692_REG_MOVIE_CURRENT_BASE
);
170 led
->mode
= KTD2692_MODE_MOVIE
;
173 ktd2692_expresswire_write(led
, led
->mode
| KTD2692_REG_MODE_BASE
);
174 mutex_unlock(&led
->lock
);
179 static int ktd2692_led_flash_strobe_set(struct led_classdev_flash
*fled_cdev
,
182 struct ktd2692_context
*led
= fled_cdev_to_led(fled_cdev
);
183 struct led_flash_setting
*timeout
= &fled_cdev
->timeout
;
186 mutex_lock(&led
->lock
);
189 flash_tm_reg
= GET_TIMEOUT_OFFSET(timeout
->val
, timeout
->step
);
190 ktd2692_expresswire_write(led
, flash_tm_reg
191 | KTD2692_REG_FLASH_TIMEOUT_BASE
);
193 led
->mode
= KTD2692_MODE_FLASH
;
194 gpiod_direction_output(led
->aux_gpio
, KTD2692_HIGH
);
196 led
->mode
= KTD2692_MODE_DISABLE
;
197 gpiod_direction_output(led
->aux_gpio
, KTD2692_LOW
);
200 ktd2692_expresswire_write(led
, led
->mode
| KTD2692_REG_MODE_BASE
);
202 fled_cdev
->led_cdev
.brightness
= LED_OFF
;
203 led
->mode
= KTD2692_MODE_DISABLE
;
205 mutex_unlock(&led
->lock
);
210 static int ktd2692_led_flash_timeout_set(struct led_classdev_flash
*fled_cdev
,
216 static void ktd2692_init_movie_current_max(struct ktd2692_led_config_data
*cfg
)
219 u32 movie_current_microamp
;
221 offset
= KTD2692_MOVIE_MODE_CURRENT_LEVELS
;
222 step
= KTD2692_MM_TO_FL_RATIO(cfg
->flash_max_microamp
)
223 / KTD2692_MOVIE_MODE_CURRENT_LEVELS
;
226 movie_current_microamp
= step
* offset
;
228 } while ((movie_current_microamp
> cfg
->movie_max_microamp
) &&
231 cfg
->max_brightness
= offset
;
234 static void ktd2692_init_flash_timeout(struct led_classdev_flash
*fled_cdev
,
235 struct ktd2692_led_config_data
*cfg
)
237 struct led_flash_setting
*setting
;
239 setting
= &fled_cdev
->timeout
;
240 setting
->min
= KTD2692_FLASH_MODE_TIMEOUT_DISABLE
;
241 setting
->max
= cfg
->flash_max_timeout
;
242 setting
->step
= cfg
->flash_max_timeout
243 / (KTD2692_FLASH_MODE_TIMEOUT_LEVELS
- 1);
244 setting
->val
= cfg
->flash_max_timeout
;
247 static void ktd2692_setup(struct ktd2692_context
*led
)
249 led
->mode
= KTD2692_MODE_DISABLE
;
250 ktd2692_expresswire_reset(led
);
251 gpiod_direction_output(led
->aux_gpio
, KTD2692_LOW
);
253 ktd2692_expresswire_write(led
, (KTD2692_MM_MIN_CURR_THRESHOLD_SCALE
- 1)
254 | KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE
);
255 ktd2692_expresswire_write(led
, KTD2692_FLASH_MODE_CURR_PERCENT(45)
256 | KTD2692_REG_FLASH_CURRENT_BASE
);
259 static int ktd2692_parse_dt(struct ktd2692_context
*led
, struct device
*dev
,
260 struct ktd2692_led_config_data
*cfg
)
262 struct device_node
*np
= dev_of_node(dev
);
263 struct device_node
*child_node
;
266 if (!dev_of_node(dev
))
269 led
->ctrl_gpio
= devm_gpiod_get(dev
, "ctrl", GPIOD_ASIS
);
270 ret
= PTR_ERR_OR_ZERO(led
->ctrl_gpio
);
272 dev_err(dev
, "cannot get ctrl-gpios %d\n", ret
);
276 led
->aux_gpio
= devm_gpiod_get(dev
, "aux", GPIOD_ASIS
);
277 ret
= PTR_ERR_OR_ZERO(led
->aux_gpio
);
279 dev_err(dev
, "cannot get aux-gpios %d\n", ret
);
283 led
->regulator
= devm_regulator_get(dev
, "vin");
284 if (IS_ERR(led
->regulator
))
285 led
->regulator
= NULL
;
287 if (led
->regulator
) {
288 ret
= regulator_enable(led
->regulator
);
290 dev_err(dev
, "Failed to enable supply: %d\n", ret
);
293 child_node
= of_get_next_available_child(np
, NULL
);
295 dev_err(dev
, "No DT child node found for connected LED.\n");
299 led
->fled_cdev
.led_cdev
.name
=
300 of_get_property(child_node
, "label", NULL
) ? : child_node
->name
;
302 ret
= of_property_read_u32(child_node
, "led-max-microamp",
303 &cfg
->movie_max_microamp
);
305 dev_err(dev
, "failed to parse led-max-microamp\n");
309 ret
= of_property_read_u32(child_node
, "flash-max-microamp",
310 &cfg
->flash_max_microamp
);
312 dev_err(dev
, "failed to parse flash-max-microamp\n");
316 ret
= of_property_read_u32(child_node
, "flash-max-timeout-us",
317 &cfg
->flash_max_timeout
);
319 dev_err(dev
, "failed to parse flash-max-timeout-us\n");
324 of_node_put(child_node
);
328 static const struct led_flash_ops flash_ops
= {
329 .strobe_set
= ktd2692_led_flash_strobe_set
,
330 .timeout_set
= ktd2692_led_flash_timeout_set
,
333 static int ktd2692_probe(struct platform_device
*pdev
)
335 struct ktd2692_context
*led
;
336 struct led_classdev
*led_cdev
;
337 struct led_classdev_flash
*fled_cdev
;
338 struct ktd2692_led_config_data led_cfg
;
341 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
345 fled_cdev
= &led
->fled_cdev
;
346 led_cdev
= &fled_cdev
->led_cdev
;
348 ret
= ktd2692_parse_dt(led
, &pdev
->dev
, &led_cfg
);
352 ktd2692_init_flash_timeout(fled_cdev
, &led_cfg
);
353 ktd2692_init_movie_current_max(&led_cfg
);
355 fled_cdev
->ops
= &flash_ops
;
357 led_cdev
->max_brightness
= led_cfg
.max_brightness
;
358 led_cdev
->brightness_set_blocking
= ktd2692_led_brightness_set
;
359 led_cdev
->flags
|= LED_CORE_SUSPENDRESUME
| LED_DEV_CAP_FLASH
;
361 mutex_init(&led
->lock
);
363 platform_set_drvdata(pdev
, led
);
365 ret
= led_classdev_flash_register(&pdev
->dev
, fled_cdev
);
367 dev_err(&pdev
->dev
, "can't register LED %s\n", led_cdev
->name
);
368 mutex_destroy(&led
->lock
);
377 static int ktd2692_remove(struct platform_device
*pdev
)
379 struct ktd2692_context
*led
= platform_get_drvdata(pdev
);
382 led_classdev_flash_unregister(&led
->fled_cdev
);
384 if (led
->regulator
) {
385 ret
= regulator_disable(led
->regulator
);
388 "Failed to disable supply: %d\n", ret
);
391 mutex_destroy(&led
->lock
);
396 static const struct of_device_id ktd2692_match
[] = {
397 { .compatible
= "kinetic,ktd2692", },
400 MODULE_DEVICE_TABLE(of
, ktd2692_match
);
402 static struct platform_driver ktd2692_driver
= {
405 .of_match_table
= ktd2692_match
,
407 .probe
= ktd2692_probe
,
408 .remove
= ktd2692_remove
,
411 module_platform_driver(ktd2692_driver
);
413 MODULE_AUTHOR("Ingi Kim <ingi2.kim@samsung.com>");
414 MODULE_DESCRIPTION("Kinetic KTD2692 LED driver");
415 MODULE_LICENSE("GPL v2");