gpio-langwell.c 13 KB

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