blob: e660ddd611c465dc6665ecf7d1346f82a5e77b98 [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
Pete Popov2cce8262005-09-18 11:18:10 +000030#include <linux/module.h>
Florian Fainelli4ead1682007-05-22 21:44:42 +020031
Florian Fainelli4ead1682007-05-22 21:44:42 +020032#include <asm/mach-au1x00/au1000.h>
33#include <asm/gpio.h>
Pete Popov2cce8262005-09-18 11:18:10 +000034
35#define gpio1 sys
36#if !defined(CONFIG_SOC_AU1000)
Pete Popov2cce8262005-09-18 11:18:10 +000037
Florian Fainelli4ead1682007-05-22 21:44:42 +020038static struct au1x00_gpio2 *const gpio2 = (struct au1x00_gpio2 *) GPIO2_BASE;
39#define GPIO2_OUTPUT_ENABLE_MASK 0x00010000
Pete Popov2cce8262005-09-18 11:18:10 +000040
Florian Fainelli4ead1682007-05-22 21:44:42 +020041static int au1xxx_gpio2_read(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000042{
Florian Fainelli4ead1682007-05-22 21:44:42 +020043 gpio -= AU1XXX_GPIO_BASE;
44 return ((gpio2->pinstate >> gpio) & 0x01);
Pete Popov2cce8262005-09-18 11:18:10 +000045}
46
Florian Fainelli4ead1682007-05-22 21:44:42 +020047static void au1xxx_gpio2_write(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000048{
Florian Fainelli4ead1682007-05-22 21:44:42 +020049 gpio -= AU1XXX_GPIO_BASE;
Pete Popov2cce8262005-09-18 11:18:10 +000050
Bruno Randolf19506fc2008-09-25 16:45:10 +020051 gpio2->output = (GPIO2_OUTPUT_ENABLE_MASK << gpio) | ((!!value) << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000052}
53
Florian Fainelli4ead1682007-05-22 21:44:42 +020054static int au1xxx_gpio2_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000055{
Florian Fainelli4ead1682007-05-22 21:44:42 +020056 gpio -= AU1XXX_GPIO_BASE;
57 gpio2->dir &= ~(0x01 << gpio);
58 return 0;
Pete Popov2cce8262005-09-18 11:18:10 +000059}
60
Florian Fainelli4ead1682007-05-22 21:44:42 +020061static int au1xxx_gpio2_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000062{
Florian Fainelli4ead1682007-05-22 21:44:42 +020063 gpio -= AU1XXX_GPIO_BASE;
Bruno Randolf44ce1712008-09-23 19:48:36 +020064 gpio2->dir |= 0x01 << gpio;
Bruno Randolf19506fc2008-09-25 16:45:10 +020065 gpio2->output = (GPIO2_OUTPUT_ENABLE_MASK << gpio) | ((!!value) << gpio);
Florian Fainelli4ead1682007-05-22 21:44:42 +020066 return 0;
67}
68
69#endif /* !defined(CONFIG_SOC_AU1000) */
70
71static int au1xxx_gpio1_read(unsigned gpio)
72{
Sergei Shtylyovc1dcb142008-04-30 23:18:41 +040073 return (gpio1->pinstaterd >> gpio) & 0x01;
Florian Fainelli4ead1682007-05-22 21:44:42 +020074}
75
76static void au1xxx_gpio1_write(unsigned gpio, int value)
77{
78 if (value)
79 gpio1->outputset = (0x01 << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000080 else
Florian Fainelli4ead1682007-05-22 21:44:42 +020081 /* Output a zero */
82 gpio1->outputclr = (0x01 << gpio);
Pete Popov2cce8262005-09-18 11:18:10 +000083}
84
Florian Fainelli4ead1682007-05-22 21:44:42 +020085static int au1xxx_gpio1_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +000086{
Florian Fainelli4ead1682007-05-22 21:44:42 +020087 gpio1->pininputen = (0x01 << gpio);
88 return 0;
Pete Popov2cce8262005-09-18 11:18:10 +000089}
90
Florian Fainelli4ead1682007-05-22 21:44:42 +020091static int au1xxx_gpio1_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +000092{
Florian Fainelli4ead1682007-05-22 21:44:42 +020093 gpio1->trioutclr = (0x01 & gpio);
Bruno Randolf44ce1712008-09-23 19:48:36 +020094 au1xxx_gpio1_write(gpio, value);
Florian Fainelli4ead1682007-05-22 21:44:42 +020095 return 0;
96}
97
98int au1xxx_gpio_get_value(unsigned gpio)
99{
100 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000101#if defined(CONFIG_SOC_AU1000)
102 return 0;
103#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200104 return au1xxx_gpio2_read(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000105#endif
106 else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200107 return au1xxx_gpio1_read(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000108}
Florian Fainelli4ead1682007-05-22 21:44:42 +0200109EXPORT_SYMBOL(au1xxx_gpio_get_value);
110
111void au1xxx_gpio_set_value(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +0000112{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200113 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000114#if defined(CONFIG_SOC_AU1000)
115 ;
116#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200117 au1xxx_gpio2_write(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000118#endif
119 else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200120 au1xxx_gpio1_write(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000121}
Florian Fainelli4ead1682007-05-22 21:44:42 +0200122EXPORT_SYMBOL(au1xxx_gpio_set_value);
123
124int au1xxx_gpio_direction_input(unsigned gpio)
Pete Popov2cce8262005-09-18 11:18:10 +0000125{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200126 if (gpio >= AU1XXX_GPIO_BASE)
Pete Popov2cce8262005-09-18 11:18:10 +0000127#if defined(CONFIG_SOC_AU1000)
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900128 return -ENODEV;
Pete Popov2cce8262005-09-18 11:18:10 +0000129#else
Florian Fainelli4ead1682007-05-22 21:44:42 +0200130 return au1xxx_gpio2_direction_input(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000131#endif
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900132
133 return au1xxx_gpio1_direction_input(gpio);
Pete Popov2cce8262005-09-18 11:18:10 +0000134}
Florian Fainelli4ead1682007-05-22 21:44:42 +0200135EXPORT_SYMBOL(au1xxx_gpio_direction_input);
136
137int au1xxx_gpio_direction_output(unsigned gpio, int value)
Pete Popov2cce8262005-09-18 11:18:10 +0000138{
Florian Fainelli4ead1682007-05-22 21:44:42 +0200139 if (gpio >= AU1XXX_GPIO_BASE)
140#if defined(CONFIG_SOC_AU1000)
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900141 return -ENODEV;
Florian Fainelli4ead1682007-05-22 21:44:42 +0200142#else
143 return au1xxx_gpio2_direction_output(gpio, value);
144#endif
Yoichi Yuasa7acae222007-08-02 12:48:00 +0900145
146 return au1xxx_gpio1_direction_output(gpio, value);
Pete Popov2cce8262005-09-18 11:18:10 +0000147}
Florian Fainelli4ead1682007-05-22 21:44:42 +0200148EXPORT_SYMBOL(au1xxx_gpio_direction_output);