This client driver allows you to use a GPIO pin as a source for PPS
[linux-2.6/next.git] / drivers / leds / leds-lt3593.c
blob53f67b8ce55db505bd4ae5127423202e9141bca3
1 /*
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/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/leds.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/gpio.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
29 struct lt3593_led_data {
30 struct led_classdev cdev;
31 unsigned gpio;
32 struct work_struct work;
33 u8 new_level;
36 static void lt3593_led_work(struct work_struct *work)
38 int pulses;
39 struct lt3593_led_data *led_dat =
40 container_of(work, struct lt3593_led_data, work);
43 * The LT3593 resets its internal current level register to the maximum
44 * level on the first falling edge on the control pin. Each following
45 * falling edge decreases the current level by 625uA. Up to 32 pulses
46 * can be sent, so the maximum power reduction is 20mA.
47 * After a timeout of 128us, the value is taken from the register and
48 * applied is to the output driver.
51 if (led_dat->new_level == 0) {
52 gpio_set_value_cansleep(led_dat->gpio, 0);
53 return;
56 pulses = 32 - (led_dat->new_level * 32) / 255;
58 if (pulses == 0) {
59 gpio_set_value_cansleep(led_dat->gpio, 0);
60 mdelay(1);
61 gpio_set_value_cansleep(led_dat->gpio, 1);
62 return;
65 gpio_set_value_cansleep(led_dat->gpio, 1);
67 while (pulses--) {
68 gpio_set_value_cansleep(led_dat->gpio, 0);
69 udelay(1);
70 gpio_set_value_cansleep(led_dat->gpio, 1);
71 udelay(1);
75 static void lt3593_led_set(struct led_classdev *led_cdev,
76 enum led_brightness value)
78 struct lt3593_led_data *led_dat =
79 container_of(led_cdev, struct lt3593_led_data, cdev);
81 led_dat->new_level = value;
82 schedule_work(&led_dat->work);
85 static int __devinit create_lt3593_led(const struct gpio_led *template,
86 struct lt3593_led_data *led_dat, struct device *parent)
88 int ret, state;
90 /* skip leds on GPIOs that aren't available */
91 if (!gpio_is_valid(template->gpio)) {
92 printk(KERN_INFO "%s: skipping unavailable LT3593 LED at gpio %d (%s)\n",
93 KBUILD_MODNAME, template->gpio, template->name);
94 return 0;
97 ret = gpio_request(template->gpio, template->name);
98 if (ret < 0)
99 return ret;
101 led_dat->cdev.name = template->name;
102 led_dat->cdev.default_trigger = template->default_trigger;
103 led_dat->gpio = template->gpio;
105 led_dat->cdev.brightness_set = lt3593_led_set;
107 state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
108 led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
110 if (!template->retain_state_suspended)
111 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
113 ret = gpio_direction_output(led_dat->gpio, state);
114 if (ret < 0)
115 goto err;
117 INIT_WORK(&led_dat->work, lt3593_led_work);
119 ret = led_classdev_register(parent, &led_dat->cdev);
120 if (ret < 0)
121 goto err;
123 printk(KERN_INFO "%s: registered LT3593 LED '%s' at GPIO %d\n",
124 KBUILD_MODNAME, template->name, template->gpio);
126 return 0;
128 err:
129 gpio_free(led_dat->gpio);
130 return ret;
133 static void delete_lt3593_led(struct lt3593_led_data *led)
135 if (!gpio_is_valid(led->gpio))
136 return;
138 led_classdev_unregister(&led->cdev);
139 cancel_work_sync(&led->work);
140 gpio_free(led->gpio);
143 static int __devinit lt3593_led_probe(struct platform_device *pdev)
145 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
146 struct lt3593_led_data *leds_data;
147 int i, ret = 0;
149 if (!pdata)
150 return -EBUSY;
152 leds_data = kzalloc(sizeof(struct lt3593_led_data) * pdata->num_leds,
153 GFP_KERNEL);
154 if (!leds_data)
155 return -ENOMEM;
157 for (i = 0; i < pdata->num_leds; i++) {
158 ret = create_lt3593_led(&pdata->leds[i], &leds_data[i],
159 &pdev->dev);
160 if (ret < 0)
161 goto err;
164 platform_set_drvdata(pdev, leds_data);
166 return 0;
168 err:
169 for (i = i - 1; i >= 0; i--)
170 delete_lt3593_led(&leds_data[i]);
172 kfree(leds_data);
174 return ret;
177 static int __devexit lt3593_led_remove(struct platform_device *pdev)
179 int i;
180 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
181 struct lt3593_led_data *leds_data;
183 leds_data = platform_get_drvdata(pdev);
185 for (i = 0; i < pdata->num_leds; i++)
186 delete_lt3593_led(&leds_data[i]);
188 kfree(leds_data);
190 return 0;
193 static struct platform_driver lt3593_led_driver = {
194 .probe = lt3593_led_probe,
195 .remove = __devexit_p(lt3593_led_remove),
196 .driver = {
197 .name = "leds-lt3593",
198 .owner = THIS_MODULE,
202 MODULE_ALIAS("platform:leds-lt3593");
204 static int __init lt3593_led_init(void)
206 return platform_driver_register(&lt3593_led_driver);
209 static void __exit lt3593_led_exit(void)
211 platform_driver_unregister(&lt3593_led_driver);
214 module_init(lt3593_led_init);
215 module_exit(lt3593_led_exit);
217 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
218 MODULE_DESCRIPTION("LED driver for LT3593 controllers");
219 MODULE_LICENSE("GPL");