usb: dwc3: rockchip: fix possible circular deadlock
[firefly-linux-kernel-4.4.55.git] / drivers / gpio / gpio-rk29.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/rk29_iomap.h>
31 #include <mach/iomux.h>
32 #include <asm/gpio.h>
33 #include <asm/mach/irq.h>
34
35 #define MAX_PIN RK29_PIN6_PD7
36
37 #define to_rk29_gpio_bank(c) container_of(c, struct rk29_gpio_bank, chip)
38
39 struct rk29_gpio_bank {
40         struct gpio_chip chip;
41         unsigned short id;
42         short irq;
43         void __iomem *regbase;  /* Base of register bank */
44         struct clk *clk;
45         u32 suspend_wakeup;
46         u32 saved_wakeup;
47         spinlock_t lock;
48 };
49
50 static struct lock_class_key gpio_lock_class;
51
52 static void rk29_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
53 static void rk29_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
54 static int rk29_gpiolib_get(struct gpio_chip *chip, unsigned offset);
55 static int rk29_gpiolib_direction_output(struct gpio_chip *chip,unsigned offset, int val);
56 static int rk29_gpiolib_direction_input(struct gpio_chip *chip,unsigned offset);
57 static int rk29_gpiolib_pull_updown(struct gpio_chip *chip, unsigned offset, unsigned enable);
58 static int rk29_gpiolib_to_irq(struct gpio_chip *chip,unsigned offset);
59
60 #define RK29_GPIO_BANK(ID)                      \
61         {                                                               \
62                 .chip = {                                               \
63                         .label            = "gpio" #ID,                 \
64                         .direction_input  = rk29_gpiolib_direction_input, \
65                         .direction_output = rk29_gpiolib_direction_output, \
66                         .get              = rk29_gpiolib_get,           \
67                         .set              = rk29_gpiolib_set,           \
68                         .pull_updown      = rk29_gpiolib_pull_updown,   \
69                         .dbg_show         = rk29_gpiolib_dbg_show,      \
70                         .to_irq           = rk29_gpiolib_to_irq,        \
71                         .base             = PIN_BASE + ID*NUM_GROUP,    \
72                         .ngpio            = NUM_GROUP,                  \
73                 },                                                      \
74                 .id = ID, \
75                 .irq = IRQ_GPIO##ID, \
76                 .regbase = (unsigned char __iomem *) RK29_GPIO##ID##_BASE, \
77         }
78
79 static struct rk29_gpio_bank rk29_gpio_banks[] = {
80         RK29_GPIO_BANK(0),
81         RK29_GPIO_BANK(1),
82         RK29_GPIO_BANK(2),
83         RK29_GPIO_BANK(3),
84         RK29_GPIO_BANK(4),
85         RK29_GPIO_BANK(5),
86         RK29_GPIO_BANK(6),
87 };
88
89 static inline void rk29_gpio_bit_op(void __iomem *regbase, unsigned int offset, u32 bit, unsigned char flag)
90 {
91         u32 val = __raw_readl(regbase + offset);
92         if (flag)
93                 val |= bit;
94         else
95                 val &= ~bit;
96         __raw_writel(val, regbase + offset);
97 }
98
99 static inline struct gpio_chip *pin_to_gpio_chip(unsigned pin)
100 {
101         if (pin < PIN_BASE || pin > MAX_PIN)
102                 return NULL;
103
104         pin -= PIN_BASE;
105         pin /= NUM_GROUP;
106         if (likely(pin < ARRAY_SIZE(rk29_gpio_banks)))
107                 return &(rk29_gpio_banks[pin].chip);
108         return NULL;
109 }
110
111 static inline unsigned gpio_to_bit(unsigned gpio)
112 {
113         gpio -= PIN_BASE;
114         return 1u << (gpio % NUM_GROUP);
115 }
116
117 static inline unsigned offset_to_bit(unsigned offset)
118 {
119         return 1u << offset;
120 }
121
122 static void GPIOSetPinLevel(void __iomem *regbase, unsigned int bit, eGPIOPinLevel_t level)
123 {
124         rk29_gpio_bit_op(regbase, GPIO_SWPORT_DDR, bit, 1);
125         rk29_gpio_bit_op(regbase, GPIO_SWPORT_DR, bit, level);
126 }
127
128 static int GPIOGetPinLevel(void __iomem *regbase, unsigned int bit)
129 {
130         return ((__raw_readl(regbase + GPIO_EXT_PORT) & bit) != 0);
131 }
132
133 static void GPIOSetPinDirection(void __iomem *regbase, unsigned int bit, eGPIOPinDirection_t direction)
134 {
135         rk29_gpio_bit_op(regbase, GPIO_SWPORT_DDR, bit, direction);
136         /* Enable debounce may halt cpu on wfi, disable it by default */
137         //rk29_gpio_bit_op(regbase, GPIO_DEBOUNCE, bit, 1);
138 }
139
140 static void GPIOEnableIntr(void __iomem *regbase, unsigned int bit)
141 {
142         rk29_gpio_bit_op(regbase, GPIO_INTEN, bit, 1);
143 }
144
145 static void GPIODisableIntr(void __iomem *regbase, unsigned int bit)
146 {
147         rk29_gpio_bit_op(regbase, GPIO_INTEN, bit, 0);
148 }
149
150 static void GPIOAckIntr(void __iomem *regbase, unsigned int bit)
151 {
152         rk29_gpio_bit_op(regbase, GPIO_PORTS_EOI, bit, 1);
153 }
154
155 static void GPIOSetIntrType(void __iomem *regbase, unsigned int bit, eGPIOIntType_t type)
156 {
157         switch (type) {
158         case GPIOLevelLow:
159                 rk29_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 0);
160                 rk29_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 0);
161                 break;
162         case GPIOLevelHigh:
163                 rk29_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 0);
164                 rk29_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 1);
165                 break;
166         case GPIOEdgelFalling:
167                 rk29_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 1);
168                 rk29_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 0);
169                 break;
170         case GPIOEdgelRising:
171                 rk29_gpio_bit_op(regbase, GPIO_INTTYPE_LEVEL, bit, 1);
172                 rk29_gpio_bit_op(regbase, GPIO_INT_POLARITY, bit, 1);
173                 break;
174         }
175 }
176
177 static int rk29_gpio_irq_set_type(struct irq_data *d, unsigned int type)
178 {
179         struct rk29_gpio_bank *bank = irq_data_get_irq_chip_data(d);
180         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
181         eGPIOIntType_t int_type;
182         unsigned long flags;
183
184         switch (type) {
185         case IRQ_TYPE_EDGE_RISING:
186                 int_type = GPIOEdgelRising;
187                 break;
188         case IRQ_TYPE_EDGE_FALLING:
189                 int_type = GPIOEdgelFalling;
190                 break;
191         case IRQ_TYPE_LEVEL_HIGH:
192                 int_type = GPIOLevelHigh;
193                 break;
194         case IRQ_TYPE_LEVEL_LOW:
195                 int_type = GPIOLevelLow;
196                 break;
197         default:
198                 return -EINVAL;
199         }
200
201         spin_lock_irqsave(&bank->lock, flags);
202         //ÉèÖÃΪÖжÏ֮ǰ£¬±ØÐëÏÈÉèÖÃΪÊäÈë״̬
203         GPIOSetPinDirection(bank->regbase, bit, GPIO_IN);
204         GPIOSetIntrType(bank->regbase, bit, int_type);
205         spin_unlock_irqrestore(&bank->lock, flags);
206
207         if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
208                 __irq_set_handler_locked(d->irq, handle_level_irq);
209         else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
210                 __irq_set_handler_locked(d->irq, handle_edge_irq);
211
212         return 0;
213 }
214
215 static int rk29_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
216 {
217         struct rk29_gpio_bank *bank = irq_data_get_irq_chip_data(d);
218         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
219         unsigned long flags;
220
221         spin_lock_irqsave(&bank->lock, flags);
222         if (on)
223                 bank->suspend_wakeup |= bit;
224         else
225                 bank->suspend_wakeup &= ~bit;
226         spin_unlock_irqrestore(&bank->lock, flags);
227
228         return 0;
229 }
230
231 static void rk29_gpio_irq_unmask(struct irq_data *d)
232 {
233         struct rk29_gpio_bank *bank = irq_data_get_irq_chip_data(d);
234         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
235         unsigned long flags;
236
237         spin_lock_irqsave(&bank->lock, flags);
238         GPIOEnableIntr(bank->regbase, bit);
239         spin_unlock_irqrestore(&bank->lock, flags);
240 }
241
242 static void rk29_gpio_irq_mask(struct irq_data *d)
243 {
244         struct rk29_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         GPIODisableIntr(bank->regbase, bit);
250         spin_unlock_irqrestore(&bank->lock, flags);
251 }
252
253 static void rk29_gpio_irq_ack(struct irq_data *d)
254 {
255         struct rk29_gpio_bank *bank = irq_data_get_irq_chip_data(d);
256         u32 bit = gpio_to_bit(irq_to_gpio(d->irq));
257
258         GPIOAckIntr(bank->regbase, bit);
259 }
260
261 static int rk29_gpiolib_direction_output(struct gpio_chip *chip, unsigned offset, int val)
262 {
263         struct rk29_gpio_bank *bank = to_rk29_gpio_bank(chip);
264         u32 bit = offset_to_bit(offset);
265         unsigned long flags;
266
267         spin_lock_irqsave(&bank->lock, flags);
268         GPIOSetPinDirection(bank->regbase, bit, GPIO_OUT);
269         GPIOSetPinLevel(bank->regbase, bit, val);
270         spin_unlock_irqrestore(&bank->lock, flags);
271         return 0;
272 }
273
274 static int rk29_gpiolib_direction_input(struct gpio_chip *chip,unsigned offset)
275 {
276         struct rk29_gpio_bank *bank = to_rk29_gpio_bank(chip);
277         unsigned long flags;
278
279         spin_lock_irqsave(&bank->lock, flags);
280         GPIOSetPinDirection(bank->regbase, offset_to_bit(offset), GPIO_IN);
281         spin_unlock_irqrestore(&bank->lock, flags);
282         return 0;
283 }
284
285
286 static int rk29_gpiolib_get(struct gpio_chip *chip, unsigned offset)
287 {
288         return GPIOGetPinLevel(to_rk29_gpio_bank(chip)->regbase, offset_to_bit(offset));
289 }
290
291 static void rk29_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
292 {
293         struct rk29_gpio_bank *bank = to_rk29_gpio_bank(chip);
294         unsigned long flags;
295
296         spin_lock_irqsave(&bank->lock, flags);
297         GPIOSetPinLevel(bank->regbase, offset_to_bit(offset), val);
298         spin_unlock_irqrestore(&bank->lock, flags);
299 }
300
301 static int rk29_gpiolib_pull_updown(struct gpio_chip *chip, unsigned offset, unsigned enable)
302 {
303         struct rk29_gpio_bank *bank = to_rk29_gpio_bank(chip);
304         unsigned long flags;
305
306         spin_lock_irqsave(&bank->lock, flags);
307         rk29_gpio_bit_op((void *__iomem) RK29_GRF_BASE, 0x78 + bank->id * 4, offset_to_bit(offset), !enable);
308         spin_unlock_irqrestore(&bank->lock, flags);
309
310         return 0;
311 }
312
313 static int rk29_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
314 {
315         return chip->base + offset;
316 }
317
318 static void rk29_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
319 {
320 #if 0
321         int i;
322
323         for (i = 0; i < chip->ngpio; i++) {
324                 unsigned pin = chip->base + i;
325                 struct gpio_chip *chip = pin_to_gpioChip(pin);
326                 u32 bit = pin_to_bit(pin);
327                 const char *gpio_label;
328                 
329                 if(!chip ||!bit)
330                         return;
331                 
332                 gpio_label = gpiochip_is_requested(chip, i);
333                 if (gpio_label) {
334                         seq_printf(s, "[%s] GPIO%s%d: ",
335                                    gpio_label, chip->label, i);
336                         
337                         if (!chip || !bit)
338                         {
339                                 seq_printf(s, "!chip || !bit\t");
340                                 return;
341                         }
342                                 
343                         GPIOSetPinDirection(chip,bit,GPIO_IN);
344                         seq_printf(s, "pin=%d,level=%d\t", pin,GPIOGetPinLevel(chip,bit));
345                         seq_printf(s, "\t");
346                 }
347         }
348 #endif
349 }
350
351 static void rk29_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
352 {
353         struct rk29_gpio_bank *bank = irq_get_handler_data(irq);
354         struct irq_chip *chip = irq_desc_get_chip(desc);
355         unsigned gpio_irq;
356         u32 isr, ilr;
357         unsigned pin;
358         unsigned unmasked = 0;
359
360         chained_irq_enter(chip, desc);
361
362         isr = __raw_readl(bank->regbase + GPIO_INT_STATUS);
363         ilr = __raw_readl(bank->regbase + GPIO_INTTYPE_LEVEL);
364
365         gpio_irq = gpio_to_irq(bank->chip.base);
366
367         while (isr) {
368                 pin = fls(isr) - 1;
369                 /* if gpio is edge triggered, clear condition
370                  * before executing the hander so that we don't
371                  * miss edges
372                  */
373                 if (ilr & (1 << pin)) {
374                         unmasked = 1;
375                         chained_irq_exit(chip, desc);
376                 }
377
378                 generic_handle_irq(gpio_irq + pin);
379                 isr &= ~(1 << pin);
380         }
381
382         if (!unmasked)
383                 chained_irq_exit(chip, desc);
384 }
385
386 static struct irq_chip rk29_gpio_irq_chip = {
387         .name           = "GPIO",
388         .irq_ack        = rk29_gpio_irq_ack,
389         .irq_disable    = rk29_gpio_irq_mask,
390         .irq_mask       = rk29_gpio_irq_mask,
391         .irq_unmask     = rk29_gpio_irq_unmask,
392         .irq_set_type   = rk29_gpio_irq_set_type,
393         .irq_set_wake   = rk29_gpio_irq_set_wake,
394 };
395
396 void __init rk29_gpio_init(void)
397 {
398         unsigned int i, j, pin;
399         struct rk29_gpio_bank *bank;
400
401         bank = rk29_gpio_banks;
402         pin = PIN_BASE;
403
404         for (i = 0; i < ARRAY_SIZE(rk29_gpio_banks); i++, bank++, pin += 32) {
405                 spin_lock_init(&bank->lock);
406                 bank->clk = clk_get(NULL, bank->chip.label);
407                 clk_enable(bank->clk);
408                 gpiochip_add(&bank->chip);
409
410                 __raw_writel(0, bank->regbase + GPIO_INTEN);
411                 for (j = 0; j < 32; j++) {
412                         unsigned int irq = gpio_to_irq(pin + j);
413                         irq_set_lockdep_class(irq, &gpio_lock_class);
414                         irq_set_chip_data(irq, bank);
415                         irq_set_chip_and_handler(irq, &rk29_gpio_irq_chip, handle_level_irq);
416                         set_irq_flags(irq, IRQF_VALID);
417                 }
418
419                 irq_set_handler_data(bank->irq, bank);
420                 irq_set_chained_handler(bank->irq, rk29_gpio_irq_handler);
421         }
422         printk("%s: %d gpio irqs in %d banks\n", __func__, pin - PIN_BASE, ARRAY_SIZE(rk29_gpio_banks));
423 }
424
425 #ifdef CONFIG_PM
426 __weak void rk29_setgpio_suspend_board(void)
427 {
428 }
429
430 __weak void rk29_setgpio_resume_board(void)
431 {
432 }
433
434 static int rk29_gpio_suspend(void)
435 {
436         unsigned i;
437         
438         rk29_setgpio_suspend_board();
439
440         for (i = 0; i < ARRAY_SIZE(rk29_gpio_banks); i++) {
441                 struct rk29_gpio_bank *bank = &rk29_gpio_banks[i];
442
443                 bank->saved_wakeup = __raw_readl(bank->regbase + GPIO_INTEN);
444                 __raw_writel(bank->suspend_wakeup, bank->regbase + GPIO_INTEN);
445
446                 if (!bank->suspend_wakeup)
447                         clk_disable(bank->clk);
448         }
449
450         return 0;
451 }
452
453 static void rk29_gpio_resume(void)
454 {
455         unsigned i;
456
457         for (i = 0; i < ARRAY_SIZE(rk29_gpio_banks); i++) {
458                 struct rk29_gpio_bank *bank = &rk29_gpio_banks[i];
459                 u32 isr;
460
461                 if (!bank->suspend_wakeup)
462                         clk_enable(bank->clk);
463
464                 /* keep enable for resume irq */
465                 isr = __raw_readl(bank->regbase + GPIO_INT_STATUS);
466                 __raw_writel(bank->saved_wakeup | (bank->suspend_wakeup & isr), bank->regbase + GPIO_INTEN);
467         }
468
469         rk29_setgpio_resume_board();
470 }
471
472 static struct syscore_ops rk29_gpio_syscore_ops = {
473         .suspend        = rk29_gpio_suspend,
474         .resume         = rk29_gpio_resume,
475 };
476
477 static int __init rk29_gpio_sysinit(void)
478 {
479         register_syscore_ops(&rk29_gpio_syscore_ops);
480         return 0;
481 }
482
483 arch_initcall(rk29_gpio_sysinit);
484 #endif