1 // SPDX-License-Identifier: GPL-2.0-only
3 * regmap based generic GPIO driver
5 * Copyright 2020 Michael Walle <michael@walle.cc>
8 #include <linux/gpio/driver.h>
9 #include <linux/gpio/regmap.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
15 struct device
*parent
;
16 struct regmap
*regmap
;
17 struct gpio_chip gpio_chip
;
21 unsigned int reg_dat_base
;
22 unsigned int reg_set_base
;
23 unsigned int reg_clr_base
;
24 unsigned int reg_dir_in_base
;
25 unsigned int reg_dir_out_base
;
27 int (*reg_mask_xlate
)(struct gpio_regmap
*gpio
, unsigned int base
,
28 unsigned int offset
, unsigned int *reg
,
34 static unsigned int gpio_regmap_addr(unsigned int addr
)
36 if (addr
== GPIO_REGMAP_ADDR_ZERO
)
42 static int gpio_regmap_simple_xlate(struct gpio_regmap
*gpio
,
43 unsigned int base
, unsigned int offset
,
44 unsigned int *reg
, unsigned int *mask
)
46 unsigned int line
= offset
% gpio
->ngpio_per_reg
;
47 unsigned int stride
= offset
/ gpio
->ngpio_per_reg
;
49 *reg
= base
+ stride
* gpio
->reg_stride
;
55 static int gpio_regmap_get(struct gpio_chip
*chip
, unsigned int offset
)
57 struct gpio_regmap
*gpio
= gpiochip_get_data(chip
);
58 unsigned int base
, val
, reg
, mask
;
61 /* we might not have an output register if we are input only */
62 if (gpio
->reg_dat_base
)
63 base
= gpio_regmap_addr(gpio
->reg_dat_base
);
65 base
= gpio_regmap_addr(gpio
->reg_set_base
);
67 ret
= gpio
->reg_mask_xlate(gpio
, base
, offset
, ®
, &mask
);
71 ret
= regmap_read(gpio
->regmap
, reg
, &val
);
75 return !!(val
& mask
);
78 static void gpio_regmap_set(struct gpio_chip
*chip
, unsigned int offset
,
81 struct gpio_regmap
*gpio
= gpiochip_get_data(chip
);
82 unsigned int base
= gpio_regmap_addr(gpio
->reg_set_base
);
83 unsigned int reg
, mask
;
85 gpio
->reg_mask_xlate(gpio
, base
, offset
, ®
, &mask
);
87 regmap_update_bits(gpio
->regmap
, reg
, mask
, mask
);
89 regmap_update_bits(gpio
->regmap
, reg
, mask
, 0);
92 static void gpio_regmap_set_with_clear(struct gpio_chip
*chip
,
93 unsigned int offset
, int val
)
95 struct gpio_regmap
*gpio
= gpiochip_get_data(chip
);
96 unsigned int base
, reg
, mask
;
99 base
= gpio_regmap_addr(gpio
->reg_set_base
);
101 base
= gpio_regmap_addr(gpio
->reg_clr_base
);
103 gpio
->reg_mask_xlate(gpio
, base
, offset
, ®
, &mask
);
104 regmap_write(gpio
->regmap
, reg
, mask
);
107 static int gpio_regmap_get_direction(struct gpio_chip
*chip
,
110 struct gpio_regmap
*gpio
= gpiochip_get_data(chip
);
111 unsigned int base
, val
, reg
, mask
;
114 if (gpio
->reg_dir_out_base
) {
115 base
= gpio_regmap_addr(gpio
->reg_dir_out_base
);
117 } else if (gpio
->reg_dir_in_base
) {
118 base
= gpio_regmap_addr(gpio
->reg_dir_in_base
);
124 ret
= gpio
->reg_mask_xlate(gpio
, base
, offset
, ®
, &mask
);
128 ret
= regmap_read(gpio
->regmap
, reg
, &val
);
132 if (!!(val
& mask
) ^ invert
)
133 return GPIO_LINE_DIRECTION_OUT
;
135 return GPIO_LINE_DIRECTION_IN
;
138 static int gpio_regmap_set_direction(struct gpio_chip
*chip
,
139 unsigned int offset
, bool output
)
141 struct gpio_regmap
*gpio
= gpiochip_get_data(chip
);
142 unsigned int base
, val
, reg
, mask
;
145 if (gpio
->reg_dir_out_base
) {
146 base
= gpio_regmap_addr(gpio
->reg_dir_out_base
);
148 } else if (gpio
->reg_dir_in_base
) {
149 base
= gpio_regmap_addr(gpio
->reg_dir_in_base
);
155 ret
= gpio
->reg_mask_xlate(gpio
, base
, offset
, ®
, &mask
);
160 val
= output
? 0 : mask
;
162 val
= output
? mask
: 0;
164 return regmap_update_bits(gpio
->regmap
, reg
, mask
, val
);
167 static int gpio_regmap_direction_input(struct gpio_chip
*chip
,
170 return gpio_regmap_set_direction(chip
, offset
, false);
173 static int gpio_regmap_direction_output(struct gpio_chip
*chip
,
174 unsigned int offset
, int value
)
176 gpio_regmap_set(chip
, offset
, value
);
178 return gpio_regmap_set_direction(chip
, offset
, true);
181 void gpio_regmap_set_drvdata(struct gpio_regmap
*gpio
, void *data
)
183 gpio
->driver_data
= data
;
185 EXPORT_SYMBOL_GPL(gpio_regmap_set_drvdata
);
187 void *gpio_regmap_get_drvdata(struct gpio_regmap
*gpio
)
189 return gpio
->driver_data
;
191 EXPORT_SYMBOL_GPL(gpio_regmap_get_drvdata
);
194 * gpio_regmap_register() - Register a generic regmap GPIO controller
195 * @config: configuration for gpio_regmap
197 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value.
199 struct gpio_regmap
*gpio_regmap_register(const struct gpio_regmap_config
*config
)
201 struct gpio_regmap
*gpio
;
202 struct gpio_chip
*chip
;
206 return ERR_PTR(-EINVAL
);
209 return ERR_PTR(-EINVAL
);
211 /* we need at least one */
212 if (!config
->reg_dat_base
&& !config
->reg_set_base
)
213 return ERR_PTR(-EINVAL
);
215 /* if we have a direction register we need both input and output */
216 if ((config
->reg_dir_out_base
|| config
->reg_dir_in_base
) &&
217 (!config
->reg_dat_base
|| !config
->reg_set_base
))
218 return ERR_PTR(-EINVAL
);
220 /* we don't support having both registers simultaneously for now */
221 if (config
->reg_dir_out_base
&& config
->reg_dir_in_base
)
222 return ERR_PTR(-EINVAL
);
224 gpio
= kzalloc(sizeof(*gpio
), GFP_KERNEL
);
226 return ERR_PTR(-ENOMEM
);
228 gpio
->parent
= config
->parent
;
229 gpio
->regmap
= config
->regmap
;
230 gpio
->ngpio_per_reg
= config
->ngpio_per_reg
;
231 gpio
->reg_stride
= config
->reg_stride
;
232 gpio
->reg_mask_xlate
= config
->reg_mask_xlate
;
233 gpio
->reg_dat_base
= config
->reg_dat_base
;
234 gpio
->reg_set_base
= config
->reg_set_base
;
235 gpio
->reg_clr_base
= config
->reg_clr_base
;
236 gpio
->reg_dir_in_base
= config
->reg_dir_in_base
;
237 gpio
->reg_dir_out_base
= config
->reg_dir_out_base
;
239 /* if not set, assume there is only one register */
240 if (!gpio
->ngpio_per_reg
)
241 gpio
->ngpio_per_reg
= config
->ngpio
;
243 /* if not set, assume they are consecutive */
244 if (!gpio
->reg_stride
)
245 gpio
->reg_stride
= 1;
247 if (!gpio
->reg_mask_xlate
)
248 gpio
->reg_mask_xlate
= gpio_regmap_simple_xlate
;
250 chip
= &gpio
->gpio_chip
;
251 chip
->parent
= config
->parent
;
253 chip
->ngpio
= config
->ngpio
;
254 chip
->names
= config
->names
;
255 chip
->label
= config
->label
?: dev_name(config
->parent
);
258 * If our regmap is fast_io we should probably set can_sleep to false.
259 * Right now, the regmap doesn't save this property, nor is there any
260 * access function for it.
261 * The only regmap type which uses fast_io is regmap-mmio. For now,
262 * assume a safe default of true here.
264 chip
->can_sleep
= true;
266 chip
->get
= gpio_regmap_get
;
267 if (gpio
->reg_set_base
&& gpio
->reg_clr_base
)
268 chip
->set
= gpio_regmap_set_with_clear
;
269 else if (gpio
->reg_set_base
)
270 chip
->set
= gpio_regmap_set
;
272 if (gpio
->reg_dir_in_base
|| gpio
->reg_dir_out_base
) {
273 chip
->get_direction
= gpio_regmap_get_direction
;
274 chip
->direction_input
= gpio_regmap_direction_input
;
275 chip
->direction_output
= gpio_regmap_direction_output
;
278 ret
= gpiochip_add_data(chip
, gpio
);
282 if (config
->irq_domain
) {
283 ret
= gpiochip_irqchip_add_domain(chip
, config
->irq_domain
);
285 goto err_remove_gpiochip
;
291 gpiochip_remove(chip
);
296 EXPORT_SYMBOL_GPL(gpio_regmap_register
);
299 * gpio_regmap_unregister() - Unregister a generic regmap GPIO controller
300 * @gpio: gpio_regmap device to unregister
302 void gpio_regmap_unregister(struct gpio_regmap
*gpio
)
304 gpiochip_remove(&gpio
->gpio_chip
);
307 EXPORT_SYMBOL_GPL(gpio_regmap_unregister
);
309 static void devm_gpio_regmap_unregister(struct device
*dev
, void *res
)
311 gpio_regmap_unregister(*(struct gpio_regmap
**)res
);
315 * devm_gpio_regmap_register() - resource managed gpio_regmap_register()
316 * @dev: device that is registering this GPIO device
317 * @config: configuration for gpio_regmap
319 * Managed gpio_regmap_register(). For generic regmap GPIO device registered by
320 * this function, gpio_regmap_unregister() is automatically called on driver
321 * detach. See gpio_regmap_register() for more information.
323 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value.
325 struct gpio_regmap
*devm_gpio_regmap_register(struct device
*dev
,
326 const struct gpio_regmap_config
*config
)
328 struct gpio_regmap
**ptr
, *gpio
;
330 ptr
= devres_alloc(devm_gpio_regmap_unregister
, sizeof(*ptr
),
333 return ERR_PTR(-ENOMEM
);
335 gpio
= gpio_regmap_register(config
);
338 devres_add(dev
, ptr
);
345 EXPORT_SYMBOL_GPL(devm_gpio_regmap_register
);
347 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
348 MODULE_DESCRIPTION("GPIO generic regmap driver core");
349 MODULE_LICENSE("GPL");