diff options
author | Lukas Wunner <lukas@wunner.de> | 2017-10-12 06:40:10 -0400 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2017-10-19 16:32:39 -0400 |
commit | eec1d566cdf94b57e8f5ba9fe60eea214929bcfc (patch) | |
tree | f10fab2d393d231f711123fcf66c1b01fc893370 /drivers/gpio/gpiolib.c | |
parent | 5307e2ad69ab3b0e0622fdf8b254c1d4565eb924 (diff) |
gpio: Introduce ->get_multiple callback
SPI-attached GPIO controllers typically read out all inputs in one go.
If callers desire the values of multipe inputs, ideally a single readout
should take place to return the desired values. However the current
driver API only offers a ->get callback but no ->get_multiple (unlike
->set_multiple, which is present). Thus, to read multiple inputs, a
full readout needs to be performed for every single value (barring
driver-internal caching), which is inefficient.
In fact, the lack of a ->get_multiple callback has been bemoaned
repeatedly by the gpio subsystem maintainer:
http://www.spinics.net/lists/linux-gpio/msg10571.html
http://www.spinics.net/lists/devicetree/msg121734.html
Introduce the missing callback. Add corresponding consumer functions
such as gpiod_get_array_value(). Amend linehandle_ioctl() to take
advantage of the newly added infrastructure. Update the documentation.
Cc: Rojhalat Ibrahim <imr@rtschenk.de>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r-- | drivers/gpio/gpiolib.c | 179 |
1 files changed, 168 insertions, 11 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 8113929eb2bd..ff46a1b6299e 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -365,28 +365,28 @@ static long linehandle_ioctl(struct file *filep, unsigned int cmd, | |||
365 | struct linehandle_state *lh = filep->private_data; | 365 | struct linehandle_state *lh = filep->private_data; |
366 | void __user *ip = (void __user *)arg; | 366 | void __user *ip = (void __user *)arg; |
367 | struct gpiohandle_data ghd; | 367 | struct gpiohandle_data ghd; |
368 | int vals[GPIOHANDLES_MAX]; | ||
368 | int i; | 369 | int i; |
369 | 370 | ||
370 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { | 371 | if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) { |
371 | int val; | 372 | /* TODO: check if descriptors are really input */ |
373 | int ret = gpiod_get_array_value_complex(false, | ||
374 | true, | ||
375 | lh->numdescs, | ||
376 | lh->descs, | ||
377 | vals); | ||
378 | if (ret) | ||
379 | return ret; | ||
372 | 380 | ||
373 | memset(&ghd, 0, sizeof(ghd)); | 381 | memset(&ghd, 0, sizeof(ghd)); |
374 | 382 | for (i = 0; i < lh->numdescs; i++) | |
375 | /* TODO: check if descriptors are really input */ | 383 | ghd.values[i] = vals[i]; |
376 | for (i = 0; i < lh->numdescs; i++) { | ||
377 | val = gpiod_get_value_cansleep(lh->descs[i]); | ||
378 | if (val < 0) | ||
379 | return val; | ||
380 | ghd.values[i] = val; | ||
381 | } | ||
382 | 384 | ||
383 | if (copy_to_user(ip, &ghd, sizeof(ghd))) | 385 | if (copy_to_user(ip, &ghd, sizeof(ghd))) |
384 | return -EFAULT; | 386 | return -EFAULT; |
385 | 387 | ||
386 | return 0; | 388 | return 0; |
387 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { | 389 | } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) { |
388 | int vals[GPIOHANDLES_MAX]; | ||
389 | |||
390 | /* TODO: check if descriptors are really output */ | 390 | /* TODO: check if descriptors are really output */ |
391 | if (copy_from_user(&ghd, ip, sizeof(ghd))) | 391 | if (copy_from_user(&ghd, ip, sizeof(ghd))) |
392 | return -EFAULT; | 392 | return -EFAULT; |
@@ -2466,6 +2466,71 @@ static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) | |||
2466 | return value; | 2466 | return value; |
2467 | } | 2467 | } |
2468 | 2468 | ||
2469 | static int gpio_chip_get_multiple(struct gpio_chip *chip, | ||
2470 | unsigned long *mask, unsigned long *bits) | ||
2471 | { | ||
2472 | if (chip->get_multiple) { | ||
2473 | return chip->get_multiple(chip, mask, bits); | ||
2474 | } else if (chip->get) { | ||
2475 | int i, value; | ||
2476 | |||
2477 | for_each_set_bit(i, mask, chip->ngpio) { | ||
2478 | value = chip->get(chip, i); | ||
2479 | if (value < 0) | ||
2480 | return value; | ||
2481 | __assign_bit(i, bits, value); | ||
2482 | } | ||
2483 | return 0; | ||
2484 | } | ||
2485 | return -EIO; | ||
2486 | } | ||
2487 | |||
2488 | int gpiod_get_array_value_complex(bool raw, bool can_sleep, | ||
2489 | unsigned int array_size, | ||
2490 | struct gpio_desc **desc_array, | ||
2491 | int *value_array) | ||
2492 | { | ||
2493 | int i = 0; | ||
2494 | |||
2495 | while (i < array_size) { | ||
2496 | struct gpio_chip *chip = desc_array[i]->gdev->chip; | ||
2497 | unsigned long mask[BITS_TO_LONGS(chip->ngpio)]; | ||
2498 | unsigned long bits[BITS_TO_LONGS(chip->ngpio)]; | ||
2499 | int first, j, ret; | ||
2500 | |||
2501 | if (!can_sleep) | ||
2502 | WARN_ON(chip->can_sleep); | ||
2503 | |||
2504 | /* collect all inputs belonging to the same chip */ | ||
2505 | first = i; | ||
2506 | memset(mask, 0, sizeof(mask)); | ||
2507 | do { | ||
2508 | const struct gpio_desc *desc = desc_array[i]; | ||
2509 | int hwgpio = gpio_chip_hwgpio(desc); | ||
2510 | |||
2511 | __set_bit(hwgpio, mask); | ||
2512 | i++; | ||
2513 | } while ((i < array_size) && | ||
2514 | (desc_array[i]->gdev->chip == chip)); | ||
2515 | |||
2516 | ret = gpio_chip_get_multiple(chip, mask, bits); | ||
2517 | if (ret) | ||
2518 | return ret; | ||
2519 | |||
2520 | for (j = first; j < i; j++) { | ||
2521 | const struct gpio_desc *desc = desc_array[j]; | ||
2522 | int hwgpio = gpio_chip_hwgpio(desc); | ||
2523 | int value = test_bit(hwgpio, bits); | ||
2524 | |||
2525 | if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | ||
2526 | value = !value; | ||
2527 | value_array[j] = value; | ||
2528 | trace_gpio_value(desc_to_gpio(desc), 1, value); | ||
2529 | } | ||
2530 | } | ||
2531 | return 0; | ||
2532 | } | ||
2533 | |||
2469 | /** | 2534 | /** |
2470 | * gpiod_get_raw_value() - return a gpio's raw value | 2535 | * gpiod_get_raw_value() - return a gpio's raw value |
2471 | * @desc: gpio whose value will be returned | 2536 | * @desc: gpio whose value will be returned |
@@ -2514,6 +2579,51 @@ int gpiod_get_value(const struct gpio_desc *desc) | |||
2514 | } | 2579 | } |
2515 | EXPORT_SYMBOL_GPL(gpiod_get_value); | 2580 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
2516 | 2581 | ||
2582 | /** | ||
2583 | * gpiod_get_raw_array_value() - read raw values from an array of GPIOs | ||
2584 | * @array_size: number of elements in the descriptor / value arrays | ||
2585 | * @desc_array: array of GPIO descriptors whose values will be read | ||
2586 | * @value_array: array to store the read values | ||
2587 | * | ||
2588 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | ||
2589 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | ||
2590 | * else an error code. | ||
2591 | * | ||
2592 | * This function should be called from contexts where we cannot sleep, | ||
2593 | * and it will complain if the GPIO chip functions potentially sleep. | ||
2594 | */ | ||
2595 | int gpiod_get_raw_array_value(unsigned int array_size, | ||
2596 | struct gpio_desc **desc_array, int *value_array) | ||
2597 | { | ||
2598 | if (!desc_array) | ||
2599 | return -EINVAL; | ||
2600 | return gpiod_get_array_value_complex(true, false, array_size, | ||
2601 | desc_array, value_array); | ||
2602 | } | ||
2603 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); | ||
2604 | |||
2605 | /** | ||
2606 | * gpiod_get_array_value() - read values from an array of GPIOs | ||
2607 | * @array_size: number of elements in the descriptor / value arrays | ||
2608 | * @desc_array: array of GPIO descriptors whose values will be read | ||
2609 | * @value_array: array to store the read values | ||
2610 | * | ||
2611 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | ||
2612 | * into account. Return 0 in case of success, else an error code. | ||
2613 | * | ||
2614 | * This function should be called from contexts where we cannot sleep, | ||
2615 | * and it will complain if the GPIO chip functions potentially sleep. | ||
2616 | */ | ||
2617 | int gpiod_get_array_value(unsigned int array_size, | ||
2618 | struct gpio_desc **desc_array, int *value_array) | ||
2619 | { | ||
2620 | if (!desc_array) | ||
2621 | return -EINVAL; | ||
2622 | return gpiod_get_array_value_complex(false, false, array_size, | ||
2623 | desc_array, value_array); | ||
2624 | } | ||
2625 | EXPORT_SYMBOL_GPL(gpiod_get_array_value); | ||
2626 | |||
2517 | /* | 2627 | /* |
2518 | * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. | 2628 | * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. |
2519 | * @desc: gpio descriptor whose state need to be set. | 2629 | * @desc: gpio descriptor whose state need to be set. |
@@ -2943,6 +3053,53 @@ int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |||
2943 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); | 3053 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
2944 | 3054 | ||
2945 | /** | 3055 | /** |
3056 | * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs | ||
3057 | * @array_size: number of elements in the descriptor / value arrays | ||
3058 | * @desc_array: array of GPIO descriptors whose values will be read | ||
3059 | * @value_array: array to store the read values | ||
3060 | * | ||
3061 | * Read the raw values of the GPIOs, i.e. the values of the physical lines | ||
3062 | * without regard for their ACTIVE_LOW status. Return 0 in case of success, | ||
3063 | * else an error code. | ||
3064 | * | ||
3065 | * This function is to be called from contexts that can sleep. | ||
3066 | */ | ||
3067 | int gpiod_get_raw_array_value_cansleep(unsigned int array_size, | ||
3068 | struct gpio_desc **desc_array, | ||
3069 | int *value_array) | ||
3070 | { | ||
3071 | might_sleep_if(extra_checks); | ||
3072 | if (!desc_array) | ||
3073 | return -EINVAL; | ||
3074 | return gpiod_get_array_value_complex(true, true, array_size, | ||
3075 | desc_array, value_array); | ||
3076 | } | ||
3077 | EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); | ||
3078 | |||
3079 | /** | ||
3080 | * gpiod_get_array_value_cansleep() - read values from an array of GPIOs | ||
3081 | * @array_size: number of elements in the descriptor / value arrays | ||
3082 | * @desc_array: array of GPIO descriptors whose values will be read | ||
3083 | * @value_array: array to store the read values | ||
3084 | * | ||
3085 | * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status | ||
3086 | * into account. Return 0 in case of success, else an error code. | ||
3087 | * | ||
3088 | * This function is to be called from contexts that can sleep. | ||
3089 | */ | ||
3090 | int gpiod_get_array_value_cansleep(unsigned int array_size, | ||
3091 | struct gpio_desc **desc_array, | ||
3092 | int *value_array) | ||
3093 | { | ||
3094 | might_sleep_if(extra_checks); | ||
3095 | if (!desc_array) | ||
3096 | return -EINVAL; | ||
3097 | return gpiod_get_array_value_complex(false, true, array_size, | ||
3098 | desc_array, value_array); | ||
3099 | } | ||
3100 | EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); | ||
3101 | |||
3102 | /** | ||
2946 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | 3103 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value |
2947 | * @desc: gpio whose value will be assigned | 3104 | * @desc: gpio whose value will be assigned |
2948 | * @value: value to assign | 3105 | * @value: value to assign |