Linux Audio

Check our new training course

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