2 * GPIO support for RDC SoC R3210/R8610
4 * Copyright (C) 2007, Florian Fainelli <florian@openwrt.org>
5 * Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/spinlock.h>
26 #include <linux/types.h>
27 #include <linux/module.h>
28 #include <linux/gpio.h>
30 #include <asm/rdc321x_gpio.h>
31 #include <asm/rdc321x_defs.h>
34 /* spin lock to protect our private copy of GPIO data register plus
35 the access to PCI conf registers. */
36 static DEFINE_SPINLOCK(gpio_lock);
38 /* copy of GPIO data registers */
39 static u32 gpio_data_reg1;
40 static u32 gpio_data_reg2;
42 static inline void rdc321x_conf_write(unsigned addr, u32 value)
44 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
45 outl(value, RDC3210_CFGREG_DATA);
48 static inline void rdc321x_conf_or(unsigned addr, u32 value)
50 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
51 value |= inl(RDC3210_CFGREG_DATA);
52 outl(value, RDC3210_CFGREG_DATA);
55 static inline u32 rdc321x_conf_read(unsigned addr)
57 outl((1 << 31) | (7 << 11) | addr, RDC3210_CFGREG_ADDR);
59 return inl(RDC3210_CFGREG_DATA);
62 /* configure pin as GPIO */
63 static void rdc321x_configure_gpio(unsigned gpio)
67 spin_lock_irqsave(&gpio_lock, flags);
68 rdc321x_conf_or(gpio < 32
69 ? RDC321X_GPIO_CTRL_REG1 : RDC321X_GPIO_CTRL_REG2,
71 spin_unlock_irqrestore(&gpio_lock, flags);
75 static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
80 spin_lock_irqsave(&gpio_lock, flags);
81 reg = rdc321x_conf_read(gpio < 32
82 ? RDC321X_GPIO_DATA_REG1 : RDC321X_GPIO_DATA_REG2);
83 spin_unlock_irqrestore(&gpio_lock, flags);
85 return (1 << (gpio & 0x1f)) & reg ? 1 : 0;
88 /* set GPIO pin to value */
89 static void rdc_gpio_set_value(struct gpio_chip *chip,
90 unsigned gpio, int value)
95 reg = 1 << (gpio & 0x1f);
97 spin_lock_irqsave(&gpio_lock, flags);
99 gpio_data_reg1 |= reg;
101 gpio_data_reg1 &= ~reg;
102 rdc321x_conf_write(RDC321X_GPIO_DATA_REG1, gpio_data_reg1);
103 spin_unlock_irqrestore(&gpio_lock, flags);
105 spin_lock_irqsave(&gpio_lock, flags);
107 gpio_data_reg2 |= reg;
109 gpio_data_reg2 &= ~reg;
110 rdc321x_conf_write(RDC321X_GPIO_DATA_REG2, gpio_data_reg2);
111 spin_unlock_irqrestore(&gpio_lock, flags);
115 /* configure GPIO pin as input */
116 static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
118 rdc321x_configure_gpio(gpio);
123 /* configure GPIO pin as output and set value */
124 static int rdc_gpio_direction_output(struct gpio_chip *chip,
125 unsigned gpio, int value)
127 rdc321x_configure_gpio(gpio);
128 gpio_set_value(gpio, value);
133 static struct gpio_chip rdc321x_gpio_chip = {
134 .label = "rdc321x-gpio",
135 .direction_input = rdc_gpio_direction_input,
136 .direction_output = rdc_gpio_direction_output,
137 .get = rdc_gpio_get_value,
138 .set = rdc_gpio_set_value,
140 .ngpio = RDC321X_MAX_GPIO,
143 /* initially setup the 2 copies of the gpio data registers.
144 This function is called before the platform setup code. */
145 static int __init rdc321x_gpio_setup(void)
147 /* this might not be, what others (BIOS, bootloader, etc.)
148 wrote to these registers before, but it's a good guess. Still
149 better than just using 0xffffffff. */
151 gpio_data_reg1 = rdc321x_conf_read(RDC321X_GPIO_DATA_REG1);
152 gpio_data_reg2 = rdc321x_conf_read(RDC321X_GPIO_DATA_REG2);
154 printk(KERN_INFO "rdc321x: registering %d GPIOs\n", rdc321x_gpio_chip.ngpio);
155 return gpiochip_add(&rdc321x_gpio_chip);
158 arch_initcall(rdc321x_gpio_setup);