mxc_gpio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2009
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * Copyright (C) 2011
  6. * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <asm/arch/imx-regs.h>
  28. #include <asm/gpio.h>
  29. #include <asm/io.h>
  30. #include <errno.h>
  31. enum mxc_gpio_direction {
  32. MXC_GPIO_DIRECTION_IN,
  33. MXC_GPIO_DIRECTION_OUT,
  34. };
  35. #define GPIO_TO_PORT(n) (n / 32)
  36. /* GPIO port description */
  37. static unsigned long gpio_ports[] = {
  38. [0] = GPIO1_BASE_ADDR,
  39. [1] = GPIO2_BASE_ADDR,
  40. [2] = GPIO3_BASE_ADDR,
  41. #if defined(CONFIG_MX51) || defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
  42. [3] = GPIO4_BASE_ADDR,
  43. #endif
  44. #if defined(CONFIG_MX53) || defined(CONFIG_MX6Q)
  45. [4] = GPIO5_BASE_ADDR,
  46. [5] = GPIO6_BASE_ADDR,
  47. [6] = GPIO7_BASE_ADDR,
  48. #endif
  49. };
  50. static int mxc_gpio_direction(unsigned int gpio,
  51. enum mxc_gpio_direction direction)
  52. {
  53. unsigned int port = GPIO_TO_PORT(gpio);
  54. struct gpio_regs *regs;
  55. u32 l;
  56. if (port >= ARRAY_SIZE(gpio_ports))
  57. return -1;
  58. gpio &= 0x1f;
  59. regs = (struct gpio_regs *)gpio_ports[port];
  60. l = readl(&regs->gpio_dir);
  61. switch (direction) {
  62. case MXC_GPIO_DIRECTION_OUT:
  63. l |= 1 << gpio;
  64. break;
  65. case MXC_GPIO_DIRECTION_IN:
  66. l &= ~(1 << gpio);
  67. }
  68. writel(l, &regs->gpio_dir);
  69. return 0;
  70. }
  71. int gpio_set_value(unsigned gpio, int value)
  72. {
  73. unsigned int port = GPIO_TO_PORT(gpio);
  74. struct gpio_regs *regs;
  75. u32 l;
  76. if (port >= ARRAY_SIZE(gpio_ports))
  77. return -1;
  78. gpio &= 0x1f;
  79. regs = (struct gpio_regs *)gpio_ports[port];
  80. l = readl(&regs->gpio_dr);
  81. if (value)
  82. l |= 1 << gpio;
  83. else
  84. l &= ~(1 << gpio);
  85. writel(l, &regs->gpio_dr);
  86. return 0;
  87. }
  88. int gpio_get_value(unsigned gpio)
  89. {
  90. unsigned int port = GPIO_TO_PORT(gpio);
  91. struct gpio_regs *regs;
  92. u32 val;
  93. if (port >= ARRAY_SIZE(gpio_ports))
  94. return -1;
  95. gpio &= 0x1f;
  96. regs = (struct gpio_regs *)gpio_ports[port];
  97. val = (readl(&regs->gpio_dr) >> gpio) & 0x01;
  98. return val;
  99. }
  100. int gpio_request(unsigned gpio, const char *label)
  101. {
  102. unsigned int port = GPIO_TO_PORT(gpio);
  103. if (port >= ARRAY_SIZE(gpio_ports))
  104. return -1;
  105. return 0;
  106. }
  107. int gpio_free(unsigned gpio)
  108. {
  109. return 0;
  110. }
  111. int gpio_direction_input(unsigned gpio)
  112. {
  113. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
  114. }
  115. int gpio_direction_output(unsigned gpio, int value)
  116. {
  117. int ret = mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
  118. if (ret < 0)
  119. return ret;
  120. return gpio_set_value(gpio, value);
  121. }