1f0168a9bSKeerthy /*
2f0168a9bSKeerthy  * Regulator driver for LP87565 PMIC
3f0168a9bSKeerthy  *
4f0168a9bSKeerthy  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
5f0168a9bSKeerthy  *
6f0168a9bSKeerthy  *  This program is free software; you can redistribute  it and/or modify it
7f0168a9bSKeerthy  *  under  the terms of  the GNU General  Public License as published by the
8580331d0SKeerthy  *  Free Software Foundation version 2.
9f0168a9bSKeerthy  */
10f0168a9bSKeerthy 
11f0168a9bSKeerthy #include <linux/module.h>
12f0168a9bSKeerthy #include <linux/platform_device.h>
13f0168a9bSKeerthy #include <linux/regmap.h>
14f0168a9bSKeerthy 
15f0168a9bSKeerthy #include <linux/mfd/lp87565.h>
16f0168a9bSKeerthy 
17f0168a9bSKeerthy #define LP87565_REGULATOR(_name, _id, _of, _ops, _n, _vr, _vm, _er, _em, \
18f0168a9bSKeerthy 			 _delay, _lr, _cr)				\
19f0168a9bSKeerthy 	[_id] = {							\
20f0168a9bSKeerthy 		.desc = {						\
21f0168a9bSKeerthy 			.name			= _name,		\
22f0168a9bSKeerthy 			.supply_name		= _of "-in",		\
23f0168a9bSKeerthy 			.id			= _id,			\
24f0168a9bSKeerthy 			.of_match		= of_match_ptr(_of),	\
25f0168a9bSKeerthy 			.regulators_node	= of_match_ptr("regulators"),\
26f0168a9bSKeerthy 			.ops			= &_ops,		\
27f0168a9bSKeerthy 			.n_voltages		= _n,			\
28f0168a9bSKeerthy 			.type			= REGULATOR_VOLTAGE,	\
29f0168a9bSKeerthy 			.owner			= THIS_MODULE,		\
30f0168a9bSKeerthy 			.vsel_reg		= _vr,			\
31f0168a9bSKeerthy 			.vsel_mask		= _vm,			\
32f0168a9bSKeerthy 			.enable_reg		= _er,			\
33f0168a9bSKeerthy 			.enable_mask		= _em,			\
34f0168a9bSKeerthy 			.ramp_delay		= _delay,		\
35f0168a9bSKeerthy 			.linear_ranges		= _lr,			\
36f0168a9bSKeerthy 			.n_linear_ranges	= ARRAY_SIZE(_lr),	\
37d0ccbe11SAxel Lin 			.curr_table = lp87565_buck_uA,			\
38d0ccbe11SAxel Lin 			.n_current_limits = ARRAY_SIZE(lp87565_buck_uA),\
39d0ccbe11SAxel Lin 			.csel_reg = (_cr),				\
40d0ccbe11SAxel Lin 			.csel_mask = LP87565_BUCK_CTRL_2_ILIM,		\
41f0168a9bSKeerthy 		},							\
42f0168a9bSKeerthy 		.ctrl2_reg = _cr,					\
43f0168a9bSKeerthy 	}
44f0168a9bSKeerthy 
45f0168a9bSKeerthy struct lp87565_regulator {
46f0168a9bSKeerthy 	struct regulator_desc desc;
47f0168a9bSKeerthy 	unsigned int ctrl2_reg;
48f0168a9bSKeerthy };
49f0168a9bSKeerthy 
50f0168a9bSKeerthy static const struct lp87565_regulator regulators[];
51f0168a9bSKeerthy 
52f0168a9bSKeerthy static const struct regulator_linear_range buck0_1_2_3_ranges[] = {
5342f1ea48SKeerthy 	REGULATOR_LINEAR_RANGE(600000, 0xA, 0x17, 10000),
54f0168a9bSKeerthy 	REGULATOR_LINEAR_RANGE(735000, 0x18, 0x9d, 5000),
55f0168a9bSKeerthy 	REGULATOR_LINEAR_RANGE(1420000, 0x9e, 0xff, 20000),
56f0168a9bSKeerthy };
57f0168a9bSKeerthy 
58b7fbc592SAxel Lin static const unsigned int lp87565_buck_ramp_delay[] = {
59f0168a9bSKeerthy 	30000, 15000, 10000, 7500, 3800, 1900, 940, 470
60f0168a9bSKeerthy };
61f0168a9bSKeerthy 
62f0168a9bSKeerthy /* LP87565 BUCK current limit */
63f0168a9bSKeerthy static const unsigned int lp87565_buck_uA[] = {
64f0168a9bSKeerthy 	1500000, 2000000, 2500000, 3000000, 3500000, 4000000, 4500000, 5000000,
65f0168a9bSKeerthy };
66f0168a9bSKeerthy 
67f0168a9bSKeerthy static int lp87565_buck_set_ramp_delay(struct regulator_dev *rdev,
68f0168a9bSKeerthy 				       int ramp_delay)
69f0168a9bSKeerthy {
70f0168a9bSKeerthy 	int id = rdev_get_id(rdev);
71f0168a9bSKeerthy 	struct lp87565 *lp87565 = rdev_get_drvdata(rdev);
72f0168a9bSKeerthy 	unsigned int reg;
73f0168a9bSKeerthy 	int ret;
74f0168a9bSKeerthy 
75f0168a9bSKeerthy 	if (ramp_delay <= 470)
76f0168a9bSKeerthy 		reg = 7;
77f0168a9bSKeerthy 	else if (ramp_delay <= 940)
78f0168a9bSKeerthy 		reg = 6;
79f0168a9bSKeerthy 	else if (ramp_delay <= 1900)
80f0168a9bSKeerthy 		reg = 5;
81f0168a9bSKeerthy 	else if (ramp_delay <= 3800)
82f0168a9bSKeerthy 		reg = 4;
83f0168a9bSKeerthy 	else if (ramp_delay <= 7500)
84f0168a9bSKeerthy 		reg = 3;
85f0168a9bSKeerthy 	else if (ramp_delay <= 10000)
86f0168a9bSKeerthy 		reg = 2;
87f0168a9bSKeerthy 	else if (ramp_delay <= 15000)
88f0168a9bSKeerthy 		reg = 1;
89f0168a9bSKeerthy 	else
90f0168a9bSKeerthy 		reg = 0;
91f0168a9bSKeerthy 
92f0168a9bSKeerthy 	ret = regmap_update_bits(lp87565->regmap, regulators[id].ctrl2_reg,
93f0168a9bSKeerthy 				 LP87565_BUCK_CTRL_2_SLEW_RATE,
94f0168a9bSKeerthy 				 reg << __ffs(LP87565_BUCK_CTRL_2_SLEW_RATE));
95f0168a9bSKeerthy 	if (ret) {
96f0168a9bSKeerthy 		dev_err(lp87565->dev, "SLEW RATE write failed: %d\n", ret);
97f0168a9bSKeerthy 		return ret;
98f0168a9bSKeerthy 	}
99f0168a9bSKeerthy 
100f0168a9bSKeerthy 	rdev->constraints->ramp_delay = lp87565_buck_ramp_delay[reg];
101f0168a9bSKeerthy 
1022d045e94SKeerthy 	/* Conservatively give a 15% margin */
1032d045e94SKeerthy 	rdev->constraints->ramp_delay =
1042d045e94SKeerthy 				rdev->constraints->ramp_delay * 85 / 100;
1052d045e94SKeerthy 
106f0168a9bSKeerthy 	return 0;
107f0168a9bSKeerthy }
108f0168a9bSKeerthy 
109d0ccbe11SAxel Lin /* Operations permitted on BUCKs */
110b7fbc592SAxel Lin static const struct regulator_ops lp87565_buck_ops = {
111f0168a9bSKeerthy 	.is_enabled		= regulator_is_enabled_regmap,
112f0168a9bSKeerthy 	.enable			= regulator_enable_regmap,
113f0168a9bSKeerthy 	.disable		= regulator_disable_regmap,
114f0168a9bSKeerthy 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
115f0168a9bSKeerthy 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
116f0168a9bSKeerthy 	.list_voltage		= regulator_list_voltage_linear_range,
117f0168a9bSKeerthy 	.map_voltage		= regulator_map_voltage_linear_range,
118f0168a9bSKeerthy 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
119f0168a9bSKeerthy 	.set_ramp_delay		= lp87565_buck_set_ramp_delay,
120d0ccbe11SAxel Lin 	.set_current_limit	= regulator_set_current_limit_regmap,
121d0ccbe11SAxel Lin 	.get_current_limit	= regulator_get_current_limit_regmap,
122f0168a9bSKeerthy };
123f0168a9bSKeerthy 
124f0168a9bSKeerthy static const struct lp87565_regulator regulators[] = {
125f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK0", LP87565_BUCK_0, "buck0", lp87565_buck_ops,
126f0168a9bSKeerthy 			  256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
127f0168a9bSKeerthy 			  LP87565_REG_BUCK0_CTRL_1,
1282d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
129f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
130f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK1", LP87565_BUCK_1, "buck1", lp87565_buck_ops,
131f0168a9bSKeerthy 			  256, LP87565_REG_BUCK1_VOUT, LP87565_BUCK_VSET,
132f0168a9bSKeerthy 			  LP87565_REG_BUCK1_CTRL_1,
1332d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
134f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK1_CTRL_2),
135f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK2", LP87565_BUCK_2, "buck2", lp87565_buck_ops,
136f0168a9bSKeerthy 			  256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
137f0168a9bSKeerthy 			  LP87565_REG_BUCK2_CTRL_1,
1382d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
139f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
140f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK3", LP87565_BUCK_3, "buck3", lp87565_buck_ops,
141f0168a9bSKeerthy 			  256, LP87565_REG_BUCK3_VOUT, LP87565_BUCK_VSET,
142f0168a9bSKeerthy 			  LP87565_REG_BUCK3_CTRL_1,
1432d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
144f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK3_CTRL_2),
145f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK10", LP87565_BUCK_10, "buck10", lp87565_buck_ops,
146f0168a9bSKeerthy 			  256, LP87565_REG_BUCK0_VOUT, LP87565_BUCK_VSET,
147f0168a9bSKeerthy 			  LP87565_REG_BUCK0_CTRL_1,
1482f51a260SKeerthy 			  LP87565_BUCK_CTRL_1_EN |
1492f51a260SKeerthy 			  LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
150f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
151f0168a9bSKeerthy 	LP87565_REGULATOR("BUCK23", LP87565_BUCK_23, "buck23", lp87565_buck_ops,
152f0168a9bSKeerthy 			  256, LP87565_REG_BUCK2_VOUT, LP87565_BUCK_VSET,
153f0168a9bSKeerthy 			  LP87565_REG_BUCK2_CTRL_1,
1542d045e94SKeerthy 			  LP87565_BUCK_CTRL_1_EN, 3230,
155f0168a9bSKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK2_CTRL_2),
1567ee63bd7SKeerthy 	LP87565_REGULATOR("BUCK3210", LP87565_BUCK_3210, "buck3210",
1577ee63bd7SKeerthy 			  lp87565_buck_ops, 256, LP87565_REG_BUCK0_VOUT,
1587ee63bd7SKeerthy 			  LP87565_BUCK_VSET, LP87565_REG_BUCK0_CTRL_1,
1597ee63bd7SKeerthy 			  LP87565_BUCK_CTRL_1_EN |
1607ee63bd7SKeerthy 			  LP87565_BUCK_CTRL_1_FPWM_MP_0_2, 3230,
1617ee63bd7SKeerthy 			  buck0_1_2_3_ranges, LP87565_REG_BUCK0_CTRL_2),
162f0168a9bSKeerthy };
163f0168a9bSKeerthy 
164f0168a9bSKeerthy static int lp87565_regulator_probe(struct platform_device *pdev)
165f0168a9bSKeerthy {
166f0168a9bSKeerthy 	struct lp87565 *lp87565 = dev_get_drvdata(pdev->dev.parent);
167f0168a9bSKeerthy 	struct regulator_config config = { };
168f0168a9bSKeerthy 	struct regulator_dev *rdev;
169d1a6cbdfSAxel Lin 	int i, min_idx = LP87565_BUCK_0, max_idx = LP87565_BUCK_3;
170f0168a9bSKeerthy 
171f0168a9bSKeerthy 	platform_set_drvdata(pdev, lp87565);
172f0168a9bSKeerthy 
173f0168a9bSKeerthy 	config.dev = &pdev->dev;
174f0168a9bSKeerthy 	config.dev->of_node = lp87565->dev->of_node;
175f0168a9bSKeerthy 	config.driver_data = lp87565;
176f0168a9bSKeerthy 	config.regmap = lp87565->regmap;
177f0168a9bSKeerthy 
1787ee63bd7SKeerthy 	switch (lp87565->dev_type) {
1797ee63bd7SKeerthy 	case LP87565_DEVICE_TYPE_LP87565_Q1:
180f0168a9bSKeerthy 		min_idx = LP87565_BUCK_10;
181f0168a9bSKeerthy 		max_idx = LP87565_BUCK_23;
1827ee63bd7SKeerthy 		break;
1837ee63bd7SKeerthy 	case LP87565_DEVICE_TYPE_LP87561_Q1:
1847ee63bd7SKeerthy 		min_idx = LP87565_BUCK_3210;
1857ee63bd7SKeerthy 		max_idx = LP87565_BUCK_3210;
186f3f4363bSColin Ian King 		break;
1877ee63bd7SKeerthy 	default:
1887ee63bd7SKeerthy 		dev_err(lp87565->dev, "Invalid lp config %d\n",
1897ee63bd7SKeerthy 			lp87565->dev_type);
1907ee63bd7SKeerthy 		return -EINVAL;
191f0168a9bSKeerthy 	}
192f0168a9bSKeerthy 
193f0168a9bSKeerthy 	for (i = min_idx; i <= max_idx; i++) {
194f0168a9bSKeerthy 		rdev = devm_regulator_register(&pdev->dev, &regulators[i].desc,
195f0168a9bSKeerthy 					       &config);
196f0168a9bSKeerthy 		if (IS_ERR(rdev)) {
197f0168a9bSKeerthy 			dev_err(lp87565->dev, "failed to register %s regulator\n",
198f0168a9bSKeerthy 				pdev->name);
199f0168a9bSKeerthy 			return PTR_ERR(rdev);
200f0168a9bSKeerthy 		}
201f0168a9bSKeerthy 	}
202f0168a9bSKeerthy 
203f0168a9bSKeerthy 	return 0;
204f0168a9bSKeerthy }
205f0168a9bSKeerthy 
206f0168a9bSKeerthy static const struct platform_device_id lp87565_regulator_id_table[] = {
207f0168a9bSKeerthy 	{ "lp87565-regulator", },
208f0168a9bSKeerthy 	{ "lp87565-q1-regulator", },
209f0168a9bSKeerthy 	{ /* sentinel */ }
210f0168a9bSKeerthy };
211f0168a9bSKeerthy MODULE_DEVICE_TABLE(platform, lp87565_regulator_id_table);
212f0168a9bSKeerthy 
213f0168a9bSKeerthy static struct platform_driver lp87565_regulator_driver = {
214f0168a9bSKeerthy 	.driver = {
215f0168a9bSKeerthy 		.name = "lp87565-pmic",
216f0168a9bSKeerthy 	},
217f0168a9bSKeerthy 	.probe = lp87565_regulator_probe,
218f0168a9bSKeerthy 	.id_table = lp87565_regulator_id_table,
219f0168a9bSKeerthy };
220f0168a9bSKeerthy module_platform_driver(lp87565_regulator_driver);
221f0168a9bSKeerthy 
222f0168a9bSKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
223f0168a9bSKeerthy MODULE_DESCRIPTION("LP87565 voltage regulator driver");
224f0168a9bSKeerthy MODULE_LICENSE("GPL v2");
225