blob: 47f7c9f310bea6d4f7c2094c3706a3c9a6f60b2d [file] [log] [blame]
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -07001/*
2 * Driver for BCM6328 memory-mapped LEDs, based on leds-syscon.c
3 *
4 * Copyright 2015 Álvaro Fernández Rojas <noltari@gmail.com>
5 * Copyright 2015 Jonas Gorski <jogo@openwrt.org>
6 *
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.
11 */
12#include <linux/io.h>
13#include <linux/leds.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/platform_device.h>
17#include <linux/spinlock.h>
18
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
28
29#define BCM6328_LED_MAX_COUNT 24
30#define BCM6328_LED_DEF_DELAY 500
31#define BCM6328_LED_INTERVAL_MS 20
32
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)
Álvaro Fernández Rojas9f82c772015-10-08 12:35:54 +020044#define BCM6328_INIT_MASK (BCM6328_SERIAL_LED_EN | \
Álvaro Fernández Rojas41251e22015-12-15 21:03:45 +010045 BCM6328_SERIAL_LED_MUX | \
Álvaro Fernández Rojas9f82c772015-10-08 12:35:54 +020046 BCM6328_SERIAL_LED_CLK_NPOL | \
47 BCM6328_SERIAL_LED_DATA_PPOL | \
48 BCM6328_SERIAL_LED_SHIFT_DIR)
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -070049
50#define BCM6328_LED_MODE_MASK 3
Simon Arlottb964c5b2015-11-15 13:34:37 +000051#define BCM6328_LED_MODE_ON 0
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -070052#define BCM6328_LED_MODE_FAST 1
53#define BCM6328_LED_MODE_BLINK 2
Simon Arlottb964c5b2015-11-15 13:34:37 +000054#define BCM6328_LED_MODE_OFF 3
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -070055#define BCM6328_LED_SHIFT(X) ((X) << 1)
56
57/**
58 * struct bcm6328_led - state container for bcm6328 based LEDs
59 * @cdev: LED class device for this LED
60 * @mem: memory resource
61 * @lock: memory lock
62 * @pin: LED pin number
63 * @blink_leds: blinking LEDs
64 * @blink_delay: blinking delay
65 * @active_low: LED is active low
66 */
67struct bcm6328_led {
68 struct led_classdev cdev;
69 void __iomem *mem;
70 spinlock_t *lock;
71 unsigned long pin;
72 unsigned long *blink_leds;
73 unsigned long *blink_delay;
74 bool active_low;
75};
76
77static void bcm6328_led_write(void __iomem *reg, unsigned long data)
78{
Álvaro Fernández Rojasa06cd4b2015-12-15 21:03:49 +010079#ifdef CONFIG_CPU_BIG_ENDIAN
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -070080 iowrite32be(data, reg);
Álvaro Fernández Rojasa06cd4b2015-12-15 21:03:49 +010081#else
82 writel(data, reg);
83#endif
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -070084}
85
86static unsigned long bcm6328_led_read(void __iomem *reg)
87{
Álvaro Fernández Rojasa06cd4b2015-12-15 21:03:49 +010088#ifdef CONFIG_CPU_BIG_ENDIAN
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -070089 return ioread32be(reg);
Álvaro Fernández Rojasa06cd4b2015-12-15 21:03:49 +010090#else
91 return readl(reg);
92#endif
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -070093}
94
95/**
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
100 */
101static unsigned long bcm6328_pin2shift(unsigned long pin)
102{
103 if (pin < 8)
104 return pin + 16; /* LEDs 0-7 (bits 47:32) */
105 else
106 return pin - 8; /* LEDs 8-23 (bits 31:0) */
107}
108
109static void bcm6328_led_mode(struct bcm6328_led *led, unsigned long value)
110{
111 void __iomem *mode;
112 unsigned long val, shift;
113
114 shift = bcm6328_pin2shift(led->pin);
115 if (shift / 16)
116 mode = led->mem + BCM6328_REG_MODE_HI;
117 else
118 mode = led->mem + BCM6328_REG_MODE_LO;
119
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);
124}
125
126static void bcm6328_led_set(struct led_classdev *led_cdev,
127 enum led_brightness value)
128{
129 struct bcm6328_led *led =
130 container_of(led_cdev, struct bcm6328_led, cdev);
131 unsigned long flags;
132
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))
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700137 bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
Simon Arlottb964c5b2015-11-15 13:34:37 +0000138 else
139 bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700140 spin_unlock_irqrestore(led->lock, flags);
141}
142
143static int bcm6328_blink_set(struct led_classdev *led_cdev,
144 unsigned long *delay_on, unsigned long *delay_off)
145{
146 struct bcm6328_led *led =
147 container_of(led_cdev, struct bcm6328_led, cdev);
148 unsigned long delay, flags;
Álvaro Fernández Rojas143b77c2015-12-15 21:03:51 +0100149 int rc;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700150
151 if (!*delay_on)
152 *delay_on = BCM6328_LED_DEF_DELAY;
153 if (!*delay_off)
154 *delay_off = BCM6328_LED_DEF_DELAY;
155
156 if (*delay_on != *delay_off) {
157 dev_dbg(led_cdev->dev,
158 "fallback to soft blinking (delay_on != delay_off)\n");
159 return -EINVAL;
160 }
161
162 delay = *delay_on / BCM6328_LED_INTERVAL_MS;
Álvaro Fernández Rojas41251e22015-12-15 21:03:45 +0100163 if (delay == 0) {
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700164 delay = 1;
Álvaro Fernández Rojas41251e22015-12-15 21:03:45 +0100165 } else if (delay > BCM6328_LED_INTV_MASK) {
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700166 dev_dbg(led_cdev->dev,
167 "fallback to soft blinking (delay > %ums)\n",
168 BCM6328_LED_INTV_MASK * BCM6328_LED_INTERVAL_MS);
169 return -EINVAL;
170 }
171
172 spin_lock_irqsave(led->lock, flags);
173 if (*(led->blink_leds) == 0 ||
174 *(led->blink_leds) == BIT(led->pin) ||
175 *(led->blink_delay) == delay) {
176 unsigned long val;
177
178 *(led->blink_leds) |= BIT(led->pin);
179 *(led->blink_delay) = delay;
180
181 val = bcm6328_led_read(led->mem + BCM6328_REG_INIT);
182 val &= ~BCM6328_LED_FAST_INTV_MASK;
183 val |= (delay << BCM6328_LED_FAST_INTV_SHIFT);
184 bcm6328_led_write(led->mem + BCM6328_REG_INIT, val);
185
186 bcm6328_led_mode(led, BCM6328_LED_MODE_BLINK);
Álvaro Fernández Rojas143b77c2015-12-15 21:03:51 +0100187 rc = 0;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700188 } else {
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700189 dev_dbg(led_cdev->dev,
190 "fallback to soft blinking (delay already set)\n");
Álvaro Fernández Rojas143b77c2015-12-15 21:03:51 +0100191 rc = -EINVAL;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700192 }
Álvaro Fernández Rojas143b77c2015-12-15 21:03:51 +0100193 spin_unlock_irqrestore(led->lock, flags);
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700194
Álvaro Fernández Rojas143b77c2015-12-15 21:03:51 +0100195 return rc;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700196}
197
198static int bcm6328_hwled(struct device *dev, struct device_node *nc, u32 reg,
199 void __iomem *mem, spinlock_t *lock)
200{
201 int i, cnt;
202 unsigned long flags, val;
203
204 spin_lock_irqsave(lock, flags);
205 val = bcm6328_led_read(mem + BCM6328_REG_HWDIS);
206 val &= ~BIT(reg);
207 bcm6328_led_write(mem + BCM6328_REG_HWDIS, val);
208 spin_unlock_irqrestore(lock, flags);
209
210 /* Only LEDs 0-7 can be activity/link controlled */
211 if (reg >= 8)
212 return 0;
213
214 cnt = of_property_count_elems_of_size(nc, "brcm,link-signal-sources",
215 sizeof(u32));
216 for (i = 0; i < cnt; i++) {
217 u32 sel;
218 void __iomem *addr;
219
220 if (reg < 4)
221 addr = mem + BCM6328_REG_LNKACTSEL_LO;
222 else
223 addr = mem + BCM6328_REG_LNKACTSEL_HI;
224
225 of_property_read_u32_index(nc, "brcm,link-signal-sources", i,
226 &sel);
227
228 if (reg / 4 != sel / 4) {
229 dev_warn(dev, "invalid link signal source\n");
230 continue;
231 }
232
233 spin_lock_irqsave(lock, flags);
234 val = bcm6328_led_read(addr);
235 val |= (BIT(reg) << (((sel % 4) * 4) + 16));
236 bcm6328_led_write(addr, val);
237 spin_unlock_irqrestore(lock, flags);
238 }
239
240 cnt = of_property_count_elems_of_size(nc,
241 "brcm,activity-signal-sources",
242 sizeof(u32));
243 for (i = 0; i < cnt; i++) {
244 u32 sel;
245 void __iomem *addr;
246
247 if (reg < 4)
248 addr = mem + BCM6328_REG_LNKACTSEL_LO;
249 else
250 addr = mem + BCM6328_REG_LNKACTSEL_HI;
251
252 of_property_read_u32_index(nc, "brcm,activity-signal-sources",
253 i, &sel);
254
255 if (reg / 4 != sel / 4) {
256 dev_warn(dev, "invalid activity signal source\n");
257 continue;
258 }
259
260 spin_lock_irqsave(lock, flags);
261 val = bcm6328_led_read(addr);
262 val |= (BIT(reg) << ((sel % 4) * 4));
263 bcm6328_led_write(addr, val);
264 spin_unlock_irqrestore(lock, flags);
265 }
266
267 return 0;
268}
269
270static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
271 void __iomem *mem, spinlock_t *lock,
272 unsigned long *blink_leds, unsigned long *blink_delay)
273{
274 struct bcm6328_led *led;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700275 const char *state;
276 int rc;
277
278 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
279 if (!led)
280 return -ENOMEM;
281
282 led->pin = reg;
283 led->mem = mem;
284 led->lock = lock;
285 led->blink_leds = blink_leds;
286 led->blink_delay = blink_delay;
287
288 if (of_property_read_bool(nc, "active-low"))
289 led->active_low = true;
290
291 led->cdev.name = of_get_property(nc, "label", NULL) ? : nc->name;
292 led->cdev.default_trigger = of_get_property(nc,
293 "linux,default-trigger",
294 NULL);
295
296 if (!of_property_read_string(nc, "default-state", &state)) {
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700297 if (!strcmp(state, "on")) {
298 led->cdev.brightness = LED_FULL;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700299 } else if (!strcmp(state, "keep")) {
300 void __iomem *mode;
301 unsigned long val, shift;
302
303 shift = bcm6328_pin2shift(led->pin);
304 if (shift / 16)
305 mode = mem + BCM6328_REG_MODE_HI;
306 else
307 mode = mem + BCM6328_REG_MODE_LO;
308
Álvaro Fernández Rojasd8fe1602015-10-08 12:35:53 +0200309 val = bcm6328_led_read(mode) >>
310 BCM6328_LED_SHIFT(shift % 16);
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700311 val &= BCM6328_LED_MODE_MASK;
Simon Arlottb964c5b2015-11-15 13:34:37 +0000312 if ((led->active_low && val == BCM6328_LED_MODE_OFF) ||
313 (!led->active_low && val == BCM6328_LED_MODE_ON))
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700314 led->cdev.brightness = LED_FULL;
Álvaro Fernández Rojasd8fe1602015-10-08 12:35:53 +0200315 else
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700316 led->cdev.brightness = LED_OFF;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700317 } else {
318 led->cdev.brightness = LED_OFF;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700319 }
Álvaro Fernández Rojasd8fe1602015-10-08 12:35:53 +0200320 } else {
321 led->cdev.brightness = LED_OFF;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700322 }
Álvaro Fernández Rojasd8fe1602015-10-08 12:35:53 +0200323
Simon Arlott9d3c0662015-11-16 20:24:59 +0000324 bcm6328_led_set(&led->cdev, led->cdev.brightness);
325
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700326 led->cdev.brightness_set = bcm6328_led_set;
327 led->cdev.blink_set = bcm6328_blink_set;
328
329 rc = led_classdev_register(dev, &led->cdev);
330 if (rc < 0)
331 return rc;
332
333 dev_dbg(dev, "registered LED %s\n", led->cdev.name);
334
335 return 0;
336}
337
338static int bcm6328_leds_probe(struct platform_device *pdev)
339{
340 struct device *dev = &pdev->dev;
341 struct device_node *np = pdev->dev.of_node;
342 struct device_node *child;
343 struct resource *mem_r;
344 void __iomem *mem;
Álvaro Fernández Rojas41251e22015-12-15 21:03:45 +0100345 spinlock_t *lock; /* memory lock */
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700346 unsigned long val, *blink_leds, *blink_delay;
347
348 mem_r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
349 if (!mem_r)
350 return -EINVAL;
351
352 mem = devm_ioremap_resource(dev, mem_r);
353 if (IS_ERR(mem))
354 return PTR_ERR(mem);
355
356 lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL);
357 if (!lock)
358 return -ENOMEM;
359
360 blink_leds = devm_kzalloc(dev, sizeof(*blink_leds), GFP_KERNEL);
361 if (!blink_leds)
362 return -ENOMEM;
363
364 blink_delay = devm_kzalloc(dev, sizeof(*blink_delay), GFP_KERNEL);
365 if (!blink_delay)
366 return -ENOMEM;
367
368 spin_lock_init(lock);
369
370 bcm6328_led_write(mem + BCM6328_REG_HWDIS, ~0);
371 bcm6328_led_write(mem + BCM6328_REG_LNKACTSEL_HI, 0);
372 bcm6328_led_write(mem + BCM6328_REG_LNKACTSEL_LO, 0);
373
374 val = bcm6328_led_read(mem + BCM6328_REG_INIT);
Álvaro Fernández Rojas9f82c772015-10-08 12:35:54 +0200375 val &= ~(BCM6328_INIT_MASK);
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700376 if (of_property_read_bool(np, "brcm,serial-leds"))
377 val |= BCM6328_SERIAL_LED_EN;
Álvaro Fernández Rojas9f82c772015-10-08 12:35:54 +0200378 if (of_property_read_bool(np, "brcm,serial-mux"))
379 val |= BCM6328_SERIAL_LED_MUX;
380 if (of_property_read_bool(np, "brcm,serial-clk-low"))
381 val |= BCM6328_SERIAL_LED_CLK_NPOL;
382 if (!of_property_read_bool(np, "brcm,serial-dat-low"))
383 val |= BCM6328_SERIAL_LED_DATA_PPOL;
384 if (!of_property_read_bool(np, "brcm,serial-shift-inv"))
385 val |= BCM6328_SERIAL_LED_SHIFT_DIR;
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700386 bcm6328_led_write(mem + BCM6328_REG_INIT, val);
387
388 for_each_available_child_of_node(np, child) {
389 int rc;
390 u32 reg;
391
392 if (of_property_read_u32(child, "reg", &reg))
393 continue;
394
395 if (reg >= BCM6328_LED_MAX_COUNT) {
Álvaro Fernández Rojas79653fb2015-10-08 12:35:52 +0200396 dev_err(dev, "invalid LED (%u >= %d)\n", reg,
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700397 BCM6328_LED_MAX_COUNT);
398 continue;
399 }
400
401 if (of_property_read_bool(child, "brcm,hardware-controlled"))
402 rc = bcm6328_hwled(dev, child, reg, mem, lock);
403 else
404 rc = bcm6328_led(dev, child, reg, mem, lock,
405 blink_leds, blink_delay);
406
Julia Lawall6cc76242015-10-24 16:42:29 +0200407 if (rc < 0) {
408 of_node_put(child);
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700409 return rc;
Julia Lawall6cc76242015-10-24 16:42:29 +0200410 }
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700411 }
412
413 return 0;
414}
415
416static const struct of_device_id bcm6328_leds_of_match[] = {
417 { .compatible = "brcm,bcm6328-leds", },
418 { },
419};
Luis de Bethencourt6528ca12015-09-01 23:35:38 +0200420MODULE_DEVICE_TABLE(of, bcm6328_leds_of_match);
Álvaro Fernández Rojasfd7b0252015-04-28 09:50:50 -0700421
422static struct platform_driver bcm6328_leds_driver = {
423 .probe = bcm6328_leds_probe,
424 .driver = {
425 .name = "leds-bcm6328",
426 .of_match_table = bcm6328_leds_of_match,
427 },
428};
429
430module_platform_driver(bcm6328_leds_driver);
431
432MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
433MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
434MODULE_DESCRIPTION("LED driver for BCM6328 controllers");
435MODULE_LICENSE("GPL v2");
436MODULE_ALIAS("platform:leds-bcm6328");