blob: 0b658f1db4cedde3021e4eea3916091b36e4ab87 [file] [log] [blame]
Pete Popov2cce8262005-09-18 11:18:10 +00001/*
Florian Fainelli4ead1682007-05-22 21:44:42 +02002 * Copyright (C) 2007, OpenWrt.org, Florian Fainelli <florian@openwrt.org>
3 * Architecture specific GPIO support
4 *
Pete Popov2cce8262005-09-18 11:18:10 +00005 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
11 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
13 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
14 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
15 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
16 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
17 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
19 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 675 Mass Ave, Cambridge, MA 02139, USA.
Florian Fainelli4ead1682007-05-22 21:44:42 +020024 *
25 * Notes :
26 * au1000 SoC have only one GPIO line : GPIO1
27 * others have a second one : GPIO2
Pete Popov2cce8262005-09-18 11:18:10 +000028 */
Florian Fainelli4ead1682007-05-22 21:44:42 +020029
Florian Fainelli4ead1682007-05-22 21:44:42 +020030#include <linux/init.h>
31#include <linux/io.h>
32#include <linux/types.h>
Pete Popov2cce8262005-09-18 11:18:10 +000033#include <linux/module.h>
Florian Fainelli4ead1682007-05-22 21:44:42 +020034
35#include <asm/addrspace.h>
36
37#include <asm/mach-au1x00/au1000.h>
38#include <asm/gpio.h>
Pete Popov2cce8262005-09-18 11:18:10 +000039
40#define gpio1 sys
41#if !defined(CONFIG_SOC_AU1000)
Pete Popov2cce8262005-09-18 11:18:10 +000042
Florian Fainelli4ead1682007-05-22 21:44:42 +020043static struct au1x00_gpio2 *const gpio2 = (struct au1x00_gpio2 *) GPIO2_BASE;
44#define GPIO2_OUTPUT_ENABLE_MASK 0x00010000
Pete Popov2cce8262005-09-18 11:18:10 +000045
Florian Fainelli4ead1682007-05-22 21:44:42 +020046static int au1xxx_gpio2_read(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000047{
Florian Fainelli4ead1682007-05-22 21:44:42 +020048 gpio -= AU1XXX_GPIO_BASE;
49 return ((gpio2->pinstate >> gpio) & 0x01);
Pete Popov2cce8262005-09-18 11:18:10 +000050}
51
Florian Fainelli4ead1682007-05-22 21:44:42 +020052static void au1xxx_gpio2_write(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000053{
Florian Fainelli4ead1682007-05-22 21:44:42 +020054 gpio -= AU1XXX_GPIO_BASE;
Pete Popov2cce8262005-09-18 11:18:10 +000055
Florian Fainelli4ead1682007-05-22 21:44:42 +020056 gpio2->output = (GPIO2_OUTPUT_ENABLE_MASK << gpio) | (value << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000057}
58
Florian Fainelli4ead1682007-05-22 21:44:42 +020059static int au1xxx_gpio2_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000060{
Florian Fainelli4ead1682007-05-22 21:44:42 +020061 gpio -= AU1XXX_GPIO_BASE;
62 gpio2->dir &= ~(0x01 << gpio);
63 return 0;
Pete Popov2cce8262005-09-18 11:18:10 +000064}
65
Florian Fainelli4ead1682007-05-22 21:44:42 +020066static int au1xxx_gpio2_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000067{
Florian Fainelli4ead1682007-05-22 21:44:42 +020068 gpio -= AU1XXX_GPIO_BASE;
69 gpio2->dir = (0x01 << gpio) | (value << gpio);
70 return 0;
71}
72
73#endif /* !defined(CONFIG_SOC_AU1000) */
74
75static int au1xxx_gpio1_read(unsigned gpio)
76{
77 return ((gpio1->pinstaterd >> gpio) & 0x01);
78}
79
80static void au1xxx_gpio1_write(unsigned gpio, int value)
81{
82 if (value)
83 gpio1->outputset = (0x01 << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000084 else
Florian Fainelli4ead1682007-05-22 21:44:42 +020085 /* Output a zero */
86 gpio1->outputclr = (0x01 << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000087}
88
Florian Fainelli4ead1682007-05-22 21:44:42 +020089static int au1xxx_gpio1_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000090{
Florian Fainelli4ead1682007-05-22 21:44:42 +020091 gpio1->pininputen = (0x01 << gpio);
92 return 0;
Pete Popov2cce8262005-09-18 11:18:10 +000093}
94
Florian Fainelli4ead1682007-05-22 21:44:42 +020095static int au1xxx_gpio1_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000096{
Florian Fainelli4ead1682007-05-22 21:44:42 +020097 gpio1->trioutclr = (0x01 & gpio);
98 return 0;
99}
100
101int au1xxx_gpio_get_value(unsigned gpio)
102{
103 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000104#if defined(CONFIG_SOC_AU1000)
105 return 0;
106#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200107 return au1xxx_gpio2_read(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000108#endif
109 else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200110 return au1xxx_gpio1_read(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000111}
112
Florian Fainelli4ead1682007-05-22 21:44:42 +0200113EXPORT_SYMBOL(au1xxx_gpio_get_value);
114
115void au1xxx_gpio_set_value(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +0000116{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200117 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000118#if defined(CONFIG_SOC_AU1000)
119 ;
120#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200121 au1xxx_gpio2_write(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000122#endif
123 else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200124 au1xxx_gpio1_write(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000125}
126
Florian Fainelli4ead1682007-05-22 21:44:42 +0200127EXPORT_SYMBOL(au1xxx_gpio_set_value);
128
129int au1xxx_gpio_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +0000130{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200131 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000132#if defined(CONFIG_SOC_AU1000)
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900133 return -ENODEV;
Pete Popov2cce8262005-09-18 11:18:10 +0000134#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200135 return au1xxx_gpio2_direction_input(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000136#endif
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900137
138 return au1xxx_gpio1_direction_input(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000139}
140
Florian Fainelli4ead1682007-05-22 21:44:42 +0200141EXPORT_SYMBOL(au1xxx_gpio_direction_input);
142
143int au1xxx_gpio_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +0000144{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200145 if (gpio >= AU1XXX_GPIO_BASE)
146#if defined(CONFIG_SOC_AU1000)
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900147 return -ENODEV;
Florian Fainelli4ead1682007-05-22 21:44:42 +0200148#else
149 return au1xxx_gpio2_direction_output(gpio, value);
150#endif
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900151
152 return au1xxx_gpio1_direction_output(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000153}
154
Florian Fainelli4ead1682007-05-22 21:44:42 +0200155EXPORT_SYMBOL(au1xxx_gpio_direction_output);