1e9f4d569SChristian Lamparter /* 2e9f4d569SChristian Lamparter * Generic driver for memory-mapped GPIO controllers. 3e9f4d569SChristian Lamparter * 4e9f4d569SChristian Lamparter * Copyright 2008 MontaVista Software, Inc. 5e9f4d569SChristian Lamparter * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com> 6e9f4d569SChristian Lamparter * 7e9f4d569SChristian Lamparter * This program is free software; you can redistribute it and/or modify it 8e9f4d569SChristian Lamparter * under the terms of the GNU General Public License as published by the 9e9f4d569SChristian Lamparter * Free Software Foundation; either version 2 of the License, or (at your 10e9f4d569SChristian Lamparter * option) any later version. 11e9f4d569SChristian Lamparter * 12e9f4d569SChristian Lamparter * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`....... 13e9f4d569SChristian Lamparter * ...`` ```````.. 14e9f4d569SChristian Lamparter * ..The simplest form of a GPIO controller that the driver supports is`` 15e9f4d569SChristian Lamparter * `.just a single "data" register, where GPIO state can be read and/or ` 16e9f4d569SChristian Lamparter * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.``````` 17e9f4d569SChristian Lamparter * ````````` 18e9f4d569SChristian Lamparter ___ 19e9f4d569SChristian Lamparter _/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,... 20e9f4d569SChristian Lamparter __________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO . 21e9f4d569SChristian Lamparter o ` ~~~~\___/~~~~ ` controller in FPGA is ,.` 22e9f4d569SChristian Lamparter `....trivial..'~`.```.``` 23e9f4d569SChristian Lamparter * ``````` 24e9f4d569SChristian Lamparter * .```````~~~~`..`.``.``. 25e9f4d569SChristian Lamparter * . The driver supports `... ,..```.`~~~```````````````....````.``,, 26e9f4d569SChristian Lamparter * . big-endian notation, just`. .. A bit more sophisticated controllers , 27e9f4d569SChristian Lamparter * . register the device with -be`. .with a pair of set/clear-bit registers , 28e9f4d569SChristian Lamparter * `.. suffix. ```~~`````....`.` . affecting the data register and the .` 29e9f4d569SChristian Lamparter * ``.`.``...``` ```.. output pins are also supported.` 30e9f4d569SChristian Lamparter * ^^ `````.`````````.,``~``~``~~`````` 31e9f4d569SChristian Lamparter * . ^^ 32e9f4d569SChristian Lamparter * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`.. 33e9f4d569SChristian Lamparter * .. The expectation is that in at least some cases . ,-~~~-, 34e9f4d569SChristian Lamparter * .this will be used with roll-your-own ASIC/FPGA .` \ / 35e9f4d569SChristian Lamparter * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ / 36e9f4d569SChristian Lamparter * ..````````......``````````` \o_ 37e9f4d569SChristian Lamparter * | 38e9f4d569SChristian Lamparter * ^^ / \ 39e9f4d569SChristian Lamparter * 40e9f4d569SChristian Lamparter * ...`````~~`.....``.`..........``````.`.``.```........``. 41e9f4d569SChristian Lamparter * ` 8, 16, 32 and 64 bits registers are supported, and``. 42e9f4d569SChristian Lamparter * . the number of GPIOs is determined by the width of ~ 43e9f4d569SChristian Lamparter * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~ 44e9f4d569SChristian Lamparter * `.......````.``` 45e9f4d569SChristian Lamparter */ 46e9f4d569SChristian Lamparter 47e9f4d569SChristian Lamparter #include <linux/init.h> 48e9f4d569SChristian Lamparter #include <linux/err.h> 49e9f4d569SChristian Lamparter #include <linux/bug.h> 50e9f4d569SChristian Lamparter #include <linux/kernel.h> 51e9f4d569SChristian Lamparter #include <linux/module.h> 52e9f4d569SChristian Lamparter #include <linux/spinlock.h> 53e9f4d569SChristian Lamparter #include <linux/compiler.h> 54e9f4d569SChristian Lamparter #include <linux/types.h> 55e9f4d569SChristian Lamparter #include <linux/errno.h> 56e9f4d569SChristian Lamparter #include <linux/log2.h> 57e9f4d569SChristian Lamparter #include <linux/ioport.h> 58e9f4d569SChristian Lamparter #include <linux/io.h> 59e9f4d569SChristian Lamparter #include <linux/gpio/driver.h> 60e9f4d569SChristian Lamparter #include <linux/slab.h> 61e9f4d569SChristian Lamparter #include <linux/bitops.h> 62e9f4d569SChristian Lamparter #include <linux/platform_device.h> 63e9f4d569SChristian Lamparter #include <linux/mod_devicetable.h> 64e698613aSÁlvaro Fernández Rojas #include <linux/of.h> 65e698613aSÁlvaro Fernández Rojas #include <linux/of_device.h> 66e9f4d569SChristian Lamparter 67e9f4d569SChristian Lamparter static void bgpio_write8(void __iomem *reg, unsigned long data) 68e9f4d569SChristian Lamparter { 69e9f4d569SChristian Lamparter writeb(data, reg); 70e9f4d569SChristian Lamparter } 71e9f4d569SChristian Lamparter 72e9f4d569SChristian Lamparter static unsigned long bgpio_read8(void __iomem *reg) 73e9f4d569SChristian Lamparter { 74e9f4d569SChristian Lamparter return readb(reg); 75e9f4d569SChristian Lamparter } 76e9f4d569SChristian Lamparter 77e9f4d569SChristian Lamparter static void bgpio_write16(void __iomem *reg, unsigned long data) 78e9f4d569SChristian Lamparter { 79e9f4d569SChristian Lamparter writew(data, reg); 80e9f4d569SChristian Lamparter } 81e9f4d569SChristian Lamparter 82e9f4d569SChristian Lamparter static unsigned long bgpio_read16(void __iomem *reg) 83e9f4d569SChristian Lamparter { 84e9f4d569SChristian Lamparter return readw(reg); 85e9f4d569SChristian Lamparter } 86e9f4d569SChristian Lamparter 87e9f4d569SChristian Lamparter static void bgpio_write32(void __iomem *reg, unsigned long data) 88e9f4d569SChristian Lamparter { 89e9f4d569SChristian Lamparter writel(data, reg); 90e9f4d569SChristian Lamparter } 91e9f4d569SChristian Lamparter 92e9f4d569SChristian Lamparter static unsigned long bgpio_read32(void __iomem *reg) 93e9f4d569SChristian Lamparter { 94e9f4d569SChristian Lamparter return readl(reg); 95e9f4d569SChristian Lamparter } 96e9f4d569SChristian Lamparter 97e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64 98e9f4d569SChristian Lamparter static void bgpio_write64(void __iomem *reg, unsigned long data) 99e9f4d569SChristian Lamparter { 100e9f4d569SChristian Lamparter writeq(data, reg); 101e9f4d569SChristian Lamparter } 102e9f4d569SChristian Lamparter 103e9f4d569SChristian Lamparter static unsigned long bgpio_read64(void __iomem *reg) 104e9f4d569SChristian Lamparter { 105e9f4d569SChristian Lamparter return readq(reg); 106e9f4d569SChristian Lamparter } 107e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */ 108e9f4d569SChristian Lamparter 109e9f4d569SChristian Lamparter static void bgpio_write16be(void __iomem *reg, unsigned long data) 110e9f4d569SChristian Lamparter { 111e9f4d569SChristian Lamparter iowrite16be(data, reg); 112e9f4d569SChristian Lamparter } 113e9f4d569SChristian Lamparter 114e9f4d569SChristian Lamparter static unsigned long bgpio_read16be(void __iomem *reg) 115e9f4d569SChristian Lamparter { 116e9f4d569SChristian Lamparter return ioread16be(reg); 117e9f4d569SChristian Lamparter } 118e9f4d569SChristian Lamparter 119e9f4d569SChristian Lamparter static void bgpio_write32be(void __iomem *reg, unsigned long data) 120e9f4d569SChristian Lamparter { 121e9f4d569SChristian Lamparter iowrite32be(data, reg); 122e9f4d569SChristian Lamparter } 123e9f4d569SChristian Lamparter 124e9f4d569SChristian Lamparter static unsigned long bgpio_read32be(void __iomem *reg) 125e9f4d569SChristian Lamparter { 126e9f4d569SChristian Lamparter return ioread32be(reg); 127e9f4d569SChristian Lamparter } 128e9f4d569SChristian Lamparter 129*24efd94bSLinus Walleij static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line) 130e9f4d569SChristian Lamparter { 131*24efd94bSLinus Walleij if (gc->be_bits) 132*24efd94bSLinus Walleij return BIT(gc->bgpio_bits - 1 - line); 133*24efd94bSLinus Walleij return BIT(line); 134e9f4d569SChristian Lamparter } 135e9f4d569SChristian Lamparter 136e9f4d569SChristian Lamparter static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio) 137e9f4d569SChristian Lamparter { 138*24efd94bSLinus Walleij unsigned long pinmask = bgpio_line2mask(gc, gpio); 139e9f4d569SChristian Lamparter 140e9f4d569SChristian Lamparter if (gc->bgpio_dir & pinmask) 141e9f4d569SChristian Lamparter return !!(gc->read_reg(gc->reg_set) & pinmask); 142e9f4d569SChristian Lamparter else 143e9f4d569SChristian Lamparter return !!(gc->read_reg(gc->reg_dat) & pinmask); 144e9f4d569SChristian Lamparter } 145e9f4d569SChristian Lamparter 146e9f4d569SChristian Lamparter static int bgpio_get(struct gpio_chip *gc, unsigned int gpio) 147e9f4d569SChristian Lamparter { 148*24efd94bSLinus Walleij return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio)); 149e9f4d569SChristian Lamparter } 150e9f4d569SChristian Lamparter 151e9f4d569SChristian Lamparter static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val) 152e9f4d569SChristian Lamparter { 153e9f4d569SChristian Lamparter } 154e9f4d569SChristian Lamparter 155e9f4d569SChristian Lamparter static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) 156e9f4d569SChristian Lamparter { 157*24efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 158e9f4d569SChristian Lamparter unsigned long flags; 159e9f4d569SChristian Lamparter 160e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 161e9f4d569SChristian Lamparter 162e9f4d569SChristian Lamparter if (val) 163e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 164e9f4d569SChristian Lamparter else 165e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 166e9f4d569SChristian Lamparter 167e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dat, gc->bgpio_data); 168e9f4d569SChristian Lamparter 169e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 170e9f4d569SChristian Lamparter } 171e9f4d569SChristian Lamparter 172e9f4d569SChristian Lamparter static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio, 173e9f4d569SChristian Lamparter int val) 174e9f4d569SChristian Lamparter { 175*24efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 176e9f4d569SChristian Lamparter 177e9f4d569SChristian Lamparter if (val) 178e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, mask); 179e9f4d569SChristian Lamparter else 180e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, mask); 181e9f4d569SChristian Lamparter } 182e9f4d569SChristian Lamparter 183e9f4d569SChristian Lamparter static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val) 184e9f4d569SChristian Lamparter { 185*24efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 186e9f4d569SChristian Lamparter unsigned long flags; 187e9f4d569SChristian Lamparter 188e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 189e9f4d569SChristian Lamparter 190e9f4d569SChristian Lamparter if (val) 191e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 192e9f4d569SChristian Lamparter else 193e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 194e9f4d569SChristian Lamparter 195e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, gc->bgpio_data); 196e9f4d569SChristian Lamparter 197e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 198e9f4d569SChristian Lamparter } 199e9f4d569SChristian Lamparter 200e9f4d569SChristian Lamparter static void bgpio_multiple_get_masks(struct gpio_chip *gc, 201e9f4d569SChristian Lamparter unsigned long *mask, unsigned long *bits, 202e9f4d569SChristian Lamparter unsigned long *set_mask, 203e9f4d569SChristian Lamparter unsigned long *clear_mask) 204e9f4d569SChristian Lamparter { 205e9f4d569SChristian Lamparter int i; 206e9f4d569SChristian Lamparter 207e9f4d569SChristian Lamparter *set_mask = 0; 208e9f4d569SChristian Lamparter *clear_mask = 0; 209e9f4d569SChristian Lamparter 210e9f4d569SChristian Lamparter for (i = 0; i < gc->bgpio_bits; i++) { 211e9f4d569SChristian Lamparter if (*mask == 0) 212e9f4d569SChristian Lamparter break; 213e9f4d569SChristian Lamparter if (__test_and_clear_bit(i, mask)) { 214e9f4d569SChristian Lamparter if (test_bit(i, bits)) 215*24efd94bSLinus Walleij *set_mask |= bgpio_line2mask(gc, i); 216e9f4d569SChristian Lamparter else 217*24efd94bSLinus Walleij *clear_mask |= bgpio_line2mask(gc, i); 218e9f4d569SChristian Lamparter } 219e9f4d569SChristian Lamparter } 220e9f4d569SChristian Lamparter } 221e9f4d569SChristian Lamparter 222e9f4d569SChristian Lamparter static void bgpio_set_multiple_single_reg(struct gpio_chip *gc, 223e9f4d569SChristian Lamparter unsigned long *mask, 224e9f4d569SChristian Lamparter unsigned long *bits, 225e9f4d569SChristian Lamparter void __iomem *reg) 226e9f4d569SChristian Lamparter { 227e9f4d569SChristian Lamparter unsigned long flags; 228e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 229e9f4d569SChristian Lamparter 230e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 231e9f4d569SChristian Lamparter 232e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 233e9f4d569SChristian Lamparter 234e9f4d569SChristian Lamparter gc->bgpio_data |= set_mask; 235e9f4d569SChristian Lamparter gc->bgpio_data &= ~clear_mask; 236e9f4d569SChristian Lamparter 237e9f4d569SChristian Lamparter gc->write_reg(reg, gc->bgpio_data); 238e9f4d569SChristian Lamparter 239e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 240e9f4d569SChristian Lamparter } 241e9f4d569SChristian Lamparter 242e9f4d569SChristian Lamparter static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, 243e9f4d569SChristian Lamparter unsigned long *bits) 244e9f4d569SChristian Lamparter { 245e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat); 246e9f4d569SChristian Lamparter } 247e9f4d569SChristian Lamparter 248e9f4d569SChristian Lamparter static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask, 249e9f4d569SChristian Lamparter unsigned long *bits) 250e9f4d569SChristian Lamparter { 251e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set); 252e9f4d569SChristian Lamparter } 253e9f4d569SChristian Lamparter 254e9f4d569SChristian Lamparter static void bgpio_set_multiple_with_clear(struct gpio_chip *gc, 255e9f4d569SChristian Lamparter unsigned long *mask, 256e9f4d569SChristian Lamparter unsigned long *bits) 257e9f4d569SChristian Lamparter { 258e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 259e9f4d569SChristian Lamparter 260e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 261e9f4d569SChristian Lamparter 262e9f4d569SChristian Lamparter if (set_mask) 263e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, set_mask); 264e9f4d569SChristian Lamparter if (clear_mask) 265e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, clear_mask); 266e9f4d569SChristian Lamparter } 267e9f4d569SChristian Lamparter 268e9f4d569SChristian Lamparter static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio) 269e9f4d569SChristian Lamparter { 270e9f4d569SChristian Lamparter return 0; 271e9f4d569SChristian Lamparter } 272e9f4d569SChristian Lamparter 273e9f4d569SChristian Lamparter static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio, 274e9f4d569SChristian Lamparter int val) 275e9f4d569SChristian Lamparter { 276e9f4d569SChristian Lamparter return -EINVAL; 277e9f4d569SChristian Lamparter } 278e9f4d569SChristian Lamparter 279e9f4d569SChristian Lamparter static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio, 280e9f4d569SChristian Lamparter int val) 281e9f4d569SChristian Lamparter { 282e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 283e9f4d569SChristian Lamparter 284e9f4d569SChristian Lamparter return 0; 285e9f4d569SChristian Lamparter } 286e9f4d569SChristian Lamparter 287e9f4d569SChristian Lamparter static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) 288e9f4d569SChristian Lamparter { 289e9f4d569SChristian Lamparter unsigned long flags; 290e9f4d569SChristian Lamparter 291e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 292e9f4d569SChristian Lamparter 293*24efd94bSLinus Walleij gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio); 294e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 295e9f4d569SChristian Lamparter 296e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 297e9f4d569SChristian Lamparter 298e9f4d569SChristian Lamparter return 0; 299e9f4d569SChristian Lamparter } 300e9f4d569SChristian Lamparter 301e9f4d569SChristian Lamparter static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio) 302e9f4d569SChristian Lamparter { 303e9f4d569SChristian Lamparter /* Return 0 if output, 1 of input */ 304*24efd94bSLinus Walleij return !(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio)); 305e9f4d569SChristian Lamparter } 306e9f4d569SChristian Lamparter 307e9f4d569SChristian Lamparter static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 308e9f4d569SChristian Lamparter { 309e9f4d569SChristian Lamparter unsigned long flags; 310e9f4d569SChristian Lamparter 311e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 312e9f4d569SChristian Lamparter 313e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 314e9f4d569SChristian Lamparter 315*24efd94bSLinus Walleij gc->bgpio_dir |= bgpio_line2mask(gc, gpio); 316e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 317e9f4d569SChristian Lamparter 318e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 319e9f4d569SChristian Lamparter 320e9f4d569SChristian Lamparter return 0; 321e9f4d569SChristian Lamparter } 322e9f4d569SChristian Lamparter 323e9f4d569SChristian Lamparter static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio) 324e9f4d569SChristian Lamparter { 325e9f4d569SChristian Lamparter unsigned long flags; 326e9f4d569SChristian Lamparter 327e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 328e9f4d569SChristian Lamparter 329*24efd94bSLinus Walleij gc->bgpio_dir |= bgpio_line2mask(gc, gpio); 330e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 331e9f4d569SChristian Lamparter 332e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 333e9f4d569SChristian Lamparter 334e9f4d569SChristian Lamparter return 0; 335e9f4d569SChristian Lamparter } 336e9f4d569SChristian Lamparter 337e9f4d569SChristian Lamparter static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val) 338e9f4d569SChristian Lamparter { 339e9f4d569SChristian Lamparter unsigned long flags; 340e9f4d569SChristian Lamparter 341e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 342e9f4d569SChristian Lamparter 343e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 344e9f4d569SChristian Lamparter 345*24efd94bSLinus Walleij gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio); 346e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 347e9f4d569SChristian Lamparter 348e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 349e9f4d569SChristian Lamparter 350e9f4d569SChristian Lamparter return 0; 351e9f4d569SChristian Lamparter } 352e9f4d569SChristian Lamparter 353e9f4d569SChristian Lamparter static int bgpio_get_dir_inv(struct gpio_chip *gc, unsigned int gpio) 354e9f4d569SChristian Lamparter { 355e9f4d569SChristian Lamparter /* Return 0 if output, 1 if input */ 356*24efd94bSLinus Walleij return !!(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio)); 357e9f4d569SChristian Lamparter } 358e9f4d569SChristian Lamparter 359e9f4d569SChristian Lamparter static int bgpio_setup_accessors(struct device *dev, 360e9f4d569SChristian Lamparter struct gpio_chip *gc, 361e9f4d569SChristian Lamparter bool byte_be) 362e9f4d569SChristian Lamparter { 363e9f4d569SChristian Lamparter 364e9f4d569SChristian Lamparter switch (gc->bgpio_bits) { 365e9f4d569SChristian Lamparter case 8: 366e9f4d569SChristian Lamparter gc->read_reg = bgpio_read8; 367e9f4d569SChristian Lamparter gc->write_reg = bgpio_write8; 368e9f4d569SChristian Lamparter break; 369e9f4d569SChristian Lamparter case 16: 370e9f4d569SChristian Lamparter if (byte_be) { 371e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16be; 372e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16be; 373e9f4d569SChristian Lamparter } else { 374e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16; 375e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16; 376e9f4d569SChristian Lamparter } 377e9f4d569SChristian Lamparter break; 378e9f4d569SChristian Lamparter case 32: 379e9f4d569SChristian Lamparter if (byte_be) { 380e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32be; 381e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32be; 382e9f4d569SChristian Lamparter } else { 383e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32; 384e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32; 385e9f4d569SChristian Lamparter } 386e9f4d569SChristian Lamparter break; 387e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64 388e9f4d569SChristian Lamparter case 64: 389e9f4d569SChristian Lamparter if (byte_be) { 390e9f4d569SChristian Lamparter dev_err(dev, 391e9f4d569SChristian Lamparter "64 bit big endian byte order unsupported\n"); 392e9f4d569SChristian Lamparter return -EINVAL; 393e9f4d569SChristian Lamparter } else { 394e9f4d569SChristian Lamparter gc->read_reg = bgpio_read64; 395e9f4d569SChristian Lamparter gc->write_reg = bgpio_write64; 396e9f4d569SChristian Lamparter } 397e9f4d569SChristian Lamparter break; 398e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */ 399e9f4d569SChristian Lamparter default: 400e9f4d569SChristian Lamparter dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits); 401e9f4d569SChristian Lamparter return -EINVAL; 402e9f4d569SChristian Lamparter } 403e9f4d569SChristian Lamparter 404e9f4d569SChristian Lamparter return 0; 405e9f4d569SChristian Lamparter } 406e9f4d569SChristian Lamparter 407e9f4d569SChristian Lamparter /* 408e9f4d569SChristian Lamparter * Create the device and allocate the resources. For setting GPIO's there are 409e9f4d569SChristian Lamparter * three supported configurations: 410e9f4d569SChristian Lamparter * 411e9f4d569SChristian Lamparter * - single input/output register resource (named "dat"). 412e9f4d569SChristian Lamparter * - set/clear pair (named "set" and "clr"). 413e9f4d569SChristian Lamparter * - single output register resource and single input resource ("set" and 414e9f4d569SChristian Lamparter * dat"). 415e9f4d569SChristian Lamparter * 416e9f4d569SChristian Lamparter * For the single output register, this drives a 1 by setting a bit and a zero 417e9f4d569SChristian Lamparter * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit 418e9f4d569SChristian Lamparter * in the set register and clears it by setting a bit in the clear register. 419e9f4d569SChristian Lamparter * The configuration is detected by which resources are present. 420e9f4d569SChristian Lamparter * 421e9f4d569SChristian Lamparter * For setting the GPIO direction, there are three supported configurations: 422e9f4d569SChristian Lamparter * 423e9f4d569SChristian Lamparter * - simple bidirection GPIO that requires no configuration. 424e9f4d569SChristian Lamparter * - an output direction register (named "dirout") where a 1 bit 425e9f4d569SChristian Lamparter * indicates the GPIO is an output. 426e9f4d569SChristian Lamparter * - an input direction register (named "dirin") where a 1 bit indicates 427e9f4d569SChristian Lamparter * the GPIO is an input. 428e9f4d569SChristian Lamparter */ 429e9f4d569SChristian Lamparter static int bgpio_setup_io(struct gpio_chip *gc, 430e9f4d569SChristian Lamparter void __iomem *dat, 431e9f4d569SChristian Lamparter void __iomem *set, 432e9f4d569SChristian Lamparter void __iomem *clr, 433e9f4d569SChristian Lamparter unsigned long flags) 434e9f4d569SChristian Lamparter { 435e9f4d569SChristian Lamparter 436e9f4d569SChristian Lamparter gc->reg_dat = dat; 437e9f4d569SChristian Lamparter if (!gc->reg_dat) 438e9f4d569SChristian Lamparter return -EINVAL; 439e9f4d569SChristian Lamparter 440e9f4d569SChristian Lamparter if (set && clr) { 441e9f4d569SChristian Lamparter gc->reg_set = set; 442e9f4d569SChristian Lamparter gc->reg_clr = clr; 443e9f4d569SChristian Lamparter gc->set = bgpio_set_with_clear; 444e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_with_clear; 445e9f4d569SChristian Lamparter } else if (set && !clr) { 446e9f4d569SChristian Lamparter gc->reg_set = set; 447e9f4d569SChristian Lamparter gc->set = bgpio_set_set; 448e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_set; 449e9f4d569SChristian Lamparter } else if (flags & BGPIOF_NO_OUTPUT) { 450e9f4d569SChristian Lamparter gc->set = bgpio_set_none; 451e9f4d569SChristian Lamparter gc->set_multiple = NULL; 452e9f4d569SChristian Lamparter } else { 453e9f4d569SChristian Lamparter gc->set = bgpio_set; 454e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple; 455e9f4d569SChristian Lamparter } 456e9f4d569SChristian Lamparter 457e9f4d569SChristian Lamparter if (!(flags & BGPIOF_UNREADABLE_REG_SET) && 458e9f4d569SChristian Lamparter (flags & BGPIOF_READ_OUTPUT_REG_SET)) 459e9f4d569SChristian Lamparter gc->get = bgpio_get_set; 460e9f4d569SChristian Lamparter else 461e9f4d569SChristian Lamparter gc->get = bgpio_get; 462e9f4d569SChristian Lamparter 463e9f4d569SChristian Lamparter return 0; 464e9f4d569SChristian Lamparter } 465e9f4d569SChristian Lamparter 466e9f4d569SChristian Lamparter static int bgpio_setup_direction(struct gpio_chip *gc, 467e9f4d569SChristian Lamparter void __iomem *dirout, 468e9f4d569SChristian Lamparter void __iomem *dirin, 469e9f4d569SChristian Lamparter unsigned long flags) 470e9f4d569SChristian Lamparter { 471e9f4d569SChristian Lamparter if (dirout && dirin) { 472e9f4d569SChristian Lamparter return -EINVAL; 473e9f4d569SChristian Lamparter } else if (dirout) { 474e9f4d569SChristian Lamparter gc->reg_dir = dirout; 475e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out; 476e9f4d569SChristian Lamparter gc->direction_input = bgpio_dir_in; 477e9f4d569SChristian Lamparter gc->get_direction = bgpio_get_dir; 478e9f4d569SChristian Lamparter } else if (dirin) { 479e9f4d569SChristian Lamparter gc->reg_dir = dirin; 480e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out_inv; 481e9f4d569SChristian Lamparter gc->direction_input = bgpio_dir_in_inv; 482e9f4d569SChristian Lamparter gc->get_direction = bgpio_get_dir_inv; 483e9f4d569SChristian Lamparter } else { 484e9f4d569SChristian Lamparter if (flags & BGPIOF_NO_OUTPUT) 485e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out_err; 486e9f4d569SChristian Lamparter else 487e9f4d569SChristian Lamparter gc->direction_output = bgpio_simple_dir_out; 488e9f4d569SChristian Lamparter gc->direction_input = bgpio_simple_dir_in; 489e9f4d569SChristian Lamparter } 490e9f4d569SChristian Lamparter 491e9f4d569SChristian Lamparter return 0; 492e9f4d569SChristian Lamparter } 493e9f4d569SChristian Lamparter 494e9f4d569SChristian Lamparter static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin) 495e9f4d569SChristian Lamparter { 496e9f4d569SChristian Lamparter if (gpio_pin < chip->ngpio) 497e9f4d569SChristian Lamparter return 0; 498e9f4d569SChristian Lamparter 499e9f4d569SChristian Lamparter return -EINVAL; 500e9f4d569SChristian Lamparter } 501e9f4d569SChristian Lamparter 502e9f4d569SChristian Lamparter int bgpio_init(struct gpio_chip *gc, struct device *dev, 503e9f4d569SChristian Lamparter unsigned long sz, void __iomem *dat, void __iomem *set, 504e9f4d569SChristian Lamparter void __iomem *clr, void __iomem *dirout, void __iomem *dirin, 505e9f4d569SChristian Lamparter unsigned long flags) 506e9f4d569SChristian Lamparter { 507e9f4d569SChristian Lamparter int ret; 508e9f4d569SChristian Lamparter 509e9f4d569SChristian Lamparter if (!is_power_of_2(sz)) 510e9f4d569SChristian Lamparter return -EINVAL; 511e9f4d569SChristian Lamparter 512e9f4d569SChristian Lamparter gc->bgpio_bits = sz * 8; 513e9f4d569SChristian Lamparter if (gc->bgpio_bits > BITS_PER_LONG) 514e9f4d569SChristian Lamparter return -EINVAL; 515e9f4d569SChristian Lamparter 516e9f4d569SChristian Lamparter spin_lock_init(&gc->bgpio_lock); 517e9f4d569SChristian Lamparter gc->parent = dev; 518e9f4d569SChristian Lamparter gc->label = dev_name(dev); 519e9f4d569SChristian Lamparter gc->base = -1; 520e9f4d569SChristian Lamparter gc->ngpio = gc->bgpio_bits; 521e9f4d569SChristian Lamparter gc->request = bgpio_request; 522e9f4d569SChristian Lamparter 523e9f4d569SChristian Lamparter ret = bgpio_setup_io(gc, dat, set, clr, flags); 524e9f4d569SChristian Lamparter if (ret) 525e9f4d569SChristian Lamparter return ret; 526e9f4d569SChristian Lamparter 527*24efd94bSLinus Walleij gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN); 528*24efd94bSLinus Walleij ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER); 529e9f4d569SChristian Lamparter if (ret) 530e9f4d569SChristian Lamparter return ret; 531e9f4d569SChristian Lamparter 532e9f4d569SChristian Lamparter ret = bgpio_setup_direction(gc, dirout, dirin, flags); 533e9f4d569SChristian Lamparter if (ret) 534e9f4d569SChristian Lamparter return ret; 535e9f4d569SChristian Lamparter 536e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_dat); 537e9f4d569SChristian Lamparter if (gc->set == bgpio_set_set && 538e9f4d569SChristian Lamparter !(flags & BGPIOF_UNREADABLE_REG_SET)) 539e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_set); 540e9f4d569SChristian Lamparter if (gc->reg_dir && !(flags & BGPIOF_UNREADABLE_REG_DIR)) 541e9f4d569SChristian Lamparter gc->bgpio_dir = gc->read_reg(gc->reg_dir); 542e9f4d569SChristian Lamparter 543e9f4d569SChristian Lamparter return ret; 544e9f4d569SChristian Lamparter } 545e9f4d569SChristian Lamparter EXPORT_SYMBOL_GPL(bgpio_init); 546e9f4d569SChristian Lamparter 547e9f4d569SChristian Lamparter #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM) 548e9f4d569SChristian Lamparter 549e9f4d569SChristian Lamparter static void __iomem *bgpio_map(struct platform_device *pdev, 550e9f4d569SChristian Lamparter const char *name, 551e9f4d569SChristian Lamparter resource_size_t sane_sz) 552e9f4d569SChristian Lamparter { 553e9f4d569SChristian Lamparter struct resource *r; 554e9f4d569SChristian Lamparter resource_size_t sz; 555e9f4d569SChristian Lamparter 556e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); 557e9f4d569SChristian Lamparter if (!r) 558e9f4d569SChristian Lamparter return NULL; 559e9f4d569SChristian Lamparter 560e9f4d569SChristian Lamparter sz = resource_size(r); 561e9f4d569SChristian Lamparter if (sz != sane_sz) 562e9f4d569SChristian Lamparter return IOMEM_ERR_PTR(-EINVAL); 563e9f4d569SChristian Lamparter 564e9f4d569SChristian Lamparter return devm_ioremap_resource(&pdev->dev, r); 565e9f4d569SChristian Lamparter } 566e9f4d569SChristian Lamparter 567e698613aSÁlvaro Fernández Rojas #ifdef CONFIG_OF 568e698613aSÁlvaro Fernández Rojas static const struct of_device_id bgpio_of_match[] = { 56905cc995fSChristian Lamparter { .compatible = "brcm,bcm6345-gpio" }, 570c0d30ecfSChristian Lamparter { .compatible = "wd,mbl-gpio" }, 571b8c90199SNathan Sullivan { .compatible = "ni,169445-nand-gpio" }, 572e698613aSÁlvaro Fernández Rojas { } 573e698613aSÁlvaro Fernández Rojas }; 574e698613aSÁlvaro Fernández Rojas MODULE_DEVICE_TABLE(of, bgpio_of_match); 575e698613aSÁlvaro Fernández Rojas 576e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 577e698613aSÁlvaro Fernández Rojas unsigned long *flags) 578e698613aSÁlvaro Fernández Rojas { 579e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 580e698613aSÁlvaro Fernández Rojas 581e698613aSÁlvaro Fernández Rojas if (!of_match_device(bgpio_of_match, &pdev->dev)) 582e698613aSÁlvaro Fernández Rojas return NULL; 583e698613aSÁlvaro Fernández Rojas 584e698613aSÁlvaro Fernández Rojas pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata), 585e698613aSÁlvaro Fernández Rojas GFP_KERNEL); 586e698613aSÁlvaro Fernández Rojas if (!pdata) 587e698613aSÁlvaro Fernández Rojas return ERR_PTR(-ENOMEM); 588e698613aSÁlvaro Fernández Rojas 589e698613aSÁlvaro Fernández Rojas pdata->base = -1; 590e698613aSÁlvaro Fernández Rojas 59105cc995fSChristian Lamparter if (of_device_is_big_endian(pdev->dev.of_node)) 59205cc995fSChristian Lamparter *flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER; 59305cc995fSChristian Lamparter 594c0d30ecfSChristian Lamparter if (of_property_read_bool(pdev->dev.of_node, "no-output")) 595c0d30ecfSChristian Lamparter *flags |= BGPIOF_NO_OUTPUT; 596c0d30ecfSChristian Lamparter 597e698613aSÁlvaro Fernández Rojas return pdata; 598e698613aSÁlvaro Fernández Rojas } 599e698613aSÁlvaro Fernández Rojas #else 600e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 601e698613aSÁlvaro Fernández Rojas unsigned long *flags) 602e698613aSÁlvaro Fernández Rojas { 603e698613aSÁlvaro Fernández Rojas return NULL; 604e698613aSÁlvaro Fernández Rojas } 605e698613aSÁlvaro Fernández Rojas #endif /* CONFIG_OF */ 606e698613aSÁlvaro Fernández Rojas 607e9f4d569SChristian Lamparter static int bgpio_pdev_probe(struct platform_device *pdev) 608e9f4d569SChristian Lamparter { 609e9f4d569SChristian Lamparter struct device *dev = &pdev->dev; 610e9f4d569SChristian Lamparter struct resource *r; 611e9f4d569SChristian Lamparter void __iomem *dat; 612e9f4d569SChristian Lamparter void __iomem *set; 613e9f4d569SChristian Lamparter void __iomem *clr; 614e9f4d569SChristian Lamparter void __iomem *dirout; 615e9f4d569SChristian Lamparter void __iomem *dirin; 616e9f4d569SChristian Lamparter unsigned long sz; 617e698613aSÁlvaro Fernández Rojas unsigned long flags = 0; 618e9f4d569SChristian Lamparter int err; 619e9f4d569SChristian Lamparter struct gpio_chip *gc; 620e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 621e698613aSÁlvaro Fernández Rojas 622e698613aSÁlvaro Fernández Rojas pdata = bgpio_parse_dt(pdev, &flags); 623e698613aSÁlvaro Fernández Rojas if (IS_ERR(pdata)) 624e698613aSÁlvaro Fernández Rojas return PTR_ERR(pdata); 625e698613aSÁlvaro Fernández Rojas 626e698613aSÁlvaro Fernández Rojas if (!pdata) { 627e698613aSÁlvaro Fernández Rojas pdata = dev_get_platdata(dev); 628e698613aSÁlvaro Fernández Rojas flags = pdev->id_entry->driver_data; 629e698613aSÁlvaro Fernández Rojas } 630e9f4d569SChristian Lamparter 631e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); 632e9f4d569SChristian Lamparter if (!r) 633e9f4d569SChristian Lamparter return -EINVAL; 634e9f4d569SChristian Lamparter 635e9f4d569SChristian Lamparter sz = resource_size(r); 636e9f4d569SChristian Lamparter 637e9f4d569SChristian Lamparter dat = bgpio_map(pdev, "dat", sz); 638e9f4d569SChristian Lamparter if (IS_ERR(dat)) 639e9f4d569SChristian Lamparter return PTR_ERR(dat); 640e9f4d569SChristian Lamparter 641e9f4d569SChristian Lamparter set = bgpio_map(pdev, "set", sz); 642e9f4d569SChristian Lamparter if (IS_ERR(set)) 643e9f4d569SChristian Lamparter return PTR_ERR(set); 644e9f4d569SChristian Lamparter 645e9f4d569SChristian Lamparter clr = bgpio_map(pdev, "clr", sz); 646e9f4d569SChristian Lamparter if (IS_ERR(clr)) 647e9f4d569SChristian Lamparter return PTR_ERR(clr); 648e9f4d569SChristian Lamparter 649e9f4d569SChristian Lamparter dirout = bgpio_map(pdev, "dirout", sz); 650e9f4d569SChristian Lamparter if (IS_ERR(dirout)) 651e9f4d569SChristian Lamparter return PTR_ERR(dirout); 652e9f4d569SChristian Lamparter 653e9f4d569SChristian Lamparter dirin = bgpio_map(pdev, "dirin", sz); 654e9f4d569SChristian Lamparter if (IS_ERR(dirin)) 655e9f4d569SChristian Lamparter return PTR_ERR(dirin); 656e9f4d569SChristian Lamparter 657e9f4d569SChristian Lamparter gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL); 658e9f4d569SChristian Lamparter if (!gc) 659e9f4d569SChristian Lamparter return -ENOMEM; 660e9f4d569SChristian Lamparter 661e9f4d569SChristian Lamparter err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags); 662e9f4d569SChristian Lamparter if (err) 663e9f4d569SChristian Lamparter return err; 664e9f4d569SChristian Lamparter 665e9f4d569SChristian Lamparter if (pdata) { 666e9f4d569SChristian Lamparter if (pdata->label) 667e9f4d569SChristian Lamparter gc->label = pdata->label; 668e9f4d569SChristian Lamparter gc->base = pdata->base; 669e9f4d569SChristian Lamparter if (pdata->ngpio > 0) 670e9f4d569SChristian Lamparter gc->ngpio = pdata->ngpio; 671e9f4d569SChristian Lamparter } 672e9f4d569SChristian Lamparter 673e9f4d569SChristian Lamparter platform_set_drvdata(pdev, gc); 674e9f4d569SChristian Lamparter 675e9f4d569SChristian Lamparter return devm_gpiochip_add_data(&pdev->dev, gc, NULL); 676e9f4d569SChristian Lamparter } 677e9f4d569SChristian Lamparter 678e9f4d569SChristian Lamparter static const struct platform_device_id bgpio_id_table[] = { 679e9f4d569SChristian Lamparter { 680e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 681e9f4d569SChristian Lamparter .driver_data = 0, 682e9f4d569SChristian Lamparter }, { 683e9f4d569SChristian Lamparter .name = "basic-mmio-gpio-be", 684e9f4d569SChristian Lamparter .driver_data = BGPIOF_BIG_ENDIAN, 685e9f4d569SChristian Lamparter }, 686e9f4d569SChristian Lamparter { } 687e9f4d569SChristian Lamparter }; 688e9f4d569SChristian Lamparter MODULE_DEVICE_TABLE(platform, bgpio_id_table); 689e9f4d569SChristian Lamparter 690e9f4d569SChristian Lamparter static struct platform_driver bgpio_driver = { 691e9f4d569SChristian Lamparter .driver = { 692e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 693e698613aSÁlvaro Fernández Rojas .of_match_table = of_match_ptr(bgpio_of_match), 694e9f4d569SChristian Lamparter }, 695e9f4d569SChristian Lamparter .id_table = bgpio_id_table, 696e9f4d569SChristian Lamparter .probe = bgpio_pdev_probe, 697e9f4d569SChristian Lamparter }; 698e9f4d569SChristian Lamparter 699e9f4d569SChristian Lamparter module_platform_driver(bgpio_driver); 700e9f4d569SChristian Lamparter 701e9f4d569SChristian Lamparter #endif /* CONFIG_GPIO_GENERIC_PLATFORM */ 702e9f4d569SChristian Lamparter 703e9f4d569SChristian Lamparter MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers"); 704e9f4d569SChristian Lamparter MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); 705e9f4d569SChristian Lamparter MODULE_LICENSE("GPL"); 706