Skip to content

Commit 1b9e890

Browse files
Anton Vorontsovkumargala
authored andcommitted
powerpc/qe: Implement QE Pin Multiplexing API
With this API we're able to set a QE pin to the GPIO mode or a dedicated peripheral function. The API relies on the fact that QE gpio controllers are registered. If they aren't, the API won't work (gracefully though). There is one caveat though: if anybody occupied the node->data before us, or overwrote it, then bad things will happen. Luckily this is all in the platform code that we fully control, so this should never happen. I could implement more checks (for example we could create a list of successfully registered QE controllers, and compare the node->data in the qe_pin_request()), but this is unneeded if nobody is going to do silly things behind our back. Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com> Signed-off-by: Kumar Gala <galak@kernel.crashing.org>
1 parent 78c7705 commit 1b9e890

2 files changed

Lines changed: 216 additions & 0 deletions

File tree

‎arch/powerpc/include/asm/qe.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifdef __KERNEL__
1818

1919
#include <linux/spinlock.h>
20+
#include <linux/errno.h>
21+
#include <linux/err.h>
2022
#include <asm/cpm.h>
2123
#include <asm/immap_qe.h>
2224

@@ -112,6 +114,25 @@ extern int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
112114
int assignment, int has_irq);
113115
extern int par_io_data_set(u8 port, u8 pin, u8 val);
114116

117+
/*
118+
* Pin multiplexing functions.
119+
*/
120+
struct qe_pin;
121+
#ifdef CONFIG_QE_GPIO
122+
extern struct qe_pin *qe_pin_request(struct device_node *np, int index);
123+
extern void qe_pin_free(struct qe_pin *qe_pin);
124+
extern void qe_pin_set_gpio(struct qe_pin *qe_pin);
125+
extern void qe_pin_set_dedicated(struct qe_pin *pin);
126+
#else
127+
static inline struct qe_pin *qe_pin_request(struct device_node *np, int index)
128+
{
129+
return ERR_PTR(-ENOSYS);
130+
}
131+
static inline void qe_pin_free(struct qe_pin *qe_pin) {}
132+
static inline void qe_pin_set_gpio(struct qe_pin *qe_pin) {}
133+
static inline void qe_pin_set_dedicated(struct qe_pin *pin) {}
134+
#endif /* CONFIG_QE_GPIO */
135+
115136
/* QE internal API */
116137
int qe_issue_cmd(u32 cmd, u32 device, u8 mcn_protocol, u32 cmd_input);
117138
enum qe_clock qe_clock_source(const char *source);

‎arch/powerpc/sysdev/qe_lib/gpio.c‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/kernel.h>
1515
#include <linux/init.h>
1616
#include <linux/spinlock.h>
17+
#include <linux/err.h>
1718
#include <linux/io.h>
1819
#include <linux/of.h>
1920
#include <linux/of_gpio.h>
@@ -24,8 +25,14 @@ struct qe_gpio_chip {
2425
struct of_mm_gpio_chip mm_gc;
2526
spinlock_t lock;
2627

28+
unsigned long pin_flags[QE_PIO_PINS];
29+
#define QE_PIN_REQUESTED 0
30+
2731
/* shadowed data register to clear/set bits safely */
2832
u32 cpdata;
33+
34+
/* saved_regs used to restore dedicated functions */
35+
struct qe_pio_regs saved_regs;
2936
};
3037

3138
static inline struct qe_gpio_chip *
@@ -40,6 +47,12 @@ static void qe_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
4047
struct qe_pio_regs __iomem *regs = mm_gc->regs;
4148

4249
qe_gc->cpdata = in_be32(&regs->cpdata);
50+
qe_gc->saved_regs.cpdata = qe_gc->cpdata;
51+
qe_gc->saved_regs.cpdir1 = in_be32(&regs->cpdir1);
52+
qe_gc->saved_regs.cpdir2 = in_be32(&regs->cpdir2);
53+
qe_gc->saved_regs.cppar1 = in_be32(&regs->cppar1);
54+
qe_gc->saved_regs.cppar2 = in_be32(&regs->cppar2);
55+
qe_gc->saved_regs.cpodr = in_be32(&regs->cpodr);
4356
}
4457

4558
static int qe_gpio_get(struct gpio_chip *gc, unsigned int gpio)
@@ -103,6 +116,188 @@ static int qe_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
103116
return 0;
104117
}
105118

119+
struct qe_pin {
120+
/*
121+
* The qe_gpio_chip name is unfortunate, we should change that to
122+
* something like qe_pio_controller. Someday.
123+
*/
124+
struct qe_gpio_chip *controller;
125+
int num;
126+
};
127+
128+
/**
129+
* qe_pin_request - Request a QE pin
130+
* @np: device node to get a pin from
131+
* @index: index of a pin in the device tree
132+
* Context: non-atomic
133+
*
134+
* This function return qe_pin so that you could use it with the rest of
135+
* the QE Pin Multiplexing API.
136+
*/
137+
struct qe_pin *qe_pin_request(struct device_node *np, int index)
138+
{
139+
struct qe_pin *qe_pin;
140+
struct device_node *gc;
141+
struct of_gpio_chip *of_gc = NULL;
142+
struct of_mm_gpio_chip *mm_gc;
143+
struct qe_gpio_chip *qe_gc;
144+
int err;
145+
int size;
146+
const void *gpio_spec;
147+
const u32 *gpio_cells;
148+
unsigned long flags;
149+
150+
qe_pin = kzalloc(sizeof(*qe_pin), GFP_KERNEL);
151+
if (!qe_pin) {
152+
pr_debug("%s: can't allocate memory\n", __func__);
153+
return ERR_PTR(-ENOMEM);
154+
}
155+
156+
err = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index,
157+
&gc, &gpio_spec);
158+
if (err) {
159+
pr_debug("%s: can't parse gpios property\n", __func__);
160+
goto err0;
161+
}
162+
163+
if (!of_device_is_compatible(gc, "fsl,mpc8323-qe-pario-bank")) {
164+
pr_debug("%s: tried to get a non-qe pin\n", __func__);
165+
err = -EINVAL;
166+
goto err1;
167+
}
168+
169+
of_gc = gc->data;
170+
if (!of_gc) {
171+
pr_debug("%s: gpio controller %s isn't registered\n",
172+
np->full_name, gc->full_name);
173+
err = -ENODEV;
174+
goto err1;
175+
}
176+
177+
gpio_cells = of_get_property(gc, "#gpio-cells", &size);
178+
if (!gpio_cells || size != sizeof(*gpio_cells) ||
179+
*gpio_cells != of_gc->gpio_cells) {
180+
pr_debug("%s: wrong #gpio-cells for %s\n",
181+
np->full_name, gc->full_name);
182+
err = -EINVAL;
183+
goto err1;
184+
}
185+
186+
err = of_gc->xlate(of_gc, np, gpio_spec, NULL);
187+
if (err < 0)
188+
goto err1;
189+
190+
mm_gc = to_of_mm_gpio_chip(&of_gc->gc);
191+
qe_gc = to_qe_gpio_chip(mm_gc);
192+
193+
spin_lock_irqsave(&qe_gc->lock, flags);
194+
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+
err1:
208+
of_node_put(gc);
209+
err0:
210+
kfree(qe_pin);
211+
pr_debug("%s failed with status %d\n", __func__, err);
212+
return ERR_PTR(err);
213+
}
214+
EXPORT_SYMBOL(qe_pin_request);
215+
216+
/**
217+
* qe_pin_free - Free a pin
218+
* @qe_pin: pointer to the qe_pin structure
219+
* Context: any
220+
*
221+
* This function frees the qe_pin structure and makes a pin available
222+
* for further qe_pin_request() calls.
223+
*/
224+
void qe_pin_free(struct qe_pin *qe_pin)
225+
{
226+
struct qe_gpio_chip *qe_gc = qe_pin->controller;
227+
unsigned long flags;
228+
const int pin = qe_pin->num;
229+
230+
spin_lock_irqsave(&qe_gc->lock, flags);
231+
test_and_clear_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[pin]);
232+
spin_unlock_irqrestore(&qe_gc->lock, flags);
233+
234+
kfree(qe_pin);
235+
}
236+
EXPORT_SYMBOL(qe_pin_free);
237+
238+
/**
239+
* qe_pin_set_dedicated - Revert a pin to a dedicated peripheral function mode
240+
* @qe_pin: pointer to the qe_pin structure
241+
* Context: any
242+
*
243+
* This function resets a pin to a dedicated peripheral function that
244+
* has been set up by the firmware.
245+
*/
246+
void qe_pin_set_dedicated(struct qe_pin *qe_pin)
247+
{
248+
struct qe_gpio_chip *qe_gc = qe_pin->controller;
249+
struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
250+
struct qe_pio_regs *sregs = &qe_gc->saved_regs;
251+
int pin = qe_pin->num;
252+
u32 mask1 = 1 << (QE_PIO_PINS - (pin + 1));
253+
u32 mask2 = 0x3 << (QE_PIO_PINS - (pin % (QE_PIO_PINS / 2) + 1) * 2);
254+
bool second_reg = pin > (QE_PIO_PINS / 2) - 1;
255+
unsigned long flags;
256+
257+
spin_lock_irqsave(&qe_gc->lock, flags);
258+
259+
if (second_reg) {
260+
clrsetbits_be32(&regs->cpdir2, mask2, sregs->cpdir2 & mask2);
261+
clrsetbits_be32(&regs->cppar2, mask2, sregs->cppar2 & mask2);
262+
} else {
263+
clrsetbits_be32(&regs->cpdir1, mask2, sregs->cpdir1 & mask2);
264+
clrsetbits_be32(&regs->cppar1, mask2, sregs->cppar1 & mask2);
265+
}
266+
267+
if (sregs->cpdata & mask1)
268+
qe_gc->cpdata |= mask1;
269+
else
270+
qe_gc->cpdata &= ~mask1;
271+
272+
out_be32(&regs->cpdata, qe_gc->cpdata);
273+
clrsetbits_be32(&regs->cpodr, mask1, sregs->cpodr & mask1);
274+
275+
spin_unlock_irqrestore(&qe_gc->lock, flags);
276+
}
277+
EXPORT_SYMBOL(qe_pin_set_dedicated);
278+
279+
/**
280+
* qe_pin_set_gpio - Set a pin to the GPIO mode
281+
* @qe_pin: pointer to the qe_pin structure
282+
* Context: any
283+
*
284+
* This function sets a pin to the GPIO mode.
285+
*/
286+
void qe_pin_set_gpio(struct qe_pin *qe_pin)
287+
{
288+
struct qe_gpio_chip *qe_gc = qe_pin->controller;
289+
struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
290+
unsigned long flags;
291+
292+
spin_lock_irqsave(&qe_gc->lock, flags);
293+
294+
/* Let's make it input by default, GPIO API is able to change that. */
295+
__par_io_config_pin(regs, qe_pin->num, QE_PIO_DIR_IN, 0, 0, 0);
296+
297+
spin_unlock_irqrestore(&qe_gc->lock, flags);
298+
}
299+
EXPORT_SYMBOL(qe_pin_set_gpio);
300+
106301
static int __init qe_add_gpiochips(void)
107302
{
108303
struct device_node *np;

0 commit comments

Comments
 (0)