timbgpio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * timbgpio.c timberdale FPGA GPIO driver
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Supports:
  19. * Timberdale FPGA GPIO
  20. */
  21. #include <linux/module.h>
  22. #include <linux/gpio.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/irq.h>
  25. #include <linux/io.h>
  26. #include <linux/timb_gpio.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/slab.h>
  29. #define DRIVER_NAME "timb-gpio"
  30. #define TGPIOVAL 0x00
  31. #define TGPIODIR 0x04
  32. #define TGPIO_IER 0x08
  33. #define TGPIO_ISR 0x0c
  34. #define TGPIO_IPR 0x10
  35. #define TGPIO_ICR 0x14
  36. #define TGPIO_FLR 0x18
  37. #define TGPIO_LVR 0x1c
  38. #define TGPIO_VER 0x20
  39. #define TGPIO_BFLR 0x24
  40. struct timbgpio {
  41. void __iomem *membase;
  42. spinlock_t lock; /* mutual exclusion */
  43. struct gpio_chip gpio;
  44. int irq_base;
  45. unsigned long last_ier;
  46. };
  47. static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index,
  48. unsigned offset, bool enabled)
  49. {
  50. struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
  51. u32 reg;
  52. spin_lock(&tgpio->lock);
  53. reg = ioread32(tgpio->membase + offset);
  54. if (enabled)
  55. reg |= (1 << index);
  56. else
  57. reg &= ~(1 << index);
  58. iowrite32(reg, tgpio->membase + offset);
  59. spin_unlock(&tgpio->lock);
  60. return 0;
  61. }
  62. static int timbgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  63. {
  64. return timbgpio_update_bit(gpio, nr, TGPIODIR, true);
  65. }
  66. static int timbgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
  67. {
  68. struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
  69. u32 value;
  70. value = ioread32(tgpio->membase + TGPIOVAL);
  71. return (value & (1 << nr)) ? 1 : 0;
  72. }
  73. static int timbgpio_gpio_direction_output(struct gpio_chip *gpio,
  74. unsigned nr, int val)
  75. {
  76. return timbgpio_update_bit(gpio, nr, TGPIODIR, false);
  77. }
  78. static void timbgpio_gpio_set(struct gpio_chip *gpio,
  79. unsigned nr, int val)
  80. {
  81. timbgpio_update_bit(gpio, nr, TGPIOVAL, val != 0);
  82. }
  83. static int timbgpio_to_irq(struct gpio_chip *gpio, unsigned offset)
  84. {
  85. struct timbgpio *tgpio = container_of(gpio, struct timbgpio, gpio);
  86. if (tgpio->irq_base <= 0)
  87. return -EINVAL;
  88. return tgpio->irq_base + offset;
  89. }
  90. /*
  91. * GPIO IRQ
  92. */
  93. static void timbgpio_irq_disable(struct irq_data *d)
  94. {
  95. struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
  96. int offset = d->irq - tgpio->irq_base;
  97. unsigned long flags;
  98. spin_lock_irqsave(&tgpio->lock, flags);
  99. tgpio->last_ier &= ~(1 << offset);
  100. iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
  101. spin_unlock_irqrestore(&tgpio->lock, flags);
  102. }
  103. static void timbgpio_irq_enable(struct irq_data *d)
  104. {
  105. struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
  106. int offset = d->irq - tgpio->irq_base;
  107. unsigned long flags;
  108. spin_lock_irqsave(&tgpio->lock, flags);
  109. tgpio->last_ier |= 1 << offset;
  110. iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
  111. spin_unlock_irqrestore(&tgpio->lock, flags);
  112. }
  113. static int timbgpio_irq_type(struct irq_data *d, unsigned trigger)
  114. {
  115. struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
  116. int offset = d->irq - tgpio->irq_base;
  117. unsigned long flags;
  118. u32 lvr, flr, bflr = 0;
  119. u32 ver;
  120. int ret = 0;
  121. if (offset < 0 || offset > tgpio->gpio.ngpio)
  122. return -EINVAL;
  123. ver = ioread32(tgpio->membase + TGPIO_VER);
  124. spin_lock_irqsave(&tgpio->lock, flags);
  125. lvr = ioread32(tgpio->membase + TGPIO_LVR);
  126. flr = ioread32(tgpio->membase + TGPIO_FLR);
  127. if (ver > 2)
  128. bflr = ioread32(tgpio->membase + TGPIO_BFLR);
  129. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  130. bflr &= ~(1 << offset);
  131. flr &= ~(1 << offset);
  132. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  133. lvr |= 1 << offset;
  134. else
  135. lvr &= ~(1 << offset);
  136. }
  137. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  138. if (ver < 3) {
  139. ret = -EINVAL;
  140. goto out;
  141. }
  142. else {
  143. flr |= 1 << offset;
  144. bflr |= 1 << offset;
  145. }
  146. } else {
  147. bflr &= ~(1 << offset);
  148. flr |= 1 << offset;
  149. if (trigger & IRQ_TYPE_EDGE_FALLING)
  150. lvr &= ~(1 << offset);
  151. else
  152. lvr |= 1 << offset;
  153. }
  154. iowrite32(lvr, tgpio->membase + TGPIO_LVR);
  155. iowrite32(flr, tgpio->membase + TGPIO_FLR);
  156. if (ver > 2)
  157. iowrite32(bflr, tgpio->membase + TGPIO_BFLR);
  158. iowrite32(1 << offset, tgpio->membase + TGPIO_ICR);
  159. out:
  160. spin_unlock_irqrestore(&tgpio->lock, flags);
  161. return ret;
  162. }
  163. static void timbgpio_irq(unsigned int irq, struct irq_desc *desc)
  164. {
  165. struct timbgpio *tgpio = irq_get_handler_data(irq);
  166. unsigned long ipr;
  167. int offset;
  168. desc->irq_data.chip->irq_ack(irq_get_irq_data(irq));
  169. ipr = ioread32(tgpio->membase + TGPIO_IPR);
  170. iowrite32(ipr, tgpio->membase + TGPIO_ICR);
  171. /*
  172. * Some versions of the hardware trash the IER register if more than
  173. * one interrupt is received simultaneously.
  174. */
  175. iowrite32(0, tgpio->membase + TGPIO_IER);
  176. for_each_set_bit(offset, &ipr, tgpio->gpio.ngpio)
  177. generic_handle_irq(timbgpio_to_irq(&tgpio->gpio, offset));
  178. iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
  179. }
  180. static struct irq_chip timbgpio_irqchip = {
  181. .name = "GPIO",
  182. .irq_enable = timbgpio_irq_enable,
  183. .irq_disable = timbgpio_irq_disable,
  184. .irq_set_type = timbgpio_irq_type,
  185. };
  186. static int __devinit timbgpio_probe(struct platform_device *pdev)
  187. {
  188. int err, i;
  189. struct gpio_chip *gc;
  190. struct timbgpio *tgpio;
  191. struct resource *iomem;
  192. struct timbgpio_platform_data *pdata = pdev->dev.platform_data;
  193. int irq = platform_get_irq(pdev, 0);
  194. if (!pdata || pdata->nr_pins > 32) {
  195. err = -EINVAL;
  196. goto err_mem;
  197. }
  198. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  199. if (!iomem) {
  200. err = -EINVAL;
  201. goto err_mem;
  202. }
  203. tgpio = kzalloc(sizeof(*tgpio), GFP_KERNEL);
  204. if (!tgpio) {
  205. err = -EINVAL;
  206. goto err_mem;
  207. }
  208. tgpio->irq_base = pdata->irq_base;
  209. spin_lock_init(&tgpio->lock);
  210. if (!request_mem_region(iomem->start, resource_size(iomem),
  211. DRIVER_NAME)) {
  212. err = -EBUSY;
  213. goto err_request;
  214. }
  215. tgpio->membase = ioremap(iomem->start, resource_size(iomem));
  216. if (!tgpio->membase) {
  217. err = -ENOMEM;
  218. goto err_ioremap;
  219. }
  220. gc = &tgpio->gpio;
  221. gc->label = dev_name(&pdev->dev);
  222. gc->owner = THIS_MODULE;
  223. gc->dev = &pdev->dev;
  224. gc->direction_input = timbgpio_gpio_direction_input;
  225. gc->get = timbgpio_gpio_get;
  226. gc->direction_output = timbgpio_gpio_direction_output;
  227. gc->set = timbgpio_gpio_set;
  228. gc->to_irq = (irq >= 0 && tgpio->irq_base > 0) ? timbgpio_to_irq : NULL;
  229. gc->dbg_show = NULL;
  230. gc->base = pdata->gpio_base;
  231. gc->ngpio = pdata->nr_pins;
  232. gc->can_sleep = 0;
  233. err = gpiochip_add(gc);
  234. if (err)
  235. goto err_chipadd;
  236. platform_set_drvdata(pdev, tgpio);
  237. /* make sure to disable interrupts */
  238. iowrite32(0x0, tgpio->membase + TGPIO_IER);
  239. if (irq < 0 || tgpio->irq_base <= 0)
  240. return 0;
  241. for (i = 0; i < pdata->nr_pins; i++) {
  242. irq_set_chip_and_handler_name(tgpio->irq_base + i,
  243. &timbgpio_irqchip, handle_simple_irq, "mux");
  244. irq_set_chip_data(tgpio->irq_base + i, tgpio);
  245. #ifdef CONFIG_ARM
  246. set_irq_flags(tgpio->irq_base + i, IRQF_VALID | IRQF_PROBE);
  247. #endif
  248. }
  249. irq_set_handler_data(irq, tgpio);
  250. irq_set_chained_handler(irq, timbgpio_irq);
  251. return 0;
  252. err_chipadd:
  253. iounmap(tgpio->membase);
  254. err_ioremap:
  255. release_mem_region(iomem->start, resource_size(iomem));
  256. err_request:
  257. kfree(tgpio);
  258. err_mem:
  259. printk(KERN_ERR DRIVER_NAME": Failed to register GPIOs: %d\n", err);
  260. return err;
  261. }
  262. static int __devexit timbgpio_remove(struct platform_device *pdev)
  263. {
  264. int err;
  265. struct timbgpio_platform_data *pdata = pdev->dev.platform_data;
  266. struct timbgpio *tgpio = platform_get_drvdata(pdev);
  267. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  268. int irq = platform_get_irq(pdev, 0);
  269. if (irq >= 0 && tgpio->irq_base > 0) {
  270. int i;
  271. for (i = 0; i < pdata->nr_pins; i++) {
  272. irq_set_chip(tgpio->irq_base + i, NULL);
  273. irq_set_chip_data(tgpio->irq_base + i, NULL);
  274. }
  275. irq_set_handler(irq, NULL);
  276. irq_set_handler_data(irq, NULL);
  277. }
  278. err = gpiochip_remove(&tgpio->gpio);
  279. if (err)
  280. printk(KERN_ERR DRIVER_NAME": failed to remove gpio_chip\n");
  281. iounmap(tgpio->membase);
  282. release_mem_region(iomem->start, resource_size(iomem));
  283. kfree(tgpio);
  284. platform_set_drvdata(pdev, NULL);
  285. return 0;
  286. }
  287. static struct platform_driver timbgpio_platform_driver = {
  288. .driver = {
  289. .name = DRIVER_NAME,
  290. .owner = THIS_MODULE,
  291. },
  292. .probe = timbgpio_probe,
  293. .remove = timbgpio_remove,
  294. };
  295. /*--------------------------------------------------------------------------*/
  296. static int __init timbgpio_init(void)
  297. {
  298. return platform_driver_register(&timbgpio_platform_driver);
  299. }
  300. static void __exit timbgpio_exit(void)
  301. {
  302. platform_driver_unregister(&timbgpio_platform_driver);
  303. }
  304. module_init(timbgpio_init);
  305. module_exit(timbgpio_exit);
  306. MODULE_DESCRIPTION("Timberdale GPIO driver");
  307. MODULE_LICENSE("GPL v2");
  308. MODULE_AUTHOR("Mocean Laboratories");
  309. MODULE_ALIAS("platform:"DRIVER_NAME);