rbd: convert to blk-mq
[deliverable/linux.git] / drivers / gpio / gpio-mpc5200.c
CommitLineData
3cd2550c 1/*
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>
5a0e3ad6 22#include <linux/slab.h>
3cd2550c 23#include <linux/of_gpio.h>
24#include <linux/io.h>
25#include <linux/of_platform.h>
bb207ef1 26#include <linux/module.h>
3cd2550c 27
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);
93072457 105 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
3cd2550c 106 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);
93072457 127 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
3cd2550c 128 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
3836309d 151static int mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
3cd2550c 152{
153 struct mpc52xx_gpiochip *chip;
93072457 154 struct mpc52xx_gpio_wkup __iomem *regs;
a19e3da5 155 struct gpio_chip *gc;
3cd2550c 156 int ret;
157
158 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
159 if (!chip)
160 return -ENOMEM;
161
a19e3da5 162 gc = &chip->mmchip.gc;
3cd2550c 163
a19e3da5
AV
164 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;
3cd2550c 169
61c7a080 170 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
3cd2550c 171 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
a454dc50 182static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
3cd2550c 183{
184 return -EBUSY;
185}
186
187static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
6eae1ace 188 { .compatible = "fsl,mpc5200-gpio-wkup", },
3cd2550c 189 {}
190};
191
00006124 192static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
4018294b 193 .driver = {
6eae1ace 194 .name = "mpc5200-gpio-wkup",
4018294b
GL
195 .of_match_table = mpc52xx_wkup_gpiochip_match,
196 },
3cd2550c 197 .probe = mpc52xx_wkup_gpiochip_probe,
198 .remove = mpc52xx_gpiochip_remove,
199};
200
201/*
202 * GPIO LIB API implementation for simple GPIOs
203 *
204 * There's a maximum of 32 simple GPIOs. Which of these are available
205 * for use depends on your board setup.
206 * The numbering reflects the bit numbering in the port registers:
207 *
208 * 0..1 > reserved
209 * 2..3 > IRDA
210 * 4..7 > ETHR
211 * 8..11 > reserved
212 * 12..15 > USB
213 * 16..17 > reserved
214 * 18..23 > PSC3
215 * 24..27 > PSC2
216 * 28..31 > PSC1
217 */
218static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
219{
220 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
221 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
222 unsigned int ret;
223
224 ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
225
226 return ret;
227}
228
229static inline void
230__mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
231{
232 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
233 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
234 struct mpc52xx_gpiochip, mmchip);
235 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
236
237 if (val)
238 chip->shadow_dvo |= 1 << (31 - gpio);
239 else
240 chip->shadow_dvo &= ~(1 << (31 - gpio));
241 out_be32(&regs->simple_dvo, chip->shadow_dvo);
242}
243
244static void
245mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
246{
247 unsigned long flags;
248
249 spin_lock_irqsave(&gpio_lock, flags);
250
251 __mpc52xx_simple_gpio_set(gc, gpio, val);
252
253 spin_unlock_irqrestore(&gpio_lock, flags);
254
255 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
256}
257
258static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
259{
260 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
261 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
262 struct mpc52xx_gpiochip, mmchip);
93072457 263 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
3cd2550c 264 unsigned long flags;
265
266 spin_lock_irqsave(&gpio_lock, flags);
267
268 /* set the direction */
269 chip->shadow_ddr &= ~(1 << (31 - gpio));
270 out_be32(&regs->simple_ddr, chip->shadow_ddr);
271
272 /* and enable the pin */
273 chip->shadow_gpioe |= 1 << (31 - gpio);
274 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
275
276 spin_unlock_irqrestore(&gpio_lock, flags);
277
278 return 0;
279}
280
281static int
282mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
283{
284 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
285 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
286 struct mpc52xx_gpiochip, mmchip);
93072457 287 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
3cd2550c 288 unsigned long flags;
289
290 spin_lock_irqsave(&gpio_lock, flags);
291
292 /* First set initial value */
293 __mpc52xx_simple_gpio_set(gc, gpio, val);
294
295 /* Then set direction */
296 chip->shadow_ddr |= 1 << (31 - gpio);
297 out_be32(&regs->simple_ddr, chip->shadow_ddr);
298
299 /* Finally enable the pin */
300 chip->shadow_gpioe |= 1 << (31 - gpio);
301 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
302
303 spin_unlock_irqrestore(&gpio_lock, flags);
304
305 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
306
307 return 0;
308}
309
3836309d 310static int mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
3cd2550c 311{
312 struct mpc52xx_gpiochip *chip;
a19e3da5 313 struct gpio_chip *gc;
93072457 314 struct mpc52xx_gpio __iomem *regs;
3cd2550c 315 int ret;
316
317 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
318 if (!chip)
319 return -ENOMEM;
320
a19e3da5 321 gc = &chip->mmchip.gc;
3cd2550c 322
a19e3da5
AV
323 gc->ngpio = 32;
324 gc->direction_input = mpc52xx_simple_gpio_dir_in;
325 gc->direction_output = mpc52xx_simple_gpio_dir_out;
326 gc->get = mpc52xx_simple_gpio_get;
327 gc->set = mpc52xx_simple_gpio_set;
3cd2550c 328
61c7a080 329 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
3cd2550c 330 if (ret)
331 return ret;
332
333 regs = chip->mmchip.regs;
334 chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
335 chip->shadow_ddr = in_be32(&regs->simple_ddr);
336 chip->shadow_dvo = in_be32(&regs->simple_dvo);
337
338 return 0;
339}
340
341static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
6eae1ace 342 { .compatible = "fsl,mpc5200-gpio", },
3cd2550c 343 {}
344};
345
00006124 346static struct platform_driver mpc52xx_simple_gpiochip_driver = {
4018294b 347 .driver = {
6eae1ace 348 .name = "mpc5200-gpio",
4018294b
GL
349 .of_match_table = mpc52xx_simple_gpiochip_match,
350 },
3cd2550c 351 .probe = mpc52xx_simple_gpiochip_probe,
352 .remove = mpc52xx_gpiochip_remove,
353};
354
3cd2550c 355static int __init mpc52xx_gpio_init(void)
356{
00006124 357 if (platform_driver_register(&mpc52xx_wkup_gpiochip_driver))
3cd2550c 358 printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
359
00006124 360 if (platform_driver_register(&mpc52xx_simple_gpiochip_driver))
3cd2550c 361 printk(KERN_ERR "Unable to register simple GPIO driver\n");
362
3cd2550c 363 return 0;
364}
365
366
367/* Make sure we get initialised before anyone else tries to use us */
368subsys_initcall(mpc52xx_gpio_init);
369
370/* No exit call at the moment as we cannot unregister of gpio chips */
371
372MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
373MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
374MODULE_LICENSE("GPL v2");
375
This page took 0.421462 seconds and 5 git commands to generate.