gpio.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * (C) Copyright 2009 Samsung Electronics
  3. * Minkyu Kang <mk7.kang@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <asm/io.h>
  22. #include <asm/arch/gpio.h>
  23. #define CON_MASK(x) (0xf << ((x) << 2))
  24. #define CON_SFR(x, v) ((v) << ((x) << 2))
  25. #define DAT_MASK(x) (0x1 << (x))
  26. #define DAT_SET(x) (0x1 << (x))
  27. #define PULL_MASK(x) (0x3 << ((x) << 1))
  28. #define PULL_MODE(x, v) ((v) << ((x) << 1))
  29. #define DRV_MASK(x) (0x3 << ((x) << 1))
  30. #define DRV_SET(x, m) ((m) << ((x) << 1))
  31. #define RATE_MASK(x) (0x1 << (x + 16))
  32. #define RATE_SET(x) (0x1 << (x + 16))
  33. void gpio_cfg_pin(struct s5pc1xx_gpio_bank *bank, int gpio, int cfg)
  34. {
  35. unsigned int value;
  36. value = readl(&bank->con);
  37. value &= ~CON_MASK(gpio);
  38. value |= CON_SFR(gpio, cfg);
  39. writel(value, &bank->con);
  40. }
  41. void gpio_direction_output(struct s5pc1xx_gpio_bank *bank, int gpio, int en)
  42. {
  43. unsigned int value;
  44. gpio_cfg_pin(bank, gpio, GPIO_OUTPUT);
  45. value = readl(&bank->dat);
  46. value &= ~DAT_MASK(gpio);
  47. if (en)
  48. value |= DAT_SET(gpio);
  49. writel(value, &bank->dat);
  50. }
  51. void gpio_direction_input(struct s5pc1xx_gpio_bank *bank, int gpio)
  52. {
  53. gpio_cfg_pin(bank, gpio, GPIO_INPUT);
  54. }
  55. void gpio_set_value(struct s5pc1xx_gpio_bank *bank, int gpio, int en)
  56. {
  57. unsigned int value;
  58. value = readl(&bank->dat);
  59. value &= ~DAT_MASK(gpio);
  60. if (en)
  61. value |= DAT_SET(gpio);
  62. writel(value, &bank->dat);
  63. }
  64. unsigned int gpio_get_value(struct s5pc1xx_gpio_bank *bank, int gpio)
  65. {
  66. unsigned int value;
  67. value = readl(&bank->dat);
  68. return !!(value & DAT_MASK(gpio));
  69. }
  70. void gpio_set_pull(struct s5pc1xx_gpio_bank *bank, int gpio, int mode)
  71. {
  72. unsigned int value;
  73. value = readl(&bank->pull);
  74. value &= ~PULL_MASK(gpio);
  75. switch (mode) {
  76. case GPIO_PULL_DOWN:
  77. case GPIO_PULL_UP:
  78. value |= PULL_MODE(gpio, mode);
  79. break;
  80. default:
  81. return;
  82. }
  83. writel(value, &bank->pull);
  84. }
  85. void gpio_set_drv(struct s5pc1xx_gpio_bank *bank, int gpio, int mode)
  86. {
  87. unsigned int value;
  88. value = readl(&bank->drv);
  89. value &= ~DRV_MASK(gpio);
  90. switch (mode) {
  91. case GPIO_DRV_1X:
  92. case GPIO_DRV_2X:
  93. case GPIO_DRV_3X:
  94. case GPIO_DRV_4X:
  95. value |= DRV_SET(gpio, mode);
  96. break;
  97. default:
  98. return;
  99. }
  100. writel(value, &bank->drv);
  101. }
  102. void gpio_set_rate(struct s5pc1xx_gpio_bank *bank, int gpio, int mode)
  103. {
  104. unsigned int value;
  105. value = readl(&bank->drv);
  106. value &= ~RATE_MASK(gpio);
  107. switch (mode) {
  108. case GPIO_DRV_FAST:
  109. case GPIO_DRV_SLOW:
  110. value |= RATE_SET(gpio);
  111. break;
  112. default:
  113. return;
  114. }
  115. writel(value, &bank->drv);
  116. }