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_ON 0
52 #define BCM6328_LED_MODE_FAST 1
53 #define BCM6328_LED_MODE_BLINK 2
54 #define BCM6328_LED_MODE_OFF 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 #ifdef CONFIG_CPU_BIG_ENDIAN
80 iowrite32be(data
, reg
);
86 static unsigned long bcm6328_led_read(void __iomem
*reg
)
88 #ifdef CONFIG_CPU_BIG_ENDIAN
89 return ioread32be(reg
);
96 * LEDMode 64 bits / 24 LEDs
97 * bits [31:0] -> LEDs 8-23
98 * bits [47:32] -> LEDs 0-7
99 * bits [63:48] -> unused
101 static unsigned long bcm6328_pin2shift(unsigned long pin
)
104 return pin
+ 16; /* LEDs 0-7 (bits 47:32) */
106 return pin
- 8; /* LEDs 8-23 (bits 31:0) */
109 static void bcm6328_led_mode(struct bcm6328_led
*led
, unsigned long value
)
112 unsigned long val
, shift
;
114 shift
= bcm6328_pin2shift(led
->pin
);
116 mode
= led
->mem
+ BCM6328_REG_MODE_HI
;
118 mode
= led
->mem
+ BCM6328_REG_MODE_LO
;
120 val
= bcm6328_led_read(mode
);
121 val
&= ~(BCM6328_LED_MODE_MASK
<< BCM6328_LED_SHIFT(shift
% 16));
122 val
|= (value
<< BCM6328_LED_SHIFT(shift
% 16));
123 bcm6328_led_write(mode
, val
);
126 static void bcm6328_led_set(struct led_classdev
*led_cdev
,
127 enum led_brightness value
)
129 struct bcm6328_led
*led
=
130 container_of(led_cdev
, struct bcm6328_led
, cdev
);
133 spin_lock_irqsave(led
->lock
, flags
);
134 *(led
->blink_leds
) &= ~BIT(led
->pin
);
135 if ((led
->active_low
&& value
== LED_OFF
) ||
136 (!led
->active_low
&& value
!= LED_OFF
))
137 bcm6328_led_mode(led
, BCM6328_LED_MODE_ON
);
139 bcm6328_led_mode(led
, BCM6328_LED_MODE_OFF
);
140 spin_unlock_irqrestore(led
->lock
, flags
);
143 static unsigned long bcm6328_blink_delay(unsigned long delay
)
145 unsigned long bcm6328_delay
;
147 bcm6328_delay
= delay
+ BCM6328_LED_INTERVAL_MS
/ 2;
148 bcm6328_delay
= bcm6328_delay
/ BCM6328_LED_INTERVAL_MS
;
149 if (bcm6328_delay
== 0)
152 return bcm6328_delay
;
155 static int bcm6328_blink_set(struct led_classdev
*led_cdev
,
156 unsigned long *delay_on
, unsigned long *delay_off
)
158 struct bcm6328_led
*led
=
159 container_of(led_cdev
, struct bcm6328_led
, cdev
);
160 unsigned long delay
, flags
;
164 *delay_on
= BCM6328_LED_DEF_DELAY
;
166 *delay_off
= BCM6328_LED_DEF_DELAY
;
168 delay
= bcm6328_blink_delay(*delay_on
);
169 if (delay
!= bcm6328_blink_delay(*delay_off
)) {
170 dev_dbg(led_cdev
->dev
,
171 "fallback to soft blinking (delay_on != delay_off)\n");
175 if (delay
> BCM6328_LED_INTV_MASK
) {
176 dev_dbg(led_cdev
->dev
,
177 "fallback to soft blinking (delay > %ums)\n",
178 BCM6328_LED_INTV_MASK
* BCM6328_LED_INTERVAL_MS
);
182 spin_lock_irqsave(led
->lock
, flags
);
183 if (*(led
->blink_leds
) == 0 ||
184 *(led
->blink_leds
) == BIT(led
->pin
) ||
185 *(led
->blink_delay
) == delay
) {
188 *(led
->blink_leds
) |= BIT(led
->pin
);
189 *(led
->blink_delay
) = delay
;
191 val
= bcm6328_led_read(led
->mem
+ BCM6328_REG_INIT
);
192 val
&= ~BCM6328_LED_FAST_INTV_MASK
;
193 val
|= (delay
<< BCM6328_LED_FAST_INTV_SHIFT
);
194 bcm6328_led_write(led
->mem
+ BCM6328_REG_INIT
, val
);
196 bcm6328_led_mode(led
, BCM6328_LED_MODE_BLINK
);
199 dev_dbg(led_cdev
->dev
,
200 "fallback to soft blinking (delay already set)\n");
203 spin_unlock_irqrestore(led
->lock
, flags
);
208 static int bcm6328_hwled(struct device
*dev
, struct device_node
*nc
, u32 reg
,
209 void __iomem
*mem
, spinlock_t
*lock
)
212 unsigned long flags
, val
;
214 spin_lock_irqsave(lock
, flags
);
215 val
= bcm6328_led_read(mem
+ BCM6328_REG_HWDIS
);
217 bcm6328_led_write(mem
+ BCM6328_REG_HWDIS
, val
);
218 spin_unlock_irqrestore(lock
, flags
);
220 /* Only LEDs 0-7 can be activity/link controlled */
224 cnt
= of_property_count_elems_of_size(nc
, "brcm,link-signal-sources",
226 for (i
= 0; i
< cnt
; i
++) {
231 addr
= mem
+ BCM6328_REG_LNKACTSEL_LO
;
233 addr
= mem
+ BCM6328_REG_LNKACTSEL_HI
;
235 of_property_read_u32_index(nc
, "brcm,link-signal-sources", i
,
238 if (reg
/ 4 != sel
/ 4) {
239 dev_warn(dev
, "invalid link signal source\n");
243 spin_lock_irqsave(lock
, flags
);
244 val
= bcm6328_led_read(addr
);
245 val
|= (BIT(reg
) << (((sel
% 4) * 4) + 16));
246 bcm6328_led_write(addr
, val
);
247 spin_unlock_irqrestore(lock
, flags
);
250 cnt
= of_property_count_elems_of_size(nc
,
251 "brcm,activity-signal-sources",
253 for (i
= 0; i
< cnt
; i
++) {
258 addr
= mem
+ BCM6328_REG_LNKACTSEL_LO
;
260 addr
= mem
+ BCM6328_REG_LNKACTSEL_HI
;
262 of_property_read_u32_index(nc
, "brcm,activity-signal-sources",
265 if (reg
/ 4 != sel
/ 4) {
266 dev_warn(dev
, "invalid activity signal source\n");
270 spin_lock_irqsave(lock
, flags
);
271 val
= bcm6328_led_read(addr
);
272 val
|= (BIT(reg
) << ((sel
% 4) * 4));
273 bcm6328_led_write(addr
, val
);
274 spin_unlock_irqrestore(lock
, flags
);
280 static int bcm6328_led(struct device
*dev
, struct device_node
*nc
, u32 reg
,
281 void __iomem
*mem
, spinlock_t
*lock
,
282 unsigned long *blink_leds
, unsigned long *blink_delay
)
284 struct bcm6328_led
*led
;
288 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
295 led
->blink_leds
= blink_leds
;
296 led
->blink_delay
= blink_delay
;
298 if (of_property_read_bool(nc
, "active-low"))
299 led
->active_low
= true;
301 led
->cdev
.name
= of_get_property(nc
, "label", NULL
) ? : nc
->name
;
302 led
->cdev
.default_trigger
= of_get_property(nc
,
303 "linux,default-trigger",
306 if (!of_property_read_string(nc
, "default-state", &state
)) {
307 if (!strcmp(state
, "on")) {
308 led
->cdev
.brightness
= LED_FULL
;
309 } else if (!strcmp(state
, "keep")) {
311 unsigned long val
, shift
;
313 shift
= bcm6328_pin2shift(led
->pin
);
315 mode
= mem
+ BCM6328_REG_MODE_HI
;
317 mode
= mem
+ BCM6328_REG_MODE_LO
;
319 val
= bcm6328_led_read(mode
) >>
320 BCM6328_LED_SHIFT(shift
% 16);
321 val
&= BCM6328_LED_MODE_MASK
;
322 if ((led
->active_low
&& val
== BCM6328_LED_MODE_OFF
) ||
323 (!led
->active_low
&& val
== BCM6328_LED_MODE_ON
))
324 led
->cdev
.brightness
= LED_FULL
;
326 led
->cdev
.brightness
= LED_OFF
;
328 led
->cdev
.brightness
= LED_OFF
;
331 led
->cdev
.brightness
= LED_OFF
;
334 bcm6328_led_set(&led
->cdev
, led
->cdev
.brightness
);
336 led
->cdev
.brightness_set
= bcm6328_led_set
;
337 led
->cdev
.blink_set
= bcm6328_blink_set
;
339 rc
= led_classdev_register(dev
, &led
->cdev
);
343 dev_dbg(dev
, "registered LED %s\n", led
->cdev
.name
);
348 static int bcm6328_leds_probe(struct platform_device
*pdev
)
350 struct device
*dev
= &pdev
->dev
;
351 struct device_node
*np
= pdev
->dev
.of_node
;
352 struct device_node
*child
;
353 struct resource
*mem_r
;
355 spinlock_t
*lock
; /* memory lock */
356 unsigned long val
, *blink_leds
, *blink_delay
;
358 mem_r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
362 mem
= devm_ioremap_resource(dev
, mem_r
);
366 lock
= devm_kzalloc(dev
, sizeof(*lock
), GFP_KERNEL
);
370 blink_leds
= devm_kzalloc(dev
, sizeof(*blink_leds
), GFP_KERNEL
);
374 blink_delay
= devm_kzalloc(dev
, sizeof(*blink_delay
), GFP_KERNEL
);
378 spin_lock_init(lock
);
380 bcm6328_led_write(mem
+ BCM6328_REG_HWDIS
, ~0);
381 bcm6328_led_write(mem
+ BCM6328_REG_LNKACTSEL_HI
, 0);
382 bcm6328_led_write(mem
+ BCM6328_REG_LNKACTSEL_LO
, 0);
384 val
= bcm6328_led_read(mem
+ BCM6328_REG_INIT
);
385 val
&= ~(BCM6328_INIT_MASK
);
386 if (of_property_read_bool(np
, "brcm,serial-leds"))
387 val
|= BCM6328_SERIAL_LED_EN
;
388 if (of_property_read_bool(np
, "brcm,serial-mux"))
389 val
|= BCM6328_SERIAL_LED_MUX
;
390 if (of_property_read_bool(np
, "brcm,serial-clk-low"))
391 val
|= BCM6328_SERIAL_LED_CLK_NPOL
;
392 if (!of_property_read_bool(np
, "brcm,serial-dat-low"))
393 val
|= BCM6328_SERIAL_LED_DATA_PPOL
;
394 if (!of_property_read_bool(np
, "brcm,serial-shift-inv"))
395 val
|= BCM6328_SERIAL_LED_SHIFT_DIR
;
396 bcm6328_led_write(mem
+ BCM6328_REG_INIT
, val
);
398 for_each_available_child_of_node(np
, child
) {
402 if (of_property_read_u32(child
, "reg", ®
))
405 if (reg
>= BCM6328_LED_MAX_COUNT
) {
406 dev_err(dev
, "invalid LED (%u >= %d)\n", reg
,
407 BCM6328_LED_MAX_COUNT
);
411 if (of_property_read_bool(child
, "brcm,hardware-controlled"))
412 rc
= bcm6328_hwled(dev
, child
, reg
, mem
, lock
);
414 rc
= bcm6328_led(dev
, child
, reg
, mem
, lock
,
415 blink_leds
, blink_delay
);
426 static const struct of_device_id bcm6328_leds_of_match
[] = {
427 { .compatible
= "brcm,bcm6328-leds", },
430 MODULE_DEVICE_TABLE(of
, bcm6328_leds_of_match
);
432 static struct platform_driver bcm6328_leds_driver
= {
433 .probe
= bcm6328_leds_probe
,
435 .name
= "leds-bcm6328",
436 .of_match_table
= bcm6328_leds_of_match
,
440 module_platform_driver(bcm6328_leds_driver
);
442 MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
443 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
444 MODULE_DESCRIPTION("LED driver for BCM6328 controllers");
445 MODULE_LICENSE("GPL v2");
446 MODULE_ALIAS("platform:leds-bcm6328");