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