blob: 0dad9a935eb5f1f34d57c134c7441c3d26dfacdf [file] [log] [blame]
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +10001/*
2 * MPC52xx gpio driver
3 *
4 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
5 *
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.
9 *
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.
14 *
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
18 */
19
20#include <linux/of.h>
21#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +100023#include <linux/of_gpio.h>
24#include <linux/io.h>
25#include <linux/of_platform.h>
26
27#include <asm/gpio.h>
28#include <asm/mpc52xx.h>
29#include <sysdev/fsl_soc.h>
30
31static DEFINE_SPINLOCK(gpio_lock);
32
33struct mpc52xx_gpiochip {
34 struct of_mm_gpio_chip mmchip;
35 unsigned int shadow_dvo;
36 unsigned int shadow_gpioe;
37 unsigned int shadow_ddr;
38};
39
40/*
41 * GPIO LIB API implementation for wakeup GPIOs.
42 *
43 * There's a maximum of 8 wakeup GPIOs. Which of these are available
44 * for use depends on your board setup.
45 *
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
54 *
55 */
56static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
57{
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;
61
62 ret = (in_8(&regs->wkup_ival) >> (7 - gpio)) & 1;
63
64 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
65
66 return ret;
67}
68
69static inline void
70__mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
71{
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;
76
77 if (val)
78 chip->shadow_dvo |= 1 << (7 - gpio);
79 else
80 chip->shadow_dvo &= ~(1 << (7 - gpio));
81
82 out_8(&regs->wkup_dvo, chip->shadow_dvo);
83}
84
85static void
86mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
87{
88 unsigned long flags;
89
90 spin_lock_irqsave(&gpio_lock, flags);
91
92 __mpc52xx_wkup_gpio_set(gc, gpio, val);
93
94 spin_unlock_irqrestore(&gpio_lock, flags);
95
96 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
97}
98
99static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
100{
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);
Al Viro93072452008-06-02 10:59:02 +0100104 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000105 unsigned long flags;
106
107 spin_lock_irqsave(&gpio_lock, flags);
108
109 /* set the direction */
110 chip->shadow_ddr &= ~(1 << (7 - gpio));
111 out_8(&regs->wkup_ddr, chip->shadow_ddr);
112
113 /* and enable the pin */
114 chip->shadow_gpioe |= 1 << (7 - gpio);
115 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
116
117 spin_unlock_irqrestore(&gpio_lock, flags);
118
119 return 0;
120}
121
122static int
123mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
124{
125 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
Al Viro93072452008-06-02 10:59:02 +0100126 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000127 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
128 struct mpc52xx_gpiochip, mmchip);
129 unsigned long flags;
130
131 spin_lock_irqsave(&gpio_lock, flags);
132
133 __mpc52xx_wkup_gpio_set(gc, gpio, val);
134
135 /* Then set direction */
136 chip->shadow_ddr |= 1 << (7 - gpio);
137 out_8(&regs->wkup_ddr, chip->shadow_ddr);
138
139 /* Finally enable the pin */
140 chip->shadow_gpioe |= 1 << (7 - gpio);
141 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
142
143 spin_unlock_irqrestore(&gpio_lock, flags);
144
145 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
146
147 return 0;
148}
149
Grant Likelya454dc52010-07-22 15:52:34 -0600150static int __devinit mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev,
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000151 const struct of_device_id *match)
152{
153 struct mpc52xx_gpiochip *chip;
Al Viro93072452008-06-02 10:59:02 +0100154 struct mpc52xx_gpio_wkup __iomem *regs;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600155 struct gpio_chip *gc;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000156 int ret;
157
158 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
159 if (!chip)
160 return -ENOMEM;
161
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600162 gc = &chip->mmchip.gc;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000163
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600164 gc->ngpio = 8;
165 gc->direction_input = mpc52xx_wkup_gpio_dir_in;
166 gc->direction_output = mpc52xx_wkup_gpio_dir_out;
167 gc->get = mpc52xx_wkup_gpio_get;
168 gc->set = mpc52xx_wkup_gpio_set;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000169
Grant Likely61c7a082010-04-13 16:12:29 -0700170 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000171 if (ret)
172 return ret;
173
174 regs = chip->mmchip.regs;
175 chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
176 chip->shadow_ddr = in_8(&regs->wkup_ddr);
177 chip->shadow_dvo = in_8(&regs->wkup_dvo);
178
179 return 0;
180}
181
Grant Likelya454dc52010-07-22 15:52:34 -0600182static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000183{
184 return -EBUSY;
185}
186
187static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
188 {
189 .compatible = "fsl,mpc5200-gpio-wkup",
190 },
191 {}
192};
193
194static struct of_platform_driver mpc52xx_wkup_gpiochip_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700195 .driver = {
196 .name = "gpio_wkup",
197 .owner = THIS_MODULE,
198 .of_match_table = mpc52xx_wkup_gpiochip_match,
199 },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000200 .probe = mpc52xx_wkup_gpiochip_probe,
201 .remove = mpc52xx_gpiochip_remove,
202};
203
204/*
205 * GPIO LIB API implementation for simple GPIOs
206 *
207 * There's a maximum of 32 simple GPIOs. Which of these are available
208 * for use depends on your board setup.
209 * The numbering reflects the bit numbering in the port registers:
210 *
211 * 0..1 > reserved
212 * 2..3 > IRDA
213 * 4..7 > ETHR
214 * 8..11 > reserved
215 * 12..15 > USB
216 * 16..17 > reserved
217 * 18..23 > PSC3
218 * 24..27 > PSC2
219 * 28..31 > PSC1
220 */
221static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
222{
223 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
224 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
225 unsigned int ret;
226
227 ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
228
229 return ret;
230}
231
232static inline void
233__mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
234{
235 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
236 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
237 struct mpc52xx_gpiochip, mmchip);
238 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
239
240 if (val)
241 chip->shadow_dvo |= 1 << (31 - gpio);
242 else
243 chip->shadow_dvo &= ~(1 << (31 - gpio));
244 out_be32(&regs->simple_dvo, chip->shadow_dvo);
245}
246
247static void
248mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
249{
250 unsigned long flags;
251
252 spin_lock_irqsave(&gpio_lock, flags);
253
254 __mpc52xx_simple_gpio_set(gc, gpio, val);
255
256 spin_unlock_irqrestore(&gpio_lock, flags);
257
258 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
259}
260
261static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
262{
263 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
264 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
265 struct mpc52xx_gpiochip, mmchip);
Al Viro93072452008-06-02 10:59:02 +0100266 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000267 unsigned long flags;
268
269 spin_lock_irqsave(&gpio_lock, flags);
270
271 /* set the direction */
272 chip->shadow_ddr &= ~(1 << (31 - gpio));
273 out_be32(&regs->simple_ddr, chip->shadow_ddr);
274
275 /* and enable the pin */
276 chip->shadow_gpioe |= 1 << (31 - gpio);
277 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
278
279 spin_unlock_irqrestore(&gpio_lock, flags);
280
281 return 0;
282}
283
284static int
285mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
286{
287 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
288 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
289 struct mpc52xx_gpiochip, mmchip);
Al Viro93072452008-06-02 10:59:02 +0100290 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000291 unsigned long flags;
292
293 spin_lock_irqsave(&gpio_lock, flags);
294
295 /* First set initial value */
296 __mpc52xx_simple_gpio_set(gc, gpio, val);
297
298 /* Then set direction */
299 chip->shadow_ddr |= 1 << (31 - gpio);
300 out_be32(&regs->simple_ddr, chip->shadow_ddr);
301
302 /* Finally enable the pin */
303 chip->shadow_gpioe |= 1 << (31 - gpio);
304 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
305
306 spin_unlock_irqrestore(&gpio_lock, flags);
307
308 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
309
310 return 0;
311}
312
Grant Likelya454dc52010-07-22 15:52:34 -0600313static int __devinit mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev,
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000314 const struct of_device_id *match)
315{
316 struct mpc52xx_gpiochip *chip;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600317 struct gpio_chip *gc;
Al Viro93072452008-06-02 10:59:02 +0100318 struct mpc52xx_gpio __iomem *regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000319 int ret;
320
321 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
322 if (!chip)
323 return -ENOMEM;
324
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600325 gc = &chip->mmchip.gc;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000326
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600327 gc->ngpio = 32;
328 gc->direction_input = mpc52xx_simple_gpio_dir_in;
329 gc->direction_output = mpc52xx_simple_gpio_dir_out;
330 gc->get = mpc52xx_simple_gpio_get;
331 gc->set = mpc52xx_simple_gpio_set;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000332
Grant Likely61c7a082010-04-13 16:12:29 -0700333 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000334 if (ret)
335 return ret;
336
337 regs = chip->mmchip.regs;
338 chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
339 chip->shadow_ddr = in_be32(&regs->simple_ddr);
340 chip->shadow_dvo = in_be32(&regs->simple_dvo);
341
342 return 0;
343}
344
345static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
346 {
347 .compatible = "fsl,mpc5200-gpio",
348 },
349 {}
350};
351
352static struct of_platform_driver mpc52xx_simple_gpiochip_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700353 .driver = {
354 .name = "gpio",
355 .owner = THIS_MODULE,
356 .of_match_table = mpc52xx_simple_gpiochip_match,
357 },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000358 .probe = mpc52xx_simple_gpiochip_probe,
359 .remove = mpc52xx_gpiochip_remove,
360};
361
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000362static int __init mpc52xx_gpio_init(void)
363{
364 if (of_register_platform_driver(&mpc52xx_wkup_gpiochip_driver))
365 printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
366
367 if (of_register_platform_driver(&mpc52xx_simple_gpiochip_driver))
368 printk(KERN_ERR "Unable to register simple GPIO driver\n");
369
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000370 return 0;
371}
372
373
374/* Make sure we get initialised before anyone else tries to use us */
375subsys_initcall(mpc52xx_gpio_init);
376
377/* No exit call at the moment as we cannot unregister of gpio chips */
378
379MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
380MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
381MODULE_LICENSE("GPL v2");
382