gpio-sodaville.c 6.4 KB

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