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