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