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 12924efd94bSLinus Walleij static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line) 130e9f4d569SChristian Lamparter { 13124efd94bSLinus Walleij if (gc->be_bits) 13224efd94bSLinus Walleij return BIT(gc->bgpio_bits - 1 - line); 13324efd94bSLinus Walleij return BIT(line); 134e9f4d569SChristian Lamparter } 135e9f4d569SChristian Lamparter 136e9f4d569SChristian Lamparter static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio) 137e9f4d569SChristian Lamparter { 13824efd94bSLinus 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 146*80057cb4SLinus Walleij /* 147*80057cb4SLinus Walleij * This assumes that the bits in the GPIO register are in native endianness. 148*80057cb4SLinus Walleij * We only assign the function pointer if we have that. 149*80057cb4SLinus Walleij */ 150*80057cb4SLinus Walleij static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask, 151*80057cb4SLinus Walleij unsigned long *bits) 152*80057cb4SLinus Walleij { 153*80057cb4SLinus Walleij unsigned long get_mask = 0; 154*80057cb4SLinus Walleij unsigned long set_mask = 0; 155*80057cb4SLinus Walleij int bit = 0; 156*80057cb4SLinus Walleij 157*80057cb4SLinus Walleij while ((bit = find_next_bit(mask, gc->ngpio, bit)) != gc->ngpio) { 158*80057cb4SLinus Walleij if (gc->bgpio_dir & BIT(bit)) 159*80057cb4SLinus Walleij set_mask |= BIT(bit); 160*80057cb4SLinus Walleij else 161*80057cb4SLinus Walleij get_mask |= BIT(bit); 162*80057cb4SLinus Walleij } 163*80057cb4SLinus Walleij 164*80057cb4SLinus Walleij if (set_mask) 165*80057cb4SLinus Walleij *bits |= gc->read_reg(gc->reg_set) & set_mask; 166*80057cb4SLinus Walleij if (get_mask) 167*80057cb4SLinus Walleij *bits |= gc->read_reg(gc->reg_dat) & get_mask; 168*80057cb4SLinus Walleij 169*80057cb4SLinus Walleij return 0; 170*80057cb4SLinus Walleij } 171*80057cb4SLinus Walleij 172e9f4d569SChristian Lamparter static int bgpio_get(struct gpio_chip *gc, unsigned int gpio) 173e9f4d569SChristian Lamparter { 17424efd94bSLinus Walleij return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio)); 175e9f4d569SChristian Lamparter } 176e9f4d569SChristian Lamparter 177*80057cb4SLinus Walleij /* 178*80057cb4SLinus Walleij * This only works if the bits in the GPIO register are in native endianness. 179*80057cb4SLinus Walleij * It is dirt simple and fast in this case. (Also the most common case.) 180*80057cb4SLinus Walleij */ 181*80057cb4SLinus Walleij static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask, 182*80057cb4SLinus Walleij unsigned long *bits) 183*80057cb4SLinus Walleij { 184*80057cb4SLinus Walleij 185*80057cb4SLinus Walleij *bits = gc->read_reg(gc->reg_dat) & *mask; 186*80057cb4SLinus Walleij return 0; 187*80057cb4SLinus Walleij } 188*80057cb4SLinus Walleij 189*80057cb4SLinus Walleij /* 190*80057cb4SLinus Walleij * With big endian mirrored bit order it becomes more tedious. 191*80057cb4SLinus Walleij */ 192*80057cb4SLinus Walleij static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask, 193*80057cb4SLinus Walleij unsigned long *bits) 194*80057cb4SLinus Walleij { 195*80057cb4SLinus Walleij unsigned long readmask = 0; 196*80057cb4SLinus Walleij unsigned long val; 197*80057cb4SLinus Walleij int bit; 198*80057cb4SLinus Walleij 199*80057cb4SLinus Walleij /* Create a mirrored mask */ 200*80057cb4SLinus Walleij bit = 0; 201*80057cb4SLinus Walleij while ((bit = find_next_bit(mask, gc->ngpio, bit)) != gc->ngpio) 202*80057cb4SLinus Walleij readmask |= bgpio_line2mask(gc, bit); 203*80057cb4SLinus Walleij 204*80057cb4SLinus Walleij /* Read the register */ 205*80057cb4SLinus Walleij val = gc->read_reg(gc->reg_dat) & readmask; 206*80057cb4SLinus Walleij 207*80057cb4SLinus Walleij /* 208*80057cb4SLinus Walleij * Mirror the result into the "bits" result, this will give line 0 209*80057cb4SLinus Walleij * in bit 0 ... line 31 in bit 31 for a 32bit register. 210*80057cb4SLinus Walleij */ 211*80057cb4SLinus Walleij bit = 0; 212*80057cb4SLinus Walleij while ((bit = find_next_bit(&val, gc->ngpio, bit)) != gc->ngpio) 213*80057cb4SLinus Walleij *bits |= bgpio_line2mask(gc, bit); 214*80057cb4SLinus Walleij 215*80057cb4SLinus Walleij return 0; 216*80057cb4SLinus Walleij } 217*80057cb4SLinus Walleij 218e9f4d569SChristian Lamparter static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val) 219e9f4d569SChristian Lamparter { 220e9f4d569SChristian Lamparter } 221e9f4d569SChristian Lamparter 222e9f4d569SChristian Lamparter static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val) 223e9f4d569SChristian Lamparter { 22424efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 225e9f4d569SChristian Lamparter unsigned long flags; 226e9f4d569SChristian Lamparter 227e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 228e9f4d569SChristian Lamparter 229e9f4d569SChristian Lamparter if (val) 230e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 231e9f4d569SChristian Lamparter else 232e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 233e9f4d569SChristian Lamparter 234e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dat, gc->bgpio_data); 235e9f4d569SChristian Lamparter 236e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 237e9f4d569SChristian Lamparter } 238e9f4d569SChristian Lamparter 239e9f4d569SChristian Lamparter static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio, 240e9f4d569SChristian Lamparter int val) 241e9f4d569SChristian Lamparter { 24224efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 243e9f4d569SChristian Lamparter 244e9f4d569SChristian Lamparter if (val) 245e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, mask); 246e9f4d569SChristian Lamparter else 247e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, mask); 248e9f4d569SChristian Lamparter } 249e9f4d569SChristian Lamparter 250e9f4d569SChristian Lamparter static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val) 251e9f4d569SChristian Lamparter { 25224efd94bSLinus Walleij unsigned long mask = bgpio_line2mask(gc, gpio); 253e9f4d569SChristian Lamparter unsigned long flags; 254e9f4d569SChristian Lamparter 255e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 256e9f4d569SChristian Lamparter 257e9f4d569SChristian Lamparter if (val) 258e9f4d569SChristian Lamparter gc->bgpio_data |= mask; 259e9f4d569SChristian Lamparter else 260e9f4d569SChristian Lamparter gc->bgpio_data &= ~mask; 261e9f4d569SChristian Lamparter 262e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, gc->bgpio_data); 263e9f4d569SChristian Lamparter 264e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 265e9f4d569SChristian Lamparter } 266e9f4d569SChristian Lamparter 267e9f4d569SChristian Lamparter static void bgpio_multiple_get_masks(struct gpio_chip *gc, 268e9f4d569SChristian Lamparter unsigned long *mask, unsigned long *bits, 269e9f4d569SChristian Lamparter unsigned long *set_mask, 270e9f4d569SChristian Lamparter unsigned long *clear_mask) 271e9f4d569SChristian Lamparter { 272e9f4d569SChristian Lamparter int i; 273e9f4d569SChristian Lamparter 274e9f4d569SChristian Lamparter *set_mask = 0; 275e9f4d569SChristian Lamparter *clear_mask = 0; 276e9f4d569SChristian Lamparter 277e9f4d569SChristian Lamparter for (i = 0; i < gc->bgpio_bits; i++) { 278e9f4d569SChristian Lamparter if (*mask == 0) 279e9f4d569SChristian Lamparter break; 280e9f4d569SChristian Lamparter if (__test_and_clear_bit(i, mask)) { 281e9f4d569SChristian Lamparter if (test_bit(i, bits)) 28224efd94bSLinus Walleij *set_mask |= bgpio_line2mask(gc, i); 283e9f4d569SChristian Lamparter else 28424efd94bSLinus Walleij *clear_mask |= bgpio_line2mask(gc, i); 285e9f4d569SChristian Lamparter } 286e9f4d569SChristian Lamparter } 287e9f4d569SChristian Lamparter } 288e9f4d569SChristian Lamparter 289e9f4d569SChristian Lamparter static void bgpio_set_multiple_single_reg(struct gpio_chip *gc, 290e9f4d569SChristian Lamparter unsigned long *mask, 291e9f4d569SChristian Lamparter unsigned long *bits, 292e9f4d569SChristian Lamparter void __iomem *reg) 293e9f4d569SChristian Lamparter { 294e9f4d569SChristian Lamparter unsigned long flags; 295e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 296e9f4d569SChristian Lamparter 297e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 298e9f4d569SChristian Lamparter 299e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 300e9f4d569SChristian Lamparter 301e9f4d569SChristian Lamparter gc->bgpio_data |= set_mask; 302e9f4d569SChristian Lamparter gc->bgpio_data &= ~clear_mask; 303e9f4d569SChristian Lamparter 304e9f4d569SChristian Lamparter gc->write_reg(reg, gc->bgpio_data); 305e9f4d569SChristian Lamparter 306e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 307e9f4d569SChristian Lamparter } 308e9f4d569SChristian Lamparter 309e9f4d569SChristian Lamparter static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, 310e9f4d569SChristian Lamparter unsigned long *bits) 311e9f4d569SChristian Lamparter { 312e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat); 313e9f4d569SChristian Lamparter } 314e9f4d569SChristian Lamparter 315e9f4d569SChristian Lamparter static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask, 316e9f4d569SChristian Lamparter unsigned long *bits) 317e9f4d569SChristian Lamparter { 318e9f4d569SChristian Lamparter bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set); 319e9f4d569SChristian Lamparter } 320e9f4d569SChristian Lamparter 321e9f4d569SChristian Lamparter static void bgpio_set_multiple_with_clear(struct gpio_chip *gc, 322e9f4d569SChristian Lamparter unsigned long *mask, 323e9f4d569SChristian Lamparter unsigned long *bits) 324e9f4d569SChristian Lamparter { 325e9f4d569SChristian Lamparter unsigned long set_mask, clear_mask; 326e9f4d569SChristian Lamparter 327e9f4d569SChristian Lamparter bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask); 328e9f4d569SChristian Lamparter 329e9f4d569SChristian Lamparter if (set_mask) 330e9f4d569SChristian Lamparter gc->write_reg(gc->reg_set, set_mask); 331e9f4d569SChristian Lamparter if (clear_mask) 332e9f4d569SChristian Lamparter gc->write_reg(gc->reg_clr, clear_mask); 333e9f4d569SChristian Lamparter } 334e9f4d569SChristian Lamparter 335e9f4d569SChristian Lamparter static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio) 336e9f4d569SChristian Lamparter { 337e9f4d569SChristian Lamparter return 0; 338e9f4d569SChristian Lamparter } 339e9f4d569SChristian Lamparter 340e9f4d569SChristian Lamparter static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio, 341e9f4d569SChristian Lamparter int val) 342e9f4d569SChristian Lamparter { 343e9f4d569SChristian Lamparter return -EINVAL; 344e9f4d569SChristian Lamparter } 345e9f4d569SChristian Lamparter 346e9f4d569SChristian Lamparter static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio, 347e9f4d569SChristian Lamparter int val) 348e9f4d569SChristian Lamparter { 349e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 350e9f4d569SChristian Lamparter 351e9f4d569SChristian Lamparter return 0; 352e9f4d569SChristian Lamparter } 353e9f4d569SChristian Lamparter 354e9f4d569SChristian Lamparter static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) 355e9f4d569SChristian Lamparter { 356e9f4d569SChristian Lamparter unsigned long flags; 357e9f4d569SChristian Lamparter 358e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 359e9f4d569SChristian Lamparter 36024efd94bSLinus Walleij gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio); 361e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 362e9f4d569SChristian Lamparter 363e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 364e9f4d569SChristian Lamparter 365e9f4d569SChristian Lamparter return 0; 366e9f4d569SChristian Lamparter } 367e9f4d569SChristian Lamparter 368e9f4d569SChristian Lamparter static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio) 369e9f4d569SChristian Lamparter { 370e9f4d569SChristian Lamparter /* Return 0 if output, 1 of input */ 37124efd94bSLinus Walleij return !(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio)); 372e9f4d569SChristian Lamparter } 373e9f4d569SChristian Lamparter 374e9f4d569SChristian Lamparter static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) 375e9f4d569SChristian Lamparter { 376e9f4d569SChristian Lamparter unsigned long flags; 377e9f4d569SChristian Lamparter 378e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 379e9f4d569SChristian Lamparter 380e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 381e9f4d569SChristian Lamparter 38224efd94bSLinus Walleij gc->bgpio_dir |= bgpio_line2mask(gc, gpio); 383e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 384e9f4d569SChristian Lamparter 385e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 386e9f4d569SChristian Lamparter 387e9f4d569SChristian Lamparter return 0; 388e9f4d569SChristian Lamparter } 389e9f4d569SChristian Lamparter 390e9f4d569SChristian Lamparter static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio) 391e9f4d569SChristian Lamparter { 392e9f4d569SChristian Lamparter unsigned long flags; 393e9f4d569SChristian Lamparter 394e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 395e9f4d569SChristian Lamparter 39624efd94bSLinus Walleij gc->bgpio_dir |= bgpio_line2mask(gc, gpio); 397e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 398e9f4d569SChristian Lamparter 399e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 400e9f4d569SChristian Lamparter 401e9f4d569SChristian Lamparter return 0; 402e9f4d569SChristian Lamparter } 403e9f4d569SChristian Lamparter 404e9f4d569SChristian Lamparter static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val) 405e9f4d569SChristian Lamparter { 406e9f4d569SChristian Lamparter unsigned long flags; 407e9f4d569SChristian Lamparter 408e9f4d569SChristian Lamparter gc->set(gc, gpio, val); 409e9f4d569SChristian Lamparter 410e9f4d569SChristian Lamparter spin_lock_irqsave(&gc->bgpio_lock, flags); 411e9f4d569SChristian Lamparter 41224efd94bSLinus Walleij gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio); 413e9f4d569SChristian Lamparter gc->write_reg(gc->reg_dir, gc->bgpio_dir); 414e9f4d569SChristian Lamparter 415e9f4d569SChristian Lamparter spin_unlock_irqrestore(&gc->bgpio_lock, flags); 416e9f4d569SChristian Lamparter 417e9f4d569SChristian Lamparter return 0; 418e9f4d569SChristian Lamparter } 419e9f4d569SChristian Lamparter 420e9f4d569SChristian Lamparter static int bgpio_get_dir_inv(struct gpio_chip *gc, unsigned int gpio) 421e9f4d569SChristian Lamparter { 422e9f4d569SChristian Lamparter /* Return 0 if output, 1 if input */ 42324efd94bSLinus Walleij return !!(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio)); 424e9f4d569SChristian Lamparter } 425e9f4d569SChristian Lamparter 426e9f4d569SChristian Lamparter static int bgpio_setup_accessors(struct device *dev, 427e9f4d569SChristian Lamparter struct gpio_chip *gc, 428e9f4d569SChristian Lamparter bool byte_be) 429e9f4d569SChristian Lamparter { 430e9f4d569SChristian Lamparter 431e9f4d569SChristian Lamparter switch (gc->bgpio_bits) { 432e9f4d569SChristian Lamparter case 8: 433e9f4d569SChristian Lamparter gc->read_reg = bgpio_read8; 434e9f4d569SChristian Lamparter gc->write_reg = bgpio_write8; 435e9f4d569SChristian Lamparter break; 436e9f4d569SChristian Lamparter case 16: 437e9f4d569SChristian Lamparter if (byte_be) { 438e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16be; 439e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16be; 440e9f4d569SChristian Lamparter } else { 441e9f4d569SChristian Lamparter gc->read_reg = bgpio_read16; 442e9f4d569SChristian Lamparter gc->write_reg = bgpio_write16; 443e9f4d569SChristian Lamparter } 444e9f4d569SChristian Lamparter break; 445e9f4d569SChristian Lamparter case 32: 446e9f4d569SChristian Lamparter if (byte_be) { 447e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32be; 448e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32be; 449e9f4d569SChristian Lamparter } else { 450e9f4d569SChristian Lamparter gc->read_reg = bgpio_read32; 451e9f4d569SChristian Lamparter gc->write_reg = bgpio_write32; 452e9f4d569SChristian Lamparter } 453e9f4d569SChristian Lamparter break; 454e9f4d569SChristian Lamparter #if BITS_PER_LONG >= 64 455e9f4d569SChristian Lamparter case 64: 456e9f4d569SChristian Lamparter if (byte_be) { 457e9f4d569SChristian Lamparter dev_err(dev, 458e9f4d569SChristian Lamparter "64 bit big endian byte order unsupported\n"); 459e9f4d569SChristian Lamparter return -EINVAL; 460e9f4d569SChristian Lamparter } else { 461e9f4d569SChristian Lamparter gc->read_reg = bgpio_read64; 462e9f4d569SChristian Lamparter gc->write_reg = bgpio_write64; 463e9f4d569SChristian Lamparter } 464e9f4d569SChristian Lamparter break; 465e9f4d569SChristian Lamparter #endif /* BITS_PER_LONG >= 64 */ 466e9f4d569SChristian Lamparter default: 467e9f4d569SChristian Lamparter dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits); 468e9f4d569SChristian Lamparter return -EINVAL; 469e9f4d569SChristian Lamparter } 470e9f4d569SChristian Lamparter 471e9f4d569SChristian Lamparter return 0; 472e9f4d569SChristian Lamparter } 473e9f4d569SChristian Lamparter 474e9f4d569SChristian Lamparter /* 475e9f4d569SChristian Lamparter * Create the device and allocate the resources. For setting GPIO's there are 476e9f4d569SChristian Lamparter * three supported configurations: 477e9f4d569SChristian Lamparter * 478e9f4d569SChristian Lamparter * - single input/output register resource (named "dat"). 479e9f4d569SChristian Lamparter * - set/clear pair (named "set" and "clr"). 480e9f4d569SChristian Lamparter * - single output register resource and single input resource ("set" and 481e9f4d569SChristian Lamparter * dat"). 482e9f4d569SChristian Lamparter * 483e9f4d569SChristian Lamparter * For the single output register, this drives a 1 by setting a bit and a zero 484e9f4d569SChristian Lamparter * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit 485e9f4d569SChristian Lamparter * in the set register and clears it by setting a bit in the clear register. 486e9f4d569SChristian Lamparter * The configuration is detected by which resources are present. 487e9f4d569SChristian Lamparter * 488e9f4d569SChristian Lamparter * For setting the GPIO direction, there are three supported configurations: 489e9f4d569SChristian Lamparter * 490e9f4d569SChristian Lamparter * - simple bidirection GPIO that requires no configuration. 491e9f4d569SChristian Lamparter * - an output direction register (named "dirout") where a 1 bit 492e9f4d569SChristian Lamparter * indicates the GPIO is an output. 493e9f4d569SChristian Lamparter * - an input direction register (named "dirin") where a 1 bit indicates 494e9f4d569SChristian Lamparter * the GPIO is an input. 495e9f4d569SChristian Lamparter */ 496e9f4d569SChristian Lamparter static int bgpio_setup_io(struct gpio_chip *gc, 497e9f4d569SChristian Lamparter void __iomem *dat, 498e9f4d569SChristian Lamparter void __iomem *set, 499e9f4d569SChristian Lamparter void __iomem *clr, 500e9f4d569SChristian Lamparter unsigned long flags) 501e9f4d569SChristian Lamparter { 502e9f4d569SChristian Lamparter 503e9f4d569SChristian Lamparter gc->reg_dat = dat; 504e9f4d569SChristian Lamparter if (!gc->reg_dat) 505e9f4d569SChristian Lamparter return -EINVAL; 506e9f4d569SChristian Lamparter 507e9f4d569SChristian Lamparter if (set && clr) { 508e9f4d569SChristian Lamparter gc->reg_set = set; 509e9f4d569SChristian Lamparter gc->reg_clr = clr; 510e9f4d569SChristian Lamparter gc->set = bgpio_set_with_clear; 511e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_with_clear; 512e9f4d569SChristian Lamparter } else if (set && !clr) { 513e9f4d569SChristian Lamparter gc->reg_set = set; 514e9f4d569SChristian Lamparter gc->set = bgpio_set_set; 515e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple_set; 516e9f4d569SChristian Lamparter } else if (flags & BGPIOF_NO_OUTPUT) { 517e9f4d569SChristian Lamparter gc->set = bgpio_set_none; 518e9f4d569SChristian Lamparter gc->set_multiple = NULL; 519e9f4d569SChristian Lamparter } else { 520e9f4d569SChristian Lamparter gc->set = bgpio_set; 521e9f4d569SChristian Lamparter gc->set_multiple = bgpio_set_multiple; 522e9f4d569SChristian Lamparter } 523e9f4d569SChristian Lamparter 524e9f4d569SChristian Lamparter if (!(flags & BGPIOF_UNREADABLE_REG_SET) && 525*80057cb4SLinus Walleij (flags & BGPIOF_READ_OUTPUT_REG_SET)) { 526e9f4d569SChristian Lamparter gc->get = bgpio_get_set; 527*80057cb4SLinus Walleij if (!gc->be_bits) 528*80057cb4SLinus Walleij gc->get_multiple = bgpio_get_set_multiple; 529*80057cb4SLinus Walleij /* 530*80057cb4SLinus Walleij * We deliberately avoid assigning the ->get_multiple() call 531*80057cb4SLinus Walleij * for big endian mirrored registers which are ALSO reflecting 532*80057cb4SLinus Walleij * their value in the set register when used as output. It is 533*80057cb4SLinus Walleij * simply too much complexity, let the GPIO core fall back to 534*80057cb4SLinus Walleij * reading each line individually in that fringe case. 535*80057cb4SLinus Walleij */ 536*80057cb4SLinus Walleij } else { 537e9f4d569SChristian Lamparter gc->get = bgpio_get; 538*80057cb4SLinus Walleij if (gc->be_bits) 539*80057cb4SLinus Walleij gc->get_multiple = bgpio_get_multiple_be; 540*80057cb4SLinus Walleij else 541*80057cb4SLinus Walleij gc->get_multiple = bgpio_get_multiple; 542*80057cb4SLinus Walleij } 543e9f4d569SChristian Lamparter 544e9f4d569SChristian Lamparter return 0; 545e9f4d569SChristian Lamparter } 546e9f4d569SChristian Lamparter 547e9f4d569SChristian Lamparter static int bgpio_setup_direction(struct gpio_chip *gc, 548e9f4d569SChristian Lamparter void __iomem *dirout, 549e9f4d569SChristian Lamparter void __iomem *dirin, 550e9f4d569SChristian Lamparter unsigned long flags) 551e9f4d569SChristian Lamparter { 552e9f4d569SChristian Lamparter if (dirout && dirin) { 553e9f4d569SChristian Lamparter return -EINVAL; 554e9f4d569SChristian Lamparter } else if (dirout) { 555e9f4d569SChristian Lamparter gc->reg_dir = dirout; 556e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out; 557e9f4d569SChristian Lamparter gc->direction_input = bgpio_dir_in; 558e9f4d569SChristian Lamparter gc->get_direction = bgpio_get_dir; 559e9f4d569SChristian Lamparter } else if (dirin) { 560e9f4d569SChristian Lamparter gc->reg_dir = dirin; 561e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out_inv; 562e9f4d569SChristian Lamparter gc->direction_input = bgpio_dir_in_inv; 563e9f4d569SChristian Lamparter gc->get_direction = bgpio_get_dir_inv; 564e9f4d569SChristian Lamparter } else { 565e9f4d569SChristian Lamparter if (flags & BGPIOF_NO_OUTPUT) 566e9f4d569SChristian Lamparter gc->direction_output = bgpio_dir_out_err; 567e9f4d569SChristian Lamparter else 568e9f4d569SChristian Lamparter gc->direction_output = bgpio_simple_dir_out; 569e9f4d569SChristian Lamparter gc->direction_input = bgpio_simple_dir_in; 570e9f4d569SChristian Lamparter } 571e9f4d569SChristian Lamparter 572e9f4d569SChristian Lamparter return 0; 573e9f4d569SChristian Lamparter } 574e9f4d569SChristian Lamparter 575e9f4d569SChristian Lamparter static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin) 576e9f4d569SChristian Lamparter { 577e9f4d569SChristian Lamparter if (gpio_pin < chip->ngpio) 578e9f4d569SChristian Lamparter return 0; 579e9f4d569SChristian Lamparter 580e9f4d569SChristian Lamparter return -EINVAL; 581e9f4d569SChristian Lamparter } 582e9f4d569SChristian Lamparter 583e9f4d569SChristian Lamparter int bgpio_init(struct gpio_chip *gc, struct device *dev, 584e9f4d569SChristian Lamparter unsigned long sz, void __iomem *dat, void __iomem *set, 585e9f4d569SChristian Lamparter void __iomem *clr, void __iomem *dirout, void __iomem *dirin, 586e9f4d569SChristian Lamparter unsigned long flags) 587e9f4d569SChristian Lamparter { 588e9f4d569SChristian Lamparter int ret; 589e9f4d569SChristian Lamparter 590e9f4d569SChristian Lamparter if (!is_power_of_2(sz)) 591e9f4d569SChristian Lamparter return -EINVAL; 592e9f4d569SChristian Lamparter 593e9f4d569SChristian Lamparter gc->bgpio_bits = sz * 8; 594e9f4d569SChristian Lamparter if (gc->bgpio_bits > BITS_PER_LONG) 595e9f4d569SChristian Lamparter return -EINVAL; 596e9f4d569SChristian Lamparter 597e9f4d569SChristian Lamparter spin_lock_init(&gc->bgpio_lock); 598e9f4d569SChristian Lamparter gc->parent = dev; 599e9f4d569SChristian Lamparter gc->label = dev_name(dev); 600e9f4d569SChristian Lamparter gc->base = -1; 601e9f4d569SChristian Lamparter gc->ngpio = gc->bgpio_bits; 602e9f4d569SChristian Lamparter gc->request = bgpio_request; 603*80057cb4SLinus Walleij gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN); 604e9f4d569SChristian Lamparter 605e9f4d569SChristian Lamparter ret = bgpio_setup_io(gc, dat, set, clr, flags); 606e9f4d569SChristian Lamparter if (ret) 607e9f4d569SChristian Lamparter return ret; 608e9f4d569SChristian Lamparter 60924efd94bSLinus Walleij ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER); 610e9f4d569SChristian Lamparter if (ret) 611e9f4d569SChristian Lamparter return ret; 612e9f4d569SChristian Lamparter 613e9f4d569SChristian Lamparter ret = bgpio_setup_direction(gc, dirout, dirin, flags); 614e9f4d569SChristian Lamparter if (ret) 615e9f4d569SChristian Lamparter return ret; 616e9f4d569SChristian Lamparter 617e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_dat); 618e9f4d569SChristian Lamparter if (gc->set == bgpio_set_set && 619e9f4d569SChristian Lamparter !(flags & BGPIOF_UNREADABLE_REG_SET)) 620e9f4d569SChristian Lamparter gc->bgpio_data = gc->read_reg(gc->reg_set); 621e9f4d569SChristian Lamparter if (gc->reg_dir && !(flags & BGPIOF_UNREADABLE_REG_DIR)) 622e9f4d569SChristian Lamparter gc->bgpio_dir = gc->read_reg(gc->reg_dir); 623e9f4d569SChristian Lamparter 624e9f4d569SChristian Lamparter return ret; 625e9f4d569SChristian Lamparter } 626e9f4d569SChristian Lamparter EXPORT_SYMBOL_GPL(bgpio_init); 627e9f4d569SChristian Lamparter 628e9f4d569SChristian Lamparter #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM) 629e9f4d569SChristian Lamparter 630e9f4d569SChristian Lamparter static void __iomem *bgpio_map(struct platform_device *pdev, 631e9f4d569SChristian Lamparter const char *name, 632e9f4d569SChristian Lamparter resource_size_t sane_sz) 633e9f4d569SChristian Lamparter { 634e9f4d569SChristian Lamparter struct resource *r; 635e9f4d569SChristian Lamparter resource_size_t sz; 636e9f4d569SChristian Lamparter 637e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); 638e9f4d569SChristian Lamparter if (!r) 639e9f4d569SChristian Lamparter return NULL; 640e9f4d569SChristian Lamparter 641e9f4d569SChristian Lamparter sz = resource_size(r); 642e9f4d569SChristian Lamparter if (sz != sane_sz) 643e9f4d569SChristian Lamparter return IOMEM_ERR_PTR(-EINVAL); 644e9f4d569SChristian Lamparter 645e9f4d569SChristian Lamparter return devm_ioremap_resource(&pdev->dev, r); 646e9f4d569SChristian Lamparter } 647e9f4d569SChristian Lamparter 648e698613aSÁlvaro Fernández Rojas #ifdef CONFIG_OF 649e698613aSÁlvaro Fernández Rojas static const struct of_device_id bgpio_of_match[] = { 65005cc995fSChristian Lamparter { .compatible = "brcm,bcm6345-gpio" }, 651c0d30ecfSChristian Lamparter { .compatible = "wd,mbl-gpio" }, 652b8c90199SNathan Sullivan { .compatible = "ni,169445-nand-gpio" }, 653e698613aSÁlvaro Fernández Rojas { } 654e698613aSÁlvaro Fernández Rojas }; 655e698613aSÁlvaro Fernández Rojas MODULE_DEVICE_TABLE(of, bgpio_of_match); 656e698613aSÁlvaro Fernández Rojas 657e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 658e698613aSÁlvaro Fernández Rojas unsigned long *flags) 659e698613aSÁlvaro Fernández Rojas { 660e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 661e698613aSÁlvaro Fernández Rojas 662e698613aSÁlvaro Fernández Rojas if (!of_match_device(bgpio_of_match, &pdev->dev)) 663e698613aSÁlvaro Fernández Rojas return NULL; 664e698613aSÁlvaro Fernández Rojas 665e698613aSÁlvaro Fernández Rojas pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata), 666e698613aSÁlvaro Fernández Rojas GFP_KERNEL); 667e698613aSÁlvaro Fernández Rojas if (!pdata) 668e698613aSÁlvaro Fernández Rojas return ERR_PTR(-ENOMEM); 669e698613aSÁlvaro Fernández Rojas 670e698613aSÁlvaro Fernández Rojas pdata->base = -1; 671e698613aSÁlvaro Fernández Rojas 67205cc995fSChristian Lamparter if (of_device_is_big_endian(pdev->dev.of_node)) 67305cc995fSChristian Lamparter *flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER; 67405cc995fSChristian Lamparter 675c0d30ecfSChristian Lamparter if (of_property_read_bool(pdev->dev.of_node, "no-output")) 676c0d30ecfSChristian Lamparter *flags |= BGPIOF_NO_OUTPUT; 677c0d30ecfSChristian Lamparter 678e698613aSÁlvaro Fernández Rojas return pdata; 679e698613aSÁlvaro Fernández Rojas } 680e698613aSÁlvaro Fernández Rojas #else 681e698613aSÁlvaro Fernández Rojas static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev, 682e698613aSÁlvaro Fernández Rojas unsigned long *flags) 683e698613aSÁlvaro Fernández Rojas { 684e698613aSÁlvaro Fernández Rojas return NULL; 685e698613aSÁlvaro Fernández Rojas } 686e698613aSÁlvaro Fernández Rojas #endif /* CONFIG_OF */ 687e698613aSÁlvaro Fernández Rojas 688e9f4d569SChristian Lamparter static int bgpio_pdev_probe(struct platform_device *pdev) 689e9f4d569SChristian Lamparter { 690e9f4d569SChristian Lamparter struct device *dev = &pdev->dev; 691e9f4d569SChristian Lamparter struct resource *r; 692e9f4d569SChristian Lamparter void __iomem *dat; 693e9f4d569SChristian Lamparter void __iomem *set; 694e9f4d569SChristian Lamparter void __iomem *clr; 695e9f4d569SChristian Lamparter void __iomem *dirout; 696e9f4d569SChristian Lamparter void __iomem *dirin; 697e9f4d569SChristian Lamparter unsigned long sz; 698e698613aSÁlvaro Fernández Rojas unsigned long flags = 0; 699e9f4d569SChristian Lamparter int err; 700e9f4d569SChristian Lamparter struct gpio_chip *gc; 701e698613aSÁlvaro Fernández Rojas struct bgpio_pdata *pdata; 702e698613aSÁlvaro Fernández Rojas 703e698613aSÁlvaro Fernández Rojas pdata = bgpio_parse_dt(pdev, &flags); 704e698613aSÁlvaro Fernández Rojas if (IS_ERR(pdata)) 705e698613aSÁlvaro Fernández Rojas return PTR_ERR(pdata); 706e698613aSÁlvaro Fernández Rojas 707e698613aSÁlvaro Fernández Rojas if (!pdata) { 708e698613aSÁlvaro Fernández Rojas pdata = dev_get_platdata(dev); 709e698613aSÁlvaro Fernández Rojas flags = pdev->id_entry->driver_data; 710e698613aSÁlvaro Fernández Rojas } 711e9f4d569SChristian Lamparter 712e9f4d569SChristian Lamparter r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat"); 713e9f4d569SChristian Lamparter if (!r) 714e9f4d569SChristian Lamparter return -EINVAL; 715e9f4d569SChristian Lamparter 716e9f4d569SChristian Lamparter sz = resource_size(r); 717e9f4d569SChristian Lamparter 718e9f4d569SChristian Lamparter dat = bgpio_map(pdev, "dat", sz); 719e9f4d569SChristian Lamparter if (IS_ERR(dat)) 720e9f4d569SChristian Lamparter return PTR_ERR(dat); 721e9f4d569SChristian Lamparter 722e9f4d569SChristian Lamparter set = bgpio_map(pdev, "set", sz); 723e9f4d569SChristian Lamparter if (IS_ERR(set)) 724e9f4d569SChristian Lamparter return PTR_ERR(set); 725e9f4d569SChristian Lamparter 726e9f4d569SChristian Lamparter clr = bgpio_map(pdev, "clr", sz); 727e9f4d569SChristian Lamparter if (IS_ERR(clr)) 728e9f4d569SChristian Lamparter return PTR_ERR(clr); 729e9f4d569SChristian Lamparter 730e9f4d569SChristian Lamparter dirout = bgpio_map(pdev, "dirout", sz); 731e9f4d569SChristian Lamparter if (IS_ERR(dirout)) 732e9f4d569SChristian Lamparter return PTR_ERR(dirout); 733e9f4d569SChristian Lamparter 734e9f4d569SChristian Lamparter dirin = bgpio_map(pdev, "dirin", sz); 735e9f4d569SChristian Lamparter if (IS_ERR(dirin)) 736e9f4d569SChristian Lamparter return PTR_ERR(dirin); 737e9f4d569SChristian Lamparter 738e9f4d569SChristian Lamparter gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL); 739e9f4d569SChristian Lamparter if (!gc) 740e9f4d569SChristian Lamparter return -ENOMEM; 741e9f4d569SChristian Lamparter 742e9f4d569SChristian Lamparter err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags); 743e9f4d569SChristian Lamparter if (err) 744e9f4d569SChristian Lamparter return err; 745e9f4d569SChristian Lamparter 746e9f4d569SChristian Lamparter if (pdata) { 747e9f4d569SChristian Lamparter if (pdata->label) 748e9f4d569SChristian Lamparter gc->label = pdata->label; 749e9f4d569SChristian Lamparter gc->base = pdata->base; 750e9f4d569SChristian Lamparter if (pdata->ngpio > 0) 751e9f4d569SChristian Lamparter gc->ngpio = pdata->ngpio; 752e9f4d569SChristian Lamparter } 753e9f4d569SChristian Lamparter 754e9f4d569SChristian Lamparter platform_set_drvdata(pdev, gc); 755e9f4d569SChristian Lamparter 756e9f4d569SChristian Lamparter return devm_gpiochip_add_data(&pdev->dev, gc, NULL); 757e9f4d569SChristian Lamparter } 758e9f4d569SChristian Lamparter 759e9f4d569SChristian Lamparter static const struct platform_device_id bgpio_id_table[] = { 760e9f4d569SChristian Lamparter { 761e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 762e9f4d569SChristian Lamparter .driver_data = 0, 763e9f4d569SChristian Lamparter }, { 764e9f4d569SChristian Lamparter .name = "basic-mmio-gpio-be", 765e9f4d569SChristian Lamparter .driver_data = BGPIOF_BIG_ENDIAN, 766e9f4d569SChristian Lamparter }, 767e9f4d569SChristian Lamparter { } 768e9f4d569SChristian Lamparter }; 769e9f4d569SChristian Lamparter MODULE_DEVICE_TABLE(platform, bgpio_id_table); 770e9f4d569SChristian Lamparter 771e9f4d569SChristian Lamparter static struct platform_driver bgpio_driver = { 772e9f4d569SChristian Lamparter .driver = { 773e9f4d569SChristian Lamparter .name = "basic-mmio-gpio", 774e698613aSÁlvaro Fernández Rojas .of_match_table = of_match_ptr(bgpio_of_match), 775e9f4d569SChristian Lamparter }, 776e9f4d569SChristian Lamparter .id_table = bgpio_id_table, 777e9f4d569SChristian Lamparter .probe = bgpio_pdev_probe, 778e9f4d569SChristian Lamparter }; 779e9f4d569SChristian Lamparter 780e9f4d569SChristian Lamparter module_platform_driver(bgpio_driver); 781e9f4d569SChristian Lamparter 782e9f4d569SChristian Lamparter #endif /* CONFIG_GPIO_GENERIC_PLATFORM */ 783e9f4d569SChristian Lamparter 784e9f4d569SChristian Lamparter MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers"); 785e9f4d569SChristian Lamparter MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); 786e9f4d569SChristian Lamparter MODULE_LICENSE("GPL"); 787