blob: 21b79a446d5ab4c31cb08f7a083b282d87aad0d2 [file] [log] [blame]
Mathias Nymana5d811b2013-06-18 14:33:02 +03001/*
2 * Pinctrl GPIO driver for Intel Baytrail
3 * Copyright (c) 2012-2013, Intel Corporation.
4 *
5 * Author: Mathias Nyman <mathias.nyman@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
Mathias Nymana5d811b2013-06-18 14:33:02 +030015 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/types.h>
21#include <linux/bitops.h>
22#include <linux/interrupt.h>
Linus Walleijbf9a5c92015-12-08 00:03:44 +010023#include <linux/gpio/driver.h>
Mathias Nymana5d811b2013-06-18 14:33:02 +030024#include <linux/acpi.h>
Mathias Nymana5d811b2013-06-18 14:33:02 +030025#include <linux/platform_device.h>
26#include <linux/seq_file.h>
27#include <linux/io.h>
28#include <linux/pm_runtime.h>
29#include <linux/pinctrl/pinctrl.h>
30
31/* memory mapped register offsets */
32#define BYT_CONF0_REG 0x000
33#define BYT_CONF1_REG 0x004
34#define BYT_VAL_REG 0x008
35#define BYT_DFT_REG 0x00c
36#define BYT_INT_STAT_REG 0x800
37
38/* BYT_CONF0_REG register bits */
Mika Westerberg3ff95882014-05-16 12:18:29 +030039#define BYT_IODEN BIT(31)
Eric Ernstff998352014-06-12 11:06:20 -070040#define BYT_DIRECT_IRQ_EN BIT(27)
Mathias Nymana5d811b2013-06-18 14:33:02 +030041#define BYT_TRIG_NEG BIT(26)
42#define BYT_TRIG_POS BIT(25)
43#define BYT_TRIG_LVL BIT(24)
Mika Westerberg3ff95882014-05-16 12:18:29 +030044#define BYT_PULL_STR_SHIFT 9
45#define BYT_PULL_STR_MASK (3 << BYT_PULL_STR_SHIFT)
46#define BYT_PULL_STR_2K (0 << BYT_PULL_STR_SHIFT)
47#define BYT_PULL_STR_10K (1 << BYT_PULL_STR_SHIFT)
48#define BYT_PULL_STR_20K (2 << BYT_PULL_STR_SHIFT)
49#define BYT_PULL_STR_40K (3 << BYT_PULL_STR_SHIFT)
50#define BYT_PULL_ASSIGN_SHIFT 7
51#define BYT_PULL_ASSIGN_MASK (3 << BYT_PULL_ASSIGN_SHIFT)
52#define BYT_PULL_ASSIGN_UP (1 << BYT_PULL_ASSIGN_SHIFT)
53#define BYT_PULL_ASSIGN_DOWN (2 << BYT_PULL_ASSIGN_SHIFT)
Mathias Nymana5d811b2013-06-18 14:33:02 +030054#define BYT_PIN_MUX 0x07
55
56/* BYT_VAL_REG register bits */
57#define BYT_INPUT_EN BIT(2) /* 0: input enabled (active low)*/
58#define BYT_OUTPUT_EN BIT(1) /* 0: output enabled (active low)*/
59#define BYT_LEVEL BIT(0)
60
61#define BYT_DIR_MASK (BIT(1) | BIT(2))
62#define BYT_TRIG_MASK (BIT(26) | BIT(25) | BIT(24))
63
Mika Westerbergfcc18de2015-02-23 14:53:13 +020064#define BYT_CONF0_RESTORE_MASK (BYT_DIRECT_IRQ_EN | BYT_TRIG_MASK | \
65 BYT_PIN_MUX)
66#define BYT_VAL_RESTORE_MASK (BYT_DIR_MASK | BYT_LEVEL)
67
Mathias Nymana5d811b2013-06-18 14:33:02 +030068#define BYT_NGPIO_SCORE 102
69#define BYT_NGPIO_NCORE 28
70#define BYT_NGPIO_SUS 44
71
Chew, Kean Ho42bd0072014-03-06 21:59:49 +080072#define BYT_SCORE_ACPI_UID "1"
73#define BYT_NCORE_ACPI_UID "2"
74#define BYT_SUS_ACPI_UID "3"
75
Mathias Nymana5d811b2013-06-18 14:33:02 +030076/*
77 * Baytrail gpio controller consist of three separate sub-controllers called
78 * SCORE, NCORE and SUS. The sub-controllers are identified by their acpi UID.
79 *
80 * GPIO numbering is _not_ ordered meaning that gpio # 0 in ACPI namespace does
81 * _not_ correspond to the first gpio register at controller's gpio base.
82 * There is no logic or pattern in mapping gpio numbers to registers (pads) so
83 * each sub-controller needs to have its own mapping table
84 */
85
86/* score_pins[gpio_nr] = pad_nr */
87
88static unsigned const score_pins[BYT_NGPIO_SCORE] = {
89 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
90 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
91 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
92 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
93 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
94 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
95 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
96 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
97 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
98 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
99 97, 100,
100};
101
102static unsigned const ncore_pins[BYT_NGPIO_NCORE] = {
103 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
104 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
105 3, 6, 10, 13, 2, 5, 9, 7,
106};
107
108static unsigned const sus_pins[BYT_NGPIO_SUS] = {
109 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
110 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
111 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
112 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
113 52, 53, 59, 40,
114};
115
116static struct pinctrl_gpio_range byt_ranges[] = {
117 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800118 .name = BYT_SCORE_ACPI_UID, /* match with acpi _UID in probe */
Mathias Nymana5d811b2013-06-18 14:33:02 +0300119 .npins = BYT_NGPIO_SCORE,
120 .pins = score_pins,
121 },
122 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800123 .name = BYT_NCORE_ACPI_UID,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300124 .npins = BYT_NGPIO_NCORE,
125 .pins = ncore_pins,
126 },
127 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800128 .name = BYT_SUS_ACPI_UID,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300129 .npins = BYT_NGPIO_SUS,
130 .pins = sus_pins,
131 },
132 {
133 },
134};
135
Mika Westerbergfcc18de2015-02-23 14:53:13 +0200136struct byt_gpio_pin_context {
137 u32 conf0;
138 u32 val;
139};
140
Mathias Nymana5d811b2013-06-18 14:33:02 +0300141struct byt_gpio {
142 struct gpio_chip chip;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300143 struct platform_device *pdev;
Mika Westerberg78e1c892015-08-17 16:03:17 +0300144 raw_spinlock_t lock;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300145 void __iomem *reg_base;
146 struct pinctrl_gpio_range *range;
Mika Westerbergfcc18de2015-02-23 14:53:13 +0200147 struct byt_gpio_pin_context *saved_context;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300148};
149
150static void __iomem *byt_gpio_reg(struct gpio_chip *chip, unsigned offset,
151 int reg)
152{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100153 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300154 u32 reg_offset;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300155
156 if (reg == BYT_INT_STAT_REG)
157 reg_offset = (offset / 32) * 4;
158 else
159 reg_offset = vg->range->pins[offset] * 16;
160
Andy Shevchenko9c5b6552013-07-10 14:55:38 +0300161 return vg->reg_base + reg_offset + reg;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300162}
163
Mika Westerberg95f09722015-02-23 14:53:11 +0200164static void byt_gpio_clear_triggering(struct byt_gpio *vg, unsigned offset)
165{
166 void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
167 unsigned long flags;
168 u32 value;
169
Mika Westerberg78e1c892015-08-17 16:03:17 +0300170 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg95f09722015-02-23 14:53:11 +0200171 value = readl(reg);
172 value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
173 writel(value, reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +0300174 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg95f09722015-02-23 14:53:11 +0200175}
176
Mika Westerbergf8323b62015-02-23 14:53:10 +0200177static u32 byt_get_gpio_mux(struct byt_gpio *vg, unsigned offset)
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800178{
179 /* SCORE pin 92-93 */
180 if (!strcmp(vg->range->name, BYT_SCORE_ACPI_UID) &&
181 offset >= 92 && offset <= 93)
Mika Westerbergf8323b62015-02-23 14:53:10 +0200182 return 1;
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800183
184 /* SUS pin 11-21 */
185 if (!strcmp(vg->range->name, BYT_SUS_ACPI_UID) &&
186 offset >= 11 && offset <= 21)
Mika Westerbergf8323b62015-02-23 14:53:10 +0200187 return 1;
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800188
Mika Westerbergf8323b62015-02-23 14:53:10 +0200189 return 0;
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800190}
191
Mathias Nymana5d811b2013-06-18 14:33:02 +0300192static int byt_gpio_request(struct gpio_chip *chip, unsigned offset)
193{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100194 struct byt_gpio *vg = gpiochip_get_data(chip);
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800195 void __iomem *reg = byt_gpio_reg(chip, offset, BYT_CONF0_REG);
Mika Westerbergf8323b62015-02-23 14:53:10 +0200196 u32 value, gpio_mux;
Mika Westerberg39ce8152015-08-04 15:03:14 +0300197 unsigned long flags;
198
Mika Westerberg78e1c892015-08-17 16:03:17 +0300199 raw_spin_lock_irqsave(&vg->lock, flags);
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800200
201 /*
202 * In most cases, func pin mux 000 means GPIO function.
203 * But, some pins may have func pin mux 001 represents
Mika Westerbergf8323b62015-02-23 14:53:10 +0200204 * GPIO function.
205 *
206 * Because there are devices out there where some pins were not
207 * configured correctly we allow changing the mux value from
208 * request (but print out warning about that).
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800209 */
210 value = readl(reg) & BYT_PIN_MUX;
Mika Westerbergf8323b62015-02-23 14:53:10 +0200211 gpio_mux = byt_get_gpio_mux(vg, offset);
212 if (WARN_ON(gpio_mux != value)) {
Mika Westerbergf8323b62015-02-23 14:53:10 +0200213 value = readl(reg) & ~BYT_PIN_MUX;
214 value |= gpio_mux;
215 writel(value, reg);
Mika Westerbergf8323b62015-02-23 14:53:10 +0200216
217 dev_warn(&vg->pdev->dev,
218 "pin %u forcibly re-configured as GPIO\n", offset);
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800219 }
Mathias Nymana5d811b2013-06-18 14:33:02 +0300220
Mika Westerberg78e1c892015-08-17 16:03:17 +0300221 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +0300222
Mathias Nymana5d811b2013-06-18 14:33:02 +0300223 pm_runtime_get(&vg->pdev->dev);
224
225 return 0;
226}
227
228static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
229{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100230 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300231
Mika Westerberg95f09722015-02-23 14:53:11 +0200232 byt_gpio_clear_triggering(vg, offset);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300233 pm_runtime_put(&vg->pdev->dev);
234}
235
236static int byt_irq_type(struct irq_data *d, unsigned type)
237{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100238 struct byt_gpio *vg = gpiochip_get_data(irq_data_get_irq_chip_data(d));
Mathias Nymana5d811b2013-06-18 14:33:02 +0300239 u32 offset = irqd_to_hwirq(d);
240 u32 value;
241 unsigned long flags;
242 void __iomem *reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
243
244 if (offset >= vg->chip.ngpio)
245 return -EINVAL;
246
Mika Westerberg78e1c892015-08-17 16:03:17 +0300247 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300248 value = readl(reg);
249
Loic Poulain3a71c052014-09-26 16:14:51 +0200250 WARN(value & BYT_DIRECT_IRQ_EN,
251 "Bad pad config for io mode, force direct_irq_en bit clearing");
252
Mathias Nymana5d811b2013-06-18 14:33:02 +0300253 /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
254 * are used to indicate high and low level triggering
255 */
Loic Poulain3a71c052014-09-26 16:14:51 +0200256 value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
257 BYT_TRIG_LVL);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300258
Mathias Nymana5d811b2013-06-18 14:33:02 +0300259 writel(value, reg);
260
Mika Westerberg31e43292015-02-23 14:53:12 +0200261 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerf3a085b2015-06-23 15:52:44 +0200262 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg31e43292015-02-23 14:53:12 +0200263 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerf3a085b2015-06-23 15:52:44 +0200264 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg31e43292015-02-23 14:53:12 +0200265
Mika Westerberg78e1c892015-08-17 16:03:17 +0300266 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300267
268 return 0;
269}
270
271static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
272{
273 void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100274 struct byt_gpio *vg = gpiochip_get_data(chip);
Mika Westerberg39ce8152015-08-04 15:03:14 +0300275 unsigned long flags;
276 u32 val;
277
Mika Westerberg78e1c892015-08-17 16:03:17 +0300278 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +0300279 val = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +0300280 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +0300281
Linus Walleij3bde8772015-12-21 16:17:20 +0100282 return !!(val & BYT_LEVEL);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300283}
284
285static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
286{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100287 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300288 void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
289 unsigned long flags;
290 u32 old_val;
291
Mika Westerberg78e1c892015-08-17 16:03:17 +0300292 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300293
294 old_val = readl(reg);
295
296 if (value)
297 writel(old_val | BYT_LEVEL, reg);
298 else
299 writel(old_val & ~BYT_LEVEL, reg);
300
Mika Westerberg78e1c892015-08-17 16:03:17 +0300301 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300302}
303
304static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
305{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100306 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300307 void __iomem *reg = byt_gpio_reg(chip, offset, BYT_VAL_REG);
308 unsigned long flags;
309 u32 value;
310
Mika Westerberg78e1c892015-08-17 16:03:17 +0300311 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300312
313 value = readl(reg) | BYT_DIR_MASK;
Andy Shevchenko496940c2013-07-10 14:55:40 +0300314 value &= ~BYT_INPUT_EN; /* active low */
Mathias Nymana5d811b2013-06-18 14:33:02 +0300315 writel(value, reg);
316
Mika Westerberg78e1c892015-08-17 16:03:17 +0300317 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300318
319 return 0;
320}
321
322static int byt_gpio_direction_output(struct gpio_chip *chip,
323 unsigned gpio, int value)
324{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100325 struct byt_gpio *vg = gpiochip_get_data(chip);
Eric Ernstff998352014-06-12 11:06:20 -0700326 void __iomem *conf_reg = byt_gpio_reg(chip, gpio, BYT_CONF0_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300327 void __iomem *reg = byt_gpio_reg(chip, gpio, BYT_VAL_REG);
328 unsigned long flags;
329 u32 reg_val;
330
Mika Westerberg78e1c892015-08-17 16:03:17 +0300331 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300332
Eric Ernstff998352014-06-12 11:06:20 -0700333 /*
334 * Before making any direction modifications, do a check if gpio
335 * is set for direct IRQ. On baytrail, setting GPIO to output does
336 * not make sense, so let's at least warn the caller before they shoot
337 * themselves in the foot.
338 */
339 WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
340 "Potential Error: Setting GPIO with direct_irq_en to output");
341
Andy Shevchenko496940c2013-07-10 14:55:40 +0300342 reg_val = readl(reg) | BYT_DIR_MASK;
David Cohend90c3382014-10-14 10:54:37 -0700343 reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);
Andy Shevchenko496940c2013-07-10 14:55:40 +0300344
345 if (value)
346 writel(reg_val | BYT_LEVEL, reg);
347 else
348 writel(reg_val & ~BYT_LEVEL, reg);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300349
Mika Westerberg78e1c892015-08-17 16:03:17 +0300350 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300351
352 return 0;
353}
354
355static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
356{
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100357 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300358 int i;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300359 u32 conf0, val, offs;
360
Mathias Nymana5d811b2013-06-18 14:33:02 +0300361 for (i = 0; i < vg->chip.ngpio; i++) {
Mika Westerberg3ff95882014-05-16 12:18:29 +0300362 const char *pull_str = NULL;
363 const char *pull = NULL;
Mika Westerberg78e1c892015-08-17 16:03:17 +0300364 unsigned long flags;
Mathias Nymana4d8d6d2013-11-22 14:01:23 +0200365 const char *label;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300366 offs = vg->range->pins[i] * 16;
Mika Westerberg78e1c892015-08-17 16:03:17 +0300367
368 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300369 conf0 = readl(vg->reg_base + offs + BYT_CONF0_REG);
370 val = readl(vg->reg_base + offs + BYT_VAL_REG);
Mika Westerberg78e1c892015-08-17 16:03:17 +0300371 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300372
Mathias Nymana4d8d6d2013-11-22 14:01:23 +0200373 label = gpiochip_is_requested(chip, i);
374 if (!label)
375 label = "Unrequested";
376
Mika Westerberg3ff95882014-05-16 12:18:29 +0300377 switch (conf0 & BYT_PULL_ASSIGN_MASK) {
378 case BYT_PULL_ASSIGN_UP:
379 pull = "up";
380 break;
381 case BYT_PULL_ASSIGN_DOWN:
382 pull = "down";
383 break;
384 }
385
386 switch (conf0 & BYT_PULL_STR_MASK) {
387 case BYT_PULL_STR_2K:
388 pull_str = "2k";
389 break;
390 case BYT_PULL_STR_10K:
391 pull_str = "10k";
392 break;
393 case BYT_PULL_STR_20K:
394 pull_str = "20k";
395 break;
396 case BYT_PULL_STR_40K:
397 pull_str = "40k";
398 break;
399 }
400
Mathias Nymana5d811b2013-06-18 14:33:02 +0300401 seq_printf(s,
Mika Westerberg3ff95882014-05-16 12:18:29 +0300402 " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
Mathias Nymana5d811b2013-06-18 14:33:02 +0300403 i,
Mathias Nymana4d8d6d2013-11-22 14:01:23 +0200404 label,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300405 val & BYT_INPUT_EN ? " " : "in",
406 val & BYT_OUTPUT_EN ? " " : "out",
407 val & BYT_LEVEL ? "hi" : "lo",
408 vg->range->pins[i], offs,
409 conf0 & 0x7,
Mika Westerberg3ff95882014-05-16 12:18:29 +0300410 conf0 & BYT_TRIG_NEG ? " fall" : " ",
411 conf0 & BYT_TRIG_POS ? " rise" : " ",
412 conf0 & BYT_TRIG_LVL ? " level" : " ");
413
414 if (pull && pull_str)
415 seq_printf(s, " %-4s %-3s", pull, pull_str);
416 else
417 seq_puts(s, " ");
418
419 if (conf0 & BYT_IODEN)
420 seq_puts(s, " open-drain");
421
422 seq_puts(s, "\n");
Mathias Nymana5d811b2013-06-18 14:33:02 +0300423 }
Mathias Nymana5d811b2013-06-18 14:33:02 +0300424}
425
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200426static void byt_gpio_irq_handler(struct irq_desc *desc)
Mathias Nymana5d811b2013-06-18 14:33:02 +0300427{
428 struct irq_data *data = irq_desc_get_irq_data(desc);
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100429 struct byt_gpio *vg = gpiochip_get_data(irq_desc_get_handler_data(desc));
Mathias Nymana5d811b2013-06-18 14:33:02 +0300430 struct irq_chip *chip = irq_data_get_irq_chip(data);
Mika Westerberg31e43292015-02-23 14:53:12 +0200431 u32 base, pin;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300432 void __iomem *reg;
Mika Westerberg31e43292015-02-23 14:53:12 +0200433 unsigned long pending;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300434 unsigned virq;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300435
436 /* check from GPIO controller which pin triggered the interrupt */
437 for (base = 0; base < vg->chip.ngpio; base += 32) {
Mathias Nymana5d811b2013-06-18 14:33:02 +0300438 reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
Mika Westerberg31e43292015-02-23 14:53:12 +0200439 pending = readl(reg);
440 for_each_set_bit(pin, &pending, 32) {
Mika Westerberge1ee5c52014-07-25 09:54:47 +0300441 virq = irq_find_mapping(vg->chip.irqdomain, base + pin);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300442 generic_handle_irq(virq);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300443 }
444 }
445 chip->irq_eoi(data);
446}
447
Mika Westerberg31e43292015-02-23 14:53:12 +0200448static void byt_irq_ack(struct irq_data *d)
449{
450 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100451 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +0200452 unsigned offset = irqd_to_hwirq(d);
453 void __iomem *reg;
454
Mika Westerberg78e1c892015-08-17 16:03:17 +0300455 raw_spin_lock(&vg->lock);
Mika Westerberg31e43292015-02-23 14:53:12 +0200456 reg = byt_gpio_reg(&vg->chip, offset, BYT_INT_STAT_REG);
457 writel(BIT(offset % 32), reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +0300458 raw_spin_unlock(&vg->lock);
Mika Westerberg31e43292015-02-23 14:53:12 +0200459}
460
Mathias Nymana5d811b2013-06-18 14:33:02 +0300461static void byt_irq_unmask(struct irq_data *d)
462{
Mika Westerberg31e43292015-02-23 14:53:12 +0200463 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100464 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +0200465 unsigned offset = irqd_to_hwirq(d);
466 unsigned long flags;
467 void __iomem *reg;
468 u32 value;
469
Mika Westerberg31e43292015-02-23 14:53:12 +0200470 reg = byt_gpio_reg(&vg->chip, offset, BYT_CONF0_REG);
Mika Westerberg78e1c892015-08-17 16:03:17 +0300471
472 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg31e43292015-02-23 14:53:12 +0200473 value = readl(reg);
474
475 switch (irqd_get_trigger_type(d)) {
476 case IRQ_TYPE_LEVEL_HIGH:
477 value |= BYT_TRIG_LVL;
478 case IRQ_TYPE_EDGE_RISING:
479 value |= BYT_TRIG_POS;
480 break;
481 case IRQ_TYPE_LEVEL_LOW:
482 value |= BYT_TRIG_LVL;
483 case IRQ_TYPE_EDGE_FALLING:
484 value |= BYT_TRIG_NEG;
485 break;
486 case IRQ_TYPE_EDGE_BOTH:
487 value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
488 break;
489 }
490
491 writel(value, reg);
492
Mika Westerberg78e1c892015-08-17 16:03:17 +0300493 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300494}
495
496static void byt_irq_mask(struct irq_data *d)
497{
Mika Westerberg31e43292015-02-23 14:53:12 +0200498 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100499 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +0200500
501 byt_gpio_clear_triggering(vg, irqd_to_hwirq(d));
Mathias Nymana5d811b2013-06-18 14:33:02 +0300502}
503
504static struct irq_chip byt_irqchip = {
505 .name = "BYT-GPIO",
Mika Westerberg31e43292015-02-23 14:53:12 +0200506 .irq_ack = byt_irq_ack,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300507 .irq_mask = byt_irq_mask,
508 .irq_unmask = byt_irq_unmask,
509 .irq_set_type = byt_irq_type,
Mathias Nyman41939e62014-08-11 16:51:55 +0300510 .flags = IRQCHIP_SKIP_SET_WAKE,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300511};
512
513static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
514{
515 void __iomem *reg;
516 u32 base, value;
Mika Westerberg95f09722015-02-23 14:53:11 +0200517 int i;
518
519 /*
520 * Clear interrupt triggers for all pins that are GPIOs and
521 * do not use direct IRQ mode. This will prevent spurious
522 * interrupts from misconfigured pins.
523 */
524 for (i = 0; i < vg->chip.ngpio; i++) {
525 value = readl(byt_gpio_reg(&vg->chip, i, BYT_CONF0_REG));
526 if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i) &&
527 !(value & BYT_DIRECT_IRQ_EN)) {
528 byt_gpio_clear_triggering(vg, i);
529 dev_dbg(&vg->pdev->dev, "disabling GPIO %d\n", i);
530 }
531 }
Mathias Nymana5d811b2013-06-18 14:33:02 +0300532
533 /* clear interrupt status trigger registers */
534 for (base = 0; base < vg->chip.ngpio; base += 32) {
535 reg = byt_gpio_reg(&vg->chip, base, BYT_INT_STAT_REG);
536 writel(0xffffffff, reg);
537 /* make sure trigger bits are cleared, if not then a pin
538 might be misconfigured in bios */
539 value = readl(reg);
540 if (value)
541 dev_err(&vg->pdev->dev,
542 "GPIO interrupt error, pins misconfigured\n");
543 }
544}
545
Mathias Nymana5d811b2013-06-18 14:33:02 +0300546static int byt_gpio_probe(struct platform_device *pdev)
547{
548 struct byt_gpio *vg;
549 struct gpio_chip *gc;
550 struct resource *mem_rc, *irq_rc;
551 struct device *dev = &pdev->dev;
552 struct acpi_device *acpi_dev;
553 struct pinctrl_gpio_range *range;
554 acpi_handle handle = ACPI_HANDLE(dev);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300555 int ret;
556
557 if (acpi_bus_get_device(handle, &acpi_dev))
558 return -ENODEV;
559
560 vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
561 if (!vg) {
562 dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
563 return -ENOMEM;
564 }
565
566 for (range = byt_ranges; range->name; range++) {
567 if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
568 vg->chip.ngpio = range->npins;
569 vg->range = range;
570 break;
571 }
572 }
573
574 if (!vg->chip.ngpio || !vg->range)
575 return -ENODEV;
576
577 vg->pdev = pdev;
578 platform_set_drvdata(pdev, vg);
579
580 mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
581 vg->reg_base = devm_ioremap_resource(dev, mem_rc);
582 if (IS_ERR(vg->reg_base))
583 return PTR_ERR(vg->reg_base);
584
Mika Westerberg78e1c892015-08-17 16:03:17 +0300585 raw_spin_lock_init(&vg->lock);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300586
587 gc = &vg->chip;
588 gc->label = dev_name(&pdev->dev);
589 gc->owner = THIS_MODULE;
590 gc->request = byt_gpio_request;
591 gc->free = byt_gpio_free;
592 gc->direction_input = byt_gpio_direction_input;
593 gc->direction_output = byt_gpio_direction_output;
594 gc->get = byt_gpio_get;
595 gc->set = byt_gpio_set;
596 gc->dbg_show = byt_gpio_dbg_show;
597 gc->base = -1;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100598 gc->can_sleep = false;
Linus Walleij58383c782015-11-04 09:56:26 +0100599 gc->parent = dev;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300600
Mika Westerbergfcc18de2015-02-23 14:53:13 +0200601#ifdef CONFIG_PM_SLEEP
602 vg->saved_context = devm_kcalloc(&pdev->dev, gc->ngpio,
603 sizeof(*vg->saved_context), GFP_KERNEL);
604#endif
605
Linus Walleijbf9a5c92015-12-08 00:03:44 +0100606 ret = gpiochip_add_data(gc, vg);
Jin Yao605a7bc2014-05-15 18:28:47 +0300607 if (ret) {
608 dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
609 return ret;
610 }
611
Mika Westerberge1ee5c52014-07-25 09:54:47 +0300612 /* set up interrupts */
613 irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
614 if (irq_rc && irq_rc->start) {
615 byt_gpio_irq_init_hw(vg);
616 ret = gpiochip_irqchip_add(gc, &byt_irqchip, 0,
617 handle_simple_irq, IRQ_TYPE_NONE);
618 if (ret) {
619 dev_err(dev, "failed to add irqchip\n");
620 gpiochip_remove(gc);
621 return ret;
622 }
623
624 gpiochip_set_chained_irqchip(gc, &byt_irqchip,
625 (unsigned)irq_rc->start,
626 byt_gpio_irq_handler);
627 }
628
Mathias Nymana5d811b2013-06-18 14:33:02 +0300629 pm_runtime_enable(dev);
630
631 return 0;
632}
633
Mika Westerbergfcc18de2015-02-23 14:53:13 +0200634#ifdef CONFIG_PM_SLEEP
635static int byt_gpio_suspend(struct device *dev)
636{
637 struct platform_device *pdev = to_platform_device(dev);
638 struct byt_gpio *vg = platform_get_drvdata(pdev);
639 int i;
640
641 for (i = 0; i < vg->chip.ngpio; i++) {
642 void __iomem *reg;
643 u32 value;
644
645 reg = byt_gpio_reg(&vg->chip, i, BYT_CONF0_REG);
646 value = readl(reg) & BYT_CONF0_RESTORE_MASK;
647 vg->saved_context[i].conf0 = value;
648
649 reg = byt_gpio_reg(&vg->chip, i, BYT_VAL_REG);
650 value = readl(reg) & BYT_VAL_RESTORE_MASK;
651 vg->saved_context[i].val = value;
652 }
653
654 return 0;
655}
656
657static int byt_gpio_resume(struct device *dev)
658{
659 struct platform_device *pdev = to_platform_device(dev);
660 struct byt_gpio *vg = platform_get_drvdata(pdev);
661 int i;
662
663 for (i = 0; i < vg->chip.ngpio; i++) {
664 void __iomem *reg;
665 u32 value;
666
667 reg = byt_gpio_reg(&vg->chip, i, BYT_CONF0_REG);
668 value = readl(reg);
669 if ((value & BYT_CONF0_RESTORE_MASK) !=
670 vg->saved_context[i].conf0) {
671 value &= ~BYT_CONF0_RESTORE_MASK;
672 value |= vg->saved_context[i].conf0;
673 writel(value, reg);
674 dev_info(dev, "restored pin %d conf0 %#08x", i, value);
675 }
676
677 reg = byt_gpio_reg(&vg->chip, i, BYT_VAL_REG);
678 value = readl(reg);
679 if ((value & BYT_VAL_RESTORE_MASK) !=
680 vg->saved_context[i].val) {
681 u32 v;
682
683 v = value & ~BYT_VAL_RESTORE_MASK;
684 v |= vg->saved_context[i].val;
685 if (v != value) {
686 writel(v, reg);
687 dev_dbg(dev, "restored pin %d val %#08x\n",
688 i, v);
689 }
690 }
691 }
692
693 return 0;
694}
695#endif
696
Mika Westerbergec879f12015-10-13 17:51:26 +0300697#ifdef CONFIG_PM
Mathias Nymana5d811b2013-06-18 14:33:02 +0300698static int byt_gpio_runtime_suspend(struct device *dev)
699{
700 return 0;
701}
702
703static int byt_gpio_runtime_resume(struct device *dev)
704{
705 return 0;
706}
Mika Westerbergec879f12015-10-13 17:51:26 +0300707#endif
Mathias Nymana5d811b2013-06-18 14:33:02 +0300708
709static const struct dev_pm_ops byt_gpio_pm_ops = {
Mika Westerbergfcc18de2015-02-23 14:53:13 +0200710 SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
711 SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
712 NULL)
Mathias Nymana5d811b2013-06-18 14:33:02 +0300713};
714
715static const struct acpi_device_id byt_gpio_acpi_match[] = {
716 { "INT33B2", 0 },
Jin Yao20482d32014-05-15 18:28:46 +0300717 { "INT33FC", 0 },
Mathias Nymana5d811b2013-06-18 14:33:02 +0300718 { }
719};
720MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
721
722static int byt_gpio_remove(struct platform_device *pdev)
723{
724 struct byt_gpio *vg = platform_get_drvdata(pdev);
Andy Shevchenkoec243322013-07-10 14:55:36 +0300725
Mathias Nymana5d811b2013-06-18 14:33:02 +0300726 pm_runtime_disable(&pdev->dev);
abdoulaye bertheb4e7c552014-07-12 22:30:13 +0200727 gpiochip_remove(&vg->chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +0300728
729 return 0;
730}
731
732static struct platform_driver byt_gpio_driver = {
733 .probe = byt_gpio_probe,
734 .remove = byt_gpio_remove,
735 .driver = {
736 .name = "byt_gpio",
Mathias Nymana5d811b2013-06-18 14:33:02 +0300737 .pm = &byt_gpio_pm_ops,
738 .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
739 },
740};
741
742static int __init byt_gpio_init(void)
743{
744 return platform_driver_register(&byt_gpio_driver);
745}
Mathias Nymana5d811b2013-06-18 14:33:02 +0300746subsys_initcall(byt_gpio_init);
Felipe Balbi9067bbe2014-10-13 15:50:08 -0500747
748static void __exit byt_gpio_exit(void)
749{
750 platform_driver_unregister(&byt_gpio_driver);
751}
752module_exit(byt_gpio_exit);