2 * Driver for BCM6358 memory-mapped LEDs, based on leds-syscon.c
4 * Copyright 2015 Álvaro Fernández Rojas <noltari@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 #include <linux/delay.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/spinlock.h>
19 #define BCM6358_REG_MODE 0x0
20 #define BCM6358_REG_CTRL 0x4
22 #define BCM6358_SLED_CLKDIV_MASK 3
23 #define BCM6358_SLED_CLKDIV_1 0
24 #define BCM6358_SLED_CLKDIV_2 1
25 #define BCM6358_SLED_CLKDIV_4 2
26 #define BCM6358_SLED_CLKDIV_8 3
28 #define BCM6358_SLED_POLARITY BIT(2)
29 #define BCM6358_SLED_BUSY BIT(3)
31 #define BCM6358_SLED_MAX_COUNT 32
32 #define BCM6358_SLED_WAIT 100
35 * struct bcm6358_led - state container for bcm6358 based LEDs
36 * @cdev: LED class device for this LED
37 * @mem: memory resource
39 * @pin: LED pin number
40 * @active_low: LED is active low
43 struct led_classdev cdev
;
50 static void bcm6358_led_write(void __iomem
*reg
, unsigned long data
)
52 #ifdef CONFIG_CPU_BIG_ENDIAN
53 iowrite32be(data
, reg
);
59 static unsigned long bcm6358_led_read(void __iomem
*reg
)
61 #ifdef CONFIG_CPU_BIG_ENDIAN
62 return ioread32be(reg
);
68 static unsigned long bcm6358_led_busy(void __iomem
*mem
)
72 while ((val
= bcm6358_led_read(mem
+ BCM6358_REG_CTRL
)) &
74 udelay(BCM6358_SLED_WAIT
);
79 static void bcm6358_led_set(struct led_classdev
*led_cdev
,
80 enum led_brightness value
)
82 struct bcm6358_led
*led
=
83 container_of(led_cdev
, struct bcm6358_led
, cdev
);
84 unsigned long flags
, val
;
86 spin_lock_irqsave(led
->lock
, flags
);
87 bcm6358_led_busy(led
->mem
);
88 val
= bcm6358_led_read(led
->mem
+ BCM6358_REG_MODE
);
89 if ((led
->active_low
&& value
== LED_OFF
) ||
90 (!led
->active_low
&& value
!= LED_OFF
))
93 val
&= ~(BIT(led
->pin
));
94 bcm6358_led_write(led
->mem
+ BCM6358_REG_MODE
, val
);
95 spin_unlock_irqrestore(led
->lock
, flags
);
98 static int bcm6358_led(struct device
*dev
, struct device_node
*nc
, u32 reg
,
99 void __iomem
*mem
, spinlock_t
*lock
)
101 struct bcm6358_led
*led
;
105 led
= devm_kzalloc(dev
, sizeof(*led
), GFP_KERNEL
);
113 if (of_property_read_bool(nc
, "active-low"))
114 led
->active_low
= true;
116 led
->cdev
.name
= of_get_property(nc
, "label", NULL
) ? : nc
->name
;
117 led
->cdev
.default_trigger
= of_get_property(nc
,
118 "linux,default-trigger",
121 if (!of_property_read_string(nc
, "default-state", &state
)) {
122 if (!strcmp(state
, "on")) {
123 led
->cdev
.brightness
= LED_FULL
;
124 } else if (!strcmp(state
, "keep")) {
126 val
= bcm6358_led_read(led
->mem
+ BCM6358_REG_MODE
);
127 val
&= BIT(led
->pin
);
128 if ((led
->active_low
&& !val
) ||
129 (!led
->active_low
&& val
))
130 led
->cdev
.brightness
= LED_FULL
;
132 led
->cdev
.brightness
= LED_OFF
;
134 led
->cdev
.brightness
= LED_OFF
;
137 led
->cdev
.brightness
= LED_OFF
;
140 bcm6358_led_set(&led
->cdev
, led
->cdev
.brightness
);
142 led
->cdev
.brightness_set
= bcm6358_led_set
;
144 rc
= led_classdev_register(dev
, &led
->cdev
);
148 dev_dbg(dev
, "registered LED %s\n", led
->cdev
.name
);
153 static int bcm6358_leds_probe(struct platform_device
*pdev
)
155 struct device
*dev
= &pdev
->dev
;
156 struct device_node
*np
= pdev
->dev
.of_node
;
157 struct device_node
*child
;
158 struct resource
*mem_r
;
160 spinlock_t
*lock
; /* memory lock */
164 mem_r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
168 mem
= devm_ioremap_resource(dev
, mem_r
);
172 lock
= devm_kzalloc(dev
, sizeof(*lock
), GFP_KERNEL
);
176 spin_lock_init(lock
);
178 val
= bcm6358_led_busy(mem
);
179 val
&= ~(BCM6358_SLED_POLARITY
| BCM6358_SLED_CLKDIV_MASK
);
180 if (of_property_read_bool(np
, "brcm,clk-dat-low"))
181 val
|= BCM6358_SLED_POLARITY
;
182 of_property_read_u32(np
, "brcm,clk-div", &clk_div
);
185 val
|= BCM6358_SLED_CLKDIV_8
;
188 val
|= BCM6358_SLED_CLKDIV_4
;
191 val
|= BCM6358_SLED_CLKDIV_2
;
194 val
|= BCM6358_SLED_CLKDIV_1
;
197 bcm6358_led_write(mem
+ BCM6358_REG_CTRL
, val
);
199 for_each_available_child_of_node(np
, child
) {
203 if (of_property_read_u32(child
, "reg", ®
))
206 if (reg
>= BCM6358_SLED_MAX_COUNT
) {
207 dev_err(dev
, "invalid LED (%u >= %d)\n", reg
,
208 BCM6358_SLED_MAX_COUNT
);
212 rc
= bcm6358_led(dev
, child
, reg
, mem
, lock
);
222 static const struct of_device_id bcm6358_leds_of_match
[] = {
223 { .compatible
= "brcm,bcm6358-leds", },
226 MODULE_DEVICE_TABLE(of
, bcm6358_leds_of_match
);
228 static struct platform_driver bcm6358_leds_driver
= {
229 .probe
= bcm6358_leds_probe
,
231 .name
= "leds-bcm6358",
232 .of_match_table
= bcm6358_leds_of_match
,
236 module_platform_driver(bcm6358_leds_driver
);
238 MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
239 MODULE_DESCRIPTION("LED driver for BCM6358 controllers");
240 MODULE_LICENSE("GPL v2");
241 MODULE_ALIAS("platform:leds-bcm6358");