2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-04-25 15:25:14 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
|
|
|
|
*
|
|
|
|
* X-Powers AXP Power Management ICs gpio driver
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/arch/pmic_bus.h>
|
2015-04-22 09:31:22 +00:00
|
|
|
#include <asm/gpio.h>
|
2015-10-03 13:18:33 +00:00
|
|
|
#include <axp_pmic.h>
|
2015-04-22 09:31:22 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
#include <dm/root.h>
|
2015-04-25 15:25:14 +00:00
|
|
|
#include <errno.h>
|
2022-09-06 09:36:38 +00:00
|
|
|
#include <sunxi_gpio.h>
|
2015-04-25 15:25:14 +00:00
|
|
|
|
2015-04-26 09:19:37 +00:00
|
|
|
static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val);
|
|
|
|
|
2015-04-25 15:25:14 +00:00
|
|
|
static u8 axp_get_gpio_ctrl_reg(unsigned pin)
|
|
|
|
{
|
|
|
|
switch (pin) {
|
|
|
|
case 0: return AXP_GPIO0_CTRL;
|
|
|
|
case 1: return AXP_GPIO1_CTRL;
|
|
|
|
#ifdef AXP_GPIO2_CTRL
|
|
|
|
case 2: return AXP_GPIO2_CTRL;
|
|
|
|
#endif
|
|
|
|
#ifdef AXP_GPIO3_CTRL
|
|
|
|
case 3: return AXP_GPIO3_CTRL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-26 09:19:37 +00:00
|
|
|
static int axp_gpio_direction_input(struct udevice *dev, unsigned pin)
|
2015-04-25 15:25:14 +00:00
|
|
|
{
|
|
|
|
u8 reg;
|
|
|
|
|
2023-01-22 23:46:22 +00:00
|
|
|
reg = axp_get_gpio_ctrl_reg(pin);
|
|
|
|
if (reg == 0)
|
|
|
|
return -EINVAL;
|
2015-04-25 15:25:14 +00:00
|
|
|
|
2023-01-22 23:46:22 +00:00
|
|
|
return pmic_bus_write(reg, AXP_GPIO_CTRL_INPUT);
|
2015-04-25 15:25:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 09:19:37 +00:00
|
|
|
static int axp_gpio_direction_output(struct udevice *dev, unsigned pin,
|
|
|
|
int val)
|
2015-04-25 15:25:14 +00:00
|
|
|
{
|
|
|
|
__maybe_unused int ret;
|
|
|
|
u8 reg;
|
|
|
|
|
|
|
|
switch (pin) {
|
2016-03-29 16:26:56 +00:00
|
|
|
#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
|
|
|
|
/* Only available on later PMICs */
|
2015-04-25 15:25:14 +00:00
|
|
|
case SUNXI_GPIO_AXP0_VBUS_ENABLE:
|
2016-03-29 16:26:56 +00:00
|
|
|
ret = pmic_bus_clrbits(AXP_MISC_CTRL,
|
|
|
|
AXP_MISC_CTRL_N_VBUSEN_FUNC);
|
2015-04-25 15:25:14 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return axp_gpio_set_value(dev, pin, val);
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
reg = axp_get_gpio_ctrl_reg(pin);
|
|
|
|
if (reg == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
|
|
|
|
AXP_GPIO_CTRL_OUTPUT_LOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-26 09:19:37 +00:00
|
|
|
static int axp_gpio_get_value(struct udevice *dev, unsigned pin)
|
2015-04-25 15:25:14 +00:00
|
|
|
{
|
|
|
|
u8 reg, val, mask;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (pin) {
|
2016-03-29 16:26:56 +00:00
|
|
|
#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
|
|
|
|
/* Only available on later PMICs */
|
2015-04-25 15:25:14 +00:00
|
|
|
case SUNXI_GPIO_AXP0_VBUS_ENABLE:
|
2016-03-29 16:26:56 +00:00
|
|
|
ret = pmic_bus_read(AXP_VBUS_IPSOUT, &val);
|
|
|
|
mask = AXP_VBUS_IPSOUT_DRIVEBUS;
|
2015-04-25 15:25:14 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
reg = axp_get_gpio_ctrl_reg(pin);
|
|
|
|
if (reg == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = pmic_bus_read(AXP_GPIO_STATE, &val);
|
|
|
|
mask = 1 << (pin + AXP_GPIO_STATE_OFFSET);
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return (val & mask) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2015-04-26 09:19:37 +00:00
|
|
|
static int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
|
2015-04-25 15:25:14 +00:00
|
|
|
{
|
|
|
|
u8 reg;
|
|
|
|
|
|
|
|
switch (pin) {
|
2016-03-29 16:26:56 +00:00
|
|
|
#ifdef AXP_MISC_CTRL_N_VBUSEN_FUNC
|
|
|
|
/* Only available on later PMICs */
|
2015-04-25 15:25:14 +00:00
|
|
|
case SUNXI_GPIO_AXP0_VBUS_ENABLE:
|
|
|
|
if (val)
|
2016-03-29 16:26:56 +00:00
|
|
|
return pmic_bus_setbits(AXP_VBUS_IPSOUT,
|
|
|
|
AXP_VBUS_IPSOUT_DRIVEBUS);
|
2015-04-25 15:25:14 +00:00
|
|
|
else
|
2016-03-29 16:26:56 +00:00
|
|
|
return pmic_bus_clrbits(AXP_VBUS_IPSOUT,
|
|
|
|
AXP_VBUS_IPSOUT_DRIVEBUS);
|
2015-04-25 15:25:14 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
reg = axp_get_gpio_ctrl_reg(pin);
|
|
|
|
if (reg == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return pmic_bus_write(reg, val ? AXP_GPIO_CTRL_OUTPUT_HIGH :
|
|
|
|
AXP_GPIO_CTRL_OUTPUT_LOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 09:31:22 +00:00
|
|
|
static const struct dm_gpio_ops gpio_axp_ops = {
|
|
|
|
.direction_input = axp_gpio_direction_input,
|
|
|
|
.direction_output = axp_gpio_direction_output,
|
|
|
|
.get_value = axp_gpio_get_value,
|
|
|
|
.set_value = axp_gpio_set_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gpio_axp_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
|
|
|
|
/* Tell the uclass how many GPIOs we have */
|
|
|
|
uc_priv->bank_name = strdup(SUNXI_GPIO_AXP0_PREFIX);
|
|
|
|
uc_priv->gpio_count = SUNXI_GPIO_AXP0_GPIO_COUNT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(gpio_axp) = {
|
|
|
|
.name = "gpio_axp",
|
|
|
|
.id = UCLASS_GPIO,
|
|
|
|
.ops = &gpio_axp_ops,
|
|
|
|
.probe = gpio_axp_probe,
|
|
|
|
};
|
|
|
|
|
2015-04-25 15:25:14 +00:00
|
|
|
int axp_gpio_init(void)
|
|
|
|
{
|
2015-04-26 09:19:37 +00:00
|
|
|
struct udevice *dev;
|
2015-04-25 15:25:14 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pmic_bus_init();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-04-22 09:31:22 +00:00
|
|
|
/* There is no devicetree support for the axp yet, so bind directly */
|
|
|
|
ret = device_bind_driver(dm_root(), "gpio_axp", "AXP-gpio", &dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-04-25 15:25:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|