gpio-merrifield.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Intel Merrifield SoC GPIO driver
  3. *
  4. * Copyright (c) 2016 Intel Corporation.
  5. * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/gpio.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/pinctrl/consumer.h>
  20. #define GCCR 0x000 /* controller configuration */
  21. #define GPLR 0x004 /* pin level r/o */
  22. #define GPDR 0x01c /* pin direction */
  23. #define GPSR 0x034 /* pin set w/o */
  24. #define GPCR 0x04c /* pin clear w/o */
  25. #define GRER 0x064 /* rising edge detect */
  26. #define GFER 0x07c /* falling edge detect */
  27. #define GFBR 0x094 /* glitch filter bypass */
  28. #define GIMR 0x0ac /* interrupt mask */
  29. #define GISR 0x0c4 /* interrupt source */
  30. #define GITR 0x300 /* input type */
  31. #define GLPR 0x318 /* level input polarity */
  32. #define GWMR 0x400 /* wake mask */
  33. #define GWSR 0x418 /* wake source */
  34. #define GSIR 0xc00 /* secure input */
  35. /* Intel Merrifield has 192 GPIO pins */
  36. #define MRFLD_NGPIO 192
  37. struct mrfld_gpio_pinrange {
  38. unsigned int gpio_base;
  39. unsigned int pin_base;
  40. unsigned int npins;
  41. };
  42. #define GPIO_PINRANGE(gstart, gend, pstart) \
  43. { \
  44. .gpio_base = (gstart), \
  45. .pin_base = (pstart), \
  46. .npins = (gend) - (gstart) + 1, \
  47. }
  48. struct mrfld_gpio {
  49. struct gpio_chip chip;
  50. void __iomem *reg_base;
  51. raw_spinlock_t lock;
  52. struct device *dev;
  53. };
  54. static const struct mrfld_gpio_pinrange mrfld_gpio_ranges[] = {
  55. GPIO_PINRANGE(0, 11, 146),
  56. GPIO_PINRANGE(12, 13, 144),
  57. GPIO_PINRANGE(14, 15, 35),
  58. GPIO_PINRANGE(16, 16, 164),
  59. GPIO_PINRANGE(17, 18, 105),
  60. GPIO_PINRANGE(19, 22, 101),
  61. GPIO_PINRANGE(23, 30, 107),
  62. GPIO_PINRANGE(32, 43, 67),
  63. GPIO_PINRANGE(44, 63, 195),
  64. GPIO_PINRANGE(64, 67, 140),
  65. GPIO_PINRANGE(68, 69, 165),
  66. GPIO_PINRANGE(70, 71, 65),
  67. GPIO_PINRANGE(72, 76, 228),
  68. GPIO_PINRANGE(77, 86, 37),
  69. GPIO_PINRANGE(87, 87, 48),
  70. GPIO_PINRANGE(88, 88, 47),
  71. GPIO_PINRANGE(89, 96, 49),
  72. GPIO_PINRANGE(97, 97, 34),
  73. GPIO_PINRANGE(102, 119, 83),
  74. GPIO_PINRANGE(120, 123, 79),
  75. GPIO_PINRANGE(124, 135, 115),
  76. GPIO_PINRANGE(137, 142, 158),
  77. GPIO_PINRANGE(154, 163, 24),
  78. GPIO_PINRANGE(164, 176, 215),
  79. GPIO_PINRANGE(177, 189, 127),
  80. GPIO_PINRANGE(190, 191, 178),
  81. };
  82. static void __iomem *gpio_reg(struct gpio_chip *chip, unsigned int offset,
  83. unsigned int reg_type_offset)
  84. {
  85. struct mrfld_gpio *priv = gpiochip_get_data(chip);
  86. u8 reg = offset / 32;
  87. return priv->reg_base + reg_type_offset + reg * 4;
  88. }
  89. static int mrfld_gpio_get(struct gpio_chip *chip, unsigned int offset)
  90. {
  91. void __iomem *gplr = gpio_reg(chip, offset, GPLR);
  92. return !!(readl(gplr) & BIT(offset % 32));
  93. }
  94. static void mrfld_gpio_set(struct gpio_chip *chip, unsigned int offset,
  95. int value)
  96. {
  97. struct mrfld_gpio *priv = gpiochip_get_data(chip);
  98. void __iomem *gpsr, *gpcr;
  99. unsigned long flags;
  100. raw_spin_lock_irqsave(&priv->lock, flags);
  101. if (value) {
  102. gpsr = gpio_reg(chip, offset, GPSR);
  103. writel(BIT(offset % 32), gpsr);
  104. } else {
  105. gpcr = gpio_reg(chip, offset, GPCR);
  106. writel(BIT(offset % 32), gpcr);
  107. }
  108. raw_spin_unlock_irqrestore(&priv->lock, flags);
  109. }
  110. static int mrfld_gpio_direction_input(struct gpio_chip *chip,
  111. unsigned int offset)
  112. {
  113. struct mrfld_gpio *priv = gpiochip_get_data(chip);
  114. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  115. unsigned long flags;
  116. u32 value;
  117. raw_spin_lock_irqsave(&priv->lock, flags);
  118. value = readl(gpdr);
  119. value &= ~BIT(offset % 32);
  120. writel(value, gpdr);
  121. raw_spin_unlock_irqrestore(&priv->lock, flags);
  122. return 0;
  123. }
  124. static int mrfld_gpio_direction_output(struct gpio_chip *chip,
  125. unsigned int offset, int value)
  126. {
  127. struct mrfld_gpio *priv = gpiochip_get_data(chip);
  128. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  129. unsigned long flags;
  130. mrfld_gpio_set(chip, offset, value);
  131. raw_spin_lock_irqsave(&priv->lock, flags);
  132. value = readl(gpdr);
  133. value |= BIT(offset % 32);
  134. writel(value, gpdr);
  135. raw_spin_unlock_irqrestore(&priv->lock, flags);
  136. return 0;
  137. }
  138. static int mrfld_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  139. {
  140. void __iomem *gpdr = gpio_reg(chip, offset, GPDR);
  141. return (readl(gpdr) & BIT(offset % 32)) ? GPIOF_DIR_OUT : GPIOF_DIR_IN;
  142. }
  143. static int mrfld_gpio_set_debounce(struct gpio_chip *chip, unsigned int offset,
  144. unsigned int debounce)
  145. {
  146. struct mrfld_gpio *priv = gpiochip_get_data(chip);
  147. void __iomem *gfbr = gpio_reg(chip, offset, GFBR);
  148. unsigned long flags;
  149. u32 value;
  150. raw_spin_lock_irqsave(&priv->lock, flags);
  151. if (debounce)
  152. value = readl(gfbr) & ~BIT(offset % 32);
  153. else
  154. value = readl(gfbr) | BIT(offset % 32);
  155. writel(value, gfbr);
  156. raw_spin_unlock_irqrestore(&priv->lock, flags);
  157. return 0;
  158. }
  159. static void mrfld_irq_ack(struct irq_data *d)
  160. {
  161. struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
  162. u32 gpio = irqd_to_hwirq(d);
  163. void __iomem *gisr = gpio_reg(&priv->chip, gpio, GISR);
  164. unsigned long flags;
  165. raw_spin_lock_irqsave(&priv->lock, flags);
  166. writel(BIT(gpio % 32), gisr);
  167. raw_spin_unlock_irqrestore(&priv->lock, flags);
  168. }
  169. static void mrfld_irq_unmask_mask(struct irq_data *d, bool unmask)
  170. {
  171. struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
  172. u32 gpio = irqd_to_hwirq(d);
  173. void __iomem *gimr = gpio_reg(&priv->chip, gpio, GIMR);
  174. unsigned long flags;
  175. u32 value;
  176. raw_spin_lock_irqsave(&priv->lock, flags);
  177. if (unmask)
  178. value = readl(gimr) | BIT(gpio % 32);
  179. else
  180. value = readl(gimr) & ~BIT(gpio % 32);
  181. writel(value, gimr);
  182. raw_spin_unlock_irqrestore(&priv->lock, flags);
  183. }
  184. static void mrfld_irq_mask(struct irq_data *d)
  185. {
  186. mrfld_irq_unmask_mask(d, false);
  187. }
  188. static void mrfld_irq_unmask(struct irq_data *d)
  189. {
  190. mrfld_irq_unmask_mask(d, true);
  191. }
  192. static int mrfld_irq_set_type(struct irq_data *d, unsigned int type)
  193. {
  194. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  195. struct mrfld_gpio *priv = gpiochip_get_data(gc);
  196. u32 gpio = irqd_to_hwirq(d);
  197. void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
  198. void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
  199. void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR);
  200. void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR);
  201. unsigned long flags;
  202. u32 value;
  203. raw_spin_lock_irqsave(&priv->lock, flags);
  204. if (type & IRQ_TYPE_EDGE_RISING)
  205. value = readl(grer) | BIT(gpio % 32);
  206. else
  207. value = readl(grer) & ~BIT(gpio % 32);
  208. writel(value, grer);
  209. if (type & IRQ_TYPE_EDGE_FALLING)
  210. value = readl(gfer) | BIT(gpio % 32);
  211. else
  212. value = readl(gfer) & ~BIT(gpio % 32);
  213. writel(value, gfer);
  214. /*
  215. * To prevent glitches from triggering an unintended level interrupt,
  216. * configure GLPR register first and then configure GITR.
  217. */
  218. if (type & IRQ_TYPE_LEVEL_LOW)
  219. value = readl(glpr) | BIT(gpio % 32);
  220. else
  221. value = readl(glpr) & ~BIT(gpio % 32);
  222. writel(value, glpr);
  223. if (type & IRQ_TYPE_LEVEL_MASK) {
  224. value = readl(gitr) | BIT(gpio % 32);
  225. writel(value, gitr);
  226. irq_set_handler_locked(d, handle_level_irq);
  227. } else if (type & IRQ_TYPE_EDGE_BOTH) {
  228. value = readl(gitr) & ~BIT(gpio % 32);
  229. writel(value, gitr);
  230. irq_set_handler_locked(d, handle_edge_irq);
  231. }
  232. raw_spin_unlock_irqrestore(&priv->lock, flags);
  233. return 0;
  234. }
  235. static int mrfld_irq_set_wake(struct irq_data *d, unsigned int on)
  236. {
  237. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  238. struct mrfld_gpio *priv = gpiochip_get_data(gc);
  239. u32 gpio = irqd_to_hwirq(d);
  240. void __iomem *gwmr = gpio_reg(&priv->chip, gpio, GWMR);
  241. void __iomem *gwsr = gpio_reg(&priv->chip, gpio, GWSR);
  242. unsigned long flags;
  243. u32 value;
  244. raw_spin_lock_irqsave(&priv->lock, flags);
  245. /* Clear the existing wake status */
  246. writel(BIT(gpio % 32), gwsr);
  247. if (on)
  248. value = readl(gwmr) | BIT(gpio % 32);
  249. else
  250. value = readl(gwmr) & ~BIT(gpio % 32);
  251. writel(value, gwmr);
  252. raw_spin_unlock_irqrestore(&priv->lock, flags);
  253. dev_dbg(priv->dev, "%sable wake for gpio %u\n", on ? "en" : "dis", gpio);
  254. return 0;
  255. }
  256. static struct irq_chip mrfld_irqchip = {
  257. .name = "gpio-merrifield",
  258. .irq_ack = mrfld_irq_ack,
  259. .irq_mask = mrfld_irq_mask,
  260. .irq_unmask = mrfld_irq_unmask,
  261. .irq_set_type = mrfld_irq_set_type,
  262. .irq_set_wake = mrfld_irq_set_wake,
  263. };
  264. static void mrfld_irq_handler(struct irq_desc *desc)
  265. {
  266. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  267. struct mrfld_gpio *priv = gpiochip_get_data(gc);
  268. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  269. unsigned long base, gpio;
  270. chained_irq_enter(irqchip, desc);
  271. /* Check GPIO controller to check which pin triggered the interrupt */
  272. for (base = 0; base < priv->chip.ngpio; base += 32) {
  273. void __iomem *gisr = gpio_reg(&priv->chip, base, GISR);
  274. void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR);
  275. unsigned long pending, enabled;
  276. pending = readl(gisr);
  277. enabled = readl(gimr);
  278. /* Only interrupts that are enabled */
  279. pending &= enabled;
  280. for_each_set_bit(gpio, &pending, 32) {
  281. unsigned int irq;
  282. irq = irq_find_mapping(gc->irqdomain, base + gpio);
  283. generic_handle_irq(irq);
  284. }
  285. }
  286. chained_irq_exit(irqchip, desc);
  287. }
  288. static void mrfld_irq_init_hw(struct mrfld_gpio *priv)
  289. {
  290. void __iomem *reg;
  291. unsigned int base;
  292. for (base = 0; base < priv->chip.ngpio; base += 32) {
  293. /* Clear the rising-edge detect register */
  294. reg = gpio_reg(&priv->chip, base, GRER);
  295. writel(0, reg);
  296. /* Clear the falling-edge detect register */
  297. reg = gpio_reg(&priv->chip, base, GFER);
  298. writel(0, reg);
  299. }
  300. }
  301. static int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  302. {
  303. const struct mrfld_gpio_pinrange *range;
  304. struct mrfld_gpio *priv;
  305. u32 gpio_base, irq_base;
  306. void __iomem *base;
  307. unsigned int i;
  308. int retval;
  309. retval = pcim_enable_device(pdev);
  310. if (retval)
  311. return retval;
  312. retval = pcim_iomap_regions(pdev, BIT(1) | BIT(0), pci_name(pdev));
  313. if (retval) {
  314. dev_err(&pdev->dev, "I/O memory mapping error\n");
  315. return retval;
  316. }
  317. base = pcim_iomap_table(pdev)[1];
  318. irq_base = readl(base);
  319. gpio_base = readl(sizeof(u32) + base);
  320. /* Release the IO mapping, since we already get the info from BAR1 */
  321. pcim_iounmap_regions(pdev, BIT(1));
  322. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  323. if (!priv) {
  324. dev_err(&pdev->dev, "can't allocate chip data\n");
  325. return -ENOMEM;
  326. }
  327. priv->dev = &pdev->dev;
  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 = gpiochip_generic_request;
  332. priv->chip.free = gpiochip_generic_free;
  333. priv->chip.direction_input = mrfld_gpio_direction_input;
  334. priv->chip.direction_output = mrfld_gpio_direction_output;
  335. priv->chip.get = mrfld_gpio_get;
  336. priv->chip.set = mrfld_gpio_set;
  337. priv->chip.get_direction = mrfld_gpio_get_direction;
  338. priv->chip.set_debounce = mrfld_gpio_set_debounce;
  339. priv->chip.base = gpio_base;
  340. priv->chip.ngpio = MRFLD_NGPIO;
  341. priv->chip.can_sleep = false;
  342. raw_spin_lock_init(&priv->lock);
  343. pci_set_drvdata(pdev, priv);
  344. retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
  345. if (retval) {
  346. dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
  347. return retval;
  348. }
  349. for (i = 0; i < ARRAY_SIZE(mrfld_gpio_ranges); i++) {
  350. range = &mrfld_gpio_ranges[i];
  351. retval = gpiochip_add_pin_range(&priv->chip,
  352. "pinctrl-merrifield",
  353. range->gpio_base,
  354. range->pin_base,
  355. range->npins);
  356. if (retval) {
  357. dev_err(&pdev->dev, "failed to add GPIO pin range\n");
  358. return retval;
  359. }
  360. }
  361. retval = gpiochip_irqchip_add(&priv->chip, &mrfld_irqchip, irq_base,
  362. handle_bad_irq, IRQ_TYPE_NONE);
  363. if (retval) {
  364. dev_err(&pdev->dev, "could not connect irqchip to gpiochip\n");
  365. return retval;
  366. }
  367. mrfld_irq_init_hw(priv);
  368. gpiochip_set_chained_irqchip(&priv->chip, &mrfld_irqchip, pdev->irq,
  369. mrfld_irq_handler);
  370. return 0;
  371. }
  372. static const struct pci_device_id mrfld_gpio_ids[] = {
  373. { PCI_VDEVICE(INTEL, 0x1199) },
  374. { }
  375. };
  376. MODULE_DEVICE_TABLE(pci, mrfld_gpio_ids);
  377. static struct pci_driver mrfld_gpio_driver = {
  378. .name = "gpio-merrifield",
  379. .id_table = mrfld_gpio_ids,
  380. .probe = mrfld_gpio_probe,
  381. };
  382. module_pci_driver(mrfld_gpio_driver);
  383. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  384. MODULE_DESCRIPTION("Intel Merrifield SoC GPIO driver");
  385. MODULE_LICENSE("GPL v2");