gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* linux/arch/arm/mach-msm/gpio.c
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2009-2010, Code Aurora Forum. 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 "gpio_hw.h"
  23. #include "gpiomux.h"
  24. #define FIRST_GPIO_IRQ MSM_GPIO_TO_INT(0)
  25. #define MSM_GPIO_BANK(bank, first, last) \
  26. { \
  27. .regs = { \
  28. .out = MSM_GPIO_OUT_##bank, \
  29. .in = MSM_GPIO_IN_##bank, \
  30. .int_status = MSM_GPIO_INT_STATUS_##bank, \
  31. .int_clear = MSM_GPIO_INT_CLEAR_##bank, \
  32. .int_en = MSM_GPIO_INT_EN_##bank, \
  33. .int_edge = MSM_GPIO_INT_EDGE_##bank, \
  34. .int_pos = MSM_GPIO_INT_POS_##bank, \
  35. .oe = MSM_GPIO_OE_##bank, \
  36. }, \
  37. .chip = { \
  38. .base = (first), \
  39. .ngpio = (last) - (first) + 1, \
  40. .get = msm_gpio_get, \
  41. .set = msm_gpio_set, \
  42. .direction_input = msm_gpio_direction_input, \
  43. .direction_output = msm_gpio_direction_output, \
  44. .to_irq = msm_gpio_to_irq, \
  45. .request = msm_gpio_request, \
  46. .free = msm_gpio_free, \
  47. } \
  48. }
  49. #define MSM_GPIO_BROKEN_INT_CLEAR 1
  50. struct msm_gpio_regs {
  51. void __iomem *out;
  52. void __iomem *in;
  53. void __iomem *int_status;
  54. void __iomem *int_clear;
  55. void __iomem *int_en;
  56. void __iomem *int_edge;
  57. void __iomem *int_pos;
  58. void __iomem *oe;
  59. };
  60. struct msm_gpio_chip {
  61. spinlock_t lock;
  62. struct gpio_chip chip;
  63. struct msm_gpio_regs regs;
  64. #if MSM_GPIO_BROKEN_INT_CLEAR
  65. unsigned int_status_copy;
  66. #endif
  67. unsigned int both_edge_detect;
  68. unsigned int int_enable[2]; /* 0: awake, 1: sleep */
  69. };
  70. static int msm_gpio_write(struct msm_gpio_chip *msm_chip,
  71. unsigned offset, unsigned on)
  72. {
  73. unsigned mask = BIT(offset);
  74. unsigned val;
  75. val = readl(msm_chip->regs.out);
  76. if (on)
  77. writel(val | mask, msm_chip->regs.out);
  78. else
  79. writel(val & ~mask, msm_chip->regs.out);
  80. return 0;
  81. }
  82. static void msm_gpio_update_both_edge_detect(struct msm_gpio_chip *msm_chip)
  83. {
  84. int loop_limit = 100;
  85. unsigned pol, val, val2, intstat;
  86. do {
  87. val = readl(msm_chip->regs.in);
  88. pol = readl(msm_chip->regs.int_pos);
  89. pol = (pol & ~msm_chip->both_edge_detect) |
  90. (~val & msm_chip->both_edge_detect);
  91. writel(pol, msm_chip->regs.int_pos);
  92. intstat = readl(msm_chip->regs.int_status);
  93. val2 = readl(msm_chip->regs.in);
  94. if (((val ^ val2) & msm_chip->both_edge_detect & ~intstat) == 0)
  95. return;
  96. } while (loop_limit-- > 0);
  97. printk(KERN_ERR "msm_gpio_update_both_edge_detect, "
  98. "failed to reach stable state %x != %x\n", val, val2);
  99. }
  100. static int msm_gpio_clear_detect_status(struct msm_gpio_chip *msm_chip,
  101. unsigned offset)
  102. {
  103. unsigned bit = BIT(offset);
  104. #if MSM_GPIO_BROKEN_INT_CLEAR
  105. /* Save interrupts that already triggered before we loose them. */
  106. /* Any interrupt that triggers between the read of int_status */
  107. /* and the write to int_clear will still be lost though. */
  108. msm_chip->int_status_copy |= readl(msm_chip->regs.int_status);
  109. msm_chip->int_status_copy &= ~bit;
  110. #endif
  111. writel(bit, msm_chip->regs.int_clear);
  112. msm_gpio_update_both_edge_detect(msm_chip);
  113. return 0;
  114. }
  115. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  116. {
  117. struct msm_gpio_chip *msm_chip;
  118. unsigned long irq_flags;
  119. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  120. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  121. writel(readl(msm_chip->regs.oe) & ~BIT(offset), msm_chip->regs.oe);
  122. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  123. return 0;
  124. }
  125. static int
  126. msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  127. {
  128. struct msm_gpio_chip *msm_chip;
  129. unsigned long irq_flags;
  130. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  131. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  132. msm_gpio_write(msm_chip, offset, value);
  133. writel(readl(msm_chip->regs.oe) | BIT(offset), msm_chip->regs.oe);
  134. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  135. return 0;
  136. }
  137. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  138. {
  139. struct msm_gpio_chip *msm_chip;
  140. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  141. return (readl(msm_chip->regs.in) & (1U << offset)) ? 1 : 0;
  142. }
  143. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  144. {
  145. struct msm_gpio_chip *msm_chip;
  146. unsigned long irq_flags;
  147. msm_chip = container_of(chip, struct msm_gpio_chip, chip);
  148. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  149. msm_gpio_write(msm_chip, offset, value);
  150. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  151. }
  152. static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  153. {
  154. return MSM_GPIO_TO_INT(chip->base + offset);
  155. }
  156. #ifdef CONFIG_MSM_GPIOMUX
  157. static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
  158. {
  159. return msm_gpiomux_get(chip->base + offset);
  160. }
  161. static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
  162. {
  163. msm_gpiomux_put(chip->base + offset);
  164. }
  165. #else
  166. #define msm_gpio_request NULL
  167. #define msm_gpio_free NULL
  168. #endif
  169. struct msm_gpio_chip msm_gpio_chips[] = {
  170. #if defined(CONFIG_ARCH_MSM7X00A)
  171. MSM_GPIO_BANK(0, 0, 15),
  172. MSM_GPIO_BANK(1, 16, 42),
  173. MSM_GPIO_BANK(2, 43, 67),
  174. MSM_GPIO_BANK(3, 68, 94),
  175. MSM_GPIO_BANK(4, 95, 106),
  176. MSM_GPIO_BANK(5, 107, 121),
  177. #elif defined(CONFIG_ARCH_MSM7X25) || defined(CONFIG_ARCH_MSM7X27)
  178. MSM_GPIO_BANK(0, 0, 15),
  179. MSM_GPIO_BANK(1, 16, 42),
  180. MSM_GPIO_BANK(2, 43, 67),
  181. MSM_GPIO_BANK(3, 68, 94),
  182. MSM_GPIO_BANK(4, 95, 106),
  183. MSM_GPIO_BANK(5, 107, 132),
  184. #elif defined(CONFIG_ARCH_MSM7X30)
  185. MSM_GPIO_BANK(0, 0, 15),
  186. MSM_GPIO_BANK(1, 16, 43),
  187. MSM_GPIO_BANK(2, 44, 67),
  188. MSM_GPIO_BANK(3, 68, 94),
  189. MSM_GPIO_BANK(4, 95, 106),
  190. MSM_GPIO_BANK(5, 107, 133),
  191. MSM_GPIO_BANK(6, 134, 150),
  192. MSM_GPIO_BANK(7, 151, 181),
  193. #elif defined(CONFIG_ARCH_QSD8X50)
  194. MSM_GPIO_BANK(0, 0, 15),
  195. MSM_GPIO_BANK(1, 16, 42),
  196. MSM_GPIO_BANK(2, 43, 67),
  197. MSM_GPIO_BANK(3, 68, 94),
  198. MSM_GPIO_BANK(4, 95, 103),
  199. MSM_GPIO_BANK(5, 104, 121),
  200. MSM_GPIO_BANK(6, 122, 152),
  201. MSM_GPIO_BANK(7, 153, 164),
  202. #endif
  203. };
  204. static void msm_gpio_irq_ack(struct irq_data *d)
  205. {
  206. unsigned long irq_flags;
  207. struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
  208. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  209. msm_gpio_clear_detect_status(msm_chip,
  210. d->irq - gpio_to_irq(msm_chip->chip.base));
  211. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  212. }
  213. static void msm_gpio_irq_mask(struct irq_data *d)
  214. {
  215. unsigned long irq_flags;
  216. struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
  217. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  218. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  219. /* level triggered interrupts are also latched */
  220. if (!(readl(msm_chip->regs.int_edge) & BIT(offset)))
  221. msm_gpio_clear_detect_status(msm_chip, offset);
  222. msm_chip->int_enable[0] &= ~BIT(offset);
  223. writel(msm_chip->int_enable[0], msm_chip->regs.int_en);
  224. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  225. }
  226. static void msm_gpio_irq_unmask(struct irq_data *d)
  227. {
  228. unsigned long irq_flags;
  229. struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
  230. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  231. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  232. /* level triggered interrupts are also latched */
  233. if (!(readl(msm_chip->regs.int_edge) & BIT(offset)))
  234. msm_gpio_clear_detect_status(msm_chip, offset);
  235. msm_chip->int_enable[0] |= BIT(offset);
  236. writel(msm_chip->int_enable[0], msm_chip->regs.int_en);
  237. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  238. }
  239. static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  240. {
  241. unsigned long irq_flags;
  242. struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
  243. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  244. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  245. if (on)
  246. msm_chip->int_enable[1] |= BIT(offset);
  247. else
  248. msm_chip->int_enable[1] &= ~BIT(offset);
  249. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  250. return 0;
  251. }
  252. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int flow_type)
  253. {
  254. unsigned long irq_flags;
  255. struct msm_gpio_chip *msm_chip = irq_data_get_irq_chip_data(d);
  256. unsigned offset = d->irq - gpio_to_irq(msm_chip->chip.base);
  257. unsigned val, mask = BIT(offset);
  258. spin_lock_irqsave(&msm_chip->lock, irq_flags);
  259. val = readl(msm_chip->regs.int_edge);
  260. if (flow_type & IRQ_TYPE_EDGE_BOTH) {
  261. writel(val | mask, msm_chip->regs.int_edge);
  262. __irq_set_handler_locked(d->irq, handle_edge_irq);
  263. } else {
  264. writel(val & ~mask, msm_chip->regs.int_edge);
  265. __irq_set_handler_locked(d->irq, handle_level_irq);
  266. }
  267. if ((flow_type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  268. msm_chip->both_edge_detect |= mask;
  269. msm_gpio_update_both_edge_detect(msm_chip);
  270. } else {
  271. msm_chip->both_edge_detect &= ~mask;
  272. val = readl(msm_chip->regs.int_pos);
  273. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_HIGH))
  274. writel(val | mask, msm_chip->regs.int_pos);
  275. else
  276. writel(val & ~mask, msm_chip->regs.int_pos);
  277. }
  278. spin_unlock_irqrestore(&msm_chip->lock, irq_flags);
  279. return 0;
  280. }
  281. static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  282. {
  283. int i, j, mask;
  284. unsigned val;
  285. for (i = 0; i < ARRAY_SIZE(msm_gpio_chips); i++) {
  286. struct msm_gpio_chip *msm_chip = &msm_gpio_chips[i];
  287. val = readl(msm_chip->regs.int_status);
  288. val &= msm_chip->int_enable[0];
  289. while (val) {
  290. mask = val & -val;
  291. j = fls(mask) - 1;
  292. /* printk("%s %08x %08x bit %d gpio %d irq %d\n",
  293. __func__, v, m, j, msm_chip->chip.start + j,
  294. FIRST_GPIO_IRQ + msm_chip->chip.start + j); */
  295. val &= ~mask;
  296. generic_handle_irq(FIRST_GPIO_IRQ +
  297. msm_chip->chip.base + j);
  298. }
  299. }
  300. desc->irq_data.chip->irq_ack(&desc->irq_data);
  301. }
  302. static struct irq_chip msm_gpio_irq_chip = {
  303. .name = "msmgpio",
  304. .irq_ack = msm_gpio_irq_ack,
  305. .irq_mask = msm_gpio_irq_mask,
  306. .irq_unmask = msm_gpio_irq_unmask,
  307. .irq_set_wake = msm_gpio_irq_set_wake,
  308. .irq_set_type = msm_gpio_irq_set_type,
  309. };
  310. static int __init msm_init_gpio(void)
  311. {
  312. int i, j = 0;
  313. for (i = FIRST_GPIO_IRQ; i < FIRST_GPIO_IRQ + NR_GPIO_IRQS; i++) {
  314. if (i - FIRST_GPIO_IRQ >=
  315. msm_gpio_chips[j].chip.base +
  316. msm_gpio_chips[j].chip.ngpio)
  317. j++;
  318. irq_set_chip_data(i, &msm_gpio_chips[j]);
  319. irq_set_chip_and_handler(i, &msm_gpio_irq_chip,
  320. handle_edge_irq);
  321. set_irq_flags(i, IRQF_VALID);
  322. }
  323. for (i = 0; i < ARRAY_SIZE(msm_gpio_chips); i++) {
  324. spin_lock_init(&msm_gpio_chips[i].lock);
  325. writel(0, msm_gpio_chips[i].regs.int_en);
  326. gpiochip_add(&msm_gpio_chips[i].chip);
  327. }
  328. irq_set_chained_handler(INT_GPIO_GROUP1, msm_gpio_irq_handler);
  329. irq_set_chained_handler(INT_GPIO_GROUP2, msm_gpio_irq_handler);
  330. irq_set_irq_wake(INT_GPIO_GROUP1, 1);
  331. irq_set_irq_wake(INT_GPIO_GROUP2, 2);
  332. return 0;
  333. }
  334. postcore_initcall(msm_init_gpio);