gpio-pl061.c 11 KB

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