gpio-pl061.c 9.4 KB

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