]>
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; |
ce793486 RW |
23 | struct acpi_device; |
24 | ||
ff2b1359 LW |
25 | /** |
26 | * struct gpio_device - internal state container for GPIO devices | |
27 | * @id: numerical ID number for the GPIO chip | |
28 | * @dev: the GPIO device struct | |
3c702e99 | 29 | * @chrdev: character device for the GPIO device |
afbc4f31 LW |
30 | * @mockdev: class device used by the deprecated sysfs interface (may be |
31 | * NULL) | |
ff2b1359 LW |
32 | * @owner: helps prevent removal of modules exporting active GPIOs |
33 | * @chip: pointer to the corresponding gpiochip, holding static | |
34 | * data for this device | |
35 | * @list: links gpio_device:s together for traversal | |
36 | * | |
37 | * This state container holds most of the runtime variable data | |
38 | * for a GPIO device and can hold references and live on after the | |
39 | * GPIO chip has been removed, if it is still being used from | |
40 | * userspace. | |
41 | */ | |
42 | struct gpio_device { | |
43 | int id; | |
44 | struct device dev; | |
3c702e99 | 45 | struct cdev chrdev; |
afbc4f31 | 46 | struct device *mockdev; |
ff2b1359 LW |
47 | struct module *owner; |
48 | struct gpio_chip *chip; | |
49 | struct list_head list; | |
50 | }; | |
51 | ||
5ccff852 MW |
52 | /** |
53 | * struct acpi_gpio_info - ACPI GPIO specific information | |
54 | * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo | |
55 | * @active_low: in case of @gpioint, the pin is active low | |
56 | */ | |
57 | struct acpi_gpio_info { | |
58 | bool gpioint; | |
52044723 CR |
59 | int polarity; |
60 | int triggering; | |
5ccff852 MW |
61 | }; |
62 | ||
7f2e553a RI |
63 | /* gpio suffixes used for ACPI and device tree lookup */ |
64 | static const char * const gpio_suffixes[] = { "gpios", "gpio" }; | |
65 | ||
664e3e5a MW |
66 | #ifdef CONFIG_ACPI |
67 | void acpi_gpiochip_add(struct gpio_chip *chip); | |
68 | void acpi_gpiochip_remove(struct gpio_chip *chip); | |
5ccff852 | 69 | |
afa82fab MW |
70 | void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); |
71 | void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); | |
72 | ||
0d9a693c MW |
73 | struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev, |
74 | const char *propname, int index, | |
5ccff852 | 75 | struct acpi_gpio_info *info); |
504a3374 RW |
76 | struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode, |
77 | const char *propname, int index, | |
78 | struct acpi_gpio_info *info); | |
66858527 RI |
79 | |
80 | int acpi_gpio_count(struct device *dev, const char *con_id); | |
10cf4899 DT |
81 | |
82 | bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id); | |
664e3e5a MW |
83 | #else |
84 | static inline void acpi_gpiochip_add(struct gpio_chip *chip) { } | |
85 | static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { } | |
5ccff852 | 86 | |
afa82fab MW |
87 | static inline void |
88 | acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } | |
89 | ||
90 | static inline void | |
91 | acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } | |
92 | ||
5ccff852 | 93 | static inline struct gpio_desc * |
0d9a693c MW |
94 | acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname, |
95 | int index, struct acpi_gpio_info *info) | |
5ccff852 MW |
96 | { |
97 | return ERR_PTR(-ENOSYS); | |
98 | } | |
504a3374 RW |
99 | static inline struct gpio_desc * |
100 | acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname, | |
101 | int index, struct acpi_gpio_info *info) | |
102 | { | |
103 | return ERR_PTR(-ENXIO); | |
104 | } | |
66858527 RI |
105 | static inline int acpi_gpio_count(struct device *dev, const char *con_id) |
106 | { | |
107 | return -ENODEV; | |
108 | } | |
10cf4899 DT |
109 | |
110 | static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev, | |
111 | const char *con_id) | |
112 | { | |
113 | return false; | |
114 | } | |
664e3e5a MW |
115 | #endif |
116 | ||
f01d9075 AC |
117 | struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
118 | const char *list_name, int index, enum of_gpio_flags *flags); | |
119 | ||
1bd6b601 AC |
120 | struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); |
121 | ||
0eb4c6c2 | 122 | extern struct spinlock gpio_lock; |
ff2b1359 | 123 | extern struct list_head gpio_devices; |
0eb4c6c2 AC |
124 | |
125 | struct gpio_desc { | |
126 | struct gpio_chip *chip; | |
127 | unsigned long flags; | |
128 | /* flag symbols are bit numbers */ | |
129 | #define FLAG_REQUESTED 0 | |
130 | #define FLAG_IS_OUT 1 | |
131 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ | |
132 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | |
0eb4c6c2 AC |
133 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
134 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ | |
135 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | |
136 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ | |
f625d460 | 137 | #define FLAG_IS_HOGGED 11 /* GPIO is hogged */ |
0eb4c6c2 | 138 | |
c0017ed7 | 139 | /* Connection label */ |
0eb4c6c2 | 140 | const char *label; |
c0017ed7 MP |
141 | /* Name of the GPIO */ |
142 | const char *name; | |
0eb4c6c2 AC |
143 | }; |
144 | ||
145 | int gpiod_request(struct gpio_desc *desc, const char *label); | |
146 | void gpiod_free(struct gpio_desc *desc); | |
f625d460 BP |
147 | int gpiod_hog(struct gpio_desc *desc, const char *name, |
148 | unsigned long lflags, enum gpiod_flags dflags); | |
0eb4c6c2 AC |
149 | |
150 | /* | |
151 | * Return the GPIO number of the passed descriptor relative to its chip | |
152 | */ | |
153 | static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc) | |
154 | { | |
155 | return desc - &desc->chip->desc[0]; | |
156 | } | |
157 | ||
158 | /* With descriptor prefix */ | |
159 | ||
160 | #define gpiod_emerg(desc, fmt, ...) \ | |
161 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
162 | ##__VA_ARGS__) | |
163 | #define gpiod_crit(desc, fmt, ...) \ | |
164 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
165 | ##__VA_ARGS__) | |
166 | #define gpiod_err(desc, fmt, ...) \ | |
167 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
168 | ##__VA_ARGS__) | |
169 | #define gpiod_warn(desc, fmt, ...) \ | |
170 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
171 | ##__VA_ARGS__) | |
172 | #define gpiod_info(desc, fmt, ...) \ | |
173 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
174 | ##__VA_ARGS__) | |
175 | #define gpiod_dbg(desc, fmt, ...) \ | |
176 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
177 | ##__VA_ARGS__) | |
178 | ||
179 | /* With chip prefix */ | |
180 | ||
181 | #define chip_emerg(chip, fmt, ...) \ | |
34ffd85d | 182 | dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 183 | #define chip_crit(chip, fmt, ...) \ |
34ffd85d | 184 | dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 185 | #define chip_err(chip, fmt, ...) \ |
34ffd85d | 186 | dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 187 | #define chip_warn(chip, fmt, ...) \ |
34ffd85d | 188 | dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 189 | #define chip_info(chip, fmt, ...) \ |
34ffd85d | 190 | dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 | 191 | #define chip_dbg(chip, fmt, ...) \ |
34ffd85d | 192 | dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__) |
0eb4c6c2 AC |
193 | |
194 | #ifdef CONFIG_GPIO_SYSFS | |
195 | ||
afbc4f31 LW |
196 | int gpiochip_sysfs_register(struct gpio_device *gdev); |
197 | void gpiochip_sysfs_unregister(struct gpio_device *gdev); | |
0eb4c6c2 AC |
198 | |
199 | #else | |
200 | ||
afbc4f31 | 201 | static inline int gpiochip_sysfs_register(struct gpio_device *gdev) |
0eb4c6c2 AC |
202 | { |
203 | return 0; | |
204 | } | |
205 | ||
afbc4f31 | 206 | static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev) |
0eb4c6c2 AC |
207 | { |
208 | } | |
209 | ||
210 | #endif /* CONFIG_GPIO_SYSFS */ | |
211 | ||
664e3e5a | 212 | #endif /* GPIOLIB_H */ |