blob: 44740580934044d479108de32f30f47a1b74517e [file] [log] [blame]
Mika Westerberg7981c0012015-03-30 17:31:49 +03001/*
2 * Intel pinctrl/GPIO core driver.
3 *
4 * Copyright (C) 2015, Intel Corporation
5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
Mika Westerberg193b40c2015-10-21 13:08:43 +030014#include <linux/interrupt.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030015#include <linux/gpio/driver.h>
16#include <linux/platform_device.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030017#include <linux/pinctrl/pinctrl.h>
18#include <linux/pinctrl/pinmux.h>
19#include <linux/pinctrl/pinconf.h>
20#include <linux/pinctrl/pinconf-generic.h>
21
Mika Westerbergc538b942016-10-10 16:39:31 +030022#include "../core.h"
Mika Westerberg7981c0012015-03-30 17:31:49 +030023#include "pinctrl-intel.h"
24
Mika Westerberg7981c0012015-03-30 17:31:49 +030025/* Offset from regs */
26#define PADBAR 0x00c
27#define GPI_IS 0x100
28#define GPI_GPE_STS 0x140
29#define GPI_GPE_EN 0x160
30
31#define PADOWN_BITS 4
32#define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
33#define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
Qipeng Zha99a735b2015-11-30 19:20:16 +080034#define PADOWN_GPP(p) ((p) / 8)
Mika Westerberg7981c0012015-03-30 17:31:49 +030035
36/* Offset from pad_regs */
37#define PADCFG0 0x000
38#define PADCFG0_RXEVCFG_SHIFT 25
39#define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
40#define PADCFG0_RXEVCFG_LEVEL 0
41#define PADCFG0_RXEVCFG_EDGE 1
42#define PADCFG0_RXEVCFG_DISABLED 2
43#define PADCFG0_RXEVCFG_EDGE_BOTH 3
44#define PADCFG0_RXINV BIT(23)
45#define PADCFG0_GPIROUTIOXAPIC BIT(20)
46#define PADCFG0_GPIROUTSCI BIT(19)
47#define PADCFG0_GPIROUTSMI BIT(18)
48#define PADCFG0_GPIROUTNMI BIT(17)
49#define PADCFG0_PMODE_SHIFT 10
50#define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
51#define PADCFG0_GPIORXDIS BIT(9)
52#define PADCFG0_GPIOTXDIS BIT(8)
53#define PADCFG0_GPIORXSTATE BIT(1)
54#define PADCFG0_GPIOTXSTATE BIT(0)
55
56#define PADCFG1 0x004
57#define PADCFG1_TERM_UP BIT(13)
58#define PADCFG1_TERM_SHIFT 10
59#define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
60#define PADCFG1_TERM_20K 4
61#define PADCFG1_TERM_2K 3
62#define PADCFG1_TERM_5K 2
63#define PADCFG1_TERM_1K 1
64
65struct intel_pad_context {
66 u32 padcfg0;
67 u32 padcfg1;
68};
69
70struct intel_community_context {
71 u32 *intmask;
72};
73
74struct intel_pinctrl_context {
75 struct intel_pad_context *pads;
76 struct intel_community_context *communities;
77};
78
79/**
80 * struct intel_pinctrl - Intel pinctrl private structure
81 * @dev: Pointer to the device structure
82 * @lock: Lock to serialize register access
83 * @pctldesc: Pin controller description
84 * @pctldev: Pointer to the pin controller device
85 * @chip: GPIO chip in this pin controller
86 * @soc: SoC/PCH specific pin configuration data
87 * @communities: All communities in this pin controller
88 * @ncommunities: Number of communities in this pin controller
89 * @context: Configuration saved over system sleep
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -070090 * @irq: pinctrl/GPIO chip irq number
Mika Westerberg7981c0012015-03-30 17:31:49 +030091 */
92struct intel_pinctrl {
93 struct device *dev;
Mika Westerberg27d90982016-06-16 11:25:36 +030094 raw_spinlock_t lock;
Mika Westerberg7981c0012015-03-30 17:31:49 +030095 struct pinctrl_desc pctldesc;
96 struct pinctrl_dev *pctldev;
97 struct gpio_chip chip;
98 const struct intel_pinctrl_soc_data *soc;
99 struct intel_community *communities;
100 size_t ncommunities;
101 struct intel_pinctrl_context context;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700102 int irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300103};
104
Mika Westerberg7981c0012015-03-30 17:31:49 +0300105#define pin_to_padno(c, p) ((p) - (c)->pin_base)
106
107static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
108 unsigned pin)
109{
110 struct intel_community *community;
111 int i;
112
113 for (i = 0; i < pctrl->ncommunities; i++) {
114 community = &pctrl->communities[i];
115 if (pin >= community->pin_base &&
116 pin < community->pin_base + community->npins)
117 return community;
118 }
119
120 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
121 return NULL;
122}
123
124static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
125 unsigned reg)
126{
127 const struct intel_community *community;
128 unsigned padno;
129
130 community = intel_get_community(pctrl, pin);
131 if (!community)
132 return NULL;
133
134 padno = pin_to_padno(community, pin);
135 return community->pad_regs + reg + padno * 8;
136}
137
138static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
139{
140 const struct intel_community *community;
Qipeng Zha99a735b2015-11-30 19:20:16 +0800141 unsigned padno, gpp, offset, group;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300142 void __iomem *padown;
143
144 community = intel_get_community(pctrl, pin);
145 if (!community)
146 return false;
147 if (!community->padown_offset)
148 return true;
149
150 padno = pin_to_padno(community, pin);
Qipeng Zha99a735b2015-11-30 19:20:16 +0800151 group = padno / community->gpp_size;
152 gpp = PADOWN_GPP(padno % community->gpp_size);
153 offset = community->padown_offset + 0x10 * group + gpp * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300154 padown = community->regs + offset;
155
156 return !(readl(padown) & PADOWN_MASK(padno));
157}
158
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300159static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300160{
161 const struct intel_community *community;
162 unsigned padno, gpp, offset;
163 void __iomem *hostown;
164
165 community = intel_get_community(pctrl, pin);
166 if (!community)
167 return true;
168 if (!community->hostown_offset)
169 return false;
170
171 padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800172 gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300173 offset = community->hostown_offset + gpp * 4;
174 hostown = community->regs + offset;
175
Qipeng Zha618a919b2015-11-26 01:09:51 +0800176 return !(readl(hostown) & BIT(padno % community->gpp_size));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300177}
178
179static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
180{
181 struct intel_community *community;
182 unsigned padno, gpp, offset;
183 u32 value;
184
185 community = intel_get_community(pctrl, pin);
186 if (!community)
187 return true;
188 if (!community->padcfglock_offset)
189 return false;
190
191 padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800192 gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300193
194 /*
195 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
196 * the pad is considered unlocked. Any other case means that it is
197 * either fully or partially locked and we don't touch it.
198 */
199 offset = community->padcfglock_offset + gpp * 8;
200 value = readl(community->regs + offset);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800201 if (value & BIT(pin % community->gpp_size))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300202 return true;
203
204 offset = community->padcfglock_offset + 4 + gpp * 8;
205 value = readl(community->regs + offset);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800206 if (value & BIT(pin % community->gpp_size))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300207 return true;
208
209 return false;
210}
211
212static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
213{
214 return intel_pad_owned_by_host(pctrl, pin) &&
Mika Westerberg7981c0012015-03-30 17:31:49 +0300215 !intel_pad_locked(pctrl, pin);
216}
217
218static int intel_get_groups_count(struct pinctrl_dev *pctldev)
219{
220 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
221
222 return pctrl->soc->ngroups;
223}
224
225static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
226 unsigned group)
227{
228 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
229
230 return pctrl->soc->groups[group].name;
231}
232
233static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
234 const unsigned **pins, unsigned *npins)
235{
236 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
237
238 *pins = pctrl->soc->groups[group].pins;
239 *npins = pctrl->soc->groups[group].npins;
240 return 0;
241}
242
243static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
244 unsigned pin)
245{
246 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
247 u32 cfg0, cfg1, mode;
248 bool locked, acpi;
249
250 if (!intel_pad_owned_by_host(pctrl, pin)) {
251 seq_puts(s, "not available");
252 return;
253 }
254
255 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
256 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
257
258 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
259 if (!mode)
260 seq_puts(s, "GPIO ");
261 else
262 seq_printf(s, "mode %d ", mode);
263
264 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
265
266 locked = intel_pad_locked(pctrl, pin);
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300267 acpi = intel_pad_acpi_mode(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300268
269 if (locked || acpi) {
270 seq_puts(s, " [");
271 if (locked) {
272 seq_puts(s, "LOCKED");
273 if (acpi)
274 seq_puts(s, ", ");
275 }
276 if (acpi)
277 seq_puts(s, "ACPI");
278 seq_puts(s, "]");
279 }
280}
281
282static const struct pinctrl_ops intel_pinctrl_ops = {
283 .get_groups_count = intel_get_groups_count,
284 .get_group_name = intel_get_group_name,
285 .get_group_pins = intel_get_group_pins,
286 .pin_dbg_show = intel_pin_dbg_show,
287};
288
289static int intel_get_functions_count(struct pinctrl_dev *pctldev)
290{
291 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
292
293 return pctrl->soc->nfunctions;
294}
295
296static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
297 unsigned function)
298{
299 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
300
301 return pctrl->soc->functions[function].name;
302}
303
304static int intel_get_function_groups(struct pinctrl_dev *pctldev,
305 unsigned function,
306 const char * const **groups,
307 unsigned * const ngroups)
308{
309 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
310
311 *groups = pctrl->soc->functions[function].groups;
312 *ngroups = pctrl->soc->functions[function].ngroups;
313 return 0;
314}
315
316static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
317 unsigned group)
318{
319 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
320 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
321 unsigned long flags;
322 int i;
323
Mika Westerberg27d90982016-06-16 11:25:36 +0300324 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300325
326 /*
327 * All pins in the groups needs to be accessible and writable
328 * before we can enable the mux for this group.
329 */
330 for (i = 0; i < grp->npins; i++) {
331 if (!intel_pad_usable(pctrl, grp->pins[i])) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300332 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300333 return -EBUSY;
334 }
335 }
336
337 /* Now enable the mux setting for each pin in the group */
338 for (i = 0; i < grp->npins; i++) {
339 void __iomem *padcfg0;
340 u32 value;
341
342 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
343 value = readl(padcfg0);
344
345 value &= ~PADCFG0_PMODE_MASK;
346 value |= grp->mode << PADCFG0_PMODE_SHIFT;
347
348 writel(value, padcfg0);
349 }
350
Mika Westerberg27d90982016-06-16 11:25:36 +0300351 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300352
353 return 0;
354}
355
356static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
357 struct pinctrl_gpio_range *range,
358 unsigned pin)
359{
360 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
361 void __iomem *padcfg0;
362 unsigned long flags;
363 u32 value;
364
Mika Westerberg27d90982016-06-16 11:25:36 +0300365 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300366
367 if (!intel_pad_usable(pctrl, pin)) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300368 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300369 return -EBUSY;
370 }
371
372 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
373 /* Put the pad into GPIO mode */
374 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
375 /* Disable SCI/SMI/NMI generation */
376 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
377 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
378 /* Disable TX buffer and enable RX (this will be input) */
379 value &= ~PADCFG0_GPIORXDIS;
380 value |= PADCFG0_GPIOTXDIS;
381 writel(value, padcfg0);
382
Mika Westerberg27d90982016-06-16 11:25:36 +0300383 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300384
385 return 0;
386}
387
388static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
389 struct pinctrl_gpio_range *range,
390 unsigned pin, bool input)
391{
392 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
393 void __iomem *padcfg0;
394 unsigned long flags;
395 u32 value;
396
Mika Westerberg27d90982016-06-16 11:25:36 +0300397 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300398
399 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
400
401 value = readl(padcfg0);
402 if (input)
403 value |= PADCFG0_GPIOTXDIS;
404 else
405 value &= ~PADCFG0_GPIOTXDIS;
406 writel(value, padcfg0);
407
Mika Westerberg27d90982016-06-16 11:25:36 +0300408 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300409
410 return 0;
411}
412
413static const struct pinmux_ops intel_pinmux_ops = {
414 .get_functions_count = intel_get_functions_count,
415 .get_function_name = intel_get_function_name,
416 .get_function_groups = intel_get_function_groups,
417 .set_mux = intel_pinmux_set_mux,
418 .gpio_request_enable = intel_gpio_request_enable,
419 .gpio_set_direction = intel_gpio_set_direction,
420};
421
422static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
423 unsigned long *config)
424{
425 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
426 enum pin_config_param param = pinconf_to_config_param(*config);
427 u32 value, term;
428 u16 arg = 0;
429
430 if (!intel_pad_owned_by_host(pctrl, pin))
431 return -ENOTSUPP;
432
433 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
434 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
435
436 switch (param) {
437 case PIN_CONFIG_BIAS_DISABLE:
438 if (term)
439 return -EINVAL;
440 break;
441
442 case PIN_CONFIG_BIAS_PULL_UP:
443 if (!term || !(value & PADCFG1_TERM_UP))
444 return -EINVAL;
445
446 switch (term) {
447 case PADCFG1_TERM_1K:
448 arg = 1000;
449 break;
450 case PADCFG1_TERM_2K:
451 arg = 2000;
452 break;
453 case PADCFG1_TERM_5K:
454 arg = 5000;
455 break;
456 case PADCFG1_TERM_20K:
457 arg = 20000;
458 break;
459 }
460
461 break;
462
463 case PIN_CONFIG_BIAS_PULL_DOWN:
464 if (!term || value & PADCFG1_TERM_UP)
465 return -EINVAL;
466
467 switch (term) {
468 case PADCFG1_TERM_5K:
469 arg = 5000;
470 break;
471 case PADCFG1_TERM_20K:
472 arg = 20000;
473 break;
474 }
475
476 break;
477
478 default:
479 return -ENOTSUPP;
480 }
481
482 *config = pinconf_to_config_packed(param, arg);
483 return 0;
484}
485
486static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
487 unsigned long config)
488{
489 unsigned param = pinconf_to_config_param(config);
490 unsigned arg = pinconf_to_config_argument(config);
491 void __iomem *padcfg1;
492 unsigned long flags;
493 int ret = 0;
494 u32 value;
495
Mika Westerberg27d90982016-06-16 11:25:36 +0300496 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300497
498 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
499 value = readl(padcfg1);
500
501 switch (param) {
502 case PIN_CONFIG_BIAS_DISABLE:
503 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
504 break;
505
506 case PIN_CONFIG_BIAS_PULL_UP:
507 value &= ~PADCFG1_TERM_MASK;
508
509 value |= PADCFG1_TERM_UP;
510
511 switch (arg) {
512 case 20000:
513 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
514 break;
515 case 5000:
516 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
517 break;
518 case 2000:
519 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
520 break;
521 case 1000:
522 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
523 break;
524 default:
525 ret = -EINVAL;
526 }
527
528 break;
529
530 case PIN_CONFIG_BIAS_PULL_DOWN:
531 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
532
533 switch (arg) {
534 case 20000:
535 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
536 break;
537 case 5000:
538 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
539 break;
540 default:
541 ret = -EINVAL;
542 }
543
544 break;
545 }
546
547 if (!ret)
548 writel(value, padcfg1);
549
Mika Westerberg27d90982016-06-16 11:25:36 +0300550 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300551
552 return ret;
553}
554
555static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
556 unsigned long *configs, unsigned nconfigs)
557{
558 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
559 int i, ret;
560
561 if (!intel_pad_usable(pctrl, pin))
562 return -ENOTSUPP;
563
564 for (i = 0; i < nconfigs; i++) {
565 switch (pinconf_to_config_param(configs[i])) {
566 case PIN_CONFIG_BIAS_DISABLE:
567 case PIN_CONFIG_BIAS_PULL_UP:
568 case PIN_CONFIG_BIAS_PULL_DOWN:
569 ret = intel_config_set_pull(pctrl, pin, configs[i]);
570 if (ret)
571 return ret;
572 break;
573
574 default:
575 return -ENOTSUPP;
576 }
577 }
578
579 return 0;
580}
581
582static const struct pinconf_ops intel_pinconf_ops = {
583 .is_generic = true,
584 .pin_config_get = intel_config_get,
585 .pin_config_set = intel_config_set,
586};
587
588static const struct pinctrl_desc intel_pinctrl_desc = {
589 .pctlops = &intel_pinctrl_ops,
590 .pmxops = &intel_pinmux_ops,
591 .confops = &intel_pinconf_ops,
592 .owner = THIS_MODULE,
593};
594
Mika Westerberg7981c0012015-03-30 17:31:49 +0300595static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
596{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100597 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300598 void __iomem *reg;
599
600 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
601 if (!reg)
602 return -EINVAL;
603
604 return !!(readl(reg) & PADCFG0_GPIORXSTATE);
605}
606
607static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
608{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100609 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300610 void __iomem *reg;
611
612 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
613 if (reg) {
614 unsigned long flags;
615 u32 padcfg0;
616
Mika Westerberg27d90982016-06-16 11:25:36 +0300617 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300618 padcfg0 = readl(reg);
619 if (value)
620 padcfg0 |= PADCFG0_GPIOTXSTATE;
621 else
622 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
623 writel(padcfg0, reg);
Mika Westerberg27d90982016-06-16 11:25:36 +0300624 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300625 }
626}
627
628static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
629{
630 return pinctrl_gpio_direction_input(chip->base + offset);
631}
632
633static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
634 int value)
635{
636 intel_gpio_set(chip, offset, value);
637 return pinctrl_gpio_direction_output(chip->base + offset);
638}
639
640static const struct gpio_chip intel_gpio_chip = {
641 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200642 .request = gpiochip_generic_request,
643 .free = gpiochip_generic_free,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300644 .direction_input = intel_gpio_direction_input,
645 .direction_output = intel_gpio_direction_output,
646 .get = intel_gpio_get,
647 .set = intel_gpio_set,
648};
649
650static void intel_gpio_irq_ack(struct irq_data *d)
651{
652 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100653 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300654 const struct intel_community *community;
655 unsigned pin = irqd_to_hwirq(d);
656
Mika Westerberg27d90982016-06-16 11:25:36 +0300657 raw_spin_lock(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300658
659 community = intel_get_community(pctrl, pin);
660 if (community) {
661 unsigned padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800662 unsigned gpp_offset = padno % community->gpp_size;
663 unsigned gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300664
665 writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
666 }
667
Mika Westerberg27d90982016-06-16 11:25:36 +0300668 raw_spin_unlock(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300669}
670
Qi Zhenga939bb52016-03-17 02:15:26 +0800671static void intel_gpio_irq_enable(struct irq_data *d)
672{
673 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
674 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
675 const struct intel_community *community;
676 unsigned pin = irqd_to_hwirq(d);
677 unsigned long flags;
678
Mika Westerberg27d90982016-06-16 11:25:36 +0300679 raw_spin_lock_irqsave(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800680
681 community = intel_get_community(pctrl, pin);
682 if (community) {
683 unsigned padno = pin_to_padno(community, pin);
684 unsigned gpp_size = community->gpp_size;
685 unsigned gpp_offset = padno % gpp_size;
686 unsigned gpp = padno / gpp_size;
687 u32 value;
688
689 /* Clear interrupt status first to avoid unexpected interrupt */
690 writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
691
692 value = readl(community->regs + community->ie_offset + gpp * 4);
693 value |= BIT(gpp_offset);
694 writel(value, community->regs + community->ie_offset + gpp * 4);
695 }
696
Mika Westerberg27d90982016-06-16 11:25:36 +0300697 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800698}
699
Mika Westerberg7981c0012015-03-30 17:31:49 +0300700static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
701{
702 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100703 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300704 const struct intel_community *community;
705 unsigned pin = irqd_to_hwirq(d);
706 unsigned long flags;
707
Mika Westerberg27d90982016-06-16 11:25:36 +0300708 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300709
710 community = intel_get_community(pctrl, pin);
711 if (community) {
712 unsigned padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800713 unsigned gpp_offset = padno % community->gpp_size;
714 unsigned gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300715 void __iomem *reg;
716 u32 value;
717
718 reg = community->regs + community->ie_offset + gpp * 4;
719 value = readl(reg);
720 if (mask)
721 value &= ~BIT(gpp_offset);
722 else
723 value |= BIT(gpp_offset);
724 writel(value, reg);
725 }
726
Mika Westerberg27d90982016-06-16 11:25:36 +0300727 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300728}
729
730static void intel_gpio_irq_mask(struct irq_data *d)
731{
732 intel_gpio_irq_mask_unmask(d, true);
733}
734
735static void intel_gpio_irq_unmask(struct irq_data *d)
736{
737 intel_gpio_irq_mask_unmask(d, false);
738}
739
740static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
741{
742 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100743 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300744 unsigned pin = irqd_to_hwirq(d);
745 unsigned long flags;
746 void __iomem *reg;
747 u32 value;
748
749 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
750 if (!reg)
751 return -EINVAL;
752
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300753 /*
754 * If the pin is in ACPI mode it is still usable as a GPIO but it
755 * cannot be used as IRQ because GPI_IS status bit will not be
756 * updated by the host controller hardware.
757 */
758 if (intel_pad_acpi_mode(pctrl, pin)) {
759 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
760 return -EPERM;
761 }
762
Mika Westerberg27d90982016-06-16 11:25:36 +0300763 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300764
765 value = readl(reg);
766
767 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
768
769 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
770 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
771 } else if (type & IRQ_TYPE_EDGE_FALLING) {
772 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
773 value |= PADCFG0_RXINV;
774 } else if (type & IRQ_TYPE_EDGE_RISING) {
775 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
Qipeng Zhabf380cf2016-03-17 02:15:25 +0800776 } else if (type & IRQ_TYPE_LEVEL_MASK) {
777 if (type & IRQ_TYPE_LEVEL_LOW)
778 value |= PADCFG0_RXINV;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300779 } else {
780 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
781 }
782
783 writel(value, reg);
784
785 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200786 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300787 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200788 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300789
Mika Westerberg27d90982016-06-16 11:25:36 +0300790 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300791
792 return 0;
793}
794
795static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
796{
797 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100798 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300799 unsigned pin = irqd_to_hwirq(d);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300800
Mika Westerberg7981c0012015-03-30 17:31:49 +0300801 if (on)
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700802 enable_irq_wake(pctrl->irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300803 else
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700804 disable_irq_wake(pctrl->irq);
Andy Shevchenko9a520fd2016-07-08 14:30:46 +0300805
Mika Westerberg7981c0012015-03-30 17:31:49 +0300806 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
807 return 0;
808}
809
Mika Westerberg193b40c2015-10-21 13:08:43 +0300810static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300811 const struct intel_community *community)
812{
Mika Westerberg193b40c2015-10-21 13:08:43 +0300813 struct gpio_chip *gc = &pctrl->chip;
814 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300815 int gpp;
816
817 for (gpp = 0; gpp < community->ngpps; gpp++) {
818 unsigned long pending, enabled, gpp_offset;
819
820 pending = readl(community->regs + GPI_IS + gpp * 4);
821 enabled = readl(community->regs + community->ie_offset +
822 gpp * 4);
823
824 /* Only interrupts that are enabled */
825 pending &= enabled;
826
Qipeng Zha618a919b2015-11-26 01:09:51 +0800827 for_each_set_bit(gpp_offset, &pending, community->gpp_size) {
Mika Westerberg7981c0012015-03-30 17:31:49 +0300828 unsigned padno, irq;
829
830 /*
831 * The last group in community can have less pins
832 * than NPADS_IN_GPP.
833 */
Qipeng Zha618a919b2015-11-26 01:09:51 +0800834 padno = gpp_offset + gpp * community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300835 if (padno >= community->npins)
836 break;
837
838 irq = irq_find_mapping(gc->irqdomain,
839 community->pin_base + padno);
840 generic_handle_irq(irq);
Mika Westerberg193b40c2015-10-21 13:08:43 +0300841
842 ret |= IRQ_HANDLED;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300843 }
844 }
Mika Westerberg193b40c2015-10-21 13:08:43 +0300845
846 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300847}
848
Mika Westerberg193b40c2015-10-21 13:08:43 +0300849static irqreturn_t intel_gpio_irq(int irq, void *data)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300850{
Mika Westerberg193b40c2015-10-21 13:08:43 +0300851 const struct intel_community *community;
852 struct intel_pinctrl *pctrl = data;
853 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300854 int i;
855
Mika Westerberg7981c0012015-03-30 17:31:49 +0300856 /* Need to check all communities for pending interrupts */
Mika Westerberg193b40c2015-10-21 13:08:43 +0300857 for (i = 0; i < pctrl->ncommunities; i++) {
858 community = &pctrl->communities[i];
859 ret |= intel_gpio_community_irq_handler(pctrl, community);
860 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300861
Mika Westerberg193b40c2015-10-21 13:08:43 +0300862 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300863}
864
865static struct irq_chip intel_gpio_irqchip = {
866 .name = "intel-gpio",
Qi Zhenga939bb52016-03-17 02:15:26 +0800867 .irq_enable = intel_gpio_irq_enable,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300868 .irq_ack = intel_gpio_irq_ack,
869 .irq_mask = intel_gpio_irq_mask,
870 .irq_unmask = intel_gpio_irq_unmask,
871 .irq_set_type = intel_gpio_irq_type,
872 .irq_set_wake = intel_gpio_irq_wake,
873};
874
Mika Westerberg7981c0012015-03-30 17:31:49 +0300875static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
876{
877 int ret;
878
879 pctrl->chip = intel_gpio_chip;
880
881 pctrl->chip.ngpio = pctrl->soc->npins;
882 pctrl->chip.label = dev_name(pctrl->dev);
Linus Walleij58383c782015-11-04 09:56:26 +0100883 pctrl->chip.parent = pctrl->dev;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300884 pctrl->chip.base = -1;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700885 pctrl->irq = irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300886
Mika Westerbergf25c3aa2017-01-10 17:31:57 +0300887 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300888 if (ret) {
889 dev_err(pctrl->dev, "failed to register gpiochip\n");
890 return ret;
891 }
892
893 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
894 0, 0, pctrl->soc->npins);
895 if (ret) {
896 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +0300897 return ret;
Mika Westerberg193b40c2015-10-21 13:08:43 +0300898 }
899
900 /*
901 * We need to request the interrupt here (instead of providing chip
902 * to the irq directly) because on some platforms several GPIO
903 * controllers share the same interrupt line.
904 */
Mika Westerberg1a7d1cb2016-06-16 11:25:37 +0300905 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
906 IRQF_SHARED | IRQF_NO_THREAD,
Mika Westerberg193b40c2015-10-21 13:08:43 +0300907 dev_name(pctrl->dev), pctrl);
908 if (ret) {
909 dev_err(pctrl->dev, "failed to request interrupt\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +0300910 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300911 }
912
913 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
Andy Shevchenko3ae02c12016-11-25 13:31:16 +0200914 handle_bad_irq, IRQ_TYPE_NONE);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300915 if (ret) {
916 dev_err(pctrl->dev, "failed to add irqchip\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +0300917 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300918 }
919
920 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
Mika Westerberg193b40c2015-10-21 13:08:43 +0300921 NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300922 return 0;
923}
924
925static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
926{
927#ifdef CONFIG_PM_SLEEP
928 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
929 struct intel_community_context *communities;
930 struct intel_pad_context *pads;
931 int i;
932
933 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
934 if (!pads)
935 return -ENOMEM;
936
937 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
938 sizeof(*communities), GFP_KERNEL);
939 if (!communities)
940 return -ENOMEM;
941
942
943 for (i = 0; i < pctrl->ncommunities; i++) {
944 struct intel_community *community = &pctrl->communities[i];
945 u32 *intmask;
946
947 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
948 sizeof(*intmask), GFP_KERNEL);
949 if (!intmask)
950 return -ENOMEM;
951
952 communities[i].intmask = intmask;
953 }
954
955 pctrl->context.pads = pads;
956 pctrl->context.communities = communities;
957#endif
958
959 return 0;
960}
961
962int intel_pinctrl_probe(struct platform_device *pdev,
963 const struct intel_pinctrl_soc_data *soc_data)
964{
965 struct intel_pinctrl *pctrl;
966 int i, ret, irq;
967
968 if (!soc_data)
969 return -EINVAL;
970
971 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
972 if (!pctrl)
973 return -ENOMEM;
974
975 pctrl->dev = &pdev->dev;
976 pctrl->soc = soc_data;
Mika Westerberg27d90982016-06-16 11:25:36 +0300977 raw_spin_lock_init(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300978
979 /*
980 * Make a copy of the communities which we can use to hold pointers
981 * to the registers.
982 */
983 pctrl->ncommunities = pctrl->soc->ncommunities;
984 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
985 sizeof(*pctrl->communities), GFP_KERNEL);
986 if (!pctrl->communities)
987 return -ENOMEM;
988
989 for (i = 0; i < pctrl->ncommunities; i++) {
990 struct intel_community *community = &pctrl->communities[i];
991 struct resource *res;
992 void __iomem *regs;
993 u32 padbar;
994
995 *community = pctrl->soc->communities[i];
996
997 res = platform_get_resource(pdev, IORESOURCE_MEM,
998 community->barno);
999 regs = devm_ioremap_resource(&pdev->dev, res);
1000 if (IS_ERR(regs))
1001 return PTR_ERR(regs);
1002
1003 /* Read offset of the pad configuration registers */
1004 padbar = readl(regs + PADBAR);
1005
1006 community->regs = regs;
1007 community->pad_regs = regs + padbar;
Qipeng Zha618a919b2015-11-26 01:09:51 +08001008 community->ngpps = DIV_ROUND_UP(community->npins,
1009 community->gpp_size);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001010 }
1011
1012 irq = platform_get_irq(pdev, 0);
1013 if (irq < 0) {
1014 dev_err(&pdev->dev, "failed to get interrupt number\n");
1015 return irq;
1016 }
1017
1018 ret = intel_pinctrl_pm_init(pctrl);
1019 if (ret)
1020 return ret;
1021
1022 pctrl->pctldesc = intel_pinctrl_desc;
1023 pctrl->pctldesc.name = dev_name(&pdev->dev);
1024 pctrl->pctldesc.pins = pctrl->soc->pins;
1025 pctrl->pctldesc.npins = pctrl->soc->npins;
1026
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301027 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1028 pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001029 if (IS_ERR(pctrl->pctldev)) {
Mika Westerberg7981c0012015-03-30 17:31:49 +03001030 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001031 return PTR_ERR(pctrl->pctldev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001032 }
1033
1034 ret = intel_gpio_probe(pctrl, irq);
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301035 if (ret)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001036 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001037
1038 platform_set_drvdata(pdev, pctrl);
1039
1040 return 0;
1041}
1042EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1043
Mika Westerberg7981c0012015-03-30 17:31:49 +03001044#ifdef CONFIG_PM_SLEEP
Mika Westerbergc538b942016-10-10 16:39:31 +03001045static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1046{
1047 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1048
1049 if (!pd || !intel_pad_usable(pctrl, pin))
1050 return false;
1051
1052 /*
1053 * Only restore the pin if it is actually in use by the kernel (or
1054 * by userspace). It is possible that some pins are used by the
1055 * BIOS during resume and those are not always locked down so leave
1056 * them alone.
1057 */
1058 if (pd->mux_owner || pd->gpio_owner ||
1059 gpiochip_line_is_irq(&pctrl->chip, pin))
1060 return true;
1061
1062 return false;
1063}
1064
Mika Westerberg7981c0012015-03-30 17:31:49 +03001065int intel_pinctrl_suspend(struct device *dev)
1066{
1067 struct platform_device *pdev = to_platform_device(dev);
1068 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1069 struct intel_community_context *communities;
1070 struct intel_pad_context *pads;
1071 int i;
1072
1073 pads = pctrl->context.pads;
1074 for (i = 0; i < pctrl->soc->npins; i++) {
1075 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1076 u32 val;
1077
Mika Westerbergc538b942016-10-10 16:39:31 +03001078 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001079 continue;
1080
1081 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1082 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1083 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1084 pads[i].padcfg1 = val;
1085 }
1086
1087 communities = pctrl->context.communities;
1088 for (i = 0; i < pctrl->ncommunities; i++) {
1089 struct intel_community *community = &pctrl->communities[i];
1090 void __iomem *base;
1091 unsigned gpp;
1092
1093 base = community->regs + community->ie_offset;
1094 for (gpp = 0; gpp < community->ngpps; gpp++)
1095 communities[i].intmask[gpp] = readl(base + gpp * 4);
1096 }
1097
1098 return 0;
1099}
1100EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1101
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001102static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1103{
1104 size_t i;
1105
1106 for (i = 0; i < pctrl->ncommunities; i++) {
1107 const struct intel_community *community;
1108 void __iomem *base;
1109 unsigned gpp;
1110
1111 community = &pctrl->communities[i];
1112 base = community->regs;
1113
1114 for (gpp = 0; gpp < community->ngpps; gpp++) {
1115 /* Mask and clear all interrupts */
1116 writel(0, base + community->ie_offset + gpp * 4);
1117 writel(0xffff, base + GPI_IS + gpp * 4);
1118 }
1119 }
1120}
1121
Mika Westerberg7981c0012015-03-30 17:31:49 +03001122int intel_pinctrl_resume(struct device *dev)
1123{
1124 struct platform_device *pdev = to_platform_device(dev);
1125 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1126 const struct intel_community_context *communities;
1127 const struct intel_pad_context *pads;
1128 int i;
1129
1130 /* Mask all interrupts */
1131 intel_gpio_irq_init(pctrl);
1132
1133 pads = pctrl->context.pads;
1134 for (i = 0; i < pctrl->soc->npins; i++) {
1135 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1136 void __iomem *padcfg;
1137 u32 val;
1138
Mika Westerbergc538b942016-10-10 16:39:31 +03001139 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001140 continue;
1141
1142 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1143 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1144 if (val != pads[i].padcfg0) {
1145 writel(pads[i].padcfg0, padcfg);
1146 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1147 desc->number, readl(padcfg));
1148 }
1149
1150 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1151 val = readl(padcfg);
1152 if (val != pads[i].padcfg1) {
1153 writel(pads[i].padcfg1, padcfg);
1154 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1155 desc->number, readl(padcfg));
1156 }
1157 }
1158
1159 communities = pctrl->context.communities;
1160 for (i = 0; i < pctrl->ncommunities; i++) {
1161 struct intel_community *community = &pctrl->communities[i];
1162 void __iomem *base;
1163 unsigned gpp;
1164
1165 base = community->regs + community->ie_offset;
1166 for (gpp = 0; gpp < community->ngpps; gpp++) {
1167 writel(communities[i].intmask[gpp], base + gpp * 4);
1168 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1169 readl(base + gpp * 4));
1170 }
1171 }
1172
1173 return 0;
1174}
1175EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1176#endif
1177
1178MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1179MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1180MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1181MODULE_LICENSE("GPL v2");