gpio-idio-16.c (7ae9fb1b7ecbb5d85d07857943f677fd1a559b18) gpio-idio-16.c (db02247827ef2adc1617839b4bdcc5e1cef3e1ed)
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#include <linux/bitmap.h>
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#include <linux/bitmap.h>
7#include <linux/device.h>
8#include <linux/err.h>
7#include <linux/export.h>
9#include <linux/export.h>
10#include <linux/gpio/regmap.h>
8#include <linux/io.h>
9#include <linux/module.h>
11#include <linux/io.h>
12#include <linux/module.h>
13#include <linux/regmap.h>
10#include <linux/spinlock.h>
11#include <linux/types.h>
12
13#include "gpio-idio-16.h"
14
15#define DEFAULT_SYMBOL_NAMESPACE GPIO_IDIO_16
16
14#include <linux/spinlock.h>
15#include <linux/types.h>
16
17#include "gpio-idio-16.h"
18
19#define DEFAULT_SYMBOL_NAMESPACE GPIO_IDIO_16
20
21#define IDIO_16_DAT_BASE 0x0
22#define IDIO_16_OUT_BASE IDIO_16_DAT_BASE
23#define IDIO_16_IN_BASE (IDIO_16_DAT_BASE + 1)
24#define IDIO_16_CLEAR_INTERRUPT 0x1
25#define IDIO_16_ENABLE_IRQ 0x2
26#define IDIO_16_DEACTIVATE_INPUT_FILTERS 0x3
27#define IDIO_16_DISABLE_IRQ IDIO_16_ENABLE_IRQ
28#define IDIO_16_INTERRUPT_STATUS 0x6
29
30#define IDIO_16_NGPIO 32
31#define IDIO_16_NGPIO_PER_REG 8
32#define IDIO_16_REG_STRIDE 4
33
34struct idio_16_data {
35 struct regmap *map;
36 unsigned int irq_mask;
37};
38
39static int idio_16_handle_mask_sync(const int index, const unsigned int mask_buf_def,
40 const unsigned int mask_buf, void *const irq_drv_data)
41{
42 struct idio_16_data *const data = irq_drv_data;
43 const unsigned int prev_mask = data->irq_mask;
44 int err;
45 unsigned int val;
46
47 /* exit early if no change since the previous mask */
48 if (mask_buf == prev_mask)
49 return 0;
50
51 /* remember the current mask for the next mask sync */
52 data->irq_mask = mask_buf;
53
54 /* if all previously masked, enable interrupts when unmasking */
55 if (prev_mask == mask_buf_def) {
56 err = regmap_write(data->map, IDIO_16_CLEAR_INTERRUPT, 0x00);
57 if (err)
58 return err;
59 return regmap_read(data->map, IDIO_16_ENABLE_IRQ, &val);
60 }
61
62 /* if all are currently masked, disable interrupts */
63 if (mask_buf == mask_buf_def)
64 return regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
65
66 return 0;
67}
68
69static int idio_16_reg_mask_xlate(struct gpio_regmap *const gpio, const unsigned int base,
70 const unsigned int offset, unsigned int *const reg,
71 unsigned int *const mask)
72{
73 unsigned int stride;
74
75 /* Input lines start at GPIO 16 */
76 if (offset < 16) {
77 stride = offset / IDIO_16_NGPIO_PER_REG;
78 *reg = IDIO_16_OUT_BASE + stride * IDIO_16_REG_STRIDE;
79 } else {
80 stride = (offset - 16) / IDIO_16_NGPIO_PER_REG;
81 *reg = IDIO_16_IN_BASE + stride * IDIO_16_REG_STRIDE;
82 }
83
84 *mask = BIT(offset % IDIO_16_NGPIO_PER_REG);
85
86 return 0;
87}
88
89static const char *idio_16_names[IDIO_16_NGPIO] = {
90 "OUT0", "OUT1", "OUT2", "OUT3", "OUT4", "OUT5", "OUT6", "OUT7",
91 "OUT8", "OUT9", "OUT10", "OUT11", "OUT12", "OUT13", "OUT14", "OUT15",
92 "IIN0", "IIN1", "IIN2", "IIN3", "IIN4", "IIN5", "IIN6", "IIN7",
93 "IIN8", "IIN9", "IIN10", "IIN11", "IIN12", "IIN13", "IIN14", "IIN15",
94};
95
17/**
96/**
97 * devm_idio_16_regmap_register - Register an IDIO-16 GPIO device
98 * @dev: device that is registering this IDIO-16 GPIO device
99 * @config: configuration for idio_16_regmap_config
100 *
101 * Registers an IDIO-16 GPIO device. Returns 0 on success and negative error number on failure.
102 */
103int devm_idio_16_regmap_register(struct device *const dev,
104 const struct idio_16_regmap_config *const config)
105{
106 struct gpio_regmap_config gpio_config = {};
107 int err;
108 struct idio_16_data *data;
109 struct regmap_irq_chip *chip;
110 struct regmap_irq_chip_data *chip_data;
111
112 if (!config->parent)
113 return -EINVAL;
114
115 if (!config->map)
116 return -EINVAL;
117
118 if (!config->regmap_irqs)
119 return -EINVAL;
120
121 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
122 if (!data)
123 return -ENOMEM;
124 data->map = config->map;
125
126 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
127 if (!chip)
128 return -ENOMEM;
129
130 chip->name = dev_name(dev);
131 chip->status_base = IDIO_16_INTERRUPT_STATUS;
132 chip->mask_base = IDIO_16_ENABLE_IRQ;
133 chip->ack_base = IDIO_16_CLEAR_INTERRUPT;
134 chip->no_status = config->no_status;
135 chip->num_regs = 1;
136 chip->irqs = config->regmap_irqs;
137 chip->num_irqs = config->num_regmap_irqs;
138 chip->handle_mask_sync = idio_16_handle_mask_sync;
139 chip->irq_drv_data = data;
140
141 /* Disable IRQ to prevent spurious interrupts before we're ready */
142 err = regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
143 if (err)
144 return err;
145
146 err = devm_regmap_add_irq_chip(dev, data->map, config->irq, 0, 0, chip, &chip_data);
147 if (err)
148 return dev_err_probe(dev, err, "IRQ registration failed\n");
149
150 if (config->filters) {
151 /* Deactivate input filters */
152 err = regmap_write(data->map, IDIO_16_DEACTIVATE_INPUT_FILTERS, 0x00);
153 if (err)
154 return err;
155 }
156
157 gpio_config.parent = config->parent;
158 gpio_config.regmap = data->map;
159 gpio_config.ngpio = IDIO_16_NGPIO;
160 gpio_config.names = idio_16_names;
161 gpio_config.reg_dat_base = GPIO_REGMAP_ADDR(IDIO_16_DAT_BASE);
162 gpio_config.reg_set_base = GPIO_REGMAP_ADDR(IDIO_16_DAT_BASE);
163 gpio_config.ngpio_per_reg = IDIO_16_NGPIO_PER_REG;
164 gpio_config.reg_stride = IDIO_16_REG_STRIDE;
165 gpio_config.irq_domain = regmap_irq_get_domain(chip_data);
166 gpio_config.reg_mask_xlate = idio_16_reg_mask_xlate;
167
168 return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
169}
170EXPORT_SYMBOL_GPL(devm_idio_16_regmap_register);
171
172/**
18 * idio_16_get - get signal value at signal offset
19 * @reg: ACCES IDIO-16 device registers
20 * @state: ACCES IDIO-16 device state
21 * @offset: offset of signal to get
22 *
23 * Returns the signal value (0=low, 1=high) for the signal at @offset.
24 */
25int idio_16_get(struct idio_16 __iomem *const reg,

--- 121 unchanged lines hidden ---
173 * idio_16_get - get signal value at signal offset
174 * @reg: ACCES IDIO-16 device registers
175 * @state: ACCES IDIO-16 device state
176 * @offset: offset of signal to get
177 *
178 * Returns the signal value (0=low, 1=high) for the signal at @offset.
179 */
180int idio_16_get(struct idio_16 __iomem *const reg,

--- 121 unchanged lines hidden ---