1*dae5f0afSLinus 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 /* 138d799a4deSLinus Walleij * If the direction is OUT we read the value from the SET 139d799a4deSLinus Walleij * register, and if the direction is IN we read the value 140d799a4deSLinus Walleij * from the DAT register. 141d799a4deSLinus Walleij * 142d799a4deSLinus Walleij * If the direction bits are inverted, naturally this gets 143d799a4deSLinus Walleij * inverted too. 144d799a4deSLinus Walleij */ 145d799a4deSLinus Walleij if (gc->bgpio_dir_inverted) 146d799a4deSLinus Walleij dir = !dir; 147d799a4deSLinus Walleij 148d799a4deSLinus Walleij if (dir) 149e9f4d569SChristian Lamparter return !!(gc->read_reg(gc->reg_set) & pinmask); 150e9f4d569SChristian Lamparter else 151e9f4d569SChristian Lamparter return !!(gc->read_reg(gc->reg_dat) & pinmask); 152e9f4d569SChristian Lamparter } 153e9f4d569SChristian Lamparter 15480057cb4SLinus Walleij /* 15580057cb4SLinus Walleij * This assumes that the bits in the GPIO register are in native endianness. 15680057cb4SLinus Walleij * We only assign the function pointer if we have that. 15780057cb4SLinus Walleij */ 15880057cb4SLinus Walleij static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask, 15980057cb4SLinus Walleij unsigned long *bits) 16080057cb4SLinus Walleij { 16180057cb4SLinus Walleij unsigned long get_mask = 0; 16280057cb4SLinus Walleij unsigned long set_mask = 0; 16380057cb4SLinus Walleij 16407c7b6a5SLinus Walleij /* Make sure we first clear any bits that are zero when we read the register */ 16507c7b6a5SLinus Walleij *bits &= ~*mask; 16607c7b6a5SLinus Walleij 16707c7b6a5SLinus Walleij /* Exploit the fact that we know which directions are set */ 168d799a4deSLinus Walleij if (gc->bgpio_dir_inverted) { 169d799a4deSLinus Walleij set_mask = *mask & ~gc->bgpio_dir; 170d799a4deSLinus Walleij get_mask = *mask & gc->bgpio_dir; 171d799a4deSLinus Walleij } else { 17207c7b6a5SLinus Walleij set_mask = *mask & gc->bgpio_dir; 17307c7b6a5SLinus Walleij get_mask = *mask & ~gc->bgpio_dir; 174d799a4deSLinus Walleij } 17580057cb4SLinus Walleij 17680057cb4SLinus Walleij if (set_mask) 17780057cb4SLinus Walleij *bits |= gc->read_reg(gc->reg_set) & set_mask; 17880057cb4SLinus Walleij if (get_mask) 17980057cb4SLinus Walleij *bits |= gc->read_reg(gc->reg_dat) & get_mask; 18080057cb4SLinus Walleij 18180057cb4SLinus Walleij return 0; 18280057cb4SLinus Walleij } 18380057cb4SLinus Walleij 184e9f4d569SChristian Lamparter static int bgpio_get(struct gpio_chip *gc, unsigned int gpio) 185e9f4d569SChristian Lamparter { 18624efd94bSLinus Walleij return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio)); 187e9f4d569SChristian Lamparter } 188e9f4d569SChristian Lamparter 18980057cb4SLinus Walleij /* 19080057cb4SLinus Walleij * This only works if the bits in the GPIO register are in native endianness. 19180057cb4SLinus Walleij */ 19280057cb4SLinus Walleij static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask, 19380057cb4SLinus Walleij unsigned long *bits) 19480057cb4SLinus Walleij { 19507c7b6a5SLinus Walleij /* Make sure we first clear any bits that are zero when we read the register */ 19607c7b6a5SLinus Walleij *bits &= ~*mask; 19707c7b6a5SLinus Walleij *bits |= gc->read_reg(gc->reg_dat) & *mask; 19880057cb4SLinus Walleij return 0; 19980057cb4SLinus Walleij } 20080057cb4SLinus Walleij 20180057cb4SLinus Walleij /* 20280057cb4SLinus Walleij * With big endian mirrored bit order it becomes more tedious. 20380057cb4SLinus Walleij */ 20480057cb4SLinus Walleij static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask, 20580057cb4SLinus Walleij unsigned long *bits) 20680057cb4SLinus Walleij { 20780057cb4SLinus Walleij unsigned long readmask = 0; 20880057cb4SLinus Walleij unsigned long val; 20980057cb4SLinus Walleij int bit; 21080057cb4SLinus Walleij 21107c7b6a5SLinus Walleij /* Make sure we first clear any bits that are zero when we read the register */ 21207c7b6a5SLinus Walleij *bits &= ~*mask; 21307c7b6a5SLinus Walleij 21480057cb4SLinus Walleij /* Create a mirrored mask */ 21507c7b6a5SLinus Walleij bit = -1; 21607c7b6a5SLinus Walleij while ((bit = find_next_bit(mask, gc->ngpio, bit + 1)) < gc->ngpio) 21780057cb4SLinus Walleij readmask |= bgpio_line2mask(gc, bit); 21880057cb4SLinus Walleij 21980057cb4SLinus Walleij /* Read the register */ 22080057cb4SLinus Walleij val = gc->read_reg(gc->reg_dat) & readmask; 22180057cb4SLinus Walleij 22280057cb4SLinus Walleij /* 22380057cb4SLinus Walleij * Mirror the result into the "bits" result, this will give line 0 22480057cb4SLinus Walleij * in bit 0 ... line 31 in bit 31 for a 32bit register. 22580057cb4SLinus Walleij */ 22607c7b6a5SLinus Walleij bit = -1; 22707c7b6a5SLinus Walleij while ((bit = find_next_bit(&val, gc->ngpio, bit + 1)) < gc->ngpio) 22880057cb4SLinus Walleij *bits |= bgpio_line2mask(gc, bit); 22980057cb4SLinus Walleij 23080057cb4SLinus Walleij return 0; 23180057cb4SLinus Walleij } 23280057cb4SLinus Walleij 233e9f4d569SChristian Lamparter static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val) 234e9f4d569SChristian Lamparter { 235e9f4d569SChristian Lamparter } 236e9f4d569SChristian Lamparter 237e9f4d569SChristian Lamparter static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) 238e9f4d569SChristian Lamparter { 23924efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 240e9f4d569SChristian Lamparter unsigned long flags; 241e9f4d569SChristian Lamparter 242e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 243e9f4d569SChristian Lamparter 244e9f4d569SChristian Lamparter if (val) 245e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 246e9f4d569SChristian Lamparter else 247e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 248e9f4d569SChristian Lamparter 249e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dat, gc->bgpio_data); 250e9f4d569SChristian Lamparter 251e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 252e9f4d569SChristian Lamparter } 253e9f4d569SChristian Lamparter 254e9f4d569SChristian Lamparter static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio, 255e9f4d569SChristian Lamparter int val) 256e9f4d569SChristian Lamparter { 25724efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 258e9f4d569SChristian Lamparter 259e9f4d569SChristian Lamparter if (val) 260e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, mask); 261e9f4d569SChristian Lamparter else 262e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, mask); 263e9f4d569SChristian Lamparter } 264e9f4d569SChristian Lamparter 265e9f4d569SChristian Lamparter static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val) 266e9f4d569SChristian Lamparter { 26724efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 268e9f4d569SChristian Lamparter unsigned long flags; 269e9f4d569SChristian Lamparter 270e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 271e9f4d569SChristian Lamparter 272e9f4d569SChristian Lamparter if (val) 273e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 274e9f4d569SChristian Lamparter else 275e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 276e9f4d569SChristian Lamparter 277e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, gc->bgpio_data); 278e9f4d569SChristian Lamparter 279e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 280e9f4d569SChristian Lamparter } 281e9f4d569SChristian Lamparter 282e9f4d569SChristian Lamparter static void bgpio_multiple_get_masks(struct gpio_chip *gc, 283e9f4d569SChristian Lamparter unsigned long *mask, unsigned long *bits, 284e9f4d569SChristian Lamparter unsigned long *set_mask, 285e9f4d569SChristian Lamparter unsigned long *clear_mask) 286e9f4d569SChristian Lamparter { 287e9f4d569SChristian Lamparter int i; 288e9f4d569SChristian Lamparter 289e9f4d569SChristian Lamparter *set_mask = 0; 290e9f4d569SChristian Lamparter *clear_mask = 0; 291e9f4d569SChristian Lamparter 292e9f4d569SChristian Lamparter for (i = 0; i < gc->bgpio_bits; i++) { 293e9f4d569SChristian Lamparter if (*mask == 0) 294e9f4d569SChristian Lamparter break; 295e9f4d569SChristian Lamparter if (__test_and_clear_bit(i, mask)) { 296e9f4d569SChristian Lamparter if (test_bit(i, bits)) 29724efd94bSLinus Walleij *set_mask |= bgpio_line2mask(gc, i); 298e9f4d569SChristian Lamparter else 29924efd94bSLinus Walleij *clear_mask |= bgpio_line2mask(gc, i); 300e9f4d569SChristian Lamparter } 301e9f4d569SChristian Lamparter } 302e9f4d569SChristian Lamparter } 303e9f4d569SChristian Lamparter 304e9f4d569SChristian Lamparter static void bgpio_set_multiple_single_reg(struct gpio_chip *gc, 305e9f4d569SChristian Lamparter unsigned long *mask, 306e9f4d569SChristian Lamparter unsigned long *bits, 307e9f4d569SChristian Lamparter void __iomem *reg) 308e9f4d569SChristian Lamparter { 309e9f4d569SChristian Lamparter unsigned long flags; 310e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 311e9f4d569SChristian Lamparter 312e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 313e9f4d569SChristian Lamparter 314e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 315e9f4d569SChristian Lamparter 316e9f4d569SChristian Lamparter gc->bgpio_data |= set_mask; 317e9f4d569SChristian Lamparter gc->bgpio_data &= ~clear_mask; 318e9f4d569SChristian Lamparter 319e9f4d569SChristian Lamparter gc->write_reg(reg, gc->bgpio_data); 320e9f4d569SChristian Lamparter 321e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 322e9f4d569SChristian Lamparter } 323e9f4d569SChristian Lamparter 324e9f4d569SChristian Lamparter static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, 325e9f4d569SChristian Lamparter unsigned long *bits) 326e9f4d569SChristian Lamparter { 327e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat); 328e9f4d569SChristian Lamparter } 329e9f4d569SChristian Lamparter 330e9f4d569SChristian Lamparter static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask, 331e9f4d569SChristian Lamparter unsigned long *bits) 332e9f4d569SChristian Lamparter { 333e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set); 334e9f4d569SChristian Lamparter } 335e9f4d569SChristian Lamparter 336e9f4d569SChristian Lamparter static void bgpio_set_multiple_with_clear(struct gpio_chip *gc, 337e9f4d569SChristian Lamparter unsigned long *mask, 338e9f4d569SChristian Lamparter unsigned long *bits) 339e9f4d569SChristian Lamparter { 340e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 341e9f4d569SChristian Lamparter 342e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 343e9f4d569SChristian Lamparter 344e9f4d569SChristian Lamparter if (set_mask) 345e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, set_mask); 346e9f4d569SChristian Lamparter if (clear_mask) 347e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, clear_mask); 348e9f4d569SChristian Lamparter } 349e9f4d569SChristian Lamparter 350e9f4d569SChristian Lamparter static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio) 351e9f4d569SChristian Lamparter { 352e9f4d569SChristian Lamparter return 0; 353e9f4d569SChristian Lamparter } 354e9f4d569SChristian Lamparter 355e9f4d569SChristian Lamparter static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio, 356e9f4d569SChristian Lamparter int val) 357e9f4d569SChristian Lamparter { 358e9f4d569SChristian Lamparter return -EINVAL; 359e9f4d569SChristian Lamparter } 360e9f4d569SChristian Lamparter 361e9f4d569SChristian Lamparter static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio, 362e9f4d569SChristian Lamparter int val) 363e9f4d569SChristian Lamparter { 364e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 365e9f4d569SChristian Lamparter 366e9f4d569SChristian Lamparter return 0; 367e9f4d569SChristian Lamparter } 368e9f4d569SChristian Lamparter 369e9f4d569SChristian Lamparter static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) 370e9f4d569SChristian Lamparter { 371e9f4d569SChristian Lamparter unsigned long flags; 372e9f4d569SChristian Lamparter 373e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 374e9f4d569SChristian Lamparter 375d799a4deSLinus Walleij if (gc->bgpio_dir_inverted) 376d799a4deSLinus Walleij gc->bgpio_dir |= bgpio_line2mask(gc, gpio); 377d799a4deSLinus Walleij else 37824efd94bSLinus Walleij gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio); 379e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 380e9f4d569SChristian Lamparter 381e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 382e9f4d569SChristian Lamparter 383e9f4d569SChristian Lamparter return 0; 384e9f4d569SChristian Lamparter } 385e9f4d569SChristian Lamparter 386e9f4d569SChristian Lamparter static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio) 387e9f4d569SChristian Lamparter { 388e9f4d569SChristian Lamparter /* Return 0 if output, 1 of input */ 389d799a4deSLinus Walleij if (gc->bgpio_dir_inverted) 390d799a4deSLinus Walleij return !!(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio)); 391d799a4deSLinus Walleij else 39224efd94bSLinus Walleij return !(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio)); 393e9f4d569SChristian Lamparter } 394e9f4d569SChristian Lamparter 395e9f4d569SChristian Lamparter static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 396e9f4d569SChristian Lamparter { 397e9f4d569SChristian Lamparter unsigned long flags; 398e9f4d569SChristian Lamparter 399e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 400e9f4d569SChristian Lamparter 401e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 402e9f4d569SChristian Lamparter 403d799a4deSLinus Walleij if (gc->bgpio_dir_inverted) 40424efd94bSLinus Walleij gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio); 405d799a4deSLinus Walleij else 406d799a4deSLinus Walleij gc->bgpio_dir |= bgpio_line2mask(gc, gpio); 407e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 408e9f4d569SChristian Lamparter 409e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 410e9f4d569SChristian Lamparter 411e9f4d569SChristian Lamparter return 0; 412e9f4d569SChristian Lamparter } 413e9f4d569SChristian Lamparter 414e9f4d569SChristian Lamparter static int bgpio_setup_accessors(struct device *dev, 415e9f4d569SChristian Lamparter struct gpio_chip *gc, 416e9f4d569SChristian Lamparter bool byte_be) 417e9f4d569SChristian Lamparter { 418e9f4d569SChristian Lamparter 419e9f4d569SChristian Lamparter switch (gc->bgpio_bits) { 420e9f4d569SChristian Lamparter case 8: 421e9f4d569SChristian Lamparter gc->read_reg = bgpio_read8; 422e9f4d569SChristian Lamparter gc->write_reg = bgpio_write8; 423e9f4d569SChristian Lamparter break; 424e9f4d569SChristian Lamparter case 16: 425e9f4d569SChristian Lamparter if (byte_be) { 426e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16be; 427e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16be; 428e9f4d569SChristian Lamparter } else { 429e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16; 430e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16; 431e9f4d569SChristian Lamparter } 432e9f4d569SChristian Lamparter break; 433e9f4d569SChristian Lamparter case 32: 434e9f4d569SChristian Lamparter if (byte_be) { 435e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32be; 436e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32be; 437e9f4d569SChristian Lamparter } else { 438e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32; 439e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32; 440e9f4d569SChristian Lamparter } 441e9f4d569SChristian Lamparter break; 442e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64 443e9f4d569SChristian Lamparter case 64: 444e9f4d569SChristian Lamparter if (byte_be) { 445e9f4d569SChristian Lamparter dev_err(dev, 446e9f4d569SChristian Lamparter "64 bit big endian byte order unsupported\n"); 447e9f4d569SChristian Lamparter return -EINVAL; 448e9f4d569SChristian Lamparter } else { 449e9f4d569SChristian Lamparter gc->read_reg = bgpio_read64; 450e9f4d569SChristian Lamparter gc->write_reg = bgpio_write64; 451e9f4d569SChristian Lamparter } 452e9f4d569SChristian Lamparter break; 453e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */ 454e9f4d569SChristian Lamparter default: 455e9f4d569SChristian Lamparter dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits); 456e9f4d569SChristian Lamparter return -EINVAL; 457e9f4d569SChristian Lamparter } 458e9f4d569SChristian Lamparter 459e9f4d569SChristian Lamparter return 0; 460e9f4d569SChristian Lamparter } 461e9f4d569SChristian Lamparter 462e9f4d569SChristian Lamparter /* 463e9f4d569SChristian Lamparter * Create the device and allocate the resources. For setting GPIO's there are 464e9f4d569SChristian Lamparter * three supported configurations: 465e9f4d569SChristian Lamparter * 466e9f4d569SChristian Lamparter * - single input/output register resource (named "dat"). 467e9f4d569SChristian Lamparter * - set/clear pair (named "set" and "clr"). 468e9f4d569SChristian Lamparter * - single output register resource and single input resource ("set" and 469e9f4d569SChristian Lamparter * dat"). 470e9f4d569SChristian Lamparter * 471e9f4d569SChristian Lamparter * For the single output register, this drives a 1 by setting a bit and a zero 472e9f4d569SChristian Lamparter * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit 473e9f4d569SChristian Lamparter * in the set register and clears it by setting a bit in the clear register. 474e9f4d569SChristian Lamparter * The configuration is detected by which resources are present. 475e9f4d569SChristian Lamparter * 476e9f4d569SChristian Lamparter * For setting the GPIO direction, there are three supported configurations: 477e9f4d569SChristian Lamparter * 478e9f4d569SChristian Lamparter * - simple bidirection GPIO that requires no configuration. 479e9f4d569SChristian Lamparter * - an output direction register (named "dirout") where a 1 bit 480e9f4d569SChristian Lamparter * indicates the GPIO is an output. 481e9f4d569SChristian Lamparter * - an input direction register (named "dirin") where a 1 bit indicates 482e9f4d569SChristian Lamparter * the GPIO is an input. 483e9f4d569SChristian Lamparter */ 484e9f4d569SChristian Lamparter static int bgpio_setup_io(struct gpio_chip *gc, 485e9f4d569SChristian Lamparter void __iomem *dat, 486e9f4d569SChristian Lamparter void __iomem *set, 487e9f4d569SChristian Lamparter void __iomem *clr, 488e9f4d569SChristian Lamparter unsigned long flags) 489e9f4d569SChristian Lamparter { 490e9f4d569SChristian Lamparter 491e9f4d569SChristian Lamparter gc->reg_dat = dat; 492e9f4d569SChristian Lamparter if (!gc->reg_dat) 493e9f4d569SChristian Lamparter return -EINVAL; 494e9f4d569SChristian Lamparter 495e9f4d569SChristian Lamparter if (set && clr) { 496e9f4d569SChristian Lamparter gc->reg_set = set; 497e9f4d569SChristian Lamparter gc->reg_clr = clr; 498e9f4d569SChristian Lamparter gc->set = bgpio_set_with_clear; 499e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_with_clear; 500e9f4d569SChristian Lamparter } else if (set && !clr) { 501e9f4d569SChristian Lamparter gc->reg_set = set; 502e9f4d569SChristian Lamparter gc->set = bgpio_set_set; 503e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_set; 504e9f4d569SChristian Lamparter } else if (flags & BGPIOF_NO_OUTPUT) { 505e9f4d569SChristian Lamparter gc->set = bgpio_set_none; 506e9f4d569SChristian Lamparter gc->set_multiple = NULL; 507e9f4d569SChristian Lamparter } else { 508e9f4d569SChristian Lamparter gc->set = bgpio_set; 509e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple; 510e9f4d569SChristian Lamparter } 511e9f4d569SChristian Lamparter 512e9f4d569SChristian Lamparter if (!(flags & BGPIOF_UNREADABLE_REG_SET) && 51380057cb4SLinus Walleij (flags & BGPIOF_READ_OUTPUT_REG_SET)) { 514e9f4d569SChristian Lamparter gc->get = bgpio_get_set; 51580057cb4SLinus Walleij if (!gc->be_bits) 51680057cb4SLinus Walleij gc->get_multiple = bgpio_get_set_multiple; 51780057cb4SLinus Walleij /* 51880057cb4SLinus Walleij * We deliberately avoid assigning the ->get_multiple() call 51980057cb4SLinus Walleij * for big endian mirrored registers which are ALSO reflecting 52080057cb4SLinus Walleij * their value in the set register when used as output. It is 52180057cb4SLinus Walleij * simply too much complexity, let the GPIO core fall back to 52280057cb4SLinus Walleij * reading each line individually in that fringe case. 52380057cb4SLinus Walleij */ 52480057cb4SLinus Walleij } else { 525e9f4d569SChristian Lamparter gc->get = bgpio_get; 52680057cb4SLinus Walleij if (gc->be_bits) 52780057cb4SLinus Walleij gc->get_multiple = bgpio_get_multiple_be; 52880057cb4SLinus Walleij else 52980057cb4SLinus Walleij gc->get_multiple = bgpio_get_multiple; 53080057cb4SLinus Walleij } 531e9f4d569SChristian Lamparter 532e9f4d569SChristian Lamparter return 0; 533e9f4d569SChristian Lamparter } 534e9f4d569SChristian Lamparter 535e9f4d569SChristian Lamparter static int bgpio_setup_direction(struct gpio_chip *gc, 536e9f4d569SChristian Lamparter void __iomem *dirout, 537e9f4d569SChristian Lamparter void __iomem *dirin, 538e9f4d569SChristian Lamparter unsigned long flags) 539e9f4d569SChristian Lamparter { 540e9f4d569SChristian Lamparter if (dirout && dirin) { 541e9f4d569SChristian Lamparter return -EINVAL; 542e9f4d569SChristian Lamparter } else if (dirout) { 543e9f4d569SChristian Lamparter gc->reg_dir = dirout; 544e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out; 545e9f4d569SChristian Lamparter gc->direction_input = bgpio_dir_in; 546e9f4d569SChristian Lamparter gc->get_direction = bgpio_get_dir; 547e9f4d569SChristian Lamparter } else if (dirin) { 548e9f4d569SChristian Lamparter gc->reg_dir = dirin; 549d799a4deSLinus Walleij gc->direction_output = bgpio_dir_out; 550d799a4deSLinus Walleij gc->direction_input = bgpio_dir_in; 551d799a4deSLinus Walleij gc->get_direction = bgpio_get_dir; 552d799a4deSLinus Walleij gc->bgpio_dir_inverted = true; 553e9f4d569SChristian Lamparter } else { 554e9f4d569SChristian Lamparter if (flags & BGPIOF_NO_OUTPUT) 555e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out_err; 556e9f4d569SChristian Lamparter else 557e9f4d569SChristian Lamparter gc->direction_output = bgpio_simple_dir_out; 558e9f4d569SChristian Lamparter gc->direction_input = bgpio_simple_dir_in; 559e9f4d569SChristian Lamparter } 560e9f4d569SChristian Lamparter 561e9f4d569SChristian Lamparter return 0; 562e9f4d569SChristian Lamparter } 563e9f4d569SChristian Lamparter 564e9f4d569SChristian Lamparter static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin) 565e9f4d569SChristian Lamparter { 566e9f4d569SChristian Lamparter if (gpio_pin < chip->ngpio) 567e9f4d569SChristian Lamparter return 0; 568e9f4d569SChristian Lamparter 569e9f4d569SChristian Lamparter return -EINVAL; 570e9f4d569SChristian Lamparter } 571e9f4d569SChristian Lamparter 572d799a4deSLinus Walleij /** 573d799a4deSLinus Walleij * bgpio_init() - Initialize generic GPIO accessor functions 574d799a4deSLinus Walleij * @gc: the GPIO chip to set up 575d799a4deSLinus Walleij * @dev: the parent device of the new GPIO chip (compulsory) 576d799a4deSLinus Walleij * @sz: the size (width) of the MMIO registers in bytes, typically 1, 2 or 4 577d799a4deSLinus Walleij * @dat: MMIO address for the register to READ the value of the GPIO lines, it 578d799a4deSLinus Walleij * is expected that a 1 in the corresponding bit in this register means the 579d799a4deSLinus Walleij * line is asserted 580d799a4deSLinus Walleij * @set: MMIO address for the register to SET the value of the GPIO lines, it is 581d799a4deSLinus Walleij * expected that we write the line with 1 in this register to drive the GPIO line 582d799a4deSLinus Walleij * high. 583d799a4deSLinus Walleij * @clr: MMIO address for the register to CLEAR the value of the GPIO lines, it is 584d799a4deSLinus Walleij * expected that we write the line with 1 in this register to drive the GPIO line 585d799a4deSLinus Walleij * low. It is allowed to leave this address as NULL, in that case the SET register 586d799a4deSLinus Walleij * will be assumed to also clear the GPIO lines, by actively writing the line 587d799a4deSLinus Walleij * with 0. 588d799a4deSLinus Walleij * @dirout: MMIO address for the register to set the line as OUTPUT. It is assumed 589d799a4deSLinus Walleij * that setting a line to 1 in this register will turn that line into an 590d799a4deSLinus Walleij * output line. Conversely, setting the line to 0 will turn that line into 591d799a4deSLinus Walleij * an input. Either this or @dirin can be defined, but never both. 592d799a4deSLinus Walleij * @dirin: MMIO address for the register to set this line as INPUT. It is assumed 593d799a4deSLinus Walleij * that setting a line to 1 in this register will turn that line into an 594d799a4deSLinus Walleij * input line. Conversely, setting the line to 0 will turn that line into 595d799a4deSLinus Walleij * an output. Either this or @dirout can be defined, but never both. 596d799a4deSLinus Walleij * @flags: Different flags that will affect the behaviour of the device, such as 597d799a4deSLinus Walleij * endianness etc. 598d799a4deSLinus Walleij */ 599e9f4d569SChristian Lamparter int bgpio_init(struct gpio_chip *gc, struct device *dev, 600e9f4d569SChristian Lamparter unsigned long sz, void __iomem *dat, void __iomem *set, 601e9f4d569SChristian Lamparter void __iomem *clr, void __iomem *dirout, void __iomem *dirin, 602e9f4d569SChristian Lamparter unsigned long flags) 603e9f4d569SChristian Lamparter { 604e9f4d569SChristian Lamparter int ret; 605e9f4d569SChristian Lamparter 606e9f4d569SChristian Lamparter if (!is_power_of_2(sz)) 607e9f4d569SChristian Lamparter return -EINVAL; 608e9f4d569SChristian Lamparter 609e9f4d569SChristian Lamparter gc->bgpio_bits = sz * 8; 610e9f4d569SChristian Lamparter if (gc->bgpio_bits > BITS_PER_LONG) 611e9f4d569SChristian Lamparter return -EINVAL; 612e9f4d569SChristian Lamparter 613e9f4d569SChristian Lamparter spin_lock_init(&gc->bgpio_lock); 614e9f4d569SChristian Lamparter gc->parent = dev; 615e9f4d569SChristian Lamparter gc->label = dev_name(dev); 616e9f4d569SChristian Lamparter gc->base = -1; 617e9f4d569SChristian Lamparter gc->ngpio = gc->bgpio_bits; 618e9f4d569SChristian Lamparter gc->request = bgpio_request; 61980057cb4SLinus Walleij gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN); 620e9f4d569SChristian Lamparter 621e9f4d569SChristian Lamparter ret = bgpio_setup_io(gc, dat, set, clr, flags); 622e9f4d569SChristian Lamparter if (ret) 623e9f4d569SChristian Lamparter return ret; 624e9f4d569SChristian Lamparter 62524efd94bSLinus Walleij ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER); 626e9f4d569SChristian Lamparter if (ret) 627e9f4d569SChristian Lamparter return ret; 628e9f4d569SChristian Lamparter 629e9f4d569SChristian Lamparter ret = bgpio_setup_direction(gc, dirout, dirin, flags); 630e9f4d569SChristian Lamparter if (ret) 631e9f4d569SChristian Lamparter return ret; 632e9f4d569SChristian Lamparter 633e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_dat); 634e9f4d569SChristian Lamparter if (gc->set == bgpio_set_set && 635e9f4d569SChristian Lamparter !(flags & BGPIOF_UNREADABLE_REG_SET)) 636e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_set); 637e9f4d569SChristian Lamparter if (gc->reg_dir && !(flags & BGPIOF_UNREADABLE_REG_DIR)) 638e9f4d569SChristian Lamparter gc->bgpio_dir = gc->read_reg(gc->reg_dir); 639e9f4d569SChristian Lamparter 640e9f4d569SChristian Lamparter return ret; 641e9f4d569SChristian Lamparter } 642e9f4d569SChristian Lamparter EXPORT_SYMBOL_GPL(bgpio_init); 643e9f4d569SChristian Lamparter 644e9f4d569SChristian Lamparter #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM) 645e9f4d569SChristian Lamparter 646e9f4d569SChristian Lamparter static void __iomem *bgpio_map(struct platform_device *pdev, 647e9f4d569SChristian Lamparter const char *name, 648e9f4d569SChristian Lamparter resource_size_t sane_sz) 649e9f4d569SChristian Lamparter { 650e9f4d569SChristian Lamparter struct resource *r; 651e9f4d569SChristian Lamparter resource_size_t sz; 652e9f4d569SChristian Lamparter 653e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); 654e9f4d569SChristian Lamparter if (!r) 655e9f4d569SChristian Lamparter return NULL; 656e9f4d569SChristian Lamparter 657e9f4d569SChristian Lamparter sz = resource_size(r); 658e9f4d569SChristian Lamparter if (sz != sane_sz) 659e9f4d569SChristian Lamparter return IOMEM_ERR_PTR(-EINVAL); 660e9f4d569SChristian Lamparter 661e9f4d569SChristian Lamparter return devm_ioremap_resource(&pdev->dev, r); 662e9f4d569SChristian Lamparter } 663e9f4d569SChristian Lamparter 664e698613aSÁlvaro Fernández Rojas #ifdef CONFIG_OF 665e698613aSÁlvaro Fernández Rojas static const struct of_device_id bgpio_of_match[] = { 66605cc995fSChristian Lamparter { .compatible = "brcm,bcm6345-gpio" }, 667c0d30ecfSChristian Lamparter { .compatible = "wd,mbl-gpio" }, 668b8c90199SNathan Sullivan { .compatible = "ni,169445-nand-gpio" }, 669e698613aSÁlvaro Fernández Rojas { } 670e698613aSÁlvaro Fernández Rojas }; 671e698613aSÁlvaro Fernández Rojas MODULE_DEVICE_TABLE(of, bgpio_of_match); 672e698613aSÁlvaro Fernández Rojas 673e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 674e698613aSÁlvaro Fernández Rojas unsigned long *flags) 675e698613aSÁlvaro Fernández Rojas { 676e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 677e698613aSÁlvaro Fernández Rojas 678e698613aSÁlvaro Fernández Rojas if (!of_match_device(bgpio_of_match, &pdev->dev)) 679e698613aSÁlvaro Fernández Rojas return NULL; 680e698613aSÁlvaro Fernández Rojas 681e698613aSÁlvaro Fernández Rojas pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata), 682e698613aSÁlvaro Fernández Rojas GFP_KERNEL); 683e698613aSÁlvaro Fernández Rojas if (!pdata) 684e698613aSÁlvaro Fernández Rojas return ERR_PTR(-ENOMEM); 685e698613aSÁlvaro Fernández Rojas 686e698613aSÁlvaro Fernández Rojas pdata->base = -1; 687e698613aSÁlvaro Fernández Rojas 68805cc995fSChristian Lamparter if (of_device_is_big_endian(pdev->dev.of_node)) 68905cc995fSChristian Lamparter *flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER; 69005cc995fSChristian Lamparter 691c0d30ecfSChristian Lamparter if (of_property_read_bool(pdev->dev.of_node, "no-output")) 692c0d30ecfSChristian Lamparter *flags |= BGPIOF_NO_OUTPUT; 693c0d30ecfSChristian Lamparter 694e698613aSÁlvaro Fernández Rojas return pdata; 695e698613aSÁlvaro Fernández Rojas } 696e698613aSÁlvaro Fernández Rojas #else 697e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 698e698613aSÁlvaro Fernández Rojas unsigned long *flags) 699e698613aSÁlvaro Fernández Rojas { 700e698613aSÁlvaro Fernández Rojas return NULL; 701e698613aSÁlvaro Fernández Rojas } 702e698613aSÁlvaro Fernández Rojas #endif /* CONFIG_OF */ 703e698613aSÁlvaro Fernández Rojas 704e9f4d569SChristian Lamparter static int bgpio_pdev_probe(struct platform_device *pdev) 705e9f4d569SChristian Lamparter { 706e9f4d569SChristian Lamparter struct device *dev = &pdev->dev; 707e9f4d569SChristian Lamparter struct resource *r; 708e9f4d569SChristian Lamparter void __iomem *dat; 709e9f4d569SChristian Lamparter void __iomem *set; 710e9f4d569SChristian Lamparter void __iomem *clr; 711e9f4d569SChristian Lamparter void __iomem *dirout; 712e9f4d569SChristian Lamparter void __iomem *dirin; 713e9f4d569SChristian Lamparter unsigned long sz; 714e698613aSÁlvaro Fernández Rojas unsigned long flags = 0; 715e9f4d569SChristian Lamparter int err; 716e9f4d569SChristian Lamparter struct gpio_chip *gc; 717e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 718e698613aSÁlvaro Fernández Rojas 719e698613aSÁlvaro Fernández Rojas pdata = bgpio_parse_dt(pdev, &flags); 720e698613aSÁlvaro Fernández Rojas if (IS_ERR(pdata)) 721e698613aSÁlvaro Fernández Rojas return PTR_ERR(pdata); 722e698613aSÁlvaro Fernández Rojas 723e698613aSÁlvaro Fernández Rojas if (!pdata) { 724e698613aSÁlvaro Fernández Rojas pdata = dev_get_platdata(dev); 725e698613aSÁlvaro Fernández Rojas flags = pdev->id_entry->driver_data; 726e698613aSÁlvaro Fernández Rojas } 727e9f4d569SChristian Lamparter 728e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); 729e9f4d569SChristian Lamparter if (!r) 730e9f4d569SChristian Lamparter return -EINVAL; 731e9f4d569SChristian Lamparter 732e9f4d569SChristian Lamparter sz = resource_size(r); 733e9f4d569SChristian Lamparter 734e9f4d569SChristian Lamparter dat = bgpio_map(pdev, "dat", sz); 735e9f4d569SChristian Lamparter if (IS_ERR(dat)) 736e9f4d569SChristian Lamparter return PTR_ERR(dat); 737e9f4d569SChristian Lamparter 738e9f4d569SChristian Lamparter set = bgpio_map(pdev, "set", sz); 739e9f4d569SChristian Lamparter if (IS_ERR(set)) 740e9f4d569SChristian Lamparter return PTR_ERR(set); 741e9f4d569SChristian Lamparter 742e9f4d569SChristian Lamparter clr = bgpio_map(pdev, "clr", sz); 743e9f4d569SChristian Lamparter if (IS_ERR(clr)) 744e9f4d569SChristian Lamparter return PTR_ERR(clr); 745e9f4d569SChristian Lamparter 746e9f4d569SChristian Lamparter dirout = bgpio_map(pdev, "dirout", sz); 747e9f4d569SChristian Lamparter if (IS_ERR(dirout)) 748e9f4d569SChristian Lamparter return PTR_ERR(dirout); 749e9f4d569SChristian Lamparter 750e9f4d569SChristian Lamparter dirin = bgpio_map(pdev, "dirin", sz); 751e9f4d569SChristian Lamparter if (IS_ERR(dirin)) 752e9f4d569SChristian Lamparter return PTR_ERR(dirin); 753e9f4d569SChristian Lamparter 754e9f4d569SChristian Lamparter gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL); 755e9f4d569SChristian Lamparter if (!gc) 756e9f4d569SChristian Lamparter return -ENOMEM; 757e9f4d569SChristian Lamparter 758e9f4d569SChristian Lamparter err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags); 759e9f4d569SChristian Lamparter if (err) 760e9f4d569SChristian Lamparter return err; 761e9f4d569SChristian Lamparter 762e9f4d569SChristian Lamparter if (pdata) { 763e9f4d569SChristian Lamparter if (pdata->label) 764e9f4d569SChristian Lamparter gc->label = pdata->label; 765e9f4d569SChristian Lamparter gc->base = pdata->base; 766e9f4d569SChristian Lamparter if (pdata->ngpio > 0) 767e9f4d569SChristian Lamparter gc->ngpio = pdata->ngpio; 768e9f4d569SChristian Lamparter } 769e9f4d569SChristian Lamparter 770e9f4d569SChristian Lamparter platform_set_drvdata(pdev, gc); 771e9f4d569SChristian Lamparter 772e9f4d569SChristian Lamparter return devm_gpiochip_add_data(&pdev->dev, gc, NULL); 773e9f4d569SChristian Lamparter } 774e9f4d569SChristian Lamparter 775e9f4d569SChristian Lamparter static const struct platform_device_id bgpio_id_table[] = { 776e9f4d569SChristian Lamparter { 777e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 778e9f4d569SChristian Lamparter .driver_data = 0, 779e9f4d569SChristian Lamparter }, { 780e9f4d569SChristian Lamparter .name = "basic-mmio-gpio-be", 781e9f4d569SChristian Lamparter .driver_data = BGPIOF_BIG_ENDIAN, 782e9f4d569SChristian Lamparter }, 783e9f4d569SChristian Lamparter { } 784e9f4d569SChristian Lamparter }; 785e9f4d569SChristian Lamparter MODULE_DEVICE_TABLE(platform, bgpio_id_table); 786e9f4d569SChristian Lamparter 787e9f4d569SChristian Lamparter static struct platform_driver bgpio_driver = { 788e9f4d569SChristian Lamparter .driver = { 789e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 790e698613aSÁlvaro Fernández Rojas .of_match_table = of_match_ptr(bgpio_of_match), 791e9f4d569SChristian Lamparter }, 792e9f4d569SChristian Lamparter .id_table = bgpio_id_table, 793e9f4d569SChristian Lamparter .probe = bgpio_pdev_probe, 794e9f4d569SChristian Lamparter }; 795e9f4d569SChristian Lamparter 796e9f4d569SChristian Lamparter module_platform_driver(bgpio_driver); 797e9f4d569SChristian Lamparter 798e9f4d569SChristian Lamparter #endif /* CONFIG_GPIO_GENERIC_PLATFORM */ 799e9f4d569SChristian Lamparter 800e9f4d569SChristian Lamparter MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers"); 801e9f4d569SChristian Lamparter MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); 802e9f4d569SChristian Lamparter MODULE_LICENSE("GPL"); 803