ar71xx: add LED driver for the RB750
[openwrt.git] / target / linux / ar71xx / files / arch / mips / ar71xx / mach-rb750.c
1 /*
2  *  MikroTik RouterBOARD 750 support
3  *
4  *  Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  */
10
11 #include <linux/platform_device.h>
12 #include <asm/mach-ar71xx/ar71xx.h>
13 #include <asm/mach-ar71xx/mach-rb750.h>
14
15 #include "machtype.h"
16 #include "dev-ap91-eth.h"
17
18 static struct rb750_led_data rb750_leds[] = {
19         {
20                 .name           = "rb750:green:act",
21                 .mask           = RB750_LED_ACT,
22                 .active_low     = 1,
23         }, {
24                 .name           = "rb750:green:port1",
25                 .mask           = RB750_LED_PORT5,
26                 .active_low     = 1,
27         }, {
28                 .name           = "rb750:green:port2",
29                 .mask           = RB750_LED_PORT4,
30                 .active_low     = 1,
31         }, {
32                 .name           = "rb750:green:port3",
33                 .mask           = RB750_LED_PORT3,
34                 .active_low     = 1,
35         }, {
36                 .name           = "rb750:green:port4",
37                 .mask           = RB750_LED_PORT2,
38                 .active_low     = 1,
39         }, {
40                 .name           = "rb750:green:port5",
41                 .mask           = RB750_LED_PORT1,
42                 .active_low     = 1,
43         }
44 };
45
46 static struct rb750_led_platform_data rb750_leds_data = {
47         .num_leds       = ARRAY_SIZE(rb750_leds),
48         .leds           = rb750_leds,
49 };
50
51 static struct platform_device rb750_leds_device = {
52         .name   = "leds-rb750",
53         .id     = -1,
54         .dev    = {
55                 .platform_data = &rb750_leds_data,
56         }
57 };
58
59 int rb750_latch_change(u32 mask_clr, u32 mask_set)
60 {
61         static DEFINE_SPINLOCK(lock);
62         static u32 latch_set = RB750_LED_BITS | RB750_LVC573_LE;
63         static u32 latch_oe;
64         static u32 latch_clr;
65         unsigned long flags;
66         u32 t;
67         int ret = 0;
68
69         spin_lock_irqsave(&lock, flags);
70
71         if ((mask_clr & BIT(31)) != 0 &&
72             (latch_set & RB750_LVC573_LE) == 0) {
73                 goto unlock;
74         }
75
76         latch_set = (latch_set | mask_set) & ~mask_clr;
77         latch_clr = (latch_clr | mask_clr) & ~mask_set;
78
79         if (latch_oe == 0)
80                 latch_oe = __raw_readl(ar71xx_gpio_base + GPIO_REG_OE);
81
82         if (likely(latch_set & RB750_LVC573_LE)) {
83                 void __iomem *base = ar71xx_gpio_base;
84
85                 t = __raw_readl(base + GPIO_REG_OE);
86                 t |= mask_clr | latch_oe | mask_set;
87
88                 __raw_writel(t, base + GPIO_REG_OE);
89                 __raw_writel(latch_clr, base + GPIO_REG_CLEAR);
90                 __raw_writel(latch_set, base + GPIO_REG_SET);
91         } else if (mask_clr & RB750_LVC573_LE) {
92                 void __iomem *base = ar71xx_gpio_base;
93
94                 latch_oe = __raw_readl(base + GPIO_REG_OE);
95                 __raw_writel(RB750_LVC573_LE, base + GPIO_REG_CLEAR);
96                 /* flush write */
97                 __raw_readl(base + GPIO_REG_CLEAR);
98         }
99
100         ret = 1;
101
102  unlock:
103         spin_unlock_irqrestore(&lock, flags);
104         return ret;
105 }
106 EXPORT_SYMBOL_GPL(rb750_latch_change);
107
108 static void __init rb750_setup(void)
109 {
110         ar71xx_gpio_function_disable(AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
111                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
112                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
113                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
114                                      AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);
115
116         ap91_eth_init(NULL);
117         platform_device_register(&rb750_leds_device);
118 }
119
120 MIPS_MACHINE(AR71XX_MACH_RB_750, "750i", "MikroTik RouterBOARD 750",
121              rb750_setup);