Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * MPC52xx gpio driver
  4 *
  5 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/of.h>
  9#include <linux/kernel.h>
 10#include <linux/slab.h>
 11#include <linux/of_gpio.h>
 12#include <linux/io.h>
 13#include <linux/of_platform.h>
 14#include <linux/module.h>
 15
 16#include <asm/mpc52xx.h>
 17#include <sysdev/fsl_soc.h>
 18
 19static DEFINE_SPINLOCK(gpio_lock);
 20
 21struct mpc52xx_gpiochip {
 22	struct of_mm_gpio_chip mmchip;
 23	unsigned int shadow_dvo;
 24	unsigned int shadow_gpioe;
 25	unsigned int shadow_ddr;
 26};
 27
 28/*
 29 * GPIO LIB API implementation for wakeup GPIOs.
 30 *
 31 * There's a maximum of 8 wakeup GPIOs. Which of these are available
 32 * for use depends on your board setup.
 33 *
 34 * 0 -> GPIO_WKUP_7
 35 * 1 -> GPIO_WKUP_6
 36 * 2 -> PSC6_1
 37 * 3 -> PSC6_0
 38 * 4 -> ETH_17
 39 * 5 -> PSC3_9
 40 * 6 -> PSC2_4
 41 * 7 -> PSC1_4
 42 *
 43 */
 44static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
 45{
 46	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
 47	struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
 48	unsigned int ret;
 49
 50	ret = (in_8(&regs->wkup_ival) >> (7 - gpio)) & 1;
 51
 52	pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
 53
 54	return ret;
 55}
 56
 57static inline void
 58__mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 59{
 60	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
 61	struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
 62	struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
 63
 64	if (val)
 65		chip->shadow_dvo |= 1 << (7 - gpio);
 66	else
 67		chip->shadow_dvo &= ~(1 << (7 - gpio));
 68
 69	out_8(&regs->wkup_dvo, chip->shadow_dvo);
 70}
 71
 72static void
 73mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 74{
 75	unsigned long flags;
 76
 77	spin_lock_irqsave(&gpio_lock, flags);
 78
 79	__mpc52xx_wkup_gpio_set(gc, gpio, val);
 80
 81	spin_unlock_irqrestore(&gpio_lock, flags);
 82
 83	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
 84}
 85
 86static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
 87{
 88	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
 89	struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
 90	struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
 91	unsigned long flags;
 92
 93	spin_lock_irqsave(&gpio_lock, flags);
 94
 95	/* set the direction */
 96	chip->shadow_ddr &= ~(1 << (7 - gpio));
 97	out_8(&regs->wkup_ddr, chip->shadow_ddr);
 98
 99	/* and enable the pin */
100	chip->shadow_gpioe |= 1 << (7 - gpio);
101	out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
102
103	spin_unlock_irqrestore(&gpio_lock, flags);
104
105	return 0;
106}
107
108static int
109mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
110{
111	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
112	struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
113	struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
114	unsigned long flags;
115
116	spin_lock_irqsave(&gpio_lock, flags);
117
118	__mpc52xx_wkup_gpio_set(gc, gpio, val);
119
120	/* Then set direction */
121	chip->shadow_ddr |= 1 << (7 - gpio);
122	out_8(&regs->wkup_ddr, chip->shadow_ddr);
123
124	/* Finally enable the pin */
125	chip->shadow_gpioe |= 1 << (7 - gpio);
126	out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
127
128	spin_unlock_irqrestore(&gpio_lock, flags);
129
130	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
131
132	return 0;
133}
134
135static int mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
136{
137	struct mpc52xx_gpiochip *chip;
138	struct mpc52xx_gpio_wkup __iomem *regs;
139	struct gpio_chip *gc;
140	int ret;
141
142	chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
143	if (!chip)
144		return -ENOMEM;
145
146	platform_set_drvdata(ofdev, chip);
147
148	gc = &chip->mmchip.gc;
149
150	gc->ngpio            = 8;
151	gc->direction_input  = mpc52xx_wkup_gpio_dir_in;
152	gc->direction_output = mpc52xx_wkup_gpio_dir_out;
153	gc->get              = mpc52xx_wkup_gpio_get;
154	gc->set              = mpc52xx_wkup_gpio_set;
155
156	ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
157	if (ret)
158		return ret;
159
160	regs = chip->mmchip.regs;
161	chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
162	chip->shadow_ddr = in_8(&regs->wkup_ddr);
163	chip->shadow_dvo = in_8(&regs->wkup_dvo);
164
165	return 0;
166}
167
168static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
169{
170	struct mpc52xx_gpiochip *chip = platform_get_drvdata(ofdev);
171
172	of_mm_gpiochip_remove(&chip->mmchip);
173
174	return 0;
175}
176
177static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
178	{ .compatible = "fsl,mpc5200-gpio-wkup", },
179	{}
180};
181
182static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
183	.driver = {
184		.name = "mpc5200-gpio-wkup",
185		.of_match_table = mpc52xx_wkup_gpiochip_match,
186	},
187	.probe = mpc52xx_wkup_gpiochip_probe,
188	.remove = mpc52xx_gpiochip_remove,
189};
190
191/*
192 * GPIO LIB API implementation for simple GPIOs
193 *
194 * There's a maximum of 32 simple GPIOs. Which of these are available
195 * for use depends on your board setup.
196 * The numbering reflects the bit numbering in the port registers:
197 *
198 *  0..1  > reserved
199 *  2..3  > IRDA
200 *  4..7  > ETHR
201 *  8..11 > reserved
202 * 12..15 > USB
203 * 16..17 > reserved
204 * 18..23 > PSC3
205 * 24..27 > PSC2
206 * 28..31 > PSC1
207 */
208static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
209{
210	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
211	struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
212	unsigned int ret;
213
214	ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
215
216	return ret;
217}
218
219static inline void
220__mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
221{
222	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
223	struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
224	struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
225
226	if (val)
227		chip->shadow_dvo |= 1 << (31 - gpio);
228	else
229		chip->shadow_dvo &= ~(1 << (31 - gpio));
230	out_be32(&regs->simple_dvo, chip->shadow_dvo);
231}
232
233static void
234mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
235{
236	unsigned long flags;
237
238	spin_lock_irqsave(&gpio_lock, flags);
239
240	__mpc52xx_simple_gpio_set(gc, gpio, val);
241
242	spin_unlock_irqrestore(&gpio_lock, flags);
243
244	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
245}
246
247static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
248{
249	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
250	struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
251	struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
252	unsigned long flags;
253
254	spin_lock_irqsave(&gpio_lock, flags);
255
256	/* set the direction */
257	chip->shadow_ddr &= ~(1 << (31 - gpio));
258	out_be32(&regs->simple_ddr, chip->shadow_ddr);
259
260	/* and enable the pin */
261	chip->shadow_gpioe |= 1 << (31 - gpio);
262	out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
263
264	spin_unlock_irqrestore(&gpio_lock, flags);
265
266	return 0;
267}
268
269static int
270mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
271{
272	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
273	struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
274	struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
275	unsigned long flags;
276
277	spin_lock_irqsave(&gpio_lock, flags);
278
279	/* First set initial value */
280	__mpc52xx_simple_gpio_set(gc, gpio, val);
281
282	/* Then set direction */
283	chip->shadow_ddr |= 1 << (31 - gpio);
284	out_be32(&regs->simple_ddr, chip->shadow_ddr);
285
286	/* Finally enable the pin */
287	chip->shadow_gpioe |= 1 << (31 - gpio);
288	out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
289
290	spin_unlock_irqrestore(&gpio_lock, flags);
291
292	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
293
294	return 0;
295}
296
297static int mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
298{
299	struct mpc52xx_gpiochip *chip;
300	struct gpio_chip *gc;
301	struct mpc52xx_gpio __iomem *regs;
302	int ret;
303
304	chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
305	if (!chip)
306		return -ENOMEM;
307
308	platform_set_drvdata(ofdev, chip);
309
310	gc = &chip->mmchip.gc;
311
312	gc->ngpio            = 32;
313	gc->direction_input  = mpc52xx_simple_gpio_dir_in;
314	gc->direction_output = mpc52xx_simple_gpio_dir_out;
315	gc->get              = mpc52xx_simple_gpio_get;
316	gc->set              = mpc52xx_simple_gpio_set;
317
318	ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
319	if (ret)
320		return ret;
321
322	regs = chip->mmchip.regs;
323	chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
324	chip->shadow_ddr = in_be32(&regs->simple_ddr);
325	chip->shadow_dvo = in_be32(&regs->simple_dvo);
326
327	return 0;
328}
329
330static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
331	{ .compatible = "fsl,mpc5200-gpio", },
332	{}
333};
334
335static struct platform_driver mpc52xx_simple_gpiochip_driver = {
336	.driver = {
337		.name = "mpc5200-gpio",
338		.of_match_table = mpc52xx_simple_gpiochip_match,
339	},
340	.probe = mpc52xx_simple_gpiochip_probe,
341	.remove = mpc52xx_gpiochip_remove,
342};
343
344static struct platform_driver * const drivers[] = {
345	&mpc52xx_wkup_gpiochip_driver,
346	&mpc52xx_simple_gpiochip_driver,
347};
348
349static int __init mpc52xx_gpio_init(void)
350{
351	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
352}
353
354/* Make sure we get initialised before anyone else tries to use us */
355subsys_initcall(mpc52xx_gpio_init);
356
357static void __exit mpc52xx_gpio_exit(void)
358{
359	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
360}
361module_exit(mpc52xx_gpio_exit);
362
363MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
364MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
365MODULE_LICENSE("GPL v2");
366
v4.10.11
 
  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>
 22#include <linux/slab.h>
 23#include <linux/of_gpio.h>
 24#include <linux/io.h>
 25#include <linux/of_platform.h>
 26#include <linux/module.h>
 27
 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 = gpiochip_get_data(gc);
 74	struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
 75
 76	if (val)
 77		chip->shadow_dvo |= 1 << (7 - gpio);
 78	else
 79		chip->shadow_dvo &= ~(1 << (7 - gpio));
 80
 81	out_8(&regs->wkup_dvo, chip->shadow_dvo);
 82}
 83
 84static void
 85mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 86{
 87	unsigned long flags;
 88
 89	spin_lock_irqsave(&gpio_lock, flags);
 90
 91	__mpc52xx_wkup_gpio_set(gc, gpio, val);
 92
 93	spin_unlock_irqrestore(&gpio_lock, flags);
 94
 95	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
 96}
 97
 98static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
 99{
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;
103	unsigned long flags;
104
105	spin_lock_irqsave(&gpio_lock, flags);
106
107	/* set the direction */
108	chip->shadow_ddr &= ~(1 << (7 - gpio));
109	out_8(&regs->wkup_ddr, chip->shadow_ddr);
110
111	/* and enable the pin */
112	chip->shadow_gpioe |= 1 << (7 - gpio);
113	out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
114
115	spin_unlock_irqrestore(&gpio_lock, flags);
116
117	return 0;
118}
119
120static int
121mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
122{
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);
126	unsigned long flags;
127
128	spin_lock_irqsave(&gpio_lock, flags);
129
130	__mpc52xx_wkup_gpio_set(gc, gpio, val);
131
132	/* Then set direction */
133	chip->shadow_ddr |= 1 << (7 - gpio);
134	out_8(&regs->wkup_ddr, chip->shadow_ddr);
135
136	/* Finally enable the pin */
137	chip->shadow_gpioe |= 1 << (7 - gpio);
138	out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
139
140	spin_unlock_irqrestore(&gpio_lock, flags);
141
142	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
143
144	return 0;
145}
146
147static int mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
148{
149	struct mpc52xx_gpiochip *chip;
150	struct mpc52xx_gpio_wkup __iomem *regs;
151	struct gpio_chip *gc;
152	int ret;
153
154	chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
155	if (!chip)
156		return -ENOMEM;
157
158	platform_set_drvdata(ofdev, chip);
159
160	gc = &chip->mmchip.gc;
161
162	gc->ngpio            = 8;
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;
167
168	ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
169	if (ret)
170		return ret;
171
172	regs = chip->mmchip.regs;
173	chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
174	chip->shadow_ddr = in_8(&regs->wkup_ddr);
175	chip->shadow_dvo = in_8(&regs->wkup_dvo);
176
177	return 0;
178}
179
180static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
181{
182	struct mpc52xx_gpiochip *chip = platform_get_drvdata(ofdev);
183
184	of_mm_gpiochip_remove(&chip->mmchip);
185
186	return 0;
187}
188
189static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
190	{ .compatible = "fsl,mpc5200-gpio-wkup", },
191	{}
192};
193
194static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
195	.driver = {
196		.name = "mpc5200-gpio-wkup",
197		.of_match_table = mpc52xx_wkup_gpiochip_match,
198	},
199	.probe = mpc52xx_wkup_gpiochip_probe,
200	.remove = mpc52xx_gpiochip_remove,
201};
202
203/*
204 * GPIO LIB API implementation for simple GPIOs
205 *
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:
209 *
210 *  0..1  > reserved
211 *  2..3  > IRDA
212 *  4..7  > ETHR
213 *  8..11 > reserved
214 * 12..15 > USB
215 * 16..17 > reserved
216 * 18..23 > PSC3
217 * 24..27 > PSC2
218 * 28..31 > PSC1
219 */
220static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
221{
222	struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
223	struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
224	unsigned int ret;
225
226	ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
227
228	return ret;
229}
230
231static inline void
232__mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
233{
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;
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 = gpiochip_get_data(gc);
263	struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
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 = gpiochip_get_data(gc);
286	struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
287	unsigned long flags;
288
289	spin_lock_irqsave(&gpio_lock, flags);
290
291	/* First set initial value */
292	__mpc52xx_simple_gpio_set(gc, gpio, val);
293
294	/* Then set direction */
295	chip->shadow_ddr |= 1 << (31 - gpio);
296	out_be32(&regs->simple_ddr, chip->shadow_ddr);
297
298	/* Finally enable the pin */
299	chip->shadow_gpioe |= 1 << (31 - gpio);
300	out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
301
302	spin_unlock_irqrestore(&gpio_lock, flags);
303
304	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
305
306	return 0;
307}
308
309static int mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
310{
311	struct mpc52xx_gpiochip *chip;
312	struct gpio_chip *gc;
313	struct mpc52xx_gpio __iomem *regs;
314	int ret;
315
316	chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
317	if (!chip)
318		return -ENOMEM;
319
320	platform_set_drvdata(ofdev, chip);
321
322	gc = &chip->mmchip.gc;
323
324	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;
329
330	ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
331	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[] = {
343	{ .compatible = "fsl,mpc5200-gpio", },
344	{}
345};
346
347static struct platform_driver mpc52xx_simple_gpiochip_driver = {
348	.driver = {
349		.name = "mpc5200-gpio",
350		.of_match_table = mpc52xx_simple_gpiochip_match,
351	},
352	.probe = mpc52xx_simple_gpiochip_probe,
353	.remove = mpc52xx_gpiochip_remove,
354};
355
356static struct platform_driver * const drivers[] = {
357	&mpc52xx_wkup_gpiochip_driver,
358	&mpc52xx_simple_gpiochip_driver,
359};
360
361static int __init mpc52xx_gpio_init(void)
362{
363	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
364}
365
366/* Make sure we get initialised before anyone else tries to use us */
367subsys_initcall(mpc52xx_gpio_init);
368
369static void __exit mpc52xx_gpio_exit(void)
370{
371	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
372}
373module_exit(mpc52xx_gpio_exit);
374
375MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
376MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
377MODULE_LICENSE("GPL v2");
378