blob: 928a00b48d501e34390873569573d9297d49c527 [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>
14#include <linux/init.h>
15#include <linux/acpi.h>
16#include <linux/gpio.h>
17#include <linux/gpio/driver.h>
18#include <linux/platform_device.h>
19#include <linux/pm.h>
20#include <linux/pinctrl/pinctrl.h>
21#include <linux/pinctrl/pinmux.h>
22#include <linux/pinctrl/pinconf.h>
23#include <linux/pinctrl/pinconf-generic.h>
24
25#include "pinctrl-intel.h"
26
27/* Maximum number of pads in each group */
28#define NPADS_IN_GPP 24
29
30/* Offset from regs */
31#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))
39
40/* Offset from pad_regs */
41#define PADCFG0 0x000
42#define PADCFG0_RXEVCFG_SHIFT 25
43#define PADCFG0_RXEVCFG_MASK (3 << PADCFG0_RXEVCFG_SHIFT)
44#define PADCFG0_RXEVCFG_LEVEL 0
45#define PADCFG0_RXEVCFG_EDGE 1
46#define PADCFG0_RXEVCFG_DISABLED 2
47#define PADCFG0_RXEVCFG_EDGE_BOTH 3
48#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
69struct intel_pad_context {
70 u32 padcfg0;
71 u32 padcfg1;
72};
73
74struct intel_community_context {
75 u32 *intmask;
76};
77
78struct intel_pinctrl_context {
79 struct intel_pad_context *pads;
80 struct intel_community_context *communities;
81};
82
83/**
84 * struct intel_pinctrl - Intel pinctrl private structure
85 * @dev: Pointer to the device structure
86 * @lock: Lock to serialize register access
87 * @pctldesc: Pin controller description
88 * @pctldev: Pointer to the pin controller device
89 * @chip: GPIO chip in this pin controller
90 * @soc: SoC/PCH specific pin configuration data
91 * @communities: All communities in this pin controller
92 * @ncommunities: Number of communities in this pin controller
93 * @context: Configuration saved over system sleep
94 */
95struct intel_pinctrl {
96 struct device *dev;
97 spinlock_t lock;
98 struct pinctrl_desc pctldesc;
99 struct pinctrl_dev *pctldev;
100 struct gpio_chip chip;
101 const struct intel_pinctrl_soc_data *soc;
102 struct intel_community *communities;
103 size_t ncommunities;
104 struct intel_pinctrl_context context;
105};
106
107#define gpiochip_to_pinctrl(c) container_of(c, struct intel_pinctrl, chip)
108#define pin_to_padno(c, p) ((p) - (c)->pin_base)
109
110static struct intel_community *intel_get_community(struct intel_pinctrl *pctrl,
111 unsigned pin)
112{
113 struct intel_community *community;
114 int i;
115
116 for (i = 0; i < pctrl->ncommunities; i++) {
117 community = &pctrl->communities[i];
118 if (pin >= community->pin_base &&
119 pin < community->pin_base + community->npins)
120 return community;
121 }
122
123 dev_warn(pctrl->dev, "failed to find community for pin %u\n", pin);
124 return NULL;
125}
126
127static void __iomem *intel_get_padcfg(struct intel_pinctrl *pctrl, unsigned pin,
128 unsigned reg)
129{
130 const struct intel_community *community;
131 unsigned padno;
132
133 community = intel_get_community(pctrl, pin);
134 if (!community)
135 return NULL;
136
137 padno = pin_to_padno(community, pin);
138 return community->pad_regs + reg + padno * 8;
139}
140
141static bool intel_pad_owned_by_host(struct intel_pinctrl *pctrl, unsigned pin)
142{
143 const struct intel_community *community;
144 unsigned padno, gpp, gpp_offset, offset;
145 void __iomem *padown;
146
147 community = intel_get_community(pctrl, pin);
148 if (!community)
149 return false;
150 if (!community->padown_offset)
151 return true;
152
153 padno = pin_to_padno(community, pin);
154 gpp = padno / NPADS_IN_GPP;
155 gpp_offset = padno % NPADS_IN_GPP;
156 offset = community->padown_offset + gpp * 16 + (gpp_offset / 8) * 4;
157 padown = community->regs + offset;
158
159 return !(readl(padown) & PADOWN_MASK(padno));
160}
161
162static bool intel_pad_reserved_for_acpi(struct intel_pinctrl *pctrl,
163 unsigned pin)
164{
165 const struct intel_community *community;
166 unsigned padno, gpp, offset;
167 void __iomem *hostown;
168
169 community = intel_get_community(pctrl, pin);
170 if (!community)
171 return true;
172 if (!community->hostown_offset)
173 return false;
174
175 padno = pin_to_padno(community, pin);
176 gpp = padno / NPADS_IN_GPP;
177 offset = community->hostown_offset + gpp * 4;
178 hostown = community->regs + offset;
179
180 return !(readl(hostown) & BIT(padno % NPADS_IN_GPP));
181}
182
183static bool intel_pad_locked(struct intel_pinctrl *pctrl, unsigned pin)
184{
185 struct intel_community *community;
186 unsigned padno, gpp, offset;
187 u32 value;
188
189 community = intel_get_community(pctrl, pin);
190 if (!community)
191 return true;
192 if (!community->padcfglock_offset)
193 return false;
194
195 padno = pin_to_padno(community, pin);
196 gpp = padno / NPADS_IN_GPP;
197
198 /*
199 * If PADCFGLOCK and PADCFGLOCKTX bits are both clear for this pad,
200 * the pad is considered unlocked. Any other case means that it is
201 * either fully or partially locked and we don't touch it.
202 */
203 offset = community->padcfglock_offset + gpp * 8;
204 value = readl(community->regs + offset);
205 if (value & BIT(pin % NPADS_IN_GPP))
206 return true;
207
208 offset = community->padcfglock_offset + 4 + gpp * 8;
209 value = readl(community->regs + offset);
210 if (value & BIT(pin % NPADS_IN_GPP))
211 return true;
212
213 return false;
214}
215
216static bool intel_pad_usable(struct intel_pinctrl *pctrl, unsigned pin)
217{
218 return intel_pad_owned_by_host(pctrl, pin) &&
219 !intel_pad_reserved_for_acpi(pctrl, pin) &&
220 !intel_pad_locked(pctrl, pin);
221}
222
223static int intel_get_groups_count(struct pinctrl_dev *pctldev)
224{
225 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
226
227 return pctrl->soc->ngroups;
228}
229
230static const char *intel_get_group_name(struct pinctrl_dev *pctldev,
231 unsigned group)
232{
233 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
234
235 return pctrl->soc->groups[group].name;
236}
237
238static int intel_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
239 const unsigned **pins, unsigned *npins)
240{
241 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
242
243 *pins = pctrl->soc->groups[group].pins;
244 *npins = pctrl->soc->groups[group].npins;
245 return 0;
246}
247
248static void intel_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
249 unsigned pin)
250{
251 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
252 u32 cfg0, cfg1, mode;
253 bool locked, acpi;
254
255 if (!intel_pad_owned_by_host(pctrl, pin)) {
256 seq_puts(s, "not available");
257 return;
258 }
259
260 cfg0 = readl(intel_get_padcfg(pctrl, pin, PADCFG0));
261 cfg1 = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
262
263 mode = (cfg0 & PADCFG0_PMODE_MASK) >> PADCFG0_PMODE_SHIFT;
264 if (!mode)
265 seq_puts(s, "GPIO ");
266 else
267 seq_printf(s, "mode %d ", mode);
268
269 seq_printf(s, "0x%08x 0x%08x", cfg0, cfg1);
270
271 locked = intel_pad_locked(pctrl, pin);
272 acpi = intel_pad_reserved_for_acpi(pctrl, pin);
273
274 if (locked || acpi) {
275 seq_puts(s, " [");
276 if (locked) {
277 seq_puts(s, "LOCKED");
278 if (acpi)
279 seq_puts(s, ", ");
280 }
281 if (acpi)
282 seq_puts(s, "ACPI");
283 seq_puts(s, "]");
284 }
285}
286
287static const struct pinctrl_ops intel_pinctrl_ops = {
288 .get_groups_count = intel_get_groups_count,
289 .get_group_name = intel_get_group_name,
290 .get_group_pins = intel_get_group_pins,
291 .pin_dbg_show = intel_pin_dbg_show,
292};
293
294static int intel_get_functions_count(struct pinctrl_dev *pctldev)
295{
296 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
297
298 return pctrl->soc->nfunctions;
299}
300
301static const char *intel_get_function_name(struct pinctrl_dev *pctldev,
302 unsigned function)
303{
304 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
305
306 return pctrl->soc->functions[function].name;
307}
308
309static int intel_get_function_groups(struct pinctrl_dev *pctldev,
310 unsigned function,
311 const char * const **groups,
312 unsigned * const ngroups)
313{
314 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
315
316 *groups = pctrl->soc->functions[function].groups;
317 *ngroups = pctrl->soc->functions[function].ngroups;
318 return 0;
319}
320
321static int intel_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
322 unsigned group)
323{
324 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
325 const struct intel_pingroup *grp = &pctrl->soc->groups[group];
326 unsigned long flags;
327 int i;
328
329 spin_lock_irqsave(&pctrl->lock, flags);
330
331 /*
332 * All pins in the groups needs to be accessible and writable
333 * before we can enable the mux for this group.
334 */
335 for (i = 0; i < grp->npins; i++) {
336 if (!intel_pad_usable(pctrl, grp->pins[i])) {
337 spin_unlock_irqrestore(&pctrl->lock, flags);
338 return -EBUSY;
339 }
340 }
341
342 /* Now enable the mux setting for each pin in the group */
343 for (i = 0; i < grp->npins; i++) {
344 void __iomem *padcfg0;
345 u32 value;
346
347 padcfg0 = intel_get_padcfg(pctrl, grp->pins[i], PADCFG0);
348 value = readl(padcfg0);
349
350 value &= ~PADCFG0_PMODE_MASK;
351 value |= grp->mode << PADCFG0_PMODE_SHIFT;
352
353 writel(value, padcfg0);
354 }
355
356 spin_unlock_irqrestore(&pctrl->lock, flags);
357
358 return 0;
359}
360
361static int intel_gpio_request_enable(struct pinctrl_dev *pctldev,
362 struct pinctrl_gpio_range *range,
363 unsigned pin)
364{
365 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
366 void __iomem *padcfg0;
367 unsigned long flags;
368 u32 value;
369
370 spin_lock_irqsave(&pctrl->lock, flags);
371
372 if (!intel_pad_usable(pctrl, pin)) {
373 spin_unlock_irqrestore(&pctrl->lock, flags);
374 return -EBUSY;
375 }
376
377 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
378 /* Put the pad into GPIO mode */
379 value = readl(padcfg0) & ~PADCFG0_PMODE_MASK;
380 /* Disable SCI/SMI/NMI generation */
381 value &= ~(PADCFG0_GPIROUTIOXAPIC | PADCFG0_GPIROUTSCI);
382 value &= ~(PADCFG0_GPIROUTSMI | PADCFG0_GPIROUTNMI);
383 /* Disable TX buffer and enable RX (this will be input) */
384 value &= ~PADCFG0_GPIORXDIS;
385 value |= PADCFG0_GPIOTXDIS;
386 writel(value, padcfg0);
387
388 spin_unlock_irqrestore(&pctrl->lock, flags);
389
390 return 0;
391}
392
393static int intel_gpio_set_direction(struct pinctrl_dev *pctldev,
394 struct pinctrl_gpio_range *range,
395 unsigned pin, bool input)
396{
397 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
398 void __iomem *padcfg0;
399 unsigned long flags;
400 u32 value;
401
402 spin_lock_irqsave(&pctrl->lock, flags);
403
404 padcfg0 = intel_get_padcfg(pctrl, pin, PADCFG0);
405
406 value = readl(padcfg0);
407 if (input)
408 value |= PADCFG0_GPIOTXDIS;
409 else
410 value &= ~PADCFG0_GPIOTXDIS;
411 writel(value, padcfg0);
412
413 spin_unlock_irqrestore(&pctrl->lock, flags);
414
415 return 0;
416}
417
418static const struct pinmux_ops intel_pinmux_ops = {
419 .get_functions_count = intel_get_functions_count,
420 .get_function_name = intel_get_function_name,
421 .get_function_groups = intel_get_function_groups,
422 .set_mux = intel_pinmux_set_mux,
423 .gpio_request_enable = intel_gpio_request_enable,
424 .gpio_set_direction = intel_gpio_set_direction,
425};
426
427static int intel_config_get(struct pinctrl_dev *pctldev, unsigned pin,
428 unsigned long *config)
429{
430 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
431 enum pin_config_param param = pinconf_to_config_param(*config);
432 u32 value, term;
433 u16 arg = 0;
434
435 if (!intel_pad_owned_by_host(pctrl, pin))
436 return -ENOTSUPP;
437
438 value = readl(intel_get_padcfg(pctrl, pin, PADCFG1));
439 term = (value & PADCFG1_TERM_MASK) >> PADCFG1_TERM_SHIFT;
440
441 switch (param) {
442 case PIN_CONFIG_BIAS_DISABLE:
443 if (term)
444 return -EINVAL;
445 break;
446
447 case PIN_CONFIG_BIAS_PULL_UP:
448 if (!term || !(value & PADCFG1_TERM_UP))
449 return -EINVAL;
450
451 switch (term) {
452 case PADCFG1_TERM_1K:
453 arg = 1000;
454 break;
455 case PADCFG1_TERM_2K:
456 arg = 2000;
457 break;
458 case PADCFG1_TERM_5K:
459 arg = 5000;
460 break;
461 case PADCFG1_TERM_20K:
462 arg = 20000;
463 break;
464 }
465
466 break;
467
468 case PIN_CONFIG_BIAS_PULL_DOWN:
469 if (!term || value & PADCFG1_TERM_UP)
470 return -EINVAL;
471
472 switch (term) {
473 case PADCFG1_TERM_5K:
474 arg = 5000;
475 break;
476 case PADCFG1_TERM_20K:
477 arg = 20000;
478 break;
479 }
480
481 break;
482
483 default:
484 return -ENOTSUPP;
485 }
486
487 *config = pinconf_to_config_packed(param, arg);
488 return 0;
489}
490
491static int intel_config_set_pull(struct intel_pinctrl *pctrl, unsigned pin,
492 unsigned long config)
493{
494 unsigned param = pinconf_to_config_param(config);
495 unsigned arg = pinconf_to_config_argument(config);
496 void __iomem *padcfg1;
497 unsigned long flags;
498 int ret = 0;
499 u32 value;
500
501 spin_lock_irqsave(&pctrl->lock, flags);
502
503 padcfg1 = intel_get_padcfg(pctrl, pin, PADCFG1);
504 value = readl(padcfg1);
505
506 switch (param) {
507 case PIN_CONFIG_BIAS_DISABLE:
508 value &= ~(PADCFG1_TERM_MASK | PADCFG1_TERM_UP);
509 break;
510
511 case PIN_CONFIG_BIAS_PULL_UP:
512 value &= ~PADCFG1_TERM_MASK;
513
514 value |= PADCFG1_TERM_UP;
515
516 switch (arg) {
517 case 20000:
518 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
519 break;
520 case 5000:
521 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
522 break;
523 case 2000:
524 value |= PADCFG1_TERM_2K << PADCFG1_TERM_SHIFT;
525 break;
526 case 1000:
527 value |= PADCFG1_TERM_1K << PADCFG1_TERM_SHIFT;
528 break;
529 default:
530 ret = -EINVAL;
531 }
532
533 break;
534
535 case PIN_CONFIG_BIAS_PULL_DOWN:
536 value &= ~(PADCFG1_TERM_UP | PADCFG1_TERM_MASK);
537
538 switch (arg) {
539 case 20000:
540 value |= PADCFG1_TERM_20K << PADCFG1_TERM_SHIFT;
541 break;
542 case 5000:
543 value |= PADCFG1_TERM_5K << PADCFG1_TERM_SHIFT;
544 break;
545 default:
546 ret = -EINVAL;
547 }
548
549 break;
550 }
551
552 if (!ret)
553 writel(value, padcfg1);
554
555 spin_unlock_irqrestore(&pctrl->lock, flags);
556
557 return ret;
558}
559
560static int intel_config_set(struct pinctrl_dev *pctldev, unsigned pin,
561 unsigned long *configs, unsigned nconfigs)
562{
563 struct intel_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
564 int i, ret;
565
566 if (!intel_pad_usable(pctrl, pin))
567 return -ENOTSUPP;
568
569 for (i = 0; i < nconfigs; i++) {
570 switch (pinconf_to_config_param(configs[i])) {
571 case PIN_CONFIG_BIAS_DISABLE:
572 case PIN_CONFIG_BIAS_PULL_UP:
573 case PIN_CONFIG_BIAS_PULL_DOWN:
574 ret = intel_config_set_pull(pctrl, pin, configs[i]);
575 if (ret)
576 return ret;
577 break;
578
579 default:
580 return -ENOTSUPP;
581 }
582 }
583
584 return 0;
585}
586
587static const struct pinconf_ops intel_pinconf_ops = {
588 .is_generic = true,
589 .pin_config_get = intel_config_get,
590 .pin_config_set = intel_config_set,
591};
592
593static const struct pinctrl_desc intel_pinctrl_desc = {
594 .pctlops = &intel_pinctrl_ops,
595 .pmxops = &intel_pinmux_ops,
596 .confops = &intel_pinconf_ops,
597 .owner = THIS_MODULE,
598};
599
Mika Westerberg7981c0012015-03-30 17:31:49 +0300600static int intel_gpio_get(struct gpio_chip *chip, unsigned offset)
601{
602 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(chip);
603 void __iomem *reg;
604
605 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
606 if (!reg)
607 return -EINVAL;
608
609 return !!(readl(reg) & PADCFG0_GPIORXSTATE);
610}
611
612static void intel_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
613{
614 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(chip);
615 void __iomem *reg;
616
617 reg = intel_get_padcfg(pctrl, offset, PADCFG0);
618 if (reg) {
619 unsigned long flags;
620 u32 padcfg0;
621
622 spin_lock_irqsave(&pctrl->lock, flags);
623 padcfg0 = readl(reg);
624 if (value)
625 padcfg0 |= PADCFG0_GPIOTXSTATE;
626 else
627 padcfg0 &= ~PADCFG0_GPIOTXSTATE;
628 writel(padcfg0, reg);
629 spin_unlock_irqrestore(&pctrl->lock, flags);
630 }
631}
632
633static int intel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
634{
635 return pinctrl_gpio_direction_input(chip->base + offset);
636}
637
638static int intel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
639 int value)
640{
641 intel_gpio_set(chip, offset, value);
642 return pinctrl_gpio_direction_output(chip->base + offset);
643}
644
645static const struct gpio_chip intel_gpio_chip = {
646 .owner = THIS_MODULE,
Jonas Gorski98c85d52015-10-11 17:34:19 +0200647 .request = gpiochip_generic_request,
648 .free = gpiochip_generic_free,
Mika Westerberg7981c0012015-03-30 17:31:49 +0300649 .direction_input = intel_gpio_direction_input,
650 .direction_output = intel_gpio_direction_output,
651 .get = intel_gpio_get,
652 .set = intel_gpio_set,
653};
654
655static void intel_gpio_irq_ack(struct irq_data *d)
656{
657 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
658 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
659 const struct intel_community *community;
660 unsigned pin = irqd_to_hwirq(d);
661
662 spin_lock(&pctrl->lock);
663
664 community = intel_get_community(pctrl, pin);
665 if (community) {
666 unsigned padno = pin_to_padno(community, pin);
667 unsigned gpp_offset = padno % NPADS_IN_GPP;
668 unsigned gpp = padno / NPADS_IN_GPP;
669
670 writel(BIT(gpp_offset), community->regs + GPI_IS + gpp * 4);
671 }
672
673 spin_unlock(&pctrl->lock);
674}
675
676static void intel_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
677{
678 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
679 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
680 const struct intel_community *community;
681 unsigned pin = irqd_to_hwirq(d);
682 unsigned long flags;
683
684 spin_lock_irqsave(&pctrl->lock, flags);
685
686 community = intel_get_community(pctrl, pin);
687 if (community) {
688 unsigned padno = pin_to_padno(community, pin);
689 unsigned gpp_offset = padno % NPADS_IN_GPP;
690 unsigned gpp = padno / NPADS_IN_GPP;
691 void __iomem *reg;
692 u32 value;
693
694 reg = community->regs + community->ie_offset + gpp * 4;
695 value = readl(reg);
696 if (mask)
697 value &= ~BIT(gpp_offset);
698 else
699 value |= BIT(gpp_offset);
700 writel(value, reg);
701 }
702
703 spin_unlock_irqrestore(&pctrl->lock, flags);
704}
705
706static void intel_gpio_irq_mask(struct irq_data *d)
707{
708 intel_gpio_irq_mask_unmask(d, true);
709}
710
711static void intel_gpio_irq_unmask(struct irq_data *d)
712{
713 intel_gpio_irq_mask_unmask(d, false);
714}
715
716static int intel_gpio_irq_type(struct irq_data *d, unsigned type)
717{
718 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
719 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
720 unsigned pin = irqd_to_hwirq(d);
721 unsigned long flags;
722 void __iomem *reg;
723 u32 value;
724
725 reg = intel_get_padcfg(pctrl, pin, PADCFG0);
726 if (!reg)
727 return -EINVAL;
728
729 spin_lock_irqsave(&pctrl->lock, flags);
730
731 value = readl(reg);
732
733 value &= ~(PADCFG0_RXEVCFG_MASK | PADCFG0_RXINV);
734
735 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
736 value |= PADCFG0_RXEVCFG_EDGE_BOTH << PADCFG0_RXEVCFG_SHIFT;
737 } else if (type & IRQ_TYPE_EDGE_FALLING) {
738 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
739 value |= PADCFG0_RXINV;
740 } else if (type & IRQ_TYPE_EDGE_RISING) {
741 value |= PADCFG0_RXEVCFG_EDGE << PADCFG0_RXEVCFG_SHIFT;
742 } else if (type & IRQ_TYPE_LEVEL_LOW) {
743 value |= PADCFG0_RXINV;
744 } else {
745 value |= PADCFG0_RXEVCFG_DISABLED << PADCFG0_RXEVCFG_SHIFT;
746 }
747
748 writel(value, reg);
749
750 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200751 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300752 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerfc756bc2015-06-23 15:52:45 +0200753 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300754
755 spin_unlock_irqrestore(&pctrl->lock, flags);
756
757 return 0;
758}
759
760static int intel_gpio_irq_wake(struct irq_data *d, unsigned int on)
761{
762 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
763 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
764 const struct intel_community *community;
765 unsigned pin = irqd_to_hwirq(d);
766 unsigned padno, gpp, gpp_offset;
767 u32 gpe_en;
768
769 community = intel_get_community(pctrl, pin);
770 if (!community)
771 return -EINVAL;
772
773 padno = pin_to_padno(community, pin);
774 gpp = padno / NPADS_IN_GPP;
775 gpp_offset = padno % NPADS_IN_GPP;
776
777 /* Clear the existing wake status */
778 writel(BIT(gpp_offset), community->regs + GPI_GPE_STS + gpp * 4);
779
780 /*
781 * The controller will generate wake when GPE of the corresponding
782 * pad is enabled and it is not routed to SCI (GPIROUTSCI is not
783 * set).
784 */
785 gpe_en = readl(community->regs + GPI_GPE_EN + gpp * 4);
786 if (on)
787 gpe_en |= BIT(gpp_offset);
788 else
789 gpe_en &= ~BIT(gpp_offset);
790 writel(gpe_en, community->regs + GPI_GPE_EN + gpp * 4);
791
792 dev_dbg(pctrl->dev, "%sable wake for pin %u\n", on ? "en" : "dis", pin);
793 return 0;
794}
795
796static void intel_gpio_community_irq_handler(struct gpio_chip *gc,
797 const struct intel_community *community)
798{
799 int gpp;
800
801 for (gpp = 0; gpp < community->ngpps; gpp++) {
802 unsigned long pending, enabled, gpp_offset;
803
804 pending = readl(community->regs + GPI_IS + gpp * 4);
805 enabled = readl(community->regs + community->ie_offset +
806 gpp * 4);
807
808 /* Only interrupts that are enabled */
809 pending &= enabled;
810
811 for_each_set_bit(gpp_offset, &pending, NPADS_IN_GPP) {
812 unsigned padno, irq;
813
814 /*
815 * The last group in community can have less pins
816 * than NPADS_IN_GPP.
817 */
818 padno = gpp_offset + gpp * NPADS_IN_GPP;
819 if (padno >= community->npins)
820 break;
821
822 irq = irq_find_mapping(gc->irqdomain,
823 community->pin_base + padno);
824 generic_handle_irq(irq);
825 }
826 }
827}
828
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200829static void intel_gpio_irq_handler(struct irq_desc *desc)
Mika Westerberg7981c0012015-03-30 17:31:49 +0300830{
831 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
832 struct intel_pinctrl *pctrl = gpiochip_to_pinctrl(gc);
Jiang Liu5663bb22015-06-04 12:13:16 +0800833 struct irq_chip *chip = irq_desc_get_chip(desc);
Mika Westerberg7981c0012015-03-30 17:31:49 +0300834 int i;
835
836 chained_irq_enter(chip, desc);
837
838 /* Need to check all communities for pending interrupts */
839 for (i = 0; i < pctrl->ncommunities; i++)
840 intel_gpio_community_irq_handler(gc, &pctrl->communities[i]);
841
842 chained_irq_exit(chip, desc);
843}
844
845static struct irq_chip intel_gpio_irqchip = {
846 .name = "intel-gpio",
847 .irq_ack = intel_gpio_irq_ack,
848 .irq_mask = intel_gpio_irq_mask,
849 .irq_unmask = intel_gpio_irq_unmask,
850 .irq_set_type = intel_gpio_irq_type,
851 .irq_set_wake = intel_gpio_irq_wake,
852};
853
854static void intel_gpio_irq_init(struct intel_pinctrl *pctrl)
855{
856 size_t i;
857
858 for (i = 0; i < pctrl->ncommunities; i++) {
859 const struct intel_community *community;
860 void __iomem *base;
861 unsigned gpp;
862
863 community = &pctrl->communities[i];
864 base = community->regs;
865
866 for (gpp = 0; gpp < community->ngpps; gpp++) {
867 /* Mask and clear all interrupts */
868 writel(0, base + community->ie_offset + gpp * 4);
869 writel(0xffff, base + GPI_IS + gpp * 4);
870 }
871 }
872}
873
874static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
875{
876 int ret;
877
878 pctrl->chip = intel_gpio_chip;
879
880 pctrl->chip.ngpio = pctrl->soc->npins;
881 pctrl->chip.label = dev_name(pctrl->dev);
882 pctrl->chip.dev = pctrl->dev;
883 pctrl->chip.base = -1;
884
885 ret = gpiochip_add(&pctrl->chip);
886 if (ret) {
887 dev_err(pctrl->dev, "failed to register gpiochip\n");
888 return ret;
889 }
890
891 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev),
892 0, 0, pctrl->soc->npins);
893 if (ret) {
894 dev_err(pctrl->dev, "failed to add GPIO pin range\n");
895 gpiochip_remove(&pctrl->chip);
896 return ret;
897 }
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");
903 gpiochip_remove(&pctrl->chip);
904 return ret;
905 }
906
907 gpiochip_set_chained_irqchip(&pctrl->chip, &intel_gpio_irqchip, irq,
908 intel_gpio_irq_handler);
909 return 0;
910}
911
912static int intel_pinctrl_pm_init(struct intel_pinctrl *pctrl)
913{
914#ifdef CONFIG_PM_SLEEP
915 const struct intel_pinctrl_soc_data *soc = pctrl->soc;
916 struct intel_community_context *communities;
917 struct intel_pad_context *pads;
918 int i;
919
920 pads = devm_kcalloc(pctrl->dev, soc->npins, sizeof(*pads), GFP_KERNEL);
921 if (!pads)
922 return -ENOMEM;
923
924 communities = devm_kcalloc(pctrl->dev, pctrl->ncommunities,
925 sizeof(*communities), GFP_KERNEL);
926 if (!communities)
927 return -ENOMEM;
928
929
930 for (i = 0; i < pctrl->ncommunities; i++) {
931 struct intel_community *community = &pctrl->communities[i];
932 u32 *intmask;
933
934 intmask = devm_kcalloc(pctrl->dev, community->ngpps,
935 sizeof(*intmask), GFP_KERNEL);
936 if (!intmask)
937 return -ENOMEM;
938
939 communities[i].intmask = intmask;
940 }
941
942 pctrl->context.pads = pads;
943 pctrl->context.communities = communities;
944#endif
945
946 return 0;
947}
948
949int intel_pinctrl_probe(struct platform_device *pdev,
950 const struct intel_pinctrl_soc_data *soc_data)
951{
952 struct intel_pinctrl *pctrl;
953 int i, ret, irq;
954
955 if (!soc_data)
956 return -EINVAL;
957
958 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
959 if (!pctrl)
960 return -ENOMEM;
961
962 pctrl->dev = &pdev->dev;
963 pctrl->soc = soc_data;
964 spin_lock_init(&pctrl->lock);
965
966 /*
967 * Make a copy of the communities which we can use to hold pointers
968 * to the registers.
969 */
970 pctrl->ncommunities = pctrl->soc->ncommunities;
971 pctrl->communities = devm_kcalloc(&pdev->dev, pctrl->ncommunities,
972 sizeof(*pctrl->communities), GFP_KERNEL);
973 if (!pctrl->communities)
974 return -ENOMEM;
975
976 for (i = 0; i < pctrl->ncommunities; i++) {
977 struct intel_community *community = &pctrl->communities[i];
978 struct resource *res;
979 void __iomem *regs;
980 u32 padbar;
981
982 *community = pctrl->soc->communities[i];
983
984 res = platform_get_resource(pdev, IORESOURCE_MEM,
985 community->barno);
986 regs = devm_ioremap_resource(&pdev->dev, res);
987 if (IS_ERR(regs))
988 return PTR_ERR(regs);
989
990 /* Read offset of the pad configuration registers */
991 padbar = readl(regs + PADBAR);
992
993 community->regs = regs;
994 community->pad_regs = regs + padbar;
995 community->ngpps = DIV_ROUND_UP(community->npins, NPADS_IN_GPP);
996 }
997
998 irq = platform_get_irq(pdev, 0);
999 if (irq < 0) {
1000 dev_err(&pdev->dev, "failed to get interrupt number\n");
1001 return irq;
1002 }
1003
1004 ret = intel_pinctrl_pm_init(pctrl);
1005 if (ret)
1006 return ret;
1007
1008 pctrl->pctldesc = intel_pinctrl_desc;
1009 pctrl->pctldesc.name = dev_name(&pdev->dev);
1010 pctrl->pctldesc.pins = pctrl->soc->pins;
1011 pctrl->pctldesc.npins = pctrl->soc->npins;
1012
1013 pctrl->pctldev = pinctrl_register(&pctrl->pctldesc, &pdev->dev, pctrl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001014 if (IS_ERR(pctrl->pctldev)) {
Mika Westerberg7981c0012015-03-30 17:31:49 +03001015 dev_err(&pdev->dev, "failed to register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001016 return PTR_ERR(pctrl->pctldev);
Mika Westerberg7981c0012015-03-30 17:31:49 +03001017 }
1018
1019 ret = intel_gpio_probe(pctrl, irq);
1020 if (ret) {
1021 pinctrl_unregister(pctrl->pctldev);
1022 return ret;
1023 }
1024
1025 platform_set_drvdata(pdev, pctrl);
1026
1027 return 0;
1028}
1029EXPORT_SYMBOL_GPL(intel_pinctrl_probe);
1030
1031int intel_pinctrl_remove(struct platform_device *pdev)
1032{
1033 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1034
1035 gpiochip_remove(&pctrl->chip);
1036 pinctrl_unregister(pctrl->pctldev);
1037
1038 return 0;
1039}
1040EXPORT_SYMBOL_GPL(intel_pinctrl_remove);
1041
1042#ifdef CONFIG_PM_SLEEP
1043int intel_pinctrl_suspend(struct device *dev)
1044{
1045 struct platform_device *pdev = to_platform_device(dev);
1046 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1047 struct intel_community_context *communities;
1048 struct intel_pad_context *pads;
1049 int i;
1050
1051 pads = pctrl->context.pads;
1052 for (i = 0; i < pctrl->soc->npins; i++) {
1053 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1054 u32 val;
1055
1056 if (!intel_pad_usable(pctrl, desc->number))
1057 continue;
1058
1059 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG0));
1060 pads[i].padcfg0 = val & ~PADCFG0_GPIORXSTATE;
1061 val = readl(intel_get_padcfg(pctrl, desc->number, PADCFG1));
1062 pads[i].padcfg1 = val;
1063 }
1064
1065 communities = pctrl->context.communities;
1066 for (i = 0; i < pctrl->ncommunities; i++) {
1067 struct intel_community *community = &pctrl->communities[i];
1068 void __iomem *base;
1069 unsigned gpp;
1070
1071 base = community->regs + community->ie_offset;
1072 for (gpp = 0; gpp < community->ngpps; gpp++)
1073 communities[i].intmask[gpp] = readl(base + gpp * 4);
1074 }
1075
1076 return 0;
1077}
1078EXPORT_SYMBOL_GPL(intel_pinctrl_suspend);
1079
1080int intel_pinctrl_resume(struct device *dev)
1081{
1082 struct platform_device *pdev = to_platform_device(dev);
1083 struct intel_pinctrl *pctrl = platform_get_drvdata(pdev);
1084 const struct intel_community_context *communities;
1085 const struct intel_pad_context *pads;
1086 int i;
1087
1088 /* Mask all interrupts */
1089 intel_gpio_irq_init(pctrl);
1090
1091 pads = pctrl->context.pads;
1092 for (i = 0; i < pctrl->soc->npins; i++) {
1093 const struct pinctrl_pin_desc *desc = &pctrl->soc->pins[i];
1094 void __iomem *padcfg;
1095 u32 val;
1096
1097 if (!intel_pad_usable(pctrl, desc->number))
1098 continue;
1099
1100 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG0);
1101 val = readl(padcfg) & ~PADCFG0_GPIORXSTATE;
1102 if (val != pads[i].padcfg0) {
1103 writel(pads[i].padcfg0, padcfg);
1104 dev_dbg(dev, "restored pin %u padcfg0 %#08x\n",
1105 desc->number, readl(padcfg));
1106 }
1107
1108 padcfg = intel_get_padcfg(pctrl, desc->number, PADCFG1);
1109 val = readl(padcfg);
1110 if (val != pads[i].padcfg1) {
1111 writel(pads[i].padcfg1, padcfg);
1112 dev_dbg(dev, "restored pin %u padcfg1 %#08x\n",
1113 desc->number, readl(padcfg));
1114 }
1115 }
1116
1117 communities = pctrl->context.communities;
1118 for (i = 0; i < pctrl->ncommunities; i++) {
1119 struct intel_community *community = &pctrl->communities[i];
1120 void __iomem *base;
1121 unsigned gpp;
1122
1123 base = community->regs + community->ie_offset;
1124 for (gpp = 0; gpp < community->ngpps; gpp++) {
1125 writel(communities[i].intmask[gpp], base + gpp * 4);
1126 dev_dbg(dev, "restored mask %d/%u %#08x\n", i, gpp,
1127 readl(base + gpp * 4));
1128 }
1129 }
1130
1131 return 0;
1132}
1133EXPORT_SYMBOL_GPL(intel_pinctrl_resume);
1134#endif
1135
1136MODULE_AUTHOR("Mathias Nyman <mathias.nyman@linux.intel.com>");
1137MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1138MODULE_DESCRIPTION("Intel pinctrl/GPIO core driver");
1139MODULE_LICENSE("GPL v2");