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>
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
= gpiochip_get_data(gc
);
74 struct mpc52xx_gpio_wkup __iomem
*regs
= mm_gc
->regs
;
77 chip
->shadow_dvo
|= 1 << (7 - gpio
);
79 chip
->shadow_dvo
&= ~(1 << (7 - gpio
));
81 out_8(®s
->wkup_dvo
, chip
->shadow_dvo
);
85 mpc52xx_wkup_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
89 spin_lock_irqsave(&gpio_lock
, flags
);
91 __mpc52xx_wkup_gpio_set(gc
, gpio
, val
);
93 spin_unlock_irqrestore(&gpio_lock
, flags
);
95 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
98 static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
100 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
101 struct mpc52xx_gpiochip
*chip
= gpiochip_get_data(gc
);
102 struct mpc52xx_gpio_wkup __iomem
*regs
= mm_gc
->regs
;
105 spin_lock_irqsave(&gpio_lock
, flags
);
107 /* set the direction */
108 chip
->shadow_ddr
&= ~(1 << (7 - gpio
));
109 out_8(®s
->wkup_ddr
, chip
->shadow_ddr
);
111 /* and enable the pin */
112 chip
->shadow_gpioe
|= 1 << (7 - gpio
);
113 out_8(®s
->wkup_gpioe
, chip
->shadow_gpioe
);
115 spin_unlock_irqrestore(&gpio_lock
, flags
);
121 mpc52xx_wkup_gpio_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
123 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
124 struct mpc52xx_gpio_wkup __iomem
*regs
= mm_gc
->regs
;
125 struct mpc52xx_gpiochip
*chip
= gpiochip_get_data(gc
);
128 spin_lock_irqsave(&gpio_lock
, flags
);
130 __mpc52xx_wkup_gpio_set(gc
, gpio
, val
);
132 /* Then set direction */
133 chip
->shadow_ddr
|= 1 << (7 - gpio
);
134 out_8(®s
->wkup_ddr
, chip
->shadow_ddr
);
136 /* Finally enable the pin */
137 chip
->shadow_gpioe
|= 1 << (7 - gpio
);
138 out_8(®s
->wkup_gpioe
, chip
->shadow_gpioe
);
140 spin_unlock_irqrestore(&gpio_lock
, flags
);
142 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
147 static int mpc52xx_wkup_gpiochip_probe(struct platform_device
*ofdev
)
149 struct mpc52xx_gpiochip
*chip
;
150 struct mpc52xx_gpio_wkup __iomem
*regs
;
151 struct gpio_chip
*gc
;
154 chip
= devm_kzalloc(&ofdev
->dev
, sizeof(*chip
), GFP_KERNEL
);
158 platform_set_drvdata(ofdev
, chip
);
160 gc
= &chip
->mmchip
.gc
;
163 gc
->direction_input
= mpc52xx_wkup_gpio_dir_in
;
164 gc
->direction_output
= mpc52xx_wkup_gpio_dir_out
;
165 gc
->get
= mpc52xx_wkup_gpio_get
;
166 gc
->set
= mpc52xx_wkup_gpio_set
;
168 ret
= of_mm_gpiochip_add_data(ofdev
->dev
.of_node
, &chip
->mmchip
, chip
);
172 regs
= chip
->mmchip
.regs
;
173 chip
->shadow_gpioe
= in_8(®s
->wkup_gpioe
);
174 chip
->shadow_ddr
= in_8(®s
->wkup_ddr
);
175 chip
->shadow_dvo
= in_8(®s
->wkup_dvo
);
180 static int mpc52xx_gpiochip_remove(struct platform_device
*ofdev
)
182 struct mpc52xx_gpiochip
*chip
= platform_get_drvdata(ofdev
);
184 of_mm_gpiochip_remove(&chip
->mmchip
);
189 static const struct of_device_id mpc52xx_wkup_gpiochip_match
[] = {
190 { .compatible
= "fsl,mpc5200-gpio-wkup", },
194 static struct platform_driver mpc52xx_wkup_gpiochip_driver
= {
196 .name
= "mpc5200-gpio-wkup",
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
= gpiochip_get_data(gc
);
236 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
239 chip
->shadow_dvo
|= 1 << (31 - gpio
);
241 chip
->shadow_dvo
&= ~(1 << (31 - gpio
));
242 out_be32(®s
->simple_dvo
, chip
->shadow_dvo
);
246 mpc52xx_simple_gpio_set(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
250 spin_lock_irqsave(&gpio_lock
, flags
);
252 __mpc52xx_simple_gpio_set(gc
, gpio
, val
);
254 spin_unlock_irqrestore(&gpio_lock
, flags
);
256 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
259 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
261 struct of_mm_gpio_chip
*mm_gc
= to_of_mm_gpio_chip(gc
);
262 struct mpc52xx_gpiochip
*chip
= gpiochip_get_data(gc
);
263 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
266 spin_lock_irqsave(&gpio_lock
, flags
);
268 /* set the direction */
269 chip
->shadow_ddr
&= ~(1 << (31 - gpio
));
270 out_be32(®s
->simple_ddr
, chip
->shadow_ddr
);
272 /* and enable the pin */
273 chip
->shadow_gpioe
|= 1 << (31 - gpio
);
274 out_be32(®s
->simple_gpioe
, chip
->shadow_gpioe
);
276 spin_unlock_irqrestore(&gpio_lock
, flags
);
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
= gpiochip_get_data(gc
);
286 struct mpc52xx_gpio __iomem
*regs
= mm_gc
->regs
;
289 spin_lock_irqsave(&gpio_lock
, flags
);
291 /* First set initial value */
292 __mpc52xx_simple_gpio_set(gc
, gpio
, val
);
294 /* Then set direction */
295 chip
->shadow_ddr
|= 1 << (31 - gpio
);
296 out_be32(®s
->simple_ddr
, chip
->shadow_ddr
);
298 /* Finally enable the pin */
299 chip
->shadow_gpioe
|= 1 << (31 - gpio
);
300 out_be32(®s
->simple_gpioe
, chip
->shadow_gpioe
);
302 spin_unlock_irqrestore(&gpio_lock
, flags
);
304 pr_debug("%s: gpio: %d val: %d\n", __func__
, gpio
, val
);
309 static int mpc52xx_simple_gpiochip_probe(struct platform_device
*ofdev
)
311 struct mpc52xx_gpiochip
*chip
;
312 struct gpio_chip
*gc
;
313 struct mpc52xx_gpio __iomem
*regs
;
316 chip
= devm_kzalloc(&ofdev
->dev
, sizeof(*chip
), GFP_KERNEL
);
320 platform_set_drvdata(ofdev
, chip
);
322 gc
= &chip
->mmchip
.gc
;
325 gc
->direction_input
= mpc52xx_simple_gpio_dir_in
;
326 gc
->direction_output
= mpc52xx_simple_gpio_dir_out
;
327 gc
->get
= mpc52xx_simple_gpio_get
;
328 gc
->set
= mpc52xx_simple_gpio_set
;
330 ret
= of_mm_gpiochip_add_data(ofdev
->dev
.of_node
, &chip
->mmchip
, chip
);
334 regs
= chip
->mmchip
.regs
;
335 chip
->shadow_gpioe
= in_be32(®s
->simple_gpioe
);
336 chip
->shadow_ddr
= in_be32(®s
->simple_ddr
);
337 chip
->shadow_dvo
= in_be32(®s
->simple_dvo
);
342 static const struct of_device_id mpc52xx_simple_gpiochip_match
[] = {
343 { .compatible
= "fsl,mpc5200-gpio", },
347 static struct platform_driver mpc52xx_simple_gpiochip_driver
= {
349 .name
= "mpc5200-gpio",
350 .of_match_table
= mpc52xx_simple_gpiochip_match
,
352 .probe
= mpc52xx_simple_gpiochip_probe
,
353 .remove
= mpc52xx_gpiochip_remove
,
356 static struct platform_driver
* const drivers
[] = {
357 &mpc52xx_wkup_gpiochip_driver
,
358 &mpc52xx_simple_gpiochip_driver
,
361 static int __init
mpc52xx_gpio_init(void)
363 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
366 /* Make sure we get initialised before anyone else tries to use us */
367 subsys_initcall(mpc52xx_gpio_init
);
369 static void __exit
mpc52xx_gpio_exit(void)
371 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
373 module_exit(mpc52xx_gpio_exit
);
375 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
376 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
377 MODULE_LICENSE("GPL v2");