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 LP5521_PROGRAM_LENGTH 32
40 #define LP5521_MAX_LEDS 3
41 #define LP5521_MAX_ENGINES 3
43 #define LP5521_ENG_MASK_BASE 0x30
44 #define LP5521_ENG_STATUS_MASK 0x07
46 #define LP5521_CMD_LOAD 0x15
47 #define LP5521_CMD_RUN 0x2a
48 #define LP5521_CMD_DIRECT 0x3f
49 #define LP5521_CMD_DISABLED 0x00
52 #define LP5521_REG_ENABLE 0x00
53 #define LP5521_REG_OP_MODE 0x01
54 #define LP5521_REG_R_PWM 0x02
55 #define LP5521_REG_G_PWM 0x03
56 #define LP5521_REG_B_PWM 0x04
57 #define LP5521_REG_R_CURRENT 0x05
58 #define LP5521_REG_G_CURRENT 0x06
59 #define LP5521_REG_B_CURRENT 0x07
60 #define LP5521_REG_CONFIG 0x08
61 #define LP5521_REG_R_CHANNEL_PC 0x09
62 #define LP5521_REG_G_CHANNEL_PC 0x0A
63 #define LP5521_REG_B_CHANNEL_PC 0x0B
64 #define LP5521_REG_STATUS 0x0C
65 #define LP5521_REG_RESET 0x0D
66 #define LP5521_REG_GPO 0x0E
67 #define LP5521_REG_R_PROG_MEM 0x10
68 #define LP5521_REG_G_PROG_MEM 0x30
69 #define LP5521_REG_B_PROG_MEM 0x50
71 #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
72 #define LP5521_PROG_MEM_SIZE 0x20
75 #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
78 #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
81 #define LP5521_MASTER_ENABLE 0x40
82 #define LP5521_LOGARITHMIC_PWM 0x80
83 #define LP5521_EXEC_RUN 0x2A
84 #define LP5521_ENABLE_DEFAULT \
85 (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM)
86 #define LP5521_ENABLE_RUN_PROGRAM \
87 (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN)
90 #define LP5521_EXT_CLK_USED 0x08
93 #define LP5521_REG_R_CURR_DEFAULT 0xAF
182 engine_state |=
mode;
224 return lp5521_write(chip->
client,
229 static void lp5521_init_engine(
struct lp5521_chip *chip)
239 static int lp5521_configure(
struct i2c_client *client)
241 struct lp5521_chip *chip = i2c_get_clientdata(client);
245 lp5521_init_engine(chip);
250 cfg = chip->
pdata->update_config ?
268 static int lp5521_run_selftest(
struct lp5521_chip *chip,
char *buf)
307 static int lp5521_detect(
struct i2c_client *client)
349 static int lp5521_do_store_load(
struct lp5521_engine *engine,
350 const char *buf,
size_t len)
352 struct lp5521_chip *chip = engine_to_lp5521(engine);
361 ret =
sscanf(buf + offset,
"%2s%n ", c, &nrchars);
364 ret =
sscanf(c,
"%2x", &cmd);
367 pattern[
i] = (
u8)cmd;
379 ret = lp5521_load_program(engine, pattern);
385 dev_err(&client->
dev,
"failed loading pattern\n");
391 dev_err(&client->
dev,
"wrong pattern format\n");
397 const char *buf,
size_t len,
int nr)
400 struct lp5521_chip *chip = i2c_get_clientdata(client);
401 return lp5521_do_store_load(&chip->
engines[nr - 1], buf, len);
404 #define store_load(nr) \
405 static ssize_t store_engine##nr##_load(struct device *dev, \
406 struct device_attribute *attr, \
407 const char *buf, size_t len) \
409 return store_engine_load(dev, attr, buf, len, nr); \
420 struct lp5521_chip *chip = i2c_get_clientdata(client);
421 switch (chip->
engines[nr - 1].mode) {
427 return sprintf(buf,
"disabled\n");
429 return sprintf(buf,
"disabled\n");
433 #define show_mode(nr) \
434 static ssize_t show_engine##nr##_mode(struct device *dev, \
435 struct device_attribute *attr, \
438 return show_engine_mode(dev, attr, buf, nr); \
446 const char *buf,
size_t len,
int nr)
449 struct lp5521_chip *chip = i2c_get_clientdata(client);
455 else if (!
strncmp(buf,
"load", 4))
457 else if (!
strncmp(buf,
"disabled", 8))
464 #define store_mode(nr) \
465 static ssize_t store_engine##nr##_mode(struct device *dev, \
466 struct device_attribute *attr, \
467 const char *buf, size_t len) \
469 return store_engine_mode(dev, attr, buf, len, nr); \
497 const char *buf,
size_t len)
505 if (kstrtoul(buf, 0, &curr))
512 ret = lp5521_set_led_current(chip, led->
id, curr);
528 struct lp5521_chip *chip = i2c_get_clientdata(client);
532 ret = lp5521_run_selftest(chip, buf);
534 return sprintf(buf,
"%s\n", ret ?
"FAIL" :
"OK");
537 static void lp5521_clear_program_memory(
struct i2c_client *
cl)
547 lp5521_write(cl, rgb_mem[i], 0);
548 lp5521_write(cl, rgb_mem[i] + 1, 0);
552 static void lp5521_write_program_memory(
struct i2c_client *cl,
557 if (!rgb || size <= 0)
560 for (i = 0; i <
size; i++)
561 lp5521_write(cl, base + i, *(rgb + i));
563 lp5521_write(cl, base + i, 0);
564 lp5521_write(cl, base + i + 1, 0);
571 ptn = chip->
pdata->patterns + (offset - 1);
575 static void lp5521_run_led_pattern(
int mode,
struct lp5521_chip *chip)
579 int num_patterns = chip->
pdata->num_patterns;
581 if (mode > num_patterns || !(chip->
pdata->patterns))
589 ptn = lp5521_get_pattern(chip, mode);
596 lp5521_clear_program_memory(cl);
613 const char *buf,
size_t len)
623 lp5521_run_led_pattern(val, chip);
632 static struct attribute *lp5521_led_attributes[] = {
633 &dev_attr_led_current.attr,
634 &dev_attr_max_current.attr,
639 .attrs = lp5521_led_attributes
644 show_engine1_mode, store_engine1_mode);
646 show_engine2_mode, store_engine2_mode);
648 show_engine3_mode, store_engine3_mode);
655 static struct attribute *lp5521_attributes[] = {
656 &dev_attr_engine1_mode.attr,
657 &dev_attr_engine2_mode.attr,
658 &dev_attr_engine3_mode.attr,
659 &dev_attr_selftest.attr,
660 &dev_attr_engine1_load.attr,
661 &dev_attr_engine2_load.attr,
662 &dev_attr_engine3_load.attr,
663 &dev_attr_led_pattern.attr,
668 .attrs = lp5521_attributes,
671 static int lp5521_register_sysfs(
struct i2c_client *client)
677 static void lp5521_unregister_sysfs(
struct i2c_client *client)
679 struct lp5521_chip *chip = i2c_get_clientdata(client);
685 for (i = 0; i < chip->
num_leds; i++)
687 &lp5521_led_attribute_group);
709 dev_err(dev,
"Use channel numbers between 0 and %d\n",
714 led->
cdev.brightness_set = lp5521_set_brightness;
718 snprintf(name,
sizeof(name),
"%s:channel%d",
725 dev_err(dev,
"couldn't register led on channel %d\n", chan);
730 &lp5521_led_attribute_group);
732 dev_err(dev,
"couldn't register current attribute\n");
751 i2c_set_clientdata(client, chip);
754 pdata = client->
dev.platform_data;
792 dev_err(&client->
dev,
"error in resetting chip\n");
797 ret = lp5521_detect(client);
806 ret = lp5521_configure(client);
808 dev_err(&client->
dev,
"error configuring chip\n");
821 ret = lp5521_init_led(&chip->
leds[led], client, i, pdata);
823 dev_err(&client->
dev,
"error initializing leds\n");
830 lp5521_set_led_current(chip, led,
831 chip->
leds[led].led_current);
834 lp5521_led_brightness_work);
839 ret = lp5521_register_sysfs(client);
841 dev_err(&client->
dev,
"registering sysfs failed\n");
846 for (i = 0; i < chip->
num_leds; i++) {
860 struct lp5521_chip *chip = i2c_get_clientdata(client);
864 lp5521_unregister_sysfs(client);
866 for (i = 0; i < chip->
num_leds; i++) {
871 if (chip->
pdata->enable)
872 chip->
pdata->enable(0);
873 if (chip->
pdata->release_resources)
874 chip->
pdata->release_resources();
888 .probe = lp5521_probe,
890 .id_table = lp5521_id,
895 MODULE_AUTHOR(
"Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");