gpio-langwell.c 12 KB

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