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)
45 #define BCM6328_LED_MODE_MASK 3
46 #define BCM6328_LED_MODE_OFF 0
47 #define BCM6328_LED_MODE_FAST 1
48 #define BCM6328_LED_MODE_BLINK 2
49 #define BCM6328_LED_MODE_ON 3
50 #define BCM6328_LED_SHIFT(X) ((X) << 1)
53 * struct bcm6328_led - state container for bcm6328 based LEDs
54 * @cdev: LED class device for this LED
55 * @mem: memory resource
57 * @pin: LED pin number
58 * @blink_leds: blinking LEDs
59 * @blink_delay: blinking delay
60 * @active_low: LED is active low
63 struct led_classdev cdev
;
67 unsigned long *blink_leds
;
68 unsigned long *blink_delay
;
72 static void bcm6328_led_write(void __iomem
*reg
, unsigned long data
)
74 iowrite32be(data
, reg
);
77 static unsigned long bcm6328_led_read(void __iomem
*reg
)
79 return ioread32be(reg
);
83 * LEDMode 64 bits / 24 LEDs
84 * bits [31:0] -> LEDs 8-23
85 * bits [47:32] -> LEDs 0-7
86 * bits [63:48] -> unused
88 static unsigned long bcm6328_pin2shift(unsigned long pin
)
91 return pin
+ 16; /* LEDs 0-7 (bits 47:32) */
93 return pin
- 8; /* LEDs 8-23 (bits 31:0) */
96 static void bcm6328_led_mode(struct bcm6328_led
*led
, unsigned long value
)
99 unsigned long val
, shift
;
101 shift
= bcm6328_pin2shift(led
->pin
);
103 mode
= led
->mem
+ BCM6328_REG_MODE_HI
;
105 mode
= led
->mem
+ BCM6328_REG_MODE_LO
;
107 val
= bcm6328_led_read(mode
);
108 val
&= ~(BCM6328_LED_MODE_MASK
<< BCM6328_LED_SHIFT(shift
% 16));
109 val
|= (value
<< BCM6328_LED_SHIFT(shift
% 16));
110 bcm6328_led_write(mode
, val
);
113 static void bcm6328_led_set(struct led_classdev
*led_cdev
,
114 enum led_brightness value
)
116 struct bcm6328_led
*led
=
117 container_of(led_cdev
, struct bcm6328_led
, cdev
);
120 spin_lock_irqsave(led
->lock
, flags
);
121 *(led
->blink_leds
) &= ~BIT(led
->pin
);
122 if ((led
->active_low
&& value
== LED_OFF
) ||
123 (!led
->active_low
&& value
!= LED_OFF
))
124 bcm6328_led_mode(led
, BCM6328_LED_MODE_OFF
);
126 bcm6328_led_mode(led
, BCM6328_LED_MODE_ON
);
127 spin_unlock_irqrestore(led
->lock
, flags
);
130 static int bcm6328_blink_set(struct led_classdev
*led_cdev
,
131 unsigned long *delay_on
, unsigned long *delay_off
)
133 struct bcm6328_led
*led
=
134 container_of(led_cdev
, struct bcm6328_led
, cdev
);
135 unsigned long delay
, flags
;
138 *delay_on
= BCM6328_LED_DEF_DELAY
;
140 *delay_off
= BCM6328_LED_DEF_DELAY
;
142 if (*delay_on
!= *delay_off
) {
143 dev_dbg(led_cdev
->dev
,
144 "fallback to soft blinking (delay_on != delay_off)\n");
148 delay
= *delay_on
/ BCM6328_LED_INTERVAL_MS
;
151 else if (delay
> BCM6328_LED_INTV_MASK
) {
152 dev_dbg(led_cdev
->dev
,
153 "fallback to soft blinking (delay > %ums)\n",
154 BCM6328_LED_INTV_MASK
* BCM6328_LED_INTERVAL_MS
);
158 spin_lock_irqsave(led
->lock
, flags
);
159 if (*(led
->blink_leds
) == 0 ||
160 *(led
->blink_leds
) == BIT(led
->pin
) ||
161 *(led
->blink_delay
) == delay
) {
164 *(led
->blink_leds
) |= BIT(led
->pin
);
165 *(led
->blink_delay
) = delay
;
167 val
= bcm6328_led_read(led
->mem
+ BCM6328_REG_INIT
);
168 val
&= ~BCM6328_LED_FAST_INTV_MASK
;
169 val
|= (delay
<< BCM6328_LED_FAST_INTV_SHIFT
);
170 bcm6328_led_write(led
->mem
+ BCM6328_REG_INIT
, val
);
172 bcm6328_led_mode(led
, BCM6328_LED_MODE_BLINK
);
174 spin_unlock_irqrestore(led
->lock
, flags
);
176 spin_unlock_irqrestore(led
->lock
, flags
);
177 dev_dbg(led_cdev
->dev
,
178 "fallback to soft blinking (delay already set)\n");
185 static int bcm6328_hwled(struct device
*dev
, struct device_node
*nc
, u32 reg
,
186 void __iomem
*mem
, spinlock_t
*lock
)
189 unsigned long flags
, val
;
191 spin_lock_irqsave(lock
, flags
);
192 val
= bcm6328_led_read(mem
+ BCM6328_REG_HWDIS
);
194 bcm6328_led_write(mem
+ BCM6328_REG_HWDIS
, val
);
195 spin_unlock_irqrestore(lock
, flags
);
197 /* Only LEDs 0-7 can be activity/link controlled */
201 cnt
= of_property_count_elems_of_size(nc
, "brcm,link-signal-sources",
203 for (i
= 0; i
< cnt
; i
++) {
208 addr
= mem
+ BCM6328_REG_LNKACTSEL_LO
;
210 addr
= mem
+ BCM6328_REG_LNKACTSEL_HI
;
212 of_property_read_u32_index(nc
, "brcm,link-signal-sources", i
,
215 if (reg
/ 4 != sel
/ 4) {
216 dev_warn(dev
, "invalid link signal source\n");
220 spin_lock_irqsave(lock
, flags
);
221 val
= bcm6328_led_read(addr
);
222 val
|= (BIT(reg
) << (((sel
% 4) * 4) + 16));
223 bcm6328_led_write(addr
, val
);
224 spin_unlock_irqrestore(lock
, flags
);
227 cnt
= of_property_count_elems_of_size(nc
,
228 "brcm,activity-signal-sources",
230 for (i
= 0; i
< cnt
; i
++) {
235 addr
= mem
+ BCM6328_REG_LNKACTSEL_LO
;
237 addr
= mem
+ BCM6328_REG_LNKACTSEL_HI
;
239 of_property_read_u32_index(nc
, "brcm,activity-signal-sources",
242 if (reg
/ 4 != sel
/ 4) {
243 dev_warn(dev
, "invalid activity signal source\n");
247 spin_lock_irqsave(lock
, flags
);
248 val
= bcm6328_led_read(addr
);
249 val
|= (BIT(reg
) << ((sel
% 4) * 4));
250 bcm6328_led_write(addr
, val
);
251 spin_unlock_irqrestore(lock
, flags
);
257 static int bcm6328_led(struct device
*dev
, struct device_node
*nc
, u32 reg
,
258 void __iomem
*mem
, spinlock_t
*lock
,
259 unsigned long *blink_leds
, unsigned long *blink_delay
)
261 struct bcm6328_led
*led
;
266 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
273 led
->blink_leds
= blink_leds
;
274 led
->blink_delay
= blink_delay
;
276 if (of_property_read_bool(nc
, "active-low"))
277 led
->active_low
= true;
279 led
->cdev
.name
= of_get_property(nc
, "label", NULL
) ? : nc
->name
;
280 led
->cdev
.default_trigger
= of_get_property(nc
,
281 "linux,default-trigger",
284 if (!of_property_read_string(nc
, "default-state", &state
)) {
285 spin_lock_irqsave(lock
, flags
);
286 if (!strcmp(state
, "on")) {
287 led
->cdev
.brightness
= LED_FULL
;
288 bcm6328_led_mode(led
, BCM6328_LED_MODE_ON
);
289 } else if (!strcmp(state
, "keep")) {
291 unsigned long val
, shift
;
293 shift
= bcm6328_pin2shift(led
->pin
);
295 mode
= mem
+ BCM6328_REG_MODE_HI
;
297 mode
= mem
+ BCM6328_REG_MODE_LO
;
299 val
= bcm6328_led_read(mode
) >> (shift
% 16);
300 val
&= BCM6328_LED_MODE_MASK
;
301 if (val
== BCM6328_LED_MODE_ON
)
302 led
->cdev
.brightness
= LED_FULL
;
304 led
->cdev
.brightness
= LED_OFF
;
305 bcm6328_led_mode(led
, BCM6328_LED_MODE_OFF
);
308 led
->cdev
.brightness
= LED_OFF
;
309 bcm6328_led_mode(led
, BCM6328_LED_MODE_OFF
);
311 spin_unlock_irqrestore(lock
, flags
);
314 led
->cdev
.brightness_set
= bcm6328_led_set
;
315 led
->cdev
.blink_set
= bcm6328_blink_set
;
317 rc
= led_classdev_register(dev
, &led
->cdev
);
321 dev_dbg(dev
, "registered LED %s\n", led
->cdev
.name
);
326 static int bcm6328_leds_probe(struct platform_device
*pdev
)
328 struct device
*dev
= &pdev
->dev
;
329 struct device_node
*np
= pdev
->dev
.of_node
;
330 struct device_node
*child
;
331 struct resource
*mem_r
;
334 unsigned long val
, *blink_leds
, *blink_delay
;
336 mem_r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
340 mem
= devm_ioremap_resource(dev
, mem_r
);
344 lock
= devm_kzalloc(dev
, sizeof(*lock
), GFP_KERNEL
);
348 blink_leds
= devm_kzalloc(dev
, sizeof(*blink_leds
), GFP_KERNEL
);
352 blink_delay
= devm_kzalloc(dev
, sizeof(*blink_delay
), GFP_KERNEL
);
356 spin_lock_init(lock
);
358 bcm6328_led_write(mem
+ BCM6328_REG_HWDIS
, ~0);
359 bcm6328_led_write(mem
+ BCM6328_REG_LNKACTSEL_HI
, 0);
360 bcm6328_led_write(mem
+ BCM6328_REG_LNKACTSEL_LO
, 0);
362 val
= bcm6328_led_read(mem
+ BCM6328_REG_INIT
);
363 val
&= ~BCM6328_SERIAL_LED_EN
;
364 if (of_property_read_bool(np
, "brcm,serial-leds"))
365 val
|= BCM6328_SERIAL_LED_EN
;
366 bcm6328_led_write(mem
+ BCM6328_REG_INIT
, val
);
368 for_each_available_child_of_node(np
, child
) {
372 if (of_property_read_u32(child
, "reg", ®
))
375 if (reg
>= BCM6328_LED_MAX_COUNT
) {
376 dev_err(dev
, "invalid LED (>= %d)\n",
377 BCM6328_LED_MAX_COUNT
);
381 if (of_property_read_bool(child
, "brcm,hardware-controlled"))
382 rc
= bcm6328_hwled(dev
, child
, reg
, mem
, lock
);
384 rc
= bcm6328_led(dev
, child
, reg
, mem
, lock
,
385 blink_leds
, blink_delay
);
394 static const struct of_device_id bcm6328_leds_of_match
[] = {
395 { .compatible
= "brcm,bcm6328-leds", },
399 static struct platform_driver bcm6328_leds_driver
= {
400 .probe
= bcm6328_leds_probe
,
402 .name
= "leds-bcm6328",
403 .of_match_table
= bcm6328_leds_of_match
,
407 module_platform_driver(bcm6328_leds_driver
);
409 MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
410 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
411 MODULE_DESCRIPTION("LED driver for BCM6328 controllers");
412 MODULE_LICENSE("GPL v2");
413 MODULE_ALIAS("platform:leds-bcm6328");