ARM: RK: add initial support for RK3026
[firefly-linux-kernel-4.4.55.git] / drivers / gpio / gpio-rk30.c
1 /* arch/arm/mach-rk29/gpio.c
2  *
3  * Copyright (C) 2010 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/clk.h>
17 #include <linux/errno.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/debugfs.h>
21 #include <linux/seq_file.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/io.h>
26 #include <linux/syscore_ops.h>
27
28 #include <mach/hardware.h>
29 #include <mach/gpio.h>
30 #include <mach/io.h>
31 #include <mach/iomux.h>
32 #include <mach/pmu.h>
33 #include <asm/gpio.h>
34 #include <asm/mach/irq.h>
35
36 #if defined(CONFIG_ARCH_RK3066B) || defined(CONFIG_ARCH_RK3188)
37 #define MAX_PIN RK30_PIN3_PD7
38 #elif defined(CONFIG_ARCH_RK30)
39 #define MAX_PIN RK30_PIN6_PB7
40 #elif defined(CONFIG_ARCH_RK2928) || defined(CONFIG_ARCH_RK3026)
41 #define MAX_PIN RK2928_PIN3_PD7
42 #define RK30_GPIO0_PHYS RK2928_GPIO0_PHYS
43 #define RK30_GPIO0_BASE RK2928_GPIO0_BASE
44 #define RK30_GPIO0_SIZE RK2928_GPIO0_SIZE
45 #define RK30_GPIO1_PHYS RK2928_GPIO1_PHYS
46 #define RK30_GPIO1_BASE RK2928_GPIO1_BASE
47 #define RK30_GPIO1_SIZE RK2928_GPIO1_SIZE
48 #define RK30_GPIO2_PHYS RK2928_GPIO2_PHYS
49 #define RK30_GPIO2_BASE RK2928_GPIO2_BASE
50 #define RK30_GPIO2_SIZE RK2928_GPIO2_SIZE
51 #define RK30_GPIO3_PHYS RK2928_GPIO3_PHYS
52 #define RK30_GPIO3_BASE RK2928_GPIO3_BASE
53 #define RK30_GPIO3_SIZE RK2928_GPIO3_SIZE
54 #define RK30_GRF_BASE   RK2928_GRF_BASE
55 #endif
56
57 #define to_rk30_gpio_bank(c) container_of(c, struct rk30_gpio_bank, chip)
58
59 struct rk30_gpio_bank {
60         struct gpio_chip chip;
61         unsigned short id;
62         short irq;
63         void __iomem *regbase;  /* Base of register bank */
64         struct clk *clk;
65         u32 suspend_wakeup;
66         u32 saved_wakeup;
67         spinlock_t lock;
68 };
69
70 static struct lock_class_key gpio_lock_class;
71
72 static void rk30_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
73 static void rk30_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
74 static int rk30_gpiolib_get(struct gpio_chip *chip, unsigned offset);
75 static int rk30_gpiolib_direction_output(struct gpio_chip *chip,unsigned offset, int val);
76 static int rk30_gpiolib_direction_input(struct gpio_chip *chip,unsigned offset);
77 static int rk30_gpiolib_pull_updown(struct gpio_chip *chip, unsigned offset, unsigned enable);
78 static int rk30_gpiolib_to_irq(struct gpio_chip *chip,unsigned offset);
79 static int rk30_gpiolib_request(struct gpio_chip *chip, unsigned offset);
80
81 #define RK30_GPIO_BANK(ID)                      \
82         {                                                               \
83                 .chip = {                                               \
84                         .label            = "gpio" #ID,                 \
85                         .direction_input  = rk30_gpiolib_direction_input, \
86                         .direction_output = rk30_gpiolib_direction_output, \
87                         .get              = rk30_gpiolib_get,           \
88                         .set              = rk30_gpiolib_set,           \
89                         .request          = rk30_gpiolib_request,       \
90                         .pull_updown      = rk30_gpiolib_pull_updown,   \
91                         .dbg_show         = rk30_gpiolib_dbg_show,      \
92                         .to_irq           = rk30_gpiolib_to_irq,        \
93                         .base             = PIN_BASE + ID*NUM_GROUP,    \
94                         .ngpio            = ID < 6 ? NUM_GROUP : 16,    \
95                 },                                                      \
96                 .id = ID, \
97                 .irq = IRQ_GPIO##ID, \
98                 .regbase = (unsigned char __iomem *) RK30_GPIO##ID##_BASE, \
99         }
100
101 static struct rk30_gpio_bank rk30_gpio_banks[] = {
102         RK30_GPIO_BANK(0),
103         RK30_GPIO_BANK(1),
104         RK30_GPIO_BANK(2),
105         RK30_GPIO_BANK(3),
106 #if defined(CONFIG_ARCH_RK30) && !defined(CONFIG_ARCH_RK3066B)
107         RK30_GPIO_BANK(4),
108         RK30_GPIO_BANK(6),
109 #endif
110 };
111
112 static inline void rk30_gpio_bit_op(void __iomem *regbase, unsigned int offset, u32 bit, unsigned char flag)
113 {
114         u32 val = __raw_readl(regbase + offset);
115         if (flag)
116                 val |= bit;
117         else
118                 val &= ~bit;
119         __raw_writel(val, regbase + offset);
120 }
121
122 static inline struct gpio_chip *pin_to_gpio_chip(unsigned pin)
123 {
124         if (pin < PIN_BASE || pin > MAX_PIN)
125                 return NULL;
126
127         pin -= PIN_BASE;
128         pin /= NUM_GROUP;
129         if (likely(pin < ARRAY_SIZE(rk30_gpio_banks)))
130                 return &(rk30_gpio_banks[pin].chip);
131         return NULL;
132 }
133
134 static inline unsigned gpio_to_bit(unsigned gpio)
135 {
136         gpio -= PIN_BASE;
137         return 1u << (gpio % NUM_GROUP);
138 }
139
140 static inline unsigned offset_to_bit(unsigned offset)
141 {
142         return 1u << offset;
143 }
144
145 static void GPIOSetPinLevel(void __iomem *regbase, unsigned int bit, eGPIOPinLevel_t level)
146 {
147         rk30_gpio_bit_op(regbase, GPIO_SWPORT_DDR, bit, 1);
148         rk30_gpio_bit_op(regbase, GPIO_SWPORT_DR, bit, level);
149 }
150
151 static int GPIOGetPinLevel(void __iomem *regbase, unsigned int bit)
152 {
153         return ((__raw_readl(regbase + GPIO_EXT_PORT) & bit) != 0);
154 }
155
156 static void GPIOSetPinDirection(void __iomem *regbase, unsigned int bit, eGPIOPinDirection_t direction)
157 {
158         rk30_gpio_bit_op(regbase, GPIO_SWPORT_DDR, bit, direction);
159         /* Enable debounce may halt cpu on wfi, disable it by default */
160         //rk30_gpio_bit_op(regbase, GPIO_DEBOUNCE, bit, 1);
161 }
162
163 static void GPIOEnableIntr(void __iomem *regbase, unsigned int bit)
164 {
165         rk30_gpio_bit_op(regbase, GPIO_INTEN, bit, 1);
166 }
167
168 static void GPIODisableIntr(void __iomem *regbase, unsigned int bit)
169 {
170         rk30_gpio_bit_op(regbase, GPIO_INTEN, bit, 0);
171 }
172
173 static void GPIOAckIntr(void __iomem *regbase, unsigned int bit)
174 {
175         rk30_gpio_bit_op(regbase, GPIO_PORTS_EOI, bit, 1);
176 }
177
178 static void GPIOSetIntrType(void __iomem *regbase, unsigned int bit, eGPIOIntType_t type)
179 {
180         switch (type) {
181         case GPIOLevelLow:
182                 rk30_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 0);
183                 rk30_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 0);
184                 break;
185         case GPIOLevelHigh:
186                 rk30_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 0);
187                 rk30_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 1);
188                 break;
189         case GPIOEdgelFalling:
190                 rk30_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 1);
191                 rk30_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 0);
192                 break;
193         case GPIOEdgelRising:
194                 rk30_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 1);
195                 rk30_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 1);
196                 break;
197         }
198 }
199
200 static int rk30_gpio_irq_set_type(struct irq_data *d, unsigned int type)
201 {
202         struct rk30_gpio_bank *bank = irq_data_get_irq_chip_data(d);
203         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
204         eGPIOIntType_t int_type;
205         unsigned long flags;
206
207         switch (type) {
208         case IRQ_TYPE_EDGE_RISING:
209                 int_type = GPIOEdgelRising;
210                 break;
211         case IRQ_TYPE_EDGE_FALLING:
212                 int_type = GPIOEdgelFalling;
213                 break;
214         case IRQ_TYPE_LEVEL_HIGH:
215                 int_type = GPIOLevelHigh;
216                 break;
217         case IRQ_TYPE_LEVEL_LOW:
218                 int_type = GPIOLevelLow;
219                 break;
220         default:
221                 return -EINVAL;
222         }
223
224         spin_lock_irqsave(&bank->lock, flags);
225         //ÉèÖÃΪÖжÏ֮ǰ£¬±ØÐëÏÈÉèÖÃΪÊäÈë״̬
226         GPIOSetPinDirection(bank->regbase, bit, GPIO_IN);
227         GPIOSetIntrType(bank->regbase, bit, int_type);
228         spin_unlock_irqrestore(&bank->lock, flags);
229
230         if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
231                 __irq_set_handler_locked(d->irq, handle_level_irq);
232         else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
233                 __irq_set_handler_locked(d->irq, handle_edge_irq);
234
235         return 0;
236 }
237
238 static int rk30_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
239 {
240         struct rk30_gpio_bank *bank = irq_data_get_irq_chip_data(d);
241         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
242         unsigned long flags;
243
244         spin_lock_irqsave(&bank->lock, flags);
245         if (on)
246                 bank->suspend_wakeup |= bit;
247         else
248                 bank->suspend_wakeup &= ~bit;
249         spin_unlock_irqrestore(&bank->lock, flags);
250
251         return 0;
252 }
253
254 static void rk30_gpio_irq_unmask(struct irq_data *d)
255 {
256         struct rk30_gpio_bank *bank = irq_data_get_irq_chip_data(d);
257         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
258         unsigned long flags;
259
260         spin_lock_irqsave(&bank->lock, flags);
261         GPIOEnableIntr(bank->regbase, bit);
262         spin_unlock_irqrestore(&bank->lock, flags);
263 }
264
265 static void rk30_gpio_irq_mask(struct irq_data *d)
266 {
267         struct rk30_gpio_bank *bank = irq_data_get_irq_chip_data(d);
268         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
269         unsigned long flags;
270
271         spin_lock_irqsave(&bank->lock, flags);
272         GPIODisableIntr(bank->regbase, bit);
273         spin_unlock_irqrestore(&bank->lock, flags);
274 }
275
276 static void rk30_gpio_irq_ack(struct irq_data *d)
277 {
278         struct rk30_gpio_bank *bank = irq_data_get_irq_chip_data(d);
279         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
280
281         GPIOAckIntr(bank->regbase, bit);
282 }
283
284 static int rk30_gpiolib_direction_output(struct gpio_chip *chip, unsigned offset, int val)
285 {
286         struct rk30_gpio_bank *bank = to_rk30_gpio_bank(chip);
287         u32 bit = offset_to_bit(offset);
288         unsigned long flags;
289
290         spin_lock_irqsave(&bank->lock, flags);
291         GPIOSetPinDirection(bank->regbase, bit, GPIO_OUT);
292         GPIOSetPinLevel(bank->regbase, bit, val);
293         spin_unlock_irqrestore(&bank->lock, flags);
294         return 0;
295 }
296
297 static int rk30_gpiolib_direction_input(struct gpio_chip *chip,unsigned offset)
298 {
299         struct rk30_gpio_bank *bank = to_rk30_gpio_bank(chip);
300         unsigned long flags;
301
302         spin_lock_irqsave(&bank->lock, flags);
303         GPIOSetPinDirection(bank->regbase, offset_to_bit(offset), GPIO_IN);
304         spin_unlock_irqrestore(&bank->lock, flags);
305         return 0;
306 }
307
308 static int rk30_gpiolib_request(struct gpio_chip *chip, unsigned offset)
309 {
310         iomux_set_gpio_mode(chip->base + offset);
311         return 0;
312 }
313
314 static int rk30_gpiolib_get(struct gpio_chip *chip, unsigned offset)
315 {
316         return GPIOGetPinLevel(to_rk30_gpio_bank(chip)->regbase, offset_to_bit(offset));
317 }
318
319 static void rk30_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
320 {
321         struct rk30_gpio_bank *bank = to_rk30_gpio_bank(chip);
322         unsigned long flags;
323
324         spin_lock_irqsave(&bank->lock, flags);
325         GPIOSetPinLevel(bank->regbase, offset_to_bit(offset), val);
326         spin_unlock_irqrestore(&bank->lock, flags);
327 }
328
329 static int rk30_gpiolib_pull_updown(struct gpio_chip *chip, unsigned offset, enum GPIOPullType type)
330 {
331 #if defined(CONFIG_ARCH_RK3066B)
332 #else
333         struct rk30_gpio_bank *bank = to_rk30_gpio_bank(chip);
334         void __iomem *base;
335         u32 val;
336
337 #if defined(CONFIG_ARCH_RK3188)
338         /*
339          * pull setting
340          * 2'b00: Z(Noraml operaton)
341          * 2'b01: weak 1(pull-up)
342          * 2'b10: weak 0(pull-down)
343          * 2'b11: Repeater(Bus keeper)
344          */
345         switch (type) {
346         case PullDisable:
347                 val = 0;
348                 break;
349         case GPIOPullUp:
350                 val = 1;
351                 break;
352         case GPIOPullDown:
353                 val = 2;
354                 break;
355         default:
356                 WARN(1, "%s: unsupported pull type %d\n", __func__, type);
357                 return -EINVAL;
358         }
359
360         if (bank->id == 0 && offset < 12) {
361                 base = RK30_PMU_BASE + PMU_GPIO0A_PULL + ((offset / 8) * 4);
362                 offset = (offset % 8) * 2;
363                 __raw_writel((0x3 << (16 + offset)) | (val << offset), base);
364         } else {
365                 base = RK30_GRF_BASE + GRF_GPIO0B_PULL - 4 + bank->id * 16 + ((offset / 8) * 4);
366                 offset = (7 - (offset % 8)) * 2;
367                 __raw_writel((0x3 << (16 + offset)) | (val << offset), base);
368         }
369 #else
370         /* RK30XX && RK292X */
371         /*
372          * Values written to this register independently
373          * control Pullup/Pulldown or not for the
374          * corresponding data bit in GPIO.
375          * 0: pull up/down enable, PAD type will decide
376          * to be up or down, not related with this value
377          * 1: pull up/down disable
378         */
379         val = (type == PullDisable) ? 1 : 0;
380         base = RK30_GRF_BASE + GRF_GPIO0L_PULL + bank->id * 8 + ((offset / 16) * 4);
381         offset = offset % 16;
382         __raw_writel((1 << (16 + offset)) | (val << offset), base);
383 #endif
384 #endif
385         return 0;
386 }
387
388 static int rk30_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
389 {
390         return chip->base + offset;
391 }
392
393 static void rk30_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
394 {
395 #if 0
396         int i;
397
398         for (i = 0; i < chip->ngpio; i++) {
399                 unsigned pin = chip->base + i;
400                 struct gpio_chip *chip = pin_to_gpioChip(pin);
401                 u32 bit = pin_to_bit(pin);
402                 const char *gpio_label;
403                 
404                 if(!chip ||!bit)
405                         return;
406                 
407                 gpio_label = gpiochip_is_requested(chip, i);
408                 if (gpio_label) {
409                         seq_printf(s, "[%s] GPIO%s%d: ",
410                                    gpio_label, chip->label, i);
411                         
412                         if (!chip || !bit)
413                         {
414                                 seq_printf(s, "!chip || !bit\t");
415                                 return;
416                         }
417                                 
418                         GPIOSetPinDirection(chip,bit,GPIO_IN);
419                         seq_printf(s, "pin=%d,level=%d\t", pin,GPIOGetPinLevel(chip,bit));
420                         seq_printf(s, "\t");
421                 }
422         }
423 #endif
424 }
425
426 static void rk30_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
427 {
428         struct rk30_gpio_bank *bank = irq_get_handler_data(irq);
429         struct irq_chip *chip = irq_desc_get_chip(desc);
430         unsigned gpio_irq;
431         u32 isr, ilr;
432         unsigned pin;
433         unsigned unmasked = 0;
434
435         chained_irq_enter(chip, desc);
436
437         isr = __raw_readl(bank->regbase + GPIO_INT_STATUS);
438         ilr = __raw_readl(bank->regbase + GPIO_INTTYPE_LEVEL);
439
440         gpio_irq = gpio_to_irq(bank->chip.base);
441
442         while (isr) {
443                 pin = fls(isr) - 1;
444                 /* if gpio is edge triggered, clear condition
445                  * before executing the hander so that we don't
446                  * miss edges
447                  */
448                 if (ilr & (1 << pin)) {
449                         unmasked = 1;
450                         chained_irq_exit(chip, desc);
451                 }
452
453                 generic_handle_irq(gpio_irq + pin);
454                 isr &= ~(1 << pin);
455         }
456
457         if (!unmasked)
458                 chained_irq_exit(chip, desc);
459 }
460
461 static struct irq_chip rk30_gpio_irq_chip = {
462         .name           = "GPIO",
463         .irq_ack        = rk30_gpio_irq_ack,
464         .irq_disable    = rk30_gpio_irq_mask,
465         .irq_mask       = rk30_gpio_irq_mask,
466         .irq_unmask     = rk30_gpio_irq_unmask,
467         .irq_set_type   = rk30_gpio_irq_set_type,
468         .irq_set_wake   = rk30_gpio_irq_set_wake,
469 };
470
471 void __init rk30_gpio_init(void)
472 {
473         unsigned int i, j, pin, irqs = 0;
474         struct rk30_gpio_bank *bank;
475
476         bank = rk30_gpio_banks;
477
478         for (i = 0; i < ARRAY_SIZE(rk30_gpio_banks); i++, bank++) {
479                 spin_lock_init(&bank->lock);
480                 bank->clk = clk_get(NULL, bank->chip.label);
481                 clk_enable(bank->clk);
482                 gpiochip_add(&bank->chip);
483
484                 __raw_writel(0, bank->regbase + GPIO_INTEN);
485                 pin = bank->chip.base;
486                 for (j = 0; j < 32; j++) {
487                         unsigned int irq = gpio_to_irq(pin);
488                         if (pin > MAX_PIN)
489                                 break;
490                         irq_set_lockdep_class(irq, &gpio_lock_class);
491                         irq_set_chip_data(irq, bank);
492                         irq_set_chip_and_handler(irq, &rk30_gpio_irq_chip, handle_level_irq);
493                         set_irq_flags(irq, IRQF_VALID);
494                         pin++;
495                         irqs++;
496                 }
497
498                 irq_set_handler_data(bank->irq, bank);
499                 irq_set_chained_handler(bank->irq, rk30_gpio_irq_handler);
500         }
501         printk("%s: %d gpio irqs in %d banks\n", __func__, irqs, ARRAY_SIZE(rk30_gpio_banks));
502 }
503
504 #ifdef CONFIG_PM
505 __weak void rk30_setgpio_suspend_board(void)
506 {
507 }
508
509 __weak void rk30_setgpio_resume_board(void)
510 {
511 }
512
513 static int rk30_gpio_suspend(void)
514 {
515         unsigned i;
516         
517         rk30_setgpio_suspend_board();
518
519         for (i = 0; i < ARRAY_SIZE(rk30_gpio_banks); i++) {
520                 struct rk30_gpio_bank *bank = &rk30_gpio_banks[i];
521
522                 bank->saved_wakeup = __raw_readl(bank->regbase + GPIO_INTEN);
523                 __raw_writel(bank->suspend_wakeup, bank->regbase + GPIO_INTEN);
524
525                 if (!bank->suspend_wakeup)
526                         clk_disable(bank->clk);
527         }
528
529         return 0;
530 }
531
532 static void rk30_gpio_resume(void)
533 {
534         unsigned i;
535
536         for (i = 0; i < ARRAY_SIZE(rk30_gpio_banks); i++) {
537                 struct rk30_gpio_bank *bank = &rk30_gpio_banks[i];
538                 u32 isr;
539
540                 if (!bank->suspend_wakeup)
541                         clk_enable(bank->clk);
542
543                 /* keep enable for resume irq */
544                 isr = __raw_readl(bank->regbase + GPIO_INT_STATUS);
545                 __raw_writel(bank->saved_wakeup | (bank->suspend_wakeup & isr), bank->regbase + GPIO_INTEN);
546         }
547
548         rk30_setgpio_resume_board();
549 }
550
551 static struct syscore_ops rk30_gpio_syscore_ops = {
552         .suspend        = rk30_gpio_suspend,
553         .resume         = rk30_gpio_resume,
554 };
555
556 static int __init rk30_gpio_sysinit(void)
557 {
558         register_syscore_ops(&rk30_gpio_syscore_ops);
559         return 0;
560 }
561
562 arch_initcall(rk30_gpio_sysinit);
563 #endif