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>
26 #include <linux/module.h>
29 #include <asm/mpc52xx.h>
30 #include <sysdev/fsl_soc.h>
32 static DEFINE_SPINLOCK(gpio_lock
);
34 struct mpc52xx_gpiochip
{
35 struct of_mm_gpio_chip mmchip
;
36 unsigned int shadow_dvo
;
37 unsigned int shadow_gpioe
;
38 unsigned int shadow_ddr
;
42 * GPIO LIB API implementation for wakeup GPIOs.
44 * There's a maximum of 8 wakeup GPIOs. Which of these are available
45 * for use depends on your board setup.
57 static int mpc52xx_wkup_gpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
59 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
60 struct mpc52xx_gpio_wkup __iomem
*regs
= mm_gc
->regs
;
63 ret
= (in_8(®s
->wkup_ival
) >> (7 - gpio
)) & 1;
65 pr_debug("%s: gpio: %d ret: %d\n", __func__
, gpio
, ret
);
71 __mpc52xx_wkup_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
73 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
74 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
75 struct mpc52xx_gpiochip
, mmchip
);
76 struct mpc52xx_gpio_wkup __iomem
*regs
= mm_gc
->regs
;
79 chip
->shadow_dvo
|= 1 << (7 - gpio
);
81 chip
->shadow_dvo
&= ~(1 << (7 - gpio
));
83 out_8(®s
->wkup_dvo
, chip
->shadow_dvo
);
87 mpc52xx_wkup_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
91 spin_lock_irqsave(&gpio_lock
, flags
);
93 __mpc52xx_wkup_gpio_set(gc
, gpio
, val
);
95 spin_unlock_irqrestore(&gpio_lock
, flags
);
97 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
100 static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
102 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
103 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
104 struct mpc52xx_gpiochip
, mmchip
);
105 struct mpc52xx_gpio_wkup __iomem
*regs
= mm_gc
->regs
;
108 spin_lock_irqsave(&gpio_lock
, flags
);
110 /* set the direction */
111 chip
->shadow_ddr
&= ~(1 << (7 - gpio
));
112 out_8(®s
->wkup_ddr
, chip
->shadow_ddr
);
114 /* and enable the pin */
115 chip
->shadow_gpioe
|= 1 << (7 - gpio
);
116 out_8(®s
->wkup_gpioe
, chip
->shadow_gpioe
);
118 spin_unlock_irqrestore(&gpio_lock
, flags
);
124 mpc52xx_wkup_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
126 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
127 struct mpc52xx_gpio_wkup __iomem
*regs
= mm_gc
->regs
;
128 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
129 struct mpc52xx_gpiochip
, mmchip
);
132 spin_lock_irqsave(&gpio_lock
, flags
);
134 __mpc52xx_wkup_gpio_set(gc
, gpio
, val
);
136 /* Then set direction */
137 chip
->shadow_ddr
|= 1 << (7 - gpio
);
138 out_8(®s
->wkup_ddr
, chip
->shadow_ddr
);
140 /* Finally enable the pin */
141 chip
->shadow_gpioe
|= 1 << (7 - gpio
);
142 out_8(®s
->wkup_gpioe
, chip
->shadow_gpioe
);
144 spin_unlock_irqrestore(&gpio_lock
, flags
);
146 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
151 static int mpc52xx_wkup_gpiochip_probe(struct platform_device
*ofdev
)
153 struct mpc52xx_gpiochip
*chip
;
154 struct mpc52xx_gpio_wkup __iomem
*regs
;
155 struct gpio_chip
*gc
;
158 chip
= devm_kzalloc(&ofdev
->dev
, sizeof(*chip
), GFP_KERNEL
);
162 platform_set_drvdata(ofdev
, chip
);
164 gc
= &chip
->mmchip
.gc
;
167 gc
->direction_input
= mpc52xx_wkup_gpio_dir_in
;
168 gc
->direction_output
= mpc52xx_wkup_gpio_dir_out
;
169 gc
->get
= mpc52xx_wkup_gpio_get
;
170 gc
->set
= mpc52xx_wkup_gpio_set
;
172 ret
= of_mm_gpiochip_add(ofdev
->dev
.of_node
, &chip
->mmchip
);
176 regs
= chip
->mmchip
.regs
;
177 chip
->shadow_gpioe
= in_8(®s
->wkup_gpioe
);
178 chip
->shadow_ddr
= in_8(®s
->wkup_ddr
);
179 chip
->shadow_dvo
= in_8(®s
->wkup_dvo
);
184 static int mpc52xx_gpiochip_remove(struct platform_device
*ofdev
)
186 struct mpc52xx_gpiochip
*chip
= platform_get_drvdata(ofdev
);
188 of_mm_gpiochip_remove(&chip
->mmchip
);
193 static const struct of_device_id mpc52xx_wkup_gpiochip_match
[] = {
194 { .compatible
= "fsl,mpc5200-gpio-wkup", },
198 static struct platform_driver mpc52xx_wkup_gpiochip_driver
= {
200 .name
= "mpc5200-gpio-wkup",
201 .of_match_table
= mpc52xx_wkup_gpiochip_match
,
203 .probe
= mpc52xx_wkup_gpiochip_probe
,
204 .remove
= mpc52xx_gpiochip_remove
,
208 * GPIO LIB API implementation for simple GPIOs
210 * There's a maximum of 32 simple GPIOs. Which of these are available
211 * for use depends on your board setup.
212 * The numbering reflects the bit numbering in the port registers:
224 static int mpc52xx_simple_gpio_get(struct gpio_chip
*gc
, unsigned int gpio
)
226 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
227 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
230 ret
= (in_be32(®s
->simple_ival
) >> (31 - gpio
)) & 1;
236 __mpc52xx_simple_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
238 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
239 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
240 struct mpc52xx_gpiochip
, mmchip
);
241 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
244 chip
->shadow_dvo
|= 1 << (31 - gpio
);
246 chip
->shadow_dvo
&= ~(1 << (31 - gpio
));
247 out_be32(®s
->simple_dvo
, chip
->shadow_dvo
);
251 mpc52xx_simple_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
255 spin_lock_irqsave(&gpio_lock
, flags
);
257 __mpc52xx_simple_gpio_set(gc
, gpio
, val
);
259 spin_unlock_irqrestore(&gpio_lock
, flags
);
261 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
264 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
266 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
267 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
268 struct mpc52xx_gpiochip
, mmchip
);
269 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
272 spin_lock_irqsave(&gpio_lock
, flags
);
274 /* set the direction */
275 chip
->shadow_ddr
&= ~(1 << (31 - gpio
));
276 out_be32(®s
->simple_ddr
, chip
->shadow_ddr
);
278 /* and enable the pin */
279 chip
->shadow_gpioe
|= 1 << (31 - gpio
);
280 out_be32(®s
->simple_gpioe
, chip
->shadow_gpioe
);
282 spin_unlock_irqrestore(&gpio_lock
, flags
);
288 mpc52xx_simple_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
290 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
291 struct mpc52xx_gpiochip
*chip
= container_of(mm_gc
,
292 struct mpc52xx_gpiochip
, mmchip
);
293 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
296 spin_lock_irqsave(&gpio_lock
, flags
);
298 /* First set initial value */
299 __mpc52xx_simple_gpio_set(gc
, gpio
, val
);
301 /* Then set direction */
302 chip
->shadow_ddr
|= 1 << (31 - gpio
);
303 out_be32(®s
->simple_ddr
, chip
->shadow_ddr
);
305 /* Finally enable the pin */
306 chip
->shadow_gpioe
|= 1 << (31 - gpio
);
307 out_be32(®s
->simple_gpioe
, chip
->shadow_gpioe
);
309 spin_unlock_irqrestore(&gpio_lock
, flags
);
311 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
316 static int mpc52xx_simple_gpiochip_probe(struct platform_device
*ofdev
)
318 struct mpc52xx_gpiochip
*chip
;
319 struct gpio_chip
*gc
;
320 struct mpc52xx_gpio __iomem
*regs
;
323 chip
= devm_kzalloc(&ofdev
->dev
, sizeof(*chip
), GFP_KERNEL
);
327 platform_set_drvdata(ofdev
, chip
);
329 gc
= &chip
->mmchip
.gc
;
332 gc
->direction_input
= mpc52xx_simple_gpio_dir_in
;
333 gc
->direction_output
= mpc52xx_simple_gpio_dir_out
;
334 gc
->get
= mpc52xx_simple_gpio_get
;
335 gc
->set
= mpc52xx_simple_gpio_set
;
337 ret
= of_mm_gpiochip_add(ofdev
->dev
.of_node
, &chip
->mmchip
);
341 regs
= chip
->mmchip
.regs
;
342 chip
->shadow_gpioe
= in_be32(®s
->simple_gpioe
);
343 chip
->shadow_ddr
= in_be32(®s
->simple_ddr
);
344 chip
->shadow_dvo
= in_be32(®s
->simple_dvo
);
349 static const struct of_device_id mpc52xx_simple_gpiochip_match
[] = {
350 { .compatible
= "fsl,mpc5200-gpio", },
354 static struct platform_driver mpc52xx_simple_gpiochip_driver
= {
356 .name
= "mpc5200-gpio",
357 .of_match_table
= mpc52xx_simple_gpiochip_match
,
359 .probe
= mpc52xx_simple_gpiochip_probe
,
360 .remove
= mpc52xx_gpiochip_remove
,
363 static int __init
mpc52xx_gpio_init(void)
365 if (platform_driver_register(&mpc52xx_wkup_gpiochip_driver
))
366 printk(KERN_ERR
"Unable to register wakeup GPIO driver\n");
368 if (platform_driver_register(&mpc52xx_simple_gpiochip_driver
))
369 printk(KERN_ERR
"Unable to register simple GPIO driver\n");
374 /* Make sure we get initialised before anyone else tries to use us */
375 subsys_initcall(mpc52xx_gpio_init
);
377 static void __exit
mpc52xx_gpio_exit(void)
379 platform_driver_unregister(&mpc52xx_wkup_gpiochip_driver
);
381 platform_driver_unregister(&mpc52xx_simple_gpiochip_driver
);
383 module_exit(mpc52xx_gpio_exit
);
385 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
386 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
387 MODULE_LICENSE("GPL v2");