162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  Copyright (C) 2007-2009, OpenWrt.org, Florian Fainelli <florian@openwrt.org>
362306a36Sopenharmony_ci *	GPIOLIB support for Alchemy chips.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  This program is free software; you can redistribute	 it and/or modify it
662306a36Sopenharmony_ci *  under  the terms of	 the GNU General  Public License as published by the
762306a36Sopenharmony_ci *  Free Software Foundation;  either version 2 of the	License, or (at your
862306a36Sopenharmony_ci *  option) any later version.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci *  THIS  SOFTWARE  IS PROVIDED	  ``AS	IS'' AND   ANY	EXPRESS OR IMPLIED
1162306a36Sopenharmony_ci *  WARRANTIES,	  INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
1262306a36Sopenharmony_ci *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
1362306a36Sopenharmony_ci *  NO	EVENT  SHALL   THE AUTHOR  BE	 LIABLE FOR ANY	  DIRECT, INDIRECT,
1462306a36Sopenharmony_ci *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1562306a36Sopenharmony_ci *  NOT LIMITED	  TO, PROCUREMENT OF  SUBSTITUTE GOODS	OR SERVICES; LOSS OF
1662306a36Sopenharmony_ci *  USE, DATA,	OR PROFITS; OR	BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1762306a36Sopenharmony_ci *  ANY THEORY OF LIABILITY, WHETHER IN	 CONTRACT, STRICT LIABILITY, OR TORT
1862306a36Sopenharmony_ci *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1962306a36Sopenharmony_ci *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci *  You should have received a copy of the  GNU General Public License along
2262306a36Sopenharmony_ci *  with this program; if not, write  to the Free Software Foundation, Inc.,
2362306a36Sopenharmony_ci *  675 Mass Ave, Cambridge, MA 02139, USA.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci *  Notes :
2662306a36Sopenharmony_ci *	au1000 SoC have only one GPIO block : GPIO1
2762306a36Sopenharmony_ci *	Au1100, Au15x0, Au12x0 have a second one : GPIO2
2862306a36Sopenharmony_ci *	Au1300 is totally different: 1 block with up to 128 GPIOs
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#include <linux/init.h>
3262306a36Sopenharmony_ci#include <linux/kernel.h>
3362306a36Sopenharmony_ci#include <linux/types.h>
3462306a36Sopenharmony_ci#include <linux/gpio/driver.h>
3562306a36Sopenharmony_ci#include <asm/mach-au1x00/gpio-au1000.h>
3662306a36Sopenharmony_ci#include <asm/mach-au1x00/gpio-au1300.h>
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic int gpio2_get(struct gpio_chip *chip, unsigned offset)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	return !!alchemy_gpio2_get_value(offset + ALCHEMY_GPIO2_BASE);
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic void gpio2_set(struct gpio_chip *chip, unsigned offset, int value)
4462306a36Sopenharmony_ci{
4562306a36Sopenharmony_ci	alchemy_gpio2_set_value(offset + ALCHEMY_GPIO2_BASE, value);
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic int gpio2_direction_input(struct gpio_chip *chip, unsigned offset)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	return alchemy_gpio2_direction_input(offset + ALCHEMY_GPIO2_BASE);
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic int gpio2_direction_output(struct gpio_chip *chip, unsigned offset,
5462306a36Sopenharmony_ci				  int value)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	return alchemy_gpio2_direction_output(offset + ALCHEMY_GPIO2_BASE,
5762306a36Sopenharmony_ci						value);
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic int gpio2_to_irq(struct gpio_chip *chip, unsigned offset)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	return alchemy_gpio2_to_irq(offset + ALCHEMY_GPIO2_BASE);
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic int gpio1_get(struct gpio_chip *chip, unsigned offset)
6762306a36Sopenharmony_ci{
6862306a36Sopenharmony_ci	return !!alchemy_gpio1_get_value(offset + ALCHEMY_GPIO1_BASE);
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic void gpio1_set(struct gpio_chip *chip,
7262306a36Sopenharmony_ci				unsigned offset, int value)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	alchemy_gpio1_set_value(offset + ALCHEMY_GPIO1_BASE, value);
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cistatic int gpio1_direction_input(struct gpio_chip *chip, unsigned offset)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	return alchemy_gpio1_direction_input(offset + ALCHEMY_GPIO1_BASE);
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic int gpio1_direction_output(struct gpio_chip *chip,
8362306a36Sopenharmony_ci					unsigned offset, int value)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	return alchemy_gpio1_direction_output(offset + ALCHEMY_GPIO1_BASE,
8662306a36Sopenharmony_ci					     value);
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic int gpio1_to_irq(struct gpio_chip *chip, unsigned offset)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	return alchemy_gpio1_to_irq(offset + ALCHEMY_GPIO1_BASE);
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistruct gpio_chip alchemy_gpio_chip[] = {
9562306a36Sopenharmony_ci	[0] = {
9662306a36Sopenharmony_ci		.label			= "alchemy-gpio1",
9762306a36Sopenharmony_ci		.direction_input	= gpio1_direction_input,
9862306a36Sopenharmony_ci		.direction_output	= gpio1_direction_output,
9962306a36Sopenharmony_ci		.get			= gpio1_get,
10062306a36Sopenharmony_ci		.set			= gpio1_set,
10162306a36Sopenharmony_ci		.to_irq			= gpio1_to_irq,
10262306a36Sopenharmony_ci		.base			= ALCHEMY_GPIO1_BASE,
10362306a36Sopenharmony_ci		.ngpio			= ALCHEMY_GPIO1_NUM,
10462306a36Sopenharmony_ci	},
10562306a36Sopenharmony_ci	[1] = {
10662306a36Sopenharmony_ci		.label			= "alchemy-gpio2",
10762306a36Sopenharmony_ci		.direction_input	= gpio2_direction_input,
10862306a36Sopenharmony_ci		.direction_output	= gpio2_direction_output,
10962306a36Sopenharmony_ci		.get			= gpio2_get,
11062306a36Sopenharmony_ci		.set			= gpio2_set,
11162306a36Sopenharmony_ci		.to_irq			= gpio2_to_irq,
11262306a36Sopenharmony_ci		.base			= ALCHEMY_GPIO2_BASE,
11362306a36Sopenharmony_ci		.ngpio			= ALCHEMY_GPIO2_NUM,
11462306a36Sopenharmony_ci	},
11562306a36Sopenharmony_ci};
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistatic int alchemy_gpic_get(struct gpio_chip *chip, unsigned int off)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	return !!au1300_gpio_get_value(off + AU1300_GPIO_BASE);
12062306a36Sopenharmony_ci}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cistatic void alchemy_gpic_set(struct gpio_chip *chip, unsigned int off, int v)
12362306a36Sopenharmony_ci{
12462306a36Sopenharmony_ci	au1300_gpio_set_value(off + AU1300_GPIO_BASE, v);
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic int alchemy_gpic_dir_input(struct gpio_chip *chip, unsigned int off)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	return au1300_gpio_direction_input(off + AU1300_GPIO_BASE);
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic int alchemy_gpic_dir_output(struct gpio_chip *chip, unsigned int off,
13362306a36Sopenharmony_ci				   int v)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	return au1300_gpio_direction_output(off + AU1300_GPIO_BASE, v);
13662306a36Sopenharmony_ci}
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistatic int alchemy_gpic_gpio_to_irq(struct gpio_chip *chip, unsigned int off)
13962306a36Sopenharmony_ci{
14062306a36Sopenharmony_ci	return au1300_gpio_to_irq(off + AU1300_GPIO_BASE);
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cistatic struct gpio_chip au1300_gpiochip = {
14462306a36Sopenharmony_ci	.label			= "alchemy-gpic",
14562306a36Sopenharmony_ci	.direction_input	= alchemy_gpic_dir_input,
14662306a36Sopenharmony_ci	.direction_output	= alchemy_gpic_dir_output,
14762306a36Sopenharmony_ci	.get			= alchemy_gpic_get,
14862306a36Sopenharmony_ci	.set			= alchemy_gpic_set,
14962306a36Sopenharmony_ci	.to_irq			= alchemy_gpic_gpio_to_irq,
15062306a36Sopenharmony_ci	.base			= AU1300_GPIO_BASE,
15162306a36Sopenharmony_ci	.ngpio			= AU1300_GPIO_NUM,
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistatic int __init alchemy_gpiochip_init(void)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	int ret = 0;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	switch (alchemy_get_cputype()) {
15962306a36Sopenharmony_ci	case ALCHEMY_CPU_AU1000:
16062306a36Sopenharmony_ci		ret = gpiochip_add_data(&alchemy_gpio_chip[0], NULL);
16162306a36Sopenharmony_ci		break;
16262306a36Sopenharmony_ci	case ALCHEMY_CPU_AU1500...ALCHEMY_CPU_AU1200:
16362306a36Sopenharmony_ci		ret = gpiochip_add_data(&alchemy_gpio_chip[0], NULL);
16462306a36Sopenharmony_ci		ret |= gpiochip_add_data(&alchemy_gpio_chip[1], NULL);
16562306a36Sopenharmony_ci		break;
16662306a36Sopenharmony_ci	case ALCHEMY_CPU_AU1300:
16762306a36Sopenharmony_ci		ret = gpiochip_add_data(&au1300_gpiochip, NULL);
16862306a36Sopenharmony_ci		break;
16962306a36Sopenharmony_ci	}
17062306a36Sopenharmony_ci	return ret;
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ciarch_initcall(alchemy_gpiochip_init);
173