blob: 6dfca83bcd90843ea22c2952dd0084081b7aafc7 [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
Linus Walleijb3222f72017-10-20 16:08:12 +020050/*
51 * This hardware has a big endian bit assignment such that GPIO line 0 is
52 * connected to bit 31, line 1 to bit 30 ... line 31 to bit 0.
53 * This inline helper give the right bitmask for a certain line.
54 */
55static inline u32 mpc_pin2mask(unsigned int offset)
56{
57 return BIT(31 - offset);
58}
59
Felix Radenskyc1a676d2009-08-12 08:57:39 +030060/* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
61 * defined as output cannot be determined by reading GPDAT register,
62 * so we use shadow data register instead. The status of input pins
63 * is determined by reading GPDAT register.
64 */
65static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
66{
67 u32 val;
Linus Walleij709d71a2015-12-07 10:34:28 +010068 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Liu Gang1aeef302013-11-22 16:12:40 +080069 u32 out_mask, out_shadow;
Felix Radenskyc1a676d2009-08-12 08:57:39 +030070
Axel Lincd0d3f52016-02-22 15:24:01 +080071 out_mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_DIR);
72 val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
Liu Gang42178e22016-02-03 19:27:34 +080073 out_shadow = gc->bgpio_data & out_mask;
Felix Radenskyc1a676d2009-08-12 08:57:39 +030074
Linus Walleijb3222f72017-10-20 16:08:12 +020075 return !!((val | out_shadow) & mpc_pin2mask(gpio));
Felix Radenskyc1a676d2009-08-12 08:57:39 +030076}
77
Liu Gang42178e22016-02-03 19:27:34 +080078static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
79 unsigned int gpio, int val)
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020080{
Linus Walleij709d71a2015-12-07 10:34:28 +010081 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Wolfram Sang28538df2011-12-13 10:12:48 +010082 /* GPIO 28..31 are input only on MPC5121 */
83 if (gpio >= 28)
84 return -EINVAL;
85
Liu Gang42178e22016-02-03 19:27:34 +080086 return mpc8xxx_gc->direction_output(gc, gpio, val);
Wolfram Sang28538df2011-12-13 10:12:48 +010087}
88
Liu Gang42178e22016-02-03 19:27:34 +080089static int mpc5125_gpio_dir_out(struct gpio_chip *gc,
90 unsigned int gpio, int val)
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +020091{
Liu Gang42178e22016-02-03 19:27:34 +080092 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +020093 /* GPIO 0..3 are input only on MPC5125 */
94 if (gpio <= 3)
95 return -EINVAL;
96
Liu Gang42178e22016-02-03 19:27:34 +080097 return mpc8xxx_gc->direction_output(gc, gpio, val);
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +020098}
99
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100100static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
101{
Linus Walleij709d71a2015-12-07 10:34:28 +0100102 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100103
104 if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
105 return irq_create_mapping(mpc8xxx_gc->irq, offset);
106 else
107 return -ENXIO;
108}
109
Song Hui698b8ee2019-10-11 08:56:43 +0800110static irqreturn_t mpc8xxx_gpio_irq_cascade(int irq, void *data)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100111{
Song Hui698b8ee2019-10-11 08:56:43 +0800112 struct mpc8xxx_gpio_chip *mpc8xxx_gc = data;
Axel Lincd0d3f52016-02-22 15:24:01 +0800113 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Song Hui698b8ee2019-10-11 08:56:43 +0800114 unsigned long mask;
115 int i;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100116
Axel Lincd0d3f52016-02-22 15:24:01 +0800117 mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_IER)
118 & gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR);
Song Hui698b8ee2019-10-11 08:56:43 +0800119 for_each_set_bit(i, &mask, 32)
120 generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq, 31 - i));
121
122 return IRQ_HANDLED;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100123}
124
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000125static void mpc8xxx_irq_unmask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100126{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000127 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800128 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100129 unsigned long flags;
130
Alexander Stein50593612015-07-21 15:54:30 +0200131 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100132
Axel Lincd0d3f52016-02-22 15:24:01 +0800133 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
134 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200135 | mpc_pin2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100136
Alexander Stein50593612015-07-21 15:54:30 +0200137 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100138}
139
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000140static void mpc8xxx_irq_mask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100141{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000142 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800143 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100144 unsigned long flags;
145
Alexander Stein50593612015-07-21 15:54:30 +0200146 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100147
Axel Lincd0d3f52016-02-22 15:24:01 +0800148 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
149 gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200150 & ~mpc_pin2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100151
Alexander Stein50593612015-07-21 15:54:30 +0200152 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100153}
154
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000155static void mpc8xxx_irq_ack(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100156{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000157 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800158 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100159
Axel Lincd0d3f52016-02-22 15:24:01 +0800160 gc->write_reg(mpc8xxx_gc->regs + GPIO_IER,
Linus Walleijb3222f72017-10-20 16:08:12 +0200161 mpc_pin2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100162}
163
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000164static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100165{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000166 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Liu Gang42178e22016-02-03 19:27:34 +0800167 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100168 unsigned long flags;
169
170 switch (flow_type) {
171 case IRQ_TYPE_EDGE_FALLING:
Alexander Stein50593612015-07-21 15:54:30 +0200172 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800173 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
174 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200175 | mpc_pin2mask(irqd_to_hwirq(d)));
Alexander Stein50593612015-07-21 15:54:30 +0200176 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100177 break;
178
179 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200180 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800181 gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
182 gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
Linus Walleijb3222f72017-10-20 16:08:12 +0200183 & ~mpc_pin2mask(irqd_to_hwirq(d)));
Alexander Stein50593612015-07-21 15:54:30 +0200184 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100185 break;
186
187 default:
188 return -EINVAL;
189 }
190
191 return 0;
192}
193
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000194static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200195{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000196 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Axel Lincd0d3f52016-02-22 15:24:01 +0800197 struct gpio_chip *gc = &mpc8xxx_gc->gc;
Grant Likely476eb492011-05-04 15:02:15 +1000198 unsigned long gpio = irqd_to_hwirq(d);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200199 void __iomem *reg;
200 unsigned int shift;
201 unsigned long flags;
202
203 if (gpio < 16) {
Liu Gang42178e22016-02-03 19:27:34 +0800204 reg = mpc8xxx_gc->regs + GPIO_ICR;
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200205 shift = (15 - gpio) * 2;
206 } else {
Liu Gang42178e22016-02-03 19:27:34 +0800207 reg = mpc8xxx_gc->regs + GPIO_ICR2;
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200208 shift = (15 - (gpio % 16)) * 2;
209 }
210
211 switch (flow_type) {
212 case IRQ_TYPE_EDGE_FALLING:
213 case IRQ_TYPE_LEVEL_LOW:
Alexander Stein50593612015-07-21 15:54:30 +0200214 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800215 gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
Liu Gang42178e22016-02-03 19:27:34 +0800216 | (2 << shift));
Alexander Stein50593612015-07-21 15:54:30 +0200217 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200218 break;
219
220 case IRQ_TYPE_EDGE_RISING:
221 case IRQ_TYPE_LEVEL_HIGH:
Alexander Stein50593612015-07-21 15:54:30 +0200222 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800223 gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
Liu Gang42178e22016-02-03 19:27:34 +0800224 | (1 << shift));
Alexander Stein50593612015-07-21 15:54:30 +0200225 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200226 break;
227
228 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200229 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Axel Lincd0d3f52016-02-22 15:24:01 +0800230 gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift)));
Alexander Stein50593612015-07-21 15:54:30 +0200231 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200232 break;
233
234 default:
235 return -EINVAL;
236 }
237
238 return 0;
239}
240
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100241static struct irq_chip mpc8xxx_irq_chip = {
242 .name = "mpc8xxx-gpio",
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000243 .irq_unmask = mpc8xxx_irq_unmask,
244 .irq_mask = mpc8xxx_irq_mask,
245 .irq_ack = mpc8xxx_irq_ack,
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200246 /* this might get overwritten in mpc8xxx_probe() */
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000247 .irq_set_type = mpc8xxx_irq_set_type,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100248};
249
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200250static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
251 irq_hw_number_t hwirq)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100252{
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200253 irq_set_chip_data(irq, h->host_data);
Liu Gangd71cf152016-10-21 15:31:28 +0800254 irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_edge_irq);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100255
256 return 0;
257}
258
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900259static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100260 .map = mpc8xxx_gpio_irq_map,
Grant Likelyff8c3ab2012-01-24 17:09:13 -0700261 .xlate = irq_domain_xlate_twocell,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100262};
263
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200264struct mpc8xxx_gpio_devtype {
265 int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
266 int (*gpio_get)(struct gpio_chip *, unsigned int);
267 int (*irq_set_type)(struct irq_data *, unsigned int);
268};
269
270static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
271 .gpio_dir_out = mpc5121_gpio_dir_out,
272 .irq_set_type = mpc512x_irq_set_type,
273};
274
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200275static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
276 .gpio_dir_out = mpc5125_gpio_dir_out,
277 .irq_set_type = mpc512x_irq_set_type,
278};
279
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200280static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
281 .gpio_get = mpc8572_gpio_get,
282};
283
284static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200285 .irq_set_type = mpc8xxx_irq_set_type,
286};
287
Uwe Kleine-König4183afe2015-07-16 21:08:21 +0200288static const struct of_device_id mpc8xxx_gpio_ids[] = {
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200289 { .compatible = "fsl,mpc8349-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200290 { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200291 { .compatible = "fsl,mpc8610-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200292 { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200293 { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
Kumar Gala15a51482011-10-22 16:20:42 -0500294 { .compatible = "fsl,pq3-gpio", },
Michael Walle3795d7c2020-09-30 09:42:11 +0200295 { .compatible = "fsl,ls1028a-gpio", },
296 { .compatible = "fsl,ls1088a-gpio", },
Anatolij Gustschind1dcfbb2011-01-08 16:51:16 +0100297 { .compatible = "fsl,qoriq-gpio", },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200298 {}
299};
300
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100301static int mpc8xxx_probe(struct platform_device *pdev)
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200302{
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100303 struct device_node *np = pdev->dev.of_node;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200304 struct mpc8xxx_gpio_chip *mpc8xxx_gc;
Liu Gang42178e22016-02-03 19:27:34 +0800305 struct gpio_chip *gc;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200306 const struct mpc8xxx_gpio_devtype *devtype =
307 of_device_get_match_data(&pdev->dev);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200308 int ret;
309
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100310 mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
311 if (!mpc8xxx_gc)
312 return -ENOMEM;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200313
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100314 platform_set_drvdata(pdev, mpc8xxx_gc);
315
Alexander Stein50593612015-07-21 15:54:30 +0200316 raw_spin_lock_init(&mpc8xxx_gc->lock);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200317
Liu Gang42178e22016-02-03 19:27:34 +0800318 mpc8xxx_gc->regs = of_iomap(np, 0);
319 if (!mpc8xxx_gc->regs)
320 return -ENOMEM;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200321
Liu Gang42178e22016-02-03 19:27:34 +0800322 gc = &mpc8xxx_gc->gc;
Johnson CH Chen (陳昭勳)322f6a32019-11-26 06:51:11 +0000323 gc->parent = &pdev->dev;
Liu Gang42178e22016-02-03 19:27:34 +0800324
325 if (of_property_read_bool(np, "little-endian")) {
326 ret = bgpio_init(gc, &pdev->dev, 4,
327 mpc8xxx_gc->regs + GPIO_DAT,
328 NULL, NULL,
329 mpc8xxx_gc->regs + GPIO_DIR, NULL,
330 BGPIOF_BIG_ENDIAN);
331 if (ret)
332 goto err;
333 dev_dbg(&pdev->dev, "GPIO registers are LITTLE endian\n");
334 } else {
335 ret = bgpio_init(gc, &pdev->dev, 4,
336 mpc8xxx_gc->regs + GPIO_DAT,
337 NULL, NULL,
338 mpc8xxx_gc->regs + GPIO_DIR, NULL,
339 BGPIOF_BIG_ENDIAN
340 | BGPIOF_BIG_ENDIAN_BYTE_ORDER);
341 if (ret)
342 goto err;
343 dev_dbg(&pdev->dev, "GPIO registers are BIG endian\n");
344 }
345
Axel Linfa4007c2016-02-22 15:22:52 +0800346 mpc8xxx_gc->direction_output = gc->direction_output;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200347
348 if (!devtype)
349 devtype = &mpc8xxx_gpio_devtype_default;
350
351 /*
352 * It's assumed that only a single type of gpio controller is available
353 * on the current machine, so overwriting global data is fine.
354 */
Vladimir Oltean4e505732019-11-15 14:55:51 +0200355 if (devtype->irq_set_type)
356 mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200357
Axel Linadf32ea2016-02-22 15:24:54 +0800358 if (devtype->gpio_dir_out)
359 gc->direction_output = devtype->gpio_dir_out;
360 if (devtype->gpio_get)
361 gc->get = devtype->gpio_get;
362
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100363 gc->to_irq = mpc8xxx_gpio_to_irq;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200364
Michael Walle3795d7c2020-09-30 09:42:11 +0200365 /*
366 * The GPIO Input Buffer Enable register(GPIO_IBE) is used to control
367 * the input enable of each individual GPIO port. When an individual
368 * GPIO port’s direction is set to input (GPIO_GPDIR[DRn=0]), the
369 * associated input enable must be set (GPIOxGPIE[IEn]=1) to propagate
370 * the port value to the GPIO Data Register.
371 */
372 if (of_device_is_compatible(np, "fsl,qoriq-gpio") ||
373 of_device_is_compatible(np, "fsl,ls1028a-gpio") ||
374 of_device_is_compatible(np, "fsl,ls1088a-gpio"))
Russell King787b64a2019-11-19 13:10:38 +0000375 gc->write_reg(mpc8xxx_gc->regs + GPIO_IBE, 0xffffffff);
376
Liu Gang42178e22016-02-03 19:27:34 +0800377 ret = gpiochip_add_data(gc, mpc8xxx_gc);
378 if (ret) {
Rob Herring7eb6ce22017-07-18 16:43:03 -0500379 pr_err("%pOF: GPIO chip registration failed with status %d\n",
380 np, ret);
Liu Gang42178e22016-02-03 19:27:34 +0800381 goto err;
382 }
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200383
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100384 mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
Liu Gang42178e22016-02-03 19:27:34 +0800385 if (!mpc8xxx_gc->irqn)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100386 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100387
Grant Likelya8db8cf2012-02-14 14:06:54 -0700388 mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
389 &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100390 if (!mpc8xxx_gc->irq)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100391 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100392
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100393 /* ack and mask all irqs */
Axel Lincd0d3f52016-02-22 15:24:01 +0800394 gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
395 gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100396
Song Hui698b8ee2019-10-11 08:56:43 +0800397 ret = devm_request_irq(&pdev->dev, mpc8xxx_gc->irqn,
398 mpc8xxx_gpio_irq_cascade,
Song Hui3d5bfbd2020-06-11 18:28:09 +0800399 IRQF_SHARED, "gpio-cascade",
Song Hui698b8ee2019-10-11 08:56:43 +0800400 mpc8xxx_gc);
401 if (ret) {
402 dev_err(&pdev->dev, "%s: failed to devm_request_irq(%d), ret = %d\n",
403 np->full_name, mpc8xxx_gc->irqn, ret);
404 goto err;
405 }
406
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100407 return 0;
Liu Gang42178e22016-02-03 19:27:34 +0800408err:
409 iounmap(mpc8xxx_gc->regs);
410 return ret;
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100411}
412
413static int mpc8xxx_remove(struct platform_device *pdev)
414{
415 struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);
416
417 if (mpc8xxx_gc->irq) {
Thomas Gleixner05379812015-06-21 21:10:46 +0200418 irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100419 irq_domain_remove(mpc8xxx_gc->irq);
420 }
421
Liu Gang42178e22016-02-03 19:27:34 +0800422 gpiochip_remove(&mpc8xxx_gc->gc);
423 iounmap(mpc8xxx_gc->regs);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100424
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200425 return 0;
426}
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100427
428static struct platform_driver mpc8xxx_plat_driver = {
429 .probe = mpc8xxx_probe,
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100430 .remove = mpc8xxx_remove,
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100431 .driver = {
432 .name = "gpio-mpc8xxx",
433 .of_match_table = mpc8xxx_gpio_ids,
434 },
435};
436
437static int __init mpc8xxx_init(void)
438{
439 return platform_driver_register(&mpc8xxx_plat_driver);
440}
441
442arch_initcall(mpc8xxx_init);