2 * lp5523.c - LP5523 LED Driver
4 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
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.
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.
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
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>
39 #include "leds-lp55xx-common.h"
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
75 #define LP5523_CMD_LOAD 0x15 /* 00010101 */
76 #define LP5523_CMD_RUN 0x2a /* 00101010 */
77 #define LP5523_CMD_DISABLED 0x00 /* 00000000 */
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
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
98 #define LP5523_ENGINES 3
100 #define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */
102 #define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */
104 #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING
106 #define LP5523_EXT_CLK_USED 0x08
108 #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
109 #define SHIFT_MASK(id) (((id) - 1) * 2)
111 enum lp5523_chip_id {
116 struct lp5523_engine {
130 struct led_classdev cdev;
131 struct work_struct brightness_work;
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;
145 static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
147 return container_of(cdev, struct lp5523_led, cdev);
150 static inline struct lp5523_chip *engine_to_lp5523(struct lp5523_engine *engine)
152 return container_of(engine, struct lp5523_chip,
153 engines[engine->id - 1]);
156 static inline struct lp5523_chip *led_to_lp5523(struct lp5523_led *led)
158 return container_of(led, struct lp5523_chip,
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);
166 static void lp5523_led_brightness_work(struct work_struct *work);
168 static int lp5523_write(struct i2c_client *client, u8 reg, u8 value)
170 return i2c_smbus_write_byte_data(client, reg, value);
173 static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf)
175 s32 ret = i2c_smbus_read_byte_data(client, reg);
184 static int lp5523_post_init_device(struct lp55xx_chip *chip)
188 ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE);
192 /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
193 usleep_range(1000, 2000);
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);
202 /* turn on all leds */
203 ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01);
207 return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff);
210 static int lp5523_set_engine_mode(struct lp5523_engine *engine, u8 mode)
212 struct lp5523_chip *chip = engine_to_lp5523(engine);
213 struct i2c_client *client = chip->client;
217 ret = lp5523_read(client, LP5523_REG_OP_MODE, &engine_state);
221 engine_state &= ~(engine->engine_mask);
223 /* set mode only for this engine */
224 mode &= engine->engine_mask;
226 engine_state |= mode;
228 ret |= lp5523_write(client, LP5523_REG_OP_MODE, engine_state);
233 static int lp5523_load_mux(struct lp5523_engine *engine, u16 mux)
235 struct lp5523_chip *chip = engine_to_lp5523(engine);
236 struct i2c_client *client = chip->client;
239 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
241 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL, engine->mux_page);
242 ret |= lp5523_write(client, LP5523_REG_PROG_MEM,
244 ret |= lp5523_write(client, LP5523_REG_PROG_MEM + 1, (u8)(mux));
245 engine->led_mux = mux;
250 static int lp5523_load_program(struct lp5523_engine *engine, const u8 *pattern)
252 struct lp5523_chip *chip = engine_to_lp5523(engine);
253 struct i2c_client *client = chip->client;
257 ret |= lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
259 ret |= lp5523_write(client, LP5523_REG_PROG_PAGE_SEL,
261 ret |= i2c_smbus_write_i2c_block_data(client, LP5523_REG_PROG_MEM,
262 LP5523_PROGRAM_LENGTH, pattern);
267 static int lp5523_run_program(struct lp5523_engine *engine)
269 struct lp5523_chip *chip = engine_to_lp5523(engine);
270 struct i2c_client *client = chip->client;
273 ret = lp5523_write(client, LP5523_REG_ENABLE,
274 LP5523_CMD_RUN | LP5523_ENABLE);
278 ret = lp5523_set_engine_mode(engine, LP5523_CMD_RUN);
283 static int lp5523_mux_parse(const char *buf, u16 *mux, size_t len)
288 len = min_t(int, len, LP5523_LEDS);
289 for (i = 0; i < len; i++) {
308 static void lp5523_mux_to_array(u16 led_mux, char *array)
311 for (i = 0; i < LP5523_LEDS; i++)
312 pos += sprintf(array + pos, "%x", LED_ACTIVE(led_mux, i));
317 /*--------------------------------------------------------------*/
318 /* Sysfs interface */
319 /*--------------------------------------------------------------*/
321 static ssize_t show_engine_leds(struct device *dev,
322 struct device_attribute *attr,
325 struct i2c_client *client = to_i2c_client(dev);
326 struct lp5523_chip *chip = i2c_get_clientdata(client);
327 char mux[LP5523_LEDS + 1];
329 lp5523_mux_to_array(chip->engines[nr - 1].led_mux, mux);
331 return sprintf(buf, "%s\n", mux);
334 #define show_leds(nr) \
335 static ssize_t show_engine##nr##_leds(struct device *dev, \
336 struct device_attribute *attr, \
339 return show_engine_leds(dev, attr, buf, nr); \
345 static ssize_t store_engine_leds(struct device *dev,
346 struct device_attribute *attr,
347 const char *buf, size_t len, int nr)
349 struct i2c_client *client = to_i2c_client(dev);
350 struct lp5523_chip *chip = i2c_get_clientdata(client);
354 if (lp5523_mux_parse(buf, &mux, len))
357 mutex_lock(&chip->lock);
359 if (chip->engines[nr - 1].mode != LP5523_CMD_LOAD)
362 if (lp5523_load_mux(&chip->engines[nr - 1], mux))
367 mutex_unlock(&chip->lock);
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) \
376 return store_engine_leds(dev, attr, buf, len, nr); \
382 static ssize_t lp5523_selftest(struct device *dev,
383 struct device_attribute *attr,
386 struct i2c_client *client = to_i2c_client(dev);
387 struct lp5523_chip *chip = i2c_get_clientdata(client);
392 mutex_lock(&chip->lock);
394 ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status);
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)
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);
411 if (!(status & LP5523_LEDTEST_DONE))
412 usleep_range(3000, 6000); /* Was not ready. Wait little bit */
414 ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd);
418 vdd--; /* There may be some fluctuation in measurement */
420 for (i = 0; i < LP5523_LEDS; i++) {
421 /* Skip non-existing channels */
422 if (chip->pdata->led_config[i].led_current == 0)
425 /* Set default current */
426 lp5523_write(chip->client,
427 LP5523_REG_LED_CURRENT_BASE + i,
428 chip->pdata->led_config[i].led_current);
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);
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);
448 if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM)
449 pos += sprintf(buf + pos, "LED %d FAIL\n", i);
451 lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00);
453 /* Restore current */
454 lp5523_write(chip->client,
455 LP5523_REG_LED_CURRENT_BASE + i,
456 chip->leds[led].led_current);
460 pos = sprintf(buf, "OK\n");
463 pos = sprintf(buf, "FAIL\n");
466 mutex_unlock(&chip->lock);
471 static void lp5523_set_brightness(struct led_classdev *cdev,
472 enum led_brightness brightness)
474 struct lp5523_led *led = cdev_to_led(cdev);
476 led->brightness = (u8)brightness;
478 schedule_work(&led->brightness_work);
481 static void lp5523_led_brightness_work(struct work_struct *work)
483 struct lp5523_led *led = container_of(work,
486 struct lp5523_chip *chip = led_to_lp5523(led);
487 struct i2c_client *client = chip->client;
489 mutex_lock(&chip->lock);
491 lp5523_write(client, LP5523_REG_LED_PWM_BASE + led->chan_nr,
494 mutex_unlock(&chip->lock);
497 static int lp5523_do_store_load(struct lp5523_engine *engine,
498 const char *buf, size_t len)
500 struct lp5523_chip *chip = engine_to_lp5523(engine);
501 struct i2c_client *client = chip->client;
502 int ret, nrchars, offset = 0, i = 0;
505 u8 pattern[LP5523_PROGRAM_LENGTH] = {0};
507 if (engine->mode != LP5523_CMD_LOAD)
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);
516 pattern[i] = (u8)cmd;
522 /* Each instruction is 16bit long. Check that length is even */
526 mutex_lock(&chip->lock);
527 ret = lp5523_load_program(engine, pattern);
528 mutex_unlock(&chip->lock);
531 dev_err(&client->dev, "failed loading pattern\n");
537 dev_err(&client->dev, "wrong pattern format\n");
541 static ssize_t store_engine_load(struct device *dev,
542 struct device_attribute *attr,
543 const char *buf, size_t len, int nr)
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);
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) \
555 return store_engine_load(dev, attr, buf, len, nr); \
561 static ssize_t show_engine_mode(struct device *dev,
562 struct device_attribute *attr,
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) {
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");
575 return sprintf(buf, "disabled\n");
579 #define show_mode(nr) \
580 static ssize_t show_engine##nr##_mode(struct device *dev, \
581 struct device_attribute *attr, \
584 return show_engine_mode(dev, attr, buf, nr); \
590 static ssize_t store_engine_mode(struct device *dev,
591 struct device_attribute *attr,
592 const char *buf, size_t len, int nr)
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);
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);
606 mutex_unlock(&chip->lock);
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) \
615 return store_engine_mode(dev, attr, buf, len, nr); \
621 static ssize_t show_max_current(struct device *dev,
622 struct device_attribute *attr,
625 struct led_classdev *led_cdev = dev_get_drvdata(dev);
626 struct lp5523_led *led = cdev_to_led(led_cdev);
628 return sprintf(buf, "%d\n", led->max_current);
631 static ssize_t show_current(struct device *dev,
632 struct device_attribute *attr,
635 struct led_classdev *led_cdev = dev_get_drvdata(dev);
636 struct lp5523_led *led = cdev_to_led(led_cdev);
638 return sprintf(buf, "%d\n", led->led_current);
641 static ssize_t store_current(struct device *dev,
642 struct device_attribute *attr,
643 const char *buf, size_t len)
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);
651 if (kstrtoul(buf, 0, &curr))
654 if (curr > led->max_current)
657 mutex_lock(&chip->lock);
658 ret = lp5523_write(chip->client,
659 LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
661 mutex_unlock(&chip->lock);
666 led->led_current = (u8)curr;
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);
675 static struct attribute *lp5523_led_attributes[] = {
676 &dev_attr_led_current.attr,
677 &dev_attr_max_current.attr,
681 static struct attribute_group lp5523_led_attribute_group = {
682 .attrs = lp5523_led_attributes
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);
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,
717 static const struct attribute_group lp5523_group = {
718 .attrs = lp5523_attributes,
721 static int lp5523_register_sysfs(struct i2c_client *client)
723 struct device *dev = &client->dev;
726 ret = sysfs_create_group(&dev->kobj, &lp5523_group);
733 static void lp5523_unregister_sysfs(struct i2c_client *client)
735 struct lp5523_chip *chip = i2c_get_clientdata(client);
736 struct device *dev = &client->dev;
739 sysfs_remove_group(&dev->kobj, &lp5523_group);
741 for (i = 0; i < chip->num_leds; i++)
742 sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
743 &lp5523_led_attribute_group);
746 /*--------------------------------------------------------------*/
747 /* Set chip operating mode */
748 /*--------------------------------------------------------------*/
749 static void lp5523_set_mode(struct lp5523_engine *engine, u8 mode)
751 /* if in that mode already do nothing, except for run */
752 if (mode == engine->mode && mode != LP5523_CMD_RUN)
757 lp5523_run_program(engine);
759 case LP5523_CMD_LOAD:
760 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
761 lp5523_set_engine_mode(engine, LP5523_CMD_LOAD);
763 case LP5523_CMD_DISABLED:
764 lp5523_set_engine_mode(engine, LP5523_CMD_DISABLED);
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)
783 if (chan >= LP5523_LEDS)
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;
791 if (led->chan_nr >= LP5523_LEDS) {
792 dev_err(dev, "Use channel numbers between 0 and %d\n",
797 if (pdata->led_config[chan].name) {
798 led->cdev.name = pdata->led_config[chan].name;
800 snprintf(name, sizeof(name), "%s:channel%d",
801 pdata->label ? : chip_name, chan);
802 led->cdev.name = name;
805 led->cdev.brightness_set = lp5523_set_brightness;
806 res = led_classdev_register(dev, &led->cdev);
808 dev_err(dev, "couldn't register led on channel %d\n",
812 res = sysfs_create_group(&led->cdev.dev->kobj,
813 &lp5523_led_attribute_group);
815 dev_err(dev, "couldn't register current attribute\n");
816 led_classdev_unregister(&led->cdev);
820 led->led_current = 0;
825 static int lp5523_register_leds(struct lp5523_chip *chip, const char *name)
827 struct lp5523_platform_data *pdata = chip->pdata;
828 struct i2c_client *client = chip->client;
833 /* Initialize leds */
834 chip->num_channels = pdata->num_channels;
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)
842 INIT_WORK(&chip->leds[led].brightness_work,
843 lp5523_led_brightness_work);
845 ret = lp5523_init_led(&chip->leds[led], &client->dev, i, pdata,
848 dev_err(&client->dev, "error initializing leds\n");
853 chip->leds[led].id = led;
854 /* Set LED current */
856 LP5523_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
857 chip->leds[led].led_current);
865 static void lp5523_unregister_leds(struct lp5523_chip *chip)
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);
875 static void lp5523_deinit_device(struct lp5523_chip *chip)
877 struct lp5523_platform_data *pdata = chip->pdata;
881 if (pdata->release_resources)
882 pdata->release_resources();
885 /* Chip specific configurations */
886 static struct lp55xx_device_config lp5523_cfg = {
888 .addr = LP5523_REG_RESET,
892 .addr = LP5523_REG_ENABLE,
893 .val = LP5523_ENABLE,
895 .post_init_device = lp5523_post_init_device,
898 static int lp5523_probe(struct i2c_client *client,
899 const struct i2c_device_id *id)
901 struct lp5523_chip *old_chip = NULL;
903 struct lp55xx_chip *chip;
904 struct lp55xx_led *led;
905 struct lp55xx_platform_data *pdata = client->dev.platform_data;
908 dev_err(&client->dev, "no platform data\n");
912 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
916 led = devm_kzalloc(&client->dev,
917 sizeof(*led) * pdata->num_channels, GFP_KERNEL);
923 chip->cfg = &lp5523_cfg;
925 mutex_init(&chip->lock);
927 i2c_set_clientdata(client, led);
929 ret = lp55xx_init_device(chip);
933 dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
935 ret = lp5523_register_leds(old_chip, id->name);
939 ret = lp5523_register_sysfs(client);
941 dev_err(&client->dev, "registering sysfs failed\n");
946 lp5523_unregister_leds(old_chip);
947 lp5523_deinit_device(old_chip);
952 static int lp5523_remove(struct i2c_client *client)
954 struct lp5523_chip *old_chip = i2c_get_clientdata(client);
956 /* Disable engine mode */
957 lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED);
959 lp5523_unregister_sysfs(client);
961 lp5523_unregister_leds(old_chip);
963 lp5523_deinit_device(old_chip);
967 static const struct i2c_device_id lp5523_id[] = {
968 { "lp5523", LP5523 },
969 { "lp55231", LP55231 },
973 MODULE_DEVICE_TABLE(i2c, lp5523_id);
975 static struct i2c_driver lp5523_driver = {
979 .probe = lp5523_probe,
980 .remove = lp5523_remove,
981 .id_table = lp5523_id,
984 module_i2c_driver(lp5523_driver);
986 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
987 MODULE_DESCRIPTION("LP5523 LED engine");
988 MODULE_LICENSE("GPL");