gpio-langwell.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. pending = readl(gedr);
  214. while (pending) {
  215. gpio = __ffs(pending);
  216. mask = BIT(gpio);
  217. pending &= ~mask;
  218. /* Clear before handling so we can't lose an edge */
  219. writel(mask, gedr);
  220. generic_handle_irq(irq_find_mapping(lnw->domain,
  221. base + gpio));
  222. }
  223. }
  224. chip->irq_eoi(data);
  225. }
  226. static void lnw_irq_init_hw(struct lnw_gpio *lnw)
  227. {
  228. void __iomem *reg;
  229. unsigned base;
  230. for (base = 0; base < lnw->chip.ngpio; base += 32) {
  231. /* Clear the rising-edge detect register */
  232. reg = gpio_reg(&lnw->chip, base, GRER);
  233. writel(0, reg);
  234. /* Clear the falling-edge detect register */
  235. reg = gpio_reg(&lnw->chip, base, GFER);
  236. writel(0, reg);
  237. /* Clear the edge detect status register */
  238. reg = gpio_reg(&lnw->chip, base, GEDR);
  239. writel(~0, reg);
  240. }
  241. }
  242. static int lnw_gpio_irq_map(struct irq_domain *d, unsigned int virq,
  243. irq_hw_number_t hw)
  244. {
  245. struct lnw_gpio *lnw = d->host_data;
  246. irq_set_chip_and_handler_name(virq, &lnw_irqchip, handle_simple_irq,
  247. "demux");
  248. irq_set_chip_data(virq, lnw);
  249. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  250. return 0;
  251. }
  252. static const struct irq_domain_ops lnw_gpio_irq_ops = {
  253. .map = lnw_gpio_irq_map,
  254. .xlate = irq_domain_xlate_twocell,
  255. };
  256. #ifdef CONFIG_PM
  257. static int lnw_gpio_runtime_resume(struct device *dev)
  258. {
  259. return 0;
  260. }
  261. static int lnw_gpio_runtime_suspend(struct device *dev)
  262. {
  263. return 0;
  264. }
  265. static int lnw_gpio_runtime_idle(struct device *dev)
  266. {
  267. int err = pm_schedule_suspend(dev, 500);
  268. if (!err)
  269. return 0;
  270. return -EBUSY;
  271. }
  272. #else
  273. #define lnw_gpio_runtime_suspend NULL
  274. #define lnw_gpio_runtime_resume NULL
  275. #define lnw_gpio_runtime_idle NULL
  276. #endif
  277. static const struct dev_pm_ops lnw_gpio_pm_ops = {
  278. .runtime_suspend = lnw_gpio_runtime_suspend,
  279. .runtime_resume = lnw_gpio_runtime_resume,
  280. .runtime_idle = lnw_gpio_runtime_idle,
  281. };
  282. static int __devinit lnw_gpio_probe(struct pci_dev *pdev,
  283. const struct pci_device_id *id)
  284. {
  285. void *base;
  286. resource_size_t start, len;
  287. struct lnw_gpio *lnw;
  288. u32 gpio_base;
  289. int retval = 0;
  290. int ngpio = id->driver_data;
  291. retval = pci_enable_device(pdev);
  292. if (retval)
  293. return retval;
  294. retval = pci_request_regions(pdev, "langwell_gpio");
  295. if (retval) {
  296. dev_err(&pdev->dev, "error requesting resources\n");
  297. goto err2;
  298. }
  299. /* get the gpio_base from bar1 */
  300. start = pci_resource_start(pdev, 1);
  301. len = pci_resource_len(pdev, 1);
  302. base = ioremap_nocache(start, len);
  303. if (!base) {
  304. dev_err(&pdev->dev, "error mapping bar1\n");
  305. goto err3;
  306. }
  307. gpio_base = *((u32 *)base + 1);
  308. /* release the IO mapping, since we already get the info from bar1 */
  309. iounmap(base);
  310. /* get the register base from bar0 */
  311. start = pci_resource_start(pdev, 0);
  312. len = pci_resource_len(pdev, 0);
  313. base = devm_ioremap_nocache(&pdev->dev, start, len);
  314. if (!base) {
  315. dev_err(&pdev->dev, "error mapping bar0\n");
  316. retval = -EFAULT;
  317. goto err3;
  318. }
  319. lnw = devm_kzalloc(&pdev->dev, sizeof(struct lnw_gpio), GFP_KERNEL);
  320. if (!lnw) {
  321. dev_err(&pdev->dev, "can't allocate langwell_gpio chip data\n");
  322. retval = -ENOMEM;
  323. goto err3;
  324. }
  325. lnw->domain = irq_domain_add_linear(pdev->dev.of_node, ngpio,
  326. &lnw_gpio_irq_ops, lnw);
  327. if (!lnw->domain)
  328. goto err3;
  329. lnw->reg_base = base;
  330. lnw->chip.label = dev_name(&pdev->dev);
  331. lnw->chip.request = lnw_gpio_request;
  332. lnw->chip.direction_input = lnw_gpio_direction_input;
  333. lnw->chip.direction_output = lnw_gpio_direction_output;
  334. lnw->chip.get = lnw_gpio_get;
  335. lnw->chip.set = lnw_gpio_set;
  336. lnw->chip.to_irq = lnw_gpio_to_irq;
  337. lnw->chip.base = gpio_base;
  338. lnw->chip.ngpio = ngpio;
  339. lnw->chip.can_sleep = 0;
  340. lnw->pdev = pdev;
  341. pci_set_drvdata(pdev, lnw);
  342. retval = gpiochip_add(&lnw->chip);
  343. if (retval) {
  344. dev_err(&pdev->dev, "langwell gpiochip_add error %d\n", retval);
  345. goto err3;
  346. }
  347. lnw_irq_init_hw(lnw);
  348. irq_set_handler_data(pdev->irq, lnw);
  349. irq_set_chained_handler(pdev->irq, lnw_irq_handler);
  350. spin_lock_init(&lnw->lock);
  351. pm_runtime_put_noidle(&pdev->dev);
  352. pm_runtime_allow(&pdev->dev);
  353. return 0;
  354. err3:
  355. pci_release_regions(pdev);
  356. err2:
  357. pci_disable_device(pdev);
  358. return retval;
  359. }
  360. static struct pci_driver lnw_gpio_driver = {
  361. .name = "langwell_gpio",
  362. .id_table = lnw_gpio_ids,
  363. .probe = lnw_gpio_probe,
  364. .driver = {
  365. .pm = &lnw_gpio_pm_ops,
  366. },
  367. };
  368. static int __devinit wp_gpio_probe(struct platform_device *pdev)
  369. {
  370. struct lnw_gpio *lnw;
  371. struct gpio_chip *gc;
  372. struct resource *rc;
  373. int retval = 0;
  374. rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  375. if (!rc)
  376. return -EINVAL;
  377. lnw = kzalloc(sizeof(struct lnw_gpio), GFP_KERNEL);
  378. if (!lnw) {
  379. dev_err(&pdev->dev,
  380. "can't allocate whitneypoint_gpio chip data\n");
  381. return -ENOMEM;
  382. }
  383. lnw->reg_base = ioremap_nocache(rc->start, resource_size(rc));
  384. if (lnw->reg_base == NULL) {
  385. retval = -EINVAL;
  386. goto err_kmalloc;
  387. }
  388. spin_lock_init(&lnw->lock);
  389. gc = &lnw->chip;
  390. gc->label = dev_name(&pdev->dev);
  391. gc->owner = THIS_MODULE;
  392. gc->direction_input = lnw_gpio_direction_input;
  393. gc->direction_output = lnw_gpio_direction_output;
  394. gc->get = lnw_gpio_get;
  395. gc->set = lnw_gpio_set;
  396. gc->to_irq = NULL;
  397. gc->base = 0;
  398. gc->ngpio = 64;
  399. gc->can_sleep = 0;
  400. retval = gpiochip_add(gc);
  401. if (retval) {
  402. dev_err(&pdev->dev, "whitneypoint gpiochip_add error %d\n",
  403. retval);
  404. goto err_ioremap;
  405. }
  406. platform_set_drvdata(pdev, lnw);
  407. return 0;
  408. err_ioremap:
  409. iounmap(lnw->reg_base);
  410. err_kmalloc:
  411. kfree(lnw);
  412. return retval;
  413. }
  414. static int __devexit wp_gpio_remove(struct platform_device *pdev)
  415. {
  416. struct lnw_gpio *lnw = platform_get_drvdata(pdev);
  417. int err;
  418. err = gpiochip_remove(&lnw->chip);
  419. if (err)
  420. dev_err(&pdev->dev, "failed to remove gpio_chip.\n");
  421. iounmap(lnw->reg_base);
  422. kfree(lnw);
  423. platform_set_drvdata(pdev, NULL);
  424. return 0;
  425. }
  426. static struct platform_driver wp_gpio_driver = {
  427. .probe = wp_gpio_probe,
  428. .remove = __devexit_p(wp_gpio_remove),
  429. .driver = {
  430. .name = "wp_gpio",
  431. .owner = THIS_MODULE,
  432. },
  433. };
  434. static int __init lnw_gpio_init(void)
  435. {
  436. int ret;
  437. ret = pci_register_driver(&lnw_gpio_driver);
  438. if (ret < 0)
  439. return ret;
  440. ret = platform_driver_register(&wp_gpio_driver);
  441. if (ret < 0)
  442. pci_unregister_driver(&lnw_gpio_driver);
  443. return ret;
  444. }
  445. device_initcall(lnw_gpio_init);