]>
Commit | Line | Data |
---|---|---|
664e3e5a MW |
1 | /* |
2 | * Internal GPIO functions. | |
3 | * | |
4 | * Copyright (C) 2013, Intel Corporation | |
5 | * Author: Mika Westerberg <mika.westerberg@linux.intel.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | ||
12 | #ifndef GPIOLIB_H | |
13 | #define GPIOLIB_H | |
14 | ||
ff2b1359 | 15 | #include <linux/gpio/driver.h> |
5ccff852 MW |
16 | #include <linux/err.h> |
17 | #include <linux/device.h> | |
ff2b1359 LW |
18 | #include <linux/module.h> |
19 | #include <linux/cdev.h> | |
5ccff852 | 20 | |
f01d9075 | 21 | enum of_gpio_flags; |
29ab875b | 22 | enum gpiod_flags; |
ea713bc4 | 23 | enum gpio_lookup_flags; |
ce793486 RW |
24 | struct acpi_device; |
25 | ||
ff2b1359 LW |
26 | /** |
27 | * struct gpio_device - internal state container for GPIO devices | |
28 | * @id: numerical ID number for the GPIO chip | |
29 | * @dev: the GPIO device struct | |
3c702e99 | 30 | * @chrdev: character device for the GPIO device |
afbc4f31 LW |
31 | * @mockdev: class device used by the deprecated sysfs interface (may be |
32 | * NULL) | |
ff2b1359 LW |
33 | * @owner: helps prevent removal of modules exporting active GPIOs |
34 | * @chip: pointer to the corresponding gpiochip, holding static | |
35 | * data for this device | |
1c3cdb18 | 36 | * @descs: array of ngpio descriptors. |
fdeb8e15 LW |
37 | * @ngpio: the number of GPIO lines on this GPIO device, equal to the size |
38 | * of the @descs array. | |
39 | * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned | |
40 | * at device creation time. | |
df4878e9 LW |
41 | * @label: a descriptive name for the GPIO device, such as the part number |
42 | * or name of the IP component in a System on Chip. | |
43c54eca | 43 | * @data: per-instance data assigned by the driver |
ff2b1359 LW |
44 | * @list: links gpio_device:s together for traversal |
45 | * | |
46 | * This state container holds most of the runtime variable data | |
47 | * for a GPIO device and can hold references and live on after the | |
48 | * GPIO chip has been removed, if it is still being used from | |
49 | * userspace. | |
50 | */ | |
51 | struct gpio_device { | |
52 | int id; | |
53 | struct device dev; | |
3c702e99 | 54 | struct cdev chrdev; |
afbc4f31 | 55 | struct device *mockdev; |
ff2b1359 LW |
56 | struct module *owner; |
57 | struct gpio_chip *chip; | |
1c3cdb18 | 58 | struct gpio_desc *descs; |
fdeb8e15 LW |
59 | int base; |
60 | u16 ngpio; | |
df4878e9 | 61 | char *label; |
43c54eca | 62 | void *data; |
ff2b1359 | 63 | struct list_head list; |
20ec3e39 LW |
64 | |
65 | #ifdef CONFIG_PINCTRL | |
66 | /* | |
67 | * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally | |
68 | * describe the actual pin range which they serve in an SoC. This | |
69 | * information would be used by pinctrl subsystem to configure | |
70 | * corresponding pins for gpio usage. | |
71 | */ | |
72 | struct list_head pin_ranges; | |
73 | #endif | |
ff2b1359 LW |
74 | }; |
75 | ||
5ccff852 MW |
76 | /** |
77 | * struct acpi_gpio_info - ACPI GPIO specific information | |
78 | * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo | |
79 | * @active_low: in case of @gpioint, the pin is active low | |
80 | */ | |
81 | struct acpi_gpio_info { | |
82 | bool gpioint; | |
52044723 CR |
83 | int polarity; |
84 | int triggering; | |
5ccff852 MW |
85 | }; |
86 | ||
7f2e553a RI |
87 | /* gpio suffixes used for ACPI and device tree lookup */ |
88 | static const char * const gpio_suffixes[] = { "gpios", "gpio" }; | |
89 | ||
ea713bc4 LW |
90 | #ifdef CONFIG_OF_GPIO |
91 | struct gpio_desc *of_find_gpio(struct device *dev, | |
92 | const char *con_id, | |
93 | unsigned int idx, | |
94 | enum gpio_lookup_flags *flags); | |
e0852940 LW |
95 | struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
96 | const char *list_name, int index, enum of_gpio_flags *flags); | |
f4c1181f LW |
97 | int of_gpiochip_add(struct gpio_chip *gc); |
98 | void of_gpiochip_remove(struct gpio_chip *gc); | |
ea713bc4 LW |
99 | #else |
100 | static inline struct gpio_desc *of_find_gpio(struct device *dev, | |
101 | const char *con_id, | |
102 | unsigned int idx, | |
103 | enum gpio_lookup_flags *flags) | |
104 | { | |
105 | return ERR_PTR(-ENOENT); | |
106 | } | |
e0852940 LW |
107 | static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
108 | const char *list_name, int index, enum of_gpio_flags *flags) | |
109 | { | |
110 | return ERR_PTR(-ENOENT); | |
111 | } | |
f4c1181f LW |
112 | static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; } |
113 | static inline void of_gpiochip_remove(struct gpio_chip *gc) { } | |
ea713bc4 LW |
114 | #endif /* CONFIG_OF_GPIO */ |
115 | ||
664e3e5a MW |
116 | #ifdef CONFIG_ACPI |
117 | void acpi_gpiochip_add(struct gpio_chip *chip); | |
118 | void acpi_gpiochip_remove(struct gpio_chip *chip); | |
5ccff852 | 119 | |
afa82fab MW |
120 | void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); |
121 | void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); | |
122 | ||
031ba28a LW |
123 | struct gpio_desc *acpi_find_gpio(struct device *dev, |
124 | const char *con_id, | |
125 | unsigned int idx, | |
126 | enum gpiod_flags flags, | |
127 | enum gpio_lookup_flags *lookupflags); | |
504a3374 RW |
128 | struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, |
129 | const char *propname, int index, | |
130 | struct acpi_gpio_info *info); | |
66858527 RI |
131 | |
132 | int acpi_gpio_count(struct device *dev, const char *con_id); | |
10cf4899 DT |
133 | |
134 | bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id); | |
664e3e5a MW |
135 | #else |
136 | static inline void acpi_gpiochip_add(struct gpio_chip *chip) { } | |
137 | static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { } | |
5ccff852 | 138 | |
afa82fab MW |
139 | static inline void |
140 | acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } | |
141 | ||
142 | static inline void | |
143 | acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } | |
144 | ||
5ccff852 | 145 | static inline struct gpio_desc * |
031ba28a LW |
146 | acpi_find_gpio(struct device *dev, const char *con_id, |
147 | unsigned int idx, enum gpiod_flags flags, | |
148 | enum gpio_lookup_flags *lookupflags) | |
5ccff852 | 149 | { |
031ba28a | 150 | return ERR_PTR(-ENOENT); |
5ccff852 | 151 | } |
504a3374 RW |
152 | static inline struct gpio_desc * |
153 | acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname, | |
154 | int index, struct acpi_gpio_info *info) | |
155 | { | |
156 | return ERR_PTR(-ENXIO); | |
157 | } | |
66858527 RI |
158 | static inline int acpi_gpio_count(struct device *dev, const char *con_id) |
159 | { | |
160 | return -ENODEV; | |
161 | } | |
10cf4899 DT |
162 | |
163 | static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev, | |
164 | const char *con_id) | |
165 | { | |
166 | return false; | |
167 | } | |
664e3e5a MW |
168 | #endif |
169 | ||
1bd6b601 | 170 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); |
44c7288f LW |
171 | void gpiod_set_array_value_complex(bool raw, bool can_sleep, |
172 | unsigned int array_size, | |
173 | struct gpio_desc **desc_array, | |
174 | int *value_array); | |
1bd6b601 | 175 | |
0eb4c6c2 | 176 | extern struct spinlock gpio_lock; |
ff2b1359 | 177 | extern struct list_head gpio_devices; |
0eb4c6c2 AC |
178 | |
179 | struct gpio_desc { | |
fdeb8e15 | 180 | struct gpio_device *gdev; |
0eb4c6c2 AC |
181 | unsigned long flags; |
182 | /* flag symbols are bit numbers */ | |
183 | #define FLAG_REQUESTED 0 | |
184 | #define FLAG_IS_OUT 1 | |
185 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ | |
186 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | |
0eb4c6c2 AC |
187 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
188 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | |
189 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | |
190 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ | |
f625d460 | 191 | #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ |
0eb4c6c2 | 192 | |
c0017ed7 | 193 | /* Connection label */ |
0eb4c6c2 | 194 | const char *label; |
c0017ed7 MP |
195 | /* Name of the GPIO */ |
196 | const char *name; | |
0eb4c6c2 AC |
197 | }; |
198 | ||
199 | int gpiod_request(struct gpio_desc *desc, const char *label); | |
200 | void gpiod_free(struct gpio_desc *desc); | |
f625d460 BP |
201 | int gpiod_hog(struct gpio_desc *desc, const char *name, |
202 | unsigned long lflags, enum gpiod_flags dflags); | |
0eb4c6c2 AC |
203 | |
204 | /* | |
205 | * Return the GPIO number of the passed descriptor relative to its chip | |
206 | */ | |
207 | static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc) | |
208 | { | |
fdeb8e15 | 209 | return desc - &desc->gdev->descs[0]; |
0eb4c6c2 AC |
210 | } |
211 | ||
9427ecbe MW |
212 | void devprop_gpiochip_set_names(struct gpio_chip *chip); |
213 | ||
0eb4c6c2 AC |
214 | /* With descriptor prefix */ |
215 | ||
216 | #define gpiod_emerg(desc, fmt, ...) \ | |
217 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
218 | ##__VA_ARGS__) | |
219 | #define gpiod_crit(desc, fmt, ...) \ | |
220 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
221 | ##__VA_ARGS__) | |
222 | #define gpiod_err(desc, fmt, ...) \ | |
223 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
224 | ##__VA_ARGS__) | |
225 | #define gpiod_warn(desc, fmt, ...) \ | |
226 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
227 | ##__VA_ARGS__) | |
228 | #define gpiod_info(desc, fmt, ...) \ | |
229 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
230 | ##__VA_ARGS__) | |
231 | #define gpiod_dbg(desc, fmt, ...) \ | |
232 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
233 | ##__VA_ARGS__) | |
234 | ||
235 | /* With chip prefix */ | |
236 | ||
237 | #define chip_emerg(chip, fmt, ...) \ | |
34ffd85d | 238 | dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 239 | #define chip_crit(chip, fmt, ...) \ |
34ffd85d | 240 | dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 241 | #define chip_err(chip, fmt, ...) \ |
34ffd85d | 242 | dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 243 | #define chip_warn(chip, fmt, ...) \ |
34ffd85d | 244 | dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 245 | #define chip_info(chip, fmt, ...) \ |
34ffd85d | 246 | dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 247 | #define chip_dbg(chip, fmt, ...) \ |
34ffd85d | 248 | dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 AC |
249 | |
250 | #ifdef CONFIG_GPIO_SYSFS | |
251 | ||
afbc4f31 LW |
252 | int gpiochip_sysfs_register(struct gpio_device *gdev); |
253 | void gpiochip_sysfs_unregister(struct gpio_device *gdev); | |
0eb4c6c2 AC |
254 | |
255 | #else | |
256 | ||
afbc4f31 | 257 | static inline int gpiochip_sysfs_register(struct gpio_device *gdev) |
0eb4c6c2 AC |
258 | { |
259 | return 0; | |
260 | } | |
261 | ||
afbc4f31 | 262 | static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev) |
0eb4c6c2 AC |
263 | { |
264 | } | |
265 | ||
266 | #endif /* CONFIG_GPIO_SYSFS */ | |
267 | ||
664e3e5a | 268 | #endif /* GPIOLIB_H */ |