blob: d7acbb79cdf7d43fe349293716699c9ccc88d6af [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/module.h>
Mika Westerberg193b40c2015-10-21 13:08:43 +030012#include <linux/interrupt.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030013#include <linux/gpio/driver.h>
Mika Westerberge57725e2017-01-27 13:07:14 +030014#include <linux/log2.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>
17
Mika Westerberg7981c0012015-03-30 17:31:49 +030018#include <linux/pinctrl/pinctrl.h>
19#include <linux/pinctrl/pinmux.h>
20#include <linux/pinctrl/pinconf.h>
21#include <linux/pinctrl/pinconf-generic.h>
22
Mika Westerbergc538b942016-10-10 16:39:31 +030023#include "../core.h"
Mika Westerberg7981c0012015-03-30 17:31:49 +030024#include "pinctrl-intel.h"
25
Mika Westerberg7981c0012015-03-30 17:31:49 +030026/* Offset from regs */
Mika Westerberge57725e2017-01-27 13:07:14 +030027#define REVID 0x000
28#define REVID_SHIFT 16
29#define REVID_MASK GENMASK(31, 16)
30
Mika Westerberg7981c0012015-03-30 17:31:49 +030031#define PADBAR 0x00c
32#define GPI_IS 0x100
Mika Westerberg7981c0012015-03-30 17:31:49 +030033
34#define PADOWN_BITS 4
35#define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
36#define PADOWN_MASK(p) (0xf << 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
42#define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
43#define PADCFG0_RXEVCFG_LEVEL 0
44#define PADCFG0_RXEVCFG_EDGE 1
45#define PADCFG0_RXEVCFG_DISABLED 2
46#define PADCFG0_RXEVCFG_EDGE_BOTH 3
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
54#define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
55#define PADCFG0_GPIORXDIS BIT(9)
56#define PADCFG0_GPIOTXDIS BIT(8)
57#define PADCFG0_GPIORXSTATE BIT(1)
58#define PADCFG0_GPIOTXSTATE BIT(0)
59
60#define PADCFG1 0x004
61#define PADCFG1_TERM_UP BIT(13)
62#define PADCFG1_TERM_SHIFT 10
63#define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
64#define PADCFG1_TERM_20K 4
65#define PADCFG1_TERM_2K 3
66#define PADCFG1_TERM_5K 2
67#define PADCFG1_TERM_1K 1
68
Mika Westerberge57725e2017-01-27 13:07:14 +030069#define PADCFG2 0x008
70#define PADCFG2_DEBEN BIT(0)
71#define PADCFG2_DEBOUNCE_SHIFT 1
72#define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
73
74#define DEBOUNCE_PERIOD 31250 /* ns */
75
Mika Westerberg7981c0012015-03-30 17:31:49 +030076struct intel_pad_context {
77 u32 padcfg0;
78 u32 padcfg1;
Mika Westerberge57725e2017-01-27 13:07:14 +030079 u32 padcfg2;
Mika Westerberg7981c0012015-03-30 17:31:49 +030080};
81
82struct intel_community_context {
83 u32 *intmask;
Chris Chiua0a5f762019-04-15 13:53:58 +080084 u32 *hostown;
Mika Westerberg7981c0012015-03-30 17:31:49 +030085};
86
87struct intel_pinctrl_context {
88 struct intel_pad_context *pads;
89 struct intel_community_context *communities;
90};
91
92/**
93 * struct intel_pinctrl - Intel pinctrl private structure
94 * @dev: Pointer to the device structure
95 * @lock: Lock to serialize register access
96 * @pctldesc: Pin controller description
97 * @pctldev: Pointer to the pin controller device
98 * @chip: GPIO chip in this pin controller
99 * @soc: SoC/PCH specific pin configuration data
100 * @communities: All communities in this pin controller
101 * @ncommunities: Number of communities in this pin controller
102 * @context: Configuration saved over system sleep
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700103 * @irq: pinctrl/GPIO chip irq number
Mika Westerberg7981c0012015-03-30 17:31:49 +0300104 */
105struct intel_pinctrl {
106 struct device *dev;
Mika Westerberg27d90982016-06-16 11:25:36 +0300107 raw_spinlock_t lock;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300108 struct pinctrl_desc pctldesc;
109 struct pinctrl_dev *pctldev;
110 struct gpio_chip chip;
111 const struct intel_pinctrl_soc_data *soc;
112 struct intel_community *communities;
113 size_t ncommunities;
114 struct intel_pinctrl_context context;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700115 int irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300116};
117
Mika Westerberg7981c0012015-03-30 17:31:49 +0300118#define pin_to_padno(c, p) ((p) - (c)->pin_base)
Mika Westerberg919eb472017-06-06 16:18:17 +0300119#define padgroup_offset(g, p) ((p) - (g)->base)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300120
121static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300122 unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300123{
124 struct intel_community *community;
125 int i;
126
127 for (i = 0; i < pctrl->ncommunities; i++) {
128 community = &pctrl->communities[i];
129 if (pin >= community->pin_base &&
130 pin < community->pin_base + community->npins)
131 return community;
132 }
133
134 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
135 return NULL;
136}
137
Mika Westerberg919eb472017-06-06 16:18:17 +0300138static const struct intel_padgroup *
139intel_community_get_padgroup(const struct intel_community *community,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300140 unsigned int pin)
Mika Westerberg919eb472017-06-06 16:18:17 +0300141{
142 int i;
143
144 for (i = 0; i < community->ngpps; i++) {
145 const struct intel_padgroup *padgrp = &community->gpps[i];
146
147 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
148 return padgrp;
149 }
150
151 return NULL;
152}
153
Andy Shevchenko04035f72018-09-26 17:50:26 +0300154static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl,
155 unsigned int pin, unsigned int reg)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300156{
157 const struct intel_community *community;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300158 unsigned int padno;
Mika Westerberge57725e2017-01-27 13:07:14 +0300159 size_t nregs;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300160
161 community = intel_get_community(pctrl, pin);
162 if (!community)
163 return NULL;
164
165 padno = pin_to_padno(community, pin);
Mika Westerberge57725e2017-01-27 13:07:14 +0300166 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
167
168 if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
169 return NULL;
170
171 return community->pad_regs + reg + padno * nregs * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300172}
173
Andy Shevchenko04035f72018-09-26 17:50:26 +0300174static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300175{
176 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300177 const struct intel_padgroup *padgrp;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300178 unsigned int gpp, offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300179 void __iomem *padown;
180
181 community = intel_get_community(pctrl, pin);
182 if (!community)
183 return false;
184 if (!community->padown_offset)
185 return true;
186
Mika Westerberg919eb472017-06-06 16:18:17 +0300187 padgrp = intel_community_get_padgroup(community, pin);
188 if (!padgrp)
189 return false;
190
191 gpp_offset = padgroup_offset(padgrp, pin);
192 gpp = PADOWN_GPP(gpp_offset);
193 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300194 padown = community->regs + offset;
195
Mika Westerberg919eb472017-06-06 16:18:17 +0300196 return !(readl(padown) & PADOWN_MASK(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300197}
198
Andy Shevchenko04035f72018-09-26 17:50:26 +0300199static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300200{
201 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300202 const struct intel_padgroup *padgrp;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300203 unsigned int offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300204 void __iomem *hostown;
205
206 community = intel_get_community(pctrl, pin);
207 if (!community)
208 return true;
209 if (!community->hostown_offset)
210 return false;
211
Mika Westerberg919eb472017-06-06 16:18:17 +0300212 padgrp = intel_community_get_padgroup(community, pin);
213 if (!padgrp)
214 return true;
215
216 gpp_offset = padgroup_offset(padgrp, pin);
217 offset = community->hostown_offset + padgrp->reg_num * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300218 hostown = community->regs + offset;
219
Mika Westerberg919eb472017-06-06 16:18:17 +0300220 return !(readl(hostown) & BIT(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300221}
222
Andy Shevchenko04035f72018-09-26 17:50:26 +0300223static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300224{
225 struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300226 const struct intel_padgroup *padgrp;
Andy Shevchenko04035f72018-09-26 17:50:26 +0300227 unsigned int offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300228 u32 value;
229
230 community = intel_get_community(pctrl, pin);
231 if (!community)
232 return true;
233 if (!community->padcfglock_offset)
234 return false;
235
Mika Westerberg919eb472017-06-06 16:18:17 +0300236 padgrp = intel_community_get_padgroup(community, pin);
237 if (!padgrp)
238 return true;
239
240 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300241
242 /*
243 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
244 * the pad is considered unlocked. Any other case means that it is
245 * either fully or partially locked and we don't touch it.
246 */
Mika Westerberg919eb472017-06-06 16:18:17 +0300247 offset = community->padcfglock_offset + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300248 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300249 if (value & BIT(gpp_offset))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300250 return true;
251
Mika Westerberg919eb472017-06-06 16:18:17 +0300252 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300253 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300254 if (value & BIT(gpp_offset))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300255 return true;
256
257 return false;
258}
259
Andy Shevchenko04035f72018-09-26 17:50:26 +0300260static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300261{
262 return intel_pad_owned_by_host(pctrl, pin) &&
Mika Westerberg7981c0012015-03-30 17:31:49 +0300263 !intel_pad_locked(pctrl, pin);
264}
265
266static int intel_get_groups_count(struct pinctrl_dev *pctldev)
267{
268 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
269
270 return pctrl->soc->ngroups;
271}
272
273static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300274 unsigned int group)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300275{
276 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
277
278 return pctrl->soc->groups[group].name;
279}
280
Andy Shevchenko04035f72018-09-26 17:50:26 +0300281static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned int group,
282 const unsigned int **pins, unsigned int *npins)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300283{
284 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
285
286 *pins = pctrl->soc->groups[group].pins;
287 *npins = pctrl->soc->groups[group].npins;
288 return 0;
289}
290
291static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300292 unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300293{
294 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
Mika Westerberge57725e2017-01-27 13:07:14 +0300295 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300296 u32 cfg0, cfg1, mode;
297 bool locked, acpi;
298
299 if (!intel_pad_owned_by_host(pctrl, pin)) {
300 seq_puts(s, "not available");
301 return;
302 }
303
304 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
305 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
306
307 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
308 if (!mode)
309 seq_puts(s, "GPIO ");
310 else
311 seq_printf(s, "mode %d ", mode);
312
313 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
314
Mika Westerberge57725e2017-01-27 13:07:14 +0300315 /* Dump the additional PADCFG registers if available */
316 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
317 if (padcfg)
318 seq_printf(s, " 0x%08x", readl(padcfg));
319
Mika Westerberg7981c0012015-03-30 17:31:49 +0300320 locked = intel_pad_locked(pctrl, pin);
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300321 acpi = intel_pad_acpi_mode(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300322
323 if (locked || acpi) {
324 seq_puts(s, " [");
325 if (locked) {
326 seq_puts(s, "LOCKED");
327 if (acpi)
328 seq_puts(s, ", ");
329 }
330 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
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300429static void intel_gpio_set_gpio_mode(void __iomem *padcfg0)
430{
431 u32 value;
432
433 /* Put the pad into GPIO mode */
434 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
435 /* Disable SCI/SMI/NMI generation */
436 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
437 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
438 writel(value, padcfg0);
439}
440
Mika Westerberg7981c0012015-03-30 17:31:49 +0300441static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
442 struct pinctrl_gpio_range *range,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300443 unsigned int pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300444{
445 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
446 void __iomem *padcfg0;
447 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300448
Mika Westerberg27d90982016-06-16 11:25:36 +0300449 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300450
451 if (!intel_pad_usable(pctrl, pin)) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300452 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300453 return -EBUSY;
454 }
455
456 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
Mika Westerbergf5a26ac2017-11-29 16:25:44 +0300457 intel_gpio_set_gpio_mode(padcfg0);
Andy Shevchenko17fab472017-01-02 14:07:22 +0200458 /* Disable TX buffer and enable RX (this will be input) */
459 __intel_gpio_set_direction(padcfg0, true);
460
Mika Westerberg27d90982016-06-16 11:25:36 +0300461 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300462
463 return 0;
464}
465
466static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
467 struct pinctrl_gpio_range *range,
Andy Shevchenko04035f72018-09-26 17:50:26 +0300468 unsigned int pin, bool input)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300469{
470 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
471 void __iomem *padcfg0;
472 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300473
Mika Westerberg27d90982016-06-16 11:25:36 +0300474 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300475
476 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
Andy Shevchenko17fab472017-01-02 14:07:22 +0200477 __intel_gpio_set_direction(padcfg0, input);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300478
Mika Westerberg27d90982016-06-16 11:25:36 +0300479 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300480
481 return 0;
482}
483
484static const struct pinmux_ops intel_pinmux_ops = {
485 .get_functions_count = intel_get_functions_count,
486 .get_function_name = intel_get_function_name,
487 .get_function_groups = intel_get_function_groups,
488 .set_mux = intel_pinmux_set_mux,
489 .gpio_request_enable = intel_gpio_request_enable,
490 .gpio_set_direction = intel_gpio_set_direction,
491};
492
Andy Shevchenko04035f72018-09-26 17:50:26 +0300493static int intel_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300494 unsigned long *config)
495{
496 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
497 enum pin_config_param param = pinconf_to_config_param(*config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300498 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300499 u32 value, term;
Mika Westerberge57725e2017-01-27 13:07:14 +0300500 u32 arg = 0;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300501
502 if (!intel_pad_owned_by_host(pctrl, pin))
503 return -ENOTSUPP;
504
Mika Westerberg04cc0582017-01-27 13:07:15 +0300505 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300506 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
507 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
508
509 switch (param) {
510 case PIN_CONFIG_BIAS_DISABLE:
511 if (term)
512 return -EINVAL;
513 break;
514
515 case PIN_CONFIG_BIAS_PULL_UP:
516 if (!term || !(value & PADCFG1_TERM_UP))
517 return -EINVAL;
518
519 switch (term) {
520 case PADCFG1_TERM_1K:
521 arg = 1000;
522 break;
523 case PADCFG1_TERM_2K:
524 arg = 2000;
525 break;
526 case PADCFG1_TERM_5K:
527 arg = 5000;
528 break;
529 case PADCFG1_TERM_20K:
530 arg = 20000;
531 break;
532 }
533
534 break;
535
536 case PIN_CONFIG_BIAS_PULL_DOWN:
537 if (!term || value & PADCFG1_TERM_UP)
538 return -EINVAL;
539
540 switch (term) {
Mika Westerberg04cc0582017-01-27 13:07:15 +0300541 case PADCFG1_TERM_1K:
542 if (!(community->features & PINCTRL_FEATURE_1K_PD))
543 return -EINVAL;
544 arg = 1000;
545 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300546 case PADCFG1_TERM_5K:
547 arg = 5000;
548 break;
549 case PADCFG1_TERM_20K:
550 arg = 20000;
551 break;
552 }
553
554 break;
555
Mika Westerberge57725e2017-01-27 13:07:14 +0300556 case PIN_CONFIG_INPUT_DEBOUNCE: {
557 void __iomem *padcfg2;
558 u32 v;
559
560 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
561 if (!padcfg2)
562 return -ENOTSUPP;
563
564 v = readl(padcfg2);
565 if (!(v & PADCFG2_DEBEN))
566 return -EINVAL;
567
568 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
569 arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
570
571 break;
572 }
573
Mika Westerberg7981c0012015-03-30 17:31:49 +0300574 default:
575 return -ENOTSUPP;
576 }
577
578 *config = pinconf_to_config_packed(param, arg);
579 return 0;
580}
581
Andy Shevchenko04035f72018-09-26 17:50:26 +0300582static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned int pin,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300583 unsigned long config)
584{
Andy Shevchenko04035f72018-09-26 17:50:26 +0300585 unsigned int param = pinconf_to_config_param(config);
586 unsigned int arg = pinconf_to_config_argument(config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300587 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300588 void __iomem *padcfg1;
589 unsigned long flags;
590 int ret = 0;
591 u32 value;
592
Mika Westerberg27d90982016-06-16 11:25:36 +0300593 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300594
Mika Westerberg04cc0582017-01-27 13:07:15 +0300595 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300596 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
597 value = readl(padcfg1);
598
599 switch (param) {
600 case PIN_CONFIG_BIAS_DISABLE:
601 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
602 break;
603
604 case PIN_CONFIG_BIAS_PULL_UP:
605 value &= ~PADCFG1_TERM_MASK;
606
607 value |= PADCFG1_TERM_UP;
608
609 switch (arg) {
610 case 20000:
611 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
612 break;
613 case 5000:
614 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
615 break;
616 case 2000:
617 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
618 break;
619 case 1000:
620 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
621 break;
622 default:
623 ret = -EINVAL;
624 }
625
626 break;
627
628 case PIN_CONFIG_BIAS_PULL_DOWN:
629 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
630
631 switch (arg) {
632 case 20000:
633 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
634 break;
635 case 5000:
636 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
637 break;
Mika Westerberg04cc0582017-01-27 13:07:15 +0300638 case 1000:
Dan Carpenteraa1dd802017-02-07 16:20:08 +0300639 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
640 ret = -EINVAL;
641 break;
642 }
Mika Westerberg04cc0582017-01-27 13:07:15 +0300643 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
644 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300645 default:
646 ret = -EINVAL;
647 }
648
649 break;
650 }
651
652 if (!ret)
653 writel(value, padcfg1);
654
Mika Westerberg27d90982016-06-16 11:25:36 +0300655 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300656
657 return ret;
658}
659
Andy Shevchenko04035f72018-09-26 17:50:26 +0300660static int intel_config_set_debounce(struct intel_pinctrl *pctrl,
661 unsigned int pin, unsigned int debounce)
Mika Westerberge57725e2017-01-27 13:07:14 +0300662{
663 void __iomem *padcfg0, *padcfg2;
664 unsigned long flags;
665 u32 value0, value2;
666 int ret = 0;
667
668 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
669 if (!padcfg2)
670 return -ENOTSUPP;
671
672 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
673
674 raw_spin_lock_irqsave(&pctrl->lock, flags);
675
676 value0 = readl(padcfg0);
677 value2 = readl(padcfg2);
678
679 /* Disable glitch filter and debouncer */
680 value0 &= ~PADCFG0_PREGFRXSEL;
681 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
682
683 if (debounce) {
684 unsigned long v;
685
686 v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
687 if (v < 3 || v > 15) {
688 ret = -EINVAL;
689 goto exit_unlock;
690 } else {
691 /* Enable glitch filter and debouncer */
692 value0 |= PADCFG0_PREGFRXSEL;
693 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
694 value2 |= PADCFG2_DEBEN;
695 }
696 }
697
698 writel(value0, padcfg0);
699 writel(value2, padcfg2);
700
701exit_unlock:
702 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
703
704 return ret;
705}
706
Andy Shevchenko04035f72018-09-26 17:50:26 +0300707static int intel_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
708 unsigned long *configs, unsigned int nconfigs)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300709{
710 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
711 int i, ret;
712
713 if (!intel_pad_usable(pctrl, pin))
714 return -ENOTSUPP;
715
716 for (i = 0; i < nconfigs; i++) {
717 switch (pinconf_to_config_param(configs[i])) {
718 case PIN_CONFIG_BIAS_DISABLE:
719 case PIN_CONFIG_BIAS_PULL_UP:
720 case PIN_CONFIG_BIAS_PULL_DOWN:
721 ret = intel_config_set_pull(pctrl, pin, configs[i]);
722 if (ret)
723 return ret;
724 break;
725
Mika Westerberge57725e2017-01-27 13:07:14 +0300726 case PIN_CONFIG_INPUT_DEBOUNCE:
727 ret = intel_config_set_debounce(pctrl, pin,
728 pinconf_to_config_argument(configs[i]));
729 if (ret)
730 return ret;
731 break;
732
Mika Westerberg7981c0012015-03-30 17:31:49 +0300733 default:
734 return -ENOTSUPP;
735 }
736 }
737
738 return 0;
739}
740
741static const struct pinconf_ops intel_pinconf_ops = {
742 .is_generic = true,
743 .pin_config_get = intel_config_get,
744 .pin_config_set = intel_config_set,
745};
746
747static const struct pinctrl_desc intel_pinctrl_desc = {
748 .pctlops = &intel_pinctrl_ops,
749 .pmxops = &intel_pinmux_ops,
750 .confops = &intel_pinconf_ops,
751 .owner = THIS_MODULE,
752};
753
Mika Westerberga60eac32017-11-27 16:54:43 +0300754/**
755 * intel_gpio_to_pin() - Translate from GPIO offset to pin number
756 * @pctrl: Pinctrl structure
757 * @offset: GPIO offset from gpiolib
Andy Shevchenko946ffef2018-09-26 17:43:17 +0300758 * @community: Community is filled here if not %NULL
Mika Westerberga60eac32017-11-27 16:54:43 +0300759 * @padgrp: Pad group is filled here if not %NULL
760 *
761 * When coming through gpiolib irqchip, the GPIO offset is not
762 * automatically translated to pinctrl pin number. This function can be
763 * used to find out the corresponding pinctrl pin.
764 */
Andy Shevchenko04035f72018-09-26 17:50:26 +0300765static int intel_gpio_to_pin(struct intel_pinctrl *pctrl, unsigned int offset,
Mika Westerberga60eac32017-11-27 16:54:43 +0300766 const struct intel_community **community,
767 const struct intel_padgroup **padgrp)
768{
769 int i;
770
771 for (i = 0; i < pctrl->ncommunities; i++) {
772 const struct intel_community *comm = &pctrl->communities[i];
773 int j;
774
775 for (j = 0; j < comm->ngpps; j++) {
776 const struct intel_padgroup *pgrp = &comm->gpps[j];
777
778 if (pgrp->gpio_base < 0)
779 continue;
780
781 if (offset >= pgrp->gpio_base &&
782 offset < pgrp->gpio_base + pgrp->size) {
783 int pin;
784
785 pin = pgrp->base + offset - pgrp->gpio_base;
786 if (community)
787 *community = comm;
788 if (padgrp)
789 *padgrp = pgrp;
790
791 return pin;
792 }
793 }
794 }
795
796 return -EINVAL;
797}
798
Andy Shevchenko04035f72018-09-26 17:50:26 +0300799static int intel_gpio_get(struct gpio_chip *chip, unsigned int offset)
Andy Shevchenko55aedef52018-07-25 15:42:08 +0300800{
Mika Westerberg96147db2018-09-18 18:36:21 +0300801 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
802 void __iomem *reg;
803 u32 padcfg0;
Andy Shevchenko55aedef52018-07-25 15:42:08 +0300804 int pin;
805
Mika Westerberg96147db2018-09-18 18:36:21 +0300806 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
807 if (pin < 0)
808 return -EINVAL;
809
810 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
811 if (!reg)
812 return -EINVAL;
813
814 padcfg0 = readl(reg);
815 if (!(padcfg0 & PADCFG0_GPIOTXDIS))
816 return !!(padcfg0 & PADCFG0_GPIOTXSTATE);
817
818 return !!(padcfg0 & PADCFG0_GPIORXSTATE);
Andy Shevchenko55aedef52018-07-25 15:42:08 +0300819}
820
Andy Shevchenko04035f72018-09-26 17:50:26 +0300821static void intel_gpio_set(struct gpio_chip *chip, unsigned int offset,
822 int value)
Mika Westerberg96147db2018-09-18 18:36:21 +0300823{
824 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
825 unsigned long flags;
826 void __iomem *reg;
827 u32 padcfg0;
828 int pin;
829
830 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
831 if (pin < 0)
832 return;
833
834 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
835 if (!reg)
836 return;
837
838 raw_spin_lock_irqsave(&pctrl->lock, flags);
839 padcfg0 = readl(reg);
840 if (value)
841 padcfg0 |= PADCFG0_GPIOTXSTATE;
842 else
843 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
844 writel(padcfg0, reg);
845 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
846}
847
848static int intel_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
849{
850 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
851 void __iomem *reg;
852 u32 padcfg0;
853 int pin;
854
855 pin = intel_gpio_to_pin(pctrl, offset, NULL, NULL);
856 if (pin < 0)
857 return -EINVAL;
858
859 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
860 if (!reg)
861 return -EINVAL;
862
863 padcfg0 = readl(reg);
864
865 if (padcfg0 & PADCFG0_PMODE_MASK)
866 return -EINVAL;
867
868 return !!(padcfg0 & PADCFG0_GPIOTXDIS);
869}
870
Andy Shevchenko04035f72018-09-26 17:50:26 +0300871static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
Mika Westerberg96147db2018-09-18 18:36:21 +0300872{
873 return pinctrl_gpio_direction_input(chip->base + offset);
874}
875
Andy Shevchenko04035f72018-09-26 17:50:26 +0300876static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned int offset,
Mika Westerberg96147db2018-09-18 18:36:21 +0300877 int value)
878{
879 intel_gpio_set(chip, offset, value);
880 return pinctrl_gpio_direction_output(chip->base + offset);
881}
882
883static const struct gpio_chip intel_gpio_chip = {
884 .owner = THIS_MODULE,
885 .request = gpiochip_generic_request,
886 .free = gpiochip_generic_free,
887 .get_direction = intel_gpio_get_direction,
888 .direction_input = intel_gpio_direction_input,
889 .direction_output = intel_gpio_direction_output,
890 .get = intel_gpio_get,
891 .set = intel_gpio_set,
892 .set_config = gpiochip_generic_config,
893};
894
Mika Westerberg7981c0012015-03-30 17:31:49 +0300895static void intel_gpio_irq_ack(struct irq_data *d)
896{
897 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100898 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300899 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300900 const struct intel_padgroup *padgrp;
901 int pin;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300902
Mika Westerberga60eac32017-11-27 16:54:43 +0300903 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
904 if (pin >= 0) {
Andy Shevchenko04035f72018-09-26 17:50:26 +0300905 unsigned int gpp, gpp_offset, is_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300906
Mika Westerberg919eb472017-06-06 16:18:17 +0300907 gpp = padgrp->reg_num;
908 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300909 is_offset = community->is_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300910
911 raw_spin_lock(&pctrl->lock);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300912 writel(BIT(gpp_offset), community->regs + is_offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300913 raw_spin_unlock(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300914 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300915}
916
Qi Zhenga939bb52016-03-17 02:15:26 +0800917static void intel_gpio_irq_enable(struct irq_data *d)
918{
919 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
920 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
921 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300922 const struct intel_padgroup *padgrp;
923 int pin;
Qi Zhenga939bb52016-03-17 02:15:26 +0800924
Mika Westerberga60eac32017-11-27 16:54:43 +0300925 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
926 if (pin >= 0) {
Andy Shevchenko04035f72018-09-26 17:50:26 +0300927 unsigned int gpp, gpp_offset, is_offset;
Mika Westerberg919eb472017-06-06 16:18:17 +0300928 unsigned long flags;
Qi Zhenga939bb52016-03-17 02:15:26 +0800929 u32 value;
930
Mika Westerberg919eb472017-06-06 16:18:17 +0300931 gpp = padgrp->reg_num;
932 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300933 is_offset = community->is_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300934
935 raw_spin_lock_irqsave(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800936 /* Clear interrupt status first to avoid unexpected interrupt */
Mika Westerbergcf769bd2017-10-23 15:40:25 +0300937 writel(BIT(gpp_offset), community->regs + is_offset);
Qi Zhenga939bb52016-03-17 02:15:26 +0800938
939 value = readl(community->regs + community->ie_offset + gpp * 4);
940 value |= BIT(gpp_offset);
941 writel(value, community->regs + community->ie_offset + gpp * 4);
Mika Westerberg919eb472017-06-06 16:18:17 +0300942 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800943 }
Qi Zhenga939bb52016-03-17 02:15:26 +0800944}
945
Mika Westerberg7981c0012015-03-30 17:31:49 +0300946static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
947{
948 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100949 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300950 const struct intel_community *community;
Mika Westerberga60eac32017-11-27 16:54:43 +0300951 const struct intel_padgroup *padgrp;
952 int pin;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300953
Mika Westerberga60eac32017-11-27 16:54:43 +0300954 pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), &community, &padgrp);
955 if (pin >= 0) {
Andy Shevchenko04035f72018-09-26 17:50:26 +0300956 unsigned int gpp, gpp_offset;
Mika Westerberg919eb472017-06-06 16:18:17 +0300957 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300958 void __iomem *reg;
959 u32 value;
960
Mika Westerberg919eb472017-06-06 16:18:17 +0300961 gpp = padgrp->reg_num;
962 gpp_offset = padgroup_offset(padgrp, pin);
963
Mika Westerberg7981c0012015-03-30 17:31:49 +0300964 reg = community->regs + community->ie_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300965
966 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300967 value = readl(reg);
968 if (mask)
969 value &= ~BIT(gpp_offset);
970 else
971 value |= BIT(gpp_offset);
972 writel(value, reg);
Mika Westerberg919eb472017-06-06 16:18:17 +0300973 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300974 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300975}
976
977static void intel_gpio_irq_mask(struct irq_data *d)
978{
979 intel_gpio_irq_mask_unmask(d, true);
980}
981
982static void intel_gpio_irq_unmask(struct irq_data *d)
983{
984 intel_gpio_irq_mask_unmask(d, false);
985}
986
Andy Shevchenko04035f72018-09-26 17:50:26 +0300987static int intel_gpio_irq_type(struct irq_data *d, unsigned int type)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300988{
989 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100990 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Andy Shevchenko04035f72018-09-26 17:50:26 +0300991 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300992 unsigned long flags;
993 void __iomem *reg;
994 u32 value;
995
996 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
997 if (!reg)
998 return -EINVAL;
999
Mika Westerberg4341e8a2015-10-21 13:08:44 +03001000 /*
1001 * If the pin is in ACPI mode it is still usable as a GPIO but it
1002 * cannot be used as IRQ because GPI_IS status bit will not be
1003 * updated by the host controller hardware.
1004 */
1005 if (intel_pad_acpi_mode(pctrl, pin)) {
1006 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
1007 return -EPERM;
1008 }
1009
Mika Westerberg27d90982016-06-16 11:25:36 +03001010 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001011
Mika Westerbergf5a26ac2017-11-29 16:25:44 +03001012 intel_gpio_set_gpio_mode(reg);
1013
Mika Westerberg7981c0012015-03-30 17:31:49 +03001014 value = readl(reg);
1015
1016 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
1017
1018 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
1019 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
1020 } else if (type & IRQ_TYPE_EDGE_FALLING) {
1021 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
1022 value |= PADCFG0_RXINV;
1023 } else if (type & IRQ_TYPE_EDGE_RISING) {
1024 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
Qipeng Zhabf380cf2016-03-17 02:15:25 +08001025 } else if (type & IRQ_TYPE_LEVEL_MASK) {
1026 if (type & IRQ_TYPE_LEVEL_LOW)
1027 value |= PADCFG0_RXINV;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001028 } else {
1029 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
1030 }
1031
1032 writel(value, reg);
1033
1034 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +02001035 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001036 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +02001037 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001038
Mika Westerberg27d90982016-06-16 11:25:36 +03001039 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001040
1041 return 0;
1042}
1043
1044static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
1045{
1046 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +01001047 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Andy Shevchenko04035f72018-09-26 17:50:26 +03001048 unsigned int pin = intel_gpio_to_pin(pctrl, irqd_to_hwirq(d), NULL, NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001049
Mika Westerberg7981c0012015-03-30 17:31:49 +03001050 if (on)
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001051 enable_irq_wake(pctrl->irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001052 else
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001053 disable_irq_wake(pctrl->irq);
Andy Shevchenko9a520fd2016-07-08 14:30:46 +03001054
Mika Westerberg7981c0012015-03-30 17:31:49 +03001055 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
1056 return 0;
1057}
1058
Mika Westerberg193b40c2015-10-21 13:08:43 +03001059static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
Mika Westerberg7981c0012015-03-30 17:31:49 +03001060 const struct intel_community *community)
1061{
Mika Westerberg193b40c2015-10-21 13:08:43 +03001062 struct gpio_chip *gc = &pctrl->chip;
1063 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001064 int gpp;
1065
1066 for (gpp = 0; gpp < community->ngpps; gpp++) {
Mika Westerberg919eb472017-06-06 16:18:17 +03001067 const struct intel_padgroup *padgrp = &community->gpps[gpp];
Mika Westerberg7981c0012015-03-30 17:31:49 +03001068 unsigned long pending, enabled, gpp_offset;
1069
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001070 pending = readl(community->regs + community->is_offset +
1071 padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001072 enabled = readl(community->regs + community->ie_offset +
Mika Westerberg919eb472017-06-06 16:18:17 +03001073 padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001074
1075 /* Only interrupts that are enabled */
1076 pending &= enabled;
1077
Mika Westerberg919eb472017-06-06 16:18:17 +03001078 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
Mika Westerberga60eac32017-11-27 16:54:43 +03001079 unsigned irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001080
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001081 irq = irq_find_mapping(gc->irq.domain,
Mika Westerberga60eac32017-11-27 16:54:43 +03001082 padgrp->gpio_base + gpp_offset);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001083 generic_handle_irq(irq);
Mika Westerberg193b40c2015-10-21 13:08:43 +03001084
1085 ret |= IRQ_HANDLED;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001086 }
1087 }
Mika Westerberg193b40c2015-10-21 13:08:43 +03001088
1089 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001090}
1091
Mika Westerberg193b40c2015-10-21 13:08:43 +03001092static irqreturn_t intel_gpio_irq(int irq, void *data)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001093{
Mika Westerberg193b40c2015-10-21 13:08:43 +03001094 const struct intel_community *community;
1095 struct intel_pinctrl *pctrl = data;
1096 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001097 int i;
1098
Mika Westerberg7981c0012015-03-30 17:31:49 +03001099 /* Need to check all communities for pending interrupts */
Mika Westerberg193b40c2015-10-21 13:08:43 +03001100 for (i = 0; i < pctrl->ncommunities; i++) {
1101 community = &pctrl->communities[i];
1102 ret |= intel_gpio_community_irq_handler(pctrl, community);
1103 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001104
Mika Westerberg193b40c2015-10-21 13:08:43 +03001105 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001106}
1107
1108static struct irq_chip intel_gpio_irqchip = {
1109 .name = "intel-gpio",
Qi Zhenga939bb52016-03-17 02:15:26 +08001110 .irq_enable = intel_gpio_irq_enable,
Mika Westerberg7981c0012015-03-30 17:31:49 +03001111 .irq_ack = intel_gpio_irq_ack,
1112 .irq_mask = intel_gpio_irq_mask,
1113 .irq_unmask = intel_gpio_irq_unmask,
1114 .irq_set_type = intel_gpio_irq_type,
1115 .irq_set_wake = intel_gpio_irq_wake,
Rushikesh S Kadam5ff56b02017-08-11 13:53:44 +05301116 .flags = IRQCHIP_MASK_ON_SUSPEND,
Mika Westerberg7981c0012015-03-30 17:31:49 +03001117};
1118
Mika Westerberga60eac32017-11-27 16:54:43 +03001119static int intel_gpio_add_pin_ranges(struct intel_pinctrl *pctrl,
1120 const struct intel_community *community)
1121{
Colin Ian King33b6cb52017-12-04 17:08:15 +00001122 int ret = 0, i;
Mika Westerberga60eac32017-11-27 16:54:43 +03001123
1124 for (i = 0; i < community->ngpps; i++) {
1125 const struct intel_padgroup *gpp = &community->gpps[i];
1126
1127 if (gpp->gpio_base < 0)
1128 continue;
1129
1130 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1131 gpp->gpio_base, gpp->base,
1132 gpp->size);
1133 if (ret)
1134 return ret;
1135 }
1136
1137 return ret;
1138}
1139
1140static unsigned intel_gpio_ngpio(const struct intel_pinctrl *pctrl)
1141{
1142 const struct intel_community *community;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001143 unsigned int ngpio = 0;
Mika Westerberga60eac32017-11-27 16:54:43 +03001144 int i, j;
1145
1146 for (i = 0; i < pctrl->ncommunities; i++) {
1147 community = &pctrl->communities[i];
1148 for (j = 0; j < community->ngpps; j++) {
1149 const struct intel_padgroup *gpp = &community->gpps[j];
1150
1151 if (gpp->gpio_base < 0)
1152 continue;
1153
1154 if (gpp->gpio_base + gpp->size > ngpio)
1155 ngpio = gpp->gpio_base + gpp->size;
1156 }
1157 }
1158
1159 return ngpio;
1160}
1161
Mika Westerberg7981c0012015-03-30 17:31:49 +03001162static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1163{
Mika Westerberga60eac32017-11-27 16:54:43 +03001164 int ret, i;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001165
1166 pctrl->chip = intel_gpio_chip;
1167
Mika Westerberga60eac32017-11-27 16:54:43 +03001168 pctrl->chip.ngpio = intel_gpio_ngpio(pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001169 pctrl->chip.label = dev_name(pctrl->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001170 pctrl->chip.parent = pctrl->dev;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001171 pctrl->chip.base = -1;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001172 pctrl->irq = irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001173
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001174 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001175 if (ret) {
1176 dev_err(pctrl->dev, "failed to register gpiochip\n");
1177 return ret;
1178 }
1179
Mika Westerberga60eac32017-11-27 16:54:43 +03001180 for (i = 0; i < pctrl->ncommunities; i++) {
1181 struct intel_community *community = &pctrl->communities[i];
1182
1183 ret = intel_gpio_add_pin_ranges(pctrl, community);
1184 if (ret) {
1185 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1186 return ret;
1187 }
Mika Westerberg193b40c2015-10-21 13:08:43 +03001188 }
1189
1190 /*
1191 * We need to request the interrupt here (instead of providing chip
1192 * to the irq directly) because on some platforms several GPIO
1193 * controllers share the same interrupt line.
1194 */
Mika Westerberg1a7d1cb2016-06-16 11:25:37 +03001195 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1196 IRQF_SHARED | IRQF_NO_THREAD,
Mika Westerberg193b40c2015-10-21 13:08:43 +03001197 dev_name(pctrl->dev), pctrl);
1198 if (ret) {
1199 dev_err(pctrl->dev, "failed to request interrupt\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001200 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001201 }
1202
1203 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
Andy Shevchenko3ae02c12016-11-25 13:31:16 +02001204 handle_bad_irq, IRQ_TYPE_NONE);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001205 if (ret) {
1206 dev_err(pctrl->dev, "failed to add irqchip\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001207 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001208 }
1209
1210 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
Mika Westerberg193b40c2015-10-21 13:08:43 +03001211 NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001212 return 0;
1213}
1214
Mika Westerberg919eb472017-06-06 16:18:17 +03001215static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1216 struct intel_community *community)
1217{
1218 struct intel_padgroup *gpps;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001219 unsigned int npins = community->npins;
1220 unsigned int padown_num = 0;
Mika Westerberg919eb472017-06-06 16:18:17 +03001221 size_t ngpps, i;
1222
1223 if (community->gpps)
1224 ngpps = community->ngpps;
1225 else
1226 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1227
1228 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1229 if (!gpps)
1230 return -ENOMEM;
1231
1232 for (i = 0; i < ngpps; i++) {
1233 if (community->gpps) {
1234 gpps[i] = community->gpps[i];
1235 } else {
Andy Shevchenko04035f72018-09-26 17:50:26 +03001236 unsigned int gpp_size = community->gpp_size;
Mika Westerberg919eb472017-06-06 16:18:17 +03001237
1238 gpps[i].reg_num = i;
1239 gpps[i].base = community->pin_base + i * gpp_size;
1240 gpps[i].size = min(gpp_size, npins);
1241 npins -= gpps[i].size;
1242 }
1243
1244 if (gpps[i].size > 32)
1245 return -EINVAL;
1246
Mika Westerberga60eac32017-11-27 16:54:43 +03001247 if (!gpps[i].gpio_base)
1248 gpps[i].gpio_base = gpps[i].base;
1249
Mika Westerberg919eb472017-06-06 16:18:17 +03001250 gpps[i].padown_num = padown_num;
1251
1252 /*
1253 * In older hardware the number of padown registers per
1254 * group is fixed regardless of the group size.
1255 */
1256 if (community->gpp_num_padown_regs)
1257 padown_num += community->gpp_num_padown_regs;
1258 else
1259 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1260 }
1261
1262 community->ngpps = ngpps;
1263 community->gpps = gpps;
1264
1265 return 0;
1266}
1267
Mika Westerberg7981c0012015-03-30 17:31:49 +03001268static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1269{
1270#ifdef CONFIG_PM_SLEEP
1271 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1272 struct intel_community_context *communities;
1273 struct intel_pad_context *pads;
1274 int i;
1275
1276 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1277 if (!pads)
1278 return -ENOMEM;
1279
1280 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1281 sizeof(*communities), GFP_KERNEL);
1282 if (!communities)
1283 return -ENOMEM;
1284
1285
1286 for (i = 0; i < pctrl->ncommunities; i++) {
1287 struct intel_community *community = &pctrl->communities[i];
Chris Chiua0a5f762019-04-15 13:53:58 +08001288 u32 *intmask, *hostown;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001289
1290 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1291 sizeof(*intmask), GFP_KERNEL);
1292 if (!intmask)
1293 return -ENOMEM;
1294
1295 communities[i].intmask = intmask;
Chris Chiua0a5f762019-04-15 13:53:58 +08001296
1297 hostown = devm_kcalloc(pctrl->dev, community->ngpps,
1298 sizeof(*hostown), GFP_KERNEL);
1299 if (!hostown)
1300 return -ENOMEM;
1301
1302 communities[i].hostown = hostown;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001303 }
1304
1305 pctrl->context.pads = pads;
1306 pctrl->context.communities = communities;
1307#endif
1308
1309 return 0;
1310}
1311
Andy Shevchenko0dd519e2018-10-17 19:10:27 +03001312static int intel_pinctrl_probe(struct platform_device *pdev,
1313 const struct intel_pinctrl_soc_data *soc_data)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001314{
1315 struct intel_pinctrl *pctrl;
1316 int i, ret, irq;
1317
1318 if (!soc_data)
1319 return -EINVAL;
1320
1321 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1322 if (!pctrl)
1323 return -ENOMEM;
1324
1325 pctrl->dev = &pdev->dev;
1326 pctrl->soc = soc_data;
Mika Westerberg27d90982016-06-16 11:25:36 +03001327 raw_spin_lock_init(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001328
1329 /*
1330 * Make a copy of the communities which we can use to hold pointers
1331 * to the registers.
1332 */
1333 pctrl->ncommunities = pctrl->soc->ncommunities;
1334 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1335 sizeof(*pctrl->communities), GFP_KERNEL);
1336 if (!pctrl->communities)
1337 return -ENOMEM;
1338
1339 for (i = 0; i < pctrl->ncommunities; i++) {
1340 struct intel_community *community = &pctrl->communities[i];
1341 struct resource *res;
1342 void __iomem *regs;
1343 u32 padbar;
1344
1345 *community = pctrl->soc->communities[i];
1346
1347 res = platform_get_resource(pdev, IORESOURCE_MEM,
1348 community->barno);
1349 regs = devm_ioremap_resource(&pdev->dev, res);
1350 if (IS_ERR(regs))
1351 return PTR_ERR(regs);
1352
Mika Westerberge57725e2017-01-27 13:07:14 +03001353 /*
1354 * Determine community features based on the revision if
1355 * not specified already.
1356 */
1357 if (!community->features) {
1358 u32 rev;
1359
1360 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001361 if (rev >= 0x94) {
Mika Westerberge57725e2017-01-27 13:07:14 +03001362 community->features |= PINCTRL_FEATURE_DEBOUNCE;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001363 community->features |= PINCTRL_FEATURE_1K_PD;
1364 }
Mika Westerberge57725e2017-01-27 13:07:14 +03001365 }
1366
Mika Westerberg7981c0012015-03-30 17:31:49 +03001367 /* Read offset of the pad configuration registers */
1368 padbar = readl(regs + PADBAR);
1369
1370 community->regs = regs;
1371 community->pad_regs = regs + padbar;
Mika Westerberg919eb472017-06-06 16:18:17 +03001372
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001373 if (!community->is_offset)
1374 community->is_offset = GPI_IS;
1375
Mika Westerberg919eb472017-06-06 16:18:17 +03001376 ret = intel_pinctrl_add_padgroups(pctrl, community);
1377 if (ret)
1378 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001379 }
1380
1381 irq = platform_get_irq(pdev, 0);
1382 if (irq < 0) {
1383 dev_err(&pdev->dev, "failed to get interrupt number\n");
1384 return irq;
1385 }
1386
1387 ret = intel_pinctrl_pm_init(pctrl);
1388 if (ret)
1389 return ret;
1390
1391 pctrl->pctldesc = intel_pinctrl_desc;
1392 pctrl->pctldesc.name = dev_name(&pdev->dev);
1393 pctrl->pctldesc.pins = pctrl->soc->pins;
1394 pctrl->pctldesc.npins = pctrl->soc->npins;
1395
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301396 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1397 pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001398 if (IS_ERR(pctrl->pctldev)) {
Mika Westerberg7981c0012015-03-30 17:31:49 +03001399 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001400 return PTR_ERR(pctrl->pctldev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001401 }
1402
1403 ret = intel_gpio_probe(pctrl, irq);
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301404 if (ret)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001405 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001406
1407 platform_set_drvdata(pdev, pctrl);
1408
1409 return 0;
1410}
Mika Westerberg7981c0012015-03-30 17:31:49 +03001411
Andy Shevchenko70c263c2018-08-30 19:27:40 +03001412int intel_pinctrl_probe_by_hid(struct platform_device *pdev)
1413{
1414 const struct intel_pinctrl_soc_data *data;
1415
1416 data = device_get_match_data(&pdev->dev);
1417 return intel_pinctrl_probe(pdev, data);
1418}
1419EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_hid);
1420
Andy Shevchenko924cf802018-08-30 19:27:36 +03001421int intel_pinctrl_probe_by_uid(struct platform_device *pdev)
1422{
1423 const struct intel_pinctrl_soc_data *data = NULL;
1424 const struct intel_pinctrl_soc_data **table;
1425 struct acpi_device *adev;
1426 unsigned int i;
1427
1428 adev = ACPI_COMPANION(&pdev->dev);
1429 if (adev) {
1430 const void *match = device_get_match_data(&pdev->dev);
1431
1432 table = (const struct intel_pinctrl_soc_data **)match;
1433 for (i = 0; table[i]; i++) {
1434 if (!strcmp(adev->pnp.unique_id, table[i]->uid)) {
1435 data = table[i];
1436 break;
1437 }
1438 }
1439 } else {
1440 const struct platform_device_id *id;
1441
1442 id = platform_get_device_id(pdev);
1443 if (!id)
1444 return -ENODEV;
1445
1446 table = (const struct intel_pinctrl_soc_data **)id->driver_data;
1447 data = table[pdev->id];
1448 }
1449 if (!data)
1450 return -ENODEV;
1451
1452 return intel_pinctrl_probe(pdev, data);
1453}
1454EXPORT_SYMBOL_GPL(intel_pinctrl_probe_by_uid);
1455
Mika Westerberg7981c0012015-03-30 17:31:49 +03001456#ifdef CONFIG_PM_SLEEP
Andy Shevchenko04035f72018-09-26 17:50:26 +03001457static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned int pin)
Mika Westerbergc538b942016-10-10 16:39:31 +03001458{
1459 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1460
1461 if (!pd || !intel_pad_usable(pctrl, pin))
1462 return false;
1463
1464 /*
1465 * Only restore the pin if it is actually in use by the kernel (or
1466 * by userspace). It is possible that some pins are used by the
1467 * BIOS during resume and those are not always locked down so leave
1468 * them alone.
1469 */
1470 if (pd->mux_owner || pd->gpio_owner ||
1471 gpiochip_line_is_irq(&pctrl->chip, pin))
1472 return true;
1473
1474 return false;
1475}
1476
Binbin Wu2fef3272019-04-08 18:49:26 +08001477int intel_pinctrl_suspend_noirq(struct device *dev)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001478{
Wolfram Sangcb035d72018-10-21 22:00:29 +02001479 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001480 struct intel_community_context *communities;
1481 struct intel_pad_context *pads;
1482 int i;
1483
1484 pads = pctrl->context.pads;
1485 for (i = 0; i < pctrl->soc->npins; i++) {
1486 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
Mika Westerberge57725e2017-01-27 13:07:14 +03001487 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001488 u32 val;
1489
Mika Westerbergc538b942016-10-10 16:39:31 +03001490 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001491 continue;
1492
1493 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1494 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1495 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1496 pads[i].padcfg1 = val;
Mika Westerberge57725e2017-01-27 13:07:14 +03001497
1498 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1499 if (padcfg)
1500 pads[i].padcfg2 = readl(padcfg);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001501 }
1502
1503 communities = pctrl->context.communities;
1504 for (i = 0; i < pctrl->ncommunities; i++) {
1505 struct intel_community *community = &pctrl->communities[i];
1506 void __iomem *base;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001507 unsigned int gpp;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001508
1509 base = community->regs + community->ie_offset;
1510 for (gpp = 0; gpp < community->ngpps; gpp++)
1511 communities[i].intmask[gpp] = readl(base + gpp * 4);
Chris Chiua0a5f762019-04-15 13:53:58 +08001512
1513 base = community->regs + community->hostown_offset;
1514 for (gpp = 0; gpp < community->ngpps; gpp++)
1515 communities[i].hostown[gpp] = readl(base + gpp * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001516 }
1517
1518 return 0;
1519}
Binbin Wu2fef3272019-04-08 18:49:26 +08001520EXPORT_SYMBOL_GPL(intel_pinctrl_suspend_noirq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001521
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001522static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1523{
1524 size_t i;
1525
1526 for (i = 0; i < pctrl->ncommunities; i++) {
1527 const struct intel_community *community;
1528 void __iomem *base;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001529 unsigned int gpp;
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001530
1531 community = &pctrl->communities[i];
1532 base = community->regs;
1533
1534 for (gpp = 0; gpp < community->ngpps; gpp++) {
1535 /* Mask and clear all interrupts */
1536 writel(0, base + community->ie_offset + gpp * 4);
Mika Westerbergcf769bd2017-10-23 15:40:25 +03001537 writel(0xffff, base + community->is_offset + gpp * 4);
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001538 }
1539 }
1540}
1541
Chris Chiua0a5f762019-04-15 13:53:58 +08001542static u32
1543intel_gpio_is_requested(struct gpio_chip *chip, int base, unsigned int size)
1544{
1545 u32 requested = 0;
1546 unsigned int i;
1547
1548 for (i = 0; i < size; i++)
1549 if (gpiochip_is_requested(chip, base + i))
1550 requested |= BIT(i);
1551
1552 return requested;
1553}
1554
1555static u32
1556intel_gpio_update_pad_mode(void __iomem *hostown, u32 mask, u32 value)
1557{
Andy Shevchenko5f61d952019-04-28 20:19:06 +03001558 u32 curr, updated;
Chris Chiua0a5f762019-04-15 13:53:58 +08001559
Andy Shevchenko5f61d952019-04-28 20:19:06 +03001560 curr = readl(hostown);
1561 updated = (curr & ~mask) | (value & mask);
Chris Chiua0a5f762019-04-15 13:53:58 +08001562 writel(updated, hostown);
Andy Shevchenko5f61d952019-04-28 20:19:06 +03001563
Chris Chiua0a5f762019-04-15 13:53:58 +08001564 return curr;
1565}
1566
Binbin Wu2fef3272019-04-08 18:49:26 +08001567int intel_pinctrl_resume_noirq(struct device *dev)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001568{
Wolfram Sangcb035d72018-10-21 22:00:29 +02001569 struct intel_pinctrl *pctrl = dev_get_drvdata(dev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001570 const struct intel_community_context *communities;
1571 const struct intel_pad_context *pads;
1572 int i;
1573
1574 /* Mask all interrupts */
1575 intel_gpio_irq_init(pctrl);
1576
1577 pads = pctrl->context.pads;
1578 for (i = 0; i < pctrl->soc->npins; i++) {
1579 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1580 void __iomem *padcfg;
1581 u32 val;
1582
Mika Westerbergc538b942016-10-10 16:39:31 +03001583 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001584 continue;
1585
1586 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1587 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1588 if (val != pads[i].padcfg0) {
1589 writel(pads[i].padcfg0, padcfg);
1590 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1591 desc->number, readl(padcfg));
1592 }
1593
1594 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1595 val = readl(padcfg);
1596 if (val != pads[i].padcfg1) {
1597 writel(pads[i].padcfg1, padcfg);
1598 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1599 desc->number, readl(padcfg));
1600 }
Mika Westerberge57725e2017-01-27 13:07:14 +03001601
1602 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1603 if (padcfg) {
1604 val = readl(padcfg);
1605 if (val != pads[i].padcfg2) {
1606 writel(pads[i].padcfg2, padcfg);
1607 dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1608 desc->number, readl(padcfg));
1609 }
1610 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001611 }
1612
1613 communities = pctrl->context.communities;
1614 for (i = 0; i < pctrl->ncommunities; i++) {
1615 struct intel_community *community = &pctrl->communities[i];
1616 void __iomem *base;
Andy Shevchenko04035f72018-09-26 17:50:26 +03001617 unsigned int gpp;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001618
1619 base = community->regs + community->ie_offset;
1620 for (gpp = 0; gpp < community->ngpps; gpp++) {
1621 writel(communities[i].intmask[gpp], base + gpp * 4);
1622 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1623 readl(base + gpp * 4));
1624 }
Chris Chiua0a5f762019-04-15 13:53:58 +08001625
1626 base = community->regs + community->hostown_offset;
1627 for (gpp = 0; gpp < community->ngpps; gpp++) {
1628 const struct intel_padgroup *padgrp = &community->gpps[gpp];
1629 u32 requested = 0, value = 0;
1630 u32 saved = communities[i].hostown[gpp];
1631
1632 if (padgrp->gpio_base < 0)
1633 continue;
1634
1635 requested = intel_gpio_is_requested(&pctrl->chip,
1636 padgrp->gpio_base, padgrp->size);
1637 value = intel_gpio_update_pad_mode(base + gpp * 4,
1638 requested, saved);
1639 if ((value ^ saved) & requested) {
1640 dev_warn(dev, "restore hostown %d/%u %#8x->%#8x\n",
1641 i, gpp, value, saved);
1642 }
1643 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001644 }
1645
1646 return 0;
1647}
Binbin Wu2fef3272019-04-08 18:49:26 +08001648EXPORT_SYMBOL_GPL(intel_pinctrl_resume_noirq);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001649#endif
1650
1651MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1652MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1653MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1654MODULE_LICENSE("GPL v2");