1 // SPDX-License-Identifier: GPL-2.0
3 * GPIO library for the ACCES IDIO-16 family
4 * Copyright (C) 2022 William Breathitt Gray
6 #include <linux/bits.h>
7 #include <linux/device.h>
9 #include <linux/export.h>
10 #include <linux/gpio/regmap.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/types.h>
15 #include "gpio-idio-16.h"
17 #define DEFAULT_SYMBOL_NAMESPACE GPIO_IDIO_16
19 #define IDIO_16_DAT_BASE 0x0
20 #define IDIO_16_OUT_BASE IDIO_16_DAT_BASE
21 #define IDIO_16_IN_BASE (IDIO_16_DAT_BASE + 1)
22 #define IDIO_16_CLEAR_INTERRUPT 0x1
23 #define IDIO_16_ENABLE_IRQ 0x2
24 #define IDIO_16_DEACTIVATE_INPUT_FILTERS 0x3
25 #define IDIO_16_DISABLE_IRQ IDIO_16_ENABLE_IRQ
26 #define IDIO_16_INTERRUPT_STATUS 0x6
28 #define IDIO_16_NGPIO 32
29 #define IDIO_16_NGPIO_PER_REG 8
30 #define IDIO_16_REG_STRIDE 4
34 unsigned int irq_mask;
37 static int idio_16_handle_mask_sync(const int index, const unsigned int mask_buf_def,
38 const unsigned int mask_buf, void *const irq_drv_data)
40 struct idio_16_data *const data = irq_drv_data;
41 const unsigned int prev_mask = data->irq_mask;
45 /* exit early if no change since the previous mask */
46 if (mask_buf == prev_mask)
49 /* remember the current mask for the next mask sync */
50 data->irq_mask = mask_buf;
52 /* if all previously masked, enable interrupts when unmasking */
53 if (prev_mask == mask_buf_def) {
54 err = regmap_write(data->map, IDIO_16_CLEAR_INTERRUPT, 0x00);
57 return regmap_read(data->map, IDIO_16_ENABLE_IRQ, &val);
60 /* if all are currently masked, disable interrupts */
61 if (mask_buf == mask_buf_def)
62 return regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
67 static int idio_16_reg_mask_xlate(struct gpio_regmap *const gpio, const unsigned int base,
68 const unsigned int offset, unsigned int *const reg,
69 unsigned int *const mask)
73 /* Input lines start at GPIO 16 */
75 stride = offset / IDIO_16_NGPIO_PER_REG;
76 *reg = IDIO_16_OUT_BASE + stride * IDIO_16_REG_STRIDE;
78 stride = (offset - 16) / IDIO_16_NGPIO_PER_REG;
79 *reg = IDIO_16_IN_BASE + stride * IDIO_16_REG_STRIDE;
82 *mask = BIT(offset % IDIO_16_NGPIO_PER_REG);
87 static const char *idio_16_names[IDIO_16_NGPIO] = {
88 "OUT0", "OUT1", "OUT2", "OUT3", "OUT4", "OUT5", "OUT6", "OUT7",
89 "OUT8", "OUT9", "OUT10", "OUT11", "OUT12", "OUT13", "OUT14", "OUT15",
90 "IIN0", "IIN1", "IIN2", "IIN3", "IIN4", "IIN5", "IIN6", "IIN7",
91 "IIN8", "IIN9", "IIN10", "IIN11", "IIN12", "IIN13", "IIN14", "IIN15",
95 * devm_idio_16_regmap_register - Register an IDIO-16 GPIO device
96 * @dev: device that is registering this IDIO-16 GPIO device
97 * @config: configuration for idio_16_regmap_config
99 * Registers an IDIO-16 GPIO device. Returns 0 on success and negative error number on failure.
101 int devm_idio_16_regmap_register(struct device *const dev,
102 const struct idio_16_regmap_config *const config)
104 struct gpio_regmap_config gpio_config = {};
106 struct idio_16_data *data;
107 struct regmap_irq_chip *chip;
108 struct regmap_irq_chip_data *chip_data;
116 if (!config->regmap_irqs)
119 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
122 data->map = config->map;
124 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
128 chip->name = dev_name(dev);
129 chip->status_base = IDIO_16_INTERRUPT_STATUS;
130 chip->mask_base = IDIO_16_ENABLE_IRQ;
131 chip->ack_base = IDIO_16_CLEAR_INTERRUPT;
132 chip->no_status = config->no_status;
134 chip->irqs = config->regmap_irqs;
135 chip->num_irqs = config->num_regmap_irqs;
136 chip->handle_mask_sync = idio_16_handle_mask_sync;
137 chip->irq_drv_data = data;
139 /* Disable IRQ to prevent spurious interrupts before we're ready */
140 err = regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
144 err = devm_regmap_add_irq_chip(dev, data->map, config->irq, 0, 0, chip, &chip_data);
146 return dev_err_probe(dev, err, "IRQ registration failed\n");
148 if (config->filters) {
149 /* Deactivate input filters */
150 err = regmap_write(data->map, IDIO_16_DEACTIVATE_INPUT_FILTERS, 0x00);
155 gpio_config.parent = config->parent;
156 gpio_config.regmap = data->map;
157 gpio_config.ngpio = IDIO_16_NGPIO;
158 gpio_config.names = idio_16_names;
159 gpio_config.reg_dat_base = GPIO_REGMAP_ADDR(IDIO_16_DAT_BASE);
160 gpio_config.reg_set_base = GPIO_REGMAP_ADDR(IDIO_16_DAT_BASE);
161 gpio_config.ngpio_per_reg = IDIO_16_NGPIO_PER_REG;
162 gpio_config.reg_stride = IDIO_16_REG_STRIDE;
163 gpio_config.irq_domain = regmap_irq_get_domain(chip_data);
164 gpio_config.reg_mask_xlate = idio_16_reg_mask_xlate;
166 return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
168 EXPORT_SYMBOL_GPL(devm_idio_16_regmap_register);
170 MODULE_AUTHOR("William Breathitt Gray");
171 MODULE_DESCRIPTION("ACCES IDIO-16 GPIO Library");
172 MODULE_LICENSE("GPL");