iomux-mx1-mx2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * arch/arm/mach-mxc/generic.c
  3. *
  4. * author: Sascha Hauer
  5. * Created: april 20th, 2004
  6. * Copyright: Synertronixx GmbH
  7. *
  8. * Common code for i.MX machines
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/string.h>
  30. #include <linux/gpio.h>
  31. #include <mach/hardware.h>
  32. #include <asm/mach/map.h>
  33. #include <mach/iomux-mx1-mx2.h>
  34. void mxc_gpio_mode(int gpio_mode)
  35. {
  36. unsigned int pin = gpio_mode & GPIO_PIN_MASK;
  37. unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
  38. unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
  39. unsigned int tmp;
  40. /* Pullup enable */
  41. tmp = __raw_readl(VA_GPIO_BASE + MXC_PUEN(port));
  42. if (gpio_mode & GPIO_PUEN)
  43. tmp |= (1 << pin);
  44. else
  45. tmp &= ~(1 << pin);
  46. __raw_writel(tmp, VA_GPIO_BASE + MXC_PUEN(port));
  47. /* Data direction */
  48. tmp = __raw_readl(VA_GPIO_BASE + MXC_DDIR(port));
  49. if (gpio_mode & GPIO_OUT)
  50. tmp |= 1 << pin;
  51. else
  52. tmp &= ~(1 << pin);
  53. __raw_writel(tmp, VA_GPIO_BASE + MXC_DDIR(port));
  54. /* Primary / alternate function */
  55. tmp = __raw_readl(VA_GPIO_BASE + MXC_GPR(port));
  56. if (gpio_mode & GPIO_AF)
  57. tmp |= (1 << pin);
  58. else
  59. tmp &= ~(1 << pin);
  60. __raw_writel(tmp, VA_GPIO_BASE + MXC_GPR(port));
  61. /* use as gpio? */
  62. tmp = __raw_readl(VA_GPIO_BASE + MXC_GIUS(port));
  63. if (gpio_mode & (GPIO_PF | GPIO_AF))
  64. tmp &= ~(1 << pin);
  65. else
  66. tmp |= (1 << pin);
  67. __raw_writel(tmp, VA_GPIO_BASE + MXC_GIUS(port));
  68. if (pin < 16) {
  69. tmp = __raw_readl(VA_GPIO_BASE + MXC_OCR1(port));
  70. tmp &= ~(3 << (pin * 2));
  71. tmp |= (ocr << (pin * 2));
  72. __raw_writel(tmp, VA_GPIO_BASE + MXC_OCR1(port));
  73. tmp = __raw_readl(VA_GPIO_BASE + MXC_ICONFA1(port));
  74. tmp &= ~(3 << (pin * 2));
  75. tmp |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2);
  76. __raw_writel(tmp, VA_GPIO_BASE + MXC_ICONFA1(port));
  77. tmp = __raw_readl(VA_GPIO_BASE + MXC_ICONFB1(port));
  78. tmp &= ~(3 << (pin * 2));
  79. tmp |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2);
  80. __raw_writel(tmp, VA_GPIO_BASE + MXC_ICONFB1(port));
  81. } else {
  82. pin -= 16;
  83. tmp = __raw_readl(VA_GPIO_BASE + MXC_OCR2(port));
  84. tmp &= ~(3 << (pin * 2));
  85. tmp |= (ocr << (pin * 2));
  86. __raw_writel(tmp, VA_GPIO_BASE + MXC_OCR2(port));
  87. tmp = __raw_readl(VA_GPIO_BASE + MXC_ICONFA2(port));
  88. tmp &= ~(3 << (pin * 2));
  89. tmp |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2);
  90. __raw_writel(tmp, VA_GPIO_BASE + MXC_ICONFA2(port));
  91. tmp = __raw_readl(VA_GPIO_BASE + MXC_ICONFB2(port));
  92. tmp &= ~(3 << (pin * 2));
  93. tmp |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2);
  94. __raw_writel(tmp, VA_GPIO_BASE + MXC_ICONFB2(port));
  95. }
  96. }
  97. EXPORT_SYMBOL(mxc_gpio_mode);
  98. int mxc_gpio_setup_multiple_pins(const int *pin_list, unsigned count,
  99. int alloc_mode, const char *label)
  100. {
  101. const int *p = pin_list;
  102. int i;
  103. unsigned gpio;
  104. unsigned mode;
  105. for (i = 0; i < count; i++) {
  106. gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
  107. mode = *p & ~(GPIO_PIN_MASK | GPIO_PORT_MASK);
  108. if (gpio >= (GPIO_PORT_MAX + 1) * 32)
  109. goto setup_error;
  110. if (alloc_mode & MXC_GPIO_ALLOC_MODE_RELEASE)
  111. gpio_free(gpio);
  112. else if (!(alloc_mode & MXC_GPIO_ALLOC_MODE_NO_ALLOC))
  113. if (gpio_request(gpio, label)
  114. && !(alloc_mode & MXC_GPIO_ALLOC_MODE_TRY_ALLOC))
  115. goto setup_error;
  116. if (!(alloc_mode & (MXC_GPIO_ALLOC_MODE_ALLOC_ONLY |
  117. MXC_GPIO_ALLOC_MODE_RELEASE)))
  118. mxc_gpio_mode(gpio | mode);
  119. p++;
  120. }
  121. return 0;
  122. setup_error:
  123. if (alloc_mode & (MXC_GPIO_ALLOC_MODE_NO_ALLOC |
  124. MXC_GPIO_ALLOC_MODE_TRY_ALLOC))
  125. return -EINVAL;
  126. while (p != pin_list) {
  127. p--;
  128. gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
  129. gpio_free(gpio);
  130. }
  131. return -EINVAL;
  132. }
  133. EXPORT_SYMBOL(mxc_gpio_setup_multiple_pins);