Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_GPIO_CONSUMER_H |
| 2 | #define __LINUX_GPIO_CONSUMER_H |
| 3 | |
| 4 | #include <linux/err.h> |
| 5 | #include <linux/kernel.h> |
| 6 | |
| 7 | #ifdef CONFIG_GPIOLIB |
| 8 | |
| 9 | struct device; |
| 10 | struct gpio_chip; |
| 11 | |
| 12 | /** |
| 13 | * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are |
| 14 | * preferable to the old integer-based handles. |
| 15 | * |
| 16 | * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid |
| 17 | * until the GPIO is released. |
| 18 | */ |
| 19 | struct gpio_desc; |
| 20 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 21 | /* Acquire and dispose GPIOs */ |
| 22 | struct gpio_desc *__must_check gpiod_get(struct device *dev, |
| 23 | const char *con_id); |
| 24 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 25 | const char *con_id, |
| 26 | unsigned int idx); |
| 27 | void gpiod_put(struct gpio_desc *desc); |
| 28 | |
| 29 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
| 30 | const char *con_id); |
| 31 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
| 32 | const char *con_id, |
| 33 | unsigned int idx); |
| 34 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); |
| 35 | |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 36 | int gpiod_get_direction(const struct gpio_desc *desc); |
| 37 | int gpiod_direction_input(struct gpio_desc *desc); |
| 38 | int gpiod_direction_output(struct gpio_desc *desc, int value); |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame^] | 39 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value); |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* Value get/set from non-sleeping context */ |
| 42 | int gpiod_get_value(const struct gpio_desc *desc); |
| 43 | void gpiod_set_value(struct gpio_desc *desc, int value); |
| 44 | int gpiod_get_raw_value(const struct gpio_desc *desc); |
| 45 | void gpiod_set_raw_value(struct gpio_desc *desc, int value); |
| 46 | |
| 47 | /* Value get/set from sleeping context */ |
| 48 | int gpiod_get_value_cansleep(const struct gpio_desc *desc); |
| 49 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); |
| 50 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); |
| 51 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); |
| 52 | |
| 53 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); |
| 54 | |
| 55 | int gpiod_is_active_low(const struct gpio_desc *desc); |
| 56 | int gpiod_cansleep(const struct gpio_desc *desc); |
| 57 | |
| 58 | int gpiod_to_irq(const struct gpio_desc *desc); |
| 59 | |
| 60 | /* Convert between the old gpio_ and new gpiod_ interfaces */ |
| 61 | struct gpio_desc *gpio_to_desc(unsigned gpio); |
| 62 | int desc_to_gpio(const struct gpio_desc *desc); |
| 63 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc); |
| 64 | |
| 65 | #else /* CONFIG_GPIOLIB */ |
| 66 | |
| 67 | static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, |
| 68 | const char *con_id) |
| 69 | { |
| 70 | return ERR_PTR(-ENOSYS); |
| 71 | } |
| 72 | static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
| 73 | const char *con_id, |
| 74 | unsigned int idx) |
| 75 | { |
| 76 | return ERR_PTR(-ENOSYS); |
| 77 | } |
| 78 | static inline void gpiod_put(struct gpio_desc *desc) |
| 79 | { |
| 80 | might_sleep(); |
| 81 | |
| 82 | /* GPIO can never have been requested */ |
| 83 | WARN_ON(1); |
| 84 | } |
| 85 | |
| 86 | static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
| 87 | const char *con_id) |
| 88 | { |
| 89 | return ERR_PTR(-ENOSYS); |
| 90 | } |
| 91 | static inline |
| 92 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
| 93 | const char *con_id, |
| 94 | unsigned int idx) |
| 95 | { |
| 96 | return ERR_PTR(-ENOSYS); |
| 97 | } |
| 98 | static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 99 | { |
| 100 | might_sleep(); |
| 101 | |
| 102 | /* GPIO can never have been requested */ |
| 103 | WARN_ON(1); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static inline int gpiod_get_direction(const struct gpio_desc *desc) |
| 108 | { |
| 109 | /* GPIO can never have been requested */ |
| 110 | WARN_ON(1); |
| 111 | return -ENOSYS; |
| 112 | } |
| 113 | static inline int gpiod_direction_input(struct gpio_desc *desc) |
| 114 | { |
| 115 | /* GPIO can never have been requested */ |
| 116 | WARN_ON(1); |
| 117 | return -ENOSYS; |
| 118 | } |
| 119 | static inline int gpiod_direction_output(struct gpio_desc *desc, int value) |
| 120 | { |
| 121 | /* GPIO can never have been requested */ |
| 122 | WARN_ON(1); |
| 123 | return -ENOSYS; |
| 124 | } |
Philipp Zabel | ef70bbe | 2014-01-07 12:34:11 +0100 | [diff] [blame^] | 125 | static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
| 126 | { |
| 127 | /* GPIO can never have been requested */ |
| 128 | WARN_ON(1); |
| 129 | return -ENOSYS; |
| 130 | } |
Alexandre Courbot | 79a9bec | 2013-10-17 10:21:36 -0700 | [diff] [blame] | 131 | |
| 132 | |
| 133 | static inline int gpiod_get_value(const struct gpio_desc *desc) |
| 134 | { |
| 135 | /* GPIO can never have been requested */ |
| 136 | WARN_ON(1); |
| 137 | return 0; |
| 138 | } |
| 139 | static inline void gpiod_set_value(struct gpio_desc *desc, int value) |
| 140 | { |
| 141 | /* GPIO can never have been requested */ |
| 142 | WARN_ON(1); |
| 143 | } |
| 144 | static inline int gpiod_get_raw_value(const struct gpio_desc *desc) |
| 145 | { |
| 146 | /* GPIO can never have been requested */ |
| 147 | WARN_ON(1); |
| 148 | return 0; |
| 149 | } |
| 150 | static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
| 151 | { |
| 152 | /* GPIO can never have been requested */ |
| 153 | WARN_ON(1); |
| 154 | } |
| 155 | |
| 156 | static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) |
| 157 | { |
| 158 | /* GPIO can never have been requested */ |
| 159 | WARN_ON(1); |
| 160 | return 0; |
| 161 | } |
| 162 | static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) |
| 163 | { |
| 164 | /* GPIO can never have been requested */ |
| 165 | WARN_ON(1); |
| 166 | } |
| 167 | static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
| 168 | { |
| 169 | /* GPIO can never have been requested */ |
| 170 | WARN_ON(1); |
| 171 | return 0; |
| 172 | } |
| 173 | static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, |
| 174 | int value) |
| 175 | { |
| 176 | /* GPIO can never have been requested */ |
| 177 | WARN_ON(1); |
| 178 | } |
| 179 | |
| 180 | static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
| 181 | { |
| 182 | /* GPIO can never have been requested */ |
| 183 | WARN_ON(1); |
| 184 | return -ENOSYS; |
| 185 | } |
| 186 | |
| 187 | static inline int gpiod_is_active_low(const struct gpio_desc *desc) |
| 188 | { |
| 189 | /* GPIO can never have been requested */ |
| 190 | WARN_ON(1); |
| 191 | return 0; |
| 192 | } |
| 193 | static inline int gpiod_cansleep(const struct gpio_desc *desc) |
| 194 | { |
| 195 | /* GPIO can never have been requested */ |
| 196 | WARN_ON(1); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static inline int gpiod_to_irq(const struct gpio_desc *desc) |
| 201 | { |
| 202 | /* GPIO can never have been requested */ |
| 203 | WARN_ON(1); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | static inline struct gpio_desc *gpio_to_desc(unsigned gpio) |
| 208 | { |
| 209 | return ERR_PTR(-EINVAL); |
| 210 | } |
| 211 | static inline int desc_to_gpio(const struct gpio_desc *desc) |
| 212 | { |
| 213 | /* GPIO can never have been requested */ |
| 214 | WARN_ON(1); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) |
| 218 | { |
| 219 | /* GPIO can never have been requested */ |
| 220 | WARN_ON(1); |
| 221 | return ERR_PTR(-ENODEV); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | #endif /* CONFIG_GPIOLIB */ |
| 226 | |
| 227 | #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) |
| 228 | |
| 229 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change); |
| 230 | int gpiod_export_link(struct device *dev, const char *name, |
| 231 | struct gpio_desc *desc); |
| 232 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); |
| 233 | void gpiod_unexport(struct gpio_desc *desc); |
| 234 | |
| 235 | #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ |
| 236 | |
| 237 | static inline int gpiod_export(struct gpio_desc *desc, |
| 238 | bool direction_may_change) |
| 239 | { |
| 240 | return -ENOSYS; |
| 241 | } |
| 242 | |
| 243 | static inline int gpiod_export_link(struct device *dev, const char *name, |
| 244 | struct gpio_desc *desc) |
| 245 | { |
| 246 | return -ENOSYS; |
| 247 | } |
| 248 | |
| 249 | static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
| 250 | { |
| 251 | return -ENOSYS; |
| 252 | } |
| 253 | |
| 254 | static inline void gpiod_unexport(struct gpio_desc *desc) |
| 255 | { |
| 256 | } |
| 257 | |
| 258 | #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ |
| 259 | |
| 260 | #endif |