gpio-pl061.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. spinlock_t lock;
  48. void __iomem *base;
  49. int irq_base;
  50. struct irq_chip_generic *irq_gc;
  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. if (chip->irq_base <= 0)
  105. return -EINVAL;
  106. return chip->irq_base + offset;
  107. }
  108. static int pl061_irq_type(struct irq_data *d, unsigned trigger)
  109. {
  110. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  111. struct pl061_gpio *chip = gc->private;
  112. int offset = d->irq - chip->irq_base;
  113. unsigned long flags;
  114. u8 gpiois, gpioibe, gpioiev;
  115. if (offset < 0 || offset >= PL061_GPIO_NR)
  116. return -EINVAL;
  117. raw_spin_lock_irqsave(&gc->lock, flags);
  118. gpioiev = readb(chip->base + GPIOIEV);
  119. gpiois = readb(chip->base + GPIOIS);
  120. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  121. gpiois |= 1 << offset;
  122. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  123. gpioiev |= 1 << offset;
  124. else
  125. gpioiev &= ~(1 << offset);
  126. } else
  127. gpiois &= ~(1 << offset);
  128. writeb(gpiois, chip->base + GPIOIS);
  129. gpioibe = readb(chip->base + GPIOIBE);
  130. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  131. gpioibe |= 1 << offset;
  132. else {
  133. gpioibe &= ~(1 << offset);
  134. if (trigger & IRQ_TYPE_EDGE_RISING)
  135. gpioiev |= 1 << offset;
  136. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  137. gpioiev &= ~(1 << offset);
  138. }
  139. writeb(gpioibe, chip->base + GPIOIBE);
  140. writeb(gpioiev, chip->base + GPIOIEV);
  141. raw_spin_unlock_irqrestore(&gc->lock, flags);
  142. return 0;
  143. }
  144. static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
  145. {
  146. unsigned long pending;
  147. int offset;
  148. struct pl061_gpio *chip = irq_desc_get_handler_data(desc);
  149. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  150. chained_irq_enter(irqchip, desc);
  151. pending = readb(chip->base + GPIOMIS);
  152. writeb(pending, chip->base + GPIOIC);
  153. if (pending) {
  154. for_each_set_bit(offset, &pending, PL061_GPIO_NR)
  155. generic_handle_irq(pl061_to_irq(&chip->gc, offset));
  156. }
  157. chained_irq_exit(irqchip, desc);
  158. }
  159. static void __init pl061_init_gc(struct pl061_gpio *chip, int irq_base)
  160. {
  161. struct irq_chip_type *ct;
  162. chip->irq_gc = irq_alloc_generic_chip("gpio-pl061", 1, irq_base,
  163. chip->base, handle_simple_irq);
  164. chip->irq_gc->private = chip;
  165. ct = chip->irq_gc->chip_types;
  166. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  167. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  168. ct->chip.irq_set_type = pl061_irq_type;
  169. ct->chip.irq_set_wake = irq_gc_set_wake;
  170. ct->regs.mask = GPIOIE;
  171. irq_setup_generic_chip(chip->irq_gc, IRQ_MSK(PL061_GPIO_NR),
  172. IRQ_GC_INIT_NESTED_LOCK, IRQ_NOREQUEST, 0);
  173. }
  174. static int pl061_probe(struct amba_device *adev, const struct amba_id *id)
  175. {
  176. struct device *dev = &adev->dev;
  177. struct pl061_platform_data *pdata = dev->platform_data;
  178. struct pl061_gpio *chip;
  179. int ret, irq, i;
  180. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  181. if (chip == NULL)
  182. return -ENOMEM;
  183. if (pdata) {
  184. chip->gc.base = pdata->gpio_base;
  185. chip->irq_base = pdata->irq_base;
  186. } else if (adev->dev.of_node) {
  187. chip->gc.base = -1;
  188. chip->irq_base = 0;
  189. } else
  190. return -ENODEV;
  191. if (!devm_request_mem_region(dev, adev->res.start,
  192. resource_size(&adev->res), "pl061"))
  193. return -EBUSY;
  194. chip->base = devm_ioremap(dev, adev->res.start,
  195. resource_size(&adev->res));
  196. if (chip->base == NULL)
  197. return -ENOMEM;
  198. spin_lock_init(&chip->lock);
  199. chip->gc.direction_input = pl061_direction_input;
  200. chip->gc.direction_output = pl061_direction_output;
  201. chip->gc.get = pl061_get_value;
  202. chip->gc.set = pl061_set_value;
  203. chip->gc.to_irq = pl061_to_irq;
  204. chip->gc.ngpio = PL061_GPIO_NR;
  205. chip->gc.label = dev_name(dev);
  206. chip->gc.dev = dev;
  207. chip->gc.owner = THIS_MODULE;
  208. ret = gpiochip_add(&chip->gc);
  209. if (ret)
  210. return ret;
  211. /*
  212. * irq_chip support
  213. */
  214. if (chip->irq_base <= 0)
  215. return 0;
  216. pl061_init_gc(chip, chip->irq_base);
  217. writeb(0, chip->base + GPIOIE); /* disable irqs */
  218. irq = adev->irq[0];
  219. if (irq < 0)
  220. return -ENODEV;
  221. irq_set_chained_handler(irq, pl061_irq_handler);
  222. irq_set_handler_data(irq, chip);
  223. for (i = 0; i < PL061_GPIO_NR; i++) {
  224. if (pdata) {
  225. if (pdata->directions & (1 << i))
  226. pl061_direction_output(&chip->gc, i,
  227. pdata->values & (1 << i));
  228. else
  229. pl061_direction_input(&chip->gc, i);
  230. }
  231. }
  232. amba_set_drvdata(adev, chip);
  233. return 0;
  234. }
  235. #ifdef CONFIG_PM
  236. static int pl061_suspend(struct device *dev)
  237. {
  238. struct pl061_gpio *chip = dev_get_drvdata(dev);
  239. int offset;
  240. chip->csave_regs.gpio_data = 0;
  241. chip->csave_regs.gpio_dir = readb(chip->base + GPIODIR);
  242. chip->csave_regs.gpio_is = readb(chip->base + GPIOIS);
  243. chip->csave_regs.gpio_ibe = readb(chip->base + GPIOIBE);
  244. chip->csave_regs.gpio_iev = readb(chip->base + GPIOIEV);
  245. chip->csave_regs.gpio_ie = readb(chip->base + GPIOIE);
  246. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  247. if (chip->csave_regs.gpio_dir & (1 << offset))
  248. chip->csave_regs.gpio_data |=
  249. pl061_get_value(&chip->gc, offset) << offset;
  250. }
  251. return 0;
  252. }
  253. static int pl061_resume(struct device *dev)
  254. {
  255. struct pl061_gpio *chip = dev_get_drvdata(dev);
  256. int offset;
  257. for (offset = 0; offset < PL061_GPIO_NR; offset++) {
  258. if (chip->csave_regs.gpio_dir & (1 << offset))
  259. pl061_direction_output(&chip->gc, offset,
  260. chip->csave_regs.gpio_data &
  261. (1 << offset));
  262. else
  263. pl061_direction_input(&chip->gc, offset);
  264. }
  265. writeb(chip->csave_regs.gpio_is, chip->base + GPIOIS);
  266. writeb(chip->csave_regs.gpio_ibe, chip->base + GPIOIBE);
  267. writeb(chip->csave_regs.gpio_iev, chip->base + GPIOIEV);
  268. writeb(chip->csave_regs.gpio_ie, chip->base + GPIOIE);
  269. return 0;
  270. }
  271. static const struct dev_pm_ops pl061_dev_pm_ops = {
  272. .suspend = pl061_suspend,
  273. .resume = pl061_resume,
  274. .freeze = pl061_suspend,
  275. .restore = pl061_resume,
  276. };
  277. #endif
  278. static struct amba_id pl061_ids[] = {
  279. {
  280. .id = 0x00041061,
  281. .mask = 0x000fffff,
  282. },
  283. { 0, 0 },
  284. };
  285. MODULE_DEVICE_TABLE(amba, pl061_ids);
  286. static struct amba_driver pl061_gpio_driver = {
  287. .drv = {
  288. .name = "pl061_gpio",
  289. #ifdef CONFIG_PM
  290. .pm = &pl061_dev_pm_ops,
  291. #endif
  292. },
  293. .id_table = pl061_ids,
  294. .probe = pl061_probe,
  295. };
  296. static int __init pl061_gpio_init(void)
  297. {
  298. return amba_driver_register(&pl061_gpio_driver);
  299. }
  300. module_init(pl061_gpio_init);
  301. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  302. MODULE_DESCRIPTION("PL061 GPIO driver");
  303. MODULE_LICENSE("GPL");