gpio-uniphier.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) 2017 Socionext Inc.
  3. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spinlock.h>
  24. #include <dt-bindings/gpio/uniphier-gpio.h>
  25. #define UNIPHIER_GPIO_BANK_MASK \
  26. GENMASK((UNIPHIER_GPIO_LINES_PER_BANK) - 1, 0)
  27. #define UNIPHIER_GPIO_IRQ_MAX_NUM 24
  28. #define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */
  29. #define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */
  30. #define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */
  31. #define UNIPHIER_GPIO_IRQ_MODE 0x94 /* irq mode (1: both edge) */
  32. #define UNIPHIER_GPIO_IRQ_FLT_EN 0x98 /* noise filter enable */
  33. #define UNIPHIER_GPIO_IRQ_FLT_CYC 0x9c /* noise filter clock cycle */
  34. struct uniphier_gpio_priv {
  35. struct gpio_chip chip;
  36. struct irq_chip irq_chip;
  37. struct irq_domain *domain;
  38. void __iomem *regs;
  39. spinlock_t lock;
  40. u32 saved_vals[0];
  41. };
  42. static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank)
  43. {
  44. unsigned int reg;
  45. reg = (bank + 1) * 8;
  46. /*
  47. * Unfortunately, the GPIO port registers are not contiguous because
  48. * offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region.
  49. */
  50. if (reg >= UNIPHIER_GPIO_IRQ_EN)
  51. reg += 0x10;
  52. return reg;
  53. }
  54. static void uniphier_gpio_get_bank_and_mask(unsigned int offset,
  55. unsigned int *bank, u32 *mask)
  56. {
  57. *bank = offset / UNIPHIER_GPIO_LINES_PER_BANK;
  58. *mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK);
  59. }
  60. static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv,
  61. unsigned int reg, u32 mask, u32 val)
  62. {
  63. unsigned long flags;
  64. u32 tmp;
  65. spin_lock_irqsave(&priv->lock, flags);
  66. tmp = readl(priv->regs + reg);
  67. tmp &= ~mask;
  68. tmp |= mask & val;
  69. writel(tmp, priv->regs + reg);
  70. spin_unlock_irqrestore(&priv->lock, flags);
  71. }
  72. static void uniphier_gpio_bank_write(struct gpio_chip *chip, unsigned int bank,
  73. unsigned int reg, u32 mask, u32 val)
  74. {
  75. struct uniphier_gpio_priv *priv = gpiochip_get_data(chip);
  76. if (!mask)
  77. return;
  78. uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg,
  79. mask, val);
  80. }
  81. static void uniphier_gpio_offset_write(struct gpio_chip *chip,
  82. unsigned int offset, unsigned int reg,
  83. int val)
  84. {
  85. unsigned int bank;
  86. u32 mask;
  87. uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
  88. uniphier_gpio_bank_write(chip, bank, reg, mask, val ? mask : 0);
  89. }
  90. static int uniphier_gpio_offset_read(struct gpio_chip *chip,
  91. unsigned int offset, unsigned int reg)
  92. {
  93. struct uniphier_gpio_priv *priv = gpiochip_get_data(chip);
  94. unsigned int bank, reg_offset;
  95. u32 mask;
  96. uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
  97. reg_offset = uniphier_gpio_bank_to_reg(bank) + reg;
  98. return !!(readl(priv->regs + reg_offset) & mask);
  99. }
  100. static int uniphier_gpio_get_direction(struct gpio_chip *chip,
  101. unsigned int offset)
  102. {
  103. return uniphier_gpio_offset_read(chip, offset, UNIPHIER_GPIO_PORT_DIR);
  104. }
  105. static int uniphier_gpio_direction_input(struct gpio_chip *chip,
  106. unsigned int offset)
  107. {
  108. uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DIR, 1);
  109. return 0;
  110. }
  111. static int uniphier_gpio_direction_output(struct gpio_chip *chip,
  112. unsigned int offset, int val)
  113. {
  114. uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DATA, val);
  115. uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DIR, 0);
  116. return 0;
  117. }
  118. static int uniphier_gpio_get(struct gpio_chip *chip, unsigned int offset)
  119. {
  120. return uniphier_gpio_offset_read(chip, offset, UNIPHIER_GPIO_PORT_DATA);
  121. }
  122. static void uniphier_gpio_set(struct gpio_chip *chip,
  123. unsigned int offset, int val)
  124. {
  125. uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DATA, val);
  126. }
  127. static void uniphier_gpio_set_multiple(struct gpio_chip *chip,
  128. unsigned long *mask, unsigned long *bits)
  129. {
  130. unsigned int bank, shift, bank_mask, bank_bits;
  131. int i;
  132. for (i = 0; i < chip->ngpio; i += UNIPHIER_GPIO_LINES_PER_BANK) {
  133. bank = i / UNIPHIER_GPIO_LINES_PER_BANK;
  134. shift = i % BITS_PER_LONG;
  135. bank_mask = (mask[BIT_WORD(i)] >> shift) &
  136. UNIPHIER_GPIO_BANK_MASK;
  137. bank_bits = bits[BIT_WORD(i)] >> shift;
  138. uniphier_gpio_bank_write(chip, bank, UNIPHIER_GPIO_PORT_DATA,
  139. bank_mask, bank_bits);
  140. }
  141. }
  142. static int uniphier_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
  143. {
  144. struct irq_fwspec fwspec;
  145. if (offset < UNIPHIER_GPIO_IRQ_OFFSET)
  146. return -ENXIO;
  147. fwspec.fwnode = of_node_to_fwnode(chip->parent->of_node);
  148. fwspec.param_count = 2;
  149. fwspec.param[0] = offset - UNIPHIER_GPIO_IRQ_OFFSET;
  150. /*
  151. * IRQ_TYPE_NONE is rejected by the parent irq domain. Set LEVEL_HIGH
  152. * temporarily. Anyway, ->irq_set_type() will override it later.
  153. */
  154. fwspec.param[1] = IRQ_TYPE_LEVEL_HIGH;
  155. return irq_create_fwspec_mapping(&fwspec);
  156. }
  157. static void uniphier_gpio_irq_mask(struct irq_data *data)
  158. {
  159. struct uniphier_gpio_priv *priv = data->chip_data;
  160. u32 mask = BIT(data->hwirq);
  161. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, 0);
  162. return irq_chip_mask_parent(data);
  163. }
  164. static void uniphier_gpio_irq_unmask(struct irq_data *data)
  165. {
  166. struct uniphier_gpio_priv *priv = data->chip_data;
  167. u32 mask = BIT(data->hwirq);
  168. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, mask);
  169. return irq_chip_unmask_parent(data);
  170. }
  171. static int uniphier_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  172. {
  173. struct uniphier_gpio_priv *priv = data->chip_data;
  174. u32 mask = BIT(data->hwirq);
  175. u32 val = 0;
  176. if (type == IRQ_TYPE_EDGE_BOTH) {
  177. val = mask;
  178. type = IRQ_TYPE_EDGE_FALLING;
  179. }
  180. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_MODE, mask, val);
  181. /* To enable both edge detection, the noise filter must be enabled. */
  182. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_FLT_EN, mask, val);
  183. return irq_chip_set_type_parent(data, type);
  184. }
  185. static int uniphier_gpio_irq_get_parent_hwirq(struct uniphier_gpio_priv *priv,
  186. unsigned int hwirq)
  187. {
  188. struct device_node *np = priv->chip.parent->of_node;
  189. const __be32 *range;
  190. u32 base, parent_base, size;
  191. int len;
  192. range = of_get_property(np, "socionext,interrupt-ranges", &len);
  193. if (!range)
  194. return -EINVAL;
  195. len /= sizeof(*range);
  196. for (; len >= 3; len -= 3) {
  197. base = be32_to_cpu(*range++);
  198. parent_base = be32_to_cpu(*range++);
  199. size = be32_to_cpu(*range++);
  200. if (base <= hwirq && hwirq < base + size)
  201. return hwirq - base + parent_base;
  202. }
  203. return -ENOENT;
  204. }
  205. static int uniphier_gpio_irq_domain_translate(struct irq_domain *domain,
  206. struct irq_fwspec *fwspec,
  207. unsigned long *out_hwirq,
  208. unsigned int *out_type)
  209. {
  210. if (WARN_ON(fwspec->param_count < 2))
  211. return -EINVAL;
  212. *out_hwirq = fwspec->param[0];
  213. *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  214. return 0;
  215. }
  216. static int uniphier_gpio_irq_domain_alloc(struct irq_domain *domain,
  217. unsigned int virq,
  218. unsigned int nr_irqs, void *arg)
  219. {
  220. struct uniphier_gpio_priv *priv = domain->host_data;
  221. struct irq_fwspec parent_fwspec;
  222. irq_hw_number_t hwirq;
  223. unsigned int type;
  224. int ret;
  225. if (WARN_ON(nr_irqs != 1))
  226. return -EINVAL;
  227. ret = uniphier_gpio_irq_domain_translate(domain, arg, &hwirq, &type);
  228. if (ret)
  229. return ret;
  230. ret = uniphier_gpio_irq_get_parent_hwirq(priv, hwirq);
  231. if (ret < 0)
  232. return ret;
  233. /* parent is UniPhier AIDET */
  234. parent_fwspec.fwnode = domain->parent->fwnode;
  235. parent_fwspec.param_count = 2;
  236. parent_fwspec.param[0] = ret;
  237. parent_fwspec.param[1] = (type == IRQ_TYPE_EDGE_BOTH) ?
  238. IRQ_TYPE_EDGE_FALLING : type;
  239. ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  240. &priv->irq_chip, priv);
  241. if (ret)
  242. return ret;
  243. return irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
  244. }
  245. static int uniphier_gpio_irq_domain_activate(struct irq_domain *domain,
  246. struct irq_data *data, bool early)
  247. {
  248. struct uniphier_gpio_priv *priv = domain->host_data;
  249. struct gpio_chip *chip = &priv->chip;
  250. return gpiochip_lock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
  251. }
  252. static void uniphier_gpio_irq_domain_deactivate(struct irq_domain *domain,
  253. struct irq_data *data)
  254. {
  255. struct uniphier_gpio_priv *priv = domain->host_data;
  256. struct gpio_chip *chip = &priv->chip;
  257. gpiochip_unlock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
  258. }
  259. static const struct irq_domain_ops uniphier_gpio_irq_domain_ops = {
  260. .alloc = uniphier_gpio_irq_domain_alloc,
  261. .free = irq_domain_free_irqs_common,
  262. .activate = uniphier_gpio_irq_domain_activate,
  263. .deactivate = uniphier_gpio_irq_domain_deactivate,
  264. .translate = uniphier_gpio_irq_domain_translate,
  265. };
  266. static void uniphier_gpio_hw_init(struct uniphier_gpio_priv *priv)
  267. {
  268. /*
  269. * Due to the hardware design, the noise filter must be enabled to
  270. * detect both edge interrupts. This filter is intended to remove the
  271. * noise from the irq lines. It does not work for GPIO input, so GPIO
  272. * debounce is not supported. Unfortunately, the filter period is
  273. * shared among all irq lines. Just choose a sensible period here.
  274. */
  275. writel(0xff, priv->regs + UNIPHIER_GPIO_IRQ_FLT_CYC);
  276. }
  277. static unsigned int uniphier_gpio_get_nbanks(unsigned int ngpio)
  278. {
  279. return DIV_ROUND_UP(ngpio, UNIPHIER_GPIO_LINES_PER_BANK);
  280. }
  281. static int uniphier_gpio_probe(struct platform_device *pdev)
  282. {
  283. struct device *dev = &pdev->dev;
  284. struct device_node *parent_np;
  285. struct irq_domain *parent_domain;
  286. struct uniphier_gpio_priv *priv;
  287. struct gpio_chip *chip;
  288. struct irq_chip *irq_chip;
  289. struct resource *regs;
  290. unsigned int nregs;
  291. u32 ngpios;
  292. int ret;
  293. parent_np = of_irq_find_parent(dev->of_node);
  294. if (!parent_np)
  295. return -ENXIO;
  296. parent_domain = irq_find_host(parent_np);
  297. of_node_put(parent_np);
  298. if (!parent_domain)
  299. return -EPROBE_DEFER;
  300. ret = of_property_read_u32(dev->of_node, "ngpios", &ngpios);
  301. if (ret)
  302. return ret;
  303. nregs = uniphier_gpio_get_nbanks(ngpios) * 2 + 3;
  304. priv = devm_kzalloc(dev, struct_size(priv, saved_vals, nregs),
  305. GFP_KERNEL);
  306. if (!priv)
  307. return -ENOMEM;
  308. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  309. priv->regs = devm_ioremap_resource(dev, regs);
  310. if (IS_ERR(priv->regs))
  311. return PTR_ERR(priv->regs);
  312. spin_lock_init(&priv->lock);
  313. chip = &priv->chip;
  314. chip->label = dev_name(dev);
  315. chip->parent = dev;
  316. chip->request = gpiochip_generic_request;
  317. chip->free = gpiochip_generic_free;
  318. chip->get_direction = uniphier_gpio_get_direction;
  319. chip->direction_input = uniphier_gpio_direction_input;
  320. chip->direction_output = uniphier_gpio_direction_output;
  321. chip->get = uniphier_gpio_get;
  322. chip->set = uniphier_gpio_set;
  323. chip->set_multiple = uniphier_gpio_set_multiple;
  324. chip->to_irq = uniphier_gpio_to_irq;
  325. chip->base = -1;
  326. chip->ngpio = ngpios;
  327. irq_chip = &priv->irq_chip;
  328. irq_chip->name = dev_name(dev);
  329. irq_chip->irq_mask = uniphier_gpio_irq_mask;
  330. irq_chip->irq_unmask = uniphier_gpio_irq_unmask;
  331. irq_chip->irq_eoi = irq_chip_eoi_parent;
  332. irq_chip->irq_set_affinity = irq_chip_set_affinity_parent;
  333. irq_chip->irq_set_type = uniphier_gpio_irq_set_type;
  334. uniphier_gpio_hw_init(priv);
  335. ret = devm_gpiochip_add_data(dev, chip, priv);
  336. if (ret)
  337. return ret;
  338. priv->domain = irq_domain_create_hierarchy(
  339. parent_domain, 0,
  340. UNIPHIER_GPIO_IRQ_MAX_NUM,
  341. of_node_to_fwnode(dev->of_node),
  342. &uniphier_gpio_irq_domain_ops, priv);
  343. if (!priv->domain)
  344. return -ENOMEM;
  345. platform_set_drvdata(pdev, priv);
  346. return 0;
  347. }
  348. static int uniphier_gpio_remove(struct platform_device *pdev)
  349. {
  350. struct uniphier_gpio_priv *priv = platform_get_drvdata(pdev);
  351. irq_domain_remove(priv->domain);
  352. return 0;
  353. }
  354. static int __maybe_unused uniphier_gpio_suspend(struct device *dev)
  355. {
  356. struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
  357. unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
  358. u32 *val = priv->saved_vals;
  359. unsigned int reg;
  360. int i;
  361. for (i = 0; i < nbanks; i++) {
  362. reg = uniphier_gpio_bank_to_reg(i);
  363. *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
  364. *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
  365. }
  366. *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_EN);
  367. *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_MODE);
  368. *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
  369. return 0;
  370. }
  371. static int __maybe_unused uniphier_gpio_resume(struct device *dev)
  372. {
  373. struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
  374. unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
  375. const u32 *val = priv->saved_vals;
  376. unsigned int reg;
  377. int i;
  378. for (i = 0; i < nbanks; i++) {
  379. reg = uniphier_gpio_bank_to_reg(i);
  380. writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
  381. writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
  382. }
  383. writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_EN);
  384. writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_MODE);
  385. writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
  386. uniphier_gpio_hw_init(priv);
  387. return 0;
  388. }
  389. static const struct dev_pm_ops uniphier_gpio_pm_ops = {
  390. SET_LATE_SYSTEM_SLEEP_PM_OPS(uniphier_gpio_suspend,
  391. uniphier_gpio_resume)
  392. };
  393. static const struct of_device_id uniphier_gpio_match[] = {
  394. { .compatible = "socionext,uniphier-gpio" },
  395. { /* sentinel */ }
  396. };
  397. MODULE_DEVICE_TABLE(of, uniphier_gpio_match);
  398. static struct platform_driver uniphier_gpio_driver = {
  399. .probe = uniphier_gpio_probe,
  400. .remove = uniphier_gpio_remove,
  401. .driver = {
  402. .name = "uniphier-gpio",
  403. .of_match_table = uniphier_gpio_match,
  404. .pm = &uniphier_gpio_pm_ops,
  405. },
  406. };
  407. module_platform_driver(uniphier_gpio_driver);
  408. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  409. MODULE_DESCRIPTION("UniPhier GPIO driver");
  410. MODULE_LICENSE("GPL v2");