gpio-uniphier.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. fwspec.param[1] = IRQ_TYPE_NONE;
  151. return irq_create_fwspec_mapping(&fwspec);
  152. }
  153. static void uniphier_gpio_irq_mask(struct irq_data *data)
  154. {
  155. struct uniphier_gpio_priv *priv = data->chip_data;
  156. u32 mask = BIT(data->hwirq);
  157. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, 0);
  158. return irq_chip_mask_parent(data);
  159. }
  160. static void uniphier_gpio_irq_unmask(struct irq_data *data)
  161. {
  162. struct uniphier_gpio_priv *priv = data->chip_data;
  163. u32 mask = BIT(data->hwirq);
  164. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, mask);
  165. return irq_chip_unmask_parent(data);
  166. }
  167. static int uniphier_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  168. {
  169. struct uniphier_gpio_priv *priv = data->chip_data;
  170. u32 mask = BIT(data->hwirq);
  171. u32 val = 0;
  172. if (type == IRQ_TYPE_EDGE_BOTH) {
  173. val = mask;
  174. type = IRQ_TYPE_EDGE_FALLING;
  175. }
  176. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_MODE, mask, val);
  177. /* To enable both edge detection, the noise filter must be enabled. */
  178. uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_FLT_EN, mask, val);
  179. return irq_chip_set_type_parent(data, type);
  180. }
  181. static int uniphier_gpio_irq_get_parent_hwirq(struct uniphier_gpio_priv *priv,
  182. unsigned int hwirq)
  183. {
  184. struct device_node *np = priv->chip.parent->of_node;
  185. const __be32 *range;
  186. u32 base, parent_base, size;
  187. int len;
  188. range = of_get_property(np, "socionext,interrupt-ranges", &len);
  189. if (!range)
  190. return -EINVAL;
  191. len /= sizeof(*range);
  192. for (; len >= 3; len -= 3) {
  193. base = be32_to_cpu(*range++);
  194. parent_base = be32_to_cpu(*range++);
  195. size = be32_to_cpu(*range++);
  196. if (base <= hwirq && hwirq < base + size)
  197. return hwirq - base + parent_base;
  198. }
  199. return -ENOENT;
  200. }
  201. static int uniphier_gpio_irq_domain_translate(struct irq_domain *domain,
  202. struct irq_fwspec *fwspec,
  203. unsigned long *out_hwirq,
  204. unsigned int *out_type)
  205. {
  206. if (WARN_ON(fwspec->param_count < 2))
  207. return -EINVAL;
  208. *out_hwirq = fwspec->param[0];
  209. *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  210. return 0;
  211. }
  212. static int uniphier_gpio_irq_domain_alloc(struct irq_domain *domain,
  213. unsigned int virq,
  214. unsigned int nr_irqs, void *arg)
  215. {
  216. struct uniphier_gpio_priv *priv = domain->host_data;
  217. struct irq_fwspec parent_fwspec;
  218. irq_hw_number_t hwirq;
  219. unsigned int type;
  220. int ret;
  221. if (WARN_ON(nr_irqs != 1))
  222. return -EINVAL;
  223. ret = uniphier_gpio_irq_domain_translate(domain, arg, &hwirq, &type);
  224. if (ret)
  225. return ret;
  226. ret = uniphier_gpio_irq_get_parent_hwirq(priv, hwirq);
  227. if (ret < 0)
  228. return ret;
  229. /* parent is UniPhier AIDET */
  230. parent_fwspec.fwnode = domain->parent->fwnode;
  231. parent_fwspec.param_count = 2;
  232. parent_fwspec.param[0] = ret;
  233. parent_fwspec.param[1] = (type == IRQ_TYPE_EDGE_BOTH) ?
  234. IRQ_TYPE_EDGE_FALLING : type;
  235. ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  236. &priv->irq_chip, priv);
  237. if (ret)
  238. return ret;
  239. return irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
  240. }
  241. static void uniphier_gpio_irq_domain_activate(struct irq_domain *domain,
  242. struct irq_data *data)
  243. {
  244. struct uniphier_gpio_priv *priv = domain->host_data;
  245. struct gpio_chip *chip = &priv->chip;
  246. gpiochip_lock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
  247. }
  248. static void uniphier_gpio_irq_domain_deactivate(struct irq_domain *domain,
  249. struct irq_data *data)
  250. {
  251. struct uniphier_gpio_priv *priv = domain->host_data;
  252. struct gpio_chip *chip = &priv->chip;
  253. gpiochip_unlock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
  254. }
  255. static const struct irq_domain_ops uniphier_gpio_irq_domain_ops = {
  256. .alloc = uniphier_gpio_irq_domain_alloc,
  257. .free = irq_domain_free_irqs_common,
  258. .activate = uniphier_gpio_irq_domain_activate,
  259. .deactivate = uniphier_gpio_irq_domain_deactivate,
  260. .translate = uniphier_gpio_irq_domain_translate,
  261. };
  262. static void uniphier_gpio_hw_init(struct uniphier_gpio_priv *priv)
  263. {
  264. /*
  265. * Due to the hardware design, the noise filter must be enabled to
  266. * detect both edge interrupts. This filter is intended to remove the
  267. * noise from the irq lines. It does not work for GPIO input, so GPIO
  268. * debounce is not supported. Unfortunately, the filter period is
  269. * shared among all irq lines. Just choose a sensible period here.
  270. */
  271. writel(0xff, priv->regs + UNIPHIER_GPIO_IRQ_FLT_CYC);
  272. }
  273. static unsigned int uniphier_gpio_get_nbanks(unsigned int ngpio)
  274. {
  275. return DIV_ROUND_UP(ngpio, UNIPHIER_GPIO_LINES_PER_BANK);
  276. }
  277. static int uniphier_gpio_probe(struct platform_device *pdev)
  278. {
  279. struct device *dev = &pdev->dev;
  280. struct device_node *parent_np;
  281. struct irq_domain *parent_domain;
  282. struct uniphier_gpio_priv *priv;
  283. struct gpio_chip *chip;
  284. struct irq_chip *irq_chip;
  285. struct resource *regs;
  286. unsigned int nregs;
  287. u32 ngpios;
  288. int ret;
  289. parent_np = of_irq_find_parent(dev->of_node);
  290. if (!parent_np)
  291. return -ENXIO;
  292. parent_domain = irq_find_host(parent_np);
  293. of_node_put(parent_np);
  294. if (!parent_domain)
  295. return -EPROBE_DEFER;
  296. ret = of_property_read_u32(dev->of_node, "ngpios", &ngpios);
  297. if (ret)
  298. return ret;
  299. nregs = uniphier_gpio_get_nbanks(ngpios) * 2 + 3;
  300. priv = devm_kzalloc(dev,
  301. sizeof(*priv) + sizeof(priv->saved_vals[0]) * nregs,
  302. GFP_KERNEL);
  303. if (!priv)
  304. return -ENOMEM;
  305. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  306. priv->regs = devm_ioremap_resource(dev, regs);
  307. if (IS_ERR(priv->regs))
  308. return PTR_ERR(priv->regs);
  309. spin_lock_init(&priv->lock);
  310. chip = &priv->chip;
  311. chip->label = dev_name(dev);
  312. chip->parent = dev;
  313. chip->request = gpiochip_generic_request;
  314. chip->free = gpiochip_generic_free;
  315. chip->get_direction = uniphier_gpio_get_direction;
  316. chip->direction_input = uniphier_gpio_direction_input;
  317. chip->direction_output = uniphier_gpio_direction_output;
  318. chip->get = uniphier_gpio_get;
  319. chip->set = uniphier_gpio_set;
  320. chip->set_multiple = uniphier_gpio_set_multiple;
  321. chip->to_irq = uniphier_gpio_to_irq;
  322. chip->base = -1;
  323. chip->ngpio = ngpios;
  324. irq_chip = &priv->irq_chip;
  325. irq_chip->name = dev_name(dev);
  326. irq_chip->irq_mask = uniphier_gpio_irq_mask;
  327. irq_chip->irq_unmask = uniphier_gpio_irq_unmask;
  328. irq_chip->irq_eoi = irq_chip_eoi_parent;
  329. irq_chip->irq_set_affinity = irq_chip_set_affinity_parent;
  330. irq_chip->irq_set_type = uniphier_gpio_irq_set_type;
  331. uniphier_gpio_hw_init(priv);
  332. ret = devm_gpiochip_add_data(dev, chip, priv);
  333. if (ret)
  334. return ret;
  335. priv->domain = irq_domain_create_hierarchy(
  336. parent_domain, 0,
  337. UNIPHIER_GPIO_IRQ_MAX_NUM,
  338. of_node_to_fwnode(dev->of_node),
  339. &uniphier_gpio_irq_domain_ops, priv);
  340. if (!priv->domain)
  341. return -ENOMEM;
  342. platform_set_drvdata(pdev, priv);
  343. return 0;
  344. }
  345. static int uniphier_gpio_remove(struct platform_device *pdev)
  346. {
  347. struct uniphier_gpio_priv *priv = platform_get_drvdata(pdev);
  348. irq_domain_remove(priv->domain);
  349. return 0;
  350. }
  351. static int __maybe_unused uniphier_gpio_suspend(struct device *dev)
  352. {
  353. struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
  354. unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
  355. u32 *val = priv->saved_vals;
  356. unsigned int reg;
  357. int i;
  358. for (i = 0; i < nbanks; i++) {
  359. reg = uniphier_gpio_bank_to_reg(i);
  360. *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
  361. *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
  362. }
  363. *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_EN);
  364. *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_MODE);
  365. *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
  366. return 0;
  367. }
  368. static int __maybe_unused uniphier_gpio_resume(struct device *dev)
  369. {
  370. struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
  371. unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
  372. const u32 *val = priv->saved_vals;
  373. unsigned int reg;
  374. int i;
  375. for (i = 0; i < nbanks; i++) {
  376. reg = uniphier_gpio_bank_to_reg(i);
  377. writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
  378. writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
  379. }
  380. writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_EN);
  381. writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_MODE);
  382. writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
  383. uniphier_gpio_hw_init(priv);
  384. return 0;
  385. }
  386. static const struct dev_pm_ops uniphier_gpio_pm_ops = {
  387. SET_LATE_SYSTEM_SLEEP_PM_OPS(uniphier_gpio_suspend,
  388. uniphier_gpio_resume)
  389. };
  390. static const struct of_device_id uniphier_gpio_match[] = {
  391. { .compatible = "socionext,uniphier-gpio" },
  392. { /* sentinel */ }
  393. };
  394. MODULE_DEVICE_TABLE(of, uniphier_gpio_match);
  395. static struct platform_driver uniphier_gpio_driver = {
  396. .probe = uniphier_gpio_probe,
  397. .remove = uniphier_gpio_remove,
  398. .driver = {
  399. .name = "uniphier-gpio",
  400. .of_match_table = uniphier_gpio_match,
  401. .pm = &uniphier_gpio_pm_ops,
  402. },
  403. };
  404. module_platform_driver(uniphier_gpio_driver);
  405. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  406. MODULE_DESCRIPTION("UniPhier GPIO driver");
  407. MODULE_LICENSE("GPL");