blob: 9f41124765cc5503044fc1af4fd5cba4a992d73c [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>
Daniel Macka8dd18f2009-10-15 02:59:35 +020020#include <linux/platform_device.h>
21#include <linux/leds.h>
22#include <linux/workqueue.h>
23#include <linux/delay.h>
24#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040026#include <linux/module.h>
Daniel Macka8dd18f2009-10-15 02:59:35 +020027
28struct lt3593_led_data {
29 struct led_classdev cdev;
30 unsigned gpio;
31 struct work_struct work;
32 u8 new_level;
33};
34
35static void lt3593_led_work(struct work_struct *work)
36{
37 int pulses;
38 struct lt3593_led_data *led_dat =
39 container_of(work, struct lt3593_led_data, work);
40
41 /*
42 * The LT3593 resets its internal current level register to the maximum
43 * level on the first falling edge on the control pin. Each following
44 * falling edge decreases the current level by 625uA. Up to 32 pulses
45 * can be sent, so the maximum power reduction is 20mA.
46 * After a timeout of 128us, the value is taken from the register and
47 * applied is to the output driver.
48 */
49
50 if (led_dat->new_level == 0) {
51 gpio_set_value_cansleep(led_dat->gpio, 0);
52 return;
53 }
54
55 pulses = 32 - (led_dat->new_level * 32) / 255;
56
57 if (pulses == 0) {
58 gpio_set_value_cansleep(led_dat->gpio, 0);
59 mdelay(1);
60 gpio_set_value_cansleep(led_dat->gpio, 1);
61 return;
62 }
63
64 gpio_set_value_cansleep(led_dat->gpio, 1);
65
66 while (pulses--) {
67 gpio_set_value_cansleep(led_dat->gpio, 0);
68 udelay(1);
69 gpio_set_value_cansleep(led_dat->gpio, 1);
70 udelay(1);
71 }
72}
73
74static void lt3593_led_set(struct led_classdev *led_cdev,
75 enum led_brightness value)
76{
77 struct lt3593_led_data *led_dat =
78 container_of(led_cdev, struct lt3593_led_data, cdev);
79
80 led_dat->new_level = value;
81 schedule_work(&led_dat->work);
82}
83
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050084static int create_lt3593_led(const struct gpio_led *template,
Daniel Macka8dd18f2009-10-15 02:59:35 +020085 struct lt3593_led_data *led_dat, struct device *parent)
86{
87 int ret, state;
88
89 /* skip leds on GPIOs that aren't available */
90 if (!gpio_is_valid(template->gpio)) {
Sachin Kamat30e5c3e2012-11-25 12:10:27 +053091 dev_info(parent, "%s: skipping unavailable LT3593 LED at gpio %d (%s)\n",
Daniel Macka8dd18f2009-10-15 02:59:35 +020092 KBUILD_MODNAME, template->gpio, template->name);
93 return 0;
94 }
95
Daniel Macka8dd18f2009-10-15 02:59:35 +020096 led_dat->cdev.name = template->name;
97 led_dat->cdev.default_trigger = template->default_trigger;
98 led_dat->gpio = template->gpio;
99
100 led_dat->cdev.brightness_set = lt3593_led_set;
101
102 state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
103 led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
104
105 if (!template->retain_state_suspended)
106 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
107
Jingoo Han84f69422013-03-07 18:37:38 -0800108 ret = devm_gpio_request_one(parent, template->gpio, state ?
109 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
110 template->name);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200111 if (ret < 0)
Jingoo Han507d967bc2012-10-23 05:17:56 -0700112 return ret;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200113
114 INIT_WORK(&led_dat->work, lt3593_led_work);
115
116 ret = led_classdev_register(parent, &led_dat->cdev);
117 if (ret < 0)
Sachin Kamat95a8ff82012-11-25 10:14:27 +0530118 return ret;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200119
Sachin Kamat30e5c3e2012-11-25 12:10:27 +0530120 dev_info(parent, "%s: registered LT3593 LED '%s' at GPIO %d\n",
Daniel Macka8dd18f2009-10-15 02:59:35 +0200121 KBUILD_MODNAME, template->name, template->gpio);
122
123 return 0;
Daniel Macka8dd18f2009-10-15 02:59:35 +0200124}
125
126static void delete_lt3593_led(struct lt3593_led_data *led)
127{
128 if (!gpio_is_valid(led->gpio))
129 return;
130
131 led_classdev_unregister(&led->cdev);
132 cancel_work_sync(&led->work);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200133}
134
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500135static int lt3593_led_probe(struct platform_device *pdev)
Daniel Macka8dd18f2009-10-15 02:59:35 +0200136{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700137 struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200138 struct lt3593_led_data *leds_data;
139 int i, ret = 0;
140
141 if (!pdata)
142 return -EBUSY;
143
Bryan Wu0a8d9d42012-07-04 11:51:51 +0800144 leds_data = devm_kzalloc(&pdev->dev,
145 sizeof(struct lt3593_led_data) * pdata->num_leds,
146 GFP_KERNEL);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200147 if (!leds_data)
148 return -ENOMEM;
149
150 for (i = 0; i < pdata->num_leds; i++) {
151 ret = create_lt3593_led(&pdata->leds[i], &leds_data[i],
152 &pdev->dev);
153 if (ret < 0)
154 goto err;
155 }
156
157 platform_set_drvdata(pdev, leds_data);
158
159 return 0;
160
161err:
162 for (i = i - 1; i >= 0; i--)
163 delete_lt3593_led(&leds_data[i]);
164
Daniel Macka8dd18f2009-10-15 02:59:35 +0200165 return ret;
166}
167
Bill Pemberton678e8a62012-11-19 13:26:00 -0500168static int lt3593_led_remove(struct platform_device *pdev)
Daniel Macka8dd18f2009-10-15 02:59:35 +0200169{
170 int i;
Jingoo Han87aae1e2013-07-30 01:07:35 -0700171 struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200172 struct lt3593_led_data *leds_data;
173
174 leds_data = platform_get_drvdata(pdev);
175
176 for (i = 0; i < pdata->num_leds; i++)
177 delete_lt3593_led(&leds_data[i]);
178
Daniel Macka8dd18f2009-10-15 02:59:35 +0200179 return 0;
180}
181
182static struct platform_driver lt3593_led_driver = {
183 .probe = lt3593_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500184 .remove = lt3593_led_remove,
Daniel Macka8dd18f2009-10-15 02:59:35 +0200185 .driver = {
186 .name = "leds-lt3593",
Daniel Macka8dd18f2009-10-15 02:59:35 +0200187 },
188};
189
Axel Lin892a8842012-01-10 15:09:24 -0800190module_platform_driver(lt3593_led_driver);
Daniel Macka8dd18f2009-10-15 02:59:35 +0200191
192MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
193MODULE_DESCRIPTION("LED driver for LT3593 controllers");
194MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800195MODULE_ALIAS("platform:leds-lt3593");