2 * GPIOs on MPC8349/8572/8610 and compatible
4 * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
16 #include <linux/of_gpio.h>
17 #include <linux/gpio.h>
19 #define MPC8XXX_GPIO_PINS 32
28 struct mpc8xxx_gpio_chip {
29 struct of_mm_gpio_chip mm_gc;
33 * shadowed data register to be able to clear/set output pins in
34 * open drain mode safely
39 static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
41 return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
44 static inline struct mpc8xxx_gpio_chip *
45 to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
47 return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
50 static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
52 struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
54 mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
57 static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
59 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
61 return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
64 static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
66 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
67 struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
70 spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
73 mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
75 mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
77 out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
79 spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
82 static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
84 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
85 struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
88 spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
90 clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
92 spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
97 static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
99 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
100 struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
103 mpc8xxx_gpio_set(gc, gpio, val);
105 spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
107 setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
109 spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
114 static void __init mpc8xxx_add_controller(struct device_node *np)
116 struct mpc8xxx_gpio_chip *mpc8xxx_gc;
117 struct of_mm_gpio_chip *mm_gc;
118 struct of_gpio_chip *of_gc;
119 struct gpio_chip *gc;
122 mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
128 spin_lock_init(&mpc8xxx_gc->lock);
130 mm_gc = &mpc8xxx_gc->mm_gc;
131 of_gc = &mm_gc->of_gc;
134 mm_gc->save_regs = mpc8xxx_gpio_save_regs;
135 of_gc->gpio_cells = 2;
136 gc->ngpio = MPC8XXX_GPIO_PINS;
137 gc->direction_input = mpc8xxx_gpio_dir_in;
138 gc->direction_output = mpc8xxx_gpio_dir_out;
139 gc->get = mpc8xxx_gpio_get;
140 gc->set = mpc8xxx_gpio_set;
142 ret = of_mm_gpiochip_add(np, mm_gc);
149 pr_err("%s: registration failed with status %d\n",
156 static int __init mpc8xxx_add_gpiochips(void)
158 struct device_node *np;
160 for_each_compatible_node(np, NULL, "fsl,mpc8349-gpio")
161 mpc8xxx_add_controller(np);
163 for_each_compatible_node(np, NULL, "fsl,mpc8572-gpio")
164 mpc8xxx_add_controller(np);
166 for_each_compatible_node(np, NULL, "fsl,mpc8610-gpio")
167 mpc8xxx_add_controller(np);
171 arch_initcall(mpc8xxx_add_gpiochips);