2 * LEDs driver for LT3593 controllers
4 * See the datasheet at http://cds.linear.com/docs/Datasheet/3593f.pdf
6 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
8 * Based on leds-gpio.c,
10 * Copyright (C) 2007 8D Technologies inc.
11 * Raphael Assenat <raph@8d.com>
12 * Copyright (C) 2008 Freescale Semiconductor, Inc.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/kernel.h>
20 #include <linux/platform_device.h>
21 #include <linux/leds.h>
22 #include <linux/delay.h>
23 #include <linux/gpio.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
27 struct lt3593_led_data
{
28 struct led_classdev cdev
;
32 static int lt3593_led_set(struct led_classdev
*led_cdev
,
33 enum led_brightness value
)
35 struct lt3593_led_data
*led_dat
=
36 container_of(led_cdev
, struct lt3593_led_data
, cdev
);
40 * The LT3593 resets its internal current level register to the maximum
41 * level on the first falling edge on the control pin. Each following
42 * falling edge decreases the current level by 625uA. Up to 32 pulses
43 * can be sent, so the maximum power reduction is 20mA.
44 * After a timeout of 128us, the value is taken from the register and
45 * applied is to the output driver.
49 gpio_set_value_cansleep(led_dat
->gpio
, 0);
53 pulses
= 32 - (value
* 32) / 255;
56 gpio_set_value_cansleep(led_dat
->gpio
, 0);
58 gpio_set_value_cansleep(led_dat
->gpio
, 1);
62 gpio_set_value_cansleep(led_dat
->gpio
, 1);
65 gpio_set_value_cansleep(led_dat
->gpio
, 0);
67 gpio_set_value_cansleep(led_dat
->gpio
, 1);
74 static int create_lt3593_led(const struct gpio_led
*template,
75 struct lt3593_led_data
*led_dat
, struct device
*parent
)
79 /* skip leds on GPIOs that aren't available */
80 if (!gpio_is_valid(template->gpio
)) {
81 dev_info(parent
, "%s: skipping unavailable LT3593 LED at gpio %d (%s)\n",
82 KBUILD_MODNAME
, template->gpio
, template->name
);
86 led_dat
->cdev
.name
= template->name
;
87 led_dat
->cdev
.default_trigger
= template->default_trigger
;
88 led_dat
->gpio
= template->gpio
;
90 led_dat
->cdev
.brightness_set_blocking
= lt3593_led_set
;
92 state
= (template->default_state
== LEDS_GPIO_DEFSTATE_ON
);
93 led_dat
->cdev
.brightness
= state
? LED_FULL
: LED_OFF
;
95 if (!template->retain_state_suspended
)
96 led_dat
->cdev
.flags
|= LED_CORE_SUSPENDRESUME
;
98 ret
= devm_gpio_request_one(parent
, template->gpio
, state
?
99 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
,
104 ret
= led_classdev_register(parent
, &led_dat
->cdev
);
108 dev_info(parent
, "%s: registered LT3593 LED '%s' at GPIO %d\n",
109 KBUILD_MODNAME
, template->name
, template->gpio
);
114 static void delete_lt3593_led(struct lt3593_led_data
*led
)
116 if (!gpio_is_valid(led
->gpio
))
119 led_classdev_unregister(&led
->cdev
);
122 static int lt3593_led_probe(struct platform_device
*pdev
)
124 struct gpio_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
125 struct lt3593_led_data
*leds_data
;
131 leds_data
= devm_kzalloc(&pdev
->dev
,
132 sizeof(struct lt3593_led_data
) * pdata
->num_leds
,
137 for (i
= 0; i
< pdata
->num_leds
; i
++) {
138 ret
= create_lt3593_led(&pdata
->leds
[i
], &leds_data
[i
],
144 platform_set_drvdata(pdev
, leds_data
);
149 for (i
= i
- 1; i
>= 0; i
--)
150 delete_lt3593_led(&leds_data
[i
]);
155 static int lt3593_led_remove(struct platform_device
*pdev
)
158 struct gpio_led_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
159 struct lt3593_led_data
*leds_data
;
161 leds_data
= platform_get_drvdata(pdev
);
163 for (i
= 0; i
< pdata
->num_leds
; i
++)
164 delete_lt3593_led(&leds_data
[i
]);
169 static struct platform_driver lt3593_led_driver
= {
170 .probe
= lt3593_led_probe
,
171 .remove
= lt3593_led_remove
,
173 .name
= "leds-lt3593",
177 module_platform_driver(lt3593_led_driver
);
179 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
180 MODULE_DESCRIPTION("LED driver for LT3593 controllers");
181 MODULE_LICENSE("GPL");
182 MODULE_ALIAS("platform:leds-lt3593");