12aec85b2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a493077fSAnilKumar Ch /*
3a493077fSAnilKumar Ch  * tps65217-regulator.c
4a493077fSAnilKumar Ch  *
5a493077fSAnilKumar Ch  * Regulator driver for TPS65217 PMIC
6a493077fSAnilKumar Ch  *
72ca76b3eSAlexander A. Klimov  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
8a493077fSAnilKumar Ch  */
9a493077fSAnilKumar Ch 
10a493077fSAnilKumar Ch #include <linux/kernel.h>
11a493077fSAnilKumar Ch #include <linux/module.h>
12a493077fSAnilKumar Ch #include <linux/device.h>
13a493077fSAnilKumar Ch #include <linux/init.h>
14a493077fSAnilKumar Ch #include <linux/err.h>
15a493077fSAnilKumar Ch #include <linux/platform_device.h>
16a493077fSAnilKumar Ch 
171922b0f2SAnilKumar Ch #include <linux/regulator/of_regulator.h>
18a493077fSAnilKumar Ch #include <linux/regulator/driver.h>
19a493077fSAnilKumar Ch #include <linux/regulator/machine.h>
20a493077fSAnilKumar Ch #include <linux/mfd/tps65217.h>
21a493077fSAnilKumar Ch 
227d42a7f2SMark Brown #define TPS65217_REGULATOR(_name, _id, _of_match, _ops, _n, _vr, _vm, _em, \
233de56099SRuss Dill 			   _t, _lr, _nlr,  _sr, _sm)	\
24a493077fSAnilKumar Ch 	{						\
25a493077fSAnilKumar Ch 		.name		= _name,		\
26a493077fSAnilKumar Ch 		.id		= _id,			\
277d42a7f2SMark Brown 		.of_match       = of_match_ptr(_of_match),    \
287d42a7f2SMark Brown 		.regulators_node= of_match_ptr("regulators"), \
29a493077fSAnilKumar Ch 		.ops		= &_ops,		\
30a493077fSAnilKumar Ch 		.n_voltages	= _n,			\
31a493077fSAnilKumar Ch 		.type		= REGULATOR_VOLTAGE,	\
32a493077fSAnilKumar Ch 		.owner		= THIS_MODULE,		\
33b4bc9ef6SAxel Lin 		.vsel_reg	= _vr,			\
34b4bc9ef6SAxel Lin 		.vsel_mask	= _vm,			\
35b4bc9ef6SAxel Lin 		.enable_reg	= TPS65217_REG_ENABLE,	\
36b4bc9ef6SAxel Lin 		.enable_mask	= _em,			\
37d172f319SAxel Lin 		.volt_table	= _t,			\
386290d606SAxel Lin 		.linear_ranges	= _lr,			\
396290d606SAxel Lin 		.n_linear_ranges = _nlr,		\
403de56099SRuss Dill 		.bypass_reg	= _sr,			\
413de56099SRuss Dill 		.bypass_mask	= _sm,			\
42a493077fSAnilKumar Ch 	}						\
43a493077fSAnilKumar Ch 
44d172f319SAxel Lin static const unsigned int LDO1_VSEL_table[] = {
45a493077fSAnilKumar Ch 	1000000, 1100000, 1200000, 1250000,
46a493077fSAnilKumar Ch 	1300000, 1350000, 1400000, 1500000,
47a493077fSAnilKumar Ch 	1600000, 1800000, 2500000, 2750000,
48a493077fSAnilKumar Ch 	2800000, 3000000, 3100000, 3300000,
49a493077fSAnilKumar Ch };
50a493077fSAnilKumar Ch 
5160ab7f41SMatti Vaittinen static const struct linear_range tps65217_uv1_ranges[] = {
528828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(900000, 0, 24, 25000),
53689b9e02SAxel Lin 	REGULATOR_LINEAR_RANGE(1550000, 25, 52, 50000),
548828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(3000000, 53, 55, 100000),
550b5e200cSAxel Lin 	REGULATOR_LINEAR_RANGE(3300000, 56, 63, 0),
566290d606SAxel Lin };
57a493077fSAnilKumar Ch 
5860ab7f41SMatti Vaittinen static const struct linear_range tps65217_uv2_ranges[] = {
598828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(1500000, 0, 8, 50000),
608828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(2000000, 9, 13, 100000),
618828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(2450000, 14, 31, 50000),
62a493077fSAnilKumar Ch };
63a493077fSAnilKumar Ch 
tps65217_pmic_enable(struct regulator_dev * dev)64fc56911bSAxel Lin static int tps65217_pmic_enable(struct regulator_dev *dev)
65a493077fSAnilKumar Ch {
66a493077fSAnilKumar Ch 	struct tps65217 *tps = rdev_get_drvdata(dev);
676bdaaf0eSSachin Kamat 	int rid = rdev_get_id(dev);
68a493077fSAnilKumar Ch 
69fc56911bSAxel Lin 	if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
70a493077fSAnilKumar Ch 		return -EINVAL;
71a493077fSAnilKumar Ch 
72a493077fSAnilKumar Ch 	/* Enable the regulator and password protection is level 1 */
73a493077fSAnilKumar Ch 	return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
74b4bc9ef6SAxel Lin 				 dev->desc->enable_mask, dev->desc->enable_mask,
75a493077fSAnilKumar Ch 				 TPS65217_PROTECT_L1);
76a493077fSAnilKumar Ch }
77a493077fSAnilKumar Ch 
tps65217_pmic_disable(struct regulator_dev * dev)78fc56911bSAxel Lin static int tps65217_pmic_disable(struct regulator_dev *dev)
79a493077fSAnilKumar Ch {
80a493077fSAnilKumar Ch 	struct tps65217 *tps = rdev_get_drvdata(dev);
816bdaaf0eSSachin Kamat 	int rid = rdev_get_id(dev);
82a493077fSAnilKumar Ch 
83fc56911bSAxel Lin 	if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
84a493077fSAnilKumar Ch 		return -EINVAL;
85a493077fSAnilKumar Ch 
86a493077fSAnilKumar Ch 	/* Disable the regulator and password protection is level 1 */
87a493077fSAnilKumar Ch 	return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
88b4bc9ef6SAxel Lin 				   dev->desc->enable_mask, TPS65217_PROTECT_L1);
89a493077fSAnilKumar Ch }
90a493077fSAnilKumar Ch 
tps65217_pmic_set_voltage_sel(struct regulator_dev * dev,unsigned selector)918b22285fSAxel Lin static int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev,
92a493077fSAnilKumar Ch 					 unsigned selector)
93a493077fSAnilKumar Ch {
94a493077fSAnilKumar Ch 	int ret;
95a493077fSAnilKumar Ch 	struct tps65217 *tps = rdev_get_drvdata(dev);
96fc56911bSAxel Lin 	unsigned int rid = rdev_get_id(dev);
97a493077fSAnilKumar Ch 
98a493077fSAnilKumar Ch 	/* Set the voltage based on vsel value and write protect level is 2 */
99b4bc9ef6SAxel Lin 	ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
1008b22285fSAxel Lin 				selector, TPS65217_PROTECT_L2);
101fc56911bSAxel Lin 
102fc56911bSAxel Lin 	/* Set GO bit for DCDCx to initiate voltage transistion */
103fc56911bSAxel Lin 	switch (rid) {
104fc56911bSAxel Lin 	case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
105fc56911bSAxel Lin 		ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
106fc56911bSAxel Lin 				       TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
107fc56911bSAxel Lin 				       TPS65217_PROTECT_L2);
108fc56911bSAxel Lin 		break;
109a493077fSAnilKumar Ch 	}
110a493077fSAnilKumar Ch 
111fc56911bSAxel Lin 	return ret;
112fc56911bSAxel Lin }
113fc56911bSAxel Lin 
tps65217_pmic_set_suspend_enable(struct regulator_dev * dev)1143de56099SRuss Dill static int tps65217_pmic_set_suspend_enable(struct regulator_dev *dev)
1153de56099SRuss Dill {
1163de56099SRuss Dill 	struct tps65217 *tps = rdev_get_drvdata(dev);
1173de56099SRuss Dill 	unsigned int rid = rdev_get_id(dev);
1183de56099SRuss Dill 
119f10a5e49SLee Jones 	if (rid > TPS65217_LDO_4)
1203de56099SRuss Dill 		return -EINVAL;
1213de56099SRuss Dill 
1223de56099SRuss Dill 	return tps65217_clear_bits(tps, dev->desc->bypass_reg,
1233de56099SRuss Dill 				   dev->desc->bypass_mask,
1243de56099SRuss Dill 				   TPS65217_PROTECT_L1);
1253de56099SRuss Dill }
1263de56099SRuss Dill 
tps65217_pmic_set_suspend_disable(struct regulator_dev * dev)1273de56099SRuss Dill static int tps65217_pmic_set_suspend_disable(struct regulator_dev *dev)
1283de56099SRuss Dill {
1293de56099SRuss Dill 	struct tps65217 *tps = rdev_get_drvdata(dev);
1303de56099SRuss Dill 	unsigned int rid = rdev_get_id(dev);
1313de56099SRuss Dill 
132f10a5e49SLee Jones 	if (rid > TPS65217_LDO_4)
1333de56099SRuss Dill 		return -EINVAL;
1343de56099SRuss Dill 
1353de56099SRuss Dill 	if (!tps->strobes[rid])
1363de56099SRuss Dill 		return -EINVAL;
1373de56099SRuss Dill 
1383de56099SRuss Dill 	return tps65217_set_bits(tps, dev->desc->bypass_reg,
1393de56099SRuss Dill 				 dev->desc->bypass_mask,
1403de56099SRuss Dill 				 tps->strobes[rid], TPS65217_PROTECT_L1);
1413de56099SRuss Dill }
1423de56099SRuss Dill 
143fc56911bSAxel Lin /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
1442c33b50eSAxel Lin static const struct regulator_ops tps65217_pmic_ops = {
145b4bc9ef6SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
146fc56911bSAxel Lin 	.enable			= tps65217_pmic_enable,
147fc56911bSAxel Lin 	.disable		= tps65217_pmic_disable,
148b4bc9ef6SAxel Lin 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
1498b22285fSAxel Lin 	.set_voltage_sel	= tps65217_pmic_set_voltage_sel,
1506290d606SAxel Lin 	.list_voltage		= regulator_list_voltage_linear_range,
1516290d606SAxel Lin 	.map_voltage		= regulator_map_voltage_linear_range,
1523de56099SRuss Dill 	.set_suspend_enable	= tps65217_pmic_set_suspend_enable,
1533de56099SRuss Dill 	.set_suspend_disable	= tps65217_pmic_set_suspend_disable,
154a493077fSAnilKumar Ch };
155a493077fSAnilKumar Ch 
156a493077fSAnilKumar Ch /* Operations permitted on LDO1 */
1572c33b50eSAxel Lin static const struct regulator_ops tps65217_pmic_ldo1_ops = {
158b4bc9ef6SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
159fc56911bSAxel Lin 	.enable			= tps65217_pmic_enable,
160fc56911bSAxel Lin 	.disable		= tps65217_pmic_disable,
161b4bc9ef6SAxel Lin 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
1628b22285fSAxel Lin 	.set_voltage_sel	= tps65217_pmic_set_voltage_sel,
163d172f319SAxel Lin 	.list_voltage		= regulator_list_voltage_table,
1645957ae1fSAxel Lin 	.map_voltage		= regulator_map_voltage_ascend,
1653de56099SRuss Dill 	.set_suspend_enable	= tps65217_pmic_set_suspend_enable,
1663de56099SRuss Dill 	.set_suspend_disable	= tps65217_pmic_set_suspend_disable,
167a493077fSAnilKumar Ch };
168a493077fSAnilKumar Ch 
16978637a3dSAxel Lin static const struct regulator_desc regulators[] = {
1707d42a7f2SMark Brown 	TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, "dcdc1",
1717d42a7f2SMark Brown 			   tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC1,
1727d42a7f2SMark Brown 			   TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC1_EN,
173b4c2e158SMåns Andersson 			   NULL, tps65217_uv1_ranges,
174b4c2e158SMåns Andersson 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1,
1753de56099SRuss Dill 			   TPS65217_SEQ1_DC1_SEQ_MASK),
1767d42a7f2SMark Brown 	TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, "dcdc2",
1777d42a7f2SMark Brown 			   tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC2,
1787d42a7f2SMark Brown 			   TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC2_EN,
1797d42a7f2SMark Brown 			   NULL, tps65217_uv1_ranges,
1803de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1,
1813de56099SRuss Dill 			   TPS65217_SEQ1_DC2_SEQ_MASK),
1827d42a7f2SMark Brown 	TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, "dcdc3",
1837d42a7f2SMark Brown 			   tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC3,
1847d42a7f2SMark Brown 			   TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC3_EN,
185b4c2e158SMåns Andersson 			   NULL, tps65217_uv1_ranges,
186b4c2e158SMåns Andersson 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ2,
1873de56099SRuss Dill 			   TPS65217_SEQ2_DC3_SEQ_MASK),
1887d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, "ldo1",
1897d42a7f2SMark Brown 			   tps65217_pmic_ldo1_ops, 16, TPS65217_REG_DEFLDO1,
1907d42a7f2SMark Brown 			   TPS65217_DEFLDO1_LDO1_MASK, TPS65217_ENABLE_LDO1_EN,
1913de56099SRuss Dill 			   LDO1_VSEL_table, NULL, 0, TPS65217_REG_SEQ2,
1923de56099SRuss Dill 			   TPS65217_SEQ2_LDO1_SEQ_MASK),
1937d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, "ldo2", tps65217_pmic_ops,
1947d42a7f2SMark Brown 			   64, TPS65217_REG_DEFLDO2,
1957d42a7f2SMark Brown 			   TPS65217_DEFLDO2_LDO2_MASK, TPS65217_ENABLE_LDO2_EN,
1967d42a7f2SMark Brown 			   NULL, tps65217_uv1_ranges,
1973de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ3,
1983de56099SRuss Dill 			   TPS65217_SEQ3_LDO2_SEQ_MASK),
1997d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, "ldo3", tps65217_pmic_ops,
2007d42a7f2SMark Brown 			   32, TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK,
201d172f319SAxel Lin 			   TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN,
2026290d606SAxel Lin 			   NULL, tps65217_uv2_ranges,
2033de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ3,
2043de56099SRuss Dill 			   TPS65217_SEQ3_LDO3_SEQ_MASK),
2057d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, "ldo4", tps65217_pmic_ops,
2067d42a7f2SMark Brown 			   32, TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK,
207d172f319SAxel Lin 			   TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN,
2086290d606SAxel Lin 			   NULL, tps65217_uv2_ranges,
2093de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ4,
2103de56099SRuss Dill 			   TPS65217_SEQ4_LDO4_SEQ_MASK),
211a493077fSAnilKumar Ch };
212a493077fSAnilKumar Ch 
tps65217_regulator_probe(struct platform_device * pdev)213a5023574SBill Pemberton static int tps65217_regulator_probe(struct platform_device *pdev)
214a493077fSAnilKumar Ch {
2151922b0f2SAnilKumar Ch 	struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent);
2161922b0f2SAnilKumar Ch 	struct tps65217_board *pdata = dev_get_platdata(tps->dev);
217a493077fSAnilKumar Ch 	struct regulator_dev *rdev;
218c172708dSMark Brown 	struct regulator_config config = { };
2193de56099SRuss Dill 	int i, ret;
2203de56099SRuss Dill 	unsigned int val;
221a493077fSAnilKumar Ch 
2223de56099SRuss Dill 	/* Allocate memory for strobes */
223a86854d0SKees Cook 	tps->strobes = devm_kcalloc(&pdev->dev,
224a86854d0SKees Cook 				    TPS65217_NUM_REGULATOR, sizeof(u8),
225a86854d0SKees Cook 				    GFP_KERNEL);
2264f919ca2SAnton Vasilyev 	if (!tps->strobes)
2274f919ca2SAnton Vasilyev 		return -ENOMEM;
2283de56099SRuss Dill 
2291922b0f2SAnilKumar Ch 	platform_set_drvdata(pdev, tps);
2301922b0f2SAnilKumar Ch 
2311922b0f2SAnilKumar Ch 	for (i = 0; i < TPS65217_NUM_REGULATOR; i++) {
2321922b0f2SAnilKumar Ch 		/* Register the regulators */
2331922b0f2SAnilKumar Ch 		config.dev = tps->dev;
23481baf9feSMark Brown 		if (pdata)
23594ee607cSAxel Lin 			config.init_data = pdata->tps65217_init_data[i];
236c172708dSMark Brown 		config.driver_data = tps;
2371922b0f2SAnilKumar Ch 		config.regmap = tps->regmap;
238c172708dSMark Brown 
2394aac198dSSachin Kamat 		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
2404aac198dSSachin Kamat 					       &config);
2411922b0f2SAnilKumar Ch 		if (IS_ERR(rdev)) {
2421922b0f2SAnilKumar Ch 			dev_err(tps->dev, "failed to register %s regulator\n",
2431922b0f2SAnilKumar Ch 				pdev->name);
2444aac198dSSachin Kamat 			return PTR_ERR(rdev);
2451922b0f2SAnilKumar Ch 		}
2463de56099SRuss Dill 
2473de56099SRuss Dill 		/* Store default strobe info */
2483de56099SRuss Dill 		ret = tps65217_reg_read(tps, regulators[i].bypass_reg, &val);
24944455a6dSLee Jones 		if (ret)
25044455a6dSLee Jones 			return ret;
25144455a6dSLee Jones 
2523de56099SRuss Dill 		tps->strobes[i] = val & regulators[i].bypass_mask;
2531922b0f2SAnilKumar Ch 	}
2547d42a7f2SMark Brown 
255a493077fSAnilKumar Ch 	return 0;
256a493077fSAnilKumar Ch }
257a493077fSAnilKumar Ch 
258a493077fSAnilKumar Ch static struct platform_driver tps65217_regulator_driver = {
259a493077fSAnilKumar Ch 	.driver = {
260a493077fSAnilKumar Ch 		.name = "tps65217-pmic",
261*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
262a493077fSAnilKumar Ch 	},
263a493077fSAnilKumar Ch 	.probe = tps65217_regulator_probe,
264a493077fSAnilKumar Ch };
265a493077fSAnilKumar Ch 
tps65217_regulator_init(void)266a493077fSAnilKumar Ch static int __init tps65217_regulator_init(void)
267a493077fSAnilKumar Ch {
268a493077fSAnilKumar Ch 	return platform_driver_register(&tps65217_regulator_driver);
269a493077fSAnilKumar Ch }
270a493077fSAnilKumar Ch subsys_initcall(tps65217_regulator_init);
271a493077fSAnilKumar Ch 
tps65217_regulator_exit(void)272a493077fSAnilKumar Ch static void __exit tps65217_regulator_exit(void)
273a493077fSAnilKumar Ch {
274a493077fSAnilKumar Ch 	platform_driver_unregister(&tps65217_regulator_driver);
275a493077fSAnilKumar Ch }
276a493077fSAnilKumar Ch module_exit(tps65217_regulator_exit);
277a493077fSAnilKumar Ch 
278a493077fSAnilKumar Ch MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
279a493077fSAnilKumar Ch MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
280a493077fSAnilKumar Ch MODULE_ALIAS("platform:tps65217-pmic");
281a493077fSAnilKumar Ch MODULE_LICENSE("GPL v2");
282