xref: /linux/drivers/pinctrl/intel/pinctrl-intel.c (revision 63e037bc51b32d414cd07dd700e71359ce27a11f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel pinctrl/GPIO core driver.
4  *
5  * Copyright (C) 2015, Intel Corporation
6  * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
7  *          Mika Westerberg <mika.westerberg@linux.intel.com>
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 
23 #include "../core.h"
24 #include "pinctrl-intel.h"
25 
26 /* Offset from regs */
27 #define REVID				0x000
28 #define REVID_SHIFT			16
29 #define REVID_MASK			GENMASK(31, 16)
30 
31 #define PADBAR				0x00c
32 #define GPI_IS				0x100
33 
34 #define PADOWN_BITS			4
35 #define PADOWN_SHIFT(p)			((p) % 8 * PADOWN_BITS)
36 #define PADOWN_MASK(p)			(0xf << PADOWN_SHIFT(p))
37 #define PADOWN_GPP(p)			((p) / 8)
38 
39 /* Offset from pad_regs */
40 #define PADCFG0				0x000
41 #define PADCFG0_RXEVCFG_SHIFT		25
42 #define PADCFG0_RXEVCFG_MASK		(3 << PADCFG0_RXEVCFG_SHIFT)
43 #define PADCFG0_RXEVCFG_LEVEL		0
44 #define PADCFG0_RXEVCFG_EDGE		1
45 #define PADCFG0_RXEVCFG_DISABLED	2
46 #define PADCFG0_RXEVCFG_EDGE_BOTH	3
47 #define PADCFG0_PREGFRXSEL		BIT(24)
48 #define PADCFG0_RXINV			BIT(23)
49 #define PADCFG0_GPIROUTIOXAPIC		BIT(20)
50 #define PADCFG0_GPIROUTSCI		BIT(19)
51 #define PADCFG0_GPIROUTSMI		BIT(18)
52 #define PADCFG0_GPIROUTNMI		BIT(17)
53 #define PADCFG0_PMODE_SHIFT		10
54 #define PADCFG0_PMODE_MASK		(0xf << PADCFG0_PMODE_SHIFT)
55 #define PADCFG0_GPIORXDIS		BIT(9)
56 #define PADCFG0_GPIOTXDIS		BIT(8)
57 #define PADCFG0_GPIORXSTATE		BIT(1)
58 #define PADCFG0_GPIOTXSTATE		BIT(0)
59 
60 #define PADCFG1				0x004
61 #define PADCFG1_TERM_UP			BIT(13)
62 #define PADCFG1_TERM_SHIFT		10
63 #define PADCFG1_TERM_MASK		(7 << PADCFG1_TERM_SHIFT)
64 #define PADCFG1_TERM_20K		4
65 #define PADCFG1_TERM_2K			3
66 #define PADCFG1_TERM_5K			2
67 #define PADCFG1_TERM_1K			1
68 
69 #define PADCFG2				0x008
70 #define PADCFG2_DEBEN			BIT(0)
71 #define PADCFG2_DEBOUNCE_SHIFT		1
72 #define PADCFG2_DEBOUNCE_MASK		GENMASK(4, 1)
73 
74 #define DEBOUNCE_PERIOD			31250 /* ns */
75 
76 struct intel_pad_context {
77 	u32 padcfg0;
78 	u32 padcfg1;
79 	u32 padcfg2;
80 };
81 
82 struct intel_community_context {
83 	u32 *intmask;
84 };
85 
86 struct intel_pinctrl_context {
87 	struct intel_pad_context *pads;
88 	struct intel_community_context *communities;
89 };
90 
91 /**
92  * struct intel_pinctrl - Intel pinctrl private structure
93  * @dev: Pointer to the device structure
94  * @lock: Lock to serialize register access
95  * @pctldesc: Pin controller description
96  * @pctldev: Pointer to the pin controller device
97  * @chip: GPIO chip in this pin controller
98  * @soc: SoC/PCH specific pin configuration data
99  * @communities: All communities in this pin controller
100  * @ncommunities: Number of communities in this pin controller
101  * @context: Configuration saved over system sleep
102  * @irq: pinctrl/GPIO chip irq number
103  */
104 struct intel_pinctrl {
105 	struct device *dev;
106 	raw_spinlock_t lock;
107 	struct pinctrl_desc pctldesc;
108 	struct pinctrl_dev *pctldev;
109 	struct gpio_chip chip;
110 	const struct intel_pinctrl_soc_data *soc;
111 	struct intel_community *communities;
112 	size_t ncommunities;
113 	struct intel_pinctrl_context context;
114 	int irq;
115 };
116 
117 #define pin_to_padno(c, p)	((p) - (c)->pin_base)
118 #define padgroup_offset(g, p)	((p) - (g)->base)
119 
120 static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
121 						   unsigned pin)
122 {
123 	struct intel_community *community;
124 	int i;
125 
126 	for (i = 0; i < pctrl->ncommunities; i++) {
127 		community = &pctrl->communities[i];
128 		if (pin >= community->pin_base &&
129 		    pin < community->pin_base + community->npins)
130 			return community;
131 	}
132 
133 	dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
134 	return NULL;
135 }
136 
137 static const struct intel_padgroup *
138 intel_community_get_padgroup(const struct intel_community *community,
139 			     unsigned pin)
140 {
141 	int i;
142 
143 	for (i = 0; i < community->ngpps; i++) {
144 		const struct intel_padgroup *padgrp = &community->gpps[i];
145 
146 		if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
147 			return padgrp;
148 	}
149 
150 	return NULL;
151 }
152 
153 static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
154 				      unsigned reg)
155 {
156 	const struct intel_community *community;
157 	unsigned padno;
158 	size_t nregs;
159 
160 	community = intel_get_community(pctrl, pin);
161 	if (!community)
162 		return NULL;
163 
164 	padno = pin_to_padno(community, pin);
165 	nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
166 
167 	if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
168 		return NULL;
169 
170 	return community->pad_regs + reg + padno * nregs * 4;
171 }
172 
173 static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
174 {
175 	const struct intel_community *community;
176 	const struct intel_padgroup *padgrp;
177 	unsigned gpp, offset, gpp_offset;
178 	void __iomem *padown;
179 
180 	community = intel_get_community(pctrl, pin);
181 	if (!community)
182 		return false;
183 	if (!community->padown_offset)
184 		return true;
185 
186 	padgrp = intel_community_get_padgroup(community, pin);
187 	if (!padgrp)
188 		return false;
189 
190 	gpp_offset = padgroup_offset(padgrp, pin);
191 	gpp = PADOWN_GPP(gpp_offset);
192 	offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
193 	padown = community->regs + offset;
194 
195 	return !(readl(padown) & PADOWN_MASK(gpp_offset));
196 }
197 
198 static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
199 {
200 	const struct intel_community *community;
201 	const struct intel_padgroup *padgrp;
202 	unsigned offset, gpp_offset;
203 	void __iomem *hostown;
204 
205 	community = intel_get_community(pctrl, pin);
206 	if (!community)
207 		return true;
208 	if (!community->hostown_offset)
209 		return false;
210 
211 	padgrp = intel_community_get_padgroup(community, pin);
212 	if (!padgrp)
213 		return true;
214 
215 	gpp_offset = padgroup_offset(padgrp, pin);
216 	offset = community->hostown_offset + padgrp->reg_num * 4;
217 	hostown = community->regs + offset;
218 
219 	return !(readl(hostown) & BIT(gpp_offset));
220 }
221 
222 static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
223 {
224 	struct intel_community *community;
225 	const struct intel_padgroup *padgrp;
226 	unsigned offset, gpp_offset;
227 	u32 value;
228 
229 	community = intel_get_community(pctrl, pin);
230 	if (!community)
231 		return true;
232 	if (!community->padcfglock_offset)
233 		return false;
234 
235 	padgrp = intel_community_get_padgroup(community, pin);
236 	if (!padgrp)
237 		return true;
238 
239 	gpp_offset = padgroup_offset(padgrp, pin);
240 
241 	/*
242 	 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
243 	 * the pad is considered unlocked. Any other case means that it is
244 	 * either fully or partially locked and we don't touch it.
245 	 */
246 	offset = community->padcfglock_offset + padgrp->reg_num * 8;
247 	value = readl(community->regs + offset);
248 	if (value & BIT(gpp_offset))
249 		return true;
250 
251 	offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
252 	value = readl(community->regs + offset);
253 	if (value & BIT(gpp_offset))
254 		return true;
255 
256 	return false;
257 }
258 
259 static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
260 {
261 	return intel_pad_owned_by_host(pctrl, pin) &&
262 		!intel_pad_locked(pctrl, pin);
263 }
264 
265 static int intel_get_groups_count(struct pinctrl_dev *pctldev)
266 {
267 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
268 
269 	return pctrl->soc->ngroups;
270 }
271 
272 static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
273 				      unsigned group)
274 {
275 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
276 
277 	return pctrl->soc->groups[group].name;
278 }
279 
280 static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
281 			      const unsigned **pins, unsigned *npins)
282 {
283 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
284 
285 	*pins = pctrl->soc->groups[group].pins;
286 	*npins = pctrl->soc->groups[group].npins;
287 	return 0;
288 }
289 
290 static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
291 			       unsigned pin)
292 {
293 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
294 	void __iomem *padcfg;
295 	u32 cfg0, cfg1, mode;
296 	bool locked, acpi;
297 
298 	if (!intel_pad_owned_by_host(pctrl, pin)) {
299 		seq_puts(s, "not available");
300 		return;
301 	}
302 
303 	cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
304 	cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
305 
306 	mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
307 	if (!mode)
308 		seq_puts(s, "GPIO ");
309 	else
310 		seq_printf(s, "mode %d ", mode);
311 
312 	seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
313 
314 	/* Dump the additional PADCFG registers if available */
315 	padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
316 	if (padcfg)
317 		seq_printf(s, " 0x%08x", readl(padcfg));
318 
319 	locked = intel_pad_locked(pctrl, pin);
320 	acpi = intel_pad_acpi_mode(pctrl, pin);
321 
322 	if (locked || acpi) {
323 		seq_puts(s, " [");
324 		if (locked) {
325 			seq_puts(s, "LOCKED");
326 			if (acpi)
327 				seq_puts(s, ", ");
328 		}
329 		if (acpi)
330 			seq_puts(s, "ACPI");
331 		seq_puts(s, "]");
332 	}
333 }
334 
335 static const struct pinctrl_ops intel_pinctrl_ops = {
336 	.get_groups_count = intel_get_groups_count,
337 	.get_group_name = intel_get_group_name,
338 	.get_group_pins = intel_get_group_pins,
339 	.pin_dbg_show = intel_pin_dbg_show,
340 };
341 
342 static int intel_get_functions_count(struct pinctrl_dev *pctldev)
343 {
344 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
345 
346 	return pctrl->soc->nfunctions;
347 }
348 
349 static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
350 					   unsigned function)
351 {
352 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
353 
354 	return pctrl->soc->functions[function].name;
355 }
356 
357 static int intel_get_function_groups(struct pinctrl_dev *pctldev,
358 				     unsigned function,
359 				     const char * const **groups,
360 				     unsigned * const ngroups)
361 {
362 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
363 
364 	*groups = pctrl->soc->functions[function].groups;
365 	*ngroups = pctrl->soc->functions[function].ngroups;
366 	return 0;
367 }
368 
369 static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
370 				unsigned group)
371 {
372 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
373 	const struct intel_pingroup *grp = &pctrl->soc->groups[group];
374 	unsigned long flags;
375 	int i;
376 
377 	raw_spin_lock_irqsave(&pctrl->lock, flags);
378 
379 	/*
380 	 * All pins in the groups needs to be accessible and writable
381 	 * before we can enable the mux for this group.
382 	 */
383 	for (i = 0; i < grp->npins; i++) {
384 		if (!intel_pad_usable(pctrl, grp->pins[i])) {
385 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
386 			return -EBUSY;
387 		}
388 	}
389 
390 	/* Now enable the mux setting for each pin in the group */
391 	for (i = 0; i < grp->npins; i++) {
392 		void __iomem *padcfg0;
393 		u32 value;
394 
395 		padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
396 		value = readl(padcfg0);
397 
398 		value &= ~PADCFG0_PMODE_MASK;
399 
400 		if (grp->modes)
401 			value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
402 		else
403 			value |= grp->mode << PADCFG0_PMODE_SHIFT;
404 
405 		writel(value, padcfg0);
406 	}
407 
408 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
409 
410 	return 0;
411 }
412 
413 static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
414 {
415 	u32 value;
416 
417 	value = readl(padcfg0);
418 	if (input) {
419 		value &= ~PADCFG0_GPIORXDIS;
420 		value |= PADCFG0_GPIOTXDIS;
421 	} else {
422 		value &= ~PADCFG0_GPIOTXDIS;
423 		value |= PADCFG0_GPIORXDIS;
424 	}
425 	writel(value, padcfg0);
426 }
427 
428 static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
429 {
430 	u32 value;
431 
432 	/* Put the pad into GPIO mode */
433 	value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
434 	/* Disable SCI/SMI/NMI generation */
435 	value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
436 	value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
437 	writel(value, padcfg0);
438 }
439 
440 static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
441 				     struct pinctrl_gpio_range *range,
442 				     unsigned pin)
443 {
444 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
445 	void __iomem *padcfg0;
446 	unsigned long flags;
447 
448 	raw_spin_lock_irqsave(&pctrl->lock, flags);
449 
450 	if (!intel_pad_usable(pctrl, pin)) {
451 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
452 		return -EBUSY;
453 	}
454 
455 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
456 	intel_gpio_set_gpio_mode(padcfg0);
457 	/* Disable TX buffer and enable RX (this will be input) */
458 	__intel_gpio_set_direction(padcfg0, true);
459 
460 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
461 
462 	return 0;
463 }
464 
465 static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
466 				    struct pinctrl_gpio_range *range,
467 				    unsigned pin, bool input)
468 {
469 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
470 	void __iomem *padcfg0;
471 	unsigned long flags;
472 
473 	raw_spin_lock_irqsave(&pctrl->lock, flags);
474 
475 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
476 	__intel_gpio_set_direction(padcfg0, input);
477 
478 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
479 
480 	return 0;
481 }
482 
483 static const struct pinmux_ops intel_pinmux_ops = {
484 	.get_functions_count = intel_get_functions_count,
485 	.get_function_name = intel_get_function_name,
486 	.get_function_groups = intel_get_function_groups,
487 	.set_mux = intel_pinmux_set_mux,
488 	.gpio_request_enable = intel_gpio_request_enable,
489 	.gpio_set_direction = intel_gpio_set_direction,
490 };
491 
492 static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
493 			    unsigned long *config)
494 {
495 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
496 	enum pin_config_param param = pinconf_to_config_param(*config);
497 	const struct intel_community *community;
498 	u32 value, term;
499 	u32 arg = 0;
500 
501 	if (!intel_pad_owned_by_host(pctrl, pin))
502 		return -ENOTSUPP;
503 
504 	community = intel_get_community(pctrl, pin);
505 	value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
506 	term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
507 
508 	switch (param) {
509 	case PIN_CONFIG_BIAS_DISABLE:
510 		if (term)
511 			return -EINVAL;
512 		break;
513 
514 	case PIN_CONFIG_BIAS_PULL_UP:
515 		if (!term || !(value & PADCFG1_TERM_UP))
516 			return -EINVAL;
517 
518 		switch (term) {
519 		case PADCFG1_TERM_1K:
520 			arg = 1000;
521 			break;
522 		case PADCFG1_TERM_2K:
523 			arg = 2000;
524 			break;
525 		case PADCFG1_TERM_5K:
526 			arg = 5000;
527 			break;
528 		case PADCFG1_TERM_20K:
529 			arg = 20000;
530 			break;
531 		}
532 
533 		break;
534 
535 	case PIN_CONFIG_BIAS_PULL_DOWN:
536 		if (!term || value & PADCFG1_TERM_UP)
537 			return -EINVAL;
538 
539 		switch (term) {
540 		case PADCFG1_TERM_1K:
541 			if (!(community->features & PINCTRL_FEATURE_1K_PD))
542 				return -EINVAL;
543 			arg = 1000;
544 			break;
545 		case PADCFG1_TERM_5K:
546 			arg = 5000;
547 			break;
548 		case PADCFG1_TERM_20K:
549 			arg = 20000;
550 			break;
551 		}
552 
553 		break;
554 
555 	case PIN_CONFIG_INPUT_DEBOUNCE: {
556 		void __iomem *padcfg2;
557 		u32 v;
558 
559 		padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
560 		if (!padcfg2)
561 			return -ENOTSUPP;
562 
563 		v = readl(padcfg2);
564 		if (!(v & PADCFG2_DEBEN))
565 			return -EINVAL;
566 
567 		v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
568 		arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
569 
570 		break;
571 	}
572 
573 	default:
574 		return -ENOTSUPP;
575 	}
576 
577 	*config = pinconf_to_config_packed(param, arg);
578 	return 0;
579 }
580 
581 static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
582 				 unsigned long config)
583 {
584 	unsigned param = pinconf_to_config_param(config);
585 	unsigned arg = pinconf_to_config_argument(config);
586 	const struct intel_community *community;
587 	void __iomem *padcfg1;
588 	unsigned long flags;
589 	int ret = 0;
590 	u32 value;
591 
592 	raw_spin_lock_irqsave(&pctrl->lock, flags);
593 
594 	community = intel_get_community(pctrl, pin);
595 	padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
596 	value = readl(padcfg1);
597 
598 	switch (param) {
599 	case PIN_CONFIG_BIAS_DISABLE:
600 		value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
601 		break;
602 
603 	case PIN_CONFIG_BIAS_PULL_UP:
604 		value &= ~PADCFG1_TERM_MASK;
605 
606 		value |= PADCFG1_TERM_UP;
607 
608 		switch (arg) {
609 		case 20000:
610 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
611 			break;
612 		case 5000:
613 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
614 			break;
615 		case 2000:
616 			value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
617 			break;
618 		case 1000:
619 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
620 			break;
621 		default:
622 			ret = -EINVAL;
623 		}
624 
625 		break;
626 
627 	case PIN_CONFIG_BIAS_PULL_DOWN:
628 		value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
629 
630 		switch (arg) {
631 		case 20000:
632 			value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
633 			break;
634 		case 5000:
635 			value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
636 			break;
637 		case 1000:
638 			if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
639 				ret = -EINVAL;
640 				break;
641 			}
642 			value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
643 			break;
644 		default:
645 			ret = -EINVAL;
646 		}
647 
648 		break;
649 	}
650 
651 	if (!ret)
652 		writel(value, padcfg1);
653 
654 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
655 
656 	return ret;
657 }
658 
659 static int intel_config_set_debounce(struct intel_pinctrl *pctrl, unsigned pin,
660 				     unsigned debounce)
661 {
662 	void __iomem *padcfg0, *padcfg2;
663 	unsigned long flags;
664 	u32 value0, value2;
665 	int ret = 0;
666 
667 	padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
668 	if (!padcfg2)
669 		return -ENOTSUPP;
670 
671 	padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
672 
673 	raw_spin_lock_irqsave(&pctrl->lock, flags);
674 
675 	value0 = readl(padcfg0);
676 	value2 = readl(padcfg2);
677 
678 	/* Disable glitch filter and debouncer */
679 	value0 &= ~PADCFG0_PREGFRXSEL;
680 	value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
681 
682 	if (debounce) {
683 		unsigned long v;
684 
685 		v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
686 		if (v < 3 || v > 15) {
687 			ret = -EINVAL;
688 			goto exit_unlock;
689 		} else {
690 			/* Enable glitch filter and debouncer */
691 			value0 |= PADCFG0_PREGFRXSEL;
692 			value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
693 			value2 |= PADCFG2_DEBEN;
694 		}
695 	}
696 
697 	writel(value0, padcfg0);
698 	writel(value2, padcfg2);
699 
700 exit_unlock:
701 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
702 
703 	return ret;
704 }
705 
706 static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
707 			  unsigned long *configs, unsigned nconfigs)
708 {
709 	struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
710 	int i, ret;
711 
712 	if (!intel_pad_usable(pctrl, pin))
713 		return -ENOTSUPP;
714 
715 	for (i = 0; i < nconfigs; i++) {
716 		switch (pinconf_to_config_param(configs[i])) {
717 		case PIN_CONFIG_BIAS_DISABLE:
718 		case PIN_CONFIG_BIAS_PULL_UP:
719 		case PIN_CONFIG_BIAS_PULL_DOWN:
720 			ret = intel_config_set_pull(pctrl, pin, configs[i]);
721 			if (ret)
722 				return ret;
723 			break;
724 
725 		case PIN_CONFIG_INPUT_DEBOUNCE:
726 			ret = intel_config_set_debounce(pctrl, pin,
727 				pinconf_to_config_argument(configs[i]));
728 			if (ret)
729 				return ret;
730 			break;
731 
732 		default:
733 			return -ENOTSUPP;
734 		}
735 	}
736 
737 	return 0;
738 }
739 
740 static const struct pinconf_ops intel_pinconf_ops = {
741 	.is_generic = true,
742 	.pin_config_get = intel_config_get,
743 	.pin_config_set = intel_config_set,
744 };
745 
746 static const struct pinctrl_desc intel_pinctrl_desc = {
747 	.pctlops = &intel_pinctrl_ops,
748 	.pmxops = &intel_pinmux_ops,
749 	.confops = &intel_pinconf_ops,
750 	.owner = THIS_MODULE,
751 };
752 
753 static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
754 {
755 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
756 	void __iomem *reg;
757 	u32 padcfg0;
758 
759 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
760 	if (!reg)
761 		return -EINVAL;
762 
763 	padcfg0 = readl(reg);
764 	if (!(padcfg0 & PADCFG0_GPIOTXDIS))
765 		return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
766 
767 	return !!(padcfg0 & PADCFG0_GPIORXSTATE);
768 }
769 
770 static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
771 {
772 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
773 	unsigned long flags;
774 	void __iomem *reg;
775 	u32 padcfg0;
776 
777 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
778 	if (!reg)
779 		return;
780 
781 	raw_spin_lock_irqsave(&pctrl->lock, flags);
782 	padcfg0 = readl(reg);
783 	if (value)
784 		padcfg0 |= PADCFG0_GPIOTXSTATE;
785 	else
786 		padcfg0 &= ~PADCFG0_GPIOTXSTATE;
787 	writel(padcfg0, reg);
788 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
789 }
790 
791 static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
792 {
793 	struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
794 	void __iomem *reg;
795 	u32 padcfg0;
796 
797 	reg = intel_get_padcfg(pctrl, offset, PADCFG0);
798 	if (!reg)
799 		return -EINVAL;
800 
801 	padcfg0 = readl(reg);
802 
803 	if (padcfg0 & PADCFG0_PMODE_MASK)
804 		return -EINVAL;
805 
806 	return !!(padcfg0 & PADCFG0_GPIOTXDIS);
807 }
808 
809 static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
810 {
811 	return pinctrl_gpio_direction_input(chip->base + offset);
812 }
813 
814 static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
815 				       int value)
816 {
817 	intel_gpio_set(chip, offset, value);
818 	return pinctrl_gpio_direction_output(chip->base + offset);
819 }
820 
821 static const struct gpio_chip intel_gpio_chip = {
822 	.owner = THIS_MODULE,
823 	.request = gpiochip_generic_request,
824 	.free = gpiochip_generic_free,
825 	.get_direction = intel_gpio_get_direction,
826 	.direction_input = intel_gpio_direction_input,
827 	.direction_output = intel_gpio_direction_output,
828 	.get = intel_gpio_get,
829 	.set = intel_gpio_set,
830 	.set_config = gpiochip_generic_config,
831 };
832 
833 /**
834  * intel_gpio_to_pin() - Translate from GPIO offset to pin number
835  * @pctrl: Pinctrl structure
836  * @offset: GPIO offset from gpiolib
837  * @community: Community is filled here if not %NULL
838  * @padgrp: Pad group is filled here if not %NULL
839  *
840  * When coming through gpiolib irqchip, the GPIO offset is not
841  * automatically translated to pinctrl pin number. This function can be
842  * used to find out the corresponding pinctrl pin.
843  */
844 static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned offset,
845 			     const struct intel_community **community,
846 			     const struct intel_padgroup **padgrp)
847 {
848 	int i;
849 
850 	for (i = 0; i < pctrl->ncommunities; i++) {
851 		const struct intel_community *comm = &pctrl->communities[i];
852 		int j;
853 
854 		for (j = 0; j < comm->ngpps; j++) {
855 			const struct intel_padgroup *pgrp = &comm->gpps[j];
856 
857 			if (pgrp->gpio_base < 0)
858 				continue;
859 
860 			if (offset >= pgrp->gpio_base &&
861 			    offset < pgrp->gpio_base + pgrp->size) {
862 				int pin;
863 
864 				pin = pgrp->base + offset - pgrp->gpio_base;
865 				if (community)
866 					*community = comm;
867 				if (padgrp)
868 					*padgrp = pgrp;
869 
870 				return pin;
871 			}
872 		}
873 	}
874 
875 	return -EINVAL;
876 }
877 
878 static int intel_gpio_irq_reqres(struct irq_data *d)
879 {
880 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
881 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
882 	int pin;
883 	int ret;
884 
885 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
886 	if (pin >= 0) {
887 		ret = gpiochip_lock_as_irq(gc, pin);
888 		if (ret) {
889 			dev_err(pctrl->dev, "unable to lock HW IRQ %d for IRQ\n",
890 				pin);
891 			return ret;
892 		}
893 	}
894 	return 0;
895 }
896 
897 static void intel_gpio_irq_relres(struct irq_data *d)
898 {
899 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
900 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
901 	int pin;
902 
903 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
904 	if (pin >= 0)
905 		gpiochip_unlock_as_irq(gc, pin);
906 }
907 
908 static void intel_gpio_irq_ack(struct irq_data *d)
909 {
910 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
911 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
912 	const struct intel_community *community;
913 	const struct intel_padgroup *padgrp;
914 	int pin;
915 
916 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
917 	if (pin >= 0) {
918 		unsigned gpp, gpp_offset, is_offset;
919 
920 		gpp = padgrp->reg_num;
921 		gpp_offset = padgroup_offset(padgrp, pin);
922 		is_offset = community->is_offset + gpp * 4;
923 
924 		raw_spin_lock(&pctrl->lock);
925 		writel(BIT(gpp_offset), community->regs + is_offset);
926 		raw_spin_unlock(&pctrl->lock);
927 	}
928 }
929 
930 static void intel_gpio_irq_enable(struct irq_data *d)
931 {
932 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
933 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
934 	const struct intel_community *community;
935 	const struct intel_padgroup *padgrp;
936 	int pin;
937 
938 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
939 	if (pin >= 0) {
940 		unsigned gpp, gpp_offset, is_offset;
941 		unsigned long flags;
942 		u32 value;
943 
944 		gpp = padgrp->reg_num;
945 		gpp_offset = padgroup_offset(padgrp, pin);
946 		is_offset = community->is_offset + gpp * 4;
947 
948 		raw_spin_lock_irqsave(&pctrl->lock, flags);
949 		/* Clear interrupt status first to avoid unexpected interrupt */
950 		writel(BIT(gpp_offset), community->regs + is_offset);
951 
952 		value = readl(community->regs + community->ie_offset + gpp * 4);
953 		value |= BIT(gpp_offset);
954 		writel(value, community->regs + community->ie_offset + gpp * 4);
955 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
956 	}
957 }
958 
959 static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
960 {
961 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
962 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
963 	const struct intel_community *community;
964 	const struct intel_padgroup *padgrp;
965 	int pin;
966 
967 	pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
968 	if (pin >= 0) {
969 		unsigned gpp, gpp_offset;
970 		unsigned long flags;
971 		void __iomem *reg;
972 		u32 value;
973 
974 		gpp = padgrp->reg_num;
975 		gpp_offset = padgroup_offset(padgrp, pin);
976 
977 		reg = community->regs + community->ie_offset + gpp * 4;
978 
979 		raw_spin_lock_irqsave(&pctrl->lock, flags);
980 		value = readl(reg);
981 		if (mask)
982 			value &= ~BIT(gpp_offset);
983 		else
984 			value |= BIT(gpp_offset);
985 		writel(value, reg);
986 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
987 	}
988 }
989 
990 static void intel_gpio_irq_mask(struct irq_data *d)
991 {
992 	intel_gpio_irq_mask_unmask(d, true);
993 }
994 
995 static void intel_gpio_irq_unmask(struct irq_data *d)
996 {
997 	intel_gpio_irq_mask_unmask(d, false);
998 }
999 
1000 static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
1001 {
1002 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1003 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1004 	unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1005 	unsigned long flags;
1006 	void __iomem *reg;
1007 	u32 value;
1008 
1009 	reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1010 	if (!reg)
1011 		return -EINVAL;
1012 
1013 	/*
1014 	 * If the pin is in ACPI mode it is still usable as a GPIO but it
1015 	 * cannot be used as IRQ because GPI_IS status bit will not be
1016 	 * updated by the host controller hardware.
1017 	 */
1018 	if (intel_pad_acpi_mode(pctrl, pin)) {
1019 		dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1020 		return -EPERM;
1021 	}
1022 
1023 	raw_spin_lock_irqsave(&pctrl->lock, flags);
1024 
1025 	intel_gpio_set_gpio_mode(reg);
1026 
1027 	value = readl(reg);
1028 
1029 	value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1030 
1031 	if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1032 		value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1033 	} else if (type & IRQ_TYPE_EDGE_FALLING) {
1034 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1035 		value |= PADCFG0_RXINV;
1036 	} else if (type & IRQ_TYPE_EDGE_RISING) {
1037 		value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1038 	} else if (type & IRQ_TYPE_LEVEL_MASK) {
1039 		if (type & IRQ_TYPE_LEVEL_LOW)
1040 			value |= PADCFG0_RXINV;
1041 	} else {
1042 		value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1043 	}
1044 
1045 	writel(value, reg);
1046 
1047 	if (type & IRQ_TYPE_EDGE_BOTH)
1048 		irq_set_handler_locked(d, handle_edge_irq);
1049 	else if (type & IRQ_TYPE_LEVEL_MASK)
1050 		irq_set_handler_locked(d, handle_level_irq);
1051 
1052 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1053 
1054 	return 0;
1055 }
1056 
1057 static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1058 {
1059 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1060 	struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1061 	unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
1062 
1063 	if (on)
1064 		enable_irq_wake(pctrl->irq);
1065 	else
1066 		disable_irq_wake(pctrl->irq);
1067 
1068 	dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1069 	return 0;
1070 }
1071 
1072 static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1073 	const struct intel_community *community)
1074 {
1075 	struct gpio_chip *gc = &pctrl->chip;
1076 	irqreturn_t ret = IRQ_NONE;
1077 	int gpp;
1078 
1079 	for (gpp = 0; gpp < community->ngpps; gpp++) {
1080 		const struct intel_padgroup *padgrp = &community->gpps[gpp];
1081 		unsigned long pending, enabled, gpp_offset;
1082 
1083 		pending = readl(community->regs + community->is_offset +
1084 				padgrp->reg_num * 4);
1085 		enabled = readl(community->regs + community->ie_offset +
1086 				padgrp->reg_num * 4);
1087 
1088 		/* Only interrupts that are enabled */
1089 		pending &= enabled;
1090 
1091 		for_each_set_bit(gpp_offset, &pending, padgrp->size) {
1092 			unsigned irq;
1093 
1094 			irq = irq_find_mapping(gc->irq.domain,
1095 					       padgrp->gpio_base + gpp_offset);
1096 			generic_handle_irq(irq);
1097 
1098 			ret |= IRQ_HANDLED;
1099 		}
1100 	}
1101 
1102 	return ret;
1103 }
1104 
1105 static irqreturn_t intel_gpio_irq(int irq, void *data)
1106 {
1107 	const struct intel_community *community;
1108 	struct intel_pinctrl *pctrl = data;
1109 	irqreturn_t ret = IRQ_NONE;
1110 	int i;
1111 
1112 	/* Need to check all communities for pending interrupts */
1113 	for (i = 0; i < pctrl->ncommunities; i++) {
1114 		community = &pctrl->communities[i];
1115 		ret |= intel_gpio_community_irq_handler(pctrl, community);
1116 	}
1117 
1118 	return ret;
1119 }
1120 
1121 static struct irq_chip intel_gpio_irqchip = {
1122 	.name = "intel-gpio",
1123 	.irq_request_resources = intel_gpio_irq_reqres,
1124 	.irq_release_resources = intel_gpio_irq_relres,
1125 	.irq_enable = intel_gpio_irq_enable,
1126 	.irq_ack = intel_gpio_irq_ack,
1127 	.irq_mask = intel_gpio_irq_mask,
1128 	.irq_unmask = intel_gpio_irq_unmask,
1129 	.irq_set_type = intel_gpio_irq_type,
1130 	.irq_set_wake = intel_gpio_irq_wake,
1131 	.flags = IRQCHIP_MASK_ON_SUSPEND,
1132 };
1133 
1134 static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1135 				     const struct intel_community *community)
1136 {
1137 	int ret = 0, i;
1138 
1139 	for (i = 0; i < community->ngpps; i++) {
1140 		const struct intel_padgroup *gpp = &community->gpps[i];
1141 
1142 		if (gpp->gpio_base < 0)
1143 			continue;
1144 
1145 		ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1146 					     gpp->gpio_base, gpp->base,
1147 					     gpp->size);
1148 		if (ret)
1149 			return ret;
1150 	}
1151 
1152 	return ret;
1153 }
1154 
1155 static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1156 {
1157 	const struct intel_community *community;
1158 	unsigned ngpio = 0;
1159 	int i, j;
1160 
1161 	for (i = 0; i < pctrl->ncommunities; i++) {
1162 		community = &pctrl->communities[i];
1163 		for (j = 0; j < community->ngpps; j++) {
1164 			const struct intel_padgroup *gpp = &community->gpps[j];
1165 
1166 			if (gpp->gpio_base < 0)
1167 				continue;
1168 
1169 			if (gpp->gpio_base + gpp->size > ngpio)
1170 				ngpio = gpp->gpio_base + gpp->size;
1171 		}
1172 	}
1173 
1174 	return ngpio;
1175 }
1176 
1177 static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1178 {
1179 	int ret, i;
1180 
1181 	pctrl->chip = intel_gpio_chip;
1182 
1183 	pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
1184 	pctrl->chip.label = dev_name(pctrl->dev);
1185 	pctrl->chip.parent = pctrl->dev;
1186 	pctrl->chip.base = -1;
1187 	pctrl->irq = irq;
1188 
1189 	ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
1190 	if (ret) {
1191 		dev_err(pctrl->dev, "failed to register gpiochip\n");
1192 		return ret;
1193 	}
1194 
1195 	for (i = 0; i < pctrl->ncommunities; i++) {
1196 		struct intel_community *community = &pctrl->communities[i];
1197 
1198 		ret = intel_gpio_add_pin_ranges(pctrl, community);
1199 		if (ret) {
1200 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1201 			return ret;
1202 		}
1203 	}
1204 
1205 	/*
1206 	 * We need to request the interrupt here (instead of providing chip
1207 	 * to the irq directly) because on some platforms several GPIO
1208 	 * controllers share the same interrupt line.
1209 	 */
1210 	ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1211 			       IRQF_SHARED | IRQF_NO_THREAD,
1212 			       dev_name(pctrl->dev), pctrl);
1213 	if (ret) {
1214 		dev_err(pctrl->dev, "failed to request interrupt\n");
1215 		return ret;
1216 	}
1217 
1218 	ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
1219 				   handle_bad_irq, IRQ_TYPE_NONE);
1220 	if (ret) {
1221 		dev_err(pctrl->dev, "failed to add irqchip\n");
1222 		return ret;
1223 	}
1224 
1225 	gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
1226 				     NULL);
1227 	return 0;
1228 }
1229 
1230 static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1231 				       struct intel_community *community)
1232 {
1233 	struct intel_padgroup *gpps;
1234 	unsigned npins = community->npins;
1235 	unsigned padown_num = 0;
1236 	size_t ngpps, i;
1237 
1238 	if (community->gpps)
1239 		ngpps = community->ngpps;
1240 	else
1241 		ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1242 
1243 	gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1244 	if (!gpps)
1245 		return -ENOMEM;
1246 
1247 	for (i = 0; i < ngpps; i++) {
1248 		if (community->gpps) {
1249 			gpps[i] = community->gpps[i];
1250 		} else {
1251 			unsigned gpp_size = community->gpp_size;
1252 
1253 			gpps[i].reg_num = i;
1254 			gpps[i].base = community->pin_base + i * gpp_size;
1255 			gpps[i].size = min(gpp_size, npins);
1256 			npins -= gpps[i].size;
1257 		}
1258 
1259 		if (gpps[i].size > 32)
1260 			return -EINVAL;
1261 
1262 		if (!gpps[i].gpio_base)
1263 			gpps[i].gpio_base = gpps[i].base;
1264 
1265 		gpps[i].padown_num = padown_num;
1266 
1267 		/*
1268 		 * In older hardware the number of padown registers per
1269 		 * group is fixed regardless of the group size.
1270 		 */
1271 		if (community->gpp_num_padown_regs)
1272 			padown_num += community->gpp_num_padown_regs;
1273 		else
1274 			padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1275 	}
1276 
1277 	community->ngpps = ngpps;
1278 	community->gpps = gpps;
1279 
1280 	return 0;
1281 }
1282 
1283 static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1284 {
1285 #ifdef CONFIG_PM_SLEEP
1286 	const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1287 	struct intel_community_context *communities;
1288 	struct intel_pad_context *pads;
1289 	int i;
1290 
1291 	pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1292 	if (!pads)
1293 		return -ENOMEM;
1294 
1295 	communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1296 				   sizeof(*communities), GFP_KERNEL);
1297 	if (!communities)
1298 		return -ENOMEM;
1299 
1300 
1301 	for (i = 0; i < pctrl->ncommunities; i++) {
1302 		struct intel_community *community = &pctrl->communities[i];
1303 		u32 *intmask;
1304 
1305 		intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1306 				       sizeof(*intmask), GFP_KERNEL);
1307 		if (!intmask)
1308 			return -ENOMEM;
1309 
1310 		communities[i].intmask = intmask;
1311 	}
1312 
1313 	pctrl->context.pads = pads;
1314 	pctrl->context.communities = communities;
1315 #endif
1316 
1317 	return 0;
1318 }
1319 
1320 int intel_pinctrl_probe(struct platform_device *pdev,
1321 			const struct intel_pinctrl_soc_data *soc_data)
1322 {
1323 	struct intel_pinctrl *pctrl;
1324 	int i, ret, irq;
1325 
1326 	if (!soc_data)
1327 		return -EINVAL;
1328 
1329 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1330 	if (!pctrl)
1331 		return -ENOMEM;
1332 
1333 	pctrl->dev = &pdev->dev;
1334 	pctrl->soc = soc_data;
1335 	raw_spin_lock_init(&pctrl->lock);
1336 
1337 	/*
1338 	 * Make a copy of the communities which we can use to hold pointers
1339 	 * to the registers.
1340 	 */
1341 	pctrl->ncommunities = pctrl->soc->ncommunities;
1342 	pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1343 				  sizeof(*pctrl->communities), GFP_KERNEL);
1344 	if (!pctrl->communities)
1345 		return -ENOMEM;
1346 
1347 	for (i = 0; i < pctrl->ncommunities; i++) {
1348 		struct intel_community *community = &pctrl->communities[i];
1349 		struct resource *res;
1350 		void __iomem *regs;
1351 		u32 padbar;
1352 
1353 		*community = pctrl->soc->communities[i];
1354 
1355 		res = platform_get_resource(pdev, IORESOURCE_MEM,
1356 					    community->barno);
1357 		regs = devm_ioremap_resource(&pdev->dev, res);
1358 		if (IS_ERR(regs))
1359 			return PTR_ERR(regs);
1360 
1361 		/*
1362 		 * Determine community features based on the revision if
1363 		 * not specified already.
1364 		 */
1365 		if (!community->features) {
1366 			u32 rev;
1367 
1368 			rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
1369 			if (rev >= 0x94) {
1370 				community->features |= PINCTRL_FEATURE_DEBOUNCE;
1371 				community->features |= PINCTRL_FEATURE_1K_PD;
1372 			}
1373 		}
1374 
1375 		/* Read offset of the pad configuration registers */
1376 		padbar = readl(regs + PADBAR);
1377 
1378 		community->regs = regs;
1379 		community->pad_regs = regs + padbar;
1380 
1381 		if (!community->is_offset)
1382 			community->is_offset = GPI_IS;
1383 
1384 		ret = intel_pinctrl_add_padgroups(pctrl, community);
1385 		if (ret)
1386 			return ret;
1387 	}
1388 
1389 	irq = platform_get_irq(pdev, 0);
1390 	if (irq < 0) {
1391 		dev_err(&pdev->dev, "failed to get interrupt number\n");
1392 		return irq;
1393 	}
1394 
1395 	ret = intel_pinctrl_pm_init(pctrl);
1396 	if (ret)
1397 		return ret;
1398 
1399 	pctrl->pctldesc = intel_pinctrl_desc;
1400 	pctrl->pctldesc.name = dev_name(&pdev->dev);
1401 	pctrl->pctldesc.pins = pctrl->soc->pins;
1402 	pctrl->pctldesc.npins = pctrl->soc->npins;
1403 
1404 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1405 					       pctrl);
1406 	if (IS_ERR(pctrl->pctldev)) {
1407 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1408 		return PTR_ERR(pctrl->pctldev);
1409 	}
1410 
1411 	ret = intel_gpio_probe(pctrl, irq);
1412 	if (ret)
1413 		return ret;
1414 
1415 	platform_set_drvdata(pdev, pctrl);
1416 
1417 	return 0;
1418 }
1419 EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1420 
1421 int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1422 {
1423 	const struct intel_pinctrl_soc_data *data;
1424 
1425 	data = device_get_match_data(&pdev->dev);
1426 	return intel_pinctrl_probe(pdev, data);
1427 }
1428 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
1429 
1430 int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1431 {
1432 	const struct intel_pinctrl_soc_data *data = NULL;
1433 	const struct intel_pinctrl_soc_data **table;
1434 	struct acpi_device *adev;
1435 	unsigned int i;
1436 
1437 	adev = ACPI_COMPANION(&pdev->dev);
1438 	if (adev) {
1439 		const void *match = device_get_match_data(&pdev->dev);
1440 
1441 		table = (const struct intel_pinctrl_soc_data **)match;
1442 		for (i = 0; table[i]; i++) {
1443 			if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1444 				data = table[i];
1445 				break;
1446 			}
1447 		}
1448 	} else {
1449 		const struct platform_device_id *id;
1450 
1451 		id = platform_get_device_id(pdev);
1452 		if (!id)
1453 			return -ENODEV;
1454 
1455 		table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1456 		data = table[pdev->id];
1457 	}
1458 	if (!data)
1459 		return -ENODEV;
1460 
1461 	return intel_pinctrl_probe(pdev, data);
1462 }
1463 EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1464 
1465 #ifdef CONFIG_PM_SLEEP
1466 static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1467 {
1468 	const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1469 
1470 	if (!pd || !intel_pad_usable(pctrl, pin))
1471 		return false;
1472 
1473 	/*
1474 	 * Only restore the pin if it is actually in use by the kernel (or
1475 	 * by userspace). It is possible that some pins are used by the
1476 	 * BIOS during resume and those are not always locked down so leave
1477 	 * them alone.
1478 	 */
1479 	if (pd->mux_owner || pd->gpio_owner ||
1480 	    gpiochip_line_is_irq(&pctrl->chip, pin))
1481 		return true;
1482 
1483 	return false;
1484 }
1485 
1486 int intel_pinctrl_suspend(struct device *dev)
1487 {
1488 	struct platform_device *pdev = to_platform_device(dev);
1489 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1490 	struct intel_community_context *communities;
1491 	struct intel_pad_context *pads;
1492 	int i;
1493 
1494 	pads = pctrl->context.pads;
1495 	for (i = 0; i < pctrl->soc->npins; i++) {
1496 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1497 		void __iomem *padcfg;
1498 		u32 val;
1499 
1500 		if (!intel_pinctrl_should_save(pctrl, desc->number))
1501 			continue;
1502 
1503 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1504 		pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1505 		val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1506 		pads[i].padcfg1 = val;
1507 
1508 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1509 		if (padcfg)
1510 			pads[i].padcfg2 = readl(padcfg);
1511 	}
1512 
1513 	communities = pctrl->context.communities;
1514 	for (i = 0; i < pctrl->ncommunities; i++) {
1515 		struct intel_community *community = &pctrl->communities[i];
1516 		void __iomem *base;
1517 		unsigned gpp;
1518 
1519 		base = community->regs + community->ie_offset;
1520 		for (gpp = 0; gpp < community->ngpps; gpp++)
1521 			communities[i].intmask[gpp] = readl(base + gpp * 4);
1522 	}
1523 
1524 	return 0;
1525 }
1526 EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1527 
1528 static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1529 {
1530 	size_t i;
1531 
1532 	for (i = 0; i < pctrl->ncommunities; i++) {
1533 		const struct intel_community *community;
1534 		void __iomem *base;
1535 		unsigned gpp;
1536 
1537 		community = &pctrl->communities[i];
1538 		base = community->regs;
1539 
1540 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1541 			/* Mask and clear all interrupts */
1542 			writel(0, base + community->ie_offset + gpp * 4);
1543 			writel(0xffff, base + community->is_offset + gpp * 4);
1544 		}
1545 	}
1546 }
1547 
1548 int intel_pinctrl_resume(struct device *dev)
1549 {
1550 	struct platform_device *pdev = to_platform_device(dev);
1551 	struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1552 	const struct intel_community_context *communities;
1553 	const struct intel_pad_context *pads;
1554 	int i;
1555 
1556 	/* Mask all interrupts */
1557 	intel_gpio_irq_init(pctrl);
1558 
1559 	pads = pctrl->context.pads;
1560 	for (i = 0; i < pctrl->soc->npins; i++) {
1561 		const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1562 		void __iomem *padcfg;
1563 		u32 val;
1564 
1565 		if (!intel_pinctrl_should_save(pctrl, desc->number))
1566 			continue;
1567 
1568 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1569 		val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1570 		if (val != pads[i].padcfg0) {
1571 			writel(pads[i].padcfg0, padcfg);
1572 			dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1573 				desc->number, readl(padcfg));
1574 		}
1575 
1576 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1577 		val = readl(padcfg);
1578 		if (val != pads[i].padcfg1) {
1579 			writel(pads[i].padcfg1, padcfg);
1580 			dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1581 				desc->number, readl(padcfg));
1582 		}
1583 
1584 		padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1585 		if (padcfg) {
1586 			val = readl(padcfg);
1587 			if (val != pads[i].padcfg2) {
1588 				writel(pads[i].padcfg2, padcfg);
1589 				dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1590 					desc->number, readl(padcfg));
1591 			}
1592 		}
1593 	}
1594 
1595 	communities = pctrl->context.communities;
1596 	for (i = 0; i < pctrl->ncommunities; i++) {
1597 		struct intel_community *community = &pctrl->communities[i];
1598 		void __iomem *base;
1599 		unsigned gpp;
1600 
1601 		base = community->regs + community->ie_offset;
1602 		for (gpp = 0; gpp < community->ngpps; gpp++) {
1603 			writel(communities[i].intmask[gpp], base + gpp * 4);
1604 			dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1605 				readl(base + gpp * 4));
1606 		}
1607 	}
1608 
1609 	return 0;
1610 }
1611 EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1612 #endif
1613 
1614 MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1615 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1616 MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1617 MODULE_LICENSE("GPL v2");
1618