gpio-pl061.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright (C) 2008, 2009 Provigent Ltd.
  3. *
  4. * Author: Baruch Siach <baruch@tkos.co.il>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Driver for the ARM PrimeCell(tm) General Purpose Input/Output (PL061)
  11. *
  12. * Data sheet: ARM DDI 0190B, September 2000
  13. */
  14. #include <linux/spinlock.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/irqchip/chained_irq.h>
  22. #include <linux/bitops.h>
  23. #include <linux/gpio.h>
  24. #include <linux/device.h>
  25. #include <linux/amba/bus.h>
  26. #include <linux/slab.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <linux/pm.h>
  29. #define GPIODIR 0x400
  30. #define GPIOIS 0x404
  31. #define GPIOIBE 0x408
  32. #define GPIOIEV 0x40C
  33. #define GPIOIE 0x410
  34. #define GPIORIS 0x414
  35. #define GPIOMIS 0x418
  36. #define GPIOIC 0x41C
  37. #define PL061_GPIO_NR 8
  38. #ifdef CONFIG_PM
  39. struct pl061_context_save_regs {
  40. u8 gpio_data;
  41. u8 gpio_dir;
  42. u8 gpio_is;
  43. u8 gpio_ibe;
  44. u8 gpio_iev;
  45. u8 gpio_ie;
  46. };
  47. #endif
  48. struct pl061 {
  49. spinlock_t lock;
  50. void __iomem *base;
  51. struct gpio_chip gc;
  52. int parent_irq;
  53. #ifdef CONFIG_PM
  54. struct pl061_context_save_regs csave_regs;
  55. #endif
  56. };
  57. static int pl061_get_direction(struct gpio_chip *gc, unsigned offset)
  58. {
  59. struct pl061 *pl061 = gpiochip_get_data(gc);
  60. return !(readb(pl061->base + GPIODIR) & BIT(offset));
  61. }
  62. static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
  63. {
  64. struct pl061 *pl061 = gpiochip_get_data(gc);
  65. unsigned long flags;
  66. unsigned char gpiodir;
  67. spin_lock_irqsave(&pl061->lock, flags);
  68. gpiodir = readb(pl061->base + GPIODIR);
  69. gpiodir &= ~(BIT(offset));
  70. writeb(gpiodir, pl061->base + GPIODIR);
  71. spin_unlock_irqrestore(&pl061->lock, flags);
  72. return 0;
  73. }
  74. static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
  75. int value)
  76. {
  77. struct pl061 *pl061 = gpiochip_get_data(gc);
  78. unsigned long flags;
  79. unsigned char gpiodir;
  80. spin_lock_irqsave(&pl061->lock, flags);
  81. writeb(!!value << offset, pl061->base + (BIT(offset + 2)));
  82. gpiodir = readb(pl061->base + GPIODIR);
  83. gpiodir |= BIT(offset);
  84. writeb(gpiodir, pl061->base + GPIODIR);
  85. /*
  86. * gpio value is set again, because pl061 doesn't allow to set value of
  87. * a gpio pin before configuring it in OUT mode.
  88. */
  89. writeb(!!value << offset, pl061->base + (BIT(offset + 2)));
  90. spin_unlock_irqrestore(&pl061->lock, flags);
  91. return 0;
  92. }
  93. static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
  94. {
  95. struct pl061 *pl061 = gpiochip_get_data(gc);
  96. return !!readb(pl061->base + (BIT(offset + 2)));
  97. }
  98. static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
  99. {
  100. struct pl061 *pl061 = gpiochip_get_data(gc);
  101. writeb(!!value << offset, pl061->base + (BIT(offset + 2)));
  102. }
  103. static int pl061_irq_type(struct irq_data *d, unsigned trigger)
  104. {
  105. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  106. struct pl061 *pl061 = gpiochip_get_data(gc);
  107. int offset = irqd_to_hwirq(d);
  108. unsigned long flags;
  109. u8 gpiois, gpioibe, gpioiev;
  110. u8 bit = BIT(offset);
  111. if (offset < 0 || offset >= PL061_GPIO_NR)
  112. return -EINVAL;
  113. if ((trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) &&
  114. (trigger & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)))
  115. {
  116. dev_err(gc->parent,
  117. "trying to configure line %d for both level and edge "
  118. "detection, choose one!\n",
  119. offset);
  120. return -EINVAL;
  121. }
  122. spin_lock_irqsave(&pl061->lock, flags);
  123. gpioiev = readb(pl061->base + GPIOIEV);
  124. gpiois = readb(pl061->base + GPIOIS);
  125. gpioibe = readb(pl061->base + GPIOIBE);
  126. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  127. bool polarity = trigger & IRQ_TYPE_LEVEL_HIGH;
  128. /* Disable edge detection */
  129. gpioibe &= ~bit;
  130. /* Enable level detection */
  131. gpiois |= bit;
  132. /* Select polarity */
  133. if (polarity)
  134. gpioiev |= bit;
  135. else
  136. gpioiev &= ~bit;
  137. irq_set_handler_locked(d, handle_level_irq);
  138. dev_dbg(gc->parent, "line %d: IRQ on %s level\n",
  139. offset,
  140. polarity ? "HIGH" : "LOW");
  141. } else if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  142. /* Disable level detection */
  143. gpiois &= ~bit;
  144. /* Select both edges, setting this makes GPIOEV be ignored */
  145. gpioibe |= bit;
  146. irq_set_handler_locked(d, handle_edge_irq);
  147. dev_dbg(gc->parent, "line %d: IRQ on both edges\n", offset);
  148. } else if ((trigger & IRQ_TYPE_EDGE_RISING) ||
  149. (trigger & IRQ_TYPE_EDGE_FALLING)) {
  150. bool rising = trigger & IRQ_TYPE_EDGE_RISING;
  151. /* Disable level detection */
  152. gpiois &= ~bit;
  153. /* Clear detection on both edges */
  154. gpioibe &= ~bit;
  155. /* Select edge */
  156. if (rising)
  157. gpioiev |= bit;
  158. else
  159. gpioiev &= ~bit;
  160. irq_set_handler_locked(d, handle_edge_irq);
  161. dev_dbg(gc->parent, "line %d: IRQ on %s edge\n",
  162. offset,
  163. rising ? "RISING" : "FALLING");
  164. } else {
  165. /* No trigger: disable everything */
  166. gpiois &= ~bit;
  167. gpioibe &= ~bit;
  168. gpioiev &= ~bit;
  169. irq_set_handler_locked(d, handle_bad_irq);
  170. dev_warn(gc->parent, "no trigger selected for line %d\n",
  171. offset);
  172. }
  173. writeb(gpiois, pl061->base + GPIOIS);
  174. writeb(gpioibe, pl061->base + GPIOIBE);
  175. writeb(gpioiev, pl061->base + GPIOIEV);
  176. spin_unlock_irqrestore(&pl061->lock, flags);
  177. return 0;
  178. }
  179. static void pl061_irq_handler(struct irq_desc *desc)
  180. {
  181. unsigned long pending;
  182. int offset;
  183. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  184. struct pl061 *pl061 = gpiochip_get_data(gc);
  185. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  186. chained_irq_enter(irqchip, desc);
  187. pending = readb(pl061->base + GPIOMIS);
  188. if (pending) {
  189. for_each_set_bit(offset, &pending, PL061_GPIO_NR)
  190. generic_handle_irq(irq_find_mapping(gc->irqdomain,
  191. offset));
  192. }
  193. chained_irq_exit(irqchip, desc);
  194. }
  195. static void pl061_irq_mask(struct irq_data *d)
  196. {
  197. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  198. struct pl061 *pl061 = gpiochip_get_data(gc);
  199. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  200. u8 gpioie;
  201. spin_lock(&pl061->lock);
  202. gpioie = readb(pl061->base + GPIOIE) & ~mask;
  203. writeb(gpioie, pl061->base + GPIOIE);
  204. spin_unlock(&pl061->lock);
  205. }
  206. static void pl061_irq_unmask(struct irq_data *d)
  207. {
  208. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  209. struct pl061 *pl061 = gpiochip_get_data(gc);
  210. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  211. u8 gpioie;
  212. spin_lock(&pl061->lock);
  213. gpioie = readb(pl061->base + GPIOIE) | mask;
  214. writeb(gpioie, pl061->base + GPIOIE);
  215. spin_unlock(&pl061->lock);
  216. }
  217. /**
  218. * pl061_irq_ack() - ACK an edge IRQ
  219. * @d: IRQ data for this IRQ
  220. *
  221. * This gets called from the edge IRQ handler to ACK the edge IRQ
  222. * in the GPIOIC (interrupt-clear) register. For level IRQs this is
  223. * not needed: these go away when the level signal goes away.
  224. */
  225. static void pl061_irq_ack(struct irq_data *d)
  226. {
  227. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  228. struct pl061 *pl061 = gpiochip_get_data(gc);
  229. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  230. spin_lock(&pl061->lock);
  231. writeb(mask, pl061->base + GPIOIC);
  232. spin_unlock(&pl061->lock);
  233. }
  234. static int pl061_irq_set_wake(struct irq_data *d, unsigned int state)
  235. {
  236. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  237. struct pl061 *pl061 = gpiochip_get_data(gc);
  238. return irq_set_irq_wake(pl061->parent_irq, state);
  239. }
  240. static struct irq_chip pl061_irqchip = {
  241. .name = "pl061",
  242. .irq_ack = pl061_irq_ack,
  243. .irq_mask = pl061_irq_mask,
  244. .irq_unmask = pl061_irq_unmask,
  245. .irq_set_type = pl061_irq_type,
  246. .irq_set_wake = pl061_irq_set_wake,
  247. };
  248. static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
  249. {
  250. struct device *dev = &adev->dev;
  251. struct pl061 *pl061;
  252. int ret, irq;
  253. pl061 = devm_kzalloc(dev, sizeof(*pl061), GFP_KERNEL);
  254. if (pl061 == NULL)
  255. return -ENOMEM;
  256. pl061->base = devm_ioremap_resource(dev, &adev->res);
  257. if (IS_ERR(pl061->base))
  258. return PTR_ERR(pl061->base);
  259. spin_lock_init(&pl061->lock);
  260. if (of_property_read_bool(dev->of_node, "gpio-ranges")) {
  261. pl061->gc.request = gpiochip_generic_request;
  262. pl061->gc.free = gpiochip_generic_free;
  263. }
  264. pl061->gc.base = -1;
  265. pl061->gc.get_direction = pl061_get_direction;
  266. pl061->gc.direction_input = pl061_direction_input;
  267. pl061->gc.direction_output = pl061_direction_output;
  268. pl061->gc.get = pl061_get_value;
  269. pl061->gc.set = pl061_set_value;
  270. pl061->gc.ngpio = PL061_GPIO_NR;
  271. pl061->gc.label = dev_name(dev);
  272. pl061->gc.parent = dev;
  273. pl061->gc.owner = THIS_MODULE;
  274. ret = gpiochip_add_data(&pl061->gc, pl061);
  275. if (ret)
  276. return ret;
  277. /*
  278. * irq_chip support
  279. */
  280. writeb(0, pl061->base + GPIOIE); /* disable irqs */
  281. irq = adev->irq[0];
  282. if (irq < 0) {
  283. dev_err(&adev->dev, "invalid IRQ\n");
  284. return -ENODEV;
  285. }
  286. pl061->parent_irq = irq;
  287. ret = gpiochip_irqchip_add(&pl061->gc, &pl061_irqchip,
  288. 0, handle_bad_irq,
  289. IRQ_TYPE_NONE);
  290. if (ret) {
  291. dev_info(&adev->dev, "could not add irqchip\n");
  292. return ret;
  293. }
  294. gpiochip_set_chained_irqchip(&pl061->gc, &pl061_irqchip,
  295. irq, pl061_irq_handler);
  296. amba_set_drvdata(adev, pl061);
  297. dev_info(&adev->dev, "PL061 GPIO chip @%pa registered\n",
  298. &adev->res.start);
  299. return 0;
  300. }
  301. #ifdef CONFIG_PM
  302. static int pl061_suspend(struct device *dev)
  303. {
  304. struct pl061 *pl061 = dev_get_drvdata(dev);
  305. int offset;
  306. pl061->csave_regs.gpio_data = 0;
  307. pl061->csave_regs.gpio_dir = readb(pl061->base + GPIODIR);
  308. pl061->csave_regs.gpio_is = readb(pl061->base + GPIOIS);
  309. pl061->csave_regs.gpio_ibe = readb(pl061->base + GPIOIBE);
  310. pl061->csave_regs.gpio_iev = readb(pl061->base + GPIOIEV);
  311. pl061->csave_regs.gpio_ie = readb(pl061->base + GPIOIE);
  312. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  313. if (pl061->csave_regs.gpio_dir & (BIT(offset)))
  314. pl061->csave_regs.gpio_data |=
  315. pl061_get_value(&pl061->gc, offset) << offset;
  316. }
  317. return 0;
  318. }
  319. static int pl061_resume(struct device *dev)
  320. {
  321. struct pl061 *pl061 = dev_get_drvdata(dev);
  322. int offset;
  323. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  324. if (pl061->csave_regs.gpio_dir & (BIT(offset)))
  325. pl061_direction_output(&pl061->gc, offset,
  326. pl061->csave_regs.gpio_data &
  327. (BIT(offset)));
  328. else
  329. pl061_direction_input(&pl061->gc, offset);
  330. }
  331. writeb(pl061->csave_regs.gpio_is, pl061->base + GPIOIS);
  332. writeb(pl061->csave_regs.gpio_ibe, pl061->base + GPIOIBE);
  333. writeb(pl061->csave_regs.gpio_iev, pl061->base + GPIOIEV);
  334. writeb(pl061->csave_regs.gpio_ie, pl061->base + GPIOIE);
  335. return 0;
  336. }
  337. static const struct dev_pm_ops pl061_dev_pm_ops = {
  338. .suspend = pl061_suspend,
  339. .resume = pl061_resume,
  340. .freeze = pl061_suspend,
  341. .restore = pl061_resume,
  342. };
  343. #endif
  344. static struct amba_id pl061_ids[] = {
  345. {
  346. .id = 0x00041061,
  347. .mask = 0x000fffff,
  348. },
  349. { 0, 0 },
  350. };
  351. static struct amba_driver pl061_gpio_driver = {
  352. .drv = {
  353. .name = "pl061_gpio",
  354. #ifdef CONFIG_PM
  355. .pm = &pl061_dev_pm_ops,
  356. #endif
  357. },
  358. .id_table = pl061_ids,
  359. .probe = pl061_probe,
  360. };
  361. static int __init pl061_gpio_init(void)
  362. {
  363. return amba_driver_register(&pl061_gpio_driver);
  364. }
  365. device_initcall(pl061_gpio_init);