mvebu: fix portability issues in the image makefile code
[openwrt/openwrt.git] / target / linux / mvebu / patches-4.1 / 051-leds_tlc59116_add_driver.patch
1 The TLC59116 is an I2C bus controlled 16-channel LED driver. Each LED
2 output has its own 8-bit fixed-frequency PWM controller to control the
3 brightness of the LED.
4
5 This is based on a driver from Belkin, but has been extensively
6 rewritten.
7
8 Signed-off-by: Andrew Lunn <andrew@lunn.ch>
9 ---
10 drivers/leds/Kconfig | 8 ++
11 drivers/leds/Makefile | 1 +
12 drivers/leds/leds-tlc59116.c | 252 +++++++++++++++++++++++++++++++++++++++++++
13 3 files changed, 261 insertions(+)
14 create mode 100644 drivers/leds/leds-tlc59116.c
15
16 --- a/drivers/leds/Kconfig
17 +++ b/drivers/leds/Kconfig
18 @@ -467,6 +467,14 @@ config LEDS_TCA6507
19 LED driver chips accessed via the I2C bus.
20 Driver support brightness control and hardware-assisted blinking.
21
22 +config LEDS_TLC59116
23 + tristate "LED driver for TLC59116F controllers"
24 + depends on LEDS_CLASS && I2C
25 + select REGMAP_I2C
26 + help
27 + This option enables support for Texas Instruments TLC59116F
28 + LED controller.
29 +
30 config LEDS_MAX8997
31 tristate "LED support for MAX8997 PMIC"
32 depends on LEDS_CLASS && MFD_MAX8997
33 --- a/drivers/leds/Makefile
34 +++ b/drivers/leds/Makefile
35 @@ -31,6 +31,7 @@ obj-$(CONFIG_LEDS_LP8501) += leds-lp850
36 obj-$(CONFIG_LEDS_LP8788) += leds-lp8788.o
37 obj-$(CONFIG_LEDS_LP8860) += leds-lp8860.o
38 obj-$(CONFIG_LEDS_TCA6507) += leds-tca6507.o
39 +obj-$(CONFIG_LEDS_TLC59116) += leds-tlc59116.o
40 obj-$(CONFIG_LEDS_CLEVO_MAIL) += leds-clevo-mail.o
41 obj-$(CONFIG_LEDS_IPAQ_MICRO) += leds-ipaq-micro.o
42 obj-$(CONFIG_LEDS_HP6XX) += leds-hp6xx.o
43 --- /dev/null
44 +++ b/drivers/leds/leds-tlc59116.c
45 @@ -0,0 +1,252 @@
46 +/*
47 + * Copyright 2014 Belkin Inc.
48 + * Copyright 2014 Andrew Lunn <andrew@lunn.ch>
49 + *
50 + * This program is free software; you can redistribute it and/or modify
51 + * it under the terms of the GNU General Public License as published by
52 + * the Free Software Foundation; version 2 of the License.
53 + */
54 +
55 +#include <linux/i2c.h>
56 +#include <linux/leds.h>
57 +#include <linux/module.h>
58 +#include <linux/of.h>
59 +#include <linux/regmap.h>
60 +#include <linux/slab.h>
61 +#include <linux/workqueue.h>
62 +
63 +#define TLC59116_LEDS 16
64 +
65 +#define TLC59116_REG_MODE1 0x00
66 +#define MODE1_RESPON_ADDR_MASK 0xF0
67 +#define MODE1_NORMAL_MODE (0 << 4)
68 +#define MODE1_SPEED_MODE (1 << 4)
69 +
70 +#define TLC59116_REG_MODE2 0x01
71 +#define MODE2_DIM (0 << 5)
72 +#define MODE2_BLINK (1 << 5)
73 +#define MODE2_OCH_STOP (0 << 3)
74 +#define MODE2_OCH_ACK (1 << 3)
75 +
76 +#define TLC59116_REG_PWM(x) (0x02 + (x))
77 +
78 +#define TLC59116_REG_GRPPWM 0x12
79 +#define TLC59116_REG_GRPFREQ 0x13
80 +
81 +/* LED Driver Output State, determine the source that drives LED outputs */
82 +#define TLC59116_REG_LEDOUT(x) (0x14 + ((x) >> 2))
83 +#define TLC59116_LED_OFF 0x0 /* Output LOW */
84 +#define TLC59116_LED_ON 0x1 /* Output HI-Z */
85 +#define TLC59116_DIM 0x2 /* Dimming */
86 +#define TLC59116_BLINK 0x3 /* Blinking */
87 +#define LED_MASK 0x3
88 +
89 +#define ldev_to_led(c) container_of(c, struct tlc59116_led, ldev)
90 +#define work_to_led(work) container_of(work, struct tlc59116_led, work)
91 +
92 +struct tlc59116_led {
93 + bool active;
94 + struct regmap *regmap;
95 + unsigned int led_no;
96 + struct led_classdev ldev;
97 + struct work_struct work;
98 +};
99 +
100 +struct tlc59116_priv {
101 + struct tlc59116_led leds[TLC59116_LEDS];
102 +};
103 +
104 +static int
105 +tlc59116_set_mode(struct regmap *regmap, u8 mode)
106 +{
107 + int err;
108 + u8 val;
109 +
110 + if ((mode != MODE2_DIM) && (mode != MODE2_BLINK))
111 + mode = MODE2_DIM;
112 +
113 + /* Configure MODE1 register */
114 + err = regmap_write(regmap, TLC59116_REG_MODE1, MODE1_NORMAL_MODE);
115 + if (err)
116 + return err;
117 +
118 + /* Configure MODE2 Reg */
119 + val = MODE2_OCH_STOP | mode;
120 +
121 + return regmap_write(regmap, TLC59116_REG_MODE2, val);
122 +}
123 +
124 +static int
125 +tlc59116_set_led(struct tlc59116_led *led, u8 val)
126 +{
127 + struct regmap *regmap = led->regmap;
128 + unsigned int i = (led->led_no % 4) * 2;
129 + unsigned int addr = TLC59116_REG_LEDOUT(led->led_no);
130 + unsigned int mask = LED_MASK << i;
131 +
132 + val = val << i;
133 +
134 + return regmap_update_bits(regmap, addr, mask, val);
135 +}
136 +
137 +static void
138 +tlc59116_led_work(struct work_struct *work)
139 +{
140 + struct tlc59116_led *led = work_to_led(work);
141 + struct regmap *regmap = led->regmap;
142 + int err;
143 + u8 pwm;
144 +
145 + pwm = TLC59116_REG_PWM(led->led_no);
146 + err = regmap_write(regmap, pwm, led->ldev.brightness);
147 + if (err)
148 + dev_err(led->ldev.dev, "Failed setting brightness\n");
149 +}
150 +
151 +static void
152 +tlc59116_led_set(struct led_classdev *led_cdev, enum led_brightness value)
153 +{
154 + struct tlc59116_led *led = ldev_to_led(led_cdev);
155 +
156 + led->ldev.brightness = value;
157 + schedule_work(&led->work);
158 +}
159 +
160 +static void
161 +tlc59116_destroy_devices(struct tlc59116_priv *priv, unsigned int i)
162 +{
163 + while (--i >= 0) {
164 + if (priv->leds[i].active) {
165 + led_classdev_unregister(&priv->leds[i].ldev);
166 + cancel_work_sync(&priv->leds[i].work);
167 + }
168 + }
169 +}
170 +
171 +static int
172 +tlc59116_configure(struct device *dev,
173 + struct tlc59116_priv *priv,
174 + struct regmap *regmap)
175 +{
176 + unsigned int i;
177 + int err = 0;
178 +
179 + tlc59116_set_mode(regmap, MODE2_DIM);
180 + for (i = 0; i < TLC59116_LEDS; i++) {
181 + struct tlc59116_led *led = &priv->leds[i];
182 +
183 + if (!led->active)
184 + continue;
185 +
186 + led->regmap = regmap;
187 + led->led_no = i;
188 + led->ldev.brightness_set = tlc59116_led_set;
189 + led->ldev.max_brightness = LED_FULL;
190 + INIT_WORK(&led->work, tlc59116_led_work);
191 + err = led_classdev_register(dev, &led->ldev);
192 + if (err < 0) {
193 + dev_err(dev, "couldn't register LED %s\n",
194 + led->ldev.name);
195 + goto exit;
196 + }
197 + tlc59116_set_led(led, TLC59116_DIM);
198 + }
199 +
200 + return 0;
201 +
202 +exit:
203 + tlc59116_destroy_devices(priv, i);
204 + return err;
205 +}
206 +
207 +static const struct regmap_config tlc59116_regmap = {
208 + .reg_bits = 8,
209 + .val_bits = 8,
210 + .max_register = 0x1e,
211 +};
212 +
213 +static int
214 +tlc59116_probe(struct i2c_client *client,
215 + const struct i2c_device_id *id)
216 +{
217 + struct tlc59116_priv *priv = i2c_get_clientdata(client);
218 + struct device *dev = &client->dev;
219 + struct device_node *np = client->dev.of_node, *child;
220 + struct regmap *regmap;
221 + int err, count, reg;
222 +
223 + count = of_get_child_count(np);
224 + if (!count || count > TLC59116_LEDS)
225 + return -EINVAL;
226 +
227 + if (!i2c_check_functionality(client->adapter,
228 + I2C_FUNC_SMBUS_BYTE_DATA))
229 + return -EIO;
230 +
231 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
232 + if (!priv)
233 + return -ENOMEM;
234 +
235 + regmap = devm_regmap_init_i2c(client, &tlc59116_regmap);
236 + if (IS_ERR(regmap)) {
237 + err = PTR_ERR(regmap);
238 + dev_err(dev, "Failed to allocate register map: %d\n", err);
239 + return err;
240 + }
241 +
242 + i2c_set_clientdata(client, priv);
243 +
244 + for_each_child_of_node(np, child) {
245 + err = of_property_read_u32(child, "reg", &reg);
246 + if (err)
247 + return err;
248 + if (reg < 0 || reg >= TLC59116_LEDS)
249 + return -EINVAL;
250 + if (priv->leds[reg].active)
251 + return -EINVAL;
252 + priv->leds[reg].active = true;
253 + priv->leds[reg].ldev.name =
254 + of_get_property(child, "label", NULL) ? : child->name;
255 + priv->leds[reg].ldev.default_trigger =
256 + of_get_property(child, "linux,default-trigger", NULL);
257 + }
258 + return tlc59116_configure(dev, priv, regmap);
259 +}
260 +
261 +static int
262 +tlc59116_remove(struct i2c_client *client)
263 +{
264 + struct tlc59116_priv *priv = i2c_get_clientdata(client);
265 +
266 + tlc59116_destroy_devices(priv, TLC59116_LEDS);
267 +
268 + return 0;
269 +}
270 +
271 +static const struct of_device_id of_tlc59116_leds_match[] = {
272 + { .compatible = "ti,tlc59116", },
273 + {},
274 +};
275 +MODULE_DEVICE_TABLE(of, of_tlc59116_leds_match);
276 +
277 +static const struct i2c_device_id tlc59116_id[] = {
278 + { "tlc59116" },
279 + {},
280 +};
281 +MODULE_DEVICE_TABLE(i2c, tlc59116_id);
282 +
283 +static struct i2c_driver tlc59116_driver = {
284 + .driver = {
285 + .name = "tlc59116",
286 + .of_match_table = of_match_ptr(of_tlc59116_leds_match),
287 + },
288 + .probe = tlc59116_probe,
289 + .remove = tlc59116_remove,
290 + .id_table = tlc59116_id,
291 +};
292 +
293 +module_i2c_driver(tlc59116_driver);
294 +
295 +MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
296 +MODULE_LICENSE("GPL");
297 +MODULE_DESCRIPTION("TLC59116 LED driver");