• 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 "core.h"
22 #include "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         max_debounce = (GENMASK(0x17, 0) + 1) * 0x2 * 0xf4240 / freq;
202         if ((unsigned long)debounce > max_debounce) {
203             return -EINVAL;
204         }
205 
206         div = (unsigned long)debounce * freq;
207         div_reg = DIV_ROUND_CLOSEST_ULL(div, 0x2 * USEC_PER_SEC) - 1;
208     } else {
209         div_debounce_support = false;
210     }
211 
212     raw_spin_lock_irqsave(&bank->slock, flags);
213 
214     /* Only the v1 needs to configure div_en and div_con for dbclk */
215     if (debounce) {
216         if (div_debounce_support) {
217             /* Configure the max debounce from consumers */
218             cur_div_reg = readl(bank->reg_base + reg->dbclk_div_con);
219             if ((unsigned long)cur_div_reg < div_reg) {
220                 writel(div_reg, bank->reg_base + reg->dbclk_div_con);
221             }
222             rockchip_gpio_writel_bit(bank, offset, 1, reg->dbclk_div_en);
223         }
224 
225         rockchip_gpio_writel_bit(bank, offset, 1, reg->debounce);
226     } else {
227         if (div_debounce_support) {
228             rockchip_gpio_writel_bit(bank, offset, 0, reg->dbclk_div_en);
229         }
230 
231         rockchip_gpio_writel_bit(bank, offset, 0, reg->debounce);
232     }
233 
234     raw_spin_unlock_irqrestore(&bank->slock, flags);
235 
236     /* Enable or disable dbclk at last */
237     if (div_debounce_support) {
238         if (debounce) {
239             clk_prepare_enable(bank->db_clk);
240         } else {
241             clk_disable_unprepare(bank->db_clk);
242         }
243     }
244 
245     return 0;
246 }
247 
248 /*
249  * gpiolib set_config callback function. The setting of the pin
250  * mux function as 'gpio output' will be handled by the pinctrl subsystem
251  * interface.
252  */
rockchip_gpio_set_config(struct gpio_chip * gc,unsigned int offset,unsigned long config)253 static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset, unsigned long config)
254 {
255     enum pin_config_param param = pinconf_to_config_param(config);
256     unsigned int debounce = pinconf_to_config_argument(config);
257     int ret = 0;
258 
259     switch (param) {
260         case PIN_CONFIG_INPUT_DEBOUNCE:
261             /*
262              * Rockchip's gpio could only support up to one period
263              * of the debounce clock(pclk), which is far away from
264              * satisftying the requirement, as pclk is usually near
265              * 100MHz shared by all peripherals. So the fact is it
266              * has crippled debounce capability could only be useful
267              * to prevent any spurious glitches from waking up the system
268              * if the gpio is conguired as wakeup interrupt source. Let's
269              * still return -ENOTSUPP as before, to make sure the caller
270              * of gpiod_set_debounce won't change its behaviour.
271              */
272             rockchip_gpio_set_debounce(gc, offset, debounce);
273             ret = -ENOTSUPP;
274             break;
275         default:
276             ret = -ENOTSUPP;
277             break;
278     }
279 
280     return ret;
281 }
282 
283 /*
284  * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
285  * and a virtual IRQ, if not already present.
286  */
rockchip_gpio_to_irq(struct gpio_chip * gc,unsigned int offset)287 static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
288 {
289     struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
290     unsigned int virq;
291 
292     if (!bank->domain) {
293         return -ENXIO;
294     }
295 
296     virq = irq_create_mapping(bank->domain, offset);
297 
298     return (virq) ?: -ENXIO;
299 }
300 
301 static const struct gpio_chip rockchip_gpiolib_chip = {
302     .request = gpiochip_generic_request,
303     .free = gpiochip_generic_free,
304     .set = rockchip_gpio_set,
305     .get = rockchip_gpio_get,
306     .get_direction = rockchip_gpio_get_direction,
307     .direction_input = rockchip_gpio_direction_input,
308     .direction_output = rockchip_gpio_direction_output,
309     .set_config = rockchip_gpio_set_config,
310     .to_irq = rockchip_gpio_to_irq,
311     .owner = THIS_MODULE,
312 };
313 
rockchip_irq_demux(struct irq_desc * desc)314 static void rockchip_irq_demux(struct irq_desc *desc)
315 {
316     struct irq_chip *chip = irq_desc_get_chip(desc);
317     struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
318     const struct rockchip_gpio_regs *reg = bank->gpio_regs;
319     u32 pend;
320 
321     dev_dbg(bank->dev, "got irq for bank %s\n", bank->name);
322 
323     chained_irq_enter(chip, desc);
324 
325     pend = readl_relaxed(bank->reg_base + reg->int_status);
326 
327     while (pend) {
328         unsigned int irq, virq;
329 
330         irq = __ffs(pend);
331         pend &= ~BIT(irq);
332         virq = irq_find_mapping(bank->domain, irq);
333         if (!virq) {
334             dev_err(bank->dev, "unmapped irq %d\n", irq);
335             continue;
336         }
337 
338         dev_dbg(bank->dev, "handling irq %d\n", irq);
339 
340         /*
341          * Triggering IRQ on both rising and falling edge
342          * needs manual intervention.
343          */
344         if (bank->toggle_edge_mode & BIT(irq)) {
345             u32 data, data_old, polarity;
346             unsigned long flags;
347 
348             data = readl_relaxed(bank->reg_base + reg->ext_port);
349             do {
350                 raw_spin_lock_irqsave(&bank->slock, flags);
351 
352                 polarity = readl_relaxed(bank->reg_base + reg->int_polarity);
353                 if (data & BIT(irq)) {
354                     polarity &= ~BIT(irq);
355                 } else {
356                     polarity |= BIT(irq);
357                 }
358                 writel(polarity, bank->reg_base + reg->int_polarity);
359 
360                 raw_spin_unlock_irqrestore(&bank->slock, flags);
361 
362                 data_old = data;
363                 data = readl_relaxed(bank->reg_base + reg->ext_port);
364             } while ((data & BIT(irq)) != (data_old & BIT(irq)));
365         }
366 
367         generic_handle_irq(virq);
368     }
369 
370     chained_irq_exit(chip, desc);
371 }
372 
rockchip_irq_set_type(struct irq_data * d,unsigned int type)373 static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
374 {
375     struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
376     struct rockchip_pin_bank *bank = gc->private;
377     u32 mask = BIT(d->hwirq);
378     u32 polarity;
379     u32 level;
380     u32 data;
381     unsigned long flags;
382     int ret = 0;
383 
384     raw_spin_lock_irqsave(&bank->slock, flags);
385 
386     rockchip_gpio_writel_bit(bank, d->hwirq, 0, bank->gpio_regs->port_ddr);
387 
388     raw_spin_unlock_irqrestore(&bank->slock, flags);
389 
390     if (type & IRQ_TYPE_EDGE_BOTH) {
391         irq_set_handler_locked(d, handle_edge_irq);
392     } else {
393         irq_set_handler_locked(d, handle_level_irq);
394     }
395 
396     raw_spin_lock_irqsave(&bank->slock, flags);
397 
398     level = rockchip_gpio_readl(bank, bank->gpio_regs->int_type);
399     polarity = rockchip_gpio_readl(bank, bank->gpio_regs->int_polarity);
400 
401     switch (type) {
402         case IRQ_TYPE_EDGE_BOTH:
403             if (bank->gpio_type == GPIO_TYPE_V2) {
404                 bank->toggle_edge_mode &= ~mask;
405                 rockchip_gpio_writel_bit(bank, d->hwirq, 1, bank->gpio_regs->int_bothedge);
406                 goto out;
407             } else {
408                 bank->toggle_edge_mode |= mask;
409                 level |= mask;
410 
411                 /*
412                  * Determine gpio state. If 1 next interrupt should be falling
413                  * otherwise rising.
414                  */
415                 data = readl(bank->reg_base + bank->gpio_regs->ext_port);
416                 if (data & mask) {
417                     polarity &= ~mask;
418                 } else {
419                     polarity |= mask;
420                 }
421             }
422             break;
423         case IRQ_TYPE_EDGE_RISING:
424             bank->toggle_edge_mode &= ~mask;
425             level |= mask;
426             polarity |= mask;
427             break;
428         case IRQ_TYPE_EDGE_FALLING:
429             bank->toggle_edge_mode &= ~mask;
430             level |= mask;
431             polarity &= ~mask;
432             break;
433         case IRQ_TYPE_LEVEL_HIGH:
434             bank->toggle_edge_mode &= ~mask;
435             level &= ~mask;
436             polarity |= mask;
437             break;
438         case IRQ_TYPE_LEVEL_LOW:
439             bank->toggle_edge_mode &= ~mask;
440             level &= ~mask;
441             polarity &= ~mask;
442             break;
443         default:
444             ret = -EINVAL;
445             goto out;
446     }
447 
448     rockchip_gpio_writel(bank, level, bank->gpio_regs->int_type);
449     rockchip_gpio_writel(bank, polarity, bank->gpio_regs->int_polarity);
450 out:
451     raw_spin_unlock_irqrestore(&bank->slock, flags);
452 
453     return ret;
454 }
455 
rockchip_irq_suspend(struct irq_data * d)456 static void rockchip_irq_suspend(struct irq_data *d)
457 {
458     struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
459     struct rockchip_pin_bank *bank = gc->private;
460 
461     bank->saved_masks = irq_reg_readl(gc, bank->gpio_regs->int_mask);
462     irq_reg_writel(gc, ~gc->wake_active, bank->gpio_regs->int_mask);
463 }
464 
rockchip_irq_resume(struct irq_data * d)465 static void rockchip_irq_resume(struct irq_data *d)
466 {
467     struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
468     struct rockchip_pin_bank *bank = gc->private;
469 
470     irq_reg_writel(gc, bank->saved_masks, bank->gpio_regs->int_mask);
471 }
472 
rockchip_interrupts_register(struct rockchip_pin_bank * bank)473 static int rockchip_interrupts_register(struct rockchip_pin_bank *bank)
474 {
475     unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
476     struct irq_chip_generic *gc;
477     int ret;
478 
479     bank->domain = irq_domain_add_linear(bank->of_node, 0x20, &irq_generic_chip_ops, NULL);
480     if (!bank->domain) {
481         dev_warn(bank->dev, "could not initialize irq domain for bank %s\n", bank->name);
482         return -EINVAL;
483     }
484 
485     ret = irq_alloc_domain_generic_chips(bank->domain, 0x20, 1, bank->name, handle_level_irq, clr, 0, 0);
486     if (ret) {
487         dev_err(bank->dev, "could not alloc generic chips for bank %s\n", bank->name);
488         irq_domain_remove(bank->domain);
489         return ret;
490     }
491 
492     gc = irq_get_domain_generic_chip(bank->domain, 0);
493     if (bank->gpio_type == GPIO_TYPE_V2) {
494         gc->reg_writel = gpio_writel_v2;
495         gc->reg_readl = gpio_readl_v2;
496     }
497     gc->reg_base = bank->reg_base;
498     gc->private = bank;
499     gc->chip_types[0].regs.mask = bank->gpio_regs->int_mask;
500     gc->chip_types[0].regs.ack = bank->gpio_regs->port_eoi;
501     gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
502     gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
503     gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
504     gc->chip_types[0].chip.irq_enable = irq_gc_mask_clr_bit;
505     gc->chip_types[0].chip.irq_disable = irq_gc_mask_set_bit;
506     gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
507     gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
508     gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
509     gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
510     gc->wake_enabled = IRQ_MSK(bank->nr_pins);
511 
512     /*
513      * Linux assumes that all interrupts start out disabled/masked.
514      * Our driver only uses the concept of masked and always keeps
515      * things enabled, so for us that's all masked and all enabled.
516      */
517     rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_mask);
518     rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->port_eoi);
519     rockchip_gpio_writel(bank, 0xffffffff, bank->gpio_regs->int_en);
520     gc->mask_cache = 0xffffffff;
521 
522     irq_set_chained_handler_and_data(bank->irq, rockchip_irq_demux, bank);
523 
524     return 0;
525 }
526 
rockchip_gpiolib_register(struct rockchip_pin_bank * bank)527 static int rockchip_gpiolib_register(struct rockchip_pin_bank *bank)
528 {
529     struct gpio_chip *gc;
530     int ret;
531 
532     bank->gpio_chip = rockchip_gpiolib_chip;
533 
534     gc = &bank->gpio_chip;
535     gc->base = bank->pin_base;
536     gc->ngpio = bank->nr_pins;
537     gc->label = bank->name;
538     gc->parent = bank->dev;
539 #ifdef CONFIG_OF_GPIO
540     gc->of_node = of_node_get(bank->of_node);
541 #endif
542 
543     ret = gpiochip_add_data(gc, bank);
544     if (ret) {
545         dev_err(bank->dev, "failed to add gpiochip %s, %d\n", gc->label, ret);
546         return ret;
547     }
548 
549     /*
550      * For DeviceTree-supported systems, the gpio core checks the
551      * pinctrl's device node for the "gpio-ranges" property.
552      * If it is present, it takes care of adding the pin ranges
553      * for the driver. In this case the driver can skip ahead.
554      *
555      * In order to remain compatible with older, existing DeviceTree
556      * files which don't set the "gpio-ranges" property or systems that
557      * utilize ACPI the driver has to call gpiochip_add_pin_range().
558      */
559     if (!of_property_read_bool(bank->of_node, "gpio-ranges")) {
560         struct device_node *pctlnp = of_get_parent(bank->of_node);
561         struct pinctrl_dev *pctldev = NULL;
562 
563         if (!pctlnp) {
564             return -ENODATA;
565         }
566 
567         pctldev = of_pinctrl_get(pctlnp);
568         if (!pctldev) {
569             return -ENODEV;
570         }
571 
572         ret = gpiochip_add_pin_range(gc, dev_name(pctldev->dev), 0, gc->base, gc->ngpio);
573         if (ret) {
574             dev_err(bank->dev, "Failed to add pin range\n");
575             goto fail;
576         }
577     }
578 
579     ret = rockchip_interrupts_register(bank);
580     if (ret) {
581         dev_err(bank->dev, "failed to register interrupt, %d\n", ret);
582         goto fail;
583     }
584 
585     return 0;
586 
587 fail:
588     gpiochip_remove(&bank->gpio_chip);
589 
590     return ret;
591 }
592 
rockchip_get_bank_data(struct rockchip_pin_bank * bank)593 static int rockchip_get_bank_data(struct rockchip_pin_bank *bank)
594 {
595     struct resource res;
596     int id = 0;
597 
598     if (of_address_to_resource(bank->of_node, 0, &res)) {
599         return -ENOENT;
600     }
601 
602     bank->reg_base = devm_ioremap_resource(bank->dev, &res);
603     if (IS_ERR(bank->reg_base)) {
604         return PTR_ERR(bank->reg_base);
605     }
606 
607     bank->irq = irq_of_parse_and_map(bank->of_node, 0);
608     if (!bank->irq) {
609         return -EINVAL;
610     }
611 
612     bank->clk = of_clk_get(bank->of_node, 0);
613     if (IS_ERR(bank->clk)) {
614         return PTR_ERR(bank->clk);
615     }
616 
617     clk_prepare_enable(bank->clk);
618     id = readl(bank->reg_base + gpio_regs_v2.version_id);
619     /* If not gpio v2, that is default to v1. */
620     if (id == GPIO_TYPE_V2) {
621         bank->gpio_regs = &gpio_regs_v2;
622         bank->gpio_type = GPIO_TYPE_V2;
623         bank->db_clk = of_clk_get(bank->of_node, 1);
624         if (IS_ERR(bank->db_clk)) {
625             dev_err(bank->dev, "cannot find debounce clk\n");
626             bank->db_clk = NULL;
627             return -EINVAL;
628         }
629     } else {
630         bank->gpio_regs = &gpio_regs_v1;
631         bank->gpio_type = GPIO_TYPE_V1;
632     }
633 
634     return 0;
635 }
636 
rockchip_gpio_find_bank(struct pinctrl_dev * pctldev,int id)637 static struct rockchip_pin_bank *rockchip_gpio_find_bank(struct pinctrl_dev *pctldev, int id)
638 {
639     struct rockchip_pinctrl *info;
640     struct rockchip_pin_bank *bank;
641     int i, found = 0;
642 
643     info = pinctrl_dev_get_drvdata(pctldev);
644     bank = info->ctrl->pin_banks;
645     for (i = 0; i < info->ctrl->nr_banks; i++, bank++) {
646         if (bank->bank_num == id) {
647             found = 1;
648             break;
649         }
650     }
651 
652     return found ? bank : NULL;
653 }
654 
rockchip_gpio_probe(struct platform_device * pdev)655 static int rockchip_gpio_probe(struct platform_device *pdev)
656 {
657     struct device *dev = &pdev->dev;
658     struct device_node *np = pdev->dev.of_node;
659     struct device_node *pctlnp = of_get_parent(np);
660     struct pinctrl_dev *pctldev = NULL;
661     struct rockchip_pin_bank *bank = NULL;
662     static int gpio;
663     int id, ret;
664 
665     if (!np || !pctlnp) {
666         return -ENODEV;
667     }
668 
669     pctldev = of_pinctrl_get(pctlnp);
670     if (!pctldev) {
671         return -EPROBE_DEFER;
672     }
673 
674     id = of_alias_get_id(np, "gpio");
675     if (id < 0) {
676         id = gpio++;
677     }
678 
679     bank = rockchip_gpio_find_bank(pctldev, id);
680     if (!bank) {
681         return -EINVAL;
682     }
683 
684     bank->dev = dev;
685     bank->of_node = dev->of_node;
686 
687     raw_spin_lock_init(&bank->slock);
688 
689     ret = rockchip_get_bank_data(bank);
690     if (ret) {
691         return ret;
692     }
693 
694     ret = rockchip_gpiolib_register(bank);
695     if (ret) {
696         goto err_clk;
697     }
698 
699     platform_set_drvdata(pdev, bank);
700     dev_info(dev, "probed %s (%s)\n", bank->name, dev_name(dev));
701 
702     return 0;
703 err_clk:
704     clk_disable_unprepare(bank->clk);
705 
706     return ret;
707 }
708 
rockchip_gpio_remove(struct platform_device * pdev)709 static int rockchip_gpio_remove(struct platform_device *pdev)
710 {
711     struct rockchip_pin_bank *bank = platform_get_drvdata(pdev);
712 
713     clk_disable_unprepare(bank->clk);
714     gpiochip_remove(&bank->gpio_chip);
715 
716     return 0;
717 }
718 
719 static const struct of_device_id rockchip_gpio_match[] = {
720     {
721         .compatible = "rockchip,gpio-bank",
722     },
723     {.compatible = "rockchip,rk3188-gpio-bank0"},
724     {},
725 };
726 
727 static struct platform_driver rockchip_gpio_driver = {
728     .probe = rockchip_gpio_probe,
729     .remove = rockchip_gpio_remove,
730     .driver =
731         {
732             .name = "rockchip-gpio",
733             .of_match_table = rockchip_gpio_match,
734         },
735 };
736 
rockchip_gpio_init(void)737 static int __init rockchip_gpio_init(void)
738 {
739     return platform_driver_register(&rockchip_gpio_driver);
740 }
741 postcore_initcall(rockchip_gpio_init);
742 
rockchip_gpio_exit(void)743 static void __exit rockchip_gpio_exit(void)
744 {
745     platform_driver_unregister(&rockchip_gpio_driver);
746 }
747 module_exit(rockchip_gpio_exit);
748 
749 MODULE_DESCRIPTION("Rockchip gpio driver");
750 MODULE_ALIAS("platform:rockchip-gpio");
751 MODULE_LICENSE("GPL v2");
752 MODULE_DEVICE_TABLE(of, rockchip_gpio_match);
753