gpio-davinci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * TI DaVinci GPIO Support
  3. *
  4. * Copyright (c) 2006-2007 David Brownell
  5. * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/gpio.h>
  13. #include <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <asm/mach/irq.h>
  19. struct davinci_gpio_regs {
  20. u32 dir;
  21. u32 out_data;
  22. u32 set_data;
  23. u32 clr_data;
  24. u32 in_data;
  25. u32 set_rising;
  26. u32 clr_rising;
  27. u32 set_falling;
  28. u32 clr_falling;
  29. u32 intstat;
  30. };
  31. #define chip2controller(chip) \
  32. container_of(chip, struct davinci_gpio_controller, chip)
  33. static struct davinci_gpio_controller chips[DIV_ROUND_UP(DAVINCI_N_GPIO, 32)];
  34. static void __iomem *gpio_base;
  35. static struct davinci_gpio_regs __iomem __init *gpio2regs(unsigned gpio)
  36. {
  37. void __iomem *ptr;
  38. if (gpio < 32 * 1)
  39. ptr = gpio_base + 0x10;
  40. else if (gpio < 32 * 2)
  41. ptr = gpio_base + 0x38;
  42. else if (gpio < 32 * 3)
  43. ptr = gpio_base + 0x60;
  44. else if (gpio < 32 * 4)
  45. ptr = gpio_base + 0x88;
  46. else if (gpio < 32 * 5)
  47. ptr = gpio_base + 0xb0;
  48. else
  49. ptr = NULL;
  50. return ptr;
  51. }
  52. static inline struct davinci_gpio_regs __iomem *irq2regs(int irq)
  53. {
  54. struct davinci_gpio_regs __iomem *g;
  55. g = (__force struct davinci_gpio_regs __iomem *)irq_get_chip_data(irq);
  56. return g;
  57. }
  58. static int __init davinci_gpio_irq_setup(void);
  59. /*--------------------------------------------------------------------------*/
  60. /* board setup code *MUST* setup pinmux and enable the GPIO clock. */
  61. static inline int __davinci_direction(struct gpio_chip *chip,
  62. unsigned offset, bool out, int value)
  63. {
  64. struct davinci_gpio_controller *d = chip2controller(chip);
  65. struct davinci_gpio_regs __iomem *g = d->regs;
  66. unsigned long flags;
  67. u32 temp;
  68. u32 mask = 1 << offset;
  69. spin_lock_irqsave(&d->lock, flags);
  70. temp = __raw_readl(&g->dir);
  71. if (out) {
  72. temp &= ~mask;
  73. __raw_writel(mask, value ? &g->set_data : &g->clr_data);
  74. } else {
  75. temp |= mask;
  76. }
  77. __raw_writel(temp, &g->dir);
  78. spin_unlock_irqrestore(&d->lock, flags);
  79. return 0;
  80. }
  81. static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
  82. {
  83. return __davinci_direction(chip, offset, false, 0);
  84. }
  85. static int
  86. davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
  87. {
  88. return __davinci_direction(chip, offset, true, value);
  89. }
  90. /*
  91. * Read the pin's value (works even if it's set up as output);
  92. * returns zero/nonzero.
  93. *
  94. * Note that changes are synched to the GPIO clock, so reading values back
  95. * right after you've set them may give old values.
  96. */
  97. static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
  98. {
  99. struct davinci_gpio_controller *d = chip2controller(chip);
  100. struct davinci_gpio_regs __iomem *g = d->regs;
  101. return (1 << offset) & __raw_readl(&g->in_data);
  102. }
  103. /*
  104. * Assuming the pin is muxed as a gpio output, set its output value.
  105. */
  106. static void
  107. davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  108. {
  109. struct davinci_gpio_controller *d = chip2controller(chip);
  110. struct davinci_gpio_regs __iomem *g = d->regs;
  111. __raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
  112. }
  113. static int __init davinci_gpio_setup(void)
  114. {
  115. int i, base;
  116. unsigned ngpio;
  117. struct davinci_soc_info *soc_info = &davinci_soc_info;
  118. struct davinci_gpio_regs *regs;
  119. if (soc_info->gpio_type != GPIO_TYPE_DAVINCI)
  120. return 0;
  121. /*
  122. * The gpio banks conceptually expose a segmented bitmap,
  123. * and "ngpio" is one more than the largest zero-based
  124. * bit index that's valid.
  125. */
  126. ngpio = soc_info->gpio_num;
  127. if (ngpio == 0) {
  128. pr_err("GPIO setup: how many GPIOs?\n");
  129. return -EINVAL;
  130. }
  131. if (WARN_ON(DAVINCI_N_GPIO < ngpio))
  132. ngpio = DAVINCI_N_GPIO;
  133. gpio_base = ioremap(soc_info->gpio_base, SZ_4K);
  134. if (WARN_ON(!gpio_base))
  135. return -ENOMEM;
  136. for (i = 0, base = 0; base < ngpio; i++, base += 32) {
  137. chips[i].chip.label = "DaVinci";
  138. chips[i].chip.direction_input = davinci_direction_in;
  139. chips[i].chip.get = davinci_gpio_get;
  140. chips[i].chip.direction_output = davinci_direction_out;
  141. chips[i].chip.set = davinci_gpio_set;
  142. chips[i].chip.base = base;
  143. chips[i].chip.ngpio = ngpio - base;
  144. if (chips[i].chip.ngpio > 32)
  145. chips[i].chip.ngpio = 32;
  146. spin_lock_init(&chips[i].lock);
  147. regs = gpio2regs(base);
  148. chips[i].regs = regs;
  149. chips[i].set_data = &regs->set_data;
  150. chips[i].clr_data = &regs->clr_data;
  151. chips[i].in_data = &regs->in_data;
  152. gpiochip_add(&chips[i].chip);
  153. }
  154. soc_info->gpio_ctlrs = chips;
  155. soc_info->gpio_ctlrs_num = DIV_ROUND_UP(ngpio, 32);
  156. davinci_gpio_irq_setup();
  157. return 0;
  158. }
  159. pure_initcall(davinci_gpio_setup);
  160. /*--------------------------------------------------------------------------*/
  161. /*
  162. * We expect irqs will normally be set up as input pins, but they can also be
  163. * used as output pins ... which is convenient for testing.
  164. *
  165. * NOTE: The first few GPIOs also have direct INTC hookups in addition
  166. * to their GPIOBNK0 irq, with a bit less overhead.
  167. *
  168. * All those INTC hookups (direct, plus several IRQ banks) can also
  169. * serve as EDMA event triggers.
  170. */
  171. static void gpio_irq_disable(struct irq_data *d)
  172. {
  173. struct davinci_gpio_regs __iomem *g = irq2regs(d->irq);
  174. u32 mask = (u32) irq_data_get_irq_handler_data(d);
  175. __raw_writel(mask, &g->clr_falling);
  176. __raw_writel(mask, &g->clr_rising);
  177. }
  178. static void gpio_irq_enable(struct irq_data *d)
  179. {
  180. struct davinci_gpio_regs __iomem *g = irq2regs(d->irq);
  181. u32 mask = (u32) irq_data_get_irq_handler_data(d);
  182. unsigned status = irqd_get_trigger_type(d);
  183. status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
  184. if (!status)
  185. status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
  186. if (status & IRQ_TYPE_EDGE_FALLING)
  187. __raw_writel(mask, &g->set_falling);
  188. if (status & IRQ_TYPE_EDGE_RISING)
  189. __raw_writel(mask, &g->set_rising);
  190. }
  191. static int gpio_irq_type(struct irq_data *d, unsigned trigger)
  192. {
  193. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  194. return -EINVAL;
  195. return 0;
  196. }
  197. static struct irq_chip gpio_irqchip = {
  198. .name = "GPIO",
  199. .irq_enable = gpio_irq_enable,
  200. .irq_disable = gpio_irq_disable,
  201. .irq_set_type = gpio_irq_type,
  202. .flags = IRQCHIP_SET_TYPE_MASKED,
  203. };
  204. static void
  205. gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  206. {
  207. struct davinci_gpio_regs __iomem *g;
  208. u32 mask = 0xffff;
  209. struct davinci_gpio_controller *d;
  210. d = (struct davinci_gpio_controller *)irq_desc_get_handler_data(desc);
  211. g = (struct davinci_gpio_regs __iomem *)d->regs;
  212. /* we only care about one bank */
  213. if (irq & 1)
  214. mask <<= 16;
  215. /* temporarily mask (level sensitive) parent IRQ */
  216. desc->irq_data.chip->irq_mask(&desc->irq_data);
  217. desc->irq_data.chip->irq_ack(&desc->irq_data);
  218. while (1) {
  219. u32 status;
  220. int n;
  221. int res;
  222. /* ack any irqs */
  223. status = __raw_readl(&g->intstat) & mask;
  224. if (!status)
  225. break;
  226. __raw_writel(status, &g->intstat);
  227. /* now demux them to the right lowlevel handler */
  228. n = d->irq_base;
  229. if (irq & 1) {
  230. n += 16;
  231. status >>= 16;
  232. }
  233. while (status) {
  234. res = ffs(status);
  235. n += res;
  236. generic_handle_irq(n - 1);
  237. status >>= res;
  238. }
  239. }
  240. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  241. /* now it may re-trigger */
  242. }
  243. static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
  244. {
  245. struct davinci_gpio_controller *d = chip2controller(chip);
  246. if (d->irq_base >= 0)
  247. return d->irq_base + offset;
  248. else
  249. return -ENODEV;
  250. }
  251. static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
  252. {
  253. struct davinci_soc_info *soc_info = &davinci_soc_info;
  254. /* NOTE: we assume for now that only irqs in the first gpio_chip
  255. * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
  256. */
  257. if (offset < soc_info->gpio_unbanked)
  258. return soc_info->gpio_irq + offset;
  259. else
  260. return -ENODEV;
  261. }
  262. static int gpio_irq_type_unbanked(struct irq_data *data, unsigned trigger)
  263. {
  264. struct davinci_gpio_controller *d;
  265. struct davinci_gpio_regs __iomem *g;
  266. struct davinci_soc_info *soc_info = &davinci_soc_info;
  267. u32 mask;
  268. d = (struct davinci_gpio_controller *)data->handler_data;
  269. g = (struct davinci_gpio_regs __iomem *)d->regs;
  270. mask = __gpio_mask(data->irq - soc_info->gpio_irq);
  271. if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  272. return -EINVAL;
  273. __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
  274. ? &g->set_falling : &g->clr_falling);
  275. __raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
  276. ? &g->set_rising : &g->clr_rising);
  277. return 0;
  278. }
  279. /*
  280. * NOTE: for suspend/resume, probably best to make a platform_device with
  281. * suspend_late/resume_resume calls hooking into results of the set_wake()
  282. * calls ... so if no gpios are wakeup events the clock can be disabled,
  283. * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
  284. * (dm6446) can be set appropriately for GPIOV33 pins.
  285. */
  286. static int __init davinci_gpio_irq_setup(void)
  287. {
  288. unsigned gpio, irq, bank;
  289. struct clk *clk;
  290. u32 binten = 0;
  291. unsigned ngpio, bank_irq;
  292. struct davinci_soc_info *soc_info = &davinci_soc_info;
  293. struct davinci_gpio_regs __iomem *g;
  294. ngpio = soc_info->gpio_num;
  295. bank_irq = soc_info->gpio_irq;
  296. if (bank_irq == 0) {
  297. printk(KERN_ERR "Don't know first GPIO bank IRQ.\n");
  298. return -EINVAL;
  299. }
  300. clk = clk_get(NULL, "gpio");
  301. if (IS_ERR(clk)) {
  302. printk(KERN_ERR "Error %ld getting gpio clock?\n",
  303. PTR_ERR(clk));
  304. return PTR_ERR(clk);
  305. }
  306. clk_enable(clk);
  307. /* Arrange gpio_to_irq() support, handling either direct IRQs or
  308. * banked IRQs. Having GPIOs in the first GPIO bank use direct
  309. * IRQs, while the others use banked IRQs, would need some setup
  310. * tweaks to recognize hardware which can do that.
  311. */
  312. for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 32) {
  313. chips[bank].chip.to_irq = gpio_to_irq_banked;
  314. chips[bank].irq_base = soc_info->gpio_unbanked
  315. ? -EINVAL
  316. : (soc_info->intc_irq_num + gpio);
  317. }
  318. /*
  319. * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
  320. * controller only handling trigger modes. We currently assume no
  321. * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
  322. */
  323. if (soc_info->gpio_unbanked) {
  324. static struct irq_chip_type gpio_unbanked;
  325. /* pass "bank 0" GPIO IRQs to AINTC */
  326. chips[0].chip.to_irq = gpio_to_irq_unbanked;
  327. binten = BIT(0);
  328. /* AINTC handles mask/unmask; GPIO handles triggering */
  329. irq = bank_irq;
  330. gpio_unbanked = *container_of(irq_get_chip(irq),
  331. struct irq_chip_type, chip);
  332. gpio_unbanked.chip.name = "GPIO-AINTC";
  333. gpio_unbanked.chip.irq_set_type = gpio_irq_type_unbanked;
  334. /* default trigger: both edges */
  335. g = gpio2regs(0);
  336. __raw_writel(~0, &g->set_falling);
  337. __raw_writel(~0, &g->set_rising);
  338. /* set the direct IRQs up to use that irqchip */
  339. for (gpio = 0; gpio < soc_info->gpio_unbanked; gpio++, irq++) {
  340. irq_set_chip(irq, &gpio_unbanked.chip);
  341. irq_set_handler_data(irq, &chips[gpio / 32]);
  342. irq_set_status_flags(irq, IRQ_TYPE_EDGE_BOTH);
  343. }
  344. goto done;
  345. }
  346. /*
  347. * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
  348. * then chain through our own handler.
  349. */
  350. for (gpio = 0, irq = gpio_to_irq(0), bank = 0;
  351. gpio < ngpio;
  352. bank++, bank_irq++) {
  353. unsigned i;
  354. /* disabled by default, enabled only as needed */
  355. g = gpio2regs(gpio);
  356. __raw_writel(~0, &g->clr_falling);
  357. __raw_writel(~0, &g->clr_rising);
  358. /* set up all irqs in this bank */
  359. irq_set_chained_handler(bank_irq, gpio_irq_handler);
  360. /*
  361. * Each chip handles 32 gpios, and each irq bank consists of 16
  362. * gpio irqs. Pass the irq bank's corresponding controller to
  363. * the chained irq handler.
  364. */
  365. irq_set_handler_data(bank_irq, &chips[gpio / 32]);
  366. for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
  367. irq_set_chip(irq, &gpio_irqchip);
  368. irq_set_chip_data(irq, (__force void *)g);
  369. irq_set_handler_data(irq, (void *)__gpio_mask(gpio));
  370. irq_set_handler(irq, handle_simple_irq);
  371. set_irq_flags(irq, IRQF_VALID);
  372. }
  373. binten |= BIT(bank);
  374. }
  375. done:
  376. /* BINTEN -- per-bank interrupt enable. genirq would also let these
  377. * bits be set/cleared dynamically.
  378. */
  379. __raw_writel(binten, gpio_base + 0x08);
  380. printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
  381. return 0;
  382. }