Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for basic memory-mapped GPIO controllers. |
| 3 | * |
| 4 | * Copyright 2008 MontaVista Software, Inc. |
| 5 | * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`....... |
| 13 | * ...`` ```````.. |
| 14 | * ..The simplest form of a GPIO controller that the driver supports is`` |
| 15 | * `.just a single "data" register, where GPIO state can be read and/or ` |
| 16 | * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.``````` |
| 17 | * ````````` |
| 18 | ___ |
| 19 | _/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,... |
| 20 | __________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO . |
| 21 | o ` ~~~~\___/~~~~ ` controller in FPGA is ,.` |
| 22 | `....trivial..'~`.```.``` |
| 23 | * ``````` |
| 24 | * .```````~~~~`..`.``.``. |
| 25 | * . The driver supports `... ,..```.`~~~```````````````....````.``,, |
| 26 | * . big-endian notation, just`. .. A bit more sophisticated controllers , |
| 27 | * . register the device with -be`. .with a pair of set/clear-bit registers , |
| 28 | * `.. suffix. ```~~`````....`.` . affecting the data register and the .` |
| 29 | * ``.`.``...``` ```.. output pins are also supported.` |
| 30 | * ^^ `````.`````````.,``~``~``~~`````` |
| 31 | * . ^^ |
| 32 | * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`.. |
| 33 | * .. The expectation is that in at least some cases . ,-~~~-, |
| 34 | * .this will be used with roll-your-own ASIC/FPGA .` \ / |
| 35 | * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ / |
| 36 | * ..````````......``````````` \o_ |
| 37 | * | |
| 38 | * ^^ / \ |
| 39 | * |
| 40 | * ...`````~~`.....``.`..........``````.`.``.```........``. |
| 41 | * ` 8, 16, 32 and 64 bits registers are supported, and``. |
| 42 | * . the number of GPIOs is determined by the width of ~ |
| 43 | * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~ |
| 44 | * `.......````.``` |
| 45 | */ |
| 46 | |
| 47 | #include <linux/init.h> |
| 48 | #include <linux/bug.h> |
| 49 | #include <linux/kernel.h> |
| 50 | #include <linux/module.h> |
| 51 | #include <linux/spinlock.h> |
| 52 | #include <linux/compiler.h> |
| 53 | #include <linux/types.h> |
| 54 | #include <linux/errno.h> |
| 55 | #include <linux/log2.h> |
| 56 | #include <linux/ioport.h> |
| 57 | #include <linux/io.h> |
| 58 | #include <linux/gpio.h> |
| 59 | #include <linux/slab.h> |
| 60 | #include <linux/platform_device.h> |
| 61 | #include <linux/mod_devicetable.h> |
| 62 | #include <linux/basic_mmio_gpio.h> |
| 63 | |
| 64 | struct bgpio_chip { |
| 65 | struct gpio_chip gc; |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 66 | |
| 67 | unsigned long (*read_reg)(void __iomem *reg); |
| 68 | void (*write_reg)(void __iomem *reg, unsigned long data); |
| 69 | |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 70 | void __iomem *reg_dat; |
| 71 | void __iomem *reg_set; |
| 72 | void __iomem *reg_clr; |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 73 | void __iomem *reg_dir; |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 74 | |
| 75 | /* Number of bits (GPIOs): <register width> * 8. */ |
| 76 | int bits; |
| 77 | |
| 78 | /* |
| 79 | * Some GPIO controllers work with the big-endian bits notation, |
| 80 | * e.g. in a 8-bits register, GPIO7 is the least significant bit. |
| 81 | */ |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 82 | unsigned long (*pin2mask)(struct bgpio_chip *bgc, unsigned int pin); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Used to lock bgpio_chip->data. Also, this is needed to keep |
| 86 | * shadowed and real data registers writes together. |
| 87 | */ |
| 88 | spinlock_t lock; |
| 89 | |
| 90 | /* Shadowed data register to clear/set bits safely. */ |
| 91 | unsigned long data; |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 92 | |
| 93 | /* Shadowed direction registers to clear/set direction safely. */ |
| 94 | unsigned long dir; |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static struct bgpio_chip *to_bgpio_chip(struct gpio_chip *gc) |
| 98 | { |
| 99 | return container_of(gc, struct bgpio_chip, gc); |
| 100 | } |
| 101 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 102 | static void bgpio_write8(void __iomem *reg, unsigned long data) |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 103 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 104 | writeb(data, reg); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 107 | static unsigned long bgpio_read8(void __iomem *reg) |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 108 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 109 | return readb(reg); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 112 | static void bgpio_write16(void __iomem *reg, unsigned long data) |
| 113 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 114 | writew(data, reg); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static unsigned long bgpio_read16(void __iomem *reg) |
| 118 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 119 | return readw(reg); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void bgpio_write32(void __iomem *reg, unsigned long data) |
| 123 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 124 | writel(data, reg); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static unsigned long bgpio_read32(void __iomem *reg) |
| 128 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 129 | return readl(reg); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | #if BITS_PER_LONG >= 64 |
| 133 | static void bgpio_write64(void __iomem *reg, unsigned long data) |
| 134 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 135 | writeq(data, reg); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static unsigned long bgpio_read64(void __iomem *reg) |
| 139 | { |
Jamie Iles | fd99623 | 2011-05-20 00:40:17 -0600 | [diff] [blame^] | 140 | return readq(reg); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 141 | } |
| 142 | #endif /* BITS_PER_LONG >= 64 */ |
| 143 | |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 144 | static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin) |
| 145 | { |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 146 | return 1 << pin; |
| 147 | } |
| 148 | |
| 149 | static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc, |
| 150 | unsigned int pin) |
| 151 | { |
| 152 | return 1 << (bgc->bits - 1 - pin); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int bgpio_get(struct gpio_chip *gc, unsigned int gpio) |
| 156 | { |
| 157 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 158 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 159 | return bgc->read_reg(bgc->reg_dat) & bgc->pin2mask(bgc, gpio); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) |
| 163 | { |
| 164 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 165 | unsigned long mask = bgc->pin2mask(bgc, gpio); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 166 | unsigned long flags; |
| 167 | |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 168 | spin_lock_irqsave(&bgc->lock, flags); |
| 169 | |
| 170 | if (val) |
| 171 | bgc->data |= mask; |
| 172 | else |
| 173 | bgc->data &= ~mask; |
| 174 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 175 | bgc->write_reg(bgc->reg_dat, bgc->data); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 176 | |
| 177 | spin_unlock_irqrestore(&bgc->lock, flags); |
| 178 | } |
| 179 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 180 | static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio, |
| 181 | int val) |
| 182 | { |
| 183 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 184 | unsigned long mask = bgc->pin2mask(bgc, gpio); |
| 185 | |
| 186 | if (val) |
| 187 | bgc->write_reg(bgc->reg_set, mask); |
| 188 | else |
| 189 | bgc->write_reg(bgc->reg_clr, mask); |
| 190 | } |
| 191 | |
Jamie Iles | dd86a0c | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 192 | static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val) |
| 193 | { |
| 194 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 195 | unsigned long mask = bgc->pin2mask(bgc, gpio); |
| 196 | unsigned long flags; |
| 197 | |
| 198 | spin_lock_irqsave(&bgc->lock, flags); |
| 199 | |
| 200 | if (val) |
| 201 | bgc->data |= mask; |
| 202 | else |
| 203 | bgc->data &= ~mask; |
| 204 | |
| 205 | bgc->write_reg(bgc->reg_set, bgc->data); |
| 206 | |
| 207 | spin_unlock_irqrestore(&bgc->lock, flags); |
| 208 | } |
| 209 | |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 210 | static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio) |
| 211 | { |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio, |
| 216 | int val) |
| 217 | { |
| 218 | gc->set(gc, gpio, val); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 223 | static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) |
| 224 | { |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 225 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 226 | unsigned long flags; |
| 227 | |
| 228 | spin_lock_irqsave(&bgc->lock, flags); |
| 229 | |
| 230 | bgc->dir &= ~bgc->pin2mask(bgc, gpio); |
| 231 | bgc->write_reg(bgc->reg_dir, bgc->dir); |
| 232 | |
| 233 | spin_unlock_irqrestore(&bgc->lock, flags); |
| 234 | |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) |
| 239 | { |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 240 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 241 | unsigned long flags; |
| 242 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 243 | gc->set(gc, gpio, val); |
| 244 | |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 245 | spin_lock_irqsave(&bgc->lock, flags); |
| 246 | |
| 247 | bgc->dir |= bgc->pin2mask(bgc, gpio); |
| 248 | bgc->write_reg(bgc->reg_dir, bgc->dir); |
| 249 | |
| 250 | spin_unlock_irqrestore(&bgc->lock, flags); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio) |
| 256 | { |
| 257 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 258 | unsigned long flags; |
| 259 | |
| 260 | spin_lock_irqsave(&bgc->lock, flags); |
| 261 | |
| 262 | bgc->dir |= bgc->pin2mask(bgc, gpio); |
| 263 | bgc->write_reg(bgc->reg_dir, bgc->dir); |
| 264 | |
| 265 | spin_unlock_irqrestore(&bgc->lock, flags); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val) |
| 271 | { |
| 272 | struct bgpio_chip *bgc = to_bgpio_chip(gc); |
| 273 | unsigned long flags; |
| 274 | |
| 275 | gc->set(gc, gpio, val); |
| 276 | |
| 277 | spin_lock_irqsave(&bgc->lock, flags); |
| 278 | |
| 279 | bgc->dir &= ~bgc->pin2mask(bgc, gpio); |
| 280 | bgc->write_reg(bgc->reg_dir, bgc->dir); |
| 281 | |
| 282 | spin_unlock_irqrestore(&bgc->lock, flags); |
| 283 | |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Jamie Iles | 364b5e8 | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 287 | static void __iomem *bgpio_request_and_map(struct device *dev, |
| 288 | struct resource *res) |
| 289 | { |
| 290 | if (!devm_request_mem_region(dev, res->start, resource_size(res), |
| 291 | res->name ?: "mmio_gpio")) |
| 292 | return NULL; |
| 293 | |
| 294 | return devm_ioremap(dev, res->start, resource_size(res)); |
| 295 | } |
| 296 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 297 | static int bgpio_setup_accessors(struct platform_device *pdev, |
| 298 | struct bgpio_chip *bgc) |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 299 | { |
| 300 | const struct platform_device_id *platid = platform_get_device_id(pdev); |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 301 | |
| 302 | switch (bgc->bits) { |
| 303 | case 8: |
| 304 | bgc->read_reg = bgpio_read8; |
| 305 | bgc->write_reg = bgpio_write8; |
| 306 | break; |
| 307 | case 16: |
| 308 | bgc->read_reg = bgpio_read16; |
| 309 | bgc->write_reg = bgpio_write16; |
| 310 | break; |
| 311 | case 32: |
| 312 | bgc->read_reg = bgpio_read32; |
| 313 | bgc->write_reg = bgpio_write32; |
| 314 | break; |
| 315 | #if BITS_PER_LONG >= 64 |
| 316 | case 64: |
| 317 | bgc->read_reg = bgpio_read64; |
| 318 | bgc->write_reg = bgpio_write64; |
| 319 | break; |
| 320 | #endif /* BITS_PER_LONG >= 64 */ |
| 321 | default: |
| 322 | dev_err(&pdev->dev, "unsupported data width %u bits\n", |
| 323 | bgc->bits); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | bgc->pin2mask = strcmp(platid->name, "basic-mmio-gpio-be") ? |
| 328 | bgpio_pin2mask : bgpio_pin2mask_be; |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 333 | /* |
| 334 | * Create the device and allocate the resources. For setting GPIO's there are |
Jamie Iles | dd86a0c | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 335 | * three supported configurations: |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 336 | * |
Jamie Iles | dd86a0c | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 337 | * - single input/output register resource (named "dat"). |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 338 | * - set/clear pair (named "set" and "clr"). |
Jamie Iles | dd86a0c | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 339 | * - single output register resource and single input resource ("set" and |
| 340 | * dat"). |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 341 | * |
| 342 | * For the single output register, this drives a 1 by setting a bit and a zero |
| 343 | * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit |
| 344 | * in the set register and clears it by setting a bit in the clear register. |
| 345 | * The configuration is detected by which resources are present. |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 346 | * |
| 347 | * For setting the GPIO direction, there are three supported configurations: |
| 348 | * |
| 349 | * - simple bidirection GPIO that requires no configuration. |
| 350 | * - an output direction register (named "dirout") where a 1 bit |
| 351 | * indicates the GPIO is an output. |
| 352 | * - an input direction register (named "dirin") where a 1 bit indicates |
| 353 | * the GPIO is an input. |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 354 | */ |
| 355 | static int bgpio_setup_io(struct platform_device *pdev, |
| 356 | struct bgpio_chip *bgc) |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 357 | { |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 358 | struct resource *res_set; |
| 359 | struct resource *res_clr; |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 360 | struct resource *res_dat; |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 361 | resource_size_t dat_sz; |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 362 | |
| 363 | res_dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); |
| 364 | if (!res_dat) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | dat_sz = resource_size(res_dat); |
| 368 | if (!is_power_of_2(dat_sz)) |
| 369 | return -EINVAL; |
| 370 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 371 | bgc->bits = dat_sz * 8; |
| 372 | if (bgc->bits > BITS_PER_LONG) |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 373 | return -EINVAL; |
| 374 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 375 | bgc->reg_dat = bgpio_request_and_map(&pdev->dev, res_dat); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 376 | if (!bgc->reg_dat) |
| 377 | return -ENOMEM; |
| 378 | |
| 379 | res_set = platform_get_resource_byname(pdev, IORESOURCE_MEM, "set"); |
| 380 | res_clr = platform_get_resource_byname(pdev, IORESOURCE_MEM, "clr"); |
| 381 | if (res_set && res_clr) { |
| 382 | if (resource_size(res_set) != resource_size(res_clr) || |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 383 | resource_size(res_set) != resource_size(res_dat)) |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 384 | return -EINVAL; |
| 385 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 386 | bgc->reg_set = bgpio_request_and_map(&pdev->dev, res_set); |
| 387 | bgc->reg_clr = bgpio_request_and_map(&pdev->dev, res_clr); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 388 | if (!bgc->reg_set || !bgc->reg_clr) |
| 389 | return -ENOMEM; |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 390 | |
| 391 | bgc->gc.set = bgpio_set_with_clear; |
Jamie Iles | dd86a0c | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 392 | } else if (res_set && !res_clr) { |
| 393 | if (resource_size(res_set) != resource_size(res_dat)) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | bgc->reg_set = bgpio_request_and_map(&pdev->dev, res_set); |
| 397 | if (!bgc->reg_set) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | bgc->gc.set = bgpio_set_set; |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 401 | } else { |
| 402 | bgc->gc.set = bgpio_set; |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Jamie Iles | dd86a0c | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 405 | bgc->gc.get = bgpio_get; |
| 406 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 407 | return 0; |
| 408 | } |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 409 | |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 410 | static int bgpio_setup_direction(struct platform_device *pdev, |
| 411 | struct bgpio_chip *bgc) |
| 412 | { |
| 413 | struct resource *res_dirout; |
| 414 | struct resource *res_dirin; |
| 415 | |
| 416 | res_dirout = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 417 | "dirout"); |
| 418 | res_dirin = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dirin"); |
| 419 | if (res_dirout && res_dirin) { |
| 420 | return -EINVAL; |
| 421 | } else if (res_dirout) { |
| 422 | bgc->reg_dir = bgpio_request_and_map(&pdev->dev, res_dirout); |
| 423 | if (!bgc->reg_dir) |
| 424 | return -ENOMEM; |
| 425 | bgc->gc.direction_output = bgpio_dir_out; |
| 426 | bgc->gc.direction_input = bgpio_dir_in; |
| 427 | } else if (res_dirin) { |
| 428 | bgc->reg_dir = bgpio_request_and_map(&pdev->dev, res_dirin); |
| 429 | if (!bgc->reg_dir) |
| 430 | return -ENOMEM; |
| 431 | bgc->gc.direction_output = bgpio_dir_out_inv; |
| 432 | bgc->gc.direction_input = bgpio_dir_in_inv; |
| 433 | } else { |
| 434 | bgc->gc.direction_output = bgpio_simple_dir_out; |
| 435 | bgc->gc.direction_input = bgpio_simple_dir_in; |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 441 | static int __devinit bgpio_probe(struct platform_device *pdev) |
| 442 | { |
| 443 | struct device *dev = &pdev->dev; |
| 444 | struct bgpio_pdata *pdata = dev_get_platdata(dev); |
| 445 | struct bgpio_chip *bgc; |
| 446 | int ret; |
| 447 | int ngpio; |
| 448 | |
| 449 | bgc = devm_kzalloc(dev, sizeof(*bgc), GFP_KERNEL); |
| 450 | if (!bgc) |
| 451 | return -ENOMEM; |
| 452 | |
| 453 | ret = bgpio_setup_io(pdev, bgc); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | |
| 457 | ngpio = bgc->bits; |
Jamie Iles | 924e7a9 | 2011-05-20 00:40:15 -0600 | [diff] [blame] | 458 | if (pdata) { |
| 459 | bgc->gc.base = pdata->base; |
| 460 | if (pdata->ngpio > 0) |
| 461 | ngpio = pdata->ngpio; |
| 462 | } else { |
| 463 | bgc->gc.base = -1; |
| 464 | } |
| 465 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 466 | ret = bgpio_setup_accessors(pdev, bgc); |
| 467 | if (ret) |
| 468 | return ret; |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 469 | |
Jamie Iles | e027d6f | 2011-05-20 00:40:16 -0600 | [diff] [blame] | 470 | spin_lock_init(&bgc->lock); |
Jamie Iles | 3102911 | 2011-05-20 00:40:17 -0600 | [diff] [blame] | 471 | ret = bgpio_setup_direction(pdev, bgc); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
Jamie Iles | 8467afe | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 475 | bgc->data = bgc->read_reg(bgc->reg_dat); |
Jamie Iles | 924e7a9 | 2011-05-20 00:40:15 -0600 | [diff] [blame] | 476 | |
| 477 | bgc->gc.ngpio = ngpio; |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 478 | bgc->gc.dev = dev; |
| 479 | bgc->gc.label = dev_name(dev); |
| 480 | |
Jamie Iles | 4ddb8ae | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 481 | platform_set_drvdata(pdev, bgc); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 482 | |
| 483 | ret = gpiochip_add(&bgc->gc); |
| 484 | if (ret) |
| 485 | dev_err(dev, "gpiochip_add() failed: %d\n", ret); |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | static int __devexit bgpio_remove(struct platform_device *pdev) |
| 491 | { |
Jamie Iles | 4ddb8ae | 2011-05-20 00:40:14 -0600 | [diff] [blame] | 492 | struct bgpio_chip *bgc = platform_get_drvdata(pdev); |
Anton Vorontsov | aeec56e | 2010-10-27 15:33:15 -0700 | [diff] [blame] | 493 | |
| 494 | return gpiochip_remove(&bgc->gc); |
| 495 | } |
| 496 | |
| 497 | static const struct platform_device_id bgpio_id_table[] = { |
| 498 | { "basic-mmio-gpio", }, |
| 499 | { "basic-mmio-gpio-be", }, |
| 500 | {}, |
| 501 | }; |
| 502 | MODULE_DEVICE_TABLE(platform, bgpio_id_table); |
| 503 | |
| 504 | static struct platform_driver bgpio_driver = { |
| 505 | .driver = { |
| 506 | .name = "basic-mmio-gpio", |
| 507 | }, |
| 508 | .id_table = bgpio_id_table, |
| 509 | .probe = bgpio_probe, |
| 510 | .remove = __devexit_p(bgpio_remove), |
| 511 | }; |
| 512 | |
| 513 | static int __init bgpio_init(void) |
| 514 | { |
| 515 | return platform_driver_register(&bgpio_driver); |
| 516 | } |
| 517 | module_init(bgpio_init); |
| 518 | |
| 519 | static void __exit bgpio_exit(void) |
| 520 | { |
| 521 | platform_driver_unregister(&bgpio_driver); |
| 522 | } |
| 523 | module_exit(bgpio_exit); |
| 524 | |
| 525 | MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers"); |
| 526 | MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); |
| 527 | MODULE_LICENSE("GPL"); |