gpio-msm-v1.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /* linux/arch/arm/mach-msm/gpio.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  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. */
  16. #include <linux/bitops.h>
  17. #include <linux/gpio.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/irq.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <asm/mach/irq.h>
  24. #include <mach/gpiomux.h>
  25. #include <mach/msm_iomap.h>
  26. #include <mach/msm_smem.h>
  27. #include <mach/proc_comm.h>
  28. /* see 80-VA736-2 Rev C pp 695-751
  29. **
  30. ** These are actually the *shadow* gpio registers, since the
  31. ** real ones (which allow full access) are only available to the
  32. ** ARM9 side of the world.
  33. **
  34. ** Since the _BASE need to be page-aligned when we're mapping them
  35. ** to virtual addresses, adjust for the additional offset in these
  36. ** macros.
  37. */
  38. #if defined(CONFIG_ARCH_MSM7X30)
  39. #define MSM_GPIO1_REG(off) (MSM_GPIO1_BASE + (off))
  40. #define MSM_GPIO2_REG(off) (MSM_GPIO2_BASE + 0x400 + (off))
  41. #else
  42. #define MSM_GPIO1_REG(off) (MSM_GPIO1_BASE + 0x800 + (off))
  43. #define MSM_GPIO2_REG(off) (MSM_GPIO2_BASE + 0xC00 + (off))
  44. #endif
  45. #if defined(CONFIG_ARCH_MSM7X00A) || defined(CONFIG_ARCH_MSM7X25) ||\
  46. defined(CONFIG_ARCH_MSM7X27)
  47. /* output value */
  48. #define MSM_GPIO_OUT_0 MSM_GPIO1_REG(0x00) /* gpio 15-0 */
  49. #define MSM_GPIO_OUT_1 MSM_GPIO2_REG(0x00) /* gpio 42-16 */
  50. #define MSM_GPIO_OUT_2 MSM_GPIO1_REG(0x04) /* gpio 67-43 */
  51. #define MSM_GPIO_OUT_3 MSM_GPIO1_REG(0x08) /* gpio 94-68 */
  52. #define MSM_GPIO_OUT_4 MSM_GPIO1_REG(0x0C) /* gpio 106-95 */
  53. #define MSM_GPIO_OUT_5 MSM_GPIO1_REG(0x50) /* gpio 107-121 */
  54. /* same pin map as above, output enable */
  55. #define MSM_GPIO_OE_0 MSM_GPIO1_REG(0x10)
  56. #define MSM_GPIO_OE_1 MSM_GPIO2_REG(0x08)
  57. #define MSM_GPIO_OE_2 MSM_GPIO1_REG(0x14)
  58. #define MSM_GPIO_OE_3 MSM_GPIO1_REG(0x18)
  59. #define MSM_GPIO_OE_4 MSM_GPIO1_REG(0x1C)
  60. #define MSM_GPIO_OE_5 MSM_GPIO1_REG(0x54)
  61. /* same pin map as above, input read */
  62. #define MSM_GPIO_IN_0 MSM_GPIO1_REG(0x34)
  63. #define MSM_GPIO_IN_1 MSM_GPIO2_REG(0x20)
  64. #define MSM_GPIO_IN_2 MSM_GPIO1_REG(0x38)
  65. #define MSM_GPIO_IN_3 MSM_GPIO1_REG(0x3C)
  66. #define MSM_GPIO_IN_4 MSM_GPIO1_REG(0x40)
  67. #define MSM_GPIO_IN_5 MSM_GPIO1_REG(0x44)
  68. /* same pin map as above, 1=edge 0=level interrup */
  69. #define MSM_GPIO_INT_EDGE_0 MSM_GPIO1_REG(0x60)
  70. #define MSM_GPIO_INT_EDGE_1 MSM_GPIO2_REG(0x50)
  71. #define MSM_GPIO_INT_EDGE_2 MSM_GPIO1_REG(0x64)
  72. #define MSM_GPIO_INT_EDGE_3 MSM_GPIO1_REG(0x68)
  73. #define MSM_GPIO_INT_EDGE_4 MSM_GPIO1_REG(0x6C)
  74. #define MSM_GPIO_INT_EDGE_5 MSM_GPIO1_REG(0xC0)
  75. /* same pin map as above, 1=positive 0=negative */
  76. #define MSM_GPIO_INT_POS_0 MSM_GPIO1_REG(0x70)
  77. #define MSM_GPIO_INT_POS_1 MSM_GPIO2_REG(0x58)
  78. #define MSM_GPIO_INT_POS_2 MSM_GPIO1_REG(0x74)
  79. #define MSM_GPIO_INT_POS_3 MSM_GPIO1_REG(0x78)
  80. #define MSM_GPIO_INT_POS_4 MSM_GPIO1_REG(0x7C)
  81. #define MSM_GPIO_INT_POS_5 MSM_GPIO1_REG(0xBC)
  82. /* same pin map as above, interrupt enable */
  83. #define MSM_GPIO_INT_EN_0 MSM_GPIO1_REG(0x80)
  84. #define MSM_GPIO_INT_EN_1 MSM_GPIO2_REG(0x60)
  85. #define MSM_GPIO_INT_EN_2 MSM_GPIO1_REG(0x84)
  86. #define MSM_GPIO_INT_EN_3 MSM_GPIO1_REG(0x88)
  87. #define MSM_GPIO_INT_EN_4 MSM_GPIO1_REG(0x8C)
  88. #define MSM_GPIO_INT_EN_5 MSM_GPIO1_REG(0xB8)
  89. /* same pin map as above, write 1 to clear interrupt */
  90. #define MSM_GPIO_INT_CLEAR_0 MSM_GPIO1_REG(0x90)
  91. #define MSM_GPIO_INT_CLEAR_1 MSM_GPIO2_REG(0x68)
  92. #define MSM_GPIO_INT_CLEAR_2 MSM_GPIO1_REG(0x94)
  93. #define MSM_GPIO_INT_CLEAR_3 MSM_GPIO1_REG(0x98)
  94. #define MSM_GPIO_INT_CLEAR_4 MSM_GPIO1_REG(0x9C)
  95. #define MSM_GPIO_INT_CLEAR_5 MSM_GPIO1_REG(0xB4)
  96. /* same pin map as above, 1=interrupt pending */
  97. #define MSM_GPIO_INT_STATUS_0 MSM_GPIO1_REG(0xA0)
  98. #define MSM_GPIO_INT_STATUS_1 MSM_GPIO2_REG(0x70)
  99. #define MSM_GPIO_INT_STATUS_2 MSM_GPIO1_REG(0xA4)
  100. #define MSM_GPIO_INT_STATUS_3 MSM_GPIO1_REG(0xA8)
  101. #define MSM_GPIO_INT_STATUS_4 MSM_GPIO1_REG(0xAC)
  102. #define MSM_GPIO_INT_STATUS_5 MSM_GPIO1_REG(0xB0)
  103. #endif
  104. #if defined(CONFIG_ARCH_MSM7X30)
  105. /* output value */
  106. #define MSM_GPIO_OUT_0 MSM_GPIO1_REG(0x00) /* gpio 15-0 */
  107. #define MSM_GPIO_OUT_1 MSM_GPIO2_REG(0x00) /* gpio 43-16 */
  108. #define MSM_GPIO_OUT_2 MSM_GPIO1_REG(0x04) /* gpio 67-44 */
  109. #define MSM_GPIO_OUT_3 MSM_GPIO1_REG(0x08) /* gpio 94-68 */
  110. #define MSM_GPIO_OUT_4 MSM_GPIO1_REG(0x0C) /* gpio 106-95 */
  111. #define MSM_GPIO_OUT_5 MSM_GPIO1_REG(0x50) /* gpio 133-107 */
  112. #define MSM_GPIO_OUT_6 MSM_GPIO1_REG(0xC4) /* gpio 150-134 */
  113. #define MSM_GPIO_OUT_7 MSM_GPIO1_REG(0x214) /* gpio 181-151 */
  114. /* same pin map as above, output enable */
  115. #define MSM_GPIO_OE_0 MSM_GPIO1_REG(0x10)
  116. #define MSM_GPIO_OE_1 MSM_GPIO2_REG(0x08)
  117. #define MSM_GPIO_OE_2 MSM_GPIO1_REG(0x14)
  118. #define MSM_GPIO_OE_3 MSM_GPIO1_REG(0x18)
  119. #define MSM_GPIO_OE_4 MSM_GPIO1_REG(0x1C)
  120. #define MSM_GPIO_OE_5 MSM_GPIO1_REG(0x54)
  121. #define MSM_GPIO_OE_6 MSM_GPIO1_REG(0xC8)
  122. #define MSM_GPIO_OE_7 MSM_GPIO1_REG(0x218)
  123. /* same pin map as above, input read */
  124. #define MSM_GPIO_IN_0 MSM_GPIO1_REG(0x34)
  125. #define MSM_GPIO_IN_1 MSM_GPIO2_REG(0x20)
  126. #define MSM_GPIO_IN_2 MSM_GPIO1_REG(0x38)
  127. #define MSM_GPIO_IN_3 MSM_GPIO1_REG(0x3C)
  128. #define MSM_GPIO_IN_4 MSM_GPIO1_REG(0x40)
  129. #define MSM_GPIO_IN_5 MSM_GPIO1_REG(0x44)
  130. #define MSM_GPIO_IN_6 MSM_GPIO1_REG(0xCC)
  131. #define MSM_GPIO_IN_7 MSM_GPIO1_REG(0x21C)
  132. /* same pin map as above, 1=edge 0=level interrup */
  133. #define MSM_GPIO_INT_EDGE_0 MSM_GPIO1_REG(0x60)
  134. #define MSM_GPIO_INT_EDGE_1 MSM_GPIO2_REG(0x50)
  135. #define MSM_GPIO_INT_EDGE_2 MSM_GPIO1_REG(0x64)
  136. #define MSM_GPIO_INT_EDGE_3 MSM_GPIO1_REG(0x68)
  137. #define MSM_GPIO_INT_EDGE_4 MSM_GPIO1_REG(0x6C)
  138. #define MSM_GPIO_INT_EDGE_5 MSM_GPIO1_REG(0xC0)
  139. #define MSM_GPIO_INT_EDGE_6 MSM_GPIO1_REG(0xD0)
  140. #define MSM_GPIO_INT_EDGE_7 MSM_GPIO1_REG(0x240)
  141. /* same pin map as above, 1=positive 0=negative */
  142. #define MSM_GPIO_INT_POS_0 MSM_GPIO1_REG(0x70)
  143. #define MSM_GPIO_INT_POS_1 MSM_GPIO2_REG(0x58)
  144. #define MSM_GPIO_INT_POS_2 MSM_GPIO1_REG(0x74)
  145. #define MSM_GPIO_INT_POS_3 MSM_GPIO1_REG(0x78)
  146. #define MSM_GPIO_INT_POS_4 MSM_GPIO1_REG(0x7C)
  147. #define MSM_GPIO_INT_POS_5 MSM_GPIO1_REG(0xBC)
  148. #define MSM_GPIO_INT_POS_6 MSM_GPIO1_REG(0xD4)
  149. #define MSM_GPIO_INT_POS_7 MSM_GPIO1_REG(0x228)
  150. /* same pin map as above, interrupt enable */
  151. #define MSM_GPIO_INT_EN_0 MSM_GPIO1_REG(0x80)
  152. #define MSM_GPIO_INT_EN_1 MSM_GPIO2_REG(0x60)
  153. #define MSM_GPIO_INT_EN_2 MSM_GPIO1_REG(0x84)
  154. #define MSM_GPIO_INT_EN_3 MSM_GPIO1_REG(0x88)
  155. #define MSM_GPIO_INT_EN_4 MSM_GPIO1_REG(0x8C)
  156. #define MSM_GPIO_INT_EN_5 MSM_GPIO1_REG(0xB8)
  157. #define MSM_GPIO_INT_EN_6 MSM_GPIO1_REG(0xD8)
  158. #define MSM_GPIO_INT_EN_7 MSM_GPIO1_REG(0x22C)
  159. /* same pin map as above, write 1 to clear interrupt */
  160. #define MSM_GPIO_INT_CLEAR_0 MSM_GPIO1_REG(0x90)
  161. #define MSM_GPIO_INT_CLEAR_1 MSM_GPIO2_REG(0x68)
  162. #define MSM_GPIO_INT_CLEAR_2 MSM_GPIO1_REG(0x94)
  163. #define MSM_GPIO_INT_CLEAR_3 MSM_GPIO1_REG(0x98)
  164. #define MSM_GPIO_INT_CLEAR_4 MSM_GPIO1_REG(0x9C)
  165. #define MSM_GPIO_INT_CLEAR_5 MSM_GPIO1_REG(0xB4)
  166. #define MSM_GPIO_INT_CLEAR_6 MSM_GPIO1_REG(0xDC)
  167. #define MSM_GPIO_INT_CLEAR_7 MSM_GPIO1_REG(0x230)
  168. /* same pin map as above, 1=interrupt pending */
  169. #define MSM_GPIO_INT_STATUS_0 MSM_GPIO1_REG(0xA0)
  170. #define MSM_GPIO_INT_STATUS_1 MSM_GPIO2_REG(0x70)
  171. #define MSM_GPIO_INT_STATUS_2 MSM_GPIO1_REG(0xA4)
  172. #define MSM_GPIO_INT_STATUS_3 MSM_GPIO1_REG(0xA8)
  173. #define MSM_GPIO_INT_STATUS_4 MSM_GPIO1_REG(0xAC)
  174. #define MSM_GPIO_INT_STATUS_5 MSM_GPIO1_REG(0xB0)
  175. #define MSM_GPIO_INT_STATUS_6 MSM_GPIO1_REG(0xE0)
  176. #define MSM_GPIO_INT_STATUS_7 MSM_GPIO1_REG(0x234)
  177. #endif
  178. enum {
  179. GPIO_DEBUG_SLEEP = 1U << 0,
  180. };
  181. static int msm_gpio_debug_mask;
  182. module_param_named(debug_mask, msm_gpio_debug_mask, int,
  183. S_IRUGO | S_IWUSR | S_IWGRP);
  184. #define FIRST_GPIO_IRQ MSM_GPIO_TO_INT(0)
  185. #define MSM_GPIO_BANK(bank, first, last) \
  186. { \
  187. .regs = { \
  188. .out = MSM_GPIO_OUT_##bank, \
  189. .in = MSM_GPIO_IN_##bank, \
  190. .int_status = MSM_GPIO_INT_STATUS_##bank, \
  191. .int_clear = MSM_GPIO_INT_CLEAR_##bank, \
  192. .int_en = MSM_GPIO_INT_EN_##bank, \
  193. .int_edge = MSM_GPIO_INT_EDGE_##bank, \
  194. .int_pos = MSM_GPIO_INT_POS_##bank, \
  195. .oe = MSM_GPIO_OE_##bank, \
  196. }, \
  197. .chip = { \
  198. .base = (first), \
  199. .ngpio = (last) - (first) + 1, \
  200. .get = msm_gpio_get, \
  201. .set = msm_gpio_set, \
  202. .direction_input = msm_gpio_direction_input, \
  203. .direction_output = msm_gpio_direction_output, \
  204. .to_irq = msm_gpio_to_irq, \
  205. .request = msm_gpio_request, \
  206. .free = msm_gpio_free, \
  207. } \
  208. }
  209. #define MSM_GPIO_BROKEN_INT_CLEAR 1
  210. struct msm_gpio_regs {
  211. void __iomem *out;
  212. void __iomem *in;
  213. void __iomem *int_status;
  214. void __iomem *int_clear;
  215. void __iomem *int_en;
  216. void __iomem *int_edge;
  217. void __iomem *int_pos;
  218. void __iomem *oe;
  219. };
  220. struct msm_gpio_chip {
  221. spinlock_t lock;
  222. struct gpio_chip chip;
  223. struct msm_gpio_regs regs;
  224. #if MSM_GPIO_BROKEN_INT_CLEAR
  225. unsigned int_status_copy;
  226. #endif
  227. unsigned int both_edge_detect;
  228. unsigned int int_enable[2]; /* 0: awake, 1: sleep */
  229. };
  230. static int msm_gpio_write(struct msm_gpio_chip *msm_chip,
  231. unsigned offset, unsigned on)
  232. {
  233. unsigned mask = BIT(offset);
  234. unsigned val;
  235. val = __raw_readl(msm_chip->regs.out);
  236. if (on)
  237. __raw_writel(val | mask, msm_chip->regs.out);
  238. else
  239. __raw_writel(val & ~mask, msm_chip->regs.out);
  240. return 0;
  241. }
  242. static void msm_gpio_update_both_edge_detect(struct msm_gpio_chip *msm_chip)
  243. {
  244. int loop_limit = 100;
  245. unsigned pol, val, val2, intstat;
  246. do {
  247. val = __raw_readl(msm_chip->regs.in);
  248. pol = __raw_readl(msm_chip->regs.int_pos);
  249. pol = (pol & ~msm_chip->both_edge_detect) |
  250. (~val & msm_chip->both_edge_detect);
  251. __raw_writel(pol, msm_chip->regs.int_pos);
  252. intstat = __raw_readl(msm_chip->regs.int_status);
  253. val2 = __raw_readl(msm_chip->regs.in);
  254. if (((val ^ val2) & msm_chip->both_edge_detect & ~intstat) == 0)
  255. return;
  256. } while (loop_limit-- > 0);
  257. printk(KERN_ERR "msm_gpio_update_both_edge_detect, "
  258. "failed to reach stable state %x != %x\n", val, val2);
  259. }
  260. static int msm_gpio_clear_detect_status(struct msm_gpio_chip *msm_chip,
  261. unsigned offset)
  262. {
  263. unsigned bit = BIT(offset);
  264. #if MSM_GPIO_BROKEN_INT_CLEAR
  265. /* Save interrupts that already triggered before we loose them. */
  266. /* Any interrupt that triggers between the read of int_status */
  267. /* and the write to int_clear will still be lost though. */
  268. msm_chip->int_status_copy |= __raw_readl(msm_chip->regs.int_status);
  269. msm_chip->int_status_copy &= ~bit;
  270. #endif
  271. __raw_writel(bit, msm_chip->regs.int_clear);
  272. msm_gpio_update_both_edge_detect(msm_chip);
  273. return 0;
  274. }
  275. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  276. {
  277. struct msm_gpio_chip *msm_chip;
  278. unsigned long irq_flags;
  279. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  280. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  281. __raw_writel(__raw_readl(msm_chip->regs.oe) & ~BIT(offset),
  282. msm_chip->regs.oe);
  283. mb();
  284. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  285. return 0;
  286. }
  287. static int
  288. msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  289. {
  290. struct msm_gpio_chip *msm_chip;
  291. unsigned long irq_flags;
  292. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  293. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  294. msm_gpio_write(msm_chip, offset, value);
  295. __raw_writel(__raw_readl(msm_chip->regs.oe) | BIT(offset),
  296. msm_chip->regs.oe);
  297. mb();
  298. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  299. return 0;
  300. }
  301. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  302. {
  303. struct msm_gpio_chip *msm_chip;
  304. int rc;
  305. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  306. rc = (__raw_readl(msm_chip->regs.in) & (1U << offset)) ? 1 : 0;
  307. mb();
  308. return rc;
  309. }
  310. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  311. {
  312. struct msm_gpio_chip *msm_chip;
  313. unsigned long irq_flags;
  314. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  315. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  316. msm_gpio_write(msm_chip, offset, value);
  317. mb();
  318. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  319. }
  320. static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  321. {
  322. return MSM_GPIO_TO_INT(chip->base + offset);
  323. }
  324. #ifdef CONFIG_MSM_GPIOMUX
  325. static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
  326. {
  327. return msm_gpiomux_get(chip->base + offset);
  328. }
  329. static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
  330. {
  331. msm_gpiomux_put(chip->base + offset);
  332. }
  333. #else
  334. #define msm_gpio_request NULL
  335. #define msm_gpio_free NULL
  336. #endif
  337. struct msm_gpio_chip msm_gpio_chips[] = {
  338. #if defined(CONFIG_ARCH_MSM7X00A)
  339. MSM_GPIO_BANK(0, 0, 15),
  340. MSM_GPIO_BANK(1, 16, 42),
  341. MSM_GPIO_BANK(2, 43, 67),
  342. MSM_GPIO_BANK(3, 68, 94),
  343. MSM_GPIO_BANK(4, 95, 106),
  344. MSM_GPIO_BANK(5, 107, 121),
  345. #elif defined(CONFIG_ARCH_MSM7X25) || defined(CONFIG_ARCH_MSM7X27)
  346. MSM_GPIO_BANK(0, 0, 15),
  347. MSM_GPIO_BANK(1, 16, 42),
  348. MSM_GPIO_BANK(2, 43, 67),
  349. MSM_GPIO_BANK(3, 68, 94),
  350. MSM_GPIO_BANK(4, 95, 106),
  351. MSM_GPIO_BANK(5, 107, 132),
  352. #elif defined(CONFIG_ARCH_MSM7X30)
  353. MSM_GPIO_BANK(0, 0, 15),
  354. MSM_GPIO_BANK(1, 16, 43),
  355. MSM_GPIO_BANK(2, 44, 67),
  356. MSM_GPIO_BANK(3, 68, 94),
  357. MSM_GPIO_BANK(4, 95, 106),
  358. MSM_GPIO_BANK(5, 107, 133),
  359. MSM_GPIO_BANK(6, 134, 150),
  360. MSM_GPIO_BANK(7, 151, 181),
  361. #elif defined(CONFIG_ARCH_QSD8X50)
  362. MSM_GPIO_BANK(0, 0, 15),
  363. MSM_GPIO_BANK(1, 16, 42),
  364. MSM_GPIO_BANK(2, 43, 67),
  365. MSM_GPIO_BANK(3, 68, 94),
  366. MSM_GPIO_BANK(4, 95, 103),
  367. MSM_GPIO_BANK(5, 104, 121),
  368. MSM_GPIO_BANK(6, 122, 152),
  369. MSM_GPIO_BANK(7, 153, 164),
  370. #endif
  371. };
  372. static void msm_gpio_irq_ack(struct irq_data *d)
  373. {
  374. unsigned long irq_flags;
  375. struct msm_gpio_chip *msm_chip = irq_get_chip_data(d->irq);
  376. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  377. msm_gpio_clear_detect_status(msm_chip,
  378. d->irq - gpio_to_irq(msm_chip->chip.base));
  379. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  380. }
  381. static void msm_gpio_irq_mask(struct irq_data *d)
  382. {
  383. unsigned long irq_flags;
  384. struct msm_gpio_chip *msm_chip = irq_get_chip_data(d->irq);
  385. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  386. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  387. /* level triggered interrupts are also latched */
  388. if (!(__raw_readl(msm_chip->regs.int_edge) & BIT(offset)))
  389. msm_gpio_clear_detect_status(msm_chip, offset);
  390. msm_chip->int_enable[0] &= ~BIT(offset);
  391. __raw_writel(msm_chip->int_enable[0], msm_chip->regs.int_en);
  392. mb();
  393. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  394. }
  395. static void msm_gpio_irq_unmask(struct irq_data *d)
  396. {
  397. unsigned long irq_flags;
  398. struct msm_gpio_chip *msm_chip = irq_get_chip_data(d->irq);
  399. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  400. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  401. /* level triggered interrupts are also latched */
  402. if (!(__raw_readl(msm_chip->regs.int_edge) & BIT(offset)))
  403. msm_gpio_clear_detect_status(msm_chip, offset);
  404. msm_chip->int_enable[0] |= BIT(offset);
  405. __raw_writel(msm_chip->int_enable[0], msm_chip->regs.int_en);
  406. mb();
  407. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  408. }
  409. static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  410. {
  411. unsigned long irq_flags;
  412. struct msm_gpio_chip *msm_chip = irq_get_chip_data(d->irq);
  413. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  414. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  415. if (on)
  416. msm_chip->int_enable[1] |= BIT(offset);
  417. else
  418. msm_chip->int_enable[1] &= ~BIT(offset);
  419. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  420. return 0;
  421. }
  422. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
  423. {
  424. unsigned long irq_flags;
  425. struct msm_gpio_chip *msm_chip = irq_get_chip_data(d->irq);
  426. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  427. unsigned val, mask = BIT(offset);
  428. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  429. val = __raw_readl(msm_chip->regs.int_edge);
  430. if (flow_type & IRQ_TYPE_EDGE_BOTH) {
  431. __raw_writel(val | mask, msm_chip->regs.int_edge);
  432. __irq_set_handler_locked(d->irq, handle_edge_irq);
  433. } else {
  434. __raw_writel(val & ~mask, msm_chip->regs.int_edge);
  435. __irq_set_handler_locked(d->irq, handle_level_irq);
  436. }
  437. if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  438. msm_chip->both_edge_detect |= mask;
  439. msm_gpio_update_both_edge_detect(msm_chip);
  440. } else {
  441. msm_chip->both_edge_detect &= ~mask;
  442. val = __raw_readl(msm_chip->regs.int_pos);
  443. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_HIGH))
  444. __raw_writel(val | mask, msm_chip->regs.int_pos);
  445. else
  446. __raw_writel(val & ~mask, msm_chip->regs.int_pos);
  447. }
  448. mb();
  449. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  450. return 0;
  451. }
  452. static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  453. {
  454. int i, j, mask;
  455. unsigned val;
  456. struct irq_chip *chip = irq_desc_get_chip(desc);
  457. chained_irq_enter(chip, desc);
  458. for (i = 0; i < ARRAY_SIZE(msm_gpio_chips); i++) {
  459. struct msm_gpio_chip *msm_chip = &msm_gpio_chips[i];
  460. val = __raw_readl(msm_chip->regs.int_status);
  461. val &= msm_chip->int_enable[0];
  462. while (val) {
  463. mask = val & -val;
  464. j = fls(mask) - 1;
  465. /* printk("%s %08x %08x bit %d gpio %d irq %d\n",
  466. __func__, v, m, j, msm_chip->chip.start + j,
  467. FIRST_GPIO_IRQ + msm_chip->chip.start + j); */
  468. val &= ~mask;
  469. generic_handle_irq(FIRST_GPIO_IRQ +
  470. msm_chip->chip.base + j);
  471. }
  472. }
  473. chained_irq_exit(chip, desc);
  474. }
  475. static struct irq_chip msm_gpio_irq_chip = {
  476. .name = "msmgpio",
  477. .irq_ack = msm_gpio_irq_ack,
  478. .irq_mask = msm_gpio_irq_mask,
  479. .irq_unmask = msm_gpio_irq_unmask,
  480. .irq_set_wake = msm_gpio_irq_set_wake,
  481. .irq_set_type = msm_gpio_irq_set_type,
  482. };
  483. #define NUM_GPIO_SMEM_BANKS 6
  484. #define GPIO_SMEM_NUM_GROUPS 2
  485. #define GPIO_SMEM_MAX_PC_INTERRUPTS 8
  486. struct tramp_gpio_smem {
  487. uint16_t num_fired[GPIO_SMEM_NUM_GROUPS];
  488. uint16_t fired[GPIO_SMEM_NUM_GROUPS][GPIO_SMEM_MAX_PC_INTERRUPTS];
  489. uint32_t enabled[NUM_GPIO_SMEM_BANKS];
  490. uint32_t detection[NUM_GPIO_SMEM_BANKS];
  491. uint32_t polarity[NUM_GPIO_SMEM_BANKS];
  492. };
  493. static void msm_gpio_sleep_int(unsigned long arg)
  494. {
  495. int i, j;
  496. struct tramp_gpio_smem *smem_gpio;
  497. BUILD_BUG_ON(NR_GPIO_IRQS > NUM_GPIO_SMEM_BANKS * 32);
  498. smem_gpio = smem_alloc(SMEM_GPIO_INT, sizeof(*smem_gpio));
  499. if (smem_gpio == NULL)
  500. return;
  501. local_irq_disable();
  502. for (i = 0; i < GPIO_SMEM_NUM_GROUPS; i++) {
  503. int count = smem_gpio->num_fired[i];
  504. for (j = 0; j < count; j++) {
  505. /* TODO: Check mask */
  506. generic_handle_irq(
  507. MSM_GPIO_TO_INT(smem_gpio->fired[i][j]));
  508. }
  509. }
  510. local_irq_enable();
  511. }
  512. static DECLARE_TASKLET(msm_gpio_sleep_int_tasklet, msm_gpio_sleep_int, 0);
  513. void msm_gpio_enter_sleep(int from_idle)
  514. {
  515. int i;
  516. struct tramp_gpio_smem *smem_gpio;
  517. smem_gpio = smem_alloc(SMEM_GPIO_INT, sizeof(*smem_gpio));
  518. if (smem_gpio) {
  519. for (i = 0; i < ARRAY_SIZE(smem_gpio->enabled); i++) {
  520. smem_gpio->enabled[i] = 0;
  521. smem_gpio->detection[i] = 0;
  522. smem_gpio->polarity[i] = 0;
  523. }
  524. }
  525. for (i = 0; i < ARRAY_SIZE(msm_gpio_chips); i++) {
  526. __raw_writel(msm_gpio_chips[i].int_enable[!from_idle],
  527. msm_gpio_chips[i].regs.int_en);
  528. if (smem_gpio) {
  529. uint32_t tmp;
  530. int start, index, shiftl, shiftr;
  531. start = msm_gpio_chips[i].chip.base;
  532. index = start / 32;
  533. shiftl = start % 32;
  534. shiftr = 32 - shiftl;
  535. tmp = msm_gpio_chips[i].int_enable[!from_idle];
  536. smem_gpio->enabled[index] |= tmp << shiftl;
  537. smem_gpio->enabled[index+1] |= tmp >> shiftr;
  538. smem_gpio->detection[index] |=
  539. __raw_readl(msm_gpio_chips[i].regs.int_edge) <<
  540. shiftl;
  541. smem_gpio->detection[index+1] |=
  542. __raw_readl(msm_gpio_chips[i].regs.int_edge) >>
  543. shiftr;
  544. smem_gpio->polarity[index] |=
  545. __raw_readl(msm_gpio_chips[i].regs.int_pos) <<
  546. shiftl;
  547. smem_gpio->polarity[index+1] |=
  548. __raw_readl(msm_gpio_chips[i].regs.int_pos) >>
  549. shiftr;
  550. }
  551. }
  552. mb();
  553. if (smem_gpio) {
  554. if (msm_gpio_debug_mask & GPIO_DEBUG_SLEEP)
  555. for (i = 0; i < ARRAY_SIZE(smem_gpio->enabled); i++) {
  556. printk("msm_gpio_enter_sleep gpio %d-%d: enable"
  557. " %08x, edge %08x, polarity %08x\n",
  558. i * 32, i * 32 + 31,
  559. smem_gpio->enabled[i],
  560. smem_gpio->detection[i],
  561. smem_gpio->polarity[i]);
  562. }
  563. for (i = 0; i < GPIO_SMEM_NUM_GROUPS; i++)
  564. smem_gpio->num_fired[i] = 0;
  565. }
  566. }
  567. void msm_gpio_exit_sleep(void)
  568. {
  569. int i;
  570. struct tramp_gpio_smem *smem_gpio;
  571. smem_gpio = smem_alloc(SMEM_GPIO_INT, sizeof(*smem_gpio));
  572. for (i = 0; i < ARRAY_SIZE(msm_gpio_chips); i++) {
  573. __raw_writel(msm_gpio_chips[i].int_enable[0],
  574. msm_gpio_chips[i].regs.int_en);
  575. }
  576. mb();
  577. if (smem_gpio && (smem_gpio->num_fired[0] || smem_gpio->num_fired[1])) {
  578. if (msm_gpio_debug_mask & GPIO_DEBUG_SLEEP)
  579. printk(KERN_INFO "gpio: fired %x %x\n",
  580. smem_gpio->num_fired[0], smem_gpio->num_fired[1]);
  581. tasklet_schedule(&msm_gpio_sleep_int_tasklet);
  582. }
  583. }
  584. int gpio_tlmm_config(unsigned config, unsigned disable)
  585. {
  586. return msm_proc_comm(PCOM_RPC_GPIO_TLMM_CONFIG_EX, &config, &disable);
  587. }
  588. EXPORT_SYMBOL(gpio_tlmm_config);
  589. int msm_gpios_request_enable(const struct msm_gpio *table, int size)
  590. {
  591. int rc = msm_gpios_request(table, size);
  592. if (rc)
  593. return rc;
  594. rc = msm_gpios_enable(table, size);
  595. if (rc)
  596. msm_gpios_free(table, size);
  597. return rc;
  598. }
  599. EXPORT_SYMBOL(msm_gpios_request_enable);
  600. void msm_gpios_disable_free(const struct msm_gpio *table, int size)
  601. {
  602. msm_gpios_disable(table, size);
  603. msm_gpios_free(table, size);
  604. }
  605. EXPORT_SYMBOL(msm_gpios_disable_free);
  606. int msm_gpios_request(const struct msm_gpio *table, int size)
  607. {
  608. int rc;
  609. int i;
  610. const struct msm_gpio *g;
  611. for (i = 0; i < size; i++) {
  612. g = table + i;
  613. rc = gpio_request(GPIO_PIN(g->gpio_cfg), g->label);
  614. if (rc) {
  615. pr_err("gpio_request(%d) <%s> failed: %d\n",
  616. GPIO_PIN(g->gpio_cfg), g->label ?: "?", rc);
  617. goto err;
  618. }
  619. }
  620. return 0;
  621. err:
  622. msm_gpios_free(table, i);
  623. return rc;
  624. }
  625. EXPORT_SYMBOL(msm_gpios_request);
  626. void msm_gpios_free(const struct msm_gpio *table, int size)
  627. {
  628. int i;
  629. const struct msm_gpio *g;
  630. for (i = size-1; i >= 0; i--) {
  631. g = table + i;
  632. gpio_free(GPIO_PIN(g->gpio_cfg));
  633. }
  634. }
  635. EXPORT_SYMBOL(msm_gpios_free);
  636. int msm_gpios_enable(const struct msm_gpio *table, int size)
  637. {
  638. int rc;
  639. int i;
  640. const struct msm_gpio *g;
  641. for (i = 0; i < size; i++) {
  642. g = table + i;
  643. rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
  644. if (rc) {
  645. pr_err("gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE)"
  646. " <%s> failed: %d\n",
  647. g->gpio_cfg, g->label ?: "?", rc);
  648. pr_err("pin %d func %d dir %d pull %d drvstr %d\n",
  649. GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
  650. GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
  651. GPIO_DRVSTR(g->gpio_cfg));
  652. goto err;
  653. }
  654. }
  655. return 0;
  656. err:
  657. msm_gpios_disable(table, i);
  658. return rc;
  659. }
  660. EXPORT_SYMBOL(msm_gpios_enable);
  661. int msm_gpios_disable(const struct msm_gpio *table, int size)
  662. {
  663. int rc = 0;
  664. int i;
  665. const struct msm_gpio *g;
  666. for (i = size-1; i >= 0; i--) {
  667. int tmp;
  668. g = table + i;
  669. tmp = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_DISABLE);
  670. if (tmp) {
  671. pr_err("gpio_tlmm_config(0x%08x, GPIO_CFG_DISABLE)"
  672. " <%s> failed: %d\n",
  673. g->gpio_cfg, g->label ?: "?", rc);
  674. pr_err("pin %d func %d dir %d pull %d drvstr %d\n",
  675. GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
  676. GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
  677. GPIO_DRVSTR(g->gpio_cfg));
  678. if (!rc)
  679. rc = tmp;
  680. }
  681. }
  682. return rc;
  683. }
  684. EXPORT_SYMBOL(msm_gpios_disable);
  685. /* Locate the GPIO_OUT register for the given GPIO and return its address
  686. * and the bit position of the gpio's bit within the register.
  687. *
  688. * This function is used by gpiomux-v1 in order to support output transitions.
  689. */
  690. void msm_gpio_find_out(const unsigned gpio, void __iomem **out,
  691. unsigned *offset)
  692. {
  693. struct msm_gpio_chip *msm_chip = msm_gpio_chips;
  694. while (gpio >= msm_chip->chip.base + msm_chip->chip.ngpio)
  695. ++msm_chip;
  696. *out = msm_chip->regs.out;
  697. *offset = gpio - msm_chip->chip.base;
  698. }
  699. static int __devinit msm_gpio_probe(struct platform_device *dev)
  700. {
  701. int i, j = 0;
  702. int grp_irq;
  703. for (i = FIRST_GPIO_IRQ; i < FIRST_GPIO_IRQ + NR_GPIO_IRQS; i++) {
  704. if (i - FIRST_GPIO_IRQ >=
  705. msm_gpio_chips[j].chip.base +
  706. msm_gpio_chips[j].chip.ngpio)
  707. j++;
  708. irq_set_chip_data(i, &msm_gpio_chips[j]);
  709. irq_set_chip_and_handler(i, &msm_gpio_irq_chip,
  710. handle_edge_irq);
  711. set_irq_flags(i, IRQF_VALID);
  712. }
  713. for (i = 0; i < dev->num_resources; i++) {
  714. grp_irq = platform_get_irq(dev, i);
  715. if (grp_irq < 0)
  716. return -ENXIO;
  717. irq_set_chained_handler(grp_irq, msm_gpio_irq_handler);
  718. irq_set_irq_wake(grp_irq, (i + 1));
  719. }
  720. for (i = 0; i < ARRAY_SIZE(msm_gpio_chips); i++) {
  721. spin_lock_init(&msm_gpio_chips[i].lock);
  722. __raw_writel(0, msm_gpio_chips[i].regs.int_en);
  723. gpiochip_add(&msm_gpio_chips[i].chip);
  724. }
  725. mb();
  726. return 0;
  727. }
  728. static struct platform_driver msm_gpio_driver = {
  729. .probe = msm_gpio_probe,
  730. .driver = {
  731. .name = "msmgpio",
  732. .owner = THIS_MODULE,
  733. },
  734. };
  735. static int __init msm_gpio_init(void)
  736. {
  737. return platform_driver_register(&msm_gpio_driver);
  738. }
  739. postcore_initcall(msm_gpio_init);