gpio.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Coldfire generic GPIO support
  4. *
  5. * (C) Copyright 2009, Steven King <[email protected]>
  6. */
  7. #ifndef coldfire_gpio_h
  8. #define coldfire_gpio_h
  9. #include <linux/io.h>
  10. #include <asm/coldfire.h>
  11. #include <asm/mcfsim.h>
  12. #include <asm/mcfgpio.h>
  13. /*
  14. * The Generic GPIO functions
  15. *
  16. * If the gpio is a compile time constant and is one of the Coldfire gpios,
  17. * use the inline version, otherwise dispatch thru gpiolib.
  18. */
  19. static inline int gpio_get_value(unsigned gpio)
  20. {
  21. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX)
  22. return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio);
  23. else
  24. return __gpio_get_value(gpio);
  25. }
  26. static inline void gpio_set_value(unsigned gpio, int value)
  27. {
  28. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX) {
  29. if (gpio < MCFGPIO_SCR_START) {
  30. unsigned long flags;
  31. MCFGPIO_PORTTYPE data;
  32. local_irq_save(flags);
  33. data = mcfgpio_read(__mcfgpio_podr(gpio));
  34. if (value)
  35. data |= mcfgpio_bit(gpio);
  36. else
  37. data &= ~mcfgpio_bit(gpio);
  38. mcfgpio_write(data, __mcfgpio_podr(gpio));
  39. local_irq_restore(flags);
  40. } else {
  41. if (value)
  42. mcfgpio_write(mcfgpio_bit(gpio),
  43. MCFGPIO_SETR_PORT(gpio));
  44. else
  45. mcfgpio_write(~mcfgpio_bit(gpio),
  46. MCFGPIO_CLRR_PORT(gpio));
  47. }
  48. } else
  49. __gpio_set_value(gpio, value);
  50. }
  51. static inline int gpio_to_irq(unsigned gpio)
  52. {
  53. #if defined(MCFGPIO_IRQ_MIN)
  54. if ((gpio >= MCFGPIO_IRQ_MIN) && (gpio < MCFGPIO_IRQ_MAX))
  55. #else
  56. if (gpio < MCFGPIO_IRQ_MAX)
  57. #endif
  58. return gpio + MCFGPIO_IRQ_VECBASE;
  59. else
  60. return __gpio_to_irq(gpio);
  61. }
  62. static inline int irq_to_gpio(unsigned irq)
  63. {
  64. return (irq >= MCFGPIO_IRQ_VECBASE &&
  65. irq < (MCFGPIO_IRQ_VECBASE + MCFGPIO_IRQ_MAX)) ?
  66. irq - MCFGPIO_IRQ_VECBASE : -ENXIO;
  67. }
  68. static inline int gpio_cansleep(unsigned gpio)
  69. {
  70. return gpio < MCFGPIO_PIN_MAX ? 0 : __gpio_cansleep(gpio);
  71. }
  72. #ifndef CONFIG_GPIOLIB
  73. static inline int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  74. {
  75. int err;
  76. err = gpio_request(gpio, label);
  77. if (err)
  78. return err;
  79. if (flags & GPIOF_DIR_IN)
  80. err = gpio_direction_input(gpio);
  81. else
  82. err = gpio_direction_output(gpio,
  83. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  84. if (err)
  85. gpio_free(gpio);
  86. return err;
  87. }
  88. #endif /* !CONFIG_GPIOLIB */
  89. #endif