xref: /linux-6.15/include/linux/gpio/driver.h (revision 9b443b68)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
279a9becdSAlexandre Courbot #ifndef __LINUX_GPIO_DRIVER_H
379a9becdSAlexandre Courbot #define __LINUX_GPIO_DRIVER_H
479a9becdSAlexandre Courbot 
5380c7ba3SAndy Shevchenko #include <linux/bits.h>
69e4555d1SBartosz Golaszewski #include <linux/cleanup.h>
79e4555d1SBartosz Golaszewski #include <linux/err.h>
814250520SLinus Walleij #include <linux/irqchip/chained_irq.h>
914250520SLinus Walleij #include <linux/irqdomain.h>
10380c7ba3SAndy Shevchenko #include <linux/irqhandler.h>
11a0a8bcf4SGrygorii Strashko #include <linux/lockdep.h>
122956b5d9SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
1308a149c4SAndy Shevchenko #include <linux/pinctrl/pinctrl.h>
1485ebb1a6SAndy Shevchenko #include <linux/property.h>
15380c7ba3SAndy Shevchenko #include <linux/spinlock_types.h>
1685ebb1a6SAndy Shevchenko #include <linux/types.h>
1723318614SAndy Shevchenko #include <linux/util_macros.h>
1879a9becdSAlexandre Courbot 
19380c7ba3SAndy Shevchenko #ifdef CONFIG_GENERIC_MSI_IRQ
2091a29af4SMarc Zyngier #include <asm/msi.h>
21380c7ba3SAndy Shevchenko #endif
2291a29af4SMarc Zyngier 
23380c7ba3SAndy Shevchenko struct device;
24380c7ba3SAndy Shevchenko struct irq_chip;
25380c7ba3SAndy Shevchenko struct irq_data;
26d47529b2SPaul Gortmaker struct module;
27380c7ba3SAndy Shevchenko struct of_phandle_args;
28380c7ba3SAndy Shevchenko struct pinctrl_dev;
29380c7ba3SAndy Shevchenko struct seq_file;
3079a9becdSAlexandre Courbot 
31fdd61a01SLinus Walleij struct gpio_chip;
32380c7ba3SAndy Shevchenko struct gpio_desc;
33380c7ba3SAndy Shevchenko struct gpio_device;
34380c7ba3SAndy Shevchenko 
35380c7ba3SAndy Shevchenko enum gpio_lookup_flags;
36380c7ba3SAndy Shevchenko enum gpiod_flags;
37fdd61a01SLinus Walleij 
3891a29af4SMarc Zyngier union gpio_irq_fwspec {
3991a29af4SMarc Zyngier 	struct irq_fwspec	fwspec;
4013e7accbSThomas Gleixner #ifdef CONFIG_GENERIC_MSI_IRQ
4191a29af4SMarc Zyngier 	msi_alloc_info_t	msiinfo;
4291a29af4SMarc Zyngier #endif
4391a29af4SMarc Zyngier };
4491a29af4SMarc Zyngier 
459208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_IN	1
469208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_OUT	0
479208b1e7SMatti Vaittinen 
48c44eafd7SThierry Reding /**
49c44eafd7SThierry Reding  * struct gpio_irq_chip - GPIO interrupt controller
50c44eafd7SThierry Reding  */
51c44eafd7SThierry Reding struct gpio_irq_chip {
52c44eafd7SThierry Reding 	/**
53da80ff81SThierry Reding 	 * @chip:
54da80ff81SThierry Reding 	 *
55da80ff81SThierry Reding 	 * GPIO IRQ chip implementation, provided by GPIO driver.
56da80ff81SThierry Reding 	 */
57da80ff81SThierry Reding 	struct irq_chip *chip;
58da80ff81SThierry Reding 
59da80ff81SThierry Reding 	/**
60f0fbe7bcSThierry Reding 	 * @domain:
61f0fbe7bcSThierry Reding 	 *
62f0fbe7bcSThierry Reding 	 * Interrupt translation domain; responsible for mapping between GPIO
63f0fbe7bcSThierry Reding 	 * hwirq number and Linux IRQ number.
64f0fbe7bcSThierry Reding 	 */
65f0fbe7bcSThierry Reding 	struct irq_domain *domain;
66f0fbe7bcSThierry Reding 
67fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
68fdd61a01SLinus Walleij 	/**
69fdd61a01SLinus Walleij 	 * @fwnode:
70fdd61a01SLinus Walleij 	 *
71fdd61a01SLinus Walleij 	 * Firmware node corresponding to this gpiochip/irqchip, necessary
72fdd61a01SLinus Walleij 	 * for hierarchical irqdomain support.
73fdd61a01SLinus Walleij 	 */
74fdd61a01SLinus Walleij 	struct fwnode_handle *fwnode;
75fdd61a01SLinus Walleij 
76fdd61a01SLinus Walleij 	/**
77fdd61a01SLinus Walleij 	 * @parent_domain:
78fdd61a01SLinus Walleij 	 *
79fdd61a01SLinus Walleij 	 * If non-NULL, will be set as the parent of this GPIO interrupt
80fdd61a01SLinus Walleij 	 * controller's IRQ domain to establish a hierarchical interrupt
81fdd61a01SLinus Walleij 	 * domain. The presence of this will activate the hierarchical
82fdd61a01SLinus Walleij 	 * interrupt support.
83fdd61a01SLinus Walleij 	 */
84fdd61a01SLinus Walleij 	struct irq_domain *parent_domain;
85fdd61a01SLinus Walleij 
86fdd61a01SLinus Walleij 	/**
87fdd61a01SLinus Walleij 	 * @child_to_parent_hwirq:
88fdd61a01SLinus Walleij 	 *
89fdd61a01SLinus Walleij 	 * This callback translates a child hardware IRQ offset to a parent
90fdd61a01SLinus Walleij 	 * hardware IRQ offset on a hierarchical interrupt chip. The child
91fdd61a01SLinus Walleij 	 * hardware IRQs correspond to the GPIO index 0..ngpio-1 (see the
92fdd61a01SLinus Walleij 	 * ngpio field of struct gpio_chip) and the corresponding parent
93fdd61a01SLinus Walleij 	 * hardware IRQ and type (such as IRQ_TYPE_*) shall be returned by
94fdd61a01SLinus Walleij 	 * the driver. The driver can calculate this from an offset or using
95fdd61a01SLinus Walleij 	 * a lookup table or whatever method is best for this chip. Return
96fdd61a01SLinus Walleij 	 * 0 on successful translation in the driver.
97fdd61a01SLinus Walleij 	 *
98fdd61a01SLinus Walleij 	 * If some ranges of hardware IRQs do not have a corresponding parent
99fdd61a01SLinus Walleij 	 * HWIRQ, return -EINVAL, but also make sure to fill in @valid_mask and
100fdd61a01SLinus Walleij 	 * @need_valid_mask to make these GPIO lines unavailable for
101fdd61a01SLinus Walleij 	 * translation.
102fdd61a01SLinus Walleij 	 */
103a0b66a73SLinus Walleij 	int (*child_to_parent_hwirq)(struct gpio_chip *gc,
104fdd61a01SLinus Walleij 				     unsigned int child_hwirq,
105fdd61a01SLinus Walleij 				     unsigned int child_type,
106fdd61a01SLinus Walleij 				     unsigned int *parent_hwirq,
107fdd61a01SLinus Walleij 				     unsigned int *parent_type);
108fdd61a01SLinus Walleij 
109fdd61a01SLinus Walleij 	/**
11024258761SKevin Hao 	 * @populate_parent_alloc_arg :
111fdd61a01SLinus Walleij 	 *
11224258761SKevin Hao 	 * This optional callback allocates and populates the specific struct
11324258761SKevin Hao 	 * for the parent's IRQ domain. If this is not specified, then
114fdd61a01SLinus Walleij 	 * &gpiochip_populate_parent_fwspec_twocell will be used. A four-cell
115fdd61a01SLinus Walleij 	 * variant named &gpiochip_populate_parent_fwspec_fourcell is also
116fdd61a01SLinus Walleij 	 * available.
117fdd61a01SLinus Walleij 	 */
11891a29af4SMarc Zyngier 	int (*populate_parent_alloc_arg)(struct gpio_chip *gc,
11991a29af4SMarc Zyngier 					 union gpio_irq_fwspec *fwspec,
120fdd61a01SLinus Walleij 					 unsigned int parent_hwirq,
121fdd61a01SLinus Walleij 					 unsigned int parent_type);
122fdd61a01SLinus Walleij 
123fdd61a01SLinus Walleij 	/**
124fdd61a01SLinus Walleij 	 * @child_offset_to_irq:
125fdd61a01SLinus Walleij 	 *
126fdd61a01SLinus Walleij 	 * This optional callback is used to translate the child's GPIO line
127fdd61a01SLinus Walleij 	 * offset on the GPIO chip to an IRQ number for the GPIO to_irq()
128fdd61a01SLinus Walleij 	 * callback. If this is not specified, then a default callback will be
129fdd61a01SLinus Walleij 	 * provided that returns the line offset.
130fdd61a01SLinus Walleij 	 */
131a0b66a73SLinus Walleij 	unsigned int (*child_offset_to_irq)(struct gpio_chip *gc,
132fdd61a01SLinus Walleij 					    unsigned int pin);
133fdd61a01SLinus Walleij 
134fdd61a01SLinus Walleij 	/**
135fdd61a01SLinus Walleij 	 * @child_irq_domain_ops:
136fdd61a01SLinus Walleij 	 *
137fdd61a01SLinus Walleij 	 * The IRQ domain operations that will be used for this GPIO IRQ
138fdd61a01SLinus Walleij 	 * chip. If no operations are provided, then default callbacks will
139fdd61a01SLinus Walleij 	 * be populated to setup the IRQ hierarchy. Some drivers need to
140fdd61a01SLinus Walleij 	 * supply their own translate function.
141fdd61a01SLinus Walleij 	 */
142fdd61a01SLinus Walleij 	struct irq_domain_ops child_irq_domain_ops;
143fdd61a01SLinus Walleij #endif
144fdd61a01SLinus Walleij 
145c44eafd7SThierry Reding 	/**
146c7a0aa59SThierry Reding 	 * @handler:
147c7a0aa59SThierry Reding 	 *
148c7a0aa59SThierry Reding 	 * The IRQ handler to use (often a predefined IRQ core function) for
149c7a0aa59SThierry Reding 	 * GPIO IRQs, provided by GPIO driver.
150c7a0aa59SThierry Reding 	 */
151c7a0aa59SThierry Reding 	irq_flow_handler_t handler;
152c7a0aa59SThierry Reding 
153c7a0aa59SThierry Reding 	/**
1543634eeb0SThierry Reding 	 * @default_type:
1553634eeb0SThierry Reding 	 *
1563634eeb0SThierry Reding 	 * Default IRQ triggering type applied during GPIO driver
1573634eeb0SThierry Reding 	 * initialization, provided by GPIO driver.
1583634eeb0SThierry Reding 	 */
1593634eeb0SThierry Reding 	unsigned int default_type;
1603634eeb0SThierry Reding 
1613634eeb0SThierry Reding 	/**
162ca9df053SThierry Reding 	 * @lock_key:
163ca9df053SThierry Reding 	 *
16402ad0437SRandy Dunlap 	 * Per GPIO IRQ chip lockdep class for IRQ lock.
165ca9df053SThierry Reding 	 */
166ca9df053SThierry Reding 	struct lock_class_key *lock_key;
16702ad0437SRandy Dunlap 
16802ad0437SRandy Dunlap 	/**
16902ad0437SRandy Dunlap 	 * @request_key:
17002ad0437SRandy Dunlap 	 *
17102ad0437SRandy Dunlap 	 * Per GPIO IRQ chip lockdep class for IRQ request.
17202ad0437SRandy Dunlap 	 */
17339c3fd58SAndrew Lunn 	struct lock_class_key *request_key;
174ca9df053SThierry Reding 
175ca9df053SThierry Reding 	/**
176c44eafd7SThierry Reding 	 * @parent_handler:
177c44eafd7SThierry Reding 	 *
178c44eafd7SThierry Reding 	 * The interrupt handler for the GPIO chip's parent interrupts, may be
179c44eafd7SThierry Reding 	 * NULL if the parent interrupts are nested rather than cascaded.
180c44eafd7SThierry Reding 	 */
181c44eafd7SThierry Reding 	irq_flow_handler_t parent_handler;
182c44eafd7SThierry Reding 
183c7e1c443SAkira Yokosawa 	union {
184c44eafd7SThierry Reding 		/**
185c44eafd7SThierry Reding 		 * @parent_handler_data:
18648ec13d3SJoey Gouly 		 *
187c7e1c443SAkira Yokosawa 		 * If @per_parent_data is false, @parent_handler_data is a
188c7e1c443SAkira Yokosawa 		 * single pointer used as the data associated with every
189c7e1c443SAkira Yokosawa 		 * parent interrupt.
190c7e1c443SAkira Yokosawa 		 */
191c7e1c443SAkira Yokosawa 		void *parent_handler_data;
192c7e1c443SAkira Yokosawa 
193c7e1c443SAkira Yokosawa 		/**
194cfe6807dSMarc Zyngier 		 * @parent_handler_data_array:
195c44eafd7SThierry Reding 		 *
19648ec13d3SJoey Gouly 		 * If @per_parent_data is true, @parent_handler_data_array is
19748ec13d3SJoey Gouly 		 * an array of @num_parents pointers, and is used to associate
19848ec13d3SJoey Gouly 		 * different data for each parent. This cannot be NULL if
19948ec13d3SJoey Gouly 		 * @per_parent_data is true.
200c44eafd7SThierry Reding 		 */
201cfe6807dSMarc Zyngier 		void **parent_handler_data_array;
202cfe6807dSMarc Zyngier 	};
20339e5f096SThierry Reding 
20439e5f096SThierry Reding 	/**
20539e5f096SThierry Reding 	 * @num_parents:
20639e5f096SThierry Reding 	 *
20739e5f096SThierry Reding 	 * The number of interrupt parents of a GPIO chip.
20839e5f096SThierry Reding 	 */
20939e5f096SThierry Reding 	unsigned int num_parents;
21039e5f096SThierry Reding 
21139e5f096SThierry Reding 	/**
21239e5f096SThierry Reding 	 * @parents:
21339e5f096SThierry Reding 	 *
21439e5f096SThierry Reding 	 * A list of interrupt parents of a GPIO chip. This is owned by the
21539e5f096SThierry Reding 	 * driver, so the core will only reference this list, not modify it.
21639e5f096SThierry Reding 	 */
21739e5f096SThierry Reding 	unsigned int *parents;
218dc6bafeeSThierry Reding 
219dc6bafeeSThierry Reding 	/**
220e0d89728SThierry Reding 	 * @map:
221e0d89728SThierry Reding 	 *
222e0d89728SThierry Reding 	 * A list of interrupt parents for each line of a GPIO chip.
223e0d89728SThierry Reding 	 */
224e0d89728SThierry Reding 	unsigned int *map;
225e0d89728SThierry Reding 
226e0d89728SThierry Reding 	/**
22760ed54caSThierry Reding 	 * @threaded:
228dc6bafeeSThierry Reding 	 *
22960ed54caSThierry Reding 	 * True if set the interrupt handling uses nested threads.
230dc6bafeeSThierry Reding 	 */
23160ed54caSThierry Reding 	bool threaded;
232dc7b0387SThierry Reding 
233dc7b0387SThierry Reding 	/**
234cfe6807dSMarc Zyngier 	 * @per_parent_data:
235cfe6807dSMarc Zyngier 	 *
236cfe6807dSMarc Zyngier 	 * True if parent_handler_data_array describes a @num_parents
237cfe6807dSMarc Zyngier 	 * sized array to be used as parent data.
238cfe6807dSMarc Zyngier 	 */
239cfe6807dSMarc Zyngier 	bool per_parent_data;
240cfe6807dSMarc Zyngier 
241cfe6807dSMarc Zyngier 	/**
2425467801fSShreeya Patel 	 * @initialized:
2435467801fSShreeya Patel 	 *
2445467801fSShreeya Patel 	 * Flag to track GPIO chip irq member's initialization.
2455467801fSShreeya Patel 	 * This flag will make sure GPIO chip irq members are not used
2465467801fSShreeya Patel 	 * before they are initialized.
2475467801fSShreeya Patel 	 */
2485467801fSShreeya Patel 	bool initialized;
2495467801fSShreeya Patel 
2505467801fSShreeya Patel 	/**
251ff7a1790SMichael Walle 	 * @domain_is_allocated_externally:
252ff7a1790SMichael Walle 	 *
253ff7a1790SMichael Walle 	 * True it the irq_domain was allocated outside of gpiolib, in which
254ff7a1790SMichael Walle 	 * case gpiolib won't free the irq_domain itself.
255ff7a1790SMichael Walle 	 */
256ff7a1790SMichael Walle 	bool domain_is_allocated_externally;
257ff7a1790SMichael Walle 
258ff7a1790SMichael Walle 	/**
2599411e3aaSAndy Shevchenko 	 * @init_hw: optional routine to initialize hardware before
2609411e3aaSAndy Shevchenko 	 * an IRQ chip will be added. This is quite useful when
2619411e3aaSAndy Shevchenko 	 * a particular driver wants to clear IRQ related registers
2629411e3aaSAndy Shevchenko 	 * in order to avoid undesired events.
2639411e3aaSAndy Shevchenko 	 */
264a0b66a73SLinus Walleij 	int (*init_hw)(struct gpio_chip *gc);
2659411e3aaSAndy Shevchenko 
2669411e3aaSAndy Shevchenko 	/**
2675fbe5b58SLinus Walleij 	 * @init_valid_mask: optional routine to initialize @valid_mask, to be
2685fbe5b58SLinus Walleij 	 * used if not all GPIO lines are valid interrupts. Sometimes some
2695fbe5b58SLinus Walleij 	 * lines just cannot fire interrupts, and this routine, when defined,
2705fbe5b58SLinus Walleij 	 * is passed a bitmap in "valid_mask" and it will have ngpios
2715fbe5b58SLinus Walleij 	 * bits from 0..(ngpios-1) set to "1" as in valid. The callback can
2725fbe5b58SLinus Walleij 	 * then directly set some bits to "0" if they cannot be used for
2735fbe5b58SLinus Walleij 	 * interrupts.
274dc7b0387SThierry Reding 	 */
275a0b66a73SLinus Walleij 	void (*init_valid_mask)(struct gpio_chip *gc,
2765fbe5b58SLinus Walleij 				unsigned long *valid_mask,
2775fbe5b58SLinus Walleij 				unsigned int ngpios);
278dc7b0387SThierry Reding 
279dc7b0387SThierry Reding 	/**
280dc7b0387SThierry Reding 	 * @valid_mask:
281dc7b0387SThierry Reding 	 *
2822d93018fSRandy Dunlap 	 * If not %NULL, holds bitmask of GPIOs which are valid to be included
283dc7b0387SThierry Reding 	 * in IRQ domain of the chip.
284dc7b0387SThierry Reding 	 */
285dc7b0387SThierry Reding 	unsigned long *valid_mask;
2868302cf58SThierry Reding 
2878302cf58SThierry Reding 	/**
2888302cf58SThierry Reding 	 * @first:
2898302cf58SThierry Reding 	 *
2908302cf58SThierry Reding 	 * Required for static IRQ allocation. If set, irq_domain_add_simple()
2918302cf58SThierry Reding 	 * will allocate and map all IRQs during initialization.
2928302cf58SThierry Reding 	 */
2938302cf58SThierry Reding 	unsigned int first;
294461c1a7dSHans Verkuil 
295461c1a7dSHans Verkuil 	/**
296461c1a7dSHans Verkuil 	 * @irq_enable:
297461c1a7dSHans Verkuil 	 *
298461c1a7dSHans Verkuil 	 * Store old irq_chip irq_enable callback
299461c1a7dSHans Verkuil 	 */
300461c1a7dSHans Verkuil 	void		(*irq_enable)(struct irq_data *data);
301461c1a7dSHans Verkuil 
302461c1a7dSHans Verkuil 	/**
303461c1a7dSHans Verkuil 	 * @irq_disable:
304461c1a7dSHans Verkuil 	 *
305461c1a7dSHans Verkuil 	 * Store old irq_chip irq_disable callback
306461c1a7dSHans Verkuil 	 */
307461c1a7dSHans Verkuil 	void		(*irq_disable)(struct irq_data *data);
308a8173820SMaulik Shah 	/**
309a8173820SMaulik Shah 	 * @irq_unmask:
310a8173820SMaulik Shah 	 *
311a8173820SMaulik Shah 	 * Store old irq_chip irq_unmask callback
312a8173820SMaulik Shah 	 */
313a8173820SMaulik Shah 	void		(*irq_unmask)(struct irq_data *data);
314a8173820SMaulik Shah 
315a8173820SMaulik Shah 	/**
316a8173820SMaulik Shah 	 * @irq_mask:
317a8173820SMaulik Shah 	 *
318a8173820SMaulik Shah 	 * Store old irq_chip irq_mask callback
319a8173820SMaulik Shah 	 */
320a8173820SMaulik Shah 	void		(*irq_mask)(struct irq_data *data);
321c44eafd7SThierry Reding };
322c44eafd7SThierry Reding 
32379a9becdSAlexandre Courbot /**
32479a9becdSAlexandre Courbot  * struct gpio_chip - abstract a GPIO controller
325df4878e9SLinus Walleij  * @label: a functional name for the GPIO device, such as a part
326df4878e9SLinus Walleij  *	number or the name of the SoC IP-block implementing it.
327ff2b1359SLinus Walleij  * @gpiodev: the internal state holder, opaque struct
32858383c78SLinus Walleij  * @parent: optional parent device providing the GPIOs
329990f6756SBartosz Golaszewski  * @fwnode: optional fwnode providing this controller's properties
33079a9becdSAlexandre Courbot  * @owner: helps prevent removal of modules exporting active GPIOs
33179a9becdSAlexandre Courbot  * @request: optional hook for chip-specific activation, such as
33269920338SBartosz Golaszewski  *	enabling module power and clock; may sleep; must return 0 on success
33369920338SBartosz Golaszewski  *	or negative error number on failure
33479a9becdSAlexandre Courbot  * @free: optional hook for chip-specific deactivation, such as
33579a9becdSAlexandre Courbot  *	disabling module power and clock; may sleep
33679a9becdSAlexandre Courbot  * @get_direction: returns direction for signal "offset", 0=out, 1=in,
33736b52154SDouglas Anderson  *	(same as GPIO_LINE_DIRECTION_OUT / GPIO_LINE_DIRECTION_IN),
33836b52154SDouglas Anderson  *	or negative error. It is recommended to always implement this
33936b52154SDouglas Anderson  *	function, even on input-only or output-only gpio chips.
3406933ba52SBartosz Golaszewski  * @direction_input: configures signal "offset" as input, returns 0 on success
3416933ba52SBartosz Golaszewski  *	or a negative error number. This can be omitted on input-only or
3426933ba52SBartosz Golaszewski  *	output-only gpio chips.
3436933ba52SBartosz Golaszewski  * @direction_output: configures signal "offset" as output, returns 0 on
3446933ba52SBartosz Golaszewski  *	success or a negative error number. This can be omitted on input-only
3456933ba52SBartosz Golaszewski  *	or output-only gpio chips.
34660befd2eSVladimir Zapolskiy  * @get: returns value for signal "offset", 0=low, 1=high, or negative error
347eec1d566SLukas Wunner  * @get_multiple: reads values for multiple signals defined by "mask" and
348eec1d566SLukas Wunner  *	stores them in "bits", returns 0 on success or negative error
3496224e7fcSBartosz Golaszewski  * @set: **DEPRECATED** - please use set_rv() instead
3506224e7fcSBartosz Golaszewski  * @set_multiple: **DEPRECATED** - please use set_multiple_rv() instead
35198ce1eb1SBartosz Golaszewski  * @set_rv: assigns output value for signal "offset", returns 0 on success or
35298ce1eb1SBartosz Golaszewski  *          negative error value
35398ce1eb1SBartosz Golaszewski  * @set_multiple_rv: assigns output values for multiple signals defined by
35498ce1eb1SBartosz Golaszewski  *                   "mask", returns 0 on success or negative error value
3552956b5d9SMika Westerberg  * @set_config: optional hook for all kinds of settings. Uses the same
356dcf8f3bfSBartosz Golaszewski  *	packed config format as generic pinconf. Must return 0 on success and
357dcf8f3bfSBartosz Golaszewski  *	a negative error number on failure.
3589a7dcaefSAndy Shevchenko  * @to_irq: optional hook supporting non-static gpiod_to_irq() mappings;
35979a9becdSAlexandre Courbot  *	implementation may not sleep
36079a9becdSAlexandre Courbot  * @dbg_show: optional routine to show contents in debugfs; default code
36179a9becdSAlexandre Courbot  *	will be used when this is omitted, but custom code can show extra
36279a9becdSAlexandre Courbot  *	state (such as pullup/pulldown configuration).
363f99d479bSGeert Uytterhoeven  * @init_valid_mask: optional routine to initialize @valid_mask, to be used if
364f99d479bSGeert Uytterhoeven  *	not all GPIOs are valid.
365b056ca1cSAndy Shevchenko  * @add_pin_ranges: optional routine to initialize pin ranges, to be used when
366b056ca1cSAndy Shevchenko  *	requires special mapping of the pins that provides GPIO functionality.
367b056ca1cSAndy Shevchenko  *	It is called after adding GPIO chip and before adding IRQ chip.
36842112dd7SDipen Patel  * @en_hw_timestamp: Dependent on GPIO chip, an optional routine to
36942112dd7SDipen Patel  *	enable hardware timestamp.
37042112dd7SDipen Patel  * @dis_hw_timestamp: Dependent on GPIO chip, an optional routine to
37142112dd7SDipen Patel  *	disable hardware timestamp.
372af6c235dSLinus Walleij  * @base: identifies the first GPIO number handled by this chip;
373af6c235dSLinus Walleij  *	or, if negative during registration, requests dynamic ID allocation.
374af6c235dSLinus Walleij  *	DEPRECATION: providing anything non-negative and nailing the base
37530bb6fb3SGeert Uytterhoeven  *	offset of GPIO chips is deprecated. Please pass -1 as base to
376af6c235dSLinus Walleij  *	let gpiolib select the chip base in all possible cases. We want to
377af6c235dSLinus Walleij  *	get rid of the static GPIO number space in the long run.
37879a9becdSAlexandre Courbot  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
37979a9becdSAlexandre Courbot  *	handled is (base + ngpio - 1).
3804e804c39SSergio Paracuellos  * @offset: when multiple gpio chips belong to the same device this
3814e804c39SSergio Paracuellos  *	can be used as offset within the device so friendly names can
3824e804c39SSergio Paracuellos  *	be properly assigned.
38379a9becdSAlexandre Courbot  * @names: if set, must be an array of strings to use as alternative
38479a9becdSAlexandre Courbot  *      names for the GPIOs in this chip. Any entry in the array
38579a9becdSAlexandre Courbot  *      may be NULL if there is no alias for the GPIO, however the
3862b5ae9c7SAndy Shevchenko  *      array must be @ngpio entries long.
3879fb1f39eSLinus Walleij  * @can_sleep: flag must be set iff get()/set() methods sleep, as they
3881c8732bbSLinus Walleij  *	must while accessing GPIO expander chips over I2C or SPI. This
3891c8732bbSLinus Walleij  *	implies that if the chip supports IRQs, these IRQs need to be threaded
3901c8732bbSLinus Walleij  *	as the chip access may sleep when e.g. reading out the IRQ status
3911c8732bbSLinus Walleij  *	registers.
3920f4630f3SLinus Walleij  * @read_reg: reader function for generic GPIO
3930f4630f3SLinus Walleij  * @write_reg: writer function for generic GPIO
39424efd94bSLinus Walleij  * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing
39524efd94bSLinus Walleij  *	line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the
39624efd94bSLinus Walleij  *	generic GPIO core. It is for internal housekeeping only.
3970f4630f3SLinus Walleij  * @reg_dat: data (in) register for generic GPIO
3980f4630f3SLinus Walleij  * @reg_set: output set register (out=high) for generic GPIO
39908bcd3edSAnthony Best  * @reg_clr: output clear register (out=low) for generic GPIO
400f69e00bdSLinus Walleij  * @reg_dir_out: direction out setting register for generic GPIO
401f69e00bdSLinus Walleij  * @reg_dir_in: direction in setting register for generic GPIO
402f69e00bdSLinus Walleij  * @bgpio_dir_unreadable: indicates that the direction register(s) cannot
403f69e00bdSLinus Walleij  *	be read and we need to rely on out internal state tracking.
4042145ba37SLinus Walleij  * @bgpio_pinctrl: the generic GPIO uses a pin control backend.
4050f4630f3SLinus Walleij  * @bgpio_bits: number of register bits used for a generic GPIO i.e.
4060f4630f3SLinus Walleij  *	<register width> * 8
4070f4630f3SLinus Walleij  * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep
4080f4630f3SLinus Walleij  *	shadowed and real data registers writes together.
4090f4630f3SLinus Walleij  * @bgpio_data:	shadowed data register for generic GPIO to clear/set bits
4100f4630f3SLinus Walleij  *	safely.
4110f4630f3SLinus Walleij  * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
412f69e00bdSLinus Walleij  *	direction safely. A "1" in this word means the line is set as
413f69e00bdSLinus Walleij  *	output.
41479a9becdSAlexandre Courbot  *
41579a9becdSAlexandre Courbot  * A gpio_chip can help platforms abstract various sources of GPIOs so
4162d93018fSRandy Dunlap  * they can all be accessed through a common programming interface.
41779a9becdSAlexandre Courbot  * Example sources would be SOC controllers, FPGAs, multifunction
41879a9becdSAlexandre Courbot  * chips, dedicated GPIO expanders, and so on.
41979a9becdSAlexandre Courbot  *
42079a9becdSAlexandre Courbot  * Each chip controls a number of signals, identified in method calls
42179a9becdSAlexandre Courbot  * by "offset" values in the range 0..(@ngpio - 1).  When those signals
42279a9becdSAlexandre Courbot  * are referenced through calls like gpio_get_value(gpio), the offset
42379a9becdSAlexandre Courbot  * is calculated by subtracting @base from the gpio number.
42479a9becdSAlexandre Courbot  */
42579a9becdSAlexandre Courbot struct gpio_chip {
42679a9becdSAlexandre Courbot 	const char		*label;
427ff2b1359SLinus Walleij 	struct gpio_device	*gpiodev;
42858383c78SLinus Walleij 	struct device		*parent;
429990f6756SBartosz Golaszewski 	struct fwnode_handle	*fwnode;
43079a9becdSAlexandre Courbot 	struct module		*owner;
43179a9becdSAlexandre Courbot 
432a0b66a73SLinus Walleij 	int			(*request)(struct gpio_chip *gc,
4338d091012SDouglas Anderson 						unsigned int offset);
434a0b66a73SLinus Walleij 	void			(*free)(struct gpio_chip *gc,
4358d091012SDouglas Anderson 						unsigned int offset);
436a0b66a73SLinus Walleij 	int			(*get_direction)(struct gpio_chip *gc,
4378d091012SDouglas Anderson 						unsigned int offset);
438a0b66a73SLinus Walleij 	int			(*direction_input)(struct gpio_chip *gc,
4398d091012SDouglas Anderson 						unsigned int offset);
440a0b66a73SLinus Walleij 	int			(*direction_output)(struct gpio_chip *gc,
4418d091012SDouglas Anderson 						unsigned int offset, int value);
442a0b66a73SLinus Walleij 	int			(*get)(struct gpio_chip *gc,
4438d091012SDouglas Anderson 						unsigned int offset);
444a0b66a73SLinus Walleij 	int			(*get_multiple)(struct gpio_chip *gc,
445eec1d566SLukas Wunner 						unsigned long *mask,
446eec1d566SLukas Wunner 						unsigned long *bits);
447a0b66a73SLinus Walleij 	void			(*set)(struct gpio_chip *gc,
4488d091012SDouglas Anderson 						unsigned int offset, int value);
449a0b66a73SLinus Walleij 	void			(*set_multiple)(struct gpio_chip *gc,
4505f424243SRojhalat Ibrahim 						unsigned long *mask,
4515f424243SRojhalat Ibrahim 						unsigned long *bits);
45298ce1eb1SBartosz Golaszewski 	int			(*set_rv)(struct gpio_chip *gc,
45398ce1eb1SBartosz Golaszewski 					  unsigned int offset,
45498ce1eb1SBartosz Golaszewski 					  int value);
45598ce1eb1SBartosz Golaszewski 	int			(*set_multiple_rv)(struct gpio_chip *gc,
45698ce1eb1SBartosz Golaszewski 						   unsigned long *mask,
45798ce1eb1SBartosz Golaszewski 						   unsigned long *bits);
458a0b66a73SLinus Walleij 	int			(*set_config)(struct gpio_chip *gc,
4598d091012SDouglas Anderson 					      unsigned int offset,
4602956b5d9SMika Westerberg 					      unsigned long config);
461a0b66a73SLinus Walleij 	int			(*to_irq)(struct gpio_chip *gc,
4628d091012SDouglas Anderson 						unsigned int offset);
46379a9becdSAlexandre Courbot 
46479a9becdSAlexandre Courbot 	void			(*dbg_show)(struct seq_file *s,
465a0b66a73SLinus Walleij 						struct gpio_chip *gc);
466f8ec92a9SRicardo Ribalda Delgado 
467a0b66a73SLinus Walleij 	int			(*init_valid_mask)(struct gpio_chip *gc,
468c9fc5affSLinus Walleij 						   unsigned long *valid_mask,
469c9fc5affSLinus Walleij 						   unsigned int ngpios);
470f8ec92a9SRicardo Ribalda Delgado 
471a0b66a73SLinus Walleij 	int			(*add_pin_ranges)(struct gpio_chip *gc);
472b056ca1cSAndy Shevchenko 
47342112dd7SDipen Patel 	int			(*en_hw_timestamp)(struct gpio_chip *gc,
47442112dd7SDipen Patel 						   u32 offset,
47542112dd7SDipen Patel 						   unsigned long flags);
47642112dd7SDipen Patel 	int			(*dis_hw_timestamp)(struct gpio_chip *gc,
47742112dd7SDipen Patel 						    u32 offset,
47842112dd7SDipen Patel 						    unsigned long flags);
47979a9becdSAlexandre Courbot 	int			base;
48079a9becdSAlexandre Courbot 	u16			ngpio;
4814e804c39SSergio Paracuellos 	u16			offset;
48279a9becdSAlexandre Courbot 	const char		*const *names;
4839fb1f39eSLinus Walleij 	bool			can_sleep;
48479a9becdSAlexandre Courbot 
4850f4630f3SLinus Walleij #if IS_ENABLED(CONFIG_GPIO_GENERIC)
4860f4630f3SLinus Walleij 	unsigned long (*read_reg)(void __iomem *reg);
4870f4630f3SLinus Walleij 	void (*write_reg)(void __iomem *reg, unsigned long data);
48824efd94bSLinus Walleij 	bool be_bits;
4890f4630f3SLinus Walleij 	void __iomem *reg_dat;
4900f4630f3SLinus Walleij 	void __iomem *reg_set;
4910f4630f3SLinus Walleij 	void __iomem *reg_clr;
492f69e00bdSLinus Walleij 	void __iomem *reg_dir_out;
493f69e00bdSLinus Walleij 	void __iomem *reg_dir_in;
494f69e00bdSLinus Walleij 	bool bgpio_dir_unreadable;
4952145ba37SLinus Walleij 	bool bgpio_pinctrl;
4960f4630f3SLinus Walleij 	int bgpio_bits;
4973c938cc5SSchspa Shi 	raw_spinlock_t bgpio_lock;
4980f4630f3SLinus Walleij 	unsigned long bgpio_data;
4990f4630f3SLinus Walleij 	unsigned long bgpio_dir;
500f310f2efSEnrico Weigelt #endif /* CONFIG_GPIO_GENERIC */
5010f4630f3SLinus Walleij 
50214250520SLinus Walleij #ifdef CONFIG_GPIOLIB_IRQCHIP
50314250520SLinus Walleij 	/*
5047d75a871SPaul Bolle 	 * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
50514250520SLinus Walleij 	 * to handle IRQs for most practical cases.
50614250520SLinus Walleij 	 */
507c44eafd7SThierry Reding 
508c44eafd7SThierry Reding 	/**
509c44eafd7SThierry Reding 	 * @irq:
510c44eafd7SThierry Reding 	 *
511c44eafd7SThierry Reding 	 * Integrates interrupt chip functionality with the GPIO chip. Can be
512c44eafd7SThierry Reding 	 * used to handle IRQs for most practical cases.
513c44eafd7SThierry Reding 	 */
514c44eafd7SThierry Reding 	struct gpio_irq_chip irq;
515f310f2efSEnrico Weigelt #endif /* CONFIG_GPIOLIB_IRQCHIP */
51614250520SLinus Walleij 
51779a9becdSAlexandre Courbot #if defined(CONFIG_OF_GPIO)
51879a9becdSAlexandre Courbot 	/*
5192d93018fSRandy Dunlap 	 * If CONFIG_OF_GPIO is enabled, then all GPIO controllers described in
5202d93018fSRandy Dunlap 	 * the device tree automatically may have an OF translation
52179a9becdSAlexandre Courbot 	 */
52267049c50SThierry Reding 
52367049c50SThierry Reding 	/**
52467049c50SThierry Reding 	 * @of_gpio_n_cells:
52567049c50SThierry Reding 	 *
526bd3ce710SLinus Walleij 	 * Number of cells used to form the GPIO specifier. The standard is 2
527bd3ce710SLinus Walleij 	 * cells:
528bd3ce710SLinus Walleij 	 *
529bd3ce710SLinus Walleij 	 * gpios = <&gpio offset flags>;
530bd3ce710SLinus Walleij 	 *
531bd3ce710SLinus Walleij 	 * some complex GPIO controllers instantiate more than one chip per
532bd3ce710SLinus Walleij 	 * device tree node and have 3 cells:
533bd3ce710SLinus Walleij 	 *
534bd3ce710SLinus Walleij 	 * gpios = <&gpio instance offset flags>;
535bd3ce710SLinus Walleij 	 *
536bd3ce710SLinus Walleij 	 * Legacy GPIO controllers may even have 1 cell:
537bd3ce710SLinus Walleij 	 *
538bd3ce710SLinus Walleij 	 * gpios = <&gpio offset>;
53967049c50SThierry Reding 	 */
540e3b445d7SThierry Reding 	unsigned int of_gpio_n_cells;
54167049c50SThierry Reding 
54267049c50SThierry Reding 	/**
543*9b443b68SBartosz Golaszewski 	 * @of_node_instance_match:
544bd3ce710SLinus Walleij 	 *
545bd3ce710SLinus Walleij 	 * Determine if a chip is the right instance. Must be implemented by
546bd3ce710SLinus Walleij 	 * any driver using more than one gpio_chip per device tree node.
547bd3ce710SLinus Walleij 	 * Returns true if gc is the instance indicated by i (which is the
548bd3ce710SLinus Walleij 	 * first cell in the phandles for GPIO lines and gpio-ranges).
549bd3ce710SLinus Walleij 	 */
550bd3ce710SLinus Walleij 	bool (*of_node_instance_match)(struct gpio_chip *gc, unsigned int i);
551bd3ce710SLinus Walleij 
552bd3ce710SLinus Walleij 	/**
55367049c50SThierry Reding 	 * @of_xlate:
55467049c50SThierry Reding 	 *
55567049c50SThierry Reding 	 * Callback to translate a device tree GPIO specifier into a chip-
55667049c50SThierry Reding 	 * relative GPIO number and flags.
55767049c50SThierry Reding 	 */
55879a9becdSAlexandre Courbot 	int (*of_xlate)(struct gpio_chip *gc,
55979a9becdSAlexandre Courbot 			const struct of_phandle_args *gpiospec, u32 *flags);
560f310f2efSEnrico Weigelt #endif /* CONFIG_OF_GPIO */
56179a9becdSAlexandre Courbot };
56279a9becdSAlexandre Courbot 
563ee25fba7SBartosz Golaszewski char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset);
56479a9becdSAlexandre Courbot 
5656fd9c993SBartosz Golaszewski 
5666fd9c993SBartosz Golaszewski struct _gpiochip_for_each_data {
5676fd9c993SBartosz Golaszewski 	const char **label;
5686fd9c993SBartosz Golaszewski 	unsigned int *i;
5696fd9c993SBartosz Golaszewski };
5706fd9c993SBartosz Golaszewski 
5716fd9c993SBartosz Golaszewski DEFINE_CLASS(_gpiochip_for_each_data,
5726fd9c993SBartosz Golaszewski 	     struct _gpiochip_for_each_data,
5736fd9c993SBartosz Golaszewski 	     if (*_T.label) kfree(*_T.label),
5746fd9c993SBartosz Golaszewski 	     ({
5756fd9c993SBartosz Golaszewski 		struct _gpiochip_for_each_data _data = { label, i };
5766fd9c993SBartosz Golaszewski 		*_data.i = 0;
5776fd9c993SBartosz Golaszewski 		_data;
5786fd9c993SBartosz Golaszewski 	     }),
5796fd9c993SBartosz Golaszewski 	     const char **label, int *i)
58079a9becdSAlexandre Courbot 
581b3337eb2SAndy Shevchenko /**
58288935160SAndy Shevchenko  * for_each_hwgpio_in_range - Iterates over all GPIOs in a given range
58388935160SAndy Shevchenko  * @_chip: Chip to iterate over.
58488935160SAndy Shevchenko  * @_i: Loop counter.
58588935160SAndy Shevchenko  * @_base: First GPIO in the ranger.
58688935160SAndy Shevchenko  * @_size: Amount of GPIOs to check starting from @base.
58788935160SAndy Shevchenko  * @_label: Place to store the address of the label if the GPIO is requested.
58888935160SAndy Shevchenko  *          Set to NULL for unused GPIOs.
58988935160SAndy Shevchenko  */
59088935160SAndy Shevchenko #define for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)			\
59188935160SAndy Shevchenko 	for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i);			\
592767412f0SAndy Shevchenko 	     _i < _size;								\
593767412f0SAndy Shevchenko 	     _i++, kfree(_label), _label = NULL)					\
59423318614SAndy Shevchenko 		for_each_if(!IS_ERR(_label = gpiochip_dup_line_label(_chip, _base + _i)))
59588935160SAndy Shevchenko 
59688935160SAndy Shevchenko /**
5973d8bb3d3SBartosz Golaszewski  * for_each_hwgpio - Iterates over all GPIOs for given chip.
5983d8bb3d3SBartosz Golaszewski  * @_chip: Chip to iterate over.
5993d8bb3d3SBartosz Golaszewski  * @_i: Loop counter.
6003d8bb3d3SBartosz Golaszewski  * @_label: Place to store the address of the label if the GPIO is requested.
6013d8bb3d3SBartosz Golaszewski  *          Set to NULL for unused GPIOs.
6023d8bb3d3SBartosz Golaszewski  */
6033d8bb3d3SBartosz Golaszewski #define for_each_hwgpio(_chip, _i, _label)						\
60488935160SAndy Shevchenko 	for_each_hwgpio_in_range(_chip, _i, 0, _chip->ngpio, _label)
6053d8bb3d3SBartosz Golaszewski 
6063d8bb3d3SBartosz Golaszewski /**
607b3337eb2SAndy Shevchenko  * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range
6086fd9c993SBartosz Golaszewski  * @_chip:	the chip to query
6096fd9c993SBartosz Golaszewski  * @_i:		loop variable
6106fd9c993SBartosz Golaszewski  * @_base:	first GPIO in the range
6116fd9c993SBartosz Golaszewski  * @_size:	amount of GPIOs to check starting from @base
6126fd9c993SBartosz Golaszewski  * @_label:	label of current GPIO
613b3337eb2SAndy Shevchenko  */
6146fd9c993SBartosz Golaszewski #define for_each_requested_gpio_in_range(_chip, _i, _base, _size, _label)		\
61588935160SAndy Shevchenko 	for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)			\
61623318614SAndy Shevchenko 		for_each_if(_label)
617b3337eb2SAndy Shevchenko 
618b3337eb2SAndy Shevchenko /* Iterates over all requested GPIO of the given @chip */
619b3337eb2SAndy Shevchenko #define for_each_requested_gpio(chip, i, label)						\
620b3337eb2SAndy Shevchenko 	for_each_requested_gpio_in_range(chip, i, 0, chip->ngpio, label)
621b3337eb2SAndy Shevchenko 
62279a9becdSAlexandre Courbot /* add/remove chips */
62337d42ab3SBartosz Golaszewski int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
62439c3fd58SAndrew Lunn 			       struct lock_class_key *lock_key,
62539c3fd58SAndrew Lunn 			       struct lock_class_key *request_key);
626959bc7b2SThierry Reding 
627959bc7b2SThierry Reding /**
628959bc7b2SThierry Reding  * gpiochip_add_data() - register a gpio_chip
6298fc3ed3aSColton Lewis  * @gc: the chip to register, with gc->base initialized
630959bc7b2SThierry Reding  * @data: driver-private data associated with this chip
631959bc7b2SThierry Reding  *
632959bc7b2SThierry Reding  * Context: potentially before irqs will work
633959bc7b2SThierry Reding  *
634959bc7b2SThierry Reding  * When gpiochip_add_data() is called very early during boot, so that GPIOs
6358fc3ed3aSColton Lewis  * can be freely used, the gc->parent device must be registered before
636959bc7b2SThierry Reding  * the gpio framework's arch_initcall().  Otherwise sysfs initialization
637959bc7b2SThierry Reding  * for GPIOs will fail rudely.
638959bc7b2SThierry Reding  *
639959bc7b2SThierry Reding  * gpiochip_add_data() must only be called after gpiolib initialization,
6402d93018fSRandy Dunlap  * i.e. after core_initcall().
641959bc7b2SThierry Reding  *
6428fc3ed3aSColton Lewis  * If gc->base is negative, this requests dynamic assignment of
643959bc7b2SThierry Reding  * a range of valid GPIOs.
644959bc7b2SThierry Reding  *
645959bc7b2SThierry Reding  * Returns:
646959bc7b2SThierry Reding  * A negative errno if the chip can't be registered, such as because the
6478fc3ed3aSColton Lewis  * gc->base is invalid or already associated with a different chip.
648959bc7b2SThierry Reding  * Otherwise it returns zero as a success code.
649959bc7b2SThierry Reding  */
650959bc7b2SThierry Reding #ifdef CONFIG_LOCKDEP
651a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) ({		\
65239c3fd58SAndrew Lunn 		static struct lock_class_key lock_key;	\
65339c3fd58SAndrew Lunn 		static struct lock_class_key request_key;	  \
654a0b66a73SLinus Walleij 		gpiochip_add_data_with_key(gc, data, &lock_key, \
65539c3fd58SAndrew Lunn 					   &request_key);	  \
656959bc7b2SThierry Reding 	})
6575f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) ({ \
6585f402bb1SAhmad Fatoum 		static struct lock_class_key lock_key;	\
6595f402bb1SAhmad Fatoum 		static struct lock_class_key request_key;	  \
6605f402bb1SAhmad Fatoum 		devm_gpiochip_add_data_with_key(dev, gc, data, &lock_key, \
6615f402bb1SAhmad Fatoum 					   &request_key);	  \
6625f402bb1SAhmad Fatoum 	})
663959bc7b2SThierry Reding #else
664a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) gpiochip_add_data_with_key(gc, data, NULL, NULL)
6655f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) \
6665f402bb1SAhmad Fatoum 	devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
667f310f2efSEnrico Weigelt #endif /* CONFIG_LOCKDEP */
668959bc7b2SThierry Reding 
66937d42ab3SBartosz Golaszewski void gpiochip_remove(struct gpio_chip *gc);
67037d42ab3SBartosz Golaszewski int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc,
67137d42ab3SBartosz Golaszewski 				    void *data, struct lock_class_key *lock_key,
6725f402bb1SAhmad Fatoum 				    struct lock_class_key *request_key);
6730cf3292cSLaxman Dewangan 
6744a92857dSKrzysztof Kozlowski struct gpio_device *gpio_device_find(const void *data,
675faf6efd2SKrzysztof Kozlowski 				int (*match)(struct gpio_chip *gc,
676faf6efd2SKrzysztof Kozlowski 					     const void *data));
677cfe102f6SBartosz Golaszewski 
67836aa129fSBartosz Golaszewski struct gpio_device *gpio_device_get(struct gpio_device *gdev);
67936aa129fSBartosz Golaszewski void gpio_device_put(struct gpio_device *gdev);
68036aa129fSBartosz Golaszewski 
6819e4555d1SBartosz Golaszewski DEFINE_FREE(gpio_device_put, struct gpio_device *,
682832b3710SLukas Wunner 	    if (!IS_ERR_OR_NULL(_T)) gpio_device_put(_T))
6839e4555d1SBartosz Golaszewski 
6841559d149SBartosz Golaszewski struct device *gpio_device_to_device(struct gpio_device *gdev);
6851559d149SBartosz Golaszewski 
686a0b66a73SLinus Walleij bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset);
687a0b66a73SLinus Walleij int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset);
688a0b66a73SLinus Walleij void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset);
689a0b66a73SLinus Walleij void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset);
690a0b66a73SLinus Walleij void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset);
69179a9becdSAlexandre Courbot 
692704f0875SMarc Zyngier /* irq_data versions of the above */
693704f0875SMarc Zyngier int gpiochip_irq_reqres(struct irq_data *data);
694704f0875SMarc Zyngier void gpiochip_irq_relres(struct irq_data *data);
695704f0875SMarc Zyngier 
69636b78aaeSMarc Zyngier /* Paste this in your irq_chip structure  */
69736b78aaeSMarc Zyngier #define	GPIOCHIP_IRQ_RESOURCE_HELPERS					\
69836b78aaeSMarc Zyngier 		.irq_request_resources  = gpiochip_irq_reqres,		\
69936b78aaeSMarc Zyngier 		.irq_release_resources  = gpiochip_irq_relres
70036b78aaeSMarc Zyngier 
gpio_irq_chip_set_chip(struct gpio_irq_chip * girq,const struct irq_chip * chip)70136b78aaeSMarc Zyngier static inline void gpio_irq_chip_set_chip(struct gpio_irq_chip *girq,
70236b78aaeSMarc Zyngier 					  const struct irq_chip *chip)
70336b78aaeSMarc Zyngier {
70436b78aaeSMarc Zyngier 	/* Yes, dropping const is ugly, but it isn't like we have a choice */
70536b78aaeSMarc Zyngier 	girq->chip = (struct irq_chip *)chip;
70636b78aaeSMarc Zyngier }
70736b78aaeSMarc Zyngier 
708143b65d6SLinus Walleij /* Line status inquiry for drivers */
709a0b66a73SLinus Walleij bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset);
710a0b66a73SLinus Walleij bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset);
711143b65d6SLinus Walleij 
71205f479bfSCharles Keepax /* Sleep persistence inquiry for drivers */
713a0b66a73SLinus Walleij bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset);
714a0b66a73SLinus Walleij bool gpiochip_line_is_valid(const struct gpio_chip *gc, unsigned int offset);
715f636d4f6SMatti Vaittinen const unsigned long *gpiochip_query_valid_mask(const struct gpio_chip *gc);
71605f479bfSCharles Keepax 
717b08ea35aSLinus Walleij /* get driver data */
718a0b66a73SLinus Walleij void *gpiochip_get_data(struct gpio_chip *gc);
719b08ea35aSLinus Walleij 
7200f4630f3SLinus Walleij struct bgpio_pdata {
7210f4630f3SLinus Walleij 	const char *label;
7220f4630f3SLinus Walleij 	int base;
7230f4630f3SLinus Walleij 	int ngpio;
7240f4630f3SLinus Walleij };
7250f4630f3SLinus Walleij 
726fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
727fdd61a01SLinus Walleij 
72891a29af4SMarc Zyngier int gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
72991a29af4SMarc Zyngier 					    union gpio_irq_fwspec *gfwspec,
730fdd61a01SLinus Walleij 					    unsigned int parent_hwirq,
731fdd61a01SLinus Walleij 					    unsigned int parent_type);
73291a29af4SMarc Zyngier int gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
73391a29af4SMarc Zyngier 					     union gpio_irq_fwspec *gfwspec,
734fdd61a01SLinus Walleij 					     unsigned int parent_hwirq,
735fdd61a01SLinus Walleij 					     unsigned int parent_type);
736fdd61a01SLinus Walleij 
737fdd61a01SLinus Walleij #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
738fdd61a01SLinus Walleij 
7390f4630f3SLinus Walleij int bgpio_init(struct gpio_chip *gc, struct device *dev,
7400f4630f3SLinus Walleij 	       unsigned long sz, void __iomem *dat, void __iomem *set,
7410f4630f3SLinus Walleij 	       void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
7420f4630f3SLinus Walleij 	       unsigned long flags);
7430f4630f3SLinus Walleij 
7440f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN		BIT(0)
7450f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_SET	BIT(1) /* reg_set is unreadable */
7460f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_DIR	BIT(2) /* reg_dir is unreadable */
7470f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN_BYTE_ORDER	BIT(3)
7480f4630f3SLinus Walleij #define BGPIOF_READ_OUTPUT_REG_SET	BIT(4) /* reg_set stores output value */
7490f4630f3SLinus Walleij #define BGPIOF_NO_OUTPUT		BIT(5) /* only input */
750d19d2de6SChuanhong Guo #define BGPIOF_NO_SET_ON_INPUT		BIT(6)
7512145ba37SLinus Walleij #define BGPIOF_PINCTRL_BACKEND		BIT(7) /* Call pinctrl direction setters */
7520f4630f3SLinus Walleij 
7539c7d2469SÁlvaro Fernández Rojas #ifdef CONFIG_GPIOLIB_IRQCHIP
7546a45b0e2SMichael Walle int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
7556a45b0e2SMichael Walle 				struct irq_domain *domain);
7569c7d2469SÁlvaro Fernández Rojas #else
757380c7ba3SAndy Shevchenko 
758380c7ba3SAndy Shevchenko #include <asm/bug.h>
759380c7ba3SAndy Shevchenko 
gpiochip_irqchip_add_domain(struct gpio_chip * gc,struct irq_domain * domain)7609c7d2469SÁlvaro Fernández Rojas static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
7619c7d2469SÁlvaro Fernández Rojas 					      struct irq_domain *domain)
7629c7d2469SÁlvaro Fernández Rojas {
7639c7d2469SÁlvaro Fernández Rojas 	WARN_ON(1);
7649c7d2469SÁlvaro Fernández Rojas 	return -EINVAL;
7659c7d2469SÁlvaro Fernández Rojas }
7669c7d2469SÁlvaro Fernández Rojas #endif
7676a45b0e2SMichael Walle 
7688d091012SDouglas Anderson int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);
7698d091012SDouglas Anderson void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset);
7708d091012SDouglas Anderson int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
7712956b5d9SMika Westerberg 			    unsigned long config);
772c771c2f4SJonas Gorski 
773964cb341SLinus Walleij /**
774964cb341SLinus Walleij  * struct gpio_pin_range - pin range controlled by a gpio chip
775950d55f5SThierry Reding  * @node: list for maintaining set of pin ranges, used internally
776964cb341SLinus Walleij  * @pctldev: pinctrl device which handles corresponding pins
777964cb341SLinus Walleij  * @range: actual range of pins controlled by a gpio controller
778964cb341SLinus Walleij  */
779964cb341SLinus Walleij struct gpio_pin_range {
780964cb341SLinus Walleij 	struct list_head node;
781964cb341SLinus Walleij 	struct pinctrl_dev *pctldev;
782964cb341SLinus Walleij 	struct pinctrl_gpio_range range;
783964cb341SLinus Walleij };
784964cb341SLinus Walleij 
7859091373aSMasahiro Yamada #ifdef CONFIG_PINCTRL
7869091373aSMasahiro Yamada 
787a0b66a73SLinus Walleij int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
788964cb341SLinus Walleij 			   unsigned int gpio_offset, unsigned int pin_offset,
789964cb341SLinus Walleij 			   unsigned int npins);
790a0b66a73SLinus Walleij int gpiochip_add_pingroup_range(struct gpio_chip *gc,
791964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
792964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group);
793a0b66a73SLinus Walleij void gpiochip_remove_pin_ranges(struct gpio_chip *gc);
794964cb341SLinus Walleij 
795f310f2efSEnrico Weigelt #else /* ! CONFIG_PINCTRL */
796964cb341SLinus Walleij 
797964cb341SLinus Walleij static inline int
gpiochip_add_pin_range(struct gpio_chip * gc,const char * pinctl_name,unsigned int gpio_offset,unsigned int pin_offset,unsigned int npins)798a0b66a73SLinus Walleij gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
799964cb341SLinus Walleij 		       unsigned int gpio_offset, unsigned int pin_offset,
800964cb341SLinus Walleij 		       unsigned int npins)
801964cb341SLinus Walleij {
802964cb341SLinus Walleij 	return 0;
803964cb341SLinus Walleij }
804964cb341SLinus Walleij static inline int
gpiochip_add_pingroup_range(struct gpio_chip * gc,struct pinctrl_dev * pctldev,unsigned int gpio_offset,const char * pin_group)805a0b66a73SLinus Walleij gpiochip_add_pingroup_range(struct gpio_chip *gc,
806964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
807964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group)
808964cb341SLinus Walleij {
809964cb341SLinus Walleij 	return 0;
810964cb341SLinus Walleij }
811964cb341SLinus Walleij 
812964cb341SLinus Walleij static inline void
gpiochip_remove_pin_ranges(struct gpio_chip * gc)813a0b66a73SLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *gc)
814964cb341SLinus Walleij {
815964cb341SLinus Walleij }
816964cb341SLinus Walleij 
817964cb341SLinus Walleij #endif /* CONFIG_PINCTRL */
818964cb341SLinus Walleij 
819a0b66a73SLinus Walleij struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
82006863620SBartosz Golaszewski 					    unsigned int hwnum,
82121abf103SLinus Walleij 					    const char *label,
8225923ea6cSLinus Walleij 					    enum gpio_lookup_flags lflags,
8235923ea6cSLinus Walleij 					    enum gpiod_flags dflags);
824f7d4ad98SGuenter Roeck void gpiochip_free_own_desc(struct gpio_desc *desc);
825f7d4ad98SGuenter Roeck 
82693548f8bSBartosz Golaszewski struct gpio_desc *
82793548f8bSBartosz Golaszewski gpio_device_get_desc(struct gpio_device *gdev, unsigned int hwnum);
828fe4fa2e4SBartosz Golaszewski 
8299b418780SBartosz Golaszewski struct gpio_chip *gpio_device_get_chip(struct gpio_device *gdev);
8309b418780SBartosz Golaszewski 
831ae0755b5SLinus Walleij #ifdef CONFIG_GPIOLIB
832ae0755b5SLinus Walleij 
833c7663fa2SYueHaibing /* lock/unlock as IRQ */
834a0b66a73SLinus Walleij int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
835a0b66a73SLinus Walleij void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);
836c7663fa2SYueHaibing 
8379091373aSMasahiro Yamada struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
838370232d0SBartosz Golaszewski struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc);
8399091373aSMasahiro Yamada 
8408c85a102SBartosz Golaszewski /* struct gpio_device getters */
8418c85a102SBartosz Golaszewski int gpio_device_get_base(struct gpio_device *gdev);
842d1f77282SBartosz Golaszewski const char *gpio_device_get_label(struct gpio_device *gdev);
8438c85a102SBartosz Golaszewski 
84452464f59SMark Brown struct gpio_device *gpio_device_find_by_label(const char *label);
84552464f59SMark Brown struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode);
84652464f59SMark Brown 
847bb1e88ccSAlexandre Courbot #else /* CONFIG_GPIOLIB */
848bb1e88ccSAlexandre Courbot 
849380c7ba3SAndy Shevchenko #include <asm/bug.h>
850380c7ba3SAndy Shevchenko 
gpiod_to_chip(const struct gpio_desc * desc)851bb1e88ccSAlexandre Courbot static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
852bb1e88ccSAlexandre Courbot {
853bb1e88ccSAlexandre Courbot 	/* GPIO can never have been requested */
854bb1e88ccSAlexandre Courbot 	WARN_ON(1);
855bb1e88ccSAlexandre Courbot 	return ERR_PTR(-ENODEV);
856bb1e88ccSAlexandre Courbot }
857bb1e88ccSAlexandre Courbot 
gpiod_to_gpio_device(struct gpio_desc * desc)8586ac86372SKrzysztof Kozlowski static inline struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
8596ac86372SKrzysztof Kozlowski {
8606ac86372SKrzysztof Kozlowski 	WARN_ON(1);
8616ac86372SKrzysztof Kozlowski 	return ERR_PTR(-ENODEV);
8626ac86372SKrzysztof Kozlowski }
8636ac86372SKrzysztof Kozlowski 
gpio_device_get_base(struct gpio_device * gdev)864ebe0c15bSKrzysztof Kozlowski static inline int gpio_device_get_base(struct gpio_device *gdev)
865ebe0c15bSKrzysztof Kozlowski {
866ebe0c15bSKrzysztof Kozlowski 	WARN_ON(1);
867ebe0c15bSKrzysztof Kozlowski 	return -ENODEV;
868ebe0c15bSKrzysztof Kozlowski }
869ebe0c15bSKrzysztof Kozlowski 
gpio_device_get_label(struct gpio_device * gdev)8702df8aa3cSKrzysztof Kozlowski static inline const char *gpio_device_get_label(struct gpio_device *gdev)
8712df8aa3cSKrzysztof Kozlowski {
8722df8aa3cSKrzysztof Kozlowski 	WARN_ON(1);
8732df8aa3cSKrzysztof Kozlowski 	return NULL;
8742df8aa3cSKrzysztof Kozlowski }
8752df8aa3cSKrzysztof Kozlowski 
gpio_device_find_by_label(const char * label)87652464f59SMark Brown static inline struct gpio_device *gpio_device_find_by_label(const char *label)
87752464f59SMark Brown {
87852464f59SMark Brown 	WARN_ON(1);
87952464f59SMark Brown 	return NULL;
88052464f59SMark Brown }
88152464f59SMark Brown 
gpio_device_find_by_fwnode(const struct fwnode_handle * fwnode)88252464f59SMark Brown static inline struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode)
88352464f59SMark Brown {
88452464f59SMark Brown 	WARN_ON(1);
88552464f59SMark Brown 	return NULL;
88652464f59SMark Brown }
88752464f59SMark Brown 
gpiochip_lock_as_irq(struct gpio_chip * gc,unsigned int offset)888a0b66a73SLinus Walleij static inline int gpiochip_lock_as_irq(struct gpio_chip *gc,
889c7663fa2SYueHaibing 				       unsigned int offset)
890c7663fa2SYueHaibing {
891c7663fa2SYueHaibing 	WARN_ON(1);
892c7663fa2SYueHaibing 	return -EINVAL;
893c7663fa2SYueHaibing }
894c7663fa2SYueHaibing 
gpiochip_unlock_as_irq(struct gpio_chip * gc,unsigned int offset)895a0b66a73SLinus Walleij static inline void gpiochip_unlock_as_irq(struct gpio_chip *gc,
896c7663fa2SYueHaibing 					  unsigned int offset)
897c7663fa2SYueHaibing {
898c7663fa2SYueHaibing 	WARN_ON(1);
899c7663fa2SYueHaibing }
900bb1e88ccSAlexandre Courbot #endif /* CONFIG_GPIOLIB */
901bb1e88ccSAlexandre Courbot 
90285ebb1a6SAndy Shevchenko #define for_each_gpiochip_node(dev, child)					\
90385ebb1a6SAndy Shevchenko 	device_for_each_child_node(dev, child)					\
90423318614SAndy Shevchenko 		for_each_if(fwnode_property_present(child, "gpio-controller"))
90585ebb1a6SAndy Shevchenko 
gpiochip_node_count(struct device * dev)9060b19dde9SAndy Shevchenko static inline unsigned int gpiochip_node_count(struct device *dev)
9070b19dde9SAndy Shevchenko {
9080b19dde9SAndy Shevchenko 	struct fwnode_handle *child;
9090b19dde9SAndy Shevchenko 	unsigned int count = 0;
9100b19dde9SAndy Shevchenko 
9110b19dde9SAndy Shevchenko 	for_each_gpiochip_node(dev, child)
9120b19dde9SAndy Shevchenko 		count++;
9130b19dde9SAndy Shevchenko 
9140b19dde9SAndy Shevchenko 	return count;
9150b19dde9SAndy Shevchenko }
9160b19dde9SAndy Shevchenko 
gpiochip_node_get_first(struct device * dev)917af47d803SAndy Shevchenko static inline struct fwnode_handle *gpiochip_node_get_first(struct device *dev)
918af47d803SAndy Shevchenko {
919af47d803SAndy Shevchenko 	struct fwnode_handle *fwnode;
920af47d803SAndy Shevchenko 
921af47d803SAndy Shevchenko 	for_each_gpiochip_node(dev, fwnode)
922af47d803SAndy Shevchenko 		return fwnode;
923af47d803SAndy Shevchenko 
924af47d803SAndy Shevchenko 	return NULL;
925af47d803SAndy Shevchenko }
926af47d803SAndy Shevchenko 
9279091373aSMasahiro Yamada #endif /* __LINUX_GPIO_DRIVER_H */
928