1a493077fSAnilKumar Ch /*
2a493077fSAnilKumar Ch  * tps65217-regulator.c
3a493077fSAnilKumar Ch  *
4a493077fSAnilKumar Ch  * Regulator driver for TPS65217 PMIC
5a493077fSAnilKumar Ch  *
62ca76b3eSAlexander A. Klimov  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
7a493077fSAnilKumar Ch  *
8a493077fSAnilKumar Ch  * This program is free software; you can redistribute it and/or
9a493077fSAnilKumar Ch  * modify it under the terms of the GNU General Public License as
10a493077fSAnilKumar Ch  * published by the Free Software Foundation version 2.
11a493077fSAnilKumar Ch  *
12a493077fSAnilKumar Ch  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13a493077fSAnilKumar Ch  * kind, whether express or implied; without even the implied warranty
14a493077fSAnilKumar Ch  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15a493077fSAnilKumar Ch  * GNU General Public License for more details.
16a493077fSAnilKumar Ch  */
17a493077fSAnilKumar Ch 
18a493077fSAnilKumar Ch #include <linux/kernel.h>
19a493077fSAnilKumar Ch #include <linux/module.h>
20a493077fSAnilKumar Ch #include <linux/device.h>
21a493077fSAnilKumar Ch #include <linux/init.h>
22a493077fSAnilKumar Ch #include <linux/err.h>
23a493077fSAnilKumar Ch #include <linux/platform_device.h>
24a493077fSAnilKumar Ch 
251922b0f2SAnilKumar Ch #include <linux/regulator/of_regulator.h>
26a493077fSAnilKumar Ch #include <linux/regulator/driver.h>
27a493077fSAnilKumar Ch #include <linux/regulator/machine.h>
28a493077fSAnilKumar Ch #include <linux/mfd/tps65217.h>
29a493077fSAnilKumar Ch 
307d42a7f2SMark Brown #define TPS65217_REGULATOR(_name, _id, _of_match, _ops, _n, _vr, _vm, _em, \
313de56099SRuss Dill 			   _t, _lr, _nlr,  _sr, _sm)	\
32a493077fSAnilKumar Ch 	{						\
33a493077fSAnilKumar Ch 		.name		= _name,		\
34a493077fSAnilKumar Ch 		.id		= _id,			\
357d42a7f2SMark Brown 		.of_match       = of_match_ptr(_of_match),    \
367d42a7f2SMark Brown 		.regulators_node= of_match_ptr("regulators"), \
37a493077fSAnilKumar Ch 		.ops		= &_ops,		\
38a493077fSAnilKumar Ch 		.n_voltages	= _n,			\
39a493077fSAnilKumar Ch 		.type		= REGULATOR_VOLTAGE,	\
40a493077fSAnilKumar Ch 		.owner		= THIS_MODULE,		\
41b4bc9ef6SAxel Lin 		.vsel_reg	= _vr,			\
42b4bc9ef6SAxel Lin 		.vsel_mask	= _vm,			\
43b4bc9ef6SAxel Lin 		.enable_reg	= TPS65217_REG_ENABLE,	\
44b4bc9ef6SAxel Lin 		.enable_mask	= _em,			\
45d172f319SAxel Lin 		.volt_table	= _t,			\
466290d606SAxel Lin 		.linear_ranges	= _lr,			\
476290d606SAxel Lin 		.n_linear_ranges = _nlr,		\
483de56099SRuss Dill 		.bypass_reg	= _sr,			\
493de56099SRuss Dill 		.bypass_mask	= _sm,			\
50a493077fSAnilKumar Ch 	}						\
51a493077fSAnilKumar Ch 
52d172f319SAxel Lin static const unsigned int LDO1_VSEL_table[] = {
53a493077fSAnilKumar Ch 	1000000, 1100000, 1200000, 1250000,
54a493077fSAnilKumar Ch 	1300000, 1350000, 1400000, 1500000,
55a493077fSAnilKumar Ch 	1600000, 1800000, 2500000, 2750000,
56a493077fSAnilKumar Ch 	2800000, 3000000, 3100000, 3300000,
57a493077fSAnilKumar Ch };
58a493077fSAnilKumar Ch 
5960ab7f41SMatti Vaittinen static const struct linear_range tps65217_uv1_ranges[] = {
608828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(900000, 0, 24, 25000),
61689b9e02SAxel Lin 	REGULATOR_LINEAR_RANGE(1550000, 25, 52, 50000),
628828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(3000000, 53, 55, 100000),
630b5e200cSAxel Lin 	REGULATOR_LINEAR_RANGE(3300000, 56, 63, 0),
646290d606SAxel Lin };
65a493077fSAnilKumar Ch 
6660ab7f41SMatti Vaittinen static const struct linear_range tps65217_uv2_ranges[] = {
678828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(1500000, 0, 8, 50000),
688828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(2000000, 9, 13, 100000),
698828bae4SAxel Lin 	REGULATOR_LINEAR_RANGE(2450000, 14, 31, 50000),
70a493077fSAnilKumar Ch };
71a493077fSAnilKumar Ch 
72fc56911bSAxel Lin static int tps65217_pmic_enable(struct regulator_dev *dev)
73a493077fSAnilKumar Ch {
74a493077fSAnilKumar Ch 	struct tps65217 *tps = rdev_get_drvdata(dev);
756bdaaf0eSSachin Kamat 	int rid = rdev_get_id(dev);
76a493077fSAnilKumar Ch 
77fc56911bSAxel Lin 	if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
78a493077fSAnilKumar Ch 		return -EINVAL;
79a493077fSAnilKumar Ch 
80a493077fSAnilKumar Ch 	/* Enable the regulator and password protection is level 1 */
81a493077fSAnilKumar Ch 	return tps65217_set_bits(tps, TPS65217_REG_ENABLE,
82b4bc9ef6SAxel Lin 				 dev->desc->enable_mask, dev->desc->enable_mask,
83a493077fSAnilKumar Ch 				 TPS65217_PROTECT_L1);
84a493077fSAnilKumar Ch }
85a493077fSAnilKumar Ch 
86fc56911bSAxel Lin static int tps65217_pmic_disable(struct regulator_dev *dev)
87a493077fSAnilKumar Ch {
88a493077fSAnilKumar Ch 	struct tps65217 *tps = rdev_get_drvdata(dev);
896bdaaf0eSSachin Kamat 	int rid = rdev_get_id(dev);
90a493077fSAnilKumar Ch 
91fc56911bSAxel Lin 	if (rid < TPS65217_DCDC_1 || rid > TPS65217_LDO_4)
92a493077fSAnilKumar Ch 		return -EINVAL;
93a493077fSAnilKumar Ch 
94a493077fSAnilKumar Ch 	/* Disable the regulator and password protection is level 1 */
95a493077fSAnilKumar Ch 	return tps65217_clear_bits(tps, TPS65217_REG_ENABLE,
96b4bc9ef6SAxel Lin 				   dev->desc->enable_mask, TPS65217_PROTECT_L1);
97a493077fSAnilKumar Ch }
98a493077fSAnilKumar Ch 
998b22285fSAxel Lin static int tps65217_pmic_set_voltage_sel(struct regulator_dev *dev,
100a493077fSAnilKumar Ch 					 unsigned selector)
101a493077fSAnilKumar Ch {
102a493077fSAnilKumar Ch 	int ret;
103a493077fSAnilKumar Ch 	struct tps65217 *tps = rdev_get_drvdata(dev);
104fc56911bSAxel Lin 	unsigned int rid = rdev_get_id(dev);
105a493077fSAnilKumar Ch 
106a493077fSAnilKumar Ch 	/* Set the voltage based on vsel value and write protect level is 2 */
107b4bc9ef6SAxel Lin 	ret = tps65217_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
1088b22285fSAxel Lin 				selector, TPS65217_PROTECT_L2);
109fc56911bSAxel Lin 
110fc56911bSAxel Lin 	/* Set GO bit for DCDCx to initiate voltage transistion */
111fc56911bSAxel Lin 	switch (rid) {
112fc56911bSAxel Lin 	case TPS65217_DCDC_1 ... TPS65217_DCDC_3:
113fc56911bSAxel Lin 		ret = tps65217_set_bits(tps, TPS65217_REG_DEFSLEW,
114fc56911bSAxel Lin 				       TPS65217_DEFSLEW_GO, TPS65217_DEFSLEW_GO,
115fc56911bSAxel Lin 				       TPS65217_PROTECT_L2);
116fc56911bSAxel Lin 		break;
117a493077fSAnilKumar Ch 	}
118a493077fSAnilKumar Ch 
119fc56911bSAxel Lin 	return ret;
120fc56911bSAxel Lin }
121fc56911bSAxel Lin 
1223de56099SRuss Dill static int tps65217_pmic_set_suspend_enable(struct regulator_dev *dev)
1233de56099SRuss Dill {
1243de56099SRuss Dill 	struct tps65217 *tps = rdev_get_drvdata(dev);
1253de56099SRuss Dill 	unsigned int rid = rdev_get_id(dev);
1263de56099SRuss Dill 
127f10a5e49SLee Jones 	if (rid > TPS65217_LDO_4)
1283de56099SRuss Dill 		return -EINVAL;
1293de56099SRuss Dill 
1303de56099SRuss Dill 	return tps65217_clear_bits(tps, dev->desc->bypass_reg,
1313de56099SRuss Dill 				   dev->desc->bypass_mask,
1323de56099SRuss Dill 				   TPS65217_PROTECT_L1);
1333de56099SRuss Dill }
1343de56099SRuss Dill 
1353de56099SRuss Dill static int tps65217_pmic_set_suspend_disable(struct regulator_dev *dev)
1363de56099SRuss Dill {
1373de56099SRuss Dill 	struct tps65217 *tps = rdev_get_drvdata(dev);
1383de56099SRuss Dill 	unsigned int rid = rdev_get_id(dev);
1393de56099SRuss Dill 
140f10a5e49SLee Jones 	if (rid > TPS65217_LDO_4)
1413de56099SRuss Dill 		return -EINVAL;
1423de56099SRuss Dill 
1433de56099SRuss Dill 	if (!tps->strobes[rid])
1443de56099SRuss Dill 		return -EINVAL;
1453de56099SRuss Dill 
1463de56099SRuss Dill 	return tps65217_set_bits(tps, dev->desc->bypass_reg,
1473de56099SRuss Dill 				 dev->desc->bypass_mask,
1483de56099SRuss Dill 				 tps->strobes[rid], TPS65217_PROTECT_L1);
1493de56099SRuss Dill }
1503de56099SRuss Dill 
151fc56911bSAxel Lin /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
1522c33b50eSAxel Lin static const struct regulator_ops tps65217_pmic_ops = {
153b4bc9ef6SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
154fc56911bSAxel Lin 	.enable			= tps65217_pmic_enable,
155fc56911bSAxel Lin 	.disable		= tps65217_pmic_disable,
156b4bc9ef6SAxel Lin 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
1578b22285fSAxel Lin 	.set_voltage_sel	= tps65217_pmic_set_voltage_sel,
1586290d606SAxel Lin 	.list_voltage		= regulator_list_voltage_linear_range,
1596290d606SAxel Lin 	.map_voltage		= regulator_map_voltage_linear_range,
1603de56099SRuss Dill 	.set_suspend_enable	= tps65217_pmic_set_suspend_enable,
1613de56099SRuss Dill 	.set_suspend_disable	= tps65217_pmic_set_suspend_disable,
162a493077fSAnilKumar Ch };
163a493077fSAnilKumar Ch 
164a493077fSAnilKumar Ch /* Operations permitted on LDO1 */
1652c33b50eSAxel Lin static const struct regulator_ops tps65217_pmic_ldo1_ops = {
166b4bc9ef6SAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
167fc56911bSAxel Lin 	.enable			= tps65217_pmic_enable,
168fc56911bSAxel Lin 	.disable		= tps65217_pmic_disable,
169b4bc9ef6SAxel Lin 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
1708b22285fSAxel Lin 	.set_voltage_sel	= tps65217_pmic_set_voltage_sel,
171d172f319SAxel Lin 	.list_voltage		= regulator_list_voltage_table,
1725957ae1fSAxel Lin 	.map_voltage		= regulator_map_voltage_ascend,
1733de56099SRuss Dill 	.set_suspend_enable	= tps65217_pmic_set_suspend_enable,
1743de56099SRuss Dill 	.set_suspend_disable	= tps65217_pmic_set_suspend_disable,
175a493077fSAnilKumar Ch };
176a493077fSAnilKumar Ch 
17778637a3dSAxel Lin static const struct regulator_desc regulators[] = {
1787d42a7f2SMark Brown 	TPS65217_REGULATOR("DCDC1", TPS65217_DCDC_1, "dcdc1",
1797d42a7f2SMark Brown 			   tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC1,
1807d42a7f2SMark Brown 			   TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC1_EN,
181b4c2e158SMåns Andersson 			   NULL, tps65217_uv1_ranges,
182b4c2e158SMåns Andersson 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1,
1833de56099SRuss Dill 			   TPS65217_SEQ1_DC1_SEQ_MASK),
1847d42a7f2SMark Brown 	TPS65217_REGULATOR("DCDC2", TPS65217_DCDC_2, "dcdc2",
1857d42a7f2SMark Brown 			   tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC2,
1867d42a7f2SMark Brown 			   TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC2_EN,
1877d42a7f2SMark Brown 			   NULL, tps65217_uv1_ranges,
1883de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ1,
1893de56099SRuss Dill 			   TPS65217_SEQ1_DC2_SEQ_MASK),
1907d42a7f2SMark Brown 	TPS65217_REGULATOR("DCDC3", TPS65217_DCDC_3, "dcdc3",
1917d42a7f2SMark Brown 			   tps65217_pmic_ops, 64, TPS65217_REG_DEFDCDC3,
1927d42a7f2SMark Brown 			   TPS65217_DEFDCDCX_DCDC_MASK, TPS65217_ENABLE_DC3_EN,
193b4c2e158SMåns Andersson 			   NULL, tps65217_uv1_ranges,
194b4c2e158SMåns Andersson 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ2,
1953de56099SRuss Dill 			   TPS65217_SEQ2_DC3_SEQ_MASK),
1967d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO1", TPS65217_LDO_1, "ldo1",
1977d42a7f2SMark Brown 			   tps65217_pmic_ldo1_ops, 16, TPS65217_REG_DEFLDO1,
1987d42a7f2SMark Brown 			   TPS65217_DEFLDO1_LDO1_MASK, TPS65217_ENABLE_LDO1_EN,
1993de56099SRuss Dill 			   LDO1_VSEL_table, NULL, 0, TPS65217_REG_SEQ2,
2003de56099SRuss Dill 			   TPS65217_SEQ2_LDO1_SEQ_MASK),
2017d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO2", TPS65217_LDO_2, "ldo2", tps65217_pmic_ops,
2027d42a7f2SMark Brown 			   64, TPS65217_REG_DEFLDO2,
2037d42a7f2SMark Brown 			   TPS65217_DEFLDO2_LDO2_MASK, TPS65217_ENABLE_LDO2_EN,
2047d42a7f2SMark Brown 			   NULL, tps65217_uv1_ranges,
2053de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv1_ranges), TPS65217_REG_SEQ3,
2063de56099SRuss Dill 			   TPS65217_SEQ3_LDO2_SEQ_MASK),
2077d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO3", TPS65217_LDO_3, "ldo3", tps65217_pmic_ops,
2087d42a7f2SMark Brown 			   32, TPS65217_REG_DEFLS1, TPS65217_DEFLDO3_LDO3_MASK,
209d172f319SAxel Lin 			   TPS65217_ENABLE_LS1_EN | TPS65217_DEFLDO3_LDO3_EN,
2106290d606SAxel Lin 			   NULL, tps65217_uv2_ranges,
2113de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ3,
2123de56099SRuss Dill 			   TPS65217_SEQ3_LDO3_SEQ_MASK),
2137d42a7f2SMark Brown 	TPS65217_REGULATOR("LDO4", TPS65217_LDO_4, "ldo4", tps65217_pmic_ops,
2147d42a7f2SMark Brown 			   32, TPS65217_REG_DEFLS2, TPS65217_DEFLDO4_LDO4_MASK,
215d172f319SAxel Lin 			   TPS65217_ENABLE_LS2_EN | TPS65217_DEFLDO4_LDO4_EN,
2166290d606SAxel Lin 			   NULL, tps65217_uv2_ranges,
2173de56099SRuss Dill 			   ARRAY_SIZE(tps65217_uv2_ranges), TPS65217_REG_SEQ4,
2183de56099SRuss Dill 			   TPS65217_SEQ4_LDO4_SEQ_MASK),
219a493077fSAnilKumar Ch };
220a493077fSAnilKumar Ch 
221a5023574SBill Pemberton static int tps65217_regulator_probe(struct platform_device *pdev)
222a493077fSAnilKumar Ch {
2231922b0f2SAnilKumar Ch 	struct tps65217 *tps = dev_get_drvdata(pdev->dev.parent);
2241922b0f2SAnilKumar Ch 	struct tps65217_board *pdata = dev_get_platdata(tps->dev);
225a493077fSAnilKumar Ch 	struct regulator_dev *rdev;
226c172708dSMark Brown 	struct regulator_config config = { };
2273de56099SRuss Dill 	int i, ret;
2283de56099SRuss Dill 	unsigned int val;
229a493077fSAnilKumar Ch 
2303de56099SRuss Dill 	/* Allocate memory for strobes */
231a86854d0SKees Cook 	tps->strobes = devm_kcalloc(&pdev->dev,
232a86854d0SKees Cook 				    TPS65217_NUM_REGULATOR, sizeof(u8),
233a86854d0SKees Cook 				    GFP_KERNEL);
2344f919ca2SAnton Vasilyev 	if (!tps->strobes)
2354f919ca2SAnton Vasilyev 		return -ENOMEM;
2363de56099SRuss Dill 
2371922b0f2SAnilKumar Ch 	platform_set_drvdata(pdev, tps);
2381922b0f2SAnilKumar Ch 
2391922b0f2SAnilKumar Ch 	for (i = 0; i < TPS65217_NUM_REGULATOR; i++) {
2401922b0f2SAnilKumar Ch 		/* Register the regulators */
2411922b0f2SAnilKumar Ch 		config.dev = tps->dev;
24281baf9feSMark Brown 		if (pdata)
24394ee607cSAxel Lin 			config.init_data = pdata->tps65217_init_data[i];
244c172708dSMark Brown 		config.driver_data = tps;
2451922b0f2SAnilKumar Ch 		config.regmap = tps->regmap;
246c172708dSMark Brown 
2474aac198dSSachin Kamat 		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
2484aac198dSSachin Kamat 					       &config);
2491922b0f2SAnilKumar Ch 		if (IS_ERR(rdev)) {
2501922b0f2SAnilKumar Ch 			dev_err(tps->dev, "failed to register %s regulator\n",
2511922b0f2SAnilKumar Ch 				pdev->name);
2524aac198dSSachin Kamat 			return PTR_ERR(rdev);
2531922b0f2SAnilKumar Ch 		}
2543de56099SRuss Dill 
2553de56099SRuss Dill 		/* Store default strobe info */
2563de56099SRuss Dill 		ret = tps65217_reg_read(tps, regulators[i].bypass_reg, &val);
25744455a6dSLee Jones 		if (ret)
25844455a6dSLee Jones 			return ret;
25944455a6dSLee Jones 
2603de56099SRuss Dill 		tps->strobes[i] = val & regulators[i].bypass_mask;
2611922b0f2SAnilKumar Ch 	}
2627d42a7f2SMark Brown 
263a493077fSAnilKumar Ch 	return 0;
264a493077fSAnilKumar Ch }
265a493077fSAnilKumar Ch 
266a493077fSAnilKumar Ch static struct platform_driver tps65217_regulator_driver = {
267a493077fSAnilKumar Ch 	.driver = {
268a493077fSAnilKumar Ch 		.name = "tps65217-pmic",
269a493077fSAnilKumar Ch 	},
270a493077fSAnilKumar Ch 	.probe = tps65217_regulator_probe,
271a493077fSAnilKumar Ch };
272a493077fSAnilKumar Ch 
273a493077fSAnilKumar Ch static int __init tps65217_regulator_init(void)
274a493077fSAnilKumar Ch {
275a493077fSAnilKumar Ch 	return platform_driver_register(&tps65217_regulator_driver);
276a493077fSAnilKumar Ch }
277a493077fSAnilKumar Ch subsys_initcall(tps65217_regulator_init);
278a493077fSAnilKumar Ch 
279a493077fSAnilKumar Ch static void __exit tps65217_regulator_exit(void)
280a493077fSAnilKumar Ch {
281a493077fSAnilKumar Ch 	platform_driver_unregister(&tps65217_regulator_driver);
282a493077fSAnilKumar Ch }
283a493077fSAnilKumar Ch module_exit(tps65217_regulator_exit);
284a493077fSAnilKumar Ch 
285a493077fSAnilKumar Ch MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
286a493077fSAnilKumar Ch MODULE_DESCRIPTION("TPS65217 voltage regulator driver");
287a493077fSAnilKumar Ch MODULE_ALIAS("platform:tps65217-pmic");
288a493077fSAnilKumar Ch MODULE_LICENSE("GPL v2");
289