15 #include <linux/module.h>
16 #include <linux/i2c.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
37 #define BD2802_CURRENT_000 0x00
39 #define BD2802_PATTERN_FULL 0x07
40 #define BD2802_PATTERN_HALF 0x03
117 return !led->
led[
id].r;
119 return !led->
led[
id].g;
121 return !led->
led[
id].b;
130 if (led->
led[
id].r || led->
led[
id].g || led->
led[
id].b)
136 static inline int bd2802_is_all_off(
struct bd2802_led *led)
141 if (!bd2802_is_led_off(led, i))
155 return reg_offset + bd2802_get_base_offset(
id, color);
169 dev_err(&client->
dev,
"%s: reg 0x%x, val 0x%x, err %d\n",
170 __func__, reg, val, ret);
181 for (i = 0; i <
LED_NUM; i++) {
185 led->
led[
i].r = led_bit;
188 led->
led[
i].g = led_bit;
191 led->
led[
i].b = led_bit;
195 "%s: Invalid color\n", __func__);
204 if (!bd2802_is_led_off(led,
id))
207 if (bd2802_is_all_off(led) && !led->
adf_on) {
220 static void bd2802_configure(
struct bd2802_led *led)
232 static void bd2802_reset_cancel(
struct bd2802_led *led)
236 bd2802_configure(led);
244 other_led_on = !bd2802_is_led_off(led, other_led);
246 value =
LED_CTL(other_led_on, 1);
248 value =
LED_CTL(1 , other_led_on);
258 if (bd2802_is_all_off(led) && !led->
adf_on)
259 bd2802_reset_cancel(led);
268 bd2802_enable(led,
id);
269 bd2802_update_state(led,
id, color,
BD2802_ON);
277 if (bd2802_is_all_off(led) && !led->
adf_on)
278 bd2802_reset_cancel(led);
287 bd2802_enable(led,
id);
296 "Only 'blink' and 'on' are allowed\n");
301 bd2802_set_blink(led,
id, color);
303 bd2802_set_on(led,
id, color);
311 if (bd2802_is_rgb_off(led,
id, color))
319 bd2802_update_state(led,
id, color,
BD2802_OFF);
322 #define BD2802_SET_REGISTER(reg_addr, reg_name) \
323 static ssize_t bd2802_store_reg##reg_addr(struct device *dev, \
324 struct device_attribute *attr, const char *buf, size_t count) \
326 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
331 ret = strict_strtoul(buf, 16, &val); \
334 down_write(&led->rwsem); \
335 bd2802_write_byte(led->client, reg_addr, (u8) val); \
336 up_write(&led->rwsem); \
339 static struct device_attribute bd2802_reg##reg_addr##_attr = { \
340 .attr = {.name = reg_name, .mode = 0644}, \
341 .store = bd2802_store_reg##reg_addr, \
368 &bd2802_reg0x00_attr,
369 &bd2802_reg0x01_attr,
370 &bd2802_reg0x02_attr,
371 &bd2802_reg0x03_attr,
372 &bd2802_reg0x04_attr,
373 &bd2802_reg0x05_attr,
374 &bd2802_reg0x06_attr,
375 &bd2802_reg0x07_attr,
376 &bd2802_reg0x08_attr,
377 &bd2802_reg0x09_attr,
378 &bd2802_reg0x0a_attr,
379 &bd2802_reg0x0b_attr,
380 &bd2802_reg0x0c_attr,
381 &bd2802_reg0x0d_attr,
382 &bd2802_reg0x0e_attr,
383 &bd2802_reg0x0f_attr,
384 &bd2802_reg0x10_attr,
385 &bd2802_reg0x11_attr,
386 &bd2802_reg0x12_attr,
387 &bd2802_reg0x13_attr,
388 &bd2802_reg0x14_attr,
389 &bd2802_reg0x15_attr,
392 static void bd2802_enable_adv_conf(
struct bd2802_led *led)
396 for (i = 0; i <
ARRAY_SIZE(bd2802_addr_attributes); i++) {
398 bd2802_addr_attributes[i]);
401 bd2802_addr_attributes[i]->
attr.name);
402 goto failed_remove_files;
406 if (bd2802_is_all_off(led))
407 bd2802_reset_cancel(led);
414 for (i--; i >= 0; i--)
416 bd2802_addr_attributes[i]);
419 static void bd2802_disable_adv_conf(
struct bd2802_led *led)
423 for (i = 0; i <
ARRAY_SIZE(bd2802_addr_attributes); i++)
425 bd2802_addr_attributes[i]);
427 if (bd2802_is_all_off(led))
459 bd2802_enable_adv_conf(led);
461 bd2802_disable_adv_conf(led);
469 .name =
"advanced_configuration",
472 .show = bd2802_show_adv_conf,
473 .store = bd2802_store_adv_conf,
476 #define BD2802_CONTROL_ATTR(attr_name, name_str) \
477 static ssize_t bd2802_show_##attr_name(struct device *dev, \
478 struct device_attribute *attr, char *buf) \
480 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
482 down_read(&led->rwsem); \
483 ret = sprintf(buf, "0x%02x\n", led->attr_name); \
484 up_read(&led->rwsem); \
487 static ssize_t bd2802_store_##attr_name(struct device *dev, \
488 struct device_attribute *attr, const char *buf, size_t count) \
490 struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
495 ret = strict_strtoul(buf, 16, &val); \
498 down_write(&led->rwsem); \
499 led->attr_name = val; \
500 up_write(&led->rwsem); \
503 static struct device_attribute bd2802_##attr_name##_attr = { \
508 .show = bd2802_show_##attr_name, \
509 .store = bd2802_store_##attr_name, \
516 &bd2802_adv_conf_attr,
517 &bd2802_wave_pattern_attr,
518 &bd2802_rgb_current_attr,
531 #define BD2802_CONTROL_RGBS(name, id, clr) \
532 static void bd2802_set_##name##_brightness(struct led_classdev *led_cdev,\
533 enum led_brightness value) \
535 struct bd2802_led *led = \
536 container_of(led_cdev, struct bd2802_led, cdev_##name); \
539 if (value == LED_OFF) \
540 led->state = BD2802_OFF; \
542 led->state = BD2802_ON; \
543 schedule_work(&led->work); \
545 static int bd2802_set_##name##_blink(struct led_classdev *led_cdev, \
546 unsigned long *delay_on, unsigned long *delay_off) \
548 struct bd2802_led *led = \
549 container_of(led_cdev, struct bd2802_led, cdev_##name); \
550 if (*delay_on == 0 || *delay_off == 0) \
554 led->state = BD2802_BLINK; \
555 schedule_work(&led->work); \
566 static int bd2802_register_led_classdev(
struct bd2802_led *led)
574 led->
cdev_led1r.brightness_set = bd2802_set_led1r_brightness;
575 led->
cdev_led1r.blink_set = bd2802_set_led1r_blink;
581 goto failed_unregister_led1_R;
586 led->
cdev_led1g.brightness_set = bd2802_set_led1g_brightness;
587 led->
cdev_led1g.blink_set = bd2802_set_led1g_blink;
593 goto failed_unregister_led1_G;
598 led->
cdev_led1b.brightness_set = bd2802_set_led1b_brightness;
599 led->
cdev_led1b.blink_set = bd2802_set_led1b_blink;
605 goto failed_unregister_led1_B;
610 led->
cdev_led2r.brightness_set = bd2802_set_led2r_brightness;
611 led->
cdev_led2r.blink_set = bd2802_set_led2r_blink;
617 goto failed_unregister_led2_R;
622 led->
cdev_led2g.brightness_set = bd2802_set_led2g_brightness;
623 led->
cdev_led2g.blink_set = bd2802_set_led2g_blink;
629 goto failed_unregister_led2_G;
634 led->
cdev_led2b.brightness_set = bd2802_set_led2b_brightness;
635 led->
cdev_led2b.blink_set = bd2802_set_led2b_blink;
642 goto failed_unregister_led2_B;
647 failed_unregister_led2_B:
649 failed_unregister_led2_G:
651 failed_unregister_led2_R:
653 failed_unregister_led1_B:
655 failed_unregister_led1_G:
657 failed_unregister_led1_R:
662 static void bd2802_unregister_led_classdev(
struct bd2802_led *led)
682 dev_err(&client->
dev,
"failed to allocate driver data\n");
687 pdata = led->
pdata = client->
dev.platform_data;
688 i2c_set_clientdata(client, led);
699 dev_err(&client->
dev,
"failed to detect device\n");
713 for (i = 0; i <
ARRAY_SIZE(bd2802_attributes); i++) {
715 bd2802_attributes[i]);
718 bd2802_attributes[i]->
attr.name);
719 goto failed_unregister_dev_file;
723 ret = bd2802_register_led_classdev(led);
725 goto failed_unregister_dev_file;
729 failed_unregister_dev_file:
730 for (i--; i >= 0; i--)
737 struct bd2802_led *led = i2c_get_clientdata(client);
741 bd2802_unregister_led_classdev(led);
743 bd2802_disable_adv_conf(led);
744 for (i = 0; i <
ARRAY_SIZE(bd2802_attributes); i++)
752 static void bd2802_restore_state(
struct bd2802_led *led)
756 for (i = 0; i <
LED_NUM; i++) {
758 bd2802_turn_on(led, i,
RED, led->
led[i].r);
760 bd2802_turn_on(led, i,
GREEN, led->
led[i].g);
762 bd2802_turn_on(led, i,
BLUE, led->
led[i].b);
766 static int bd2802_suspend(
struct device *dev)
769 struct bd2802_led *led = i2c_get_clientdata(client);
776 static int bd2802_resume(
struct device *dev)
779 struct bd2802_led *led = i2c_get_clientdata(client);
781 if (!bd2802_is_all_off(led) || led->
adf_on) {
782 bd2802_reset_cancel(led);
783 bd2802_restore_state(led);
790 #define BD2802_PM (&bd2802_pm)
792 #define BD2802_PM NULL
801 static struct i2c_driver bd2802_i2c_driver = {
806 .probe = bd2802_probe,
808 .id_table = bd2802_id,