1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0168a9bSKeerthy /*
3f0168a9bSKeerthy  * Regulator driver for LP87565 PMIC
4f0168a9bSKeerthy  *
5f0168a9bSKeerthy  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
6f0168a9bSKeerthy  */
7f0168a9bSKeerthy 
8f0168a9bSKeerthy #include <linux/module.h>
9f0168a9bSKeerthy #include <linux/platform_device.h>
10f0168a9bSKeerthy #include <linux/regmap.h>
11f0168a9bSKeerthy 
12f0168a9bSKeerthy #include <linux/mfd/lp87565.h>
13f0168a9bSKeerthy 
14f0168a9bSKeerthy #define LP87565_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \
15f0168a9bSKeerthy 			 _delay, _lr, _cr)				\
16f0168a9bSKeerthy 	[_id] = {							\
17f0168a9bSKeerthy 		.desc = {						\
18f0168a9bSKeerthy 			.name			= _name,		\
19f0168a9bSKeerthy 			.supply_name		= _of "-in",		\
20f0168a9bSKeerthy 			.id			= _id,			\
21f0168a9bSKeerthy 			.of_match		= of_match_ptr(_of),	\
22f0168a9bSKeerthy 			.regulators_node	= of_match_ptr("regulators"),\
23f0168a9bSKeerthy 			.ops			= &_ops,		\
24f0168a9bSKeerthy 			.n_voltages		= _n,			\
25f0168a9bSKeerthy 			.type			= REGULATOR_VOLTAGE,	\
26f0168a9bSKeerthy 			.owner			= THIS_MODULE,		\
27f0168a9bSKeerthy 			.vsel_reg		= _vr,			\
28f0168a9bSKeerthy 			.vsel_mask		= _vm,			\
29f0168a9bSKeerthy 			.enable_reg		= _er,			\
30f0168a9bSKeerthy 			.enable_mask		= _em,			\
31f0168a9bSKeerthy 			.ramp_delay		= _delay,		\
32f0168a9bSKeerthy 			.linear_ranges		= _lr,			\
33f0168a9bSKeerthy 			.n_linear_ranges	= ARRAY_SIZE(_lr),	\
34d0ccbe11SAxel Lin 			.curr_table = lp87565_buck_uA,			\
35d0ccbe11SAxel Lin 			.n_current_limits = ARRAY_SIZE(lp87565_buck_uA),\
36d0ccbe11SAxel Lin 			.csel_reg = (_cr),				\
37d0ccbe11SAxel Lin 			.csel_mask = LP87565_BUCK_CTRL_2_ILIM,		\
38f0168a9bSKeerthy 		},							\
39f0168a9bSKeerthy 		.ctrl2_reg = _cr,					\
40f0168a9bSKeerthy 	}
41f0168a9bSKeerthy 
42f0168a9bSKeerthy struct lp87565_regulator {
43f0168a9bSKeerthy 	struct regulator_desc desc;
44f0168a9bSKeerthy 	unsigned int ctrl2_reg;
45f0168a9bSKeerthy };
46f0168a9bSKeerthy 
47f0168a9bSKeerthy static const struct lp87565_regulator regulators[];
48f0168a9bSKeerthy 
4960ab7f41SMatti Vaittinen static const struct linear_range buck0_1_2_3_ranges[] = {
5042f1ea48SKeerthy 	REGULATOR_LINEAR_RANGE(600000, 0xA, 0x17, 10000),
51f0168a9bSKeerthy 	REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
52f0168a9bSKeerthy 	REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
53f0168a9bSKeerthy };
54f0168a9bSKeerthy 
55b7fbc592SAxel Lin static const unsigned int lp87565_buck_ramp_delay[] = {
56f0168a9bSKeerthy 	30000, 15000, 10000, 7500, 3800, 1900, 940, 470
57f0168a9bSKeerthy };
58f0168a9bSKeerthy 
59f0168a9bSKeerthy /* LP87565 BUCK current limit */
60f0168a9bSKeerthy static const unsigned int lp87565_buck_uA[] = {
61f0168a9bSKeerthy 	1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 4500000, 5000000,
62f0168a9bSKeerthy };
63f0168a9bSKeerthy 
64f0168a9bSKeerthy static int lp87565_buck_set_ramp_delay(struct regulator_dev *rdev,
65f0168a9bSKeerthy 				       int ramp_delay)
66f0168a9bSKeerthy {
67f0168a9bSKeerthy 	int id = rdev_get_id(rdev);
68f0168a9bSKeerthy 	unsigned int reg;
69f0168a9bSKeerthy 	int ret;
70f0168a9bSKeerthy 
71f0168a9bSKeerthy 	if (ramp_delay <= 470)
72f0168a9bSKeerthy 		reg = 7;
73f0168a9bSKeerthy 	else if (ramp_delay <= 940)
74f0168a9bSKeerthy 		reg = 6;
75f0168a9bSKeerthy 	else if (ramp_delay <= 1900)
76f0168a9bSKeerthy 		reg = 5;
77f0168a9bSKeerthy 	else if (ramp_delay <= 3800)
78f0168a9bSKeerthy 		reg = 4;
79f0168a9bSKeerthy 	else if (ramp_delay <= 7500)
80f0168a9bSKeerthy 		reg = 3;
81f0168a9bSKeerthy 	else if (ramp_delay <= 10000)
82f0168a9bSKeerthy 		reg = 2;
83f0168a9bSKeerthy 	else if (ramp_delay <= 15000)
84f0168a9bSKeerthy 		reg = 1;
85f0168a9bSKeerthy 	else
86f0168a9bSKeerthy 		reg = 0;
87f0168a9bSKeerthy 
886cadd8aeSAxel Lin 	ret = regmap_update_bits(rdev->regmap, regulators[id].ctrl2_reg,
89f0168a9bSKeerthy 				 LP87565_BUCK_CTRL_2_SLEW_RATE,
90f0168a9bSKeerthy 				 reg << __ffs(LP87565_BUCK_CTRL_2_SLEW_RATE));
91f0168a9bSKeerthy 	if (ret) {
926cadd8aeSAxel Lin 		dev_err(&rdev->dev, "SLEW RATE write failed: %d\n", ret);
93f0168a9bSKeerthy 		return ret;
94f0168a9bSKeerthy 	}
95f0168a9bSKeerthy 
96f0168a9bSKeerthy 	rdev->constraints->ramp_delay = lp87565_buck_ramp_delay[reg];
97f0168a9bSKeerthy 
982d045e94SKeerthy 	/* Conservatively give a 15% margin */
992d045e94SKeerthy 	rdev->constraints->ramp_delay =
1002d045e94SKeerthy 				rdev->constraints->ramp_delay * 85 / 100;
1012d045e94SKeerthy 
102f0168a9bSKeerthy 	return 0;
103f0168a9bSKeerthy }
104f0168a9bSKeerthy 
105d0ccbe11SAxel Lin /* Operations permitted on BUCKs */
106b7fbc592SAxel Lin static const struct regulator_ops lp87565_buck_ops = {
107f0168a9bSKeerthy 	.is_enabled		= regulator_is_enabled_regmap,
108f0168a9bSKeerthy 	.enable			= regulator_enable_regmap,
109f0168a9bSKeerthy 	.disable		= regulator_disable_regmap,
110f0168a9bSKeerthy 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
111f0168a9bSKeerthy 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
112f0168a9bSKeerthy 	.list_voltage		= regulator_list_voltage_linear_range,
113f0168a9bSKeerthy 	.map_voltage		= regulator_map_voltage_linear_range,
114f0168a9bSKeerthy 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
115f0168a9bSKeerthy 	.set_ramp_delay		= lp87565_buck_set_ramp_delay,
116d0ccbe11SAxel Lin 	.set_current_limit	= regulator_set_current_limit_regmap,
117d0ccbe11SAxel Lin 	.get_current_limit	= regulator_get_current_limit_regmap,
118f0168a9bSKeerthy };
119f0168a9bSKeerthy 
120f0168a9bSKeerthy static const struct lp87565_regulator regulators[] = {
121f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK0", LP87565_BUCK_0, "buck0", lp87565_buck_ops,
122f0168a9bSKeerthy 			  256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
123f0168a9bSKeerthy 			  LP87565_REG_BUCK0_CTRL_1,
1242d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
125f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
126f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK1", LP87565_BUCK_1, "buck1", lp87565_buck_ops,
127f0168a9bSKeerthy 			  256, LP87565_REG_BUCK1_VOUT, LP87565_BUCK_VSET,
128f0168a9bSKeerthy 			  LP87565_REG_BUCK1_CTRL_1,
1292d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
130f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK1_CTRL_2),
131f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK2", LP87565_BUCK_2, "buck2", lp87565_buck_ops,
132f0168a9bSKeerthy 			  256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
133f0168a9bSKeerthy 			  LP87565_REG_BUCK2_CTRL_1,
1342d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
135f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
136f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK3", LP87565_BUCK_3, "buck3", lp87565_buck_ops,
137f0168a9bSKeerthy 			  256, LP87565_REG_BUCK3_VOUT, LP87565_BUCK_VSET,
138f0168a9bSKeerthy 			  LP87565_REG_BUCK3_CTRL_1,
1392d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
140f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK3_CTRL_2),
141f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK10", LP87565_BUCK_10, "buck10", lp87565_buck_ops,
142f0168a9bSKeerthy 			  256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
143f0168a9bSKeerthy 			  LP87565_REG_BUCK0_CTRL_1,
1442f51a260SKeerthy 			  LP87565_BUCK_CTRL_1_EN |
1452f51a260SKeerthy 			  LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
146f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
147f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK23", LP87565_BUCK_23, "buck23", lp87565_buck_ops,
148f0168a9bSKeerthy 			  256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
149f0168a9bSKeerthy 			  LP87565_REG_BUCK2_CTRL_1,
1502d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
151f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
1527ee63bd7SKeerthy 	LP87565_REGULATOR("BUCK3210", LP87565_BUCK_3210, "buck3210",
1537ee63bd7SKeerthy 			  lp87565_buck_ops, 256, LP87565_REG_BUCK0_VOUT,
1547ee63bd7SKeerthy 			  LP87565_BUCK_VSET, LP87565_REG_BUCK0_CTRL_1,
1557ee63bd7SKeerthy 			  LP87565_BUCK_CTRL_1_EN |
1567ee63bd7SKeerthy 			  LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
1577ee63bd7SKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
158f0168a9bSKeerthy };
159f0168a9bSKeerthy 
160f0168a9bSKeerthy static int lp87565_regulator_probe(struct platform_device *pdev)
161f0168a9bSKeerthy {
162f0168a9bSKeerthy 	struct lp87565 *lp87565 = dev_get_drvdata(pdev->dev.parent);
163f0168a9bSKeerthy 	struct regulator_config config = { };
164f0168a9bSKeerthy 	struct regulator_dev *rdev;
165a853c0a0SAxel Lin 	int i, min_idx, max_idx;
166f0168a9bSKeerthy 
167f0168a9bSKeerthy 	platform_set_drvdata(pdev, lp87565);
168f0168a9bSKeerthy 
169f0168a9bSKeerthy 	config.dev = &pdev->dev;
170f0168a9bSKeerthy 	config.dev->of_node = lp87565->dev->of_node;
171f0168a9bSKeerthy 	config.driver_data = lp87565;
172f0168a9bSKeerthy 	config.regmap = lp87565->regmap;
173f0168a9bSKeerthy 
1747ee63bd7SKeerthy 	switch (lp87565->dev_type) {
1757ee63bd7SKeerthy 	case LP87565_DEVICE_TYPE_LP87565_Q1:
176f0168a9bSKeerthy 		min_idx = LP87565_BUCK_10;
177f0168a9bSKeerthy 		max_idx = LP87565_BUCK_23;
1787ee63bd7SKeerthy 		break;
1797ee63bd7SKeerthy 	case LP87565_DEVICE_TYPE_LP87561_Q1:
1807ee63bd7SKeerthy 		min_idx = LP87565_BUCK_3210;
1817ee63bd7SKeerthy 		max_idx = LP87565_BUCK_3210;
182f3f4363bSColin Ian King 		break;
1837ee63bd7SKeerthy 	default:
184a853c0a0SAxel Lin 		min_idx = LP87565_BUCK_0;
185a853c0a0SAxel Lin 		max_idx = LP87565_BUCK_3;
186a853c0a0SAxel Lin 		break;
187f0168a9bSKeerthy 	}
188f0168a9bSKeerthy 
189f0168a9bSKeerthy 	for (i = min_idx; i <= max_idx; i++) {
190f0168a9bSKeerthy 		rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
191f0168a9bSKeerthy 					       &config);
192f0168a9bSKeerthy 		if (IS_ERR(rdev)) {
193f0168a9bSKeerthy 			dev_err(lp87565->dev, "failed to register %s regulator\n",
194f0168a9bSKeerthy 				pdev->name);
195f0168a9bSKeerthy 			return PTR_ERR(rdev);
196f0168a9bSKeerthy 		}
197f0168a9bSKeerthy 	}
198f0168a9bSKeerthy 
199f0168a9bSKeerthy 	return 0;
200f0168a9bSKeerthy }
201f0168a9bSKeerthy 
202f0168a9bSKeerthy static const struct platform_device_id lp87565_regulator_id_table[] = {
203f0168a9bSKeerthy 	{ "lp87565-regulator", },
204f0168a9bSKeerthy 	{ "lp87565-q1-regulator", },
205f0168a9bSKeerthy 	{ /* sentinel */ }
206f0168a9bSKeerthy };
207f0168a9bSKeerthy MODULE_DEVICE_TABLE(platform, lp87565_regulator_id_table);
208f0168a9bSKeerthy 
209f0168a9bSKeerthy static struct platform_driver lp87565_regulator_driver = {
210f0168a9bSKeerthy 	.driver = {
211f0168a9bSKeerthy 		.name = "lp87565-pmic",
212f0168a9bSKeerthy 	},
213f0168a9bSKeerthy 	.probe = lp87565_regulator_probe,
214f0168a9bSKeerthy 	.id_table = lp87565_regulator_id_table,
215f0168a9bSKeerthy };
216f0168a9bSKeerthy module_platform_driver(lp87565_regulator_driver);
217f0168a9bSKeerthy 
218f0168a9bSKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
219f0168a9bSKeerthy MODULE_DESCRIPTION("LP87565 voltage regulator driver");
220f0168a9bSKeerthy MODULE_LICENSE("GPL v2");
221