2 * leds-lm3533.c -- LM3533 LED driver
4 * Copyright (C) 2011-2012 Texas Instruments
6 * Author: Johan Hovold <jhovold@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/leds.h>
16 #include <linux/mfd/core.h>
17 #include <linux/mutex.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/workqueue.h>
22 #include <linux/mfd/lm3533.h>
25 #define LM3533_LVCTRLBANK_MIN 2
26 #define LM3533_LVCTRLBANK_MAX 5
27 #define LM3533_LVCTRLBANK_COUNT 4
28 #define LM3533_RISEFALLTIME_MAX 7
29 #define LM3533_ALS_CHANNEL_LV_MIN 1
30 #define LM3533_ALS_CHANNEL_LV_MAX 2
32 #define LM3533_REG_CTRLBANK_BCONF_BASE 0x1b
33 #define LM3533_REG_PATTERN_ENABLE 0x28
34 #define LM3533_REG_PATTERN_LOW_TIME_BASE 0x71
35 #define LM3533_REG_PATTERN_HIGH_TIME_BASE 0x72
36 #define LM3533_REG_PATTERN_RISETIME_BASE 0x74
37 #define LM3533_REG_PATTERN_FALLTIME_BASE 0x75
39 #define LM3533_REG_PATTERN_STEP 0x10
41 #define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK 0x04
42 #define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK 0x02
43 #define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK 0x01
45 #define LM3533_LED_FLAG_PATTERN_ENABLE 1
49 struct lm3533
*lm3533
;
50 struct lm3533_ctrlbank cb
;
51 struct led_classdev cdev
;
57 struct work_struct work
;
62 static inline struct lm3533_led
*to_lm3533_led(struct led_classdev
*cdev
)
64 return container_of(cdev
, struct lm3533_led
, cdev
);
67 static inline int lm3533_led_get_ctrlbank_id(struct lm3533_led
*led
)
72 static inline u8
lm3533_led_get_lv_reg(struct lm3533_led
*led
, u8 base
)
74 return base
+ led
->id
;
77 static inline u8
lm3533_led_get_pattern(struct lm3533_led
*led
)
82 static inline u8
lm3533_led_get_pattern_reg(struct lm3533_led
*led
,
85 return base
+ lm3533_led_get_pattern(led
) * LM3533_REG_PATTERN_STEP
;
88 static int lm3533_led_pattern_enable(struct lm3533_led
*led
, int enable
)
96 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, enable
);
98 mutex_lock(&led
->mutex
);
100 state
= test_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
101 if ((enable
&& state
) || (!enable
&& !state
))
104 pattern
= lm3533_led_get_pattern(led
);
105 mask
= 1 << (2 * pattern
);
112 ret
= lm3533_update(led
->lm3533
, LM3533_REG_PATTERN_ENABLE
, val
, mask
);
114 dev_err(led
->cdev
.dev
, "failed to enable pattern %d (%d)\n",
119 __change_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
121 mutex_unlock(&led
->mutex
);
126 static void lm3533_led_work(struct work_struct
*work
)
128 struct lm3533_led
*led
= container_of(work
, struct lm3533_led
, work
);
130 dev_dbg(led
->cdev
.dev
, "%s - %u\n", __func__
, led
->new_brightness
);
132 if (led
->new_brightness
== 0)
133 lm3533_led_pattern_enable(led
, 0); /* disable blink */
135 lm3533_ctrlbank_set_brightness(&led
->cb
, led
->new_brightness
);
138 static void lm3533_led_set(struct led_classdev
*cdev
,
139 enum led_brightness value
)
141 struct lm3533_led
*led
= to_lm3533_led(cdev
);
143 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, value
);
145 led
->new_brightness
= value
;
146 schedule_work(&led
->work
);
149 static enum led_brightness
lm3533_led_get(struct led_classdev
*cdev
)
151 struct lm3533_led
*led
= to_lm3533_led(cdev
);
155 ret
= lm3533_ctrlbank_get_brightness(&led
->cb
, &val
);
159 dev_dbg(led
->cdev
.dev
, "%s - %u\n", __func__
, val
);
164 /* Pattern generator defines (delays in us). */
165 #define LM3533_LED_DELAY1_VMIN 0x00
166 #define LM3533_LED_DELAY2_VMIN 0x3d
167 #define LM3533_LED_DELAY3_VMIN 0x80
169 #define LM3533_LED_DELAY1_VMAX (LM3533_LED_DELAY2_VMIN - 1)
170 #define LM3533_LED_DELAY2_VMAX (LM3533_LED_DELAY3_VMIN - 1)
171 #define LM3533_LED_DELAY3_VMAX 0xff
173 #define LM3533_LED_DELAY1_TMIN 16384U
174 #define LM3533_LED_DELAY2_TMIN 1130496U
175 #define LM3533_LED_DELAY3_TMIN 10305536U
177 #define LM3533_LED_DELAY1_TMAX 999424U
178 #define LM3533_LED_DELAY2_TMAX 9781248U
179 #define LM3533_LED_DELAY3_TMAX 76890112U
181 /* t_step = (t_max - t_min) / (v_max - v_min) */
182 #define LM3533_LED_DELAY1_TSTEP 16384
183 #define LM3533_LED_DELAY2_TSTEP 131072
184 #define LM3533_LED_DELAY3_TSTEP 524288
186 /* Delay limits for hardware accelerated blinking (in ms). */
187 #define LM3533_LED_DELAY_ON_MAX \
188 ((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000)
189 #define LM3533_LED_DELAY_OFF_MAX \
190 ((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000)
193 * Returns linear map of *t from [t_min,t_max] to [v_min,v_max] with a step
194 * size of t_step, where
196 * t_step = (t_max - t_min) / (v_max - v_min)
198 * and updates *t to reflect the mapped value.
200 static u8
time_to_val(unsigned *t
, unsigned t_min
, unsigned t_step
,
205 val
= (*t
+ t_step
/ 2 - t_min
) / t_step
+ v_min
;
207 *t
= t_step
* (val
- v_min
) + t_min
;
213 * Returns time code corresponding to *delay (in ms) and updates *delay to
214 * reflect actual hardware delay.
216 * Hardware supports 256 discrete delay times, divided into three groups with
217 * the following ranges and step-sizes:
219 * [ 16, 999] [0x00, 0x3e] step 16 ms
220 * [ 1130, 9781] [0x3d, 0x7f] step 131 ms
221 * [10306, 76890] [0x80, 0xff] step 524 ms
223 * Note that delay group 3 is only available for delay_off.
225 static u8
lm3533_led_get_hw_delay(unsigned *delay
)
232 if (t
>= (LM3533_LED_DELAY2_TMAX
+ LM3533_LED_DELAY3_TMIN
) / 2) {
233 t
= clamp(t
, LM3533_LED_DELAY3_TMIN
, LM3533_LED_DELAY3_TMAX
);
234 val
= time_to_val(&t
, LM3533_LED_DELAY3_TMIN
,
235 LM3533_LED_DELAY3_TSTEP
,
236 LM3533_LED_DELAY3_VMIN
,
237 LM3533_LED_DELAY3_VMAX
);
238 } else if (t
>= (LM3533_LED_DELAY1_TMAX
+ LM3533_LED_DELAY2_TMIN
) / 2) {
239 t
= clamp(t
, LM3533_LED_DELAY2_TMIN
, LM3533_LED_DELAY2_TMAX
);
240 val
= time_to_val(&t
, LM3533_LED_DELAY2_TMIN
,
241 LM3533_LED_DELAY2_TSTEP
,
242 LM3533_LED_DELAY2_VMIN
,
243 LM3533_LED_DELAY2_VMAX
);
245 t
= clamp(t
, LM3533_LED_DELAY1_TMIN
, LM3533_LED_DELAY1_TMAX
);
246 val
= time_to_val(&t
, LM3533_LED_DELAY1_TMIN
,
247 LM3533_LED_DELAY1_TSTEP
,
248 LM3533_LED_DELAY1_VMIN
,
249 LM3533_LED_DELAY1_VMAX
);
252 *delay
= (t
+ 500) / 1000;
258 * Set delay register base to *delay (in ms) and update *delay to reflect
259 * actual hardware delay used.
261 static u8
lm3533_led_delay_set(struct lm3533_led
*led
, u8 base
,
262 unsigned long *delay
)
269 t
= (unsigned)*delay
;
271 /* Delay group 3 is only available for low time (delay off). */
272 if (base
!= LM3533_REG_PATTERN_LOW_TIME_BASE
)
273 t
= min(t
, LM3533_LED_DELAY2_TMAX
/ 1000);
275 val
= lm3533_led_get_hw_delay(&t
);
277 dev_dbg(led
->cdev
.dev
, "%s - %lu: %u (0x%02x)\n", __func__
,
279 reg
= lm3533_led_get_pattern_reg(led
, base
);
280 ret
= lm3533_write(led
->lm3533
, reg
, val
);
282 dev_err(led
->cdev
.dev
, "failed to set delay (%02x)\n", reg
);
289 static int lm3533_led_delay_on_set(struct lm3533_led
*led
, unsigned long *t
)
291 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_HIGH_TIME_BASE
, t
);
294 static int lm3533_led_delay_off_set(struct lm3533_led
*led
, unsigned long *t
)
296 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_LOW_TIME_BASE
, t
);
299 static int lm3533_led_blink_set(struct led_classdev
*cdev
,
300 unsigned long *delay_on
,
301 unsigned long *delay_off
)
303 struct lm3533_led
*led
= to_lm3533_led(cdev
);
306 dev_dbg(led
->cdev
.dev
, "%s - on = %lu, off = %lu\n", __func__
,
307 *delay_on
, *delay_off
);
309 if (*delay_on
> LM3533_LED_DELAY_ON_MAX
||
310 *delay_off
> LM3533_LED_DELAY_OFF_MAX
)
313 if (*delay_on
== 0 && *delay_off
== 0) {
318 ret
= lm3533_led_delay_on_set(led
, delay_on
);
322 ret
= lm3533_led_delay_off_set(led
, delay_off
);
326 return lm3533_led_pattern_enable(led
, 1);
329 static ssize_t
show_id(struct device
*dev
,
330 struct device_attribute
*attr
, char *buf
)
332 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
333 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
335 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->id
);
339 * Pattern generator rise/fall times:
341 * 0 - 2048 us (default)
350 static ssize_t
show_risefalltime(struct device
*dev
,
351 struct device_attribute
*attr
,
354 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
355 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
360 reg
= lm3533_led_get_pattern_reg(led
, base
);
361 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
365 return scnprintf(buf
, PAGE_SIZE
, "%x\n", val
);
368 static ssize_t
show_risetime(struct device
*dev
,
369 struct device_attribute
*attr
, char *buf
)
371 return show_risefalltime(dev
, attr
, buf
,
372 LM3533_REG_PATTERN_RISETIME_BASE
);
375 static ssize_t
show_falltime(struct device
*dev
,
376 struct device_attribute
*attr
, char *buf
)
378 return show_risefalltime(dev
, attr
, buf
,
379 LM3533_REG_PATTERN_FALLTIME_BASE
);
382 static ssize_t
store_risefalltime(struct device
*dev
,
383 struct device_attribute
*attr
,
384 const char *buf
, size_t len
, u8 base
)
386 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
387 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
392 if (kstrtou8(buf
, 0, &val
) || val
> LM3533_RISEFALLTIME_MAX
)
395 reg
= lm3533_led_get_pattern_reg(led
, base
);
396 ret
= lm3533_write(led
->lm3533
, reg
, val
);
403 static ssize_t
store_risetime(struct device
*dev
,
404 struct device_attribute
*attr
,
405 const char *buf
, size_t len
)
407 return store_risefalltime(dev
, attr
, buf
, len
,
408 LM3533_REG_PATTERN_RISETIME_BASE
);
411 static ssize_t
store_falltime(struct device
*dev
,
412 struct device_attribute
*attr
,
413 const char *buf
, size_t len
)
415 return store_risefalltime(dev
, attr
, buf
, len
,
416 LM3533_REG_PATTERN_FALLTIME_BASE
);
419 static ssize_t
show_als_channel(struct device
*dev
,
420 struct device_attribute
*attr
, char *buf
)
422 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
423 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
429 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
430 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
434 channel
= (val
& LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
) + 1;
436 return scnprintf(buf
, PAGE_SIZE
, "%u\n", channel
);
439 static ssize_t
store_als_channel(struct device
*dev
,
440 struct device_attribute
*attr
,
441 const char *buf
, size_t len
)
443 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
444 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
451 if (kstrtouint(buf
, 0, &channel
))
454 if (channel
< LM3533_ALS_CHANNEL_LV_MIN
||
455 channel
> LM3533_ALS_CHANNEL_LV_MAX
)
458 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
459 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
;
462 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
469 static ssize_t
show_als_en(struct device
*dev
,
470 struct device_attribute
*attr
, char *buf
)
472 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
473 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
479 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
480 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
484 enable
= val
& LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
486 return scnprintf(buf
, PAGE_SIZE
, "%d\n", enable
);
489 static ssize_t
store_als_en(struct device
*dev
,
490 struct device_attribute
*attr
,
491 const char *buf
, size_t len
)
493 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
494 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
501 if (kstrtouint(buf
, 0, &enable
))
504 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
505 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
512 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
519 static ssize_t
show_linear(struct device
*dev
,
520 struct device_attribute
*attr
, char *buf
)
522 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
523 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
529 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
530 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
534 if (val
& LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
)
539 return scnprintf(buf
, PAGE_SIZE
, "%x\n", linear
);
542 static ssize_t
store_linear(struct device
*dev
,
543 struct device_attribute
*attr
,
544 const char *buf
, size_t len
)
546 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
547 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
548 unsigned long linear
;
554 if (kstrtoul(buf
, 0, &linear
))
557 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
558 mask
= LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
;
565 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
572 static ssize_t
show_pwm(struct device
*dev
,
573 struct device_attribute
*attr
,
576 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
577 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
581 ret
= lm3533_ctrlbank_get_pwm(&led
->cb
, &val
);
585 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
588 static ssize_t
store_pwm(struct device
*dev
,
589 struct device_attribute
*attr
,
590 const char *buf
, size_t len
)
592 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
593 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
597 if (kstrtou8(buf
, 0, &val
))
600 ret
= lm3533_ctrlbank_set_pwm(&led
->cb
, val
);
607 static LM3533_ATTR_RW(als_channel
);
608 static LM3533_ATTR_RW(als_en
);
609 static LM3533_ATTR_RW(falltime
);
610 static LM3533_ATTR_RO(id
);
611 static LM3533_ATTR_RW(linear
);
612 static LM3533_ATTR_RW(pwm
);
613 static LM3533_ATTR_RW(risetime
);
615 static struct attribute
*lm3533_led_attributes
[] = {
616 &dev_attr_als_channel
.attr
,
617 &dev_attr_als_en
.attr
,
618 &dev_attr_falltime
.attr
,
620 &dev_attr_linear
.attr
,
622 &dev_attr_risetime
.attr
,
626 static umode_t
lm3533_led_attr_is_visible(struct kobject
*kobj
,
627 struct attribute
*attr
, int n
)
629 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
630 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
631 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
632 umode_t mode
= attr
->mode
;
634 if (attr
== &dev_attr_als_channel
.attr
||
635 attr
== &dev_attr_als_en
.attr
) {
636 if (!led
->lm3533
->have_als
)
643 static struct attribute_group lm3533_led_attribute_group
= {
644 .is_visible
= lm3533_led_attr_is_visible
,
645 .attrs
= lm3533_led_attributes
648 static const struct attribute_group
*lm3533_led_attribute_groups
[] = {
649 &lm3533_led_attribute_group
,
653 static int lm3533_led_setup(struct lm3533_led
*led
,
654 struct lm3533_led_platform_data
*pdata
)
658 ret
= lm3533_ctrlbank_set_max_current(&led
->cb
, pdata
->max_current
);
662 return lm3533_ctrlbank_set_pwm(&led
->cb
, pdata
->pwm
);
665 static int lm3533_led_probe(struct platform_device
*pdev
)
667 struct lm3533
*lm3533
;
668 struct lm3533_led_platform_data
*pdata
;
669 struct lm3533_led
*led
;
672 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
674 lm3533
= dev_get_drvdata(pdev
->dev
.parent
);
678 pdata
= dev_get_platdata(&pdev
->dev
);
680 dev_err(&pdev
->dev
, "no platform data\n");
684 if (pdev
->id
< 0 || pdev
->id
>= LM3533_LVCTRLBANK_COUNT
) {
685 dev_err(&pdev
->dev
, "illegal LED id %d\n", pdev
->id
);
689 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
693 led
->lm3533
= lm3533
;
694 led
->cdev
.name
= pdata
->name
;
695 led
->cdev
.default_trigger
= pdata
->default_trigger
;
696 led
->cdev
.brightness_set
= lm3533_led_set
;
697 led
->cdev
.brightness_get
= lm3533_led_get
;
698 led
->cdev
.blink_set
= lm3533_led_blink_set
;
699 led
->cdev
.brightness
= LED_OFF
;
700 led
->cdev
.groups
= lm3533_led_attribute_groups
,
703 mutex_init(&led
->mutex
);
704 INIT_WORK(&led
->work
, lm3533_led_work
);
706 /* The class framework makes a callback to get brightness during
707 * registration so use parent device (for error reporting) until
710 led
->cb
.lm3533
= lm3533
;
711 led
->cb
.id
= lm3533_led_get_ctrlbank_id(led
);
712 led
->cb
.dev
= lm3533
->dev
;
714 platform_set_drvdata(pdev
, led
);
716 ret
= led_classdev_register(pdev
->dev
.parent
, &led
->cdev
);
718 dev_err(&pdev
->dev
, "failed to register LED %d\n", pdev
->id
);
722 led
->cb
.dev
= led
->cdev
.dev
;
724 ret
= lm3533_led_setup(led
, pdata
);
728 ret
= lm3533_ctrlbank_enable(&led
->cb
);
735 led_classdev_unregister(&led
->cdev
);
736 flush_work(&led
->work
);
741 static int lm3533_led_remove(struct platform_device
*pdev
)
743 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
745 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
747 lm3533_ctrlbank_disable(&led
->cb
);
748 led_classdev_unregister(&led
->cdev
);
749 flush_work(&led
->work
);
754 static void lm3533_led_shutdown(struct platform_device
*pdev
)
757 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
759 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
761 lm3533_ctrlbank_disable(&led
->cb
);
762 lm3533_led_set(&led
->cdev
, LED_OFF
); /* disable blink */
763 flush_work(&led
->work
);
766 static struct platform_driver lm3533_led_driver
= {
768 .name
= "lm3533-leds",
769 .owner
= THIS_MODULE
,
771 .probe
= lm3533_led_probe
,
772 .remove
= lm3533_led_remove
,
773 .shutdown
= lm3533_led_shutdown
,
775 module_platform_driver(lm3533_led_driver
);
777 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
778 MODULE_DESCRIPTION("LM3533 LED driver");
779 MODULE_LICENSE("GPL");
780 MODULE_ALIAS("platform:lm3533-leds");