pinctrl-baytrail.c 48.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Pinctrl GPIO driver for Intel Baytrail
 * Copyright (c) 2012-2013, Intel Corporation.
 *
 * Author: Mathias Nyman <mathias.nyman@linux.intel.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/bitops.h>
#include <linux/interrupt.h>
23
#include <linux/gpio/driver.h>
24 25 26 27 28 29
#include <linux/acpi.h>
#include <linux/platform_device.h>
#include <linux/seq_file.h>
#include <linux/io.h>
#include <linux/pm_runtime.h>
#include <linux/pinctrl/pinctrl.h>
30 31 32
#include <linux/pinctrl/pinmux.h>
#include <linux/pinctrl/pinconf.h>
#include <linux/pinctrl/pinconf-generic.h>
33 34 35 36 37 38 39 40 41

/* memory mapped register offsets */
#define BYT_CONF0_REG		0x000
#define BYT_CONF1_REG		0x004
#define BYT_VAL_REG		0x008
#define BYT_DFT_REG		0x00c
#define BYT_INT_STAT_REG	0x800

/* BYT_CONF0_REG register bits */
42
#define BYT_IODEN		BIT(31)
43
#define BYT_DIRECT_IRQ_EN	BIT(27)
44 45 46
#define BYT_TRIG_NEG		BIT(26)
#define BYT_TRIG_POS		BIT(25)
#define BYT_TRIG_LVL		BIT(24)
47 48 49 50 51 52 53 54 55 56
#define BYT_PULL_STR_SHIFT	9
#define BYT_PULL_STR_MASK	(3 << BYT_PULL_STR_SHIFT)
#define BYT_PULL_STR_2K		(0 << BYT_PULL_STR_SHIFT)
#define BYT_PULL_STR_10K	(1 << BYT_PULL_STR_SHIFT)
#define BYT_PULL_STR_20K	(2 << BYT_PULL_STR_SHIFT)
#define BYT_PULL_STR_40K	(3 << BYT_PULL_STR_SHIFT)
#define BYT_PULL_ASSIGN_SHIFT	7
#define BYT_PULL_ASSIGN_MASK	(3 << BYT_PULL_ASSIGN_SHIFT)
#define BYT_PULL_ASSIGN_UP	(1 << BYT_PULL_ASSIGN_SHIFT)
#define BYT_PULL_ASSIGN_DOWN	(2 << BYT_PULL_ASSIGN_SHIFT)
57 58 59 60 61 62 63 64 65 66
#define BYT_PIN_MUX		0x07

/* BYT_VAL_REG register bits */
#define BYT_INPUT_EN		BIT(2)  /* 0: input enabled (active low)*/
#define BYT_OUTPUT_EN		BIT(1)  /* 0: output enabled (active low)*/
#define BYT_LEVEL		BIT(0)

#define BYT_DIR_MASK		(BIT(1) | BIT(2))
#define BYT_TRIG_MASK		(BIT(26) | BIT(25) | BIT(24))

67 68 69 70
#define BYT_CONF0_RESTORE_MASK	(BYT_DIRECT_IRQ_EN | BYT_TRIG_MASK | \
				 BYT_PIN_MUX)
#define BYT_VAL_RESTORE_MASK	(BYT_DIR_MASK | BYT_LEVEL)

71 72 73 74
#define BYT_NGPIO_SCORE		102
#define BYT_NGPIO_NCORE		28
#define BYT_NGPIO_SUS		44

75 76 77 78
#define BYT_SCORE_ACPI_UID	"1"
#define BYT_NCORE_ACPI_UID	"2"
#define BYT_SUS_ACPI_UID	"3"

79 80 81 82 83 84 85 86
/*
 * This is the function value most pins have for GPIO muxing. If the value
 * differs from the default one, it must be explicitly mentioned. Otherwise, the
 * pin control implementation will set the muxing value to default GPIO if it
 * does not find a match for the requested function.
 */
#define BYT_DEFAULT_GPIO_MUX	0

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
struct byt_gpio_pin_context {
	u32 conf0;
	u32 val;
};

struct byt_simple_func_mux {
	const char *name;
	unsigned short func;
};

struct byt_mixed_func_mux {
	const char *name;
	const unsigned short *func_values;
};

struct byt_pingroup {
	const char *name;
	const unsigned int *pins;
	size_t npins;
	unsigned short has_simple_funcs;
	union {
		const struct byt_simple_func_mux *simple_funcs;
		const struct byt_mixed_func_mux *mixed_funcs;
	};
	size_t nfuncs;
};

struct byt_function {
	const char *name;
	const char * const *groups;
	size_t ngroups;
};

struct byt_community {
	unsigned int pin_base;
	size_t npins;
	const unsigned int *pad_map;
	void __iomem *reg_base;
};

#define SIMPLE_FUNC(n, f)	\
	{			\
		.name	= (n),	\
		.func	= (f),	\
	}
#define MIXED_FUNC(n, f)		\
	{				\
		.name		= (n),	\
		.func_values	= (f),	\
	}

#define PIN_GROUP_SIMPLE(n, p, f)				\
	{							\
		.name			= (n),			\
		.pins			= (p),			\
		.npins			= ARRAY_SIZE((p)),	\
		.has_simple_funcs	= 1,		\
		.simple_funcs		= (f),			\
		.nfuncs			= ARRAY_SIZE((f)),	\
	}
#define PIN_GROUP_MIXED(n, p, f)				\
	{							\
		.name			= (n),			\
		.pins			= (p),			\
		.npins			= ARRAY_SIZE((p)),	\
		.has_simple_funcs	= 0,			\
		.mixed_funcs		= (f),			\
		.nfuncs			= ARRAY_SIZE((f)),	\
	}

#define FUNCTION(n, g)					\
	{						\
		.name		= (n),			\
		.groups		= (g),			\
		.ngroups	= ARRAY_SIZE((g)),	\
	}

#define COMMUNITY(p, n, map)		\
	{				\
		.pin_base	= (p),	\
		.npins		= (n),	\
		.pad_map	= (map),\
	}
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
struct byt_pinctrl_soc_data {
	const char *uid;
	const struct pinctrl_pin_desc *pins;
	size_t npins;
	const struct byt_pingroup *groups;
	size_t ngroups;
	const struct byt_function *functions;
	size_t nfunctions;
	const struct byt_community *communities;
	size_t ncommunities;
};

/* SCORE pins, aka GPIOC_<pin_no> or GPIO_S0_SC[<pin_no>] */
static const struct pinctrl_pin_desc byt_score_pins[] = {
	PINCTRL_PIN(0, "SATA_GP0"),
	PINCTRL_PIN(1, "SATA_GP1"),
	PINCTRL_PIN(2, "SATA_LED#"),
	PINCTRL_PIN(3, "PCIE_CLKREQ0"),
	PINCTRL_PIN(4, "PCIE_CLKREQ1"),
	PINCTRL_PIN(5, "PCIE_CLKREQ2"),
	PINCTRL_PIN(6, "PCIE_CLKREQ3"),
	PINCTRL_PIN(7, "SD3_WP"),
	PINCTRL_PIN(8, "HDA_RST"),
	PINCTRL_PIN(9, "HDA_SYNC"),
	PINCTRL_PIN(10, "HDA_CLK"),
	PINCTRL_PIN(11, "HDA_SDO"),
	PINCTRL_PIN(12, "HDA_SDI0"),
	PINCTRL_PIN(13, "HDA_SDI1"),
	PINCTRL_PIN(14, "GPIO_S0_SC14"),
	PINCTRL_PIN(15, "GPIO_S0_SC15"),
	PINCTRL_PIN(16, "MMC1_CLK"),
	PINCTRL_PIN(17, "MMC1_D0"),
	PINCTRL_PIN(18, "MMC1_D1"),
	PINCTRL_PIN(19, "MMC1_D2"),
	PINCTRL_PIN(20, "MMC1_D3"),
	PINCTRL_PIN(21, "MMC1_D4"),
	PINCTRL_PIN(22, "MMC1_D5"),
	PINCTRL_PIN(23, "MMC1_D6"),
	PINCTRL_PIN(24, "MMC1_D7"),
	PINCTRL_PIN(25, "MMC1_CMD"),
	PINCTRL_PIN(26, "MMC1_RST"),
	PINCTRL_PIN(27, "SD2_CLK"),
	PINCTRL_PIN(28, "SD2_D0"),
	PINCTRL_PIN(29, "SD2_D1"),
	PINCTRL_PIN(30, "SD2_D2"),
	PINCTRL_PIN(31, "SD2_D3_CD"),
	PINCTRL_PIN(32, "SD2_CMD"),
	PINCTRL_PIN(33, "SD3_CLK"),
	PINCTRL_PIN(34, "SD3_D0"),
	PINCTRL_PIN(35, "SD3_D1"),
	PINCTRL_PIN(36, "SD3_D2"),
	PINCTRL_PIN(37, "SD3_D3"),
	PINCTRL_PIN(38, "SD3_CD"),
	PINCTRL_PIN(39, "SD3_CMD"),
	PINCTRL_PIN(40, "SD3_1P8EN"),
	PINCTRL_PIN(41, "SD3_PWREN#"),
	PINCTRL_PIN(42, "ILB_LPC_AD0"),
	PINCTRL_PIN(43, "ILB_LPC_AD1"),
	PINCTRL_PIN(44, "ILB_LPC_AD2"),
	PINCTRL_PIN(45, "ILB_LPC_AD3"),
	PINCTRL_PIN(46, "ILB_LPC_FRAME"),
	PINCTRL_PIN(47, "ILB_LPC_CLK0"),
	PINCTRL_PIN(48, "ILB_LPC_CLK1"),
	PINCTRL_PIN(49, "ILB_LPC_CLKRUN"),
	PINCTRL_PIN(50, "ILB_LPC_SERIRQ"),
	PINCTRL_PIN(51, "PCU_SMB_DATA"),
	PINCTRL_PIN(52, "PCU_SMB_CLK"),
	PINCTRL_PIN(53, "PCU_SMB_ALERT"),
	PINCTRL_PIN(54, "ILB_8254_SPKR"),
	PINCTRL_PIN(55, "GPIO_S0_SC55"),
	PINCTRL_PIN(56, "GPIO_S0_SC56"),
	PINCTRL_PIN(57, "GPIO_S0_SC57"),
	PINCTRL_PIN(58, "GPIO_S0_SC58"),
	PINCTRL_PIN(59, "GPIO_S0_SC59"),
	PINCTRL_PIN(60, "GPIO_S0_SC60"),
	PINCTRL_PIN(61, "GPIO_S0_SC61"),
	PINCTRL_PIN(62, "LPE_I2S2_CLK"),
	PINCTRL_PIN(63, "LPE_I2S2_FRM"),
	PINCTRL_PIN(64, "LPE_I2S2_DATAIN"),
	PINCTRL_PIN(65, "LPE_I2S2_DATAOUT"),
	PINCTRL_PIN(66, "SIO_SPI_CS"),
	PINCTRL_PIN(67, "SIO_SPI_MISO"),
	PINCTRL_PIN(68, "SIO_SPI_MOSI"),
	PINCTRL_PIN(69, "SIO_SPI_CLK"),
	PINCTRL_PIN(70, "SIO_UART1_RXD"),
	PINCTRL_PIN(71, "SIO_UART1_TXD"),
	PINCTRL_PIN(72, "SIO_UART1_RTS"),
	PINCTRL_PIN(73, "SIO_UART1_CTS"),
	PINCTRL_PIN(74, "SIO_UART2_RXD"),
	PINCTRL_PIN(75, "SIO_UART2_TXD"),
	PINCTRL_PIN(76, "SIO_UART2_RTS"),
	PINCTRL_PIN(77, "SIO_UART2_CTS"),
	PINCTRL_PIN(78, "SIO_I2C0_DATA"),
	PINCTRL_PIN(79, "SIO_I2C0_CLK"),
	PINCTRL_PIN(80, "SIO_I2C1_DATA"),
	PINCTRL_PIN(81, "SIO_I2C1_CLK"),
	PINCTRL_PIN(82, "SIO_I2C2_DATA"),
	PINCTRL_PIN(83, "SIO_I2C2_CLK"),
	PINCTRL_PIN(84, "SIO_I2C3_DATA"),
	PINCTRL_PIN(85, "SIO_I2C3_CLK"),
	PINCTRL_PIN(86, "SIO_I2C4_DATA"),
	PINCTRL_PIN(87, "SIO_I2C4_CLK"),
	PINCTRL_PIN(88, "SIO_I2C5_DATA"),
	PINCTRL_PIN(89, "SIO_I2C5_CLK"),
	PINCTRL_PIN(90, "SIO_I2C6_DATA"),
	PINCTRL_PIN(91, "SIO_I2C6_CLK"),
	PINCTRL_PIN(92, "GPIO_S0_SC92"),
	PINCTRL_PIN(93, "GPIO_S0_SC93"),
	PINCTRL_PIN(94, "SIO_PWM0"),
	PINCTRL_PIN(95, "SIO_PWM1"),
	PINCTRL_PIN(96, "PMC_PLT_CLK0"),
	PINCTRL_PIN(97, "PMC_PLT_CLK1"),
	PINCTRL_PIN(98, "PMC_PLT_CLK2"),
	PINCTRL_PIN(99, "PMC_PLT_CLK3"),
	PINCTRL_PIN(100, "PMC_PLT_CLK4"),
	PINCTRL_PIN(101, "PMC_PLT_CLK5"),
};
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

static unsigned const score_pins[BYT_NGPIO_SCORE] = {
	85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
	36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
	54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
	52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
	95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
	86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
	80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
	2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
	31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
	24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
	97, 100,
};

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
static const unsigned int byt_score_pins_map[BYT_NGPIO_SCORE] = {
	85, 89, 93, 96, 99, 102, 98, 101, 34, 37,
	36, 38, 39, 35, 40, 84, 62, 61, 64, 59,
	54, 56, 60, 55, 63, 57, 51, 50, 53, 47,
	52, 49, 48, 43, 46, 41, 45, 42, 58, 44,
	95, 105, 70, 68, 67, 66, 69, 71, 65, 72,
	86, 90, 88, 92, 103, 77, 79, 83, 78, 81,
	80, 82, 13, 12, 15, 14, 17, 18, 19, 16,
	2, 1, 0, 4, 6, 7, 9, 8, 33, 32,
	31, 30, 29, 27, 25, 28, 26, 23, 21, 20,
	24, 22, 5, 3, 10, 11, 106, 87, 91, 104,
	97, 100,
};

/* SCORE groups */
static const unsigned int byt_score_uart1_pins[] = { 70, 71, 72, 73 };
static const unsigned int byt_score_uart2_pins[] = { 74, 75, 76, 77 };
static const struct byt_simple_func_mux byt_score_uart_mux[] = {
	SIMPLE_FUNC("uart", 1),
};

static const unsigned int byt_score_pwm0_pins[] = { 94 };
static const unsigned int byt_score_pwm1_pins[] = { 95 };
static const struct byt_simple_func_mux byt_score_pwm_mux[] = {
	SIMPLE_FUNC("pwm", 1),
};

static const unsigned int byt_score_sio_spi_pins[] = { 66, 67, 68, 69 };
static const struct byt_simple_func_mux byt_score_spi_mux[] = {
	SIMPLE_FUNC("spi", 1),
};

static const unsigned int byt_score_i2c5_pins[] = { 88, 89 };
static const unsigned int byt_score_i2c6_pins[] = { 90, 91 };
static const unsigned int byt_score_i2c4_pins[] = { 86, 87 };
static const unsigned int byt_score_i2c3_pins[] = { 84, 85 };
static const unsigned int byt_score_i2c2_pins[] = { 82, 83 };
static const unsigned int byt_score_i2c1_pins[] = { 80, 81 };
static const unsigned int byt_score_i2c0_pins[] = { 78, 79 };
static const struct byt_simple_func_mux byt_score_i2c_mux[] = {
	SIMPLE_FUNC("i2c", 1),
};

static const unsigned int byt_score_ssp0_pins[] = { 8, 9, 10, 11 };
static const unsigned int byt_score_ssp1_pins[] = { 12, 13, 14, 15 };
static const unsigned int byt_score_ssp2_pins[] = { 62, 63, 64, 65 };
static const struct byt_simple_func_mux byt_score_ssp_mux[] = {
	SIMPLE_FUNC("ssp", 1),
};

static const unsigned int byt_score_sdcard_pins[] = {
	7, 33, 34, 35, 36, 37, 38, 39, 40, 41,
};
static const unsigned short byt_score_sdcard_mux_values[] = {
	2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const struct byt_mixed_func_mux byt_score_sdcard_mux[] = {
	MIXED_FUNC("sdcard", byt_score_sdcard_mux_values),
};

static const unsigned int byt_score_sdio_pins[] = { 27, 28, 29, 30, 31, 32 };
static const struct byt_simple_func_mux byt_score_sdio_mux[] = {
	SIMPLE_FUNC("sdio", 1),
};

static const unsigned int byt_score_emmc_pins[] = {
	16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
};
static const struct byt_simple_func_mux byt_score_emmc_mux[] = {
	SIMPLE_FUNC("emmc", 1),
};

static const unsigned int byt_score_ilb_lpc_pins[] = {
	42, 43, 44, 45, 46, 47, 48, 49, 50,
};
static const struct byt_simple_func_mux byt_score_lpc_mux[] = {
	SIMPLE_FUNC("lpc", 1),
};

static const unsigned int byt_score_sata_pins[] = { 0, 1, 2 };
static const struct byt_simple_func_mux byt_score_sata_mux[] = {
	SIMPLE_FUNC("sata", 1),
};

static const unsigned int byt_score_plt_clk0_pins[] = { 96 };
static const unsigned int byt_score_plt_clk1_pins[] = { 97 };
static const unsigned int byt_score_plt_clk2_pins[] = { 98 };
static const unsigned int byt_score_plt_clk4_pins[] = { 99 };
static const unsigned int byt_score_plt_clk5_pins[] = { 100 };
static const unsigned int byt_score_plt_clk3_pins[] = { 101 };
static const struct byt_simple_func_mux byt_score_plt_clk_mux[] = {
	SIMPLE_FUNC("plt_clk", 1),
395 396
};

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static const unsigned int byt_score_smbus_pins[] = { 51, 52, 53 };
static const struct byt_simple_func_mux byt_score_smbus_mux[] = {
	SIMPLE_FUNC("smbus", 1),
};

static const struct byt_pingroup byt_score_groups[] = {
	PIN_GROUP_SIMPLE("uart1_grp",
			 byt_score_uart1_pins, byt_score_uart_mux),
	PIN_GROUP_SIMPLE("uart2_grp",
			 byt_score_uart2_pins, byt_score_uart_mux),
	PIN_GROUP_SIMPLE("pwm0_grp",
			 byt_score_pwm0_pins, byt_score_pwm_mux),
	PIN_GROUP_SIMPLE("pwm1_grp",
			 byt_score_pwm1_pins, byt_score_pwm_mux),
	PIN_GROUP_SIMPLE("ssp2_grp",
			 byt_score_ssp2_pins, byt_score_pwm_mux),
	PIN_GROUP_SIMPLE("sio_spi_grp",
			 byt_score_sio_spi_pins, byt_score_spi_mux),
	PIN_GROUP_SIMPLE("i2c5_grp",
			 byt_score_i2c5_pins, byt_score_i2c_mux),
	PIN_GROUP_SIMPLE("i2c6_grp",
			 byt_score_i2c6_pins, byt_score_i2c_mux),
	PIN_GROUP_SIMPLE("i2c4_grp",
			 byt_score_i2c4_pins, byt_score_i2c_mux),
	PIN_GROUP_SIMPLE("i2c3_grp",
			 byt_score_i2c3_pins, byt_score_i2c_mux),
	PIN_GROUP_SIMPLE("i2c2_grp",
			 byt_score_i2c2_pins, byt_score_i2c_mux),
	PIN_GROUP_SIMPLE("i2c1_grp",
			 byt_score_i2c1_pins, byt_score_i2c_mux),
	PIN_GROUP_SIMPLE("i2c0_grp",
			 byt_score_i2c0_pins, byt_score_i2c_mux),
	PIN_GROUP_SIMPLE("ssp0_grp",
			 byt_score_ssp0_pins, byt_score_ssp_mux),
	PIN_GROUP_SIMPLE("ssp1_grp",
			 byt_score_ssp1_pins, byt_score_ssp_mux),
	PIN_GROUP_MIXED("sdcard_grp",
			byt_score_sdcard_pins, byt_score_sdcard_mux),
	PIN_GROUP_SIMPLE("sdio_grp",
			 byt_score_sdio_pins, byt_score_sdio_mux),
	PIN_GROUP_SIMPLE("emmc_grp",
			 byt_score_emmc_pins, byt_score_emmc_mux),
	PIN_GROUP_SIMPLE("lpc_grp",
			 byt_score_ilb_lpc_pins, byt_score_lpc_mux),
	PIN_GROUP_SIMPLE("sata_grp",
			 byt_score_sata_pins, byt_score_sata_mux),
	PIN_GROUP_SIMPLE("plt_clk0_grp",
			 byt_score_plt_clk0_pins, byt_score_plt_clk_mux),
	PIN_GROUP_SIMPLE("plt_clk1_grp",
			 byt_score_plt_clk1_pins, byt_score_plt_clk_mux),
	PIN_GROUP_SIMPLE("plt_clk2_grp",
			 byt_score_plt_clk2_pins, byt_score_plt_clk_mux),
	PIN_GROUP_SIMPLE("plt_clk3_grp",
			 byt_score_plt_clk3_pins, byt_score_plt_clk_mux),
	PIN_GROUP_SIMPLE("plt_clk4_grp",
			 byt_score_plt_clk4_pins, byt_score_plt_clk_mux),
	PIN_GROUP_SIMPLE("plt_clk5_grp",
			 byt_score_plt_clk5_pins, byt_score_plt_clk_mux),
	PIN_GROUP_SIMPLE("smbus_grp",
			 byt_score_smbus_pins, byt_score_smbus_mux),
};

static const char * const byt_score_uart_groups[] = {
	"uart1_grp", "uart2_grp",
};
static const char * const byt_score_pwm_groups[] = {
	"pwm0_grp", "pwm1_grp",
};
static const char * const byt_score_ssp_groups[] = {
	"ssp0_grp", "ssp1_grp", "ssp2_grp",
};
static const char * const byt_score_spi_groups[] = { "sio_spi_grp" };
static const char * const byt_score_i2c_groups[] = {
	"i2c0_grp", "i2c1_grp", "i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp",
	"i2c6_grp",
};
static const char * const byt_score_sdcard_groups[] = { "sdcard_grp" };
static const char * const byt_score_sdio_groups[] = { "sdio_grp" };
static const char * const byt_score_emmc_groups[] = { "emmc_grp" };
static const char * const byt_score_lpc_groups[] = { "lpc_grp" };
static const char * const byt_score_sata_groups[] = { "sata_grp" };
static const char * const byt_score_plt_clk_groups[] = {
	"plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
	"plt_clk4_grp", "plt_clk5_grp",
};
static const char * const byt_score_smbus_groups[] = { "smbus_grp" };
static const char * const byt_score_gpio_groups[] = {
	"uart1_grp", "uart2_grp", "pwm0_grp", "pwm1_grp", "ssp0_grp",
	"ssp1_grp", "ssp2_grp", "sio_spi_grp", "i2c0_grp", "i2c1_grp",
	"i2c2_grp", "i2c3_grp", "i2c4_grp", "i2c5_grp", "i2c6_grp",
	"sdcard_grp", "sdio_grp", "emmc_grp", "lpc_grp", "sata_grp",
	"plt_clk0_grp", "plt_clk1_grp", "plt_clk2_grp", "plt_clk3_grp",
	"plt_clk4_grp", "plt_clk5_grp", "smbus_grp",

};

static const struct byt_function byt_score_functions[] = {
	FUNCTION("uart", byt_score_uart_groups),
	FUNCTION("pwm", byt_score_pwm_groups),
	FUNCTION("ssp", byt_score_ssp_groups),
	FUNCTION("spi", byt_score_spi_groups),
	FUNCTION("i2c", byt_score_i2c_groups),
	FUNCTION("sdcard", byt_score_sdcard_groups),
	FUNCTION("sdio", byt_score_sdio_groups),
	FUNCTION("emmc", byt_score_emmc_groups),
	FUNCTION("lpc", byt_score_lpc_groups),
	FUNCTION("sata", byt_score_sata_groups),
	FUNCTION("plt_clk", byt_score_plt_clk_groups),
	FUNCTION("smbus", byt_score_smbus_groups),
	FUNCTION("gpio", byt_score_gpio_groups),
};

static const struct byt_community byt_score_communities[] = {
	COMMUNITY(0, BYT_NGPIO_SCORE, byt_score_pins_map),
};

static const struct byt_pinctrl_soc_data byt_score_soc_data = {
	.uid		= BYT_SCORE_ACPI_UID,
	.pins		= byt_score_pins,
	.npins		= ARRAY_SIZE(byt_score_pins),
	.groups		= byt_score_groups,
	.ngroups	= ARRAY_SIZE(byt_score_groups),
	.functions	= byt_score_functions,
	.nfunctions	= ARRAY_SIZE(byt_score_functions),
	.communities	= byt_score_communities,
	.ncommunities	= ARRAY_SIZE(byt_score_communities),
};

/* SUS pins, aka GPIOS_<pin_no> or GPIO_S5[<pin_no>]  */
static const struct pinctrl_pin_desc byt_sus_pins[] = {
	PINCTRL_PIN(0, "GPIO_S50"),
	PINCTRL_PIN(1, "GPIO_S51"),
	PINCTRL_PIN(2, "GPIO_S52"),
	PINCTRL_PIN(3, "GPIO_S53"),
	PINCTRL_PIN(4, "GPIO_S54"),
	PINCTRL_PIN(5, "GPIO_S55"),
	PINCTRL_PIN(6, "GPIO_S56"),
	PINCTRL_PIN(7, "GPIO_S57"),
	PINCTRL_PIN(8, "GPIO_S58"),
	PINCTRL_PIN(9, "GPIO_S59"),
	PINCTRL_PIN(10, "GPIO_S510"),
	PINCTRL_PIN(11, "PMC_SUSPWRDNACK"),
	PINCTRL_PIN(12, "PMC_SUSCLK0"),
	PINCTRL_PIN(13, "GPIO_S513"),
	PINCTRL_PIN(14, "USB_ULPI_RST"),
	PINCTRL_PIN(15, "PMC_WAKE_PCIE0#"),
	PINCTRL_PIN(16, "PMC_PWRBTN"),
	PINCTRL_PIN(17, "GPIO_S517"),
	PINCTRL_PIN(18, "PMC_SUS_STAT"),
	PINCTRL_PIN(19, "USB_OC0"),
	PINCTRL_PIN(20, "USB_OC1"),
	PINCTRL_PIN(21, "PCU_SPI_CS1"),
	PINCTRL_PIN(22, "GPIO_S522"),
	PINCTRL_PIN(23, "GPIO_S523"),
	PINCTRL_PIN(24, "GPIO_S524"),
	PINCTRL_PIN(25, "GPIO_S525"),
	PINCTRL_PIN(26, "GPIO_S526"),
	PINCTRL_PIN(27, "GPIO_S527"),
	PINCTRL_PIN(28, "GPIO_S528"),
	PINCTRL_PIN(29, "GPIO_S529"),
	PINCTRL_PIN(30, "GPIO_S530"),
	PINCTRL_PIN(31, "USB_ULPI_CLK"),
	PINCTRL_PIN(32, "USB_ULPI_DATA0"),
	PINCTRL_PIN(33, "USB_ULPI_DATA1"),
	PINCTRL_PIN(34, "USB_ULPI_DATA2"),
	PINCTRL_PIN(35, "USB_ULPI_DATA3"),
	PINCTRL_PIN(36, "USB_ULPI_DATA4"),
	PINCTRL_PIN(37, "USB_ULPI_DATA5"),
	PINCTRL_PIN(38, "USB_ULPI_DATA6"),
	PINCTRL_PIN(39, "USB_ULPI_DATA7"),
	PINCTRL_PIN(40, "USB_ULPI_DIR"),
	PINCTRL_PIN(41, "USB_ULPI_NXT"),
	PINCTRL_PIN(42, "USB_ULPI_STP"),
	PINCTRL_PIN(43, "USB_ULPI_REFCLK"),
};

static const unsigned int sus_pins[BYT_NGPIO_SUS] = {
	29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
	18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
	0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
	26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
	52, 53, 59, 40,
};

static const unsigned int byt_sus_pins_map[BYT_NGPIO_SUS] = {
582 583 584 585 586 587 588
	29, 33, 30, 31, 32, 34, 36, 35, 38, 37,
	18, 7, 11, 20, 17, 1, 8, 10, 19, 12,
	0, 2, 23, 39, 28, 27, 22, 21, 24, 25,
	26, 51, 56, 54, 49, 55, 48, 57, 50, 58,
	52, 53, 59, 40,
};

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
static const unsigned int byt_sus_usb_over_current_pins[] = { 19, 20 };
static const struct byt_simple_func_mux byt_sus_usb_oc_mux[] = {
	SIMPLE_FUNC("usb", 0),
	SIMPLE_FUNC("gpio", 1),
};

static const unsigned int byt_sus_usb_ulpi_pins[] = {
	14, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
};
static const unsigned short byt_sus_usb_ulpi_mode_values[] = {
	2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
static const unsigned short byt_sus_usb_ulpi_gpio_mode_values[] = {
	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static const struct byt_mixed_func_mux byt_sus_usb_ulpi_mux[] = {
	MIXED_FUNC("usb", byt_sus_usb_ulpi_mode_values),
	MIXED_FUNC("gpio", byt_sus_usb_ulpi_gpio_mode_values),
};

static const unsigned int byt_sus_pcu_spi_pins[] = { 21 };
static const struct byt_simple_func_mux byt_sus_pcu_spi_mux[] = {
	SIMPLE_FUNC("spi", 0),
	SIMPLE_FUNC("gpio", 1),
};

static const struct byt_pingroup byt_sus_groups[] = {
	PIN_GROUP_SIMPLE("usb_oc_grp",
			byt_sus_usb_over_current_pins, byt_sus_usb_oc_mux),
	PIN_GROUP_MIXED("usb_ulpi_grp",
			byt_sus_usb_ulpi_pins, byt_sus_usb_ulpi_mux),
	PIN_GROUP_SIMPLE("pcu_spi_grp",
			byt_sus_pcu_spi_pins, byt_sus_pcu_spi_mux),
};

static const char * const byt_sus_usb_groups[] = {
	"usb_oc_grp", "usb_ulpi_grp",
};
static const char * const byt_sus_spi_groups[] = { "pcu_spi_grp" };
static const char * const byt_sus_gpio_groups[] = {
	"usb_oc_grp", "usb_ulpi_grp", "pcu_spi_grp",
};

static const struct byt_function byt_sus_functions[] = {
	FUNCTION("usb", byt_sus_usb_groups),
	FUNCTION("spi", byt_sus_spi_groups),
	FUNCTION("gpio", byt_sus_gpio_groups),
};

static const struct byt_community byt_sus_communities[] = {
	COMMUNITY(0, BYT_NGPIO_SUS, byt_sus_pins_map),
};

static const struct byt_pinctrl_soc_data byt_sus_soc_data = {
	.uid		= BYT_SUS_ACPI_UID,
	.pins		= byt_sus_pins,
	.npins		= ARRAY_SIZE(byt_sus_pins),
	.groups		= byt_sus_groups,
	.ngroups	= ARRAY_SIZE(byt_sus_groups),
	.functions	= byt_sus_functions,
	.nfunctions	= ARRAY_SIZE(byt_sus_functions),
	.communities	= byt_sus_communities,
	.ncommunities	= ARRAY_SIZE(byt_sus_communities),
};

static const struct pinctrl_pin_desc byt_ncore_pins[] = {
	PINCTRL_PIN(0, "GPIO_NCORE0"),
	PINCTRL_PIN(1, "GPIO_NCORE1"),
	PINCTRL_PIN(2, "GPIO_NCORE2"),
	PINCTRL_PIN(3, "GPIO_NCORE3"),
	PINCTRL_PIN(4, "GPIO_NCORE4"),
	PINCTRL_PIN(5, "GPIO_NCORE5"),
	PINCTRL_PIN(6, "GPIO_NCORE6"),
	PINCTRL_PIN(7, "GPIO_NCORE7"),
	PINCTRL_PIN(8, "GPIO_NCORE8"),
	PINCTRL_PIN(9, "GPIO_NCORE9"),
	PINCTRL_PIN(10, "GPIO_NCORE10"),
	PINCTRL_PIN(11, "GPIO_NCORE11"),
	PINCTRL_PIN(12, "GPIO_NCORE12"),
	PINCTRL_PIN(13, "GPIO_NCORE13"),
	PINCTRL_PIN(14, "GPIO_NCORE14"),
	PINCTRL_PIN(15, "GPIO_NCORE15"),
	PINCTRL_PIN(16, "GPIO_NCORE16"),
	PINCTRL_PIN(17, "GPIO_NCORE17"),
	PINCTRL_PIN(18, "GPIO_NCORE18"),
	PINCTRL_PIN(19, "GPIO_NCORE19"),
	PINCTRL_PIN(20, "GPIO_NCORE20"),
	PINCTRL_PIN(21, "GPIO_NCORE21"),
	PINCTRL_PIN(22, "GPIO_NCORE22"),
	PINCTRL_PIN(23, "GPIO_NCORE23"),
	PINCTRL_PIN(24, "GPIO_NCORE24"),
	PINCTRL_PIN(25, "GPIO_NCORE25"),
	PINCTRL_PIN(26, "GPIO_NCORE26"),
	PINCTRL_PIN(27, "GPIO_NCORE27"),
};

static unsigned const byt_ncore_pins_map[BYT_NGPIO_NCORE] = {
	19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
	14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
	3, 6, 10, 13, 2, 5, 9, 7,
};

static const struct byt_community byt_ncore_communities[] = {
	COMMUNITY(0, BYT_NGPIO_NCORE, byt_ncore_pins_map),
};

static const struct byt_pinctrl_soc_data byt_ncore_soc_data = {
	.uid		= BYT_NCORE_ACPI_UID,
	.pins		= byt_ncore_pins,
	.npins		= ARRAY_SIZE(byt_ncore_pins),
	.communities	= byt_ncore_communities,
	.ncommunities	= ARRAY_SIZE(byt_ncore_communities),
};

static unsigned const ncore_pins[BYT_NGPIO_NCORE] = {
	19, 18, 17, 20, 21, 22, 24, 25, 23, 16,
	14, 15, 12, 26, 27, 1, 4, 8, 11, 0,
	3, 6, 10, 13, 2, 5, 9, 7,
};

709 710
static struct pinctrl_gpio_range byt_ranges[] = {
	{
711
		.name = BYT_SCORE_ACPI_UID, /* match with acpi _UID in probe */
712 713 714 715
		.npins = BYT_NGPIO_SCORE,
		.pins = score_pins,
	},
	{
716
		.name = BYT_NCORE_ACPI_UID,
717 718 719 720
		.npins = BYT_NGPIO_NCORE,
		.pins = ncore_pins,
	},
	{
721
		.name = BYT_SUS_ACPI_UID,
722 723 724 725 726 727 728 729 730 731
		.npins = BYT_NGPIO_SUS,
		.pins = sus_pins,
	},
	{
	},
};

struct byt_gpio {
	struct gpio_chip		chip;
	struct platform_device		*pdev;
732
	raw_spinlock_t			lock;
733 734
	void __iomem			*reg_base;
	struct pinctrl_gpio_range	*range;
735
	struct byt_gpio_pin_context	*saved_context;
736 737
	const struct byt_pinctrl_soc_data *soc_data;
	struct byt_community		*communities_copy;
738 739
};

740 741 742 743 744 745 746
static const struct byt_pinctrl_soc_data *byt_soc_data[] = {
	&byt_score_soc_data,
	&byt_sus_soc_data,
	&byt_ncore_soc_data,
	NULL,
};

747 748
static struct byt_community *byt_get_community(struct byt_gpio *vg,
					       unsigned int pin)
749
{
750 751 752 753 754 755 756 757
	struct byt_community *comm;
	int i;

	for (i = 0; i < vg->soc_data->ncommunities; i++) {
		comm = vg->communities_copy + i;
		if (pin < comm->pin_base + comm->npins && pin >= comm->pin_base)
			return comm;
	}
758

759 760 761 762 763 764 765 766 767 768 769 770 771
	return NULL;
}

static void __iomem *byt_gpio_reg(struct byt_gpio *vg, unsigned int offset,
				  int reg)
{
	struct byt_community *comm = byt_get_community(vg, offset);
	u32 reg_offset = 0;

	if (!comm)
		return NULL;

	offset -= comm->pin_base;
772 773 774
	if (reg == BYT_INT_STAT_REG)
		reg_offset = (offset / 32) * 4;
	else
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
		reg_offset = comm->pad_map[offset] * 16;

	return comm->reg_base + reg_offset + reg;
}

static int byt_get_groups_count(struct pinctrl_dev *pctldev)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);

	return vg->soc_data->ngroups;
}

static const char *byt_get_group_name(struct pinctrl_dev *pctldev,
				      unsigned int selector)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);

	return vg->soc_data->groups[selector].name;
}

static int byt_get_group_pins(struct pinctrl_dev *pctldev,
			      unsigned int selector,
			      const unsigned int **pins,
			      unsigned int *num_pins)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);

	*pins		= vg->soc_data->groups[selector].pins;
	*num_pins	= vg->soc_data->groups[selector].npins;

	return 0;
}

static const struct pinctrl_ops byt_pinctrl_ops = {
	.get_groups_count	= byt_get_groups_count,
	.get_group_name		= byt_get_group_name,
	.get_group_pins		= byt_get_group_pins,
};

static int byt_get_functions_count(struct pinctrl_dev *pctldev)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);

	return vg->soc_data->nfunctions;
}

static const char *byt_get_function_name(struct pinctrl_dev *pctldev,
					 unsigned int selector)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);

	return vg->soc_data->functions[selector].name;
}

static int byt_get_function_groups(struct pinctrl_dev *pctldev,
				   unsigned int selector,
				   const char * const **groups,
				   unsigned int *num_groups)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);

	*groups		= vg->soc_data->functions[selector].groups;
	*num_groups	= vg->soc_data->functions[selector].ngroups;

	return 0;
}

static int byt_get_group_simple_mux(const struct byt_pingroup group,
				    const char *func_name,
				    unsigned short *func)
{
	int i;

	for (i = 0; i < group.nfuncs; i++) {
		if (!strcmp(group.simple_funcs[i].name, func_name)) {
			*func = group.simple_funcs[i].func;
			return 0;
		}
	}

	return 1;
}

static int byt_get_group_mixed_mux(const struct byt_pingroup group,
				   const char *func_name,
				   const unsigned short **func)
{
	int i;

	for (i = 0; i < group.nfuncs; i++) {
		if (!strcmp(group.mixed_funcs[i].name, func_name)) {
			*func = group.mixed_funcs[i].func_values;
			return 0;
		}
	}
870

871
	return 1;
872 873
}

874 875 876
static void byt_set_group_simple_mux(struct byt_gpio *vg,
				     const struct byt_pingroup group,
				     unsigned short func)
877 878
{
	unsigned long flags;
879
	int i;
880

881
	raw_spin_lock_irqsave(&vg->lock, flags);
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

	for (i = 0; i < group.npins; i++) {
		void __iomem *padcfg0;
		u32 value;

		padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
		if (!padcfg0) {
			dev_warn(&vg->pdev->dev,
				 "Group %s, pin %i not muxed (no padcfg0)\n",
				 group.name, i);
			continue;
		}

		value = readl(padcfg0);
		value &= ~BYT_PIN_MUX;
		value |= func;
		writel(value, padcfg0);
	}

901
	raw_spin_unlock_irqrestore(&vg->lock, flags);
902 903
}

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
static void byt_set_group_mixed_mux(struct byt_gpio *vg,
				    const struct byt_pingroup group,
				    const unsigned short *func)
{
	unsigned long flags;
	int i;

	raw_spin_lock_irqsave(&vg->lock, flags);

	for (i = 0; i < group.npins; i++) {
		void __iomem *padcfg0;
		u32 value;

		padcfg0 = byt_gpio_reg(vg, group.pins[i], BYT_CONF0_REG);
		if (!padcfg0) {
			dev_warn(&vg->pdev->dev,
				 "Group %s, pin %i not muxed (no padcfg0)\n",
				 group.name, i);
			continue;
		}

		value = readl(padcfg0);
		value &= ~BYT_PIN_MUX;
		value |= func[i];
		writel(value, padcfg0);
	}

	raw_spin_unlock_irqrestore(&vg->lock, flags);
}

static int byt_set_mux(struct pinctrl_dev *pctldev, unsigned int func_selector,
		       unsigned int group_selector)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctldev);
	const struct byt_function func = vg->soc_data->functions[func_selector];
	const struct byt_pingroup group = vg->soc_data->groups[group_selector];
	const unsigned short *mixed_func;
	unsigned short simple_func;
	int ret = 1;

	if (group.has_simple_funcs)
		ret = byt_get_group_simple_mux(group, func.name, &simple_func);
	else
		ret = byt_get_group_mixed_mux(group, func.name, &mixed_func);

	if (ret)
		byt_set_group_simple_mux(vg, group, BYT_DEFAULT_GPIO_MUX);
	else if (group.has_simple_funcs)
		byt_set_group_simple_mux(vg, group, simple_func);
	else
		byt_set_group_mixed_mux(vg, group, mixed_func);

	return 0;
}

959
static u32 byt_get_gpio_mux(struct byt_gpio *vg, unsigned offset)
960 961
{
	/* SCORE pin 92-93 */
962 963
	if (!strcmp(vg->soc_data->uid, BYT_SCORE_ACPI_UID) &&
	    offset >= 92 && offset <= 93)
964
		return 1;
965 966

	/* SUS pin 11-21 */
967 968
	if (!strcmp(vg->soc_data->uid, BYT_SUS_ACPI_UID) &&
	    offset >= 11 && offset <= 21)
969
		return 1;
970

971
	return 0;
972 973
}

974
static void byt_gpio_clear_triggering(struct byt_gpio *vg, unsigned int offset)
975
{
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
	unsigned long flags;
	u32 value;

	raw_spin_lock_irqsave(&vg->lock, flags);
	value = readl(reg);
	value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
	writel(value, reg);
	raw_spin_unlock_irqrestore(&vg->lock, flags);
}

static int byt_gpio_request_enable(struct pinctrl_dev *pctl_dev,
				   struct pinctrl_gpio_range *range,
				   unsigned int offset)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
	void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
993
	u32 value, gpio_mux;
994 995
	unsigned long flags;

996
	raw_spin_lock_irqsave(&vg->lock, flags);
997 998 999 1000

	/*
	 * In most cases, func pin mux 000 means GPIO function.
	 * But, some pins may have func pin mux 001 represents
1001 1002 1003 1004 1005
	 * GPIO function.
	 *
	 * Because there are devices out there where some pins were not
	 * configured correctly we allow changing the mux value from
	 * request (but print out warning about that).
1006 1007
	 */
	value = readl(reg) & BYT_PIN_MUX;
1008 1009 1010 1011 1012 1013 1014 1015
	gpio_mux = byt_get_gpio_mux(vg, offset);
	if (WARN_ON(gpio_mux != value)) {
		value = readl(reg) & ~BYT_PIN_MUX;
		value |= gpio_mux;
		writel(value, reg);

		dev_warn(&vg->pdev->dev,
			 "pin %u forcibly re-configured as GPIO\n", offset);
1016
	}
1017

1018
	raw_spin_unlock_irqrestore(&vg->lock, flags);
1019

1020 1021 1022 1023 1024 1025 1026
	pm_runtime_get(&vg->pdev->dev);

	return 0;
}

static void byt_gpio_free(struct gpio_chip *chip, unsigned offset)
{
1027
	struct byt_gpio *vg = gpiochip_get_data(chip);
1028

1029
	byt_gpio_clear_triggering(vg, offset);
1030 1031 1032
	pm_runtime_put(&vg->pdev->dev);
}

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
static void byt_gpio_disable_free(struct pinctrl_dev *pctl_dev,
				  struct pinctrl_gpio_range *range,
				  unsigned int offset)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);

	byt_gpio_clear_triggering(vg, offset);
	pm_runtime_put(&vg->pdev->dev);
}

static int byt_gpio_set_direction(struct pinctrl_dev *pctl_dev,
				  struct pinctrl_gpio_range *range,
				  unsigned int offset,
				  bool input)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
	void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
	void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
	unsigned long flags;
	u32 value;

	raw_spin_lock_irqsave(&vg->lock, flags);

	value = readl(val_reg);
	value &= ~BYT_DIR_MASK;
	if (input)
		value |= BYT_OUTPUT_EN;
	else
		/*
		 * Before making any direction modifications, do a check if gpio
		 * is set for direct IRQ.  On baytrail, setting GPIO to output
		 * does not make sense, so let's at least warn the caller before
		 * they shoot themselves in the foot.
		 */
		WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
		     "Potential Error: Setting GPIO with direct_irq_en to output");
	writel(value, val_reg);

	raw_spin_unlock_irqrestore(&vg->lock, flags);

	return 0;
}

static const struct pinmux_ops byt_pinmux_ops = {
	.get_functions_count	= byt_get_functions_count,
	.get_function_name	= byt_get_function_name,
	.get_function_groups	= byt_get_function_groups,
	.set_mux		= byt_set_mux,
	.gpio_request_enable	= byt_gpio_request_enable,
	.gpio_disable_free	= byt_gpio_disable_free,
	.gpio_set_direction	= byt_gpio_set_direction,
};

static int byt_gpio_request(struct gpio_chip *chip, unsigned int offset)
{
	struct byt_gpio *vg = gpiochip_get_data(chip);
	void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
	u32 value, gpio_mux;
	unsigned long flags;

	raw_spin_lock_irqsave(&vg->lock, flags);

	/*
	 * In most cases, func pin mux 000 means GPIO function.
	 * But, some pins may have func pin mux 001 represents
	 * GPIO function.
	 *
	 * Because there are devices out there where some pins were not
	 * configured correctly we allow changing the mux value from
	 * request (but print out warning about that).
	 */
	value = readl(reg) & BYT_PIN_MUX;
	gpio_mux = byt_get_gpio_mux(vg, offset);
	if (WARN_ON(gpio_mux != value)) {
		value = readl(reg) & ~BYT_PIN_MUX;
		value |= gpio_mux;
		writel(value, reg);

		dev_warn(&vg->pdev->dev,
			 "pin %u forcibly re-configured as GPIO\n", offset);
	}

	raw_spin_unlock_irqrestore(&vg->lock, flags);

	pm_runtime_get(&vg->pdev->dev);

	return 0;
}

1122 1123
static int byt_irq_type(struct irq_data *d, unsigned type)
{
1124
	struct byt_gpio *vg = gpiochip_get_data(irq_data_get_irq_chip_data(d));
1125 1126 1127
	u32 offset = irqd_to_hwirq(d);
	u32 value;
	unsigned long flags;
1128
	void __iomem *reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1129 1130 1131 1132

	if (offset >= vg->chip.ngpio)
		return -EINVAL;

1133
	raw_spin_lock_irqsave(&vg->lock, flags);
1134 1135
	value = readl(reg);

1136
	WARN(value & BYT_DIRECT_IRQ_EN,
1137
	     "Bad pad config for io mode, force direct_irq_en bit clearing");
1138

1139 1140 1141
	/* For level trigges the BYT_TRIG_POS and BYT_TRIG_NEG bits
	 * are used to indicate high and low level triggering
	 */
1142 1143
	value &= ~(BYT_DIRECT_IRQ_EN | BYT_TRIG_POS | BYT_TRIG_NEG |
		   BYT_TRIG_LVL);
1144 1145 1146

	writel(value, reg);

1147
	if (type & IRQ_TYPE_EDGE_BOTH)
1148
		irq_set_handler_locked(d, handle_edge_irq);
1149
	else if (type & IRQ_TYPE_LEVEL_MASK)
1150
		irq_set_handler_locked(d, handle_level_irq);
1151

1152
	raw_spin_unlock_irqrestore(&vg->lock, flags);
1153 1154 1155 1156

	return 0;
}

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
static void byt_get_pull_strength(u32 reg, u16 *strength)
{
	switch (reg & BYT_PULL_STR_MASK) {
	case BYT_PULL_STR_2K:
		*strength = 2000;
		break;
	case BYT_PULL_STR_10K:
		*strength = 10000;
		break;
	case BYT_PULL_STR_20K:
		*strength = 20000;
		break;
	case BYT_PULL_STR_40K:
		*strength = 40000;
		break;
	}
}

static int byt_set_pull_strength(u32 *reg, u16 strength)
{
	*reg &= ~BYT_PULL_STR_MASK;

	switch (strength) {
	case 2000:
		*reg |= BYT_PULL_STR_2K;
		break;
	case 10000:
		*reg |= BYT_PULL_STR_10K;
		break;
	case 20000:
		*reg |= BYT_PULL_STR_20K;
		break;
	case 40000:
		*reg |= BYT_PULL_STR_40K;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int byt_pin_config_get(struct pinctrl_dev *pctl_dev, unsigned int offset,
			      unsigned long *config)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
	enum pin_config_param param = pinconf_to_config_param(*config);
	void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
	void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
	unsigned long flags;
	u32 conf, pull, val;
	u16 arg = 0;

	raw_spin_lock_irqsave(&vg->lock, flags);
	conf = readl(conf_reg);
	pull = conf & BYT_PULL_ASSIGN_MASK;
	val = readl(val_reg);
	raw_spin_unlock_irqrestore(&vg->lock, flags);

	switch (param) {
	case PIN_CONFIG_BIAS_DISABLE:
		if (pull)
			return -EINVAL;
		break;
	case PIN_CONFIG_BIAS_PULL_DOWN:
		/* Pull assignment is only applicable in input mode */
		if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_DOWN)
			return -EINVAL;

		byt_get_pull_strength(conf, &arg);

		break;
	case PIN_CONFIG_BIAS_PULL_UP:
		/* Pull assignment is only applicable in input mode */
		if ((val & BYT_INPUT_EN) || pull != BYT_PULL_ASSIGN_UP)
			return -EINVAL;

		byt_get_pull_strength(conf, &arg);

		break;
	default:
		return -ENOTSUPP;
	}

	*config = pinconf_to_config_packed(param, arg);

	return 0;
}

static int byt_pin_config_set(struct pinctrl_dev *pctl_dev,
			      unsigned int offset,
			      unsigned long *configs,
			      unsigned int num_configs)
{
	struct byt_gpio *vg = pinctrl_dev_get_drvdata(pctl_dev);
	unsigned int param, arg;
	void __iomem *conf_reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
	void __iomem *val_reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
	unsigned long flags;
	u32 conf, val;
	int i, ret = 0;

	raw_spin_lock_irqsave(&vg->lock, flags);

	conf = readl(conf_reg);
	val = readl(val_reg);

	for (i = 0; i < num_configs; i++) {
		param = pinconf_to_config_param(configs[i]);
		arg = pinconf_to_config_argument(configs[i]);

		switch (param) {
		case PIN_CONFIG_BIAS_DISABLE:
			conf &= ~BYT_PULL_ASSIGN_MASK;
			break;
		case PIN_CONFIG_BIAS_PULL_DOWN:
			/* Set default strength value in case none is given */
			if (arg == 1)
				arg = 2000;

			/*
			 * Pull assignment is only applicable in input mode. If
			 * chip is not in input mode, set it and warn about it.
			 */
			if (val & BYT_INPUT_EN) {
				val &= ~BYT_INPUT_EN;
				writel(val, val_reg);
				dev_warn(&vg->pdev->dev,
					 "pin %u forcibly set to input mode\n",
					 offset);
			}

			conf &= ~BYT_PULL_ASSIGN_MASK;
			conf |= BYT_PULL_ASSIGN_DOWN;
			ret = byt_set_pull_strength(&conf, arg);

			break;
		case PIN_CONFIG_BIAS_PULL_UP:
			/* Set default strength value in case none is given */
			if (arg == 1)
				arg = 2000;

			/*
			 * Pull assignment is only applicable in input mode. If
			 * chip is not in input mode, set it and warn about it.
			 */
			if (val & BYT_INPUT_EN) {
				val &= ~BYT_INPUT_EN;
				writel(val, val_reg);
				dev_warn(&vg->pdev->dev,
					 "pin %u forcibly set to input mode\n",
					 offset);
			}

			conf &= ~BYT_PULL_ASSIGN_MASK;
			conf |= BYT_PULL_ASSIGN_UP;
			ret = byt_set_pull_strength(&conf, arg);

			break;
		default:
			ret = -ENOTSUPP;
		}

		if (ret)
			break;
	}

	if (!ret)
		writel(conf, conf_reg);

	raw_spin_unlock_irqrestore(&vg->lock, flags);

	return ret;
}

static const struct pinconf_ops byt_pinconf_ops = {
	.is_generic	= true,
	.pin_config_get	= byt_pin_config_get,
	.pin_config_set	= byt_pin_config_set,
};

static const struct pinctrl_desc byt_pinctrl_desc = {
	.pctlops	= &byt_pinctrl_ops,
	.pmxops		= &byt_pinmux_ops,
	.confops	= &byt_pinconf_ops,
	.owner		= THIS_MODULE,
};

1345 1346
static int byt_gpio_get(struct gpio_chip *chip, unsigned offset)
{
1347
	struct byt_gpio *vg = gpiochip_get_data(chip);
1348
	void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1349 1350 1351
	unsigned long flags;
	u32 val;

1352
	raw_spin_lock_irqsave(&vg->lock, flags);
1353
	val = readl(reg);
1354
	raw_spin_unlock_irqrestore(&vg->lock, flags);
1355

1356
	return !!(val & BYT_LEVEL);
1357 1358 1359 1360
}

static void byt_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
1361
	struct byt_gpio *vg = gpiochip_get_data(chip);
1362
	void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1363 1364 1365
	unsigned long flags;
	u32 old_val;

1366
	raw_spin_lock_irqsave(&vg->lock, flags);
1367 1368 1369 1370 1371 1372 1373 1374

	old_val = readl(reg);

	if (value)
		writel(old_val | BYT_LEVEL, reg);
	else
		writel(old_val & ~BYT_LEVEL, reg);

1375
	raw_spin_unlock_irqrestore(&vg->lock, flags);
1376 1377 1378 1379
}

static int byt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
{
1380
	struct byt_gpio *vg = gpiochip_get_data(chip);
1381
	void __iomem *reg = byt_gpio_reg(vg, offset, BYT_VAL_REG);
1382 1383 1384
	unsigned long flags;
	u32 value;

1385
	raw_spin_lock_irqsave(&vg->lock, flags);
1386 1387

	value = readl(reg) | BYT_DIR_MASK;
1388
	value &= ~BYT_INPUT_EN;		/* active low */
1389 1390
	writel(value, reg);

1391
	raw_spin_unlock_irqrestore(&vg->lock, flags);
1392 1393 1394 1395 1396 1397 1398

	return 0;
}

static int byt_gpio_direction_output(struct gpio_chip *chip,
				     unsigned gpio, int value)
{
1399
	struct byt_gpio *vg = gpiochip_get_data(chip);
1400 1401
	void __iomem *conf_reg = byt_gpio_reg(vg, gpio, BYT_CONF0_REG);
	void __iomem *reg = byt_gpio_reg(vg, gpio, BYT_VAL_REG);
1402 1403 1404
	unsigned long flags;
	u32 reg_val;

1405
	raw_spin_lock_irqsave(&vg->lock, flags);
1406

1407 1408 1409 1410 1411 1412 1413 1414 1415
	/*
	 * Before making any direction modifications, do a check if gpio
	 * is set for direct IRQ.  On baytrail, setting GPIO to output does
	 * not make sense, so let's at least warn the caller before they shoot
	 * themselves in the foot.
	 */
	WARN(readl(conf_reg) & BYT_DIRECT_IRQ_EN,
		"Potential Error: Setting GPIO with direct_irq_en to output");

1416
	reg_val = readl(reg) | BYT_DIR_MASK;
1417
	reg_val &= ~(BYT_OUTPUT_EN | BYT_INPUT_EN);
1418 1419 1420 1421 1422

	if (value)
		writel(reg_val | BYT_LEVEL, reg);
	else
		writel(reg_val & ~BYT_LEVEL, reg);
1423

1424
	raw_spin_unlock_irqrestore(&vg->lock, flags);
1425 1426 1427 1428 1429 1430

	return 0;
}

static void byt_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
{
1431
	struct byt_gpio *vg = gpiochip_get_data(chip);
1432 1433 1434 1435
	int i;
	u32 conf0, val, offs;

	for (i = 0; i < vg->chip.ngpio; i++) {
1436 1437
		const char *pull_str = NULL;
		const char *pull = NULL;
1438
		unsigned long flags;
1439
		const char *label;
1440
		offs = vg->range->pins[i] * 16;
1441 1442

		raw_spin_lock_irqsave(&vg->lock, flags);
1443 1444
		conf0 = readl(vg->reg_base + offs + BYT_CONF0_REG);
		val = readl(vg->reg_base + offs + BYT_VAL_REG);
1445
		raw_spin_unlock_irqrestore(&vg->lock, flags);
1446

1447 1448 1449 1450
		label = gpiochip_is_requested(chip, i);
		if (!label)
			label = "Unrequested";

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
		switch (conf0 & BYT_PULL_ASSIGN_MASK) {
		case BYT_PULL_ASSIGN_UP:
			pull = "up";
			break;
		case BYT_PULL_ASSIGN_DOWN:
			pull = "down";
			break;
		}

		switch (conf0 & BYT_PULL_STR_MASK) {
		case BYT_PULL_STR_2K:
			pull_str = "2k";
			break;
		case BYT_PULL_STR_10K:
			pull_str = "10k";
			break;
		case BYT_PULL_STR_20K:
			pull_str = "20k";
			break;
		case BYT_PULL_STR_40K:
			pull_str = "40k";
			break;
		}

1475
		seq_printf(s,
1476
			   " gpio-%-3d (%-20.20s) %s %s %s pad-%-3d offset:0x%03x mux:%d %s%s%s",
1477
			   i,
1478
			   label,
1479 1480 1481 1482 1483
			   val & BYT_INPUT_EN ? "  " : "in",
			   val & BYT_OUTPUT_EN ? "   " : "out",
			   val & BYT_LEVEL ? "hi" : "lo",
			   vg->range->pins[i], offs,
			   conf0 & 0x7,
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
			   conf0 & BYT_TRIG_NEG ? " fall" : "     ",
			   conf0 & BYT_TRIG_POS ? " rise" : "     ",
			   conf0 & BYT_TRIG_LVL ? " level" : "      ");

		if (pull && pull_str)
			seq_printf(s, " %-4s %-3s", pull, pull_str);
		else
			seq_puts(s, "          ");

		if (conf0 & BYT_IODEN)
			seq_puts(s, " open-drain");

		seq_puts(s, "\n");
1497 1498 1499
	}
}

1500
static void byt_gpio_irq_handler(struct irq_desc *desc)
1501 1502
{
	struct irq_data *data = irq_desc_get_irq_data(desc);
1503
	struct byt_gpio *vg = gpiochip_get_data(irq_desc_get_handler_data(desc));
1504
	struct irq_chip *chip = irq_data_get_irq_chip(data);
1505
	u32 base, pin;
1506
	void __iomem *reg;
1507
	unsigned long pending;
1508 1509 1510 1511
	unsigned virq;

	/* check from GPIO controller which pin triggered the interrupt */
	for (base = 0; base < vg->chip.ngpio; base += 32) {
1512
		reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
1513 1514
		pending = readl(reg);
		for_each_set_bit(pin, &pending, 32) {
1515
			virq = irq_find_mapping(vg->chip.irqdomain, base + pin);
1516 1517 1518 1519 1520 1521
			generic_handle_irq(virq);
		}
	}
	chip->irq_eoi(data);
}

1522 1523 1524
static void byt_irq_ack(struct irq_data *d)
{
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1525
	struct byt_gpio *vg = gpiochip_get_data(gc);
1526 1527 1528
	unsigned offset = irqd_to_hwirq(d);
	void __iomem *reg;

1529
	raw_spin_lock(&vg->lock);
1530
	reg = byt_gpio_reg(vg, offset, BYT_INT_STAT_REG);
1531
	writel(BIT(offset % 32), reg);
1532
	raw_spin_unlock(&vg->lock);
1533 1534
}

1535 1536
static void byt_irq_unmask(struct irq_data *d)
{
1537
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1538
	struct byt_gpio *vg = gpiochip_get_data(gc);
1539 1540 1541 1542 1543
	unsigned offset = irqd_to_hwirq(d);
	unsigned long flags;
	void __iomem *reg;
	u32 value;

1544
	reg = byt_gpio_reg(vg, offset, BYT_CONF0_REG);
1545 1546

	raw_spin_lock_irqsave(&vg->lock, flags);
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	value = readl(reg);

	switch (irqd_get_trigger_type(d)) {
	case IRQ_TYPE_LEVEL_HIGH:
		value |= BYT_TRIG_LVL;
	case IRQ_TYPE_EDGE_RISING:
		value |= BYT_TRIG_POS;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		value |= BYT_TRIG_LVL;
	case IRQ_TYPE_EDGE_FALLING:
		value |= BYT_TRIG_NEG;
		break;
	case IRQ_TYPE_EDGE_BOTH:
		value |= (BYT_TRIG_NEG | BYT_TRIG_POS);
		break;
	}

	writel(value, reg);

1567
	raw_spin_unlock_irqrestore(&vg->lock, flags);
1568 1569 1570 1571
}

static void byt_irq_mask(struct irq_data *d)
{
1572
	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1573
	struct byt_gpio *vg = gpiochip_get_data(gc);
1574 1575

	byt_gpio_clear_triggering(vg, irqd_to_hwirq(d));
1576 1577 1578 1579
}

static struct irq_chip byt_irqchip = {
	.name = "BYT-GPIO",
1580
	.irq_ack = byt_irq_ack,
1581 1582 1583
	.irq_mask = byt_irq_mask,
	.irq_unmask = byt_irq_unmask,
	.irq_set_type = byt_irq_type,
1584
	.flags = IRQCHIP_SKIP_SET_WAKE,
1585 1586 1587 1588 1589 1590
};

static void byt_gpio_irq_init_hw(struct byt_gpio *vg)
{
	void __iomem *reg;
	u32 base, value;
1591 1592 1593 1594 1595 1596 1597 1598
	int i;

	/*
	 * Clear interrupt triggers for all pins that are GPIOs and
	 * do not use direct IRQ mode. This will prevent spurious
	 * interrupts from misconfigured pins.
	 */
	for (i = 0; i < vg->chip.ngpio; i++) {
1599
		value = readl(byt_gpio_reg(vg, i, BYT_CONF0_REG));
1600 1601 1602 1603 1604 1605
		if ((value & BYT_PIN_MUX) == byt_get_gpio_mux(vg, i) &&
		    !(value & BYT_DIRECT_IRQ_EN)) {
			byt_gpio_clear_triggering(vg, i);
			dev_dbg(&vg->pdev->dev, "disabling GPIO %d\n", i);
		}
	}
1606 1607 1608

	/* clear interrupt status trigger registers */
	for (base = 0; base < vg->chip.ngpio; base += 32) {
1609
		reg = byt_gpio_reg(vg, base, BYT_INT_STAT_REG);
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
		writel(0xffffffff, reg);
		/* make sure trigger bits are cleared, if not then a pin
		   might be misconfigured in bios */
		value = readl(reg);
		if (value)
			dev_err(&vg->pdev->dev,
				"GPIO interrupt error, pins misconfigured\n");
	}
}

static int byt_gpio_probe(struct platform_device *pdev)
{
	struct byt_gpio *vg;
	struct gpio_chip *gc;
	struct resource *mem_rc, *irq_rc;
	struct device *dev = &pdev->dev;
	struct acpi_device *acpi_dev;
	struct pinctrl_gpio_range *range;
	acpi_handle handle = ACPI_HANDLE(dev);
	int ret;

	if (acpi_bus_get_device(handle, &acpi_dev))
		return -ENODEV;

	vg = devm_kzalloc(dev, sizeof(struct byt_gpio), GFP_KERNEL);
	if (!vg) {
		dev_err(&pdev->dev, "can't allocate byt_gpio chip data\n");
		return -ENOMEM;
	}

	for (range = byt_ranges; range->name; range++) {
		if (!strcmp(acpi_dev->pnp.unique_id, range->name)) {
			vg->chip.ngpio = range->npins;
			vg->range = range;
			break;
		}
	}

	if (!vg->chip.ngpio || !vg->range)
		return -ENODEV;

	vg->pdev = pdev;
	platform_set_drvdata(pdev, vg);

	mem_rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	vg->reg_base = devm_ioremap_resource(dev, mem_rc);
	if (IS_ERR(vg->reg_base))
		return PTR_ERR(vg->reg_base);

1659
	raw_spin_lock_init(&vg->lock);
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

	gc = &vg->chip;
	gc->label = dev_name(&pdev->dev);
	gc->owner = THIS_MODULE;
	gc->request = byt_gpio_request;
	gc->free = byt_gpio_free;
	gc->direction_input = byt_gpio_direction_input;
	gc->direction_output = byt_gpio_direction_output;
	gc->get = byt_gpio_get;
	gc->set = byt_gpio_set;
	gc->dbg_show = byt_gpio_dbg_show;
	gc->base = -1;
1672
	gc->can_sleep = false;
1673
	gc->parent = dev;
1674

1675 1676 1677 1678 1679
#ifdef CONFIG_PM_SLEEP
	vg->saved_context = devm_kcalloc(&pdev->dev, gc->ngpio,
				       sizeof(*vg->saved_context), GFP_KERNEL);
#endif

1680
	ret = gpiochip_add_data(gc, vg);
1681 1682 1683 1684 1685
	if (ret) {
		dev_err(&pdev->dev, "failed adding byt-gpio chip\n");
		return ret;
	}

1686 1687 1688 1689
	/* set up interrupts  */
	irq_rc = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (irq_rc && irq_rc->start) {
		byt_gpio_irq_init_hw(vg);
1690 1691 1692 1693 1694 1695 1696
		ret = gpiochip_irqchip_add(gc, &byt_irqchip, 0,
					   handle_simple_irq, IRQ_TYPE_NONE);
		if (ret) {
			dev_err(dev, "failed to add irqchip\n");
			gpiochip_remove(gc);
			return ret;
		}
1697

1698 1699 1700
		gpiochip_set_chained_irqchip(gc, &byt_irqchip,
					     (unsigned)irq_rc->start,
					     byt_gpio_irq_handler);
1701 1702
	}

1703 1704 1705 1706 1707
	pm_runtime_enable(dev);

	return 0;
}

1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
#ifdef CONFIG_PM_SLEEP
static int byt_gpio_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct byt_gpio *vg = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < vg->chip.ngpio; i++) {
		void __iomem *reg;
		u32 value;

1719
		reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
1720 1721 1722
		value = readl(reg) & BYT_CONF0_RESTORE_MASK;
		vg->saved_context[i].conf0 = value;

1723
		reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
		value = readl(reg) & BYT_VAL_RESTORE_MASK;
		vg->saved_context[i].val = value;
	}

	return 0;
}

static int byt_gpio_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct byt_gpio *vg = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < vg->chip.ngpio; i++) {
		void __iomem *reg;
		u32 value;

1741
		reg = byt_gpio_reg(vg, i, BYT_CONF0_REG);
1742 1743 1744 1745 1746 1747 1748 1749 1750
		value = readl(reg);
		if ((value & BYT_CONF0_RESTORE_MASK) !=
		     vg->saved_context[i].conf0) {
			value &= ~BYT_CONF0_RESTORE_MASK;
			value |= vg->saved_context[i].conf0;
			writel(value, reg);
			dev_info(dev, "restored pin %d conf0 %#08x", i, value);
		}

1751
		reg = byt_gpio_reg(vg, i, BYT_VAL_REG);
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
		value = readl(reg);
		if ((value & BYT_VAL_RESTORE_MASK) !=
		     vg->saved_context[i].val) {
			u32 v;

			v = value & ~BYT_VAL_RESTORE_MASK;
			v |= vg->saved_context[i].val;
			if (v != value) {
				writel(v, reg);
				dev_dbg(dev, "restored pin %d val %#08x\n",
					i, v);
			}
		}
	}

	return 0;
}
#endif

1771
#ifdef CONFIG_PM
1772 1773 1774 1775 1776 1777 1778 1779 1780
static int byt_gpio_runtime_suspend(struct device *dev)
{
	return 0;
}

static int byt_gpio_runtime_resume(struct device *dev)
{
	return 0;
}
1781
#endif
1782 1783

static const struct dev_pm_ops byt_gpio_pm_ops = {
1784 1785 1786
	SET_LATE_SYSTEM_SLEEP_PM_OPS(byt_gpio_suspend, byt_gpio_resume)
	SET_RUNTIME_PM_OPS(byt_gpio_runtime_suspend, byt_gpio_runtime_resume,
			   NULL)
1787 1788 1789
};

static const struct acpi_device_id byt_gpio_acpi_match[] = {
1790 1791
	{ "INT33B2", (kernel_ulong_t)byt_soc_data },
	{ "INT33FC", (kernel_ulong_t)byt_soc_data },
1792 1793 1794 1795 1796 1797 1798
	{ }
};
MODULE_DEVICE_TABLE(acpi, byt_gpio_acpi_match);

static int byt_gpio_remove(struct platform_device *pdev)
{
	struct byt_gpio *vg = platform_get_drvdata(pdev);
1799

1800
	pm_runtime_disable(&pdev->dev);
1801
	gpiochip_remove(&vg->chip);
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820

	return 0;
}

static struct platform_driver byt_gpio_driver = {
	.probe          = byt_gpio_probe,
	.remove         = byt_gpio_remove,
	.driver         = {
		.name   = "byt_gpio",
		.pm	= &byt_gpio_pm_ops,
		.acpi_match_table = ACPI_PTR(byt_gpio_acpi_match),
	},
};

static int __init byt_gpio_init(void)
{
	return platform_driver_register(&byt_gpio_driver);
}
subsys_initcall(byt_gpio_init);
1821 1822 1823 1824 1825 1826

static void __exit byt_gpio_exit(void)
{
	platform_driver_unregister(&byt_gpio_driver);
}
module_exit(byt_gpio_exit);