162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Regulator driver for Ricoh RN5T618 PMIC
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/mfd/rn5t618.h>
962306a36Sopenharmony_ci#include <linux/module.h>
1062306a36Sopenharmony_ci#include <linux/of.h>
1162306a36Sopenharmony_ci#include <linux/platform_device.h>
1262306a36Sopenharmony_ci#include <linux/regmap.h>
1362306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1462306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistatic const struct regulator_ops rn5t618_reg_ops = {
1762306a36Sopenharmony_ci	.enable			= regulator_enable_regmap,
1862306a36Sopenharmony_ci	.disable		= regulator_disable_regmap,
1962306a36Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
2062306a36Sopenharmony_ci	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
2162306a36Sopenharmony_ci	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
2262306a36Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
2362306a36Sopenharmony_ci};
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define REG(rid, ereg, emask, vreg, vmask, min, max, step)		\
2662306a36Sopenharmony_ci	{								\
2762306a36Sopenharmony_ci		.name		= #rid,					\
2862306a36Sopenharmony_ci		.of_match	= of_match_ptr(#rid),			\
2962306a36Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),		\
3062306a36Sopenharmony_ci		.id		= RN5T618_##rid,			\
3162306a36Sopenharmony_ci		.type		= REGULATOR_VOLTAGE,			\
3262306a36Sopenharmony_ci		.owner		= THIS_MODULE,				\
3362306a36Sopenharmony_ci		.ops		= &rn5t618_reg_ops,			\
3462306a36Sopenharmony_ci		.n_voltages	= ((max) - (min)) / (step) + 1,		\
3562306a36Sopenharmony_ci		.min_uV		= (min),				\
3662306a36Sopenharmony_ci		.uV_step	= (step),				\
3762306a36Sopenharmony_ci		.enable_reg	= RN5T618_##ereg,			\
3862306a36Sopenharmony_ci		.enable_mask	= (emask),				\
3962306a36Sopenharmony_ci		.vsel_reg	= RN5T618_##vreg,			\
4062306a36Sopenharmony_ci		.vsel_mask	= (vmask),				\
4162306a36Sopenharmony_ci	}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic const struct regulator_desc rn5t567_regulators[] = {
4462306a36Sopenharmony_ci	/* DCDC */
4562306a36Sopenharmony_ci	REG(DCDC1, DC1CTL, BIT(0), DC1DAC, 0xff, 600000, 3500000, 12500),
4662306a36Sopenharmony_ci	REG(DCDC2, DC2CTL, BIT(0), DC2DAC, 0xff, 600000, 3500000, 12500),
4762306a36Sopenharmony_ci	REG(DCDC3, DC3CTL, BIT(0), DC3DAC, 0xff, 600000, 3500000, 12500),
4862306a36Sopenharmony_ci	REG(DCDC4, DC4CTL, BIT(0), DC4DAC, 0xff, 600000, 3500000, 12500),
4962306a36Sopenharmony_ci	/* LDO */
5062306a36Sopenharmony_ci	REG(LDO1, LDOEN1, BIT(0), LDO1DAC, 0x7f, 900000, 3500000, 25000),
5162306a36Sopenharmony_ci	REG(LDO2, LDOEN1, BIT(1), LDO2DAC, 0x7f, 900000, 3500000, 25000),
5262306a36Sopenharmony_ci	REG(LDO3, LDOEN1, BIT(2), LDO3DAC, 0x7f, 600000, 3500000, 25000),
5362306a36Sopenharmony_ci	REG(LDO4, LDOEN1, BIT(3), LDO4DAC, 0x7f, 900000, 3500000, 25000),
5462306a36Sopenharmony_ci	REG(LDO5, LDOEN1, BIT(4), LDO5DAC, 0x7f, 900000, 3500000, 25000),
5562306a36Sopenharmony_ci	/* LDO RTC */
5662306a36Sopenharmony_ci	REG(LDORTC1, LDOEN2, BIT(4), LDORTCDAC, 0x7f, 1200000, 3500000, 25000),
5762306a36Sopenharmony_ci	REG(LDORTC2, LDOEN2, BIT(5), LDORTC2DAC, 0x7f, 900000, 3500000, 25000),
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic const struct regulator_desc rn5t618_regulators[] = {
6162306a36Sopenharmony_ci	/* DCDC */
6262306a36Sopenharmony_ci	REG(DCDC1, DC1CTL, BIT(0), DC1DAC, 0xff, 600000, 3500000, 12500),
6362306a36Sopenharmony_ci	REG(DCDC2, DC2CTL, BIT(0), DC2DAC, 0xff, 600000, 3500000, 12500),
6462306a36Sopenharmony_ci	REG(DCDC3, DC3CTL, BIT(0), DC3DAC, 0xff, 600000, 3500000, 12500),
6562306a36Sopenharmony_ci	/* LDO */
6662306a36Sopenharmony_ci	REG(LDO1, LDOEN1, BIT(0), LDO1DAC, 0x7f, 900000, 3500000, 25000),
6762306a36Sopenharmony_ci	REG(LDO2, LDOEN1, BIT(1), LDO2DAC, 0x7f, 900000, 3500000, 25000),
6862306a36Sopenharmony_ci	REG(LDO3, LDOEN1, BIT(2), LDO3DAC, 0x7f, 600000, 3500000, 25000),
6962306a36Sopenharmony_ci	REG(LDO4, LDOEN1, BIT(3), LDO4DAC, 0x7f, 900000, 3500000, 25000),
7062306a36Sopenharmony_ci	REG(LDO5, LDOEN1, BIT(4), LDO5DAC, 0x7f, 900000, 3500000, 25000),
7162306a36Sopenharmony_ci	/* LDO RTC */
7262306a36Sopenharmony_ci	REG(LDORTC1, LDOEN2, BIT(4), LDORTCDAC, 0x7f, 1700000, 3500000, 25000),
7362306a36Sopenharmony_ci	REG(LDORTC2, LDOEN2, BIT(5), LDORTC2DAC, 0x7f, 900000, 3500000, 25000),
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic const struct regulator_desc rc5t619_regulators[] = {
7762306a36Sopenharmony_ci	/* DCDC */
7862306a36Sopenharmony_ci	REG(DCDC1, DC1CTL, BIT(0), DC1DAC, 0xff, 600000, 3500000, 12500),
7962306a36Sopenharmony_ci	REG(DCDC2, DC2CTL, BIT(0), DC2DAC, 0xff, 600000, 3500000, 12500),
8062306a36Sopenharmony_ci	REG(DCDC3, DC3CTL, BIT(0), DC3DAC, 0xff, 600000, 3500000, 12500),
8162306a36Sopenharmony_ci	REG(DCDC4, DC4CTL, BIT(0), DC4DAC, 0xff, 600000, 3500000, 12500),
8262306a36Sopenharmony_ci	REG(DCDC5, DC5CTL, BIT(0), DC5DAC, 0xff, 600000, 3500000, 12500),
8362306a36Sopenharmony_ci	/* LDO */
8462306a36Sopenharmony_ci	REG(LDO1, LDOEN1, BIT(0), LDO1DAC, 0x7f, 900000, 3500000, 25000),
8562306a36Sopenharmony_ci	REG(LDO2, LDOEN1, BIT(1), LDO2DAC, 0x7f, 900000, 3500000, 25000),
8662306a36Sopenharmony_ci	REG(LDO3, LDOEN1, BIT(2), LDO3DAC, 0x7f, 900000, 3500000, 25000),
8762306a36Sopenharmony_ci	REG(LDO4, LDOEN1, BIT(3), LDO4DAC, 0x7f, 900000, 3500000, 25000),
8862306a36Sopenharmony_ci	REG(LDO5, LDOEN1, BIT(4), LDO5DAC, 0x7f, 600000, 3500000, 25000),
8962306a36Sopenharmony_ci	REG(LDO6, LDOEN1, BIT(5), LDO6DAC, 0x7f, 600000, 3500000, 25000),
9062306a36Sopenharmony_ci	REG(LDO7, LDOEN1, BIT(6), LDO7DAC, 0x7f, 900000, 3500000, 25000),
9162306a36Sopenharmony_ci	REG(LDO8, LDOEN1, BIT(7), LDO8DAC, 0x7f, 900000, 3500000, 25000),
9262306a36Sopenharmony_ci	REG(LDO9, LDOEN2, BIT(0), LDO9DAC, 0x7f, 900000, 3500000, 25000),
9362306a36Sopenharmony_ci	REG(LDO10, LDOEN2, BIT(1), LDO10DAC, 0x7f, 900000, 3500000, 25000),
9462306a36Sopenharmony_ci	/* LDO RTC */
9562306a36Sopenharmony_ci	REG(LDORTC1, LDOEN2, BIT(4), LDORTCDAC, 0x7f, 1700000, 3500000, 25000),
9662306a36Sopenharmony_ci	REG(LDORTC2, LDOEN2, BIT(5), LDORTC2DAC, 0x7f, 900000, 3500000, 25000),
9762306a36Sopenharmony_ci};
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistatic int rn5t618_regulator_probe(struct platform_device *pdev)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
10262306a36Sopenharmony_ci	struct regulator_config config = { };
10362306a36Sopenharmony_ci	struct regulator_dev *rdev;
10462306a36Sopenharmony_ci	const struct regulator_desc *regulators;
10562306a36Sopenharmony_ci	int i;
10662306a36Sopenharmony_ci	int num_regulators = 0;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	switch (rn5t618->variant) {
10962306a36Sopenharmony_ci	case RN5T567:
11062306a36Sopenharmony_ci		regulators = rn5t567_regulators;
11162306a36Sopenharmony_ci		num_regulators = ARRAY_SIZE(rn5t567_regulators);
11262306a36Sopenharmony_ci		break;
11362306a36Sopenharmony_ci	case RN5T618:
11462306a36Sopenharmony_ci		regulators = rn5t618_regulators;
11562306a36Sopenharmony_ci		num_regulators = ARRAY_SIZE(rn5t618_regulators);
11662306a36Sopenharmony_ci		break;
11762306a36Sopenharmony_ci	case RC5T619:
11862306a36Sopenharmony_ci		regulators = rc5t619_regulators;
11962306a36Sopenharmony_ci		num_regulators = ARRAY_SIZE(rc5t619_regulators);
12062306a36Sopenharmony_ci		break;
12162306a36Sopenharmony_ci	default:
12262306a36Sopenharmony_ci		return -EINVAL;
12362306a36Sopenharmony_ci	}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	config.dev = pdev->dev.parent;
12662306a36Sopenharmony_ci	config.regmap = rn5t618->regmap;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	for (i = 0; i < num_regulators; i++) {
12962306a36Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev,
13062306a36Sopenharmony_ci					       &regulators[i],
13162306a36Sopenharmony_ci					       &config);
13262306a36Sopenharmony_ci		if (IS_ERR(rdev)) {
13362306a36Sopenharmony_ci			dev_err(&pdev->dev, "failed to register %s regulator\n",
13462306a36Sopenharmony_ci				regulators[i].name);
13562306a36Sopenharmony_ci			return PTR_ERR(rdev);
13662306a36Sopenharmony_ci		}
13762306a36Sopenharmony_ci	}
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	return 0;
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic struct platform_driver rn5t618_regulator_driver = {
14362306a36Sopenharmony_ci	.probe = rn5t618_regulator_probe,
14462306a36Sopenharmony_ci	.driver = {
14562306a36Sopenharmony_ci		.name	= "rn5t618-regulator",
14662306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
14762306a36Sopenharmony_ci	},
14862306a36Sopenharmony_ci};
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_cimodule_platform_driver(rn5t618_regulator_driver);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciMODULE_ALIAS("platform:rn5t618-regulator");
15362306a36Sopenharmony_ciMODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
15462306a36Sopenharmony_ciMODULE_DESCRIPTION("RN5T618 regulator driver");
15562306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
156