Merge tag 'gpio-updates-for-v6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / drivers / gpio / gpio-rockchip.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2013 MundoReader S.L.
4  * Author: Heiko Stuebner <heiko@sntech.de>
5  *
6  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
7  */
8
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/regmap.h>
24
25 #include "../pinctrl/core.h"
26 #include "../pinctrl/pinctrl-rockchip.h"
27
28 #define GPIO_TYPE_V1            (0)           /* GPIO Version ID reserved */
29 #define GPIO_TYPE_V2            (0x01000C2B)  /* GPIO Version ID 0x01000C2B */
30 #define GPIO_TYPE_V2_1          (0x0101157C)  /* GPIO Version ID 0x0101157C */
31
32 static const struct rockchip_gpio_regs gpio_regs_v1 = {
33         .port_dr = 0x00,
34         .port_ddr = 0x04,
35         .int_en = 0x30,
36         .int_mask = 0x34,
37         .int_type = 0x38,
38         .int_polarity = 0x3c,
39         .int_status = 0x40,
40         .int_rawstatus = 0x44,
41         .debounce = 0x48,
42         .port_eoi = 0x4c,
43         .ext_port = 0x50,
44 };
45
46 static const struct rockchip_gpio_regs gpio_regs_v2 = {
47         .port_dr = 0x00,
48         .port_ddr = 0x08,
49         .int_en = 0x10,
50         .int_mask = 0x18,
51         .int_type = 0x20,
52         .int_polarity = 0x28,
53         .int_bothedge = 0x30,
54         .int_status = 0x50,
55         .int_rawstatus = 0x58,
56         .debounce = 0x38,
57         .dbclk_div_en = 0x40,
58         .dbclk_div_con = 0x48,
59         .port_eoi = 0x60,
60         .ext_port = 0x70,
61         .version_id = 0x78,
62 };
63
64 static inline void gpio_writel_v2(u32 val, void __iomem *reg)
65 {
66         writel((val & 0xffff) | 0xffff0000, reg);
67         writel((val >> 16) | 0xffff0000, reg + 0x4);
68 }
69
70 static inline u32 gpio_readl_v2(void __iomem *reg)
71 {
72         return readl(reg + 0x4) << 16 | readl(reg);
73 }
74
75 static inline void rockchip_gpio_writel(struct rockchip_pin_bank *bank,
76                                         u32 value, unsigned int offset)
77 {
78         void __iomem *reg = bank->reg_base + offset;
79
80         if (bank->gpio_type == GPIO_TYPE_V2)
81                 gpio_writel_v2(value, reg);
82         else
83                 writel(value, reg);
84 }
85
86 static inline u32 rockchip_gpio_readl(struct rockchip_pin_bank *bank,
87                                       unsigned int offset)
88 {
89         void __iomem *reg = bank->reg_base + offset;
90         u32 value;
91
92         if (bank->gpio_type == GPIO_TYPE_V2)
93                 value = gpio_readl_v2(reg);
94         else
95                 value = readl(reg);
96
97         return value;
98 }
99
100 static inline void rockchip_gpio_writel_bit(struct rockchip_pin_bank *bank,
101                                             u32 bit, u32 value,
102                                             unsigned int offset)
103 {
104         void __iomem *reg = bank->reg_base + offset;
105         u32 data;
106
107         if (bank->gpio_type == GPIO_TYPE_V2) {
108                 if (value)
109                         data = BIT(bit % 16) | BIT(bit % 16 + 16);
110                 else
111                         data = BIT(bit % 16 + 16);
112                 writel(data, bit >= 16 ? reg + 0x4 : reg);
113         } else {
114                 data = readl(reg);
115                 data &= ~BIT(bit);
116                 if (value)
117                         data |= BIT(bit);
118                 writel(data, reg);
119         }
120 }
121
122 static inline u32 rockchip_gpio_readl_bit(struct rockchip_pin_bank *bank,
123                                           u32 bit, unsigned int offset)
124 {
125         void __iomem *reg = bank->reg_base + offset;
126         u32 data;
127
128         if (bank->gpio_type == GPIO_TYPE_V2) {
129                 data = readl(bit >= 16 ? reg + 0x4 : reg);
130                 data >>= bit % 16;
131         } else {
132                 data = readl(reg);
133                 data >>= bit;
134         }
135
136         return data & (0x1);
137 }
138
139 static int rockchip_gpio_get_direction(struct gpio_chip *chip,
140                                        unsigned int offset)
141 {
142         struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
143         u32 data;
144
145         data = rockchip_gpio_readl_bit(bank, offset, bank->gpio_regs->port_ddr);
146         if (data)
147                 return GPIO_LINE_DIRECTION_OUT;
148
149         return GPIO_LINE_DIRECTION_IN;
150 }
151
152 static int rockchip_gpio_set_direction(struct gpio_chip *chip,
153                                        unsigned int offset, bool input)
154 {
155         struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
156         unsigned long flags;
157         u32 data = input ? 0 : 1;
158
159         raw_spin_lock_irqsave(&bank->slock, flags);
160         rockchip_gpio_writel_bit(bank, offset, data, bank->gpio_regs->port_ddr);
161         raw_spin_unlock_irqrestore(&bank->slock, flags);
162
163         return 0;
164 }
165
166 static void rockchip_gpio_set(struct gpio_chip *gc, unsigned int offset,
167                               int value)
168 {
169         struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
170         unsigned long flags;
171
172         raw_spin_lock_irqsave(&bank->slock, flags);
173         rockchip_gpio_writel_bit(bank, offset, value, bank->gpio_regs->port_dr);
174         raw_spin_unlock_irqrestore(&bank->slock, flags);
175 }
176
177 static int rockchip_gpio_get(struct gpio_chip *gc, unsigned int offset)
178 {
179         struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
180         u32 data;
181
182         data = readl(bank->reg_base + bank->gpio_regs->ext_port);
183         data >>= offset;
184         data &= 1;
185
186         return data;
187 }
188
189 static int rockchip_gpio_set_debounce(struct gpio_chip *gc,
190                                       unsigned int offset,
191                                       unsigned int debounce)
192 {
193         struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
194         const struct rockchip_gpio_regs *reg = bank->gpio_regs;
195         unsigned long flags, div_reg, freq, max_debounce;
196         bool div_debounce_support;
197         unsigned int cur_div_reg;
198         u64 div;
199
200         if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) {
201                 div_debounce_support = true;
202                 freq = clk_get_rate(bank->db_clk);
203                 max_debounce = (GENMASK(23, 0) + 1) * 2 * 1000000 / freq;
204                 if (debounce > max_debounce)
205                         return -EINVAL;
206
207                 div = debounce * freq;
208                 div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1;
209         } else {
210                 div_debounce_support = false;
211         }
212
213         raw_spin_lock_irqsave(&bank->slock, flags);
214
215         /* Only the v1 needs to configure div_en and div_con for dbclk */
216         if (debounce) {
217                 if (div_debounce_support) {
218                         /* Configure the max debounce from consumers */
219                         cur_div_reg = readl(bank->reg_base +
220                                             reg->dbclk_div_con);
221                         if (cur_div_reg < div_reg)
222                                 writel(div_reg, bank->reg_base +
223                                        reg->dbclk_div_con);
224                         rockchip_gpio_writel_bit(bank, offset, 1,
225                                                  reg->dbclk_div_en);
226                 }
227
228                 rockchip_gpio_writel_bit(bank, offset, 1, reg->debounce);
229         } else {
230                 if (div_debounce_support)
231                         rockchip_gpio_writel_bit(bank, offset, 0,
232                                                  reg->dbclk_div_en);
233
234                 rockchip_gpio_writel_bit(bank, offset, 0, reg->debounce);
235         }
236
237         raw_spin_unlock_irqrestore(&bank->slock, flags);
238
239         /* Enable or disable dbclk at last */
240         if (div_debounce_support) {
241                 if (debounce)
242                         clk_prepare_enable(bank->db_clk);
243                 else
244                         clk_disable_unprepare(bank->db_clk);
245         }
246
247         return 0;
248 }
249
250 static int rockchip_gpio_direction_input(struct gpio_chip *gc,
251                                          unsigned int offset)
252 {
253         return rockchip_gpio_set_direction(gc, offset, true);
254 }
255
256 static int rockchip_gpio_direction_output(struct gpio_chip *gc,
257                                           unsigned int offset, int value)
258 {
259         rockchip_gpio_set(gc, offset, value);
260
261         return rockchip_gpio_set_direction(gc, offset, false);
262 }
263
264 /*
265  * gpiolib set_config callback function. The setting of the pin
266  * mux function as 'gpio output' will be handled by the pinctrl subsystem
267  * interface.
268  */
269 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
270                                   unsigned long config)
271 {
272         enum pin_config_param param = pinconf_to_config_param(config);
273
274         switch (param) {
275         case PIN_CONFIG_INPUT_DEBOUNCE:
276                 rockchip_gpio_set_debounce(gc, offset, true);
277                 /*
278                  * Rockchip's gpio could only support up to one period
279                  * of the debounce clock(pclk), which is far away from
280                  * satisftying the requirement, as pclk is usually near
281                  * 100MHz shared by all peripherals. So the fact is it
282                  * has crippled debounce capability could only be useful
283                  * to prevent any spurious glitches from waking up the system
284                  * if the gpio is conguired as wakeup interrupt source. Let's
285                  * still return -ENOTSUPP as before, to make sure the caller
286                  * of gpiod_set_debounce won't change its behaviour.
287                  */
288                 return -ENOTSUPP;
289         default:
290                 return -ENOTSUPP;
291         }
292 }
293
294 /*
295  * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
296  * and a virtual IRQ, if not already present.
297  */
298 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
299 {
300         struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
301         unsigned int virq;
302
303         if (!bank->domain)
304                 return -ENXIO;
305
306         virq = irq_create_mapping(bank->domain, offset);
307
308         return (virq) ? : -ENXIO;
309 }
310
311 static const struct gpio_chip rockchip_gpiolib_chip = {
312         .request = gpiochip_generic_request,
313         .free = gpiochip_generic_free,
314         .set = rockchip_gpio_set,
315         .get = rockchip_gpio_get,
316         .get_direction  = rockchip_gpio_get_direction,
317         .direction_input = rockchip_gpio_direction_input,
318         .direction_output = rockchip_gpio_direction_output,
319         .set_config = rockchip_gpio_set_config,
320         .to_irq = rockchip_gpio_to_irq,
321         .owner = THIS_MODULE,
322 };
323
324 static void rockchip_irq_demux(struct irq_desc *desc)
325 {
326         struct irq_chip *chip = irq_desc_get_chip(desc);
327         struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
328         unsigned long pending;
329         unsigned int irq;
330
331         dev_dbg(bank->dev, "got irq for bank %s\n", bank->name);
332
333         chained_irq_enter(chip, desc);
334
335         pending = readl_relaxed(bank->reg_base + bank->gpio_regs->int_status);
336         for_each_set_bit(irq, &pending, 32) {
337                 dev_dbg(bank->dev, "handling irq %d\n", irq);
338
339                 /*
340                  * Triggering IRQ on both rising and falling edge
341                  * needs manual intervention.
342                  */
343                 if (bank->toggle_edge_mode & BIT(irq)) {
344                         u32 data, data_old, polarity;
345                         unsigned long flags;
346
347                         data = readl_relaxed(bank->reg_base +
348                                              bank->gpio_regs->ext_port);
349                         do {
350                                 raw_spin_lock_irqsave(&bank->slock, flags);
351
352                                 polarity = readl_relaxed(bank->reg_base +
353                                                          bank->gpio_regs->int_polarity);
354                                 if (data & BIT(irq))
355                                         polarity &= ~BIT(irq);
356                                 else
357                                         polarity |= BIT(irq);
358                                 writel(polarity,
359                                        bank->reg_base +
360                                        bank->gpio_regs->int_polarity);
361
362                                 raw_spin_unlock_irqrestore(&bank->slock, flags);
363
364                                 data_old = data;
365                                 data = readl_relaxed(bank->reg_base +
366                                                      bank->gpio_regs->ext_port);
367                         } while ((data & BIT(irq)) != (data_old & BIT(irq)));
368                 }
369
370                 generic_handle_domain_irq(bank->domain, irq);
371         }
372
373         chained_irq_exit(chip, desc);
374 }
375
376 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
377 {
378         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
379         struct rockchip_pin_bank *bank = gc->private;
380         u32 mask = BIT(d->hwirq);
381         u32 polarity;
382         u32 level;
383         u32 data;
384         unsigned long flags;
385         int ret = 0;
386
387         raw_spin_lock_irqsave(&bank->slock, flags);
388
389         rockchip_gpio_writel_bit(bank, d->hwirq, 0,
390                                  bank->gpio_regs->port_ddr);
391
392         raw_spin_unlock_irqrestore(&bank->slock, flags);
393
394         if (type & IRQ_TYPE_EDGE_BOTH)
395                 irq_set_handler_locked(d, handle_edge_irq);
396         else
397                 irq_set_handler_locked(d, handle_level_irq);
398
399         raw_spin_lock_irqsave(&bank->slock, flags);
400
401         level = rockchip_gpio_readl(bank, bank->gpio_regs->int_type);
402         polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity);
403
404         if (type == IRQ_TYPE_EDGE_BOTH) {
405                 if (bank->gpio_type == GPIO_TYPE_V2) {
406                         rockchip_gpio_writel_bit(bank, d->hwirq, 1,
407                                                  bank->gpio_regs->int_bothedge);
408                         goto out;
409                 } else {
410                         bank->toggle_edge_mode |= mask;
411                         level &= ~mask;
412
413                         /*
414                          * Determine gpio state. If 1 next interrupt should be
415                          * low otherwise high.
416                          */
417                         data = readl(bank->reg_base + bank->gpio_regs->ext_port);
418                         if (data & mask)
419                                 polarity &= ~mask;
420                         else
421                                 polarity |= mask;
422                 }
423         } else {
424                 if (bank->gpio_type == GPIO_TYPE_V2) {
425                         rockchip_gpio_writel_bit(bank, d->hwirq, 0,
426                                                  bank->gpio_regs->int_bothedge);
427                 } else {
428                         bank->toggle_edge_mode &= ~mask;
429                 }
430                 switch (type) {
431                 case IRQ_TYPE_EDGE_RISING:
432                         level |= mask;
433                         polarity |= mask;
434                         break;
435                 case IRQ_TYPE_EDGE_FALLING:
436                         level |= mask;
437                         polarity &= ~mask;
438                         break;
439                 case IRQ_TYPE_LEVEL_HIGH:
440                         level &= ~mask;
441                         polarity |= mask;
442                         break;
443                 case IRQ_TYPE_LEVEL_LOW:
444                         level &= ~mask;
445                         polarity &= ~mask;
446                         break;
447                 default:
448                         ret = -EINVAL;
449                         goto out;
450                 }
451         }
452
453         rockchip_gpio_writel(bank, level, bank->gpio_regs->int_type);
454         rockchip_gpio_writel(bank, polarity, bank->gpio_regs->int_polarity);
455 out:
456         raw_spin_unlock_irqrestore(&bank->slock, flags);
457
458         return ret;
459 }
460
461 static int rockchip_irq_reqres(struct irq_data *d)
462 {
463         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
464         struct rockchip_pin_bank *bank = gc->private;
465
466         return gpiochip_reqres_irq(&bank->gpio_chip, d->hwirq);
467 }
468
469 static void rockchip_irq_relres(struct irq_data *d)
470 {
471         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
472         struct rockchip_pin_bank *bank = gc->private;
473
474         gpiochip_relres_irq(&bank->gpio_chip, d->hwirq);
475 }
476
477 static void rockchip_irq_suspend(struct irq_data *d)
478 {
479         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
480         struct rockchip_pin_bank *bank = gc->private;
481
482         bank->saved_masks = irq_reg_readl(gc, bank->gpio_regs->int_mask);
483         irq_reg_writel(gc, ~gc->wake_active, bank->gpio_regs->int_mask);
484 }
485
486 static void rockchip_irq_resume(struct irq_data *d)
487 {
488         struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
489         struct rockchip_pin_bank *bank = gc->private;
490
491         irq_reg_writel(gc, bank->saved_masks, bank->gpio_regs->int_mask);
492 }
493
494 static void rockchip_irq_enable(struct irq_data *d)
495 {
496         irq_gc_mask_clr_bit(d);
497 }
498
499 static void rockchip_irq_disable(struct irq_data *d)
500 {
501         irq_gc_mask_set_bit(d);
502 }
503
504 static int rockchip_interrupts_register(struct rockchip_pin_bank *bank)
505 {
506         unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
507         struct irq_chip_generic *gc;
508         int ret;
509
510         bank->domain = irq_domain_add_linear(bank->of_node, 32,
511                                         &irq_generic_chip_ops, NULL);
512         if (!bank->domain) {
513                 dev_warn(bank->dev, "could not init irq domain for bank %s\n",
514                          bank->name);
515                 return -EINVAL;
516         }
517
518         ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
519                                              "rockchip_gpio_irq",
520                                              handle_level_irq,
521                                              clr, 0, 0);
522         if (ret) {
523                 dev_err(bank->dev, "could not alloc generic chips for bank %s\n",
524                         bank->name);
525                 irq_domain_remove(bank->domain);
526                 return -EINVAL;
527         }
528
529         gc = irq_get_domain_generic_chip(bank->domain, 0);
530         if (bank->gpio_type == GPIO_TYPE_V2) {
531                 gc->reg_writel = gpio_writel_v2;
532                 gc->reg_readl = gpio_readl_v2;
533         }
534
535         gc->reg_base = bank->reg_base;
536         gc->private = bank;
537         gc->chip_types[0].regs.mask = bank->gpio_regs->int_mask;
538         gc->chip_types[0].regs.ack = bank->gpio_regs->port_eoi;
539         gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
540         gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
541         gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
542         gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
543         gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
544         gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
545         gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
546         gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
547         gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
548         gc->chip_types[0].chip.irq_request_resources = rockchip_irq_reqres;
549         gc->chip_types[0].chip.irq_release_resources = rockchip_irq_relres;
550         gc->wake_enabled = IRQ_MSK(bank->nr_pins);
551
552         /*
553          * Linux assumes that all interrupts start out disabled/masked.
554          * Our driver only uses the concept of masked and always keeps
555          * things enabled, so for us that's all masked and all enabled.
556          */
557         rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_mask);
558         rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->port_eoi);
559         rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_en);
560         gc->mask_cache = 0xffffffff;
561
562         irq_set_chained_handler_and_data(bank->irq,
563                                          rockchip_irq_demux, bank);
564
565         return 0;
566 }
567
568 static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank)
569 {
570         struct gpio_chip *gc;
571         int ret;
572
573         bank->gpio_chip = rockchip_gpiolib_chip;
574
575         gc = &bank->gpio_chip;
576         gc->base = bank->pin_base;
577         gc->ngpio = bank->nr_pins;
578         gc->label = bank->name;
579         gc->parent = bank->dev;
580
581         ret = gpiochip_add_data(gc, bank);
582         if (ret) {
583                 dev_err(bank->dev, "failed to add gpiochip %s, %d\n",
584                         gc->label, ret);
585                 return ret;
586         }
587
588         /*
589          * For DeviceTree-supported systems, the gpio core checks the
590          * pinctrl's device node for the "gpio-ranges" property.
591          * If it is present, it takes care of adding the pin ranges
592          * for the driver. In this case the driver can skip ahead.
593          *
594          * In order to remain compatible with older, existing DeviceTree
595          * files which don't set the "gpio-ranges" property or systems that
596          * utilize ACPI the driver has to call gpiochip_add_pin_range().
597          */
598         if (!of_property_read_bool(bank->of_node, "gpio-ranges")) {
599                 struct device_node *pctlnp = of_get_parent(bank->of_node);
600                 struct pinctrl_dev *pctldev = NULL;
601
602                 if (!pctlnp)
603                         return -ENODATA;
604
605                 pctldev = of_pinctrl_get(pctlnp);
606                 if (!pctldev)
607                         return -ENODEV;
608
609                 ret = gpiochip_add_pin_range(gc, dev_name(pctldev->dev), 0,
610                                              gc->base, gc->ngpio);
611                 if (ret) {
612                         dev_err(bank->dev, "Failed to add pin range\n");
613                         goto fail;
614                 }
615         }
616
617         ret = rockchip_interrupts_register(bank);
618         if (ret) {
619                 dev_err(bank->dev, "failed to register interrupt, %d\n", ret);
620                 goto fail;
621         }
622
623         return 0;
624
625 fail:
626         gpiochip_remove(&bank->gpio_chip);
627
628         return ret;
629 }
630
631 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
632 {
633         struct resource res;
634         int id = 0;
635
636         if (of_address_to_resource(bank->of_node, 0, &res)) {
637                 dev_err(bank->dev, "cannot find IO resource for bank\n");
638                 return -ENOENT;
639         }
640
641         bank->reg_base = devm_ioremap_resource(bank->dev, &res);
642         if (IS_ERR(bank->reg_base))
643                 return PTR_ERR(bank->reg_base);
644
645         bank->irq = irq_of_parse_and_map(bank->of_node, 0);
646         if (!bank->irq)
647                 return -EINVAL;
648
649         bank->clk = of_clk_get(bank->of_node, 0);
650         if (IS_ERR(bank->clk))
651                 return PTR_ERR(bank->clk);
652
653         clk_prepare_enable(bank->clk);
654         id = readl(bank->reg_base + gpio_regs_v2.version_id);
655
656         /* If not gpio v2, that is default to v1. */
657         if (id == GPIO_TYPE_V2 || id == GPIO_TYPE_V2_1) {
658                 bank->gpio_regs = &gpio_regs_v2;
659                 bank->gpio_type = GPIO_TYPE_V2;
660                 bank->db_clk = of_clk_get(bank->of_node, 1);
661                 if (IS_ERR(bank->db_clk)) {
662                         dev_err(bank->dev, "cannot find debounce clk\n");
663                         clk_disable_unprepare(bank->clk);
664                         return -EINVAL;
665                 }
666         } else {
667                 bank->gpio_regs = &gpio_regs_v1;
668                 bank->gpio_type = GPIO_TYPE_V1;
669         }
670
671         return 0;
672 }
673
674 static struct rockchip_pin_bank *
675 rockchip_gpio_find_bank(struct pinctrl_dev *pctldev, int id)
676 {
677         struct rockchip_pinctrl *info;
678         struct rockchip_pin_bank *bank;
679         int i, found = 0;
680
681         info = pinctrl_dev_get_drvdata(pctldev);
682         bank = info->ctrl->pin_banks;
683         for (i = 0; i < info->ctrl->nr_banks; i++, bank++) {
684                 if (bank->bank_num == id) {
685                         found = 1;
686                         break;
687                 }
688         }
689
690         return found ? bank : NULL;
691 }
692
693 static int rockchip_gpio_probe(struct platform_device *pdev)
694 {
695         struct device *dev = &pdev->dev;
696         struct device_node *np = dev->of_node;
697         struct device_node *pctlnp = of_get_parent(np);
698         struct pinctrl_dev *pctldev = NULL;
699         struct rockchip_pin_bank *bank = NULL;
700         struct rockchip_pin_deferred *cfg;
701         static int gpio;
702         int id, ret;
703
704         if (!np || !pctlnp)
705                 return -ENODEV;
706
707         pctldev = of_pinctrl_get(pctlnp);
708         if (!pctldev)
709                 return -EPROBE_DEFER;
710
711         id = of_alias_get_id(np, "gpio");
712         if (id < 0)
713                 id = gpio++;
714
715         bank = rockchip_gpio_find_bank(pctldev, id);
716         if (!bank)
717                 return -EINVAL;
718
719         bank->dev = dev;
720         bank->of_node = np;
721
722         raw_spin_lock_init(&bank->slock);
723
724         ret = rockchip_get_bank_data(bank);
725         if (ret)
726                 return ret;
727
728         /*
729          * Prevent clashes with a deferred output setting
730          * being added right at this moment.
731          */
732         mutex_lock(&bank->deferred_lock);
733
734         ret = rockchip_gpiolib_register(bank);
735         if (ret) {
736                 clk_disable_unprepare(bank->clk);
737                 mutex_unlock(&bank->deferred_lock);
738                 return ret;
739         }
740
741         while (!list_empty(&bank->deferred_pins)) {
742                 cfg = list_first_entry(&bank->deferred_pins,
743                                        struct rockchip_pin_deferred, head);
744                 list_del(&cfg->head);
745
746                 switch (cfg->param) {
747                 case PIN_CONFIG_OUTPUT:
748                         ret = rockchip_gpio_direction_output(&bank->gpio_chip, cfg->pin, cfg->arg);
749                         if (ret)
750                                 dev_warn(dev, "setting output pin %u to %u failed\n", cfg->pin,
751                                          cfg->arg);
752                         break;
753                 case PIN_CONFIG_INPUT_ENABLE:
754                         ret = rockchip_gpio_direction_input(&bank->gpio_chip, cfg->pin);
755                         if (ret)
756                                 dev_warn(dev, "setting input pin %u failed\n", cfg->pin);
757                         break;
758                 default:
759                         dev_warn(dev, "unknown deferred config param %d\n", cfg->param);
760                         break;
761                 }
762                 kfree(cfg);
763         }
764
765         mutex_unlock(&bank->deferred_lock);
766
767         platform_set_drvdata(pdev, bank);
768         dev_info(dev, "probed %pOF\n", np);
769
770         return 0;
771 }
772
773 static int rockchip_gpio_remove(struct platform_device *pdev)
774 {
775         struct rockchip_pin_bank *bank = platform_get_drvdata(pdev);
776
777         clk_disable_unprepare(bank->clk);
778         gpiochip_remove(&bank->gpio_chip);
779
780         return 0;
781 }
782
783 static const struct of_device_id rockchip_gpio_match[] = {
784         { .compatible = "rockchip,gpio-bank", },
785         { .compatible = "rockchip,rk3188-gpio-bank0" },
786         { },
787 };
788
789 static struct platform_driver rockchip_gpio_driver = {
790         .probe          = rockchip_gpio_probe,
791         .remove         = rockchip_gpio_remove,
792         .driver         = {
793                 .name   = "rockchip-gpio",
794                 .of_match_table = rockchip_gpio_match,
795         },
796 };
797
798 static int __init rockchip_gpio_init(void)
799 {
800         return platform_driver_register(&rockchip_gpio_driver);
801 }
802 postcore_initcall(rockchip_gpio_init);
803
804 static void __exit rockchip_gpio_exit(void)
805 {
806         platform_driver_unregister(&rockchip_gpio_driver);
807 }
808 module_exit(rockchip_gpio_exit);
809
810 MODULE_DESCRIPTION("Rockchip gpio driver");
811 MODULE_ALIAS("platform:rockchip-gpio");
812 MODULE_LICENSE("GPL v2");
813 MODULE_DEVICE_TABLE(of, rockchip_gpio_match);