2 * Driver for BCM6328 memory-mapped LEDs, based on leds-syscon.c
4 * Copyright 2015 Álvaro Fernández Rojas <noltari@gmail.com>
5 * Copyright 2015 Jonas Gorski <jogo@openwrt.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/leds.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/spinlock.h>
19 #define BCM6328_REG_INIT 0x00
20 #define BCM6328_REG_MODE_HI 0x04
21 #define BCM6328_REG_MODE_LO 0x08
22 #define BCM6328_REG_HWDIS 0x0c
23 #define BCM6328_REG_STROBE 0x10
24 #define BCM6328_REG_LNKACTSEL_HI 0x14
25 #define BCM6328_REG_LNKACTSEL_LO 0x18
26 #define BCM6328_REG_RBACK 0x1c
27 #define BCM6328_REG_SERMUX 0x20
29 #define BCM6328_LED_MAX_COUNT 24
30 #define BCM6328_LED_DEF_DELAY 500
31 #define BCM6328_LED_INTERVAL_MS 20
33 #define BCM6328_LED_INTV_MASK 0x3f
34 #define BCM6328_LED_FAST_INTV_SHIFT 6
35 #define BCM6328_LED_FAST_INTV_MASK (BCM6328_LED_INTV_MASK << \
36 BCM6328_LED_FAST_INTV_SHIFT)
37 #define BCM6328_SERIAL_LED_EN BIT(12)
38 #define BCM6328_SERIAL_LED_MUX BIT(13)
39 #define BCM6328_SERIAL_LED_CLK_NPOL BIT(14)
40 #define BCM6328_SERIAL_LED_DATA_PPOL BIT(15)
41 #define BCM6328_SERIAL_LED_SHIFT_DIR BIT(16)
42 #define BCM6328_LED_SHIFT_TEST BIT(30)
43 #define BCM6328_LED_TEST BIT(31)
44 #define BCM6328_INIT_MASK (BCM6328_SERIAL_LED_EN | \
45 BCM6328_SERIAL_LED_MUX | \
46 BCM6328_SERIAL_LED_CLK_NPOL | \
47 BCM6328_SERIAL_LED_DATA_PPOL | \
48 BCM6328_SERIAL_LED_SHIFT_DIR)
50 #define BCM6328_LED_MODE_MASK 3
51 #define BCM6328_LED_MODE_OFF 0
52 #define BCM6328_LED_MODE_FAST 1
53 #define BCM6328_LED_MODE_BLINK 2
54 #define BCM6328_LED_MODE_ON 3
55 #define BCM6328_LED_SHIFT(X) ((X) << 1)
58 * struct bcm6328_led - state container for bcm6328 based LEDs
59 * @cdev: LED class device for this LED
60 * @mem: memory resource
62 * @pin: LED pin number
63 * @blink_leds: blinking LEDs
64 * @blink_delay: blinking delay
65 * @active_low: LED is active low
68 struct led_classdev cdev
;
72 unsigned long *blink_leds
;
73 unsigned long *blink_delay
;
77 static void bcm6328_led_write(void __iomem
*reg
, unsigned long data
)
79 iowrite32be(data
, reg
);
82 static unsigned long bcm6328_led_read(void __iomem
*reg
)
84 return ioread32be(reg
);
88 * LEDMode 64 bits / 24 LEDs
89 * bits [31:0] -> LEDs 8-23
90 * bits [47:32] -> LEDs 0-7
91 * bits [63:48] -> unused
93 static unsigned long bcm6328_pin2shift(unsigned long pin
)
96 return pin
+ 16; /* LEDs 0-7 (bits 47:32) */
98 return pin
- 8; /* LEDs 8-23 (bits 31:0) */
101 static void bcm6328_led_mode(struct bcm6328_led
*led
, unsigned long value
)
104 unsigned long val
, shift
;
106 shift
= bcm6328_pin2shift(led
->pin
);
108 mode
= led
->mem
+ BCM6328_REG_MODE_HI
;
110 mode
= led
->mem
+ BCM6328_REG_MODE_LO
;
112 val
= bcm6328_led_read(mode
);
113 val
&= ~(BCM6328_LED_MODE_MASK
<< BCM6328_LED_SHIFT(shift
% 16));
114 val
|= (value
<< BCM6328_LED_SHIFT(shift
% 16));
115 bcm6328_led_write(mode
, val
);
118 static void bcm6328_led_set(struct led_classdev
*led_cdev
,
119 enum led_brightness value
)
121 struct bcm6328_led
*led
=
122 container_of(led_cdev
, struct bcm6328_led
, cdev
);
125 spin_lock_irqsave(led
->lock
, flags
);
126 *(led
->blink_leds
) &= ~BIT(led
->pin
);
127 if ((led
->active_low
&& value
== LED_OFF
) ||
128 (!led
->active_low
&& value
!= LED_OFF
))
129 bcm6328_led_mode(led
, BCM6328_LED_MODE_OFF
);
131 bcm6328_led_mode(led
, BCM6328_LED_MODE_ON
);
132 spin_unlock_irqrestore(led
->lock
, flags
);
135 static int bcm6328_blink_set(struct led_classdev
*led_cdev
,
136 unsigned long *delay_on
, unsigned long *delay_off
)
138 struct bcm6328_led
*led
=
139 container_of(led_cdev
, struct bcm6328_led
, cdev
);
140 unsigned long delay
, flags
;
143 *delay_on
= BCM6328_LED_DEF_DELAY
;
145 *delay_off
= BCM6328_LED_DEF_DELAY
;
147 if (*delay_on
!= *delay_off
) {
148 dev_dbg(led_cdev
->dev
,
149 "fallback to soft blinking (delay_on != delay_off)\n");
153 delay
= *delay_on
/ BCM6328_LED_INTERVAL_MS
;
156 else if (delay
> BCM6328_LED_INTV_MASK
) {
157 dev_dbg(led_cdev
->dev
,
158 "fallback to soft blinking (delay > %ums)\n",
159 BCM6328_LED_INTV_MASK
* BCM6328_LED_INTERVAL_MS
);
163 spin_lock_irqsave(led
->lock
, flags
);
164 if (*(led
->blink_leds
) == 0 ||
165 *(led
->blink_leds
) == BIT(led
->pin
) ||
166 *(led
->blink_delay
) == delay
) {
169 *(led
->blink_leds
) |= BIT(led
->pin
);
170 *(led
->blink_delay
) = delay
;
172 val
= bcm6328_led_read(led
->mem
+ BCM6328_REG_INIT
);
173 val
&= ~BCM6328_LED_FAST_INTV_MASK
;
174 val
|= (delay
<< BCM6328_LED_FAST_INTV_SHIFT
);
175 bcm6328_led_write(led
->mem
+ BCM6328_REG_INIT
, val
);
177 bcm6328_led_mode(led
, BCM6328_LED_MODE_BLINK
);
179 spin_unlock_irqrestore(led
->lock
, flags
);
181 spin_unlock_irqrestore(led
->lock
, flags
);
182 dev_dbg(led_cdev
->dev
,
183 "fallback to soft blinking (delay already set)\n");
190 static int bcm6328_hwled(struct device
*dev
, struct device_node
*nc
, u32 reg
,
191 void __iomem
*mem
, spinlock_t
*lock
)
194 unsigned long flags
, val
;
196 spin_lock_irqsave(lock
, flags
);
197 val
= bcm6328_led_read(mem
+ BCM6328_REG_HWDIS
);
199 bcm6328_led_write(mem
+ BCM6328_REG_HWDIS
, val
);
200 spin_unlock_irqrestore(lock
, flags
);
202 /* Only LEDs 0-7 can be activity/link controlled */
206 cnt
= of_property_count_elems_of_size(nc
, "brcm,link-signal-sources",
208 for (i
= 0; i
< cnt
; i
++) {
213 addr
= mem
+ BCM6328_REG_LNKACTSEL_LO
;
215 addr
= mem
+ BCM6328_REG_LNKACTSEL_HI
;
217 of_property_read_u32_index(nc
, "brcm,link-signal-sources", i
,
220 if (reg
/ 4 != sel
/ 4) {
221 dev_warn(dev
, "invalid link signal source\n");
225 spin_lock_irqsave(lock
, flags
);
226 val
= bcm6328_led_read(addr
);
227 val
|= (BIT(reg
) << (((sel
% 4) * 4) + 16));
228 bcm6328_led_write(addr
, val
);
229 spin_unlock_irqrestore(lock
, flags
);
232 cnt
= of_property_count_elems_of_size(nc
,
233 "brcm,activity-signal-sources",
235 for (i
= 0; i
< cnt
; i
++) {
240 addr
= mem
+ BCM6328_REG_LNKACTSEL_LO
;
242 addr
= mem
+ BCM6328_REG_LNKACTSEL_HI
;
244 of_property_read_u32_index(nc
, "brcm,activity-signal-sources",
247 if (reg
/ 4 != sel
/ 4) {
248 dev_warn(dev
, "invalid activity signal source\n");
252 spin_lock_irqsave(lock
, flags
);
253 val
= bcm6328_led_read(addr
);
254 val
|= (BIT(reg
) << ((sel
% 4) * 4));
255 bcm6328_led_write(addr
, val
);
256 spin_unlock_irqrestore(lock
, flags
);
262 static int bcm6328_led(struct device
*dev
, struct device_node
*nc
, u32 reg
,
263 void __iomem
*mem
, spinlock_t
*lock
,
264 unsigned long *blink_leds
, unsigned long *blink_delay
)
266 struct bcm6328_led
*led
;
271 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
278 led
->blink_leds
= blink_leds
;
279 led
->blink_delay
= blink_delay
;
281 if (of_property_read_bool(nc
, "active-low"))
282 led
->active_low
= true;
284 led
->cdev
.name
= of_get_property(nc
, "label", NULL
) ? : nc
->name
;
285 led
->cdev
.default_trigger
= of_get_property(nc
,
286 "linux,default-trigger",
289 spin_lock_irqsave(lock
, flags
);
290 if (!of_property_read_string(nc
, "default-state", &state
)) {
291 if (!strcmp(state
, "on")) {
292 led
->cdev
.brightness
= LED_FULL
;
293 } else if (!strcmp(state
, "keep")) {
295 unsigned long val
, shift
;
297 shift
= bcm6328_pin2shift(led
->pin
);
299 mode
= mem
+ BCM6328_REG_MODE_HI
;
301 mode
= mem
+ BCM6328_REG_MODE_LO
;
303 val
= bcm6328_led_read(mode
) >>
304 BCM6328_LED_SHIFT(shift
% 16);
305 val
&= BCM6328_LED_MODE_MASK
;
306 if ((led
->active_low
&& val
== BCM6328_LED_MODE_ON
) ||
307 (!led
->active_low
&& val
== BCM6328_LED_MODE_OFF
))
308 led
->cdev
.brightness
= LED_FULL
;
310 led
->cdev
.brightness
= LED_OFF
;
312 led
->cdev
.brightness
= LED_OFF
;
315 led
->cdev
.brightness
= LED_OFF
;
318 if ((led
->active_low
&& led
->cdev
.brightness
== LED_FULL
) ||
319 (!led
->active_low
&& led
->cdev
.brightness
== LED_OFF
))
320 bcm6328_led_mode(led
, BCM6328_LED_MODE_ON
);
322 bcm6328_led_mode(led
, BCM6328_LED_MODE_OFF
);
323 spin_unlock_irqrestore(lock
, flags
);
325 led
->cdev
.brightness_set
= bcm6328_led_set
;
326 led
->cdev
.blink_set
= bcm6328_blink_set
;
328 rc
= led_classdev_register(dev
, &led
->cdev
);
332 dev_dbg(dev
, "registered LED %s\n", led
->cdev
.name
);
337 static int bcm6328_leds_probe(struct platform_device
*pdev
)
339 struct device
*dev
= &pdev
->dev
;
340 struct device_node
*np
= pdev
->dev
.of_node
;
341 struct device_node
*child
;
342 struct resource
*mem_r
;
345 unsigned long val
, *blink_leds
, *blink_delay
;
347 mem_r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
351 mem
= devm_ioremap_resource(dev
, mem_r
);
355 lock
= devm_kzalloc(dev
, sizeof(*lock
), GFP_KERNEL
);
359 blink_leds
= devm_kzalloc(dev
, sizeof(*blink_leds
), GFP_KERNEL
);
363 blink_delay
= devm_kzalloc(dev
, sizeof(*blink_delay
), GFP_KERNEL
);
367 spin_lock_init(lock
);
369 bcm6328_led_write(mem
+ BCM6328_REG_HWDIS
, ~0);
370 bcm6328_led_write(mem
+ BCM6328_REG_LNKACTSEL_HI
, 0);
371 bcm6328_led_write(mem
+ BCM6328_REG_LNKACTSEL_LO
, 0);
373 val
= bcm6328_led_read(mem
+ BCM6328_REG_INIT
);
374 val
&= ~(BCM6328_INIT_MASK
);
375 if (of_property_read_bool(np
, "brcm,serial-leds"))
376 val
|= BCM6328_SERIAL_LED_EN
;
377 if (of_property_read_bool(np
, "brcm,serial-mux"))
378 val
|= BCM6328_SERIAL_LED_MUX
;
379 if (of_property_read_bool(np
, "brcm,serial-clk-low"))
380 val
|= BCM6328_SERIAL_LED_CLK_NPOL
;
381 if (!of_property_read_bool(np
, "brcm,serial-dat-low"))
382 val
|= BCM6328_SERIAL_LED_DATA_PPOL
;
383 if (!of_property_read_bool(np
, "brcm,serial-shift-inv"))
384 val
|= BCM6328_SERIAL_LED_SHIFT_DIR
;
385 bcm6328_led_write(mem
+ BCM6328_REG_INIT
, val
);
387 for_each_available_child_of_node(np
, child
) {
391 if (of_property_read_u32(child
, "reg", ®
))
394 if (reg
>= BCM6328_LED_MAX_COUNT
) {
395 dev_err(dev
, "invalid LED (%u >= %d)\n", reg
,
396 BCM6328_LED_MAX_COUNT
);
400 if (of_property_read_bool(child
, "brcm,hardware-controlled"))
401 rc
= bcm6328_hwled(dev
, child
, reg
, mem
, lock
);
403 rc
= bcm6328_led(dev
, child
, reg
, mem
, lock
,
404 blink_leds
, blink_delay
);
415 static const struct of_device_id bcm6328_leds_of_match
[] = {
416 { .compatible
= "brcm,bcm6328-leds", },
419 MODULE_DEVICE_TABLE(of
, bcm6328_leds_of_match
);
421 static struct platform_driver bcm6328_leds_driver
= {
422 .probe
= bcm6328_leds_probe
,
424 .name
= "leds-bcm6328",
425 .of_match_table
= bcm6328_leds_of_match
,
429 module_platform_driver(bcm6328_leds_driver
);
431 MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
432 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
433 MODULE_DESCRIPTION("LED driver for BCM6328 controllers");
434 MODULE_LICENSE("GPL v2");
435 MODULE_ALIAS("platform:leds-bcm6328");