xref: /openbmc/linux/include/linux/gpio/driver.h (revision c771c2f4)
179a9becdSAlexandre Courbot #ifndef __LINUX_GPIO_DRIVER_H
279a9becdSAlexandre Courbot #define __LINUX_GPIO_DRIVER_H
379a9becdSAlexandre Courbot 
479a9becdSAlexandre Courbot #include <linux/types.h>
5c9a9972bSAlexandre Courbot #include <linux/module.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>
1179a9becdSAlexandre Courbot 
1279a9becdSAlexandre Courbot struct device;
1379a9becdSAlexandre Courbot struct gpio_desc;
14c9a9972bSAlexandre Courbot struct of_phandle_args;
15c9a9972bSAlexandre Courbot struct device_node;
16f3ed0b66SStephen Rothwell struct seq_file;
1779a9becdSAlexandre Courbot 
18bb1e88ccSAlexandre Courbot #ifdef CONFIG_GPIOLIB
19bb1e88ccSAlexandre Courbot 
2079a9becdSAlexandre Courbot /**
2179a9becdSAlexandre Courbot  * struct gpio_chip - abstract a GPIO controller
2279a9becdSAlexandre Courbot  * @label: for diagnostics
2379a9becdSAlexandre Courbot  * @dev: optional device providing the GPIOs
246a4b6b0aSJohan Hovold  * @cdev: class device used by sysfs interface (may be NULL)
2579a9becdSAlexandre Courbot  * @owner: helps prevent removal of modules exporting active GPIOs
2679a9becdSAlexandre Courbot  * @list: links gpio_chips together for traversal
2779a9becdSAlexandre Courbot  * @request: optional hook for chip-specific activation, such as
2879a9becdSAlexandre Courbot  *	enabling module power and clock; may sleep
2979a9becdSAlexandre Courbot  * @free: optional hook for chip-specific deactivation, such as
3079a9becdSAlexandre Courbot  *	disabling module power and clock; may sleep
3179a9becdSAlexandre Courbot  * @get_direction: returns direction for signal "offset", 0=out, 1=in,
3279a9becdSAlexandre Courbot  *	(same as GPIOF_DIR_XXX), or negative error
3379a9becdSAlexandre Courbot  * @direction_input: configures signal "offset" as input, or returns error
3479a9becdSAlexandre Courbot  * @direction_output: configures signal "offset" as output, or returns error
3579a9becdSAlexandre Courbot  * @get: returns value for signal "offset"; for output signals this
3679a9becdSAlexandre Courbot  *	returns either the value actually sensed, or zero
3779a9becdSAlexandre Courbot  * @set: assigns output value for signal "offset"
385f424243SRojhalat Ibrahim  * @set_multiple: assigns output values for multiple signals defined by "mask"
3979a9becdSAlexandre Courbot  * @set_debounce: optional hook for setting debounce time for specified gpio in
4079a9becdSAlexandre Courbot  *      interrupt triggered gpio chips
4179a9becdSAlexandre Courbot  * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
4279a9becdSAlexandre Courbot  *	implementation may not sleep
4379a9becdSAlexandre Courbot  * @dbg_show: optional routine to show contents in debugfs; default code
4479a9becdSAlexandre Courbot  *	will be used when this is omitted, but custom code can show extra
4579a9becdSAlexandre Courbot  *	state (such as pullup/pulldown configuration).
46af6c235dSLinus Walleij  * @base: identifies the first GPIO number handled by this chip;
47af6c235dSLinus Walleij  *	or, if negative during registration, requests dynamic ID allocation.
48af6c235dSLinus Walleij  *	DEPRECATION: providing anything non-negative and nailing the base
4930bb6fb3SGeert Uytterhoeven  *	offset of GPIO chips is deprecated. Please pass -1 as base to
50af6c235dSLinus Walleij  *	let gpiolib select the chip base in all possible cases. We want to
51af6c235dSLinus Walleij  *	get rid of the static GPIO number space in the long run.
5279a9becdSAlexandre Courbot  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
5379a9becdSAlexandre Courbot  *	handled is (base + ngpio - 1).
5479a9becdSAlexandre Courbot  * @desc: array of ngpio descriptors. Private.
5579a9becdSAlexandre Courbot  * @names: if set, must be an array of strings to use as alternative
5679a9becdSAlexandre Courbot  *      names for the GPIOs in this chip. Any entry in the array
5779a9becdSAlexandre Courbot  *      may be NULL if there is no alias for the GPIO, however the
5879a9becdSAlexandre Courbot  *      array must be @ngpio entries long.  A name can include a single printk
5979a9becdSAlexandre Courbot  *      format specifier for an unsigned int.  It is substituted by the actual
6079a9becdSAlexandre Courbot  *      number of the gpio.
619fb1f39eSLinus Walleij  * @can_sleep: flag must be set iff get()/set() methods sleep, as they
621c8732bbSLinus Walleij  *	must while accessing GPIO expander chips over I2C or SPI. This
631c8732bbSLinus Walleij  *	implies that if the chip supports IRQs, these IRQs need to be threaded
641c8732bbSLinus Walleij  *	as the chip access may sleep when e.g. reading out the IRQ status
651c8732bbSLinus Walleij  *	registers.
66295494afSOctavian Purdila  * @irq_not_threaded: flag must be set if @can_sleep is set but the
67295494afSOctavian Purdila  *	IRQs don't need to be threaded
6841d6bb4cSGrygorii Strashko  * @irqchip: GPIO IRQ chip impl, provided by GPIO driver
6941d6bb4cSGrygorii Strashko  * @irqdomain: Interrupt translation domain; responsible for mapping
7041d6bb4cSGrygorii Strashko  *	between GPIO hwirq number and linux irq number
7141d6bb4cSGrygorii Strashko  * @irq_base: first linux IRQ number assigned to GPIO IRQ chip (deprecated)
7241d6bb4cSGrygorii Strashko  * @irq_handler: the irq handler to use (often a predefined irq core function)
7341d6bb4cSGrygorii Strashko  *	for GPIO IRQs, provided by GPIO driver
7441d6bb4cSGrygorii Strashko  * @irq_default_type: default IRQ triggering type applied during GPIO driver
7541d6bb4cSGrygorii Strashko  *	initialization, provided by GPIO driver
7641d6bb4cSGrygorii Strashko  * @irq_parent: GPIO IRQ chip parent/bank linux irq number,
7741d6bb4cSGrygorii Strashko  *	provided by GPIO driver
7841d6bb4cSGrygorii Strashko  * @lock_key: per GPIO IRQ chip lockdep class
7979a9becdSAlexandre Courbot  *
8079a9becdSAlexandre Courbot  * A gpio_chip can help platforms abstract various sources of GPIOs so
8179a9becdSAlexandre Courbot  * they can all be accessed through a common programing interface.
8279a9becdSAlexandre Courbot  * Example sources would be SOC controllers, FPGAs, multifunction
8379a9becdSAlexandre Courbot  * chips, dedicated GPIO expanders, and so on.
8479a9becdSAlexandre Courbot  *
8579a9becdSAlexandre Courbot  * Each chip controls a number of signals, identified in method calls
8679a9becdSAlexandre Courbot  * by "offset" values in the range 0..(@ngpio - 1).  When those signals
8779a9becdSAlexandre Courbot  * are referenced through calls like gpio_get_value(gpio), the offset
8879a9becdSAlexandre Courbot  * is calculated by subtracting @base from the gpio number.
8979a9becdSAlexandre Courbot  */
9079a9becdSAlexandre Courbot struct gpio_chip {
9179a9becdSAlexandre Courbot 	const char		*label;
9279a9becdSAlexandre Courbot 	struct device		*dev;
936a4b6b0aSJohan Hovold 	struct device		*cdev;
9479a9becdSAlexandre Courbot 	struct module		*owner;
9579a9becdSAlexandre Courbot 	struct list_head        list;
9679a9becdSAlexandre Courbot 
9779a9becdSAlexandre Courbot 	int			(*request)(struct gpio_chip *chip,
9879a9becdSAlexandre Courbot 						unsigned offset);
9979a9becdSAlexandre Courbot 	void			(*free)(struct gpio_chip *chip,
10079a9becdSAlexandre Courbot 						unsigned offset);
10179a9becdSAlexandre Courbot 	int			(*get_direction)(struct gpio_chip *chip,
10279a9becdSAlexandre Courbot 						unsigned offset);
10379a9becdSAlexandre Courbot 	int			(*direction_input)(struct gpio_chip *chip,
10479a9becdSAlexandre Courbot 						unsigned offset);
10579a9becdSAlexandre Courbot 	int			(*direction_output)(struct gpio_chip *chip,
10679a9becdSAlexandre Courbot 						unsigned offset, int value);
10779a9becdSAlexandre Courbot 	int			(*get)(struct gpio_chip *chip,
10879a9becdSAlexandre Courbot 						unsigned offset);
10979a9becdSAlexandre Courbot 	void			(*set)(struct gpio_chip *chip,
11079a9becdSAlexandre Courbot 						unsigned offset, int value);
1115f424243SRojhalat Ibrahim 	void			(*set_multiple)(struct gpio_chip *chip,
1125f424243SRojhalat Ibrahim 						unsigned long *mask,
1135f424243SRojhalat Ibrahim 						unsigned long *bits);
11479a9becdSAlexandre Courbot 	int			(*set_debounce)(struct gpio_chip *chip,
11579a9becdSAlexandre Courbot 						unsigned offset,
11679a9becdSAlexandre Courbot 						unsigned debounce);
11779a9becdSAlexandre Courbot 
11879a9becdSAlexandre Courbot 	int			(*to_irq)(struct gpio_chip *chip,
11979a9becdSAlexandre Courbot 						unsigned offset);
12079a9becdSAlexandre Courbot 
12179a9becdSAlexandre Courbot 	void			(*dbg_show)(struct seq_file *s,
12279a9becdSAlexandre Courbot 						struct gpio_chip *chip);
12379a9becdSAlexandre Courbot 	int			base;
12479a9becdSAlexandre Courbot 	u16			ngpio;
12579a9becdSAlexandre Courbot 	struct gpio_desc	*desc;
12679a9becdSAlexandre Courbot 	const char		*const *names;
1279fb1f39eSLinus Walleij 	bool			can_sleep;
128295494afSOctavian Purdila 	bool			irq_not_threaded;
12979a9becdSAlexandre Courbot 
13014250520SLinus Walleij #ifdef CONFIG_GPIOLIB_IRQCHIP
13114250520SLinus Walleij 	/*
1327d75a871SPaul Bolle 	 * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip inside the gpiolib
13314250520SLinus Walleij 	 * to handle IRQs for most practical cases.
13414250520SLinus Walleij 	 */
13514250520SLinus Walleij 	struct irq_chip		*irqchip;
13614250520SLinus Walleij 	struct irq_domain	*irqdomain;
137c3626fdeSLinus Walleij 	unsigned int		irq_base;
13814250520SLinus Walleij 	irq_flow_handler_t	irq_handler;
13914250520SLinus Walleij 	unsigned int		irq_default_type;
14025e4fe92SDmitry Eremin-Solenikov 	int			irq_parent;
141a0a8bcf4SGrygorii Strashko 	struct lock_class_key	*lock_key;
14214250520SLinus Walleij #endif
14314250520SLinus Walleij 
14479a9becdSAlexandre Courbot #if defined(CONFIG_OF_GPIO)
14579a9becdSAlexandre Courbot 	/*
14679a9becdSAlexandre Courbot 	 * If CONFIG_OF is enabled, then all GPIO controllers described in the
14779a9becdSAlexandre Courbot 	 * device tree automatically may have an OF translation
14879a9becdSAlexandre Courbot 	 */
14979a9becdSAlexandre Courbot 	struct device_node *of_node;
15079a9becdSAlexandre Courbot 	int of_gpio_n_cells;
15179a9becdSAlexandre Courbot 	int (*of_xlate)(struct gpio_chip *gc,
15279a9becdSAlexandre Courbot 			const struct of_phandle_args *gpiospec, u32 *flags);
15379a9becdSAlexandre Courbot #endif
15479a9becdSAlexandre Courbot #ifdef CONFIG_PINCTRL
15579a9becdSAlexandre Courbot 	/*
15679a9becdSAlexandre Courbot 	 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
15779a9becdSAlexandre Courbot 	 * describe the actual pin range which they serve in an SoC. This
15879a9becdSAlexandre Courbot 	 * information would be used by pinctrl subsystem to configure
15979a9becdSAlexandre Courbot 	 * corresponding pins for gpio usage.
16079a9becdSAlexandre Courbot 	 */
16179a9becdSAlexandre Courbot 	struct list_head pin_ranges;
16279a9becdSAlexandre Courbot #endif
16379a9becdSAlexandre Courbot };
16479a9becdSAlexandre Courbot 
16579a9becdSAlexandre Courbot extern const char *gpiochip_is_requested(struct gpio_chip *chip,
16679a9becdSAlexandre Courbot 			unsigned offset);
16779a9becdSAlexandre Courbot 
16879a9becdSAlexandre Courbot /* add/remove chips */
16979a9becdSAlexandre Courbot extern int gpiochip_add(struct gpio_chip *chip);
170e1db1706Sabdoulaye berthe extern void gpiochip_remove(struct gpio_chip *chip);
17179a9becdSAlexandre Courbot extern struct gpio_chip *gpiochip_find(void *data,
17279a9becdSAlexandre Courbot 			      int (*match)(struct gpio_chip *chip, void *data));
17379a9becdSAlexandre Courbot 
17479a9becdSAlexandre Courbot /* lock/unlock as IRQ */
175e3a2e878SAlexandre Courbot int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset);
176e3a2e878SAlexandre Courbot void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset);
17779a9becdSAlexandre Courbot 
178bb1e88ccSAlexandre Courbot struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
179bb1e88ccSAlexandre Courbot 
18014250520SLinus Walleij #ifdef CONFIG_GPIOLIB_IRQCHIP
18114250520SLinus Walleij 
18214250520SLinus Walleij void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
18314250520SLinus Walleij 		struct irq_chip *irqchip,
18414250520SLinus Walleij 		int parent_irq,
18514250520SLinus Walleij 		irq_flow_handler_t parent_handler);
18614250520SLinus Walleij 
187a0a8bcf4SGrygorii Strashko int _gpiochip_irqchip_add(struct gpio_chip *gpiochip,
18814250520SLinus Walleij 			  struct irq_chip *irqchip,
18914250520SLinus Walleij 			  unsigned int first_irq,
19014250520SLinus Walleij 			  irq_flow_handler_t handler,
191a0a8bcf4SGrygorii Strashko 			  unsigned int type,
192a0a8bcf4SGrygorii Strashko 			  struct lock_class_key *lock_key);
193a0a8bcf4SGrygorii Strashko 
194a0a8bcf4SGrygorii Strashko #ifdef CONFIG_LOCKDEP
195a0a8bcf4SGrygorii Strashko #define gpiochip_irqchip_add(...)				\
196a0a8bcf4SGrygorii Strashko (								\
197a0a8bcf4SGrygorii Strashko 	({							\
198a0a8bcf4SGrygorii Strashko 		static struct lock_class_key _key;		\
199a0a8bcf4SGrygorii Strashko 		_gpiochip_irqchip_add(__VA_ARGS__, &_key);	\
200a0a8bcf4SGrygorii Strashko 	})							\
201a0a8bcf4SGrygorii Strashko )
202a0a8bcf4SGrygorii Strashko #else
203a0a8bcf4SGrygorii Strashko #define gpiochip_irqchip_add(...)				\
204a0a8bcf4SGrygorii Strashko 	_gpiochip_irqchip_add(__VA_ARGS__, NULL)
205a0a8bcf4SGrygorii Strashko #endif
20614250520SLinus Walleij 
2077d75a871SPaul Bolle #endif /* CONFIG_GPIOLIB_IRQCHIP */
20814250520SLinus Walleij 
209c771c2f4SJonas Gorski int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset);
210c771c2f4SJonas Gorski void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset);
211c771c2f4SJonas Gorski 
212964cb341SLinus Walleij #ifdef CONFIG_PINCTRL
213964cb341SLinus Walleij 
214964cb341SLinus Walleij /**
215964cb341SLinus Walleij  * struct gpio_pin_range - pin range controlled by a gpio chip
216964cb341SLinus Walleij  * @head: list for maintaining set of pin ranges, used internally
217964cb341SLinus Walleij  * @pctldev: pinctrl device which handles corresponding pins
218964cb341SLinus Walleij  * @range: actual range of pins controlled by a gpio controller
219964cb341SLinus Walleij  */
220964cb341SLinus Walleij 
221964cb341SLinus Walleij struct gpio_pin_range {
222964cb341SLinus Walleij 	struct list_head node;
223964cb341SLinus Walleij 	struct pinctrl_dev *pctldev;
224964cb341SLinus Walleij 	struct pinctrl_gpio_range range;
225964cb341SLinus Walleij };
226964cb341SLinus Walleij 
227964cb341SLinus Walleij int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
228964cb341SLinus Walleij 			   unsigned int gpio_offset, unsigned int pin_offset,
229964cb341SLinus Walleij 			   unsigned int npins);
230964cb341SLinus Walleij int gpiochip_add_pingroup_range(struct gpio_chip *chip,
231964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
232964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group);
233964cb341SLinus Walleij void gpiochip_remove_pin_ranges(struct gpio_chip *chip);
234964cb341SLinus Walleij 
235964cb341SLinus Walleij #else
236964cb341SLinus Walleij 
237964cb341SLinus Walleij static inline int
238964cb341SLinus Walleij gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
239964cb341SLinus Walleij 		       unsigned int gpio_offset, unsigned int pin_offset,
240964cb341SLinus Walleij 		       unsigned int npins)
241964cb341SLinus Walleij {
242964cb341SLinus Walleij 	return 0;
243964cb341SLinus Walleij }
244964cb341SLinus Walleij static inline int
245964cb341SLinus Walleij gpiochip_add_pingroup_range(struct gpio_chip *chip,
246964cb341SLinus Walleij 			struct pinctrl_dev *pctldev,
247964cb341SLinus Walleij 			unsigned int gpio_offset, const char *pin_group)
248964cb341SLinus Walleij {
249964cb341SLinus Walleij 	return 0;
250964cb341SLinus Walleij }
251964cb341SLinus Walleij 
252964cb341SLinus Walleij static inline void
253964cb341SLinus Walleij gpiochip_remove_pin_ranges(struct gpio_chip *chip)
254964cb341SLinus Walleij {
255964cb341SLinus Walleij }
256964cb341SLinus Walleij 
257964cb341SLinus Walleij #endif /* CONFIG_PINCTRL */
258964cb341SLinus Walleij 
259abdc08a3SAlexandre Courbot struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
260abdc08a3SAlexandre Courbot 					    const char *label);
261f7d4ad98SGuenter Roeck void gpiochip_free_own_desc(struct gpio_desc *desc);
262f7d4ad98SGuenter Roeck 
263bb1e88ccSAlexandre Courbot #else /* CONFIG_GPIOLIB */
264bb1e88ccSAlexandre Courbot 
265bb1e88ccSAlexandre Courbot static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
266bb1e88ccSAlexandre Courbot {
267bb1e88ccSAlexandre Courbot 	/* GPIO can never have been requested */
268bb1e88ccSAlexandre Courbot 	WARN_ON(1);
269bb1e88ccSAlexandre Courbot 	return ERR_PTR(-ENODEV);
270bb1e88ccSAlexandre Courbot }
271bb1e88ccSAlexandre Courbot 
272bb1e88ccSAlexandre Courbot #endif /* CONFIG_GPIOLIB */
273bb1e88ccSAlexandre Courbot 
27479a9becdSAlexandre Courbot #endif
275