blob: 7abe420994390461ffb9d059475f6e5fb7defcce [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
30#include <linux/autoconf.h>
31#include <linux/init.h>
32#include <linux/io.h>
33#include <linux/types.h>
Pete Popov2cce8262005-09-18 11:18:10 +000034#include <linux/module.h>
Florian Fainelli4ead1682007-05-22 21:44:42 +020035
36#include <asm/addrspace.h>
37
38#include <asm/mach-au1x00/au1000.h>
39#include <asm/gpio.h>
Pete Popov2cce8262005-09-18 11:18:10 +000040
41#define gpio1 sys
42#if !defined(CONFIG_SOC_AU1000)
Pete Popov2cce8262005-09-18 11:18:10 +000043
Florian Fainelli4ead1682007-05-22 21:44:42 +020044static struct au1x00_gpio2 *const gpio2 = (struct au1x00_gpio2 *) GPIO2_BASE;
45#define GPIO2_OUTPUT_ENABLE_MASK 0x00010000
Pete Popov2cce8262005-09-18 11:18:10 +000046
Florian Fainelli4ead1682007-05-22 21:44:42 +020047static int au1xxx_gpio2_read(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000048{
Florian Fainelli4ead1682007-05-22 21:44:42 +020049 gpio -= AU1XXX_GPIO_BASE;
50 return ((gpio2->pinstate >> gpio) & 0x01);
Pete Popov2cce8262005-09-18 11:18:10 +000051}
52
Florian Fainelli4ead1682007-05-22 21:44:42 +020053static void au1xxx_gpio2_write(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000054{
Florian Fainelli4ead1682007-05-22 21:44:42 +020055 gpio -= AU1XXX_GPIO_BASE;
Pete Popov2cce8262005-09-18 11:18:10 +000056
Florian Fainelli4ead1682007-05-22 21:44:42 +020057 gpio2->output = (GPIO2_OUTPUT_ENABLE_MASK << gpio) | (value << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000058}
59
Florian Fainelli4ead1682007-05-22 21:44:42 +020060static int au1xxx_gpio2_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000061{
Florian Fainelli4ead1682007-05-22 21:44:42 +020062 gpio -= AU1XXX_GPIO_BASE;
63 gpio2->dir &= ~(0x01 << gpio);
64 return 0;
Pete Popov2cce8262005-09-18 11:18:10 +000065}
66
Florian Fainelli4ead1682007-05-22 21:44:42 +020067static int au1xxx_gpio2_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000068{
Florian Fainelli4ead1682007-05-22 21:44:42 +020069 gpio -= AU1XXX_GPIO_BASE;
70 gpio2->dir = (0x01 << gpio) | (value << gpio);
71 return 0;
72}
73
74#endif /* !defined(CONFIG_SOC_AU1000) */
75
76static int au1xxx_gpio1_read(unsigned gpio)
77{
78 return ((gpio1->pinstaterd >> gpio) & 0x01);
79}
80
81static void au1xxx_gpio1_write(unsigned gpio, int value)
82{
83 if (value)
84 gpio1->outputset = (0x01 << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000085 else
Florian Fainelli4ead1682007-05-22 21:44:42 +020086 /* Output a zero */
87 gpio1->outputclr = (0x01 << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000088}
89
Florian Fainelli4ead1682007-05-22 21:44:42 +020090static int au1xxx_gpio1_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000091{
Florian Fainelli4ead1682007-05-22 21:44:42 +020092 gpio1->pininputen = (0x01 << gpio);
93 return 0;
Pete Popov2cce8262005-09-18 11:18:10 +000094}
95
Florian Fainelli4ead1682007-05-22 21:44:42 +020096static int au1xxx_gpio1_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000097{
Florian Fainelli4ead1682007-05-22 21:44:42 +020098 gpio1->trioutclr = (0x01 & gpio);
99 return 0;
100}
101
102int au1xxx_gpio_get_value(unsigned gpio)
103{
104 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000105#if defined(CONFIG_SOC_AU1000)
106 return 0;
107#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200108 return au1xxx_gpio2_read(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000109#endif
110 else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200111 return au1xxx_gpio1_read(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000112}
113
Florian Fainelli4ead1682007-05-22 21:44:42 +0200114EXPORT_SYMBOL(au1xxx_gpio_get_value);
115
116void au1xxx_gpio_set_value(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +0000117{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200118 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000119#if defined(CONFIG_SOC_AU1000)
120 ;
121#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200122 au1xxx_gpio2_write(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000123#endif
124 else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200125 au1xxx_gpio1_write(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000126}
127
Florian Fainelli4ead1682007-05-22 21:44:42 +0200128EXPORT_SYMBOL(au1xxx_gpio_set_value);
129
130int au1xxx_gpio_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +0000131{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200132 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000133#if defined(CONFIG_SOC_AU1000)
134 ;
135#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200136 return au1xxx_gpio2_direction_input(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000137#endif
138 else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200139 return au1xxx_gpio1_direction_input(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000140}
141
Florian Fainelli4ead1682007-05-22 21:44:42 +0200142EXPORT_SYMBOL(au1xxx_gpio_direction_input);
143
144int au1xxx_gpio_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +0000145{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200146 if (gpio >= AU1XXX_GPIO_BASE)
147#if defined(CONFIG_SOC_AU1000)
148 ;
149#else
150 return au1xxx_gpio2_direction_output(gpio, value);
151#endif
152 else
153 return au1xxx_gpio1_direction_output(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000154}
155
Florian Fainelli4ead1682007-05-22 21:44:42 +0200156EXPORT_SYMBOL(au1xxx_gpio_direction_output);