xref: /openbmc/linux/drivers/gpio/gpio-ich.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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 
8488f270cSAndy Shevchenko #include <linux/bitops.h>
9488f270cSAndy Shevchenko #include <linux/gpio/driver.h>
108a06b08eSWilliam Breathitt Gray #include <linux/ioport.h>
11488f270cSAndy Shevchenko #include <linux/mfd/lpc_ich.h>
126ed9f9c4SPeter Tyser #include <linux/module.h>
136ed9f9c4SPeter Tyser #include <linux/platform_device.h>
146ed9f9c4SPeter Tyser 
156ed9f9c4SPeter Tyser #define DRV_NAME "gpio_ich"
166ed9f9c4SPeter Tyser 
176ed9f9c4SPeter Tyser /*
186ed9f9c4SPeter Tyser  * GPIO register offsets in GPIO I/O space.
196ed9f9c4SPeter Tyser  * Each chunk of 32 GPIOs is manipulated via its own USE_SELx, IO_SELx, and
206ed9f9c4SPeter Tyser  * LVLx registers.  Logic in the read/write functions takes a register and
216ed9f9c4SPeter Tyser  * an absolute bit number and determines the proper register offset and bit
226ed9f9c4SPeter Tyser  * number in that register.  For example, to read the value of GPIO bit 50
236ed9f9c4SPeter Tyser  * the code would access offset ichx_regs[2(=GPIO_LVL)][1(=50/32)],
246ed9f9c4SPeter Tyser  * bit 18 (50%32).
256ed9f9c4SPeter Tyser  */
266ed9f9c4SPeter Tyser enum GPIO_REG {
276ed9f9c4SPeter Tyser 	GPIO_USE_SEL = 0,
286ed9f9c4SPeter Tyser 	GPIO_IO_SEL,
296ed9f9c4SPeter Tyser 	GPIO_LVL,
307f6569f5SVincent Donnefort 	GPO_BLINK
316ed9f9c4SPeter Tyser };
326ed9f9c4SPeter Tyser 
337f6569f5SVincent Donnefort static const u8 ichx_regs[4][3] = {
346ed9f9c4SPeter Tyser 	{0x00, 0x30, 0x40},	/* USE_SEL[1-3] offsets */
356ed9f9c4SPeter Tyser 	{0x04, 0x34, 0x44},	/* IO_SEL[1-3] offsets */
366ed9f9c4SPeter Tyser 	{0x0c, 0x38, 0x48},	/* LVL[1-3] offsets */
377f6569f5SVincent Donnefort 	{0x18, 0x18, 0x18},	/* BLINK offset */
386ed9f9c4SPeter Tyser };
396ed9f9c4SPeter Tyser 
404f600adaSJean Delvare static const u8 ichx_reglen[3] = {
414f600adaSJean Delvare 	0x30, 0x10, 0x10,
424f600adaSJean Delvare };
434f600adaSJean Delvare 
443b923189SVincent Donnefort static const u8 avoton_regs[4][3] = {
453b923189SVincent Donnefort 	{0x00, 0x80, 0x00},
463b923189SVincent Donnefort 	{0x04, 0x84, 0x00},
473b923189SVincent Donnefort 	{0x08, 0x88, 0x00},
483b923189SVincent Donnefort };
493b923189SVincent Donnefort 
503b923189SVincent Donnefort static const u8 avoton_reglen[3] = {
513b923189SVincent Donnefort 	0x10, 0x10, 0x00,
523b923189SVincent Donnefort };
533b923189SVincent Donnefort 
546ed9f9c4SPeter Tyser #define ICHX_WRITE(val, reg, base_res)	outl(val, (reg) + (base_res)->start)
556ed9f9c4SPeter Tyser #define ICHX_READ(reg, base_res)	inl((reg) + (base_res)->start)
566ed9f9c4SPeter Tyser 
576ed9f9c4SPeter Tyser struct ichx_desc {
586ed9f9c4SPeter Tyser 	/* Max GPIO pins the chipset can have */
596ed9f9c4SPeter Tyser 	uint ngpio;
606ed9f9c4SPeter Tyser 
61bb62a35bSVincent Donnefort 	/* chipset registers */
62bb62a35bSVincent Donnefort 	const u8 (*regs)[3];
63bb62a35bSVincent Donnefort 	const u8 *reglen;
64bb62a35bSVincent Donnefort 
65ba7f74feSVincent Donnefort 	/* GPO_BLINK is available on this chipset */
66ba7f74feSVincent Donnefort 	bool have_blink;
67ba7f74feSVincent Donnefort 
686ed9f9c4SPeter Tyser 	/* Whether the chipset has GPIO in GPE0_STS in the PM IO region */
696ed9f9c4SPeter Tyser 	bool uses_gpe0;
706ed9f9c4SPeter Tyser 
716ed9f9c4SPeter Tyser 	/* USE_SEL is bogus on some chipsets, eg 3100 */
726ed9f9c4SPeter Tyser 	u32 use_sel_ignore[3];
736ed9f9c4SPeter Tyser 
746ed9f9c4SPeter Tyser 	/* Some chipsets have quirks, let these use their own request/get */
75ae84f15cSAbanoub Sameh 	int (*request)(struct gpio_chip *chip, unsigned int offset);
76ae84f15cSAbanoub Sameh 	int (*get)(struct gpio_chip *chip, unsigned int offset);
77e6540f33SVincent Donnefort 
78e6540f33SVincent Donnefort 	/*
79e6540f33SVincent Donnefort 	 * Some chipsets don't let reading output values on GPIO_LVL register
80e6540f33SVincent Donnefort 	 * this option allows driver caching written output values
81e6540f33SVincent Donnefort 	 */
82e6540f33SVincent Donnefort 	bool use_outlvl_cache;
836ed9f9c4SPeter Tyser };
846ed9f9c4SPeter Tyser 
856ed9f9c4SPeter Tyser static struct {
866ed9f9c4SPeter Tyser 	spinlock_t lock;
87ff4709b4SAndy Shevchenko 	struct device *dev;
886ed9f9c4SPeter Tyser 	struct gpio_chip chip;
896ed9f9c4SPeter Tyser 	struct resource *gpio_base;	/* GPIO IO base */
909b6d5690Ssachin agarwal 	struct resource *pm_base;	/* Power Management IO base */
916ed9f9c4SPeter Tyser 	struct ichx_desc *desc;	/* Pointer to chipset-specific description */
926ed9f9c4SPeter Tyser 	u32 orig_gpio_ctrl;	/* Orig CTRL value, used to restore on exit */
934f600adaSJean Delvare 	u8 use_gpio;		/* Which GPIO groups are usable */
94e6540f33SVincent Donnefort 	int outlvl_cache[3];	/* cached output values */
956ed9f9c4SPeter Tyser } ichx_priv;
966ed9f9c4SPeter Tyser 
976ed9f9c4SPeter Tyser static int modparam_gpiobase = -1;	/* dynamic */
986ed9f9c4SPeter Tyser module_param_named(gpiobase, modparam_gpiobase, int, 0444);
995f6f2b9fSAndy Shevchenko MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, which is the default.");
1006ed9f9c4SPeter Tyser 
ichx_write_bit(int reg,unsigned int nr,int val,int verify)101ae84f15cSAbanoub Sameh static int ichx_write_bit(int reg, unsigned int nr, int val, int verify)
1026ed9f9c4SPeter Tyser {
1036ed9f9c4SPeter Tyser 	unsigned long flags;
1046ed9f9c4SPeter Tyser 	u32 data, tmp;
1056ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
1066ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
1076ed9f9c4SPeter Tyser 
1086ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
1096ed9f9c4SPeter Tyser 
110e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
111e6540f33SVincent Donnefort 		data = ichx_priv.outlvl_cache[reg_nr];
112e6540f33SVincent Donnefort 	else
113bb62a35bSVincent Donnefort 		data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
114bb62a35bSVincent Donnefort 				 ichx_priv.gpio_base);
115e6540f33SVincent Donnefort 
1166ed9f9c4SPeter Tyser 	if (val)
1177a8fd1f5SLinus Walleij 		data |= BIT(bit);
1186ed9f9c4SPeter Tyser 	else
1197a8fd1f5SLinus Walleij 		data &= ~BIT(bit);
120bb62a35bSVincent Donnefort 	ICHX_WRITE(data, ichx_priv.desc->regs[reg][reg_nr],
121bb62a35bSVincent Donnefort 			 ichx_priv.gpio_base);
122e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
123e6540f33SVincent Donnefort 		ichx_priv.outlvl_cache[reg_nr] = data;
124e6540f33SVincent Donnefort 
125bb62a35bSVincent Donnefort 	tmp = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
126bb62a35bSVincent Donnefort 			ichx_priv.gpio_base);
1276ed9f9c4SPeter Tyser 
1286ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1296ed9f9c4SPeter Tyser 
130c5aaa316SAndy Shevchenko 	return (verify && data != tmp) ? -EPERM : 0;
1316ed9f9c4SPeter Tyser }
1326ed9f9c4SPeter Tyser 
ichx_read_bit(int reg,unsigned int nr)133ae84f15cSAbanoub Sameh static int ichx_read_bit(int reg, unsigned int nr)
1346ed9f9c4SPeter Tyser {
1356ed9f9c4SPeter Tyser 	unsigned long flags;
1366ed9f9c4SPeter Tyser 	u32 data;
1376ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
1386ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
1396ed9f9c4SPeter Tyser 
1406ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
1416ed9f9c4SPeter Tyser 
142bb62a35bSVincent Donnefort 	data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
143bb62a35bSVincent Donnefort 			 ichx_priv.gpio_base);
1446ed9f9c4SPeter Tyser 
145e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
146e6540f33SVincent Donnefort 		data = ichx_priv.outlvl_cache[reg_nr] | data;
147e6540f33SVincent Donnefort 
1486ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1496ed9f9c4SPeter Tyser 
1507a8fd1f5SLinus Walleij 	return !!(data & BIT(bit));
1516ed9f9c4SPeter Tyser }
1526ed9f9c4SPeter Tyser 
ichx_gpio_check_available(struct gpio_chip * gpio,unsigned int nr)153ae84f15cSAbanoub Sameh static bool ichx_gpio_check_available(struct gpio_chip *gpio, unsigned int nr)
1544f600adaSJean Delvare {
1557a8fd1f5SLinus Walleij 	return !!(ichx_priv.use_gpio & BIT(nr / 32));
1564f600adaSJean Delvare }
1574f600adaSJean Delvare 
ichx_gpio_get_direction(struct gpio_chip * gpio,unsigned int nr)158ae84f15cSAbanoub Sameh static int ichx_gpio_get_direction(struct gpio_chip *gpio, unsigned int nr)
15962e08f25SAaron Sierra {
160e42615ecSMatti Vaittinen 	if (ichx_read_bit(GPIO_IO_SEL, nr))
161e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
162e42615ecSMatti Vaittinen 
163e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
16462e08f25SAaron Sierra }
16562e08f25SAaron Sierra 
ichx_gpio_direction_input(struct gpio_chip * gpio,unsigned int nr)166ae84f15cSAbanoub Sameh static int ichx_gpio_direction_input(struct gpio_chip *gpio, unsigned int nr)
1676ed9f9c4SPeter Tyser {
1686ed9f9c4SPeter Tyser 	/*
1696ed9f9c4SPeter Tyser 	 * Try setting pin as an input and verify it worked since many pins
1706ed9f9c4SPeter Tyser 	 * are output-only.
1716ed9f9c4SPeter Tyser 	 */
172c5aaa316SAndy Shevchenko 	return ichx_write_bit(GPIO_IO_SEL, nr, 1, 1);
1736ed9f9c4SPeter Tyser }
1746ed9f9c4SPeter Tyser 
ichx_gpio_direction_output(struct gpio_chip * gpio,unsigned int nr,int val)175ae84f15cSAbanoub Sameh static int ichx_gpio_direction_output(struct gpio_chip *gpio, unsigned int nr,
1766ed9f9c4SPeter Tyser 					int val)
1776ed9f9c4SPeter Tyser {
1787f6569f5SVincent Donnefort 	/* Disable blink hardware which is available for GPIOs from 0 to 31. */
179ba7f74feSVincent Donnefort 	if (nr < 32 && ichx_priv.desc->have_blink)
1807f6569f5SVincent Donnefort 		ichx_write_bit(GPO_BLINK, nr, 0, 0);
1817f6569f5SVincent Donnefort 
1826ed9f9c4SPeter Tyser 	/* Set GPIO output value. */
1836ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
1846ed9f9c4SPeter Tyser 
1856ed9f9c4SPeter Tyser 	/*
1866ed9f9c4SPeter Tyser 	 * Try setting pin as an output and verify it worked since many pins
1876ed9f9c4SPeter Tyser 	 * are input-only.
1886ed9f9c4SPeter Tyser 	 */
189c5aaa316SAndy Shevchenko 	return ichx_write_bit(GPIO_IO_SEL, nr, 0, 1);
1906ed9f9c4SPeter Tyser }
1916ed9f9c4SPeter Tyser 
ichx_gpio_get(struct gpio_chip * chip,unsigned int nr)192ae84f15cSAbanoub Sameh static int ichx_gpio_get(struct gpio_chip *chip, unsigned int nr)
1936ed9f9c4SPeter Tyser {
1946ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_LVL, nr);
1956ed9f9c4SPeter Tyser }
1966ed9f9c4SPeter Tyser 
ich6_gpio_get(struct gpio_chip * chip,unsigned int nr)197ae84f15cSAbanoub Sameh static int ich6_gpio_get(struct gpio_chip *chip, unsigned int nr)
1986ed9f9c4SPeter Tyser {
1996ed9f9c4SPeter Tyser 	unsigned long flags;
2006ed9f9c4SPeter Tyser 	u32 data;
2016ed9f9c4SPeter Tyser 
2026ed9f9c4SPeter Tyser 	/*
2036ed9f9c4SPeter Tyser 	 * GPI 0 - 15 need to be read from the power management registers on
2046ed9f9c4SPeter Tyser 	 * a ICH6/3100 bridge.
2056ed9f9c4SPeter Tyser 	 */
2066ed9f9c4SPeter Tyser 	if (nr < 16) {
2076ed9f9c4SPeter Tyser 		if (!ichx_priv.pm_base)
2086ed9f9c4SPeter Tyser 			return -ENXIO;
2096ed9f9c4SPeter Tyser 
2106ed9f9c4SPeter Tyser 		spin_lock_irqsave(&ichx_priv.lock, flags);
2116ed9f9c4SPeter Tyser 
2126ed9f9c4SPeter Tyser 		/* GPI 0 - 15 are latched, write 1 to clear*/
2137a8fd1f5SLinus Walleij 		ICHX_WRITE(BIT(16 + nr), 0, ichx_priv.pm_base);
2146ed9f9c4SPeter Tyser 		data = ICHX_READ(0, ichx_priv.pm_base);
2156ed9f9c4SPeter Tyser 
2166ed9f9c4SPeter Tyser 		spin_unlock_irqrestore(&ichx_priv.lock, flags);
2176ed9f9c4SPeter Tyser 
2187a8fd1f5SLinus Walleij 		return !!((data >> 16) & BIT(nr));
2196ed9f9c4SPeter Tyser 	} else {
2206ed9f9c4SPeter Tyser 		return ichx_gpio_get(chip, nr);
2216ed9f9c4SPeter Tyser 	}
2226ed9f9c4SPeter Tyser }
2236ed9f9c4SPeter Tyser 
ichx_gpio_request(struct gpio_chip * chip,unsigned int nr)224ae84f15cSAbanoub Sameh static int ichx_gpio_request(struct gpio_chip *chip, unsigned int nr)
2256ed9f9c4SPeter Tyser {
22625f27db4SJean Delvare 	if (!ichx_gpio_check_available(chip, nr))
22725f27db4SJean Delvare 		return -ENXIO;
22825f27db4SJean Delvare 
2296ed9f9c4SPeter Tyser 	/*
2306ed9f9c4SPeter Tyser 	 * Note we assume the BIOS properly set a bridge's USE value.  Some
2316ed9f9c4SPeter Tyser 	 * chips (eg Intel 3100) have bogus USE values though, so first see if
2326ed9f9c4SPeter Tyser 	 * the chipset's USE value can be trusted for this specific bit.
2336ed9f9c4SPeter Tyser 	 * If it can't be trusted, assume that the pin can be used as a GPIO.
2346ed9f9c4SPeter Tyser 	 */
2357a8fd1f5SLinus Walleij 	if (ichx_priv.desc->use_sel_ignore[nr / 32] & BIT(nr & 0x1f))
2362ab3a749SJean Delvare 		return 0;
2376ed9f9c4SPeter Tyser 
2386ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_USE_SEL, nr) ? 0 : -ENODEV;
2396ed9f9c4SPeter Tyser }
2406ed9f9c4SPeter Tyser 
ich6_gpio_request(struct gpio_chip * chip,unsigned int nr)241ae84f15cSAbanoub Sameh static int ich6_gpio_request(struct gpio_chip *chip, unsigned int nr)
2426ed9f9c4SPeter Tyser {
2436ed9f9c4SPeter Tyser 	/*
2446ed9f9c4SPeter Tyser 	 * Fixups for bits 16 and 17 are necessary on the Intel ICH6/3100
2456ed9f9c4SPeter Tyser 	 * bridge as they are controlled by USE register bits 0 and 1.  See
2466ed9f9c4SPeter Tyser 	 * "Table 704 GPIO_USE_SEL1 register" in the i3100 datasheet for
2476ed9f9c4SPeter Tyser 	 * additional info.
2486ed9f9c4SPeter Tyser 	 */
2496ed9f9c4SPeter Tyser 	if (nr == 16 || nr == 17)
2506ed9f9c4SPeter Tyser 		nr -= 16;
2516ed9f9c4SPeter Tyser 
2526ed9f9c4SPeter Tyser 	return ichx_gpio_request(chip, nr);
2536ed9f9c4SPeter Tyser }
2546ed9f9c4SPeter Tyser 
ichx_gpio_set(struct gpio_chip * chip,unsigned int nr,int val)255ae84f15cSAbanoub Sameh static void ichx_gpio_set(struct gpio_chip *chip, unsigned int nr, int val)
2566ed9f9c4SPeter Tyser {
2576ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
2586ed9f9c4SPeter Tyser }
2596ed9f9c4SPeter Tyser 
ichx_gpiolib_setup(struct gpio_chip * chip)2603836309dSBill Pemberton static void ichx_gpiolib_setup(struct gpio_chip *chip)
2616ed9f9c4SPeter Tyser {
2626ed9f9c4SPeter Tyser 	chip->owner = THIS_MODULE;
2636ed9f9c4SPeter Tyser 	chip->label = DRV_NAME;
264ff4709b4SAndy Shevchenko 	chip->parent = ichx_priv.dev;
2656ed9f9c4SPeter Tyser 
2666ed9f9c4SPeter Tyser 	/* Allow chip-specific overrides of request()/get() */
2676ed9f9c4SPeter Tyser 	chip->request = ichx_priv.desc->request ?
2686ed9f9c4SPeter Tyser 		ichx_priv.desc->request : ichx_gpio_request;
2696ed9f9c4SPeter Tyser 	chip->get = ichx_priv.desc->get ?
2706ed9f9c4SPeter Tyser 		ichx_priv.desc->get : ichx_gpio_get;
2716ed9f9c4SPeter Tyser 
2726ed9f9c4SPeter Tyser 	chip->set = ichx_gpio_set;
27362e08f25SAaron Sierra 	chip->get_direction = ichx_gpio_get_direction;
2746ed9f9c4SPeter Tyser 	chip->direction_input = ichx_gpio_direction_input;
2756ed9f9c4SPeter Tyser 	chip->direction_output = ichx_gpio_direction_output;
2766ed9f9c4SPeter Tyser 	chip->base = modparam_gpiobase;
2776ed9f9c4SPeter Tyser 	chip->ngpio = ichx_priv.desc->ngpio;
2789fb1f39eSLinus Walleij 	chip->can_sleep = false;
2796ed9f9c4SPeter Tyser 	chip->dbg_show = NULL;
2806ed9f9c4SPeter Tyser }
2816ed9f9c4SPeter Tyser 
2826ed9f9c4SPeter Tyser /* ICH6-based, 631xesb-based */
2836ed9f9c4SPeter Tyser static struct ichx_desc ich6_desc = {
2846ed9f9c4SPeter Tyser 	/* Bridges using the ICH6 controller need fixups for GPIO 0 - 17 */
2856ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
2866ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
2876ed9f9c4SPeter Tyser 
2886ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
2896ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
2906ed9f9c4SPeter Tyser 
2916ed9f9c4SPeter Tyser 	.ngpio = 50,
292ba7f74feSVincent Donnefort 	.have_blink = true,
293a7008ee1SVincent Donnefort 	.regs = ichx_regs,
294a7008ee1SVincent Donnefort 	.reglen = ichx_reglen,
2956ed9f9c4SPeter Tyser };
2966ed9f9c4SPeter Tyser 
2976ed9f9c4SPeter Tyser /* Intel 3100 */
2986ed9f9c4SPeter Tyser static struct ichx_desc i3100_desc = {
2996ed9f9c4SPeter Tyser 	/*
3006ed9f9c4SPeter Tyser 	 * Bits 16,17, 20 of USE_SEL and bit 16 of USE_SEL2 always read 0 on
3016ed9f9c4SPeter Tyser 	 * the Intel 3100.  See "Table 712. GPIO Summary Table" of 3100
3026ed9f9c4SPeter Tyser 	 * Datasheet for more info.
3036ed9f9c4SPeter Tyser 	 */
3046ed9f9c4SPeter Tyser 	.use_sel_ignore = {0x00130000, 0x00010000, 0x0},
3056ed9f9c4SPeter Tyser 
3066ed9f9c4SPeter Tyser 	/* The 3100 needs fixups for GPIO 0 - 17 */
3076ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
3086ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
3096ed9f9c4SPeter Tyser 
3106ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
3116ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
3126ed9f9c4SPeter Tyser 
3136ed9f9c4SPeter Tyser 	.ngpio = 50,
314a7008ee1SVincent Donnefort 	.regs = ichx_regs,
315a7008ee1SVincent Donnefort 	.reglen = ichx_reglen,
3166ed9f9c4SPeter Tyser };
3176ed9f9c4SPeter Tyser 
3186ed9f9c4SPeter Tyser /* ICH7 and ICH8-based */
3196ed9f9c4SPeter Tyser static struct ichx_desc ich7_desc = {
3206ed9f9c4SPeter Tyser 	.ngpio = 50,
321ba7f74feSVincent Donnefort 	.have_blink = true,
322bb62a35bSVincent Donnefort 	.regs = ichx_regs,
323bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3246ed9f9c4SPeter Tyser };
3256ed9f9c4SPeter Tyser 
3266ed9f9c4SPeter Tyser /* ICH9-based */
3276ed9f9c4SPeter Tyser static struct ichx_desc ich9_desc = {
3286ed9f9c4SPeter Tyser 	.ngpio = 61,
329ba7f74feSVincent Donnefort 	.have_blink = true,
330bb62a35bSVincent Donnefort 	.regs = ichx_regs,
331bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3326ed9f9c4SPeter Tyser };
3336ed9f9c4SPeter Tyser 
3346ed9f9c4SPeter Tyser /* ICH10-based - Consumer/corporate versions have different amount of GPIO */
3356ed9f9c4SPeter Tyser static struct ichx_desc ich10_cons_desc = {
3366ed9f9c4SPeter Tyser 	.ngpio = 61,
337ba7f74feSVincent Donnefort 	.have_blink = true,
338bb62a35bSVincent Donnefort 	.regs = ichx_regs,
339bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3406ed9f9c4SPeter Tyser };
3416ed9f9c4SPeter Tyser static struct ichx_desc ich10_corp_desc = {
3426ed9f9c4SPeter Tyser 	.ngpio = 72,
343ba7f74feSVincent Donnefort 	.have_blink = true,
344bb62a35bSVincent Donnefort 	.regs = ichx_regs,
345bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3466ed9f9c4SPeter Tyser };
3476ed9f9c4SPeter Tyser 
3486ed9f9c4SPeter Tyser /* Intel 5 series, 6 series, 3400 series, and C200 series */
3496ed9f9c4SPeter Tyser static struct ichx_desc intel5_desc = {
3506ed9f9c4SPeter Tyser 	.ngpio = 76,
351bb62a35bSVincent Donnefort 	.regs = ichx_regs,
352bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3536ed9f9c4SPeter Tyser };
3546ed9f9c4SPeter Tyser 
3553b923189SVincent Donnefort /* Avoton */
3563b923189SVincent Donnefort static struct ichx_desc avoton_desc = {
3573b923189SVincent Donnefort 	/* Avoton has only 59 GPIOs, but we assume the first set of register
3583b923189SVincent Donnefort 	 * (Core) has 32 instead of 31 to keep gpio-ich compliance
3593b923189SVincent Donnefort 	 */
3603b923189SVincent Donnefort 	.ngpio = 60,
3613b923189SVincent Donnefort 	.regs = avoton_regs,
3623b923189SVincent Donnefort 	.reglen = avoton_reglen,
3633b923189SVincent Donnefort 	.use_outlvl_cache = true,
3643b923189SVincent Donnefort };
3653b923189SVincent Donnefort 
ichx_gpio_request_regions(struct device * dev,struct resource * res_base,const char * name,u8 use_gpio)3668a06b08eSWilliam Breathitt Gray static int ichx_gpio_request_regions(struct device *dev,
3678a06b08eSWilliam Breathitt Gray 	struct resource *res_base, const char *name, u8 use_gpio)
3684f600adaSJean Delvare {
3694f600adaSJean Delvare 	int i;
3704f600adaSJean Delvare 
3714f600adaSJean Delvare 	if (!res_base || !res_base->start || !res_base->end)
3724f600adaSJean Delvare 		return -ENODEV;
3734f600adaSJean Delvare 
374bb62a35bSVincent Donnefort 	for (i = 0; i < ARRAY_SIZE(ichx_priv.desc->regs[0]); i++) {
3757a8fd1f5SLinus Walleij 		if (!(use_gpio & BIT(i)))
3764f600adaSJean Delvare 			continue;
3778a06b08eSWilliam Breathitt Gray 		if (!devm_request_region(dev,
378bb62a35bSVincent Donnefort 				res_base->start + ichx_priv.desc->regs[0][i],
379bb62a35bSVincent Donnefort 				ichx_priv.desc->reglen[i], name))
3804f600adaSJean Delvare 			return -EBUSY;
3814f600adaSJean Delvare 	}
3828a06b08eSWilliam Breathitt Gray 	return 0;
3834f600adaSJean Delvare }
3844f600adaSJean Delvare 
ichx_gpio_probe(struct platform_device * pdev)3853836309dSBill Pemberton static int ichx_gpio_probe(struct platform_device *pdev)
3866ed9f9c4SPeter Tyser {
387ff4709b4SAndy Shevchenko 	struct device *dev = &pdev->dev;
388ff4709b4SAndy Shevchenko 	struct lpc_ich_info *ich_info = dev_get_platdata(dev);
3896ed9f9c4SPeter Tyser 	struct resource *res_base, *res_pm;
3906ed9f9c4SPeter Tyser 	int err;
3916ed9f9c4SPeter Tyser 
3926ed9f9c4SPeter Tyser 	if (!ich_info)
3936ed9f9c4SPeter Tyser 		return -ENODEV;
3946ed9f9c4SPeter Tyser 
3956ed9f9c4SPeter Tyser 	switch (ich_info->gpio_version) {
3966ed9f9c4SPeter Tyser 	case ICH_I3100_GPIO:
3976ed9f9c4SPeter Tyser 		ichx_priv.desc = &i3100_desc;
3986ed9f9c4SPeter Tyser 		break;
3996ed9f9c4SPeter Tyser 	case ICH_V5_GPIO:
4006ed9f9c4SPeter Tyser 		ichx_priv.desc = &intel5_desc;
4016ed9f9c4SPeter Tyser 		break;
4026ed9f9c4SPeter Tyser 	case ICH_V6_GPIO:
4036ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich6_desc;
4046ed9f9c4SPeter Tyser 		break;
4056ed9f9c4SPeter Tyser 	case ICH_V7_GPIO:
4066ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich7_desc;
4076ed9f9c4SPeter Tyser 		break;
4086ed9f9c4SPeter Tyser 	case ICH_V9_GPIO:
4096ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich9_desc;
4106ed9f9c4SPeter Tyser 		break;
4116ed9f9c4SPeter Tyser 	case ICH_V10CORP_GPIO:
4126ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_corp_desc;
4136ed9f9c4SPeter Tyser 		break;
4146ed9f9c4SPeter Tyser 	case ICH_V10CONS_GPIO:
4156ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_cons_desc;
4166ed9f9c4SPeter Tyser 		break;
4173b923189SVincent Donnefort 	case AVOTON_GPIO:
4183b923189SVincent Donnefort 		ichx_priv.desc = &avoton_desc;
4193b923189SVincent Donnefort 		break;
4206ed9f9c4SPeter Tyser 	default:
4216ed9f9c4SPeter Tyser 		return -ENODEV;
4226ed9f9c4SPeter Tyser 	}
4236ed9f9c4SPeter Tyser 
424ff4709b4SAndy Shevchenko 	ichx_priv.dev = dev;
425d39a948fSJean Delvare 	spin_lock_init(&ichx_priv.lock);
426ff4709b4SAndy Shevchenko 
4276ed9f9c4SPeter Tyser 	res_base = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPIO);
428ff4709b4SAndy Shevchenko 	err = ichx_gpio_request_regions(dev, res_base, pdev->name,
429ff4709b4SAndy Shevchenko 					ich_info->use_gpio);
4304f600adaSJean Delvare 	if (err)
4314f600adaSJean Delvare 		return err;
4326ed9f9c4SPeter Tyser 
4336ed9f9c4SPeter Tyser 	ichx_priv.gpio_base = res_base;
434ff4709b4SAndy Shevchenko 	ichx_priv.use_gpio = ich_info->use_gpio;
4356ed9f9c4SPeter Tyser 
4366ed9f9c4SPeter Tyser 	/*
4376ed9f9c4SPeter Tyser 	 * If necessary, determine the I/O address of ACPI/power management
4385f6f2b9fSAndy Shevchenko 	 * registers which are needed to read the GPE0 register for GPI pins
4396ed9f9c4SPeter Tyser 	 * 0 - 15 on some chipsets.
4406ed9f9c4SPeter Tyser 	 */
4416ed9f9c4SPeter Tyser 	if (!ichx_priv.desc->uses_gpe0)
4426ed9f9c4SPeter Tyser 		goto init;
4436ed9f9c4SPeter Tyser 
4446ed9f9c4SPeter Tyser 	res_pm = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPE0);
4456ed9f9c4SPeter Tyser 	if (!res_pm) {
446c086bea5SAndy Shevchenko 		dev_warn(dev, "ACPI BAR is unavailable, GPI 0 - 15 unavailable\n");
4476ed9f9c4SPeter Tyser 		goto init;
4486ed9f9c4SPeter Tyser 	}
4496ed9f9c4SPeter Tyser 
450ff4709b4SAndy Shevchenko 	if (!devm_request_region(dev, res_pm->start, resource_size(res_pm),
451ff4709b4SAndy Shevchenko 				 pdev->name)) {
452c086bea5SAndy Shevchenko 		dev_warn(dev, "ACPI BAR is busy, GPI 0 - 15 unavailable\n");
4536ed9f9c4SPeter Tyser 		goto init;
4546ed9f9c4SPeter Tyser 	}
4556ed9f9c4SPeter Tyser 
4566ed9f9c4SPeter Tyser 	ichx_priv.pm_base = res_pm;
4576ed9f9c4SPeter Tyser 
4586ed9f9c4SPeter Tyser init:
4596ed9f9c4SPeter Tyser 	ichx_gpiolib_setup(&ichx_priv.chip);
460*56b16a9aSAndrew Davis 	err = devm_gpiochip_add_data(dev, &ichx_priv.chip, NULL);
4616ed9f9c4SPeter Tyser 	if (err) {
462c086bea5SAndy Shevchenko 		dev_err(dev, "Failed to register GPIOs\n");
4638a06b08eSWilliam Breathitt Gray 		return err;
4646ed9f9c4SPeter Tyser 	}
4656ed9f9c4SPeter Tyser 
466c086bea5SAndy Shevchenko 	dev_info(dev, "GPIO from %d to %d\n", ichx_priv.chip.base,
467c086bea5SAndy Shevchenko 		 ichx_priv.chip.base + ichx_priv.chip.ngpio - 1);
4686ed9f9c4SPeter Tyser 
4696ed9f9c4SPeter Tyser 	return 0;
4706ed9f9c4SPeter Tyser }
4716ed9f9c4SPeter Tyser 
4726ed9f9c4SPeter Tyser static struct platform_driver ichx_gpio_driver = {
4736ed9f9c4SPeter Tyser 	.driver		= {
4746ed9f9c4SPeter Tyser 		.name	= DRV_NAME,
4756ed9f9c4SPeter Tyser 	},
4766ed9f9c4SPeter Tyser 	.probe		= ichx_gpio_probe,
4776ed9f9c4SPeter Tyser };
4786ed9f9c4SPeter Tyser 
4796ed9f9c4SPeter Tyser module_platform_driver(ichx_gpio_driver);
4806ed9f9c4SPeter Tyser 
4816ed9f9c4SPeter Tyser MODULE_AUTHOR("Peter Tyser <ptyser@xes-inc.com>");
4826ed9f9c4SPeter Tyser MODULE_DESCRIPTION("GPIO interface for Intel ICH series");
4836ed9f9c4SPeter Tyser MODULE_LICENSE("GPL");
4846ed9f9c4SPeter Tyser MODULE_ALIAS("platform:"DRV_NAME);
485