2 * leds-bd2802.c - RGB LED Driver
4 * Copyright (C) 2009 Samsung Electronics
5 * Kim Kyuwon <q1.kim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Datasheet: http://www.rohm.com/products/databook/driver/pdf/bd2802gu-e.pdf
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/gpio.h>
18 #include <linux/delay.h>
19 #include <linux/leds.h>
20 #include <linux/leds-bd2802.h>
23 #define LED_CTL(rgb2en, rgb1en) ((rgb2en) << 4 | ((rgb1en) << 0))
25 #define BD2802_LED_OFFSET 0xa
26 #define BD2802_COLOR_OFFSET 0x3
28 #define BD2802_REG_CLKSETUP 0x00
29 #define BD2802_REG_CONTROL 0x01
30 #define BD2802_REG_HOURSETUP 0x02
31 #define BD2802_REG_CURRENT1SETUP 0x03
32 #define BD2802_REG_CURRENT2SETUP 0x04
33 #define BD2802_REG_WAVEPATTERN 0x05
35 #define BD2802_CURRENT_032 0x10 /* 3.2mA */
36 #define BD2802_CURRENT_000 0x00 /* 0.0mA */
38 #define BD2802_PATTERN_FULL 0x07
39 #define BD2802_PATTERN_HALF 0x03
71 struct bd2802_led_platform_data *pdata;
72 struct i2c_client *client;
73 struct rw_semaphore rwsem;
74 struct work_struct work;
76 struct led_state led[2];
79 * Making led_classdev as array is not recommended, because array
80 * members prevent using 'container_of' macro. So repetitive works
83 struct led_classdev cdev_led1r;
84 struct led_classdev cdev_led1g;
85 struct led_classdev cdev_led1b;
86 struct led_classdev cdev_led2r;
87 struct led_classdev cdev_led2g;
88 struct led_classdev cdev_led2b;
91 * Advanced Configuration Function(ADF) mode:
92 * In ADF mode, user can set registers of BD2802GU directly,
93 * therefore BD2802GU doesn't enter reset state.
98 enum led_colors color;
103 /*--------------------------------------------------------------*/
104 /* BD2802GU helper functions */
105 /*--------------------------------------------------------------*/
107 static inline int bd2802_is_rgb_off(struct bd2802_led *led, enum led_ids id,
108 enum led_colors color)
112 return !led->led[id].r;
114 return !led->led[id].g;
116 return !led->led[id].b;
118 dev_err(&led->client->dev, "%s: Invalid color\n", __func__);
123 static inline int bd2802_is_led_off(struct bd2802_led *led, enum led_ids id)
125 if (led->led[id].r || led->led[id].g || led->led[id].b)
131 static inline int bd2802_is_all_off(struct bd2802_led *led)
135 for (i = 0; i < LED_NUM; i++)
136 if (!bd2802_is_led_off(led, i))
142 static inline u8 bd2802_get_base_offset(enum led_ids id, enum led_colors color)
144 return id * BD2802_LED_OFFSET + color * BD2802_COLOR_OFFSET;
147 static inline u8 bd2802_get_reg_addr(enum led_ids id, enum led_colors color,
150 return reg_offset + bd2802_get_base_offset(id, color);
154 /*--------------------------------------------------------------*/
155 /* BD2802GU core functions */
156 /*--------------------------------------------------------------*/
158 static int bd2802_write_byte(struct i2c_client *client, u8 reg, u8 val)
160 int ret = i2c_smbus_write_byte_data(client, reg, val);
164 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
165 __func__, reg, val, ret);
170 static void bd2802_update_state(struct bd2802_led *led, enum led_ids id,
171 enum led_colors color, enum led_bits led_bit)
176 for (i = 0; i < LED_NUM; i++) {
180 led->led[i].r = led_bit;
183 led->led[i].g = led_bit;
186 led->led[i].b = led_bit;
189 dev_err(&led->client->dev,
190 "%s: Invalid color\n", __func__);
196 if (led_bit == BD2802_BLINK || led_bit == BD2802_ON)
199 if (!bd2802_is_led_off(led, id))
202 if (bd2802_is_all_off(led) && !led->adf_on) {
203 gpio_set_value(led->pdata->reset_gpio, 0);
208 * In this case, other led is turned on, and current led is turned
209 * off. So set RGB LED Control register to stop the current RGB LED
211 value = (id == LED1) ? LED_CTL(1, 0) : LED_CTL(0, 1);
212 bd2802_write_byte(led->client, BD2802_REG_CONTROL, value);
215 static void bd2802_configure(struct bd2802_led *led)
217 struct bd2802_led_platform_data *pdata = led->pdata;
220 reg = bd2802_get_reg_addr(LED1, RED, BD2802_REG_HOURSETUP);
221 bd2802_write_byte(led->client, reg, pdata->rgb_time);
223 reg = bd2802_get_reg_addr(LED2, RED, BD2802_REG_HOURSETUP);
224 bd2802_write_byte(led->client, reg, pdata->rgb_time);
227 static void bd2802_reset_cancel(struct bd2802_led *led)
229 gpio_set_value(led->pdata->reset_gpio, 1);
231 bd2802_configure(led);
234 static void bd2802_enable(struct bd2802_led *led, enum led_ids id)
236 enum led_ids other_led = (id == LED1) ? LED2 : LED1;
237 u8 value, other_led_on;
239 other_led_on = !bd2802_is_led_off(led, other_led);
241 value = LED_CTL(other_led_on, 1);
243 value = LED_CTL(1 , other_led_on);
245 bd2802_write_byte(led->client, BD2802_REG_CONTROL, value);
248 static void bd2802_set_on(struct bd2802_led *led, enum led_ids id,
249 enum led_colors color)
253 if (bd2802_is_all_off(led) && !led->adf_on)
254 bd2802_reset_cancel(led);
256 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
257 bd2802_write_byte(led->client, reg, BD2802_CURRENT_032);
258 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
259 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
260 reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN);
261 bd2802_write_byte(led->client, reg, BD2802_PATTERN_FULL);
263 bd2802_enable(led, id);
264 bd2802_update_state(led, id, color, BD2802_ON);
267 static void bd2802_set_blink(struct bd2802_led *led, enum led_ids id,
268 enum led_colors color)
272 if (bd2802_is_all_off(led) && !led->adf_on)
273 bd2802_reset_cancel(led);
275 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
276 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
277 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
278 bd2802_write_byte(led->client, reg, BD2802_CURRENT_032);
279 reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN);
280 bd2802_write_byte(led->client, reg, BD2802_PATTERN_HALF);
282 bd2802_enable(led, id);
283 bd2802_update_state(led, id, color, BD2802_BLINK);
286 static void bd2802_turn_on(struct bd2802_led *led, enum led_ids id,
287 enum led_colors color, enum led_bits led_bit)
289 if (led_bit == BD2802_OFF) {
290 dev_err(&led->client->dev,
291 "Only 'blink' and 'on' are allowed\n");
295 if (led_bit == BD2802_BLINK)
296 bd2802_set_blink(led, id, color);
298 bd2802_set_on(led, id, color);
301 static void bd2802_turn_off(struct bd2802_led *led, enum led_ids id,
302 enum led_colors color)
306 if (bd2802_is_rgb_off(led, id, color))
309 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
310 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
311 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
312 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
314 bd2802_update_state(led, id, color, BD2802_OFF);
317 static void bd2802_restore_state(struct bd2802_led *led)
321 for (i = 0; i < LED_NUM; i++) {
323 bd2802_turn_on(led, i, RED, led->led[i].r);
325 bd2802_turn_on(led, i, GREEN, led->led[i].g);
327 bd2802_turn_on(led, i, BLUE, led->led[i].b);
331 #define BD2802_SET_REGISTER(reg_addr, reg_name) \
332 static ssize_t bd2802_store_reg##reg_addr(struct device *dev, \
333 struct device_attribute *attr, const char *buf, size_t count) \
335 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
340 ret = strict_strtoul(buf, 16, &val); \
343 down_write(&led->rwsem); \
344 bd2802_write_byte(led->client, reg_addr, (u8) val); \
345 up_write(&led->rwsem); \
348 static struct device_attribute bd2802_reg##reg_addr##_attr = { \
349 .attr = {.name = reg_name, .mode = 0644, .owner = THIS_MODULE}, \
350 .store = bd2802_store_reg##reg_addr, \
353 BD2802_SET_REGISTER(0x00, "0x00");
354 BD2802_SET_REGISTER(0x01, "0x01");
355 BD2802_SET_REGISTER(0x02, "0x02");
356 BD2802_SET_REGISTER(0x03, "0x03");
357 BD2802_SET_REGISTER(0x04, "0x04");
358 BD2802_SET_REGISTER(0x05, "0x05");
359 BD2802_SET_REGISTER(0x06, "0x06");
360 BD2802_SET_REGISTER(0x07, "0x07");
361 BD2802_SET_REGISTER(0x08, "0x08");
362 BD2802_SET_REGISTER(0x09, "0x09");
363 BD2802_SET_REGISTER(0x0a, "0x0a");
364 BD2802_SET_REGISTER(0x0b, "0x0b");
365 BD2802_SET_REGISTER(0x0c, "0x0c");
366 BD2802_SET_REGISTER(0x0d, "0x0d");
367 BD2802_SET_REGISTER(0x0e, "0x0e");
368 BD2802_SET_REGISTER(0x0f, "0x0f");
369 BD2802_SET_REGISTER(0x10, "0x10");
370 BD2802_SET_REGISTER(0x11, "0x11");
371 BD2802_SET_REGISTER(0x12, "0x12");
372 BD2802_SET_REGISTER(0x13, "0x13");
373 BD2802_SET_REGISTER(0x14, "0x14");
374 BD2802_SET_REGISTER(0x15, "0x15");
376 static struct device_attribute *bd2802_addr_attributes[] = {
377 &bd2802_reg0x00_attr,
378 &bd2802_reg0x01_attr,
379 &bd2802_reg0x02_attr,
380 &bd2802_reg0x03_attr,
381 &bd2802_reg0x04_attr,
382 &bd2802_reg0x05_attr,
383 &bd2802_reg0x06_attr,
384 &bd2802_reg0x07_attr,
385 &bd2802_reg0x08_attr,
386 &bd2802_reg0x09_attr,
387 &bd2802_reg0x0a_attr,
388 &bd2802_reg0x0b_attr,
389 &bd2802_reg0x0c_attr,
390 &bd2802_reg0x0d_attr,
391 &bd2802_reg0x0e_attr,
392 &bd2802_reg0x0f_attr,
393 &bd2802_reg0x10_attr,
394 &bd2802_reg0x11_attr,
395 &bd2802_reg0x12_attr,
396 &bd2802_reg0x13_attr,
397 &bd2802_reg0x14_attr,
398 &bd2802_reg0x15_attr,
401 static void bd2802_enable_adv_conf(struct bd2802_led *led)
405 for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++) {
406 ret = device_create_file(&led->client->dev,
407 bd2802_addr_attributes[i]);
409 dev_err(&led->client->dev, "failed to sysfs file %s\n",
410 bd2802_addr_attributes[i]->attr.name);
411 goto failed_remove_files;
415 if (bd2802_is_all_off(led))
416 bd2802_reset_cancel(led);
423 for (i--; i >= 0; i--)
424 device_remove_file(&led->client->dev,
425 bd2802_addr_attributes[i]);
428 static void bd2802_disable_adv_conf(struct bd2802_led *led)
432 for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++)
433 device_remove_file(&led->client->dev,
434 bd2802_addr_attributes[i]);
436 if (bd2802_is_all_off(led))
437 gpio_set_value(led->pdata->reset_gpio, 0);
442 static ssize_t bd2802_show_adv_conf(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));
448 down_read(&led->rwsem);
450 ret = sprintf(buf, "on\n");
452 ret = sprintf(buf, "off\n");
453 up_read(&led->rwsem);
458 static ssize_t bd2802_store_adv_conf(struct device *dev,
459 struct device_attribute *attr, const char *buf, size_t count)
461 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));
466 down_write(&led->rwsem);
467 if (!led->adf_on && !strncmp(buf, "on", 2))
468 bd2802_enable_adv_conf(led);
469 else if (led->adf_on && !strncmp(buf, "off", 3))
470 bd2802_disable_adv_conf(led);
471 up_write(&led->rwsem);
476 static struct device_attribute bd2802_adv_conf_attr = {
478 .name = "advanced_configuration",
482 .show = bd2802_show_adv_conf,
483 .store = bd2802_store_adv_conf,
486 static void bd2802_led_work(struct work_struct *work)
488 struct bd2802_led *led = container_of(work, struct bd2802_led, work);
491 bd2802_turn_on(led, led->led_id, led->color, led->state);
493 bd2802_turn_off(led, led->led_id, led->color);
496 #define BD2802_CONTROL_RGBS(name, id, clr) \
497 static void bd2802_set_##name##_brightness(struct led_classdev *led_cdev,\
498 enum led_brightness value) \
500 struct bd2802_led *led = \
501 container_of(led_cdev, struct bd2802_led, cdev_##name); \
504 if (value == LED_OFF) \
505 led->state = BD2802_OFF; \
507 led->state = BD2802_ON; \
508 schedule_work(&led->work); \
510 static int bd2802_set_##name##_blink(struct led_classdev *led_cdev, \
511 unsigned long *delay_on, unsigned long *delay_off) \
513 struct bd2802_led *led = \
514 container_of(led_cdev, struct bd2802_led, cdev_##name); \
515 if (*delay_on == 0 || *delay_off == 0) \
519 led->state = BD2802_BLINK; \
520 schedule_work(&led->work); \
524 BD2802_CONTROL_RGBS(led1r, LED1, RED);
525 BD2802_CONTROL_RGBS(led1g, LED1, GREEN);
526 BD2802_CONTROL_RGBS(led1b, LED1, BLUE);
527 BD2802_CONTROL_RGBS(led2r, LED2, RED);
528 BD2802_CONTROL_RGBS(led2g, LED2, GREEN);
529 BD2802_CONTROL_RGBS(led2b, LED2, BLUE);
531 static int bd2802_register_led_classdev(struct bd2802_led *led)
535 INIT_WORK(&led->work, bd2802_led_work);
537 led->cdev_led1r.name = "led1_R";
538 led->cdev_led1r.brightness = LED_OFF;
539 led->cdev_led1r.brightness_set = bd2802_set_led1r_brightness;
540 led->cdev_led1r.blink_set = bd2802_set_led1r_blink;
541 led->cdev_led1r.flags |= LED_CORE_SUSPENDRESUME;
543 ret = led_classdev_register(&led->client->dev, &led->cdev_led1r);
545 dev_err(&led->client->dev, "couldn't register LED %s\n",
546 led->cdev_led1r.name);
547 goto failed_unregister_led1_R;
550 led->cdev_led1g.name = "led1_G";
551 led->cdev_led1g.brightness = LED_OFF;
552 led->cdev_led1g.brightness_set = bd2802_set_led1g_brightness;
553 led->cdev_led1g.blink_set = bd2802_set_led1g_blink;
554 led->cdev_led1g.flags |= LED_CORE_SUSPENDRESUME;
556 ret = led_classdev_register(&led->client->dev, &led->cdev_led1g);
558 dev_err(&led->client->dev, "couldn't register LED %s\n",
559 led->cdev_led1g.name);
560 goto failed_unregister_led1_G;
563 led->cdev_led1b.name = "led1_B";
564 led->cdev_led1b.brightness = LED_OFF;
565 led->cdev_led1b.brightness_set = bd2802_set_led1b_brightness;
566 led->cdev_led1b.blink_set = bd2802_set_led1b_blink;
567 led->cdev_led1b.flags |= LED_CORE_SUSPENDRESUME;
569 ret = led_classdev_register(&led->client->dev, &led->cdev_led1b);
571 dev_err(&led->client->dev, "couldn't register LED %s\n",
572 led->cdev_led1b.name);
573 goto failed_unregister_led1_B;
576 led->cdev_led2r.name = "led2_R";
577 led->cdev_led2r.brightness = LED_OFF;
578 led->cdev_led2r.brightness_set = bd2802_set_led2r_brightness;
579 led->cdev_led2r.blink_set = bd2802_set_led2r_blink;
580 led->cdev_led2r.flags |= LED_CORE_SUSPENDRESUME;
582 ret = led_classdev_register(&led->client->dev, &led->cdev_led2r);
584 dev_err(&led->client->dev, "couldn't register LED %s\n",
585 led->cdev_led2r.name);
586 goto failed_unregister_led2_R;
589 led->cdev_led2g.name = "led2_G";
590 led->cdev_led2g.brightness = LED_OFF;
591 led->cdev_led2g.brightness_set = bd2802_set_led2g_brightness;
592 led->cdev_led2g.blink_set = bd2802_set_led2g_blink;
593 led->cdev_led2g.flags |= LED_CORE_SUSPENDRESUME;
595 ret = led_classdev_register(&led->client->dev, &led->cdev_led2g);
597 dev_err(&led->client->dev, "couldn't register LED %s\n",
598 led->cdev_led2g.name);
599 goto failed_unregister_led2_G;
602 led->cdev_led2b.name = "led2_B";
603 led->cdev_led2b.brightness = LED_OFF;
604 led->cdev_led2b.brightness_set = bd2802_set_led2b_brightness;
605 led->cdev_led2b.blink_set = bd2802_set_led2b_blink;
606 led->cdev_led2b.flags |= LED_CORE_SUSPENDRESUME;
608 ret = led_classdev_register(&led->client->dev, &led->cdev_led2b);
610 dev_err(&led->client->dev, "couldn't register LED %s\n",
611 led->cdev_led2b.name);
612 goto failed_unregister_led2_B;
617 failed_unregister_led2_B:
618 led_classdev_unregister(&led->cdev_led2g);
619 failed_unregister_led2_G:
620 led_classdev_unregister(&led->cdev_led2r);
621 failed_unregister_led2_R:
622 led_classdev_unregister(&led->cdev_led1b);
623 failed_unregister_led1_B:
624 led_classdev_unregister(&led->cdev_led1g);
625 failed_unregister_led1_G:
626 led_classdev_unregister(&led->cdev_led1r);
627 failed_unregister_led1_R:
632 static void bd2802_unregister_led_classdev(struct bd2802_led *led)
634 cancel_work_sync(&led->work);
635 led_classdev_unregister(&led->cdev_led1r);
638 static int __devinit bd2802_probe(struct i2c_client *client,
639 const struct i2c_device_id *id)
641 struct bd2802_led *led;
642 struct bd2802_led_platform_data *pdata;
645 led = kzalloc(sizeof(struct bd2802_led), GFP_KERNEL);
647 dev_err(&client->dev, "failed to allocate driver data\n");
651 led->client = client;
652 pdata = led->pdata = client->dev.platform_data;
653 i2c_set_clientdata(client, led);
655 /* Configure RESET GPIO (L: RESET, H: RESET cancel) */
656 gpio_request(pdata->reset_gpio, "RGB_RESETB");
657 gpio_direction_output(pdata->reset_gpio, 1);
659 /* Tacss = min 0.1ms */
662 /* Detect BD2802GU */
663 ret = bd2802_write_byte(client, BD2802_REG_CLKSETUP, 0x00);
665 dev_err(&client->dev, "failed to detect device\n");
668 dev_info(&client->dev, "return 0x%02x\n", ret);
670 /* To save the power, reset BD2802 after detecting */
671 gpio_set_value(led->pdata->reset_gpio, 0);
673 init_rwsem(&led->rwsem);
675 ret = device_create_file(&client->dev, &bd2802_adv_conf_attr);
677 dev_err(&client->dev, "failed to create sysfs file %s\n",
678 bd2802_adv_conf_attr.attr.name);
682 ret = bd2802_register_led_classdev(led);
684 goto failed_unregister_dev_file;
688 failed_unregister_dev_file:
689 device_remove_file(&client->dev, &bd2802_adv_conf_attr);
691 i2c_set_clientdata(client, NULL);
697 static int __exit bd2802_remove(struct i2c_client *client)
699 struct bd2802_led *led = i2c_get_clientdata(client);
701 bd2802_unregister_led_classdev(led);
702 gpio_set_value(led->pdata->reset_gpio, 0);
704 bd2802_disable_adv_conf(led);
705 device_remove_file(&client->dev, &bd2802_adv_conf_attr);
706 i2c_set_clientdata(client, NULL);
712 static int bd2802_suspend(struct i2c_client *client, pm_message_t mesg)
714 struct bd2802_led *led = i2c_get_clientdata(client);
716 gpio_set_value(led->pdata->reset_gpio, 0);
721 static int bd2802_resume(struct i2c_client *client)
723 struct bd2802_led *led = i2c_get_clientdata(client);
725 if (!bd2802_is_all_off(led) || led->adf_on) {
726 gpio_set_value(led->pdata->reset_gpio, 1);
728 bd2802_restore_state(led);
734 static const struct i2c_device_id bd2802_id[] = {
738 MODULE_DEVICE_TABLE(i2c, bd2802_id);
740 static struct i2c_driver bd2802_i2c_driver = {
744 .probe = bd2802_probe,
745 .remove = __exit_p(bd2802_remove),
746 .suspend = bd2802_suspend,
747 .resume = bd2802_resume,
748 .id_table = bd2802_id,
751 static int __init bd2802_init(void)
753 return i2c_add_driver(&bd2802_i2c_driver);
755 module_init(bd2802_init);
757 static void __exit bd2802_exit(void)
759 i2c_del_driver(&bd2802_i2c_driver);
761 module_exit(bd2802_exit);
763 MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
764 MODULE_DESCRIPTION("BD2802 LED driver");
765 MODULE_LICENSE("GPL");