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