blob: a24b51e47f00e00cab6ee9352adf6a383ac53148 [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>
Cristina Ciocan86e3ef812016-04-01 14:00:04 +030023#include <linux/gpio.h>
Linus Walleijbf9a5c92015-12-08 00:03:44 +010024#include <linux/gpio/driver.h>
Mathias Nymana5d811b2013-06-18 14:33:02 +030025#include <linux/acpi.h>
Mathias Nymana5d811b2013-06-18 14:33:02 +030026#include <linux/platform_device.h>
27#include <linux/seq_file.h>
28#include <linux/io.h>
29#include <linux/pm_runtime.h>
30#include <linux/pinctrl/pinctrl.h>
Cristina Ciocanc501d0b2016-04-01 14:00:03 +030031#include <linux/pinctrl/pinmux.h>
32#include <linux/pinctrl/pinconf.h>
33#include <linux/pinctrl/pinconf-generic.h>
Mathias Nymana5d811b2013-06-18 14:33:02 +030034
35/* memory mapped register offsets */
36#define BYT_CONF0_REG 0x000
37#define BYT_CONF1_REG 0x004
38#define BYT_VAL_REG 0x008
39#define BYT_DFT_REG 0x00c
40#define BYT_INT_STAT_REG 0x800
41
42/* BYT_CONF0_REG register bits */
Mika Westerberg3ff95882014-05-16 12:18:29 +030043#define BYT_IODEN BIT(31)
Eric Ernstff998352014-06-12 11:06:20 -070044#define BYT_DIRECT_IRQ_EN BIT(27)
Mathias Nymana5d811b2013-06-18 14:33:02 +030045#define BYT_TRIG_NEG BIT(26)
46#define BYT_TRIG_POS BIT(25)
47#define BYT_TRIG_LVL BIT(24)
Mika Westerberg3ff95882014-05-16 12:18:29 +030048#define BYT_PULL_STR_SHIFT 9
49#define BYT_PULL_STR_MASK (3 << BYT_PULL_STR_SHIFT)
50#define BYT_PULL_STR_2K (0 << BYT_PULL_STR_SHIFT)
51#define BYT_PULL_STR_10K (1 << BYT_PULL_STR_SHIFT)
52#define BYT_PULL_STR_20K (2 << BYT_PULL_STR_SHIFT)
53#define BYT_PULL_STR_40K (3 << BYT_PULL_STR_SHIFT)
54#define BYT_PULL_ASSIGN_SHIFT 7
55#define BYT_PULL_ASSIGN_MASK (3 << BYT_PULL_ASSIGN_SHIFT)
56#define BYT_PULL_ASSIGN_UP (1 << BYT_PULL_ASSIGN_SHIFT)
57#define BYT_PULL_ASSIGN_DOWN (2 << BYT_PULL_ASSIGN_SHIFT)
Mathias Nymana5d811b2013-06-18 14:33:02 +030058#define BYT_PIN_MUX 0x07
59
60/* BYT_VAL_REG register bits */
61#define BYT_INPUT_EN BIT(2) /* 0: input enabled (active low)*/
62#define BYT_OUTPUT_EN BIT(1) /* 0: output enabled (active low)*/
63#define BYT_LEVEL BIT(0)
64
65#define BYT_DIR_MASK (BIT(1) | BIT(2))
66#define BYT_TRIG_MASK (BIT(26) | BIT(25) | BIT(24))
67
Mika Westerbergfcc18de2015-02-23 14:53:13 +020068#define BYT_CONF0_RESTORE_MASK (BYT_DIRECT_IRQ_EN | BYT_TRIG_MASK | \
69 BYT_PIN_MUX)
70#define BYT_VAL_RESTORE_MASK (BYT_DIR_MASK | BYT_LEVEL)
71
Mathias Nymana5d811b2013-06-18 14:33:02 +030072#define BYT_NGPIO_SCORE 102
73#define BYT_NGPIO_NCORE 28
74#define BYT_NGPIO_SUS 44
75
Chew, Kean Ho42bd0072014-03-06 21:59:49 +080076#define BYT_SCORE_ACPI_UID "1"
77#define BYT_NCORE_ACPI_UID "2"
78#define BYT_SUS_ACPI_UID "3"
79
Cristina Ciocanc501d0b2016-04-01 14:00:03 +030080/*
81 * This is the function value most pins have for GPIO muxing. If the value
82 * differs from the default one, it must be explicitly mentioned. Otherwise, the
83 * pin control implementation will set the muxing value to default GPIO if it
84 * does not find a match for the requested function.
85 */
86#define BYT_DEFAULT_GPIO_MUX 0
87
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +030088struct byt_gpio_pin_context {
89 u32 conf0;
90 u32 val;
91};
Mathias Nymana5d811b2013-06-18 14:33:02 +030092
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +030093struct byt_simple_func_mux {
94 const char *name;
95 unsigned short func;
96};
97
98struct byt_mixed_func_mux {
99 const char *name;
100 const unsigned short *func_values;
101};
102
103struct byt_pingroup {
104 const char *name;
105 const unsigned int *pins;
106 size_t npins;
107 unsigned short has_simple_funcs;
108 union {
109 const struct byt_simple_func_mux *simple_funcs;
110 const struct byt_mixed_func_mux *mixed_funcs;
111 };
112 size_t nfuncs;
113};
114
115struct byt_function {
116 const char *name;
117 const char * const *groups;
118 size_t ngroups;
119};
120
121struct byt_community {
122 unsigned int pin_base;
123 size_t npins;
124 const unsigned int *pad_map;
125 void __iomem *reg_base;
126};
127
128#define SIMPLE_FUNC(n, f) \
129 { \
130 .name = (n), \
131 .func = (f), \
132 }
133#define MIXED_FUNC(n, f) \
134 { \
135 .name = (n), \
136 .func_values = (f), \
137 }
138
139#define PIN_GROUP_SIMPLE(n, p, f) \
140 { \
141 .name = (n), \
142 .pins = (p), \
143 .npins = ARRAY_SIZE((p)), \
144 .has_simple_funcs = 1, \
145 .simple_funcs = (f), \
146 .nfuncs = ARRAY_SIZE((f)), \
147 }
148#define PIN_GROUP_MIXED(n, p, f) \
149 { \
150 .name = (n), \
151 .pins = (p), \
152 .npins = ARRAY_SIZE((p)), \
153 .has_simple_funcs = 0, \
154 .mixed_funcs = (f), \
155 .nfuncs = ARRAY_SIZE((f)), \
156 }
157
158#define FUNCTION(n, g) \
159 { \
160 .name = (n), \
161 .groups = (g), \
162 .ngroups = ARRAY_SIZE((g)), \
163 }
164
165#define COMMUNITY(p, n, map) \
166 { \
167 .pin_base = (p), \
168 .npins = (n), \
169 .pad_map = (map),\
170 }
171
172struct byt_pinctrl_soc_data {
173 const char *uid;
174 const struct pinctrl_pin_desc *pins;
175 size_t npins;
176 const struct byt_pingroup *groups;
177 size_t ngroups;
178 const struct byt_function *functions;
179 size_t nfunctions;
180 const struct byt_community *communities;
181 size_t ncommunities;
182};
183
184/* SCORE pins, aka GPIOC_<pin_no> or GPIO_S0_SC[<pin_no>] */
185static const struct pinctrl_pin_desc byt_score_pins[] = {
186 PINCTRL_PIN(0, "SATA_GP0"),
187 PINCTRL_PIN(1, "SATA_GP1"),
188 PINCTRL_PIN(2, "SATA_LED#"),
189 PINCTRL_PIN(3, "PCIE_CLKREQ0"),
190 PINCTRL_PIN(4, "PCIE_CLKREQ1"),
191 PINCTRL_PIN(5, "PCIE_CLKREQ2"),
192 PINCTRL_PIN(6, "PCIE_CLKREQ3"),
193 PINCTRL_PIN(7, "SD3_WP"),
194 PINCTRL_PIN(8, "HDA_RST"),
195 PINCTRL_PIN(9, "HDA_SYNC"),
196 PINCTRL_PIN(10, "HDA_CLK"),
197 PINCTRL_PIN(11, "HDA_SDO"),
198 PINCTRL_PIN(12, "HDA_SDI0"),
199 PINCTRL_PIN(13, "HDA_SDI1"),
200 PINCTRL_PIN(14, "GPIO_S0_SC14"),
201 PINCTRL_PIN(15, "GPIO_S0_SC15"),
202 PINCTRL_PIN(16, "MMC1_CLK"),
203 PINCTRL_PIN(17, "MMC1_D0"),
204 PINCTRL_PIN(18, "MMC1_D1"),
205 PINCTRL_PIN(19, "MMC1_D2"),
206 PINCTRL_PIN(20, "MMC1_D3"),
207 PINCTRL_PIN(21, "MMC1_D4"),
208 PINCTRL_PIN(22, "MMC1_D5"),
209 PINCTRL_PIN(23, "MMC1_D6"),
210 PINCTRL_PIN(24, "MMC1_D7"),
211 PINCTRL_PIN(25, "MMC1_CMD"),
212 PINCTRL_PIN(26, "MMC1_RST"),
213 PINCTRL_PIN(27, "SD2_CLK"),
214 PINCTRL_PIN(28, "SD2_D0"),
215 PINCTRL_PIN(29, "SD2_D1"),
216 PINCTRL_PIN(30, "SD2_D2"),
217 PINCTRL_PIN(31, "SD2_D3_CD"),
218 PINCTRL_PIN(32, "SD2_CMD"),
219 PINCTRL_PIN(33, "SD3_CLK"),
220 PINCTRL_PIN(34, "SD3_D0"),
221 PINCTRL_PIN(35, "SD3_D1"),
222 PINCTRL_PIN(36, "SD3_D2"),
223 PINCTRL_PIN(37, "SD3_D3"),
224 PINCTRL_PIN(38, "SD3_CD"),
225 PINCTRL_PIN(39, "SD3_CMD"),
226 PINCTRL_PIN(40, "SD3_1P8EN"),
227 PINCTRL_PIN(41, "SD3_PWREN#"),
228 PINCTRL_PIN(42, "ILB_LPC_AD0"),
229 PINCTRL_PIN(43, "ILB_LPC_AD1"),
230 PINCTRL_PIN(44, "ILB_LPC_AD2"),
231 PINCTRL_PIN(45, "ILB_LPC_AD3"),
232 PINCTRL_PIN(46, "ILB_LPC_FRAME"),
233 PINCTRL_PIN(47, "ILB_LPC_CLK0"),
234 PINCTRL_PIN(48, "ILB_LPC_CLK1"),
235 PINCTRL_PIN(49, "ILB_LPC_CLKRUN"),
236 PINCTRL_PIN(50, "ILB_LPC_SERIRQ"),
237 PINCTRL_PIN(51, "PCU_SMB_DATA"),
238 PINCTRL_PIN(52, "PCU_SMB_CLK"),
239 PINCTRL_PIN(53, "PCU_SMB_ALERT"),
240 PINCTRL_PIN(54, "ILB_8254_SPKR"),
241 PINCTRL_PIN(55, "GPIO_S0_SC55"),
242 PINCTRL_PIN(56, "GPIO_S0_SC56"),
243 PINCTRL_PIN(57, "GPIO_S0_SC57"),
244 PINCTRL_PIN(58, "GPIO_S0_SC58"),
245 PINCTRL_PIN(59, "GPIO_S0_SC59"),
246 PINCTRL_PIN(60, "GPIO_S0_SC60"),
247 PINCTRL_PIN(61, "GPIO_S0_SC61"),
248 PINCTRL_PIN(62, "LPE_I2S2_CLK"),
249 PINCTRL_PIN(63, "LPE_I2S2_FRM"),
250 PINCTRL_PIN(64, "LPE_I2S2_DATAIN"),
251 PINCTRL_PIN(65, "LPE_I2S2_DATAOUT"),
252 PINCTRL_PIN(66, "SIO_SPI_CS"),
253 PINCTRL_PIN(67, "SIO_SPI_MISO"),
254 PINCTRL_PIN(68, "SIO_SPI_MOSI"),
255 PINCTRL_PIN(69, "SIO_SPI_CLK"),
256 PINCTRL_PIN(70, "SIO_UART1_RXD"),
257 PINCTRL_PIN(71, "SIO_UART1_TXD"),
258 PINCTRL_PIN(72, "SIO_UART1_RTS"),
259 PINCTRL_PIN(73, "SIO_UART1_CTS"),
260 PINCTRL_PIN(74, "SIO_UART2_RXD"),
261 PINCTRL_PIN(75, "SIO_UART2_TXD"),
262 PINCTRL_PIN(76, "SIO_UART2_RTS"),
263 PINCTRL_PIN(77, "SIO_UART2_CTS"),
264 PINCTRL_PIN(78, "SIO_I2C0_DATA"),
265 PINCTRL_PIN(79, "SIO_I2C0_CLK"),
266 PINCTRL_PIN(80, "SIO_I2C1_DATA"),
267 PINCTRL_PIN(81, "SIO_I2C1_CLK"),
268 PINCTRL_PIN(82, "SIO_I2C2_DATA"),
269 PINCTRL_PIN(83, "SIO_I2C2_CLK"),
270 PINCTRL_PIN(84, "SIO_I2C3_DATA"),
271 PINCTRL_PIN(85, "SIO_I2C3_CLK"),
272 PINCTRL_PIN(86, "SIO_I2C4_DATA"),
273 PINCTRL_PIN(87, "SIO_I2C4_CLK"),
274 PINCTRL_PIN(88, "SIO_I2C5_DATA"),
275 PINCTRL_PIN(89, "SIO_I2C5_CLK"),
276 PINCTRL_PIN(90, "SIO_I2C6_DATA"),
277 PINCTRL_PIN(91, "SIO_I2C6_CLK"),
278 PINCTRL_PIN(92, "GPIO_S0_SC92"),
279 PINCTRL_PIN(93, "GPIO_S0_SC93"),
280 PINCTRL_PIN(94, "SIO_PWM0"),
281 PINCTRL_PIN(95, "SIO_PWM1"),
282 PINCTRL_PIN(96, "PMC_PLT_CLK0"),
283 PINCTRL_PIN(97, "PMC_PLT_CLK1"),
284 PINCTRL_PIN(98, "PMC_PLT_CLK2"),
285 PINCTRL_PIN(99, "PMC_PLT_CLK3"),
286 PINCTRL_PIN(100, "PMC_PLT_CLK4"),
287 PINCTRL_PIN(101, "PMC_PLT_CLK5"),
288};
Mathias Nymana5d811b2013-06-18 14:33:02 +0300289
290static unsigned const score_pins[BYT_NGPIO_SCORE] = {
291 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
292 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
293 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
294 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
295 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
296 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
297 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
298 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
299 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
300 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
301 97, 100,
302};
303
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300304static const unsigned int byt_score_pins_map[BYT_NGPIO_SCORE] = {
305 85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
306 36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
307 54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
308 52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
309 95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
310 86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
311 80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
312 2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
313 31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
314 24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
315 97, 100,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300316};
317
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300318/* SCORE groups */
319static const unsigned int byt_score_uart1_pins[] = { 70, 71, 72, 73 };
320static const unsigned int byt_score_uart2_pins[] = { 74, 75, 76, 77 };
321static const struct byt_simple_func_mux byt_score_uart_mux[] = {
322 SIMPLE_FUNC("uart", 1),
323};
324
325static const unsigned int byt_score_pwm0_pins[] = { 94 };
326static const unsigned int byt_score_pwm1_pins[] = { 95 };
327static const struct byt_simple_func_mux byt_score_pwm_mux[] = {
328 SIMPLE_FUNC("pwm", 1),
329};
330
331static const unsigned int byt_score_sio_spi_pins[] = { 66, 67, 68, 69 };
332static const struct byt_simple_func_mux byt_score_spi_mux[] = {
333 SIMPLE_FUNC("spi", 1),
334};
335
336static const unsigned int byt_score_i2c5_pins[] = { 88, 89 };
337static const unsigned int byt_score_i2c6_pins[] = { 90, 91 };
338static const unsigned int byt_score_i2c4_pins[] = { 86, 87 };
339static const unsigned int byt_score_i2c3_pins[] = { 84, 85 };
340static const unsigned int byt_score_i2c2_pins[] = { 82, 83 };
341static const unsigned int byt_score_i2c1_pins[] = { 80, 81 };
342static const unsigned int byt_score_i2c0_pins[] = { 78, 79 };
343static const struct byt_simple_func_mux byt_score_i2c_mux[] = {
344 SIMPLE_FUNC("i2c", 1),
345};
346
347static const unsigned int byt_score_ssp0_pins[] = { 8, 9, 10, 11 };
348static const unsigned int byt_score_ssp1_pins[] = { 12, 13, 14, 15 };
349static const unsigned int byt_score_ssp2_pins[] = { 62, 63, 64, 65 };
350static const struct byt_simple_func_mux byt_score_ssp_mux[] = {
351 SIMPLE_FUNC("ssp", 1),
352};
353
354static const unsigned int byt_score_sdcard_pins[] = {
355 7, 33, 34, 35, 36, 37, 38, 39, 40, 41,
356};
357static const unsigned short byt_score_sdcard_mux_values[] = {
358 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
359};
360static const struct byt_mixed_func_mux byt_score_sdcard_mux[] = {
361 MIXED_FUNC("sdcard", byt_score_sdcard_mux_values),
362};
363
364static const unsigned int byt_score_sdio_pins[] = { 27, 28, 29, 30, 31, 32 };
365static const struct byt_simple_func_mux byt_score_sdio_mux[] = {
366 SIMPLE_FUNC("sdio", 1),
367};
368
369static const unsigned int byt_score_emmc_pins[] = {
370 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
371};
372static const struct byt_simple_func_mux byt_score_emmc_mux[] = {
373 SIMPLE_FUNC("emmc", 1),
374};
375
376static const unsigned int byt_score_ilb_lpc_pins[] = {
377 42, 43, 44, 45, 46, 47, 48, 49, 50,
378};
379static const struct byt_simple_func_mux byt_score_lpc_mux[] = {
380 SIMPLE_FUNC("lpc", 1),
381};
382
383static const unsigned int byt_score_sata_pins[] = { 0, 1, 2 };
384static const struct byt_simple_func_mux byt_score_sata_mux[] = {
385 SIMPLE_FUNC("sata", 1),
386};
387
388static const unsigned int byt_score_plt_clk0_pins[] = { 96 };
389static const unsigned int byt_score_plt_clk1_pins[] = { 97 };
390static const unsigned int byt_score_plt_clk2_pins[] = { 98 };
391static const unsigned int byt_score_plt_clk4_pins[] = { 99 };
392static const unsigned int byt_score_plt_clk5_pins[] = { 100 };
393static const unsigned int byt_score_plt_clk3_pins[] = { 101 };
394static const struct byt_simple_func_mux byt_score_plt_clk_mux[] = {
395 SIMPLE_FUNC("plt_clk", 1),
396};
397
398static const unsigned int byt_score_smbus_pins[] = { 51, 52, 53 };
399static const struct byt_simple_func_mux byt_score_smbus_mux[] = {
400 SIMPLE_FUNC("smbus", 1),
401};
402
403static const struct byt_pingroup byt_score_groups[] = {
404 PIN_GROUP_SIMPLE("uart1_grp",
405 byt_score_uart1_pins, byt_score_uart_mux),
406 PIN_GROUP_SIMPLE("uart2_grp",
407 byt_score_uart2_pins, byt_score_uart_mux),
408 PIN_GROUP_SIMPLE("pwm0_grp",
409 byt_score_pwm0_pins, byt_score_pwm_mux),
410 PIN_GROUP_SIMPLE("pwm1_grp",
411 byt_score_pwm1_pins, byt_score_pwm_mux),
412 PIN_GROUP_SIMPLE("ssp2_grp",
413 byt_score_ssp2_pins, byt_score_pwm_mux),
414 PIN_GROUP_SIMPLE("sio_spi_grp",
415 byt_score_sio_spi_pins, byt_score_spi_mux),
416 PIN_GROUP_SIMPLE("i2c5_grp",
417 byt_score_i2c5_pins, byt_score_i2c_mux),
418 PIN_GROUP_SIMPLE("i2c6_grp",
419 byt_score_i2c6_pins, byt_score_i2c_mux),
420 PIN_GROUP_SIMPLE("i2c4_grp",
421 byt_score_i2c4_pins, byt_score_i2c_mux),
422 PIN_GROUP_SIMPLE("i2c3_grp",
423 byt_score_i2c3_pins, byt_score_i2c_mux),
424 PIN_GROUP_SIMPLE("i2c2_grp",
425 byt_score_i2c2_pins, byt_score_i2c_mux),
426 PIN_GROUP_SIMPLE("i2c1_grp",
427 byt_score_i2c1_pins, byt_score_i2c_mux),
428 PIN_GROUP_SIMPLE("i2c0_grp",
429 byt_score_i2c0_pins, byt_score_i2c_mux),
430 PIN_GROUP_SIMPLE("ssp0_grp",
431 byt_score_ssp0_pins, byt_score_ssp_mux),
432 PIN_GROUP_SIMPLE("ssp1_grp",
433 byt_score_ssp1_pins, byt_score_ssp_mux),
434 PIN_GROUP_MIXED("sdcard_grp",
435 byt_score_sdcard_pins, byt_score_sdcard_mux),
436 PIN_GROUP_SIMPLE("sdio_grp",
437 byt_score_sdio_pins, byt_score_sdio_mux),
438 PIN_GROUP_SIMPLE("emmc_grp",
439 byt_score_emmc_pins, byt_score_emmc_mux),
440 PIN_GROUP_SIMPLE("lpc_grp",
441 byt_score_ilb_lpc_pins, byt_score_lpc_mux),
442 PIN_GROUP_SIMPLE("sata_grp",
443 byt_score_sata_pins, byt_score_sata_mux),
444 PIN_GROUP_SIMPLE("plt_clk0_grp",
445 byt_score_plt_clk0_pins, byt_score_plt_clk_mux),
446 PIN_GROUP_SIMPLE("plt_clk1_grp",
447 byt_score_plt_clk1_pins, byt_score_plt_clk_mux),
448 PIN_GROUP_SIMPLE("plt_clk2_grp",
449 byt_score_plt_clk2_pins, byt_score_plt_clk_mux),
450 PIN_GROUP_SIMPLE("plt_clk3_grp",
451 byt_score_plt_clk3_pins, byt_score_plt_clk_mux),
452 PIN_GROUP_SIMPLE("plt_clk4_grp",
453 byt_score_plt_clk4_pins, byt_score_plt_clk_mux),
454 PIN_GROUP_SIMPLE("plt_clk5_grp",
455 byt_score_plt_clk5_pins, byt_score_plt_clk_mux),
456 PIN_GROUP_SIMPLE("smbus_grp",
457 byt_score_smbus_pins, byt_score_smbus_mux),
458};
459
460static const char * const byt_score_uart_groups[] = {
461 "uart1_grp", "uart2_grp",
462};
463static const char * const byt_score_pwm_groups[] = {
464 "pwm0_grp", "pwm1_grp",
465};
466static const char * const byt_score_ssp_groups[] = {
467 "ssp0_grp", "ssp1_grp", "ssp2_grp",
468};
469static const char * const byt_score_spi_groups[] = { "sio_spi_grp" };
470static const char * const byt_score_i2c_groups[] = {
471 "i2c0_grp", "i2c1_grp", "i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp",
472 "i2c6_grp",
473};
474static const char * const byt_score_sdcard_groups[] = { "sdcard_grp" };
475static const char * const byt_score_sdio_groups[] = { "sdio_grp" };
476static const char * const byt_score_emmc_groups[] = { "emmc_grp" };
477static const char * const byt_score_lpc_groups[] = { "lpc_grp" };
478static const char * const byt_score_sata_groups[] = { "sata_grp" };
479static const char * const byt_score_plt_clk_groups[] = {
480 "plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
481 "plt_clk4_grp", "plt_clk5_grp",
482};
483static const char * const byt_score_smbus_groups[] = { "smbus_grp" };
484static const char * const byt_score_gpio_groups[] = {
485 "uart1_grp", "uart2_grp", "pwm0_grp", "pwm1_grp", "ssp0_grp",
486 "ssp1_grp", "ssp2_grp", "sio_spi_grp", "i2c0_grp", "i2c1_grp",
487 "i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp", "i2c6_grp",
488 "sdcard_grp", "sdio_grp", "emmc_grp", "lpc_grp", "sata_grp",
489 "plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
490 "plt_clk4_grp", "plt_clk5_grp", "smbus_grp",
491
492};
493
494static const struct byt_function byt_score_functions[] = {
495 FUNCTION("uart", byt_score_uart_groups),
496 FUNCTION("pwm", byt_score_pwm_groups),
497 FUNCTION("ssp", byt_score_ssp_groups),
498 FUNCTION("spi", byt_score_spi_groups),
499 FUNCTION("i2c", byt_score_i2c_groups),
500 FUNCTION("sdcard", byt_score_sdcard_groups),
501 FUNCTION("sdio", byt_score_sdio_groups),
502 FUNCTION("emmc", byt_score_emmc_groups),
503 FUNCTION("lpc", byt_score_lpc_groups),
504 FUNCTION("sata", byt_score_sata_groups),
505 FUNCTION("plt_clk", byt_score_plt_clk_groups),
506 FUNCTION("smbus", byt_score_smbus_groups),
507 FUNCTION("gpio", byt_score_gpio_groups),
508};
509
510static const struct byt_community byt_score_communities[] = {
511 COMMUNITY(0, BYT_NGPIO_SCORE, byt_score_pins_map),
512};
513
514static const struct byt_pinctrl_soc_data byt_score_soc_data = {
515 .uid = BYT_SCORE_ACPI_UID,
516 .pins = byt_score_pins,
517 .npins = ARRAY_SIZE(byt_score_pins),
518 .groups = byt_score_groups,
519 .ngroups = ARRAY_SIZE(byt_score_groups),
520 .functions = byt_score_functions,
521 .nfunctions = ARRAY_SIZE(byt_score_functions),
522 .communities = byt_score_communities,
523 .ncommunities = ARRAY_SIZE(byt_score_communities),
524};
525
526/* SUS pins, aka GPIOS_<pin_no> or GPIO_S5[<pin_no>] */
527static const struct pinctrl_pin_desc byt_sus_pins[] = {
528 PINCTRL_PIN(0, "GPIO_S50"),
529 PINCTRL_PIN(1, "GPIO_S51"),
530 PINCTRL_PIN(2, "GPIO_S52"),
531 PINCTRL_PIN(3, "GPIO_S53"),
532 PINCTRL_PIN(4, "GPIO_S54"),
533 PINCTRL_PIN(5, "GPIO_S55"),
534 PINCTRL_PIN(6, "GPIO_S56"),
535 PINCTRL_PIN(7, "GPIO_S57"),
536 PINCTRL_PIN(8, "GPIO_S58"),
537 PINCTRL_PIN(9, "GPIO_S59"),
538 PINCTRL_PIN(10, "GPIO_S510"),
539 PINCTRL_PIN(11, "PMC_SUSPWRDNACK"),
540 PINCTRL_PIN(12, "PMC_SUSCLK0"),
541 PINCTRL_PIN(13, "GPIO_S513"),
542 PINCTRL_PIN(14, "USB_ULPI_RST"),
543 PINCTRL_PIN(15, "PMC_WAKE_PCIE0#"),
544 PINCTRL_PIN(16, "PMC_PWRBTN"),
545 PINCTRL_PIN(17, "GPIO_S517"),
546 PINCTRL_PIN(18, "PMC_SUS_STAT"),
547 PINCTRL_PIN(19, "USB_OC0"),
548 PINCTRL_PIN(20, "USB_OC1"),
549 PINCTRL_PIN(21, "PCU_SPI_CS1"),
550 PINCTRL_PIN(22, "GPIO_S522"),
551 PINCTRL_PIN(23, "GPIO_S523"),
552 PINCTRL_PIN(24, "GPIO_S524"),
553 PINCTRL_PIN(25, "GPIO_S525"),
554 PINCTRL_PIN(26, "GPIO_S526"),
555 PINCTRL_PIN(27, "GPIO_S527"),
556 PINCTRL_PIN(28, "GPIO_S528"),
557 PINCTRL_PIN(29, "GPIO_S529"),
558 PINCTRL_PIN(30, "GPIO_S530"),
559 PINCTRL_PIN(31, "USB_ULPI_CLK"),
560 PINCTRL_PIN(32, "USB_ULPI_DATA0"),
561 PINCTRL_PIN(33, "USB_ULPI_DATA1"),
562 PINCTRL_PIN(34, "USB_ULPI_DATA2"),
563 PINCTRL_PIN(35, "USB_ULPI_DATA3"),
564 PINCTRL_PIN(36, "USB_ULPI_DATA4"),
565 PINCTRL_PIN(37, "USB_ULPI_DATA5"),
566 PINCTRL_PIN(38, "USB_ULPI_DATA6"),
567 PINCTRL_PIN(39, "USB_ULPI_DATA7"),
568 PINCTRL_PIN(40, "USB_ULPI_DIR"),
569 PINCTRL_PIN(41, "USB_ULPI_NXT"),
570 PINCTRL_PIN(42, "USB_ULPI_STP"),
571 PINCTRL_PIN(43, "USB_ULPI_REFCLK"),
572};
573
574static const unsigned int sus_pins[BYT_NGPIO_SUS] = {
Mathias Nymana5d811b2013-06-18 14:33:02 +0300575 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
576 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
577 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
578 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
579 52, 53, 59, 40,
580};
581
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300582static const unsigned int byt_sus_pins_map[BYT_NGPIO_SUS] = {
583 29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
584 18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
585 0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
586 26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
587 52, 53, 59, 40,
588};
589
590static const unsigned int byt_sus_usb_over_current_pins[] = { 19, 20 };
591static const struct byt_simple_func_mux byt_sus_usb_oc_mux[] = {
592 SIMPLE_FUNC("usb", 0),
593 SIMPLE_FUNC("gpio", 1),
594};
595
596static const unsigned int byt_sus_usb_ulpi_pins[] = {
597 14, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
598};
599static const unsigned short byt_sus_usb_ulpi_mode_values[] = {
600 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
601};
602static const unsigned short byt_sus_usb_ulpi_gpio_mode_values[] = {
603 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
604};
605static const struct byt_mixed_func_mux byt_sus_usb_ulpi_mux[] = {
606 MIXED_FUNC("usb", byt_sus_usb_ulpi_mode_values),
607 MIXED_FUNC("gpio", byt_sus_usb_ulpi_gpio_mode_values),
608};
609
610static const unsigned int byt_sus_pcu_spi_pins[] = { 21 };
611static const struct byt_simple_func_mux byt_sus_pcu_spi_mux[] = {
612 SIMPLE_FUNC("spi", 0),
613 SIMPLE_FUNC("gpio", 1),
614};
615
616static const struct byt_pingroup byt_sus_groups[] = {
617 PIN_GROUP_SIMPLE("usb_oc_grp",
618 byt_sus_usb_over_current_pins, byt_sus_usb_oc_mux),
619 PIN_GROUP_MIXED("usb_ulpi_grp",
620 byt_sus_usb_ulpi_pins, byt_sus_usb_ulpi_mux),
621 PIN_GROUP_SIMPLE("pcu_spi_grp",
622 byt_sus_pcu_spi_pins, byt_sus_pcu_spi_mux),
623};
624
625static const char * const byt_sus_usb_groups[] = {
626 "usb_oc_grp", "usb_ulpi_grp",
627};
628static const char * const byt_sus_spi_groups[] = { "pcu_spi_grp" };
629static const char * const byt_sus_gpio_groups[] = {
630 "usb_oc_grp", "usb_ulpi_grp", "pcu_spi_grp",
631};
632
633static const struct byt_function byt_sus_functions[] = {
634 FUNCTION("usb", byt_sus_usb_groups),
635 FUNCTION("spi", byt_sus_spi_groups),
636 FUNCTION("gpio", byt_sus_gpio_groups),
637};
638
639static const struct byt_community byt_sus_communities[] = {
640 COMMUNITY(0, BYT_NGPIO_SUS, byt_sus_pins_map),
641};
642
643static const struct byt_pinctrl_soc_data byt_sus_soc_data = {
644 .uid = BYT_SUS_ACPI_UID,
645 .pins = byt_sus_pins,
646 .npins = ARRAY_SIZE(byt_sus_pins),
647 .groups = byt_sus_groups,
648 .ngroups = ARRAY_SIZE(byt_sus_groups),
649 .functions = byt_sus_functions,
650 .nfunctions = ARRAY_SIZE(byt_sus_functions),
651 .communities = byt_sus_communities,
652 .ncommunities = ARRAY_SIZE(byt_sus_communities),
653};
654
655static const struct pinctrl_pin_desc byt_ncore_pins[] = {
656 PINCTRL_PIN(0, "GPIO_NCORE0"),
657 PINCTRL_PIN(1, "GPIO_NCORE1"),
658 PINCTRL_PIN(2, "GPIO_NCORE2"),
659 PINCTRL_PIN(3, "GPIO_NCORE3"),
660 PINCTRL_PIN(4, "GPIO_NCORE4"),
661 PINCTRL_PIN(5, "GPIO_NCORE5"),
662 PINCTRL_PIN(6, "GPIO_NCORE6"),
663 PINCTRL_PIN(7, "GPIO_NCORE7"),
664 PINCTRL_PIN(8, "GPIO_NCORE8"),
665 PINCTRL_PIN(9, "GPIO_NCORE9"),
666 PINCTRL_PIN(10, "GPIO_NCORE10"),
667 PINCTRL_PIN(11, "GPIO_NCORE11"),
668 PINCTRL_PIN(12, "GPIO_NCORE12"),
669 PINCTRL_PIN(13, "GPIO_NCORE13"),
670 PINCTRL_PIN(14, "GPIO_NCORE14"),
671 PINCTRL_PIN(15, "GPIO_NCORE15"),
672 PINCTRL_PIN(16, "GPIO_NCORE16"),
673 PINCTRL_PIN(17, "GPIO_NCORE17"),
674 PINCTRL_PIN(18, "GPIO_NCORE18"),
675 PINCTRL_PIN(19, "GPIO_NCORE19"),
676 PINCTRL_PIN(20, "GPIO_NCORE20"),
677 PINCTRL_PIN(21, "GPIO_NCORE21"),
678 PINCTRL_PIN(22, "GPIO_NCORE22"),
679 PINCTRL_PIN(23, "GPIO_NCORE23"),
680 PINCTRL_PIN(24, "GPIO_NCORE24"),
681 PINCTRL_PIN(25, "GPIO_NCORE25"),
682 PINCTRL_PIN(26, "GPIO_NCORE26"),
683 PINCTRL_PIN(27, "GPIO_NCORE27"),
684};
685
686static unsigned const byt_ncore_pins_map[BYT_NGPIO_NCORE] = {
687 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
688 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
689 3, 6, 10, 13, 2, 5, 9, 7,
690};
691
692static const struct byt_community byt_ncore_communities[] = {
693 COMMUNITY(0, BYT_NGPIO_NCORE, byt_ncore_pins_map),
694};
695
696static const struct byt_pinctrl_soc_data byt_ncore_soc_data = {
697 .uid = BYT_NCORE_ACPI_UID,
698 .pins = byt_ncore_pins,
699 .npins = ARRAY_SIZE(byt_ncore_pins),
700 .communities = byt_ncore_communities,
701 .ncommunities = ARRAY_SIZE(byt_ncore_communities),
702};
703
704static unsigned const ncore_pins[BYT_NGPIO_NCORE] = {
705 19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
706 14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
707 3, 6, 10, 13, 2, 5, 9, 7,
708};
709
Mathias Nymana5d811b2013-06-18 14:33:02 +0300710static struct pinctrl_gpio_range byt_ranges[] = {
711 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800712 .name = BYT_SCORE_ACPI_UID, /* match with acpi _UID in probe */
Mathias Nymana5d811b2013-06-18 14:33:02 +0300713 .npins = BYT_NGPIO_SCORE,
714 .pins = score_pins,
715 },
716 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800717 .name = BYT_NCORE_ACPI_UID,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300718 .npins = BYT_NGPIO_NCORE,
719 .pins = ncore_pins,
720 },
721 {
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800722 .name = BYT_SUS_ACPI_UID,
Mathias Nymana5d811b2013-06-18 14:33:02 +0300723 .npins = BYT_NGPIO_SUS,
724 .pins = sus_pins,
725 },
726 {
727 },
728};
729
730struct byt_gpio {
731 struct gpio_chip chip;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300732 struct platform_device *pdev;
Mika Westerberg78e1c892015-08-17 16:03:17 +0300733 raw_spinlock_t lock;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300734 void __iomem *reg_base;
735 struct pinctrl_gpio_range *range;
Mika Westerbergfcc18de2015-02-23 14:53:13 +0200736 struct byt_gpio_pin_context *saved_context;
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300737 const struct byt_pinctrl_soc_data *soc_data;
738 struct byt_community *communities_copy;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300739};
740
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +0300741static const struct byt_pinctrl_soc_data *byt_soc_data[] = {
742 &byt_score_soc_data,
743 &byt_sus_soc_data,
744 &byt_ncore_soc_data,
745 NULL,
746};
747
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300748static struct byt_community *byt_get_community(struct byt_gpio *vg,
749 unsigned int pin)
Mathias Nymana5d811b2013-06-18 14:33:02 +0300750{
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300751 struct byt_community *comm;
752 int i;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300753
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300754 for (i = 0; i < vg->soc_data->ncommunities; i++) {
755 comm = vg->communities_copy + i;
756 if (pin < comm->pin_base + comm->npins && pin >= comm->pin_base)
757 return comm;
758 }
759
760 return NULL;
761}
762
763static void __iomem *byt_gpio_reg(struct byt_gpio *vg, unsigned int offset,
764 int reg)
765{
766 struct byt_community *comm = byt_get_community(vg, offset);
767 u32 reg_offset = 0;
768
769 if (!comm)
770 return NULL;
771
772 offset -= comm->pin_base;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300773 if (reg == BYT_INT_STAT_REG)
774 reg_offset = (offset / 32) * 4;
775 else
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300776 reg_offset = comm->pad_map[offset] * 16;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300777
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300778 return comm->reg_base + reg_offset + reg;
Mathias Nymana5d811b2013-06-18 14:33:02 +0300779}
780
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300781static int byt_get_groups_count(struct pinctrl_dev *pctldev)
Mika Westerberg95f09722015-02-23 14:53:11 +0200782{
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300783 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
784
785 return vg->soc_data->ngroups;
786}
787
788static const char *byt_get_group_name(struct pinctrl_dev *pctldev,
789 unsigned int selector)
790{
791 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
792
793 return vg->soc_data->groups[selector].name;
794}
795
796static int byt_get_group_pins(struct pinctrl_dev *pctldev,
797 unsigned int selector,
798 const unsigned int **pins,
799 unsigned int *num_pins)
800{
801 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
802
803 *pins = vg->soc_data->groups[selector].pins;
804 *num_pins = vg->soc_data->groups[selector].npins;
805
806 return 0;
807}
808
809static const struct pinctrl_ops byt_pinctrl_ops = {
810 .get_groups_count = byt_get_groups_count,
811 .get_group_name = byt_get_group_name,
812 .get_group_pins = byt_get_group_pins,
813};
814
815static int byt_get_functions_count(struct pinctrl_dev *pctldev)
816{
817 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
818
819 return vg->soc_data->nfunctions;
820}
821
822static const char *byt_get_function_name(struct pinctrl_dev *pctldev,
823 unsigned int selector)
824{
825 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
826
827 return vg->soc_data->functions[selector].name;
828}
829
830static int byt_get_function_groups(struct pinctrl_dev *pctldev,
831 unsigned int selector,
832 const char * const **groups,
833 unsigned int *num_groups)
834{
835 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
836
837 *groups = vg->soc_data->functions[selector].groups;
838 *num_groups = vg->soc_data->functions[selector].ngroups;
839
840 return 0;
841}
842
843static int byt_get_group_simple_mux(const struct byt_pingroup group,
844 const char *func_name,
845 unsigned short *func)
846{
847 int i;
848
849 for (i = 0; i < group.nfuncs; i++) {
850 if (!strcmp(group.simple_funcs[i].name, func_name)) {
851 *func = group.simple_funcs[i].func;
852 return 0;
853 }
854 }
855
856 return 1;
857}
858
859static int byt_get_group_mixed_mux(const struct byt_pingroup group,
860 const char *func_name,
861 const unsigned short **func)
862{
863 int i;
864
865 for (i = 0; i < group.nfuncs; i++) {
866 if (!strcmp(group.mixed_funcs[i].name, func_name)) {
867 *func = group.mixed_funcs[i].func_values;
868 return 0;
869 }
870 }
871
872 return 1;
873}
874
875static void byt_set_group_simple_mux(struct byt_gpio *vg,
876 const struct byt_pingroup group,
877 unsigned short func)
878{
879 unsigned long flags;
880 int i;
881
882 raw_spin_lock_irqsave(&vg->lock, flags);
883
884 for (i = 0; i < group.npins; i++) {
885 void __iomem *padcfg0;
886 u32 value;
887
888 padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
889 if (!padcfg0) {
890 dev_warn(&vg->pdev->dev,
891 "Group %s, pin %i not muxed (no padcfg0)\n",
892 group.name, i);
893 continue;
894 }
895
896 value = readl(padcfg0);
897 value &= ~BYT_PIN_MUX;
898 value |= func;
899 writel(value, padcfg0);
900 }
901
902 raw_spin_unlock_irqrestore(&vg->lock, flags);
903}
904
905static void byt_set_group_mixed_mux(struct byt_gpio *vg,
906 const struct byt_pingroup group,
907 const unsigned short *func)
908{
909 unsigned long flags;
910 int i;
911
912 raw_spin_lock_irqsave(&vg->lock, flags);
913
914 for (i = 0; i < group.npins; i++) {
915 void __iomem *padcfg0;
916 u32 value;
917
918 padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
919 if (!padcfg0) {
920 dev_warn(&vg->pdev->dev,
921 "Group %s, pin %i not muxed (no padcfg0)\n",
922 group.name, i);
923 continue;
924 }
925
926 value = readl(padcfg0);
927 value &= ~BYT_PIN_MUX;
928 value |= func[i];
929 writel(value, padcfg0);
930 }
931
932 raw_spin_unlock_irqrestore(&vg->lock, flags);
933}
934
935static int byt_set_mux(struct pinctrl_dev *pctldev, unsigned int func_selector,
936 unsigned int group_selector)
937{
938 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
939 const struct byt_function func = vg->soc_data->functions[func_selector];
940 const struct byt_pingroup group = vg->soc_data->groups[group_selector];
941 const unsigned short *mixed_func;
942 unsigned short simple_func;
943 int ret = 1;
944
945 if (group.has_simple_funcs)
946 ret = byt_get_group_simple_mux(group, func.name, &simple_func);
947 else
948 ret = byt_get_group_mixed_mux(group, func.name, &mixed_func);
949
950 if (ret)
951 byt_set_group_simple_mux(vg, group, BYT_DEFAULT_GPIO_MUX);
952 else if (group.has_simple_funcs)
953 byt_set_group_simple_mux(vg, group, simple_func);
954 else
955 byt_set_group_mixed_mux(vg, group, mixed_func);
956
957 return 0;
958}
959
960static u32 byt_get_gpio_mux(struct byt_gpio *vg, unsigned offset)
961{
962 /* SCORE pin 92-93 */
963 if (!strcmp(vg->soc_data->uid, BYT_SCORE_ACPI_UID) &&
964 offset >= 92 && offset <= 93)
965 return 1;
966
967 /* SUS pin 11-21 */
968 if (!strcmp(vg->soc_data->uid, BYT_SUS_ACPI_UID) &&
969 offset >= 11 && offset <= 21)
970 return 1;
971
972 return 0;
973}
974
975static void byt_gpio_clear_triggering(struct byt_gpio *vg, unsigned int offset)
976{
977 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mika Westerberg95f09722015-02-23 14:53:11 +0200978 unsigned long flags;
979 u32 value;
980
Mika Westerberg78e1c892015-08-17 16:03:17 +0300981 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg95f09722015-02-23 14:53:11 +0200982 value = readl(reg);
983 value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
984 writel(value, reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +0300985 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg95f09722015-02-23 14:53:11 +0200986}
987
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300988static int byt_gpio_request_enable(struct pinctrl_dev *pctl_dev,
989 struct pinctrl_gpio_range *range,
990 unsigned int offset)
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800991{
Cristina Ciocanc501d0b2016-04-01 14:00:03 +0300992 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
993 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mika Westerbergf8323b62015-02-23 14:53:10 +0200994 u32 value, gpio_mux;
Mika Westerberg39ce8152015-08-04 15:03:14 +0300995 unsigned long flags;
996
Mika Westerberg78e1c892015-08-17 16:03:17 +0300997 raw_spin_lock_irqsave(&vg->lock, flags);
Chew, Kean Ho42bd0072014-03-06 21:59:49 +0800998
999 /*
1000 * In most cases, func pin mux 000 means GPIO function.
1001 * But, some pins may have func pin mux 001 represents
Mika Westerbergf8323b62015-02-23 14:53:10 +02001002 * GPIO function.
1003 *
1004 * Because there are devices out there where some pins were not
1005 * configured correctly we allow changing the mux value from
1006 * request (but print out warning about that).
Chew, Kean Ho42bd0072014-03-06 21:59:49 +08001007 */
1008 value = readl(reg) & BYT_PIN_MUX;
Mika Westerbergf8323b62015-02-23 14:53:10 +02001009 gpio_mux = byt_get_gpio_mux(vg, offset);
1010 if (WARN_ON(gpio_mux != value)) {
Mika Westerbergf8323b62015-02-23 14:53:10 +02001011 value = readl(reg) & ~BYT_PIN_MUX;
1012 value |= gpio_mux;
1013 writel(value, reg);
Mika Westerbergf8323b62015-02-23 14:53:10 +02001014
1015 dev_warn(&vg->pdev->dev,
1016 "pin %u forcibly re-configured as GPIO\n", offset);
Chew, Kean Ho42bd0072014-03-06 21:59:49 +08001017 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001018
Mika Westerberg78e1c892015-08-17 16:03:17 +03001019 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001020
Mathias Nymana5d811b2013-06-18 14:33:02 +03001021 pm_runtime_get(&vg->pdev->dev);
1022
1023 return 0;
1024}
1025
1026static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
1027{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001028 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001029
Mika Westerberg95f09722015-02-23 14:53:11 +02001030 byt_gpio_clear_triggering(vg, offset);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001031 pm_runtime_put(&vg->pdev->dev);
1032}
1033
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001034static void byt_gpio_disable_free(struct pinctrl_dev *pctl_dev,
1035 struct pinctrl_gpio_range *range,
1036 unsigned int offset)
1037{
1038 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1039
1040 byt_gpio_clear_triggering(vg, offset);
1041 pm_runtime_put(&vg->pdev->dev);
1042}
1043
1044static int byt_gpio_set_direction(struct pinctrl_dev *pctl_dev,
1045 struct pinctrl_gpio_range *range,
1046 unsigned int offset,
1047 bool input)
1048{
1049 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1050 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1051 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1052 unsigned long flags;
1053 u32 value;
1054
1055 raw_spin_lock_irqsave(&vg->lock, flags);
1056
1057 value = readl(val_reg);
1058 value &= ~BYT_DIR_MASK;
1059 if (input)
1060 value |= BYT_OUTPUT_EN;
1061 else
1062 /*
1063 * Before making any direction modifications, do a check if gpio
1064 * is set for direct IRQ. On baytrail, setting GPIO to output
1065 * does not make sense, so let's at least warn the caller before
1066 * they shoot themselves in the foot.
1067 */
1068 WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
1069 "Potential Error: Setting GPIO with direct_irq_en to output");
1070 writel(value, val_reg);
1071
1072 raw_spin_unlock_irqrestore(&vg->lock, flags);
1073
1074 return 0;
1075}
1076
1077static const struct pinmux_ops byt_pinmux_ops = {
1078 .get_functions_count = byt_get_functions_count,
1079 .get_function_name = byt_get_function_name,
1080 .get_function_groups = byt_get_function_groups,
1081 .set_mux = byt_set_mux,
1082 .gpio_request_enable = byt_gpio_request_enable,
1083 .gpio_disable_free = byt_gpio_disable_free,
1084 .gpio_set_direction = byt_gpio_set_direction,
1085};
1086
1087static int byt_gpio_request(struct gpio_chip *chip, unsigned int offset)
1088{
1089 struct byt_gpio *vg = gpiochip_get_data(chip);
1090 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1091 u32 value, gpio_mux;
1092 unsigned long flags;
1093
1094 raw_spin_lock_irqsave(&vg->lock, flags);
1095
1096 /*
1097 * In most cases, func pin mux 000 means GPIO function.
1098 * But, some pins may have func pin mux 001 represents
1099 * GPIO function.
1100 *
1101 * Because there are devices out there where some pins were not
1102 * configured correctly we allow changing the mux value from
1103 * request (but print out warning about that).
1104 */
1105 value = readl(reg) & BYT_PIN_MUX;
1106 gpio_mux = byt_get_gpio_mux(vg, offset);
1107 if (WARN_ON(gpio_mux != value)) {
1108 value = readl(reg) & ~BYT_PIN_MUX;
1109 value |= gpio_mux;
1110 writel(value, reg);
1111
1112 dev_warn(&vg->pdev->dev,
1113 "pin %u forcibly re-configured as GPIO\n", offset);
1114 }
1115
1116 raw_spin_unlock_irqrestore(&vg->lock, flags);
1117
1118 pm_runtime_get(&vg->pdev->dev);
1119
1120 return 0;
1121}
1122
Mathias Nymana5d811b2013-06-18 14:33:02 +03001123static int byt_irq_type(struct irq_data *d, unsigned type)
1124{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001125 struct byt_gpio *vg = gpiochip_get_data(irq_data_get_irq_chip_data(d));
Mathias Nymana5d811b2013-06-18 14:33:02 +03001126 u32 offset = irqd_to_hwirq(d);
1127 u32 value;
1128 unsigned long flags;
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001129 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001130
1131 if (offset >= vg->chip.ngpio)
1132 return -EINVAL;
1133
Mika Westerberg78e1c892015-08-17 16:03:17 +03001134 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001135 value = readl(reg);
1136
Loic Poulain3a71c052014-09-26 16:14:51 +02001137 WARN(value & BYT_DIRECT_IRQ_EN,
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001138 "Bad pad config for io mode, force direct_irq_en bit clearing");
Loic Poulain3a71c052014-09-26 16:14:51 +02001139
Mathias Nymana5d811b2013-06-18 14:33:02 +03001140 /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
1141 * are used to indicate high and low level triggering
1142 */
Loic Poulain3a71c052014-09-26 16:14:51 +02001143 value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
1144 BYT_TRIG_LVL);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001145
Mathias Nymana5d811b2013-06-18 14:33:02 +03001146 writel(value, reg);
1147
Mika Westerberg31e43292015-02-23 14:53:12 +02001148 if (type & IRQ_TYPE_EDGE_BOTH)
Thomas Gleixnerf3a085b2015-06-23 15:52:44 +02001149 irq_set_handler_locked(d, handle_edge_irq);
Mika Westerberg31e43292015-02-23 14:53:12 +02001150 else if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerf3a085b2015-06-23 15:52:44 +02001151 irq_set_handler_locked(d, handle_level_irq);
Mika Westerberg31e43292015-02-23 14:53:12 +02001152
Mika Westerberg78e1c892015-08-17 16:03:17 +03001153 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001154
1155 return 0;
1156}
1157
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001158static void byt_get_pull_strength(u32 reg, u16 *strength)
1159{
1160 switch (reg & BYT_PULL_STR_MASK) {
1161 case BYT_PULL_STR_2K:
1162 *strength = 2000;
1163 break;
1164 case BYT_PULL_STR_10K:
1165 *strength = 10000;
1166 break;
1167 case BYT_PULL_STR_20K:
1168 *strength = 20000;
1169 break;
1170 case BYT_PULL_STR_40K:
1171 *strength = 40000;
1172 break;
1173 }
1174}
1175
1176static int byt_set_pull_strength(u32 *reg, u16 strength)
1177{
1178 *reg &= ~BYT_PULL_STR_MASK;
1179
1180 switch (strength) {
1181 case 2000:
1182 *reg |= BYT_PULL_STR_2K;
1183 break;
1184 case 10000:
1185 *reg |= BYT_PULL_STR_10K;
1186 break;
1187 case 20000:
1188 *reg |= BYT_PULL_STR_20K;
1189 break;
1190 case 40000:
1191 *reg |= BYT_PULL_STR_40K;
1192 break;
1193 default:
1194 return -EINVAL;
1195 }
1196
1197 return 0;
1198}
1199
1200static int byt_pin_config_get(struct pinctrl_dev *pctl_dev, unsigned int offset,
1201 unsigned long *config)
1202{
1203 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1204 enum pin_config_param param = pinconf_to_config_param(*config);
1205 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1206 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1207 unsigned long flags;
1208 u32 conf, pull, val;
1209 u16 arg = 0;
1210
1211 raw_spin_lock_irqsave(&vg->lock, flags);
1212 conf = readl(conf_reg);
1213 pull = conf & BYT_PULL_ASSIGN_MASK;
1214 val = readl(val_reg);
1215 raw_spin_unlock_irqrestore(&vg->lock, flags);
1216
1217 switch (param) {
1218 case PIN_CONFIG_BIAS_DISABLE:
1219 if (pull)
1220 return -EINVAL;
1221 break;
1222 case PIN_CONFIG_BIAS_PULL_DOWN:
1223 /* Pull assignment is only applicable in input mode */
1224 if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_DOWN)
1225 return -EINVAL;
1226
1227 byt_get_pull_strength(conf, &arg);
1228
1229 break;
1230 case PIN_CONFIG_BIAS_PULL_UP:
1231 /* Pull assignment is only applicable in input mode */
1232 if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_UP)
1233 return -EINVAL;
1234
1235 byt_get_pull_strength(conf, &arg);
1236
1237 break;
1238 default:
1239 return -ENOTSUPP;
1240 }
1241
1242 *config = pinconf_to_config_packed(param, arg);
1243
1244 return 0;
1245}
1246
1247static int byt_pin_config_set(struct pinctrl_dev *pctl_dev,
1248 unsigned int offset,
1249 unsigned long *configs,
1250 unsigned int num_configs)
1251{
1252 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1253 unsigned int param, arg;
1254 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1255 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1256 unsigned long flags;
1257 u32 conf, val;
1258 int i, ret = 0;
1259
1260 raw_spin_lock_irqsave(&vg->lock, flags);
1261
1262 conf = readl(conf_reg);
1263 val = readl(val_reg);
1264
1265 for (i = 0; i < num_configs; i++) {
1266 param = pinconf_to_config_param(configs[i]);
1267 arg = pinconf_to_config_argument(configs[i]);
1268
1269 switch (param) {
1270 case PIN_CONFIG_BIAS_DISABLE:
1271 conf &= ~BYT_PULL_ASSIGN_MASK;
1272 break;
1273 case PIN_CONFIG_BIAS_PULL_DOWN:
1274 /* Set default strength value in case none is given */
1275 if (arg == 1)
1276 arg = 2000;
1277
1278 /*
1279 * Pull assignment is only applicable in input mode. If
1280 * chip is not in input mode, set it and warn about it.
1281 */
1282 if (val & BYT_INPUT_EN) {
1283 val &= ~BYT_INPUT_EN;
1284 writel(val, val_reg);
1285 dev_warn(&vg->pdev->dev,
1286 "pin %u forcibly set to input mode\n",
1287 offset);
1288 }
1289
1290 conf &= ~BYT_PULL_ASSIGN_MASK;
1291 conf |= BYT_PULL_ASSIGN_DOWN;
1292 ret = byt_set_pull_strength(&conf, arg);
1293
1294 break;
1295 case PIN_CONFIG_BIAS_PULL_UP:
1296 /* Set default strength value in case none is given */
1297 if (arg == 1)
1298 arg = 2000;
1299
1300 /*
1301 * Pull assignment is only applicable in input mode. If
1302 * chip is not in input mode, set it and warn about it.
1303 */
1304 if (val & BYT_INPUT_EN) {
1305 val &= ~BYT_INPUT_EN;
1306 writel(val, val_reg);
1307 dev_warn(&vg->pdev->dev,
1308 "pin %u forcibly set to input mode\n",
1309 offset);
1310 }
1311
1312 conf &= ~BYT_PULL_ASSIGN_MASK;
1313 conf |= BYT_PULL_ASSIGN_UP;
1314 ret = byt_set_pull_strength(&conf, arg);
1315
1316 break;
1317 default:
1318 ret = -ENOTSUPP;
1319 }
1320
1321 if (ret)
1322 break;
1323 }
1324
1325 if (!ret)
1326 writel(conf, conf_reg);
1327
1328 raw_spin_unlock_irqrestore(&vg->lock, flags);
1329
1330 return ret;
1331}
1332
1333static const struct pinconf_ops byt_pinconf_ops = {
1334 .is_generic = true,
1335 .pin_config_get = byt_pin_config_get,
1336 .pin_config_set = byt_pin_config_set,
1337};
1338
1339static const struct pinctrl_desc byt_pinctrl_desc = {
1340 .pctlops = &byt_pinctrl_ops,
1341 .pmxops = &byt_pinmux_ops,
1342 .confops = &byt_pinconf_ops,
1343 .owner = THIS_MODULE,
1344};
1345
Mathias Nymana5d811b2013-06-18 14:33:02 +03001346static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
1347{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001348 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001349 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001350 unsigned long flags;
1351 u32 val;
1352
Mika Westerberg78e1c892015-08-17 16:03:17 +03001353 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001354 val = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001355 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001356
Linus Walleij3bde8772015-12-21 16:17:20 +01001357 return !!(val & BYT_LEVEL);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001358}
1359
1360static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1361{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001362 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001363 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001364 unsigned long flags;
1365 u32 old_val;
1366
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001367 if (!reg)
1368 return;
1369
Mika Westerberg78e1c892015-08-17 16:03:17 +03001370 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001371 old_val = readl(reg);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001372 if (value)
1373 writel(old_val | BYT_LEVEL, reg);
1374 else
1375 writel(old_val & ~BYT_LEVEL, reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001376 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001377}
1378
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001379static int byt_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001380{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001381 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001382 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001383 unsigned long flags;
1384 u32 value;
1385
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001386 if (!reg)
1387 return -EINVAL;
1388
Mika Westerberg78e1c892015-08-17 16:03:17 +03001389 raw_spin_lock_irqsave(&vg->lock, flags);
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001390 value = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001391 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001392
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001393 if (!(value & BYT_OUTPUT_EN))
1394 return GPIOF_DIR_OUT;
1395 if (!(value & BYT_INPUT_EN))
1396 return GPIOF_DIR_IN;
1397
1398 return -EINVAL;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001399}
1400
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001401static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001402{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001403 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001404 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001405 unsigned long flags;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001406
Mika Westerberg78e1c892015-08-17 16:03:17 +03001407 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001408
Eric Ernstff998352014-06-12 11:06:20 -07001409 /*
1410 * Before making any direction modifications, do a check if gpio
1411 * is set for direct IRQ. On baytrail, setting GPIO to output does
1412 * not make sense, so let's at least warn the caller before they shoot
1413 * themselves in the foot.
1414 */
1415 WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
1416 "Potential Error: Setting GPIO with direct_irq_en to output");
1417
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001418 return pinctrl_gpio_direction_input(chip->base + offset);
1419}
Andy Shevchenko496940c2013-07-10 14:55:40 +03001420
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001421static int byt_gpio_direction_output(struct gpio_chip *chip,
1422 unsigned int offset, int value)
1423{
1424 int ret = pinctrl_gpio_direction_output(chip->base + offset);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001425
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001426 if (ret)
1427 return ret;
1428
1429 byt_gpio_set(chip, offset, value);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001430
1431 return 0;
1432}
1433
1434static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1435{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001436 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001437 int i;
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001438 u32 conf0, val;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001439
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001440 for (i = 0; i < vg->soc_data->npins; i++) {
1441 const struct byt_community *comm;
Mika Westerberg3ff95882014-05-16 12:18:29 +03001442 const char *pull_str = NULL;
1443 const char *pull = NULL;
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001444 void __iomem *reg;
Mika Westerberg78e1c892015-08-17 16:03:17 +03001445 unsigned long flags;
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001446 const char *label;
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001447 unsigned int pin;
Mika Westerberg78e1c892015-08-17 16:03:17 +03001448
1449 raw_spin_lock_irqsave(&vg->lock, flags);
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001450 pin = vg->soc_data->pins[i].number;
1451 reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
1452 if (!reg) {
1453 seq_printf(s,
1454 "Could not retrieve pin %i conf0 reg\n",
1455 pin);
1456 continue;
1457 }
1458 conf0 = readl(reg);
1459
1460 reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
1461 if (!reg) {
1462 seq_printf(s,
1463 "Could not retrieve pin %i val reg\n", pin);
1464 }
1465 val = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001466 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001467
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001468 comm = byt_get_community(vg, pin);
1469 if (!comm) {
1470 seq_printf(s,
1471 "Could not get community for pin %i\n", pin);
1472 continue;
1473 }
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001474 label = gpiochip_is_requested(chip, i);
1475 if (!label)
1476 label = "Unrequested";
1477
Mika Westerberg3ff95882014-05-16 12:18:29 +03001478 switch (conf0 & BYT_PULL_ASSIGN_MASK) {
1479 case BYT_PULL_ASSIGN_UP:
1480 pull = "up";
1481 break;
1482 case BYT_PULL_ASSIGN_DOWN:
1483 pull = "down";
1484 break;
1485 }
1486
1487 switch (conf0 & BYT_PULL_STR_MASK) {
1488 case BYT_PULL_STR_2K:
1489 pull_str = "2k";
1490 break;
1491 case BYT_PULL_STR_10K:
1492 pull_str = "10k";
1493 break;
1494 case BYT_PULL_STR_20K:
1495 pull_str = "20k";
1496 break;
1497 case BYT_PULL_STR_40K:
1498 pull_str = "40k";
1499 break;
1500 }
1501
Mathias Nymana5d811b2013-06-18 14:33:02 +03001502 seq_printf(s,
Mika Westerberg3ff95882014-05-16 12:18:29 +03001503 " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001504 pin,
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001505 label,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001506 val & BYT_INPUT_EN ? " " : "in",
1507 val & BYT_OUTPUT_EN ? " " : "out",
1508 val & BYT_LEVEL ? "hi" : "lo",
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001509 comm->pad_map[i], comm->pad_map[i] * 32,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001510 conf0 & 0x7,
Mika Westerberg3ff95882014-05-16 12:18:29 +03001511 conf0 & BYT_TRIG_NEG ? " fall" : " ",
1512 conf0 & BYT_TRIG_POS ? " rise" : " ",
1513 conf0 & BYT_TRIG_LVL ? " level" : " ");
1514
1515 if (pull && pull_str)
1516 seq_printf(s, " %-4s %-3s", pull, pull_str);
1517 else
1518 seq_puts(s, " ");
1519
1520 if (conf0 & BYT_IODEN)
1521 seq_puts(s, " open-drain");
1522
1523 seq_puts(s, "\n");
Mathias Nymana5d811b2013-06-18 14:33:02 +03001524 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001525}
1526
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001527static void byt_gpio_irq_handler(struct irq_desc *desc)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001528{
1529 struct irq_data *data = irq_desc_get_irq_data(desc);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001530 struct byt_gpio *vg = gpiochip_get_data(irq_desc_get_handler_data(desc));
Mathias Nymana5d811b2013-06-18 14:33:02 +03001531 struct irq_chip *chip = irq_data_get_irq_chip(data);
Mika Westerberg31e43292015-02-23 14:53:12 +02001532 u32 base, pin;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001533 void __iomem *reg;
Mika Westerberg31e43292015-02-23 14:53:12 +02001534 unsigned long pending;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001535 unsigned virq;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001536
1537 /* check from GPIO controller which pin triggered the interrupt */
1538 for (base = 0; base < vg->chip.ngpio; base += 32) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001539 reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
Mika Westerberg31e43292015-02-23 14:53:12 +02001540 pending = readl(reg);
1541 for_each_set_bit(pin, &pending, 32) {
Mika Westerberge1ee5c52014-07-25 09:54:47 +03001542 virq = irq_find_mapping(vg->chip.irqdomain, base + pin);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001543 generic_handle_irq(virq);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001544 }
1545 }
1546 chip->irq_eoi(data);
1547}
1548
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001549static const struct gpio_chip byt_gpio_chip = {
1550 .owner = THIS_MODULE,
1551 .request = gpiochip_generic_request,
1552 .free = gpiochip_generic_free,
1553 .get_direction = byt_gpio_get_direction,
1554 .direction_input = byt_gpio_direction_input,
1555 .direction_output = byt_gpio_direction_output,
1556 .get = byt_gpio_get,
1557 .set = byt_gpio_set,
1558 .dbg_show = byt_gpio_dbg_show,
1559};
1560
Mika Westerberg31e43292015-02-23 14:53:12 +02001561static void byt_irq_ack(struct irq_data *d)
1562{
1563 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001564 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001565 unsigned offset = irqd_to_hwirq(d);
1566 void __iomem *reg;
1567
Mika Westerberg78e1c892015-08-17 16:03:17 +03001568 raw_spin_lock(&vg->lock);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001569 reg = byt_gpio_reg(vg, offset, BYT_INT_STAT_REG);
Mika Westerberg31e43292015-02-23 14:53:12 +02001570 writel(BIT(offset % 32), reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001571 raw_spin_unlock(&vg->lock);
Mika Westerberg31e43292015-02-23 14:53:12 +02001572}
1573
Mathias Nymana5d811b2013-06-18 14:33:02 +03001574static void byt_irq_unmask(struct irq_data *d)
1575{
Mika Westerberg31e43292015-02-23 14:53:12 +02001576 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001577 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001578 unsigned offset = irqd_to_hwirq(d);
1579 unsigned long flags;
1580 void __iomem *reg;
1581 u32 value;
1582
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001583 reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001584
1585 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg31e43292015-02-23 14:53:12 +02001586 value = readl(reg);
1587
1588 switch (irqd_get_trigger_type(d)) {
1589 case IRQ_TYPE_LEVEL_HIGH:
1590 value |= BYT_TRIG_LVL;
1591 case IRQ_TYPE_EDGE_RISING:
1592 value |= BYT_TRIG_POS;
1593 break;
1594 case IRQ_TYPE_LEVEL_LOW:
1595 value |= BYT_TRIG_LVL;
1596 case IRQ_TYPE_EDGE_FALLING:
1597 value |= BYT_TRIG_NEG;
1598 break;
1599 case IRQ_TYPE_EDGE_BOTH:
1600 value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
1601 break;
1602 }
1603
1604 writel(value, reg);
1605
Mika Westerberg78e1c892015-08-17 16:03:17 +03001606 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001607}
1608
1609static void byt_irq_mask(struct irq_data *d)
1610{
Mika Westerberg31e43292015-02-23 14:53:12 +02001611 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001612 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001613
1614 byt_gpio_clear_triggering(vg, irqd_to_hwirq(d));
Mathias Nymana5d811b2013-06-18 14:33:02 +03001615}
1616
1617static struct irq_chip byt_irqchip = {
1618 .name = "BYT-GPIO",
Mika Westerberg31e43292015-02-23 14:53:12 +02001619 .irq_ack = byt_irq_ack,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001620 .irq_mask = byt_irq_mask,
1621 .irq_unmask = byt_irq_unmask,
1622 .irq_set_type = byt_irq_type,
Mathias Nyman41939e62014-08-11 16:51:55 +03001623 .flags = IRQCHIP_SKIP_SET_WAKE,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001624};
1625
1626static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
1627{
1628 void __iomem *reg;
1629 u32 base, value;
Mika Westerberg95f09722015-02-23 14:53:11 +02001630 int i;
1631
1632 /*
1633 * Clear interrupt triggers for all pins that are GPIOs and
1634 * do not use direct IRQ mode. This will prevent spurious
1635 * interrupts from misconfigured pins.
1636 */
1637 for (i = 0; i < vg->chip.ngpio; i++) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001638 value = readl(byt_gpio_reg(vg, i, BYT_CONF0_REG));
Mika Westerberg95f09722015-02-23 14:53:11 +02001639 if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i) &&
1640 !(value & BYT_DIRECT_IRQ_EN)) {
1641 byt_gpio_clear_triggering(vg, i);
1642 dev_dbg(&vg->pdev->dev, "disabling GPIO %d\n", i);
1643 }
1644 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001645
1646 /* clear interrupt status trigger registers */
1647 for (base = 0; base < vg->chip.ngpio; base += 32) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001648 reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001649 writel(0xffffffff, reg);
1650 /* make sure trigger bits are cleared, if not then a pin
1651 might be misconfigured in bios */
1652 value = readl(reg);
1653 if (value)
1654 dev_err(&vg->pdev->dev,
1655 "GPIO interrupt error, pins misconfigured\n");
1656 }
1657}
1658
Mathias Nymana5d811b2013-06-18 14:33:02 +03001659static int byt_gpio_probe(struct platform_device *pdev)
1660{
1661 struct byt_gpio *vg;
1662 struct gpio_chip *gc;
1663 struct resource *mem_rc, *irq_rc;
1664 struct device *dev = &pdev->dev;
1665 struct acpi_device *acpi_dev;
1666 struct pinctrl_gpio_range *range;
1667 acpi_handle handle = ACPI_HANDLE(dev);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001668 int ret;
1669
1670 if (acpi_bus_get_device(handle, &acpi_dev))
1671 return -ENODEV;
1672
1673 vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
1674 if (!vg) {
1675 dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
1676 return -ENOMEM;
1677 }
1678
1679 for (range = byt_ranges; range->name; range++) {
1680 if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
1681 vg->chip.ngpio = range->npins;
1682 vg->range = range;
1683 break;
1684 }
1685 }
1686
1687 if (!vg->chip.ngpio || !vg->range)
1688 return -ENODEV;
1689
1690 vg->pdev = pdev;
1691 platform_set_drvdata(pdev, vg);
1692
1693 mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1694 vg->reg_base = devm_ioremap_resource(dev, mem_rc);
1695 if (IS_ERR(vg->reg_base))
1696 return PTR_ERR(vg->reg_base);
1697
Mika Westerberg78e1c892015-08-17 16:03:17 +03001698 raw_spin_lock_init(&vg->lock);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001699
1700 gc = &vg->chip;
1701 gc->label = dev_name(&pdev->dev);
1702 gc->owner = THIS_MODULE;
1703 gc->request = byt_gpio_request;
1704 gc->free = byt_gpio_free;
1705 gc->direction_input = byt_gpio_direction_input;
1706 gc->direction_output = byt_gpio_direction_output;
1707 gc->get = byt_gpio_get;
1708 gc->set = byt_gpio_set;
1709 gc->dbg_show = byt_gpio_dbg_show;
1710 gc->base = -1;
Linus Walleij9fb1f392013-12-04 14:42:46 +01001711 gc->can_sleep = false;
Linus Walleij58383c782015-11-04 09:56:26 +01001712 gc->parent = dev;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001713
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001714#ifdef CONFIG_PM_SLEEP
1715 vg->saved_context = devm_kcalloc(&pdev->dev, gc->ngpio,
1716 sizeof(*vg->saved_context), GFP_KERNEL);
1717#endif
1718
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001719 ret = gpiochip_add_data(gc, vg);
Jin Yao605a7bc2014-05-15 18:28:47 +03001720 if (ret) {
1721 dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
1722 return ret;
1723 }
1724
Mika Westerberge1ee5c52014-07-25 09:54:47 +03001725 /* set up interrupts */
1726 irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1727 if (irq_rc && irq_rc->start) {
1728 byt_gpio_irq_init_hw(vg);
1729 ret = gpiochip_irqchip_add(gc, &byt_irqchip, 0,
1730 handle_simple_irq, IRQ_TYPE_NONE);
1731 if (ret) {
1732 dev_err(dev, "failed to add irqchip\n");
1733 gpiochip_remove(gc);
1734 return ret;
1735 }
1736
1737 gpiochip_set_chained_irqchip(gc, &byt_irqchip,
1738 (unsigned)irq_rc->start,
1739 byt_gpio_irq_handler);
1740 }
1741
Mathias Nymana5d811b2013-06-18 14:33:02 +03001742 pm_runtime_enable(dev);
1743
1744 return 0;
1745}
1746
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001747#ifdef CONFIG_PM_SLEEP
1748static int byt_gpio_suspend(struct device *dev)
1749{
1750 struct platform_device *pdev = to_platform_device(dev);
1751 struct byt_gpio *vg = platform_get_drvdata(pdev);
1752 int i;
1753
1754 for (i = 0; i < vg->chip.ngpio; i++) {
1755 void __iomem *reg;
1756 u32 value;
1757
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001758 reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001759 value = readl(reg) & BYT_CONF0_RESTORE_MASK;
1760 vg->saved_context[i].conf0 = value;
1761
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001762 reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001763 value = readl(reg) & BYT_VAL_RESTORE_MASK;
1764 vg->saved_context[i].val = value;
1765 }
1766
1767 return 0;
1768}
1769
1770static int byt_gpio_resume(struct device *dev)
1771{
1772 struct platform_device *pdev = to_platform_device(dev);
1773 struct byt_gpio *vg = platform_get_drvdata(pdev);
1774 int i;
1775
1776 for (i = 0; i < vg->chip.ngpio; i++) {
1777 void __iomem *reg;
1778 u32 value;
1779
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001780 reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001781 value = readl(reg);
1782 if ((value & BYT_CONF0_RESTORE_MASK) !=
1783 vg->saved_context[i].conf0) {
1784 value &= ~BYT_CONF0_RESTORE_MASK;
1785 value |= vg->saved_context[i].conf0;
1786 writel(value, reg);
1787 dev_info(dev, "restored pin %d conf0 %#08x", i, value);
1788 }
1789
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001790 reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001791 value = readl(reg);
1792 if ((value & BYT_VAL_RESTORE_MASK) !=
1793 vg->saved_context[i].val) {
1794 u32 v;
1795
1796 v = value & ~BYT_VAL_RESTORE_MASK;
1797 v |= vg->saved_context[i].val;
1798 if (v != value) {
1799 writel(v, reg);
1800 dev_dbg(dev, "restored pin %d val %#08x\n",
1801 i, v);
1802 }
1803 }
1804 }
1805
1806 return 0;
1807}
1808#endif
1809
Mika Westerbergec879f12015-10-13 17:51:26 +03001810#ifdef CONFIG_PM
Mathias Nymana5d811b2013-06-18 14:33:02 +03001811static int byt_gpio_runtime_suspend(struct device *dev)
1812{
1813 return 0;
1814}
1815
1816static int byt_gpio_runtime_resume(struct device *dev)
1817{
1818 return 0;
1819}
Mika Westerbergec879f12015-10-13 17:51:26 +03001820#endif
Mathias Nymana5d811b2013-06-18 14:33:02 +03001821
1822static const struct dev_pm_ops byt_gpio_pm_ops = {
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001823 SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
1824 SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
1825 NULL)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001826};
1827
1828static const struct acpi_device_id byt_gpio_acpi_match[] = {
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +03001829 { "INT33B2", (kernel_ulong_t)byt_soc_data },
1830 { "INT33FC", (kernel_ulong_t)byt_soc_data },
Mathias Nymana5d811b2013-06-18 14:33:02 +03001831 { }
1832};
1833MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
1834
1835static int byt_gpio_remove(struct platform_device *pdev)
1836{
1837 struct byt_gpio *vg = platform_get_drvdata(pdev);
Andy Shevchenkoec243322013-07-10 14:55:36 +03001838
Mathias Nymana5d811b2013-06-18 14:33:02 +03001839 pm_runtime_disable(&pdev->dev);
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001840 gpiochip_remove(&vg->chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001841
1842 return 0;
1843}
1844
1845static struct platform_driver byt_gpio_driver = {
1846 .probe = byt_gpio_probe,
1847 .remove = byt_gpio_remove,
1848 .driver = {
1849 .name = "byt_gpio",
Mathias Nymana5d811b2013-06-18 14:33:02 +03001850 .pm = &byt_gpio_pm_ops,
1851 .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
1852 },
1853};
1854
1855static int __init byt_gpio_init(void)
1856{
1857 return platform_driver_register(&byt_gpio_driver);
1858}
Mathias Nymana5d811b2013-06-18 14:33:02 +03001859subsys_initcall(byt_gpio_init);
Felipe Balbi9067bbe2014-10-13 15:50:08 -05001860
1861static void __exit byt_gpio_exit(void)
1862{
1863 platform_driver_unregister(&byt_gpio_driver);
1864}
1865module_exit(byt_gpio_exit);