xref: /openbmc/linux/include/linux/gpio/driver.h (revision 930beb5a)
1 #ifndef __LINUX_GPIO_DRIVER_H
2 #define __LINUX_GPIO_DRIVER_H
3 
4 #include <linux/types.h>
5 #include <linux/module.h>
6 
7 struct device;
8 struct gpio_desc;
9 struct of_phandle_args;
10 struct device_node;
11 struct seq_file;
12 
13 /**
14  * struct gpio_chip - abstract a GPIO controller
15  * @label: for diagnostics
16  * @dev: optional device providing the GPIOs
17  * @owner: helps prevent removal of modules exporting active GPIOs
18  * @list: links gpio_chips together for traversal
19  * @request: optional hook for chip-specific activation, such as
20  *	enabling module power and clock; may sleep
21  * @free: optional hook for chip-specific deactivation, such as
22  *	disabling module power and clock; may sleep
23  * @get_direction: returns direction for signal "offset", 0=out, 1=in,
24  *	(same as GPIOF_DIR_XXX), or negative error
25  * @direction_input: configures signal "offset" as input, or returns error
26  * @direction_output: configures signal "offset" as output, or returns error
27  * @get: returns value for signal "offset"; for output signals this
28  *	returns either the value actually sensed, or zero
29  * @set: assigns output value for signal "offset"
30  * @set_debounce: optional hook for setting debounce time for specified gpio in
31  *      interrupt triggered gpio chips
32  * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
33  *	implementation may not sleep
34  * @dbg_show: optional routine to show contents in debugfs; default code
35  *	will be used when this is omitted, but custom code can show extra
36  *	state (such as pullup/pulldown configuration).
37  * @base: identifies the first GPIO number handled by this chip; or, if
38  *	negative during registration, requests dynamic ID allocation.
39  * @ngpio: the number of GPIOs handled by this controller; the last GPIO
40  *	handled is (base + ngpio - 1).
41  * @desc: array of ngpio descriptors. Private.
42  * @can_sleep: flag must be set iff get()/set() methods sleep, as they
43  *	must while accessing GPIO expander chips over I2C or SPI
44  * @names: if set, must be an array of strings to use as alternative
45  *      names for the GPIOs in this chip. Any entry in the array
46  *      may be NULL if there is no alias for the GPIO, however the
47  *      array must be @ngpio entries long.  A name can include a single printk
48  *      format specifier for an unsigned int.  It is substituted by the actual
49  *      number of the gpio.
50  *
51  * A gpio_chip can help platforms abstract various sources of GPIOs so
52  * they can all be accessed through a common programing interface.
53  * Example sources would be SOC controllers, FPGAs, multifunction
54  * chips, dedicated GPIO expanders, and so on.
55  *
56  * Each chip controls a number of signals, identified in method calls
57  * by "offset" values in the range 0..(@ngpio - 1).  When those signals
58  * are referenced through calls like gpio_get_value(gpio), the offset
59  * is calculated by subtracting @base from the gpio number.
60  */
61 struct gpio_chip {
62 	const char		*label;
63 	struct device		*dev;
64 	struct module		*owner;
65 	struct list_head        list;
66 
67 	int			(*request)(struct gpio_chip *chip,
68 						unsigned offset);
69 	void			(*free)(struct gpio_chip *chip,
70 						unsigned offset);
71 	int			(*get_direction)(struct gpio_chip *chip,
72 						unsigned offset);
73 	int			(*direction_input)(struct gpio_chip *chip,
74 						unsigned offset);
75 	int			(*direction_output)(struct gpio_chip *chip,
76 						unsigned offset, int value);
77 	int			(*get)(struct gpio_chip *chip,
78 						unsigned offset);
79 	void			(*set)(struct gpio_chip *chip,
80 						unsigned offset, int value);
81 	int			(*set_debounce)(struct gpio_chip *chip,
82 						unsigned offset,
83 						unsigned debounce);
84 
85 	int			(*to_irq)(struct gpio_chip *chip,
86 						unsigned offset);
87 
88 	void			(*dbg_show)(struct seq_file *s,
89 						struct gpio_chip *chip);
90 	int			base;
91 	u16			ngpio;
92 	struct gpio_desc	*desc;
93 	const char		*const *names;
94 	unsigned		can_sleep:1;
95 	unsigned		exported:1;
96 
97 #if defined(CONFIG_OF_GPIO)
98 	/*
99 	 * If CONFIG_OF is enabled, then all GPIO controllers described in the
100 	 * device tree automatically may have an OF translation
101 	 */
102 	struct device_node *of_node;
103 	int of_gpio_n_cells;
104 	int (*of_xlate)(struct gpio_chip *gc,
105 			const struct of_phandle_args *gpiospec, u32 *flags);
106 #endif
107 #ifdef CONFIG_PINCTRL
108 	/*
109 	 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
110 	 * describe the actual pin range which they serve in an SoC. This
111 	 * information would be used by pinctrl subsystem to configure
112 	 * corresponding pins for gpio usage.
113 	 */
114 	struct list_head pin_ranges;
115 #endif
116 };
117 
118 extern const char *gpiochip_is_requested(struct gpio_chip *chip,
119 			unsigned offset);
120 
121 /* add/remove chips */
122 extern int gpiochip_add(struct gpio_chip *chip);
123 extern int __must_check gpiochip_remove(struct gpio_chip *chip);
124 extern struct gpio_chip *gpiochip_find(void *data,
125 			      int (*match)(struct gpio_chip *chip, void *data));
126 
127 /* lock/unlock as IRQ */
128 int gpiod_lock_as_irq(struct gpio_desc *desc);
129 void gpiod_unlock_as_irq(struct gpio_desc *desc);
130 
131 enum gpio_lookup_flags {
132 	GPIO_ACTIVE_HIGH = (0 << 0),
133 	GPIO_ACTIVE_LOW = (1 << 0),
134 	GPIO_OPEN_DRAIN = (1 << 1),
135 	GPIO_OPEN_SOURCE = (1 << 2),
136 };
137 
138 /**
139  * Lookup table for associating GPIOs to specific devices and functions using
140  * platform data.
141  */
142 struct gpiod_lookup {
143 	struct list_head list;
144 	/*
145 	 * name of the chip the GPIO belongs to
146 	 */
147 	const char *chip_label;
148 	/*
149 	 * hardware number (i.e. relative to the chip) of the GPIO
150 	 */
151 	u16 chip_hwnum;
152 	/*
153 	 * name of device that can claim this GPIO
154 	 */
155 	const char *dev_id;
156 	/*
157 	 * name of the GPIO from the device's point of view
158 	 */
159 	const char *con_id;
160 	/*
161 	 * index of the GPIO in case several GPIOs share the same name
162 	 */
163 	unsigned int idx;
164 	/*
165 	 * mask of GPIO_* values
166 	 */
167 	enum gpio_lookup_flags flags;
168 };
169 
170 /*
171  * Simple definition of a single GPIO under a con_id
172  */
173 #define GPIO_LOOKUP(_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \
174 	GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags)
175 
176 /*
177  * Use this macro if you need to have several GPIOs under the same con_id.
178  * Each GPIO needs to use a different index and can be accessed using
179  * gpiod_get_index()
180  */
181 #define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \
182 			_flags)                                           \
183 {                                                                         \
184 	.chip_label = _chip_label,                                        \
185 	.chip_hwnum = _chip_hwnum,                                        \
186 	.dev_id = _dev_id,                                                \
187 	.con_id = _con_id,                                                \
188 	.idx = _idx,                                                      \
189 	.flags = _flags,                                                  \
190 }
191 
192 void gpiod_add_table(struct gpiod_lookup *table, size_t size);
193 
194 #endif
195