gpio.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Coldfire generic GPIO support
  3. *
  4. * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #ifndef coldfire_gpio_h
  16. #define coldfire_gpio_h
  17. #include <linux/io.h>
  18. #include <asm/coldfire.h>
  19. #include <asm/mcfsim.h>
  20. #include <asm/mcfgpio.h>
  21. /*
  22. * The Generic GPIO functions
  23. *
  24. * If the gpio is a compile time constant and is one of the Coldfire gpios,
  25. * use the inline version, otherwise dispatch thru gpiolib.
  26. */
  27. static inline int gpio_get_value(unsigned gpio)
  28. {
  29. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX)
  30. return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio);
  31. else
  32. return __gpio_get_value(gpio);
  33. }
  34. static inline void gpio_set_value(unsigned gpio, int value)
  35. {
  36. if (__builtin_constant_p(gpio) && gpio < MCFGPIO_PIN_MAX) {
  37. if (gpio < MCFGPIO_SCR_START) {
  38. unsigned long flags;
  39. MCFGPIO_PORTTYPE data;
  40. local_irq_save(flags);
  41. data = mcfgpio_read(__mcfgpio_podr(gpio));
  42. if (value)
  43. data |= mcfgpio_bit(gpio);
  44. else
  45. data &= ~mcfgpio_bit(gpio);
  46. mcfgpio_write(data, __mcfgpio_podr(gpio));
  47. local_irq_restore(flags);
  48. } else {
  49. if (value)
  50. mcfgpio_write(mcfgpio_bit(gpio),
  51. MCFGPIO_SETR_PORT(gpio));
  52. else
  53. mcfgpio_write(~mcfgpio_bit(gpio),
  54. MCFGPIO_CLRR_PORT(gpio));
  55. }
  56. } else
  57. __gpio_set_value(gpio, value);
  58. }
  59. static inline int gpio_to_irq(unsigned gpio)
  60. {
  61. #if defined(MCFGPIO_IRQ_MIN)
  62. if ((gpio >= MCFGPIO_IRQ_MIN) && (gpio < MCFGPIO_IRQ_MAX))
  63. #else
  64. if (gpio < MCFGPIO_IRQ_MAX)
  65. #endif
  66. return gpio + MCFGPIO_IRQ_VECBASE;
  67. else
  68. return __gpio_to_irq(gpio);
  69. }
  70. static inline int irq_to_gpio(unsigned irq)
  71. {
  72. return (irq >= MCFGPIO_IRQ_VECBASE &&
  73. irq < (MCFGPIO_IRQ_VECBASE + MCFGPIO_IRQ_MAX)) ?
  74. irq - MCFGPIO_IRQ_VECBASE : -ENXIO;
  75. }
  76. static inline int gpio_cansleep(unsigned gpio)
  77. {
  78. return gpio < MCFGPIO_PIN_MAX ? 0 : __gpio_cansleep(gpio);
  79. }
  80. #ifndef CONFIG_GPIOLIB
  81. static inline int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
  82. {
  83. int err;
  84. err = gpio_request(gpio, label);
  85. if (err)
  86. return err;
  87. if (flags & GPIOF_DIR_IN)
  88. err = gpio_direction_input(gpio);
  89. else
  90. err = gpio_direction_output(gpio,
  91. (flags & GPIOF_INIT_HIGH) ? 1 : 0);
  92. if (err)
  93. gpio_free(gpio);
  94. return err;
  95. }
  96. #endif /* !CONFIG_GPIOLIB */
  97. #endif