1 This patch adds a new GPIO driver for the RDC321x SoC GPIO controller.
3 Signed-off-by: Florian Fainelli <florian@openwrt.org>
6 - initialize spinlock earlier
7 - do not declare and assign gpch variables on the same line
8 - use the pci_dev pointer passed as platform data
9 - replaced rdc321x_pci_{read,write}
11 --- a/drivers/gpio/Kconfig
12 +++ b/drivers/gpio/Kconfig
13 @@ -161,6 +161,14 @@ config GPIO_BT8XX
18 + tristate "RDC R-321x GPIO support"
19 + depends on PCI && GPIOLIB
22 + Support for the RDC R321x SoC GPIOs over southbridge
23 + PCI configuration space.
25 comment "SPI GPIO expanders:"
28 --- a/drivers/gpio/Makefile
29 +++ b/drivers/gpio/Makefile
30 @@ -12,3 +12,4 @@ obj-$(CONFIG_GPIO_PCF857X) += pcf857x.o
31 obj-$(CONFIG_GPIO_TWL4030) += twl4030-gpio.o
32 obj-$(CONFIG_GPIO_XILINX) += xilinx_gpio.o
33 obj-$(CONFIG_GPIO_BT8XX) += bt8xxgpio.o
34 +obj-$(CONFIG_GPIO_RDC321X) += rdc321x-gpio.o
36 +++ b/drivers/gpio/rdc321x-gpio.c
39 + * RDC321x GPIO driver
41 + * Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
42 + * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
44 + * This program is free software; you can redistribute it and/or modify
45 + * it under the terms of the GNU General Public License as published by
46 + * the Free Software Foundation; either version 2 of the License, or
47 + * (at your option) any later version.
49 + * This program is distributed in the hope that it will be useful,
50 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 + * GNU General Public License for more details.
54 + * You should have received a copy of the GNU General Public License
55 + * along with this program; if not, write to the Free Software
56 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
59 +#include <linux/module.h>
60 +#include <linux/kernel.h>
61 +#include <linux/init.h>
62 +#include <linux/spinlock.h>
63 +#include <linux/platform_device.h>
64 +#include <linux/pci.h>
65 +#include <linux/gpio.h>
66 +#include <linux/mfd/rdc321x.h>
68 +struct rdc321x_gpio {
70 + struct pci_dev *sb_pdev;
76 + struct gpio_chip chip;
80 +static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
82 + struct rdc321x_gpio *gpch;
86 + gpch = container_of(chip, struct rdc321x_gpio, chip);
87 + reg = gpio < 32 ? gpch->reg1_data_base : gpch->reg2_data_base;
89 + spin_lock(&gpch->lock);
90 + pci_write_config_dword(gpch->sb_pdev, reg,
91 + gpch->data_reg[gpio < 32 ? 0 : 1]);
92 + pci_read_config_dword(gpch->sb_pdev, reg, &value);
93 + spin_unlock(&gpch->lock);
95 + return (1 << (gpio & 0x1f)) & value ? 1 : 0;
98 +static void rdc_gpio_set_value_impl(struct gpio_chip *chip,
99 + unsigned gpio, int value)
101 + struct rdc321x_gpio *gpch;
102 + int reg = (gpio < 32) ? 0 : 1;
104 + gpch = container_of(chip, struct rdc321x_gpio, chip);
107 + gpch->data_reg[reg] |= 1 << (gpio & 0x1f);
109 + gpch->data_reg[reg] &= ~(1 << (gpio & 0x1f));
111 + pci_write_config_dword(gpch->sb_pdev,
112 + reg ? gpch->reg2_data_base : gpch->reg1_data_base,
113 + gpch->data_reg[reg]);
116 +/* set GPIO pin to value */
117 +static void rdc_gpio_set_value(struct gpio_chip *chip,
118 + unsigned gpio, int value)
120 + struct rdc321x_gpio *gpch;
122 + gpch = container_of(chip, struct rdc321x_gpio, chip);
123 + spin_lock(&gpch->lock);
124 + rdc_gpio_set_value_impl(chip, gpio, value);
125 + spin_unlock(&gpch->lock);
128 +static int rdc_gpio_config(struct gpio_chip *chip,
129 + unsigned gpio, int value)
131 + struct rdc321x_gpio *gpch;
135 + gpch = container_of(chip, struct rdc321x_gpio, chip);
137 + spin_lock(&gpch->lock);
138 + err = pci_read_config_dword(gpch->sb_pdev, gpio < 32 ?
139 + gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, ®);
143 + reg |= 1 << (gpio & 0x1f);
145 + err = pci_write_config_dword(gpch->sb_pdev, gpio < 32 ?
146 + gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, reg);
150 + rdc_gpio_set_value_impl(chip, gpio, value);
153 + spin_unlock(&gpch->lock);
158 +/* configure GPIO pin as input */
159 +static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
161 + return rdc_gpio_config(chip, gpio, 1);
165 + * Cache the initial value of both GPIO data registers
167 +static int __devinit rdc321x_gpio_probe(struct platform_device *pdev)
170 + struct resource *r;
171 + struct rdc321x_gpio *rdc321x_gpio_dev;
172 + struct rdc321x_gpio_pdata *pdata;
174 + pdata = pdev->dev.driver_data;
176 + dev_err(&pdev->dev, "no platform data supplied\n");
180 + rdc321x_gpio_dev = kzalloc(sizeof(struct rdc321x_gpio), GFP_KERNEL);
181 + if (!rdc321x_gpio_dev) {
182 + dev_err(&pdev->dev, "failed to allocate private data\n");
186 + r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg1");
188 + dev_err(&pdev->dev, "failed to get gpio-reg1 resource\n");
193 + spin_lock_init(&rdc321x_gpio_dev->lock);
194 + rdc321x_gpio_dev->sb_pdev = pdata->sb_pdev;
195 + rdc321x_gpio_dev->reg1_ctrl_base = r->start;
196 + rdc321x_gpio_dev->reg1_data_base = r->start + 0x4;
198 + r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg2");
200 + dev_err(&pdev->dev, "failed to get gpio-reg2 resource\n");
205 + rdc321x_gpio_dev->reg2_ctrl_base = r->start;
206 + rdc321x_gpio_dev->reg2_data_base = r->start + 0x4;
208 + rdc321x_gpio_dev->chip.label = "rdc321x-gpio";
209 + rdc321x_gpio_dev->chip.direction_input = rdc_gpio_direction_input;
210 + rdc321x_gpio_dev->chip.direction_output = rdc_gpio_config;
211 + rdc321x_gpio_dev->chip.get = rdc_gpio_get_value;
212 + rdc321x_gpio_dev->chip.set = rdc_gpio_set_value;
213 + rdc321x_gpio_dev->chip.base = 0;
214 + rdc321x_gpio_dev->chip.ngpio = pdata->max_gpios;
216 + platform_set_drvdata(pdev, rdc321x_gpio_dev);
218 + /* This might not be, what others (BIOS, bootloader, etc.)
219 + wrote to these registers before, but it's a good guess. Still
220 + better than just using 0xffffffff. */
221 + err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
222 + rdc321x_gpio_dev->reg1_data_base,
223 + &rdc321x_gpio_dev->data_reg[0]);
227 + err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
228 + rdc321x_gpio_dev->reg2_data_base,
229 + &rdc321x_gpio_dev->data_reg[1]);
233 + dev_info(&pdev->dev, "registering %d GPIOs\n",
234 + rdc321x_gpio_dev->chip.ngpio);
235 + return gpiochip_add(&rdc321x_gpio_dev->chip);
238 + platform_set_drvdata(pdev, NULL);
240 + kfree(rdc321x_gpio_dev);
244 +static int __devexit rdc321x_gpio_remove(struct platform_device *pdev)
247 + struct rdc321x_gpio *rdc321x_gpio_dev = platform_get_drvdata(pdev);
249 + ret = gpiochip_remove(&rdc321x_gpio_dev->chip);
251 + dev_err(&pdev->dev, "failed to unregister chip\n");
253 + kfree(rdc321x_gpio_dev);
254 + platform_set_drvdata(pdev, NULL);
259 +static struct platform_driver rdc321x_gpio_driver = {
260 + .driver.name = "rdc321x-gpio",
261 + .driver.owner = THIS_MODULE,
262 + .probe = rdc321x_gpio_probe,
263 + .remove = __devexit_p(rdc321x_gpio_remove),
266 +static int __init rdc321x_gpio_init(void)
268 + return platform_driver_register(&rdc321x_gpio_driver);
271 +static void __exit rdc321x_gpio_exit(void)
273 + platform_driver_unregister(&rdc321x_gpio_driver);
276 +module_init(rdc321x_gpio_init);
277 +module_exit(rdc321x_gpio_exit);
279 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
280 +MODULE_DESCRIPTION("RDC321x GPIO driver");
281 +MODULE_LICENSE("GPL");
282 +MODULE_ALIAS("platform:rdc321x-gpio");