| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | #include <linux/bitmap.h> |
|---|
| 2 | 3 | #include <linux/kernel.h> |
|---|
| 3 | 4 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 10 | 11 | #include <linux/debugfs.h> |
|---|
| 11 | 12 | #include <linux/seq_file.h> |
|---|
| 12 | 13 | #include <linux/gpio.h> |
|---|
| 13 | | -#include <linux/of_gpio.h> |
|---|
| 14 | 14 | #include <linux/idr.h> |
|---|
| 15 | 15 | #include <linux/slab.h> |
|---|
| 16 | 16 | #include <linux/acpi.h> |
|---|
| 17 | 17 | #include <linux/gpio/driver.h> |
|---|
| 18 | 18 | #include <linux/gpio/machine.h> |
|---|
| 19 | 19 | #include <linux/pinctrl/consumer.h> |
|---|
| 20 | | -#include <linux/cdev.h> |
|---|
| 21 | 20 | #include <linux/fs.h> |
|---|
| 22 | | -#include <linux/uaccess.h> |
|---|
| 23 | 21 | #include <linux/compat.h> |
|---|
| 24 | | -#include <linux/anon_inodes.h> |
|---|
| 25 | 22 | #include <linux/file.h> |
|---|
| 26 | | -#include <linux/kfifo.h> |
|---|
| 27 | | -#include <linux/poll.h> |
|---|
| 28 | | -#include <linux/timekeeping.h> |
|---|
| 29 | 23 | #include <uapi/linux/gpio.h> |
|---|
| 30 | 24 | |
|---|
| 31 | 25 | #include "gpiolib.h" |
|---|
| 26 | +#include "gpiolib-of.h" |
|---|
| 27 | +#include "gpiolib-acpi.h" |
|---|
| 28 | +#include "gpiolib-cdev.h" |
|---|
| 29 | +#include "gpiolib-sysfs.h" |
|---|
| 32 | 30 | |
|---|
| 33 | 31 | #define CREATE_TRACE_POINTS |
|---|
| 34 | 32 | #include <trace/events/gpio.h> |
|---|
| 33 | +#undef CREATE_TRACE_POINTS |
|---|
| 34 | +#include <trace/hooks/gpiolib.h> |
|---|
| 35 | 35 | |
|---|
| 36 | 36 | /* Implementation infrastructure for GPIO interfaces. |
|---|
| 37 | 37 | * |
|---|
| .. | .. |
|---|
| 57 | 57 | static DEFINE_IDA(gpio_ida); |
|---|
| 58 | 58 | static dev_t gpio_devt; |
|---|
| 59 | 59 | #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */ |
|---|
| 60 | +static int gpio_bus_match(struct device *dev, struct device_driver *drv); |
|---|
| 60 | 61 | static struct bus_type gpio_bus_type = { |
|---|
| 61 | 62 | .name = "gpio", |
|---|
| 63 | + .match = gpio_bus_match, |
|---|
| 62 | 64 | }; |
|---|
| 63 | 65 | |
|---|
| 64 | 66 | /* |
|---|
| .. | .. |
|---|
| 79 | 81 | static DEFINE_MUTEX(gpio_machine_hogs_mutex); |
|---|
| 80 | 82 | static LIST_HEAD(gpio_machine_hogs); |
|---|
| 81 | 83 | |
|---|
| 82 | | -static void gpiochip_free_hogs(struct gpio_chip *chip); |
|---|
| 83 | | -static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
|---|
| 84 | +static void gpiochip_free_hogs(struct gpio_chip *gc); |
|---|
| 85 | +static int gpiochip_add_irqchip(struct gpio_chip *gc, |
|---|
| 84 | 86 | struct lock_class_key *lock_key, |
|---|
| 85 | 87 | struct lock_class_key *request_key); |
|---|
| 86 | | -static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip); |
|---|
| 87 | | -static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip); |
|---|
| 88 | | -static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip); |
|---|
| 88 | +static void gpiochip_irqchip_remove(struct gpio_chip *gc); |
|---|
| 89 | +static int gpiochip_irqchip_init_hw(struct gpio_chip *gc); |
|---|
| 90 | +static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc); |
|---|
| 91 | +static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc); |
|---|
| 89 | 92 | |
|---|
| 90 | 93 | static bool gpiolib_initialized; |
|---|
| 91 | 94 | |
|---|
| .. | .. |
|---|
| 129 | 132 | /** |
|---|
| 130 | 133 | * gpiochip_get_desc - get the GPIO descriptor corresponding to the given |
|---|
| 131 | 134 | * hardware number for this chip |
|---|
| 132 | | - * @chip: GPIO chip |
|---|
| 135 | + * @gc: GPIO chip |
|---|
| 133 | 136 | * @hwnum: hardware number of the GPIO for this chip |
|---|
| 134 | 137 | * |
|---|
| 135 | 138 | * Returns: |
|---|
| 136 | | - * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists |
|---|
| 139 | + * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists |
|---|
| 137 | 140 | * in the given chip for the specified hardware number. |
|---|
| 138 | 141 | */ |
|---|
| 139 | | -struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, |
|---|
| 140 | | - u16 hwnum) |
|---|
| 142 | +struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, |
|---|
| 143 | + unsigned int hwnum) |
|---|
| 141 | 144 | { |
|---|
| 142 | | - struct gpio_device *gdev = chip->gpiodev; |
|---|
| 145 | + struct gpio_device *gdev = gc->gpiodev; |
|---|
| 143 | 146 | |
|---|
| 144 | 147 | if (hwnum >= gdev->ngpio) |
|---|
| 145 | 148 | return ERR_PTR(-EINVAL); |
|---|
| 146 | 149 | |
|---|
| 147 | 150 | return &gdev->descs[hwnum]; |
|---|
| 148 | 151 | } |
|---|
| 152 | +EXPORT_SYMBOL_GPL(gpiochip_get_desc); |
|---|
| 149 | 153 | |
|---|
| 150 | 154 | /** |
|---|
| 151 | 155 | * desc_to_gpio - convert a GPIO descriptor to the integer namespace |
|---|
| .. | .. |
|---|
| 186 | 190 | /* found a free space? */ |
|---|
| 187 | 191 | if (gdev->base + gdev->ngpio <= base) |
|---|
| 188 | 192 | break; |
|---|
| 189 | | - else |
|---|
| 190 | | - /* nope, check the space right before the chip */ |
|---|
| 191 | | - base = gdev->base - ngpio; |
|---|
| 193 | + /* nope, check the space right before the chip */ |
|---|
| 194 | + base = gdev->base - ngpio; |
|---|
| 192 | 195 | } |
|---|
| 193 | 196 | |
|---|
| 194 | 197 | if (gpio_is_valid(base)) { |
|---|
| .. | .. |
|---|
| 210 | 213 | */ |
|---|
| 211 | 214 | int gpiod_get_direction(struct gpio_desc *desc) |
|---|
| 212 | 215 | { |
|---|
| 213 | | - struct gpio_chip *chip; |
|---|
| 214 | | - unsigned offset; |
|---|
| 215 | | - int status = -EINVAL; |
|---|
| 216 | + struct gpio_chip *gc; |
|---|
| 217 | + unsigned offset; |
|---|
| 218 | + int ret; |
|---|
| 216 | 219 | |
|---|
| 217 | | - chip = gpiod_to_chip(desc); |
|---|
| 220 | + gc = gpiod_to_chip(desc); |
|---|
| 218 | 221 | offset = gpio_chip_hwgpio(desc); |
|---|
| 219 | 222 | |
|---|
| 220 | 223 | /* |
|---|
| .. | .. |
|---|
| 225 | 228 | test_bit(FLAG_IS_OUT, &desc->flags)) |
|---|
| 226 | 229 | return 0; |
|---|
| 227 | 230 | |
|---|
| 228 | | - if (!chip->get_direction) |
|---|
| 229 | | - return status; |
|---|
| 231 | + if (!gc->get_direction) |
|---|
| 232 | + return -ENOTSUPP; |
|---|
| 230 | 233 | |
|---|
| 231 | | - status = chip->get_direction(chip, offset); |
|---|
| 232 | | - if (status > 0) { |
|---|
| 233 | | - /* GPIOF_DIR_IN, or other positive */ |
|---|
| 234 | | - status = 1; |
|---|
| 235 | | - clear_bit(FLAG_IS_OUT, &desc->flags); |
|---|
| 236 | | - } |
|---|
| 237 | | - if (status == 0) { |
|---|
| 238 | | - /* GPIOF_DIR_OUT */ |
|---|
| 239 | | - set_bit(FLAG_IS_OUT, &desc->flags); |
|---|
| 240 | | - } |
|---|
| 241 | | - return status; |
|---|
| 234 | + ret = gc->get_direction(gc, offset); |
|---|
| 235 | + if (ret < 0) |
|---|
| 236 | + return ret; |
|---|
| 237 | + |
|---|
| 238 | + /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */ |
|---|
| 239 | + if (ret > 0) |
|---|
| 240 | + ret = 1; |
|---|
| 241 | + |
|---|
| 242 | + assign_bit(FLAG_IS_OUT, &desc->flags, !ret); |
|---|
| 243 | + |
|---|
| 244 | + return ret; |
|---|
| 242 | 245 | } |
|---|
| 243 | 246 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
|---|
| 244 | 247 | |
|---|
| .. | .. |
|---|
| 292 | 295 | |
|---|
| 293 | 296 | /* |
|---|
| 294 | 297 | * Convert a GPIO name to its descriptor |
|---|
| 298 | + * Note that there is no guarantee that GPIO names are globally unique! |
|---|
| 299 | + * Hence this function will return, if it exists, a reference to the first GPIO |
|---|
| 300 | + * line found that matches the given name. |
|---|
| 295 | 301 | */ |
|---|
| 296 | 302 | static struct gpio_desc *gpio_name_to_desc(const char * const name) |
|---|
| 297 | 303 | { |
|---|
| 298 | 304 | struct gpio_device *gdev; |
|---|
| 299 | 305 | unsigned long flags; |
|---|
| 306 | + |
|---|
| 307 | + if (!name) |
|---|
| 308 | + return NULL; |
|---|
| 300 | 309 | |
|---|
| 301 | 310 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 302 | 311 | |
|---|
| .. | .. |
|---|
| 306 | 315 | for (i = 0; i != gdev->ngpio; ++i) { |
|---|
| 307 | 316 | struct gpio_desc *desc = &gdev->descs[i]; |
|---|
| 308 | 317 | |
|---|
| 309 | | - if (!desc->name || !name) |
|---|
| 318 | + if (!desc->name) |
|---|
| 310 | 319 | continue; |
|---|
| 311 | 320 | |
|---|
| 312 | 321 | if (!strcmp(desc->name, name)) { |
|---|
| .. | .. |
|---|
| 322 | 331 | } |
|---|
| 323 | 332 | |
|---|
| 324 | 333 | /* |
|---|
| 325 | | - * Takes the names from gc->names and checks if they are all unique. If they |
|---|
| 326 | | - * are, they are assigned to their gpio descriptors. |
|---|
| 334 | + * Take the names from gc->names and assign them to their GPIO descriptors. |
|---|
| 335 | + * Warn if a name is already used for a GPIO line on a different GPIO chip. |
|---|
| 327 | 336 | * |
|---|
| 328 | | - * Warning if one of the names is already used for a different GPIO. |
|---|
| 337 | + * Note that: |
|---|
| 338 | + * 1. Non-unique names are still accepted, |
|---|
| 339 | + * 2. Name collisions within the same GPIO chip are not reported. |
|---|
| 329 | 340 | */ |
|---|
| 330 | 341 | static int gpiochip_set_desc_names(struct gpio_chip *gc) |
|---|
| 331 | 342 | { |
|---|
| 332 | 343 | struct gpio_device *gdev = gc->gpiodev; |
|---|
| 333 | 344 | int i; |
|---|
| 334 | | - |
|---|
| 335 | | - if (!gc->names) |
|---|
| 336 | | - return 0; |
|---|
| 337 | 345 | |
|---|
| 338 | 346 | /* First check all names if they are unique */ |
|---|
| 339 | 347 | for (i = 0; i != gc->ngpio; ++i) { |
|---|
| .. | .. |
|---|
| 353 | 361 | return 0; |
|---|
| 354 | 362 | } |
|---|
| 355 | 363 | |
|---|
| 356 | | -static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip) |
|---|
| 364 | +/* |
|---|
| 365 | + * devprop_gpiochip_set_names - Set GPIO line names using device properties |
|---|
| 366 | + * @chip: GPIO chip whose lines should be named, if possible |
|---|
| 367 | + * |
|---|
| 368 | + * Looks for device property "gpio-line-names" and if it exists assigns |
|---|
| 369 | + * GPIO line names for the chip. The memory allocated for the assigned |
|---|
| 370 | + * names belong to the underlying firmware node and should not be released |
|---|
| 371 | + * by the caller. |
|---|
| 372 | + */ |
|---|
| 373 | +static int devprop_gpiochip_set_names(struct gpio_chip *chip) |
|---|
| 374 | +{ |
|---|
| 375 | + struct gpio_device *gdev = chip->gpiodev; |
|---|
| 376 | + struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev); |
|---|
| 377 | + const char **names; |
|---|
| 378 | + int ret, i; |
|---|
| 379 | + int count; |
|---|
| 380 | + |
|---|
| 381 | + count = fwnode_property_string_array_count(fwnode, "gpio-line-names"); |
|---|
| 382 | + if (count < 0) |
|---|
| 383 | + return 0; |
|---|
| 384 | + |
|---|
| 385 | + if (count > gdev->ngpio) { |
|---|
| 386 | + dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d", |
|---|
| 387 | + count, gdev->ngpio); |
|---|
| 388 | + count = gdev->ngpio; |
|---|
| 389 | + } |
|---|
| 390 | + |
|---|
| 391 | + names = kcalloc(count, sizeof(*names), GFP_KERNEL); |
|---|
| 392 | + if (!names) |
|---|
| 393 | + return -ENOMEM; |
|---|
| 394 | + |
|---|
| 395 | + ret = fwnode_property_read_string_array(fwnode, "gpio-line-names", |
|---|
| 396 | + names, count); |
|---|
| 397 | + if (ret < 0) { |
|---|
| 398 | + dev_warn(&gdev->dev, "failed to read GPIO line names\n"); |
|---|
| 399 | + kfree(names); |
|---|
| 400 | + return ret; |
|---|
| 401 | + } |
|---|
| 402 | + |
|---|
| 403 | + for (i = 0; i < count; i++) |
|---|
| 404 | + gdev->descs[i].name = names[i]; |
|---|
| 405 | + |
|---|
| 406 | + kfree(names); |
|---|
| 407 | + |
|---|
| 408 | + return 0; |
|---|
| 409 | +} |
|---|
| 410 | + |
|---|
| 411 | +static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc) |
|---|
| 357 | 412 | { |
|---|
| 358 | 413 | unsigned long *p; |
|---|
| 359 | 414 | |
|---|
| 360 | | - p = kmalloc_array(BITS_TO_LONGS(chip->ngpio), sizeof(*p), GFP_KERNEL); |
|---|
| 415 | + p = bitmap_alloc(gc->ngpio, GFP_KERNEL); |
|---|
| 361 | 416 | if (!p) |
|---|
| 362 | 417 | return NULL; |
|---|
| 363 | 418 | |
|---|
| 364 | 419 | /* Assume by default all GPIOs are valid */ |
|---|
| 365 | | - bitmap_fill(p, chip->ngpio); |
|---|
| 420 | + bitmap_fill(p, gc->ngpio); |
|---|
| 366 | 421 | |
|---|
| 367 | 422 | return p; |
|---|
| 368 | 423 | } |
|---|
| 369 | 424 | |
|---|
| 370 | | -static int gpiochip_init_valid_mask(struct gpio_chip *gpiochip) |
|---|
| 425 | +static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) |
|---|
| 371 | 426 | { |
|---|
| 372 | | -#ifdef CONFIG_OF_GPIO |
|---|
| 373 | | - int size; |
|---|
| 374 | | - struct device_node *np = gpiochip->of_node; |
|---|
| 375 | | - |
|---|
| 376 | | - size = of_property_count_u32_elems(np, "gpio-reserved-ranges"); |
|---|
| 377 | | - if (size > 0 && size % 2 == 0) |
|---|
| 378 | | - gpiochip->need_valid_mask = true; |
|---|
| 379 | | -#endif |
|---|
| 380 | | - |
|---|
| 381 | | - if (!gpiochip->need_valid_mask) |
|---|
| 427 | + if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask)) |
|---|
| 382 | 428 | return 0; |
|---|
| 383 | 429 | |
|---|
| 384 | | - gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip); |
|---|
| 385 | | - if (!gpiochip->valid_mask) |
|---|
| 430 | + gc->valid_mask = gpiochip_allocate_mask(gc); |
|---|
| 431 | + if (!gc->valid_mask) |
|---|
| 386 | 432 | return -ENOMEM; |
|---|
| 387 | 433 | |
|---|
| 388 | 434 | return 0; |
|---|
| 389 | 435 | } |
|---|
| 390 | 436 | |
|---|
| 391 | | -static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip) |
|---|
| 437 | +static int gpiochip_init_valid_mask(struct gpio_chip *gc) |
|---|
| 392 | 438 | { |
|---|
| 393 | | - kfree(gpiochip->valid_mask); |
|---|
| 394 | | - gpiochip->valid_mask = NULL; |
|---|
| 439 | + if (gc->init_valid_mask) |
|---|
| 440 | + return gc->init_valid_mask(gc, |
|---|
| 441 | + gc->valid_mask, |
|---|
| 442 | + gc->ngpio); |
|---|
| 443 | + |
|---|
| 444 | + return 0; |
|---|
| 395 | 445 | } |
|---|
| 396 | 446 | |
|---|
| 397 | | -bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip, |
|---|
| 447 | +static void gpiochip_free_valid_mask(struct gpio_chip *gc) |
|---|
| 448 | +{ |
|---|
| 449 | + bitmap_free(gc->valid_mask); |
|---|
| 450 | + gc->valid_mask = NULL; |
|---|
| 451 | +} |
|---|
| 452 | + |
|---|
| 453 | +static int gpiochip_add_pin_ranges(struct gpio_chip *gc) |
|---|
| 454 | +{ |
|---|
| 455 | + if (gc->add_pin_ranges) |
|---|
| 456 | + return gc->add_pin_ranges(gc); |
|---|
| 457 | + |
|---|
| 458 | + return 0; |
|---|
| 459 | +} |
|---|
| 460 | + |
|---|
| 461 | +bool gpiochip_line_is_valid(const struct gpio_chip *gc, |
|---|
| 398 | 462 | unsigned int offset) |
|---|
| 399 | 463 | { |
|---|
| 400 | 464 | /* No mask means all valid */ |
|---|
| 401 | | - if (likely(!gpiochip->valid_mask)) |
|---|
| 465 | + if (likely(!gc->valid_mask)) |
|---|
| 402 | 466 | return true; |
|---|
| 403 | | - return test_bit(offset, gpiochip->valid_mask); |
|---|
| 467 | + return test_bit(offset, gc->valid_mask); |
|---|
| 404 | 468 | } |
|---|
| 405 | 469 | EXPORT_SYMBOL_GPL(gpiochip_line_is_valid); |
|---|
| 406 | 470 | |
|---|
| 407 | | -/* |
|---|
| 408 | | - * GPIO line handle management |
|---|
| 409 | | - */ |
|---|
| 410 | | - |
|---|
| 411 | | -/** |
|---|
| 412 | | - * struct linehandle_state - contains the state of a userspace handle |
|---|
| 413 | | - * @gdev: the GPIO device the handle pertains to |
|---|
| 414 | | - * @label: consumer label used to tag descriptors |
|---|
| 415 | | - * @descs: the GPIO descriptors held by this handle |
|---|
| 416 | | - * @numdescs: the number of descriptors held in the descs array |
|---|
| 417 | | - */ |
|---|
| 418 | | -struct linehandle_state { |
|---|
| 419 | | - struct gpio_device *gdev; |
|---|
| 420 | | - const char *label; |
|---|
| 421 | | - struct gpio_desc *descs[GPIOHANDLES_MAX]; |
|---|
| 422 | | - u32 numdescs; |
|---|
| 423 | | -}; |
|---|
| 424 | | - |
|---|
| 425 | | -#define GPIOHANDLE_REQUEST_VALID_FLAGS \ |
|---|
| 426 | | - (GPIOHANDLE_REQUEST_INPUT | \ |
|---|
| 427 | | - GPIOHANDLE_REQUEST_OUTPUT | \ |
|---|
| 428 | | - GPIOHANDLE_REQUEST_ACTIVE_LOW | \ |
|---|
| 429 | | - GPIOHANDLE_REQUEST_OPEN_DRAIN | \ |
|---|
| 430 | | - GPIOHANDLE_REQUEST_OPEN_SOURCE) |
|---|
| 431 | | - |
|---|
| 432 | | -static long linehandle_ioctl(struct file *filep, unsigned int cmd, |
|---|
| 433 | | - unsigned long arg) |
|---|
| 434 | | -{ |
|---|
| 435 | | - struct linehandle_state *lh = filep->private_data; |
|---|
| 436 | | - void __user *ip = (void __user *)arg; |
|---|
| 437 | | - struct gpiohandle_data ghd; |
|---|
| 438 | | - int vals[GPIOHANDLES_MAX]; |
|---|
| 439 | | - int i; |
|---|
| 440 | | - |
|---|
| 441 | | - if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
|---|
| 442 | | - /* NOTE: It's ok to read values of output lines. */ |
|---|
| 443 | | - int ret = gpiod_get_array_value_complex(false, |
|---|
| 444 | | - true, |
|---|
| 445 | | - lh->numdescs, |
|---|
| 446 | | - lh->descs, |
|---|
| 447 | | - vals); |
|---|
| 448 | | - if (ret) |
|---|
| 449 | | - return ret; |
|---|
| 450 | | - |
|---|
| 451 | | - memset(&ghd, 0, sizeof(ghd)); |
|---|
| 452 | | - for (i = 0; i < lh->numdescs; i++) |
|---|
| 453 | | - ghd.values[i] = vals[i]; |
|---|
| 454 | | - |
|---|
| 455 | | - if (copy_to_user(ip, &ghd, sizeof(ghd))) |
|---|
| 456 | | - return -EFAULT; |
|---|
| 457 | | - |
|---|
| 458 | | - return 0; |
|---|
| 459 | | - } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { |
|---|
| 460 | | - /* |
|---|
| 461 | | - * All line descriptors were created at once with the same |
|---|
| 462 | | - * flags so just check if the first one is really output. |
|---|
| 463 | | - */ |
|---|
| 464 | | - if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags)) |
|---|
| 465 | | - return -EPERM; |
|---|
| 466 | | - |
|---|
| 467 | | - if (copy_from_user(&ghd, ip, sizeof(ghd))) |
|---|
| 468 | | - return -EFAULT; |
|---|
| 469 | | - |
|---|
| 470 | | - /* Clamp all values to [0,1] */ |
|---|
| 471 | | - for (i = 0; i < lh->numdescs; i++) |
|---|
| 472 | | - vals[i] = !!ghd.values[i]; |
|---|
| 473 | | - |
|---|
| 474 | | - /* Reuse the array setting function */ |
|---|
| 475 | | - return gpiod_set_array_value_complex(false, |
|---|
| 476 | | - true, |
|---|
| 477 | | - lh->numdescs, |
|---|
| 478 | | - lh->descs, |
|---|
| 479 | | - vals); |
|---|
| 480 | | - } |
|---|
| 481 | | - return -EINVAL; |
|---|
| 482 | | -} |
|---|
| 483 | | - |
|---|
| 484 | | -#ifdef CONFIG_COMPAT |
|---|
| 485 | | -static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd, |
|---|
| 486 | | - unsigned long arg) |
|---|
| 487 | | -{ |
|---|
| 488 | | - return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); |
|---|
| 489 | | -} |
|---|
| 490 | | -#endif |
|---|
| 491 | | - |
|---|
| 492 | | -static int linehandle_release(struct inode *inode, struct file *filep) |
|---|
| 493 | | -{ |
|---|
| 494 | | - struct linehandle_state *lh = filep->private_data; |
|---|
| 495 | | - struct gpio_device *gdev = lh->gdev; |
|---|
| 496 | | - int i; |
|---|
| 497 | | - |
|---|
| 498 | | - for (i = 0; i < lh->numdescs; i++) |
|---|
| 499 | | - gpiod_free(lh->descs[i]); |
|---|
| 500 | | - kfree(lh->label); |
|---|
| 501 | | - kfree(lh); |
|---|
| 502 | | - put_device(&gdev->dev); |
|---|
| 503 | | - return 0; |
|---|
| 504 | | -} |
|---|
| 505 | | - |
|---|
| 506 | | -static const struct file_operations linehandle_fileops = { |
|---|
| 507 | | - .release = linehandle_release, |
|---|
| 508 | | - .owner = THIS_MODULE, |
|---|
| 509 | | - .llseek = noop_llseek, |
|---|
| 510 | | - .unlocked_ioctl = linehandle_ioctl, |
|---|
| 511 | | -#ifdef CONFIG_COMPAT |
|---|
| 512 | | - .compat_ioctl = linehandle_ioctl_compat, |
|---|
| 513 | | -#endif |
|---|
| 514 | | -}; |
|---|
| 515 | | - |
|---|
| 516 | | -static int linehandle_create(struct gpio_device *gdev, void __user *ip) |
|---|
| 517 | | -{ |
|---|
| 518 | | - struct gpiohandle_request handlereq; |
|---|
| 519 | | - struct linehandle_state *lh; |
|---|
| 520 | | - struct file *file; |
|---|
| 521 | | - int fd, i, count = 0, ret; |
|---|
| 522 | | - u32 lflags; |
|---|
| 523 | | - |
|---|
| 524 | | - if (copy_from_user(&handlereq, ip, sizeof(handlereq))) |
|---|
| 525 | | - return -EFAULT; |
|---|
| 526 | | - if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX)) |
|---|
| 527 | | - return -EINVAL; |
|---|
| 528 | | - |
|---|
| 529 | | - lflags = handlereq.flags; |
|---|
| 530 | | - |
|---|
| 531 | | - /* Return an error if an unknown flag is set */ |
|---|
| 532 | | - if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) |
|---|
| 533 | | - return -EINVAL; |
|---|
| 534 | | - |
|---|
| 535 | | - /* |
|---|
| 536 | | - * Do not allow both INPUT & OUTPUT flags to be set as they are |
|---|
| 537 | | - * contradictory. |
|---|
| 538 | | - */ |
|---|
| 539 | | - if ((lflags & GPIOHANDLE_REQUEST_INPUT) && |
|---|
| 540 | | - (lflags & GPIOHANDLE_REQUEST_OUTPUT)) |
|---|
| 541 | | - return -EINVAL; |
|---|
| 542 | | - |
|---|
| 543 | | - /* |
|---|
| 544 | | - * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If |
|---|
| 545 | | - * the hardware actually supports enabling both at the same time the |
|---|
| 546 | | - * electrical result would be disastrous. |
|---|
| 547 | | - */ |
|---|
| 548 | | - if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) && |
|---|
| 549 | | - (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) |
|---|
| 550 | | - return -EINVAL; |
|---|
| 551 | | - |
|---|
| 552 | | - /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */ |
|---|
| 553 | | - if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) && |
|---|
| 554 | | - ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
|---|
| 555 | | - (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))) |
|---|
| 556 | | - return -EINVAL; |
|---|
| 557 | | - |
|---|
| 558 | | - lh = kzalloc(sizeof(*lh), GFP_KERNEL); |
|---|
| 559 | | - if (!lh) |
|---|
| 560 | | - return -ENOMEM; |
|---|
| 561 | | - lh->gdev = gdev; |
|---|
| 562 | | - get_device(&gdev->dev); |
|---|
| 563 | | - |
|---|
| 564 | | - /* Make sure this is terminated */ |
|---|
| 565 | | - handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0'; |
|---|
| 566 | | - if (strlen(handlereq.consumer_label)) { |
|---|
| 567 | | - lh->label = kstrdup(handlereq.consumer_label, |
|---|
| 568 | | - GFP_KERNEL); |
|---|
| 569 | | - if (!lh->label) { |
|---|
| 570 | | - ret = -ENOMEM; |
|---|
| 571 | | - goto out_free_lh; |
|---|
| 572 | | - } |
|---|
| 573 | | - } |
|---|
| 574 | | - |
|---|
| 575 | | - /* Request each GPIO */ |
|---|
| 576 | | - for (i = 0; i < handlereq.lines; i++) { |
|---|
| 577 | | - u32 offset = handlereq.lineoffsets[i]; |
|---|
| 578 | | - struct gpio_desc *desc; |
|---|
| 579 | | - |
|---|
| 580 | | - if (offset >= gdev->ngpio) { |
|---|
| 581 | | - ret = -EINVAL; |
|---|
| 582 | | - goto out_free_descs; |
|---|
| 583 | | - } |
|---|
| 584 | | - |
|---|
| 585 | | - desc = &gdev->descs[offset]; |
|---|
| 586 | | - ret = gpiod_request(desc, lh->label); |
|---|
| 587 | | - if (ret) |
|---|
| 588 | | - goto out_free_descs; |
|---|
| 589 | | - lh->descs[i] = desc; |
|---|
| 590 | | - count = i + 1; |
|---|
| 591 | | - |
|---|
| 592 | | - if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) |
|---|
| 593 | | - set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
|---|
| 594 | | - if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) |
|---|
| 595 | | - set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
|---|
| 596 | | - if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE) |
|---|
| 597 | | - set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
|---|
| 598 | | - |
|---|
| 599 | | - ret = gpiod_set_transitory(desc, false); |
|---|
| 600 | | - if (ret < 0) |
|---|
| 601 | | - goto out_free_descs; |
|---|
| 602 | | - |
|---|
| 603 | | - /* |
|---|
| 604 | | - * Lines have to be requested explicitly for input |
|---|
| 605 | | - * or output, else the line will be treated "as is". |
|---|
| 606 | | - */ |
|---|
| 607 | | - if (lflags & GPIOHANDLE_REQUEST_OUTPUT) { |
|---|
| 608 | | - int val = !!handlereq.default_values[i]; |
|---|
| 609 | | - |
|---|
| 610 | | - ret = gpiod_direction_output(desc, val); |
|---|
| 611 | | - if (ret) |
|---|
| 612 | | - goto out_free_descs; |
|---|
| 613 | | - } else if (lflags & GPIOHANDLE_REQUEST_INPUT) { |
|---|
| 614 | | - ret = gpiod_direction_input(desc); |
|---|
| 615 | | - if (ret) |
|---|
| 616 | | - goto out_free_descs; |
|---|
| 617 | | - } |
|---|
| 618 | | - dev_dbg(&gdev->dev, "registered chardev handle for line %d\n", |
|---|
| 619 | | - offset); |
|---|
| 620 | | - } |
|---|
| 621 | | - /* Let i point at the last handle */ |
|---|
| 622 | | - i--; |
|---|
| 623 | | - lh->numdescs = handlereq.lines; |
|---|
| 624 | | - |
|---|
| 625 | | - fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
|---|
| 626 | | - if (fd < 0) { |
|---|
| 627 | | - ret = fd; |
|---|
| 628 | | - goto out_free_descs; |
|---|
| 629 | | - } |
|---|
| 630 | | - |
|---|
| 631 | | - file = anon_inode_getfile("gpio-linehandle", |
|---|
| 632 | | - &linehandle_fileops, |
|---|
| 633 | | - lh, |
|---|
| 634 | | - O_RDONLY | O_CLOEXEC); |
|---|
| 635 | | - if (IS_ERR(file)) { |
|---|
| 636 | | - ret = PTR_ERR(file); |
|---|
| 637 | | - goto out_put_unused_fd; |
|---|
| 638 | | - } |
|---|
| 639 | | - |
|---|
| 640 | | - handlereq.fd = fd; |
|---|
| 641 | | - if (copy_to_user(ip, &handlereq, sizeof(handlereq))) { |
|---|
| 642 | | - /* |
|---|
| 643 | | - * fput() will trigger the release() callback, so do not go onto |
|---|
| 644 | | - * the regular error cleanup path here. |
|---|
| 645 | | - */ |
|---|
| 646 | | - fput(file); |
|---|
| 647 | | - put_unused_fd(fd); |
|---|
| 648 | | - return -EFAULT; |
|---|
| 649 | | - } |
|---|
| 650 | | - |
|---|
| 651 | | - fd_install(fd, file); |
|---|
| 652 | | - |
|---|
| 653 | | - dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n", |
|---|
| 654 | | - lh->numdescs); |
|---|
| 655 | | - |
|---|
| 656 | | - return 0; |
|---|
| 657 | | - |
|---|
| 658 | | -out_put_unused_fd: |
|---|
| 659 | | - put_unused_fd(fd); |
|---|
| 660 | | -out_free_descs: |
|---|
| 661 | | - for (i = 0; i < count; i++) |
|---|
| 662 | | - gpiod_free(lh->descs[i]); |
|---|
| 663 | | - kfree(lh->label); |
|---|
| 664 | | -out_free_lh: |
|---|
| 665 | | - kfree(lh); |
|---|
| 666 | | - put_device(&gdev->dev); |
|---|
| 667 | | - return ret; |
|---|
| 668 | | -} |
|---|
| 669 | | - |
|---|
| 670 | | -/* |
|---|
| 671 | | - * GPIO line event management |
|---|
| 672 | | - */ |
|---|
| 673 | | - |
|---|
| 674 | | -/** |
|---|
| 675 | | - * struct lineevent_state - contains the state of a userspace event |
|---|
| 676 | | - * @gdev: the GPIO device the event pertains to |
|---|
| 677 | | - * @label: consumer label used to tag descriptors |
|---|
| 678 | | - * @desc: the GPIO descriptor held by this event |
|---|
| 679 | | - * @eflags: the event flags this line was requested with |
|---|
| 680 | | - * @irq: the interrupt that trigger in response to events on this GPIO |
|---|
| 681 | | - * @wait: wait queue that handles blocking reads of events |
|---|
| 682 | | - * @events: KFIFO for the GPIO events |
|---|
| 683 | | - * @read_lock: mutex lock to protect reads from colliding with adding |
|---|
| 684 | | - * new events to the FIFO |
|---|
| 685 | | - * @timestamp: cache for the timestamp storing it between hardirq |
|---|
| 686 | | - * and IRQ thread, used to bring the timestamp close to the actual |
|---|
| 687 | | - * event |
|---|
| 688 | | - */ |
|---|
| 689 | | -struct lineevent_state { |
|---|
| 690 | | - struct gpio_device *gdev; |
|---|
| 691 | | - const char *label; |
|---|
| 692 | | - struct gpio_desc *desc; |
|---|
| 693 | | - u32 eflags; |
|---|
| 694 | | - int irq; |
|---|
| 695 | | - wait_queue_head_t wait; |
|---|
| 696 | | - DECLARE_KFIFO(events, struct gpioevent_data, 16); |
|---|
| 697 | | - struct mutex read_lock; |
|---|
| 698 | | - u64 timestamp; |
|---|
| 699 | | -}; |
|---|
| 700 | | - |
|---|
| 701 | | -#define GPIOEVENT_REQUEST_VALID_FLAGS \ |
|---|
| 702 | | - (GPIOEVENT_REQUEST_RISING_EDGE | \ |
|---|
| 703 | | - GPIOEVENT_REQUEST_FALLING_EDGE) |
|---|
| 704 | | - |
|---|
| 705 | | -static __poll_t lineevent_poll(struct file *filep, |
|---|
| 706 | | - struct poll_table_struct *wait) |
|---|
| 707 | | -{ |
|---|
| 708 | | - struct lineevent_state *le = filep->private_data; |
|---|
| 709 | | - __poll_t events = 0; |
|---|
| 710 | | - |
|---|
| 711 | | - poll_wait(filep, &le->wait, wait); |
|---|
| 712 | | - |
|---|
| 713 | | - if (!kfifo_is_empty(&le->events)) |
|---|
| 714 | | - events = EPOLLIN | EPOLLRDNORM; |
|---|
| 715 | | - |
|---|
| 716 | | - return events; |
|---|
| 717 | | -} |
|---|
| 718 | | - |
|---|
| 719 | | - |
|---|
| 720 | | -static ssize_t lineevent_read(struct file *filep, |
|---|
| 721 | | - char __user *buf, |
|---|
| 722 | | - size_t count, |
|---|
| 723 | | - loff_t *f_ps) |
|---|
| 724 | | -{ |
|---|
| 725 | | - struct lineevent_state *le = filep->private_data; |
|---|
| 726 | | - unsigned int copied; |
|---|
| 727 | | - int ret; |
|---|
| 728 | | - |
|---|
| 729 | | - if (count < sizeof(struct gpioevent_data)) |
|---|
| 730 | | - return -EINVAL; |
|---|
| 731 | | - |
|---|
| 732 | | - do { |
|---|
| 733 | | - if (kfifo_is_empty(&le->events)) { |
|---|
| 734 | | - if (filep->f_flags & O_NONBLOCK) |
|---|
| 735 | | - return -EAGAIN; |
|---|
| 736 | | - |
|---|
| 737 | | - ret = wait_event_interruptible(le->wait, |
|---|
| 738 | | - !kfifo_is_empty(&le->events)); |
|---|
| 739 | | - if (ret) |
|---|
| 740 | | - return ret; |
|---|
| 741 | | - } |
|---|
| 742 | | - |
|---|
| 743 | | - if (mutex_lock_interruptible(&le->read_lock)) |
|---|
| 744 | | - return -ERESTARTSYS; |
|---|
| 745 | | - ret = kfifo_to_user(&le->events, buf, count, &copied); |
|---|
| 746 | | - mutex_unlock(&le->read_lock); |
|---|
| 747 | | - |
|---|
| 748 | | - if (ret) |
|---|
| 749 | | - return ret; |
|---|
| 750 | | - |
|---|
| 751 | | - /* |
|---|
| 752 | | - * If we couldn't read anything from the fifo (a different |
|---|
| 753 | | - * thread might have been faster) we either return -EAGAIN if |
|---|
| 754 | | - * the file descriptor is non-blocking, otherwise we go back to |
|---|
| 755 | | - * sleep and wait for more data to arrive. |
|---|
| 756 | | - */ |
|---|
| 757 | | - if (copied == 0 && (filep->f_flags & O_NONBLOCK)) |
|---|
| 758 | | - return -EAGAIN; |
|---|
| 759 | | - |
|---|
| 760 | | - } while (copied == 0); |
|---|
| 761 | | - |
|---|
| 762 | | - return copied; |
|---|
| 763 | | -} |
|---|
| 764 | | - |
|---|
| 765 | | -static int lineevent_release(struct inode *inode, struct file *filep) |
|---|
| 766 | | -{ |
|---|
| 767 | | - struct lineevent_state *le = filep->private_data; |
|---|
| 768 | | - struct gpio_device *gdev = le->gdev; |
|---|
| 769 | | - |
|---|
| 770 | | - free_irq(le->irq, le); |
|---|
| 771 | | - gpiod_free(le->desc); |
|---|
| 772 | | - kfree(le->label); |
|---|
| 773 | | - kfree(le); |
|---|
| 774 | | - put_device(&gdev->dev); |
|---|
| 775 | | - return 0; |
|---|
| 776 | | -} |
|---|
| 777 | | - |
|---|
| 778 | | -static long lineevent_ioctl(struct file *filep, unsigned int cmd, |
|---|
| 779 | | - unsigned long arg) |
|---|
| 780 | | -{ |
|---|
| 781 | | - struct lineevent_state *le = filep->private_data; |
|---|
| 782 | | - void __user *ip = (void __user *)arg; |
|---|
| 783 | | - struct gpiohandle_data ghd; |
|---|
| 784 | | - |
|---|
| 785 | | - /* |
|---|
| 786 | | - * We can get the value for an event line but not set it, |
|---|
| 787 | | - * because it is input by definition. |
|---|
| 788 | | - */ |
|---|
| 789 | | - if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
|---|
| 790 | | - int val; |
|---|
| 791 | | - |
|---|
| 792 | | - memset(&ghd, 0, sizeof(ghd)); |
|---|
| 793 | | - |
|---|
| 794 | | - val = gpiod_get_value_cansleep(le->desc); |
|---|
| 795 | | - if (val < 0) |
|---|
| 796 | | - return val; |
|---|
| 797 | | - ghd.values[0] = val; |
|---|
| 798 | | - |
|---|
| 799 | | - if (copy_to_user(ip, &ghd, sizeof(ghd))) |
|---|
| 800 | | - return -EFAULT; |
|---|
| 801 | | - |
|---|
| 802 | | - return 0; |
|---|
| 803 | | - } |
|---|
| 804 | | - return -EINVAL; |
|---|
| 805 | | -} |
|---|
| 806 | | - |
|---|
| 807 | | -#ifdef CONFIG_COMPAT |
|---|
| 808 | | -static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd, |
|---|
| 809 | | - unsigned long arg) |
|---|
| 810 | | -{ |
|---|
| 811 | | - return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg)); |
|---|
| 812 | | -} |
|---|
| 813 | | -#endif |
|---|
| 814 | | - |
|---|
| 815 | | -static const struct file_operations lineevent_fileops = { |
|---|
| 816 | | - .release = lineevent_release, |
|---|
| 817 | | - .read = lineevent_read, |
|---|
| 818 | | - .poll = lineevent_poll, |
|---|
| 819 | | - .owner = THIS_MODULE, |
|---|
| 820 | | - .llseek = noop_llseek, |
|---|
| 821 | | - .unlocked_ioctl = lineevent_ioctl, |
|---|
| 822 | | -#ifdef CONFIG_COMPAT |
|---|
| 823 | | - .compat_ioctl = lineevent_ioctl_compat, |
|---|
| 824 | | -#endif |
|---|
| 825 | | -}; |
|---|
| 826 | | - |
|---|
| 827 | | -static irqreturn_t lineevent_irq_thread(int irq, void *p) |
|---|
| 828 | | -{ |
|---|
| 829 | | - struct lineevent_state *le = p; |
|---|
| 830 | | - struct gpioevent_data ge; |
|---|
| 831 | | - int ret, level; |
|---|
| 832 | | - |
|---|
| 833 | | - /* Do not leak kernel stack to userspace */ |
|---|
| 834 | | - memset(&ge, 0, sizeof(ge)); |
|---|
| 835 | | - |
|---|
| 836 | | - /* |
|---|
| 837 | | - * We may be running from a nested threaded interrupt in which case |
|---|
| 838 | | - * we didn't get the timestamp from lineevent_irq_handler(). |
|---|
| 839 | | - */ |
|---|
| 840 | | - if (!le->timestamp) |
|---|
| 841 | | - ge.timestamp = ktime_get_real_ns(); |
|---|
| 842 | | - else |
|---|
| 843 | | - ge.timestamp = le->timestamp; |
|---|
| 844 | | - |
|---|
| 845 | | - level = gpiod_get_value_cansleep(le->desc); |
|---|
| 846 | | - |
|---|
| 847 | | - if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE |
|---|
| 848 | | - && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) { |
|---|
| 849 | | - if (level) |
|---|
| 850 | | - /* Emit low-to-high event */ |
|---|
| 851 | | - ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
|---|
| 852 | | - else |
|---|
| 853 | | - /* Emit high-to-low event */ |
|---|
| 854 | | - ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
|---|
| 855 | | - } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) { |
|---|
| 856 | | - /* Emit low-to-high event */ |
|---|
| 857 | | - ge.id = GPIOEVENT_EVENT_RISING_EDGE; |
|---|
| 858 | | - } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) { |
|---|
| 859 | | - /* Emit high-to-low event */ |
|---|
| 860 | | - ge.id = GPIOEVENT_EVENT_FALLING_EDGE; |
|---|
| 861 | | - } else { |
|---|
| 862 | | - return IRQ_NONE; |
|---|
| 863 | | - } |
|---|
| 864 | | - |
|---|
| 865 | | - ret = kfifo_put(&le->events, ge); |
|---|
| 866 | | - if (ret != 0) |
|---|
| 867 | | - wake_up_poll(&le->wait, EPOLLIN); |
|---|
| 868 | | - |
|---|
| 869 | | - return IRQ_HANDLED; |
|---|
| 870 | | -} |
|---|
| 871 | | - |
|---|
| 872 | | -static irqreturn_t lineevent_irq_handler(int irq, void *p) |
|---|
| 873 | | -{ |
|---|
| 874 | | - struct lineevent_state *le = p; |
|---|
| 875 | | - |
|---|
| 876 | | - /* |
|---|
| 877 | | - * Just store the timestamp in hardirq context so we get it as |
|---|
| 878 | | - * close in time as possible to the actual event. |
|---|
| 879 | | - */ |
|---|
| 880 | | - le->timestamp = ktime_get_real_ns(); |
|---|
| 881 | | - |
|---|
| 882 | | - return IRQ_WAKE_THREAD; |
|---|
| 883 | | -} |
|---|
| 884 | | - |
|---|
| 885 | | -static int lineevent_create(struct gpio_device *gdev, void __user *ip) |
|---|
| 886 | | -{ |
|---|
| 887 | | - struct gpioevent_request eventreq; |
|---|
| 888 | | - struct lineevent_state *le; |
|---|
| 889 | | - struct gpio_desc *desc; |
|---|
| 890 | | - struct file *file; |
|---|
| 891 | | - u32 offset; |
|---|
| 892 | | - u32 lflags; |
|---|
| 893 | | - u32 eflags; |
|---|
| 894 | | - int fd; |
|---|
| 895 | | - int ret; |
|---|
| 896 | | - int irqflags = 0; |
|---|
| 897 | | - |
|---|
| 898 | | - if (copy_from_user(&eventreq, ip, sizeof(eventreq))) |
|---|
| 899 | | - return -EFAULT; |
|---|
| 900 | | - |
|---|
| 901 | | - le = kzalloc(sizeof(*le), GFP_KERNEL); |
|---|
| 902 | | - if (!le) |
|---|
| 903 | | - return -ENOMEM; |
|---|
| 904 | | - le->gdev = gdev; |
|---|
| 905 | | - get_device(&gdev->dev); |
|---|
| 906 | | - |
|---|
| 907 | | - /* Make sure this is terminated */ |
|---|
| 908 | | - eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0'; |
|---|
| 909 | | - if (strlen(eventreq.consumer_label)) { |
|---|
| 910 | | - le->label = kstrdup(eventreq.consumer_label, |
|---|
| 911 | | - GFP_KERNEL); |
|---|
| 912 | | - if (!le->label) { |
|---|
| 913 | | - ret = -ENOMEM; |
|---|
| 914 | | - goto out_free_le; |
|---|
| 915 | | - } |
|---|
| 916 | | - } |
|---|
| 917 | | - |
|---|
| 918 | | - offset = eventreq.lineoffset; |
|---|
| 919 | | - lflags = eventreq.handleflags; |
|---|
| 920 | | - eflags = eventreq.eventflags; |
|---|
| 921 | | - |
|---|
| 922 | | - if (offset >= gdev->ngpio) { |
|---|
| 923 | | - ret = -EINVAL; |
|---|
| 924 | | - goto out_free_label; |
|---|
| 925 | | - } |
|---|
| 926 | | - |
|---|
| 927 | | - /* Return an error if a unknown flag is set */ |
|---|
| 928 | | - if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) || |
|---|
| 929 | | - (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) { |
|---|
| 930 | | - ret = -EINVAL; |
|---|
| 931 | | - goto out_free_label; |
|---|
| 932 | | - } |
|---|
| 933 | | - |
|---|
| 934 | | - /* This is just wrong: we don't look for events on output lines */ |
|---|
| 935 | | - if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) || |
|---|
| 936 | | - (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) || |
|---|
| 937 | | - (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) { |
|---|
| 938 | | - ret = -EINVAL; |
|---|
| 939 | | - goto out_free_label; |
|---|
| 940 | | - } |
|---|
| 941 | | - |
|---|
| 942 | | - desc = &gdev->descs[offset]; |
|---|
| 943 | | - ret = gpiod_request(desc, le->label); |
|---|
| 944 | | - if (ret) |
|---|
| 945 | | - goto out_free_label; |
|---|
| 946 | | - le->desc = desc; |
|---|
| 947 | | - le->eflags = eflags; |
|---|
| 948 | | - |
|---|
| 949 | | - if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW) |
|---|
| 950 | | - set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
|---|
| 951 | | - |
|---|
| 952 | | - ret = gpiod_direction_input(desc); |
|---|
| 953 | | - if (ret) |
|---|
| 954 | | - goto out_free_desc; |
|---|
| 955 | | - |
|---|
| 956 | | - le->irq = gpiod_to_irq(desc); |
|---|
| 957 | | - if (le->irq <= 0) { |
|---|
| 958 | | - ret = -ENODEV; |
|---|
| 959 | | - goto out_free_desc; |
|---|
| 960 | | - } |
|---|
| 961 | | - |
|---|
| 962 | | - if (eflags & GPIOEVENT_REQUEST_RISING_EDGE) |
|---|
| 963 | | - irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
|---|
| 964 | | - IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; |
|---|
| 965 | | - if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE) |
|---|
| 966 | | - irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
|---|
| 967 | | - IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; |
|---|
| 968 | | - irqflags |= IRQF_ONESHOT; |
|---|
| 969 | | - irqflags |= IRQF_SHARED; |
|---|
| 970 | | - |
|---|
| 971 | | - INIT_KFIFO(le->events); |
|---|
| 972 | | - init_waitqueue_head(&le->wait); |
|---|
| 973 | | - mutex_init(&le->read_lock); |
|---|
| 974 | | - |
|---|
| 975 | | - /* Request a thread to read the events */ |
|---|
| 976 | | - ret = request_threaded_irq(le->irq, |
|---|
| 977 | | - lineevent_irq_handler, |
|---|
| 978 | | - lineevent_irq_thread, |
|---|
| 979 | | - irqflags, |
|---|
| 980 | | - le->label, |
|---|
| 981 | | - le); |
|---|
| 982 | | - if (ret) |
|---|
| 983 | | - goto out_free_desc; |
|---|
| 984 | | - |
|---|
| 985 | | - fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC); |
|---|
| 986 | | - if (fd < 0) { |
|---|
| 987 | | - ret = fd; |
|---|
| 988 | | - goto out_free_irq; |
|---|
| 989 | | - } |
|---|
| 990 | | - |
|---|
| 991 | | - file = anon_inode_getfile("gpio-event", |
|---|
| 992 | | - &lineevent_fileops, |
|---|
| 993 | | - le, |
|---|
| 994 | | - O_RDONLY | O_CLOEXEC); |
|---|
| 995 | | - if (IS_ERR(file)) { |
|---|
| 996 | | - ret = PTR_ERR(file); |
|---|
| 997 | | - goto out_put_unused_fd; |
|---|
| 998 | | - } |
|---|
| 999 | | - |
|---|
| 1000 | | - eventreq.fd = fd; |
|---|
| 1001 | | - if (copy_to_user(ip, &eventreq, sizeof(eventreq))) { |
|---|
| 1002 | | - /* |
|---|
| 1003 | | - * fput() will trigger the release() callback, so do not go onto |
|---|
| 1004 | | - * the regular error cleanup path here. |
|---|
| 1005 | | - */ |
|---|
| 1006 | | - fput(file); |
|---|
| 1007 | | - put_unused_fd(fd); |
|---|
| 1008 | | - return -EFAULT; |
|---|
| 1009 | | - } |
|---|
| 1010 | | - |
|---|
| 1011 | | - fd_install(fd, file); |
|---|
| 1012 | | - |
|---|
| 1013 | | - return 0; |
|---|
| 1014 | | - |
|---|
| 1015 | | -out_put_unused_fd: |
|---|
| 1016 | | - put_unused_fd(fd); |
|---|
| 1017 | | -out_free_irq: |
|---|
| 1018 | | - free_irq(le->irq, le); |
|---|
| 1019 | | -out_free_desc: |
|---|
| 1020 | | - gpiod_free(le->desc); |
|---|
| 1021 | | -out_free_label: |
|---|
| 1022 | | - kfree(le->label); |
|---|
| 1023 | | -out_free_le: |
|---|
| 1024 | | - kfree(le); |
|---|
| 1025 | | - put_device(&gdev->dev); |
|---|
| 1026 | | - return ret; |
|---|
| 1027 | | -} |
|---|
| 1028 | | - |
|---|
| 1029 | | -/* |
|---|
| 1030 | | - * gpio_ioctl() - ioctl handler for the GPIO chardev |
|---|
| 1031 | | - */ |
|---|
| 1032 | | -static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
|---|
| 1033 | | -{ |
|---|
| 1034 | | - struct gpio_device *gdev = filp->private_data; |
|---|
| 1035 | | - struct gpio_chip *chip = gdev->chip; |
|---|
| 1036 | | - void __user *ip = (void __user *)arg; |
|---|
| 1037 | | - |
|---|
| 1038 | | - /* We fail any subsequent ioctl():s when the chip is gone */ |
|---|
| 1039 | | - if (!chip) |
|---|
| 1040 | | - return -ENODEV; |
|---|
| 1041 | | - |
|---|
| 1042 | | - /* Fill in the struct and pass to userspace */ |
|---|
| 1043 | | - if (cmd == GPIO_GET_CHIPINFO_IOCTL) { |
|---|
| 1044 | | - struct gpiochip_info chipinfo; |
|---|
| 1045 | | - |
|---|
| 1046 | | - memset(&chipinfo, 0, sizeof(chipinfo)); |
|---|
| 1047 | | - |
|---|
| 1048 | | - strncpy(chipinfo.name, dev_name(&gdev->dev), |
|---|
| 1049 | | - sizeof(chipinfo.name)); |
|---|
| 1050 | | - chipinfo.name[sizeof(chipinfo.name)-1] = '\0'; |
|---|
| 1051 | | - strncpy(chipinfo.label, gdev->label, |
|---|
| 1052 | | - sizeof(chipinfo.label)); |
|---|
| 1053 | | - chipinfo.label[sizeof(chipinfo.label)-1] = '\0'; |
|---|
| 1054 | | - chipinfo.lines = gdev->ngpio; |
|---|
| 1055 | | - if (copy_to_user(ip, &chipinfo, sizeof(chipinfo))) |
|---|
| 1056 | | - return -EFAULT; |
|---|
| 1057 | | - return 0; |
|---|
| 1058 | | - } else if (cmd == GPIO_GET_LINEINFO_IOCTL) { |
|---|
| 1059 | | - struct gpioline_info lineinfo; |
|---|
| 1060 | | - struct gpio_desc *desc; |
|---|
| 1061 | | - |
|---|
| 1062 | | - if (copy_from_user(&lineinfo, ip, sizeof(lineinfo))) |
|---|
| 1063 | | - return -EFAULT; |
|---|
| 1064 | | - if (lineinfo.line_offset >= gdev->ngpio) |
|---|
| 1065 | | - return -EINVAL; |
|---|
| 1066 | | - |
|---|
| 1067 | | - desc = &gdev->descs[lineinfo.line_offset]; |
|---|
| 1068 | | - if (desc->name) { |
|---|
| 1069 | | - strncpy(lineinfo.name, desc->name, |
|---|
| 1070 | | - sizeof(lineinfo.name)); |
|---|
| 1071 | | - lineinfo.name[sizeof(lineinfo.name)-1] = '\0'; |
|---|
| 1072 | | - } else { |
|---|
| 1073 | | - lineinfo.name[0] = '\0'; |
|---|
| 1074 | | - } |
|---|
| 1075 | | - if (desc->label) { |
|---|
| 1076 | | - strncpy(lineinfo.consumer, desc->label, |
|---|
| 1077 | | - sizeof(lineinfo.consumer)); |
|---|
| 1078 | | - lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0'; |
|---|
| 1079 | | - } else { |
|---|
| 1080 | | - lineinfo.consumer[0] = '\0'; |
|---|
| 1081 | | - } |
|---|
| 1082 | | - |
|---|
| 1083 | | - /* |
|---|
| 1084 | | - * Userspace only need to know that the kernel is using |
|---|
| 1085 | | - * this GPIO so it can't use it. |
|---|
| 1086 | | - */ |
|---|
| 1087 | | - lineinfo.flags = 0; |
|---|
| 1088 | | - if (test_bit(FLAG_REQUESTED, &desc->flags) || |
|---|
| 1089 | | - test_bit(FLAG_IS_HOGGED, &desc->flags) || |
|---|
| 1090 | | - test_bit(FLAG_USED_AS_IRQ, &desc->flags) || |
|---|
| 1091 | | - test_bit(FLAG_EXPORT, &desc->flags) || |
|---|
| 1092 | | - test_bit(FLAG_SYSFS, &desc->flags)) |
|---|
| 1093 | | - lineinfo.flags |= GPIOLINE_FLAG_KERNEL; |
|---|
| 1094 | | - if (test_bit(FLAG_IS_OUT, &desc->flags)) |
|---|
| 1095 | | - lineinfo.flags |= GPIOLINE_FLAG_IS_OUT; |
|---|
| 1096 | | - if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
|---|
| 1097 | | - lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW; |
|---|
| 1098 | | - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) |
|---|
| 1099 | | - lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN | |
|---|
| 1100 | | - GPIOLINE_FLAG_IS_OUT); |
|---|
| 1101 | | - if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) |
|---|
| 1102 | | - lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE | |
|---|
| 1103 | | - GPIOLINE_FLAG_IS_OUT); |
|---|
| 1104 | | - |
|---|
| 1105 | | - if (copy_to_user(ip, &lineinfo, sizeof(lineinfo))) |
|---|
| 1106 | | - return -EFAULT; |
|---|
| 1107 | | - return 0; |
|---|
| 1108 | | - } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) { |
|---|
| 1109 | | - return linehandle_create(gdev, ip); |
|---|
| 1110 | | - } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) { |
|---|
| 1111 | | - return lineevent_create(gdev, ip); |
|---|
| 1112 | | - } |
|---|
| 1113 | | - return -EINVAL; |
|---|
| 1114 | | -} |
|---|
| 1115 | | - |
|---|
| 1116 | | -#ifdef CONFIG_COMPAT |
|---|
| 1117 | | -static long gpio_ioctl_compat(struct file *filp, unsigned int cmd, |
|---|
| 1118 | | - unsigned long arg) |
|---|
| 1119 | | -{ |
|---|
| 1120 | | - return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
|---|
| 1121 | | -} |
|---|
| 1122 | | -#endif |
|---|
| 1123 | | - |
|---|
| 1124 | | -/** |
|---|
| 1125 | | - * gpio_chrdev_open() - open the chardev for ioctl operations |
|---|
| 1126 | | - * @inode: inode for this chardev |
|---|
| 1127 | | - * @filp: file struct for storing private data |
|---|
| 1128 | | - * Returns 0 on success |
|---|
| 1129 | | - */ |
|---|
| 1130 | | -static int gpio_chrdev_open(struct inode *inode, struct file *filp) |
|---|
| 1131 | | -{ |
|---|
| 1132 | | - struct gpio_device *gdev = container_of(inode->i_cdev, |
|---|
| 1133 | | - struct gpio_device, chrdev); |
|---|
| 1134 | | - |
|---|
| 1135 | | - /* Fail on open if the backing gpiochip is gone */ |
|---|
| 1136 | | - if (!gdev->chip) |
|---|
| 1137 | | - return -ENODEV; |
|---|
| 1138 | | - get_device(&gdev->dev); |
|---|
| 1139 | | - filp->private_data = gdev; |
|---|
| 1140 | | - |
|---|
| 1141 | | - return nonseekable_open(inode, filp); |
|---|
| 1142 | | -} |
|---|
| 1143 | | - |
|---|
| 1144 | | -/** |
|---|
| 1145 | | - * gpio_chrdev_release() - close chardev after ioctl operations |
|---|
| 1146 | | - * @inode: inode for this chardev |
|---|
| 1147 | | - * @filp: file struct for storing private data |
|---|
| 1148 | | - * Returns 0 on success |
|---|
| 1149 | | - */ |
|---|
| 1150 | | -static int gpio_chrdev_release(struct inode *inode, struct file *filp) |
|---|
| 1151 | | -{ |
|---|
| 1152 | | - struct gpio_device *gdev = container_of(inode->i_cdev, |
|---|
| 1153 | | - struct gpio_device, chrdev); |
|---|
| 1154 | | - |
|---|
| 1155 | | - put_device(&gdev->dev); |
|---|
| 1156 | | - return 0; |
|---|
| 1157 | | -} |
|---|
| 1158 | | - |
|---|
| 1159 | | - |
|---|
| 1160 | | -static const struct file_operations gpio_fileops = { |
|---|
| 1161 | | - .release = gpio_chrdev_release, |
|---|
| 1162 | | - .open = gpio_chrdev_open, |
|---|
| 1163 | | - .owner = THIS_MODULE, |
|---|
| 1164 | | - .llseek = no_llseek, |
|---|
| 1165 | | - .unlocked_ioctl = gpio_ioctl, |
|---|
| 1166 | | -#ifdef CONFIG_COMPAT |
|---|
| 1167 | | - .compat_ioctl = gpio_ioctl_compat, |
|---|
| 1168 | | -#endif |
|---|
| 1169 | | -}; |
|---|
| 1170 | | - |
|---|
| 1171 | 471 | static void gpiodevice_release(struct device *dev) |
|---|
| 1172 | 472 | { |
|---|
| 1173 | | - struct gpio_device *gdev = dev_get_drvdata(dev); |
|---|
| 473 | + struct gpio_device *gdev = container_of(dev, struct gpio_device, dev); |
|---|
| 474 | + unsigned long flags; |
|---|
| 1174 | 475 | |
|---|
| 476 | + spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 1175 | 477 | list_del(&gdev->list); |
|---|
| 1176 | | - ida_simple_remove(&gpio_ida, gdev->id); |
|---|
| 478 | + spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 479 | + |
|---|
| 480 | + ida_free(&gpio_ida, gdev->id); |
|---|
| 1177 | 481 | kfree_const(gdev->label); |
|---|
| 1178 | 482 | kfree(gdev->descs); |
|---|
| 1179 | 483 | kfree(gdev); |
|---|
| 1180 | 484 | } |
|---|
| 1181 | 485 | |
|---|
| 486 | +#ifdef CONFIG_GPIO_CDEV |
|---|
| 487 | +#define gcdev_register(gdev, devt) gpiolib_cdev_register((gdev), (devt)) |
|---|
| 488 | +#define gcdev_unregister(gdev) gpiolib_cdev_unregister((gdev)) |
|---|
| 489 | +#else |
|---|
| 490 | +/* |
|---|
| 491 | + * gpiolib_cdev_register() indirectly calls device_add(), which is still |
|---|
| 492 | + * required even when cdev is not selected. |
|---|
| 493 | + */ |
|---|
| 494 | +#define gcdev_register(gdev, devt) device_add(&(gdev)->dev) |
|---|
| 495 | +#define gcdev_unregister(gdev) device_del(&(gdev)->dev) |
|---|
| 496 | +#endif |
|---|
| 497 | + |
|---|
| 1182 | 498 | static int gpiochip_setup_dev(struct gpio_device *gdev) |
|---|
| 1183 | 499 | { |
|---|
| 1184 | | - int status; |
|---|
| 500 | + int ret; |
|---|
| 1185 | 501 | |
|---|
| 1186 | | - cdev_init(&gdev->chrdev, &gpio_fileops); |
|---|
| 1187 | | - gdev->chrdev.owner = THIS_MODULE; |
|---|
| 1188 | | - gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id); |
|---|
| 502 | + ret = gcdev_register(gdev, gpio_devt); |
|---|
| 503 | + if (ret) |
|---|
| 504 | + return ret; |
|---|
| 1189 | 505 | |
|---|
| 1190 | | - status = cdev_device_add(&gdev->chrdev, &gdev->dev); |
|---|
| 1191 | | - if (status) |
|---|
| 1192 | | - return status; |
|---|
| 1193 | | - |
|---|
| 1194 | | - chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n", |
|---|
| 1195 | | - MAJOR(gpio_devt), gdev->id); |
|---|
| 1196 | | - |
|---|
| 1197 | | - status = gpiochip_sysfs_register(gdev); |
|---|
| 1198 | | - if (status) |
|---|
| 506 | + ret = gpiochip_sysfs_register(gdev); |
|---|
| 507 | + if (ret) |
|---|
| 1199 | 508 | goto err_remove_device; |
|---|
| 1200 | 509 | |
|---|
| 1201 | 510 | /* From this point, the .release() function cleans up gpio_device */ |
|---|
| 1202 | 511 | gdev->dev.release = gpiodevice_release; |
|---|
| 1203 | | - pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n", |
|---|
| 1204 | | - __func__, gdev->base, gdev->base + gdev->ngpio - 1, |
|---|
| 1205 | | - dev_name(&gdev->dev), gdev->chip->label ? : "generic"); |
|---|
| 512 | + dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, |
|---|
| 513 | + gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic"); |
|---|
| 1206 | 514 | |
|---|
| 1207 | 515 | return 0; |
|---|
| 1208 | 516 | |
|---|
| 1209 | 517 | err_remove_device: |
|---|
| 1210 | | - cdev_device_del(&gdev->chrdev, &gdev->dev); |
|---|
| 1211 | | - return status; |
|---|
| 518 | + gcdev_unregister(gdev); |
|---|
| 519 | + return ret; |
|---|
| 1212 | 520 | } |
|---|
| 1213 | 521 | |
|---|
| 1214 | | -static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog) |
|---|
| 522 | +static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog) |
|---|
| 1215 | 523 | { |
|---|
| 1216 | 524 | struct gpio_desc *desc; |
|---|
| 1217 | 525 | int rv; |
|---|
| 1218 | 526 | |
|---|
| 1219 | | - desc = gpiochip_get_desc(chip, hog->chip_hwnum); |
|---|
| 527 | + desc = gpiochip_get_desc(gc, hog->chip_hwnum); |
|---|
| 1220 | 528 | if (IS_ERR(desc)) { |
|---|
| 1221 | | - pr_err("%s: unable to get GPIO desc: %ld\n", |
|---|
| 1222 | | - __func__, PTR_ERR(desc)); |
|---|
| 529 | + chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__, |
|---|
| 530 | + PTR_ERR(desc)); |
|---|
| 1223 | 531 | return; |
|---|
| 1224 | 532 | } |
|---|
| 1225 | 533 | |
|---|
| .. | .. |
|---|
| 1228 | 536 | |
|---|
| 1229 | 537 | rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); |
|---|
| 1230 | 538 | if (rv) |
|---|
| 1231 | | - pr_err("%s: unable to hog GPIO line (%s:%u): %d\n", |
|---|
| 1232 | | - __func__, chip->label, hog->chip_hwnum, rv); |
|---|
| 539 | + gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n", |
|---|
| 540 | + __func__, gc->label, hog->chip_hwnum, rv); |
|---|
| 1233 | 541 | } |
|---|
| 1234 | 542 | |
|---|
| 1235 | | -static void machine_gpiochip_add(struct gpio_chip *chip) |
|---|
| 543 | +static void machine_gpiochip_add(struct gpio_chip *gc) |
|---|
| 1236 | 544 | { |
|---|
| 1237 | 545 | struct gpiod_hog *hog; |
|---|
| 1238 | 546 | |
|---|
| 1239 | 547 | mutex_lock(&gpio_machine_hogs_mutex); |
|---|
| 1240 | 548 | |
|---|
| 1241 | 549 | list_for_each_entry(hog, &gpio_machine_hogs, list) { |
|---|
| 1242 | | - if (!strcmp(chip->label, hog->chip_label)) |
|---|
| 1243 | | - gpiochip_machine_hog(chip, hog); |
|---|
| 550 | + if (!strcmp(gc->label, hog->chip_label)) |
|---|
| 551 | + gpiochip_machine_hog(gc, hog); |
|---|
| 1244 | 552 | } |
|---|
| 1245 | 553 | |
|---|
| 1246 | 554 | mutex_unlock(&gpio_machine_hogs_mutex); |
|---|
| .. | .. |
|---|
| 1249 | 557 | static void gpiochip_setup_devs(void) |
|---|
| 1250 | 558 | { |
|---|
| 1251 | 559 | struct gpio_device *gdev; |
|---|
| 1252 | | - int err; |
|---|
| 560 | + int ret; |
|---|
| 1253 | 561 | |
|---|
| 1254 | 562 | list_for_each_entry(gdev, &gpio_devices, list) { |
|---|
| 1255 | | - err = gpiochip_setup_dev(gdev); |
|---|
| 1256 | | - if (err) |
|---|
| 1257 | | - pr_err("%s: Failed to initialize gpio device (%d)\n", |
|---|
| 1258 | | - dev_name(&gdev->dev), err); |
|---|
| 563 | + ret = gpiochip_setup_dev(gdev); |
|---|
| 564 | + if (ret) |
|---|
| 565 | + dev_err(&gdev->dev, |
|---|
| 566 | + "Failed to initialize gpio device (%d)\n", ret); |
|---|
| 1259 | 567 | } |
|---|
| 1260 | 568 | } |
|---|
| 1261 | 569 | |
|---|
| 1262 | | -int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, |
|---|
| 570 | +int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, |
|---|
| 1263 | 571 | struct lock_class_key *lock_key, |
|---|
| 1264 | 572 | struct lock_class_key *request_key) |
|---|
| 1265 | 573 | { |
|---|
| 574 | + struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL; |
|---|
| 1266 | 575 | unsigned long flags; |
|---|
| 1267 | | - int status = 0; |
|---|
| 576 | + int ret = 0; |
|---|
| 1268 | 577 | unsigned i; |
|---|
| 1269 | | - int base = chip->base; |
|---|
| 578 | + int base = gc->base; |
|---|
| 1270 | 579 | struct gpio_device *gdev; |
|---|
| 580 | + bool block_gpio_read = false; |
|---|
| 1271 | 581 | |
|---|
| 1272 | 582 | /* |
|---|
| 1273 | 583 | * First: allocate and populate the internal stat container, and |
|---|
| .. | .. |
|---|
| 1277 | 587 | if (!gdev) |
|---|
| 1278 | 588 | return -ENOMEM; |
|---|
| 1279 | 589 | gdev->dev.bus = &gpio_bus_type; |
|---|
| 1280 | | - gdev->chip = chip; |
|---|
| 1281 | | - chip->gpiodev = gdev; |
|---|
| 1282 | | - if (chip->parent) { |
|---|
| 1283 | | - gdev->dev.parent = chip->parent; |
|---|
| 1284 | | - gdev->dev.of_node = chip->parent->of_node; |
|---|
| 590 | + gdev->chip = gc; |
|---|
| 591 | + gc->gpiodev = gdev; |
|---|
| 592 | + if (gc->parent) { |
|---|
| 593 | + gdev->dev.parent = gc->parent; |
|---|
| 594 | + gdev->dev.of_node = gc->parent->of_node; |
|---|
| 1285 | 595 | } |
|---|
| 1286 | 596 | |
|---|
| 1287 | | -#ifdef CONFIG_OF_GPIO |
|---|
| 1288 | | - /* If the gpiochip has an assigned OF node this takes precedence */ |
|---|
| 1289 | | - if (chip->of_node) |
|---|
| 1290 | | - gdev->dev.of_node = chip->of_node; |
|---|
| 1291 | | - else |
|---|
| 1292 | | - chip->of_node = gdev->dev.of_node; |
|---|
| 1293 | | -#endif |
|---|
| 597 | + of_gpio_dev_init(gc, gdev); |
|---|
| 1294 | 598 | |
|---|
| 1295 | | - gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL); |
|---|
| 599 | + /* |
|---|
| 600 | + * Assign fwnode depending on the result of the previous calls, |
|---|
| 601 | + * if none of them succeed, assign it to the parent's one. |
|---|
| 602 | + */ |
|---|
| 603 | + gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode; |
|---|
| 604 | + |
|---|
| 605 | + gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL); |
|---|
| 1296 | 606 | if (gdev->id < 0) { |
|---|
| 1297 | | - status = gdev->id; |
|---|
| 607 | + ret = gdev->id; |
|---|
| 1298 | 608 | goto err_free_gdev; |
|---|
| 1299 | 609 | } |
|---|
| 1300 | | - dev_set_name(&gdev->dev, "gpiochip%d", gdev->id); |
|---|
| 610 | + |
|---|
| 611 | + ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); |
|---|
| 612 | + if (ret) |
|---|
| 613 | + goto err_free_ida; |
|---|
| 614 | + |
|---|
| 1301 | 615 | device_initialize(&gdev->dev); |
|---|
| 1302 | | - dev_set_drvdata(&gdev->dev, gdev); |
|---|
| 1303 | | - if (chip->parent && chip->parent->driver) |
|---|
| 1304 | | - gdev->owner = chip->parent->driver->owner; |
|---|
| 1305 | | - else if (chip->owner) |
|---|
| 616 | + if (gc->parent && gc->parent->driver) |
|---|
| 617 | + gdev->owner = gc->parent->driver->owner; |
|---|
| 618 | + else if (gc->owner) |
|---|
| 1306 | 619 | /* TODO: remove chip->owner */ |
|---|
| 1307 | | - gdev->owner = chip->owner; |
|---|
| 620 | + gdev->owner = gc->owner; |
|---|
| 1308 | 621 | else |
|---|
| 1309 | 622 | gdev->owner = THIS_MODULE; |
|---|
| 1310 | 623 | |
|---|
| 1311 | | - gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); |
|---|
| 624 | + gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); |
|---|
| 1312 | 625 | if (!gdev->descs) { |
|---|
| 1313 | | - status = -ENOMEM; |
|---|
| 1314 | | - goto err_free_ida; |
|---|
| 626 | + ret = -ENOMEM; |
|---|
| 627 | + goto err_free_dev_name; |
|---|
| 1315 | 628 | } |
|---|
| 1316 | 629 | |
|---|
| 1317 | | - if (chip->ngpio == 0) { |
|---|
| 1318 | | - chip_err(chip, "tried to insert a GPIO chip with zero lines\n"); |
|---|
| 1319 | | - status = -EINVAL; |
|---|
| 630 | + if (gc->ngpio == 0) { |
|---|
| 631 | + chip_err(gc, "tried to insert a GPIO chip with zero lines\n"); |
|---|
| 632 | + ret = -EINVAL; |
|---|
| 1320 | 633 | goto err_free_descs; |
|---|
| 1321 | 634 | } |
|---|
| 1322 | 635 | |
|---|
| 1323 | | - if (chip->ngpio > FASTPATH_NGPIO) |
|---|
| 1324 | | - chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n", |
|---|
| 1325 | | - chip->ngpio, FASTPATH_NGPIO); |
|---|
| 636 | + if (gc->ngpio > FASTPATH_NGPIO) |
|---|
| 637 | + chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n", |
|---|
| 638 | + gc->ngpio, FASTPATH_NGPIO); |
|---|
| 1326 | 639 | |
|---|
| 1327 | | - gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL); |
|---|
| 640 | + gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL); |
|---|
| 1328 | 641 | if (!gdev->label) { |
|---|
| 1329 | | - status = -ENOMEM; |
|---|
| 642 | + ret = -ENOMEM; |
|---|
| 1330 | 643 | goto err_free_descs; |
|---|
| 1331 | 644 | } |
|---|
| 1332 | 645 | |
|---|
| 1333 | | - gdev->ngpio = chip->ngpio; |
|---|
| 646 | + gdev->ngpio = gc->ngpio; |
|---|
| 1334 | 647 | gdev->data = data; |
|---|
| 1335 | 648 | |
|---|
| 1336 | 649 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| .. | .. |
|---|
| 1343 | 656 | * of the sysfs interface anyways. |
|---|
| 1344 | 657 | */ |
|---|
| 1345 | 658 | if (base < 0) { |
|---|
| 1346 | | - base = gpiochip_find_base(chip->ngpio); |
|---|
| 659 | + base = gpiochip_find_base(gc->ngpio); |
|---|
| 1347 | 660 | if (base < 0) { |
|---|
| 1348 | | - status = base; |
|---|
| 661 | + ret = base; |
|---|
| 1349 | 662 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 1350 | 663 | goto err_free_label; |
|---|
| 1351 | 664 | } |
|---|
| .. | .. |
|---|
| 1355 | 668 | * see if anyone makes use of this, else drop this and assign |
|---|
| 1356 | 669 | * a poison instead. |
|---|
| 1357 | 670 | */ |
|---|
| 1358 | | - chip->base = base; |
|---|
| 671 | + gc->base = base; |
|---|
| 1359 | 672 | } |
|---|
| 1360 | 673 | gdev->base = base; |
|---|
| 1361 | 674 | |
|---|
| 1362 | | - status = gpiodev_add_to_list(gdev); |
|---|
| 1363 | | - if (status) { |
|---|
| 675 | + ret = gpiodev_add_to_list(gdev); |
|---|
| 676 | + if (ret) { |
|---|
| 1364 | 677 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 1365 | 678 | goto err_free_label; |
|---|
| 1366 | 679 | } |
|---|
| 1367 | 680 | |
|---|
| 681 | + for (i = 0; i < gc->ngpio; i++) |
|---|
| 682 | + gdev->descs[i].gdev = gdev; |
|---|
| 683 | + |
|---|
| 1368 | 684 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 1369 | 685 | |
|---|
| 1370 | | - for (i = 0; i < chip->ngpio; i++) { |
|---|
| 1371 | | - struct gpio_desc *desc = &gdev->descs[i]; |
|---|
| 1372 | | - |
|---|
| 1373 | | - desc->gdev = gdev; |
|---|
| 1374 | | - |
|---|
| 1375 | | - /* REVISIT: most hardware initializes GPIOs as inputs (often |
|---|
| 1376 | | - * with pullups enabled) so power usage is minimized. Linux |
|---|
| 1377 | | - * code should set the gpio direction first thing; but until |
|---|
| 1378 | | - * it does, and in case chip->get_direction is not set, we may |
|---|
| 1379 | | - * expose the wrong direction in sysfs. |
|---|
| 1380 | | - */ |
|---|
| 1381 | | - desc->flags = !chip->direction_input ? (1 << FLAG_IS_OUT) : 0; |
|---|
| 1382 | | - } |
|---|
| 686 | + BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier); |
|---|
| 1383 | 687 | |
|---|
| 1384 | 688 | #ifdef CONFIG_PINCTRL |
|---|
| 1385 | 689 | INIT_LIST_HEAD(&gdev->pin_ranges); |
|---|
| 1386 | 690 | #endif |
|---|
| 1387 | 691 | |
|---|
| 1388 | | - status = gpiochip_set_desc_names(chip); |
|---|
| 1389 | | - if (status) |
|---|
| 692 | + if (gc->names) |
|---|
| 693 | + ret = gpiochip_set_desc_names(gc); |
|---|
| 694 | + else |
|---|
| 695 | + ret = devprop_gpiochip_set_names(gc); |
|---|
| 696 | + if (ret) |
|---|
| 1390 | 697 | goto err_remove_from_list; |
|---|
| 1391 | 698 | |
|---|
| 1392 | | - status = gpiochip_irqchip_init_valid_mask(chip); |
|---|
| 1393 | | - if (status) |
|---|
| 699 | + ret = gpiochip_alloc_valid_mask(gc); |
|---|
| 700 | + if (ret) |
|---|
| 1394 | 701 | goto err_remove_from_list; |
|---|
| 1395 | 702 | |
|---|
| 1396 | | - status = gpiochip_init_valid_mask(chip); |
|---|
| 1397 | | - if (status) |
|---|
| 703 | + ret = of_gpiochip_add(gc); |
|---|
| 704 | + if (ret) |
|---|
| 705 | + goto err_free_gpiochip_mask; |
|---|
| 706 | + |
|---|
| 707 | + ret = gpiochip_init_valid_mask(gc); |
|---|
| 708 | + if (ret) |
|---|
| 709 | + goto err_remove_of_chip; |
|---|
| 710 | + |
|---|
| 711 | + trace_android_vh_gpio_block_read(gdev, &block_gpio_read); |
|---|
| 712 | + if (!block_gpio_read) { |
|---|
| 713 | + for (i = 0; i < gc->ngpio; i++) { |
|---|
| 714 | + struct gpio_desc *desc = &gdev->descs[i]; |
|---|
| 715 | + |
|---|
| 716 | + if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { |
|---|
| 717 | + assign_bit(FLAG_IS_OUT, |
|---|
| 718 | + &desc->flags, !gc->get_direction(gc, i)); |
|---|
| 719 | + } else { |
|---|
| 720 | + assign_bit(FLAG_IS_OUT, |
|---|
| 721 | + &desc->flags, !gc->direction_input); |
|---|
| 722 | + } |
|---|
| 723 | + } |
|---|
| 724 | + } |
|---|
| 725 | + |
|---|
| 726 | + ret = gpiochip_add_pin_ranges(gc); |
|---|
| 727 | + if (ret) |
|---|
| 728 | + goto err_remove_of_chip; |
|---|
| 729 | + |
|---|
| 730 | + acpi_gpiochip_add(gc); |
|---|
| 731 | + |
|---|
| 732 | + machine_gpiochip_add(gc); |
|---|
| 733 | + |
|---|
| 734 | + ret = gpiochip_irqchip_init_valid_mask(gc); |
|---|
| 735 | + if (ret) |
|---|
| 736 | + goto err_remove_acpi_chip; |
|---|
| 737 | + |
|---|
| 738 | + ret = gpiochip_irqchip_init_hw(gc); |
|---|
| 739 | + if (ret) |
|---|
| 740 | + goto err_remove_acpi_chip; |
|---|
| 741 | + |
|---|
| 742 | + ret = gpiochip_add_irqchip(gc, lock_key, request_key); |
|---|
| 743 | + if (ret) |
|---|
| 1398 | 744 | goto err_remove_irqchip_mask; |
|---|
| 1399 | | - |
|---|
| 1400 | | - status = gpiochip_add_irqchip(chip, lock_key, request_key); |
|---|
| 1401 | | - if (status) |
|---|
| 1402 | | - goto err_remove_chip; |
|---|
| 1403 | | - |
|---|
| 1404 | | - status = of_gpiochip_add(chip); |
|---|
| 1405 | | - if (status) |
|---|
| 1406 | | - goto err_remove_chip; |
|---|
| 1407 | | - |
|---|
| 1408 | | - acpi_gpiochip_add(chip); |
|---|
| 1409 | | - |
|---|
| 1410 | | - machine_gpiochip_add(chip); |
|---|
| 1411 | 745 | |
|---|
| 1412 | 746 | /* |
|---|
| 1413 | 747 | * By first adding the chardev, and then adding the device, |
|---|
| .. | .. |
|---|
| 1418 | 752 | * Otherwise, defer until later. |
|---|
| 1419 | 753 | */ |
|---|
| 1420 | 754 | if (gpiolib_initialized) { |
|---|
| 1421 | | - status = gpiochip_setup_dev(gdev); |
|---|
| 1422 | | - if (status) |
|---|
| 1423 | | - goto err_remove_chip; |
|---|
| 755 | + ret = gpiochip_setup_dev(gdev); |
|---|
| 756 | + if (ret) |
|---|
| 757 | + goto err_remove_irqchip; |
|---|
| 1424 | 758 | } |
|---|
| 1425 | 759 | return 0; |
|---|
| 1426 | 760 | |
|---|
| 1427 | | -err_remove_chip: |
|---|
| 1428 | | - acpi_gpiochip_remove(chip); |
|---|
| 1429 | | - gpiochip_free_hogs(chip); |
|---|
| 1430 | | - of_gpiochip_remove(chip); |
|---|
| 1431 | | - gpiochip_free_valid_mask(chip); |
|---|
| 761 | +err_remove_irqchip: |
|---|
| 762 | + gpiochip_irqchip_remove(gc); |
|---|
| 1432 | 763 | err_remove_irqchip_mask: |
|---|
| 1433 | | - gpiochip_irqchip_free_valid_mask(chip); |
|---|
| 764 | + gpiochip_irqchip_free_valid_mask(gc); |
|---|
| 765 | +err_remove_acpi_chip: |
|---|
| 766 | + acpi_gpiochip_remove(gc); |
|---|
| 767 | +err_remove_of_chip: |
|---|
| 768 | + gpiochip_free_hogs(gc); |
|---|
| 769 | + of_gpiochip_remove(gc); |
|---|
| 770 | +err_free_gpiochip_mask: |
|---|
| 771 | + gpiochip_remove_pin_ranges(gc); |
|---|
| 772 | + gpiochip_free_valid_mask(gc); |
|---|
| 1434 | 773 | err_remove_from_list: |
|---|
| 1435 | 774 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 1436 | 775 | list_del(&gdev->list); |
|---|
| .. | .. |
|---|
| 1439 | 778 | kfree_const(gdev->label); |
|---|
| 1440 | 779 | err_free_descs: |
|---|
| 1441 | 780 | kfree(gdev->descs); |
|---|
| 781 | +err_free_dev_name: |
|---|
| 782 | + kfree(dev_name(&gdev->dev)); |
|---|
| 1442 | 783 | err_free_ida: |
|---|
| 1443 | | - ida_simple_remove(&gpio_ida, gdev->id); |
|---|
| 784 | + ida_free(&gpio_ida, gdev->id); |
|---|
| 1444 | 785 | err_free_gdev: |
|---|
| 1445 | 786 | /* failures here can mean systems won't boot... */ |
|---|
| 1446 | 787 | pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, |
|---|
| 1447 | 788 | gdev->base, gdev->base + gdev->ngpio - 1, |
|---|
| 1448 | | - chip->label ? : "generic", status); |
|---|
| 789 | + gc->label ? : "generic", ret); |
|---|
| 1449 | 790 | kfree(gdev); |
|---|
| 1450 | | - return status; |
|---|
| 791 | + return ret; |
|---|
| 1451 | 792 | } |
|---|
| 1452 | 793 | EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key); |
|---|
| 1453 | 794 | |
|---|
| 1454 | 795 | /** |
|---|
| 1455 | 796 | * gpiochip_get_data() - get per-subdriver data for the chip |
|---|
| 1456 | | - * @chip: GPIO chip |
|---|
| 797 | + * @gc: GPIO chip |
|---|
| 1457 | 798 | * |
|---|
| 1458 | 799 | * Returns: |
|---|
| 1459 | 800 | * The per-subdriver data for the chip. |
|---|
| 1460 | 801 | */ |
|---|
| 1461 | | -void *gpiochip_get_data(struct gpio_chip *chip) |
|---|
| 802 | +void *gpiochip_get_data(struct gpio_chip *gc) |
|---|
| 1462 | 803 | { |
|---|
| 1463 | | - return chip->gpiodev->data; |
|---|
| 804 | + return gc->gpiodev->data; |
|---|
| 1464 | 805 | } |
|---|
| 1465 | 806 | EXPORT_SYMBOL_GPL(gpiochip_get_data); |
|---|
| 1466 | 807 | |
|---|
| 1467 | 808 | /** |
|---|
| 1468 | 809 | * gpiochip_remove() - unregister a gpio_chip |
|---|
| 1469 | | - * @chip: the chip to unregister |
|---|
| 810 | + * @gc: the chip to unregister |
|---|
| 1470 | 811 | * |
|---|
| 1471 | 812 | * A gpio_chip with any GPIOs still requested may not be removed. |
|---|
| 1472 | 813 | */ |
|---|
| 1473 | | -void gpiochip_remove(struct gpio_chip *chip) |
|---|
| 814 | +void gpiochip_remove(struct gpio_chip *gc) |
|---|
| 1474 | 815 | { |
|---|
| 1475 | | - struct gpio_device *gdev = chip->gpiodev; |
|---|
| 1476 | | - struct gpio_desc *desc; |
|---|
| 816 | + struct gpio_device *gdev = gc->gpiodev; |
|---|
| 1477 | 817 | unsigned long flags; |
|---|
| 1478 | | - unsigned i; |
|---|
| 1479 | | - bool requested = false; |
|---|
| 818 | + unsigned int i; |
|---|
| 1480 | 819 | |
|---|
| 1481 | 820 | /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ |
|---|
| 1482 | 821 | gpiochip_sysfs_unregister(gdev); |
|---|
| 1483 | | - gpiochip_free_hogs(chip); |
|---|
| 822 | + gpiochip_free_hogs(gc); |
|---|
| 1484 | 823 | /* Numb the device, cancelling all outstanding operations */ |
|---|
| 1485 | 824 | gdev->chip = NULL; |
|---|
| 1486 | | - gpiochip_irqchip_remove(chip); |
|---|
| 1487 | | - acpi_gpiochip_remove(chip); |
|---|
| 1488 | | - gpiochip_remove_pin_ranges(chip); |
|---|
| 1489 | | - of_gpiochip_remove(chip); |
|---|
| 1490 | | - gpiochip_free_valid_mask(chip); |
|---|
| 825 | + gpiochip_irqchip_remove(gc); |
|---|
| 826 | + acpi_gpiochip_remove(gc); |
|---|
| 827 | + of_gpiochip_remove(gc); |
|---|
| 828 | + gpiochip_remove_pin_ranges(gc); |
|---|
| 829 | + gpiochip_free_valid_mask(gc); |
|---|
| 1491 | 830 | /* |
|---|
| 1492 | 831 | * We accept no more calls into the driver from this point, so |
|---|
| 1493 | 832 | * NULL the driver data pointer |
|---|
| .. | .. |
|---|
| 1496 | 835 | |
|---|
| 1497 | 836 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 1498 | 837 | for (i = 0; i < gdev->ngpio; i++) { |
|---|
| 1499 | | - desc = &gdev->descs[i]; |
|---|
| 1500 | | - if (test_bit(FLAG_REQUESTED, &desc->flags)) |
|---|
| 1501 | | - requested = true; |
|---|
| 838 | + if (gpiochip_is_requested(gc, i)) |
|---|
| 839 | + break; |
|---|
| 1502 | 840 | } |
|---|
| 1503 | 841 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 1504 | 842 | |
|---|
| 1505 | | - if (requested) |
|---|
| 843 | + if (i != gdev->ngpio) |
|---|
| 1506 | 844 | dev_crit(&gdev->dev, |
|---|
| 1507 | 845 | "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); |
|---|
| 1508 | 846 | |
|---|
| .. | .. |
|---|
| 1512 | 850 | * be removed, else it will be dangling until the last user is |
|---|
| 1513 | 851 | * gone. |
|---|
| 1514 | 852 | */ |
|---|
| 1515 | | - cdev_device_del(&gdev->chrdev, &gdev->dev); |
|---|
| 853 | + gcdev_unregister(gdev); |
|---|
| 1516 | 854 | put_device(&gdev->dev); |
|---|
| 1517 | 855 | } |
|---|
| 1518 | 856 | EXPORT_SYMBOL_GPL(gpiochip_remove); |
|---|
| 1519 | | - |
|---|
| 1520 | | -static void devm_gpio_chip_release(struct device *dev, void *res) |
|---|
| 1521 | | -{ |
|---|
| 1522 | | - struct gpio_chip *chip = *(struct gpio_chip **)res; |
|---|
| 1523 | | - |
|---|
| 1524 | | - gpiochip_remove(chip); |
|---|
| 1525 | | -} |
|---|
| 1526 | | - |
|---|
| 1527 | | -static int devm_gpio_chip_match(struct device *dev, void *res, void *data) |
|---|
| 1528 | | - |
|---|
| 1529 | | -{ |
|---|
| 1530 | | - struct gpio_chip **r = res; |
|---|
| 1531 | | - |
|---|
| 1532 | | - if (!r || !*r) { |
|---|
| 1533 | | - WARN_ON(!r || !*r); |
|---|
| 1534 | | - return 0; |
|---|
| 1535 | | - } |
|---|
| 1536 | | - |
|---|
| 1537 | | - return *r == data; |
|---|
| 1538 | | -} |
|---|
| 1539 | | - |
|---|
| 1540 | | -/** |
|---|
| 1541 | | - * devm_gpiochip_add_data() - Resource manager gpiochip_add_data() |
|---|
| 1542 | | - * @dev: the device pointer on which irq_chip belongs to. |
|---|
| 1543 | | - * @chip: the chip to register, with chip->base initialized |
|---|
| 1544 | | - * @data: driver-private data associated with this chip |
|---|
| 1545 | | - * |
|---|
| 1546 | | - * Context: potentially before irqs will work |
|---|
| 1547 | | - * |
|---|
| 1548 | | - * The gpio chip automatically be released when the device is unbound. |
|---|
| 1549 | | - * |
|---|
| 1550 | | - * Returns: |
|---|
| 1551 | | - * A negative errno if the chip can't be registered, such as because the |
|---|
| 1552 | | - * chip->base is invalid or already associated with a different chip. |
|---|
| 1553 | | - * Otherwise it returns zero as a success code. |
|---|
| 1554 | | - */ |
|---|
| 1555 | | -int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip, |
|---|
| 1556 | | - void *data) |
|---|
| 1557 | | -{ |
|---|
| 1558 | | - struct gpio_chip **ptr; |
|---|
| 1559 | | - int ret; |
|---|
| 1560 | | - |
|---|
| 1561 | | - ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr), |
|---|
| 1562 | | - GFP_KERNEL); |
|---|
| 1563 | | - if (!ptr) |
|---|
| 1564 | | - return -ENOMEM; |
|---|
| 1565 | | - |
|---|
| 1566 | | - ret = gpiochip_add_data(chip, data); |
|---|
| 1567 | | - if (ret < 0) { |
|---|
| 1568 | | - devres_free(ptr); |
|---|
| 1569 | | - return ret; |
|---|
| 1570 | | - } |
|---|
| 1571 | | - |
|---|
| 1572 | | - *ptr = chip; |
|---|
| 1573 | | - devres_add(dev, ptr); |
|---|
| 1574 | | - |
|---|
| 1575 | | - return 0; |
|---|
| 1576 | | -} |
|---|
| 1577 | | -EXPORT_SYMBOL_GPL(devm_gpiochip_add_data); |
|---|
| 1578 | | - |
|---|
| 1579 | | -/** |
|---|
| 1580 | | - * devm_gpiochip_remove() - Resource manager of gpiochip_remove() |
|---|
| 1581 | | - * @dev: device for which which resource was allocated |
|---|
| 1582 | | - * @chip: the chip to remove |
|---|
| 1583 | | - * |
|---|
| 1584 | | - * A gpio_chip with any GPIOs still requested may not be removed. |
|---|
| 1585 | | - */ |
|---|
| 1586 | | -void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip) |
|---|
| 1587 | | -{ |
|---|
| 1588 | | - int ret; |
|---|
| 1589 | | - |
|---|
| 1590 | | - ret = devres_release(dev, devm_gpio_chip_release, |
|---|
| 1591 | | - devm_gpio_chip_match, chip); |
|---|
| 1592 | | - WARN_ON(ret); |
|---|
| 1593 | | -} |
|---|
| 1594 | | -EXPORT_SYMBOL_GPL(devm_gpiochip_remove); |
|---|
| 1595 | 857 | |
|---|
| 1596 | 858 | /** |
|---|
| 1597 | 859 | * gpiochip_find() - iterator for locating a specific gpio_chip |
|---|
| .. | .. |
|---|
| 1605 | 867 | * more gpio_chips. |
|---|
| 1606 | 868 | */ |
|---|
| 1607 | 869 | struct gpio_chip *gpiochip_find(void *data, |
|---|
| 1608 | | - int (*match)(struct gpio_chip *chip, |
|---|
| 870 | + int (*match)(struct gpio_chip *gc, |
|---|
| 1609 | 871 | void *data)) |
|---|
| 1610 | 872 | { |
|---|
| 1611 | 873 | struct gpio_device *gdev; |
|---|
| 1612 | | - struct gpio_chip *chip = NULL; |
|---|
| 874 | + struct gpio_chip *gc = NULL; |
|---|
| 1613 | 875 | unsigned long flags; |
|---|
| 1614 | 876 | |
|---|
| 1615 | 877 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 1616 | 878 | list_for_each_entry(gdev, &gpio_devices, list) |
|---|
| 1617 | 879 | if (gdev->chip && match(gdev->chip, data)) { |
|---|
| 1618 | | - chip = gdev->chip; |
|---|
| 880 | + gc = gdev->chip; |
|---|
| 1619 | 881 | break; |
|---|
| 1620 | 882 | } |
|---|
| 1621 | 883 | |
|---|
| 1622 | 884 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 1623 | 885 | |
|---|
| 1624 | | - return chip; |
|---|
| 886 | + return gc; |
|---|
| 1625 | 887 | } |
|---|
| 1626 | 888 | EXPORT_SYMBOL_GPL(gpiochip_find); |
|---|
| 1627 | 889 | |
|---|
| 1628 | | -static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
|---|
| 890 | +static int gpiochip_match_name(struct gpio_chip *gc, void *data) |
|---|
| 1629 | 891 | { |
|---|
| 1630 | 892 | const char *name = data; |
|---|
| 1631 | 893 | |
|---|
| 1632 | | - return !strcmp(chip->label, name); |
|---|
| 894 | + return !strcmp(gc->label, name); |
|---|
| 1633 | 895 | } |
|---|
| 1634 | 896 | |
|---|
| 1635 | 897 | static struct gpio_chip *find_chip_by_name(const char *name) |
|---|
| .. | .. |
|---|
| 1643 | 905 | * The following is irqchip helper code for gpiochips. |
|---|
| 1644 | 906 | */ |
|---|
| 1645 | 907 | |
|---|
| 1646 | | -static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
|---|
| 908 | +static int gpiochip_irqchip_init_hw(struct gpio_chip *gc) |
|---|
| 1647 | 909 | { |
|---|
| 1648 | | - if (!gpiochip->irq.need_valid_mask) |
|---|
| 910 | + struct gpio_irq_chip *girq = &gc->irq; |
|---|
| 911 | + |
|---|
| 912 | + if (!girq->init_hw) |
|---|
| 1649 | 913 | return 0; |
|---|
| 1650 | 914 | |
|---|
| 1651 | | - gpiochip->irq.valid_mask = gpiochip_allocate_mask(gpiochip); |
|---|
| 1652 | | - if (!gpiochip->irq.valid_mask) |
|---|
| 915 | + return girq->init_hw(gc); |
|---|
| 916 | +} |
|---|
| 917 | + |
|---|
| 918 | +static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc) |
|---|
| 919 | +{ |
|---|
| 920 | + struct gpio_irq_chip *girq = &gc->irq; |
|---|
| 921 | + |
|---|
| 922 | + if (!girq->init_valid_mask) |
|---|
| 923 | + return 0; |
|---|
| 924 | + |
|---|
| 925 | + girq->valid_mask = gpiochip_allocate_mask(gc); |
|---|
| 926 | + if (!girq->valid_mask) |
|---|
| 1653 | 927 | return -ENOMEM; |
|---|
| 928 | + |
|---|
| 929 | + girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); |
|---|
| 1654 | 930 | |
|---|
| 1655 | 931 | return 0; |
|---|
| 1656 | 932 | } |
|---|
| 1657 | 933 | |
|---|
| 1658 | | -static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) |
|---|
| 934 | +static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc) |
|---|
| 1659 | 935 | { |
|---|
| 1660 | | - kfree(gpiochip->irq.valid_mask); |
|---|
| 1661 | | - gpiochip->irq.valid_mask = NULL; |
|---|
| 936 | + bitmap_free(gc->irq.valid_mask); |
|---|
| 937 | + gc->irq.valid_mask = NULL; |
|---|
| 1662 | 938 | } |
|---|
| 1663 | 939 | |
|---|
| 1664 | | -bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip, |
|---|
| 940 | +bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc, |
|---|
| 1665 | 941 | unsigned int offset) |
|---|
| 1666 | 942 | { |
|---|
| 1667 | | - if (!gpiochip_line_is_valid(gpiochip, offset)) |
|---|
| 943 | + if (!gpiochip_line_is_valid(gc, offset)) |
|---|
| 1668 | 944 | return false; |
|---|
| 1669 | 945 | /* No mask means all valid */ |
|---|
| 1670 | | - if (likely(!gpiochip->irq.valid_mask)) |
|---|
| 946 | + if (likely(!gc->irq.valid_mask)) |
|---|
| 1671 | 947 | return true; |
|---|
| 1672 | | - return test_bit(offset, gpiochip->irq.valid_mask); |
|---|
| 948 | + return test_bit(offset, gc->irq.valid_mask); |
|---|
| 1673 | 949 | } |
|---|
| 1674 | 950 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid); |
|---|
| 1675 | 951 | |
|---|
| 1676 | 952 | /** |
|---|
| 1677 | 953 | * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip |
|---|
| 1678 | | - * @gpiochip: the gpiochip to set the irqchip chain to |
|---|
| 1679 | | - * @irqchip: the irqchip to chain to the gpiochip |
|---|
| 954 | + * @gc: the gpiochip to set the irqchip chain to |
|---|
| 1680 | 955 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
|---|
| 1681 | | - * chained irqchip |
|---|
| 956 | + * cascaded irqchip |
|---|
| 1682 | 957 | * @parent_handler: the parent interrupt handler for the accumulated IRQ |
|---|
| 1683 | 958 | * coming out of the gpiochip. If the interrupt is nested rather than |
|---|
| 1684 | 959 | * cascaded, pass NULL in this handler argument |
|---|
| 1685 | 960 | */ |
|---|
| 1686 | | -static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gpiochip, |
|---|
| 1687 | | - struct irq_chip *irqchip, |
|---|
| 961 | +static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc, |
|---|
| 1688 | 962 | unsigned int parent_irq, |
|---|
| 1689 | 963 | irq_flow_handler_t parent_handler) |
|---|
| 1690 | 964 | { |
|---|
| 1691 | | - unsigned int offset; |
|---|
| 965 | + struct gpio_irq_chip *girq = &gc->irq; |
|---|
| 966 | + struct device *dev = &gc->gpiodev->dev; |
|---|
| 1692 | 967 | |
|---|
| 1693 | | - if (!gpiochip->irq.domain) { |
|---|
| 1694 | | - chip_err(gpiochip, "called %s before setting up irqchip\n", |
|---|
| 968 | + if (!girq->domain) { |
|---|
| 969 | + chip_err(gc, "called %s before setting up irqchip\n", |
|---|
| 1695 | 970 | __func__); |
|---|
| 1696 | 971 | return; |
|---|
| 1697 | 972 | } |
|---|
| 1698 | 973 | |
|---|
| 1699 | 974 | if (parent_handler) { |
|---|
| 1700 | | - if (gpiochip->can_sleep) { |
|---|
| 1701 | | - chip_err(gpiochip, |
|---|
| 975 | + if (gc->can_sleep) { |
|---|
| 976 | + chip_err(gc, |
|---|
| 1702 | 977 | "you cannot have chained interrupts on a chip that may sleep\n"); |
|---|
| 1703 | 978 | return; |
|---|
| 1704 | 979 | } |
|---|
| 980 | + girq->parents = devm_kcalloc(dev, 1, |
|---|
| 981 | + sizeof(*girq->parents), |
|---|
| 982 | + GFP_KERNEL); |
|---|
| 983 | + if (!girq->parents) { |
|---|
| 984 | + chip_err(gc, "out of memory allocating parent IRQ\n"); |
|---|
| 985 | + return; |
|---|
| 986 | + } |
|---|
| 987 | + girq->parents[0] = parent_irq; |
|---|
| 988 | + girq->num_parents = 1; |
|---|
| 1705 | 989 | /* |
|---|
| 1706 | 990 | * The parent irqchip is already using the chip_data for this |
|---|
| 1707 | 991 | * irqchip, so our callbacks simply use the handler_data. |
|---|
| 1708 | 992 | */ |
|---|
| 1709 | 993 | irq_set_chained_handler_and_data(parent_irq, parent_handler, |
|---|
| 1710 | | - gpiochip); |
|---|
| 1711 | | - |
|---|
| 1712 | | - gpiochip->irq.parent_irq = parent_irq; |
|---|
| 1713 | | - gpiochip->irq.parents = &gpiochip->irq.parent_irq; |
|---|
| 1714 | | - gpiochip->irq.num_parents = 1; |
|---|
| 1715 | | - } |
|---|
| 1716 | | - |
|---|
| 1717 | | - /* Set the parent IRQ for all affected IRQs */ |
|---|
| 1718 | | - for (offset = 0; offset < gpiochip->ngpio; offset++) { |
|---|
| 1719 | | - if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
|---|
| 1720 | | - continue; |
|---|
| 1721 | | - irq_set_parent(irq_find_mapping(gpiochip->irq.domain, offset), |
|---|
| 1722 | | - parent_irq); |
|---|
| 994 | + gc); |
|---|
| 1723 | 995 | } |
|---|
| 1724 | 996 | } |
|---|
| 1725 | | - |
|---|
| 1726 | | -/** |
|---|
| 1727 | | - * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip |
|---|
| 1728 | | - * @gpiochip: the gpiochip to set the irqchip chain to |
|---|
| 1729 | | - * @irqchip: the irqchip to chain to the gpiochip |
|---|
| 1730 | | - * @parent_irq: the irq number corresponding to the parent IRQ for this |
|---|
| 1731 | | - * chained irqchip |
|---|
| 1732 | | - * @parent_handler: the parent interrupt handler for the accumulated IRQ |
|---|
| 1733 | | - * coming out of the gpiochip. If the interrupt is nested rather than |
|---|
| 1734 | | - * cascaded, pass NULL in this handler argument |
|---|
| 1735 | | - */ |
|---|
| 1736 | | -void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip, |
|---|
| 1737 | | - struct irq_chip *irqchip, |
|---|
| 1738 | | - unsigned int parent_irq, |
|---|
| 1739 | | - irq_flow_handler_t parent_handler) |
|---|
| 1740 | | -{ |
|---|
| 1741 | | - if (gpiochip->irq.threaded) { |
|---|
| 1742 | | - chip_err(gpiochip, "tried to chain a threaded gpiochip\n"); |
|---|
| 1743 | | - return; |
|---|
| 1744 | | - } |
|---|
| 1745 | | - |
|---|
| 1746 | | - gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, |
|---|
| 1747 | | - parent_handler); |
|---|
| 1748 | | -} |
|---|
| 1749 | | -EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip); |
|---|
| 1750 | 997 | |
|---|
| 1751 | 998 | /** |
|---|
| 1752 | 999 | * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip |
|---|
| 1753 | | - * @gpiochip: the gpiochip to set the irqchip nested handler to |
|---|
| 1000 | + * @gc: the gpiochip to set the irqchip nested handler to |
|---|
| 1754 | 1001 | * @irqchip: the irqchip to nest to the gpiochip |
|---|
| 1755 | 1002 | * @parent_irq: the irq number corresponding to the parent IRQ for this |
|---|
| 1756 | 1003 | * nested irqchip |
|---|
| 1757 | 1004 | */ |
|---|
| 1758 | | -void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip, |
|---|
| 1005 | +void gpiochip_set_nested_irqchip(struct gpio_chip *gc, |
|---|
| 1759 | 1006 | struct irq_chip *irqchip, |
|---|
| 1760 | 1007 | unsigned int parent_irq) |
|---|
| 1761 | 1008 | { |
|---|
| 1762 | | - gpiochip_set_cascaded_irqchip(gpiochip, irqchip, parent_irq, |
|---|
| 1763 | | - NULL); |
|---|
| 1009 | + gpiochip_set_cascaded_irqchip(gc, parent_irq, NULL); |
|---|
| 1764 | 1010 | } |
|---|
| 1765 | 1011 | EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip); |
|---|
| 1012 | + |
|---|
| 1013 | +#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
|---|
| 1014 | + |
|---|
| 1015 | +/** |
|---|
| 1016 | + * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip |
|---|
| 1017 | + * to a gpiochip |
|---|
| 1018 | + * @gc: the gpiochip to set the irqchip hierarchical handler to |
|---|
| 1019 | + * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt |
|---|
| 1020 | + * will then percolate up to the parent |
|---|
| 1021 | + */ |
|---|
| 1022 | +static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc, |
|---|
| 1023 | + struct irq_chip *irqchip) |
|---|
| 1024 | +{ |
|---|
| 1025 | + /* DT will deal with mapping each IRQ as we go along */ |
|---|
| 1026 | + if (is_of_node(gc->irq.fwnode)) |
|---|
| 1027 | + return; |
|---|
| 1028 | + |
|---|
| 1029 | + /* |
|---|
| 1030 | + * This is for legacy and boardfile "irqchip" fwnodes: allocate |
|---|
| 1031 | + * irqs upfront instead of dynamically since we don't have the |
|---|
| 1032 | + * dynamic type of allocation that hardware description languages |
|---|
| 1033 | + * provide. Once all GPIO drivers using board files are gone from |
|---|
| 1034 | + * the kernel we can delete this code, but for a transitional period |
|---|
| 1035 | + * it is necessary to keep this around. |
|---|
| 1036 | + */ |
|---|
| 1037 | + if (is_fwnode_irqchip(gc->irq.fwnode)) { |
|---|
| 1038 | + int i; |
|---|
| 1039 | + int ret; |
|---|
| 1040 | + |
|---|
| 1041 | + for (i = 0; i < gc->ngpio; i++) { |
|---|
| 1042 | + struct irq_fwspec fwspec; |
|---|
| 1043 | + unsigned int parent_hwirq; |
|---|
| 1044 | + unsigned int parent_type; |
|---|
| 1045 | + struct gpio_irq_chip *girq = &gc->irq; |
|---|
| 1046 | + |
|---|
| 1047 | + /* |
|---|
| 1048 | + * We call the child to parent translation function |
|---|
| 1049 | + * only to check if the child IRQ is valid or not. |
|---|
| 1050 | + * Just pick the rising edge type here as that is what |
|---|
| 1051 | + * we likely need to support. |
|---|
| 1052 | + */ |
|---|
| 1053 | + ret = girq->child_to_parent_hwirq(gc, i, |
|---|
| 1054 | + IRQ_TYPE_EDGE_RISING, |
|---|
| 1055 | + &parent_hwirq, |
|---|
| 1056 | + &parent_type); |
|---|
| 1057 | + if (ret) { |
|---|
| 1058 | + chip_err(gc, "skip set-up on hwirq %d\n", |
|---|
| 1059 | + i); |
|---|
| 1060 | + continue; |
|---|
| 1061 | + } |
|---|
| 1062 | + |
|---|
| 1063 | + fwspec.fwnode = gc->irq.fwnode; |
|---|
| 1064 | + /* This is the hwirq for the GPIO line side of things */ |
|---|
| 1065 | + fwspec.param[0] = girq->child_offset_to_irq(gc, i); |
|---|
| 1066 | + /* Just pick something */ |
|---|
| 1067 | + fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
|---|
| 1068 | + fwspec.param_count = 2; |
|---|
| 1069 | + ret = __irq_domain_alloc_irqs(gc->irq.domain, |
|---|
| 1070 | + /* just pick something */ |
|---|
| 1071 | + -1, |
|---|
| 1072 | + 1, |
|---|
| 1073 | + NUMA_NO_NODE, |
|---|
| 1074 | + &fwspec, |
|---|
| 1075 | + false, |
|---|
| 1076 | + NULL); |
|---|
| 1077 | + if (ret < 0) { |
|---|
| 1078 | + chip_err(gc, |
|---|
| 1079 | + "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n", |
|---|
| 1080 | + i, parent_hwirq, |
|---|
| 1081 | + ret); |
|---|
| 1082 | + } |
|---|
| 1083 | + } |
|---|
| 1084 | + } |
|---|
| 1085 | + |
|---|
| 1086 | + chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__); |
|---|
| 1087 | + |
|---|
| 1088 | + return; |
|---|
| 1089 | +} |
|---|
| 1090 | + |
|---|
| 1091 | +static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d, |
|---|
| 1092 | + struct irq_fwspec *fwspec, |
|---|
| 1093 | + unsigned long *hwirq, |
|---|
| 1094 | + unsigned int *type) |
|---|
| 1095 | +{ |
|---|
| 1096 | + /* We support standard DT translation */ |
|---|
| 1097 | + if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { |
|---|
| 1098 | + return irq_domain_translate_twocell(d, fwspec, hwirq, type); |
|---|
| 1099 | + } |
|---|
| 1100 | + |
|---|
| 1101 | + /* This is for board files and others not using DT */ |
|---|
| 1102 | + if (is_fwnode_irqchip(fwspec->fwnode)) { |
|---|
| 1103 | + int ret; |
|---|
| 1104 | + |
|---|
| 1105 | + ret = irq_domain_translate_twocell(d, fwspec, hwirq, type); |
|---|
| 1106 | + if (ret) |
|---|
| 1107 | + return ret; |
|---|
| 1108 | + WARN_ON(*type == IRQ_TYPE_NONE); |
|---|
| 1109 | + return 0; |
|---|
| 1110 | + } |
|---|
| 1111 | + return -EINVAL; |
|---|
| 1112 | +} |
|---|
| 1113 | + |
|---|
| 1114 | +static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d, |
|---|
| 1115 | + unsigned int irq, |
|---|
| 1116 | + unsigned int nr_irqs, |
|---|
| 1117 | + void *data) |
|---|
| 1118 | +{ |
|---|
| 1119 | + struct gpio_chip *gc = d->host_data; |
|---|
| 1120 | + irq_hw_number_t hwirq; |
|---|
| 1121 | + unsigned int type = IRQ_TYPE_NONE; |
|---|
| 1122 | + struct irq_fwspec *fwspec = data; |
|---|
| 1123 | + void *parent_arg; |
|---|
| 1124 | + unsigned int parent_hwirq; |
|---|
| 1125 | + unsigned int parent_type; |
|---|
| 1126 | + struct gpio_irq_chip *girq = &gc->irq; |
|---|
| 1127 | + int ret; |
|---|
| 1128 | + |
|---|
| 1129 | + /* |
|---|
| 1130 | + * The nr_irqs parameter is always one except for PCI multi-MSI |
|---|
| 1131 | + * so this should not happen. |
|---|
| 1132 | + */ |
|---|
| 1133 | + WARN_ON(nr_irqs != 1); |
|---|
| 1134 | + |
|---|
| 1135 | + ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); |
|---|
| 1136 | + if (ret) |
|---|
| 1137 | + return ret; |
|---|
| 1138 | + |
|---|
| 1139 | + chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq); |
|---|
| 1140 | + |
|---|
| 1141 | + ret = girq->child_to_parent_hwirq(gc, hwirq, type, |
|---|
| 1142 | + &parent_hwirq, &parent_type); |
|---|
| 1143 | + if (ret) { |
|---|
| 1144 | + chip_err(gc, "can't look up hwirq %lu\n", hwirq); |
|---|
| 1145 | + return ret; |
|---|
| 1146 | + } |
|---|
| 1147 | + chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq); |
|---|
| 1148 | + |
|---|
| 1149 | + /* |
|---|
| 1150 | + * We set handle_bad_irq because the .set_type() should |
|---|
| 1151 | + * always be invoked and set the right type of handler. |
|---|
| 1152 | + */ |
|---|
| 1153 | + irq_domain_set_info(d, |
|---|
| 1154 | + irq, |
|---|
| 1155 | + hwirq, |
|---|
| 1156 | + gc->irq.chip, |
|---|
| 1157 | + gc, |
|---|
| 1158 | + girq->handler, |
|---|
| 1159 | + NULL, NULL); |
|---|
| 1160 | + irq_set_probe(irq); |
|---|
| 1161 | + |
|---|
| 1162 | + /* This parent only handles asserted level IRQs */ |
|---|
| 1163 | + parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type); |
|---|
| 1164 | + if (!parent_arg) |
|---|
| 1165 | + return -ENOMEM; |
|---|
| 1166 | + |
|---|
| 1167 | + chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n", |
|---|
| 1168 | + irq, parent_hwirq); |
|---|
| 1169 | + irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); |
|---|
| 1170 | + ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg); |
|---|
| 1171 | + /* |
|---|
| 1172 | + * If the parent irqdomain is msi, the interrupts have already |
|---|
| 1173 | + * been allocated, so the EEXIST is good. |
|---|
| 1174 | + */ |
|---|
| 1175 | + if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) |
|---|
| 1176 | + ret = 0; |
|---|
| 1177 | + if (ret) |
|---|
| 1178 | + chip_err(gc, |
|---|
| 1179 | + "failed to allocate parent hwirq %d for hwirq %lu\n", |
|---|
| 1180 | + parent_hwirq, hwirq); |
|---|
| 1181 | + |
|---|
| 1182 | + kfree(parent_arg); |
|---|
| 1183 | + return ret; |
|---|
| 1184 | +} |
|---|
| 1185 | + |
|---|
| 1186 | +static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc, |
|---|
| 1187 | + unsigned int offset) |
|---|
| 1188 | +{ |
|---|
| 1189 | + return offset; |
|---|
| 1190 | +} |
|---|
| 1191 | + |
|---|
| 1192 | +static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops) |
|---|
| 1193 | +{ |
|---|
| 1194 | + ops->activate = gpiochip_irq_domain_activate; |
|---|
| 1195 | + ops->deactivate = gpiochip_irq_domain_deactivate; |
|---|
| 1196 | + ops->alloc = gpiochip_hierarchy_irq_domain_alloc; |
|---|
| 1197 | + ops->free = irq_domain_free_irqs_common; |
|---|
| 1198 | + |
|---|
| 1199 | + /* |
|---|
| 1200 | + * We only allow overriding the translate() function for |
|---|
| 1201 | + * hierarchical chips, and this should only be done if the user |
|---|
| 1202 | + * really need something other than 1:1 translation. |
|---|
| 1203 | + */ |
|---|
| 1204 | + if (!ops->translate) |
|---|
| 1205 | + ops->translate = gpiochip_hierarchy_irq_domain_translate; |
|---|
| 1206 | +} |
|---|
| 1207 | + |
|---|
| 1208 | +static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) |
|---|
| 1209 | +{ |
|---|
| 1210 | + if (!gc->irq.child_to_parent_hwirq || |
|---|
| 1211 | + !gc->irq.fwnode) { |
|---|
| 1212 | + chip_err(gc, "missing irqdomain vital data\n"); |
|---|
| 1213 | + return -EINVAL; |
|---|
| 1214 | + } |
|---|
| 1215 | + |
|---|
| 1216 | + if (!gc->irq.child_offset_to_irq) |
|---|
| 1217 | + gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; |
|---|
| 1218 | + |
|---|
| 1219 | + if (!gc->irq.populate_parent_alloc_arg) |
|---|
| 1220 | + gc->irq.populate_parent_alloc_arg = |
|---|
| 1221 | + gpiochip_populate_parent_fwspec_twocell; |
|---|
| 1222 | + |
|---|
| 1223 | + gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); |
|---|
| 1224 | + |
|---|
| 1225 | + gc->irq.domain = irq_domain_create_hierarchy( |
|---|
| 1226 | + gc->irq.parent_domain, |
|---|
| 1227 | + 0, |
|---|
| 1228 | + gc->ngpio, |
|---|
| 1229 | + gc->irq.fwnode, |
|---|
| 1230 | + &gc->irq.child_irq_domain_ops, |
|---|
| 1231 | + gc); |
|---|
| 1232 | + |
|---|
| 1233 | + if (!gc->irq.domain) |
|---|
| 1234 | + return -ENOMEM; |
|---|
| 1235 | + |
|---|
| 1236 | + gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); |
|---|
| 1237 | + |
|---|
| 1238 | + return 0; |
|---|
| 1239 | +} |
|---|
| 1240 | + |
|---|
| 1241 | +static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) |
|---|
| 1242 | +{ |
|---|
| 1243 | + return !!gc->irq.parent_domain; |
|---|
| 1244 | +} |
|---|
| 1245 | + |
|---|
| 1246 | +void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc, |
|---|
| 1247 | + unsigned int parent_hwirq, |
|---|
| 1248 | + unsigned int parent_type) |
|---|
| 1249 | +{ |
|---|
| 1250 | + struct irq_fwspec *fwspec; |
|---|
| 1251 | + |
|---|
| 1252 | + fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL); |
|---|
| 1253 | + if (!fwspec) |
|---|
| 1254 | + return NULL; |
|---|
| 1255 | + |
|---|
| 1256 | + fwspec->fwnode = gc->irq.parent_domain->fwnode; |
|---|
| 1257 | + fwspec->param_count = 2; |
|---|
| 1258 | + fwspec->param[0] = parent_hwirq; |
|---|
| 1259 | + fwspec->param[1] = parent_type; |
|---|
| 1260 | + |
|---|
| 1261 | + return fwspec; |
|---|
| 1262 | +} |
|---|
| 1263 | +EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell); |
|---|
| 1264 | + |
|---|
| 1265 | +void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc, |
|---|
| 1266 | + unsigned int parent_hwirq, |
|---|
| 1267 | + unsigned int parent_type) |
|---|
| 1268 | +{ |
|---|
| 1269 | + struct irq_fwspec *fwspec; |
|---|
| 1270 | + |
|---|
| 1271 | + fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL); |
|---|
| 1272 | + if (!fwspec) |
|---|
| 1273 | + return NULL; |
|---|
| 1274 | + |
|---|
| 1275 | + fwspec->fwnode = gc->irq.parent_domain->fwnode; |
|---|
| 1276 | + fwspec->param_count = 4; |
|---|
| 1277 | + fwspec->param[0] = 0; |
|---|
| 1278 | + fwspec->param[1] = parent_hwirq; |
|---|
| 1279 | + fwspec->param[2] = 0; |
|---|
| 1280 | + fwspec->param[3] = parent_type; |
|---|
| 1281 | + |
|---|
| 1282 | + return fwspec; |
|---|
| 1283 | +} |
|---|
| 1284 | +EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell); |
|---|
| 1285 | + |
|---|
| 1286 | +#else |
|---|
| 1287 | + |
|---|
| 1288 | +static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) |
|---|
| 1289 | +{ |
|---|
| 1290 | + return -EINVAL; |
|---|
| 1291 | +} |
|---|
| 1292 | + |
|---|
| 1293 | +static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) |
|---|
| 1294 | +{ |
|---|
| 1295 | + return false; |
|---|
| 1296 | +} |
|---|
| 1297 | + |
|---|
| 1298 | +#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ |
|---|
| 1766 | 1299 | |
|---|
| 1767 | 1300 | /** |
|---|
| 1768 | 1301 | * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip |
|---|
| .. | .. |
|---|
| 1777 | 1310 | int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, |
|---|
| 1778 | 1311 | irq_hw_number_t hwirq) |
|---|
| 1779 | 1312 | { |
|---|
| 1780 | | - struct gpio_chip *chip = d->host_data; |
|---|
| 1781 | | - int err = 0; |
|---|
| 1313 | + struct gpio_chip *gc = d->host_data; |
|---|
| 1314 | + int ret = 0; |
|---|
| 1782 | 1315 | |
|---|
| 1783 | | - if (!gpiochip_irqchip_irq_valid(chip, hwirq)) |
|---|
| 1316 | + if (!gpiochip_irqchip_irq_valid(gc, hwirq)) |
|---|
| 1784 | 1317 | return -ENXIO; |
|---|
| 1785 | 1318 | |
|---|
| 1786 | | - irq_set_chip_data(irq, chip); |
|---|
| 1319 | + irq_set_chip_data(irq, gc); |
|---|
| 1787 | 1320 | /* |
|---|
| 1788 | 1321 | * This lock class tells lockdep that GPIO irqs are in a different |
|---|
| 1789 | 1322 | * category than their parents, so it won't report false recursion. |
|---|
| 1790 | 1323 | */ |
|---|
| 1791 | | - irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key); |
|---|
| 1792 | | - irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler); |
|---|
| 1324 | + irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); |
|---|
| 1325 | + irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler); |
|---|
| 1793 | 1326 | /* Chips that use nested thread handlers have them marked */ |
|---|
| 1794 | | - if (chip->irq.threaded) |
|---|
| 1327 | + if (gc->irq.threaded) |
|---|
| 1795 | 1328 | irq_set_nested_thread(irq, 1); |
|---|
| 1796 | 1329 | irq_set_noprobe(irq); |
|---|
| 1797 | 1330 | |
|---|
| 1798 | | - if (chip->irq.num_parents == 1) |
|---|
| 1799 | | - err = irq_set_parent(irq, chip->irq.parents[0]); |
|---|
| 1800 | | - else if (chip->irq.map) |
|---|
| 1801 | | - err = irq_set_parent(irq, chip->irq.map[hwirq]); |
|---|
| 1331 | + if (gc->irq.num_parents == 1) |
|---|
| 1332 | + ret = irq_set_parent(irq, gc->irq.parents[0]); |
|---|
| 1333 | + else if (gc->irq.map) |
|---|
| 1334 | + ret = irq_set_parent(irq, gc->irq.map[hwirq]); |
|---|
| 1802 | 1335 | |
|---|
| 1803 | | - if (err < 0) |
|---|
| 1804 | | - return err; |
|---|
| 1336 | + if (ret < 0) |
|---|
| 1337 | + return ret; |
|---|
| 1805 | 1338 | |
|---|
| 1806 | 1339 | /* |
|---|
| 1807 | 1340 | * No set-up of the hardware will happen if IRQ_TYPE_NONE |
|---|
| 1808 | 1341 | * is passed as default type. |
|---|
| 1809 | 1342 | */ |
|---|
| 1810 | | - if (chip->irq.default_type != IRQ_TYPE_NONE) |
|---|
| 1811 | | - irq_set_irq_type(irq, chip->irq.default_type); |
|---|
| 1343 | + if (gc->irq.default_type != IRQ_TYPE_NONE) |
|---|
| 1344 | + irq_set_irq_type(irq, gc->irq.default_type); |
|---|
| 1812 | 1345 | |
|---|
| 1813 | 1346 | return 0; |
|---|
| 1814 | 1347 | } |
|---|
| .. | .. |
|---|
| 1816 | 1349 | |
|---|
| 1817 | 1350 | void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) |
|---|
| 1818 | 1351 | { |
|---|
| 1819 | | - struct gpio_chip *chip = d->host_data; |
|---|
| 1352 | + struct gpio_chip *gc = d->host_data; |
|---|
| 1820 | 1353 | |
|---|
| 1821 | | - if (chip->irq.threaded) |
|---|
| 1354 | + if (gc->irq.threaded) |
|---|
| 1822 | 1355 | irq_set_nested_thread(irq, 0); |
|---|
| 1823 | 1356 | irq_set_chip_and_handler(irq, NULL, NULL); |
|---|
| 1824 | 1357 | irq_set_chip_data(irq, NULL); |
|---|
| .. | .. |
|---|
| 1832 | 1365 | .xlate = irq_domain_xlate_twocell, |
|---|
| 1833 | 1366 | }; |
|---|
| 1834 | 1367 | |
|---|
| 1368 | +/* |
|---|
| 1369 | + * TODO: move these activate/deactivate in under the hierarchicial |
|---|
| 1370 | + * irqchip implementation as static once SPMI and SSBI (all external |
|---|
| 1371 | + * users) are phased over. |
|---|
| 1372 | + */ |
|---|
| 1373 | +/** |
|---|
| 1374 | + * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ |
|---|
| 1375 | + * @domain: The IRQ domain used by this IRQ chip |
|---|
| 1376 | + * @data: Outermost irq_data associated with the IRQ |
|---|
| 1377 | + * @reserve: If set, only reserve an interrupt vector instead of assigning one |
|---|
| 1378 | + * |
|---|
| 1379 | + * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be |
|---|
| 1380 | + * used as the activate function for the &struct irq_domain_ops. The host_data |
|---|
| 1381 | + * for the IRQ domain must be the &struct gpio_chip. |
|---|
| 1382 | + */ |
|---|
| 1383 | +int gpiochip_irq_domain_activate(struct irq_domain *domain, |
|---|
| 1384 | + struct irq_data *data, bool reserve) |
|---|
| 1385 | +{ |
|---|
| 1386 | + struct gpio_chip *gc = domain->host_data; |
|---|
| 1387 | + |
|---|
| 1388 | + return gpiochip_lock_as_irq(gc, data->hwirq); |
|---|
| 1389 | +} |
|---|
| 1390 | +EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate); |
|---|
| 1391 | + |
|---|
| 1392 | +/** |
|---|
| 1393 | + * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ |
|---|
| 1394 | + * @domain: The IRQ domain used by this IRQ chip |
|---|
| 1395 | + * @data: Outermost irq_data associated with the IRQ |
|---|
| 1396 | + * |
|---|
| 1397 | + * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to |
|---|
| 1398 | + * be used as the deactivate function for the &struct irq_domain_ops. The |
|---|
| 1399 | + * host_data for the IRQ domain must be the &struct gpio_chip. |
|---|
| 1400 | + */ |
|---|
| 1401 | +void gpiochip_irq_domain_deactivate(struct irq_domain *domain, |
|---|
| 1402 | + struct irq_data *data) |
|---|
| 1403 | +{ |
|---|
| 1404 | + struct gpio_chip *gc = domain->host_data; |
|---|
| 1405 | + |
|---|
| 1406 | + return gpiochip_unlock_as_irq(gc, data->hwirq); |
|---|
| 1407 | +} |
|---|
| 1408 | +EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate); |
|---|
| 1409 | + |
|---|
| 1410 | +static int gpiochip_to_irq(struct gpio_chip *gc, unsigned offset) |
|---|
| 1411 | +{ |
|---|
| 1412 | + struct irq_domain *domain = gc->irq.domain; |
|---|
| 1413 | + |
|---|
| 1414 | +#ifdef CONFIG_GPIOLIB_IRQCHIP |
|---|
| 1415 | + /* |
|---|
| 1416 | + * Avoid race condition with other code, which tries to lookup |
|---|
| 1417 | + * an IRQ before the irqchip has been properly registered, |
|---|
| 1418 | + * i.e. while gpiochip is still being brought up. |
|---|
| 1419 | + */ |
|---|
| 1420 | + if (!gc->irq.initialized) |
|---|
| 1421 | + return -EPROBE_DEFER; |
|---|
| 1422 | +#endif |
|---|
| 1423 | + |
|---|
| 1424 | + if (!gpiochip_irqchip_irq_valid(gc, offset)) |
|---|
| 1425 | + return -ENXIO; |
|---|
| 1426 | + |
|---|
| 1427 | +#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
|---|
| 1428 | + if (irq_domain_is_hierarchy(domain)) { |
|---|
| 1429 | + struct irq_fwspec spec; |
|---|
| 1430 | + |
|---|
| 1431 | + spec.fwnode = domain->fwnode; |
|---|
| 1432 | + spec.param_count = 2; |
|---|
| 1433 | + spec.param[0] = gc->irq.child_offset_to_irq(gc, offset); |
|---|
| 1434 | + spec.param[1] = IRQ_TYPE_NONE; |
|---|
| 1435 | + |
|---|
| 1436 | + return irq_create_fwspec_mapping(&spec); |
|---|
| 1437 | + } |
|---|
| 1438 | +#endif |
|---|
| 1439 | + |
|---|
| 1440 | + return irq_create_mapping(domain, offset); |
|---|
| 1441 | +} |
|---|
| 1442 | + |
|---|
| 1835 | 1443 | static int gpiochip_irq_reqres(struct irq_data *d) |
|---|
| 1836 | 1444 | { |
|---|
| 1837 | | - struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
|---|
| 1838 | | - int ret; |
|---|
| 1445 | + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
|---|
| 1839 | 1446 | |
|---|
| 1840 | | - if (!try_module_get(chip->gpiodev->owner)) |
|---|
| 1841 | | - return -ENODEV; |
|---|
| 1842 | | - |
|---|
| 1843 | | - ret = gpiochip_lock_as_irq(chip, d->hwirq); |
|---|
| 1844 | | - if (ret) { |
|---|
| 1845 | | - chip_err(chip, |
|---|
| 1846 | | - "unable to lock HW IRQ %lu for IRQ\n", |
|---|
| 1847 | | - d->hwirq); |
|---|
| 1848 | | - module_put(chip->gpiodev->owner); |
|---|
| 1849 | | - return ret; |
|---|
| 1850 | | - } |
|---|
| 1851 | | - return 0; |
|---|
| 1447 | + return gpiochip_reqres_irq(gc, d->hwirq); |
|---|
| 1852 | 1448 | } |
|---|
| 1853 | 1449 | |
|---|
| 1854 | 1450 | static void gpiochip_irq_relres(struct irq_data *d) |
|---|
| 1855 | 1451 | { |
|---|
| 1856 | | - struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
|---|
| 1452 | + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
|---|
| 1857 | 1453 | |
|---|
| 1858 | | - gpiochip_unlock_as_irq(chip, d->hwirq); |
|---|
| 1859 | | - module_put(chip->gpiodev->owner); |
|---|
| 1454 | + gpiochip_relres_irq(gc, d->hwirq); |
|---|
| 1860 | 1455 | } |
|---|
| 1861 | 1456 | |
|---|
| 1862 | | -static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset) |
|---|
| 1457 | +static void gpiochip_irq_mask(struct irq_data *d) |
|---|
| 1863 | 1458 | { |
|---|
| 1864 | | - if (!gpiochip_irqchip_irq_valid(chip, offset)) |
|---|
| 1865 | | - return -ENXIO; |
|---|
| 1459 | + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
|---|
| 1866 | 1460 | |
|---|
| 1867 | | - return irq_create_mapping(chip->irq.domain, offset); |
|---|
| 1461 | + if (gc->irq.irq_mask) |
|---|
| 1462 | + gc->irq.irq_mask(d); |
|---|
| 1463 | + gpiochip_disable_irq(gc, d->hwirq); |
|---|
| 1464 | +} |
|---|
| 1465 | + |
|---|
| 1466 | +static void gpiochip_irq_unmask(struct irq_data *d) |
|---|
| 1467 | +{ |
|---|
| 1468 | + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
|---|
| 1469 | + |
|---|
| 1470 | + gpiochip_enable_irq(gc, d->hwirq); |
|---|
| 1471 | + if (gc->irq.irq_unmask) |
|---|
| 1472 | + gc->irq.irq_unmask(d); |
|---|
| 1473 | +} |
|---|
| 1474 | + |
|---|
| 1475 | +static void gpiochip_irq_enable(struct irq_data *d) |
|---|
| 1476 | +{ |
|---|
| 1477 | + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
|---|
| 1478 | + |
|---|
| 1479 | + gpiochip_enable_irq(gc, d->hwirq); |
|---|
| 1480 | + gc->irq.irq_enable(d); |
|---|
| 1481 | +} |
|---|
| 1482 | + |
|---|
| 1483 | +static void gpiochip_irq_disable(struct irq_data *d) |
|---|
| 1484 | +{ |
|---|
| 1485 | + struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
|---|
| 1486 | + |
|---|
| 1487 | + gc->irq.irq_disable(d); |
|---|
| 1488 | + gpiochip_disable_irq(gc, d->hwirq); |
|---|
| 1489 | +} |
|---|
| 1490 | + |
|---|
| 1491 | +static void gpiochip_set_irq_hooks(struct gpio_chip *gc) |
|---|
| 1492 | +{ |
|---|
| 1493 | + struct irq_chip *irqchip = gc->irq.chip; |
|---|
| 1494 | + |
|---|
| 1495 | + if (!irqchip->irq_request_resources && |
|---|
| 1496 | + !irqchip->irq_release_resources) { |
|---|
| 1497 | + irqchip->irq_request_resources = gpiochip_irq_reqres; |
|---|
| 1498 | + irqchip->irq_release_resources = gpiochip_irq_relres; |
|---|
| 1499 | + } |
|---|
| 1500 | + if (WARN_ON(gc->irq.irq_enable)) |
|---|
| 1501 | + return; |
|---|
| 1502 | + /* Check if the irqchip already has this hook... */ |
|---|
| 1503 | + if (irqchip->irq_enable == gpiochip_irq_enable || |
|---|
| 1504 | + irqchip->irq_mask == gpiochip_irq_mask) { |
|---|
| 1505 | + /* |
|---|
| 1506 | + * ...and if so, give a gentle warning that this is bad |
|---|
| 1507 | + * practice. |
|---|
| 1508 | + */ |
|---|
| 1509 | + chip_info(gc, |
|---|
| 1510 | + "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n"); |
|---|
| 1511 | + return; |
|---|
| 1512 | + } |
|---|
| 1513 | + |
|---|
| 1514 | + if (irqchip->irq_disable) { |
|---|
| 1515 | + gc->irq.irq_disable = irqchip->irq_disable; |
|---|
| 1516 | + irqchip->irq_disable = gpiochip_irq_disable; |
|---|
| 1517 | + } else { |
|---|
| 1518 | + gc->irq.irq_mask = irqchip->irq_mask; |
|---|
| 1519 | + irqchip->irq_mask = gpiochip_irq_mask; |
|---|
| 1520 | + } |
|---|
| 1521 | + |
|---|
| 1522 | + if (irqchip->irq_enable) { |
|---|
| 1523 | + gc->irq.irq_enable = irqchip->irq_enable; |
|---|
| 1524 | + irqchip->irq_enable = gpiochip_irq_enable; |
|---|
| 1525 | + } else { |
|---|
| 1526 | + gc->irq.irq_unmask = irqchip->irq_unmask; |
|---|
| 1527 | + irqchip->irq_unmask = gpiochip_irq_unmask; |
|---|
| 1528 | + } |
|---|
| 1868 | 1529 | } |
|---|
| 1869 | 1530 | |
|---|
| 1870 | 1531 | /** |
|---|
| 1871 | 1532 | * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip |
|---|
| 1872 | | - * @gpiochip: the GPIO chip to add the IRQ chip to |
|---|
| 1533 | + * @gc: the GPIO chip to add the IRQ chip to |
|---|
| 1873 | 1534 | * @lock_key: lockdep class for IRQ lock |
|---|
| 1874 | 1535 | * @request_key: lockdep class for IRQ request |
|---|
| 1875 | 1536 | */ |
|---|
| 1876 | | -static int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
|---|
| 1537 | +static int gpiochip_add_irqchip(struct gpio_chip *gc, |
|---|
| 1877 | 1538 | struct lock_class_key *lock_key, |
|---|
| 1878 | 1539 | struct lock_class_key *request_key) |
|---|
| 1879 | 1540 | { |
|---|
| 1880 | | - struct irq_chip *irqchip = gpiochip->irq.chip; |
|---|
| 1881 | | - const struct irq_domain_ops *ops; |
|---|
| 1541 | + struct irq_chip *irqchip = gc->irq.chip; |
|---|
| 1542 | + const struct irq_domain_ops *ops = NULL; |
|---|
| 1882 | 1543 | struct device_node *np; |
|---|
| 1883 | 1544 | unsigned int type; |
|---|
| 1884 | 1545 | unsigned int i; |
|---|
| .. | .. |
|---|
| 1886 | 1547 | if (!irqchip) |
|---|
| 1887 | 1548 | return 0; |
|---|
| 1888 | 1549 | |
|---|
| 1889 | | - if (gpiochip->irq.parent_handler && gpiochip->can_sleep) { |
|---|
| 1890 | | - chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n"); |
|---|
| 1550 | + if (gc->irq.parent_handler && gc->can_sleep) { |
|---|
| 1551 | + chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n"); |
|---|
| 1891 | 1552 | return -EINVAL; |
|---|
| 1892 | 1553 | } |
|---|
| 1893 | 1554 | |
|---|
| 1894 | | - np = gpiochip->gpiodev->dev.of_node; |
|---|
| 1895 | | - type = gpiochip->irq.default_type; |
|---|
| 1555 | + np = gc->gpiodev->dev.of_node; |
|---|
| 1556 | + type = gc->irq.default_type; |
|---|
| 1896 | 1557 | |
|---|
| 1897 | 1558 | /* |
|---|
| 1898 | 1559 | * Specifying a default trigger is a terrible idea if DT or ACPI is |
|---|
| .. | .. |
|---|
| 1903 | 1564 | "%s: Ignoring %u default trigger\n", np->full_name, type)) |
|---|
| 1904 | 1565 | type = IRQ_TYPE_NONE; |
|---|
| 1905 | 1566 | |
|---|
| 1906 | | - if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { |
|---|
| 1907 | | - acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), |
|---|
| 1567 | + if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { |
|---|
| 1568 | + acpi_handle_warn(ACPI_HANDLE(gc->parent), |
|---|
| 1908 | 1569 | "Ignoring %u default trigger\n", type); |
|---|
| 1909 | 1570 | type = IRQ_TYPE_NONE; |
|---|
| 1910 | 1571 | } |
|---|
| 1911 | 1572 | |
|---|
| 1912 | | -#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
|---|
| 1913 | | - if (!gpiochip->to_irq) |
|---|
| 1914 | | -#endif |
|---|
| 1915 | | - gpiochip->to_irq = gpiochip_to_irq; |
|---|
| 1573 | + gc->to_irq = gpiochip_to_irq; |
|---|
| 1574 | + gc->irq.default_type = type; |
|---|
| 1575 | + gc->irq.lock_key = lock_key; |
|---|
| 1576 | + gc->irq.request_key = request_key; |
|---|
| 1916 | 1577 | |
|---|
| 1917 | | - gpiochip->irq.default_type = type; |
|---|
| 1918 | | - gpiochip->irq.lock_key = lock_key; |
|---|
| 1919 | | - gpiochip->irq.request_key = request_key; |
|---|
| 1578 | + /* If a parent irqdomain is provided, let's build a hierarchy */ |
|---|
| 1579 | + if (gpiochip_hierarchy_is_hierarchical(gc)) { |
|---|
| 1580 | + int ret = gpiochip_hierarchy_add_domain(gc); |
|---|
| 1581 | + if (ret) |
|---|
| 1582 | + return ret; |
|---|
| 1583 | + } else { |
|---|
| 1584 | + /* Some drivers provide custom irqdomain ops */ |
|---|
| 1585 | + if (gc->irq.domain_ops) |
|---|
| 1586 | + ops = gc->irq.domain_ops; |
|---|
| 1920 | 1587 | |
|---|
| 1921 | | - if (gpiochip->irq.domain_ops) |
|---|
| 1922 | | - ops = gpiochip->irq.domain_ops; |
|---|
| 1923 | | - else |
|---|
| 1924 | | - ops = &gpiochip_domain_ops; |
|---|
| 1925 | | - |
|---|
| 1926 | | -#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
|---|
| 1927 | | - if (gpiochip->irq.parent_domain) |
|---|
| 1928 | | - gpiochip->irq.domain = irq_domain_add_hierarchy(gpiochip->irq.parent_domain, |
|---|
| 1929 | | - 0, gpiochip->ngpio, |
|---|
| 1930 | | - np, ops, gpiochip); |
|---|
| 1931 | | - else |
|---|
| 1932 | | -#endif |
|---|
| 1933 | | - gpiochip->irq.domain = irq_domain_add_simple(np, gpiochip->ngpio, |
|---|
| 1934 | | - gpiochip->irq.first, |
|---|
| 1935 | | - ops, gpiochip); |
|---|
| 1936 | | - if (!gpiochip->irq.domain) |
|---|
| 1937 | | - return -EINVAL; |
|---|
| 1938 | | - |
|---|
| 1939 | | - /* |
|---|
| 1940 | | - * It is possible for a driver to override this, but only if the |
|---|
| 1941 | | - * alternative functions are both implemented. |
|---|
| 1942 | | - */ |
|---|
| 1943 | | - if (!irqchip->irq_request_resources && |
|---|
| 1944 | | - !irqchip->irq_release_resources) { |
|---|
| 1945 | | - irqchip->irq_request_resources = gpiochip_irq_reqres; |
|---|
| 1946 | | - irqchip->irq_release_resources = gpiochip_irq_relres; |
|---|
| 1588 | + if (!ops) |
|---|
| 1589 | + ops = &gpiochip_domain_ops; |
|---|
| 1590 | + gc->irq.domain = irq_domain_add_simple(np, |
|---|
| 1591 | + gc->ngpio, |
|---|
| 1592 | + gc->irq.first, |
|---|
| 1593 | + ops, gc); |
|---|
| 1594 | + if (!gc->irq.domain) |
|---|
| 1595 | + return -EINVAL; |
|---|
| 1947 | 1596 | } |
|---|
| 1948 | 1597 | |
|---|
| 1949 | | - if (gpiochip->irq.parent_handler) { |
|---|
| 1950 | | - void *data = gpiochip->irq.parent_handler_data ?: gpiochip; |
|---|
| 1598 | + if (gc->irq.parent_handler) { |
|---|
| 1599 | + void *data = gc->irq.parent_handler_data ?: gc; |
|---|
| 1951 | 1600 | |
|---|
| 1952 | | - for (i = 0; i < gpiochip->irq.num_parents; i++) { |
|---|
| 1601 | + for (i = 0; i < gc->irq.num_parents; i++) { |
|---|
| 1953 | 1602 | /* |
|---|
| 1954 | 1603 | * The parent IRQ chip is already using the chip_data |
|---|
| 1955 | 1604 | * for this IRQ chip, so our callbacks simply use the |
|---|
| 1956 | 1605 | * handler_data. |
|---|
| 1957 | 1606 | */ |
|---|
| 1958 | | - irq_set_chained_handler_and_data(gpiochip->irq.parents[i], |
|---|
| 1959 | | - gpiochip->irq.parent_handler, |
|---|
| 1607 | + irq_set_chained_handler_and_data(gc->irq.parents[i], |
|---|
| 1608 | + gc->irq.parent_handler, |
|---|
| 1960 | 1609 | data); |
|---|
| 1961 | 1610 | } |
|---|
| 1962 | 1611 | } |
|---|
| 1963 | 1612 | |
|---|
| 1964 | | - acpi_gpiochip_request_interrupts(gpiochip); |
|---|
| 1613 | + gpiochip_set_irq_hooks(gc); |
|---|
| 1614 | + |
|---|
| 1615 | + /* |
|---|
| 1616 | + * Using barrier() here to prevent compiler from reordering |
|---|
| 1617 | + * gc->irq.initialized before initialization of above |
|---|
| 1618 | + * GPIO chip irq members. |
|---|
| 1619 | + */ |
|---|
| 1620 | + barrier(); |
|---|
| 1621 | + |
|---|
| 1622 | + gc->irq.initialized = true; |
|---|
| 1623 | + |
|---|
| 1624 | + acpi_gpiochip_request_interrupts(gc); |
|---|
| 1965 | 1625 | |
|---|
| 1966 | 1626 | return 0; |
|---|
| 1967 | 1627 | } |
|---|
| 1968 | 1628 | |
|---|
| 1969 | 1629 | /** |
|---|
| 1970 | 1630 | * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip |
|---|
| 1971 | | - * @gpiochip: the gpiochip to remove the irqchip from |
|---|
| 1631 | + * @gc: the gpiochip to remove the irqchip from |
|---|
| 1972 | 1632 | * |
|---|
| 1973 | 1633 | * This is called only from gpiochip_remove() |
|---|
| 1974 | 1634 | */ |
|---|
| 1975 | | -static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) |
|---|
| 1635 | +static void gpiochip_irqchip_remove(struct gpio_chip *gc) |
|---|
| 1976 | 1636 | { |
|---|
| 1637 | + struct irq_chip *irqchip = gc->irq.chip; |
|---|
| 1977 | 1638 | unsigned int offset; |
|---|
| 1978 | 1639 | |
|---|
| 1979 | | - acpi_gpiochip_free_interrupts(gpiochip); |
|---|
| 1640 | + acpi_gpiochip_free_interrupts(gc); |
|---|
| 1980 | 1641 | |
|---|
| 1981 | | - if (gpiochip->irq.chip && gpiochip->irq.parent_handler) { |
|---|
| 1982 | | - struct gpio_irq_chip *irq = &gpiochip->irq; |
|---|
| 1642 | + if (irqchip && gc->irq.parent_handler) { |
|---|
| 1643 | + struct gpio_irq_chip *irq = &gc->irq; |
|---|
| 1983 | 1644 | unsigned int i; |
|---|
| 1984 | 1645 | |
|---|
| 1985 | 1646 | for (i = 0; i < irq->num_parents; i++) |
|---|
| .. | .. |
|---|
| 1988 | 1649 | } |
|---|
| 1989 | 1650 | |
|---|
| 1990 | 1651 | /* Remove all IRQ mappings and delete the domain */ |
|---|
| 1991 | | - if (gpiochip->irq.domain) { |
|---|
| 1652 | + if (gc->irq.domain) { |
|---|
| 1992 | 1653 | unsigned int irq; |
|---|
| 1993 | 1654 | |
|---|
| 1994 | | - for (offset = 0; offset < gpiochip->ngpio; offset++) { |
|---|
| 1995 | | - if (!gpiochip_irqchip_irq_valid(gpiochip, offset)) |
|---|
| 1655 | + for (offset = 0; offset < gc->ngpio; offset++) { |
|---|
| 1656 | + if (!gpiochip_irqchip_irq_valid(gc, offset)) |
|---|
| 1996 | 1657 | continue; |
|---|
| 1997 | 1658 | |
|---|
| 1998 | | - irq = irq_find_mapping(gpiochip->irq.domain, offset); |
|---|
| 1659 | + irq = irq_find_mapping(gc->irq.domain, offset); |
|---|
| 1999 | 1660 | irq_dispose_mapping(irq); |
|---|
| 2000 | 1661 | } |
|---|
| 2001 | 1662 | |
|---|
| 2002 | | - irq_domain_remove(gpiochip->irq.domain); |
|---|
| 1663 | + irq_domain_remove(gc->irq.domain); |
|---|
| 2003 | 1664 | } |
|---|
| 2004 | 1665 | |
|---|
| 2005 | | - if (gpiochip->irq.chip) { |
|---|
| 2006 | | - gpiochip->irq.chip->irq_request_resources = NULL; |
|---|
| 2007 | | - gpiochip->irq.chip->irq_release_resources = NULL; |
|---|
| 2008 | | - gpiochip->irq.chip = NULL; |
|---|
| 1666 | + if (irqchip) { |
|---|
| 1667 | + if (irqchip->irq_request_resources == gpiochip_irq_reqres) { |
|---|
| 1668 | + irqchip->irq_request_resources = NULL; |
|---|
| 1669 | + irqchip->irq_release_resources = NULL; |
|---|
| 1670 | + } |
|---|
| 1671 | + if (irqchip->irq_enable == gpiochip_irq_enable) { |
|---|
| 1672 | + irqchip->irq_enable = gc->irq.irq_enable; |
|---|
| 1673 | + irqchip->irq_disable = gc->irq.irq_disable; |
|---|
| 1674 | + } |
|---|
| 2009 | 1675 | } |
|---|
| 1676 | + gc->irq.irq_enable = NULL; |
|---|
| 1677 | + gc->irq.irq_disable = NULL; |
|---|
| 1678 | + gc->irq.chip = NULL; |
|---|
| 2010 | 1679 | |
|---|
| 2011 | | - gpiochip_irqchip_free_valid_mask(gpiochip); |
|---|
| 1680 | + gpiochip_irqchip_free_valid_mask(gc); |
|---|
| 2012 | 1681 | } |
|---|
| 2013 | 1682 | |
|---|
| 2014 | 1683 | /** |
|---|
| 2015 | 1684 | * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip |
|---|
| 2016 | | - * @gpiochip: the gpiochip to add the irqchip to |
|---|
| 1685 | + * @gc: the gpiochip to add the irqchip to |
|---|
| 2017 | 1686 | * @irqchip: the irqchip to add to the gpiochip |
|---|
| 2018 | 1687 | * @first_irq: if not dynamically assigned, the base (first) IRQ to |
|---|
| 2019 | 1688 | * allocate gpiochip irqs from |
|---|
| .. | .. |
|---|
| 2038 | 1707 | * the pins on the gpiochip can generate a unique IRQ. Everything else |
|---|
| 2039 | 1708 | * need to be open coded. |
|---|
| 2040 | 1709 | */ |
|---|
| 2041 | | -int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip, |
|---|
| 1710 | +int gpiochip_irqchip_add_key(struct gpio_chip *gc, |
|---|
| 2042 | 1711 | struct irq_chip *irqchip, |
|---|
| 2043 | 1712 | unsigned int first_irq, |
|---|
| 2044 | 1713 | irq_flow_handler_t handler, |
|---|
| .. | .. |
|---|
| 2049 | 1718 | { |
|---|
| 2050 | 1719 | struct device_node *of_node; |
|---|
| 2051 | 1720 | |
|---|
| 2052 | | - if (!gpiochip || !irqchip) |
|---|
| 1721 | + if (!gc || !irqchip) |
|---|
| 2053 | 1722 | return -EINVAL; |
|---|
| 2054 | 1723 | |
|---|
| 2055 | | - if (!gpiochip->parent) { |
|---|
| 2056 | | - pr_err("missing gpiochip .dev parent pointer\n"); |
|---|
| 1724 | + if (!gc->parent) { |
|---|
| 1725 | + chip_err(gc, "missing gpiochip .dev parent pointer\n"); |
|---|
| 2057 | 1726 | return -EINVAL; |
|---|
| 2058 | 1727 | } |
|---|
| 2059 | | - gpiochip->irq.threaded = threaded; |
|---|
| 2060 | | - of_node = gpiochip->parent->of_node; |
|---|
| 1728 | + gc->irq.threaded = threaded; |
|---|
| 1729 | + of_node = gc->parent->of_node; |
|---|
| 2061 | 1730 | #ifdef CONFIG_OF_GPIO |
|---|
| 2062 | 1731 | /* |
|---|
| 2063 | 1732 | * If the gpiochip has an assigned OF node this takes precedence |
|---|
| 2064 | | - * FIXME: get rid of this and use gpiochip->parent->of_node |
|---|
| 1733 | + * FIXME: get rid of this and use gc->parent->of_node |
|---|
| 2065 | 1734 | * everywhere |
|---|
| 2066 | 1735 | */ |
|---|
| 2067 | | - if (gpiochip->of_node) |
|---|
| 2068 | | - of_node = gpiochip->of_node; |
|---|
| 1736 | + if (gc->of_node) |
|---|
| 1737 | + of_node = gc->of_node; |
|---|
| 2069 | 1738 | #endif |
|---|
| 2070 | 1739 | /* |
|---|
| 2071 | 1740 | * Specifying a default trigger is a terrible idea if DT or ACPI is |
|---|
| .. | .. |
|---|
| 2075 | 1744 | if (WARN(of_node && type != IRQ_TYPE_NONE, |
|---|
| 2076 | 1745 | "%pOF: Ignoring %d default trigger\n", of_node, type)) |
|---|
| 2077 | 1746 | type = IRQ_TYPE_NONE; |
|---|
| 2078 | | - if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) { |
|---|
| 2079 | | - acpi_handle_warn(ACPI_HANDLE(gpiochip->parent), |
|---|
| 1747 | + if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { |
|---|
| 1748 | + acpi_handle_warn(ACPI_HANDLE(gc->parent), |
|---|
| 2080 | 1749 | "Ignoring %d default trigger\n", type); |
|---|
| 2081 | 1750 | type = IRQ_TYPE_NONE; |
|---|
| 2082 | 1751 | } |
|---|
| 2083 | 1752 | |
|---|
| 2084 | | - gpiochip->irq.chip = irqchip; |
|---|
| 2085 | | - gpiochip->irq.handler = handler; |
|---|
| 2086 | | - gpiochip->irq.default_type = type; |
|---|
| 2087 | | - gpiochip->to_irq = gpiochip_to_irq; |
|---|
| 2088 | | - gpiochip->irq.lock_key = lock_key; |
|---|
| 2089 | | - gpiochip->irq.request_key = request_key; |
|---|
| 2090 | | - gpiochip->irq.domain = irq_domain_add_simple(of_node, |
|---|
| 2091 | | - gpiochip->ngpio, first_irq, |
|---|
| 2092 | | - &gpiochip_domain_ops, gpiochip); |
|---|
| 2093 | | - if (!gpiochip->irq.domain) { |
|---|
| 2094 | | - gpiochip->irq.chip = NULL; |
|---|
| 1753 | + gc->irq.chip = irqchip; |
|---|
| 1754 | + gc->irq.handler = handler; |
|---|
| 1755 | + gc->irq.default_type = type; |
|---|
| 1756 | + gc->to_irq = gpiochip_to_irq; |
|---|
| 1757 | + gc->irq.lock_key = lock_key; |
|---|
| 1758 | + gc->irq.request_key = request_key; |
|---|
| 1759 | + gc->irq.domain = irq_domain_add_simple(of_node, |
|---|
| 1760 | + gc->ngpio, first_irq, |
|---|
| 1761 | + &gpiochip_domain_ops, gc); |
|---|
| 1762 | + if (!gc->irq.domain) { |
|---|
| 1763 | + gc->irq.chip = NULL; |
|---|
| 2095 | 1764 | return -EINVAL; |
|---|
| 2096 | 1765 | } |
|---|
| 2097 | 1766 | |
|---|
| 2098 | | - /* |
|---|
| 2099 | | - * It is possible for a driver to override this, but only if the |
|---|
| 2100 | | - * alternative functions are both implemented. |
|---|
| 2101 | | - */ |
|---|
| 2102 | | - if (!irqchip->irq_request_resources && |
|---|
| 2103 | | - !irqchip->irq_release_resources) { |
|---|
| 2104 | | - irqchip->irq_request_resources = gpiochip_irq_reqres; |
|---|
| 2105 | | - irqchip->irq_release_resources = gpiochip_irq_relres; |
|---|
| 2106 | | - } |
|---|
| 1767 | + gpiochip_set_irq_hooks(gc); |
|---|
| 2107 | 1768 | |
|---|
| 2108 | | - acpi_gpiochip_request_interrupts(gpiochip); |
|---|
| 1769 | + acpi_gpiochip_request_interrupts(gc); |
|---|
| 2109 | 1770 | |
|---|
| 2110 | 1771 | return 0; |
|---|
| 2111 | 1772 | } |
|---|
| 2112 | 1773 | EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); |
|---|
| 2113 | 1774 | |
|---|
| 1775 | +/** |
|---|
| 1776 | + * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip |
|---|
| 1777 | + * @gc: the gpiochip to add the irqchip to |
|---|
| 1778 | + * @domain: the irqdomain to add to the gpiochip |
|---|
| 1779 | + * |
|---|
| 1780 | + * This function adds an IRQ domain to the gpiochip. |
|---|
| 1781 | + */ |
|---|
| 1782 | +int gpiochip_irqchip_add_domain(struct gpio_chip *gc, |
|---|
| 1783 | + struct irq_domain *domain) |
|---|
| 1784 | +{ |
|---|
| 1785 | + if (!domain) |
|---|
| 1786 | + return -EINVAL; |
|---|
| 1787 | + |
|---|
| 1788 | + gc->to_irq = gpiochip_to_irq; |
|---|
| 1789 | + gc->irq.domain = domain; |
|---|
| 1790 | + |
|---|
| 1791 | + return 0; |
|---|
| 1792 | +} |
|---|
| 1793 | +EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain); |
|---|
| 1794 | + |
|---|
| 2114 | 1795 | #else /* CONFIG_GPIOLIB_IRQCHIP */ |
|---|
| 2115 | 1796 | |
|---|
| 2116 | | -static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip, |
|---|
| 1797 | +static inline int gpiochip_add_irqchip(struct gpio_chip *gc, |
|---|
| 2117 | 1798 | struct lock_class_key *lock_key, |
|---|
| 2118 | 1799 | struct lock_class_key *request_key) |
|---|
| 2119 | 1800 | { |
|---|
| 2120 | 1801 | return 0; |
|---|
| 2121 | 1802 | } |
|---|
| 1803 | +static void gpiochip_irqchip_remove(struct gpio_chip *gc) {} |
|---|
| 2122 | 1804 | |
|---|
| 2123 | | -static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {} |
|---|
| 2124 | | -static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip) |
|---|
| 1805 | +static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc) |
|---|
| 2125 | 1806 | { |
|---|
| 2126 | 1807 | return 0; |
|---|
| 2127 | 1808 | } |
|---|
| 2128 | | -static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip) |
|---|
| 1809 | + |
|---|
| 1810 | +static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc) |
|---|
| 1811 | +{ |
|---|
| 1812 | + return 0; |
|---|
| 1813 | +} |
|---|
| 1814 | +static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc) |
|---|
| 2129 | 1815 | { } |
|---|
| 2130 | 1816 | |
|---|
| 2131 | 1817 | #endif /* CONFIG_GPIOLIB_IRQCHIP */ |
|---|
| 2132 | 1818 | |
|---|
| 2133 | 1819 | /** |
|---|
| 2134 | 1820 | * gpiochip_generic_request() - request the gpio function for a pin |
|---|
| 2135 | | - * @chip: the gpiochip owning the GPIO |
|---|
| 1821 | + * @gc: the gpiochip owning the GPIO |
|---|
| 2136 | 1822 | * @offset: the offset of the GPIO to request for GPIO function |
|---|
| 2137 | 1823 | */ |
|---|
| 2138 | | -int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset) |
|---|
| 1824 | +int gpiochip_generic_request(struct gpio_chip *gc, unsigned offset) |
|---|
| 2139 | 1825 | { |
|---|
| 2140 | | - return pinctrl_gpio_request(chip->gpiodev->base + offset); |
|---|
| 1826 | +#ifdef CONFIG_PINCTRL |
|---|
| 1827 | + if (list_empty(&gc->gpiodev->pin_ranges)) |
|---|
| 1828 | + return 0; |
|---|
| 1829 | +#endif |
|---|
| 1830 | + |
|---|
| 1831 | + return pinctrl_gpio_request(gc->gpiodev->base + offset); |
|---|
| 2141 | 1832 | } |
|---|
| 2142 | 1833 | EXPORT_SYMBOL_GPL(gpiochip_generic_request); |
|---|
| 2143 | 1834 | |
|---|
| 2144 | 1835 | /** |
|---|
| 2145 | 1836 | * gpiochip_generic_free() - free the gpio function from a pin |
|---|
| 2146 | | - * @chip: the gpiochip to request the gpio function for |
|---|
| 1837 | + * @gc: the gpiochip to request the gpio function for |
|---|
| 2147 | 1838 | * @offset: the offset of the GPIO to free from GPIO function |
|---|
| 2148 | 1839 | */ |
|---|
| 2149 | | -void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset) |
|---|
| 1840 | +void gpiochip_generic_free(struct gpio_chip *gc, unsigned offset) |
|---|
| 2150 | 1841 | { |
|---|
| 2151 | | - pinctrl_gpio_free(chip->gpiodev->base + offset); |
|---|
| 1842 | +#ifdef CONFIG_PINCTRL |
|---|
| 1843 | + if (list_empty(&gc->gpiodev->pin_ranges)) |
|---|
| 1844 | + return; |
|---|
| 1845 | +#endif |
|---|
| 1846 | + |
|---|
| 1847 | + pinctrl_gpio_free(gc->gpiodev->base + offset); |
|---|
| 2152 | 1848 | } |
|---|
| 2153 | 1849 | EXPORT_SYMBOL_GPL(gpiochip_generic_free); |
|---|
| 2154 | 1850 | |
|---|
| 2155 | 1851 | /** |
|---|
| 2156 | 1852 | * gpiochip_generic_config() - apply configuration for a pin |
|---|
| 2157 | | - * @chip: the gpiochip owning the GPIO |
|---|
| 1853 | + * @gc: the gpiochip owning the GPIO |
|---|
| 2158 | 1854 | * @offset: the offset of the GPIO to apply the configuration |
|---|
| 2159 | 1855 | * @config: the configuration to be applied |
|---|
| 2160 | 1856 | */ |
|---|
| 2161 | | -int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset, |
|---|
| 1857 | +int gpiochip_generic_config(struct gpio_chip *gc, unsigned offset, |
|---|
| 2162 | 1858 | unsigned long config) |
|---|
| 2163 | 1859 | { |
|---|
| 2164 | | - return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config); |
|---|
| 1860 | + return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config); |
|---|
| 2165 | 1861 | } |
|---|
| 2166 | 1862 | EXPORT_SYMBOL_GPL(gpiochip_generic_config); |
|---|
| 2167 | 1863 | |
|---|
| .. | .. |
|---|
| 2169 | 1865 | |
|---|
| 2170 | 1866 | /** |
|---|
| 2171 | 1867 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping |
|---|
| 2172 | | - * @chip: the gpiochip to add the range for |
|---|
| 1868 | + * @gc: the gpiochip to add the range for |
|---|
| 2173 | 1869 | * @pctldev: the pin controller to map to |
|---|
| 2174 | 1870 | * @gpio_offset: the start offset in the current gpio_chip number space |
|---|
| 2175 | 1871 | * @pin_group: name of the pin group inside the pin controller |
|---|
| .. | .. |
|---|
| 2179 | 1875 | * Documentation/devicetree/bindings/gpio/gpio.txt on how to |
|---|
| 2180 | 1876 | * bind pinctrl and gpio drivers via the "gpio-ranges" property. |
|---|
| 2181 | 1877 | */ |
|---|
| 2182 | | -int gpiochip_add_pingroup_range(struct gpio_chip *chip, |
|---|
| 1878 | +int gpiochip_add_pingroup_range(struct gpio_chip *gc, |
|---|
| 2183 | 1879 | struct pinctrl_dev *pctldev, |
|---|
| 2184 | 1880 | unsigned int gpio_offset, const char *pin_group) |
|---|
| 2185 | 1881 | { |
|---|
| 2186 | 1882 | struct gpio_pin_range *pin_range; |
|---|
| 2187 | | - struct gpio_device *gdev = chip->gpiodev; |
|---|
| 1883 | + struct gpio_device *gdev = gc->gpiodev; |
|---|
| 2188 | 1884 | int ret; |
|---|
| 2189 | 1885 | |
|---|
| 2190 | 1886 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
|---|
| 2191 | 1887 | if (!pin_range) { |
|---|
| 2192 | | - chip_err(chip, "failed to allocate pin ranges\n"); |
|---|
| 1888 | + chip_err(gc, "failed to allocate pin ranges\n"); |
|---|
| 2193 | 1889 | return -ENOMEM; |
|---|
| 2194 | 1890 | } |
|---|
| 2195 | 1891 | |
|---|
| 2196 | 1892 | /* Use local offset as range ID */ |
|---|
| 2197 | 1893 | pin_range->range.id = gpio_offset; |
|---|
| 2198 | | - pin_range->range.gc = chip; |
|---|
| 2199 | | - pin_range->range.name = chip->label; |
|---|
| 1894 | + pin_range->range.gc = gc; |
|---|
| 1895 | + pin_range->range.name = gc->label; |
|---|
| 2200 | 1896 | pin_range->range.base = gdev->base + gpio_offset; |
|---|
| 2201 | 1897 | pin_range->pctldev = pctldev; |
|---|
| 2202 | 1898 | |
|---|
| .. | .. |
|---|
| 2210 | 1906 | |
|---|
| 2211 | 1907 | pinctrl_add_gpio_range(pctldev, &pin_range->range); |
|---|
| 2212 | 1908 | |
|---|
| 2213 | | - chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
|---|
| 1909 | + chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n", |
|---|
| 2214 | 1910 | gpio_offset, gpio_offset + pin_range->range.npins - 1, |
|---|
| 2215 | 1911 | pinctrl_dev_get_devname(pctldev), pin_group); |
|---|
| 2216 | 1912 | |
|---|
| .. | .. |
|---|
| 2222 | 1918 | |
|---|
| 2223 | 1919 | /** |
|---|
| 2224 | 1920 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping |
|---|
| 2225 | | - * @chip: the gpiochip to add the range for |
|---|
| 1921 | + * @gc: the gpiochip to add the range for |
|---|
| 2226 | 1922 | * @pinctl_name: the dev_name() of the pin controller to map to |
|---|
| 2227 | 1923 | * @gpio_offset: the start offset in the current gpio_chip number space |
|---|
| 2228 | 1924 | * @pin_offset: the start offset in the pin controller number space |
|---|
| .. | .. |
|---|
| 2237 | 1933 | * Documentation/devicetree/bindings/gpio/gpio.txt on how to |
|---|
| 2238 | 1934 | * bind pinctrl and gpio drivers via the "gpio-ranges" property. |
|---|
| 2239 | 1935 | */ |
|---|
| 2240 | | -int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
|---|
| 1936 | +int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name, |
|---|
| 2241 | 1937 | unsigned int gpio_offset, unsigned int pin_offset, |
|---|
| 2242 | 1938 | unsigned int npins) |
|---|
| 2243 | 1939 | { |
|---|
| 2244 | 1940 | struct gpio_pin_range *pin_range; |
|---|
| 2245 | | - struct gpio_device *gdev = chip->gpiodev; |
|---|
| 1941 | + struct gpio_device *gdev = gc->gpiodev; |
|---|
| 2246 | 1942 | int ret; |
|---|
| 2247 | 1943 | |
|---|
| 2248 | 1944 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
|---|
| 2249 | 1945 | if (!pin_range) { |
|---|
| 2250 | | - chip_err(chip, "failed to allocate pin ranges\n"); |
|---|
| 1946 | + chip_err(gc, "failed to allocate pin ranges\n"); |
|---|
| 2251 | 1947 | return -ENOMEM; |
|---|
| 2252 | 1948 | } |
|---|
| 2253 | 1949 | |
|---|
| 2254 | 1950 | /* Use local offset as range ID */ |
|---|
| 2255 | 1951 | pin_range->range.id = gpio_offset; |
|---|
| 2256 | | - pin_range->range.gc = chip; |
|---|
| 2257 | | - pin_range->range.name = chip->label; |
|---|
| 1952 | + pin_range->range.gc = gc; |
|---|
| 1953 | + pin_range->range.name = gc->label; |
|---|
| 2258 | 1954 | pin_range->range.base = gdev->base + gpio_offset; |
|---|
| 2259 | 1955 | pin_range->range.pin_base = pin_offset; |
|---|
| 2260 | 1956 | pin_range->range.npins = npins; |
|---|
| .. | .. |
|---|
| 2262 | 1958 | &pin_range->range); |
|---|
| 2263 | 1959 | if (IS_ERR(pin_range->pctldev)) { |
|---|
| 2264 | 1960 | ret = PTR_ERR(pin_range->pctldev); |
|---|
| 2265 | | - chip_err(chip, "could not create pin range\n"); |
|---|
| 1961 | + chip_err(gc, "could not create pin range\n"); |
|---|
| 2266 | 1962 | kfree(pin_range); |
|---|
| 2267 | 1963 | return ret; |
|---|
| 2268 | 1964 | } |
|---|
| 2269 | | - chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
|---|
| 1965 | + chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n", |
|---|
| 2270 | 1966 | gpio_offset, gpio_offset + npins - 1, |
|---|
| 2271 | 1967 | pinctl_name, |
|---|
| 2272 | 1968 | pin_offset, pin_offset + npins - 1); |
|---|
| .. | .. |
|---|
| 2279 | 1975 | |
|---|
| 2280 | 1976 | /** |
|---|
| 2281 | 1977 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings |
|---|
| 2282 | | - * @chip: the chip to remove all the mappings for |
|---|
| 1978 | + * @gc: the chip to remove all the mappings for |
|---|
| 2283 | 1979 | */ |
|---|
| 2284 | | -void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
|---|
| 1980 | +void gpiochip_remove_pin_ranges(struct gpio_chip *gc) |
|---|
| 2285 | 1981 | { |
|---|
| 2286 | 1982 | struct gpio_pin_range *pin_range, *tmp; |
|---|
| 2287 | | - struct gpio_device *gdev = chip->gpiodev; |
|---|
| 1983 | + struct gpio_device *gdev = gc->gpiodev; |
|---|
| 2288 | 1984 | |
|---|
| 2289 | 1985 | list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { |
|---|
| 2290 | 1986 | list_del(&pin_range->node); |
|---|
| .. | .. |
|---|
| 2303 | 1999 | */ |
|---|
| 2304 | 2000 | static int gpiod_request_commit(struct gpio_desc *desc, const char *label) |
|---|
| 2305 | 2001 | { |
|---|
| 2306 | | - struct gpio_chip *chip = desc->gdev->chip; |
|---|
| 2307 | | - int status; |
|---|
| 2002 | + struct gpio_chip *gc = desc->gdev->chip; |
|---|
| 2003 | + int ret; |
|---|
| 2308 | 2004 | unsigned long flags; |
|---|
| 2309 | 2005 | unsigned offset; |
|---|
| 2310 | 2006 | |
|---|
| .. | .. |
|---|
| 2322 | 2018 | |
|---|
| 2323 | 2019 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { |
|---|
| 2324 | 2020 | desc_set_label(desc, label ? : "?"); |
|---|
| 2325 | | - status = 0; |
|---|
| 2021 | + ret = 0; |
|---|
| 2326 | 2022 | } else { |
|---|
| 2327 | 2023 | kfree_const(label); |
|---|
| 2328 | | - status = -EBUSY; |
|---|
| 2024 | + ret = -EBUSY; |
|---|
| 2329 | 2025 | goto done; |
|---|
| 2330 | 2026 | } |
|---|
| 2331 | 2027 | |
|---|
| 2332 | | - if (chip->request) { |
|---|
| 2333 | | - /* chip->request may sleep */ |
|---|
| 2028 | + if (gc->request) { |
|---|
| 2029 | + /* gc->request may sleep */ |
|---|
| 2334 | 2030 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 2335 | 2031 | offset = gpio_chip_hwgpio(desc); |
|---|
| 2336 | | - if (gpiochip_line_is_valid(chip, offset)) |
|---|
| 2337 | | - status = chip->request(chip, offset); |
|---|
| 2032 | + if (gpiochip_line_is_valid(gc, offset)) |
|---|
| 2033 | + ret = gc->request(gc, offset); |
|---|
| 2338 | 2034 | else |
|---|
| 2339 | | - status = -EINVAL; |
|---|
| 2035 | + ret = -EINVAL; |
|---|
| 2340 | 2036 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 2341 | 2037 | |
|---|
| 2342 | | - if (status < 0) { |
|---|
| 2038 | + if (ret < 0) { |
|---|
| 2343 | 2039 | desc_set_label(desc, NULL); |
|---|
| 2344 | 2040 | kfree_const(label); |
|---|
| 2345 | 2041 | clear_bit(FLAG_REQUESTED, &desc->flags); |
|---|
| 2346 | 2042 | goto done; |
|---|
| 2347 | 2043 | } |
|---|
| 2348 | 2044 | } |
|---|
| 2349 | | - if (chip->get_direction) { |
|---|
| 2350 | | - /* chip->get_direction may sleep */ |
|---|
| 2045 | + if (gc->get_direction) { |
|---|
| 2046 | + /* gc->get_direction may sleep */ |
|---|
| 2351 | 2047 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 2352 | 2048 | gpiod_get_direction(desc); |
|---|
| 2353 | 2049 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 2354 | 2050 | } |
|---|
| 2355 | 2051 | done: |
|---|
| 2356 | 2052 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 2357 | | - return status; |
|---|
| 2053 | + return ret; |
|---|
| 2358 | 2054 | } |
|---|
| 2359 | 2055 | |
|---|
| 2360 | 2056 | /* |
|---|
| .. | .. |
|---|
| 2397 | 2093 | |
|---|
| 2398 | 2094 | int gpiod_request(struct gpio_desc *desc, const char *label) |
|---|
| 2399 | 2095 | { |
|---|
| 2400 | | - int status = -EPROBE_DEFER; |
|---|
| 2096 | + int ret = -EPROBE_DEFER; |
|---|
| 2401 | 2097 | struct gpio_device *gdev; |
|---|
| 2402 | 2098 | |
|---|
| 2403 | 2099 | VALIDATE_DESC(desc); |
|---|
| 2404 | 2100 | gdev = desc->gdev; |
|---|
| 2405 | 2101 | |
|---|
| 2406 | 2102 | if (try_module_get(gdev->owner)) { |
|---|
| 2407 | | - status = gpiod_request_commit(desc, label); |
|---|
| 2408 | | - if (status < 0) |
|---|
| 2103 | + ret = gpiod_request_commit(desc, label); |
|---|
| 2104 | + if (ret < 0) |
|---|
| 2409 | 2105 | module_put(gdev->owner); |
|---|
| 2410 | 2106 | else |
|---|
| 2411 | 2107 | get_device(&gdev->dev); |
|---|
| 2412 | 2108 | } |
|---|
| 2413 | 2109 | |
|---|
| 2414 | | - if (status) |
|---|
| 2415 | | - gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
|---|
| 2110 | + if (ret) |
|---|
| 2111 | + gpiod_dbg(desc, "%s: status %d\n", __func__, ret); |
|---|
| 2416 | 2112 | |
|---|
| 2417 | | - return status; |
|---|
| 2113 | + return ret; |
|---|
| 2418 | 2114 | } |
|---|
| 2419 | 2115 | |
|---|
| 2420 | 2116 | static bool gpiod_free_commit(struct gpio_desc *desc) |
|---|
| 2421 | 2117 | { |
|---|
| 2422 | 2118 | bool ret = false; |
|---|
| 2423 | 2119 | unsigned long flags; |
|---|
| 2424 | | - struct gpio_chip *chip; |
|---|
| 2120 | + struct gpio_chip *gc; |
|---|
| 2425 | 2121 | |
|---|
| 2426 | 2122 | might_sleep(); |
|---|
| 2427 | 2123 | |
|---|
| .. | .. |
|---|
| 2429 | 2125 | |
|---|
| 2430 | 2126 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 2431 | 2127 | |
|---|
| 2432 | | - chip = desc->gdev->chip; |
|---|
| 2433 | | - if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { |
|---|
| 2434 | | - if (chip->free) { |
|---|
| 2128 | + gc = desc->gdev->chip; |
|---|
| 2129 | + if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) { |
|---|
| 2130 | + if (gc->free) { |
|---|
| 2435 | 2131 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 2436 | | - might_sleep_if(chip->can_sleep); |
|---|
| 2437 | | - chip->free(chip, gpio_chip_hwgpio(desc)); |
|---|
| 2132 | + might_sleep_if(gc->can_sleep); |
|---|
| 2133 | + gc->free(gc, gpio_chip_hwgpio(desc)); |
|---|
| 2438 | 2134 | spin_lock_irqsave(&gpio_lock, flags); |
|---|
| 2439 | 2135 | } |
|---|
| 2440 | 2136 | kfree_const(desc->label); |
|---|
| .. | .. |
|---|
| 2443 | 2139 | clear_bit(FLAG_REQUESTED, &desc->flags); |
|---|
| 2444 | 2140 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
|---|
| 2445 | 2141 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
|---|
| 2142 | + clear_bit(FLAG_PULL_UP, &desc->flags); |
|---|
| 2143 | + clear_bit(FLAG_PULL_DOWN, &desc->flags); |
|---|
| 2144 | + clear_bit(FLAG_BIAS_DISABLE, &desc->flags); |
|---|
| 2145 | + clear_bit(FLAG_EDGE_RISING, &desc->flags); |
|---|
| 2146 | + clear_bit(FLAG_EDGE_FALLING, &desc->flags); |
|---|
| 2446 | 2147 | clear_bit(FLAG_IS_HOGGED, &desc->flags); |
|---|
| 2148 | +#ifdef CONFIG_OF_DYNAMIC |
|---|
| 2149 | + desc->hog = NULL; |
|---|
| 2150 | +#endif |
|---|
| 2151 | +#ifdef CONFIG_GPIO_CDEV |
|---|
| 2152 | + WRITE_ONCE(desc->debounce_period_us, 0); |
|---|
| 2153 | +#endif |
|---|
| 2447 | 2154 | ret = true; |
|---|
| 2448 | 2155 | } |
|---|
| 2449 | 2156 | |
|---|
| 2450 | 2157 | spin_unlock_irqrestore(&gpio_lock, flags); |
|---|
| 2158 | + blocking_notifier_call_chain(&desc->gdev->notifier, |
|---|
| 2159 | + GPIOLINE_CHANGED_RELEASED, desc); |
|---|
| 2160 | + |
|---|
| 2451 | 2161 | return ret; |
|---|
| 2452 | 2162 | } |
|---|
| 2453 | 2163 | |
|---|
| .. | .. |
|---|
| 2463 | 2173 | |
|---|
| 2464 | 2174 | /** |
|---|
| 2465 | 2175 | * gpiochip_is_requested - return string iff signal was requested |
|---|
| 2466 | | - * @chip: controller managing the signal |
|---|
| 2176 | + * @gc: controller managing the signal |
|---|
| 2467 | 2177 | * @offset: of signal within controller's 0..(ngpio - 1) range |
|---|
| 2468 | 2178 | * |
|---|
| 2469 | 2179 | * Returns NULL if the GPIO is not currently requested, else a string. |
|---|
| .. | .. |
|---|
| 2474 | 2184 | * help with diagnostics, and knowing that the signal is used as a GPIO |
|---|
| 2475 | 2185 | * can help avoid accidentally multiplexing it to another controller. |
|---|
| 2476 | 2186 | */ |
|---|
| 2477 | | -const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) |
|---|
| 2187 | +const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned offset) |
|---|
| 2478 | 2188 | { |
|---|
| 2479 | 2189 | struct gpio_desc *desc; |
|---|
| 2480 | 2190 | |
|---|
| 2481 | | - if (offset >= chip->ngpio) |
|---|
| 2191 | + if (offset >= gc->ngpio) |
|---|
| 2482 | 2192 | return NULL; |
|---|
| 2483 | 2193 | |
|---|
| 2484 | | - desc = &chip->gpiodev->descs[offset]; |
|---|
| 2194 | + desc = gpiochip_get_desc(gc, offset); |
|---|
| 2195 | + if (IS_ERR(desc)) |
|---|
| 2196 | + return NULL; |
|---|
| 2485 | 2197 | |
|---|
| 2486 | 2198 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
|---|
| 2487 | 2199 | return NULL; |
|---|
| .. | .. |
|---|
| 2491 | 2203 | |
|---|
| 2492 | 2204 | /** |
|---|
| 2493 | 2205 | * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor |
|---|
| 2494 | | - * @chip: GPIO chip |
|---|
| 2206 | + * @gc: GPIO chip |
|---|
| 2495 | 2207 | * @hwnum: hardware number of the GPIO for which to request the descriptor |
|---|
| 2496 | 2208 | * @label: label for the GPIO |
|---|
| 2209 | + * @lflags: lookup flags for this GPIO or 0 if default, this can be used to |
|---|
| 2210 | + * specify things like line inversion semantics with the machine flags |
|---|
| 2211 | + * such as GPIO_OUT_LOW |
|---|
| 2212 | + * @dflags: descriptor request flags for this GPIO or 0 if default, this |
|---|
| 2213 | + * can be used to specify consumer semantics such as open drain |
|---|
| 2497 | 2214 | * |
|---|
| 2498 | 2215 | * Function allows GPIO chip drivers to request and use their own GPIO |
|---|
| 2499 | 2216 | * descriptors via gpiolib API. Difference to gpiod_request() is that this |
|---|
| .. | .. |
|---|
| 2505 | 2222 | * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error |
|---|
| 2506 | 2223 | * code on failure. |
|---|
| 2507 | 2224 | */ |
|---|
| 2508 | | -struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum, |
|---|
| 2509 | | - const char *label) |
|---|
| 2225 | +struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc, |
|---|
| 2226 | + unsigned int hwnum, |
|---|
| 2227 | + const char *label, |
|---|
| 2228 | + enum gpio_lookup_flags lflags, |
|---|
| 2229 | + enum gpiod_flags dflags) |
|---|
| 2510 | 2230 | { |
|---|
| 2511 | | - struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum); |
|---|
| 2512 | | - int err; |
|---|
| 2231 | + struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum); |
|---|
| 2232 | + int ret; |
|---|
| 2513 | 2233 | |
|---|
| 2514 | 2234 | if (IS_ERR(desc)) { |
|---|
| 2515 | | - chip_err(chip, "failed to get GPIO descriptor\n"); |
|---|
| 2235 | + chip_err(gc, "failed to get GPIO descriptor\n"); |
|---|
| 2516 | 2236 | return desc; |
|---|
| 2517 | 2237 | } |
|---|
| 2518 | 2238 | |
|---|
| 2519 | | - err = gpiod_request_commit(desc, label); |
|---|
| 2520 | | - if (err < 0) |
|---|
| 2521 | | - return ERR_PTR(err); |
|---|
| 2239 | + ret = gpiod_request_commit(desc, label); |
|---|
| 2240 | + if (ret < 0) |
|---|
| 2241 | + return ERR_PTR(ret); |
|---|
| 2242 | + |
|---|
| 2243 | + ret = gpiod_configure_flags(desc, label, lflags, dflags); |
|---|
| 2244 | + if (ret) { |
|---|
| 2245 | + chip_err(gc, "setup of own GPIO %s failed\n", label); |
|---|
| 2246 | + gpiod_free_commit(desc); |
|---|
| 2247 | + return ERR_PTR(ret); |
|---|
| 2248 | + } |
|---|
| 2522 | 2249 | |
|---|
| 2523 | 2250 | return desc; |
|---|
| 2524 | 2251 | } |
|---|
| .. | .. |
|---|
| 2548 | 2275 | * rely on gpio_request() having been called beforehand. |
|---|
| 2549 | 2276 | */ |
|---|
| 2550 | 2277 | |
|---|
| 2278 | +static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset, |
|---|
| 2279 | + unsigned long config) |
|---|
| 2280 | +{ |
|---|
| 2281 | + if (!gc->set_config) |
|---|
| 2282 | + return -ENOTSUPP; |
|---|
| 2283 | + |
|---|
| 2284 | + return gc->set_config(gc, offset, config); |
|---|
| 2285 | +} |
|---|
| 2286 | + |
|---|
| 2287 | +static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode) |
|---|
| 2288 | +{ |
|---|
| 2289 | + struct gpio_chip *gc = desc->gdev->chip; |
|---|
| 2290 | + unsigned long config; |
|---|
| 2291 | + unsigned arg; |
|---|
| 2292 | + |
|---|
| 2293 | + switch (mode) { |
|---|
| 2294 | + case PIN_CONFIG_BIAS_PULL_DOWN: |
|---|
| 2295 | + case PIN_CONFIG_BIAS_PULL_UP: |
|---|
| 2296 | + arg = 1; |
|---|
| 2297 | + break; |
|---|
| 2298 | + |
|---|
| 2299 | + default: |
|---|
| 2300 | + arg = 0; |
|---|
| 2301 | + } |
|---|
| 2302 | + |
|---|
| 2303 | + config = PIN_CONF_PACKED(mode, arg); |
|---|
| 2304 | + return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config); |
|---|
| 2305 | +} |
|---|
| 2306 | + |
|---|
| 2307 | +static int gpio_set_bias(struct gpio_desc *desc) |
|---|
| 2308 | +{ |
|---|
| 2309 | + int bias = 0; |
|---|
| 2310 | + int ret = 0; |
|---|
| 2311 | + |
|---|
| 2312 | + if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) |
|---|
| 2313 | + bias = PIN_CONFIG_BIAS_DISABLE; |
|---|
| 2314 | + else if (test_bit(FLAG_PULL_UP, &desc->flags)) |
|---|
| 2315 | + bias = PIN_CONFIG_BIAS_PULL_UP; |
|---|
| 2316 | + else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) |
|---|
| 2317 | + bias = PIN_CONFIG_BIAS_PULL_DOWN; |
|---|
| 2318 | + |
|---|
| 2319 | + if (bias) { |
|---|
| 2320 | + ret = gpio_set_config(desc, bias); |
|---|
| 2321 | + if (ret != -ENOTSUPP) |
|---|
| 2322 | + return ret; |
|---|
| 2323 | + } |
|---|
| 2324 | + return 0; |
|---|
| 2325 | +} |
|---|
| 2326 | + |
|---|
| 2551 | 2327 | /** |
|---|
| 2552 | 2328 | * gpiod_direction_input - set the GPIO direction to input |
|---|
| 2553 | 2329 | * @desc: GPIO to set to input |
|---|
| .. | .. |
|---|
| 2559 | 2335 | */ |
|---|
| 2560 | 2336 | int gpiod_direction_input(struct gpio_desc *desc) |
|---|
| 2561 | 2337 | { |
|---|
| 2562 | | - struct gpio_chip *chip; |
|---|
| 2563 | | - int status = 0; |
|---|
| 2338 | + struct gpio_chip *gc; |
|---|
| 2339 | + int ret = 0; |
|---|
| 2564 | 2340 | |
|---|
| 2565 | 2341 | VALIDATE_DESC(desc); |
|---|
| 2566 | | - chip = desc->gdev->chip; |
|---|
| 2342 | + gc = desc->gdev->chip; |
|---|
| 2567 | 2343 | |
|---|
| 2568 | | - if (!chip->get && chip->direction_input) { |
|---|
| 2344 | + /* |
|---|
| 2345 | + * It is legal to have no .get() and .direction_input() specified if |
|---|
| 2346 | + * the chip is output-only, but you can't specify .direction_input() |
|---|
| 2347 | + * and not support the .get() operation, that doesn't make sense. |
|---|
| 2348 | + */ |
|---|
| 2349 | + if (!gc->get && gc->direction_input) { |
|---|
| 2569 | 2350 | gpiod_warn(desc, |
|---|
| 2570 | | - "%s: missing get() and direction_input() operations\n", |
|---|
| 2571 | | - __func__); |
|---|
| 2351 | + "%s: missing get() but have direction_input()\n", |
|---|
| 2352 | + __func__); |
|---|
| 2572 | 2353 | return -EIO; |
|---|
| 2573 | 2354 | } |
|---|
| 2574 | 2355 | |
|---|
| 2575 | | - if (chip->direction_input) { |
|---|
| 2576 | | - status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
|---|
| 2577 | | - } else if (chip->get_direction && |
|---|
| 2578 | | - (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) { |
|---|
| 2356 | + /* |
|---|
| 2357 | + * If we have a .direction_input() callback, things are simple, |
|---|
| 2358 | + * just call it. Else we are some input-only chip so try to check the |
|---|
| 2359 | + * direction (if .get_direction() is supported) else we silently |
|---|
| 2360 | + * assume we are in input mode after this. |
|---|
| 2361 | + */ |
|---|
| 2362 | + if (gc->direction_input) { |
|---|
| 2363 | + ret = gc->direction_input(gc, gpio_chip_hwgpio(desc)); |
|---|
| 2364 | + } else if (gc->get_direction && |
|---|
| 2365 | + (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) { |
|---|
| 2579 | 2366 | gpiod_warn(desc, |
|---|
| 2580 | | - "%s: missing direction_input() operation\n", |
|---|
| 2581 | | - __func__); |
|---|
| 2367 | + "%s: missing direction_input() operation and line is output\n", |
|---|
| 2368 | + __func__); |
|---|
| 2582 | 2369 | return -EIO; |
|---|
| 2583 | 2370 | } |
|---|
| 2584 | | - if (status == 0) |
|---|
| 2371 | + if (ret == 0) { |
|---|
| 2585 | 2372 | clear_bit(FLAG_IS_OUT, &desc->flags); |
|---|
| 2373 | + ret = gpio_set_bias(desc); |
|---|
| 2374 | + } |
|---|
| 2586 | 2375 | |
|---|
| 2587 | | - trace_gpio_direction(desc_to_gpio(desc), 1, status); |
|---|
| 2376 | + trace_gpio_direction(desc_to_gpio(desc), 1, ret); |
|---|
| 2588 | 2377 | |
|---|
| 2589 | | - return status; |
|---|
| 2378 | + return ret; |
|---|
| 2590 | 2379 | } |
|---|
| 2591 | 2380 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
|---|
| 2592 | | - |
|---|
| 2593 | | -static int gpio_set_drive_single_ended(struct gpio_chip *gc, unsigned offset, |
|---|
| 2594 | | - enum pin_config_param mode) |
|---|
| 2595 | | -{ |
|---|
| 2596 | | - unsigned long config = { PIN_CONF_PACKED(mode, 0) }; |
|---|
| 2597 | | - |
|---|
| 2598 | | - return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP; |
|---|
| 2599 | | -} |
|---|
| 2600 | 2381 | |
|---|
| 2601 | 2382 | static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) |
|---|
| 2602 | 2383 | { |
|---|
| .. | .. |
|---|
| 2604 | 2385 | int val = !!value; |
|---|
| 2605 | 2386 | int ret = 0; |
|---|
| 2606 | 2387 | |
|---|
| 2388 | + /* |
|---|
| 2389 | + * It's OK not to specify .direction_output() if the gpiochip is |
|---|
| 2390 | + * output-only, but if there is then not even a .set() operation it |
|---|
| 2391 | + * is pretty tricky to drive the output line. |
|---|
| 2392 | + */ |
|---|
| 2607 | 2393 | if (!gc->set && !gc->direction_output) { |
|---|
| 2608 | 2394 | gpiod_warn(desc, |
|---|
| 2609 | | - "%s: missing set() and direction_output() operations\n", |
|---|
| 2610 | | - __func__); |
|---|
| 2395 | + "%s: missing set() and direction_output() operations\n", |
|---|
| 2396 | + __func__); |
|---|
| 2611 | 2397 | return -EIO; |
|---|
| 2612 | 2398 | } |
|---|
| 2613 | 2399 | |
|---|
| 2614 | 2400 | if (gc->direction_output) { |
|---|
| 2615 | 2401 | ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); |
|---|
| 2616 | 2402 | } else { |
|---|
| 2403 | + /* Check that we are in output mode if we can */ |
|---|
| 2617 | 2404 | if (gc->get_direction && |
|---|
| 2618 | 2405 | gc->get_direction(gc, gpio_chip_hwgpio(desc))) { |
|---|
| 2619 | 2406 | gpiod_warn(desc, |
|---|
| .. | .. |
|---|
| 2621 | 2408 | __func__); |
|---|
| 2622 | 2409 | return -EIO; |
|---|
| 2623 | 2410 | } |
|---|
| 2411 | + /* |
|---|
| 2412 | + * If we can't actively set the direction, we are some |
|---|
| 2413 | + * output-only chip, so just drive the output as desired. |
|---|
| 2414 | + */ |
|---|
| 2624 | 2415 | gc->set(gc, gpio_chip_hwgpio(desc), val); |
|---|
| 2625 | 2416 | } |
|---|
| 2626 | 2417 | |
|---|
| .. | .. |
|---|
| 2663 | 2454 | */ |
|---|
| 2664 | 2455 | int gpiod_direction_output(struct gpio_desc *desc, int value) |
|---|
| 2665 | 2456 | { |
|---|
| 2666 | | - struct gpio_chip *gc; |
|---|
| 2667 | 2457 | int ret; |
|---|
| 2668 | 2458 | |
|---|
| 2669 | 2459 | VALIDATE_DESC(desc); |
|---|
| .. | .. |
|---|
| 2672 | 2462 | else |
|---|
| 2673 | 2463 | value = !!value; |
|---|
| 2674 | 2464 | |
|---|
| 2675 | | - /* GPIOs used for IRQs shall not be set as output */ |
|---|
| 2676 | | - if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
|---|
| 2465 | + /* GPIOs used for enabled IRQs shall not be set as output */ |
|---|
| 2466 | + if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && |
|---|
| 2467 | + test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { |
|---|
| 2677 | 2468 | gpiod_err(desc, |
|---|
| 2678 | 2469 | "%s: tried to set a GPIO tied to an IRQ as output\n", |
|---|
| 2679 | 2470 | __func__); |
|---|
| 2680 | 2471 | return -EIO; |
|---|
| 2681 | 2472 | } |
|---|
| 2682 | 2473 | |
|---|
| 2683 | | - gc = desc->gdev->chip; |
|---|
| 2684 | 2474 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { |
|---|
| 2685 | 2475 | /* First see if we can enable open drain in hardware */ |
|---|
| 2686 | | - ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), |
|---|
| 2687 | | - PIN_CONFIG_DRIVE_OPEN_DRAIN); |
|---|
| 2476 | + ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN); |
|---|
| 2688 | 2477 | if (!ret) |
|---|
| 2689 | 2478 | goto set_output_value; |
|---|
| 2690 | 2479 | /* Emulate open drain by not actively driving the line high */ |
|---|
| .. | .. |
|---|
| 2692 | 2481 | ret = gpiod_direction_input(desc); |
|---|
| 2693 | 2482 | goto set_output_flag; |
|---|
| 2694 | 2483 | } |
|---|
| 2695 | | - } |
|---|
| 2696 | | - else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
|---|
| 2697 | | - ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), |
|---|
| 2698 | | - PIN_CONFIG_DRIVE_OPEN_SOURCE); |
|---|
| 2484 | + } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { |
|---|
| 2485 | + ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE); |
|---|
| 2699 | 2486 | if (!ret) |
|---|
| 2700 | 2487 | goto set_output_value; |
|---|
| 2701 | 2488 | /* Emulate open source by not actively driving the line low */ |
|---|
| .. | .. |
|---|
| 2704 | 2491 | goto set_output_flag; |
|---|
| 2705 | 2492 | } |
|---|
| 2706 | 2493 | } else { |
|---|
| 2707 | | - gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), |
|---|
| 2708 | | - PIN_CONFIG_DRIVE_PUSH_PULL); |
|---|
| 2494 | + gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL); |
|---|
| 2709 | 2495 | } |
|---|
| 2710 | 2496 | |
|---|
| 2711 | 2497 | set_output_value: |
|---|
| 2498 | + ret = gpio_set_bias(desc); |
|---|
| 2499 | + if (ret) |
|---|
| 2500 | + return ret; |
|---|
| 2712 | 2501 | return gpiod_direction_output_raw_commit(desc, value); |
|---|
| 2713 | 2502 | |
|---|
| 2714 | 2503 | set_output_flag: |
|---|
| .. | .. |
|---|
| 2725 | 2514 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
|---|
| 2726 | 2515 | |
|---|
| 2727 | 2516 | /** |
|---|
| 2517 | + * gpiod_set_config - sets @config for a GPIO |
|---|
| 2518 | + * @desc: descriptor of the GPIO for which to set the configuration |
|---|
| 2519 | + * @config: Same packed config format as generic pinconf |
|---|
| 2520 | + * |
|---|
| 2521 | + * Returns: |
|---|
| 2522 | + * 0 on success, %-ENOTSUPP if the controller doesn't support setting the |
|---|
| 2523 | + * configuration. |
|---|
| 2524 | + */ |
|---|
| 2525 | +int gpiod_set_config(struct gpio_desc *desc, unsigned long config) |
|---|
| 2526 | +{ |
|---|
| 2527 | + struct gpio_chip *gc; |
|---|
| 2528 | + |
|---|
| 2529 | + VALIDATE_DESC(desc); |
|---|
| 2530 | + gc = desc->gdev->chip; |
|---|
| 2531 | + |
|---|
| 2532 | + return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config); |
|---|
| 2533 | +} |
|---|
| 2534 | +EXPORT_SYMBOL_GPL(gpiod_set_config); |
|---|
| 2535 | + |
|---|
| 2536 | +/** |
|---|
| 2728 | 2537 | * gpiod_set_debounce - sets @debounce time for a GPIO |
|---|
| 2729 | 2538 | * @desc: descriptor of the GPIO for which to set debounce time |
|---|
| 2730 | 2539 | * @debounce: debounce time in microseconds |
|---|
| .. | .. |
|---|
| 2735 | 2544 | */ |
|---|
| 2736 | 2545 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
|---|
| 2737 | 2546 | { |
|---|
| 2738 | | - struct gpio_chip *chip; |
|---|
| 2739 | | - unsigned long config; |
|---|
| 2740 | | - |
|---|
| 2741 | | - VALIDATE_DESC(desc); |
|---|
| 2742 | | - chip = desc->gdev->chip; |
|---|
| 2743 | | - if (!chip->set || !chip->set_config) { |
|---|
| 2744 | | - gpiod_dbg(desc, |
|---|
| 2745 | | - "%s: missing set() or set_config() operations\n", |
|---|
| 2746 | | - __func__); |
|---|
| 2747 | | - return -ENOTSUPP; |
|---|
| 2748 | | - } |
|---|
| 2547 | + unsigned long config; |
|---|
| 2749 | 2548 | |
|---|
| 2750 | 2549 | config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce); |
|---|
| 2751 | | - return chip->set_config(chip, gpio_chip_hwgpio(desc), config); |
|---|
| 2550 | + return gpiod_set_config(desc, config); |
|---|
| 2752 | 2551 | } |
|---|
| 2753 | 2552 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
|---|
| 2754 | 2553 | |
|---|
| .. | .. |
|---|
| 2762 | 2561 | */ |
|---|
| 2763 | 2562 | int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) |
|---|
| 2764 | 2563 | { |
|---|
| 2765 | | - struct gpio_chip *chip; |
|---|
| 2564 | + struct gpio_chip *gc; |
|---|
| 2766 | 2565 | unsigned long packed; |
|---|
| 2767 | 2566 | int gpio; |
|---|
| 2768 | 2567 | int rc; |
|---|
| .. | .. |
|---|
| 2772 | 2571 | * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for |
|---|
| 2773 | 2572 | * persistence state. |
|---|
| 2774 | 2573 | */ |
|---|
| 2775 | | - if (transitory) |
|---|
| 2776 | | - set_bit(FLAG_TRANSITORY, &desc->flags); |
|---|
| 2777 | | - else |
|---|
| 2778 | | - clear_bit(FLAG_TRANSITORY, &desc->flags); |
|---|
| 2574 | + assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); |
|---|
| 2779 | 2575 | |
|---|
| 2780 | 2576 | /* If the driver supports it, set the persistence state now */ |
|---|
| 2781 | | - chip = desc->gdev->chip; |
|---|
| 2782 | | - if (!chip->set_config) |
|---|
| 2577 | + gc = desc->gdev->chip; |
|---|
| 2578 | + if (!gc->set_config) |
|---|
| 2783 | 2579 | return 0; |
|---|
| 2784 | 2580 | |
|---|
| 2785 | 2581 | packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE, |
|---|
| 2786 | 2582 | !transitory); |
|---|
| 2787 | 2583 | gpio = gpio_chip_hwgpio(desc); |
|---|
| 2788 | | - rc = chip->set_config(chip, gpio, packed); |
|---|
| 2584 | + rc = gpio_do_set_config(gc, gpio, packed); |
|---|
| 2789 | 2585 | if (rc == -ENOTSUPP) { |
|---|
| 2790 | 2586 | dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n", |
|---|
| 2791 | 2587 | gpio); |
|---|
| .. | .. |
|---|
| 2808 | 2604 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
|---|
| 2809 | 2605 | } |
|---|
| 2810 | 2606 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
|---|
| 2607 | + |
|---|
| 2608 | +/** |
|---|
| 2609 | + * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not |
|---|
| 2610 | + * @desc: the gpio descriptor to change |
|---|
| 2611 | + */ |
|---|
| 2612 | +void gpiod_toggle_active_low(struct gpio_desc *desc) |
|---|
| 2613 | +{ |
|---|
| 2614 | + VALIDATE_DESC_VOID(desc); |
|---|
| 2615 | + change_bit(FLAG_ACTIVE_LOW, &desc->flags); |
|---|
| 2616 | +} |
|---|
| 2617 | +EXPORT_SYMBOL_GPL(gpiod_toggle_active_low); |
|---|
| 2811 | 2618 | |
|---|
| 2812 | 2619 | /* I/O calls are only valid after configuration completed; the relevant |
|---|
| 2813 | 2620 | * "is this a valid GPIO" error checks should already have been done. |
|---|
| .. | .. |
|---|
| 2833 | 2640 | |
|---|
| 2834 | 2641 | static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) |
|---|
| 2835 | 2642 | { |
|---|
| 2836 | | - struct gpio_chip *chip; |
|---|
| 2643 | + struct gpio_chip *gc; |
|---|
| 2837 | 2644 | int offset; |
|---|
| 2838 | 2645 | int value; |
|---|
| 2839 | 2646 | |
|---|
| 2840 | | - chip = desc->gdev->chip; |
|---|
| 2647 | + gc = desc->gdev->chip; |
|---|
| 2841 | 2648 | offset = gpio_chip_hwgpio(desc); |
|---|
| 2842 | | - value = chip->get ? chip->get(chip, offset) : -EIO; |
|---|
| 2649 | + value = gc->get ? gc->get(gc, offset) : -EIO; |
|---|
| 2843 | 2650 | value = value < 0 ? value : !!value; |
|---|
| 2844 | 2651 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
|---|
| 2845 | 2652 | return value; |
|---|
| 2846 | 2653 | } |
|---|
| 2847 | 2654 | |
|---|
| 2848 | | -static int gpio_chip_get_multiple(struct gpio_chip *chip, |
|---|
| 2655 | +static int gpio_chip_get_multiple(struct gpio_chip *gc, |
|---|
| 2849 | 2656 | unsigned long *mask, unsigned long *bits) |
|---|
| 2850 | 2657 | { |
|---|
| 2851 | | - if (chip->get_multiple) { |
|---|
| 2852 | | - return chip->get_multiple(chip, mask, bits); |
|---|
| 2853 | | - } else if (chip->get) { |
|---|
| 2658 | + if (gc->get_multiple) |
|---|
| 2659 | + return gc->get_multiple(gc, mask, bits); |
|---|
| 2660 | + if (gc->get) { |
|---|
| 2854 | 2661 | int i, value; |
|---|
| 2855 | 2662 | |
|---|
| 2856 | | - for_each_set_bit(i, mask, chip->ngpio) { |
|---|
| 2857 | | - value = chip->get(chip, i); |
|---|
| 2663 | + for_each_set_bit(i, mask, gc->ngpio) { |
|---|
| 2664 | + value = gc->get(gc, i); |
|---|
| 2858 | 2665 | if (value < 0) |
|---|
| 2859 | 2666 | return value; |
|---|
| 2860 | 2667 | __assign_bit(i, bits, value); |
|---|
| .. | .. |
|---|
| 2867 | 2674 | int gpiod_get_array_value_complex(bool raw, bool can_sleep, |
|---|
| 2868 | 2675 | unsigned int array_size, |
|---|
| 2869 | 2676 | struct gpio_desc **desc_array, |
|---|
| 2870 | | - int *value_array) |
|---|
| 2677 | + struct gpio_array *array_info, |
|---|
| 2678 | + unsigned long *value_bitmap) |
|---|
| 2871 | 2679 | { |
|---|
| 2872 | | - int i = 0; |
|---|
| 2680 | + int ret, i = 0; |
|---|
| 2681 | + |
|---|
| 2682 | + /* |
|---|
| 2683 | + * Validate array_info against desc_array and its size. |
|---|
| 2684 | + * It should immediately follow desc_array if both |
|---|
| 2685 | + * have been obtained from the same gpiod_get_array() call. |
|---|
| 2686 | + */ |
|---|
| 2687 | + if (array_info && array_info->desc == desc_array && |
|---|
| 2688 | + array_size <= array_info->size && |
|---|
| 2689 | + (void *)array_info == desc_array + array_info->size) { |
|---|
| 2690 | + if (!can_sleep) |
|---|
| 2691 | + WARN_ON(array_info->chip->can_sleep); |
|---|
| 2692 | + |
|---|
| 2693 | + ret = gpio_chip_get_multiple(array_info->chip, |
|---|
| 2694 | + array_info->get_mask, |
|---|
| 2695 | + value_bitmap); |
|---|
| 2696 | + if (ret) |
|---|
| 2697 | + return ret; |
|---|
| 2698 | + |
|---|
| 2699 | + if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) |
|---|
| 2700 | + bitmap_xor(value_bitmap, value_bitmap, |
|---|
| 2701 | + array_info->invert_mask, array_size); |
|---|
| 2702 | + |
|---|
| 2703 | + i = find_first_zero_bit(array_info->get_mask, array_size); |
|---|
| 2704 | + if (i == array_size) |
|---|
| 2705 | + return 0; |
|---|
| 2706 | + } else { |
|---|
| 2707 | + array_info = NULL; |
|---|
| 2708 | + } |
|---|
| 2873 | 2709 | |
|---|
| 2874 | 2710 | while (i < array_size) { |
|---|
| 2875 | | - struct gpio_chip *chip = desc_array[i]->gdev->chip; |
|---|
| 2711 | + struct gpio_chip *gc = desc_array[i]->gdev->chip; |
|---|
| 2876 | 2712 | unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; |
|---|
| 2877 | 2713 | unsigned long *mask, *bits; |
|---|
| 2878 | 2714 | int first, j, ret; |
|---|
| 2879 | 2715 | |
|---|
| 2880 | | - if (likely(chip->ngpio <= FASTPATH_NGPIO)) { |
|---|
| 2716 | + if (likely(gc->ngpio <= FASTPATH_NGPIO)) { |
|---|
| 2881 | 2717 | mask = fastpath; |
|---|
| 2882 | 2718 | } else { |
|---|
| 2883 | | - mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio), |
|---|
| 2719 | + mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), |
|---|
| 2884 | 2720 | sizeof(*mask), |
|---|
| 2885 | 2721 | can_sleep ? GFP_KERNEL : GFP_ATOMIC); |
|---|
| 2886 | 2722 | if (!mask) |
|---|
| 2887 | 2723 | return -ENOMEM; |
|---|
| 2888 | 2724 | } |
|---|
| 2889 | 2725 | |
|---|
| 2890 | | - bits = mask + BITS_TO_LONGS(chip->ngpio); |
|---|
| 2891 | | - bitmap_zero(mask, chip->ngpio); |
|---|
| 2726 | + bits = mask + BITS_TO_LONGS(gc->ngpio); |
|---|
| 2727 | + bitmap_zero(mask, gc->ngpio); |
|---|
| 2892 | 2728 | |
|---|
| 2893 | 2729 | if (!can_sleep) |
|---|
| 2894 | | - WARN_ON(chip->can_sleep); |
|---|
| 2730 | + WARN_ON(gc->can_sleep); |
|---|
| 2895 | 2731 | |
|---|
| 2896 | 2732 | /* collect all inputs belonging to the same chip */ |
|---|
| 2897 | 2733 | first = i; |
|---|
| .. | .. |
|---|
| 2901 | 2737 | |
|---|
| 2902 | 2738 | __set_bit(hwgpio, mask); |
|---|
| 2903 | 2739 | i++; |
|---|
| 2904 | | - } while ((i < array_size) && |
|---|
| 2905 | | - (desc_array[i]->gdev->chip == chip)); |
|---|
| 2906 | 2740 | |
|---|
| 2907 | | - ret = gpio_chip_get_multiple(chip, mask, bits); |
|---|
| 2741 | + if (array_info) |
|---|
| 2742 | + i = find_next_zero_bit(array_info->get_mask, |
|---|
| 2743 | + array_size, i); |
|---|
| 2744 | + } while ((i < array_size) && |
|---|
| 2745 | + (desc_array[i]->gdev->chip == gc)); |
|---|
| 2746 | + |
|---|
| 2747 | + ret = gpio_chip_get_multiple(gc, mask, bits); |
|---|
| 2908 | 2748 | if (ret) { |
|---|
| 2909 | 2749 | if (mask != fastpath) |
|---|
| 2910 | 2750 | kfree(mask); |
|---|
| 2911 | 2751 | return ret; |
|---|
| 2912 | 2752 | } |
|---|
| 2913 | 2753 | |
|---|
| 2914 | | - for (j = first; j < i; j++) { |
|---|
| 2754 | + for (j = first; j < i; ) { |
|---|
| 2915 | 2755 | const struct gpio_desc *desc = desc_array[j]; |
|---|
| 2916 | 2756 | int hwgpio = gpio_chip_hwgpio(desc); |
|---|
| 2917 | 2757 | int value = test_bit(hwgpio, bits); |
|---|
| 2918 | 2758 | |
|---|
| 2919 | 2759 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
|---|
| 2920 | 2760 | value = !value; |
|---|
| 2921 | | - value_array[j] = value; |
|---|
| 2761 | + __assign_bit(j, value_bitmap, value); |
|---|
| 2922 | 2762 | trace_gpio_value(desc_to_gpio(desc), 1, value); |
|---|
| 2763 | + j++; |
|---|
| 2764 | + |
|---|
| 2765 | + if (array_info) |
|---|
| 2766 | + j = find_next_zero_bit(array_info->get_mask, i, |
|---|
| 2767 | + j); |
|---|
| 2923 | 2768 | } |
|---|
| 2924 | 2769 | |
|---|
| 2925 | 2770 | if (mask != fastpath) |
|---|
| .. | .. |
|---|
| 2935 | 2780 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
|---|
| 2936 | 2781 | * its ACTIVE_LOW status, or negative errno on failure. |
|---|
| 2937 | 2782 | * |
|---|
| 2938 | | - * This function should be called from contexts where we cannot sleep, and will |
|---|
| 2783 | + * This function can be called from contexts where we cannot sleep, and will |
|---|
| 2939 | 2784 | * complain if the GPIO chip functions potentially sleep. |
|---|
| 2940 | 2785 | */ |
|---|
| 2941 | 2786 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
|---|
| .. | .. |
|---|
| 2954 | 2799 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into |
|---|
| 2955 | 2800 | * account, or negative errno on failure. |
|---|
| 2956 | 2801 | * |
|---|
| 2957 | | - * This function should be called from contexts where we cannot sleep, and will |
|---|
| 2802 | + * This function can be called from contexts where we cannot sleep, and will |
|---|
| 2958 | 2803 | * complain if the GPIO chip functions potentially sleep. |
|---|
| 2959 | 2804 | */ |
|---|
| 2960 | 2805 | int gpiod_get_value(const struct gpio_desc *desc) |
|---|
| .. | .. |
|---|
| 2978 | 2823 | |
|---|
| 2979 | 2824 | /** |
|---|
| 2980 | 2825 | * gpiod_get_raw_array_value() - read raw values from an array of GPIOs |
|---|
| 2981 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 2826 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 2982 | 2827 | * @desc_array: array of GPIO descriptors whose values will be read |
|---|
| 2983 | | - * @value_array: array to store the read values |
|---|
| 2828 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 2829 | + * @value_bitmap: bitmap to store the read values |
|---|
| 2984 | 2830 | * |
|---|
| 2985 | 2831 | * Read the raw values of the GPIOs, i.e. the values of the physical lines |
|---|
| 2986 | 2832 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, |
|---|
| 2987 | 2833 | * else an error code. |
|---|
| 2988 | 2834 | * |
|---|
| 2989 | | - * This function should be called from contexts where we cannot sleep, |
|---|
| 2835 | + * This function can be called from contexts where we cannot sleep, |
|---|
| 2990 | 2836 | * and it will complain if the GPIO chip functions potentially sleep. |
|---|
| 2991 | 2837 | */ |
|---|
| 2992 | 2838 | int gpiod_get_raw_array_value(unsigned int array_size, |
|---|
| 2993 | | - struct gpio_desc **desc_array, int *value_array) |
|---|
| 2839 | + struct gpio_desc **desc_array, |
|---|
| 2840 | + struct gpio_array *array_info, |
|---|
| 2841 | + unsigned long *value_bitmap) |
|---|
| 2994 | 2842 | { |
|---|
| 2995 | 2843 | if (!desc_array) |
|---|
| 2996 | 2844 | return -EINVAL; |
|---|
| 2997 | 2845 | return gpiod_get_array_value_complex(true, false, array_size, |
|---|
| 2998 | | - desc_array, value_array); |
|---|
| 2846 | + desc_array, array_info, |
|---|
| 2847 | + value_bitmap); |
|---|
| 2999 | 2848 | } |
|---|
| 3000 | 2849 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); |
|---|
| 3001 | 2850 | |
|---|
| 3002 | 2851 | /** |
|---|
| 3003 | 2852 | * gpiod_get_array_value() - read values from an array of GPIOs |
|---|
| 3004 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 2853 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 3005 | 2854 | * @desc_array: array of GPIO descriptors whose values will be read |
|---|
| 3006 | | - * @value_array: array to store the read values |
|---|
| 2855 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 2856 | + * @value_bitmap: bitmap to store the read values |
|---|
| 3007 | 2857 | * |
|---|
| 3008 | 2858 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
|---|
| 3009 | 2859 | * into account. Return 0 in case of success, else an error code. |
|---|
| 3010 | 2860 | * |
|---|
| 3011 | | - * This function should be called from contexts where we cannot sleep, |
|---|
| 2861 | + * This function can be called from contexts where we cannot sleep, |
|---|
| 3012 | 2862 | * and it will complain if the GPIO chip functions potentially sleep. |
|---|
| 3013 | 2863 | */ |
|---|
| 3014 | 2864 | int gpiod_get_array_value(unsigned int array_size, |
|---|
| 3015 | | - struct gpio_desc **desc_array, int *value_array) |
|---|
| 2865 | + struct gpio_desc **desc_array, |
|---|
| 2866 | + struct gpio_array *array_info, |
|---|
| 2867 | + unsigned long *value_bitmap) |
|---|
| 3016 | 2868 | { |
|---|
| 3017 | 2869 | if (!desc_array) |
|---|
| 3018 | 2870 | return -EINVAL; |
|---|
| 3019 | 2871 | return gpiod_get_array_value_complex(false, false, array_size, |
|---|
| 3020 | | - desc_array, value_array); |
|---|
| 2872 | + desc_array, array_info, |
|---|
| 2873 | + value_bitmap); |
|---|
| 3021 | 2874 | } |
|---|
| 3022 | 2875 | EXPORT_SYMBOL_GPL(gpiod_get_array_value); |
|---|
| 3023 | 2876 | |
|---|
| .. | .. |
|---|
| 3028 | 2881 | */ |
|---|
| 3029 | 2882 | static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) |
|---|
| 3030 | 2883 | { |
|---|
| 3031 | | - int err = 0; |
|---|
| 3032 | | - struct gpio_chip *chip = desc->gdev->chip; |
|---|
| 2884 | + int ret = 0; |
|---|
| 2885 | + struct gpio_chip *gc = desc->gdev->chip; |
|---|
| 3033 | 2886 | int offset = gpio_chip_hwgpio(desc); |
|---|
| 3034 | 2887 | |
|---|
| 3035 | 2888 | if (value) { |
|---|
| 3036 | | - err = chip->direction_input(chip, offset); |
|---|
| 2889 | + ret = gc->direction_input(gc, offset); |
|---|
| 3037 | 2890 | } else { |
|---|
| 3038 | | - err = chip->direction_output(chip, offset, 0); |
|---|
| 3039 | | - if (!err) |
|---|
| 2891 | + ret = gc->direction_output(gc, offset, 0); |
|---|
| 2892 | + if (!ret) |
|---|
| 3040 | 2893 | set_bit(FLAG_IS_OUT, &desc->flags); |
|---|
| 3041 | 2894 | } |
|---|
| 3042 | | - trace_gpio_direction(desc_to_gpio(desc), value, err); |
|---|
| 3043 | | - if (err < 0) |
|---|
| 2895 | + trace_gpio_direction(desc_to_gpio(desc), value, ret); |
|---|
| 2896 | + if (ret < 0) |
|---|
| 3044 | 2897 | gpiod_err(desc, |
|---|
| 3045 | 2898 | "%s: Error in set_value for open drain err %d\n", |
|---|
| 3046 | | - __func__, err); |
|---|
| 2899 | + __func__, ret); |
|---|
| 3047 | 2900 | } |
|---|
| 3048 | 2901 | |
|---|
| 3049 | 2902 | /* |
|---|
| .. | .. |
|---|
| 3053 | 2906 | */ |
|---|
| 3054 | 2907 | static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) |
|---|
| 3055 | 2908 | { |
|---|
| 3056 | | - int err = 0; |
|---|
| 3057 | | - struct gpio_chip *chip = desc->gdev->chip; |
|---|
| 2909 | + int ret = 0; |
|---|
| 2910 | + struct gpio_chip *gc = desc->gdev->chip; |
|---|
| 3058 | 2911 | int offset = gpio_chip_hwgpio(desc); |
|---|
| 3059 | 2912 | |
|---|
| 3060 | 2913 | if (value) { |
|---|
| 3061 | | - err = chip->direction_output(chip, offset, 1); |
|---|
| 3062 | | - if (!err) |
|---|
| 2914 | + ret = gc->direction_output(gc, offset, 1); |
|---|
| 2915 | + if (!ret) |
|---|
| 3063 | 2916 | set_bit(FLAG_IS_OUT, &desc->flags); |
|---|
| 3064 | 2917 | } else { |
|---|
| 3065 | | - err = chip->direction_input(chip, offset); |
|---|
| 2918 | + ret = gc->direction_input(gc, offset); |
|---|
| 3066 | 2919 | } |
|---|
| 3067 | | - trace_gpio_direction(desc_to_gpio(desc), !value, err); |
|---|
| 3068 | | - if (err < 0) |
|---|
| 2920 | + trace_gpio_direction(desc_to_gpio(desc), !value, ret); |
|---|
| 2921 | + if (ret < 0) |
|---|
| 3069 | 2922 | gpiod_err(desc, |
|---|
| 3070 | 2923 | "%s: Error in set_value for open source err %d\n", |
|---|
| 3071 | | - __func__, err); |
|---|
| 2924 | + __func__, ret); |
|---|
| 3072 | 2925 | } |
|---|
| 3073 | 2926 | |
|---|
| 3074 | 2927 | static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) |
|---|
| 3075 | 2928 | { |
|---|
| 3076 | | - struct gpio_chip *chip; |
|---|
| 2929 | + struct gpio_chip *gc; |
|---|
| 3077 | 2930 | |
|---|
| 3078 | | - chip = desc->gdev->chip; |
|---|
| 2931 | + gc = desc->gdev->chip; |
|---|
| 3079 | 2932 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
|---|
| 3080 | | - chip->set(chip, gpio_chip_hwgpio(desc), value); |
|---|
| 2933 | + gc->set(gc, gpio_chip_hwgpio(desc), value); |
|---|
| 3081 | 2934 | } |
|---|
| 3082 | 2935 | |
|---|
| 3083 | 2936 | /* |
|---|
| 3084 | 2937 | * set multiple outputs on the same chip; |
|---|
| 3085 | 2938 | * use the chip's set_multiple function if available; |
|---|
| 3086 | 2939 | * otherwise set the outputs sequentially; |
|---|
| 2940 | + * @chip: the GPIO chip we operate on |
|---|
| 3087 | 2941 | * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word |
|---|
| 3088 | 2942 | * defines which outputs are to be changed |
|---|
| 3089 | 2943 | * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word |
|---|
| 3090 | 2944 | * defines the values the outputs specified by mask are to be set to |
|---|
| 3091 | 2945 | */ |
|---|
| 3092 | | -static void gpio_chip_set_multiple(struct gpio_chip *chip, |
|---|
| 2946 | +static void gpio_chip_set_multiple(struct gpio_chip *gc, |
|---|
| 3093 | 2947 | unsigned long *mask, unsigned long *bits) |
|---|
| 3094 | 2948 | { |
|---|
| 3095 | | - if (chip->set_multiple) { |
|---|
| 3096 | | - chip->set_multiple(chip, mask, bits); |
|---|
| 2949 | + if (gc->set_multiple) { |
|---|
| 2950 | + gc->set_multiple(gc, mask, bits); |
|---|
| 3097 | 2951 | } else { |
|---|
| 3098 | 2952 | unsigned int i; |
|---|
| 3099 | 2953 | |
|---|
| 3100 | 2954 | /* set outputs if the corresponding mask bit is set */ |
|---|
| 3101 | | - for_each_set_bit(i, mask, chip->ngpio) |
|---|
| 3102 | | - chip->set(chip, i, test_bit(i, bits)); |
|---|
| 2955 | + for_each_set_bit(i, mask, gc->ngpio) |
|---|
| 2956 | + gc->set(gc, i, test_bit(i, bits)); |
|---|
| 3103 | 2957 | } |
|---|
| 3104 | 2958 | } |
|---|
| 3105 | 2959 | |
|---|
| 3106 | 2960 | int gpiod_set_array_value_complex(bool raw, bool can_sleep, |
|---|
| 3107 | | - unsigned int array_size, |
|---|
| 3108 | | - struct gpio_desc **desc_array, |
|---|
| 3109 | | - int *value_array) |
|---|
| 2961 | + unsigned int array_size, |
|---|
| 2962 | + struct gpio_desc **desc_array, |
|---|
| 2963 | + struct gpio_array *array_info, |
|---|
| 2964 | + unsigned long *value_bitmap) |
|---|
| 3110 | 2965 | { |
|---|
| 3111 | 2966 | int i = 0; |
|---|
| 3112 | 2967 | |
|---|
| 2968 | + /* |
|---|
| 2969 | + * Validate array_info against desc_array and its size. |
|---|
| 2970 | + * It should immediately follow desc_array if both |
|---|
| 2971 | + * have been obtained from the same gpiod_get_array() call. |
|---|
| 2972 | + */ |
|---|
| 2973 | + if (array_info && array_info->desc == desc_array && |
|---|
| 2974 | + array_size <= array_info->size && |
|---|
| 2975 | + (void *)array_info == desc_array + array_info->size) { |
|---|
| 2976 | + if (!can_sleep) |
|---|
| 2977 | + WARN_ON(array_info->chip->can_sleep); |
|---|
| 2978 | + |
|---|
| 2979 | + if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) |
|---|
| 2980 | + bitmap_xor(value_bitmap, value_bitmap, |
|---|
| 2981 | + array_info->invert_mask, array_size); |
|---|
| 2982 | + |
|---|
| 2983 | + gpio_chip_set_multiple(array_info->chip, array_info->set_mask, |
|---|
| 2984 | + value_bitmap); |
|---|
| 2985 | + |
|---|
| 2986 | + i = find_first_zero_bit(array_info->set_mask, array_size); |
|---|
| 2987 | + if (i == array_size) |
|---|
| 2988 | + return 0; |
|---|
| 2989 | + } else { |
|---|
| 2990 | + array_info = NULL; |
|---|
| 2991 | + } |
|---|
| 2992 | + |
|---|
| 3113 | 2993 | while (i < array_size) { |
|---|
| 3114 | | - struct gpio_chip *chip = desc_array[i]->gdev->chip; |
|---|
| 2994 | + struct gpio_chip *gc = desc_array[i]->gdev->chip; |
|---|
| 3115 | 2995 | unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; |
|---|
| 3116 | 2996 | unsigned long *mask, *bits; |
|---|
| 3117 | 2997 | int count = 0; |
|---|
| 3118 | 2998 | |
|---|
| 3119 | | - if (likely(chip->ngpio <= FASTPATH_NGPIO)) { |
|---|
| 2999 | + if (likely(gc->ngpio <= FASTPATH_NGPIO)) { |
|---|
| 3120 | 3000 | mask = fastpath; |
|---|
| 3121 | 3001 | } else { |
|---|
| 3122 | | - mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio), |
|---|
| 3002 | + mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), |
|---|
| 3123 | 3003 | sizeof(*mask), |
|---|
| 3124 | 3004 | can_sleep ? GFP_KERNEL : GFP_ATOMIC); |
|---|
| 3125 | 3005 | if (!mask) |
|---|
| 3126 | 3006 | return -ENOMEM; |
|---|
| 3127 | 3007 | } |
|---|
| 3128 | 3008 | |
|---|
| 3129 | | - bits = mask + BITS_TO_LONGS(chip->ngpio); |
|---|
| 3130 | | - bitmap_zero(mask, chip->ngpio); |
|---|
| 3009 | + bits = mask + BITS_TO_LONGS(gc->ngpio); |
|---|
| 3010 | + bitmap_zero(mask, gc->ngpio); |
|---|
| 3131 | 3011 | |
|---|
| 3132 | 3012 | if (!can_sleep) |
|---|
| 3133 | | - WARN_ON(chip->can_sleep); |
|---|
| 3013 | + WARN_ON(gc->can_sleep); |
|---|
| 3134 | 3014 | |
|---|
| 3135 | 3015 | do { |
|---|
| 3136 | 3016 | struct gpio_desc *desc = desc_array[i]; |
|---|
| 3137 | 3017 | int hwgpio = gpio_chip_hwgpio(desc); |
|---|
| 3138 | | - int value = value_array[i]; |
|---|
| 3018 | + int value = test_bit(i, value_bitmap); |
|---|
| 3139 | 3019 | |
|---|
| 3140 | | - if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
|---|
| 3020 | + /* |
|---|
| 3021 | + * Pins applicable for fast input but not for |
|---|
| 3022 | + * fast output processing may have been already |
|---|
| 3023 | + * inverted inside the fast path, skip them. |
|---|
| 3024 | + */ |
|---|
| 3025 | + if (!raw && !(array_info && |
|---|
| 3026 | + test_bit(i, array_info->invert_mask)) && |
|---|
| 3027 | + test_bit(FLAG_ACTIVE_LOW, &desc->flags)) |
|---|
| 3141 | 3028 | value = !value; |
|---|
| 3142 | 3029 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
|---|
| 3143 | 3030 | /* |
|---|
| .. | .. |
|---|
| 3150 | 3037 | gpio_set_open_source_value_commit(desc, value); |
|---|
| 3151 | 3038 | } else { |
|---|
| 3152 | 3039 | __set_bit(hwgpio, mask); |
|---|
| 3153 | | - if (value) |
|---|
| 3154 | | - __set_bit(hwgpio, bits); |
|---|
| 3155 | | - else |
|---|
| 3156 | | - __clear_bit(hwgpio, bits); |
|---|
| 3040 | + __assign_bit(hwgpio, bits, value); |
|---|
| 3157 | 3041 | count++; |
|---|
| 3158 | 3042 | } |
|---|
| 3159 | 3043 | i++; |
|---|
| 3044 | + |
|---|
| 3045 | + if (array_info) |
|---|
| 3046 | + i = find_next_zero_bit(array_info->set_mask, |
|---|
| 3047 | + array_size, i); |
|---|
| 3160 | 3048 | } while ((i < array_size) && |
|---|
| 3161 | | - (desc_array[i]->gdev->chip == chip)); |
|---|
| 3049 | + (desc_array[i]->gdev->chip == gc)); |
|---|
| 3162 | 3050 | /* push collected bits to outputs */ |
|---|
| 3163 | 3051 | if (count != 0) |
|---|
| 3164 | | - gpio_chip_set_multiple(chip, mask, bits); |
|---|
| 3052 | + gpio_chip_set_multiple(gc, mask, bits); |
|---|
| 3165 | 3053 | |
|---|
| 3166 | 3054 | if (mask != fastpath) |
|---|
| 3167 | 3055 | kfree(mask); |
|---|
| .. | .. |
|---|
| 3177 | 3065 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
|---|
| 3178 | 3066 | * regard for its ACTIVE_LOW status. |
|---|
| 3179 | 3067 | * |
|---|
| 3180 | | - * This function should be called from contexts where we cannot sleep, and will |
|---|
| 3068 | + * This function can be called from contexts where we cannot sleep, and will |
|---|
| 3181 | 3069 | * complain if the GPIO chip functions potentially sleep. |
|---|
| 3182 | 3070 | */ |
|---|
| 3183 | 3071 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
|---|
| .. | .. |
|---|
| 3218 | 3106 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, |
|---|
| 3219 | 3107 | * OPEN_DRAIN and OPEN_SOURCE flags into account. |
|---|
| 3220 | 3108 | * |
|---|
| 3221 | | - * This function should be called from contexts where we cannot sleep, and will |
|---|
| 3109 | + * This function can be called from contexts where we cannot sleep, and will |
|---|
| 3222 | 3110 | * complain if the GPIO chip functions potentially sleep. |
|---|
| 3223 | 3111 | */ |
|---|
| 3224 | 3112 | void gpiod_set_value(struct gpio_desc *desc, int value) |
|---|
| .. | .. |
|---|
| 3232 | 3120 | |
|---|
| 3233 | 3121 | /** |
|---|
| 3234 | 3122 | * gpiod_set_raw_array_value() - assign values to an array of GPIOs |
|---|
| 3235 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 3123 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 3236 | 3124 | * @desc_array: array of GPIO descriptors whose values will be assigned |
|---|
| 3237 | | - * @value_array: array of values to assign |
|---|
| 3125 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 3126 | + * @value_bitmap: bitmap of values to assign |
|---|
| 3238 | 3127 | * |
|---|
| 3239 | 3128 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
|---|
| 3240 | 3129 | * without regard for their ACTIVE_LOW status. |
|---|
| 3241 | 3130 | * |
|---|
| 3242 | | - * This function should be called from contexts where we cannot sleep, and will |
|---|
| 3131 | + * This function can be called from contexts where we cannot sleep, and will |
|---|
| 3243 | 3132 | * complain if the GPIO chip functions potentially sleep. |
|---|
| 3244 | 3133 | */ |
|---|
| 3245 | 3134 | int gpiod_set_raw_array_value(unsigned int array_size, |
|---|
| 3246 | | - struct gpio_desc **desc_array, int *value_array) |
|---|
| 3135 | + struct gpio_desc **desc_array, |
|---|
| 3136 | + struct gpio_array *array_info, |
|---|
| 3137 | + unsigned long *value_bitmap) |
|---|
| 3247 | 3138 | { |
|---|
| 3248 | 3139 | if (!desc_array) |
|---|
| 3249 | 3140 | return -EINVAL; |
|---|
| 3250 | 3141 | return gpiod_set_array_value_complex(true, false, array_size, |
|---|
| 3251 | | - desc_array, value_array); |
|---|
| 3142 | + desc_array, array_info, value_bitmap); |
|---|
| 3252 | 3143 | } |
|---|
| 3253 | 3144 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); |
|---|
| 3254 | 3145 | |
|---|
| 3255 | 3146 | /** |
|---|
| 3256 | 3147 | * gpiod_set_array_value() - assign values to an array of GPIOs |
|---|
| 3257 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 3148 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 3258 | 3149 | * @desc_array: array of GPIO descriptors whose values will be assigned |
|---|
| 3259 | | - * @value_array: array of values to assign |
|---|
| 3150 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 3151 | + * @value_bitmap: bitmap of values to assign |
|---|
| 3260 | 3152 | * |
|---|
| 3261 | 3153 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
|---|
| 3262 | 3154 | * into account. |
|---|
| 3263 | 3155 | * |
|---|
| 3264 | | - * This function should be called from contexts where we cannot sleep, and will |
|---|
| 3156 | + * This function can be called from contexts where we cannot sleep, and will |
|---|
| 3265 | 3157 | * complain if the GPIO chip functions potentially sleep. |
|---|
| 3266 | 3158 | */ |
|---|
| 3267 | | -void gpiod_set_array_value(unsigned int array_size, |
|---|
| 3268 | | - struct gpio_desc **desc_array, int *value_array) |
|---|
| 3159 | +int gpiod_set_array_value(unsigned int array_size, |
|---|
| 3160 | + struct gpio_desc **desc_array, |
|---|
| 3161 | + struct gpio_array *array_info, |
|---|
| 3162 | + unsigned long *value_bitmap) |
|---|
| 3269 | 3163 | { |
|---|
| 3270 | 3164 | if (!desc_array) |
|---|
| 3271 | | - return; |
|---|
| 3272 | | - gpiod_set_array_value_complex(false, false, array_size, desc_array, |
|---|
| 3273 | | - value_array); |
|---|
| 3165 | + return -EINVAL; |
|---|
| 3166 | + return gpiod_set_array_value_complex(false, false, array_size, |
|---|
| 3167 | + desc_array, array_info, |
|---|
| 3168 | + value_bitmap); |
|---|
| 3274 | 3169 | } |
|---|
| 3275 | 3170 | EXPORT_SYMBOL_GPL(gpiod_set_array_value); |
|---|
| 3276 | 3171 | |
|---|
| .. | .. |
|---|
| 3316 | 3211 | */ |
|---|
| 3317 | 3212 | int gpiod_to_irq(const struct gpio_desc *desc) |
|---|
| 3318 | 3213 | { |
|---|
| 3319 | | - struct gpio_chip *chip; |
|---|
| 3214 | + struct gpio_chip *gc; |
|---|
| 3320 | 3215 | int offset; |
|---|
| 3321 | 3216 | |
|---|
| 3322 | 3217 | /* |
|---|
| .. | .. |
|---|
| 3327 | 3222 | if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) |
|---|
| 3328 | 3223 | return -EINVAL; |
|---|
| 3329 | 3224 | |
|---|
| 3330 | | - chip = desc->gdev->chip; |
|---|
| 3225 | + gc = desc->gdev->chip; |
|---|
| 3331 | 3226 | offset = gpio_chip_hwgpio(desc); |
|---|
| 3332 | | - if (chip->to_irq) { |
|---|
| 3333 | | - int retirq = chip->to_irq(chip, offset); |
|---|
| 3227 | + if (gc->to_irq) { |
|---|
| 3228 | + int retirq = gc->to_irq(gc, offset); |
|---|
| 3334 | 3229 | |
|---|
| 3335 | 3230 | /* Zero means NO_IRQ */ |
|---|
| 3336 | 3231 | if (!retirq) |
|---|
| .. | .. |
|---|
| 3338 | 3233 | |
|---|
| 3339 | 3234 | return retirq; |
|---|
| 3340 | 3235 | } |
|---|
| 3236 | +#ifdef CONFIG_GPIOLIB_IRQCHIP |
|---|
| 3237 | + if (gc->irq.chip) { |
|---|
| 3238 | + /* |
|---|
| 3239 | + * Avoid race condition with other code, which tries to lookup |
|---|
| 3240 | + * an IRQ before the irqchip has been properly registered, |
|---|
| 3241 | + * i.e. while gpiochip is still being brought up. |
|---|
| 3242 | + */ |
|---|
| 3243 | + return -EPROBE_DEFER; |
|---|
| 3244 | + } |
|---|
| 3245 | +#endif |
|---|
| 3341 | 3246 | return -ENXIO; |
|---|
| 3342 | 3247 | } |
|---|
| 3343 | 3248 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
|---|
| 3344 | 3249 | |
|---|
| 3345 | 3250 | /** |
|---|
| 3346 | 3251 | * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ |
|---|
| 3347 | | - * @chip: the chip the GPIO to lock belongs to |
|---|
| 3252 | + * @gc: the chip the GPIO to lock belongs to |
|---|
| 3348 | 3253 | * @offset: the offset of the GPIO to lock as IRQ |
|---|
| 3349 | 3254 | * |
|---|
| 3350 | 3255 | * This is used directly by GPIO drivers that want to lock down |
|---|
| 3351 | 3256 | * a certain GPIO line to be used for IRQs. |
|---|
| 3352 | 3257 | */ |
|---|
| 3353 | | -int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
|---|
| 3258 | +int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3354 | 3259 | { |
|---|
| 3355 | 3260 | struct gpio_desc *desc; |
|---|
| 3356 | 3261 | |
|---|
| 3357 | | - desc = gpiochip_get_desc(chip, offset); |
|---|
| 3262 | + desc = gpiochip_get_desc(gc, offset); |
|---|
| 3358 | 3263 | if (IS_ERR(desc)) |
|---|
| 3359 | 3264 | return PTR_ERR(desc); |
|---|
| 3360 | 3265 | |
|---|
| .. | .. |
|---|
| 3362 | 3267 | * If it's fast: flush the direction setting if something changed |
|---|
| 3363 | 3268 | * behind our back |
|---|
| 3364 | 3269 | */ |
|---|
| 3365 | | - if (!chip->can_sleep && chip->get_direction) { |
|---|
| 3270 | + if (!gc->can_sleep && gc->get_direction) { |
|---|
| 3366 | 3271 | int dir = gpiod_get_direction(desc); |
|---|
| 3367 | 3272 | |
|---|
| 3368 | 3273 | if (dir < 0) { |
|---|
| 3369 | | - chip_err(chip, "%s: cannot get GPIO direction\n", |
|---|
| 3274 | + chip_err(gc, "%s: cannot get GPIO direction\n", |
|---|
| 3370 | 3275 | __func__); |
|---|
| 3371 | 3276 | return dir; |
|---|
| 3372 | 3277 | } |
|---|
| 3373 | 3278 | } |
|---|
| 3374 | 3279 | |
|---|
| 3375 | | - if (test_bit(FLAG_IS_OUT, &desc->flags)) { |
|---|
| 3376 | | - chip_err(chip, |
|---|
| 3280 | + /* To be valid for IRQ the line needs to be input or open drain */ |
|---|
| 3281 | + if (test_bit(FLAG_IS_OUT, &desc->flags) && |
|---|
| 3282 | + !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { |
|---|
| 3283 | + chip_err(gc, |
|---|
| 3377 | 3284 | "%s: tried to flag a GPIO set as output for IRQ\n", |
|---|
| 3378 | 3285 | __func__); |
|---|
| 3379 | 3286 | return -EIO; |
|---|
| 3380 | 3287 | } |
|---|
| 3381 | 3288 | |
|---|
| 3382 | 3289 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); |
|---|
| 3290 | + set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
|---|
| 3383 | 3291 | |
|---|
| 3384 | 3292 | /* |
|---|
| 3385 | 3293 | * If the consumer has not set up a label (such as when the |
|---|
| .. | .. |
|---|
| 3395 | 3303 | |
|---|
| 3396 | 3304 | /** |
|---|
| 3397 | 3305 | * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ |
|---|
| 3398 | | - * @chip: the chip the GPIO to lock belongs to |
|---|
| 3306 | + * @gc: the chip the GPIO to lock belongs to |
|---|
| 3399 | 3307 | * @offset: the offset of the GPIO to lock as IRQ |
|---|
| 3400 | 3308 | * |
|---|
| 3401 | 3309 | * This is used directly by GPIO drivers that want to indicate |
|---|
| 3402 | 3310 | * that a certain GPIO is no longer used exclusively for IRQ. |
|---|
| 3403 | 3311 | */ |
|---|
| 3404 | | -void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) |
|---|
| 3312 | +void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3405 | 3313 | { |
|---|
| 3406 | 3314 | struct gpio_desc *desc; |
|---|
| 3407 | 3315 | |
|---|
| 3408 | | - desc = gpiochip_get_desc(chip, offset); |
|---|
| 3316 | + desc = gpiochip_get_desc(gc, offset); |
|---|
| 3409 | 3317 | if (IS_ERR(desc)) |
|---|
| 3410 | 3318 | return; |
|---|
| 3411 | 3319 | |
|---|
| 3412 | 3320 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
|---|
| 3321 | + clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
|---|
| 3413 | 3322 | |
|---|
| 3414 | 3323 | /* If we only had this marking, erase it */ |
|---|
| 3415 | 3324 | if (desc->label && !strcmp(desc->label, "interrupt")) |
|---|
| .. | .. |
|---|
| 3417 | 3326 | } |
|---|
| 3418 | 3327 | EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); |
|---|
| 3419 | 3328 | |
|---|
| 3420 | | -bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset) |
|---|
| 3329 | +void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3421 | 3330 | { |
|---|
| 3422 | | - if (offset >= chip->ngpio) |
|---|
| 3331 | + struct gpio_desc *desc = gpiochip_get_desc(gc, offset); |
|---|
| 3332 | + |
|---|
| 3333 | + if (!IS_ERR(desc) && |
|---|
| 3334 | + !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) |
|---|
| 3335 | + clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
|---|
| 3336 | +} |
|---|
| 3337 | +EXPORT_SYMBOL_GPL(gpiochip_disable_irq); |
|---|
| 3338 | + |
|---|
| 3339 | +void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3340 | +{ |
|---|
| 3341 | + struct gpio_desc *desc = gpiochip_get_desc(gc, offset); |
|---|
| 3342 | + |
|---|
| 3343 | + if (!IS_ERR(desc) && |
|---|
| 3344 | + !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { |
|---|
| 3345 | + /* |
|---|
| 3346 | + * We must not be output when using IRQ UNLESS we are |
|---|
| 3347 | + * open drain. |
|---|
| 3348 | + */ |
|---|
| 3349 | + WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && |
|---|
| 3350 | + !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); |
|---|
| 3351 | + set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); |
|---|
| 3352 | + } |
|---|
| 3353 | +} |
|---|
| 3354 | +EXPORT_SYMBOL_GPL(gpiochip_enable_irq); |
|---|
| 3355 | + |
|---|
| 3356 | +bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3357 | +{ |
|---|
| 3358 | + if (offset >= gc->ngpio) |
|---|
| 3423 | 3359 | return false; |
|---|
| 3424 | 3360 | |
|---|
| 3425 | | - return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags); |
|---|
| 3361 | + return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); |
|---|
| 3426 | 3362 | } |
|---|
| 3427 | 3363 | EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); |
|---|
| 3428 | 3364 | |
|---|
| 3429 | | -bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset) |
|---|
| 3365 | +int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3430 | 3366 | { |
|---|
| 3431 | | - if (offset >= chip->ngpio) |
|---|
| 3367 | + int ret; |
|---|
| 3368 | + |
|---|
| 3369 | + if (!try_module_get(gc->gpiodev->owner)) |
|---|
| 3370 | + return -ENODEV; |
|---|
| 3371 | + |
|---|
| 3372 | + ret = gpiochip_lock_as_irq(gc, offset); |
|---|
| 3373 | + if (ret) { |
|---|
| 3374 | + chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset); |
|---|
| 3375 | + module_put(gc->gpiodev->owner); |
|---|
| 3376 | + return ret; |
|---|
| 3377 | + } |
|---|
| 3378 | + return 0; |
|---|
| 3379 | +} |
|---|
| 3380 | +EXPORT_SYMBOL_GPL(gpiochip_reqres_irq); |
|---|
| 3381 | + |
|---|
| 3382 | +void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3383 | +{ |
|---|
| 3384 | + gpiochip_unlock_as_irq(gc, offset); |
|---|
| 3385 | + module_put(gc->gpiodev->owner); |
|---|
| 3386 | +} |
|---|
| 3387 | +EXPORT_SYMBOL_GPL(gpiochip_relres_irq); |
|---|
| 3388 | + |
|---|
| 3389 | +bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3390 | +{ |
|---|
| 3391 | + if (offset >= gc->ngpio) |
|---|
| 3432 | 3392 | return false; |
|---|
| 3433 | 3393 | |
|---|
| 3434 | | - return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags); |
|---|
| 3394 | + return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); |
|---|
| 3435 | 3395 | } |
|---|
| 3436 | 3396 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); |
|---|
| 3437 | 3397 | |
|---|
| 3438 | | -bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset) |
|---|
| 3398 | +bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3439 | 3399 | { |
|---|
| 3440 | | - if (offset >= chip->ngpio) |
|---|
| 3400 | + if (offset >= gc->ngpio) |
|---|
| 3441 | 3401 | return false; |
|---|
| 3442 | 3402 | |
|---|
| 3443 | | - return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags); |
|---|
| 3403 | + return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); |
|---|
| 3444 | 3404 | } |
|---|
| 3445 | 3405 | EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); |
|---|
| 3446 | 3406 | |
|---|
| 3447 | | -bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset) |
|---|
| 3407 | +bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset) |
|---|
| 3448 | 3408 | { |
|---|
| 3449 | | - if (offset >= chip->ngpio) |
|---|
| 3409 | + if (offset >= gc->ngpio) |
|---|
| 3450 | 3410 | return false; |
|---|
| 3451 | 3411 | |
|---|
| 3452 | | - return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags); |
|---|
| 3412 | + return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); |
|---|
| 3453 | 3413 | } |
|---|
| 3454 | 3414 | EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); |
|---|
| 3455 | 3415 | |
|---|
| .. | .. |
|---|
| 3498 | 3458 | |
|---|
| 3499 | 3459 | /** |
|---|
| 3500 | 3460 | * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs |
|---|
| 3501 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 3461 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 3502 | 3462 | * @desc_array: array of GPIO descriptors whose values will be read |
|---|
| 3503 | | - * @value_array: array to store the read values |
|---|
| 3463 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 3464 | + * @value_bitmap: bitmap to store the read values |
|---|
| 3504 | 3465 | * |
|---|
| 3505 | 3466 | * Read the raw values of the GPIOs, i.e. the values of the physical lines |
|---|
| 3506 | 3467 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, |
|---|
| .. | .. |
|---|
| 3510 | 3471 | */ |
|---|
| 3511 | 3472 | int gpiod_get_raw_array_value_cansleep(unsigned int array_size, |
|---|
| 3512 | 3473 | struct gpio_desc **desc_array, |
|---|
| 3513 | | - int *value_array) |
|---|
| 3474 | + struct gpio_array *array_info, |
|---|
| 3475 | + unsigned long *value_bitmap) |
|---|
| 3514 | 3476 | { |
|---|
| 3515 | 3477 | might_sleep_if(extra_checks); |
|---|
| 3516 | 3478 | if (!desc_array) |
|---|
| 3517 | 3479 | return -EINVAL; |
|---|
| 3518 | 3480 | return gpiod_get_array_value_complex(true, true, array_size, |
|---|
| 3519 | | - desc_array, value_array); |
|---|
| 3481 | + desc_array, array_info, |
|---|
| 3482 | + value_bitmap); |
|---|
| 3520 | 3483 | } |
|---|
| 3521 | 3484 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); |
|---|
| 3522 | 3485 | |
|---|
| 3523 | 3486 | /** |
|---|
| 3524 | 3487 | * gpiod_get_array_value_cansleep() - read values from an array of GPIOs |
|---|
| 3525 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 3488 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 3526 | 3489 | * @desc_array: array of GPIO descriptors whose values will be read |
|---|
| 3527 | | - * @value_array: array to store the read values |
|---|
| 3490 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 3491 | + * @value_bitmap: bitmap to store the read values |
|---|
| 3528 | 3492 | * |
|---|
| 3529 | 3493 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
|---|
| 3530 | 3494 | * into account. Return 0 in case of success, else an error code. |
|---|
| .. | .. |
|---|
| 3533 | 3497 | */ |
|---|
| 3534 | 3498 | int gpiod_get_array_value_cansleep(unsigned int array_size, |
|---|
| 3535 | 3499 | struct gpio_desc **desc_array, |
|---|
| 3536 | | - int *value_array) |
|---|
| 3500 | + struct gpio_array *array_info, |
|---|
| 3501 | + unsigned long *value_bitmap) |
|---|
| 3537 | 3502 | { |
|---|
| 3538 | 3503 | might_sleep_if(extra_checks); |
|---|
| 3539 | 3504 | if (!desc_array) |
|---|
| 3540 | 3505 | return -EINVAL; |
|---|
| 3541 | 3506 | return gpiod_get_array_value_complex(false, true, array_size, |
|---|
| 3542 | | - desc_array, value_array); |
|---|
| 3507 | + desc_array, array_info, |
|---|
| 3508 | + value_bitmap); |
|---|
| 3543 | 3509 | } |
|---|
| 3544 | 3510 | EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); |
|---|
| 3545 | 3511 | |
|---|
| .. | .. |
|---|
| 3581 | 3547 | |
|---|
| 3582 | 3548 | /** |
|---|
| 3583 | 3549 | * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs |
|---|
| 3584 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 3550 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 3585 | 3551 | * @desc_array: array of GPIO descriptors whose values will be assigned |
|---|
| 3586 | | - * @value_array: array of values to assign |
|---|
| 3552 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 3553 | + * @value_bitmap: bitmap of values to assign |
|---|
| 3587 | 3554 | * |
|---|
| 3588 | 3555 | * Set the raw values of the GPIOs, i.e. the values of the physical lines |
|---|
| 3589 | 3556 | * without regard for their ACTIVE_LOW status. |
|---|
| .. | .. |
|---|
| 3591 | 3558 | * This function is to be called from contexts that can sleep. |
|---|
| 3592 | 3559 | */ |
|---|
| 3593 | 3560 | int gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
|---|
| 3594 | | - struct gpio_desc **desc_array, |
|---|
| 3595 | | - int *value_array) |
|---|
| 3561 | + struct gpio_desc **desc_array, |
|---|
| 3562 | + struct gpio_array *array_info, |
|---|
| 3563 | + unsigned long *value_bitmap) |
|---|
| 3596 | 3564 | { |
|---|
| 3597 | 3565 | might_sleep_if(extra_checks); |
|---|
| 3598 | 3566 | if (!desc_array) |
|---|
| 3599 | 3567 | return -EINVAL; |
|---|
| 3600 | 3568 | return gpiod_set_array_value_complex(true, true, array_size, desc_array, |
|---|
| 3601 | | - value_array); |
|---|
| 3569 | + array_info, value_bitmap); |
|---|
| 3602 | 3570 | } |
|---|
| 3603 | 3571 | EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); |
|---|
| 3604 | 3572 | |
|---|
| .. | .. |
|---|
| 3621 | 3589 | |
|---|
| 3622 | 3590 | /** |
|---|
| 3623 | 3591 | * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs |
|---|
| 3624 | | - * @array_size: number of elements in the descriptor / value arrays |
|---|
| 3592 | + * @array_size: number of elements in the descriptor array / value bitmap |
|---|
| 3625 | 3593 | * @desc_array: array of GPIO descriptors whose values will be assigned |
|---|
| 3626 | | - * @value_array: array of values to assign |
|---|
| 3594 | + * @array_info: information on applicability of fast bitmap processing path |
|---|
| 3595 | + * @value_bitmap: bitmap of values to assign |
|---|
| 3627 | 3596 | * |
|---|
| 3628 | 3597 | * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status |
|---|
| 3629 | 3598 | * into account. |
|---|
| 3630 | 3599 | * |
|---|
| 3631 | 3600 | * This function is to be called from contexts that can sleep. |
|---|
| 3632 | 3601 | */ |
|---|
| 3633 | | -void gpiod_set_array_value_cansleep(unsigned int array_size, |
|---|
| 3634 | | - struct gpio_desc **desc_array, |
|---|
| 3635 | | - int *value_array) |
|---|
| 3602 | +int gpiod_set_array_value_cansleep(unsigned int array_size, |
|---|
| 3603 | + struct gpio_desc **desc_array, |
|---|
| 3604 | + struct gpio_array *array_info, |
|---|
| 3605 | + unsigned long *value_bitmap) |
|---|
| 3636 | 3606 | { |
|---|
| 3637 | 3607 | might_sleep_if(extra_checks); |
|---|
| 3638 | 3608 | if (!desc_array) |
|---|
| 3639 | | - return; |
|---|
| 3640 | | - gpiod_set_array_value_complex(false, true, array_size, desc_array, |
|---|
| 3641 | | - value_array); |
|---|
| 3609 | + return -EINVAL; |
|---|
| 3610 | + return gpiod_set_array_value_complex(false, true, array_size, |
|---|
| 3611 | + desc_array, array_info, |
|---|
| 3612 | + value_bitmap); |
|---|
| 3642 | 3613 | } |
|---|
| 3643 | 3614 | EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); |
|---|
| 3644 | 3615 | |
|---|
| .. | .. |
|---|
| 3676 | 3647 | */ |
|---|
| 3677 | 3648 | void gpiod_add_hogs(struct gpiod_hog *hogs) |
|---|
| 3678 | 3649 | { |
|---|
| 3679 | | - struct gpio_chip *chip; |
|---|
| 3650 | + struct gpio_chip *gc; |
|---|
| 3680 | 3651 | struct gpiod_hog *hog; |
|---|
| 3681 | 3652 | |
|---|
| 3682 | 3653 | mutex_lock(&gpio_machine_hogs_mutex); |
|---|
| .. | .. |
|---|
| 3688 | 3659 | * The chip may have been registered earlier, so check if it |
|---|
| 3689 | 3660 | * exists and, if so, try to hog the line now. |
|---|
| 3690 | 3661 | */ |
|---|
| 3691 | | - chip = find_chip_by_name(hog->chip_label); |
|---|
| 3692 | | - if (chip) |
|---|
| 3693 | | - gpiochip_machine_hog(chip, hog); |
|---|
| 3662 | + gc = find_chip_by_name(hog->chip_label); |
|---|
| 3663 | + if (gc) |
|---|
| 3664 | + gpiochip_machine_hog(gc, hog); |
|---|
| 3694 | 3665 | } |
|---|
| 3695 | 3666 | |
|---|
| 3696 | 3667 | mutex_unlock(&gpio_machine_hogs_mutex); |
|---|
| .. | .. |
|---|
| 3729 | 3700 | } |
|---|
| 3730 | 3701 | |
|---|
| 3731 | 3702 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
|---|
| 3732 | | - unsigned int idx, |
|---|
| 3733 | | - enum gpio_lookup_flags *flags) |
|---|
| 3703 | + unsigned int idx, unsigned long *flags) |
|---|
| 3734 | 3704 | { |
|---|
| 3735 | 3705 | struct gpio_desc *desc = ERR_PTR(-ENOENT); |
|---|
| 3736 | 3706 | struct gpiod_lookup_table *table; |
|---|
| .. | .. |
|---|
| 3740 | 3710 | if (!table) |
|---|
| 3741 | 3711 | return desc; |
|---|
| 3742 | 3712 | |
|---|
| 3743 | | - for (p = &table->table[0]; p->chip_label; p++) { |
|---|
| 3744 | | - struct gpio_chip *chip; |
|---|
| 3713 | + for (p = &table->table[0]; p->key; p++) { |
|---|
| 3714 | + struct gpio_chip *gc; |
|---|
| 3745 | 3715 | |
|---|
| 3746 | 3716 | /* idx must always match exactly */ |
|---|
| 3747 | 3717 | if (p->idx != idx) |
|---|
| .. | .. |
|---|
| 3751 | 3721 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) |
|---|
| 3752 | 3722 | continue; |
|---|
| 3753 | 3723 | |
|---|
| 3754 | | - chip = find_chip_by_name(p->chip_label); |
|---|
| 3724 | + if (p->chip_hwnum == U16_MAX) { |
|---|
| 3725 | + desc = gpio_name_to_desc(p->key); |
|---|
| 3726 | + if (desc) { |
|---|
| 3727 | + *flags = p->flags; |
|---|
| 3728 | + return desc; |
|---|
| 3729 | + } |
|---|
| 3755 | 3730 | |
|---|
| 3756 | | - if (!chip) { |
|---|
| 3731 | + dev_warn(dev, "cannot find GPIO line %s, deferring\n", |
|---|
| 3732 | + p->key); |
|---|
| 3733 | + return ERR_PTR(-EPROBE_DEFER); |
|---|
| 3734 | + } |
|---|
| 3735 | + |
|---|
| 3736 | + gc = find_chip_by_name(p->key); |
|---|
| 3737 | + |
|---|
| 3738 | + if (!gc) { |
|---|
| 3757 | 3739 | /* |
|---|
| 3758 | 3740 | * As the lookup table indicates a chip with |
|---|
| 3759 | | - * p->chip_label should exist, assume it may |
|---|
| 3741 | + * p->key should exist, assume it may |
|---|
| 3760 | 3742 | * still appear later and let the interested |
|---|
| 3761 | 3743 | * consumer be probed again or let the Deferred |
|---|
| 3762 | 3744 | * Probe infrastructure handle the error. |
|---|
| 3763 | 3745 | */ |
|---|
| 3764 | 3746 | dev_warn(dev, "cannot find GPIO chip %s, deferring\n", |
|---|
| 3765 | | - p->chip_label); |
|---|
| 3747 | + p->key); |
|---|
| 3766 | 3748 | return ERR_PTR(-EPROBE_DEFER); |
|---|
| 3767 | 3749 | } |
|---|
| 3768 | 3750 | |
|---|
| 3769 | | - if (chip->ngpio <= p->chip_hwnum) { |
|---|
| 3751 | + if (gc->ngpio <= p->chip_hwnum) { |
|---|
| 3770 | 3752 | dev_err(dev, |
|---|
| 3771 | 3753 | "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n", |
|---|
| 3772 | | - idx, p->chip_hwnum, chip->ngpio - 1, |
|---|
| 3773 | | - chip->label); |
|---|
| 3754 | + idx, p->chip_hwnum, gc->ngpio - 1, |
|---|
| 3755 | + gc->label); |
|---|
| 3774 | 3756 | return ERR_PTR(-EINVAL); |
|---|
| 3775 | 3757 | } |
|---|
| 3776 | 3758 | |
|---|
| 3777 | | - desc = gpiochip_get_desc(chip, p->chip_hwnum); |
|---|
| 3759 | + desc = gpiochip_get_desc(gc, p->chip_hwnum); |
|---|
| 3778 | 3760 | *flags = p->flags; |
|---|
| 3779 | 3761 | |
|---|
| 3780 | 3762 | return desc; |
|---|
| 3781 | 3763 | } |
|---|
| 3782 | 3764 | |
|---|
| 3783 | 3765 | return desc; |
|---|
| 3784 | | -} |
|---|
| 3785 | | - |
|---|
| 3786 | | -static int dt_gpio_count(struct device *dev, const char *con_id) |
|---|
| 3787 | | -{ |
|---|
| 3788 | | - int ret; |
|---|
| 3789 | | - char propname[32]; |
|---|
| 3790 | | - unsigned int i; |
|---|
| 3791 | | - |
|---|
| 3792 | | - for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
|---|
| 3793 | | - if (con_id) |
|---|
| 3794 | | - snprintf(propname, sizeof(propname), "%s-%s", |
|---|
| 3795 | | - con_id, gpio_suffixes[i]); |
|---|
| 3796 | | - else |
|---|
| 3797 | | - snprintf(propname, sizeof(propname), "%s", |
|---|
| 3798 | | - gpio_suffixes[i]); |
|---|
| 3799 | | - |
|---|
| 3800 | | - ret = of_gpio_named_count(dev->of_node, propname); |
|---|
| 3801 | | - if (ret > 0) |
|---|
| 3802 | | - break; |
|---|
| 3803 | | - } |
|---|
| 3804 | | - return ret ? ret : -ENOENT; |
|---|
| 3805 | 3766 | } |
|---|
| 3806 | 3767 | |
|---|
| 3807 | 3768 | static int platform_gpio_count(struct device *dev, const char *con_id) |
|---|
| .. | .. |
|---|
| 3814 | 3775 | if (!table) |
|---|
| 3815 | 3776 | return -ENOENT; |
|---|
| 3816 | 3777 | |
|---|
| 3817 | | - for (p = &table->table[0]; p->chip_label; p++) { |
|---|
| 3778 | + for (p = &table->table[0]; p->key; p++) { |
|---|
| 3818 | 3779 | if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || |
|---|
| 3819 | 3780 | (!con_id && !p->con_id)) |
|---|
| 3820 | 3781 | count++; |
|---|
| .. | .. |
|---|
| 3824 | 3785 | |
|---|
| 3825 | 3786 | return count; |
|---|
| 3826 | 3787 | } |
|---|
| 3788 | + |
|---|
| 3789 | +/** |
|---|
| 3790 | + * fwnode_gpiod_get_index - obtain a GPIO from firmware node |
|---|
| 3791 | + * @fwnode: handle of the firmware node |
|---|
| 3792 | + * @con_id: function within the GPIO consumer |
|---|
| 3793 | + * @index: index of the GPIO to obtain for the consumer |
|---|
| 3794 | + * @flags: GPIO initialization flags |
|---|
| 3795 | + * @label: label to attach to the requested GPIO |
|---|
| 3796 | + * |
|---|
| 3797 | + * This function can be used for drivers that get their configuration |
|---|
| 3798 | + * from opaque firmware. |
|---|
| 3799 | + * |
|---|
| 3800 | + * The function properly finds the corresponding GPIO using whatever is the |
|---|
| 3801 | + * underlying firmware interface and then makes sure that the GPIO |
|---|
| 3802 | + * descriptor is requested before it is returned to the caller. |
|---|
| 3803 | + * |
|---|
| 3804 | + * Returns: |
|---|
| 3805 | + * On successful request the GPIO pin is configured in accordance with |
|---|
| 3806 | + * provided @flags. |
|---|
| 3807 | + * |
|---|
| 3808 | + * In case of error an ERR_PTR() is returned. |
|---|
| 3809 | + */ |
|---|
| 3810 | +struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, |
|---|
| 3811 | + const char *con_id, int index, |
|---|
| 3812 | + enum gpiod_flags flags, |
|---|
| 3813 | + const char *label) |
|---|
| 3814 | +{ |
|---|
| 3815 | + struct gpio_desc *desc; |
|---|
| 3816 | + char prop_name[32]; /* 32 is max size of property name */ |
|---|
| 3817 | + unsigned int i; |
|---|
| 3818 | + |
|---|
| 3819 | + for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
|---|
| 3820 | + if (con_id) |
|---|
| 3821 | + snprintf(prop_name, sizeof(prop_name), "%s-%s", |
|---|
| 3822 | + con_id, gpio_suffixes[i]); |
|---|
| 3823 | + else |
|---|
| 3824 | + snprintf(prop_name, sizeof(prop_name), "%s", |
|---|
| 3825 | + gpio_suffixes[i]); |
|---|
| 3826 | + |
|---|
| 3827 | + desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags, |
|---|
| 3828 | + label); |
|---|
| 3829 | + if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) |
|---|
| 3830 | + break; |
|---|
| 3831 | + } |
|---|
| 3832 | + |
|---|
| 3833 | + return desc; |
|---|
| 3834 | +} |
|---|
| 3835 | +EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); |
|---|
| 3827 | 3836 | |
|---|
| 3828 | 3837 | /** |
|---|
| 3829 | 3838 | * gpiod_count - return the number of GPIOs associated with a device / function |
|---|
| .. | .. |
|---|
| 3836 | 3845 | int count = -ENOENT; |
|---|
| 3837 | 3846 | |
|---|
| 3838 | 3847 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) |
|---|
| 3839 | | - count = dt_gpio_count(dev, con_id); |
|---|
| 3848 | + count = of_gpio_get_count(dev, con_id); |
|---|
| 3840 | 3849 | else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) |
|---|
| 3841 | 3850 | count = acpi_gpio_count(dev, con_id); |
|---|
| 3842 | 3851 | |
|---|
| .. | .. |
|---|
| 3887 | 3896 | * gpiod_configure_flags - helper function to configure a given GPIO |
|---|
| 3888 | 3897 | * @desc: gpio whose value will be assigned |
|---|
| 3889 | 3898 | * @con_id: function within the GPIO consumer |
|---|
| 3890 | | - * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
|---|
| 3891 | | - * of_get_gpio_hog() |
|---|
| 3899 | + * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
|---|
| 3900 | + * of_find_gpio() or of_get_gpio_hog() |
|---|
| 3892 | 3901 | * @dflags: gpiod_flags - optional GPIO initialization flags |
|---|
| 3893 | 3902 | * |
|---|
| 3894 | 3903 | * Return 0 on success, -ENOENT if no GPIO has been assigned to the |
|---|
| .. | .. |
|---|
| 3898 | 3907 | int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, |
|---|
| 3899 | 3908 | unsigned long lflags, enum gpiod_flags dflags) |
|---|
| 3900 | 3909 | { |
|---|
| 3901 | | - int status; |
|---|
| 3910 | + int ret; |
|---|
| 3902 | 3911 | |
|---|
| 3903 | 3912 | if (lflags & GPIO_ACTIVE_LOW) |
|---|
| 3904 | 3913 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
|---|
| .. | .. |
|---|
| 3920 | 3929 | if (lflags & GPIO_OPEN_SOURCE) |
|---|
| 3921 | 3930 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
|---|
| 3922 | 3931 | |
|---|
| 3923 | | - status = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); |
|---|
| 3924 | | - if (status < 0) |
|---|
| 3925 | | - return status; |
|---|
| 3932 | + if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) { |
|---|
| 3933 | + gpiod_err(desc, |
|---|
| 3934 | + "both pull-up and pull-down enabled, invalid configuration\n"); |
|---|
| 3935 | + return -EINVAL; |
|---|
| 3936 | + } |
|---|
| 3937 | + |
|---|
| 3938 | + if (lflags & GPIO_PULL_UP) |
|---|
| 3939 | + set_bit(FLAG_PULL_UP, &desc->flags); |
|---|
| 3940 | + else if (lflags & GPIO_PULL_DOWN) |
|---|
| 3941 | + set_bit(FLAG_PULL_DOWN, &desc->flags); |
|---|
| 3942 | + |
|---|
| 3943 | + ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); |
|---|
| 3944 | + if (ret < 0) |
|---|
| 3945 | + return ret; |
|---|
| 3926 | 3946 | |
|---|
| 3927 | 3947 | /* No particular flag request, return here... */ |
|---|
| 3928 | 3948 | if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { |
|---|
| 3929 | | - pr_debug("no flags found for %s\n", con_id); |
|---|
| 3949 | + gpiod_dbg(desc, "no flags found for %s\n", con_id); |
|---|
| 3930 | 3950 | return 0; |
|---|
| 3931 | 3951 | } |
|---|
| 3932 | 3952 | |
|---|
| 3933 | 3953 | /* Process flags */ |
|---|
| 3934 | 3954 | if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) |
|---|
| 3935 | | - status = gpiod_direction_output(desc, |
|---|
| 3955 | + ret = gpiod_direction_output(desc, |
|---|
| 3936 | 3956 | !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); |
|---|
| 3937 | 3957 | else |
|---|
| 3938 | | - status = gpiod_direction_input(desc); |
|---|
| 3958 | + ret = gpiod_direction_input(desc); |
|---|
| 3939 | 3959 | |
|---|
| 3940 | | - return status; |
|---|
| 3960 | + return ret; |
|---|
| 3941 | 3961 | } |
|---|
| 3942 | 3962 | |
|---|
| 3943 | 3963 | /** |
|---|
| .. | .. |
|---|
| 3959 | 3979 | unsigned int idx, |
|---|
| 3960 | 3980 | enum gpiod_flags flags) |
|---|
| 3961 | 3981 | { |
|---|
| 3982 | + unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
|---|
| 3962 | 3983 | struct gpio_desc *desc = NULL; |
|---|
| 3963 | | - int status; |
|---|
| 3964 | | - enum gpio_lookup_flags lookupflags = 0; |
|---|
| 3984 | + int ret; |
|---|
| 3965 | 3985 | /* Maybe we have a device name, maybe not */ |
|---|
| 3966 | 3986 | const char *devname = dev ? dev_name(dev) : "?"; |
|---|
| 3967 | 3987 | |
|---|
| .. | .. |
|---|
| 3996 | 4016 | * If a connection label was passed use that, else attempt to use |
|---|
| 3997 | 4017 | * the device name as label |
|---|
| 3998 | 4018 | */ |
|---|
| 3999 | | - status = gpiod_request(desc, con_id ? con_id : devname); |
|---|
| 4000 | | - if (status < 0) |
|---|
| 4001 | | - return ERR_PTR(status); |
|---|
| 4019 | + ret = gpiod_request(desc, con_id ? con_id : devname); |
|---|
| 4020 | + if (ret < 0) { |
|---|
| 4021 | + if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { |
|---|
| 4022 | + /* |
|---|
| 4023 | + * This happens when there are several consumers for |
|---|
| 4024 | + * the same GPIO line: we just return here without |
|---|
| 4025 | + * further initialization. It is a bit if a hack. |
|---|
| 4026 | + * This is necessary to support fixed regulators. |
|---|
| 4027 | + * |
|---|
| 4028 | + * FIXME: Make this more sane and safe. |
|---|
| 4029 | + */ |
|---|
| 4030 | + dev_info(dev, "nonexclusive access to GPIO for %s\n", |
|---|
| 4031 | + con_id ? con_id : devname); |
|---|
| 4032 | + return desc; |
|---|
| 4033 | + } else { |
|---|
| 4034 | + return ERR_PTR(ret); |
|---|
| 4035 | + } |
|---|
| 4036 | + } |
|---|
| 4002 | 4037 | |
|---|
| 4003 | | - status = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
|---|
| 4004 | | - if (status < 0) { |
|---|
| 4038 | + ret = gpiod_configure_flags(desc, con_id, lookupflags, flags); |
|---|
| 4039 | + if (ret < 0) { |
|---|
| 4005 | 4040 | dev_dbg(dev, "setup of GPIO %s failed\n", con_id); |
|---|
| 4006 | 4041 | gpiod_put(desc); |
|---|
| 4007 | | - return ERR_PTR(status); |
|---|
| 4042 | + return ERR_PTR(ret); |
|---|
| 4008 | 4043 | } |
|---|
| 4044 | + |
|---|
| 4045 | + blocking_notifier_call_chain(&desc->gdev->notifier, |
|---|
| 4046 | + GPIOLINE_CHANGED_REQUESTED, desc); |
|---|
| 4009 | 4047 | |
|---|
| 4010 | 4048 | return desc; |
|---|
| 4011 | 4049 | } |
|---|
| 4012 | 4050 | EXPORT_SYMBOL_GPL(gpiod_get_index); |
|---|
| 4013 | | - |
|---|
| 4014 | | -/** |
|---|
| 4015 | | - * gpiod_get_from_of_node() - obtain a GPIO from an OF node |
|---|
| 4016 | | - * @node: handle of the OF node |
|---|
| 4017 | | - * @propname: name of the DT property representing the GPIO |
|---|
| 4018 | | - * @index: index of the GPIO to obtain for the consumer |
|---|
| 4019 | | - * @dflags: GPIO initialization flags |
|---|
| 4020 | | - * @label: label to attach to the requested GPIO |
|---|
| 4021 | | - * |
|---|
| 4022 | | - * Returns: |
|---|
| 4023 | | - * On successful request the GPIO pin is configured in accordance with |
|---|
| 4024 | | - * provided @dflags. If the node does not have the requested GPIO |
|---|
| 4025 | | - * property, NULL is returned. |
|---|
| 4026 | | - * |
|---|
| 4027 | | - * In case of error an ERR_PTR() is returned. |
|---|
| 4028 | | - */ |
|---|
| 4029 | | -struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, |
|---|
| 4030 | | - const char *propname, int index, |
|---|
| 4031 | | - enum gpiod_flags dflags, |
|---|
| 4032 | | - const char *label) |
|---|
| 4033 | | -{ |
|---|
| 4034 | | - struct gpio_desc *desc; |
|---|
| 4035 | | - unsigned long lflags = 0; |
|---|
| 4036 | | - enum of_gpio_flags flags; |
|---|
| 4037 | | - bool active_low = false; |
|---|
| 4038 | | - bool single_ended = false; |
|---|
| 4039 | | - bool open_drain = false; |
|---|
| 4040 | | - bool transitory = false; |
|---|
| 4041 | | - int ret; |
|---|
| 4042 | | - |
|---|
| 4043 | | - desc = of_get_named_gpiod_flags(node, propname, |
|---|
| 4044 | | - index, &flags); |
|---|
| 4045 | | - |
|---|
| 4046 | | - if (!desc || IS_ERR(desc)) { |
|---|
| 4047 | | - /* If it is not there, just return NULL */ |
|---|
| 4048 | | - if (PTR_ERR(desc) == -ENOENT) |
|---|
| 4049 | | - return NULL; |
|---|
| 4050 | | - return desc; |
|---|
| 4051 | | - } |
|---|
| 4052 | | - |
|---|
| 4053 | | - active_low = flags & OF_GPIO_ACTIVE_LOW; |
|---|
| 4054 | | - single_ended = flags & OF_GPIO_SINGLE_ENDED; |
|---|
| 4055 | | - open_drain = flags & OF_GPIO_OPEN_DRAIN; |
|---|
| 4056 | | - transitory = flags & OF_GPIO_TRANSITORY; |
|---|
| 4057 | | - |
|---|
| 4058 | | - ret = gpiod_request(desc, label); |
|---|
| 4059 | | - if (ret) |
|---|
| 4060 | | - return ERR_PTR(ret); |
|---|
| 4061 | | - |
|---|
| 4062 | | - if (active_low) |
|---|
| 4063 | | - lflags |= GPIO_ACTIVE_LOW; |
|---|
| 4064 | | - |
|---|
| 4065 | | - if (single_ended) { |
|---|
| 4066 | | - if (open_drain) |
|---|
| 4067 | | - lflags |= GPIO_OPEN_DRAIN; |
|---|
| 4068 | | - else |
|---|
| 4069 | | - lflags |= GPIO_OPEN_SOURCE; |
|---|
| 4070 | | - } |
|---|
| 4071 | | - |
|---|
| 4072 | | - if (transitory) |
|---|
| 4073 | | - lflags |= GPIO_TRANSITORY; |
|---|
| 4074 | | - |
|---|
| 4075 | | - ret = gpiod_configure_flags(desc, propname, lflags, dflags); |
|---|
| 4076 | | - if (ret < 0) { |
|---|
| 4077 | | - gpiod_put(desc); |
|---|
| 4078 | | - return ERR_PTR(ret); |
|---|
| 4079 | | - } |
|---|
| 4080 | | - |
|---|
| 4081 | | - return desc; |
|---|
| 4082 | | -} |
|---|
| 4083 | | -EXPORT_SYMBOL(gpiod_get_from_of_node); |
|---|
| 4084 | 4051 | |
|---|
| 4085 | 4052 | /** |
|---|
| 4086 | 4053 | * fwnode_get_named_gpiod - obtain a GPIO from firmware node |
|---|
| .. | .. |
|---|
| 4108 | 4075 | enum gpiod_flags dflags, |
|---|
| 4109 | 4076 | const char *label) |
|---|
| 4110 | 4077 | { |
|---|
| 4078 | + unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
|---|
| 4111 | 4079 | struct gpio_desc *desc = ERR_PTR(-ENODEV); |
|---|
| 4112 | | - unsigned long lflags = 0; |
|---|
| 4113 | 4080 | int ret; |
|---|
| 4114 | 4081 | |
|---|
| 4115 | 4082 | if (!fwnode) |
|---|
| .. | .. |
|---|
| 4129 | 4096 | return desc; |
|---|
| 4130 | 4097 | |
|---|
| 4131 | 4098 | acpi_gpio_update_gpiod_flags(&dflags, &info); |
|---|
| 4132 | | - |
|---|
| 4133 | | - if (info.polarity == GPIO_ACTIVE_LOW) |
|---|
| 4134 | | - lflags |= GPIO_ACTIVE_LOW; |
|---|
| 4099 | + acpi_gpio_update_gpiod_lookup_flags(&lflags, &info); |
|---|
| 4135 | 4100 | } |
|---|
| 4136 | 4101 | |
|---|
| 4137 | 4102 | /* Currently only ACPI takes this path */ |
|---|
| .. | .. |
|---|
| 4144 | 4109 | gpiod_put(desc); |
|---|
| 4145 | 4110 | return ERR_PTR(ret); |
|---|
| 4146 | 4111 | } |
|---|
| 4112 | + |
|---|
| 4113 | + blocking_notifier_call_chain(&desc->gdev->notifier, |
|---|
| 4114 | + GPIOLINE_CHANGED_REQUESTED, desc); |
|---|
| 4147 | 4115 | |
|---|
| 4148 | 4116 | return desc; |
|---|
| 4149 | 4117 | } |
|---|
| .. | .. |
|---|
| 4182 | 4150 | * gpiod_hog - Hog the specified GPIO desc given the provided flags |
|---|
| 4183 | 4151 | * @desc: gpio whose value will be assigned |
|---|
| 4184 | 4152 | * @name: gpio line name |
|---|
| 4185 | | - * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
|---|
| 4186 | | - * of_get_gpio_hog() |
|---|
| 4153 | + * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
|---|
| 4154 | + * of_find_gpio() or of_get_gpio_hog() |
|---|
| 4187 | 4155 | * @dflags: gpiod_flags - optional GPIO initialization flags |
|---|
| 4188 | 4156 | */ |
|---|
| 4189 | 4157 | int gpiod_hog(struct gpio_desc *desc, const char *name, |
|---|
| 4190 | 4158 | unsigned long lflags, enum gpiod_flags dflags) |
|---|
| 4191 | 4159 | { |
|---|
| 4192 | | - struct gpio_chip *chip; |
|---|
| 4160 | + struct gpio_chip *gc; |
|---|
| 4193 | 4161 | struct gpio_desc *local_desc; |
|---|
| 4194 | 4162 | int hwnum; |
|---|
| 4195 | | - int status; |
|---|
| 4163 | + int ret; |
|---|
| 4196 | 4164 | |
|---|
| 4197 | | - chip = gpiod_to_chip(desc); |
|---|
| 4165 | + gc = gpiod_to_chip(desc); |
|---|
| 4198 | 4166 | hwnum = gpio_chip_hwgpio(desc); |
|---|
| 4199 | 4167 | |
|---|
| 4200 | | - local_desc = gpiochip_request_own_desc(chip, hwnum, name); |
|---|
| 4168 | + local_desc = gpiochip_request_own_desc(gc, hwnum, name, |
|---|
| 4169 | + lflags, dflags); |
|---|
| 4201 | 4170 | if (IS_ERR(local_desc)) { |
|---|
| 4202 | | - status = PTR_ERR(local_desc); |
|---|
| 4171 | + ret = PTR_ERR(local_desc); |
|---|
| 4203 | 4172 | pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", |
|---|
| 4204 | | - name, chip->label, hwnum, status); |
|---|
| 4205 | | - return status; |
|---|
| 4206 | | - } |
|---|
| 4207 | | - |
|---|
| 4208 | | - status = gpiod_configure_flags(desc, name, lflags, dflags); |
|---|
| 4209 | | - if (status < 0) { |
|---|
| 4210 | | - pr_err("setup of hog GPIO %s (chip %s, offset %d) failed, %d\n", |
|---|
| 4211 | | - name, chip->label, hwnum, status); |
|---|
| 4212 | | - gpiochip_free_own_desc(desc); |
|---|
| 4213 | | - return status; |
|---|
| 4173 | + name, gc->label, hwnum, ret); |
|---|
| 4174 | + return ret; |
|---|
| 4214 | 4175 | } |
|---|
| 4215 | 4176 | |
|---|
| 4216 | 4177 | /* Mark GPIO as hogged so it can be identified and removed later */ |
|---|
| 4217 | 4178 | set_bit(FLAG_IS_HOGGED, &desc->flags); |
|---|
| 4218 | 4179 | |
|---|
| 4219 | | - pr_info("GPIO line %d (%s) hogged as %s%s\n", |
|---|
| 4220 | | - desc_to_gpio(desc), name, |
|---|
| 4221 | | - (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", |
|---|
| 4222 | | - (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? |
|---|
| 4223 | | - (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":""); |
|---|
| 4180 | + gpiod_info(desc, "hogged as %s%s\n", |
|---|
| 4181 | + (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", |
|---|
| 4182 | + (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? |
|---|
| 4183 | + (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : ""); |
|---|
| 4224 | 4184 | |
|---|
| 4225 | 4185 | return 0; |
|---|
| 4226 | 4186 | } |
|---|
| 4227 | 4187 | |
|---|
| 4228 | 4188 | /** |
|---|
| 4229 | 4189 | * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog |
|---|
| 4230 | | - * @chip: gpio chip to act on |
|---|
| 4231 | | - * |
|---|
| 4232 | | - * This is only used by of_gpiochip_remove to free hogged gpios |
|---|
| 4190 | + * @gc: gpio chip to act on |
|---|
| 4233 | 4191 | */ |
|---|
| 4234 | | -static void gpiochip_free_hogs(struct gpio_chip *chip) |
|---|
| 4192 | +static void gpiochip_free_hogs(struct gpio_chip *gc) |
|---|
| 4235 | 4193 | { |
|---|
| 4236 | 4194 | int id; |
|---|
| 4237 | 4195 | |
|---|
| 4238 | | - for (id = 0; id < chip->ngpio; id++) { |
|---|
| 4239 | | - if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags)) |
|---|
| 4240 | | - gpiochip_free_own_desc(&chip->gpiodev->descs[id]); |
|---|
| 4196 | + for (id = 0; id < gc->ngpio; id++) { |
|---|
| 4197 | + if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags)) |
|---|
| 4198 | + gpiochip_free_own_desc(&gc->gpiodev->descs[id]); |
|---|
| 4241 | 4199 | } |
|---|
| 4242 | 4200 | } |
|---|
| 4243 | 4201 | |
|---|
| .. | .. |
|---|
| 4259 | 4217 | { |
|---|
| 4260 | 4218 | struct gpio_desc *desc; |
|---|
| 4261 | 4219 | struct gpio_descs *descs; |
|---|
| 4262 | | - int count; |
|---|
| 4220 | + struct gpio_array *array_info = NULL; |
|---|
| 4221 | + struct gpio_chip *gc; |
|---|
| 4222 | + int count, bitmap_size; |
|---|
| 4263 | 4223 | |
|---|
| 4264 | 4224 | count = gpiod_count(dev, con_id); |
|---|
| 4265 | 4225 | if (count < 0) |
|---|
| .. | .. |
|---|
| 4275 | 4235 | gpiod_put_array(descs); |
|---|
| 4276 | 4236 | return ERR_CAST(desc); |
|---|
| 4277 | 4237 | } |
|---|
| 4238 | + |
|---|
| 4278 | 4239 | descs->desc[descs->ndescs] = desc; |
|---|
| 4240 | + |
|---|
| 4241 | + gc = gpiod_to_chip(desc); |
|---|
| 4242 | + /* |
|---|
| 4243 | + * If pin hardware number of array member 0 is also 0, select |
|---|
| 4244 | + * its chip as a candidate for fast bitmap processing path. |
|---|
| 4245 | + */ |
|---|
| 4246 | + if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { |
|---|
| 4247 | + struct gpio_descs *array; |
|---|
| 4248 | + |
|---|
| 4249 | + bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? |
|---|
| 4250 | + gc->ngpio : count); |
|---|
| 4251 | + |
|---|
| 4252 | + array = kzalloc(struct_size(descs, desc, count) + |
|---|
| 4253 | + struct_size(array_info, invert_mask, |
|---|
| 4254 | + 3 * bitmap_size), GFP_KERNEL); |
|---|
| 4255 | + if (!array) { |
|---|
| 4256 | + gpiod_put_array(descs); |
|---|
| 4257 | + return ERR_PTR(-ENOMEM); |
|---|
| 4258 | + } |
|---|
| 4259 | + |
|---|
| 4260 | + memcpy(array, descs, |
|---|
| 4261 | + struct_size(descs, desc, descs->ndescs + 1)); |
|---|
| 4262 | + kfree(descs); |
|---|
| 4263 | + |
|---|
| 4264 | + descs = array; |
|---|
| 4265 | + array_info = (void *)(descs->desc + count); |
|---|
| 4266 | + array_info->get_mask = array_info->invert_mask + |
|---|
| 4267 | + bitmap_size; |
|---|
| 4268 | + array_info->set_mask = array_info->get_mask + |
|---|
| 4269 | + bitmap_size; |
|---|
| 4270 | + |
|---|
| 4271 | + array_info->desc = descs->desc; |
|---|
| 4272 | + array_info->size = count; |
|---|
| 4273 | + array_info->chip = gc; |
|---|
| 4274 | + bitmap_set(array_info->get_mask, descs->ndescs, |
|---|
| 4275 | + count - descs->ndescs); |
|---|
| 4276 | + bitmap_set(array_info->set_mask, descs->ndescs, |
|---|
| 4277 | + count - descs->ndescs); |
|---|
| 4278 | + descs->info = array_info; |
|---|
| 4279 | + } |
|---|
| 4280 | + /* Unmark array members which don't belong to the 'fast' chip */ |
|---|
| 4281 | + if (array_info && array_info->chip != gc) { |
|---|
| 4282 | + __clear_bit(descs->ndescs, array_info->get_mask); |
|---|
| 4283 | + __clear_bit(descs->ndescs, array_info->set_mask); |
|---|
| 4284 | + } |
|---|
| 4285 | + /* |
|---|
| 4286 | + * Detect array members which belong to the 'fast' chip |
|---|
| 4287 | + * but their pins are not in hardware order. |
|---|
| 4288 | + */ |
|---|
| 4289 | + else if (array_info && |
|---|
| 4290 | + gpio_chip_hwgpio(desc) != descs->ndescs) { |
|---|
| 4291 | + /* |
|---|
| 4292 | + * Don't use fast path if all array members processed so |
|---|
| 4293 | + * far belong to the same chip as this one but its pin |
|---|
| 4294 | + * hardware number is different from its array index. |
|---|
| 4295 | + */ |
|---|
| 4296 | + if (bitmap_full(array_info->get_mask, descs->ndescs)) { |
|---|
| 4297 | + array_info = NULL; |
|---|
| 4298 | + } else { |
|---|
| 4299 | + __clear_bit(descs->ndescs, |
|---|
| 4300 | + array_info->get_mask); |
|---|
| 4301 | + __clear_bit(descs->ndescs, |
|---|
| 4302 | + array_info->set_mask); |
|---|
| 4303 | + } |
|---|
| 4304 | + } else if (array_info) { |
|---|
| 4305 | + /* Exclude open drain or open source from fast output */ |
|---|
| 4306 | + if (gpiochip_line_is_open_drain(gc, descs->ndescs) || |
|---|
| 4307 | + gpiochip_line_is_open_source(gc, descs->ndescs)) |
|---|
| 4308 | + __clear_bit(descs->ndescs, |
|---|
| 4309 | + array_info->set_mask); |
|---|
| 4310 | + /* Identify 'fast' pins which require invertion */ |
|---|
| 4311 | + if (gpiod_is_active_low(desc)) |
|---|
| 4312 | + __set_bit(descs->ndescs, |
|---|
| 4313 | + array_info->invert_mask); |
|---|
| 4314 | + } |
|---|
| 4315 | + |
|---|
| 4279 | 4316 | descs->ndescs++; |
|---|
| 4280 | 4317 | } |
|---|
| 4318 | + if (array_info) |
|---|
| 4319 | + dev_dbg(dev, |
|---|
| 4320 | + "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", |
|---|
| 4321 | + array_info->chip->label, array_info->size, |
|---|
| 4322 | + *array_info->get_mask, *array_info->set_mask, |
|---|
| 4323 | + *array_info->invert_mask); |
|---|
| 4281 | 4324 | return descs; |
|---|
| 4282 | 4325 | } |
|---|
| 4283 | 4326 | EXPORT_SYMBOL_GPL(gpiod_get_array); |
|---|
| .. | .. |
|---|
| 4299 | 4342 | struct gpio_descs *descs; |
|---|
| 4300 | 4343 | |
|---|
| 4301 | 4344 | descs = gpiod_get_array(dev, con_id, flags); |
|---|
| 4302 | | - if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) |
|---|
| 4345 | + if (PTR_ERR(descs) == -ENOENT) |
|---|
| 4303 | 4346 | return NULL; |
|---|
| 4304 | 4347 | |
|---|
| 4305 | 4348 | return descs; |
|---|
| .. | .. |
|---|
| 4314 | 4357 | */ |
|---|
| 4315 | 4358 | void gpiod_put(struct gpio_desc *desc) |
|---|
| 4316 | 4359 | { |
|---|
| 4317 | | - gpiod_free(desc); |
|---|
| 4360 | + if (desc) |
|---|
| 4361 | + gpiod_free(desc); |
|---|
| 4318 | 4362 | } |
|---|
| 4319 | 4363 | EXPORT_SYMBOL_GPL(gpiod_put); |
|---|
| 4320 | 4364 | |
|---|
| .. | .. |
|---|
| 4333 | 4377 | } |
|---|
| 4334 | 4378 | EXPORT_SYMBOL_GPL(gpiod_put_array); |
|---|
| 4335 | 4379 | |
|---|
| 4380 | + |
|---|
| 4381 | +static int gpio_bus_match(struct device *dev, struct device_driver *drv) |
|---|
| 4382 | +{ |
|---|
| 4383 | + /* |
|---|
| 4384 | + * Only match if the fwnode doesn't already have a proper struct device |
|---|
| 4385 | + * created for it. |
|---|
| 4386 | + */ |
|---|
| 4387 | + if (dev->fwnode && dev->fwnode->dev != dev) |
|---|
| 4388 | + return 0; |
|---|
| 4389 | + return 1; |
|---|
| 4390 | +} |
|---|
| 4391 | + |
|---|
| 4392 | +static int gpio_stub_drv_probe(struct device *dev) |
|---|
| 4393 | +{ |
|---|
| 4394 | + /* |
|---|
| 4395 | + * The DT node of some GPIO chips have a "compatible" property, but |
|---|
| 4396 | + * never have a struct device added and probed by a driver to register |
|---|
| 4397 | + * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause |
|---|
| 4398 | + * the consumers of the GPIO chip to get probe deferred forever because |
|---|
| 4399 | + * they will be waiting for a device associated with the GPIO chip |
|---|
| 4400 | + * firmware node to get added and bound to a driver. |
|---|
| 4401 | + * |
|---|
| 4402 | + * To allow these consumers to probe, we associate the struct |
|---|
| 4403 | + * gpio_device of the GPIO chip with the firmware node and then simply |
|---|
| 4404 | + * bind it to this stub driver. |
|---|
| 4405 | + */ |
|---|
| 4406 | + return 0; |
|---|
| 4407 | +} |
|---|
| 4408 | + |
|---|
| 4409 | +static struct device_driver gpio_stub_drv = { |
|---|
| 4410 | + .name = "gpio_stub_drv", |
|---|
| 4411 | + .bus = &gpio_bus_type, |
|---|
| 4412 | + .probe = gpio_stub_drv_probe, |
|---|
| 4413 | +}; |
|---|
| 4414 | + |
|---|
| 4336 | 4415 | static int __init gpiolib_dev_init(void) |
|---|
| 4337 | 4416 | { |
|---|
| 4338 | 4417 | int ret; |
|---|
| .. | .. |
|---|
| 4344 | 4423 | return ret; |
|---|
| 4345 | 4424 | } |
|---|
| 4346 | 4425 | |
|---|
| 4347 | | - ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip"); |
|---|
| 4426 | + ret = driver_register(&gpio_stub_drv); |
|---|
| 4427 | + if (ret < 0) { |
|---|
| 4428 | + pr_err("gpiolib: could not register GPIO stub driver\n"); |
|---|
| 4429 | + bus_unregister(&gpio_bus_type); |
|---|
| 4430 | + return ret; |
|---|
| 4431 | + } |
|---|
| 4432 | + |
|---|
| 4433 | + ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME); |
|---|
| 4348 | 4434 | if (ret < 0) { |
|---|
| 4349 | 4435 | pr_err("gpiolib: failed to allocate char dev region\n"); |
|---|
| 4436 | + driver_unregister(&gpio_stub_drv); |
|---|
| 4350 | 4437 | bus_unregister(&gpio_bus_type); |
|---|
| 4351 | | - } else { |
|---|
| 4352 | | - gpiolib_initialized = true; |
|---|
| 4353 | | - gpiochip_setup_devs(); |
|---|
| 4438 | + return ret; |
|---|
| 4354 | 4439 | } |
|---|
| 4440 | + |
|---|
| 4441 | + gpiolib_initialized = true; |
|---|
| 4442 | + gpiochip_setup_devs(); |
|---|
| 4443 | + |
|---|
| 4444 | +#if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO) |
|---|
| 4445 | + WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier)); |
|---|
| 4446 | +#endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */ |
|---|
| 4447 | + |
|---|
| 4355 | 4448 | return ret; |
|---|
| 4356 | 4449 | } |
|---|
| 4357 | 4450 | core_initcall(gpiolib_dev_init); |
|---|
| .. | .. |
|---|
| 4361 | 4454 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) |
|---|
| 4362 | 4455 | { |
|---|
| 4363 | 4456 | unsigned i; |
|---|
| 4364 | | - struct gpio_chip *chip = gdev->chip; |
|---|
| 4457 | + struct gpio_chip *gc = gdev->chip; |
|---|
| 4365 | 4458 | unsigned gpio = gdev->base; |
|---|
| 4366 | 4459 | struct gpio_desc *gdesc = &gdev->descs[0]; |
|---|
| 4367 | | - int is_out; |
|---|
| 4368 | | - int is_irq; |
|---|
| 4460 | + bool is_out; |
|---|
| 4461 | + bool is_irq; |
|---|
| 4462 | + bool active_low; |
|---|
| 4369 | 4463 | |
|---|
| 4370 | 4464 | for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { |
|---|
| 4371 | 4465 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { |
|---|
| .. | .. |
|---|
| 4379 | 4473 | gpiod_get_direction(gdesc); |
|---|
| 4380 | 4474 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
|---|
| 4381 | 4475 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
|---|
| 4382 | | - seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s", |
|---|
| 4476 | + active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags); |
|---|
| 4477 | + seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s", |
|---|
| 4383 | 4478 | gpio, gdesc->name ? gdesc->name : "", gdesc->label, |
|---|
| 4384 | 4479 | is_out ? "out" : "in ", |
|---|
| 4385 | | - chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "? ", |
|---|
| 4386 | | - is_irq ? "IRQ" : " "); |
|---|
| 4480 | + gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ", |
|---|
| 4481 | + is_irq ? "IRQ " : "", |
|---|
| 4482 | + active_low ? "ACTIVE LOW" : ""); |
|---|
| 4387 | 4483 | seq_printf(s, "\n"); |
|---|
| 4388 | 4484 | } |
|---|
| 4389 | 4485 | } |
|---|
| .. | .. |
|---|
| 4433 | 4529 | static int gpiolib_seq_show(struct seq_file *s, void *v) |
|---|
| 4434 | 4530 | { |
|---|
| 4435 | 4531 | struct gpio_device *gdev = v; |
|---|
| 4436 | | - struct gpio_chip *chip = gdev->chip; |
|---|
| 4532 | + struct gpio_chip *gc = gdev->chip; |
|---|
| 4437 | 4533 | struct device *parent; |
|---|
| 4438 | 4534 | |
|---|
| 4439 | | - if (!chip) { |
|---|
| 4535 | + if (!gc) { |
|---|
| 4440 | 4536 | seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, |
|---|
| 4441 | 4537 | dev_name(&gdev->dev)); |
|---|
| 4442 | 4538 | return 0; |
|---|
| .. | .. |
|---|
| 4445 | 4541 | seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, |
|---|
| 4446 | 4542 | dev_name(&gdev->dev), |
|---|
| 4447 | 4543 | gdev->base, gdev->base + gdev->ngpio - 1); |
|---|
| 4448 | | - parent = chip->parent; |
|---|
| 4544 | + parent = gc->parent; |
|---|
| 4449 | 4545 | if (parent) |
|---|
| 4450 | 4546 | seq_printf(s, ", parent: %s/%s", |
|---|
| 4451 | 4547 | parent->bus ? parent->bus->name : "no-bus", |
|---|
| 4452 | 4548 | dev_name(parent)); |
|---|
| 4453 | | - if (chip->label) |
|---|
| 4454 | | - seq_printf(s, ", %s", chip->label); |
|---|
| 4455 | | - if (chip->can_sleep) |
|---|
| 4549 | + if (gc->label) |
|---|
| 4550 | + seq_printf(s, ", %s", gc->label); |
|---|
| 4551 | + if (gc->can_sleep) |
|---|
| 4456 | 4552 | seq_printf(s, ", can sleep"); |
|---|
| 4457 | 4553 | seq_printf(s, ":\n"); |
|---|
| 4458 | 4554 | |
|---|
| 4459 | | - if (chip->dbg_show) |
|---|
| 4460 | | - chip->dbg_show(s, chip); |
|---|
| 4555 | + if (gc->dbg_show) |
|---|
| 4556 | + gc->dbg_show(s, gc); |
|---|
| 4461 | 4557 | else |
|---|
| 4462 | 4558 | gpiolib_dbg_show(s, gdev); |
|---|
| 4463 | 4559 | |
|---|
| 4464 | 4560 | return 0; |
|---|
| 4465 | 4561 | } |
|---|
| 4466 | 4562 | |
|---|
| 4467 | | -static const struct seq_operations gpiolib_seq_ops = { |
|---|
| 4563 | +static const struct seq_operations gpiolib_sops = { |
|---|
| 4468 | 4564 | .start = gpiolib_seq_start, |
|---|
| 4469 | 4565 | .next = gpiolib_seq_next, |
|---|
| 4470 | 4566 | .stop = gpiolib_seq_stop, |
|---|
| 4471 | 4567 | .show = gpiolib_seq_show, |
|---|
| 4472 | 4568 | }; |
|---|
| 4473 | | - |
|---|
| 4474 | | -static int gpiolib_open(struct inode *inode, struct file *file) |
|---|
| 4475 | | -{ |
|---|
| 4476 | | - return seq_open(file, &gpiolib_seq_ops); |
|---|
| 4477 | | -} |
|---|
| 4478 | | - |
|---|
| 4479 | | -static const struct file_operations gpiolib_operations = { |
|---|
| 4480 | | - .owner = THIS_MODULE, |
|---|
| 4481 | | - .open = gpiolib_open, |
|---|
| 4482 | | - .read = seq_read, |
|---|
| 4483 | | - .llseek = seq_lseek, |
|---|
| 4484 | | - .release = seq_release, |
|---|
| 4485 | | -}; |
|---|
| 4569 | +DEFINE_SEQ_ATTRIBUTE(gpiolib); |
|---|
| 4486 | 4570 | |
|---|
| 4487 | 4571 | static int __init gpiolib_debugfs_init(void) |
|---|
| 4488 | 4572 | { |
|---|
| 4489 | 4573 | /* /sys/kernel/debug/gpio */ |
|---|
| 4490 | | - (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, |
|---|
| 4491 | | - NULL, NULL, &gpiolib_operations); |
|---|
| 4574 | + debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops); |
|---|
| 4492 | 4575 | return 0; |
|---|
| 4493 | 4576 | } |
|---|
| 4494 | 4577 | subsys_initcall(gpiolib_debugfs_init); |
|---|