langwell_gpio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* langwell_gpio.c Moorestown platform Langwell chip GPIO driver
  2. * Copyright (c) 2008 - 2009, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* Supports:
  18. * Moorestown platform Langwell chip.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/stddef.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <linux/irq.h>
  28. #include <linux/io.h>
  29. #include <linux/gpio.h>
  30. #include <linux/slab.h>
  31. struct lnw_gpio_register {
  32. u32 GPLR[2];
  33. u32 GPDR[2];
  34. u32 GPSR[2];
  35. u32 GPCR[2];
  36. u32 GRER[2];
  37. u32 GFER[2];
  38. u32 GEDR[2];
  39. };
  40. struct lnw_gpio {
  41. struct gpio_chip chip;
  42. struct lnw_gpio_register *reg_base;
  43. spinlock_t lock;
  44. unsigned irq_base;
  45. };
  46. static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
  47. {
  48. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  49. u8 reg = offset / 32;
  50. void __iomem *gplr;
  51. gplr = (void __iomem *)(&lnw->reg_base->GPLR[reg]);
  52. return readl(gplr) & BIT(offset % 32);
  53. }
  54. static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  55. {
  56. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  57. u8 reg = offset / 32;
  58. void __iomem *gpsr, *gpcr;
  59. if (value) {
  60. gpsr = (void __iomem *)(&lnw->reg_base->GPSR[reg]);
  61. writel(BIT(offset % 32), gpsr);
  62. } else {
  63. gpcr = (void __iomem *)(&lnw->reg_base->GPCR[reg]);
  64. writel(BIT(offset % 32), gpcr);
  65. }
  66. }
  67. static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  68. {
  69. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  70. u8 reg = offset / 32;
  71. u32 value;
  72. unsigned long flags;
  73. void __iomem *gpdr;
  74. gpdr = (void __iomem *)(&lnw->reg_base->GPDR[reg]);
  75. spin_lock_irqsave(&lnw->lock, flags);
  76. value = readl(gpdr);
  77. value &= ~BIT(offset % 32);
  78. writel(value, gpdr);
  79. spin_unlock_irqrestore(&lnw->lock, flags);
  80. return 0;
  81. }
  82. static int lnw_gpio_direction_output(struct gpio_chip *chip,
  83. unsigned offset, int value)
  84. {
  85. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  86. u8 reg = offset / 32;
  87. unsigned long flags;
  88. void __iomem *gpdr;
  89. lnw_gpio_set(chip, offset, value);
  90. gpdr = (void __iomem *)(&lnw->reg_base->GPDR[reg]);
  91. spin_lock_irqsave(&lnw->lock, flags);
  92. value = readl(gpdr);
  93. value |= BIT(offset % 32);;
  94. writel(value, gpdr);
  95. spin_unlock_irqrestore(&lnw->lock, flags);
  96. return 0;
  97. }
  98. static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  99. {
  100. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  101. return lnw->irq_base + offset;
  102. }
  103. static int lnw_irq_type(unsigned irq, unsigned type)
  104. {
  105. struct lnw_gpio *lnw = get_irq_chip_data(irq);
  106. u32 gpio = irq - lnw->irq_base;
  107. u8 reg = gpio / 32;
  108. unsigned long flags;
  109. u32 value;
  110. void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]);
  111. void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]);
  112. if (gpio >= lnw->chip.ngpio)
  113. return -EINVAL;
  114. spin_lock_irqsave(&lnw->lock, flags);
  115. if (type & IRQ_TYPE_EDGE_RISING)
  116. value = readl(grer) | BIT(gpio % 32);
  117. else
  118. value = readl(grer) & (~BIT(gpio % 32));
  119. writel(value, grer);
  120. if (type & IRQ_TYPE_EDGE_FALLING)
  121. value = readl(gfer) | BIT(gpio % 32);
  122. else
  123. value = readl(gfer) & (~BIT(gpio % 32));
  124. writel(value, gfer);
  125. spin_unlock_irqrestore(&lnw->lock, flags);
  126. return 0;
  127. };
  128. static void lnw_irq_unmask(unsigned irq)
  129. {
  130. };
  131. static void lnw_irq_mask(unsigned irq)
  132. {
  133. };
  134. static struct irq_chip lnw_irqchip = {
  135. .name = "LNW-GPIO",
  136. .mask = lnw_irq_mask,
  137. .unmask = lnw_irq_unmask,
  138. .set_type = lnw_irq_type,
  139. };
  140. static struct pci_device_id lnw_gpio_ids[] = {
  141. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f) },
  142. { 0, }
  143. };
  144. MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
  145. static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
  146. {
  147. struct lnw_gpio *lnw = (struct lnw_gpio *)get_irq_data(irq);
  148. u32 reg, gpio;
  149. void __iomem *gedr;
  150. u32 gedr_v;
  151. /* check GPIO controller to check which pin triggered the interrupt */
  152. for (reg = 0; reg < lnw->chip.ngpio / 32; reg++) {
  153. gedr = (void __iomem *)(&lnw->reg_base->GEDR[reg]);
  154. gedr_v = readl(gedr);
  155. if (!gedr_v)
  156. continue;
  157. for (gpio = reg*32; gpio < reg*32+32; gpio++)
  158. if (gedr_v & BIT(gpio % 32)) {
  159. pr_debug("pin %d triggered\n", gpio);
  160. generic_handle_irq(lnw->irq_base + gpio);
  161. }
  162. /* clear the edge detect status bit */
  163. writel(gedr_v, gedr);
  164. }
  165. desc->chip->eoi(irq);
  166. }
  167. static int __devinit lnw_gpio_probe(struct pci_dev *pdev,
  168. const struct pci_device_id *id)
  169. {
  170. void *base;
  171. int i;
  172. resource_size_t start, len;
  173. struct lnw_gpio *lnw;
  174. u32 irq_base;
  175. u32 gpio_base;
  176. int retval = 0;
  177. retval = pci_enable_device(pdev);
  178. if (retval)
  179. goto done;
  180. retval = pci_request_regions(pdev, "langwell_gpio");
  181. if (retval) {
  182. dev_err(&pdev->dev, "error requesting resources\n");
  183. goto err2;
  184. }
  185. /* get the irq_base from bar1 */
  186. start = pci_resource_start(pdev, 1);
  187. len = pci_resource_len(pdev, 1);
  188. base = ioremap_nocache(start, len);
  189. if (!base) {
  190. dev_err(&pdev->dev, "error mapping bar1\n");
  191. goto err3;
  192. }
  193. irq_base = *(u32 *)base;
  194. gpio_base = *((u32 *)base + 1);
  195. /* release the IO mapping, since we already get the info from bar1 */
  196. iounmap(base);
  197. /* get the register base from bar0 */
  198. start = pci_resource_start(pdev, 0);
  199. len = pci_resource_len(pdev, 0);
  200. base = ioremap_nocache(start, len);
  201. if (!base) {
  202. dev_err(&pdev->dev, "error mapping bar0\n");
  203. retval = -EFAULT;
  204. goto err3;
  205. }
  206. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  207. if (!lnw) {
  208. dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
  209. retval = -ENOMEM;
  210. goto err4;
  211. }
  212. lnw->reg_base = base;
  213. lnw->irq_base = irq_base;
  214. lnw->chip.label = dev_name(&pdev->dev);
  215. lnw->chip.direction_input = lnw_gpio_direction_input;
  216. lnw->chip.direction_output = lnw_gpio_direction_output;
  217. lnw->chip.get = lnw_gpio_get;
  218. lnw->chip.set = lnw_gpio_set;
  219. lnw->chip.to_irq = lnw_gpio_to_irq;
  220. lnw->chip.base = gpio_base;
  221. lnw->chip.ngpio = 64;
  222. lnw->chip.can_sleep = 0;
  223. pci_set_drvdata(pdev, lnw);
  224. retval = gpiochip_add(&lnw->chip);
  225. if (retval) {
  226. dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
  227. goto err5;
  228. }
  229. set_irq_data(pdev->irq, lnw);
  230. set_irq_chained_handler(pdev->irq, lnw_irq_handler);
  231. for (i = 0; i < lnw->chip.ngpio; i++) {
  232. set_irq_chip_and_handler_name(i + lnw->irq_base, &lnw_irqchip,
  233. handle_simple_irq, "demux");
  234. set_irq_chip_data(i + lnw->irq_base, lnw);
  235. }
  236. spin_lock_init(&lnw->lock);
  237. goto done;
  238. err5:
  239. kfree(lnw);
  240. err4:
  241. iounmap(base);
  242. err3:
  243. pci_release_regions(pdev);
  244. err2:
  245. pci_disable_device(pdev);
  246. done:
  247. return retval;
  248. }
  249. static struct pci_driver lnw_gpio_driver = {
  250. .name = "langwell_gpio",
  251. .id_table = lnw_gpio_ids,
  252. .probe = lnw_gpio_probe,
  253. };
  254. static int __init lnw_gpio_init(void)
  255. {
  256. return pci_register_driver(&lnw_gpio_driver);
  257. }
  258. device_initcall(lnw_gpio_init);