gpio-pl061.c 11 KB

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