gpio-langwell.c 12 KB

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