18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  Copyright (C) 2007-2009, OpenWrt.org, Florian Fainelli <florian@openwrt.org>
38c2ecf20Sopenharmony_ci *	GPIOLIB support for Alchemy chips.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  This program is free software; you can redistribute	 it and/or modify it
68c2ecf20Sopenharmony_ci *  under  the terms of	 the GNU General  Public License as published by the
78c2ecf20Sopenharmony_ci *  Free Software Foundation;  either version 2 of the	License, or (at your
88c2ecf20Sopenharmony_ci *  option) any later version.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *  THIS  SOFTWARE  IS PROVIDED	  ``AS	IS'' AND   ANY	EXPRESS OR IMPLIED
118c2ecf20Sopenharmony_ci *  WARRANTIES,	  INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
128c2ecf20Sopenharmony_ci *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
138c2ecf20Sopenharmony_ci *  NO	EVENT  SHALL   THE AUTHOR  BE	 LIABLE FOR ANY	  DIRECT, INDIRECT,
148c2ecf20Sopenharmony_ci *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
158c2ecf20Sopenharmony_ci *  NOT LIMITED	  TO, PROCUREMENT OF  SUBSTITUTE GOODS	OR SERVICES; LOSS OF
168c2ecf20Sopenharmony_ci *  USE, DATA,	OR PROFITS; OR	BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
178c2ecf20Sopenharmony_ci *  ANY THEORY OF LIABILITY, WHETHER IN	 CONTRACT, STRICT LIABILITY, OR TORT
188c2ecf20Sopenharmony_ci *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
198c2ecf20Sopenharmony_ci *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci *  You should have received a copy of the  GNU General Public License along
228c2ecf20Sopenharmony_ci *  with this program; if not, write  to the Free Software Foundation, Inc.,
238c2ecf20Sopenharmony_ci *  675 Mass Ave, Cambridge, MA 02139, USA.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci *  Notes :
268c2ecf20Sopenharmony_ci *	This file must ONLY be built when CONFIG_GPIOLIB=y and
278c2ecf20Sopenharmony_ci *	 CONFIG_ALCHEMY_GPIO_INDIRECT=n, otherwise compilation will fail!
288c2ecf20Sopenharmony_ci *	au1000 SoC have only one GPIO block : GPIO1
298c2ecf20Sopenharmony_ci *	Au1100, Au15x0, Au12x0 have a second one : GPIO2
308c2ecf20Sopenharmony_ci *	Au1300 is totally different: 1 block with up to 128 GPIOs
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <linux/init.h>
348c2ecf20Sopenharmony_ci#include <linux/kernel.h>
358c2ecf20Sopenharmony_ci#include <linux/types.h>
368c2ecf20Sopenharmony_ci#include <linux/gpio.h>
378c2ecf20Sopenharmony_ci#include <asm/mach-au1x00/gpio-au1000.h>
388c2ecf20Sopenharmony_ci#include <asm/mach-au1x00/gpio-au1300.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int gpio2_get(struct gpio_chip *chip, unsigned offset)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	return !!alchemy_gpio2_get_value(offset + ALCHEMY_GPIO2_BASE);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void gpio2_set(struct gpio_chip *chip, unsigned offset, int value)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	alchemy_gpio2_set_value(offset + ALCHEMY_GPIO2_BASE, value);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int gpio2_direction_input(struct gpio_chip *chip, unsigned offset)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	return alchemy_gpio2_direction_input(offset + ALCHEMY_GPIO2_BASE);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic int gpio2_direction_output(struct gpio_chip *chip, unsigned offset,
568c2ecf20Sopenharmony_ci				  int value)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	return alchemy_gpio2_direction_output(offset + ALCHEMY_GPIO2_BASE,
598c2ecf20Sopenharmony_ci						value);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int gpio2_to_irq(struct gpio_chip *chip, unsigned offset)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	return alchemy_gpio2_to_irq(offset + ALCHEMY_GPIO2_BASE);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int gpio1_get(struct gpio_chip *chip, unsigned offset)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	return !!alchemy_gpio1_get_value(offset + ALCHEMY_GPIO1_BASE);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void gpio1_set(struct gpio_chip *chip,
748c2ecf20Sopenharmony_ci				unsigned offset, int value)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	alchemy_gpio1_set_value(offset + ALCHEMY_GPIO1_BASE, value);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int gpio1_direction_input(struct gpio_chip *chip, unsigned offset)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	return alchemy_gpio1_direction_input(offset + ALCHEMY_GPIO1_BASE);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int gpio1_direction_output(struct gpio_chip *chip,
858c2ecf20Sopenharmony_ci					unsigned offset, int value)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	return alchemy_gpio1_direction_output(offset + ALCHEMY_GPIO1_BASE,
888c2ecf20Sopenharmony_ci					     value);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic int gpio1_to_irq(struct gpio_chip *chip, unsigned offset)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	return alchemy_gpio1_to_irq(offset + ALCHEMY_GPIO1_BASE);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct gpio_chip alchemy_gpio_chip[] = {
978c2ecf20Sopenharmony_ci	[0] = {
988c2ecf20Sopenharmony_ci		.label			= "alchemy-gpio1",
998c2ecf20Sopenharmony_ci		.direction_input	= gpio1_direction_input,
1008c2ecf20Sopenharmony_ci		.direction_output	= gpio1_direction_output,
1018c2ecf20Sopenharmony_ci		.get			= gpio1_get,
1028c2ecf20Sopenharmony_ci		.set			= gpio1_set,
1038c2ecf20Sopenharmony_ci		.to_irq			= gpio1_to_irq,
1048c2ecf20Sopenharmony_ci		.base			= ALCHEMY_GPIO1_BASE,
1058c2ecf20Sopenharmony_ci		.ngpio			= ALCHEMY_GPIO1_NUM,
1068c2ecf20Sopenharmony_ci	},
1078c2ecf20Sopenharmony_ci	[1] = {
1088c2ecf20Sopenharmony_ci		.label			= "alchemy-gpio2",
1098c2ecf20Sopenharmony_ci		.direction_input	= gpio2_direction_input,
1108c2ecf20Sopenharmony_ci		.direction_output	= gpio2_direction_output,
1118c2ecf20Sopenharmony_ci		.get			= gpio2_get,
1128c2ecf20Sopenharmony_ci		.set			= gpio2_set,
1138c2ecf20Sopenharmony_ci		.to_irq			= gpio2_to_irq,
1148c2ecf20Sopenharmony_ci		.base			= ALCHEMY_GPIO2_BASE,
1158c2ecf20Sopenharmony_ci		.ngpio			= ALCHEMY_GPIO2_NUM,
1168c2ecf20Sopenharmony_ci	},
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int alchemy_gpic_get(struct gpio_chip *chip, unsigned int off)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	return !!au1300_gpio_get_value(off + AU1300_GPIO_BASE);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void alchemy_gpic_set(struct gpio_chip *chip, unsigned int off, int v)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	au1300_gpio_set_value(off + AU1300_GPIO_BASE, v);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic int alchemy_gpic_dir_input(struct gpio_chip *chip, unsigned int off)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	return au1300_gpio_direction_input(off + AU1300_GPIO_BASE);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int alchemy_gpic_dir_output(struct gpio_chip *chip, unsigned int off,
1358c2ecf20Sopenharmony_ci				   int v)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return au1300_gpio_direction_output(off + AU1300_GPIO_BASE, v);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic int alchemy_gpic_gpio_to_irq(struct gpio_chip *chip, unsigned int off)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	return au1300_gpio_to_irq(off + AU1300_GPIO_BASE);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic struct gpio_chip au1300_gpiochip = {
1468c2ecf20Sopenharmony_ci	.label			= "alchemy-gpic",
1478c2ecf20Sopenharmony_ci	.direction_input	= alchemy_gpic_dir_input,
1488c2ecf20Sopenharmony_ci	.direction_output	= alchemy_gpic_dir_output,
1498c2ecf20Sopenharmony_ci	.get			= alchemy_gpic_get,
1508c2ecf20Sopenharmony_ci	.set			= alchemy_gpic_set,
1518c2ecf20Sopenharmony_ci	.to_irq			= alchemy_gpic_gpio_to_irq,
1528c2ecf20Sopenharmony_ci	.base			= AU1300_GPIO_BASE,
1538c2ecf20Sopenharmony_ci	.ngpio			= AU1300_GPIO_NUM,
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int __init alchemy_gpiochip_init(void)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	int ret = 0;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	switch (alchemy_get_cputype()) {
1618c2ecf20Sopenharmony_ci	case ALCHEMY_CPU_AU1000:
1628c2ecf20Sopenharmony_ci		ret = gpiochip_add_data(&alchemy_gpio_chip[0], NULL);
1638c2ecf20Sopenharmony_ci		break;
1648c2ecf20Sopenharmony_ci	case ALCHEMY_CPU_AU1500...ALCHEMY_CPU_AU1200:
1658c2ecf20Sopenharmony_ci		ret = gpiochip_add_data(&alchemy_gpio_chip[0], NULL);
1668c2ecf20Sopenharmony_ci		ret |= gpiochip_add_data(&alchemy_gpio_chip[1], NULL);
1678c2ecf20Sopenharmony_ci		break;
1688c2ecf20Sopenharmony_ci	case ALCHEMY_CPU_AU1300:
1698c2ecf20Sopenharmony_ci		ret = gpiochip_add_data(&au1300_gpiochip, NULL);
1708c2ecf20Sopenharmony_ci		break;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci	return ret;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ciarch_initcall(alchemy_gpiochip_init);
175