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