gpio: rk30: simplify rk30_gpiolib_request
[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
307 static int rk30_gpiolib_request(struct gpio_chip *chip, unsigned offset)
308 {
309         iomux_set_gpio_mode(chip->base + offset);
310         return 0;
311 }
312
313 static int rk30_gpiolib_get(struct gpio_chip *chip, unsigned offset)
314 {
315         return GPIOGetPinLevel(to_rk30_gpio_bank(chip)->regbase, offset_to_bit(offset));
316 }
317
318 static void rk30_gpiolib_set(struct gpio_chip *chip, unsigned offset, int val)
319 {
320         struct rk30_gpio_bank *bank = to_rk30_gpio_bank(chip);
321         unsigned long flags;
322
323         spin_lock_irqsave(&bank->lock, flags);
324         GPIOSetPinLevel(bank->regbase, offset_to_bit(offset), val);
325         spin_unlock_irqrestore(&bank->lock, flags);
326 }
327
328 static int rk30_gpiolib_pull_updown(struct gpio_chip *chip, unsigned offset, unsigned enable)
329 {
330 #if !defined(CONFIG_ARCH_RK3066B)
331         struct rk30_gpio_bank *bank = to_rk30_gpio_bank(chip);
332         unsigned long flags;
333
334         spin_lock_irqsave(&bank->lock, flags);
335         if(offset>=16)  
336                 rk30_gpio_bit_op((void *__iomem) RK30_GRF_BASE, GRF_GPIO0H_PULL + bank->id * 8, (1<<offset) | offset_to_bit(offset-16), !enable);
337         else    
338                 rk30_gpio_bit_op((void *__iomem) RK30_GRF_BASE, GRF_GPIO0L_PULL + bank->id * 8, (1<<(offset+16)) | offset_to_bit(offset), !enable);
339         spin_unlock_irqrestore(&bank->lock, flags);
340 #endif
341         return 0;
342 }
343
344 static int rk30_gpiolib_to_irq(struct gpio_chip *chip, unsigned offset)
345 {
346         return chip->base + offset;
347 }
348
349 static void rk30_gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
350 {
351 #if 0
352         int i;
353
354         for (i = 0; i < chip->ngpio; i++) {
355                 unsigned pin = chip->base + i;
356                 struct gpio_chip *chip = pin_to_gpioChip(pin);
357                 u32 bit = pin_to_bit(pin);
358                 const char *gpio_label;
359                 
360                 if(!chip ||!bit)
361                         return;
362                 
363                 gpio_label = gpiochip_is_requested(chip, i);
364                 if (gpio_label) {
365                         seq_printf(s, "[%s] GPIO%s%d: ",
366                                    gpio_label, chip->label, i);
367                         
368                         if (!chip || !bit)
369                         {
370                                 seq_printf(s, "!chip || !bit\t");
371                                 return;
372                         }
373                                 
374                         GPIOSetPinDirection(chip,bit,GPIO_IN);
375                         seq_printf(s, "pin=%d,level=%d\t", pin,GPIOGetPinLevel(chip,bit));
376                         seq_printf(s, "\t");
377                 }
378         }
379 #endif
380 }
381
382 static void rk30_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
383 {
384         struct rk30_gpio_bank *bank = irq_get_handler_data(irq);
385         struct irq_chip *chip = irq_desc_get_chip(desc);
386         unsigned gpio_irq;
387         u32 isr, ilr;
388         unsigned pin;
389         unsigned unmasked = 0;
390
391         chained_irq_enter(chip, desc);
392
393         isr = __raw_readl(bank->regbase + GPIO_INT_STATUS);
394         ilr = __raw_readl(bank->regbase + GPIO_INTTYPE_LEVEL);
395
396         gpio_irq = gpio_to_irq(bank->chip.base);
397
398         while (isr) {
399                 pin = fls(isr) - 1;
400                 /* if gpio is edge triggered, clear condition
401                  * before executing the hander so that we don't
402                  * miss edges
403                  */
404                 if (ilr & (1 << pin)) {
405                         unmasked = 1;
406                         chained_irq_exit(chip, desc);
407                 }
408
409                 generic_handle_irq(gpio_irq + pin);
410                 isr &= ~(1 << pin);
411         }
412
413         if (!unmasked)
414                 chained_irq_exit(chip, desc);
415 }
416
417 static struct irq_chip rk30_gpio_irq_chip = {
418         .name           = "GPIO",
419         .irq_ack        = rk30_gpio_irq_ack,
420         .irq_disable    = rk30_gpio_irq_mask,
421         .irq_mask       = rk30_gpio_irq_mask,
422         .irq_unmask     = rk30_gpio_irq_unmask,
423         .irq_set_type   = rk30_gpio_irq_set_type,
424         .irq_set_wake   = rk30_gpio_irq_set_wake,
425 };
426
427 void __init rk30_gpio_init(void)
428 {
429         unsigned int i, j, pin;
430         struct rk30_gpio_bank *bank;
431
432         bank = rk30_gpio_banks;
433         pin = PIN_BASE;
434
435         for (i = 0; i < ARRAY_SIZE(rk30_gpio_banks); i++, bank++) {
436                 spin_lock_init(&bank->lock);
437                 bank->clk = clk_get(NULL, bank->chip.label);
438                 clk_enable(bank->clk);
439                 gpiochip_add(&bank->chip);
440
441                 __raw_writel(0, bank->regbase + GPIO_INTEN);
442                 for (j = 0; j < 32; j++) {
443                         unsigned int irq = gpio_to_irq(pin);
444                         if (pin > MAX_PIN)
445                                 break;
446                         irq_set_lockdep_class(irq, &gpio_lock_class);
447                         irq_set_chip_data(irq, bank);
448                         irq_set_chip_and_handler(irq, &rk30_gpio_irq_chip, handle_level_irq);
449                         set_irq_flags(irq, IRQF_VALID);
450                         pin++;
451                 }
452
453                 irq_set_handler_data(bank->irq, bank);
454                 irq_set_chained_handler(bank->irq, rk30_gpio_irq_handler);
455         }
456         printk("%s: %d gpio irqs in %d banks\n", __func__, pin - PIN_BASE, ARRAY_SIZE(rk30_gpio_banks));
457 }
458
459 #ifdef CONFIG_PM
460 __weak void rk30_setgpio_suspend_board(void)
461 {
462 }
463
464 __weak void rk30_setgpio_resume_board(void)
465 {
466 }
467
468 static int rk30_gpio_suspend(void)
469 {
470         unsigned i;
471         
472         rk30_setgpio_suspend_board();
473
474         for (i = 0; i < ARRAY_SIZE(rk30_gpio_banks); i++) {
475                 struct rk30_gpio_bank *bank = &rk30_gpio_banks[i];
476
477                 bank->saved_wakeup = __raw_readl(bank->regbase + GPIO_INTEN);
478                 __raw_writel(bank->suspend_wakeup, bank->regbase + GPIO_INTEN);
479
480                 if (!bank->suspend_wakeup)
481                         clk_disable(bank->clk);
482         }
483
484         return 0;
485 }
486
487 static void rk30_gpio_resume(void)
488 {
489         unsigned i;
490
491         for (i = 0; i < ARRAY_SIZE(rk30_gpio_banks); i++) {
492                 struct rk30_gpio_bank *bank = &rk30_gpio_banks[i];
493                 u32 isr;
494
495                 if (!bank->suspend_wakeup)
496                         clk_enable(bank->clk);
497
498                 /* keep enable for resume irq */
499                 isr = __raw_readl(bank->regbase + GPIO_INT_STATUS);
500                 __raw_writel(bank->saved_wakeup | (bank->suspend_wakeup & isr), bank->regbase + GPIO_INTEN);
501         }
502
503         rk30_setgpio_resume_board();
504 }
505
506 static struct syscore_ops rk30_gpio_syscore_ops = {
507         .suspend        = rk30_gpio_suspend,
508         .resume         = rk30_gpio_resume,
509 };
510
511 static int __init rk30_gpio_sysinit(void)
512 {
513         register_syscore_ops(&rk30_gpio_syscore_ops);
514         return 0;
515 }
516
517 arch_initcall(rk30_gpio_sysinit);
518 #endif