xref: /linux-6.15/include/linux/gpio/machine.h (revision 08a149c4)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
20a6d3158SLinus Walleij #ifndef __LINUX_GPIO_MACHINE_H
30a6d3158SLinus Walleij #define __LINUX_GPIO_MACHINE_H
40a6d3158SLinus Walleij 
5b3ea074fSAlexandre Courbot #include <linux/types.h>
6b3ea074fSAlexandre Courbot 
70a6d3158SLinus Walleij enum gpio_lookup_flags {
80a6d3158SLinus Walleij 	GPIO_ACTIVE_HIGH		= (0 << 0),
90a6d3158SLinus Walleij 	GPIO_ACTIVE_LOW			= (1 << 0),
100a6d3158SLinus Walleij 	GPIO_OPEN_DRAIN			= (1 << 1),
110a6d3158SLinus Walleij 	GPIO_OPEN_SOURCE		= (1 << 2),
12e10f72bfSAndrew Jeffery 	GPIO_PERSISTENT			= (0 << 3),
13e10f72bfSAndrew Jeffery 	GPIO_TRANSITORY			= (1 << 3),
14d449991cSThomas Petazzoni 	GPIO_PULL_UP			= (1 << 4),
15d449991cSThomas Petazzoni 	GPIO_PULL_DOWN			= (1 << 5),
16*c269df8cSNuno Sá 	GPIO_PULL_DISABLE		= (1 << 6),
172d6c06f5SAndy Shevchenko 
182d6c06f5SAndy Shevchenko 	GPIO_LOOKUP_FLAGS_DEFAULT	= GPIO_ACTIVE_HIGH | GPIO_PERSISTENT,
190a6d3158SLinus Walleij };
200a6d3158SLinus Walleij 
210a6d3158SLinus Walleij /**
220a6d3158SLinus Walleij  * struct gpiod_lookup - lookup table
234c033b54SGeert Uytterhoeven  * @key: either the name of the chip the GPIO belongs to, or the GPIO line name
244c033b54SGeert Uytterhoeven  *       Note that GPIO line names are not guaranteed to be globally unique,
254c033b54SGeert Uytterhoeven  *       so this will use the first match found!
264c033b54SGeert Uytterhoeven  * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO, or
274c033b54SGeert Uytterhoeven  *              U16_MAX to indicate that @key is a GPIO line name
280a6d3158SLinus Walleij  * @con_id: name of the GPIO from the device's point of view
290a6d3158SLinus Walleij  * @idx: index of the GPIO in case several GPIOs share the same name
30fed7026aSAndy Shevchenko  * @flags: bitmask of gpio_lookup_flags GPIO_* values
310a6d3158SLinus Walleij  *
320a6d3158SLinus Walleij  * gpiod_lookup is a lookup table for associating GPIOs to specific devices and
330a6d3158SLinus Walleij  * functions using platform data.
340a6d3158SLinus Walleij  */
350a6d3158SLinus Walleij struct gpiod_lookup {
364c033b54SGeert Uytterhoeven 	const char *key;
370a6d3158SLinus Walleij 	u16 chip_hwnum;
380a6d3158SLinus Walleij 	const char *con_id;
390a6d3158SLinus Walleij 	unsigned int idx;
40fed7026aSAndy Shevchenko 	unsigned long flags;
410a6d3158SLinus Walleij };
420a6d3158SLinus Walleij 
430a6d3158SLinus Walleij struct gpiod_lookup_table {
440a6d3158SLinus Walleij 	struct list_head list;
450a6d3158SLinus Walleij 	const char *dev_id;
460a6d3158SLinus Walleij 	struct gpiod_lookup table[];
470a6d3158SLinus Walleij };
480a6d3158SLinus Walleij 
49a411e81eSBartosz Golaszewski /**
50a411e81eSBartosz Golaszewski  * struct gpiod_hog - GPIO line hog table
51a411e81eSBartosz Golaszewski  * @chip_label: name of the chip the GPIO belongs to
52a411e81eSBartosz Golaszewski  * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO
53a411e81eSBartosz Golaszewski  * @line_name: consumer name for the hogged line
54fed7026aSAndy Shevchenko  * @lflags: bitmask of gpio_lookup_flags GPIO_* values
55a411e81eSBartosz Golaszewski  * @dflags: GPIO flags used to specify the direction and value
56a411e81eSBartosz Golaszewski  */
57a411e81eSBartosz Golaszewski struct gpiod_hog {
58a411e81eSBartosz Golaszewski 	struct list_head list;
59a411e81eSBartosz Golaszewski 	const char *chip_label;
60a411e81eSBartosz Golaszewski 	u16 chip_hwnum;
61a411e81eSBartosz Golaszewski 	const char *line_name;
62fed7026aSAndy Shevchenko 	unsigned long lflags;
63a411e81eSBartosz Golaszewski 	int dflags;
64a411e81eSBartosz Golaszewski };
65a411e81eSBartosz Golaszewski 
660a6d3158SLinus Walleij /*
67813c2aeeSLinus Walleij  * Helper for lookup tables with just one single lookup for a device.
68813c2aeeSLinus Walleij  */
69813c2aeeSLinus Walleij #define GPIO_LOOKUP_SINGLE(_name, _dev_id, _key, _chip_hwnum, _con_id, _flags) \
70813c2aeeSLinus Walleij static struct gpiod_lookup_table _name = {				\
71813c2aeeSLinus Walleij 	.dev_id = _dev_id,						\
72813c2aeeSLinus Walleij 	.table = {							\
73813c2aeeSLinus Walleij 		GPIO_LOOKUP(_key, _chip_hwnum, _con_id, _flags),	\
74813c2aeeSLinus Walleij 		{},							\
75813c2aeeSLinus Walleij 	},								\
76813c2aeeSLinus Walleij }
77813c2aeeSLinus Walleij 
78813c2aeeSLinus Walleij /*
790a6d3158SLinus Walleij  * Simple definition of a single GPIO under a con_id
800a6d3158SLinus Walleij  */
814c033b54SGeert Uytterhoeven #define GPIO_LOOKUP(_key, _chip_hwnum, _con_id, _flags) \
824c033b54SGeert Uytterhoeven 	GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, 0, _flags)
830a6d3158SLinus Walleij 
840a6d3158SLinus Walleij /*
850a6d3158SLinus Walleij  * Use this macro if you need to have several GPIOs under the same con_id.
860a6d3158SLinus Walleij  * Each GPIO needs to use a different index and can be accessed using
870a6d3158SLinus Walleij  * gpiod_get_index()
880a6d3158SLinus Walleij  */
894c033b54SGeert Uytterhoeven #define GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, _idx, _flags)         \
90b2498cb8SAndy Shevchenko (struct gpiod_lookup) {                                                   \
914c033b54SGeert Uytterhoeven 	.key = _key,                                                      \
920a6d3158SLinus Walleij 	.chip_hwnum = _chip_hwnum,                                        \
930a6d3158SLinus Walleij 	.con_id = _con_id,                                                \
940a6d3158SLinus Walleij 	.idx = _idx,                                                      \
950a6d3158SLinus Walleij 	.flags = _flags,                                                  \
960a6d3158SLinus Walleij }
970a6d3158SLinus Walleij 
98a411e81eSBartosz Golaszewski /*
99a411e81eSBartosz Golaszewski  * Simple definition of a single GPIO hog in an array.
100a411e81eSBartosz Golaszewski  */
101a411e81eSBartosz Golaszewski #define GPIO_HOG(_chip_label, _chip_hwnum, _line_name, _lflags, _dflags)  \
102b2498cb8SAndy Shevchenko (struct gpiod_hog) {                                                      \
103a411e81eSBartosz Golaszewski 	.chip_label = _chip_label,                                        \
104a411e81eSBartosz Golaszewski 	.chip_hwnum = _chip_hwnum,                                        \
105a411e81eSBartosz Golaszewski 	.line_name = _line_name,                                          \
106a411e81eSBartosz Golaszewski 	.lflags = _lflags,                                                \
107a411e81eSBartosz Golaszewski 	.dflags = _dflags,                                                \
108a411e81eSBartosz Golaszewski }
109a411e81eSBartosz Golaszewski 
110020e0b1cSAnatolij Gustschin #ifdef CONFIG_GPIOLIB
1110a6d3158SLinus Walleij void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
1123946d187SDmitry Torokhov void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n);
113be9015abSShobhit Kumar void gpiod_remove_lookup_table(struct gpiod_lookup_table *table);
114a411e81eSBartosz Golaszewski void gpiod_add_hogs(struct gpiod_hog *hogs);
115dd61b292SBartosz Golaszewski void gpiod_remove_hogs(struct gpiod_hog *hogs);
116f310f2efSEnrico Weigelt #else /* ! CONFIG_GPIOLIB */
117020e0b1cSAnatolij Gustschin static inline
gpiod_add_lookup_table(struct gpiod_lookup_table * table)118020e0b1cSAnatolij Gustschin void gpiod_add_lookup_table(struct gpiod_lookup_table *table) {}
119020e0b1cSAnatolij Gustschin static inline
gpiod_add_lookup_tables(struct gpiod_lookup_table ** tables,size_t n)1203946d187SDmitry Torokhov void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) {}
1213946d187SDmitry Torokhov static inline
gpiod_remove_lookup_table(struct gpiod_lookup_table * table)122020e0b1cSAnatolij Gustschin void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) {}
gpiod_add_hogs(struct gpiod_hog * hogs)123a411e81eSBartosz Golaszewski static inline void gpiod_add_hogs(struct gpiod_hog *hogs) {}
gpiod_remove_hogs(struct gpiod_hog * hogs)124dd61b292SBartosz Golaszewski static inline void gpiod_remove_hogs(struct gpiod_hog *hogs) {}
125f310f2efSEnrico Weigelt #endif /* CONFIG_GPIOLIB */
1260a6d3158SLinus Walleij 
1270a6d3158SLinus Walleij #endif /* __LINUX_GPIO_MACHINE_H */
128