gpio-pl061.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/bitops.h>
  19. #include <linux/workqueue.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/pm.h>
  26. #include <asm/mach/irq.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. /* Each of the two spinlocks protects a different set of hardware
  48. * regiters and data structurs. This decouples the code of the IRQ from
  49. * the GPIO code. This also makes the case of a GPIO routine call from
  50. * the IRQ code simpler.
  51. */
  52. spinlock_t lock; /* GPIO registers */
  53. void __iomem *base;
  54. int irq_base;
  55. struct irq_chip_generic *irq_gc;
  56. struct gpio_chip gc;
  57. #ifdef CONFIG_PM
  58. struct pl061_context_save_regs csave_regs;
  59. #endif
  60. };
  61. static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
  62. {
  63. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  64. unsigned long flags;
  65. unsigned char gpiodir;
  66. if (offset >= gc->ngpio)
  67. return -EINVAL;
  68. spin_lock_irqsave(&chip->lock, flags);
  69. gpiodir = readb(chip->base + GPIODIR);
  70. gpiodir &= ~(1 << offset);
  71. writeb(gpiodir, chip->base + GPIODIR);
  72. spin_unlock_irqrestore(&chip->lock, flags);
  73. return 0;
  74. }
  75. static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
  76. int value)
  77. {
  78. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  79. unsigned long flags;
  80. unsigned char gpiodir;
  81. if (offset >= gc->ngpio)
  82. return -EINVAL;
  83. spin_lock_irqsave(&chip->lock, flags);
  84. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  85. gpiodir = readb(chip->base + GPIODIR);
  86. gpiodir |= 1 << offset;
  87. writeb(gpiodir, chip->base + GPIODIR);
  88. /*
  89. * gpio value is set again, because pl061 doesn't allow to set value of
  90. * a gpio pin before configuring it in OUT mode.
  91. */
  92. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  93. spin_unlock_irqrestore(&chip->lock, flags);
  94. return 0;
  95. }
  96. static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
  97. {
  98. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  99. return !!readb(chip->base + (1 << (offset + 2)));
  100. }
  101. static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
  102. {
  103. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  104. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  105. }
  106. static int pl061_to_irq(struct gpio_chip *gc, unsigned offset)
  107. {
  108. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  109. if (chip->irq_base <= 0)
  110. return -EINVAL;
  111. return chip->irq_base + offset;
  112. }
  113. static int pl061_irq_type(struct irq_data *d, unsigned trigger)
  114. {
  115. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  116. struct pl061_gpio *chip = gc->private;
  117. int offset = d->irq - chip->irq_base;
  118. unsigned long flags;
  119. u8 gpiois, gpioibe, gpioiev;
  120. if (offset < 0 || offset >= PL061_GPIO_NR)
  121. return -EINVAL;
  122. raw_spin_lock_irqsave(&gc->lock, flags);
  123. gpioiev = readb(chip->base + GPIOIEV);
  124. gpiois = readb(chip->base + GPIOIS);
  125. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  126. gpiois |= 1 << offset;
  127. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  128. gpioiev |= 1 << offset;
  129. else
  130. gpioiev &= ~(1 << offset);
  131. } else
  132. gpiois &= ~(1 << offset);
  133. writeb(gpiois, chip->base + GPIOIS);
  134. gpioibe = readb(chip->base + GPIOIBE);
  135. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  136. gpioibe |= 1 << offset;
  137. else {
  138. gpioibe &= ~(1 << offset);
  139. if (trigger & IRQ_TYPE_EDGE_RISING)
  140. gpioiev |= 1 << offset;
  141. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  142. gpioiev &= ~(1 << offset);
  143. }
  144. writeb(gpioibe, chip->base + GPIOIBE);
  145. writeb(gpioiev, chip->base + GPIOIEV);
  146. raw_spin_unlock_irqrestore(&gc->lock, flags);
  147. return 0;
  148. }
  149. static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
  150. {
  151. unsigned long pending;
  152. int offset;
  153. struct pl061_gpio *chip = irq_desc_get_handler_data(desc);
  154. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  155. chained_irq_enter(irqchip, desc);
  156. pending = readb(chip->base + GPIOMIS);
  157. writeb(pending, chip->base + GPIOIC);
  158. if (pending) {
  159. for_each_set_bit(offset, &pending, PL061_GPIO_NR)
  160. generic_handle_irq(pl061_to_irq(&chip->gc, offset));
  161. }
  162. chained_irq_exit(irqchip, desc);
  163. }
  164. static void __init pl061_init_gc(struct pl061_gpio *chip, int irq_base)
  165. {
  166. struct irq_chip_type *ct;
  167. chip->irq_gc = irq_alloc_generic_chip("gpio-pl061", 1, irq_base,
  168. chip->base, handle_simple_irq);
  169. chip->irq_gc->private = chip;
  170. ct = chip->irq_gc->chip_types;
  171. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  172. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  173. ct->chip.irq_set_type = pl061_irq_type;
  174. ct->chip.irq_set_wake = irq_gc_set_wake;
  175. ct->regs.mask = GPIOIE;
  176. irq_setup_generic_chip(chip->irq_gc, IRQ_MSK(PL061_GPIO_NR),
  177. IRQ_GC_INIT_NESTED_LOCK, IRQ_NOREQUEST, 0);
  178. }
  179. static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
  180. {
  181. struct device *dev = &adev->dev;
  182. struct pl061_platform_data *pdata = dev->platform_data;
  183. struct pl061_gpio *chip;
  184. int ret, irq, i;
  185. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  186. if (chip == NULL)
  187. return -ENOMEM;
  188. if (pdata) {
  189. chip->gc.base = pdata->gpio_base;
  190. chip->irq_base = pdata->irq_base;
  191. } else if (adev->dev.of_node) {
  192. chip->gc.base = -1;
  193. chip->irq_base = 0;
  194. } else
  195. return -ENODEV;
  196. if (!devm_request_mem_region(dev, adev->res.start,
  197. resource_size(&adev->res), "pl061"))
  198. return -EBUSY;
  199. chip->base = devm_ioremap(dev, adev->res.start,
  200. resource_size(&adev->res));
  201. if (chip->base == NULL)
  202. return -ENOMEM;
  203. spin_lock_init(&chip->lock);
  204. chip->gc.direction_input = pl061_direction_input;
  205. chip->gc.direction_output = pl061_direction_output;
  206. chip->gc.get = pl061_get_value;
  207. chip->gc.set = pl061_set_value;
  208. chip->gc.to_irq = pl061_to_irq;
  209. chip->gc.ngpio = PL061_GPIO_NR;
  210. chip->gc.label = dev_name(dev);
  211. chip->gc.dev = dev;
  212. chip->gc.owner = THIS_MODULE;
  213. ret = gpiochip_add(&chip->gc);
  214. if (ret)
  215. return ret;
  216. /*
  217. * irq_chip support
  218. */
  219. if (chip->irq_base <= 0)
  220. return 0;
  221. pl061_init_gc(chip, chip->irq_base);
  222. writeb(0, chip->base + GPIOIE); /* disable irqs */
  223. irq = adev->irq[0];
  224. if (irq < 0)
  225. return -ENODEV;
  226. irq_set_chained_handler(irq, pl061_irq_handler);
  227. irq_set_handler_data(irq, chip);
  228. for (i = 0; i < PL061_GPIO_NR; i++) {
  229. if (pdata) {
  230. if (pdata->directions & (1 << i))
  231. pl061_direction_output(&chip->gc, i,
  232. pdata->values & (1 << i));
  233. else
  234. pl061_direction_input(&chip->gc, i);
  235. }
  236. }
  237. amba_set_drvdata(adev, chip);
  238. return 0;
  239. }
  240. #ifdef CONFIG_PM
  241. static int pl061_suspend(struct device *dev)
  242. {
  243. struct pl061_gpio *chip = dev_get_drvdata(dev);
  244. int offset;
  245. chip->csave_regs.gpio_data = 0;
  246. chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
  247. chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
  248. chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
  249. chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
  250. chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
  251. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  252. if (chip->csave_regs.gpio_dir & (1 << offset))
  253. chip->csave_regs.gpio_data |=
  254. pl061_get_value(&chip->gc, offset) << offset;
  255. }
  256. return 0;
  257. }
  258. static int pl061_resume(struct device *dev)
  259. {
  260. struct pl061_gpio *chip = dev_get_drvdata(dev);
  261. int offset;
  262. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  263. if (chip->csave_regs.gpio_dir & (1 << offset))
  264. pl061_direction_output(&chip->gc, offset,
  265. chip->csave_regs.gpio_data &
  266. (1 << offset));
  267. else
  268. pl061_direction_input(&chip->gc, offset);
  269. }
  270. writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
  271. writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
  272. writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
  273. writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
  274. return 0;
  275. }
  276. static const struct dev_pm_ops pl061_dev_pm_ops = {
  277. .suspend = pl061_suspend,
  278. .resume = pl061_resume,
  279. .freeze = pl061_suspend,
  280. .restore = pl061_resume,
  281. };
  282. #endif
  283. static struct amba_id pl061_ids[] = {
  284. {
  285. .id = 0x00041061,
  286. .mask = 0x000fffff,
  287. },
  288. { 0, 0 },
  289. };
  290. MODULE_DEVICE_TABLE(amba, pl061_ids);
  291. static struct amba_driver pl061_gpio_driver = {
  292. .drv = {
  293. .name = "pl061_gpio",
  294. #ifdef CONFIG_PM
  295. .pm = &pl061_dev_pm_ops,
  296. #endif
  297. },
  298. .id_table = pl061_ids,
  299. .probe = pl061_probe,
  300. };
  301. static int __init pl061_gpio_init(void)
  302. {
  303. return amba_driver_register(&pl061_gpio_driver);
  304. }
  305. subsys_initcall(pl061_gpio_init);
  306. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  307. MODULE_DESCRIPTION("PL061 GPIO driver");
  308. MODULE_LICENSE("GPL");