langwell_gpio.c 11 KB

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