1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * GPIO library for the ACCES IDIO-16 family
4 * Copyright (C) 2022 William Breathitt Gray
5 */
6
7 #define DEFAULT_SYMBOL_NAMESPACE "GPIO_IDIO_16"
8
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>
17
18 #include "gpio-idio-16.h"
19
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
28
29 #define IDIO_16_NGPIO 32
30 #define IDIO_16_NGPIO_PER_REG 8
31 #define IDIO_16_REG_STRIDE 4
32
33 struct idio_16_data {
34 struct regmap *map;
35 unsigned int irq_mask;
36 };
37
idio_16_handle_mask_sync(const int index,const unsigned int mask_buf_def,const unsigned int mask_buf,void * const irq_drv_data)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)
40 {
41 struct idio_16_data *const data = irq_drv_data;
42 const unsigned int prev_mask = data->irq_mask;
43 int err;
44 unsigned int val;
45
46 /* exit early if no change since the previous mask */
47 if (mask_buf == prev_mask)
48 return 0;
49
50 /* remember the current mask for the next mask sync */
51 data->irq_mask = mask_buf;
52
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);
56 if (err)
57 return err;
58 return regmap_read(data->map, IDIO_16_ENABLE_IRQ, &val);
59 }
60
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);
64
65 return 0;
66 }
67
idio_16_reg_mask_xlate(struct gpio_regmap * const gpio,const unsigned int base,const unsigned int offset,unsigned int * const reg,unsigned int * const mask)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)
71 {
72 unsigned int stride;
73
74 /* Input lines start at GPIO 16 */
75 if (offset < 16) {
76 stride = offset / IDIO_16_NGPIO_PER_REG;
77 *reg = IDIO_16_OUT_BASE + stride * IDIO_16_REG_STRIDE;
78 } else {
79 stride = (offset - 16) / IDIO_16_NGPIO_PER_REG;
80 *reg = IDIO_16_IN_BASE + stride * IDIO_16_REG_STRIDE;
81 }
82
83 *mask = BIT(offset % IDIO_16_NGPIO_PER_REG);
84
85 return 0;
86 }
87
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",
93 };
94
95 /**
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
99 *
100 * Registers an IDIO-16 GPIO device. Returns 0 on success and negative error number on failure.
101 */
devm_idio_16_regmap_register(struct device * const dev,const struct idio_16_regmap_config * const config)102 int devm_idio_16_regmap_register(struct device *const dev,
103 const struct idio_16_regmap_config *const config)
104 {
105 struct gpio_regmap_config gpio_config = {};
106 int err;
107 struct idio_16_data *data;
108 struct regmap_irq_chip *chip;
109 struct regmap_irq_chip_data *chip_data;
110
111 if (!config->parent)
112 return -EINVAL;
113
114 if (!config->map)
115 return -EINVAL;
116
117 if (!config->regmap_irqs)
118 return -EINVAL;
119
120 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
121 if (!data)
122 return -ENOMEM;
123 data->map = config->map;
124
125 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
126 if (!chip)
127 return -ENOMEM;
128
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;
134 chip->num_regs = 1;
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;
139
140 /* Disable IRQ to prevent spurious interrupts before we're ready */
141 err = regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
142 if (err)
143 return err;
144
145 err = devm_regmap_add_irq_chip(dev, data->map, config->irq, 0, 0, chip, &chip_data);
146 if (err)
147 return dev_err_probe(dev, err, "IRQ registration failed\n");
148
149 if (config->filters) {
150 /* Deactivate input filters */
151 err = regmap_write(data->map, IDIO_16_DEACTIVATE_INPUT_FILTERS, 0x00);
152 if (err)
153 return err;
154 }
155
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;
166
167 return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
168 }
169 EXPORT_SYMBOL_GPL(devm_idio_16_regmap_register);
170
171 MODULE_AUTHOR("William Breathitt Gray");
172 MODULE_DESCRIPTION("ACCES IDIO-16 GPIO Library");
173 MODULE_LICENSE("GPL");
174