4 * Copyright (c) MontaVista Software, Inc. 2008.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
19 #include <linux/of_gpio.h>
20 #include <linux/gpio.h>
24 struct of_mm_gpio_chip mm_gc;
27 /* shadowed data register to clear/set bits safely */
31 static inline struct qe_gpio_chip *
32 to_qe_gpio_chip(struct of_mm_gpio_chip *mm_gc)
34 return container_of(mm_gc, struct qe_gpio_chip, mm_gc);
37 static void qe_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
39 struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
40 struct qe_pio_regs __iomem *regs = mm_gc->regs;
42 qe_gc->cpdata = in_be32(®s->cpdata);
45 static int qe_gpio_get(struct gpio_chip *gc, unsigned int gpio)
47 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
48 struct qe_pio_regs __iomem *regs = mm_gc->regs;
49 u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
51 return in_be32(®s->cpdata) & pin_mask;
54 static void qe_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
56 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
57 struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
58 struct qe_pio_regs __iomem *regs = mm_gc->regs;
60 u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
62 spin_lock_irqsave(&qe_gc->lock, flags);
65 qe_gc->cpdata |= pin_mask;
67 qe_gc->cpdata &= ~pin_mask;
69 out_be32(®s->cpdata, qe_gc->cpdata);
71 spin_unlock_irqrestore(&qe_gc->lock, flags);
74 static int qe_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
76 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
77 struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
80 spin_lock_irqsave(&qe_gc->lock, flags);
82 __par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_IN, 0, 0, 0);
84 spin_unlock_irqrestore(&qe_gc->lock, flags);
89 static int qe_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
91 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
92 struct qe_gpio_chip *qe_gc = to_qe_gpio_chip(mm_gc);
95 spin_lock_irqsave(&qe_gc->lock, flags);
97 __par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_OUT, 0, 0, 0);
99 spin_unlock_irqrestore(&qe_gc->lock, flags);
101 qe_gpio_set(gc, gpio, val);
106 static int __init qe_add_gpiochips(void)
108 struct device_node *np;
110 for_each_compatible_node(np, NULL, "fsl,mpc8323-qe-pario-bank") {
112 struct qe_gpio_chip *qe_gc;
113 struct of_mm_gpio_chip *mm_gc;
114 struct of_gpio_chip *of_gc;
115 struct gpio_chip *gc;
117 qe_gc = kzalloc(sizeof(*qe_gc), GFP_KERNEL);
123 spin_lock_init(&qe_gc->lock);
125 mm_gc = &qe_gc->mm_gc;
126 of_gc = &mm_gc->of_gc;
129 mm_gc->save_regs = qe_gpio_save_regs;
130 of_gc->gpio_cells = 2;
131 gc->ngpio = QE_PIO_PINS;
132 gc->direction_input = qe_gpio_dir_in;
133 gc->direction_output = qe_gpio_dir_out;
134 gc->get = qe_gpio_get;
135 gc->set = qe_gpio_set;
137 ret = of_mm_gpiochip_add(np, mm_gc);
142 pr_err("%s: registration failed with status %d\n",
145 /* try others anyway */
149 arch_initcall(qe_add_gpiochips);