Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * regmap based generic GPIO driver
4 *
5 * Copyright 2020 Michael Walle <michael@walle.cc>
6 */
7
8#include <linux/bits.h>
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/io.h>
12#include <linux/module.h>
13#include <linux/regmap.h>
14#include <linux/slab.h>
15#include <linux/types.h>
16
17#include <linux/gpio/driver.h>
18#include <linux/gpio/regmap.h>
19
20#include "gpiolib.h"
21
22struct gpio_regmap {
23 struct device *parent;
24 struct regmap *regmap;
25 struct gpio_chip gpio_chip;
26
27 int reg_stride;
28 int ngpio_per_reg;
29 unsigned int reg_dat_base;
30 unsigned int reg_set_base;
31 unsigned int reg_clr_base;
32 unsigned int reg_dir_in_base;
33 unsigned int reg_dir_out_base;
34
35 int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base,
36 unsigned int offset, unsigned int *reg,
37 unsigned int *mask);
38
39 void *driver_data;
40};
41
42static unsigned int gpio_regmap_addr(unsigned int addr)
43{
44 if (addr == GPIO_REGMAP_ADDR_ZERO)
45 return 0;
46
47 return addr;
48}
49
50static int gpio_regmap_simple_xlate(struct gpio_regmap *gpio,
51 unsigned int base, unsigned int offset,
52 unsigned int *reg, unsigned int *mask)
53{
54 unsigned int line = offset % gpio->ngpio_per_reg;
55 unsigned int stride = offset / gpio->ngpio_per_reg;
56
57 *reg = base + stride * gpio->reg_stride;
58 *mask = BIT(line);
59
60 return 0;
61}
62
63static int gpio_regmap_get(struct gpio_chip *chip, unsigned int offset)
64{
65 struct gpio_regmap *gpio = gpiochip_get_data(chip);
66 unsigned int base, val, reg, mask;
67 int ret;
68
69 /* we might not have an output register if we are input only */
70 if (gpio->reg_dat_base)
71 base = gpio_regmap_addr(gpio->reg_dat_base);
72 else
73 base = gpio_regmap_addr(gpio->reg_set_base);
74
75 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask);
76 if (ret)
77 return ret;
78
79 ret = regmap_read(gpio->regmap, reg, &val);
80 if (ret)
81 return ret;
82
83 return !!(val & mask);
84}
85
86static int gpio_regmap_set(struct gpio_chip *chip, unsigned int offset,
87 int val)
88{
89 struct gpio_regmap *gpio = gpiochip_get_data(chip);
90 unsigned int base = gpio_regmap_addr(gpio->reg_set_base);
91 unsigned int reg, mask;
92 int ret;
93
94 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask);
95 if (ret)
96 return ret;
97
98 if (val)
99 ret = regmap_update_bits(gpio->regmap, reg, mask, mask);
100 else
101 ret = regmap_update_bits(gpio->regmap, reg, mask, 0);
102
103 return ret;
104}
105
106static int gpio_regmap_set_with_clear(struct gpio_chip *chip,
107 unsigned int offset, int val)
108{
109 struct gpio_regmap *gpio = gpiochip_get_data(chip);
110 unsigned int base, reg, mask;
111 int ret;
112
113 if (val)
114 base = gpio_regmap_addr(gpio->reg_set_base);
115 else
116 base = gpio_regmap_addr(gpio->reg_clr_base);
117
118 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask);
119 if (ret)
120 return ret;
121
122 return regmap_write(gpio->regmap, reg, mask);
123}
124
125static int gpio_regmap_get_direction(struct gpio_chip *chip,
126 unsigned int offset)
127{
128 struct gpio_regmap *gpio = gpiochip_get_data(chip);
129 unsigned int base, val, reg, mask;
130 int invert, ret;
131
132 if (gpio->reg_dat_base && !gpio->reg_set_base)
133 return GPIO_LINE_DIRECTION_IN;
134 if (gpio->reg_set_base && !gpio->reg_dat_base)
135 return GPIO_LINE_DIRECTION_OUT;
136
137 if (gpio->reg_dir_out_base) {
138 base = gpio_regmap_addr(gpio->reg_dir_out_base);
139 invert = 0;
140 } else if (gpio->reg_dir_in_base) {
141 base = gpio_regmap_addr(gpio->reg_dir_in_base);
142 invert = 1;
143 } else {
144 return -ENOTSUPP;
145 }
146
147 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask);
148 if (ret)
149 return ret;
150
151 ret = regmap_read(gpio->regmap, reg, &val);
152 if (ret)
153 return ret;
154
155 if (!!(val & mask) ^ invert)
156 return GPIO_LINE_DIRECTION_OUT;
157 else
158 return GPIO_LINE_DIRECTION_IN;
159}
160
161static int gpio_regmap_set_direction(struct gpio_chip *chip,
162 unsigned int offset, bool output)
163{
164 struct gpio_regmap *gpio = gpiochip_get_data(chip);
165 unsigned int base, val, reg, mask;
166 int invert, ret;
167
168 if (gpio->reg_dir_out_base) {
169 base = gpio_regmap_addr(gpio->reg_dir_out_base);
170 invert = 0;
171 } else if (gpio->reg_dir_in_base) {
172 base = gpio_regmap_addr(gpio->reg_dir_in_base);
173 invert = 1;
174 } else {
175 return -ENOTSUPP;
176 }
177
178 ret = gpio->reg_mask_xlate(gpio, base, offset, ®, &mask);
179 if (ret)
180 return ret;
181
182 if (invert)
183 val = output ? 0 : mask;
184 else
185 val = output ? mask : 0;
186
187 return regmap_update_bits(gpio->regmap, reg, mask, val);
188}
189
190static int gpio_regmap_direction_input(struct gpio_chip *chip,
191 unsigned int offset)
192{
193 return gpio_regmap_set_direction(chip, offset, false);
194}
195
196static int gpio_regmap_direction_output(struct gpio_chip *chip,
197 unsigned int offset, int value)
198{
199 gpio_regmap_set(chip, offset, value);
200
201 return gpio_regmap_set_direction(chip, offset, true);
202}
203
204void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio)
205{
206 return gpio->driver_data;
207}
208EXPORT_SYMBOL_GPL(gpio_regmap_get_drvdata);
209
210/**
211 * gpio_regmap_register() - Register a generic regmap GPIO controller
212 * @config: configuration for gpio_regmap
213 *
214 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value.
215 */
216struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config)
217{
218 struct gpio_regmap *gpio;
219 struct gpio_chip *chip;
220 int ret;
221
222 if (!config->parent)
223 return ERR_PTR(-EINVAL);
224
225 /* we need at least one */
226 if (!config->reg_dat_base && !config->reg_set_base)
227 return ERR_PTR(-EINVAL);
228
229 /* if we have a direction register we need both input and output */
230 if ((config->reg_dir_out_base || config->reg_dir_in_base) &&
231 (!config->reg_dat_base || !config->reg_set_base))
232 return ERR_PTR(-EINVAL);
233
234 /* we don't support having both registers simultaneously for now */
235 if (config->reg_dir_out_base && config->reg_dir_in_base)
236 return ERR_PTR(-EINVAL);
237
238 gpio = kzalloc(sizeof(*gpio), GFP_KERNEL);
239 if (!gpio)
240 return ERR_PTR(-ENOMEM);
241
242 gpio->parent = config->parent;
243 gpio->driver_data = config->drvdata;
244 gpio->regmap = config->regmap;
245 gpio->reg_dat_base = config->reg_dat_base;
246 gpio->reg_set_base = config->reg_set_base;
247 gpio->reg_clr_base = config->reg_clr_base;
248 gpio->reg_dir_in_base = config->reg_dir_in_base;
249 gpio->reg_dir_out_base = config->reg_dir_out_base;
250
251 chip = &gpio->gpio_chip;
252 chip->parent = config->parent;
253 chip->fwnode = config->fwnode;
254 chip->base = -1;
255 chip->names = config->names;
256 chip->label = config->label ?: dev_name(config->parent);
257 chip->can_sleep = regmap_might_sleep(config->regmap);
258
259 chip->request = gpiochip_generic_request;
260 chip->free = gpiochip_generic_free;
261 chip->get = gpio_regmap_get;
262 if (gpio->reg_set_base && gpio->reg_clr_base)
263 chip->set_rv = gpio_regmap_set_with_clear;
264 else if (gpio->reg_set_base)
265 chip->set_rv = gpio_regmap_set;
266
267 chip->get_direction = gpio_regmap_get_direction;
268 if (gpio->reg_dir_in_base || gpio->reg_dir_out_base) {
269 chip->direction_input = gpio_regmap_direction_input;
270 chip->direction_output = gpio_regmap_direction_output;
271 }
272
273 chip->ngpio = config->ngpio;
274 if (!chip->ngpio) {
275 ret = gpiochip_get_ngpios(chip, chip->parent);
276 if (ret)
277 return ERR_PTR(ret);
278 }
279
280 /* if not set, assume there is only one register */
281 gpio->ngpio_per_reg = config->ngpio_per_reg;
282 if (!gpio->ngpio_per_reg)
283 gpio->ngpio_per_reg = config->ngpio;
284
285 /* if not set, assume they are consecutive */
286 gpio->reg_stride = config->reg_stride;
287 if (!gpio->reg_stride)
288 gpio->reg_stride = 1;
289
290 gpio->reg_mask_xlate = config->reg_mask_xlate;
291 if (!gpio->reg_mask_xlate)
292 gpio->reg_mask_xlate = gpio_regmap_simple_xlate;
293
294 ret = gpiochip_add_data(chip, gpio);
295 if (ret < 0)
296 goto err_free_gpio;
297
298 if (config->irq_domain) {
299 ret = gpiochip_irqchip_add_domain(chip, config->irq_domain);
300 if (ret)
301 goto err_remove_gpiochip;
302 }
303
304 return gpio;
305
306err_remove_gpiochip:
307 gpiochip_remove(chip);
308err_free_gpio:
309 kfree(gpio);
310 return ERR_PTR(ret);
311}
312EXPORT_SYMBOL_GPL(gpio_regmap_register);
313
314/**
315 * gpio_regmap_unregister() - Unregister a generic regmap GPIO controller
316 * @gpio: gpio_regmap device to unregister
317 */
318void gpio_regmap_unregister(struct gpio_regmap *gpio)
319{
320 gpiochip_remove(&gpio->gpio_chip);
321 kfree(gpio);
322}
323EXPORT_SYMBOL_GPL(gpio_regmap_unregister);
324
325static void devm_gpio_regmap_unregister(void *res)
326{
327 gpio_regmap_unregister(res);
328}
329
330/**
331 * devm_gpio_regmap_register() - resource managed gpio_regmap_register()
332 * @dev: device that is registering this GPIO device
333 * @config: configuration for gpio_regmap
334 *
335 * Managed gpio_regmap_register(). For generic regmap GPIO device registered by
336 * this function, gpio_regmap_unregister() is automatically called on driver
337 * detach. See gpio_regmap_register() for more information.
338 *
339 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value.
340 */
341struct gpio_regmap *devm_gpio_regmap_register(struct device *dev,
342 const struct gpio_regmap_config *config)
343{
344 struct gpio_regmap *gpio;
345 int ret;
346
347 gpio = gpio_regmap_register(config);
348
349 if (IS_ERR(gpio))
350 return gpio;
351
352 ret = devm_add_action_or_reset(dev, devm_gpio_regmap_unregister, gpio);
353 if (ret)
354 return ERR_PTR(ret);
355
356 return gpio;
357}
358EXPORT_SYMBOL_GPL(devm_gpio_regmap_register);
359
360MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
361MODULE_DESCRIPTION("GPIO generic regmap driver core");
362MODULE_LICENSE("GPL");