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