xref: /openbmc/linux/drivers/gpio/gpio-ich.c (revision ae84f15c651ac47aee2e0301059025813a041d19)
17ed0cf0aSAndy Shevchenko // SPDX-License-Identifier: GPL-2.0+
26ed9f9c4SPeter Tyser /*
33b923189SVincent Donnefort  * Intel ICH6-10, Series 5 and 6, Atom C2000 (Avoton/Rangeley) GPIO driver
46ed9f9c4SPeter Tyser  *
56ed9f9c4SPeter Tyser  * Copyright (C) 2010 Extreme Engineering Solutions.
66ed9f9c4SPeter Tyser  */
76ed9f9c4SPeter Tyser 
86ed9f9c4SPeter Tyser 
9488f270cSAndy Shevchenko #include <linux/bitops.h>
10488f270cSAndy Shevchenko #include <linux/gpio/driver.h>
118a06b08eSWilliam Breathitt Gray #include <linux/ioport.h>
12488f270cSAndy Shevchenko #include <linux/mfd/lpc_ich.h>
136ed9f9c4SPeter Tyser #include <linux/module.h>
146ed9f9c4SPeter Tyser #include <linux/pci.h>
156ed9f9c4SPeter Tyser #include <linux/platform_device.h>
166ed9f9c4SPeter Tyser 
176ed9f9c4SPeter Tyser #define DRV_NAME "gpio_ich"
186ed9f9c4SPeter Tyser 
196ed9f9c4SPeter Tyser /*
206ed9f9c4SPeter Tyser  * GPIO register offsets in GPIO I/O space.
216ed9f9c4SPeter Tyser  * Each chunk of 32 GPIOs is manipulated via its own USE_SELx, IO_SELx, and
226ed9f9c4SPeter Tyser  * LVLx registers.  Logic in the read/write functions takes a register and
236ed9f9c4SPeter Tyser  * an absolute bit number and determines the proper register offset and bit
246ed9f9c4SPeter Tyser  * number in that register.  For example, to read the value of GPIO bit 50
256ed9f9c4SPeter Tyser  * the code would access offset ichx_regs[2(=GPIO_LVL)][1(=50/32)],
266ed9f9c4SPeter Tyser  * bit 18 (50%32).
276ed9f9c4SPeter Tyser  */
286ed9f9c4SPeter Tyser enum GPIO_REG {
296ed9f9c4SPeter Tyser 	GPIO_USE_SEL = 0,
306ed9f9c4SPeter Tyser 	GPIO_IO_SEL,
316ed9f9c4SPeter Tyser 	GPIO_LVL,
327f6569f5SVincent Donnefort 	GPO_BLINK
336ed9f9c4SPeter Tyser };
346ed9f9c4SPeter Tyser 
357f6569f5SVincent Donnefort static const u8 ichx_regs[4][3] = {
366ed9f9c4SPeter Tyser 	{0x00, 0x30, 0x40},	/* USE_SEL[1-3] offsets */
376ed9f9c4SPeter Tyser 	{0x04, 0x34, 0x44},	/* IO_SEL[1-3] offsets */
386ed9f9c4SPeter Tyser 	{0x0c, 0x38, 0x48},	/* LVL[1-3] offsets */
397f6569f5SVincent Donnefort 	{0x18, 0x18, 0x18},	/* BLINK offset */
406ed9f9c4SPeter Tyser };
416ed9f9c4SPeter Tyser 
424f600adaSJean Delvare static const u8 ichx_reglen[3] = {
434f600adaSJean Delvare 	0x30, 0x10, 0x10,
444f600adaSJean Delvare };
454f600adaSJean Delvare 
463b923189SVincent Donnefort static const u8 avoton_regs[4][3] = {
473b923189SVincent Donnefort 	{0x00, 0x80, 0x00},
483b923189SVincent Donnefort 	{0x04, 0x84, 0x00},
493b923189SVincent Donnefort 	{0x08, 0x88, 0x00},
503b923189SVincent Donnefort };
513b923189SVincent Donnefort 
523b923189SVincent Donnefort static const u8 avoton_reglen[3] = {
533b923189SVincent Donnefort 	0x10, 0x10, 0x00,
543b923189SVincent Donnefort };
553b923189SVincent Donnefort 
566ed9f9c4SPeter Tyser #define ICHX_WRITE(val, reg, base_res)	outl(val, (reg) + (base_res)->start)
576ed9f9c4SPeter Tyser #define ICHX_READ(reg, base_res)	inl((reg) + (base_res)->start)
586ed9f9c4SPeter Tyser 
596ed9f9c4SPeter Tyser struct ichx_desc {
606ed9f9c4SPeter Tyser 	/* Max GPIO pins the chipset can have */
616ed9f9c4SPeter Tyser 	uint ngpio;
626ed9f9c4SPeter Tyser 
63bb62a35bSVincent Donnefort 	/* chipset registers */
64bb62a35bSVincent Donnefort 	const u8 (*regs)[3];
65bb62a35bSVincent Donnefort 	const u8 *reglen;
66bb62a35bSVincent Donnefort 
67ba7f74feSVincent Donnefort 	/* GPO_BLINK is available on this chipset */
68ba7f74feSVincent Donnefort 	bool have_blink;
69ba7f74feSVincent Donnefort 
706ed9f9c4SPeter Tyser 	/* Whether the chipset has GPIO in GPE0_STS in the PM IO region */
716ed9f9c4SPeter Tyser 	bool uses_gpe0;
726ed9f9c4SPeter Tyser 
736ed9f9c4SPeter Tyser 	/* USE_SEL is bogus on some chipsets, eg 3100 */
746ed9f9c4SPeter Tyser 	u32 use_sel_ignore[3];
756ed9f9c4SPeter Tyser 
766ed9f9c4SPeter Tyser 	/* Some chipsets have quirks, let these use their own request/get */
77*ae84f15cSAbanoub Sameh 	int (*request)(struct gpio_chip *chip, unsigned int offset);
78*ae84f15cSAbanoub Sameh 	int (*get)(struct gpio_chip *chip, unsigned int offset);
79e6540f33SVincent Donnefort 
80e6540f33SVincent Donnefort 	/*
81e6540f33SVincent Donnefort 	 * Some chipsets don't let reading output values on GPIO_LVL register
82e6540f33SVincent Donnefort 	 * this option allows driver caching written output values
83e6540f33SVincent Donnefort 	 */
84e6540f33SVincent Donnefort 	bool use_outlvl_cache;
856ed9f9c4SPeter Tyser };
866ed9f9c4SPeter Tyser 
876ed9f9c4SPeter Tyser static struct {
886ed9f9c4SPeter Tyser 	spinlock_t lock;
89ff4709b4SAndy Shevchenko 	struct device *dev;
906ed9f9c4SPeter Tyser 	struct gpio_chip chip;
916ed9f9c4SPeter Tyser 	struct resource *gpio_base;	/* GPIO IO base */
929b6d5690Ssachin agarwal 	struct resource *pm_base;	/* Power Management IO base */
936ed9f9c4SPeter Tyser 	struct ichx_desc *desc;	/* Pointer to chipset-specific description */
946ed9f9c4SPeter Tyser 	u32 orig_gpio_ctrl;	/* Orig CTRL value, used to restore on exit */
954f600adaSJean Delvare 	u8 use_gpio;		/* Which GPIO groups are usable */
96e6540f33SVincent Donnefort 	int outlvl_cache[3];	/* cached output values */
976ed9f9c4SPeter Tyser } ichx_priv;
986ed9f9c4SPeter Tyser 
996ed9f9c4SPeter Tyser static int modparam_gpiobase = -1;	/* dynamic */
1006ed9f9c4SPeter Tyser module_param_named(gpiobase, modparam_gpiobase, int, 0444);
1015f6f2b9fSAndy Shevchenko MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, which is the default.");
1026ed9f9c4SPeter Tyser 
103*ae84f15cSAbanoub Sameh static int ichx_write_bit(int reg, unsigned int nr, int val, int verify)
1046ed9f9c4SPeter Tyser {
1056ed9f9c4SPeter Tyser 	unsigned long flags;
1066ed9f9c4SPeter Tyser 	u32 data, tmp;
1076ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
1086ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
1096ed9f9c4SPeter Tyser 
1106ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
1116ed9f9c4SPeter Tyser 
112e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
113e6540f33SVincent Donnefort 		data = ichx_priv.outlvl_cache[reg_nr];
114e6540f33SVincent Donnefort 	else
115bb62a35bSVincent Donnefort 		data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
116bb62a35bSVincent Donnefort 				 ichx_priv.gpio_base);
117e6540f33SVincent Donnefort 
1186ed9f9c4SPeter Tyser 	if (val)
1197a8fd1f5SLinus Walleij 		data |= BIT(bit);
1206ed9f9c4SPeter Tyser 	else
1217a8fd1f5SLinus Walleij 		data &= ~BIT(bit);
122bb62a35bSVincent Donnefort 	ICHX_WRITE(data, ichx_priv.desc->regs[reg][reg_nr],
123bb62a35bSVincent Donnefort 			 ichx_priv.gpio_base);
124e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
125e6540f33SVincent Donnefort 		ichx_priv.outlvl_cache[reg_nr] = data;
126e6540f33SVincent Donnefort 
127bb62a35bSVincent Donnefort 	tmp = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
128bb62a35bSVincent Donnefort 			ichx_priv.gpio_base);
1296ed9f9c4SPeter Tyser 
1306ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1316ed9f9c4SPeter Tyser 
132c5aaa316SAndy Shevchenko 	return (verify && data != tmp) ? -EPERM : 0;
1336ed9f9c4SPeter Tyser }
1346ed9f9c4SPeter Tyser 
135*ae84f15cSAbanoub Sameh static int ichx_read_bit(int reg, unsigned int nr)
1366ed9f9c4SPeter Tyser {
1376ed9f9c4SPeter Tyser 	unsigned long flags;
1386ed9f9c4SPeter Tyser 	u32 data;
1396ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
1406ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
1416ed9f9c4SPeter Tyser 
1426ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
1436ed9f9c4SPeter Tyser 
144bb62a35bSVincent Donnefort 	data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
145bb62a35bSVincent Donnefort 			 ichx_priv.gpio_base);
1466ed9f9c4SPeter Tyser 
147e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
148e6540f33SVincent Donnefort 		data = ichx_priv.outlvl_cache[reg_nr] | data;
149e6540f33SVincent Donnefort 
1506ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1516ed9f9c4SPeter Tyser 
1527a8fd1f5SLinus Walleij 	return !!(data & BIT(bit));
1536ed9f9c4SPeter Tyser }
1546ed9f9c4SPeter Tyser 
155*ae84f15cSAbanoub Sameh static bool ichx_gpio_check_available(struct gpio_chip *gpio, unsigned int nr)
1564f600adaSJean Delvare {
1577a8fd1f5SLinus Walleij 	return !!(ichx_priv.use_gpio & BIT(nr / 32));
1584f600adaSJean Delvare }
1594f600adaSJean Delvare 
160*ae84f15cSAbanoub Sameh static int ichx_gpio_get_direction(struct gpio_chip *gpio, unsigned int nr)
16162e08f25SAaron Sierra {
162e42615ecSMatti Vaittinen 	if (ichx_read_bit(GPIO_IO_SEL, nr))
163e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
164e42615ecSMatti Vaittinen 
165e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
16662e08f25SAaron Sierra }
16762e08f25SAaron Sierra 
168*ae84f15cSAbanoub Sameh static int ichx_gpio_direction_input(struct gpio_chip *gpio, unsigned int nr)
1696ed9f9c4SPeter Tyser {
1706ed9f9c4SPeter Tyser 	/*
1716ed9f9c4SPeter Tyser 	 * Try setting pin as an input and verify it worked since many pins
1726ed9f9c4SPeter Tyser 	 * are output-only.
1736ed9f9c4SPeter Tyser 	 */
174c5aaa316SAndy Shevchenko 	return ichx_write_bit(GPIO_IO_SEL, nr, 1, 1);
1756ed9f9c4SPeter Tyser }
1766ed9f9c4SPeter Tyser 
177*ae84f15cSAbanoub Sameh static int ichx_gpio_direction_output(struct gpio_chip *gpio, unsigned int nr,
1786ed9f9c4SPeter Tyser 					int val)
1796ed9f9c4SPeter Tyser {
1807f6569f5SVincent Donnefort 	/* Disable blink hardware which is available for GPIOs from 0 to 31. */
181ba7f74feSVincent Donnefort 	if (nr < 32 && ichx_priv.desc->have_blink)
1827f6569f5SVincent Donnefort 		ichx_write_bit(GPO_BLINK, nr, 0, 0);
1837f6569f5SVincent Donnefort 
1846ed9f9c4SPeter Tyser 	/* Set GPIO output value. */
1856ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
1866ed9f9c4SPeter Tyser 
1876ed9f9c4SPeter Tyser 	/*
1886ed9f9c4SPeter Tyser 	 * Try setting pin as an output and verify it worked since many pins
1896ed9f9c4SPeter Tyser 	 * are input-only.
1906ed9f9c4SPeter Tyser 	 */
191c5aaa316SAndy Shevchenko 	return ichx_write_bit(GPIO_IO_SEL, nr, 0, 1);
1926ed9f9c4SPeter Tyser }
1936ed9f9c4SPeter Tyser 
194*ae84f15cSAbanoub Sameh static int ichx_gpio_get(struct gpio_chip *chip, unsigned int nr)
1956ed9f9c4SPeter Tyser {
1966ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_LVL, nr);
1976ed9f9c4SPeter Tyser }
1986ed9f9c4SPeter Tyser 
199*ae84f15cSAbanoub Sameh static int ich6_gpio_get(struct gpio_chip *chip, unsigned int nr)
2006ed9f9c4SPeter Tyser {
2016ed9f9c4SPeter Tyser 	unsigned long flags;
2026ed9f9c4SPeter Tyser 	u32 data;
2036ed9f9c4SPeter Tyser 
2046ed9f9c4SPeter Tyser 	/*
2056ed9f9c4SPeter Tyser 	 * GPI 0 - 15 need to be read from the power management registers on
2066ed9f9c4SPeter Tyser 	 * a ICH6/3100 bridge.
2076ed9f9c4SPeter Tyser 	 */
2086ed9f9c4SPeter Tyser 	if (nr < 16) {
2096ed9f9c4SPeter Tyser 		if (!ichx_priv.pm_base)
2106ed9f9c4SPeter Tyser 			return -ENXIO;
2116ed9f9c4SPeter Tyser 
2126ed9f9c4SPeter Tyser 		spin_lock_irqsave(&ichx_priv.lock, flags);
2136ed9f9c4SPeter Tyser 
2146ed9f9c4SPeter Tyser 		/* GPI 0 - 15 are latched, write 1 to clear*/
2157a8fd1f5SLinus Walleij 		ICHX_WRITE(BIT(16 + nr), 0, ichx_priv.pm_base);
2166ed9f9c4SPeter Tyser 		data = ICHX_READ(0, ichx_priv.pm_base);
2176ed9f9c4SPeter Tyser 
2186ed9f9c4SPeter Tyser 		spin_unlock_irqrestore(&ichx_priv.lock, flags);
2196ed9f9c4SPeter Tyser 
2207a8fd1f5SLinus Walleij 		return !!((data >> 16) & BIT(nr));
2216ed9f9c4SPeter Tyser 	} else {
2226ed9f9c4SPeter Tyser 		return ichx_gpio_get(chip, nr);
2236ed9f9c4SPeter Tyser 	}
2246ed9f9c4SPeter Tyser }
2256ed9f9c4SPeter Tyser 
226*ae84f15cSAbanoub Sameh static int ichx_gpio_request(struct gpio_chip *chip, unsigned int nr)
2276ed9f9c4SPeter Tyser {
22825f27db4SJean Delvare 	if (!ichx_gpio_check_available(chip, nr))
22925f27db4SJean Delvare 		return -ENXIO;
23025f27db4SJean Delvare 
2316ed9f9c4SPeter Tyser 	/*
2326ed9f9c4SPeter Tyser 	 * Note we assume the BIOS properly set a bridge's USE value.  Some
2336ed9f9c4SPeter Tyser 	 * chips (eg Intel 3100) have bogus USE values though, so first see if
2346ed9f9c4SPeter Tyser 	 * the chipset's USE value can be trusted for this specific bit.
2356ed9f9c4SPeter Tyser 	 * If it can't be trusted, assume that the pin can be used as a GPIO.
2366ed9f9c4SPeter Tyser 	 */
2377a8fd1f5SLinus Walleij 	if (ichx_priv.desc->use_sel_ignore[nr / 32] & BIT(nr & 0x1f))
2382ab3a749SJean Delvare 		return 0;
2396ed9f9c4SPeter Tyser 
2406ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_USE_SEL, nr) ? 0 : -ENODEV;
2416ed9f9c4SPeter Tyser }
2426ed9f9c4SPeter Tyser 
243*ae84f15cSAbanoub Sameh static int ich6_gpio_request(struct gpio_chip *chip, unsigned int nr)
2446ed9f9c4SPeter Tyser {
2456ed9f9c4SPeter Tyser 	/*
2466ed9f9c4SPeter Tyser 	 * Fixups for bits 16 and 17 are necessary on the Intel ICH6/3100
2476ed9f9c4SPeter Tyser 	 * bridge as they are controlled by USE register bits 0 and 1.  See
2486ed9f9c4SPeter Tyser 	 * "Table 704 GPIO_USE_SEL1 register" in the i3100 datasheet for
2496ed9f9c4SPeter Tyser 	 * additional info.
2506ed9f9c4SPeter Tyser 	 */
2516ed9f9c4SPeter Tyser 	if (nr == 16 || nr == 17)
2526ed9f9c4SPeter Tyser 		nr -= 16;
2536ed9f9c4SPeter Tyser 
2546ed9f9c4SPeter Tyser 	return ichx_gpio_request(chip, nr);
2556ed9f9c4SPeter Tyser }
2566ed9f9c4SPeter Tyser 
257*ae84f15cSAbanoub Sameh static void ichx_gpio_set(struct gpio_chip *chip, unsigned int nr, int val)
2586ed9f9c4SPeter Tyser {
2596ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
2606ed9f9c4SPeter Tyser }
2616ed9f9c4SPeter Tyser 
2623836309dSBill Pemberton static void ichx_gpiolib_setup(struct gpio_chip *chip)
2636ed9f9c4SPeter Tyser {
2646ed9f9c4SPeter Tyser 	chip->owner = THIS_MODULE;
2656ed9f9c4SPeter Tyser 	chip->label = DRV_NAME;
266ff4709b4SAndy Shevchenko 	chip->parent = ichx_priv.dev;
2676ed9f9c4SPeter Tyser 
2686ed9f9c4SPeter Tyser 	/* Allow chip-specific overrides of request()/get() */
2696ed9f9c4SPeter Tyser 	chip->request = ichx_priv.desc->request ?
2706ed9f9c4SPeter Tyser 		ichx_priv.desc->request : ichx_gpio_request;
2716ed9f9c4SPeter Tyser 	chip->get = ichx_priv.desc->get ?
2726ed9f9c4SPeter Tyser 		ichx_priv.desc->get : ichx_gpio_get;
2736ed9f9c4SPeter Tyser 
2746ed9f9c4SPeter Tyser 	chip->set = ichx_gpio_set;
27562e08f25SAaron Sierra 	chip->get_direction = ichx_gpio_get_direction;
2766ed9f9c4SPeter Tyser 	chip->direction_input = ichx_gpio_direction_input;
2776ed9f9c4SPeter Tyser 	chip->direction_output = ichx_gpio_direction_output;
2786ed9f9c4SPeter Tyser 	chip->base = modparam_gpiobase;
2796ed9f9c4SPeter Tyser 	chip->ngpio = ichx_priv.desc->ngpio;
2809fb1f39eSLinus Walleij 	chip->can_sleep = false;
2816ed9f9c4SPeter Tyser 	chip->dbg_show = NULL;
2826ed9f9c4SPeter Tyser }
2836ed9f9c4SPeter Tyser 
2846ed9f9c4SPeter Tyser /* ICH6-based, 631xesb-based */
2856ed9f9c4SPeter Tyser static struct ichx_desc ich6_desc = {
2866ed9f9c4SPeter Tyser 	/* Bridges using the ICH6 controller need fixups for GPIO 0 - 17 */
2876ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
2886ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
2896ed9f9c4SPeter Tyser 
2906ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
2916ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
2926ed9f9c4SPeter Tyser 
2936ed9f9c4SPeter Tyser 	.ngpio = 50,
294ba7f74feSVincent Donnefort 	.have_blink = true,
295a7008ee1SVincent Donnefort 	.regs = ichx_regs,
296a7008ee1SVincent Donnefort 	.reglen = ichx_reglen,
2976ed9f9c4SPeter Tyser };
2986ed9f9c4SPeter Tyser 
2996ed9f9c4SPeter Tyser /* Intel 3100 */
3006ed9f9c4SPeter Tyser static struct ichx_desc i3100_desc = {
3016ed9f9c4SPeter Tyser 	/*
3026ed9f9c4SPeter Tyser 	 * Bits 16,17, 20 of USE_SEL and bit 16 of USE_SEL2 always read 0 on
3036ed9f9c4SPeter Tyser 	 * the Intel 3100.  See "Table 712. GPIO Summary Table" of 3100
3046ed9f9c4SPeter Tyser 	 * Datasheet for more info.
3056ed9f9c4SPeter Tyser 	 */
3066ed9f9c4SPeter Tyser 	.use_sel_ignore = {0x00130000, 0x00010000, 0x0},
3076ed9f9c4SPeter Tyser 
3086ed9f9c4SPeter Tyser 	/* The 3100 needs fixups for GPIO 0 - 17 */
3096ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
3106ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
3116ed9f9c4SPeter Tyser 
3126ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
3136ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
3146ed9f9c4SPeter Tyser 
3156ed9f9c4SPeter Tyser 	.ngpio = 50,
316a7008ee1SVincent Donnefort 	.regs = ichx_regs,
317a7008ee1SVincent Donnefort 	.reglen = ichx_reglen,
3186ed9f9c4SPeter Tyser };
3196ed9f9c4SPeter Tyser 
3206ed9f9c4SPeter Tyser /* ICH7 and ICH8-based */
3216ed9f9c4SPeter Tyser static struct ichx_desc ich7_desc = {
3226ed9f9c4SPeter Tyser 	.ngpio = 50,
323ba7f74feSVincent Donnefort 	.have_blink = true,
324bb62a35bSVincent Donnefort 	.regs = ichx_regs,
325bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3266ed9f9c4SPeter Tyser };
3276ed9f9c4SPeter Tyser 
3286ed9f9c4SPeter Tyser /* ICH9-based */
3296ed9f9c4SPeter Tyser static struct ichx_desc ich9_desc = {
3306ed9f9c4SPeter Tyser 	.ngpio = 61,
331ba7f74feSVincent Donnefort 	.have_blink = true,
332bb62a35bSVincent Donnefort 	.regs = ichx_regs,
333bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3346ed9f9c4SPeter Tyser };
3356ed9f9c4SPeter Tyser 
3366ed9f9c4SPeter Tyser /* ICH10-based - Consumer/corporate versions have different amount of GPIO */
3376ed9f9c4SPeter Tyser static struct ichx_desc ich10_cons_desc = {
3386ed9f9c4SPeter Tyser 	.ngpio = 61,
339ba7f74feSVincent Donnefort 	.have_blink = true,
340bb62a35bSVincent Donnefort 	.regs = ichx_regs,
341bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3426ed9f9c4SPeter Tyser };
3436ed9f9c4SPeter Tyser static struct ichx_desc ich10_corp_desc = {
3446ed9f9c4SPeter Tyser 	.ngpio = 72,
345ba7f74feSVincent Donnefort 	.have_blink = true,
346bb62a35bSVincent Donnefort 	.regs = ichx_regs,
347bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3486ed9f9c4SPeter Tyser };
3496ed9f9c4SPeter Tyser 
3506ed9f9c4SPeter Tyser /* Intel 5 series, 6 series, 3400 series, and C200 series */
3516ed9f9c4SPeter Tyser static struct ichx_desc intel5_desc = {
3526ed9f9c4SPeter Tyser 	.ngpio = 76,
353bb62a35bSVincent Donnefort 	.regs = ichx_regs,
354bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3556ed9f9c4SPeter Tyser };
3566ed9f9c4SPeter Tyser 
3573b923189SVincent Donnefort /* Avoton */
3583b923189SVincent Donnefort static struct ichx_desc avoton_desc = {
3593b923189SVincent Donnefort 	/* Avoton has only 59 GPIOs, but we assume the first set of register
3603b923189SVincent Donnefort 	 * (Core) has 32 instead of 31 to keep gpio-ich compliance
3613b923189SVincent Donnefort 	 */
3623b923189SVincent Donnefort 	.ngpio = 60,
3633b923189SVincent Donnefort 	.regs = avoton_regs,
3643b923189SVincent Donnefort 	.reglen = avoton_reglen,
3653b923189SVincent Donnefort 	.use_outlvl_cache = true,
3663b923189SVincent Donnefort };
3673b923189SVincent Donnefort 
3688a06b08eSWilliam Breathitt Gray static int ichx_gpio_request_regions(struct device *dev,
3698a06b08eSWilliam Breathitt Gray 	struct resource *res_base, const char *name, u8 use_gpio)
3704f600adaSJean Delvare {
3714f600adaSJean Delvare 	int i;
3724f600adaSJean Delvare 
3734f600adaSJean Delvare 	if (!res_base || !res_base->start || !res_base->end)
3744f600adaSJean Delvare 		return -ENODEV;
3754f600adaSJean Delvare 
376bb62a35bSVincent Donnefort 	for (i = 0; i < ARRAY_SIZE(ichx_priv.desc->regs[0]); i++) {
3777a8fd1f5SLinus Walleij 		if (!(use_gpio & BIT(i)))
3784f600adaSJean Delvare 			continue;
3798a06b08eSWilliam Breathitt Gray 		if (!devm_request_region(dev,
380bb62a35bSVincent Donnefort 				res_base->start + ichx_priv.desc->regs[0][i],
381bb62a35bSVincent Donnefort 				ichx_priv.desc->reglen[i], name))
3824f600adaSJean Delvare 			return -EBUSY;
3834f600adaSJean Delvare 	}
3848a06b08eSWilliam Breathitt Gray 	return 0;
3854f600adaSJean Delvare }
3864f600adaSJean Delvare 
3873836309dSBill Pemberton static int ichx_gpio_probe(struct platform_device *pdev)
3886ed9f9c4SPeter Tyser {
389ff4709b4SAndy Shevchenko 	struct device *dev = &pdev->dev;
390ff4709b4SAndy Shevchenko 	struct lpc_ich_info *ich_info = dev_get_platdata(dev);
3916ed9f9c4SPeter Tyser 	struct resource *res_base, *res_pm;
3926ed9f9c4SPeter Tyser 	int err;
3936ed9f9c4SPeter Tyser 
3946ed9f9c4SPeter Tyser 	if (!ich_info)
3956ed9f9c4SPeter Tyser 		return -ENODEV;
3966ed9f9c4SPeter Tyser 
3976ed9f9c4SPeter Tyser 	switch (ich_info->gpio_version) {
3986ed9f9c4SPeter Tyser 	case ICH_I3100_GPIO:
3996ed9f9c4SPeter Tyser 		ichx_priv.desc = &i3100_desc;
4006ed9f9c4SPeter Tyser 		break;
4016ed9f9c4SPeter Tyser 	case ICH_V5_GPIO:
4026ed9f9c4SPeter Tyser 		ichx_priv.desc = &intel5_desc;
4036ed9f9c4SPeter Tyser 		break;
4046ed9f9c4SPeter Tyser 	case ICH_V6_GPIO:
4056ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich6_desc;
4066ed9f9c4SPeter Tyser 		break;
4076ed9f9c4SPeter Tyser 	case ICH_V7_GPIO:
4086ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich7_desc;
4096ed9f9c4SPeter Tyser 		break;
4106ed9f9c4SPeter Tyser 	case ICH_V9_GPIO:
4116ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich9_desc;
4126ed9f9c4SPeter Tyser 		break;
4136ed9f9c4SPeter Tyser 	case ICH_V10CORP_GPIO:
4146ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_corp_desc;
4156ed9f9c4SPeter Tyser 		break;
4166ed9f9c4SPeter Tyser 	case ICH_V10CONS_GPIO:
4176ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_cons_desc;
4186ed9f9c4SPeter Tyser 		break;
4193b923189SVincent Donnefort 	case AVOTON_GPIO:
4203b923189SVincent Donnefort 		ichx_priv.desc = &avoton_desc;
4213b923189SVincent Donnefort 		break;
4226ed9f9c4SPeter Tyser 	default:
4236ed9f9c4SPeter Tyser 		return -ENODEV;
4246ed9f9c4SPeter Tyser 	}
4256ed9f9c4SPeter Tyser 
426ff4709b4SAndy Shevchenko 	ichx_priv.dev = dev;
427d39a948fSJean Delvare 	spin_lock_init(&ichx_priv.lock);
428ff4709b4SAndy Shevchenko 
4296ed9f9c4SPeter Tyser 	res_base = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPIO);
430ff4709b4SAndy Shevchenko 	err = ichx_gpio_request_regions(dev, res_base, pdev->name,
431ff4709b4SAndy Shevchenko 					ich_info->use_gpio);
4324f600adaSJean Delvare 	if (err)
4334f600adaSJean Delvare 		return err;
4346ed9f9c4SPeter Tyser 
4356ed9f9c4SPeter Tyser 	ichx_priv.gpio_base = res_base;
436ff4709b4SAndy Shevchenko 	ichx_priv.use_gpio = ich_info->use_gpio;
4376ed9f9c4SPeter Tyser 
4386ed9f9c4SPeter Tyser 	/*
4396ed9f9c4SPeter Tyser 	 * If necessary, determine the I/O address of ACPI/power management
4405f6f2b9fSAndy Shevchenko 	 * registers which are needed to read the GPE0 register for GPI pins
4416ed9f9c4SPeter Tyser 	 * 0 - 15 on some chipsets.
4426ed9f9c4SPeter Tyser 	 */
4436ed9f9c4SPeter Tyser 	if (!ichx_priv.desc->uses_gpe0)
4446ed9f9c4SPeter Tyser 		goto init;
4456ed9f9c4SPeter Tyser 
4466ed9f9c4SPeter Tyser 	res_pm = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPE0);
4476ed9f9c4SPeter Tyser 	if (!res_pm) {
448c086bea5SAndy Shevchenko 		dev_warn(dev, "ACPI BAR is unavailable, GPI 0 - 15 unavailable\n");
4496ed9f9c4SPeter Tyser 		goto init;
4506ed9f9c4SPeter Tyser 	}
4516ed9f9c4SPeter Tyser 
452ff4709b4SAndy Shevchenko 	if (!devm_request_region(dev, res_pm->start, resource_size(res_pm),
453ff4709b4SAndy Shevchenko 				 pdev->name)) {
454c086bea5SAndy Shevchenko 		dev_warn(dev, "ACPI BAR is busy, GPI 0 - 15 unavailable\n");
4556ed9f9c4SPeter Tyser 		goto init;
4566ed9f9c4SPeter Tyser 	}
4576ed9f9c4SPeter Tyser 
4586ed9f9c4SPeter Tyser 	ichx_priv.pm_base = res_pm;
4596ed9f9c4SPeter Tyser 
4606ed9f9c4SPeter Tyser init:
4616ed9f9c4SPeter Tyser 	ichx_gpiolib_setup(&ichx_priv.chip);
4624eab22e7SLinus Walleij 	err = gpiochip_add_data(&ichx_priv.chip, NULL);
4636ed9f9c4SPeter Tyser 	if (err) {
464c086bea5SAndy Shevchenko 		dev_err(dev, "Failed to register GPIOs\n");
4658a06b08eSWilliam Breathitt Gray 		return err;
4666ed9f9c4SPeter Tyser 	}
4676ed9f9c4SPeter Tyser 
468c086bea5SAndy Shevchenko 	dev_info(dev, "GPIO from %d to %d\n", ichx_priv.chip.base,
469c086bea5SAndy Shevchenko 		 ichx_priv.chip.base + ichx_priv.chip.ngpio - 1);
4706ed9f9c4SPeter Tyser 
4716ed9f9c4SPeter Tyser 	return 0;
4726ed9f9c4SPeter Tyser }
4736ed9f9c4SPeter Tyser 
474206210ceSBill Pemberton static int ichx_gpio_remove(struct platform_device *pdev)
4756ed9f9c4SPeter Tyser {
4769f5132aeSabdoulaye berthe 	gpiochip_remove(&ichx_priv.chip);
4776ed9f9c4SPeter Tyser 
4786ed9f9c4SPeter Tyser 	return 0;
4796ed9f9c4SPeter Tyser }
4806ed9f9c4SPeter Tyser 
4816ed9f9c4SPeter Tyser static struct platform_driver ichx_gpio_driver = {
4826ed9f9c4SPeter Tyser 	.driver		= {
4836ed9f9c4SPeter Tyser 		.name	= DRV_NAME,
4846ed9f9c4SPeter Tyser 	},
4856ed9f9c4SPeter Tyser 	.probe		= ichx_gpio_probe,
4868283c4ffSBill Pemberton 	.remove		= ichx_gpio_remove,
4876ed9f9c4SPeter Tyser };
4886ed9f9c4SPeter Tyser 
4896ed9f9c4SPeter Tyser module_platform_driver(ichx_gpio_driver);
4906ed9f9c4SPeter Tyser 
4916ed9f9c4SPeter Tyser MODULE_AUTHOR("Peter Tyser <ptyser@xes-inc.com>");
4926ed9f9c4SPeter Tyser MODULE_DESCRIPTION("GPIO interface for Intel ICH series");
4936ed9f9c4SPeter Tyser MODULE_LICENSE("GPL");
4946ed9f9c4SPeter Tyser MODULE_ALIAS("platform:"DRV_NAME);
495