blob: c216cb3bef2f86729163fae772e981dd1deb60da [file] [log] [blame]
Mika Westerberg7981c0012015-03-30 17:31:49 +03001/*
2 * Intel pinctrl/GPIO core driver.
3 *
4 * Copyright (C) 2015, Intel Corporation
5 * Authors: Mathias Nyman <mathias.nyman@linux.intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
Mika Westerberg193b40c2015-10-21 13:08:43 +030014#include <linux/interrupt.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030015#include <linux/gpio/driver.h>
16#include <linux/platform_device.h>
Mika Westerberg7981c0012015-03-30 17:31:49 +030017#include <linux/pinctrl/pinctrl.h>
18#include <linux/pinctrl/pinmux.h>
19#include <linux/pinctrl/pinconf.h>
20#include <linux/pinctrl/pinconf-generic.h>
21
22#include "pinctrl-intel.h"
23
Mika Westerberg7981c0012015-03-30 17:31:49 +030024/* Offset from regs */
25#define PADBAR 0x00c
26#define GPI_IS 0x100
27#define GPI_GPE_STS 0x140
28#define GPI_GPE_EN 0x160
29
30#define PADOWN_BITS 4
31#define PADOWN_SHIFT(p) ((p) % 8 * PADOWN_BITS)
32#define PADOWN_MASK(p) (0xf << PADOWN_SHIFT(p))
Qipeng Zha99a735b2015-11-30 19:20:16 +080033#define PADOWN_GPP(p) ((p) / 8)
Mika Westerberg7981c0012015-03-30 17:31:49 +030034
35/* Offset from pad_regs */
36#define PADCFG0 0x000
37#define PADCFG0_RXEVCFG_SHIFT 25
38#define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
39#define PADCFG0_RXEVCFG_LEVEL 0
40#define PADCFG0_RXEVCFG_EDGE 1
41#define PADCFG0_RXEVCFG_DISABLED 2
42#define PADCFG0_RXEVCFG_EDGE_BOTH 3
43#define PADCFG0_RXINV BIT(23)
44#define PADCFG0_GPIROUTIOXAPIC BIT(20)
45#define PADCFG0_GPIROUTSCI BIT(19)
46#define PADCFG0_GPIROUTSMI BIT(18)
47#define PADCFG0_GPIROUTNMI BIT(17)
48#define PADCFG0_PMODE_SHIFT 10
49#define PADCFG0_PMODE_MASK (0xf << PADCFG0_PMODE_SHIFT)
50#define PADCFG0_GPIORXDIS BIT(9)
51#define PADCFG0_GPIOTXDIS BIT(8)
52#define PADCFG0_GPIORXSTATE BIT(1)
53#define PADCFG0_GPIOTXSTATE BIT(0)
54
55#define PADCFG1 0x004
56#define PADCFG1_TERM_UP BIT(13)
57#define PADCFG1_TERM_SHIFT 10
58#define PADCFG1_TERM_MASK (7 << PADCFG1_TERM_SHIFT)
59#define PADCFG1_TERM_20K 4
60#define PADCFG1_TERM_2K 3
61#define PADCFG1_TERM_5K 2
62#define PADCFG1_TERM_1K 1
63
64struct intel_pad_context {
65 u32 padcfg0;
66 u32 padcfg1;
67};
68
69struct intel_community_context {
70 u32 *intmask;
71};
72
73struct intel_pinctrl_context {
74 struct intel_pad_context *pads;
75 struct intel_community_context *communities;
76};
77
78/**
79 * struct intel_pinctrl - Intel pinctrl private structure
80 * @dev: Pointer to the device structure
81 * @lock: Lock to serialize register access
82 * @pctldesc: Pin controller description
83 * @pctldev: Pointer to the pin controller device
84 * @chip: GPIO chip in this pin controller
85 * @soc: SoC/PCH specific pin configuration data
86 * @communities: All communities in this pin controller
87 * @ncommunities: Number of communities in this pin controller
88 * @context: Configuration saved over system sleep
89 */
90struct intel_pinctrl {
91 struct device *dev;
92 spinlock_t lock;
93 struct pinctrl_desc pctldesc;
94 struct pinctrl_dev *pctldev;
95 struct gpio_chip chip;
96 const struct intel_pinctrl_soc_data *soc;
97 struct intel_community *communities;
98 size_t ncommunities;
99 struct intel_pinctrl_context context;
100};
101
Mika Westerberg7981c0012015-03-30 17:31:49 +0300102#define pin_to_padno(c, p) ((p) - (c)->pin_base)
103
104static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
105 unsigned pin)
106{
107 struct intel_community *community;
108 int i;
109
110 for (i = 0; i < pctrl->ncommunities; i++) {
111 community = &pctrl->communities[i];
112 if (pin >= community->pin_base &&
113 pin < community->pin_base + community->npins)
114 return community;
115 }
116
117 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
118 return NULL;
119}
120
121static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
122 unsigned reg)
123{
124 const struct intel_community *community;
125 unsigned padno;
126
127 community = intel_get_community(pctrl, pin);
128 if (!community)
129 return NULL;
130
131 padno = pin_to_padno(community, pin);
132 return community->pad_regs + reg + padno * 8;
133}
134
135static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
136{
137 const struct intel_community *community;
Qipeng Zha99a735b2015-11-30 19:20:16 +0800138 unsigned padno, gpp, offset, group;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300139 void __iomem *padown;
140
141 community = intel_get_community(pctrl, pin);
142 if (!community)
143 return false;
144 if (!community->padown_offset)
145 return true;
146
147 padno = pin_to_padno(community, pin);
Qipeng Zha99a735b2015-11-30 19:20:16 +0800148 group = padno / community->gpp_size;
149 gpp = PADOWN_GPP(padno % community->gpp_size);
150 offset = community->padown_offset + 0x10 * group + gpp * 4;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300151 padown = community->regs + offset;
152
153 return !(readl(padown) & PADOWN_MASK(padno));
154}
155
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300156static bool intel_pad_acpi_mode(struct intel_pinctrl *pctrl, unsigned pin)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300157{
158 const struct intel_community *community;
159 unsigned padno, gpp, offset;
160 void __iomem *hostown;
161
162 community = intel_get_community(pctrl, pin);
163 if (!community)
164 return true;
165 if (!community->hostown_offset)
166 return false;
167
168 padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800169 gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300170 offset = community->hostown_offset + gpp * 4;
171 hostown = community->regs + offset;
172
Qipeng Zha618a919b2015-11-26 01:09:51 +0800173 return !(readl(hostown) & BIT(padno % community->gpp_size));
Mika Westerberg7981c0012015-03-30 17:31:49 +0300174}
175
176static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
177{
178 struct intel_community *community;
179 unsigned padno, gpp, offset;
180 u32 value;
181
182 community = intel_get_community(pctrl, pin);
183 if (!community)
184 return true;
185 if (!community->padcfglock_offset)
186 return false;
187
188 padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800189 gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300190
191 /*
192 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
193 * the pad is considered unlocked. Any other case means that it is
194 * either fully or partially locked and we don't touch it.
195 */
196 offset = community->padcfglock_offset + gpp * 8;
197 value = readl(community->regs + offset);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800198 if (value & BIT(pin % community->gpp_size))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300199 return true;
200
201 offset = community->padcfglock_offset + 4 + gpp * 8;
202 value = readl(community->regs + offset);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800203 if (value & BIT(pin % community->gpp_size))
Mika Westerberg7981c0012015-03-30 17:31:49 +0300204 return true;
205
206 return false;
207}
208
209static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
210{
211 return intel_pad_owned_by_host(pctrl, pin) &&
Mika Westerberg7981c0012015-03-30 17:31:49 +0300212 !intel_pad_locked(pctrl, pin);
213}
214
215static int intel_get_groups_count(struct pinctrl_dev *pctldev)
216{
217 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
218
219 return pctrl->soc->ngroups;
220}
221
222static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
223 unsigned group)
224{
225 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
226
227 return pctrl->soc->groups[group].name;
228}
229
230static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
231 const unsigned **pins, unsigned *npins)
232{
233 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
234
235 *pins = pctrl->soc->groups[group].pins;
236 *npins = pctrl->soc->groups[group].npins;
237 return 0;
238}
239
240static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
241 unsigned pin)
242{
243 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
244 u32 cfg0, cfg1, mode;
245 bool locked, acpi;
246
247 if (!intel_pad_owned_by_host(pctrl, pin)) {
248 seq_puts(s, "not available");
249 return;
250 }
251
252 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
253 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
254
255 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
256 if (!mode)
257 seq_puts(s, "GPIO ");
258 else
259 seq_printf(s, "mode %d ", mode);
260
261 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
262
263 locked = intel_pad_locked(pctrl, pin);
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300264 acpi = intel_pad_acpi_mode(pctrl, pin);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300265
266 if (locked || acpi) {
267 seq_puts(s, " [");
268 if (locked) {
269 seq_puts(s, "LOCKED");
270 if (acpi)
271 seq_puts(s, ", ");
272 }
273 if (acpi)
274 seq_puts(s, "ACPI");
275 seq_puts(s, "]");
276 }
277}
278
279static const struct pinctrl_ops intel_pinctrl_ops = {
280 .get_groups_count = intel_get_groups_count,
281 .get_group_name = intel_get_group_name,
282 .get_group_pins = intel_get_group_pins,
283 .pin_dbg_show = intel_pin_dbg_show,
284};
285
286static int intel_get_functions_count(struct pinctrl_dev *pctldev)
287{
288 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
289
290 return pctrl->soc->nfunctions;
291}
292
293static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
294 unsigned function)
295{
296 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
297
298 return pctrl->soc->functions[function].name;
299}
300
301static int intel_get_function_groups(struct pinctrl_dev *pctldev,
302 unsigned function,
303 const char * const **groups,
304 unsigned * const ngroups)
305{
306 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
307
308 *groups = pctrl->soc->functions[function].groups;
309 *ngroups = pctrl->soc->functions[function].ngroups;
310 return 0;
311}
312
313static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
314 unsigned group)
315{
316 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
317 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
318 unsigned long flags;
319 int i;
320
321 spin_lock_irqsave(&pctrl->lock, flags);
322
323 /*
324 * All pins in the groups needs to be accessible and writable
325 * before we can enable the mux for this group.
326 */
327 for (i = 0; i < grp->npins; i++) {
328 if (!intel_pad_usable(pctrl, grp->pins[i])) {
329 spin_unlock_irqrestore(&pctrl->lock, flags);
330 return -EBUSY;
331 }
332 }
333
334 /* Now enable the mux setting for each pin in the group */
335 for (i = 0; i < grp->npins; i++) {
336 void __iomem *padcfg0;
337 u32 value;
338
339 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
340 value = readl(padcfg0);
341
342 value &= ~PADCFG0_PMODE_MASK;
343 value |= grp->mode << PADCFG0_PMODE_SHIFT;
344
345 writel(value, padcfg0);
346 }
347
348 spin_unlock_irqrestore(&pctrl->lock, flags);
349
350 return 0;
351}
352
353static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
354 struct pinctrl_gpio_range *range,
355 unsigned pin)
356{
357 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
358 void __iomem *padcfg0;
359 unsigned long flags;
360 u32 value;
361
362 spin_lock_irqsave(&pctrl->lock, flags);
363
364 if (!intel_pad_usable(pctrl, pin)) {
365 spin_unlock_irqrestore(&pctrl->lock, flags);
366 return -EBUSY;
367 }
368
369 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
370 /* Put the pad into GPIO mode */
371 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
372 /* Disable SCI/SMI/NMI generation */
373 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
374 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
375 /* Disable TX buffer and enable RX (this will be input) */
376 value &= ~PADCFG0_GPIORXDIS;
377 value |= PADCFG0_GPIOTXDIS;
378 writel(value, padcfg0);
379
380 spin_unlock_irqrestore(&pctrl->lock, flags);
381
382 return 0;
383}
384
385static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
386 struct pinctrl_gpio_range *range,
387 unsigned pin, bool input)
388{
389 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
390 void __iomem *padcfg0;
391 unsigned long flags;
392 u32 value;
393
394 spin_lock_irqsave(&pctrl->lock, flags);
395
396 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
397
398 value = readl(padcfg0);
399 if (input)
400 value |= PADCFG0_GPIOTXDIS;
401 else
402 value &= ~PADCFG0_GPIOTXDIS;
403 writel(value, padcfg0);
404
405 spin_unlock_irqrestore(&pctrl->lock, flags);
406
407 return 0;
408}
409
410static const struct pinmux_ops intel_pinmux_ops = {
411 .get_functions_count = intel_get_functions_count,
412 .get_function_name = intel_get_function_name,
413 .get_function_groups = intel_get_function_groups,
414 .set_mux = intel_pinmux_set_mux,
415 .gpio_request_enable = intel_gpio_request_enable,
416 .gpio_set_direction = intel_gpio_set_direction,
417};
418
419static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
420 unsigned long *config)
421{
422 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
423 enum pin_config_param param = pinconf_to_config_param(*config);
424 u32 value, term;
425 u16 arg = 0;
426
427 if (!intel_pad_owned_by_host(pctrl, pin))
428 return -ENOTSUPP;
429
430 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
431 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
432
433 switch (param) {
434 case PIN_CONFIG_BIAS_DISABLE:
435 if (term)
436 return -EINVAL;
437 break;
438
439 case PIN_CONFIG_BIAS_PULL_UP:
440 if (!term || !(value & PADCFG1_TERM_UP))
441 return -EINVAL;
442
443 switch (term) {
444 case PADCFG1_TERM_1K:
445 arg = 1000;
446 break;
447 case PADCFG1_TERM_2K:
448 arg = 2000;
449 break;
450 case PADCFG1_TERM_5K:
451 arg = 5000;
452 break;
453 case PADCFG1_TERM_20K:
454 arg = 20000;
455 break;
456 }
457
458 break;
459
460 case PIN_CONFIG_BIAS_PULL_DOWN:
461 if (!term || value & PADCFG1_TERM_UP)
462 return -EINVAL;
463
464 switch (term) {
465 case PADCFG1_TERM_5K:
466 arg = 5000;
467 break;
468 case PADCFG1_TERM_20K:
469 arg = 20000;
470 break;
471 }
472
473 break;
474
475 default:
476 return -ENOTSUPP;
477 }
478
479 *config = pinconf_to_config_packed(param, arg);
480 return 0;
481}
482
483static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
484 unsigned long config)
485{
486 unsigned param = pinconf_to_config_param(config);
487 unsigned arg = pinconf_to_config_argument(config);
488 void __iomem *padcfg1;
489 unsigned long flags;
490 int ret = 0;
491 u32 value;
492
493 spin_lock_irqsave(&pctrl->lock, flags);
494
495 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
496 value = readl(padcfg1);
497
498 switch (param) {
499 case PIN_CONFIG_BIAS_DISABLE:
500 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
501 break;
502
503 case PIN_CONFIG_BIAS_PULL_UP:
504 value &= ~PADCFG1_TERM_MASK;
505
506 value |= PADCFG1_TERM_UP;
507
508 switch (arg) {
509 case 20000:
510 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
511 break;
512 case 5000:
513 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
514 break;
515 case 2000:
516 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
517 break;
518 case 1000:
519 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
520 break;
521 default:
522 ret = -EINVAL;
523 }
524
525 break;
526
527 case PIN_CONFIG_BIAS_PULL_DOWN:
528 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
529
530 switch (arg) {
531 case 20000:
532 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
533 break;
534 case 5000:
535 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
536 break;
537 default:
538 ret = -EINVAL;
539 }
540
541 break;
542 }
543
544 if (!ret)
545 writel(value, padcfg1);
546
547 spin_unlock_irqrestore(&pctrl->lock, flags);
548
549 return ret;
550}
551
552static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
553 unsigned long *configs, unsigned nconfigs)
554{
555 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
556 int i, ret;
557
558 if (!intel_pad_usable(pctrl, pin))
559 return -ENOTSUPP;
560
561 for (i = 0; i < nconfigs; i++) {
562 switch (pinconf_to_config_param(configs[i])) {
563 case PIN_CONFIG_BIAS_DISABLE:
564 case PIN_CONFIG_BIAS_PULL_UP:
565 case PIN_CONFIG_BIAS_PULL_DOWN:
566 ret = intel_config_set_pull(pctrl, pin, configs[i]);
567 if (ret)
568 return ret;
569 break;
570
571 default:
572 return -ENOTSUPP;
573 }
574 }
575
576 return 0;
577}
578
579static const struct pinconf_ops intel_pinconf_ops = {
580 .is_generic = true,
581 .pin_config_get = intel_config_get,
582 .pin_config_set = intel_config_set,
583};
584
585static const struct pinctrl_desc intel_pinctrl_desc = {
586 .pctlops = &intel_pinctrl_ops,
587 .pmxops = &intel_pinmux_ops,
588 .confops = &intel_pinconf_ops,
589 .owner = THIS_MODULE,
590};
591
Mika Westerberg7981c0012015-03-30 17:31:49 +0300592static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
593{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100594 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300595 void __iomem *reg;
596
597 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
598 if (!reg)
599 return -EINVAL;
600
601 return !!(readl(reg) & PADCFG0_GPIORXSTATE);
602}
603
604static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
605{
Linus Walleijacfd4c62015-12-08 00:18:59 +0100606 struct intel_pinctrl *pctrl = gpiochip_get_data(chip);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300607 void __iomem *reg;
608
609 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
610 if (reg) {
611 unsigned long flags;
612 u32 padcfg0;
613
614 spin_lock_irqsave(&pctrl->lock, flags);
615 padcfg0 = readl(reg);
616 if (value)
617 padcfg0 |= PADCFG0_GPIOTXSTATE;
618 else
619 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
620 writel(padcfg0, reg);
621 spin_unlock_irqrestore(&pctrl->lock, flags);
622 }
623}
624
625static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
626{
627 return pinctrl_gpio_direction_input(chip->base + offset);
628}
629
630static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
631 int value)
632{
633 intel_gpio_set(chip, offset, value);
634 return pinctrl_gpio_direction_output(chip->base + offset);
635}
636
637static const struct gpio_chip intel_gpio_chip = {
638 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200639 .request = gpiochip_generic_request,
640 .free = gpiochip_generic_free,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300641 .direction_input = intel_gpio_direction_input,
642 .direction_output = intel_gpio_direction_output,
643 .get = intel_gpio_get,
644 .set = intel_gpio_set,
645};
646
647static void intel_gpio_irq_ack(struct irq_data *d)
648{
649 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100650 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300651 const struct intel_community *community;
652 unsigned pin = irqd_to_hwirq(d);
653
654 spin_lock(&pctrl->lock);
655
656 community = intel_get_community(pctrl, pin);
657 if (community) {
658 unsigned padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800659 unsigned gpp_offset = padno % community->gpp_size;
660 unsigned gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300661
662 writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
663 }
664
665 spin_unlock(&pctrl->lock);
666}
667
668static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
669{
670 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100671 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300672 const struct intel_community *community;
673 unsigned pin = irqd_to_hwirq(d);
674 unsigned long flags;
675
676 spin_lock_irqsave(&pctrl->lock, flags);
677
678 community = intel_get_community(pctrl, pin);
679 if (community) {
680 unsigned padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800681 unsigned gpp_offset = padno % community->gpp_size;
682 unsigned gpp = padno / community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300683 void __iomem *reg;
684 u32 value;
685
686 reg = community->regs + community->ie_offset + gpp * 4;
687 value = readl(reg);
688 if (mask)
689 value &= ~BIT(gpp_offset);
690 else
691 value |= BIT(gpp_offset);
692 writel(value, reg);
693 }
694
695 spin_unlock_irqrestore(&pctrl->lock, flags);
696}
697
698static void intel_gpio_irq_mask(struct irq_data *d)
699{
700 intel_gpio_irq_mask_unmask(d, true);
701}
702
703static void intel_gpio_irq_unmask(struct irq_data *d)
704{
705 intel_gpio_irq_mask_unmask(d, false);
706}
707
708static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
709{
710 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100711 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300712 unsigned pin = irqd_to_hwirq(d);
713 unsigned long flags;
714 void __iomem *reg;
715 u32 value;
716
717 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
718 if (!reg)
719 return -EINVAL;
720
Mika Westerberg4341e8a2015-10-21 13:08:44 +0300721 /*
722 * If the pin is in ACPI mode it is still usable as a GPIO but it
723 * cannot be used as IRQ because GPI_IS status bit will not be
724 * updated by the host controller hardware.
725 */
726 if (intel_pad_acpi_mode(pctrl, pin)) {
727 dev_warn(pctrl->dev, "pin %u cannot be used as IRQ\n", pin);
728 return -EPERM;
729 }
730
Mika Westerberg7981c0012015-03-30 17:31:49 +0300731 spin_lock_irqsave(&pctrl->lock, flags);
732
733 value = readl(reg);
734
735 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
736
737 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
738 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
739 } else if (type & IRQ_TYPE_EDGE_FALLING) {
740 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
741 value |= PADCFG0_RXINV;
742 } else if (type & IRQ_TYPE_EDGE_RISING) {
743 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
Qipeng Zhabf380cf2016-03-17 02:15:25 +0800744 } else if (type & IRQ_TYPE_LEVEL_MASK) {
745 if (type & IRQ_TYPE_LEVEL_LOW)
746 value |= PADCFG0_RXINV;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300747 } else {
748 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
749 }
750
751 writel(value, reg);
752
753 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200754 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300755 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200756 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300757
758 spin_unlock_irqrestore(&pctrl->lock, flags);
759
760 return 0;
761}
762
763static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
764{
765 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijacfd4c62015-12-08 00:18:59 +0100766 struct intel_pinctrl *pctrl = gpiochip_get_data(gc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300767 const struct intel_community *community;
768 unsigned pin = irqd_to_hwirq(d);
769 unsigned padno, gpp, gpp_offset;
770 u32 gpe_en;
771
772 community = intel_get_community(pctrl, pin);
773 if (!community)
774 return -EINVAL;
775
776 padno = pin_to_padno(community, pin);
Qipeng Zha618a919b2015-11-26 01:09:51 +0800777 gpp = padno / community->gpp_size;
778 gpp_offset = padno % community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300779
780 /* Clear the existing wake status */
781 writel(BIT(gpp_offset), community->regs + GPI_GPE_STS + gpp * 4);
782
783 /*
784 * The controller will generate wake when GPE of the corresponding
785 * pad is enabled and it is not routed to SCI (GPIROUTSCI is not
786 * set).
787 */
788 gpe_en = readl(community->regs + GPI_GPE_EN + gpp * 4);
789 if (on)
790 gpe_en |= BIT(gpp_offset);
791 else
792 gpe_en &= ~BIT(gpp_offset);
793 writel(gpe_en, community->regs + GPI_GPE_EN + gpp * 4);
794
795 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
796 return 0;
797}
798
Mika Westerberg193b40c2015-10-21 13:08:43 +0300799static irqreturn_t intel_gpio_community_irq_handler(struct intel_pinctrl *pctrl,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300800 const struct intel_community *community)
801{
Mika Westerberg193b40c2015-10-21 13:08:43 +0300802 struct gpio_chip *gc = &pctrl->chip;
803 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300804 int gpp;
805
806 for (gpp = 0; gpp < community->ngpps; gpp++) {
807 unsigned long pending, enabled, gpp_offset;
808
809 pending = readl(community->regs + GPI_IS + gpp * 4);
810 enabled = readl(community->regs + community->ie_offset +
811 gpp * 4);
812
813 /* Only interrupts that are enabled */
814 pending &= enabled;
815
Qipeng Zha618a919b2015-11-26 01:09:51 +0800816 for_each_set_bit(gpp_offset, &pending, community->gpp_size) {
Mika Westerberg7981c0012015-03-30 17:31:49 +0300817 unsigned padno, irq;
818
819 /*
820 * The last group in community can have less pins
821 * than NPADS_IN_GPP.
822 */
Qipeng Zha618a919b2015-11-26 01:09:51 +0800823 padno = gpp_offset + gpp * community->gpp_size;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300824 if (padno >= community->npins)
825 break;
826
827 irq = irq_find_mapping(gc->irqdomain,
828 community->pin_base + padno);
829 generic_handle_irq(irq);
Mika Westerberg193b40c2015-10-21 13:08:43 +0300830
831 ret |= IRQ_HANDLED;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300832 }
833 }
Mika Westerberg193b40c2015-10-21 13:08:43 +0300834
835 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300836}
837
Mika Westerberg193b40c2015-10-21 13:08:43 +0300838static irqreturn_t intel_gpio_irq(int irq, void *data)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300839{
Mika Westerberg193b40c2015-10-21 13:08:43 +0300840 const struct intel_community *community;
841 struct intel_pinctrl *pctrl = data;
842 irqreturn_t ret = IRQ_NONE;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300843 int i;
844
Mika Westerberg7981c0012015-03-30 17:31:49 +0300845 /* Need to check all communities for pending interrupts */
Mika Westerberg193b40c2015-10-21 13:08:43 +0300846 for (i = 0; i < pctrl->ncommunities; i++) {
847 community = &pctrl->communities[i];
848 ret |= intel_gpio_community_irq_handler(pctrl, community);
849 }
Mika Westerberg7981c0012015-03-30 17:31:49 +0300850
Mika Westerberg193b40c2015-10-21 13:08:43 +0300851 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300852}
853
854static struct irq_chip intel_gpio_irqchip = {
855 .name = "intel-gpio",
856 .irq_ack = intel_gpio_irq_ack,
857 .irq_mask = intel_gpio_irq_mask,
858 .irq_unmask = intel_gpio_irq_unmask,
859 .irq_set_type = intel_gpio_irq_type,
860 .irq_set_wake = intel_gpio_irq_wake,
861};
862
Mika Westerberg7981c0012015-03-30 17:31:49 +0300863static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
864{
865 int ret;
866
867 pctrl->chip = intel_gpio_chip;
868
869 pctrl->chip.ngpio = pctrl->soc->npins;
870 pctrl->chip.label = dev_name(pctrl->dev);
Linus Walleij58383c72015-11-04 09:56:26 +0100871 pctrl->chip.parent = pctrl->dev;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300872 pctrl->chip.base = -1;
873
Linus Walleijacfd4c62015-12-08 00:18:59 +0100874 ret = gpiochip_add_data(&pctrl->chip, pctrl);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300875 if (ret) {
876 dev_err(pctrl->dev, "failed to register gpiochip\n");
877 return ret;
878 }
879
880 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
881 0, 0, pctrl->soc->npins);
882 if (ret) {
883 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
Mika Westerberg193b40c2015-10-21 13:08:43 +0300884 goto fail;
885 }
886
887 /*
888 * We need to request the interrupt here (instead of providing chip
889 * to the irq directly) because on some platforms several GPIO
890 * controllers share the same interrupt line.
891 */
892 ret = devm_request_irq(pctrl->dev, irq, intel_gpio_irq, IRQF_SHARED,
893 dev_name(pctrl->dev), pctrl);
894 if (ret) {
895 dev_err(pctrl->dev, "failed to request interrupt\n");
896 goto fail;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300897 }
898
899 ret = gpiochip_irqchip_add(&pctrl->chip, &intel_gpio_irqchip, 0,
900 handle_simple_irq, IRQ_TYPE_NONE);
901 if (ret) {
902 dev_err(pctrl->dev, "failed to add irqchip\n");
Mika Westerberg193b40c2015-10-21 13:08:43 +0300903 goto fail;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300904 }
905
906 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
Mika Westerberg193b40c2015-10-21 13:08:43 +0300907 NULL);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300908 return 0;
Mika Westerberg193b40c2015-10-21 13:08:43 +0300909
910fail:
911 gpiochip_remove(&pctrl->chip);
912
913 return ret;
Mika Westerberg7981c0012015-03-30 17:31:49 +0300914}
915
916static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
917{
918#ifdef CONFIG_PM_SLEEP
919 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
920 struct intel_community_context *communities;
921 struct intel_pad_context *pads;
922 int i;
923
924 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
925 if (!pads)
926 return -ENOMEM;
927
928 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
929 sizeof(*communities), GFP_KERNEL);
930 if (!communities)
931 return -ENOMEM;
932
933
934 for (i = 0; i < pctrl->ncommunities; i++) {
935 struct intel_community *community = &pctrl->communities[i];
936 u32 *intmask;
937
938 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
939 sizeof(*intmask), GFP_KERNEL);
940 if (!intmask)
941 return -ENOMEM;
942
943 communities[i].intmask = intmask;
944 }
945
946 pctrl->context.pads = pads;
947 pctrl->context.communities = communities;
948#endif
949
950 return 0;
951}
952
953int intel_pinctrl_probe(struct platform_device *pdev,
954 const struct intel_pinctrl_soc_data *soc_data)
955{
956 struct intel_pinctrl *pctrl;
957 int i, ret, irq;
958
959 if (!soc_data)
960 return -EINVAL;
961
962 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
963 if (!pctrl)
964 return -ENOMEM;
965
966 pctrl->dev = &pdev->dev;
967 pctrl->soc = soc_data;
968 spin_lock_init(&pctrl->lock);
969
970 /*
971 * Make a copy of the communities which we can use to hold pointers
972 * to the registers.
973 */
974 pctrl->ncommunities = pctrl->soc->ncommunities;
975 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
976 sizeof(*pctrl->communities), GFP_KERNEL);
977 if (!pctrl->communities)
978 return -ENOMEM;
979
980 for (i = 0; i < pctrl->ncommunities; i++) {
981 struct intel_community *community = &pctrl->communities[i];
982 struct resource *res;
983 void __iomem *regs;
984 u32 padbar;
985
986 *community = pctrl->soc->communities[i];
987
988 res = platform_get_resource(pdev, IORESOURCE_MEM,
989 community->barno);
990 regs = devm_ioremap_resource(&pdev->dev, res);
991 if (IS_ERR(regs))
992 return PTR_ERR(regs);
993
994 /* Read offset of the pad configuration registers */
995 padbar = readl(regs + PADBAR);
996
997 community->regs = regs;
998 community->pad_regs = regs + padbar;
Qipeng Zha618a919b2015-11-26 01:09:51 +0800999 community->ngpps = DIV_ROUND_UP(community->npins,
1000 community->gpp_size);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001001 }
1002
1003 irq = platform_get_irq(pdev, 0);
1004 if (irq < 0) {
1005 dev_err(&pdev->dev, "failed to get interrupt number\n");
1006 return irq;
1007 }
1008
1009 ret = intel_pinctrl_pm_init(pctrl);
1010 if (ret)
1011 return ret;
1012
1013 pctrl->pctldesc = intel_pinctrl_desc;
1014 pctrl->pctldesc.name = dev_name(&pdev->dev);
1015 pctrl->pctldesc.pins = pctrl->soc->pins;
1016 pctrl->pctldesc.npins = pctrl->soc->npins;
1017
1018 pctrl->pctldev = pinctrl_register(&pctrl->pctldesc, &pdev->dev, pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001019 if (IS_ERR(pctrl->pctldev)) {
Mika Westerberg7981c0012015-03-30 17:31:49 +03001020 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001021 return PTR_ERR(pctrl->pctldev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001022 }
1023
1024 ret = intel_gpio_probe(pctrl, irq);
1025 if (ret) {
1026 pinctrl_unregister(pctrl->pctldev);
1027 return ret;
1028 }
1029
1030 platform_set_drvdata(pdev, pctrl);
1031
1032 return 0;
1033}
1034EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1035
1036int intel_pinctrl_remove(struct platform_device *pdev)
1037{
1038 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1039
1040 gpiochip_remove(&pctrl->chip);
1041 pinctrl_unregister(pctrl->pctldev);
1042
1043 return 0;
1044}
1045EXPORT_SYMBOL_GPL(intel_pinctrl_remove);
1046
1047#ifdef CONFIG_PM_SLEEP
1048int intel_pinctrl_suspend(struct device *dev)
1049{
1050 struct platform_device *pdev = to_platform_device(dev);
1051 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1052 struct intel_community_context *communities;
1053 struct intel_pad_context *pads;
1054 int i;
1055
1056 pads = pctrl->context.pads;
1057 for (i = 0; i < pctrl->soc->npins; i++) {
1058 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1059 u32 val;
1060
1061 if (!intel_pad_usable(pctrl, desc->number))
1062 continue;
1063
1064 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1065 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1066 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1067 pads[i].padcfg1 = val;
1068 }
1069
1070 communities = pctrl->context.communities;
1071 for (i = 0; i < pctrl->ncommunities; i++) {
1072 struct intel_community *community = &pctrl->communities[i];
1073 void __iomem *base;
1074 unsigned gpp;
1075
1076 base = community->regs + community->ie_offset;
1077 for (gpp = 0; gpp < community->ngpps; gpp++)
1078 communities[i].intmask[gpp] = readl(base + gpp * 4);
1079 }
1080
1081 return 0;
1082}
1083EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1084
Mika Westerbergf487bbf2015-10-13 17:51:25 +03001085static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
1086{
1087 size_t i;
1088
1089 for (i = 0; i < pctrl->ncommunities; i++) {
1090 const struct intel_community *community;
1091 void __iomem *base;
1092 unsigned gpp;
1093
1094 community = &pctrl->communities[i];
1095 base = community->regs;
1096
1097 for (gpp = 0; gpp < community->ngpps; gpp++) {
1098 /* Mask and clear all interrupts */
1099 writel(0, base + community->ie_offset + gpp * 4);
1100 writel(0xffff, base + GPI_IS + gpp * 4);
1101 }
1102 }
1103}
1104
Mika Westerberg7981c0012015-03-30 17:31:49 +03001105int intel_pinctrl_resume(struct device *dev)
1106{
1107 struct platform_device *pdev = to_platform_device(dev);
1108 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1109 const struct intel_community_context *communities;
1110 const struct intel_pad_context *pads;
1111 int i;
1112
1113 /* Mask all interrupts */
1114 intel_gpio_irq_init(pctrl);
1115
1116 pads = pctrl->context.pads;
1117 for (i = 0; i < pctrl->soc->npins; i++) {
1118 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1119 void __iomem *padcfg;
1120 u32 val;
1121
1122 if (!intel_pad_usable(pctrl, desc->number))
1123 continue;
1124
1125 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1126 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1127 if (val != pads[i].padcfg0) {
1128 writel(pads[i].padcfg0, padcfg);
1129 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1130 desc->number, readl(padcfg));
1131 }
1132
1133 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1134 val = readl(padcfg);
1135 if (val != pads[i].padcfg1) {
1136 writel(pads[i].padcfg1, padcfg);
1137 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1138 desc->number, readl(padcfg));
1139 }
1140 }
1141
1142 communities = pctrl->context.communities;
1143 for (i = 0; i < pctrl->ncommunities; i++) {
1144 struct intel_community *community = &pctrl->communities[i];
1145 void __iomem *base;
1146 unsigned gpp;
1147
1148 base = community->regs + community->ie_offset;
1149 for (gpp = 0; gpp < community->ngpps; gpp++) {
1150 writel(communities[i].intmask[gpp], base + gpp * 4);
1151 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1152 readl(base + gpp * 4));
1153 }
1154 }
1155
1156 return 0;
1157}
1158EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1159#endif
1160
1161MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1162MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1163MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1164MODULE_LICENSE("GPL v2");