2 * lp5521.c - LP5521 LED Driver
4 * Copyright (C) 2007 Nokia Corporation
6 * Written by Mathias Nyman <mathias.nyman@nokia.com>
7 * Updated by Felipe Balbi <felipe.balbi@nokia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/i2c.h>
27 #include <linux/leds.h>
28 #include <linux/mutex.h>
29 #include <linux/workqueue.h>
30 #include <linux/i2c/lp5521.h>
32 #define LP5521_DRIVER_NAME "lp5521"
34 #define LP5521_REG_R_PWM 0x02
35 #define LP5521_REG_B_PWM 0x04
36 #define LP5521_REG_ENABLE 0x00
37 #define LP5521_REG_OP_MODE 0x01
38 #define LP5521_REG_G_PWM 0x03
39 #define LP5521_REG_R_CNTRL 0x05
40 #define LP5521_REG_G_CNTRL 0x06
41 #define LP5521_REG_B_CNTRL 0x07
42 #define LP5521_REG_MISC 0x08
43 #define LP5521_REG_R_CHANNEL_PC 0x09
44 #define LP5521_REG_G_CHANNEL_PC 0x0a
45 #define LP5521_REG_B_CHANNEL_PC 0x0b
46 #define LP5521_REG_STATUS 0x0c
47 #define LP5521_REG_RESET 0x0d
48 #define LP5521_REG_GPO 0x0e
49 #define LP5521_REG_R_PROG_MEM 0x10
50 #define LP5521_REG_G_PROG_MEM 0x30
51 #define LP5521_REG_B_PROG_MEM 0x50
53 #define LP5521_CURRENT_1m5 0x0f
54 #define LP5521_CURRENT_3m1 0x1f
55 #define LP5521_CURRENT_4m7 0x2f
56 #define LP5521_CURRENT_6m3 0x3f
57 #define LP5521_CURRENT_7m9 0x4f
58 #define LP5521_CURRENT_9m5 0x5f
59 #define LP5521_CURRENT_11m1 0x6f
60 #define LP5521_CURRENT_12m7 0x7f
61 #define LP5521_CURRENT_14m3 0x8f
62 #define LP5521_CURRENT_15m9 0x9f
63 #define LP5521_CURRENT_17m5 0xaf
64 #define LP5521_CURRENT_19m1 0xbf
65 #define LP5521_CURRENT_20m7 0xcf
66 #define LP5521_CURRENT_22m3 0xdf
67 #define LP5521_CURRENT_23m9 0xef
68 #define LP5521_CURRENT_25m5 0xff
70 #define LP5521_PROGRAM_LENGTH 32 /* in bytes */
75 struct i2c_client *client;
77 struct work_struct red_work;
78 struct work_struct green_work;
79 struct work_struct blue_work;
81 struct led_classdev ledr;
82 struct led_classdev ledg;
83 struct led_classdev ledb;
85 enum lp5521_mode mode;
92 static int lp5521_set_mode(struct lp5521_chip *chip, enum lp5521_mode mode);
94 static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
96 return i2c_smbus_write_byte_data(client, reg, value);
99 static inline int lp5521_read(struct i2c_client *client, u8 reg)
101 return i2c_smbus_read_byte_data(client, reg);
104 static int lp5521_configure(struct i2c_client *client)
108 /* Enable chip and set light to logarithmic mode*/
109 ret |= lp5521_write(client, LP5521_REG_ENABLE, 0xc0);
111 /* setting all color pwms to direct control mode */
112 ret |= lp5521_write(client, LP5521_REG_OP_MODE, 0x3f);
114 /* setting current to 4.7 mA for all channels */
115 ret |= lp5521_write(client, LP5521_REG_R_CNTRL, LP5521_CURRENT_4m7);
116 ret |= lp5521_write(client, LP5521_REG_G_CNTRL, LP5521_CURRENT_4m7);
117 ret |= lp5521_write(client, LP5521_REG_B_CNTRL, LP5521_CURRENT_4m7);
119 /* Enable auto-powersave, set charge pump to auto, red to battery */
120 ret |= lp5521_write(client, LP5521_REG_MISC, 0x3c);
122 /* initialize all channels pwm to zero */
123 ret |= lp5521_write(client, LP5521_REG_R_PWM, 0);
124 ret |= lp5521_write(client, LP5521_REG_G_PWM, 0);
125 ret |= lp5521_write(client, LP5521_REG_B_PWM, 0);
127 /* Not much can be done about errors at this point */
131 static int lp5521_load_program(struct lp5521_chip *chip, u8 *pattern)
133 struct i2c_client *client = chip->client;
136 /* Enter load program mode for all led channels */
137 ret |= lp5521_write(client, LP5521_REG_OP_MODE, 0x15); /* 0001 0101 */
142 ret |= i2c_smbus_write_i2c_block_data(client,
143 LP5521_REG_R_PROG_MEM,
144 LP5521_PROGRAM_LENGTH,
147 ret |= i2c_smbus_write_i2c_block_data(client,
148 LP5521_REG_G_PROG_MEM,
149 LP5521_PROGRAM_LENGTH,
152 ret |= i2c_smbus_write_i2c_block_data(client,
153 LP5521_REG_B_PROG_MEM,
154 LP5521_PROGRAM_LENGTH,
160 static int lp5521_run_program(struct lp5521_chip *chip)
162 struct i2c_client *client = chip->client;
167 reg = lp5521_read(client, LP5521_REG_ENABLE);
173 /* set all active channels exec state to countinous run*/
174 exec_state |= (chip->red << 5);
175 exec_state |= (chip->green << 3);
176 exec_state |= (chip->blue << 1);
180 if (lp5521_write(client, LP5521_REG_ENABLE, reg))
181 dev_dbg(&client->dev, "failed writing to register %02x\n",
184 /* set op-mode to run for active channels, disabled for others */
185 if (lp5521_write(client, LP5521_REG_OP_MODE, exec_state))
186 dev_dbg(&client->dev, "failed writing to register %02x\n",
192 /*--------------------------------------------------------------*/
193 /* Sysfs interface */
194 /*--------------------------------------------------------------*/
196 static ssize_t show_active_channels(struct device *dev,
197 struct device_attribute *attr,
200 struct lp5521_chip *chip = dev_get_drvdata(dev);
205 pos += sprintf(channels + pos, "r");
207 pos += sprintf(channels + pos, "g");
209 pos += sprintf(channels + pos, "b");
211 channels[pos] = '\0';
213 return sprintf(buf, "%s\n", channels);
216 static ssize_t store_active_channels(struct device *dev,
217 struct device_attribute *attr,
218 const char *buf, size_t len)
220 struct lp5521_chip *chip = dev_get_drvdata(dev);
226 if (strchr(buf, 'r') != NULL)
228 if (strchr(buf, 'b') != NULL)
230 if (strchr(buf, 'g') != NULL)
236 static ssize_t show_color(struct device *dev,
237 struct device_attribute *attr,
240 struct i2c_client *client = to_i2c_client(dev);
243 r = lp5521_read(client, LP5521_REG_R_PWM);
244 g = lp5521_read(client, LP5521_REG_G_PWM);
245 b = lp5521_read(client, LP5521_REG_B_PWM);
247 if (r < 0 || g < 0 || b < 0)
250 return sprintf(buf, "%.2x:%.2x:%.2x\n", r, g, b);
253 static ssize_t store_color(struct device *dev,
254 struct device_attribute *attr,
255 const char *buf, size_t len)
257 struct i2c_client *client = to_i2c_client(dev);
258 struct lp5521_chip *chip = i2c_get_clientdata(client);
263 ret = sscanf(buf, "%2x:%2x:%2x", &r, &g, &b);
267 mutex_lock(&chip->lock);
269 ret = lp5521_write(client, LP5521_REG_R_PWM, (u8)r);
270 ret = lp5521_write(client, LP5521_REG_G_PWM, (u8)g);
271 ret = lp5521_write(client, LP5521_REG_B_PWM, (u8)b);
273 mutex_unlock(&chip->lock);
278 static ssize_t store_load(struct device *dev,
279 struct device_attribute *attr,
280 const char *buf, size_t len)
282 struct lp5521_chip *chip = dev_get_drvdata(dev);
283 int ret, nrchars, offset = 0, i = 0;
286 u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
288 while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
290 /* separate sscanfs because length is working only for %s */
291 ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
292 ret = sscanf(c, "%2x", &cmd);
295 pattern[i] = (u8)cmd;
301 /* pattern commands are always two bytes long */
305 mutex_lock(&chip->lock);
307 ret = lp5521_load_program(chip, pattern);
308 mutex_unlock(&chip->lock);
311 dev_err(dev, "lp5521 failed loading pattern\n");
317 dev_err(dev, "lp5521 wrong pattern format\n");
321 static ssize_t show_mode(struct device *dev,
322 struct device_attribute *attr,
325 struct lp5521_chip *chip = dev_get_drvdata(dev);
328 mutex_lock(&chip->lock);
329 switch (chip->mode) {
330 case LP5521_MODE_RUN:
333 case LP5521_MODE_LOAD:
336 case LP5521_MODE_DIRECT_CONTROL:
342 mutex_unlock(&chip->lock);
344 return sprintf(buf, "%s\n", mode);
347 static ssize_t store_mode(struct device *dev,
348 struct device_attribute *attr,
349 const char *buf, size_t len)
351 struct lp5521_chip *chip = dev_get_drvdata(dev);
353 mutex_lock(&chip->lock);
355 if (sysfs_streq(buf, "run"))
356 lp5521_set_mode(chip, LP5521_MODE_RUN);
357 else if (sysfs_streq(buf, "load"))
358 lp5521_set_mode(chip, LP5521_MODE_LOAD);
359 else if (sysfs_streq(buf, "direct"))
360 lp5521_set_mode(chip, LP5521_MODE_DIRECT_CONTROL);
364 mutex_unlock(&chip->lock);
369 static ssize_t show_current(struct device *dev,
370 struct device_attribute *attr,
373 struct i2c_client *client = to_i2c_client(dev);
376 r = lp5521_read(client, LP5521_REG_R_CNTRL);
377 g = lp5521_read(client, LP5521_REG_G_CNTRL);
378 b = lp5521_read(client, LP5521_REG_B_CNTRL);
380 if (r < 0 || g < 0 || b < 0)
387 return sprintf(buf, "%x %x %x\n", r, g, b);
390 static ssize_t store_current(struct device *dev,
391 struct device_attribute *attr,
392 const char *buf, size_t len)
394 struct lp5521_chip *chip = dev_get_drvdata(dev);
395 struct i2c_client *client = chip->client;
399 ret = sscanf(buf, "%1x", &curr);
403 /* current level is determined by the 4 upper bits, rest is ones */
404 curr = (curr << 4) | 0x0f;
406 mutex_lock(&chip->lock);
408 ret |= lp5521_write(client, LP5521_REG_R_CNTRL, (u8)curr);
409 ret |= lp5521_write(client, LP5521_REG_G_CNTRL, (u8)curr);
410 ret |= lp5521_write(client, LP5521_REG_B_CNTRL, (u8)curr);
412 mutex_unlock(&chip->lock);
417 static DEVICE_ATTR(color, S_IRUGO | S_IWUGO, show_color, store_color);
418 static DEVICE_ATTR(load, S_IWUGO, NULL, store_load);
419 static DEVICE_ATTR(mode, S_IRUGO | S_IWUGO, show_mode, store_mode);
420 static DEVICE_ATTR(active_channels, S_IRUGO | S_IWUGO,
421 show_active_channels, store_active_channels);
422 static DEVICE_ATTR(led_current, S_IRUGO | S_IWUGO, show_current, store_current);
424 static int lp5521_register_sysfs(struct i2c_client *client)
426 struct device *dev = &client->dev;
429 ret = device_create_file(dev, &dev_attr_color);
432 ret = device_create_file(dev, &dev_attr_load);
435 ret = device_create_file(dev, &dev_attr_active_channels);
438 ret = device_create_file(dev, &dev_attr_mode);
441 ret = device_create_file(dev, &dev_attr_led_current);
448 device_remove_file(dev, &dev_attr_mode);
450 device_remove_file(dev, &dev_attr_active_channels);
452 device_remove_file(dev, &dev_attr_load);
454 device_remove_file(dev, &dev_attr_color);
459 static void lp5521_unregister_sysfs(struct i2c_client *client)
461 struct device *dev = &client->dev;
463 device_remove_file(dev, &dev_attr_led_current);
464 device_remove_file(dev, &dev_attr_mode);
465 device_remove_file(dev, &dev_attr_active_channels);
466 device_remove_file(dev, &dev_attr_color);
467 device_remove_file(dev, &dev_attr_load);
470 /*--------------------------------------------------------------*/
471 /* Set chip operating mode */
472 /*--------------------------------------------------------------*/
474 static int lp5521_set_mode(struct lp5521_chip *chip, enum lp5521_mode mode)
476 struct i2c_client *client = chip->client ;
479 /* if in that mode already do nothing, except for run */
480 if (chip->mode == mode && mode != LP5521_MODE_RUN)
484 case LP5521_MODE_RUN:
485 ret = lp5521_run_program(chip);
487 case LP5521_MODE_LOAD:
488 ret |= lp5521_write(client, LP5521_REG_OP_MODE, 0x15);
490 case LP5521_MODE_DIRECT_CONTROL:
491 ret |= lp5521_write(client, LP5521_REG_OP_MODE, 0x3F);
494 dev_dbg(&client->dev, "unsupported mode %d\n", mode);
502 static void lp5521_red_work(struct work_struct *work)
504 struct lp5521_chip *chip = container_of(work, struct lp5521_chip, red_work);
507 ret = lp5521_configure(chip->client);
509 dev_dbg(&chip->client->dev, "could not configure lp5521, %d\n",
514 ret = lp5521_write(chip->client, LP5521_REG_R_PWM, chip->red);
516 dev_dbg(&chip->client->dev, "could not set brightness, %d\n",
520 static void lp5521_red_set(struct led_classdev *led,
521 enum led_brightness value)
523 struct lp5521_chip *chip = container_of(led, struct lp5521_chip, ledr);
526 schedule_work(&chip->red_work);
529 static void lp5521_green_work(struct work_struct *work)
531 struct lp5521_chip *chip = container_of(work, struct lp5521_chip, green_work);
534 ret = lp5521_configure(chip->client);
536 dev_dbg(&chip->client->dev, "could not configure lp5521, %d\n",
541 ret = lp5521_write(chip->client, LP5521_REG_G_PWM, chip->green);
543 dev_dbg(&chip->client->dev, "could not set brightness, %d\n",
547 static void lp5521_green_set(struct led_classdev *led,
548 enum led_brightness value)
550 struct lp5521_chip *chip = container_of(led, struct lp5521_chip, ledg);
553 schedule_work(&chip->green_work);
556 static void lp5521_blue_work(struct work_struct *work)
558 struct lp5521_chip *chip = container_of(work, struct lp5521_chip, blue_work);
561 ret = lp5521_configure(chip->client);
563 dev_dbg(&chip->client->dev, "could not configure lp5521, %d\n",
568 ret = lp5521_write(chip->client, LP5521_REG_B_PWM, chip->blue);
570 dev_dbg(&chip->client->dev, "could not set brightness, %d\n",
574 static void lp5521_blue_set(struct led_classdev *led,
575 enum led_brightness value)
577 struct lp5521_chip *chip = container_of(led, struct lp5521_chip, ledb);
580 schedule_work(&chip->blue_work);
583 /*--------------------------------------------------------------*/
584 /* Probe, Attach, Remove */
585 /*--------------------------------------------------------------*/
587 static int __init lp5521_probe(struct i2c_client *client,
588 const struct i2c_device_id *id)
590 struct lp5521_platform_data *pdata = client->dev.platform_data;
591 struct lp5521_chip *chip;
596 dev_err(&client->dev, "platform_data is missing\n");
600 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
604 chip->client = client;
605 strncpy(client->name, LP5521_DRIVER_NAME, I2C_NAME_SIZE);
606 i2c_set_clientdata(client, chip);
608 mutex_init(&chip->lock);
610 INIT_WORK(&chip->red_work, lp5521_red_work);
611 INIT_WORK(&chip->green_work, lp5521_green_work);
612 INIT_WORK(&chip->blue_work, lp5521_blue_work);
614 ret = lp5521_configure(client);
616 dev_err(&client->dev, "lp5521 error configuring chip \n");
620 /* Set default values */
621 chip->mode = pdata->mode;
622 chip->red = pdata->red_present;
623 chip->green = pdata->green_present;
624 chip->blue = pdata->blue_present;
626 chip->ledr.brightness_set = lp5521_red_set;
627 chip->ledr.default_trigger = NULL;
628 snprintf(name, sizeof(name), "%s::red", pdata->label);
629 chip->ledr.name = name;
630 ret = led_classdev_register(&client->dev, &chip->ledr);
632 dev_dbg(&client->dev, "failed to register led %s, %d\n",
633 chip->ledb.name, ret);
637 chip->ledg.brightness_set = lp5521_green_set;
638 chip->ledg.default_trigger = NULL;
639 snprintf(name, sizeof(name), "%s::green", pdata->label);
640 chip->ledg.name = name;
641 ret = led_classdev_register(&client->dev, &chip->ledg);
643 dev_dbg(&client->dev, "failed to register led %s, %d\n",
644 chip->ledb.name, ret);
648 chip->ledb.brightness_set = lp5521_blue_set;
649 chip->ledb.default_trigger = NULL;
650 snprintf(name, sizeof(name), "%s::blue", pdata->label);
651 chip->ledb.name = name;
652 ret = led_classdev_register(&client->dev, &chip->ledb);
654 dev_dbg(&client->dev, "failed to register led %s, %d\n", chip->ledb.name, ret);
658 ret = lp5521_register_sysfs(client);
660 dev_err(&client->dev, "lp5521 registering sysfs failed \n");
667 led_classdev_unregister(&chip->ledb);
669 led_classdev_unregister(&chip->ledg);
671 led_classdev_unregister(&chip->ledr);
673 i2c_set_clientdata(client, NULL);
679 static int __exit lp5521_remove(struct i2c_client *client)
681 struct lp5521_chip *chip = i2c_get_clientdata(client);
683 lp5521_unregister_sysfs(client);
684 i2c_set_clientdata(client, NULL);
686 led_classdev_unregister(&chip->ledb);
687 led_classdev_unregister(&chip->ledg);
688 led_classdev_unregister(&chip->ledr);
695 static const struct i2c_device_id lp5521_id[] = {
696 { LP5521_DRIVER_NAME, 0},
699 MODULE_DEVICE_TABLE(i2c, lp5521_id);
701 static struct i2c_driver lp5521_driver = {
703 .name = LP5521_DRIVER_NAME,
705 .probe = lp5521_probe,
706 .remove = __exit_p(lp5521_remove),
707 .id_table = lp5521_id,
710 static int __init lp5521_init(void)
712 return i2c_add_driver(&lp5521_driver);
714 module_init(lp5521_init);
716 static void __exit lp5521_exit(void)
718 i2c_del_driver(&lp5521_driver);
720 module_exit(lp5521_exit);
722 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>");
723 MODULE_DESCRIPTION("lp5521 LED driver");
724 MODULE_LICENSE("GPL");