blob: d023b64825d081798745135f92f51b6ec4833f9e [file] [log] [blame]
Andy Shevchenko875a92b2018-06-29 15:36:34 +03001// SPDX-License-Identifier: GPL-2.0
Mika Westerberg7981c0012015-03-30 17:31:49 +03002/*
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>
Mika Westerberg7981c0012015-03-30 17:31:49 +03008 */
9
10#include <linux/module.h>
Mika Westerberg193b40c2015-10-21 13:08:43 +030011#include <linux/interrupt.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030012#include <linux/gpio/driver.h>
Mika Westerberge57725e2017-01-27 13:07:14 +030013#include <linux/log2.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030014#include <linux/platform_device.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030015#include <linux/pinctrl/pinctrl.h>
16#include <linux/pinctrl/pinmux.h>
17#include <linux/pinctrl/pinconf.h>
18#include <linux/pinctrl/pinconf-generic.h>
19
Mika Westerbergc538b942016-10-10 16:39:31 +030020#include "../core.h"
Mika Westerberg7981c0012015-03-30 17:31:49 +030021#include "pinctrl-intel.h"
22
Mika Westerberg7981c0012015-03-30 17:31:49 +030023/* Offset from regs */
Mika Westerberge57725e2017-01-27 13:07:14 +030024#define REVID 0x000
25#define REVID_SHIFT 16
26#define REVID_MASK GENMASK(31, 16)
27
Mika Westerberg7981c0012015-03-30 17:31:49 +030028#define PADBAR 0x00c
29#define GPI_IS 0x100
Mika Westerberg7981c0012015-03-30 17:31:49 +030030
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
Mika Westerberge57725e2017-01-27 13:07:14 +030044#define PADCFG0_PREGFRXSEL BIT(24)
Mika Westerberg7981c0012015-03-30 17:31:49 +030045#define PADCFG0_RXINV BIT(23)
46#define PADCFG0_GPIROUTIOXAPIC BIT(20)
47#define PADCFG0_GPIROUTSCI BIT(19)
48#define PADCFG0_GPIROUTSMI BIT(18)
49#define PADCFG0_GPIROUTNMI BIT(17)
50#define PADCFG0_PMODE_SHIFT 10
51#define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
52#define PADCFG0_GPIORXDIS BIT(9)
53#define PADCFG0_GPIOTXDIS BIT(8)
54#define PADCFG0_GPIORXSTATE BIT(1)
55#define PADCFG0_GPIOTXSTATE BIT(0)
56
57#define PADCFG1 0x004
58#define PADCFG1_TERM_UP BIT(13)
59#define PADCFG1_TERM_SHIFT 10
60#define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
61#define PADCFG1_TERM_20K 4
62#define PADCFG1_TERM_2K 3
63#define PADCFG1_TERM_5K 2
64#define PADCFG1_TERM_1K 1
65
Mika Westerberge57725e2017-01-27 13:07:14 +030066#define PADCFG2 0x008
67#define PADCFG2_DEBEN BIT(0)
68#define PADCFG2_DEBOUNCE_SHIFT 1
69#define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
70
71#define DEBOUNCE_PERIOD 31250 /* ns */
72
Mika Westerberg7981c0012015-03-30 17:31:49 +030073struct intel_pad_context {
74 u32 padcfg0;
75 u32 padcfg1;
Mika Westerberge57725e2017-01-27 13:07:14 +030076 u32 padcfg2;
Mika Westerberg7981c0012015-03-30 17:31:49 +030077};
78
79struct intel_community_context {
80 u32 *intmask;
81};
82
83struct intel_pinctrl_context {
84 struct intel_pad_context *pads;
85 struct intel_community_context *communities;
86};
87
88/**
89 * struct intel_pinctrl - Intel pinctrl private structure
90 * @dev: Pointer to the device structure
91 * @lock: Lock to serialize register access
92 * @pctldesc: Pin controller description
93 * @pctldev: Pointer to the pin controller device
94 * @chip: GPIO chip in this pin controller
95 * @soc: SoC/PCH specific pin configuration data
96 * @communities: All communities in this pin controller
97 * @ncommunities: Number of communities in this pin controller
98 * @context: Configuration saved over system sleep
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -070099 * @irq: pinctrl/GPIO chip irq number
Mika Westerberg7981c0012015-03-30 17:31:49 +0300100 */
101struct intel_pinctrl {
102 struct device *dev;
Mika Westerberg27d90982016-06-16 11:25:36 +0300103 raw_spinlock_t lock;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300104 struct pinctrl_desc pctldesc;
105 struct pinctrl_dev *pctldev;
106 struct gpio_chip chip;
107 const struct intel_pinctrl_soc_data *soc;
108 struct intel_community *communities;
109 size_t ncommunities;
110 struct intel_pinctrl_context context;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700111 int irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300112};
113
Mika Westerberg7981c0012015-03-30 17:31:49 +0300114#define pin_to_padno(c, p) ((p) - (c)->pin_base)
Mika Westerberg919eb472017-06-06 16:18:17 +0300115#define padgroup_offset(g, p) ((p) - (g)->base)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300116
117static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
118 unsigned pin)
119{
120 struct intel_community *community;
121 int i;
122
123 for (i = 0; i < pctrl->ncommunities; i++) {
124 community = &pctrl->communities[i];
125 if (pin >= community->pin_base &&
126 pin < community->pin_base + community->npins)
127 return community;
128 }
129
130 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
131 return NULL;
132}
133
Mika Westerberg919eb472017-06-06 16:18:17 +0300134static const struct intel_padgroup *
135intel_community_get_padgroup(const struct intel_community *community,
136 unsigned pin)
137{
138 int i;
139
140 for (i = 0; i < community->ngpps; i++) {
141 const struct intel_padgroup *padgrp = &community->gpps[i];
142
143 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
144 return padgrp;
145 }
146
147 return NULL;
148}
149
Mika Westerberg7981c0012015-03-30 17:31:49 +0300150static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
151 unsigned reg)
152{
153 const struct intel_community *community;
154 unsigned padno;
Mika Westerberge57725e2017-01-27 13:07:14 +0300155 size_t nregs;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300156
157 community = intel_get_community(pctrl, pin);
158 if (!community)
159 return NULL;
160
161 padno = pin_to_padno(community, pin);
Mika Westerberge57725e2017-01-27 13:07:14 +0300162 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
163
164 if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
165 return NULL;
166
167 return community->pad_regs + reg + padno * nregs * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300168}
169
170static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
171{
172 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300173 const struct intel_padgroup *padgrp;
174 unsigned gpp, offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300175 void __iomem *padown;
176
177 community = intel_get_community(pctrl, pin);
178 if (!community)
179 return false;
180 if (!community->padown_offset)
181 return true;
182
Mika Westerberg919eb472017-06-06 16:18:17 +0300183 padgrp = intel_community_get_padgroup(community, pin);
184 if (!padgrp)
185 return false;
186
187 gpp_offset = padgroup_offset(padgrp, pin);
188 gpp = PADOWN_GPP(gpp_offset);
189 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300190 padown = community->regs + offset;
191
Mika Westerberg919eb472017-06-06 16:18:17 +0300192 return !(readl(padown) & PADOWN_MASK(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300193}
194
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300195static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300196{
197 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300198 const struct intel_padgroup *padgrp;
199 unsigned offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300200 void __iomem *hostown;
201
202 community = intel_get_community(pctrl, pin);
203 if (!community)
204 return true;
205 if (!community->hostown_offset)
206 return false;
207
Mika Westerberg919eb472017-06-06 16:18:17 +0300208 padgrp = intel_community_get_padgroup(community, pin);
209 if (!padgrp)
210 return true;
211
212 gpp_offset = padgroup_offset(padgrp, pin);
213 offset = community->hostown_offset + padgrp->reg_num * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300214 hostown = community->regs + offset;
215
Mika Westerberg919eb472017-06-06 16:18:17 +0300216 return !(readl(hostown) & BIT(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300217}
218
219static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
220{
221 struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300222 const struct intel_padgroup *padgrp;
223 unsigned offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300224 u32 value;
225
226 community = intel_get_community(pctrl, pin);
227 if (!community)
228 return true;
229 if (!community->padcfglock_offset)
230 return false;
231
Mika Westerberg919eb472017-06-06 16:18:17 +0300232 padgrp = intel_community_get_padgroup(community, pin);
233 if (!padgrp)
234 return true;
235
236 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300237
238 /*
239 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
240 * the pad is considered unlocked. Any other case means that it is
241 * either fully or partially locked and we don't touch it.
242 */
Mika Westerberg919eb472017-06-06 16:18:17 +0300243 offset = community->padcfglock_offset + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300244 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300245 if (value & BIT(gpp_offset))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300246 return true;
247
Mika Westerberg919eb472017-06-06 16:18:17 +0300248 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300249 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300250 if (value & BIT(gpp_offset))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300251 return true;
252
253 return false;
254}
255
256static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
257{
258 return intel_pad_owned_by_host(pctrl, pin) &&
Mika Westerberg7981c0012015-03-30 17:31:49 +0300259 !intel_pad_locked(pctrl, pin);
260}
261
262static int intel_get_groups_count(struct pinctrl_dev *pctldev)
263{
264 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
265
266 return pctrl->soc->ngroups;
267}
268
269static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
270 unsigned group)
271{
272 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
273
274 return pctrl->soc->groups[group].name;
275}
276
277static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
278 const unsigned **pins, unsigned *npins)
279{
280 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
281
282 *pins = pctrl->soc->groups[group].pins;
283 *npins = pctrl->soc->groups[group].npins;
284 return 0;
285}
286
287static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
288 unsigned pin)
289{
290 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
Mika Westerberge57725e2017-01-27 13:07:14 +0300291 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300292 u32 cfg0, cfg1, mode;
293 bool locked, acpi;
294
295 if (!intel_pad_owned_by_host(pctrl, pin)) {
296 seq_puts(s, "not available");
297 return;
298 }
299
300 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
301 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
302
303 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
304 if (!mode)
305 seq_puts(s, "GPIO ");
306 else
307 seq_printf(s, "mode %d ", mode);
308
309 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
310
Mika Westerberge57725e2017-01-27 13:07:14 +0300311 /* Dump the additional PADCFG registers if available */
312 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
313 if (padcfg)
314 seq_printf(s, " 0x%08x", readl(padcfg));
315
Mika Westerberg7981c0012015-03-30 17:31:49 +0300316 locked = intel_pad_locked(pctrl, pin);
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300317 acpi = intel_pad_acpi_mode(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300318
319 if (locked || acpi) {
320 seq_puts(s, " [");
321 if (locked) {
322 seq_puts(s, "LOCKED");
323 if (acpi)
324 seq_puts(s, ", ");
325 }
326 if (acpi)
327 seq_puts(s, "ACPI");
328 seq_puts(s, "]");
329 }
330}
331
332static const struct pinctrl_ops intel_pinctrl_ops = {
333 .get_groups_count = intel_get_groups_count,
334 .get_group_name = intel_get_group_name,
335 .get_group_pins = intel_get_group_pins,
336 .pin_dbg_show = intel_pin_dbg_show,
337};
338
339static int intel_get_functions_count(struct pinctrl_dev *pctldev)
340{
341 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
342
343 return pctrl->soc->nfunctions;
344}
345
346static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
347 unsigned function)
348{
349 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
350
351 return pctrl->soc->functions[function].name;
352}
353
354static int intel_get_function_groups(struct pinctrl_dev *pctldev,
355 unsigned function,
356 const char * const **groups,
357 unsigned * const ngroups)
358{
359 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
360
361 *groups = pctrl->soc->functions[function].groups;
362 *ngroups = pctrl->soc->functions[function].ngroups;
363 return 0;
364}
365
366static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
367 unsigned group)
368{
369 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
370 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
371 unsigned long flags;
372 int i;
373
Mika Westerberg27d90982016-06-16 11:25:36 +0300374 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300375
376 /*
377 * All pins in the groups needs to be accessible and writable
378 * before we can enable the mux for this group.
379 */
380 for (i = 0; i < grp->npins; i++) {
381 if (!intel_pad_usable(pctrl, grp->pins[i])) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300382 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300383 return -EBUSY;
384 }
385 }
386
387 /* Now enable the mux setting for each pin in the group */
388 for (i = 0; i < grp->npins; i++) {
389 void __iomem *padcfg0;
390 u32 value;
391
392 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
393 value = readl(padcfg0);
394
395 value &= ~PADCFG0_PMODE_MASK;
Mika Westerberg1f6b4192017-06-06 16:18:18 +0300396
397 if (grp->modes)
398 value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
399 else
400 value |= grp->mode << PADCFG0_PMODE_SHIFT;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300401
402 writel(value, padcfg0);
403 }
404
Mika Westerberg27d90982016-06-16 11:25:36 +0300405 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300406
407 return 0;
408}
409
Andy Shevchenko17fab472017-01-02 14:07:22 +0200410static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
411{
412 u32 value;
413
414 value = readl(padcfg0);
415 if (input) {
416 value &= ~PADCFG0_GPIORXDIS;
417 value |= PADCFG0_GPIOTXDIS;
418 } else {
419 value &= ~PADCFG0_GPIOTXDIS;
420 value |= PADCFG0_GPIORXDIS;
421 }
422 writel(value, padcfg0);
423}
424
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300425static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
426{
427 u32 value;
428
429 /* Put the pad into GPIO mode */
430 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
431 /* Disable SCI/SMI/NMI generation */
432 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
433 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
434 writel(value, padcfg0);
435}
436
Mika Westerberg7981c0012015-03-30 17:31:49 +0300437static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
438 struct pinctrl_gpio_range *range,
439 unsigned pin)
440{
441 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
442 void __iomem *padcfg0;
443 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300444
Mika Westerberg27d90982016-06-16 11:25:36 +0300445 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300446
447 if (!intel_pad_usable(pctrl, pin)) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300448 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300449 return -EBUSY;
450 }
451
452 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300453 intel_gpio_set_gpio_mode(padcfg0);
Andy Shevchenko17fab472017-01-02 14:07:22 +0200454 /* Disable TX buffer and enable RX (this will be input) */
455 __intel_gpio_set_direction(padcfg0, true);
456
Mika Westerberg27d90982016-06-16 11:25:36 +0300457 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300458
459 return 0;
460}
461
462static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
463 struct pinctrl_gpio_range *range,
464 unsigned pin, bool input)
465{
466 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
467 void __iomem *padcfg0;
468 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300469
Mika Westerberg27d90982016-06-16 11:25:36 +0300470 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300471
472 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
Andy Shevchenko17fab472017-01-02 14:07:22 +0200473 __intel_gpio_set_direction(padcfg0, input);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300474
Mika Westerberg27d90982016-06-16 11:25:36 +0300475 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300476
477 return 0;
478}
479
480static const struct pinmux_ops intel_pinmux_ops = {
481 .get_functions_count = intel_get_functions_count,
482 .get_function_name = intel_get_function_name,
483 .get_function_groups = intel_get_function_groups,
484 .set_mux = intel_pinmux_set_mux,
485 .gpio_request_enable = intel_gpio_request_enable,
486 .gpio_set_direction = intel_gpio_set_direction,
487};
488
489static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
490 unsigned long *config)
491{
492 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
493 enum pin_config_param param = pinconf_to_config_param(*config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300494 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300495 u32 value, term;
Mika Westerberge57725e2017-01-27 13:07:14 +0300496 u32 arg = 0;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300497
498 if (!intel_pad_owned_by_host(pctrl, pin))
499 return -ENOTSUPP;
500
Mika Westerberg04cc0582017-01-27 13:07:15 +0300501 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300502 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
503 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
504
505 switch (param) {
506 case PIN_CONFIG_BIAS_DISABLE:
507 if (term)
508 return -EINVAL;
509 break;
510
511 case PIN_CONFIG_BIAS_PULL_UP:
512 if (!term || !(value & PADCFG1_TERM_UP))
513 return -EINVAL;
514
515 switch (term) {
516 case PADCFG1_TERM_1K:
517 arg = 1000;
518 break;
519 case PADCFG1_TERM_2K:
520 arg = 2000;
521 break;
522 case PADCFG1_TERM_5K:
523 arg = 5000;
524 break;
525 case PADCFG1_TERM_20K:
526 arg = 20000;
527 break;
528 }
529
530 break;
531
532 case PIN_CONFIG_BIAS_PULL_DOWN:
533 if (!term || value & PADCFG1_TERM_UP)
534 return -EINVAL;
535
536 switch (term) {
Mika Westerberg04cc0582017-01-27 13:07:15 +0300537 case PADCFG1_TERM_1K:
538 if (!(community->features & PINCTRL_FEATURE_1K_PD))
539 return -EINVAL;
540 arg = 1000;
541 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300542 case PADCFG1_TERM_5K:
543 arg = 5000;
544 break;
545 case PADCFG1_TERM_20K:
546 arg = 20000;
547 break;
548 }
549
550 break;
551
Mika Westerberge57725e2017-01-27 13:07:14 +0300552 case PIN_CONFIG_INPUT_DEBOUNCE: {
553 void __iomem *padcfg2;
554 u32 v;
555
556 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
557 if (!padcfg2)
558 return -ENOTSUPP;
559
560 v = readl(padcfg2);
561 if (!(v & PADCFG2_DEBEN))
562 return -EINVAL;
563
564 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
565 arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
566
567 break;
568 }
569
Mika Westerberg7981c0012015-03-30 17:31:49 +0300570 default:
571 return -ENOTSUPP;
572 }
573
574 *config = pinconf_to_config_packed(param, arg);
575 return 0;
576}
577
578static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
579 unsigned long config)
580{
581 unsigned param = pinconf_to_config_param(config);
582 unsigned arg = pinconf_to_config_argument(config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300583 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300584 void __iomem *padcfg1;
585 unsigned long flags;
586 int ret = 0;
587 u32 value;
588
Mika Westerberg27d90982016-06-16 11:25:36 +0300589 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300590
Mika Westerberg04cc0582017-01-27 13:07:15 +0300591 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300592 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
593 value = readl(padcfg1);
594
595 switch (param) {
596 case PIN_CONFIG_BIAS_DISABLE:
597 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
598 break;
599
600 case PIN_CONFIG_BIAS_PULL_UP:
601 value &= ~PADCFG1_TERM_MASK;
602
603 value |= PADCFG1_TERM_UP;
604
605 switch (arg) {
606 case 20000:
607 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
608 break;
609 case 5000:
610 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
611 break;
612 case 2000:
613 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
614 break;
615 case 1000:
616 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
617 break;
618 default:
619 ret = -EINVAL;
620 }
621
622 break;
623
624 case PIN_CONFIG_BIAS_PULL_DOWN:
625 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
626
627 switch (arg) {
628 case 20000:
629 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
630 break;
631 case 5000:
632 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
633 break;
Mika Westerberg04cc0582017-01-27 13:07:15 +0300634 case 1000:
Dan Carpenteraa1dd802017-02-07 16:20:08 +0300635 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
636 ret = -EINVAL;
637 break;
638 }
Mika Westerberg04cc0582017-01-27 13:07:15 +0300639 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
640 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300641 default:
642 ret = -EINVAL;
643 }
644
645 break;
646 }
647
648 if (!ret)
649 writel(value, padcfg1);
650
Mika Westerberg27d90982016-06-16 11:25:36 +0300651 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300652
653 return ret;
654}
655
Mika Westerberge57725e2017-01-27 13:07:14 +0300656static int intel_config_set_debounce(struct intel_pinctrl *pctrl, unsigned pin,
657 unsigned debounce)
658{
659 void __iomem *padcfg0, *padcfg2;
660 unsigned long flags;
661 u32 value0, value2;
662 int ret = 0;
663
664 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
665 if (!padcfg2)
666 return -ENOTSUPP;
667
668 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
669
670 raw_spin_lock_irqsave(&pctrl->lock, flags);
671
672 value0 = readl(padcfg0);
673 value2 = readl(padcfg2);
674
675 /* Disable glitch filter and debouncer */
676 value0 &= ~PADCFG0_PREGFRXSEL;
677 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
678
679 if (debounce) {
680 unsigned long v;
681
682 v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
683 if (v < 3 || v > 15) {
684 ret = -EINVAL;
685 goto exit_unlock;
686 } else {
687 /* Enable glitch filter and debouncer */
688 value0 |= PADCFG0_PREGFRXSEL;
689 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
690 value2 |= PADCFG2_DEBEN;
691 }
692 }
693
694 writel(value0, padcfg0);
695 writel(value2, padcfg2);
696
697exit_unlock:
698 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
699
700 return ret;
701}
702
Mika Westerberg7981c0012015-03-30 17:31:49 +0300703static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
704 unsigned long *configs, unsigned nconfigs)
705{
706 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
707 int i, ret;
708
709 if (!intel_pad_usable(pctrl, pin))
710 return -ENOTSUPP;
711
712 for (i = 0; i < nconfigs; i++) {
713 switch (pinconf_to_config_param(configs[i])) {
714 case PIN_CONFIG_BIAS_DISABLE:
715 case PIN_CONFIG_BIAS_PULL_UP:
716 case PIN_CONFIG_BIAS_PULL_DOWN:
717 ret = intel_config_set_pull(pctrl, pin, configs[i]);
718 if (ret)
719 return ret;
720 break;
721
Mika Westerberge57725e2017-01-27 13:07:14 +0300722 case PIN_CONFIG_INPUT_DEBOUNCE:
723 ret = intel_config_set_debounce(pctrl, pin,
724 pinconf_to_config_argument(configs[i]));
725 if (ret)
726 return ret;
727 break;
728
Mika Westerberg7981c0012015-03-30 17:31:49 +0300729 default:
730 return -ENOTSUPP;
731 }
732 }
733
734 return 0;
735}
736
737static const struct pinconf_ops intel_pinconf_ops = {
738 .is_generic = true,
739 .pin_config_get = intel_config_get,
740 .pin_config_set = intel_config_set,
741};
742
743static const struct pinctrl_desc intel_pinctrl_desc = {
744 .pctlops = &intel_pinctrl_ops,
745 .pmxops = &intel_pinmux_ops,
746 .confops = &intel_pinconf_ops,
747 .owner = THIS_MODULE,
748};
749
Mika Westerberg7981c0012015-03-30 17:31:49 +0300750static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
751{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100752 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300753 void __iomem *reg;
Andy Shevchenkod68b42e2017-08-24 11:19:33 +0300754 u32 padcfg0;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300755
756 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
757 if (!reg)
758 return -EINVAL;
759
Andy Shevchenkod68b42e2017-08-24 11:19:33 +0300760 padcfg0 = readl(reg);
761 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
762 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
763
764 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300765}
766
767static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
768{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100769 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Andy Shevchenko85461372017-08-24 11:19:34 +0300770 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300771 void __iomem *reg;
Andy Shevchenko85461372017-08-24 11:19:34 +0300772 u32 padcfg0;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300773
774 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
Andy Shevchenko85461372017-08-24 11:19:34 +0300775 if (!reg)
776 return;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300777
Andy Shevchenko85461372017-08-24 11:19:34 +0300778 raw_spin_lock_irqsave(&pctrl->lock, flags);
779 padcfg0 = readl(reg);
780 if (value)
781 padcfg0 |= PADCFG0_GPIOTXSTATE;
782 else
783 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
784 writel(padcfg0, reg);
785 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300786}
787
Javier Arteaga67e6d3e2018-03-06 13:42:13 +0000788static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
789{
790 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
791 void __iomem *reg;
792 u32 padcfg0;
793
794 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
795 if (!reg)
796 return -EINVAL;
797
798 padcfg0 = readl(reg);
799
800 if (padcfg0 & PADCFG0_PMODE_MASK)
801 return -EINVAL;
802
803 return !!(padcfg0 & PADCFG0_GPIOTXDIS);
804}
805
Mika Westerberg7981c0012015-03-30 17:31:49 +0300806static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
807{
808 return pinctrl_gpio_direction_input(chip->base + offset);
809}
810
811static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
812 int value)
813{
814 intel_gpio_set(chip, offset, value);
815 return pinctrl_gpio_direction_output(chip->base + offset);
816}
817
818static const struct gpio_chip intel_gpio_chip = {
819 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200820 .request = gpiochip_generic_request,
821 .free = gpiochip_generic_free,
Javier Arteaga67e6d3e2018-03-06 13:42:13 +0000822 .get_direction = intel_gpio_get_direction,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300823 .direction_input = intel_gpio_direction_input,
824 .direction_output = intel_gpio_direction_output,
825 .get = intel_gpio_get,
826 .set = intel_gpio_set,
Mika Westerberge57725e2017-01-27 13:07:14 +0300827 .set_config = gpiochip_generic_config,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300828};
829
Mika Westerberga60eac32017-11-27 16:54:43 +0300830/**
831 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
832 * @pctrl: Pinctrl structure
833 * @offset: GPIO offset from gpiolib
834 * @commmunity: Community is filled here if not %NULL
835 * @padgrp: Pad group is filled here if not %NULL
836 *
837 * When coming through gpiolib irqchip, the GPIO offset is not
838 * automatically translated to pinctrl pin number. This function can be
839 * used to find out the corresponding pinctrl pin.
840 */
841static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned offset,
842 const struct intel_community **community,
843 const struct intel_padgroup **padgrp)
844{
845 int i;
846
847 for (i = 0; i < pctrl->ncommunities; i++) {
848 const struct intel_community *comm = &pctrl->communities[i];
849 int j;
850
851 for (j = 0; j < comm->ngpps; j++) {
852 const struct intel_padgroup *pgrp = &comm->gpps[j];
853
854 if (pgrp->gpio_base < 0)
855 continue;
856
857 if (offset >= pgrp->gpio_base &&
858 offset < pgrp->gpio_base + pgrp->size) {
859 int pin;
860
861 pin = pgrp->base + offset - pgrp->gpio_base;
862 if (community)
863 *community = comm;
864 if (padgrp)
865 *padgrp = pgrp;
866
867 return pin;
868 }
869 }
870 }
871
872 return -EINVAL;
873}
874
Andy Shevchenko55aedef52018-07-25 15:42:08 +0300875static int intel_gpio_irq_reqres(struct irq_data *d)
876{
877 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
878 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
879 int pin;
880
881 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
882 if (pin >= 0) {
883 if (gpiochip_lock_as_irq(gc, pin)) {
884 dev_err(pctrl->dev, "unable to lock HW IRQ %d for IRQ\n",
885 pin);
886 return -EINVAL;
887 }
888 }
889 return 0;
890}
891
892static void intel_gpio_irq_relres(struct irq_data *d)
893{
894 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
895 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
896 int pin;
897
898 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
899 if (pin >= 0)
900 gpiochip_unlock_as_irq(gc, pin);
901}
902
Mika Westerberg7981c0012015-03-30 17:31:49 +0300903static void intel_gpio_irq_ack(struct irq_data *d)
904{
905 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100906 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300907 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300908 const struct intel_padgroup *padgrp;
909 int pin;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300910
Mika Westerberga60eac32017-11-27 16:54:43 +0300911 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
912 if (pin >= 0) {
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300913 unsigned gpp, gpp_offset, is_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300914
Mika Westerberg919eb472017-06-06 16:18:17 +0300915 gpp = padgrp->reg_num;
916 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300917 is_offset = community->is_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300918
919 raw_spin_lock(&pctrl->lock);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300920 writel(BIT(gpp_offset), community->regs + is_offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300921 raw_spin_unlock(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300922 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300923}
924
Qi Zhenga939bb52016-03-17 02:15:26 +0800925static void intel_gpio_irq_enable(struct irq_data *d)
926{
927 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
928 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
929 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300930 const struct intel_padgroup *padgrp;
931 int pin;
Qi Zhenga939bb52016-03-17 02:15:26 +0800932
Mika Westerberga60eac32017-11-27 16:54:43 +0300933 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
934 if (pin >= 0) {
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300935 unsigned gpp, gpp_offset, is_offset;
Mika Westerberg919eb472017-06-06 16:18:17 +0300936 unsigned long flags;
Qi Zhenga939bb52016-03-17 02:15:26 +0800937 u32 value;
938
Mika Westerberg919eb472017-06-06 16:18:17 +0300939 gpp = padgrp->reg_num;
940 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300941 is_offset = community->is_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300942
943 raw_spin_lock_irqsave(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800944 /* Clear interrupt status first to avoid unexpected interrupt */
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300945 writel(BIT(gpp_offset), community->regs + is_offset);
Qi Zhenga939bb52016-03-17 02:15:26 +0800946
947 value = readl(community->regs + community->ie_offset + gpp * 4);
948 value |= BIT(gpp_offset);
949 writel(value, community->regs + community->ie_offset + gpp * 4);
Mika Westerberg919eb472017-06-06 16:18:17 +0300950 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800951 }
Qi Zhenga939bb52016-03-17 02:15:26 +0800952}
953
Mika Westerberg7981c0012015-03-30 17:31:49 +0300954static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
955{
956 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100957 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300958 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300959 const struct intel_padgroup *padgrp;
960 int pin;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300961
Mika Westerberga60eac32017-11-27 16:54:43 +0300962 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
963 if (pin >= 0) {
Mika Westerberg919eb472017-06-06 16:18:17 +0300964 unsigned gpp, gpp_offset;
965 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300966 void __iomem *reg;
967 u32 value;
968
Mika Westerberg919eb472017-06-06 16:18:17 +0300969 gpp = padgrp->reg_num;
970 gpp_offset = padgroup_offset(padgrp, pin);
971
Mika Westerberg7981c0012015-03-30 17:31:49 +0300972 reg = community->regs + community->ie_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300973
974 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300975 value = readl(reg);
976 if (mask)
977 value &= ~BIT(gpp_offset);
978 else
979 value |= BIT(gpp_offset);
980 writel(value, reg);
Mika Westerberg919eb472017-06-06 16:18:17 +0300981 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300982 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300983}
984
985static void intel_gpio_irq_mask(struct irq_data *d)
986{
987 intel_gpio_irq_mask_unmask(d, true);
988}
989
990static void intel_gpio_irq_unmask(struct irq_data *d)
991{
992 intel_gpio_irq_mask_unmask(d, false);
993}
994
995static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
996{
997 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100998 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberga60eac32017-11-27 16:54:43 +0300999 unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001000 unsigned long flags;
1001 void __iomem *reg;
1002 u32 value;
1003
1004 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1005 if (!reg)
1006 return -EINVAL;
1007
Mika Westerberg4341e8a2015-10-21 13:08:44 +03001008 /*
1009 * If the pin is in ACPI mode it is still usable as a GPIO but it
1010 * cannot be used as IRQ because GPI_IS status bit will not be
1011 * updated by the host controller hardware.
1012 */
1013 if (intel_pad_acpi_mode(pctrl, pin)) {
1014 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1015 return -EPERM;
1016 }
1017
Mika Westerberg27d90982016-06-16 11:25:36 +03001018 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001019
Mika Westerbergf5a26ac2017-11-29 16:25:44 +03001020 intel_gpio_set_gpio_mode(reg);
1021
Mika Westerberg7981c0012015-03-30 17:31:49 +03001022 value = readl(reg);
1023
1024 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1025
1026 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1027 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1028 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1029 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1030 value |= PADCFG0_RXINV;
1031 } else if (type & IRQ_TYPE_EDGE_RISING) {
1032 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
Qipeng Zhabf380cf2016-03-17 02:15:25 +08001033 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1034 if (type & IRQ_TYPE_LEVEL_LOW)
1035 value |= PADCFG0_RXINV;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001036 } else {
1037 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1038 }
1039
1040 writel(value, reg);
1041
1042 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +02001043 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001044 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +02001045 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001046
Mika Westerberg27d90982016-06-16 11:25:36 +03001047 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001048
1049 return 0;
1050}
1051
1052static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1053{
1054 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +01001055 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberga60eac32017-11-27 16:54:43 +03001056 unsigned pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001057
Mika Westerberg7981c0012015-03-30 17:31:49 +03001058 if (on)
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001059 enable_irq_wake(pctrl->irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001060 else
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001061 disable_irq_wake(pctrl->irq);
Andy Shevchenko9a520fd2016-07-08 14:30:46 +03001062
Mika Westerberg7981c0012015-03-30 17:31:49 +03001063 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1064 return 0;
1065}
1066
Mika Westerberg193b40c2015-10-21 13:08:43 +03001067static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
Mika Westerberg7981c0012015-03-30 17:31:49 +03001068 const struct intel_community *community)
1069{
Mika Westerberg193b40c2015-10-21 13:08:43 +03001070 struct gpio_chip *gc = &pctrl->chip;
1071 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001072 int gpp;
1073
1074 for (gpp = 0; gpp < community->ngpps; gpp++) {
Mika Westerberg919eb472017-06-06 16:18:17 +03001075 const struct intel_padgroup *padgrp = &community->gpps[gpp];
Mika Westerberg7981c0012015-03-30 17:31:49 +03001076 unsigned long pending, enabled, gpp_offset;
1077
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001078 pending = readl(community->regs + community->is_offset +
1079 padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001080 enabled = readl(community->regs + community->ie_offset +
Mika Westerberg919eb472017-06-06 16:18:17 +03001081 padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001082
1083 /* Only interrupts that are enabled */
1084 pending &= enabled;
1085
Mika Westerberg919eb472017-06-06 16:18:17 +03001086 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
Mika Westerberga60eac32017-11-27 16:54:43 +03001087 unsigned irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001088
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001089 irq = irq_find_mapping(gc->irq.domain,
Mika Westerberga60eac32017-11-27 16:54:43 +03001090 padgrp->gpio_base + gpp_offset);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001091 generic_handle_irq(irq);
Mika Westerberg193b40c2015-10-21 13:08:43 +03001092
1093 ret |= IRQ_HANDLED;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001094 }
1095 }
Mika Westerberg193b40c2015-10-21 13:08:43 +03001096
1097 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001098}
1099
Mika Westerberg193b40c2015-10-21 13:08:43 +03001100static irqreturn_t intel_gpio_irq(int irq, void *data)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001101{
Mika Westerberg193b40c2015-10-21 13:08:43 +03001102 const struct intel_community *community;
1103 struct intel_pinctrl *pctrl = data;
1104 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001105 int i;
1106
Mika Westerberg7981c0012015-03-30 17:31:49 +03001107 /* Need to check all communities for pending interrupts */
Mika Westerberg193b40c2015-10-21 13:08:43 +03001108 for (i = 0; i < pctrl->ncommunities; i++) {
1109 community = &pctrl->communities[i];
1110 ret |= intel_gpio_community_irq_handler(pctrl, community);
1111 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001112
Mika Westerberg193b40c2015-10-21 13:08:43 +03001113 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001114}
1115
1116static struct irq_chip intel_gpio_irqchip = {
1117 .name = "intel-gpio",
Andy Shevchenko55aedef52018-07-25 15:42:08 +03001118 .irq_request_resources = intel_gpio_irq_reqres,
1119 .irq_release_resources = intel_gpio_irq_relres,
Qi Zhenga939bb52016-03-17 02:15:26 +08001120 .irq_enable = intel_gpio_irq_enable,
Mika Westerberg7981c0012015-03-30 17:31:49 +03001121 .irq_ack = intel_gpio_irq_ack,
1122 .irq_mask = intel_gpio_irq_mask,
1123 .irq_unmask = intel_gpio_irq_unmask,
1124 .irq_set_type = intel_gpio_irq_type,
1125 .irq_set_wake = intel_gpio_irq_wake,
Rushikesh S Kadam5ff56b02017-08-11 13:53:44 +05301126 .flags = IRQCHIP_MASK_ON_SUSPEND,
Mika Westerberg7981c0012015-03-30 17:31:49 +03001127};
1128
Mika Westerberga60eac32017-11-27 16:54:43 +03001129static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1130 const struct intel_community *community)
1131{
Colin Ian King33b6cb52017-12-04 17:08:15 +00001132 int ret = 0, i;
Mika Westerberga60eac32017-11-27 16:54:43 +03001133
1134 for (i = 0; i < community->ngpps; i++) {
1135 const struct intel_padgroup *gpp = &community->gpps[i];
1136
1137 if (gpp->gpio_base < 0)
1138 continue;
1139
1140 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1141 gpp->gpio_base, gpp->base,
1142 gpp->size);
1143 if (ret)
1144 return ret;
1145 }
1146
1147 return ret;
1148}
1149
1150static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1151{
1152 const struct intel_community *community;
1153 unsigned ngpio = 0;
1154 int i, j;
1155
1156 for (i = 0; i < pctrl->ncommunities; i++) {
1157 community = &pctrl->communities[i];
1158 for (j = 0; j < community->ngpps; j++) {
1159 const struct intel_padgroup *gpp = &community->gpps[j];
1160
1161 if (gpp->gpio_base < 0)
1162 continue;
1163
1164 if (gpp->gpio_base + gpp->size > ngpio)
1165 ngpio = gpp->gpio_base + gpp->size;
1166 }
1167 }
1168
1169 return ngpio;
1170}
1171
Mika Westerberg7981c0012015-03-30 17:31:49 +03001172static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1173{
Mika Westerberga60eac32017-11-27 16:54:43 +03001174 int ret, i;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001175
1176 pctrl->chip = intel_gpio_chip;
1177
Mika Westerberga60eac32017-11-27 16:54:43 +03001178 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001179 pctrl->chip.label = dev_name(pctrl->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001180 pctrl->chip.parent = pctrl->dev;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001181 pctrl->chip.base = -1;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001182 pctrl->irq = irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001183
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001184 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001185 if (ret) {
1186 dev_err(pctrl->dev, "failed to register gpiochip\n");
1187 return ret;
1188 }
1189
Mika Westerberga60eac32017-11-27 16:54:43 +03001190 for (i = 0; i < pctrl->ncommunities; i++) {
1191 struct intel_community *community = &pctrl->communities[i];
1192
1193 ret = intel_gpio_add_pin_ranges(pctrl, community);
1194 if (ret) {
1195 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1196 return ret;
1197 }
Mika Westerberg193b40c2015-10-21 13:08:43 +03001198 }
1199
1200 /*
1201 * We need to request the interrupt here (instead of providing chip
1202 * to the irq directly) because on some platforms several GPIO
1203 * controllers share the same interrupt line.
1204 */
Mika Westerberg1a7d1cb2016-06-16 11:25:37 +03001205 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1206 IRQF_SHARED | IRQF_NO_THREAD,
Mika Westerberg193b40c2015-10-21 13:08:43 +03001207 dev_name(pctrl->dev), pctrl);
1208 if (ret) {
1209 dev_err(pctrl->dev, "failed to request interrupt\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001210 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001211 }
1212
1213 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
Andy Shevchenko3ae02c12016-11-25 13:31:16 +02001214 handle_bad_irq, IRQ_TYPE_NONE);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001215 if (ret) {
1216 dev_err(pctrl->dev, "failed to add irqchip\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001217 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001218 }
1219
1220 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
Mika Westerberg193b40c2015-10-21 13:08:43 +03001221 NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001222 return 0;
1223}
1224
Mika Westerberg919eb472017-06-06 16:18:17 +03001225static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1226 struct intel_community *community)
1227{
1228 struct intel_padgroup *gpps;
1229 unsigned npins = community->npins;
1230 unsigned padown_num = 0;
1231 size_t ngpps, i;
1232
1233 if (community->gpps)
1234 ngpps = community->ngpps;
1235 else
1236 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1237
1238 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1239 if (!gpps)
1240 return -ENOMEM;
1241
1242 for (i = 0; i < ngpps; i++) {
1243 if (community->gpps) {
1244 gpps[i] = community->gpps[i];
1245 } else {
1246 unsigned gpp_size = community->gpp_size;
1247
1248 gpps[i].reg_num = i;
1249 gpps[i].base = community->pin_base + i * gpp_size;
1250 gpps[i].size = min(gpp_size, npins);
1251 npins -= gpps[i].size;
1252 }
1253
1254 if (gpps[i].size > 32)
1255 return -EINVAL;
1256
Mika Westerberga60eac32017-11-27 16:54:43 +03001257 if (!gpps[i].gpio_base)
1258 gpps[i].gpio_base = gpps[i].base;
1259
Mika Westerberg919eb472017-06-06 16:18:17 +03001260 gpps[i].padown_num = padown_num;
1261
1262 /*
1263 * In older hardware the number of padown registers per
1264 * group is fixed regardless of the group size.
1265 */
1266 if (community->gpp_num_padown_regs)
1267 padown_num += community->gpp_num_padown_regs;
1268 else
1269 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1270 }
1271
1272 community->ngpps = ngpps;
1273 community->gpps = gpps;
1274
1275 return 0;
1276}
1277
Mika Westerberg7981c0012015-03-30 17:31:49 +03001278static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1279{
1280#ifdef CONFIG_PM_SLEEP
1281 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1282 struct intel_community_context *communities;
1283 struct intel_pad_context *pads;
1284 int i;
1285
1286 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1287 if (!pads)
1288 return -ENOMEM;
1289
1290 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1291 sizeof(*communities), GFP_KERNEL);
1292 if (!communities)
1293 return -ENOMEM;
1294
1295
1296 for (i = 0; i < pctrl->ncommunities; i++) {
1297 struct intel_community *community = &pctrl->communities[i];
1298 u32 *intmask;
1299
1300 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1301 sizeof(*intmask), GFP_KERNEL);
1302 if (!intmask)
1303 return -ENOMEM;
1304
1305 communities[i].intmask = intmask;
1306 }
1307
1308 pctrl->context.pads = pads;
1309 pctrl->context.communities = communities;
1310#endif
1311
1312 return 0;
1313}
1314
1315int intel_pinctrl_probe(struct platform_device *pdev,
1316 const struct intel_pinctrl_soc_data *soc_data)
1317{
1318 struct intel_pinctrl *pctrl;
1319 int i, ret, irq;
1320
1321 if (!soc_data)
1322 return -EINVAL;
1323
1324 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1325 if (!pctrl)
1326 return -ENOMEM;
1327
1328 pctrl->dev = &pdev->dev;
1329 pctrl->soc = soc_data;
Mika Westerberg27d90982016-06-16 11:25:36 +03001330 raw_spin_lock_init(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001331
1332 /*
1333 * Make a copy of the communities which we can use to hold pointers
1334 * to the registers.
1335 */
1336 pctrl->ncommunities = pctrl->soc->ncommunities;
1337 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1338 sizeof(*pctrl->communities), GFP_KERNEL);
1339 if (!pctrl->communities)
1340 return -ENOMEM;
1341
1342 for (i = 0; i < pctrl->ncommunities; i++) {
1343 struct intel_community *community = &pctrl->communities[i];
1344 struct resource *res;
1345 void __iomem *regs;
1346 u32 padbar;
1347
1348 *community = pctrl->soc->communities[i];
1349
1350 res = platform_get_resource(pdev, IORESOURCE_MEM,
1351 community->barno);
1352 regs = devm_ioremap_resource(&pdev->dev, res);
1353 if (IS_ERR(regs))
1354 return PTR_ERR(regs);
1355
Mika Westerberge57725e2017-01-27 13:07:14 +03001356 /*
1357 * Determine community features based on the revision if
1358 * not specified already.
1359 */
1360 if (!community->features) {
1361 u32 rev;
1362
1363 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001364 if (rev >= 0x94) {
Mika Westerberge57725e2017-01-27 13:07:14 +03001365 community->features |= PINCTRL_FEATURE_DEBOUNCE;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001366 community->features |= PINCTRL_FEATURE_1K_PD;
1367 }
Mika Westerberge57725e2017-01-27 13:07:14 +03001368 }
1369
Mika Westerberg7981c0012015-03-30 17:31:49 +03001370 /* Read offset of the pad configuration registers */
1371 padbar = readl(regs + PADBAR);
1372
1373 community->regs = regs;
1374 community->pad_regs = regs + padbar;
Mika Westerberg919eb472017-06-06 16:18:17 +03001375
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001376 if (!community->is_offset)
1377 community->is_offset = GPI_IS;
1378
Mika Westerberg919eb472017-06-06 16:18:17 +03001379 ret = intel_pinctrl_add_padgroups(pctrl, community);
1380 if (ret)
1381 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001382 }
1383
1384 irq = platform_get_irq(pdev, 0);
1385 if (irq < 0) {
1386 dev_err(&pdev->dev, "failed to get interrupt number\n");
1387 return irq;
1388 }
1389
1390 ret = intel_pinctrl_pm_init(pctrl);
1391 if (ret)
1392 return ret;
1393
1394 pctrl->pctldesc = intel_pinctrl_desc;
1395 pctrl->pctldesc.name = dev_name(&pdev->dev);
1396 pctrl->pctldesc.pins = pctrl->soc->pins;
1397 pctrl->pctldesc.npins = pctrl->soc->npins;
1398
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301399 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1400 pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001401 if (IS_ERR(pctrl->pctldev)) {
Mika Westerberg7981c0012015-03-30 17:31:49 +03001402 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001403 return PTR_ERR(pctrl->pctldev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001404 }
1405
1406 ret = intel_gpio_probe(pctrl, irq);
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301407 if (ret)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001408 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001409
1410 platform_set_drvdata(pdev, pctrl);
1411
1412 return 0;
1413}
1414EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1415
Mika Westerberg7981c0012015-03-30 17:31:49 +03001416#ifdef CONFIG_PM_SLEEP
Mika Westerbergc538b942016-10-10 16:39:31 +03001417static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1418{
1419 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1420
1421 if (!pd || !intel_pad_usable(pctrl, pin))
1422 return false;
1423
1424 /*
1425 * Only restore the pin if it is actually in use by the kernel (or
1426 * by userspace). It is possible that some pins are used by the
1427 * BIOS during resume and those are not always locked down so leave
1428 * them alone.
1429 */
1430 if (pd->mux_owner || pd->gpio_owner ||
1431 gpiochip_line_is_irq(&pctrl->chip, pin))
1432 return true;
1433
1434 return false;
1435}
1436
Mika Westerberg7981c0012015-03-30 17:31:49 +03001437int intel_pinctrl_suspend(struct device *dev)
1438{
1439 struct platform_device *pdev = to_platform_device(dev);
1440 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1441 struct intel_community_context *communities;
1442 struct intel_pad_context *pads;
1443 int i;
1444
1445 pads = pctrl->context.pads;
1446 for (i = 0; i < pctrl->soc->npins; i++) {
1447 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
Mika Westerberge57725e2017-01-27 13:07:14 +03001448 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001449 u32 val;
1450
Mika Westerbergc538b942016-10-10 16:39:31 +03001451 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001452 continue;
1453
1454 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1455 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1456 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1457 pads[i].padcfg1 = val;
Mika Westerberge57725e2017-01-27 13:07:14 +03001458
1459 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1460 if (padcfg)
1461 pads[i].padcfg2 = readl(padcfg);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001462 }
1463
1464 communities = pctrl->context.communities;
1465 for (i = 0; i < pctrl->ncommunities; i++) {
1466 struct intel_community *community = &pctrl->communities[i];
1467 void __iomem *base;
1468 unsigned gpp;
1469
1470 base = community->regs + community->ie_offset;
1471 for (gpp = 0; gpp < community->ngpps; gpp++)
1472 communities[i].intmask[gpp] = readl(base + gpp * 4);
1473 }
1474
1475 return 0;
1476}
1477EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1478
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001479static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1480{
1481 size_t i;
1482
1483 for (i = 0; i < pctrl->ncommunities; i++) {
1484 const struct intel_community *community;
1485 void __iomem *base;
1486 unsigned gpp;
1487
1488 community = &pctrl->communities[i];
1489 base = community->regs;
1490
1491 for (gpp = 0; gpp < community->ngpps; gpp++) {
1492 /* Mask and clear all interrupts */
1493 writel(0, base + community->ie_offset + gpp * 4);
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001494 writel(0xffff, base + community->is_offset + gpp * 4);
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001495 }
1496 }
1497}
1498
Mika Westerberg7981c0012015-03-30 17:31:49 +03001499int intel_pinctrl_resume(struct device *dev)
1500{
1501 struct platform_device *pdev = to_platform_device(dev);
1502 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1503 const struct intel_community_context *communities;
1504 const struct intel_pad_context *pads;
1505 int i;
1506
1507 /* Mask all interrupts */
1508 intel_gpio_irq_init(pctrl);
1509
1510 pads = pctrl->context.pads;
1511 for (i = 0; i < pctrl->soc->npins; i++) {
1512 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1513 void __iomem *padcfg;
1514 u32 val;
1515
Mika Westerbergc538b942016-10-10 16:39:31 +03001516 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001517 continue;
1518
1519 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1520 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1521 if (val != pads[i].padcfg0) {
1522 writel(pads[i].padcfg0, padcfg);
1523 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1524 desc->number, readl(padcfg));
1525 }
1526
1527 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1528 val = readl(padcfg);
1529 if (val != pads[i].padcfg1) {
1530 writel(pads[i].padcfg1, padcfg);
1531 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1532 desc->number, readl(padcfg));
1533 }
Mika Westerberge57725e2017-01-27 13:07:14 +03001534
1535 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1536 if (padcfg) {
1537 val = readl(padcfg);
1538 if (val != pads[i].padcfg2) {
1539 writel(pads[i].padcfg2, padcfg);
1540 dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1541 desc->number, readl(padcfg));
1542 }
1543 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001544 }
1545
1546 communities = pctrl->context.communities;
1547 for (i = 0; i < pctrl->ncommunities; i++) {
1548 struct intel_community *community = &pctrl->communities[i];
1549 void __iomem *base;
1550 unsigned gpp;
1551
1552 base = community->regs + community->ie_offset;
1553 for (gpp = 0; gpp < community->ngpps; gpp++) {
1554 writel(communities[i].intmask[gpp], base + gpp * 4);
1555 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1556 readl(base + gpp * 4));
1557 }
1558 }
1559
1560 return 0;
1561}
1562EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1563#endif
1564
1565MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1566MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1567MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1568MODULE_LICENSE("GPL v2");