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