xref: /linux/drivers/gpio/gpio-tb10x.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Abilis Systems MODULE DESCRIPTION
3  *
4  * Copyright (C) Abilis Systems 2013
5  *
6  * Authors: Sascha Leuenberger <sascha.leuenberger@abilis.com>
7  *          Christian Ruppert <christian.ruppert@abilis.com>
8  */
9 
10 #include <linux/bitops.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/gpio/generic.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_platform.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 
25 #define TB10X_GPIO_DIR_IN	(0x00000000)
26 #define TB10X_GPIO_DIR_OUT	(0x00000001)
27 #define OFFSET_TO_REG_DDR	(0x00)
28 #define OFFSET_TO_REG_DATA	(0x04)
29 #define OFFSET_TO_REG_INT_EN	(0x08)
30 #define OFFSET_TO_REG_CHANGE	(0x0C)
31 #define OFFSET_TO_REG_WRMASK	(0x10)
32 #define OFFSET_TO_REG_INT_TYPE	(0x14)
33 
34 
35 /**
36  * struct tb10x_gpio - TB10x GPIO controller structure
37  * @base: register base address
38  * @domain: IRQ domain of GPIO generated interrupts managed by this controller
39  * @irq: Interrupt line of parent interrupt controller
40  * @chip: Generic GPIO chip structure associated with this GPIO controller
41  */
42 struct tb10x_gpio {
43 	void __iomem *base;
44 	struct irq_domain *domain;
45 	int irq;
46 	struct gpio_generic_chip chip;
47 };
48 
49 static inline u32 tb10x_reg_read(struct tb10x_gpio *gpio, unsigned int offs)
50 {
51 	return ioread32(gpio->base + offs);
52 }
53 
54 static int tb10x_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
55 {
56 	struct tb10x_gpio *tb10x_gpio = gpiochip_get_data(chip);
57 
58 	return irq_create_mapping(tb10x_gpio->domain, offset);
59 }
60 
61 static int tb10x_gpio_irq_set_type(struct irq_data *data, unsigned int type)
62 {
63 	if ((type & IRQF_TRIGGER_MASK) != IRQ_TYPE_EDGE_BOTH) {
64 		pr_err("Only (both) edge triggered interrupts supported.\n");
65 		return -EINVAL;
66 	}
67 
68 	irqd_set_trigger_type(data, type);
69 
70 	return IRQ_SET_MASK_OK;
71 }
72 
73 static irqreturn_t tb10x_gpio_irq_cascade(int irq, void *data)
74 {
75 	struct tb10x_gpio *tb10x_gpio = data;
76 	u32 r = tb10x_reg_read(tb10x_gpio, OFFSET_TO_REG_CHANGE);
77 	u32 m = tb10x_reg_read(tb10x_gpio, OFFSET_TO_REG_INT_EN);
78 	const unsigned long bits = r & m;
79 	int i;
80 
81 	for_each_set_bit(i, &bits, 32)
82 		generic_handle_domain_irq(tb10x_gpio->domain, i);
83 
84 	return IRQ_HANDLED;
85 }
86 
87 static int tb10x_gpio_probe(struct platform_device *pdev)
88 {
89 	struct gpio_generic_chip_config config;
90 	struct tb10x_gpio *tb10x_gpio;
91 	struct device *dev = &pdev->dev;
92 	struct device_node *np = dev->of_node;
93 	int ret = -EBUSY;
94 	u32 ngpio;
95 
96 	if (!np)
97 		return -EINVAL;
98 
99 	if (of_property_read_u32(np, "abilis,ngpio", &ngpio))
100 		return -EINVAL;
101 
102 	tb10x_gpio = devm_kzalloc(dev, sizeof(*tb10x_gpio), GFP_KERNEL);
103 	if (tb10x_gpio == NULL)
104 		return -ENOMEM;
105 
106 	tb10x_gpio->base = devm_platform_ioremap_resource(pdev, 0);
107 	if (IS_ERR(tb10x_gpio->base))
108 		return PTR_ERR(tb10x_gpio->base);
109 
110 	tb10x_gpio->chip.gc.label =
111 		devm_kasprintf(dev, GFP_KERNEL, "%pOF", pdev->dev.of_node);
112 	if (!tb10x_gpio->chip.gc.label)
113 		return -ENOMEM;
114 
115 	/*
116 	 * Initialize generic GPIO with one single register for reading and setting
117 	 * the lines, no special set or clear registers and a data direction register
118 	 * wher 1 means "output".
119 	 */
120 	config = (struct gpio_generic_chip_config) {
121 		.dev = dev,
122 		.sz = 4,
123 		.dat = tb10x_gpio->base + OFFSET_TO_REG_DATA,
124 		.dirout = tb10x_gpio->base + OFFSET_TO_REG_DDR,
125 	};
126 
127 	ret = gpio_generic_chip_init(&tb10x_gpio->chip, &config);
128 	if (ret) {
129 		dev_err(dev, "unable to init generic GPIO\n");
130 		return ret;
131 	}
132 	tb10x_gpio->chip.gc.base = -1;
133 	tb10x_gpio->chip.gc.parent = dev;
134 	tb10x_gpio->chip.gc.owner = THIS_MODULE;
135 	/*
136 	 * ngpio is set by gpio_generic_chip_init() but we override it, this
137 	 * .request() callback also overrides the one set up by generic GPIO.
138 	 */
139 	tb10x_gpio->chip.gc.ngpio = ngpio;
140 	tb10x_gpio->chip.gc.request = gpiochip_generic_request;
141 	tb10x_gpio->chip.gc.free = gpiochip_generic_free;
142 
143 	ret = devm_gpiochip_add_data(dev, &tb10x_gpio->chip.gc, tb10x_gpio);
144 	if (ret < 0) {
145 		dev_err(dev, "Could not add gpiochip.\n");
146 		return ret;
147 	}
148 
149 	platform_set_drvdata(pdev, tb10x_gpio);
150 
151 	if (of_property_read_bool(np, "interrupt-controller")) {
152 		struct irq_chip_generic *gc;
153 
154 		ret = platform_get_irq(pdev, 0);
155 		if (ret < 0)
156 			return ret;
157 
158 		tb10x_gpio->chip.gc.to_irq = tb10x_gpio_to_irq;
159 		tb10x_gpio->irq		= ret;
160 
161 		ret = devm_request_irq(dev, ret, tb10x_gpio_irq_cascade,
162 				IRQF_TRIGGER_NONE | IRQF_SHARED,
163 				dev_name(dev), tb10x_gpio);
164 		if (ret != 0)
165 			return ret;
166 
167 		tb10x_gpio->domain = irq_domain_create_linear(dev_fwnode(dev),
168 							      tb10x_gpio->chip.gc.ngpio,
169 							      &irq_generic_chip_ops, NULL);
170 		if (!tb10x_gpio->domain)
171 			return -ENOMEM;
172 
173 		ret = irq_alloc_domain_generic_chips(tb10x_gpio->domain,
174 				tb10x_gpio->chip.gc.ngpio, 1, tb10x_gpio->chip.gc.label,
175 				handle_edge_irq, IRQ_NOREQUEST, IRQ_NOPROBE,
176 				IRQ_GC_INIT_MASK_CACHE);
177 		if (ret)
178 			goto err_remove_domain;
179 
180 		gc = tb10x_gpio->domain->gc->gc[0];
181 		gc->reg_base                         = tb10x_gpio->base;
182 		gc->chip_types[0].type               = IRQ_TYPE_EDGE_BOTH;
183 		gc->chip_types[0].chip.irq_ack       = irq_gc_ack_set_bit;
184 		gc->chip_types[0].chip.irq_mask      = irq_gc_mask_clr_bit;
185 		gc->chip_types[0].chip.irq_unmask    = irq_gc_mask_set_bit;
186 		gc->chip_types[0].chip.irq_set_type  = tb10x_gpio_irq_set_type;
187 		gc->chip_types[0].regs.ack           = OFFSET_TO_REG_CHANGE;
188 		gc->chip_types[0].regs.mask          = OFFSET_TO_REG_INT_EN;
189 	}
190 
191 	return 0;
192 
193 err_remove_domain:
194 	irq_domain_remove(tb10x_gpio->domain);
195 	return ret;
196 }
197 
198 static void tb10x_gpio_remove(struct platform_device *pdev)
199 {
200 	struct tb10x_gpio *tb10x_gpio = platform_get_drvdata(pdev);
201 
202 	if (tb10x_gpio->chip.gc.to_irq) {
203 		irq_remove_generic_chip(tb10x_gpio->domain->gc->gc[0],
204 					BIT(tb10x_gpio->chip.gc.ngpio) - 1, 0, 0);
205 		kfree(tb10x_gpio->domain->gc);
206 		irq_domain_remove(tb10x_gpio->domain);
207 	}
208 }
209 
210 static const struct of_device_id tb10x_gpio_dt_ids[] = {
211 	{ .compatible = "abilis,tb10x-gpio" },
212 	{ }
213 };
214 MODULE_DEVICE_TABLE(of, tb10x_gpio_dt_ids);
215 
216 static struct platform_driver tb10x_gpio_driver = {
217 	.probe		= tb10x_gpio_probe,
218 	.remove		= tb10x_gpio_remove,
219 	.driver = {
220 		.name	= "tb10x-gpio",
221 		.of_match_table = tb10x_gpio_dt_ids,
222 	}
223 };
224 
225 module_platform_driver(tb10x_gpio_driver);
226 MODULE_LICENSE("GPL");
227 MODULE_DESCRIPTION("tb10x gpio.");
228