gpio-intel-mid.c 13 KB

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