1 #ifndef __LINUX_GPIO_CONSUMER_H 2 #define __LINUX_GPIO_CONSUMER_H 3 4 #include <linux/bug.h> 5 #include <linux/err.h> 6 #include <linux/kernel.h> 7 8 struct device; 9 10 /** 11 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are 12 * preferable to the old integer-based handles. 13 * 14 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid 15 * until the GPIO is released. 16 */ 17 struct gpio_desc; 18 19 /** 20 * Struct containing an array of descriptors that can be obtained using 21 * gpiod_get_array(). 22 */ 23 struct gpio_descs { 24 unsigned int ndescs; 25 struct gpio_desc *desc[]; 26 }; 27 28 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0) 29 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) 30 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) 31 32 /** 33 * Optional flags that can be passed to one of gpiod_* to configure direction 34 * and output value. These values cannot be OR'd. 35 */ 36 enum gpiod_flags { 37 GPIOD_ASIS = 0, 38 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, 39 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, 40 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | 41 GPIOD_FLAGS_BIT_DIR_VAL, 42 }; 43 44 #ifdef CONFIG_GPIOLIB 45 46 /* Return the number of GPIOs associated with a device / function */ 47 int gpiod_count(struct device *dev, const char *con_id); 48 49 /* Acquire and dispose GPIOs */ 50 struct gpio_desc *__must_check gpiod_get(struct device *dev, 51 const char *con_id, 52 enum gpiod_flags flags); 53 struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 54 const char *con_id, 55 unsigned int idx, 56 enum gpiod_flags flags); 57 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 58 const char *con_id, 59 enum gpiod_flags flags); 60 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 61 const char *con_id, 62 unsigned int index, 63 enum gpiod_flags flags); 64 struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 65 const char *con_id, 66 enum gpiod_flags flags); 67 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 68 const char *con_id, 69 enum gpiod_flags flags); 70 void gpiod_put(struct gpio_desc *desc); 71 void gpiod_put_array(struct gpio_descs *descs); 72 73 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 74 const char *con_id, 75 enum gpiod_flags flags); 76 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 77 const char *con_id, 78 unsigned int idx, 79 enum gpiod_flags flags); 80 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 81 const char *con_id, 82 enum gpiod_flags flags); 83 struct gpio_desc *__must_check 84 devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 85 unsigned int index, enum gpiod_flags flags); 86 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, 87 const char *con_id, 88 enum gpiod_flags flags); 89 struct gpio_descs *__must_check 90 devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 91 enum gpiod_flags flags); 92 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 93 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); 94 95 int gpiod_get_direction(struct gpio_desc *desc); 96 int gpiod_direction_input(struct gpio_desc *desc); 97 int gpiod_direction_output(struct gpio_desc *desc, int value); 98 int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 99 100 /* Value get/set from non-sleeping context */ 101 int gpiod_get_value(const struct gpio_desc *desc); 102 void gpiod_set_value(struct gpio_desc *desc, int value); 103 void gpiod_set_array_value(unsigned int array_size, 104 struct gpio_desc **desc_array, int *value_array); 105 int gpiod_get_raw_value(const struct gpio_desc *desc); 106 void gpiod_set_raw_value(struct gpio_desc *desc, int value); 107 void gpiod_set_raw_array_value(unsigned int array_size, 108 struct gpio_desc **desc_array, 109 int *value_array); 110 111 /* Value get/set from sleeping context */ 112 int gpiod_get_value_cansleep(const struct gpio_desc *desc); 113 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 114 void gpiod_set_array_value_cansleep(unsigned int array_size, 115 struct gpio_desc **desc_array, 116 int *value_array); 117 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 118 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 119 void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 120 struct gpio_desc **desc_array, 121 int *value_array); 122 123 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 124 125 int gpiod_is_active_low(const struct gpio_desc *desc); 126 int gpiod_cansleep(const struct gpio_desc *desc); 127 128 int gpiod_to_irq(const struct gpio_desc *desc); 129 130 /* Convert between the old gpio_ and new gpiod_ interfaces */ 131 struct gpio_desc *gpio_to_desc(unsigned gpio); 132 int desc_to_gpio(const struct gpio_desc *desc); 133 134 /* Child properties interface */ 135 struct fwnode_handle; 136 137 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 138 const char *propname); 139 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev, 140 const char *con_id, 141 struct fwnode_handle *child); 142 #else /* CONFIG_GPIOLIB */ 143 144 static inline int gpiod_count(struct device *dev, const char *con_id) 145 { 146 return 0; 147 } 148 149 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 150 const char *con_id, 151 enum gpiod_flags flags) 152 { 153 return ERR_PTR(-ENOSYS); 154 } 155 static inline struct gpio_desc *__must_check 156 gpiod_get_index(struct device *dev, 157 const char *con_id, 158 unsigned int idx, 159 enum gpiod_flags flags) 160 { 161 return ERR_PTR(-ENOSYS); 162 } 163 164 static inline struct gpio_desc *__must_check 165 gpiod_get_optional(struct device *dev, const char *con_id, 166 enum gpiod_flags flags) 167 { 168 return ERR_PTR(-ENOSYS); 169 } 170 171 static inline struct gpio_desc *__must_check 172 gpiod_get_index_optional(struct device *dev, const char *con_id, 173 unsigned int index, enum gpiod_flags flags) 174 { 175 return ERR_PTR(-ENOSYS); 176 } 177 178 static inline struct gpio_descs *__must_check 179 gpiod_get_array(struct device *dev, const char *con_id, 180 enum gpiod_flags flags) 181 { 182 return ERR_PTR(-ENOSYS); 183 } 184 185 static inline struct gpio_descs *__must_check 186 gpiod_get_array_optional(struct device *dev, const char *con_id, 187 enum gpiod_flags flags) 188 { 189 return ERR_PTR(-ENOSYS); 190 } 191 192 static inline void gpiod_put(struct gpio_desc *desc) 193 { 194 might_sleep(); 195 196 /* GPIO can never have been requested */ 197 WARN_ON(1); 198 } 199 200 static inline void gpiod_put_array(struct gpio_descs *descs) 201 { 202 might_sleep(); 203 204 /* GPIO can never have been requested */ 205 WARN_ON(1); 206 } 207 208 static inline struct gpio_desc *__must_check 209 devm_gpiod_get(struct device *dev, 210 const char *con_id, 211 enum gpiod_flags flags) 212 { 213 return ERR_PTR(-ENOSYS); 214 } 215 static inline 216 struct gpio_desc *__must_check 217 devm_gpiod_get_index(struct device *dev, 218 const char *con_id, 219 unsigned int idx, 220 enum gpiod_flags flags) 221 { 222 return ERR_PTR(-ENOSYS); 223 } 224 225 static inline struct gpio_desc *__must_check 226 devm_gpiod_get_optional(struct device *dev, const char *con_id, 227 enum gpiod_flags flags) 228 { 229 return ERR_PTR(-ENOSYS); 230 } 231 232 static inline struct gpio_desc *__must_check 233 devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 234 unsigned int index, enum gpiod_flags flags) 235 { 236 return ERR_PTR(-ENOSYS); 237 } 238 239 static inline struct gpio_descs *__must_check 240 devm_gpiod_get_array(struct device *dev, const char *con_id, 241 enum gpiod_flags flags) 242 { 243 return ERR_PTR(-ENOSYS); 244 } 245 246 static inline struct gpio_descs *__must_check 247 devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 248 enum gpiod_flags flags) 249 { 250 return ERR_PTR(-ENOSYS); 251 } 252 253 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 254 { 255 might_sleep(); 256 257 /* GPIO can never have been requested */ 258 WARN_ON(1); 259 } 260 261 static inline void devm_gpiod_put_array(struct device *dev, 262 struct gpio_descs *descs) 263 { 264 might_sleep(); 265 266 /* GPIO can never have been requested */ 267 WARN_ON(1); 268 } 269 270 271 static inline int gpiod_get_direction(const struct gpio_desc *desc) 272 { 273 /* GPIO can never have been requested */ 274 WARN_ON(1); 275 return -ENOSYS; 276 } 277 static inline int gpiod_direction_input(struct gpio_desc *desc) 278 { 279 /* GPIO can never have been requested */ 280 WARN_ON(1); 281 return -ENOSYS; 282 } 283 static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 284 { 285 /* GPIO can never have been requested */ 286 WARN_ON(1); 287 return -ENOSYS; 288 } 289 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 290 { 291 /* GPIO can never have been requested */ 292 WARN_ON(1); 293 return -ENOSYS; 294 } 295 296 297 static inline int gpiod_get_value(const struct gpio_desc *desc) 298 { 299 /* GPIO can never have been requested */ 300 WARN_ON(1); 301 return 0; 302 } 303 static inline void gpiod_set_value(struct gpio_desc *desc, int value) 304 { 305 /* GPIO can never have been requested */ 306 WARN_ON(1); 307 } 308 static inline void gpiod_set_array_value(unsigned int array_size, 309 struct gpio_desc **desc_array, 310 int *value_array) 311 { 312 /* GPIO can never have been requested */ 313 WARN_ON(1); 314 } 315 static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 316 { 317 /* GPIO can never have been requested */ 318 WARN_ON(1); 319 return 0; 320 } 321 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 322 { 323 /* GPIO can never have been requested */ 324 WARN_ON(1); 325 } 326 static inline void gpiod_set_raw_array_value(unsigned int array_size, 327 struct gpio_desc **desc_array, 328 int *value_array) 329 { 330 /* GPIO can never have been requested */ 331 WARN_ON(1); 332 } 333 334 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 335 { 336 /* GPIO can never have been requested */ 337 WARN_ON(1); 338 return 0; 339 } 340 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 341 { 342 /* GPIO can never have been requested */ 343 WARN_ON(1); 344 } 345 static inline void gpiod_set_array_value_cansleep(unsigned int array_size, 346 struct gpio_desc **desc_array, 347 int *value_array) 348 { 349 /* GPIO can never have been requested */ 350 WARN_ON(1); 351 } 352 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 353 { 354 /* GPIO can never have been requested */ 355 WARN_ON(1); 356 return 0; 357 } 358 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 359 int value) 360 { 361 /* GPIO can never have been requested */ 362 WARN_ON(1); 363 } 364 static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 365 struct gpio_desc **desc_array, 366 int *value_array) 367 { 368 /* GPIO can never have been requested */ 369 WARN_ON(1); 370 } 371 372 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 373 { 374 /* GPIO can never have been requested */ 375 WARN_ON(1); 376 return -ENOSYS; 377 } 378 379 static inline int gpiod_is_active_low(const struct gpio_desc *desc) 380 { 381 /* GPIO can never have been requested */ 382 WARN_ON(1); 383 return 0; 384 } 385 static inline int gpiod_cansleep(const struct gpio_desc *desc) 386 { 387 /* GPIO can never have been requested */ 388 WARN_ON(1); 389 return 0; 390 } 391 392 static inline int gpiod_to_irq(const struct gpio_desc *desc) 393 { 394 /* GPIO can never have been requested */ 395 WARN_ON(1); 396 return -EINVAL; 397 } 398 399 static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 400 { 401 return ERR_PTR(-EINVAL); 402 } 403 404 static inline int desc_to_gpio(const struct gpio_desc *desc) 405 { 406 /* GPIO can never have been requested */ 407 WARN_ON(1); 408 return -EINVAL; 409 } 410 411 /* Child properties interface */ 412 struct fwnode_handle; 413 414 static inline struct gpio_desc *fwnode_get_named_gpiod( 415 struct fwnode_handle *fwnode, const char *propname) 416 { 417 return ERR_PTR(-ENOSYS); 418 } 419 420 static inline struct gpio_desc *devm_get_gpiod_from_child( 421 struct device *dev, const char *con_id, struct fwnode_handle *child) 422 { 423 return ERR_PTR(-ENOSYS); 424 } 425 426 #endif /* CONFIG_GPIOLIB */ 427 428 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 429 430 int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 431 int gpiod_export_link(struct device *dev, const char *name, 432 struct gpio_desc *desc); 433 void gpiod_unexport(struct gpio_desc *desc); 434 435 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 436 437 static inline int gpiod_export(struct gpio_desc *desc, 438 bool direction_may_change) 439 { 440 return -ENOSYS; 441 } 442 443 static inline int gpiod_export_link(struct device *dev, const char *name, 444 struct gpio_desc *desc) 445 { 446 return -ENOSYS; 447 } 448 449 static inline void gpiod_unexport(struct gpio_desc *desc) 450 { 451 } 452 453 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 454 455 #endif 456