blob: 2c7cef367fc0e85664231cedd0b0b91cc6ec1062 [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>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -040026#include <linux/module.h>
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +100027
28#include <asm/gpio.h>
29#include <asm/mpc52xx.h>
30#include <sysdev/fsl_soc.h>
31
32static DEFINE_SPINLOCK(gpio_lock);
33
34struct mpc52xx_gpiochip {
35 struct of_mm_gpio_chip mmchip;
36 unsigned int shadow_dvo;
37 unsigned int shadow_gpioe;
38 unsigned int shadow_ddr;
39};
40
41/*
42 * GPIO LIB API implementation for wakeup GPIOs.
43 *
44 * There's a maximum of 8 wakeup GPIOs. Which of these are available
45 * for use depends on your board setup.
46 *
47 * 0 -> GPIO_WKUP_7
48 * 1 -> GPIO_WKUP_6
49 * 2 -> PSC6_1
50 * 3 -> PSC6_0
51 * 4 -> ETH_17
52 * 5 -> PSC3_9
53 * 6 -> PSC2_4
54 * 7 -> PSC1_4
55 *
56 */
57static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
58{
59 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
60 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
61 unsigned int ret;
62
63 ret = (in_8(&regs->wkup_ival) >> (7 - gpio)) & 1;
64
65 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
66
67 return ret;
68}
69
70static inline void
71__mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
72{
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;
77
78 if (val)
79 chip->shadow_dvo |= 1 << (7 - gpio);
80 else
81 chip->shadow_dvo &= ~(1 << (7 - gpio));
82
83 out_8(&regs->wkup_dvo, chip->shadow_dvo);
84}
85
86static void
87mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
88{
89 unsigned long flags;
90
91 spin_lock_irqsave(&gpio_lock, flags);
92
93 __mpc52xx_wkup_gpio_set(gc, gpio, val);
94
95 spin_unlock_irqrestore(&gpio_lock, flags);
96
97 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
98}
99
100static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
101{
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);
Al Viro93072452008-06-02 10:59:02 +0100105 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000106 unsigned long flags;
107
108 spin_lock_irqsave(&gpio_lock, flags);
109
110 /* set the direction */
111 chip->shadow_ddr &= ~(1 << (7 - gpio));
112 out_8(&regs->wkup_ddr, chip->shadow_ddr);
113
114 /* and enable the pin */
115 chip->shadow_gpioe |= 1 << (7 - gpio);
116 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
117
118 spin_unlock_irqrestore(&gpio_lock, flags);
119
120 return 0;
121}
122
123static int
124mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
125{
126 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
Al Viro93072452008-06-02 10:59:02 +0100127 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000128 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
129 struct mpc52xx_gpiochip, mmchip);
130 unsigned long flags;
131
132 spin_lock_irqsave(&gpio_lock, flags);
133
134 __mpc52xx_wkup_gpio_set(gc, gpio, val);
135
136 /* Then set direction */
137 chip->shadow_ddr |= 1 << (7 - gpio);
138 out_8(&regs->wkup_ddr, chip->shadow_ddr);
139
140 /* Finally enable the pin */
141 chip->shadow_gpioe |= 1 << (7 - gpio);
142 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
143
144 spin_unlock_irqrestore(&gpio_lock, flags);
145
146 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
147
148 return 0;
149}
150
Grant Likely00006122011-02-22 19:59:54 -0700151static int __devinit mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000152{
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[] = {
Grant Likely6eae1ac2011-07-06 11:54:19 -0600188 { .compatible = "fsl,mpc5200-gpio-wkup", },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000189 {}
190};
191
Grant Likely00006122011-02-22 19:59:54 -0700192static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700193 .driver = {
Grant Likely6eae1ac2011-07-06 11:54:19 -0600194 .name = "mpc5200-gpio-wkup",
Grant Likely40182942010-04-13 16:13:02 -0700195 .owner = THIS_MODULE,
196 .of_match_table = mpc52xx_wkup_gpiochip_match,
197 },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000198 .probe = mpc52xx_wkup_gpiochip_probe,
199 .remove = mpc52xx_gpiochip_remove,
200};
201
202/*
203 * GPIO LIB API implementation for simple GPIOs
204 *
205 * There's a maximum of 32 simple GPIOs. Which of these are available
206 * for use depends on your board setup.
207 * The numbering reflects the bit numbering in the port registers:
208 *
209 * 0..1 > reserved
210 * 2..3 > IRDA
211 * 4..7 > ETHR
212 * 8..11 > reserved
213 * 12..15 > USB
214 * 16..17 > reserved
215 * 18..23 > PSC3
216 * 24..27 > PSC2
217 * 28..31 > PSC1
218 */
219static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
220{
221 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
222 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
223 unsigned int ret;
224
225 ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
226
227 return ret;
228}
229
230static inline void
231__mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
232{
233 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
234 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
235 struct mpc52xx_gpiochip, mmchip);
236 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
237
238 if (val)
239 chip->shadow_dvo |= 1 << (31 - gpio);
240 else
241 chip->shadow_dvo &= ~(1 << (31 - gpio));
242 out_be32(&regs->simple_dvo, chip->shadow_dvo);
243}
244
245static void
246mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
247{
248 unsigned long flags;
249
250 spin_lock_irqsave(&gpio_lock, flags);
251
252 __mpc52xx_simple_gpio_set(gc, gpio, val);
253
254 spin_unlock_irqrestore(&gpio_lock, flags);
255
256 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
257}
258
259static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
260{
261 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
262 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
263 struct mpc52xx_gpiochip, mmchip);
Al Viro93072452008-06-02 10:59:02 +0100264 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000265 unsigned long flags;
266
267 spin_lock_irqsave(&gpio_lock, flags);
268
269 /* set the direction */
270 chip->shadow_ddr &= ~(1 << (31 - gpio));
271 out_be32(&regs->simple_ddr, chip->shadow_ddr);
272
273 /* and enable the pin */
274 chip->shadow_gpioe |= 1 << (31 - gpio);
275 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
276
277 spin_unlock_irqrestore(&gpio_lock, flags);
278
279 return 0;
280}
281
282static int
283mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
284{
285 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
286 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
287 struct mpc52xx_gpiochip, mmchip);
Al Viro93072452008-06-02 10:59:02 +0100288 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000289 unsigned long flags;
290
291 spin_lock_irqsave(&gpio_lock, flags);
292
293 /* First set initial value */
294 __mpc52xx_simple_gpio_set(gc, gpio, val);
295
296 /* Then set direction */
297 chip->shadow_ddr |= 1 << (31 - gpio);
298 out_be32(&regs->simple_ddr, chip->shadow_ddr);
299
300 /* Finally enable the pin */
301 chip->shadow_gpioe |= 1 << (31 - gpio);
302 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
303
304 spin_unlock_irqrestore(&gpio_lock, flags);
305
306 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
307
308 return 0;
309}
310
Grant Likely00006122011-02-22 19:59:54 -0700311static int __devinit mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000312{
313 struct mpc52xx_gpiochip *chip;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600314 struct gpio_chip *gc;
Al Viro93072452008-06-02 10:59:02 +0100315 struct mpc52xx_gpio __iomem *regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000316 int ret;
317
318 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
319 if (!chip)
320 return -ENOMEM;
321
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600322 gc = &chip->mmchip.gc;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000323
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600324 gc->ngpio = 32;
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;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000329
Grant Likely61c7a082010-04-13 16:12:29 -0700330 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000331 if (ret)
332 return ret;
333
334 regs = chip->mmchip.regs;
335 chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
336 chip->shadow_ddr = in_be32(&regs->simple_ddr);
337 chip->shadow_dvo = in_be32(&regs->simple_dvo);
338
339 return 0;
340}
341
342static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
Grant Likely6eae1ac2011-07-06 11:54:19 -0600343 { .compatible = "fsl,mpc5200-gpio", },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000344 {}
345};
346
Grant Likely00006122011-02-22 19:59:54 -0700347static struct platform_driver mpc52xx_simple_gpiochip_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700348 .driver = {
Grant Likely6eae1ac2011-07-06 11:54:19 -0600349 .name = "mpc5200-gpio",
Grant Likely40182942010-04-13 16:13:02 -0700350 .owner = THIS_MODULE,
351 .of_match_table = mpc52xx_simple_gpiochip_match,
352 },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000353 .probe = mpc52xx_simple_gpiochip_probe,
354 .remove = mpc52xx_gpiochip_remove,
355};
356
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000357static int __init mpc52xx_gpio_init(void)
358{
Grant Likely00006122011-02-22 19:59:54 -0700359 if (platform_driver_register(&mpc52xx_wkup_gpiochip_driver))
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000360 printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
361
Grant Likely00006122011-02-22 19:59:54 -0700362 if (platform_driver_register(&mpc52xx_simple_gpiochip_driver))
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000363 printk(KERN_ERR "Unable to register simple GPIO driver\n");
364
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000365 return 0;
366}
367
368
369/* Make sure we get initialised before anyone else tries to use us */
370subsys_initcall(mpc52xx_gpio_init);
371
372/* No exit call at the moment as we cannot unregister of gpio chips */
373
374MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
375MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
376MODULE_LICENSE("GPL v2");
377