gpio-timberdale.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * 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 &= ~(1UL << 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 |= 1UL << 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. } else {
  142. flr |= 1 << offset;
  143. bflr |= 1 << offset;
  144. }
  145. } else {
  146. bflr &= ~(1 << offset);
  147. flr |= 1 << offset;
  148. if (trigger & IRQ_TYPE_EDGE_FALLING)
  149. lvr &= ~(1 << offset);
  150. else
  151. lvr |= 1 << offset;
  152. }
  153. iowrite32(lvr, tgpio->membase + TGPIO_LVR);
  154. iowrite32(flr, tgpio->membase + TGPIO_FLR);
  155. if (ver > 2)
  156. iowrite32(bflr, tgpio->membase + TGPIO_BFLR);
  157. iowrite32(1 << offset, tgpio->membase + TGPIO_ICR);
  158. out:
  159. spin_unlock_irqrestore(&tgpio->lock, flags);
  160. return ret;
  161. }
  162. static void timbgpio_irq(struct irq_desc *desc)
  163. {
  164. struct timbgpio *tgpio = irq_desc_get_handler_data(desc);
  165. struct irq_data *data = irq_desc_get_irq_data(desc);
  166. unsigned long ipr;
  167. int offset;
  168. data->chip->irq_ack(data);
  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 timbgpio_probe(struct platform_device *pdev)
  187. {
  188. int err, i;
  189. struct device *dev = &pdev->dev;
  190. struct gpio_chip *gc;
  191. struct timbgpio *tgpio;
  192. struct resource *iomem;
  193. struct timbgpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  194. int irq = platform_get_irq(pdev, 0);
  195. if (!pdata || pdata->nr_pins > 32) {
  196. dev_err(dev, "Invalid platform data\n");
  197. return -EINVAL;
  198. }
  199. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  200. if (!iomem) {
  201. dev_err(dev, "Unable to get resource\n");
  202. return -EINVAL;
  203. }
  204. tgpio = devm_kzalloc(dev, sizeof(struct timbgpio), GFP_KERNEL);
  205. if (!tgpio) {
  206. dev_err(dev, "Memory alloc failed\n");
  207. return -EINVAL;
  208. }
  209. tgpio->irq_base = pdata->irq_base;
  210. spin_lock_init(&tgpio->lock);
  211. if (!devm_request_mem_region(dev, iomem->start, resource_size(iomem),
  212. DRIVER_NAME)) {
  213. dev_err(dev, "Region already claimed\n");
  214. return -EBUSY;
  215. }
  216. tgpio->membase = devm_ioremap(dev, iomem->start, resource_size(iomem));
  217. if (!tgpio->membase) {
  218. dev_err(dev, "Cannot ioremap\n");
  219. return -ENOMEM;
  220. }
  221. gc = &tgpio->gpio;
  222. gc->label = dev_name(&pdev->dev);
  223. gc->owner = THIS_MODULE;
  224. gc->dev = &pdev->dev;
  225. gc->direction_input = timbgpio_gpio_direction_input;
  226. gc->get = timbgpio_gpio_get;
  227. gc->direction_output = timbgpio_gpio_direction_output;
  228. gc->set = timbgpio_gpio_set;
  229. gc->to_irq = (irq >= 0 && tgpio->irq_base > 0) ? timbgpio_to_irq : NULL;
  230. gc->dbg_show = NULL;
  231. gc->base = pdata->gpio_base;
  232. gc->ngpio = pdata->nr_pins;
  233. gc->can_sleep = false;
  234. err = gpiochip_add(gc);
  235. if (err)
  236. return err;
  237. platform_set_drvdata(pdev, tgpio);
  238. /* make sure to disable interrupts */
  239. iowrite32(0x0, tgpio->membase + TGPIO_IER);
  240. if (irq < 0 || tgpio->irq_base <= 0)
  241. return 0;
  242. for (i = 0; i < pdata->nr_pins; i++) {
  243. irq_set_chip_and_handler(tgpio->irq_base + i,
  244. &timbgpio_irqchip, handle_simple_irq);
  245. irq_set_chip_data(tgpio->irq_base + i, tgpio);
  246. irq_clear_status_flags(tgpio->irq_base + i, IRQ_NOREQUEST | IRQ_NOPROBE);
  247. }
  248. irq_set_chained_handler_and_data(irq, timbgpio_irq, tgpio);
  249. return 0;
  250. }
  251. static int timbgpio_remove(struct platform_device *pdev)
  252. {
  253. struct timbgpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  254. struct timbgpio *tgpio = platform_get_drvdata(pdev);
  255. int irq = platform_get_irq(pdev, 0);
  256. if (irq >= 0 && tgpio->irq_base > 0) {
  257. int i;
  258. for (i = 0; i < pdata->nr_pins; i++) {
  259. irq_set_chip(tgpio->irq_base + i, NULL);
  260. irq_set_chip_data(tgpio->irq_base + i, NULL);
  261. }
  262. irq_set_handler(irq, NULL);
  263. irq_set_handler_data(irq, NULL);
  264. }
  265. gpiochip_remove(&tgpio->gpio);
  266. return 0;
  267. }
  268. static struct platform_driver timbgpio_platform_driver = {
  269. .driver = {
  270. .name = DRIVER_NAME,
  271. },
  272. .probe = timbgpio_probe,
  273. .remove = timbgpio_remove,
  274. };
  275. /*--------------------------------------------------------------------------*/
  276. module_platform_driver(timbgpio_platform_driver);
  277. MODULE_DESCRIPTION("Timberdale GPIO driver");
  278. MODULE_LICENSE("GPL v2");
  279. MODULE_AUTHOR("Mocean Laboratories");
  280. MODULE_ALIAS("platform:"DRIVER_NAME);