gpio-merrifield.c 12 KB

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