| 1 | This patch adds a new GPIO driver for the RDC321x SoC GPIO controller. |
| 2 | |
| 3 | Signed-off-by: Florian Fainelli <florian@openwrt.org> |
| 4 | --- |
| 5 | Changes from v2: |
| 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} |
| 10 | |
| 11 | --- a/drivers/gpio/Kconfig |
| 12 | +++ b/drivers/gpio/Kconfig |
| 13 | @@ -196,6 +196,14 @@ config GPIO_LANGWELL |
| 14 | help |
| 15 | Say Y here to support Intel Moorestown platform GPIO. |
| 16 | |
| 17 | +config GPIO_RDC321X |
| 18 | + tristate "RDC R-321x GPIO support" |
| 19 | + depends on PCI && GPIOLIB |
| 20 | + select MFD_RDC321X |
| 21 | + help |
| 22 | + Support for the RDC R321x SoC GPIOs over southbridge |
| 23 | + PCI configuration space. |
| 24 | + |
| 25 | comment "SPI GPIO expanders:" |
| 26 | |
| 27 | config GPIO_MAX7301 |
| 28 | --- a/drivers/gpio/Makefile |
| 29 | +++ b/drivers/gpio/Makefile |
| 30 | @@ -19,3 +19,4 @@ obj-$(CONFIG_GPIO_XILINX) += xilinx_gpio |
| 31 | obj-$(CONFIG_GPIO_BT8XX) += bt8xxgpio.o |
| 32 | obj-$(CONFIG_GPIO_VR41XX) += vr41xx_giu.o |
| 33 | obj-$(CONFIG_GPIO_WM831X) += wm831x-gpio.o |
| 34 | +obj-$(CONFIG_GPIO_RDC321X) += rdc321x-gpio.o |
| 35 | --- /dev/null |
| 36 | +++ b/drivers/gpio/rdc321x-gpio.c |
| 37 | @@ -0,0 +1,245 @@ |
| 38 | +/* |
| 39 | + * RDC321x GPIO driver |
| 40 | + * |
| 41 | + * Copyright (C) 2008, Volker Weiss <dev@tintuc.de> |
| 42 | + * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org> |
| 43 | + * |
| 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. |
| 48 | + * |
| 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. |
| 53 | + * |
| 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. |
| 57 | + * |
| 58 | + */ |
| 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> |
| 67 | + |
| 68 | +struct rdc321x_gpio { |
| 69 | + spinlock_t lock; |
| 70 | + struct pci_dev *sb_pdev; |
| 71 | + u32 data_reg[2]; |
| 72 | + int reg1_ctrl_base; |
| 73 | + int reg1_data_base; |
| 74 | + int reg2_ctrl_base; |
| 75 | + int reg2_data_base; |
| 76 | + struct gpio_chip chip; |
| 77 | +}; |
| 78 | + |
| 79 | +/* read GPIO pin */ |
| 80 | +static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio) |
| 81 | +{ |
| 82 | + struct rdc321x_gpio *gpch; |
| 83 | + u32 value = 0; |
| 84 | + int reg; |
| 85 | + |
| 86 | + gpch = container_of(chip, struct rdc321x_gpio, chip); |
| 87 | + reg = gpio < 32 ? gpch->reg1_data_base : gpch->reg2_data_base; |
| 88 | + |
| 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); |
| 94 | + |
| 95 | + return (1 << (gpio & 0x1f)) & value ? 1 : 0; |
| 96 | +} |
| 97 | + |
| 98 | +static void rdc_gpio_set_value_impl(struct gpio_chip *chip, |
| 99 | + unsigned gpio, int value) |
| 100 | +{ |
| 101 | + struct rdc321x_gpio *gpch; |
| 102 | + int reg = (gpio < 32) ? 0 : 1; |
| 103 | + |
| 104 | + gpch = container_of(chip, struct rdc321x_gpio, chip); |
| 105 | + |
| 106 | + if (value) |
| 107 | + gpch->data_reg[reg] |= 1 << (gpio & 0x1f); |
| 108 | + else |
| 109 | + gpch->data_reg[reg] &= ~(1 << (gpio & 0x1f)); |
| 110 | + |
| 111 | + pci_write_config_dword(gpch->sb_pdev, |
| 112 | + reg ? gpch->reg2_data_base : gpch->reg1_data_base, |
| 113 | + gpch->data_reg[reg]); |
| 114 | +} |
| 115 | + |
| 116 | +/* set GPIO pin to value */ |
| 117 | +static void rdc_gpio_set_value(struct gpio_chip *chip, |
| 118 | + unsigned gpio, int value) |
| 119 | +{ |
| 120 | + struct rdc321x_gpio *gpch; |
| 121 | + |
| 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); |
| 126 | +} |
| 127 | + |
| 128 | +static int rdc_gpio_config(struct gpio_chip *chip, |
| 129 | + unsigned gpio, int value) |
| 130 | +{ |
| 131 | + struct rdc321x_gpio *gpch; |
| 132 | + int err; |
| 133 | + u32 reg; |
| 134 | + |
| 135 | + gpch = container_of(chip, struct rdc321x_gpio, chip); |
| 136 | + |
| 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, ®); |
| 140 | + if (err) |
| 141 | + goto unlock; |
| 142 | + |
| 143 | + reg |= 1 << (gpio & 0x1f); |
| 144 | + |
| 145 | + err = pci_write_config_dword(gpch->sb_pdev, gpio < 32 ? |
| 146 | + gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, reg); |
| 147 | + if (err) |
| 148 | + goto unlock; |
| 149 | + |
| 150 | + rdc_gpio_set_value_impl(chip, gpio, value); |
| 151 | + |
| 152 | +unlock: |
| 153 | + spin_unlock(&gpch->lock); |
| 154 | + |
| 155 | + return err; |
| 156 | +} |
| 157 | + |
| 158 | +/* configure GPIO pin as input */ |
| 159 | +static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 160 | +{ |
| 161 | + return rdc_gpio_config(chip, gpio, 1); |
| 162 | +} |
| 163 | + |
| 164 | +/* |
| 165 | + * Cache the initial value of both GPIO data registers |
| 166 | + */ |
| 167 | +static int __devinit rdc321x_gpio_probe(struct platform_device *pdev) |
| 168 | +{ |
| 169 | + int err; |
| 170 | + struct resource *r; |
| 171 | + struct rdc321x_gpio *rdc321x_gpio_dev; |
| 172 | + struct rdc321x_gpio_pdata *pdata; |
| 173 | + |
| 174 | + pdata = platform_get_drvdata(pdev); |
| 175 | + if (!pdata) { |
| 176 | + dev_err(&pdev->dev, "no platform data supplied\n"); |
| 177 | + return -ENODEV; |
| 178 | + } |
| 179 | + |
| 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"); |
| 183 | + return -ENOMEM; |
| 184 | + } |
| 185 | + |
| 186 | + r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg1"); |
| 187 | + if (!r) { |
| 188 | + dev_err(&pdev->dev, "failed to get gpio-reg1 resource\n"); |
| 189 | + err = -ENODEV; |
| 190 | + goto out_free; |
| 191 | + } |
| 192 | + |
| 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; |
| 197 | + |
| 198 | + r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg2"); |
| 199 | + if (!r) { |
| 200 | + dev_err(&pdev->dev, "failed to get gpio-reg2 resource\n"); |
| 201 | + err = -ENODEV; |
| 202 | + goto out_free; |
| 203 | + } |
| 204 | + |
| 205 | + rdc321x_gpio_dev->reg2_ctrl_base = r->start; |
| 206 | + rdc321x_gpio_dev->reg2_data_base = r->start + 0x4; |
| 207 | + |
| 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; |
| 215 | + |
| 216 | + platform_set_drvdata(pdev, rdc321x_gpio_dev); |
| 217 | + |
| 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]); |
| 224 | + if (err) |
| 225 | + goto out_drvdata; |
| 226 | + |
| 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]); |
| 230 | + if (err) |
| 231 | + goto out_drvdata; |
| 232 | + |
| 233 | + dev_info(&pdev->dev, "registering %d GPIOs\n", |
| 234 | + rdc321x_gpio_dev->chip.ngpio); |
| 235 | + return gpiochip_add(&rdc321x_gpio_dev->chip); |
| 236 | + |
| 237 | +out_drvdata: |
| 238 | + platform_set_drvdata(pdev, NULL); |
| 239 | +out_free: |
| 240 | + kfree(rdc321x_gpio_dev); |
| 241 | + return err; |
| 242 | +} |
| 243 | + |
| 244 | +static int __devexit rdc321x_gpio_remove(struct platform_device *pdev) |
| 245 | +{ |
| 246 | + int ret; |
| 247 | + struct rdc321x_gpio *rdc321x_gpio_dev = platform_get_drvdata(pdev); |
| 248 | + |
| 249 | + ret = gpiochip_remove(&rdc321x_gpio_dev->chip); |
| 250 | + if (ret) |
| 251 | + dev_err(&pdev->dev, "failed to unregister chip\n"); |
| 252 | + |
| 253 | + kfree(rdc321x_gpio_dev); |
| 254 | + platform_set_drvdata(pdev, NULL); |
| 255 | + |
| 256 | + return ret; |
| 257 | +} |
| 258 | + |
| 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), |
| 264 | +}; |
| 265 | + |
| 266 | +static int __init rdc321x_gpio_init(void) |
| 267 | +{ |
| 268 | + return platform_driver_register(&rdc321x_gpio_driver); |
| 269 | +} |
| 270 | + |
| 271 | +static void __exit rdc321x_gpio_exit(void) |
| 272 | +{ |
| 273 | + platform_driver_unregister(&rdc321x_gpio_driver); |
| 274 | +} |
| 275 | + |
| 276 | +module_init(rdc321x_gpio_init); |
| 277 | +module_exit(rdc321x_gpio_exit); |
| 278 | + |
| 279 | +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); |
| 280 | +MODULE_DESCRIPTION("RDC321x GPIO driver"); |
| 281 | +MODULE_LICENSE("GPL"); |
| 282 | +MODULE_ALIAS("platform:rdc321x-gpio"); |
| 283 | |