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