GNU Linux-libre 4.14.303-gnu1
[releases.git] / drivers / gpio / gpio-vr41xx.c
1 /*
2  *  Driver for NEC VR4100 series General-purpose I/O Unit.
3  *
4  *  Copyright (C) 2002 MontaVista Software Inc.
5  *      Author: Yoichi Yuasa <source@mvista.com>
6  *  Copyright (C) 2003-2009  Yoichi Yuasa <yuasa@linux-mips.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 #include <linux/errno.h>
23 #include <linux/fs.h>
24 #include <linux/gpio.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/platform_device.h>
32 #include <linux/spinlock.h>
33 #include <linux/types.h>
34
35 #include <asm/vr41xx/giu.h>
36 #include <asm/vr41xx/irq.h>
37 #include <asm/vr41xx/vr41xx.h>
38
39 MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
40 MODULE_DESCRIPTION("NEC VR4100 series General-purpose I/O Unit driver");
41 MODULE_LICENSE("GPL");
42
43 #define GIUIOSELL       0x00
44 #define GIUIOSELH       0x02
45 #define GIUPIODL        0x04
46 #define GIUPIODH        0x06
47 #define GIUINTSTATL     0x08
48 #define GIUINTSTATH     0x0a
49 #define GIUINTENL       0x0c
50 #define GIUINTENH       0x0e
51 #define GIUINTTYPL      0x10
52 #define GIUINTTYPH      0x12
53 #define GIUINTALSELL    0x14
54 #define GIUINTALSELH    0x16
55 #define GIUINTHTSELL    0x18
56 #define GIUINTHTSELH    0x1a
57 #define GIUPODATL       0x1c
58 #define GIUPODATEN      0x1c
59 #define GIUPODATH       0x1e
60  #define PIOEN0         0x0100
61  #define PIOEN1         0x0200
62 #define GIUPODAT        0x1e
63 #define GIUFEDGEINHL    0x20
64 #define GIUFEDGEINHH    0x22
65 #define GIUREDGEINHL    0x24
66 #define GIUREDGEINHH    0x26
67
68 #define GIUUSEUPDN      0x1e0
69 #define GIUTERMUPDN     0x1e2
70
71 #define GPIO_HAS_PULLUPDOWN_IO          0x0001
72 #define GPIO_HAS_OUTPUT_ENABLE          0x0002
73 #define GPIO_HAS_INTERRUPT_EDGE_SELECT  0x0100
74
75 enum {
76         GPIO_INPUT,
77         GPIO_OUTPUT,
78 };
79
80 static DEFINE_SPINLOCK(giu_lock);
81 static unsigned long giu_flags;
82
83 static void __iomem *giu_base;
84 static struct gpio_chip vr41xx_gpio_chip;
85
86 #define giu_read(offset)                readw(giu_base + (offset))
87 #define giu_write(offset, value)        writew((value), giu_base + (offset))
88
89 #define GPIO_PIN_OF_IRQ(irq)    ((irq) - GIU_IRQ_BASE)
90 #define GIUINT_HIGH_OFFSET      16
91 #define GIUINT_HIGH_MAX         32
92
93 static inline u16 giu_set(u16 offset, u16 set)
94 {
95         u16 data;
96
97         data = giu_read(offset);
98         data |= set;
99         giu_write(offset, data);
100
101         return data;
102 }
103
104 static inline u16 giu_clear(u16 offset, u16 clear)
105 {
106         u16 data;
107
108         data = giu_read(offset);
109         data &= ~clear;
110         giu_write(offset, data);
111
112         return data;
113 }
114
115 static void ack_giuint_low(struct irq_data *d)
116 {
117         giu_write(GIUINTSTATL, 1 << GPIO_PIN_OF_IRQ(d->irq));
118 }
119
120 static void mask_giuint_low(struct irq_data *d)
121 {
122         giu_clear(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(d->irq));
123 }
124
125 static void mask_ack_giuint_low(struct irq_data *d)
126 {
127         unsigned int pin;
128
129         pin = GPIO_PIN_OF_IRQ(d->irq);
130         giu_clear(GIUINTENL, 1 << pin);
131         giu_write(GIUINTSTATL, 1 << pin);
132 }
133
134 static void unmask_giuint_low(struct irq_data *d)
135 {
136         giu_set(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(d->irq));
137 }
138
139 static unsigned int startup_giuint(struct irq_data *data)
140 {
141         if (gpiochip_lock_as_irq(&vr41xx_gpio_chip, data->hwirq))
142                 dev_err(vr41xx_gpio_chip.parent,
143                         "unable to lock HW IRQ %lu for IRQ\n",
144                         data->hwirq);
145         /* Satisfy the .enable semantics by unmasking the line */
146         unmask_giuint_low(data);
147         return 0;
148 }
149
150 static void shutdown_giuint(struct irq_data *data)
151 {
152         mask_giuint_low(data);
153         gpiochip_unlock_as_irq(&vr41xx_gpio_chip, data->hwirq);
154 }
155
156 static struct irq_chip giuint_low_irq_chip = {
157         .name           = "GIUINTL",
158         .irq_ack        = ack_giuint_low,
159         .irq_mask       = mask_giuint_low,
160         .irq_mask_ack   = mask_ack_giuint_low,
161         .irq_unmask     = unmask_giuint_low,
162         .irq_startup    = startup_giuint,
163         .irq_shutdown   = shutdown_giuint,
164 };
165
166 static void ack_giuint_high(struct irq_data *d)
167 {
168         giu_write(GIUINTSTATH,
169                   1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
170 }
171
172 static void mask_giuint_high(struct irq_data *d)
173 {
174         giu_clear(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
175 }
176
177 static void mask_ack_giuint_high(struct irq_data *d)
178 {
179         unsigned int pin;
180
181         pin = GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET;
182         giu_clear(GIUINTENH, 1 << pin);
183         giu_write(GIUINTSTATH, 1 << pin);
184 }
185
186 static void unmask_giuint_high(struct irq_data *d)
187 {
188         giu_set(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
189 }
190
191 static struct irq_chip giuint_high_irq_chip = {
192         .name           = "GIUINTH",
193         .irq_ack        = ack_giuint_high,
194         .irq_mask       = mask_giuint_high,
195         .irq_mask_ack   = mask_ack_giuint_high,
196         .irq_unmask     = unmask_giuint_high,
197 };
198
199 static int giu_get_irq(unsigned int irq)
200 {
201         u16 pendl, pendh, maskl, maskh;
202         int i;
203
204         pendl = giu_read(GIUINTSTATL);
205         pendh = giu_read(GIUINTSTATH);
206         maskl = giu_read(GIUINTENL);
207         maskh = giu_read(GIUINTENH);
208
209         maskl &= pendl;
210         maskh &= pendh;
211
212         if (maskl) {
213                 for (i = 0; i < 16; i++) {
214                         if (maskl & (1 << i))
215                                 return GIU_IRQ(i);
216                 }
217         } else if (maskh) {
218                 for (i = 0; i < 16; i++) {
219                         if (maskh & (1 << i))
220                                 return GIU_IRQ(i + GIUINT_HIGH_OFFSET);
221                 }
222         }
223
224         printk(KERN_ERR "spurious GIU interrupt: %04x(%04x),%04x(%04x)\n",
225                maskl, pendl, maskh, pendh);
226
227         return -EINVAL;
228 }
229
230 void vr41xx_set_irq_trigger(unsigned int pin, irq_trigger_t trigger,
231                             irq_signal_t signal)
232 {
233         u16 mask;
234
235         if (pin < GIUINT_HIGH_OFFSET) {
236                 mask = 1 << pin;
237                 if (trigger != IRQ_TRIGGER_LEVEL) {
238                         giu_set(GIUINTTYPL, mask);
239                         if (signal == IRQ_SIGNAL_HOLD)
240                                 giu_set(GIUINTHTSELL, mask);
241                         else
242                                 giu_clear(GIUINTHTSELL, mask);
243                         if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
244                                 switch (trigger) {
245                                 case IRQ_TRIGGER_EDGE_FALLING:
246                                         giu_set(GIUFEDGEINHL, mask);
247                                         giu_clear(GIUREDGEINHL, mask);
248                                         break;
249                                 case IRQ_TRIGGER_EDGE_RISING:
250                                         giu_clear(GIUFEDGEINHL, mask);
251                                         giu_set(GIUREDGEINHL, mask);
252                                         break;
253                                 default:
254                                         giu_set(GIUFEDGEINHL, mask);
255                                         giu_set(GIUREDGEINHL, mask);
256                                         break;
257                                 }
258                         }
259                         irq_set_chip_and_handler(GIU_IRQ(pin),
260                                                  &giuint_low_irq_chip,
261                                                  handle_edge_irq);
262                 } else {
263                         giu_clear(GIUINTTYPL, mask);
264                         giu_clear(GIUINTHTSELL, mask);
265                         irq_set_chip_and_handler(GIU_IRQ(pin),
266                                                  &giuint_low_irq_chip,
267                                                  handle_level_irq);
268                 }
269                 giu_write(GIUINTSTATL, mask);
270         } else if (pin < GIUINT_HIGH_MAX) {
271                 mask = 1 << (pin - GIUINT_HIGH_OFFSET);
272                 if (trigger != IRQ_TRIGGER_LEVEL) {
273                         giu_set(GIUINTTYPH, mask);
274                         if (signal == IRQ_SIGNAL_HOLD)
275                                 giu_set(GIUINTHTSELH, mask);
276                         else
277                                 giu_clear(GIUINTHTSELH, mask);
278                         if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
279                                 switch (trigger) {
280                                 case IRQ_TRIGGER_EDGE_FALLING:
281                                         giu_set(GIUFEDGEINHH, mask);
282                                         giu_clear(GIUREDGEINHH, mask);
283                                         break;
284                                 case IRQ_TRIGGER_EDGE_RISING:
285                                         giu_clear(GIUFEDGEINHH, mask);
286                                         giu_set(GIUREDGEINHH, mask);
287                                         break;
288                                 default:
289                                         giu_set(GIUFEDGEINHH, mask);
290                                         giu_set(GIUREDGEINHH, mask);
291                                         break;
292                                 }
293                         }
294                         irq_set_chip_and_handler(GIU_IRQ(pin),
295                                                  &giuint_high_irq_chip,
296                                                  handle_edge_irq);
297                 } else {
298                         giu_clear(GIUINTTYPH, mask);
299                         giu_clear(GIUINTHTSELH, mask);
300                         irq_set_chip_and_handler(GIU_IRQ(pin),
301                                                  &giuint_high_irq_chip,
302                                                  handle_level_irq);
303                 }
304                 giu_write(GIUINTSTATH, mask);
305         }
306 }
307 EXPORT_SYMBOL_GPL(vr41xx_set_irq_trigger);
308
309 void vr41xx_set_irq_level(unsigned int pin, irq_level_t level)
310 {
311         u16 mask;
312
313         if (pin < GIUINT_HIGH_OFFSET) {
314                 mask = 1 << pin;
315                 if (level == IRQ_LEVEL_HIGH)
316                         giu_set(GIUINTALSELL, mask);
317                 else
318                         giu_clear(GIUINTALSELL, mask);
319                 giu_write(GIUINTSTATL, mask);
320         } else if (pin < GIUINT_HIGH_MAX) {
321                 mask = 1 << (pin - GIUINT_HIGH_OFFSET);
322                 if (level == IRQ_LEVEL_HIGH)
323                         giu_set(GIUINTALSELH, mask);
324                 else
325                         giu_clear(GIUINTALSELH, mask);
326                 giu_write(GIUINTSTATH, mask);
327         }
328 }
329 EXPORT_SYMBOL_GPL(vr41xx_set_irq_level);
330
331 static int giu_set_direction(struct gpio_chip *chip, unsigned pin, int dir)
332 {
333         u16 offset, mask, reg;
334         unsigned long flags;
335
336         if (pin >= chip->ngpio)
337                 return -EINVAL;
338
339         if (pin < 16) {
340                 offset = GIUIOSELL;
341                 mask = 1 << pin;
342         } else if (pin < 32) {
343                 offset = GIUIOSELH;
344                 mask = 1 << (pin - 16);
345         } else {
346                 if (giu_flags & GPIO_HAS_OUTPUT_ENABLE) {
347                         offset = GIUPODATEN;
348                         mask = 1 << (pin - 32);
349                 } else {
350                         switch (pin) {
351                         case 48:
352                                 offset = GIUPODATH;
353                                 mask = PIOEN0;
354                                 break;
355                         case 49:
356                                 offset = GIUPODATH;
357                                 mask = PIOEN1;
358                                 break;
359                         default:
360                                 return -EINVAL;
361                         }
362                 }
363         }
364
365         spin_lock_irqsave(&giu_lock, flags);
366
367         reg = giu_read(offset);
368         if (dir == GPIO_OUTPUT)
369                 reg |= mask;
370         else
371                 reg &= ~mask;
372         giu_write(offset, reg);
373
374         spin_unlock_irqrestore(&giu_lock, flags);
375
376         return 0;
377 }
378
379 int vr41xx_gpio_pullupdown(unsigned int pin, gpio_pull_t pull)
380 {
381         u16 reg, mask;
382         unsigned long flags;
383
384         if ((giu_flags & GPIO_HAS_PULLUPDOWN_IO) != GPIO_HAS_PULLUPDOWN_IO)
385                 return -EPERM;
386
387         if (pin >= 15)
388                 return -EINVAL;
389
390         mask = 1 << pin;
391
392         spin_lock_irqsave(&giu_lock, flags);
393
394         if (pull == GPIO_PULL_UP || pull == GPIO_PULL_DOWN) {
395                 reg = giu_read(GIUTERMUPDN);
396                 if (pull == GPIO_PULL_UP)
397                         reg |= mask;
398                 else
399                         reg &= ~mask;
400                 giu_write(GIUTERMUPDN, reg);
401
402                 reg = giu_read(GIUUSEUPDN);
403                 reg |= mask;
404                 giu_write(GIUUSEUPDN, reg);
405         } else {
406                 reg = giu_read(GIUUSEUPDN);
407                 reg &= ~mask;
408                 giu_write(GIUUSEUPDN, reg);
409         }
410
411         spin_unlock_irqrestore(&giu_lock, flags);
412
413         return 0;
414 }
415 EXPORT_SYMBOL_GPL(vr41xx_gpio_pullupdown);
416
417 static int vr41xx_gpio_get(struct gpio_chip *chip, unsigned pin)
418 {
419         u16 reg, mask;
420
421         if (pin >= chip->ngpio)
422                 return -EINVAL;
423
424         if (pin < 16) {
425                 reg = giu_read(GIUPIODL);
426                 mask = 1 << pin;
427         } else if (pin < 32) {
428                 reg = giu_read(GIUPIODH);
429                 mask = 1 << (pin - 16);
430         } else if (pin < 48) {
431                 reg = giu_read(GIUPODATL);
432                 mask = 1 << (pin - 32);
433         } else {
434                 reg = giu_read(GIUPODATH);
435                 mask = 1 << (pin - 48);
436         }
437
438         if (reg & mask)
439                 return 1;
440
441         return 0;
442 }
443
444 static void vr41xx_gpio_set(struct gpio_chip *chip, unsigned pin,
445                             int value)
446 {
447         u16 offset, mask, reg;
448         unsigned long flags;
449
450         if (pin >= chip->ngpio)
451                 return;
452
453         if (pin < 16) {
454                 offset = GIUPIODL;
455                 mask = 1 << pin;
456         } else if (pin < 32) {
457                 offset = GIUPIODH;
458                 mask = 1 << (pin - 16);
459         } else if (pin < 48) {
460                 offset = GIUPODATL;
461                 mask = 1 << (pin - 32);
462         } else {
463                 offset = GIUPODATH;
464                 mask = 1 << (pin - 48);
465         }
466
467         spin_lock_irqsave(&giu_lock, flags);
468
469         reg = giu_read(offset);
470         if (value)
471                 reg |= mask;
472         else
473                 reg &= ~mask;
474         giu_write(offset, reg);
475
476         spin_unlock_irqrestore(&giu_lock, flags);
477 }
478
479
480 static int vr41xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
481 {
482         return giu_set_direction(chip, offset, GPIO_INPUT);
483 }
484
485 static int vr41xx_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
486                                 int value)
487 {
488         vr41xx_gpio_set(chip, offset, value);
489
490         return giu_set_direction(chip, offset, GPIO_OUTPUT);
491 }
492
493 static int vr41xx_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
494 {
495         if (offset >= chip->ngpio)
496                 return -EINVAL;
497
498         return GIU_IRQ_BASE + offset;
499 }
500
501 static struct gpio_chip vr41xx_gpio_chip = {
502         .label                  = "vr41xx",
503         .owner                  = THIS_MODULE,
504         .direction_input        = vr41xx_gpio_direction_input,
505         .get                    = vr41xx_gpio_get,
506         .direction_output       = vr41xx_gpio_direction_output,
507         .set                    = vr41xx_gpio_set,
508         .to_irq                 = vr41xx_gpio_to_irq,
509 };
510
511 static int giu_probe(struct platform_device *pdev)
512 {
513         struct resource *res;
514         unsigned int trigger, i, pin;
515         struct irq_chip *chip;
516         int irq, ret;
517
518         switch (pdev->id) {
519         case GPIO_50PINS_PULLUPDOWN:
520                 giu_flags = GPIO_HAS_PULLUPDOWN_IO;
521                 vr41xx_gpio_chip.ngpio = 50;
522                 break;
523         case GPIO_36PINS:
524                 vr41xx_gpio_chip.ngpio = 36;
525                 break;
526         case GPIO_48PINS_EDGE_SELECT:
527                 giu_flags = GPIO_HAS_INTERRUPT_EDGE_SELECT;
528                 vr41xx_gpio_chip.ngpio = 48;
529                 break;
530         default:
531                 dev_err(&pdev->dev, "GIU: unknown ID %d\n", pdev->id);
532                 return -ENODEV;
533         }
534
535         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
536         if (!res)
537                 return -EBUSY;
538
539         giu_base = ioremap(res->start, resource_size(res));
540         if (!giu_base)
541                 return -ENOMEM;
542
543         vr41xx_gpio_chip.parent = &pdev->dev;
544
545         ret = gpiochip_add_data(&vr41xx_gpio_chip, NULL);
546         if (!ret) {
547                 iounmap(giu_base);
548                 return -ENODEV;
549         }
550
551         giu_write(GIUINTENL, 0);
552         giu_write(GIUINTENH, 0);
553
554         trigger = giu_read(GIUINTTYPH) << 16;
555         trigger |= giu_read(GIUINTTYPL);
556         for (i = GIU_IRQ_BASE; i <= GIU_IRQ_LAST; i++) {
557                 pin = GPIO_PIN_OF_IRQ(i);
558                 if (pin < GIUINT_HIGH_OFFSET)
559                         chip = &giuint_low_irq_chip;
560                 else
561                         chip = &giuint_high_irq_chip;
562
563                 if (trigger & (1 << pin))
564                         irq_set_chip_and_handler(i, chip, handle_edge_irq);
565                 else
566                         irq_set_chip_and_handler(i, chip, handle_level_irq);
567
568         }
569
570         irq = platform_get_irq(pdev, 0);
571         if (irq < 0 || irq >= nr_irqs)
572                 return -EBUSY;
573
574         return cascade_irq(irq, giu_get_irq);
575 }
576
577 static int giu_remove(struct platform_device *pdev)
578 {
579         if (giu_base) {
580                 iounmap(giu_base);
581                 giu_base = NULL;
582         }
583
584         return 0;
585 }
586
587 static struct platform_driver giu_device_driver = {
588         .probe          = giu_probe,
589         .remove         = giu_remove,
590         .driver         = {
591                 .name   = "GIU",
592         },
593 };
594
595 module_platform_driver(giu_device_driver);