Lines Matching +full:gpio +full:-

1 // SPDX-License-Identifier: GPL-2.0-only
3 * regmap based generic GPIO driver
17 #include <linux/gpio/driver.h>
18 #include <linux/gpio/regmap.h>
33 int (*reg_mask_xlate)(struct gpio_regmap *gpio, unsigned int base,
48 static int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, in gpio_regmap_simple_xlate() argument
52 unsigned int line = offset % gpio->ngpio_per_reg; in gpio_regmap_simple_xlate()
53 unsigned int stride = offset / gpio->ngpio_per_reg; in gpio_regmap_simple_xlate()
55 *reg = base + stride * gpio->reg_stride; in gpio_regmap_simple_xlate()
63 struct gpio_regmap *gpio = gpiochip_get_data(chip); in gpio_regmap_get() local
68 if (gpio->reg_dat_base) in gpio_regmap_get()
69 base = gpio_regmap_addr(gpio->reg_dat_base); in gpio_regmap_get()
71 base = gpio_regmap_addr(gpio->reg_set_base); in gpio_regmap_get()
73 ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask); in gpio_regmap_get()
77 ret = regmap_read(gpio->regmap, reg, &val); in gpio_regmap_get()
87 struct gpio_regmap *gpio = gpiochip_get_data(chip); in gpio_regmap_set() local
88 unsigned int base = gpio_regmap_addr(gpio->reg_set_base); in gpio_regmap_set()
91 gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask); in gpio_regmap_set()
93 regmap_update_bits(gpio->regmap, reg, mask, mask); in gpio_regmap_set()
95 regmap_update_bits(gpio->regmap, reg, mask, 0); in gpio_regmap_set()
101 struct gpio_regmap *gpio = gpiochip_get_data(chip); in gpio_regmap_set_with_clear() local
105 base = gpio_regmap_addr(gpio->reg_set_base); in gpio_regmap_set_with_clear()
107 base = gpio_regmap_addr(gpio->reg_clr_base); in gpio_regmap_set_with_clear()
109 gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask); in gpio_regmap_set_with_clear()
110 regmap_write(gpio->regmap, reg, mask); in gpio_regmap_set_with_clear()
116 struct gpio_regmap *gpio = gpiochip_get_data(chip); in gpio_regmap_get_direction() local
120 if (gpio->reg_dat_base && !gpio->reg_set_base) in gpio_regmap_get_direction()
122 if (gpio->reg_set_base && !gpio->reg_dat_base) in gpio_regmap_get_direction()
125 if (gpio->reg_dir_out_base) { in gpio_regmap_get_direction()
126 base = gpio_regmap_addr(gpio->reg_dir_out_base); in gpio_regmap_get_direction()
128 } else if (gpio->reg_dir_in_base) { in gpio_regmap_get_direction()
129 base = gpio_regmap_addr(gpio->reg_dir_in_base); in gpio_regmap_get_direction()
132 return -ENOTSUPP; in gpio_regmap_get_direction()
135 ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask); in gpio_regmap_get_direction()
139 ret = regmap_read(gpio->regmap, reg, &val); in gpio_regmap_get_direction()
152 struct gpio_regmap *gpio = gpiochip_get_data(chip); in gpio_regmap_set_direction() local
156 if (gpio->reg_dir_out_base) { in gpio_regmap_set_direction()
157 base = gpio_regmap_addr(gpio->reg_dir_out_base); in gpio_regmap_set_direction()
159 } else if (gpio->reg_dir_in_base) { in gpio_regmap_set_direction()
160 base = gpio_regmap_addr(gpio->reg_dir_in_base); in gpio_regmap_set_direction()
163 return -ENOTSUPP; in gpio_regmap_set_direction()
166 ret = gpio->reg_mask_xlate(gpio, base, offset, &reg, &mask); in gpio_regmap_set_direction()
175 return regmap_update_bits(gpio->regmap, reg, mask, val); in gpio_regmap_set_direction()
192 void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio) in gpio_regmap_get_drvdata() argument
194 return gpio->driver_data; in gpio_regmap_get_drvdata()
199 * gpio_regmap_register() - Register a generic regmap GPIO controller
206 struct gpio_regmap *gpio; in gpio_regmap_register() local
210 if (!config->parent) in gpio_regmap_register()
211 return ERR_PTR(-EINVAL); in gpio_regmap_register()
213 if (!config->ngpio) in gpio_regmap_register()
214 return ERR_PTR(-EINVAL); in gpio_regmap_register()
217 if (!config->reg_dat_base && !config->reg_set_base) in gpio_regmap_register()
218 return ERR_PTR(-EINVAL); in gpio_regmap_register()
221 if ((config->reg_dir_out_base || config->reg_dir_in_base) && in gpio_regmap_register()
222 (!config->reg_dat_base || !config->reg_set_base)) in gpio_regmap_register()
223 return ERR_PTR(-EINVAL); in gpio_regmap_register()
226 if (config->reg_dir_out_base && config->reg_dir_in_base) in gpio_regmap_register()
227 return ERR_PTR(-EINVAL); in gpio_regmap_register()
229 gpio = kzalloc(sizeof(*gpio), GFP_KERNEL); in gpio_regmap_register()
230 if (!gpio) in gpio_regmap_register()
231 return ERR_PTR(-ENOMEM); in gpio_regmap_register()
233 gpio->parent = config->parent; in gpio_regmap_register()
234 gpio->driver_data = config->drvdata; in gpio_regmap_register()
235 gpio->regmap = config->regmap; in gpio_regmap_register()
236 gpio->ngpio_per_reg = config->ngpio_per_reg; in gpio_regmap_register()
237 gpio->reg_stride = config->reg_stride; in gpio_regmap_register()
238 gpio->reg_mask_xlate = config->reg_mask_xlate; in gpio_regmap_register()
239 gpio->reg_dat_base = config->reg_dat_base; in gpio_regmap_register()
240 gpio->reg_set_base = config->reg_set_base; in gpio_regmap_register()
241 gpio->reg_clr_base = config->reg_clr_base; in gpio_regmap_register()
242 gpio->reg_dir_in_base = config->reg_dir_in_base; in gpio_regmap_register()
243 gpio->reg_dir_out_base = config->reg_dir_out_base; in gpio_regmap_register()
246 if (!gpio->ngpio_per_reg) in gpio_regmap_register()
247 gpio->ngpio_per_reg = config->ngpio; in gpio_regmap_register()
250 if (!gpio->reg_stride) in gpio_regmap_register()
251 gpio->reg_stride = 1; in gpio_regmap_register()
253 if (!gpio->reg_mask_xlate) in gpio_regmap_register()
254 gpio->reg_mask_xlate = gpio_regmap_simple_xlate; in gpio_regmap_register()
256 chip = &gpio->gpio_chip; in gpio_regmap_register()
257 chip->parent = config->parent; in gpio_regmap_register()
258 chip->fwnode = config->fwnode; in gpio_regmap_register()
259 chip->base = -1; in gpio_regmap_register()
260 chip->ngpio = config->ngpio; in gpio_regmap_register()
261 chip->names = config->names; in gpio_regmap_register()
262 chip->label = config->label ?: dev_name(config->parent); in gpio_regmap_register()
263 chip->can_sleep = regmap_might_sleep(config->regmap); in gpio_regmap_register()
265 chip->get = gpio_regmap_get; in gpio_regmap_register()
266 if (gpio->reg_set_base && gpio->reg_clr_base) in gpio_regmap_register()
267 chip->set = gpio_regmap_set_with_clear; in gpio_regmap_register()
268 else if (gpio->reg_set_base) in gpio_regmap_register()
269 chip->set = gpio_regmap_set; in gpio_regmap_register()
271 chip->get_direction = gpio_regmap_get_direction; in gpio_regmap_register()
272 if (gpio->reg_dir_in_base || gpio->reg_dir_out_base) { in gpio_regmap_register()
273 chip->direction_input = gpio_regmap_direction_input; in gpio_regmap_register()
274 chip->direction_output = gpio_regmap_direction_output; in gpio_regmap_register()
277 ret = gpiochip_add_data(chip, gpio); in gpio_regmap_register()
281 if (config->irq_domain) { in gpio_regmap_register()
282 ret = gpiochip_irqchip_add_domain(chip, config->irq_domain); in gpio_regmap_register()
287 return gpio; in gpio_regmap_register()
292 kfree(gpio); in gpio_regmap_register()
298 * gpio_regmap_unregister() - Unregister a generic regmap GPIO controller
299 * @gpio: gpio_regmap device to unregister
301 void gpio_regmap_unregister(struct gpio_regmap *gpio) in gpio_regmap_unregister() argument
303 gpiochip_remove(&gpio->gpio_chip); in gpio_regmap_unregister()
304 kfree(gpio); in gpio_regmap_unregister()
314 * devm_gpio_regmap_register() - resource managed gpio_regmap_register()
315 * @dev: device that is registering this GPIO device
318 * Managed gpio_regmap_register(). For generic regmap GPIO device registered by
327 struct gpio_regmap *gpio; in devm_gpio_regmap_register() local
330 gpio = gpio_regmap_register(config); in devm_gpio_regmap_register()
332 if (IS_ERR(gpio)) in devm_gpio_regmap_register()
333 return gpio; in devm_gpio_regmap_register()
335 ret = devm_add_action_or_reset(dev, devm_gpio_regmap_unregister, gpio); in devm_gpio_regmap_register()
339 return gpio; in devm_gpio_regmap_register()
344 MODULE_DESCRIPTION("GPIO generic regmap driver core");