gpio-pl061.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 *dev, const struct amba_id *id)
  180. {
  181. struct pl061_platform_data *pdata;
  182. struct pl061_gpio *chip;
  183. int ret, irq, i;
  184. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  185. if (chip == NULL)
  186. return -ENOMEM;
  187. pdata = dev->dev.platform_data;
  188. if (pdata) {
  189. chip->gc.base = pdata->gpio_base;
  190. chip->irq_base = pdata->irq_base;
  191. } else if (dev->dev.of_node) {
  192. chip->gc.base = -1;
  193. chip->irq_base = 0;
  194. } else {
  195. ret = -ENODEV;
  196. goto free_mem;
  197. }
  198. if (!request_mem_region(dev->res.start,
  199. resource_size(&dev->res), "pl061")) {
  200. ret = -EBUSY;
  201. goto free_mem;
  202. }
  203. chip->base = ioremap(dev->res.start, resource_size(&dev->res));
  204. if (chip->base == NULL) {
  205. ret = -ENOMEM;
  206. goto release_region;
  207. }
  208. spin_lock_init(&chip->lock);
  209. chip->gc.direction_input = pl061_direction_input;
  210. chip->gc.direction_output = pl061_direction_output;
  211. chip->gc.get = pl061_get_value;
  212. chip->gc.set = pl061_set_value;
  213. chip->gc.to_irq = pl061_to_irq;
  214. chip->gc.ngpio = PL061_GPIO_NR;
  215. chip->gc.label = dev_name(&dev->dev);
  216. chip->gc.dev = &dev->dev;
  217. chip->gc.owner = THIS_MODULE;
  218. ret = gpiochip_add(&chip->gc);
  219. if (ret)
  220. goto iounmap;
  221. /*
  222. * irq_chip support
  223. */
  224. if (chip->irq_base <= 0)
  225. return 0;
  226. pl061_init_gc(chip, chip->irq_base);
  227. writeb(0, chip->base + GPIOIE); /* disable irqs */
  228. irq = dev->irq[0];
  229. if (irq < 0) {
  230. ret = -ENODEV;
  231. goto iounmap;
  232. }
  233. irq_set_chained_handler(irq, pl061_irq_handler);
  234. irq_set_handler_data(irq, chip);
  235. for (i = 0; i < PL061_GPIO_NR; i++) {
  236. if (pdata) {
  237. if (pdata->directions & (1 << i))
  238. pl061_direction_output(&chip->gc, i,
  239. pdata->values & (1 << i));
  240. else
  241. pl061_direction_input(&chip->gc, i);
  242. }
  243. }
  244. amba_set_drvdata(dev, chip);
  245. return 0;
  246. iounmap:
  247. iounmap(chip->base);
  248. release_region:
  249. release_mem_region(dev->res.start, resource_size(&dev->res));
  250. free_mem:
  251. kfree(chip);
  252. return ret;
  253. }
  254. #ifdef CONFIG_PM
  255. static int pl061_suspend(struct device *dev)
  256. {
  257. struct pl061_gpio *chip = dev_get_drvdata(dev);
  258. int offset;
  259. chip->csave_regs.gpio_data = 0;
  260. chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
  261. chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
  262. chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
  263. chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
  264. chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
  265. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  266. if (chip->csave_regs.gpio_dir & (1 << offset))
  267. chip->csave_regs.gpio_data |=
  268. pl061_get_value(&chip->gc, offset) << offset;
  269. }
  270. return 0;
  271. }
  272. static int pl061_resume(struct device *dev)
  273. {
  274. struct pl061_gpio *chip = dev_get_drvdata(dev);
  275. int offset;
  276. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  277. if (chip->csave_regs.gpio_dir & (1 << offset))
  278. pl061_direction_output(&chip->gc, offset,
  279. chip->csave_regs.gpio_data &
  280. (1 << offset));
  281. else
  282. pl061_direction_input(&chip->gc, offset);
  283. }
  284. writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
  285. writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
  286. writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
  287. writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
  288. return 0;
  289. }
  290. static const struct dev_pm_ops pl061_dev_pm_ops = {
  291. .suspend = pl061_suspend,
  292. .resume = pl061_resume,
  293. .freeze = pl061_suspend,
  294. .restore = pl061_resume,
  295. };
  296. #endif
  297. static struct amba_id pl061_ids[] = {
  298. {
  299. .id = 0x00041061,
  300. .mask = 0x000fffff,
  301. },
  302. { 0, 0 },
  303. };
  304. MODULE_DEVICE_TABLE(amba, pl061_ids);
  305. static struct amba_driver pl061_gpio_driver = {
  306. .drv = {
  307. .name = "pl061_gpio",
  308. #ifdef CONFIG_PM
  309. .pm = &pl061_dev_pm_ops,
  310. #endif
  311. },
  312. .id_table = pl061_ids,
  313. .probe = pl061_probe,
  314. };
  315. static int __init pl061_gpio_init(void)
  316. {
  317. return amba_driver_register(&pl061_gpio_driver);
  318. }
  319. subsys_initcall(pl061_gpio_init);
  320. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  321. MODULE_DESCRIPTION("PL061 GPIO driver");
  322. MODULE_LICENSE("GPL");