Linux Audio

Check our new training course

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