gpio-langwell.c 11 KB

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