xref: /linux/drivers/gpio/gpio-regmap.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
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/compiler_attributes.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 
18 #include <linux/gpio/driver.h>
19 #include <linux/gpio/regmap.h>
20 
21 #include "gpiolib.h"
22 
23 struct gpio_regmap {
24 	struct device *parent;
25 	struct regmap *regmap;
26 	struct gpio_chip gpio_chip;
27 
28 	int reg_stride;
29 	int ngpio_per_reg;
30 	unsigned int reg_dat_base;
31 	unsigned int reg_set_base;
32 	unsigned int reg_clr_base;
33 	unsigned int reg_dir_in_base;
34 	unsigned int reg_dir_out_base;
35 	unsigned long *fixed_direction_mask;
36 	unsigned long *fixed_direction_output;
37 
38 #ifdef CONFIG_REGMAP_IRQ
39 	int regmap_irq_line;
40 	struct regmap_irq_chip_data *irq_chip_data;
41 #endif
42 
43 	int (*reg_mask_xlate)(struct gpio_regmap *gpio,
44 			      enum gpio_regmap_operation op,
45 			      unsigned int base, unsigned int offset,
46 			      unsigned int *reg, unsigned int *mask);
47 
48 	int (*value_xlate)(struct gpio_regmap *gpio,
49 			   enum gpio_regmap_operation op,
50 			   unsigned int base, unsigned int offset,
51 			   unsigned int reg, unsigned int *mask,
52 			   unsigned int *val);
53 
54 	int (*set_config)(struct gpio_regmap *gpio, struct gpio_chip *chip,
55 			  unsigned int offset, unsigned long config);
56 
57 	void *driver_data;
58 };
59 
60 static unsigned int gpio_regmap_addr(unsigned int addr)
61 {
62 	if (addr == GPIO_REGMAP_ADDR_ZERO)
63 		return 0;
64 
65 	return addr;
66 }
67 
68 static int gpio_regmap_simple_xlate(struct gpio_regmap *gpio,
69 				    enum gpio_regmap_operation __maybe_unused op,
70 				    unsigned int base, unsigned int offset,
71 				    unsigned int *reg, unsigned int *mask)
72 {
73 	unsigned int line = offset % gpio->ngpio_per_reg;
74 	unsigned int stride = offset / gpio->ngpio_per_reg;
75 
76 	*reg = base + stride * gpio->reg_stride;
77 	*mask = BIT(line);
78 
79 	return 0;
80 }
81 
82 static int gpio_regmap_get(struct gpio_chip *chip, unsigned int offset)
83 {
84 	struct gpio_regmap *gpio = gpiochip_get_data(chip);
85 	unsigned int base, val, reg, mask;
86 	int ret;
87 
88 	/* we might not have an output register if we are input only */
89 	if (gpio->reg_dat_base)
90 		base = gpio_regmap_addr(gpio->reg_dat_base);
91 	else
92 		base = gpio_regmap_addr(gpio->reg_set_base);
93 
94 	ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_GET_OP, base, offset, &reg, &mask);
95 	if (ret)
96 		return ret;
97 
98 	/* ensure we don't spoil any register cache with pin input values */
99 	if (gpio->reg_dat_base == gpio->reg_set_base)
100 		ret = regmap_read_bypassed(gpio->regmap, reg, &val);
101 	else
102 		ret = regmap_read(gpio->regmap, reg, &val);
103 	if (ret)
104 		return ret;
105 
106 	return !!(val & mask);
107 }
108 
109 static int gpio_regmap_set(struct gpio_chip *chip, unsigned int offset,
110 			   int val)
111 {
112 	struct gpio_regmap *gpio = gpiochip_get_data(chip);
113 	unsigned int base = gpio_regmap_addr(gpio->reg_set_base);
114 	unsigned int reg, mask, mask_val;
115 	int ret;
116 
117 	ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset, &reg, &mask);
118 	if (ret)
119 		return ret;
120 
121 	if (val)
122 		mask_val = mask;
123 	else
124 		mask_val = 0;
125 
126 	if (gpio->value_xlate) {
127 		ret = gpio->value_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset,
128 					reg, &mask, &mask_val);
129 		if (ret)
130 			return ret;
131 	}
132 
133 	/* ignore input values which shadow the old output value */
134 	if (gpio->reg_dat_base == gpio->reg_set_base)
135 		ret = regmap_write_bits(gpio->regmap, reg, mask, mask_val);
136 	else
137 		ret = regmap_update_bits(gpio->regmap, reg, mask, mask_val);
138 
139 	return ret;
140 }
141 
142 static int gpio_regmap_set_with_clear(struct gpio_chip *chip,
143 				      unsigned int offset, int val)
144 {
145 	struct gpio_regmap *gpio = gpiochip_get_data(chip);
146 	unsigned int base, reg, mask, value = 0;
147 	int ret;
148 
149 	if (val)
150 		base = gpio_regmap_addr(gpio->reg_set_base);
151 	else
152 		base = gpio_regmap_addr(gpio->reg_clr_base);
153 
154 	ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset, &reg, &mask);
155 	if (ret)
156 		return ret;
157 
158 	if (gpio->value_xlate) {
159 		ret = gpio->value_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset,
160 					reg, &mask, &value);
161 		if (ret)
162 			return ret;
163 	}
164 
165 	return regmap_write(gpio->regmap, reg, mask);
166 }
167 
168 static bool gpio_regmap_fixed_direction(struct gpio_regmap *gpio,
169 					unsigned int offset)
170 {
171 	if (!gpio->fixed_direction_output)
172 		return false;
173 
174 	/* In this case only some GPIOs are fixed as input/output */
175 	if (gpio->fixed_direction_mask &&
176 	    !test_bit(offset, gpio->fixed_direction_mask))
177 		return false;
178 
179 	return true;
180 }
181 
182 static int gpio_regmap_get_direction(struct gpio_chip *chip,
183 				     unsigned int offset)
184 {
185 	struct gpio_regmap *gpio = gpiochip_get_data(chip);
186 	unsigned int base, val, reg, mask;
187 	int invert, ret;
188 
189 	if (gpio_regmap_fixed_direction(gpio, offset)) {
190 		if (test_bit(offset, gpio->fixed_direction_output))
191 			return GPIO_LINE_DIRECTION_OUT;
192 		else
193 			return GPIO_LINE_DIRECTION_IN;
194 	}
195 
196 	if (gpio->reg_dat_base && !gpio->reg_set_base)
197 		return GPIO_LINE_DIRECTION_IN;
198 	if (gpio->reg_set_base && !gpio->reg_dat_base)
199 		return GPIO_LINE_DIRECTION_OUT;
200 
201 	if (gpio->reg_dir_out_base) {
202 		base = gpio_regmap_addr(gpio->reg_dir_out_base);
203 		invert = 0;
204 	} else if (gpio->reg_dir_in_base) {
205 		base = gpio_regmap_addr(gpio->reg_dir_in_base);
206 		invert = 1;
207 	} else {
208 		return -ENOTSUPP;
209 	}
210 
211 	ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_GET_DIR_OP, base, offset, &reg, &mask);
212 	if (ret)
213 		return ret;
214 
215 	ret = regmap_read(gpio->regmap, reg, &val);
216 	if (ret)
217 		return ret;
218 
219 	if (!!(val & mask) ^ invert)
220 		return GPIO_LINE_DIRECTION_OUT;
221 	else
222 		return GPIO_LINE_DIRECTION_IN;
223 }
224 
225 static int gpio_regmap_try_direction_fixed(struct gpio_regmap *gpio,
226 					   unsigned int offset, bool output)
227 {
228 	if (test_bit(offset, gpio->fixed_direction_output)) {
229 		if (output)
230 			return 0;
231 		else
232 			return -EINVAL;
233 	} else {
234 		if (output)
235 			return -EINVAL;
236 		else
237 			return 0;
238 	}
239 }
240 
241 static int gpio_regmap_set_direction(struct gpio_chip *chip,
242 				     unsigned int offset, bool output)
243 {
244 	struct gpio_regmap *gpio = gpiochip_get_data(chip);
245 	unsigned int base, val, reg, mask;
246 	int invert, ret;
247 
248 	/*
249 	 * If the direction is fixed, only accept the fixed
250 	 * direction in this call.
251 	 */
252 	if (gpio_regmap_fixed_direction(gpio, offset))
253 		return gpio_regmap_try_direction_fixed(gpio, offset, output);
254 
255 	if (gpio->reg_dir_out_base) {
256 		base = gpio_regmap_addr(gpio->reg_dir_out_base);
257 		invert = 0;
258 	} else if (gpio->reg_dir_in_base) {
259 		base = gpio_regmap_addr(gpio->reg_dir_in_base);
260 		invert = 1;
261 	} else {
262 		return -ENOTSUPP;
263 	}
264 
265 	ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_SET_DIR_OP, base, offset, &reg, &mask);
266 	if (ret)
267 		return ret;
268 
269 	if (invert)
270 		val = output ? 0 : mask;
271 	else
272 		val = output ? mask : 0;
273 
274 	if (gpio->value_xlate) {
275 		ret = gpio->value_xlate(gpio, GPIO_REGMAP_SET_DIR_OP, base, offset,
276 					reg, &mask, &val);
277 		if (ret)
278 			return ret;
279 	}
280 
281 	return regmap_update_bits(gpio->regmap, reg, mask, val);
282 }
283 
284 static int gpio_regmap_direction_input(struct gpio_chip *chip,
285 				       unsigned int offset)
286 {
287 	return gpio_regmap_set_direction(chip, offset, false);
288 }
289 
290 static int gpio_regmap_direction_output(struct gpio_chip *chip,
291 					unsigned int offset, int value)
292 {
293 	struct gpio_regmap *gpio = gpiochip_get_data(chip);
294 	int ret;
295 
296 	/*
297 	 * First check if this is gonna work on a fixed direction line,
298 	 * if it doesn't (i.e. this is a fixed input line), then do not
299 	 * attempt to set the output value either and just bail out.
300 	 */
301 	if (gpio_regmap_fixed_direction(gpio, offset)) {
302 		ret = gpio_regmap_try_direction_fixed(gpio, offset, true);
303 		if (ret)
304 			return ret;
305 	}
306 
307 	gpio_regmap_set(chip, offset, value);
308 
309 	return gpio_regmap_set_direction(chip, offset, true);
310 }
311 
312 static int gpio_regmap_set_config(struct gpio_chip *chip,
313 				  unsigned int offset,
314 				  unsigned long cfg)
315 {
316 	struct gpio_regmap *gpio = gpiochip_get_data(chip);
317 
318 	return gpio->set_config(gpio, chip, offset, cfg);
319 }
320 
321 int gpio_regmap_reqres_irq(struct gpio_regmap *gpio, unsigned int offset)
322 {
323 	return gpiochip_reqres_irq(&gpio->gpio_chip, offset);
324 }
325 EXPORT_SYMBOL_GPL(gpio_regmap_reqres_irq);
326 
327 void gpio_regmap_relres_irq(struct gpio_regmap *gpio, unsigned int offset)
328 {
329 	gpiochip_relres_irq(&gpio->gpio_chip, offset);
330 }
331 EXPORT_SYMBOL_GPL(gpio_regmap_relres_irq);
332 
333 static int __maybe_unused gpio_regmap_irq_reqres(void *irq_drv_data, irq_hw_number_t hwirq)
334 {
335 	return gpio_regmap_reqres_irq(irq_drv_data, hwirq);
336 }
337 
338 static void __maybe_unused gpio_regmap_irq_relres(void *irq_drv_data, irq_hw_number_t hwirq)
339 {
340 	gpio_regmap_relres_irq(irq_drv_data, hwirq);
341 }
342 
343 void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio)
344 {
345 	return gpio->driver_data;
346 }
347 EXPORT_SYMBOL_GPL(gpio_regmap_get_drvdata);
348 
349 void gpio_regmap_enable_irq(struct gpio_regmap *gpio, irq_hw_number_t hwirq)
350 {
351 	gpiochip_enable_irq(&gpio->gpio_chip, hwirq);
352 }
353 EXPORT_SYMBOL_GPL(gpio_regmap_enable_irq);
354 
355 void gpio_regmap_disable_irq(struct gpio_regmap *gpio, irq_hw_number_t hwirq)
356 {
357 	gpiochip_disable_irq(&gpio->gpio_chip, hwirq);
358 }
359 EXPORT_SYMBOL_GPL(gpio_regmap_disable_irq);
360 
361 /**
362  * gpio_regmap_register() - Register a generic regmap GPIO controller
363  * @config: configuration for gpio_regmap
364  *
365  * Return: A pointer to the registered gpio_regmap or ERR_PTR error value.
366  */
367 struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config)
368 {
369 	struct irq_domain *irq_domain;
370 	struct gpio_regmap *gpio;
371 	struct gpio_chip *chip;
372 	int ret;
373 
374 	if (!config->parent)
375 		return ERR_PTR(-EINVAL);
376 
377 	/* we need at least one */
378 	if (!config->reg_dat_base && !config->reg_set_base)
379 		return ERR_PTR(-EINVAL);
380 
381 	/* if we have a direction register we need both input and output */
382 	if ((config->reg_dir_out_base || config->reg_dir_in_base) &&
383 	    (!config->reg_dat_base || !config->reg_set_base))
384 		return ERR_PTR(-EINVAL);
385 
386 	/* we don't support having both registers simultaneously for now */
387 	if (config->reg_dir_out_base && config->reg_dir_in_base)
388 		return ERR_PTR(-EINVAL);
389 
390 	gpio = kzalloc_obj(*gpio);
391 	if (!gpio)
392 		return ERR_PTR(-ENOMEM);
393 
394 	gpio->parent = config->parent;
395 	gpio->driver_data = config->drvdata;
396 	gpio->regmap = config->regmap;
397 	gpio->reg_dat_base = config->reg_dat_base;
398 	gpio->reg_set_base = config->reg_set_base;
399 	gpio->reg_clr_base = config->reg_clr_base;
400 	gpio->reg_dir_in_base = config->reg_dir_in_base;
401 	gpio->reg_dir_out_base = config->reg_dir_out_base;
402 
403 	chip = &gpio->gpio_chip;
404 	chip->parent = config->parent;
405 	chip->fwnode = config->fwnode;
406 	chip->base = -1;
407 	chip->names = config->names;
408 	chip->label = config->label ?: dev_name(config->parent);
409 	chip->can_sleep = regmap_might_sleep(config->regmap);
410 	chip->init_valid_mask = config->init_valid_mask;
411 
412 	chip->request = gpiochip_generic_request;
413 	chip->free = gpiochip_generic_free;
414 	chip->get = gpio_regmap_get;
415 	if (gpio->reg_set_base && gpio->reg_clr_base)
416 		chip->set = gpio_regmap_set_with_clear;
417 	else if (gpio->reg_set_base)
418 		chip->set = gpio_regmap_set;
419 
420 	chip->get_direction = gpio_regmap_get_direction;
421 	if (gpio->reg_dir_in_base || gpio->reg_dir_out_base) {
422 		chip->direction_input = gpio_regmap_direction_input;
423 		chip->direction_output = gpio_regmap_direction_output;
424 	}
425 
426 	chip->ngpio = config->ngpio;
427 	if (!chip->ngpio) {
428 		ret = gpiochip_get_ngpios(chip, chip->parent);
429 		if (ret)
430 			goto err_free_gpio;
431 	}
432 
433 	if (config->fixed_direction_mask) {
434 		gpio->fixed_direction_mask = bitmap_alloc(chip->ngpio,
435 							    GFP_KERNEL);
436 		if (!gpio->fixed_direction_mask) {
437 			ret = -ENOMEM;
438 			goto err_free_gpio;
439 		}
440 		bitmap_copy(gpio->fixed_direction_mask,
441 			    config->fixed_direction_mask, chip->ngpio);
442 	}
443 
444 	if (config->fixed_direction_output) {
445 		gpio->fixed_direction_output = bitmap_alloc(chip->ngpio,
446 							    GFP_KERNEL);
447 		if (!gpio->fixed_direction_output) {
448 			ret = -ENOMEM;
449 			goto err_free_bitmap_dirmask;
450 		}
451 		bitmap_copy(gpio->fixed_direction_output,
452 			    config->fixed_direction_output, chip->ngpio);
453 	}
454 
455 	/* if not set, assume there is only one register */
456 	gpio->ngpio_per_reg = config->ngpio_per_reg;
457 	if (!gpio->ngpio_per_reg)
458 		gpio->ngpio_per_reg = config->ngpio;
459 
460 	/* if not set, assume they are consecutive */
461 	gpio->reg_stride = config->reg_stride;
462 	if (!gpio->reg_stride)
463 		gpio->reg_stride = 1;
464 
465 	gpio->reg_mask_xlate = config->reg_mask_xlate;
466 	if (!gpio->reg_mask_xlate)
467 		gpio->reg_mask_xlate = gpio_regmap_simple_xlate;
468 
469 	gpio->value_xlate = config->value_xlate;
470 
471 	if (config->set_config) {
472 		gpio->set_config = config->set_config;
473 		chip->set_config = gpio_regmap_set_config;
474 	}
475 
476 	ret = gpiochip_add_data(chip, gpio);
477 	if (ret < 0)
478 		goto err_free_bitmap_output;
479 
480 #ifdef CONFIG_REGMAP_IRQ
481 	if (config->regmap_irq_chip) {
482 		gpio->regmap_irq_line = config->regmap_irq_line;
483 		config->regmap_irq_chip->irq_reqres = gpio_regmap_irq_reqres;
484 		config->regmap_irq_chip->irq_relres = gpio_regmap_irq_relres;
485 		config->regmap_irq_chip->irq_drv_data = gpio;
486 		ret = regmap_add_irq_chip_fwnode(dev_fwnode(config->parent), config->regmap,
487 						 config->regmap_irq_line, config->regmap_irq_flags,
488 						 0, config->regmap_irq_chip, &gpio->irq_chip_data);
489 		if (ret)
490 			goto err_remove_gpiochip;
491 
492 		irq_domain = regmap_irq_get_domain(gpio->irq_chip_data);
493 	} else
494 #endif
495 	irq_domain = config->irq_domain;
496 
497 	if (irq_domain) {
498 		ret = gpiochip_irqchip_add_domain(chip, irq_domain);
499 		if (ret)
500 			goto err_remove_gpiochip;
501 	}
502 
503 	return gpio;
504 
505 err_remove_gpiochip:
506 	gpiochip_remove(chip);
507 err_free_bitmap_output:
508 	bitmap_free(gpio->fixed_direction_output);
509 err_free_bitmap_dirmask:
510 	bitmap_free(gpio->fixed_direction_mask);
511 err_free_gpio:
512 	kfree(gpio);
513 	return ERR_PTR(ret);
514 }
515 EXPORT_SYMBOL_GPL(gpio_regmap_register);
516 
517 /**
518  * gpio_regmap_unregister() - Unregister a generic regmap GPIO controller
519  * @gpio: gpio_regmap device to unregister
520  */
521 void gpio_regmap_unregister(struct gpio_regmap *gpio)
522 {
523 #ifdef CONFIG_REGMAP_IRQ
524 	if (gpio->irq_chip_data)
525 		regmap_del_irq_chip(gpio->regmap_irq_line, gpio->irq_chip_data);
526 #endif
527 
528 	gpiochip_remove(&gpio->gpio_chip);
529 	bitmap_free(gpio->fixed_direction_output);
530 	bitmap_free(gpio->fixed_direction_mask);
531 	kfree(gpio);
532 }
533 EXPORT_SYMBOL_GPL(gpio_regmap_unregister);
534 
535 static void devm_gpio_regmap_unregister(void *res)
536 {
537 	gpio_regmap_unregister(res);
538 }
539 
540 /**
541  * devm_gpio_regmap_register() - resource managed gpio_regmap_register()
542  * @dev: device that is registering this GPIO device
543  * @config: configuration for gpio_regmap
544  *
545  * Managed gpio_regmap_register(). For generic regmap GPIO device registered by
546  * this function, gpio_regmap_unregister() is automatically called on driver
547  * detach. See gpio_regmap_register() for more information.
548  *
549  * Return: A pointer to the registered gpio_regmap or ERR_PTR error value.
550  */
551 struct gpio_regmap *devm_gpio_regmap_register(struct device *dev,
552 					      const struct gpio_regmap_config *config)
553 {
554 	struct gpio_regmap *gpio;
555 	int ret;
556 
557 	gpio = gpio_regmap_register(config);
558 
559 	if (IS_ERR(gpio))
560 		return gpio;
561 
562 	ret = devm_add_action_or_reset(dev, devm_gpio_regmap_unregister, gpio);
563 	if (ret)
564 		return ERR_PTR(ret);
565 
566 	return gpio;
567 }
568 EXPORT_SYMBOL_GPL(devm_gpio_regmap_register);
569 
570 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
571 MODULE_DESCRIPTION("GPIO generic regmap driver core");
572 MODULE_LICENSE("GPL");
573