leds: lp5523: make separate API for loading engine
[deliverable/linux.git] / drivers / leds / leds-lp5523.c
1 /*
2 * lp5523.c - LP5523 LED Driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Copyright (C) 2012 Texas Instruments
6 *
7 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
8 * Milo(Woogyom) Kim <milo.kim@ti.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25 #include <linux/delay.h>
26 #include <linux/firmware.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/leds.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/platform_data/leds-lp55xx.h>
33 #include <linux/slab.h>
34
35 #include "leds-lp55xx-common.h"
36
37 #define LP5523_PROGRAM_LENGTH 32
38 #define LP5523_MAX_LEDS 9
39
40 /* Registers */
41 #define LP5523_REG_ENABLE 0x00
42 #define LP5523_REG_OP_MODE 0x01
43 #define LP5523_REG_ENABLE_LEDS_MSB 0x04
44 #define LP5523_REG_ENABLE_LEDS_LSB 0x05
45 #define LP5523_REG_LED_PWM_BASE 0x16
46 #define LP5523_REG_LED_CURRENT_BASE 0x26
47 #define LP5523_REG_CONFIG 0x36
48 #define LP5523_REG_STATUS 0x3A
49 #define LP5523_REG_RESET 0x3D
50 #define LP5523_REG_LED_TEST_CTRL 0x41
51 #define LP5523_REG_LED_TEST_ADC 0x42
52 #define LP5523_REG_PROG_PAGE_SEL 0x4F
53 #define LP5523_REG_PROG_MEM 0x50
54
55 /* Bit description in registers */
56 #define LP5523_ENABLE 0x40
57 #define LP5523_AUTO_INC 0x40
58 #define LP5523_PWR_SAVE 0x20
59 #define LP5523_PWM_PWR_SAVE 0x04
60 #define LP5523_CP_AUTO 0x18
61 #define LP5523_AUTO_CLK 0x02
62
63 #define LP5523_EN_LEDTEST 0x80
64 #define LP5523_LEDTEST_DONE 0x80
65 #define LP5523_RESET 0xFF
66 #define LP5523_ADC_SHORTCIRC_LIM 80
67 #define LP5523_EXT_CLK_USED 0x08
68
69 /* Memory Page Selection */
70 #define LP5523_PAGE_ENG1 0
71 #define LP5523_PAGE_ENG2 1
72 #define LP5523_PAGE_ENG3 2
73
74 /* Program Memory Operations */
75 #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */
76 #define LP5523_MODE_ENG2_M 0x0C
77 #define LP5523_MODE_ENG3_M 0x03
78 #define LP5523_LOAD_ENG1 0x10
79 #define LP5523_LOAD_ENG2 0x04
80 #define LP5523_LOAD_ENG3 0x01
81
82 #define LP5523_ENG1_IS_LOADING(mode) \
83 ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
84 #define LP5523_ENG2_IS_LOADING(mode) \
85 ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
86 #define LP5523_ENG3_IS_LOADING(mode) \
87 ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
88
89 #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */
90 #define LP5523_EXEC_ENG2_M 0x0C
91 #define LP5523_EXEC_ENG3_M 0x03
92 #define LP5523_EXEC_M 0x3F
93 #define LP5523_RUN_ENG1 0x20
94 #define LP5523_RUN_ENG2 0x08
95 #define LP5523_RUN_ENG3 0x02
96
97 enum lp5523_chip_id {
98 LP5523,
99 LP55231,
100 };
101
102 static inline void lp5523_wait_opmode_done(void)
103 {
104 usleep_range(1000, 2000);
105 }
106
107 static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
108 {
109 led->led_current = led_current;
110 lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
111 led_current);
112 }
113
114 static int lp5523_post_init_device(struct lp55xx_chip *chip)
115 {
116 int ret;
117
118 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
119 if (ret)
120 return ret;
121
122 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
123 usleep_range(1000, 2000);
124
125 ret = lp55xx_write(chip, LP5523_REG_CONFIG,
126 LP5523_AUTO_INC | LP5523_PWR_SAVE |
127 LP5523_CP_AUTO | LP5523_AUTO_CLK |
128 LP5523_PWM_PWR_SAVE);
129 if (ret)
130 return ret;
131
132 /* turn on all leds */
133 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
134 if (ret)
135 return ret;
136
137 return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
138 }
139
140 static void lp5523_load_engine(struct lp55xx_chip *chip)
141 {
142 enum lp55xx_engine_index idx = chip->engine_idx;
143 u8 mask[] = {
144 [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
145 [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
146 [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
147 };
148
149 u8 val[] = {
150 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
151 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
152 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
153 };
154
155 lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
156
157 lp5523_wait_opmode_done();
158 }
159
160 static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
161 {
162 enum lp55xx_engine_index idx = chip->engine_idx;
163 u8 page_sel[] = {
164 [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
165 [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
166 [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
167 };
168
169 lp5523_load_engine(chip);
170
171 lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
172 }
173
174 static void lp5523_stop_engine(struct lp55xx_chip *chip)
175 {
176 lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
177 lp5523_wait_opmode_done();
178 }
179
180 static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
181 {
182 int i;
183
184 for (i = 0; i < LP5523_MAX_LEDS; i++)
185 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
186 }
187
188 static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
189 {
190 int ret;
191 u8 mode;
192 u8 exec;
193
194 /* stop engine */
195 if (!start) {
196 lp5523_stop_engine(chip);
197 lp5523_turn_off_channels(chip);
198 return;
199 }
200
201 /*
202 * To run the engine,
203 * operation mode and enable register should updated at the same time
204 */
205
206 ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
207 if (ret)
208 return;
209
210 ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
211 if (ret)
212 return;
213
214 /* change operation mode to RUN only when each engine is loading */
215 if (LP5523_ENG1_IS_LOADING(mode)) {
216 mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
217 exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
218 }
219
220 if (LP5523_ENG2_IS_LOADING(mode)) {
221 mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
222 exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
223 }
224
225 if (LP5523_ENG3_IS_LOADING(mode)) {
226 mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
227 exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
228 }
229
230 lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
231 lp5523_wait_opmode_done();
232
233 lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
234 }
235
236 static int lp5523_update_program_memory(struct lp55xx_chip *chip,
237 const u8 *data, size_t size)
238 {
239 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
240 unsigned cmd;
241 char c[3];
242 int update_size;
243 int nrchars;
244 int offset = 0;
245 int ret;
246 int i;
247
248 /* clear program memory before updating */
249 for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
250 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
251
252 i = 0;
253 while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
254 /* separate sscanfs because length is working only for %s */
255 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
256 if (ret != 1)
257 goto err;
258
259 ret = sscanf(c, "%2x", &cmd);
260 if (ret != 1)
261 goto err;
262
263 pattern[i] = (u8)cmd;
264 offset += nrchars;
265 i++;
266 }
267
268 /* Each instruction is 16bit long. Check that length is even */
269 if (i % 2)
270 goto err;
271
272 update_size = i;
273 for (i = 0; i < update_size; i++)
274 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
275
276 return 0;
277
278 err:
279 dev_err(&chip->cl->dev, "wrong pattern format\n");
280 return -EINVAL;
281 }
282
283 static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
284 {
285 const struct firmware *fw = chip->fw;
286
287 if (fw->size > LP5523_PROGRAM_LENGTH) {
288 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
289 fw->size);
290 return;
291 }
292
293 /*
294 * Program momery sequence
295 * 1) set engine mode to "LOAD"
296 * 2) write firmware data into program memory
297 */
298
299 lp5523_load_engine_and_select_page(chip);
300 lp5523_update_program_memory(chip, fw->data, fw->size);
301 }
302
303 static ssize_t lp5523_selftest(struct device *dev,
304 struct device_attribute *attr,
305 char *buf)
306 {
307 struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
308 struct lp55xx_chip *chip = led->chip;
309 struct lp55xx_platform_data *pdata = chip->pdata;
310 int i, ret, pos = 0;
311 u8 status, adc, vdd;
312
313 mutex_lock(&chip->lock);
314
315 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
316 if (ret < 0)
317 goto fail;
318
319 /* Check that ext clock is really in use if requested */
320 if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
321 if ((status & LP5523_EXT_CLK_USED) == 0)
322 goto fail;
323 }
324
325 /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
326 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
327 usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
328 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
329 if (ret < 0)
330 goto fail;
331
332 if (!(status & LP5523_LEDTEST_DONE))
333 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
334
335 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
336 if (ret < 0)
337 goto fail;
338
339 vdd--; /* There may be some fluctuation in measurement */
340
341 for (i = 0; i < LP5523_MAX_LEDS; i++) {
342 /* Skip non-existing channels */
343 if (pdata->led_config[i].led_current == 0)
344 continue;
345
346 /* Set default current */
347 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
348 pdata->led_config[i].led_current);
349
350 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
351 /* let current stabilize 2 - 4ms before measurements start */
352 usleep_range(2000, 4000);
353 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
354 LP5523_EN_LEDTEST | i);
355 /* ADC conversion time is 2.7 ms typically */
356 usleep_range(3000, 6000);
357 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
358 if (ret < 0)
359 goto fail;
360
361 if (!(status & LP5523_LEDTEST_DONE))
362 usleep_range(3000, 6000);/* Was not ready. Wait. */
363
364 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
365 if (ret < 0)
366 goto fail;
367
368 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
369 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
370
371 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
372
373 /* Restore current */
374 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
375 led->led_current);
376 led++;
377 }
378 if (pos == 0)
379 pos = sprintf(buf, "OK\n");
380 goto release_lock;
381 fail:
382 pos = sprintf(buf, "FAIL\n");
383
384 release_lock:
385 mutex_unlock(&chip->lock);
386
387 return pos;
388 }
389
390 static void lp5523_led_brightness_work(struct work_struct *work)
391 {
392 struct lp55xx_led *led = container_of(work, struct lp55xx_led,
393 brightness_work);
394 struct lp55xx_chip *chip = led->chip;
395
396 mutex_lock(&chip->lock);
397 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
398 led->brightness);
399 mutex_unlock(&chip->lock);
400 }
401
402 static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
403
404 static struct attribute *lp5523_attributes[] = {
405 &dev_attr_selftest.attr,
406 NULL,
407 };
408
409 static const struct attribute_group lp5523_group = {
410 .attrs = lp5523_attributes,
411 };
412
413 /* Chip specific configurations */
414 static struct lp55xx_device_config lp5523_cfg = {
415 .reset = {
416 .addr = LP5523_REG_RESET,
417 .val = LP5523_RESET,
418 },
419 .enable = {
420 .addr = LP5523_REG_ENABLE,
421 .val = LP5523_ENABLE,
422 },
423 .max_channel = LP5523_MAX_LEDS,
424 .post_init_device = lp5523_post_init_device,
425 .brightness_work_fn = lp5523_led_brightness_work,
426 .set_led_current = lp5523_set_led_current,
427 .firmware_cb = lp5523_firmware_loaded,
428 .run_engine = lp5523_run_engine,
429 .dev_attr_group = &lp5523_group,
430 };
431
432 static int lp5523_probe(struct i2c_client *client,
433 const struct i2c_device_id *id)
434 {
435 int ret;
436 struct lp55xx_chip *chip;
437 struct lp55xx_led *led;
438 struct lp55xx_platform_data *pdata;
439 struct device_node *np = client->dev.of_node;
440
441 if (!dev_get_platdata(&client->dev)) {
442 if (np) {
443 ret = lp55xx_of_populate_pdata(&client->dev, np);
444 if (ret < 0)
445 return ret;
446 } else {
447 dev_err(&client->dev, "no platform data\n");
448 return -EINVAL;
449 }
450 }
451 pdata = dev_get_platdata(&client->dev);
452
453 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
454 if (!chip)
455 return -ENOMEM;
456
457 led = devm_kzalloc(&client->dev,
458 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
459 if (!led)
460 return -ENOMEM;
461
462 chip->cl = client;
463 chip->pdata = pdata;
464 chip->cfg = &lp5523_cfg;
465
466 mutex_init(&chip->lock);
467
468 i2c_set_clientdata(client, led);
469
470 ret = lp55xx_init_device(chip);
471 if (ret)
472 goto err_init;
473
474 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
475
476 ret = lp55xx_register_leds(led, chip);
477 if (ret)
478 goto err_register_leds;
479
480 ret = lp55xx_register_sysfs(chip);
481 if (ret) {
482 dev_err(&client->dev, "registering sysfs failed\n");
483 goto err_register_sysfs;
484 }
485
486 return 0;
487
488 err_register_sysfs:
489 lp55xx_unregister_leds(led, chip);
490 err_register_leds:
491 lp55xx_deinit_device(chip);
492 err_init:
493 return ret;
494 }
495
496 static int lp5523_remove(struct i2c_client *client)
497 {
498 struct lp55xx_led *led = i2c_get_clientdata(client);
499 struct lp55xx_chip *chip = led->chip;
500
501 lp5523_stop_engine(chip);
502 lp55xx_unregister_sysfs(chip);
503 lp55xx_unregister_leds(led, chip);
504 lp55xx_deinit_device(chip);
505
506 return 0;
507 }
508
509 static const struct i2c_device_id lp5523_id[] = {
510 { "lp5523", LP5523 },
511 { "lp55231", LP55231 },
512 { }
513 };
514
515 MODULE_DEVICE_TABLE(i2c, lp5523_id);
516
517 #ifdef CONFIG_OF
518 static const struct of_device_id of_lp5523_leds_match[] = {
519 { .compatible = "national,lp5523", },
520 {},
521 };
522
523 MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
524 #endif
525
526 static struct i2c_driver lp5523_driver = {
527 .driver = {
528 .name = "lp5523x",
529 .of_match_table = of_match_ptr(of_lp5523_leds_match),
530 },
531 .probe = lp5523_probe,
532 .remove = lp5523_remove,
533 .id_table = lp5523_id,
534 };
535
536 module_i2c_driver(lp5523_driver);
537
538 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
539 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
540 MODULE_DESCRIPTION("LP5523 LED engine");
541 MODULE_LICENSE("GPL");
This page took 0.067398 seconds and 5 git commands to generate.