gpio-intel-mid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Intel MID GPIO driver
  3. *
  4. * Copyright (c) 2008-2014,2016 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. /* Supports:
  16. * Moorestown platform Langwell chip.
  17. * Medfield platform Penwell chip.
  18. * Clovertrail platform Cloverview chip.
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/gpio/driver.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/slab.h>
  31. #include <linux/stddef.h>
  32. #define INTEL_MID_IRQ_TYPE_EDGE (1 << 0)
  33. #define INTEL_MID_IRQ_TYPE_LEVEL (1 << 1)
  34. /*
  35. * Langwell chip has 64 pins and thus there are 2 32bit registers to control
  36. * each feature, while Penwell chip has 96 pins for each block, and need 3 32bit
  37. * registers to control them, so we only define the order here instead of a
  38. * structure, to get a bit offset for a pin (use GPDR as an example):
  39. *
  40. * nreg = ngpio / 32;
  41. * reg = offset / 32;
  42. * bit = offset % 32;
  43. * reg_addr = reg_base + GPDR * nreg * 4 + reg * 4;
  44. *
  45. * so the bit of reg_addr is to control pin offset's GPDR feature
  46. */
  47. enum GPIO_REG {
  48. GPLR = 0, /* pin level read-only */
  49. GPDR, /* pin direction */
  50. GPSR, /* pin set */
  51. GPCR, /* pin clear */
  52. GRER, /* rising edge detect */
  53. GFER, /* falling edge detect */
  54. GEDR, /* edge detect result */
  55. GAFR, /* alt function */
  56. };
  57. /* intel_mid gpio driver data */
  58. struct intel_mid_gpio_ddata {
  59. u16 ngpio; /* number of gpio pins */
  60. u32 chip_irq_type; /* chip interrupt type */
  61. };
  62. struct intel_mid_gpio {
  63. struct gpio_chip chip;
  64. void __iomem *reg_base;
  65. spinlock_t lock;
  66. struct pci_dev *pdev;
  67. };
  68. static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
  69. enum GPIO_REG reg_type)
  70. {
  71. struct intel_mid_gpio *priv = gpiochip_get_data(chip);
  72. unsigned nreg = chip->ngpio / 32;
  73. u8 reg = offset / 32;
  74. return priv->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 intel_mid_gpio *priv = gpiochip_get_data(chip);
  80. unsigned nreg = chip->ngpio / 32;
  81. u8 reg = offset / 16;
  82. return priv->reg_base + reg_type * nreg * 4 + reg * 4;
  83. }
  84. static int intel_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 intel_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 intel_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 intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  112. {
  113. struct intel_mid_gpio *priv = gpiochip_get_data(chip);
  114. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  115. u32 value;
  116. unsigned long flags;
  117. if (priv->pdev)
  118. pm_runtime_get(&priv->pdev->dev);
  119. spin_lock_irqsave(&priv->lock, flags);
  120. value = readl(gpdr);
  121. value &= ~BIT(offset % 32);
  122. writel(value, gpdr);
  123. spin_unlock_irqrestore(&priv->lock, flags);
  124. if (priv->pdev)
  125. pm_runtime_put(&priv->pdev->dev);
  126. return 0;
  127. }
  128. static int intel_gpio_direction_output(struct gpio_chip *chip,
  129. unsigned offset, int value)
  130. {
  131. struct intel_mid_gpio *priv = gpiochip_get_data(chip);
  132. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  133. unsigned long flags;
  134. intel_gpio_set(chip, offset, value);
  135. if (priv->pdev)
  136. pm_runtime_get(&priv->pdev->dev);
  137. spin_lock_irqsave(&priv->lock, flags);
  138. value = readl(gpdr);
  139. value |= BIT(offset % 32);
  140. writel(value, gpdr);
  141. spin_unlock_irqrestore(&priv->lock, flags);
  142. if (priv->pdev)
  143. pm_runtime_put(&priv->pdev->dev);
  144. return 0;
  145. }
  146. static int intel_mid_irq_type(struct irq_data *d, unsigned type)
  147. {
  148. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  149. struct intel_mid_gpio *priv = gpiochip_get_data(gc);
  150. u32 gpio = irqd_to_hwirq(d);
  151. unsigned long flags;
  152. u32 value;
  153. void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
  154. void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
  155. if (gpio >= priv->chip.ngpio)
  156. return -EINVAL;
  157. if (priv->pdev)
  158. pm_runtime_get(&priv->pdev->dev);
  159. spin_lock_irqsave(&priv->lock, flags);
  160. if (type & IRQ_TYPE_EDGE_RISING)
  161. value = readl(grer) | BIT(gpio % 32);
  162. else
  163. value = readl(grer) & (~BIT(gpio % 32));
  164. writel(value, grer);
  165. if (type & IRQ_TYPE_EDGE_FALLING)
  166. value = readl(gfer) | BIT(gpio % 32);
  167. else
  168. value = readl(gfer) & (~BIT(gpio % 32));
  169. writel(value, gfer);
  170. spin_unlock_irqrestore(&priv->lock, flags);
  171. if (priv->pdev)
  172. pm_runtime_put(&priv->pdev->dev);
  173. return 0;
  174. }
  175. static void intel_mid_irq_unmask(struct irq_data *d)
  176. {
  177. }
  178. static void intel_mid_irq_mask(struct irq_data *d)
  179. {
  180. }
  181. static struct irq_chip intel_mid_irqchip = {
  182. .name = "INTEL_MID-GPIO",
  183. .irq_mask = intel_mid_irq_mask,
  184. .irq_unmask = intel_mid_irq_unmask,
  185. .irq_set_type = intel_mid_irq_type,
  186. };
  187. static const struct intel_mid_gpio_ddata gpio_lincroft = {
  188. .ngpio = 64,
  189. };
  190. static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
  191. .ngpio = 96,
  192. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
  193. };
  194. static const struct intel_mid_gpio_ddata gpio_penwell_core = {
  195. .ngpio = 96,
  196. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
  197. };
  198. static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
  199. .ngpio = 96,
  200. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
  201. };
  202. static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
  203. .ngpio = 96,
  204. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
  205. };
  206. static const struct pci_device_id intel_gpio_ids[] = {
  207. {
  208. /* Lincroft */
  209. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
  210. .driver_data = (kernel_ulong_t)&gpio_lincroft,
  211. },
  212. {
  213. /* Penwell AON */
  214. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
  215. .driver_data = (kernel_ulong_t)&gpio_penwell_aon,
  216. },
  217. {
  218. /* Penwell Core */
  219. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
  220. .driver_data = (kernel_ulong_t)&gpio_penwell_core,
  221. },
  222. {
  223. /* Cloverview Aon */
  224. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
  225. .driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
  226. },
  227. {
  228. /* Cloverview Core */
  229. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
  230. .driver_data = (kernel_ulong_t)&gpio_cloverview_core,
  231. },
  232. { 0 }
  233. };
  234. MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
  235. static void intel_mid_irq_handler(struct irq_desc *desc)
  236. {
  237. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  238. struct intel_mid_gpio *priv = gpiochip_get_data(gc);
  239. struct irq_data *data = irq_desc_get_irq_data(desc);
  240. struct irq_chip *chip = irq_data_get_irq_chip(data);
  241. u32 base, gpio, mask;
  242. unsigned long pending;
  243. void __iomem *gedr;
  244. /* check GPIO controller to check which pin triggered the interrupt */
  245. for (base = 0; base < priv->chip.ngpio; base += 32) {
  246. gedr = gpio_reg(&priv->chip, base, GEDR);
  247. while ((pending = readl(gedr))) {
  248. gpio = __ffs(pending);
  249. mask = BIT(gpio);
  250. /* Clear before handling so we can't lose an edge */
  251. writel(mask, gedr);
  252. generic_handle_irq(irq_find_mapping(gc->irq.domain,
  253. base + gpio));
  254. }
  255. }
  256. chip->irq_eoi(data);
  257. }
  258. static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
  259. {
  260. void __iomem *reg;
  261. unsigned base;
  262. for (base = 0; base < priv->chip.ngpio; base += 32) {
  263. /* Clear the rising-edge detect register */
  264. reg = gpio_reg(&priv->chip, base, GRER);
  265. writel(0, reg);
  266. /* Clear the falling-edge detect register */
  267. reg = gpio_reg(&priv->chip, base, GFER);
  268. writel(0, reg);
  269. /* Clear the edge detect status register */
  270. reg = gpio_reg(&priv->chip, base, GEDR);
  271. writel(~0, reg);
  272. }
  273. }
  274. static int __maybe_unused intel_gpio_runtime_idle(struct device *dev)
  275. {
  276. int err = pm_schedule_suspend(dev, 500);
  277. return err ?: -EBUSY;
  278. }
  279. static const struct dev_pm_ops intel_gpio_pm_ops = {
  280. SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
  281. };
  282. static int intel_gpio_probe(struct pci_dev *pdev,
  283. const struct pci_device_id *id)
  284. {
  285. void __iomem *base;
  286. struct intel_mid_gpio *priv;
  287. u32 gpio_base;
  288. u32 irq_base;
  289. int retval;
  290. struct intel_mid_gpio_ddata *ddata =
  291. (struct intel_mid_gpio_ddata *)id->driver_data;
  292. retval = pcim_enable_device(pdev);
  293. if (retval)
  294. return retval;
  295. retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
  296. if (retval) {
  297. dev_err(&pdev->dev, "I/O memory mapping error\n");
  298. return retval;
  299. }
  300. base = pcim_iomap_table(pdev)[1];
  301. irq_base = readl(base);
  302. gpio_base = readl(sizeof(u32) + base);
  303. /* release the IO mapping, since we already get the info from bar1 */
  304. pcim_iounmap_regions(pdev, 1 << 1);
  305. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  306. if (!priv)
  307. return -ENOMEM;
  308. priv->reg_base = pcim_iomap_table(pdev)[0];
  309. priv->chip.label = dev_name(&pdev->dev);
  310. priv->chip.parent = &pdev->dev;
  311. priv->chip.request = intel_gpio_request;
  312. priv->chip.direction_input = intel_gpio_direction_input;
  313. priv->chip.direction_output = intel_gpio_direction_output;
  314. priv->chip.get = intel_gpio_get;
  315. priv->chip.set = intel_gpio_set;
  316. priv->chip.base = gpio_base;
  317. priv->chip.ngpio = ddata->ngpio;
  318. priv->chip.can_sleep = false;
  319. priv->pdev = pdev;
  320. spin_lock_init(&priv->lock);
  321. pci_set_drvdata(pdev, priv);
  322. retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
  323. if (retval) {
  324. dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
  325. return retval;
  326. }
  327. retval = gpiochip_irqchip_add(&priv->chip,
  328. &intel_mid_irqchip,
  329. irq_base,
  330. handle_simple_irq,
  331. IRQ_TYPE_NONE);
  332. if (retval) {
  333. dev_err(&pdev->dev,
  334. "could not connect irqchip to gpiochip\n");
  335. return retval;
  336. }
  337. intel_mid_irq_init_hw(priv);
  338. gpiochip_set_chained_irqchip(&priv->chip,
  339. &intel_mid_irqchip,
  340. pdev->irq,
  341. intel_mid_irq_handler);
  342. pm_runtime_put_noidle(&pdev->dev);
  343. pm_runtime_allow(&pdev->dev);
  344. return 0;
  345. }
  346. static struct pci_driver intel_gpio_driver = {
  347. .name = "intel_mid_gpio",
  348. .id_table = intel_gpio_ids,
  349. .probe = intel_gpio_probe,
  350. .driver = {
  351. .pm = &intel_gpio_pm_ops,
  352. },
  353. };
  354. builtin_pci_driver(intel_gpio_driver);