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