gpio-sodaville.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * GPIO interface for Intel Sodaville SoCs.
  3. *
  4. * Copyright (c) 2010, 2011 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 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/gpio.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/basic_mmio_gpio.h>
  23. #define DRV_NAME "sdv_gpio"
  24. #define SDV_NUM_PUB_GPIOS 12
  25. #define PCI_DEVICE_ID_SDV_GPIO 0x2e67
  26. #define GPIO_BAR 0
  27. #define GPOUTR 0x00
  28. #define GPOER 0x04
  29. #define GPINR 0x08
  30. #define GPSTR 0x0c
  31. #define GPIT1R0 0x10
  32. #define GPIO_INT 0x14
  33. #define GPIT1R1 0x18
  34. #define GPMUXCTL 0x1c
  35. struct sdv_gpio_chip_data {
  36. int irq_base;
  37. void __iomem *gpio_pub_base;
  38. struct irq_domain id;
  39. struct irq_chip_generic *gc;
  40. struct bgpio_chip bgpio;
  41. };
  42. static int sdv_gpio_pub_set_type(struct irq_data *d, unsigned int type)
  43. {
  44. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  45. struct sdv_gpio_chip_data *sd = gc->private;
  46. void __iomem *type_reg;
  47. u32 irq_offs = d->irq - sd->irq_base;
  48. u32 reg;
  49. if (irq_offs < 8)
  50. type_reg = sd->gpio_pub_base + GPIT1R0;
  51. else
  52. type_reg = sd->gpio_pub_base + GPIT1R1;
  53. reg = readl(type_reg);
  54. switch (type) {
  55. case IRQ_TYPE_LEVEL_HIGH:
  56. reg &= ~BIT(4 * (irq_offs % 8));
  57. break;
  58. case IRQ_TYPE_LEVEL_LOW:
  59. reg |= BIT(4 * (irq_offs % 8));
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. writel(reg, type_reg);
  65. return 0;
  66. }
  67. static irqreturn_t sdv_gpio_pub_irq_handler(int irq, void *data)
  68. {
  69. struct sdv_gpio_chip_data *sd = data;
  70. u32 irq_stat = readl(sd->gpio_pub_base + GPSTR);
  71. irq_stat &= readl(sd->gpio_pub_base + GPIO_INT);
  72. if (!irq_stat)
  73. return IRQ_NONE;
  74. while (irq_stat) {
  75. u32 irq_bit = __fls(irq_stat);
  76. irq_stat &= ~BIT(irq_bit);
  77. generic_handle_irq(sd->irq_base + irq_bit);
  78. }
  79. return IRQ_HANDLED;
  80. }
  81. static int sdv_xlate(struct irq_domain *h, struct device_node *node,
  82. const u32 *intspec, u32 intsize, irq_hw_number_t *out_hwirq,
  83. u32 *out_type)
  84. {
  85. u32 line, type;
  86. if (node != h->of_node)
  87. return -EINVAL;
  88. if (intsize < 2)
  89. return -EINVAL;
  90. line = *intspec;
  91. *out_hwirq = line;
  92. intspec++;
  93. type = *intspec;
  94. switch (type) {
  95. case IRQ_TYPE_LEVEL_LOW:
  96. case IRQ_TYPE_LEVEL_HIGH:
  97. *out_type = type;
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. return 0;
  103. }
  104. static struct irq_domain_ops irq_domain_sdv_ops = {
  105. .dt_translate = sdv_xlate,
  106. };
  107. static __devinit int sdv_register_irqsupport(struct sdv_gpio_chip_data *sd,
  108. struct pci_dev *pdev)
  109. {
  110. struct irq_chip_type *ct;
  111. int ret;
  112. sd->irq_base = irq_alloc_descs(-1, 0, SDV_NUM_PUB_GPIOS, -1);
  113. if (sd->irq_base < 0)
  114. return sd->irq_base;
  115. /* mask + ACK all interrupt sources */
  116. writel(0, sd->gpio_pub_base + GPIO_INT);
  117. writel((1 << 11) - 1, sd->gpio_pub_base + GPSTR);
  118. ret = request_irq(pdev->irq, sdv_gpio_pub_irq_handler, IRQF_SHARED,
  119. "sdv_gpio", sd);
  120. if (ret)
  121. goto out_free_desc;
  122. sd->id.irq_base = sd->irq_base;
  123. sd->id.of_node = of_node_get(pdev->dev.of_node);
  124. sd->id.ops = &irq_domain_sdv_ops;
  125. /*
  126. * This gpio irq controller latches level irqs. Testing shows that if
  127. * we unmask & ACK the IRQ before the source of the interrupt is gone
  128. * then the interrupt is active again.
  129. */
  130. sd->gc = irq_alloc_generic_chip("sdv-gpio", 1, sd->irq_base,
  131. sd->gpio_pub_base, handle_fasteoi_irq);
  132. if (!sd->gc) {
  133. ret = -ENOMEM;
  134. goto out_free_irq;
  135. }
  136. sd->gc->private = sd;
  137. ct = sd->gc->chip_types;
  138. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  139. ct->regs.eoi = GPSTR;
  140. ct->regs.mask = GPIO_INT;
  141. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  142. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  143. ct->chip.irq_eoi = irq_gc_eoi;
  144. ct->chip.irq_set_type = sdv_gpio_pub_set_type;
  145. irq_setup_generic_chip(sd->gc, IRQ_MSK(SDV_NUM_PUB_GPIOS),
  146. IRQ_GC_INIT_MASK_CACHE, IRQ_NOREQUEST,
  147. IRQ_LEVEL | IRQ_NOPROBE);
  148. irq_domain_add(&sd->id);
  149. return 0;
  150. out_free_irq:
  151. free_irq(pdev->irq, sd);
  152. out_free_desc:
  153. irq_free_descs(sd->irq_base, SDV_NUM_PUB_GPIOS);
  154. return ret;
  155. }
  156. static int __devinit sdv_gpio_probe(struct pci_dev *pdev,
  157. const struct pci_device_id *pci_id)
  158. {
  159. struct sdv_gpio_chip_data *sd;
  160. unsigned long addr;
  161. const void *prop;
  162. int len;
  163. int ret;
  164. u32 mux_val;
  165. sd = kzalloc(sizeof(struct sdv_gpio_chip_data), GFP_KERNEL);
  166. if (!sd)
  167. return -ENOMEM;
  168. ret = pci_enable_device(pdev);
  169. if (ret) {
  170. dev_err(&pdev->dev, "can't enable device.\n");
  171. goto done;
  172. }
  173. ret = pci_request_region(pdev, GPIO_BAR, DRV_NAME);
  174. if (ret) {
  175. dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
  176. goto disable_pci;
  177. }
  178. addr = pci_resource_start(pdev, GPIO_BAR);
  179. if (!addr)
  180. goto release_reg;
  181. sd->gpio_pub_base = ioremap(addr, pci_resource_len(pdev, GPIO_BAR));
  182. prop = of_get_property(pdev->dev.of_node, "intel,muxctl", &len);
  183. if (prop && len == 4) {
  184. mux_val = of_read_number(prop, 1);
  185. writel(mux_val, sd->gpio_pub_base + GPMUXCTL);
  186. }
  187. ret = bgpio_init(&sd->bgpio, &pdev->dev, 4,
  188. sd->gpio_pub_base + GPINR, sd->gpio_pub_base + GPOUTR,
  189. NULL, sd->gpio_pub_base + GPOER, NULL, false);
  190. if (ret)
  191. goto unmap;
  192. sd->bgpio.gc.ngpio = SDV_NUM_PUB_GPIOS;
  193. ret = gpiochip_add(&sd->bgpio.gc);
  194. if (ret < 0) {
  195. dev_err(&pdev->dev, "gpiochip_add() failed.\n");
  196. goto unmap;
  197. }
  198. ret = sdv_register_irqsupport(sd, pdev);
  199. if (ret)
  200. goto unmap;
  201. pci_set_drvdata(pdev, sd);
  202. dev_info(&pdev->dev, "Sodaville GPIO driver registered.\n");
  203. return 0;
  204. unmap:
  205. iounmap(sd->gpio_pub_base);
  206. release_reg:
  207. pci_release_region(pdev, GPIO_BAR);
  208. disable_pci:
  209. pci_disable_device(pdev);
  210. done:
  211. kfree(sd);
  212. return ret;
  213. }
  214. static void sdv_gpio_remove(struct pci_dev *pdev)
  215. {
  216. struct sdv_gpio_chip_data *sd = pci_get_drvdata(pdev);
  217. irq_domain_del(&sd->id);
  218. free_irq(pdev->irq, sd);
  219. irq_free_descs(sd->irq_base, SDV_NUM_PUB_GPIOS);
  220. if (gpiochip_remove(&sd->bgpio.gc))
  221. dev_err(&pdev->dev, "gpiochip_remove() failed.\n");
  222. pci_release_region(pdev, GPIO_BAR);
  223. iounmap(sd->gpio_pub_base);
  224. pci_disable_device(pdev);
  225. kfree(sd);
  226. }
  227. static struct pci_device_id sdv_gpio_pci_ids[] __devinitdata = {
  228. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_SDV_GPIO) },
  229. { 0, },
  230. };
  231. static struct pci_driver sdv_gpio_driver = {
  232. .name = DRV_NAME,
  233. .id_table = sdv_gpio_pci_ids,
  234. .probe = sdv_gpio_probe,
  235. .remove = sdv_gpio_remove,
  236. };
  237. static int __init sdv_gpio_init(void)
  238. {
  239. return pci_register_driver(&sdv_gpio_driver);
  240. }
  241. module_init(sdv_gpio_init);
  242. static void __exit sdv_gpio_exit(void)
  243. {
  244. pci_unregister_driver(&sdv_gpio_driver);
  245. }
  246. module_exit(sdv_gpio_exit);
  247. MODULE_AUTHOR("Hans J. Koch <hjk@linutronix.de>");
  248. MODULE_DESCRIPTION("GPIO interface for Intel Sodaville SoCs");
  249. MODULE_LICENSE("GPL v2");