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>
21 #include <linux/slab.h>
24 #define LED_CTL(rgb2en, rgb1en) ((rgb2en) << 4 | ((rgb1en) << 0))
26 #define BD2802_LED_OFFSET 0xa
27 #define BD2802_COLOR_OFFSET 0x3
29 #define BD2802_REG_CLKSETUP 0x00
30 #define BD2802_REG_CONTROL 0x01
31 #define BD2802_REG_HOURSETUP 0x02
32 #define BD2802_REG_CURRENT1SETUP 0x03
33 #define BD2802_REG_CURRENT2SETUP 0x04
34 #define BD2802_REG_WAVEPATTERN 0x05
36 #define BD2802_CURRENT_032 0x10 /* 3.2mA */
37 #define BD2802_CURRENT_000 0x00 /* 0.0mA */
39 #define BD2802_PATTERN_FULL 0x07
40 #define BD2802_PATTERN_HALF 0x03
72 struct bd2802_led_platform_data *pdata;
73 struct i2c_client *client;
74 struct rw_semaphore rwsem;
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;
101 /* General attributes of RGB LEDs */
107 /*--------------------------------------------------------------*/
108 /* BD2802GU helper functions */
109 /*--------------------------------------------------------------*/
111 static inline int bd2802_is_rgb_off(struct bd2802_led *led, enum led_ids id,
112 enum led_colors color)
116 return !led->led[id].r;
118 return !led->led[id].g;
120 return !led->led[id].b;
122 dev_err(&led->client->dev, "%s: Invalid color\n", __func__);
127 static inline int bd2802_is_led_off(struct bd2802_led *led, enum led_ids id)
129 if (led->led[id].r || led->led[id].g || led->led[id].b)
135 static inline int bd2802_is_all_off(struct bd2802_led *led)
139 for (i = 0; i < LED_NUM; i++)
140 if (!bd2802_is_led_off(led, i))
146 static inline u8 bd2802_get_base_offset(enum led_ids id, enum led_colors color)
148 return id * BD2802_LED_OFFSET + color * BD2802_COLOR_OFFSET;
151 static inline u8 bd2802_get_reg_addr(enum led_ids id, enum led_colors color,
154 return reg_offset + bd2802_get_base_offset(id, color);
158 /*--------------------------------------------------------------*/
159 /* BD2802GU core functions */
160 /*--------------------------------------------------------------*/
162 static int bd2802_write_byte(struct i2c_client *client, u8 reg, u8 val)
164 int ret = i2c_smbus_write_byte_data(client, reg, val);
168 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
169 __func__, reg, val, ret);
174 static void bd2802_update_state(struct bd2802_led *led, enum led_ids id,
175 enum led_colors color, enum led_bits led_bit)
180 for (i = 0; i < LED_NUM; i++) {
184 led->led[i].r = led_bit;
187 led->led[i].g = led_bit;
190 led->led[i].b = led_bit;
193 dev_err(&led->client->dev,
194 "%s: Invalid color\n", __func__);
200 if (led_bit == BD2802_BLINK || led_bit == BD2802_ON)
203 if (!bd2802_is_led_off(led, id))
206 if (bd2802_is_all_off(led) && !led->adf_on) {
207 gpio_set_value(led->pdata->reset_gpio, 0);
212 * In this case, other led is turned on, and current led is turned
213 * off. So set RGB LED Control register to stop the current RGB LED
215 value = (id == LED1) ? LED_CTL(1, 0) : LED_CTL(0, 1);
216 bd2802_write_byte(led->client, BD2802_REG_CONTROL, value);
219 static void bd2802_configure(struct bd2802_led *led)
221 struct bd2802_led_platform_data *pdata = led->pdata;
224 reg = bd2802_get_reg_addr(LED1, RED, BD2802_REG_HOURSETUP);
225 bd2802_write_byte(led->client, reg, pdata->rgb_time);
227 reg = bd2802_get_reg_addr(LED2, RED, BD2802_REG_HOURSETUP);
228 bd2802_write_byte(led->client, reg, pdata->rgb_time);
231 static void bd2802_reset_cancel(struct bd2802_led *led)
233 gpio_set_value(led->pdata->reset_gpio, 1);
235 bd2802_configure(led);
238 static void bd2802_enable(struct bd2802_led *led, enum led_ids id)
240 enum led_ids other_led = (id == LED1) ? LED2 : LED1;
241 u8 value, other_led_on;
243 other_led_on = !bd2802_is_led_off(led, other_led);
245 value = LED_CTL(other_led_on, 1);
247 value = LED_CTL(1 , other_led_on);
249 bd2802_write_byte(led->client, BD2802_REG_CONTROL, value);
252 static void bd2802_set_on(struct bd2802_led *led, enum led_ids id,
253 enum led_colors color)
257 if (bd2802_is_all_off(led) && !led->adf_on)
258 bd2802_reset_cancel(led);
260 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
261 bd2802_write_byte(led->client, reg, led->rgb_current);
262 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
263 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
264 reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN);
265 bd2802_write_byte(led->client, reg, BD2802_PATTERN_FULL);
267 bd2802_enable(led, id);
268 bd2802_update_state(led, id, color, BD2802_ON);
271 static void bd2802_set_blink(struct bd2802_led *led, enum led_ids id,
272 enum led_colors color)
276 if (bd2802_is_all_off(led) && !led->adf_on)
277 bd2802_reset_cancel(led);
279 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
280 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
281 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
282 bd2802_write_byte(led->client, reg, led->rgb_current);
283 reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN);
284 bd2802_write_byte(led->client, reg, led->wave_pattern);
286 bd2802_enable(led, id);
287 bd2802_update_state(led, id, color, BD2802_BLINK);
290 static void bd2802_turn_on(struct bd2802_led *led, enum led_ids id,
291 enum led_colors color, enum led_bits led_bit)
293 if (led_bit == BD2802_OFF) {
294 dev_err(&led->client->dev,
295 "Only 'blink' and 'on' are allowed\n");
299 if (led_bit == BD2802_BLINK)
300 bd2802_set_blink(led, id, color);
302 bd2802_set_on(led, id, color);
305 static void bd2802_turn_off(struct bd2802_led *led, enum led_ids id,
306 enum led_colors color)
310 if (bd2802_is_rgb_off(led, id, color))
313 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
314 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
315 reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
316 bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
318 bd2802_update_state(led, id, color, BD2802_OFF);
321 #define BD2802_SET_REGISTER(reg_addr, reg_name) \
322 static ssize_t bd2802_store_reg##reg_addr(struct device *dev, \
323 struct device_attribute *attr, const char *buf, size_t count) \
325 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
330 ret = kstrtoul(buf, 16, &val); \
333 down_write(&led->rwsem); \
334 bd2802_write_byte(led->client, reg_addr, (u8) val); \
335 up_write(&led->rwsem); \
338 static struct device_attribute bd2802_reg##reg_addr##_attr = { \
339 .attr = {.name = reg_name, .mode = 0644}, \
340 .store = bd2802_store_reg##reg_addr, \
343 BD2802_SET_REGISTER(0x00, "0x00");
344 BD2802_SET_REGISTER(0x01, "0x01");
345 BD2802_SET_REGISTER(0x02, "0x02");
346 BD2802_SET_REGISTER(0x03, "0x03");
347 BD2802_SET_REGISTER(0x04, "0x04");
348 BD2802_SET_REGISTER(0x05, "0x05");
349 BD2802_SET_REGISTER(0x06, "0x06");
350 BD2802_SET_REGISTER(0x07, "0x07");
351 BD2802_SET_REGISTER(0x08, "0x08");
352 BD2802_SET_REGISTER(0x09, "0x09");
353 BD2802_SET_REGISTER(0x0a, "0x0a");
354 BD2802_SET_REGISTER(0x0b, "0x0b");
355 BD2802_SET_REGISTER(0x0c, "0x0c");
356 BD2802_SET_REGISTER(0x0d, "0x0d");
357 BD2802_SET_REGISTER(0x0e, "0x0e");
358 BD2802_SET_REGISTER(0x0f, "0x0f");
359 BD2802_SET_REGISTER(0x10, "0x10");
360 BD2802_SET_REGISTER(0x11, "0x11");
361 BD2802_SET_REGISTER(0x12, "0x12");
362 BD2802_SET_REGISTER(0x13, "0x13");
363 BD2802_SET_REGISTER(0x14, "0x14");
364 BD2802_SET_REGISTER(0x15, "0x15");
366 static struct device_attribute *bd2802_addr_attributes[] = {
367 &bd2802_reg0x00_attr,
368 &bd2802_reg0x01_attr,
369 &bd2802_reg0x02_attr,
370 &bd2802_reg0x03_attr,
371 &bd2802_reg0x04_attr,
372 &bd2802_reg0x05_attr,
373 &bd2802_reg0x06_attr,
374 &bd2802_reg0x07_attr,
375 &bd2802_reg0x08_attr,
376 &bd2802_reg0x09_attr,
377 &bd2802_reg0x0a_attr,
378 &bd2802_reg0x0b_attr,
379 &bd2802_reg0x0c_attr,
380 &bd2802_reg0x0d_attr,
381 &bd2802_reg0x0e_attr,
382 &bd2802_reg0x0f_attr,
383 &bd2802_reg0x10_attr,
384 &bd2802_reg0x11_attr,
385 &bd2802_reg0x12_attr,
386 &bd2802_reg0x13_attr,
387 &bd2802_reg0x14_attr,
388 &bd2802_reg0x15_attr,
391 static void bd2802_enable_adv_conf(struct bd2802_led *led)
395 for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++) {
396 ret = device_create_file(&led->client->dev,
397 bd2802_addr_attributes[i]);
399 dev_err(&led->client->dev, "failed: sysfs file %s\n",
400 bd2802_addr_attributes[i]->attr.name);
401 goto failed_remove_files;
405 if (bd2802_is_all_off(led))
406 bd2802_reset_cancel(led);
413 for (i--; i >= 0; i--)
414 device_remove_file(&led->client->dev,
415 bd2802_addr_attributes[i]);
418 static void bd2802_disable_adv_conf(struct bd2802_led *led)
422 for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++)
423 device_remove_file(&led->client->dev,
424 bd2802_addr_attributes[i]);
426 if (bd2802_is_all_off(led))
427 gpio_set_value(led->pdata->reset_gpio, 0);
432 static ssize_t bd2802_show_adv_conf(struct device *dev,
433 struct device_attribute *attr, char *buf)
435 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));
438 down_read(&led->rwsem);
440 ret = sprintf(buf, "on\n");
442 ret = sprintf(buf, "off\n");
443 up_read(&led->rwsem);
448 static ssize_t bd2802_store_adv_conf(struct device *dev,
449 struct device_attribute *attr, const char *buf, size_t count)
451 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));
456 down_write(&led->rwsem);
457 if (!led->adf_on && !strncmp(buf, "on", 2))
458 bd2802_enable_adv_conf(led);
459 else if (led->adf_on && !strncmp(buf, "off", 3))
460 bd2802_disable_adv_conf(led);
461 up_write(&led->rwsem);
466 static struct device_attribute bd2802_adv_conf_attr = {
468 .name = "advanced_configuration",
471 .show = bd2802_show_adv_conf,
472 .store = bd2802_store_adv_conf,
475 #define BD2802_CONTROL_ATTR(attr_name, name_str) \
476 static ssize_t bd2802_show_##attr_name(struct device *dev, \
477 struct device_attribute *attr, char *buf) \
479 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
481 down_read(&led->rwsem); \
482 ret = sprintf(buf, "0x%02x\n", led->attr_name); \
483 up_read(&led->rwsem); \
486 static ssize_t bd2802_store_##attr_name(struct device *dev, \
487 struct device_attribute *attr, const char *buf, size_t count) \
489 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
494 ret = kstrtoul(buf, 16, &val); \
497 down_write(&led->rwsem); \
498 led->attr_name = val; \
499 up_write(&led->rwsem); \
502 static struct device_attribute bd2802_##attr_name##_attr = { \
507 .show = bd2802_show_##attr_name, \
508 .store = bd2802_store_##attr_name, \
511 BD2802_CONTROL_ATTR(wave_pattern, "wave_pattern");
512 BD2802_CONTROL_ATTR(rgb_current, "rgb_current");
514 static struct device_attribute *bd2802_attributes[] = {
515 &bd2802_adv_conf_attr,
516 &bd2802_wave_pattern_attr,
517 &bd2802_rgb_current_attr,
520 #define BD2802_CONTROL_RGBS(name, id, clr) \
521 static int bd2802_set_##name##_brightness(struct led_classdev *led_cdev,\
522 enum led_brightness value) \
524 struct bd2802_led *led = \
525 container_of(led_cdev, struct bd2802_led, cdev_##name); \
528 if (value == LED_OFF) { \
529 led->state = BD2802_OFF; \
530 bd2802_turn_off(led, led->led_id, led->color); \
532 led->state = BD2802_ON; \
533 bd2802_turn_on(led, led->led_id, led->color, BD2802_ON);\
537 static int bd2802_set_##name##_blink(struct led_classdev *led_cdev, \
538 unsigned long *delay_on, unsigned long *delay_off) \
540 struct bd2802_led *led = \
541 container_of(led_cdev, struct bd2802_led, cdev_##name); \
542 if (*delay_on == 0 || *delay_off == 0) \
546 led->state = BD2802_BLINK; \
547 bd2802_turn_on(led, led->led_id, led->color, BD2802_BLINK); \
551 BD2802_CONTROL_RGBS(led1r, LED1, RED);
552 BD2802_CONTROL_RGBS(led1g, LED1, GREEN);
553 BD2802_CONTROL_RGBS(led1b, LED1, BLUE);
554 BD2802_CONTROL_RGBS(led2r, LED2, RED);
555 BD2802_CONTROL_RGBS(led2g, LED2, GREEN);
556 BD2802_CONTROL_RGBS(led2b, LED2, BLUE);
558 static int bd2802_register_led_classdev(struct bd2802_led *led)
562 led->cdev_led1r.name = "led1_R";
563 led->cdev_led1r.brightness = LED_OFF;
564 led->cdev_led1r.brightness_set_blocking = bd2802_set_led1r_brightness;
565 led->cdev_led1r.blink_set = bd2802_set_led1r_blink;
567 ret = led_classdev_register(&led->client->dev, &led->cdev_led1r);
569 dev_err(&led->client->dev, "couldn't register LED %s\n",
570 led->cdev_led1r.name);
571 goto failed_unregister_led1_R;
574 led->cdev_led1g.name = "led1_G";
575 led->cdev_led1g.brightness = LED_OFF;
576 led->cdev_led1g.brightness_set_blocking = bd2802_set_led1g_brightness;
577 led->cdev_led1g.blink_set = bd2802_set_led1g_blink;
579 ret = led_classdev_register(&led->client->dev, &led->cdev_led1g);
581 dev_err(&led->client->dev, "couldn't register LED %s\n",
582 led->cdev_led1g.name);
583 goto failed_unregister_led1_G;
586 led->cdev_led1b.name = "led1_B";
587 led->cdev_led1b.brightness = LED_OFF;
588 led->cdev_led1b.brightness_set_blocking = bd2802_set_led1b_brightness;
589 led->cdev_led1b.blink_set = bd2802_set_led1b_blink;
591 ret = led_classdev_register(&led->client->dev, &led->cdev_led1b);
593 dev_err(&led->client->dev, "couldn't register LED %s\n",
594 led->cdev_led1b.name);
595 goto failed_unregister_led1_B;
598 led->cdev_led2r.name = "led2_R";
599 led->cdev_led2r.brightness = LED_OFF;
600 led->cdev_led2r.brightness_set_blocking = bd2802_set_led2r_brightness;
601 led->cdev_led2r.blink_set = bd2802_set_led2r_blink;
603 ret = led_classdev_register(&led->client->dev, &led->cdev_led2r);
605 dev_err(&led->client->dev, "couldn't register LED %s\n",
606 led->cdev_led2r.name);
607 goto failed_unregister_led2_R;
610 led->cdev_led2g.name = "led2_G";
611 led->cdev_led2g.brightness = LED_OFF;
612 led->cdev_led2g.brightness_set_blocking = bd2802_set_led2g_brightness;
613 led->cdev_led2g.blink_set = bd2802_set_led2g_blink;
615 ret = led_classdev_register(&led->client->dev, &led->cdev_led2g);
617 dev_err(&led->client->dev, "couldn't register LED %s\n",
618 led->cdev_led2g.name);
619 goto failed_unregister_led2_G;
622 led->cdev_led2b.name = "led2_B";
623 led->cdev_led2b.brightness = LED_OFF;
624 led->cdev_led2b.brightness_set_blocking = bd2802_set_led2b_brightness;
625 led->cdev_led2b.blink_set = bd2802_set_led2b_blink;
626 led->cdev_led2b.flags |= LED_CORE_SUSPENDRESUME;
628 ret = led_classdev_register(&led->client->dev, &led->cdev_led2b);
630 dev_err(&led->client->dev, "couldn't register LED %s\n",
631 led->cdev_led2b.name);
632 goto failed_unregister_led2_B;
637 failed_unregister_led2_B:
638 led_classdev_unregister(&led->cdev_led2g);
639 failed_unregister_led2_G:
640 led_classdev_unregister(&led->cdev_led2r);
641 failed_unregister_led2_R:
642 led_classdev_unregister(&led->cdev_led1b);
643 failed_unregister_led1_B:
644 led_classdev_unregister(&led->cdev_led1g);
645 failed_unregister_led1_G:
646 led_classdev_unregister(&led->cdev_led1r);
647 failed_unregister_led1_R:
652 static void bd2802_unregister_led_classdev(struct bd2802_led *led)
654 led_classdev_unregister(&led->cdev_led2b);
655 led_classdev_unregister(&led->cdev_led2g);
656 led_classdev_unregister(&led->cdev_led2r);
657 led_classdev_unregister(&led->cdev_led1b);
658 led_classdev_unregister(&led->cdev_led1g);
659 led_classdev_unregister(&led->cdev_led1r);
662 static int bd2802_probe(struct i2c_client *client,
663 const struct i2c_device_id *id)
665 struct bd2802_led *led;
666 struct bd2802_led_platform_data *pdata;
669 led = devm_kzalloc(&client->dev, sizeof(struct bd2802_led), GFP_KERNEL);
673 led->client = client;
674 pdata = led->pdata = dev_get_platdata(&client->dev);
675 i2c_set_clientdata(client, led);
677 /* Configure RESET GPIO (L: RESET, H: RESET cancel) */
678 gpio_request_one(pdata->reset_gpio, GPIOF_OUT_INIT_HIGH, "RGB_RESETB");
680 /* Tacss = min 0.1ms */
683 /* Detect BD2802GU */
684 ret = bd2802_write_byte(client, BD2802_REG_CLKSETUP, 0x00);
686 dev_err(&client->dev, "failed to detect device\n");
689 dev_info(&client->dev, "return 0x%02x\n", ret);
691 /* To save the power, reset BD2802 after detecting */
692 gpio_set_value(led->pdata->reset_gpio, 0);
694 /* Default attributes */
695 led->wave_pattern = BD2802_PATTERN_HALF;
696 led->rgb_current = BD2802_CURRENT_032;
698 init_rwsem(&led->rwsem);
700 for (i = 0; i < ARRAY_SIZE(bd2802_attributes); i++) {
701 ret = device_create_file(&led->client->dev,
702 bd2802_attributes[i]);
704 dev_err(&led->client->dev, "failed: sysfs file %s\n",
705 bd2802_attributes[i]->attr.name);
706 goto failed_unregister_dev_file;
710 ret = bd2802_register_led_classdev(led);
712 goto failed_unregister_dev_file;
716 failed_unregister_dev_file:
717 for (i--; i >= 0; i--)
718 device_remove_file(&led->client->dev, bd2802_attributes[i]);
722 static int bd2802_remove(struct i2c_client *client)
724 struct bd2802_led *led = i2c_get_clientdata(client);
727 gpio_set_value(led->pdata->reset_gpio, 0);
728 bd2802_unregister_led_classdev(led);
730 bd2802_disable_adv_conf(led);
731 for (i = 0; i < ARRAY_SIZE(bd2802_attributes); i++)
732 device_remove_file(&led->client->dev, bd2802_attributes[i]);
737 #ifdef CONFIG_PM_SLEEP
738 static void bd2802_restore_state(struct bd2802_led *led)
742 for (i = 0; i < LED_NUM; i++) {
744 bd2802_turn_on(led, i, RED, led->led[i].r);
746 bd2802_turn_on(led, i, GREEN, led->led[i].g);
748 bd2802_turn_on(led, i, BLUE, led->led[i].b);
752 static int bd2802_suspend(struct device *dev)
754 struct i2c_client *client = to_i2c_client(dev);
755 struct bd2802_led *led = i2c_get_clientdata(client);
757 gpio_set_value(led->pdata->reset_gpio, 0);
762 static int bd2802_resume(struct device *dev)
764 struct i2c_client *client = to_i2c_client(dev);
765 struct bd2802_led *led = i2c_get_clientdata(client);
767 if (!bd2802_is_all_off(led) || led->adf_on) {
768 bd2802_reset_cancel(led);
769 bd2802_restore_state(led);
776 static SIMPLE_DEV_PM_OPS(bd2802_pm, bd2802_suspend, bd2802_resume);
778 static const struct i2c_device_id bd2802_id[] = {
782 MODULE_DEVICE_TABLE(i2c, bd2802_id);
784 static struct i2c_driver bd2802_i2c_driver = {
789 .probe = bd2802_probe,
790 .remove = bd2802_remove,
791 .id_table = bd2802_id,
794 module_i2c_driver(bd2802_i2c_driver);
796 MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
797 MODULE_DESCRIPTION("BD2802 LED driver");
798 MODULE_LICENSE("GPL v2");