14 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/export.h>
31 #define QE_PIN_REQUESTED 0
41 to_qe_gpio_chip(
struct of_mm_gpio_chip *
mm_gc)
46 static void qe_gpio_save_regs(
struct of_mm_gpio_chip *
mm_gc)
60 static int qe_gpio_get(
struct gpio_chip *
gc,
unsigned int gpio)
62 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
64 u32 pin_mask = 1 << (QE_PIO_PINS - 1 -
gpio);
66 return in_be32(®s->cpdata) & pin_mask;
69 static void qe_gpio_set(
struct gpio_chip *
gc,
unsigned int gpio,
int val)
71 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
75 u32 pin_mask = 1 << (QE_PIO_PINS - 1 -
gpio);
82 qe_gc->
cpdata &= ~pin_mask;
86 spin_unlock_irqrestore(&qe_gc->
lock, flags);
89 static int qe_gpio_dir_in(
struct gpio_chip *gc,
unsigned int gpio)
91 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
99 spin_unlock_irqrestore(&qe_gc->
lock, flags);
104 static int qe_gpio_dir_out(
struct gpio_chip *gc,
unsigned int gpio,
int val)
106 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
110 qe_gpio_set(gc, gpio, val);
116 spin_unlock_irqrestore(&qe_gc->
lock, flags);
142 struct gpio_chip *gc;
143 struct of_mm_gpio_chip *mm_gc;
148 qe_pin = kzalloc(
sizeof(*qe_pin),
GFP_KERNEL);
150 pr_debug(
"%s: can't allocate memory\n", __func__);
154 err = of_get_gpio(np, index);
162 pr_debug(
"%s: tried to get a non-qe pin\n", __func__);
167 mm_gc = to_of_mm_gpio_chip(gc);
168 qe_gc = to_qe_gpio_chip(mm_gc);
181 spin_unlock_irqrestore(&qe_gc->
lock, flags);
187 pr_debug(
"%s failed with status %d\n", __func__, err);
204 const int pin = qe_pin->
num;
208 spin_unlock_irqrestore(&qe_gc->
lock, flags);
225 struct qe_pio_regs __iomem *regs = qe_gc->
mm_gc.regs;
226 struct qe_pio_regs *sregs = &qe_gc->
saved_regs;
228 u32 mask1 = 1 << (QE_PIO_PINS - (pin + 1));
229 u32 mask2 = 0x3 << (QE_PIO_PINS - (pin % (QE_PIO_PINS / 2) + 1) * 2);
230 bool second_reg = pin > (QE_PIO_PINS / 2) - 1;
236 clrsetbits_be32(®s->cpdir2, mask2, sregs->cpdir2 & mask2);
237 clrsetbits_be32(®s->cppar2, mask2, sregs->cppar2 & mask2);
239 clrsetbits_be32(®s->cpdir1, mask2, sregs->cpdir1 & mask2);
240 clrsetbits_be32(®s->cppar1, mask2, sregs->cppar1 & mask2);
243 if (sregs->cpdata & mask1)
249 clrsetbits_be32(®s->cpodr, mask1, sregs->cpodr & mask1);
251 spin_unlock_irqrestore(&qe_gc->
lock, flags);
265 struct qe_pio_regs __iomem *regs = qe_gc->
mm_gc.regs;
273 spin_unlock_irqrestore(&qe_gc->
lock, flags);
277 static int __init qe_add_gpiochips(
void)
281 for_each_compatible_node(np,
NULL,
"fsl,mpc8323-qe-pario-bank") {
284 struct of_mm_gpio_chip *mm_gc;
285 struct gpio_chip *gc;
295 mm_gc = &qe_gc->
mm_gc;
298 mm_gc->save_regs = qe_gpio_save_regs;
299 gc->ngpio = QE_PIO_PINS;
300 gc->direction_input = qe_gpio_dir_in;
301 gc->direction_output = qe_gpio_dir_out;
302 gc->get = qe_gpio_get;
303 gc->set = qe_gpio_set;
310 pr_err(
"%s: registration failed with status %d\n",