1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * leds-lm3533.c -- LM3533 LED driver
5 * Copyright (C) 2011-2012 Texas Instruments
7 * Author: Johan Hovold <jhovold@gmail.com>
10 #include <linux/module.h>
11 #include <linux/leds.h>
12 #include <linux/mfd/core.h>
13 #include <linux/mutex.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
17 #include <linux/mfd/lm3533.h>
20 #define LM3533_LVCTRLBANK_MIN 2
21 #define LM3533_LVCTRLBANK_MAX 5
22 #define LM3533_LVCTRLBANK_COUNT 4
23 #define LM3533_RISEFALLTIME_MAX 7
24 #define LM3533_ALS_CHANNEL_LV_MIN 1
25 #define LM3533_ALS_CHANNEL_LV_MAX 2
27 #define LM3533_REG_CTRLBANK_BCONF_BASE 0x1b
28 #define LM3533_REG_PATTERN_ENABLE 0x28
29 #define LM3533_REG_PATTERN_LOW_TIME_BASE 0x71
30 #define LM3533_REG_PATTERN_HIGH_TIME_BASE 0x72
31 #define LM3533_REG_PATTERN_RISETIME_BASE 0x74
32 #define LM3533_REG_PATTERN_FALLTIME_BASE 0x75
34 #define LM3533_REG_PATTERN_STEP 0x10
36 #define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK 0x04
37 #define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK 0x02
38 #define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK 0x01
40 #define LM3533_LED_FLAG_PATTERN_ENABLE 1
44 struct lm3533
*lm3533
;
45 struct lm3533_ctrlbank cb
;
46 struct led_classdev cdev
;
54 static inline struct lm3533_led
*to_lm3533_led(struct led_classdev
*cdev
)
56 return container_of(cdev
, struct lm3533_led
, cdev
);
59 static inline int lm3533_led_get_ctrlbank_id(struct lm3533_led
*led
)
64 static inline u8
lm3533_led_get_lv_reg(struct lm3533_led
*led
, u8 base
)
66 return base
+ led
->id
;
69 static inline u8
lm3533_led_get_pattern(struct lm3533_led
*led
)
74 static inline u8
lm3533_led_get_pattern_reg(struct lm3533_led
*led
,
77 return base
+ lm3533_led_get_pattern(led
) * LM3533_REG_PATTERN_STEP
;
80 static int lm3533_led_pattern_enable(struct lm3533_led
*led
, int enable
)
88 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, enable
);
90 mutex_lock(&led
->mutex
);
92 state
= test_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
93 if ((enable
&& state
) || (!enable
&& !state
))
96 pattern
= lm3533_led_get_pattern(led
);
97 mask
= 1 << (2 * pattern
);
104 ret
= lm3533_update(led
->lm3533
, LM3533_REG_PATTERN_ENABLE
, val
, mask
);
106 dev_err(led
->cdev
.dev
, "failed to enable pattern %d (%d)\n",
111 __change_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
113 mutex_unlock(&led
->mutex
);
118 static int lm3533_led_set(struct led_classdev
*cdev
,
119 enum led_brightness value
)
121 struct lm3533_led
*led
= to_lm3533_led(cdev
);
123 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, value
);
126 lm3533_led_pattern_enable(led
, 0); /* disable blink */
128 return lm3533_ctrlbank_set_brightness(&led
->cb
, value
);
131 static enum led_brightness
lm3533_led_get(struct led_classdev
*cdev
)
133 struct lm3533_led
*led
= to_lm3533_led(cdev
);
137 ret
= lm3533_ctrlbank_get_brightness(&led
->cb
, &val
);
141 dev_dbg(led
->cdev
.dev
, "%s - %u\n", __func__
, val
);
146 /* Pattern generator defines (delays in us). */
147 #define LM3533_LED_DELAY1_VMIN 0x00
148 #define LM3533_LED_DELAY2_VMIN 0x3d
149 #define LM3533_LED_DELAY3_VMIN 0x80
151 #define LM3533_LED_DELAY1_VMAX (LM3533_LED_DELAY2_VMIN - 1)
152 #define LM3533_LED_DELAY2_VMAX (LM3533_LED_DELAY3_VMIN - 1)
153 #define LM3533_LED_DELAY3_VMAX 0xff
155 #define LM3533_LED_DELAY1_TMIN 16384U
156 #define LM3533_LED_DELAY2_TMIN 1130496U
157 #define LM3533_LED_DELAY3_TMIN 10305536U
159 #define LM3533_LED_DELAY1_TMAX 999424U
160 #define LM3533_LED_DELAY2_TMAX 9781248U
161 #define LM3533_LED_DELAY3_TMAX 76890112U
163 /* t_step = (t_max - t_min) / (v_max - v_min) */
164 #define LM3533_LED_DELAY1_TSTEP 16384
165 #define LM3533_LED_DELAY2_TSTEP 131072
166 #define LM3533_LED_DELAY3_TSTEP 524288
168 /* Delay limits for hardware accelerated blinking (in ms). */
169 #define LM3533_LED_DELAY_ON_MAX \
170 ((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000)
171 #define LM3533_LED_DELAY_OFF_MAX \
172 ((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000)
175 * Returns linear map of *t from [t_min,t_max] to [v_min,v_max] with a step
176 * size of t_step, where
178 * t_step = (t_max - t_min) / (v_max - v_min)
180 * and updates *t to reflect the mapped value.
182 static u8
time_to_val(unsigned *t
, unsigned t_min
, unsigned t_step
,
187 val
= (*t
+ t_step
/ 2 - t_min
) / t_step
+ v_min
;
189 *t
= t_step
* (val
- v_min
) + t_min
;
195 * Returns time code corresponding to *delay (in ms) and updates *delay to
196 * reflect actual hardware delay.
198 * Hardware supports 256 discrete delay times, divided into three groups with
199 * the following ranges and step-sizes:
201 * [ 16, 999] [0x00, 0x3e] step 16 ms
202 * [ 1130, 9781] [0x3d, 0x7f] step 131 ms
203 * [10306, 76890] [0x80, 0xff] step 524 ms
205 * Note that delay group 3 is only available for delay_off.
207 static u8
lm3533_led_get_hw_delay(unsigned *delay
)
214 if (t
>= (LM3533_LED_DELAY2_TMAX
+ LM3533_LED_DELAY3_TMIN
) / 2) {
215 t
= clamp(t
, LM3533_LED_DELAY3_TMIN
, LM3533_LED_DELAY3_TMAX
);
216 val
= time_to_val(&t
, LM3533_LED_DELAY3_TMIN
,
217 LM3533_LED_DELAY3_TSTEP
,
218 LM3533_LED_DELAY3_VMIN
,
219 LM3533_LED_DELAY3_VMAX
);
220 } else if (t
>= (LM3533_LED_DELAY1_TMAX
+ LM3533_LED_DELAY2_TMIN
) / 2) {
221 t
= clamp(t
, LM3533_LED_DELAY2_TMIN
, LM3533_LED_DELAY2_TMAX
);
222 val
= time_to_val(&t
, LM3533_LED_DELAY2_TMIN
,
223 LM3533_LED_DELAY2_TSTEP
,
224 LM3533_LED_DELAY2_VMIN
,
225 LM3533_LED_DELAY2_VMAX
);
227 t
= clamp(t
, LM3533_LED_DELAY1_TMIN
, LM3533_LED_DELAY1_TMAX
);
228 val
= time_to_val(&t
, LM3533_LED_DELAY1_TMIN
,
229 LM3533_LED_DELAY1_TSTEP
,
230 LM3533_LED_DELAY1_VMIN
,
231 LM3533_LED_DELAY1_VMAX
);
234 *delay
= (t
+ 500) / 1000;
240 * Set delay register base to *delay (in ms) and update *delay to reflect
241 * actual hardware delay used.
243 static u8
lm3533_led_delay_set(struct lm3533_led
*led
, u8 base
,
244 unsigned long *delay
)
251 t
= (unsigned)*delay
;
253 /* Delay group 3 is only available for low time (delay off). */
254 if (base
!= LM3533_REG_PATTERN_LOW_TIME_BASE
)
255 t
= min(t
, LM3533_LED_DELAY2_TMAX
/ 1000);
257 val
= lm3533_led_get_hw_delay(&t
);
259 dev_dbg(led
->cdev
.dev
, "%s - %lu: %u (0x%02x)\n", __func__
,
261 reg
= lm3533_led_get_pattern_reg(led
, base
);
262 ret
= lm3533_write(led
->lm3533
, reg
, val
);
264 dev_err(led
->cdev
.dev
, "failed to set delay (%02x)\n", reg
);
271 static int lm3533_led_delay_on_set(struct lm3533_led
*led
, unsigned long *t
)
273 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_HIGH_TIME_BASE
, t
);
276 static int lm3533_led_delay_off_set(struct lm3533_led
*led
, unsigned long *t
)
278 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_LOW_TIME_BASE
, t
);
281 static int lm3533_led_blink_set(struct led_classdev
*cdev
,
282 unsigned long *delay_on
,
283 unsigned long *delay_off
)
285 struct lm3533_led
*led
= to_lm3533_led(cdev
);
288 dev_dbg(led
->cdev
.dev
, "%s - on = %lu, off = %lu\n", __func__
,
289 *delay_on
, *delay_off
);
291 if (*delay_on
> LM3533_LED_DELAY_ON_MAX
||
292 *delay_off
> LM3533_LED_DELAY_OFF_MAX
)
295 if (*delay_on
== 0 && *delay_off
== 0) {
300 ret
= lm3533_led_delay_on_set(led
, delay_on
);
304 ret
= lm3533_led_delay_off_set(led
, delay_off
);
308 return lm3533_led_pattern_enable(led
, 1);
311 static ssize_t
show_id(struct device
*dev
,
312 struct device_attribute
*attr
, char *buf
)
314 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
315 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
317 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->id
);
321 * Pattern generator rise/fall times:
323 * 0 - 2048 us (default)
332 static ssize_t
show_risefalltime(struct device
*dev
,
333 struct device_attribute
*attr
,
336 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
337 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
342 reg
= lm3533_led_get_pattern_reg(led
, base
);
343 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
347 return scnprintf(buf
, PAGE_SIZE
, "%x\n", val
);
350 static ssize_t
show_risetime(struct device
*dev
,
351 struct device_attribute
*attr
, char *buf
)
353 return show_risefalltime(dev
, attr
, buf
,
354 LM3533_REG_PATTERN_RISETIME_BASE
);
357 static ssize_t
show_falltime(struct device
*dev
,
358 struct device_attribute
*attr
, char *buf
)
360 return show_risefalltime(dev
, attr
, buf
,
361 LM3533_REG_PATTERN_FALLTIME_BASE
);
364 static ssize_t
store_risefalltime(struct device
*dev
,
365 struct device_attribute
*attr
,
366 const char *buf
, size_t len
, u8 base
)
368 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
369 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
374 if (kstrtou8(buf
, 0, &val
) || val
> LM3533_RISEFALLTIME_MAX
)
377 reg
= lm3533_led_get_pattern_reg(led
, base
);
378 ret
= lm3533_write(led
->lm3533
, reg
, val
);
385 static ssize_t
store_risetime(struct device
*dev
,
386 struct device_attribute
*attr
,
387 const char *buf
, size_t len
)
389 return store_risefalltime(dev
, attr
, buf
, len
,
390 LM3533_REG_PATTERN_RISETIME_BASE
);
393 static ssize_t
store_falltime(struct device
*dev
,
394 struct device_attribute
*attr
,
395 const char *buf
, size_t len
)
397 return store_risefalltime(dev
, attr
, buf
, len
,
398 LM3533_REG_PATTERN_FALLTIME_BASE
);
401 static ssize_t
show_als_channel(struct device
*dev
,
402 struct device_attribute
*attr
, char *buf
)
404 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
405 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
411 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
412 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
416 channel
= (val
& LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
) + 1;
418 return scnprintf(buf
, PAGE_SIZE
, "%u\n", channel
);
421 static ssize_t
store_als_channel(struct device
*dev
,
422 struct device_attribute
*attr
,
423 const char *buf
, size_t len
)
425 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
426 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
433 if (kstrtouint(buf
, 0, &channel
))
436 if (channel
< LM3533_ALS_CHANNEL_LV_MIN
||
437 channel
> LM3533_ALS_CHANNEL_LV_MAX
)
440 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
441 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
;
444 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
451 static ssize_t
show_als_en(struct device
*dev
,
452 struct device_attribute
*attr
, char *buf
)
454 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
455 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
461 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
462 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
466 enable
= val
& LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
468 return scnprintf(buf
, PAGE_SIZE
, "%d\n", enable
);
471 static ssize_t
store_als_en(struct device
*dev
,
472 struct device_attribute
*attr
,
473 const char *buf
, size_t len
)
475 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
476 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
483 if (kstrtouint(buf
, 0, &enable
))
486 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
487 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
494 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
501 static ssize_t
show_linear(struct device
*dev
,
502 struct device_attribute
*attr
, char *buf
)
504 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
505 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
511 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
512 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
516 if (val
& LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
)
521 return scnprintf(buf
, PAGE_SIZE
, "%x\n", linear
);
524 static ssize_t
store_linear(struct device
*dev
,
525 struct device_attribute
*attr
,
526 const char *buf
, size_t len
)
528 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
529 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
530 unsigned long linear
;
536 if (kstrtoul(buf
, 0, &linear
))
539 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
540 mask
= LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
;
547 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
554 static ssize_t
show_pwm(struct device
*dev
,
555 struct device_attribute
*attr
,
558 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
559 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
563 ret
= lm3533_ctrlbank_get_pwm(&led
->cb
, &val
);
567 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
570 static ssize_t
store_pwm(struct device
*dev
,
571 struct device_attribute
*attr
,
572 const char *buf
, size_t len
)
574 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
575 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
579 if (kstrtou8(buf
, 0, &val
))
582 ret
= lm3533_ctrlbank_set_pwm(&led
->cb
, val
);
589 static LM3533_ATTR_RW(als_channel
);
590 static LM3533_ATTR_RW(als_en
);
591 static LM3533_ATTR_RW(falltime
);
592 static LM3533_ATTR_RO(id
);
593 static LM3533_ATTR_RW(linear
);
594 static LM3533_ATTR_RW(pwm
);
595 static LM3533_ATTR_RW(risetime
);
597 static struct attribute
*lm3533_led_attributes
[] = {
598 &dev_attr_als_channel
.attr
,
599 &dev_attr_als_en
.attr
,
600 &dev_attr_falltime
.attr
,
602 &dev_attr_linear
.attr
,
604 &dev_attr_risetime
.attr
,
608 static umode_t
lm3533_led_attr_is_visible(struct kobject
*kobj
,
609 struct attribute
*attr
, int n
)
611 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
612 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
613 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
614 umode_t mode
= attr
->mode
;
616 if (attr
== &dev_attr_als_channel
.attr
||
617 attr
== &dev_attr_als_en
.attr
) {
618 if (!led
->lm3533
->have_als
)
625 static const struct attribute_group lm3533_led_attribute_group
= {
626 .is_visible
= lm3533_led_attr_is_visible
,
627 .attrs
= lm3533_led_attributes
630 static const struct attribute_group
*lm3533_led_attribute_groups
[] = {
631 &lm3533_led_attribute_group
,
635 static int lm3533_led_setup(struct lm3533_led
*led
,
636 struct lm3533_led_platform_data
*pdata
)
640 ret
= lm3533_ctrlbank_set_max_current(&led
->cb
, pdata
->max_current
);
644 return lm3533_ctrlbank_set_pwm(&led
->cb
, pdata
->pwm
);
647 static int lm3533_led_probe(struct platform_device
*pdev
)
649 struct lm3533
*lm3533
;
650 struct lm3533_led_platform_data
*pdata
;
651 struct lm3533_led
*led
;
654 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
656 lm3533
= dev_get_drvdata(pdev
->dev
.parent
);
660 pdata
= dev_get_platdata(&pdev
->dev
);
662 dev_err(&pdev
->dev
, "no platform data\n");
666 if (pdev
->id
< 0 || pdev
->id
>= LM3533_LVCTRLBANK_COUNT
) {
667 dev_err(&pdev
->dev
, "illegal LED id %d\n", pdev
->id
);
671 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
675 led
->lm3533
= lm3533
;
676 led
->cdev
.name
= pdata
->name
;
677 led
->cdev
.default_trigger
= pdata
->default_trigger
;
678 led
->cdev
.brightness_set_blocking
= lm3533_led_set
;
679 led
->cdev
.brightness_get
= lm3533_led_get
;
680 led
->cdev
.blink_set
= lm3533_led_blink_set
;
681 led
->cdev
.brightness
= LED_OFF
;
682 led
->cdev
.groups
= lm3533_led_attribute_groups
,
685 mutex_init(&led
->mutex
);
687 /* The class framework makes a callback to get brightness during
688 * registration so use parent device (for error reporting) until
691 led
->cb
.lm3533
= lm3533
;
692 led
->cb
.id
= lm3533_led_get_ctrlbank_id(led
);
693 led
->cb
.dev
= lm3533
->dev
;
695 platform_set_drvdata(pdev
, led
);
697 ret
= led_classdev_register(pdev
->dev
.parent
, &led
->cdev
);
699 dev_err(&pdev
->dev
, "failed to register LED %d\n", pdev
->id
);
703 led
->cb
.dev
= led
->cdev
.dev
;
705 ret
= lm3533_led_setup(led
, pdata
);
709 ret
= lm3533_ctrlbank_enable(&led
->cb
);
716 led_classdev_unregister(&led
->cdev
);
721 static int lm3533_led_remove(struct platform_device
*pdev
)
723 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
725 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
727 lm3533_ctrlbank_disable(&led
->cb
);
728 led_classdev_unregister(&led
->cdev
);
733 static void lm3533_led_shutdown(struct platform_device
*pdev
)
736 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
738 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
740 lm3533_ctrlbank_disable(&led
->cb
);
741 lm3533_led_set(&led
->cdev
, LED_OFF
); /* disable blink */
744 static struct platform_driver lm3533_led_driver
= {
746 .name
= "lm3533-leds",
748 .probe
= lm3533_led_probe
,
749 .remove
= lm3533_led_remove
,
750 .shutdown
= lm3533_led_shutdown
,
752 module_platform_driver(lm3533_led_driver
);
754 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
755 MODULE_DESCRIPTION("LM3533 LED driver");
756 MODULE_LICENSE("GPL");
757 MODULE_ALIAS("platform:lm3533-leds");