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