xref: /openbmc/linux/drivers/regulator/tps68470-regulator.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
10fc31d8fSHans de Goede // SPDX-License-Identifier: GPL-2.0
20fc31d8fSHans de Goede //
30fc31d8fSHans de Goede // Regulator driver for TPS68470 PMIC
40fc31d8fSHans de Goede //
50fc31d8fSHans de Goede // Copyright (c) 2021 Red Hat Inc.
60fc31d8fSHans de Goede // Copyright (C) 2018 Intel Corporation
70fc31d8fSHans de Goede //
80fc31d8fSHans de Goede // Authors:
90fc31d8fSHans de Goede //	Hans de Goede <hdegoede@redhat.com>
100fc31d8fSHans de Goede //	Zaikuo Wang <zaikuo.wang@intel.com>
110fc31d8fSHans de Goede //	Tianshu Qiu <tian.shu.qiu@intel.com>
120fc31d8fSHans de Goede //	Jian Xu Zheng <jian.xu.zheng@intel.com>
130fc31d8fSHans de Goede //	Yuning Pu <yuning.pu@intel.com>
140fc31d8fSHans de Goede //	Rajmohan Mani <rajmohan.mani@intel.com>
150fc31d8fSHans de Goede 
160fc31d8fSHans de Goede #include <linux/clk.h>
170fc31d8fSHans de Goede #include <linux/device.h>
180fc31d8fSHans de Goede #include <linux/err.h>
190fc31d8fSHans de Goede #include <linux/init.h>
200fc31d8fSHans de Goede #include <linux/kernel.h>
210fc31d8fSHans de Goede #include <linux/mfd/tps68470.h>
220fc31d8fSHans de Goede #include <linux/module.h>
230fc31d8fSHans de Goede #include <linux/platform_data/tps68470.h>
240fc31d8fSHans de Goede #include <linux/platform_device.h>
250fc31d8fSHans de Goede #include <linux/regulator/driver.h>
260fc31d8fSHans de Goede #include <linux/regulator/machine.h>
270fc31d8fSHans de Goede 
280fc31d8fSHans de Goede struct tps68470_regulator_data {
290fc31d8fSHans de Goede 	struct clk *clk;
300fc31d8fSHans de Goede };
310fc31d8fSHans de Goede 
320fc31d8fSHans de Goede #define TPS68470_REGULATOR(_name, _id, _ops, _n,			\
330fc31d8fSHans de Goede 			   _vr, _vm, _er, _em, _lr, _nlr)		\
340fc31d8fSHans de Goede 	[TPS68470_ ## _name] = {					\
350fc31d8fSHans de Goede 		.name			= # _name,			\
360fc31d8fSHans de Goede 		.id			= _id,				\
370fc31d8fSHans de Goede 		.ops			= &_ops,			\
380fc31d8fSHans de Goede 		.n_voltages		= _n,				\
390fc31d8fSHans de Goede 		.type			= REGULATOR_VOLTAGE,		\
400fc31d8fSHans de Goede 		.owner			= THIS_MODULE,			\
410fc31d8fSHans de Goede 		.vsel_reg		= _vr,				\
420fc31d8fSHans de Goede 		.vsel_mask		= _vm,				\
430fc31d8fSHans de Goede 		.enable_reg		= _er,				\
440fc31d8fSHans de Goede 		.enable_mask		= _em,				\
450fc31d8fSHans de Goede 		.linear_ranges		= _lr,				\
460fc31d8fSHans de Goede 		.n_linear_ranges	= _nlr,				\
470fc31d8fSHans de Goede 	}
480fc31d8fSHans de Goede 
490fc31d8fSHans de Goede static const struct linear_range tps68470_ldo_ranges[] = {
500fc31d8fSHans de Goede 	REGULATOR_LINEAR_RANGE(875000, 0, 125, 17800),
510fc31d8fSHans de Goede };
520fc31d8fSHans de Goede 
530fc31d8fSHans de Goede static const struct linear_range tps68470_core_ranges[] = {
540fc31d8fSHans de Goede 	REGULATOR_LINEAR_RANGE(900000, 0, 42, 25000),
550fc31d8fSHans de Goede };
560fc31d8fSHans de Goede 
tps68470_regulator_enable(struct regulator_dev * rdev)570fc31d8fSHans de Goede static int tps68470_regulator_enable(struct regulator_dev *rdev)
580fc31d8fSHans de Goede {
590fc31d8fSHans de Goede 	struct tps68470_regulator_data *data = rdev->reg_data;
600fc31d8fSHans de Goede 	int ret;
610fc31d8fSHans de Goede 
620fc31d8fSHans de Goede 	/* The Core buck regulator needs the PMIC's PLL to be enabled */
630fc31d8fSHans de Goede 	if (rdev->desc->id == TPS68470_CORE) {
640fc31d8fSHans de Goede 		ret = clk_prepare_enable(data->clk);
650fc31d8fSHans de Goede 		if (ret) {
660fc31d8fSHans de Goede 			dev_err(&rdev->dev, "Error enabling TPS68470 clock\n");
670fc31d8fSHans de Goede 			return ret;
680fc31d8fSHans de Goede 		}
690fc31d8fSHans de Goede 	}
700fc31d8fSHans de Goede 
710fc31d8fSHans de Goede 	return regulator_enable_regmap(rdev);
720fc31d8fSHans de Goede }
730fc31d8fSHans de Goede 
tps68470_regulator_disable(struct regulator_dev * rdev)740fc31d8fSHans de Goede static int tps68470_regulator_disable(struct regulator_dev *rdev)
750fc31d8fSHans de Goede {
760fc31d8fSHans de Goede 	struct tps68470_regulator_data *data = rdev->reg_data;
770fc31d8fSHans de Goede 
780fc31d8fSHans de Goede 	if (rdev->desc->id == TPS68470_CORE)
790fc31d8fSHans de Goede 		clk_disable_unprepare(data->clk);
800fc31d8fSHans de Goede 
810fc31d8fSHans de Goede 	return regulator_disable_regmap(rdev);
820fc31d8fSHans de Goede }
830fc31d8fSHans de Goede 
840fc31d8fSHans de Goede /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
850fc31d8fSHans de Goede static const struct regulator_ops tps68470_regulator_ops = {
860fc31d8fSHans de Goede 	.is_enabled		= regulator_is_enabled_regmap,
870fc31d8fSHans de Goede 	.enable			= tps68470_regulator_enable,
880fc31d8fSHans de Goede 	.disable		= tps68470_regulator_disable,
890fc31d8fSHans de Goede 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
900fc31d8fSHans de Goede 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
910fc31d8fSHans de Goede 	.list_voltage		= regulator_list_voltage_linear_range,
920fc31d8fSHans de Goede 	.map_voltage		= regulator_map_voltage_linear_range,
930fc31d8fSHans de Goede };
940fc31d8fSHans de Goede 
950fc31d8fSHans de Goede static const struct regulator_ops tps68470_always_on_reg_ops = {
960fc31d8fSHans de Goede 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
970fc31d8fSHans de Goede 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
980fc31d8fSHans de Goede 	.list_voltage		= regulator_list_voltage_linear_range,
990fc31d8fSHans de Goede 	.map_voltage		= regulator_map_voltage_linear_range,
1000fc31d8fSHans de Goede };
1010fc31d8fSHans de Goede 
1020fc31d8fSHans de Goede static const struct regulator_desc regulators[] = {
1030fc31d8fSHans de Goede 	TPS68470_REGULATOR(CORE, TPS68470_CORE, tps68470_regulator_ops, 43,
1040fc31d8fSHans de Goede 			   TPS68470_REG_VDVAL, TPS68470_VDVAL_DVOLT_MASK,
1050fc31d8fSHans de Goede 			   TPS68470_REG_VDCTL, TPS68470_VDCTL_EN_MASK,
1060fc31d8fSHans de Goede 			   tps68470_core_ranges, ARRAY_SIZE(tps68470_core_ranges)),
1070fc31d8fSHans de Goede 	TPS68470_REGULATOR(ANA, TPS68470_ANA, tps68470_regulator_ops, 126,
1080fc31d8fSHans de Goede 			   TPS68470_REG_VAVAL, TPS68470_VAVAL_AVOLT_MASK,
1090fc31d8fSHans de Goede 			   TPS68470_REG_VACTL, TPS68470_VACTL_EN_MASK,
1100fc31d8fSHans de Goede 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
1110fc31d8fSHans de Goede 	TPS68470_REGULATOR(VCM, TPS68470_VCM, tps68470_regulator_ops, 126,
1120fc31d8fSHans de Goede 			   TPS68470_REG_VCMVAL, TPS68470_VCMVAL_VCVOLT_MASK,
1130fc31d8fSHans de Goede 			   TPS68470_REG_VCMCTL, TPS68470_VCMCTL_EN_MASK,
1140fc31d8fSHans de Goede 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
1150fc31d8fSHans de Goede 	TPS68470_REGULATOR(VIO, TPS68470_VIO, tps68470_always_on_reg_ops, 126,
1160fc31d8fSHans de Goede 			   TPS68470_REG_VIOVAL, TPS68470_VIOVAL_IOVOLT_MASK,
1170fc31d8fSHans de Goede 			   0, 0,
1180fc31d8fSHans de Goede 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
1190fc31d8fSHans de Goede /*
1200fc31d8fSHans de Goede  * (1) This regulator must have the same voltage as VIO if S_IO LDO is used to
1210fc31d8fSHans de Goede  *     power a sensor/VCM which I2C is daisy chained behind the PMIC.
1220fc31d8fSHans de Goede  * (2) If there is no I2C daisy chain it can be set freely.
1230fc31d8fSHans de Goede  */
1240fc31d8fSHans de Goede 	TPS68470_REGULATOR(VSIO, TPS68470_VSIO, tps68470_regulator_ops, 126,
1250fc31d8fSHans de Goede 			   TPS68470_REG_VSIOVAL, TPS68470_VSIOVAL_IOVOLT_MASK,
1260fc31d8fSHans de Goede 			   TPS68470_REG_S_I2C_CTL, TPS68470_S_I2C_CTL_EN_MASK,
1270fc31d8fSHans de Goede 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
1280fc31d8fSHans de Goede 	TPS68470_REGULATOR(AUX1, TPS68470_AUX1, tps68470_regulator_ops, 126,
1290fc31d8fSHans de Goede 			   TPS68470_REG_VAUX1VAL, TPS68470_VAUX1VAL_AUX1VOLT_MASK,
1300fc31d8fSHans de Goede 			   TPS68470_REG_VAUX1CTL, TPS68470_VAUX1CTL_EN_MASK,
1310fc31d8fSHans de Goede 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
1320fc31d8fSHans de Goede 	TPS68470_REGULATOR(AUX2, TPS68470_AUX2, tps68470_regulator_ops, 126,
1330fc31d8fSHans de Goede 			   TPS68470_REG_VAUX2VAL, TPS68470_VAUX2VAL_AUX2VOLT_MASK,
1340fc31d8fSHans de Goede 			   TPS68470_REG_VAUX2CTL, TPS68470_VAUX2CTL_EN_MASK,
1350fc31d8fSHans de Goede 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
1360fc31d8fSHans de Goede };
1370fc31d8fSHans de Goede 
tps68470_regulator_probe(struct platform_device * pdev)1380fc31d8fSHans de Goede static int tps68470_regulator_probe(struct platform_device *pdev)
1390fc31d8fSHans de Goede {
1400fc31d8fSHans de Goede 	struct device *dev = &pdev->dev;
1410fc31d8fSHans de Goede 	struct tps68470_regulator_platform_data *pdata = dev_get_platdata(dev);
1420fc31d8fSHans de Goede 	struct tps68470_regulator_data *data;
1430fc31d8fSHans de Goede 	struct regulator_config config = { };
1440fc31d8fSHans de Goede 	struct regulator_dev *rdev;
1450fc31d8fSHans de Goede 	int i;
1460fc31d8fSHans de Goede 
1470fc31d8fSHans de Goede 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1480fc31d8fSHans de Goede 	if (!data)
1490fc31d8fSHans de Goede 		return -ENOMEM;
1500fc31d8fSHans de Goede 
1510fc31d8fSHans de Goede 	data->clk = devm_clk_get(dev, "tps68470-clk");
1520fc31d8fSHans de Goede 	if (IS_ERR(data->clk))
1530fc31d8fSHans de Goede 		return dev_err_probe(dev, PTR_ERR(data->clk), "getting tps68470-clk\n");
1540fc31d8fSHans de Goede 
1550fc31d8fSHans de Goede 	config.dev = dev->parent;
1560fc31d8fSHans de Goede 	config.regmap = dev_get_drvdata(dev->parent);
1570fc31d8fSHans de Goede 	config.driver_data = data;
1580fc31d8fSHans de Goede 
1590fc31d8fSHans de Goede 	for (i = 0; i < TPS68470_NUM_REGULATORS; i++) {
1600fc31d8fSHans de Goede 		if (pdata)
1610fc31d8fSHans de Goede 			config.init_data = pdata->reg_init_data[i];
1620fc31d8fSHans de Goede 		else
1630fc31d8fSHans de Goede 			config.init_data = NULL;
1640fc31d8fSHans de Goede 
1650fc31d8fSHans de Goede 		rdev = devm_regulator_register(dev, &regulators[i], &config);
1660fc31d8fSHans de Goede 		if (IS_ERR(rdev))
1670fc31d8fSHans de Goede 			return dev_err_probe(dev, PTR_ERR(rdev),
1680fc31d8fSHans de Goede 					     "registering %s regulator\n",
1690fc31d8fSHans de Goede 					     regulators[i].name);
1700fc31d8fSHans de Goede 	}
1710fc31d8fSHans de Goede 
1720fc31d8fSHans de Goede 	return 0;
1730fc31d8fSHans de Goede }
1740fc31d8fSHans de Goede 
1750fc31d8fSHans de Goede static struct platform_driver tps68470_regulator_driver = {
1760fc31d8fSHans de Goede 	.driver = {
1770fc31d8fSHans de Goede 		.name = "tps68470-regulator",
178*41cff178SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1790fc31d8fSHans de Goede 	},
1800fc31d8fSHans de Goede 	.probe = tps68470_regulator_probe,
1810fc31d8fSHans de Goede };
1820fc31d8fSHans de Goede 
1830fc31d8fSHans de Goede /*
1840fc31d8fSHans de Goede  * The ACPI tps68470 probe-ordering depends on the clk/gpio/regulator drivers
1850fc31d8fSHans de Goede  * registering before the drivers for the camera-sensors which use them bind.
1860fc31d8fSHans de Goede  * subsys_initcall() ensures this when the drivers are builtin.
1870fc31d8fSHans de Goede  */
tps68470_regulator_init(void)1880fc31d8fSHans de Goede static int __init tps68470_regulator_init(void)
1890fc31d8fSHans de Goede {
1900fc31d8fSHans de Goede 	return platform_driver_register(&tps68470_regulator_driver);
1910fc31d8fSHans de Goede }
1920fc31d8fSHans de Goede subsys_initcall(tps68470_regulator_init);
1930fc31d8fSHans de Goede 
tps68470_regulator_exit(void)1940fc31d8fSHans de Goede static void __exit tps68470_regulator_exit(void)
1950fc31d8fSHans de Goede {
1960fc31d8fSHans de Goede 	platform_driver_unregister(&tps68470_regulator_driver);
1970fc31d8fSHans de Goede }
1980fc31d8fSHans de Goede module_exit(tps68470_regulator_exit);
1990fc31d8fSHans de Goede 
2000fc31d8fSHans de Goede MODULE_ALIAS("platform:tps68470-regulator");
2010fc31d8fSHans de Goede MODULE_DESCRIPTION("TPS68470 voltage regulator driver");
2020fc31d8fSHans de Goede MODULE_LICENSE("GPL v2");
203