gpio-langwell.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. */
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/kernel.h>
  27. #include <linux/delay.h>
  28. #include <linux/stddef.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/init.h>
  31. #include <linux/irq.h>
  32. #include <linux/io.h>
  33. #include <linux/gpio.h>
  34. #include <linux/slab.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/irqdomain.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 __iomem *reg_base;
  63. spinlock_t lock;
  64. struct pci_dev *pdev;
  65. struct irq_domain *domain;
  66. };
  67. #define to_lnw_priv(chip) container_of(chip, struct lnw_gpio, chip)
  68. static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
  69. enum GPIO_REG reg_type)
  70. {
  71. struct lnw_gpio *lnw = to_lnw_priv(chip);
  72. unsigned nreg = chip->ngpio / 32;
  73. u8 reg = offset / 32;
  74. return lnw->reg_base + reg_type * nreg * 4 + reg * 4;
  75. }
  76. static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
  77. enum GPIO_REG reg_type)
  78. {
  79. struct lnw_gpio *lnw = to_lnw_priv(chip);
  80. unsigned nreg = chip->ngpio / 32;
  81. u8 reg = offset / 16;
  82. return lnw->reg_base + reg_type * nreg * 4 + reg * 4;
  83. }
  84. static int lnw_gpio_request(struct gpio_chip *chip, unsigned offset)
  85. {
  86. void __iomem *gafr = gpio_reg_2bit(chip, offset, GAFR);
  87. u32 value = readl(gafr);
  88. int shift = (offset % 16) << 1, af = (value >> shift) & 3;
  89. if (af) {
  90. value &= ~(3 << shift);
  91. writel(value, gafr);
  92. }
  93. return 0;
  94. }
  95. static int lnw_gpio_get(struct gpio_chip *chip, unsigned offset)
  96. {
  97. void __iomem *gplr = gpio_reg(chip, offset, GPLR);
  98. return readl(gplr) & BIT(offset % 32);
  99. }
  100. static void lnw_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  101. {
  102. void __iomem *gpsr, *gpcr;
  103. if (value) {
  104. gpsr = gpio_reg(chip, offset, GPSR);
  105. writel(BIT(offset % 32), gpsr);
  106. } else {
  107. gpcr = gpio_reg(chip, offset, GPCR);
  108. writel(BIT(offset % 32), gpcr);
  109. }
  110. }
  111. static int lnw_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  112. {
  113. struct lnw_gpio *lnw = to_lnw_priv(chip);
  114. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  115. u32 value;
  116. unsigned long flags;
  117. if (lnw->pdev)
  118. pm_runtime_get(&lnw->pdev->dev);
  119. spin_lock_irqsave(&lnw->lock, flags);
  120. value = readl(gpdr);
  121. value &= ~BIT(offset % 32);
  122. writel(value, gpdr);
  123. spin_unlock_irqrestore(&lnw->lock, flags);
  124. if (lnw->pdev)
  125. pm_runtime_put(&lnw->pdev->dev);
  126. return 0;
  127. }
  128. static int lnw_gpio_direction_output(struct gpio_chip *chip,
  129. unsigned offset, int value)
  130. {
  131. struct lnw_gpio *lnw = to_lnw_priv(chip);
  132. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  133. unsigned long flags;
  134. lnw_gpio_set(chip, offset, value);
  135. if (lnw->pdev)
  136. pm_runtime_get(&lnw->pdev->dev);
  137. spin_lock_irqsave(&lnw->lock, flags);
  138. value = readl(gpdr);
  139. value |= BIT(offset % 32);
  140. writel(value, gpdr);
  141. spin_unlock_irqrestore(&lnw->lock, flags);
  142. if (lnw->pdev)
  143. pm_runtime_put(&lnw->pdev->dev);
  144. return 0;
  145. }
  146. static int lnw_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  147. {
  148. struct lnw_gpio *lnw = to_lnw_priv(chip);
  149. return irq_create_mapping(lnw->domain, offset);
  150. }
  151. static int lnw_irq_type(struct irq_data *d, unsigned type)
  152. {
  153. struct lnw_gpio *lnw = irq_data_get_irq_chip_data(d);
  154. u32 gpio = irqd_to_hwirq(d);
  155. unsigned long flags;
  156. u32 value;
  157. void __iomem *grer = gpio_reg(&lnw->chip, gpio, GRER);
  158. void __iomem *gfer = gpio_reg(&lnw->chip, gpio, GFER);
  159. if (gpio >= lnw->chip.ngpio)
  160. return -EINVAL;
  161. if (lnw->pdev)
  162. pm_runtime_get(&lnw->pdev->dev);
  163. spin_lock_irqsave(&lnw->lock, flags);
  164. if (type & IRQ_TYPE_EDGE_RISING)
  165. value = readl(grer) | BIT(gpio % 32);
  166. else
  167. value = readl(grer) & (~BIT(gpio % 32));
  168. writel(value, grer);
  169. if (type & IRQ_TYPE_EDGE_FALLING)
  170. value = readl(gfer) | BIT(gpio % 32);
  171. else
  172. value = readl(gfer) & (~BIT(gpio % 32));
  173. writel(value, gfer);
  174. spin_unlock_irqrestore(&lnw->lock, flags);
  175. if (lnw->pdev)
  176. pm_runtime_put(&lnw->pdev->dev);
  177. return 0;
  178. }
  179. static void lnw_irq_unmask(struct irq_data *d)
  180. {
  181. }
  182. static void lnw_irq_mask(struct irq_data *d)
  183. {
  184. }
  185. static struct irq_chip lnw_irqchip = {
  186. .name = "LNW-GPIO",
  187. .irq_mask = lnw_irq_mask,
  188. .irq_unmask = lnw_irq_unmask,
  189. .irq_set_type = lnw_irq_type,
  190. };
  191. static DEFINE_PCI_DEVICE_TABLE(lnw_gpio_ids) = { /* pin number */
  192. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f), .driver_data = 64 },
  193. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f), .driver_data = 96 },
  194. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a), .driver_data = 96 },
  195. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb), .driver_data = 96 },
  196. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7), .driver_data = 96 },
  197. { 0, }
  198. };
  199. MODULE_DEVICE_TABLE(pci, lnw_gpio_ids);
  200. static void lnw_irq_handler(unsigned irq, struct irq_desc *desc)
  201. {
  202. struct irq_data *data = irq_desc_get_irq_data(desc);
  203. struct lnw_gpio *lnw = irq_data_get_irq_handler_data(data);
  204. struct irq_chip *chip = irq_data_get_irq_chip(data);
  205. u32 base, gpio, mask;
  206. unsigned long pending;
  207. void __iomem *gedr;
  208. /* check GPIO controller to check which pin triggered the interrupt */
  209. for (base = 0; base < lnw->chip.ngpio; base += 32) {
  210. gedr = gpio_reg(&lnw->chip, base, GEDR);
  211. while ((pending = readl(gedr))) {
  212. gpio = __ffs(pending);
  213. mask = BIT(gpio);
  214. /* Clear before handling so we can't lose an edge */
  215. writel(mask, gedr);
  216. generic_handle_irq(irq_find_mapping(lnw->domain,
  217. base + gpio));
  218. }
  219. }
  220. chip->irq_eoi(data);
  221. }
  222. static void lnw_irq_init_hw(struct lnw_gpio *lnw)
  223. {
  224. void __iomem *reg;
  225. unsigned base;
  226. for (base = 0; base < lnw->chip.ngpio; base += 32) {
  227. /* Clear the rising-edge detect register */
  228. reg = gpio_reg(&lnw->chip, base, GRER);
  229. writel(0, reg);
  230. /* Clear the falling-edge detect register */
  231. reg = gpio_reg(&lnw->chip, base, GFER);
  232. writel(0, reg);
  233. /* Clear the edge detect status register */
  234. reg = gpio_reg(&lnw->chip, base, GEDR);
  235. writel(~0, reg);
  236. }
  237. }
  238. static int lnw_gpio_irq_map(struct irq_domain *d, unsigned int virq,
  239. irq_hw_number_t hw)
  240. {
  241. struct lnw_gpio *lnw = d->host_data;
  242. irq_set_chip_and_handler_name(virq, &lnw_irqchip, handle_simple_irq,
  243. "demux");
  244. irq_set_chip_data(virq, lnw);
  245. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  246. return 0;
  247. }
  248. static const struct irq_domain_ops lnw_gpio_irq_ops = {
  249. .map = lnw_gpio_irq_map,
  250. .xlate = irq_domain_xlate_twocell,
  251. };
  252. static int lnw_gpio_runtime_idle(struct device *dev)
  253. {
  254. pm_schedule_suspend(dev, 500);
  255. return -EBUSY;
  256. }
  257. static const struct dev_pm_ops lnw_gpio_pm_ops = {
  258. SET_RUNTIME_PM_OPS(NULL, NULL, lnw_gpio_runtime_idle)
  259. };
  260. static int lnw_gpio_probe(struct pci_dev *pdev,
  261. const struct pci_device_id *id)
  262. {
  263. void __iomem *base;
  264. struct lnw_gpio *lnw;
  265. u32 gpio_base;
  266. u32 irq_base;
  267. int retval;
  268. int ngpio = id->driver_data;
  269. retval = pcim_enable_device(pdev);
  270. if (retval)
  271. return retval;
  272. retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
  273. if (retval) {
  274. dev_err(&pdev->dev, "I/O memory mapping error\n");
  275. return retval;
  276. }
  277. base = pcim_iomap_table(pdev)[1];
  278. irq_base = readl(base);
  279. gpio_base = readl(sizeof(u32) + base);
  280. /* release the IO mapping, since we already get the info from bar1 */
  281. pcim_iounmap_regions(pdev, 1 << 1);
  282. lnw = devm_kzalloc(&pdev->dev, sizeof(*lnw), GFP_KERNEL);
  283. if (!lnw) {
  284. dev_err(&pdev->dev, "can't allocate chip data\n");
  285. return -ENOMEM;
  286. }
  287. lnw->reg_base = pcim_iomap_table(pdev)[0];
  288. lnw->chip.label = dev_name(&pdev->dev);
  289. lnw->chip.request = lnw_gpio_request;
  290. lnw->chip.direction_input = lnw_gpio_direction_input;
  291. lnw->chip.direction_output = lnw_gpio_direction_output;
  292. lnw->chip.get = lnw_gpio_get;
  293. lnw->chip.set = lnw_gpio_set;
  294. lnw->chip.to_irq = lnw_gpio_to_irq;
  295. lnw->chip.base = gpio_base;
  296. lnw->chip.ngpio = ngpio;
  297. lnw->chip.can_sleep = 0;
  298. lnw->pdev = pdev;
  299. spin_lock_init(&lnw->lock);
  300. lnw->domain = irq_domain_add_simple(pdev->dev.of_node, ngpio, irq_base,
  301. &lnw_gpio_irq_ops, lnw);
  302. if (!lnw->domain)
  303. return -ENOMEM;
  304. pci_set_drvdata(pdev, lnw);
  305. retval = gpiochip_add(&lnw->chip);
  306. if (retval) {
  307. dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
  308. return retval;
  309. }
  310. lnw_irq_init_hw(lnw);
  311. irq_set_handler_data(pdev->irq, lnw);
  312. irq_set_chained_handler(pdev->irq, lnw_irq_handler);
  313. pm_runtime_put_noidle(&pdev->dev);
  314. pm_runtime_allow(&pdev->dev);
  315. return 0;
  316. }
  317. static struct pci_driver lnw_gpio_driver = {
  318. .name = "langwell_gpio",
  319. .id_table = lnw_gpio_ids,
  320. .probe = lnw_gpio_probe,
  321. .driver = {
  322. .pm = &lnw_gpio_pm_ops,
  323. },
  324. };
  325. static int __init lnw_gpio_init(void)
  326. {
  327. return pci_register_driver(&lnw_gpio_driver);
  328. }
  329. device_initcall(lnw_gpio_init);