blob: 9d40787e66c02970783e87f046f30846d789e58d [file] [log] [blame]
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +02001/*
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +02002 * GPIOs on MPC512x/8349/8572/8610 and compatible
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +02003 *
4 * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
5 *
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/io.h>
15#include <linux/of.h>
16#include <linux/of_gpio.h>
Rob Herring5af50732013-09-17 14:28:33 -050017#include <linux/of_irq.h>
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +010018#include <linux/of_platform.h>
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020019#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Peter Korsgaard345e5c82010-01-07 17:57:46 +010021#include <linux/irq.h>
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020022
23#define MPC8XXX_GPIO_PINS 32
24
25#define GPIO_DIR 0x00
26#define GPIO_ODR 0x04
27#define GPIO_DAT 0x08
28#define GPIO_IER 0x0c
29#define GPIO_IMR 0x10
30#define GPIO_ICR 0x14
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +020031#define GPIO_ICR2 0x18
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020032
33struct mpc8xxx_gpio_chip {
34 struct of_mm_gpio_chip mm_gc;
Alexander Stein50593612015-07-21 15:54:30 +020035 raw_spinlock_t lock;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020036
37 /*
38 * shadowed data register to be able to clear/set output pins in
39 * open drain mode safely
40 */
41 u32 data;
Grant Likelybae1d8f2012-02-14 14:06:50 -070042 struct irq_domain *irq;
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +010043 unsigned int irqn;
Uwe Kleine-König01a04dd2012-05-21 21:57:39 +020044 const void *of_dev_id_data;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020045};
46
47static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
48{
49 return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
50}
51
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020052static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
53{
Guenter Roeck78179982016-01-07 08:25:24 -080054 struct mpc8xxx_gpio_chip *mpc8xxx_gc =
55 container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020056
57 mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
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;
68 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
Linus Walleij709d71a2015-12-07 10:34:28 +010069 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Liu Gang1aeef302013-11-22 16:12:40 +080070 u32 out_mask, out_shadow;
Felix Radenskyc1a676d2009-08-12 08:57:39 +030071
Liu Gang1aeef302013-11-22 16:12:40 +080072 out_mask = in_be32(mm->regs + GPIO_DIR);
Felix Radenskyc1a676d2009-08-12 08:57:39 +030073
Liu Gang1aeef302013-11-22 16:12:40 +080074 val = in_be32(mm->regs + GPIO_DAT) & ~out_mask;
75 out_shadow = mpc8xxx_gc->data & out_mask;
76
Linus Walleijc7591742015-12-21 11:20:56 +010077 return !!((val | out_shadow) & mpc8xxx_gpio2mask(gpio));
Felix Radenskyc1a676d2009-08-12 08:57:39 +030078}
79
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020080static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
81{
82 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
83
84 return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
85}
86
87static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
88{
89 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
Linus Walleij709d71a2015-12-07 10:34:28 +010090 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020091 unsigned long flags;
92
Alexander Stein50593612015-07-21 15:54:30 +020093 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +020094
95 if (val)
96 mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
97 else
98 mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
99
100 out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
101
Alexander Stein50593612015-07-21 15:54:30 +0200102 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200103}
104
Rojhalat Ibrahime5db3b32014-11-04 17:12:09 +0100105static void mpc8xxx_gpio_set_multiple(struct gpio_chip *gc,
106 unsigned long *mask, unsigned long *bits)
107{
108 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
Linus Walleij709d71a2015-12-07 10:34:28 +0100109 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Rojhalat Ibrahime5db3b32014-11-04 17:12:09 +0100110 unsigned long flags;
111 int i;
112
Alexander Stein50593612015-07-21 15:54:30 +0200113 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Rojhalat Ibrahime5db3b32014-11-04 17:12:09 +0100114
115 for (i = 0; i < gc->ngpio; i++) {
116 if (*mask == 0)
117 break;
118 if (__test_and_clear_bit(i, mask)) {
119 if (test_bit(i, bits))
120 mpc8xxx_gc->data |= mpc8xxx_gpio2mask(i);
121 else
122 mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(i);
123 }
124 }
125
126 out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
127
Alexander Stein50593612015-07-21 15:54:30 +0200128 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Rojhalat Ibrahime5db3b32014-11-04 17:12:09 +0100129}
130
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200131static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
132{
133 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
Linus Walleij709d71a2015-12-07 10:34:28 +0100134 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200135 unsigned long flags;
136
Alexander Stein50593612015-07-21 15:54:30 +0200137 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200138
139 clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
140
Alexander Stein50593612015-07-21 15:54:30 +0200141 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200142
143 return 0;
144}
145
146static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
147{
148 struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
Linus Walleij709d71a2015-12-07 10:34:28 +0100149 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200150 unsigned long flags;
151
152 mpc8xxx_gpio_set(gc, gpio, val);
153
Alexander Stein50593612015-07-21 15:54:30 +0200154 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200155
156 setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
157
Alexander Stein50593612015-07-21 15:54:30 +0200158 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200159
160 return 0;
161}
162
Wolfram Sang28538df2011-12-13 10:12:48 +0100163static int mpc5121_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
164{
165 /* GPIO 28..31 are input only on MPC5121 */
166 if (gpio >= 28)
167 return -EINVAL;
168
169 return mpc8xxx_gpio_dir_out(gc, gpio, val);
170}
171
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200172static int mpc5125_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
173{
174 /* GPIO 0..3 are input only on MPC5125 */
175 if (gpio <= 3)
176 return -EINVAL;
177
178 return mpc8xxx_gpio_dir_out(gc, gpio, val);
179}
180
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100181static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
182{
Linus Walleij709d71a2015-12-07 10:34:28 +0100183 struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100184
185 if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
186 return irq_create_mapping(mpc8xxx_gc->irq, offset);
187 else
188 return -ENXIO;
189}
190
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200191static void mpc8xxx_gpio_irq_cascade(struct irq_desc *desc)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100192{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100193 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
Felix Radenskycfadd832011-10-11 10:24:21 +0200194 struct irq_chip *chip = irq_desc_get_chip(desc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100195 struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
196 unsigned int mask;
197
198 mask = in_be32(mm->regs + GPIO_IER) & in_be32(mm->regs + GPIO_IMR);
199 if (mask)
200 generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
201 32 - ffs(mask)));
Thomas Gleixnerd6de85e2012-05-03 12:22:06 +0200202 if (chip->irq_eoi)
203 chip->irq_eoi(&desc->irq_data);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100204}
205
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000206static void mpc8xxx_irq_unmask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100207{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000208 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100209 struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
210 unsigned long flags;
211
Alexander Stein50593612015-07-21 15:54:30 +0200212 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100213
Grant Likely476eb492011-05-04 15:02:15 +1000214 setbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100215
Alexander Stein50593612015-07-21 15:54:30 +0200216 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100217}
218
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000219static void mpc8xxx_irq_mask(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100220{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000221 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100222 struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
223 unsigned long flags;
224
Alexander Stein50593612015-07-21 15:54:30 +0200225 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100226
Grant Likely476eb492011-05-04 15:02:15 +1000227 clrbits32(mm->regs + GPIO_IMR, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100228
Alexander Stein50593612015-07-21 15:54:30 +0200229 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100230}
231
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000232static void mpc8xxx_irq_ack(struct irq_data *d)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100233{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000234 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100235 struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
236
Grant Likely476eb492011-05-04 15:02:15 +1000237 out_be32(mm->regs + GPIO_IER, mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100238}
239
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000240static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100241{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000242 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100243 struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
244 unsigned long flags;
245
246 switch (flow_type) {
247 case IRQ_TYPE_EDGE_FALLING:
Alexander Stein50593612015-07-21 15:54:30 +0200248 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100249 setbits32(mm->regs + GPIO_ICR,
Grant Likely476eb492011-05-04 15:02:15 +1000250 mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
Alexander Stein50593612015-07-21 15:54:30 +0200251 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100252 break;
253
254 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200255 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100256 clrbits32(mm->regs + GPIO_ICR,
Grant Likely476eb492011-05-04 15:02:15 +1000257 mpc8xxx_gpio2mask(irqd_to_hwirq(d)));
Alexander Stein50593612015-07-21 15:54:30 +0200258 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100259 break;
260
261 default:
262 return -EINVAL;
263 }
264
265 return 0;
266}
267
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000268static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200269{
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000270 struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200271 struct of_mm_gpio_chip *mm = &mpc8xxx_gc->mm_gc;
Grant Likely476eb492011-05-04 15:02:15 +1000272 unsigned long gpio = irqd_to_hwirq(d);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200273 void __iomem *reg;
274 unsigned int shift;
275 unsigned long flags;
276
277 if (gpio < 16) {
278 reg = mm->regs + GPIO_ICR;
279 shift = (15 - gpio) * 2;
280 } else {
281 reg = mm->regs + GPIO_ICR2;
282 shift = (15 - (gpio % 16)) * 2;
283 }
284
285 switch (flow_type) {
286 case IRQ_TYPE_EDGE_FALLING:
287 case IRQ_TYPE_LEVEL_LOW:
Alexander Stein50593612015-07-21 15:54:30 +0200288 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200289 clrsetbits_be32(reg, 3 << shift, 2 << shift);
Alexander Stein50593612015-07-21 15:54:30 +0200290 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200291 break;
292
293 case IRQ_TYPE_EDGE_RISING:
294 case IRQ_TYPE_LEVEL_HIGH:
Alexander Stein50593612015-07-21 15:54:30 +0200295 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200296 clrsetbits_be32(reg, 3 << shift, 1 << shift);
Alexander Stein50593612015-07-21 15:54:30 +0200297 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200298 break;
299
300 case IRQ_TYPE_EDGE_BOTH:
Alexander Stein50593612015-07-21 15:54:30 +0200301 raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200302 clrbits32(reg, 3 << shift);
Alexander Stein50593612015-07-21 15:54:30 +0200303 raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200304 break;
305
306 default:
307 return -EINVAL;
308 }
309
310 return 0;
311}
312
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100313static struct irq_chip mpc8xxx_irq_chip = {
314 .name = "mpc8xxx-gpio",
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000315 .irq_unmask = mpc8xxx_irq_unmask,
316 .irq_mask = mpc8xxx_irq_mask,
317 .irq_ack = mpc8xxx_irq_ack,
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200318 /* this might get overwritten in mpc8xxx_probe() */
Lennert Buytenhek94347cb2011-03-08 22:26:58 +0000319 .irq_set_type = mpc8xxx_irq_set_type,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100320};
321
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200322static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
323 irq_hw_number_t hwirq)
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100324{
Linus Walleij5ba17ae2013-10-11 19:37:30 +0200325 irq_set_chip_data(irq, h->host_data);
326 irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_level_irq);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100327
328 return 0;
329}
330
Krzysztof Kozlowski0b354dc2015-04-27 21:54:07 +0900331static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100332 .map = mpc8xxx_gpio_irq_map,
Grant Likelyff8c3ab2012-01-24 17:09:13 -0700333 .xlate = irq_domain_xlate_twocell,
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100334};
335
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200336struct mpc8xxx_gpio_devtype {
337 int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
338 int (*gpio_get)(struct gpio_chip *, unsigned int);
339 int (*irq_set_type)(struct irq_data *, unsigned int);
340};
341
342static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
343 .gpio_dir_out = mpc5121_gpio_dir_out,
344 .irq_set_type = mpc512x_irq_set_type,
345};
346
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200347static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
348 .gpio_dir_out = mpc5125_gpio_dir_out,
349 .irq_set_type = mpc512x_irq_set_type,
350};
351
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200352static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
353 .gpio_get = mpc8572_gpio_get,
354};
355
356static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
357 .gpio_dir_out = mpc8xxx_gpio_dir_out,
358 .gpio_get = mpc8xxx_gpio_get,
359 .irq_set_type = mpc8xxx_irq_set_type,
360};
361
Uwe Kleine-König4183afe2015-07-16 21:08:21 +0200362static const struct of_device_id mpc8xxx_gpio_ids[] = {
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200363 { .compatible = "fsl,mpc8349-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200364 { .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200365 { .compatible = "fsl,mpc8610-gpio", },
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200366 { .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
Uwe Kleine-König0ba69e02015-07-16 21:08:23 +0200367 { .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
Kumar Gala15a51482011-10-22 16:20:42 -0500368 { .compatible = "fsl,pq3-gpio", },
Anatolij Gustschind1dcfbb2011-01-08 16:51:16 +0100369 { .compatible = "fsl,qoriq-gpio", },
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200370 {}
371};
372
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100373static int mpc8xxx_probe(struct platform_device *pdev)
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200374{
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100375 struct device_node *np = pdev->dev.of_node;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200376 struct mpc8xxx_gpio_chip *mpc8xxx_gc;
377 struct of_mm_gpio_chip *mm_gc;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200378 struct gpio_chip *gc;
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200379 const struct of_device_id *id;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200380 const struct mpc8xxx_gpio_devtype *devtype =
381 of_device_get_match_data(&pdev->dev);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200382 int ret;
383
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100384 mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
385 if (!mpc8xxx_gc)
386 return -ENOMEM;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200387
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100388 platform_set_drvdata(pdev, mpc8xxx_gc);
389
Alexander Stein50593612015-07-21 15:54:30 +0200390 raw_spin_lock_init(&mpc8xxx_gc->lock);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200391
392 mm_gc = &mpc8xxx_gc->mm_gc;
Anton Vorontsova19e3da2010-06-08 07:48:16 -0600393 gc = &mm_gc->gc;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200394
395 mm_gc->save_regs = mpc8xxx_gpio_save_regs;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200396 gc->ngpio = MPC8XXX_GPIO_PINS;
397 gc->direction_input = mpc8xxx_gpio_dir_in;
Uwe Kleine-König82e39b02015-07-16 21:08:22 +0200398
399 if (!devtype)
400 devtype = &mpc8xxx_gpio_devtype_default;
401
402 /*
403 * It's assumed that only a single type of gpio controller is available
404 * on the current machine, so overwriting global data is fine.
405 */
406 mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;
407
408 gc->direction_output = devtype->gpio_dir_out ?: mpc8xxx_gpio_dir_out;
409 gc->get = devtype->gpio_get ?: mpc8xxx_gpio_get;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200410 gc->set = mpc8xxx_gpio_set;
Rojhalat Ibrahime5db3b32014-11-04 17:12:09 +0100411 gc->set_multiple = mpc8xxx_gpio_set_multiple;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100412 gc->to_irq = mpc8xxx_gpio_to_irq;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200413
Linus Walleij709d71a2015-12-07 10:34:28 +0100414 ret = of_mm_gpiochip_add_data(np, mm_gc, mpc8xxx_gc);
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200415 if (ret)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100416 return ret;
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200417
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100418 mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
419 if (mpc8xxx_gc->irqn == NO_IRQ)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100420 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100421
Grant Likelya8db8cf2012-02-14 14:06:54 -0700422 mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
423 &mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100424 if (!mpc8xxx_gc->irq)
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100425 return 0;
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100426
Anatolij Gustschine39d5ef2010-08-09 07:58:48 +0200427 id = of_match_node(mpc8xxx_gpio_ids, np);
428 if (id)
429 mpc8xxx_gc->of_dev_id_data = id->data;
430
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100431 /* ack and mask all irqs */
432 out_be32(mm_gc->regs + GPIO_IER, 0xffffffff);
433 out_be32(mm_gc->regs + GPIO_IMR, 0);
434
Thomas Gleixner05379812015-06-21 21:10:46 +0200435 irq_set_chained_handler_and_data(mpc8xxx_gc->irqn,
436 mpc8xxx_gpio_irq_cascade, mpc8xxx_gc);
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100437
438 return 0;
439}
440
441static int mpc8xxx_remove(struct platform_device *pdev)
442{
443 struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);
444
445 if (mpc8xxx_gc->irq) {
Thomas Gleixner05379812015-06-21 21:10:46 +0200446 irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100447 irq_domain_remove(mpc8xxx_gc->irq);
448 }
449
450 of_mm_gpiochip_remove(&mpc8xxx_gc->mm_gc);
Peter Korsgaard345e5c82010-01-07 17:57:46 +0100451
Peter Korsgaard1e16dfc2008-09-23 17:35:38 +0200452 return 0;
453}
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100454
455static struct platform_driver mpc8xxx_plat_driver = {
456 .probe = mpc8xxx_probe,
Ricardo Ribalda Delgado257e1072015-01-18 12:39:33 +0100457 .remove = mpc8xxx_remove,
Ricardo Ribalda Delgado98686d9a52015-01-18 12:39:32 +0100458 .driver = {
459 .name = "gpio-mpc8xxx",
460 .of_match_table = mpc8xxx_gpio_ids,
461 },
462};
463
464static int __init mpc8xxx_init(void)
465{
466 return platform_driver_register(&mpc8xxx_plat_driver);
467}
468
469arch_initcall(mpc8xxx_init);