aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/leds
diff options
context:
space:
mode:
authorMilo(Woogyom) Kim <milo.kim@ti.com>2013-02-05 05:09:32 -0500
committerBryan Wu <cooloney@gmail.com>2013-02-06 18:59:28 -0500
commita96bfa135ddc8045166fc6311ce4d21bfcb8d13d (patch)
tree405bc5264b0b09af0d77db11cdae2923279db188 /drivers/leds
parenta6e4679a09a0a2bcfa63074272fc9fb2a40f11ad (diff)
leds-lp55xx: provide common LED current setting
LED current is configurable via the sysfs. Max current is a read-only attribute. These attributes code can be shared in lp55xx common driver. Device attributes: 'led_current' and 'max_current' move to lp55xx common driver Replaced functions: show_max_current() => lp55xx_show_max_current() show_current() => lp55xx_show_current() store_current() => lp55xx_store_current() LED setting function: set_led_current() Current registers are device specific, so configurable function is added in each driver. Signed-off-by: Milo(Woogyom) Kim <milo.kim@ti.com> Signed-off-by: Bryan Wu <cooloney@gmail.com>
Diffstat (limited to 'drivers/leds')
-rw-r--r--drivers/leds/leds-lp5521.c74
-rw-r--r--drivers/leds/leds-lp5523.c69
-rw-r--r--drivers/leds/leds-lp55xx-common.c53
3 files changed, 69 insertions, 127 deletions
diff --git a/drivers/leds/leds-lp5521.c b/drivers/leds/leds-lp5521.c
index 6efbb7ec0e2d..7133af824b5e 100644
--- a/drivers/leds/leds-lp5521.c
+++ b/drivers/leds/leds-lp5521.c
@@ -134,6 +134,13 @@ static inline void lp5521_wait_enable_done(void)
134 usleep_range(500, 600); 134 usleep_range(500, 600);
135} 135}
136 136
137static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current)
138{
139 led->led_current = led_current;
140 lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr,
141 led_current);
142}
143
137static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev) 144static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
138{ 145{
139 return container_of(cdev, struct lp5521_led, cdev); 146 return container_of(cdev, struct lp5521_led, cdev);
@@ -229,13 +236,6 @@ static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
229 return lp5521_write(client, LP5521_REG_OP_MODE, mode); 236 return lp5521_write(client, LP5521_REG_OP_MODE, mode);
230} 237}
231 238
232static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
233{
234 return lp5521_write(chip->client,
235 LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
236 curr);
237}
238
239static int lp5521_post_init_device(struct lp55xx_chip *chip) 239static int lp5521_post_init_device(struct lp55xx_chip *chip)
240{ 240{
241 int ret; 241 int ret;
@@ -462,54 +462,6 @@ store_mode(1)
462store_mode(2) 462store_mode(2)
463store_mode(3) 463store_mode(3)
464 464
465static ssize_t show_max_current(struct device *dev,
466 struct device_attribute *attr,
467 char *buf)
468{
469 struct led_classdev *led_cdev = dev_get_drvdata(dev);
470 struct lp5521_led *led = cdev_to_led(led_cdev);
471
472 return sprintf(buf, "%d\n", led->max_current);
473}
474
475static ssize_t show_current(struct device *dev,
476 struct device_attribute *attr,
477 char *buf)
478{
479 struct led_classdev *led_cdev = dev_get_drvdata(dev);
480 struct lp5521_led *led = cdev_to_led(led_cdev);
481
482 return sprintf(buf, "%d\n", led->led_current);
483}
484
485static ssize_t store_current(struct device *dev,
486 struct device_attribute *attr,
487 const char *buf, size_t len)
488{
489 struct led_classdev *led_cdev = dev_get_drvdata(dev);
490 struct lp5521_led *led = cdev_to_led(led_cdev);
491 struct lp5521_chip *chip = led_to_lp5521(led);
492 ssize_t ret;
493 unsigned long curr;
494
495 if (kstrtoul(buf, 0, &curr))
496 return -EINVAL;
497
498 if (curr > led->max_current)
499 return -EINVAL;
500
501 mutex_lock(&chip->lock);
502 ret = lp5521_set_led_current(chip, led->id, curr);
503 mutex_unlock(&chip->lock);
504
505 if (ret < 0)
506 return ret;
507
508 led->led_current = (u8)curr;
509
510 return len;
511}
512
513static ssize_t lp5521_selftest(struct device *dev, 465static ssize_t lp5521_selftest(struct device *dev,
514 struct device_attribute *attr, 466 struct device_attribute *attr,
515 char *buf) 467 char *buf)
@@ -615,18 +567,7 @@ static ssize_t store_led_pattern(struct device *dev,
615 return len; 567 return len;
616} 568}
617 569
618/* led class device attributes */
619static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
620static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
621
622static struct attribute *lp5521_led_attributes[] = {
623 &dev_attr_led_current.attr,
624 &dev_attr_max_current.attr,
625 NULL,
626};
627
628static struct attribute_group lp5521_led_attribute_group = { 570static struct attribute_group lp5521_led_attribute_group = {
629 .attrs = lp5521_led_attributes
630}; 571};
631 572
632/* device attributes */ 573/* device attributes */
@@ -700,6 +641,7 @@ static struct lp55xx_device_config lp5521_cfg = {
700 .max_channel = LP5521_MAX_LEDS, 641 .max_channel = LP5521_MAX_LEDS,
701 .post_init_device = lp5521_post_init_device, 642 .post_init_device = lp5521_post_init_device,
702 .brightness_work_fn = lp5521_led_brightness_work, 643 .brightness_work_fn = lp5521_led_brightness_work,
644 .set_led_current = lp5521_set_led_current,
703}; 645};
704 646
705static int lp5521_probe(struct i2c_client *client, 647static int lp5521_probe(struct i2c_client *client,
diff --git a/drivers/leds/leds-lp5523.c b/drivers/leds/leds-lp5523.c
index 43db2429616b..dfb335420568 100644
--- a/drivers/leds/leds-lp5523.c
+++ b/drivers/leds/leds-lp5523.c
@@ -142,6 +142,13 @@ struct lp5523_chip {
142 u8 num_leds; 142 u8 num_leds;
143}; 143};
144 144
145static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current)
146{
147 led->led_current = led_current;
148 lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
149 led_current);
150}
151
145static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev) 152static inline struct lp5523_led *cdev_to_led(struct led_classdev *cdev)
146{ 153{
147 return container_of(cdev, struct lp5523_led, cdev); 154 return container_of(cdev, struct lp5523_led, cdev);
@@ -602,68 +609,7 @@ store_mode(1)
602store_mode(2) 609store_mode(2)
603store_mode(3) 610store_mode(3)
604 611
605static ssize_t show_max_current(struct device *dev,
606 struct device_attribute *attr,
607 char *buf)
608{
609 struct led_classdev *led_cdev = dev_get_drvdata(dev);
610 struct lp5523_led *led = cdev_to_led(led_cdev);
611
612 return sprintf(buf, "%d\n", led->max_current);
613}
614
615static ssize_t show_current(struct device *dev,
616 struct device_attribute *attr,
617 char *buf)
618{
619 struct led_classdev *led_cdev = dev_get_drvdata(dev);
620 struct lp5523_led *led = cdev_to_led(led_cdev);
621
622 return sprintf(buf, "%d\n", led->led_current);
623}
624
625static ssize_t store_current(struct device *dev,
626 struct device_attribute *attr,
627 const char *buf, size_t len)
628{
629 struct led_classdev *led_cdev = dev_get_drvdata(dev);
630 struct lp5523_led *led = cdev_to_led(led_cdev);
631 struct lp5523_chip *chip = led_to_lp5523(led);
632 ssize_t ret;
633 unsigned long curr;
634
635 if (kstrtoul(buf, 0, &curr))
636 return -EINVAL;
637
638 if (curr > led->max_current)
639 return -EINVAL;
640
641 mutex_lock(&chip->lock);
642 ret = lp5523_write(chip->client,
643 LP5523_REG_LED_CURRENT_BASE + led->chan_nr,
644 (u8)curr);
645 mutex_unlock(&chip->lock);
646
647 if (ret < 0)
648 return ret;
649
650 led->led_current = (u8)curr;
651
652 return len;
653}
654
655/* led class device attributes */
656static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
657static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
658
659static struct attribute *lp5523_led_attributes[] = {
660 &dev_attr_led_current.attr,
661 &dev_attr_max_current.attr,
662 NULL,
663};
664
665static struct attribute_group lp5523_led_attribute_group = { 612static struct attribute_group lp5523_led_attribute_group = {
666 .attrs = lp5523_led_attributes
667}; 613};
668 614
669/* device attributes */ 615/* device attributes */
@@ -780,6 +726,7 @@ static struct lp55xx_device_config lp5523_cfg = {
780 .max_channel = LP5523_MAX_LEDS, 726 .max_channel = LP5523_MAX_LEDS,
781 .post_init_device = lp5523_post_init_device, 727 .post_init_device = lp5523_post_init_device,
782 .brightness_work_fn = lp5523_led_brightness_work, 728 .brightness_work_fn = lp5523_led_brightness_work,
729 .set_led_current = lp5523_set_led_current,
783}; 730};
784 731
785static int lp5523_probe(struct i2c_client *client, 732static int lp5523_probe(struct i2c_client *client,
diff --git a/drivers/leds/leds-lp55xx-common.c b/drivers/leds/leds-lp55xx-common.c
index 8244d78447f4..6b3d03709f5f 100644
--- a/drivers/leds/leds-lp55xx-common.c
+++ b/drivers/leds/leds-lp55xx-common.c
@@ -25,6 +25,11 @@ static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
25 return container_of(cdev, struct lp55xx_led, cdev); 25 return container_of(cdev, struct lp55xx_led, cdev);
26} 26}
27 27
28static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
29{
30 return cdev_to_lp55xx_led(dev_get_drvdata(dev));
31}
32
28static void lp55xx_reset_device(struct lp55xx_chip *chip) 33static void lp55xx_reset_device(struct lp55xx_chip *chip)
29{ 34{
30 struct lp55xx_device_config *cfg = chip->cfg; 35 struct lp55xx_device_config *cfg = chip->cfg;
@@ -68,7 +73,55 @@ static int lp55xx_post_init_device(struct lp55xx_chip *chip)
68 return cfg->post_init_device(chip); 73 return cfg->post_init_device(chip);
69} 74}
70 75
76static ssize_t lp55xx_show_current(struct device *dev,
77 struct device_attribute *attr,
78 char *buf)
79{
80 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
81
82 return sprintf(buf, "%d\n", led->led_current);
83}
84
85static ssize_t lp55xx_store_current(struct device *dev,
86 struct device_attribute *attr,
87 const char *buf, size_t len)
88{
89 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
90 struct lp55xx_chip *chip = led->chip;
91 unsigned long curr;
92
93 if (kstrtoul(buf, 0, &curr))
94 return -EINVAL;
95
96 if (curr > led->max_current)
97 return -EINVAL;
98
99 if (!chip->cfg->set_led_current)
100 return len;
101
102 mutex_lock(&chip->lock);
103 chip->cfg->set_led_current(led, (u8)curr);
104 mutex_unlock(&chip->lock);
105
106 return len;
107}
108
109static ssize_t lp55xx_show_max_current(struct device *dev,
110 struct device_attribute *attr,
111 char *buf)
112{
113 struct lp55xx_led *led = dev_to_lp55xx_led(dev);
114
115 return sprintf(buf, "%d\n", led->max_current);
116}
117
118static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current,
119 lp55xx_store_current);
120static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL);
121
71static struct attribute *lp55xx_led_attributes[] = { 122static struct attribute *lp55xx_led_attributes[] = {
123 &dev_attr_led_current.attr,
124 &dev_attr_max_current.attr,
72 NULL, 125 NULL,
73}; 126};
74 127