gpio-sodaville.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/init.h>
  13. #include <linux/io.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/gpio/driver.h>
  22. #define DRV_NAME "sdv_gpio"
  23. #define SDV_NUM_PUB_GPIOS 12
  24. #define PCI_DEVICE_ID_SDV_GPIO 0x2e67
  25. #define GPIO_BAR 0
  26. #define GPOUTR 0x00
  27. #define GPOER 0x04
  28. #define GPINR 0x08
  29. #define GPSTR 0x0c
  30. #define GPIT1R0 0x10
  31. #define GPIO_INT 0x14
  32. #define GPIT1R1 0x18
  33. #define GPMUXCTL 0x1c
  34. struct sdv_gpio_chip_data {
  35. int irq_base;
  36. void __iomem *gpio_pub_base;
  37. struct irq_domain *id;
  38. struct irq_chip_generic *gc;
  39. struct gpio_chip chip;
  40. };
  41. static int sdv_gpio_pub_set_type(struct irq_data *d, unsigned int type)
  42. {
  43. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  44. struct sdv_gpio_chip_data *sd = gc->private;
  45. void __iomem *type_reg;
  46. u32 reg;
  47. if (d->hwirq < 8)
  48. type_reg = sd->gpio_pub_base + GPIT1R0;
  49. else
  50. type_reg = sd->gpio_pub_base + GPIT1R1;
  51. reg = readl(type_reg);
  52. switch (type) {
  53. case IRQ_TYPE_LEVEL_HIGH:
  54. reg &= ~BIT(4 * (d->hwirq % 8));
  55. break;
  56. case IRQ_TYPE_LEVEL_LOW:
  57. reg |= BIT(4 * (d->hwirq % 8));
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  62. writel(reg, type_reg);
  63. return 0;
  64. }
  65. static irqreturn_t sdv_gpio_pub_irq_handler(int irq, void *data)
  66. {
  67. struct sdv_gpio_chip_data *sd = data;
  68. u32 irq_stat = readl(sd->gpio_pub_base + GPSTR);
  69. irq_stat &= readl(sd->gpio_pub_base + GPIO_INT);
  70. if (!irq_stat)
  71. return IRQ_NONE;
  72. while (irq_stat) {
  73. u32 irq_bit = __fls(irq_stat);
  74. irq_stat &= ~BIT(irq_bit);
  75. generic_handle_irq(irq_find_mapping(sd->id, irq_bit));
  76. }
  77. return IRQ_HANDLED;
  78. }
  79. static int sdv_xlate(struct irq_domain *h, struct device_node *node,
  80. const u32 *intspec, u32 intsize, irq_hw_number_t *out_hwirq,
  81. u32 *out_type)
  82. {
  83. u32 line, type;
  84. if (node != irq_domain_get_of_node(h))
  85. return -EINVAL;
  86. if (intsize < 2)
  87. return -EINVAL;
  88. line = *intspec;
  89. *out_hwirq = line;
  90. intspec++;
  91. type = *intspec;
  92. switch (type) {
  93. case IRQ_TYPE_LEVEL_LOW:
  94. case IRQ_TYPE_LEVEL_HIGH:
  95. *out_type = type;
  96. break;
  97. default:
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. static const struct irq_domain_ops irq_domain_sdv_ops = {
  103. .xlate = sdv_xlate,
  104. };
  105. static int sdv_register_irqsupport(struct sdv_gpio_chip_data *sd,
  106. struct pci_dev *pdev)
  107. {
  108. struct irq_chip_type *ct;
  109. int ret;
  110. sd->irq_base = irq_alloc_descs(-1, 0, SDV_NUM_PUB_GPIOS, -1);
  111. if (sd->irq_base < 0)
  112. return sd->irq_base;
  113. /* mask + ACK all interrupt sources */
  114. writel(0, sd->gpio_pub_base + GPIO_INT);
  115. writel((1 << 11) - 1, sd->gpio_pub_base + GPSTR);
  116. ret = request_irq(pdev->irq, sdv_gpio_pub_irq_handler, IRQF_SHARED,
  117. "sdv_gpio", sd);
  118. if (ret)
  119. goto out_free_desc;
  120. /*
  121. * This gpio irq controller latches level irqs. Testing shows that if
  122. * we unmask & ACK the IRQ before the source of the interrupt is gone
  123. * then the interrupt is active again.
  124. */
  125. sd->gc = irq_alloc_generic_chip("sdv-gpio", 1, sd->irq_base,
  126. sd->gpio_pub_base, handle_fasteoi_irq);
  127. if (!sd->gc) {
  128. ret = -ENOMEM;
  129. goto out_free_irq;
  130. }
  131. sd->gc->private = sd;
  132. ct = sd->gc->chip_types;
  133. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  134. ct->regs.eoi = GPSTR;
  135. ct->regs.mask = GPIO_INT;
  136. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  137. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  138. ct->chip.irq_eoi = irq_gc_eoi;
  139. ct->chip.irq_set_type = sdv_gpio_pub_set_type;
  140. irq_setup_generic_chip(sd->gc, IRQ_MSK(SDV_NUM_PUB_GPIOS),
  141. IRQ_GC_INIT_MASK_CACHE, IRQ_NOREQUEST,
  142. IRQ_LEVEL | IRQ_NOPROBE);
  143. sd->id = irq_domain_add_legacy(pdev->dev.of_node, SDV_NUM_PUB_GPIOS,
  144. sd->irq_base, 0, &irq_domain_sdv_ops, sd);
  145. if (!sd->id) {
  146. ret = -ENODEV;
  147. goto out_free_irq;
  148. }
  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 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. ret = -ENODEV;
  181. goto release_reg;
  182. }
  183. sd->gpio_pub_base = ioremap(addr, pci_resource_len(pdev, GPIO_BAR));
  184. prop = of_get_property(pdev->dev.of_node, "intel,muxctl", &len);
  185. if (prop && len == 4) {
  186. mux_val = of_read_number(prop, 1);
  187. writel(mux_val, sd->gpio_pub_base + GPMUXCTL);
  188. }
  189. ret = bgpio_init(&sd->chip, &pdev->dev, 4,
  190. sd->gpio_pub_base + GPINR, sd->gpio_pub_base + GPOUTR,
  191. NULL, sd->gpio_pub_base + GPOER, NULL, 0);
  192. if (ret)
  193. goto unmap;
  194. sd->chip.ngpio = SDV_NUM_PUB_GPIOS;
  195. ret = gpiochip_add_data(&sd->chip, sd);
  196. if (ret < 0) {
  197. dev_err(&pdev->dev, "gpiochip_add() failed.\n");
  198. goto unmap;
  199. }
  200. ret = sdv_register_irqsupport(sd, pdev);
  201. if (ret)
  202. goto unmap;
  203. pci_set_drvdata(pdev, sd);
  204. dev_info(&pdev->dev, "Sodaville GPIO driver registered.\n");
  205. return 0;
  206. unmap:
  207. iounmap(sd->gpio_pub_base);
  208. release_reg:
  209. pci_release_region(pdev, GPIO_BAR);
  210. disable_pci:
  211. pci_disable_device(pdev);
  212. done:
  213. kfree(sd);
  214. return ret;
  215. }
  216. static void sdv_gpio_remove(struct pci_dev *pdev)
  217. {
  218. struct sdv_gpio_chip_data *sd = pci_get_drvdata(pdev);
  219. free_irq(pdev->irq, sd);
  220. irq_free_descs(sd->irq_base, SDV_NUM_PUB_GPIOS);
  221. gpiochip_remove(&sd->chip);
  222. pci_release_region(pdev, GPIO_BAR);
  223. iounmap(sd->gpio_pub_base);
  224. pci_disable_device(pdev);
  225. kfree(sd);
  226. }
  227. static const struct pci_device_id sdv_gpio_pci_ids[] = {
  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. module_pci_driver(sdv_gpio_driver);
  238. MODULE_AUTHOR("Hans J. Koch <hjk@linutronix.de>");
  239. MODULE_DESCRIPTION("GPIO interface for Intel Sodaville SoCs");
  240. MODULE_LICENSE("GPL v2");