Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: pci-idio-16: Migrate to the regmap API

The regmap API supports IO port accessors so we can take advantage of
regmap abstractions rather than handling access to the device registers
directly in the driver. Migrate the pci-idio-16 module to the new
idio-16 library interface leveraging the gpio-regmap API.

Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/5ba5405c64aca984d5cf3bdbdffa04c325e5a147.1680618405.git.william.gray@linaro.org/
Signed-off-by: William Breathitt Gray <william.gray@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

authored by

William Breathitt Gray and committed by
Bartosz Golaszewski
73d8f3ef 2c210c9a

+62 -234
+1 -1
drivers/gpio/Kconfig
··· 1655 1655 1656 1656 config GPIO_PCI_IDIO_16 1657 1657 tristate "ACCES PCI-IDIO-16 GPIO support" 1658 - select GPIOLIB_IRQCHIP 1658 + select REGMAP_MMIO 1659 1659 select GPIO_IDIO_16 1660 1660 help 1661 1661 Enables GPIO support for the ACCES PCI-IDIO-16. An interrupt is
+61 -233
drivers/gpio/gpio-pci-idio-16.c
··· 5 5 */ 6 6 #include <linux/bits.h> 7 7 #include <linux/device.h> 8 - #include <linux/errno.h> 9 - #include <linux/gpio/driver.h> 10 - #include <linux/interrupt.h> 11 - #include <linux/irqdesc.h> 8 + #include <linux/err.h> 9 + #include <linux/irq.h> 12 10 #include <linux/kernel.h> 13 11 #include <linux/module.h> 14 12 #include <linux/pci.h> 15 - #include <linux/spinlock.h> 13 + #include <linux/regmap.h> 16 14 #include <linux/types.h> 17 15 18 16 #include "gpio-idio-16.h" 19 17 20 - /** 21 - * struct idio_16_gpio - GPIO device private data structure 22 - * @chip: instance of the gpio_chip 23 - * @lock: synchronization lock to prevent I/O race conditions 24 - * @reg: I/O address offset for the GPIO device registers 25 - * @state: ACCES IDIO-16 device state 26 - * @irq_mask: I/O bits affected by interrupts 27 - */ 28 - struct idio_16_gpio { 29 - struct gpio_chip chip; 30 - raw_spinlock_t lock; 31 - struct idio_16 __iomem *reg; 32 - struct idio_16_state state; 33 - unsigned long irq_mask; 18 + static const struct regmap_range idio_16_wr_ranges[] = { 19 + regmap_reg_range(0x0, 0x2), regmap_reg_range(0x3, 0x4), 20 + }; 21 + static const struct regmap_range idio_16_rd_ranges[] = { 22 + regmap_reg_range(0x1, 0x2), regmap_reg_range(0x5, 0x6), 23 + }; 24 + static const struct regmap_range idio_16_precious_ranges[] = { 25 + regmap_reg_range(0x2, 0x2), 26 + }; 27 + static const struct regmap_access_table idio_16_wr_table = { 28 + .yes_ranges = idio_16_wr_ranges, 29 + .n_yes_ranges = ARRAY_SIZE(idio_16_wr_ranges), 30 + }; 31 + static const struct regmap_access_table idio_16_rd_table = { 32 + .yes_ranges = idio_16_rd_ranges, 33 + .n_yes_ranges = ARRAY_SIZE(idio_16_rd_ranges), 34 + }; 35 + static const struct regmap_access_table idio_16_precious_table = { 36 + .yes_ranges = idio_16_precious_ranges, 37 + .n_yes_ranges = ARRAY_SIZE(idio_16_precious_ranges), 38 + }; 39 + static const struct regmap_config idio_16_regmap_config = { 40 + .reg_bits = 8, 41 + .reg_stride = 1, 42 + .val_bits = 8, 43 + .io_port = true, 44 + .wr_table = &idio_16_wr_table, 45 + .rd_table = &idio_16_rd_table, 46 + .volatile_table = &idio_16_rd_table, 47 + .precious_table = &idio_16_precious_table, 48 + .cache_type = REGCACHE_FLAT, 49 + .use_raw_spinlock = true, 34 50 }; 35 51 36 - static int idio_16_gpio_get_direction(struct gpio_chip *chip, 37 - unsigned int offset) 38 - { 39 - if (idio_16_get_direction(offset)) 40 - return GPIO_LINE_DIRECTION_IN; 41 - 42 - return GPIO_LINE_DIRECTION_OUT; 43 - } 44 - 45 - static int idio_16_gpio_direction_input(struct gpio_chip *chip, 46 - unsigned int offset) 47 - { 48 - return 0; 49 - } 50 - 51 - static int idio_16_gpio_direction_output(struct gpio_chip *chip, 52 - unsigned int offset, int value) 53 - { 54 - chip->set(chip, offset, value); 55 - return 0; 56 - } 57 - 58 - static int idio_16_gpio_get(struct gpio_chip *chip, unsigned int offset) 59 - { 60 - struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); 61 - 62 - return idio_16_get(idio16gpio->reg, &idio16gpio->state, offset); 63 - } 64 - 65 - static int idio_16_gpio_get_multiple(struct gpio_chip *chip, 66 - unsigned long *mask, unsigned long *bits) 67 - { 68 - struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); 69 - 70 - idio_16_get_multiple(idio16gpio->reg, &idio16gpio->state, mask, bits); 71 - return 0; 72 - } 73 - 74 - static void idio_16_gpio_set(struct gpio_chip *chip, unsigned int offset, 75 - int value) 76 - { 77 - struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); 78 - 79 - idio_16_set(idio16gpio->reg, &idio16gpio->state, offset, value); 80 - } 81 - 82 - static void idio_16_gpio_set_multiple(struct gpio_chip *chip, 83 - unsigned long *mask, unsigned long *bits) 84 - { 85 - struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); 86 - 87 - idio_16_set_multiple(idio16gpio->reg, &idio16gpio->state, mask, bits); 88 - } 89 - 90 - static void idio_16_irq_ack(struct irq_data *data) 91 - { 92 - } 93 - 94 - static void idio_16_irq_mask(struct irq_data *data) 95 - { 96 - struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 97 - struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); 98 - const unsigned long mask = BIT(irqd_to_hwirq(data)); 99 - unsigned long flags; 100 - 101 - idio16gpio->irq_mask &= ~mask; 102 - 103 - if (!idio16gpio->irq_mask) { 104 - raw_spin_lock_irqsave(&idio16gpio->lock, flags); 105 - 106 - iowrite8(0, &idio16gpio->reg->irq_ctl); 107 - 108 - raw_spin_unlock_irqrestore(&idio16gpio->lock, flags); 52 + /* Only input lines (GPIO 16-31) support interrupts */ 53 + #define IDIO_16_REGMAP_IRQ(_id) \ 54 + [16 + _id] = { \ 55 + .mask = BIT(2), \ 56 + .type = { .types_supported = IRQ_TYPE_EDGE_BOTH }, \ 109 57 } 110 58 111 - gpiochip_disable_irq(chip, irqd_to_hwirq(data)); 112 - } 113 - 114 - static void idio_16_irq_unmask(struct irq_data *data) 115 - { 116 - struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 117 - struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip); 118 - const unsigned long mask = BIT(irqd_to_hwirq(data)); 119 - const unsigned long prev_irq_mask = idio16gpio->irq_mask; 120 - unsigned long flags; 121 - 122 - gpiochip_enable_irq(chip, irqd_to_hwirq(data)); 123 - 124 - idio16gpio->irq_mask |= mask; 125 - 126 - if (!prev_irq_mask) { 127 - raw_spin_lock_irqsave(&idio16gpio->lock, flags); 128 - 129 - ioread8(&idio16gpio->reg->irq_ctl); 130 - 131 - raw_spin_unlock_irqrestore(&idio16gpio->lock, flags); 132 - } 133 - } 134 - 135 - static int idio_16_irq_set_type(struct irq_data *data, unsigned int flow_type) 136 - { 137 - /* The only valid irq types are none and both-edges */ 138 - if (flow_type != IRQ_TYPE_NONE && 139 - (flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH) 140 - return -EINVAL; 141 - 142 - return 0; 143 - } 144 - 145 - static const struct irq_chip idio_16_irqchip = { 146 - .name = "pci-idio-16", 147 - .irq_ack = idio_16_irq_ack, 148 - .irq_mask = idio_16_irq_mask, 149 - .irq_unmask = idio_16_irq_unmask, 150 - .irq_set_type = idio_16_irq_set_type, 151 - .flags = IRQCHIP_IMMUTABLE, 152 - GPIOCHIP_IRQ_RESOURCE_HELPERS, 59 + static const struct regmap_irq idio_16_regmap_irqs[] = { 60 + IDIO_16_REGMAP_IRQ(0), IDIO_16_REGMAP_IRQ(1), IDIO_16_REGMAP_IRQ(2), /* 0-2 */ 61 + IDIO_16_REGMAP_IRQ(3), IDIO_16_REGMAP_IRQ(4), IDIO_16_REGMAP_IRQ(5), /* 3-5 */ 62 + IDIO_16_REGMAP_IRQ(6), IDIO_16_REGMAP_IRQ(7), IDIO_16_REGMAP_IRQ(8), /* 6-8 */ 63 + IDIO_16_REGMAP_IRQ(9), IDIO_16_REGMAP_IRQ(10), IDIO_16_REGMAP_IRQ(11), /* 9-11 */ 64 + IDIO_16_REGMAP_IRQ(12), IDIO_16_REGMAP_IRQ(13), IDIO_16_REGMAP_IRQ(14), /* 12-14 */ 65 + IDIO_16_REGMAP_IRQ(15), /* 15 */ 153 66 }; 154 - 155 - static irqreturn_t idio_16_irq_handler(int irq, void *dev_id) 156 - { 157 - struct idio_16_gpio *const idio16gpio = dev_id; 158 - unsigned int irq_status; 159 - struct gpio_chip *const chip = &idio16gpio->chip; 160 - int gpio; 161 - 162 - raw_spin_lock(&idio16gpio->lock); 163 - 164 - irq_status = ioread8(&idio16gpio->reg->irq_status); 165 - 166 - raw_spin_unlock(&idio16gpio->lock); 167 - 168 - /* Make sure our device generated IRQ */ 169 - if (!(irq_status & 0x3) || !(irq_status & 0x4)) 170 - return IRQ_NONE; 171 - 172 - for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio) 173 - generic_handle_domain_irq(chip->irq.domain, gpio); 174 - 175 - raw_spin_lock(&idio16gpio->lock); 176 - 177 - /* Clear interrupt */ 178 - iowrite8(0, &idio16gpio->reg->in0_7); 179 - 180 - raw_spin_unlock(&idio16gpio->lock); 181 - 182 - return IRQ_HANDLED; 183 - } 184 - 185 - #define IDIO_16_NGPIO 32 186 - static const char *idio_16_names[IDIO_16_NGPIO] = { 187 - "OUT0", "OUT1", "OUT2", "OUT3", "OUT4", "OUT5", "OUT6", "OUT7", 188 - "OUT8", "OUT9", "OUT10", "OUT11", "OUT12", "OUT13", "OUT14", "OUT15", 189 - "IIN0", "IIN1", "IIN2", "IIN3", "IIN4", "IIN5", "IIN6", "IIN7", 190 - "IIN8", "IIN9", "IIN10", "IIN11", "IIN12", "IIN13", "IIN14", "IIN15" 191 - }; 192 - 193 - static int idio_16_irq_init_hw(struct gpio_chip *gc) 194 - { 195 - struct idio_16_gpio *const idio16gpio = gpiochip_get_data(gc); 196 - 197 - /* Disable IRQ by default and clear any pending interrupt */ 198 - iowrite8(0, &idio16gpio->reg->irq_ctl); 199 - iowrite8(0, &idio16gpio->reg->in0_7); 200 - 201 - return 0; 202 - } 203 67 204 68 static int idio_16_probe(struct pci_dev *pdev, const struct pci_device_id *id) 205 69 { 206 70 struct device *const dev = &pdev->dev; 207 - struct idio_16_gpio *idio16gpio; 208 71 int err; 209 72 const size_t pci_bar_index = 2; 210 73 const char *const name = pci_name(pdev); 211 - struct gpio_irq_chip *girq; 212 - 213 - idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL); 214 - if (!idio16gpio) 215 - return -ENOMEM; 74 + struct idio_16_regmap_config config = {}; 75 + void __iomem *regs; 76 + struct regmap *map; 216 77 217 78 err = pcim_enable_device(pdev); 218 79 if (err) { ··· 87 226 return err; 88 227 } 89 228 90 - idio16gpio->reg = pcim_iomap_table(pdev)[pci_bar_index]; 229 + regs = pcim_iomap_table(pdev)[pci_bar_index]; 91 230 92 - /* Deactivate input filters */ 93 - iowrite8(0, &idio16gpio->reg->filter_ctl); 231 + map = devm_regmap_init_mmio(dev, regs, &idio_16_regmap_config); 232 + if (IS_ERR(map)) 233 + return dev_err_probe(dev, PTR_ERR(map), "Unable to initialize register map\n"); 94 234 95 - idio16gpio->chip.label = name; 96 - idio16gpio->chip.parent = dev; 97 - idio16gpio->chip.owner = THIS_MODULE; 98 - idio16gpio->chip.base = -1; 99 - idio16gpio->chip.ngpio = IDIO_16_NGPIO; 100 - idio16gpio->chip.names = idio_16_names; 101 - idio16gpio->chip.get_direction = idio_16_gpio_get_direction; 102 - idio16gpio->chip.direction_input = idio_16_gpio_direction_input; 103 - idio16gpio->chip.direction_output = idio_16_gpio_direction_output; 104 - idio16gpio->chip.get = idio_16_gpio_get; 105 - idio16gpio->chip.get_multiple = idio_16_gpio_get_multiple; 106 - idio16gpio->chip.set = idio_16_gpio_set; 107 - idio16gpio->chip.set_multiple = idio_16_gpio_set_multiple; 235 + config.parent = dev; 236 + config.map = map; 237 + config.regmap_irqs = idio_16_regmap_irqs; 238 + config.num_regmap_irqs = ARRAY_SIZE(idio_16_regmap_irqs); 239 + config.irq = pdev->irq; 240 + config.filters = true; 108 241 109 - idio_16_state_init(&idio16gpio->state); 110 - 111 - girq = &idio16gpio->chip.irq; 112 - gpio_irq_chip_set_chip(girq, &idio_16_irqchip); 113 - /* This will let us handle the parent IRQ in the driver */ 114 - girq->parent_handler = NULL; 115 - girq->num_parents = 0; 116 - girq->parents = NULL; 117 - girq->default_type = IRQ_TYPE_NONE; 118 - girq->handler = handle_edge_irq; 119 - girq->init_hw = idio_16_irq_init_hw; 120 - 121 - raw_spin_lock_init(&idio16gpio->lock); 122 - 123 - err = devm_gpiochip_add_data(dev, &idio16gpio->chip, idio16gpio); 124 - if (err) { 125 - dev_err(dev, "GPIO registering failed (%d)\n", err); 126 - return err; 127 - } 128 - 129 - err = devm_request_irq(dev, pdev->irq, idio_16_irq_handler, IRQF_SHARED, 130 - name, idio16gpio); 131 - if (err) { 132 - dev_err(dev, "IRQ handler registering failed (%d)\n", err); 133 - return err; 134 - } 135 - 136 - return 0; 242 + return devm_idio_16_regmap_register(dev, &config); 137 243 } 138 244 139 245 static const struct pci_device_id idio_16_pci_dev_id[] = {