blob: ecdb27ad9f1bbbb746c7693b3d8fd078b03ef529 [file] [log] [blame]
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +02001/*
Liu Gang42178e22016-02-03 19:27:34 +08002 * GPIOs on MPC512x/8349/8572/8610/QorIQ and compatible
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +02003 *
4 * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
Liu Gang42178e22016-02-03 19:27:34 +08005 * Copyright (C) 2016 Freescale Semiconductor Inc.
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +02006 *
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/spinlock.h>
15#include <linux/io.h>
16#include <linux/of.h>
17#include <linux/of_gpio.h>
Liu Gang42178e22016-02-03 19:27:34 +080018#include <linux/of_address.h>
Rob Herring5af50732013-09-17 14:28:33 -050019#include <linux/of_irq.h>
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +010020#include <linux/of_platform.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Peter Korsgaard345e5c82010-01-07 17:57:46 +010022#include <linux/irq.h>
Liu Gang42178e22016-02-03 19:27:34 +080023#include <linux/gpio/driver.h>
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020024
25#define MPC8XXX_GPIO_PINS 32
26
27#define GPIO_DIR 0x00
28#define GPIO_ODR 0x04
29#define GPIO_DAT 0x08
30#define GPIO_IER 0x0c
31#define GPIO_IMR 0x10
32#define GPIO_ICR 0x14
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +020033#define GPIO_ICR2 0x18
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020034
35struct mpc8xxx_gpio_chip {
Liu Gang42178e22016-02-03 19:27:34 +080036 struct gpio_chip gc;
37 void __iomem *regs;
Alexander Stein50593612015-07-21 15:54:30 +020038 raw_spinlock_t lock;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020039
Liu Gang42178e22016-02-03 19:27:34 +080040 unsigned long (*read_reg)(void __iomem *reg);
41 void (*write_reg)(void __iomem *reg, unsigned long data);
42
43 int (*direction_output)(struct gpio_chip *chip,
44 unsigned offset, int value);
45
Grant Likelybae1d8f2012-02-14 14:06:50 -070046 struct irq_domain *irq;
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +010047 unsigned int irqn;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020048};
49
Felix Radenskyc1a676d2009-08-12 08:57:39 +030050/* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
51 * defined as output cannot be determined by reading GPDAT register,
52 * so we use shadow data register instead. The status of input pins
53 * is determined by reading GPDAT register.
54 */
55static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
56{
57 u32 val;
Linus Walleij709d71a2015-12-07 10:34:28 +010058 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Liu Gang1aeef302013-11-22 16:12:40 +080059 u32 out_mask, out_shadow;
Felix Radenskyc1a676d2009-08-12 08:57:39 +030060
Liu Gang42178e22016-02-03 19:27:34 +080061 out_mask = mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_DIR);
62 val = mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
63 out_shadow = gc->bgpio_data & out_mask;
Felix Radenskyc1a676d2009-08-12 08:57:39 +030064
Liu Gang42178e22016-02-03 19:27:34 +080065 return !!((val | out_shadow) & gc->pin2mask(gc, gpio));
Felix Radenskyc1a676d2009-08-12 08:57:39 +030066}
67
Liu Gang42178e22016-02-03 19:27:34 +080068static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
69 unsigned int gpio, int val)
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020070{
Linus Walleij709d71a2015-12-07 10:34:28 +010071 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Wolfram Sang28538df2011-12-13 10:12:48 +010072 /* GPIO 28..31 are input only on MPC5121 */
73 if (gpio >= 28)
74 return -EINVAL;
75
Liu Gang42178e22016-02-03 19:27:34 +080076 return mpc8xxx_gc->direction_output(gc, gpio, val);
Wolfram Sang28538df2011-12-13 10:12:48 +010077}
78
Liu Gang42178e22016-02-03 19:27:34 +080079static int mpc5125_gpio_dir_out(struct gpio_chip *gc,
80 unsigned int gpio, int val)
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +020081{
Liu Gang42178e22016-02-03 19:27:34 +080082 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +020083 /* GPIO 0..3 are input only on MPC5125 */
84 if (gpio <= 3)
85 return -EINVAL;
86
Liu Gang42178e22016-02-03 19:27:34 +080087 return mpc8xxx_gc->direction_output(gc, gpio, val);
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +020088}
89
Peter Korsgaard345e5c82010-01-07 17:57:46 +010090static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
91{
Linus Walleij709d71a2015-12-07 10:34:28 +010092 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +010093
94 if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
95 return irq_create_mapping(mpc8xxx_gc->irq, offset);
96 else
97 return -ENXIO;
98}
99
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200100static void mpc8xxx_gpio_irq_cascade(struct irq_desc *desc)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100101{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100102 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
Felix Radenskycfadd832011-10-11 10:24:21 +0200103 struct irq_chip *chip = irq_desc_get_chip(desc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100104 unsigned int mask;
105
Liu Gang42178e22016-02-03 19:27:34 +0800106 mask = mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_IER)
107 & mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100108 if (mask)
109 generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
110 32 - ffs(mask)));
Thomas Gleixnerd6de85e2012-05-03 12:22:06 +0200111 if (chip->irq_eoi)
112 chip->irq_eoi(&desc->irq_data);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100113}
114
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000115static void mpc8xxx_irq_unmask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100116{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000117 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800118 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100119 unsigned long flags;
120
Alexander Stein50593612015-07-21 15:54:30 +0200121 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100122
Liu Gang42178e22016-02-03 19:27:34 +0800123 mpc8xxx_gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
124 mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
125 | gc->pin2mask(gc, irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100126
Alexander Stein50593612015-07-21 15:54:30 +0200127 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100128}
129
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000130static void mpc8xxx_irq_mask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100131{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000132 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800133 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100134 unsigned long flags;
135
Alexander Stein50593612015-07-21 15:54:30 +0200136 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100137
Liu Gang42178e22016-02-03 19:27:34 +0800138 mpc8xxx_gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
139 mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
140 & ~(gc->pin2mask(gc, irqd_to_hwirq(d))));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100141
Alexander Stein50593612015-07-21 15:54:30 +0200142 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100143}
144
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000145static void mpc8xxx_irq_ack(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100146{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000147 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800148 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100149
Liu Gang42178e22016-02-03 19:27:34 +0800150 mpc8xxx_gc->write_reg(mpc8xxx_gc->regs + GPIO_IER,
151 gc->pin2mask(gc, irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100152}
153
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000154static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100155{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000156 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800157 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100158 unsigned long flags;
159
160 switch (flow_type) {
161 case IRQ_TYPE_EDGE_FALLING:
Alexander Stein50593612015-07-21 15:54:30 +0200162 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Liu Gang42178e22016-02-03 19:27:34 +0800163 mpc8xxx_gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
164 mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
165 | gc->pin2mask(gc, irqd_to_hwirq(d)));
Alexander Stein50593612015-07-21 15:54:30 +0200166 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100167 break;
168
169 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200170 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Liu Gang42178e22016-02-03 19:27:34 +0800171 mpc8xxx_gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
172 mpc8xxx_gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
173 & ~(gc->pin2mask(gc, irqd_to_hwirq(d))));
Alexander Stein50593612015-07-21 15:54:30 +0200174 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100175 break;
176
177 default:
178 return -EINVAL;
179 }
180
181 return 0;
182}
183
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000184static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200185{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000186 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Grant Likely476eb492011-05-04 15:02:15 +1000187 unsigned long gpio = irqd_to_hwirq(d);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200188 void __iomem *reg;
189 unsigned int shift;
190 unsigned long flags;
191
192 if (gpio < 16) {
Liu Gang42178e22016-02-03 19:27:34 +0800193 reg = mpc8xxx_gc->regs + GPIO_ICR;
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200194 shift = (15 - gpio) * 2;
195 } else {
Liu Gang42178e22016-02-03 19:27:34 +0800196 reg = mpc8xxx_gc->regs + GPIO_ICR2;
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200197 shift = (15 - (gpio % 16)) * 2;
198 }
199
200 switch (flow_type) {
201 case IRQ_TYPE_EDGE_FALLING:
202 case IRQ_TYPE_LEVEL_LOW:
Alexander Stein50593612015-07-21 15:54:30 +0200203 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Liu Gang42178e22016-02-03 19:27:34 +0800204 mpc8xxx_gc->write_reg(reg,
205 (mpc8xxx_gc->read_reg(reg) & ~(3 << shift))
206 | (2 << shift));
Alexander Stein50593612015-07-21 15:54:30 +0200207 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200208 break;
209
210 case IRQ_TYPE_EDGE_RISING:
211 case IRQ_TYPE_LEVEL_HIGH:
Alexander Stein50593612015-07-21 15:54:30 +0200212 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Liu Gang42178e22016-02-03 19:27:34 +0800213 mpc8xxx_gc->write_reg(reg,
214 (mpc8xxx_gc->read_reg(reg) & ~(3 << shift))
215 | (1 << shift));
Alexander Stein50593612015-07-21 15:54:30 +0200216 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200217 break;
218
219 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200220 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Liu Gang42178e22016-02-03 19:27:34 +0800221 mpc8xxx_gc->write_reg(reg,
222 (mpc8xxx_gc->read_reg(reg) & ~(3 << shift)));
Alexander Stein50593612015-07-21 15:54:30 +0200223 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200224 break;
225
226 default:
227 return -EINVAL;
228 }
229
230 return 0;
231}
232
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100233static struct irq_chip mpc8xxx_irq_chip = {
234 .name = "mpc8xxx-gpio",
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000235 .irq_unmask = mpc8xxx_irq_unmask,
236 .irq_mask = mpc8xxx_irq_mask,
237 .irq_ack = mpc8xxx_irq_ack,
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200238 /* this might get overwritten in mpc8xxx_probe() */
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000239 .irq_set_type = mpc8xxx_irq_set_type,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100240};
241
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200242static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
243 irq_hw_number_t hwirq)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100244{
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200245 irq_set_chip_data(irq, h->host_data);
246 irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_level_irq);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100247
248 return 0;
249}
250
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900251static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100252 .map = mpc8xxx_gpio_irq_map,
Grant Likelyff8c3ab2012-01-24 17:09:13 -0700253 .xlate = irq_domain_xlate_twocell,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100254};
255
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200256struct mpc8xxx_gpio_devtype {
257 int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
258 int (*gpio_get)(struct gpio_chip *, unsigned int);
259 int (*irq_set_type)(struct irq_data *, unsigned int);
260};
261
262static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
263 .gpio_dir_out = mpc5121_gpio_dir_out,
264 .irq_set_type = mpc512x_irq_set_type,
265};
266
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200267static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
268 .gpio_dir_out = mpc5125_gpio_dir_out,
269 .irq_set_type = mpc512x_irq_set_type,
270};
271
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200272static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
273 .gpio_get = mpc8572_gpio_get,
274};
275
276static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200277 .irq_set_type = mpc8xxx_irq_set_type,
278};
279
Uwe Kleine-König4183afe2015-07-16 21:08:21 +0200280static const struct of_device_id mpc8xxx_gpio_ids[] = {
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200281 { .compatible = "fsl,mpc8349-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200282 { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200283 { .compatible = "fsl,mpc8610-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200284 { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200285 { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
Kumar Gala15a51482011-10-22 16:20:42 -0500286 { .compatible = "fsl,pq3-gpio", },
Anatolij Gustschind1dcfbb2011-01-08 16:51:16 +0100287 { .compatible = "fsl,qoriq-gpio", },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200288 {}
289};
290
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100291static int mpc8xxx_probe(struct platform_device *pdev)
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200292{
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100293 struct device_node *np = pdev->dev.of_node;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200294 struct mpc8xxx_gpio_chip *mpc8xxx_gc;
Liu Gang42178e22016-02-03 19:27:34 +0800295 struct gpio_chip *gc;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200296 const struct mpc8xxx_gpio_devtype *devtype =
297 of_device_get_match_data(&pdev->dev);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200298 int ret;
299
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100300 mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
301 if (!mpc8xxx_gc)
302 return -ENOMEM;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200303
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100304 platform_set_drvdata(pdev, mpc8xxx_gc);
305
Alexander Stein50593612015-07-21 15:54:30 +0200306 raw_spin_lock_init(&mpc8xxx_gc->lock);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200307
Liu Gang42178e22016-02-03 19:27:34 +0800308 mpc8xxx_gc->regs = of_iomap(np, 0);
309 if (!mpc8xxx_gc->regs)
310 return -ENOMEM;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200311
Liu Gang42178e22016-02-03 19:27:34 +0800312 gc = &mpc8xxx_gc->gc;
313
314 if (of_property_read_bool(np, "little-endian")) {
315 ret = bgpio_init(gc, &pdev->dev, 4,
316 mpc8xxx_gc->regs + GPIO_DAT,
317 NULL, NULL,
318 mpc8xxx_gc->regs + GPIO_DIR, NULL,
319 BGPIOF_BIG_ENDIAN);
320 if (ret)
321 goto err;
322 dev_dbg(&pdev->dev, "GPIO registers are LITTLE endian\n");
323 } else {
324 ret = bgpio_init(gc, &pdev->dev, 4,
325 mpc8xxx_gc->regs + GPIO_DAT,
326 NULL, NULL,
327 mpc8xxx_gc->regs + GPIO_DIR, NULL,
328 BGPIOF_BIG_ENDIAN
329 | BGPIOF_BIG_ENDIAN_BYTE_ORDER);
330 if (ret)
331 goto err;
332 dev_dbg(&pdev->dev, "GPIO registers are BIG endian\n");
333 }
334
335 mpc8xxx_gc->read_reg = gc->read_reg;
336 mpc8xxx_gc->write_reg = gc->write_reg;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200337
338 if (!devtype)
339 devtype = &mpc8xxx_gpio_devtype_default;
340
341 /*
342 * It's assumed that only a single type of gpio controller is available
343 * on the current machine, so overwriting global data is fine.
344 */
345 mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;
346
Liu Gang42178e22016-02-03 19:27:34 +0800347 gc->direction_output = devtype->gpio_dir_out ?: gc->direction_output;
348 gc->get = devtype->gpio_get ?: gc->get;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100349 gc->to_irq = mpc8xxx_gpio_to_irq;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200350
Liu Gang42178e22016-02-03 19:27:34 +0800351 mpc8xxx_gc->direction_output = gc->direction_output;
352
353 ret = gpiochip_add_data(gc, mpc8xxx_gc);
354 if (ret) {
355 pr_err("%s: GPIO chip registration failed with status %d\n",
356 np->full_name, ret);
357 goto err;
358 }
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200359
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100360 mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
Liu Gang42178e22016-02-03 19:27:34 +0800361 if (!mpc8xxx_gc->irqn)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100362 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100363
Grant Likelya8db8cf2012-02-14 14:06:54 -0700364 mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
365 &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100366 if (!mpc8xxx_gc->irq)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100367 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100368
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100369 /* ack and mask all irqs */
Liu Gang42178e22016-02-03 19:27:34 +0800370 mpc8xxx_gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
371 mpc8xxx_gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100372
Thomas Gleixner05379812015-06-21 21:10:46 +0200373 irq_set_chained_handler_and_data(mpc8xxx_gc->irqn,
374 mpc8xxx_gpio_irq_cascade, mpc8xxx_gc);
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100375 return 0;
Liu Gang42178e22016-02-03 19:27:34 +0800376err:
377 iounmap(mpc8xxx_gc->regs);
378 return ret;
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100379}
380
381static int mpc8xxx_remove(struct platform_device *pdev)
382{
383 struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);
384
385 if (mpc8xxx_gc->irq) {
Thomas Gleixner05379812015-06-21 21:10:46 +0200386 irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100387 irq_domain_remove(mpc8xxx_gc->irq);
388 }
389
Liu Gang42178e22016-02-03 19:27:34 +0800390 gpiochip_remove(&mpc8xxx_gc->gc);
391 iounmap(mpc8xxx_gc->regs);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100392
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200393 return 0;
394}
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100395
396static struct platform_driver mpc8xxx_plat_driver = {
397 .probe = mpc8xxx_probe,
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100398 .remove = mpc8xxx_remove,
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100399 .driver = {
400 .name = "gpio-mpc8xxx",
401 .of_match_table = mpc8xxx_gpio_ids,
402 },
403};
404
405static int __init mpc8xxx_init(void)
406{
407 return platform_driver_register(&mpc8xxx_plat_driver);
408}
409
410arch_initcall(mpc8xxx_init);