blob: 415d2d5554b54a2ebdcbc1dd3b3e4f562b160040 [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>
Cristina Ciocanc501d0b2016-04-01 14:00:03 +030030#include <linux/pinctrl/pinmux.h>
31#include <linux/pinctrl/pinconf.h>
32#include <linux/pinctrl/pinconf-generic.h>
Mathias Nymana5d811b2013-06-18 14:33:02 +030033
34/* memory mapped register offsets */
35#define BYT_CONF0_REG 0x000
36#define BYT_CONF1_REG 0x004
37#define BYT_VAL_REG 0x008
38#define BYT_DFT_REG 0x00c
39#define BYT_INT_STAT_REG 0x800
40
41/* BYT_CONF0_REG register bits */
Mika Westerberg3ff95882014-05-16 12:18:29 +030042#define BYT_IODEN BIT(31)
Eric Ernstff998352014-06-12 11:06:20 -070043#define BYT_DIRECT_IRQ_EN BIT(27)
Mathias Nymana5d811b2013-06-18 14:33:02 +030044#define BYT_TRIG_NEG BIT(26)
45#define BYT_TRIG_POS BIT(25)
46#define BYT_TRIG_LVL BIT(24)
Mika Westerberg3ff95882014-05-16 12:18:29 +030047#define BYT_PULL_STR_SHIFT 9
48#define BYT_PULL_STR_MASK (3 << BYT_PULL_STR_SHIFT)
49#define BYT_PULL_STR_2K (0 << BYT_PULL_STR_SHIFT)
50#define BYT_PULL_STR_10K (1 << BYT_PULL_STR_SHIFT)
51#define BYT_PULL_STR_20K (2 << BYT_PULL_STR_SHIFT)
52#define BYT_PULL_STR_40K (3 << BYT_PULL_STR_SHIFT)
53#define BYT_PULL_ASSIGN_SHIFT 7
54#define BYT_PULL_ASSIGN_MASK (3 << BYT_PULL_ASSIGN_SHIFT)
55#define BYT_PULL_ASSIGN_UP (1 << BYT_PULL_ASSIGN_SHIFT)
56#define BYT_PULL_ASSIGN_DOWN (2 << BYT_PULL_ASSIGN_SHIFT)
Mathias Nymana5d811b2013-06-18 14:33:02 +030057#define BYT_PIN_MUX 0x07
58
59/* BYT_VAL_REG register bits */
60#define BYT_INPUT_EN BIT(2) /* 0: input enabled (active low)*/
61#define BYT_OUTPUT_EN BIT(1) /* 0: output enabled (active low)*/
62#define BYT_LEVEL BIT(0)
63
64#define BYT_DIR_MASK (BIT(1) | BIT(2))
65#define BYT_TRIG_MASK (BIT(26) | BIT(25) | BIT(24))
66
Mika Westerbergfcc18de2015-02-23 14:53:13 +020067#define BYT_CONF0_RESTORE_MASK (BYT_DIRECT_IRQ_EN | BYT_TRIG_MASK | \
68 BYT_PIN_MUX)
69#define BYT_VAL_RESTORE_MASK (BYT_DIR_MASK | BYT_LEVEL)
70
Mathias Nymana5d811b2013-06-18 14:33:02 +030071#define BYT_NGPIO_SCORE 102
72#define BYT_NGPIO_NCORE 28
73#define BYT_NGPIO_SUS 44
74
Chew, Kean Ho42bd0072014-03-06 21:59:49 +080075#define BYT_SCORE_ACPI_UID "1"
76#define BYT_NCORE_ACPI_UID "2"
77#define BYT_SUS_ACPI_UID "3"
78
Cristina Ciocanc501d0b2016-04-01 14:00:03 +030079/*
80 * This is the function value most pins have for GPIO muxing. If the value
81 * differs from the default one, it must be explicitly mentioned. Otherwise, the
82 * pin control implementation will set the muxing value to default GPIO if it
83 * does not find a match for the requested function.
84 */
85#define BYT_DEFAULT_GPIO_MUX 0
86
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +030087struct byt_gpio_pin_context {
88 u32 conf0;
89 u32 val;
90};
Mathias Nymana5d811b2013-06-18 14:33:02 +030091
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +030092struct byt_simple_func_mux {
93 const char *name;
94 unsigned short func;
95};
96
97struct byt_mixed_func_mux {
98 const char *name;
99 const unsigned short *func_values;
100};
101
102struct byt_pingroup {
103 const char *name;
104 const unsigned int *pins;
105 size_t npins;
106 unsigned short has_simple_funcs;
107 union {
108 const struct byt_simple_func_mux *simple_funcs;
109 const struct byt_mixed_func_mux *mixed_funcs;
110 };
111 size_t nfuncs;
112};
113
114struct byt_function {
115 const char *name;
116 const char * const *groups;
117 size_t ngroups;
118};
119
120struct byt_community {
121 unsigned int pin_base;
122 size_t npins;
123 const unsigned int *pad_map;
124 void __iomem *reg_base;
125};
126
127#define SIMPLE_FUNC(n, f) \
128 { \
129 .name = (n), \
130 .func = (f), \
131 }
132#define MIXED_FUNC(n, f) \
133 { \
134 .name = (n), \
135 .func_values = (f), \
136 }
137
138#define PIN_GROUP_SIMPLE(n, p, f) \
139 { \
140 .name = (n), \
141 .pins = (p), \
142 .npins = ARRAY_SIZE((p)), \
143 .has_simple_funcs = 1, \
144 .simple_funcs = (f), \
145 .nfuncs = ARRAY_SIZE((f)), \
146 }
147#define PIN_GROUP_MIXED(n, p, f) \
148 { \
149 .name = (n), \
150 .pins = (p), \
151 .npins = ARRAY_SIZE((p)), \
152 .has_simple_funcs = 0, \
153 .mixed_funcs = (f), \
154 .nfuncs = ARRAY_SIZE((f)), \
155 }
156
157#define FUNCTION(n, g) \
158 { \
159 .name = (n), \
160 .groups = (g), \
161 .ngroups = ARRAY_SIZE((g)), \
162 }
163
164#define COMMUNITY(p, n, map) \
165 { \
166 .pin_base = (p), \
167 .npins = (n), \
168 .pad_map = (map),\
169 }
170
171struct byt_pinctrl_soc_data {
172 const char *uid;
173 const struct pinctrl_pin_desc *pins;
174 size_t npins;
175 const struct byt_pingroup *groups;
176 size_t ngroups;
177 const struct byt_function *functions;
178 size_t nfunctions;
179 const struct byt_community *communities;
180 size_t ncommunities;
181};
182
183/* SCORE pins, aka GPIOC_<pin_no> or GPIO_S0_SC[<pin_no>] */
184static const struct pinctrl_pin_desc byt_score_pins[] = {
185 PINCTRL_PIN(0, "SATA_GP0"),
186 PINCTRL_PIN(1, "SATA_GP1"),
187 PINCTRL_PIN(2, "SATA_LED#"),
188 PINCTRL_PIN(3, "PCIE_CLKREQ0"),
189 PINCTRL_PIN(4, "PCIE_CLKREQ1"),
190 PINCTRL_PIN(5, "PCIE_CLKREQ2"),
191 PINCTRL_PIN(6, "PCIE_CLKREQ3"),
192 PINCTRL_PIN(7, "SD3_WP"),
193 PINCTRL_PIN(8, "HDA_RST"),
194 PINCTRL_PIN(9, "HDA_SYNC"),
195 PINCTRL_PIN(10, "HDA_CLK"),
196 PINCTRL_PIN(11, "HDA_SDO"),
197 PINCTRL_PIN(12, "HDA_SDI0"),
198 PINCTRL_PIN(13, "HDA_SDI1"),
199 PINCTRL_PIN(14, "GPIO_S0_SC14"),
200 PINCTRL_PIN(15, "GPIO_S0_SC15"),
201 PINCTRL_PIN(16, "MMC1_CLK"),
202 PINCTRL_PIN(17, "MMC1_D0"),
203 PINCTRL_PIN(18, "MMC1_D1"),
204 PINCTRL_PIN(19, "MMC1_D2"),
205 PINCTRL_PIN(20, "MMC1_D3"),
206 PINCTRL_PIN(21, "MMC1_D4"),
207 PINCTRL_PIN(22, "MMC1_D5"),
208 PINCTRL_PIN(23, "MMC1_D6"),
209 PINCTRL_PIN(24, "MMC1_D7"),
210 PINCTRL_PIN(25, "MMC1_CMD"),
211 PINCTRL_PIN(26, "MMC1_RST"),
212 PINCTRL_PIN(27, "SD2_CLK"),
213 PINCTRL_PIN(28, "SD2_D0"),
214 PINCTRL_PIN(29, "SD2_D1"),
215 PINCTRL_PIN(30, "SD2_D2"),
216 PINCTRL_PIN(31, "SD2_D3_CD"),
217 PINCTRL_PIN(32, "SD2_CMD"),
218 PINCTRL_PIN(33, "SD3_CLK"),
219 PINCTRL_PIN(34, "SD3_D0"),
220 PINCTRL_PIN(35, "SD3_D1"),
221 PINCTRL_PIN(36, "SD3_D2"),
222 PINCTRL_PIN(37, "SD3_D3"),
223 PINCTRL_PIN(38, "SD3_CD"),
224 PINCTRL_PIN(39, "SD3_CMD"),
225 PINCTRL_PIN(40, "SD3_1P8EN"),
226 PINCTRL_PIN(41, "SD3_PWREN#"),
227 PINCTRL_PIN(42, "ILB_LPC_AD0"),
228 PINCTRL_PIN(43, "ILB_LPC_AD1"),
229 PINCTRL_PIN(44, "ILB_LPC_AD2"),
230 PINCTRL_PIN(45, "ILB_LPC_AD3"),
231 PINCTRL_PIN(46, "ILB_LPC_FRAME"),
232 PINCTRL_PIN(47, "ILB_LPC_CLK0"),
233 PINCTRL_PIN(48, "ILB_LPC_CLK1"),
234 PINCTRL_PIN(49, "ILB_LPC_CLKRUN"),
235 PINCTRL_PIN(50, "ILB_LPC_SERIRQ"),
236 PINCTRL_PIN(51, "PCU_SMB_DATA"),
237 PINCTRL_PIN(52, "PCU_SMB_CLK"),
238 PINCTRL_PIN(53, "PCU_SMB_ALERT"),
239 PINCTRL_PIN(54, "ILB_8254_SPKR"),
240 PINCTRL_PIN(55, "GPIO_S0_SC55"),
241 PINCTRL_PIN(56, "GPIO_S0_SC56"),
242 PINCTRL_PIN(57, "GPIO_S0_SC57"),
243 PINCTRL_PIN(58, "GPIO_S0_SC58"),
244 PINCTRL_PIN(59, "GPIO_S0_SC59"),
245 PINCTRL_PIN(60, "GPIO_S0_SC60"),
246 PINCTRL_PIN(61, "GPIO_S0_SC61"),
247 PINCTRL_PIN(62, "LPE_I2S2_CLK"),
248 PINCTRL_PIN(63, "LPE_I2S2_FRM"),
249 PINCTRL_PIN(64, "LPE_I2S2_DATAIN"),
250 PINCTRL_PIN(65, "LPE_I2S2_DATAOUT"),
251 PINCTRL_PIN(66, "SIO_SPI_CS"),
252 PINCTRL_PIN(67, "SIO_SPI_MISO"),
253 PINCTRL_PIN(68, "SIO_SPI_MOSI"),
254 PINCTRL_PIN(69, "SIO_SPI_CLK"),
255 PINCTRL_PIN(70, "SIO_UART1_RXD"),
256 PINCTRL_PIN(71, "SIO_UART1_TXD"),
257 PINCTRL_PIN(72, "SIO_UART1_RTS"),
258 PINCTRL_PIN(73, "SIO_UART1_CTS"),
259 PINCTRL_PIN(74, "SIO_UART2_RXD"),
260 PINCTRL_PIN(75, "SIO_UART2_TXD"),
261 PINCTRL_PIN(76, "SIO_UART2_RTS"),
262 PINCTRL_PIN(77, "SIO_UART2_CTS"),
263 PINCTRL_PIN(78, "SIO_I2C0_DATA"),
264 PINCTRL_PIN(79, "SIO_I2C0_CLK"),
265 PINCTRL_PIN(80, "SIO_I2C1_DATA"),
266 PINCTRL_PIN(81, "SIO_I2C1_CLK"),
267 PINCTRL_PIN(82, "SIO_I2C2_DATA"),
268 PINCTRL_PIN(83, "SIO_I2C2_CLK"),
269 PINCTRL_PIN(84, "SIO_I2C3_DATA"),
270 PINCTRL_PIN(85, "SIO_I2C3_CLK"),
271 PINCTRL_PIN(86, "SIO_I2C4_DATA"),
272 PINCTRL_PIN(87, "SIO_I2C4_CLK"),
273 PINCTRL_PIN(88, "SIO_I2C5_DATA"),
274 PINCTRL_PIN(89, "SIO_I2C5_CLK"),
275 PINCTRL_PIN(90, "SIO_I2C6_DATA"),
276 PINCTRL_PIN(91, "SIO_I2C6_CLK"),
277 PINCTRL_PIN(92, "GPIO_S0_SC92"),
278 PINCTRL_PIN(93, "GPIO_S0_SC93"),
279 PINCTRL_PIN(94, "SIO_PWM0"),
280 PINCTRL_PIN(95, "SIO_PWM1"),
281 PINCTRL_PIN(96, "PMC_PLT_CLK0"),
282 PINCTRL_PIN(97, "PMC_PLT_CLK1"),
283 PINCTRL_PIN(98, "PMC_PLT_CLK2"),
284 PINCTRL_PIN(99, "PMC_PLT_CLK3"),
285 PINCTRL_PIN(100, "PMC_PLT_CLK4"),
286 PINCTRL_PIN(101, "PMC_PLT_CLK5"),
287};
Mathias Nymana5d811b2013-06-18 14:33:02 +0300288
289static unsigned const score_pins[BYT_NGPIO_SCORE] = {
290 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
291 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
292 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
293 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
294 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
295 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
296 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
297 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
298 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
299 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
300 97, 100,
301};
302
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300303static const unsigned int byt_score_pins_map[BYT_NGPIO_SCORE] = {
304 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
305 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
306 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
307 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
308 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
309 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
310 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
311 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
312 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
313 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
314 97, 100,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300315};
316
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300317/* SCORE groups */
318static const unsigned int byt_score_uart1_pins[] = { 70, 71, 72, 73 };
319static const unsigned int byt_score_uart2_pins[] = { 74, 75, 76, 77 };
320static const struct byt_simple_func_mux byt_score_uart_mux[] = {
321 SIMPLE_FUNC("uart", 1),
322};
323
324static const unsigned int byt_score_pwm0_pins[] = { 94 };
325static const unsigned int byt_score_pwm1_pins[] = { 95 };
326static const struct byt_simple_func_mux byt_score_pwm_mux[] = {
327 SIMPLE_FUNC("pwm", 1),
328};
329
330static const unsigned int byt_score_sio_spi_pins[] = { 66, 67, 68, 69 };
331static const struct byt_simple_func_mux byt_score_spi_mux[] = {
332 SIMPLE_FUNC("spi", 1),
333};
334
335static const unsigned int byt_score_i2c5_pins[] = { 88, 89 };
336static const unsigned int byt_score_i2c6_pins[] = { 90, 91 };
337static const unsigned int byt_score_i2c4_pins[] = { 86, 87 };
338static const unsigned int byt_score_i2c3_pins[] = { 84, 85 };
339static const unsigned int byt_score_i2c2_pins[] = { 82, 83 };
340static const unsigned int byt_score_i2c1_pins[] = { 80, 81 };
341static const unsigned int byt_score_i2c0_pins[] = { 78, 79 };
342static const struct byt_simple_func_mux byt_score_i2c_mux[] = {
343 SIMPLE_FUNC("i2c", 1),
344};
345
346static const unsigned int byt_score_ssp0_pins[] = { 8, 9, 10, 11 };
347static const unsigned int byt_score_ssp1_pins[] = { 12, 13, 14, 15 };
348static const unsigned int byt_score_ssp2_pins[] = { 62, 63, 64, 65 };
349static const struct byt_simple_func_mux byt_score_ssp_mux[] = {
350 SIMPLE_FUNC("ssp", 1),
351};
352
353static const unsigned int byt_score_sdcard_pins[] = {
354 7, 33, 34, 35, 36, 37, 38, 39, 40, 41,
355};
356static const unsigned short byt_score_sdcard_mux_values[] = {
357 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
358};
359static const struct byt_mixed_func_mux byt_score_sdcard_mux[] = {
360 MIXED_FUNC("sdcard", byt_score_sdcard_mux_values),
361};
362
363static const unsigned int byt_score_sdio_pins[] = { 27, 28, 29, 30, 31, 32 };
364static const struct byt_simple_func_mux byt_score_sdio_mux[] = {
365 SIMPLE_FUNC("sdio", 1),
366};
367
368static const unsigned int byt_score_emmc_pins[] = {
369 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
370};
371static const struct byt_simple_func_mux byt_score_emmc_mux[] = {
372 SIMPLE_FUNC("emmc", 1),
373};
374
375static const unsigned int byt_score_ilb_lpc_pins[] = {
376 42, 43, 44, 45, 46, 47, 48, 49, 50,
377};
378static const struct byt_simple_func_mux byt_score_lpc_mux[] = {
379 SIMPLE_FUNC("lpc", 1),
380};
381
382static const unsigned int byt_score_sata_pins[] = { 0, 1, 2 };
383static const struct byt_simple_func_mux byt_score_sata_mux[] = {
384 SIMPLE_FUNC("sata", 1),
385};
386
387static const unsigned int byt_score_plt_clk0_pins[] = { 96 };
388static const unsigned int byt_score_plt_clk1_pins[] = { 97 };
389static const unsigned int byt_score_plt_clk2_pins[] = { 98 };
390static const unsigned int byt_score_plt_clk4_pins[] = { 99 };
391static const unsigned int byt_score_plt_clk5_pins[] = { 100 };
392static const unsigned int byt_score_plt_clk3_pins[] = { 101 };
393static const struct byt_simple_func_mux byt_score_plt_clk_mux[] = {
394 SIMPLE_FUNC("plt_clk", 1),
395};
396
397static const unsigned int byt_score_smbus_pins[] = { 51, 52, 53 };
398static const struct byt_simple_func_mux byt_score_smbus_mux[] = {
399 SIMPLE_FUNC("smbus", 1),
400};
401
402static const struct byt_pingroup byt_score_groups[] = {
403 PIN_GROUP_SIMPLE("uart1_grp",
404 byt_score_uart1_pins, byt_score_uart_mux),
405 PIN_GROUP_SIMPLE("uart2_grp",
406 byt_score_uart2_pins, byt_score_uart_mux),
407 PIN_GROUP_SIMPLE("pwm0_grp",
408 byt_score_pwm0_pins, byt_score_pwm_mux),
409 PIN_GROUP_SIMPLE("pwm1_grp",
410 byt_score_pwm1_pins, byt_score_pwm_mux),
411 PIN_GROUP_SIMPLE("ssp2_grp",
412 byt_score_ssp2_pins, byt_score_pwm_mux),
413 PIN_GROUP_SIMPLE("sio_spi_grp",
414 byt_score_sio_spi_pins, byt_score_spi_mux),
415 PIN_GROUP_SIMPLE("i2c5_grp",
416 byt_score_i2c5_pins, byt_score_i2c_mux),
417 PIN_GROUP_SIMPLE("i2c6_grp",
418 byt_score_i2c6_pins, byt_score_i2c_mux),
419 PIN_GROUP_SIMPLE("i2c4_grp",
420 byt_score_i2c4_pins, byt_score_i2c_mux),
421 PIN_GROUP_SIMPLE("i2c3_grp",
422 byt_score_i2c3_pins, byt_score_i2c_mux),
423 PIN_GROUP_SIMPLE("i2c2_grp",
424 byt_score_i2c2_pins, byt_score_i2c_mux),
425 PIN_GROUP_SIMPLE("i2c1_grp",
426 byt_score_i2c1_pins, byt_score_i2c_mux),
427 PIN_GROUP_SIMPLE("i2c0_grp",
428 byt_score_i2c0_pins, byt_score_i2c_mux),
429 PIN_GROUP_SIMPLE("ssp0_grp",
430 byt_score_ssp0_pins, byt_score_ssp_mux),
431 PIN_GROUP_SIMPLE("ssp1_grp",
432 byt_score_ssp1_pins, byt_score_ssp_mux),
433 PIN_GROUP_MIXED("sdcard_grp",
434 byt_score_sdcard_pins, byt_score_sdcard_mux),
435 PIN_GROUP_SIMPLE("sdio_grp",
436 byt_score_sdio_pins, byt_score_sdio_mux),
437 PIN_GROUP_SIMPLE("emmc_grp",
438 byt_score_emmc_pins, byt_score_emmc_mux),
439 PIN_GROUP_SIMPLE("lpc_grp",
440 byt_score_ilb_lpc_pins, byt_score_lpc_mux),
441 PIN_GROUP_SIMPLE("sata_grp",
442 byt_score_sata_pins, byt_score_sata_mux),
443 PIN_GROUP_SIMPLE("plt_clk0_grp",
444 byt_score_plt_clk0_pins, byt_score_plt_clk_mux),
445 PIN_GROUP_SIMPLE("plt_clk1_grp",
446 byt_score_plt_clk1_pins, byt_score_plt_clk_mux),
447 PIN_GROUP_SIMPLE("plt_clk2_grp",
448 byt_score_plt_clk2_pins, byt_score_plt_clk_mux),
449 PIN_GROUP_SIMPLE("plt_clk3_grp",
450 byt_score_plt_clk3_pins, byt_score_plt_clk_mux),
451 PIN_GROUP_SIMPLE("plt_clk4_grp",
452 byt_score_plt_clk4_pins, byt_score_plt_clk_mux),
453 PIN_GROUP_SIMPLE("plt_clk5_grp",
454 byt_score_plt_clk5_pins, byt_score_plt_clk_mux),
455 PIN_GROUP_SIMPLE("smbus_grp",
456 byt_score_smbus_pins, byt_score_smbus_mux),
457};
458
459static const char * const byt_score_uart_groups[] = {
460 "uart1_grp", "uart2_grp",
461};
462static const char * const byt_score_pwm_groups[] = {
463 "pwm0_grp", "pwm1_grp",
464};
465static const char * const byt_score_ssp_groups[] = {
466 "ssp0_grp", "ssp1_grp", "ssp2_grp",
467};
468static const char * const byt_score_spi_groups[] = { "sio_spi_grp" };
469static const char * const byt_score_i2c_groups[] = {
470 "i2c0_grp", "i2c1_grp", "i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp",
471 "i2c6_grp",
472};
473static const char * const byt_score_sdcard_groups[] = { "sdcard_grp" };
474static const char * const byt_score_sdio_groups[] = { "sdio_grp" };
475static const char * const byt_score_emmc_groups[] = { "emmc_grp" };
476static const char * const byt_score_lpc_groups[] = { "lpc_grp" };
477static const char * const byt_score_sata_groups[] = { "sata_grp" };
478static const char * const byt_score_plt_clk_groups[] = {
479 "plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
480 "plt_clk4_grp", "plt_clk5_grp",
481};
482static const char * const byt_score_smbus_groups[] = { "smbus_grp" };
483static const char * const byt_score_gpio_groups[] = {
484 "uart1_grp", "uart2_grp", "pwm0_grp", "pwm1_grp", "ssp0_grp",
485 "ssp1_grp", "ssp2_grp", "sio_spi_grp", "i2c0_grp", "i2c1_grp",
486 "i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp", "i2c6_grp",
487 "sdcard_grp", "sdio_grp", "emmc_grp", "lpc_grp", "sata_grp",
488 "plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
489 "plt_clk4_grp", "plt_clk5_grp", "smbus_grp",
490
491};
492
493static const struct byt_function byt_score_functions[] = {
494 FUNCTION("uart", byt_score_uart_groups),
495 FUNCTION("pwm", byt_score_pwm_groups),
496 FUNCTION("ssp", byt_score_ssp_groups),
497 FUNCTION("spi", byt_score_spi_groups),
498 FUNCTION("i2c", byt_score_i2c_groups),
499 FUNCTION("sdcard", byt_score_sdcard_groups),
500 FUNCTION("sdio", byt_score_sdio_groups),
501 FUNCTION("emmc", byt_score_emmc_groups),
502 FUNCTION("lpc", byt_score_lpc_groups),
503 FUNCTION("sata", byt_score_sata_groups),
504 FUNCTION("plt_clk", byt_score_plt_clk_groups),
505 FUNCTION("smbus", byt_score_smbus_groups),
506 FUNCTION("gpio", byt_score_gpio_groups),
507};
508
509static const struct byt_community byt_score_communities[] = {
510 COMMUNITY(0, BYT_NGPIO_SCORE, byt_score_pins_map),
511};
512
513static const struct byt_pinctrl_soc_data byt_score_soc_data = {
514 .uid = BYT_SCORE_ACPI_UID,
515 .pins = byt_score_pins,
516 .npins = ARRAY_SIZE(byt_score_pins),
517 .groups = byt_score_groups,
518 .ngroups = ARRAY_SIZE(byt_score_groups),
519 .functions = byt_score_functions,
520 .nfunctions = ARRAY_SIZE(byt_score_functions),
521 .communities = byt_score_communities,
522 .ncommunities = ARRAY_SIZE(byt_score_communities),
523};
524
525/* SUS pins, aka GPIOS_<pin_no> or GPIO_S5[<pin_no>] */
526static const struct pinctrl_pin_desc byt_sus_pins[] = {
527 PINCTRL_PIN(0, "GPIO_S50"),
528 PINCTRL_PIN(1, "GPIO_S51"),
529 PINCTRL_PIN(2, "GPIO_S52"),
530 PINCTRL_PIN(3, "GPIO_S53"),
531 PINCTRL_PIN(4, "GPIO_S54"),
532 PINCTRL_PIN(5, "GPIO_S55"),
533 PINCTRL_PIN(6, "GPIO_S56"),
534 PINCTRL_PIN(7, "GPIO_S57"),
535 PINCTRL_PIN(8, "GPIO_S58"),
536 PINCTRL_PIN(9, "GPIO_S59"),
537 PINCTRL_PIN(10, "GPIO_S510"),
538 PINCTRL_PIN(11, "PMC_SUSPWRDNACK"),
539 PINCTRL_PIN(12, "PMC_SUSCLK0"),
540 PINCTRL_PIN(13, "GPIO_S513"),
541 PINCTRL_PIN(14, "USB_ULPI_RST"),
542 PINCTRL_PIN(15, "PMC_WAKE_PCIE0#"),
543 PINCTRL_PIN(16, "PMC_PWRBTN"),
544 PINCTRL_PIN(17, "GPIO_S517"),
545 PINCTRL_PIN(18, "PMC_SUS_STAT"),
546 PINCTRL_PIN(19, "USB_OC0"),
547 PINCTRL_PIN(20, "USB_OC1"),
548 PINCTRL_PIN(21, "PCU_SPI_CS1"),
549 PINCTRL_PIN(22, "GPIO_S522"),
550 PINCTRL_PIN(23, "GPIO_S523"),
551 PINCTRL_PIN(24, "GPIO_S524"),
552 PINCTRL_PIN(25, "GPIO_S525"),
553 PINCTRL_PIN(26, "GPIO_S526"),
554 PINCTRL_PIN(27, "GPIO_S527"),
555 PINCTRL_PIN(28, "GPIO_S528"),
556 PINCTRL_PIN(29, "GPIO_S529"),
557 PINCTRL_PIN(30, "GPIO_S530"),
558 PINCTRL_PIN(31, "USB_ULPI_CLK"),
559 PINCTRL_PIN(32, "USB_ULPI_DATA0"),
560 PINCTRL_PIN(33, "USB_ULPI_DATA1"),
561 PINCTRL_PIN(34, "USB_ULPI_DATA2"),
562 PINCTRL_PIN(35, "USB_ULPI_DATA3"),
563 PINCTRL_PIN(36, "USB_ULPI_DATA4"),
564 PINCTRL_PIN(37, "USB_ULPI_DATA5"),
565 PINCTRL_PIN(38, "USB_ULPI_DATA6"),
566 PINCTRL_PIN(39, "USB_ULPI_DATA7"),
567 PINCTRL_PIN(40, "USB_ULPI_DIR"),
568 PINCTRL_PIN(41, "USB_ULPI_NXT"),
569 PINCTRL_PIN(42, "USB_ULPI_STP"),
570 PINCTRL_PIN(43, "USB_ULPI_REFCLK"),
571};
572
573static const unsigned int sus_pins[BYT_NGPIO_SUS] = {
Mathias Nymana5d811b2013-06-18 14:33:02 +0300574 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
575 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
576 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
577 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
578 52, 53, 59, 40,
579};
580
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300581static const unsigned int byt_sus_pins_map[BYT_NGPIO_SUS] = {
582 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
583 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
584 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
585 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
586 52, 53, 59, 40,
587};
588
589static const unsigned int byt_sus_usb_over_current_pins[] = { 19, 20 };
590static const struct byt_simple_func_mux byt_sus_usb_oc_mux[] = {
591 SIMPLE_FUNC("usb", 0),
592 SIMPLE_FUNC("gpio", 1),
593};
594
595static const unsigned int byt_sus_usb_ulpi_pins[] = {
596 14, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
597};
598static const unsigned short byt_sus_usb_ulpi_mode_values[] = {
599 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
600};
601static const unsigned short byt_sus_usb_ulpi_gpio_mode_values[] = {
602 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
603};
604static const struct byt_mixed_func_mux byt_sus_usb_ulpi_mux[] = {
605 MIXED_FUNC("usb", byt_sus_usb_ulpi_mode_values),
606 MIXED_FUNC("gpio", byt_sus_usb_ulpi_gpio_mode_values),
607};
608
609static const unsigned int byt_sus_pcu_spi_pins[] = { 21 };
610static const struct byt_simple_func_mux byt_sus_pcu_spi_mux[] = {
611 SIMPLE_FUNC("spi", 0),
612 SIMPLE_FUNC("gpio", 1),
613};
614
615static const struct byt_pingroup byt_sus_groups[] = {
616 PIN_GROUP_SIMPLE("usb_oc_grp",
617 byt_sus_usb_over_current_pins, byt_sus_usb_oc_mux),
618 PIN_GROUP_MIXED("usb_ulpi_grp",
619 byt_sus_usb_ulpi_pins, byt_sus_usb_ulpi_mux),
620 PIN_GROUP_SIMPLE("pcu_spi_grp",
621 byt_sus_pcu_spi_pins, byt_sus_pcu_spi_mux),
622};
623
624static const char * const byt_sus_usb_groups[] = {
625 "usb_oc_grp", "usb_ulpi_grp",
626};
627static const char * const byt_sus_spi_groups[] = { "pcu_spi_grp" };
628static const char * const byt_sus_gpio_groups[] = {
629 "usb_oc_grp", "usb_ulpi_grp", "pcu_spi_grp",
630};
631
632static const struct byt_function byt_sus_functions[] = {
633 FUNCTION("usb", byt_sus_usb_groups),
634 FUNCTION("spi", byt_sus_spi_groups),
635 FUNCTION("gpio", byt_sus_gpio_groups),
636};
637
638static const struct byt_community byt_sus_communities[] = {
639 COMMUNITY(0, BYT_NGPIO_SUS, byt_sus_pins_map),
640};
641
642static const struct byt_pinctrl_soc_data byt_sus_soc_data = {
643 .uid = BYT_SUS_ACPI_UID,
644 .pins = byt_sus_pins,
645 .npins = ARRAY_SIZE(byt_sus_pins),
646 .groups = byt_sus_groups,
647 .ngroups = ARRAY_SIZE(byt_sus_groups),
648 .functions = byt_sus_functions,
649 .nfunctions = ARRAY_SIZE(byt_sus_functions),
650 .communities = byt_sus_communities,
651 .ncommunities = ARRAY_SIZE(byt_sus_communities),
652};
653
654static const struct pinctrl_pin_desc byt_ncore_pins[] = {
655 PINCTRL_PIN(0, "GPIO_NCORE0"),
656 PINCTRL_PIN(1, "GPIO_NCORE1"),
657 PINCTRL_PIN(2, "GPIO_NCORE2"),
658 PINCTRL_PIN(3, "GPIO_NCORE3"),
659 PINCTRL_PIN(4, "GPIO_NCORE4"),
660 PINCTRL_PIN(5, "GPIO_NCORE5"),
661 PINCTRL_PIN(6, "GPIO_NCORE6"),
662 PINCTRL_PIN(7, "GPIO_NCORE7"),
663 PINCTRL_PIN(8, "GPIO_NCORE8"),
664 PINCTRL_PIN(9, "GPIO_NCORE9"),
665 PINCTRL_PIN(10, "GPIO_NCORE10"),
666 PINCTRL_PIN(11, "GPIO_NCORE11"),
667 PINCTRL_PIN(12, "GPIO_NCORE12"),
668 PINCTRL_PIN(13, "GPIO_NCORE13"),
669 PINCTRL_PIN(14, "GPIO_NCORE14"),
670 PINCTRL_PIN(15, "GPIO_NCORE15"),
671 PINCTRL_PIN(16, "GPIO_NCORE16"),
672 PINCTRL_PIN(17, "GPIO_NCORE17"),
673 PINCTRL_PIN(18, "GPIO_NCORE18"),
674 PINCTRL_PIN(19, "GPIO_NCORE19"),
675 PINCTRL_PIN(20, "GPIO_NCORE20"),
676 PINCTRL_PIN(21, "GPIO_NCORE21"),
677 PINCTRL_PIN(22, "GPIO_NCORE22"),
678 PINCTRL_PIN(23, "GPIO_NCORE23"),
679 PINCTRL_PIN(24, "GPIO_NCORE24"),
680 PINCTRL_PIN(25, "GPIO_NCORE25"),
681 PINCTRL_PIN(26, "GPIO_NCORE26"),
682 PINCTRL_PIN(27, "GPIO_NCORE27"),
683};
684
685static unsigned const byt_ncore_pins_map[BYT_NGPIO_NCORE] = {
686 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
687 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
688 3, 6, 10, 13, 2, 5, 9, 7,
689};
690
691static const struct byt_community byt_ncore_communities[] = {
692 COMMUNITY(0, BYT_NGPIO_NCORE, byt_ncore_pins_map),
693};
694
695static const struct byt_pinctrl_soc_data byt_ncore_soc_data = {
696 .uid = BYT_NCORE_ACPI_UID,
697 .pins = byt_ncore_pins,
698 .npins = ARRAY_SIZE(byt_ncore_pins),
699 .communities = byt_ncore_communities,
700 .ncommunities = ARRAY_SIZE(byt_ncore_communities),
701};
702
703static unsigned const ncore_pins[BYT_NGPIO_NCORE] = {
704 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
705 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
706 3, 6, 10, 13, 2, 5, 9, 7,
707};
708
Mathias Nymana5d811b2013-06-18 14:33:02 +0300709static struct pinctrl_gpio_range byt_ranges[] = {
710 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800711 .name = BYT_SCORE_ACPI_UID, /* match with acpi _UID in probe */
Mathias Nymana5d811b2013-06-18 14:33:02 +0300712 .npins = BYT_NGPIO_SCORE,
713 .pins = score_pins,
714 },
715 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800716 .name = BYT_NCORE_ACPI_UID,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300717 .npins = BYT_NGPIO_NCORE,
718 .pins = ncore_pins,
719 },
720 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800721 .name = BYT_SUS_ACPI_UID,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300722 .npins = BYT_NGPIO_SUS,
723 .pins = sus_pins,
724 },
725 {
726 },
727};
728
729struct byt_gpio {
730 struct gpio_chip chip;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300731 struct platform_device *pdev;
Mika Westerberg78e1c892015-08-17 16:03:17 +0300732 raw_spinlock_t lock;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300733 void __iomem *reg_base;
734 struct pinctrl_gpio_range *range;
Mika Westerbergfcc18de2015-02-23 14:53:13 +0200735 struct byt_gpio_pin_context *saved_context;
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300736 const struct byt_pinctrl_soc_data *soc_data;
737 struct byt_community *communities_copy;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300738};
739
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300740static const struct byt_pinctrl_soc_data *byt_soc_data[] = {
741 &byt_score_soc_data,
742 &byt_sus_soc_data,
743 &byt_ncore_soc_data,
744 NULL,
745};
746
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300747static struct byt_community *byt_get_community(struct byt_gpio *vg,
748 unsigned int pin)
Mathias Nymana5d811b2013-06-18 14:33:02 +0300749{
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300750 struct byt_community *comm;
751 int i;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300752
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300753 for (i = 0; i < vg->soc_data->ncommunities; i++) {
754 comm = vg->communities_copy + i;
755 if (pin < comm->pin_base + comm->npins && pin >= comm->pin_base)
756 return comm;
757 }
758
759 return NULL;
760}
761
762static void __iomem *byt_gpio_reg(struct byt_gpio *vg, unsigned int offset,
763 int reg)
764{
765 struct byt_community *comm = byt_get_community(vg, offset);
766 u32 reg_offset = 0;
767
768 if (!comm)
769 return NULL;
770
771 offset -= comm->pin_base;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300772 if (reg == BYT_INT_STAT_REG)
773 reg_offset = (offset / 32) * 4;
774 else
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300775 reg_offset = comm->pad_map[offset] * 16;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300776
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300777 return comm->reg_base + reg_offset + reg;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300778}
779
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300780static int byt_get_groups_count(struct pinctrl_dev *pctldev)
Mika Westerberg95f09722015-02-23 14:53:11 +0200781{
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300782 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
783
784 return vg->soc_data->ngroups;
785}
786
787static const char *byt_get_group_name(struct pinctrl_dev *pctldev,
788 unsigned int selector)
789{
790 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
791
792 return vg->soc_data->groups[selector].name;
793}
794
795static int byt_get_group_pins(struct pinctrl_dev *pctldev,
796 unsigned int selector,
797 const unsigned int **pins,
798 unsigned int *num_pins)
799{
800 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
801
802 *pins = vg->soc_data->groups[selector].pins;
803 *num_pins = vg->soc_data->groups[selector].npins;
804
805 return 0;
806}
807
808static const struct pinctrl_ops byt_pinctrl_ops = {
809 .get_groups_count = byt_get_groups_count,
810 .get_group_name = byt_get_group_name,
811 .get_group_pins = byt_get_group_pins,
812};
813
814static int byt_get_functions_count(struct pinctrl_dev *pctldev)
815{
816 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
817
818 return vg->soc_data->nfunctions;
819}
820
821static const char *byt_get_function_name(struct pinctrl_dev *pctldev,
822 unsigned int selector)
823{
824 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
825
826 return vg->soc_data->functions[selector].name;
827}
828
829static int byt_get_function_groups(struct pinctrl_dev *pctldev,
830 unsigned int selector,
831 const char * const **groups,
832 unsigned int *num_groups)
833{
834 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
835
836 *groups = vg->soc_data->functions[selector].groups;
837 *num_groups = vg->soc_data->functions[selector].ngroups;
838
839 return 0;
840}
841
842static int byt_get_group_simple_mux(const struct byt_pingroup group,
843 const char *func_name,
844 unsigned short *func)
845{
846 int i;
847
848 for (i = 0; i < group.nfuncs; i++) {
849 if (!strcmp(group.simple_funcs[i].name, func_name)) {
850 *func = group.simple_funcs[i].func;
851 return 0;
852 }
853 }
854
855 return 1;
856}
857
858static int byt_get_group_mixed_mux(const struct byt_pingroup group,
859 const char *func_name,
860 const unsigned short **func)
861{
862 int i;
863
864 for (i = 0; i < group.nfuncs; i++) {
865 if (!strcmp(group.mixed_funcs[i].name, func_name)) {
866 *func = group.mixed_funcs[i].func_values;
867 return 0;
868 }
869 }
870
871 return 1;
872}
873
874static void byt_set_group_simple_mux(struct byt_gpio *vg,
875 const struct byt_pingroup group,
876 unsigned short func)
877{
878 unsigned long flags;
879 int i;
880
881 raw_spin_lock_irqsave(&vg->lock, flags);
882
883 for (i = 0; i < group.npins; i++) {
884 void __iomem *padcfg0;
885 u32 value;
886
887 padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
888 if (!padcfg0) {
889 dev_warn(&vg->pdev->dev,
890 "Group %s, pin %i not muxed (no padcfg0)\n",
891 group.name, i);
892 continue;
893 }
894
895 value = readl(padcfg0);
896 value &= ~BYT_PIN_MUX;
897 value |= func;
898 writel(value, padcfg0);
899 }
900
901 raw_spin_unlock_irqrestore(&vg->lock, flags);
902}
903
904static void byt_set_group_mixed_mux(struct byt_gpio *vg,
905 const struct byt_pingroup group,
906 const unsigned short *func)
907{
908 unsigned long flags;
909 int i;
910
911 raw_spin_lock_irqsave(&vg->lock, flags);
912
913 for (i = 0; i < group.npins; i++) {
914 void __iomem *padcfg0;
915 u32 value;
916
917 padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
918 if (!padcfg0) {
919 dev_warn(&vg->pdev->dev,
920 "Group %s, pin %i not muxed (no padcfg0)\n",
921 group.name, i);
922 continue;
923 }
924
925 value = readl(padcfg0);
926 value &= ~BYT_PIN_MUX;
927 value |= func[i];
928 writel(value, padcfg0);
929 }
930
931 raw_spin_unlock_irqrestore(&vg->lock, flags);
932}
933
934static int byt_set_mux(struct pinctrl_dev *pctldev, unsigned int func_selector,
935 unsigned int group_selector)
936{
937 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
938 const struct byt_function func = vg->soc_data->functions[func_selector];
939 const struct byt_pingroup group = vg->soc_data->groups[group_selector];
940 const unsigned short *mixed_func;
941 unsigned short simple_func;
942 int ret = 1;
943
944 if (group.has_simple_funcs)
945 ret = byt_get_group_simple_mux(group, func.name, &simple_func);
946 else
947 ret = byt_get_group_mixed_mux(group, func.name, &mixed_func);
948
949 if (ret)
950 byt_set_group_simple_mux(vg, group, BYT_DEFAULT_GPIO_MUX);
951 else if (group.has_simple_funcs)
952 byt_set_group_simple_mux(vg, group, simple_func);
953 else
954 byt_set_group_mixed_mux(vg, group, mixed_func);
955
956 return 0;
957}
958
959static u32 byt_get_gpio_mux(struct byt_gpio *vg, unsigned offset)
960{
961 /* SCORE pin 92-93 */
962 if (!strcmp(vg->soc_data->uid, BYT_SCORE_ACPI_UID) &&
963 offset >= 92 && offset <= 93)
964 return 1;
965
966 /* SUS pin 11-21 */
967 if (!strcmp(vg->soc_data->uid, BYT_SUS_ACPI_UID) &&
968 offset >= 11 && offset <= 21)
969 return 1;
970
971 return 0;
972}
973
974static void byt_gpio_clear_triggering(struct byt_gpio *vg, unsigned int offset)
975{
976 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mika Westerberg95f09722015-02-23 14:53:11 +0200977 unsigned long flags;
978 u32 value;
979
Mika Westerberg78e1c892015-08-17 16:03:17 +0300980 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg95f09722015-02-23 14:53:11 +0200981 value = readl(reg);
982 value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
983 writel(value, reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +0300984 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg95f09722015-02-23 14:53:11 +0200985}
986
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300987static int byt_gpio_request_enable(struct pinctrl_dev *pctl_dev,
988 struct pinctrl_gpio_range *range,
989 unsigned int offset)
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800990{
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300991 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
992 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mika Westerbergf8323b62015-02-23 14:53:10 +0200993 u32 value, gpio_mux;
Mika Westerberg39ce8152015-08-04 15:03:14 +0300994 unsigned long flags;
995
Mika Westerberg78e1c892015-08-17 16:03:17 +0300996 raw_spin_lock_irqsave(&vg->lock, flags);
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800997
998 /*
999 * In most cases, func pin mux 000 means GPIO function.
1000 * But, some pins may have func pin mux 001 represents
Mika Westerbergf8323b62015-02-23 14:53:10 +02001001 * GPIO function.
1002 *
1003 * Because there are devices out there where some pins were not
1004 * configured correctly we allow changing the mux value from
1005 * request (but print out warning about that).
Chew, Kean Ho42bd0072014-03-06 21:59:49 +08001006 */
1007 value = readl(reg) & BYT_PIN_MUX;
Mika Westerbergf8323b62015-02-23 14:53:10 +02001008 gpio_mux = byt_get_gpio_mux(vg, offset);
1009 if (WARN_ON(gpio_mux != value)) {
Mika Westerbergf8323b62015-02-23 14:53:10 +02001010 value = readl(reg) & ~BYT_PIN_MUX;
1011 value |= gpio_mux;
1012 writel(value, reg);
Mika Westerbergf8323b62015-02-23 14:53:10 +02001013
1014 dev_warn(&vg->pdev->dev,
1015 "pin %u forcibly re-configured as GPIO\n", offset);
Chew, Kean Ho42bd0072014-03-06 21:59:49 +08001016 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001017
Mika Westerberg78e1c892015-08-17 16:03:17 +03001018 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001019
Mathias Nymana5d811b2013-06-18 14:33:02 +03001020 pm_runtime_get(&vg->pdev->dev);
1021
1022 return 0;
1023}
1024
1025static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
1026{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001027 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001028
Mika Westerberg95f09722015-02-23 14:53:11 +02001029 byt_gpio_clear_triggering(vg, offset);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001030 pm_runtime_put(&vg->pdev->dev);
1031}
1032
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001033static void byt_gpio_disable_free(struct pinctrl_dev *pctl_dev,
1034 struct pinctrl_gpio_range *range,
1035 unsigned int offset)
1036{
1037 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1038
1039 byt_gpio_clear_triggering(vg, offset);
1040 pm_runtime_put(&vg->pdev->dev);
1041}
1042
1043static int byt_gpio_set_direction(struct pinctrl_dev *pctl_dev,
1044 struct pinctrl_gpio_range *range,
1045 unsigned int offset,
1046 bool input)
1047{
1048 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1049 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1050 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1051 unsigned long flags;
1052 u32 value;
1053
1054 raw_spin_lock_irqsave(&vg->lock, flags);
1055
1056 value = readl(val_reg);
1057 value &= ~BYT_DIR_MASK;
1058 if (input)
1059 value |= BYT_OUTPUT_EN;
1060 else
1061 /*
1062 * Before making any direction modifications, do a check if gpio
1063 * is set for direct IRQ. On baytrail, setting GPIO to output
1064 * does not make sense, so let's at least warn the caller before
1065 * they shoot themselves in the foot.
1066 */
1067 WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
1068 "Potential Error: Setting GPIO with direct_irq_en to output");
1069 writel(value, val_reg);
1070
1071 raw_spin_unlock_irqrestore(&vg->lock, flags);
1072
1073 return 0;
1074}
1075
1076static const struct pinmux_ops byt_pinmux_ops = {
1077 .get_functions_count = byt_get_functions_count,
1078 .get_function_name = byt_get_function_name,
1079 .get_function_groups = byt_get_function_groups,
1080 .set_mux = byt_set_mux,
1081 .gpio_request_enable = byt_gpio_request_enable,
1082 .gpio_disable_free = byt_gpio_disable_free,
1083 .gpio_set_direction = byt_gpio_set_direction,
1084};
1085
1086static int byt_gpio_request(struct gpio_chip *chip, unsigned int offset)
1087{
1088 struct byt_gpio *vg = gpiochip_get_data(chip);
1089 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1090 u32 value, gpio_mux;
1091 unsigned long flags;
1092
1093 raw_spin_lock_irqsave(&vg->lock, flags);
1094
1095 /*
1096 * In most cases, func pin mux 000 means GPIO function.
1097 * But, some pins may have func pin mux 001 represents
1098 * GPIO function.
1099 *
1100 * Because there are devices out there where some pins were not
1101 * configured correctly we allow changing the mux value from
1102 * request (but print out warning about that).
1103 */
1104 value = readl(reg) & BYT_PIN_MUX;
1105 gpio_mux = byt_get_gpio_mux(vg, offset);
1106 if (WARN_ON(gpio_mux != value)) {
1107 value = readl(reg) & ~BYT_PIN_MUX;
1108 value |= gpio_mux;
1109 writel(value, reg);
1110
1111 dev_warn(&vg->pdev->dev,
1112 "pin %u forcibly re-configured as GPIO\n", offset);
1113 }
1114
1115 raw_spin_unlock_irqrestore(&vg->lock, flags);
1116
1117 pm_runtime_get(&vg->pdev->dev);
1118
1119 return 0;
1120}
1121
Mathias Nymana5d811b2013-06-18 14:33:02 +03001122static int byt_irq_type(struct irq_data *d, unsigned type)
1123{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001124 struct byt_gpio *vg = gpiochip_get_data(irq_data_get_irq_chip_data(d));
Mathias Nymana5d811b2013-06-18 14:33:02 +03001125 u32 offset = irqd_to_hwirq(d);
1126 u32 value;
1127 unsigned long flags;
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001128 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001129
1130 if (offset >= vg->chip.ngpio)
1131 return -EINVAL;
1132
Mika Westerberg78e1c892015-08-17 16:03:17 +03001133 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001134 value = readl(reg);
1135
Loic Poulain3a71c052014-09-26 16:14:51 +02001136 WARN(value & BYT_DIRECT_IRQ_EN,
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001137 "Bad pad config for io mode, force direct_irq_en bit clearing");
Loic Poulain3a71c052014-09-26 16:14:51 +02001138
Mathias Nymana5d811b2013-06-18 14:33:02 +03001139 /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
1140 * are used to indicate high and low level triggering
1141 */
Loic Poulain3a71c052014-09-26 16:14:51 +02001142 value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
1143 BYT_TRIG_LVL);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001144
Mathias Nymana5d811b2013-06-18 14:33:02 +03001145 writel(value, reg);
1146
Mika Westerberg31e43292015-02-23 14:53:12 +02001147 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerf3a085b2015-06-23 15:52:44 +02001148 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg31e43292015-02-23 14:53:12 +02001149 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerf3a085b2015-06-23 15:52:44 +02001150 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg31e43292015-02-23 14:53:12 +02001151
Mika Westerberg78e1c892015-08-17 16:03:17 +03001152 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001153
1154 return 0;
1155}
1156
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001157static void byt_get_pull_strength(u32 reg, u16 *strength)
1158{
1159 switch (reg & BYT_PULL_STR_MASK) {
1160 case BYT_PULL_STR_2K:
1161 *strength = 2000;
1162 break;
1163 case BYT_PULL_STR_10K:
1164 *strength = 10000;
1165 break;
1166 case BYT_PULL_STR_20K:
1167 *strength = 20000;
1168 break;
1169 case BYT_PULL_STR_40K:
1170 *strength = 40000;
1171 break;
1172 }
1173}
1174
1175static int byt_set_pull_strength(u32 *reg, u16 strength)
1176{
1177 *reg &= ~BYT_PULL_STR_MASK;
1178
1179 switch (strength) {
1180 case 2000:
1181 *reg |= BYT_PULL_STR_2K;
1182 break;
1183 case 10000:
1184 *reg |= BYT_PULL_STR_10K;
1185 break;
1186 case 20000:
1187 *reg |= BYT_PULL_STR_20K;
1188 break;
1189 case 40000:
1190 *reg |= BYT_PULL_STR_40K;
1191 break;
1192 default:
1193 return -EINVAL;
1194 }
1195
1196 return 0;
1197}
1198
1199static int byt_pin_config_get(struct pinctrl_dev *pctl_dev, unsigned int offset,
1200 unsigned long *config)
1201{
1202 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1203 enum pin_config_param param = pinconf_to_config_param(*config);
1204 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1205 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1206 unsigned long flags;
1207 u32 conf, pull, val;
1208 u16 arg = 0;
1209
1210 raw_spin_lock_irqsave(&vg->lock, flags);
1211 conf = readl(conf_reg);
1212 pull = conf & BYT_PULL_ASSIGN_MASK;
1213 val = readl(val_reg);
1214 raw_spin_unlock_irqrestore(&vg->lock, flags);
1215
1216 switch (param) {
1217 case PIN_CONFIG_BIAS_DISABLE:
1218 if (pull)
1219 return -EINVAL;
1220 break;
1221 case PIN_CONFIG_BIAS_PULL_DOWN:
1222 /* Pull assignment is only applicable in input mode */
1223 if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_DOWN)
1224 return -EINVAL;
1225
1226 byt_get_pull_strength(conf, &arg);
1227
1228 break;
1229 case PIN_CONFIG_BIAS_PULL_UP:
1230 /* Pull assignment is only applicable in input mode */
1231 if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_UP)
1232 return -EINVAL;
1233
1234 byt_get_pull_strength(conf, &arg);
1235
1236 break;
1237 default:
1238 return -ENOTSUPP;
1239 }
1240
1241 *config = pinconf_to_config_packed(param, arg);
1242
1243 return 0;
1244}
1245
1246static int byt_pin_config_set(struct pinctrl_dev *pctl_dev,
1247 unsigned int offset,
1248 unsigned long *configs,
1249 unsigned int num_configs)
1250{
1251 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1252 unsigned int param, arg;
1253 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1254 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1255 unsigned long flags;
1256 u32 conf, val;
1257 int i, ret = 0;
1258
1259 raw_spin_lock_irqsave(&vg->lock, flags);
1260
1261 conf = readl(conf_reg);
1262 val = readl(val_reg);
1263
1264 for (i = 0; i < num_configs; i++) {
1265 param = pinconf_to_config_param(configs[i]);
1266 arg = pinconf_to_config_argument(configs[i]);
1267
1268 switch (param) {
1269 case PIN_CONFIG_BIAS_DISABLE:
1270 conf &= ~BYT_PULL_ASSIGN_MASK;
1271 break;
1272 case PIN_CONFIG_BIAS_PULL_DOWN:
1273 /* Set default strength value in case none is given */
1274 if (arg == 1)
1275 arg = 2000;
1276
1277 /*
1278 * Pull assignment is only applicable in input mode. If
1279 * chip is not in input mode, set it and warn about it.
1280 */
1281 if (val & BYT_INPUT_EN) {
1282 val &= ~BYT_INPUT_EN;
1283 writel(val, val_reg);
1284 dev_warn(&vg->pdev->dev,
1285 "pin %u forcibly set to input mode\n",
1286 offset);
1287 }
1288
1289 conf &= ~BYT_PULL_ASSIGN_MASK;
1290 conf |= BYT_PULL_ASSIGN_DOWN;
1291 ret = byt_set_pull_strength(&conf, arg);
1292
1293 break;
1294 case PIN_CONFIG_BIAS_PULL_UP:
1295 /* Set default strength value in case none is given */
1296 if (arg == 1)
1297 arg = 2000;
1298
1299 /*
1300 * Pull assignment is only applicable in input mode. If
1301 * chip is not in input mode, set it and warn about it.
1302 */
1303 if (val & BYT_INPUT_EN) {
1304 val &= ~BYT_INPUT_EN;
1305 writel(val, val_reg);
1306 dev_warn(&vg->pdev->dev,
1307 "pin %u forcibly set to input mode\n",
1308 offset);
1309 }
1310
1311 conf &= ~BYT_PULL_ASSIGN_MASK;
1312 conf |= BYT_PULL_ASSIGN_UP;
1313 ret = byt_set_pull_strength(&conf, arg);
1314
1315 break;
1316 default:
1317 ret = -ENOTSUPP;
1318 }
1319
1320 if (ret)
1321 break;
1322 }
1323
1324 if (!ret)
1325 writel(conf, conf_reg);
1326
1327 raw_spin_unlock_irqrestore(&vg->lock, flags);
1328
1329 return ret;
1330}
1331
1332static const struct pinconf_ops byt_pinconf_ops = {
1333 .is_generic = true,
1334 .pin_config_get = byt_pin_config_get,
1335 .pin_config_set = byt_pin_config_set,
1336};
1337
1338static const struct pinctrl_desc byt_pinctrl_desc = {
1339 .pctlops = &byt_pinctrl_ops,
1340 .pmxops = &byt_pinmux_ops,
1341 .confops = &byt_pinconf_ops,
1342 .owner = THIS_MODULE,
1343};
1344
Mathias Nymana5d811b2013-06-18 14:33:02 +03001345static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
1346{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001347 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001348 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001349 unsigned long flags;
1350 u32 val;
1351
Mika Westerberg78e1c892015-08-17 16:03:17 +03001352 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001353 val = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001354 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001355
Linus Walleij3bde8772015-12-21 16:17:20 +01001356 return !!(val & BYT_LEVEL);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001357}
1358
1359static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1360{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001361 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001362 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001363 unsigned long flags;
1364 u32 old_val;
1365
Mika Westerberg78e1c892015-08-17 16:03:17 +03001366 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001367
1368 old_val = readl(reg);
1369
1370 if (value)
1371 writel(old_val | BYT_LEVEL, reg);
1372 else
1373 writel(old_val & ~BYT_LEVEL, reg);
1374
Mika Westerberg78e1c892015-08-17 16:03:17 +03001375 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001376}
1377
1378static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1379{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001380 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001381 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001382 unsigned long flags;
1383 u32 value;
1384
Mika Westerberg78e1c892015-08-17 16:03:17 +03001385 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001386
1387 value = readl(reg) | BYT_DIR_MASK;
Andy Shevchenko496940c2013-07-10 14:55:40 +03001388 value &= ~BYT_INPUT_EN; /* active low */
Mathias Nymana5d811b2013-06-18 14:33:02 +03001389 writel(value, reg);
1390
Mika Westerberg78e1c892015-08-17 16:03:17 +03001391 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001392
1393 return 0;
1394}
1395
1396static int byt_gpio_direction_output(struct gpio_chip *chip,
1397 unsigned gpio, int value)
1398{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001399 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001400 void __iomem *conf_reg = byt_gpio_reg(vg, gpio, BYT_CONF0_REG);
1401 void __iomem *reg = byt_gpio_reg(vg, gpio, BYT_VAL_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001402 unsigned long flags;
1403 u32 reg_val;
1404
Mika Westerberg78e1c892015-08-17 16:03:17 +03001405 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001406
Eric Ernstff998352014-06-12 11:06:20 -07001407 /*
1408 * Before making any direction modifications, do a check if gpio
1409 * is set for direct IRQ. On baytrail, setting GPIO to output does
1410 * not make sense, so let's at least warn the caller before they shoot
1411 * themselves in the foot.
1412 */
1413 WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
1414 "Potential Error: Setting GPIO with direct_irq_en to output");
1415
Andy Shevchenko496940c2013-07-10 14:55:40 +03001416 reg_val = readl(reg) | BYT_DIR_MASK;
David Cohend90c3382014-10-14 10:54:37 -07001417 reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);
Andy Shevchenko496940c2013-07-10 14:55:40 +03001418
1419 if (value)
1420 writel(reg_val | BYT_LEVEL, reg);
1421 else
1422 writel(reg_val & ~BYT_LEVEL, reg);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001423
Mika Westerberg78e1c892015-08-17 16:03:17 +03001424 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001425
1426 return 0;
1427}
1428
1429static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1430{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001431 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001432 int i;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001433 u32 conf0, val, offs;
1434
Mathias Nymana5d811b2013-06-18 14:33:02 +03001435 for (i = 0; i < vg->chip.ngpio; i++) {
Mika Westerberg3ff95882014-05-16 12:18:29 +03001436 const char *pull_str = NULL;
1437 const char *pull = NULL;
Mika Westerberg78e1c892015-08-17 16:03:17 +03001438 unsigned long flags;
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001439 const char *label;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001440 offs = vg->range->pins[i] * 16;
Mika Westerberg78e1c892015-08-17 16:03:17 +03001441
1442 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001443 conf0 = readl(vg->reg_base + offs + BYT_CONF0_REG);
1444 val = readl(vg->reg_base + offs + BYT_VAL_REG);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001445 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001446
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001447 label = gpiochip_is_requested(chip, i);
1448 if (!label)
1449 label = "Unrequested";
1450
Mika Westerberg3ff95882014-05-16 12:18:29 +03001451 switch (conf0 & BYT_PULL_ASSIGN_MASK) {
1452 case BYT_PULL_ASSIGN_UP:
1453 pull = "up";
1454 break;
1455 case BYT_PULL_ASSIGN_DOWN:
1456 pull = "down";
1457 break;
1458 }
1459
1460 switch (conf0 & BYT_PULL_STR_MASK) {
1461 case BYT_PULL_STR_2K:
1462 pull_str = "2k";
1463 break;
1464 case BYT_PULL_STR_10K:
1465 pull_str = "10k";
1466 break;
1467 case BYT_PULL_STR_20K:
1468 pull_str = "20k";
1469 break;
1470 case BYT_PULL_STR_40K:
1471 pull_str = "40k";
1472 break;
1473 }
1474
Mathias Nymana5d811b2013-06-18 14:33:02 +03001475 seq_printf(s,
Mika Westerberg3ff95882014-05-16 12:18:29 +03001476 " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
Mathias Nymana5d811b2013-06-18 14:33:02 +03001477 i,
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001478 label,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001479 val & BYT_INPUT_EN ? " " : "in",
1480 val & BYT_OUTPUT_EN ? " " : "out",
1481 val & BYT_LEVEL ? "hi" : "lo",
1482 vg->range->pins[i], offs,
1483 conf0 & 0x7,
Mika Westerberg3ff95882014-05-16 12:18:29 +03001484 conf0 & BYT_TRIG_NEG ? " fall" : " ",
1485 conf0 & BYT_TRIG_POS ? " rise" : " ",
1486 conf0 & BYT_TRIG_LVL ? " level" : " ");
1487
1488 if (pull && pull_str)
1489 seq_printf(s, " %-4s %-3s", pull, pull_str);
1490 else
1491 seq_puts(s, " ");
1492
1493 if (conf0 & BYT_IODEN)
1494 seq_puts(s, " open-drain");
1495
1496 seq_puts(s, "\n");
Mathias Nymana5d811b2013-06-18 14:33:02 +03001497 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001498}
1499
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001500static void byt_gpio_irq_handler(struct irq_desc *desc)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001501{
1502 struct irq_data *data = irq_desc_get_irq_data(desc);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001503 struct byt_gpio *vg = gpiochip_get_data(irq_desc_get_handler_data(desc));
Mathias Nymana5d811b2013-06-18 14:33:02 +03001504 struct irq_chip *chip = irq_data_get_irq_chip(data);
Mika Westerberg31e43292015-02-23 14:53:12 +02001505 u32 base, pin;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001506 void __iomem *reg;
Mika Westerberg31e43292015-02-23 14:53:12 +02001507 unsigned long pending;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001508 unsigned virq;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001509
1510 /* check from GPIO controller which pin triggered the interrupt */
1511 for (base = 0; base < vg->chip.ngpio; base += 32) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001512 reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
Mika Westerberg31e43292015-02-23 14:53:12 +02001513 pending = readl(reg);
1514 for_each_set_bit(pin, &pending, 32) {
Mika Westerberge1ee5c52014-07-25 09:54:47 +03001515 virq = irq_find_mapping(vg->chip.irqdomain, base + pin);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001516 generic_handle_irq(virq);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001517 }
1518 }
1519 chip->irq_eoi(data);
1520}
1521
Mika Westerberg31e43292015-02-23 14:53:12 +02001522static void byt_irq_ack(struct irq_data *d)
1523{
1524 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001525 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001526 unsigned offset = irqd_to_hwirq(d);
1527 void __iomem *reg;
1528
Mika Westerberg78e1c892015-08-17 16:03:17 +03001529 raw_spin_lock(&vg->lock);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001530 reg = byt_gpio_reg(vg, offset, BYT_INT_STAT_REG);
Mika Westerberg31e43292015-02-23 14:53:12 +02001531 writel(BIT(offset % 32), reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001532 raw_spin_unlock(&vg->lock);
Mika Westerberg31e43292015-02-23 14:53:12 +02001533}
1534
Mathias Nymana5d811b2013-06-18 14:33:02 +03001535static void byt_irq_unmask(struct irq_data *d)
1536{
Mika Westerberg31e43292015-02-23 14:53:12 +02001537 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001538 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001539 unsigned offset = irqd_to_hwirq(d);
1540 unsigned long flags;
1541 void __iomem *reg;
1542 u32 value;
1543
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001544 reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001545
1546 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg31e43292015-02-23 14:53:12 +02001547 value = readl(reg);
1548
1549 switch (irqd_get_trigger_type(d)) {
1550 case IRQ_TYPE_LEVEL_HIGH:
1551 value |= BYT_TRIG_LVL;
1552 case IRQ_TYPE_EDGE_RISING:
1553 value |= BYT_TRIG_POS;
1554 break;
1555 case IRQ_TYPE_LEVEL_LOW:
1556 value |= BYT_TRIG_LVL;
1557 case IRQ_TYPE_EDGE_FALLING:
1558 value |= BYT_TRIG_NEG;
1559 break;
1560 case IRQ_TYPE_EDGE_BOTH:
1561 value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
1562 break;
1563 }
1564
1565 writel(value, reg);
1566
Mika Westerberg78e1c892015-08-17 16:03:17 +03001567 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001568}
1569
1570static void byt_irq_mask(struct irq_data *d)
1571{
Mika Westerberg31e43292015-02-23 14:53:12 +02001572 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001573 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001574
1575 byt_gpio_clear_triggering(vg, irqd_to_hwirq(d));
Mathias Nymana5d811b2013-06-18 14:33:02 +03001576}
1577
1578static struct irq_chip byt_irqchip = {
1579 .name = "BYT-GPIO",
Mika Westerberg31e43292015-02-23 14:53:12 +02001580 .irq_ack = byt_irq_ack,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001581 .irq_mask = byt_irq_mask,
1582 .irq_unmask = byt_irq_unmask,
1583 .irq_set_type = byt_irq_type,
Mathias Nyman41939e62014-08-11 16:51:55 +03001584 .flags = IRQCHIP_SKIP_SET_WAKE,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001585};
1586
1587static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
1588{
1589 void __iomem *reg;
1590 u32 base, value;
Mika Westerberg95f09722015-02-23 14:53:11 +02001591 int i;
1592
1593 /*
1594 * Clear interrupt triggers for all pins that are GPIOs and
1595 * do not use direct IRQ mode. This will prevent spurious
1596 * interrupts from misconfigured pins.
1597 */
1598 for (i = 0; i < vg->chip.ngpio; i++) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001599 value = readl(byt_gpio_reg(vg, i, BYT_CONF0_REG));
Mika Westerberg95f09722015-02-23 14:53:11 +02001600 if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i) &&
1601 !(value & BYT_DIRECT_IRQ_EN)) {
1602 byt_gpio_clear_triggering(vg, i);
1603 dev_dbg(&vg->pdev->dev, "disabling GPIO %d\n", i);
1604 }
1605 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001606
1607 /* clear interrupt status trigger registers */
1608 for (base = 0; base < vg->chip.ngpio; base += 32) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001609 reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001610 writel(0xffffffff, reg);
1611 /* make sure trigger bits are cleared, if not then a pin
1612 might be misconfigured in bios */
1613 value = readl(reg);
1614 if (value)
1615 dev_err(&vg->pdev->dev,
1616 "GPIO interrupt error, pins misconfigured\n");
1617 }
1618}
1619
Mathias Nymana5d811b2013-06-18 14:33:02 +03001620static int byt_gpio_probe(struct platform_device *pdev)
1621{
1622 struct byt_gpio *vg;
1623 struct gpio_chip *gc;
1624 struct resource *mem_rc, *irq_rc;
1625 struct device *dev = &pdev->dev;
1626 struct acpi_device *acpi_dev;
1627 struct pinctrl_gpio_range *range;
1628 acpi_handle handle = ACPI_HANDLE(dev);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001629 int ret;
1630
1631 if (acpi_bus_get_device(handle, &acpi_dev))
1632 return -ENODEV;
1633
1634 vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
1635 if (!vg) {
1636 dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
1637 return -ENOMEM;
1638 }
1639
1640 for (range = byt_ranges; range->name; range++) {
1641 if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
1642 vg->chip.ngpio = range->npins;
1643 vg->range = range;
1644 break;
1645 }
1646 }
1647
1648 if (!vg->chip.ngpio || !vg->range)
1649 return -ENODEV;
1650
1651 vg->pdev = pdev;
1652 platform_set_drvdata(pdev, vg);
1653
1654 mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1655 vg->reg_base = devm_ioremap_resource(dev, mem_rc);
1656 if (IS_ERR(vg->reg_base))
1657 return PTR_ERR(vg->reg_base);
1658
Mika Westerberg78e1c892015-08-17 16:03:17 +03001659 raw_spin_lock_init(&vg->lock);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001660
1661 gc = &vg->chip;
1662 gc->label = dev_name(&pdev->dev);
1663 gc->owner = THIS_MODULE;
1664 gc->request = byt_gpio_request;
1665 gc->free = byt_gpio_free;
1666 gc->direction_input = byt_gpio_direction_input;
1667 gc->direction_output = byt_gpio_direction_output;
1668 gc->get = byt_gpio_get;
1669 gc->set = byt_gpio_set;
1670 gc->dbg_show = byt_gpio_dbg_show;
1671 gc->base = -1;
Linus Walleij9fb1f392013-12-04 14:42:46 +01001672 gc->can_sleep = false;
Linus Walleij58383c782015-11-04 09:56:26 +01001673 gc->parent = dev;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001674
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001675#ifdef CONFIG_PM_SLEEP
1676 vg->saved_context = devm_kcalloc(&pdev->dev, gc->ngpio,
1677 sizeof(*vg->saved_context), GFP_KERNEL);
1678#endif
1679
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001680 ret = gpiochip_add_data(gc, vg);
Jin Yao605a7bc2014-05-15 18:28:47 +03001681 if (ret) {
1682 dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
1683 return ret;
1684 }
1685
Mika Westerberge1ee5c52014-07-25 09:54:47 +03001686 /* set up interrupts */
1687 irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1688 if (irq_rc && irq_rc->start) {
1689 byt_gpio_irq_init_hw(vg);
1690 ret = gpiochip_irqchip_add(gc, &byt_irqchip, 0,
1691 handle_simple_irq, IRQ_TYPE_NONE);
1692 if (ret) {
1693 dev_err(dev, "failed to add irqchip\n");
1694 gpiochip_remove(gc);
1695 return ret;
1696 }
1697
1698 gpiochip_set_chained_irqchip(gc, &byt_irqchip,
1699 (unsigned)irq_rc->start,
1700 byt_gpio_irq_handler);
1701 }
1702
Mathias Nymana5d811b2013-06-18 14:33:02 +03001703 pm_runtime_enable(dev);
1704
1705 return 0;
1706}
1707
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001708#ifdef CONFIG_PM_SLEEP
1709static int byt_gpio_suspend(struct device *dev)
1710{
1711 struct platform_device *pdev = to_platform_device(dev);
1712 struct byt_gpio *vg = platform_get_drvdata(pdev);
1713 int i;
1714
1715 for (i = 0; i < vg->chip.ngpio; i++) {
1716 void __iomem *reg;
1717 u32 value;
1718
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001719 reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001720 value = readl(reg) & BYT_CONF0_RESTORE_MASK;
1721 vg->saved_context[i].conf0 = value;
1722
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001723 reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001724 value = readl(reg) & BYT_VAL_RESTORE_MASK;
1725 vg->saved_context[i].val = value;
1726 }
1727
1728 return 0;
1729}
1730
1731static int byt_gpio_resume(struct device *dev)
1732{
1733 struct platform_device *pdev = to_platform_device(dev);
1734 struct byt_gpio *vg = platform_get_drvdata(pdev);
1735 int i;
1736
1737 for (i = 0; i < vg->chip.ngpio; i++) {
1738 void __iomem *reg;
1739 u32 value;
1740
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001741 reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001742 value = readl(reg);
1743 if ((value & BYT_CONF0_RESTORE_MASK) !=
1744 vg->saved_context[i].conf0) {
1745 value &= ~BYT_CONF0_RESTORE_MASK;
1746 value |= vg->saved_context[i].conf0;
1747 writel(value, reg);
1748 dev_info(dev, "restored pin %d conf0 %#08x", i, value);
1749 }
1750
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001751 reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001752 value = readl(reg);
1753 if ((value & BYT_VAL_RESTORE_MASK) !=
1754 vg->saved_context[i].val) {
1755 u32 v;
1756
1757 v = value & ~BYT_VAL_RESTORE_MASK;
1758 v |= vg->saved_context[i].val;
1759 if (v != value) {
1760 writel(v, reg);
1761 dev_dbg(dev, "restored pin %d val %#08x\n",
1762 i, v);
1763 }
1764 }
1765 }
1766
1767 return 0;
1768}
1769#endif
1770
Mika Westerbergec879f12015-10-13 17:51:26 +03001771#ifdef CONFIG_PM
Mathias Nymana5d811b2013-06-18 14:33:02 +03001772static int byt_gpio_runtime_suspend(struct device *dev)
1773{
1774 return 0;
1775}
1776
1777static int byt_gpio_runtime_resume(struct device *dev)
1778{
1779 return 0;
1780}
Mika Westerbergec879f12015-10-13 17:51:26 +03001781#endif
Mathias Nymana5d811b2013-06-18 14:33:02 +03001782
1783static const struct dev_pm_ops byt_gpio_pm_ops = {
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001784 SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
1785 SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
1786 NULL)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001787};
1788
1789static const struct acpi_device_id byt_gpio_acpi_match[] = {
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +03001790 { "INT33B2", (kernel_ulong_t)byt_soc_data },
1791 { "INT33FC", (kernel_ulong_t)byt_soc_data },
Mathias Nymana5d811b2013-06-18 14:33:02 +03001792 { }
1793};
1794MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
1795
1796static int byt_gpio_remove(struct platform_device *pdev)
1797{
1798 struct byt_gpio *vg = platform_get_drvdata(pdev);
Andy Shevchenkoec243322013-07-10 14:55:36 +03001799
Mathias Nymana5d811b2013-06-18 14:33:02 +03001800 pm_runtime_disable(&pdev->dev);
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001801 gpiochip_remove(&vg->chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001802
1803 return 0;
1804}
1805
1806static struct platform_driver byt_gpio_driver = {
1807 .probe = byt_gpio_probe,
1808 .remove = byt_gpio_remove,
1809 .driver = {
1810 .name = "byt_gpio",
Mathias Nymana5d811b2013-06-18 14:33:02 +03001811 .pm = &byt_gpio_pm_ops,
1812 .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
1813 },
1814};
1815
1816static int __init byt_gpio_init(void)
1817{
1818 return platform_driver_register(&byt_gpio_driver);
1819}
Mathias Nymana5d811b2013-06-18 14:33:02 +03001820subsys_initcall(byt_gpio_init);
Felipe Balbi9067bbe2014-10-13 15:50:08 -05001821
1822static void __exit byt_gpio_exit(void)
1823{
1824 platform_driver_unregister(&byt_gpio_driver);
1825}
1826module_exit(byt_gpio_exit);