langwell_gpio.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. * Medfield platform Penwell chip.
  20. * Whitney point.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/stddef.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/init.h>
  30. #include <linux/irq.h>
  31. #include <linux/io.h>
  32. #include <linux/gpio.h>
  33. #include <linux/slab.h>
  34. /*
  35. * Langwell chip has 64 pins and thus there are 2 32bit registers to control
  36. * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
  37. * registers to control them, so we only define the order here instead of a
  38. * structure, to get a bit offset for a pin (use GPDR as an example):
  39. *
  40. * nreg = ngpio / 32;
  41. * reg = offset / 32;
  42. * bit = offset % 32;
  43. * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
  44. *
  45. * so the bit of reg_addr is to control pin offset's GPDR feature
  46. */
  47. enum GPIO_REG {
  48. GPLR = 0, /* pin level read-only */
  49. GPDR, /* pin direction */
  50. GPSR, /* pin set */
  51. GPCR, /* pin clear */
  52. GRER, /* rising edge detect */
  53. GFER, /* falling edge detect */
  54. GEDR, /* edge detect result */
  55. };
  56. struct lnw_gpio {
  57. struct gpio_chip chip;
  58. void *reg_base;
  59. spinlock_t lock;
  60. unsigned irq_base;
  61. };
  62. static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
  63. enum GPIO_REG reg_type)
  64. {
  65. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  66. unsigned nreg = chip->ngpio / 32;
  67. u8 reg = offset / 32;
  68. void __iomem *ptr;
  69. ptr = (void __iomem *)(lnw->reg_base + reg_type * nreg * 4 + reg * 4);
  70. return ptr;
  71. }
  72. static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
  73. {
  74. void __iomem *gplr = gpio_reg(chip, offset, GPLR);
  75. return readl(gplr) & BIT(offset % 32);
  76. }
  77. static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  78. {
  79. void __iomem *gpsr, *gpcr;
  80. if (value) {
  81. gpsr = gpio_reg(chip, offset, GPSR);
  82. writel(BIT(offset % 32), gpsr);
  83. } else {
  84. gpcr = gpio_reg(chip, offset, GPCR);
  85. writel(BIT(offset % 32), gpcr);
  86. }
  87. }
  88. static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  89. {
  90. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  91. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  92. u32 value;
  93. unsigned long flags;
  94. spin_lock_irqsave(&lnw->lock, flags);
  95. value = readl(gpdr);
  96. value &= ~BIT(offset % 32);
  97. writel(value, gpdr);
  98. spin_unlock_irqrestore(&lnw->lock, flags);
  99. return 0;
  100. }
  101. static int lnw_gpio_direction_output(struct gpio_chip *chip,
  102. unsigned offset, int value)
  103. {
  104. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  105. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  106. unsigned long flags;
  107. lnw_gpio_set(chip, offset, value);
  108. spin_lock_irqsave(&lnw->lock, flags);
  109. value = readl(gpdr);
  110. value |= BIT(offset % 32);;
  111. writel(value, gpdr);
  112. spin_unlock_irqrestore(&lnw->lock, flags);
  113. return 0;
  114. }
  115. static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  116. {
  117. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  118. return lnw->irq_base + offset;
  119. }
  120. static int lnw_irq_type(unsigned irq, unsigned type)
  121. {
  122. struct lnw_gpio *lnw = get_irq_chip_data(irq);
  123. u32 gpio = irq - lnw->irq_base;
  124. unsigned long flags;
  125. u32 value;
  126. void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
  127. void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
  128. if (gpio >= lnw->chip.ngpio)
  129. return -EINVAL;
  130. spin_lock_irqsave(&lnw->lock, flags);
  131. if (type & IRQ_TYPE_EDGE_RISING)
  132. value = readl(grer) | BIT(gpio % 32);
  133. else
  134. value = readl(grer) & (~BIT(gpio % 32));
  135. writel(value, grer);
  136. if (type & IRQ_TYPE_EDGE_FALLING)
  137. value = readl(gfer) | BIT(gpio % 32);
  138. else
  139. value = readl(gfer) & (~BIT(gpio % 32));
  140. writel(value, gfer);
  141. spin_unlock_irqrestore(&lnw->lock, flags);
  142. return 0;
  143. }
  144. static void lnw_irq_unmask(unsigned irq)
  145. {
  146. }
  147. static void lnw_irq_mask(unsigned irq)
  148. {
  149. }
  150. static struct irq_chip lnw_irqchip = {
  151. .name = "LNW-GPIO",
  152. .mask = lnw_irq_mask,
  153. .unmask = lnw_irq_unmask,
  154. .set_type = lnw_irq_type,
  155. };
  156. static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = { /* pin number */
  157. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f), .driver_data = 64 },
  158. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f), .driver_data = 96 },
  159. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a), .driver_data = 96 },
  160. { 0, }
  161. };
  162. MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
  163. static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
  164. {
  165. struct lnw_gpio *lnw = (struct lnw_gpio *)get_irq_data(irq);
  166. u32 base, gpio;
  167. void __iomem *gedr;
  168. u32 gedr_v;
  169. /* check GPIO controller to check which pin triggered the interrupt */
  170. for (base = 0; base < lnw->chip.ngpio; base += 32) {
  171. gedr = gpio_reg(&lnw->chip, base, GEDR);
  172. gedr_v = readl(gedr);
  173. if (!gedr_v)
  174. continue;
  175. for (gpio = base; gpio < base + 32; gpio++)
  176. if (gedr_v & BIT(gpio % 32)) {
  177. pr_debug("pin %d triggered\n", gpio);
  178. generic_handle_irq(lnw->irq_base + gpio);
  179. }
  180. /* clear the edge detect status bit */
  181. writel(gedr_v, gedr);
  182. }
  183. desc->chip->eoi(irq);
  184. }
  185. static int __devinit lnw_gpio_probe(struct pci_dev *pdev,
  186. const struct pci_device_id *id)
  187. {
  188. void *base;
  189. int i;
  190. resource_size_t start, len;
  191. struct lnw_gpio *lnw;
  192. u32 irq_base;
  193. u32 gpio_base;
  194. int retval = 0;
  195. retval = pci_enable_device(pdev);
  196. if (retval)
  197. goto done;
  198. retval = pci_request_regions(pdev, "langwell_gpio");
  199. if (retval) {
  200. dev_err(&pdev->dev, "error requesting resources\n");
  201. goto err2;
  202. }
  203. /* get the irq_base from bar1 */
  204. start = pci_resource_start(pdev, 1);
  205. len = pci_resource_len(pdev, 1);
  206. base = ioremap_nocache(start, len);
  207. if (!base) {
  208. dev_err(&pdev->dev, "error mapping bar1\n");
  209. goto err3;
  210. }
  211. irq_base = *(u32 *)base;
  212. gpio_base = *((u32 *)base + 1);
  213. /* release the IO mapping, since we already get the info from bar1 */
  214. iounmap(base);
  215. /* get the register base from bar0 */
  216. start = pci_resource_start(pdev, 0);
  217. len = pci_resource_len(pdev, 0);
  218. base = ioremap_nocache(start, len);
  219. if (!base) {
  220. dev_err(&pdev->dev, "error mapping bar0\n");
  221. retval = -EFAULT;
  222. goto err3;
  223. }
  224. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  225. if (!lnw) {
  226. dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
  227. retval = -ENOMEM;
  228. goto err4;
  229. }
  230. lnw->reg_base = base;
  231. lnw->irq_base = irq_base;
  232. lnw->chip.label = dev_name(&pdev->dev);
  233. lnw->chip.direction_input = lnw_gpio_direction_input;
  234. lnw->chip.direction_output = lnw_gpio_direction_output;
  235. lnw->chip.get = lnw_gpio_get;
  236. lnw->chip.set = lnw_gpio_set;
  237. lnw->chip.to_irq = lnw_gpio_to_irq;
  238. lnw->chip.base = gpio_base;
  239. lnw->chip.ngpio = id->driver_data;
  240. lnw->chip.can_sleep = 0;
  241. pci_set_drvdata(pdev, lnw);
  242. retval = gpiochip_add(&lnw->chip);
  243. if (retval) {
  244. dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
  245. goto err5;
  246. }
  247. set_irq_data(pdev->irq, lnw);
  248. set_irq_chained_handler(pdev->irq, lnw_irq_handler);
  249. for (i = 0; i < lnw->chip.ngpio; i++) {
  250. set_irq_chip_and_handler_name(i + lnw->irq_base, &lnw_irqchip,
  251. handle_simple_irq, "demux");
  252. set_irq_chip_data(i + lnw->irq_base, lnw);
  253. }
  254. spin_lock_init(&lnw->lock);
  255. goto done;
  256. err5:
  257. kfree(lnw);
  258. err4:
  259. iounmap(base);
  260. err3:
  261. pci_release_regions(pdev);
  262. err2:
  263. pci_disable_device(pdev);
  264. done:
  265. return retval;
  266. }
  267. static struct pci_driver lnw_gpio_driver = {
  268. .name = "langwell_gpio",
  269. .id_table = lnw_gpio_ids,
  270. .probe = lnw_gpio_probe,
  271. };
  272. static int __devinit wp_gpio_probe(struct platform_device *pdev)
  273. {
  274. struct lnw_gpio *lnw;
  275. struct gpio_chip *gc;
  276. struct resource *rc;
  277. int retval = 0;
  278. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  279. if (!rc)
  280. return -EINVAL;
  281. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  282. if (!lnw) {
  283. dev_err(&pdev->dev,
  284. "can't allocate whitneypoint_gpio chip data\n");
  285. return -ENOMEM;
  286. }
  287. lnw->reg_base = ioremap_nocache(rc->start, resource_size(rc));
  288. if (lnw->reg_base == NULL) {
  289. retval = -EINVAL;
  290. goto err_kmalloc;
  291. }
  292. spin_lock_init(&lnw->lock);
  293. gc = &lnw->chip;
  294. gc->label = dev_name(&pdev->dev);
  295. gc->owner = THIS_MODULE;
  296. gc->direction_input = lnw_gpio_direction_input;
  297. gc->direction_output = lnw_gpio_direction_output;
  298. gc->get = lnw_gpio_get;
  299. gc->set = lnw_gpio_set;
  300. gc->to_irq = NULL;
  301. gc->base = 0;
  302. gc->ngpio = 64;
  303. gc->can_sleep = 0;
  304. retval = gpiochip_add(gc);
  305. if (retval) {
  306. dev_err(&pdev->dev, "whitneypoint gpiochip_add error %d\n",
  307. retval);
  308. goto err_ioremap;
  309. }
  310. platform_set_drvdata(pdev, lnw);
  311. return 0;
  312. err_ioremap:
  313. iounmap(lnw->reg_base);
  314. err_kmalloc:
  315. kfree(lnw);
  316. return retval;
  317. }
  318. static int __devexit wp_gpio_remove(struct platform_device *pdev)
  319. {
  320. struct lnw_gpio *lnw = platform_get_drvdata(pdev);
  321. int err;
  322. err = gpiochip_remove(&lnw->chip);
  323. if (err)
  324. dev_err(&pdev->dev, "failed to remove gpio_chip.\n");
  325. iounmap(lnw->reg_base);
  326. kfree(lnw);
  327. platform_set_drvdata(pdev, NULL);
  328. return 0;
  329. }
  330. static struct platform_driver wp_gpio_driver = {
  331. .probe = wp_gpio_probe,
  332. .remove = __devexit_p(wp_gpio_remove),
  333. .driver = {
  334. .name = "wp_gpio",
  335. .owner = THIS_MODULE,
  336. },
  337. };
  338. static int __init lnw_gpio_init(void)
  339. {
  340. int ret;
  341. ret = pci_register_driver(&lnw_gpio_driver);
  342. if (ret < 0)
  343. return ret;
  344. ret = platform_driver_register(&wp_gpio_driver);
  345. if (ret < 0)
  346. pci_unregister_driver(&lnw_gpio_driver);
  347. return ret;
  348. }
  349. device_initcall(lnw_gpio_init);