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>
21 #include <linux/mfd/lm3533.h>
24 #define LM3533_LVCTRLBANK_MIN 2
25 #define LM3533_LVCTRLBANK_MAX 5
26 #define LM3533_LVCTRLBANK_COUNT 4
27 #define LM3533_RISEFALLTIME_MAX 7
28 #define LM3533_ALS_CHANNEL_LV_MIN 1
29 #define LM3533_ALS_CHANNEL_LV_MAX 2
31 #define LM3533_REG_CTRLBANK_BCONF_BASE 0x1b
32 #define LM3533_REG_PATTERN_ENABLE 0x28
33 #define LM3533_REG_PATTERN_LOW_TIME_BASE 0x71
34 #define LM3533_REG_PATTERN_HIGH_TIME_BASE 0x72
35 #define LM3533_REG_PATTERN_RISETIME_BASE 0x74
36 #define LM3533_REG_PATTERN_FALLTIME_BASE 0x75
38 #define LM3533_REG_PATTERN_STEP 0x10
40 #define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK 0x04
41 #define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK 0x02
42 #define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK 0x01
44 #define LM3533_LED_FLAG_PATTERN_ENABLE 1
48 struct lm3533
*lm3533
;
49 struct lm3533_ctrlbank cb
;
50 struct led_classdev cdev
;
58 static inline struct lm3533_led
*to_lm3533_led(struct led_classdev
*cdev
)
60 return container_of(cdev
, struct lm3533_led
, cdev
);
63 static inline int lm3533_led_get_ctrlbank_id(struct lm3533_led
*led
)
68 static inline u8
lm3533_led_get_lv_reg(struct lm3533_led
*led
, u8 base
)
70 return base
+ led
->id
;
73 static inline u8
lm3533_led_get_pattern(struct lm3533_led
*led
)
78 static inline u8
lm3533_led_get_pattern_reg(struct lm3533_led
*led
,
81 return base
+ lm3533_led_get_pattern(led
) * LM3533_REG_PATTERN_STEP
;
84 static int lm3533_led_pattern_enable(struct lm3533_led
*led
, int enable
)
92 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, enable
);
94 mutex_lock(&led
->mutex
);
96 state
= test_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
97 if ((enable
&& state
) || (!enable
&& !state
))
100 pattern
= lm3533_led_get_pattern(led
);
101 mask
= 1 << (2 * pattern
);
108 ret
= lm3533_update(led
->lm3533
, LM3533_REG_PATTERN_ENABLE
, val
, mask
);
110 dev_err(led
->cdev
.dev
, "failed to enable pattern %d (%d)\n",
115 __change_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
117 mutex_unlock(&led
->mutex
);
122 static int lm3533_led_set(struct led_classdev
*cdev
,
123 enum led_brightness value
)
125 struct lm3533_led
*led
= to_lm3533_led(cdev
);
127 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, value
);
130 lm3533_led_pattern_enable(led
, 0); /* disable blink */
132 return lm3533_ctrlbank_set_brightness(&led
->cb
, value
);
135 static enum led_brightness
lm3533_led_get(struct led_classdev
*cdev
)
137 struct lm3533_led
*led
= to_lm3533_led(cdev
);
141 ret
= lm3533_ctrlbank_get_brightness(&led
->cb
, &val
);
145 dev_dbg(led
->cdev
.dev
, "%s - %u\n", __func__
, val
);
150 /* Pattern generator defines (delays in us). */
151 #define LM3533_LED_DELAY1_VMIN 0x00
152 #define LM3533_LED_DELAY2_VMIN 0x3d
153 #define LM3533_LED_DELAY3_VMIN 0x80
155 #define LM3533_LED_DELAY1_VMAX (LM3533_LED_DELAY2_VMIN - 1)
156 #define LM3533_LED_DELAY2_VMAX (LM3533_LED_DELAY3_VMIN - 1)
157 #define LM3533_LED_DELAY3_VMAX 0xff
159 #define LM3533_LED_DELAY1_TMIN 16384U
160 #define LM3533_LED_DELAY2_TMIN 1130496U
161 #define LM3533_LED_DELAY3_TMIN 10305536U
163 #define LM3533_LED_DELAY1_TMAX 999424U
164 #define LM3533_LED_DELAY2_TMAX 9781248U
165 #define LM3533_LED_DELAY3_TMAX 76890112U
167 /* t_step = (t_max - t_min) / (v_max - v_min) */
168 #define LM3533_LED_DELAY1_TSTEP 16384
169 #define LM3533_LED_DELAY2_TSTEP 131072
170 #define LM3533_LED_DELAY3_TSTEP 524288
172 /* Delay limits for hardware accelerated blinking (in ms). */
173 #define LM3533_LED_DELAY_ON_MAX \
174 ((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000)
175 #define LM3533_LED_DELAY_OFF_MAX \
176 ((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000)
179 * Returns linear map of *t from [t_min,t_max] to [v_min,v_max] with a step
180 * size of t_step, where
182 * t_step = (t_max - t_min) / (v_max - v_min)
184 * and updates *t to reflect the mapped value.
186 static u8
time_to_val(unsigned *t
, unsigned t_min
, unsigned t_step
,
191 val
= (*t
+ t_step
/ 2 - t_min
) / t_step
+ v_min
;
193 *t
= t_step
* (val
- v_min
) + t_min
;
199 * Returns time code corresponding to *delay (in ms) and updates *delay to
200 * reflect actual hardware delay.
202 * Hardware supports 256 discrete delay times, divided into three groups with
203 * the following ranges and step-sizes:
205 * [ 16, 999] [0x00, 0x3e] step 16 ms
206 * [ 1130, 9781] [0x3d, 0x7f] step 131 ms
207 * [10306, 76890] [0x80, 0xff] step 524 ms
209 * Note that delay group 3 is only available for delay_off.
211 static u8
lm3533_led_get_hw_delay(unsigned *delay
)
218 if (t
>= (LM3533_LED_DELAY2_TMAX
+ LM3533_LED_DELAY3_TMIN
) / 2) {
219 t
= clamp(t
, LM3533_LED_DELAY3_TMIN
, LM3533_LED_DELAY3_TMAX
);
220 val
= time_to_val(&t
, LM3533_LED_DELAY3_TMIN
,
221 LM3533_LED_DELAY3_TSTEP
,
222 LM3533_LED_DELAY3_VMIN
,
223 LM3533_LED_DELAY3_VMAX
);
224 } else if (t
>= (LM3533_LED_DELAY1_TMAX
+ LM3533_LED_DELAY2_TMIN
) / 2) {
225 t
= clamp(t
, LM3533_LED_DELAY2_TMIN
, LM3533_LED_DELAY2_TMAX
);
226 val
= time_to_val(&t
, LM3533_LED_DELAY2_TMIN
,
227 LM3533_LED_DELAY2_TSTEP
,
228 LM3533_LED_DELAY2_VMIN
,
229 LM3533_LED_DELAY2_VMAX
);
231 t
= clamp(t
, LM3533_LED_DELAY1_TMIN
, LM3533_LED_DELAY1_TMAX
);
232 val
= time_to_val(&t
, LM3533_LED_DELAY1_TMIN
,
233 LM3533_LED_DELAY1_TSTEP
,
234 LM3533_LED_DELAY1_VMIN
,
235 LM3533_LED_DELAY1_VMAX
);
238 *delay
= (t
+ 500) / 1000;
244 * Set delay register base to *delay (in ms) and update *delay to reflect
245 * actual hardware delay used.
247 static u8
lm3533_led_delay_set(struct lm3533_led
*led
, u8 base
,
248 unsigned long *delay
)
255 t
= (unsigned)*delay
;
257 /* Delay group 3 is only available for low time (delay off). */
258 if (base
!= LM3533_REG_PATTERN_LOW_TIME_BASE
)
259 t
= min(t
, LM3533_LED_DELAY2_TMAX
/ 1000);
261 val
= lm3533_led_get_hw_delay(&t
);
263 dev_dbg(led
->cdev
.dev
, "%s - %lu: %u (0x%02x)\n", __func__
,
265 reg
= lm3533_led_get_pattern_reg(led
, base
);
266 ret
= lm3533_write(led
->lm3533
, reg
, val
);
268 dev_err(led
->cdev
.dev
, "failed to set delay (%02x)\n", reg
);
275 static int lm3533_led_delay_on_set(struct lm3533_led
*led
, unsigned long *t
)
277 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_HIGH_TIME_BASE
, t
);
280 static int lm3533_led_delay_off_set(struct lm3533_led
*led
, unsigned long *t
)
282 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_LOW_TIME_BASE
, t
);
285 static int lm3533_led_blink_set(struct led_classdev
*cdev
,
286 unsigned long *delay_on
,
287 unsigned long *delay_off
)
289 struct lm3533_led
*led
= to_lm3533_led(cdev
);
292 dev_dbg(led
->cdev
.dev
, "%s - on = %lu, off = %lu\n", __func__
,
293 *delay_on
, *delay_off
);
295 if (*delay_on
> LM3533_LED_DELAY_ON_MAX
||
296 *delay_off
> LM3533_LED_DELAY_OFF_MAX
)
299 if (*delay_on
== 0 && *delay_off
== 0) {
304 ret
= lm3533_led_delay_on_set(led
, delay_on
);
308 ret
= lm3533_led_delay_off_set(led
, delay_off
);
312 return lm3533_led_pattern_enable(led
, 1);
315 static ssize_t
show_id(struct device
*dev
,
316 struct device_attribute
*attr
, char *buf
)
318 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
319 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
321 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->id
);
325 * Pattern generator rise/fall times:
327 * 0 - 2048 us (default)
336 static ssize_t
show_risefalltime(struct device
*dev
,
337 struct device_attribute
*attr
,
340 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
341 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
346 reg
= lm3533_led_get_pattern_reg(led
, base
);
347 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
351 return scnprintf(buf
, PAGE_SIZE
, "%x\n", val
);
354 static ssize_t
show_risetime(struct device
*dev
,
355 struct device_attribute
*attr
, char *buf
)
357 return show_risefalltime(dev
, attr
, buf
,
358 LM3533_REG_PATTERN_RISETIME_BASE
);
361 static ssize_t
show_falltime(struct device
*dev
,
362 struct device_attribute
*attr
, char *buf
)
364 return show_risefalltime(dev
, attr
, buf
,
365 LM3533_REG_PATTERN_FALLTIME_BASE
);
368 static ssize_t
store_risefalltime(struct device
*dev
,
369 struct device_attribute
*attr
,
370 const char *buf
, size_t len
, u8 base
)
372 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
373 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
378 if (kstrtou8(buf
, 0, &val
) || val
> LM3533_RISEFALLTIME_MAX
)
381 reg
= lm3533_led_get_pattern_reg(led
, base
);
382 ret
= lm3533_write(led
->lm3533
, reg
, val
);
389 static ssize_t
store_risetime(struct device
*dev
,
390 struct device_attribute
*attr
,
391 const char *buf
, size_t len
)
393 return store_risefalltime(dev
, attr
, buf
, len
,
394 LM3533_REG_PATTERN_RISETIME_BASE
);
397 static ssize_t
store_falltime(struct device
*dev
,
398 struct device_attribute
*attr
,
399 const char *buf
, size_t len
)
401 return store_risefalltime(dev
, attr
, buf
, len
,
402 LM3533_REG_PATTERN_FALLTIME_BASE
);
405 static ssize_t
show_als_channel(struct device
*dev
,
406 struct device_attribute
*attr
, char *buf
)
408 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
409 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
415 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
416 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
420 channel
= (val
& LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
) + 1;
422 return scnprintf(buf
, PAGE_SIZE
, "%u\n", channel
);
425 static ssize_t
store_als_channel(struct device
*dev
,
426 struct device_attribute
*attr
,
427 const char *buf
, size_t len
)
429 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
430 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
437 if (kstrtouint(buf
, 0, &channel
))
440 if (channel
< LM3533_ALS_CHANNEL_LV_MIN
||
441 channel
> LM3533_ALS_CHANNEL_LV_MAX
)
444 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
445 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
;
448 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
455 static ssize_t
show_als_en(struct device
*dev
,
456 struct device_attribute
*attr
, char *buf
)
458 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
459 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
465 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
466 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
470 enable
= val
& LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
472 return scnprintf(buf
, PAGE_SIZE
, "%d\n", enable
);
475 static ssize_t
store_als_en(struct device
*dev
,
476 struct device_attribute
*attr
,
477 const char *buf
, size_t len
)
479 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
480 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
487 if (kstrtouint(buf
, 0, &enable
))
490 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
491 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
498 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
505 static ssize_t
show_linear(struct device
*dev
,
506 struct device_attribute
*attr
, char *buf
)
508 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
509 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
515 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
516 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
520 if (val
& LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
)
525 return scnprintf(buf
, PAGE_SIZE
, "%x\n", linear
);
528 static ssize_t
store_linear(struct device
*dev
,
529 struct device_attribute
*attr
,
530 const char *buf
, size_t len
)
532 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
533 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
534 unsigned long linear
;
540 if (kstrtoul(buf
, 0, &linear
))
543 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
544 mask
= LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
;
551 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
558 static ssize_t
show_pwm(struct device
*dev
,
559 struct device_attribute
*attr
,
562 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
563 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
567 ret
= lm3533_ctrlbank_get_pwm(&led
->cb
, &val
);
571 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
574 static ssize_t
store_pwm(struct device
*dev
,
575 struct device_attribute
*attr
,
576 const char *buf
, size_t len
)
578 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
579 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
583 if (kstrtou8(buf
, 0, &val
))
586 ret
= lm3533_ctrlbank_set_pwm(&led
->cb
, val
);
593 static LM3533_ATTR_RW(als_channel
);
594 static LM3533_ATTR_RW(als_en
);
595 static LM3533_ATTR_RW(falltime
);
596 static LM3533_ATTR_RO(id
);
597 static LM3533_ATTR_RW(linear
);
598 static LM3533_ATTR_RW(pwm
);
599 static LM3533_ATTR_RW(risetime
);
601 static struct attribute
*lm3533_led_attributes
[] = {
602 &dev_attr_als_channel
.attr
,
603 &dev_attr_als_en
.attr
,
604 &dev_attr_falltime
.attr
,
606 &dev_attr_linear
.attr
,
608 &dev_attr_risetime
.attr
,
612 static umode_t
lm3533_led_attr_is_visible(struct kobject
*kobj
,
613 struct attribute
*attr
, int n
)
615 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
616 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
617 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
618 umode_t mode
= attr
->mode
;
620 if (attr
== &dev_attr_als_channel
.attr
||
621 attr
== &dev_attr_als_en
.attr
) {
622 if (!led
->lm3533
->have_als
)
629 static struct attribute_group lm3533_led_attribute_group
= {
630 .is_visible
= lm3533_led_attr_is_visible
,
631 .attrs
= lm3533_led_attributes
634 static const struct attribute_group
*lm3533_led_attribute_groups
[] = {
635 &lm3533_led_attribute_group
,
639 static int lm3533_led_setup(struct lm3533_led
*led
,
640 struct lm3533_led_platform_data
*pdata
)
644 ret
= lm3533_ctrlbank_set_max_current(&led
->cb
, pdata
->max_current
);
648 return lm3533_ctrlbank_set_pwm(&led
->cb
, pdata
->pwm
);
651 static int lm3533_led_probe(struct platform_device
*pdev
)
653 struct lm3533
*lm3533
;
654 struct lm3533_led_platform_data
*pdata
;
655 struct lm3533_led
*led
;
658 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
660 lm3533
= dev_get_drvdata(pdev
->dev
.parent
);
664 pdata
= dev_get_platdata(&pdev
->dev
);
666 dev_err(&pdev
->dev
, "no platform data\n");
670 if (pdev
->id
< 0 || pdev
->id
>= LM3533_LVCTRLBANK_COUNT
) {
671 dev_err(&pdev
->dev
, "illegal LED id %d\n", pdev
->id
);
675 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
679 led
->lm3533
= lm3533
;
680 led
->cdev
.name
= pdata
->name
;
681 led
->cdev
.default_trigger
= pdata
->default_trigger
;
682 led
->cdev
.brightness_set_blocking
= lm3533_led_set
;
683 led
->cdev
.brightness_get
= lm3533_led_get
;
684 led
->cdev
.blink_set
= lm3533_led_blink_set
;
685 led
->cdev
.brightness
= LED_OFF
;
686 led
->cdev
.groups
= lm3533_led_attribute_groups
,
689 mutex_init(&led
->mutex
);
691 /* The class framework makes a callback to get brightness during
692 * registration so use parent device (for error reporting) until
695 led
->cb
.lm3533
= lm3533
;
696 led
->cb
.id
= lm3533_led_get_ctrlbank_id(led
);
697 led
->cb
.dev
= lm3533
->dev
;
699 platform_set_drvdata(pdev
, led
);
701 ret
= devm_led_classdev_register(pdev
->dev
.parent
, &led
->cdev
);
703 dev_err(&pdev
->dev
, "failed to register LED %d\n", pdev
->id
);
707 led
->cb
.dev
= led
->cdev
.dev
;
709 ret
= lm3533_led_setup(led
, pdata
);
713 ret
= lm3533_ctrlbank_enable(&led
->cb
);
720 static int lm3533_led_remove(struct platform_device
*pdev
)
722 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
724 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
726 lm3533_ctrlbank_disable(&led
->cb
);
731 static void lm3533_led_shutdown(struct platform_device
*pdev
)
734 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
736 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
738 lm3533_ctrlbank_disable(&led
->cb
);
739 lm3533_led_set(&led
->cdev
, LED_OFF
); /* disable blink */
742 static struct platform_driver lm3533_led_driver
= {
744 .name
= "lm3533-leds",
746 .probe
= lm3533_led_probe
,
747 .remove
= lm3533_led_remove
,
748 .shutdown
= lm3533_led_shutdown
,
750 module_platform_driver(lm3533_led_driver
);
752 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
753 MODULE_DESCRIPTION("LM3533 LED driver");
754 MODULE_LICENSE("GPL");
755 MODULE_ALIAS("platform:lm3533-leds");