gpio-pl061.c 9.8 KB

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