blob: 3f9675bd214a404fcd5e771ed87bf4079dacf938 [file] [log] [blame]
Kim, Milo33b3a562013-07-09 02:11:37 -07001/*
2 * TI LP8501 9 channel LED Driver
3 *
4 * Copyright (C) 2013 Texas Instruments
5 *
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/delay.h>
15#include <linux/firmware.h>
16#include <linux/i2c.h>
17#include <linux/init.h>
18#include <linux/leds.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
Sachin Kamatc68f46d2013-09-28 04:38:30 -070021#include <linux/of.h>
Kim, Milo33b3a562013-07-09 02:11:37 -070022#include <linux/platform_data/leds-lp55xx.h>
23#include <linux/slab.h>
24
25#include "leds-lp55xx-common.h"
26
27#define LP8501_PROGRAM_LENGTH 32
28#define LP8501_MAX_LEDS 9
29
30/* Registers */
31#define LP8501_REG_ENABLE 0x00
32#define LP8501_ENABLE BIT(6)
33#define LP8501_EXEC_M 0x3F
34#define LP8501_EXEC_ENG1_M 0x30
35#define LP8501_EXEC_ENG2_M 0x0C
36#define LP8501_EXEC_ENG3_M 0x03
37#define LP8501_RUN_ENG1 0x20
38#define LP8501_RUN_ENG2 0x08
39#define LP8501_RUN_ENG3 0x02
40
41#define LP8501_REG_OP_MODE 0x01
42#define LP8501_MODE_ENG1_M 0x30
43#define LP8501_MODE_ENG2_M 0x0C
44#define LP8501_MODE_ENG3_M 0x03
45#define LP8501_LOAD_ENG1 0x10
46#define LP8501_LOAD_ENG2 0x04
47#define LP8501_LOAD_ENG3 0x01
48
49#define LP8501_REG_PWR_CONFIG 0x05
50#define LP8501_PWR_CONFIG_M 0x03
51
52#define LP8501_REG_LED_PWM_BASE 0x16
53
54#define LP8501_REG_LED_CURRENT_BASE 0x26
55
56#define LP8501_REG_CONFIG 0x36
57#define LP8501_PWM_PSAVE BIT(7)
58#define LP8501_AUTO_INC BIT(6)
59#define LP8501_PWR_SAVE BIT(5)
60#define LP8501_CP_AUTO 0x18
61#define LP8501_INT_CLK BIT(0)
62#define LP8501_DEFAULT_CFG \
63 (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE | LP8501_CP_AUTO)
64
65#define LP8501_REG_RESET 0x3D
66#define LP8501_RESET 0xFF
67
68#define LP8501_REG_PROG_PAGE_SEL 0x4F
69#define LP8501_PAGE_ENG1 0
70#define LP8501_PAGE_ENG2 1
71#define LP8501_PAGE_ENG3 2
72
73#define LP8501_REG_PROG_MEM 0x50
74
75#define LP8501_ENG1_IS_LOADING(mode) \
76 ((mode & LP8501_MODE_ENG1_M) == LP8501_LOAD_ENG1)
77#define LP8501_ENG2_IS_LOADING(mode) \
78 ((mode & LP8501_MODE_ENG2_M) == LP8501_LOAD_ENG2)
79#define LP8501_ENG3_IS_LOADING(mode) \
80 ((mode & LP8501_MODE_ENG3_M) == LP8501_LOAD_ENG3)
81
82static inline void lp8501_wait_opmode_done(void)
83{
84 usleep_range(1000, 2000);
85}
86
87static void lp8501_set_led_current(struct lp55xx_led *led, u8 led_current)
88{
89 led->led_current = led_current;
90 lp55xx_write(led->chip, LP8501_REG_LED_CURRENT_BASE + led->chan_nr,
91 led_current);
92}
93
94static int lp8501_post_init_device(struct lp55xx_chip *chip)
95{
96 int ret;
97 u8 val = LP8501_DEFAULT_CFG;
98
99 ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
100 if (ret)
101 return ret;
102
103 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
104 usleep_range(1000, 2000);
105
106 if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT)
107 val |= LP8501_INT_CLK;
108
109 ret = lp55xx_write(chip, LP8501_REG_CONFIG, val);
110 if (ret)
111 return ret;
112
113 /* Power selection for each output */
114 return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG,
115 LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
116}
117
118static void lp8501_load_engine(struct lp55xx_chip *chip)
119{
120 enum lp55xx_engine_index idx = chip->engine_idx;
121 u8 mask[] = {
122 [LP55XX_ENGINE_1] = LP8501_MODE_ENG1_M,
123 [LP55XX_ENGINE_2] = LP8501_MODE_ENG2_M,
124 [LP55XX_ENGINE_3] = LP8501_MODE_ENG3_M,
125 };
126
127 u8 val[] = {
128 [LP55XX_ENGINE_1] = LP8501_LOAD_ENG1,
129 [LP55XX_ENGINE_2] = LP8501_LOAD_ENG2,
130 [LP55XX_ENGINE_3] = LP8501_LOAD_ENG3,
131 };
132
133 u8 page_sel[] = {
134 [LP55XX_ENGINE_1] = LP8501_PAGE_ENG1,
135 [LP55XX_ENGINE_2] = LP8501_PAGE_ENG2,
136 [LP55XX_ENGINE_3] = LP8501_PAGE_ENG3,
137 };
138
139 lp55xx_update_bits(chip, LP8501_REG_OP_MODE, mask[idx], val[idx]);
140
141 lp8501_wait_opmode_done();
142
143 lp55xx_write(chip, LP8501_REG_PROG_PAGE_SEL, page_sel[idx]);
144}
145
146static void lp8501_stop_engine(struct lp55xx_chip *chip)
147{
148 lp55xx_write(chip, LP8501_REG_OP_MODE, 0);
149 lp8501_wait_opmode_done();
150}
151
152static void lp8501_turn_off_channels(struct lp55xx_chip *chip)
153{
154 int i;
155
156 for (i = 0; i < LP8501_MAX_LEDS; i++)
157 lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + i, 0);
158}
159
160static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
161{
162 int ret;
163 u8 mode;
164 u8 exec;
165
166 /* stop engine */
167 if (!start) {
168 lp8501_stop_engine(chip);
169 lp8501_turn_off_channels(chip);
170 return;
171 }
172
173 /*
174 * To run the engine,
175 * operation mode and enable register should updated at the same time
176 */
177
178 ret = lp55xx_read(chip, LP8501_REG_OP_MODE, &mode);
179 if (ret)
180 return;
181
182 ret = lp55xx_read(chip, LP8501_REG_ENABLE, &exec);
183 if (ret)
184 return;
185
186 /* change operation mode to RUN only when each engine is loading */
187 if (LP8501_ENG1_IS_LOADING(mode)) {
188 mode = (mode & ~LP8501_MODE_ENG1_M) | LP8501_RUN_ENG1;
189 exec = (exec & ~LP8501_EXEC_ENG1_M) | LP8501_RUN_ENG1;
190 }
191
192 if (LP8501_ENG2_IS_LOADING(mode)) {
193 mode = (mode & ~LP8501_MODE_ENG2_M) | LP8501_RUN_ENG2;
194 exec = (exec & ~LP8501_EXEC_ENG2_M) | LP8501_RUN_ENG2;
195 }
196
197 if (LP8501_ENG3_IS_LOADING(mode)) {
198 mode = (mode & ~LP8501_MODE_ENG3_M) | LP8501_RUN_ENG3;
199 exec = (exec & ~LP8501_EXEC_ENG3_M) | LP8501_RUN_ENG3;
200 }
201
202 lp55xx_write(chip, LP8501_REG_OP_MODE, mode);
203 lp8501_wait_opmode_done();
204
205 lp55xx_update_bits(chip, LP8501_REG_ENABLE, LP8501_EXEC_M, exec);
206}
207
208static int lp8501_update_program_memory(struct lp55xx_chip *chip,
209 const u8 *data, size_t size)
210{
211 u8 pattern[LP8501_PROGRAM_LENGTH] = {0};
212 unsigned cmd;
213 char c[3];
214 int update_size;
215 int nrchars;
216 int offset = 0;
217 int ret;
218 int i;
219
220 /* clear program memory before updating */
221 for (i = 0; i < LP8501_PROGRAM_LENGTH; i++)
222 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, 0);
223
224 i = 0;
225 while ((offset < size - 1) && (i < LP8501_PROGRAM_LENGTH)) {
226 /* separate sscanfs because length is working only for %s */
227 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
228 if (ret != 1)
229 goto err;
230
231 ret = sscanf(c, "%2x", &cmd);
232 if (ret != 1)
233 goto err;
234
235 pattern[i] = (u8)cmd;
236 offset += nrchars;
237 i++;
238 }
239
240 /* Each instruction is 16bit long. Check that length is even */
241 if (i % 2)
242 goto err;
243
244 update_size = i;
245 for (i = 0; i < update_size; i++)
246 lp55xx_write(chip, LP8501_REG_PROG_MEM + i, pattern[i]);
247
248 return 0;
249
250err:
251 dev_err(&chip->cl->dev, "wrong pattern format\n");
252 return -EINVAL;
253}
254
255static void lp8501_firmware_loaded(struct lp55xx_chip *chip)
256{
257 const struct firmware *fw = chip->fw;
258
259 if (fw->size > LP8501_PROGRAM_LENGTH) {
260 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
261 fw->size);
262 return;
263 }
264
265 /*
Pavel Machekbfb18d82014-01-10 00:14:05 +0100266 * Program memory sequence
Kim, Milo33b3a562013-07-09 02:11:37 -0700267 * 1) set engine mode to "LOAD"
268 * 2) write firmware data into program memory
269 */
270
271 lp8501_load_engine(chip);
272 lp8501_update_program_memory(chip, fw->data, fw->size);
273}
274
Andrew Lunn95b2af62015-08-20 12:22:57 +0200275static int lp8501_led_brightness(struct lp55xx_led *led)
Kim, Milo33b3a562013-07-09 02:11:37 -0700276{
Kim, Milo33b3a562013-07-09 02:11:37 -0700277 struct lp55xx_chip *chip = led->chip;
Andrew Lunn95b2af62015-08-20 12:22:57 +0200278 int ret;
Kim, Milo33b3a562013-07-09 02:11:37 -0700279
280 mutex_lock(&chip->lock);
Andrew Lunn95b2af62015-08-20 12:22:57 +0200281 ret = lp55xx_write(chip, LP8501_REG_LED_PWM_BASE + led->chan_nr,
Kim, Milo33b3a562013-07-09 02:11:37 -0700282 led->brightness);
283 mutex_unlock(&chip->lock);
Andrew Lunn95b2af62015-08-20 12:22:57 +0200284
285 return ret;
Kim, Milo33b3a562013-07-09 02:11:37 -0700286}
287
288/* Chip specific configurations */
289static struct lp55xx_device_config lp8501_cfg = {
290 .reset = {
291 .addr = LP8501_REG_RESET,
292 .val = LP8501_RESET,
293 },
294 .enable = {
295 .addr = LP8501_REG_ENABLE,
296 .val = LP8501_ENABLE,
297 },
298 .max_channel = LP8501_MAX_LEDS,
299 .post_init_device = lp8501_post_init_device,
Andrew Lunn95b2af62015-08-20 12:22:57 +0200300 .brightness_fn = lp8501_led_brightness,
Kim, Milo33b3a562013-07-09 02:11:37 -0700301 .set_led_current = lp8501_set_led_current,
302 .firmware_cb = lp8501_firmware_loaded,
303 .run_engine = lp8501_run_engine,
304};
305
306static int lp8501_probe(struct i2c_client *client,
307 const struct i2c_device_id *id)
308{
309 int ret;
310 struct lp55xx_chip *chip;
311 struct lp55xx_led *led;
Milo Kimed1333522015-08-24 16:09:55 +0900312 struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
Kim, Milo33b3a562013-07-09 02:11:37 -0700313 struct device_node *np = client->dev.of_node;
314
Milo Kimed1333522015-08-24 16:09:55 +0900315 if (!pdata) {
Kim, Milo33b3a562013-07-09 02:11:37 -0700316 if (np) {
Milo Kimed1333522015-08-24 16:09:55 +0900317 pdata = lp55xx_of_populate_pdata(&client->dev, np);
318 if (IS_ERR(pdata))
319 return PTR_ERR(pdata);
Kim, Milo33b3a562013-07-09 02:11:37 -0700320 } else {
321 dev_err(&client->dev, "no platform data\n");
322 return -EINVAL;
323 }
324 }
Kim, Milo33b3a562013-07-09 02:11:37 -0700325
326 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
327 if (!chip)
328 return -ENOMEM;
329
330 led = devm_kzalloc(&client->dev,
331 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
332 if (!led)
333 return -ENOMEM;
334
335 chip->cl = client;
336 chip->pdata = pdata;
337 chip->cfg = &lp8501_cfg;
338
339 mutex_init(&chip->lock);
340
341 i2c_set_clientdata(client, led);
342
343 ret = lp55xx_init_device(chip);
344 if (ret)
345 goto err_init;
346
347 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
348
349 ret = lp55xx_register_leds(led, chip);
350 if (ret)
351 goto err_register_leds;
352
353 ret = lp55xx_register_sysfs(chip);
354 if (ret) {
355 dev_err(&client->dev, "registering sysfs failed\n");
356 goto err_register_sysfs;
357 }
358
359 return 0;
360
361err_register_sysfs:
362 lp55xx_unregister_leds(led, chip);
363err_register_leds:
364 lp55xx_deinit_device(chip);
365err_init:
366 return ret;
367}
368
369static int lp8501_remove(struct i2c_client *client)
370{
371 struct lp55xx_led *led = i2c_get_clientdata(client);
372 struct lp55xx_chip *chip = led->chip;
373
374 lp8501_stop_engine(chip);
375 lp55xx_unregister_sysfs(chip);
376 lp55xx_unregister_leds(led, chip);
377 lp55xx_deinit_device(chip);
378
379 return 0;
380}
381
382static const struct i2c_device_id lp8501_id[] = {
383 { "lp8501", 0 },
384 { }
385};
386MODULE_DEVICE_TABLE(i2c, lp8501_id);
387
388#ifdef CONFIG_OF
389static const struct of_device_id of_lp8501_leds_match[] = {
390 { .compatible = "ti,lp8501", },
391 {},
392};
393
394MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
395#endif
396
397static struct i2c_driver lp8501_driver = {
398 .driver = {
399 .name = "lp8501",
400 .of_match_table = of_match_ptr(of_lp8501_leds_match),
401 },
402 .probe = lp8501_probe,
403 .remove = lp8501_remove,
404 .id_table = lp8501_id,
405};
406
407module_i2c_driver(lp8501_driver);
408
Masanari Iida336af372015-03-30 02:50:10 -0700409MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver");
Kim, Milo33b3a562013-07-09 02:11:37 -0700410MODULE_AUTHOR("Milo Kim");
411MODULE_LICENSE("GPL");