gpio-sodaville.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * GPIO interface for Intel Sodaville SoCs.
  3. *
  4. * Copyright (c) 2010, 2011 Intel Corporation
  5. *
  6. * Author: Hans J. Koch <hjk@linutronix.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License 2 as published
  10. * by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/gpio/driver.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 gpio_chip chip;
  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 = devm_irq_alloc_descs(&pdev->dev, -1, 0,
  112. 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 = devm_request_irq(&pdev->dev, pdev->irq,
  119. sdv_gpio_pub_irq_handler, IRQF_SHARED,
  120. "sdv_gpio", sd);
  121. if (ret)
  122. return ret;
  123. /*
  124. * This gpio irq controller latches level irqs. Testing shows that if
  125. * we unmask & ACK the IRQ before the source of the interrupt is gone
  126. * then the interrupt is active again.
  127. */
  128. sd->gc = irq_alloc_generic_chip("sdv-gpio", 1, sd->irq_base,
  129. sd->gpio_pub_base, handle_fasteoi_irq);
  130. if (!sd->gc)
  131. return -ENOMEM;
  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. return -ENODEV;
  148. return 0;
  149. }
  150. static int sdv_gpio_probe(struct pci_dev *pdev,
  151. const struct pci_device_id *pci_id)
  152. {
  153. struct sdv_gpio_chip_data *sd;
  154. unsigned long addr;
  155. const void *prop;
  156. int len;
  157. int ret;
  158. u32 mux_val;
  159. sd = kzalloc(sizeof(struct sdv_gpio_chip_data), GFP_KERNEL);
  160. if (!sd)
  161. return -ENOMEM;
  162. ret = pci_enable_device(pdev);
  163. if (ret) {
  164. dev_err(&pdev->dev, "can't enable device.\n");
  165. goto done;
  166. }
  167. ret = pci_request_region(pdev, GPIO_BAR, DRV_NAME);
  168. if (ret) {
  169. dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
  170. goto disable_pci;
  171. }
  172. addr = pci_resource_start(pdev, GPIO_BAR);
  173. if (!addr) {
  174. ret = -ENODEV;
  175. goto release_reg;
  176. }
  177. sd->gpio_pub_base = ioremap(addr, pci_resource_len(pdev, GPIO_BAR));
  178. prop = of_get_property(pdev->dev.of_node, "intel,muxctl", &len);
  179. if (prop && len == 4) {
  180. mux_val = of_read_number(prop, 1);
  181. writel(mux_val, sd->gpio_pub_base + GPMUXCTL);
  182. }
  183. ret = bgpio_init(&sd->chip, &pdev->dev, 4,
  184. sd->gpio_pub_base + GPINR, sd->gpio_pub_base + GPOUTR,
  185. NULL, sd->gpio_pub_base + GPOER, NULL, 0);
  186. if (ret)
  187. goto unmap;
  188. sd->chip.ngpio = SDV_NUM_PUB_GPIOS;
  189. ret = gpiochip_add_data(&sd->chip, sd);
  190. if (ret < 0) {
  191. dev_err(&pdev->dev, "gpiochip_add() failed.\n");
  192. goto unmap;
  193. }
  194. ret = sdv_register_irqsupport(sd, pdev);
  195. if (ret)
  196. goto unmap;
  197. pci_set_drvdata(pdev, sd);
  198. dev_info(&pdev->dev, "Sodaville GPIO driver registered.\n");
  199. return 0;
  200. unmap:
  201. iounmap(sd->gpio_pub_base);
  202. release_reg:
  203. pci_release_region(pdev, GPIO_BAR);
  204. disable_pci:
  205. pci_disable_device(pdev);
  206. done:
  207. kfree(sd);
  208. return ret;
  209. }
  210. static const struct pci_device_id sdv_gpio_pci_ids[] = {
  211. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_SDV_GPIO) },
  212. { 0, },
  213. };
  214. static struct pci_driver sdv_gpio_driver = {
  215. .driver = {
  216. .suppress_bind_attrs = true,
  217. },
  218. .name = DRV_NAME,
  219. .id_table = sdv_gpio_pci_ids,
  220. .probe = sdv_gpio_probe,
  221. };
  222. builtin_pci_driver(sdv_gpio_driver);