mxc_gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <asm/arch/imx-regs.h>
  12. #include <asm/gpio.h>
  13. #include <asm/io.h>
  14. #include <errno.h>
  15. enum mxc_gpio_direction {
  16. MXC_GPIO_DIRECTION_IN,
  17. MXC_GPIO_DIRECTION_OUT,
  18. };
  19. #define GPIO_TO_PORT(n) (n / 32)
  20. /* GPIO port description */
  21. static unsigned long gpio_ports[] = {
  22. [0] = GPIO1_BASE_ADDR,
  23. [1] = GPIO2_BASE_ADDR,
  24. [2] = GPIO3_BASE_ADDR,
  25. #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
  26. defined(CONFIG_MX53) || defined(CONFIG_MX6)
  27. [3] = GPIO4_BASE_ADDR,
  28. #endif
  29. #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
  30. [4] = GPIO5_BASE_ADDR,
  31. [5] = GPIO6_BASE_ADDR,
  32. #endif
  33. #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
  34. [6] = GPIO7_BASE_ADDR,
  35. #endif
  36. };
  37. static int mxc_gpio_direction(unsigned int gpio,
  38. enum mxc_gpio_direction direction)
  39. {
  40. unsigned int port = GPIO_TO_PORT(gpio);
  41. struct gpio_regs *regs;
  42. u32 l;
  43. if (port >= ARRAY_SIZE(gpio_ports))
  44. return -1;
  45. gpio &= 0x1f;
  46. regs = (struct gpio_regs *)gpio_ports[port];
  47. l = readl(&regs->gpio_dir);
  48. switch (direction) {
  49. case MXC_GPIO_DIRECTION_OUT:
  50. l |= 1 << gpio;
  51. break;
  52. case MXC_GPIO_DIRECTION_IN:
  53. l &= ~(1 << gpio);
  54. }
  55. writel(l, &regs->gpio_dir);
  56. return 0;
  57. }
  58. int gpio_set_value(unsigned gpio, int value)
  59. {
  60. unsigned int port = GPIO_TO_PORT(gpio);
  61. struct gpio_regs *regs;
  62. u32 l;
  63. if (port >= ARRAY_SIZE(gpio_ports))
  64. return -1;
  65. gpio &= 0x1f;
  66. regs = (struct gpio_regs *)gpio_ports[port];
  67. l = readl(&regs->gpio_dr);
  68. if (value)
  69. l |= 1 << gpio;
  70. else
  71. l &= ~(1 << gpio);
  72. writel(l, &regs->gpio_dr);
  73. return 0;
  74. }
  75. int gpio_get_value(unsigned gpio)
  76. {
  77. unsigned int port = GPIO_TO_PORT(gpio);
  78. struct gpio_regs *regs;
  79. u32 val;
  80. if (port >= ARRAY_SIZE(gpio_ports))
  81. return -1;
  82. gpio &= 0x1f;
  83. regs = (struct gpio_regs *)gpio_ports[port];
  84. val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
  85. return val;
  86. }
  87. int gpio_request(unsigned gpio, const char *label)
  88. {
  89. unsigned int port = GPIO_TO_PORT(gpio);
  90. if (port >= ARRAY_SIZE(gpio_ports))
  91. return -1;
  92. return 0;
  93. }
  94. int gpio_free(unsigned gpio)
  95. {
  96. return 0;
  97. }
  98. int gpio_direction_input(unsigned gpio)
  99. {
  100. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
  101. }
  102. int gpio_direction_output(unsigned gpio, int value)
  103. {
  104. int ret = gpio_set_value(gpio, value);
  105. if (ret < 0)
  106. return ret;
  107. return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
  108. }