2007-05-14 20:30:15 +07:00
|
|
|
/*
|
|
|
|
* arch/arm/mach-ks8695/gpio.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Andrew Victor
|
2008-12-14 03:44:12 +07:00
|
|
|
* Updated to GPIOLIB, Copyright 2008 Simtec Electronics
|
|
|
|
* Daniel Silverstone <dsilvers@simtec.co.uk>
|
2007-05-14 20:30:15 +07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2018-03-12 18:20:23 +07:00
|
|
|
#include <linux/gpio/driver.h>
|
2007-05-14 20:30:15 +07:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/init.h>
|
2007-10-15 20:27:41 +07:00
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/seq_file.h>
|
2007-05-14 20:30:15 +07:00
|
|
|
#include <linux/module.h>
|
2008-09-06 18:10:45 +07:00
|
|
|
#include <linux/io.h>
|
2007-05-14 20:30:15 +07:00
|
|
|
|
2008-08-05 22:14:15 +07:00
|
|
|
#include <mach/hardware.h>
|
2007-05-14 20:30:15 +07:00
|
|
|
#include <asm/mach/irq.h>
|
|
|
|
|
2008-08-05 22:14:15 +07:00
|
|
|
#include <mach/regs-gpio.h>
|
2011-08-22 14:37:38 +07:00
|
|
|
#include <mach/gpio-ks8695.h>
|
2007-05-14 20:30:15 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure a GPIO line for either GPIO function, or its internal
|
|
|
|
* function (Interrupt, Timer, etc).
|
|
|
|
*/
|
2008-12-14 03:44:12 +07:00
|
|
|
static void ks8695_gpio_mode(unsigned int pin, short gpio)
|
2007-05-14 20:30:15 +07:00
|
|
|
{
|
|
|
|
unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
|
|
|
|
unsigned long x, flags;
|
|
|
|
|
|
|
|
if (pin > KS8695_GPIO_5) /* only GPIO 0..5 have internal functions */
|
|
|
|
return;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
|
|
|
|
if (gpio) /* GPIO: set bit to 0 */
|
|
|
|
x &= ~enable[pin];
|
|
|
|
else /* Internal function: set bit to 1 */
|
|
|
|
x |= enable[pin];
|
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPC);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned short gpio_irq[] = { KS8695_IRQ_EXTERN0, KS8695_IRQ_EXTERN1, KS8695_IRQ_EXTERN2, KS8695_IRQ_EXTERN3 };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure GPIO pin as external interrupt source.
|
|
|
|
*/
|
2008-12-14 03:44:12 +07:00
|
|
|
int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
|
2007-05-14 20:30:15 +07:00
|
|
|
{
|
|
|
|
unsigned long x, flags;
|
|
|
|
|
|
|
|
if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
/* set pin as input */
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
|
2008-10-08 02:20:15 +07:00
|
|
|
x &= ~IOPM(pin);
|
2007-05-14 20:30:15 +07:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
/* Set IRQ triggering type */
|
2011-03-24 19:25:22 +07:00
|
|
|
irq_set_irq_type(gpio_irq[pin], type);
|
2007-05-14 20:30:15 +07:00
|
|
|
|
|
|
|
/* enable interrupt mode */
|
|
|
|
ks8695_gpio_mode(pin, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(ks8695_gpio_interrupt);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* .... Generic GPIO interface .............................................. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure the GPIO line as an input.
|
|
|
|
*/
|
2008-12-14 03:44:12 +07:00
|
|
|
static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
|
2007-05-14 20:30:15 +07:00
|
|
|
{
|
|
|
|
unsigned long x, flags;
|
|
|
|
|
|
|
|
if (pin > KS8695_GPIO_15)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* set pin to GPIO mode */
|
|
|
|
ks8695_gpio_mode(pin, 1);
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
/* set pin as input */
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
|
2008-10-08 02:20:15 +07:00
|
|
|
x &= ~IOPM(pin);
|
2007-05-14 20:30:15 +07:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure the GPIO line as an output, with default state.
|
|
|
|
*/
|
2008-12-14 03:44:12 +07:00
|
|
|
static int ks8695_gpio_direction_output(struct gpio_chip *gc,
|
|
|
|
unsigned int pin, int state)
|
2007-05-14 20:30:15 +07:00
|
|
|
{
|
|
|
|
unsigned long x, flags;
|
|
|
|
|
|
|
|
if (pin > KS8695_GPIO_15)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* set pin to GPIO mode */
|
|
|
|
ks8695_gpio_mode(pin, 1);
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
/* set line state */
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
|
|
|
|
if (state)
|
2008-10-08 02:20:15 +07:00
|
|
|
x |= IOPD(pin);
|
2007-05-14 20:30:15 +07:00
|
|
|
else
|
2008-10-08 02:20:15 +07:00
|
|
|
x &= ~IOPD(pin);
|
2007-05-14 20:30:15 +07:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
|
|
|
|
|
|
|
|
/* set pin as output */
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
|
2008-10-08 02:20:15 +07:00
|
|
|
x |= IOPM(pin);
|
2007-05-14 20:30:15 +07:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the state of an output GPIO line.
|
|
|
|
*/
|
2008-12-14 03:44:12 +07:00
|
|
|
static void ks8695_gpio_set_value(struct gpio_chip *gc,
|
|
|
|
unsigned int pin, int state)
|
2007-05-14 20:30:15 +07:00
|
|
|
{
|
|
|
|
unsigned long x, flags;
|
|
|
|
|
|
|
|
if (pin > KS8695_GPIO_15)
|
|
|
|
return;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
/* set output line state */
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
|
|
|
|
if (state)
|
2008-10-08 02:20:15 +07:00
|
|
|
x |= IOPD(pin);
|
2007-05-14 20:30:15 +07:00
|
|
|
else
|
2008-10-08 02:20:15 +07:00
|
|
|
x &= ~IOPD(pin);
|
2007-05-14 20:30:15 +07:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the state of a GPIO line.
|
|
|
|
*/
|
2008-12-14 03:44:12 +07:00
|
|
|
static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
|
2007-05-14 20:30:15 +07:00
|
|
|
{
|
|
|
|
unsigned long x;
|
|
|
|
|
|
|
|
if (pin > KS8695_GPIO_15)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
|
2008-10-08 02:20:15 +07:00
|
|
|
return (x & IOPD(pin)) != 0;
|
2007-05-14 20:30:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map GPIO line to IRQ number.
|
|
|
|
*/
|
2008-12-14 03:44:13 +07:00
|
|
|
static int ks8695_gpio_to_irq(struct gpio_chip *gc, unsigned int pin)
|
2007-05-14 20:30:15 +07:00
|
|
|
{
|
|
|
|
if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return gpio_irq[pin];
|
|
|
|
}
|
|
|
|
|
2008-12-14 03:44:12 +07:00
|
|
|
/* GPIOLIB interface */
|
|
|
|
|
|
|
|
static struct gpio_chip ks8695_gpio_chip = {
|
|
|
|
.label = "KS8695",
|
|
|
|
.direction_input = ks8695_gpio_direction_input,
|
|
|
|
.direction_output = ks8695_gpio_direction_output,
|
|
|
|
.get = ks8695_gpio_get_value,
|
|
|
|
.set = ks8695_gpio_set_value,
|
2008-12-14 03:44:13 +07:00
|
|
|
.to_irq = ks8695_gpio_to_irq,
|
2008-12-14 03:44:12 +07:00
|
|
|
.base = 0,
|
|
|
|
.ngpio = 16,
|
2013-12-04 20:42:46 +07:00
|
|
|
.can_sleep = false,
|
2008-12-14 03:44:12 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Register the GPIOs */
|
|
|
|
void ks8695_register_gpios(void)
|
|
|
|
{
|
2015-12-08 16:41:44 +07:00
|
|
|
if (gpiochip_add_data(&ks8695_gpio_chip, NULL))
|
2008-12-14 03:44:12 +07:00
|
|
|
printk(KERN_ERR "Unable to register core GPIOs\n");
|
|
|
|
}
|
2007-10-15 20:27:41 +07:00
|
|
|
|
|
|
|
/* .... Debug interface ..................................................... */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
|
|
|
|
static int ks8695_gpio_show(struct seq_file *s, void *unused)
|
|
|
|
{
|
|
|
|
unsigned int enable[] = { IOPC_IOEINT0EN, IOPC_IOEINT1EN, IOPC_IOEINT2EN, IOPC_IOEINT3EN, IOPC_IOTIM0EN, IOPC_IOTIM1EN };
|
|
|
|
unsigned int intmask[] = { IOPC_IOEINT0TM, IOPC_IOEINT1TM, IOPC_IOEINT2TM, IOPC_IOEINT3TM };
|
|
|
|
unsigned long mode, ctrl, data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mode = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
|
|
|
|
ctrl = __raw_readl(KS8695_GPIO_VA + KS8695_IOPC);
|
|
|
|
data = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
|
|
|
|
|
|
|
|
seq_printf(s, "Pin\tI/O\tFunction\tState\n\n");
|
|
|
|
|
|
|
|
for (i = KS8695_GPIO_0; i <= KS8695_GPIO_15 ; i++) {
|
|
|
|
seq_printf(s, "%i:\t", i);
|
|
|
|
|
2008-10-08 02:20:15 +07:00
|
|
|
seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
|
2007-10-15 20:27:41 +07:00
|
|
|
|
|
|
|
if (i <= KS8695_GPIO_3) {
|
|
|
|
if (ctrl & enable[i]) {
|
|
|
|
seq_printf(s, "EXT%i ", i);
|
|
|
|
|
|
|
|
switch ((ctrl & intmask[i]) >> (4 * i)) {
|
2014-09-20 22:14:30 +07:00
|
|
|
case IOPC_TM_LOW:
|
|
|
|
seq_printf(s, "(Low)"); break;
|
|
|
|
case IOPC_TM_HIGH:
|
|
|
|
seq_printf(s, "(High)"); break;
|
|
|
|
case IOPC_TM_RISING:
|
|
|
|
seq_printf(s, "(Rising)"); break;
|
|
|
|
case IOPC_TM_FALLING:
|
|
|
|
seq_printf(s, "(Falling)"); break;
|
|
|
|
case IOPC_TM_EDGE:
|
|
|
|
seq_printf(s, "(Edges)"); break;
|
2007-10-15 20:27:41 +07:00
|
|
|
}
|
2014-09-27 20:35:41 +07:00
|
|
|
} else
|
2007-10-15 20:27:41 +07:00
|
|
|
seq_printf(s, "GPIO\t");
|
2014-09-27 20:35:41 +07:00
|
|
|
} else if (i <= KS8695_GPIO_5) {
|
2007-10-15 20:27:41 +07:00
|
|
|
if (ctrl & enable[i])
|
|
|
|
seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
|
|
|
|
else
|
|
|
|
seq_printf(s, "GPIO\t");
|
2014-09-27 20:35:41 +07:00
|
|
|
} else {
|
2007-10-15 20:27:41 +07:00
|
|
|
seq_printf(s, "GPIO\t");
|
2014-09-27 20:35:41 +07:00
|
|
|
}
|
2007-10-15 20:27:41 +07:00
|
|
|
|
|
|
|
seq_printf(s, "\t");
|
|
|
|
|
2008-10-08 02:20:15 +07:00
|
|
|
seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
|
2007-10-15 20:27:41 +07:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-30 23:22:46 +07:00
|
|
|
DEFINE_SHOW_ATTRIBUTE(ks8695_gpio);
|
2007-10-15 20:27:41 +07:00
|
|
|
|
|
|
|
static int __init ks8695_gpio_debugfs_init(void)
|
|
|
|
{
|
|
|
|
/* /sys/kernel/debug/ks8695_gpio */
|
2018-11-30 23:22:46 +07:00
|
|
|
debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL,
|
|
|
|
&ks8695_gpio_fops);
|
2007-10-15 20:27:41 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
postcore_initcall(ks8695_gpio_debugfs_init);
|
|
|
|
|
|
|
|
#endif
|