2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-08-30 22:55:27 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 Google, Inc
|
|
|
|
*
|
|
|
|
* (C) Copyright 2008-2014 Rockchip Electronics
|
|
|
|
* Peter, Software Engineering, <superpeter.cai@gmail.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2016-01-22 02:44:09 +00:00
|
|
|
#include <syscon.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2015-08-30 22:55:27 +00:00
|
|
|
#include <asm/gpio.h>
|
|
|
|
#include <asm/io.h>
|
2016-01-22 02:44:09 +00:00
|
|
|
#include <asm/arch/clock.h>
|
|
|
|
#include <dm/pinctrl.h>
|
|
|
|
#include <dt-bindings/clock/rk3288-cru.h>
|
2015-08-30 22:55:27 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
ROCKCHIP_GPIOS_PER_BANK = 32,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OFFSET_TO_BIT(bit) (1UL << (bit))
|
|
|
|
|
|
|
|
struct rockchip_gpio_priv {
|
|
|
|
struct rockchip_gpio_regs *regs;
|
2016-01-22 02:44:09 +00:00
|
|
|
struct udevice *pinctrl;
|
|
|
|
int bank;
|
2015-08-30 22:55:27 +00:00
|
|
|
char name[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
static int rockchip_gpio_direction_input(struct udevice *dev, unsigned offset)
|
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct rockchip_gpio_regs *regs = priv->regs;
|
|
|
|
|
|
|
|
clrbits_le32(®s->swport_ddr, OFFSET_TO_BIT(offset));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_gpio_direction_output(struct udevice *dev, unsigned offset,
|
|
|
|
int value)
|
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct rockchip_gpio_regs *regs = priv->regs;
|
|
|
|
int mask = OFFSET_TO_BIT(offset);
|
|
|
|
|
|
|
|
clrsetbits_le32(®s->swport_dr, mask, value ? mask : 0);
|
|
|
|
setbits_le32(®s->swport_ddr, mask);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_gpio_get_value(struct udevice *dev, unsigned offset)
|
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct rockchip_gpio_regs *regs = priv->regs;
|
|
|
|
|
2016-01-22 02:44:08 +00:00
|
|
|
return readl(®s->ext_port) & OFFSET_TO_BIT(offset) ? 1 : 0;
|
2015-08-30 22:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_gpio_set_value(struct udevice *dev, unsigned offset,
|
|
|
|
int value)
|
|
|
|
{
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct rockchip_gpio_regs *regs = priv->regs;
|
|
|
|
int mask = OFFSET_TO_BIT(offset);
|
|
|
|
|
|
|
|
clrsetbits_le32(®s->swport_dr, mask, value ? mask : 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_gpio_get_function(struct udevice *dev, unsigned offset)
|
|
|
|
{
|
2016-01-22 02:44:09 +00:00
|
|
|
#ifdef CONFIG_SPL_BUILD
|
|
|
|
return -ENODATA;
|
|
|
|
#else
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
struct rockchip_gpio_regs *regs = priv->regs;
|
|
|
|
bool is_output;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pinctrl_get_gpio_mux(priv->pinctrl, priv->bank, offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
is_output = readl(®s->swport_ddr) & OFFSET_TO_BIT(offset);
|
|
|
|
|
|
|
|
return is_output ? GPIOF_OUTPUT : GPIOF_INPUT;
|
|
|
|
#endif
|
2015-08-30 22:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rockchip_gpio_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
struct rockchip_gpio_priv *priv = dev_get_priv(dev);
|
|
|
|
char *end;
|
2016-01-22 02:44:09 +00:00
|
|
|
int ret;
|
2015-08-30 22:55:27 +00:00
|
|
|
|
2017-09-11 20:04:24 +00:00
|
|
|
priv->regs = dev_read_addr_ptr(dev);
|
2016-02-11 20:23:26 +00:00
|
|
|
ret = uclass_first_device_err(UCLASS_PINCTRL, &priv->pinctrl);
|
2016-01-22 02:44:09 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-08-30 22:55:27 +00:00
|
|
|
uc_priv->gpio_count = ROCKCHIP_GPIOS_PER_BANK;
|
|
|
|
end = strrchr(dev->name, '@');
|
2016-01-22 02:44:09 +00:00
|
|
|
priv->bank = trailing_strtoln(dev->name, end);
|
|
|
|
priv->name[0] = 'A' + priv->bank;
|
2015-08-30 22:55:27 +00:00
|
|
|
uc_priv->bank_name = priv->name;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_gpio_ops gpio_rockchip_ops = {
|
|
|
|
.direction_input = rockchip_gpio_direction_input,
|
|
|
|
.direction_output = rockchip_gpio_direction_output,
|
|
|
|
.get_value = rockchip_gpio_get_value,
|
|
|
|
.set_value = rockchip_gpio_set_value,
|
|
|
|
.get_function = rockchip_gpio_get_function,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id rockchip_gpio_ids[] = {
|
|
|
|
{ .compatible = "rockchip,gpio-bank" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(gpio_rockchip) = {
|
|
|
|
.name = "gpio_rockchip",
|
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.of_match = rockchip_gpio_ids,
|
|
|
|
.ops = &gpio_rockchip_ops,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct rockchip_gpio_priv),
|
|
|
|
.probe = rockchip_gpio_probe,
|
|
|
|
};
|