gpio-langwell.c 11 KB

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