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