gpio-pl061.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/list.h>
  16. #include <linux/io.h>
  17. #include <linux/ioport.h>
  18. #include <linux/irq.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. #define GPIODIR 0x400
  27. #define GPIOIS 0x404
  28. #define GPIOIBE 0x408
  29. #define GPIOIEV 0x40C
  30. #define GPIOIE 0x410
  31. #define GPIORIS 0x414
  32. #define GPIOMIS 0x418
  33. #define GPIOIC 0x41C
  34. #define PL061_GPIO_NR 8
  35. struct pl061_gpio {
  36. /* We use a list of pl061_gpio structs for each trigger IRQ in the main
  37. * interrupts controller of the system. We need this to support systems
  38. * in which more that one PL061s are connected to the same IRQ. The ISR
  39. * interates through this list to find the source of the interrupt.
  40. */
  41. struct list_head list;
  42. /* Each of the two spinlocks protects a different set of hardware
  43. * regiters and data structurs. This decouples the code of the IRQ from
  44. * the GPIO code. This also makes the case of a GPIO routine call from
  45. * the IRQ code simpler.
  46. */
  47. spinlock_t lock; /* GPIO registers */
  48. spinlock_t irq_lock; /* IRQ registers */
  49. void __iomem *base;
  50. unsigned irq_base;
  51. struct gpio_chip gc;
  52. };
  53. static int pl061_direction_input(struct gpio_chip *gc, unsigned offset)
  54. {
  55. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  56. unsigned long flags;
  57. unsigned char gpiodir;
  58. if (offset >= gc->ngpio)
  59. return -EINVAL;
  60. spin_lock_irqsave(&chip->lock, flags);
  61. gpiodir = readb(chip->base + GPIODIR);
  62. gpiodir &= ~(1 << offset);
  63. writeb(gpiodir, chip->base + GPIODIR);
  64. spin_unlock_irqrestore(&chip->lock, flags);
  65. return 0;
  66. }
  67. static int pl061_direction_output(struct gpio_chip *gc, unsigned offset,
  68. int value)
  69. {
  70. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  71. unsigned long flags;
  72. unsigned char gpiodir;
  73. if (offset >= gc->ngpio)
  74. return -EINVAL;
  75. spin_lock_irqsave(&chip->lock, flags);
  76. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  77. gpiodir = readb(chip->base + GPIODIR);
  78. gpiodir |= 1 << offset;
  79. writeb(gpiodir, chip->base + GPIODIR);
  80. /*
  81. * gpio value is set again, because pl061 doesn't allow to set value of
  82. * a gpio pin before configuring it in OUT mode.
  83. */
  84. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  85. spin_unlock_irqrestore(&chip->lock, flags);
  86. return 0;
  87. }
  88. static int pl061_get_value(struct gpio_chip *gc, unsigned offset)
  89. {
  90. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  91. return !!readb(chip->base + (1 << (offset + 2)));
  92. }
  93. static void pl061_set_value(struct gpio_chip *gc, unsigned offset, int value)
  94. {
  95. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  96. writeb(!!value << offset, chip->base + (1 << (offset + 2)));
  97. }
  98. static int pl061_to_irq(struct gpio_chip *gc, unsigned offset)
  99. {
  100. struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
  101. if (chip->irq_base == (unsigned) -1)
  102. return -EINVAL;
  103. return chip->irq_base + offset;
  104. }
  105. /*
  106. * PL061 GPIO IRQ
  107. */
  108. static void pl061_irq_disable(struct irq_data *d)
  109. {
  110. struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
  111. int offset = d->irq - chip->irq_base;
  112. unsigned long flags;
  113. u8 gpioie;
  114. spin_lock_irqsave(&chip->irq_lock, flags);
  115. gpioie = readb(chip->base + GPIOIE);
  116. gpioie &= ~(1 << offset);
  117. writeb(gpioie, chip->base + GPIOIE);
  118. spin_unlock_irqrestore(&chip->irq_lock, flags);
  119. }
  120. static void pl061_irq_enable(struct irq_data *d)
  121. {
  122. struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
  123. int offset = d->irq - chip->irq_base;
  124. unsigned long flags;
  125. u8 gpioie;
  126. spin_lock_irqsave(&chip->irq_lock, flags);
  127. gpioie = readb(chip->base + GPIOIE);
  128. gpioie |= 1 << offset;
  129. writeb(gpioie, chip->base + GPIOIE);
  130. spin_unlock_irqrestore(&chip->irq_lock, flags);
  131. }
  132. static int pl061_irq_type(struct irq_data *d, unsigned trigger)
  133. {
  134. struct pl061_gpio *chip = irq_data_get_irq_chip_data(d);
  135. int offset = d->irq - chip->irq_base;
  136. unsigned long flags;
  137. u8 gpiois, gpioibe, gpioiev;
  138. if (offset < 0 || offset >= PL061_GPIO_NR)
  139. return -EINVAL;
  140. spin_lock_irqsave(&chip->irq_lock, flags);
  141. gpioiev = readb(chip->base + GPIOIEV);
  142. gpiois = readb(chip->base + GPIOIS);
  143. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  144. gpiois |= 1 << offset;
  145. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  146. gpioiev |= 1 << offset;
  147. else
  148. gpioiev &= ~(1 << offset);
  149. } else
  150. gpiois &= ~(1 << offset);
  151. writeb(gpiois, chip->base + GPIOIS);
  152. gpioibe = readb(chip->base + GPIOIBE);
  153. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  154. gpioibe |= 1 << offset;
  155. else {
  156. gpioibe &= ~(1 << offset);
  157. if (trigger & IRQ_TYPE_EDGE_RISING)
  158. gpioiev |= 1 << offset;
  159. else if (trigger & IRQ_TYPE_EDGE_FALLING)
  160. gpioiev &= ~(1 << offset);
  161. }
  162. writeb(gpioibe, chip->base + GPIOIBE);
  163. writeb(gpioiev, chip->base + GPIOIEV);
  164. spin_unlock_irqrestore(&chip->irq_lock, flags);
  165. return 0;
  166. }
  167. static struct irq_chip pl061_irqchip = {
  168. .name = "GPIO",
  169. .irq_enable = pl061_irq_enable,
  170. .irq_disable = pl061_irq_disable,
  171. .irq_set_type = pl061_irq_type,
  172. };
  173. static void pl061_irq_handler(unsigned irq, struct irq_desc *desc)
  174. {
  175. struct list_head *chip_list = irq_get_handler_data(irq);
  176. struct list_head *ptr;
  177. struct pl061_gpio *chip;
  178. desc->irq_data.chip->irq_ack(&desc->irq_data);
  179. list_for_each(ptr, chip_list) {
  180. unsigned long pending;
  181. int offset;
  182. chip = list_entry(ptr, struct pl061_gpio, list);
  183. pending = readb(chip->base + GPIOMIS);
  184. writeb(pending, chip->base + GPIOIC);
  185. if (pending == 0)
  186. continue;
  187. for_each_set_bit(offset, &pending, PL061_GPIO_NR)
  188. generic_handle_irq(pl061_to_irq(&chip->gc, offset));
  189. }
  190. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  191. }
  192. static int pl061_probe(struct amba_device *dev, const struct amba_id *id)
  193. {
  194. struct pl061_platform_data *pdata;
  195. struct pl061_gpio *chip;
  196. struct list_head *chip_list;
  197. int ret, irq, i;
  198. static DECLARE_BITMAP(init_irq, NR_IRQS);
  199. pdata = dev->dev.platform_data;
  200. if (pdata == NULL)
  201. return -ENODEV;
  202. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  203. if (chip == NULL)
  204. return -ENOMEM;
  205. if (!request_mem_region(dev->res.start,
  206. resource_size(&dev->res), "pl061")) {
  207. ret = -EBUSY;
  208. goto free_mem;
  209. }
  210. chip->base = ioremap(dev->res.start, resource_size(&dev->res));
  211. if (chip->base == NULL) {
  212. ret = -ENOMEM;
  213. goto release_region;
  214. }
  215. spin_lock_init(&chip->lock);
  216. spin_lock_init(&chip->irq_lock);
  217. INIT_LIST_HEAD(&chip->list);
  218. chip->gc.direction_input = pl061_direction_input;
  219. chip->gc.direction_output = pl061_direction_output;
  220. chip->gc.get = pl061_get_value;
  221. chip->gc.set = pl061_set_value;
  222. chip->gc.to_irq = pl061_to_irq;
  223. chip->gc.base = pdata->gpio_base;
  224. chip->gc.ngpio = PL061_GPIO_NR;
  225. chip->gc.label = dev_name(&dev->dev);
  226. chip->gc.dev = &dev->dev;
  227. chip->gc.owner = THIS_MODULE;
  228. chip->irq_base = pdata->irq_base;
  229. ret = gpiochip_add(&chip->gc);
  230. if (ret)
  231. goto iounmap;
  232. /*
  233. * irq_chip support
  234. */
  235. if (chip->irq_base == (unsigned) -1)
  236. return 0;
  237. writeb(0, chip->base + GPIOIE); /* disable irqs */
  238. irq = dev->irq[0];
  239. if (irq < 0) {
  240. ret = -ENODEV;
  241. goto iounmap;
  242. }
  243. irq_set_chained_handler(irq, pl061_irq_handler);
  244. if (!test_and_set_bit(irq, init_irq)) { /* list initialized? */
  245. chip_list = kmalloc(sizeof(*chip_list), GFP_KERNEL);
  246. if (chip_list == NULL) {
  247. clear_bit(irq, init_irq);
  248. ret = -ENOMEM;
  249. goto iounmap;
  250. }
  251. INIT_LIST_HEAD(chip_list);
  252. irq_set_handler_data(irq, chip_list);
  253. } else
  254. chip_list = irq_get_handler_data(irq);
  255. list_add(&chip->list, chip_list);
  256. for (i = 0; i < PL061_GPIO_NR; i++) {
  257. if (pdata->directions & (1 << i))
  258. pl061_direction_output(&chip->gc, i,
  259. pdata->values & (1 << i));
  260. else
  261. pl061_direction_input(&chip->gc, i);
  262. irq_set_chip_and_handler(i + chip->irq_base, &pl061_irqchip,
  263. handle_simple_irq);
  264. set_irq_flags(i+chip->irq_base, IRQF_VALID);
  265. irq_set_chip_data(i + chip->irq_base, chip);
  266. }
  267. return 0;
  268. iounmap:
  269. iounmap(chip->base);
  270. release_region:
  271. release_mem_region(dev->res.start, resource_size(&dev->res));
  272. free_mem:
  273. kfree(chip);
  274. return ret;
  275. }
  276. static struct amba_id pl061_ids[] = {
  277. {
  278. .id = 0x00041061,
  279. .mask = 0x000fffff,
  280. },
  281. { 0, 0 },
  282. };
  283. static struct amba_driver pl061_gpio_driver = {
  284. .drv = {
  285. .name = "pl061_gpio",
  286. },
  287. .id_table = pl061_ids,
  288. .probe = pl061_probe,
  289. };
  290. static int __init pl061_gpio_init(void)
  291. {
  292. return amba_driver_register(&pl061_gpio_driver);
  293. }
  294. subsys_initcall(pl061_gpio_init);
  295. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  296. MODULE_DESCRIPTION("PL061 GPIO driver");
  297. MODULE_LICENSE("GPL");