Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/interrupt.h> |
| 4 | #include <linux/irq.h> |
| 5 | #include <linux/spinlock.h> |
| 6 | #include <linux/list.h> |
| 7 | #include <linux/device.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | #include <linux/gpio.h> |
| 12 | #include <linux/of_gpio.h> |
| 13 | #include <linux/idr.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/acpi.h> |
| 16 | #include <linux/gpio/driver.h> |
| 17 | #include <linux/gpio/machine.h> |
| 18 | #include <linux/pinctrl/consumer.h> |
| 19 | |
| 20 | #include "gpiolib.h" |
| 21 | |
| 22 | #define CREATE_TRACE_POINTS |
| 23 | #include <trace/events/gpio.h> |
| 24 | |
| 25 | /* Implementation infrastructure for GPIO interfaces. |
| 26 | * |
| 27 | * The GPIO programming interface allows for inlining speed-critical |
| 28 | * get/set operations for common cases, so that access to SOC-integrated |
| 29 | * GPIOs can sometimes cost only an instruction or two per bit. |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | /* When debugging, extend minimal trust to callers and platform code. |
| 34 | * Also emit diagnostic messages that may help initial bringup, when |
| 35 | * board setup or driver bugs are most common. |
| 36 | * |
| 37 | * Otherwise, minimize overhead in what may be bitbanging codepaths. |
| 38 | */ |
| 39 | #ifdef DEBUG |
| 40 | #define extra_checks 1 |
| 41 | #else |
| 42 | #define extra_checks 0 |
| 43 | #endif |
| 44 | |
| 45 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. |
| 46 | * While any GPIO is requested, its gpio_chip is not removable; |
| 47 | * each GPIO's "requested" flag serves as a lock and refcount. |
| 48 | */ |
| 49 | DEFINE_SPINLOCK(gpio_lock); |
| 50 | |
| 51 | static DEFINE_MUTEX(gpio_lookup_lock); |
| 52 | static LIST_HEAD(gpio_lookup_list); |
| 53 | LIST_HEAD(gpio_chips); |
| 54 | |
| 55 | |
| 56 | static void gpiochip_free_hogs(struct gpio_chip *chip); |
| 57 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
| 58 | |
| 59 | |
| 60 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
| 61 | { |
| 62 | d->label = label; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Convert a GPIO number to its descriptor |
| 67 | */ |
| 68 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
| 69 | { |
| 70 | struct gpio_chip *chip; |
| 71 | unsigned long flags; |
| 72 | |
| 73 | spin_lock_irqsave(&gpio_lock, flags); |
| 74 | |
| 75 | list_for_each_entry(chip, &gpio_chips, list) { |
| 76 | if (chip->base <= gpio && chip->base + chip->ngpio > gpio) { |
| 77 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 78 | return &chip->desc[gpio - chip->base]; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 83 | |
| 84 | if (!gpio_is_valid(gpio)) |
| 85 | WARN(1, "invalid GPIO %d\n", gpio); |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
| 90 | |
| 91 | /** |
| 92 | * Get the GPIO descriptor corresponding to the given hw number for this chip. |
| 93 | */ |
| 94 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
| 95 | u16 hwnum) |
| 96 | { |
| 97 | if (hwnum >= chip->ngpio) |
| 98 | return ERR_PTR(-EINVAL); |
| 99 | |
| 100 | return &chip->desc[hwnum]; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Convert a GPIO descriptor to the integer namespace. |
| 105 | * This should disappear in the future but is needed since we still |
| 106 | * use GPIO numbers for error messages and sysfs nodes |
| 107 | */ |
| 108 | int desc_to_gpio(const struct gpio_desc *desc) |
| 109 | { |
| 110 | return desc->chip->base + (desc - &desc->chip->desc[0]); |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
| 117 | * @desc: descriptor to return the chip of |
| 118 | */ |
| 119 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
| 120 | { |
| 121 | return desc ? desc->chip : NULL; |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
| 124 | |
| 125 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
| 126 | static int gpiochip_find_base(int ngpio) |
| 127 | { |
| 128 | struct gpio_chip *chip; |
| 129 | int base = ARCH_NR_GPIOS - ngpio; |
| 130 | |
| 131 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
| 132 | /* found a free space? */ |
| 133 | if (chip->base + chip->ngpio <= base) |
| 134 | break; |
| 135 | else |
| 136 | /* nope, check the space right before the chip */ |
| 137 | base = chip->base - ngpio; |
| 138 | } |
| 139 | |
| 140 | if (gpio_is_valid(base)) { |
| 141 | pr_debug("%s: found new base at %d\n", __func__, base); |
| 142 | return base; |
| 143 | } else { |
| 144 | pr_err("%s: cannot find free range\n", __func__); |
| 145 | return -ENOSPC; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * gpiod_get_direction - return the current direction of a GPIO |
| 151 | * @desc: GPIO to get the direction of |
| 152 | * |
| 153 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. |
| 154 | * |
| 155 | * This function may sleep if gpiod_cansleep() is true. |
| 156 | */ |
| 157 | int gpiod_get_direction(struct gpio_desc *desc) |
| 158 | { |
| 159 | struct gpio_chip *chip; |
| 160 | unsigned offset; |
| 161 | int status = -EINVAL; |
| 162 | |
| 163 | chip = gpiod_to_chip(desc); |
| 164 | offset = gpio_chip_hwgpio(desc); |
| 165 | |
| 166 | if (!chip->get_direction) |
| 167 | return status; |
| 168 | |
| 169 | status = chip->get_direction(chip, offset); |
| 170 | if (status > 0) { |
| 171 | /* GPIOF_DIR_IN, or other positive */ |
| 172 | status = 1; |
| 173 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 174 | } |
| 175 | if (status == 0) { |
| 176 | /* GPIOF_DIR_OUT */ |
| 177 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 178 | } |
| 179 | return status; |
| 180 | } |
| 181 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
| 182 | |
| 183 | /* |
| 184 | * Add a new chip to the global chips list, keeping the list of chips sorted |
| 185 | * by base order. |
| 186 | * |
| 187 | * Return -EBUSY if the new chip overlaps with some other chip's integer |
| 188 | * space. |
| 189 | */ |
| 190 | static int gpiochip_add_to_list(struct gpio_chip *chip) |
| 191 | { |
| 192 | struct list_head *pos; |
| 193 | struct gpio_chip *_chip; |
| 194 | int err = 0; |
| 195 | |
| 196 | /* find where to insert our chip */ |
| 197 | list_for_each(pos, &gpio_chips) { |
| 198 | _chip = list_entry(pos, struct gpio_chip, list); |
| 199 | /* shall we insert before _chip? */ |
| 200 | if (_chip->base >= chip->base + chip->ngpio) |
| 201 | break; |
| 202 | } |
| 203 | |
| 204 | /* are we stepping on the chip right before? */ |
| 205 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { |
| 206 | _chip = list_entry(pos->prev, struct gpio_chip, list); |
| 207 | if (_chip->base + _chip->ngpio > chip->base) { |
| 208 | dev_err(chip->dev, |
| 209 | "GPIO integer space overlap, cannot add chip\n"); |
| 210 | err = -EBUSY; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (!err) |
| 215 | list_add_tail(&chip->list, pos); |
| 216 | |
| 217 | return err; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Convert a GPIO name to its descriptor |
| 222 | */ |
| 223 | static struct gpio_desc *gpio_name_to_desc(const char * const name) |
| 224 | { |
| 225 | struct gpio_chip *chip; |
| 226 | unsigned long flags; |
| 227 | |
| 228 | spin_lock_irqsave(&gpio_lock, flags); |
| 229 | |
| 230 | list_for_each_entry(chip, &gpio_chips, list) { |
| 231 | int i; |
| 232 | |
| 233 | for (i = 0; i != chip->ngpio; ++i) { |
| 234 | struct gpio_desc *gpio = &chip->desc[i]; |
| 235 | |
| 236 | if (!gpio->name || !name) |
| 237 | continue; |
| 238 | |
| 239 | if (!strcmp(gpio->name, name)) { |
| 240 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 241 | return gpio; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 247 | |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Takes the names from gc->names and checks if they are all unique. If they |
| 253 | * are, they are assigned to their gpio descriptors. |
| 254 | * |
| 255 | * Returns -EEXIST if one of the names is already used for a different GPIO. |
| 256 | */ |
| 257 | static int gpiochip_set_desc_names(struct gpio_chip *gc) |
| 258 | { |
| 259 | int i; |
| 260 | |
| 261 | if (!gc->names) |
| 262 | return 0; |
| 263 | |
| 264 | /* First check all names if they are unique */ |
| 265 | for (i = 0; i != gc->ngpio; ++i) { |
| 266 | struct gpio_desc *gpio; |
| 267 | |
| 268 | gpio = gpio_name_to_desc(gc->names[i]); |
| 269 | if (gpio) |
| 270 | dev_warn(gc->dev, "Detected name collision for " |
| 271 | "GPIO name '%s'\n", |
| 272 | gc->names[i]); |
| 273 | } |
| 274 | |
| 275 | /* Then add all names to the GPIO descriptors */ |
| 276 | for (i = 0; i != gc->ngpio; ++i) |
| 277 | gc->desc[i].name = gc->names[i]; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * gpiochip_add() - register a gpio_chip |
| 284 | * @chip: the chip to register, with chip->base initialized |
| 285 | * Context: potentially before irqs will work |
| 286 | * |
| 287 | * Returns a negative errno if the chip can't be registered, such as |
| 288 | * because the chip->base is invalid or already associated with a |
| 289 | * different chip. Otherwise it returns zero as a success code. |
| 290 | * |
| 291 | * When gpiochip_add() is called very early during boot, so that GPIOs |
| 292 | * can be freely used, the chip->dev device must be registered before |
| 293 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization |
| 294 | * for GPIOs will fail rudely. |
| 295 | * |
| 296 | * If chip->base is negative, this requests dynamic assignment of |
| 297 | * a range of valid GPIOs. |
| 298 | */ |
| 299 | int gpiochip_add(struct gpio_chip *chip) |
| 300 | { |
| 301 | unsigned long flags; |
| 302 | int status = 0; |
| 303 | unsigned id; |
| 304 | int base = chip->base; |
| 305 | struct gpio_desc *descs; |
| 306 | |
| 307 | descs = kcalloc(chip->ngpio, sizeof(descs[0]), GFP_KERNEL); |
| 308 | if (!descs) |
| 309 | return -ENOMEM; |
| 310 | |
| 311 | spin_lock_irqsave(&gpio_lock, flags); |
| 312 | |
| 313 | if (base < 0) { |
| 314 | base = gpiochip_find_base(chip->ngpio); |
| 315 | if (base < 0) { |
| 316 | status = base; |
| 317 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 318 | goto err_free_descs; |
| 319 | } |
| 320 | chip->base = base; |
| 321 | } |
| 322 | |
| 323 | status = gpiochip_add_to_list(chip); |
| 324 | if (status) { |
| 325 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 326 | goto err_free_descs; |
| 327 | } |
| 328 | |
| 329 | for (id = 0; id < chip->ngpio; id++) { |
| 330 | struct gpio_desc *desc = &descs[id]; |
| 331 | |
| 332 | desc->chip = chip; |
| 333 | |
| 334 | /* REVISIT: most hardware initializes GPIOs as inputs (often |
| 335 | * with pullups enabled) so power usage is minimized. Linux |
| 336 | * code should set the gpio direction first thing; but until |
| 337 | * it does, and in case chip->get_direction is not set, we may |
| 338 | * expose the wrong direction in sysfs. |
| 339 | */ |
| 340 | desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0; |
| 341 | } |
| 342 | |
| 343 | chip->desc = descs; |
| 344 | |
| 345 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 346 | |
| 347 | #ifdef CONFIG_PINCTRL |
| 348 | INIT_LIST_HEAD(&chip->pin_ranges); |
| 349 | #endif |
| 350 | |
| 351 | if (!chip->owner && chip->dev && chip->dev->driver) |
| 352 | chip->owner = chip->dev->driver->owner; |
| 353 | |
| 354 | status = gpiochip_set_desc_names(chip); |
| 355 | if (status) |
| 356 | goto err_remove_from_list; |
| 357 | |
| 358 | status = of_gpiochip_add(chip); |
| 359 | if (status) |
| 360 | goto err_remove_chip; |
| 361 | |
| 362 | acpi_gpiochip_add(chip); |
| 363 | |
| 364 | status = gpiochip_sysfs_register(chip); |
| 365 | if (status) |
| 366 | goto err_remove_chip; |
| 367 | |
| 368 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, |
| 369 | chip->base, chip->base + chip->ngpio - 1, |
| 370 | chip->label ? : "generic"); |
| 371 | |
| 372 | return 0; |
| 373 | |
| 374 | err_remove_chip: |
| 375 | acpi_gpiochip_remove(chip); |
| 376 | gpiochip_free_hogs(chip); |
| 377 | of_gpiochip_remove(chip); |
| 378 | err_remove_from_list: |
| 379 | spin_lock_irqsave(&gpio_lock, flags); |
| 380 | list_del(&chip->list); |
| 381 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 382 | chip->desc = NULL; |
| 383 | err_free_descs: |
| 384 | kfree(descs); |
| 385 | |
| 386 | /* failures here can mean systems won't boot... */ |
| 387 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
| 388 | chip->base, chip->base + chip->ngpio - 1, |
| 389 | chip->label ? : "generic"); |
| 390 | return status; |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(gpiochip_add); |
| 393 | |
| 394 | /** |
| 395 | * gpiochip_remove() - unregister a gpio_chip |
| 396 | * @chip: the chip to unregister |
| 397 | * |
| 398 | * A gpio_chip with any GPIOs still requested may not be removed. |
| 399 | */ |
| 400 | void gpiochip_remove(struct gpio_chip *chip) |
| 401 | { |
| 402 | struct gpio_desc *desc; |
| 403 | unsigned long flags; |
| 404 | unsigned id; |
| 405 | bool requested = false; |
| 406 | |
| 407 | gpiochip_sysfs_unregister(chip); |
| 408 | |
| 409 | gpiochip_irqchip_remove(chip); |
| 410 | |
| 411 | acpi_gpiochip_remove(chip); |
| 412 | gpiochip_remove_pin_ranges(chip); |
| 413 | gpiochip_free_hogs(chip); |
| 414 | of_gpiochip_remove(chip); |
| 415 | |
| 416 | spin_lock_irqsave(&gpio_lock, flags); |
| 417 | for (id = 0; id < chip->ngpio; id++) { |
| 418 | desc = &chip->desc[id]; |
| 419 | desc->chip = NULL; |
| 420 | if (test_bit(FLAG_REQUESTED, &desc->flags)) |
| 421 | requested = true; |
| 422 | } |
| 423 | list_del(&chip->list); |
| 424 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 425 | |
| 426 | if (requested) |
| 427 | dev_crit(chip->dev, "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
| 428 | |
| 429 | kfree(chip->desc); |
| 430 | chip->desc = NULL; |
| 431 | } |
| 432 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
| 433 | |
| 434 | /** |
| 435 | * gpiochip_find() - iterator for locating a specific gpio_chip |
| 436 | * @data: data to pass to match function |
| 437 | * @callback: Callback function to check gpio_chip |
| 438 | * |
| 439 | * Similar to bus_find_device. It returns a reference to a gpio_chip as |
| 440 | * determined by a user supplied @match callback. The callback should return |
| 441 | * 0 if the device doesn't match and non-zero if it does. If the callback is |
| 442 | * non-zero, this function will return to the caller and not iterate over any |
| 443 | * more gpio_chips. |
| 444 | */ |
| 445 | struct gpio_chip *gpiochip_find(void *data, |
| 446 | int (*match)(struct gpio_chip *chip, |
| 447 | void *data)) |
| 448 | { |
| 449 | struct gpio_chip *chip; |
| 450 | unsigned long flags; |
| 451 | |
| 452 | spin_lock_irqsave(&gpio_lock, flags); |
| 453 | list_for_each_entry(chip, &gpio_chips, list) |
| 454 | if (match(chip, data)) |
| 455 | break; |
| 456 | |
| 457 | /* No match? */ |
| 458 | if (&chip->list == &gpio_chips) |
| 459 | chip = NULL; |
| 460 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 461 | |
| 462 | return chip; |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(gpiochip_find); |
| 465 | |
| 466 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
| 467 | { |
| 468 | const char *name = data; |
| 469 | |
| 470 | return !strcmp(chip->label, name); |
| 471 | } |
| 472 | |
| 473 | static struct gpio_chip *find_chip_by_name(const char *name) |
| 474 | { |
| 475 | return gpiochip_find((void *)name, gpiochip_match_name); |
| 476 | } |
| 477 | |
| 478 | #ifdef CONFIG_GPIOLIB_IRQCHIP |
| 479 | |
| 480 | /* |
| 481 | * The following is irqchip helper code for gpiochips. |
| 482 | */ |
| 483 | |
| 484 | /** |
| 485 | * gpiochip_set_chained_irqchip() - sets a chained irqchip to a gpiochip |
| 486 | * @gpiochip: the gpiochip to set the irqchip chain to |
| 487 | * @irqchip: the irqchip to chain to the gpiochip |
| 488 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
| 489 | * chained irqchip |
| 490 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
| 491 | * coming out of the gpiochip. If the interrupt is nested rather than |
| 492 | * cascaded, pass NULL in this handler argument |
| 493 | */ |
| 494 | void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
| 495 | struct irq_chip *irqchip, |
| 496 | int parent_irq, |
| 497 | irq_flow_handler_t parent_handler) |
| 498 | { |
| 499 | unsigned int offset; |
| 500 | |
| 501 | if (!gpiochip->irqdomain) { |
| 502 | chip_err(gpiochip, "called %s before setting up irqchip\n", |
| 503 | __func__); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | if (parent_handler) { |
| 508 | if (gpiochip->can_sleep) { |
| 509 | chip_err(gpiochip, |
| 510 | "you cannot have chained interrupts on a " |
| 511 | "chip that may sleep\n"); |
| 512 | return; |
| 513 | } |
| 514 | /* |
| 515 | * The parent irqchip is already using the chip_data for this |
| 516 | * irqchip, so our callbacks simply use the handler_data. |
| 517 | */ |
| 518 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
| 519 | gpiochip); |
| 520 | |
| 521 | gpiochip->irq_parent = parent_irq; |
| 522 | } |
| 523 | |
| 524 | /* Set the parent IRQ for all affected IRQs */ |
| 525 | for (offset = 0; offset < gpiochip->ngpio; offset++) |
| 526 | irq_set_parent(irq_find_mapping(gpiochip->irqdomain, offset), |
| 527 | parent_irq); |
| 528 | } |
| 529 | EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); |
| 530 | |
| 531 | /** |
| 532 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip |
| 533 | * @d: the irqdomain used by this irqchip |
| 534 | * @irq: the global irq number used by this GPIO irqchip irq |
| 535 | * @hwirq: the local IRQ/GPIO line offset on this gpiochip |
| 536 | * |
| 537 | * This function will set up the mapping for a certain IRQ line on a |
| 538 | * gpiochip by assigning the gpiochip as chip data, and using the irqchip |
| 539 | * stored inside the gpiochip. |
| 540 | */ |
| 541 | static int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
| 542 | irq_hw_number_t hwirq) |
| 543 | { |
| 544 | struct gpio_chip *chip = d->host_data; |
| 545 | |
| 546 | irq_set_chip_data(irq, chip); |
| 547 | /* |
| 548 | * This lock class tells lockdep that GPIO irqs are in a different |
| 549 | * category than their parents, so it won't report false recursion. |
| 550 | */ |
| 551 | irq_set_lockdep_class(irq, chip->lock_key); |
| 552 | irq_set_chip_and_handler(irq, chip->irqchip, chip->irq_handler); |
| 553 | /* Chips that can sleep need nested thread handlers */ |
| 554 | if (chip->can_sleep && !chip->irq_not_threaded) |
| 555 | irq_set_nested_thread(irq, 1); |
| 556 | irq_set_noprobe(irq); |
| 557 | |
| 558 | /* |
| 559 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
| 560 | * is passed as default type. |
| 561 | */ |
| 562 | if (chip->irq_default_type != IRQ_TYPE_NONE) |
| 563 | irq_set_irq_type(irq, chip->irq_default_type); |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
| 569 | { |
| 570 | struct gpio_chip *chip = d->host_data; |
| 571 | |
| 572 | if (chip->can_sleep) |
| 573 | irq_set_nested_thread(irq, 0); |
| 574 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 575 | irq_set_chip_data(irq, NULL); |
| 576 | } |
| 577 | |
| 578 | static const struct irq_domain_ops gpiochip_domain_ops = { |
| 579 | .map = gpiochip_irq_map, |
| 580 | .unmap = gpiochip_irq_unmap, |
| 581 | /* Virtually all GPIO irqchips are twocell:ed */ |
| 582 | .xlate = irq_domain_xlate_twocell, |
| 583 | }; |
| 584 | |
| 585 | static int gpiochip_irq_reqres(struct irq_data *d) |
| 586 | { |
| 587 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 588 | |
| 589 | if (!try_module_get(chip->owner)) |
| 590 | return -ENODEV; |
| 591 | |
| 592 | if (gpiochip_lock_as_irq(chip, d->hwirq)) { |
| 593 | chip_err(chip, |
| 594 | "unable to lock HW IRQ %lu for IRQ\n", |
| 595 | d->hwirq); |
| 596 | module_put(chip->owner); |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static void gpiochip_irq_relres(struct irq_data *d) |
| 603 | { |
| 604 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
| 605 | |
| 606 | gpiochip_unlock_as_irq(chip, d->hwirq); |
| 607 | module_put(chip->owner); |
| 608 | } |
| 609 | |
| 610 | static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) |
| 611 | { |
| 612 | return irq_find_mapping(chip->irqdomain, offset); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip |
| 617 | * @gpiochip: the gpiochip to remove the irqchip from |
| 618 | * |
| 619 | * This is called only from gpiochip_remove() |
| 620 | */ |
| 621 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) |
| 622 | { |
| 623 | unsigned int offset; |
| 624 | |
| 625 | acpi_gpiochip_free_interrupts(gpiochip); |
| 626 | |
| 627 | if (gpiochip->irq_parent) { |
| 628 | irq_set_chained_handler(gpiochip->irq_parent, NULL); |
| 629 | irq_set_handler_data(gpiochip->irq_parent, NULL); |
| 630 | } |
| 631 | |
| 632 | /* Remove all IRQ mappings and delete the domain */ |
| 633 | if (gpiochip->irqdomain) { |
| 634 | for (offset = 0; offset < gpiochip->ngpio; offset++) |
| 635 | irq_dispose_mapping( |
| 636 | irq_find_mapping(gpiochip->irqdomain, offset)); |
| 637 | irq_domain_remove(gpiochip->irqdomain); |
| 638 | } |
| 639 | |
| 640 | if (gpiochip->irqchip) { |
| 641 | gpiochip->irqchip->irq_request_resources = NULL; |
| 642 | gpiochip->irqchip->irq_release_resources = NULL; |
| 643 | gpiochip->irqchip = NULL; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * gpiochip_irqchip_add() - adds an irqchip to a gpiochip |
| 649 | * @gpiochip: the gpiochip to add the irqchip to |
| 650 | * @irqchip: the irqchip to add to the gpiochip |
| 651 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
| 652 | * allocate gpiochip irqs from |
| 653 | * @handler: the irq handler to use (often a predefined irq core function) |
| 654 | * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE |
| 655 | * to have the core avoid setting up any default type in the hardware. |
| 656 | * @lock_key: lockdep class |
| 657 | * |
| 658 | * This function closely associates a certain irqchip with a certain |
| 659 | * gpiochip, providing an irq domain to translate the local IRQs to |
| 660 | * global irqs in the gpiolib core, and making sure that the gpiochip |
| 661 | * is passed as chip data to all related functions. Driver callbacks |
| 662 | * need to use container_of() to get their local state containers back |
| 663 | * from the gpiochip passed as chip data. An irqdomain will be stored |
| 664 | * in the gpiochip that shall be used by the driver to handle IRQ number |
| 665 | * translation. The gpiochip will need to be initialized and registered |
| 666 | * before calling this function. |
| 667 | * |
| 668 | * This function will handle two cell:ed simple IRQs and assumes all |
| 669 | * the pins on the gpiochip can generate a unique IRQ. Everything else |
| 670 | * need to be open coded. |
| 671 | */ |
| 672 | int _gpiochip_irqchip_add(struct gpio_chip *gpiochip, |
| 673 | struct irq_chip *irqchip, |
| 674 | unsigned int first_irq, |
| 675 | irq_flow_handler_t handler, |
| 676 | unsigned int type, |
| 677 | struct lock_class_key *lock_key) |
| 678 | { |
| 679 | struct device_node *of_node; |
| 680 | unsigned int offset; |
| 681 | unsigned irq_base = 0; |
| 682 | |
| 683 | if (!gpiochip || !irqchip) |
| 684 | return -EINVAL; |
| 685 | |
| 686 | if (!gpiochip->dev) { |
| 687 | pr_err("missing gpiochip .dev parent pointer\n"); |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | of_node = gpiochip->dev->of_node; |
| 691 | #ifdef CONFIG_OF_GPIO |
| 692 | /* |
| 693 | * If the gpiochip has an assigned OF node this takes precedence |
| 694 | * FIXME: get rid of this and use gpiochip->dev->of_node everywhere |
| 695 | */ |
| 696 | if (gpiochip->of_node) |
| 697 | of_node = gpiochip->of_node; |
| 698 | #endif |
| 699 | gpiochip->irqchip = irqchip; |
| 700 | gpiochip->irq_handler = handler; |
| 701 | gpiochip->irq_default_type = type; |
| 702 | gpiochip->to_irq = gpiochip_to_irq; |
| 703 | gpiochip->lock_key = lock_key; |
| 704 | gpiochip->irqdomain = irq_domain_add_simple(of_node, |
| 705 | gpiochip->ngpio, first_irq, |
| 706 | &gpiochip_domain_ops, gpiochip); |
| 707 | if (!gpiochip->irqdomain) { |
| 708 | gpiochip->irqchip = NULL; |
| 709 | return -EINVAL; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * It is possible for a driver to override this, but only if the |
| 714 | * alternative functions are both implemented. |
| 715 | */ |
| 716 | if (!irqchip->irq_request_resources && |
| 717 | !irqchip->irq_release_resources) { |
| 718 | irqchip->irq_request_resources = gpiochip_irq_reqres; |
| 719 | irqchip->irq_release_resources = gpiochip_irq_relres; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Prepare the mapping since the irqchip shall be orthogonal to |
| 724 | * any gpiochip calls. If the first_irq was zero, this is |
| 725 | * necessary to allocate descriptors for all IRQs. |
| 726 | */ |
| 727 | for (offset = 0; offset < gpiochip->ngpio; offset++) { |
| 728 | irq_base = irq_create_mapping(gpiochip->irqdomain, offset); |
| 729 | if (offset == 0) |
| 730 | /* |
| 731 | * Store the base into the gpiochip to be used when |
| 732 | * unmapping the irqs. |
| 733 | */ |
| 734 | gpiochip->irq_base = irq_base; |
| 735 | } |
| 736 | |
| 737 | acpi_gpiochip_request_interrupts(gpiochip); |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | EXPORT_SYMBOL_GPL(_gpiochip_irqchip_add); |
| 742 | |
| 743 | #else /* CONFIG_GPIOLIB_IRQCHIP */ |
| 744 | |
| 745 | static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
| 746 | |
| 747 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
| 748 | |
| 749 | /** |
| 750 | * gpiochip_generic_request() - request the gpio function for a pin |
| 751 | * @chip: the gpiochip owning the GPIO |
| 752 | * @offset: the offset of the GPIO to request for GPIO function |
| 753 | */ |
| 754 | int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset) |
| 755 | { |
| 756 | return pinctrl_request_gpio(chip->base + offset); |
| 757 | } |
| 758 | EXPORT_SYMBOL_GPL(gpiochip_generic_request); |
| 759 | |
| 760 | /** |
| 761 | * gpiochip_generic_free() - free the gpio function from a pin |
| 762 | * @chip: the gpiochip to request the gpio function for |
| 763 | * @offset: the offset of the GPIO to free from GPIO function |
| 764 | */ |
| 765 | void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset) |
| 766 | { |
| 767 | pinctrl_free_gpio(chip->base + offset); |
| 768 | } |
| 769 | EXPORT_SYMBOL_GPL(gpiochip_generic_free); |
| 770 | |
| 771 | #ifdef CONFIG_PINCTRL |
| 772 | |
| 773 | /** |
| 774 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
| 775 | * @chip: the gpiochip to add the range for |
| 776 | * @pctldev: the pin controller to map to |
| 777 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 778 | * @pin_group: name of the pin group inside the pin controller |
| 779 | */ |
| 780 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
| 781 | struct pinctrl_dev *pctldev, |
| 782 | unsigned int gpio_offset, const char *pin_group) |
| 783 | { |
| 784 | struct gpio_pin_range *pin_range; |
| 785 | int ret; |
| 786 | |
| 787 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 788 | if (!pin_range) { |
| 789 | chip_err(chip, "failed to allocate pin ranges\n"); |
| 790 | return -ENOMEM; |
| 791 | } |
| 792 | |
| 793 | /* Use local offset as range ID */ |
| 794 | pin_range->range.id = gpio_offset; |
| 795 | pin_range->range.gc = chip; |
| 796 | pin_range->range.name = chip->label; |
| 797 | pin_range->range.base = chip->base + gpio_offset; |
| 798 | pin_range->pctldev = pctldev; |
| 799 | |
| 800 | ret = pinctrl_get_group_pins(pctldev, pin_group, |
| 801 | &pin_range->range.pins, |
| 802 | &pin_range->range.npins); |
| 803 | if (ret < 0) { |
| 804 | kfree(pin_range); |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
| 809 | |
| 810 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
| 811 | gpio_offset, gpio_offset + pin_range->range.npins - 1, |
| 812 | pinctrl_dev_get_devname(pctldev), pin_group); |
| 813 | |
| 814 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); |
| 819 | |
| 820 | /** |
| 821 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
| 822 | * @chip: the gpiochip to add the range for |
| 823 | * @pinctrl_name: the dev_name() of the pin controller to map to |
| 824 | * @gpio_offset: the start offset in the current gpio_chip number space |
| 825 | * @pin_offset: the start offset in the pin controller number space |
| 826 | * @npins: the number of pins from the offset of each pin space (GPIO and |
| 827 | * pin controller) to accumulate in this range |
| 828 | */ |
| 829 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
| 830 | unsigned int gpio_offset, unsigned int pin_offset, |
| 831 | unsigned int npins) |
| 832 | { |
| 833 | struct gpio_pin_range *pin_range; |
| 834 | int ret; |
| 835 | |
| 836 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
| 837 | if (!pin_range) { |
| 838 | chip_err(chip, "failed to allocate pin ranges\n"); |
| 839 | return -ENOMEM; |
| 840 | } |
| 841 | |
| 842 | /* Use local offset as range ID */ |
| 843 | pin_range->range.id = gpio_offset; |
| 844 | pin_range->range.gc = chip; |
| 845 | pin_range->range.name = chip->label; |
| 846 | pin_range->range.base = chip->base + gpio_offset; |
| 847 | pin_range->range.pin_base = pin_offset; |
| 848 | pin_range->range.npins = npins; |
| 849 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
| 850 | &pin_range->range); |
| 851 | if (IS_ERR(pin_range->pctldev)) { |
| 852 | ret = PTR_ERR(pin_range->pctldev); |
| 853 | chip_err(chip, "could not create pin range\n"); |
| 854 | kfree(pin_range); |
| 855 | return ret; |
| 856 | } |
| 857 | chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
| 858 | gpio_offset, gpio_offset + npins - 1, |
| 859 | pinctl_name, |
| 860 | pin_offset, pin_offset + npins - 1); |
| 861 | |
| 862 | list_add_tail(&pin_range->node, &chip->pin_ranges); |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
| 867 | |
| 868 | /** |
| 869 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
| 870 | * @chip: the chip to remove all the mappings for |
| 871 | */ |
| 872 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
| 873 | { |
| 874 | struct gpio_pin_range *pin_range, *tmp; |
| 875 | |
| 876 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { |
| 877 | list_del(&pin_range->node); |
| 878 | pinctrl_remove_gpio_range(pin_range->pctldev, |
| 879 | &pin_range->range); |
| 880 | kfree(pin_range); |
| 881 | } |
| 882 | } |
| 883 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
| 884 | |
| 885 | #endif /* CONFIG_PINCTRL */ |
| 886 | |
| 887 | /* These "optional" allocation calls help prevent drivers from stomping |
| 888 | * on each other, and help provide better diagnostics in debugfs. |
| 889 | * They're called even less than the "set direction" calls. |
| 890 | */ |
| 891 | static int __gpiod_request(struct gpio_desc *desc, const char *label) |
| 892 | { |
| 893 | struct gpio_chip *chip = desc->chip; |
| 894 | int status; |
| 895 | unsigned long flags; |
| 896 | |
| 897 | spin_lock_irqsave(&gpio_lock, flags); |
| 898 | |
| 899 | /* NOTE: gpio_request() can be called in early boot, |
| 900 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
| 901 | */ |
| 902 | |
| 903 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
| 904 | desc_set_label(desc, label ? : "?"); |
| 905 | status = 0; |
| 906 | } else { |
| 907 | status = -EBUSY; |
| 908 | goto done; |
| 909 | } |
| 910 | |
| 911 | if (chip->request) { |
| 912 | /* chip->request may sleep */ |
| 913 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 914 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
| 915 | spin_lock_irqsave(&gpio_lock, flags); |
| 916 | |
| 917 | if (status < 0) { |
| 918 | desc_set_label(desc, NULL); |
| 919 | clear_bit(FLAG_REQUESTED, &desc->flags); |
| 920 | goto done; |
| 921 | } |
| 922 | } |
| 923 | if (chip->get_direction) { |
| 924 | /* chip->get_direction may sleep */ |
| 925 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 926 | gpiod_get_direction(desc); |
| 927 | spin_lock_irqsave(&gpio_lock, flags); |
| 928 | } |
| 929 | done: |
| 930 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 931 | return status; |
| 932 | } |
| 933 | |
| 934 | int gpiod_request(struct gpio_desc *desc, const char *label) |
| 935 | { |
| 936 | int status = -EPROBE_DEFER; |
| 937 | struct gpio_chip *chip; |
| 938 | |
| 939 | if (!desc) { |
| 940 | pr_warn("%s: invalid GPIO\n", __func__); |
| 941 | return -EINVAL; |
| 942 | } |
| 943 | |
| 944 | chip = desc->chip; |
| 945 | if (!chip) |
| 946 | goto done; |
| 947 | |
| 948 | if (try_module_get(chip->owner)) { |
| 949 | status = __gpiod_request(desc, label); |
| 950 | if (status < 0) |
| 951 | module_put(chip->owner); |
| 952 | } |
| 953 | |
| 954 | done: |
| 955 | if (status) |
| 956 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
| 957 | |
| 958 | return status; |
| 959 | } |
| 960 | |
| 961 | static bool __gpiod_free(struct gpio_desc *desc) |
| 962 | { |
| 963 | bool ret = false; |
| 964 | unsigned long flags; |
| 965 | struct gpio_chip *chip; |
| 966 | |
| 967 | might_sleep(); |
| 968 | |
| 969 | gpiod_unexport(desc); |
| 970 | |
| 971 | spin_lock_irqsave(&gpio_lock, flags); |
| 972 | |
| 973 | chip = desc->chip; |
| 974 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
| 975 | if (chip->free) { |
| 976 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 977 | might_sleep_if(chip->can_sleep); |
| 978 | chip->free(chip, gpio_chip_hwgpio(desc)); |
| 979 | spin_lock_irqsave(&gpio_lock, flags); |
| 980 | } |
| 981 | desc_set_label(desc, NULL); |
| 982 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 983 | clear_bit(FLAG_REQUESTED, &desc->flags); |
| 984 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 985 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 986 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
| 987 | ret = true; |
| 988 | } |
| 989 | |
| 990 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | void gpiod_free(struct gpio_desc *desc) |
| 995 | { |
| 996 | if (desc && __gpiod_free(desc)) |
| 997 | module_put(desc->chip->owner); |
| 998 | else |
| 999 | WARN_ON(extra_checks); |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * gpiochip_is_requested - return string iff signal was requested |
| 1004 | * @chip: controller managing the signal |
| 1005 | * @offset: of signal within controller's 0..(ngpio - 1) range |
| 1006 | * |
| 1007 | * Returns NULL if the GPIO is not currently requested, else a string. |
| 1008 | * The string returned is the label passed to gpio_request(); if none has been |
| 1009 | * passed it is a meaningless, non-NULL constant. |
| 1010 | * |
| 1011 | * This function is for use by GPIO controller drivers. The label can |
| 1012 | * help with diagnostics, and knowing that the signal is used as a GPIO |
| 1013 | * can help avoid accidentally multiplexing it to another controller. |
| 1014 | */ |
| 1015 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
| 1016 | { |
| 1017 | struct gpio_desc *desc; |
| 1018 | |
| 1019 | if (offset >= chip->ngpio) |
| 1020 | return NULL; |
| 1021 | |
| 1022 | desc = &chip->desc[offset]; |
| 1023 | |
| 1024 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
| 1025 | return NULL; |
| 1026 | return desc->label; |
| 1027 | } |
| 1028 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); |
| 1029 | |
| 1030 | /** |
| 1031 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor |
| 1032 | * @desc: GPIO descriptor to request |
| 1033 | * @label: label for the GPIO |
| 1034 | * |
| 1035 | * Function allows GPIO chip drivers to request and use their own GPIO |
| 1036 | * descriptors via gpiolib API. Difference to gpiod_request() is that this |
| 1037 | * function will not increase reference count of the GPIO chip module. This |
| 1038 | * allows the GPIO chip module to be unloaded as needed (we assume that the |
| 1039 | * GPIO chip driver handles freeing the GPIOs it has requested). |
| 1040 | */ |
| 1041 | struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
| 1042 | const char *label) |
| 1043 | { |
| 1044 | struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
| 1045 | int err; |
| 1046 | |
| 1047 | if (IS_ERR(desc)) { |
| 1048 | chip_err(chip, "failed to get GPIO descriptor\n"); |
| 1049 | return desc; |
| 1050 | } |
| 1051 | |
| 1052 | err = __gpiod_request(desc, label); |
| 1053 | if (err < 0) |
| 1054 | return ERR_PTR(err); |
| 1055 | |
| 1056 | return desc; |
| 1057 | } |
| 1058 | EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); |
| 1059 | |
| 1060 | /** |
| 1061 | * gpiochip_free_own_desc - Free GPIO requested by the chip driver |
| 1062 | * @desc: GPIO descriptor to free |
| 1063 | * |
| 1064 | * Function frees the given GPIO requested previously with |
| 1065 | * gpiochip_request_own_desc(). |
| 1066 | */ |
| 1067 | void gpiochip_free_own_desc(struct gpio_desc *desc) |
| 1068 | { |
| 1069 | if (desc) |
| 1070 | __gpiod_free(desc); |
| 1071 | } |
| 1072 | EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); |
| 1073 | |
| 1074 | /* Drivers MUST set GPIO direction before making get/set calls. In |
| 1075 | * some cases this is done in early boot, before IRQs are enabled. |
| 1076 | * |
| 1077 | * As a rule these aren't called more than once (except for drivers |
| 1078 | * using the open-drain emulation idiom) so these are natural places |
| 1079 | * to accumulate extra debugging checks. Note that we can't (yet) |
| 1080 | * rely on gpio_request() having been called beforehand. |
| 1081 | */ |
| 1082 | |
| 1083 | /** |
| 1084 | * gpiod_direction_input - set the GPIO direction to input |
| 1085 | * @desc: GPIO to set to input |
| 1086 | * |
| 1087 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can |
| 1088 | * be called safely on it. |
| 1089 | * |
| 1090 | * Return 0 in case of success, else an error code. |
| 1091 | */ |
| 1092 | int gpiod_direction_input(struct gpio_desc *desc) |
| 1093 | { |
| 1094 | struct gpio_chip *chip; |
| 1095 | int status = -EINVAL; |
| 1096 | |
| 1097 | if (!desc || !desc->chip) { |
| 1098 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1099 | return -EINVAL; |
| 1100 | } |
| 1101 | |
| 1102 | chip = desc->chip; |
| 1103 | if (!chip->get || !chip->direction_input) { |
| 1104 | gpiod_warn(desc, |
| 1105 | "%s: missing get() or direction_input() operations\n", |
| 1106 | __func__); |
| 1107 | return -EIO; |
| 1108 | } |
| 1109 | |
| 1110 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
| 1111 | if (status == 0) |
| 1112 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 1113 | |
| 1114 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
| 1115 | |
| 1116 | return status; |
| 1117 | } |
| 1118 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
| 1119 | |
| 1120 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 1121 | { |
| 1122 | struct gpio_chip *chip; |
| 1123 | int status = -EINVAL; |
| 1124 | |
| 1125 | /* GPIOs used for IRQs shall not be set as output */ |
| 1126 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
| 1127 | gpiod_err(desc, |
| 1128 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
| 1129 | __func__); |
| 1130 | return -EIO; |
| 1131 | } |
| 1132 | |
| 1133 | /* Open drain pin should not be driven to 1 */ |
| 1134 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 1135 | return gpiod_direction_input(desc); |
| 1136 | |
| 1137 | /* Open source pin should not be driven to 0 */ |
| 1138 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 1139 | return gpiod_direction_input(desc); |
| 1140 | |
| 1141 | chip = desc->chip; |
| 1142 | if (!chip->set || !chip->direction_output) { |
| 1143 | gpiod_warn(desc, |
| 1144 | "%s: missing set() or direction_output() operations\n", |
| 1145 | __func__); |
| 1146 | return -EIO; |
| 1147 | } |
| 1148 | |
| 1149 | status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value); |
| 1150 | if (status == 0) |
| 1151 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 1152 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1153 | trace_gpio_direction(desc_to_gpio(desc), 0, status); |
| 1154 | return status; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * gpiod_direction_output_raw - set the GPIO direction to output |
| 1159 | * @desc: GPIO to set to output |
| 1160 | * @value: initial output value of the GPIO |
| 1161 | * |
| 1162 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1163 | * be called safely on it. The initial value of the output must be specified |
| 1164 | * as raw value on the physical line without regard for the ACTIVE_LOW status. |
| 1165 | * |
| 1166 | * Return 0 in case of success, else an error code. |
| 1167 | */ |
| 1168 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 1169 | { |
| 1170 | if (!desc || !desc->chip) { |
| 1171 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1172 | return -EINVAL; |
| 1173 | } |
| 1174 | return _gpiod_direction_output_raw(desc, value); |
| 1175 | } |
| 1176 | EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); |
| 1177 | |
| 1178 | /** |
| 1179 | * gpiod_direction_output - set the GPIO direction to output |
| 1180 | * @desc: GPIO to set to output |
| 1181 | * @value: initial output value of the GPIO |
| 1182 | * |
| 1183 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can |
| 1184 | * be called safely on it. The initial value of the output must be specified |
| 1185 | * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1186 | * account. |
| 1187 | * |
| 1188 | * Return 0 in case of success, else an error code. |
| 1189 | */ |
| 1190 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 1191 | { |
| 1192 | if (!desc || !desc->chip) { |
| 1193 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1194 | return -EINVAL; |
| 1195 | } |
| 1196 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1197 | value = !value; |
| 1198 | return _gpiod_direction_output_raw(desc, value); |
| 1199 | } |
| 1200 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
| 1201 | |
| 1202 | /** |
| 1203 | * gpiod_set_debounce - sets @debounce time for a @gpio |
| 1204 | * @gpio: the gpio to set debounce time |
| 1205 | * @debounce: debounce time is microseconds |
| 1206 | * |
| 1207 | * returns -ENOTSUPP if the controller does not support setting |
| 1208 | * debounce. |
| 1209 | */ |
| 1210 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
| 1211 | { |
| 1212 | struct gpio_chip *chip; |
| 1213 | |
| 1214 | if (!desc || !desc->chip) { |
| 1215 | pr_warn("%s: invalid GPIO\n", __func__); |
| 1216 | return -EINVAL; |
| 1217 | } |
| 1218 | |
| 1219 | chip = desc->chip; |
| 1220 | if (!chip->set || !chip->set_debounce) { |
| 1221 | gpiod_dbg(desc, |
| 1222 | "%s: missing set() or set_debounce() operations\n", |
| 1223 | __func__); |
| 1224 | return -ENOTSUPP; |
| 1225 | } |
| 1226 | |
| 1227 | return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce); |
| 1228 | } |
| 1229 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
| 1230 | |
| 1231 | /** |
| 1232 | * gpiod_is_active_low - test whether a GPIO is active-low or not |
| 1233 | * @desc: the gpio descriptor to test |
| 1234 | * |
| 1235 | * Returns 1 if the GPIO is active-low, 0 otherwise. |
| 1236 | */ |
| 1237 | int gpiod_is_active_low(const struct gpio_desc *desc) |
| 1238 | { |
| 1239 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 1240 | } |
| 1241 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
| 1242 | |
| 1243 | /* I/O calls are only valid after configuration completed; the relevant |
| 1244 | * "is this a valid GPIO" error checks should already have been done. |
| 1245 | * |
| 1246 | * "Get" operations are often inlinable as reading a pin value register, |
| 1247 | * and masking the relevant bit in that register. |
| 1248 | * |
| 1249 | * When "set" operations are inlinable, they involve writing that mask to |
| 1250 | * one register to set a low value, or a different register to set it high. |
| 1251 | * Otherwise locking is needed, so there may be little value to inlining. |
| 1252 | * |
| 1253 | *------------------------------------------------------------------------ |
| 1254 | * |
| 1255 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers |
| 1256 | * have requested the GPIO. That can include implicit requesting by |
| 1257 | * a direction setting call. Marking a gpio as requested locks its chip |
| 1258 | * in memory, guaranteeing that these table lookups need no more locking |
| 1259 | * and that gpiochip_remove() will fail. |
| 1260 | * |
| 1261 | * REVISIT when debugging, consider adding some instrumentation to ensure |
| 1262 | * that the GPIO was actually requested. |
| 1263 | */ |
| 1264 | |
| 1265 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
| 1266 | { |
| 1267 | struct gpio_chip *chip; |
| 1268 | int offset; |
| 1269 | int value; |
| 1270 | |
| 1271 | chip = desc->chip; |
| 1272 | offset = gpio_chip_hwgpio(desc); |
| 1273 | value = chip->get ? chip->get(chip, offset) : -EIO; |
| 1274 | /* |
| 1275 | * FIXME: fix all drivers to clamp to [0,1] or return negative, |
| 1276 | * then change this to: |
| 1277 | * value = value < 0 ? value : !!value; |
| 1278 | * so we can properly propagate error codes. |
| 1279 | */ |
| 1280 | value = !!value; |
| 1281 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
| 1282 | return value; |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * gpiod_get_raw_value() - return a gpio's raw value |
| 1287 | * @desc: gpio whose value will be returned |
| 1288 | * |
| 1289 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 1290 | * its ACTIVE_LOW status, or negative errno on failure. |
| 1291 | * |
| 1292 | * This function should be called from contexts where we cannot sleep, and will |
| 1293 | * complain if the GPIO chip functions potentially sleep. |
| 1294 | */ |
| 1295 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
| 1296 | { |
| 1297 | if (!desc) |
| 1298 | return 0; |
| 1299 | /* Should be using gpio_get_value_cansleep() */ |
| 1300 | WARN_ON(desc->chip->can_sleep); |
| 1301 | return _gpiod_get_raw_value(desc); |
| 1302 | } |
| 1303 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
| 1304 | |
| 1305 | /** |
| 1306 | * gpiod_get_value() - return a gpio's value |
| 1307 | * @desc: gpio whose value will be returned |
| 1308 | * |
| 1309 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
| 1310 | * account, or negative errno on failure. |
| 1311 | * |
| 1312 | * This function should be called from contexts where we cannot sleep, and will |
| 1313 | * complain if the GPIO chip functions potentially sleep. |
| 1314 | */ |
| 1315 | int gpiod_get_value(const struct gpio_desc *desc) |
| 1316 | { |
| 1317 | int value; |
| 1318 | if (!desc) |
| 1319 | return 0; |
| 1320 | /* Should be using gpio_get_value_cansleep() */ |
| 1321 | WARN_ON(desc->chip->can_sleep); |
| 1322 | |
| 1323 | value = _gpiod_get_raw_value(desc); |
| 1324 | if (value < 0) |
| 1325 | return value; |
| 1326 | |
| 1327 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1328 | value = !value; |
| 1329 | |
| 1330 | return value; |
| 1331 | } |
| 1332 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
| 1333 | |
| 1334 | /* |
| 1335 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. |
| 1336 | * @desc: gpio descriptor whose state need to be set. |
| 1337 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
| 1338 | */ |
| 1339 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, bool value) |
| 1340 | { |
| 1341 | int err = 0; |
| 1342 | struct gpio_chip *chip = desc->chip; |
| 1343 | int offset = gpio_chip_hwgpio(desc); |
| 1344 | |
| 1345 | if (value) { |
| 1346 | err = chip->direction_input(chip, offset); |
| 1347 | if (!err) |
| 1348 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 1349 | } else { |
| 1350 | err = chip->direction_output(chip, offset, 0); |
| 1351 | if (!err) |
| 1352 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 1353 | } |
| 1354 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
| 1355 | if (err < 0) |
| 1356 | gpiod_err(desc, |
| 1357 | "%s: Error in set_value for open drain err %d\n", |
| 1358 | __func__, err); |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
| 1363 | * @desc: gpio descriptor whose state need to be set. |
| 1364 | * @value: Non-zero for setting it HIGH otherwise it will set to LOW. |
| 1365 | */ |
| 1366 | static void _gpio_set_open_source_value(struct gpio_desc *desc, bool value) |
| 1367 | { |
| 1368 | int err = 0; |
| 1369 | struct gpio_chip *chip = desc->chip; |
| 1370 | int offset = gpio_chip_hwgpio(desc); |
| 1371 | |
| 1372 | if (value) { |
| 1373 | err = chip->direction_output(chip, offset, 1); |
| 1374 | if (!err) |
| 1375 | set_bit(FLAG_IS_OUT, &desc->flags); |
| 1376 | } else { |
| 1377 | err = chip->direction_input(chip, offset); |
| 1378 | if (!err) |
| 1379 | clear_bit(FLAG_IS_OUT, &desc->flags); |
| 1380 | } |
| 1381 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
| 1382 | if (err < 0) |
| 1383 | gpiod_err(desc, |
| 1384 | "%s: Error in set_value for open source err %d\n", |
| 1385 | __func__, err); |
| 1386 | } |
| 1387 | |
| 1388 | static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) |
| 1389 | { |
| 1390 | struct gpio_chip *chip; |
| 1391 | |
| 1392 | chip = desc->chip; |
| 1393 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1394 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
| 1395 | _gpio_set_open_drain_value(desc, value); |
| 1396 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
| 1397 | _gpio_set_open_source_value(desc, value); |
| 1398 | else |
| 1399 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | * set multiple outputs on the same chip; |
| 1404 | * use the chip's set_multiple function if available; |
| 1405 | * otherwise set the outputs sequentially; |
| 1406 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word |
| 1407 | * defines which outputs are to be changed |
| 1408 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word |
| 1409 | * defines the values the outputs specified by mask are to be set to |
| 1410 | */ |
| 1411 | static void gpio_chip_set_multiple(struct gpio_chip *chip, |
| 1412 | unsigned long *mask, unsigned long *bits) |
| 1413 | { |
| 1414 | if (chip->set_multiple) { |
| 1415 | chip->set_multiple(chip, mask, bits); |
| 1416 | } else { |
| 1417 | int i; |
| 1418 | for (i = 0; i < chip->ngpio; i++) { |
| 1419 | if (mask[BIT_WORD(i)] == 0) { |
| 1420 | /* no more set bits in this mask word; |
| 1421 | * skip ahead to the next word */ |
| 1422 | i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1; |
| 1423 | continue; |
| 1424 | } |
| 1425 | /* set outputs if the corresponding mask bit is set */ |
| 1426 | if (__test_and_clear_bit(i, mask)) |
| 1427 | chip->set(chip, i, test_bit(i, bits)); |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | static void gpiod_set_array_value_priv(bool raw, bool can_sleep, |
| 1433 | unsigned int array_size, |
| 1434 | struct gpio_desc **desc_array, |
| 1435 | int *value_array) |
| 1436 | { |
| 1437 | int i = 0; |
| 1438 | |
| 1439 | while (i < array_size) { |
| 1440 | struct gpio_chip *chip = desc_array[i]->chip; |
| 1441 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; |
| 1442 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; |
| 1443 | int count = 0; |
| 1444 | |
| 1445 | if (!can_sleep) |
| 1446 | WARN_ON(chip->can_sleep); |
| 1447 | |
| 1448 | memset(mask, 0, sizeof(mask)); |
| 1449 | do { |
| 1450 | struct gpio_desc *desc = desc_array[i]; |
| 1451 | int hwgpio = gpio_chip_hwgpio(desc); |
| 1452 | int value = value_array[i]; |
| 1453 | |
| 1454 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1455 | value = !value; |
| 1456 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
| 1457 | /* |
| 1458 | * collect all normal outputs belonging to the same chip |
| 1459 | * open drain and open source outputs are set individually |
| 1460 | */ |
| 1461 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { |
| 1462 | _gpio_set_open_drain_value(desc, value); |
| 1463 | } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
| 1464 | _gpio_set_open_source_value(desc, value); |
| 1465 | } else { |
| 1466 | __set_bit(hwgpio, mask); |
| 1467 | if (value) |
| 1468 | __set_bit(hwgpio, bits); |
| 1469 | else |
| 1470 | __clear_bit(hwgpio, bits); |
| 1471 | count++; |
| 1472 | } |
| 1473 | i++; |
| 1474 | } while ((i < array_size) && (desc_array[i]->chip == chip)); |
| 1475 | /* push collected bits to outputs */ |
| 1476 | if (count != 0) |
| 1477 | gpio_chip_set_multiple(chip, mask, bits); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * gpiod_set_raw_value() - assign a gpio's raw value |
| 1483 | * @desc: gpio whose value will be assigned |
| 1484 | * @value: value to assign |
| 1485 | * |
| 1486 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 1487 | * regard for its ACTIVE_LOW status. |
| 1488 | * |
| 1489 | * This function should be called from contexts where we cannot sleep, and will |
| 1490 | * complain if the GPIO chip functions potentially sleep. |
| 1491 | */ |
| 1492 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
| 1493 | { |
| 1494 | if (!desc) |
| 1495 | return; |
| 1496 | /* Should be using gpio_set_value_cansleep() */ |
| 1497 | WARN_ON(desc->chip->can_sleep); |
| 1498 | _gpiod_set_raw_value(desc, value); |
| 1499 | } |
| 1500 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
| 1501 | |
| 1502 | /** |
| 1503 | * gpiod_set_value() - assign a gpio's value |
| 1504 | * @desc: gpio whose value will be assigned |
| 1505 | * @value: value to assign |
| 1506 | * |
| 1507 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1508 | * account |
| 1509 | * |
| 1510 | * This function should be called from contexts where we cannot sleep, and will |
| 1511 | * complain if the GPIO chip functions potentially sleep. |
| 1512 | */ |
| 1513 | void gpiod_set_value(struct gpio_desc *desc, int value) |
| 1514 | { |
| 1515 | if (!desc) |
| 1516 | return; |
| 1517 | /* Should be using gpio_set_value_cansleep() */ |
| 1518 | WARN_ON(desc->chip->can_sleep); |
| 1519 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1520 | value = !value; |
| 1521 | _gpiod_set_raw_value(desc, value); |
| 1522 | } |
| 1523 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
| 1524 | |
| 1525 | /** |
| 1526 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
| 1527 | * @array_size: number of elements in the descriptor / value arrays |
| 1528 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1529 | * @value_array: array of values to assign |
| 1530 | * |
| 1531 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 1532 | * without regard for their ACTIVE_LOW status. |
| 1533 | * |
| 1534 | * This function should be called from contexts where we cannot sleep, and will |
| 1535 | * complain if the GPIO chip functions potentially sleep. |
| 1536 | */ |
| 1537 | void gpiod_set_raw_array_value(unsigned int array_size, |
| 1538 | struct gpio_desc **desc_array, int *value_array) |
| 1539 | { |
| 1540 | if (!desc_array) |
| 1541 | return; |
| 1542 | gpiod_set_array_value_priv(true, false, array_size, desc_array, |
| 1543 | value_array); |
| 1544 | } |
| 1545 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
| 1546 | |
| 1547 | /** |
| 1548 | * gpiod_set_array_value() - assign values to an array of GPIOs |
| 1549 | * @array_size: number of elements in the descriptor / value arrays |
| 1550 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1551 | * @value_array: array of values to assign |
| 1552 | * |
| 1553 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 1554 | * into account. |
| 1555 | * |
| 1556 | * This function should be called from contexts where we cannot sleep, and will |
| 1557 | * complain if the GPIO chip functions potentially sleep. |
| 1558 | */ |
| 1559 | void gpiod_set_array_value(unsigned int array_size, |
| 1560 | struct gpio_desc **desc_array, int *value_array) |
| 1561 | { |
| 1562 | if (!desc_array) |
| 1563 | return; |
| 1564 | gpiod_set_array_value_priv(false, false, array_size, desc_array, |
| 1565 | value_array); |
| 1566 | } |
| 1567 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
| 1568 | |
| 1569 | /** |
| 1570 | * gpiod_cansleep() - report whether gpio value access may sleep |
| 1571 | * @desc: gpio to check |
| 1572 | * |
| 1573 | */ |
| 1574 | int gpiod_cansleep(const struct gpio_desc *desc) |
| 1575 | { |
| 1576 | if (!desc) |
| 1577 | return 0; |
| 1578 | return desc->chip->can_sleep; |
| 1579 | } |
| 1580 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
| 1581 | |
| 1582 | /** |
| 1583 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
| 1584 | * @desc: gpio whose IRQ will be returned (already requested) |
| 1585 | * |
| 1586 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
| 1587 | * error. |
| 1588 | */ |
| 1589 | int gpiod_to_irq(const struct gpio_desc *desc) |
| 1590 | { |
| 1591 | struct gpio_chip *chip; |
| 1592 | int offset; |
| 1593 | |
| 1594 | if (!desc) |
| 1595 | return -EINVAL; |
| 1596 | chip = desc->chip; |
| 1597 | offset = gpio_chip_hwgpio(desc); |
| 1598 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; |
| 1599 | } |
| 1600 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
| 1601 | |
| 1602 | /** |
| 1603 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
| 1604 | * @chip: the chip the GPIO to lock belongs to |
| 1605 | * @offset: the offset of the GPIO to lock as IRQ |
| 1606 | * |
| 1607 | * This is used directly by GPIO drivers that want to lock down |
| 1608 | * a certain GPIO line to be used for IRQs. |
| 1609 | */ |
| 1610 | int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 1611 | { |
| 1612 | if (offset >= chip->ngpio) |
| 1613 | return -EINVAL; |
| 1614 | |
| 1615 | if (test_bit(FLAG_IS_OUT, &chip->desc[offset].flags)) { |
| 1616 | chip_err(chip, |
| 1617 | "%s: tried to flag a GPIO set as output for IRQ\n", |
| 1618 | __func__); |
| 1619 | return -EIO; |
| 1620 | } |
| 1621 | |
| 1622 | set_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); |
| 1626 | |
| 1627 | /** |
| 1628 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
| 1629 | * @chip: the chip the GPIO to lock belongs to |
| 1630 | * @offset: the offset of the GPIO to lock as IRQ |
| 1631 | * |
| 1632 | * This is used directly by GPIO drivers that want to indicate |
| 1633 | * that a certain GPIO is no longer used exclusively for IRQ. |
| 1634 | */ |
| 1635 | void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
| 1636 | { |
| 1637 | if (offset >= chip->ngpio) |
| 1638 | return; |
| 1639 | |
| 1640 | clear_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags); |
| 1641 | } |
| 1642 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
| 1643 | |
| 1644 | /** |
| 1645 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value |
| 1646 | * @desc: gpio whose value will be returned |
| 1647 | * |
| 1648 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
| 1649 | * its ACTIVE_LOW status, or negative errno on failure. |
| 1650 | * |
| 1651 | * This function is to be called from contexts that can sleep. |
| 1652 | */ |
| 1653 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
| 1654 | { |
| 1655 | might_sleep_if(extra_checks); |
| 1656 | if (!desc) |
| 1657 | return 0; |
| 1658 | return _gpiod_get_raw_value(desc); |
| 1659 | } |
| 1660 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
| 1661 | |
| 1662 | /** |
| 1663 | * gpiod_get_value_cansleep() - return a gpio's value |
| 1664 | * @desc: gpio whose value will be returned |
| 1665 | * |
| 1666 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
| 1667 | * account, or negative errno on failure. |
| 1668 | * |
| 1669 | * This function is to be called from contexts that can sleep. |
| 1670 | */ |
| 1671 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
| 1672 | { |
| 1673 | int value; |
| 1674 | |
| 1675 | might_sleep_if(extra_checks); |
| 1676 | if (!desc) |
| 1677 | return 0; |
| 1678 | |
| 1679 | value = _gpiod_get_raw_value(desc); |
| 1680 | if (value < 0) |
| 1681 | return value; |
| 1682 | |
| 1683 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1684 | value = !value; |
| 1685 | |
| 1686 | return value; |
| 1687 | } |
| 1688 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
| 1689 | |
| 1690 | /** |
| 1691 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
| 1692 | * @desc: gpio whose value will be assigned |
| 1693 | * @value: value to assign |
| 1694 | * |
| 1695 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
| 1696 | * regard for its ACTIVE_LOW status. |
| 1697 | * |
| 1698 | * This function is to be called from contexts that can sleep. |
| 1699 | */ |
| 1700 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) |
| 1701 | { |
| 1702 | might_sleep_if(extra_checks); |
| 1703 | if (!desc) |
| 1704 | return; |
| 1705 | _gpiod_set_raw_value(desc, value); |
| 1706 | } |
| 1707 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
| 1708 | |
| 1709 | /** |
| 1710 | * gpiod_set_value_cansleep() - assign a gpio's value |
| 1711 | * @desc: gpio whose value will be assigned |
| 1712 | * @value: value to assign |
| 1713 | * |
| 1714 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into |
| 1715 | * account |
| 1716 | * |
| 1717 | * This function is to be called from contexts that can sleep. |
| 1718 | */ |
| 1719 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
| 1720 | { |
| 1721 | might_sleep_if(extra_checks); |
| 1722 | if (!desc) |
| 1723 | return; |
| 1724 | |
| 1725 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
| 1726 | value = !value; |
| 1727 | _gpiod_set_raw_value(desc, value); |
| 1728 | } |
| 1729 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
| 1730 | |
| 1731 | /** |
| 1732 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
| 1733 | * @array_size: number of elements in the descriptor / value arrays |
| 1734 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1735 | * @value_array: array of values to assign |
| 1736 | * |
| 1737 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
| 1738 | * without regard for their ACTIVE_LOW status. |
| 1739 | * |
| 1740 | * This function is to be called from contexts that can sleep. |
| 1741 | */ |
| 1742 | void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
| 1743 | struct gpio_desc **desc_array, |
| 1744 | int *value_array) |
| 1745 | { |
| 1746 | might_sleep_if(extra_checks); |
| 1747 | if (!desc_array) |
| 1748 | return; |
| 1749 | gpiod_set_array_value_priv(true, true, array_size, desc_array, |
| 1750 | value_array); |
| 1751 | } |
| 1752 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
| 1753 | |
| 1754 | /** |
| 1755 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
| 1756 | * @array_size: number of elements in the descriptor / value arrays |
| 1757 | * @desc_array: array of GPIO descriptors whose values will be assigned |
| 1758 | * @value_array: array of values to assign |
| 1759 | * |
| 1760 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
| 1761 | * into account. |
| 1762 | * |
| 1763 | * This function is to be called from contexts that can sleep. |
| 1764 | */ |
| 1765 | void gpiod_set_array_value_cansleep(unsigned int array_size, |
| 1766 | struct gpio_desc **desc_array, |
| 1767 | int *value_array) |
| 1768 | { |
| 1769 | might_sleep_if(extra_checks); |
| 1770 | if (!desc_array) |
| 1771 | return; |
| 1772 | gpiod_set_array_value_priv(false, true, array_size, desc_array, |
| 1773 | value_array); |
| 1774 | } |
| 1775 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
| 1776 | |
| 1777 | /** |
| 1778 | * gpiod_add_lookup_table() - register GPIO device consumers |
| 1779 | * @table: table of consumers to register |
| 1780 | */ |
| 1781 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
| 1782 | { |
| 1783 | mutex_lock(&gpio_lookup_lock); |
| 1784 | |
| 1785 | list_add_tail(&table->list, &gpio_lookup_list); |
| 1786 | |
| 1787 | mutex_unlock(&gpio_lookup_lock); |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * gpiod_remove_lookup_table() - unregister GPIO device consumers |
| 1792 | * @table: table of consumers to unregister |
| 1793 | */ |
| 1794 | void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) |
| 1795 | { |
| 1796 | mutex_lock(&gpio_lookup_lock); |
| 1797 | |
| 1798 | list_del(&table->list); |
| 1799 | |
| 1800 | mutex_unlock(&gpio_lookup_lock); |
| 1801 | } |
| 1802 | |
| 1803 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
| 1804 | unsigned int idx, |
| 1805 | enum gpio_lookup_flags *flags) |
| 1806 | { |
| 1807 | char prop_name[32]; /* 32 is max size of property name */ |
| 1808 | enum of_gpio_flags of_flags; |
| 1809 | struct gpio_desc *desc; |
| 1810 | unsigned int i; |
| 1811 | |
| 1812 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 1813 | if (con_id) |
| 1814 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, |
| 1815 | gpio_suffixes[i]); |
| 1816 | else |
| 1817 | snprintf(prop_name, sizeof(prop_name), "%s", |
| 1818 | gpio_suffixes[i]); |
| 1819 | |
| 1820 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, |
| 1821 | &of_flags); |
| 1822 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
| 1823 | break; |
| 1824 | } |
| 1825 | |
| 1826 | if (IS_ERR(desc)) |
| 1827 | return desc; |
| 1828 | |
| 1829 | if (of_flags & OF_GPIO_ACTIVE_LOW) |
| 1830 | *flags |= GPIO_ACTIVE_LOW; |
| 1831 | |
| 1832 | if (of_flags & OF_GPIO_SINGLE_ENDED) { |
| 1833 | if (of_flags & OF_GPIO_ACTIVE_LOW) |
| 1834 | *flags |= GPIO_OPEN_DRAIN; |
| 1835 | else |
| 1836 | *flags |= GPIO_OPEN_SOURCE; |
| 1837 | } |
| 1838 | |
| 1839 | return desc; |
| 1840 | } |
| 1841 | |
| 1842 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
| 1843 | unsigned int idx, |
| 1844 | enum gpio_lookup_flags *flags) |
| 1845 | { |
| 1846 | struct acpi_device *adev = ACPI_COMPANION(dev); |
| 1847 | struct acpi_gpio_info info; |
| 1848 | struct gpio_desc *desc; |
| 1849 | char propname[32]; |
| 1850 | int i; |
| 1851 | |
| 1852 | /* Try first from _DSD */ |
| 1853 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 1854 | if (con_id && strcmp(con_id, "gpios")) { |
| 1855 | snprintf(propname, sizeof(propname), "%s-%s", |
| 1856 | con_id, gpio_suffixes[i]); |
| 1857 | } else { |
| 1858 | snprintf(propname, sizeof(propname), "%s", |
| 1859 | gpio_suffixes[i]); |
| 1860 | } |
| 1861 | |
| 1862 | desc = acpi_get_gpiod_by_index(adev, propname, idx, &info); |
| 1863 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
| 1864 | break; |
| 1865 | } |
| 1866 | |
| 1867 | /* Then from plain _CRS GPIOs */ |
| 1868 | if (IS_ERR(desc)) { |
| 1869 | desc = acpi_get_gpiod_by_index(adev, NULL, idx, &info); |
| 1870 | if (IS_ERR(desc)) |
| 1871 | return desc; |
| 1872 | } |
| 1873 | |
| 1874 | if (info.active_low) |
| 1875 | *flags |= GPIO_ACTIVE_LOW; |
| 1876 | |
| 1877 | return desc; |
| 1878 | } |
| 1879 | |
| 1880 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
| 1881 | { |
| 1882 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 1883 | struct gpiod_lookup_table *table; |
| 1884 | |
| 1885 | mutex_lock(&gpio_lookup_lock); |
| 1886 | |
| 1887 | list_for_each_entry(table, &gpio_lookup_list, list) { |
| 1888 | if (table->dev_id && dev_id) { |
| 1889 | /* |
| 1890 | * Valid strings on both ends, must be identical to have |
| 1891 | * a match |
| 1892 | */ |
| 1893 | if (!strcmp(table->dev_id, dev_id)) |
| 1894 | goto found; |
| 1895 | } else { |
| 1896 | /* |
| 1897 | * One of the pointers is NULL, so both must be to have |
| 1898 | * a match |
| 1899 | */ |
| 1900 | if (dev_id == table->dev_id) |
| 1901 | goto found; |
| 1902 | } |
| 1903 | } |
| 1904 | table = NULL; |
| 1905 | |
| 1906 | found: |
| 1907 | mutex_unlock(&gpio_lookup_lock); |
| 1908 | return table; |
| 1909 | } |
| 1910 | |
| 1911 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
| 1912 | unsigned int idx, |
| 1913 | enum gpio_lookup_flags *flags) |
| 1914 | { |
| 1915 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
| 1916 | struct gpiod_lookup_table *table; |
| 1917 | struct gpiod_lookup *p; |
| 1918 | |
| 1919 | table = gpiod_find_lookup_table(dev); |
| 1920 | if (!table) |
| 1921 | return desc; |
| 1922 | |
| 1923 | for (p = &table->table[0]; p->chip_label; p++) { |
| 1924 | struct gpio_chip *chip; |
| 1925 | |
| 1926 | /* idx must always match exactly */ |
| 1927 | if (p->idx != idx) |
| 1928 | continue; |
| 1929 | |
| 1930 | /* If the lookup entry has a con_id, require exact match */ |
| 1931 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) |
| 1932 | continue; |
| 1933 | |
| 1934 | chip = find_chip_by_name(p->chip_label); |
| 1935 | |
| 1936 | if (!chip) { |
| 1937 | dev_err(dev, "cannot find GPIO chip %s\n", |
| 1938 | p->chip_label); |
| 1939 | return ERR_PTR(-ENODEV); |
| 1940 | } |
| 1941 | |
| 1942 | if (chip->ngpio <= p->chip_hwnum) { |
| 1943 | dev_err(dev, |
| 1944 | "requested GPIO %d is out of range [0..%d] for chip %s\n", |
| 1945 | idx, chip->ngpio, chip->label); |
| 1946 | return ERR_PTR(-EINVAL); |
| 1947 | } |
| 1948 | |
| 1949 | desc = gpiochip_get_desc(chip, p->chip_hwnum); |
| 1950 | *flags = p->flags; |
| 1951 | |
| 1952 | return desc; |
| 1953 | } |
| 1954 | |
| 1955 | return desc; |
| 1956 | } |
| 1957 | |
| 1958 | static int dt_gpio_count(struct device *dev, const char *con_id) |
| 1959 | { |
| 1960 | int ret; |
| 1961 | char propname[32]; |
| 1962 | unsigned int i; |
| 1963 | |
| 1964 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
| 1965 | if (con_id) |
| 1966 | snprintf(propname, sizeof(propname), "%s-%s", |
| 1967 | con_id, gpio_suffixes[i]); |
| 1968 | else |
| 1969 | snprintf(propname, sizeof(propname), "%s", |
| 1970 | gpio_suffixes[i]); |
| 1971 | |
| 1972 | ret = of_gpio_named_count(dev->of_node, propname); |
| 1973 | if (ret >= 0) |
| 1974 | break; |
| 1975 | } |
| 1976 | return ret; |
| 1977 | } |
| 1978 | |
| 1979 | static int platform_gpio_count(struct device *dev, const char *con_id) |
| 1980 | { |
| 1981 | struct gpiod_lookup_table *table; |
| 1982 | struct gpiod_lookup *p; |
| 1983 | unsigned int count = 0; |
| 1984 | |
| 1985 | table = gpiod_find_lookup_table(dev); |
| 1986 | if (!table) |
| 1987 | return -ENOENT; |
| 1988 | |
| 1989 | for (p = &table->table[0]; p->chip_label; p++) { |
| 1990 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || |
| 1991 | (!con_id && !p->con_id)) |
| 1992 | count++; |
| 1993 | } |
| 1994 | if (!count) |
| 1995 | return -ENOENT; |
| 1996 | |
| 1997 | return count; |
| 1998 | } |
| 1999 | |
| 2000 | /** |
| 2001 | * gpiod_count - return the number of GPIOs associated with a device / function |
| 2002 | * or -ENOENT if no GPIO has been assigned to the requested function |
| 2003 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2004 | * @con_id: function within the GPIO consumer |
| 2005 | */ |
| 2006 | int gpiod_count(struct device *dev, const char *con_id) |
| 2007 | { |
| 2008 | int count = -ENOENT; |
| 2009 | |
| 2010 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) |
| 2011 | count = dt_gpio_count(dev, con_id); |
| 2012 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) |
| 2013 | count = acpi_gpio_count(dev, con_id); |
| 2014 | |
| 2015 | if (count < 0) |
| 2016 | count = platform_gpio_count(dev, con_id); |
| 2017 | |
| 2018 | return count; |
| 2019 | } |
| 2020 | EXPORT_SYMBOL_GPL(gpiod_count); |
| 2021 | |
| 2022 | /** |
| 2023 | * gpiod_get - obtain a GPIO for a given GPIO function |
| 2024 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2025 | * @con_id: function within the GPIO consumer |
| 2026 | * @flags: optional GPIO initialization flags |
| 2027 | * |
| 2028 | * Return the GPIO descriptor corresponding to the function con_id of device |
| 2029 | * dev, -ENOENT if no GPIO has been assigned to the requested function, or |
| 2030 | * another IS_ERR() code if an error occurred while trying to acquire the GPIO. |
| 2031 | */ |
| 2032 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, |
| 2033 | enum gpiod_flags flags) |
| 2034 | { |
| 2035 | return gpiod_get_index(dev, con_id, 0, flags); |
| 2036 | } |
| 2037 | EXPORT_SYMBOL_GPL(gpiod_get); |
| 2038 | |
| 2039 | /** |
| 2040 | * gpiod_get_optional - obtain an optional GPIO for a given GPIO function |
| 2041 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2042 | * @con_id: function within the GPIO consumer |
| 2043 | * @flags: optional GPIO initialization flags |
| 2044 | * |
| 2045 | * This is equivalent to gpiod_get(), except that when no GPIO was assigned to |
| 2046 | * the requested function it will return NULL. This is convenient for drivers |
| 2047 | * that need to handle optional GPIOs. |
| 2048 | */ |
| 2049 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
| 2050 | const char *con_id, |
| 2051 | enum gpiod_flags flags) |
| 2052 | { |
| 2053 | return gpiod_get_index_optional(dev, con_id, 0, flags); |
| 2054 | } |
| 2055 | EXPORT_SYMBOL_GPL(gpiod_get_optional); |
| 2056 | |
| 2057 | |
| 2058 | /** |
| 2059 | * gpiod_configure_flags - helper function to configure a given GPIO |
| 2060 | * @desc: gpio whose value will be assigned |
| 2061 | * @con_id: function within the GPIO consumer |
| 2062 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
| 2063 | * of_get_gpio_hog() |
| 2064 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 2065 | * |
| 2066 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the |
| 2067 | * requested function and/or index, or another IS_ERR() code if an error |
| 2068 | * occurred while trying to acquire the GPIO. |
| 2069 | */ |
| 2070 | static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
| 2071 | unsigned long lflags, enum gpiod_flags dflags) |
| 2072 | { |
| 2073 | int status; |
| 2074 | |
| 2075 | if (lflags & GPIO_ACTIVE_LOW) |
| 2076 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 2077 | if (lflags & GPIO_OPEN_DRAIN) |
| 2078 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 2079 | if (lflags & GPIO_OPEN_SOURCE) |
| 2080 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 2081 | |
| 2082 | /* No particular flag request, return here... */ |
| 2083 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { |
| 2084 | pr_debug("no flags found for %s\n", con_id); |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | /* Process flags */ |
| 2089 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) |
| 2090 | status = gpiod_direction_output(desc, |
| 2091 | dflags & GPIOD_FLAGS_BIT_DIR_VAL); |
| 2092 | else |
| 2093 | status = gpiod_direction_input(desc); |
| 2094 | |
| 2095 | return status; |
| 2096 | } |
| 2097 | |
| 2098 | /** |
| 2099 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function |
| 2100 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2101 | * @con_id: function within the GPIO consumer |
| 2102 | * @idx: index of the GPIO to obtain in the consumer |
| 2103 | * @flags: optional GPIO initialization flags |
| 2104 | * |
| 2105 | * This variant of gpiod_get() allows to access GPIOs other than the first |
| 2106 | * defined one for functions that define several GPIOs. |
| 2107 | * |
| 2108 | * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the |
| 2109 | * requested function and/or index, or another IS_ERR() code if an error |
| 2110 | * occurred while trying to acquire the GPIO. |
| 2111 | */ |
| 2112 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 2113 | const char *con_id, |
| 2114 | unsigned int idx, |
| 2115 | enum gpiod_flags flags) |
| 2116 | { |
| 2117 | struct gpio_desc *desc = NULL; |
| 2118 | int status; |
| 2119 | enum gpio_lookup_flags lookupflags = 0; |
| 2120 | |
| 2121 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); |
| 2122 | |
| 2123 | if (dev) { |
| 2124 | /* Using device tree? */ |
| 2125 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
| 2126 | dev_dbg(dev, "using device tree for GPIO lookup\n"); |
| 2127 | desc = of_find_gpio(dev, con_id, idx, &lookupflags); |
| 2128 | } else if (ACPI_COMPANION(dev)) { |
| 2129 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); |
| 2130 | desc = acpi_find_gpio(dev, con_id, idx, &lookupflags); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | * Either we are not using DT or ACPI, or their lookup did not return |
| 2136 | * a result. In that case, use platform lookup as a fallback. |
| 2137 | */ |
| 2138 | if (!desc || desc == ERR_PTR(-ENOENT)) { |
| 2139 | dev_dbg(dev, "using lookup tables for GPIO lookup\n"); |
| 2140 | desc = gpiod_find(dev, con_id, idx, &lookupflags); |
| 2141 | } |
| 2142 | |
| 2143 | if (IS_ERR(desc)) { |
| 2144 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
| 2145 | return desc; |
| 2146 | } |
| 2147 | |
| 2148 | status = gpiod_request(desc, con_id); |
| 2149 | if (status < 0) |
| 2150 | return ERR_PTR(status); |
| 2151 | |
| 2152 | status = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
| 2153 | if (status < 0) { |
| 2154 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); |
| 2155 | gpiod_put(desc); |
| 2156 | return ERR_PTR(status); |
| 2157 | } |
| 2158 | |
| 2159 | return desc; |
| 2160 | } |
| 2161 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
| 2162 | |
| 2163 | /** |
| 2164 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node |
| 2165 | * @fwnode: handle of the firmware node |
| 2166 | * @propname: name of the firmware property representing the GPIO |
| 2167 | * |
| 2168 | * This function can be used for drivers that get their configuration |
| 2169 | * from firmware. |
| 2170 | * |
| 2171 | * Function properly finds the corresponding GPIO using whatever is the |
| 2172 | * underlying firmware interface and then makes sure that the GPIO |
| 2173 | * descriptor is requested before it is returned to the caller. |
| 2174 | * |
| 2175 | * In case of error an ERR_PTR() is returned. |
| 2176 | */ |
| 2177 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, |
| 2178 | const char *propname) |
| 2179 | { |
| 2180 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
| 2181 | bool active_low = false; |
| 2182 | bool single_ended = false; |
| 2183 | int ret; |
| 2184 | |
| 2185 | if (!fwnode) |
| 2186 | return ERR_PTR(-EINVAL); |
| 2187 | |
| 2188 | if (is_of_node(fwnode)) { |
| 2189 | enum of_gpio_flags flags; |
| 2190 | |
| 2191 | desc = of_get_named_gpiod_flags(to_of_node(fwnode), propname, 0, |
| 2192 | &flags); |
| 2193 | if (!IS_ERR(desc)) { |
| 2194 | active_low = flags & OF_GPIO_ACTIVE_LOW; |
| 2195 | single_ended = flags & OF_GPIO_SINGLE_ENDED; |
| 2196 | } |
| 2197 | } else if (is_acpi_node(fwnode)) { |
| 2198 | struct acpi_gpio_info info; |
| 2199 | |
| 2200 | desc = acpi_node_get_gpiod(fwnode, propname, 0, &info); |
| 2201 | if (!IS_ERR(desc)) |
| 2202 | active_low = info.active_low; |
| 2203 | } |
| 2204 | |
| 2205 | if (IS_ERR(desc)) |
| 2206 | return desc; |
| 2207 | |
| 2208 | ret = gpiod_request(desc, NULL); |
| 2209 | if (ret) |
| 2210 | return ERR_PTR(ret); |
| 2211 | |
| 2212 | if (active_low) |
| 2213 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
| 2214 | |
| 2215 | if (single_ended) { |
| 2216 | if (active_low) |
| 2217 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
| 2218 | else |
| 2219 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
| 2220 | } |
| 2221 | |
| 2222 | return desc; |
| 2223 | } |
| 2224 | EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); |
| 2225 | |
| 2226 | /** |
| 2227 | * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO |
| 2228 | * function |
| 2229 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2230 | * @con_id: function within the GPIO consumer |
| 2231 | * @index: index of the GPIO to obtain in the consumer |
| 2232 | * @flags: optional GPIO initialization flags |
| 2233 | * |
| 2234 | * This is equivalent to gpiod_get_index(), except that when no GPIO with the |
| 2235 | * specified index was assigned to the requested function it will return NULL. |
| 2236 | * This is convenient for drivers that need to handle optional GPIOs. |
| 2237 | */ |
| 2238 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
| 2239 | const char *con_id, |
| 2240 | unsigned int index, |
| 2241 | enum gpiod_flags flags) |
| 2242 | { |
| 2243 | struct gpio_desc *desc; |
| 2244 | |
| 2245 | desc = gpiod_get_index(dev, con_id, index, flags); |
| 2246 | if (IS_ERR(desc)) { |
| 2247 | if (PTR_ERR(desc) == -ENOENT) |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | |
| 2251 | return desc; |
| 2252 | } |
| 2253 | EXPORT_SYMBOL_GPL(gpiod_get_index_optional); |
| 2254 | |
| 2255 | /** |
| 2256 | * gpiod_hog - Hog the specified GPIO desc given the provided flags |
| 2257 | * @desc: gpio whose value will be assigned |
| 2258 | * @name: gpio line name |
| 2259 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
| 2260 | * of_get_gpio_hog() |
| 2261 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 2262 | */ |
| 2263 | int gpiod_hog(struct gpio_desc *desc, const char *name, |
| 2264 | unsigned long lflags, enum gpiod_flags dflags) |
| 2265 | { |
| 2266 | struct gpio_chip *chip; |
| 2267 | struct gpio_desc *local_desc; |
| 2268 | int hwnum; |
| 2269 | int status; |
| 2270 | |
| 2271 | chip = gpiod_to_chip(desc); |
| 2272 | hwnum = gpio_chip_hwgpio(desc); |
| 2273 | |
| 2274 | local_desc = gpiochip_request_own_desc(chip, hwnum, name); |
| 2275 | if (IS_ERR(local_desc)) { |
| 2276 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed\n", |
| 2277 | name, chip->label, hwnum); |
| 2278 | return PTR_ERR(local_desc); |
| 2279 | } |
| 2280 | |
| 2281 | status = gpiod_configure_flags(desc, name, lflags, dflags); |
| 2282 | if (status < 0) { |
| 2283 | pr_err("setup of hog GPIO %s (chip %s, offset %d) failed\n", |
| 2284 | name, chip->label, hwnum); |
| 2285 | gpiochip_free_own_desc(desc); |
| 2286 | return status; |
| 2287 | } |
| 2288 | |
| 2289 | /* Mark GPIO as hogged so it can be identified and removed later */ |
| 2290 | set_bit(FLAG_IS_HOGGED, &desc->flags); |
| 2291 | |
| 2292 | pr_info("GPIO line %d (%s) hogged as %s%s\n", |
| 2293 | desc_to_gpio(desc), name, |
| 2294 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", |
| 2295 | (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? |
| 2296 | (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); |
| 2297 | |
| 2298 | return 0; |
| 2299 | } |
| 2300 | |
| 2301 | /** |
| 2302 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog |
| 2303 | * @chip: gpio chip to act on |
| 2304 | * |
| 2305 | * This is only used by of_gpiochip_remove to free hogged gpios |
| 2306 | */ |
| 2307 | static void gpiochip_free_hogs(struct gpio_chip *chip) |
| 2308 | { |
| 2309 | int id; |
| 2310 | |
| 2311 | for (id = 0; id < chip->ngpio; id++) { |
| 2312 | if (test_bit(FLAG_IS_HOGGED, &chip->desc[id].flags)) |
| 2313 | gpiochip_free_own_desc(&chip->desc[id]); |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | /** |
| 2318 | * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function |
| 2319 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2320 | * @con_id: function within the GPIO consumer |
| 2321 | * @flags: optional GPIO initialization flags |
| 2322 | * |
| 2323 | * This function acquires all the GPIOs defined under a given function. |
| 2324 | * |
| 2325 | * Return a struct gpio_descs containing an array of descriptors, -ENOENT if |
| 2326 | * no GPIO has been assigned to the requested function, or another IS_ERR() |
| 2327 | * code if an error occurred while trying to acquire the GPIOs. |
| 2328 | */ |
| 2329 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, |
| 2330 | const char *con_id, |
| 2331 | enum gpiod_flags flags) |
| 2332 | { |
| 2333 | struct gpio_desc *desc; |
| 2334 | struct gpio_descs *descs; |
| 2335 | int count; |
| 2336 | |
| 2337 | count = gpiod_count(dev, con_id); |
| 2338 | if (count < 0) |
| 2339 | return ERR_PTR(count); |
| 2340 | |
| 2341 | descs = kzalloc(sizeof(*descs) + sizeof(descs->desc[0]) * count, |
| 2342 | GFP_KERNEL); |
| 2343 | if (!descs) |
| 2344 | return ERR_PTR(-ENOMEM); |
| 2345 | |
| 2346 | for (descs->ndescs = 0; descs->ndescs < count; ) { |
| 2347 | desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); |
| 2348 | if (IS_ERR(desc)) { |
| 2349 | gpiod_put_array(descs); |
| 2350 | return ERR_CAST(desc); |
| 2351 | } |
| 2352 | descs->desc[descs->ndescs] = desc; |
| 2353 | descs->ndescs++; |
| 2354 | } |
| 2355 | return descs; |
| 2356 | } |
| 2357 | EXPORT_SYMBOL_GPL(gpiod_get_array); |
| 2358 | |
| 2359 | /** |
| 2360 | * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO |
| 2361 | * function |
| 2362 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
| 2363 | * @con_id: function within the GPIO consumer |
| 2364 | * @flags: optional GPIO initialization flags |
| 2365 | * |
| 2366 | * This is equivalent to gpiod_get_array(), except that when no GPIO was |
| 2367 | * assigned to the requested function it will return NULL. |
| 2368 | */ |
| 2369 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, |
| 2370 | const char *con_id, |
| 2371 | enum gpiod_flags flags) |
| 2372 | { |
| 2373 | struct gpio_descs *descs; |
| 2374 | |
| 2375 | descs = gpiod_get_array(dev, con_id, flags); |
| 2376 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) |
| 2377 | return NULL; |
| 2378 | |
| 2379 | return descs; |
| 2380 | } |
| 2381 | EXPORT_SYMBOL_GPL(gpiod_get_array_optional); |
| 2382 | |
| 2383 | /** |
| 2384 | * gpiod_put - dispose of a GPIO descriptor |
| 2385 | * @desc: GPIO descriptor to dispose of |
| 2386 | * |
| 2387 | * No descriptor can be used after gpiod_put() has been called on it. |
| 2388 | */ |
| 2389 | void gpiod_put(struct gpio_desc *desc) |
| 2390 | { |
| 2391 | gpiod_free(desc); |
| 2392 | } |
| 2393 | EXPORT_SYMBOL_GPL(gpiod_put); |
| 2394 | |
| 2395 | /** |
| 2396 | * gpiod_put_array - dispose of multiple GPIO descriptors |
| 2397 | * @descs: struct gpio_descs containing an array of descriptors |
| 2398 | */ |
| 2399 | void gpiod_put_array(struct gpio_descs *descs) |
| 2400 | { |
| 2401 | unsigned int i; |
| 2402 | |
| 2403 | for (i = 0; i < descs->ndescs; i++) |
| 2404 | gpiod_put(descs->desc[i]); |
| 2405 | |
| 2406 | kfree(descs); |
| 2407 | } |
| 2408 | EXPORT_SYMBOL_GPL(gpiod_put_array); |
| 2409 | |
| 2410 | #ifdef CONFIG_DEBUG_FS |
| 2411 | |
| 2412 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 2413 | { |
| 2414 | unsigned i; |
| 2415 | unsigned gpio = chip->base; |
| 2416 | struct gpio_desc *gdesc = &chip->desc[0]; |
| 2417 | int is_out; |
| 2418 | int is_irq; |
| 2419 | |
| 2420 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { |
| 2421 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
| 2422 | if (gdesc->name) { |
| 2423 | seq_printf(s, " gpio-%-3d (%-20.20s)\n", |
| 2424 | gpio, gdesc->name); |
| 2425 | } |
| 2426 | continue; |
| 2427 | } |
| 2428 | |
| 2429 | gpiod_get_direction(gdesc); |
| 2430 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
| 2431 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
| 2432 | seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s", |
| 2433 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, |
| 2434 | is_out ? "out" : "in ", |
| 2435 | chip->get |
| 2436 | ? (chip->get(chip, i) ? "hi" : "lo") |
| 2437 | : "? ", |
| 2438 | is_irq ? "IRQ" : " "); |
| 2439 | seq_printf(s, "\n"); |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
| 2444 | { |
| 2445 | unsigned long flags; |
| 2446 | struct gpio_chip *chip = NULL; |
| 2447 | loff_t index = *pos; |
| 2448 | |
| 2449 | s->private = ""; |
| 2450 | |
| 2451 | spin_lock_irqsave(&gpio_lock, flags); |
| 2452 | list_for_each_entry(chip, &gpio_chips, list) |
| 2453 | if (index-- == 0) { |
| 2454 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 2455 | return chip; |
| 2456 | } |
| 2457 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 2458 | |
| 2459 | return NULL; |
| 2460 | } |
| 2461 | |
| 2462 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2463 | { |
| 2464 | unsigned long flags; |
| 2465 | struct gpio_chip *chip = v; |
| 2466 | void *ret = NULL; |
| 2467 | |
| 2468 | spin_lock_irqsave(&gpio_lock, flags); |
| 2469 | if (list_is_last(&chip->list, &gpio_chips)) |
| 2470 | ret = NULL; |
| 2471 | else |
| 2472 | ret = list_entry(chip->list.next, struct gpio_chip, list); |
| 2473 | spin_unlock_irqrestore(&gpio_lock, flags); |
| 2474 | |
| 2475 | s->private = "\n"; |
| 2476 | ++*pos; |
| 2477 | |
| 2478 | return ret; |
| 2479 | } |
| 2480 | |
| 2481 | static void gpiolib_seq_stop(struct seq_file *s, void *v) |
| 2482 | { |
| 2483 | } |
| 2484 | |
| 2485 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
| 2486 | { |
| 2487 | struct gpio_chip *chip = v; |
| 2488 | struct device *dev; |
| 2489 | |
| 2490 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, |
| 2491 | chip->base, chip->base + chip->ngpio - 1); |
| 2492 | dev = chip->dev; |
| 2493 | if (dev) |
| 2494 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", |
| 2495 | dev_name(dev)); |
| 2496 | if (chip->label) |
| 2497 | seq_printf(s, ", %s", chip->label); |
| 2498 | if (chip->can_sleep) |
| 2499 | seq_printf(s, ", can sleep"); |
| 2500 | seq_printf(s, ":\n"); |
| 2501 | |
| 2502 | if (chip->dbg_show) |
| 2503 | chip->dbg_show(s, chip); |
| 2504 | else |
| 2505 | gpiolib_dbg_show(s, chip); |
| 2506 | |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
| 2510 | static const struct seq_operations gpiolib_seq_ops = { |
| 2511 | .start = gpiolib_seq_start, |
| 2512 | .next = gpiolib_seq_next, |
| 2513 | .stop = gpiolib_seq_stop, |
| 2514 | .show = gpiolib_seq_show, |
| 2515 | }; |
| 2516 | |
| 2517 | static int gpiolib_open(struct inode *inode, struct file *file) |
| 2518 | { |
| 2519 | return seq_open(file, &gpiolib_seq_ops); |
| 2520 | } |
| 2521 | |
| 2522 | static const struct file_operations gpiolib_operations = { |
| 2523 | .owner = THIS_MODULE, |
| 2524 | .open = gpiolib_open, |
| 2525 | .read = seq_read, |
| 2526 | .llseek = seq_lseek, |
| 2527 | .release = seq_release, |
| 2528 | }; |
| 2529 | |
| 2530 | static int __init gpiolib_debugfs_init(void) |
| 2531 | { |
| 2532 | /* /sys/kernel/debug/gpio */ |
| 2533 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
| 2534 | NULL, NULL, &gpiolib_operations); |
| 2535 | return 0; |
| 2536 | } |
| 2537 | subsys_initcall(gpiolib_debugfs_init); |
| 2538 | |
| 2539 | #endif /* DEBUG_FS */ |