2019-05-30 07:57:47 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2007-05-14 21:30:15 +08:00
|
|
|
/*
|
|
|
|
* arch/arm/mach-ks8695/gpio.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Andrew Victor
|
2008-12-14 04:44:12 +08:00
|
|
|
* Updated to GPIOLIB, Copyright 2008 Simtec Electronics
|
|
|
|
* Daniel Silverstone <dsilvers@simtec.co.uk>
|
2007-05-14 21:30:15 +08:00
|
|
|
*/
|
2018-03-12 19:20:23 +08:00
|
|
|
#include <linux/gpio/driver.h>
|
2007-05-14 21:30:15 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/init.h>
|
2007-10-15 21:27:41 +08:00
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/seq_file.h>
|
2007-05-14 21:30:15 +08:00
|
|
|
#include <linux/module.h>
|
2008-09-06 19:10:45 +08:00
|
|
|
#include <linux/io.h>
|
2007-05-14 21:30:15 +08:00
|
|
|
|
2008-08-05 23:14:15 +08:00
|
|
|
#include <mach/hardware.h>
|
2007-05-14 21:30:15 +08:00
|
|
|
#include <asm/mach/irq.h>
|
|
|
|
|
2008-08-05 23:14:15 +08:00
|
|
|
#include <mach/regs-gpio.h>
|
2011-08-22 15:37:38 +08:00
|
|
|
#include <mach/gpio-ks8695.h>
|
2007-05-14 21:30:15 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure a GPIO line for either GPIO function, or its internal
|
|
|
|
* function (Interrupt, Timer, etc).
|
|
|
|
*/
|
2008-12-14 04:44:12 +08:00
|
|
|
static void ks8695_gpio_mode(unsigned int pin, short gpio)
|
2007-05-14 21:30:15 +08: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 04:44:12 +08:00
|
|
|
int ks8695_gpio_interrupt(unsigned int pin, unsigned int type)
|
2007-05-14 21:30:15 +08: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 03:20:15 +08:00
|
|
|
x &= ~IOPM(pin);
|
2007-05-14 21:30:15 +08:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPM);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
/* Set IRQ triggering type */
|
2011-03-24 20:25:22 +08:00
|
|
|
irq_set_irq_type(gpio_irq[pin], type);
|
2007-05-14 21:30:15 +08: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 04:44:12 +08:00
|
|
|
static int ks8695_gpio_direction_input(struct gpio_chip *gc, unsigned int pin)
|
2007-05-14 21:30:15 +08: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 03:20:15 +08:00
|
|
|
x &= ~IOPM(pin);
|
2007-05-14 21:30:15 +08: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 04:44:12 +08:00
|
|
|
static int ks8695_gpio_direction_output(struct gpio_chip *gc,
|
|
|
|
unsigned int pin, int state)
|
2007-05-14 21:30:15 +08: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 03:20:15 +08:00
|
|
|
x |= IOPD(pin);
|
2007-05-14 21:30:15 +08:00
|
|
|
else
|
2008-10-08 03:20:15 +08:00
|
|
|
x &= ~IOPD(pin);
|
2007-05-14 21:30:15 +08:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
|
|
|
|
|
|
|
|
/* set pin as output */
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPM);
|
2008-10-08 03:20:15 +08:00
|
|
|
x |= IOPM(pin);
|
2007-05-14 21:30:15 +08: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 04:44:12 +08:00
|
|
|
static void ks8695_gpio_set_value(struct gpio_chip *gc,
|
|
|
|
unsigned int pin, int state)
|
2007-05-14 21:30:15 +08: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 03:20:15 +08:00
|
|
|
x |= IOPD(pin);
|
2007-05-14 21:30:15 +08:00
|
|
|
else
|
2008-10-08 03:20:15 +08:00
|
|
|
x &= ~IOPD(pin);
|
2007-05-14 21:30:15 +08:00
|
|
|
__raw_writel(x, KS8695_GPIO_VA + KS8695_IOPD);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the state of a GPIO line.
|
|
|
|
*/
|
2008-12-14 04:44:12 +08:00
|
|
|
static int ks8695_gpio_get_value(struct gpio_chip *gc, unsigned int pin)
|
2007-05-14 21:30:15 +08:00
|
|
|
{
|
|
|
|
unsigned long x;
|
|
|
|
|
|
|
|
if (pin > KS8695_GPIO_15)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
x = __raw_readl(KS8695_GPIO_VA + KS8695_IOPD);
|
2008-10-08 03:20:15 +08:00
|
|
|
return (x & IOPD(pin)) != 0;
|
2007-05-14 21:30:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map GPIO line to IRQ number.
|
|
|
|
*/
|
2008-12-14 04:44:13 +08:00
|
|
|
static int ks8695_gpio_to_irq(struct gpio_chip *gc, unsigned int pin)
|
2007-05-14 21:30:15 +08:00
|
|
|
{
|
|
|
|
if (pin > KS8695_GPIO_3) /* only GPIO 0..3 can generate IRQ */
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return gpio_irq[pin];
|
|
|
|
}
|
|
|
|
|
2008-12-14 04:44:12 +08: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 04:44:13 +08:00
|
|
|
.to_irq = ks8695_gpio_to_irq,
|
2008-12-14 04:44:12 +08:00
|
|
|
.base = 0,
|
|
|
|
.ngpio = 16,
|
2013-12-04 21:42:46 +08:00
|
|
|
.can_sleep = false,
|
2008-12-14 04:44:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Register the GPIOs */
|
|
|
|
void ks8695_register_gpios(void)
|
|
|
|
{
|
2015-12-08 17:41:44 +08:00
|
|
|
if (gpiochip_add_data(&ks8695_gpio_chip, NULL))
|
2008-12-14 04:44:12 +08:00
|
|
|
printk(KERN_ERR "Unable to register core GPIOs\n");
|
|
|
|
}
|
2007-10-15 21:27:41 +08: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 03:20:15 +08:00
|
|
|
seq_printf(s, "%s\t", (mode & IOPM(i)) ? "Output" : "Input");
|
2007-10-15 21:27:41 +08: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 23:14:30 +08: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 21:27:41 +08:00
|
|
|
}
|
2014-09-27 21:35:41 +08:00
|
|
|
} else
|
2007-10-15 21:27:41 +08:00
|
|
|
seq_printf(s, "GPIO\t");
|
2014-09-27 21:35:41 +08:00
|
|
|
} else if (i <= KS8695_GPIO_5) {
|
2007-10-15 21:27:41 +08:00
|
|
|
if (ctrl & enable[i])
|
|
|
|
seq_printf(s, "TOUT%i\t", i - KS8695_GPIO_4);
|
|
|
|
else
|
|
|
|
seq_printf(s, "GPIO\t");
|
2014-09-27 21:35:41 +08:00
|
|
|
} else {
|
2007-10-15 21:27:41 +08:00
|
|
|
seq_printf(s, "GPIO\t");
|
2014-09-27 21:35:41 +08:00
|
|
|
}
|
2007-10-15 21:27:41 +08:00
|
|
|
|
|
|
|
seq_printf(s, "\t");
|
|
|
|
|
2008-10-08 03:20:15 +08:00
|
|
|
seq_printf(s, "%i\n", (data & IOPD(i)) ? 1 : 0);
|
2007-10-15 21:27:41 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-01 00:22:46 +08:00
|
|
|
DEFINE_SHOW_ATTRIBUTE(ks8695_gpio);
|
2007-10-15 21:27:41 +08:00
|
|
|
|
|
|
|
static int __init ks8695_gpio_debugfs_init(void)
|
|
|
|
{
|
|
|
|
/* /sys/kernel/debug/ks8695_gpio */
|
2018-12-01 00:22:46 +08:00
|
|
|
debugfs_create_file("ks8695_gpio", S_IFREG | S_IRUGO, NULL, NULL,
|
|
|
|
&ks8695_gpio_fops);
|
2007-10-15 21:27:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
postcore_initcall(ks8695_gpio_debugfs_init);
|
|
|
|
|
|
|
|
#endif
|