blob: f7c752b4bb6d540d96a12d530092ec9d5c5ea01a [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
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001123static void byt_get_pull_strength(u32 reg, u16 *strength)
1124{
1125 switch (reg & BYT_PULL_STR_MASK) {
1126 case BYT_PULL_STR_2K:
1127 *strength = 2000;
1128 break;
1129 case BYT_PULL_STR_10K:
1130 *strength = 10000;
1131 break;
1132 case BYT_PULL_STR_20K:
1133 *strength = 20000;
1134 break;
1135 case BYT_PULL_STR_40K:
1136 *strength = 40000;
1137 break;
1138 }
1139}
1140
1141static int byt_set_pull_strength(u32 *reg, u16 strength)
1142{
1143 *reg &= ~BYT_PULL_STR_MASK;
1144
1145 switch (strength) {
1146 case 2000:
1147 *reg |= BYT_PULL_STR_2K;
1148 break;
1149 case 10000:
1150 *reg |= BYT_PULL_STR_10K;
1151 break;
1152 case 20000:
1153 *reg |= BYT_PULL_STR_20K;
1154 break;
1155 case 40000:
1156 *reg |= BYT_PULL_STR_40K;
1157 break;
1158 default:
1159 return -EINVAL;
1160 }
1161
1162 return 0;
1163}
1164
1165static int byt_pin_config_get(struct pinctrl_dev *pctl_dev, unsigned int offset,
1166 unsigned long *config)
1167{
1168 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1169 enum pin_config_param param = pinconf_to_config_param(*config);
1170 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1171 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1172 unsigned long flags;
1173 u32 conf, pull, val;
1174 u16 arg = 0;
1175
1176 raw_spin_lock_irqsave(&vg->lock, flags);
1177 conf = readl(conf_reg);
1178 pull = conf & BYT_PULL_ASSIGN_MASK;
1179 val = readl(val_reg);
1180 raw_spin_unlock_irqrestore(&vg->lock, flags);
1181
1182 switch (param) {
1183 case PIN_CONFIG_BIAS_DISABLE:
1184 if (pull)
1185 return -EINVAL;
1186 break;
1187 case PIN_CONFIG_BIAS_PULL_DOWN:
1188 /* Pull assignment is only applicable in input mode */
1189 if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_DOWN)
1190 return -EINVAL;
1191
1192 byt_get_pull_strength(conf, &arg);
1193
1194 break;
1195 case PIN_CONFIG_BIAS_PULL_UP:
1196 /* Pull assignment is only applicable in input mode */
1197 if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_UP)
1198 return -EINVAL;
1199
1200 byt_get_pull_strength(conf, &arg);
1201
1202 break;
1203 default:
1204 return -ENOTSUPP;
1205 }
1206
1207 *config = pinconf_to_config_packed(param, arg);
1208
1209 return 0;
1210}
1211
1212static int byt_pin_config_set(struct pinctrl_dev *pctl_dev,
1213 unsigned int offset,
1214 unsigned long *configs,
1215 unsigned int num_configs)
1216{
1217 struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
1218 unsigned int param, arg;
1219 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1220 void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1221 unsigned long flags;
1222 u32 conf, val;
1223 int i, ret = 0;
1224
1225 raw_spin_lock_irqsave(&vg->lock, flags);
1226
1227 conf = readl(conf_reg);
1228 val = readl(val_reg);
1229
1230 for (i = 0; i < num_configs; i++) {
1231 param = pinconf_to_config_param(configs[i]);
1232 arg = pinconf_to_config_argument(configs[i]);
1233
1234 switch (param) {
1235 case PIN_CONFIG_BIAS_DISABLE:
1236 conf &= ~BYT_PULL_ASSIGN_MASK;
1237 break;
1238 case PIN_CONFIG_BIAS_PULL_DOWN:
1239 /* Set default strength value in case none is given */
1240 if (arg == 1)
1241 arg = 2000;
1242
1243 /*
1244 * Pull assignment is only applicable in input mode. If
1245 * chip is not in input mode, set it and warn about it.
1246 */
1247 if (val & BYT_INPUT_EN) {
1248 val &= ~BYT_INPUT_EN;
1249 writel(val, val_reg);
1250 dev_warn(&vg->pdev->dev,
1251 "pin %u forcibly set to input mode\n",
1252 offset);
1253 }
1254
1255 conf &= ~BYT_PULL_ASSIGN_MASK;
1256 conf |= BYT_PULL_ASSIGN_DOWN;
1257 ret = byt_set_pull_strength(&conf, arg);
1258
1259 break;
1260 case PIN_CONFIG_BIAS_PULL_UP:
1261 /* Set default strength value in case none is given */
1262 if (arg == 1)
1263 arg = 2000;
1264
1265 /*
1266 * Pull assignment is only applicable in input mode. If
1267 * chip is not in input mode, set it and warn about it.
1268 */
1269 if (val & BYT_INPUT_EN) {
1270 val &= ~BYT_INPUT_EN;
1271 writel(val, val_reg);
1272 dev_warn(&vg->pdev->dev,
1273 "pin %u forcibly set to input mode\n",
1274 offset);
1275 }
1276
1277 conf &= ~BYT_PULL_ASSIGN_MASK;
1278 conf |= BYT_PULL_ASSIGN_UP;
1279 ret = byt_set_pull_strength(&conf, arg);
1280
1281 break;
1282 default:
1283 ret = -ENOTSUPP;
1284 }
1285
1286 if (ret)
1287 break;
1288 }
1289
1290 if (!ret)
1291 writel(conf, conf_reg);
1292
1293 raw_spin_unlock_irqrestore(&vg->lock, flags);
1294
1295 return ret;
1296}
1297
1298static const struct pinconf_ops byt_pinconf_ops = {
1299 .is_generic = true,
1300 .pin_config_get = byt_pin_config_get,
1301 .pin_config_set = byt_pin_config_set,
1302};
1303
1304static const struct pinctrl_desc byt_pinctrl_desc = {
1305 .pctlops = &byt_pinctrl_ops,
1306 .pmxops = &byt_pinmux_ops,
1307 .confops = &byt_pinconf_ops,
1308 .owner = THIS_MODULE,
1309};
1310
Mathias Nymana5d811b2013-06-18 14:33:02 +03001311static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
1312{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001313 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001314 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001315 unsigned long flags;
1316 u32 val;
1317
Mika Westerberg78e1c892015-08-17 16:03:17 +03001318 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001319 val = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001320 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mika Westerberg39ce8152015-08-04 15:03:14 +03001321
Linus Walleij3bde8772015-12-21 16:17:20 +01001322 return !!(val & BYT_LEVEL);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001323}
1324
1325static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1326{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001327 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001328 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001329 unsigned long flags;
1330 u32 old_val;
1331
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001332 if (!reg)
1333 return;
1334
Mika Westerberg78e1c892015-08-17 16:03:17 +03001335 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001336 old_val = readl(reg);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001337 if (value)
1338 writel(old_val | BYT_LEVEL, reg);
1339 else
1340 writel(old_val & ~BYT_LEVEL, reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001341 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001342}
1343
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001344static int byt_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001345{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001346 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001347 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001348 unsigned long flags;
1349 u32 value;
1350
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001351 if (!reg)
1352 return -EINVAL;
1353
Mika Westerberg78e1c892015-08-17 16:03:17 +03001354 raw_spin_lock_irqsave(&vg->lock, flags);
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001355 value = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001356 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001357
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001358 if (!(value & BYT_OUTPUT_EN))
1359 return GPIOF_DIR_OUT;
1360 if (!(value & BYT_INPUT_EN))
1361 return GPIOF_DIR_IN;
1362
1363 return -EINVAL;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001364}
1365
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001366static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001367{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001368 struct byt_gpio *vg = gpiochip_get_data(chip);
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001369 void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001370 unsigned long flags;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001371
Mika Westerberg78e1c892015-08-17 16:03:17 +03001372 raw_spin_lock_irqsave(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001373
Eric Ernstff998352014-06-12 11:06:20 -07001374 /*
1375 * Before making any direction modifications, do a check if gpio
1376 * is set for direct IRQ. On baytrail, setting GPIO to output does
1377 * not make sense, so let's at least warn the caller before they shoot
1378 * themselves in the foot.
1379 */
1380 WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
1381 "Potential Error: Setting GPIO with direct_irq_en to output");
1382
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001383 return pinctrl_gpio_direction_input(chip->base + offset);
1384}
Andy Shevchenko496940c2013-07-10 14:55:40 +03001385
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001386static int byt_gpio_direction_output(struct gpio_chip *chip,
1387 unsigned int offset, int value)
1388{
1389 int ret = pinctrl_gpio_direction_output(chip->base + offset);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001390
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001391 if (ret)
1392 return ret;
1393
1394 byt_gpio_set(chip, offset, value);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001395
1396 return 0;
1397}
1398
1399static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1400{
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001401 struct byt_gpio *vg = gpiochip_get_data(chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001402 int i;
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001403 u32 conf0, val;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001404
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001405 for (i = 0; i < vg->soc_data->npins; i++) {
1406 const struct byt_community *comm;
Mika Westerberg3ff95882014-05-16 12:18:29 +03001407 const char *pull_str = NULL;
1408 const char *pull = NULL;
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001409 void __iomem *reg;
Mika Westerberg78e1c892015-08-17 16:03:17 +03001410 unsigned long flags;
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001411 const char *label;
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001412 unsigned int pin;
Mika Westerberg78e1c892015-08-17 16:03:17 +03001413
1414 raw_spin_lock_irqsave(&vg->lock, flags);
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001415 pin = vg->soc_data->pins[i].number;
1416 reg = byt_gpio_reg(vg, pin, BYT_CONF0_REG);
1417 if (!reg) {
1418 seq_printf(s,
1419 "Could not retrieve pin %i conf0 reg\n",
1420 pin);
1421 continue;
1422 }
1423 conf0 = readl(reg);
1424
1425 reg = byt_gpio_reg(vg, pin, BYT_VAL_REG);
1426 if (!reg) {
1427 seq_printf(s,
1428 "Could not retrieve pin %i val reg\n", pin);
1429 }
1430 val = readl(reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001431 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001432
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001433 comm = byt_get_community(vg, pin);
1434 if (!comm) {
1435 seq_printf(s,
1436 "Could not get community for pin %i\n", pin);
1437 continue;
1438 }
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001439 label = gpiochip_is_requested(chip, i);
1440 if (!label)
1441 label = "Unrequested";
1442
Mika Westerberg3ff95882014-05-16 12:18:29 +03001443 switch (conf0 & BYT_PULL_ASSIGN_MASK) {
1444 case BYT_PULL_ASSIGN_UP:
1445 pull = "up";
1446 break;
1447 case BYT_PULL_ASSIGN_DOWN:
1448 pull = "down";
1449 break;
1450 }
1451
1452 switch (conf0 & BYT_PULL_STR_MASK) {
1453 case BYT_PULL_STR_2K:
1454 pull_str = "2k";
1455 break;
1456 case BYT_PULL_STR_10K:
1457 pull_str = "10k";
1458 break;
1459 case BYT_PULL_STR_20K:
1460 pull_str = "20k";
1461 break;
1462 case BYT_PULL_STR_40K:
1463 pull_str = "40k";
1464 break;
1465 }
1466
Mathias Nymana5d811b2013-06-18 14:33:02 +03001467 seq_printf(s,
Mika Westerberg3ff95882014-05-16 12:18:29 +03001468 " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001469 pin,
Mathias Nymana4d8d6d2013-11-22 14:01:23 +02001470 label,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001471 val & BYT_INPUT_EN ? " " : "in",
1472 val & BYT_OUTPUT_EN ? " " : "out",
1473 val & BYT_LEVEL ? "hi" : "lo",
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001474 comm->pad_map[i], comm->pad_map[i] * 32,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001475 conf0 & 0x7,
Mika Westerberg3ff95882014-05-16 12:18:29 +03001476 conf0 & BYT_TRIG_NEG ? " fall" : " ",
1477 conf0 & BYT_TRIG_POS ? " rise" : " ",
1478 conf0 & BYT_TRIG_LVL ? " level" : " ");
1479
1480 if (pull && pull_str)
1481 seq_printf(s, " %-4s %-3s", pull, pull_str);
1482 else
1483 seq_puts(s, " ");
1484
1485 if (conf0 & BYT_IODEN)
1486 seq_puts(s, " open-drain");
1487
1488 seq_puts(s, "\n");
Mathias Nymana5d811b2013-06-18 14:33:02 +03001489 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001490}
1491
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001492static void byt_gpio_irq_handler(struct irq_desc *desc)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001493{
1494 struct irq_data *data = irq_desc_get_irq_data(desc);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001495 struct byt_gpio *vg = gpiochip_get_data(irq_desc_get_handler_data(desc));
Mathias Nymana5d811b2013-06-18 14:33:02 +03001496 struct irq_chip *chip = irq_data_get_irq_chip(data);
Mika Westerberg31e43292015-02-23 14:53:12 +02001497 u32 base, pin;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001498 void __iomem *reg;
Mika Westerberg31e43292015-02-23 14:53:12 +02001499 unsigned long pending;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001500 unsigned virq;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001501
1502 /* check from GPIO controller which pin triggered the interrupt */
1503 for (base = 0; base < vg->chip.ngpio; base += 32) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001504 reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
Mika Westerberg31e43292015-02-23 14:53:12 +02001505 pending = readl(reg);
1506 for_each_set_bit(pin, &pending, 32) {
Mika Westerberge1ee5c52014-07-25 09:54:47 +03001507 virq = irq_find_mapping(vg->chip.irqdomain, base + pin);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001508 generic_handle_irq(virq);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001509 }
1510 }
1511 chip->irq_eoi(data);
1512}
1513
Cristina Ciocan86e3ef812016-04-01 14:00:04 +03001514static const struct gpio_chip byt_gpio_chip = {
1515 .owner = THIS_MODULE,
1516 .request = gpiochip_generic_request,
1517 .free = gpiochip_generic_free,
1518 .get_direction = byt_gpio_get_direction,
1519 .direction_input = byt_gpio_direction_input,
1520 .direction_output = byt_gpio_direction_output,
1521 .get = byt_gpio_get,
1522 .set = byt_gpio_set,
1523 .dbg_show = byt_gpio_dbg_show,
1524};
1525
Mika Westerberg31e43292015-02-23 14:53:12 +02001526static void byt_irq_ack(struct irq_data *d)
1527{
1528 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001529 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001530 unsigned offset = irqd_to_hwirq(d);
1531 void __iomem *reg;
1532
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001533 reg = byt_gpio_reg(vg, offset, BYT_INT_STAT_REG);
Cristina Ciocan9f573b92016-04-01 14:00:05 +03001534 if (!reg)
1535 return;
1536
1537 raw_spin_lock(&vg->lock);
Mika Westerberg31e43292015-02-23 14:53:12 +02001538 writel(BIT(offset % 32), reg);
Mika Westerberg78e1c892015-08-17 16:03:17 +03001539 raw_spin_unlock(&vg->lock);
Mika Westerberg31e43292015-02-23 14:53:12 +02001540}
1541
Cristina Ciocan9f573b92016-04-01 14:00:05 +03001542static void byt_irq_mask(struct irq_data *d)
1543{
1544 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1545 struct byt_gpio *vg = gpiochip_get_data(gc);
1546
1547 byt_gpio_clear_triggering(vg, irqd_to_hwirq(d));
1548}
1549
Mathias Nymana5d811b2013-06-18 14:33:02 +03001550static void byt_irq_unmask(struct irq_data *d)
1551{
Mika Westerberg31e43292015-02-23 14:53:12 +02001552 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001553 struct byt_gpio *vg = gpiochip_get_data(gc);
Mika Westerberg31e43292015-02-23 14:53:12 +02001554 unsigned offset = irqd_to_hwirq(d);
1555 unsigned long flags;
1556 void __iomem *reg;
1557 u32 value;
1558
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001559 reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Cristina Ciocan9f573b92016-04-01 14:00:05 +03001560 if (!reg)
1561 return;
Mika Westerberg78e1c892015-08-17 16:03:17 +03001562
1563 raw_spin_lock_irqsave(&vg->lock, flags);
Mika Westerberg31e43292015-02-23 14:53:12 +02001564 value = readl(reg);
1565
1566 switch (irqd_get_trigger_type(d)) {
1567 case IRQ_TYPE_LEVEL_HIGH:
1568 value |= BYT_TRIG_LVL;
1569 case IRQ_TYPE_EDGE_RISING:
1570 value |= BYT_TRIG_POS;
1571 break;
1572 case IRQ_TYPE_LEVEL_LOW:
1573 value |= BYT_TRIG_LVL;
1574 case IRQ_TYPE_EDGE_FALLING:
1575 value |= BYT_TRIG_NEG;
1576 break;
1577 case IRQ_TYPE_EDGE_BOTH:
1578 value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
1579 break;
1580 }
1581
1582 writel(value, reg);
1583
Mika Westerberg78e1c892015-08-17 16:03:17 +03001584 raw_spin_unlock_irqrestore(&vg->lock, flags);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001585}
1586
Cristina Ciocan9f573b92016-04-01 14:00:05 +03001587static int byt_irq_type(struct irq_data *d, unsigned int type)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001588{
Cristina Ciocan9f573b92016-04-01 14:00:05 +03001589 struct byt_gpio *vg = gpiochip_get_data(irq_data_get_irq_chip_data(d));
1590 u32 offset = irqd_to_hwirq(d);
1591 u32 value;
1592 unsigned long flags;
1593 void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
Mika Westerberg31e43292015-02-23 14:53:12 +02001594
Cristina Ciocan9f573b92016-04-01 14:00:05 +03001595 if (!reg || offset >= vg->chip.ngpio)
1596 return -EINVAL;
1597
1598 raw_spin_lock_irqsave(&vg->lock, flags);
1599 value = readl(reg);
1600
1601 WARN(value & BYT_DIRECT_IRQ_EN,
1602 "Bad pad config for io mode, force direct_irq_en bit clearing");
1603
1604 /* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
1605 * are used to indicate high and low level triggering
1606 */
1607 value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
1608 BYT_TRIG_LVL);
1609
1610 writel(value, reg);
1611
1612 if (type & IRQ_TYPE_EDGE_BOTH)
1613 irq_set_handler_locked(d, handle_edge_irq);
1614 else if (type & IRQ_TYPE_LEVEL_MASK)
1615 irq_set_handler_locked(d, handle_level_irq);
1616
1617 raw_spin_unlock_irqrestore(&vg->lock, flags);
1618
1619 return 0;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001620}
1621
1622static struct irq_chip byt_irqchip = {
Cristina Ciocan9f573b92016-04-01 14:00:05 +03001623 .name = "BYT-GPIO",
1624 .irq_ack = byt_irq_ack,
1625 .irq_mask = byt_irq_mask,
1626 .irq_unmask = byt_irq_unmask,
1627 .irq_set_type = byt_irq_type,
1628 .flags = IRQCHIP_SKIP_SET_WAKE,
Mathias Nymana5d811b2013-06-18 14:33:02 +03001629};
1630
1631static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
1632{
1633 void __iomem *reg;
1634 u32 base, value;
Mika Westerberg95f09722015-02-23 14:53:11 +02001635 int i;
1636
1637 /*
1638 * Clear interrupt triggers for all pins that are GPIOs and
1639 * do not use direct IRQ mode. This will prevent spurious
1640 * interrupts from misconfigured pins.
1641 */
1642 for (i = 0; i < vg->chip.ngpio; i++) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001643 value = readl(byt_gpio_reg(vg, i, BYT_CONF0_REG));
Mika Westerberg95f09722015-02-23 14:53:11 +02001644 if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i) &&
1645 !(value & BYT_DIRECT_IRQ_EN)) {
1646 byt_gpio_clear_triggering(vg, i);
1647 dev_dbg(&vg->pdev->dev, "disabling GPIO %d\n", i);
1648 }
1649 }
Mathias Nymana5d811b2013-06-18 14:33:02 +03001650
1651 /* clear interrupt status trigger registers */
1652 for (base = 0; base < vg->chip.ngpio; base += 32) {
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001653 reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001654 writel(0xffffffff, reg);
1655 /* make sure trigger bits are cleared, if not then a pin
1656 might be misconfigured in bios */
1657 value = readl(reg);
1658 if (value)
1659 dev_err(&vg->pdev->dev,
1660 "GPIO interrupt error, pins misconfigured\n");
1661 }
1662}
1663
Mathias Nymana5d811b2013-06-18 14:33:02 +03001664static int byt_gpio_probe(struct platform_device *pdev)
1665{
1666 struct byt_gpio *vg;
1667 struct gpio_chip *gc;
1668 struct resource *mem_rc, *irq_rc;
1669 struct device *dev = &pdev->dev;
1670 struct acpi_device *acpi_dev;
1671 struct pinctrl_gpio_range *range;
1672 acpi_handle handle = ACPI_HANDLE(dev);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001673 int ret;
1674
1675 if (acpi_bus_get_device(handle, &acpi_dev))
1676 return -ENODEV;
1677
1678 vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
1679 if (!vg) {
1680 dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
1681 return -ENOMEM;
1682 }
1683
1684 for (range = byt_ranges; range->name; range++) {
1685 if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
1686 vg->chip.ngpio = range->npins;
1687 vg->range = range;
1688 break;
1689 }
1690 }
1691
1692 if (!vg->chip.ngpio || !vg->range)
1693 return -ENODEV;
1694
1695 vg->pdev = pdev;
1696 platform_set_drvdata(pdev, vg);
1697
1698 mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1699 vg->reg_base = devm_ioremap_resource(dev, mem_rc);
1700 if (IS_ERR(vg->reg_base))
1701 return PTR_ERR(vg->reg_base);
1702
Mika Westerberg78e1c892015-08-17 16:03:17 +03001703 raw_spin_lock_init(&vg->lock);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001704
1705 gc = &vg->chip;
1706 gc->label = dev_name(&pdev->dev);
1707 gc->owner = THIS_MODULE;
1708 gc->request = byt_gpio_request;
1709 gc->free = byt_gpio_free;
1710 gc->direction_input = byt_gpio_direction_input;
1711 gc->direction_output = byt_gpio_direction_output;
1712 gc->get = byt_gpio_get;
1713 gc->set = byt_gpio_set;
1714 gc->dbg_show = byt_gpio_dbg_show;
1715 gc->base = -1;
Linus Walleij9fb1f392013-12-04 14:42:46 +01001716 gc->can_sleep = false;
Linus Walleij58383c782015-11-04 09:56:26 +01001717 gc->parent = dev;
Mathias Nymana5d811b2013-06-18 14:33:02 +03001718
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001719#ifdef CONFIG_PM_SLEEP
1720 vg->saved_context = devm_kcalloc(&pdev->dev, gc->ngpio,
1721 sizeof(*vg->saved_context), GFP_KERNEL);
1722#endif
1723
Linus Walleijbf9a5c92015-12-08 00:03:44 +01001724 ret = gpiochip_add_data(gc, vg);
Jin Yao605a7bc2014-05-15 18:28:47 +03001725 if (ret) {
1726 dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
1727 return ret;
1728 }
1729
Mika Westerberge1ee5c52014-07-25 09:54:47 +03001730 /* set up interrupts */
1731 irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1732 if (irq_rc && irq_rc->start) {
1733 byt_gpio_irq_init_hw(vg);
1734 ret = gpiochip_irqchip_add(gc, &byt_irqchip, 0,
1735 handle_simple_irq, IRQ_TYPE_NONE);
1736 if (ret) {
1737 dev_err(dev, "failed to add irqchip\n");
1738 gpiochip_remove(gc);
1739 return ret;
1740 }
1741
1742 gpiochip_set_chained_irqchip(gc, &byt_irqchip,
1743 (unsigned)irq_rc->start,
1744 byt_gpio_irq_handler);
1745 }
1746
Mathias Nymana5d811b2013-06-18 14:33:02 +03001747 pm_runtime_enable(dev);
1748
1749 return 0;
1750}
1751
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001752#ifdef CONFIG_PM_SLEEP
1753static int byt_gpio_suspend(struct device *dev)
1754{
1755 struct platform_device *pdev = to_platform_device(dev);
1756 struct byt_gpio *vg = platform_get_drvdata(pdev);
1757 int i;
1758
1759 for (i = 0; i < vg->chip.ngpio; i++) {
1760 void __iomem *reg;
1761 u32 value;
1762
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001763 reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001764 value = readl(reg) & BYT_CONF0_RESTORE_MASK;
1765 vg->saved_context[i].conf0 = value;
1766
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001767 reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001768 value = readl(reg) & BYT_VAL_RESTORE_MASK;
1769 vg->saved_context[i].val = value;
1770 }
1771
1772 return 0;
1773}
1774
1775static int byt_gpio_resume(struct device *dev)
1776{
1777 struct platform_device *pdev = to_platform_device(dev);
1778 struct byt_gpio *vg = platform_get_drvdata(pdev);
1779 int i;
1780
1781 for (i = 0; i < vg->chip.ngpio; i++) {
1782 void __iomem *reg;
1783 u32 value;
1784
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001785 reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001786 value = readl(reg);
1787 if ((value & BYT_CONF0_RESTORE_MASK) !=
1788 vg->saved_context[i].conf0) {
1789 value &= ~BYT_CONF0_RESTORE_MASK;
1790 value |= vg->saved_context[i].conf0;
1791 writel(value, reg);
1792 dev_info(dev, "restored pin %d conf0 %#08x", i, value);
1793 }
1794
Cristina Ciocanc501d0b2016-04-01 14:00:03 +03001795 reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001796 value = readl(reg);
1797 if ((value & BYT_VAL_RESTORE_MASK) !=
1798 vg->saved_context[i].val) {
1799 u32 v;
1800
1801 v = value & ~BYT_VAL_RESTORE_MASK;
1802 v |= vg->saved_context[i].val;
1803 if (v != value) {
1804 writel(v, reg);
1805 dev_dbg(dev, "restored pin %d val %#08x\n",
1806 i, v);
1807 }
1808 }
1809 }
1810
1811 return 0;
1812}
1813#endif
1814
Mika Westerbergec879f12015-10-13 17:51:26 +03001815#ifdef CONFIG_PM
Mathias Nymana5d811b2013-06-18 14:33:02 +03001816static int byt_gpio_runtime_suspend(struct device *dev)
1817{
1818 return 0;
1819}
1820
1821static int byt_gpio_runtime_resume(struct device *dev)
1822{
1823 return 0;
1824}
Mika Westerbergec879f12015-10-13 17:51:26 +03001825#endif
Mathias Nymana5d811b2013-06-18 14:33:02 +03001826
1827static const struct dev_pm_ops byt_gpio_pm_ops = {
Mika Westerbergfcc18de2015-02-23 14:53:13 +02001828 SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
1829 SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
1830 NULL)
Mathias Nymana5d811b2013-06-18 14:33:02 +03001831};
1832
1833static const struct acpi_device_id byt_gpio_acpi_match[] = {
Cristina Ciocanc8f5c4c2016-04-01 14:00:02 +03001834 { "INT33B2", (kernel_ulong_t)byt_soc_data },
1835 { "INT33FC", (kernel_ulong_t)byt_soc_data },
Mathias Nymana5d811b2013-06-18 14:33:02 +03001836 { }
1837};
1838MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);
1839
1840static int byt_gpio_remove(struct platform_device *pdev)
1841{
1842 struct byt_gpio *vg = platform_get_drvdata(pdev);
Andy Shevchenkoec243322013-07-10 14:55:36 +03001843
Mathias Nymana5d811b2013-06-18 14:33:02 +03001844 pm_runtime_disable(&pdev->dev);
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001845 gpiochip_remove(&vg->chip);
Mathias Nymana5d811b2013-06-18 14:33:02 +03001846
1847 return 0;
1848}
1849
1850static struct platform_driver byt_gpio_driver = {
1851 .probe = byt_gpio_probe,
1852 .remove = byt_gpio_remove,
1853 .driver = {
1854 .name = "byt_gpio",
Mathias Nymana5d811b2013-06-18 14:33:02 +03001855 .pm = &byt_gpio_pm_ops,
1856 .acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
1857 },
1858};
1859
1860static int __init byt_gpio_init(void)
1861{
1862 return platform_driver_register(&byt_gpio_driver);
1863}
Mathias Nymana5d811b2013-06-18 14:33:02 +03001864subsys_initcall(byt_gpio_init);
Felipe Balbi9067bbe2014-10-13 15:50:08 -05001865
1866static void __exit byt_gpio_exit(void)
1867{
1868 platform_driver_unregister(&byt_gpio_driver);
1869}
1870module_exit(byt_gpio_exit);