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 Index: linux-2.6.32.10/drivers/gpio/Kconfig
12 ===================================================================
13 --- linux-2.6.32.10.orig/drivers/gpio/Kconfig 2010-05-15 22:54:31.000000000 +0200
14 +++ linux-2.6.32.10/drivers/gpio/Kconfig 2010-05-15 22:54:51.000000000 +0200
17 Say Y here to support Intel Moorestown platform GPIO.
20 + tristate "RDC R-321x GPIO support"
21 + depends on PCI && GPIOLIB
24 + Support for the RDC R321x SoC GPIOs over southbridge
25 + PCI configuration space.
27 comment "SPI GPIO expanders:"
30 Index: linux-2.6.32.10/drivers/gpio/Makefile
31 ===================================================================
32 --- linux-2.6.32.10.orig/drivers/gpio/Makefile 2010-05-15 22:54:31.000000000 +0200
33 +++ linux-2.6.32.10/drivers/gpio/Makefile 2010-05-15 22:54:51.000000000 +0200
35 obj-$(CONFIG_GPIO_BT8XX) += bt8xxgpio.o
36 obj-$(CONFIG_GPIO_VR41XX) += vr41xx_giu.o
37 obj-$(CONFIG_GPIO_WM831X) += wm831x-gpio.o
38 +obj-$(CONFIG_GPIO_RDC321X) += rdc321x-gpio.o
39 Index: linux-2.6.32.10/drivers/gpio/rdc321x-gpio.c
40 ===================================================================
41 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
42 +++ linux-2.6.32.10/drivers/gpio/rdc321x-gpio.c 2010-05-15 22:55:10.000000000 +0200
45 + * RDC321x GPIO driver
47 + * Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
48 + * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
50 + * This program is free software; you can redistribute it and/or modify
51 + * it under the terms of the GNU General Public License as published by
52 + * the Free Software Foundation; either version 2 of the License, or
53 + * (at your option) any later version.
55 + * This program is distributed in the hope that it will be useful,
56 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 + * GNU General Public License for more details.
60 + * You should have received a copy of the GNU General Public License
61 + * along with this program; if not, write to the Free Software
62 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
65 +#include <linux/module.h>
66 +#include <linux/kernel.h>
67 +#include <linux/init.h>
68 +#include <linux/spinlock.h>
69 +#include <linux/platform_device.h>
70 +#include <linux/pci.h>
71 +#include <linux/gpio.h>
72 +#include <linux/mfd/rdc321x.h>
74 +struct rdc321x_gpio {
76 + struct pci_dev *sb_pdev;
82 + struct gpio_chip chip;
86 +static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
88 + struct rdc321x_gpio *gpch;
92 + gpch = container_of(chip, struct rdc321x_gpio, chip);
93 + reg = gpio < 32 ? gpch->reg1_data_base : gpch->reg2_data_base;
95 + spin_lock(&gpch->lock);
96 + pci_write_config_dword(gpch->sb_pdev, reg,
97 + gpch->data_reg[gpio < 32 ? 0 : 1]);
98 + pci_read_config_dword(gpch->sb_pdev, reg, &value);
99 + spin_unlock(&gpch->lock);
101 + return (1 << (gpio & 0x1f)) & value ? 1 : 0;
104 +static void rdc_gpio_set_value_impl(struct gpio_chip *chip,
105 + unsigned gpio, int value)
107 + struct rdc321x_gpio *gpch;
108 + int reg = (gpio < 32) ? 0 : 1;
110 + gpch = container_of(chip, struct rdc321x_gpio, chip);
113 + gpch->data_reg[reg] |= 1 << (gpio & 0x1f);
115 + gpch->data_reg[reg] &= ~(1 << (gpio & 0x1f));
117 + pci_write_config_dword(gpch->sb_pdev,
118 + reg ? gpch->reg2_data_base : gpch->reg1_data_base,
119 + gpch->data_reg[reg]);
122 +/* set GPIO pin to value */
123 +static void rdc_gpio_set_value(struct gpio_chip *chip,
124 + unsigned gpio, int value)
126 + struct rdc321x_gpio *gpch;
128 + gpch = container_of(chip, struct rdc321x_gpio, chip);
129 + spin_lock(&gpch->lock);
130 + rdc_gpio_set_value_impl(chip, gpio, value);
131 + spin_unlock(&gpch->lock);
134 +static int rdc_gpio_config(struct gpio_chip *chip,
135 + unsigned gpio, int value)
137 + struct rdc321x_gpio *gpch;
141 + gpch = container_of(chip, struct rdc321x_gpio, chip);
143 + spin_lock(&gpch->lock);
144 + err = pci_read_config_dword(gpch->sb_pdev, gpio < 32 ?
145 + gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, ®);
149 + reg |= 1 << (gpio & 0x1f);
151 + err = pci_write_config_dword(gpch->sb_pdev, gpio < 32 ?
152 + gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, reg);
156 + rdc_gpio_set_value_impl(chip, gpio, value);
159 + spin_unlock(&gpch->lock);
164 +/* configure GPIO pin as input */
165 +static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
167 + return rdc_gpio_config(chip, gpio, 1);
171 + * Cache the initial value of both GPIO data registers
173 +static int __devinit rdc321x_gpio_probe(struct platform_device *pdev)
176 + struct resource *r;
177 + struct rdc321x_gpio *rdc321x_gpio_dev;
178 + struct rdc321x_gpio_pdata *pdata;
180 + pdata = platform_get_drvdata(pdev);
182 + dev_err(&pdev->dev, "no platform data supplied\n");
186 + rdc321x_gpio_dev = kzalloc(sizeof(struct rdc321x_gpio), GFP_KERNEL);
187 + if (!rdc321x_gpio_dev) {
188 + dev_err(&pdev->dev, "failed to allocate private data\n");
192 + r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg1");
194 + dev_err(&pdev->dev, "failed to get gpio-reg1 resource\n");
199 + spin_lock_init(&rdc321x_gpio_dev->lock);
200 + rdc321x_gpio_dev->sb_pdev = pdata->sb_pdev;
201 + rdc321x_gpio_dev->reg1_ctrl_base = r->start;
202 + rdc321x_gpio_dev->reg1_data_base = r->start + 0x4;
204 + r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg2");
206 + dev_err(&pdev->dev, "failed to get gpio-reg2 resource\n");
211 + rdc321x_gpio_dev->reg2_ctrl_base = r->start;
212 + rdc321x_gpio_dev->reg2_data_base = r->start + 0x4;
214 + rdc321x_gpio_dev->chip.label = "rdc321x-gpio";
215 + rdc321x_gpio_dev->chip.direction_input = rdc_gpio_direction_input;
216 + rdc321x_gpio_dev->chip.direction_output = rdc_gpio_config;
217 + rdc321x_gpio_dev->chip.get = rdc_gpio_get_value;
218 + rdc321x_gpio_dev->chip.set = rdc_gpio_set_value;
219 + rdc321x_gpio_dev->chip.base = 0;
220 + rdc321x_gpio_dev->chip.ngpio = pdata->max_gpios;
222 + platform_set_drvdata(pdev, rdc321x_gpio_dev);
224 + /* This might not be, what others (BIOS, bootloader, etc.)
225 + wrote to these registers before, but it's a good guess. Still
226 + better than just using 0xffffffff. */
227 + err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
228 + rdc321x_gpio_dev->reg1_data_base,
229 + &rdc321x_gpio_dev->data_reg[0]);
233 + err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
234 + rdc321x_gpio_dev->reg2_data_base,
235 + &rdc321x_gpio_dev->data_reg[1]);
239 + dev_info(&pdev->dev, "registering %d GPIOs\n",
240 + rdc321x_gpio_dev->chip.ngpio);
241 + return gpiochip_add(&rdc321x_gpio_dev->chip);
244 + platform_set_drvdata(pdev, NULL);
246 + kfree(rdc321x_gpio_dev);
250 +static int __devexit rdc321x_gpio_remove(struct platform_device *pdev)
253 + struct rdc321x_gpio *rdc321x_gpio_dev = platform_get_drvdata(pdev);
255 + ret = gpiochip_remove(&rdc321x_gpio_dev->chip);
257 + dev_err(&pdev->dev, "failed to unregister chip\n");
259 + kfree(rdc321x_gpio_dev);
260 + platform_set_drvdata(pdev, NULL);
265 +static struct platform_driver rdc321x_gpio_driver = {
266 + .driver.name = "rdc321x-gpio",
267 + .driver.owner = THIS_MODULE,
268 + .probe = rdc321x_gpio_probe,
269 + .remove = __devexit_p(rdc321x_gpio_remove),
272 +static int __init rdc321x_gpio_init(void)
274 + return platform_driver_register(&rdc321x_gpio_driver);
277 +static void __exit rdc321x_gpio_exit(void)
279 + platform_driver_unregister(&rdc321x_gpio_driver);
282 +module_init(rdc321x_gpio_init);
283 +module_exit(rdc321x_gpio_exit);
285 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
286 +MODULE_DESCRIPTION("RDC321x GPIO driver");
287 +MODULE_LICENSE("GPL");
288 +MODULE_ALIAS("platform:rdc321x-gpio");