leds: lp5523: LED MUX configuration on initializing
[cascardo/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_CH1_PROG_START       0x4C
53 #define LP5523_REG_CH2_PROG_START       0x4D
54 #define LP5523_REG_CH3_PROG_START       0x4E
55 #define LP5523_REG_PROG_PAGE_SEL        0x4F
56 #define LP5523_REG_PROG_MEM             0x50
57
58 /* Bit description in registers */
59 #define LP5523_ENABLE                   0x40
60 #define LP5523_AUTO_INC                 0x40
61 #define LP5523_PWR_SAVE                 0x20
62 #define LP5523_PWM_PWR_SAVE             0x04
63 #define LP5523_CP_AUTO                  0x18
64 #define LP5523_AUTO_CLK                 0x02
65
66 #define LP5523_EN_LEDTEST               0x80
67 #define LP5523_LEDTEST_DONE             0x80
68 #define LP5523_RESET                    0xFF
69 #define LP5523_ADC_SHORTCIRC_LIM        80
70 #define LP5523_EXT_CLK_USED             0x08
71 #define LP5523_ENG_STATUS_MASK          0x07
72
73 /* Memory Page Selection */
74 #define LP5523_PAGE_ENG1                0
75 #define LP5523_PAGE_ENG2                1
76 #define LP5523_PAGE_ENG3                2
77
78 /* Program Memory Operations */
79 #define LP5523_MODE_ENG1_M              0x30    /* Operation Mode Register */
80 #define LP5523_MODE_ENG2_M              0x0C
81 #define LP5523_MODE_ENG3_M              0x03
82 #define LP5523_LOAD_ENG1                0x10
83 #define LP5523_LOAD_ENG2                0x04
84 #define LP5523_LOAD_ENG3                0x01
85
86 #define LP5523_ENG1_IS_LOADING(mode)    \
87         ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1)
88 #define LP5523_ENG2_IS_LOADING(mode)    \
89         ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2)
90 #define LP5523_ENG3_IS_LOADING(mode)    \
91         ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3)
92
93 #define LP5523_EXEC_ENG1_M              0x30    /* Enable Register */
94 #define LP5523_EXEC_ENG2_M              0x0C
95 #define LP5523_EXEC_ENG3_M              0x03
96 #define LP5523_EXEC_M                   0x3F
97 #define LP5523_RUN_ENG1                 0x20
98 #define LP5523_RUN_ENG2                 0x08
99 #define LP5523_RUN_ENG3                 0x02
100
101 enum lp5523_chip_id {
102         LP5523,
103         LP55231,
104 };
105
106 static int lp5523_init_program_engine(struct lp55xx_chip *chip);
107
108 static inline void lp5523_wait_opmode_done(void)
109 {
110         usleep_range(1000, 2000);
111 }
112
113 static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
114 {
115         led->led_current = led_current;
116         lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
117                 led_current);
118 }
119
120 static int lp5523_post_init_device(struct lp55xx_chip *chip)
121 {
122         int ret;
123
124         ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
125         if (ret)
126                 return ret;
127
128         /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
129         usleep_range(1000, 2000);
130
131         ret = lp55xx_write(chip, LP5523_REG_CONFIG,
132                             LP5523_AUTO_INC | LP5523_PWR_SAVE |
133                             LP5523_CP_AUTO | LP5523_AUTO_CLK |
134                             LP5523_PWM_PWR_SAVE);
135         if (ret)
136                 return ret;
137
138         /* turn on all leds */
139         ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
140         if (ret)
141                 return ret;
142
143         ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
144         if (ret)
145                 return ret;
146
147         return lp5523_init_program_engine(chip);
148 }
149
150 static void lp5523_load_engine(struct lp55xx_chip *chip)
151 {
152         enum lp55xx_engine_index idx = chip->engine_idx;
153         u8 mask[] = {
154                 [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M,
155                 [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M,
156                 [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M,
157         };
158
159         u8 val[] = {
160                 [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1,
161                 [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2,
162                 [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3,
163         };
164
165         lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]);
166
167         lp5523_wait_opmode_done();
168 }
169
170 static void lp5523_load_engine_and_select_page(struct lp55xx_chip *chip)
171 {
172         enum lp55xx_engine_index idx = chip->engine_idx;
173         u8 page_sel[] = {
174                 [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1,
175                 [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2,
176                 [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3,
177         };
178
179         lp5523_load_engine(chip);
180
181         lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]);
182 }
183
184 static void lp5523_stop_engine(struct lp55xx_chip *chip)
185 {
186         lp55xx_write(chip, LP5523_REG_OP_MODE, 0);
187         lp5523_wait_opmode_done();
188 }
189
190 static void lp5523_turn_off_channels(struct lp55xx_chip *chip)
191 {
192         int i;
193
194         for (i = 0; i < LP5523_MAX_LEDS; i++)
195                 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0);
196 }
197
198 static void lp5523_run_engine(struct lp55xx_chip *chip, bool start)
199 {
200         int ret;
201         u8 mode;
202         u8 exec;
203
204         /* stop engine */
205         if (!start) {
206                 lp5523_stop_engine(chip);
207                 lp5523_turn_off_channels(chip);
208                 return;
209         }
210
211         /*
212          * To run the engine,
213          * operation mode and enable register should updated at the same time
214          */
215
216         ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode);
217         if (ret)
218                 return;
219
220         ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec);
221         if (ret)
222                 return;
223
224         /* change operation mode to RUN only when each engine is loading */
225         if (LP5523_ENG1_IS_LOADING(mode)) {
226                 mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1;
227                 exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1;
228         }
229
230         if (LP5523_ENG2_IS_LOADING(mode)) {
231                 mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2;
232                 exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2;
233         }
234
235         if (LP5523_ENG3_IS_LOADING(mode)) {
236                 mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3;
237                 exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3;
238         }
239
240         lp55xx_write(chip, LP5523_REG_OP_MODE, mode);
241         lp5523_wait_opmode_done();
242
243         lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec);
244 }
245
246 static int lp5523_init_program_engine(struct lp55xx_chip *chip)
247 {
248         int i;
249         int j;
250         int ret;
251         u8 status;
252         /* one pattern per engine setting LED MUX start and stop addresses */
253         static const u8 pattern[][LP5523_PROGRAM_LENGTH] =  {
254                 { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
255                 { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
256                 { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
257         };
258
259         /* hardcode 32 bytes of memory for each engine from program memory */
260         ret = lp55xx_write(chip, LP5523_REG_CH1_PROG_START, 0x00);
261         if (ret)
262                 return ret;
263
264         ret = lp55xx_write(chip, LP5523_REG_CH2_PROG_START, 0x10);
265         if (ret)
266                 return ret;
267
268         ret = lp55xx_write(chip, LP5523_REG_CH3_PROG_START, 0x20);
269         if (ret)
270                 return ret;
271
272         /* write LED MUX address space for each engine */
273         for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
274                 chip->engine_idx = i;
275                 lp5523_load_engine_and_select_page(chip);
276
277                 for (j = 0; j < LP5523_PROGRAM_LENGTH; j++) {
278                         ret = lp55xx_write(chip, LP5523_REG_PROG_MEM + j,
279                                         pattern[i - 1][j]);
280                         if (ret)
281                                 goto out;
282                 }
283         }
284
285         lp5523_run_engine(chip, true);
286
287         /* Let the programs run for couple of ms and check the engine status */
288         usleep_range(3000, 6000);
289         lp55xx_read(chip, LP5523_REG_STATUS, &status);
290         status &= LP5523_ENG_STATUS_MASK;
291
292         if (status != LP5523_ENG_STATUS_MASK) {
293                 dev_err(&chip->cl->dev,
294                         "cound not configure LED engine, status = 0x%.2x\n",
295                         status);
296                 ret = -1;
297         }
298
299 out:
300         lp5523_stop_engine(chip);
301         return ret;
302 }
303
304 static int lp5523_update_program_memory(struct lp55xx_chip *chip,
305                                         const u8 *data, size_t size)
306 {
307         u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
308         unsigned cmd;
309         char c[3];
310         int update_size;
311         int nrchars;
312         int offset = 0;
313         int ret;
314         int i;
315
316         /* clear program memory before updating */
317         for (i = 0; i < LP5523_PROGRAM_LENGTH; i++)
318                 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0);
319
320         i = 0;
321         while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) {
322                 /* separate sscanfs because length is working only for %s */
323                 ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
324                 if (ret != 1)
325                         goto err;
326
327                 ret = sscanf(c, "%2x", &cmd);
328                 if (ret != 1)
329                         goto err;
330
331                 pattern[i] = (u8)cmd;
332                 offset += nrchars;
333                 i++;
334         }
335
336         /* Each instruction is 16bit long. Check that length is even */
337         if (i % 2)
338                 goto err;
339
340         update_size = i;
341         for (i = 0; i < update_size; i++)
342                 lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]);
343
344         return 0;
345
346 err:
347         dev_err(&chip->cl->dev, "wrong pattern format\n");
348         return -EINVAL;
349 }
350
351 static void lp5523_firmware_loaded(struct lp55xx_chip *chip)
352 {
353         const struct firmware *fw = chip->fw;
354
355         if (fw->size > LP5523_PROGRAM_LENGTH) {
356                 dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
357                         fw->size);
358                 return;
359         }
360
361         /*
362          * Program momery sequence
363          *  1) set engine mode to "LOAD"
364          *  2) write firmware data into program memory
365          */
366
367         lp5523_load_engine_and_select_page(chip);
368         lp5523_update_program_memory(chip, fw->data, fw->size);
369 }
370
371 static ssize_t lp5523_selftest(struct device *dev,
372                                struct device_attribute *attr,
373                                char *buf)
374 {
375         struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
376         struct lp55xx_chip *chip = led->chip;
377         struct lp55xx_platform_data *pdata = chip->pdata;
378         int i, ret, pos = 0;
379         u8 status, adc, vdd;
380
381         mutex_lock(&chip->lock);
382
383         ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
384         if (ret < 0)
385                 goto fail;
386
387         /* Check that ext clock is really in use if requested */
388         if (pdata->clock_mode == LP55XX_CLOCK_EXT) {
389                 if  ((status & LP5523_EXT_CLK_USED) == 0)
390                         goto fail;
391         }
392
393         /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
394         lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16);
395         usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
396         ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
397         if (ret < 0)
398                 goto fail;
399
400         if (!(status & LP5523_LEDTEST_DONE))
401                 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
402
403         ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd);
404         if (ret < 0)
405                 goto fail;
406
407         vdd--;  /* There may be some fluctuation in measurement */
408
409         for (i = 0; i < LP5523_MAX_LEDS; i++) {
410                 /* Skip non-existing channels */
411                 if (pdata->led_config[i].led_current == 0)
412                         continue;
413
414                 /* Set default current */
415                 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
416                         pdata->led_config[i].led_current);
417
418                 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff);
419                 /* let current stabilize 2 - 4ms before measurements start */
420                 usleep_range(2000, 4000);
421                 lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL,
422                              LP5523_EN_LEDTEST | i);
423                 /* ADC conversion time is 2.7 ms typically */
424                 usleep_range(3000, 6000);
425                 ret = lp55xx_read(chip, LP5523_REG_STATUS, &status);
426                 if (ret < 0)
427                         goto fail;
428
429                 if (!(status & LP5523_LEDTEST_DONE))
430                         usleep_range(3000, 6000);/* Was not ready. Wait. */
431
432                 ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc);
433                 if (ret < 0)
434                         goto fail;
435
436                 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
437                         pos += sprintf(buf + pos, "LED %d FAIL\n", i);
438
439                 lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00);
440
441                 /* Restore current */
442                 lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i,
443                         led->led_current);
444                 led++;
445         }
446         if (pos == 0)
447                 pos = sprintf(buf, "OK\n");
448         goto release_lock;
449 fail:
450         pos = sprintf(buf, "FAIL\n");
451
452 release_lock:
453         mutex_unlock(&chip->lock);
454
455         return pos;
456 }
457
458 static void lp5523_led_brightness_work(struct work_struct *work)
459 {
460         struct lp55xx_led *led = container_of(work, struct lp55xx_led,
461                                               brightness_work);
462         struct lp55xx_chip *chip = led->chip;
463
464         mutex_lock(&chip->lock);
465         lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr,
466                      led->brightness);
467         mutex_unlock(&chip->lock);
468 }
469
470 static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
471
472 static struct attribute *lp5523_attributes[] = {
473         &dev_attr_selftest.attr,
474         NULL,
475 };
476
477 static const struct attribute_group lp5523_group = {
478         .attrs = lp5523_attributes,
479 };
480
481 /* Chip specific configurations */
482 static struct lp55xx_device_config lp5523_cfg = {
483         .reset = {
484                 .addr = LP5523_REG_RESET,
485                 .val  = LP5523_RESET,
486         },
487         .enable = {
488                 .addr = LP5523_REG_ENABLE,
489                 .val  = LP5523_ENABLE,
490         },
491         .max_channel  = LP5523_MAX_LEDS,
492         .post_init_device   = lp5523_post_init_device,
493         .brightness_work_fn = lp5523_led_brightness_work,
494         .set_led_current    = lp5523_set_led_current,
495         .firmware_cb        = lp5523_firmware_loaded,
496         .run_engine         = lp5523_run_engine,
497         .dev_attr_group     = &lp5523_group,
498 };
499
500 static int lp5523_probe(struct i2c_client *client,
501                         const struct i2c_device_id *id)
502 {
503         int ret;
504         struct lp55xx_chip *chip;
505         struct lp55xx_led *led;
506         struct lp55xx_platform_data *pdata;
507         struct device_node *np = client->dev.of_node;
508
509         if (!dev_get_platdata(&client->dev)) {
510                 if (np) {
511                         ret = lp55xx_of_populate_pdata(&client->dev, np);
512                         if (ret < 0)
513                                 return ret;
514                 } else {
515                         dev_err(&client->dev, "no platform data\n");
516                         return -EINVAL;
517                 }
518         }
519         pdata = dev_get_platdata(&client->dev);
520
521         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
522         if (!chip)
523                 return -ENOMEM;
524
525         led = devm_kzalloc(&client->dev,
526                         sizeof(*led) * pdata->num_channels, GFP_KERNEL);
527         if (!led)
528                 return -ENOMEM;
529
530         chip->cl = client;
531         chip->pdata = pdata;
532         chip->cfg = &lp5523_cfg;
533
534         mutex_init(&chip->lock);
535
536         i2c_set_clientdata(client, led);
537
538         ret = lp55xx_init_device(chip);
539         if (ret)
540                 goto err_init;
541
542         dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
543
544         ret = lp55xx_register_leds(led, chip);
545         if (ret)
546                 goto err_register_leds;
547
548         ret = lp55xx_register_sysfs(chip);
549         if (ret) {
550                 dev_err(&client->dev, "registering sysfs failed\n");
551                 goto err_register_sysfs;
552         }
553
554         return 0;
555
556 err_register_sysfs:
557         lp55xx_unregister_leds(led, chip);
558 err_register_leds:
559         lp55xx_deinit_device(chip);
560 err_init:
561         return ret;
562 }
563
564 static int lp5523_remove(struct i2c_client *client)
565 {
566         struct lp55xx_led *led = i2c_get_clientdata(client);
567         struct lp55xx_chip *chip = led->chip;
568
569         lp5523_stop_engine(chip);
570         lp55xx_unregister_sysfs(chip);
571         lp55xx_unregister_leds(led, chip);
572         lp55xx_deinit_device(chip);
573
574         return 0;
575 }
576
577 static const struct i2c_device_id lp5523_id[] = {
578         { "lp5523",  LP5523 },
579         { "lp55231", LP55231 },
580         { }
581 };
582
583 MODULE_DEVICE_TABLE(i2c, lp5523_id);
584
585 #ifdef CONFIG_OF
586 static const struct of_device_id of_lp5523_leds_match[] = {
587         { .compatible = "national,lp5523", },
588         {},
589 };
590
591 MODULE_DEVICE_TABLE(of, of_lp5523_leds_match);
592 #endif
593
594 static struct i2c_driver lp5523_driver = {
595         .driver = {
596                 .name   = "lp5523x",
597                 .of_match_table = of_match_ptr(of_lp5523_leds_match),
598         },
599         .probe          = lp5523_probe,
600         .remove         = lp5523_remove,
601         .id_table       = lp5523_id,
602 };
603
604 module_i2c_driver(lp5523_driver);
605
606 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
607 MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
608 MODULE_DESCRIPTION("LP5523 LED engine");
609 MODULE_LICENSE("GPL");