2 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2007 Eugene Konev <ejka@openwrt.org>
4 * Copyright (C) 2009-2010 Florian Fainelli <florian@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/module.h>
22 #include <linux/gpio.h>
24 #include <asm/mach-ar7/ar7.h>
26 #define AR7_GPIO_MAX 32
27 #define TITAN_GPIO_MAX 51
29 struct ar7_gpio_chip
{
31 struct gpio_chip chip
;
34 static int ar7_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
36 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
37 void __iomem
*gpio_in
= gpch
->regs
+ AR7_GPIO_INPUT
;
39 return !!(readl(gpio_in
) & (1 << gpio
));
42 static int titan_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
44 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
45 void __iomem
*gpio_in0
= gpch
->regs
+ TITAN_GPIO_INPUT_0
;
46 void __iomem
*gpio_in1
= gpch
->regs
+ TITAN_GPIO_INPUT_1
;
48 return readl(gpio
>> 5 ? gpio_in1
: gpio_in0
) & (1 << (gpio
& 0x1f));
51 static void ar7_gpio_set_value(struct gpio_chip
*chip
,
52 unsigned gpio
, int value
)
54 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
55 void __iomem
*gpio_out
= gpch
->regs
+ AR7_GPIO_OUTPUT
;
58 tmp
= readl(gpio_out
) & ~(1 << gpio
);
61 writel(tmp
, gpio_out
);
64 static void titan_gpio_set_value(struct gpio_chip
*chip
,
65 unsigned gpio
, int value
)
67 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
68 void __iomem
*gpio_out0
= gpch
->regs
+ TITAN_GPIO_OUTPUT_0
;
69 void __iomem
*gpio_out1
= gpch
->regs
+ TITAN_GPIO_OUTPUT_1
;
72 tmp
= readl(gpio
>> 5 ? gpio_out1
: gpio_out0
) & ~(1 << (gpio
& 0x1f));
74 tmp
|= 1 << (gpio
& 0x1f);
75 writel(tmp
, gpio
>> 5 ? gpio_out1
: gpio_out0
);
78 static int ar7_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
80 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
81 void __iomem
*gpio_dir
= gpch
->regs
+ AR7_GPIO_DIR
;
83 writel(readl(gpio_dir
) | (1 << gpio
), gpio_dir
);
88 static int titan_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
90 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
91 void __iomem
*gpio_dir0
= gpch
->regs
+ TITAN_GPIO_DIR_0
;
92 void __iomem
*gpio_dir1
= gpch
->regs
+ TITAN_GPIO_DIR_1
;
94 if (gpio
>= TITAN_GPIO_MAX
)
97 writel(readl(gpio
>> 5 ? gpio_dir1
: gpio_dir0
) | (1 << (gpio
& 0x1f)),
98 gpio
>> 5 ? gpio_dir1
: gpio_dir0
);
102 static int ar7_gpio_direction_output(struct gpio_chip
*chip
,
103 unsigned gpio
, int value
)
105 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
106 void __iomem
*gpio_dir
= gpch
->regs
+ AR7_GPIO_DIR
;
108 ar7_gpio_set_value(chip
, gpio
, value
);
109 writel(readl(gpio_dir
) & ~(1 << gpio
), gpio_dir
);
114 static int titan_gpio_direction_output(struct gpio_chip
*chip
,
115 unsigned gpio
, int value
)
117 struct ar7_gpio_chip
*gpch
= gpiochip_get_data(chip
);
118 void __iomem
*gpio_dir0
= gpch
->regs
+ TITAN_GPIO_DIR_0
;
119 void __iomem
*gpio_dir1
= gpch
->regs
+ TITAN_GPIO_DIR_1
;
121 if (gpio
>= TITAN_GPIO_MAX
)
124 titan_gpio_set_value(chip
, gpio
, value
);
125 writel(readl(gpio
>> 5 ? gpio_dir1
: gpio_dir0
) & ~(1 <<
126 (gpio
& 0x1f)), gpio
>> 5 ? gpio_dir1
: gpio_dir0
);
131 static struct ar7_gpio_chip ar7_gpio_chip
= {
134 .direction_input
= ar7_gpio_direction_input
,
135 .direction_output
= ar7_gpio_direction_output
,
136 .set
= ar7_gpio_set_value
,
137 .get
= ar7_gpio_get_value
,
139 .ngpio
= AR7_GPIO_MAX
,
143 static struct ar7_gpio_chip titan_gpio_chip
= {
145 .label
= "titan-gpio",
146 .direction_input
= titan_gpio_direction_input
,
147 .direction_output
= titan_gpio_direction_output
,
148 .set
= titan_gpio_set_value
,
149 .get
= titan_gpio_get_value
,
151 .ngpio
= TITAN_GPIO_MAX
,
155 static inline int ar7_gpio_enable_ar7(unsigned gpio
)
157 void __iomem
*gpio_en
= ar7_gpio_chip
.regs
+ AR7_GPIO_ENABLE
;
159 writel(readl(gpio_en
) | (1 << gpio
), gpio_en
);
164 static inline int ar7_gpio_enable_titan(unsigned gpio
)
166 void __iomem
*gpio_en0
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_0
;
167 void __iomem
*gpio_en1
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_1
;
169 writel(readl(gpio
>> 5 ? gpio_en1
: gpio_en0
) | (1 << (gpio
& 0x1f)),
170 gpio
>> 5 ? gpio_en1
: gpio_en0
);
175 int ar7_gpio_enable(unsigned gpio
)
177 return ar7_is_titan() ? ar7_gpio_enable_titan(gpio
) :
178 ar7_gpio_enable_ar7(gpio
);
180 EXPORT_SYMBOL(ar7_gpio_enable
);
182 static inline int ar7_gpio_disable_ar7(unsigned gpio
)
184 void __iomem
*gpio_en
= ar7_gpio_chip
.regs
+ AR7_GPIO_ENABLE
;
186 writel(readl(gpio_en
) & ~(1 << gpio
), gpio_en
);
191 static inline int ar7_gpio_disable_titan(unsigned gpio
)
193 void __iomem
*gpio_en0
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_0
;
194 void __iomem
*gpio_en1
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_1
;
196 writel(readl(gpio
>> 5 ? gpio_en1
: gpio_en0
) & ~(1 << (gpio
& 0x1f)),
197 gpio
>> 5 ? gpio_en1
: gpio_en0
);
202 int ar7_gpio_disable(unsigned gpio
)
204 return ar7_is_titan() ? ar7_gpio_disable_titan(gpio
) :
205 ar7_gpio_disable_ar7(gpio
);
207 EXPORT_SYMBOL(ar7_gpio_disable
);
209 struct titan_gpio_cfg
{
215 static const struct titan_gpio_cfg titan_gpio_table
[] = {
216 /* reg, start bit, mux value */
271 static int titan_gpio_pinsel(unsigned gpio
)
273 struct titan_gpio_cfg gpio_cfg
;
274 u32 mux_status
, pin_sel_reg
, tmp
;
275 void __iomem
*pin_sel
= (void __iomem
*)KSEG1ADDR(AR7_REGS_PINSEL
);
277 if (gpio
>= ARRAY_SIZE(titan_gpio_table
))
280 gpio_cfg
= titan_gpio_table
[gpio
];
281 pin_sel_reg
= gpio_cfg
.reg
- 1;
283 mux_status
= (readl(pin_sel
+ pin_sel_reg
) >> gpio_cfg
.shift
) & 0x3;
285 /* Check the mux status */
286 if (!((mux_status
== 0) || (mux_status
== gpio_cfg
.func
)))
289 /* Set the pin sel value */
290 tmp
= readl(pin_sel
+ pin_sel_reg
);
291 tmp
|= ((gpio_cfg
.func
& 0x3) << gpio_cfg
.shift
);
292 writel(tmp
, pin_sel
+ pin_sel_reg
);
297 /* Perform minimal Titan GPIO configuration */
298 static void titan_gpio_init(void)
302 for (i
= 44; i
< 48; i
++) {
303 titan_gpio_pinsel(i
);
304 ar7_gpio_enable_titan(i
);
305 titan_gpio_direction_input(&titan_gpio_chip
.chip
, i
);
309 int __init
ar7_gpio_init(void)
312 struct ar7_gpio_chip
*gpch
;
315 if (!ar7_is_titan()) {
316 gpch
= &ar7_gpio_chip
;
319 gpch
= &titan_gpio_chip
;
323 gpch
->regs
= ioremap_nocache(AR7_REGS_GPIO
, size
);
325 printk(KERN_ERR
"%s: failed to ioremap regs\n",
330 ret
= gpiochip_add_data(&gpch
->chip
, gpch
);
332 printk(KERN_ERR
"%s: failed to add gpiochip\n",
336 printk(KERN_INFO
"%s: registered %d GPIOs\n",
337 gpch
->chip
.label
, gpch
->chip
.ngpio
);