xref: /openbmc/linux/include/linux/gpio/driver.h (revision 990f6756)
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>
679a9becdSAlexandre Courbot #include <linux/types.h>
714250520SLinus Walleij #include <linux/irq.h>
814250520SLinus Walleij #include <linux/irqchip/chained_irq.h>
914250520SLinus Walleij #include <linux/irqdomain.h>
10a0a8bcf4SGrygorii Strashko #include <linux/lockdep.h>
11964cb341SLinus Walleij #include <linux/pinctrl/pinctrl.h>
122956b5d9SMika Westerberg #include <linux/pinctrl/pinconf-generic.h>
1379a9becdSAlexandre Courbot 
1479a9becdSAlexandre Courbot struct gpio_desc;
15c9a9972bSAlexandre Courbot struct of_phandle_args;
16c9a9972bSAlexandre Courbot struct device_node;
17f3ed0b66SStephen Rothwell struct seq_file;
18ff2b1359SLinus Walleij struct gpio_device;
19d47529b2SPaul Gortmaker struct module;
2021abf103SLinus Walleij enum gpiod_flags;
215923ea6cSLinus Walleij enum gpio_lookup_flags;
2279a9becdSAlexandre Courbot 
23fdd61a01SLinus Walleij struct gpio_chip;
24fdd61a01SLinus Walleij 
259208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_IN	1
269208b1e7SMatti Vaittinen #define GPIO_LINE_DIRECTION_OUT	0
279208b1e7SMatti Vaittinen 
28c44eafd7SThierry Reding /**
29c44eafd7SThierry Reding  * struct gpio_irq_chip - GPIO interrupt controller
30c44eafd7SThierry Reding  */
31c44eafd7SThierry Reding struct gpio_irq_chip {
32c44eafd7SThierry Reding 	/**
33da80ff81SThierry Reding 	 * @chip:
34da80ff81SThierry Reding 	 *
35da80ff81SThierry Reding 	 * GPIO IRQ chip implementation, provided by GPIO driver.
36da80ff81SThierry Reding 	 */
37da80ff81SThierry Reding 	struct irq_chip *chip;
38da80ff81SThierry Reding 
39da80ff81SThierry Reding 	/**
40f0fbe7bcSThierry Reding 	 * @domain:
41f0fbe7bcSThierry Reding 	 *
42f0fbe7bcSThierry Reding 	 * Interrupt translation domain; responsible for mapping between GPIO
43f0fbe7bcSThierry Reding 	 * hwirq number and Linux IRQ number.
44f0fbe7bcSThierry Reding 	 */
45f0fbe7bcSThierry Reding 	struct irq_domain *domain;
46f0fbe7bcSThierry Reding 
47f0fbe7bcSThierry Reding 	/**
48c44eafd7SThierry Reding 	 * @domain_ops:
49c44eafd7SThierry Reding 	 *
50c44eafd7SThierry Reding 	 * Table of interrupt domain operations for this IRQ chip.
51c44eafd7SThierry Reding 	 */
52c44eafd7SThierry Reding 	const struct irq_domain_ops *domain_ops;
53c44eafd7SThierry Reding 
54fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
55fdd61a01SLinus Walleij 	/**
56fdd61a01SLinus Walleij 	 * @fwnode:
57fdd61a01SLinus Walleij 	 *
58fdd61a01SLinus Walleij 	 * Firmware node corresponding to this gpiochip/irqchip, necessary
59fdd61a01SLinus Walleij 	 * for hierarchical irqdomain support.
60fdd61a01SLinus Walleij 	 */
61fdd61a01SLinus Walleij 	struct fwnode_handle *fwnode;
62fdd61a01SLinus Walleij 
63fdd61a01SLinus Walleij 	/**
64fdd61a01SLinus Walleij 	 * @parent_domain:
65fdd61a01SLinus Walleij 	 *
66fdd61a01SLinus Walleij 	 * If non-NULL, will be set as the parent of this GPIO interrupt
67fdd61a01SLinus Walleij 	 * controller's IRQ domain to establish a hierarchical interrupt
68fdd61a01SLinus Walleij 	 * domain. The presence of this will activate the hierarchical
69fdd61a01SLinus Walleij 	 * interrupt support.
70fdd61a01SLinus Walleij 	 */
71fdd61a01SLinus Walleij 	struct irq_domain *parent_domain;
72fdd61a01SLinus Walleij 
73fdd61a01SLinus Walleij 	/**
74fdd61a01SLinus Walleij 	 * @child_to_parent_hwirq:
75fdd61a01SLinus Walleij 	 *
76fdd61a01SLinus Walleij 	 * This callback translates a child hardware IRQ offset to a parent
77fdd61a01SLinus Walleij 	 * hardware IRQ offset on a hierarchical interrupt chip. The child
78fdd61a01SLinus Walleij 	 * hardware IRQs correspond to the GPIO index 0..ngpio-1 (see the
79fdd61a01SLinus Walleij 	 * ngpio field of struct gpio_chip) and the corresponding parent
80fdd61a01SLinus Walleij 	 * hardware IRQ and type (such as IRQ_TYPE_*) shall be returned by
81fdd61a01SLinus Walleij 	 * the driver. The driver can calculate this from an offset or using
82fdd61a01SLinus Walleij 	 * a lookup table or whatever method is best for this chip. Return
83fdd61a01SLinus Walleij 	 * 0 on successful translation in the driver.
84fdd61a01SLinus Walleij 	 *
85fdd61a01SLinus Walleij 	 * If some ranges of hardware IRQs do not have a corresponding parent
86fdd61a01SLinus Walleij 	 * HWIRQ, return -EINVAL, but also make sure to fill in @valid_mask and
87fdd61a01SLinus Walleij 	 * @need_valid_mask to make these GPIO lines unavailable for
88fdd61a01SLinus Walleij 	 * translation.
89fdd61a01SLinus Walleij 	 */
90a0b66a73SLinus Walleij 	int (*child_to_parent_hwirq)(struct gpio_chip *gc,
91fdd61a01SLinus Walleij 				     unsigned int child_hwirq,
92fdd61a01SLinus Walleij 				     unsigned int child_type,
93fdd61a01SLinus Walleij 				     unsigned int *parent_hwirq,
94fdd61a01SLinus Walleij 				     unsigned int *parent_type);
95fdd61a01SLinus Walleij 
96fdd61a01SLinus Walleij 	/**
9724258761SKevin Hao 	 * @populate_parent_alloc_arg :
98fdd61a01SLinus Walleij 	 *
9924258761SKevin Hao 	 * This optional callback allocates and populates the specific struct
10024258761SKevin Hao 	 * for the parent's IRQ domain. If this is not specified, then
101fdd61a01SLinus Walleij 	 * &gpiochip_populate_parent_fwspec_twocell will be used. A four-cell
102fdd61a01SLinus Walleij 	 * variant named &gpiochip_populate_parent_fwspec_fourcell is also
103fdd61a01SLinus Walleij 	 * available.
104fdd61a01SLinus Walleij 	 */
105a0b66a73SLinus Walleij 	void *(*populate_parent_alloc_arg)(struct gpio_chip *gc,
106fdd61a01SLinus Walleij 				       unsigned int parent_hwirq,
107fdd61a01SLinus Walleij 				       unsigned int parent_type);
108fdd61a01SLinus Walleij 
109fdd61a01SLinus Walleij 	/**
110fdd61a01SLinus Walleij 	 * @child_offset_to_irq:
111fdd61a01SLinus Walleij 	 *
112fdd61a01SLinus Walleij 	 * This optional callback is used to translate the child's GPIO line
113fdd61a01SLinus Walleij 	 * offset on the GPIO chip to an IRQ number for the GPIO to_irq()
114fdd61a01SLinus Walleij 	 * callback. If this is not specified, then a default callback will be
115fdd61a01SLinus Walleij 	 * provided that returns the line offset.
116fdd61a01SLinus Walleij 	 */
117a0b66a73SLinus Walleij 	unsigned int (*child_offset_to_irq)(struct gpio_chip *gc,
118fdd61a01SLinus Walleij 					    unsigned int pin);
119fdd61a01SLinus Walleij 
120fdd61a01SLinus Walleij 	/**
121fdd61a01SLinus Walleij 	 * @child_irq_domain_ops:
122fdd61a01SLinus Walleij 	 *
123fdd61a01SLinus Walleij 	 * The IRQ domain operations that will be used for this GPIO IRQ
124fdd61a01SLinus Walleij 	 * chip. If no operations are provided, then default callbacks will
125fdd61a01SLinus Walleij 	 * be populated to setup the IRQ hierarchy. Some drivers need to
126fdd61a01SLinus Walleij 	 * supply their own translate function.
127fdd61a01SLinus Walleij 	 */
128fdd61a01SLinus Walleij 	struct irq_domain_ops child_irq_domain_ops;
129fdd61a01SLinus Walleij #endif
130fdd61a01SLinus Walleij 
131c44eafd7SThierry Reding 	/**
132c7a0aa59SThierry Reding 	 * @handler:
133c7a0aa59SThierry Reding 	 *
134c7a0aa59SThierry Reding 	 * The IRQ handler to use (often a predefined IRQ core function) for
135c7a0aa59SThierry Reding 	 * GPIO IRQs, provided by GPIO driver.
136c7a0aa59SThierry Reding 	 */
137c7a0aa59SThierry Reding 	irq_flow_handler_t handler;
138c7a0aa59SThierry Reding 
139c7a0aa59SThierry Reding 	/**
1403634eeb0SThierry Reding 	 * @default_type:
1413634eeb0SThierry Reding 	 *
1423634eeb0SThierry Reding 	 * Default IRQ triggering type applied during GPIO driver
1433634eeb0SThierry Reding 	 * initialization, provided by GPIO driver.
1443634eeb0SThierry Reding 	 */
1453634eeb0SThierry Reding 	unsigned int default_type;
1463634eeb0SThierry Reding 
1473634eeb0SThierry Reding 	/**
148ca9df053SThierry Reding 	 * @lock_key:
149ca9df053SThierry Reding 	 *
15002ad0437SRandy Dunlap 	 * Per GPIO IRQ chip lockdep class for IRQ lock.
151ca9df053SThierry Reding 	 */
152ca9df053SThierry Reding 	struct lock_class_key *lock_key;
15302ad0437SRandy Dunlap 
15402ad0437SRandy Dunlap 	/**
15502ad0437SRandy Dunlap 	 * @request_key:
15602ad0437SRandy Dunlap 	 *
15702ad0437SRandy Dunlap 	 * Per GPIO IRQ chip lockdep class for IRQ request.
15802ad0437SRandy Dunlap 	 */
15939c3fd58SAndrew Lunn 	struct lock_class_key *request_key;
160ca9df053SThierry Reding 
161ca9df053SThierry Reding 	/**
162c44eafd7SThierry Reding 	 * @parent_handler:
163c44eafd7SThierry Reding 	 *
164c44eafd7SThierry Reding 	 * The interrupt handler for the GPIO chip's parent interrupts, may be
165c44eafd7SThierry Reding 	 * NULL if the parent interrupts are nested rather than cascaded.
166c44eafd7SThierry Reding 	 */
167c44eafd7SThierry Reding 	irq_flow_handler_t parent_handler;
168c44eafd7SThierry Reding 
169c44eafd7SThierry Reding 	/**
170c44eafd7SThierry Reding 	 * @parent_handler_data:
171cfe6807dSMarc Zyngier 	 * @parent_handler_data_array:
172c44eafd7SThierry Reding 	 *
173c44eafd7SThierry Reding 	 * Data associated, and passed to, the handler for the parent
174cfe6807dSMarc Zyngier 	 * interrupt. Can either be a single pointer if @per_parent_data
175cfe6807dSMarc Zyngier 	 * is false, or an array of @num_parents pointers otherwise.  If
176cfe6807dSMarc Zyngier 	 * @per_parent_data is true, @parent_handler_data_array cannot be
177cfe6807dSMarc Zyngier 	 * NULL.
178c44eafd7SThierry Reding 	 */
179cfe6807dSMarc Zyngier 	union {
180c44eafd7SThierry Reding 		void *parent_handler_data;
181cfe6807dSMarc Zyngier 		void **parent_handler_data_array;
182cfe6807dSMarc Zyngier 	};
18339e5f096SThierry Reding 
18439e5f096SThierry Reding 	/**
18539e5f096SThierry Reding 	 * @num_parents:
18639e5f096SThierry Reding 	 *
18739e5f096SThierry Reding 	 * The number of interrupt parents of a GPIO chip.
18839e5f096SThierry Reding 	 */
18939e5f096SThierry Reding 	unsigned int num_parents;
19039e5f096SThierry Reding 
19139e5f096SThierry Reding 	/**
19239e5f096SThierry Reding 	 * @parents:
19339e5f096SThierry Reding 	 *
19439e5f096SThierry Reding 	 * A list of interrupt parents of a GPIO chip. This is owned by the
19539e5f096SThierry Reding 	 * driver, so the core will only reference this list, not modify it.
19639e5f096SThierry Reding 	 */
19739e5f096SThierry Reding 	unsigned int *parents;
198dc6bafeeSThierry Reding 
199dc6bafeeSThierry Reding 	/**
200e0d89728SThierry Reding 	 * @map:
201e0d89728SThierry Reding 	 *
202e0d89728SThierry Reding 	 * A list of interrupt parents for each line of a GPIO chip.
203e0d89728SThierry Reding 	 */
204e0d89728SThierry Reding 	unsigned int *map;
205e0d89728SThierry Reding 
206e0d89728SThierry Reding 	/**
20760ed54caSThierry Reding 	 * @threaded:
208dc6bafeeSThierry Reding 	 *
20960ed54caSThierry Reding 	 * True if set the interrupt handling uses nested threads.
210dc6bafeeSThierry Reding 	 */
21160ed54caSThierry Reding 	bool threaded;
212dc7b0387SThierry Reding 
213dc7b0387SThierry Reding 	/**
214cfe6807dSMarc Zyngier 	 * @per_parent_data:
215cfe6807dSMarc Zyngier 	 *
216cfe6807dSMarc Zyngier 	 * True if parent_handler_data_array describes a @num_parents
217cfe6807dSMarc Zyngier 	 * sized array to be used as parent data.
218cfe6807dSMarc Zyngier 	 */
219cfe6807dSMarc Zyngier 	bool per_parent_data;
220cfe6807dSMarc Zyngier 
221cfe6807dSMarc Zyngier 	/**
2229411e3aaSAndy Shevchenko 	 * @init_hw: optional routine to initialize hardware before
2239411e3aaSAndy Shevchenko 	 * an IRQ chip will be added. This is quite useful when
2249411e3aaSAndy Shevchenko 	 * a particular driver wants to clear IRQ related registers
2259411e3aaSAndy Shevchenko 	 * in order to avoid undesired events.
2269411e3aaSAndy Shevchenko 	 */
227a0b66a73SLinus Walleij 	int (*init_hw)(struct gpio_chip *gc);
2289411e3aaSAndy Shevchenko 
2299411e3aaSAndy Shevchenko 	/**
2305fbe5b58SLinus Walleij 	 * @init_valid_mask: optional routine to initialize @valid_mask, to be
2315fbe5b58SLinus Walleij 	 * used if not all GPIO lines are valid interrupts. Sometimes some
2325fbe5b58SLinus Walleij 	 * lines just cannot fire interrupts, and this routine, when defined,
2335fbe5b58SLinus Walleij 	 * is passed a bitmap in "valid_mask" and it will have ngpios
2345fbe5b58SLinus Walleij 	 * bits from 0..(ngpios-1) set to "1" as in valid. The callback can
2355fbe5b58SLinus Walleij 	 * then directly set some bits to "0" if they cannot be used for
2365fbe5b58SLinus Walleij 	 * interrupts.
237dc7b0387SThierry Reding 	 */
238a0b66a73SLinus Walleij 	void (*init_valid_mask)(struct gpio_chip *gc,
2395fbe5b58SLinus Walleij 				unsigned long *valid_mask,
2405fbe5b58SLinus Walleij 				unsigned int ngpios);
241dc7b0387SThierry Reding 
242dc7b0387SThierry Reding 	/**
243dc7b0387SThierry Reding 	 * @valid_mask:
244dc7b0387SThierry Reding 	 *
2452d93018fSRandy Dunlap 	 * If not %NULL, holds bitmask of GPIOs which are valid to be included
246dc7b0387SThierry Reding 	 * in IRQ domain of the chip.
247dc7b0387SThierry Reding 	 */
248dc7b0387SThierry Reding 	unsigned long *valid_mask;
2498302cf58SThierry Reding 
2508302cf58SThierry Reding 	/**
2518302cf58SThierry Reding 	 * @first:
2528302cf58SThierry Reding 	 *
2538302cf58SThierry Reding 	 * Required for static IRQ allocation. If set, irq_domain_add_simple()
2548302cf58SThierry Reding 	 * will allocate and map all IRQs during initialization.
2558302cf58SThierry Reding 	 */
2568302cf58SThierry Reding 	unsigned int first;
257461c1a7dSHans Verkuil 
258461c1a7dSHans Verkuil 	/**
259461c1a7dSHans Verkuil 	 * @irq_enable:
260461c1a7dSHans Verkuil 	 *
261461c1a7dSHans Verkuil 	 * Store old irq_chip irq_enable callback
262461c1a7dSHans Verkuil 	 */
263461c1a7dSHans Verkuil 	void		(*irq_enable)(struct irq_data *data);
264461c1a7dSHans Verkuil 
265461c1a7dSHans Verkuil 	/**
266461c1a7dSHans Verkuil 	 * @irq_disable:
267461c1a7dSHans Verkuil 	 *
268461c1a7dSHans Verkuil 	 * Store old irq_chip irq_disable callback
269461c1a7dSHans Verkuil 	 */
270461c1a7dSHans Verkuil 	void		(*irq_disable)(struct irq_data *data);
271a8173820SMaulik Shah 	/**
272a8173820SMaulik Shah 	 * @irq_unmask:
273a8173820SMaulik Shah 	 *
274a8173820SMaulik Shah 	 * Store old irq_chip irq_unmask callback
275a8173820SMaulik Shah 	 */
276a8173820SMaulik Shah 	void		(*irq_unmask)(struct irq_data *data);
277a8173820SMaulik Shah 
278a8173820SMaulik Shah 	/**
279a8173820SMaulik Shah 	 * @irq_mask:
280a8173820SMaulik Shah 	 *
281a8173820SMaulik Shah 	 * Store old irq_chip irq_mask callback
282a8173820SMaulik Shah 	 */
283a8173820SMaulik Shah 	void		(*irq_mask)(struct irq_data *data);
284c44eafd7SThierry Reding };
285c44eafd7SThierry Reding 
28679a9becdSAlexandre Courbot /**
28779a9becdSAlexandre Courbot  * struct gpio_chip - abstract a GPIO controller
288df4878e9SLinus Walleij  * @label: a functional name for the GPIO device, such as a part
289df4878e9SLinus Walleij  *	number or the name of the SoC IP-block implementing it.
290ff2b1359SLinus Walleij  * @gpiodev: the internal state holder, opaque struct
29158383c78SLinus Walleij  * @parent: optional parent device providing the GPIOs
292*990f6756SBartosz Golaszewski  * @fwnode: optional fwnode providing this controller's properties
29379a9becdSAlexandre Courbot  * @owner: helps prevent removal of modules exporting active GPIOs
29479a9becdSAlexandre Courbot  * @request: optional hook for chip-specific activation, such as
29579a9becdSAlexandre Courbot  *	enabling module power and clock; may sleep
29679a9becdSAlexandre Courbot  * @free: optional hook for chip-specific deactivation, such as
29779a9becdSAlexandre Courbot  *	disabling module power and clock; may sleep
29879a9becdSAlexandre Courbot  * @get_direction: returns direction for signal "offset", 0=out, 1=in,
29936b52154SDouglas Anderson  *	(same as GPIO_LINE_DIRECTION_OUT / GPIO_LINE_DIRECTION_IN),
30036b52154SDouglas Anderson  *	or negative error. It is recommended to always implement this
30136b52154SDouglas Anderson  *	function, even on input-only or output-only gpio chips.
30279a9becdSAlexandre Courbot  * @direction_input: configures signal "offset" as input, or returns error
303e48d194dSLinus Walleij  *	This can be omitted on input-only or output-only gpio chips.
30479a9becdSAlexandre Courbot  * @direction_output: configures signal "offset" as output, or returns error
305e48d194dSLinus Walleij  *	This can be omitted on input-only or output-only gpio chips.
30660befd2eSVladimir Zapolskiy  * @get: returns value for signal "offset", 0=low, 1=high, or negative error
307eec1d566SLukas Wunner  * @get_multiple: reads values for multiple signals defined by "mask" and
308eec1d566SLukas Wunner  *	stores them in "bits", returns 0 on success or negative error
30979a9becdSAlexandre Courbot  * @set: assigns output value for signal "offset"
3105f424243SRojhalat Ibrahim  * @set_multiple: assigns output values for multiple signals defined by "mask"
3112956b5d9SMika Westerberg  * @set_config: optional hook for all kinds of settings. Uses the same
3122956b5d9SMika Westerberg  *	packed config format as generic pinconf.
31379a9becdSAlexandre Courbot  * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
31479a9becdSAlexandre Courbot  *	implementation may not sleep
31579a9becdSAlexandre Courbot  * @dbg_show: optional routine to show contents in debugfs; default code
31679a9becdSAlexandre Courbot  *	will be used when this is omitted, but custom code can show extra
31779a9becdSAlexandre Courbot  *	state (such as pullup/pulldown configuration).
318f99d479bSGeert Uytterhoeven  * @init_valid_mask: optional routine to initialize @valid_mask, to be used if
319f99d479bSGeert Uytterhoeven  *	not all GPIOs are valid.
320b056ca1cSAndy Shevchenko  * @add_pin_ranges: optional routine to initialize pin ranges, to be used when
321b056ca1cSAndy Shevchenko  *	requires special mapping of the pins that provides GPIO functionality.
322b056ca1cSAndy Shevchenko  *	It is called after adding GPIO chip and before adding IRQ chip.
323af6c235dSLinus Walleij  * @base: identifies the first GPIO number handled by this chip;
324af6c235dSLinus Walleij  *	or, if negative during registration, requests dynamic ID allocation.
325af6c235dSLinus Walleij  *	DEPRECATION: providing anything non-negative and nailing the base
32630bb6fb3SGeert Uytterhoeven  *	offset of GPIO chips is deprecated. Please pass -1 as base to
327af6c235dSLinus Walleij  *	let gpiolib select the chip base in all possible cases. We want to
328af6c235dSLinus Walleij  *	get rid of the static GPIO number space in the long run.
32979a9becdSAlexandre Courbot  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
33079a9becdSAlexandre Courbot  *	handled is (base + ngpio - 1).
3314e804c39SSergio Paracuellos  * @offset: when multiple gpio chips belong to the same device this
3324e804c39SSergio Paracuellos  *	can be used as offset within the device so friendly names can
3334e804c39SSergio Paracuellos  *	be properly assigned.
33479a9becdSAlexandre Courbot  * @names: if set, must be an array of strings to use as alternative
33579a9becdSAlexandre Courbot  *      names for the GPIOs in this chip. Any entry in the array
33679a9becdSAlexandre Courbot  *      may be NULL if there is no alias for the GPIO, however the
33779a9becdSAlexandre Courbot  *      array must be @ngpio entries long.  A name can include a single printk
33879a9becdSAlexandre Courbot  *      format specifier for an unsigned int.  It is substituted by the actual
33979a9becdSAlexandre Courbot  *      number of the gpio.
3409fb1f39eSLinus Walleij  * @can_sleep: flag must be set iff get()/set() methods sleep, as they
3411c8732bbSLinus Walleij  *	must while accessing GPIO expander chips over I2C or SPI. This
3421c8732bbSLinus Walleij  *	implies that if the chip supports IRQs, these IRQs need to be threaded
3431c8732bbSLinus Walleij  *	as the chip access may sleep when e.g. reading out the IRQ status
3441c8732bbSLinus Walleij  *	registers.
3450f4630f3SLinus Walleij  * @read_reg: reader function for generic GPIO
3460f4630f3SLinus Walleij  * @write_reg: writer function for generic GPIO
34724efd94bSLinus Walleij  * @be_bits: if the generic GPIO has big endian bit order (bit 31 is representing
34824efd94bSLinus Walleij  *	line 0, bit 30 is line 1 ... bit 0 is line 31) this is set to true by the
34924efd94bSLinus Walleij  *	generic GPIO core. It is for internal housekeeping only.
3500f4630f3SLinus Walleij  * @reg_dat: data (in) register for generic GPIO
3510f4630f3SLinus Walleij  * @reg_set: output set register (out=high) for generic GPIO
35208bcd3edSAnthony Best  * @reg_clr: output clear register (out=low) for generic GPIO
353f69e00bdSLinus Walleij  * @reg_dir_out: direction out setting register for generic GPIO
354f69e00bdSLinus Walleij  * @reg_dir_in: direction in setting register for generic GPIO
355f69e00bdSLinus Walleij  * @bgpio_dir_unreadable: indicates that the direction register(s) cannot
356f69e00bdSLinus Walleij  *	be read and we need to rely on out internal state tracking.
3570f4630f3SLinus Walleij  * @bgpio_bits: number of register bits used for a generic GPIO i.e.
3580f4630f3SLinus Walleij  *	<register width> * 8
3590f4630f3SLinus Walleij  * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep
3600f4630f3SLinus Walleij  *	shadowed and real data registers writes together.
3610f4630f3SLinus Walleij  * @bgpio_data:	shadowed data register for generic GPIO to clear/set bits
3620f4630f3SLinus Walleij  *	safely.
3630f4630f3SLinus Walleij  * @bgpio_dir: shadowed direction register for generic GPIO to clear/set
364f69e00bdSLinus Walleij  *	direction safely. A "1" in this word means the line is set as
365f69e00bdSLinus Walleij  *	output.
36679a9becdSAlexandre Courbot  *
36779a9becdSAlexandre Courbot  * A gpio_chip can help platforms abstract various sources of GPIOs so
3682d93018fSRandy Dunlap  * they can all be accessed through a common programming interface.
36979a9becdSAlexandre Courbot  * Example sources would be SOC controllers, FPGAs, multifunction
37079a9becdSAlexandre Courbot  * chips, dedicated GPIO expanders, and so on.
37179a9becdSAlexandre Courbot  *
37279a9becdSAlexandre Courbot  * Each chip controls a number of signals, identified in method calls
37379a9becdSAlexandre Courbot  * by "offset" values in the range 0..(@ngpio - 1).  When those signals
37479a9becdSAlexandre Courbot  * are referenced through calls like gpio_get_value(gpio), the offset
37579a9becdSAlexandre Courbot  * is calculated by subtracting @base from the gpio number.
37679a9becdSAlexandre Courbot  */
37779a9becdSAlexandre Courbot struct gpio_chip {
37879a9becdSAlexandre Courbot 	const char		*label;
379ff2b1359SLinus Walleij 	struct gpio_device	*gpiodev;
38058383c78SLinus Walleij 	struct device		*parent;
381*990f6756SBartosz Golaszewski 	struct fwnode_handle	*fwnode;
38279a9becdSAlexandre Courbot 	struct module		*owner;
38379a9becdSAlexandre Courbot 
384a0b66a73SLinus Walleij 	int			(*request)(struct gpio_chip *gc,
3858d091012SDouglas Anderson 						unsigned int offset);
386a0b66a73SLinus Walleij 	void			(*free)(struct gpio_chip *gc,
3878d091012SDouglas Anderson 						unsigned int offset);
388a0b66a73SLinus Walleij 	int			(*get_direction)(struct gpio_chip *gc,
3898d091012SDouglas Anderson 						unsigned int offset);
390a0b66a73SLinus Walleij 	int			(*direction_input)(struct gpio_chip *gc,
3918d091012SDouglas Anderson 						unsigned int offset);
392a0b66a73SLinus Walleij 	int			(*direction_output)(struct gpio_chip *gc,
3938d091012SDouglas Anderson 						unsigned int offset, int value);
394a0b66a73SLinus Walleij 	int			(*get)(struct gpio_chip *gc,
3958d091012SDouglas Anderson 						unsigned int offset);
396a0b66a73SLinus Walleij 	int			(*get_multiple)(struct gpio_chip *gc,
397eec1d566SLukas Wunner 						unsigned long *mask,
398eec1d566SLukas Wunner 						unsigned long *bits);
399a0b66a73SLinus Walleij 	void			(*set)(struct gpio_chip *gc,
4008d091012SDouglas Anderson 						unsigned int offset, int value);
401a0b66a73SLinus Walleij 	void			(*set_multiple)(struct gpio_chip *gc,
4025f424243SRojhalat Ibrahim 						unsigned long *mask,
4035f424243SRojhalat Ibrahim 						unsigned long *bits);
404a0b66a73SLinus Walleij 	int			(*set_config)(struct gpio_chip *gc,
4058d091012SDouglas Anderson 					      unsigned int offset,
4062956b5d9SMika Westerberg 					      unsigned long config);
407a0b66a73SLinus Walleij 	int			(*to_irq)(struct gpio_chip *gc,
4088d091012SDouglas Anderson 						unsigned int offset);
40979a9becdSAlexandre Courbot 
41079a9becdSAlexandre Courbot 	void			(*dbg_show)(struct seq_file *s,
411a0b66a73SLinus Walleij 						struct gpio_chip *gc);
412f8ec92a9SRicardo Ribalda Delgado 
413a0b66a73SLinus Walleij 	int			(*init_valid_mask)(struct gpio_chip *gc,
414c9fc5affSLinus Walleij 						   unsigned long *valid_mask,
415c9fc5affSLinus Walleij 						   unsigned int ngpios);
416f8ec92a9SRicardo Ribalda Delgado 
417a0b66a73SLinus Walleij 	int			(*add_pin_ranges)(struct gpio_chip *gc);
418b056ca1cSAndy Shevchenko 
41979a9becdSAlexandre Courbot 	int			base;
42079a9becdSAlexandre Courbot 	u16			ngpio;
4214e804c39SSergio Paracuellos 	u16			offset;
42279a9becdSAlexandre Courbot 	const char		*const *names;
4239fb1f39eSLinus Walleij 	bool			can_sleep;
42479a9becdSAlexandre Courbot 
4250f4630f3SLinus Walleij #if IS_ENABLED(CONFIG_GPIO_GENERIC)
4260f4630f3SLinus Walleij 	unsigned long (*read_reg)(void __iomem *reg);
4270f4630f3SLinus Walleij 	void (*write_reg)(void __iomem *reg, unsigned long data);
42824efd94bSLinus Walleij 	bool be_bits;
4290f4630f3SLinus Walleij 	void __iomem *reg_dat;
4300f4630f3SLinus Walleij 	void __iomem *reg_set;
4310f4630f3SLinus Walleij 	void __iomem *reg_clr;
432f69e00bdSLinus Walleij 	void __iomem *reg_dir_out;
433f69e00bdSLinus Walleij 	void __iomem *reg_dir_in;
434f69e00bdSLinus Walleij 	bool bgpio_dir_unreadable;
4350f4630f3SLinus Walleij 	int bgpio_bits;
4360f4630f3SLinus Walleij 	spinlock_t bgpio_lock;
4370f4630f3SLinus Walleij 	unsigned long bgpio_data;
4380f4630f3SLinus Walleij 	unsigned long bgpio_dir;
439f310f2efSEnrico Weigelt #endif /* CONFIG_GPIO_GENERIC */
4400f4630f3SLinus Walleij 
44114250520SLinus Walleij #ifdef CONFIG_GPIOLIB_IRQCHIP
44214250520SLinus Walleij 	/*
4437d75a871SPaul Bolle 	 * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
44414250520SLinus Walleij 	 * to handle IRQs for most practical cases.
44514250520SLinus Walleij 	 */
446c44eafd7SThierry Reding 
447c44eafd7SThierry Reding 	/**
448c44eafd7SThierry Reding 	 * @irq:
449c44eafd7SThierry Reding 	 *
450c44eafd7SThierry Reding 	 * Integrates interrupt chip functionality with the GPIO chip. Can be
451c44eafd7SThierry Reding 	 * used to handle IRQs for most practical cases.
452c44eafd7SThierry Reding 	 */
453c44eafd7SThierry Reding 	struct gpio_irq_chip irq;
454f310f2efSEnrico Weigelt #endif /* CONFIG_GPIOLIB_IRQCHIP */
45514250520SLinus Walleij 
456726cb3baSStephen Boyd 	/**
457726cb3baSStephen Boyd 	 * @valid_mask:
458726cb3baSStephen Boyd 	 *
4592d93018fSRandy Dunlap 	 * If not %NULL, holds bitmask of GPIOs which are valid to be used
460726cb3baSStephen Boyd 	 * from the chip.
461726cb3baSStephen Boyd 	 */
462726cb3baSStephen Boyd 	unsigned long *valid_mask;
463726cb3baSStephen Boyd 
46479a9becdSAlexandre Courbot #if defined(CONFIG_OF_GPIO)
46579a9becdSAlexandre Courbot 	/*
4662d93018fSRandy Dunlap 	 * If CONFIG_OF_GPIO is enabled, then all GPIO controllers described in
4672d93018fSRandy Dunlap 	 * the device tree automatically may have an OF translation
46879a9becdSAlexandre Courbot 	 */
46967049c50SThierry Reding 
47067049c50SThierry Reding 	/**
47167049c50SThierry Reding 	 * @of_node:
47267049c50SThierry Reding 	 *
47367049c50SThierry Reding 	 * Pointer to a device tree node representing this GPIO controller.
47467049c50SThierry Reding 	 */
47579a9becdSAlexandre Courbot 	struct device_node *of_node;
47667049c50SThierry Reding 
47767049c50SThierry Reding 	/**
47867049c50SThierry Reding 	 * @of_gpio_n_cells:
47967049c50SThierry Reding 	 *
48067049c50SThierry Reding 	 * Number of cells used to form the GPIO specifier.
48167049c50SThierry Reding 	 */
482e3b445d7SThierry Reding 	unsigned int of_gpio_n_cells;
48367049c50SThierry Reding 
48467049c50SThierry Reding 	/**
48567049c50SThierry Reding 	 * @of_xlate:
48667049c50SThierry Reding 	 *
48767049c50SThierry Reding 	 * Callback to translate a device tree GPIO specifier into a chip-
48867049c50SThierry Reding 	 * relative GPIO number and flags.
48967049c50SThierry Reding 	 */
49079a9becdSAlexandre Courbot 	int (*of_xlate)(struct gpio_chip *gc,
49179a9becdSAlexandre Courbot 			const struct of_phandle_args *gpiospec, u32 *flags);
492f310f2efSEnrico Weigelt #endif /* CONFIG_OF_GPIO */
49379a9becdSAlexandre Courbot };
49479a9becdSAlexandre Courbot 
495a0b66a73SLinus Walleij extern const char *gpiochip_is_requested(struct gpio_chip *gc,
4968d091012SDouglas Anderson 			unsigned int offset);
49779a9becdSAlexandre Courbot 
498b3337eb2SAndy Shevchenko /**
499b3337eb2SAndy Shevchenko  * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range
500b3337eb2SAndy Shevchenko  * @chip:	the chip to query
501b3337eb2SAndy Shevchenko  * @i:		loop variable
502b3337eb2SAndy Shevchenko  * @base:	first GPIO in the range
503b3337eb2SAndy Shevchenko  * @size:	amount of GPIOs to check starting from @base
504b3337eb2SAndy Shevchenko  * @label:	label of current GPIO
505b3337eb2SAndy Shevchenko  */
506b3337eb2SAndy Shevchenko #define for_each_requested_gpio_in_range(chip, i, base, size, label)			\
507b3337eb2SAndy Shevchenko 	for (i = 0; i < size; i++)							\
508b3337eb2SAndy Shevchenko 		if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
509b3337eb2SAndy Shevchenko 
510b3337eb2SAndy Shevchenko /* Iterates over all requested GPIO of the given @chip */
511b3337eb2SAndy Shevchenko #define for_each_requested_gpio(chip, i, label)						\
512b3337eb2SAndy Shevchenko 	for_each_requested_gpio_in_range(chip, i, 0, chip->ngpio, label)
513b3337eb2SAndy Shevchenko 
51479a9becdSAlexandre Courbot /* add/remove chips */
515a0b66a73SLinus Walleij extern int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
51639c3fd58SAndrew Lunn 				      struct lock_class_key *lock_key,
51739c3fd58SAndrew Lunn 				      struct lock_class_key *request_key);
518959bc7b2SThierry Reding 
519959bc7b2SThierry Reding /**
520959bc7b2SThierry Reding  * gpiochip_add_data() - register a gpio_chip
5218fc3ed3aSColton Lewis  * @gc: the chip to register, with gc->base initialized
522959bc7b2SThierry Reding  * @data: driver-private data associated with this chip
523959bc7b2SThierry Reding  *
524959bc7b2SThierry Reding  * Context: potentially before irqs will work
525959bc7b2SThierry Reding  *
526959bc7b2SThierry Reding  * When gpiochip_add_data() is called very early during boot, so that GPIOs
5278fc3ed3aSColton Lewis  * can be freely used, the gc->parent device must be registered before
528959bc7b2SThierry Reding  * the gpio framework's arch_initcall().  Otherwise sysfs initialization
529959bc7b2SThierry Reding  * for GPIOs will fail rudely.
530959bc7b2SThierry Reding  *
531959bc7b2SThierry Reding  * gpiochip_add_data() must only be called after gpiolib initialization,
5322d93018fSRandy Dunlap  * i.e. after core_initcall().
533959bc7b2SThierry Reding  *
5348fc3ed3aSColton Lewis  * If gc->base is negative, this requests dynamic assignment of
535959bc7b2SThierry Reding  * a range of valid GPIOs.
536959bc7b2SThierry Reding  *
537959bc7b2SThierry Reding  * Returns:
538959bc7b2SThierry Reding  * A negative errno if the chip can't be registered, such as because the
5398fc3ed3aSColton Lewis  * gc->base is invalid or already associated with a different chip.
540959bc7b2SThierry Reding  * Otherwise it returns zero as a success code.
541959bc7b2SThierry Reding  */
542959bc7b2SThierry Reding #ifdef CONFIG_LOCKDEP
543a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) ({		\
54439c3fd58SAndrew Lunn 		static struct lock_class_key lock_key;	\
54539c3fd58SAndrew Lunn 		static struct lock_class_key request_key;	  \
546a0b66a73SLinus Walleij 		gpiochip_add_data_with_key(gc, data, &lock_key, \
54739c3fd58SAndrew Lunn 					   &request_key);	  \
548959bc7b2SThierry Reding 	})
5495f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) ({ \
5505f402bb1SAhmad Fatoum 		static struct lock_class_key lock_key;	\
5515f402bb1SAhmad Fatoum 		static struct lock_class_key request_key;	  \
5525f402bb1SAhmad Fatoum 		devm_gpiochip_add_data_with_key(dev, gc, data, &lock_key, \
5535f402bb1SAhmad Fatoum 					   &request_key);	  \
5545f402bb1SAhmad Fatoum 	})
555959bc7b2SThierry Reding #else
556a0b66a73SLinus Walleij #define gpiochip_add_data(gc, data) gpiochip_add_data_with_key(gc, data, NULL, NULL)
5575f402bb1SAhmad Fatoum #define devm_gpiochip_add_data(dev, gc, data) \
5585f402bb1SAhmad Fatoum 	devm_gpiochip_add_data_with_key(dev, gc, data, NULL, NULL)
559f310f2efSEnrico Weigelt #endif /* CONFIG_LOCKDEP */
560959bc7b2SThierry Reding 
561a0b66a73SLinus Walleij static inline int gpiochip_add(struct gpio_chip *gc)
562b08ea35aSLinus Walleij {
563a0b66a73SLinus Walleij 	return gpiochip_add_data(gc, NULL);
564b08ea35aSLinus Walleij }
565a0b66a73SLinus Walleij extern void gpiochip_remove(struct gpio_chip *gc);
5665f402bb1SAhmad Fatoum extern int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
5675f402bb1SAhmad Fatoum 					   struct lock_class_key *lock_key,
5685f402bb1SAhmad Fatoum 					   struct lock_class_key *request_key);
5690cf3292cSLaxman Dewangan 
57079a9becdSAlexandre Courbot extern struct gpio_chip *gpiochip_find(void *data,
571a0b66a73SLinus Walleij 			      int (*match)(struct gpio_chip *gc, void *data));
57279a9becdSAlexandre Courbot 
573a0b66a73SLinus Walleij bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset);
574a0b66a73SLinus Walleij int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset);
575a0b66a73SLinus Walleij void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset);
576a0b66a73SLinus Walleij void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset);
577a0b66a73SLinus Walleij void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset);
57879a9becdSAlexandre Courbot 
579143b65d6SLinus Walleij /* Line status inquiry for drivers */
580a0b66a73SLinus Walleij bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset);
581a0b66a73SLinus Walleij bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset);
582143b65d6SLinus Walleij 
58305f479bfSCharles Keepax /* Sleep persistence inquiry for drivers */
584a0b66a73SLinus Walleij bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset);
585a0b66a73SLinus Walleij bool gpiochip_line_is_valid(const struct gpio_chip *gc, unsigned int offset);
58605f479bfSCharles Keepax 
587b08ea35aSLinus Walleij /* get driver data */
588a0b66a73SLinus Walleij void *gpiochip_get_data(struct gpio_chip *gc);
589b08ea35aSLinus Walleij 
5900f4630f3SLinus Walleij struct bgpio_pdata {
5910f4630f3SLinus Walleij 	const char *label;
5920f4630f3SLinus Walleij 	int base;
5930f4630f3SLinus Walleij 	int ngpio;
5940f4630f3SLinus Walleij };
5950f4630f3SLinus Walleij 
596fdd61a01SLinus Walleij #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
597fdd61a01SLinus Walleij 
598a0b66a73SLinus Walleij void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
599fdd61a01SLinus Walleij 					     unsigned int parent_hwirq,
600fdd61a01SLinus Walleij 					     unsigned int parent_type);
601a0b66a73SLinus Walleij void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
602fdd61a01SLinus Walleij 					      unsigned int parent_hwirq,
603fdd61a01SLinus Walleij 					      unsigned int parent_type);
604fdd61a01SLinus Walleij 
605fdd61a01SLinus Walleij #else
606fdd61a01SLinus Walleij 
607a0b66a73SLinus Walleij static inline void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc,
608fdd61a01SLinus Walleij 						    unsigned int parent_hwirq,
609fdd61a01SLinus Walleij 						    unsigned int parent_type)
610fdd61a01SLinus Walleij {
6119c6722d8SKevin Hao 	return NULL;
612fdd61a01SLinus Walleij }
613fdd61a01SLinus Walleij 
614a0b66a73SLinus Walleij static inline void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc,
615fdd61a01SLinus Walleij 						     unsigned int parent_hwirq,
616fdd61a01SLinus Walleij 						     unsigned int parent_type)
617fdd61a01SLinus Walleij {
6189c6722d8SKevin Hao 	return NULL;
619fdd61a01SLinus Walleij }
620fdd61a01SLinus Walleij 
621fdd61a01SLinus Walleij #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
622fdd61a01SLinus Walleij 
6230f4630f3SLinus Walleij int bgpio_init(struct gpio_chip *gc, struct device *dev,
6240f4630f3SLinus Walleij 	       unsigned long sz, void __iomem *dat, void __iomem *set,
6250f4630f3SLinus Walleij 	       void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
6260f4630f3SLinus Walleij 	       unsigned long flags);
6270f4630f3SLinus Walleij 
6280f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN		BIT(0)
6290f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_SET	BIT(1) /* reg_set is unreadable */
6300f4630f3SLinus Walleij #define BGPIOF_UNREADABLE_REG_DIR	BIT(2) /* reg_dir is unreadable */
6310f4630f3SLinus Walleij #define BGPIOF_BIG_ENDIAN_BYTE_ORDER	BIT(3)
6320f4630f3SLinus Walleij #define BGPIOF_READ_OUTPUT_REG_SET	BIT(4) /* reg_set stores output value */
6330f4630f3SLinus Walleij #define BGPIOF_NO_OUTPUT		BIT(5) /* only input */
634d19d2de6SChuanhong Guo #define BGPIOF_NO_SET_ON_INPUT		BIT(6)
6350f4630f3SLinus Walleij 
6361b95b4ebSThierry Reding int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
6371b95b4ebSThierry Reding 		     irq_hw_number_t hwirq);
6381b95b4ebSThierry Reding void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq);
6391b95b4ebSThierry Reding 
640ef74f70eSBrian Masney int gpiochip_irq_domain_activate(struct irq_domain *domain,
641ef74f70eSBrian Masney 				 struct irq_data *data, bool reserve);
642ef74f70eSBrian Masney void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
643ef74f70eSBrian Masney 				    struct irq_data *data);
644ef74f70eSBrian Masney 
645a0b66a73SLinus Walleij bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc,
64664ff2c8eSStephen Boyd 				unsigned int offset);
64764ff2c8eSStephen Boyd 
6489c7d2469SÁlvaro Fernández Rojas #ifdef CONFIG_GPIOLIB_IRQCHIP
6496a45b0e2SMichael Walle int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
6506a45b0e2SMichael Walle 				struct irq_domain *domain);
6519c7d2469SÁlvaro Fernández Rojas #else
6529c7d2469SÁlvaro Fernández Rojas static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
6539c7d2469SÁlvaro Fernández Rojas 					      struct irq_domain *domain)
6549c7d2469SÁlvaro Fernández Rojas {
6559c7d2469SÁlvaro Fernández Rojas 	WARN_ON(1);
6569c7d2469SÁlvaro Fernández Rojas 	return -EINVAL;
6579c7d2469SÁlvaro Fernández Rojas }
6589c7d2469SÁlvaro Fernández Rojas #endif
6596a45b0e2SMichael Walle 
6608d091012SDouglas Anderson int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);
6618d091012SDouglas Anderson void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset);
6628d091012SDouglas Anderson int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
6632956b5d9SMika Westerberg 			    unsigned long config);
664c771c2f4SJonas Gorski 
665964cb341SLinus Walleij /**
666964cb341SLinus Walleij  * struct gpio_pin_range - pin range controlled by a gpio chip
667950d55f5SThierry Reding  * @node: list for maintaining set of pin ranges, used internally
668964cb341SLinus Walleij  * @pctldev: pinctrl device which handles corresponding pins
669964cb341SLinus Walleij  * @range: actual range of pins controlled by a gpio controller
670964cb341SLinus Walleij  */
671964cb341SLinus Walleij struct gpio_pin_range {
672964cb341SLinus Walleij 	struct list_head node;
673964cb341SLinus Walleij 	struct pinctrl_dev *pctldev;
674964cb341SLinus Walleij 	struct pinctrl_gpio_range range;
675964cb341SLinus Walleij };
676964cb341SLinus Walleij 
6779091373aSMasahiro Yamada #ifdef CONFIG_PINCTRL
6789091373aSMasahiro Yamada 
679a0b66a73SLinus Walleij int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
680964cb341SLinus Walleij 			   unsigned int gpio_offset, unsigned int pin_offset,
681964cb341SLinus Walleij 			   unsigned int npins);
682a0b66a73SLinus Walleij int gpiochip_add_pingroup_range(struct gpio_chip *gc,
683964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
684964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group);
685a0b66a73SLinus Walleij void gpiochip_remove_pin_ranges(struct gpio_chip *gc);
686964cb341SLinus Walleij 
687f310f2efSEnrico Weigelt #else /* ! CONFIG_PINCTRL */
688964cb341SLinus Walleij 
689964cb341SLinus Walleij static inline int
690a0b66a73SLinus Walleij gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name,
691964cb341SLinus Walleij 		       unsigned int gpio_offset, unsigned int pin_offset,
692964cb341SLinus Walleij 		       unsigned int npins)
693964cb341SLinus Walleij {
694964cb341SLinus Walleij 	return 0;
695964cb341SLinus Walleij }
696964cb341SLinus Walleij static inline int
697a0b66a73SLinus Walleij gpiochip_add_pingroup_range(struct gpio_chip *gc,
698964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
699964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group)
700964cb341SLinus Walleij {
701964cb341SLinus Walleij 	return 0;
702964cb341SLinus Walleij }
703964cb341SLinus Walleij 
704964cb341SLinus Walleij static inline void
705a0b66a73SLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *gc)
706964cb341SLinus Walleij {
707964cb341SLinus Walleij }
708964cb341SLinus Walleij 
709964cb341SLinus Walleij #endif /* CONFIG_PINCTRL */
710964cb341SLinus Walleij 
711a0b66a73SLinus Walleij struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc,
71206863620SBartosz Golaszewski 					    unsigned int hwnum,
71321abf103SLinus Walleij 					    const char *label,
7145923ea6cSLinus Walleij 					    enum gpio_lookup_flags lflags,
7155923ea6cSLinus Walleij 					    enum gpiod_flags dflags);
716f7d4ad98SGuenter Roeck void gpiochip_free_own_desc(struct gpio_desc *desc);
717f7d4ad98SGuenter Roeck 
718ae0755b5SLinus Walleij #ifdef CONFIG_GPIOLIB
719ae0755b5SLinus Walleij 
720c7663fa2SYueHaibing /* lock/unlock as IRQ */
721a0b66a73SLinus Walleij int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
722a0b66a73SLinus Walleij void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);
723c7663fa2SYueHaibing 
7249091373aSMasahiro Yamada 
7259091373aSMasahiro Yamada struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
7269091373aSMasahiro Yamada 
727bb1e88ccSAlexandre Courbot #else /* CONFIG_GPIOLIB */
728bb1e88ccSAlexandre Courbot 
729bb1e88ccSAlexandre Courbot static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
730bb1e88ccSAlexandre Courbot {
731bb1e88ccSAlexandre Courbot 	/* GPIO can never have been requested */
732bb1e88ccSAlexandre Courbot 	WARN_ON(1);
733bb1e88ccSAlexandre Courbot 	return ERR_PTR(-ENODEV);
734bb1e88ccSAlexandre Courbot }
735bb1e88ccSAlexandre Courbot 
736a0b66a73SLinus Walleij static inline int gpiochip_lock_as_irq(struct gpio_chip *gc,
737c7663fa2SYueHaibing 				       unsigned int offset)
738c7663fa2SYueHaibing {
739c7663fa2SYueHaibing 	WARN_ON(1);
740c7663fa2SYueHaibing 	return -EINVAL;
741c7663fa2SYueHaibing }
742c7663fa2SYueHaibing 
743a0b66a73SLinus Walleij static inline void gpiochip_unlock_as_irq(struct gpio_chip *gc,
744c7663fa2SYueHaibing 					  unsigned int offset)
745c7663fa2SYueHaibing {
746c7663fa2SYueHaibing 	WARN_ON(1);
747c7663fa2SYueHaibing }
748bb1e88ccSAlexandre Courbot #endif /* CONFIG_GPIOLIB */
749bb1e88ccSAlexandre Courbot 
7509091373aSMasahiro Yamada #endif /* __LINUX_GPIO_DRIVER_H */
751