2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-03-31 21:12:31 +00:00
|
|
|
/*
|
2022-08-04 14:27:17 +00:00
|
|
|
* Qualcomm generic pmic gpio driver
|
2016-03-31 21:12:31 +00:00
|
|
|
*
|
|
|
|
* (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2016-03-31 21:12:31 +00:00
|
|
|
#include <power/pmic.h>
|
|
|
|
#include <spmi/spmi.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/gpio.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
|
|
|
|
/* Register offset for each gpio */
|
|
|
|
#define REG_OFFSET(x) ((x) * 0x100)
|
|
|
|
|
|
|
|
/* Register maps */
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
/* Type and subtype are shared for all PMIC peripherals */
|
2016-03-31 21:12:31 +00:00
|
|
|
#define REG_TYPE 0x4
|
|
|
|
#define REG_SUBTYPE 0x5
|
|
|
|
|
2022-08-04 14:27:18 +00:00
|
|
|
/* GPIO peripheral type and subtype out_values */
|
|
|
|
#define REG_TYPE_VAL 0x10
|
|
|
|
#define REG_SUBTYPE_GPIO_4CH 0x1
|
|
|
|
#define REG_SUBTYPE_GPIOC_4CH 0x5
|
|
|
|
#define REG_SUBTYPE_GPIO_8CH 0x9
|
|
|
|
#define REG_SUBTYPE_GPIOC_8CH 0xd
|
|
|
|
#define REG_SUBTYPE_GPIO_LV 0x10
|
|
|
|
#define REG_SUBTYPE_GPIO_MV 0x11
|
|
|
|
|
2016-03-31 21:12:31 +00:00
|
|
|
#define REG_STATUS 0x08
|
|
|
|
#define REG_STATUS_VAL_MASK 0x1
|
|
|
|
|
|
|
|
/* MODE_CTL */
|
2018-01-10 10:33:51 +00:00
|
|
|
#define REG_CTL 0x40
|
2016-03-31 21:12:31 +00:00
|
|
|
#define REG_CTL_MODE_MASK 0x70
|
|
|
|
#define REG_CTL_MODE_INPUT 0x00
|
|
|
|
#define REG_CTL_MODE_INOUT 0x20
|
|
|
|
#define REG_CTL_MODE_OUTPUT 0x10
|
|
|
|
#define REG_CTL_OUTPUT_MASK 0x0F
|
2022-08-04 14:27:18 +00:00
|
|
|
#define REG_CTL_LV_MV_MODE_MASK 0x3
|
|
|
|
#define REG_CTL_LV_MV_MODE_INPUT 0x0
|
|
|
|
#define REG_CTL_LV_MV_MODE_INOUT 0x2
|
|
|
|
#define REG_CTL_LV_MV_MODE_OUTPUT 0x1
|
2016-03-31 21:12:31 +00:00
|
|
|
|
|
|
|
#define REG_DIG_VIN_CTL 0x41
|
|
|
|
#define REG_DIG_VIN_VIN0 0
|
|
|
|
|
|
|
|
#define REG_DIG_PULL_CTL 0x42
|
|
|
|
#define REG_DIG_PULL_NO_PU 0x5
|
|
|
|
|
2022-08-04 14:27:18 +00:00
|
|
|
#define REG_LV_MV_OUTPUT_CTL 0x44
|
|
|
|
#define REG_LV_MV_OUTPUT_CTL_MASK 0x80
|
|
|
|
#define REG_LV_MV_OUTPUT_CTL_SHIFT 7
|
|
|
|
|
2016-03-31 21:12:31 +00:00
|
|
|
#define REG_DIG_OUT_CTL 0x45
|
|
|
|
#define REG_DIG_OUT_CTL_CMOS (0x0 << 4)
|
|
|
|
#define REG_DIG_OUT_CTL_DRIVE_L 0x1
|
|
|
|
|
|
|
|
#define REG_EN_CTL 0x46
|
|
|
|
#define REG_EN_CTL_ENABLE (1 << 7)
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
struct qcom_gpio_bank {
|
2016-04-12 19:11:23 +00:00
|
|
|
uint32_t pid; /* Peripheral ID on SPMI bus */
|
2022-08-04 14:27:18 +00:00
|
|
|
bool lv_mv_type; /* If subtype is GPIO_LV(0x10) or GPIO_MV(0x11) */
|
2016-03-31 21:12:31 +00:00
|
|
|
};
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_set_direction(struct udevice *dev, unsigned offset,
|
|
|
|
bool input, int value)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
2022-08-04 14:27:17 +00:00
|
|
|
struct qcom_gpio_bank *priv = dev_get_priv(dev);
|
2016-03-31 21:12:31 +00:00
|
|
|
uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
|
2022-08-04 14:27:18 +00:00
|
|
|
uint32_t reg_ctl_val;
|
2016-03-31 21:12:31 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Disable the GPIO */
|
|
|
|
ret = pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL,
|
|
|
|
REG_EN_CTL_ENABLE, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-08-04 14:27:18 +00:00
|
|
|
/* Select the mode and output */
|
|
|
|
if (priv->lv_mv_type) {
|
|
|
|
if (input)
|
|
|
|
reg_ctl_val = REG_CTL_LV_MV_MODE_INPUT;
|
|
|
|
else
|
|
|
|
reg_ctl_val = REG_CTL_LV_MV_MODE_INOUT;
|
|
|
|
} else {
|
|
|
|
if (input)
|
|
|
|
reg_ctl_val = REG_CTL_MODE_INPUT;
|
|
|
|
else
|
|
|
|
reg_ctl_val = REG_CTL_MODE_INOUT | !!value;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pmic_reg_write(dev->parent, gpio_base + REG_CTL, reg_ctl_val);
|
2016-03-31 21:12:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-08-04 14:27:18 +00:00
|
|
|
if (priv->lv_mv_type && !input) {
|
|
|
|
ret = pmic_reg_write(dev->parent,
|
|
|
|
gpio_base + REG_LV_MV_OUTPUT_CTL,
|
|
|
|
!!value << REG_LV_MV_OUTPUT_CTL_SHIFT);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-31 21:12:31 +00:00
|
|
|
/* Set the right pull (no pull) */
|
|
|
|
ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_PULL_CTL,
|
|
|
|
REG_DIG_PULL_NO_PU);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Configure output pin drivers if needed */
|
|
|
|
if (!input) {
|
|
|
|
/* Select the VIN - VIN0, pin is input so it doesn't matter */
|
|
|
|
ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_VIN_CTL,
|
|
|
|
REG_DIG_VIN_VIN0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Set the right dig out control */
|
|
|
|
ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_OUT_CTL,
|
|
|
|
REG_DIG_OUT_CTL_CMOS |
|
|
|
|
REG_DIG_OUT_CTL_DRIVE_L);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable the GPIO */
|
|
|
|
return pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL, 0,
|
|
|
|
REG_EN_CTL_ENABLE);
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_direction_input(struct udevice *dev, unsigned offset)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
2022-08-04 14:27:17 +00:00
|
|
|
return qcom_gpio_set_direction(dev, offset, true, 0);
|
2016-03-31 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_direction_output(struct udevice *dev, unsigned offset,
|
|
|
|
int value)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
2022-08-04 14:27:17 +00:00
|
|
|
return qcom_gpio_set_direction(dev, offset, false, value);
|
2016-03-31 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_get_function(struct udevice *dev, unsigned offset)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
2022-08-04 14:27:17 +00:00
|
|
|
struct qcom_gpio_bank *priv = dev_get_priv(dev);
|
2016-03-31 21:12:31 +00:00
|
|
|
uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
|
|
|
|
int reg;
|
|
|
|
|
|
|
|
reg = pmic_reg_read(dev->parent, gpio_base + REG_CTL);
|
|
|
|
if (reg < 0)
|
|
|
|
return reg;
|
|
|
|
|
2022-08-04 14:27:18 +00:00
|
|
|
if (priv->lv_mv_type) {
|
|
|
|
switch (reg & REG_CTL_LV_MV_MODE_MASK) {
|
|
|
|
case REG_CTL_LV_MV_MODE_INPUT:
|
|
|
|
return GPIOF_INPUT;
|
|
|
|
case REG_CTL_LV_MV_MODE_INOUT: /* Fallthrough */
|
|
|
|
case REG_CTL_LV_MV_MODE_OUTPUT:
|
|
|
|
return GPIOF_OUTPUT;
|
|
|
|
default:
|
|
|
|
return GPIOF_UNKNOWN;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (reg & REG_CTL_MODE_MASK) {
|
|
|
|
case REG_CTL_MODE_INPUT:
|
|
|
|
return GPIOF_INPUT;
|
|
|
|
case REG_CTL_MODE_INOUT: /* Fallthrough */
|
|
|
|
case REG_CTL_MODE_OUTPUT:
|
|
|
|
return GPIOF_OUTPUT;
|
|
|
|
default:
|
|
|
|
return GPIOF_UNKNOWN;
|
|
|
|
}
|
2016-03-31 21:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_get_value(struct udevice *dev, unsigned offset)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
2022-08-04 14:27:17 +00:00
|
|
|
struct qcom_gpio_bank *priv = dev_get_priv(dev);
|
2016-03-31 21:12:31 +00:00
|
|
|
uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
|
|
|
|
int reg;
|
|
|
|
|
|
|
|
reg = pmic_reg_read(dev->parent, gpio_base + REG_STATUS);
|
|
|
|
if (reg < 0)
|
|
|
|
return reg;
|
|
|
|
|
|
|
|
return !!(reg & REG_STATUS_VAL_MASK);
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_set_value(struct udevice *dev, unsigned offset,
|
|
|
|
int value)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
2022-08-04 14:27:17 +00:00
|
|
|
struct qcom_gpio_bank *priv = dev_get_priv(dev);
|
2016-03-31 21:12:31 +00:00
|
|
|
uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
|
|
|
|
|
|
|
|
/* Set the output value of the gpio */
|
2022-08-04 14:27:18 +00:00
|
|
|
if (priv->lv_mv_type)
|
|
|
|
return pmic_clrsetbits(dev->parent,
|
|
|
|
gpio_base + REG_LV_MV_OUTPUT_CTL,
|
|
|
|
REG_LV_MV_OUTPUT_CTL_MASK,
|
|
|
|
!!value << REG_LV_MV_OUTPUT_CTL_SHIFT);
|
|
|
|
else
|
|
|
|
return pmic_clrsetbits(dev->parent, gpio_base + REG_CTL,
|
|
|
|
REG_CTL_OUTPUT_MASK, !!value);
|
2016-03-31 21:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static const struct dm_gpio_ops qcom_gpio_ops = {
|
|
|
|
.direction_input = qcom_gpio_direction_input,
|
|
|
|
.direction_output = qcom_gpio_direction_output,
|
|
|
|
.get_value = qcom_gpio_get_value,
|
|
|
|
.set_value = qcom_gpio_set_value,
|
|
|
|
.get_function = qcom_gpio_get_function,
|
2016-03-31 21:12:31 +00:00
|
|
|
};
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_probe(struct udevice *dev)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
2022-08-04 14:27:17 +00:00
|
|
|
struct qcom_gpio_bank *priv = dev_get_priv(dev);
|
2016-03-31 21:12:31 +00:00
|
|
|
int reg;
|
2023-12-05 13:46:46 +00:00
|
|
|
u64 pid;
|
2016-03-31 21:12:31 +00:00
|
|
|
|
2023-12-05 13:46:46 +00:00
|
|
|
pid = dev_read_addr(dev);
|
|
|
|
if (pid == FDT_ADDR_T_NONE)
|
2019-09-25 14:55:59 +00:00
|
|
|
return log_msg_ret("bad address", -EINVAL);
|
2016-03-31 21:12:31 +00:00
|
|
|
|
2023-12-05 13:46:46 +00:00
|
|
|
priv->pid = pid;
|
|
|
|
|
2016-03-31 21:12:31 +00:00
|
|
|
/* Do a sanity check */
|
|
|
|
reg = pmic_reg_read(dev->parent, priv->pid + REG_TYPE);
|
2022-08-04 14:27:18 +00:00
|
|
|
if (reg != REG_TYPE_VAL)
|
2019-09-25 14:55:59 +00:00
|
|
|
return log_msg_ret("bad type", -ENXIO);
|
2016-03-31 21:12:31 +00:00
|
|
|
|
|
|
|
reg = pmic_reg_read(dev->parent, priv->pid + REG_SUBTYPE);
|
2022-08-04 14:27:18 +00:00
|
|
|
if (reg != REG_SUBTYPE_GPIO_4CH && reg != REG_SUBTYPE_GPIOC_4CH &&
|
|
|
|
reg != REG_SUBTYPE_GPIO_LV && reg != REG_SUBTYPE_GPIO_MV)
|
2019-09-25 14:55:59 +00:00
|
|
|
return log_msg_ret("bad subtype", -ENXIO);
|
2016-03-31 21:12:31 +00:00
|
|
|
|
2022-08-04 14:27:18 +00:00
|
|
|
priv->lv_mv_type = reg == REG_SUBTYPE_GPIO_LV ||
|
|
|
|
reg == REG_SUBTYPE_GPIO_MV;
|
|
|
|
|
2016-03-31 21:12:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-05 13:46:50 +00:00
|
|
|
/*
|
|
|
|
* Parse basic GPIO count specified via the gpio-ranges property
|
|
|
|
* as specified in Linux devicetrees
|
|
|
|
* Returns < 0 on error, otherwise gpio count
|
|
|
|
*/
|
|
|
|
static int qcom_gpio_of_parse_ranges(struct udevice *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct ofnode_phandle_args args;
|
|
|
|
|
|
|
|
ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "gpio-ranges",
|
|
|
|
NULL, 3, 0, &args);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("gpio-ranges", ret);
|
|
|
|
|
|
|
|
return args.args[2];
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static int qcom_gpio_of_to_plat(struct udevice *dev)
|
2016-03-31 21:12:31 +00:00
|
|
|
{
|
|
|
|
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
2023-12-05 13:46:50 +00:00
|
|
|
int ret;
|
2016-03-31 21:12:31 +00:00
|
|
|
|
2023-12-05 13:46:52 +00:00
|
|
|
ret = qcom_gpio_of_parse_ranges(dev);
|
|
|
|
if (ret > 0)
|
|
|
|
uc_priv->gpio_count = ret;
|
|
|
|
else
|
|
|
|
return ret;
|
2023-12-05 13:46:50 +00:00
|
|
|
|
|
|
|
uc_priv->bank_name = "pmic";
|
2016-03-31 21:12:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
static const struct udevice_id qcom_gpio_ids[] = {
|
2016-03-31 21:12:31 +00:00
|
|
|
{ .compatible = "qcom,pm8916-gpio" },
|
2018-01-10 10:33:51 +00:00
|
|
|
{ .compatible = "qcom,pm8994-gpio" }, /* 22 GPIO's */
|
2021-10-17 10:44:29 +00:00
|
|
|
{ .compatible = "qcom,pm8998-gpio" },
|
2022-08-04 14:27:18 +00:00
|
|
|
{ .compatible = "qcom,pms405-gpio" },
|
2016-03-31 21:12:31 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2022-08-04 14:27:17 +00:00
|
|
|
U_BOOT_DRIVER(qcom_pmic_gpio) = {
|
|
|
|
.name = "qcom_pmic_gpio",
|
2016-03-31 21:12:31 +00:00
|
|
|
.id = UCLASS_GPIO,
|
2022-08-04 14:27:17 +00:00
|
|
|
.of_match = qcom_gpio_ids,
|
|
|
|
.of_to_plat = qcom_gpio_of_to_plat,
|
|
|
|
.probe = qcom_gpio_probe,
|
|
|
|
.ops = &qcom_gpio_ops,
|
|
|
|
.priv_auto = sizeof(struct qcom_gpio_bank),
|
2016-03-31 21:12:31 +00:00
|
|
|
};
|
|
|
|
|