blob: f1e164cecff80375ddcfa89017d15fe2af277886 [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>
Linus Walleijb3222f72017-10-20 16:08:12 +020024#include <linux/bitops.h>
Song Hui698b8ee2019-10-11 08:56:43 +080025#include <linux/interrupt.h>
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020026
27#define MPC8XXX_GPIO_PINS 32
28
29#define GPIO_DIR 0x00
30#define GPIO_ODR 0x04
31#define GPIO_DAT 0x08
32#define GPIO_IER 0x0c
33#define GPIO_IMR 0x10
34#define GPIO_ICR 0x14
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +020035#define GPIO_ICR2 0x18
Song Huibd4bd332019-07-18 17:49:02 +080036#define GPIO_IBE 0x18
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020037
38struct mpc8xxx_gpio_chip {
Liu Gang42178e22016-02-03 19:27:34 +080039 struct gpio_chip gc;
40 void __iomem *regs;
Alexander Stein50593612015-07-21 15:54:30 +020041 raw_spinlock_t lock;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020042
Liu Gang42178e22016-02-03 19:27:34 +080043 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
Song Huibd4bd332019-07-18 17:49:02 +080050/* The GPIO Input Buffer Enable register(GPIO_IBE) is used to
51 * control the input enable of each individual GPIO port.
52 * When an individual GPIO port’s direction is set to
53 * input (GPIO_GPDIR[DRn=0]), the associated input enable must be
54 * set (GPIOxGPIE[IEn]=1) to propagate the port value to the GPIO
55 * Data Register.
56 */
57static int ls1028a_gpio_dir_in_init(struct gpio_chip *gc)
58{
59 unsigned long flags;
60 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
61
62 spin_lock_irqsave(&gc->bgpio_lock, flags);
63
64 gc->write_reg(mpc8xxx_gc->regs + GPIO_IBE, 0xffffffff);
65
66 spin_unlock_irqrestore(&gc->bgpio_lock, flags);
67
68 return 0;
69}
70
Linus Walleijb3222f72017-10-20 16:08:12 +020071/*
72 * This hardware has a big endian bit assignment such that GPIO line 0 is
73 * connected to bit 31, line 1 to bit 30 ... line 31 to bit 0.
74 * This inline helper give the right bitmask for a certain line.
75 */
76static inline u32 mpc_pin2mask(unsigned int offset)
77{
78 return BIT(31 - offset);
79}
80
Felix Radenskyc1a676d2009-08-12 08:57:39 +030081/* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
82 * defined as output cannot be determined by reading GPDAT register,
83 * so we use shadow data register instead. The status of input pins
84 * is determined by reading GPDAT register.
85 */
86static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
87{
88 u32 val;
Linus Walleij709d71a2015-12-07 10:34:28 +010089 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Liu Gang1aeef302013-11-22 16:12:40 +080090 u32 out_mask, out_shadow;
Felix Radenskyc1a676d2009-08-12 08:57:39 +030091
Axel Lincd0d3f52016-02-22 15:24:01 +080092 out_mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_DIR);
93 val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
Liu Gang42178e22016-02-03 19:27:34 +080094 out_shadow = gc->bgpio_data & out_mask;
Felix Radenskyc1a676d2009-08-12 08:57:39 +030095
Linus Walleijb3222f72017-10-20 16:08:12 +020096 return !!((val | out_shadow) & mpc_pin2mask(gpio));
Felix Radenskyc1a676d2009-08-12 08:57:39 +030097}
98
Liu Gang42178e22016-02-03 19:27:34 +080099static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
100 unsigned int gpio, int val)
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200101{
Linus Walleij709d71a2015-12-07 10:34:28 +0100102 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Wolfram Sang28538df2011-12-13 10:12:48 +0100103 /* GPIO 28..31 are input only on MPC5121 */
104 if (gpio >= 28)
105 return -EINVAL;
106
Liu Gang42178e22016-02-03 19:27:34 +0800107 return mpc8xxx_gc->direction_output(gc, gpio, val);
Wolfram Sang28538df2011-12-13 10:12:48 +0100108}
109
Liu Gang42178e22016-02-03 19:27:34 +0800110static int mpc5125_gpio_dir_out(struct gpio_chip *gc,
111 unsigned int gpio, int val)
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200112{
Liu Gang42178e22016-02-03 19:27:34 +0800113 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200114 /* GPIO 0..3 are input only on MPC5125 */
115 if (gpio <= 3)
116 return -EINVAL;
117
Liu Gang42178e22016-02-03 19:27:34 +0800118 return mpc8xxx_gc->direction_output(gc, gpio, val);
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200119}
120
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100121static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
122{
Linus Walleij709d71a2015-12-07 10:34:28 +0100123 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100124
125 if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
126 return irq_create_mapping(mpc8xxx_gc->irq, offset);
127 else
128 return -ENXIO;
129}
130
Song Hui698b8ee2019-10-11 08:56:43 +0800131static irqreturn_t mpc8xxx_gpio_irq_cascade(int irq, void *data)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100132{
Song Hui698b8ee2019-10-11 08:56:43 +0800133 struct mpc8xxx_gpio_chip *mpc8xxx_gc = data;
Axel Lincd0d3f52016-02-22 15:24:01 +0800134 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Song Hui698b8ee2019-10-11 08:56:43 +0800135 unsigned long mask;
136 int i;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100137
Axel Lincd0d3f52016-02-22 15:24:01 +0800138 mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_IER)
139 & gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR);
Song Hui698b8ee2019-10-11 08:56:43 +0800140 for_each_set_bit(i, &mask, 32)
141 generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq, 31 - i));
142
143 return IRQ_HANDLED;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100144}
145
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000146static void mpc8xxx_irq_unmask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100147{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000148 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800149 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100150 unsigned long flags;
151
Alexander Stein50593612015-07-21 15:54:30 +0200152 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100153
Axel Lincd0d3f52016-02-22 15:24:01 +0800154 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
155 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200156 | mpc_pin2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100157
Alexander Stein50593612015-07-21 15:54:30 +0200158 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100159}
160
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000161static void mpc8xxx_irq_mask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100162{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000163 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800164 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100165 unsigned long flags;
166
Alexander Stein50593612015-07-21 15:54:30 +0200167 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100168
Axel Lincd0d3f52016-02-22 15:24:01 +0800169 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
170 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200171 & ~mpc_pin2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100172
Alexander Stein50593612015-07-21 15:54:30 +0200173 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100174}
175
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000176static void mpc8xxx_irq_ack(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100177{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000178 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800179 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100180
Axel Lincd0d3f52016-02-22 15:24:01 +0800181 gc->write_reg(mpc8xxx_gc->regs + GPIO_IER,
Linus Walleijb3222f72017-10-20 16:08:12 +0200182 mpc_pin2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100183}
184
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000185static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100186{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000187 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800188 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100189 unsigned long flags;
190
191 switch (flow_type) {
192 case IRQ_TYPE_EDGE_FALLING:
Alexander Stein50593612015-07-21 15:54:30 +0200193 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800194 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
195 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200196 | mpc_pin2mask(irqd_to_hwirq(d)));
Alexander Stein50593612015-07-21 15:54:30 +0200197 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100198 break;
199
200 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200201 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800202 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
203 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200204 & ~mpc_pin2mask(irqd_to_hwirq(d)));
Alexander Stein50593612015-07-21 15:54:30 +0200205 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100206 break;
207
208 default:
209 return -EINVAL;
210 }
211
212 return 0;
213}
214
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000215static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200216{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000217 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Axel Lincd0d3f52016-02-22 15:24:01 +0800218 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Grant Likely476eb492011-05-04 15:02:15 +1000219 unsigned long gpio = irqd_to_hwirq(d);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200220 void __iomem *reg;
221 unsigned int shift;
222 unsigned long flags;
223
224 if (gpio < 16) {
Liu Gang42178e22016-02-03 19:27:34 +0800225 reg = mpc8xxx_gc->regs + GPIO_ICR;
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200226 shift = (15 - gpio) * 2;
227 } else {
Liu Gang42178e22016-02-03 19:27:34 +0800228 reg = mpc8xxx_gc->regs + GPIO_ICR2;
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200229 shift = (15 - (gpio % 16)) * 2;
230 }
231
232 switch (flow_type) {
233 case IRQ_TYPE_EDGE_FALLING:
234 case IRQ_TYPE_LEVEL_LOW:
Alexander Stein50593612015-07-21 15:54:30 +0200235 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800236 gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
Liu Gang42178e22016-02-03 19:27:34 +0800237 | (2 << shift));
Alexander Stein50593612015-07-21 15:54:30 +0200238 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200239 break;
240
241 case IRQ_TYPE_EDGE_RISING:
242 case IRQ_TYPE_LEVEL_HIGH:
Alexander Stein50593612015-07-21 15:54:30 +0200243 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800244 gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
Liu Gang42178e22016-02-03 19:27:34 +0800245 | (1 << shift));
Alexander Stein50593612015-07-21 15:54:30 +0200246 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200247 break;
248
249 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200250 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800251 gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift)));
Alexander Stein50593612015-07-21 15:54:30 +0200252 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200253 break;
254
255 default:
256 return -EINVAL;
257 }
258
259 return 0;
260}
261
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100262static struct irq_chip mpc8xxx_irq_chip = {
263 .name = "mpc8xxx-gpio",
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000264 .irq_unmask = mpc8xxx_irq_unmask,
265 .irq_mask = mpc8xxx_irq_mask,
266 .irq_ack = mpc8xxx_irq_ack,
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200267 /* this might get overwritten in mpc8xxx_probe() */
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000268 .irq_set_type = mpc8xxx_irq_set_type,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100269};
270
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200271static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
272 irq_hw_number_t hwirq)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100273{
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200274 irq_set_chip_data(irq, h->host_data);
Liu Gangd71cf152016-10-21 15:31:28 +0800275 irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_edge_irq);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100276
277 return 0;
278}
279
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900280static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100281 .map = mpc8xxx_gpio_irq_map,
Grant Likelyff8c3ab2012-01-24 17:09:13 -0700282 .xlate = irq_domain_xlate_twocell,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100283};
284
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200285struct mpc8xxx_gpio_devtype {
Song Huibd4bd332019-07-18 17:49:02 +0800286 int (*gpio_dir_in_init)(struct gpio_chip *chip);
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200287 int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
288 int (*gpio_get)(struct gpio_chip *, unsigned int);
289 int (*irq_set_type)(struct irq_data *, unsigned int);
290};
291
292static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
293 .gpio_dir_out = mpc5121_gpio_dir_out,
294 .irq_set_type = mpc512x_irq_set_type,
295};
296
Song Huibd4bd332019-07-18 17:49:02 +0800297static const struct mpc8xxx_gpio_devtype ls1028a_gpio_devtype = {
298 .gpio_dir_in_init = ls1028a_gpio_dir_in_init,
299};
300
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200301static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
302 .gpio_dir_out = mpc5125_gpio_dir_out,
303 .irq_set_type = mpc512x_irq_set_type,
304};
305
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200306static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
307 .gpio_get = mpc8572_gpio_get,
308};
309
310static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200311 .irq_set_type = mpc8xxx_irq_set_type,
312};
313
Uwe Kleine-König4183afe2015-07-16 21:08:21 +0200314static const struct of_device_id mpc8xxx_gpio_ids[] = {
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200315 { .compatible = "fsl,mpc8349-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200316 { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200317 { .compatible = "fsl,mpc8610-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200318 { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200319 { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
Kumar Gala15a51482011-10-22 16:20:42 -0500320 { .compatible = "fsl,pq3-gpio", },
Song Huibd4bd332019-07-18 17:49:02 +0800321 { .compatible = "fsl,ls1028a-gpio", .data = &ls1028a_gpio_devtype, },
Song Hui7b732202019-08-08 18:16:28 +0800322 { .compatible = "fsl,ls1088a-gpio", .data = &ls1028a_gpio_devtype, },
Anatolij Gustschind1dcfbb2011-01-08 16:51:16 +0100323 { .compatible = "fsl,qoriq-gpio", },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200324 {}
325};
326
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100327static int mpc8xxx_probe(struct platform_device *pdev)
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200328{
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100329 struct device_node *np = pdev->dev.of_node;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200330 struct mpc8xxx_gpio_chip *mpc8xxx_gc;
Liu Gang42178e22016-02-03 19:27:34 +0800331 struct gpio_chip *gc;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200332 const struct mpc8xxx_gpio_devtype *devtype =
333 of_device_get_match_data(&pdev->dev);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200334 int ret;
335
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100336 mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
337 if (!mpc8xxx_gc)
338 return -ENOMEM;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200339
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100340 platform_set_drvdata(pdev, mpc8xxx_gc);
341
Alexander Stein50593612015-07-21 15:54:30 +0200342 raw_spin_lock_init(&mpc8xxx_gc->lock);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200343
Liu Gang42178e22016-02-03 19:27:34 +0800344 mpc8xxx_gc->regs = of_iomap(np, 0);
345 if (!mpc8xxx_gc->regs)
346 return -ENOMEM;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200347
Liu Gang42178e22016-02-03 19:27:34 +0800348 gc = &mpc8xxx_gc->gc;
349
350 if (of_property_read_bool(np, "little-endian")) {
351 ret = bgpio_init(gc, &pdev->dev, 4,
352 mpc8xxx_gc->regs + GPIO_DAT,
353 NULL, NULL,
354 mpc8xxx_gc->regs + GPIO_DIR, NULL,
355 BGPIOF_BIG_ENDIAN);
356 if (ret)
357 goto err;
358 dev_dbg(&pdev->dev, "GPIO registers are LITTLE endian\n");
359 } else {
360 ret = bgpio_init(gc, &pdev->dev, 4,
361 mpc8xxx_gc->regs + GPIO_DAT,
362 NULL, NULL,
363 mpc8xxx_gc->regs + GPIO_DIR, NULL,
364 BGPIOF_BIG_ENDIAN
365 | BGPIOF_BIG_ENDIAN_BYTE_ORDER);
366 if (ret)
367 goto err;
368 dev_dbg(&pdev->dev, "GPIO registers are BIG endian\n");
369 }
370
Axel Linfa4007c2016-02-22 15:22:52 +0800371 mpc8xxx_gc->direction_output = gc->direction_output;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200372
373 if (!devtype)
374 devtype = &mpc8xxx_gpio_devtype_default;
375
376 /*
377 * It's assumed that only a single type of gpio controller is available
378 * on the current machine, so overwriting global data is fine.
379 */
Vladimir Oltean4e505732019-11-15 14:55:51 +0200380 if (devtype->irq_set_type)
381 mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200382
Axel Linadf32ea2016-02-22 15:24:54 +0800383 if (devtype->gpio_dir_out)
384 gc->direction_output = devtype->gpio_dir_out;
385 if (devtype->gpio_get)
386 gc->get = devtype->gpio_get;
387
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100388 gc->to_irq = mpc8xxx_gpio_to_irq;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200389
Russell King787b64a2019-11-19 13:10:38 +0000390 if (of_device_is_compatible(np, "fsl,qoriq-gpio"))
391 gc->write_reg(mpc8xxx_gc->regs + GPIO_IBE, 0xffffffff);
392
Liu Gang42178e22016-02-03 19:27:34 +0800393 ret = gpiochip_add_data(gc, mpc8xxx_gc);
394 if (ret) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500395 pr_err("%pOF: GPIO chip registration failed with status %d\n",
396 np, ret);
Liu Gang42178e22016-02-03 19:27:34 +0800397 goto err;
398 }
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200399
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100400 mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
Liu Gang42178e22016-02-03 19:27:34 +0800401 if (!mpc8xxx_gc->irqn)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100402 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100403
Grant Likelya8db8cf2012-02-14 14:06:54 -0700404 mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
405 &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100406 if (!mpc8xxx_gc->irq)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100407 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100408
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100409 /* ack and mask all irqs */
Axel Lincd0d3f52016-02-22 15:24:01 +0800410 gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
411 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
Song Huibd4bd332019-07-18 17:49:02 +0800412 /* enable input buffer */
413 if (devtype->gpio_dir_in_init)
414 devtype->gpio_dir_in_init(gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100415
Song Hui698b8ee2019-10-11 08:56:43 +0800416 ret = devm_request_irq(&pdev->dev, mpc8xxx_gc->irqn,
417 mpc8xxx_gpio_irq_cascade,
418 IRQF_NO_THREAD | IRQF_SHARED, "gpio-cascade",
419 mpc8xxx_gc);
420 if (ret) {
421 dev_err(&pdev->dev, "%s: failed to devm_request_irq(%d), ret = %d\n",
422 np->full_name, mpc8xxx_gc->irqn, ret);
423 goto err;
424 }
425
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100426 return 0;
Liu Gang42178e22016-02-03 19:27:34 +0800427err:
428 iounmap(mpc8xxx_gc->regs);
429 return ret;
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100430}
431
432static int mpc8xxx_remove(struct platform_device *pdev)
433{
434 struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);
435
436 if (mpc8xxx_gc->irq) {
Thomas Gleixner05379812015-06-21 21:10:46 +0200437 irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100438 irq_domain_remove(mpc8xxx_gc->irq);
439 }
440
Liu Gang42178e22016-02-03 19:27:34 +0800441 gpiochip_remove(&mpc8xxx_gc->gc);
442 iounmap(mpc8xxx_gc->regs);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100443
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200444 return 0;
445}
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100446
447static struct platform_driver mpc8xxx_plat_driver = {
448 .probe = mpc8xxx_probe,
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100449 .remove = mpc8xxx_remove,
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100450 .driver = {
451 .name = "gpio-mpc8xxx",
452 .of_match_table = mpc8xxx_gpio_ids,
453 },
454};
455
456static int __init mpc8xxx_init(void)
457{
458 return platform_driver_register(&mpc8xxx_plat_driver);
459}
460
461arch_initcall(mpc8xxx_init);