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