vmwgfx: Restrict number of GMR pages to device limit
[linux-2.6/libata-dev.git] / drivers / gpio / gpio-mpc5200.c
blob52d3ed208105adf8a40e7fbf64fb1618ac986100
1 /*
2 * MPC52xx gpio driver
4 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/of.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/of_gpio.h>
24 #include <linux/io.h>
25 #include <linux/of_platform.h>
27 #include <asm/gpio.h>
28 #include <asm/mpc52xx.h>
29 #include <sysdev/fsl_soc.h>
31 static DEFINE_SPINLOCK(gpio_lock);
33 struct mpc52xx_gpiochip {
34 struct of_mm_gpio_chip mmchip;
35 unsigned int shadow_dvo;
36 unsigned int shadow_gpioe;
37 unsigned int shadow_ddr;
41 * GPIO LIB API implementation for wakeup GPIOs.
43 * There's a maximum of 8 wakeup GPIOs. Which of these are available
44 * for use depends on your board setup.
46 * 0 -> GPIO_WKUP_7
47 * 1 -> GPIO_WKUP_6
48 * 2 -> PSC6_1
49 * 3 -> PSC6_0
50 * 4 -> ETH_17
51 * 5 -> PSC3_9
52 * 6 -> PSC2_4
53 * 7 -> PSC1_4
56 static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
58 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
59 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
60 unsigned int ret;
62 ret = (in_8(&regs->wkup_ival) >> (7 - gpio)) & 1;
64 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
66 return ret;
69 static inline void
70 __mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
72 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
73 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
74 struct mpc52xx_gpiochip, mmchip);
75 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
77 if (val)
78 chip->shadow_dvo |= 1 << (7 - gpio);
79 else
80 chip->shadow_dvo &= ~(1 << (7 - gpio));
82 out_8(&regs->wkup_dvo, chip->shadow_dvo);
85 static void
86 mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
88 unsigned long flags;
90 spin_lock_irqsave(&gpio_lock, flags);
92 __mpc52xx_wkup_gpio_set(gc, gpio, val);
94 spin_unlock_irqrestore(&gpio_lock, flags);
96 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
99 static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
101 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
102 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
103 struct mpc52xx_gpiochip, mmchip);
104 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
105 unsigned long flags;
107 spin_lock_irqsave(&gpio_lock, flags);
109 /* set the direction */
110 chip->shadow_ddr &= ~(1 << (7 - gpio));
111 out_8(&regs->wkup_ddr, chip->shadow_ddr);
113 /* and enable the pin */
114 chip->shadow_gpioe |= 1 << (7 - gpio);
115 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
117 spin_unlock_irqrestore(&gpio_lock, flags);
119 return 0;
122 static int
123 mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
125 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
126 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
127 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
128 struct mpc52xx_gpiochip, mmchip);
129 unsigned long flags;
131 spin_lock_irqsave(&gpio_lock, flags);
133 __mpc52xx_wkup_gpio_set(gc, gpio, val);
135 /* Then set direction */
136 chip->shadow_ddr |= 1 << (7 - gpio);
137 out_8(&regs->wkup_ddr, chip->shadow_ddr);
139 /* Finally enable the pin */
140 chip->shadow_gpioe |= 1 << (7 - gpio);
141 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
143 spin_unlock_irqrestore(&gpio_lock, flags);
145 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
147 return 0;
150 static int __devinit mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
152 struct mpc52xx_gpiochip *chip;
153 struct mpc52xx_gpio_wkup __iomem *regs;
154 struct gpio_chip *gc;
155 int ret;
157 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
158 if (!chip)
159 return -ENOMEM;
161 gc = &chip->mmchip.gc;
163 gc->ngpio = 8;
164 gc->direction_input = mpc52xx_wkup_gpio_dir_in;
165 gc->direction_output = mpc52xx_wkup_gpio_dir_out;
166 gc->get = mpc52xx_wkup_gpio_get;
167 gc->set = mpc52xx_wkup_gpio_set;
169 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
170 if (ret)
171 return ret;
173 regs = chip->mmchip.regs;
174 chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
175 chip->shadow_ddr = in_8(&regs->wkup_ddr);
176 chip->shadow_dvo = in_8(&regs->wkup_dvo);
178 return 0;
181 static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
183 return -EBUSY;
186 static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
187 { .compatible = "fsl,mpc5200-gpio-wkup", },
191 static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
192 .driver = {
193 .name = "mpc5200-gpio-wkup",
194 .owner = THIS_MODULE,
195 .of_match_table = mpc52xx_wkup_gpiochip_match,
197 .probe = mpc52xx_wkup_gpiochip_probe,
198 .remove = mpc52xx_gpiochip_remove,
202 * GPIO LIB API implementation for simple GPIOs
204 * There's a maximum of 32 simple GPIOs. Which of these are available
205 * for use depends on your board setup.
206 * The numbering reflects the bit numbering in the port registers:
208 * 0..1 > reserved
209 * 2..3 > IRDA
210 * 4..7 > ETHR
211 * 8..11 > reserved
212 * 12..15 > USB
213 * 16..17 > reserved
214 * 18..23 > PSC3
215 * 24..27 > PSC2
216 * 28..31 > PSC1
218 static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
220 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
221 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
222 unsigned int ret;
224 ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
226 return ret;
229 static inline void
230 __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
232 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
233 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
234 struct mpc52xx_gpiochip, mmchip);
235 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
237 if (val)
238 chip->shadow_dvo |= 1 << (31 - gpio);
239 else
240 chip->shadow_dvo &= ~(1 << (31 - gpio));
241 out_be32(&regs->simple_dvo, chip->shadow_dvo);
244 static void
245 mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
247 unsigned long flags;
249 spin_lock_irqsave(&gpio_lock, flags);
251 __mpc52xx_simple_gpio_set(gc, gpio, val);
253 spin_unlock_irqrestore(&gpio_lock, flags);
255 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
258 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
260 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
261 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
262 struct mpc52xx_gpiochip, mmchip);
263 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
264 unsigned long flags;
266 spin_lock_irqsave(&gpio_lock, flags);
268 /* set the direction */
269 chip->shadow_ddr &= ~(1 << (31 - gpio));
270 out_be32(&regs->simple_ddr, chip->shadow_ddr);
272 /* and enable the pin */
273 chip->shadow_gpioe |= 1 << (31 - gpio);
274 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
276 spin_unlock_irqrestore(&gpio_lock, flags);
278 return 0;
281 static int
282 mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
284 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
285 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
286 struct mpc52xx_gpiochip, mmchip);
287 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
288 unsigned long flags;
290 spin_lock_irqsave(&gpio_lock, flags);
292 /* First set initial value */
293 __mpc52xx_simple_gpio_set(gc, gpio, val);
295 /* Then set direction */
296 chip->shadow_ddr |= 1 << (31 - gpio);
297 out_be32(&regs->simple_ddr, chip->shadow_ddr);
299 /* Finally enable the pin */
300 chip->shadow_gpioe |= 1 << (31 - gpio);
301 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
303 spin_unlock_irqrestore(&gpio_lock, flags);
305 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
307 return 0;
310 static int __devinit mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
312 struct mpc52xx_gpiochip *chip;
313 struct gpio_chip *gc;
314 struct mpc52xx_gpio __iomem *regs;
315 int ret;
317 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
318 if (!chip)
319 return -ENOMEM;
321 gc = &chip->mmchip.gc;
323 gc->ngpio = 32;
324 gc->direction_input = mpc52xx_simple_gpio_dir_in;
325 gc->direction_output = mpc52xx_simple_gpio_dir_out;
326 gc->get = mpc52xx_simple_gpio_get;
327 gc->set = mpc52xx_simple_gpio_set;
329 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
330 if (ret)
331 return ret;
333 regs = chip->mmchip.regs;
334 chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
335 chip->shadow_ddr = in_be32(&regs->simple_ddr);
336 chip->shadow_dvo = in_be32(&regs->simple_dvo);
338 return 0;
341 static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
342 { .compatible = "fsl,mpc5200-gpio", },
346 static struct platform_driver mpc52xx_simple_gpiochip_driver = {
347 .driver = {
348 .name = "mpc5200-gpio",
349 .owner = THIS_MODULE,
350 .of_match_table = mpc52xx_simple_gpiochip_match,
352 .probe = mpc52xx_simple_gpiochip_probe,
353 .remove = mpc52xx_gpiochip_remove,
356 static int __init mpc52xx_gpio_init(void)
358 if (platform_driver_register(&mpc52xx_wkup_gpiochip_driver))
359 printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
361 if (platform_driver_register(&mpc52xx_simple_gpiochip_driver))
362 printk(KERN_ERR "Unable to register simple GPIO driver\n");
364 return 0;
368 /* Make sure we get initialised before anyone else tries to use us */
369 subsys_initcall(mpc52xx_gpio_init);
371 /* No exit call at the moment as we cannot unregister of gpio chips */
373 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
374 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
375 MODULE_LICENSE("GPL v2");