gpio-pl061.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/irqdomain.h>
  19. #include <linux/irqchip/chained_irq.h>
  20. #include <linux/bitops.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/gpio.h>
  23. #include <linux/device.h>
  24. #include <linux/amba/bus.h>
  25. #include <linux/amba/pl061.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_gpio {
  49. spinlock_t lock;
  50. void __iomem *base;
  51. struct irq_domain *domain;
  52. struct gpio_chip gc;
  53. #ifdef CONFIG_PM
  54. struct pl061_context_save_regs csave_regs;
  55. #endif
  56. };
  57. static int pl061_gpio_request(struct gpio_chip *chip, unsigned offset)
  58. {
  59. /*
  60. * Map back to global GPIO space and request muxing, the direction
  61. * parameter does not matter for this controller.
  62. */
  63. int gpio = chip->base + offset;
  64. return pinctrl_request_gpio(gpio);
  65. }
  66. static void pl061_gpio_free(struct gpio_chip *chip, unsigned offset)
  67. {
  68. int gpio = chip->base + offset;
  69. pinctrl_free_gpio(gpio);
  70. }
  71. static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
  72. {
  73. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  74. unsigned long flags;
  75. unsigned char gpiodir;
  76. if (offset >= gc->ngpio)
  77. return -EINVAL;
  78. spin_lock_irqsave(&chip->lock, flags);
  79. gpiodir = readb(chip->base + GPIODIR);
  80. gpiodir &= ~(1 << offset);
  81. writeb(gpiodir, chip->base + GPIODIR);
  82. spin_unlock_irqrestore(&chip->lock, flags);
  83. return 0;
  84. }
  85. static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
  86. int value)
  87. {
  88. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  89. unsigned long flags;
  90. unsigned char gpiodir;
  91. if (offset >= gc->ngpio)
  92. return -EINVAL;
  93. spin_lock_irqsave(&chip->lock, flags);
  94. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  95. gpiodir = readb(chip->base + GPIODIR);
  96. gpiodir |= 1 << offset;
  97. writeb(gpiodir, chip->base + GPIODIR);
  98. /*
  99. * gpio value is set again, because pl061 doesn't allow to set value of
  100. * a gpio pin before configuring it in OUT mode.
  101. */
  102. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  103. spin_unlock_irqrestore(&chip->lock, flags);
  104. return 0;
  105. }
  106. static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
  107. {
  108. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  109. return !!readb(chip->base + (1 << (offset + 2)));
  110. }
  111. static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
  112. {
  113. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  114. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  115. }
  116. static int pl061_to_irq(struct gpio_chip *gc, unsigned offset)
  117. {
  118. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  119. return irq_create_mapping(chip->domain, offset);
  120. }
  121. static int pl061_irq_type(struct irq_data *d, unsigned trigger)
  122. {
  123. struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
  124. int offset = irqd_to_hwirq(d);
  125. unsigned long flags;
  126. u8 gpiois, gpioibe, gpioiev;
  127. if (offset < 0 || offset >= PL061_GPIO_NR)
  128. return -EINVAL;
  129. spin_lock_irqsave(&chip->lock, flags);
  130. gpioiev = readb(chip->base + GPIOIEV);
  131. gpiois = readb(chip->base + GPIOIS);
  132. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  133. gpiois |= 1 << offset;
  134. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  135. gpioiev |= 1 << offset;
  136. else
  137. gpioiev &= ~(1 << offset);
  138. } else
  139. gpiois &= ~(1 << offset);
  140. writeb(gpiois, chip->base + GPIOIS);
  141. gpioibe = readb(chip->base + GPIOIBE);
  142. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  143. gpioibe |= 1 << offset;
  144. else {
  145. gpioibe &= ~(1 << offset);
  146. if (trigger & IRQ_TYPE_EDGE_RISING)
  147. gpioiev |= 1 << offset;
  148. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  149. gpioiev &= ~(1 << offset);
  150. }
  151. writeb(gpioibe, chip->base + GPIOIBE);
  152. writeb(gpioiev, chip->base + GPIOIEV);
  153. spin_unlock_irqrestore(&chip->lock, flags);
  154. return 0;
  155. }
  156. static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
  157. {
  158. unsigned long pending;
  159. int offset;
  160. struct pl061_gpio *chip = irq_desc_get_handler_data(desc);
  161. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  162. chained_irq_enter(irqchip, desc);
  163. pending = readb(chip->base + GPIOMIS);
  164. writeb(pending, chip->base + GPIOIC);
  165. if (pending) {
  166. for_each_set_bit(offset, &pending, PL061_GPIO_NR)
  167. generic_handle_irq(pl061_to_irq(&chip->gc, offset));
  168. }
  169. chained_irq_exit(irqchip, desc);
  170. }
  171. static void pl061_irq_mask(struct irq_data *d)
  172. {
  173. struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
  174. u8 mask = 1 << (irqd_to_hwirq(d) % PL061_GPIO_NR);
  175. u8 gpioie;
  176. spin_lock(&chip->lock);
  177. gpioie = readb(chip->base + GPIOIE) & ~mask;
  178. writeb(gpioie, chip->base + GPIOIE);
  179. spin_unlock(&chip->lock);
  180. }
  181. static void pl061_irq_unmask(struct irq_data *d)
  182. {
  183. struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
  184. u8 mask = 1 << (irqd_to_hwirq(d) % PL061_GPIO_NR);
  185. u8 gpioie;
  186. spin_lock(&chip->lock);
  187. gpioie = readb(chip->base + GPIOIE) | mask;
  188. writeb(gpioie, chip->base + GPIOIE);
  189. spin_unlock(&chip->lock);
  190. }
  191. static struct irq_chip pl061_irqchip = {
  192. .name = "pl061 gpio",
  193. .irq_mask = pl061_irq_mask,
  194. .irq_unmask = pl061_irq_unmask,
  195. .irq_set_type = pl061_irq_type,
  196. };
  197. static int pl061_irq_map(struct irq_domain *d, unsigned int virq,
  198. irq_hw_number_t hw)
  199. {
  200. struct pl061_gpio *chip = d->host_data;
  201. irq_set_chip_and_handler_name(virq, &pl061_irqchip, handle_simple_irq,
  202. "pl061");
  203. irq_set_chip_data(virq, chip);
  204. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  205. return 0;
  206. }
  207. static const struct irq_domain_ops pl061_domain_ops = {
  208. .map = pl061_irq_map,
  209. .xlate = irq_domain_xlate_twocell,
  210. };
  211. static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
  212. {
  213. struct device *dev = &adev->dev;
  214. struct pl061_platform_data *pdata = dev_get_platdata(dev);
  215. struct pl061_gpio *chip;
  216. int ret, irq, i, irq_base;
  217. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  218. if (chip == NULL)
  219. return -ENOMEM;
  220. if (pdata) {
  221. chip->gc.base = pdata->gpio_base;
  222. irq_base = pdata->irq_base;
  223. if (irq_base <= 0)
  224. return -ENODEV;
  225. } else {
  226. chip->gc.base = -1;
  227. irq_base = 0;
  228. }
  229. if (!devm_request_mem_region(dev, adev->res.start,
  230. resource_size(&adev->res), "pl061"))
  231. return -EBUSY;
  232. chip->base = devm_ioremap(dev, adev->res.start,
  233. resource_size(&adev->res));
  234. if (!chip->base)
  235. return -ENOMEM;
  236. chip->domain = irq_domain_add_simple(adev->dev.of_node, PL061_GPIO_NR,
  237. irq_base, &pl061_domain_ops, chip);
  238. if (!chip->domain)
  239. return -ENODEV;
  240. spin_lock_init(&chip->lock);
  241. chip->gc.request = pl061_gpio_request;
  242. chip->gc.free = pl061_gpio_free;
  243. chip->gc.direction_input = pl061_direction_input;
  244. chip->gc.direction_output = pl061_direction_output;
  245. chip->gc.get = pl061_get_value;
  246. chip->gc.set = pl061_set_value;
  247. chip->gc.to_irq = pl061_to_irq;
  248. chip->gc.ngpio = PL061_GPIO_NR;
  249. chip->gc.label = dev_name(dev);
  250. chip->gc.dev = dev;
  251. chip->gc.owner = THIS_MODULE;
  252. ret = gpiochip_add(&chip->gc);
  253. if (ret)
  254. return ret;
  255. /*
  256. * irq_chip support
  257. */
  258. writeb(0, chip->base + GPIOIE); /* disable irqs */
  259. irq = adev->irq[0];
  260. if (irq < 0)
  261. return -ENODEV;
  262. irq_set_chained_handler(irq, pl061_irq_handler);
  263. irq_set_handler_data(irq, chip);
  264. for (i = 0; i < PL061_GPIO_NR; i++) {
  265. if (pdata) {
  266. if (pdata->directions & (1 << i))
  267. pl061_direction_output(&chip->gc, i,
  268. pdata->values & (1 << i));
  269. else
  270. pl061_direction_input(&chip->gc, i);
  271. }
  272. }
  273. amba_set_drvdata(adev, chip);
  274. return 0;
  275. }
  276. #ifdef CONFIG_PM
  277. static int pl061_suspend(struct device *dev)
  278. {
  279. struct pl061_gpio *chip = dev_get_drvdata(dev);
  280. int offset;
  281. chip->csave_regs.gpio_data = 0;
  282. chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
  283. chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
  284. chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
  285. chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
  286. chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
  287. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  288. if (chip->csave_regs.gpio_dir & (1 << offset))
  289. chip->csave_regs.gpio_data |=
  290. pl061_get_value(&chip->gc, offset) << offset;
  291. }
  292. return 0;
  293. }
  294. static int pl061_resume(struct device *dev)
  295. {
  296. struct pl061_gpio *chip = dev_get_drvdata(dev);
  297. int offset;
  298. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  299. if (chip->csave_regs.gpio_dir & (1 << offset))
  300. pl061_direction_output(&chip->gc, offset,
  301. chip->csave_regs.gpio_data &
  302. (1 << offset));
  303. else
  304. pl061_direction_input(&chip->gc, offset);
  305. }
  306. writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
  307. writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
  308. writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
  309. writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
  310. return 0;
  311. }
  312. static const struct dev_pm_ops pl061_dev_pm_ops = {
  313. .suspend = pl061_suspend,
  314. .resume = pl061_resume,
  315. .freeze = pl061_suspend,
  316. .restore = pl061_resume,
  317. };
  318. #endif
  319. static struct amba_id pl061_ids[] = {
  320. {
  321. .id = 0x00041061,
  322. .mask = 0x000fffff,
  323. },
  324. { 0, 0 },
  325. };
  326. MODULE_DEVICE_TABLE(amba, pl061_ids);
  327. static struct amba_driver pl061_gpio_driver = {
  328. .drv = {
  329. .name = "pl061_gpio",
  330. #ifdef CONFIG_PM
  331. .pm = &pl061_dev_pm_ops,
  332. #endif
  333. },
  334. .id_table = pl061_ids,
  335. .probe = pl061_probe,
  336. };
  337. static int __init pl061_gpio_init(void)
  338. {
  339. return amba_driver_register(&pl061_gpio_driver);
  340. }
  341. module_init(pl061_gpio_init);
  342. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  343. MODULE_DESCRIPTION("PL061 GPIO driver");
  344. MODULE_LICENSE("GPL");