mxc_gpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /* GPIO port description */
  36. static unsigned long gpio_ports[] = {
  37. [0] = GPIO1_BASE_ADDR,
  38. [1] = GPIO2_BASE_ADDR,
  39. [2] = GPIO3_BASE_ADDR,
  40. #if defined(CONFIG_MX51) || defined(CONFIG_MX53)
  41. [3] = GPIO4_BASE_ADDR,
  42. #endif
  43. #if defined(CONFIG_MX53)
  44. [4] = GPIO5_BASE_ADDR,
  45. [5] = GPIO6_BASE_ADDR,
  46. [6] = GPIO7_BASE_ADDR,
  47. #endif
  48. };
  49. static int mxc_gpio_direction(unsigned int gpio,
  50. enum mxc_gpio_direction direction)
  51. {
  52. unsigned int port = gpio >> 5;
  53. struct gpio_regs *regs;
  54. u32 l;
  55. if (port >= ARRAY_SIZE(gpio_ports))
  56. return -EINVAL;
  57. gpio &= 0x1f;
  58. regs = (struct gpio_regs *)gpio_ports[port];
  59. l = readl(&regs->gpio_dir);
  60. switch (direction) {
  61. case MXC_GPIO_DIRECTION_OUT:
  62. l |= 1 << gpio;
  63. break;
  64. case MXC_GPIO_DIRECTION_IN:
  65. l &= ~(1 << gpio);
  66. }
  67. writel(l, &regs->gpio_dir);
  68. return 0;
  69. }
  70. void gpio_set_value(int gpio, int value)
  71. {
  72. unsigned int port = gpio >> 5;
  73. struct gpio_regs *regs;
  74. u32 l;
  75. if (port >= ARRAY_SIZE(gpio_ports))
  76. return;
  77. gpio &= 0x1f;
  78. regs = (struct gpio_regs *)gpio_ports[port];
  79. l = readl(&regs->gpio_dr);
  80. if (value)
  81. l |= 1 << gpio;
  82. else
  83. l &= ~(1 << gpio);
  84. writel(l, &regs->gpio_dr);
  85. }
  86. int gpio_get_value(int gpio)
  87. {
  88. unsigned int port = gpio >> 5;
  89. struct gpio_regs *regs;
  90. u32 l;
  91. if (port >= ARRAY_SIZE(gpio_ports))
  92. return -EINVAL;
  93. gpio &= 0x1f;
  94. regs = (struct gpio_regs *)gpio_ports[port];
  95. l = (readl(&regs->gpio_dr) >> gpio) & 0x01;
  96. return l;
  97. }
  98. int gpio_request(int gp, const char *label)
  99. {
  100. unsigned int port = gp >> 5;
  101. if (port >= ARRAY_SIZE(gpio_ports))
  102. return -EINVAL;
  103. return 0;
  104. }
  105. void gpio_free(int gp)
  106. {
  107. }
  108. void gpio_toggle_value(int gp)
  109. {
  110. gpio_set_value(gp, !gpio_get_value(gp));
  111. }
  112. int gpio_direction_input(int gp)
  113. {
  114. return mxc_gpio_direction(gp, MXC_GPIO_DIRECTION_IN);
  115. }
  116. int gpio_direction_output(int gp, int value)
  117. {
  118. int ret = mxc_gpio_direction(gp, MXC_GPIO_DIRECTION_OUT);
  119. if (ret < 0)
  120. return ret;
  121. gpio_set_value(gp, value);
  122. return 0;
  123. }