1b7536d87SMichael Walle // SPDX-License-Identifier: GPL-2.0-only
2b7536d87SMichael Walle /*
3b7536d87SMichael Walle * sl28cpld GPIO driver
4b7536d87SMichael Walle *
5b7536d87SMichael Walle * Copyright 2020 Michael Walle <michael@walle.cc>
6b7536d87SMichael Walle */
7b7536d87SMichael Walle
8b7536d87SMichael Walle #include <linux/device.h>
9b7536d87SMichael Walle #include <linux/gpio/driver.h>
10b7536d87SMichael Walle #include <linux/gpio/regmap.h>
11b7536d87SMichael Walle #include <linux/interrupt.h>
12b7536d87SMichael Walle #include <linux/kernel.h>
13b7536d87SMichael Walle #include <linux/mod_devicetable.h>
14b7536d87SMichael Walle #include <linux/module.h>
15b7536d87SMichael Walle #include <linux/platform_device.h>
16b7536d87SMichael Walle #include <linux/regmap.h>
17b7536d87SMichael Walle
18b7536d87SMichael Walle /* GPIO flavor */
19b7536d87SMichael Walle #define GPIO_REG_DIR 0x00
20b7536d87SMichael Walle #define GPIO_REG_OUT 0x01
21b7536d87SMichael Walle #define GPIO_REG_IN 0x02
22b7536d87SMichael Walle #define GPIO_REG_IE 0x03
23b7536d87SMichael Walle #define GPIO_REG_IP 0x04
24b7536d87SMichael Walle
25b7536d87SMichael Walle /* input-only flavor */
26b7536d87SMichael Walle #define GPI_REG_IN 0x00
27b7536d87SMichael Walle
28b7536d87SMichael Walle /* output-only flavor */
29b7536d87SMichael Walle #define GPO_REG_OUT 0x00
30b7536d87SMichael Walle
31b7536d87SMichael Walle enum sl28cpld_gpio_type {
32b7536d87SMichael Walle SL28CPLD_GPIO = 1,
33b7536d87SMichael Walle SL28CPLD_GPI,
34b7536d87SMichael Walle SL28CPLD_GPO,
35b7536d87SMichael Walle };
36b7536d87SMichael Walle
37b7536d87SMichael Walle static const struct regmap_irq sl28cpld_gpio_irqs[] = {
38b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(0, 8),
39b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(1, 8),
40b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(2, 8),
41b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(3, 8),
42b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(4, 8),
43b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(5, 8),
44b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(6, 8),
45b7536d87SMichael Walle REGMAP_IRQ_REG_LINE(7, 8),
46b7536d87SMichael Walle };
47b7536d87SMichael Walle
sl28cpld_gpio_irq_init(struct platform_device * pdev,unsigned int base,struct gpio_regmap_config * config)48b7536d87SMichael Walle static int sl28cpld_gpio_irq_init(struct platform_device *pdev,
49b7536d87SMichael Walle unsigned int base,
50b7536d87SMichael Walle struct gpio_regmap_config *config)
51b7536d87SMichael Walle {
52b7536d87SMichael Walle struct regmap_irq_chip_data *irq_data;
53b7536d87SMichael Walle struct regmap_irq_chip *irq_chip;
54b7536d87SMichael Walle struct device *dev = &pdev->dev;
55b7536d87SMichael Walle int irq, ret;
56b7536d87SMichael Walle
57b7536d87SMichael Walle if (!device_property_read_bool(dev, "interrupt-controller"))
58b7536d87SMichael Walle return 0;
59b7536d87SMichael Walle
60b7536d87SMichael Walle irq = platform_get_irq(pdev, 0);
61b7536d87SMichael Walle if (irq < 0)
62b7536d87SMichael Walle return irq;
63b7536d87SMichael Walle
64b7536d87SMichael Walle irq_chip = devm_kzalloc(dev, sizeof(*irq_chip), GFP_KERNEL);
65b7536d87SMichael Walle if (!irq_chip)
66b7536d87SMichael Walle return -ENOMEM;
67b7536d87SMichael Walle
68aab0508eSZheng Yongjun irq_chip->name = "sl28cpld-gpio-irq";
69b7536d87SMichael Walle irq_chip->irqs = sl28cpld_gpio_irqs;
70b7536d87SMichael Walle irq_chip->num_irqs = ARRAY_SIZE(sl28cpld_gpio_irqs);
71b7536d87SMichael Walle irq_chip->num_regs = 1;
72b7536d87SMichael Walle irq_chip->status_base = base + GPIO_REG_IP;
73*739be9b6SAidan MacDonald irq_chip->unmask_base = base + GPIO_REG_IE;
74b7536d87SMichael Walle irq_chip->ack_base = base + GPIO_REG_IP;
75b7536d87SMichael Walle
76b7536d87SMichael Walle ret = devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(dev),
77b7536d87SMichael Walle config->regmap, irq,
78b7536d87SMichael Walle IRQF_SHARED | IRQF_ONESHOT,
79b7536d87SMichael Walle 0, irq_chip, &irq_data);
80b7536d87SMichael Walle if (ret)
81b7536d87SMichael Walle return ret;
82b7536d87SMichael Walle
83b7536d87SMichael Walle config->irq_domain = regmap_irq_get_domain(irq_data);
84b7536d87SMichael Walle
85b7536d87SMichael Walle return 0;
86b7536d87SMichael Walle }
87b7536d87SMichael Walle
sl28cpld_gpio_probe(struct platform_device * pdev)88b7536d87SMichael Walle static int sl28cpld_gpio_probe(struct platform_device *pdev)
89b7536d87SMichael Walle {
90b7536d87SMichael Walle struct gpio_regmap_config config = {0};
91b7536d87SMichael Walle enum sl28cpld_gpio_type type;
92b7536d87SMichael Walle struct regmap *regmap;
93b7536d87SMichael Walle u32 base;
94b7536d87SMichael Walle int ret;
95b7536d87SMichael Walle
96b7536d87SMichael Walle if (!pdev->dev.parent)
97b7536d87SMichael Walle return -ENODEV;
98b7536d87SMichael Walle
99b7536d87SMichael Walle type = (uintptr_t)device_get_match_data(&pdev->dev);
100b7536d87SMichael Walle if (!type)
101b7536d87SMichael Walle return -ENODEV;
102b7536d87SMichael Walle
103b7536d87SMichael Walle ret = device_property_read_u32(&pdev->dev, "reg", &base);
104b7536d87SMichael Walle if (ret)
105b7536d87SMichael Walle return -EINVAL;
106b7536d87SMichael Walle
107b7536d87SMichael Walle regmap = dev_get_regmap(pdev->dev.parent, NULL);
108b7536d87SMichael Walle if (!regmap)
109b7536d87SMichael Walle return -ENODEV;
110b7536d87SMichael Walle
111b7536d87SMichael Walle config.regmap = regmap;
112b7536d87SMichael Walle config.parent = &pdev->dev;
113b7536d87SMichael Walle config.ngpio = 8;
114b7536d87SMichael Walle
115b7536d87SMichael Walle switch (type) {
116b7536d87SMichael Walle case SL28CPLD_GPIO:
117b7536d87SMichael Walle config.reg_dat_base = base + GPIO_REG_IN;
118b7536d87SMichael Walle config.reg_set_base = base + GPIO_REG_OUT;
119b7536d87SMichael Walle /* reg_dir_out_base might be zero */
120b7536d87SMichael Walle config.reg_dir_out_base = GPIO_REGMAP_ADDR(base + GPIO_REG_DIR);
121b7536d87SMichael Walle
122b7536d87SMichael Walle /* This type supports interrupts */
123b7536d87SMichael Walle ret = sl28cpld_gpio_irq_init(pdev, base, &config);
124b7536d87SMichael Walle if (ret)
125b7536d87SMichael Walle return ret;
126b7536d87SMichael Walle break;
127b7536d87SMichael Walle case SL28CPLD_GPO:
128b7536d87SMichael Walle config.reg_set_base = base + GPO_REG_OUT;
129b7536d87SMichael Walle break;
130b7536d87SMichael Walle case SL28CPLD_GPI:
131b7536d87SMichael Walle config.reg_dat_base = base + GPI_REG_IN;
132b7536d87SMichael Walle break;
133b7536d87SMichael Walle default:
134b7536d87SMichael Walle dev_err(&pdev->dev, "unknown type %d\n", type);
135b7536d87SMichael Walle return -ENODEV;
136b7536d87SMichael Walle }
137b7536d87SMichael Walle
138b7536d87SMichael Walle return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(&pdev->dev, &config));
139b7536d87SMichael Walle }
140b7536d87SMichael Walle
141b7536d87SMichael Walle static const struct of_device_id sl28cpld_gpio_of_match[] = {
142b7536d87SMichael Walle { .compatible = "kontron,sl28cpld-gpio", .data = (void *)SL28CPLD_GPIO },
143b7536d87SMichael Walle { .compatible = "kontron,sl28cpld-gpi", .data = (void *)SL28CPLD_GPI },
144b7536d87SMichael Walle { .compatible = "kontron,sl28cpld-gpo", .data = (void *)SL28CPLD_GPO },
145b7536d87SMichael Walle {}
146b7536d87SMichael Walle };
147b7536d87SMichael Walle MODULE_DEVICE_TABLE(of, sl28cpld_gpio_of_match);
148b7536d87SMichael Walle
149b7536d87SMichael Walle static struct platform_driver sl28cpld_gpio_driver = {
150b7536d87SMichael Walle .probe = sl28cpld_gpio_probe,
151b7536d87SMichael Walle .driver = {
152b7536d87SMichael Walle .name = "sl28cpld-gpio",
153b7536d87SMichael Walle .of_match_table = sl28cpld_gpio_of_match,
154b7536d87SMichael Walle },
155b7536d87SMichael Walle };
156b7536d87SMichael Walle module_platform_driver(sl28cpld_gpio_driver);
157b7536d87SMichael Walle
158b7536d87SMichael Walle MODULE_DESCRIPTION("sl28cpld GPIO Driver");
159b7536d87SMichael Walle MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
160b7536d87SMichael Walle MODULE_LICENSE("GPL");
161