1dae5f0afSLinus Walleij // SPDX-License-Identifier: GPL-2.0+ 2e9f4d569SChristian Lamparter /* 3e9f4d569SChristian Lamparter * Generic driver for memory-mapped GPIO controllers. 4e9f4d569SChristian Lamparter * 5e9f4d569SChristian Lamparter * Copyright 2008 MontaVista Software, Inc. 6e9f4d569SChristian Lamparter * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com> 7e9f4d569SChristian Lamparter * 8e9f4d569SChristian Lamparter * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`....... 9e9f4d569SChristian Lamparter * ...`` ```````.. 10e9f4d569SChristian Lamparter * ..The simplest form of a GPIO controller that the driver supports is`` 11e9f4d569SChristian Lamparter * `.just a single "data" register, where GPIO state can be read and/or ` 12e9f4d569SChristian Lamparter * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.``````` 13e9f4d569SChristian Lamparter * ````````` 14e9f4d569SChristian Lamparter ___ 15e9f4d569SChristian Lamparter _/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,... 16e9f4d569SChristian Lamparter __________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO . 17e9f4d569SChristian Lamparter o ` ~~~~\___/~~~~ ` controller in FPGA is ,.` 18e9f4d569SChristian Lamparter `....trivial..'~`.```.``` 19e9f4d569SChristian Lamparter * ``````` 20e9f4d569SChristian Lamparter * .```````~~~~`..`.``.``. 21e9f4d569SChristian Lamparter * . The driver supports `... ,..```.`~~~```````````````....````.``,, 22e9f4d569SChristian Lamparter * . big-endian notation, just`. .. A bit more sophisticated controllers , 23e9f4d569SChristian Lamparter * . register the device with -be`. .with a pair of set/clear-bit registers , 24e9f4d569SChristian Lamparter * `.. suffix. ```~~`````....`.` . affecting the data register and the .` 25e9f4d569SChristian Lamparter * ``.`.``...``` ```.. output pins are also supported.` 26e9f4d569SChristian Lamparter * ^^ `````.`````````.,``~``~``~~`````` 27e9f4d569SChristian Lamparter * . ^^ 28e9f4d569SChristian Lamparter * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`.. 29e9f4d569SChristian Lamparter * .. The expectation is that in at least some cases . ,-~~~-, 30e9f4d569SChristian Lamparter * .this will be used with roll-your-own ASIC/FPGA .` \ / 31e9f4d569SChristian Lamparter * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ / 32e9f4d569SChristian Lamparter * ..````````......``````````` \o_ 33e9f4d569SChristian Lamparter * | 34e9f4d569SChristian Lamparter * ^^ / \ 35e9f4d569SChristian Lamparter * 36e9f4d569SChristian Lamparter * ...`````~~`.....``.`..........``````.`.``.```........``. 37e9f4d569SChristian Lamparter * ` 8, 16, 32 and 64 bits registers are supported, and``. 38e9f4d569SChristian Lamparter * . the number of GPIOs is determined by the width of ~ 39e9f4d569SChristian Lamparter * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~ 40e9f4d569SChristian Lamparter * `.......````.``` 41e9f4d569SChristian Lamparter */ 42e9f4d569SChristian Lamparter 43e9f4d569SChristian Lamparter #include <linux/init.h> 44e9f4d569SChristian Lamparter #include <linux/err.h> 45e9f4d569SChristian Lamparter #include <linux/bug.h> 46e9f4d569SChristian Lamparter #include <linux/kernel.h> 47e9f4d569SChristian Lamparter #include <linux/module.h> 48e9f4d569SChristian Lamparter #include <linux/spinlock.h> 49e9f4d569SChristian Lamparter #include <linux/compiler.h> 50e9f4d569SChristian Lamparter #include <linux/types.h> 51e9f4d569SChristian Lamparter #include <linux/errno.h> 52e9f4d569SChristian Lamparter #include <linux/log2.h> 53e9f4d569SChristian Lamparter #include <linux/ioport.h> 54e9f4d569SChristian Lamparter #include <linux/io.h> 55e9f4d569SChristian Lamparter #include <linux/gpio/driver.h> 56e9f4d569SChristian Lamparter #include <linux/slab.h> 57e9f4d569SChristian Lamparter #include <linux/bitops.h> 58e9f4d569SChristian Lamparter #include <linux/platform_device.h> 59e9f4d569SChristian Lamparter #include <linux/mod_devicetable.h> 60e698613aSÁlvaro Fernández Rojas #include <linux/of.h> 61e698613aSÁlvaro Fernández Rojas #include <linux/of_device.h> 62e9f4d569SChristian Lamparter 63e9f4d569SChristian Lamparter static void bgpio_write8(void __iomem *reg, unsigned long data) 64e9f4d569SChristian Lamparter { 65e9f4d569SChristian Lamparter writeb(data, reg); 66e9f4d569SChristian Lamparter } 67e9f4d569SChristian Lamparter 68e9f4d569SChristian Lamparter static unsigned long bgpio_read8(void __iomem *reg) 69e9f4d569SChristian Lamparter { 70e9f4d569SChristian Lamparter return readb(reg); 71e9f4d569SChristian Lamparter } 72e9f4d569SChristian Lamparter 73e9f4d569SChristian Lamparter static void bgpio_write16(void __iomem *reg, unsigned long data) 74e9f4d569SChristian Lamparter { 75e9f4d569SChristian Lamparter writew(data, reg); 76e9f4d569SChristian Lamparter } 77e9f4d569SChristian Lamparter 78e9f4d569SChristian Lamparter static unsigned long bgpio_read16(void __iomem *reg) 79e9f4d569SChristian Lamparter { 80e9f4d569SChristian Lamparter return readw(reg); 81e9f4d569SChristian Lamparter } 82e9f4d569SChristian Lamparter 83e9f4d569SChristian Lamparter static void bgpio_write32(void __iomem *reg, unsigned long data) 84e9f4d569SChristian Lamparter { 85e9f4d569SChristian Lamparter writel(data, reg); 86e9f4d569SChristian Lamparter } 87e9f4d569SChristian Lamparter 88e9f4d569SChristian Lamparter static unsigned long bgpio_read32(void __iomem *reg) 89e9f4d569SChristian Lamparter { 90e9f4d569SChristian Lamparter return readl(reg); 91e9f4d569SChristian Lamparter } 92e9f4d569SChristian Lamparter 93e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64 94e9f4d569SChristian Lamparter static void bgpio_write64(void __iomem *reg, unsigned long data) 95e9f4d569SChristian Lamparter { 96e9f4d569SChristian Lamparter writeq(data, reg); 97e9f4d569SChristian Lamparter } 98e9f4d569SChristian Lamparter 99e9f4d569SChristian Lamparter static unsigned long bgpio_read64(void __iomem *reg) 100e9f4d569SChristian Lamparter { 101e9f4d569SChristian Lamparter return readq(reg); 102e9f4d569SChristian Lamparter } 103e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */ 104e9f4d569SChristian Lamparter 105e9f4d569SChristian Lamparter static void bgpio_write16be(void __iomem *reg, unsigned long data) 106e9f4d569SChristian Lamparter { 107e9f4d569SChristian Lamparter iowrite16be(data, reg); 108e9f4d569SChristian Lamparter } 109e9f4d569SChristian Lamparter 110e9f4d569SChristian Lamparter static unsigned long bgpio_read16be(void __iomem *reg) 111e9f4d569SChristian Lamparter { 112e9f4d569SChristian Lamparter return ioread16be(reg); 113e9f4d569SChristian Lamparter } 114e9f4d569SChristian Lamparter 115e9f4d569SChristian Lamparter static void bgpio_write32be(void __iomem *reg, unsigned long data) 116e9f4d569SChristian Lamparter { 117e9f4d569SChristian Lamparter iowrite32be(data, reg); 118e9f4d569SChristian Lamparter } 119e9f4d569SChristian Lamparter 120e9f4d569SChristian Lamparter static unsigned long bgpio_read32be(void __iomem *reg) 121e9f4d569SChristian Lamparter { 122e9f4d569SChristian Lamparter return ioread32be(reg); 123e9f4d569SChristian Lamparter } 124e9f4d569SChristian Lamparter 12524efd94bSLinus Walleij static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line) 126e9f4d569SChristian Lamparter { 12724efd94bSLinus Walleij if (gc->be_bits) 12824efd94bSLinus Walleij return BIT(gc->bgpio_bits - 1 - line); 12924efd94bSLinus Walleij return BIT(line); 130e9f4d569SChristian Lamparter } 131e9f4d569SChristian Lamparter 132e9f4d569SChristian Lamparter static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio) 133e9f4d569SChristian Lamparter { 13424efd94bSLinus Walleij unsigned long pinmask = bgpio_line2mask(gc, gpio); 135d799a4deSLinus Walleij bool dir = !!(gc->bgpio_dir & pinmask); 136e9f4d569SChristian Lamparter 137d799a4deSLinus Walleij if (dir) 138e9f4d569SChristian Lamparter return !!(gc->read_reg(gc->reg_set) & pinmask); 139e9f4d569SChristian Lamparter else 140e9f4d569SChristian Lamparter return !!(gc->read_reg(gc->reg_dat) & pinmask); 141e9f4d569SChristian Lamparter } 142e9f4d569SChristian Lamparter 14380057cb4SLinus Walleij /* 14480057cb4SLinus Walleij * This assumes that the bits in the GPIO register are in native endianness. 14580057cb4SLinus Walleij * We only assign the function pointer if we have that. 14680057cb4SLinus Walleij */ 14780057cb4SLinus Walleij static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask, 14880057cb4SLinus Walleij unsigned long *bits) 14980057cb4SLinus Walleij { 15080057cb4SLinus Walleij unsigned long get_mask = 0; 15180057cb4SLinus Walleij unsigned long set_mask = 0; 15280057cb4SLinus Walleij 15307c7b6a5SLinus Walleij /* Make sure we first clear any bits that are zero when we read the register */ 15407c7b6a5SLinus Walleij *bits &= ~*mask; 15507c7b6a5SLinus Walleij 15607c7b6a5SLinus Walleij set_mask = *mask & gc->bgpio_dir; 15707c7b6a5SLinus Walleij get_mask = *mask & ~gc->bgpio_dir; 15880057cb4SLinus Walleij 15980057cb4SLinus Walleij if (set_mask) 16080057cb4SLinus Walleij *bits |= gc->read_reg(gc->reg_set) & set_mask; 16180057cb4SLinus Walleij if (get_mask) 16280057cb4SLinus Walleij *bits |= gc->read_reg(gc->reg_dat) & get_mask; 16380057cb4SLinus Walleij 16480057cb4SLinus Walleij return 0; 16580057cb4SLinus Walleij } 16680057cb4SLinus Walleij 167e9f4d569SChristian Lamparter static int bgpio_get(struct gpio_chip *gc, unsigned int gpio) 168e9f4d569SChristian Lamparter { 16924efd94bSLinus Walleij return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio)); 170e9f4d569SChristian Lamparter } 171e9f4d569SChristian Lamparter 17280057cb4SLinus Walleij /* 17380057cb4SLinus Walleij * This only works if the bits in the GPIO register are in native endianness. 17480057cb4SLinus Walleij */ 17580057cb4SLinus Walleij static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask, 17680057cb4SLinus Walleij unsigned long *bits) 17780057cb4SLinus Walleij { 17807c7b6a5SLinus Walleij /* Make sure we first clear any bits that are zero when we read the register */ 17907c7b6a5SLinus Walleij *bits &= ~*mask; 18007c7b6a5SLinus Walleij *bits |= gc->read_reg(gc->reg_dat) & *mask; 18180057cb4SLinus Walleij return 0; 18280057cb4SLinus Walleij } 18380057cb4SLinus Walleij 18480057cb4SLinus Walleij /* 18580057cb4SLinus Walleij * With big endian mirrored bit order it becomes more tedious. 18680057cb4SLinus Walleij */ 18780057cb4SLinus Walleij static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask, 18880057cb4SLinus Walleij unsigned long *bits) 18980057cb4SLinus Walleij { 19080057cb4SLinus Walleij unsigned long readmask = 0; 19180057cb4SLinus Walleij unsigned long val; 19280057cb4SLinus Walleij int bit; 19380057cb4SLinus Walleij 19407c7b6a5SLinus Walleij /* Make sure we first clear any bits that are zero when we read the register */ 19507c7b6a5SLinus Walleij *bits &= ~*mask; 19607c7b6a5SLinus Walleij 19780057cb4SLinus Walleij /* Create a mirrored mask */ 19807c7b6a5SLinus Walleij bit = -1; 19907c7b6a5SLinus Walleij while ((bit = find_next_bit(mask, gc->ngpio, bit + 1)) < gc->ngpio) 20080057cb4SLinus Walleij readmask |= bgpio_line2mask(gc, bit); 20180057cb4SLinus Walleij 20280057cb4SLinus Walleij /* Read the register */ 20380057cb4SLinus Walleij val = gc->read_reg(gc->reg_dat) & readmask; 20480057cb4SLinus Walleij 20580057cb4SLinus Walleij /* 20680057cb4SLinus Walleij * Mirror the result into the "bits" result, this will give line 0 20780057cb4SLinus Walleij * in bit 0 ... line 31 in bit 31 for a 32bit register. 20880057cb4SLinus Walleij */ 20907c7b6a5SLinus Walleij bit = -1; 21007c7b6a5SLinus Walleij while ((bit = find_next_bit(&val, gc->ngpio, bit + 1)) < gc->ngpio) 21180057cb4SLinus Walleij *bits |= bgpio_line2mask(gc, bit); 21280057cb4SLinus Walleij 21380057cb4SLinus Walleij return 0; 21480057cb4SLinus Walleij } 21580057cb4SLinus Walleij 216e9f4d569SChristian Lamparter static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val) 217e9f4d569SChristian Lamparter { 218e9f4d569SChristian Lamparter } 219e9f4d569SChristian Lamparter 220e9f4d569SChristian Lamparter static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) 221e9f4d569SChristian Lamparter { 22224efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 223e9f4d569SChristian Lamparter unsigned long flags; 224e9f4d569SChristian Lamparter 225e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 226e9f4d569SChristian Lamparter 227e9f4d569SChristian Lamparter if (val) 228e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 229e9f4d569SChristian Lamparter else 230e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 231e9f4d569SChristian Lamparter 232e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dat, gc->bgpio_data); 233e9f4d569SChristian Lamparter 234e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 235e9f4d569SChristian Lamparter } 236e9f4d569SChristian Lamparter 237e9f4d569SChristian Lamparter static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio, 238e9f4d569SChristian Lamparter int val) 239e9f4d569SChristian Lamparter { 24024efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 241e9f4d569SChristian Lamparter 242e9f4d569SChristian Lamparter if (val) 243e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, mask); 244e9f4d569SChristian Lamparter else 245e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, mask); 246e9f4d569SChristian Lamparter } 247e9f4d569SChristian Lamparter 248e9f4d569SChristian Lamparter static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val) 249e9f4d569SChristian Lamparter { 25024efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 251e9f4d569SChristian Lamparter unsigned long flags; 252e9f4d569SChristian Lamparter 253e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 254e9f4d569SChristian Lamparter 255e9f4d569SChristian Lamparter if (val) 256e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 257e9f4d569SChristian Lamparter else 258e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 259e9f4d569SChristian Lamparter 260e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, gc->bgpio_data); 261e9f4d569SChristian Lamparter 262e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 263e9f4d569SChristian Lamparter } 264e9f4d569SChristian Lamparter 265e9f4d569SChristian Lamparter static void bgpio_multiple_get_masks(struct gpio_chip *gc, 266e9f4d569SChristian Lamparter unsigned long *mask, unsigned long *bits, 267e9f4d569SChristian Lamparter unsigned long *set_mask, 268e9f4d569SChristian Lamparter unsigned long *clear_mask) 269e9f4d569SChristian Lamparter { 270e9f4d569SChristian Lamparter int i; 271e9f4d569SChristian Lamparter 272e9f4d569SChristian Lamparter *set_mask = 0; 273e9f4d569SChristian Lamparter *clear_mask = 0; 274e9f4d569SChristian Lamparter 275e9f4d569SChristian Lamparter for (i = 0; i < gc->bgpio_bits; i++) { 276e9f4d569SChristian Lamparter if (*mask == 0) 277e9f4d569SChristian Lamparter break; 278e9f4d569SChristian Lamparter if (__test_and_clear_bit(i, mask)) { 279e9f4d569SChristian Lamparter if (test_bit(i, bits)) 28024efd94bSLinus Walleij *set_mask |= bgpio_line2mask(gc, i); 281e9f4d569SChristian Lamparter else 28224efd94bSLinus Walleij *clear_mask |= bgpio_line2mask(gc, i); 283e9f4d569SChristian Lamparter } 284e9f4d569SChristian Lamparter } 285e9f4d569SChristian Lamparter } 286e9f4d569SChristian Lamparter 287e9f4d569SChristian Lamparter static void bgpio_set_multiple_single_reg(struct gpio_chip *gc, 288e9f4d569SChristian Lamparter unsigned long *mask, 289e9f4d569SChristian Lamparter unsigned long *bits, 290e9f4d569SChristian Lamparter void __iomem *reg) 291e9f4d569SChristian Lamparter { 292e9f4d569SChristian Lamparter unsigned long flags; 293e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 294e9f4d569SChristian Lamparter 295e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 296e9f4d569SChristian Lamparter 297e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 298e9f4d569SChristian Lamparter 299e9f4d569SChristian Lamparter gc->bgpio_data |= set_mask; 300e9f4d569SChristian Lamparter gc->bgpio_data &= ~clear_mask; 301e9f4d569SChristian Lamparter 302e9f4d569SChristian Lamparter gc->write_reg(reg, gc->bgpio_data); 303e9f4d569SChristian Lamparter 304e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 305e9f4d569SChristian Lamparter } 306e9f4d569SChristian Lamparter 307e9f4d569SChristian Lamparter static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, 308e9f4d569SChristian Lamparter unsigned long *bits) 309e9f4d569SChristian Lamparter { 310e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat); 311e9f4d569SChristian Lamparter } 312e9f4d569SChristian Lamparter 313e9f4d569SChristian Lamparter static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask, 314e9f4d569SChristian Lamparter unsigned long *bits) 315e9f4d569SChristian Lamparter { 316e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set); 317e9f4d569SChristian Lamparter } 318e9f4d569SChristian Lamparter 319e9f4d569SChristian Lamparter static void bgpio_set_multiple_with_clear(struct gpio_chip *gc, 320e9f4d569SChristian Lamparter unsigned long *mask, 321e9f4d569SChristian Lamparter unsigned long *bits) 322e9f4d569SChristian Lamparter { 323e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 324e9f4d569SChristian Lamparter 325e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 326e9f4d569SChristian Lamparter 327e9f4d569SChristian Lamparter if (set_mask) 328e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, set_mask); 329e9f4d569SChristian Lamparter if (clear_mask) 330e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, clear_mask); 331e9f4d569SChristian Lamparter } 332e9f4d569SChristian Lamparter 333e9f4d569SChristian Lamparter static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio) 334e9f4d569SChristian Lamparter { 335e9f4d569SChristian Lamparter return 0; 336e9f4d569SChristian Lamparter } 337e9f4d569SChristian Lamparter 338e9f4d569SChristian Lamparter static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio, 339e9f4d569SChristian Lamparter int val) 340e9f4d569SChristian Lamparter { 341e9f4d569SChristian Lamparter return -EINVAL; 342e9f4d569SChristian Lamparter } 343e9f4d569SChristian Lamparter 344e9f4d569SChristian Lamparter static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio, 345e9f4d569SChristian Lamparter int val) 346e9f4d569SChristian Lamparter { 347e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 348e9f4d569SChristian Lamparter 349e9f4d569SChristian Lamparter return 0; 350e9f4d569SChristian Lamparter } 351e9f4d569SChristian Lamparter 352e9f4d569SChristian Lamparter static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) 353e9f4d569SChristian Lamparter { 354e9f4d569SChristian Lamparter unsigned long flags; 355e9f4d569SChristian Lamparter 356e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 357e9f4d569SChristian Lamparter 35824efd94bSLinus Walleij gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio); 359f69e00bdSLinus Walleij 360f69e00bdSLinus Walleij if (gc->reg_dir_in) 361f69e00bdSLinus Walleij gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir); 362f69e00bdSLinus Walleij if (gc->reg_dir_out) 363f69e00bdSLinus Walleij gc->write_reg(gc->reg_dir_out, gc->bgpio_dir); 364e9f4d569SChristian Lamparter 365e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 366e9f4d569SChristian Lamparter 367e9f4d569SChristian Lamparter return 0; 368e9f4d569SChristian Lamparter } 369e9f4d569SChristian Lamparter 370e9f4d569SChristian Lamparter static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio) 371e9f4d569SChristian Lamparter { 372f69e00bdSLinus Walleij /* Return 0 if output, 1 if input */ 373e42615ecSMatti Vaittinen if (gc->bgpio_dir_unreadable) { 374e42615ecSMatti Vaittinen if (gc->bgpio_dir & bgpio_line2mask(gc, gpio)) 375e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_OUT; 376e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_IN; 377e42615ecSMatti Vaittinen } 378e42615ecSMatti Vaittinen 379e42615ecSMatti Vaittinen if (gc->reg_dir_out) { 380e42615ecSMatti Vaittinen if (gc->read_reg(gc->reg_dir_out) & bgpio_line2mask(gc, gpio)) 381e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_OUT; 382e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_IN; 383e42615ecSMatti Vaittinen } 384e42615ecSMatti Vaittinen 385f69e00bdSLinus Walleij if (gc->reg_dir_in) 386e42615ecSMatti Vaittinen if (!(gc->read_reg(gc->reg_dir_in) & bgpio_line2mask(gc, gpio))) 387e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_OUT; 388f69e00bdSLinus Walleij 389e42615ecSMatti Vaittinen return GPIO_LINE_DIRECTION_IN; 390e9f4d569SChristian Lamparter } 391e9f4d569SChristian Lamparter 392*d19d2de6SChuanhong Guo static void bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 393e9f4d569SChristian Lamparter { 394e9f4d569SChristian Lamparter unsigned long flags; 395e9f4d569SChristian Lamparter 396e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 397e9f4d569SChristian Lamparter 398d799a4deSLinus Walleij gc->bgpio_dir |= bgpio_line2mask(gc, gpio); 399f69e00bdSLinus Walleij 400f69e00bdSLinus Walleij if (gc->reg_dir_in) 401f69e00bdSLinus Walleij gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir); 402f69e00bdSLinus Walleij if (gc->reg_dir_out) 403f69e00bdSLinus Walleij gc->write_reg(gc->reg_dir_out, gc->bgpio_dir); 404e9f4d569SChristian Lamparter 405e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 406*d19d2de6SChuanhong Guo } 407e9f4d569SChristian Lamparter 408*d19d2de6SChuanhong Guo static int bgpio_dir_out_dir_first(struct gpio_chip *gc, unsigned int gpio, 409*d19d2de6SChuanhong Guo int val) 410*d19d2de6SChuanhong Guo { 411*d19d2de6SChuanhong Guo bgpio_dir_out(gc, gpio, val); 412*d19d2de6SChuanhong Guo gc->set(gc, gpio, val); 413*d19d2de6SChuanhong Guo return 0; 414*d19d2de6SChuanhong Guo } 415*d19d2de6SChuanhong Guo 416*d19d2de6SChuanhong Guo static int bgpio_dir_out_val_first(struct gpio_chip *gc, unsigned int gpio, 417*d19d2de6SChuanhong Guo int val) 418*d19d2de6SChuanhong Guo { 419*d19d2de6SChuanhong Guo gc->set(gc, gpio, val); 420*d19d2de6SChuanhong Guo bgpio_dir_out(gc, gpio, val); 421e9f4d569SChristian Lamparter return 0; 422e9f4d569SChristian Lamparter } 423e9f4d569SChristian Lamparter 424e9f4d569SChristian Lamparter static int bgpio_setup_accessors(struct device *dev, 425e9f4d569SChristian Lamparter struct gpio_chip *gc, 426e9f4d569SChristian Lamparter bool byte_be) 427e9f4d569SChristian Lamparter { 428e9f4d569SChristian Lamparter 429e9f4d569SChristian Lamparter switch (gc->bgpio_bits) { 430e9f4d569SChristian Lamparter case 8: 431e9f4d569SChristian Lamparter gc->read_reg = bgpio_read8; 432e9f4d569SChristian Lamparter gc->write_reg = bgpio_write8; 433e9f4d569SChristian Lamparter break; 434e9f4d569SChristian Lamparter case 16: 435e9f4d569SChristian Lamparter if (byte_be) { 436e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16be; 437e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16be; 438e9f4d569SChristian Lamparter } else { 439e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16; 440e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16; 441e9f4d569SChristian Lamparter } 442e9f4d569SChristian Lamparter break; 443e9f4d569SChristian Lamparter case 32: 444e9f4d569SChristian Lamparter if (byte_be) { 445e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32be; 446e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32be; 447e9f4d569SChristian Lamparter } else { 448e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32; 449e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32; 450e9f4d569SChristian Lamparter } 451e9f4d569SChristian Lamparter break; 452e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64 453e9f4d569SChristian Lamparter case 64: 454e9f4d569SChristian Lamparter if (byte_be) { 455e9f4d569SChristian Lamparter dev_err(dev, 456e9f4d569SChristian Lamparter "64 bit big endian byte order unsupported\n"); 457e9f4d569SChristian Lamparter return -EINVAL; 458e9f4d569SChristian Lamparter } else { 459e9f4d569SChristian Lamparter gc->read_reg = bgpio_read64; 460e9f4d569SChristian Lamparter gc->write_reg = bgpio_write64; 461e9f4d569SChristian Lamparter } 462e9f4d569SChristian Lamparter break; 463e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */ 464e9f4d569SChristian Lamparter default: 465e9f4d569SChristian Lamparter dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits); 466e9f4d569SChristian Lamparter return -EINVAL; 467e9f4d569SChristian Lamparter } 468e9f4d569SChristian Lamparter 469e9f4d569SChristian Lamparter return 0; 470e9f4d569SChristian Lamparter } 471e9f4d569SChristian Lamparter 472e9f4d569SChristian Lamparter /* 473e9f4d569SChristian Lamparter * Create the device and allocate the resources. For setting GPIO's there are 474e9f4d569SChristian Lamparter * three supported configurations: 475e9f4d569SChristian Lamparter * 476e9f4d569SChristian Lamparter * - single input/output register resource (named "dat"). 477e9f4d569SChristian Lamparter * - set/clear pair (named "set" and "clr"). 478e9f4d569SChristian Lamparter * - single output register resource and single input resource ("set" and 479e9f4d569SChristian Lamparter * dat"). 480e9f4d569SChristian Lamparter * 481e9f4d569SChristian Lamparter * For the single output register, this drives a 1 by setting a bit and a zero 482e9f4d569SChristian Lamparter * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit 483e9f4d569SChristian Lamparter * in the set register and clears it by setting a bit in the clear register. 484e9f4d569SChristian Lamparter * The configuration is detected by which resources are present. 485e9f4d569SChristian Lamparter * 486e9f4d569SChristian Lamparter * For setting the GPIO direction, there are three supported configurations: 487e9f4d569SChristian Lamparter * 488e9f4d569SChristian Lamparter * - simple bidirection GPIO that requires no configuration. 489e9f4d569SChristian Lamparter * - an output direction register (named "dirout") where a 1 bit 490e9f4d569SChristian Lamparter * indicates the GPIO is an output. 491e9f4d569SChristian Lamparter * - an input direction register (named "dirin") where a 1 bit indicates 492e9f4d569SChristian Lamparter * the GPIO is an input. 493e9f4d569SChristian Lamparter */ 494e9f4d569SChristian Lamparter static int bgpio_setup_io(struct gpio_chip *gc, 495e9f4d569SChristian Lamparter void __iomem *dat, 496e9f4d569SChristian Lamparter void __iomem *set, 497e9f4d569SChristian Lamparter void __iomem *clr, 498e9f4d569SChristian Lamparter unsigned long flags) 499e9f4d569SChristian Lamparter { 500e9f4d569SChristian Lamparter 501e9f4d569SChristian Lamparter gc->reg_dat = dat; 502e9f4d569SChristian Lamparter if (!gc->reg_dat) 503e9f4d569SChristian Lamparter return -EINVAL; 504e9f4d569SChristian Lamparter 505e9f4d569SChristian Lamparter if (set && clr) { 506e9f4d569SChristian Lamparter gc->reg_set = set; 507e9f4d569SChristian Lamparter gc->reg_clr = clr; 508e9f4d569SChristian Lamparter gc->set = bgpio_set_with_clear; 509e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_with_clear; 510e9f4d569SChristian Lamparter } else if (set && !clr) { 511e9f4d569SChristian Lamparter gc->reg_set = set; 512e9f4d569SChristian Lamparter gc->set = bgpio_set_set; 513e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_set; 514e9f4d569SChristian Lamparter } else if (flags & BGPIOF_NO_OUTPUT) { 515e9f4d569SChristian Lamparter gc->set = bgpio_set_none; 516e9f4d569SChristian Lamparter gc->set_multiple = NULL; 517e9f4d569SChristian Lamparter } else { 518e9f4d569SChristian Lamparter gc->set = bgpio_set; 519e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple; 520e9f4d569SChristian Lamparter } 521e9f4d569SChristian Lamparter 522e9f4d569SChristian Lamparter if (!(flags & BGPIOF_UNREADABLE_REG_SET) && 52380057cb4SLinus Walleij (flags & BGPIOF_READ_OUTPUT_REG_SET)) { 524e9f4d569SChristian Lamparter gc->get = bgpio_get_set; 52580057cb4SLinus Walleij if (!gc->be_bits) 52680057cb4SLinus Walleij gc->get_multiple = bgpio_get_set_multiple; 52780057cb4SLinus Walleij /* 52880057cb4SLinus Walleij * We deliberately avoid assigning the ->get_multiple() call 52980057cb4SLinus Walleij * for big endian mirrored registers which are ALSO reflecting 53080057cb4SLinus Walleij * their value in the set register when used as output. It is 53180057cb4SLinus Walleij * simply too much complexity, let the GPIO core fall back to 53280057cb4SLinus Walleij * reading each line individually in that fringe case. 53380057cb4SLinus Walleij */ 53480057cb4SLinus Walleij } else { 535e9f4d569SChristian Lamparter gc->get = bgpio_get; 53680057cb4SLinus Walleij if (gc->be_bits) 53780057cb4SLinus Walleij gc->get_multiple = bgpio_get_multiple_be; 53880057cb4SLinus Walleij else 53980057cb4SLinus Walleij gc->get_multiple = bgpio_get_multiple; 54080057cb4SLinus Walleij } 541e9f4d569SChristian Lamparter 542e9f4d569SChristian Lamparter return 0; 543e9f4d569SChristian Lamparter } 544e9f4d569SChristian Lamparter 545e9f4d569SChristian Lamparter static int bgpio_setup_direction(struct gpio_chip *gc, 546e9f4d569SChristian Lamparter void __iomem *dirout, 547e9f4d569SChristian Lamparter void __iomem *dirin, 548e9f4d569SChristian Lamparter unsigned long flags) 549e9f4d569SChristian Lamparter { 550f69e00bdSLinus Walleij if (dirout || dirin) { 551f69e00bdSLinus Walleij gc->reg_dir_out = dirout; 552f69e00bdSLinus Walleij gc->reg_dir_in = dirin; 553*d19d2de6SChuanhong Guo if (flags & BGPIOF_NO_SET_ON_INPUT) 554*d19d2de6SChuanhong Guo gc->direction_output = bgpio_dir_out_dir_first; 555*d19d2de6SChuanhong Guo else 556*d19d2de6SChuanhong Guo gc->direction_output = bgpio_dir_out_val_first; 557e9f4d569SChristian Lamparter gc->direction_input = bgpio_dir_in; 558e9f4d569SChristian Lamparter gc->get_direction = bgpio_get_dir; 559e9f4d569SChristian Lamparter } else { 560e9f4d569SChristian Lamparter if (flags & BGPIOF_NO_OUTPUT) 561e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out_err; 562e9f4d569SChristian Lamparter else 563e9f4d569SChristian Lamparter gc->direction_output = bgpio_simple_dir_out; 564e9f4d569SChristian Lamparter gc->direction_input = bgpio_simple_dir_in; 565e9f4d569SChristian Lamparter } 566e9f4d569SChristian Lamparter 567e9f4d569SChristian Lamparter return 0; 568e9f4d569SChristian Lamparter } 569e9f4d569SChristian Lamparter 570e9f4d569SChristian Lamparter static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin) 571e9f4d569SChristian Lamparter { 572e9f4d569SChristian Lamparter if (gpio_pin < chip->ngpio) 573e9f4d569SChristian Lamparter return 0; 574e9f4d569SChristian Lamparter 575e9f4d569SChristian Lamparter return -EINVAL; 576e9f4d569SChristian Lamparter } 577e9f4d569SChristian Lamparter 578d799a4deSLinus Walleij /** 579d799a4deSLinus Walleij * bgpio_init() - Initialize generic GPIO accessor functions 580d799a4deSLinus Walleij * @gc: the GPIO chip to set up 581d799a4deSLinus Walleij * @dev: the parent device of the new GPIO chip (compulsory) 582d799a4deSLinus Walleij * @sz: the size (width) of the MMIO registers in bytes, typically 1, 2 or 4 583d799a4deSLinus Walleij * @dat: MMIO address for the register to READ the value of the GPIO lines, it 584d799a4deSLinus Walleij * is expected that a 1 in the corresponding bit in this register means the 585d799a4deSLinus Walleij * line is asserted 586d799a4deSLinus Walleij * @set: MMIO address for the register to SET the value of the GPIO lines, it is 587d799a4deSLinus Walleij * expected that we write the line with 1 in this register to drive the GPIO line 588d799a4deSLinus Walleij * high. 589d799a4deSLinus Walleij * @clr: MMIO address for the register to CLEAR the value of the GPIO lines, it is 590d799a4deSLinus Walleij * expected that we write the line with 1 in this register to drive the GPIO line 591d799a4deSLinus Walleij * low. It is allowed to leave this address as NULL, in that case the SET register 592d799a4deSLinus Walleij * will be assumed to also clear the GPIO lines, by actively writing the line 593d799a4deSLinus Walleij * with 0. 594d799a4deSLinus Walleij * @dirout: MMIO address for the register to set the line as OUTPUT. It is assumed 595d799a4deSLinus Walleij * that setting a line to 1 in this register will turn that line into an 596d799a4deSLinus Walleij * output line. Conversely, setting the line to 0 will turn that line into 597f69e00bdSLinus Walleij * an input. 598d799a4deSLinus Walleij * @dirin: MMIO address for the register to set this line as INPUT. It is assumed 599d799a4deSLinus Walleij * that setting a line to 1 in this register will turn that line into an 600d799a4deSLinus Walleij * input line. Conversely, setting the line to 0 will turn that line into 601f69e00bdSLinus Walleij * an output. 602d799a4deSLinus Walleij * @flags: Different flags that will affect the behaviour of the device, such as 603d799a4deSLinus Walleij * endianness etc. 604d799a4deSLinus Walleij */ 605e9f4d569SChristian Lamparter int bgpio_init(struct gpio_chip *gc, struct device *dev, 606e9f4d569SChristian Lamparter unsigned long sz, void __iomem *dat, void __iomem *set, 607e9f4d569SChristian Lamparter void __iomem *clr, void __iomem *dirout, void __iomem *dirin, 608e9f4d569SChristian Lamparter unsigned long flags) 609e9f4d569SChristian Lamparter { 610e9f4d569SChristian Lamparter int ret; 611e9f4d569SChristian Lamparter 612e9f4d569SChristian Lamparter if (!is_power_of_2(sz)) 613e9f4d569SChristian Lamparter return -EINVAL; 614e9f4d569SChristian Lamparter 615e9f4d569SChristian Lamparter gc->bgpio_bits = sz * 8; 616e9f4d569SChristian Lamparter if (gc->bgpio_bits > BITS_PER_LONG) 617e9f4d569SChristian Lamparter return -EINVAL; 618e9f4d569SChristian Lamparter 619e9f4d569SChristian Lamparter spin_lock_init(&gc->bgpio_lock); 620e9f4d569SChristian Lamparter gc->parent = dev; 621e9f4d569SChristian Lamparter gc->label = dev_name(dev); 622e9f4d569SChristian Lamparter gc->base = -1; 623e9f4d569SChristian Lamparter gc->ngpio = gc->bgpio_bits; 624e9f4d569SChristian Lamparter gc->request = bgpio_request; 62580057cb4SLinus Walleij gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN); 626e9f4d569SChristian Lamparter 627e9f4d569SChristian Lamparter ret = bgpio_setup_io(gc, dat, set, clr, flags); 628e9f4d569SChristian Lamparter if (ret) 629e9f4d569SChristian Lamparter return ret; 630e9f4d569SChristian Lamparter 63124efd94bSLinus Walleij ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER); 632e9f4d569SChristian Lamparter if (ret) 633e9f4d569SChristian Lamparter return ret; 634e9f4d569SChristian Lamparter 635e9f4d569SChristian Lamparter ret = bgpio_setup_direction(gc, dirout, dirin, flags); 636e9f4d569SChristian Lamparter if (ret) 637e9f4d569SChristian Lamparter return ret; 638e9f4d569SChristian Lamparter 639e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_dat); 640e9f4d569SChristian Lamparter if (gc->set == bgpio_set_set && 641e9f4d569SChristian Lamparter !(flags & BGPIOF_UNREADABLE_REG_SET)) 642e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_set); 643f69e00bdSLinus Walleij 644f69e00bdSLinus Walleij if (flags & BGPIOF_UNREADABLE_REG_DIR) 645f69e00bdSLinus Walleij gc->bgpio_dir_unreadable = true; 646f69e00bdSLinus Walleij 647f69e00bdSLinus Walleij /* 648f69e00bdSLinus Walleij * Inspect hardware to find initial direction setting. 649f69e00bdSLinus Walleij */ 650f69e00bdSLinus Walleij if ((gc->reg_dir_out || gc->reg_dir_in) && 651f69e00bdSLinus Walleij !(flags & BGPIOF_UNREADABLE_REG_DIR)) { 652f69e00bdSLinus Walleij if (gc->reg_dir_out) 653f69e00bdSLinus Walleij gc->bgpio_dir = gc->read_reg(gc->reg_dir_out); 654f69e00bdSLinus Walleij else if (gc->reg_dir_in) 655f69e00bdSLinus Walleij gc->bgpio_dir = ~gc->read_reg(gc->reg_dir_in); 656f69e00bdSLinus Walleij /* 657f69e00bdSLinus Walleij * If we have two direction registers, synchronise 658f69e00bdSLinus Walleij * input setting to output setting, the library 659f69e00bdSLinus Walleij * can not handle a line being input and output at 660f69e00bdSLinus Walleij * the same time. 661f69e00bdSLinus Walleij */ 662f69e00bdSLinus Walleij if (gc->reg_dir_out && gc->reg_dir_in) 663f69e00bdSLinus Walleij gc->write_reg(gc->reg_dir_in, ~gc->bgpio_dir); 664f69e00bdSLinus Walleij } 665e9f4d569SChristian Lamparter 666e9f4d569SChristian Lamparter return ret; 667e9f4d569SChristian Lamparter } 668e9f4d569SChristian Lamparter EXPORT_SYMBOL_GPL(bgpio_init); 669e9f4d569SChristian Lamparter 670e9f4d569SChristian Lamparter #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM) 671e9f4d569SChristian Lamparter 672e9f4d569SChristian Lamparter static void __iomem *bgpio_map(struct platform_device *pdev, 673e9f4d569SChristian Lamparter const char *name, 674e9f4d569SChristian Lamparter resource_size_t sane_sz) 675e9f4d569SChristian Lamparter { 676e9f4d569SChristian Lamparter struct resource *r; 677e9f4d569SChristian Lamparter resource_size_t sz; 678e9f4d569SChristian Lamparter 679e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); 680e9f4d569SChristian Lamparter if (!r) 681e9f4d569SChristian Lamparter return NULL; 682e9f4d569SChristian Lamparter 683e9f4d569SChristian Lamparter sz = resource_size(r); 684e9f4d569SChristian Lamparter if (sz != sane_sz) 685e9f4d569SChristian Lamparter return IOMEM_ERR_PTR(-EINVAL); 686e9f4d569SChristian Lamparter 687e9f4d569SChristian Lamparter return devm_ioremap_resource(&pdev->dev, r); 688e9f4d569SChristian Lamparter } 689e9f4d569SChristian Lamparter 690e698613aSÁlvaro Fernández Rojas #ifdef CONFIG_OF 691e698613aSÁlvaro Fernández Rojas static const struct of_device_id bgpio_of_match[] = { 69205cc995fSChristian Lamparter { .compatible = "brcm,bcm6345-gpio" }, 693c0d30ecfSChristian Lamparter { .compatible = "wd,mbl-gpio" }, 694b8c90199SNathan Sullivan { .compatible = "ni,169445-nand-gpio" }, 695e698613aSÁlvaro Fernández Rojas { } 696e698613aSÁlvaro Fernández Rojas }; 697e698613aSÁlvaro Fernández Rojas MODULE_DEVICE_TABLE(of, bgpio_of_match); 698e698613aSÁlvaro Fernández Rojas 699e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 700e698613aSÁlvaro Fernández Rojas unsigned long *flags) 701e698613aSÁlvaro Fernández Rojas { 702e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 703e698613aSÁlvaro Fernández Rojas 704e698613aSÁlvaro Fernández Rojas if (!of_match_device(bgpio_of_match, &pdev->dev)) 705e698613aSÁlvaro Fernández Rojas return NULL; 706e698613aSÁlvaro Fernández Rojas 707e698613aSÁlvaro Fernández Rojas pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata), 708e698613aSÁlvaro Fernández Rojas GFP_KERNEL); 709e698613aSÁlvaro Fernández Rojas if (!pdata) 710e698613aSÁlvaro Fernández Rojas return ERR_PTR(-ENOMEM); 711e698613aSÁlvaro Fernández Rojas 712e698613aSÁlvaro Fernández Rojas pdata->base = -1; 713e698613aSÁlvaro Fernández Rojas 71405cc995fSChristian Lamparter if (of_device_is_big_endian(pdev->dev.of_node)) 71505cc995fSChristian Lamparter *flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER; 71605cc995fSChristian Lamparter 717c0d30ecfSChristian Lamparter if (of_property_read_bool(pdev->dev.of_node, "no-output")) 718c0d30ecfSChristian Lamparter *flags |= BGPIOF_NO_OUTPUT; 719c0d30ecfSChristian Lamparter 720e698613aSÁlvaro Fernández Rojas return pdata; 721e698613aSÁlvaro Fernández Rojas } 722e698613aSÁlvaro Fernández Rojas #else 723e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 724e698613aSÁlvaro Fernández Rojas unsigned long *flags) 725e698613aSÁlvaro Fernández Rojas { 726e698613aSÁlvaro Fernández Rojas return NULL; 727e698613aSÁlvaro Fernández Rojas } 728e698613aSÁlvaro Fernández Rojas #endif /* CONFIG_OF */ 729e698613aSÁlvaro Fernández Rojas 730e9f4d569SChristian Lamparter static int bgpio_pdev_probe(struct platform_device *pdev) 731e9f4d569SChristian Lamparter { 732e9f4d569SChristian Lamparter struct device *dev = &pdev->dev; 733e9f4d569SChristian Lamparter struct resource *r; 734e9f4d569SChristian Lamparter void __iomem *dat; 735e9f4d569SChristian Lamparter void __iomem *set; 736e9f4d569SChristian Lamparter void __iomem *clr; 737e9f4d569SChristian Lamparter void __iomem *dirout; 738e9f4d569SChristian Lamparter void __iomem *dirin; 739e9f4d569SChristian Lamparter unsigned long sz; 740e698613aSÁlvaro Fernández Rojas unsigned long flags = 0; 741e9f4d569SChristian Lamparter int err; 742e9f4d569SChristian Lamparter struct gpio_chip *gc; 743e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 744e698613aSÁlvaro Fernández Rojas 745e698613aSÁlvaro Fernández Rojas pdata = bgpio_parse_dt(pdev, &flags); 746e698613aSÁlvaro Fernández Rojas if (IS_ERR(pdata)) 747e698613aSÁlvaro Fernández Rojas return PTR_ERR(pdata); 748e698613aSÁlvaro Fernández Rojas 749e698613aSÁlvaro Fernández Rojas if (!pdata) { 750e698613aSÁlvaro Fernández Rojas pdata = dev_get_platdata(dev); 751e698613aSÁlvaro Fernández Rojas flags = pdev->id_entry->driver_data; 752e698613aSÁlvaro Fernández Rojas } 753e9f4d569SChristian Lamparter 754e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); 755e9f4d569SChristian Lamparter if (!r) 756e9f4d569SChristian Lamparter return -EINVAL; 757e9f4d569SChristian Lamparter 758e9f4d569SChristian Lamparter sz = resource_size(r); 759e9f4d569SChristian Lamparter 760e9f4d569SChristian Lamparter dat = bgpio_map(pdev, "dat", sz); 761e9f4d569SChristian Lamparter if (IS_ERR(dat)) 762e9f4d569SChristian Lamparter return PTR_ERR(dat); 763e9f4d569SChristian Lamparter 764e9f4d569SChristian Lamparter set = bgpio_map(pdev, "set", sz); 765e9f4d569SChristian Lamparter if (IS_ERR(set)) 766e9f4d569SChristian Lamparter return PTR_ERR(set); 767e9f4d569SChristian Lamparter 768e9f4d569SChristian Lamparter clr = bgpio_map(pdev, "clr", sz); 769e9f4d569SChristian Lamparter if (IS_ERR(clr)) 770e9f4d569SChristian Lamparter return PTR_ERR(clr); 771e9f4d569SChristian Lamparter 772e9f4d569SChristian Lamparter dirout = bgpio_map(pdev, "dirout", sz); 773e9f4d569SChristian Lamparter if (IS_ERR(dirout)) 774e9f4d569SChristian Lamparter return PTR_ERR(dirout); 775e9f4d569SChristian Lamparter 776e9f4d569SChristian Lamparter dirin = bgpio_map(pdev, "dirin", sz); 777e9f4d569SChristian Lamparter if (IS_ERR(dirin)) 778e9f4d569SChristian Lamparter return PTR_ERR(dirin); 779e9f4d569SChristian Lamparter 780e9f4d569SChristian Lamparter gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL); 781e9f4d569SChristian Lamparter if (!gc) 782e9f4d569SChristian Lamparter return -ENOMEM; 783e9f4d569SChristian Lamparter 784e9f4d569SChristian Lamparter err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags); 785e9f4d569SChristian Lamparter if (err) 786e9f4d569SChristian Lamparter return err; 787e9f4d569SChristian Lamparter 788e9f4d569SChristian Lamparter if (pdata) { 789e9f4d569SChristian Lamparter if (pdata->label) 790e9f4d569SChristian Lamparter gc->label = pdata->label; 791e9f4d569SChristian Lamparter gc->base = pdata->base; 792e9f4d569SChristian Lamparter if (pdata->ngpio > 0) 793e9f4d569SChristian Lamparter gc->ngpio = pdata->ngpio; 794e9f4d569SChristian Lamparter } 795e9f4d569SChristian Lamparter 796e9f4d569SChristian Lamparter platform_set_drvdata(pdev, gc); 797e9f4d569SChristian Lamparter 798e9f4d569SChristian Lamparter return devm_gpiochip_add_data(&pdev->dev, gc, NULL); 799e9f4d569SChristian Lamparter } 800e9f4d569SChristian Lamparter 801e9f4d569SChristian Lamparter static const struct platform_device_id bgpio_id_table[] = { 802e9f4d569SChristian Lamparter { 803e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 804e9f4d569SChristian Lamparter .driver_data = 0, 805e9f4d569SChristian Lamparter }, { 806e9f4d569SChristian Lamparter .name = "basic-mmio-gpio-be", 807e9f4d569SChristian Lamparter .driver_data = BGPIOF_BIG_ENDIAN, 808e9f4d569SChristian Lamparter }, 809e9f4d569SChristian Lamparter { } 810e9f4d569SChristian Lamparter }; 811e9f4d569SChristian Lamparter MODULE_DEVICE_TABLE(platform, bgpio_id_table); 812e9f4d569SChristian Lamparter 813e9f4d569SChristian Lamparter static struct platform_driver bgpio_driver = { 814e9f4d569SChristian Lamparter .driver = { 815e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 816e698613aSÁlvaro Fernández Rojas .of_match_table = of_match_ptr(bgpio_of_match), 817e9f4d569SChristian Lamparter }, 818e9f4d569SChristian Lamparter .id_table = bgpio_id_table, 819e9f4d569SChristian Lamparter .probe = bgpio_pdev_probe, 820e9f4d569SChristian Lamparter }; 821e9f4d569SChristian Lamparter 822e9f4d569SChristian Lamparter module_platform_driver(bgpio_driver); 823e9f4d569SChristian Lamparter 824e9f4d569SChristian Lamparter #endif /* CONFIG_GPIO_GENERIC_PLATFORM */ 825e9f4d569SChristian Lamparter 826e9f4d569SChristian Lamparter MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers"); 827e9f4d569SChristian Lamparter MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); 828e9f4d569SChristian Lamparter MODULE_LICENSE("GPL"); 829