gpio-pl061.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/amba/pl061.h>
  27. #include <linux/slab.h>
  28. #include <linux/pinctrl/consumer.h>
  29. #include <linux/pm.h>
  30. #define GPIODIR 0x400
  31. #define GPIOIS 0x404
  32. #define GPIOIBE 0x408
  33. #define GPIOIEV 0x40C
  34. #define GPIOIE 0x410
  35. #define GPIORIS 0x414
  36. #define GPIOMIS 0x418
  37. #define GPIOIC 0x41C
  38. #define PL061_GPIO_NR 8
  39. #ifdef CONFIG_PM
  40. struct pl061_context_save_regs {
  41. u8 gpio_data;
  42. u8 gpio_dir;
  43. u8 gpio_is;
  44. u8 gpio_ibe;
  45. u8 gpio_iev;
  46. u8 gpio_ie;
  47. };
  48. #endif
  49. struct pl061_gpio {
  50. spinlock_t lock;
  51. void __iomem *base;
  52. struct gpio_chip gc;
  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_gpio *chip = gpiochip_get_data(gc);
  60. return !(readb(chip->base + GPIODIR) & BIT(offset));
  61. }
  62. static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
  63. {
  64. struct pl061_gpio *chip = gpiochip_get_data(gc);
  65. unsigned long flags;
  66. unsigned char gpiodir;
  67. spin_lock_irqsave(&chip->lock, flags);
  68. gpiodir = readb(chip->base + GPIODIR);
  69. gpiodir &= ~(BIT(offset));
  70. writeb(gpiodir, chip->base + GPIODIR);
  71. spin_unlock_irqrestore(&chip->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_gpio *chip = gpiochip_get_data(gc);
  78. unsigned long flags;
  79. unsigned char gpiodir;
  80. spin_lock_irqsave(&chip->lock, flags);
  81. writeb(!!value << offset, chip->base + (BIT(offset + 2)));
  82. gpiodir = readb(chip->base + GPIODIR);
  83. gpiodir |= BIT(offset);
  84. writeb(gpiodir, chip->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, chip->base + (BIT(offset + 2)));
  90. spin_unlock_irqrestore(&chip->lock, flags);
  91. return 0;
  92. }
  93. static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
  94. {
  95. struct pl061_gpio *chip = gpiochip_get_data(gc);
  96. return !!readb(chip->base + (BIT(offset + 2)));
  97. }
  98. static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
  99. {
  100. struct pl061_gpio *chip = gpiochip_get_data(gc);
  101. writeb(!!value << offset, chip->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_gpio *chip = 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(&chip->lock, flags);
  123. gpioiev = readb(chip->base + GPIOIEV);
  124. gpiois = readb(chip->base + GPIOIS);
  125. gpioibe = readb(chip->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, chip->base + GPIOIS);
  174. writeb(gpioibe, chip->base + GPIOIBE);
  175. writeb(gpioiev, chip->base + GPIOIEV);
  176. spin_unlock_irqrestore(&chip->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_gpio *chip = gpiochip_get_data(gc);
  185. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  186. chained_irq_enter(irqchip, desc);
  187. pending = readb(chip->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_gpio *chip = gpiochip_get_data(gc);
  199. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  200. u8 gpioie;
  201. spin_lock(&chip->lock);
  202. gpioie = readb(chip->base + GPIOIE) & ~mask;
  203. writeb(gpioie, chip->base + GPIOIE);
  204. spin_unlock(&chip->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_gpio *chip = gpiochip_get_data(gc);
  210. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  211. u8 gpioie;
  212. spin_lock(&chip->lock);
  213. gpioie = readb(chip->base + GPIOIE) | mask;
  214. writeb(gpioie, chip->base + GPIOIE);
  215. spin_unlock(&chip->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_gpio *chip = gpiochip_get_data(gc);
  229. u8 mask = BIT(irqd_to_hwirq(d) % PL061_GPIO_NR);
  230. spin_lock(&chip->lock);
  231. writeb(mask, chip->base + GPIOIC);
  232. spin_unlock(&chip->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. return irq_set_irq_wake(gc->irq_parent, state);
  238. }
  239. static struct irq_chip pl061_irqchip = {
  240. .name = "pl061",
  241. .irq_ack = pl061_irq_ack,
  242. .irq_mask = pl061_irq_mask,
  243. .irq_unmask = pl061_irq_unmask,
  244. .irq_set_type = pl061_irq_type,
  245. .irq_set_wake = pl061_irq_set_wake,
  246. };
  247. static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
  248. {
  249. struct device *dev = &adev->dev;
  250. struct pl061_platform_data *pdata = dev_get_platdata(dev);
  251. struct pl061_gpio *chip;
  252. int ret, irq, i, irq_base;
  253. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  254. if (chip == NULL)
  255. return -ENOMEM;
  256. if (pdata) {
  257. chip->gc.base = pdata->gpio_base;
  258. irq_base = pdata->irq_base;
  259. if (irq_base <= 0) {
  260. dev_err(&adev->dev, "invalid IRQ base in pdata\n");
  261. return -ENODEV;
  262. }
  263. } else {
  264. chip->gc.base = -1;
  265. irq_base = 0;
  266. }
  267. chip->base = devm_ioremap_resource(dev, &adev->res);
  268. if (IS_ERR(chip->base))
  269. return PTR_ERR(chip->base);
  270. spin_lock_init(&chip->lock);
  271. if (of_property_read_bool(dev->of_node, "gpio-ranges")) {
  272. chip->gc.request = gpiochip_generic_request;
  273. chip->gc.free = gpiochip_generic_free;
  274. }
  275. chip->gc.get_direction = pl061_get_direction;
  276. chip->gc.direction_input = pl061_direction_input;
  277. chip->gc.direction_output = pl061_direction_output;
  278. chip->gc.get = pl061_get_value;
  279. chip->gc.set = pl061_set_value;
  280. chip->gc.ngpio = PL061_GPIO_NR;
  281. chip->gc.label = dev_name(dev);
  282. chip->gc.parent = dev;
  283. chip->gc.owner = THIS_MODULE;
  284. ret = gpiochip_add_data(&chip->gc, chip);
  285. if (ret)
  286. return ret;
  287. /*
  288. * irq_chip support
  289. */
  290. writeb(0, chip->base + GPIOIE); /* disable irqs */
  291. irq = adev->irq[0];
  292. if (irq < 0) {
  293. dev_err(&adev->dev, "invalid IRQ\n");
  294. return -ENODEV;
  295. }
  296. ret = gpiochip_irqchip_add(&chip->gc, &pl061_irqchip,
  297. irq_base, handle_bad_irq,
  298. IRQ_TYPE_NONE);
  299. if (ret) {
  300. dev_info(&adev->dev, "could not add irqchip\n");
  301. return ret;
  302. }
  303. gpiochip_set_chained_irqchip(&chip->gc, &pl061_irqchip,
  304. irq, pl061_irq_handler);
  305. for (i = 0; i < PL061_GPIO_NR; i++) {
  306. if (pdata) {
  307. if (pdata->directions & (BIT(i)))
  308. pl061_direction_output(&chip->gc, i,
  309. pdata->values & (BIT(i)));
  310. else
  311. pl061_direction_input(&chip->gc, i);
  312. }
  313. }
  314. amba_set_drvdata(adev, chip);
  315. dev_info(&adev->dev, "PL061 GPIO chip @%pa registered\n",
  316. &adev->res.start);
  317. return 0;
  318. }
  319. #ifdef CONFIG_PM
  320. static int pl061_suspend(struct device *dev)
  321. {
  322. struct pl061_gpio *chip = dev_get_drvdata(dev);
  323. int offset;
  324. chip->csave_regs.gpio_data = 0;
  325. chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
  326. chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
  327. chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
  328. chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
  329. chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
  330. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  331. if (chip->csave_regs.gpio_dir & (BIT(offset)))
  332. chip->csave_regs.gpio_data |=
  333. pl061_get_value(&chip->gc, offset) << offset;
  334. }
  335. return 0;
  336. }
  337. static int pl061_resume(struct device *dev)
  338. {
  339. struct pl061_gpio *chip = dev_get_drvdata(dev);
  340. int offset;
  341. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  342. if (chip->csave_regs.gpio_dir & (BIT(offset)))
  343. pl061_direction_output(&chip->gc, offset,
  344. chip->csave_regs.gpio_data &
  345. (BIT(offset)));
  346. else
  347. pl061_direction_input(&chip->gc, offset);
  348. }
  349. writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
  350. writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
  351. writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
  352. writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
  353. return 0;
  354. }
  355. static const struct dev_pm_ops pl061_dev_pm_ops = {
  356. .suspend = pl061_suspend,
  357. .resume = pl061_resume,
  358. .freeze = pl061_suspend,
  359. .restore = pl061_resume,
  360. };
  361. #endif
  362. static struct amba_id pl061_ids[] = {
  363. {
  364. .id = 0x00041061,
  365. .mask = 0x000fffff,
  366. },
  367. { 0, 0 },
  368. };
  369. static struct amba_driver pl061_gpio_driver = {
  370. .drv = {
  371. .name = "pl061_gpio",
  372. #ifdef CONFIG_PM
  373. .pm = &pl061_dev_pm_ops,
  374. #endif
  375. },
  376. .id_table = pl061_ids,
  377. .probe = pl061_probe,
  378. };
  379. static int __init pl061_gpio_init(void)
  380. {
  381. return amba_driver_register(&pl061_gpio_driver);
  382. }
  383. device_initcall(pl061_gpio_init);