gpio-merrifield.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/acpi.h>
  12. #include <linux/bitops.h>
  13. #include <linux/gpio/driver.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));
  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 int mrfld_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  160. unsigned long config)
  161. {
  162. u32 debounce;
  163. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  164. return -ENOTSUPP;
  165. debounce = pinconf_to_config_argument(config);
  166. return mrfld_gpio_set_debounce(chip, offset, debounce);
  167. }
  168. static void mrfld_irq_ack(struct irq_data *d)
  169. {
  170. struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
  171. u32 gpio = irqd_to_hwirq(d);
  172. void __iomem *gisr = gpio_reg(&priv->chip, gpio, GISR);
  173. unsigned long flags;
  174. raw_spin_lock_irqsave(&priv->lock, flags);
  175. writel(BIT(gpio % 32), gisr);
  176. raw_spin_unlock_irqrestore(&priv->lock, flags);
  177. }
  178. static void mrfld_irq_unmask_mask(struct irq_data *d, bool unmask)
  179. {
  180. struct mrfld_gpio *priv = irq_data_get_irq_chip_data(d);
  181. u32 gpio = irqd_to_hwirq(d);
  182. void __iomem *gimr = gpio_reg(&priv->chip, gpio, GIMR);
  183. unsigned long flags;
  184. u32 value;
  185. raw_spin_lock_irqsave(&priv->lock, flags);
  186. if (unmask)
  187. value = readl(gimr) | BIT(gpio % 32);
  188. else
  189. value = readl(gimr) & ~BIT(gpio % 32);
  190. writel(value, gimr);
  191. raw_spin_unlock_irqrestore(&priv->lock, flags);
  192. }
  193. static void mrfld_irq_mask(struct irq_data *d)
  194. {
  195. mrfld_irq_unmask_mask(d, false);
  196. }
  197. static void mrfld_irq_unmask(struct irq_data *d)
  198. {
  199. mrfld_irq_unmask_mask(d, true);
  200. }
  201. static int mrfld_irq_set_type(struct irq_data *d, unsigned int type)
  202. {
  203. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  204. struct mrfld_gpio *priv = gpiochip_get_data(gc);
  205. u32 gpio = irqd_to_hwirq(d);
  206. void __iomem *grer = gpio_reg(&priv->chip, gpio, GRER);
  207. void __iomem *gfer = gpio_reg(&priv->chip, gpio, GFER);
  208. void __iomem *gitr = gpio_reg(&priv->chip, gpio, GITR);
  209. void __iomem *glpr = gpio_reg(&priv->chip, gpio, GLPR);
  210. unsigned long flags;
  211. u32 value;
  212. raw_spin_lock_irqsave(&priv->lock, flags);
  213. if (type & IRQ_TYPE_EDGE_RISING)
  214. value = readl(grer) | BIT(gpio % 32);
  215. else
  216. value = readl(grer) & ~BIT(gpio % 32);
  217. writel(value, grer);
  218. if (type & IRQ_TYPE_EDGE_FALLING)
  219. value = readl(gfer) | BIT(gpio % 32);
  220. else
  221. value = readl(gfer) & ~BIT(gpio % 32);
  222. writel(value, gfer);
  223. /*
  224. * To prevent glitches from triggering an unintended level interrupt,
  225. * configure GLPR register first and then configure GITR.
  226. */
  227. if (type & IRQ_TYPE_LEVEL_LOW)
  228. value = readl(glpr) | BIT(gpio % 32);
  229. else
  230. value = readl(glpr) & ~BIT(gpio % 32);
  231. writel(value, glpr);
  232. if (type & IRQ_TYPE_LEVEL_MASK) {
  233. value = readl(gitr) | BIT(gpio % 32);
  234. writel(value, gitr);
  235. irq_set_handler_locked(d, handle_level_irq);
  236. } else if (type & IRQ_TYPE_EDGE_BOTH) {
  237. value = readl(gitr) & ~BIT(gpio % 32);
  238. writel(value, gitr);
  239. irq_set_handler_locked(d, handle_edge_irq);
  240. }
  241. raw_spin_unlock_irqrestore(&priv->lock, flags);
  242. return 0;
  243. }
  244. static int mrfld_irq_set_wake(struct irq_data *d, unsigned int on)
  245. {
  246. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  247. struct mrfld_gpio *priv = gpiochip_get_data(gc);
  248. u32 gpio = irqd_to_hwirq(d);
  249. void __iomem *gwmr = gpio_reg(&priv->chip, gpio, GWMR);
  250. void __iomem *gwsr = gpio_reg(&priv->chip, gpio, GWSR);
  251. unsigned long flags;
  252. u32 value;
  253. raw_spin_lock_irqsave(&priv->lock, flags);
  254. /* Clear the existing wake status */
  255. writel(BIT(gpio % 32), gwsr);
  256. if (on)
  257. value = readl(gwmr) | BIT(gpio % 32);
  258. else
  259. value = readl(gwmr) & ~BIT(gpio % 32);
  260. writel(value, gwmr);
  261. raw_spin_unlock_irqrestore(&priv->lock, flags);
  262. dev_dbg(priv->dev, "%sable wake for gpio %u\n", on ? "en" : "dis", gpio);
  263. return 0;
  264. }
  265. static struct irq_chip mrfld_irqchip = {
  266. .name = "gpio-merrifield",
  267. .irq_ack = mrfld_irq_ack,
  268. .irq_mask = mrfld_irq_mask,
  269. .irq_unmask = mrfld_irq_unmask,
  270. .irq_set_type = mrfld_irq_set_type,
  271. .irq_set_wake = mrfld_irq_set_wake,
  272. };
  273. static void mrfld_irq_handler(struct irq_desc *desc)
  274. {
  275. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  276. struct mrfld_gpio *priv = gpiochip_get_data(gc);
  277. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  278. unsigned long base, gpio;
  279. chained_irq_enter(irqchip, desc);
  280. /* Check GPIO controller to check which pin triggered the interrupt */
  281. for (base = 0; base < priv->chip.ngpio; base += 32) {
  282. void __iomem *gisr = gpio_reg(&priv->chip, base, GISR);
  283. void __iomem *gimr = gpio_reg(&priv->chip, base, GIMR);
  284. unsigned long pending, enabled;
  285. pending = readl(gisr);
  286. enabled = readl(gimr);
  287. /* Only interrupts that are enabled */
  288. pending &= enabled;
  289. for_each_set_bit(gpio, &pending, 32) {
  290. unsigned int irq;
  291. irq = irq_find_mapping(gc->irq.domain, base + gpio);
  292. generic_handle_irq(irq);
  293. }
  294. }
  295. chained_irq_exit(irqchip, desc);
  296. }
  297. static void mrfld_irq_init_hw(struct mrfld_gpio *priv)
  298. {
  299. void __iomem *reg;
  300. unsigned int base;
  301. for (base = 0; base < priv->chip.ngpio; base += 32) {
  302. /* Clear the rising-edge detect register */
  303. reg = gpio_reg(&priv->chip, base, GRER);
  304. writel(0, reg);
  305. /* Clear the falling-edge detect register */
  306. reg = gpio_reg(&priv->chip, base, GFER);
  307. writel(0, reg);
  308. }
  309. }
  310. static const char *mrfld_gpio_get_pinctrl_dev_name(void)
  311. {
  312. const char *dev_name = acpi_dev_get_first_match_name("INTC1002", NULL, -1);
  313. return dev_name ? dev_name : "pinctrl-merrifield";
  314. }
  315. static int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  316. {
  317. const struct mrfld_gpio_pinrange *range;
  318. const char *pinctrl_dev_name;
  319. struct mrfld_gpio *priv;
  320. u32 gpio_base, irq_base;
  321. void __iomem *base;
  322. unsigned int i;
  323. int retval;
  324. retval = pcim_enable_device(pdev);
  325. if (retval)
  326. return retval;
  327. retval = pcim_iomap_regions(pdev, BIT(1) | BIT(0), pci_name(pdev));
  328. if (retval) {
  329. dev_err(&pdev->dev, "I/O memory mapping error\n");
  330. return retval;
  331. }
  332. base = pcim_iomap_table(pdev)[1];
  333. irq_base = readl(base);
  334. gpio_base = readl(sizeof(u32) + base);
  335. /* Release the IO mapping, since we already get the info from BAR1 */
  336. pcim_iounmap_regions(pdev, BIT(1));
  337. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  338. if (!priv)
  339. return -ENOMEM;
  340. priv->dev = &pdev->dev;
  341. priv->reg_base = pcim_iomap_table(pdev)[0];
  342. priv->chip.label = dev_name(&pdev->dev);
  343. priv->chip.parent = &pdev->dev;
  344. priv->chip.request = gpiochip_generic_request;
  345. priv->chip.free = gpiochip_generic_free;
  346. priv->chip.direction_input = mrfld_gpio_direction_input;
  347. priv->chip.direction_output = mrfld_gpio_direction_output;
  348. priv->chip.get = mrfld_gpio_get;
  349. priv->chip.set = mrfld_gpio_set;
  350. priv->chip.get_direction = mrfld_gpio_get_direction;
  351. priv->chip.set_config = mrfld_gpio_set_config;
  352. priv->chip.base = gpio_base;
  353. priv->chip.ngpio = MRFLD_NGPIO;
  354. priv->chip.can_sleep = false;
  355. raw_spin_lock_init(&priv->lock);
  356. pci_set_drvdata(pdev, priv);
  357. retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
  358. if (retval) {
  359. dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
  360. return retval;
  361. }
  362. pinctrl_dev_name = mrfld_gpio_get_pinctrl_dev_name();
  363. for (i = 0; i < ARRAY_SIZE(mrfld_gpio_ranges); i++) {
  364. range = &mrfld_gpio_ranges[i];
  365. retval = gpiochip_add_pin_range(&priv->chip,
  366. pinctrl_dev_name,
  367. range->gpio_base,
  368. range->pin_base,
  369. range->npins);
  370. if (retval) {
  371. dev_err(&pdev->dev, "failed to add GPIO pin range\n");
  372. return retval;
  373. }
  374. }
  375. retval = gpiochip_irqchip_add(&priv->chip, &mrfld_irqchip, irq_base,
  376. handle_bad_irq, IRQ_TYPE_NONE);
  377. if (retval) {
  378. dev_err(&pdev->dev, "could not connect irqchip to gpiochip\n");
  379. return retval;
  380. }
  381. mrfld_irq_init_hw(priv);
  382. gpiochip_set_chained_irqchip(&priv->chip, &mrfld_irqchip, pdev->irq,
  383. mrfld_irq_handler);
  384. return 0;
  385. }
  386. static const struct pci_device_id mrfld_gpio_ids[] = {
  387. { PCI_VDEVICE(INTEL, 0x1199) },
  388. { }
  389. };
  390. MODULE_DEVICE_TABLE(pci, mrfld_gpio_ids);
  391. static struct pci_driver mrfld_gpio_driver = {
  392. .name = "gpio-merrifield",
  393. .id_table = mrfld_gpio_ids,
  394. .probe = mrfld_gpio_probe,
  395. };
  396. module_pci_driver(mrfld_gpio_driver);
  397. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  398. MODULE_DESCRIPTION("Intel Merrifield SoC GPIO driver");
  399. MODULE_LICENSE("GPL v2");