gpio-timberdale.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Timberdale FPGA GPIO driver
  3. * Author: Mocean Laboratories
  4. * Copyright (c) 2009 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Supports:
  20. * Timberdale FPGA GPIO
  21. */
  22. #include <linux/init.h>
  23. #include <linux/gpio.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/irq.h>
  26. #include <linux/io.h>
  27. #include <linux/timb_gpio.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/slab.h>
  30. #define DRIVER_NAME "timb-gpio"
  31. #define TGPIOVAL 0x00
  32. #define TGPIODIR 0x04
  33. #define TGPIO_IER 0x08
  34. #define TGPIO_ISR 0x0c
  35. #define TGPIO_IPR 0x10
  36. #define TGPIO_ICR 0x14
  37. #define TGPIO_FLR 0x18
  38. #define TGPIO_LVR 0x1c
  39. #define TGPIO_VER 0x20
  40. #define TGPIO_BFLR 0x24
  41. struct timbgpio {
  42. void __iomem *membase;
  43. spinlock_t lock; /* mutual exclusion */
  44. struct gpio_chip gpio;
  45. int irq_base;
  46. unsigned long last_ier;
  47. };
  48. static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index,
  49. unsigned offset, bool enabled)
  50. {
  51. struct timbgpio *tgpio = gpiochip_get_data(gpio);
  52. u32 reg;
  53. spin_lock(&tgpio->lock);
  54. reg = ioread32(tgpio->membase + offset);
  55. if (enabled)
  56. reg |= (1 << index);
  57. else
  58. reg &= ~(1 << index);
  59. iowrite32(reg, tgpio->membase + offset);
  60. spin_unlock(&tgpio->lock);
  61. return 0;
  62. }
  63. static int timbgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  64. {
  65. return timbgpio_update_bit(gpio, nr, TGPIODIR, true);
  66. }
  67. static int timbgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
  68. {
  69. struct timbgpio *tgpio = gpiochip_get_data(gpio);
  70. u32 value;
  71. value = ioread32(tgpio->membase + TGPIOVAL);
  72. return (value & (1 << nr)) ? 1 : 0;
  73. }
  74. static int timbgpio_gpio_direction_output(struct gpio_chip *gpio,
  75. unsigned nr, int val)
  76. {
  77. return timbgpio_update_bit(gpio, nr, TGPIODIR, false);
  78. }
  79. static void timbgpio_gpio_set(struct gpio_chip *gpio,
  80. unsigned nr, int val)
  81. {
  82. timbgpio_update_bit(gpio, nr, TGPIOVAL, val != 0);
  83. }
  84. static int timbgpio_to_irq(struct gpio_chip *gpio, unsigned offset)
  85. {
  86. struct timbgpio *tgpio = gpiochip_get_data(gpio);
  87. if (tgpio->irq_base <= 0)
  88. return -EINVAL;
  89. return tgpio->irq_base + offset;
  90. }
  91. /*
  92. * GPIO IRQ
  93. */
  94. static void timbgpio_irq_disable(struct irq_data *d)
  95. {
  96. struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
  97. int offset = d->irq - tgpio->irq_base;
  98. unsigned long flags;
  99. spin_lock_irqsave(&tgpio->lock, flags);
  100. tgpio->last_ier &= ~(1UL << offset);
  101. iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
  102. spin_unlock_irqrestore(&tgpio->lock, flags);
  103. }
  104. static void timbgpio_irq_enable(struct irq_data *d)
  105. {
  106. struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
  107. int offset = d->irq - tgpio->irq_base;
  108. unsigned long flags;
  109. spin_lock_irqsave(&tgpio->lock, flags);
  110. tgpio->last_ier |= 1UL << offset;
  111. iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
  112. spin_unlock_irqrestore(&tgpio->lock, flags);
  113. }
  114. static int timbgpio_irq_type(struct irq_data *d, unsigned trigger)
  115. {
  116. struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
  117. int offset = d->irq - tgpio->irq_base;
  118. unsigned long flags;
  119. u32 lvr, flr, bflr = 0;
  120. u32 ver;
  121. int ret = 0;
  122. if (offset < 0 || offset > tgpio->gpio.ngpio)
  123. return -EINVAL;
  124. ver = ioread32(tgpio->membase + TGPIO_VER);
  125. spin_lock_irqsave(&tgpio->lock, flags);
  126. lvr = ioread32(tgpio->membase + TGPIO_LVR);
  127. flr = ioread32(tgpio->membase + TGPIO_FLR);
  128. if (ver > 2)
  129. bflr = ioread32(tgpio->membase + TGPIO_BFLR);
  130. if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  131. bflr &= ~(1 << offset);
  132. flr &= ~(1 << offset);
  133. if (trigger & IRQ_TYPE_LEVEL_HIGH)
  134. lvr |= 1 << offset;
  135. else
  136. lvr &= ~(1 << offset);
  137. }
  138. if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  139. if (ver < 3) {
  140. ret = -EINVAL;
  141. goto out;
  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(struct irq_desc *desc)
  164. {
  165. struct timbgpio *tgpio = irq_desc_get_handler_data(desc);
  166. struct irq_data *data = irq_desc_get_irq_data(desc);
  167. unsigned long ipr;
  168. int offset;
  169. data->chip->irq_ack(data);
  170. ipr = ioread32(tgpio->membase + TGPIO_IPR);
  171. iowrite32(ipr, tgpio->membase + TGPIO_ICR);
  172. /*
  173. * Some versions of the hardware trash the IER register if more than
  174. * one interrupt is received simultaneously.
  175. */
  176. iowrite32(0, tgpio->membase + TGPIO_IER);
  177. for_each_set_bit(offset, &ipr, tgpio->gpio.ngpio)
  178. generic_handle_irq(timbgpio_to_irq(&tgpio->gpio, offset));
  179. iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
  180. }
  181. static struct irq_chip timbgpio_irqchip = {
  182. .name = "GPIO",
  183. .irq_enable = timbgpio_irq_enable,
  184. .irq_disable = timbgpio_irq_disable,
  185. .irq_set_type = timbgpio_irq_type,
  186. };
  187. static int timbgpio_probe(struct platform_device *pdev)
  188. {
  189. int err, i;
  190. struct device *dev = &pdev->dev;
  191. struct gpio_chip *gc;
  192. struct timbgpio *tgpio;
  193. struct resource *iomem;
  194. struct timbgpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  195. int irq = platform_get_irq(pdev, 0);
  196. if (!pdata || pdata->nr_pins > 32) {
  197. dev_err(dev, "Invalid platform data\n");
  198. return -EINVAL;
  199. }
  200. tgpio = devm_kzalloc(dev, sizeof(struct timbgpio), GFP_KERNEL);
  201. if (!tgpio) {
  202. dev_err(dev, "Memory alloc failed\n");
  203. return -EINVAL;
  204. }
  205. tgpio->irq_base = pdata->irq_base;
  206. spin_lock_init(&tgpio->lock);
  207. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  208. tgpio->membase = devm_ioremap_resource(dev, iomem);
  209. if (IS_ERR(tgpio->membase))
  210. return PTR_ERR(tgpio->membase);
  211. gc = &tgpio->gpio;
  212. gc->label = dev_name(&pdev->dev);
  213. gc->owner = THIS_MODULE;
  214. gc->parent = &pdev->dev;
  215. gc->direction_input = timbgpio_gpio_direction_input;
  216. gc->get = timbgpio_gpio_get;
  217. gc->direction_output = timbgpio_gpio_direction_output;
  218. gc->set = timbgpio_gpio_set;
  219. gc->to_irq = (irq >= 0 && tgpio->irq_base > 0) ? timbgpio_to_irq : NULL;
  220. gc->dbg_show = NULL;
  221. gc->base = pdata->gpio_base;
  222. gc->ngpio = pdata->nr_pins;
  223. gc->can_sleep = false;
  224. err = devm_gpiochip_add_data(&pdev->dev, gc, tgpio);
  225. if (err)
  226. return err;
  227. platform_set_drvdata(pdev, tgpio);
  228. /* make sure to disable interrupts */
  229. iowrite32(0x0, tgpio->membase + TGPIO_IER);
  230. if (irq < 0 || tgpio->irq_base <= 0)
  231. return 0;
  232. for (i = 0; i < pdata->nr_pins; i++) {
  233. irq_set_chip_and_handler(tgpio->irq_base + i,
  234. &timbgpio_irqchip, handle_simple_irq);
  235. irq_set_chip_data(tgpio->irq_base + i, tgpio);
  236. irq_clear_status_flags(tgpio->irq_base + i, IRQ_NOREQUEST | IRQ_NOPROBE);
  237. }
  238. irq_set_chained_handler_and_data(irq, timbgpio_irq, tgpio);
  239. return 0;
  240. }
  241. static struct platform_driver timbgpio_platform_driver = {
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. .suppress_bind_attrs = true,
  245. },
  246. .probe = timbgpio_probe,
  247. };
  248. /*--------------------------------------------------------------------------*/
  249. builtin_platform_driver(timbgpio_platform_driver);