2004-08-01 22:48:16 +00:00
|
|
|
/*
|
2007-07-05 16:02:15 +00:00
|
|
|
* arch/arm/mach-imx/gpio.c
|
2004-08-01 22:48:16 +00:00
|
|
|
*
|
|
|
|
* author: Sascha Hauer
|
|
|
|
* Created: april 20th, 2004
|
|
|
|
* Copyright: Synertronixx GmbH
|
|
|
|
*
|
|
|
|
* Common code for i.MX machines
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2009-12-03 13:16:33 +00:00
|
|
|
#include <errno.h>
|
2011-09-22 17:02:57 +00:00
|
|
|
#include <io.h>
|
2009-10-22 12:21:25 +00:00
|
|
|
#include <mach/imx-regs.h>
|
2012-09-03 08:18:09 +00:00
|
|
|
#include <gpio.h>
|
|
|
|
#include <init.h>
|
2004-08-01 22:48:16 +00:00
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
struct imx_gpio_chip {
|
|
|
|
void __iomem *base;
|
|
|
|
struct gpio_chip chip;
|
2012-09-09 14:51:03 +00:00
|
|
|
struct imx_gpio_regs *regs;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct imx_gpio_regs {
|
|
|
|
unsigned int dr;
|
|
|
|
unsigned int gdir;
|
|
|
|
unsigned int psr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct imx_gpio_regs regs_imx1 = {
|
|
|
|
.dr = 0x1c,
|
|
|
|
.gdir = 0x00,
|
|
|
|
.psr = 0x24,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct imx_gpio_regs regs_imx31 = {
|
|
|
|
.dr = 0x00,
|
|
|
|
.gdir = 0x04,
|
|
|
|
.psr = 0x08,
|
2012-09-03 08:18:09 +00:00
|
|
|
};
|
2009-12-03 13:16:33 +00:00
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
static void imx_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
|
2009-12-03 13:16:33 +00:00
|
|
|
{
|
2012-09-03 08:18:09 +00:00
|
|
|
struct imx_gpio_chip *imxgpio = container_of(chip, struct imx_gpio_chip, chip);
|
|
|
|
void __iomem *base = imxgpio->base;
|
2009-12-03 13:16:33 +00:00
|
|
|
u32 val;
|
|
|
|
|
|
|
|
if (!base)
|
|
|
|
return;
|
|
|
|
|
2012-09-09 14:51:03 +00:00
|
|
|
val = readl(base + imxgpio->regs->dr);
|
2009-12-03 13:16:33 +00:00
|
|
|
|
|
|
|
if (value)
|
2012-09-03 08:18:09 +00:00
|
|
|
val |= 1 << gpio;
|
2009-06-25 14:20:26 +00:00
|
|
|
else
|
2012-09-03 08:18:09 +00:00
|
|
|
val &= ~(1 << gpio);
|
2009-12-03 13:16:33 +00:00
|
|
|
|
2012-09-09 14:51:03 +00:00
|
|
|
writel(val, base + imxgpio->regs->dr);
|
2009-06-25 14:20:26 +00:00
|
|
|
}
|
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
static int imx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
|
2009-06-25 14:20:26 +00:00
|
|
|
{
|
2012-09-03 08:18:09 +00:00
|
|
|
struct imx_gpio_chip *imxgpio = container_of(chip, struct imx_gpio_chip, chip);
|
|
|
|
void __iomem *base = imxgpio->base;
|
2009-12-03 13:16:33 +00:00
|
|
|
u32 val;
|
|
|
|
|
|
|
|
if (!base)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-09-09 14:51:03 +00:00
|
|
|
val = readl(base + imxgpio->regs->gdir);
|
2012-09-03 08:18:09 +00:00
|
|
|
val &= ~(1 << gpio);
|
2012-09-09 14:51:03 +00:00
|
|
|
writel(val, base + imxgpio->regs->gdir);
|
2009-12-03 13:16:33 +00:00
|
|
|
|
2009-06-25 14:20:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
static int imx_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int value)
|
2009-06-25 14:20:26 +00:00
|
|
|
{
|
2012-09-03 08:18:09 +00:00
|
|
|
struct imx_gpio_chip *imxgpio = container_of(chip, struct imx_gpio_chip, chip);
|
|
|
|
void __iomem *base = imxgpio->base;
|
2009-12-03 13:16:33 +00:00
|
|
|
u32 val;
|
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
gpio_set_value(gpio + chip->base, value);
|
2009-12-03 13:16:33 +00:00
|
|
|
|
2012-09-09 14:51:03 +00:00
|
|
|
val = readl(base + imxgpio->regs->gdir);
|
2012-09-03 08:18:09 +00:00
|
|
|
val |= 1 << gpio;
|
2012-09-09 14:51:03 +00:00
|
|
|
writel(val, base + imxgpio->regs->gdir);
|
2009-12-03 13:16:33 +00:00
|
|
|
|
2009-06-25 14:20:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
static int imx_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
|
2009-12-03 13:16:33 +00:00
|
|
|
{
|
2012-09-03 08:18:09 +00:00
|
|
|
struct imx_gpio_chip *imxgpio = container_of(chip, struct imx_gpio_chip, chip);
|
|
|
|
void __iomem *base = imxgpio->base;
|
2009-12-03 13:16:33 +00:00
|
|
|
u32 val;
|
|
|
|
|
2012-09-09 14:51:03 +00:00
|
|
|
val = readl(base + imxgpio->regs->psr);
|
2009-12-03 13:16:33 +00:00
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
return val & (1 << gpio) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct gpio_ops imx_gpio_ops = {
|
|
|
|
.direction_input = imx_gpio_direction_input,
|
|
|
|
.direction_output = imx_gpio_direction_output,
|
|
|
|
.get = imx_gpio_get_value,
|
|
|
|
.set = imx_gpio_set_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int imx_gpio_probe(struct device_d *dev)
|
|
|
|
{
|
|
|
|
struct imx_gpio_chip *imxgpio;
|
2012-09-09 14:51:03 +00:00
|
|
|
struct imx_gpio_regs *regs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = dev_get_drvdata(dev, (unsigned long *)®s);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2012-09-03 08:18:09 +00:00
|
|
|
|
|
|
|
imxgpio = xzalloc(sizeof(*imxgpio));
|
|
|
|
imxgpio->base = dev_request_mem_region(dev, 0);
|
|
|
|
imxgpio->chip.ops = &imx_gpio_ops;
|
|
|
|
imxgpio->chip.base = -1;
|
|
|
|
imxgpio->chip.ngpio = 32;
|
|
|
|
imxgpio->chip.dev = dev;
|
2012-09-09 14:51:03 +00:00
|
|
|
imxgpio->regs = regs;
|
2012-09-03 08:18:09 +00:00
|
|
|
gpiochip_add(&imxgpio->chip);
|
|
|
|
|
|
|
|
dev_info(dev, "probed gpiochip%d with base %d\n", dev->id, imxgpio->chip.base);
|
|
|
|
|
|
|
|
return 0;
|
2009-12-03 13:16:33 +00:00
|
|
|
}
|
2009-06-25 14:20:26 +00:00
|
|
|
|
2012-09-09 14:51:03 +00:00
|
|
|
static struct platform_device_id imx_gpio_ids[] = {
|
|
|
|
{
|
|
|
|
.name = "imx1-gpio",
|
|
|
|
.driver_data = (unsigned long)®s_imx1,
|
|
|
|
}, {
|
|
|
|
.name = "imx31-gpio",
|
|
|
|
.driver_data = (unsigned long)®s_imx31,
|
|
|
|
}, {
|
|
|
|
/* sentinel */
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-09-03 08:18:09 +00:00
|
|
|
static struct driver_d imx_gpio_driver = {
|
|
|
|
.name = "imx-gpio",
|
|
|
|
.probe = imx_gpio_probe,
|
2012-09-09 14:51:03 +00:00
|
|
|
.id_table = imx_gpio_ids,
|
2012-09-03 08:18:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int imx_gpio_add(void)
|
|
|
|
{
|
|
|
|
register_driver(&imx_gpio_driver);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
coredevice_initcall(imx_gpio_add);
|