net: ipmr: adjust mroute.h style and drop extern
[linux/fpc-iii.git] / drivers / leds / leds-bcm6328.c
blobc7ea5c62633186294eff8046dba51c8865d06eab
1 /*
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.
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>
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)
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
67 struct 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;
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);
87 /**
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)
95 if (pin < 8)
96 return pin + 16; /* LEDs 0-7 (bits 47:32) */
97 else
98 return pin - 8; /* LEDs 8-23 (bits 31:0) */
101 static void bcm6328_led_mode(struct bcm6328_led *led, unsigned long value)
103 void __iomem *mode;
104 unsigned long val, shift;
106 shift = bcm6328_pin2shift(led->pin);
107 if (shift / 16)
108 mode = led->mem + BCM6328_REG_MODE_HI;
109 else
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);
123 unsigned long flags;
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);
130 else
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;
142 if (!*delay_on)
143 *delay_on = BCM6328_LED_DEF_DELAY;
144 if (!*delay_off)
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");
150 return -EINVAL;
153 delay = *delay_on / BCM6328_LED_INTERVAL_MS;
154 if (delay == 0)
155 delay = 1;
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);
160 return -EINVAL;
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) {
167 unsigned long val;
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);
180 } else {
181 spin_unlock_irqrestore(led->lock, flags);
182 dev_dbg(led_cdev->dev,
183 "fallback to soft blinking (delay already set)\n");
184 return -EINVAL;
187 return 0;
190 static int bcm6328_hwled(struct device *dev, struct device_node *nc, u32 reg,
191 void __iomem *mem, spinlock_t *lock)
193 int i, cnt;
194 unsigned long flags, val;
196 spin_lock_irqsave(lock, flags);
197 val = bcm6328_led_read(mem + BCM6328_REG_HWDIS);
198 val &= ~BIT(reg);
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 */
203 if (reg >= 8)
204 return 0;
206 cnt = of_property_count_elems_of_size(nc, "brcm,link-signal-sources",
207 sizeof(u32));
208 for (i = 0; i < cnt; i++) {
209 u32 sel;
210 void __iomem *addr;
212 if (reg < 4)
213 addr = mem + BCM6328_REG_LNKACTSEL_LO;
214 else
215 addr = mem + BCM6328_REG_LNKACTSEL_HI;
217 of_property_read_u32_index(nc, "brcm,link-signal-sources", i,
218 &sel);
220 if (reg / 4 != sel / 4) {
221 dev_warn(dev, "invalid link signal source\n");
222 continue;
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",
234 sizeof(u32));
235 for (i = 0; i < cnt; i++) {
236 u32 sel;
237 void __iomem *addr;
239 if (reg < 4)
240 addr = mem + BCM6328_REG_LNKACTSEL_LO;
241 else
242 addr = mem + BCM6328_REG_LNKACTSEL_HI;
244 of_property_read_u32_index(nc, "brcm,activity-signal-sources",
245 i, &sel);
247 if (reg / 4 != sel / 4) {
248 dev_warn(dev, "invalid activity signal source\n");
249 continue;
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);
259 return 0;
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;
267 unsigned long flags;
268 const char *state;
269 int rc;
271 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
272 if (!led)
273 return -ENOMEM;
275 led->pin = reg;
276 led->mem = mem;
277 led->lock = lock;
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",
287 NULL);
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")) {
294 void __iomem *mode;
295 unsigned long val, shift;
297 shift = bcm6328_pin2shift(led->pin);
298 if (shift / 16)
299 mode = mem + BCM6328_REG_MODE_HI;
300 else
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;
309 else
310 led->cdev.brightness = LED_OFF;
311 } else {
312 led->cdev.brightness = LED_OFF;
314 } else {
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);
321 else
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);
329 if (rc < 0)
330 return rc;
332 dev_dbg(dev, "registered LED %s\n", led->cdev.name);
334 return 0;
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;
343 void __iomem *mem;
344 spinlock_t *lock;
345 unsigned long val, *blink_leds, *blink_delay;
347 mem_r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
348 if (!mem_r)
349 return -EINVAL;
351 mem = devm_ioremap_resource(dev, mem_r);
352 if (IS_ERR(mem))
353 return PTR_ERR(mem);
355 lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL);
356 if (!lock)
357 return -ENOMEM;
359 blink_leds = devm_kzalloc(dev, sizeof(*blink_leds), GFP_KERNEL);
360 if (!blink_leds)
361 return -ENOMEM;
363 blink_delay = devm_kzalloc(dev, sizeof(*blink_delay), GFP_KERNEL);
364 if (!blink_delay)
365 return -ENOMEM;
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) {
388 int rc;
389 u32 reg;
391 if (of_property_read_u32(child, "reg", &reg))
392 continue;
394 if (reg >= BCM6328_LED_MAX_COUNT) {
395 dev_err(dev, "invalid LED (%u >= %d)\n", reg,
396 BCM6328_LED_MAX_COUNT);
397 continue;
400 if (of_property_read_bool(child, "brcm,hardware-controlled"))
401 rc = bcm6328_hwled(dev, child, reg, mem, lock);
402 else
403 rc = bcm6328_led(dev, child, reg, mem, lock,
404 blink_leds, blink_delay);
406 if (rc < 0) {
407 of_node_put(child);
408 return rc;
412 return 0;
415 static const struct of_device_id bcm6328_leds_of_match[] = {
416 { .compatible = "brcm,bcm6328-leds", },
417 { },
419 MODULE_DEVICE_TABLE(of, bcm6328_leds_of_match);
421 static struct platform_driver bcm6328_leds_driver = {
422 .probe = bcm6328_leds_probe,
423 .driver = {
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");