1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2017 Socionext Inc.
4 // Author: Masahiro Yamada <yamada.masahiro@socionext.com>
6 #include <linux/bits.h>
7 #include <linux/gpio/driver.h>
9 #include <linux/irqdomain.h>
10 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_irq.h>
14 #include <linux/platform_device.h>
15 #include <linux/spinlock.h>
16 #include <dt-bindings/gpio/uniphier-gpio.h>
18 #define UNIPHIER_GPIO_BANK_MASK \
19 GENMASK((UNIPHIER_GPIO_LINES_PER_BANK) - 1, 0)
21 #define UNIPHIER_GPIO_IRQ_MAX_NUM 24
23 #define UNIPHIER_GPIO_PORT_DATA 0x0 /* data */
24 #define UNIPHIER_GPIO_PORT_DIR 0x4 /* direction (1:in, 0:out) */
25 #define UNIPHIER_GPIO_IRQ_EN 0x90 /* irq enable */
26 #define UNIPHIER_GPIO_IRQ_MODE 0x94 /* irq mode (1: both edge) */
27 #define UNIPHIER_GPIO_IRQ_FLT_EN 0x98 /* noise filter enable */
28 #define UNIPHIER_GPIO_IRQ_FLT_CYC 0x9c /* noise filter clock cycle */
30 struct uniphier_gpio_priv {
31 struct gpio_chip chip;
32 struct irq_chip irq_chip;
33 struct irq_domain *domain;
39 static unsigned int uniphier_gpio_bank_to_reg(unsigned int bank)
46 * Unfortunately, the GPIO port registers are not contiguous because
47 * offset 0x90-0x9f is used for IRQ. Add 0x10 when crossing the region.
49 if (reg >= UNIPHIER_GPIO_IRQ_EN)
55 static void uniphier_gpio_get_bank_and_mask(unsigned int offset,
56 unsigned int *bank, u32 *mask)
58 *bank = offset / UNIPHIER_GPIO_LINES_PER_BANK;
59 *mask = BIT(offset % UNIPHIER_GPIO_LINES_PER_BANK);
62 static void uniphier_gpio_reg_update(struct uniphier_gpio_priv *priv,
63 unsigned int reg, u32 mask, u32 val)
68 spin_lock_irqsave(&priv->lock, flags);
69 tmp = readl(priv->regs + reg);
72 writel(tmp, priv->regs + reg);
73 spin_unlock_irqrestore(&priv->lock, flags);
76 static void uniphier_gpio_bank_write(struct gpio_chip *chip, unsigned int bank,
77 unsigned int reg, u32 mask, u32 val)
79 struct uniphier_gpio_priv *priv = gpiochip_get_data(chip);
84 uniphier_gpio_reg_update(priv, uniphier_gpio_bank_to_reg(bank) + reg,
88 static void uniphier_gpio_offset_write(struct gpio_chip *chip,
89 unsigned int offset, unsigned int reg,
95 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
97 uniphier_gpio_bank_write(chip, bank, reg, mask, val ? mask : 0);
100 static int uniphier_gpio_offset_read(struct gpio_chip *chip,
101 unsigned int offset, unsigned int reg)
103 struct uniphier_gpio_priv *priv = gpiochip_get_data(chip);
104 unsigned int bank, reg_offset;
107 uniphier_gpio_get_bank_and_mask(offset, &bank, &mask);
108 reg_offset = uniphier_gpio_bank_to_reg(bank) + reg;
110 return !!(readl(priv->regs + reg_offset) & mask);
113 static int uniphier_gpio_get_direction(struct gpio_chip *chip,
116 if (uniphier_gpio_offset_read(chip, offset, UNIPHIER_GPIO_PORT_DIR))
117 return GPIO_LINE_DIRECTION_IN;
119 return GPIO_LINE_DIRECTION_OUT;
122 static int uniphier_gpio_direction_input(struct gpio_chip *chip,
125 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DIR, 1);
130 static int uniphier_gpio_direction_output(struct gpio_chip *chip,
131 unsigned int offset, int val)
133 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DATA, val);
134 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DIR, 0);
139 static int uniphier_gpio_get(struct gpio_chip *chip, unsigned int offset)
141 return uniphier_gpio_offset_read(chip, offset, UNIPHIER_GPIO_PORT_DATA);
144 static void uniphier_gpio_set(struct gpio_chip *chip,
145 unsigned int offset, int val)
147 uniphier_gpio_offset_write(chip, offset, UNIPHIER_GPIO_PORT_DATA, val);
150 static void uniphier_gpio_set_multiple(struct gpio_chip *chip,
151 unsigned long *mask, unsigned long *bits)
153 unsigned int bank, shift, bank_mask, bank_bits;
156 for (i = 0; i < chip->ngpio; i += UNIPHIER_GPIO_LINES_PER_BANK) {
157 bank = i / UNIPHIER_GPIO_LINES_PER_BANK;
158 shift = i % BITS_PER_LONG;
159 bank_mask = (mask[BIT_WORD(i)] >> shift) &
160 UNIPHIER_GPIO_BANK_MASK;
161 bank_bits = bits[BIT_WORD(i)] >> shift;
163 uniphier_gpio_bank_write(chip, bank, UNIPHIER_GPIO_PORT_DATA,
164 bank_mask, bank_bits);
168 static int uniphier_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
170 struct irq_fwspec fwspec;
172 if (offset < UNIPHIER_GPIO_IRQ_OFFSET)
175 fwspec.fwnode = of_node_to_fwnode(chip->parent->of_node);
176 fwspec.param_count = 2;
177 fwspec.param[0] = offset - UNIPHIER_GPIO_IRQ_OFFSET;
179 * IRQ_TYPE_NONE is rejected by the parent irq domain. Set LEVEL_HIGH
180 * temporarily. Anyway, ->irq_set_type() will override it later.
182 fwspec.param[1] = IRQ_TYPE_LEVEL_HIGH;
184 return irq_create_fwspec_mapping(&fwspec);
187 static void uniphier_gpio_irq_mask(struct irq_data *data)
189 struct uniphier_gpio_priv *priv = data->chip_data;
190 u32 mask = BIT(data->hwirq);
192 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, 0);
194 return irq_chip_mask_parent(data);
197 static void uniphier_gpio_irq_unmask(struct irq_data *data)
199 struct uniphier_gpio_priv *priv = data->chip_data;
200 u32 mask = BIT(data->hwirq);
202 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_EN, mask, mask);
204 return irq_chip_unmask_parent(data);
207 static int uniphier_gpio_irq_set_type(struct irq_data *data, unsigned int type)
209 struct uniphier_gpio_priv *priv = data->chip_data;
210 u32 mask = BIT(data->hwirq);
213 if (type == IRQ_TYPE_EDGE_BOTH) {
215 type = IRQ_TYPE_EDGE_FALLING;
218 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_MODE, mask, val);
219 /* To enable both edge detection, the noise filter must be enabled. */
220 uniphier_gpio_reg_update(priv, UNIPHIER_GPIO_IRQ_FLT_EN, mask, val);
222 return irq_chip_set_type_parent(data, type);
225 static int uniphier_gpio_irq_get_parent_hwirq(struct uniphier_gpio_priv *priv,
228 struct device_node *np = priv->chip.parent->of_node;
230 u32 base, parent_base, size;
233 range = of_get_property(np, "socionext,interrupt-ranges", &len);
237 len /= sizeof(*range);
239 for (; len >= 3; len -= 3) {
240 base = be32_to_cpu(*range++);
241 parent_base = be32_to_cpu(*range++);
242 size = be32_to_cpu(*range++);
244 if (base <= hwirq && hwirq < base + size)
245 return hwirq - base + parent_base;
251 static int uniphier_gpio_irq_domain_translate(struct irq_domain *domain,
252 struct irq_fwspec *fwspec,
253 unsigned long *out_hwirq,
254 unsigned int *out_type)
256 if (WARN_ON(fwspec->param_count < 2))
259 *out_hwirq = fwspec->param[0];
260 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
265 static int uniphier_gpio_irq_domain_alloc(struct irq_domain *domain,
267 unsigned int nr_irqs, void *arg)
269 struct uniphier_gpio_priv *priv = domain->host_data;
270 struct irq_fwspec parent_fwspec;
271 irq_hw_number_t hwirq;
275 if (WARN_ON(nr_irqs != 1))
278 ret = uniphier_gpio_irq_domain_translate(domain, arg, &hwirq, &type);
282 ret = uniphier_gpio_irq_get_parent_hwirq(priv, hwirq);
286 /* parent is UniPhier AIDET */
287 parent_fwspec.fwnode = domain->parent->fwnode;
288 parent_fwspec.param_count = 2;
289 parent_fwspec.param[0] = ret;
290 parent_fwspec.param[1] = (type == IRQ_TYPE_EDGE_BOTH) ?
291 IRQ_TYPE_EDGE_FALLING : type;
293 ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
294 &priv->irq_chip, priv);
298 return irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
301 static int uniphier_gpio_irq_domain_activate(struct irq_domain *domain,
302 struct irq_data *data, bool early)
304 struct uniphier_gpio_priv *priv = domain->host_data;
305 struct gpio_chip *chip = &priv->chip;
307 return gpiochip_lock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
310 static void uniphier_gpio_irq_domain_deactivate(struct irq_domain *domain,
311 struct irq_data *data)
313 struct uniphier_gpio_priv *priv = domain->host_data;
314 struct gpio_chip *chip = &priv->chip;
316 gpiochip_unlock_as_irq(chip, data->hwirq + UNIPHIER_GPIO_IRQ_OFFSET);
319 static const struct irq_domain_ops uniphier_gpio_irq_domain_ops = {
320 .alloc = uniphier_gpio_irq_domain_alloc,
321 .free = irq_domain_free_irqs_common,
322 .activate = uniphier_gpio_irq_domain_activate,
323 .deactivate = uniphier_gpio_irq_domain_deactivate,
324 .translate = uniphier_gpio_irq_domain_translate,
327 static void uniphier_gpio_hw_init(struct uniphier_gpio_priv *priv)
330 * Due to the hardware design, the noise filter must be enabled to
331 * detect both edge interrupts. This filter is intended to remove the
332 * noise from the irq lines. It does not work for GPIO input, so GPIO
333 * debounce is not supported. Unfortunately, the filter period is
334 * shared among all irq lines. Just choose a sensible period here.
336 writel(0xff, priv->regs + UNIPHIER_GPIO_IRQ_FLT_CYC);
339 static unsigned int uniphier_gpio_get_nbanks(unsigned int ngpio)
341 return DIV_ROUND_UP(ngpio, UNIPHIER_GPIO_LINES_PER_BANK);
344 static int uniphier_gpio_probe(struct platform_device *pdev)
346 struct device *dev = &pdev->dev;
347 struct device_node *parent_np;
348 struct irq_domain *parent_domain;
349 struct uniphier_gpio_priv *priv;
350 struct gpio_chip *chip;
351 struct irq_chip *irq_chip;
356 parent_np = of_irq_find_parent(dev->of_node);
360 parent_domain = irq_find_host(parent_np);
361 of_node_put(parent_np);
363 return -EPROBE_DEFER;
365 ret = of_property_read_u32(dev->of_node, "ngpios", &ngpios);
369 nregs = uniphier_gpio_get_nbanks(ngpios) * 2 + 3;
370 priv = devm_kzalloc(dev, struct_size(priv, saved_vals, nregs),
375 priv->regs = devm_platform_ioremap_resource(pdev, 0);
376 if (IS_ERR(priv->regs))
377 return PTR_ERR(priv->regs);
379 spin_lock_init(&priv->lock);
382 chip->label = dev_name(dev);
384 chip->request = gpiochip_generic_request;
385 chip->free = gpiochip_generic_free;
386 chip->get_direction = uniphier_gpio_get_direction;
387 chip->direction_input = uniphier_gpio_direction_input;
388 chip->direction_output = uniphier_gpio_direction_output;
389 chip->get = uniphier_gpio_get;
390 chip->set = uniphier_gpio_set;
391 chip->set_multiple = uniphier_gpio_set_multiple;
392 chip->to_irq = uniphier_gpio_to_irq;
394 chip->ngpio = ngpios;
396 irq_chip = &priv->irq_chip;
397 irq_chip->name = dev_name(dev);
398 irq_chip->irq_mask = uniphier_gpio_irq_mask;
399 irq_chip->irq_unmask = uniphier_gpio_irq_unmask;
400 irq_chip->irq_eoi = irq_chip_eoi_parent;
401 irq_chip->irq_set_affinity = irq_chip_set_affinity_parent;
402 irq_chip->irq_set_type = uniphier_gpio_irq_set_type;
404 uniphier_gpio_hw_init(priv);
406 ret = devm_gpiochip_add_data(dev, chip, priv);
410 priv->domain = irq_domain_create_hierarchy(
412 UNIPHIER_GPIO_IRQ_MAX_NUM,
413 of_node_to_fwnode(dev->of_node),
414 &uniphier_gpio_irq_domain_ops, priv);
418 platform_set_drvdata(pdev, priv);
423 static int uniphier_gpio_remove(struct platform_device *pdev)
425 struct uniphier_gpio_priv *priv = platform_get_drvdata(pdev);
427 irq_domain_remove(priv->domain);
432 static int __maybe_unused uniphier_gpio_suspend(struct device *dev)
434 struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
435 unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
436 u32 *val = priv->saved_vals;
440 for (i = 0; i < nbanks; i++) {
441 reg = uniphier_gpio_bank_to_reg(i);
443 *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
444 *val++ = readl(priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
447 *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_EN);
448 *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_MODE);
449 *val++ = readl(priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
454 static int __maybe_unused uniphier_gpio_resume(struct device *dev)
456 struct uniphier_gpio_priv *priv = dev_get_drvdata(dev);
457 unsigned int nbanks = uniphier_gpio_get_nbanks(priv->chip.ngpio);
458 const u32 *val = priv->saved_vals;
462 for (i = 0; i < nbanks; i++) {
463 reg = uniphier_gpio_bank_to_reg(i);
465 writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DATA);
466 writel(*val++, priv->regs + reg + UNIPHIER_GPIO_PORT_DIR);
469 writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_EN);
470 writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_MODE);
471 writel(*val++, priv->regs + UNIPHIER_GPIO_IRQ_FLT_EN);
473 uniphier_gpio_hw_init(priv);
478 static const struct dev_pm_ops uniphier_gpio_pm_ops = {
479 SET_LATE_SYSTEM_SLEEP_PM_OPS(uniphier_gpio_suspend,
480 uniphier_gpio_resume)
483 static const struct of_device_id uniphier_gpio_match[] = {
484 { .compatible = "socionext,uniphier-gpio" },
487 MODULE_DEVICE_TABLE(of, uniphier_gpio_match);
489 static struct platform_driver uniphier_gpio_driver = {
490 .probe = uniphier_gpio_probe,
491 .remove = uniphier_gpio_remove,
493 .name = "uniphier-gpio",
494 .of_match_table = uniphier_gpio_match,
495 .pm = &uniphier_gpio_pm_ops,
498 module_platform_driver(uniphier_gpio_driver);
500 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
501 MODULE_DESCRIPTION("UniPhier GPIO driver");
502 MODULE_LICENSE("GPL v2");