gpio-pl061.c 8.9 KB

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