162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2017 Spreadtrum Communications Inc.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/module.h>
762306a36Sopenharmony_ci#include <linux/of.h>
862306a36Sopenharmony_ci#include <linux/platform_device.h>
962306a36Sopenharmony_ci#include <linux/regmap.h>
1062306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1162306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * SC2731 regulator lock register
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci#define SC2731_PWR_WR_PROT		0xf0c
1762306a36Sopenharmony_ci#define SC2731_WR_UNLOCK_VALUE		0x6e7f
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/*
2062306a36Sopenharmony_ci * SC2731 enable register
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#define SC2731_POWER_PD_SW		0xc28
2362306a36Sopenharmony_ci#define SC2731_LDO_CAMA0_PD		0xcfc
2462306a36Sopenharmony_ci#define SC2731_LDO_CAMA1_PD		0xd04
2562306a36Sopenharmony_ci#define SC2731_LDO_CAMMOT_PD		0xd0c
2662306a36Sopenharmony_ci#define SC2731_LDO_VLDO_PD		0xd6c
2762306a36Sopenharmony_ci#define SC2731_LDO_EMMCCORE_PD		0xd2c
2862306a36Sopenharmony_ci#define SC2731_LDO_SDCORE_PD		0xd74
2962306a36Sopenharmony_ci#define SC2731_LDO_SDIO_PD		0xd70
3062306a36Sopenharmony_ci#define SC2731_LDO_WIFIPA_PD		0xd4c
3162306a36Sopenharmony_ci#define SC2731_LDO_USB33_PD		0xd5c
3262306a36Sopenharmony_ci#define SC2731_LDO_CAMD0_PD		0xd7c
3362306a36Sopenharmony_ci#define SC2731_LDO_CAMD1_PD		0xd84
3462306a36Sopenharmony_ci#define SC2731_LDO_CON_PD		0xd8c
3562306a36Sopenharmony_ci#define SC2731_LDO_CAMIO_PD		0xd94
3662306a36Sopenharmony_ci#define SC2731_LDO_SRAM_PD		0xd78
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/*
3962306a36Sopenharmony_ci * SC2731 enable mask
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_ci#define SC2731_DCDC_CPU0_PD_MASK	BIT(4)
4262306a36Sopenharmony_ci#define SC2731_DCDC_CPU1_PD_MASK	BIT(3)
4362306a36Sopenharmony_ci#define SC2731_DCDC_RF_PD_MASK		BIT(11)
4462306a36Sopenharmony_ci#define SC2731_LDO_CAMA0_PD_MASK	BIT(0)
4562306a36Sopenharmony_ci#define SC2731_LDO_CAMA1_PD_MASK	BIT(0)
4662306a36Sopenharmony_ci#define SC2731_LDO_CAMMOT_PD_MASK	BIT(0)
4762306a36Sopenharmony_ci#define SC2731_LDO_VLDO_PD_MASK		BIT(0)
4862306a36Sopenharmony_ci#define SC2731_LDO_EMMCCORE_PD_MASK	BIT(0)
4962306a36Sopenharmony_ci#define SC2731_LDO_SDCORE_PD_MASK	BIT(0)
5062306a36Sopenharmony_ci#define SC2731_LDO_SDIO_PD_MASK		BIT(0)
5162306a36Sopenharmony_ci#define SC2731_LDO_WIFIPA_PD_MASK	BIT(0)
5262306a36Sopenharmony_ci#define SC2731_LDO_USB33_PD_MASK	BIT(0)
5362306a36Sopenharmony_ci#define SC2731_LDO_CAMD0_PD_MASK	BIT(0)
5462306a36Sopenharmony_ci#define SC2731_LDO_CAMD1_PD_MASK	BIT(0)
5562306a36Sopenharmony_ci#define SC2731_LDO_CON_PD_MASK		BIT(0)
5662306a36Sopenharmony_ci#define SC2731_LDO_CAMIO_PD_MASK	BIT(0)
5762306a36Sopenharmony_ci#define SC2731_LDO_SRAM_PD_MASK		BIT(0)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * SC2731 vsel register
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_ci#define SC2731_DCDC_CPU0_VOL		0xc54
6362306a36Sopenharmony_ci#define SC2731_DCDC_CPU1_VOL		0xc64
6462306a36Sopenharmony_ci#define SC2731_DCDC_RF_VOL		0xcb8
6562306a36Sopenharmony_ci#define SC2731_LDO_CAMA0_VOL		0xd00
6662306a36Sopenharmony_ci#define SC2731_LDO_CAMA1_VOL		0xd08
6762306a36Sopenharmony_ci#define SC2731_LDO_CAMMOT_VOL		0xd10
6862306a36Sopenharmony_ci#define SC2731_LDO_VLDO_VOL		0xd28
6962306a36Sopenharmony_ci#define SC2731_LDO_EMMCCORE_VOL		0xd30
7062306a36Sopenharmony_ci#define SC2731_LDO_SDCORE_VOL		0xd38
7162306a36Sopenharmony_ci#define SC2731_LDO_SDIO_VOL		0xd40
7262306a36Sopenharmony_ci#define SC2731_LDO_WIFIPA_VOL		0xd50
7362306a36Sopenharmony_ci#define SC2731_LDO_USB33_VOL		0xd60
7462306a36Sopenharmony_ci#define SC2731_LDO_CAMD0_VOL		0xd80
7562306a36Sopenharmony_ci#define SC2731_LDO_CAMD1_VOL		0xd88
7662306a36Sopenharmony_ci#define SC2731_LDO_CON_VOL		0xd90
7762306a36Sopenharmony_ci#define SC2731_LDO_CAMIO_VOL		0xd98
7862306a36Sopenharmony_ci#define SC2731_LDO_SRAM_VOL		0xdB0
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/*
8162306a36Sopenharmony_ci * SC2731 vsel register mask
8262306a36Sopenharmony_ci */
8362306a36Sopenharmony_ci#define SC2731_DCDC_CPU0_VOL_MASK	GENMASK(8, 0)
8462306a36Sopenharmony_ci#define SC2731_DCDC_CPU1_VOL_MASK	GENMASK(8, 0)
8562306a36Sopenharmony_ci#define SC2731_DCDC_RF_VOL_MASK		GENMASK(8, 0)
8662306a36Sopenharmony_ci#define SC2731_LDO_CAMA0_VOL_MASK	GENMASK(7, 0)
8762306a36Sopenharmony_ci#define SC2731_LDO_CAMA1_VOL_MASK	GENMASK(7, 0)
8862306a36Sopenharmony_ci#define SC2731_LDO_CAMMOT_VOL_MASK	GENMASK(7, 0)
8962306a36Sopenharmony_ci#define SC2731_LDO_VLDO_VOL_MASK	GENMASK(7, 0)
9062306a36Sopenharmony_ci#define SC2731_LDO_EMMCCORE_VOL_MASK	GENMASK(7, 0)
9162306a36Sopenharmony_ci#define SC2731_LDO_SDCORE_VOL_MASK	GENMASK(7, 0)
9262306a36Sopenharmony_ci#define SC2731_LDO_SDIO_VOL_MASK	GENMASK(7, 0)
9362306a36Sopenharmony_ci#define SC2731_LDO_WIFIPA_VOL_MASK	GENMASK(7, 0)
9462306a36Sopenharmony_ci#define SC2731_LDO_USB33_VOL_MASK	GENMASK(7, 0)
9562306a36Sopenharmony_ci#define SC2731_LDO_CAMD0_VOL_MASK	GENMASK(6, 0)
9662306a36Sopenharmony_ci#define SC2731_LDO_CAMD1_VOL_MASK	GENMASK(6, 0)
9762306a36Sopenharmony_ci#define SC2731_LDO_CON_VOL_MASK		GENMASK(6, 0)
9862306a36Sopenharmony_ci#define SC2731_LDO_CAMIO_VOL_MASK	GENMASK(6, 0)
9962306a36Sopenharmony_ci#define SC2731_LDO_SRAM_VOL_MASK	GENMASK(6, 0)
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cienum sc2731_regulator_id {
10262306a36Sopenharmony_ci	SC2731_BUCK_CPU0,
10362306a36Sopenharmony_ci	SC2731_BUCK_CPU1,
10462306a36Sopenharmony_ci	SC2731_BUCK_RF,
10562306a36Sopenharmony_ci	SC2731_LDO_CAMA0,
10662306a36Sopenharmony_ci	SC2731_LDO_CAMA1,
10762306a36Sopenharmony_ci	SC2731_LDO_CAMMOT,
10862306a36Sopenharmony_ci	SC2731_LDO_VLDO,
10962306a36Sopenharmony_ci	SC2731_LDO_EMMCCORE,
11062306a36Sopenharmony_ci	SC2731_LDO_SDCORE,
11162306a36Sopenharmony_ci	SC2731_LDO_SDIO,
11262306a36Sopenharmony_ci	SC2731_LDO_WIFIPA,
11362306a36Sopenharmony_ci	SC2731_LDO_USB33,
11462306a36Sopenharmony_ci	SC2731_LDO_CAMD0,
11562306a36Sopenharmony_ci	SC2731_LDO_CAMD1,
11662306a36Sopenharmony_ci	SC2731_LDO_CON,
11762306a36Sopenharmony_ci	SC2731_LDO_CAMIO,
11862306a36Sopenharmony_ci	SC2731_LDO_SRAM,
11962306a36Sopenharmony_ci};
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistatic const struct regulator_ops sc2731_regu_linear_ops = {
12262306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
12362306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
12462306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
12562306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
12662306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
12762306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#define SC2731_REGU_LINEAR(_id, en_reg, en_mask, vreg, vmask,	\
13162306a36Sopenharmony_ci			  vstep, vmin, vmax) {			\
13262306a36Sopenharmony_ci	.name			= #_id,				\
13362306a36Sopenharmony_ci	.of_match		= of_match_ptr(#_id),		\
13462306a36Sopenharmony_ci	.ops			= &sc2731_regu_linear_ops,	\
13562306a36Sopenharmony_ci	.type			= REGULATOR_VOLTAGE,		\
13662306a36Sopenharmony_ci	.id			= SC2731_##_id,			\
13762306a36Sopenharmony_ci	.owner			= THIS_MODULE,			\
13862306a36Sopenharmony_ci	.min_uV			= vmin,				\
13962306a36Sopenharmony_ci	.n_voltages		= ((vmax) - (vmin)) / (vstep) + 1,	\
14062306a36Sopenharmony_ci	.uV_step		= vstep,			\
14162306a36Sopenharmony_ci	.enable_is_inverted	= true,				\
14262306a36Sopenharmony_ci	.enable_val		= 0,				\
14362306a36Sopenharmony_ci	.enable_reg		= en_reg,			\
14462306a36Sopenharmony_ci	.enable_mask		= en_mask,			\
14562306a36Sopenharmony_ci	.vsel_reg		= vreg,				\
14662306a36Sopenharmony_ci	.vsel_mask		= vmask,			\
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistatic const struct regulator_desc regulators[] = {
15062306a36Sopenharmony_ci	SC2731_REGU_LINEAR(BUCK_CPU0, SC2731_POWER_PD_SW,
15162306a36Sopenharmony_ci			   SC2731_DCDC_CPU0_PD_MASK, SC2731_DCDC_CPU0_VOL,
15262306a36Sopenharmony_ci			   SC2731_DCDC_CPU0_VOL_MASK, 3125, 400000, 1996875),
15362306a36Sopenharmony_ci	SC2731_REGU_LINEAR(BUCK_CPU1, SC2731_POWER_PD_SW,
15462306a36Sopenharmony_ci			   SC2731_DCDC_CPU1_PD_MASK, SC2731_DCDC_CPU1_VOL,
15562306a36Sopenharmony_ci			   SC2731_DCDC_CPU1_VOL_MASK, 3125, 400000, 1996875),
15662306a36Sopenharmony_ci	SC2731_REGU_LINEAR(BUCK_RF, SC2731_POWER_PD_SW, SC2731_DCDC_RF_PD_MASK,
15762306a36Sopenharmony_ci			   SC2731_DCDC_RF_VOL, SC2731_DCDC_RF_VOL_MASK,
15862306a36Sopenharmony_ci			   3125, 600000, 2196875),
15962306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_CAMA0, SC2731_LDO_CAMA0_PD,
16062306a36Sopenharmony_ci			   SC2731_LDO_CAMA0_PD_MASK, SC2731_LDO_CAMA0_VOL,
16162306a36Sopenharmony_ci			   SC2731_LDO_CAMA0_VOL_MASK, 10000, 1200000, 3750000),
16262306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_CAMA1, SC2731_LDO_CAMA1_PD,
16362306a36Sopenharmony_ci			   SC2731_LDO_CAMA1_PD_MASK, SC2731_LDO_CAMA1_VOL,
16462306a36Sopenharmony_ci			   SC2731_LDO_CAMA1_VOL_MASK, 10000, 1200000, 3750000),
16562306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_CAMMOT, SC2731_LDO_CAMMOT_PD,
16662306a36Sopenharmony_ci			   SC2731_LDO_CAMMOT_PD_MASK, SC2731_LDO_CAMMOT_VOL,
16762306a36Sopenharmony_ci			   SC2731_LDO_CAMMOT_VOL_MASK, 10000, 1200000, 3750000),
16862306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_VLDO, SC2731_LDO_VLDO_PD,
16962306a36Sopenharmony_ci			   SC2731_LDO_VLDO_PD_MASK, SC2731_LDO_VLDO_VOL,
17062306a36Sopenharmony_ci			   SC2731_LDO_VLDO_VOL_MASK, 10000, 1200000, 3750000),
17162306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_EMMCCORE, SC2731_LDO_EMMCCORE_PD,
17262306a36Sopenharmony_ci			   SC2731_LDO_EMMCCORE_PD_MASK, SC2731_LDO_EMMCCORE_VOL,
17362306a36Sopenharmony_ci			   SC2731_LDO_EMMCCORE_VOL_MASK, 10000, 1200000,
17462306a36Sopenharmony_ci			   3750000),
17562306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_SDCORE, SC2731_LDO_SDCORE_PD,
17662306a36Sopenharmony_ci			   SC2731_LDO_SDCORE_PD_MASK, SC2731_LDO_SDCORE_VOL,
17762306a36Sopenharmony_ci			   SC2731_LDO_SDCORE_VOL_MASK, 10000, 1200000, 3750000),
17862306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_SDIO, SC2731_LDO_SDIO_PD,
17962306a36Sopenharmony_ci			   SC2731_LDO_SDIO_PD_MASK, SC2731_LDO_SDIO_VOL,
18062306a36Sopenharmony_ci			   SC2731_LDO_SDIO_VOL_MASK, 10000, 1200000, 3750000),
18162306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_WIFIPA, SC2731_LDO_WIFIPA_PD,
18262306a36Sopenharmony_ci			   SC2731_LDO_WIFIPA_PD_MASK, SC2731_LDO_WIFIPA_VOL,
18362306a36Sopenharmony_ci			   SC2731_LDO_WIFIPA_VOL_MASK, 10000, 1200000, 3750000),
18462306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_USB33, SC2731_LDO_USB33_PD,
18562306a36Sopenharmony_ci			   SC2731_LDO_USB33_PD_MASK, SC2731_LDO_USB33_VOL,
18662306a36Sopenharmony_ci			   SC2731_LDO_USB33_VOL_MASK, 10000, 1200000, 3750000),
18762306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_CAMD0, SC2731_LDO_CAMD0_PD,
18862306a36Sopenharmony_ci			   SC2731_LDO_CAMD0_PD_MASK, SC2731_LDO_CAMD0_VOL,
18962306a36Sopenharmony_ci			   SC2731_LDO_CAMD0_VOL_MASK, 6250, 1000000, 1793750),
19062306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_CAMD1, SC2731_LDO_CAMD1_PD,
19162306a36Sopenharmony_ci			   SC2731_LDO_CAMD1_PD_MASK, SC2731_LDO_CAMD1_VOL,
19262306a36Sopenharmony_ci			   SC2731_LDO_CAMD1_VOL_MASK, 6250, 1000000, 1793750),
19362306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_CON, SC2731_LDO_CON_PD,
19462306a36Sopenharmony_ci			   SC2731_LDO_CON_PD_MASK, SC2731_LDO_CON_VOL,
19562306a36Sopenharmony_ci			   SC2731_LDO_CON_VOL_MASK, 6250, 1000000, 1793750),
19662306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_CAMIO, SC2731_LDO_CAMIO_PD,
19762306a36Sopenharmony_ci			   SC2731_LDO_CAMIO_PD_MASK, SC2731_LDO_CAMIO_VOL,
19862306a36Sopenharmony_ci			   SC2731_LDO_CAMIO_VOL_MASK, 6250, 1000000, 1793750),
19962306a36Sopenharmony_ci	SC2731_REGU_LINEAR(LDO_SRAM, SC2731_LDO_SRAM_PD,
20062306a36Sopenharmony_ci			   SC2731_LDO_SRAM_PD_MASK, SC2731_LDO_SRAM_VOL,
20162306a36Sopenharmony_ci			   SC2731_LDO_SRAM_VOL_MASK, 6250, 1000000, 1793750),
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic int sc2731_regulator_unlock(struct regmap *regmap)
20562306a36Sopenharmony_ci{
20662306a36Sopenharmony_ci	return regmap_write(regmap, SC2731_PWR_WR_PROT,
20762306a36Sopenharmony_ci			    SC2731_WR_UNLOCK_VALUE);
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic int sc2731_regulator_probe(struct platform_device *pdev)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	int i, ret;
21362306a36Sopenharmony_ci	struct regmap *regmap;
21462306a36Sopenharmony_ci	struct regulator_config config = { };
21562306a36Sopenharmony_ci	struct regulator_dev *rdev;
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	regmap = dev_get_regmap(pdev->dev.parent, NULL);
21862306a36Sopenharmony_ci	if (!regmap) {
21962306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to get regmap.\n");
22062306a36Sopenharmony_ci		return -ENODEV;
22162306a36Sopenharmony_ci	}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	ret = sc2731_regulator_unlock(regmap);
22462306a36Sopenharmony_ci	if (ret) {
22562306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to release regulator lock\n");
22662306a36Sopenharmony_ci		return ret;
22762306a36Sopenharmony_ci	}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	config.dev = &pdev->dev;
23062306a36Sopenharmony_ci	config.regmap = regmap;
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
23362306a36Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
23462306a36Sopenharmony_ci					       &config);
23562306a36Sopenharmony_ci		if (IS_ERR(rdev)) {
23662306a36Sopenharmony_ci			dev_err(&pdev->dev, "failed to register regulator %s\n",
23762306a36Sopenharmony_ci				regulators[i].name);
23862306a36Sopenharmony_ci			return PTR_ERR(rdev);
23962306a36Sopenharmony_ci		}
24062306a36Sopenharmony_ci	}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	return 0;
24362306a36Sopenharmony_ci}
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_cistatic struct platform_driver sc2731_regulator_driver = {
24662306a36Sopenharmony_ci	.driver = {
24762306a36Sopenharmony_ci		.name = "sc27xx-regulator",
24862306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
24962306a36Sopenharmony_ci	},
25062306a36Sopenharmony_ci	.probe = sc2731_regulator_probe,
25162306a36Sopenharmony_ci};
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_cimodule_platform_driver(sc2731_regulator_driver);
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ciMODULE_AUTHOR("Chen Junhui <erick.chen@spreadtrum.com>");
25662306a36Sopenharmony_ciMODULE_DESCRIPTION("Spreadtrum SC2731 regulator driver");
25762306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
258