gpio-intel-mid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Intel MID GPIO driver
  3. *
  4. * Copyright (c) 2008-2014 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/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/kernel.h>
  24. #include <linux/delay.h>
  25. #include <linux/stddef.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/init.h>
  28. #include <linux/io.h>
  29. #include <linux/gpio/driver.h>
  30. #include <linux/slab.h>
  31. #include <linux/pm_runtime.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 inline struct intel_mid_gpio *to_intel_gpio_priv(struct gpio_chip *gc)
  69. {
  70. return container_of(gc, struct intel_mid_gpio, chip);
  71. }
  72. static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned offset,
  73. enum GPIO_REG reg_type)
  74. {
  75. struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
  76. unsigned nreg = chip->ngpio / 32;
  77. u8 reg = offset / 32;
  78. return priv->reg_base + reg_type * nreg * 4 + reg * 4;
  79. }
  80. static void __iomem *gpio_reg_2bit(struct gpio_chip *chip, unsigned offset,
  81. enum GPIO_REG reg_type)
  82. {
  83. struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
  84. unsigned nreg = chip->ngpio / 32;
  85. u8 reg = offset / 16;
  86. return priv->reg_base + reg_type * nreg * 4 + reg * 4;
  87. }
  88. static int intel_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 intel_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 intel_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 intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  116. {
  117. struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
  118. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  119. u32 value;
  120. unsigned long flags;
  121. if (priv->pdev)
  122. pm_runtime_get(&priv->pdev->dev);
  123. spin_lock_irqsave(&priv->lock, flags);
  124. value = readl(gpdr);
  125. value &= ~BIT(offset % 32);
  126. writel(value, gpdr);
  127. spin_unlock_irqrestore(&priv->lock, flags);
  128. if (priv->pdev)
  129. pm_runtime_put(&priv->pdev->dev);
  130. return 0;
  131. }
  132. static int intel_gpio_direction_output(struct gpio_chip *chip,
  133. unsigned offset, int value)
  134. {
  135. struct intel_mid_gpio *priv = to_intel_gpio_priv(chip);
  136. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  137. unsigned long flags;
  138. intel_gpio_set(chip, offset, value);
  139. if (priv->pdev)
  140. pm_runtime_get(&priv->pdev->dev);
  141. spin_lock_irqsave(&priv->lock, flags);
  142. value = readl(gpdr);
  143. value |= BIT(offset % 32);
  144. writel(value, gpdr);
  145. spin_unlock_irqrestore(&priv->lock, flags);
  146. if (priv->pdev)
  147. pm_runtime_put(&priv->pdev->dev);
  148. return 0;
  149. }
  150. static int intel_mid_irq_type(struct irq_data *d, unsigned type)
  151. {
  152. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  153. struct intel_mid_gpio *priv = to_intel_gpio_priv(gc);
  154. u32 gpio = irqd_to_hwirq(d);
  155. unsigned long flags;
  156. u32 value;
  157. void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
  158. void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
  159. if (gpio >= priv->chip.ngpio)
  160. return -EINVAL;
  161. if (priv->pdev)
  162. pm_runtime_get(&priv->pdev->dev);
  163. spin_lock_irqsave(&priv->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(&priv->lock, flags);
  175. if (priv->pdev)
  176. pm_runtime_put(&priv->pdev->dev);
  177. return 0;
  178. }
  179. static void intel_mid_irq_unmask(struct irq_data *d)
  180. {
  181. }
  182. static void intel_mid_irq_mask(struct irq_data *d)
  183. {
  184. }
  185. static struct irq_chip intel_mid_irqchip = {
  186. .name = "INTEL_MID-GPIO",
  187. .irq_mask = intel_mid_irq_mask,
  188. .irq_unmask = intel_mid_irq_unmask,
  189. .irq_set_type = intel_mid_irq_type,
  190. };
  191. static const struct intel_mid_gpio_ddata gpio_lincroft = {
  192. .ngpio = 64,
  193. };
  194. static const struct intel_mid_gpio_ddata gpio_penwell_aon = {
  195. .ngpio = 96,
  196. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
  197. };
  198. static const struct intel_mid_gpio_ddata gpio_penwell_core = {
  199. .ngpio = 96,
  200. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
  201. };
  202. static const struct intel_mid_gpio_ddata gpio_cloverview_aon = {
  203. .ngpio = 96,
  204. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE | INTEL_MID_IRQ_TYPE_LEVEL,
  205. };
  206. static const struct intel_mid_gpio_ddata gpio_cloverview_core = {
  207. .ngpio = 96,
  208. .chip_irq_type = INTEL_MID_IRQ_TYPE_EDGE,
  209. };
  210. static const struct pci_device_id intel_gpio_ids[] = {
  211. {
  212. /* Lincroft */
  213. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080f),
  214. .driver_data = (kernel_ulong_t)&gpio_lincroft,
  215. },
  216. {
  217. /* Penwell AON */
  218. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081f),
  219. .driver_data = (kernel_ulong_t)&gpio_penwell_aon,
  220. },
  221. {
  222. /* Penwell Core */
  223. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081a),
  224. .driver_data = (kernel_ulong_t)&gpio_penwell_core,
  225. },
  226. {
  227. /* Cloverview Aon */
  228. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08eb),
  229. .driver_data = (kernel_ulong_t)&gpio_cloverview_aon,
  230. },
  231. {
  232. /* Cloverview Core */
  233. PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x08f7),
  234. .driver_data = (kernel_ulong_t)&gpio_cloverview_core,
  235. },
  236. { 0 }
  237. };
  238. MODULE_DEVICE_TABLE(pci, intel_gpio_ids);
  239. static void intel_mid_irq_handler(struct irq_desc *desc)
  240. {
  241. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  242. struct intel_mid_gpio *priv = to_intel_gpio_priv(gc);
  243. struct irq_data *data = irq_desc_get_irq_data(desc);
  244. struct irq_chip *chip = irq_data_get_irq_chip(data);
  245. u32 base, gpio, mask;
  246. unsigned long pending;
  247. void __iomem *gedr;
  248. /* check GPIO controller to check which pin triggered the interrupt */
  249. for (base = 0; base < priv->chip.ngpio; base += 32) {
  250. gedr = gpio_reg(&priv->chip, base, GEDR);
  251. while ((pending = readl(gedr))) {
  252. gpio = __ffs(pending);
  253. mask = BIT(gpio);
  254. /* Clear before handling so we can't lose an edge */
  255. writel(mask, gedr);
  256. generic_handle_irq(irq_find_mapping(gc->irqdomain,
  257. base + gpio));
  258. }
  259. }
  260. chip->irq_eoi(data);
  261. }
  262. static void intel_mid_irq_init_hw(struct intel_mid_gpio *priv)
  263. {
  264. void __iomem *reg;
  265. unsigned base;
  266. for (base = 0; base < priv->chip.ngpio; base += 32) {
  267. /* Clear the rising-edge detect register */
  268. reg = gpio_reg(&priv->chip, base, GRER);
  269. writel(0, reg);
  270. /* Clear the falling-edge detect register */
  271. reg = gpio_reg(&priv->chip, base, GFER);
  272. writel(0, reg);
  273. /* Clear the edge detect status register */
  274. reg = gpio_reg(&priv->chip, base, GEDR);
  275. writel(~0, reg);
  276. }
  277. }
  278. static int __maybe_unused intel_gpio_runtime_idle(struct device *dev)
  279. {
  280. int err = pm_schedule_suspend(dev, 500);
  281. return err ?: -EBUSY;
  282. }
  283. static const struct dev_pm_ops intel_gpio_pm_ops = {
  284. SET_RUNTIME_PM_OPS(NULL, NULL, intel_gpio_runtime_idle)
  285. };
  286. static int intel_gpio_probe(struct pci_dev *pdev,
  287. const struct pci_device_id *id)
  288. {
  289. void __iomem *base;
  290. struct intel_mid_gpio *priv;
  291. u32 gpio_base;
  292. u32 irq_base;
  293. int retval;
  294. struct intel_mid_gpio_ddata *ddata =
  295. (struct intel_mid_gpio_ddata *)id->driver_data;
  296. retval = pcim_enable_device(pdev);
  297. if (retval)
  298. return retval;
  299. retval = pcim_iomap_regions(pdev, 1 << 0 | 1 << 1, pci_name(pdev));
  300. if (retval) {
  301. dev_err(&pdev->dev, "I/O memory mapping error\n");
  302. return retval;
  303. }
  304. base = pcim_iomap_table(pdev)[1];
  305. irq_base = readl(base);
  306. gpio_base = readl(sizeof(u32) + base);
  307. /* release the IO mapping, since we already get the info from bar1 */
  308. pcim_iounmap_regions(pdev, 1 << 1);
  309. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  310. if (!priv) {
  311. dev_err(&pdev->dev, "can't allocate chip data\n");
  312. return -ENOMEM;
  313. }
  314. priv->reg_base = pcim_iomap_table(pdev)[0];
  315. priv->chip.label = dev_name(&pdev->dev);
  316. priv->chip.dev = &pdev->dev;
  317. priv->chip.request = intel_gpio_request;
  318. priv->chip.direction_input = intel_gpio_direction_input;
  319. priv->chip.direction_output = intel_gpio_direction_output;
  320. priv->chip.get = intel_gpio_get;
  321. priv->chip.set = intel_gpio_set;
  322. priv->chip.base = gpio_base;
  323. priv->chip.ngpio = ddata->ngpio;
  324. priv->chip.can_sleep = false;
  325. priv->pdev = pdev;
  326. spin_lock_init(&priv->lock);
  327. pci_set_drvdata(pdev, priv);
  328. retval = gpiochip_add(&priv->chip);
  329. if (retval) {
  330. dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
  331. return retval;
  332. }
  333. retval = gpiochip_irqchip_add(&priv->chip,
  334. &intel_mid_irqchip,
  335. irq_base,
  336. handle_simple_irq,
  337. IRQ_TYPE_NONE);
  338. if (retval) {
  339. dev_err(&pdev->dev,
  340. "could not connect irqchip to gpiochip\n");
  341. return retval;
  342. }
  343. intel_mid_irq_init_hw(priv);
  344. gpiochip_set_chained_irqchip(&priv->chip,
  345. &intel_mid_irqchip,
  346. pdev->irq,
  347. intel_mid_irq_handler);
  348. pm_runtime_put_noidle(&pdev->dev);
  349. pm_runtime_allow(&pdev->dev);
  350. return 0;
  351. }
  352. static struct pci_driver intel_gpio_driver = {
  353. .name = "intel_mid_gpio",
  354. .id_table = intel_gpio_ids,
  355. .probe = intel_gpio_probe,
  356. .driver = {
  357. .pm = &intel_gpio_pm_ops,
  358. },
  359. };
  360. static int __init intel_gpio_init(void)
  361. {
  362. return pci_register_driver(&intel_gpio_driver);
  363. }
  364. device_initcall(intel_gpio_init);