blob: 9fe2836422534bc177513bb07bb186a306930ee1 [file] [log] [blame]
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001#ifndef __LINUX_GPIO_DRIVER_H
2#define __LINUX_GPIO_DRIVER_H
3
4#include <linux/types.h>
Alexandre Courbotc9a99722013-11-25 18:34:24 +09005#include <linux/module.h>
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07006
7struct device;
8struct gpio_desc;
Alexandre Courbotc9a99722013-11-25 18:34:24 +09009struct of_phandle_args;
10struct device_node;
Stephen Rothwellf3ed0b62013-10-29 01:06:23 +110011struct seq_file;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070012
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +090013#ifdef CONFIG_GPIOLIB
14
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070015/**
16 * struct gpio_chip - abstract a GPIO controller
17 * @label: for diagnostics
18 * @dev: optional device providing the GPIOs
19 * @owner: helps prevent removal of modules exporting active GPIOs
20 * @list: links gpio_chips together for traversal
21 * @request: optional hook for chip-specific activation, such as
22 * enabling module power and clock; may sleep
23 * @free: optional hook for chip-specific deactivation, such as
24 * disabling module power and clock; may sleep
25 * @get_direction: returns direction for signal "offset", 0=out, 1=in,
26 * (same as GPIOF_DIR_XXX), or negative error
27 * @direction_input: configures signal "offset" as input, or returns error
28 * @direction_output: configures signal "offset" as output, or returns error
29 * @get: returns value for signal "offset"; for output signals this
30 * returns either the value actually sensed, or zero
31 * @set: assigns output value for signal "offset"
32 * @set_debounce: optional hook for setting debounce time for specified gpio in
33 * interrupt triggered gpio chips
34 * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
35 * implementation may not sleep
36 * @dbg_show: optional routine to show contents in debugfs; default code
37 * will be used when this is omitted, but custom code can show extra
38 * state (such as pullup/pulldown configuration).
39 * @base: identifies the first GPIO number handled by this chip; or, if
40 * negative during registration, requests dynamic ID allocation.
41 * @ngpio: the number of GPIOs handled by this controller; the last GPIO
42 * handled is (base + ngpio - 1).
43 * @desc: array of ngpio descriptors. Private.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070044 * @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.
Linus Walleij9fb1f392013-12-04 14:42:46 +010050 * @can_sleep: flag must be set iff get()/set() methods sleep, as they
51 * must while accessing GPIO expander chips over I2C or SPI
52 * @exported: flags if the gpiochip is exported for use from sysfs. Private.
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070053 *
54 * A gpio_chip can help platforms abstract various sources of GPIOs so
55 * they can all be accessed through a common programing interface.
56 * Example sources would be SOC controllers, FPGAs, multifunction
57 * chips, dedicated GPIO expanders, and so on.
58 *
59 * Each chip controls a number of signals, identified in method calls
60 * by "offset" values in the range 0..(@ngpio - 1). When those signals
61 * are referenced through calls like gpio_get_value(gpio), the offset
62 * is calculated by subtracting @base from the gpio number.
63 */
64struct gpio_chip {
65 const char *label;
66 struct device *dev;
67 struct module *owner;
68 struct list_head list;
69
70 int (*request)(struct gpio_chip *chip,
71 unsigned offset);
72 void (*free)(struct gpio_chip *chip,
73 unsigned offset);
74 int (*get_direction)(struct gpio_chip *chip,
75 unsigned offset);
76 int (*direction_input)(struct gpio_chip *chip,
77 unsigned offset);
78 int (*direction_output)(struct gpio_chip *chip,
79 unsigned offset, int value);
80 int (*get)(struct gpio_chip *chip,
81 unsigned offset);
82 void (*set)(struct gpio_chip *chip,
83 unsigned offset, int value);
84 int (*set_debounce)(struct gpio_chip *chip,
85 unsigned offset,
86 unsigned debounce);
87
88 int (*to_irq)(struct gpio_chip *chip,
89 unsigned offset);
90
91 void (*dbg_show)(struct seq_file *s,
92 struct gpio_chip *chip);
93 int base;
94 u16 ngpio;
95 struct gpio_desc *desc;
96 const char *const *names;
Linus Walleij9fb1f392013-12-04 14:42:46 +010097 bool can_sleep;
98 bool exported;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070099
100#if defined(CONFIG_OF_GPIO)
101 /*
102 * If CONFIG_OF is enabled, then all GPIO controllers described in the
103 * device tree automatically may have an OF translation
104 */
105 struct device_node *of_node;
106 int of_gpio_n_cells;
107 int (*of_xlate)(struct gpio_chip *gc,
108 const struct of_phandle_args *gpiospec, u32 *flags);
109#endif
110#ifdef CONFIG_PINCTRL
111 /*
112 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
113 * describe the actual pin range which they serve in an SoC. This
114 * information would be used by pinctrl subsystem to configure
115 * corresponding pins for gpio usage.
116 */
117 struct list_head pin_ranges;
118#endif
119};
120
121extern const char *gpiochip_is_requested(struct gpio_chip *chip,
122 unsigned offset);
123
124/* add/remove chips */
125extern int gpiochip_add(struct gpio_chip *chip);
126extern int __must_check gpiochip_remove(struct gpio_chip *chip);
127extern struct gpio_chip *gpiochip_find(void *data,
128 int (*match)(struct gpio_chip *chip, void *data));
129
130/* lock/unlock as IRQ */
131int gpiod_lock_as_irq(struct gpio_desc *desc);
132void gpiod_unlock_as_irq(struct gpio_desc *desc);
133
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900134struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
135
136struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
137 u16 hwnum);
138
Alexandre Courbot53e7cac2013-11-16 21:44:52 +0900139enum gpio_lookup_flags {
140 GPIO_ACTIVE_HIGH = (0 << 0),
141 GPIO_ACTIVE_LOW = (1 << 0),
142 GPIO_OPEN_DRAIN = (1 << 1),
143 GPIO_OPEN_SOURCE = (1 << 2),
144};
145
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700146/**
Andy Shevchenkof9244ae2013-12-05 11:26:25 +0200147 * struct gpiod_lookup - lookup table
148 * @chip_label: name of the chip the GPIO belongs to
149 * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO
150 * @con_id: name of the GPIO from the device's point of view
151 * @idx: index of the GPIO in case several GPIOs share the same name
152 * @flags: mask of GPIO_* values
153 *
154 * gpiod_lookup is a lookup table for associating GPIOs to specific devices and
155 * functions using platform data.
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700156 */
157struct gpiod_lookup {
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700158 const char *chip_label;
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700159 u16 chip_hwnum;
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700160 const char *con_id;
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700161 unsigned int idx;
Alexandre Courbot53e7cac2013-11-16 21:44:52 +0900162 enum gpio_lookup_flags flags;
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700163};
164
Alexandre Courbotad824782013-12-03 12:20:11 +0900165struct gpiod_lookup_table {
166 struct list_head list;
167 const char *dev_id;
168 struct gpiod_lookup table[];
169};
170
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700171/*
172 * Simple definition of a single GPIO under a con_id
173 */
Alexandre Courbotad824782013-12-03 12:20:11 +0900174#define GPIO_LOOKUP(_chip_label, _chip_hwnum, _con_id, _flags) \
175 GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags)
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700176
177/*
178 * Use this macro if you need to have several GPIOs under the same con_id.
179 * Each GPIO needs to use a different index and can be accessed using
180 * gpiod_get_index()
181 */
Alexandre Courbotad824782013-12-03 12:20:11 +0900182#define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, _idx, _flags) \
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700183{ \
184 .chip_label = _chip_label, \
185 .chip_hwnum = _chip_hwnum, \
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700186 .con_id = _con_id, \
187 .idx = _idx, \
188 .flags = _flags, \
189}
190
Alexandre Courbotad824782013-12-03 12:20:11 +0900191void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
Alexandre Courbotbae48da2013-10-17 10:21:38 -0700192
Alexandre Courbotbb1e88c2014-02-09 17:43:54 +0900193#else /* CONFIG_GPIOLIB */
194
195static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
196{
197 /* GPIO can never have been requested */
198 WARN_ON(1);
199 return ERR_PTR(-ENODEV);
200}
201
202#endif /* CONFIG_GPIOLIB */
203
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700204#endif