portmux-gpio.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2008 Atmel Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #ifndef __AVR32_PORTMUX_GPIO_H__
  23. #define __AVR32_PORTMUX_GPIO_H__
  24. #include <asm/io.h>
  25. /* Register layout for this specific device */
  26. #include <asm/arch/gpio-impl.h>
  27. /* Register access macros */
  28. #define gpio_readl(port, reg) \
  29. __raw_readl(&((struct gpio_regs *)port)->reg)
  30. #define gpio_writel(gpio, reg, value) \
  31. __raw_writel(value, &((struct gpio_regs *)port)->reg)
  32. /* Portmux API starts here. See doc/README.AVR32-port-muxing */
  33. enum portmux_function {
  34. PORTMUX_FUNC_A,
  35. PORTMUX_FUNC_B,
  36. PORTMUX_FUNC_C,
  37. PORTMUX_FUNC_D,
  38. };
  39. #define PORTMUX_DIR_INPUT (0 << 0)
  40. #define PORTMUX_DIR_OUTPUT (1 << 0)
  41. #define PORTMUX_INIT_LOW (0 << 1)
  42. #define PORTMUX_INIT_HIGH (1 << 1)
  43. #define PORTMUX_PULL_UP (1 << 2)
  44. #define PORTMUX_PULL_DOWN (2 << 2)
  45. #define PORTMUX_BUSKEEPER (3 << 2)
  46. #define PORTMUX_DRIVE_MIN (0 << 4)
  47. #define PORTMUX_DRIVE_LOW (1 << 4)
  48. #define PORTMUX_DRIVE_HIGH (2 << 4)
  49. #define PORTMUX_DRIVE_MAX (3 << 4)
  50. #define PORTMUX_OPEN_DRAIN (1 << 6)
  51. void portmux_select_peripheral(void *port, unsigned long pin_mask,
  52. enum portmux_function func, unsigned long flags);
  53. void portmux_select_gpio(void *port, unsigned long pin_mask,
  54. unsigned long flags);
  55. /* Internal helper functions */
  56. static inline void *gpio_pin_to_port(unsigned int pin)
  57. {
  58. return (void *)GPIO_BASE + (pin >> 5) * 0x200;
  59. }
  60. static inline void __gpio_set_output_value(void *port, unsigned int pin,
  61. int value)
  62. {
  63. if (value)
  64. gpio_writel(port, OVRS, 1 << pin);
  65. else
  66. gpio_writel(port, OVRC, 1 << pin);
  67. }
  68. static inline int __gpio_get_input_value(void *port, unsigned int pin)
  69. {
  70. return (gpio_readl(port, PVR) >> pin) & 1;
  71. }
  72. void gpio_set_output_value(unsigned int pin, int value);
  73. int gpio_get_input_value(unsigned int pin);
  74. /* GPIO API starts here */
  75. /*
  76. * GCC doesn't realize that the constant case is extremely trivial,
  77. * so we need to help it make the right decision by using
  78. * always_inline.
  79. */
  80. __attribute__((always_inline))
  81. static inline void gpio_set_value(unsigned int pin, int value)
  82. {
  83. if (__builtin_constant_p(pin))
  84. __gpio_set_output_value(gpio_pin_to_port(pin),
  85. pin & 0x1f, value);
  86. else
  87. gpio_set_output_value(pin, value);
  88. }
  89. __attribute__((always_inline))
  90. static inline int gpio_get_value(unsigned int pin)
  91. {
  92. if (__builtin_constant_p(pin))
  93. return __gpio_get_input_value(gpio_pin_to_port(pin),
  94. pin & 0x1f);
  95. else
  96. return gpio_get_input_value(pin);
  97. }
  98. #endif /* __AVR32_PORTMUX_GPIO_H__ */