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