// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * NXP PCA9450 regulator driver
 * Copyright (C) 2022 Marek Vasut <marex@denx.de>
 *
 * Largely based on:
 * ROHM BD71837 regulator driver
 */

#include <asm-generic/gpio.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <log.h>
#include <linux/bitops.h>
#include <linux/err.h>
#include <power/pca9450.h>
#include <power/pmic.h>
#include <power/regulator.h>

#define HW_STATE_CONTROL 0
#define DEBUG

/**
 * struct pca9450_vrange - describe linear range of voltages
 *
 * @min_volt:	smallest voltage in range
 * @step:	how much voltage changes at each selector step
 * @min_sel:	smallest selector in the range
 * @max_sel:	maximum selector in the range
 */
struct pca9450_vrange {
	unsigned int	min_volt;
	unsigned int	step;
	u8		min_sel;
	u8		max_sel;
};

/**
 * struct pca9450_plat - describe regulator control registers
 *
 * @name:	name of the regulator. Used for matching the dt-entry
 * @enable_reg:	register address used to enable/disable regulator
 * @enablemask:	register mask used to enable/disable regulator
 * @volt_reg:	register address used to configure regulator voltage
 * @volt_mask:	register mask used to configure regulator voltage
 * @ranges:	pointer to ranges of regulator voltages and matching register
 *		values
 * @numranges:	number of voltage ranges pointed by ranges
 */
struct pca9450_plat {
	const char		*name;
	u8			enable_reg;
	u8			enablemask;
	u8			volt_reg;
	u8			volt_mask;
	struct pca9450_vrange	*ranges;
	unsigned int		numranges;
	struct gpio_desc	*sd_vsel_gpio;
	bool			sd_vsel_fixed_low;
};

#define PCA_RANGE(_min, _vstep, _sel_low, _sel_hi) \
{ \
	.min_volt = (_min), .step = (_vstep), \
	.min_sel = (_sel_low), .max_sel = (_sel_hi), \
}

#define PCA_DATA(_name, enreg, enmask, vreg, vmask, _range) \
{ \
	.name = (_name), .enable_reg = (enreg), .enablemask = (enmask), \
	.volt_reg = (vreg), .volt_mask = (vmask), .ranges = (_range), \
	.numranges = ARRAY_SIZE(_range) \
}

static struct pca9450_vrange pca9450_buck123_vranges[] = {
	PCA_RANGE(600000, 12500, 0, 0x7f),
};

static struct pca9450_vrange pca9450_trim_buck13_vranges[] = {
	PCA_RANGE(650000, 12500, 0, 0x7f),
};

static struct pca9450_vrange pca9450_buck456_vranges[] = {
	PCA_RANGE(600000, 25000, 0, 0x70),
	PCA_RANGE(3400000, 0, 0x71, 0x7f),
};

static struct pca9450_vrange pca9450_ldo1_vranges[] = {
	PCA_RANGE(1600000, 100000, 0x0, 0x3),
	PCA_RANGE(3000000, 100000, 0x4, 0x7),
};

static struct pca9450_vrange pca9450_ldo2_vranges[] = {
	PCA_RANGE(800000, 50000, 0x0, 0x7),
};

static struct pca9450_vrange pca9450_ldo34_vranges[] = {
	PCA_RANGE(800000, 100000, 0x0, 0x19),
	PCA_RANGE(3300000, 0, 0x1a, 0x1f),
};

static struct pca9450_vrange pca9450_ldo5_vranges[] = {
	PCA_RANGE(1800000, 100000, 0x0, 0xf),
};

/*
 * We use enable mask 'HW_STATE_CONTROL' to indicate that this regulator
 * must not be enabled or disabled by SW. The typical use-case for PCA9450
 * is powering NXP i.MX8. In this use-case we (for now) only allow control
 * for BUCK4, BUCK5, BUCK6 which are not boot critical.
 */
static struct pca9450_plat pca9450_reg_data[] = {
	/* Bucks 1-3 which support dynamic voltage scaling */
	PCA_DATA("BUCK1", PCA9450_BUCK1CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK1OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_buck123_vranges),
	PCA_DATA("BUCK1_TRIM", PCA9450_BUCK1CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK1OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_trim_buck13_vranges),
	PCA_DATA("BUCK2", PCA9450_BUCK2CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK2OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_buck123_vranges),
	PCA_DATA("BUCK3", PCA9450_BUCK3CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK3OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_buck123_vranges),
	PCA_DATA("BUCK3_TRIM", PCA9450_BUCK3CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK3OUT_DVS0, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_trim_buck13_vranges),
	/* Bucks 4-6 which do not support dynamic voltage scaling */
	PCA_DATA("BUCK4", PCA9450_BUCK4CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK4OUT, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_buck456_vranges),
	PCA_DATA("BUCK5", PCA9450_BUCK5CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK5OUT, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_buck456_vranges),
	PCA_DATA("BUCK6", PCA9450_BUCK6CTRL, HW_STATE_CONTROL,
		 PCA9450_BUCK6OUT, PCA9450_DVS_BUCK_RUN_MASK,
		 pca9450_buck456_vranges),
	/* LDOs */
	PCA_DATA("LDO1", PCA9450_LDO1CTRL, HW_STATE_CONTROL,
		 PCA9450_LDO1CTRL, PCA9450_LDO12_MASK,
		 pca9450_ldo1_vranges),
	PCA_DATA("LDO2", PCA9450_LDO2CTRL, HW_STATE_CONTROL,
		 PCA9450_LDO2CTRL, PCA9450_LDO12_MASK,
		 pca9450_ldo2_vranges),
	PCA_DATA("LDO3", PCA9450_LDO3CTRL, HW_STATE_CONTROL,
		 PCA9450_LDO3CTRL, PCA9450_LDO34_MASK,
		 pca9450_ldo34_vranges),
	PCA_DATA("LDO4", PCA9450_LDO4CTRL, HW_STATE_CONTROL,
		 PCA9450_LDO4CTRL, PCA9450_LDO34_MASK,
		 pca9450_ldo34_vranges),
	PCA_DATA("LDO5", PCA9450_LDO5CTRL_L, HW_STATE_CONTROL,
		 PCA9450_LDO5CTRL_H, PCA9450_LDO5_MASK,
		 pca9450_ldo5_vranges),
};

static int vrange_find_value(struct pca9450_vrange *r, unsigned int sel,
			     unsigned int *val)
{
	if (!val || sel < r->min_sel || sel > r->max_sel)
		return -EINVAL;

	*val = r->min_volt + r->step * (sel - r->min_sel);
	return 0;
}

static int vrange_find_selector(struct pca9450_vrange *r, int val,
				unsigned int *sel)
{
	int ret = -EINVAL;
	int num_vals = r->max_sel - r->min_sel + 1;

	if (val >= r->min_volt &&
	    val <= r->min_volt + r->step * (num_vals - 1)) {
		if (r->step) {
			*sel = r->min_sel + ((val - r->min_volt) / r->step);
			ret = 0;
		} else {
			*sel = r->min_sel;
			ret = 0;
		}
	}
	return ret;
}

static int pca9450_get_enable(struct udevice *dev)
{
	struct pca9450_plat *plat = dev_get_plat(dev);
	int val;

	/*
	 * boot critical regulators on pca9450 must not be controlled by sw
	 * due to the 'feature' which leaves power rails down if pca9450 is
	 * reseted to snvs state. hence we can't get the state here.
	 *
	 * if we are alive it means we probably are on run state and
	 * if the regulator can't be controlled we can assume it is
	 * enabled.
	 */
	if (plat->enablemask == HW_STATE_CONTROL)
		return 1;

	val = pmic_reg_read(dev->parent, plat->enable_reg);
	if (val < 0)
		return val;

	return (val & plat->enablemask);
}

static int pca9450_set_enable(struct udevice *dev, bool enable)
{
	int val = 0;
	struct pca9450_plat *plat = dev_get_plat(dev);

	/*
	 * boot critical regulators on pca9450 must not be controlled by sw
	 * due to the 'feature' which leaves power rails down if pca9450 is
	 * reseted to snvs state. Hence we can't set the state here.
	 */
	if (plat->enablemask == HW_STATE_CONTROL)
		return enable ? 0 : -EINVAL;

	if (enable)
		val = plat->enablemask;

	return pmic_clrsetbits(dev->parent, plat->enable_reg, plat->enablemask,
			       val);
}

static u8 pca9450_get_vsel_reg(struct pca9450_plat *plat)
{
	if (!strcmp(plat->name, "LDO5") &&
	    ((plat->sd_vsel_gpio && !dm_gpio_get_value(plat->sd_vsel_gpio)) ||
	     plat->sd_vsel_fixed_low)) {
		return PCA9450_LDO5CTRL_L;
	}

	return plat->volt_reg;
}

static int pca9450_get_value(struct udevice *dev)
{
	struct pca9450_plat *plat = dev_get_plat(dev);
	unsigned int reg, tmp;
	int i, ret;

	ret = pmic_reg_read(dev->parent, pca9450_get_vsel_reg(plat));
	if (ret < 0)
		return ret;

	reg = ret;
	reg &= plat->volt_mask;

	for (i = 0; i < plat->numranges; i++) {
		struct pca9450_vrange *r = &plat->ranges[i];

		if (!vrange_find_value(r, reg, &tmp))
			return tmp;
	}

	pr_err("Unknown voltage value read from pmic\n");

	return -EINVAL;
}

static int pca9450_set_value(struct udevice *dev, int uvolt)
{
	struct pca9450_plat *plat = dev_get_plat(dev);
	unsigned int sel;
	int i, found = 0;

	for (i = 0; i < plat->numranges; i++) {
		struct pca9450_vrange *r = &plat->ranges[i];

		found = !vrange_find_selector(r, uvolt, &sel);
		if (found) {
			unsigned int tmp;

			/*
			 * We require exactly the requested value to be
			 * supported - this can be changed later if needed
			 */
			found = !vrange_find_value(r, sel, &tmp);
			if (found && tmp == uvolt)
				break;
			found = 0;
		}
	}

	if (!found)
		return -EINVAL;

	return pmic_clrsetbits(dev->parent, pca9450_get_vsel_reg(plat),
			       plat->volt_mask, sel);
}

static int pca9450_regulator_probe(struct udevice *dev)
{
	struct pca9450_plat *plat = dev_get_plat(dev);
	int i, type, ret;
	unsigned int val;
	bool pmic_trim = false;

	type = dev_get_driver_data(dev_get_parent(dev));

	if (type != NXP_CHIP_TYPE_PCA9450A && type != NXP_CHIP_TYPE_PCA9450BC &&
	    type != NXP_CHIP_TYPE_PCA9451A && type != NXP_CHIP_TYPE_PCA9452) {
		debug("Unknown PMIC type\n");
		return -EINVAL;
	}

	ret = pmic_reg_read(dev->parent, PCA9450_PWR_CTRL);
	if (ret < 0)
		return ret;

	val = ret;

	if ((type == NXP_CHIP_TYPE_PCA9451A || type == NXP_CHIP_TYPE_PCA9452) &&
	    (val & PCA9450_REG_PWRCTRL_TOFF_DEB))
		pmic_trim = true;

	for (i = 0; i < ARRAY_SIZE(pca9450_reg_data); i++) {
		if (strcmp(dev->name, pca9450_reg_data[i].name))
			continue;

		if (pmic_trim && (!strcmp(pca9450_reg_data[i].name, "BUCK1") ||
				  !strcmp(pca9450_reg_data[i].name, "BUCK3"))) {
			*plat = pca9450_reg_data[i + 1];
			return 0;
		}

		/* PCA9450B/PCA9450C uses BUCK1 and BUCK3 in dual-phase */
		if (type == NXP_CHIP_TYPE_PCA9450BC &&
		    !strcmp(pca9450_reg_data[i].name, "BUCK3")) {
			continue;
		}

		/* PCA9451A uses BUCK3 in dual-phase and don't have LDO2 and LDO3 */
		if (type == NXP_CHIP_TYPE_PCA9451A &&
		    (!strcmp(pca9450_reg_data[i].name, "BUCK3") ||
		    !strcmp(pca9450_reg_data[i].name, "LDO2") ||
		    !strcmp(pca9450_reg_data[i].name, "LDO3"))) {
			continue;
		}

		if (type == NXP_CHIP_TYPE_PCA9452 &&
		    (!strcmp(pca9450_reg_data[i].name, "BUCK3") ||
		    !strcmp(pca9450_reg_data[i].name, "LDO2"))) {
			continue;
		}

		*plat = pca9450_reg_data[i];

		if (!strcmp(plat->name, "LDO5")) {
			if (CONFIG_IS_ENABLED(DM_GPIO) && CONFIG_IS_ENABLED(DM_REGULATOR_PCA9450)) {
				plat->sd_vsel_gpio = devm_gpiod_get_optional(dev, "sd-vsel",
									     GPIOD_IS_IN);
				if (IS_ERR(plat->sd_vsel_gpio)) {
					ret = PTR_ERR(plat->sd_vsel_gpio);
					dev_err(dev, "Failed to request SD_VSEL GPIO: %d\n", ret);
					if (ret)
						return ret;
				}
			}

			plat->sd_vsel_fixed_low = dev_read_bool(dev, "nxp,sd-vsel-fixed-low");
		}

		return 0;
	}

	pr_err("Unknown regulator '%s'\n", dev->name);

	return -ENOENT;
}

static const struct dm_regulator_ops pca9450_regulator_ops = {
	.get_value	= pca9450_get_value,
	.set_value	= pca9450_set_value,
	.get_enable	= pca9450_get_enable,
	.set_enable	= pca9450_set_enable,
};

U_BOOT_DRIVER(pca9450_regulator) = {
	.name		= PCA9450_REGULATOR_DRIVER,
	.id		= UCLASS_REGULATOR,
	.ops		= &pca9450_regulator_ops,
	.probe		= pca9450_regulator_probe,
	.plat_auto	= sizeof(struct pca9450_plat),
};
