• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cherryview/Braswell pinctrl driver
4  *
5  * Copyright (C) 2014, Intel Corporation
6  * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7  *
8  * This driver is based on the original Cherryview GPIO driver by
9  *   Ning Li <ning.li@intel.com>
10  *   Alan Cox <alan@linux.intel.com>
11  */
12 
13 #include <linux/dmi.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/types.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/acpi.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/platform_device.h>
26 
27 #define CHV_INTSTAT			0x300
28 #define CHV_INTMASK			0x380
29 
30 #define FAMILY_PAD_REGS_OFF		0x4400
31 #define FAMILY_PAD_REGS_SIZE		0x400
32 #define MAX_FAMILY_PAD_GPIO_NO		15
33 #define GPIO_REGS_SIZE			8
34 
35 #define CHV_PADCTRL0			0x000
36 #define CHV_PADCTRL0_INTSEL_SHIFT	28
37 #define CHV_PADCTRL0_INTSEL_MASK	(0xf << CHV_PADCTRL0_INTSEL_SHIFT)
38 #define CHV_PADCTRL0_TERM_UP		BIT(23)
39 #define CHV_PADCTRL0_TERM_SHIFT		20
40 #define CHV_PADCTRL0_TERM_MASK		(7 << CHV_PADCTRL0_TERM_SHIFT)
41 #define CHV_PADCTRL0_TERM_20K		1
42 #define CHV_PADCTRL0_TERM_5K		2
43 #define CHV_PADCTRL0_TERM_1K		4
44 #define CHV_PADCTRL0_PMODE_SHIFT	16
45 #define CHV_PADCTRL0_PMODE_MASK		(0xf << CHV_PADCTRL0_PMODE_SHIFT)
46 #define CHV_PADCTRL0_GPIOEN		BIT(15)
47 #define CHV_PADCTRL0_GPIOCFG_SHIFT	8
48 #define CHV_PADCTRL0_GPIOCFG_MASK	(7 << CHV_PADCTRL0_GPIOCFG_SHIFT)
49 #define CHV_PADCTRL0_GPIOCFG_GPIO	0
50 #define CHV_PADCTRL0_GPIOCFG_GPO	1
51 #define CHV_PADCTRL0_GPIOCFG_GPI	2
52 #define CHV_PADCTRL0_GPIOCFG_HIZ	3
53 #define CHV_PADCTRL0_GPIOTXSTATE	BIT(1)
54 #define CHV_PADCTRL0_GPIORXSTATE	BIT(0)
55 
56 #define CHV_PADCTRL1			0x004
57 #define CHV_PADCTRL1_CFGLOCK		BIT(31)
58 #define CHV_PADCTRL1_INVRXTX_SHIFT	4
59 #define CHV_PADCTRL1_INVRXTX_MASK	(0xf << CHV_PADCTRL1_INVRXTX_SHIFT)
60 #define CHV_PADCTRL1_INVRXTX_TXENABLE	(2 << CHV_PADCTRL1_INVRXTX_SHIFT)
61 #define CHV_PADCTRL1_ODEN		BIT(3)
62 #define CHV_PADCTRL1_INVRXTX_RXDATA	(4 << CHV_PADCTRL1_INVRXTX_SHIFT)
63 #define CHV_PADCTRL1_INTWAKECFG_MASK	7
64 #define CHV_PADCTRL1_INTWAKECFG_FALLING	1
65 #define CHV_PADCTRL1_INTWAKECFG_RISING	2
66 #define CHV_PADCTRL1_INTWAKECFG_BOTH	3
67 #define CHV_PADCTRL1_INTWAKECFG_LEVEL	4
68 
69 /**
70  * struct chv_alternate_function - A per group or per pin alternate function
71  * @pin: Pin number (only used in per pin configs)
72  * @mode: Mode the pin should be set in
73  * @invert_oe: Invert OE for this pin
74  */
75 struct chv_alternate_function {
76 	unsigned pin;
77 	u8 mode;
78 	bool invert_oe;
79 };
80 
81 /**
82  * struct chv_pincgroup - describes a CHV pin group
83  * @name: Name of the group
84  * @pins: An array of pins in this group
85  * @npins: Number of pins in this group
86  * @altfunc: Alternate function applied to all pins in this group
87  * @overrides: Alternate function override per pin or %NULL if not used
88  * @noverrides: Number of per pin alternate function overrides if
89  *              @overrides != NULL.
90  */
91 struct chv_pingroup {
92 	const char *name;
93 	const unsigned *pins;
94 	size_t npins;
95 	struct chv_alternate_function altfunc;
96 	const struct chv_alternate_function *overrides;
97 	size_t noverrides;
98 };
99 
100 /**
101  * struct chv_function - A CHV pinmux function
102  * @name: Name of the function
103  * @groups: An array of groups for this function
104  * @ngroups: Number of groups in @groups
105  */
106 struct chv_function {
107 	const char *name;
108 	const char * const *groups;
109 	size_t ngroups;
110 };
111 
112 /**
113  * struct chv_gpio_pinrange - A range of pins that can be used as GPIOs
114  * @base: Start pin number
115  * @npins: Number of pins in this range
116  */
117 struct chv_gpio_pinrange {
118 	unsigned base;
119 	unsigned npins;
120 };
121 
122 /**
123  * struct chv_community - A community specific configuration
124  * @uid: ACPI _UID used to match the community
125  * @pins: All pins in this community
126  * @npins: Number of pins
127  * @groups: All groups in this community
128  * @ngroups: Number of groups
129  * @functions: All functions in this community
130  * @nfunctions: Number of functions
131  * @gpio_ranges: An array of GPIO ranges in this community
132  * @ngpio_ranges: Number of GPIO ranges
133  * @nirqs: Total number of IRQs this community can generate
134  */
135 struct chv_community {
136 	const char *uid;
137 	const struct pinctrl_pin_desc *pins;
138 	size_t npins;
139 	const struct chv_pingroup *groups;
140 	size_t ngroups;
141 	const struct chv_function *functions;
142 	size_t nfunctions;
143 	const struct chv_gpio_pinrange *gpio_ranges;
144 	size_t ngpio_ranges;
145 	size_t nirqs;
146 	acpi_adr_space_type acpi_space_id;
147 };
148 
149 struct chv_pin_context {
150 	u32 padctrl0;
151 	u32 padctrl1;
152 };
153 
154 /**
155  * struct chv_pinctrl - CHV pinctrl private structure
156  * @dev: Pointer to the parent device
157  * @pctldesc: Pin controller description
158  * @pctldev: Pointer to the pin controller device
159  * @chip: GPIO chip in this pin controller
160  * @irqchip: IRQ chip in this pin controller
161  * @regs: MMIO registers
162  * @intr_lines: Stores mapping between 16 HW interrupt wires and GPIO
163  *		offset (in GPIO number space)
164  * @community: Community this pinctrl instance represents
165  *
166  * The first group in @groups is expected to contain all pins that can be
167  * used as GPIOs.
168  */
169 struct chv_pinctrl {
170 	struct device *dev;
171 	struct pinctrl_desc pctldesc;
172 	struct pinctrl_dev *pctldev;
173 	struct gpio_chip chip;
174 	struct irq_chip irqchip;
175 	void __iomem *regs;
176 	unsigned intr_lines[16];
177 	const struct chv_community *community;
178 	u32 saved_intmask;
179 	struct chv_pin_context *saved_pin_context;
180 };
181 
182 #define ALTERNATE_FUNCTION(p, m, i)		\
183 	{					\
184 		.pin = (p),			\
185 		.mode = (m),			\
186 		.invert_oe = (i),		\
187 	}
188 
189 #define PIN_GROUP(n, p, m, i)			\
190 	{					\
191 		.name = (n),			\
192 		.pins = (p),			\
193 		.npins = ARRAY_SIZE((p)),	\
194 		.altfunc.mode = (m),		\
195 		.altfunc.invert_oe = (i),	\
196 	}
197 
198 #define PIN_GROUP_WITH_OVERRIDE(n, p, m, i, o)	\
199 	{					\
200 		.name = (n),			\
201 		.pins = (p),			\
202 		.npins = ARRAY_SIZE((p)),	\
203 		.altfunc.mode = (m),		\
204 		.altfunc.invert_oe = (i),	\
205 		.overrides = (o),		\
206 		.noverrides = ARRAY_SIZE((o)),	\
207 	}
208 
209 #define FUNCTION(n, g)				\
210 	{					\
211 		.name = (n),			\
212 		.groups = (g),			\
213 		.ngroups = ARRAY_SIZE((g)),	\
214 	}
215 
216 #define GPIO_PINRANGE(start, end)		\
217 	{					\
218 		.base = (start),		\
219 		.npins = (end) - (start) + 1,	\
220 	}
221 
222 static const struct pinctrl_pin_desc southwest_pins[] = {
223 	PINCTRL_PIN(0, "FST_SPI_D2"),
224 	PINCTRL_PIN(1, "FST_SPI_D0"),
225 	PINCTRL_PIN(2, "FST_SPI_CLK"),
226 	PINCTRL_PIN(3, "FST_SPI_D3"),
227 	PINCTRL_PIN(4, "FST_SPI_CS1_B"),
228 	PINCTRL_PIN(5, "FST_SPI_D1"),
229 	PINCTRL_PIN(6, "FST_SPI_CS0_B"),
230 	PINCTRL_PIN(7, "FST_SPI_CS2_B"),
231 
232 	PINCTRL_PIN(15, "UART1_RTS_B"),
233 	PINCTRL_PIN(16, "UART1_RXD"),
234 	PINCTRL_PIN(17, "UART2_RXD"),
235 	PINCTRL_PIN(18, "UART1_CTS_B"),
236 	PINCTRL_PIN(19, "UART2_RTS_B"),
237 	PINCTRL_PIN(20, "UART1_TXD"),
238 	PINCTRL_PIN(21, "UART2_TXD"),
239 	PINCTRL_PIN(22, "UART2_CTS_B"),
240 
241 	PINCTRL_PIN(30, "MF_HDA_CLK"),
242 	PINCTRL_PIN(31, "MF_HDA_RSTB"),
243 	PINCTRL_PIN(32, "MF_HDA_SDIO"),
244 	PINCTRL_PIN(33, "MF_HDA_SDO"),
245 	PINCTRL_PIN(34, "MF_HDA_DOCKRSTB"),
246 	PINCTRL_PIN(35, "MF_HDA_SYNC"),
247 	PINCTRL_PIN(36, "MF_HDA_SDI1"),
248 	PINCTRL_PIN(37, "MF_HDA_DOCKENB"),
249 
250 	PINCTRL_PIN(45, "I2C5_SDA"),
251 	PINCTRL_PIN(46, "I2C4_SDA"),
252 	PINCTRL_PIN(47, "I2C6_SDA"),
253 	PINCTRL_PIN(48, "I2C5_SCL"),
254 	PINCTRL_PIN(49, "I2C_NFC_SDA"),
255 	PINCTRL_PIN(50, "I2C4_SCL"),
256 	PINCTRL_PIN(51, "I2C6_SCL"),
257 	PINCTRL_PIN(52, "I2C_NFC_SCL"),
258 
259 	PINCTRL_PIN(60, "I2C1_SDA"),
260 	PINCTRL_PIN(61, "I2C0_SDA"),
261 	PINCTRL_PIN(62, "I2C2_SDA"),
262 	PINCTRL_PIN(63, "I2C1_SCL"),
263 	PINCTRL_PIN(64, "I2C3_SDA"),
264 	PINCTRL_PIN(65, "I2C0_SCL"),
265 	PINCTRL_PIN(66, "I2C2_SCL"),
266 	PINCTRL_PIN(67, "I2C3_SCL"),
267 
268 	PINCTRL_PIN(75, "SATA_GP0"),
269 	PINCTRL_PIN(76, "SATA_GP1"),
270 	PINCTRL_PIN(77, "SATA_LEDN"),
271 	PINCTRL_PIN(78, "SATA_GP2"),
272 	PINCTRL_PIN(79, "MF_SMB_ALERTB"),
273 	PINCTRL_PIN(80, "SATA_GP3"),
274 	PINCTRL_PIN(81, "MF_SMB_CLK"),
275 	PINCTRL_PIN(82, "MF_SMB_DATA"),
276 
277 	PINCTRL_PIN(90, "PCIE_CLKREQ0B"),
278 	PINCTRL_PIN(91, "PCIE_CLKREQ1B"),
279 	PINCTRL_PIN(92, "GP_SSP_2_CLK"),
280 	PINCTRL_PIN(93, "PCIE_CLKREQ2B"),
281 	PINCTRL_PIN(94, "GP_SSP_2_RXD"),
282 	PINCTRL_PIN(95, "PCIE_CLKREQ3B"),
283 	PINCTRL_PIN(96, "GP_SSP_2_FS"),
284 	PINCTRL_PIN(97, "GP_SSP_2_TXD"),
285 };
286 
287 static const unsigned southwest_fspi_pins[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
288 static const unsigned southwest_uart0_pins[] = { 16, 20 };
289 static const unsigned southwest_uart1_pins[] = { 15, 16, 18, 20 };
290 static const unsigned southwest_uart2_pins[] = { 17, 19, 21, 22 };
291 static const unsigned southwest_i2c0_pins[] = { 61, 65 };
292 static const unsigned southwest_hda_pins[] = { 30, 31, 32, 33, 34, 35, 36, 37 };
293 static const unsigned southwest_lpe_pins[] = {
294 	30, 31, 32, 33, 34, 35, 36, 37, 92, 94, 96, 97,
295 };
296 static const unsigned southwest_i2c1_pins[] = { 60, 63 };
297 static const unsigned southwest_i2c2_pins[] = { 62, 66 };
298 static const unsigned southwest_i2c3_pins[] = { 64, 67 };
299 static const unsigned southwest_i2c4_pins[] = { 46, 50 };
300 static const unsigned southwest_i2c5_pins[] = { 45, 48 };
301 static const unsigned southwest_i2c6_pins[] = { 47, 51 };
302 static const unsigned southwest_i2c_nfc_pins[] = { 49, 52 };
303 static const unsigned southwest_smbus_pins[] = { 79, 81, 82 };
304 static const unsigned southwest_spi3_pins[] = { 76, 79, 80, 81, 82 };
305 
306 /* LPE I2S TXD pins need to have invert_oe set */
307 static const struct chv_alternate_function southwest_lpe_altfuncs[] = {
308 	ALTERNATE_FUNCTION(30, 1, true),
309 	ALTERNATE_FUNCTION(34, 1, true),
310 	ALTERNATE_FUNCTION(97, 1, true),
311 };
312 
313 /*
314  * Two spi3 chipselects are available in different mode than the main spi3
315  * functionality, which is using mode 1.
316  */
317 static const struct chv_alternate_function southwest_spi3_altfuncs[] = {
318 	ALTERNATE_FUNCTION(76, 3, false),
319 	ALTERNATE_FUNCTION(80, 3, false),
320 };
321 
322 static const struct chv_pingroup southwest_groups[] = {
323 	PIN_GROUP("uart0_grp", southwest_uart0_pins, 2, false),
324 	PIN_GROUP("uart1_grp", southwest_uart1_pins, 1, false),
325 	PIN_GROUP("uart2_grp", southwest_uart2_pins, 1, false),
326 	PIN_GROUP("hda_grp", southwest_hda_pins, 2, false),
327 	PIN_GROUP("i2c0_grp", southwest_i2c0_pins, 1, true),
328 	PIN_GROUP("i2c1_grp", southwest_i2c1_pins, 1, true),
329 	PIN_GROUP("i2c2_grp", southwest_i2c2_pins, 1, true),
330 	PIN_GROUP("i2c3_grp", southwest_i2c3_pins, 1, true),
331 	PIN_GROUP("i2c4_grp", southwest_i2c4_pins, 1, true),
332 	PIN_GROUP("i2c5_grp", southwest_i2c5_pins, 1, true),
333 	PIN_GROUP("i2c6_grp", southwest_i2c6_pins, 1, true),
334 	PIN_GROUP("i2c_nfc_grp", southwest_i2c_nfc_pins, 2, true),
335 
336 	PIN_GROUP_WITH_OVERRIDE("lpe_grp", southwest_lpe_pins, 1, false,
337 				southwest_lpe_altfuncs),
338 	PIN_GROUP_WITH_OVERRIDE("spi3_grp", southwest_spi3_pins, 2, false,
339 				southwest_spi3_altfuncs),
340 };
341 
342 static const char * const southwest_uart0_groups[] = { "uart0_grp" };
343 static const char * const southwest_uart1_groups[] = { "uart1_grp" };
344 static const char * const southwest_uart2_groups[] = { "uart2_grp" };
345 static const char * const southwest_hda_groups[] = { "hda_grp" };
346 static const char * const southwest_lpe_groups[] = { "lpe_grp" };
347 static const char * const southwest_i2c0_groups[] = { "i2c0_grp" };
348 static const char * const southwest_i2c1_groups[] = { "i2c1_grp" };
349 static const char * const southwest_i2c2_groups[] = { "i2c2_grp" };
350 static const char * const southwest_i2c3_groups[] = { "i2c3_grp" };
351 static const char * const southwest_i2c4_groups[] = { "i2c4_grp" };
352 static const char * const southwest_i2c5_groups[] = { "i2c5_grp" };
353 static const char * const southwest_i2c6_groups[] = { "i2c6_grp" };
354 static const char * const southwest_i2c_nfc_groups[] = { "i2c_nfc_grp" };
355 static const char * const southwest_spi3_groups[] = { "spi3_grp" };
356 
357 /*
358  * Only do pinmuxing for certain LPSS devices for now. Rest of the pins are
359  * enabled only as GPIOs.
360  */
361 static const struct chv_function southwest_functions[] = {
362 	FUNCTION("uart0", southwest_uart0_groups),
363 	FUNCTION("uart1", southwest_uart1_groups),
364 	FUNCTION("uart2", southwest_uart2_groups),
365 	FUNCTION("hda", southwest_hda_groups),
366 	FUNCTION("lpe", southwest_lpe_groups),
367 	FUNCTION("i2c0", southwest_i2c0_groups),
368 	FUNCTION("i2c1", southwest_i2c1_groups),
369 	FUNCTION("i2c2", southwest_i2c2_groups),
370 	FUNCTION("i2c3", southwest_i2c3_groups),
371 	FUNCTION("i2c4", southwest_i2c4_groups),
372 	FUNCTION("i2c5", southwest_i2c5_groups),
373 	FUNCTION("i2c6", southwest_i2c6_groups),
374 	FUNCTION("i2c_nfc", southwest_i2c_nfc_groups),
375 	FUNCTION("spi3", southwest_spi3_groups),
376 };
377 
378 static const struct chv_gpio_pinrange southwest_gpio_ranges[] = {
379 	GPIO_PINRANGE(0, 7),
380 	GPIO_PINRANGE(15, 22),
381 	GPIO_PINRANGE(30, 37),
382 	GPIO_PINRANGE(45, 52),
383 	GPIO_PINRANGE(60, 67),
384 	GPIO_PINRANGE(75, 82),
385 	GPIO_PINRANGE(90, 97),
386 };
387 
388 static const struct chv_community southwest_community = {
389 	.uid = "1",
390 	.pins = southwest_pins,
391 	.npins = ARRAY_SIZE(southwest_pins),
392 	.groups = southwest_groups,
393 	.ngroups = ARRAY_SIZE(southwest_groups),
394 	.functions = southwest_functions,
395 	.nfunctions = ARRAY_SIZE(southwest_functions),
396 	.gpio_ranges = southwest_gpio_ranges,
397 	.ngpio_ranges = ARRAY_SIZE(southwest_gpio_ranges),
398 	/*
399 	 * Southwest community can benerate GPIO interrupts only for the
400 	 * first 8 interrupts. The upper half (8-15) can only be used to
401 	 * trigger GPEs.
402 	 */
403 	.nirqs = 8,
404 	.acpi_space_id = 0x91,
405 };
406 
407 static const struct pinctrl_pin_desc north_pins[] = {
408 	PINCTRL_PIN(0, "GPIO_DFX_0"),
409 	PINCTRL_PIN(1, "GPIO_DFX_3"),
410 	PINCTRL_PIN(2, "GPIO_DFX_7"),
411 	PINCTRL_PIN(3, "GPIO_DFX_1"),
412 	PINCTRL_PIN(4, "GPIO_DFX_5"),
413 	PINCTRL_PIN(5, "GPIO_DFX_4"),
414 	PINCTRL_PIN(6, "GPIO_DFX_8"),
415 	PINCTRL_PIN(7, "GPIO_DFX_2"),
416 	PINCTRL_PIN(8, "GPIO_DFX_6"),
417 
418 	PINCTRL_PIN(15, "GPIO_SUS0"),
419 	PINCTRL_PIN(16, "SEC_GPIO_SUS10"),
420 	PINCTRL_PIN(17, "GPIO_SUS3"),
421 	PINCTRL_PIN(18, "GPIO_SUS7"),
422 	PINCTRL_PIN(19, "GPIO_SUS1"),
423 	PINCTRL_PIN(20, "GPIO_SUS5"),
424 	PINCTRL_PIN(21, "SEC_GPIO_SUS11"),
425 	PINCTRL_PIN(22, "GPIO_SUS4"),
426 	PINCTRL_PIN(23, "SEC_GPIO_SUS8"),
427 	PINCTRL_PIN(24, "GPIO_SUS2"),
428 	PINCTRL_PIN(25, "GPIO_SUS6"),
429 	PINCTRL_PIN(26, "CX_PREQ_B"),
430 	PINCTRL_PIN(27, "SEC_GPIO_SUS9"),
431 
432 	PINCTRL_PIN(30, "TRST_B"),
433 	PINCTRL_PIN(31, "TCK"),
434 	PINCTRL_PIN(32, "PROCHOT_B"),
435 	PINCTRL_PIN(33, "SVIDO_DATA"),
436 	PINCTRL_PIN(34, "TMS"),
437 	PINCTRL_PIN(35, "CX_PRDY_B_2"),
438 	PINCTRL_PIN(36, "TDO_2"),
439 	PINCTRL_PIN(37, "CX_PRDY_B"),
440 	PINCTRL_PIN(38, "SVIDO_ALERT_B"),
441 	PINCTRL_PIN(39, "TDO"),
442 	PINCTRL_PIN(40, "SVIDO_CLK"),
443 	PINCTRL_PIN(41, "TDI"),
444 
445 	PINCTRL_PIN(45, "GP_CAMERASB_05"),
446 	PINCTRL_PIN(46, "GP_CAMERASB_02"),
447 	PINCTRL_PIN(47, "GP_CAMERASB_08"),
448 	PINCTRL_PIN(48, "GP_CAMERASB_00"),
449 	PINCTRL_PIN(49, "GP_CAMERASB_06"),
450 	PINCTRL_PIN(50, "GP_CAMERASB_10"),
451 	PINCTRL_PIN(51, "GP_CAMERASB_03"),
452 	PINCTRL_PIN(52, "GP_CAMERASB_09"),
453 	PINCTRL_PIN(53, "GP_CAMERASB_01"),
454 	PINCTRL_PIN(54, "GP_CAMERASB_07"),
455 	PINCTRL_PIN(55, "GP_CAMERASB_11"),
456 	PINCTRL_PIN(56, "GP_CAMERASB_04"),
457 
458 	PINCTRL_PIN(60, "PANEL0_BKLTEN"),
459 	PINCTRL_PIN(61, "HV_DDI0_HPD"),
460 	PINCTRL_PIN(62, "HV_DDI2_DDC_SDA"),
461 	PINCTRL_PIN(63, "PANEL1_BKLTCTL"),
462 	PINCTRL_PIN(64, "HV_DDI1_HPD"),
463 	PINCTRL_PIN(65, "PANEL0_BKLTCTL"),
464 	PINCTRL_PIN(66, "HV_DDI0_DDC_SDA"),
465 	PINCTRL_PIN(67, "HV_DDI2_DDC_SCL"),
466 	PINCTRL_PIN(68, "HV_DDI2_HPD"),
467 	PINCTRL_PIN(69, "PANEL1_VDDEN"),
468 	PINCTRL_PIN(70, "PANEL1_BKLTEN"),
469 	PINCTRL_PIN(71, "HV_DDI0_DDC_SCL"),
470 	PINCTRL_PIN(72, "PANEL0_VDDEN"),
471 };
472 
473 static const struct chv_gpio_pinrange north_gpio_ranges[] = {
474 	GPIO_PINRANGE(0, 8),
475 	GPIO_PINRANGE(15, 27),
476 	GPIO_PINRANGE(30, 41),
477 	GPIO_PINRANGE(45, 56),
478 	GPIO_PINRANGE(60, 72),
479 };
480 
481 static const struct chv_community north_community = {
482 	.uid = "2",
483 	.pins = north_pins,
484 	.npins = ARRAY_SIZE(north_pins),
485 	.gpio_ranges = north_gpio_ranges,
486 	.ngpio_ranges = ARRAY_SIZE(north_gpio_ranges),
487 	/*
488 	 * North community can generate GPIO interrupts only for the first
489 	 * 8 interrupts. The upper half (8-15) can only be used to trigger
490 	 * GPEs.
491 	 */
492 	.nirqs = 8,
493 	.acpi_space_id = 0x92,
494 };
495 
496 static const struct pinctrl_pin_desc east_pins[] = {
497 	PINCTRL_PIN(0, "PMU_SLP_S3_B"),
498 	PINCTRL_PIN(1, "PMU_BATLOW_B"),
499 	PINCTRL_PIN(2, "SUS_STAT_B"),
500 	PINCTRL_PIN(3, "PMU_SLP_S0IX_B"),
501 	PINCTRL_PIN(4, "PMU_AC_PRESENT"),
502 	PINCTRL_PIN(5, "PMU_PLTRST_B"),
503 	PINCTRL_PIN(6, "PMU_SUSCLK"),
504 	PINCTRL_PIN(7, "PMU_SLP_LAN_B"),
505 	PINCTRL_PIN(8, "PMU_PWRBTN_B"),
506 	PINCTRL_PIN(9, "PMU_SLP_S4_B"),
507 	PINCTRL_PIN(10, "PMU_WAKE_B"),
508 	PINCTRL_PIN(11, "PMU_WAKE_LAN_B"),
509 
510 	PINCTRL_PIN(15, "MF_ISH_GPIO_3"),
511 	PINCTRL_PIN(16, "MF_ISH_GPIO_7"),
512 	PINCTRL_PIN(17, "MF_ISH_I2C1_SCL"),
513 	PINCTRL_PIN(18, "MF_ISH_GPIO_1"),
514 	PINCTRL_PIN(19, "MF_ISH_GPIO_5"),
515 	PINCTRL_PIN(20, "MF_ISH_GPIO_9"),
516 	PINCTRL_PIN(21, "MF_ISH_GPIO_0"),
517 	PINCTRL_PIN(22, "MF_ISH_GPIO_4"),
518 	PINCTRL_PIN(23, "MF_ISH_GPIO_8"),
519 	PINCTRL_PIN(24, "MF_ISH_GPIO_2"),
520 	PINCTRL_PIN(25, "MF_ISH_GPIO_6"),
521 	PINCTRL_PIN(26, "MF_ISH_I2C1_SDA"),
522 };
523 
524 static const struct chv_gpio_pinrange east_gpio_ranges[] = {
525 	GPIO_PINRANGE(0, 11),
526 	GPIO_PINRANGE(15, 26),
527 };
528 
529 static const struct chv_community east_community = {
530 	.uid = "3",
531 	.pins = east_pins,
532 	.npins = ARRAY_SIZE(east_pins),
533 	.gpio_ranges = east_gpio_ranges,
534 	.ngpio_ranges = ARRAY_SIZE(east_gpio_ranges),
535 	.nirqs = 16,
536 	.acpi_space_id = 0x93,
537 };
538 
539 static const struct pinctrl_pin_desc southeast_pins[] = {
540 	PINCTRL_PIN(0, "MF_PLT_CLK0"),
541 	PINCTRL_PIN(1, "PWM1"),
542 	PINCTRL_PIN(2, "MF_PLT_CLK1"),
543 	PINCTRL_PIN(3, "MF_PLT_CLK4"),
544 	PINCTRL_PIN(4, "MF_PLT_CLK3"),
545 	PINCTRL_PIN(5, "PWM0"),
546 	PINCTRL_PIN(6, "MF_PLT_CLK5"),
547 	PINCTRL_PIN(7, "MF_PLT_CLK2"),
548 
549 	PINCTRL_PIN(15, "SDMMC2_D3_CD_B"),
550 	PINCTRL_PIN(16, "SDMMC1_CLK"),
551 	PINCTRL_PIN(17, "SDMMC1_D0"),
552 	PINCTRL_PIN(18, "SDMMC2_D1"),
553 	PINCTRL_PIN(19, "SDMMC2_CLK"),
554 	PINCTRL_PIN(20, "SDMMC1_D2"),
555 	PINCTRL_PIN(21, "SDMMC2_D2"),
556 	PINCTRL_PIN(22, "SDMMC2_CMD"),
557 	PINCTRL_PIN(23, "SDMMC1_CMD"),
558 	PINCTRL_PIN(24, "SDMMC1_D1"),
559 	PINCTRL_PIN(25, "SDMMC2_D0"),
560 	PINCTRL_PIN(26, "SDMMC1_D3_CD_B"),
561 
562 	PINCTRL_PIN(30, "SDMMC3_D1"),
563 	PINCTRL_PIN(31, "SDMMC3_CLK"),
564 	PINCTRL_PIN(32, "SDMMC3_D3"),
565 	PINCTRL_PIN(33, "SDMMC3_D2"),
566 	PINCTRL_PIN(34, "SDMMC3_CMD"),
567 	PINCTRL_PIN(35, "SDMMC3_D0"),
568 
569 	PINCTRL_PIN(45, "MF_LPC_AD2"),
570 	PINCTRL_PIN(46, "LPC_CLKRUNB"),
571 	PINCTRL_PIN(47, "MF_LPC_AD0"),
572 	PINCTRL_PIN(48, "LPC_FRAMEB"),
573 	PINCTRL_PIN(49, "MF_LPC_CLKOUT1"),
574 	PINCTRL_PIN(50, "MF_LPC_AD3"),
575 	PINCTRL_PIN(51, "MF_LPC_CLKOUT0"),
576 	PINCTRL_PIN(52, "MF_LPC_AD1"),
577 
578 	PINCTRL_PIN(60, "SPI1_MISO"),
579 	PINCTRL_PIN(61, "SPI1_CSO_B"),
580 	PINCTRL_PIN(62, "SPI1_CLK"),
581 	PINCTRL_PIN(63, "MMC1_D6"),
582 	PINCTRL_PIN(64, "SPI1_MOSI"),
583 	PINCTRL_PIN(65, "MMC1_D5"),
584 	PINCTRL_PIN(66, "SPI1_CS1_B"),
585 	PINCTRL_PIN(67, "MMC1_D4_SD_WE"),
586 	PINCTRL_PIN(68, "MMC1_D7"),
587 	PINCTRL_PIN(69, "MMC1_RCLK"),
588 
589 	PINCTRL_PIN(75, "USB_OC1_B"),
590 	PINCTRL_PIN(76, "PMU_RESETBUTTON_B"),
591 	PINCTRL_PIN(77, "GPIO_ALERT"),
592 	PINCTRL_PIN(78, "SDMMC3_PWR_EN_B"),
593 	PINCTRL_PIN(79, "ILB_SERIRQ"),
594 	PINCTRL_PIN(80, "USB_OC0_B"),
595 	PINCTRL_PIN(81, "SDMMC3_CD_B"),
596 	PINCTRL_PIN(82, "SPKR"),
597 	PINCTRL_PIN(83, "SUSPWRDNACK"),
598 	PINCTRL_PIN(84, "SPARE_PIN"),
599 	PINCTRL_PIN(85, "SDMMC3_1P8_EN"),
600 };
601 
602 static const unsigned southeast_pwm0_pins[] = { 5 };
603 static const unsigned southeast_pwm1_pins[] = { 1 };
604 static const unsigned southeast_sdmmc1_pins[] = {
605 	16, 17, 20, 23, 24, 26, 63, 65, 67, 68, 69,
606 };
607 static const unsigned southeast_sdmmc2_pins[] = { 15, 18, 19, 21, 22, 25 };
608 static const unsigned southeast_sdmmc3_pins[] = {
609 	30, 31, 32, 33, 34, 35, 78, 81, 85,
610 };
611 static const unsigned southeast_spi1_pins[] = { 60, 61, 62, 64, 66 };
612 static const unsigned southeast_spi2_pins[] = { 2, 3, 4, 6, 7 };
613 
614 static const struct chv_pingroup southeast_groups[] = {
615 	PIN_GROUP("pwm0_grp", southeast_pwm0_pins, 1, false),
616 	PIN_GROUP("pwm1_grp", southeast_pwm1_pins, 1, false),
617 	PIN_GROUP("sdmmc1_grp", southeast_sdmmc1_pins, 1, false),
618 	PIN_GROUP("sdmmc2_grp", southeast_sdmmc2_pins, 1, false),
619 	PIN_GROUP("sdmmc3_grp", southeast_sdmmc3_pins, 1, false),
620 	PIN_GROUP("spi1_grp", southeast_spi1_pins, 1, false),
621 	PIN_GROUP("spi2_grp", southeast_spi2_pins, 4, false),
622 };
623 
624 static const char * const southeast_pwm0_groups[] = { "pwm0_grp" };
625 static const char * const southeast_pwm1_groups[] = { "pwm1_grp" };
626 static const char * const southeast_sdmmc1_groups[] = { "sdmmc1_grp" };
627 static const char * const southeast_sdmmc2_groups[] = { "sdmmc2_grp" };
628 static const char * const southeast_sdmmc3_groups[] = { "sdmmc3_grp" };
629 static const char * const southeast_spi1_groups[] = { "spi1_grp" };
630 static const char * const southeast_spi2_groups[] = { "spi2_grp" };
631 
632 static const struct chv_function southeast_functions[] = {
633 	FUNCTION("pwm0", southeast_pwm0_groups),
634 	FUNCTION("pwm1", southeast_pwm1_groups),
635 	FUNCTION("sdmmc1", southeast_sdmmc1_groups),
636 	FUNCTION("sdmmc2", southeast_sdmmc2_groups),
637 	FUNCTION("sdmmc3", southeast_sdmmc3_groups),
638 	FUNCTION("spi1", southeast_spi1_groups),
639 	FUNCTION("spi2", southeast_spi2_groups),
640 };
641 
642 static const struct chv_gpio_pinrange southeast_gpio_ranges[] = {
643 	GPIO_PINRANGE(0, 7),
644 	GPIO_PINRANGE(15, 26),
645 	GPIO_PINRANGE(30, 35),
646 	GPIO_PINRANGE(45, 52),
647 	GPIO_PINRANGE(60, 69),
648 	GPIO_PINRANGE(75, 85),
649 };
650 
651 static const struct chv_community southeast_community = {
652 	.uid = "4",
653 	.pins = southeast_pins,
654 	.npins = ARRAY_SIZE(southeast_pins),
655 	.groups = southeast_groups,
656 	.ngroups = ARRAY_SIZE(southeast_groups),
657 	.functions = southeast_functions,
658 	.nfunctions = ARRAY_SIZE(southeast_functions),
659 	.gpio_ranges = southeast_gpio_ranges,
660 	.ngpio_ranges = ARRAY_SIZE(southeast_gpio_ranges),
661 	.nirqs = 16,
662 	.acpi_space_id = 0x94,
663 };
664 
665 static const struct chv_community *chv_communities[] = {
666 	&southwest_community,
667 	&north_community,
668 	&east_community,
669 	&southeast_community,
670 };
671 
672 /*
673  * Lock to serialize register accesses
674  *
675  * Due to a silicon issue, a shared lock must be used to prevent
676  * concurrent accesses across the 4 GPIO controllers.
677  *
678  * See Intel Atom Z8000 Processor Series Specification Update (Rev. 005),
679  * errata #CHT34, for further information.
680  */
681 static DEFINE_RAW_SPINLOCK(chv_lock);
682 
chv_padreg(struct chv_pinctrl * pctrl,unsigned offset,unsigned reg)683 static void __iomem *chv_padreg(struct chv_pinctrl *pctrl, unsigned offset,
684 				unsigned reg)
685 {
686 	unsigned family_no = offset / MAX_FAMILY_PAD_GPIO_NO;
687 	unsigned pad_no = offset % MAX_FAMILY_PAD_GPIO_NO;
688 
689 	offset = FAMILY_PAD_REGS_OFF + FAMILY_PAD_REGS_SIZE * family_no +
690 		 GPIO_REGS_SIZE * pad_no;
691 
692 	return pctrl->regs + offset + reg;
693 }
694 
chv_writel(u32 value,void __iomem * reg)695 static void chv_writel(u32 value, void __iomem *reg)
696 {
697 	writel(value, reg);
698 	/* simple readback to confirm the bus transferring done */
699 	readl(reg);
700 }
701 
702 /* When Pad Cfg is locked, driver can only change GPIOTXState or GPIORXState */
chv_pad_locked(struct chv_pinctrl * pctrl,unsigned offset)703 static bool chv_pad_locked(struct chv_pinctrl *pctrl, unsigned offset)
704 {
705 	void __iomem *reg;
706 
707 	reg = chv_padreg(pctrl, offset, CHV_PADCTRL1);
708 	return readl(reg) & CHV_PADCTRL1_CFGLOCK;
709 }
710 
chv_get_groups_count(struct pinctrl_dev * pctldev)711 static int chv_get_groups_count(struct pinctrl_dev *pctldev)
712 {
713 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
714 
715 	return pctrl->community->ngroups;
716 }
717 
chv_get_group_name(struct pinctrl_dev * pctldev,unsigned group)718 static const char *chv_get_group_name(struct pinctrl_dev *pctldev,
719 				      unsigned group)
720 {
721 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
722 
723 	return pctrl->community->groups[group].name;
724 }
725 
chv_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * npins)726 static int chv_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
727 			      const unsigned **pins, unsigned *npins)
728 {
729 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
730 
731 	*pins = pctrl->community->groups[group].pins;
732 	*npins = pctrl->community->groups[group].npins;
733 	return 0;
734 }
735 
chv_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned offset)736 static void chv_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
737 			     unsigned offset)
738 {
739 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
740 	unsigned long flags;
741 	u32 ctrl0, ctrl1;
742 	bool locked;
743 
744 	raw_spin_lock_irqsave(&chv_lock, flags);
745 
746 	ctrl0 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
747 	ctrl1 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL1));
748 	locked = chv_pad_locked(pctrl, offset);
749 
750 	raw_spin_unlock_irqrestore(&chv_lock, flags);
751 
752 	if (ctrl0 & CHV_PADCTRL0_GPIOEN) {
753 		seq_puts(s, "GPIO ");
754 	} else {
755 		u32 mode;
756 
757 		mode = ctrl0 & CHV_PADCTRL0_PMODE_MASK;
758 		mode >>= CHV_PADCTRL0_PMODE_SHIFT;
759 
760 		seq_printf(s, "mode %d ", mode);
761 	}
762 
763 	seq_printf(s, "0x%08x 0x%08x", ctrl0, ctrl1);
764 
765 	if (locked)
766 		seq_puts(s, " [LOCKED]");
767 }
768 
769 static const struct pinctrl_ops chv_pinctrl_ops = {
770 	.get_groups_count = chv_get_groups_count,
771 	.get_group_name = chv_get_group_name,
772 	.get_group_pins = chv_get_group_pins,
773 	.pin_dbg_show = chv_pin_dbg_show,
774 };
775 
chv_get_functions_count(struct pinctrl_dev * pctldev)776 static int chv_get_functions_count(struct pinctrl_dev *pctldev)
777 {
778 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
779 
780 	return pctrl->community->nfunctions;
781 }
782 
chv_get_function_name(struct pinctrl_dev * pctldev,unsigned function)783 static const char *chv_get_function_name(struct pinctrl_dev *pctldev,
784 					 unsigned function)
785 {
786 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
787 
788 	return pctrl->community->functions[function].name;
789 }
790 
chv_get_function_groups(struct pinctrl_dev * pctldev,unsigned function,const char * const ** groups,unsigned * const ngroups)791 static int chv_get_function_groups(struct pinctrl_dev *pctldev,
792 				   unsigned function,
793 				   const char * const **groups,
794 				   unsigned * const ngroups)
795 {
796 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
797 
798 	*groups = pctrl->community->functions[function].groups;
799 	*ngroups = pctrl->community->functions[function].ngroups;
800 	return 0;
801 }
802 
chv_pinmux_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)803 static int chv_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned function,
804 			      unsigned group)
805 {
806 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
807 	const struct chv_pingroup *grp;
808 	unsigned long flags;
809 	int i;
810 
811 	grp = &pctrl->community->groups[group];
812 
813 	raw_spin_lock_irqsave(&chv_lock, flags);
814 
815 	/* Check first that the pad is not locked */
816 	for (i = 0; i < grp->npins; i++) {
817 		if (chv_pad_locked(pctrl, grp->pins[i])) {
818 			dev_warn(pctrl->dev, "unable to set mode for locked pin %u\n",
819 				 grp->pins[i]);
820 			raw_spin_unlock_irqrestore(&chv_lock, flags);
821 			return -EBUSY;
822 		}
823 	}
824 
825 	for (i = 0; i < grp->npins; i++) {
826 		const struct chv_alternate_function *altfunc = &grp->altfunc;
827 		int pin = grp->pins[i];
828 		void __iomem *reg;
829 		u32 value;
830 
831 		/* Check if there is pin-specific config */
832 		if (grp->overrides) {
833 			int j;
834 
835 			for (j = 0; j < grp->noverrides; j++) {
836 				if (grp->overrides[j].pin == pin) {
837 					altfunc = &grp->overrides[j];
838 					break;
839 				}
840 			}
841 		}
842 
843 		reg = chv_padreg(pctrl, pin, CHV_PADCTRL0);
844 		value = readl(reg);
845 		/* Disable GPIO mode */
846 		value &= ~CHV_PADCTRL0_GPIOEN;
847 		/* Set to desired mode */
848 		value &= ~CHV_PADCTRL0_PMODE_MASK;
849 		value |= altfunc->mode << CHV_PADCTRL0_PMODE_SHIFT;
850 		chv_writel(value, reg);
851 
852 		/* Update for invert_oe */
853 		reg = chv_padreg(pctrl, pin, CHV_PADCTRL1);
854 		value = readl(reg) & ~CHV_PADCTRL1_INVRXTX_MASK;
855 		if (altfunc->invert_oe)
856 			value |= CHV_PADCTRL1_INVRXTX_TXENABLE;
857 		chv_writel(value, reg);
858 
859 		dev_dbg(pctrl->dev, "configured pin %u mode %u OE %sinverted\n",
860 			pin, altfunc->mode, altfunc->invert_oe ? "" : "not ");
861 	}
862 
863 	raw_spin_unlock_irqrestore(&chv_lock, flags);
864 
865 	return 0;
866 }
867 
chv_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)868 static int chv_gpio_request_enable(struct pinctrl_dev *pctldev,
869 				   struct pinctrl_gpio_range *range,
870 				   unsigned offset)
871 {
872 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
873 	unsigned long flags;
874 	void __iomem *reg;
875 	u32 value;
876 
877 	raw_spin_lock_irqsave(&chv_lock, flags);
878 
879 	if (chv_pad_locked(pctrl, offset)) {
880 		value = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
881 		if (!(value & CHV_PADCTRL0_GPIOEN)) {
882 			/* Locked so cannot enable */
883 			raw_spin_unlock_irqrestore(&chv_lock, flags);
884 			return -EBUSY;
885 		}
886 	} else {
887 		int i;
888 
889 		/* Reset the interrupt mapping */
890 		for (i = 0; i < ARRAY_SIZE(pctrl->intr_lines); i++) {
891 			if (pctrl->intr_lines[i] == offset) {
892 				pctrl->intr_lines[i] = 0;
893 				break;
894 			}
895 		}
896 
897 		/* Disable interrupt generation */
898 		reg = chv_padreg(pctrl, offset, CHV_PADCTRL1);
899 		value = readl(reg);
900 		value &= ~CHV_PADCTRL1_INTWAKECFG_MASK;
901 		value &= ~CHV_PADCTRL1_INVRXTX_MASK;
902 		chv_writel(value, reg);
903 
904 		reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
905 		value = readl(reg);
906 
907 		/*
908 		 * If the pin is in HiZ mode (both TX and RX buffers are
909 		 * disabled) we turn it to be input now.
910 		 */
911 		if ((value & CHV_PADCTRL0_GPIOCFG_MASK) ==
912 		     (CHV_PADCTRL0_GPIOCFG_HIZ << CHV_PADCTRL0_GPIOCFG_SHIFT)) {
913 			value &= ~CHV_PADCTRL0_GPIOCFG_MASK;
914 			value |= CHV_PADCTRL0_GPIOCFG_GPI <<
915 				CHV_PADCTRL0_GPIOCFG_SHIFT;
916 		}
917 
918 		/* Switch to a GPIO mode */
919 		value |= CHV_PADCTRL0_GPIOEN;
920 		chv_writel(value, reg);
921 	}
922 
923 	raw_spin_unlock_irqrestore(&chv_lock, flags);
924 
925 	return 0;
926 }
927 
chv_gpio_disable_free(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)928 static void chv_gpio_disable_free(struct pinctrl_dev *pctldev,
929 				  struct pinctrl_gpio_range *range,
930 				  unsigned offset)
931 {
932 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
933 	unsigned long flags;
934 	void __iomem *reg;
935 	u32 value;
936 
937 	raw_spin_lock_irqsave(&chv_lock, flags);
938 
939 	reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
940 	value = readl(reg) & ~CHV_PADCTRL0_GPIOEN;
941 	chv_writel(value, reg);
942 
943 	raw_spin_unlock_irqrestore(&chv_lock, flags);
944 }
945 
chv_gpio_set_direction(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset,bool input)946 static int chv_gpio_set_direction(struct pinctrl_dev *pctldev,
947 				  struct pinctrl_gpio_range *range,
948 				  unsigned offset, bool input)
949 {
950 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
951 	void __iomem *reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
952 	unsigned long flags;
953 	u32 ctrl0;
954 
955 	raw_spin_lock_irqsave(&chv_lock, flags);
956 
957 	ctrl0 = readl(reg) & ~CHV_PADCTRL0_GPIOCFG_MASK;
958 	if (input)
959 		ctrl0 |= CHV_PADCTRL0_GPIOCFG_GPI << CHV_PADCTRL0_GPIOCFG_SHIFT;
960 	else
961 		ctrl0 |= CHV_PADCTRL0_GPIOCFG_GPO << CHV_PADCTRL0_GPIOCFG_SHIFT;
962 	chv_writel(ctrl0, reg);
963 
964 	raw_spin_unlock_irqrestore(&chv_lock, flags);
965 
966 	return 0;
967 }
968 
969 static const struct pinmux_ops chv_pinmux_ops = {
970 	.get_functions_count = chv_get_functions_count,
971 	.get_function_name = chv_get_function_name,
972 	.get_function_groups = chv_get_function_groups,
973 	.set_mux = chv_pinmux_set_mux,
974 	.gpio_request_enable = chv_gpio_request_enable,
975 	.gpio_disable_free = chv_gpio_disable_free,
976 	.gpio_set_direction = chv_gpio_set_direction,
977 };
978 
chv_config_get(struct pinctrl_dev * pctldev,unsigned pin,unsigned long * config)979 static int chv_config_get(struct pinctrl_dev *pctldev, unsigned pin,
980 			  unsigned long *config)
981 {
982 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
983 	enum pin_config_param param = pinconf_to_config_param(*config);
984 	unsigned long flags;
985 	u32 ctrl0, ctrl1;
986 	u16 arg = 0;
987 	u32 term;
988 
989 	raw_spin_lock_irqsave(&chv_lock, flags);
990 	ctrl0 = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
991 	ctrl1 = readl(chv_padreg(pctrl, pin, CHV_PADCTRL1));
992 	raw_spin_unlock_irqrestore(&chv_lock, flags);
993 
994 	term = (ctrl0 & CHV_PADCTRL0_TERM_MASK) >> CHV_PADCTRL0_TERM_SHIFT;
995 
996 	switch (param) {
997 	case PIN_CONFIG_BIAS_DISABLE:
998 		if (term)
999 			return -EINVAL;
1000 		break;
1001 
1002 	case PIN_CONFIG_BIAS_PULL_UP:
1003 		if (!(ctrl0 & CHV_PADCTRL0_TERM_UP))
1004 			return -EINVAL;
1005 
1006 		switch (term) {
1007 		case CHV_PADCTRL0_TERM_20K:
1008 			arg = 20000;
1009 			break;
1010 		case CHV_PADCTRL0_TERM_5K:
1011 			arg = 5000;
1012 			break;
1013 		case CHV_PADCTRL0_TERM_1K:
1014 			arg = 1000;
1015 			break;
1016 		}
1017 
1018 		break;
1019 
1020 	case PIN_CONFIG_BIAS_PULL_DOWN:
1021 		if (!term || (ctrl0 & CHV_PADCTRL0_TERM_UP))
1022 			return -EINVAL;
1023 
1024 		switch (term) {
1025 		case CHV_PADCTRL0_TERM_20K:
1026 			arg = 20000;
1027 			break;
1028 		case CHV_PADCTRL0_TERM_5K:
1029 			arg = 5000;
1030 			break;
1031 		}
1032 
1033 		break;
1034 
1035 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
1036 		if (!(ctrl1 & CHV_PADCTRL1_ODEN))
1037 			return -EINVAL;
1038 		break;
1039 
1040 	case PIN_CONFIG_BIAS_HIGH_IMPEDANCE: {
1041 		u32 cfg;
1042 
1043 		cfg = ctrl0 & CHV_PADCTRL0_GPIOCFG_MASK;
1044 		cfg >>= CHV_PADCTRL0_GPIOCFG_SHIFT;
1045 		if (cfg != CHV_PADCTRL0_GPIOCFG_HIZ)
1046 			return -EINVAL;
1047 
1048 		break;
1049 	}
1050 
1051 	default:
1052 		return -ENOTSUPP;
1053 	}
1054 
1055 	*config = pinconf_to_config_packed(param, arg);
1056 	return 0;
1057 }
1058 
chv_config_set_pull(struct chv_pinctrl * pctrl,unsigned pin,enum pin_config_param param,u32 arg)1059 static int chv_config_set_pull(struct chv_pinctrl *pctrl, unsigned pin,
1060 			       enum pin_config_param param, u32 arg)
1061 {
1062 	void __iomem *reg = chv_padreg(pctrl, pin, CHV_PADCTRL0);
1063 	unsigned long flags;
1064 	u32 ctrl0, pull;
1065 
1066 	raw_spin_lock_irqsave(&chv_lock, flags);
1067 	ctrl0 = readl(reg);
1068 
1069 	switch (param) {
1070 	case PIN_CONFIG_BIAS_DISABLE:
1071 		ctrl0 &= ~(CHV_PADCTRL0_TERM_MASK | CHV_PADCTRL0_TERM_UP);
1072 		break;
1073 
1074 	case PIN_CONFIG_BIAS_PULL_UP:
1075 		ctrl0 &= ~(CHV_PADCTRL0_TERM_MASK | CHV_PADCTRL0_TERM_UP);
1076 
1077 		switch (arg) {
1078 		case 1000:
1079 			/* For 1k there is only pull up */
1080 			pull = CHV_PADCTRL0_TERM_1K << CHV_PADCTRL0_TERM_SHIFT;
1081 			break;
1082 		case 5000:
1083 			pull = CHV_PADCTRL0_TERM_5K << CHV_PADCTRL0_TERM_SHIFT;
1084 			break;
1085 		case 20000:
1086 			pull = CHV_PADCTRL0_TERM_20K << CHV_PADCTRL0_TERM_SHIFT;
1087 			break;
1088 		default:
1089 			raw_spin_unlock_irqrestore(&chv_lock, flags);
1090 			return -EINVAL;
1091 		}
1092 
1093 		ctrl0 |= CHV_PADCTRL0_TERM_UP | pull;
1094 		break;
1095 
1096 	case PIN_CONFIG_BIAS_PULL_DOWN:
1097 		ctrl0 &= ~(CHV_PADCTRL0_TERM_MASK | CHV_PADCTRL0_TERM_UP);
1098 
1099 		switch (arg) {
1100 		case 5000:
1101 			pull = CHV_PADCTRL0_TERM_5K << CHV_PADCTRL0_TERM_SHIFT;
1102 			break;
1103 		case 20000:
1104 			pull = CHV_PADCTRL0_TERM_20K << CHV_PADCTRL0_TERM_SHIFT;
1105 			break;
1106 		default:
1107 			raw_spin_unlock_irqrestore(&chv_lock, flags);
1108 			return -EINVAL;
1109 		}
1110 
1111 		ctrl0 |= pull;
1112 		break;
1113 
1114 	default:
1115 		raw_spin_unlock_irqrestore(&chv_lock, flags);
1116 		return -EINVAL;
1117 	}
1118 
1119 	chv_writel(ctrl0, reg);
1120 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1121 
1122 	return 0;
1123 }
1124 
chv_config_set_oden(struct chv_pinctrl * pctrl,unsigned int pin,bool enable)1125 static int chv_config_set_oden(struct chv_pinctrl *pctrl, unsigned int pin,
1126 			       bool enable)
1127 {
1128 	void __iomem *reg = chv_padreg(pctrl, pin, CHV_PADCTRL1);
1129 	unsigned long flags;
1130 	u32 ctrl1;
1131 
1132 	raw_spin_lock_irqsave(&chv_lock, flags);
1133 	ctrl1 = readl(reg);
1134 
1135 	if (enable)
1136 		ctrl1 |= CHV_PADCTRL1_ODEN;
1137 	else
1138 		ctrl1 &= ~CHV_PADCTRL1_ODEN;
1139 
1140 	chv_writel(ctrl1, reg);
1141 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1142 
1143 	return 0;
1144 }
1145 
chv_config_set(struct pinctrl_dev * pctldev,unsigned pin,unsigned long * configs,unsigned nconfigs)1146 static int chv_config_set(struct pinctrl_dev *pctldev, unsigned pin,
1147 			  unsigned long *configs, unsigned nconfigs)
1148 {
1149 	struct chv_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
1150 	enum pin_config_param param;
1151 	int i, ret;
1152 	u32 arg;
1153 
1154 	if (chv_pad_locked(pctrl, pin))
1155 		return -EBUSY;
1156 
1157 	for (i = 0; i < nconfigs; i++) {
1158 		param = pinconf_to_config_param(configs[i]);
1159 		arg = pinconf_to_config_argument(configs[i]);
1160 
1161 		switch (param) {
1162 		case PIN_CONFIG_BIAS_DISABLE:
1163 		case PIN_CONFIG_BIAS_PULL_UP:
1164 		case PIN_CONFIG_BIAS_PULL_DOWN:
1165 			ret = chv_config_set_pull(pctrl, pin, param, arg);
1166 			if (ret)
1167 				return ret;
1168 			break;
1169 
1170 		case PIN_CONFIG_DRIVE_PUSH_PULL:
1171 			ret = chv_config_set_oden(pctrl, pin, false);
1172 			if (ret)
1173 				return ret;
1174 			break;
1175 
1176 		case PIN_CONFIG_DRIVE_OPEN_DRAIN:
1177 			ret = chv_config_set_oden(pctrl, pin, true);
1178 			if (ret)
1179 				return ret;
1180 			break;
1181 
1182 		default:
1183 			return -ENOTSUPP;
1184 		}
1185 
1186 		dev_dbg(pctrl->dev, "pin %d set config %d arg %u\n", pin,
1187 			param, arg);
1188 	}
1189 
1190 	return 0;
1191 }
1192 
chv_config_group_get(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * config)1193 static int chv_config_group_get(struct pinctrl_dev *pctldev,
1194 				unsigned int group,
1195 				unsigned long *config)
1196 {
1197 	const unsigned int *pins;
1198 	unsigned int npins;
1199 	int ret;
1200 
1201 	ret = chv_get_group_pins(pctldev, group, &pins, &npins);
1202 	if (ret)
1203 		return ret;
1204 
1205 	ret = chv_config_get(pctldev, pins[0], config);
1206 	if (ret)
1207 		return ret;
1208 
1209 	return 0;
1210 }
1211 
chv_config_group_set(struct pinctrl_dev * pctldev,unsigned int group,unsigned long * configs,unsigned int num_configs)1212 static int chv_config_group_set(struct pinctrl_dev *pctldev,
1213 				unsigned int group, unsigned long *configs,
1214 				unsigned int num_configs)
1215 {
1216 	const unsigned int *pins;
1217 	unsigned int npins;
1218 	int i, ret;
1219 
1220 	ret = chv_get_group_pins(pctldev, group, &pins, &npins);
1221 	if (ret)
1222 		return ret;
1223 
1224 	for (i = 0; i < npins; i++) {
1225 		ret = chv_config_set(pctldev, pins[i], configs, num_configs);
1226 		if (ret)
1227 			return ret;
1228 	}
1229 
1230 	return 0;
1231 }
1232 
1233 static const struct pinconf_ops chv_pinconf_ops = {
1234 	.is_generic = true,
1235 	.pin_config_set = chv_config_set,
1236 	.pin_config_get = chv_config_get,
1237 	.pin_config_group_get = chv_config_group_get,
1238 	.pin_config_group_set = chv_config_group_set,
1239 };
1240 
1241 static struct pinctrl_desc chv_pinctrl_desc = {
1242 	.pctlops = &chv_pinctrl_ops,
1243 	.pmxops = &chv_pinmux_ops,
1244 	.confops = &chv_pinconf_ops,
1245 	.owner = THIS_MODULE,
1246 };
1247 
chv_gpio_get(struct gpio_chip * chip,unsigned offset)1248 static int chv_gpio_get(struct gpio_chip *chip, unsigned offset)
1249 {
1250 	struct chv_pinctrl *pctrl = gpiochip_get_data(chip);
1251 	unsigned long flags;
1252 	u32 ctrl0, cfg;
1253 
1254 	raw_spin_lock_irqsave(&chv_lock, flags);
1255 	ctrl0 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
1256 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1257 
1258 	cfg = ctrl0 & CHV_PADCTRL0_GPIOCFG_MASK;
1259 	cfg >>= CHV_PADCTRL0_GPIOCFG_SHIFT;
1260 
1261 	if (cfg == CHV_PADCTRL0_GPIOCFG_GPO)
1262 		return !!(ctrl0 & CHV_PADCTRL0_GPIOTXSTATE);
1263 	return !!(ctrl0 & CHV_PADCTRL0_GPIORXSTATE);
1264 }
1265 
chv_gpio_set(struct gpio_chip * chip,unsigned offset,int value)1266 static void chv_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1267 {
1268 	struct chv_pinctrl *pctrl = gpiochip_get_data(chip);
1269 	unsigned long flags;
1270 	void __iomem *reg;
1271 	u32 ctrl0;
1272 
1273 	raw_spin_lock_irqsave(&chv_lock, flags);
1274 
1275 	reg = chv_padreg(pctrl, offset, CHV_PADCTRL0);
1276 	ctrl0 = readl(reg);
1277 
1278 	if (value)
1279 		ctrl0 |= CHV_PADCTRL0_GPIOTXSTATE;
1280 	else
1281 		ctrl0 &= ~CHV_PADCTRL0_GPIOTXSTATE;
1282 
1283 	chv_writel(ctrl0, reg);
1284 
1285 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1286 }
1287 
chv_gpio_get_direction(struct gpio_chip * chip,unsigned offset)1288 static int chv_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1289 {
1290 	struct chv_pinctrl *pctrl = gpiochip_get_data(chip);
1291 	u32 ctrl0, direction;
1292 	unsigned long flags;
1293 
1294 	raw_spin_lock_irqsave(&chv_lock, flags);
1295 	ctrl0 = readl(chv_padreg(pctrl, offset, CHV_PADCTRL0));
1296 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1297 
1298 	direction = ctrl0 & CHV_PADCTRL0_GPIOCFG_MASK;
1299 	direction >>= CHV_PADCTRL0_GPIOCFG_SHIFT;
1300 
1301 	return direction != CHV_PADCTRL0_GPIOCFG_GPO;
1302 }
1303 
chv_gpio_direction_input(struct gpio_chip * chip,unsigned offset)1304 static int chv_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1305 {
1306 	return pinctrl_gpio_direction_input(chip->base + offset);
1307 }
1308 
chv_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)1309 static int chv_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1310 				     int value)
1311 {
1312 	chv_gpio_set(chip, offset, value);
1313 	return pinctrl_gpio_direction_output(chip->base + offset);
1314 }
1315 
1316 static const struct gpio_chip chv_gpio_chip = {
1317 	.owner = THIS_MODULE,
1318 	.request = gpiochip_generic_request,
1319 	.free = gpiochip_generic_free,
1320 	.get_direction = chv_gpio_get_direction,
1321 	.direction_input = chv_gpio_direction_input,
1322 	.direction_output = chv_gpio_direction_output,
1323 	.get = chv_gpio_get,
1324 	.set = chv_gpio_set,
1325 };
1326 
chv_gpio_irq_ack(struct irq_data * d)1327 static void chv_gpio_irq_ack(struct irq_data *d)
1328 {
1329 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1330 	struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
1331 	int pin = irqd_to_hwirq(d);
1332 	u32 intr_line;
1333 
1334 	raw_spin_lock(&chv_lock);
1335 
1336 	intr_line = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
1337 	intr_line &= CHV_PADCTRL0_INTSEL_MASK;
1338 	intr_line >>= CHV_PADCTRL0_INTSEL_SHIFT;
1339 	chv_writel(BIT(intr_line), pctrl->regs + CHV_INTSTAT);
1340 
1341 	raw_spin_unlock(&chv_lock);
1342 }
1343 
chv_gpio_irq_mask_unmask(struct irq_data * d,bool mask)1344 static void chv_gpio_irq_mask_unmask(struct irq_data *d, bool mask)
1345 {
1346 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1347 	struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
1348 	int pin = irqd_to_hwirq(d);
1349 	u32 value, intr_line;
1350 	unsigned long flags;
1351 
1352 	raw_spin_lock_irqsave(&chv_lock, flags);
1353 
1354 	intr_line = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
1355 	intr_line &= CHV_PADCTRL0_INTSEL_MASK;
1356 	intr_line >>= CHV_PADCTRL0_INTSEL_SHIFT;
1357 
1358 	value = readl(pctrl->regs + CHV_INTMASK);
1359 	if (mask)
1360 		value &= ~BIT(intr_line);
1361 	else
1362 		value |= BIT(intr_line);
1363 	chv_writel(value, pctrl->regs + CHV_INTMASK);
1364 
1365 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1366 }
1367 
chv_gpio_irq_mask(struct irq_data * d)1368 static void chv_gpio_irq_mask(struct irq_data *d)
1369 {
1370 	chv_gpio_irq_mask_unmask(d, true);
1371 }
1372 
chv_gpio_irq_unmask(struct irq_data * d)1373 static void chv_gpio_irq_unmask(struct irq_data *d)
1374 {
1375 	chv_gpio_irq_mask_unmask(d, false);
1376 }
1377 
chv_gpio_irq_startup(struct irq_data * d)1378 static unsigned chv_gpio_irq_startup(struct irq_data *d)
1379 {
1380 	/*
1381 	 * Check if the interrupt has been requested with 0 as triggering
1382 	 * type. In that case it is assumed that the current values
1383 	 * programmed to the hardware are used (e.g BIOS configured
1384 	 * defaults).
1385 	 *
1386 	 * In that case ->irq_set_type() will never be called so we need to
1387 	 * read back the values from hardware now, set correct flow handler
1388 	 * and update mappings before the interrupt is being used.
1389 	 */
1390 	if (irqd_get_trigger_type(d) == IRQ_TYPE_NONE) {
1391 		struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1392 		struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
1393 		unsigned pin = irqd_to_hwirq(d);
1394 		irq_flow_handler_t handler;
1395 		unsigned long flags;
1396 		u32 intsel, value;
1397 
1398 		raw_spin_lock_irqsave(&chv_lock, flags);
1399 		intsel = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
1400 		intsel &= CHV_PADCTRL0_INTSEL_MASK;
1401 		intsel >>= CHV_PADCTRL0_INTSEL_SHIFT;
1402 
1403 		value = readl(chv_padreg(pctrl, pin, CHV_PADCTRL1));
1404 		if (value & CHV_PADCTRL1_INTWAKECFG_LEVEL)
1405 			handler = handle_level_irq;
1406 		else
1407 			handler = handle_edge_irq;
1408 
1409 		if (!pctrl->intr_lines[intsel]) {
1410 			irq_set_handler_locked(d, handler);
1411 			pctrl->intr_lines[intsel] = pin;
1412 		}
1413 		raw_spin_unlock_irqrestore(&chv_lock, flags);
1414 	}
1415 
1416 	chv_gpio_irq_unmask(d);
1417 	return 0;
1418 }
1419 
chv_gpio_irq_type(struct irq_data * d,unsigned type)1420 static int chv_gpio_irq_type(struct irq_data *d, unsigned type)
1421 {
1422 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1423 	struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
1424 	unsigned pin = irqd_to_hwirq(d);
1425 	unsigned long flags;
1426 	u32 value;
1427 
1428 	raw_spin_lock_irqsave(&chv_lock, flags);
1429 
1430 	/*
1431 	 * Pins which can be used as shared interrupt are configured in
1432 	 * BIOS. Driver trusts BIOS configurations and assigns different
1433 	 * handler according to the irq type.
1434 	 *
1435 	 * Driver needs to save the mapping between each pin and
1436 	 * its interrupt line.
1437 	 * 1. If the pin cfg is locked in BIOS:
1438 	 *	Trust BIOS has programmed IntWakeCfg bits correctly,
1439 	 *	driver just needs to save the mapping.
1440 	 * 2. If the pin cfg is not locked in BIOS:
1441 	 *	Driver programs the IntWakeCfg bits and save the mapping.
1442 	 */
1443 	if (!chv_pad_locked(pctrl, pin)) {
1444 		void __iomem *reg = chv_padreg(pctrl, pin, CHV_PADCTRL1);
1445 
1446 		value = readl(reg);
1447 		value &= ~CHV_PADCTRL1_INTWAKECFG_MASK;
1448 		value &= ~CHV_PADCTRL1_INVRXTX_MASK;
1449 
1450 		if (type & IRQ_TYPE_EDGE_BOTH) {
1451 			if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
1452 				value |= CHV_PADCTRL1_INTWAKECFG_BOTH;
1453 			else if (type & IRQ_TYPE_EDGE_RISING)
1454 				value |= CHV_PADCTRL1_INTWAKECFG_RISING;
1455 			else if (type & IRQ_TYPE_EDGE_FALLING)
1456 				value |= CHV_PADCTRL1_INTWAKECFG_FALLING;
1457 		} else if (type & IRQ_TYPE_LEVEL_MASK) {
1458 			value |= CHV_PADCTRL1_INTWAKECFG_LEVEL;
1459 			if (type & IRQ_TYPE_LEVEL_LOW)
1460 				value |= CHV_PADCTRL1_INVRXTX_RXDATA;
1461 		}
1462 
1463 		chv_writel(value, reg);
1464 	}
1465 
1466 	value = readl(chv_padreg(pctrl, pin, CHV_PADCTRL0));
1467 	value &= CHV_PADCTRL0_INTSEL_MASK;
1468 	value >>= CHV_PADCTRL0_INTSEL_SHIFT;
1469 
1470 	pctrl->intr_lines[value] = pin;
1471 
1472 	if (type & IRQ_TYPE_EDGE_BOTH)
1473 		irq_set_handler_locked(d, handle_edge_irq);
1474 	else if (type & IRQ_TYPE_LEVEL_MASK)
1475 		irq_set_handler_locked(d, handle_level_irq);
1476 
1477 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1478 
1479 	return 0;
1480 }
1481 
chv_gpio_irq_handler(struct irq_desc * desc)1482 static void chv_gpio_irq_handler(struct irq_desc *desc)
1483 {
1484 	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1485 	struct chv_pinctrl *pctrl = gpiochip_get_data(gc);
1486 	struct irq_chip *chip = irq_desc_get_chip(desc);
1487 	unsigned long pending;
1488 	unsigned long flags;
1489 	u32 intr_line;
1490 
1491 	chained_irq_enter(chip, desc);
1492 
1493 	raw_spin_lock_irqsave(&chv_lock, flags);
1494 	pending = readl(pctrl->regs + CHV_INTSTAT);
1495 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1496 
1497 	for_each_set_bit(intr_line, &pending, pctrl->community->nirqs) {
1498 		unsigned irq, offset;
1499 
1500 		offset = pctrl->intr_lines[intr_line];
1501 		irq = irq_find_mapping(gc->irq.domain, offset);
1502 		generic_handle_irq(irq);
1503 	}
1504 
1505 	chained_irq_exit(chip, desc);
1506 }
1507 
1508 /*
1509  * Certain machines seem to hardcode Linux IRQ numbers in their ACPI
1510  * tables. Since we leave GPIOs that are not capable of generating
1511  * interrupts out of the irqdomain the numbering will be different and
1512  * cause devices using the hardcoded IRQ numbers fail. In order not to
1513  * break such machines we will only mask pins from irqdomain if the machine
1514  * is not listed below.
1515  */
1516 static const struct dmi_system_id chv_no_valid_mask[] = {
1517 	/* See https://bugzilla.kernel.org/show_bug.cgi?id=194945 */
1518 	{
1519 		.ident = "Intel_Strago based Chromebooks (All models)",
1520 		.matches = {
1521 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
1522 			DMI_MATCH(DMI_PRODUCT_FAMILY, "Intel_Strago"),
1523 		},
1524 	},
1525 	{
1526 		.ident = "HP Chromebook 11 G5 (Setzer)",
1527 		.matches = {
1528 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
1529 			DMI_MATCH(DMI_PRODUCT_NAME, "Setzer"),
1530 		},
1531 	},
1532 	{
1533 		.ident = "Acer Chromebook R11 (Cyan)",
1534 		.matches = {
1535 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
1536 			DMI_MATCH(DMI_PRODUCT_NAME, "Cyan"),
1537 		},
1538 	},
1539 	{
1540 		.ident = "Samsung Chromebook 3 (Celes)",
1541 		.matches = {
1542 			DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
1543 			DMI_MATCH(DMI_PRODUCT_NAME, "Celes"),
1544 		},
1545 	},
1546 	{}
1547 };
1548 
chv_gpio_probe(struct chv_pinctrl * pctrl,int irq)1549 static int chv_gpio_probe(struct chv_pinctrl *pctrl, int irq)
1550 {
1551 	const struct chv_gpio_pinrange *range;
1552 	struct gpio_chip *chip = &pctrl->chip;
1553 	bool need_valid_mask = !dmi_check_system(chv_no_valid_mask);
1554 	const struct chv_community *community = pctrl->community;
1555 	int ret, i, irq_base;
1556 
1557 	*chip = chv_gpio_chip;
1558 
1559 	chip->ngpio = community->pins[community->npins - 1].number + 1;
1560 	chip->label = dev_name(pctrl->dev);
1561 	chip->parent = pctrl->dev;
1562 	chip->base = -1;
1563 	chip->irq.need_valid_mask = need_valid_mask;
1564 
1565 	ret = devm_gpiochip_add_data(pctrl->dev, chip, pctrl);
1566 	if (ret) {
1567 		dev_err(pctrl->dev, "Failed to register gpiochip\n");
1568 		return ret;
1569 	}
1570 
1571 	for (i = 0; i < community->ngpio_ranges; i++) {
1572 		range = &community->gpio_ranges[i];
1573 		ret = gpiochip_add_pin_range(chip, dev_name(pctrl->dev),
1574 					     range->base, range->base,
1575 					     range->npins);
1576 		if (ret) {
1577 			dev_err(pctrl->dev, "failed to add GPIO pin range\n");
1578 			return ret;
1579 		}
1580 	}
1581 
1582 	/* Do not add GPIOs that can only generate GPEs to the IRQ domain */
1583 	for (i = 0; i < community->npins; i++) {
1584 		const struct pinctrl_pin_desc *desc;
1585 		u32 intsel;
1586 
1587 		desc = &community->pins[i];
1588 
1589 		intsel = readl(chv_padreg(pctrl, desc->number, CHV_PADCTRL0));
1590 		intsel &= CHV_PADCTRL0_INTSEL_MASK;
1591 		intsel >>= CHV_PADCTRL0_INTSEL_SHIFT;
1592 
1593 		if (need_valid_mask && intsel >= community->nirqs)
1594 			clear_bit(desc->number, chip->irq.valid_mask);
1595 	}
1596 
1597 	/*
1598 	 * The same set of machines in chv_no_valid_mask[] have incorrectly
1599 	 * configured GPIOs that generate spurious interrupts so we use
1600 	 * this same list to apply another quirk for them.
1601 	 *
1602 	 * See also https://bugzilla.kernel.org/show_bug.cgi?id=197953.
1603 	 */
1604 	if (!need_valid_mask) {
1605 		/*
1606 		 * Mask all interrupts the community is able to generate
1607 		 * but leave the ones that can only generate GPEs unmasked.
1608 		 */
1609 		chv_writel(GENMASK(31, pctrl->community->nirqs),
1610 			   pctrl->regs + CHV_INTMASK);
1611 	}
1612 
1613 	/* Clear all interrupts */
1614 	chv_writel(0xffff, pctrl->regs + CHV_INTSTAT);
1615 
1616 	if (!need_valid_mask) {
1617 		irq_base = devm_irq_alloc_descs(pctrl->dev, -1, 0,
1618 						community->npins, NUMA_NO_NODE);
1619 		if (irq_base < 0) {
1620 			dev_err(pctrl->dev, "Failed to allocate IRQ numbers\n");
1621 			return irq_base;
1622 		}
1623 	}
1624 
1625 	pctrl->irqchip.name = "chv-gpio";
1626 	pctrl->irqchip.irq_startup = chv_gpio_irq_startup;
1627 	pctrl->irqchip.irq_ack = chv_gpio_irq_ack;
1628 	pctrl->irqchip.irq_mask = chv_gpio_irq_mask;
1629 	pctrl->irqchip.irq_unmask = chv_gpio_irq_unmask;
1630 	pctrl->irqchip.irq_set_type = chv_gpio_irq_type;
1631 	pctrl->irqchip.flags = IRQCHIP_SKIP_SET_WAKE;
1632 
1633 	ret = gpiochip_irqchip_add(chip, &pctrl->irqchip, 0,
1634 				   handle_bad_irq, IRQ_TYPE_NONE);
1635 	if (ret) {
1636 		dev_err(pctrl->dev, "failed to add IRQ chip\n");
1637 		return ret;
1638 	}
1639 
1640 	if (!need_valid_mask) {
1641 		for (i = 0; i < community->ngpio_ranges; i++) {
1642 			range = &community->gpio_ranges[i];
1643 
1644 			irq_domain_associate_many(chip->irq.domain, irq_base,
1645 						  range->base, range->npins);
1646 			irq_base += range->npins;
1647 		}
1648 	}
1649 
1650 	gpiochip_set_chained_irqchip(chip, &pctrl->irqchip, irq,
1651 				     chv_gpio_irq_handler);
1652 	return 0;
1653 }
1654 
chv_pinctrl_mmio_access_handler(u32 function,acpi_physical_address address,u32 bits,u64 * value,void * handler_context,void * region_context)1655 static acpi_status chv_pinctrl_mmio_access_handler(u32 function,
1656 	acpi_physical_address address, u32 bits, u64 *value,
1657 	void *handler_context, void *region_context)
1658 {
1659 	struct chv_pinctrl *pctrl = region_context;
1660 	unsigned long flags;
1661 	acpi_status ret = AE_OK;
1662 
1663 	raw_spin_lock_irqsave(&chv_lock, flags);
1664 
1665 	if (function == ACPI_WRITE)
1666 		chv_writel((u32)(*value), pctrl->regs + (u32)address);
1667 	else if (function == ACPI_READ)
1668 		*value = readl(pctrl->regs + (u32)address);
1669 	else
1670 		ret = AE_BAD_PARAMETER;
1671 
1672 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1673 
1674 	return ret;
1675 }
1676 
chv_pinctrl_probe(struct platform_device * pdev)1677 static int chv_pinctrl_probe(struct platform_device *pdev)
1678 {
1679 	struct chv_pinctrl *pctrl;
1680 	struct acpi_device *adev;
1681 	struct resource *res;
1682 	acpi_status status;
1683 	int ret, irq, i;
1684 
1685 	adev = ACPI_COMPANION(&pdev->dev);
1686 	if (!adev)
1687 		return -ENODEV;
1688 
1689 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1690 	if (!pctrl)
1691 		return -ENOMEM;
1692 
1693 	for (i = 0; i < ARRAY_SIZE(chv_communities); i++)
1694 		if (!strcmp(adev->pnp.unique_id, chv_communities[i]->uid)) {
1695 			pctrl->community = chv_communities[i];
1696 			break;
1697 		}
1698 	if (i == ARRAY_SIZE(chv_communities))
1699 		return -ENODEV;
1700 
1701 	pctrl->dev = &pdev->dev;
1702 
1703 #ifdef CONFIG_PM_SLEEP
1704 	pctrl->saved_pin_context = devm_kcalloc(pctrl->dev,
1705 		pctrl->community->npins, sizeof(*pctrl->saved_pin_context),
1706 		GFP_KERNEL);
1707 	if (!pctrl->saved_pin_context)
1708 		return -ENOMEM;
1709 #endif
1710 
1711 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1712 	pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
1713 	if (IS_ERR(pctrl->regs))
1714 		return PTR_ERR(pctrl->regs);
1715 
1716 	irq = platform_get_irq(pdev, 0);
1717 	if (irq < 0) {
1718 		dev_err(&pdev->dev, "failed to get interrupt number\n");
1719 		return irq;
1720 	}
1721 
1722 	pctrl->pctldesc = chv_pinctrl_desc;
1723 	pctrl->pctldesc.name = dev_name(&pdev->dev);
1724 	pctrl->pctldesc.pins = pctrl->community->pins;
1725 	pctrl->pctldesc.npins = pctrl->community->npins;
1726 
1727 	pctrl->pctldev = devm_pinctrl_register(&pdev->dev, &pctrl->pctldesc,
1728 					       pctrl);
1729 	if (IS_ERR(pctrl->pctldev)) {
1730 		dev_err(&pdev->dev, "failed to register pinctrl driver\n");
1731 		return PTR_ERR(pctrl->pctldev);
1732 	}
1733 
1734 	ret = chv_gpio_probe(pctrl, irq);
1735 	if (ret)
1736 		return ret;
1737 
1738 	status = acpi_install_address_space_handler(adev->handle,
1739 					pctrl->community->acpi_space_id,
1740 					chv_pinctrl_mmio_access_handler,
1741 					NULL, pctrl);
1742 	if (ACPI_FAILURE(status))
1743 		dev_err(&pdev->dev, "failed to install ACPI addr space handler\n");
1744 
1745 	platform_set_drvdata(pdev, pctrl);
1746 
1747 	return 0;
1748 }
1749 
chv_pinctrl_remove(struct platform_device * pdev)1750 static int chv_pinctrl_remove(struct platform_device *pdev)
1751 {
1752 	struct chv_pinctrl *pctrl = platform_get_drvdata(pdev);
1753 
1754 	acpi_remove_address_space_handler(ACPI_COMPANION(&pdev->dev),
1755 					  pctrl->community->acpi_space_id,
1756 					  chv_pinctrl_mmio_access_handler);
1757 
1758 	return 0;
1759 }
1760 
1761 #ifdef CONFIG_PM_SLEEP
chv_pinctrl_suspend_noirq(struct device * dev)1762 static int chv_pinctrl_suspend_noirq(struct device *dev)
1763 {
1764 	struct platform_device *pdev = to_platform_device(dev);
1765 	struct chv_pinctrl *pctrl = platform_get_drvdata(pdev);
1766 	unsigned long flags;
1767 	int i;
1768 
1769 	raw_spin_lock_irqsave(&chv_lock, flags);
1770 
1771 	pctrl->saved_intmask = readl(pctrl->regs + CHV_INTMASK);
1772 
1773 	for (i = 0; i < pctrl->community->npins; i++) {
1774 		const struct pinctrl_pin_desc *desc;
1775 		struct chv_pin_context *ctx;
1776 		void __iomem *reg;
1777 
1778 		desc = &pctrl->community->pins[i];
1779 		if (chv_pad_locked(pctrl, desc->number))
1780 			continue;
1781 
1782 		ctx = &pctrl->saved_pin_context[i];
1783 
1784 		reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL0);
1785 		ctx->padctrl0 = readl(reg) & ~CHV_PADCTRL0_GPIORXSTATE;
1786 
1787 		reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL1);
1788 		ctx->padctrl1 = readl(reg);
1789 	}
1790 
1791 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1792 
1793 	return 0;
1794 }
1795 
chv_pinctrl_resume_noirq(struct device * dev)1796 static int chv_pinctrl_resume_noirq(struct device *dev)
1797 {
1798 	struct platform_device *pdev = to_platform_device(dev);
1799 	struct chv_pinctrl *pctrl = platform_get_drvdata(pdev);
1800 	unsigned long flags;
1801 	int i;
1802 
1803 	raw_spin_lock_irqsave(&chv_lock, flags);
1804 
1805 	/*
1806 	 * Mask all interrupts before restoring per-pin configuration
1807 	 * registers because we don't know in which state BIOS left them
1808 	 * upon exiting suspend.
1809 	 */
1810 	chv_writel(0, pctrl->regs + CHV_INTMASK);
1811 
1812 	for (i = 0; i < pctrl->community->npins; i++) {
1813 		const struct pinctrl_pin_desc *desc;
1814 		const struct chv_pin_context *ctx;
1815 		void __iomem *reg;
1816 		u32 val;
1817 
1818 		desc = &pctrl->community->pins[i];
1819 		if (chv_pad_locked(pctrl, desc->number))
1820 			continue;
1821 
1822 		ctx = &pctrl->saved_pin_context[i];
1823 
1824 		/* Only restore if our saved state differs from the current */
1825 		reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL0);
1826 		val = readl(reg) & ~CHV_PADCTRL0_GPIORXSTATE;
1827 		if (ctx->padctrl0 != val) {
1828 			chv_writel(ctx->padctrl0, reg);
1829 			dev_dbg(pctrl->dev, "restored pin %2u ctrl0 0x%08x\n",
1830 				desc->number, readl(reg));
1831 		}
1832 
1833 		reg = chv_padreg(pctrl, desc->number, CHV_PADCTRL1);
1834 		val = readl(reg);
1835 		if (ctx->padctrl1 != val) {
1836 			chv_writel(ctx->padctrl1, reg);
1837 			dev_dbg(pctrl->dev, "restored pin %2u ctrl1 0x%08x\n",
1838 				desc->number, readl(reg));
1839 		}
1840 	}
1841 
1842 	/*
1843 	 * Now that all pins are restored to known state, we can restore
1844 	 * the interrupt mask register as well.
1845 	 */
1846 	chv_writel(0xffff, pctrl->regs + CHV_INTSTAT);
1847 	chv_writel(pctrl->saved_intmask, pctrl->regs + CHV_INTMASK);
1848 
1849 	raw_spin_unlock_irqrestore(&chv_lock, flags);
1850 
1851 	return 0;
1852 }
1853 #endif
1854 
1855 static const struct dev_pm_ops chv_pinctrl_pm_ops = {
1856 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(chv_pinctrl_suspend_noirq,
1857 				      chv_pinctrl_resume_noirq)
1858 };
1859 
1860 static const struct acpi_device_id chv_pinctrl_acpi_match[] = {
1861 	{ "INT33FF" },
1862 	{ }
1863 };
1864 MODULE_DEVICE_TABLE(acpi, chv_pinctrl_acpi_match);
1865 
1866 static struct platform_driver chv_pinctrl_driver = {
1867 	.probe = chv_pinctrl_probe,
1868 	.remove = chv_pinctrl_remove,
1869 	.driver = {
1870 		.name = "cherryview-pinctrl",
1871 		.pm = &chv_pinctrl_pm_ops,
1872 		.acpi_match_table = chv_pinctrl_acpi_match,
1873 	},
1874 };
1875 
chv_pinctrl_init(void)1876 static int __init chv_pinctrl_init(void)
1877 {
1878 	return platform_driver_register(&chv_pinctrl_driver);
1879 }
1880 subsys_initcall(chv_pinctrl_init);
1881 
chv_pinctrl_exit(void)1882 static void __exit chv_pinctrl_exit(void)
1883 {
1884 	platform_driver_unregister(&chv_pinctrl_driver);
1885 }
1886 module_exit(chv_pinctrl_exit);
1887 
1888 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1889 MODULE_DESCRIPTION("Intel Cherryview/Braswell pinctrl driver");
1890 MODULE_LICENSE("GPL v2");
1891