gpio-langwell.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Moorestown platform Langwell chip GPIO driver
  3. *
  4. * Copyright (c) 2008 - 2009, 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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Supports:
  20. * Moorestown platform Langwell chip.
  21. * Medfield platform Penwell chip.
  22. * Whitney point.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/kernel.h>
  28. #include <linux/delay.h>
  29. #include <linux/stddef.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/init.h>
  32. #include <linux/irq.h>
  33. #include <linux/io.h>
  34. #include <linux/gpio.h>
  35. #include <linux/slab.h>
  36. #include <linux/pm_runtime.h>
  37. /*
  38. * Langwell chip has 64 pins and thus there are 2 32bit registers to control
  39. * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
  40. * registers to control them, so we only define the order here instead of a
  41. * structure, to get a bit offset for a pin (use GPDR as an example):
  42. *
  43. * nreg = ngpio / 32;
  44. * reg = offset / 32;
  45. * bit = offset % 32;
  46. * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
  47. *
  48. * so the bit of reg_addr is to control pin offset's GPDR feature
  49. */
  50. enum GPIO_REG {
  51. GPLR = 0, /* pin level read-only */
  52. GPDR, /* pin direction */
  53. GPSR, /* pin set */
  54. GPCR, /* pin clear */
  55. GRER, /* rising edge detect */
  56. GFER, /* falling edge detect */
  57. GEDR, /* edge detect result */
  58. };
  59. struct lnw_gpio {
  60. struct gpio_chip chip;
  61. void *reg_base;
  62. spinlock_t lock;
  63. unsigned irq_base;
  64. struct pci_dev *pdev;
  65. };
  66. static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
  67. enum GPIO_REG reg_type)
  68. {
  69. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  70. unsigned nreg = chip->ngpio / 32;
  71. u8 reg = offset / 32;
  72. void __iomem *ptr;
  73. ptr = (void __iomem *)(lnw->reg_base + reg_type * nreg * 4 + reg * 4);
  74. return ptr;
  75. }
  76. static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
  77. {
  78. void __iomem *gplr = gpio_reg(chip, offset, GPLR);
  79. return readl(gplr) & BIT(offset % 32);
  80. }
  81. static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  82. {
  83. void __iomem *gpsr, *gpcr;
  84. if (value) {
  85. gpsr = gpio_reg(chip, offset, GPSR);
  86. writel(BIT(offset % 32), gpsr);
  87. } else {
  88. gpcr = gpio_reg(chip, offset, GPCR);
  89. writel(BIT(offset % 32), gpcr);
  90. }
  91. }
  92. static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  93. {
  94. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  95. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  96. u32 value;
  97. unsigned long flags;
  98. if (lnw->pdev)
  99. pm_runtime_get(&lnw->pdev->dev);
  100. spin_lock_irqsave(&lnw->lock, flags);
  101. value = readl(gpdr);
  102. value &= ~BIT(offset % 32);
  103. writel(value, gpdr);
  104. spin_unlock_irqrestore(&lnw->lock, flags);
  105. if (lnw->pdev)
  106. pm_runtime_put(&lnw->pdev->dev);
  107. return 0;
  108. }
  109. static int lnw_gpio_direction_output(struct gpio_chip *chip,
  110. unsigned offset, int value)
  111. {
  112. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  113. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  114. unsigned long flags;
  115. lnw_gpio_set(chip, offset, value);
  116. if (lnw->pdev)
  117. pm_runtime_get(&lnw->pdev->dev);
  118. spin_lock_irqsave(&lnw->lock, flags);
  119. value = readl(gpdr);
  120. value |= BIT(offset % 32);
  121. writel(value, gpdr);
  122. spin_unlock_irqrestore(&lnw->lock, flags);
  123. if (lnw->pdev)
  124. pm_runtime_put(&lnw->pdev->dev);
  125. return 0;
  126. }
  127. static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  128. {
  129. struct lnw_gpio *lnw = container_of(chip, struct lnw_gpio, chip);
  130. return lnw->irq_base + offset;
  131. }
  132. static int lnw_irq_type(struct irq_data *d, unsigned type)
  133. {
  134. struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
  135. u32 gpio = d->irq - lnw->irq_base;
  136. unsigned long flags;
  137. u32 value;
  138. void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
  139. void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
  140. if (gpio >= lnw->chip.ngpio)
  141. return -EINVAL;
  142. if (lnw->pdev)
  143. pm_runtime_get(&lnw->pdev->dev);
  144. spin_lock_irqsave(&lnw->lock, flags);
  145. if (type & IRQ_TYPE_EDGE_RISING)
  146. value = readl(grer) | BIT(gpio % 32);
  147. else
  148. value = readl(grer) & (~BIT(gpio % 32));
  149. writel(value, grer);
  150. if (type & IRQ_TYPE_EDGE_FALLING)
  151. value = readl(gfer) | BIT(gpio % 32);
  152. else
  153. value = readl(gfer) & (~BIT(gpio % 32));
  154. writel(value, gfer);
  155. spin_unlock_irqrestore(&lnw->lock, flags);
  156. if (lnw->pdev)
  157. pm_runtime_put(&lnw->pdev->dev);
  158. return 0;
  159. }
  160. static void lnw_irq_unmask(struct irq_data *d)
  161. {
  162. }
  163. static void lnw_irq_mask(struct irq_data *d)
  164. {
  165. }
  166. static struct irq_chip lnw_irqchip = {
  167. .name = "LNW-GPIO",
  168. .irq_mask = lnw_irq_mask,
  169. .irq_unmask = lnw_irq_unmask,
  170. .irq_set_type = lnw_irq_type,
  171. };
  172. static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = { /* pin number */
  173. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f), .driver_data = 64 },
  174. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f), .driver_data = 96 },
  175. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a), .driver_data = 96 },
  176. { 0, }
  177. };
  178. MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
  179. static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
  180. {
  181. struct irq_data *data = irq_desc_get_irq_data(desc);
  182. struct lnw_gpio *lnw = irq_data_get_irq_handler_data(data);
  183. struct irq_chip *chip = irq_data_get_irq_chip(data);
  184. u32 base, gpio, mask;
  185. unsigned long pending;
  186. void __iomem *gedr;
  187. /* check GPIO controller to check which pin triggered the interrupt */
  188. for (base = 0; base < lnw->chip.ngpio; base += 32) {
  189. gedr = gpio_reg(&lnw->chip, base, GEDR);
  190. pending = readl(gedr);
  191. while (pending) {
  192. gpio = __ffs(pending);
  193. mask = BIT(gpio);
  194. pending &= ~mask;
  195. /* Clear before handling so we can't lose an edge */
  196. writel(mask, gedr);
  197. generic_handle_irq(lnw->irq_base + base + gpio);
  198. }
  199. }
  200. chip->irq_eoi(data);
  201. }
  202. #ifdef CONFIG_PM
  203. static int lnw_gpio_runtime_resume(struct device *dev)
  204. {
  205. return 0;
  206. }
  207. static int lnw_gpio_runtime_suspend(struct device *dev)
  208. {
  209. return 0;
  210. }
  211. static int lnw_gpio_runtime_idle(struct device *dev)
  212. {
  213. int err = pm_schedule_suspend(dev, 500);
  214. if (!err)
  215. return 0;
  216. return -EBUSY;
  217. }
  218. #else
  219. #define lnw_gpio_runtime_suspend NULL
  220. #define lnw_gpio_runtime_resume NULL
  221. #define lnw_gpio_runtime_idle NULL
  222. #endif
  223. static const struct dev_pm_ops lnw_gpio_pm_ops = {
  224. .runtime_suspend = lnw_gpio_runtime_suspend,
  225. .runtime_resume = lnw_gpio_runtime_resume,
  226. .runtime_idle = lnw_gpio_runtime_idle,
  227. };
  228. static int __devinit lnw_gpio_probe(struct pci_dev *pdev,
  229. const struct pci_device_id *id)
  230. {
  231. void *base;
  232. int i;
  233. resource_size_t start, len;
  234. struct lnw_gpio *lnw;
  235. u32 irq_base;
  236. u32 gpio_base;
  237. int retval = 0;
  238. retval = pci_enable_device(pdev);
  239. if (retval)
  240. goto done;
  241. retval = pci_request_regions(pdev, "langwell_gpio");
  242. if (retval) {
  243. dev_err(&pdev->dev, "error requesting resources\n");
  244. goto err2;
  245. }
  246. /* get the irq_base from bar1 */
  247. start = pci_resource_start(pdev, 1);
  248. len = pci_resource_len(pdev, 1);
  249. base = ioremap_nocache(start, len);
  250. if (!base) {
  251. dev_err(&pdev->dev, "error mapping bar1\n");
  252. goto err3;
  253. }
  254. irq_base = *(u32 *)base;
  255. gpio_base = *((u32 *)base + 1);
  256. /* release the IO mapping, since we already get the info from bar1 */
  257. iounmap(base);
  258. /* get the register base from bar0 */
  259. start = pci_resource_start(pdev, 0);
  260. len = pci_resource_len(pdev, 0);
  261. base = ioremap_nocache(start, len);
  262. if (!base) {
  263. dev_err(&pdev->dev, "error mapping bar0\n");
  264. retval = -EFAULT;
  265. goto err3;
  266. }
  267. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  268. if (!lnw) {
  269. dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
  270. retval = -ENOMEM;
  271. goto err4;
  272. }
  273. lnw->reg_base = base;
  274. lnw->irq_base = irq_base;
  275. lnw->chip.label = dev_name(&pdev->dev);
  276. lnw->chip.direction_input = lnw_gpio_direction_input;
  277. lnw->chip.direction_output = lnw_gpio_direction_output;
  278. lnw->chip.get = lnw_gpio_get;
  279. lnw->chip.set = lnw_gpio_set;
  280. lnw->chip.to_irq = lnw_gpio_to_irq;
  281. lnw->chip.base = gpio_base;
  282. lnw->chip.ngpio = id->driver_data;
  283. lnw->chip.can_sleep = 0;
  284. lnw->pdev = pdev;
  285. pci_set_drvdata(pdev, lnw);
  286. retval = gpiochip_add(&lnw->chip);
  287. if (retval) {
  288. dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
  289. goto err5;
  290. }
  291. irq_set_handler_data(pdev->irq, lnw);
  292. irq_set_chained_handler(pdev->irq, lnw_irq_handler);
  293. for (i = 0; i < lnw->chip.ngpio; i++) {
  294. irq_set_chip_and_handler_name(i + lnw->irq_base, &lnw_irqchip,
  295. handle_simple_irq, "demux");
  296. irq_set_chip_data(i + lnw->irq_base, lnw);
  297. }
  298. spin_lock_init(&lnw->lock);
  299. pm_runtime_put_noidle(&pdev->dev);
  300. pm_runtime_allow(&pdev->dev);
  301. goto done;
  302. err5:
  303. kfree(lnw);
  304. err4:
  305. iounmap(base);
  306. err3:
  307. pci_release_regions(pdev);
  308. err2:
  309. pci_disable_device(pdev);
  310. done:
  311. return retval;
  312. }
  313. static struct pci_driver lnw_gpio_driver = {
  314. .name = "langwell_gpio",
  315. .id_table = lnw_gpio_ids,
  316. .probe = lnw_gpio_probe,
  317. .driver = {
  318. .pm = &lnw_gpio_pm_ops,
  319. },
  320. };
  321. static int __devinit wp_gpio_probe(struct platform_device *pdev)
  322. {
  323. struct lnw_gpio *lnw;
  324. struct gpio_chip *gc;
  325. struct resource *rc;
  326. int retval = 0;
  327. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  328. if (!rc)
  329. return -EINVAL;
  330. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  331. if (!lnw) {
  332. dev_err(&pdev->dev,
  333. "can't allocate whitneypoint_gpio chip data\n");
  334. return -ENOMEM;
  335. }
  336. lnw->reg_base = ioremap_nocache(rc->start, resource_size(rc));
  337. if (lnw->reg_base == NULL) {
  338. retval = -EINVAL;
  339. goto err_kmalloc;
  340. }
  341. spin_lock_init(&lnw->lock);
  342. gc = &lnw->chip;
  343. gc->label = dev_name(&pdev->dev);
  344. gc->owner = THIS_MODULE;
  345. gc->direction_input = lnw_gpio_direction_input;
  346. gc->direction_output = lnw_gpio_direction_output;
  347. gc->get = lnw_gpio_get;
  348. gc->set = lnw_gpio_set;
  349. gc->to_irq = NULL;
  350. gc->base = 0;
  351. gc->ngpio = 64;
  352. gc->can_sleep = 0;
  353. retval = gpiochip_add(gc);
  354. if (retval) {
  355. dev_err(&pdev->dev, "whitneypoint gpiochip_add error %d\n",
  356. retval);
  357. goto err_ioremap;
  358. }
  359. platform_set_drvdata(pdev, lnw);
  360. return 0;
  361. err_ioremap:
  362. iounmap(lnw->reg_base);
  363. err_kmalloc:
  364. kfree(lnw);
  365. return retval;
  366. }
  367. static int __devexit wp_gpio_remove(struct platform_device *pdev)
  368. {
  369. struct lnw_gpio *lnw = platform_get_drvdata(pdev);
  370. int err;
  371. err = gpiochip_remove(&lnw->chip);
  372. if (err)
  373. dev_err(&pdev->dev, "failed to remove gpio_chip.\n");
  374. iounmap(lnw->reg_base);
  375. kfree(lnw);
  376. platform_set_drvdata(pdev, NULL);
  377. return 0;
  378. }
  379. static struct platform_driver wp_gpio_driver = {
  380. .probe = wp_gpio_probe,
  381. .remove = __devexit_p(wp_gpio_remove),
  382. .driver = {
  383. .name = "wp_gpio",
  384. .owner = THIS_MODULE,
  385. },
  386. };
  387. static int __init lnw_gpio_init(void)
  388. {
  389. int ret;
  390. ret = pci_register_driver(&lnw_gpio_driver);
  391. if (ret < 0)
  392. return ret;
  393. ret = platform_driver_register(&wp_gpio_driver);
  394. if (ret < 0)
  395. pci_unregister_driver(&lnw_gpio_driver);
  396. return ret;
  397. }
  398. device_initcall(lnw_gpio_init);