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, int index, 139 enum gpiod_flags dflags, 140 const char *label); 141 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 142 const char *con_id, int index, 143 struct fwnode_handle *child, 144 enum gpiod_flags flags, 145 const char *label); 146 /* FIXME: delete this helper when users are switched over */ 147 static inline struct gpio_desc *devm_get_gpiod_from_child(struct device *dev, 148 const char *con_id, struct fwnode_handle *child) 149 { 150 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 151 0, child, 152 GPIOD_ASIS, 153 "?"); 154 } 155 156 #else /* CONFIG_GPIOLIB */ 157 158 static inline int gpiod_count(struct device *dev, const char *con_id) 159 { 160 return 0; 161 } 162 163 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 164 const char *con_id, 165 enum gpiod_flags flags) 166 { 167 return ERR_PTR(-ENOSYS); 168 } 169 static inline struct gpio_desc *__must_check 170 gpiod_get_index(struct device *dev, 171 const char *con_id, 172 unsigned int idx, 173 enum gpiod_flags flags) 174 { 175 return ERR_PTR(-ENOSYS); 176 } 177 178 static inline struct gpio_desc *__must_check 179 gpiod_get_optional(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_desc *__must_check 186 gpiod_get_index_optional(struct device *dev, const char *con_id, 187 unsigned int index, enum gpiod_flags flags) 188 { 189 return ERR_PTR(-ENOSYS); 190 } 191 192 static inline struct gpio_descs *__must_check 193 gpiod_get_array(struct device *dev, const char *con_id, 194 enum gpiod_flags flags) 195 { 196 return ERR_PTR(-ENOSYS); 197 } 198 199 static inline struct gpio_descs *__must_check 200 gpiod_get_array_optional(struct device *dev, const char *con_id, 201 enum gpiod_flags flags) 202 { 203 return ERR_PTR(-ENOSYS); 204 } 205 206 static inline void gpiod_put(struct gpio_desc *desc) 207 { 208 might_sleep(); 209 210 /* GPIO can never have been requested */ 211 WARN_ON(1); 212 } 213 214 static inline void gpiod_put_array(struct gpio_descs *descs) 215 { 216 might_sleep(); 217 218 /* GPIO can never have been requested */ 219 WARN_ON(1); 220 } 221 222 static inline struct gpio_desc *__must_check 223 devm_gpiod_get(struct device *dev, 224 const char *con_id, 225 enum gpiod_flags flags) 226 { 227 return ERR_PTR(-ENOSYS); 228 } 229 static inline 230 struct gpio_desc *__must_check 231 devm_gpiod_get_index(struct device *dev, 232 const char *con_id, 233 unsigned int idx, 234 enum gpiod_flags flags) 235 { 236 return ERR_PTR(-ENOSYS); 237 } 238 239 static inline struct gpio_desc *__must_check 240 devm_gpiod_get_optional(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_desc *__must_check 247 devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 248 unsigned int index, enum gpiod_flags flags) 249 { 250 return ERR_PTR(-ENOSYS); 251 } 252 253 static inline struct gpio_descs *__must_check 254 devm_gpiod_get_array(struct device *dev, const char *con_id, 255 enum gpiod_flags flags) 256 { 257 return ERR_PTR(-ENOSYS); 258 } 259 260 static inline struct gpio_descs *__must_check 261 devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 262 enum gpiod_flags flags) 263 { 264 return ERR_PTR(-ENOSYS); 265 } 266 267 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 268 { 269 might_sleep(); 270 271 /* GPIO can never have been requested */ 272 WARN_ON(1); 273 } 274 275 static inline void devm_gpiod_put_array(struct device *dev, 276 struct gpio_descs *descs) 277 { 278 might_sleep(); 279 280 /* GPIO can never have been requested */ 281 WARN_ON(1); 282 } 283 284 285 static inline int gpiod_get_direction(const struct gpio_desc *desc) 286 { 287 /* GPIO can never have been requested */ 288 WARN_ON(1); 289 return -ENOSYS; 290 } 291 static inline int gpiod_direction_input(struct gpio_desc *desc) 292 { 293 /* GPIO can never have been requested */ 294 WARN_ON(1); 295 return -ENOSYS; 296 } 297 static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 298 { 299 /* GPIO can never have been requested */ 300 WARN_ON(1); 301 return -ENOSYS; 302 } 303 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 304 { 305 /* GPIO can never have been requested */ 306 WARN_ON(1); 307 return -ENOSYS; 308 } 309 310 311 static inline int gpiod_get_value(const struct gpio_desc *desc) 312 { 313 /* GPIO can never have been requested */ 314 WARN_ON(1); 315 return 0; 316 } 317 static inline void gpiod_set_value(struct gpio_desc *desc, int value) 318 { 319 /* GPIO can never have been requested */ 320 WARN_ON(1); 321 } 322 static inline void gpiod_set_array_value(unsigned int array_size, 323 struct gpio_desc **desc_array, 324 int *value_array) 325 { 326 /* GPIO can never have been requested */ 327 WARN_ON(1); 328 } 329 static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 330 { 331 /* GPIO can never have been requested */ 332 WARN_ON(1); 333 return 0; 334 } 335 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 336 { 337 /* GPIO can never have been requested */ 338 WARN_ON(1); 339 } 340 static inline void gpiod_set_raw_array_value(unsigned int array_size, 341 struct gpio_desc **desc_array, 342 int *value_array) 343 { 344 /* GPIO can never have been requested */ 345 WARN_ON(1); 346 } 347 348 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 349 { 350 /* GPIO can never have been requested */ 351 WARN_ON(1); 352 return 0; 353 } 354 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 355 { 356 /* GPIO can never have been requested */ 357 WARN_ON(1); 358 } 359 static inline void gpiod_set_array_value_cansleep(unsigned int array_size, 360 struct gpio_desc **desc_array, 361 int *value_array) 362 { 363 /* GPIO can never have been requested */ 364 WARN_ON(1); 365 } 366 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 367 { 368 /* GPIO can never have been requested */ 369 WARN_ON(1); 370 return 0; 371 } 372 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 373 int value) 374 { 375 /* GPIO can never have been requested */ 376 WARN_ON(1); 377 } 378 static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 379 struct gpio_desc **desc_array, 380 int *value_array) 381 { 382 /* GPIO can never have been requested */ 383 WARN_ON(1); 384 } 385 386 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 387 { 388 /* GPIO can never have been requested */ 389 WARN_ON(1); 390 return -ENOSYS; 391 } 392 393 static inline int gpiod_is_active_low(const struct gpio_desc *desc) 394 { 395 /* GPIO can never have been requested */ 396 WARN_ON(1); 397 return 0; 398 } 399 static inline int gpiod_cansleep(const struct gpio_desc *desc) 400 { 401 /* GPIO can never have been requested */ 402 WARN_ON(1); 403 return 0; 404 } 405 406 static inline int gpiod_to_irq(const struct gpio_desc *desc) 407 { 408 /* GPIO can never have been requested */ 409 WARN_ON(1); 410 return -EINVAL; 411 } 412 413 static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 414 { 415 return ERR_PTR(-EINVAL); 416 } 417 418 static inline int desc_to_gpio(const struct gpio_desc *desc) 419 { 420 /* GPIO can never have been requested */ 421 WARN_ON(1); 422 return -EINVAL; 423 } 424 425 /* Child properties interface */ 426 struct fwnode_handle; 427 428 static inline 429 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 430 const char *propname, int index, 431 enum gpiod_flags dflags, 432 const char *label) 433 { 434 return ERR_PTR(-ENOSYS); 435 } 436 437 static inline 438 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 439 const char *con_id, int index, 440 struct fwnode_handle *child, 441 enum gpiod_flags flags, 442 const char *label) 443 { 444 return ERR_PTR(-ENOSYS); 445 } 446 447 /* FIXME: delete this when all users are switched over */ 448 static inline struct gpio_desc *devm_get_gpiod_from_child(struct device *dev, 449 const char *con_id, struct fwnode_handle *child) 450 { 451 return ERR_PTR(-ENOSYS); 452 } 453 454 #endif /* CONFIG_GPIOLIB */ 455 456 static inline 457 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev, 458 const char *con_id, 459 struct fwnode_handle *child, 460 enum gpiod_flags flags, 461 const char *label) 462 { 463 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child, 464 flags, label); 465 } 466 467 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 468 469 int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 470 int gpiod_export_link(struct device *dev, const char *name, 471 struct gpio_desc *desc); 472 void gpiod_unexport(struct gpio_desc *desc); 473 474 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 475 476 static inline int gpiod_export(struct gpio_desc *desc, 477 bool direction_may_change) 478 { 479 return -ENOSYS; 480 } 481 482 static inline int gpiod_export_link(struct device *dev, const char *name, 483 struct gpio_desc *desc) 484 { 485 return -ENOSYS; 486 } 487 488 static inline void gpiod_unexport(struct gpio_desc *desc) 489 { 490 } 491 492 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 493 494 #endif 495