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