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