gpio-intel-mid.c 11 KB

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