2 * LED driver : leds-ktd2692.c
4 * Copyright (C) 2015 Samsung Electronics
5 * Ingi Kim <ingi2.kim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/led-class-flash.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
22 /* Value related the movie mode */
23 #define KTD2692_MOVIE_MODE_CURRENT_LEVELS 16
24 #define KTD2692_MM_TO_FL_RATIO(x) ((x) / 3)
25 #define KTD2962_MM_MIN_CURR_THRESHOLD_SCALE 8
27 /* Value related the flash mode */
28 #define KTD2692_FLASH_MODE_TIMEOUT_LEVELS 8
29 #define KTD2692_FLASH_MODE_TIMEOUT_DISABLE 0
30 #define KTD2692_FLASH_MODE_CURR_PERCENT(x) (((x) * 16) / 100)
32 /* Macro for getting offset of flash timeout */
33 #define GET_TIMEOUT_OFFSET(timeout, step) ((timeout) / (step))
35 /* Base register address */
36 #define KTD2692_REG_LVP_BASE 0x00
37 #define KTD2692_REG_FLASH_TIMEOUT_BASE 0x20
38 #define KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE 0x40
39 #define KTD2692_REG_MOVIE_CURRENT_BASE 0x60
40 #define KTD2692_REG_FLASH_CURRENT_BASE 0x80
41 #define KTD2692_REG_MODE_BASE 0xA0
43 /* Set bit coding time for expresswire interface */
44 #define KTD2692_TIME_RESET_US 700
45 #define KTD2692_TIME_DATA_START_TIME_US 10
46 #define KTD2692_TIME_HIGH_END_OF_DATA_US 350
47 #define KTD2692_TIME_LOW_END_OF_DATA_US 10
48 #define KTD2692_TIME_SHORT_BITSET_US 4
49 #define KTD2692_TIME_LONG_BITSET_US 12
51 /* KTD2692 default length of name */
52 #define KTD2692_NAME_LENGTH 20
59 /* Movie / Flash Mode Control */
60 enum ktd2692_led_mode {
61 KTD2692_MODE_DISABLE = 0, /* default */
66 struct ktd2692_led_config_data {
67 /* maximum LED current in movie mode */
68 u32 movie_max_microamp;
69 /* maximum LED current in flash mode */
70 u32 flash_max_microamp;
71 /* maximum flash timeout */
72 u32 flash_max_timeout;
73 /* max LED brightness level */
74 enum led_brightness max_brightness;
77 struct ktd2692_context {
78 /* Related LED Flash class device */
79 struct led_classdev_flash fled_cdev;
81 /* secures access to the device */
83 struct regulator *regulator;
85 struct gpio_desc *aux_gpio;
86 struct gpio_desc *ctrl_gpio;
88 enum ktd2692_led_mode mode;
89 enum led_brightness torch_brightness;
92 static struct ktd2692_context *fled_cdev_to_led(
93 struct led_classdev_flash *fled_cdev)
95 return container_of(fled_cdev, struct ktd2692_context, fled_cdev);
98 static void ktd2692_expresswire_start(struct ktd2692_context *led)
100 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
101 udelay(KTD2692_TIME_DATA_START_TIME_US);
104 static void ktd2692_expresswire_reset(struct ktd2692_context *led)
106 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
107 udelay(KTD2692_TIME_RESET_US);
110 static void ktd2692_expresswire_end(struct ktd2692_context *led)
112 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
113 udelay(KTD2692_TIME_LOW_END_OF_DATA_US);
114 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
115 udelay(KTD2692_TIME_HIGH_END_OF_DATA_US);
118 static void ktd2692_expresswire_set_bit(struct ktd2692_context *led, bool bit)
121 * The Low Bit(0) and High Bit(1) is based on a time detection
122 * algorithm between time low and time high
123 * Time_(L_LB) : Low time of the Low Bit(0)
124 * Time_(H_LB) : High time of the LOW Bit(0)
125 * Time_(L_HB) : Low time of the High Bit(1)
126 * Time_(H_HB) : High time of the High Bit(1)
128 * It can be simplified to:
129 * Low Bit(0) : 2 * Time_(H_LB) < Time_(L_LB)
130 * High Bit(1) : 2 * Time_(L_HB) < Time_(H_HB)
131 * HIGH ___ ____ _.. _________ ___
132 * |_________| |_.. |____| |__|
133 * LOW <L_LB> <H_LB> <L_HB> <H_HB>
134 * [ Low Bit (0) ] [ High Bit(1) ]
137 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
138 udelay(KTD2692_TIME_SHORT_BITSET_US);
139 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
140 udelay(KTD2692_TIME_LONG_BITSET_US);
142 gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
143 udelay(KTD2692_TIME_LONG_BITSET_US);
144 gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
145 udelay(KTD2692_TIME_SHORT_BITSET_US);
149 static void ktd2692_expresswire_write(struct ktd2692_context *led, u8 value)
153 ktd2692_expresswire_start(led);
154 for (i = 7; i >= 0; i--)
155 ktd2692_expresswire_set_bit(led, value & BIT(i));
156 ktd2692_expresswire_end(led);
159 static int ktd2692_led_brightness_set(struct led_classdev *led_cdev,
160 enum led_brightness brightness)
162 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
163 struct ktd2692_context *led = fled_cdev_to_led(fled_cdev);
165 mutex_lock(&led->lock);
167 if (brightness == LED_OFF) {
168 led->mode = KTD2692_MODE_DISABLE;
169 gpiod_direction_output(led->aux_gpio, KTD2692_LOW);
171 ktd2692_expresswire_write(led, brightness |
172 KTD2692_REG_MOVIE_CURRENT_BASE);
173 led->mode = KTD2692_MODE_MOVIE;
176 ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE);
177 mutex_unlock(&led->lock);
182 static int ktd2692_led_flash_strobe_set(struct led_classdev_flash *fled_cdev,
185 struct ktd2692_context *led = fled_cdev_to_led(fled_cdev);
186 struct led_flash_setting *timeout = &fled_cdev->timeout;
189 mutex_lock(&led->lock);
192 flash_tm_reg = GET_TIMEOUT_OFFSET(timeout->val, timeout->step);
193 ktd2692_expresswire_write(led, flash_tm_reg
194 | KTD2692_REG_FLASH_TIMEOUT_BASE);
196 led->mode = KTD2692_MODE_FLASH;
197 gpiod_direction_output(led->aux_gpio, KTD2692_HIGH);
199 led->mode = KTD2692_MODE_DISABLE;
200 gpiod_direction_output(led->aux_gpio, KTD2692_LOW);
203 ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE);
205 fled_cdev->led_cdev.brightness = LED_OFF;
206 led->mode = KTD2692_MODE_DISABLE;
208 mutex_unlock(&led->lock);
213 static int ktd2692_led_flash_timeout_set(struct led_classdev_flash *fled_cdev,
219 static void ktd2692_init_movie_current_max(struct ktd2692_led_config_data *cfg)
222 u32 movie_current_microamp;
224 offset = KTD2692_MOVIE_MODE_CURRENT_LEVELS;
225 step = KTD2692_MM_TO_FL_RATIO(cfg->flash_max_microamp)
226 / KTD2692_MOVIE_MODE_CURRENT_LEVELS;
229 movie_current_microamp = step * offset;
231 } while ((movie_current_microamp > cfg->movie_max_microamp) &&
234 cfg->max_brightness = offset;
237 static void ktd2692_init_flash_timeout(struct led_classdev_flash *fled_cdev,
238 struct ktd2692_led_config_data *cfg)
240 struct led_flash_setting *setting;
242 setting = &fled_cdev->timeout;
243 setting->min = KTD2692_FLASH_MODE_TIMEOUT_DISABLE;
244 setting->max = cfg->flash_max_timeout;
245 setting->step = cfg->flash_max_timeout
246 / (KTD2692_FLASH_MODE_TIMEOUT_LEVELS - 1);
247 setting->val = cfg->flash_max_timeout;
250 static void ktd2692_setup(struct ktd2692_context *led)
252 led->mode = KTD2692_MODE_DISABLE;
253 ktd2692_expresswire_reset(led);
254 gpiod_direction_output(led->aux_gpio, KTD2692_LOW);
256 ktd2692_expresswire_write(led, (KTD2962_MM_MIN_CURR_THRESHOLD_SCALE - 1)
257 | KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE);
258 ktd2692_expresswire_write(led, KTD2692_FLASH_MODE_CURR_PERCENT(45)
259 | KTD2692_REG_FLASH_CURRENT_BASE);
262 static void regulator_disable_action(void *_data)
264 struct device *dev = _data;
265 struct ktd2692_context *led = dev_get_drvdata(dev);
268 ret = regulator_disable(led->regulator);
270 dev_err(dev, "Failed to disable supply: %d\n", ret);
273 static int ktd2692_parse_dt(struct ktd2692_context *led, struct device *dev,
274 struct ktd2692_led_config_data *cfg)
276 struct device_node *np = dev->of_node;
277 struct device_node *child_node;
283 led->ctrl_gpio = devm_gpiod_get(dev, "ctrl", GPIOD_ASIS);
284 ret = PTR_ERR_OR_ZERO(led->ctrl_gpio);
286 dev_err(dev, "cannot get ctrl-gpios %d\n", ret);
290 led->aux_gpio = devm_gpiod_get(dev, "aux", GPIOD_ASIS);
291 ret = PTR_ERR_OR_ZERO(led->aux_gpio);
293 dev_err(dev, "cannot get aux-gpios %d\n", ret);
297 led->regulator = devm_regulator_get(dev, "vin");
298 if (IS_ERR(led->regulator))
299 led->regulator = NULL;
301 if (led->regulator) {
302 ret = regulator_enable(led->regulator);
304 dev_err(dev, "Failed to enable supply: %d\n", ret);
306 ret = devm_add_action_or_reset(dev,
307 regulator_disable_action, dev);
313 child_node = of_get_next_available_child(np, NULL);
315 dev_err(dev, "No DT child node found for connected LED.\n");
319 led->fled_cdev.led_cdev.name =
320 of_get_property(child_node, "label", NULL) ? : child_node->name;
322 ret = of_property_read_u32(child_node, "led-max-microamp",
323 &cfg->movie_max_microamp);
325 dev_err(dev, "failed to parse led-max-microamp\n");
329 ret = of_property_read_u32(child_node, "flash-max-microamp",
330 &cfg->flash_max_microamp);
332 dev_err(dev, "failed to parse flash-max-microamp\n");
336 ret = of_property_read_u32(child_node, "flash-max-timeout-us",
337 &cfg->flash_max_timeout);
339 dev_err(dev, "failed to parse flash-max-timeout-us\n");
344 of_node_put(child_node);
348 static const struct led_flash_ops flash_ops = {
349 .strobe_set = ktd2692_led_flash_strobe_set,
350 .timeout_set = ktd2692_led_flash_timeout_set,
353 static int ktd2692_probe(struct platform_device *pdev)
355 struct ktd2692_context *led;
356 struct led_classdev *led_cdev;
357 struct led_classdev_flash *fled_cdev;
358 struct ktd2692_led_config_data led_cfg;
361 led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
365 fled_cdev = &led->fled_cdev;
366 led_cdev = &fled_cdev->led_cdev;
368 ret = ktd2692_parse_dt(led, &pdev->dev, &led_cfg);
372 ktd2692_init_flash_timeout(fled_cdev, &led_cfg);
373 ktd2692_init_movie_current_max(&led_cfg);
375 fled_cdev->ops = &flash_ops;
377 led_cdev->max_brightness = led_cfg.max_brightness;
378 led_cdev->brightness_set_blocking = ktd2692_led_brightness_set;
379 led_cdev->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH;
381 mutex_init(&led->lock);
383 platform_set_drvdata(pdev, led);
385 ret = led_classdev_flash_register(&pdev->dev, fled_cdev);
387 dev_err(&pdev->dev, "can't register LED %s\n", led_cdev->name);
388 mutex_destroy(&led->lock);
397 static int ktd2692_remove(struct platform_device *pdev)
399 struct ktd2692_context *led = platform_get_drvdata(pdev);
401 led_classdev_flash_unregister(&led->fled_cdev);
403 mutex_destroy(&led->lock);
408 static const struct of_device_id ktd2692_match[] = {
409 { .compatible = "kinetic,ktd2692", },
412 MODULE_DEVICE_TABLE(of, ktd2692_match);
414 static struct platform_driver ktd2692_driver = {
417 .of_match_table = ktd2692_match,
419 .probe = ktd2692_probe,
420 .remove = ktd2692_remove,
423 module_platform_driver(ktd2692_driver);
425 MODULE_AUTHOR("Ingi Kim <ingi2.kim@samsung.com>");
426 MODULE_DESCRIPTION("Kinetic KTD2692 LED driver");
427 MODULE_LICENSE("GPL v2");