langwell_gpio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. struct lnw_gpio_register {
  31. u32 GPLR[2];
  32. u32 GPDR[2];
  33. u32 GPSR[2];
  34. u32 GPCR[2];
  35. u32 GRER[2];
  36. u32 GFER[2];
  37. u32 GEDR[2];
  38. };
  39. struct lnw_gpio {
  40. struct gpio_chip chip;
  41. struct lnw_gpio_register *reg_base;
  42. spinlock_t lock;
  43. unsigned irq_base;
  44. };
  45. static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
  46. {
  47. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  48. u8 reg = offset / 32;
  49. void __iomem *gplr;
  50. gplr = (void __iomem *)(&lnw->reg_base->GPLR[reg]);
  51. return readl(gplr) & BIT(offset % 32);
  52. }
  53. static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  54. {
  55. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  56. u8 reg = offset / 32;
  57. void __iomem *gpsr, *gpcr;
  58. if (value) {
  59. gpsr = (void __iomem *)(&lnw->reg_base->GPSR[reg]);
  60. writel(BIT(offset % 32), gpsr);
  61. } else {
  62. gpcr = (void __iomem *)(&lnw->reg_base->GPCR[reg]);
  63. writel(BIT(offset % 32), gpcr);
  64. }
  65. }
  66. static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  67. {
  68. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  69. u8 reg = offset / 32;
  70. u32 value;
  71. unsigned long flags;
  72. void __iomem *gpdr;
  73. gpdr = (void __iomem *)(&lnw->reg_base->GPDR[reg]);
  74. spin_lock_irqsave(&lnw->lock, flags);
  75. value = readl(gpdr);
  76. value &= ~BIT(offset % 32);
  77. writel(value, gpdr);
  78. spin_unlock_irqrestore(&lnw->lock, flags);
  79. return 0;
  80. }
  81. static int lnw_gpio_direction_output(struct gpio_chip *chip,
  82. unsigned offset, int value)
  83. {
  84. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  85. u8 reg = offset / 32;
  86. unsigned long flags;
  87. void __iomem *gpdr;
  88. lnw_gpio_set(chip, offset, value);
  89. gpdr = (void __iomem *)(&lnw->reg_base->GPDR[reg]);
  90. spin_lock_irqsave(&lnw->lock, flags);
  91. value = readl(gpdr);
  92. value |= BIT(offset % 32);;
  93. writel(value, gpdr);
  94. spin_unlock_irqrestore(&lnw->lock, flags);
  95. return 0;
  96. }
  97. static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  98. {
  99. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  100. return lnw->irq_base + offset;
  101. }
  102. static int lnw_irq_type(unsigned irq, unsigned type)
  103. {
  104. struct lnw_gpio *lnw = get_irq_chip_data(irq);
  105. u32 gpio = irq - lnw->irq_base;
  106. u8 reg = gpio / 32;
  107. unsigned long flags;
  108. u32 value;
  109. void __iomem *grer = (void __iomem *)(&lnw->reg_base->GRER[reg]);
  110. void __iomem *gfer = (void __iomem *)(&lnw->reg_base->GFER[reg]);
  111. if (gpio >= lnw->chip.ngpio)
  112. return -EINVAL;
  113. spin_lock_irqsave(&lnw->lock, flags);
  114. if (type & IRQ_TYPE_EDGE_RISING)
  115. value = readl(grer) | BIT(gpio % 32);
  116. else
  117. value = readl(grer) & (~BIT(gpio % 32));
  118. writel(value, grer);
  119. if (type & IRQ_TYPE_EDGE_FALLING)
  120. value = readl(gfer) | BIT(gpio % 32);
  121. else
  122. value = readl(gfer) & (~BIT(gpio % 32));
  123. writel(value, gfer);
  124. spin_unlock_irqrestore(&lnw->lock, flags);
  125. return 0;
  126. };
  127. static void lnw_irq_unmask(unsigned irq)
  128. {
  129. };
  130. static void lnw_irq_mask(unsigned irq)
  131. {
  132. };
  133. static struct irq_chip lnw_irqchip = {
  134. .name = "LNW-GPIO",
  135. .mask = lnw_irq_mask,
  136. .unmask = lnw_irq_unmask,
  137. .set_type = lnw_irq_type,
  138. };
  139. static struct pci_device_id lnw_gpio_ids[] = {
  140. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f) },
  141. { 0, }
  142. };
  143. MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
  144. static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
  145. {
  146. struct lnw_gpio *lnw = (struct lnw_gpio *)get_irq_data(irq);
  147. u32 reg, gpio;
  148. void __iomem *gedr;
  149. u32 gedr_v;
  150. /* check GPIO controller to check which pin triggered the interrupt */
  151. for (reg = 0; reg < lnw->chip.ngpio / 32; reg++) {
  152. gedr = (void __iomem *)(&lnw->reg_base->GEDR[reg]);
  153. gedr_v = readl(gedr);
  154. if (!gedr_v)
  155. continue;
  156. for (gpio = reg*32; gpio < reg*32+32; gpio++)
  157. if (gedr_v & BIT(gpio % 32)) {
  158. pr_debug("pin %d triggered\n", gpio);
  159. generic_handle_irq(lnw->irq_base + gpio);
  160. }
  161. /* clear the edge detect status bit */
  162. writel(gedr_v, gedr);
  163. }
  164. desc->chip->eoi(irq);
  165. }
  166. static int __devinit lnw_gpio_probe(struct pci_dev *pdev,
  167. const struct pci_device_id *id)
  168. {
  169. void *base;
  170. int i;
  171. resource_size_t start, len;
  172. struct lnw_gpio *lnw;
  173. u32 irq_base;
  174. u32 gpio_base;
  175. int retval = 0;
  176. retval = pci_enable_device(pdev);
  177. if (retval)
  178. goto done;
  179. retval = pci_request_regions(pdev, "langwell_gpio");
  180. if (retval) {
  181. dev_err(&pdev->dev, "error requesting resources\n");
  182. goto err2;
  183. }
  184. /* get the irq_base from bar1 */
  185. start = pci_resource_start(pdev, 1);
  186. len = pci_resource_len(pdev, 1);
  187. base = ioremap_nocache(start, len);
  188. if (!base) {
  189. dev_err(&pdev->dev, "error mapping bar1\n");
  190. goto err3;
  191. }
  192. irq_base = *(u32 *)base;
  193. gpio_base = *((u32 *)base + 1);
  194. /* release the IO mapping, since we already get the info from bar1 */
  195. iounmap(base);
  196. /* get the register base from bar0 */
  197. start = pci_resource_start(pdev, 0);
  198. len = pci_resource_len(pdev, 0);
  199. base = ioremap_nocache(start, len);
  200. if (!base) {
  201. dev_err(&pdev->dev, "error mapping bar0\n");
  202. retval = -EFAULT;
  203. goto err3;
  204. }
  205. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  206. if (!lnw) {
  207. dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
  208. retval = -ENOMEM;
  209. goto err4;
  210. }
  211. lnw->reg_base = base;
  212. lnw->irq_base = irq_base;
  213. lnw->chip.label = dev_name(&pdev->dev);
  214. lnw->chip.direction_input = lnw_gpio_direction_input;
  215. lnw->chip.direction_output = lnw_gpio_direction_output;
  216. lnw->chip.get = lnw_gpio_get;
  217. lnw->chip.set = lnw_gpio_set;
  218. lnw->chip.to_irq = lnw_gpio_to_irq;
  219. lnw->chip.base = gpio_base;
  220. lnw->chip.ngpio = 64;
  221. lnw->chip.can_sleep = 0;
  222. pci_set_drvdata(pdev, lnw);
  223. retval = gpiochip_add(&lnw->chip);
  224. if (retval) {
  225. dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
  226. goto err5;
  227. }
  228. set_irq_data(pdev->irq, lnw);
  229. set_irq_chained_handler(pdev->irq, lnw_irq_handler);
  230. for (i = 0; i < lnw->chip.ngpio; i++) {
  231. set_irq_chip_and_handler_name(i + lnw->irq_base, &lnw_irqchip,
  232. handle_simple_irq, "demux");
  233. set_irq_chip_data(i + lnw->irq_base, lnw);
  234. }
  235. spin_lock_init(&lnw->lock);
  236. goto done;
  237. err5:
  238. kfree(lnw);
  239. err4:
  240. iounmap(base);
  241. err3:
  242. pci_release_regions(pdev);
  243. err2:
  244. pci_disable_device(pdev);
  245. done:
  246. return retval;
  247. }
  248. static struct pci_driver lnw_gpio_driver = {
  249. .name = "langwell_gpio",
  250. .id_table = lnw_gpio_ids,
  251. .probe = lnw_gpio_probe,
  252. };
  253. static int __init lnw_gpio_init(void)
  254. {
  255. return pci_register_driver(&lnw_gpio_driver);
  256. }
  257. device_initcall(lnw_gpio_init);