Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/gpio/consumer.h>
0003 #include <linux/gpio/driver.h>
0004 
0005 #include <linux/gpio.h>
0006 
0007 #include "gpiolib.h"
0008 
0009 void gpio_free(unsigned gpio)
0010 {
0011     gpiod_free(gpio_to_desc(gpio));
0012 }
0013 EXPORT_SYMBOL_GPL(gpio_free);
0014 
0015 /**
0016  * gpio_request_one - request a single GPIO with initial configuration
0017  * @gpio:   the GPIO number
0018  * @flags:  GPIO configuration as specified by GPIOF_*
0019  * @label:  a literal description string of this GPIO
0020  */
0021 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
0022 {
0023     struct gpio_desc *desc;
0024     int err;
0025 
0026     desc = gpio_to_desc(gpio);
0027 
0028     /* Compatibility: assume unavailable "valid" GPIOs will appear later */
0029     if (!desc && gpio_is_valid(gpio))
0030         return -EPROBE_DEFER;
0031 
0032     err = gpiod_request(desc, label);
0033     if (err)
0034         return err;
0035 
0036     if (flags & GPIOF_OPEN_DRAIN)
0037         set_bit(FLAG_OPEN_DRAIN, &desc->flags);
0038 
0039     if (flags & GPIOF_OPEN_SOURCE)
0040         set_bit(FLAG_OPEN_SOURCE, &desc->flags);
0041 
0042     if (flags & GPIOF_ACTIVE_LOW)
0043         set_bit(FLAG_ACTIVE_LOW, &desc->flags);
0044 
0045     if (flags & GPIOF_DIR_IN)
0046         err = gpiod_direction_input(desc);
0047     else
0048         err = gpiod_direction_output_raw(desc,
0049                 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
0050 
0051     if (err)
0052         goto free_gpio;
0053 
0054     if (flags & GPIOF_EXPORT) {
0055         err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
0056         if (err)
0057             goto free_gpio;
0058     }
0059 
0060     return 0;
0061 
0062  free_gpio:
0063     gpiod_free(desc);
0064     return err;
0065 }
0066 EXPORT_SYMBOL_GPL(gpio_request_one);
0067 
0068 int gpio_request(unsigned gpio, const char *label)
0069 {
0070     struct gpio_desc *desc = gpio_to_desc(gpio);
0071 
0072     /* Compatibility: assume unavailable "valid" GPIOs will appear later */
0073     if (!desc && gpio_is_valid(gpio))
0074         return -EPROBE_DEFER;
0075 
0076     return gpiod_request(desc, label);
0077 }
0078 EXPORT_SYMBOL_GPL(gpio_request);
0079 
0080 /**
0081  * gpio_request_array - request multiple GPIOs in a single call
0082  * @array:  array of the 'struct gpio'
0083  * @num:    how many GPIOs in the array
0084  */
0085 int gpio_request_array(const struct gpio *array, size_t num)
0086 {
0087     int i, err;
0088 
0089     for (i = 0; i < num; i++, array++) {
0090         err = gpio_request_one(array->gpio, array->flags, array->label);
0091         if (err)
0092             goto err_free;
0093     }
0094     return 0;
0095 
0096 err_free:
0097     while (i--)
0098         gpio_free((--array)->gpio);
0099     return err;
0100 }
0101 EXPORT_SYMBOL_GPL(gpio_request_array);
0102 
0103 /**
0104  * gpio_free_array - release multiple GPIOs in a single call
0105  * @array:  array of the 'struct gpio'
0106  * @num:    how many GPIOs in the array
0107  */
0108 void gpio_free_array(const struct gpio *array, size_t num)
0109 {
0110     while (num--)
0111         gpio_free((array++)->gpio);
0112 }
0113 EXPORT_SYMBOL_GPL(gpio_free_array);