xref: /openbmc/linux/drivers/gpio/gpio-ich.c (revision 4f600ada70beeb1dfe08e11e871bf31015aa0a3d)
16ed9f9c4SPeter Tyser /*
26ed9f9c4SPeter Tyser  * Intel ICH6-10, Series 5 and 6 GPIO driver
36ed9f9c4SPeter Tyser  *
46ed9f9c4SPeter Tyser  * Copyright (C) 2010 Extreme Engineering Solutions.
56ed9f9c4SPeter Tyser  *
66ed9f9c4SPeter Tyser  * This program is free software; you can redistribute it and/or modify
76ed9f9c4SPeter Tyser  * it under the terms of the GNU General Public License as published by
86ed9f9c4SPeter Tyser  * the Free Software Foundation; either version 2 of the License, or
96ed9f9c4SPeter Tyser  * (at your option) any later version.
106ed9f9c4SPeter Tyser  *
116ed9f9c4SPeter Tyser  * This program is distributed in the hope that it will be useful,
126ed9f9c4SPeter Tyser  * but WITHOUT ANY WARRANTY; without even the implied warranty of
136ed9f9c4SPeter Tyser  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
146ed9f9c4SPeter Tyser  * GNU General Public License for more details.
156ed9f9c4SPeter Tyser  *
166ed9f9c4SPeter Tyser  * You should have received a copy of the GNU General Public License
176ed9f9c4SPeter Tyser  * along with this program; if not, write to the Free Software
186ed9f9c4SPeter Tyser  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
196ed9f9c4SPeter Tyser  */
206ed9f9c4SPeter Tyser 
216ed9f9c4SPeter Tyser #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
226ed9f9c4SPeter Tyser 
236ed9f9c4SPeter Tyser #include <linux/module.h>
246ed9f9c4SPeter Tyser #include <linux/pci.h>
256ed9f9c4SPeter Tyser #include <linux/gpio.h>
266ed9f9c4SPeter Tyser #include <linux/platform_device.h>
276ed9f9c4SPeter Tyser #include <linux/mfd/lpc_ich.h>
286ed9f9c4SPeter Tyser 
296ed9f9c4SPeter Tyser #define DRV_NAME "gpio_ich"
306ed9f9c4SPeter Tyser 
316ed9f9c4SPeter Tyser /*
326ed9f9c4SPeter Tyser  * GPIO register offsets in GPIO I/O space.
336ed9f9c4SPeter Tyser  * Each chunk of 32 GPIOs is manipulated via its own USE_SELx, IO_SELx, and
346ed9f9c4SPeter Tyser  * LVLx registers.  Logic in the read/write functions takes a register and
356ed9f9c4SPeter Tyser  * an absolute bit number and determines the proper register offset and bit
366ed9f9c4SPeter Tyser  * number in that register.  For example, to read the value of GPIO bit 50
376ed9f9c4SPeter Tyser  * the code would access offset ichx_regs[2(=GPIO_LVL)][1(=50/32)],
386ed9f9c4SPeter Tyser  * bit 18 (50%32).
396ed9f9c4SPeter Tyser  */
406ed9f9c4SPeter Tyser enum GPIO_REG {
416ed9f9c4SPeter Tyser 	GPIO_USE_SEL = 0,
426ed9f9c4SPeter Tyser 	GPIO_IO_SEL,
436ed9f9c4SPeter Tyser 	GPIO_LVL,
446ed9f9c4SPeter Tyser };
456ed9f9c4SPeter Tyser 
466ed9f9c4SPeter Tyser static const u8 ichx_regs[3][3] = {
476ed9f9c4SPeter Tyser 	{0x00, 0x30, 0x40},	/* USE_SEL[1-3] offsets */
486ed9f9c4SPeter Tyser 	{0x04, 0x34, 0x44},	/* IO_SEL[1-3] offsets */
496ed9f9c4SPeter Tyser 	{0x0c, 0x38, 0x48},	/* LVL[1-3] offsets */
506ed9f9c4SPeter Tyser };
516ed9f9c4SPeter Tyser 
52*4f600adaSJean Delvare static const u8 ichx_reglen[3] = {
53*4f600adaSJean Delvare 	0x30, 0x10, 0x10,
54*4f600adaSJean Delvare };
55*4f600adaSJean Delvare 
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 
636ed9f9c4SPeter Tyser 	/* Whether the chipset has GPIO in GPE0_STS in the PM IO region */
646ed9f9c4SPeter Tyser 	bool uses_gpe0;
656ed9f9c4SPeter Tyser 
666ed9f9c4SPeter Tyser 	/* USE_SEL is bogus on some chipsets, eg 3100 */
676ed9f9c4SPeter Tyser 	u32 use_sel_ignore[3];
686ed9f9c4SPeter Tyser 
696ed9f9c4SPeter Tyser 	/* Some chipsets have quirks, let these use their own request/get */
706ed9f9c4SPeter Tyser 	int (*request)(struct gpio_chip *chip, unsigned offset);
716ed9f9c4SPeter Tyser 	int (*get)(struct gpio_chip *chip, unsigned offset);
726ed9f9c4SPeter Tyser };
736ed9f9c4SPeter Tyser 
746ed9f9c4SPeter Tyser static struct {
756ed9f9c4SPeter Tyser 	spinlock_t lock;
766ed9f9c4SPeter Tyser 	struct platform_device *dev;
776ed9f9c4SPeter Tyser 	struct gpio_chip chip;
786ed9f9c4SPeter Tyser 	struct resource *gpio_base;	/* GPIO IO base */
796ed9f9c4SPeter Tyser 	struct resource *pm_base;	/* Power Mangagment IO base */
806ed9f9c4SPeter Tyser 	struct ichx_desc *desc;	/* Pointer to chipset-specific description */
816ed9f9c4SPeter Tyser 	u32 orig_gpio_ctrl;	/* Orig CTRL value, used to restore on exit */
82*4f600adaSJean Delvare 	u8 use_gpio;		/* Which GPIO groups are usable */
836ed9f9c4SPeter Tyser } ichx_priv;
846ed9f9c4SPeter Tyser 
856ed9f9c4SPeter Tyser static int modparam_gpiobase = -1;	/* dynamic */
866ed9f9c4SPeter Tyser module_param_named(gpiobase, modparam_gpiobase, int, 0444);
876ed9f9c4SPeter Tyser MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, "
886ed9f9c4SPeter Tyser 			   "which is the default.");
896ed9f9c4SPeter Tyser 
906ed9f9c4SPeter Tyser static int ichx_write_bit(int reg, unsigned nr, int val, int verify)
916ed9f9c4SPeter Tyser {
926ed9f9c4SPeter Tyser 	unsigned long flags;
936ed9f9c4SPeter Tyser 	u32 data, tmp;
946ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
956ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
966ed9f9c4SPeter Tyser 	int ret = 0;
976ed9f9c4SPeter Tyser 
986ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
996ed9f9c4SPeter Tyser 
1006ed9f9c4SPeter Tyser 	data = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
1016ed9f9c4SPeter Tyser 	if (val)
1026ed9f9c4SPeter Tyser 		data |= 1 << bit;
1036ed9f9c4SPeter Tyser 	else
1046ed9f9c4SPeter Tyser 		data &= ~(1 << bit);
1056ed9f9c4SPeter Tyser 	ICHX_WRITE(data, ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
1066ed9f9c4SPeter Tyser 	tmp = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
1076ed9f9c4SPeter Tyser 	if (verify && data != tmp)
1086ed9f9c4SPeter Tyser 		ret = -EPERM;
1096ed9f9c4SPeter Tyser 
1106ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1116ed9f9c4SPeter Tyser 
1126ed9f9c4SPeter Tyser 	return ret;
1136ed9f9c4SPeter Tyser }
1146ed9f9c4SPeter Tyser 
1156ed9f9c4SPeter Tyser static int ichx_read_bit(int reg, unsigned nr)
1166ed9f9c4SPeter Tyser {
1176ed9f9c4SPeter Tyser 	unsigned long flags;
1186ed9f9c4SPeter Tyser 	u32 data;
1196ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
1206ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
1216ed9f9c4SPeter Tyser 
1226ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
1236ed9f9c4SPeter Tyser 
1246ed9f9c4SPeter Tyser 	data = ICHX_READ(ichx_regs[reg][reg_nr], ichx_priv.gpio_base);
1256ed9f9c4SPeter Tyser 
1266ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1276ed9f9c4SPeter Tyser 
1286ed9f9c4SPeter Tyser 	return data & (1 << bit) ? 1 : 0;
1296ed9f9c4SPeter Tyser }
1306ed9f9c4SPeter Tyser 
131*4f600adaSJean Delvare static int ichx_gpio_check_available(struct gpio_chip *gpio, unsigned nr)
132*4f600adaSJean Delvare {
133*4f600adaSJean Delvare 	return (ichx_priv.use_gpio & (1 << (nr / 32))) ? 0 : -ENXIO;
134*4f600adaSJean Delvare }
135*4f600adaSJean Delvare 
1366ed9f9c4SPeter Tyser static int ichx_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
1376ed9f9c4SPeter Tyser {
138*4f600adaSJean Delvare 	if (!ichx_gpio_check_available(gpio, nr))
139*4f600adaSJean Delvare 		return -ENXIO;
140*4f600adaSJean Delvare 
1416ed9f9c4SPeter Tyser 	/*
1426ed9f9c4SPeter Tyser 	 * Try setting pin as an input and verify it worked since many pins
1436ed9f9c4SPeter Tyser 	 * are output-only.
1446ed9f9c4SPeter Tyser 	 */
1456ed9f9c4SPeter Tyser 	if (ichx_write_bit(GPIO_IO_SEL, nr, 1, 1))
1466ed9f9c4SPeter Tyser 		return -EINVAL;
1476ed9f9c4SPeter Tyser 
1486ed9f9c4SPeter Tyser 	return 0;
1496ed9f9c4SPeter Tyser }
1506ed9f9c4SPeter Tyser 
1516ed9f9c4SPeter Tyser static int ichx_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
1526ed9f9c4SPeter Tyser 					int val)
1536ed9f9c4SPeter Tyser {
154*4f600adaSJean Delvare 	if (!ichx_gpio_check_available(gpio, nr))
155*4f600adaSJean Delvare 		return -ENXIO;
156*4f600adaSJean Delvare 
1576ed9f9c4SPeter Tyser 	/* Set GPIO output value. */
1586ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
1596ed9f9c4SPeter Tyser 
1606ed9f9c4SPeter Tyser 	/*
1616ed9f9c4SPeter Tyser 	 * Try setting pin as an output and verify it worked since many pins
1626ed9f9c4SPeter Tyser 	 * are input-only.
1636ed9f9c4SPeter Tyser 	 */
1646ed9f9c4SPeter Tyser 	if (ichx_write_bit(GPIO_IO_SEL, nr, 0, 1))
1656ed9f9c4SPeter Tyser 		return -EINVAL;
1666ed9f9c4SPeter Tyser 
1676ed9f9c4SPeter Tyser 	return 0;
1686ed9f9c4SPeter Tyser }
1696ed9f9c4SPeter Tyser 
1706ed9f9c4SPeter Tyser static int ichx_gpio_get(struct gpio_chip *chip, unsigned nr)
1716ed9f9c4SPeter Tyser {
172*4f600adaSJean Delvare 	if (!ichx_gpio_check_available(chip, nr))
173*4f600adaSJean Delvare 		return -ENXIO;
174*4f600adaSJean Delvare 
1756ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_LVL, nr);
1766ed9f9c4SPeter Tyser }
1776ed9f9c4SPeter Tyser 
1786ed9f9c4SPeter Tyser static int ich6_gpio_get(struct gpio_chip *chip, unsigned nr)
1796ed9f9c4SPeter Tyser {
1806ed9f9c4SPeter Tyser 	unsigned long flags;
1816ed9f9c4SPeter Tyser 	u32 data;
1826ed9f9c4SPeter Tyser 
183*4f600adaSJean Delvare 	if (!ichx_gpio_check_available(chip, nr))
184*4f600adaSJean Delvare 		return -ENXIO;
185*4f600adaSJean Delvare 
1866ed9f9c4SPeter Tyser 	/*
1876ed9f9c4SPeter Tyser 	 * GPI 0 - 15 need to be read from the power management registers on
1886ed9f9c4SPeter Tyser 	 * a ICH6/3100 bridge.
1896ed9f9c4SPeter Tyser 	 */
1906ed9f9c4SPeter Tyser 	if (nr < 16) {
1916ed9f9c4SPeter Tyser 		if (!ichx_priv.pm_base)
1926ed9f9c4SPeter Tyser 			return -ENXIO;
1936ed9f9c4SPeter Tyser 
1946ed9f9c4SPeter Tyser 		spin_lock_irqsave(&ichx_priv.lock, flags);
1956ed9f9c4SPeter Tyser 
1966ed9f9c4SPeter Tyser 		/* GPI 0 - 15 are latched, write 1 to clear*/
1976ed9f9c4SPeter Tyser 		ICHX_WRITE(1 << (16 + nr), 0, ichx_priv.pm_base);
1986ed9f9c4SPeter Tyser 		data = ICHX_READ(0, ichx_priv.pm_base);
1996ed9f9c4SPeter Tyser 
2006ed9f9c4SPeter Tyser 		spin_unlock_irqrestore(&ichx_priv.lock, flags);
2016ed9f9c4SPeter Tyser 
2026ed9f9c4SPeter Tyser 		return (data >> 16) & (1 << nr) ? 1 : 0;
2036ed9f9c4SPeter Tyser 	} else {
2046ed9f9c4SPeter Tyser 		return ichx_gpio_get(chip, nr);
2056ed9f9c4SPeter Tyser 	}
2066ed9f9c4SPeter Tyser }
2076ed9f9c4SPeter Tyser 
2086ed9f9c4SPeter Tyser static int ichx_gpio_request(struct gpio_chip *chip, unsigned nr)
2096ed9f9c4SPeter Tyser {
2106ed9f9c4SPeter Tyser 	/*
2116ed9f9c4SPeter Tyser 	 * Note we assume the BIOS properly set a bridge's USE value.  Some
2126ed9f9c4SPeter Tyser 	 * chips (eg Intel 3100) have bogus USE values though, so first see if
2136ed9f9c4SPeter Tyser 	 * the chipset's USE value can be trusted for this specific bit.
2146ed9f9c4SPeter Tyser 	 * If it can't be trusted, assume that the pin can be used as a GPIO.
2156ed9f9c4SPeter Tyser 	 */
2166ed9f9c4SPeter Tyser 	if (ichx_priv.desc->use_sel_ignore[nr / 32] & (1 << (nr & 0x1f)))
2176ed9f9c4SPeter Tyser 		return 1;
2186ed9f9c4SPeter Tyser 
2196ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_USE_SEL, nr) ? 0 : -ENODEV;
2206ed9f9c4SPeter Tyser }
2216ed9f9c4SPeter Tyser 
2226ed9f9c4SPeter Tyser static int ich6_gpio_request(struct gpio_chip *chip, unsigned nr)
2236ed9f9c4SPeter Tyser {
2246ed9f9c4SPeter Tyser 	/*
2256ed9f9c4SPeter Tyser 	 * Fixups for bits 16 and 17 are necessary on the Intel ICH6/3100
2266ed9f9c4SPeter Tyser 	 * bridge as they are controlled by USE register bits 0 and 1.  See
2276ed9f9c4SPeter Tyser 	 * "Table 704 GPIO_USE_SEL1 register" in the i3100 datasheet for
2286ed9f9c4SPeter Tyser 	 * additional info.
2296ed9f9c4SPeter Tyser 	 */
2306ed9f9c4SPeter Tyser 	if (nr == 16 || nr == 17)
2316ed9f9c4SPeter Tyser 		nr -= 16;
2326ed9f9c4SPeter Tyser 
2336ed9f9c4SPeter Tyser 	return ichx_gpio_request(chip, nr);
2346ed9f9c4SPeter Tyser }
2356ed9f9c4SPeter Tyser 
2366ed9f9c4SPeter Tyser static void ichx_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
2376ed9f9c4SPeter Tyser {
2386ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
2396ed9f9c4SPeter Tyser }
2406ed9f9c4SPeter Tyser 
2416ed9f9c4SPeter Tyser static void __devinit ichx_gpiolib_setup(struct gpio_chip *chip)
2426ed9f9c4SPeter Tyser {
2436ed9f9c4SPeter Tyser 	chip->owner = THIS_MODULE;
2446ed9f9c4SPeter Tyser 	chip->label = DRV_NAME;
2456ed9f9c4SPeter Tyser 	chip->dev = &ichx_priv.dev->dev;
2466ed9f9c4SPeter Tyser 
2476ed9f9c4SPeter Tyser 	/* Allow chip-specific overrides of request()/get() */
2486ed9f9c4SPeter Tyser 	chip->request = ichx_priv.desc->request ?
2496ed9f9c4SPeter Tyser 		ichx_priv.desc->request : ichx_gpio_request;
2506ed9f9c4SPeter Tyser 	chip->get = ichx_priv.desc->get ?
2516ed9f9c4SPeter Tyser 		ichx_priv.desc->get : ichx_gpio_get;
2526ed9f9c4SPeter Tyser 
2536ed9f9c4SPeter Tyser 	chip->set = ichx_gpio_set;
2546ed9f9c4SPeter Tyser 	chip->direction_input = ichx_gpio_direction_input;
2556ed9f9c4SPeter Tyser 	chip->direction_output = ichx_gpio_direction_output;
2566ed9f9c4SPeter Tyser 	chip->base = modparam_gpiobase;
2576ed9f9c4SPeter Tyser 	chip->ngpio = ichx_priv.desc->ngpio;
2586ed9f9c4SPeter Tyser 	chip->can_sleep = 0;
2596ed9f9c4SPeter Tyser 	chip->dbg_show = NULL;
2606ed9f9c4SPeter Tyser }
2616ed9f9c4SPeter Tyser 
2626ed9f9c4SPeter Tyser /* ICH6-based, 631xesb-based */
2636ed9f9c4SPeter Tyser static struct ichx_desc ich6_desc = {
2646ed9f9c4SPeter Tyser 	/* Bridges using the ICH6 controller need fixups for GPIO 0 - 17 */
2656ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
2666ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
2676ed9f9c4SPeter Tyser 
2686ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
2696ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
2706ed9f9c4SPeter Tyser 
2716ed9f9c4SPeter Tyser 	.ngpio = 50,
2726ed9f9c4SPeter Tyser };
2736ed9f9c4SPeter Tyser 
2746ed9f9c4SPeter Tyser /* Intel 3100 */
2756ed9f9c4SPeter Tyser static struct ichx_desc i3100_desc = {
2766ed9f9c4SPeter Tyser 	/*
2776ed9f9c4SPeter Tyser 	 * Bits 16,17, 20 of USE_SEL and bit 16 of USE_SEL2 always read 0 on
2786ed9f9c4SPeter Tyser 	 * the Intel 3100.  See "Table 712. GPIO Summary Table" of 3100
2796ed9f9c4SPeter Tyser 	 * Datasheet for more info.
2806ed9f9c4SPeter Tyser 	 */
2816ed9f9c4SPeter Tyser 	.use_sel_ignore = {0x00130000, 0x00010000, 0x0},
2826ed9f9c4SPeter Tyser 
2836ed9f9c4SPeter Tyser 	/* The 3100 needs fixups for GPIO 0 - 17 */
2846ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
2856ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
2866ed9f9c4SPeter Tyser 
2876ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
2886ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
2896ed9f9c4SPeter Tyser 
2906ed9f9c4SPeter Tyser 	.ngpio = 50,
2916ed9f9c4SPeter Tyser };
2926ed9f9c4SPeter Tyser 
2936ed9f9c4SPeter Tyser /* ICH7 and ICH8-based */
2946ed9f9c4SPeter Tyser static struct ichx_desc ich7_desc = {
2956ed9f9c4SPeter Tyser 	.ngpio = 50,
2966ed9f9c4SPeter Tyser };
2976ed9f9c4SPeter Tyser 
2986ed9f9c4SPeter Tyser /* ICH9-based */
2996ed9f9c4SPeter Tyser static struct ichx_desc ich9_desc = {
3006ed9f9c4SPeter Tyser 	.ngpio = 61,
3016ed9f9c4SPeter Tyser };
3026ed9f9c4SPeter Tyser 
3036ed9f9c4SPeter Tyser /* ICH10-based - Consumer/corporate versions have different amount of GPIO */
3046ed9f9c4SPeter Tyser static struct ichx_desc ich10_cons_desc = {
3056ed9f9c4SPeter Tyser 	.ngpio = 61,
3066ed9f9c4SPeter Tyser };
3076ed9f9c4SPeter Tyser static struct ichx_desc ich10_corp_desc = {
3086ed9f9c4SPeter Tyser 	.ngpio = 72,
3096ed9f9c4SPeter Tyser };
3106ed9f9c4SPeter Tyser 
3116ed9f9c4SPeter Tyser /* Intel 5 series, 6 series, 3400 series, and C200 series */
3126ed9f9c4SPeter Tyser static struct ichx_desc intel5_desc = {
3136ed9f9c4SPeter Tyser 	.ngpio = 76,
3146ed9f9c4SPeter Tyser };
3156ed9f9c4SPeter Tyser 
316*4f600adaSJean Delvare static int __devinit ichx_gpio_request_regions(struct resource *res_base,
317*4f600adaSJean Delvare 						const char *name, u8 use_gpio)
318*4f600adaSJean Delvare {
319*4f600adaSJean Delvare 	int i;
320*4f600adaSJean Delvare 
321*4f600adaSJean Delvare 	if (!res_base || !res_base->start || !res_base->end)
322*4f600adaSJean Delvare 		return -ENODEV;
323*4f600adaSJean Delvare 
324*4f600adaSJean Delvare 	for (i = 0; i < ARRAY_SIZE(ichx_regs[0]); i++) {
325*4f600adaSJean Delvare 		if (!(use_gpio & (1 << i)))
326*4f600adaSJean Delvare 			continue;
327*4f600adaSJean Delvare 		if (!request_region(res_base->start + ichx_regs[0][i],
328*4f600adaSJean Delvare 				    ichx_reglen[i], name))
329*4f600adaSJean Delvare 			goto request_err;
330*4f600adaSJean Delvare 	}
331*4f600adaSJean Delvare 	return 0;
332*4f600adaSJean Delvare 
333*4f600adaSJean Delvare request_err:
334*4f600adaSJean Delvare 	/* Clean up: release already requested regions, if any */
335*4f600adaSJean Delvare 	for (i--; i >= 0; i--) {
336*4f600adaSJean Delvare 		if (!(use_gpio & (1 << i)))
337*4f600adaSJean Delvare 			continue;
338*4f600adaSJean Delvare 		release_region(res_base->start + ichx_regs[0][i],
339*4f600adaSJean Delvare 			       ichx_reglen[i]);
340*4f600adaSJean Delvare 	}
341*4f600adaSJean Delvare 	return -EBUSY;
342*4f600adaSJean Delvare }
343*4f600adaSJean Delvare 
344*4f600adaSJean Delvare static void ichx_gpio_release_regions(struct resource *res_base, u8 use_gpio)
345*4f600adaSJean Delvare {
346*4f600adaSJean Delvare 	int i;
347*4f600adaSJean Delvare 
348*4f600adaSJean Delvare 	for (i = 0; i < ARRAY_SIZE(ichx_regs[0]); i++) {
349*4f600adaSJean Delvare 		if (!(use_gpio & (1 << i)))
350*4f600adaSJean Delvare 			continue;
351*4f600adaSJean Delvare 		release_region(res_base->start + ichx_regs[0][i],
352*4f600adaSJean Delvare 			       ichx_reglen[i]);
353*4f600adaSJean Delvare 	}
354*4f600adaSJean Delvare }
355*4f600adaSJean Delvare 
3566ed9f9c4SPeter Tyser static int __devinit ichx_gpio_probe(struct platform_device *pdev)
3576ed9f9c4SPeter Tyser {
3586ed9f9c4SPeter Tyser 	struct resource *res_base, *res_pm;
3596ed9f9c4SPeter Tyser 	int err;
3606ed9f9c4SPeter Tyser 	struct lpc_ich_info *ich_info = pdev->dev.platform_data;
3616ed9f9c4SPeter Tyser 
3626ed9f9c4SPeter Tyser 	if (!ich_info)
3636ed9f9c4SPeter Tyser 		return -ENODEV;
3646ed9f9c4SPeter Tyser 
3656ed9f9c4SPeter Tyser 	ichx_priv.dev = pdev;
3666ed9f9c4SPeter Tyser 
3676ed9f9c4SPeter Tyser 	switch (ich_info->gpio_version) {
3686ed9f9c4SPeter Tyser 	case ICH_I3100_GPIO:
3696ed9f9c4SPeter Tyser 		ichx_priv.desc = &i3100_desc;
3706ed9f9c4SPeter Tyser 		break;
3716ed9f9c4SPeter Tyser 	case ICH_V5_GPIO:
3726ed9f9c4SPeter Tyser 		ichx_priv.desc = &intel5_desc;
3736ed9f9c4SPeter Tyser 		break;
3746ed9f9c4SPeter Tyser 	case ICH_V6_GPIO:
3756ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich6_desc;
3766ed9f9c4SPeter Tyser 		break;
3776ed9f9c4SPeter Tyser 	case ICH_V7_GPIO:
3786ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich7_desc;
3796ed9f9c4SPeter Tyser 		break;
3806ed9f9c4SPeter Tyser 	case ICH_V9_GPIO:
3816ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich9_desc;
3826ed9f9c4SPeter Tyser 		break;
3836ed9f9c4SPeter Tyser 	case ICH_V10CORP_GPIO:
3846ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_corp_desc;
3856ed9f9c4SPeter Tyser 		break;
3866ed9f9c4SPeter Tyser 	case ICH_V10CONS_GPIO:
3876ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_cons_desc;
3886ed9f9c4SPeter Tyser 		break;
3896ed9f9c4SPeter Tyser 	default:
3906ed9f9c4SPeter Tyser 		return -ENODEV;
3916ed9f9c4SPeter Tyser 	}
3926ed9f9c4SPeter Tyser 
3936ed9f9c4SPeter Tyser 	res_base = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPIO);
394*4f600adaSJean Delvare 	ichx_priv.use_gpio = ich_info->use_gpio;
395*4f600adaSJean Delvare 	err = ichx_gpio_request_regions(res_base, pdev->name,
396*4f600adaSJean Delvare 					ichx_priv.use_gpio);
397*4f600adaSJean Delvare 	if (err)
398*4f600adaSJean Delvare 		return err;
3996ed9f9c4SPeter Tyser 
4006ed9f9c4SPeter Tyser 	ichx_priv.gpio_base = res_base;
4016ed9f9c4SPeter Tyser 
4026ed9f9c4SPeter Tyser 	/*
4036ed9f9c4SPeter Tyser 	 * If necessary, determine the I/O address of ACPI/power management
4046ed9f9c4SPeter Tyser 	 * registers which are needed to read the the GPE0 register for GPI pins
4056ed9f9c4SPeter Tyser 	 * 0 - 15 on some chipsets.
4066ed9f9c4SPeter Tyser 	 */
4076ed9f9c4SPeter Tyser 	if (!ichx_priv.desc->uses_gpe0)
4086ed9f9c4SPeter Tyser 		goto init;
4096ed9f9c4SPeter Tyser 
4106ed9f9c4SPeter Tyser 	res_pm = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPE0);
4116ed9f9c4SPeter Tyser 	if (!res_pm) {
4126ed9f9c4SPeter Tyser 		pr_warn("ACPI BAR is unavailable, GPI 0 - 15 unavailable\n");
4136ed9f9c4SPeter Tyser 		goto init;
4146ed9f9c4SPeter Tyser 	}
4156ed9f9c4SPeter Tyser 
4166ed9f9c4SPeter Tyser 	if (!request_region(res_pm->start, resource_size(res_pm),
4176ed9f9c4SPeter Tyser 			pdev->name)) {
4186ed9f9c4SPeter Tyser 		pr_warn("ACPI BAR is busy, GPI 0 - 15 unavailable\n");
4196ed9f9c4SPeter Tyser 		goto init;
4206ed9f9c4SPeter Tyser 	}
4216ed9f9c4SPeter Tyser 
4226ed9f9c4SPeter Tyser 	ichx_priv.pm_base = res_pm;
4236ed9f9c4SPeter Tyser 
4246ed9f9c4SPeter Tyser init:
4256ed9f9c4SPeter Tyser 	ichx_gpiolib_setup(&ichx_priv.chip);
4266ed9f9c4SPeter Tyser 	err = gpiochip_add(&ichx_priv.chip);
4276ed9f9c4SPeter Tyser 	if (err) {
4286ed9f9c4SPeter Tyser 		pr_err("Failed to register GPIOs\n");
4296ed9f9c4SPeter Tyser 		goto add_err;
4306ed9f9c4SPeter Tyser 	}
4316ed9f9c4SPeter Tyser 
4326ed9f9c4SPeter Tyser 	pr_info("GPIO from %d to %d on %s\n", ichx_priv.chip.base,
4336ed9f9c4SPeter Tyser 	       ichx_priv.chip.base + ichx_priv.chip.ngpio - 1, DRV_NAME);
4346ed9f9c4SPeter Tyser 
4356ed9f9c4SPeter Tyser 	return 0;
4366ed9f9c4SPeter Tyser 
4376ed9f9c4SPeter Tyser add_err:
438*4f600adaSJean Delvare 	ichx_gpio_release_regions(ichx_priv.gpio_base, ichx_priv.use_gpio);
4396ed9f9c4SPeter Tyser 	if (ichx_priv.pm_base)
4406ed9f9c4SPeter Tyser 		release_region(ichx_priv.pm_base->start,
4416ed9f9c4SPeter Tyser 				resource_size(ichx_priv.pm_base));
4426ed9f9c4SPeter Tyser 	return err;
4436ed9f9c4SPeter Tyser }
4446ed9f9c4SPeter Tyser 
4456ed9f9c4SPeter Tyser static int __devexit ichx_gpio_remove(struct platform_device *pdev)
4466ed9f9c4SPeter Tyser {
4476ed9f9c4SPeter Tyser 	int err;
4486ed9f9c4SPeter Tyser 
4496ed9f9c4SPeter Tyser 	err = gpiochip_remove(&ichx_priv.chip);
4506ed9f9c4SPeter Tyser 	if (err) {
4516ed9f9c4SPeter Tyser 		dev_err(&pdev->dev, "%s failed, %d\n",
4526ed9f9c4SPeter Tyser 				"gpiochip_remove()", err);
4536ed9f9c4SPeter Tyser 		return err;
4546ed9f9c4SPeter Tyser 	}
4556ed9f9c4SPeter Tyser 
456*4f600adaSJean Delvare 	ichx_gpio_release_regions(ichx_priv.gpio_base, ichx_priv.use_gpio);
4576ed9f9c4SPeter Tyser 	if (ichx_priv.pm_base)
4586ed9f9c4SPeter Tyser 		release_region(ichx_priv.pm_base->start,
4596ed9f9c4SPeter Tyser 				resource_size(ichx_priv.pm_base));
4606ed9f9c4SPeter Tyser 
4616ed9f9c4SPeter Tyser 	return 0;
4626ed9f9c4SPeter Tyser }
4636ed9f9c4SPeter Tyser 
4646ed9f9c4SPeter Tyser static struct platform_driver ichx_gpio_driver = {
4656ed9f9c4SPeter Tyser 	.driver		= {
4666ed9f9c4SPeter Tyser 		.owner	= THIS_MODULE,
4676ed9f9c4SPeter Tyser 		.name	= DRV_NAME,
4686ed9f9c4SPeter Tyser 	},
4696ed9f9c4SPeter Tyser 	.probe		= ichx_gpio_probe,
4706ed9f9c4SPeter Tyser 	.remove		= __devexit_p(ichx_gpio_remove),
4716ed9f9c4SPeter Tyser };
4726ed9f9c4SPeter Tyser 
4736ed9f9c4SPeter Tyser module_platform_driver(ichx_gpio_driver);
4746ed9f9c4SPeter Tyser 
4756ed9f9c4SPeter Tyser MODULE_AUTHOR("Peter Tyser <ptyser@xes-inc.com>");
4766ed9f9c4SPeter Tyser MODULE_DESCRIPTION("GPIO interface for Intel ICH series");
4776ed9f9c4SPeter Tyser MODULE_LICENSE("GPL");
4786ed9f9c4SPeter Tyser MODULE_ALIAS("platform:"DRV_NAME);
479