gpio-fsm9xxx.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/gpio.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <mach/gpiomux.h>
  18. #include <mach/msm_iomap.h>
  19. /* see 80-VA736-2 Rev C pp 695-751
  20. **
  21. ** These are actually the *shadow* gpio registers, since the
  22. ** real ones (which allow full access) are only available to the
  23. ** ARM9 side of the world.
  24. **
  25. ** Since the _BASE need to be page-aligned when we're mapping them
  26. ** to virtual addresses, adjust for the additional offset in these
  27. ** macros.
  28. */
  29. #if defined(CONFIG_ARCH_FSM9XXX)
  30. #define MSM_GPIO1_REG(off) (MSM_TLMM_BASE + (off))
  31. #endif
  32. #if defined(CONFIG_ARCH_FSM9XXX)
  33. /* output value */
  34. #define MSM_GPIO_OUT_G(group) MSM_GPIO1_REG(0x00 + (group) * 4)
  35. #define MSM_GPIO_OUT_N(gpio) MSM_GPIO_OUT_G((gpio) / 32)
  36. #define MSM_GPIO_OUT_0 MSM_GPIO_OUT_G(0) /* gpio 31-0 */
  37. #define MSM_GPIO_OUT_1 MSM_GPIO_OUT_G(1) /* gpio 63-32 */
  38. #define MSM_GPIO_OUT_2 MSM_GPIO_OUT_G(2) /* gpio 95-64 */
  39. #define MSM_GPIO_OUT_3 MSM_GPIO_OUT_G(3) /* gpio 127-96 */
  40. #define MSM_GPIO_OUT_4 MSM_GPIO_OUT_G(4) /* gpio 159-128 */
  41. #define MSM_GPIO_OUT_5 MSM_GPIO_OUT_G(5) /* gpio 167-160 */
  42. /* same pin map as above, output enable */
  43. #define MSM_GPIO_OE_G(group) MSM_GPIO1_REG(0x20 + (group) * 4)
  44. #define MSM_GPIO_OE_N(gpio) MSM_GPIO_OE_G((gpio) / 32)
  45. #define MSM_GPIO_OE_0 MSM_GPIO_OE_G(0)
  46. #define MSM_GPIO_OE_1 MSM_GPIO_OE_G(1)
  47. #define MSM_GPIO_OE_2 MSM_GPIO_OE_G(2)
  48. #define MSM_GPIO_OE_3 MSM_GPIO_OE_G(3)
  49. #define MSM_GPIO_OE_4 MSM_GPIO_OE_G(4)
  50. #define MSM_GPIO_OE_5 MSM_GPIO_OE_G(5)
  51. /* same pin map as above, input read */
  52. #define MSM_GPIO_IN_G(group) MSM_GPIO1_REG(0x48 + (group) * 4)
  53. #define MSM_GPIO_IN_N(gpio) MSM_GPIO_IN_G((gpio) / 32)
  54. #define MSM_GPIO_IN_0 MSM_GPIO_IN_G(0)
  55. #define MSM_GPIO_IN_1 MSM_GPIO_IN_G(1)
  56. #define MSM_GPIO_IN_2 MSM_GPIO_IN_G(2)
  57. #define MSM_GPIO_IN_3 MSM_GPIO_IN_G(3)
  58. #define MSM_GPIO_IN_4 MSM_GPIO_IN_G(4)
  59. #define MSM_GPIO_IN_5 MSM_GPIO_IN_G(5)
  60. /* configuration */
  61. #define MSM_GPIO_PAGE MSM_GPIO1_REG(0x40)
  62. #define MSM_GPIO_CONFIG MSM_GPIO1_REG(0x44)
  63. #endif /* CONFIG_ARCH_FSM9XXX */
  64. #define MSM_GPIO_BANK(bank, first, last) \
  65. { \
  66. .regs = { \
  67. .out = MSM_GPIO_OUT_##bank, \
  68. .in = MSM_GPIO_IN_##bank, \
  69. .oe = MSM_GPIO_OE_##bank, \
  70. }, \
  71. .chip = { \
  72. .base = (first), \
  73. .ngpio = (last) - (first) + 1, \
  74. .get = msm_gpio_get, \
  75. .set = msm_gpio_set, \
  76. .direction_input = msm_gpio_direction_input, \
  77. .direction_output = msm_gpio_direction_output, \
  78. .request = msm_gpio_request, \
  79. .free = msm_gpio_free, \
  80. } \
  81. }
  82. struct msm_gpio_regs {
  83. void __iomem *out;
  84. void __iomem *in;
  85. void __iomem *oe;
  86. };
  87. struct msm_gpio_chip {
  88. spinlock_t lock;
  89. struct gpio_chip chip;
  90. struct msm_gpio_regs regs;
  91. };
  92. static int msm_gpio_write(struct msm_gpio_chip *msm_chip,
  93. unsigned offset, unsigned on)
  94. {
  95. unsigned mask = BIT(offset);
  96. unsigned val;
  97. val = __raw_readl(msm_chip->regs.out);
  98. if (on)
  99. __raw_writel(val | mask, msm_chip->regs.out);
  100. else
  101. __raw_writel(val & ~mask, msm_chip->regs.out);
  102. return 0;
  103. }
  104. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  105. {
  106. struct msm_gpio_chip *msm_chip;
  107. unsigned long irq_flags;
  108. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  109. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  110. __raw_writel(__raw_readl(msm_chip->regs.oe) & ~BIT(offset),
  111. msm_chip->regs.oe);
  112. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  113. return 0;
  114. }
  115. static int
  116. msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  117. {
  118. struct msm_gpio_chip *msm_chip;
  119. unsigned long irq_flags;
  120. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  121. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  122. msm_gpio_write(msm_chip, offset, value);
  123. __raw_writel(__raw_readl(msm_chip->regs.oe) | BIT(offset),
  124. msm_chip->regs.oe);
  125. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  126. return 0;
  127. }
  128. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  129. {
  130. struct msm_gpio_chip *msm_chip;
  131. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  132. return (__raw_readl(msm_chip->regs.in) & (1U << offset)) ? 1 : 0;
  133. }
  134. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  135. {
  136. struct msm_gpio_chip *msm_chip;
  137. unsigned long irq_flags;
  138. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  139. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  140. msm_gpio_write(msm_chip, offset, value);
  141. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  142. }
  143. #ifdef CONFIG_MSM_GPIOMUX
  144. static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
  145. {
  146. return msm_gpiomux_get(chip->base + offset);
  147. }
  148. static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
  149. {
  150. msm_gpiomux_put(chip->base + offset);
  151. }
  152. #else
  153. #define msm_gpio_request NULL
  154. #define msm_gpio_free NULL
  155. #endif
  156. struct msm_gpio_chip msm_gpio_chips[] = {
  157. MSM_GPIO_BANK(0, 0, 31),
  158. MSM_GPIO_BANK(1, 32, 63),
  159. MSM_GPIO_BANK(2, 64, 95),
  160. MSM_GPIO_BANK(3, 96, 127),
  161. MSM_GPIO_BANK(4, 128, 159),
  162. MSM_GPIO_BANK(5, 160, 167),
  163. };
  164. void msm_gpio_enter_sleep(int from_idle)
  165. {
  166. return;
  167. }
  168. void msm_gpio_exit_sleep(void)
  169. {
  170. return;
  171. }
  172. static int __init msm_init_gpio(void)
  173. {
  174. int i;
  175. for (i = 0; i < ARRAY_SIZE(msm_gpio_chips); i++) {
  176. spin_lock_init(&msm_gpio_chips[i].lock);
  177. gpiochip_add(&msm_gpio_chips[i].chip);
  178. }
  179. return 0;
  180. }
  181. postcore_initcall(msm_init_gpio);
  182. int gpio_tlmm_config(unsigned config, unsigned disable)
  183. {
  184. uint32_t flags;
  185. unsigned gpio = GPIO_PIN(config);
  186. if (gpio > NR_MSM_GPIOS)
  187. return -EINVAL;
  188. flags = ((GPIO_DRVSTR(config) << 6) & (0x7 << 6)) |
  189. ((GPIO_FUNC(config) << 2) & (0xf << 2)) |
  190. ((GPIO_PULL(config) & 0x3));
  191. dsb();
  192. __raw_writel(gpio, MSM_GPIO_PAGE);
  193. dsb();
  194. __raw_writel(flags, MSM_GPIO_CONFIG);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(gpio_tlmm_config);
  198. int msm_gpios_request_enable(const struct msm_gpio *table, int size)
  199. {
  200. int rc = msm_gpios_request(table, size);
  201. if (rc)
  202. return rc;
  203. rc = msm_gpios_enable(table, size);
  204. if (rc)
  205. msm_gpios_free(table, size);
  206. return rc;
  207. }
  208. EXPORT_SYMBOL(msm_gpios_request_enable);
  209. void msm_gpios_disable_free(const struct msm_gpio *table, int size)
  210. {
  211. msm_gpios_disable(table, size);
  212. msm_gpios_free(table, size);
  213. }
  214. EXPORT_SYMBOL(msm_gpios_disable_free);
  215. int msm_gpios_request(const struct msm_gpio *table, int size)
  216. {
  217. int rc;
  218. int i;
  219. const struct msm_gpio *g;
  220. for (i = 0; i < size; i++) {
  221. g = table + i;
  222. rc = gpio_request(GPIO_PIN(g->gpio_cfg), g->label);
  223. if (rc) {
  224. pr_err("gpio_request(%d) <%s> failed: %d\n",
  225. GPIO_PIN(g->gpio_cfg), g->label ?: "?", rc);
  226. goto err;
  227. }
  228. }
  229. return 0;
  230. err:
  231. msm_gpios_free(table, i);
  232. return rc;
  233. }
  234. EXPORT_SYMBOL(msm_gpios_request);
  235. void msm_gpios_free(const struct msm_gpio *table, int size)
  236. {
  237. int i;
  238. const struct msm_gpio *g;
  239. for (i = size-1; i >= 0; i--) {
  240. g = table + i;
  241. gpio_free(GPIO_PIN(g->gpio_cfg));
  242. }
  243. }
  244. EXPORT_SYMBOL(msm_gpios_free);
  245. int msm_gpios_enable(const struct msm_gpio *table, int size)
  246. {
  247. int rc;
  248. int i;
  249. const struct msm_gpio *g;
  250. for (i = 0; i < size; i++) {
  251. g = table + i;
  252. rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
  253. if (rc) {
  254. pr_err("gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE)"
  255. " <%s> failed: %d\n",
  256. g->gpio_cfg, g->label ?: "?", rc);
  257. pr_err("pin %d func %d dir %d pull %d drvstr %d\n",
  258. GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
  259. GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
  260. GPIO_DRVSTR(g->gpio_cfg));
  261. goto err;
  262. }
  263. }
  264. return 0;
  265. err:
  266. msm_gpios_disable(table, i);
  267. return rc;
  268. }
  269. EXPORT_SYMBOL(msm_gpios_enable);
  270. int msm_gpios_disable(const struct msm_gpio *table, int size)
  271. {
  272. int rc = 0;
  273. int i;
  274. const struct msm_gpio *g;
  275. for (i = size-1; i >= 0; i--) {
  276. int tmp;
  277. g = table + i;
  278. tmp = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_DISABLE);
  279. if (tmp) {
  280. pr_err("gpio_tlmm_config(0x%08x, GPIO_CFG_DISABLE)"
  281. " <%s> failed: %d\n",
  282. g->gpio_cfg, g->label ?: "?", rc);
  283. pr_err("pin %d func %d dir %d pull %d drvstr %d\n",
  284. GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
  285. GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
  286. GPIO_DRVSTR(g->gpio_cfg));
  287. if (!rc)
  288. rc = tmp;
  289. }
  290. }
  291. return rc;
  292. }
  293. EXPORT_SYMBOL(msm_gpios_disable);