xref: /openbmc/linux/include/linux/gpio/driver.h (revision c7e1c443)
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 
5ff2b1359SLinus Walleij #include <linux/device.h>
614250520SLinus Walleij #include <linux/irq.h>
714250520SLinus Walleij #include <linux/irqchip/chained_irq.h>
814250520SLinus Walleij #include <linux/irqdomain.h>
9a0a8bcf4SGrygorii Strashko #include <linux/lockdep.h>
10964cb341SLinus Walleij #include <linux/pinctrl/pinctrl.h>
112956b5d9SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
1285ebb1a6SAndy Shevchenko #include <linux/property.h>
1385ebb1a6SAndy Shevchenko #include <linux/types.h>
1479a9becdSAlexandre Courbot 
1579a9becdSAlexandre Courbot struct gpio_desc;
16c9a9972bSAlexandre Courbot struct of_phandle_args;
17c9a9972bSAlexandre Courbot struct device_node;
18f3ed0b66SStephen Rothwell struct seq_file;
19ff2b1359SLinus Walleij struct gpio_device;
20d47529b2SPaul Gortmaker struct module;
2121abf103SLinus Walleij enum gpiod_flags;
225923ea6cSLinus Walleij enum gpio_lookup_flags;
2379a9becdSAlexandre Courbot 
24fdd61a01SLinus Walleij struct gpio_chip;
25fdd61a01SLinus Walleij 
269208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_IN	1
279208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_OUT	0
289208b1e7SMatti Vaittinen 
29c44eafd7SThierry Reding /**
30c44eafd7SThierry Reding  * struct gpio_irq_chip - GPIO interrupt controller
31c44eafd7SThierry Reding  */
32c44eafd7SThierry Reding struct gpio_irq_chip {
33c44eafd7SThierry Reding 	/**
34da80ff81SThierry Reding 	 * @chip:
35da80ff81SThierry Reding 	 *
36da80ff81SThierry Reding 	 * GPIO IRQ chip implementation, provided by GPIO driver.
37da80ff81SThierry Reding 	 */
38da80ff81SThierry Reding 	struct irq_chip *chip;
39da80ff81SThierry Reding 
40da80ff81SThierry Reding 	/**
41f0fbe7bcSThierry Reding 	 * @domain:
42f0fbe7bcSThierry Reding 	 *
43f0fbe7bcSThierry Reding 	 * Interrupt translation domain; responsible for mapping between GPIO
44f0fbe7bcSThierry Reding 	 * hwirq number and Linux IRQ number.
45f0fbe7bcSThierry Reding 	 */
46f0fbe7bcSThierry Reding 	struct irq_domain *domain;
47f0fbe7bcSThierry Reding 
48f0fbe7bcSThierry Reding 	/**
49c44eafd7SThierry Reding 	 * @domain_ops:
50c44eafd7SThierry Reding 	 *
51c44eafd7SThierry Reding 	 * Table of interrupt domain operations for this IRQ chip.
52c44eafd7SThierry Reding 	 */
53c44eafd7SThierry Reding 	const struct irq_domain_ops *domain_ops;
54c44eafd7SThierry Reding 
55fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
56fdd61a01SLinus Walleij 	/**
57fdd61a01SLinus Walleij 	 * @fwnode:
58fdd61a01SLinus Walleij 	 *
59fdd61a01SLinus Walleij 	 * Firmware node corresponding to this gpiochip/irqchip, necessary
60fdd61a01SLinus Walleij 	 * for hierarchical irqdomain support.
61fdd61a01SLinus Walleij 	 */
62fdd61a01SLinus Walleij 	struct fwnode_handle *fwnode;
63fdd61a01SLinus Walleij 
64fdd61a01SLinus Walleij 	/**
65fdd61a01SLinus Walleij 	 * @parent_domain:
66fdd61a01SLinus Walleij 	 *
67fdd61a01SLinus Walleij 	 * If non-NULL, will be set as the parent of this GPIO interrupt
68fdd61a01SLinus Walleij 	 * controller's IRQ domain to establish a hierarchical interrupt
69fdd61a01SLinus Walleij 	 * domain. The presence of this will activate the hierarchical
70fdd61a01SLinus Walleij 	 * interrupt support.
71fdd61a01SLinus Walleij 	 */
72fdd61a01SLinus Walleij 	struct irq_domain *parent_domain;
73fdd61a01SLinus Walleij 
74fdd61a01SLinus Walleij 	/**
75fdd61a01SLinus Walleij 	 * @child_to_parent_hwirq:
76fdd61a01SLinus Walleij 	 *
77fdd61a01SLinus Walleij 	 * This callback translates a child hardware IRQ offset to a parent
78fdd61a01SLinus Walleij 	 * hardware IRQ offset on a hierarchical interrupt chip. The child
79fdd61a01SLinus Walleij 	 * hardware IRQs correspond to the GPIO index 0..ngpio-1 (see the
80fdd61a01SLinus Walleij 	 * ngpio field of struct gpio_chip) and the corresponding parent
81fdd61a01SLinus Walleij 	 * hardware IRQ and type (such as IRQ_TYPE_*) shall be returned by
82fdd61a01SLinus Walleij 	 * the driver. The driver can calculate this from an offset or using
83fdd61a01SLinus Walleij 	 * a lookup table or whatever method is best for this chip. Return
84fdd61a01SLinus Walleij 	 * 0 on successful translation in the driver.
85fdd61a01SLinus Walleij 	 *
86fdd61a01SLinus Walleij 	 * If some ranges of hardware IRQs do not have a corresponding parent
87fdd61a01SLinus Walleij 	 * HWIRQ, return -EINVAL, but also make sure to fill in @valid_mask and
88fdd61a01SLinus Walleij 	 * @need_valid_mask to make these GPIO lines unavailable for
89fdd61a01SLinus Walleij 	 * translation.
90fdd61a01SLinus Walleij 	 */
91a0b66a73SLinus Walleij 	int (*child_to_parent_hwirq)(struct gpio_chip *gc,
92fdd61a01SLinus Walleij 				     unsigned int child_hwirq,
93fdd61a01SLinus Walleij 				     unsigned int child_type,
94fdd61a01SLinus Walleij 				     unsigned int *parent_hwirq,
95fdd61a01SLinus Walleij 				     unsigned int *parent_type);
96fdd61a01SLinus Walleij 
97fdd61a01SLinus Walleij 	/**
9824258761SKevin Hao 	 * @populate_parent_alloc_arg :
99fdd61a01SLinus Walleij 	 *
10024258761SKevin Hao 	 * This optional callback allocates and populates the specific struct
10124258761SKevin Hao 	 * for the parent's IRQ domain. If this is not specified, then
102fdd61a01SLinus Walleij 	 * &gpiochip_populate_parent_fwspec_twocell will be used. A four-cell
103fdd61a01SLinus Walleij 	 * variant named &gpiochip_populate_parent_fwspec_fourcell is also
104fdd61a01SLinus Walleij 	 * available.
105fdd61a01SLinus Walleij 	 */
106a0b66a73SLinus Walleij 	void *(*populate_parent_alloc_arg)(struct gpio_chip *gc,
107fdd61a01SLinus Walleij 				       unsigned int parent_hwirq,
108fdd61a01SLinus Walleij 				       unsigned int parent_type);
109fdd61a01SLinus Walleij 
110fdd61a01SLinus Walleij 	/**
111fdd61a01SLinus Walleij 	 * @child_offset_to_irq:
112fdd61a01SLinus Walleij 	 *
113fdd61a01SLinus Walleij 	 * This optional callback is used to translate the child's GPIO line
114fdd61a01SLinus Walleij 	 * offset on the GPIO chip to an IRQ number for the GPIO to_irq()
115fdd61a01SLinus Walleij 	 * callback. If this is not specified, then a default callback will be
116fdd61a01SLinus Walleij 	 * provided that returns the line offset.
117fdd61a01SLinus Walleij 	 */
118a0b66a73SLinus Walleij 	unsigned int (*child_offset_to_irq)(struct gpio_chip *gc,
119fdd61a01SLinus Walleij 					    unsigned int pin);
120fdd61a01SLinus Walleij 
121fdd61a01SLinus Walleij 	/**
122fdd61a01SLinus Walleij 	 * @child_irq_domain_ops:
123fdd61a01SLinus Walleij 	 *
124fdd61a01SLinus Walleij 	 * The IRQ domain operations that will be used for this GPIO IRQ
125fdd61a01SLinus Walleij 	 * chip. If no operations are provided, then default callbacks will
126fdd61a01SLinus Walleij 	 * be populated to setup the IRQ hierarchy. Some drivers need to
127fdd61a01SLinus Walleij 	 * supply their own translate function.
128fdd61a01SLinus Walleij 	 */
129fdd61a01SLinus Walleij 	struct irq_domain_ops child_irq_domain_ops;
130fdd61a01SLinus Walleij #endif
131fdd61a01SLinus Walleij 
132c44eafd7SThierry Reding 	/**
133c7a0aa59SThierry Reding 	 * @handler:
134c7a0aa59SThierry Reding 	 *
135c7a0aa59SThierry Reding 	 * The IRQ handler to use (often a predefined IRQ core function) for
136c7a0aa59SThierry Reding 	 * GPIO IRQs, provided by GPIO driver.
137c7a0aa59SThierry Reding 	 */
138c7a0aa59SThierry Reding 	irq_flow_handler_t handler;
139c7a0aa59SThierry Reding 
140c7a0aa59SThierry Reding 	/**
1413634eeb0SThierry Reding 	 * @default_type:
1423634eeb0SThierry Reding 	 *
1433634eeb0SThierry Reding 	 * Default IRQ triggering type applied during GPIO driver
1443634eeb0SThierry Reding 	 * initialization, provided by GPIO driver.
1453634eeb0SThierry Reding 	 */
1463634eeb0SThierry Reding 	unsigned int default_type;
1473634eeb0SThierry Reding 
1483634eeb0SThierry Reding 	/**
149ca9df053SThierry Reding 	 * @lock_key:
150ca9df053SThierry Reding 	 *
15102ad0437SRandy Dunlap 	 * Per GPIO IRQ chip lockdep class for IRQ lock.
152ca9df053SThierry Reding 	 */
153ca9df053SThierry Reding 	struct lock_class_key *lock_key;
15402ad0437SRandy Dunlap 
15502ad0437SRandy Dunlap 	/**
15602ad0437SRandy Dunlap 	 * @request_key:
15702ad0437SRandy Dunlap 	 *
15802ad0437SRandy Dunlap 	 * Per GPIO IRQ chip lockdep class for IRQ request.
15902ad0437SRandy Dunlap 	 */
16039c3fd58SAndrew Lunn 	struct lock_class_key *request_key;
161ca9df053SThierry Reding 
162ca9df053SThierry Reding 	/**
163c44eafd7SThierry Reding 	 * @parent_handler:
164c44eafd7SThierry Reding 	 *
165c44eafd7SThierry Reding 	 * The interrupt handler for the GPIO chip's parent interrupts, may be
166c44eafd7SThierry Reding 	 * NULL if the parent interrupts are nested rather than cascaded.
167c44eafd7SThierry Reding 	 */
168c44eafd7SThierry Reding 	irq_flow_handler_t parent_handler;
169c44eafd7SThierry Reding 
170*c7e1c443SAkira Yokosawa 	union {
171c44eafd7SThierry Reding 		/**
172c44eafd7SThierry Reding 		 * @parent_handler_data:
17348ec13d3SJoey Gouly 		 *
174*c7e1c443SAkira Yokosawa 		 * If @per_parent_data is false, @parent_handler_data is a
175*c7e1c443SAkira Yokosawa 		 * single pointer used as the data associated with every
176*c7e1c443SAkira Yokosawa 		 * parent interrupt.
177*c7e1c443SAkira Yokosawa 		 */
178*c7e1c443SAkira Yokosawa 		void *parent_handler_data;
179*c7e1c443SAkira Yokosawa 
180*c7e1c443SAkira Yokosawa 		/**
181cfe6807dSMarc Zyngier 		 * @parent_handler_data_array:
182c44eafd7SThierry Reding 		 *
18348ec13d3SJoey Gouly 		 * If @per_parent_data is true, @parent_handler_data_array is
18448ec13d3SJoey Gouly 		 * an array of @num_parents pointers, and is used to associate
18548ec13d3SJoey Gouly 		 * different data for each parent. This cannot be NULL if
18648ec13d3SJoey Gouly 		 * @per_parent_data is true.
187c44eafd7SThierry Reding 		 */
188cfe6807dSMarc Zyngier 		void **parent_handler_data_array;
189cfe6807dSMarc Zyngier 	};
19039e5f096SThierry Reding 
19139e5f096SThierry Reding 	/**
19239e5f096SThierry Reding 	 * @num_parents:
19339e5f096SThierry Reding 	 *
19439e5f096SThierry Reding 	 * The number of interrupt parents of a GPIO chip.
19539e5f096SThierry Reding 	 */
19639e5f096SThierry Reding 	unsigned int num_parents;
19739e5f096SThierry Reding 
19839e5f096SThierry Reding 	/**
19939e5f096SThierry Reding 	 * @parents:
20039e5f096SThierry Reding 	 *
20139e5f096SThierry Reding 	 * A list of interrupt parents of a GPIO chip. This is owned by the
20239e5f096SThierry Reding 	 * driver, so the core will only reference this list, not modify it.
20339e5f096SThierry Reding 	 */
20439e5f096SThierry Reding 	unsigned int *parents;
205dc6bafeeSThierry Reding 
206dc6bafeeSThierry Reding 	/**
207e0d89728SThierry Reding 	 * @map:
208e0d89728SThierry Reding 	 *
209e0d89728SThierry Reding 	 * A list of interrupt parents for each line of a GPIO chip.
210e0d89728SThierry Reding 	 */
211e0d89728SThierry Reding 	unsigned int *map;
212e0d89728SThierry Reding 
213e0d89728SThierry Reding 	/**
21460ed54caSThierry Reding 	 * @threaded:
215dc6bafeeSThierry Reding 	 *
21660ed54caSThierry Reding 	 * True if set the interrupt handling uses nested threads.
217dc6bafeeSThierry Reding 	 */
21860ed54caSThierry Reding 	bool threaded;
219dc7b0387SThierry Reding 
220dc7b0387SThierry Reding 	/**
221cfe6807dSMarc Zyngier 	 * @per_parent_data:
222cfe6807dSMarc Zyngier 	 *
223cfe6807dSMarc Zyngier 	 * True if parent_handler_data_array describes a @num_parents
224cfe6807dSMarc Zyngier 	 * sized array to be used as parent data.
225cfe6807dSMarc Zyngier 	 */
226cfe6807dSMarc Zyngier 	bool per_parent_data;
227cfe6807dSMarc Zyngier 
228cfe6807dSMarc Zyngier 	/**
2295467801fSShreeya Patel 	 * @initialized:
2305467801fSShreeya Patel 	 *
2315467801fSShreeya Patel 	 * Flag to track GPIO chip irq member's initialization.
2325467801fSShreeya Patel 	 * This flag will make sure GPIO chip irq members are not used
2335467801fSShreeya Patel 	 * before they are initialized.
2345467801fSShreeya Patel 	 */
2355467801fSShreeya Patel 	bool initialized;
2365467801fSShreeya Patel 
2375467801fSShreeya Patel 	/**
2389411e3aaSAndy Shevchenko 	 * @init_hw: optional routine to initialize hardware before
2399411e3aaSAndy Shevchenko 	 * an IRQ chip will be added. This is quite useful when
2409411e3aaSAndy Shevchenko 	 * a particular driver wants to clear IRQ related registers
2419411e3aaSAndy Shevchenko 	 * in order to avoid undesired events.
2429411e3aaSAndy Shevchenko 	 */
243a0b66a73SLinus Walleij 	int (*init_hw)(struct gpio_chip *gc);
2449411e3aaSAndy Shevchenko 
2459411e3aaSAndy Shevchenko 	/**
2465fbe5b58SLinus Walleij 	 * @init_valid_mask: optional routine to initialize @valid_mask, to be
2475fbe5b58SLinus Walleij 	 * used if not all GPIO lines are valid interrupts. Sometimes some
2485fbe5b58SLinus Walleij 	 * lines just cannot fire interrupts, and this routine, when defined,
2495fbe5b58SLinus Walleij 	 * is passed a bitmap in "valid_mask" and it will have ngpios
2505fbe5b58SLinus Walleij 	 * bits from 0..(ngpios-1) set to "1" as in valid. The callback can
2515fbe5b58SLinus Walleij 	 * then directly set some bits to "0" if they cannot be used for
2525fbe5b58SLinus Walleij 	 * interrupts.
253dc7b0387SThierry Reding 	 */
254a0b66a73SLinus Walleij 	void (*init_valid_mask)(struct gpio_chip *gc,
2555fbe5b58SLinus Walleij 				unsigned long *valid_mask,
2565fbe5b58SLinus Walleij 				unsigned int ngpios);
257dc7b0387SThierry Reding 
258dc7b0387SThierry Reding 	/**
259dc7b0387SThierry Reding 	 * @valid_mask:
260dc7b0387SThierry Reding 	 *
2612d93018fSRandy Dunlap 	 * If not %NULL, holds bitmask of GPIOs which are valid to be included
262dc7b0387SThierry Reding 	 * in IRQ domain of the chip.
263dc7b0387SThierry Reding 	 */
264dc7b0387SThierry Reding 	unsigned long *valid_mask;
2658302cf58SThierry Reding 
2668302cf58SThierry Reding 	/**
2678302cf58SThierry Reding 	 * @first:
2688302cf58SThierry Reding 	 *
2698302cf58SThierry Reding 	 * Required for static IRQ allocation. If set, irq_domain_add_simple()
2708302cf58SThierry Reding 	 * will allocate and map all IRQs during initialization.
2718302cf58SThierry Reding 	 */
2728302cf58SThierry Reding 	unsigned int first;
273461c1a7dSHans Verkuil 
274461c1a7dSHans Verkuil 	/**
275461c1a7dSHans Verkuil 	 * @irq_enable:
276461c1a7dSHans Verkuil 	 *
277461c1a7dSHans Verkuil 	 * Store old irq_chip irq_enable callback
278461c1a7dSHans Verkuil 	 */
279461c1a7dSHans Verkuil 	void		(*irq_enable)(struct irq_data *data);
280461c1a7dSHans Verkuil 
281461c1a7dSHans Verkuil 	/**
282461c1a7dSHans Verkuil 	 * @irq_disable:
283461c1a7dSHans Verkuil 	 *
284461c1a7dSHans Verkuil 	 * Store old irq_chip irq_disable callback
285461c1a7dSHans Verkuil 	 */
286461c1a7dSHans Verkuil 	void		(*irq_disable)(struct irq_data *data);
287a8173820SMaulik Shah 	/**
288a8173820SMaulik Shah 	 * @irq_unmask:
289a8173820SMaulik Shah 	 *
290a8173820SMaulik Shah 	 * Store old irq_chip irq_unmask callback
291a8173820SMaulik Shah 	 */
292a8173820SMaulik Shah 	void		(*irq_unmask)(struct irq_data *data);
293a8173820SMaulik Shah 
294a8173820SMaulik Shah 	/**
295a8173820SMaulik Shah 	 * @irq_mask:
296a8173820SMaulik Shah 	 *
297a8173820SMaulik Shah 	 * Store old irq_chip irq_mask callback
298a8173820SMaulik Shah 	 */
299a8173820SMaulik Shah 	void		(*irq_mask)(struct irq_data *data);
300c44eafd7SThierry Reding };
301c44eafd7SThierry Reding 
30279a9becdSAlexandre Courbot /**
30379a9becdSAlexandre Courbot  * struct gpio_chip - abstract a GPIO controller
304df4878e9SLinus Walleij  * @label: a functional name for the GPIO device, such as a part
305df4878e9SLinus Walleij  *	number or the name of the SoC IP-block implementing it.
306ff2b1359SLinus Walleij  * @gpiodev: the internal state holder, opaque struct
30758383c78SLinus Walleij  * @parent: optional parent device providing the GPIOs
308990f6756SBartosz Golaszewski  * @fwnode: optional fwnode providing this controller's properties
30979a9becdSAlexandre Courbot  * @owner: helps prevent removal of modules exporting active GPIOs
31079a9becdSAlexandre Courbot  * @request: optional hook for chip-specific activation, such as
31179a9becdSAlexandre Courbot  *	enabling module power and clock; may sleep
31279a9becdSAlexandre Courbot  * @free: optional hook for chip-specific deactivation, such as
31379a9becdSAlexandre Courbot  *	disabling module power and clock; may sleep
31479a9becdSAlexandre Courbot  * @get_direction: returns direction for signal "offset", 0=out, 1=in,
31536b52154SDouglas Anderson  *	(same as GPIO_LINE_DIRECTION_OUT / GPIO_LINE_DIRECTION_IN),
31636b52154SDouglas Anderson  *	or negative error. It is recommended to always implement this
31736b52154SDouglas Anderson  *	function, even on input-only or output-only gpio chips.
31879a9becdSAlexandre Courbot  * @direction_input: configures signal "offset" as input, or returns error
319e48d194dSLinus Walleij  *	This can be omitted on input-only or output-only gpio chips.
32079a9becdSAlexandre Courbot  * @direction_output: configures signal "offset" as output, or returns error
321e48d194dSLinus Walleij  *	This can be omitted on input-only or output-only gpio chips.
32260befd2eSVladimir Zapolskiy  * @get: returns value for signal "offset", 0=low, 1=high, or negative error
323eec1d566SLukas Wunner  * @get_multiple: reads values for multiple signals defined by "mask" and
324eec1d566SLukas Wunner  *	stores them in "bits", returns 0 on success or negative error
32579a9becdSAlexandre Courbot  * @set: assigns output value for signal "offset"
3265f424243SRojhalat Ibrahim  * @set_multiple: assigns output values for multiple signals defined by "mask"
3272956b5d9SMika Westerberg  * @set_config: optional hook for all kinds of settings. Uses the same
3282956b5d9SMika Westerberg  *	packed config format as generic pinconf.
32979a9becdSAlexandre Courbot  * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
33079a9becdSAlexandre Courbot  *	implementation may not sleep
33179a9becdSAlexandre Courbot  * @dbg_show: optional routine to show contents in debugfs; default code
33279a9becdSAlexandre Courbot  *	will be used when this is omitted, but custom code can show extra
33379a9becdSAlexandre Courbot  *	state (such as pullup/pulldown configuration).
334f99d479bSGeert Uytterhoeven  * @init_valid_mask: optional routine to initialize @valid_mask, to be used if
335f99d479bSGeert Uytterhoeven  *	not all GPIOs are valid.
336b056ca1cSAndy Shevchenko  * @add_pin_ranges: optional routine to initialize pin ranges, to be used when
337b056ca1cSAndy Shevchenko  *	requires special mapping of the pins that provides GPIO functionality.
338b056ca1cSAndy Shevchenko  *	It is called after adding GPIO chip and before adding IRQ chip.
33942112dd7SDipen Patel  * @en_hw_timestamp: Dependent on GPIO chip, an optional routine to
34042112dd7SDipen Patel  *	enable hardware timestamp.
34142112dd7SDipen Patel  * @dis_hw_timestamp: Dependent on GPIO chip, an optional routine to
34242112dd7SDipen Patel  *	disable hardware timestamp.
343af6c235dSLinus Walleij  * @base: identifies the first GPIO number handled by this chip;
344af6c235dSLinus Walleij  *	or, if negative during registration, requests dynamic ID allocation.
345af6c235dSLinus Walleij  *	DEPRECATION: providing anything non-negative and nailing the base
34630bb6fb3SGeert Uytterhoeven  *	offset of GPIO chips is deprecated. Please pass -1 as base to
347af6c235dSLinus Walleij  *	let gpiolib select the chip base in all possible cases. We want to
348af6c235dSLinus Walleij  *	get rid of the static GPIO number space in the long run.
34979a9becdSAlexandre Courbot  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
35079a9becdSAlexandre Courbot  *	handled is (base + ngpio - 1).
3514e804c39SSergio Paracuellos  * @offset: when multiple gpio chips belong to the same device this
3524e804c39SSergio Paracuellos  *	can be used as offset within the device so friendly names can
3534e804c39SSergio Paracuellos  *	be properly assigned.
35479a9becdSAlexandre Courbot  * @names: if set, must be an array of strings to use as alternative
35579a9becdSAlexandre Courbot  *      names for the GPIOs in this chip. Any entry in the array
35679a9becdSAlexandre Courbot  *      may be NULL if there is no alias for the GPIO, however the
35779a9becdSAlexandre Courbot  *      array must be @ngpio entries long.  A name can include a single printk
35879a9becdSAlexandre Courbot  *      format specifier for an unsigned int.  It is substituted by the actual
35979a9becdSAlexandre Courbot  *      number of the gpio.
3609fb1f39eSLinus Walleij  * @can_sleep: flag must be set iff get()/set() methods sleep, as they
3611c8732bbSLinus Walleij  *	must while accessing GPIO expander chips over I2C or SPI. This
3621c8732bbSLinus Walleij  *	implies that if the chip supports IRQs, these IRQs need to be threaded
3631c8732bbSLinus Walleij  *	as the chip access may sleep when e.g. reading out the IRQ status
3641c8732bbSLinus Walleij  *	registers.
3650f4630f3SLinus Walleij  * @read_reg: reader function for generic GPIO
3660f4630f3SLinus Walleij  * @write_reg: writer function for generic GPIO
36724efd94bSLinus Walleij  * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing
36824efd94bSLinus Walleij  *	line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the
36924efd94bSLinus Walleij  *	generic GPIO core. It is for internal housekeeping only.
3700f4630f3SLinus Walleij  * @reg_dat: data (in) register for generic GPIO
3710f4630f3SLinus Walleij  * @reg_set: output set register (out=high) for generic GPIO
37208bcd3edSAnthony Best  * @reg_clr: output clear register (out=low) for generic GPIO
373f69e00bdSLinus Walleij  * @reg_dir_out: direction out setting register for generic GPIO
374f69e00bdSLinus Walleij  * @reg_dir_in: direction in setting register for generic GPIO
375f69e00bdSLinus Walleij  * @bgpio_dir_unreadable: indicates that the direction register(s) cannot
376f69e00bdSLinus Walleij  *	be read and we need to rely on out internal state tracking.
3770f4630f3SLinus Walleij  * @bgpio_bits: number of register bits used for a generic GPIO i.e.
3780f4630f3SLinus Walleij  *	<register width> * 8
3790f4630f3SLinus Walleij  * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep
3800f4630f3SLinus Walleij  *	shadowed and real data registers writes together.
3810f4630f3SLinus Walleij  * @bgpio_data:	shadowed data register for generic GPIO to clear/set bits
3820f4630f3SLinus Walleij  *	safely.
3830f4630f3SLinus Walleij  * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
384f69e00bdSLinus Walleij  *	direction safely. A "1" in this word means the line is set as
385f69e00bdSLinus Walleij  *	output.
38679a9becdSAlexandre Courbot  *
38779a9becdSAlexandre Courbot  * A gpio_chip can help platforms abstract various sources of GPIOs so
3882d93018fSRandy Dunlap  * they can all be accessed through a common programming interface.
38979a9becdSAlexandre Courbot  * Example sources would be SOC controllers, FPGAs, multifunction
39079a9becdSAlexandre Courbot  * chips, dedicated GPIO expanders, and so on.
39179a9becdSAlexandre Courbot  *
39279a9becdSAlexandre Courbot  * Each chip controls a number of signals, identified in method calls
39379a9becdSAlexandre Courbot  * by "offset" values in the range 0..(@ngpio - 1).  When those signals
39479a9becdSAlexandre Courbot  * are referenced through calls like gpio_get_value(gpio), the offset
39579a9becdSAlexandre Courbot  * is calculated by subtracting @base from the gpio number.
39679a9becdSAlexandre Courbot  */
39779a9becdSAlexandre Courbot struct gpio_chip {
39879a9becdSAlexandre Courbot 	const char		*label;
399ff2b1359SLinus Walleij 	struct gpio_device	*gpiodev;
40058383c78SLinus Walleij 	struct device		*parent;
401990f6756SBartosz Golaszewski 	struct fwnode_handle	*fwnode;
40279a9becdSAlexandre Courbot 	struct module		*owner;
40379a9becdSAlexandre Courbot 
404a0b66a73SLinus Walleij 	int			(*request)(struct gpio_chip *gc,
4058d091012SDouglas Anderson 						unsigned int offset);
406a0b66a73SLinus Walleij 	void			(*free)(struct gpio_chip *gc,
4078d091012SDouglas Anderson 						unsigned int offset);
408a0b66a73SLinus Walleij 	int			(*get_direction)(struct gpio_chip *gc,
4098d091012SDouglas Anderson 						unsigned int offset);
410a0b66a73SLinus Walleij 	int			(*direction_input)(struct gpio_chip *gc,
4118d091012SDouglas Anderson 						unsigned int offset);
412a0b66a73SLinus Walleij 	int			(*direction_output)(struct gpio_chip *gc,
4138d091012SDouglas Anderson 						unsigned int offset, int value);
414a0b66a73SLinus Walleij 	int			(*get)(struct gpio_chip *gc,
4158d091012SDouglas Anderson 						unsigned int offset);
416a0b66a73SLinus Walleij 	int			(*get_multiple)(struct gpio_chip *gc,
417eec1d566SLukas Wunner 						unsigned long *mask,
418eec1d566SLukas Wunner 						unsigned long *bits);
419a0b66a73SLinus Walleij 	void			(*set)(struct gpio_chip *gc,
4208d091012SDouglas Anderson 						unsigned int offset, int value);
421a0b66a73SLinus Walleij 	void			(*set_multiple)(struct gpio_chip *gc,
4225f424243SRojhalat Ibrahim 						unsigned long *mask,
4235f424243SRojhalat Ibrahim 						unsigned long *bits);
424a0b66a73SLinus Walleij 	int			(*set_config)(struct gpio_chip *gc,
4258d091012SDouglas Anderson 					      unsigned int offset,
4262956b5d9SMika Westerberg 					      unsigned long config);
427a0b66a73SLinus Walleij 	int			(*to_irq)(struct gpio_chip *gc,
4288d091012SDouglas Anderson 						unsigned int offset);
42979a9becdSAlexandre Courbot 
43079a9becdSAlexandre Courbot 	void			(*dbg_show)(struct seq_file *s,
431a0b66a73SLinus Walleij 						struct gpio_chip *gc);
432f8ec92a9SRicardo Ribalda Delgado 
433a0b66a73SLinus Walleij 	int			(*init_valid_mask)(struct gpio_chip *gc,
434c9fc5affSLinus Walleij 						   unsigned long *valid_mask,
435c9fc5affSLinus Walleij 						   unsigned int ngpios);
436f8ec92a9SRicardo Ribalda Delgado 
437a0b66a73SLinus Walleij 	int			(*add_pin_ranges)(struct gpio_chip *gc);
438b056ca1cSAndy Shevchenko 
43942112dd7SDipen Patel 	int			(*en_hw_timestamp)(struct gpio_chip *gc,
44042112dd7SDipen Patel 						   u32 offset,
44142112dd7SDipen Patel 						   unsigned long flags);
44242112dd7SDipen Patel 	int			(*dis_hw_timestamp)(struct gpio_chip *gc,
44342112dd7SDipen Patel 						    u32 offset,
44442112dd7SDipen Patel 						    unsigned long flags);
44579a9becdSAlexandre Courbot 	int			base;
44679a9becdSAlexandre Courbot 	u16			ngpio;
4474e804c39SSergio Paracuellos 	u16			offset;
44879a9becdSAlexandre Courbot 	const char		*const *names;
4499fb1f39eSLinus Walleij 	bool			can_sleep;
45079a9becdSAlexandre Courbot 
4510f4630f3SLinus Walleij #if IS_ENABLED(CONFIG_GPIO_GENERIC)
4520f4630f3SLinus Walleij 	unsigned long (*read_reg)(void __iomem *reg);
4530f4630f3SLinus Walleij 	void (*write_reg)(void __iomem *reg, unsigned long data);
45424efd94bSLinus Walleij 	bool be_bits;
4550f4630f3SLinus Walleij 	void __iomem *reg_dat;
4560f4630f3SLinus Walleij 	void __iomem *reg_set;
4570f4630f3SLinus Walleij 	void __iomem *reg_clr;
458f69e00bdSLinus Walleij 	void __iomem *reg_dir_out;
459f69e00bdSLinus Walleij 	void __iomem *reg_dir_in;
460f69e00bdSLinus Walleij 	bool bgpio_dir_unreadable;
4610f4630f3SLinus Walleij 	int bgpio_bits;
4623c938cc5SSchspa Shi 	raw_spinlock_t bgpio_lock;
4630f4630f3SLinus Walleij 	unsigned long bgpio_data;
4640f4630f3SLinus Walleij 	unsigned long bgpio_dir;
465f310f2efSEnrico Weigelt #endif /* CONFIG_GPIO_GENERIC */
4660f4630f3SLinus Walleij 
46714250520SLinus Walleij #ifdef CONFIG_GPIOLIB_IRQCHIP
46814250520SLinus Walleij 	/*
4697d75a871SPaul Bolle 	 * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
47014250520SLinus Walleij 	 * to handle IRQs for most practical cases.
47114250520SLinus Walleij 	 */
472c44eafd7SThierry Reding 
473c44eafd7SThierry Reding 	/**
474c44eafd7SThierry Reding 	 * @irq:
475c44eafd7SThierry Reding 	 *
476c44eafd7SThierry Reding 	 * Integrates interrupt chip functionality with the GPIO chip. Can be
477c44eafd7SThierry Reding 	 * used to handle IRQs for most practical cases.
478c44eafd7SThierry Reding 	 */
479c44eafd7SThierry Reding 	struct gpio_irq_chip irq;
480f310f2efSEnrico Weigelt #endif /* CONFIG_GPIOLIB_IRQCHIP */
48114250520SLinus Walleij 
482726cb3baSStephen Boyd 	/**
483726cb3baSStephen Boyd 	 * @valid_mask:
484726cb3baSStephen Boyd 	 *
4852d93018fSRandy Dunlap 	 * If not %NULL, holds bitmask of GPIOs which are valid to be used
486726cb3baSStephen Boyd 	 * from the chip.
487726cb3baSStephen Boyd 	 */
488726cb3baSStephen Boyd 	unsigned long *valid_mask;
489726cb3baSStephen Boyd 
49079a9becdSAlexandre Courbot #if defined(CONFIG_OF_GPIO)
49179a9becdSAlexandre Courbot 	/*
4922d93018fSRandy Dunlap 	 * If CONFIG_OF_GPIO is enabled, then all GPIO controllers described in
4932d93018fSRandy Dunlap 	 * the device tree automatically may have an OF translation
49479a9becdSAlexandre Courbot 	 */
49567049c50SThierry Reding 
49667049c50SThierry Reding 	/**
49767049c50SThierry Reding 	 * @of_node:
49867049c50SThierry Reding 	 *
49967049c50SThierry Reding 	 * Pointer to a device tree node representing this GPIO controller.
50067049c50SThierry Reding 	 */
50179a9becdSAlexandre Courbot 	struct device_node *of_node;
50267049c50SThierry Reding 
50367049c50SThierry Reding 	/**
50467049c50SThierry Reding 	 * @of_gpio_n_cells:
50567049c50SThierry Reding 	 *
50667049c50SThierry Reding 	 * Number of cells used to form the GPIO specifier.
50767049c50SThierry Reding 	 */
508e3b445d7SThierry Reding 	unsigned int of_gpio_n_cells;
50967049c50SThierry Reding 
51067049c50SThierry Reding 	/**
51167049c50SThierry Reding 	 * @of_xlate:
51267049c50SThierry Reding 	 *
51367049c50SThierry Reding 	 * Callback to translate a device tree GPIO specifier into a chip-
51467049c50SThierry Reding 	 * relative GPIO number and flags.
51567049c50SThierry Reding 	 */
51679a9becdSAlexandre Courbot 	int (*of_xlate)(struct gpio_chip *gc,
51779a9becdSAlexandre Courbot 			const struct of_phandle_args *gpiospec, u32 *flags);
5183550bba2SStefan Wahren 
5193550bba2SStefan Wahren 	/**
5203550bba2SStefan Wahren 	 * @of_gpio_ranges_fallback:
5213550bba2SStefan Wahren 	 *
5223550bba2SStefan Wahren 	 * Optional hook for the case that no gpio-ranges property is defined
5233550bba2SStefan Wahren 	 * within the device tree node "np" (usually DT before introduction
5243550bba2SStefan Wahren 	 * of gpio-ranges). So this callback is helpful to provide the
5253550bba2SStefan Wahren 	 * necessary backward compatibility for the pin ranges.
5263550bba2SStefan Wahren 	 */
5273550bba2SStefan Wahren 	int (*of_gpio_ranges_fallback)(struct gpio_chip *gc,
5283550bba2SStefan Wahren 				       struct device_node *np);
5293550bba2SStefan Wahren 
530f310f2efSEnrico Weigelt #endif /* CONFIG_OF_GPIO */
53179a9becdSAlexandre Courbot };
53279a9becdSAlexandre Courbot 
533a0b66a73SLinus Walleij extern const char *gpiochip_is_requested(struct gpio_chip *gc,
5348d091012SDouglas Anderson 			unsigned int offset);
53579a9becdSAlexandre Courbot 
536b3337eb2SAndy Shevchenko /**
537b3337eb2SAndy Shevchenko  * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range
538b3337eb2SAndy Shevchenko  * @chip:	the chip to query
539b3337eb2SAndy Shevchenko  * @i:		loop variable
540b3337eb2SAndy Shevchenko  * @base:	first GPIO in the range
541b3337eb2SAndy Shevchenko  * @size:	amount of GPIOs to check starting from @base
542b3337eb2SAndy Shevchenko  * @label:	label of current GPIO
543b3337eb2SAndy Shevchenko  */
544b3337eb2SAndy Shevchenko #define for_each_requested_gpio_in_range(chip, i, base, size, label)			\
545b3337eb2SAndy Shevchenko 	for (i = 0; i < size; i++)							\
546b3337eb2SAndy Shevchenko 		if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
547b3337eb2SAndy Shevchenko 
548b3337eb2SAndy Shevchenko /* Iterates over all requested GPIO of the given @chip */
549b3337eb2SAndy Shevchenko #define for_each_requested_gpio(chip, i, label)						\
550b3337eb2SAndy Shevchenko 	for_each_requested_gpio_in_range(chip, i, 0, chip->ngpio, label)
551b3337eb2SAndy Shevchenko 
55279a9becdSAlexandre Courbot /* add/remove chips */
553a0b66a73SLinus Walleij extern int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
55439c3fd58SAndrew Lunn 				      struct lock_class_key *lock_key,
55539c3fd58SAndrew Lunn 				      struct lock_class_key *request_key);
556959bc7b2SThierry Reding 
557959bc7b2SThierry Reding /**
558959bc7b2SThierry Reding  * gpiochip_add_data() - register a gpio_chip
5598fc3ed3aSColton Lewis  * @gc: the chip to register, with gc->base initialized
560959bc7b2SThierry Reding  * @data: driver-private data associated with this chip
561959bc7b2SThierry Reding  *
562959bc7b2SThierry Reding  * Context: potentially before irqs will work
563959bc7b2SThierry Reding  *
564959bc7b2SThierry Reding  * When gpiochip_add_data() is called very early during boot, so that GPIOs
5658fc3ed3aSColton Lewis  * can be freely used, the gc->parent device must be registered before
566959bc7b2SThierry Reding  * the gpio framework's arch_initcall().  Otherwise sysfs initialization
567959bc7b2SThierry Reding  * for GPIOs will fail rudely.
568959bc7b2SThierry Reding  *
569959bc7b2SThierry Reding  * gpiochip_add_data() must only be called after gpiolib initialization,
5702d93018fSRandy Dunlap  * i.e. after core_initcall().
571959bc7b2SThierry Reding  *
5728fc3ed3aSColton Lewis  * If gc->base is negative, this requests dynamic assignment of
573959bc7b2SThierry Reding  * a range of valid GPIOs.
574959bc7b2SThierry Reding  *
575959bc7b2SThierry Reding  * Returns:
576959bc7b2SThierry Reding  * A negative errno if the chip can't be registered, such as because the
5778fc3ed3aSColton Lewis  * gc->base is invalid or already associated with a different chip.
578959bc7b2SThierry Reding  * Otherwise it returns zero as a success code.
579959bc7b2SThierry Reding  */
580959bc7b2SThierry Reding #ifdef CONFIG_LOCKDEP
581a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) ({		\
58239c3fd58SAndrew Lunn 		static struct lock_class_key lock_key;	\
58339c3fd58SAndrew Lunn 		static struct lock_class_key request_key;	  \
584a0b66a73SLinus Walleij 		gpiochip_add_data_with_key(gc, data, &lock_key, \
58539c3fd58SAndrew Lunn 					   &request_key);	  \
586959bc7b2SThierry Reding 	})
5875f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) ({ \
5885f402bb1SAhmad Fatoum 		static struct lock_class_key lock_key;	\
5895f402bb1SAhmad Fatoum 		static struct lock_class_key request_key;	  \
5905f402bb1SAhmad Fatoum 		devm_gpiochip_add_data_with_key(dev, gc, data, &lock_key, \
5915f402bb1SAhmad Fatoum 					   &request_key);	  \
5925f402bb1SAhmad Fatoum 	})
593959bc7b2SThierry Reding #else
594a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) gpiochip_add_data_with_key(gc, data, NULL, NULL)
5955f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) \
5965f402bb1SAhmad Fatoum 	devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
597f310f2efSEnrico Weigelt #endif /* CONFIG_LOCKDEP */
598959bc7b2SThierry Reding 
599a0b66a73SLinus Walleij static inline int gpiochip_add(struct gpio_chip *gc)
600b08ea35aSLinus Walleij {
601a0b66a73SLinus Walleij 	return gpiochip_add_data(gc, NULL);
602b08ea35aSLinus Walleij }
603a0b66a73SLinus Walleij extern void gpiochip_remove(struct gpio_chip *gc);
6045f402bb1SAhmad Fatoum extern int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
6055f402bb1SAhmad Fatoum 					   struct lock_class_key *lock_key,
6065f402bb1SAhmad Fatoum 					   struct lock_class_key *request_key);
6070cf3292cSLaxman Dewangan 
60879a9becdSAlexandre Courbot extern struct gpio_chip *gpiochip_find(void *data,
609a0b66a73SLinus Walleij 			      int (*match)(struct gpio_chip *gc, void *data));
61079a9becdSAlexandre Courbot 
611a0b66a73SLinus Walleij bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset);
612a0b66a73SLinus Walleij int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset);
613a0b66a73SLinus Walleij void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset);
614a0b66a73SLinus Walleij void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset);
615a0b66a73SLinus Walleij void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset);
61679a9becdSAlexandre Courbot 
617704f0875SMarc Zyngier /* irq_data versions of the above */
618704f0875SMarc Zyngier int gpiochip_irq_reqres(struct irq_data *data);
619704f0875SMarc Zyngier void gpiochip_irq_relres(struct irq_data *data);
620704f0875SMarc Zyngier 
62136b78aaeSMarc Zyngier /* Paste this in your irq_chip structure  */
62236b78aaeSMarc Zyngier #define	GPIOCHIP_IRQ_RESOURCE_HELPERS					\
62336b78aaeSMarc Zyngier 		.irq_request_resources  = gpiochip_irq_reqres,		\
62436b78aaeSMarc Zyngier 		.irq_release_resources  = gpiochip_irq_relres
62536b78aaeSMarc Zyngier 
62636b78aaeSMarc Zyngier static inline void gpio_irq_chip_set_chip(struct gpio_irq_chip *girq,
62736b78aaeSMarc Zyngier 					  const struct irq_chip *chip)
62836b78aaeSMarc Zyngier {
62936b78aaeSMarc Zyngier 	/* Yes, dropping const is ugly, but it isn't like we have a choice */
63036b78aaeSMarc Zyngier 	girq->chip = (struct irq_chip *)chip;
63136b78aaeSMarc Zyngier }
63236b78aaeSMarc Zyngier 
633143b65d6SLinus Walleij /* Line status inquiry for drivers */
634a0b66a73SLinus Walleij bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset);
635a0b66a73SLinus Walleij bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset);
636143b65d6SLinus Walleij 
63705f479bfSCharles Keepax /* Sleep persistence inquiry for drivers */
638a0b66a73SLinus Walleij bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset);
639a0b66a73SLinus Walleij bool gpiochip_line_is_valid(const struct gpio_chip *gc, unsigned int offset);
64005f479bfSCharles Keepax 
641b08ea35aSLinus Walleij /* get driver data */
642a0b66a73SLinus Walleij void *gpiochip_get_data(struct gpio_chip *gc);
643b08ea35aSLinus Walleij 
6440f4630f3SLinus Walleij struct bgpio_pdata {
6450f4630f3SLinus Walleij 	const char *label;
6460f4630f3SLinus Walleij 	int base;
6470f4630f3SLinus Walleij 	int ngpio;
6480f4630f3SLinus Walleij };
6490f4630f3SLinus Walleij 
650fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
651fdd61a01SLinus Walleij 
652a0b66a73SLinus Walleij void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
653fdd61a01SLinus Walleij 					     unsigned int parent_hwirq,
654fdd61a01SLinus Walleij 					     unsigned int parent_type);
655a0b66a73SLinus Walleij void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
656fdd61a01SLinus Walleij 					      unsigned int parent_hwirq,
657fdd61a01SLinus Walleij 					      unsigned int parent_type);
658fdd61a01SLinus Walleij 
659fdd61a01SLinus Walleij #else
660fdd61a01SLinus Walleij 
661a0b66a73SLinus Walleij static inline void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
662fdd61a01SLinus Walleij 						    unsigned int parent_hwirq,
663fdd61a01SLinus Walleij 						    unsigned int parent_type)
664fdd61a01SLinus Walleij {
6659c6722d8SKevin Hao 	return NULL;
666fdd61a01SLinus Walleij }
667fdd61a01SLinus Walleij 
668a0b66a73SLinus Walleij static inline void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
669fdd61a01SLinus Walleij 						     unsigned int parent_hwirq,
670fdd61a01SLinus Walleij 						     unsigned int parent_type)
671fdd61a01SLinus Walleij {
6729c6722d8SKevin Hao 	return NULL;
673fdd61a01SLinus Walleij }
674fdd61a01SLinus Walleij 
675fdd61a01SLinus Walleij #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
676fdd61a01SLinus Walleij 
6770f4630f3SLinus Walleij int bgpio_init(struct gpio_chip *gc, struct device *dev,
6780f4630f3SLinus Walleij 	       unsigned long sz, void __iomem *dat, void __iomem *set,
6790f4630f3SLinus Walleij 	       void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
6800f4630f3SLinus Walleij 	       unsigned long flags);
6810f4630f3SLinus Walleij 
6820f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN		BIT(0)
6830f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_SET	BIT(1) /* reg_set is unreadable */
6840f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_DIR	BIT(2) /* reg_dir is unreadable */
6850f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN_BYTE_ORDER	BIT(3)
6860f4630f3SLinus Walleij #define BGPIOF_READ_OUTPUT_REG_SET	BIT(4) /* reg_set stores output value */
6870f4630f3SLinus Walleij #define BGPIOF_NO_OUTPUT		BIT(5) /* only input */
688d19d2de6SChuanhong Guo #define BGPIOF_NO_SET_ON_INPUT		BIT(6)
6890f4630f3SLinus Walleij 
6901b95b4ebSThierry Reding int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
6911b95b4ebSThierry Reding 		     irq_hw_number_t hwirq);
6921b95b4ebSThierry Reding void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
6931b95b4ebSThierry Reding 
694ef74f70eSBrian Masney int gpiochip_irq_domain_activate(struct irq_domain *domain,
695ef74f70eSBrian Masney 				 struct irq_data *data, bool reserve);
696ef74f70eSBrian Masney void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
697ef74f70eSBrian Masney 				    struct irq_data *data);
698ef74f70eSBrian Masney 
699a0b66a73SLinus Walleij bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
70064ff2c8eSStephen Boyd 				unsigned int offset);
70164ff2c8eSStephen Boyd 
7029c7d2469SÁlvaro Fernández Rojas #ifdef CONFIG_GPIOLIB_IRQCHIP
7036a45b0e2SMichael Walle int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
7046a45b0e2SMichael Walle 				struct irq_domain *domain);
7059c7d2469SÁlvaro Fernández Rojas #else
7069c7d2469SÁlvaro Fernández Rojas static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
7079c7d2469SÁlvaro Fernández Rojas 					      struct irq_domain *domain)
7089c7d2469SÁlvaro Fernández Rojas {
7099c7d2469SÁlvaro Fernández Rojas 	WARN_ON(1);
7109c7d2469SÁlvaro Fernández Rojas 	return -EINVAL;
7119c7d2469SÁlvaro Fernández Rojas }
7129c7d2469SÁlvaro Fernández Rojas #endif
7136a45b0e2SMichael Walle 
7148d091012SDouglas Anderson int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);
7158d091012SDouglas Anderson void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset);
7168d091012SDouglas Anderson int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
7172956b5d9SMika Westerberg 			    unsigned long config);
718c771c2f4SJonas Gorski 
719964cb341SLinus Walleij /**
720964cb341SLinus Walleij  * struct gpio_pin_range - pin range controlled by a gpio chip
721950d55f5SThierry Reding  * @node: list for maintaining set of pin ranges, used internally
722964cb341SLinus Walleij  * @pctldev: pinctrl device which handles corresponding pins
723964cb341SLinus Walleij  * @range: actual range of pins controlled by a gpio controller
724964cb341SLinus Walleij  */
725964cb341SLinus Walleij struct gpio_pin_range {
726964cb341SLinus Walleij 	struct list_head node;
727964cb341SLinus Walleij 	struct pinctrl_dev *pctldev;
728964cb341SLinus Walleij 	struct pinctrl_gpio_range range;
729964cb341SLinus Walleij };
730964cb341SLinus Walleij 
7319091373aSMasahiro Yamada #ifdef CONFIG_PINCTRL
7329091373aSMasahiro Yamada 
733a0b66a73SLinus Walleij int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
734964cb341SLinus Walleij 			   unsigned int gpio_offset, unsigned int pin_offset,
735964cb341SLinus Walleij 			   unsigned int npins);
736a0b66a73SLinus Walleij int gpiochip_add_pingroup_range(struct gpio_chip *gc,
737964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
738964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group);
739a0b66a73SLinus Walleij void gpiochip_remove_pin_ranges(struct gpio_chip *gc);
740964cb341SLinus Walleij 
741f310f2efSEnrico Weigelt #else /* ! CONFIG_PINCTRL */
742964cb341SLinus Walleij 
743964cb341SLinus Walleij static inline int
744a0b66a73SLinus Walleij gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
745964cb341SLinus Walleij 		       unsigned int gpio_offset, unsigned int pin_offset,
746964cb341SLinus Walleij 		       unsigned int npins)
747964cb341SLinus Walleij {
748964cb341SLinus Walleij 	return 0;
749964cb341SLinus Walleij }
750964cb341SLinus Walleij static inline int
751a0b66a73SLinus Walleij gpiochip_add_pingroup_range(struct gpio_chip *gc,
752964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
753964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group)
754964cb341SLinus Walleij {
755964cb341SLinus Walleij 	return 0;
756964cb341SLinus Walleij }
757964cb341SLinus Walleij 
758964cb341SLinus Walleij static inline void
759a0b66a73SLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *gc)
760964cb341SLinus Walleij {
761964cb341SLinus Walleij }
762964cb341SLinus Walleij 
763964cb341SLinus Walleij #endif /* CONFIG_PINCTRL */
764964cb341SLinus Walleij 
765a0b66a73SLinus Walleij struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
76606863620SBartosz Golaszewski 					    unsigned int hwnum,
76721abf103SLinus Walleij 					    const char *label,
7685923ea6cSLinus Walleij 					    enum gpio_lookup_flags lflags,
7695923ea6cSLinus Walleij 					    enum gpiod_flags dflags);
770f7d4ad98SGuenter Roeck void gpiochip_free_own_desc(struct gpio_desc *desc);
771f7d4ad98SGuenter Roeck 
772ae0755b5SLinus Walleij #ifdef CONFIG_GPIOLIB
773ae0755b5SLinus Walleij 
774c7663fa2SYueHaibing /* lock/unlock as IRQ */
775a0b66a73SLinus Walleij int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
776a0b66a73SLinus Walleij void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);
777c7663fa2SYueHaibing 
7789091373aSMasahiro Yamada 
7799091373aSMasahiro Yamada struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
7809091373aSMasahiro Yamada 
781bb1e88ccSAlexandre Courbot #else /* CONFIG_GPIOLIB */
782bb1e88ccSAlexandre Courbot 
783bb1e88ccSAlexandre Courbot static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
784bb1e88ccSAlexandre Courbot {
785bb1e88ccSAlexandre Courbot 	/* GPIO can never have been requested */
786bb1e88ccSAlexandre Courbot 	WARN_ON(1);
787bb1e88ccSAlexandre Courbot 	return ERR_PTR(-ENODEV);
788bb1e88ccSAlexandre Courbot }
789bb1e88ccSAlexandre Courbot 
790a0b66a73SLinus Walleij static inline int gpiochip_lock_as_irq(struct gpio_chip *gc,
791c7663fa2SYueHaibing 				       unsigned int offset)
792c7663fa2SYueHaibing {
793c7663fa2SYueHaibing 	WARN_ON(1);
794c7663fa2SYueHaibing 	return -EINVAL;
795c7663fa2SYueHaibing }
796c7663fa2SYueHaibing 
797a0b66a73SLinus Walleij static inline void gpiochip_unlock_as_irq(struct gpio_chip *gc,
798c7663fa2SYueHaibing 					  unsigned int offset)
799c7663fa2SYueHaibing {
800c7663fa2SYueHaibing 	WARN_ON(1);
801c7663fa2SYueHaibing }
802bb1e88ccSAlexandre Courbot #endif /* CONFIG_GPIOLIB */
803bb1e88ccSAlexandre Courbot 
80485ebb1a6SAndy Shevchenko #define for_each_gpiochip_node(dev, child)					\
80585ebb1a6SAndy Shevchenko 	device_for_each_child_node(dev, child)					\
80685ebb1a6SAndy Shevchenko 		if (!fwnode_property_present(child, "gpio-controller")) {} else
80785ebb1a6SAndy Shevchenko 
8080b19dde9SAndy Shevchenko static inline unsigned int gpiochip_node_count(struct device *dev)
8090b19dde9SAndy Shevchenko {
8100b19dde9SAndy Shevchenko 	struct fwnode_handle *child;
8110b19dde9SAndy Shevchenko 	unsigned int count = 0;
8120b19dde9SAndy Shevchenko 
8130b19dde9SAndy Shevchenko 	for_each_gpiochip_node(dev, child)
8140b19dde9SAndy Shevchenko 		count++;
8150b19dde9SAndy Shevchenko 
8160b19dde9SAndy Shevchenko 	return count;
8170b19dde9SAndy Shevchenko }
8180b19dde9SAndy Shevchenko 
819af47d803SAndy Shevchenko static inline struct fwnode_handle *gpiochip_node_get_first(struct device *dev)
820af47d803SAndy Shevchenko {
821af47d803SAndy Shevchenko 	struct fwnode_handle *fwnode;
822af47d803SAndy Shevchenko 
823af47d803SAndy Shevchenko 	for_each_gpiochip_node(dev, fwnode)
824af47d803SAndy Shevchenko 		return fwnode;
825af47d803SAndy Shevchenko 
826af47d803SAndy Shevchenko 	return NULL;
827af47d803SAndy Shevchenko }
828af47d803SAndy Shevchenko 
8299091373aSMasahiro Yamada #endif /* __LINUX_GPIO_DRIVER_H */
830