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/init.h>
16 #include <linux/leds.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
23 #include <linux/mfd/lm3533.h>
26 #define LM3533_LVCTRLBANK_MIN 2
27 #define LM3533_LVCTRLBANK_MAX 5
28 #define LM3533_LVCTRLBANK_COUNT 4
29 #define LM3533_RISEFALLTIME_MAX 7
30 #define LM3533_ALS_CHANNEL_LV_MIN 1
31 #define LM3533_ALS_CHANNEL_LV_MAX 2
33 #define LM3533_REG_CTRLBANK_BCONF_BASE 0x1b
34 #define LM3533_REG_PATTERN_ENABLE 0x28
35 #define LM3533_REG_PATTERN_LOW_TIME_BASE 0x71
36 #define LM3533_REG_PATTERN_HIGH_TIME_BASE 0x72
37 #define LM3533_REG_PATTERN_RISETIME_BASE 0x74
38 #define LM3533_REG_PATTERN_FALLTIME_BASE 0x75
40 #define LM3533_REG_PATTERN_STEP 0x10
42 #define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK 0x04
43 #define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK 0x02
44 #define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK 0x01
46 #define LM3533_LED_FLAG_PATTERN_ENABLE 1
50 struct lm3533
*lm3533
;
51 struct lm3533_ctrlbank cb
;
52 struct led_classdev cdev
;
58 struct work_struct work
;
63 static inline struct lm3533_led
*to_lm3533_led(struct led_classdev
*cdev
)
65 return container_of(cdev
, struct lm3533_led
, cdev
);
68 static inline int lm3533_led_get_ctrlbank_id(struct lm3533_led
*led
)
73 static inline u8
lm3533_led_get_lv_reg(struct lm3533_led
*led
, u8 base
)
75 return base
+ led
->id
;
78 static inline u8
lm3533_led_get_pattern(struct lm3533_led
*led
)
83 static inline u8
lm3533_led_get_pattern_reg(struct lm3533_led
*led
,
86 return base
+ lm3533_led_get_pattern(led
) * LM3533_REG_PATTERN_STEP
;
89 static int lm3533_led_pattern_enable(struct lm3533_led
*led
, int enable
)
97 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, enable
);
99 mutex_lock(&led
->mutex
);
101 state
= test_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
102 if ((enable
&& state
) || (!enable
&& !state
))
105 pattern
= lm3533_led_get_pattern(led
);
106 mask
= 1 << (2 * pattern
);
113 ret
= lm3533_update(led
->lm3533
, LM3533_REG_PATTERN_ENABLE
, val
, mask
);
115 dev_err(led
->cdev
.dev
, "failed to enable pattern %d (%d)\n",
120 __change_bit(LM3533_LED_FLAG_PATTERN_ENABLE
, &led
->flags
);
122 mutex_unlock(&led
->mutex
);
127 static void lm3533_led_work(struct work_struct
*work
)
129 struct lm3533_led
*led
= container_of(work
, struct lm3533_led
, work
);
131 dev_dbg(led
->cdev
.dev
, "%s - %u\n", __func__
, led
->new_brightness
);
133 if (led
->new_brightness
== 0)
134 lm3533_led_pattern_enable(led
, 0); /* disable blink */
136 lm3533_ctrlbank_set_brightness(&led
->cb
, led
->new_brightness
);
139 static void lm3533_led_set(struct led_classdev
*cdev
,
140 enum led_brightness value
)
142 struct lm3533_led
*led
= to_lm3533_led(cdev
);
144 dev_dbg(led
->cdev
.dev
, "%s - %d\n", __func__
, value
);
146 led
->new_brightness
= value
;
147 schedule_work(&led
->work
);
150 static enum led_brightness
lm3533_led_get(struct led_classdev
*cdev
)
152 struct lm3533_led
*led
= to_lm3533_led(cdev
);
156 ret
= lm3533_ctrlbank_get_brightness(&led
->cb
, &val
);
160 dev_dbg(led
->cdev
.dev
, "%s - %u\n", __func__
, val
);
165 /* Pattern generator defines (delays in us). */
166 #define LM3533_LED_DELAY1_VMIN 0x00
167 #define LM3533_LED_DELAY2_VMIN 0x3d
168 #define LM3533_LED_DELAY3_VMIN 0x80
170 #define LM3533_LED_DELAY1_VMAX (LM3533_LED_DELAY2_VMIN - 1)
171 #define LM3533_LED_DELAY2_VMAX (LM3533_LED_DELAY3_VMIN - 1)
172 #define LM3533_LED_DELAY3_VMAX 0xff
174 #define LM3533_LED_DELAY1_TMIN 16384U
175 #define LM3533_LED_DELAY2_TMIN 1130496U
176 #define LM3533_LED_DELAY3_TMIN 10305536U
178 #define LM3533_LED_DELAY1_TMAX 999424U
179 #define LM3533_LED_DELAY2_TMAX 9781248U
180 #define LM3533_LED_DELAY3_TMAX 76890112U
182 /* t_step = (t_max - t_min) / (v_max - v_min) */
183 #define LM3533_LED_DELAY1_TSTEP 16384
184 #define LM3533_LED_DELAY2_TSTEP 131072
185 #define LM3533_LED_DELAY3_TSTEP 524288
187 /* Delay limits for hardware accelerated blinking (in ms). */
188 #define LM3533_LED_DELAY_ON_MAX \
189 ((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000)
190 #define LM3533_LED_DELAY_OFF_MAX \
191 ((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000)
194 * Returns linear map of *t from [t_min,t_max] to [v_min,v_max] with a step
195 * size of t_step, where
197 * t_step = (t_max - t_min) / (v_max - v_min)
199 * and updates *t to reflect the mapped value.
201 static u8
time_to_val(unsigned *t
, unsigned t_min
, unsigned t_step
,
206 val
= (*t
+ t_step
/ 2 - t_min
) / t_step
+ v_min
;
208 *t
= t_step
* (val
- v_min
) + t_min
;
214 * Returns time code corresponding to *delay (in ms) and updates *delay to
215 * reflect actual hardware delay.
217 * Hardware supports 256 discrete delay times, divided into three groups with
218 * the following ranges and step-sizes:
220 * [ 16, 999] [0x00, 0x3e] step 16 ms
221 * [ 1130, 9781] [0x3d, 0x7f] step 131 ms
222 * [10306, 76890] [0x80, 0xff] step 524 ms
224 * Note that delay group 3 is only available for delay_off.
226 static u8
lm3533_led_get_hw_delay(unsigned *delay
)
233 if (t
>= (LM3533_LED_DELAY2_TMAX
+ LM3533_LED_DELAY3_TMIN
) / 2) {
234 t
= clamp(t
, LM3533_LED_DELAY3_TMIN
, LM3533_LED_DELAY3_TMAX
);
235 val
= time_to_val(&t
, LM3533_LED_DELAY3_TMIN
,
236 LM3533_LED_DELAY3_TSTEP
,
237 LM3533_LED_DELAY3_VMIN
,
238 LM3533_LED_DELAY3_VMAX
);
239 } else if (t
>= (LM3533_LED_DELAY1_TMAX
+ LM3533_LED_DELAY2_TMIN
) / 2) {
240 t
= clamp(t
, LM3533_LED_DELAY2_TMIN
, LM3533_LED_DELAY2_TMAX
);
241 val
= time_to_val(&t
, LM3533_LED_DELAY2_TMIN
,
242 LM3533_LED_DELAY2_TSTEP
,
243 LM3533_LED_DELAY2_VMIN
,
244 LM3533_LED_DELAY2_VMAX
);
246 t
= clamp(t
, LM3533_LED_DELAY1_TMIN
, LM3533_LED_DELAY1_TMAX
);
247 val
= time_to_val(&t
, LM3533_LED_DELAY1_TMIN
,
248 LM3533_LED_DELAY1_TSTEP
,
249 LM3533_LED_DELAY1_VMIN
,
250 LM3533_LED_DELAY1_VMAX
);
253 *delay
= (t
+ 500) / 1000;
259 * Set delay register base to *delay (in ms) and update *delay to reflect
260 * actual hardware delay used.
262 static u8
lm3533_led_delay_set(struct lm3533_led
*led
, u8 base
,
263 unsigned long *delay
)
270 t
= (unsigned)*delay
;
272 /* Delay group 3 is only available for low time (delay off). */
273 if (base
!= LM3533_REG_PATTERN_LOW_TIME_BASE
)
274 t
= min(t
, LM3533_LED_DELAY2_TMAX
/ 1000);
276 val
= lm3533_led_get_hw_delay(&t
);
278 dev_dbg(led
->cdev
.dev
, "%s - %lu: %u (0x%02x)\n", __func__
,
280 reg
= lm3533_led_get_pattern_reg(led
, base
);
281 ret
= lm3533_write(led
->lm3533
, reg
, val
);
283 dev_err(led
->cdev
.dev
, "failed to set delay (%02x)\n", reg
);
290 static int lm3533_led_delay_on_set(struct lm3533_led
*led
, unsigned long *t
)
292 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_HIGH_TIME_BASE
, t
);
295 static int lm3533_led_delay_off_set(struct lm3533_led
*led
, unsigned long *t
)
297 return lm3533_led_delay_set(led
, LM3533_REG_PATTERN_LOW_TIME_BASE
, t
);
300 static int lm3533_led_blink_set(struct led_classdev
*cdev
,
301 unsigned long *delay_on
,
302 unsigned long *delay_off
)
304 struct lm3533_led
*led
= to_lm3533_led(cdev
);
307 dev_dbg(led
->cdev
.dev
, "%s - on = %lu, off = %lu\n", __func__
,
308 *delay_on
, *delay_off
);
310 if (*delay_on
> LM3533_LED_DELAY_ON_MAX
||
311 *delay_off
> LM3533_LED_DELAY_OFF_MAX
)
314 if (*delay_on
== 0 && *delay_off
== 0) {
319 ret
= lm3533_led_delay_on_set(led
, delay_on
);
323 ret
= lm3533_led_delay_off_set(led
, delay_off
);
327 return lm3533_led_pattern_enable(led
, 1);
330 static ssize_t
show_id(struct device
*dev
,
331 struct device_attribute
*attr
, char *buf
)
333 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
334 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
336 return scnprintf(buf
, PAGE_SIZE
, "%d\n", led
->id
);
340 * Pattern generator rise/fall times:
342 * 0 - 2048 us (default)
351 static ssize_t
show_risefalltime(struct device
*dev
,
352 struct device_attribute
*attr
,
355 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
356 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
361 reg
= lm3533_led_get_pattern_reg(led
, base
);
362 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
366 return scnprintf(buf
, PAGE_SIZE
, "%x\n", val
);
369 static ssize_t
show_risetime(struct device
*dev
,
370 struct device_attribute
*attr
, char *buf
)
372 return show_risefalltime(dev
, attr
, buf
,
373 LM3533_REG_PATTERN_RISETIME_BASE
);
376 static ssize_t
show_falltime(struct device
*dev
,
377 struct device_attribute
*attr
, char *buf
)
379 return show_risefalltime(dev
, attr
, buf
,
380 LM3533_REG_PATTERN_FALLTIME_BASE
);
383 static ssize_t
store_risefalltime(struct device
*dev
,
384 struct device_attribute
*attr
,
385 const char *buf
, size_t len
, u8 base
)
387 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
388 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
393 if (kstrtou8(buf
, 0, &val
) || val
> LM3533_RISEFALLTIME_MAX
)
396 reg
= lm3533_led_get_pattern_reg(led
, base
);
397 ret
= lm3533_write(led
->lm3533
, reg
, val
);
404 static ssize_t
store_risetime(struct device
*dev
,
405 struct device_attribute
*attr
,
406 const char *buf
, size_t len
)
408 return store_risefalltime(dev
, attr
, buf
, len
,
409 LM3533_REG_PATTERN_RISETIME_BASE
);
412 static ssize_t
store_falltime(struct device
*dev
,
413 struct device_attribute
*attr
,
414 const char *buf
, size_t len
)
416 return store_risefalltime(dev
, attr
, buf
, len
,
417 LM3533_REG_PATTERN_FALLTIME_BASE
);
420 static ssize_t
show_als_channel(struct device
*dev
,
421 struct device_attribute
*attr
, char *buf
)
423 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
424 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
430 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
431 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
435 channel
= (val
& LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
) + 1;
437 return scnprintf(buf
, PAGE_SIZE
, "%u\n", channel
);
440 static ssize_t
store_als_channel(struct device
*dev
,
441 struct device_attribute
*attr
,
442 const char *buf
, size_t len
)
444 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
445 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
452 if (kstrtouint(buf
, 0, &channel
))
455 if (channel
< LM3533_ALS_CHANNEL_LV_MIN
||
456 channel
> LM3533_ALS_CHANNEL_LV_MAX
)
459 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
460 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK
;
463 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
470 static ssize_t
show_als_en(struct device
*dev
,
471 struct device_attribute
*attr
, char *buf
)
473 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
474 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
480 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
481 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
485 enable
= val
& LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
487 return scnprintf(buf
, PAGE_SIZE
, "%d\n", enable
);
490 static ssize_t
store_als_en(struct device
*dev
,
491 struct device_attribute
*attr
,
492 const char *buf
, size_t len
)
494 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
495 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
502 if (kstrtouint(buf
, 0, &enable
))
505 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
506 mask
= LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK
;
513 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
520 static ssize_t
show_linear(struct device
*dev
,
521 struct device_attribute
*attr
, char *buf
)
523 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
524 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
530 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
531 ret
= lm3533_read(led
->lm3533
, reg
, &val
);
535 if (val
& LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
)
540 return scnprintf(buf
, PAGE_SIZE
, "%x\n", linear
);
543 static ssize_t
store_linear(struct device
*dev
,
544 struct device_attribute
*attr
,
545 const char *buf
, size_t len
)
547 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
548 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
549 unsigned long linear
;
555 if (kstrtoul(buf
, 0, &linear
))
558 reg
= lm3533_led_get_lv_reg(led
, LM3533_REG_CTRLBANK_BCONF_BASE
);
559 mask
= LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK
;
566 ret
= lm3533_update(led
->lm3533
, reg
, val
, mask
);
573 static ssize_t
show_pwm(struct device
*dev
,
574 struct device_attribute
*attr
,
577 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
578 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
582 ret
= lm3533_ctrlbank_get_pwm(&led
->cb
, &val
);
586 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
589 static ssize_t
store_pwm(struct device
*dev
,
590 struct device_attribute
*attr
,
591 const char *buf
, size_t len
)
593 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
594 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
598 if (kstrtou8(buf
, 0, &val
))
601 ret
= lm3533_ctrlbank_set_pwm(&led
->cb
, val
);
608 static LM3533_ATTR_RW(als_channel
);
609 static LM3533_ATTR_RW(als_en
);
610 static LM3533_ATTR_RW(falltime
);
611 static LM3533_ATTR_RO(id
);
612 static LM3533_ATTR_RW(linear
);
613 static LM3533_ATTR_RW(pwm
);
614 static LM3533_ATTR_RW(risetime
);
616 static struct attribute
*lm3533_led_attributes
[] = {
617 &dev_attr_als_channel
.attr
,
618 &dev_attr_als_en
.attr
,
619 &dev_attr_falltime
.attr
,
621 &dev_attr_linear
.attr
,
623 &dev_attr_risetime
.attr
,
627 static umode_t
lm3533_led_attr_is_visible(struct kobject
*kobj
,
628 struct attribute
*attr
, int n
)
630 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
631 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
632 struct lm3533_led
*led
= to_lm3533_led(led_cdev
);
633 umode_t mode
= attr
->mode
;
635 if (attr
== &dev_attr_als_channel
.attr
||
636 attr
== &dev_attr_als_en
.attr
) {
637 if (!led
->lm3533
->have_als
)
644 static struct attribute_group lm3533_led_attribute_group
= {
645 .is_visible
= lm3533_led_attr_is_visible
,
646 .attrs
= lm3533_led_attributes
649 static int lm3533_led_setup(struct lm3533_led
*led
,
650 struct lm3533_led_platform_data
*pdata
)
654 ret
= lm3533_ctrlbank_set_max_current(&led
->cb
, pdata
->max_current
);
658 return lm3533_ctrlbank_set_pwm(&led
->cb
, pdata
->pwm
);
661 static int lm3533_led_probe(struct platform_device
*pdev
)
663 struct lm3533
*lm3533
;
664 struct lm3533_led_platform_data
*pdata
;
665 struct lm3533_led
*led
;
668 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
670 lm3533
= dev_get_drvdata(pdev
->dev
.parent
);
674 pdata
= dev_get_platdata(&pdev
->dev
);
676 dev_err(&pdev
->dev
, "no platform data\n");
680 if (pdev
->id
< 0 || pdev
->id
>= LM3533_LVCTRLBANK_COUNT
) {
681 dev_err(&pdev
->dev
, "illegal LED id %d\n", pdev
->id
);
685 led
= devm_kzalloc(&pdev
->dev
, sizeof(*led
), GFP_KERNEL
);
689 led
->lm3533
= lm3533
;
690 led
->cdev
.name
= pdata
->name
;
691 led
->cdev
.default_trigger
= pdata
->default_trigger
;
692 led
->cdev
.brightness_set
= lm3533_led_set
;
693 led
->cdev
.brightness_get
= lm3533_led_get
;
694 led
->cdev
.blink_set
= lm3533_led_blink_set
;
695 led
->cdev
.brightness
= LED_OFF
;
698 mutex_init(&led
->mutex
);
699 INIT_WORK(&led
->work
, lm3533_led_work
);
701 /* The class framework makes a callback to get brightness during
702 * registration so use parent device (for error reporting) until
705 led
->cb
.lm3533
= lm3533
;
706 led
->cb
.id
= lm3533_led_get_ctrlbank_id(led
);
707 led
->cb
.dev
= lm3533
->dev
;
709 platform_set_drvdata(pdev
, led
);
711 ret
= led_classdev_register(pdev
->dev
.parent
, &led
->cdev
);
713 dev_err(&pdev
->dev
, "failed to register LED %d\n", pdev
->id
);
717 led
->cb
.dev
= led
->cdev
.dev
;
719 ret
= sysfs_create_group(&led
->cdev
.dev
->kobj
,
720 &lm3533_led_attribute_group
);
722 dev_err(&pdev
->dev
, "failed to create sysfs attributes\n");
726 ret
= lm3533_led_setup(led
, pdata
);
728 goto err_sysfs_remove
;
730 ret
= lm3533_ctrlbank_enable(&led
->cb
);
732 goto err_sysfs_remove
;
737 sysfs_remove_group(&led
->cdev
.dev
->kobj
, &lm3533_led_attribute_group
);
739 led_classdev_unregister(&led
->cdev
);
740 flush_work(&led
->work
);
745 static int lm3533_led_remove(struct platform_device
*pdev
)
747 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
749 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
751 lm3533_ctrlbank_disable(&led
->cb
);
752 sysfs_remove_group(&led
->cdev
.dev
->kobj
, &lm3533_led_attribute_group
);
753 led_classdev_unregister(&led
->cdev
);
754 flush_work(&led
->work
);
759 static void lm3533_led_shutdown(struct platform_device
*pdev
)
762 struct lm3533_led
*led
= platform_get_drvdata(pdev
);
764 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
766 lm3533_ctrlbank_disable(&led
->cb
);
767 lm3533_led_set(&led
->cdev
, LED_OFF
); /* disable blink */
768 flush_work(&led
->work
);
771 static struct platform_driver lm3533_led_driver
= {
773 .name
= "lm3533-leds",
774 .owner
= THIS_MODULE
,
776 .probe
= lm3533_led_probe
,
777 .remove
= lm3533_led_remove
,
778 .shutdown
= lm3533_led_shutdown
,
780 module_platform_driver(lm3533_led_driver
);
782 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
783 MODULE_DESCRIPTION("LM3533 LED driver");
784 MODULE_LICENSE("GPL");
785 MODULE_ALIAS("platform:lm3533-leds");