// SPDX-License-Identifier: GPL-2.0+
/*
 *  Copyright (C) 2015 Samsung Electronics
 *
 *  Przemyslaw Marczak <p.marczak@samsung.com>
 */

#include <clk.h>
#include <errno.h>
#include <dm.h>
#include <linux/delay.h>
#include <log.h>
#include <asm/gpio.h>
#include <power/pmic.h>
#include <power/regulator.h>

#include "regulator_common.h"

struct fixed_clock_regulator_priv {
	struct clk *enable_clock;
	unsigned int clk_enable_counter;
};

static int fixed_regulator_of_to_plat(struct udevice *dev)
{
	struct dm_regulator_uclass_plat *uc_pdata;
	struct regulator_common_plat *plat;
	bool gpios;

	plat = dev_get_plat(dev);
	uc_pdata = dev_get_uclass_plat(dev);
	if (!uc_pdata)
		return -ENXIO;

	uc_pdata->type = REGULATOR_TYPE_FIXED;

	gpios = dev_read_bool(dev, "gpios");
	return regulator_common_of_to_plat(dev, plat, gpios ? "gpios" : "gpio");
}

static int fixed_regulator_get_value(struct udevice *dev)
{
	struct dm_regulator_uclass_plat *uc_pdata;

	uc_pdata = dev_get_uclass_plat(dev);
	if (!uc_pdata)
		return -ENXIO;

	if (uc_pdata->min_uV != uc_pdata->max_uV) {
		debug("Invalid constraints for: %s\n", uc_pdata->name);
		return -EINVAL;
	}

	return uc_pdata->min_uV;
}

static int fixed_regulator_get_current(struct udevice *dev)
{
	struct dm_regulator_uclass_plat *uc_pdata;

	uc_pdata = dev_get_uclass_plat(dev);
	if (!uc_pdata)
		return -ENXIO;

	if (uc_pdata->min_uA != uc_pdata->max_uA) {
		debug("Invalid constraints for: %s\n", uc_pdata->name);
		return -EINVAL;
	}

	return uc_pdata->min_uA;
}

static int fixed_regulator_get_enable(struct udevice *dev)
{
	return regulator_common_get_enable(dev, dev_get_plat(dev));
}

static int fixed_regulator_set_enable(struct udevice *dev, bool enable)
{
	return regulator_common_set_enable(dev, dev_get_plat(dev), enable);
}

static int fixed_clock_regulator_get_enable(struct udevice *dev)
{
	struct fixed_clock_regulator_priv *priv = dev_get_priv(dev);

	return priv->clk_enable_counter > 0;
}

static int fixed_clock_regulator_set_enable(struct udevice *dev, bool enable)
{
	struct fixed_clock_regulator_priv *priv = dev_get_priv(dev);
	struct regulator_common_plat *plat = dev_get_plat(dev);
	int ret = 0;

	if (enable) {
		ret = clk_enable(priv->enable_clock);
		priv->clk_enable_counter++;
	} else {
		ret = clk_disable(priv->enable_clock);
		priv->clk_enable_counter--;
	}
	if (ret)
		return ret;

	if (enable && plat->startup_delay_us)
		udelay(plat->startup_delay_us);

	if (!enable && plat->off_on_delay_us)
		udelay(plat->off_on_delay_us);

	return ret;
}

static int fixed_clock_regulator_probe(struct udevice *dev)
{
	struct fixed_clock_regulator_priv *priv = dev_get_priv(dev);

	priv->enable_clock = devm_clk_get(dev, NULL);
	if (IS_ERR(priv->enable_clock))
		return PTR_ERR(priv->enable_clock);

	return 0;
}

static const struct dm_regulator_ops fixed_regulator_ops = {
	.get_value	= fixed_regulator_get_value,
	.get_current	= fixed_regulator_get_current,
	.get_enable	= fixed_regulator_get_enable,
	.set_enable	= fixed_regulator_set_enable,
};

static const struct dm_regulator_ops fixed_clock_regulator_ops = {
	.get_enable	= fixed_clock_regulator_get_enable,
	.set_enable	= fixed_clock_regulator_set_enable,
};

static const struct udevice_id fixed_regulator_ids[] = {
	{ .compatible = "regulator-fixed" },
	{ },
};

static const struct udevice_id fixed_clock_regulator_ids[] = {
	{ .compatible = "regulator-fixed-clock" },
	{ },
};

U_BOOT_DRIVER(regulator_fixed) = {
	.name = "regulator_fixed",
	.id = UCLASS_REGULATOR,
	.ops = &fixed_regulator_ops,
	.of_match = fixed_regulator_ids,
	.of_to_plat = fixed_regulator_of_to_plat,
	.plat_auto = sizeof(struct regulator_common_plat),
};

U_BOOT_DRIVER(regulator_fixed_clock) = {
	.name = "regulator_fixed_clk",
	.id = UCLASS_REGULATOR,
	.ops = &fixed_clock_regulator_ops,
	.of_match = fixed_clock_regulator_ids,
	.probe = fixed_clock_regulator_probe,
	.of_to_plat = fixed_regulator_of_to_plat,
	.plat_auto = sizeof(struct regulator_common_plat),
	.priv_auto = sizeof(struct fixed_clock_regulator_priv),
};
