Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / leds / leds-lp5523.c
1 /*
2  * lp5523.c - LP5523 LED Driver
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contact: Samu Onkalo <samu.p.onkalo@nokia.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  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/mutex.h>
27 #include <linux/gpio.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <linux/ctype.h>
31 #include <linux/spinlock.h>
32 #include <linux/wait.h>
33 #include <linux/leds.h>
34 #include <linux/leds-lp5523.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37
38 #define LP5523_REG_ENABLE               0x00
39 #define LP5523_REG_OP_MODE              0x01
40 #define LP5523_REG_RATIOMETRIC_MSB      0x02
41 #define LP5523_REG_RATIOMETRIC_LSB      0x03
42 #define LP5523_REG_ENABLE_LEDS_MSB      0x04
43 #define LP5523_REG_ENABLE_LEDS_LSB      0x05
44 #define LP5523_REG_LED_CNTRL_BASE       0x06
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_CHANNEL1_PC          0x37
49 #define LP5523_REG_CHANNEL2_PC          0x38
50 #define LP5523_REG_CHANNEL3_PC          0x39
51 #define LP5523_REG_STATUS               0x3a
52 #define LP5523_REG_GPO                  0x3b
53 #define LP5523_REG_VARIABLE             0x3c
54 #define LP5523_REG_RESET                0x3d
55 #define LP5523_REG_TEMP_CTRL            0x3e
56 #define LP5523_REG_TEMP_READ            0x3f
57 #define LP5523_REG_TEMP_WRITE           0x40
58 #define LP5523_REG_LED_TEST_CTRL        0x41
59 #define LP5523_REG_LED_TEST_ADC         0x42
60 #define LP5523_REG_ENG1_VARIABLE        0x45
61 #define LP5523_REG_ENG2_VARIABLE        0x46
62 #define LP5523_REG_ENG3_VARIABLE        0x47
63 #define LP5523_REG_MASTER_FADER1        0x48
64 #define LP5523_REG_MASTER_FADER2        0x49
65 #define LP5523_REG_MASTER_FADER3        0x4a
66 #define LP5523_REG_CH1_PROG_START       0x4c
67 #define LP5523_REG_CH2_PROG_START       0x4d
68 #define LP5523_REG_CH3_PROG_START       0x4e
69 #define LP5523_REG_PROG_PAGE_SEL        0x4f
70 #define LP5523_REG_PROG_MEM             0x50
71
72 #define LP5523_CMD_LOAD                 0x15 /* 00010101 */
73 #define LP5523_CMD_RUN                  0x2a /* 00101010 */
74 #define LP5523_CMD_DISABLED             0x00 /* 00000000 */
75
76 #define LP5523_ENABLE                   0x40
77 #define LP5523_AUTO_INC                 0x40
78 #define LP5523_PWR_SAVE                 0x20
79 #define LP5523_PWM_PWR_SAVE             0x04
80 #define LP5523_CP_1                     0x08
81 #define LP5523_CP_1_5                   0x10
82 #define LP5523_CP_AUTO                  0x18
83 #define LP5523_INT_CLK                  0x01
84 #define LP5523_AUTO_CLK                 0x02
85 #define LP5523_EN_LEDTEST               0x80
86 #define LP5523_LEDTEST_DONE             0x80
87
88 #define LP5523_DEFAULT_CURRENT          50 /* microAmps */
89 #define LP5523_PROGRAM_LENGTH           32 /* in bytes */
90 #define LP5523_PROGRAM_PAGES            6
91 #define LP5523_ADC_SHORTCIRC_LIM        80
92
93 #define LP5523_LEDS                     9
94 #define LP5523_ENGINES                  3
95
96 #define LP5523_ENG_MASK_BASE            0x30 /* 00110000 */
97
98 #define LP5523_ENG_STATUS_MASK          0x07 /* 00000111 */
99
100 #define LP5523_IRQ_FLAGS                IRQF_TRIGGER_FALLING
101
102 #define LP5523_EXT_CLK_USED             0x08
103
104 #define LED_ACTIVE(mux, led)            (!!(mux & (0x0001 << led)))
105 #define SHIFT_MASK(id)                  (((id) - 1) * 2)
106
107 enum lp5523_chip_id {
108         LP5523,
109         LP55231,
110 };
111
112 struct lp5523_engine {
113         int             id;
114         u8              mode;
115         u8              prog_page;
116         u8              mux_page;
117         u16             led_mux;
118         u8              engine_mask;
119 };
120
121 struct lp5523_led {
122         int                     id;
123         u8                      chan_nr;
124         u8                      led_current;
125         u8                      max_current;
126         struct led_classdev     cdev;
127         struct work_struct      brightness_work;
128         u8                      brightness;
129 };
130
131 struct lp5523_chip {
132         struct mutex            lock; /* Serialize control */
133         struct i2c_client       *client;
134         struct lp5523_engine    engines[LP5523_ENGINES];
135         struct lp5523_led       leds[LP5523_LEDS];
136         struct lp5523_platform_data *pdata;
137         u8                      num_channels;
138         u8                      num_leds;
139 };
140
141 static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
142 {
143         return container_of(cdev, struct lp5523_led, cdev);
144 }
145
146 static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
147 {
148         return container_of(engine, struct lp5523_chip,
149                             engines[engine->id - 1]);
150 }
151
152 static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
153 {
154         return container_of(led, struct lp5523_chip,
155                             leds[led->id]);
156 }
157
158 static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode);
159 static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode);
160 static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern);
161
162 static void lp5523_led_brightness_work(struct work_struct *work);
163
164 static int lp5523_write(struct i2c_client *client, u8 reg, u8 value)
165 {
166         return i2c_smbus_write_byte_data(client, reg, value);
167 }
168
169 static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf)
170 {
171         s32 ret = i2c_smbus_read_byte_data(client, reg);
172
173         if (ret < 0)
174                 return ret;
175
176         *buf = ret;
177         return 0;
178 }
179
180 static int lp5523_detect(struct i2c_client *client)
181 {
182         int ret;
183         u8 buf;
184
185         ret = lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
186         if (ret)
187                 return ret;
188         ret = lp5523_read(client, LP5523_REG_ENABLE, &buf);
189         if (ret)
190                 return ret;
191         if (buf == 0x40)
192                 return 0;
193         else
194                 return -ENODEV;
195 }
196
197 static int lp5523_configure(struct i2c_client *client)
198 {
199         struct lp5523_chip *chip = i2c_get_clientdata(client);
200         int ret = 0;
201         u8 status;
202
203         /* one pattern per engine setting led mux start and stop addresses */
204         static const u8 pattern[][LP5523_PROGRAM_LENGTH] =  {
205                 { 0x9c, 0x30, 0x9c, 0xb0, 0x9d, 0x80, 0xd8, 0x00, 0},
206                 { 0x9c, 0x40, 0x9c, 0xc0, 0x9d, 0x80, 0xd8, 0x00, 0},
207                 { 0x9c, 0x50, 0x9c, 0xd0, 0x9d, 0x80, 0xd8, 0x00, 0},
208         };
209
210         ret |= lp5523_write(client, LP5523_REG_ENABLE, LP5523_ENABLE);
211         /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
212         usleep_range(1000, 2000);
213
214         ret |= lp5523_write(client, LP5523_REG_CONFIG,
215                             LP5523_AUTO_INC | LP5523_PWR_SAVE |
216                             LP5523_CP_AUTO | LP5523_AUTO_CLK |
217                             LP5523_PWM_PWR_SAVE);
218
219         /* turn on all leds */
220         ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
221         ret |= lp5523_write(client, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
222
223         /* hardcode 32 bytes of memory for each engine from program memory */
224         ret |= lp5523_write(client, LP5523_REG_CH1_PROG_START, 0x00);
225         ret |= lp5523_write(client, LP5523_REG_CH2_PROG_START, 0x10);
226         ret |= lp5523_write(client, LP5523_REG_CH3_PROG_START, 0x20);
227
228         /* write led mux address space for each channel */
229         ret |= lp5523_load_program(&chip->engines[0], pattern[0]);
230         ret |= lp5523_load_program(&chip->engines[1], pattern[1]);
231         ret |= lp5523_load_program(&chip->engines[2], pattern[2]);
232
233         if (ret) {
234                 dev_err(&client->dev, "could not load mux programs\n");
235                 return -1;
236         }
237
238         /* set all engines exec state and mode to run 00101010 */
239         ret |= lp5523_write(client, LP5523_REG_ENABLE,
240                             (LP5523_CMD_RUN | LP5523_ENABLE));
241
242         ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_RUN);
243
244         if (ret) {
245                 dev_err(&client->dev, "could not start mux programs\n");
246                 return -1;
247         }
248
249         /* Let the programs run for couple of ms and check the engine status */
250         usleep_range(3000, 6000);
251         ret = lp5523_read(client, LP5523_REG_STATUS, &status);
252         if (ret < 0)
253                 return ret;
254
255         status &= LP5523_ENG_STATUS_MASK;
256
257         if (status == LP5523_ENG_STATUS_MASK) {
258                 dev_dbg(&client->dev, "all engines configured\n");
259         } else {
260                 dev_info(&client->dev, "status == %x\n", status);
261                 dev_err(&client->dev, "cound not configure LED engine\n");
262                 return -1;
263         }
264
265         dev_info(&client->dev, "disabling engines\n");
266
267         ret |= lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
268
269         return ret;
270 }
271
272 static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode)
273 {
274         struct lp5523_chip *chip = engine_to_lp5523(engine);
275         struct i2c_client *client = chip->client;
276         int ret;
277         u8 engine_state;
278
279         ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state);
280         if (ret)
281                 goto fail;
282
283         engine_state &= ~(engine->engine_mask);
284
285         /* set mode only for this engine */
286         mode &= engine->engine_mask;
287
288         engine_state |= mode;
289
290         ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state);
291 fail:
292         return ret;
293 }
294
295 static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux)
296 {
297         struct lp5523_chip *chip = engine_to_lp5523(engine);
298         struct i2c_client *client = chip->client;
299         int ret = 0;
300
301         ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
302
303         ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page);
304         ret |= lp5523_write(client, LP5523_REG_PROG_MEM,
305                             (u8)(mux >> 8));
306         ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
307         engine->led_mux = mux;
308
309         return ret;
310 }
311
312 static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern)
313 {
314         struct lp5523_chip *chip = engine_to_lp5523(engine);
315         struct i2c_client *client = chip->client;
316
317         int ret = 0;
318
319         ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
320
321         ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL,
322                             engine->prog_page);
323         ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM,
324                                               LP5523_PROGRAM_LENGTH, pattern);
325
326         return ret;
327 }
328
329 static int lp5523_run_program(struct lp5523_engine *engine)
330 {
331         struct lp5523_chip *chip = engine_to_lp5523(engine);
332         struct i2c_client *client = chip->client;
333         int ret;
334
335         ret = lp5523_write(client, LP5523_REG_ENABLE,
336                                         LP5523_CMD_RUN | LP5523_ENABLE);
337         if (ret)
338                 goto fail;
339
340         ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN);
341 fail:
342         return ret;
343 }
344
345 static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
346 {
347         int i;
348         u16 tmp_mux = 0;
349
350         len = min_t(int, len, LP5523_LEDS);
351         for (i = 0; i < len; i++) {
352                 switch (buf[i]) {
353                 case '1':
354                         tmp_mux |= (1 << i);
355                         break;
356                 case '0':
357                         break;
358                 case '\n':
359                         i = len;
360                         break;
361                 default:
362                         return -1;
363                 }
364         }
365         *mux = tmp_mux;
366
367         return 0;
368 }
369
370 static void lp5523_mux_to_array(u16 led_mux, char *array)
371 {
372         int i, pos = 0;
373         for (i = 0; i < LP5523_LEDS; i++)
374                 pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
375
376         array[pos] = '\0';
377 }
378
379 /*--------------------------------------------------------------*/
380 /*                      Sysfs interface                         */
381 /*--------------------------------------------------------------*/
382
383 static ssize_t show_engine_leds(struct device *dev,
384                             struct device_attribute *attr,
385                             char *buf, int nr)
386 {
387         struct i2c_client *client = to_i2c_client(dev);
388         struct lp5523_chip *chip = i2c_get_clientdata(client);
389         char mux[LP5523_LEDS + 1];
390
391         lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
392
393         return sprintf(buf, "%s\n", mux);
394 }
395
396 #define show_leds(nr)                                                   \
397 static ssize_t show_engine##nr##_leds(struct device *dev,               \
398                             struct device_attribute *attr,              \
399                             char *buf)                                  \
400 {                                                                       \
401         return show_engine_leds(dev, attr, buf, nr);                    \
402 }
403 show_leds(1)
404 show_leds(2)
405 show_leds(3)
406
407 static ssize_t store_engine_leds(struct device *dev,
408                              struct device_attribute *attr,
409                              const char *buf, size_t len, int nr)
410 {
411         struct i2c_client *client = to_i2c_client(dev);
412         struct lp5523_chip *chip = i2c_get_clientdata(client);
413         u16 mux = 0;
414         ssize_t ret;
415
416         if (lp5523_mux_parse(buf, &mux, len))
417                 return -EINVAL;
418
419         mutex_lock(&chip->lock);
420         ret = -EINVAL;
421         if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD)
422                 goto leave;
423
424         if (lp5523_load_mux(&chip->engines[nr - 1], mux))
425                 goto leave;
426
427         ret = len;
428 leave:
429         mutex_unlock(&chip->lock);
430         return ret;
431 }
432
433 #define store_leds(nr)                                          \
434 static ssize_t store_engine##nr##_leds(struct device *dev,      \
435                              struct device_attribute *attr,     \
436                              const char *buf, size_t len)       \
437 {                                                               \
438         return store_engine_leds(dev, attr, buf, len, nr);      \
439 }
440 store_leds(1)
441 store_leds(2)
442 store_leds(3)
443
444 static ssize_t lp5523_selftest(struct device *dev,
445                                struct device_attribute *attr,
446                                char *buf)
447 {
448         struct i2c_client *client = to_i2c_client(dev);
449         struct lp5523_chip *chip = i2c_get_clientdata(client);
450         int i, ret, pos = 0;
451         int led = 0;
452         u8 status, adc, vdd;
453
454         mutex_lock(&chip->lock);
455
456         ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
457         if (ret < 0)
458                 goto fail;
459
460         /* Check that ext clock is really in use if requested */
461         if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT))
462                 if  ((status & LP5523_EXT_CLK_USED) == 0)
463                         goto fail;
464
465         /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */
466         lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL,
467                                     LP5523_EN_LEDTEST | 16);
468         usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */
469         ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
470         if (ret < 0)
471                 goto fail;
472
473         if (!(status & LP5523_LEDTEST_DONE))
474                 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
475
476         ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
477         if (ret < 0)
478                 goto fail;
479
480         vdd--;  /* There may be some fluctuation in measurement */
481
482         for (i = 0; i < LP5523_LEDS; i++) {
483                 /* Skip non-existing channels */
484                 if (chip->pdata->led_config[i].led_current == 0)
485                         continue;
486
487                 /* Set default current */
488                 lp5523_write(chip->client,
489                         LP5523_REG_LED_CURRENT_BASE + i,
490                         chip->pdata->led_config[i].led_current);
491
492                 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff);
493                 /* let current stabilize 2 - 4ms before measurements start */
494                 usleep_range(2000, 4000);
495                 lp5523_write(chip->client,
496                              LP5523_REG_LED_TEST_CTRL,
497                              LP5523_EN_LEDTEST | i);
498                 /* ADC conversion time is 2.7 ms typically */
499                 usleep_range(3000, 6000);
500                 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
501                 if (ret < 0)
502                         goto fail;
503
504                 if (!(status & LP5523_LEDTEST_DONE))
505                         usleep_range(3000, 6000);/* Was not ready. Wait. */
506                 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc);
507                 if (ret < 0)
508                         goto fail;
509
510                 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
511                         pos += sprintf(buf + pos, "LED %d FAIL\n", i);
512
513                 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00);
514
515                 /* Restore current */
516                 lp5523_write(chip->client,
517                         LP5523_REG_LED_CURRENT_BASE + i,
518                         chip->leds[led].led_current);
519                 led++;
520         }
521         if (pos == 0)
522                 pos = sprintf(buf, "OK\n");
523         goto release_lock;
524 fail:
525         pos = sprintf(buf, "FAIL\n");
526
527 release_lock:
528         mutex_unlock(&chip->lock);
529
530         return pos;
531 }
532
533 static void lp5523_set_brightness(struct led_classdev *cdev,
534                              enum led_brightness brightness)
535 {
536         struct lp5523_led *led = cdev_to_led(cdev);
537
538         led->brightness = (u8)brightness;
539
540         schedule_work(&led->brightness_work);
541 }
542
543 static void lp5523_led_brightness_work(struct work_struct *work)
544 {
545         struct lp5523_led *led = container_of(work,
546                                               struct lp5523_led,
547                                               brightness_work);
548         struct lp5523_chip *chip = led_to_lp5523(led);
549         struct i2c_client *client = chip->client;
550
551         mutex_lock(&chip->lock);
552
553         lp5523_write(client, LP5523_REG_LED_PWM_BASE + led->chan_nr,
554                      led->brightness);
555
556         mutex_unlock(&chip->lock);
557 }
558
559 static int lp5523_do_store_load(struct lp5523_engine *engine,
560                                 const char *buf, size_t len)
561 {
562         struct lp5523_chip *chip = engine_to_lp5523(engine);
563         struct i2c_client *client = chip->client;
564         int  ret, nrchars, offset = 0, i = 0;
565         char c[3];
566         unsigned cmd;
567         u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
568
569         if (engine->mode != LP5523_CMD_LOAD)
570                 return -EINVAL;
571
572         while ((offset < len - 1) && (i < LP5523_PROGRAM_LENGTH)) {
573                 /* separate sscanfs because length is working only for %s */
574                 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
575                 ret = sscanf(c, "%2x", &cmd);
576                 if (ret != 1)
577                         goto fail;
578                 pattern[i] = (u8)cmd;
579
580                 offset += nrchars;
581                 i++;
582         }
583
584         /* Each instruction is 16bit long. Check that length is even */
585         if (i % 2)
586                 goto fail;
587
588         mutex_lock(&chip->lock);
589         ret = lp5523_load_program(engine, pattern);
590         mutex_unlock(&chip->lock);
591
592         if (ret) {
593                 dev_err(&client->dev, "failed loading pattern\n");
594                 return ret;
595         }
596
597         return len;
598 fail:
599         dev_err(&client->dev, "wrong pattern format\n");
600         return -EINVAL;
601 }
602
603 static ssize_t store_engine_load(struct device *dev,
604                                      struct device_attribute *attr,
605                                      const char *buf, size_t len, int nr)
606 {
607         struct i2c_client *client = to_i2c_client(dev);
608         struct lp5523_chip *chip = i2c_get_clientdata(client);
609         return lp5523_do_store_load(&chip->engines[nr - 1], buf, len);
610 }
611
612 #define store_load(nr)                                                  \
613 static ssize_t store_engine##nr##_load(struct device *dev,              \
614                                      struct device_attribute *attr,     \
615                                      const char *buf, size_t len)       \
616 {                                                                       \
617         return store_engine_load(dev, attr, buf, len, nr);              \
618 }
619 store_load(1)
620 store_load(2)
621 store_load(3)
622
623 static ssize_t show_engine_mode(struct device *dev,
624                                 struct device_attribute *attr,
625                                 char *buf, int nr)
626 {
627         struct i2c_client *client = to_i2c_client(dev);
628         struct lp5523_chip *chip = i2c_get_clientdata(client);
629         switch (chip->engines[nr - 1].mode) {
630         case LP5523_CMD_RUN:
631                 return sprintf(buf, "run\n");
632         case LP5523_CMD_LOAD:
633                 return sprintf(buf, "load\n");
634         case LP5523_CMD_DISABLED:
635                 return sprintf(buf, "disabled\n");
636         default:
637                 return sprintf(buf, "disabled\n");
638         }
639 }
640
641 #define show_mode(nr)                                                   \
642 static ssize_t show_engine##nr##_mode(struct device *dev,               \
643                                     struct device_attribute *attr,      \
644                                     char *buf)                          \
645 {                                                                       \
646         return show_engine_mode(dev, attr, buf, nr);                    \
647 }
648 show_mode(1)
649 show_mode(2)
650 show_mode(3)
651
652 static ssize_t store_engine_mode(struct device *dev,
653                                  struct device_attribute *attr,
654                                  const char *buf, size_t len, int nr)
655 {
656         struct i2c_client *client = to_i2c_client(dev);
657         struct lp5523_chip *chip = i2c_get_clientdata(client);
658         struct lp5523_engine *engine = &chip->engines[nr - 1];
659         mutex_lock(&chip->lock);
660
661         if (!strncmp(buf, "run", 3))
662                 lp5523_set_mode(engine, LP5523_CMD_RUN);
663         else if (!strncmp(buf, "load", 4))
664                 lp5523_set_mode(engine, LP5523_CMD_LOAD);
665         else if (!strncmp(buf, "disabled", 8))
666                 lp5523_set_mode(engine, LP5523_CMD_DISABLED);
667
668         mutex_unlock(&chip->lock);
669         return len;
670 }
671
672 #define store_mode(nr)                                                  \
673 static ssize_t store_engine##nr##_mode(struct device *dev,              \
674                                      struct device_attribute *attr,     \
675                                      const char *buf, size_t len)       \
676 {                                                                       \
677         return store_engine_mode(dev, attr, buf, len, nr);              \
678 }
679 store_mode(1)
680 store_mode(2)
681 store_mode(3)
682
683 static ssize_t show_max_current(struct device *dev,
684                             struct device_attribute *attr,
685                             char *buf)
686 {
687         struct led_classdev *led_cdev = dev_get_drvdata(dev);
688         struct lp5523_led *led = cdev_to_led(led_cdev);
689
690         return sprintf(buf, "%d\n", led->max_current);
691 }
692
693 static ssize_t show_current(struct device *dev,
694                             struct device_attribute *attr,
695                             char *buf)
696 {
697         struct led_classdev *led_cdev = dev_get_drvdata(dev);
698         struct lp5523_led *led = cdev_to_led(led_cdev);
699
700         return sprintf(buf, "%d\n", led->led_current);
701 }
702
703 static ssize_t store_current(struct device *dev,
704                              struct device_attribute *attr,
705                              const char *buf, size_t len)
706 {
707         struct led_classdev *led_cdev = dev_get_drvdata(dev);
708         struct lp5523_led *led = cdev_to_led(led_cdev);
709         struct lp5523_chip *chip = led_to_lp5523(led);
710         ssize_t ret;
711         unsigned long curr;
712
713         if (kstrtoul(buf, 0, &curr))
714                 return -EINVAL;
715
716         if (curr > led->max_current)
717                 return -EINVAL;
718
719         mutex_lock(&chip->lock);
720         ret = lp5523_write(chip->client,
721                         LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
722                         (u8)curr);
723         mutex_unlock(&chip->lock);
724
725         if (ret < 0)
726                 return ret;
727
728         led->led_current = (u8)curr;
729
730         return len;
731 }
732
733 /* led class device attributes */
734 static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
735 static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
736
737 static struct attribute *lp5523_led_attributes[] = {
738         &dev_attr_led_current.attr,
739         &dev_attr_max_current.attr,
740         NULL,
741 };
742
743 static struct attribute_group lp5523_led_attribute_group = {
744         .attrs = lp5523_led_attributes
745 };
746
747 /* device attributes */
748 static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
749                    show_engine1_mode, store_engine1_mode);
750 static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
751                    show_engine2_mode, store_engine2_mode);
752 static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
753                    show_engine3_mode, store_engine3_mode);
754 static DEVICE_ATTR(engine1_leds, S_IRUGO | S_IWUSR,
755                    show_engine1_leds, store_engine1_leds);
756 static DEVICE_ATTR(engine2_leds, S_IRUGO | S_IWUSR,
757                    show_engine2_leds, store_engine2_leds);
758 static DEVICE_ATTR(engine3_leds, S_IRUGO | S_IWUSR,
759                    show_engine3_leds, store_engine3_leds);
760 static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
761 static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
762 static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
763 static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL);
764
765 static struct attribute *lp5523_attributes[] = {
766         &dev_attr_engine1_mode.attr,
767         &dev_attr_engine2_mode.attr,
768         &dev_attr_engine3_mode.attr,
769         &dev_attr_selftest.attr,
770         &dev_attr_engine1_load.attr,
771         &dev_attr_engine1_leds.attr,
772         &dev_attr_engine2_load.attr,
773         &dev_attr_engine2_leds.attr,
774         &dev_attr_engine3_load.attr,
775         &dev_attr_engine3_leds.attr,
776         NULL,
777 };
778
779 static const struct attribute_group lp5523_group = {
780         .attrs = lp5523_attributes,
781 };
782
783 static int lp5523_register_sysfs(struct i2c_client *client)
784 {
785         struct device *dev = &client->dev;
786         int ret;
787
788         ret = sysfs_create_group(&dev->kobj, &lp5523_group);
789         if (ret < 0)
790                 return ret;
791
792         return 0;
793 }
794
795 static void lp5523_unregister_sysfs(struct i2c_client *client)
796 {
797         struct lp5523_chip *chip = i2c_get_clientdata(client);
798         struct device *dev = &client->dev;
799         int i;
800
801         sysfs_remove_group(&dev->kobj, &lp5523_group);
802
803         for (i = 0; i < chip->num_leds; i++)
804                 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
805                                 &lp5523_led_attribute_group);
806 }
807
808 /*--------------------------------------------------------------*/
809 /*                      Set chip operating mode                 */
810 /*--------------------------------------------------------------*/
811 static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
812 {
813         /* if in that mode already do nothing, except for run */
814         if (mode == engine->mode && mode != LP5523_CMD_RUN)
815                 return;
816
817         switch (mode) {
818         case LP5523_CMD_RUN:
819                 lp5523_run_program(engine);
820                 break;
821         case LP5523_CMD_LOAD:
822                 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
823                 lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
824                 break;
825         case LP5523_CMD_DISABLED:
826                 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
827                 break;
828         default:
829                 return;
830         }
831
832         engine->mode = mode;
833 }
834
835 /*--------------------------------------------------------------*/
836 /*                      Probe, Attach, Remove                   */
837 /*--------------------------------------------------------------*/
838 static int __init lp5523_init_engine(struct lp5523_engine *engine, int id)
839 {
840         if (id < 1 || id > LP5523_ENGINES)
841                 return -1;
842         engine->id = id;
843         engine->engine_mask = LP5523_ENG_MASK_BASE >> SHIFT_MASK(id);
844         engine->prog_page = id - 1;
845         engine->mux_page = id + 2;
846
847         return 0;
848 }
849
850 static int lp5523_init_led(struct lp5523_led *led, struct device *dev,
851                            int chan, struct lp5523_platform_data *pdata,
852                            const char *chip_name)
853 {
854         char name[32];
855         int res;
856
857         if (chan >= LP5523_LEDS)
858                 return -EINVAL;
859
860         if (pdata->led_config[chan].led_current) {
861                 led->led_current = pdata->led_config[chan].led_current;
862                 led->max_current = pdata->led_config[chan].max_current;
863                 led->chan_nr = pdata->led_config[chan].chan_nr;
864
865                 if (led->chan_nr >= LP5523_LEDS) {
866                         dev_err(dev, "Use channel numbers between 0 and %d\n",
867                                 LP5523_LEDS - 1);
868                         return -EINVAL;
869                 }
870
871                 if (pdata->led_config[chan].name) {
872                         led->cdev.name = pdata->led_config[chan].name;
873                 } else {
874                         snprintf(name, sizeof(name), "%s:channel%d",
875                                 pdata->label ? : chip_name, chan);
876                         led->cdev.name = name;
877                 }
878
879                 led->cdev.brightness_set = lp5523_set_brightness;
880                 res = led_classdev_register(dev, &led->cdev);
881                 if (res < 0) {
882                         dev_err(dev, "couldn't register led on channel %d\n",
883                                 chan);
884                         return res;
885                 }
886                 res = sysfs_create_group(&led->cdev.dev->kobj,
887                                 &lp5523_led_attribute_group);
888                 if (res < 0) {
889                         dev_err(dev, "couldn't register current attribute\n");
890                         led_classdev_unregister(&led->cdev);
891                         return res;
892                 }
893         } else {
894                 led->led_current = 0;
895         }
896         return 0;
897 }
898
899 static int lp5523_probe(struct i2c_client *client,
900                         const struct i2c_device_id *id)
901 {
902         struct lp5523_chip              *chip;
903         struct lp5523_platform_data     *pdata;
904         int ret, i, led;
905
906         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
907         if (!chip)
908                 return -ENOMEM;
909
910         i2c_set_clientdata(client, chip);
911         chip->client = client;
912
913         pdata = client->dev.platform_data;
914
915         if (!pdata) {
916                 dev_err(&client->dev, "no platform data\n");
917                 return -EINVAL;
918         }
919
920         mutex_init(&chip->lock);
921
922         chip->pdata   = pdata;
923
924         if (pdata->setup_resources) {
925                 ret = pdata->setup_resources();
926                 if (ret < 0)
927                         return ret;
928         }
929
930         if (pdata->enable) {
931                 pdata->enable(0);
932                 usleep_range(1000, 2000); /* Keep enable down at least 1ms */
933                 pdata->enable(1);
934                 usleep_range(1000, 2000); /* 500us abs min. */
935         }
936
937         lp5523_write(client, LP5523_REG_RESET, 0xff);
938         usleep_range(10000, 20000); /*
939                                      * Exact value is not available. 10 - 20ms
940                                      * appears to be enough for reset.
941                                      */
942         ret = lp5523_detect(client);
943         if (ret)
944                 goto fail1;
945
946         dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
947
948         /* Initialize engines */
949         for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
950                 ret = lp5523_init_engine(&chip->engines[i], i + 1);
951                 if (ret) {
952                         dev_err(&client->dev, "error initializing engine\n");
953                         goto fail1;
954                 }
955         }
956         ret = lp5523_configure(client);
957         if (ret < 0) {
958                 dev_err(&client->dev, "error configuring chip\n");
959                 goto fail1;
960         }
961
962         /* Initialize leds */
963         chip->num_channels = pdata->num_channels;
964         chip->num_leds = 0;
965         led = 0;
966         for (i = 0; i < pdata->num_channels; i++) {
967                 /* Do not initialize channels that are not connected */
968                 if (pdata->led_config[i].led_current == 0)
969                         continue;
970
971                 INIT_WORK(&chip->leds[led].brightness_work,
972                         lp5523_led_brightness_work);
973
974                 ret = lp5523_init_led(&chip->leds[led], &client->dev, i, pdata,
975                                 id->name);
976                 if (ret) {
977                         dev_err(&client->dev, "error initializing leds\n");
978                         goto fail2;
979                 }
980                 chip->num_leds++;
981
982                 chip->leds[led].id = led;
983                 /* Set LED current */
984                 lp5523_write(client,
985                           LP5523_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
986                           chip->leds[led].led_current);
987
988                 led++;
989         }
990
991         ret = lp5523_register_sysfs(client);
992         if (ret) {
993                 dev_err(&client->dev, "registering sysfs failed\n");
994                 goto fail2;
995         }
996         return ret;
997 fail2:
998         for (i = 0; i < chip->num_leds; i++) {
999                 led_classdev_unregister(&chip->leds[i].cdev);
1000                 flush_work(&chip->leds[i].brightness_work);
1001         }
1002 fail1:
1003         if (pdata->enable)
1004                 pdata->enable(0);
1005         if (pdata->release_resources)
1006                 pdata->release_resources();
1007         return ret;
1008 }
1009
1010 static int lp5523_remove(struct i2c_client *client)
1011 {
1012         struct lp5523_chip *chip = i2c_get_clientdata(client);
1013         int i;
1014
1015         /* Disable engine mode */
1016         lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
1017
1018         lp5523_unregister_sysfs(client);
1019
1020         for (i = 0; i < chip->num_leds; i++) {
1021                 led_classdev_unregister(&chip->leds[i].cdev);
1022                 flush_work(&chip->leds[i].brightness_work);
1023         }
1024
1025         if (chip->pdata->enable)
1026                 chip->pdata->enable(0);
1027         if (chip->pdata->release_resources)
1028                 chip->pdata->release_resources();
1029         return 0;
1030 }
1031
1032 static const struct i2c_device_id lp5523_id[] = {
1033         { "lp5523",  LP5523 },
1034         { "lp55231", LP55231 },
1035         { }
1036 };
1037
1038 MODULE_DEVICE_TABLE(i2c, lp5523_id);
1039
1040 static struct i2c_driver lp5523_driver = {
1041         .driver = {
1042                 .name   = "lp5523x",
1043         },
1044         .probe          = lp5523_probe,
1045         .remove         = lp5523_remove,
1046         .id_table       = lp5523_id,
1047 };
1048
1049 module_i2c_driver(lp5523_driver);
1050
1051 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
1052 MODULE_DESCRIPTION("LP5523 LED engine");
1053 MODULE_LICENSE("GPL");