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
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/of_gpio.h>
25 #include <linux/of_platform.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.
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
;
62 ret
= (in_8(®s
->wkup_ival
) >> (7 - gpio
)) & 1;
64 pr_debug("%s: gpio: %d ret: %d\n", __func__
, gpio
, ret
);
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
;
78 chip
->shadow_dvo
|= 1 << (7 - gpio
);
80 chip
->shadow_dvo
&= ~(1 << (7 - gpio
));
82 out_8(®s
->wkup_dvo
, chip
->shadow_dvo
);
86 mpc52xx_wkup_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
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
;
107 spin_lock_irqsave(&gpio_lock
, flags
);
109 /* set the direction */
110 chip
->shadow_ddr
&= ~(1 << (7 - gpio
));
111 out_8(®s
->wkup_ddr
, chip
->shadow_ddr
);
113 /* and enable the pin */
114 chip
->shadow_gpioe
|= 1 << (7 - gpio
);
115 out_8(®s
->wkup_gpioe
, chip
->shadow_gpioe
);
117 spin_unlock_irqrestore(&gpio_lock
, flags
);
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
);
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(®s
->wkup_ddr
, chip
->shadow_ddr
);
139 /* Finally enable the pin */
140 chip
->shadow_gpioe
|= 1 << (7 - gpio
);
141 out_8(®s
->wkup_gpioe
, chip
->shadow_gpioe
);
143 spin_unlock_irqrestore(&gpio_lock
, flags
);
145 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
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
;
157 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
161 gc
= &chip
->mmchip
.gc
;
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
);
173 regs
= chip
->mmchip
.regs
;
174 chip
->shadow_gpioe
= in_8(®s
->wkup_gpioe
);
175 chip
->shadow_ddr
= in_8(®s
->wkup_ddr
);
176 chip
->shadow_dvo
= in_8(®s
->wkup_dvo
);
181 static int mpc52xx_gpiochip_remove(struct platform_device
*ofdev
)
186 static const struct of_device_id mpc52xx_wkup_gpiochip_match
[] = {
188 .compatible
= "fsl,mpc5200-gpio-wkup",
193 static struct platform_driver mpc52xx_wkup_gpiochip_driver
= {
196 .owner
= THIS_MODULE
,
197 .of_match_table
= mpc52xx_wkup_gpiochip_match
,
199 .probe
= mpc52xx_wkup_gpiochip_probe
,
200 .remove
= mpc52xx_gpiochip_remove
,
204 * GPIO LIB API implementation for simple GPIOs
206 * There's a maximum of 32 simple GPIOs. Which of these are available
207 * for use depends on your board setup.
208 * The numbering reflects the bit numbering in the port registers:
220 static int mpc52xx_simple_gpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
222 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
223 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
226 ret
= (in_be32(®s
->simple_ival
) >> (31 - gpio
)) & 1;
232 __mpc52xx_simple_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
234 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
235 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
236 struct mpc52xx_gpiochip
, mmchip
);
237 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
240 chip
->shadow_dvo
|= 1 << (31 - gpio
);
242 chip
->shadow_dvo
&= ~(1 << (31 - gpio
));
243 out_be32(®s
->simple_dvo
, chip
->shadow_dvo
);
247 mpc52xx_simple_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
251 spin_lock_irqsave(&gpio_lock
, flags
);
253 __mpc52xx_simple_gpio_set(gc
, gpio
, val
);
255 spin_unlock_irqrestore(&gpio_lock
, flags
);
257 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
260 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
262 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
263 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
264 struct mpc52xx_gpiochip
, mmchip
);
265 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
268 spin_lock_irqsave(&gpio_lock
, flags
);
270 /* set the direction */
271 chip
->shadow_ddr
&= ~(1 << (31 - gpio
));
272 out_be32(®s
->simple_ddr
, chip
->shadow_ddr
);
274 /* and enable the pin */
275 chip
->shadow_gpioe
|= 1 << (31 - gpio
);
276 out_be32(®s
->simple_gpioe
, chip
->shadow_gpioe
);
278 spin_unlock_irqrestore(&gpio_lock
, flags
);
284 mpc52xx_simple_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
286 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
287 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
288 struct mpc52xx_gpiochip
, mmchip
);
289 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
292 spin_lock_irqsave(&gpio_lock
, flags
);
294 /* First set initial value */
295 __mpc52xx_simple_gpio_set(gc
, gpio
, val
);
297 /* Then set direction */
298 chip
->shadow_ddr
|= 1 << (31 - gpio
);
299 out_be32(®s
->simple_ddr
, chip
->shadow_ddr
);
301 /* Finally enable the pin */
302 chip
->shadow_gpioe
|= 1 << (31 - gpio
);
303 out_be32(®s
->simple_gpioe
, chip
->shadow_gpioe
);
305 spin_unlock_irqrestore(&gpio_lock
, flags
);
307 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
312 static int __devinit
mpc52xx_simple_gpiochip_probe(struct platform_device
*ofdev
)
314 struct mpc52xx_gpiochip
*chip
;
315 struct gpio_chip
*gc
;
316 struct mpc52xx_gpio __iomem
*regs
;
319 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
323 gc
= &chip
->mmchip
.gc
;
326 gc
->direction_input
= mpc52xx_simple_gpio_dir_in
;
327 gc
->direction_output
= mpc52xx_simple_gpio_dir_out
;
328 gc
->get
= mpc52xx_simple_gpio_get
;
329 gc
->set
= mpc52xx_simple_gpio_set
;
331 ret
= of_mm_gpiochip_add(ofdev
->dev
.of_node
, &chip
->mmchip
);
335 regs
= chip
->mmchip
.regs
;
336 chip
->shadow_gpioe
= in_be32(®s
->simple_gpioe
);
337 chip
->shadow_ddr
= in_be32(®s
->simple_ddr
);
338 chip
->shadow_dvo
= in_be32(®s
->simple_dvo
);
343 static const struct of_device_id mpc52xx_simple_gpiochip_match
[] = {
345 .compatible
= "fsl,mpc5200-gpio",
350 static struct platform_driver mpc52xx_simple_gpiochip_driver
= {
353 .owner
= THIS_MODULE
,
354 .of_match_table
= mpc52xx_simple_gpiochip_match
,
356 .probe
= mpc52xx_simple_gpiochip_probe
,
357 .remove
= mpc52xx_gpiochip_remove
,
360 static int __init
mpc52xx_gpio_init(void)
362 if (platform_driver_register(&mpc52xx_wkup_gpiochip_driver
))
363 printk(KERN_ERR
"Unable to register wakeup GPIO driver\n");
365 if (platform_driver_register(&mpc52xx_simple_gpiochip_driver
))
366 printk(KERN_ERR
"Unable to register simple GPIO driver\n");
372 /* Make sure we get initialised before anyone else tries to use us */
373 subsys_initcall(mpc52xx_gpio_init
);
375 /* No exit call at the moment as we cannot unregister of gpio chips */
377 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
378 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
379 MODULE_LICENSE("GPL v2");