blob: 78c48497c9e665378dda95a95ab5e7d0fbde1dac [file] [log] [blame]
Mika Westerberg7981c0012015-03-30 17:31:49 +03001/*
2 * Intel pinctrl/GPIO core driver.
3 *
4 * Copyright (C) 2015, Intel Corporation
5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
Mika Westerberg193b40c2015-10-21 13:08:43 +030014#include <linux/interrupt.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030015#include <linux/gpio/driver.h>
Mika Westerberge57725e2017-01-27 13:07:14 +030016#include <linux/log2.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030017#include <linux/platform_device.h>
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
33#define GPI_GPE_STS 0x140
34#define GPI_GPE_EN 0x160
35
36#define PADOWN_BITS 4
37#define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
38#define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
Qipeng Zha99a735b2015-11-30 19:20:16 +080039#define PADOWN_GPP(p) ((p) / 8)
Mika Westerberg7981c0012015-03-30 17:31:49 +030040
41/* Offset from pad_regs */
42#define PADCFG0 0x000
43#define PADCFG0_RXEVCFG_SHIFT 25
44#define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
45#define PADCFG0_RXEVCFG_LEVEL 0
46#define PADCFG0_RXEVCFG_EDGE 1
47#define PADCFG0_RXEVCFG_DISABLED 2
48#define PADCFG0_RXEVCFG_EDGE_BOTH 3
Mika Westerberge57725e2017-01-27 13:07:14 +030049#define PADCFG0_PREGFRXSEL BIT(24)
Mika Westerberg7981c0012015-03-30 17:31:49 +030050#define PADCFG0_RXINV BIT(23)
51#define PADCFG0_GPIROUTIOXAPIC BIT(20)
52#define PADCFG0_GPIROUTSCI BIT(19)
53#define PADCFG0_GPIROUTSMI BIT(18)
54#define PADCFG0_GPIROUTNMI BIT(17)
55#define PADCFG0_PMODE_SHIFT 10
56#define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
57#define PADCFG0_GPIORXDIS BIT(9)
58#define PADCFG0_GPIOTXDIS BIT(8)
59#define PADCFG0_GPIORXSTATE BIT(1)
60#define PADCFG0_GPIOTXSTATE BIT(0)
61
62#define PADCFG1 0x004
63#define PADCFG1_TERM_UP BIT(13)
64#define PADCFG1_TERM_SHIFT 10
65#define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
66#define PADCFG1_TERM_20K 4
67#define PADCFG1_TERM_2K 3
68#define PADCFG1_TERM_5K 2
69#define PADCFG1_TERM_1K 1
70
Mika Westerberge57725e2017-01-27 13:07:14 +030071#define PADCFG2 0x008
72#define PADCFG2_DEBEN BIT(0)
73#define PADCFG2_DEBOUNCE_SHIFT 1
74#define PADCFG2_DEBOUNCE_MASK GENMASK(4, 1)
75
76#define DEBOUNCE_PERIOD 31250 /* ns */
77
Mika Westerberg7981c0012015-03-30 17:31:49 +030078struct intel_pad_context {
79 u32 padcfg0;
80 u32 padcfg1;
Mika Westerberge57725e2017-01-27 13:07:14 +030081 u32 padcfg2;
Mika Westerberg7981c0012015-03-30 17:31:49 +030082};
83
84struct intel_community_context {
85 u32 *intmask;
86};
87
88struct intel_pinctrl_context {
89 struct intel_pad_context *pads;
90 struct intel_community_context *communities;
91};
92
93/**
94 * struct intel_pinctrl - Intel pinctrl private structure
95 * @dev: Pointer to the device structure
96 * @lock: Lock to serialize register access
97 * @pctldesc: Pin controller description
98 * @pctldev: Pointer to the pin controller device
99 * @chip: GPIO chip in this pin controller
100 * @soc: SoC/PCH specific pin configuration data
101 * @communities: All communities in this pin controller
102 * @ncommunities: Number of communities in this pin controller
103 * @context: Configuration saved over system sleep
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700104 * @irq: pinctrl/GPIO chip irq number
Mika Westerberg7981c0012015-03-30 17:31:49 +0300105 */
106struct intel_pinctrl {
107 struct device *dev;
Mika Westerberg27d90982016-06-16 11:25:36 +0300108 raw_spinlock_t lock;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300109 struct pinctrl_desc pctldesc;
110 struct pinctrl_dev *pctldev;
111 struct gpio_chip chip;
112 const struct intel_pinctrl_soc_data *soc;
113 struct intel_community *communities;
114 size_t ncommunities;
115 struct intel_pinctrl_context context;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700116 int irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300117};
118
Mika Westerberg7981c0012015-03-30 17:31:49 +0300119#define pin_to_padno(c, p) ((p) - (c)->pin_base)
Mika Westerberg919eb472017-06-06 16:18:17 +0300120#define padgroup_offset(g, p) ((p) - (g)->base)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300121
122static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
123 unsigned pin)
124{
125 struct intel_community *community;
126 int i;
127
128 for (i = 0; i < pctrl->ncommunities; i++) {
129 community = &pctrl->communities[i];
130 if (pin >= community->pin_base &&
131 pin < community->pin_base + community->npins)
132 return community;
133 }
134
135 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
136 return NULL;
137}
138
Mika Westerberg919eb472017-06-06 16:18:17 +0300139static const struct intel_padgroup *
140intel_community_get_padgroup(const struct intel_community *community,
141 unsigned pin)
142{
143 int i;
144
145 for (i = 0; i < community->ngpps; i++) {
146 const struct intel_padgroup *padgrp = &community->gpps[i];
147
148 if (pin >= padgrp->base && pin < padgrp->base + padgrp->size)
149 return padgrp;
150 }
151
152 return NULL;
153}
154
Mika Westerberg7981c0012015-03-30 17:31:49 +0300155static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
156 unsigned reg)
157{
158 const struct intel_community *community;
159 unsigned padno;
Mika Westerberge57725e2017-01-27 13:07:14 +0300160 size_t nregs;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300161
162 community = intel_get_community(pctrl, pin);
163 if (!community)
164 return NULL;
165
166 padno = pin_to_padno(community, pin);
Mika Westerberge57725e2017-01-27 13:07:14 +0300167 nregs = (community->features & PINCTRL_FEATURE_DEBOUNCE) ? 4 : 2;
168
169 if (reg == PADCFG2 && !(community->features & PINCTRL_FEATURE_DEBOUNCE))
170 return NULL;
171
172 return community->pad_regs + reg + padno * nregs * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300173}
174
175static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
176{
177 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300178 const struct intel_padgroup *padgrp;
179 unsigned gpp, offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300180 void __iomem *padown;
181
182 community = intel_get_community(pctrl, pin);
183 if (!community)
184 return false;
185 if (!community->padown_offset)
186 return true;
187
Mika Westerberg919eb472017-06-06 16:18:17 +0300188 padgrp = intel_community_get_padgroup(community, pin);
189 if (!padgrp)
190 return false;
191
192 gpp_offset = padgroup_offset(padgrp, pin);
193 gpp = PADOWN_GPP(gpp_offset);
194 offset = community->padown_offset + padgrp->padown_num * 4 + gpp * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300195 padown = community->regs + offset;
196
Mika Westerberg919eb472017-06-06 16:18:17 +0300197 return !(readl(padown) & PADOWN_MASK(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300198}
199
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300200static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300201{
202 const struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300203 const struct intel_padgroup *padgrp;
204 unsigned offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300205 void __iomem *hostown;
206
207 community = intel_get_community(pctrl, pin);
208 if (!community)
209 return true;
210 if (!community->hostown_offset)
211 return false;
212
Mika Westerberg919eb472017-06-06 16:18:17 +0300213 padgrp = intel_community_get_padgroup(community, pin);
214 if (!padgrp)
215 return true;
216
217 gpp_offset = padgroup_offset(padgrp, pin);
218 offset = community->hostown_offset + padgrp->reg_num * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300219 hostown = community->regs + offset;
220
Mika Westerberg919eb472017-06-06 16:18:17 +0300221 return !(readl(hostown) & BIT(gpp_offset));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300222}
223
224static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
225{
226 struct intel_community *community;
Mika Westerberg919eb472017-06-06 16:18:17 +0300227 const struct intel_padgroup *padgrp;
228 unsigned offset, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300229 u32 value;
230
231 community = intel_get_community(pctrl, pin);
232 if (!community)
233 return true;
234 if (!community->padcfglock_offset)
235 return false;
236
Mika Westerberg919eb472017-06-06 16:18:17 +0300237 padgrp = intel_community_get_padgroup(community, pin);
238 if (!padgrp)
239 return true;
240
241 gpp_offset = padgroup_offset(padgrp, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300242
243 /*
244 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
245 * the pad is considered unlocked. Any other case means that it is
246 * either fully or partially locked and we don't touch it.
247 */
Mika Westerberg919eb472017-06-06 16:18:17 +0300248 offset = community->padcfglock_offset + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300249 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300250 if (value & BIT(gpp_offset))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300251 return true;
252
Mika Westerberg919eb472017-06-06 16:18:17 +0300253 offset = community->padcfglock_offset + 4 + padgrp->reg_num * 8;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300254 value = readl(community->regs + offset);
Mika Westerberg919eb472017-06-06 16:18:17 +0300255 if (value & BIT(gpp_offset))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300256 return true;
257
258 return false;
259}
260
261static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
262{
263 return intel_pad_owned_by_host(pctrl, pin) &&
Mika Westerberg7981c0012015-03-30 17:31:49 +0300264 !intel_pad_locked(pctrl, pin);
265}
266
267static int intel_get_groups_count(struct pinctrl_dev *pctldev)
268{
269 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
270
271 return pctrl->soc->ngroups;
272}
273
274static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
275 unsigned group)
276{
277 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
278
279 return pctrl->soc->groups[group].name;
280}
281
282static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
283 const unsigned **pins, unsigned *npins)
284{
285 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
286
287 *pins = pctrl->soc->groups[group].pins;
288 *npins = pctrl->soc->groups[group].npins;
289 return 0;
290}
291
292static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
293 unsigned pin)
294{
295 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
Mika Westerberge57725e2017-01-27 13:07:14 +0300296 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300297 u32 cfg0, cfg1, mode;
298 bool locked, acpi;
299
300 if (!intel_pad_owned_by_host(pctrl, pin)) {
301 seq_puts(s, "not available");
302 return;
303 }
304
305 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
306 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
307
308 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
309 if (!mode)
310 seq_puts(s, "GPIO ");
311 else
312 seq_printf(s, "mode %d ", mode);
313
314 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
315
Mika Westerberge57725e2017-01-27 13:07:14 +0300316 /* Dump the additional PADCFG registers if available */
317 padcfg = intel_get_padcfg(pctrl, pin, PADCFG2);
318 if (padcfg)
319 seq_printf(s, " 0x%08x", readl(padcfg));
320
Mika Westerberg7981c0012015-03-30 17:31:49 +0300321 locked = intel_pad_locked(pctrl, pin);
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300322 acpi = intel_pad_acpi_mode(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300323
324 if (locked || acpi) {
325 seq_puts(s, " [");
326 if (locked) {
327 seq_puts(s, "LOCKED");
328 if (acpi)
329 seq_puts(s, ", ");
330 }
331 if (acpi)
332 seq_puts(s, "ACPI");
333 seq_puts(s, "]");
334 }
335}
336
337static const struct pinctrl_ops intel_pinctrl_ops = {
338 .get_groups_count = intel_get_groups_count,
339 .get_group_name = intel_get_group_name,
340 .get_group_pins = intel_get_group_pins,
341 .pin_dbg_show = intel_pin_dbg_show,
342};
343
344static int intel_get_functions_count(struct pinctrl_dev *pctldev)
345{
346 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
347
348 return pctrl->soc->nfunctions;
349}
350
351static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
352 unsigned function)
353{
354 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
355
356 return pctrl->soc->functions[function].name;
357}
358
359static int intel_get_function_groups(struct pinctrl_dev *pctldev,
360 unsigned function,
361 const char * const **groups,
362 unsigned * const ngroups)
363{
364 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
365
366 *groups = pctrl->soc->functions[function].groups;
367 *ngroups = pctrl->soc->functions[function].ngroups;
368 return 0;
369}
370
371static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
372 unsigned group)
373{
374 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
375 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
376 unsigned long flags;
377 int i;
378
Mika Westerberg27d90982016-06-16 11:25:36 +0300379 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300380
381 /*
382 * All pins in the groups needs to be accessible and writable
383 * before we can enable the mux for this group.
384 */
385 for (i = 0; i < grp->npins; i++) {
386 if (!intel_pad_usable(pctrl, grp->pins[i])) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300387 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300388 return -EBUSY;
389 }
390 }
391
392 /* Now enable the mux setting for each pin in the group */
393 for (i = 0; i < grp->npins; i++) {
394 void __iomem *padcfg0;
395 u32 value;
396
397 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
398 value = readl(padcfg0);
399
400 value &= ~PADCFG0_PMODE_MASK;
401 value |= grp->mode << PADCFG0_PMODE_SHIFT;
402
403 writel(value, padcfg0);
404 }
405
Mika Westerberg27d90982016-06-16 11:25:36 +0300406 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300407
408 return 0;
409}
410
Andy Shevchenko17fab472017-01-02 14:07:22 +0200411static void __intel_gpio_set_direction(void __iomem *padcfg0, bool input)
412{
413 u32 value;
414
415 value = readl(padcfg0);
416 if (input) {
417 value &= ~PADCFG0_GPIORXDIS;
418 value |= PADCFG0_GPIOTXDIS;
419 } else {
420 value &= ~PADCFG0_GPIOTXDIS;
421 value |= PADCFG0_GPIORXDIS;
422 }
423 writel(value, padcfg0);
424}
425
Mika Westerberg7981c0012015-03-30 17:31:49 +0300426static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
427 struct pinctrl_gpio_range *range,
428 unsigned pin)
429{
430 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
431 void __iomem *padcfg0;
432 unsigned long flags;
433 u32 value;
434
Mika Westerberg27d90982016-06-16 11:25:36 +0300435 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300436
437 if (!intel_pad_usable(pctrl, pin)) {
Mika Westerberg27d90982016-06-16 11:25:36 +0300438 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300439 return -EBUSY;
440 }
441
442 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
443 /* Put the pad into GPIO mode */
444 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
445 /* Disable SCI/SMI/NMI generation */
446 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
447 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300448 writel(value, padcfg0);
449
Andy Shevchenko17fab472017-01-02 14:07:22 +0200450 /* Disable TX buffer and enable RX (this will be input) */
451 __intel_gpio_set_direction(padcfg0, true);
452
Mika Westerberg27d90982016-06-16 11:25:36 +0300453 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300454
455 return 0;
456}
457
458static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
459 struct pinctrl_gpio_range *range,
460 unsigned pin, bool input)
461{
462 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
463 void __iomem *padcfg0;
464 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300465
Mika Westerberg27d90982016-06-16 11:25:36 +0300466 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300467
468 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
Andy Shevchenko17fab472017-01-02 14:07:22 +0200469 __intel_gpio_set_direction(padcfg0, input);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300470
Mika Westerberg27d90982016-06-16 11:25:36 +0300471 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300472
473 return 0;
474}
475
476static const struct pinmux_ops intel_pinmux_ops = {
477 .get_functions_count = intel_get_functions_count,
478 .get_function_name = intel_get_function_name,
479 .get_function_groups = intel_get_function_groups,
480 .set_mux = intel_pinmux_set_mux,
481 .gpio_request_enable = intel_gpio_request_enable,
482 .gpio_set_direction = intel_gpio_set_direction,
483};
484
485static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
486 unsigned long *config)
487{
488 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
489 enum pin_config_param param = pinconf_to_config_param(*config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300490 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300491 u32 value, term;
Mika Westerberge57725e2017-01-27 13:07:14 +0300492 u32 arg = 0;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300493
494 if (!intel_pad_owned_by_host(pctrl, pin))
495 return -ENOTSUPP;
496
Mika Westerberg04cc0582017-01-27 13:07:15 +0300497 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300498 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
499 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
500
501 switch (param) {
502 case PIN_CONFIG_BIAS_DISABLE:
503 if (term)
504 return -EINVAL;
505 break;
506
507 case PIN_CONFIG_BIAS_PULL_UP:
508 if (!term || !(value & PADCFG1_TERM_UP))
509 return -EINVAL;
510
511 switch (term) {
512 case PADCFG1_TERM_1K:
513 arg = 1000;
514 break;
515 case PADCFG1_TERM_2K:
516 arg = 2000;
517 break;
518 case PADCFG1_TERM_5K:
519 arg = 5000;
520 break;
521 case PADCFG1_TERM_20K:
522 arg = 20000;
523 break;
524 }
525
526 break;
527
528 case PIN_CONFIG_BIAS_PULL_DOWN:
529 if (!term || value & PADCFG1_TERM_UP)
530 return -EINVAL;
531
532 switch (term) {
Mika Westerberg04cc0582017-01-27 13:07:15 +0300533 case PADCFG1_TERM_1K:
534 if (!(community->features & PINCTRL_FEATURE_1K_PD))
535 return -EINVAL;
536 arg = 1000;
537 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300538 case PADCFG1_TERM_5K:
539 arg = 5000;
540 break;
541 case PADCFG1_TERM_20K:
542 arg = 20000;
543 break;
544 }
545
546 break;
547
Mika Westerberge57725e2017-01-27 13:07:14 +0300548 case PIN_CONFIG_INPUT_DEBOUNCE: {
549 void __iomem *padcfg2;
550 u32 v;
551
552 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
553 if (!padcfg2)
554 return -ENOTSUPP;
555
556 v = readl(padcfg2);
557 if (!(v & PADCFG2_DEBEN))
558 return -EINVAL;
559
560 v = (v & PADCFG2_DEBOUNCE_MASK) >> PADCFG2_DEBOUNCE_SHIFT;
561 arg = BIT(v) * DEBOUNCE_PERIOD / 1000;
562
563 break;
564 }
565
Mika Westerberg7981c0012015-03-30 17:31:49 +0300566 default:
567 return -ENOTSUPP;
568 }
569
570 *config = pinconf_to_config_packed(param, arg);
571 return 0;
572}
573
574static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
575 unsigned long config)
576{
577 unsigned param = pinconf_to_config_param(config);
578 unsigned arg = pinconf_to_config_argument(config);
Mika Westerberg04cc0582017-01-27 13:07:15 +0300579 const struct intel_community *community;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300580 void __iomem *padcfg1;
581 unsigned long flags;
582 int ret = 0;
583 u32 value;
584
Mika Westerberg27d90982016-06-16 11:25:36 +0300585 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300586
Mika Westerberg04cc0582017-01-27 13:07:15 +0300587 community = intel_get_community(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300588 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
589 value = readl(padcfg1);
590
591 switch (param) {
592 case PIN_CONFIG_BIAS_DISABLE:
593 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
594 break;
595
596 case PIN_CONFIG_BIAS_PULL_UP:
597 value &= ~PADCFG1_TERM_MASK;
598
599 value |= PADCFG1_TERM_UP;
600
601 switch (arg) {
602 case 20000:
603 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
604 break;
605 case 5000:
606 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
607 break;
608 case 2000:
609 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
610 break;
611 case 1000:
612 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
613 break;
614 default:
615 ret = -EINVAL;
616 }
617
618 break;
619
620 case PIN_CONFIG_BIAS_PULL_DOWN:
621 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
622
623 switch (arg) {
624 case 20000:
625 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
626 break;
627 case 5000:
628 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
629 break;
Mika Westerberg04cc0582017-01-27 13:07:15 +0300630 case 1000:
Dan Carpenteraa1dd802017-02-07 16:20:08 +0300631 if (!(community->features & PINCTRL_FEATURE_1K_PD)) {
632 ret = -EINVAL;
633 break;
634 }
Mika Westerberg04cc0582017-01-27 13:07:15 +0300635 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
636 break;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300637 default:
638 ret = -EINVAL;
639 }
640
641 break;
642 }
643
644 if (!ret)
645 writel(value, padcfg1);
646
Mika Westerberg27d90982016-06-16 11:25:36 +0300647 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300648
649 return ret;
650}
651
Mika Westerberge57725e2017-01-27 13:07:14 +0300652static int intel_config_set_debounce(struct intel_pinctrl *pctrl, unsigned pin,
653 unsigned debounce)
654{
655 void __iomem *padcfg0, *padcfg2;
656 unsigned long flags;
657 u32 value0, value2;
658 int ret = 0;
659
660 padcfg2 = intel_get_padcfg(pctrl, pin, PADCFG2);
661 if (!padcfg2)
662 return -ENOTSUPP;
663
664 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
665
666 raw_spin_lock_irqsave(&pctrl->lock, flags);
667
668 value0 = readl(padcfg0);
669 value2 = readl(padcfg2);
670
671 /* Disable glitch filter and debouncer */
672 value0 &= ~PADCFG0_PREGFRXSEL;
673 value2 &= ~(PADCFG2_DEBEN | PADCFG2_DEBOUNCE_MASK);
674
675 if (debounce) {
676 unsigned long v;
677
678 v = order_base_2(debounce * 1000 / DEBOUNCE_PERIOD);
679 if (v < 3 || v > 15) {
680 ret = -EINVAL;
681 goto exit_unlock;
682 } else {
683 /* Enable glitch filter and debouncer */
684 value0 |= PADCFG0_PREGFRXSEL;
685 value2 |= v << PADCFG2_DEBOUNCE_SHIFT;
686 value2 |= PADCFG2_DEBEN;
687 }
688 }
689
690 writel(value0, padcfg0);
691 writel(value2, padcfg2);
692
693exit_unlock:
694 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
695
696 return ret;
697}
698
Mika Westerberg7981c0012015-03-30 17:31:49 +0300699static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
700 unsigned long *configs, unsigned nconfigs)
701{
702 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
703 int i, ret;
704
705 if (!intel_pad_usable(pctrl, pin))
706 return -ENOTSUPP;
707
708 for (i = 0; i < nconfigs; i++) {
709 switch (pinconf_to_config_param(configs[i])) {
710 case PIN_CONFIG_BIAS_DISABLE:
711 case PIN_CONFIG_BIAS_PULL_UP:
712 case PIN_CONFIG_BIAS_PULL_DOWN:
713 ret = intel_config_set_pull(pctrl, pin, configs[i]);
714 if (ret)
715 return ret;
716 break;
717
Mika Westerberge57725e2017-01-27 13:07:14 +0300718 case PIN_CONFIG_INPUT_DEBOUNCE:
719 ret = intel_config_set_debounce(pctrl, pin,
720 pinconf_to_config_argument(configs[i]));
721 if (ret)
722 return ret;
723 break;
724
Mika Westerberg7981c0012015-03-30 17:31:49 +0300725 default:
726 return -ENOTSUPP;
727 }
728 }
729
730 return 0;
731}
732
733static const struct pinconf_ops intel_pinconf_ops = {
734 .is_generic = true,
735 .pin_config_get = intel_config_get,
736 .pin_config_set = intel_config_set,
737};
738
739static const struct pinctrl_desc intel_pinctrl_desc = {
740 .pctlops = &intel_pinctrl_ops,
741 .pmxops = &intel_pinmux_ops,
742 .confops = &intel_pinconf_ops,
743 .owner = THIS_MODULE,
744};
745
Mika Westerberg7981c0012015-03-30 17:31:49 +0300746static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
747{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100748 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300749 void __iomem *reg;
750
751 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
752 if (!reg)
753 return -EINVAL;
754
755 return !!(readl(reg) & PADCFG0_GPIORXSTATE);
756}
757
758static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
759{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100760 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300761 void __iomem *reg;
762
763 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
764 if (reg) {
765 unsigned long flags;
766 u32 padcfg0;
767
Mika Westerberg27d90982016-06-16 11:25:36 +0300768 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300769 padcfg0 = readl(reg);
770 if (value)
771 padcfg0 |= PADCFG0_GPIOTXSTATE;
772 else
773 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
774 writel(padcfg0, reg);
Mika Westerberg27d90982016-06-16 11:25:36 +0300775 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300776 }
777}
778
779static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
780{
781 return pinctrl_gpio_direction_input(chip->base + offset);
782}
783
784static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
785 int value)
786{
787 intel_gpio_set(chip, offset, value);
788 return pinctrl_gpio_direction_output(chip->base + offset);
789}
790
791static const struct gpio_chip intel_gpio_chip = {
792 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200793 .request = gpiochip_generic_request,
794 .free = gpiochip_generic_free,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300795 .direction_input = intel_gpio_direction_input,
796 .direction_output = intel_gpio_direction_output,
797 .get = intel_gpio_get,
798 .set = intel_gpio_set,
Mika Westerberge57725e2017-01-27 13:07:14 +0300799 .set_config = gpiochip_generic_config,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300800};
801
802static void intel_gpio_irq_ack(struct irq_data *d)
803{
804 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100805 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300806 const struct intel_community *community;
807 unsigned pin = irqd_to_hwirq(d);
808
Mika Westerberg7981c0012015-03-30 17:31:49 +0300809 community = intel_get_community(pctrl, pin);
810 if (community) {
Mika Westerberg919eb472017-06-06 16:18:17 +0300811 const struct intel_padgroup *padgrp;
812 unsigned gpp, gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300813
Mika Westerberg919eb472017-06-06 16:18:17 +0300814 padgrp = intel_community_get_padgroup(community, pin);
815 if (!padgrp)
816 return;
817
818 gpp = padgrp->reg_num;
819 gpp_offset = padgroup_offset(padgrp, pin);
820
821 raw_spin_lock(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300822 writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
Mika Westerberg919eb472017-06-06 16:18:17 +0300823 raw_spin_unlock(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300824 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300825}
826
Qi Zhenga939bb52016-03-17 02:15:26 +0800827static void intel_gpio_irq_enable(struct irq_data *d)
828{
829 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
830 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
831 const struct intel_community *community;
832 unsigned pin = irqd_to_hwirq(d);
Qi Zhenga939bb52016-03-17 02:15:26 +0800833
834 community = intel_get_community(pctrl, pin);
835 if (community) {
Mika Westerberg919eb472017-06-06 16:18:17 +0300836 const struct intel_padgroup *padgrp;
837 unsigned gpp, gpp_offset;
838 unsigned long flags;
Qi Zhenga939bb52016-03-17 02:15:26 +0800839 u32 value;
840
Mika Westerberg919eb472017-06-06 16:18:17 +0300841 padgrp = intel_community_get_padgroup(community, pin);
842 if (!padgrp)
843 return;
844
845 gpp = padgrp->reg_num;
846 gpp_offset = padgroup_offset(padgrp, pin);
847
848 raw_spin_lock_irqsave(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800849 /* Clear interrupt status first to avoid unexpected interrupt */
850 writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
851
852 value = readl(community->regs + community->ie_offset + gpp * 4);
853 value |= BIT(gpp_offset);
854 writel(value, community->regs + community->ie_offset + gpp * 4);
Mika Westerberg919eb472017-06-06 16:18:17 +0300855 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Qi Zhenga939bb52016-03-17 02:15:26 +0800856 }
Qi Zhenga939bb52016-03-17 02:15:26 +0800857}
858
Mika Westerberg7981c0012015-03-30 17:31:49 +0300859static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
860{
861 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100862 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300863 const struct intel_community *community;
864 unsigned pin = irqd_to_hwirq(d);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300865
866 community = intel_get_community(pctrl, pin);
867 if (community) {
Mika Westerberg919eb472017-06-06 16:18:17 +0300868 const struct intel_padgroup *padgrp;
869 unsigned gpp, gpp_offset;
870 unsigned long flags;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300871 void __iomem *reg;
872 u32 value;
873
Mika Westerberg919eb472017-06-06 16:18:17 +0300874 padgrp = intel_community_get_padgroup(community, pin);
875 if (!padgrp)
876 return;
877
878 gpp = padgrp->reg_num;
879 gpp_offset = padgroup_offset(padgrp, pin);
880
Mika Westerberg7981c0012015-03-30 17:31:49 +0300881 reg = community->regs + community->ie_offset + gpp * 4;
Mika Westerberg919eb472017-06-06 16:18:17 +0300882
883 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300884 value = readl(reg);
885 if (mask)
886 value &= ~BIT(gpp_offset);
887 else
888 value |= BIT(gpp_offset);
889 writel(value, reg);
Mika Westerberg919eb472017-06-06 16:18:17 +0300890 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300891 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300892}
893
894static void intel_gpio_irq_mask(struct irq_data *d)
895{
896 intel_gpio_irq_mask_unmask(d, true);
897}
898
899static void intel_gpio_irq_unmask(struct irq_data *d)
900{
901 intel_gpio_irq_mask_unmask(d, false);
902}
903
904static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
905{
906 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100907 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300908 unsigned pin = irqd_to_hwirq(d);
909 unsigned long flags;
910 void __iomem *reg;
911 u32 value;
912
913 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
914 if (!reg)
915 return -EINVAL;
916
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300917 /*
918 * If the pin is in ACPI mode it is still usable as a GPIO but it
919 * cannot be used as IRQ because GPI_IS status bit will not be
920 * updated by the host controller hardware.
921 */
922 if (intel_pad_acpi_mode(pctrl, pin)) {
923 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
924 return -EPERM;
925 }
926
Mika Westerberg27d90982016-06-16 11:25:36 +0300927 raw_spin_lock_irqsave(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300928
929 value = readl(reg);
930
931 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
932
933 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
934 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
935 } else if (type & IRQ_TYPE_EDGE_FALLING) {
936 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
937 value |= PADCFG0_RXINV;
938 } else if (type & IRQ_TYPE_EDGE_RISING) {
939 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
Qipeng Zhabf380cf2016-03-17 02:15:25 +0800940 } else if (type & IRQ_TYPE_LEVEL_MASK) {
941 if (type & IRQ_TYPE_LEVEL_LOW)
942 value |= PADCFG0_RXINV;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300943 } else {
944 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
945 }
946
947 writel(value, reg);
948
949 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200950 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300951 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200952 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300953
Mika Westerberg27d90982016-06-16 11:25:36 +0300954 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300955
956 return 0;
957}
958
959static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
960{
961 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100962 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300963 unsigned pin = irqd_to_hwirq(d);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300964
Mika Westerberg7981c0012015-03-30 17:31:49 +0300965 if (on)
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700966 enable_irq_wake(pctrl->irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300967 else
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -0700968 disable_irq_wake(pctrl->irq);
Andy Shevchenko9a520fd2016-07-08 14:30:46 +0300969
Mika Westerberg7981c0012015-03-30 17:31:49 +0300970 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
971 return 0;
972}
973
Mika Westerberg193b40c2015-10-21 13:08:43 +0300974static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300975 const struct intel_community *community)
976{
Mika Westerberg193b40c2015-10-21 13:08:43 +0300977 struct gpio_chip *gc = &pctrl->chip;
978 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300979 int gpp;
980
981 for (gpp = 0; gpp < community->ngpps; gpp++) {
Mika Westerberg919eb472017-06-06 16:18:17 +0300982 const struct intel_padgroup *padgrp = &community->gpps[gpp];
Mika Westerberg7981c0012015-03-30 17:31:49 +0300983 unsigned long pending, enabled, gpp_offset;
984
Mika Westerberg919eb472017-06-06 16:18:17 +0300985 pending = readl(community->regs + GPI_IS + padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300986 enabled = readl(community->regs + community->ie_offset +
Mika Westerberg919eb472017-06-06 16:18:17 +0300987 padgrp->reg_num * 4);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300988
989 /* Only interrupts that are enabled */
990 pending &= enabled;
991
Mika Westerberg919eb472017-06-06 16:18:17 +0300992 for_each_set_bit(gpp_offset, &pending, padgrp->size) {
Mika Westerberg7981c0012015-03-30 17:31:49 +0300993 unsigned padno, irq;
994
Mika Westerberg919eb472017-06-06 16:18:17 +0300995 padno = padgrp->base - community->pin_base + gpp_offset;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300996 if (padno >= community->npins)
997 break;
998
999 irq = irq_find_mapping(gc->irqdomain,
1000 community->pin_base + padno);
1001 generic_handle_irq(irq);
Mika Westerberg193b40c2015-10-21 13:08:43 +03001002
1003 ret |= IRQ_HANDLED;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001004 }
1005 }
Mika Westerberg193b40c2015-10-21 13:08:43 +03001006
1007 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001008}
1009
Mika Westerberg193b40c2015-10-21 13:08:43 +03001010static irqreturn_t intel_gpio_irq(int irq, void *data)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001011{
Mika Westerberg193b40c2015-10-21 13:08:43 +03001012 const struct intel_community *community;
1013 struct intel_pinctrl *pctrl = data;
1014 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001015 int i;
1016
Mika Westerberg7981c0012015-03-30 17:31:49 +03001017 /* Need to check all communities for pending interrupts */
Mika Westerberg193b40c2015-10-21 13:08:43 +03001018 for (i = 0; i < pctrl->ncommunities; i++) {
1019 community = &pctrl->communities[i];
1020 ret |= intel_gpio_community_irq_handler(pctrl, community);
1021 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001022
Mika Westerberg193b40c2015-10-21 13:08:43 +03001023 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001024}
1025
1026static struct irq_chip intel_gpio_irqchip = {
1027 .name = "intel-gpio",
Qi Zhenga939bb52016-03-17 02:15:26 +08001028 .irq_enable = intel_gpio_irq_enable,
Mika Westerberg7981c0012015-03-30 17:31:49 +03001029 .irq_ack = intel_gpio_irq_ack,
1030 .irq_mask = intel_gpio_irq_mask,
1031 .irq_unmask = intel_gpio_irq_unmask,
1032 .irq_set_type = intel_gpio_irq_type,
1033 .irq_set_wake = intel_gpio_irq_wake,
1034};
1035
Mika Westerberg7981c0012015-03-30 17:31:49 +03001036static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
1037{
1038 int ret;
1039
1040 pctrl->chip = intel_gpio_chip;
1041
1042 pctrl->chip.ngpio = pctrl->soc->npins;
1043 pctrl->chip.label = dev_name(pctrl->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001044 pctrl->chip.parent = pctrl->dev;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001045 pctrl->chip.base = -1;
Nilesh Bacchewar01dabe92016-09-21 16:35:23 -07001046 pctrl->irq = irq;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001047
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001048 ret = devm_gpiochip_add_data(pctrl->dev, &pctrl->chip, pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001049 if (ret) {
1050 dev_err(pctrl->dev, "failed to register gpiochip\n");
1051 return ret;
1052 }
1053
1054 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
1055 0, 0, pctrl->soc->npins);
1056 if (ret) {
1057 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001058 return ret;
Mika Westerberg193b40c2015-10-21 13:08:43 +03001059 }
1060
1061 /*
1062 * We need to request the interrupt here (instead of providing chip
1063 * to the irq directly) because on some platforms several GPIO
1064 * controllers share the same interrupt line.
1065 */
Mika Westerberg1a7d1cb2016-06-16 11:25:37 +03001066 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq,
1067 IRQF_SHARED | IRQF_NO_THREAD,
Mika Westerberg193b40c2015-10-21 13:08:43 +03001068 dev_name(pctrl->dev), pctrl);
1069 if (ret) {
1070 dev_err(pctrl->dev, "failed to request interrupt\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001071 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001072 }
1073
1074 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
Andy Shevchenko3ae02c12016-11-25 13:31:16 +02001075 handle_bad_irq, IRQ_TYPE_NONE);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001076 if (ret) {
1077 dev_err(pctrl->dev, "failed to add irqchip\n");
Mika Westerbergf25c3aa2017-01-10 17:31:57 +03001078 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001079 }
1080
1081 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
Mika Westerberg193b40c2015-10-21 13:08:43 +03001082 NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001083 return 0;
1084}
1085
Mika Westerberg919eb472017-06-06 16:18:17 +03001086static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
1087 struct intel_community *community)
1088{
1089 struct intel_padgroup *gpps;
1090 unsigned npins = community->npins;
1091 unsigned padown_num = 0;
1092 size_t ngpps, i;
1093
1094 if (community->gpps)
1095 ngpps = community->ngpps;
1096 else
1097 ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
1098
1099 gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
1100 if (!gpps)
1101 return -ENOMEM;
1102
1103 for (i = 0; i < ngpps; i++) {
1104 if (community->gpps) {
1105 gpps[i] = community->gpps[i];
1106 } else {
1107 unsigned gpp_size = community->gpp_size;
1108
1109 gpps[i].reg_num = i;
1110 gpps[i].base = community->pin_base + i * gpp_size;
1111 gpps[i].size = min(gpp_size, npins);
1112 npins -= gpps[i].size;
1113 }
1114
1115 if (gpps[i].size > 32)
1116 return -EINVAL;
1117
1118 gpps[i].padown_num = padown_num;
1119
1120 /*
1121 * In older hardware the number of padown registers per
1122 * group is fixed regardless of the group size.
1123 */
1124 if (community->gpp_num_padown_regs)
1125 padown_num += community->gpp_num_padown_regs;
1126 else
1127 padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
1128 }
1129
1130 community->ngpps = ngpps;
1131 community->gpps = gpps;
1132
1133 return 0;
1134}
1135
Mika Westerberg7981c0012015-03-30 17:31:49 +03001136static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
1137{
1138#ifdef CONFIG_PM_SLEEP
1139 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
1140 struct intel_community_context *communities;
1141 struct intel_pad_context *pads;
1142 int i;
1143
1144 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
1145 if (!pads)
1146 return -ENOMEM;
1147
1148 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
1149 sizeof(*communities), GFP_KERNEL);
1150 if (!communities)
1151 return -ENOMEM;
1152
1153
1154 for (i = 0; i < pctrl->ncommunities; i++) {
1155 struct intel_community *community = &pctrl->communities[i];
1156 u32 *intmask;
1157
1158 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
1159 sizeof(*intmask), GFP_KERNEL);
1160 if (!intmask)
1161 return -ENOMEM;
1162
1163 communities[i].intmask = intmask;
1164 }
1165
1166 pctrl->context.pads = pads;
1167 pctrl->context.communities = communities;
1168#endif
1169
1170 return 0;
1171}
1172
1173int intel_pinctrl_probe(struct platform_device *pdev,
1174 const struct intel_pinctrl_soc_data *soc_data)
1175{
1176 struct intel_pinctrl *pctrl;
1177 int i, ret, irq;
1178
1179 if (!soc_data)
1180 return -EINVAL;
1181
1182 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1183 if (!pctrl)
1184 return -ENOMEM;
1185
1186 pctrl->dev = &pdev->dev;
1187 pctrl->soc = soc_data;
Mika Westerberg27d90982016-06-16 11:25:36 +03001188 raw_spin_lock_init(&pctrl->lock);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001189
1190 /*
1191 * Make a copy of the communities which we can use to hold pointers
1192 * to the registers.
1193 */
1194 pctrl->ncommunities = pctrl->soc->ncommunities;
1195 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
1196 sizeof(*pctrl->communities), GFP_KERNEL);
1197 if (!pctrl->communities)
1198 return -ENOMEM;
1199
1200 for (i = 0; i < pctrl->ncommunities; i++) {
1201 struct intel_community *community = &pctrl->communities[i];
1202 struct resource *res;
1203 void __iomem *regs;
1204 u32 padbar;
1205
1206 *community = pctrl->soc->communities[i];
1207
1208 res = platform_get_resource(pdev, IORESOURCE_MEM,
1209 community->barno);
1210 regs = devm_ioremap_resource(&pdev->dev, res);
1211 if (IS_ERR(regs))
1212 return PTR_ERR(regs);
1213
Mika Westerberge57725e2017-01-27 13:07:14 +03001214 /*
1215 * Determine community features based on the revision if
1216 * not specified already.
1217 */
1218 if (!community->features) {
1219 u32 rev;
1220
1221 rev = (readl(regs + REVID) & REVID_MASK) >> REVID_SHIFT;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001222 if (rev >= 0x94) {
Mika Westerberge57725e2017-01-27 13:07:14 +03001223 community->features |= PINCTRL_FEATURE_DEBOUNCE;
Mika Westerberg04cc0582017-01-27 13:07:15 +03001224 community->features |= PINCTRL_FEATURE_1K_PD;
1225 }
Mika Westerberge57725e2017-01-27 13:07:14 +03001226 }
1227
Mika Westerberg7981c0012015-03-30 17:31:49 +03001228 /* Read offset of the pad configuration registers */
1229 padbar = readl(regs + PADBAR);
1230
1231 community->regs = regs;
1232 community->pad_regs = regs + padbar;
Mika Westerberg919eb472017-06-06 16:18:17 +03001233
1234 ret = intel_pinctrl_add_padgroups(pctrl, community);
1235 if (ret)
1236 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001237 }
1238
1239 irq = platform_get_irq(pdev, 0);
1240 if (irq < 0) {
1241 dev_err(&pdev->dev, "failed to get interrupt number\n");
1242 return irq;
1243 }
1244
1245 ret = intel_pinctrl_pm_init(pctrl);
1246 if (ret)
1247 return ret;
1248
1249 pctrl->pctldesc = intel_pinctrl_desc;
1250 pctrl->pctldesc.name = dev_name(&pdev->dev);
1251 pctrl->pctldesc.pins = pctrl->soc->pins;
1252 pctrl->pctldesc.npins = pctrl->soc->npins;
1253
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301254 pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1255 pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001256 if (IS_ERR(pctrl->pctldev)) {
Mika Westerberg7981c0012015-03-30 17:31:49 +03001257 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001258 return PTR_ERR(pctrl->pctldev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001259 }
1260
1261 ret = intel_gpio_probe(pctrl, irq);
Laxman Dewangan54d46cd2016-02-28 14:42:47 +05301262 if (ret)
Mika Westerberg7981c0012015-03-30 17:31:49 +03001263 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001264
1265 platform_set_drvdata(pdev, pctrl);
1266
1267 return 0;
1268}
1269EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1270
Mika Westerberg7981c0012015-03-30 17:31:49 +03001271#ifdef CONFIG_PM_SLEEP
Mika Westerbergc538b942016-10-10 16:39:31 +03001272static bool intel_pinctrl_should_save(struct intel_pinctrl *pctrl, unsigned pin)
1273{
1274 const struct pin_desc *pd = pin_desc_get(pctrl->pctldev, pin);
1275
1276 if (!pd || !intel_pad_usable(pctrl, pin))
1277 return false;
1278
1279 /*
1280 * Only restore the pin if it is actually in use by the kernel (or
1281 * by userspace). It is possible that some pins are used by the
1282 * BIOS during resume and those are not always locked down so leave
1283 * them alone.
1284 */
1285 if (pd->mux_owner || pd->gpio_owner ||
1286 gpiochip_line_is_irq(&pctrl->chip, pin))
1287 return true;
1288
1289 return false;
1290}
1291
Mika Westerberg7981c0012015-03-30 17:31:49 +03001292int intel_pinctrl_suspend(struct device *dev)
1293{
1294 struct platform_device *pdev = to_platform_device(dev);
1295 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1296 struct intel_community_context *communities;
1297 struct intel_pad_context *pads;
1298 int i;
1299
1300 pads = pctrl->context.pads;
1301 for (i = 0; i < pctrl->soc->npins; i++) {
1302 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
Mika Westerberge57725e2017-01-27 13:07:14 +03001303 void __iomem *padcfg;
Mika Westerberg7981c0012015-03-30 17:31:49 +03001304 u32 val;
1305
Mika Westerbergc538b942016-10-10 16:39:31 +03001306 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001307 continue;
1308
1309 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1310 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1311 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1312 pads[i].padcfg1 = val;
Mika Westerberge57725e2017-01-27 13:07:14 +03001313
1314 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1315 if (padcfg)
1316 pads[i].padcfg2 = readl(padcfg);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001317 }
1318
1319 communities = pctrl->context.communities;
1320 for (i = 0; i < pctrl->ncommunities; i++) {
1321 struct intel_community *community = &pctrl->communities[i];
1322 void __iomem *base;
1323 unsigned gpp;
1324
1325 base = community->regs + community->ie_offset;
1326 for (gpp = 0; gpp < community->ngpps; gpp++)
1327 communities[i].intmask[gpp] = readl(base + gpp * 4);
1328 }
1329
1330 return 0;
1331}
1332EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1333
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001334static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1335{
1336 size_t i;
1337
1338 for (i = 0; i < pctrl->ncommunities; i++) {
1339 const struct intel_community *community;
1340 void __iomem *base;
1341 unsigned gpp;
1342
1343 community = &pctrl->communities[i];
1344 base = community->regs;
1345
1346 for (gpp = 0; gpp < community->ngpps; gpp++) {
1347 /* Mask and clear all interrupts */
1348 writel(0, base + community->ie_offset + gpp * 4);
1349 writel(0xffff, base + GPI_IS + gpp * 4);
1350 }
1351 }
1352}
1353
Mika Westerberg7981c0012015-03-30 17:31:49 +03001354int intel_pinctrl_resume(struct device *dev)
1355{
1356 struct platform_device *pdev = to_platform_device(dev);
1357 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1358 const struct intel_community_context *communities;
1359 const struct intel_pad_context *pads;
1360 int i;
1361
1362 /* Mask all interrupts */
1363 intel_gpio_irq_init(pctrl);
1364
1365 pads = pctrl->context.pads;
1366 for (i = 0; i < pctrl->soc->npins; i++) {
1367 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1368 void __iomem *padcfg;
1369 u32 val;
1370
Mika Westerbergc538b942016-10-10 16:39:31 +03001371 if (!intel_pinctrl_should_save(pctrl, desc->number))
Mika Westerberg7981c0012015-03-30 17:31:49 +03001372 continue;
1373
1374 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1375 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1376 if (val != pads[i].padcfg0) {
1377 writel(pads[i].padcfg0, padcfg);
1378 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1379 desc->number, readl(padcfg));
1380 }
1381
1382 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1383 val = readl(padcfg);
1384 if (val != pads[i].padcfg1) {
1385 writel(pads[i].padcfg1, padcfg);
1386 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1387 desc->number, readl(padcfg));
1388 }
Mika Westerberge57725e2017-01-27 13:07:14 +03001389
1390 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG2);
1391 if (padcfg) {
1392 val = readl(padcfg);
1393 if (val != pads[i].padcfg2) {
1394 writel(pads[i].padcfg2, padcfg);
1395 dev_dbg(dev, "restored pin %u padcfg2 %#08x\n",
1396 desc->number, readl(padcfg));
1397 }
1398 }
Mika Westerberg7981c0012015-03-30 17:31:49 +03001399 }
1400
1401 communities = pctrl->context.communities;
1402 for (i = 0; i < pctrl->ncommunities; i++) {
1403 struct intel_community *community = &pctrl->communities[i];
1404 void __iomem *base;
1405 unsigned gpp;
1406
1407 base = community->regs + community->ie_offset;
1408 for (gpp = 0; gpp < community->ngpps; gpp++) {
1409 writel(communities[i].intmask[gpp], base + gpp * 4);
1410 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1411 readl(base + gpp * 4));
1412 }
1413 }
1414
1415 return 0;
1416}
1417EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1418#endif
1419
1420MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1421MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1422MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1423MODULE_LICENSE("GPL v2");