PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/gpio/gpio-merrifield.c

https://github.com/tklauser/linux-nios2
C | 487 lines | 373 code | 93 blank | 21 comment | 22 complexity | 4e710c580418bbcc40aecae65282eb9f MD5 | raw file
  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));
  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->irqdomain, 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 int mrfld_gpio_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  311. {
  312. const struct mrfld_gpio_pinrange *range;
  313. struct mrfld_gpio *priv;
  314. u32 gpio_base, irq_base;
  315. void __iomem *base;
  316. unsigned int i;
  317. int retval;
  318. retval = pcim_enable_device(pdev);
  319. if (retval)
  320. return retval;
  321. retval = pcim_iomap_regions(pdev, BIT(1) | BIT(0), pci_name(pdev));
  322. if (retval) {
  323. dev_err(&pdev->dev, "I/O memory mapping error\n");
  324. return retval;
  325. }
  326. base = pcim_iomap_table(pdev)[1];
  327. irq_base = readl(base);
  328. gpio_base = readl(sizeof(u32) + base);
  329. /* Release the IO mapping, since we already get the info from BAR1 */
  330. pcim_iounmap_regions(pdev, BIT(1));
  331. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  332. if (!priv) {
  333. dev_err(&pdev->dev, "can't allocate chip data\n");
  334. return -ENOMEM;
  335. }
  336. priv->dev = &pdev->dev;
  337. priv->reg_base = pcim_iomap_table(pdev)[0];
  338. priv->chip.label = dev_name(&pdev->dev);
  339. priv->chip.parent = &pdev->dev;
  340. priv->chip.request = gpiochip_generic_request;
  341. priv->chip.free = gpiochip_generic_free;
  342. priv->chip.direction_input = mrfld_gpio_direction_input;
  343. priv->chip.direction_output = mrfld_gpio_direction_output;
  344. priv->chip.get = mrfld_gpio_get;
  345. priv->chip.set = mrfld_gpio_set;
  346. priv->chip.get_direction = mrfld_gpio_get_direction;
  347. priv->chip.set_config = mrfld_gpio_set_config;
  348. priv->chip.base = gpio_base;
  349. priv->chip.ngpio = MRFLD_NGPIO;
  350. priv->chip.can_sleep = false;
  351. raw_spin_lock_init(&priv->lock);
  352. pci_set_drvdata(pdev, priv);
  353. retval = devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
  354. if (retval) {
  355. dev_err(&pdev->dev, "gpiochip_add error %d\n", retval);
  356. return retval;
  357. }
  358. for (i = 0; i < ARRAY_SIZE(mrfld_gpio_ranges); i++) {
  359. range = &mrfld_gpio_ranges[i];
  360. retval = gpiochip_add_pin_range(&priv->chip,
  361. "pinctrl-merrifield",
  362. range->gpio_base,
  363. range->pin_base,
  364. range->npins);
  365. if (retval) {
  366. dev_err(&pdev->dev, "failed to add GPIO pin range\n");
  367. return retval;
  368. }
  369. }
  370. retval = gpiochip_irqchip_add(&priv->chip, &mrfld_irqchip, irq_base,
  371. handle_bad_irq, IRQ_TYPE_NONE);
  372. if (retval) {
  373. dev_err(&pdev->dev, "could not connect irqchip to gpiochip\n");
  374. return retval;
  375. }
  376. mrfld_irq_init_hw(priv);
  377. gpiochip_set_chained_irqchip(&priv->chip, &mrfld_irqchip, pdev->irq,
  378. mrfld_irq_handler);
  379. return 0;
  380. }
  381. static const struct pci_device_id mrfld_gpio_ids[] = {
  382. { PCI_VDEVICE(INTEL, 0x1199) },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(pci, mrfld_gpio_ids);
  386. static struct pci_driver mrfld_gpio_driver = {
  387. .name = "gpio-merrifield",
  388. .id_table = mrfld_gpio_ids,
  389. .probe = mrfld_gpio_probe,
  390. };
  391. module_pci_driver(mrfld_gpio_driver);
  392. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  393. MODULE_DESCRIPTION("Intel Merrifield SoC GPIO driver");
  394. MODULE_LICENSE("GPL v2");