General Purpose Input/Output (GPIO)

Contents:

Core

structgpio_irq_chip

GPIO interrupt controller

Definition

struct gpio_irq_chip {  struct irq_chip *chip;  struct irq_domain *domain;  const struct irq_domain_ops *domain_ops;#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY;  struct fwnode_handle *fwnode;  struct irq_domain *parent_domain;  int (*child_to_parent_hwirq)(struct gpio_chip *gc,unsigned int child_hwirq,unsigned int child_type,unsigned int *parent_hwirq, unsigned int *parent_type);  void *(*populate_parent_alloc_arg)(struct gpio_chip *gc,unsigned int parent_hwirq, unsigned int parent_type);  unsigned int (*child_offset_to_irq)(struct gpio_chip *gc, unsigned int pin);  struct irq_domain_ops child_irq_domain_ops;#endif;  irq_flow_handler_t handler;  unsigned int default_type;  struct lock_class_key *lock_key;  struct lock_class_key *request_key;  irq_flow_handler_t parent_handler;  void *parent_handler_data;  unsigned int num_parents;  unsigned int *parents;  unsigned int *map;  bool threaded;  int (*init_hw)(struct gpio_chip *gc);  void (*init_valid_mask)(struct gpio_chip *gc,unsigned long *valid_mask, unsigned int ngpios);  unsigned long *valid_mask;  unsigned int first;  void (*irq_enable)(struct irq_data *data);  void (*irq_disable)(struct irq_data *data);  void (*irq_unmask)(struct irq_data *data);  void (*irq_mask)(struct irq_data *data);};

Members

chip
GPIO IRQ chip implementation, provided by GPIO driver.
domain
Interrupt translation domain; responsible for mapping between GPIOhwirq number and Linux IRQ number.
domain_ops
Table of interrupt domain operations for this IRQ chip.
fwnode
Firmware node corresponding to this gpiochip/irqchip, necessaryfor hierarchical irqdomain support.
parent_domain
If non-NULL, will be set as the parent of this GPIO interruptcontroller’s IRQ domain to establish a hierarchical interruptdomain. The presence of this will activate the hierarchicalinterrupt support.
child_to_parent_hwirq

This callback translates a child hardware IRQ offset to a parenthardware IRQ offset on a hierarchical interrupt chip. The childhardware IRQs correspond to the GPIO index 0..ngpio-1 (see thengpio field of struct gpio_chip) and the corresponding parenthardware IRQ and type (such as IRQ_TYPE_*) shall be returned bythe driver. The driver can calculate this from an offset or usinga lookup table or whatever method is best for this chip. Return0 on successful translation in the driver.

If some ranges of hardware IRQs do not have a corresponding parentHWIRQ, return -EINVAL, but also make sure to fill invalid_mask andneed_valid_mask to make these GPIO lines unavailable fortranslation.

populate_parent_alloc_arg
This optional callback allocates and populates the specific structfor the parent’s IRQ domain. If this is not specified, thengpiochip_populate_parent_fwspec_twocell will be used. A four-cellvariant namedgpiochip_populate_parent_fwspec_fourcell is alsoavailable.
child_offset_to_irq
This optional callback is used to translate the child’s GPIO lineoffset on the GPIO chip to an IRQ number for the GPIO to_irq()callback. If this is not specified, then a default callback will beprovided that returns the line offset.
child_irq_domain_ops
The IRQ domain operations that will be used for this GPIO IRQchip. If no operations are provided, then default callbacks willbe populated to setup the IRQ hierarchy. Some drivers need tosupply their own translate function.
handler
The IRQ handler to use (often a predefined IRQ core function) forGPIO IRQs, provided by GPIO driver.
default_type
Default IRQ triggering type applied during GPIO driverinitialization, provided by GPIO driver.
lock_key
Per GPIO IRQ chip lockdep class for IRQ lock.
request_key
Per GPIO IRQ chip lockdep class for IRQ request.
parent_handler
The interrupt handler for the GPIO chip’s parent interrupts, may beNULL if the parent interrupts are nested rather than cascaded.
parent_handler_data
Data associated, and passed to, the handler for the parentinterrupt.
num_parents
The number of interrupt parents of a GPIO chip.
parents
A list of interrupt parents of a GPIO chip. This is owned by thedriver, so the core will only reference this list, not modify it.
map
A list of interrupt parents for each line of a GPIO chip.
threaded
True if set the interrupt handling uses nested threads.
init_hw
optional routine to initialize hardware beforean IRQ chip will be added. This is quite useful whena particular driver wants to clear IRQ related registersin order to avoid undesired events.
init_valid_mask
optional routine to initializevalid_mask, to beused if not all GPIO lines are valid interrupts. Sometimes somelines just cannot fire interrupts, and this routine, when defined,is passed a bitmap in “valid_mask” and it will have ngpiosbits from 0..(ngpios-1) set to “1” as in valid. The callback canthen directly set some bits to “0” if they cannot be used forinterrupts.
valid_mask
If notNULL holds bitmask of GPIOs which are valid to be includedin IRQ domain of the chip.
first
Required for static IRQ allocation. If set, irq_domain_add_simple()will allocate and map all IRQs during initialization.
irq_enable
Store old irq_chip irq_enable callback
irq_disable
Store old irq_chip irq_disable callback
irq_unmask
Store old irq_chip irq_unmask callback
irq_mask
Store old irq_chip irq_mask callback
structgpio_chip

abstract a GPIO controller

Definition

struct gpio_chip {  const char              *label;  struct gpio_device      *gpiodev;  struct device           *parent;  struct module           *owner;  int (*request)(struct gpio_chip *gc, unsigned int offset);  void (*free)(struct gpio_chip *gc, unsigned int offset);  int (*get_direction)(struct gpio_chip *gc, unsigned int offset);  int (*direction_input)(struct gpio_chip *gc, unsigned int offset);  int (*direction_output)(struct gpio_chip *gc, unsigned int offset, int value);  int (*get)(struct gpio_chip *gc, unsigned int offset);  int (*get_multiple)(struct gpio_chip *gc,unsigned long *mask, unsigned long *bits);  void (*set)(struct gpio_chip *gc, unsigned int offset, int value);  void (*set_multiple)(struct gpio_chip *gc,unsigned long *mask, unsigned long *bits);  int (*set_config)(struct gpio_chip *gc,unsigned int offset, unsigned long config);  int (*to_irq)(struct gpio_chip *gc, unsigned int offset);  void (*dbg_show)(struct seq_file *s, struct gpio_chip *gc);  int (*init_valid_mask)(struct gpio_chip *gc,unsigned long *valid_mask, unsigned int ngpios);  int (*add_pin_ranges)(struct gpio_chip *gc);  int base;  u16 ngpio;  const char              *const *names;  bool can_sleep;#if IS_ENABLED(CONFIG_GPIO_GENERIC);  unsigned long (*read_reg)(void __iomem *reg);  void (*write_reg)(void __iomem *reg, unsigned long data);  bool be_bits;  void __iomem *reg_dat;  void __iomem *reg_set;  void __iomem *reg_clr;  void __iomem *reg_dir_out;  void __iomem *reg_dir_in;  bool bgpio_dir_unreadable;  int bgpio_bits;  spinlock_t bgpio_lock;  unsigned long bgpio_data;  unsigned long bgpio_dir;#endif ;#ifdef CONFIG_GPIOLIB_IRQCHIP;  struct gpio_irq_chip irq;#endif ;  unsigned long *valid_mask;#if defined(CONFIG_OF_GPIO);  struct device_node *of_node;  unsigned int of_gpio_n_cells;  int (*of_xlate)(struct gpio_chip *gc, const struct of_phandle_args *gpiospec, u32 *flags);#endif ;};

Members

label
a functional name for the GPIO device, such as a partnumber or the name of the SoC IP-block implementing it.
gpiodev
the internal state holder, opaque struct
parent
optional parent device providing the GPIOs
owner
helps prevent removal of modules exporting active GPIOs
request
optional hook for chip-specific activation, such asenabling module power and clock; may sleep
free
optional hook for chip-specific deactivation, such asdisabling module power and clock; may sleep
get_direction
returns direction for signal “offset”, 0=out, 1=in,(same as GPIO_LINE_DIRECTION_OUT / GPIO_LINE_DIRECTION_IN),or negative error. It is recommended to always implement thisfunction, even on input-only or output-only gpio chips.
direction_input
configures signal “offset” as input, or returns errorThis can be omitted on input-only or output-only gpio chips.
direction_output
configures signal “offset” as output, or returns errorThis can be omitted on input-only or output-only gpio chips.
get
returns value for signal “offset”, 0=low, 1=high, or negative error
get_multiple
reads values for multiple signals defined by “mask” andstores them in “bits”, returns 0 on success or negative error
set
assigns output value for signal “offset”
set_multiple
assigns output values for multiple signals defined by “mask”
set_config
optional hook for all kinds of settings. Uses the samepacked config format as generic pinconf.
to_irq
optional hook supporting non-static gpio_to_irq() mappings;implementation may not sleep
dbg_show
optional routine to show contents in debugfs; default codewill be used when this is omitted, but custom code can show extrastate (such as pullup/pulldown configuration).
init_valid_mask
optional routine to initializevalid_mask, to be used ifnot all GPIOs are valid.
add_pin_ranges
optional routine to initialize pin ranges, to be used whenrequires special mapping of the pins that provides GPIO functionality.It is called after adding GPIO chip and before adding IRQ chip.
base
identifies the first GPIO number handled by this chip;or, if negative during registration, requests dynamic ID allocation.DEPRECATION: providing anything non-negative and nailing the baseoffset of GPIO chips is deprecated. Please pass -1 as base tolet gpiolib select the chip base in all possible cases. We want toget rid of the static GPIO number space in the long run.
ngpio
the number of GPIOs handled by this controller; the last GPIOhandled is (base + ngpio - 1).
names
if set, must be an array of strings to use as alternativenames for the GPIOs in this chip. Any entry in the arraymay be NULL if there is no alias for the GPIO, however thearray must bengpio entries long. A name can include a single printkformat specifier for an unsigned int. It is substituted by the actualnumber of the gpio.
can_sleep
flag must be set iff get()/set() methods sleep, as theymust while accessing GPIO expander chips over I2C or SPI. Thisimplies that if the chip supports IRQs, these IRQs need to be threadedas the chip access may sleep when e.g. reading out the IRQ statusregisters.
read_reg
reader function for generic GPIO
write_reg
writer function for generic GPIO
be_bits
if the generic GPIO has big endian bit order (bit 31 is representingline 0, bit 30 is line 1 … bit 0 is line 31) this is set to true by thegeneric GPIO core. It is for internal housekeeping only.
reg_dat
data (in) register for generic GPIO
reg_set
output set register (out=high) for generic GPIO
reg_clr
output clear register (out=low) for generic GPIO
reg_dir_out
direction out setting register for generic GPIO
reg_dir_in
direction in setting register for generic GPIO
bgpio_dir_unreadable
indicates that the direction register(s) cannotbe read and we need to rely on out internal state tracking.
bgpio_bits
number of register bits used for a generic GPIO i.e.<register width> * 8
bgpio_lock
used to lock chip->bgpio_data. Also, this is needed to keepshadowed and real data registers writes together.
bgpio_data
shadowed data register for generic GPIO to clear/set bitssafely.
bgpio_dir
shadowed direction register for generic GPIO to clear/setdirection safely. A “1” in this word means the line is set asoutput.
irq
Integrates interrupt chip functionality with the GPIO chip. Can beused to handle IRQs for most practical cases.
valid_mask
If notNULL holds bitmask of GPIOs which are valid to be usedfrom the chip.
of_node
Pointer to a device tree node representing this GPIO controller.
of_gpio_n_cells
Number of cells used to form the GPIO specifier.
of_xlate
Callback to translate a device tree GPIO specifier into a chip-relative GPIO number and flags.

Description

A gpio_chip can help platforms abstract various sources of GPIOs sothey can all be accessed through a common programing interface.Example sources would be SOC controllers, FPGAs, multifunctionchips, dedicated GPIO expanders, and so on.

Each chip controls a number of signals, identified in method callsby “offset” values in the range 0..(ngpio - 1). When those signalsare referenced through calls like gpio_get_value(gpio), the offsetis calculated by subtractingbase from the gpio number.

for_each_requested_gpio_in_range(chip,i,base,size,label)

iterates over requested GPIOs in a given range

Parameters

chip
the chip to query
i
loop variable
base
first GPIO in the range
size
amount of GPIOs to check starting frombase
label
label of current GPIO
gpiochip_add_data(gc,data)

register a gpio_chip

Parameters

gc
the chip to register, with gc->base initialized
data
driver-private data associated with this chip

Context

potentially before irqs will work

Description

Whengpiochip_add_data() is called very early during boot, so that GPIOscan be freely used, the gc->parent device must be registered beforethe gpio framework’s arch_initcall(). Otherwise sysfs initializationfor GPIOs will fail rudely.

gpiochip_add_data() must only be called after gpiolib initialization,ie after core_initcall().

If gc->base is negative, this requests dynamic assignment ofa range of valid GPIOs.

Return

A negative errno if the chip can’t be registered, such as because thegc->base is invalid or already associated with a different chip.Otherwise it returns zero as a success code.

structgpio_pin_range

pin range controlled by a gpio chip

Definition

struct gpio_pin_range {  struct list_head node;  struct pinctrl_dev *pctldev;  struct pinctrl_gpio_range range;};

Members

node
list for maintaining set of pin ranges, used internally
pctldev
pinctrl device which handles corresponding pins
range
actual range of pins controlled by a gpio controller
struct gpio_desc *gpio_to_desc(unsigned gpio)

Convert a GPIO number to its descriptor

Parameters

unsignedgpio
global GPIO number

Return

The GPIO descriptor associated with the given GPIO, orNULL if no GPIOwith the given number exists in the system.

struct gpio_desc *gpiochip_get_desc(structgpio_chip * gc, unsigned int hwnum)

get the GPIO descriptor corresponding to the given hardware number for this chip

Parameters

structgpio_chip*gc
GPIO chip
unsignedinthwnum
hardware number of the GPIO for this chip

Return

A pointer to the GPIO descriptor orERR_PTR(-EINVAL) if no GPIO existsin the given chip for the specified hardware number.

intdesc_to_gpio(const struct gpio_desc * desc)

convert a GPIO descriptor to the integer namespace

Parameters

conststructgpio_desc*desc
GPIO descriptor

Description

This should disappear in the future but is needed since we stilluse GPIO numbers for error messages and sysfs nodes.

Return

The global GPIO number for the GPIO specified by its descriptor.

structgpio_chip *gpiod_to_chip(const struct gpio_desc * desc)

Return the GPIO chip to which a GPIO descriptor belongs

Parameters

conststructgpio_desc*desc
descriptor to return the chip of
intgpiod_get_direction(struct gpio_desc * desc)

return the current direction of a GPIO

Parameters

structgpio_desc*desc
GPIO to get the direction of

Description

Returns 0 for output, 1 for input, or an error code in case of error.

This function may sleep ifgpiod_cansleep() is true.

void *gpiochip_get_data(structgpio_chip * gc)

get per-subdriver data for the chip

Parameters

structgpio_chip*gc
GPIO chip

Return

The per-subdriver data for the chip.

voidgpiochip_remove(structgpio_chip * gc)

unregister a gpio_chip

Parameters

structgpio_chip*gc
the chip to unregister

Description

A gpio_chip with any GPIOs still requested may not be removed.

structgpio_chip *gpiochip_find(void * data, int (*match)(structgpio_chip *gc, void *data))

iterator for locating a specific gpio_chip

Parameters

void*data
data to pass to match function
int(*)(structgpio_chip*gc,void*data)match
Callback function to check gpio_chip

Description

Similar to bus_find_device. It returns a reference to a gpio_chip asdetermined by a user suppliedmatch callback. The callback should return0 if the device doesn’t match and non-zero if it does. If the callback isnon-zero, this function will return to the caller and not iterate over anymore gpio_chips.

voidgpiochip_set_nested_irqchip(structgpio_chip * gc, structirq_chip * irqchip, unsigned int parent_irq)

connects a nested irqchip to a gpiochip

Parameters

structgpio_chip*gc
the gpiochip to set the irqchip nested handler to
structirq_chip*irqchip
the irqchip to nest to the gpiochip
unsignedintparent_irq
the irq number corresponding to the parent IRQ for thisnested irqchip
intgpiochip_irq_map(struct irq_domain * d, unsigned int irq, irq_hw_number_t hwirq)

maps an IRQ into a GPIO irqchip

Parameters

structirq_domain*d
the irqdomain used by this irqchip
unsignedintirq
the global irq number used by this GPIO irqchip irq
irq_hw_number_thwirq
the local IRQ/GPIO line offset on this gpiochip

Description

This function will set up the mapping for a certain IRQ line on agpiochip by assigning the gpiochip as chip data, and using the irqchipstored inside the gpiochip.

intgpiochip_irq_domain_activate(struct irq_domain * domain, structirq_data * data, bool reserve)

Lock a GPIO to be used as an IRQ

Parameters

structirq_domain*domain
The IRQ domain used by this IRQ chip
structirq_data*data
Outermost irq_data associated with the IRQ
boolreserve
If set, only reserve an interrupt vector instead of assigning one

Description

This function is a wrapper that callsgpiochip_lock_as_irq() and is to beused as the activate function for thestructirq_domain_ops. The host_datafor the IRQ domain must be thestructgpio_chip.

voidgpiochip_irq_domain_deactivate(struct irq_domain * domain, structirq_data * data)

Unlock a GPIO used as an IRQ

Parameters

structirq_domain*domain
The IRQ domain used by this IRQ chip
structirq_data*data
Outermost irq_data associated with the IRQ

Description

This function is a wrapper that will callgpiochip_unlock_as_irq() and is tobe used as the deactivate function for thestructirq_domain_ops. Thehost_data for the IRQ domain must be thestructgpio_chip.

intgpiochip_irqchip_add_key(structgpio_chip * gc, structirq_chip * irqchip, unsigned int first_irq, irq_flow_handler_t handler, unsigned int type, bool threaded, struct lock_class_key * lock_key, struct lock_class_key * request_key)

adds an irqchip to a gpiochip

Parameters

structgpio_chip*gc
the gpiochip to add the irqchip to
structirq_chip*irqchip
the irqchip to add to the gpiochip
unsignedintfirst_irq
if not dynamically assigned, the base (first) IRQ toallocate gpiochip irqs from
irq_flow_handler_thandler
the irq handler to use (often a predefined irq core function)
unsignedinttype
the default type for IRQs on this irqchip, pass IRQ_TYPE_NONEto have the core avoid setting up any default type in the hardware.
boolthreaded
whether this irqchip uses a nested thread handler
structlock_class_key*lock_key
lockdep class for IRQ lock
structlock_class_key*request_key
lockdep class for IRQ request

Description

This function closely associates a certain irqchip with a certaingpiochip, providing an irq domain to translate the local IRQs toglobal irqs in the gpiolib core, and making sure that the gpiochipis passed as chip data to all related functions. Driver callbacksneed to usegpiochip_get_data() to get their local state containers backfrom the gpiochip passed as chip data. An irqdomain will be storedin the gpiochip that shall be used by the driver to handle IRQ numbertranslation. The gpiochip will need to be initialized and registeredbefore calling this function.

This function will handle two cell:ed simple IRQs and assumes allthe pins on the gpiochip can generate a unique IRQ. Everything elseneed to be open coded.

intgpiochip_irqchip_add_domain(structgpio_chip * gc, struct irq_domain * domain)

adds an irqdomain to a gpiochip

Parameters

structgpio_chip*gc
the gpiochip to add the irqchip to
structirq_domain*domain
the irqdomain to add to the gpiochip

Description

This function adds an IRQ domain to the gpiochip.

intgpiochip_generic_request(structgpio_chip * gc, unsigned offset)

request the gpio function for a pin

Parameters

structgpio_chip*gc
the gpiochip owning the GPIO
unsignedoffset
the offset of the GPIO to request for GPIO function
voidgpiochip_generic_free(structgpio_chip * gc, unsigned offset)

free the gpio function from a pin

Parameters

structgpio_chip*gc
the gpiochip to request the gpio function for
unsignedoffset
the offset of the GPIO to free from GPIO function
intgpiochip_generic_config(structgpio_chip * gc, unsigned offset, unsigned long config)

apply configuration for a pin

Parameters

structgpio_chip*gc
the gpiochip owning the GPIO
unsignedoffset
the offset of the GPIO to apply the configuration
unsignedlongconfig
the configuration to be applied
intgpiochip_add_pingroup_range(structgpio_chip * gc, struct pinctrl_dev * pctldev, unsigned int gpio_offset, const char * pin_group)

add a range for GPIO <-> pin mapping

Parameters

structgpio_chip*gc
the gpiochip to add the range for
structpinctrl_dev*pctldev
the pin controller to map to
unsignedintgpio_offset
the start offset in the current gpio_chip number space
constchar*pin_group
name of the pin group inside the pin controller

Description

Calling this function directly from a DeviceTree-supportedpinctrl driver is DEPRECATED. Please see Section 2.1 ofDocumentation/devicetree/bindings/gpio/gpio.txt on how tobind pinctrl and gpio drivers via the “gpio-ranges” property.

intgpiochip_add_pin_range(structgpio_chip * gc, const char * pinctl_name, unsigned int gpio_offset, unsigned int pin_offset, unsigned int npins)

add a range for GPIO <-> pin mapping

Parameters

structgpio_chip*gc
the gpiochip to add the range for
constchar*pinctl_name
the dev_name() of the pin controller to map to
unsignedintgpio_offset
the start offset in the current gpio_chip number space
unsignedintpin_offset
the start offset in the pin controller number space
unsignedintnpins
the number of pins from the offset of each pin space (GPIO andpin controller) to accumulate in this range

Return

0 on success, or a negative error-code on failure.

Description

Calling this function directly from a DeviceTree-supportedpinctrl driver is DEPRECATED. Please see Section 2.1 ofDocumentation/devicetree/bindings/gpio/gpio.txt on how tobind pinctrl and gpio drivers via the “gpio-ranges” property.

voidgpiochip_remove_pin_ranges(structgpio_chip * gc)

remove all the GPIO <-> pin mappings

Parameters

structgpio_chip*gc
the chip to remove all the mappings for
const char *gpiochip_is_requested(structgpio_chip * gc, unsigned offset)

return string iff signal was requested

Parameters

structgpio_chip*gc
controller managing the signal
unsignedoffset
of signal within controller’s 0..(ngpio - 1) range

Description

Returns NULL if the GPIO is not currently requested, else a string.The string returned is the label passed to gpio_request(); if none has beenpassed it is a meaningless, non-NULL constant.

This function is for use by GPIO controller drivers. The label canhelp with diagnostics, and knowing that the signal is used as a GPIOcan help avoid accidentally multiplexing it to another controller.

struct gpio_desc *gpiochip_request_own_desc(structgpio_chip * gc, unsigned int hwnum, const char * label, enum gpio_lookup_flags lflags, enum gpiod_flags dflags)

Allow GPIO chip to request its own descriptor

Parameters

structgpio_chip*gc
GPIO chip
unsignedinthwnum
hardware number of the GPIO for which to request the descriptor
constchar*label
label for the GPIO
enumgpio_lookup_flagslflags
lookup flags for this GPIO or 0 if default, this can be used tospecify things like line inversion semantics with the machine flagssuch as GPIO_OUT_LOW
enumgpiod_flagsdflags
descriptor request flags for this GPIO or 0 if default, thiscan be used to specify consumer semantics such as open drain

Description

Function allows GPIO chip drivers to request and use their own GPIOdescriptors via gpiolib API. Difference to gpiod_request() is that thisfunction will not increase reference count of the GPIO chip module. Thisallows the GPIO chip module to be unloaded as needed (we assume that theGPIO chip driver handles freeing the GPIOs it has requested).

Return

A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative errorcode on failure.

voidgpiochip_free_own_desc(struct gpio_desc * desc)

Free GPIO requested by the chip driver

Parameters

structgpio_desc*desc
GPIO descriptor to free

Description

Function frees the given GPIO requested previously withgpiochip_request_own_desc().

intgpiod_direction_input(struct gpio_desc * desc)

set the GPIO direction to input

Parameters

structgpio_desc*desc
GPIO to set to input

Description

Set the direction of the passed GPIO to input, such asgpiod_get_value() canbe called safely on it.

Return 0 in case of success, else an error code.

intgpiod_direction_output_raw(struct gpio_desc * desc, int value)

set the GPIO direction to output

Parameters

structgpio_desc*desc
GPIO to set to output
intvalue
initial output value of the GPIO

Description

Set the direction of the passed GPIO to output, such asgpiod_set_value() canbe called safely on it. The initial value of the output must be specifiedas raw value on the physical line without regard for the ACTIVE_LOW status.

Return 0 in case of success, else an error code.

intgpiod_direction_output(struct gpio_desc * desc, int value)

set the GPIO direction to output

Parameters

structgpio_desc*desc
GPIO to set to output
intvalue
initial output value of the GPIO

Description

Set the direction of the passed GPIO to output, such asgpiod_set_value() canbe called safely on it. The initial value of the output must be specifiedas the logical value of the GPIO, i.e. taking its ACTIVE_LOW status intoaccount.

Return 0 in case of success, else an error code.

intgpiod_set_config(struct gpio_desc * desc, unsigned long config)

setsconfig for a GPIO

Parameters

structgpio_desc*desc
descriptor of the GPIO for which to set the configuration
unsignedlongconfig
Same packed config format as generic pinconf

Return

0 on success,-ENOTSUPP if the controller doesn’t support setting theconfiguration.

intgpiod_set_debounce(struct gpio_desc * desc, unsigned debounce)

setsdebounce time for a GPIO

Parameters

structgpio_desc*desc
descriptor of the GPIO for which to set debounce time
unsigneddebounce
debounce time in microseconds

Return

0 on success,-ENOTSUPP if the controller doesn’t support setting thedebounce time.

intgpiod_set_transitory(struct gpio_desc * desc, bool transitory)

Lose or retain GPIO state on suspend or reset

Parameters

structgpio_desc*desc
descriptor of the GPIO for which to configure persistence
booltransitory
True to lose state on suspend or reset, false for persistence

Return

0 on success, otherwise a negative error code.

intgpiod_is_active_low(const struct gpio_desc * desc)

test whether a GPIO is active-low or not

Parameters

conststructgpio_desc*desc
the gpio descriptor to test

Description

Returns 1 if the GPIO is active-low, 0 otherwise.

voidgpiod_toggle_active_low(struct gpio_desc * desc)

toggle whether a GPIO is active-low or not

Parameters

structgpio_desc*desc
the gpio descriptor to change
intgpiod_get_raw_value(const struct gpio_desc * desc)

return a gpio’s raw value

Parameters

conststructgpio_desc*desc
gpio whose value will be returned

Description

Return the GPIO’s raw value, i.e. the value of the physical line disregardingits ACTIVE_LOW status, or negative errno on failure.

This function can be called from contexts where we cannot sleep, and willcomplain if the GPIO chip functions potentially sleep.

intgpiod_get_value(const struct gpio_desc * desc)

return a gpio’s value

Parameters

conststructgpio_desc*desc
gpio whose value will be returned

Description

Return the GPIO’s logical value, i.e. taking the ACTIVE_LOW status intoaccount, or negative errno on failure.

This function can be called from contexts where we cannot sleep, and willcomplain if the GPIO chip functions potentially sleep.

intgpiod_get_raw_array_value(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

read raw values from an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be read
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap to store the read values

Description

Read the raw values of the GPIOs, i.e. the values of the physical lineswithout regard for their ACTIVE_LOW status. Return 0 in case of success,else an error code.

This function can be called from contexts where we cannot sleep,and it will complain if the GPIO chip functions potentially sleep.

intgpiod_get_array_value(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

read values from an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be read
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap to store the read values

Description

Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW statusinto account. Return 0 in case of success, else an error code.

This function can be called from contexts where we cannot sleep,and it will complain if the GPIO chip functions potentially sleep.

voidgpiod_set_raw_value(struct gpio_desc * desc, int value)

assign a gpio’s raw value

Parameters

structgpio_desc*desc
gpio whose value will be assigned
intvalue
value to assign

Description

Set the raw value of the GPIO, i.e. the value of its physical line withoutregard for its ACTIVE_LOW status.

This function can be called from contexts where we cannot sleep, and willcomplain if the GPIO chip functions potentially sleep.

voidgpiod_set_value(struct gpio_desc * desc, int value)

assign a gpio’s value

Parameters

structgpio_desc*desc
gpio whose value will be assigned
intvalue
value to assign

Description

Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,OPEN_DRAIN and OPEN_SOURCE flags into account.

This function can be called from contexts where we cannot sleep, and willcomplain if the GPIO chip functions potentially sleep.

intgpiod_set_raw_array_value(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

assign values to an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be assigned
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap of values to assign

Description

Set the raw values of the GPIOs, i.e. the values of the physical lineswithout regard for their ACTIVE_LOW status.

This function can be called from contexts where we cannot sleep, and willcomplain if the GPIO chip functions potentially sleep.

intgpiod_set_array_value(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

assign values to an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be assigned
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap of values to assign

Description

Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW statusinto account.

This function can be called from contexts where we cannot sleep, and willcomplain if the GPIO chip functions potentially sleep.

intgpiod_cansleep(const struct gpio_desc * desc)

report whether gpio value access may sleep

Parameters

conststructgpio_desc*desc
gpio to check
intgpiod_set_consumer_name(struct gpio_desc * desc, const char * name)

set the consumer name for the descriptor

Parameters

structgpio_desc*desc
gpio to set the consumer name on
constchar*name
the new consumer name
intgpiod_to_irq(const struct gpio_desc * desc)

return the IRQ corresponding to a GPIO

Parameters

conststructgpio_desc*desc
gpio whose IRQ will be returned (already requested)

Description

Return the IRQ corresponding to the passed GPIO, or an error code in case oferror.

intgpiochip_lock_as_irq(structgpio_chip * gc, unsigned int offset)

lock a GPIO to be used as IRQ

Parameters

structgpio_chip*gc
the chip the GPIO to lock belongs to
unsignedintoffset
the offset of the GPIO to lock as IRQ

Description

This is used directly by GPIO drivers that want to lock downa certain GPIO line to be used for IRQs.

voidgpiochip_unlock_as_irq(structgpio_chip * gc, unsigned int offset)

unlock a GPIO used as IRQ

Parameters

structgpio_chip*gc
the chip the GPIO to lock belongs to
unsignedintoffset
the offset of the GPIO to lock as IRQ

Description

This is used directly by GPIO drivers that want to indicatethat a certain GPIO is no longer used exclusively for IRQ.

intgpiod_get_raw_value_cansleep(const struct gpio_desc * desc)

return a gpio’s raw value

Parameters

conststructgpio_desc*desc
gpio whose value will be returned

Description

Return the GPIO’s raw value, i.e. the value of the physical line disregardingits ACTIVE_LOW status, or negative errno on failure.

This function is to be called from contexts that can sleep.

intgpiod_get_value_cansleep(const struct gpio_desc * desc)

return a gpio’s value

Parameters

conststructgpio_desc*desc
gpio whose value will be returned

Description

Return the GPIO’s logical value, i.e. taking the ACTIVE_LOW status intoaccount, or negative errno on failure.

This function is to be called from contexts that can sleep.

intgpiod_get_raw_array_value_cansleep(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

read raw values from an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be read
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap to store the read values

Description

Read the raw values of the GPIOs, i.e. the values of the physical lineswithout regard for their ACTIVE_LOW status. Return 0 in case of success,else an error code.

This function is to be called from contexts that can sleep.

intgpiod_get_array_value_cansleep(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

read values from an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be read
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap to store the read values

Description

Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW statusinto account. Return 0 in case of success, else an error code.

This function is to be called from contexts that can sleep.

voidgpiod_set_raw_value_cansleep(struct gpio_desc * desc, int value)

assign a gpio’s raw value

Parameters

structgpio_desc*desc
gpio whose value will be assigned
intvalue
value to assign

Description

Set the raw value of the GPIO, i.e. the value of its physical line withoutregard for its ACTIVE_LOW status.

This function is to be called from contexts that can sleep.

voidgpiod_set_value_cansleep(struct gpio_desc * desc, int value)

assign a gpio’s value

Parameters

structgpio_desc*desc
gpio whose value will be assigned
intvalue
value to assign

Description

Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status intoaccount

This function is to be called from contexts that can sleep.

intgpiod_set_raw_array_value_cansleep(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

assign values to an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be assigned
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap of values to assign

Description

Set the raw values of the GPIOs, i.e. the values of the physical lineswithout regard for their ACTIVE_LOW status.

This function is to be called from contexts that can sleep.

intgpiod_set_array_value_cansleep(unsigned int array_size, struct gpio_desc ** desc_array, struct gpio_array * array_info, unsigned long * value_bitmap)

assign values to an array of GPIOs

Parameters

unsignedintarray_size
number of elements in the descriptor array / value bitmap
structgpio_desc**desc_array
array of GPIO descriptors whose values will be assigned
structgpio_array*array_info
information on applicability of fast bitmap processing path
unsignedlong*value_bitmap
bitmap of values to assign

Description

Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW statusinto account.

This function is to be called from contexts that can sleep.

voidgpiod_add_lookup_table(struct gpiod_lookup_table * table)

register GPIO device consumers

Parameters

structgpiod_lookup_table*table
table of consumers to register
voidgpiod_remove_lookup_table(struct gpiod_lookup_table * table)

unregister GPIO device consumers

Parameters

structgpiod_lookup_table*table
table of consumers to unregister
voidgpiod_add_hogs(struct gpiod_hog * hogs)

register a set of GPIO hogs from machine code

Parameters

structgpiod_hog*hogs
table of gpio hog entries with a zeroed sentinel at the end
struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle * fwnode, const char * con_id, int index, enum gpiod_flags flags, const char * label)

obtain a GPIO from firmware node

Parameters

structfwnode_handle*fwnode
handle of the firmware node
constchar*con_id
function within the GPIO consumer
intindex
index of the GPIO to obtain for the consumer
enumgpiod_flagsflags
GPIO initialization flags
constchar*label
label to attach to the requested GPIO

Description

This function can be used for drivers that get their configurationfrom opaque firmware.

The function properly finds the corresponding GPIO using whatever is theunderlying firmware interface and then makes sure that the GPIOdescriptor is requested before it is returned to the caller.

In case of error an ERR_PTR() is returned.

Return

On successful request the GPIO pin is configured in accordance withprovidedflags.

intgpiod_count(structdevice * dev, const char * con_id)

return the number of GPIOs associated with a device / function or -ENOENT if no GPIO has been assigned to the requested function

Parameters

structdevice*dev
GPIO consumer, can be NULL for system-global GPIOs
constchar*con_id
function within the GPIO consumer
struct gpio_desc *gpiod_get(structdevice * dev, const char * con_id, enum gpiod_flags flags)

obtain a GPIO for a given GPIO function

Parameters

structdevice*dev
GPIO consumer, can be NULL for system-global GPIOs
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

Return the GPIO descriptor corresponding to the function con_id of devicedev, -ENOENT if no GPIO has been assigned to the requested function, oranother IS_ERR() code if an error occurred while trying to acquire the GPIO.

struct gpio_desc *gpiod_get_optional(structdevice * dev, const char * con_id, enum gpiod_flags flags)

obtain an optional GPIO for a given GPIO function

Parameters

structdevice*dev
GPIO consumer, can be NULL for system-global GPIOs
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

This is equivalent togpiod_get(), except that when no GPIO was assigned tothe requested function it will return NULL. This is convenient for driversthat need to handle optional GPIOs.

struct gpio_desc *gpiod_get_index(structdevice * dev, const char * con_id, unsigned int idx, enum gpiod_flags flags)

obtain a GPIO from a multi-index GPIO function

Parameters

structdevice*dev
GPIO consumer, can be NULL for system-global GPIOs
constchar*con_id
function within the GPIO consumer
unsignedintidx
index of the GPIO to obtain in the consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

This variant ofgpiod_get() allows to access GPIOs other than the firstdefined one for functions that define several GPIOs.

Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to therequested function and/or index, or another IS_ERR() code if an erroroccurred while trying to acquire the GPIO.

struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle * fwnode, const char * propname, int index, enum gpiod_flags dflags, const char * label)

obtain a GPIO from firmware node

Parameters

structfwnode_handle*fwnode
handle of the firmware node
constchar*propname
name of the firmware property representing the GPIO
intindex
index of the GPIO to obtain for the consumer
enumgpiod_flagsdflags
GPIO initialization flags
constchar*label
label to attach to the requested GPIO

Description

This function can be used for drivers that get their configurationfrom opaque firmware.

The function properly finds the corresponding GPIO using whatever is theunderlying firmware interface and then makes sure that the GPIOdescriptor is requested before it is returned to the caller.

In case of error an ERR_PTR() is returned.

Return

On successful request the GPIO pin is configured in accordance withprovideddflags.

struct gpio_desc *gpiod_get_index_optional(structdevice * dev, const char * con_id, unsigned int index, enum gpiod_flags flags)

obtain an optional GPIO from a multi-index GPIO function

Parameters

structdevice*dev
GPIO consumer, can be NULL for system-global GPIOs
constchar*con_id
function within the GPIO consumer
unsignedintindex
index of the GPIO to obtain in the consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

This is equivalent togpiod_get_index(), except that when no GPIO with thespecified index was assigned to the requested function it will return NULL.This is convenient for drivers that need to handle optional GPIOs.

struct gpio_descs *gpiod_get_array(structdevice * dev, const char * con_id, enum gpiod_flags flags)

obtain multiple GPIOs from a multi-index GPIO function

Parameters

structdevice*dev
GPIO consumer, can be NULL for system-global GPIOs
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

This function acquires all the GPIOs defined under a given function.

Return a struct gpio_descs containing an array of descriptors, -ENOENT ifno GPIO has been assigned to the requested function, or another IS_ERR()code if an error occurred while trying to acquire the GPIOs.

struct gpio_descs *gpiod_get_array_optional(structdevice * dev, const char * con_id, enum gpiod_flags flags)

obtain multiple GPIOs from a multi-index GPIO function

Parameters

structdevice*dev
GPIO consumer, can be NULL for system-global GPIOs
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

This is equivalent togpiod_get_array(), except that when no GPIO wasassigned to the requested function it will return NULL.

voidgpiod_put(struct gpio_desc * desc)

dispose of a GPIO descriptor

Parameters

structgpio_desc*desc
GPIO descriptor to dispose of

Description

No descriptor can be used aftergpiod_put() has been called on it.

voidgpiod_put_array(struct gpio_descs * descs)

dispose of multiple GPIO descriptors

Parameters

structgpio_descs*descs
struct gpio_descs containing an array of descriptors

ACPI support

voidacpi_gpiochip_request_interrupts(structgpio_chip * chip)

Register isr for gpio chip ACPI events

Parameters

structgpio_chip*chip
GPIO chip

Description

ACPI5 platforms can use GPIO signaled ACPI events. These GPIO interrupts arehandled by ACPI event methods which need to be called from the GPIOchip’s interrupt handler.acpi_gpiochip_request_interrupts() finds out whichGPIO pins have ACPI event methods and assigns interrupt handlers that callsthe ACPI event methods for those pins.

voidacpi_gpiochip_free_interrupts(structgpio_chip * chip)

Free GPIO ACPI event interrupts.

Parameters

structgpio_chip*chip
GPIO chip

Description

Free interrupts associated with GPIO ACPI event method for the givenGPIO chip.

intacpi_dev_gpio_irq_get(struct acpi_device * adev, int index)

Find GpioInt and translate it to Linux IRQ number

Parameters

structacpi_device*adev
pointer to a ACPI device to get IRQ from
intindex
index of GpioInt resource (starting from0)

Description

If the device has one or more GpioInt resources, this function can beused to translate from the GPIO offset in the resource to the Linux IRQnumber.

The function is idempotent, though each time it runs it will configure GPIOpin direction according to the flags in GpioInt resource.

Return

Linux IRQ number (>0) on success, negative errno on failure.

Device tree support

struct gpio_desc *gpiod_get_from_of_node(struct device_node * node, const char * propname, int index, enum gpiod_flags dflags, const char * label)

obtain a GPIO from an OF node

Parameters

structdevice_node*node
handle of the OF node
constchar*propname
name of the DT property representing the GPIO
intindex
index of the GPIO to obtain for the consumer
enumgpiod_flagsdflags
GPIO initialization flags
constchar*label
label to attach to the requested GPIO

Return

On successful request the GPIO pin is configured in accordance withprovideddflags.

Description

In case of error an ERR_PTR() is returned.

intof_mm_gpiochip_add_data(struct device_node * np, struct of_mm_gpio_chip * mm_gc, void * data)

Add memory mapped GPIO chip (bank)

Parameters

structdevice_node*np
device node of the GPIO chip
structof_mm_gpio_chip*mm_gc
pointer to the of_mm_gpio_chip allocated structure
void*data
driver data to store in the struct gpio_chip

Description

To use this function you should allocate and fill mm_gc with:

  1. In the gpio_chip structure:- all the callbacks- of_gpio_n_cells- of_xlate callback (optional)
  1. In the of_mm_gpio_chip structure:- save_regs callback (optional)

If succeeded, this function will map bank’s memory and willdo all necessary work for you. Then you’ll able to use .regsto manage GPIOs from the callbacks.

voidof_mm_gpiochip_remove(struct of_mm_gpio_chip * mm_gc)

Remove memory mapped GPIO chip (bank)

Parameters

structof_mm_gpio_chip*mm_gc
pointer to the of_mm_gpio_chip allocated structure

Device-managed API

struct gpio_desc *devm_gpiod_get(structdevice * dev, const char * con_id, enum gpiod_flags flags)

Resource-managedgpiod_get()

Parameters

structdevice*dev
GPIO consumer
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

Managedgpiod_get(). GPIO descriptors returned from this function areautomatically disposed on driver detach. Seegpiod_get() for detailedinformation about behavior and return values.

struct gpio_desc *devm_gpiod_get_optional(structdevice * dev, const char * con_id, enum gpiod_flags flags)

Resource-managedgpiod_get_optional()

Parameters

structdevice*dev
GPIO consumer
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

Managedgpiod_get_optional(). GPIO descriptors returned from this functionare automatically disposed on driver detach. Seegpiod_get_optional() fordetailed information about behavior and return values.

struct gpio_desc *devm_gpiod_get_index(structdevice * dev, const char * con_id, unsigned int idx, enum gpiod_flags flags)

Resource-managedgpiod_get_index()

Parameters

structdevice*dev
GPIO consumer
constchar*con_id
function within the GPIO consumer
unsignedintidx
index of the GPIO to obtain in the consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

Managedgpiod_get_index(). GPIO descriptors returned from this function areautomatically disposed on driver detach. Seegpiod_get_index() for detailedinformation about behavior and return values.

struct gpio_desc *devm_gpiod_get_from_of_node(structdevice * dev, struct device_node * node, const char * propname, int index, enum gpiod_flags dflags, const char * label)

obtain a GPIO from an OF node

Parameters

structdevice*dev
device for lifecycle management
structdevice_node*node
handle of the OF node
constchar*propname
name of the DT property representing the GPIO
intindex
index of the GPIO to obtain for the consumer
enumgpiod_flagsdflags
GPIO initialization flags
constchar*label
label to attach to the requested GPIO

Return

On successful request the GPIO pin is configured in accordance withprovideddflags.

Description

In case of error an ERR_PTR() is returned.

struct gpio_desc *devm_fwnode_gpiod_get_index(structdevice * dev, struct fwnode_handle * fwnode, const char * con_id, int index, enum gpiod_flags flags, const char * label)

get a GPIO descriptor from a given node

Parameters

structdevice*dev
GPIO consumer
structfwnode_handle*fwnode
firmware node containing GPIO reference
constchar*con_id
function within the GPIO consumer
intindex
index of the GPIO to obtain in the consumer
enumgpiod_flagsflags
GPIO initialization flags
constchar*label
label to attach to the requested GPIO

Description

GPIO descriptors returned from this function are automatically disposed ondriver detach.

On successful request the GPIO pin is configured in accordance withprovidedflags.

struct gpio_desc *devm_gpiod_get_index_optional(structdevice * dev, const char * con_id, unsigned int index, enum gpiod_flags flags)

Resource-managedgpiod_get_index_optional()

Parameters

structdevice*dev
GPIO consumer
constchar*con_id
function within the GPIO consumer
unsignedintindex
index of the GPIO to obtain in the consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

Managedgpiod_get_index_optional(). GPIO descriptors returned from thisfunction are automatically disposed on driver detach. Seegpiod_get_index_optional() for detailed information about behavior andreturn values.

struct gpio_descs *devm_gpiod_get_array(structdevice * dev, const char * con_id, enum gpiod_flags flags)

Resource-managedgpiod_get_array()

Parameters

structdevice*dev
GPIO consumer
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

Managedgpiod_get_array(). GPIO descriptors returned from this function areautomatically disposed on driver detach. Seegpiod_get_array() for detailedinformation about behavior and return values.

struct gpio_descs *devm_gpiod_get_array_optional(structdevice * dev, const char * con_id, enum gpiod_flags flags)

Resource-managedgpiod_get_array_optional()

Parameters

structdevice*dev
GPIO consumer
constchar*con_id
function within the GPIO consumer
enumgpiod_flagsflags
optional GPIO initialization flags

Description

Managedgpiod_get_array_optional(). GPIO descriptors returned from thisfunction are automatically disposed on driver detach.Seegpiod_get_array_optional() for detailed information about behavior andreturn values.

voiddevm_gpiod_put(structdevice * dev, struct gpio_desc * desc)

Resource-managedgpiod_put()

Parameters

structdevice*dev
GPIO consumer
structgpio_desc*desc
GPIO descriptor to dispose of

Description

Dispose of a GPIO descriptor obtained withdevm_gpiod_get() ordevm_gpiod_get_index(). Normally this function will not be called as the GPIOwill be disposed of by the resource management code.

voiddevm_gpiod_unhinge(structdevice * dev, struct gpio_desc * desc)

Remove resource management from a gpio descriptor

Parameters

structdevice*dev
GPIO consumer
structgpio_desc*desc
GPIO descriptor to remove resource management from

Description

Remove resource management from a GPIO descriptor. This is needed whenyou want to hand over lifecycle management of a descriptor to anothermechanism.

voiddevm_gpiod_put_array(structdevice * dev, struct gpio_descs * descs)

Resource-managedgpiod_put_array()

Parameters

structdevice*dev
GPIO consumer
structgpio_descs*descs
GPIO descriptor array to dispose of

Description

Dispose of an array of GPIO descriptors obtained withdevm_gpiod_get_array().Normally this function will not be called as the GPIOs will be disposed ofby the resource management code.

intdevm_gpio_request(structdevice * dev, unsigned gpio, const char * label)

request a GPIO for a managed device

Parameters

structdevice*dev
device to request the GPIO for
unsignedgpio
GPIO to allocate
constchar*label

the name of the requested GPIO

Except for the extradev argument, this function takes thesame arguments and performs the same function asgpio_request(). GPIOs requested with this function will beautomatically freed on driver detach.

If an GPIO allocated with this function needs to be freedseparately,devm_gpio_free() must be used.

intdevm_gpio_request_one(structdevice * dev, unsigned gpio, unsigned long flags, const char * label)

request a single GPIO with initial setup

Parameters

structdevice*dev
device to request for
unsignedgpio
the GPIO number
unsignedlongflags
GPIO configuration as specified by GPIOF_*
constchar*label
a literal description string of this GPIO
voiddevm_gpio_free(structdevice * dev, unsigned int gpio)

free a GPIO

Parameters

structdevice*dev
device to free GPIO for
unsignedintgpio

GPIO to free

Except for the extradev argument, this function takes thesame arguments and performs the same function as gpio_free().This function instead of gpio_free() should be used to manuallyfree GPIOs allocated withdevm_gpio_request().

intdevm_gpiochip_add_data_with_key(structdevice * dev, structgpio_chip * gc, void * data, struct lock_class_key * lock_key, struct lock_class_key * request_key)

Resource managed gpiochip_add_data_with_key()

Parameters

structdevice*dev
pointer to the device that gpio_chip belongs to.
structgpio_chip*gc
the GPIO chip to register
void*data
driver-private data associated with this chip
structlock_class_key*lock_key
lockdep class for IRQ lock
structlock_class_key*request_key
lockdep class for IRQ request

Context

potentially before irqs will work

Description

The gpio chip automatically be released when the device is unbound.

Return

A negative errno if the chip can’t be registered, such as because thegc->base is invalid or already associated with a different chip.Otherwise it returns zero as a success code.

sysfs helpers

intgpiod_export(struct gpio_desc * desc, bool direction_may_change)

export a GPIO through sysfs

Parameters

structgpio_desc*desc
GPIO to make available, already requested
booldirection_may_change
true if userspace may change GPIO direction

Context

arch_initcall or later

Description

When drivers want to make a GPIO accessible to userspace after theyhave requested it – perhaps while debugging, or as part of theirpublic interface – they may use this routine. If the GPIO canchange direction (some can’t) and the caller allows it, userspacewill see “direction” sysfs attribute which may be used to changethe gpio’s direction. A “value” attribute will always be provided.

Returns zero on success, else an error.

intgpiod_export_link(structdevice * dev, const char * name, struct gpio_desc * desc)

create a sysfs link to an exported GPIO node

Parameters

structdevice*dev
device under which to create symlink
constchar*name
name of the symlink
structgpio_desc*desc
GPIO to create symlink to, already exported

Description

Set up a symlink from /sys/…/dev/name to /sys/class/gpio/gpioNnode. Caller is responsible for unlinking.

Returns zero on success, else an error.

voidgpiod_unexport(struct gpio_desc * desc)

reverse effect ofgpiod_export()

Parameters

structgpio_desc*desc
GPIO to make unavailable

Description

This is implicit on gpiod_free().