^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "gpiolib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) void gpio_free(unsigned gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) gpiod_free(gpio_to_desc(gpio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) EXPORT_SYMBOL_GPL(gpio_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * gpio_request_one - request a single GPIO with initial configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * @gpio: the GPIO number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @flags: GPIO configuration as specified by GPIOF_*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @label: a literal description string of this GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) desc = gpio_to_desc(gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Compatibility: assume unavailable "valid" GPIOs will appear later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!desc && gpio_is_valid(gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) err = gpiod_request(desc, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (flags & GPIOF_OPEN_DRAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) set_bit(FLAG_OPEN_DRAIN, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (flags & GPIOF_OPEN_SOURCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) set_bit(FLAG_OPEN_SOURCE, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (flags & GPIOF_ACTIVE_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) set_bit(FLAG_ACTIVE_LOW, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (flags & GPIOF_DIR_IN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) err = gpiod_direction_input(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) err = gpiod_direction_output_raw(desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) (flags & GPIOF_INIT_HIGH) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) goto free_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (flags & GPIOF_EXPORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) goto free_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) free_gpio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) gpiod_free(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) EXPORT_SYMBOL_GPL(gpio_request_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int gpio_request(unsigned gpio, const char *label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct gpio_desc *desc = gpio_to_desc(gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Compatibility: assume unavailable "valid" GPIOs will appear later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!desc && gpio_is_valid(gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return gpiod_request(desc, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) EXPORT_SYMBOL_GPL(gpio_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * gpio_request_array - request multiple GPIOs in a single call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * @array: array of the 'struct gpio'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @num: how many GPIOs in the array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int gpio_request_array(const struct gpio *array, size_t num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) for (i = 0; i < num; i++, array++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) err = gpio_request_one(array->gpio, array->flags, array->label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) while (i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) gpio_free((--array)->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) EXPORT_SYMBOL_GPL(gpio_request_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * gpio_free_array - release multiple GPIOs in a single call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * @array: array of the 'struct gpio'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * @num: how many GPIOs in the array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void gpio_free_array(const struct gpio *array, size_t num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) while (num--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) gpio_free((array++)->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) EXPORT_SYMBOL_GPL(gpio_free_array);