gpio.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #endif