xref: /openbmc/linux/drivers/soc/fsl/qe/gpio.c (revision ecd25094)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * QUICC Engine GPIOs
4  *
5  * Copyright (c) MontaVista Software, Inc. 2008.
6  *
7  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/err.h>
14 #include <linux/io.h>
15 #include <linux/of.h>
16 #include <linux/of_gpio.h>
17 #include <linux/gpio/driver.h>
18 /* FIXME: needed for gpio_to_chip() get rid of this */
19 #include <linux/gpio.h>
20 #include <linux/slab.h>
21 #include <linux/export.h>
22 #include <soc/fsl/qe/qe.h>
23 
24 struct qe_gpio_chip {
25 	struct of_mm_gpio_chip mm_gc;
26 	spinlock_t lock;
27 
28 	unsigned long pin_flags[QE_PIO_PINS];
29 #define QE_PIN_REQUESTED 0
30 
31 	/* shadowed data register to clear/set bits safely */
32 	u32 cpdata;
33 
34 	/* saved_regs used to restore dedicated functions */
35 	struct qe_pio_regs saved_regs;
36 };
37 
38 static void qe_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
39 {
40 	struct qe_gpio_chip *qe_gc =
41 		container_of(mm_gc, struct qe_gpio_chip, mm_gc);
42 	struct qe_pio_regs __iomem *regs = mm_gc->regs;
43 
44 	qe_gc->cpdata = in_be32(&regs->cpdata);
45 	qe_gc->saved_regs.cpdata = qe_gc->cpdata;
46 	qe_gc->saved_regs.cpdir1 = in_be32(&regs->cpdir1);
47 	qe_gc->saved_regs.cpdir2 = in_be32(&regs->cpdir2);
48 	qe_gc->saved_regs.cppar1 = in_be32(&regs->cppar1);
49 	qe_gc->saved_regs.cppar2 = in_be32(&regs->cppar2);
50 	qe_gc->saved_regs.cpodr = in_be32(&regs->cpodr);
51 }
52 
53 static int qe_gpio_get(struct gpio_chip *gc, unsigned int gpio)
54 {
55 	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
56 	struct qe_pio_regs __iomem *regs = mm_gc->regs;
57 	u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
58 
59 	return !!(in_be32(&regs->cpdata) & pin_mask);
60 }
61 
62 static void qe_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
63 {
64 	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
65 	struct qe_gpio_chip *qe_gc = gpiochip_get_data(gc);
66 	struct qe_pio_regs __iomem *regs = mm_gc->regs;
67 	unsigned long flags;
68 	u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
69 
70 	spin_lock_irqsave(&qe_gc->lock, flags);
71 
72 	if (val)
73 		qe_gc->cpdata |= pin_mask;
74 	else
75 		qe_gc->cpdata &= ~pin_mask;
76 
77 	out_be32(&regs->cpdata, qe_gc->cpdata);
78 
79 	spin_unlock_irqrestore(&qe_gc->lock, flags);
80 }
81 
82 static void qe_gpio_set_multiple(struct gpio_chip *gc,
83 				 unsigned long *mask, unsigned long *bits)
84 {
85 	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
86 	struct qe_gpio_chip *qe_gc = gpiochip_get_data(gc);
87 	struct qe_pio_regs __iomem *regs = mm_gc->regs;
88 	unsigned long flags;
89 	int i;
90 
91 	spin_lock_irqsave(&qe_gc->lock, flags);
92 
93 	for (i = 0; i < gc->ngpio; i++) {
94 		if (*mask == 0)
95 			break;
96 		if (__test_and_clear_bit(i, mask)) {
97 			if (test_bit(i, bits))
98 				qe_gc->cpdata |= (1U << (QE_PIO_PINS - 1 - i));
99 			else
100 				qe_gc->cpdata &= ~(1U << (QE_PIO_PINS - 1 - i));
101 		}
102 	}
103 
104 	out_be32(&regs->cpdata, qe_gc->cpdata);
105 
106 	spin_unlock_irqrestore(&qe_gc->lock, flags);
107 }
108 
109 static int qe_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
110 {
111 	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
112 	struct qe_gpio_chip *qe_gc = gpiochip_get_data(gc);
113 	unsigned long flags;
114 
115 	spin_lock_irqsave(&qe_gc->lock, flags);
116 
117 	__par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_IN, 0, 0, 0);
118 
119 	spin_unlock_irqrestore(&qe_gc->lock, flags);
120 
121 	return 0;
122 }
123 
124 static int qe_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
125 {
126 	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
127 	struct qe_gpio_chip *qe_gc = gpiochip_get_data(gc);
128 	unsigned long flags;
129 
130 	qe_gpio_set(gc, gpio, val);
131 
132 	spin_lock_irqsave(&qe_gc->lock, flags);
133 
134 	__par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_OUT, 0, 0, 0);
135 
136 	spin_unlock_irqrestore(&qe_gc->lock, flags);
137 
138 	return 0;
139 }
140 
141 struct qe_pin {
142 	/*
143 	 * The qe_gpio_chip name is unfortunate, we should change that to
144 	 * something like qe_pio_controller. Someday.
145 	 */
146 	struct qe_gpio_chip *controller;
147 	int num;
148 };
149 
150 /**
151  * qe_pin_request - Request a QE pin
152  * @np:		device node to get a pin from
153  * @index:	index of a pin in the device tree
154  * Context:	non-atomic
155  *
156  * This function return qe_pin so that you could use it with the rest of
157  * the QE Pin Multiplexing API.
158  */
159 struct qe_pin *qe_pin_request(struct device_node *np, int index)
160 {
161 	struct qe_pin *qe_pin;
162 	struct gpio_chip *gc;
163 	struct of_mm_gpio_chip *mm_gc;
164 	struct qe_gpio_chip *qe_gc;
165 	int err;
166 	unsigned long flags;
167 
168 	qe_pin = kzalloc(sizeof(*qe_pin), GFP_KERNEL);
169 	if (!qe_pin) {
170 		pr_debug("%s: can't allocate memory\n", __func__);
171 		return ERR_PTR(-ENOMEM);
172 	}
173 
174 	err = of_get_gpio(np, index);
175 	if (err < 0)
176 		goto err0;
177 	gc = gpio_to_chip(err);
178 	if (WARN_ON(!gc)) {
179 		err = -ENODEV;
180 		goto err0;
181 	}
182 
183 	if (!of_device_is_compatible(gc->of_node, "fsl,mpc8323-qe-pario-bank")) {
184 		pr_debug("%s: tried to get a non-qe pin\n", __func__);
185 		err = -EINVAL;
186 		goto err0;
187 	}
188 
189 	mm_gc = to_of_mm_gpio_chip(gc);
190 	qe_gc = gpiochip_get_data(gc);
191 
192 	spin_lock_irqsave(&qe_gc->lock, flags);
193 
194 	err -= gc->base;
195 	if (test_and_set_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[err]) == 0) {
196 		qe_pin->controller = qe_gc;
197 		qe_pin->num = err;
198 		err = 0;
199 	} else {
200 		err = -EBUSY;
201 	}
202 
203 	spin_unlock_irqrestore(&qe_gc->lock, flags);
204 
205 	if (!err)
206 		return qe_pin;
207 err0:
208 	kfree(qe_pin);
209 	pr_debug("%s failed with status %d\n", __func__, err);
210 	return ERR_PTR(err);
211 }
212 EXPORT_SYMBOL(qe_pin_request);
213 
214 /**
215  * qe_pin_free - Free a pin
216  * @qe_pin:	pointer to the qe_pin structure
217  * Context:	any
218  *
219  * This function frees the qe_pin structure and makes a pin available
220  * for further qe_pin_request() calls.
221  */
222 void qe_pin_free(struct qe_pin *qe_pin)
223 {
224 	struct qe_gpio_chip *qe_gc = qe_pin->controller;
225 	unsigned long flags;
226 	const int pin = qe_pin->num;
227 
228 	spin_lock_irqsave(&qe_gc->lock, flags);
229 	test_and_clear_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[pin]);
230 	spin_unlock_irqrestore(&qe_gc->lock, flags);
231 
232 	kfree(qe_pin);
233 }
234 EXPORT_SYMBOL(qe_pin_free);
235 
236 /**
237  * qe_pin_set_dedicated - Revert a pin to a dedicated peripheral function mode
238  * @qe_pin:	pointer to the qe_pin structure
239  * Context:	any
240  *
241  * This function resets a pin to a dedicated peripheral function that
242  * has been set up by the firmware.
243  */
244 void qe_pin_set_dedicated(struct qe_pin *qe_pin)
245 {
246 	struct qe_gpio_chip *qe_gc = qe_pin->controller;
247 	struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
248 	struct qe_pio_regs *sregs = &qe_gc->saved_regs;
249 	int pin = qe_pin->num;
250 	u32 mask1 = 1 << (QE_PIO_PINS - (pin + 1));
251 	u32 mask2 = 0x3 << (QE_PIO_PINS - (pin % (QE_PIO_PINS / 2) + 1) * 2);
252 	bool second_reg = pin > (QE_PIO_PINS / 2) - 1;
253 	unsigned long flags;
254 
255 	spin_lock_irqsave(&qe_gc->lock, flags);
256 
257 	if (second_reg) {
258 		clrsetbits_be32(&regs->cpdir2, mask2, sregs->cpdir2 & mask2);
259 		clrsetbits_be32(&regs->cppar2, mask2, sregs->cppar2 & mask2);
260 	} else {
261 		clrsetbits_be32(&regs->cpdir1, mask2, sregs->cpdir1 & mask2);
262 		clrsetbits_be32(&regs->cppar1, mask2, sregs->cppar1 & mask2);
263 	}
264 
265 	if (sregs->cpdata & mask1)
266 		qe_gc->cpdata |= mask1;
267 	else
268 		qe_gc->cpdata &= ~mask1;
269 
270 	out_be32(&regs->cpdata, qe_gc->cpdata);
271 	clrsetbits_be32(&regs->cpodr, mask1, sregs->cpodr & mask1);
272 
273 	spin_unlock_irqrestore(&qe_gc->lock, flags);
274 }
275 EXPORT_SYMBOL(qe_pin_set_dedicated);
276 
277 /**
278  * qe_pin_set_gpio - Set a pin to the GPIO mode
279  * @qe_pin:	pointer to the qe_pin structure
280  * Context:	any
281  *
282  * This function sets a pin to the GPIO mode.
283  */
284 void qe_pin_set_gpio(struct qe_pin *qe_pin)
285 {
286 	struct qe_gpio_chip *qe_gc = qe_pin->controller;
287 	struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
288 	unsigned long flags;
289 
290 	spin_lock_irqsave(&qe_gc->lock, flags);
291 
292 	/* Let's make it input by default, GPIO API is able to change that. */
293 	__par_io_config_pin(regs, qe_pin->num, QE_PIO_DIR_IN, 0, 0, 0);
294 
295 	spin_unlock_irqrestore(&qe_gc->lock, flags);
296 }
297 EXPORT_SYMBOL(qe_pin_set_gpio);
298 
299 static int __init qe_add_gpiochips(void)
300 {
301 	struct device_node *np;
302 
303 	for_each_compatible_node(np, NULL, "fsl,mpc8323-qe-pario-bank") {
304 		int ret;
305 		struct qe_gpio_chip *qe_gc;
306 		struct of_mm_gpio_chip *mm_gc;
307 		struct gpio_chip *gc;
308 
309 		qe_gc = kzalloc(sizeof(*qe_gc), GFP_KERNEL);
310 		if (!qe_gc) {
311 			ret = -ENOMEM;
312 			goto err;
313 		}
314 
315 		spin_lock_init(&qe_gc->lock);
316 
317 		mm_gc = &qe_gc->mm_gc;
318 		gc = &mm_gc->gc;
319 
320 		mm_gc->save_regs = qe_gpio_save_regs;
321 		gc->ngpio = QE_PIO_PINS;
322 		gc->direction_input = qe_gpio_dir_in;
323 		gc->direction_output = qe_gpio_dir_out;
324 		gc->get = qe_gpio_get;
325 		gc->set = qe_gpio_set;
326 		gc->set_multiple = qe_gpio_set_multiple;
327 
328 		ret = of_mm_gpiochip_add_data(np, mm_gc, qe_gc);
329 		if (ret)
330 			goto err;
331 		continue;
332 err:
333 		pr_err("%pOF: registration failed with status %d\n",
334 		       np, ret);
335 		kfree(qe_gc);
336 		/* try others anyway */
337 	}
338 	return 0;
339 }
340 arch_initcall(qe_add_gpiochips);
341