23 #include <linux/module.h>
25 #include <linux/i2c.h>
30 #include <linux/ctype.h>
32 #include <linux/wait.h>
36 #include <linux/slab.h>
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
72 #define LP5523_CMD_LOAD 0x15
73 #define LP5523_CMD_RUN 0x2a
74 #define LP5523_CMD_DISABLED 0x00
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
88 #define LP5523_DEFAULT_CURRENT 50
89 #define LP5523_PROGRAM_LENGTH 32
90 #define LP5523_PROGRAM_PAGES 6
91 #define LP5523_ADC_SHORTCIRC_LIM 80
94 #define LP5523_ENGINES 3
96 #define LP5523_ENG_MASK_BASE 0x30
98 #define LP5523_ENG_STATUS_MASK 0x07
100 #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING
102 #define LP5523_EXT_CLK_USED 0x08
104 #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led)))
105 #define SHIFT_MASK(id) (((id) - 1) * 2)
180 static int lp5523_detect(
struct i2c_client *client)
197 static int lp5523_configure(
struct i2c_client *client)
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},
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]);
234 dev_err(&client->
dev,
"could not load mux programs\n");
245 dev_err(&client->
dev,
"could not start mux programs\n");
255 dev_dbg(&client->
dev,
"all engines configured\n");
258 dev_err(&client->
dev,
"cound not configure LED engine\n");
271 struct lp5523_chip *chip = engine_to_lp5523(engine);
285 engine_state |=
mode;
294 struct lp5523_chip *chip = engine_to_lp5523(engine);
309 static int lp5523_load_program(
struct lp5523_engine *engine,
const u8 *pattern)
311 struct lp5523_chip *chip = engine_to_lp5523(engine);
328 struct lp5523_chip *chip = engine_to_lp5523(engine);
342 static int lp5523_mux_parse(
const char *buf,
u16 *mux,
size_t len)
348 for (i = 0; i < len; i++) {
367 static void lp5523_mux_to_array(
u16 led_mux,
char *array)
385 struct lp5523_chip *chip = i2c_get_clientdata(client);
386 char mux[LP5523_LEDS + 1];
388 lp5523_mux_to_array(chip->
engines[nr - 1].led_mux, mux);
390 return sprintf(buf,
"%s\n", mux);
393 #define show_leds(nr) \
394 static ssize_t show_engine##nr##_leds(struct device *dev, \
395 struct device_attribute *attr, \
398 return show_engine_leds(dev, attr, buf, nr); \
406 const char *buf,
size_t len,
int nr)
409 struct lp5523_chip *chip = i2c_get_clientdata(client);
413 if (lp5523_mux_parse(buf, &mux, len))
421 if (lp5523_load_mux(&chip->
engines[nr - 1], mux))
430 #define store_leds(nr) \
431 static ssize_t store_engine##nr##_leds(struct device *dev, \
432 struct device_attribute *attr, \
433 const char *buf, size_t len) \
435 return store_engine_leds(dev, attr, buf, len, nr); \
446 struct lp5523_chip *chip = i2c_get_clientdata(client);
475 if (chip->
pdata->led_config[i].led_current == 0)
479 lp5523_write(chip->
client,
481 chip->
pdata->led_config[i].led_current);
486 lp5523_write(chip->
client,
492 if (!(status & LP5523_LEDTEST_DONE))
497 pos +=
sprintf(buf + pos,
"LED %d FAIL\n", i);
502 lp5523_write(chip->
client,
504 chip->
leds[led].led_current);
545 static int lp5523_do_store_load(
struct lp5523_engine *engine,
546 const char *buf,
size_t len)
548 struct lp5523_chip *chip = engine_to_lp5523(engine);
560 ret =
sscanf(buf + offset,
"%2s%n ", c, &nrchars);
561 ret =
sscanf(c,
"%2x", &cmd);
564 pattern[
i] = (
u8)cmd;
575 ret = lp5523_load_program(engine, pattern);
579 dev_err(&client->
dev,
"failed loading pattern\n");
585 dev_err(&client->
dev,
"wrong pattern format\n");
591 const char *buf,
size_t len,
int nr)
594 struct lp5523_chip *chip = i2c_get_clientdata(client);
595 return lp5523_do_store_load(&chip->
engines[nr - 1], buf, len);
598 #define store_load(nr) \
599 static ssize_t store_engine##nr##_load(struct device *dev, \
600 struct device_attribute *attr, \
601 const char *buf, size_t len) \
603 return store_engine_load(dev, attr, buf, len, nr); \
614 struct lp5523_chip *chip = i2c_get_clientdata(client);
615 switch (chip->
engines[nr - 1].mode) {
621 return sprintf(buf,
"disabled\n");
623 return sprintf(buf,
"disabled\n");
627 #define show_mode(nr) \
628 static ssize_t show_engine##nr##_mode(struct device *dev, \
629 struct device_attribute *attr, \
632 return show_engine_mode(dev, attr, buf, nr); \
640 const char *buf,
size_t len,
int nr)
643 struct lp5523_chip *chip = i2c_get_clientdata(client);
649 else if (!
strncmp(buf,
"load", 4))
651 else if (!
strncmp(buf,
"disabled", 8))
658 #define store_mode(nr) \
659 static ssize_t store_engine##nr##_mode(struct device *dev, \
660 struct device_attribute *attr, \
661 const char *buf, size_t len) \
663 return store_engine_mode(dev, attr, buf, len, nr); \
691 const char *buf,
size_t len)
706 ret = lp5523_write(chip->
client,
723 static struct attribute *lp5523_led_attributes[] = {
724 &dev_attr_led_current.attr,
725 &dev_attr_max_current.attr,
730 .attrs = lp5523_led_attributes
735 show_engine1_mode, store_engine1_mode);
737 show_engine2_mode, store_engine2_mode);
739 show_engine3_mode, store_engine3_mode);
741 show_engine1_leds, store_engine1_leds);
743 show_engine2_leds, store_engine2_leds);
745 show_engine3_leds, store_engine3_leds);
751 static struct attribute *lp5523_attributes[] = {
752 &dev_attr_engine1_mode.attr,
753 &dev_attr_engine2_mode.attr,
754 &dev_attr_engine3_mode.attr,
755 &dev_attr_selftest.attr,
756 &dev_attr_engine1_load.attr,
757 &dev_attr_engine1_leds.attr,
758 &dev_attr_engine2_load.attr,
759 &dev_attr_engine2_leds.attr,
760 &dev_attr_engine3_load.attr,
761 &dev_attr_engine3_leds.attr,
766 .attrs = lp5523_attributes,
769 static int lp5523_register_sysfs(
struct i2c_client *client)
781 static void lp5523_unregister_sysfs(
struct i2c_client *client)
783 struct lp5523_chip *chip = i2c_get_clientdata(client);
789 for (i = 0; i < chip->
num_leds; i++)
791 &lp5523_led_attribute_group);
805 lp5523_run_program(engine);
838 const char *chip_name)
843 if (chan >= LP5523_LEDS)
851 if (led->
chan_nr >= LP5523_LEDS) {
852 dev_err(dev,
"Use channel numbers between 0 and %d\n",
860 snprintf(name,
sizeof(name),
"%s:channel%d",
861 pdata->
label ? : chip_name, chan);
865 led->
cdev.brightness_set = lp5523_set_brightness;
868 dev_err(dev,
"couldn't register led on channel %d\n",
873 &lp5523_led_attribute_group);
875 dev_err(dev,
"couldn't register current attribute\n");
896 i2c_set_clientdata(client, chip);
899 pdata = client->
dev.platform_data;
928 ret = lp5523_detect(client);
936 ret = lp5523_init_engine(&chip->
engines[i], i + 1);
938 dev_err(&client->
dev,
"error initializing engine\n");
942 ret = lp5523_configure(client);
944 dev_err(&client->
dev,
"error configuring chip\n");
958 lp5523_led_brightness_work);
960 ret = lp5523_init_led(&chip->
leds[led], &client->
dev, i, pdata,
963 dev_err(&client->
dev,
"error initializing leds\n");
972 chip->
leds[led].led_current);
977 ret = lp5523_register_sysfs(client);
979 dev_err(&client->
dev,
"registering sysfs failed\n");
984 for (i = 0; i < chip->
num_leds; i++) {
996 static int lp5523_remove(
struct i2c_client *client)
998 struct lp5523_chip *chip = i2c_get_clientdata(client);
1004 lp5523_unregister_sysfs(client);
1006 for (i = 0; i < chip->
num_leds; i++) {
1011 if (chip->
pdata->enable)
1012 chip->
pdata->enable(0);
1013 if (chip->
pdata->release_resources)
1014 chip->
pdata->release_resources();
1030 .probe = lp5523_probe,
1031 .remove = lp5523_remove,
1032 .id_table = lp5523_id,