Linux 2.6.31.6
[linux/fpc-iii.git] / arch / powerpc / sysdev / mpc8xxx_gpio.c
blob103eace361946a65ed5cdb28f59b7afa6ed9e1ac
1 /*
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.
9 */
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/io.h>
15 #include <linux/of.h>
16 #include <linux/of_gpio.h>
17 #include <linux/gpio.h>
19 #define MPC8XXX_GPIO_PINS 32
21 #define GPIO_DIR 0x00
22 #define GPIO_ODR 0x04
23 #define GPIO_DAT 0x08
24 #define GPIO_IER 0x0c
25 #define GPIO_IMR 0x10
26 #define GPIO_ICR 0x14
28 struct mpc8xxx_gpio_chip {
29 struct of_mm_gpio_chip mm_gc;
30 spinlock_t lock;
33 * shadowed data register to be able to clear/set output pins in
34 * open drain mode safely
36 u32 data;
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);
68 unsigned long flags;
70 spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
72 if (val)
73 mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
74 else
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);
86 unsigned long flags;
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);
94 return 0;
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);
101 unsigned long flags;
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);
111 return 0;
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;
120 int ret;
122 mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
123 if (!mpc8xxx_gc) {
124 ret = -ENOMEM;
125 goto err;
128 spin_lock_init(&mpc8xxx_gc->lock);
130 mm_gc = &mpc8xxx_gc->mm_gc;
131 of_gc = &mm_gc->of_gc;
132 gc = &of_gc->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);
143 if (ret)
144 goto err;
146 return;
148 err:
149 pr_err("%s: registration failed with status %d\n",
150 np->full_name, ret);
151 kfree(mpc8xxx_gc);
153 return;
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);
169 return 0;
171 arch_initcall(mpc8xxx_add_gpiochips);