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/gpio.h>
26 struct ar7_gpio_chip
{
28 struct gpio_chip chip
;
31 static int ar7_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
33 struct ar7_gpio_chip
*gpch
=
34 container_of(chip
, struct ar7_gpio_chip
, chip
);
35 void __iomem
*gpio_in
= gpch
->regs
+ AR7_GPIO_INPUT
;
37 return readl(gpio_in
) & (1 << gpio
);
40 static int titan_gpio_get_value(struct gpio_chip
*chip
, unsigned gpio
)
42 struct ar7_gpio_chip
*gpch
=
43 container_of(chip
, struct ar7_gpio_chip
, chip
);
44 void __iomem
*gpio_in0
= gpch
->regs
+ TITAN_GPIO_INPUT_0
;
45 void __iomem
*gpio_in1
= gpch
->regs
+ TITAN_GPIO_INPUT_1
;
47 return readl(gpio
>> 5 ? gpio_in1
: gpio_in0
) & (1 << (gpio
& 0x1f));
50 static void ar7_gpio_set_value(struct gpio_chip
*chip
,
51 unsigned gpio
, int value
)
53 struct ar7_gpio_chip
*gpch
=
54 container_of(chip
, struct ar7_gpio_chip
, 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
=
68 container_of(chip
, struct ar7_gpio_chip
, chip
);
69 void __iomem
*gpio_out0
= gpch
->regs
+ TITAN_GPIO_OUTPUT_0
;
70 void __iomem
*gpio_out1
= gpch
->regs
+ TITAN_GPIO_OUTPUT_1
;
73 tmp
= readl(gpio
>> 5 ? gpio_out1
: gpio_out0
) & ~(1 << (gpio
& 0x1f));
75 tmp
|= 1 << (gpio
& 0x1f);
76 writel(tmp
, gpio
>> 5 ? gpio_out1
: gpio_out0
);
79 static int ar7_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
81 struct ar7_gpio_chip
*gpch
=
82 container_of(chip
, struct ar7_gpio_chip
, chip
);
83 void __iomem
*gpio_dir
= gpch
->regs
+ AR7_GPIO_DIR
;
85 writel(readl(gpio_dir
) | (1 << gpio
), gpio_dir
);
90 static int titan_gpio_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
92 struct ar7_gpio_chip
*gpch
=
93 container_of(chip
, struct ar7_gpio_chip
, chip
);
94 void __iomem
*gpio_dir0
= gpch
->regs
+ TITAN_GPIO_DIR_0
;
95 void __iomem
*gpio_dir1
= gpch
->regs
+ TITAN_GPIO_DIR_1
;
97 if (gpio
>= TITAN_GPIO_MAX
)
100 writel(readl(gpio
>> 5 ? gpio_dir1
: gpio_dir0
) | (1 << (gpio
& 0x1f)),
101 gpio
>> 5 ? gpio_dir1
: gpio_dir0
);
105 static int ar7_gpio_direction_output(struct gpio_chip
*chip
,
106 unsigned gpio
, int value
)
108 struct ar7_gpio_chip
*gpch
=
109 container_of(chip
, struct ar7_gpio_chip
, chip
);
110 void __iomem
*gpio_dir
= gpch
->regs
+ AR7_GPIO_DIR
;
112 ar7_gpio_set_value(chip
, gpio
, value
);
113 writel(readl(gpio_dir
) & ~(1 << gpio
), gpio_dir
);
118 static int titan_gpio_direction_output(struct gpio_chip
*chip
,
119 unsigned gpio
, int value
)
121 struct ar7_gpio_chip
*gpch
=
122 container_of(chip
, struct ar7_gpio_chip
, chip
);
123 void __iomem
*gpio_dir0
= gpch
->regs
+ TITAN_GPIO_DIR_0
;
124 void __iomem
*gpio_dir1
= gpch
->regs
+ TITAN_GPIO_DIR_1
;
126 if (gpio
>= TITAN_GPIO_MAX
)
129 titan_gpio_set_value(chip
, gpio
, value
);
130 writel(readl(gpio
>> 5 ? gpio_dir1
: gpio_dir0
) & ~(1 <<
131 (gpio
& 0x1f)), gpio
>> 5 ? gpio_dir1
: gpio_dir0
);
136 static struct ar7_gpio_chip ar7_gpio_chip
= {
139 .direction_input
= ar7_gpio_direction_input
,
140 .direction_output
= ar7_gpio_direction_output
,
141 .set
= ar7_gpio_set_value
,
142 .get
= ar7_gpio_get_value
,
144 .ngpio
= AR7_GPIO_MAX
,
148 static struct ar7_gpio_chip titan_gpio_chip
= {
150 .label
= "titan-gpio",
151 .direction_input
= titan_gpio_direction_input
,
152 .direction_output
= titan_gpio_direction_output
,
153 .set
= titan_gpio_set_value
,
154 .get
= titan_gpio_get_value
,
156 .ngpio
= TITAN_GPIO_MAX
,
160 static inline int ar7_gpio_enable_ar7(unsigned gpio
)
162 void __iomem
*gpio_en
= ar7_gpio_chip
.regs
+ AR7_GPIO_ENABLE
;
164 writel(readl(gpio_en
) | (1 << gpio
), gpio_en
);
169 static inline int ar7_gpio_enable_titan(unsigned gpio
)
171 void __iomem
*gpio_en0
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_0
;
172 void __iomem
*gpio_en1
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_1
;
174 writel(readl(gpio
>> 5 ? gpio_en1
: gpio_en0
) | (1 << (gpio
& 0x1f)),
175 gpio
>> 5 ? gpio_en1
: gpio_en0
);
180 int ar7_gpio_enable(unsigned gpio
)
182 return ar7_is_titan() ? ar7_gpio_enable_titan(gpio
) :
183 ar7_gpio_enable_ar7(gpio
);
185 EXPORT_SYMBOL(ar7_gpio_enable
);
187 static inline int ar7_gpio_disable_ar7(unsigned gpio
)
189 void __iomem
*gpio_en
= ar7_gpio_chip
.regs
+ AR7_GPIO_ENABLE
;
191 writel(readl(gpio_en
) & ~(1 << gpio
), gpio_en
);
196 static inline int ar7_gpio_disable_titan(unsigned gpio
)
198 void __iomem
*gpio_en0
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_0
;
199 void __iomem
*gpio_en1
= titan_gpio_chip
.regs
+ TITAN_GPIO_ENBL_1
;
201 writel(readl(gpio
>> 5 ? gpio_en1
: gpio_en0
) & ~(1 << (gpio
& 0x1f)),
202 gpio
>> 5 ? gpio_en1
: gpio_en0
);
207 int ar7_gpio_disable(unsigned gpio
)
209 return ar7_is_titan() ? ar7_gpio_disable_titan(gpio
) :
210 ar7_gpio_disable_ar7(gpio
);
212 EXPORT_SYMBOL(ar7_gpio_disable
);
214 struct titan_gpio_cfg
{
220 static const struct titan_gpio_cfg titan_gpio_table
[] = {
221 /* reg, start bit, mux value */
276 static int titan_gpio_pinsel(unsigned gpio
)
278 struct titan_gpio_cfg gpio_cfg
;
279 u32 mux_status
, pin_sel_reg
, tmp
;
280 void __iomem
*pin_sel
= (void __iomem
*)KSEG1ADDR(AR7_REGS_PINSEL
);
282 if (gpio
>= ARRAY_SIZE(titan_gpio_table
))
285 gpio_cfg
= titan_gpio_table
[gpio
];
286 pin_sel_reg
= gpio_cfg
.reg
- 1;
288 mux_status
= (readl(pin_sel
+ pin_sel_reg
) >> gpio_cfg
.shift
) & 0x3;
290 /* Check the mux status */
291 if (!((mux_status
== 0) || (mux_status
== gpio_cfg
.func
)))
294 /* Set the pin sel value */
295 tmp
= readl(pin_sel
+ pin_sel_reg
);
296 tmp
|= ((gpio_cfg
.func
& 0x3) << gpio_cfg
.shift
);
297 writel(tmp
, pin_sel
+ pin_sel_reg
);
302 /* Perform minimal Titan GPIO configuration */
303 static void titan_gpio_init(void)
307 for (i
= 44; i
< 48; i
++) {
308 titan_gpio_pinsel(i
);
309 ar7_gpio_enable_titan(i
);
310 titan_gpio_direction_input(&titan_gpio_chip
.chip
, i
);
314 int __init
ar7_gpio_init(void)
317 struct ar7_gpio_chip
*gpch
;
320 if (!ar7_is_titan()) {
321 gpch
= &ar7_gpio_chip
;
324 gpch
= &titan_gpio_chip
;
328 gpch
->regs
= ioremap_nocache(AR7_REGS_GPIO
, size
);
330 printk(KERN_ERR
"%s: failed to ioremap regs\n",
335 ret
= gpiochip_add(&gpch
->chip
);
337 printk(KERN_ERR
"%s: failed to add gpiochip\n",
341 printk(KERN_INFO
"%s: registered %d GPIOs\n",
342 gpch
->chip
.label
, gpch
->chip
.ngpio
);