xref: /openbmc/linux/drivers/gpio/gpio-ich.c (revision 3b9231893e5731e2212645a92f1d3d0776c58e1a)
16ed9f9c4SPeter Tyser /*
2*3b923189SVincent Donnefort  * Intel ICH6-10, Series 5 and 6, Atom C2000 (Avoton/Rangeley) 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,
447f6569f5SVincent Donnefort 	GPO_BLINK
456ed9f9c4SPeter Tyser };
466ed9f9c4SPeter Tyser 
477f6569f5SVincent Donnefort static const u8 ichx_regs[4][3] = {
486ed9f9c4SPeter Tyser 	{0x00, 0x30, 0x40},	/* USE_SEL[1-3] offsets */
496ed9f9c4SPeter Tyser 	{0x04, 0x34, 0x44},	/* IO_SEL[1-3] offsets */
506ed9f9c4SPeter Tyser 	{0x0c, 0x38, 0x48},	/* LVL[1-3] offsets */
517f6569f5SVincent Donnefort 	{0x18, 0x18, 0x18},	/* BLINK offset */
526ed9f9c4SPeter Tyser };
536ed9f9c4SPeter Tyser 
544f600adaSJean Delvare static const u8 ichx_reglen[3] = {
554f600adaSJean Delvare 	0x30, 0x10, 0x10,
564f600adaSJean Delvare };
574f600adaSJean Delvare 
58*3b923189SVincent Donnefort static const u8 avoton_regs[4][3] = {
59*3b923189SVincent Donnefort 	{0x00, 0x80, 0x00},
60*3b923189SVincent Donnefort 	{0x04, 0x84, 0x00},
61*3b923189SVincent Donnefort 	{0x08, 0x88, 0x00},
62*3b923189SVincent Donnefort };
63*3b923189SVincent Donnefort 
64*3b923189SVincent Donnefort static const u8 avoton_reglen[3] = {
65*3b923189SVincent Donnefort 	0x10, 0x10, 0x00,
66*3b923189SVincent Donnefort };
67*3b923189SVincent Donnefort 
686ed9f9c4SPeter Tyser #define ICHX_WRITE(val, reg, base_res)	outl(val, (reg) + (base_res)->start)
696ed9f9c4SPeter Tyser #define ICHX_READ(reg, base_res)	inl((reg) + (base_res)->start)
706ed9f9c4SPeter Tyser 
716ed9f9c4SPeter Tyser struct ichx_desc {
726ed9f9c4SPeter Tyser 	/* Max GPIO pins the chipset can have */
736ed9f9c4SPeter Tyser 	uint ngpio;
746ed9f9c4SPeter Tyser 
75bb62a35bSVincent Donnefort 	/* chipset registers */
76bb62a35bSVincent Donnefort 	const u8 (*regs)[3];
77bb62a35bSVincent Donnefort 	const u8 *reglen;
78bb62a35bSVincent Donnefort 
79ba7f74feSVincent Donnefort 	/* GPO_BLINK is available on this chipset */
80ba7f74feSVincent Donnefort 	bool have_blink;
81ba7f74feSVincent Donnefort 
826ed9f9c4SPeter Tyser 	/* Whether the chipset has GPIO in GPE0_STS in the PM IO region */
836ed9f9c4SPeter Tyser 	bool uses_gpe0;
846ed9f9c4SPeter Tyser 
856ed9f9c4SPeter Tyser 	/* USE_SEL is bogus on some chipsets, eg 3100 */
866ed9f9c4SPeter Tyser 	u32 use_sel_ignore[3];
876ed9f9c4SPeter Tyser 
886ed9f9c4SPeter Tyser 	/* Some chipsets have quirks, let these use their own request/get */
896ed9f9c4SPeter Tyser 	int (*request)(struct gpio_chip *chip, unsigned offset);
906ed9f9c4SPeter Tyser 	int (*get)(struct gpio_chip *chip, unsigned offset);
91e6540f33SVincent Donnefort 
92e6540f33SVincent Donnefort 	/*
93e6540f33SVincent Donnefort 	 * Some chipsets don't let reading output values on GPIO_LVL register
94e6540f33SVincent Donnefort 	 * this option allows driver caching written output values
95e6540f33SVincent Donnefort 	 */
96e6540f33SVincent Donnefort 	bool use_outlvl_cache;
976ed9f9c4SPeter Tyser };
986ed9f9c4SPeter Tyser 
996ed9f9c4SPeter Tyser static struct {
1006ed9f9c4SPeter Tyser 	spinlock_t lock;
1016ed9f9c4SPeter Tyser 	struct platform_device *dev;
1026ed9f9c4SPeter Tyser 	struct gpio_chip chip;
1036ed9f9c4SPeter Tyser 	struct resource *gpio_base;	/* GPIO IO base */
1046ed9f9c4SPeter Tyser 	struct resource *pm_base;	/* Power Mangagment IO base */
1056ed9f9c4SPeter Tyser 	struct ichx_desc *desc;	/* Pointer to chipset-specific description */
1066ed9f9c4SPeter Tyser 	u32 orig_gpio_ctrl;	/* Orig CTRL value, used to restore on exit */
1074f600adaSJean Delvare 	u8 use_gpio;		/* Which GPIO groups are usable */
108e6540f33SVincent Donnefort 	int outlvl_cache[3];	/* cached output values */
1096ed9f9c4SPeter Tyser } ichx_priv;
1106ed9f9c4SPeter Tyser 
1116ed9f9c4SPeter Tyser static int modparam_gpiobase = -1;	/* dynamic */
1126ed9f9c4SPeter Tyser module_param_named(gpiobase, modparam_gpiobase, int, 0444);
1136ed9f9c4SPeter Tyser MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, "
1146ed9f9c4SPeter Tyser 			   "which is the default.");
1156ed9f9c4SPeter Tyser 
1166ed9f9c4SPeter Tyser static int ichx_write_bit(int reg, unsigned nr, int val, int verify)
1176ed9f9c4SPeter Tyser {
1186ed9f9c4SPeter Tyser 	unsigned long flags;
1196ed9f9c4SPeter Tyser 	u32 data, tmp;
1206ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
1216ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
1226ed9f9c4SPeter Tyser 	int ret = 0;
1236ed9f9c4SPeter Tyser 
1246ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
1256ed9f9c4SPeter Tyser 
126e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
127e6540f33SVincent Donnefort 		data = ichx_priv.outlvl_cache[reg_nr];
128e6540f33SVincent Donnefort 	else
129bb62a35bSVincent Donnefort 		data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
130bb62a35bSVincent Donnefort 				 ichx_priv.gpio_base);
131e6540f33SVincent Donnefort 
1326ed9f9c4SPeter Tyser 	if (val)
1336ed9f9c4SPeter Tyser 		data |= 1 << bit;
1346ed9f9c4SPeter Tyser 	else
1356ed9f9c4SPeter Tyser 		data &= ~(1 << bit);
136bb62a35bSVincent Donnefort 	ICHX_WRITE(data, ichx_priv.desc->regs[reg][reg_nr],
137bb62a35bSVincent Donnefort 			 ichx_priv.gpio_base);
138e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
139e6540f33SVincent Donnefort 		ichx_priv.outlvl_cache[reg_nr] = data;
140e6540f33SVincent Donnefort 
141bb62a35bSVincent Donnefort 	tmp = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
142bb62a35bSVincent Donnefort 			ichx_priv.gpio_base);
1436ed9f9c4SPeter Tyser 	if (verify && data != tmp)
1446ed9f9c4SPeter Tyser 		ret = -EPERM;
1456ed9f9c4SPeter Tyser 
1466ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1476ed9f9c4SPeter Tyser 
1486ed9f9c4SPeter Tyser 	return ret;
1496ed9f9c4SPeter Tyser }
1506ed9f9c4SPeter Tyser 
1516ed9f9c4SPeter Tyser static int ichx_read_bit(int reg, unsigned nr)
1526ed9f9c4SPeter Tyser {
1536ed9f9c4SPeter Tyser 	unsigned long flags;
1546ed9f9c4SPeter Tyser 	u32 data;
1556ed9f9c4SPeter Tyser 	int reg_nr = nr / 32;
1566ed9f9c4SPeter Tyser 	int bit = nr & 0x1f;
1576ed9f9c4SPeter Tyser 
1586ed9f9c4SPeter Tyser 	spin_lock_irqsave(&ichx_priv.lock, flags);
1596ed9f9c4SPeter Tyser 
160bb62a35bSVincent Donnefort 	data = ICHX_READ(ichx_priv.desc->regs[reg][reg_nr],
161bb62a35bSVincent Donnefort 			 ichx_priv.gpio_base);
1626ed9f9c4SPeter Tyser 
163e6540f33SVincent Donnefort 	if (reg == GPIO_LVL && ichx_priv.desc->use_outlvl_cache)
164e6540f33SVincent Donnefort 		data = ichx_priv.outlvl_cache[reg_nr] | data;
165e6540f33SVincent Donnefort 
1666ed9f9c4SPeter Tyser 	spin_unlock_irqrestore(&ichx_priv.lock, flags);
1676ed9f9c4SPeter Tyser 
1686ed9f9c4SPeter Tyser 	return data & (1 << bit) ? 1 : 0;
1696ed9f9c4SPeter Tyser }
1706ed9f9c4SPeter Tyser 
171e97f9b52SMika Westerberg static bool ichx_gpio_check_available(struct gpio_chip *gpio, unsigned nr)
1724f600adaSJean Delvare {
17361d793bbSMika Westerberg 	return !!(ichx_priv.use_gpio & (1 << (nr / 32)));
1744f600adaSJean Delvare }
1754f600adaSJean Delvare 
1766ed9f9c4SPeter Tyser static int ichx_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
1776ed9f9c4SPeter Tyser {
1786ed9f9c4SPeter Tyser 	/*
1796ed9f9c4SPeter Tyser 	 * Try setting pin as an input and verify it worked since many pins
1806ed9f9c4SPeter Tyser 	 * are output-only.
1816ed9f9c4SPeter Tyser 	 */
1826ed9f9c4SPeter Tyser 	if (ichx_write_bit(GPIO_IO_SEL, nr, 1, 1))
1836ed9f9c4SPeter Tyser 		return -EINVAL;
1846ed9f9c4SPeter Tyser 
1856ed9f9c4SPeter Tyser 	return 0;
1866ed9f9c4SPeter Tyser }
1876ed9f9c4SPeter Tyser 
1886ed9f9c4SPeter Tyser static int ichx_gpio_direction_output(struct gpio_chip *gpio, unsigned nr,
1896ed9f9c4SPeter Tyser 					int val)
1906ed9f9c4SPeter Tyser {
1917f6569f5SVincent Donnefort 	/* Disable blink hardware which is available for GPIOs from 0 to 31. */
192ba7f74feSVincent Donnefort 	if (nr < 32 && ichx_priv.desc->have_blink)
1937f6569f5SVincent Donnefort 		ichx_write_bit(GPO_BLINK, nr, 0, 0);
1947f6569f5SVincent Donnefort 
1956ed9f9c4SPeter Tyser 	/* Set GPIO output value. */
1966ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
1976ed9f9c4SPeter Tyser 
1986ed9f9c4SPeter Tyser 	/*
1996ed9f9c4SPeter Tyser 	 * Try setting pin as an output and verify it worked since many pins
2006ed9f9c4SPeter Tyser 	 * are input-only.
2016ed9f9c4SPeter Tyser 	 */
2026ed9f9c4SPeter Tyser 	if (ichx_write_bit(GPIO_IO_SEL, nr, 0, 1))
2036ed9f9c4SPeter Tyser 		return -EINVAL;
2046ed9f9c4SPeter Tyser 
2056ed9f9c4SPeter Tyser 	return 0;
2066ed9f9c4SPeter Tyser }
2076ed9f9c4SPeter Tyser 
2086ed9f9c4SPeter Tyser static int ichx_gpio_get(struct gpio_chip *chip, unsigned nr)
2096ed9f9c4SPeter Tyser {
2106ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_LVL, nr);
2116ed9f9c4SPeter Tyser }
2126ed9f9c4SPeter Tyser 
2136ed9f9c4SPeter Tyser static int ich6_gpio_get(struct gpio_chip *chip, unsigned nr)
2146ed9f9c4SPeter Tyser {
2156ed9f9c4SPeter Tyser 	unsigned long flags;
2166ed9f9c4SPeter Tyser 	u32 data;
2176ed9f9c4SPeter Tyser 
2186ed9f9c4SPeter Tyser 	/*
2196ed9f9c4SPeter Tyser 	 * GPI 0 - 15 need to be read from the power management registers on
2206ed9f9c4SPeter Tyser 	 * a ICH6/3100 bridge.
2216ed9f9c4SPeter Tyser 	 */
2226ed9f9c4SPeter Tyser 	if (nr < 16) {
2236ed9f9c4SPeter Tyser 		if (!ichx_priv.pm_base)
2246ed9f9c4SPeter Tyser 			return -ENXIO;
2256ed9f9c4SPeter Tyser 
2266ed9f9c4SPeter Tyser 		spin_lock_irqsave(&ichx_priv.lock, flags);
2276ed9f9c4SPeter Tyser 
2286ed9f9c4SPeter Tyser 		/* GPI 0 - 15 are latched, write 1 to clear*/
2296ed9f9c4SPeter Tyser 		ICHX_WRITE(1 << (16 + nr), 0, ichx_priv.pm_base);
2306ed9f9c4SPeter Tyser 		data = ICHX_READ(0, ichx_priv.pm_base);
2316ed9f9c4SPeter Tyser 
2326ed9f9c4SPeter Tyser 		spin_unlock_irqrestore(&ichx_priv.lock, flags);
2336ed9f9c4SPeter Tyser 
2346ed9f9c4SPeter Tyser 		return (data >> 16) & (1 << nr) ? 1 : 0;
2356ed9f9c4SPeter Tyser 	} else {
2366ed9f9c4SPeter Tyser 		return ichx_gpio_get(chip, nr);
2376ed9f9c4SPeter Tyser 	}
2386ed9f9c4SPeter Tyser }
2396ed9f9c4SPeter Tyser 
2406ed9f9c4SPeter Tyser static int ichx_gpio_request(struct gpio_chip *chip, unsigned nr)
2416ed9f9c4SPeter Tyser {
24225f27db4SJean Delvare 	if (!ichx_gpio_check_available(chip, nr))
24325f27db4SJean Delvare 		return -ENXIO;
24425f27db4SJean Delvare 
2456ed9f9c4SPeter Tyser 	/*
2466ed9f9c4SPeter Tyser 	 * Note we assume the BIOS properly set a bridge's USE value.  Some
2476ed9f9c4SPeter Tyser 	 * chips (eg Intel 3100) have bogus USE values though, so first see if
2486ed9f9c4SPeter Tyser 	 * the chipset's USE value can be trusted for this specific bit.
2496ed9f9c4SPeter Tyser 	 * If it can't be trusted, assume that the pin can be used as a GPIO.
2506ed9f9c4SPeter Tyser 	 */
2516ed9f9c4SPeter Tyser 	if (ichx_priv.desc->use_sel_ignore[nr / 32] & (1 << (nr & 0x1f)))
2522ab3a749SJean Delvare 		return 0;
2536ed9f9c4SPeter Tyser 
2546ed9f9c4SPeter Tyser 	return ichx_read_bit(GPIO_USE_SEL, nr) ? 0 : -ENODEV;
2556ed9f9c4SPeter Tyser }
2566ed9f9c4SPeter Tyser 
2576ed9f9c4SPeter Tyser static int ich6_gpio_request(struct gpio_chip *chip, unsigned nr)
2586ed9f9c4SPeter Tyser {
2596ed9f9c4SPeter Tyser 	/*
2606ed9f9c4SPeter Tyser 	 * Fixups for bits 16 and 17 are necessary on the Intel ICH6/3100
2616ed9f9c4SPeter Tyser 	 * bridge as they are controlled by USE register bits 0 and 1.  See
2626ed9f9c4SPeter Tyser 	 * "Table 704 GPIO_USE_SEL1 register" in the i3100 datasheet for
2636ed9f9c4SPeter Tyser 	 * additional info.
2646ed9f9c4SPeter Tyser 	 */
2656ed9f9c4SPeter Tyser 	if (nr == 16 || nr == 17)
2666ed9f9c4SPeter Tyser 		nr -= 16;
2676ed9f9c4SPeter Tyser 
2686ed9f9c4SPeter Tyser 	return ichx_gpio_request(chip, nr);
2696ed9f9c4SPeter Tyser }
2706ed9f9c4SPeter Tyser 
2716ed9f9c4SPeter Tyser static void ichx_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
2726ed9f9c4SPeter Tyser {
2736ed9f9c4SPeter Tyser 	ichx_write_bit(GPIO_LVL, nr, val, 0);
2746ed9f9c4SPeter Tyser }
2756ed9f9c4SPeter Tyser 
2763836309dSBill Pemberton static void ichx_gpiolib_setup(struct gpio_chip *chip)
2776ed9f9c4SPeter Tyser {
2786ed9f9c4SPeter Tyser 	chip->owner = THIS_MODULE;
2796ed9f9c4SPeter Tyser 	chip->label = DRV_NAME;
2806ed9f9c4SPeter Tyser 	chip->dev = &ichx_priv.dev->dev;
2816ed9f9c4SPeter Tyser 
2826ed9f9c4SPeter Tyser 	/* Allow chip-specific overrides of request()/get() */
2836ed9f9c4SPeter Tyser 	chip->request = ichx_priv.desc->request ?
2846ed9f9c4SPeter Tyser 		ichx_priv.desc->request : ichx_gpio_request;
2856ed9f9c4SPeter Tyser 	chip->get = ichx_priv.desc->get ?
2866ed9f9c4SPeter Tyser 		ichx_priv.desc->get : ichx_gpio_get;
2876ed9f9c4SPeter Tyser 
2886ed9f9c4SPeter Tyser 	chip->set = ichx_gpio_set;
2896ed9f9c4SPeter Tyser 	chip->direction_input = ichx_gpio_direction_input;
2906ed9f9c4SPeter Tyser 	chip->direction_output = ichx_gpio_direction_output;
2916ed9f9c4SPeter Tyser 	chip->base = modparam_gpiobase;
2926ed9f9c4SPeter Tyser 	chip->ngpio = ichx_priv.desc->ngpio;
2939fb1f39eSLinus Walleij 	chip->can_sleep = false;
2946ed9f9c4SPeter Tyser 	chip->dbg_show = NULL;
2956ed9f9c4SPeter Tyser }
2966ed9f9c4SPeter Tyser 
2976ed9f9c4SPeter Tyser /* ICH6-based, 631xesb-based */
2986ed9f9c4SPeter Tyser static struct ichx_desc ich6_desc = {
2996ed9f9c4SPeter Tyser 	/* Bridges using the ICH6 controller need fixups for GPIO 0 - 17 */
3006ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
3016ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
3026ed9f9c4SPeter Tyser 
3036ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
3046ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
3056ed9f9c4SPeter Tyser 
3066ed9f9c4SPeter Tyser 	.ngpio = 50,
307ba7f74feSVincent Donnefort 	.have_blink = true,
3086ed9f9c4SPeter Tyser };
3096ed9f9c4SPeter Tyser 
3106ed9f9c4SPeter Tyser /* Intel 3100 */
3116ed9f9c4SPeter Tyser static struct ichx_desc i3100_desc = {
3126ed9f9c4SPeter Tyser 	/*
3136ed9f9c4SPeter Tyser 	 * Bits 16,17, 20 of USE_SEL and bit 16 of USE_SEL2 always read 0 on
3146ed9f9c4SPeter Tyser 	 * the Intel 3100.  See "Table 712. GPIO Summary Table" of 3100
3156ed9f9c4SPeter Tyser 	 * Datasheet for more info.
3166ed9f9c4SPeter Tyser 	 */
3176ed9f9c4SPeter Tyser 	.use_sel_ignore = {0x00130000, 0x00010000, 0x0},
3186ed9f9c4SPeter Tyser 
3196ed9f9c4SPeter Tyser 	/* The 3100 needs fixups for GPIO 0 - 17 */
3206ed9f9c4SPeter Tyser 	.request = ich6_gpio_request,
3216ed9f9c4SPeter Tyser 	.get = ich6_gpio_get,
3226ed9f9c4SPeter Tyser 
3236ed9f9c4SPeter Tyser 	/* GPIO 0-15 are read in the GPE0_STS PM register */
3246ed9f9c4SPeter Tyser 	.uses_gpe0 = true,
3256ed9f9c4SPeter Tyser 
3266ed9f9c4SPeter Tyser 	.ngpio = 50,
3276ed9f9c4SPeter Tyser };
3286ed9f9c4SPeter Tyser 
3296ed9f9c4SPeter Tyser /* ICH7 and ICH8-based */
3306ed9f9c4SPeter Tyser static struct ichx_desc ich7_desc = {
3316ed9f9c4SPeter Tyser 	.ngpio = 50,
332ba7f74feSVincent Donnefort 	.have_blink = true,
333bb62a35bSVincent Donnefort 	.regs = ichx_regs,
334bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3356ed9f9c4SPeter Tyser };
3366ed9f9c4SPeter Tyser 
3376ed9f9c4SPeter Tyser /* ICH9-based */
3386ed9f9c4SPeter Tyser static struct ichx_desc ich9_desc = {
3396ed9f9c4SPeter Tyser 	.ngpio = 61,
340ba7f74feSVincent Donnefort 	.have_blink = true,
341bb62a35bSVincent Donnefort 	.regs = ichx_regs,
342bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3436ed9f9c4SPeter Tyser };
3446ed9f9c4SPeter Tyser 
3456ed9f9c4SPeter Tyser /* ICH10-based - Consumer/corporate versions have different amount of GPIO */
3466ed9f9c4SPeter Tyser static struct ichx_desc ich10_cons_desc = {
3476ed9f9c4SPeter Tyser 	.ngpio = 61,
348ba7f74feSVincent Donnefort 	.have_blink = true,
349bb62a35bSVincent Donnefort 	.regs = ichx_regs,
350bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3516ed9f9c4SPeter Tyser };
3526ed9f9c4SPeter Tyser static struct ichx_desc ich10_corp_desc = {
3536ed9f9c4SPeter Tyser 	.ngpio = 72,
354ba7f74feSVincent Donnefort 	.have_blink = true,
355bb62a35bSVincent Donnefort 	.regs = ichx_regs,
356bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3576ed9f9c4SPeter Tyser };
3586ed9f9c4SPeter Tyser 
3596ed9f9c4SPeter Tyser /* Intel 5 series, 6 series, 3400 series, and C200 series */
3606ed9f9c4SPeter Tyser static struct ichx_desc intel5_desc = {
3616ed9f9c4SPeter Tyser 	.ngpio = 76,
362bb62a35bSVincent Donnefort 	.regs = ichx_regs,
363bb62a35bSVincent Donnefort 	.reglen = ichx_reglen,
3646ed9f9c4SPeter Tyser };
3656ed9f9c4SPeter Tyser 
366*3b923189SVincent Donnefort /* Avoton */
367*3b923189SVincent Donnefort static struct ichx_desc avoton_desc = {
368*3b923189SVincent Donnefort 	/* Avoton has only 59 GPIOs, but we assume the first set of register
369*3b923189SVincent Donnefort 	 * (Core) has 32 instead of 31 to keep gpio-ich compliance
370*3b923189SVincent Donnefort 	 */
371*3b923189SVincent Donnefort 	.ngpio = 60,
372*3b923189SVincent Donnefort 	.regs = avoton_regs,
373*3b923189SVincent Donnefort 	.reglen = avoton_reglen,
374*3b923189SVincent Donnefort 	.use_outlvl_cache = true,
375*3b923189SVincent Donnefort };
376*3b923189SVincent Donnefort 
3773836309dSBill Pemberton static int ichx_gpio_request_regions(struct resource *res_base,
3784f600adaSJean Delvare 						const char *name, u8 use_gpio)
3794f600adaSJean Delvare {
3804f600adaSJean Delvare 	int i;
3814f600adaSJean Delvare 
3824f600adaSJean Delvare 	if (!res_base || !res_base->start || !res_base->end)
3834f600adaSJean Delvare 		return -ENODEV;
3844f600adaSJean Delvare 
385bb62a35bSVincent Donnefort 	for (i = 0; i < ARRAY_SIZE(ichx_priv.desc->regs[0]); i++) {
3864f600adaSJean Delvare 		if (!(use_gpio & (1 << i)))
3874f600adaSJean Delvare 			continue;
388bb62a35bSVincent Donnefort 		if (!request_region(
389bb62a35bSVincent Donnefort 				res_base->start + ichx_priv.desc->regs[0][i],
390bb62a35bSVincent Donnefort 				ichx_priv.desc->reglen[i], name))
3914f600adaSJean Delvare 			goto request_err;
3924f600adaSJean Delvare 	}
3934f600adaSJean Delvare 	return 0;
3944f600adaSJean Delvare 
3954f600adaSJean Delvare request_err:
3964f600adaSJean Delvare 	/* Clean up: release already requested regions, if any */
3974f600adaSJean Delvare 	for (i--; i >= 0; i--) {
3984f600adaSJean Delvare 		if (!(use_gpio & (1 << i)))
3994f600adaSJean Delvare 			continue;
400bb62a35bSVincent Donnefort 		release_region(res_base->start + ichx_priv.desc->regs[0][i],
401bb62a35bSVincent Donnefort 			       ichx_priv.desc->reglen[i]);
4024f600adaSJean Delvare 	}
4034f600adaSJean Delvare 	return -EBUSY;
4044f600adaSJean Delvare }
4054f600adaSJean Delvare 
4064f600adaSJean Delvare static void ichx_gpio_release_regions(struct resource *res_base, u8 use_gpio)
4074f600adaSJean Delvare {
4084f600adaSJean Delvare 	int i;
4094f600adaSJean Delvare 
410bb62a35bSVincent Donnefort 	for (i = 0; i < ARRAY_SIZE(ichx_priv.desc->regs[0]); i++) {
4114f600adaSJean Delvare 		if (!(use_gpio & (1 << i)))
4124f600adaSJean Delvare 			continue;
413bb62a35bSVincent Donnefort 		release_region(res_base->start + ichx_priv.desc->regs[0][i],
414bb62a35bSVincent Donnefort 			       ichx_priv.desc->reglen[i]);
4154f600adaSJean Delvare 	}
4164f600adaSJean Delvare }
4174f600adaSJean Delvare 
4183836309dSBill Pemberton static int ichx_gpio_probe(struct platform_device *pdev)
4196ed9f9c4SPeter Tyser {
4206ed9f9c4SPeter Tyser 	struct resource *res_base, *res_pm;
4216ed9f9c4SPeter Tyser 	int err;
422e56aee18SJingoo Han 	struct lpc_ich_info *ich_info = dev_get_platdata(&pdev->dev);
4236ed9f9c4SPeter Tyser 
4246ed9f9c4SPeter Tyser 	if (!ich_info)
4256ed9f9c4SPeter Tyser 		return -ENODEV;
4266ed9f9c4SPeter Tyser 
4276ed9f9c4SPeter Tyser 	ichx_priv.dev = pdev;
4286ed9f9c4SPeter Tyser 
4296ed9f9c4SPeter Tyser 	switch (ich_info->gpio_version) {
4306ed9f9c4SPeter Tyser 	case ICH_I3100_GPIO:
4316ed9f9c4SPeter Tyser 		ichx_priv.desc = &i3100_desc;
4326ed9f9c4SPeter Tyser 		break;
4336ed9f9c4SPeter Tyser 	case ICH_V5_GPIO:
4346ed9f9c4SPeter Tyser 		ichx_priv.desc = &intel5_desc;
4356ed9f9c4SPeter Tyser 		break;
4366ed9f9c4SPeter Tyser 	case ICH_V6_GPIO:
4376ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich6_desc;
4386ed9f9c4SPeter Tyser 		break;
4396ed9f9c4SPeter Tyser 	case ICH_V7_GPIO:
4406ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich7_desc;
4416ed9f9c4SPeter Tyser 		break;
4426ed9f9c4SPeter Tyser 	case ICH_V9_GPIO:
4436ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich9_desc;
4446ed9f9c4SPeter Tyser 		break;
4456ed9f9c4SPeter Tyser 	case ICH_V10CORP_GPIO:
4466ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_corp_desc;
4476ed9f9c4SPeter Tyser 		break;
4486ed9f9c4SPeter Tyser 	case ICH_V10CONS_GPIO:
4496ed9f9c4SPeter Tyser 		ichx_priv.desc = &ich10_cons_desc;
4506ed9f9c4SPeter Tyser 		break;
451*3b923189SVincent Donnefort 	case AVOTON_GPIO:
452*3b923189SVincent Donnefort 		ichx_priv.desc = &avoton_desc;
453*3b923189SVincent Donnefort 		break;
4546ed9f9c4SPeter Tyser 	default:
4556ed9f9c4SPeter Tyser 		return -ENODEV;
4566ed9f9c4SPeter Tyser 	}
4576ed9f9c4SPeter Tyser 
458d39a948fSJean Delvare 	spin_lock_init(&ichx_priv.lock);
4596ed9f9c4SPeter Tyser 	res_base = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPIO);
4604f600adaSJean Delvare 	ichx_priv.use_gpio = ich_info->use_gpio;
4614f600adaSJean Delvare 	err = ichx_gpio_request_regions(res_base, pdev->name,
4624f600adaSJean Delvare 					ichx_priv.use_gpio);
4634f600adaSJean Delvare 	if (err)
4644f600adaSJean Delvare 		return err;
4656ed9f9c4SPeter Tyser 
4666ed9f9c4SPeter Tyser 	ichx_priv.gpio_base = res_base;
4676ed9f9c4SPeter Tyser 
4686ed9f9c4SPeter Tyser 	/*
4696ed9f9c4SPeter Tyser 	 * If necessary, determine the I/O address of ACPI/power management
4706ed9f9c4SPeter Tyser 	 * registers which are needed to read the the GPE0 register for GPI pins
4716ed9f9c4SPeter Tyser 	 * 0 - 15 on some chipsets.
4726ed9f9c4SPeter Tyser 	 */
4736ed9f9c4SPeter Tyser 	if (!ichx_priv.desc->uses_gpe0)
4746ed9f9c4SPeter Tyser 		goto init;
4756ed9f9c4SPeter Tyser 
4766ed9f9c4SPeter Tyser 	res_pm = platform_get_resource(pdev, IORESOURCE_IO, ICH_RES_GPE0);
4776ed9f9c4SPeter Tyser 	if (!res_pm) {
4786ed9f9c4SPeter Tyser 		pr_warn("ACPI BAR is unavailable, GPI 0 - 15 unavailable\n");
4796ed9f9c4SPeter Tyser 		goto init;
4806ed9f9c4SPeter Tyser 	}
4816ed9f9c4SPeter Tyser 
4826ed9f9c4SPeter Tyser 	if (!request_region(res_pm->start, resource_size(res_pm),
4836ed9f9c4SPeter Tyser 			pdev->name)) {
4846ed9f9c4SPeter Tyser 		pr_warn("ACPI BAR is busy, GPI 0 - 15 unavailable\n");
4856ed9f9c4SPeter Tyser 		goto init;
4866ed9f9c4SPeter Tyser 	}
4876ed9f9c4SPeter Tyser 
4886ed9f9c4SPeter Tyser 	ichx_priv.pm_base = res_pm;
4896ed9f9c4SPeter Tyser 
4906ed9f9c4SPeter Tyser init:
4916ed9f9c4SPeter Tyser 	ichx_gpiolib_setup(&ichx_priv.chip);
4926ed9f9c4SPeter Tyser 	err = gpiochip_add(&ichx_priv.chip);
4936ed9f9c4SPeter Tyser 	if (err) {
4946ed9f9c4SPeter Tyser 		pr_err("Failed to register GPIOs\n");
4956ed9f9c4SPeter Tyser 		goto add_err;
4966ed9f9c4SPeter Tyser 	}
4976ed9f9c4SPeter Tyser 
4986ed9f9c4SPeter Tyser 	pr_info("GPIO from %d to %d on %s\n", ichx_priv.chip.base,
4996ed9f9c4SPeter Tyser 	       ichx_priv.chip.base + ichx_priv.chip.ngpio - 1, DRV_NAME);
5006ed9f9c4SPeter Tyser 
5016ed9f9c4SPeter Tyser 	return 0;
5026ed9f9c4SPeter Tyser 
5036ed9f9c4SPeter Tyser add_err:
5044f600adaSJean Delvare 	ichx_gpio_release_regions(ichx_priv.gpio_base, ichx_priv.use_gpio);
5056ed9f9c4SPeter Tyser 	if (ichx_priv.pm_base)
5066ed9f9c4SPeter Tyser 		release_region(ichx_priv.pm_base->start,
5076ed9f9c4SPeter Tyser 				resource_size(ichx_priv.pm_base));
5086ed9f9c4SPeter Tyser 	return err;
5096ed9f9c4SPeter Tyser }
5106ed9f9c4SPeter Tyser 
511206210ceSBill Pemberton static int ichx_gpio_remove(struct platform_device *pdev)
5126ed9f9c4SPeter Tyser {
5136ed9f9c4SPeter Tyser 	int err;
5146ed9f9c4SPeter Tyser 
5156ed9f9c4SPeter Tyser 	err = gpiochip_remove(&ichx_priv.chip);
5166ed9f9c4SPeter Tyser 	if (err) {
5176ed9f9c4SPeter Tyser 		dev_err(&pdev->dev, "%s failed, %d\n",
5186ed9f9c4SPeter Tyser 				"gpiochip_remove()", err);
5196ed9f9c4SPeter Tyser 		return err;
5206ed9f9c4SPeter Tyser 	}
5216ed9f9c4SPeter Tyser 
5224f600adaSJean Delvare 	ichx_gpio_release_regions(ichx_priv.gpio_base, ichx_priv.use_gpio);
5236ed9f9c4SPeter Tyser 	if (ichx_priv.pm_base)
5246ed9f9c4SPeter Tyser 		release_region(ichx_priv.pm_base->start,
5256ed9f9c4SPeter Tyser 				resource_size(ichx_priv.pm_base));
5266ed9f9c4SPeter Tyser 
5276ed9f9c4SPeter Tyser 	return 0;
5286ed9f9c4SPeter Tyser }
5296ed9f9c4SPeter Tyser 
5306ed9f9c4SPeter Tyser static struct platform_driver ichx_gpio_driver = {
5316ed9f9c4SPeter Tyser 	.driver		= {
5326ed9f9c4SPeter Tyser 		.owner	= THIS_MODULE,
5336ed9f9c4SPeter Tyser 		.name	= DRV_NAME,
5346ed9f9c4SPeter Tyser 	},
5356ed9f9c4SPeter Tyser 	.probe		= ichx_gpio_probe,
5368283c4ffSBill Pemberton 	.remove		= ichx_gpio_remove,
5376ed9f9c4SPeter Tyser };
5386ed9f9c4SPeter Tyser 
5396ed9f9c4SPeter Tyser module_platform_driver(ichx_gpio_driver);
5406ed9f9c4SPeter Tyser 
5416ed9f9c4SPeter Tyser MODULE_AUTHOR("Peter Tyser <ptyser@xes-inc.com>");
5426ed9f9c4SPeter Tyser MODULE_DESCRIPTION("GPIO interface for Intel ICH series");
5436ed9f9c4SPeter Tyser MODULE_LICENSE("GPL");
5446ed9f9c4SPeter Tyser MODULE_ALIAS("platform:"DRV_NAME);
545