when in sleep remove the processing of gpio to board
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rk29 / gpio.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/sysdev.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
34
35 #define to_rk29_gpio_chip(c) container_of(c, struct rk29_gpio_chip, chip)
36
37 struct rk29_gpio_chip {
38         struct gpio_chip        chip;
39         unsigned short id;
40         short irq;
41         unsigned char  __iomem  *regbase;       /* Base of register bank */
42         struct clk *clk;
43         u32 suspend_wakeup;
44         u32 saved_wakeup;
45 };
46
47 static struct lock_class_key gpio_lock_class;
48
49 static void rk29_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip);
50 static void rk29_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val);
51 static int rk29_gpiolib_get(struct gpio_chip *chip, unsigned offset);
52 static int rk29_gpiolib_direction_output(struct gpio_chip *chip,unsigned offset, int val);
53 static int rk29_gpiolib_direction_input(struct gpio_chip *chip,unsigned offset);
54 static int rk29_gpiolib_PullUpDown(struct gpio_chip *chip, unsigned offset, unsigned enable);
55 static int rk29_gpiolib_to_irq(struct gpio_chip *chip,unsigned offset);
56
57 #define RK29_GPIO_CHIP(ID)                      \
58         {                                                               \
59                 .chip = {                                               \
60                         .label            = "gpio" #ID,                 \
61                         .direction_input  = rk29_gpiolib_direction_input, \
62                         .direction_output = rk29_gpiolib_direction_output, \
63                         .get              = rk29_gpiolib_get,           \
64                         .set              = rk29_gpiolib_set,           \
65                         .pull_updown      = rk29_gpiolib_PullUpDown,    \
66                         .dbg_show         = rk29_gpiolib_dbg_show,      \
67                         .to_irq           = rk29_gpiolib_to_irq,        \
68                         .base             = PIN_BASE + ID*NUM_GROUP,    \
69                         .ngpio            = NUM_GROUP,                  \
70                 },                                                      \
71                 .id = ID, \
72                 .irq = IRQ_GPIO##ID, \
73                 .regbase = (unsigned char __iomem *) RK29_GPIO##ID##_BASE, \
74         }
75
76 static struct rk29_gpio_chip rk29gpio_chip[] = {
77         RK29_GPIO_CHIP(0),
78         RK29_GPIO_CHIP(1),
79         RK29_GPIO_CHIP(2),
80         RK29_GPIO_CHIP(3),
81         RK29_GPIO_CHIP(4),
82         RK29_GPIO_CHIP(5),
83         RK29_GPIO_CHIP(6),
84 };
85
86 static inline void rk29_gpio_write(unsigned char  __iomem       *regbase, unsigned int regOff,unsigned int val)
87 {
88         __raw_writel(val,regbase + regOff);
89 }
90
91 static inline unsigned int rk29_gpio_read(unsigned char  __iomem        *regbase, unsigned int regOff)
92 {
93         return __raw_readl(regbase + regOff);
94 }
95
96 static inline void rk29_gpio_bitOp(unsigned char  __iomem       *regbase, unsigned int regOff,unsigned int mask,unsigned char opFlag)
97 {
98         unsigned int valTemp = 0;
99         
100         if(opFlag == 0)//¶Ô¼Ä´æÆ÷ÏàӦλ½øÐÐÓë0²Ù×÷
101         {
102                 valTemp = rk29_gpio_read(regbase,regOff);  
103                 valTemp &= (~mask);;
104                 rk29_gpio_write(regbase,regOff,valTemp);
105         }
106         else if(opFlag == 1)//¶Ô¼Ä´æÆ÷ÏàӦλ½øÐлò1²Ù×÷
107         {
108                 valTemp = rk29_gpio_read(regbase,regOff);
109                 valTemp |= mask;
110                 rk29_gpio_write(regbase,regOff,valTemp);
111         }
112 }
113
114 static inline  struct gpio_chip *pin_to_gpioChip(unsigned pin)
115 {
116         if(pin < PIN_BASE)
117                 return NULL;
118         
119         pin -= PIN_BASE;
120         pin /= NUM_GROUP;
121         if (likely(pin < MAX_BANK))
122                 return &(rk29gpio_chip[pin].chip);
123         return NULL;
124 }
125
126 static inline unsigned  pin_to_mask(unsigned pin)
127 {
128         if(pin < PIN_BASE)
129                 return 0;
130         pin -= PIN_BASE;
131         return 1ul << (pin % NUM_GROUP);
132 }
133
134 static inline unsigned  offset_to_mask(unsigned offset)
135 {
136         return 1ul << (offset % NUM_GROUP);
137 }
138
139 static int GPIOSetPinLevel(struct gpio_chip *chip, unsigned int mask,eGPIOPinLevel_t level)
140 {
141         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
142         unsigned char  __iomem  *gpioRegBase = rk29_gpio->regbase;
143         unsigned long flags;
144
145         if(!rk29_gpio || !gpioRegBase)
146         {
147                 return -1;
148         }
149
150         local_irq_save(flags);
151         rk29_gpio_bitOp(gpioRegBase,GPIO_SWPORT_DDR,mask,1);
152         rk29_gpio_bitOp(gpioRegBase,GPIO_SWPORT_DR,mask,level);
153         local_irq_restore(flags);
154
155         return 0;
156 }
157
158 static int GPIOGetPinLevel(struct gpio_chip *chip, unsigned int mask)
159 {
160         unsigned int valTemp;
161         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
162         unsigned char  __iomem  *gpioRegBase = rk29_gpio->regbase;
163
164         if(!rk29_gpio || !gpioRegBase)
165         {
166                 return -1;
167         }
168         
169         valTemp = rk29_gpio_read(gpioRegBase,GPIO_EXT_PORT);
170         return ((valTemp & mask) != 0);
171 }
172
173 static int GPIOSetPinDirection(struct gpio_chip *chip, unsigned int mask,eGPIOPinDirection_t direction)
174 {
175         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
176         unsigned char  __iomem  *gpioRegBase = rk29_gpio->regbase;
177         unsigned long flags;
178
179         if(!rk29_gpio || !gpioRegBase)
180         {
181                 return -1;
182         }
183
184         local_irq_save(flags);
185         rk29_gpio_bitOp(gpioRegBase,GPIO_SWPORT_DDR,mask,direction);
186         /* Enable debounce may halt cpu on wfi, disable it by default */
187         //rk29_gpio_bitOp(gpioRegBase,GPIO_DEBOUNCE,mask,1);
188         local_irq_restore(flags);
189
190         return 0;
191 }
192
193 static int GPIOEnableIntr(struct gpio_chip *chip, unsigned int mask)
194 {
195         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
196         unsigned char  __iomem  *gpioRegBase = rk29_gpio->regbase;
197
198         if(!rk29_gpio || !gpioRegBase)
199         {
200                 return -1;
201         }
202         
203         rk29_gpio_bitOp(gpioRegBase,GPIO_INTEN,mask,1);
204
205         return 0;
206 }
207
208 static int GPIODisableIntr(struct gpio_chip *chip, unsigned int mask)
209 {
210         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
211         unsigned char  __iomem  *gpioRegBase = rk29_gpio->regbase;
212
213         if(!rk29_gpio || !gpioRegBase)
214         {
215                 return -1;
216         }
217
218         rk29_gpio_bitOp(gpioRegBase,GPIO_INTEN,mask,0);
219
220         return 0;
221 }
222
223 static int GPIOSetIntrType(struct gpio_chip *chip, unsigned int mask, eGPIOIntType_t IntType)
224 {
225         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
226         unsigned char  __iomem  *gpioRegBase = rk29_gpio->regbase;
227
228         if(!rk29_gpio || !gpioRegBase)
229         {
230                 return -1;
231         }
232         
233         switch ( IntType )
234         {
235             case GPIOLevelLow:
236                         rk29_gpio_bitOp(gpioRegBase,GPIO_INT_POLARITY,mask,0);  
237                         rk29_gpio_bitOp(gpioRegBase,GPIO_INTTYPE_LEVEL,mask,0); 
238                         break;
239             case GPIOLevelHigh:
240                         rk29_gpio_bitOp(gpioRegBase,GPIO_INTTYPE_LEVEL,mask,0); 
241                         rk29_gpio_bitOp(gpioRegBase,GPIO_INT_POLARITY,mask,1);  
242                         break;
243             case GPIOEdgelFalling:
244                         rk29_gpio_bitOp(gpioRegBase,GPIO_INTTYPE_LEVEL,mask,1); 
245                         rk29_gpio_bitOp(gpioRegBase,GPIO_INT_POLARITY,mask,0);  
246                         break;
247             case GPIOEdgelRising:
248                         rk29_gpio_bitOp(gpioRegBase,GPIO_INTTYPE_LEVEL,mask,1); 
249                         rk29_gpio_bitOp(gpioRegBase,GPIO_INT_POLARITY,mask,1);  
250                         break;
251                 default:
252                         return(-1);
253         }
254          return(0);
255 }
256
257 static int gpio_irq_set_wake(unsigned int irq, unsigned int on)
258 {
259         unsigned int pin = irq_to_gpio(irq);
260         unsigned bank = (pin - PIN_BASE) / NUM_GROUP;
261         struct rk29_gpio_chip *rk29_gpio;
262         unsigned mask = pin_to_mask(pin);
263
264         if (unlikely(bank >= MAX_BANK))
265                 return -EINVAL;
266
267         rk29_gpio = &rk29gpio_chip[bank];
268         if (on)
269                 rk29_gpio->suspend_wakeup |= mask;
270         else
271                 rk29_gpio->suspend_wakeup &= ~mask;
272
273         set_irq_wake(rk29_gpio->irq, on);
274
275         return 0;
276 }
277
278 static int gpio_irq_type(unsigned irq, unsigned type)
279 {
280         unsigned int pin = irq_to_gpio(irq);
281         struct gpio_chip *chip = pin_to_gpioChip(pin);
282         unsigned        mask = pin_to_mask(pin);
283         
284         if(!chip || !mask)
285                 return -EINVAL;
286         //ÉèÖÃΪÖжÏ֮ǰ£¬±ØÐëÏÈÉèÖÃΪÊäÈë״̬
287         GPIOSetPinDirection(chip,mask,GPIO_IN);
288         
289         switch (type) {
290                 case IRQ_TYPE_NONE:
291                         break;
292                 case IRQ_TYPE_EDGE_RISING:
293                         GPIOSetIntrType(chip,mask,GPIOEdgelRising);
294                         break;
295                 case IRQ_TYPE_EDGE_FALLING:
296                         GPIOSetIntrType(chip,mask,GPIOEdgelFalling);
297                         break;
298                 case IRQ_TYPE_EDGE_BOTH:
299                         break;
300                 case IRQ_TYPE_LEVEL_HIGH:
301                         GPIOSetIntrType(chip,mask,GPIOLevelHigh);
302                         break;
303                 case IRQ_TYPE_LEVEL_LOW:
304                         GPIOSetIntrType(chip,mask,GPIOLevelLow);
305                         break;
306                 default:
307                         return -EINVAL;
308         }
309
310         if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
311                 __set_irq_handler_unlocked(irq, handle_level_irq);
312         else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
313                 __set_irq_handler_unlocked(irq, handle_edge_irq);
314
315         return 0;
316 }
317
318 static int GPIOAckIntr(struct gpio_chip *chip, unsigned int mask)
319 {
320         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
321         unsigned char  __iomem  *gpioRegBase = rk29_gpio->regbase;
322
323         if(!rk29_gpio || !gpioRegBase)
324         {
325                 return -1;
326         }
327         
328         rk29_gpio_bitOp(gpioRegBase,GPIO_PORTS_EOI,mask,1);
329         return 0;
330 }
331
332 static void gpio_irq_unmask(unsigned irq)
333 {
334         unsigned int pin = irq_to_gpio(irq);
335         struct gpio_chip *chip = pin_to_gpioChip(pin);
336         unsigned        mask = pin_to_mask(pin);
337
338         if(chip && mask)
339                 GPIOEnableIntr(chip,mask);
340 }
341
342 static void gpio_irq_mask(unsigned irq)
343 {
344         unsigned int pin = irq_to_gpio(irq);
345         struct gpio_chip *chip = pin_to_gpioChip(pin);
346         unsigned        mask = pin_to_mask(pin);
347
348         if(chip && mask)
349                 GPIODisableIntr(chip,mask);
350 }
351
352 static void gpio_ack_irq(u32 irq)
353 {
354         unsigned int pin = irq_to_gpio(irq);
355         struct gpio_chip *chip = pin_to_gpioChip(pin);
356         unsigned        mask = pin_to_mask(pin);
357
358         if(chip && mask)
359                 GPIOAckIntr(chip,mask);
360 }
361
362 static int GPIOPullUpDown(struct gpio_chip *chip, unsigned int offset, unsigned enable)
363 {
364         unsigned int temp = 0;
365         struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
366         unsigned char  __iomem *pGrfRegBase = (unsigned char  __iomem *)RK29_GRF_BASE;
367         unsigned long flags;
368
369         if(!rk29_gpio || !pGrfRegBase)
370         {
371                 return -1;
372         }
373         
374         if(offset >= 32)
375         {
376                 return -1;
377         }
378
379         local_irq_save(flags);
380         temp = __raw_readl(pGrfRegBase + 0x78 +(rk29_gpio->id)*4);
381         if(!enable)
382                 temp |= 1<<offset;
383         else
384                 temp &= ~(1<<offset);
385
386         __raw_writel(temp,pGrfRegBase + 0x78 +(rk29_gpio->id)*4);
387         local_irq_restore(flags);
388
389         return 0;
390 }
391
392
393 static int rk29_gpiolib_direction_output(struct gpio_chip *chip,unsigned offset, int val)
394 {
395         unsigned        mask = offset_to_mask(offset);
396         
397         if(GPIOSetPinDirection(chip,mask,GPIO_OUT) == 0)
398         {
399                 return GPIOSetPinLevel(chip,mask,val);
400         }
401         else
402         {
403                 return -1;
404         }
405 }
406
407 static int rk29_gpiolib_direction_input(struct gpio_chip *chip,unsigned offset)
408 {
409         unsigned        mask = offset_to_mask(offset);
410         
411         return GPIOSetPinDirection(chip,mask,GPIO_IN);
412 }
413
414
415 static int rk29_gpiolib_get(struct gpio_chip *chip, unsigned offset)
416 {
417         unsigned        mask = offset_to_mask(offset);
418         
419         return GPIOGetPinLevel(chip,mask);
420 }
421
422 static void rk29_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
423 {
424         unsigned        mask = offset_to_mask(offset);
425         
426         GPIOSetPinLevel(chip,mask,val);
427 }
428
429 static int rk29_gpiolib_PullUpDown(struct gpio_chip *chip, unsigned offset, unsigned enable)
430 {
431         return GPIOPullUpDown(chip, offset, enable);
432 }
433
434 static int rk29_gpiolib_to_irq(struct gpio_chip *chip,
435                                                 unsigned offset)
436 {
437     struct rk29_gpio_chip *rk29_gpio = to_rk29_gpio_chip(chip);
438
439     if(!rk29_gpio)
440     {
441          return -1;
442     }
443
444     return offset + NR_IRQS;
445 }
446
447 static void rk29_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
448 {
449
450         int i;
451
452         for (i = 0; i < chip->ngpio; i++) {
453                 unsigned pin = chip->base + i;
454                 struct gpio_chip *chip = pin_to_gpioChip(pin);
455                 unsigned mask = pin_to_mask(pin);
456                 const char *gpio_label;
457                 
458                 if(!chip ||!mask)
459                         return;
460                 
461                 gpio_label = gpiochip_is_requested(chip, i);
462                 if (gpio_label) {
463                         seq_printf(s, "[%s] GPIO%s%d: ",
464                                    gpio_label, chip->label, i);
465                         
466                         if (!chip || !mask)
467                         {
468                                 seq_printf(s, "!chip || !mask\t");
469                                 return;
470                         }
471                                 
472                         GPIOSetPinDirection(chip,mask,GPIO_IN);
473                         seq_printf(s, "pin=%d,level=%d\t", pin,GPIOGetPinLevel(chip,mask));
474                         seq_printf(s, "\t");
475                 }
476         }
477 }
478
479 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
480 {
481         unsigned gpio_irq;
482         struct rk29_gpio_chip *rk29_gpio;
483         u32 isr;
484
485         rk29_gpio = get_irq_chip_data(irq+13);
486
487         // temporarily mask parent IRQ
488         desc->chip->mask(irq);
489         if(desc->chip->ack)
490                 desc->chip->ack(irq);
491
492         isr = rk29_gpio_read(rk29_gpio->regbase, GPIO_INT_STATUS);
493
494         gpio_irq = gpio_to_irq(rk29_gpio->chip.base);
495
496         while (isr) {
497                 int irqoffset = fls(isr) - 1;
498                 generic_handle_irq(gpio_irq + irqoffset);
499                 isr &= ~(1 << irqoffset);
500         }
501
502         desc->chip->unmask(irq);
503         /* now it may re-trigger */
504 }
505
506 static struct irq_chip rk29gpio_irqchip = {
507         .name           = "GPIO",
508         .ack            = gpio_ack_irq,
509         .disable        = gpio_irq_mask,
510         .mask           = gpio_irq_mask,
511         .unmask         = gpio_irq_unmask,
512         .set_type       = gpio_irq_type,
513         .set_wake       = gpio_irq_set_wake,
514 };
515
516 static void __init rk29_gpio_irq_setup(void)
517 {
518         unsigned int i, j, pin;
519         struct rk29_gpio_chip *this;
520
521         this = rk29gpio_chip;
522         pin = NR_AIC_IRQS;
523         for (i = 0; i < MAX_BANK; i++) {
524                 rk29_gpio_write(this->regbase,GPIO_INTEN,0);
525                 for (j = 0; j < 32; j++) {
526                         lockdep_set_class(&irq_desc[pin+j].lock, &gpio_lock_class);
527                         set_irq_chip(pin+j, &rk29gpio_irqchip);
528                         set_irq_handler(pin+j, handle_level_irq);
529                         set_irq_flags(pin+j, IRQF_VALID);
530                 }
531
532                 set_irq_chip_data(NR_AIC_IRQS + this->id, this);
533                 set_irq_chained_handler(this->irq, gpio_irq_handler);
534                 this += 1; 
535                 pin += 32;
536         }
537         printk("rk29_gpio_irq_setup: %d gpio irqs in 7 banks\n", pin - PIN_BASE);
538 }
539
540 void __init rk29_gpio_init(void)
541 {
542         unsigned i;
543         struct rk29_gpio_chip *rk29_gpio;
544
545         for (i = 0; i < MAX_BANK; i++) {
546                 rk29_gpio = &rk29gpio_chip[i];
547                 rk29_gpio->clk = clk_get(NULL, rk29_gpio->chip.label);
548                 clk_enable(rk29_gpio->clk);
549                 gpiochip_add(&rk29_gpio->chip);
550         }
551         rk29_gpio_irq_setup();
552 }
553
554 __weak void rk29_setgpio_suspend_board(void);
555
556 __weak void rk29_setgpio_resume_board(void);
557
558 #ifdef CONFIG_PM
559 static int rk29_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
560 {
561         unsigned i;
562         
563         rk29_setgpio_suspend_board();
564         
565         for (i = 0; i < MAX_BANK; i++) {
566                 struct rk29_gpio_chip *rk29_gpio = &rk29gpio_chip[i];
567
568                 rk29_gpio->saved_wakeup = rk29_gpio_read(rk29_gpio->regbase, GPIO_INTEN);
569                 rk29_gpio_write(rk29_gpio->regbase, GPIO_INTEN, rk29_gpio->suspend_wakeup);
570
571                 if (!rk29_gpio->suspend_wakeup)
572                         clk_disable(rk29_gpio->clk);
573         }
574
575         return 0;
576 }
577
578 static int rk29_gpio_resume(struct sys_device *dev)
579 {
580         unsigned i;
581         for (i = 0; i < MAX_BANK; i++) {
582                 struct rk29_gpio_chip *rk29_gpio = &rk29gpio_chip[i];
583
584                 if (!rk29_gpio->suspend_wakeup)
585                         clk_enable(rk29_gpio->clk);
586
587                 /* keep enable for resume irq */
588                 rk29_gpio_write(rk29_gpio->regbase, GPIO_INTEN, rk29_gpio->saved_wakeup | (rk29_gpio->suspend_wakeup & rk29_gpio_read(rk29_gpio->regbase, GPIO_INT_STATUS)));
589         }
590         rk29_setgpio_resume_board();
591         return 0;
592 }
593
594 static struct sysdev_class rk29_gpio_sysclass = {
595         .name           = "gpio",
596         .suspend        = rk29_gpio_suspend,
597         .resume         = rk29_gpio_resume,
598 };
599
600 static struct sys_device rk29_gpio_device = {
601         .cls            = &rk29_gpio_sysclass,
602 };
603
604 static int __init rk29_gpio_sysinit(void)
605 {
606         int ret = sysdev_class_register(&rk29_gpio_sysclass);
607         if (ret == 0)
608                 ret = sysdev_register(&rk29_gpio_device);
609         return ret;
610 }
611
612 arch_initcall(rk29_gpio_sysinit);
613 #endif