1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GPIO_MACHINE_H
3 #define __LINUX_GPIO_MACHINE_H
4
5 #include <linux/types.h>
6 #include <linux/list.h>
7
8 enum gpio_lookup_flags {
9 GPIO_ACTIVE_HIGH = (0 << 0),
10 GPIO_ACTIVE_LOW = (1 << 0),
11 GPIO_OPEN_DRAIN = (1 << 1),
12 GPIO_OPEN_SOURCE = (1 << 2),
13 GPIO_PERSISTENT = (0 << 3),
14 GPIO_TRANSITORY = (1 << 3),
15 GPIO_PULL_UP = (1 << 4),
16 GPIO_PULL_DOWN = (1 << 5),
17 GPIO_PULL_DISABLE = (1 << 6),
18
19 GPIO_LOOKUP_FLAGS_DEFAULT = GPIO_ACTIVE_HIGH | GPIO_PERSISTENT,
20 };
21
22 /**
23 * struct gpiod_lookup - lookup table
24 * @key: either the name of the chip the GPIO belongs to, or the GPIO line name
25 * Note that GPIO line names are not guaranteed to be globally unique,
26 * so this will use the first match found!
27 * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO, or
28 * U16_MAX to indicate that @key is a GPIO line name
29 * @con_id: name of the GPIO from the device's point of view
30 * @idx: index of the GPIO in case several GPIOs share the same name
31 * @flags: bitmask of gpio_lookup_flags GPIO_* values
32 *
33 * gpiod_lookup is a lookup table for associating GPIOs to specific devices and
34 * functions using platform data.
35 */
36 struct gpiod_lookup {
37 const char *key;
38 u16 chip_hwnum;
39 const char *con_id;
40 unsigned int idx;
41 unsigned long flags;
42 };
43
44 struct gpiod_lookup_table {
45 struct list_head list;
46 const char *dev_id;
47 struct gpiod_lookup table[];
48 };
49
50 /**
51 * struct gpiod_hog - GPIO line hog table
52 * @chip_label: name of the chip the GPIO belongs to
53 * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO
54 * @line_name: consumer name for the hogged line
55 * @lflags: bitmask of gpio_lookup_flags GPIO_* values
56 * @dflags: GPIO flags used to specify the direction and value
57 */
58 struct gpiod_hog {
59 struct list_head list;
60 const char *chip_label;
61 u16 chip_hwnum;
62 const char *line_name;
63 unsigned long lflags;
64 int dflags;
65 };
66
67 /*
68 * Helper for lookup tables with just one single lookup for a device.
69 */
70 #define GPIO_LOOKUP_SINGLE(_name, _dev_id, _key, _chip_hwnum, _con_id, _flags) \
71 static struct gpiod_lookup_table _name = { \
72 .dev_id = _dev_id, \
73 .table = { \
74 GPIO_LOOKUP(_key, _chip_hwnum, _con_id, _flags), \
75 {}, \
76 }, \
77 }
78
79 /*
80 * Simple definition of a single GPIO under a con_id
81 */
82 #define GPIO_LOOKUP(_key, _chip_hwnum, _con_id, _flags) \
83 GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, 0, _flags)
84
85 /*
86 * Use this macro if you need to have several GPIOs under the same con_id.
87 * Each GPIO needs to use a different index and can be accessed using
88 * gpiod_get_index()
89 */
90 #define GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, _idx, _flags) \
91 (struct gpiod_lookup) { \
92 .key = _key, \
93 .chip_hwnum = _chip_hwnum, \
94 .con_id = _con_id, \
95 .idx = _idx, \
96 .flags = _flags, \
97 }
98
99 /*
100 * Simple definition of a single GPIO hog in an array.
101 */
102 #define GPIO_HOG(_chip_label, _chip_hwnum, _line_name, _lflags, _dflags) \
103 (struct gpiod_hog) { \
104 .chip_label = _chip_label, \
105 .chip_hwnum = _chip_hwnum, \
106 .line_name = _line_name, \
107 .lflags = _lflags, \
108 .dflags = _dflags, \
109 }
110
111 #ifdef CONFIG_GPIOLIB
112 void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
113 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n);
114 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table);
115 void gpiod_add_hogs(struct gpiod_hog *hogs);
116 void gpiod_remove_hogs(struct gpiod_hog *hogs);
117 #else /* ! CONFIG_GPIOLIB */
118 static inline
gpiod_add_lookup_table(struct gpiod_lookup_table * table)119 void gpiod_add_lookup_table(struct gpiod_lookup_table *table) {}
120 static inline
gpiod_add_lookup_tables(struct gpiod_lookup_table ** tables,size_t n)121 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) {}
122 static inline
gpiod_remove_lookup_table(struct gpiod_lookup_table * table)123 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) {}
gpiod_add_hogs(struct gpiod_hog * hogs)124 static inline void gpiod_add_hogs(struct gpiod_hog *hogs) {}
gpiod_remove_hogs(struct gpiod_hog * hogs)125 static inline void gpiod_remove_hogs(struct gpiod_hog *hogs) {}
126 #endif /* CONFIG_GPIOLIB */
127
128 #endif /* __LINUX_GPIO_MACHINE_H */
129