blob: e05273a00ff2339df6f55407335b506326ea21cf [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
Andy Shevchenko924cf802018-08-30 19:27:36 +030010#include <linux/acpi.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030011#include <linux/gpio/driver.h>
Andy Shevchenko66c812d2019-10-25 12:10:28 +030012#include <linux/interrupt.h>
Mika Westerberge57725e2017-01-27 13:07:14 +030013#include <linux/log2.h>
Andy Shevchenko6a33a1d2019-08-07 16:41:50 +030014#include <linux/module.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030015#include <linux/platform_device.h>
Andy Shevchenko924cf802018-08-30 19:27:36 +030016#include <linux/property.h>
Andy Shevchenko6a33a1d2019-08-07 16:41:50 +030017#include <linux/time.h>
Andy Shevchenko924cf802018-08-30 19:27:36 +030018
Mika Westerberg7981c0012015-03-30 17:31:49 +030019#include <linux/pinctrl/pinctrl.h>
20#include <linux/pinctrl/pinmux.h>
21#include <linux/pinctrl/pinconf.h>
22#include <linux/pinctrl/pinconf-generic.h>
23
Mika Westerbergc538b942016-10-10 16:39:31 +030024#include "../core.h"
Mika Westerberg7981c0012015-03-30 17:31:49 +030025#include "pinctrl-intel.h"
26
Mika Westerberg7981c0012015-03-30 17:31:49 +030027/* Offset from regs */
Mika Westerberge57725e2017-01-27 13:07:14 +030028#define REVID 0x000
29#define REVID_SHIFT 16
30#define REVID_MASK GENMASK(31, 16)
31
Mika Westerberg7981c0012015-03-30 17:31:49 +030032#define PADBAR 0x00c
Mika Westerberg7981c0012015-03-30 17:31:49 +030033
34#define PADOWN_BITS 4
35#define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
Andy Shevchenkoe58926e2019-04-01 15:06:44 +030036#define PADOWN_MASK(p) (GENMASK(3, 0) << PADOWN_SHIFT(p))
Qipeng Zha99a735b2015-11-30 19:20:16 +080037#define PADOWN_GPP(p) ((p) / 8)
Mika Westerberg7981c0012015-03-30 17:31:49 +030038
39/* Offset from pad_regs */
40#define PADCFG0 0x000
41#define PADCFG0_RXEVCFG_SHIFT 25
Andy Shevchenkoe58926e2019-04-01 15:06:44 +030042#define PADCFG0_RXEVCFG_MASK GENMASK(26, 25)
Mika Westerberg7981c0012015-03-30 17:31:49 +030043#define PADCFG0_RXEVCFG_LEVEL 0
44#define PADCFG0_RXEVCFG_EDGE 1
45#define PADCFG0_RXEVCFG_DISABLED 2
46#define PADCFG0_RXEVCFG_EDGE_BOTH 3
Mika Westerberge57725e2017-01-27 13:07:14 +030047#define PADCFG0_PREGFRXSEL BIT(24)
Mika Westerberg7981c0012015-03-30 17:31:49 +030048#define PADCFG0_RXINV BIT(23)
49#define PADCFG0_GPIROUTIOXAPIC BIT(20)
50#define PADCFG0_GPIROUTSCI BIT(19)
51#define PADCFG0_GPIROUTSMI BIT(18)
52#define PADCFG0_GPIROUTNMI BIT(17)
53#define PADCFG0_PMODE_SHIFT 10
Andy Shevchenkoe58926e2019-04-01 15:06:44 +030054#define PADCFG0_PMODE_MASK GENMASK(13, 10)
Andy Shevchenko4973ddc2019-10-14 12:51:04 +030055#define PADCFG0_PMODE_GPIO 0
Mika Westerberg7981c0012015-03-30 17:31:49 +030056#define PADCFG0_GPIORXDIS BIT(9)
57#define PADCFG0_GPIOTXDIS BIT(8)
58#define PADCFG0_GPIORXSTATE BIT(1)
59#define PADCFG0_GPIOTXSTATE BIT(0)
60
61#define PADCFG1 0x004
62#define PADCFG1_TERM_UP BIT(13)
63#define PADCFG1_TERM_SHIFT 10
Andy Shevchenkoe58926e2019-04-01 15:06:44 +030064#define PADCFG1_TERM_MASK GENMASK(12, 10)
Mika Westerberg7981c0012015-03-30 17:31:49 +030065#define PADCFG1_TERM_20K 4
66#define PADCFG1_TERM_2K 3
67#define PADCFG1_TERM_5K 2
68#define PADCFG1_TERM_1K 1
69
Mika Westerberge57725e2017-01-27 13:07:14 +030070#define PADCFG2 0x008
71#define PADCFG2_DEBEN BIT(0)
72#define PADCFG2_DEBOUNCE_SHIFT 1
73#define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
74
Andy Shevchenko6a33a1d2019-08-07 16:41:50 +030075#define DEBOUNCE_PERIOD_NSEC 31250
Mika Westerberge57725e2017-01-27 13:07:14 +030076
Mika Westerberg7981c0012015-03-30 17:31:49 +030077struct intel_pad_context {
78 u32 padcfg0;
79 u32 padcfg1;
Mika Westerberge57725e2017-01-27 13:07:14 +030080 u32 padcfg2;
Mika Westerberg7981c0012015-03-30 17:31:49 +030081};
82
83struct intel_community_context {
84 u32 *intmask;
Chris Chiua0a5f762019-04-15 13:53:58 +080085 u32 *hostown;
Mika Westerberg7981c0012015-03-30 17:31:49 +030086};
87
Mika Westerberg7981c0012015-03-30 17:31:49 +030088#define pin_to_padno(c, p) ((p) - (c)->pin_base)
Mika Westerberg919eb472017-06-06 16:18:17 +030089#define padgroup_offset(g, p) ((p) - (g)->base)
Mika Westerberg7981c0012015-03-30 17:31:49 +030090
91static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
Andy Shevchenko04035f72018-09-26 17:50:26 +030092 unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +030093{
94 struct intel_community *community;
95 int i;
96
97 for (i = 0; i < pctrl->ncommunities; i++) {
98 community = &pctrl->communities[i];
99 if (pin >= community->pin_base &&
100 pin < community->pin_base + community->npins)
101 return community;
102 }
103
104 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
105 return NULL;
106}
107
Mika Westerberg919eb472017-06-06 16:18:17 +0300108static const struct intel_padgroup *
109intel_community_get_padgroup(const struct intel_community *community,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300110 unsigned int pin)
Mika Westerberg919eb472017-06-06 16:18:17 +0300111{
112 int i;
113
114 for (i = 0; i < community->ngpps; i++) {
115 const struct intel_padgroup *padgrp = &community->gpps[i];
116
117 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
118 return padgrp;
119 }
120
121 return NULL;
122}
123
Andy Shevchenko04035f72018-09-26 17:50:26 +0300124static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
125 unsigned int pin, unsigned int reg)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300126{
127 const struct intel_community *community;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300128 unsigned int padno;
Mika Westerberge57725e2017-01-27 13:07:14 +0300129 size_t nregs;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300130
131 community = intel_get_community(pctrl, pin);
132 if (!community)
133 return NULL;
134
135 padno = pin_to_padno(community, pin);
Mika Westerberge57725e2017-01-27 13:07:14 +0300136 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
137
Andy Shevchenko7eb7ecd2019-07-23 18:55:14 +0300138 if (reg >= nregs * 4)
Mika Westerberge57725e2017-01-27 13:07:14 +0300139 return NULL;
140
141 return community->pad_regs + reg + padno * nregs * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300142}
143
Andy Shevchenko04035f72018-09-26 17:50:26 +0300144static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300145{
146 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300147 const struct intel_padgroup *padgrp;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300148 unsigned int gpp, offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300149 void __iomem *padown;
150
151 community = intel_get_community(pctrl, pin);
152 if (!community)
153 return false;
154 if (!community->padown_offset)
155 return true;
156
Mika Westerberg919eb472017-06-06 16:18:17 +0300157 padgrp = intel_community_get_padgroup(community, pin);
158 if (!padgrp)
159 return false;
160
161 gpp_offset = padgroup_offset(padgrp, pin);
162 gpp = PADOWN_GPP(gpp_offset);
163 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300164 padown = community->regs + offset;
165
Mika Westerberg919eb472017-06-06 16:18:17 +0300166 return !(readl(padown) & PADOWN_MASK(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300167}
168
Andy Shevchenko04035f72018-09-26 17:50:26 +0300169static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300170{
171 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300172 const struct intel_padgroup *padgrp;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300173 unsigned int offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300174 void __iomem *hostown;
175
176 community = intel_get_community(pctrl, pin);
177 if (!community)
178 return true;
179 if (!community->hostown_offset)
180 return false;
181
Mika Westerberg919eb472017-06-06 16:18:17 +0300182 padgrp = intel_community_get_padgroup(community, pin);
183 if (!padgrp)
184 return true;
185
186 gpp_offset = padgroup_offset(padgrp, pin);
187 offset = community->hostown_offset + padgrp->reg_num * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300188 hostown = community->regs + offset;
189
Mika Westerberg919eb472017-06-06 16:18:17 +0300190 return !(readl(hostown) & BIT(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300191}
192
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300193/**
194 * enum - Locking variants of the pad configuration
195 *
196 * @PAD_UNLOCKED: pad is fully controlled by the configuration registers
197 * @PAD_LOCKED: pad configuration registers, except TX state, are locked
198 * @PAD_LOCKED_TX: pad configuration TX state is locked
199 * @PAD_LOCKED_FULL: pad configuration registers are locked completely
200 *
201 * Locking is considered as read-only mode for corresponding registers and
202 * their respective fields. That said, TX state bit is locked separately from
203 * the main locking scheme.
204 */
205enum {
206 PAD_UNLOCKED = 0,
207 PAD_LOCKED = 1,
208 PAD_LOCKED_TX = 2,
209 PAD_LOCKED_FULL = PAD_LOCKED | PAD_LOCKED_TX,
210};
211
212static int intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300213{
214 struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300215 const struct intel_padgroup *padgrp;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300216 unsigned int offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300217 u32 value;
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300218 int ret = PAD_UNLOCKED;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300219
220 community = intel_get_community(pctrl, pin);
221 if (!community)
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300222 return PAD_LOCKED_FULL;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300223 if (!community->padcfglock_offset)
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300224 return PAD_UNLOCKED;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300225
Mika Westerberg919eb472017-06-06 16:18:17 +0300226 padgrp = intel_community_get_padgroup(community, pin);
227 if (!padgrp)
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300228 return PAD_LOCKED_FULL;
Mika Westerberg919eb472017-06-06 16:18:17 +0300229
230 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300231
232 /*
233 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
234 * the pad is considered unlocked. Any other case means that it is
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300235 * either fully or partially locked.
Mika Westerberg7981c0012015-03-30 17:31:49 +0300236 */
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300237 offset = community->padcfglock_offset + 0 + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300238 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300239 if (value & BIT(gpp_offset))
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300240 ret |= PAD_LOCKED;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300241
Mika Westerberg919eb472017-06-06 16:18:17 +0300242 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300243 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300244 if (value & BIT(gpp_offset))
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300245 ret |= PAD_LOCKED_TX;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300246
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300247 return ret;
248}
249
250static bool intel_pad_is_unlocked(struct intel_pinctrl *pctrl, unsigned int pin)
251{
252 return (intel_pad_locked(pctrl, pin) & PAD_LOCKED) == PAD_UNLOCKED;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300253}
254
Andy Shevchenko04035f72018-09-26 17:50:26 +0300255static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300256{
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300257 return intel_pad_owned_by_host(pctrl, pin) && intel_pad_is_unlocked(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300258}
259
260static int intel_get_groups_count(struct pinctrl_dev *pctldev)
261{
262 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
263
264 return pctrl->soc->ngroups;
265}
266
267static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300268 unsigned int group)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300269{
270 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
271
272 return pctrl->soc->groups[group].name;
273}
274
Andy Shevchenko04035f72018-09-26 17:50:26 +0300275static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
276 const unsigned int **pins, unsigned int *npins)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300277{
278 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
279
280 *pins = pctrl->soc->groups[group].pins;
281 *npins = pctrl->soc->groups[group].npins;
282 return 0;
283}
284
285static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300286 unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300287{
288 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
Mika Westerberge57725e2017-01-27 13:07:14 +0300289 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300290 u32 cfg0, cfg1, mode;
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300291 int locked;
292 bool acpi;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300293
294 if (!intel_pad_owned_by_host(pctrl, pin)) {
295 seq_puts(s, "not available");
296 return;
297 }
298
299 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
300 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
301
302 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
Andy Shevchenko4973ddc2019-10-14 12:51:04 +0300303 if (mode == PADCFG0_PMODE_GPIO)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300304 seq_puts(s, "GPIO ");
305 else
306 seq_printf(s, "mode %d ", mode);
307
308 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
309
Mika Westerberge57725e2017-01-27 13:07:14 +0300310 /* Dump the additional PADCFG registers if available */
311 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
312 if (padcfg)
313 seq_printf(s, " 0x%08x", readl(padcfg));
314
Mika Westerberg7981c0012015-03-30 17:31:49 +0300315 locked = intel_pad_locked(pctrl, pin);
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300316 acpi = intel_pad_acpi_mode(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300317
318 if (locked || acpi) {
319 seq_puts(s, " [");
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300320 if (locked)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300321 seq_puts(s, "LOCKED");
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300322 if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_TX)
323 seq_puts(s, " tx");
324 else if ((locked & PAD_LOCKED_FULL) == PAD_LOCKED_FULL)
325 seq_puts(s, " full");
326
327 if (locked && acpi)
328 seq_puts(s, ", ");
329
Mika Westerberg7981c0012015-03-30 17:31:49 +0300330 if (acpi)
331 seq_puts(s, "ACPI");
332 seq_puts(s, "]");
333 }
334}
335
336static const struct pinctrl_ops intel_pinctrl_ops = {
337 .get_groups_count = intel_get_groups_count,
338 .get_group_name = intel_get_group_name,
339 .get_group_pins = intel_get_group_pins,
340 .pin_dbg_show = intel_pin_dbg_show,
341};
342
343static int intel_get_functions_count(struct pinctrl_dev *pctldev)
344{
345 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
346
347 return pctrl->soc->nfunctions;
348}
349
350static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300351 unsigned int function)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300352{
353 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
354
355 return pctrl->soc->functions[function].name;
356}
357
358static int intel_get_function_groups(struct pinctrl_dev *pctldev,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300359 unsigned int function,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300360 const char * const **groups,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300361 unsigned int * const ngroups)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300362{
363 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
364
365 *groups = pctrl->soc->functions[function].groups;
366 *ngroups = pctrl->soc->functions[function].ngroups;
367 return 0;
368}
369
Andy Shevchenko04035f72018-09-26 17:50:26 +0300370static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev,
371 unsigned int function, unsigned int group)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300372{
373 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
374 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
375 unsigned long flags;
376 int i;
377
Mika Westerberg27d90982016-06-16 11:25:36 +0300378 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300379
380 /*
381 * All pins in the groups needs to be accessible and writable
382 * before we can enable the mux for this group.
383 */
384 for (i = 0; i < grp->npins; i++) {
385 if (!intel_pad_usable(pctrl, grp->pins[i])) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300386 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300387 return -EBUSY;
388 }
389 }
390
391 /* Now enable the mux setting for each pin in the group */
392 for (i = 0; i < grp->npins; i++) {
393 void __iomem *padcfg0;
394 u32 value;
395
396 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
397 value = readl(padcfg0);
398
399 value &= ~PADCFG0_PMODE_MASK;
Mika Westerberg1f6b4192017-06-06 16:18:18 +0300400
401 if (grp->modes)
402 value |= grp->modes[i] << PADCFG0_PMODE_SHIFT;
403 else
404 value |= grp->mode << PADCFG0_PMODE_SHIFT;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300405
406 writel(value, padcfg0);
407 }
408
Mika Westerberg27d90982016-06-16 11:25:36 +0300409 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300410
411 return 0;
412}
413
Andy Shevchenko17fab472017-01-02 14:07:22 +0200414static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
415{
416 u32 value;
417
418 value = readl(padcfg0);
419 if (input) {
420 value &= ~PADCFG0_GPIORXDIS;
421 value |= PADCFG0_GPIOTXDIS;
422 } else {
423 value &= ~PADCFG0_GPIOTXDIS;
424 value |= PADCFG0_GPIORXDIS;
425 }
426 writel(value, padcfg0);
427}
428
Andy Shevchenko4973ddc2019-10-14 12:51:04 +0300429static int intel_gpio_get_gpio_mode(void __iomem *padcfg0)
430{
431 return (readl(padcfg0) & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
432}
433
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300434static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
435{
436 u32 value;
437
Andy Shevchenkoaf7e3ee2020-06-12 17:49:54 +0300438 value = readl(padcfg0);
439
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300440 /* Put the pad into GPIO mode */
Andy Shevchenkoaf7e3ee2020-06-12 17:49:54 +0300441 value &= ~PADCFG0_PMODE_MASK;
442 value |= PADCFG0_PMODE_GPIO;
443
444 /* Disable input and output buffers */
445 value &= ~PADCFG0_GPIORXDIS;
446 value &= ~PADCFG0_GPIOTXDIS;
447
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300448 /* Disable SCI/SMI/NMI generation */
449 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
450 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
Andy Shevchenkoaf7e3ee2020-06-12 17:49:54 +0300451
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300452 writel(value, padcfg0);
453}
454
Mika Westerberg7981c0012015-03-30 17:31:49 +0300455static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
456 struct pinctrl_gpio_range *range,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300457 unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300458{
459 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
460 void __iomem *padcfg0;
461 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300462
Andy Shevchenkof62cdde2020-06-12 17:49:55 +0300463 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
464
Mika Westerberg27d90982016-06-16 11:25:36 +0300465 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300466
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300467 if (!intel_pad_owned_by_host(pctrl, pin)) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300468 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300469 return -EBUSY;
470 }
471
Andy Shevchenko1bd23152019-08-12 19:14:01 +0300472 if (!intel_pad_is_unlocked(pctrl, pin)) {
473 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
474 return 0;
475 }
476
Andy Shevchenko4973ddc2019-10-14 12:51:04 +0300477 /*
478 * If pin is already configured in GPIO mode, we assume that
479 * firmware provides correct settings. In such case we avoid
480 * potential glitches on the pin. Otherwise, for the pin in
481 * alternative mode, consumer has to supply respective flags.
482 */
483 if (intel_gpio_get_gpio_mode(padcfg0) == PADCFG0_PMODE_GPIO) {
484 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
485 return 0;
486 }
487
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300488 intel_gpio_set_gpio_mode(padcfg0);
Andy Shevchenko4973ddc2019-10-14 12:51:04 +0300489
Andy Shevchenko17fab472017-01-02 14:07:22 +0200490 /* Disable TX buffer and enable RX (this will be input) */
491 __intel_gpio_set_direction(padcfg0, true);
492
Mika Westerberg27d90982016-06-16 11:25:36 +0300493 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300494
495 return 0;
496}
497
498static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
499 struct pinctrl_gpio_range *range,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300500 unsigned int pin, bool input)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300501{
502 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
503 void __iomem *padcfg0;
504 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300505
Mika Westerberg7981c0012015-03-30 17:31:49 +0300506 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300507
Andy Shevchenkof62cdde2020-06-12 17:49:55 +0300508 raw_spin_lock_irqsave(&pctrl->lock, flags);
509 __intel_gpio_set_direction(padcfg0, input);
Mika Westerberg27d90982016-06-16 11:25:36 +0300510 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300511
512 return 0;
513}
514
515static const struct pinmux_ops intel_pinmux_ops = {
516 .get_functions_count = intel_get_functions_count,
517 .get_function_name = intel_get_function_name,
518 .get_function_groups = intel_get_function_groups,
519 .set_mux = intel_pinmux_set_mux,
520 .gpio_request_enable = intel_gpio_request_enable,
521 .gpio_set_direction = intel_gpio_set_direction,
522};
523
Andy Shevchenko04035f72018-09-26 17:50:26 +0300524static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300525 unsigned long *config)
526{
527 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
528 enum pin_config_param param = pinconf_to_config_param(*config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300529 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300530 u32 value, term;
Mika Westerberge57725e2017-01-27 13:07:14 +0300531 u32 arg = 0;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300532
533 if (!intel_pad_owned_by_host(pctrl, pin))
534 return -ENOTSUPP;
535
Mika Westerberg04cc0582017-01-27 13:07:15 +0300536 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300537 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
538 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
539
540 switch (param) {
541 case PIN_CONFIG_BIAS_DISABLE:
542 if (term)
543 return -EINVAL;
544 break;
545
546 case PIN_CONFIG_BIAS_PULL_UP:
547 if (!term || !(value & PADCFG1_TERM_UP))
548 return -EINVAL;
549
550 switch (term) {
551 case PADCFG1_TERM_1K:
552 arg = 1000;
553 break;
554 case PADCFG1_TERM_2K:
555 arg = 2000;
556 break;
557 case PADCFG1_TERM_5K:
558 arg = 5000;
559 break;
560 case PADCFG1_TERM_20K:
561 arg = 20000;
562 break;
563 }
564
565 break;
566
567 case PIN_CONFIG_BIAS_PULL_DOWN:
568 if (!term || value & PADCFG1_TERM_UP)
569 return -EINVAL;
570
571 switch (term) {
Mika Westerberg04cc0582017-01-27 13:07:15 +0300572 case PADCFG1_TERM_1K:
573 if (!(community->features & PINCTRL_FEATURE_1K_PD))
574 return -EINVAL;
575 arg = 1000;
576 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300577 case PADCFG1_TERM_5K:
578 arg = 5000;
579 break;
580 case PADCFG1_TERM_20K:
581 arg = 20000;
582 break;
583 }
584
585 break;
586
Mika Westerberge57725e2017-01-27 13:07:14 +0300587 case PIN_CONFIG_INPUT_DEBOUNCE: {
588 void __iomem *padcfg2;
589 u32 v;
590
591 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
592 if (!padcfg2)
593 return -ENOTSUPP;
594
595 v = readl(padcfg2);
596 if (!(v & PADCFG2_DEBEN))
597 return -EINVAL;
598
599 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
Andy Shevchenko6a33a1d2019-08-07 16:41:50 +0300600 arg = BIT(v) * DEBOUNCE_PERIOD_NSEC / NSEC_PER_USEC;
Mika Westerberge57725e2017-01-27 13:07:14 +0300601
602 break;
603 }
604
Mika Westerberg7981c0012015-03-30 17:31:49 +0300605 default:
606 return -ENOTSUPP;
607 }
608
609 *config = pinconf_to_config_packed(param, arg);
610 return 0;
611}
612
Andy Shevchenko04035f72018-09-26 17:50:26 +0300613static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300614 unsigned long config)
615{
Andy Shevchenko04035f72018-09-26 17:50:26 +0300616 unsigned int param = pinconf_to_config_param(config);
617 unsigned int arg = pinconf_to_config_argument(config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300618 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300619 void __iomem *padcfg1;
620 unsigned long flags;
621 int ret = 0;
622 u32 value;
623
Mika Westerberg04cc0582017-01-27 13:07:15 +0300624 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300625 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
Andy Shevchenkof62cdde2020-06-12 17:49:55 +0300626
627 raw_spin_lock_irqsave(&pctrl->lock, flags);
628
Mika Westerberg7981c0012015-03-30 17:31:49 +0300629 value = readl(padcfg1);
630
631 switch (param) {
632 case PIN_CONFIG_BIAS_DISABLE:
633 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
634 break;
635
636 case PIN_CONFIG_BIAS_PULL_UP:
637 value &= ~PADCFG1_TERM_MASK;
638
639 value |= PADCFG1_TERM_UP;
640
641 switch (arg) {
642 case 20000:
643 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
644 break;
645 case 5000:
646 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
647 break;
648 case 2000:
649 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
650 break;
651 case 1000:
652 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
653 break;
654 default:
655 ret = -EINVAL;
656 }
657
658 break;
659
660 case PIN_CONFIG_BIAS_PULL_DOWN:
661 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
662
663 switch (arg) {
664 case 20000:
665 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
666 break;
667 case 5000:
668 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
669 break;
Mika Westerberg04cc0582017-01-27 13:07:15 +0300670 case 1000:
Dan Carpenteraa1dd802017-02-07 16:20:08 +0300671 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
672 ret = -EINVAL;
673 break;
674 }
Mika Westerberg04cc0582017-01-27 13:07:15 +0300675 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
676 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300677 default:
678 ret = -EINVAL;
679 }
680
681 break;
682 }
683
684 if (!ret)
685 writel(value, padcfg1);
686
Mika Westerberg27d90982016-06-16 11:25:36 +0300687 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300688
689 return ret;
690}
691
Andy Shevchenko04035f72018-09-26 17:50:26 +0300692static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
693 unsigned int pin, unsigned int debounce)
Mika Westerberge57725e2017-01-27 13:07:14 +0300694{
695 void __iomem *padcfg0, *padcfg2;
696 unsigned long flags;
697 u32 value0, value2;
698 int ret = 0;
699
700 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
701 if (!padcfg2)
702 return -ENOTSUPP;
703
704 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
705
706 raw_spin_lock_irqsave(&pctrl->lock, flags);
707
708 value0 = readl(padcfg0);
709 value2 = readl(padcfg2);
710
711 /* Disable glitch filter and debouncer */
712 value0 &= ~PADCFG0_PREGFRXSEL;
713 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
714
715 if (debounce) {
716 unsigned long v;
717
Andy Shevchenko6a33a1d2019-08-07 16:41:50 +0300718 v = order_base_2(debounce * NSEC_PER_USEC / DEBOUNCE_PERIOD_NSEC);
Mika Westerberge57725e2017-01-27 13:07:14 +0300719 if (v < 3 || v > 15) {
720 ret = -EINVAL;
721 goto exit_unlock;
722 } else {
723 /* Enable glitch filter and debouncer */
724 value0 |= PADCFG0_PREGFRXSEL;
725 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
726 value2 |= PADCFG2_DEBEN;
727 }
728 }
729
730 writel(value0, padcfg0);
731 writel(value2, padcfg2);
732
733exit_unlock:
734 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
735
736 return ret;
737}
738
Andy Shevchenko04035f72018-09-26 17:50:26 +0300739static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
740 unsigned long *configs, unsigned int nconfigs)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300741{
742 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
743 int i, ret;
744
745 if (!intel_pad_usable(pctrl, pin))
746 return -ENOTSUPP;
747
748 for (i = 0; i < nconfigs; i++) {
749 switch (pinconf_to_config_param(configs[i])) {
750 case PIN_CONFIG_BIAS_DISABLE:
751 case PIN_CONFIG_BIAS_PULL_UP:
752 case PIN_CONFIG_BIAS_PULL_DOWN:
753 ret = intel_config_set_pull(pctrl, pin, configs[i]);
754 if (ret)
755 return ret;
756 break;
757
Mika Westerberge57725e2017-01-27 13:07:14 +0300758 case PIN_CONFIG_INPUT_DEBOUNCE:
759 ret = intel_config_set_debounce(pctrl, pin,
760 pinconf_to_config_argument(configs[i]));
761 if (ret)
762 return ret;
763 break;
764
Mika Westerberg7981c0012015-03-30 17:31:49 +0300765 default:
766 return -ENOTSUPP;
767 }
768 }
769
770 return 0;
771}
772
773static const struct pinconf_ops intel_pinconf_ops = {
774 .is_generic = true,
775 .pin_config_get = intel_config_get,
776 .pin_config_set = intel_config_set,
777};
778
779static const struct pinctrl_desc intel_pinctrl_desc = {
780 .pctlops = &intel_pinctrl_ops,
781 .pmxops = &intel_pinmux_ops,
782 .confops = &intel_pinconf_ops,
783 .owner = THIS_MODULE,
784};
785
Mika Westerberga60eac32017-11-27 16:54:43 +0300786/**
787 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
788 * @pctrl: Pinctrl structure
789 * @offset: GPIO offset from gpiolib
Andy Shevchenko946ffef2018-09-26 17:43:17 +0300790 * @community: Community is filled here if not %NULL
Mika Westerberga60eac32017-11-27 16:54:43 +0300791 * @padgrp: Pad group is filled here if not %NULL
792 *
793 * When coming through gpiolib irqchip, the GPIO offset is not
794 * automatically translated to pinctrl pin number. This function can be
795 * used to find out the corresponding pinctrl pin.
796 */
Andy Shevchenko04035f72018-09-26 17:50:26 +0300797static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
Mika Westerberga60eac32017-11-27 16:54:43 +0300798 const struct intel_community **community,
799 const struct intel_padgroup **padgrp)
800{
801 int i;
802
803 for (i = 0; i < pctrl->ncommunities; i++) {
804 const struct intel_community *comm = &pctrl->communities[i];
805 int j;
806
807 for (j = 0; j < comm->ngpps; j++) {
808 const struct intel_padgroup *pgrp = &comm->gpps[j];
809
Andy Shevchenkoe5a4ab62020-04-13 14:18:20 +0300810 if (pgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
Mika Westerberga60eac32017-11-27 16:54:43 +0300811 continue;
812
813 if (offset >= pgrp->gpio_base &&
814 offset < pgrp->gpio_base + pgrp->size) {
815 int pin;
816
817 pin = pgrp->base + offset - pgrp->gpio_base;
818 if (community)
819 *community = comm;
820 if (padgrp)
821 *padgrp = pgrp;
822
823 return pin;
824 }
825 }
826 }
827
828 return -EINVAL;
829}
830
Chris Chiu6cb08802019-08-16 17:38:38 +0800831/**
832 * intel_pin_to_gpio() - Translate from pin number to GPIO offset
833 * @pctrl: Pinctrl structure
834 * @pin: pin number
835 *
836 * Translate the pin number of pinctrl to GPIO offset
837 */
Arnd Bergmann55dac432019-09-06 20:51:59 +0200838static __maybe_unused int intel_pin_to_gpio(struct intel_pinctrl *pctrl, int pin)
Chris Chiu6cb08802019-08-16 17:38:38 +0800839{
840 const struct intel_community *community;
841 const struct intel_padgroup *padgrp;
842
843 community = intel_get_community(pctrl, pin);
844 if (!community)
845 return -EINVAL;
846
847 padgrp = intel_community_get_padgroup(community, pin);
848 if (!padgrp)
849 return -EINVAL;
850
851 return pin - padgrp->base + padgrp->gpio_base;
852}
853
Andy Shevchenko04035f72018-09-26 17:50:26 +0300854static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
Andy Shevchenko55aedef52018-07-25 15:42:08 +0300855{
Mika Westerberg96147db2018-09-18 18:36:21 +0300856 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
857 void __iomem *reg;
858 u32 padcfg0;
Andy Shevchenko55aedef52018-07-25 15:42:08 +0300859 int pin;
860
Mika Westerberg96147db2018-09-18 18:36:21 +0300861 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
862 if (pin < 0)
863 return -EINVAL;
864
865 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
866 if (!reg)
867 return -EINVAL;
868
869 padcfg0 = readl(reg);
870 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
871 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
872
873 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
Andy Shevchenko55aedef52018-07-25 15:42:08 +0300874}
875
Andy Shevchenko04035f72018-09-26 17:50:26 +0300876static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
877 int value)
Mika Westerberg96147db2018-09-18 18:36:21 +0300878{
879 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
880 unsigned long flags;
881 void __iomem *reg;
882 u32 padcfg0;
883 int pin;
884
885 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
886 if (pin < 0)
887 return;
888
889 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
890 if (!reg)
891 return;
892
893 raw_spin_lock_irqsave(&pctrl->lock, flags);
894 padcfg0 = readl(reg);
895 if (value)
896 padcfg0 |= PADCFG0_GPIOTXSTATE;
897 else
898 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
899 writel(padcfg0, reg);
900 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
901}
902
903static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
904{
905 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
906 void __iomem *reg;
907 u32 padcfg0;
908 int pin;
909
910 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
911 if (pin < 0)
912 return -EINVAL;
913
914 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
915 if (!reg)
916 return -EINVAL;
917
918 padcfg0 = readl(reg);
919
920 if (padcfg0 & PADCFG0_PMODE_MASK)
921 return -EINVAL;
922
Matti Vaittinen6a304752019-12-12 08:34:32 +0200923 if (padcfg0 & PADCFG0_GPIOTXDIS)
924 return GPIO_LINE_DIRECTION_IN;
925
926 return GPIO_LINE_DIRECTION_OUT;
Mika Westerberg96147db2018-09-18 18:36:21 +0300927}
928
Andy Shevchenko04035f72018-09-26 17:50:26 +0300929static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
Mika Westerberg96147db2018-09-18 18:36:21 +0300930{
931 return pinctrl_gpio_direction_input(chip->base + offset);
932}
933
Andy Shevchenko04035f72018-09-26 17:50:26 +0300934static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
Mika Westerberg96147db2018-09-18 18:36:21 +0300935 int value)
936{
937 intel_gpio_set(chip, offset, value);
938 return pinctrl_gpio_direction_output(chip->base + offset);
939}
940
941static const struct gpio_chip intel_gpio_chip = {
942 .owner = THIS_MODULE,
943 .request = gpiochip_generic_request,
944 .free = gpiochip_generic_free,
945 .get_direction = intel_gpio_get_direction,
946 .direction_input = intel_gpio_direction_input,
947 .direction_output = intel_gpio_direction_output,
948 .get = intel_gpio_get,
949 .set = intel_gpio_set,
950 .set_config = gpiochip_generic_config,
951};
952
Mika Westerberg7981c0012015-03-30 17:31:49 +0300953static void intel_gpio_irq_ack(struct irq_data *d)
954{
955 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100956 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300957 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300958 const struct intel_padgroup *padgrp;
959 int pin;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300960
Mika Westerberga60eac32017-11-27 16:54:43 +0300961 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
962 if (pin >= 0) {
Andy Shevchenko04035f72018-09-26 17:50:26 +0300963 unsigned int gpp, gpp_offset, is_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300964
Mika Westerberg919eb472017-06-06 16:18:17 +0300965 gpp = padgrp->reg_num;
966 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300967 is_offset = community->is_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300968
969 raw_spin_lock(&pctrl->lock);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300970 writel(BIT(gpp_offset), community->regs + is_offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300971 raw_spin_unlock(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300972 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300973}
974
975static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
976{
977 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100978 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300979 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300980 const struct intel_padgroup *padgrp;
981 int pin;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300982
Mika Westerberga60eac32017-11-27 16:54:43 +0300983 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
984 if (pin >= 0) {
Andy Shevchenko04035f72018-09-26 17:50:26 +0300985 unsigned int gpp, gpp_offset;
Mika Westerberg919eb472017-06-06 16:18:17 +0300986 unsigned long flags;
Kai-Heng Feng670784f2019-04-30 16:37:53 +0800987 void __iomem *reg, *is;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300988 u32 value;
989
Mika Westerberg919eb472017-06-06 16:18:17 +0300990 gpp = padgrp->reg_num;
991 gpp_offset = padgroup_offset(padgrp, pin);
992
Mika Westerberg7981c0012015-03-30 17:31:49 +0300993 reg = community->regs + community->ie_offset + gpp * 4;
Kai-Heng Feng670784f2019-04-30 16:37:53 +0800994 is = community->regs + community->is_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300995
996 raw_spin_lock_irqsave(&pctrl->lock, flags);
Kai-Heng Feng670784f2019-04-30 16:37:53 +0800997
998 /* Clear interrupt status first to avoid unexpected interrupt */
999 writel(BIT(gpp_offset), is);
1000
Mika Westerberg7981c0012015-03-30 17:31:49 +03001001 value = readl(reg);
1002 if (mask)
1003 value &= ~BIT(gpp_offset);
1004 else
1005 value |= BIT(gpp_offset);
1006 writel(value, reg);
Mika Westerberg919eb472017-06-06 16:18:17 +03001007 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001008 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001009}
1010
1011static void intel_gpio_irq_mask(struct irq_data *d)
1012{
1013 intel_gpio_irq_mask_unmask(d, true);
1014}
1015
1016static void intel_gpio_irq_unmask(struct irq_data *d)
1017{
1018 intel_gpio_irq_mask_unmask(d, false);
1019}
1020
Andy Shevchenko04035f72018-09-26 17:50:26 +03001021static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001022{
1023 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +01001024 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Andy Shevchenko04035f72018-09-26 17:50:26 +03001025 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001026 unsigned long flags;
1027 void __iomem *reg;
1028 u32 value;
1029
1030 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
1031 if (!reg)
1032 return -EINVAL;
1033
Mika Westerberg4341e8a2015-10-21 13:08:44 +03001034 /*
1035 * If the pin is in ACPI mode it is still usable as a GPIO but it
1036 * cannot be used as IRQ because GPI_IS status bit will not be
1037 * updated by the host controller hardware.
1038 */
1039 if (intel_pad_acpi_mode(pctrl, pin)) {
1040 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1041 return -EPERM;
1042 }
1043
Mika Westerberg27d90982016-06-16 11:25:36 +03001044 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001045
Mika Westerbergf5a26ac2017-11-29 16:25:44 +03001046 intel_gpio_set_gpio_mode(reg);
1047
Andy Shevchenkoaf7e3ee2020-06-12 17:49:54 +03001048 /* Disable TX buffer and enable RX (this will be input) */
1049 __intel_gpio_set_direction(reg, true);
1050
Mika Westerberg7981c0012015-03-30 17:31:49 +03001051 value = readl(reg);
1052
1053 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1054
1055 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1056 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1057 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1058 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1059 value |= PADCFG0_RXINV;
1060 } else if (type & IRQ_TYPE_EDGE_RISING) {
1061 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
Qipeng Zhabf380cf2016-03-17 02:15:25 +08001062 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1063 if (type & IRQ_TYPE_LEVEL_LOW)
1064 value |= PADCFG0_RXINV;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001065 } else {
1066 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1067 }
1068
1069 writel(value, reg);
1070
1071 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +02001072 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001073 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +02001074 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001075
Mika Westerberg27d90982016-06-16 11:25:36 +03001076 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001077
1078 return 0;
1079}
1080
1081static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1082{
1083 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +01001084 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Andy Shevchenko04035f72018-09-26 17:50:26 +03001085 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001086
Mika Westerberg7981c0012015-03-30 17:31:49 +03001087 if (on)
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001088 enable_irq_wake(pctrl->irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001089 else
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001090 disable_irq_wake(pctrl->irq);
Andy Shevchenko9a520fd2016-07-08 14:30:46 +03001091
Mika Westerberg7981c0012015-03-30 17:31:49 +03001092 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1093 return 0;
1094}
1095
Andy Shevchenko86851bb2020-06-12 17:49:56 +03001096static int intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
1097 const struct intel_community *community)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001098{
Mika Westerberg193b40c2015-10-21 13:08:43 +03001099 struct gpio_chip *gc = &pctrl->chip;
Andy Shevchenko86851bb2020-06-12 17:49:56 +03001100 unsigned int gpp;
1101 int ret = 0;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001102
1103 for (gpp = 0; gpp < community->ngpps; gpp++) {
Mika Westerberg919eb472017-06-06 16:18:17 +03001104 const struct intel_padgroup *padgrp = &community->gpps[gpp];
Mika Westerberg7981c0012015-03-30 17:31:49 +03001105 unsigned long pending, enabled, gpp_offset;
1106
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001107 pending = readl(community->regs + community->is_offset +
1108 padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001109 enabled = readl(community->regs + community->ie_offset +
Mika Westerberg919eb472017-06-06 16:18:17 +03001110 padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001111
1112 /* Only interrupts that are enabled */
1113 pending &= enabled;
1114
Mika Westerberg919eb472017-06-06 16:18:17 +03001115 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
Andy Shevchenko11b389c2019-11-06 16:39:48 +02001116 unsigned int irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001117
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001118 irq = irq_find_mapping(gc->irq.domain,
Mika Westerberga60eac32017-11-27 16:54:43 +03001119 padgrp->gpio_base + gpp_offset);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001120 generic_handle_irq(irq);
1121 }
Andy Shevchenko86851bb2020-06-12 17:49:56 +03001122
1123 ret += pending ? 1 : 0;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001124 }
Mika Westerberg193b40c2015-10-21 13:08:43 +03001125
1126 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001127}
1128
Mika Westerberg193b40c2015-10-21 13:08:43 +03001129static irqreturn_t intel_gpio_irq(int irq, void *data)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001130{
Mika Westerberg193b40c2015-10-21 13:08:43 +03001131 const struct intel_community *community;
1132 struct intel_pinctrl *pctrl = data;
Andy Shevchenko86851bb2020-06-12 17:49:56 +03001133 unsigned int i;
1134 int ret = 0;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001135
Mika Westerberg7981c0012015-03-30 17:31:49 +03001136 /* Need to check all communities for pending interrupts */
Mika Westerberg193b40c2015-10-21 13:08:43 +03001137 for (i = 0; i < pctrl->ncommunities; i++) {
1138 community = &pctrl->communities[i];
Andy Shevchenko86851bb2020-06-12 17:49:56 +03001139 ret += intel_gpio_community_irq_handler(pctrl, community);
Mika Westerberg193b40c2015-10-21 13:08:43 +03001140 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001141
Andy Shevchenko86851bb2020-06-12 17:49:56 +03001142 return IRQ_RETVAL(ret);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001143}
1144
Linus Walleij6d416b92020-01-09 08:53:28 +01001145static int intel_gpio_add_community_ranges(struct intel_pinctrl *pctrl,
1146 const struct intel_community *community)
Mika Westerberga60eac32017-11-27 16:54:43 +03001147{
Colin Ian King33b6cb52017-12-04 17:08:15 +00001148 int ret = 0, i;
Mika Westerberga60eac32017-11-27 16:54:43 +03001149
1150 for (i = 0; i < community->ngpps; i++) {
1151 const struct intel_padgroup *gpp = &community->gpps[i];
1152
Andy Shevchenkoe5a4ab62020-04-13 14:18:20 +03001153 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
Mika Westerberga60eac32017-11-27 16:54:43 +03001154 continue;
1155
1156 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1157 gpp->gpio_base, gpp->base,
1158 gpp->size);
1159 if (ret)
1160 return ret;
1161 }
1162
1163 return ret;
1164}
1165
Linus Walleij6d416b92020-01-09 08:53:28 +01001166static int intel_gpio_add_pin_ranges(struct gpio_chip *gc)
1167{
1168 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
1169 int ret, i;
1170
1171 for (i = 0; i < pctrl->ncommunities; i++) {
1172 struct intel_community *community = &pctrl->communities[i];
1173
1174 ret = intel_gpio_add_community_ranges(pctrl, community);
1175 if (ret) {
1176 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1177 return ret;
1178 }
1179 }
1180
1181 return 0;
1182}
1183
Andy Shevchenko11b389c2019-11-06 16:39:48 +02001184static unsigned int intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
Mika Westerberga60eac32017-11-27 16:54:43 +03001185{
1186 const struct intel_community *community;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001187 unsigned int ngpio = 0;
Mika Westerberga60eac32017-11-27 16:54:43 +03001188 int i, j;
1189
1190 for (i = 0; i < pctrl->ncommunities; i++) {
1191 community = &pctrl->communities[i];
1192 for (j = 0; j < community->ngpps; j++) {
1193 const struct intel_padgroup *gpp = &community->gpps[j];
1194
Andy Shevchenkoe5a4ab62020-04-13 14:18:20 +03001195 if (gpp->gpio_base == INTEL_GPIO_BASE_NOMAP)
Mika Westerberga60eac32017-11-27 16:54:43 +03001196 continue;
1197
1198 if (gpp->gpio_base + gpp->size > ngpio)
1199 ngpio = gpp->gpio_base + gpp->size;
1200 }
1201 }
1202
1203 return ngpio;
1204}
1205
Mika Westerberg7981c0012015-03-30 17:31:49 +03001206static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1207{
Linus Walleij6d416b92020-01-09 08:53:28 +01001208 int ret;
Linus Walleijaf0c5332020-01-09 08:53:29 +01001209 struct gpio_irq_chip *girq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001210
1211 pctrl->chip = intel_gpio_chip;
1212
Andy Shevchenko57ff2df2019-09-16 17:47:51 +03001213 /* Setup GPIO chip */
Mika Westerberga60eac32017-11-27 16:54:43 +03001214 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001215 pctrl->chip.label = dev_name(pctrl->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001216 pctrl->chip.parent = pctrl->dev;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001217 pctrl->chip.base = -1;
Linus Walleij6d416b92020-01-09 08:53:28 +01001218 pctrl->chip.add_pin_ranges = intel_gpio_add_pin_ranges;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001219 pctrl->irq = irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001220
Andy Shevchenko57ff2df2019-09-16 17:47:51 +03001221 /* Setup IRQ chip */
1222 pctrl->irqchip.name = dev_name(pctrl->dev);
1223 pctrl->irqchip.irq_ack = intel_gpio_irq_ack;
1224 pctrl->irqchip.irq_mask = intel_gpio_irq_mask;
1225 pctrl->irqchip.irq_unmask = intel_gpio_irq_unmask;
1226 pctrl->irqchip.irq_set_type = intel_gpio_irq_type;
1227 pctrl->irqchip.irq_set_wake = intel_gpio_irq_wake;
1228 pctrl->irqchip.flags = IRQCHIP_MASK_ON_SUSPEND;
1229
Mika Westerberg193b40c2015-10-21 13:08:43 +03001230 /*
Linus Walleijaf0c5332020-01-09 08:53:29 +01001231 * On some platforms several GPIO controllers share the same interrupt
1232 * line.
Mika Westerberg193b40c2015-10-21 13:08:43 +03001233 */
Mika Westerberg1a7d1cb2016-06-16 11:25:37 +03001234 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1235 IRQF_SHARED | IRQF_NO_THREAD,
Mika Westerberg193b40c2015-10-21 13:08:43 +03001236 dev_name(pctrl->dev), pctrl);
1237 if (ret) {
1238 dev_err(pctrl->dev, "failed to request interrupt\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001239 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001240 }
1241
Linus Walleijaf0c5332020-01-09 08:53:29 +01001242 girq = &pctrl->chip.irq;
1243 girq->chip = &pctrl->irqchip;
1244 /* This will let us handle the IRQ in the driver */
1245 girq->parent_handler = NULL;
1246 girq->num_parents = 0;
1247 girq->default_type = IRQ_TYPE_NONE;
1248 girq->handler = handle_bad_irq;
1249
1250 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001251 if (ret) {
Linus Walleijaf0c5332020-01-09 08:53:29 +01001252 dev_err(pctrl->dev, "failed to register gpiochip\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001253 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001254 }
1255
Mika Westerberg7981c0012015-03-30 17:31:49 +03001256 return 0;
1257}
1258
Mika Westerberg919eb472017-06-06 16:18:17 +03001259static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1260 struct intel_community *community)
1261{
1262 struct intel_padgroup *gpps;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001263 unsigned int npins = community->npins;
1264 unsigned int padown_num = 0;
Mika Westerberg919eb472017-06-06 16:18:17 +03001265 size_t ngpps, i;
1266
1267 if (community->gpps)
1268 ngpps = community->ngpps;
1269 else
1270 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1271
1272 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1273 if (!gpps)
1274 return -ENOMEM;
1275
1276 for (i = 0; i < ngpps; i++) {
1277 if (community->gpps) {
1278 gpps[i] = community->gpps[i];
1279 } else {
Andy Shevchenko04035f72018-09-26 17:50:26 +03001280 unsigned int gpp_size = community->gpp_size;
Mika Westerberg919eb472017-06-06 16:18:17 +03001281
1282 gpps[i].reg_num = i;
1283 gpps[i].base = community->pin_base + i * gpp_size;
1284 gpps[i].size = min(gpp_size, npins);
1285 npins -= gpps[i].size;
1286 }
1287
1288 if (gpps[i].size > 32)
1289 return -EINVAL;
1290
Andy Shevchenkoe5a4ab62020-04-13 14:18:20 +03001291 /* Special treatment for GPIO base */
1292 switch (gpps[i].gpio_base) {
1293 case INTEL_GPIO_BASE_MATCH:
1294 gpps[i].gpio_base = gpps[i].base;
1295 break;
Andy Shevchenko9bd59152020-04-13 14:18:24 +03001296 case INTEL_GPIO_BASE_ZERO:
1297 gpps[i].gpio_base = 0;
1298 break;
Andy Shevchenkoe5a4ab62020-04-13 14:18:20 +03001299 case INTEL_GPIO_BASE_NOMAP:
1300 default:
1301 break;
1302 }
Mika Westerberga60eac32017-11-27 16:54:43 +03001303
Mika Westerberg919eb472017-06-06 16:18:17 +03001304 gpps[i].padown_num = padown_num;
1305
1306 /*
1307 * In older hardware the number of padown registers per
1308 * group is fixed regardless of the group size.
1309 */
1310 if (community->gpp_num_padown_regs)
1311 padown_num += community->gpp_num_padown_regs;
1312 else
1313 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1314 }
1315
1316 community->ngpps = ngpps;
1317 community->gpps = gpps;
1318
1319 return 0;
1320}
1321
Mika Westerberg7981c0012015-03-30 17:31:49 +03001322static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1323{
1324#ifdef CONFIG_PM_SLEEP
1325 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1326 struct intel_community_context *communities;
1327 struct intel_pad_context *pads;
1328 int i;
1329
1330 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1331 if (!pads)
1332 return -ENOMEM;
1333
1334 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1335 sizeof(*communities), GFP_KERNEL);
1336 if (!communities)
1337 return -ENOMEM;
1338
1339
1340 for (i = 0; i < pctrl->ncommunities; i++) {
1341 struct intel_community *community = &pctrl->communities[i];
Chris Chiua0a5f762019-04-15 13:53:58 +08001342 u32 *intmask, *hostown;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001343
1344 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1345 sizeof(*intmask), GFP_KERNEL);
1346 if (!intmask)
1347 return -ENOMEM;
1348
1349 communities[i].intmask = intmask;
Chris Chiua0a5f762019-04-15 13:53:58 +08001350
1351 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1352 sizeof(*hostown), GFP_KERNEL);
1353 if (!hostown)
1354 return -ENOMEM;
1355
1356 communities[i].hostown = hostown;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001357 }
1358
1359 pctrl->context.pads = pads;
1360 pctrl->context.communities = communities;
1361#endif
1362
1363 return 0;
1364}
1365
Andy Shevchenko0dd519e2018-10-17 19:10:27 +03001366static int intel_pinctrl_probe(struct platform_device *pdev,
1367 const struct intel_pinctrl_soc_data *soc_data)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001368{
1369 struct intel_pinctrl *pctrl;
1370 int i, ret, irq;
1371
1372 if (!soc_data)
1373 return -EINVAL;
1374
1375 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1376 if (!pctrl)
1377 return -ENOMEM;
1378
1379 pctrl->dev = &pdev->dev;
1380 pctrl->soc = soc_data;
Mika Westerberg27d90982016-06-16 11:25:36 +03001381 raw_spin_lock_init(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001382
1383 /*
1384 * Make a copy of the communities which we can use to hold pointers
1385 * to the registers.
1386 */
1387 pctrl->ncommunities = pctrl->soc->ncommunities;
1388 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1389 sizeof(*pctrl->communities), GFP_KERNEL);
1390 if (!pctrl->communities)
1391 return -ENOMEM;
1392
1393 for (i = 0; i < pctrl->ncommunities; i++) {
1394 struct intel_community *community = &pctrl->communities[i];
Mika Westerberg7981c0012015-03-30 17:31:49 +03001395 void __iomem *regs;
1396 u32 padbar;
1397
1398 *community = pctrl->soc->communities[i];
1399
Andy Shevchenko9d5b6a92019-07-03 17:44:20 +03001400 regs = devm_platform_ioremap_resource(pdev, community->barno);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001401 if (IS_ERR(regs))
1402 return PTR_ERR(regs);
1403
Mika Westerberge57725e2017-01-27 13:07:14 +03001404 /*
1405 * Determine community features based on the revision if
1406 * not specified already.
1407 */
1408 if (!community->features) {
1409 u32 rev;
1410
1411 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001412 if (rev >= 0x94) {
Mika Westerberge57725e2017-01-27 13:07:14 +03001413 community->features |= PINCTRL_FEATURE_DEBOUNCE;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001414 community->features |= PINCTRL_FEATURE_1K_PD;
1415 }
Mika Westerberge57725e2017-01-27 13:07:14 +03001416 }
1417
Mika Westerberg7981c0012015-03-30 17:31:49 +03001418 /* Read offset of the pad configuration registers */
1419 padbar = readl(regs + PADBAR);
1420
1421 community->regs = regs;
1422 community->pad_regs = regs + padbar;
Mika Westerberg919eb472017-06-06 16:18:17 +03001423
1424 ret = intel_pinctrl_add_padgroups(pctrl, community);
1425 if (ret)
1426 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001427 }
1428
1429 irq = platform_get_irq(pdev, 0);
Stephen Boyd4e73d022019-07-30 11:15:34 -07001430 if (irq < 0)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001431 return irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001432
1433 ret = intel_pinctrl_pm_init(pctrl);
1434 if (ret)
1435 return ret;
1436
1437 pctrl->pctldesc = intel_pinctrl_desc;
1438 pctrl->pctldesc.name = dev_name(&pdev->dev);
1439 pctrl->pctldesc.pins = pctrl->soc->pins;
1440 pctrl->pctldesc.npins = pctrl->soc->npins;
1441
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301442 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1443 pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001444 if (IS_ERR(pctrl->pctldev)) {
Mika Westerberg7981c0012015-03-30 17:31:49 +03001445 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001446 return PTR_ERR(pctrl->pctldev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001447 }
1448
1449 ret = intel_gpio_probe(pctrl, irq);
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301450 if (ret)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001451 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001452
1453 platform_set_drvdata(pdev, pctrl);
1454
1455 return 0;
1456}
Mika Westerberg7981c0012015-03-30 17:31:49 +03001457
Andy Shevchenko70c263c2018-08-30 19:27:40 +03001458int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1459{
1460 const struct intel_pinctrl_soc_data *data;
1461
1462 data = device_get_match_data(&pdev->dev);
1463 return intel_pinctrl_probe(pdev, data);
1464}
1465EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
1466
Andy Shevchenko924cf802018-08-30 19:27:36 +03001467int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1468{
1469 const struct intel_pinctrl_soc_data *data = NULL;
1470 const struct intel_pinctrl_soc_data **table;
1471 struct acpi_device *adev;
1472 unsigned int i;
1473
1474 adev = ACPI_COMPANION(&pdev->dev);
1475 if (adev) {
1476 const void *match = device_get_match_data(&pdev->dev);
1477
1478 table = (const struct intel_pinctrl_soc_data **)match;
1479 for (i = 0; table[i]; i++) {
1480 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1481 data = table[i];
1482 break;
1483 }
1484 }
1485 } else {
1486 const struct platform_device_id *id;
1487
1488 id = platform_get_device_id(pdev);
1489 if (!id)
1490 return -ENODEV;
1491
1492 table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1493 data = table[pdev->id];
1494 }
Andy Shevchenko924cf802018-08-30 19:27:36 +03001495
1496 return intel_pinctrl_probe(pdev, data);
1497}
1498EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1499
Mika Westerberg7981c0012015-03-30 17:31:49 +03001500#ifdef CONFIG_PM_SLEEP
Andy Shevchenko04035f72018-09-26 17:50:26 +03001501static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerbergc538b942016-10-10 16:39:31 +03001502{
1503 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1504
1505 if (!pd || !intel_pad_usable(pctrl, pin))
1506 return false;
1507
1508 /*
1509 * Only restore the pin if it is actually in use by the kernel (or
1510 * by userspace). It is possible that some pins are used by the
1511 * BIOS during resume and those are not always locked down so leave
1512 * them alone.
1513 */
1514 if (pd->mux_owner || pd->gpio_owner ||
Chris Chiu6cb08802019-08-16 17:38:38 +08001515 gpiochip_line_is_irq(&pctrl->chip, intel_pin_to_gpio(pctrl, pin)))
Mika Westerbergc538b942016-10-10 16:39:31 +03001516 return true;
1517
1518 return false;
1519}
1520
Binbin Wu2fef3272019-04-08 18:49:26 +08001521int intel_pinctrl_suspend_noirq(struct device *dev)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001522{
Wolfram Sangcb035d72018-10-21 22:00:29 +02001523 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001524 struct intel_community_context *communities;
1525 struct intel_pad_context *pads;
1526 int i;
1527
1528 pads = pctrl->context.pads;
1529 for (i = 0; i < pctrl->soc->npins; i++) {
1530 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
Mika Westerberge57725e2017-01-27 13:07:14 +03001531 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001532 u32 val;
1533
Mika Westerbergc538b942016-10-10 16:39:31 +03001534 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001535 continue;
1536
1537 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1538 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1539 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1540 pads[i].padcfg1 = val;
Mika Westerberge57725e2017-01-27 13:07:14 +03001541
1542 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1543 if (padcfg)
1544 pads[i].padcfg2 = readl(padcfg);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001545 }
1546
1547 communities = pctrl->context.communities;
1548 for (i = 0; i < pctrl->ncommunities; i++) {
1549 struct intel_community *community = &pctrl->communities[i];
1550 void __iomem *base;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001551 unsigned int gpp;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001552
1553 base = community->regs + community->ie_offset;
1554 for (gpp = 0; gpp < community->ngpps; gpp++)
1555 communities[i].intmask[gpp] = readl(base + gpp * 4);
Chris Chiua0a5f762019-04-15 13:53:58 +08001556
1557 base = community->regs + community->hostown_offset;
1558 for (gpp = 0; gpp < community->ngpps; gpp++)
1559 communities[i].hostown[gpp] = readl(base + gpp * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001560 }
1561
1562 return 0;
1563}
Binbin Wu2fef3272019-04-08 18:49:26 +08001564EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001565
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001566static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1567{
1568 size_t i;
1569
1570 for (i = 0; i < pctrl->ncommunities; i++) {
1571 const struct intel_community *community;
1572 void __iomem *base;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001573 unsigned int gpp;
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001574
1575 community = &pctrl->communities[i];
1576 base = community->regs;
1577
1578 for (gpp = 0; gpp < community->ngpps; gpp++) {
1579 /* Mask and clear all interrupts */
1580 writel(0, base + community->ie_offset + gpp * 4);
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001581 writel(0xffff, base + community->is_offset + gpp * 4);
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001582 }
1583 }
1584}
1585
Chris Chiua0a5f762019-04-15 13:53:58 +08001586static u32
1587intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size)
1588{
1589 u32 requested = 0;
1590 unsigned int i;
1591
1592 for (i = 0; i < size; i++)
1593 if (gpiochip_is_requested(chip, base + i))
1594 requested |= BIT(i);
1595
1596 return requested;
1597}
1598
Andy Shevchenko942c5ea2019-10-22 13:00:04 +03001599static bool intel_gpio_update_reg(void __iomem *reg, u32 mask, u32 value)
Chris Chiua0a5f762019-04-15 13:53:58 +08001600{
Andy Shevchenko5f61d952019-04-28 20:19:06 +03001601 u32 curr, updated;
Chris Chiua0a5f762019-04-15 13:53:58 +08001602
Andy Shevchenko942c5ea2019-10-22 13:00:04 +03001603 curr = readl(reg);
Andy Shevchenko5f61d952019-04-28 20:19:06 +03001604
Andy Shevchenko942c5ea2019-10-22 13:00:04 +03001605 updated = (curr & ~mask) | (value & mask);
1606 if (curr == updated)
1607 return false;
1608
1609 writel(updated, reg);
1610 return true;
Chris Chiua0a5f762019-04-15 13:53:58 +08001611}
1612
Andy Shevchenko7101e022019-10-22 13:00:01 +03001613static void intel_restore_hostown(struct intel_pinctrl *pctrl, unsigned int c,
1614 void __iomem *base, unsigned int gpp, u32 saved)
1615{
1616 const struct intel_community *community = &pctrl->communities[c];
1617 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1618 struct device *dev = pctrl->dev;
Andy Shevchenko942c5ea2019-10-22 13:00:04 +03001619 u32 requested;
Andy Shevchenko7101e022019-10-22 13:00:01 +03001620
Andy Shevchenkoe5a4ab62020-04-13 14:18:20 +03001621 if (padgrp->gpio_base == INTEL_GPIO_BASE_NOMAP)
Andy Shevchenko7101e022019-10-22 13:00:01 +03001622 return;
1623
1624 requested = intel_gpio_is_requested(&pctrl->chip, padgrp->gpio_base, padgrp->size);
Andy Shevchenko942c5ea2019-10-22 13:00:04 +03001625 if (!intel_gpio_update_reg(base + gpp * 4, requested, saved))
Andy Shevchenko7101e022019-10-22 13:00:01 +03001626 return;
1627
Andy Shevchenko764cfe32019-10-22 13:00:03 +03001628 dev_dbg(dev, "restored hostown %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
Andy Shevchenko7101e022019-10-22 13:00:01 +03001629}
1630
Andy Shevchenko471dd9a2019-10-22 13:00:02 +03001631static void intel_restore_intmask(struct intel_pinctrl *pctrl, unsigned int c,
1632 void __iomem *base, unsigned int gpp, u32 saved)
1633{
1634 struct device *dev = pctrl->dev;
1635
Andy Shevchenko942c5ea2019-10-22 13:00:04 +03001636 if (!intel_gpio_update_reg(base + gpp * 4, ~0U, saved))
1637 return;
1638
Andy Shevchenko471dd9a2019-10-22 13:00:02 +03001639 dev_dbg(dev, "restored mask %u/%u %#08x\n", c, gpp, readl(base + gpp * 4));
1640}
1641
Andy Shevchenkof78f1522019-10-22 13:00:00 +03001642static void intel_restore_padcfg(struct intel_pinctrl *pctrl, unsigned int pin,
1643 unsigned int reg, u32 saved)
1644{
1645 u32 mask = (reg == PADCFG0) ? PADCFG0_GPIORXSTATE : 0;
1646 unsigned int n = reg / sizeof(u32);
1647 struct device *dev = pctrl->dev;
1648 void __iomem *padcfg;
Andy Shevchenkof78f1522019-10-22 13:00:00 +03001649
1650 padcfg = intel_get_padcfg(pctrl, pin, reg);
1651 if (!padcfg)
1652 return;
1653
Andy Shevchenko942c5ea2019-10-22 13:00:04 +03001654 if (!intel_gpio_update_reg(padcfg, ~mask, saved))
Andy Shevchenkof78f1522019-10-22 13:00:00 +03001655 return;
1656
Andy Shevchenkof78f1522019-10-22 13:00:00 +03001657 dev_dbg(dev, "restored pin %u padcfg%u %#08x\n", pin, n, readl(padcfg));
1658}
1659
Binbin Wu2fef3272019-04-08 18:49:26 +08001660int intel_pinctrl_resume_noirq(struct device *dev)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001661{
Wolfram Sangcb035d72018-10-21 22:00:29 +02001662 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001663 const struct intel_community_context *communities;
1664 const struct intel_pad_context *pads;
1665 int i;
1666
1667 /* Mask all interrupts */
1668 intel_gpio_irq_init(pctrl);
1669
1670 pads = pctrl->context.pads;
1671 for (i = 0; i < pctrl->soc->npins; i++) {
1672 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
Mika Westerberg7981c0012015-03-30 17:31:49 +03001673
Mika Westerbergc538b942016-10-10 16:39:31 +03001674 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001675 continue;
1676
Andy Shevchenkof78f1522019-10-22 13:00:00 +03001677 intel_restore_padcfg(pctrl, desc->number, PADCFG0, pads[i].padcfg0);
1678 intel_restore_padcfg(pctrl, desc->number, PADCFG1, pads[i].padcfg1);
1679 intel_restore_padcfg(pctrl, desc->number, PADCFG2, pads[i].padcfg2);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001680 }
1681
1682 communities = pctrl->context.communities;
1683 for (i = 0; i < pctrl->ncommunities; i++) {
1684 struct intel_community *community = &pctrl->communities[i];
1685 void __iomem *base;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001686 unsigned int gpp;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001687
1688 base = community->regs + community->ie_offset;
Andy Shevchenko471dd9a2019-10-22 13:00:02 +03001689 for (gpp = 0; gpp < community->ngpps; gpp++)
1690 intel_restore_intmask(pctrl, i, base, gpp, communities[i].intmask[gpp]);
Chris Chiua0a5f762019-04-15 13:53:58 +08001691
1692 base = community->regs + community->hostown_offset;
Andy Shevchenko7101e022019-10-22 13:00:01 +03001693 for (gpp = 0; gpp < community->ngpps; gpp++)
1694 intel_restore_hostown(pctrl, i, base, gpp, communities[i].hostown[gpp]);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001695 }
1696
1697 return 0;
1698}
Binbin Wu2fef3272019-04-08 18:49:26 +08001699EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001700#endif
1701
1702MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1703MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1704MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1705MODULE_LICENSE("GPL v2");