blob: ca48a7d5502d35fb919de639a4ddb067b6f80baf [file] [log] [blame]
Daniel Macka8dd18f2009-10-15 02:59:35 +02001/*
2 * LEDs driver for LT3593 controllers
3 *
4 * See the datasheet at http://cds.linear.com/docs/Datasheet/3593f.pdf
5 *
6 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
7 *
8 * Based on leds-gpio.c,
9 *
10 * Copyright (C) 2007 8D Technologies inc.
11 * Raphael Assenat <raph@8d.com>
12 * Copyright (C) 2008 Freescale Semiconductor, Inc.
13 *
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.
17 */
18
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040027#include <linux/module.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +020028
29struct lt3593_led_data {
30 struct led_classdev cdev;
31 unsigned gpio;
32 struct work_struct work;
33 u8 new_level;
34};
35
36static void lt3593_led_work(struct work_struct *work)
37{
38 int pulses;
39 struct lt3593_led_data *led_dat =
40 container_of(work, struct lt3593_led_data, work);
41
42 /*
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.
49 */
50
51 if (led_dat->new_level == 0) {
52 gpio_set_value_cansleep(led_dat->gpio, 0);
53 return;
54 }
55
56 pulses = 32 - (led_dat->new_level * 32) / 255;
57
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;
63 }
64
65 gpio_set_value_cansleep(led_dat->gpio, 1);
66
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);
72 }
73}
74
75static void lt3593_led_set(struct led_classdev *led_cdev,
76 enum led_brightness value)
77{
78 struct lt3593_led_data *led_dat =
79 container_of(led_cdev, struct lt3593_led_data, cdev);
80
81 led_dat->new_level = value;
82 schedule_work(&led_dat->work);
83}
84
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050085static int create_lt3593_led(const struct gpio_led *template,
Daniel Macka8dd18f2009-10-15 02:59:35 +020086 struct lt3593_led_data *led_dat, struct device *parent)
87{
88 int ret, state;
89
90 /* skip leds on GPIOs that aren't available */
91 if (!gpio_is_valid(template->gpio)) {
Sachin Kamat30e5c3e2012-11-25 12:10:27 +053092 dev_info(parent, "%s: skipping unavailable LT3593 LED at gpio %d (%s)\n",
Daniel Macka8dd18f2009-10-15 02:59:35 +020093 KBUILD_MODNAME, template->gpio, template->name);
94 return 0;
95 }
96
Daniel Macka8dd18f2009-10-15 02:59:35 +020097 led_dat->cdev.name = template->name;
98 led_dat->cdev.default_trigger = template->default_trigger;
99 led_dat->gpio = template->gpio;
100
101 led_dat->cdev.brightness_set = lt3593_led_set;
102
103 state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
104 led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
105
106 if (!template->retain_state_suspended)
107 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
108
Jingoo Han84f69422013-03-07 18:37:38 -0800109 ret = devm_gpio_request_one(parent, template->gpio, state ?
110 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
111 template->name);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200112 if (ret < 0)
Jingoo Han507d967bc2012-10-23 05:17:56 -0700113 return ret;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200114
115 INIT_WORK(&led_dat->work, lt3593_led_work);
116
117 ret = led_classdev_register(parent, &led_dat->cdev);
118 if (ret < 0)
Sachin Kamat95a8ff82012-11-25 10:14:27 +0530119 return ret;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200120
Sachin Kamat30e5c3e2012-11-25 12:10:27 +0530121 dev_info(parent, "%s: registered LT3593 LED '%s' at GPIO %d\n",
Daniel Macka8dd18f2009-10-15 02:59:35 +0200122 KBUILD_MODNAME, template->name, template->gpio);
123
124 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200125}
126
127static void delete_lt3593_led(struct lt3593_led_data *led)
128{
129 if (!gpio_is_valid(led->gpio))
130 return;
131
132 led_classdev_unregister(&led->cdev);
133 cancel_work_sync(&led->work);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200134}
135
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500136static int lt3593_led_probe(struct platform_device *pdev)
Daniel Macka8dd18f2009-10-15 02:59:35 +0200137{
138 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
139 struct lt3593_led_data *leds_data;
140 int i, ret = 0;
141
142 if (!pdata)
143 return -EBUSY;
144
Bryan Wu0a8d9d42012-07-04 11:51:51 +0800145 leds_data = devm_kzalloc(&pdev->dev,
146 sizeof(struct lt3593_led_data) * pdata->num_leds,
147 GFP_KERNEL);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200148 if (!leds_data)
149 return -ENOMEM;
150
151 for (i = 0; i < pdata->num_leds; i++) {
152 ret = create_lt3593_led(&pdata->leds[i], &leds_data[i],
153 &pdev->dev);
154 if (ret < 0)
155 goto err;
156 }
157
158 platform_set_drvdata(pdev, leds_data);
159
160 return 0;
161
162err:
163 for (i = i - 1; i >= 0; i--)
164 delete_lt3593_led(&leds_data[i]);
165
Daniel Macka8dd18f2009-10-15 02:59:35 +0200166 return ret;
167}
168
Bill Pemberton678e8a62012-11-19 13:26:00 -0500169static int lt3593_led_remove(struct platform_device *pdev)
Daniel Macka8dd18f2009-10-15 02:59:35 +0200170{
171 int i;
172 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
173 struct lt3593_led_data *leds_data;
174
175 leds_data = platform_get_drvdata(pdev);
176
177 for (i = 0; i < pdata->num_leds; i++)
178 delete_lt3593_led(&leds_data[i]);
179
Daniel Macka8dd18f2009-10-15 02:59:35 +0200180 return 0;
181}
182
183static struct platform_driver lt3593_led_driver = {
184 .probe = lt3593_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500185 .remove = lt3593_led_remove,
Daniel Macka8dd18f2009-10-15 02:59:35 +0200186 .driver = {
187 .name = "leds-lt3593",
188 .owner = THIS_MODULE,
189 },
190};
191
Axel Lin892a8842012-01-10 15:09:24 -0800192module_platform_driver(lt3593_led_driver);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200193
194MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
195MODULE_DESCRIPTION("LED driver for LT3593 controllers");
196MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800197MODULE_ALIAS("platform:leds-lt3593");