1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // mpq7920.c  - regulator driver for mps mpq7920
4 //
5 // Copyright 2019 Monolithic Power Systems, Inc
6 //
7 // Author: Saravanan Sekar <sravanhome@gmail.com>
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/err.h>
13 #include <linux/of.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/i2c.h>
19 #include <linux/regmap.h>
20 #include "mpq7920.h"
21 
22 #define MPQ7920_BUCK_VOLT_RANGE \
23 	((MPQ7920_VOLT_MAX - MPQ7920_BUCK_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
24 #define MPQ7920_LDO_VOLT_RANGE \
25 	((MPQ7920_VOLT_MAX - MPQ7920_LDO_VOLT_MIN)/MPQ7920_VOLT_STEP + 1)
26 
27 #define MPQ7920BUCK(_name, _id, _ilim)					\
28 	[MPQ7920_BUCK ## _id] = {					\
29 		.id = MPQ7920_BUCK ## _id,				\
30 		.name = _name,						\
31 		.of_match = _name,					\
32 		.regulators_node = "regulators",			\
33 		.of_parse_cb = mpq7920_parse_cb,			\
34 		.ops = &mpq7920_buck_ops,				\
35 		.min_uV = MPQ7920_BUCK_VOLT_MIN,			\
36 		.uV_step = MPQ7920_VOLT_STEP,				\
37 		.n_voltages = MPQ7920_BUCK_VOLT_RANGE,			\
38 		.curr_table = _ilim,					\
39 		.n_current_limits = ARRAY_SIZE(_ilim),			\
40 		.csel_reg = MPQ7920_BUCK ##_id## _REG_C,		\
41 		.csel_mask = MPQ7920_MASK_BUCK_ILIM,			\
42 		.enable_reg = MPQ7920_REG_REGULATOR_EN,			\
43 		.enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET -	\
44 					 MPQ7920_BUCK ## _id),		\
45 		.vsel_reg = MPQ7920_BUCK ##_id## _REG_A,		\
46 		.vsel_mask = MPQ7920_MASK_VREF,				\
47 		.active_discharge_on	= MPQ7920_DISCHARGE_ON,		\
48 		.active_discharge_reg	= MPQ7920_BUCK ##_id## _REG_B,	\
49 		.active_discharge_mask	= MPQ7920_MASK_DISCHARGE,	\
50 		.soft_start_reg		= MPQ7920_BUCK ##_id## _REG_C,	\
51 		.soft_start_mask	= MPQ7920_MASK_SOFTSTART,	\
52 		.owner			= THIS_MODULE,			\
53 	}
54 
55 #define MPQ7920LDO(_name, _id, _ops, _ilim, _ilim_sz, _creg, _cmask)	\
56 	[MPQ7920_LDO ## _id] = {					\
57 		.id = MPQ7920_LDO ## _id,				\
58 		.name = _name,						\
59 		.of_match = _name,					\
60 		.regulators_node = "regulators",			\
61 		.ops = _ops,						\
62 		.min_uV = MPQ7920_LDO_VOLT_MIN,				\
63 		.uV_step = MPQ7920_VOLT_STEP,				\
64 		.n_voltages = MPQ7920_LDO_VOLT_RANGE,			\
65 		.vsel_reg = MPQ7920_LDO ##_id## _REG_A,			\
66 		.vsel_mask = MPQ7920_MASK_VREF,				\
67 		.curr_table = _ilim,					\
68 		.n_current_limits = _ilim_sz,				\
69 		.csel_reg = _creg,					\
70 		.csel_mask = _cmask,					\
71 		.enable_reg = (_id == 1) ? 0 : MPQ7920_REG_REGULATOR_EN,\
72 		.enable_mask = BIT(MPQ7920_REGULATOR_EN_OFFSET -	\
73 					MPQ7920_LDO ##_id + 1),		\
74 		.active_discharge_on	= MPQ7920_DISCHARGE_ON,		\
75 		.active_discharge_mask	= MPQ7920_MASK_DISCHARGE,	\
76 		.active_discharge_reg	= MPQ7920_LDO ##_id## _REG_B,	\
77 		.type			= REGULATOR_VOLTAGE,		\
78 		.owner			= THIS_MODULE,			\
79 	}
80 
81 enum mpq7920_regulators {
82 	MPQ7920_BUCK1,
83 	MPQ7920_BUCK2,
84 	MPQ7920_BUCK3,
85 	MPQ7920_BUCK4,
86 	MPQ7920_LDO1, /* LDORTC */
87 	MPQ7920_LDO2,
88 	MPQ7920_LDO3,
89 	MPQ7920_LDO4,
90 	MPQ7920_LDO5,
91 	MPQ7920_MAX_REGULATORS,
92 };
93 
94 struct mpq7920_regulator_info {
95 	struct regmap *regmap;
96 	struct regulator_desc *rdesc;
97 };
98 
99 static const struct regmap_config mpq7920_regmap_config = {
100 	.reg_bits = 8,
101 	.val_bits = 8,
102 	.max_register = 0x25,
103 };
104 
105 /* Current limits array (in uA)
106  * ILIM1 & ILIM3
107  */
108 static const unsigned int mpq7920_I_limits1[] = {
109 	4600000, 6600000, 7600000, 9300000
110 };
111 
112 /* ILIM2 & ILIM4 */
113 static const unsigned int mpq7920_I_limits2[] = {
114 	2700000, 3900000, 5100000, 6100000
115 };
116 
117 /* LDO4 & LDO5 */
118 static const unsigned int mpq7920_I_limits3[] = {
119 	300000, 700000
120 };
121 
122 static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay);
123 static int mpq7920_parse_cb(struct device_node *np,
124 				const struct regulator_desc *rdesc,
125 				struct regulator_config *config);
126 
127 /* RTCLDO not controllable, always ON */
128 static const struct regulator_ops mpq7920_ldortc_ops = {
129 	.list_voltage		= regulator_list_voltage_linear,
130 	.map_voltage		= regulator_map_voltage_linear,
131 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
132 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
133 };
134 
135 static const struct regulator_ops mpq7920_ldo_wo_current_ops = {
136 	.enable			= regulator_enable_regmap,
137 	.disable		= regulator_disable_regmap,
138 	.is_enabled		= regulator_is_enabled_regmap,
139 	.list_voltage		= regulator_list_voltage_linear,
140 	.map_voltage		= regulator_map_voltage_linear,
141 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
142 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
143 	.set_active_discharge	= regulator_set_active_discharge_regmap,
144 };
145 
146 static const struct regulator_ops mpq7920_ldo_ops = {
147 	.enable			= regulator_enable_regmap,
148 	.disable		= regulator_disable_regmap,
149 	.is_enabled		= regulator_is_enabled_regmap,
150 	.list_voltage		= regulator_list_voltage_linear,
151 	.map_voltage		= regulator_map_voltage_linear,
152 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
153 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
154 	.set_active_discharge	= regulator_set_active_discharge_regmap,
155 	.get_current_limit	= regulator_get_current_limit_regmap,
156 	.set_current_limit	= regulator_set_current_limit_regmap,
157 };
158 
159 static const struct regulator_ops mpq7920_buck_ops = {
160 	.enable			= regulator_enable_regmap,
161 	.disable		= regulator_disable_regmap,
162 	.is_enabled		= regulator_is_enabled_regmap,
163 	.list_voltage		= regulator_list_voltage_linear,
164 	.map_voltage		= regulator_map_voltage_linear,
165 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
166 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
167 	.set_active_discharge	= regulator_set_active_discharge_regmap,
168 	.set_soft_start		= regulator_set_soft_start_regmap,
169 	.set_ramp_delay		= mpq7920_set_ramp_delay,
170 };
171 
172 static struct regulator_desc mpq7920_regulators_desc[MPQ7920_MAX_REGULATORS] = {
173 	MPQ7920BUCK("buck1", 1, mpq7920_I_limits1),
174 	MPQ7920BUCK("buck2", 2, mpq7920_I_limits2),
175 	MPQ7920BUCK("buck3", 3, mpq7920_I_limits1),
176 	MPQ7920BUCK("buck4", 4, mpq7920_I_limits2),
177 	MPQ7920LDO("ldortc", 1, &mpq7920_ldortc_ops, NULL, 0, 0, 0),
178 	MPQ7920LDO("ldo2", 2, &mpq7920_ldo_wo_current_ops, NULL, 0, 0, 0),
179 	MPQ7920LDO("ldo3", 3, &mpq7920_ldo_wo_current_ops, NULL, 0, 0, 0),
180 	MPQ7920LDO("ldo4", 4, &mpq7920_ldo_ops, mpq7920_I_limits3,
181 			ARRAY_SIZE(mpq7920_I_limits3), MPQ7920_LDO4_REG_B,
182 			MPQ7920_MASK_LDO_ILIM),
183 	MPQ7920LDO("ldo5", 5, &mpq7920_ldo_ops, mpq7920_I_limits3,
184 			ARRAY_SIZE(mpq7920_I_limits3), MPQ7920_LDO5_REG_B,
185 			MPQ7920_MASK_LDO_ILIM),
186 };
187 
188 /*
189  * DVS ramp rate BUCK1 to BUCK4
190  * 00-01: Reserved
191  * 10: 8mV/us
192  * 11: 4mV/us
193  */
mpq7920_set_ramp_delay(struct regulator_dev * rdev,int ramp_delay)194 static int mpq7920_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
195 {
196 	unsigned int ramp_val;
197 
198 	if (ramp_delay > 8000 || ramp_delay < 0)
199 		return -EINVAL;
200 
201 	if (ramp_delay <= 4000)
202 		ramp_val = 3;
203 	else
204 		ramp_val = 2;
205 
206 	return regmap_update_bits(rdev->regmap, MPQ7920_REG_CTL0,
207 				  MPQ7920_MASK_DVS_SLEWRATE, ramp_val << 6);
208 }
209 
mpq7920_parse_cb(struct device_node * np,const struct regulator_desc * desc,struct regulator_config * config)210 static int mpq7920_parse_cb(struct device_node *np,
211 				const struct regulator_desc *desc,
212 				struct regulator_config *config)
213 {
214 	uint8_t val;
215 	int ret;
216 	struct mpq7920_regulator_info *info = config->driver_data;
217 	struct regulator_desc *rdesc = &info->rdesc[desc->id];
218 
219 	if (of_property_read_bool(np, "mps,buck-ovp-disable")) {
220 		regmap_update_bits(config->regmap,
221 				MPQ7920_BUCK1_REG_B + (rdesc->id * 4),
222 				MPQ7920_MASK_OVP, MPQ7920_OVP_DISABLE);
223 	}
224 
225 	ret = of_property_read_u8(np, "mps,buck-phase-delay", &val);
226 	if (!ret) {
227 		regmap_update_bits(config->regmap,
228 				MPQ7920_BUCK1_REG_C + (rdesc->id * 4),
229 				MPQ7920_MASK_BUCK_PHASE_DEALY,
230 				(val & 3) << 4);
231 	}
232 
233 	ret = of_property_read_u8(np, "mps,buck-softstart", &val);
234 	if (!ret)
235 		rdesc->soft_start_val_on = (val & 3) << 2;
236 
237 	return 0;
238 }
239 
mpq7920_parse_dt(struct device * dev,struct mpq7920_regulator_info * info)240 static void mpq7920_parse_dt(struct device *dev,
241 		 struct mpq7920_regulator_info *info)
242 {
243 	int ret;
244 	struct device_node *np = dev->of_node;
245 	uint8_t freq;
246 
247 	np = of_get_child_by_name(np, "regulators");
248 	if (!np) {
249 		dev_err(dev, "missing 'regulators' subnode in DT\n");
250 		return;
251 	}
252 
253 	ret = of_property_read_u8(np, "mps,switch-freq", &freq);
254 	if (!ret) {
255 		regmap_update_bits(info->regmap, MPQ7920_REG_CTL0,
256 					MPQ7920_MASK_SWITCH_FREQ,
257 					(freq & 3) << 4);
258 	}
259 
260 	of_node_put(np);
261 }
262 
mpq7920_i2c_probe(struct i2c_client * client)263 static int mpq7920_i2c_probe(struct i2c_client *client)
264 {
265 	struct device *dev = &client->dev;
266 	struct mpq7920_regulator_info *info;
267 	struct regulator_config config = { NULL, };
268 	struct regulator_dev *rdev;
269 	struct regmap *regmap;
270 	int i;
271 
272 	info = devm_kzalloc(dev, sizeof(struct mpq7920_regulator_info),
273 				GFP_KERNEL);
274 	if (!info)
275 		return -ENOMEM;
276 
277 	info->rdesc = mpq7920_regulators_desc;
278 	regmap = devm_regmap_init_i2c(client, &mpq7920_regmap_config);
279 	if (IS_ERR(regmap)) {
280 		dev_err(dev, "Failed to allocate regmap!\n");
281 		return PTR_ERR(regmap);
282 	}
283 
284 	i2c_set_clientdata(client, info);
285 	info->regmap = regmap;
286 	if (client->dev.of_node)
287 		mpq7920_parse_dt(&client->dev, info);
288 
289 	config.dev = dev;
290 	config.regmap = regmap;
291 	config.driver_data = info;
292 
293 	for (i = 0; i < MPQ7920_MAX_REGULATORS; i++) {
294 		rdev = devm_regulator_register(dev,
295 					       &mpq7920_regulators_desc[i],
296 					       &config);
297 		if (IS_ERR(rdev)) {
298 			dev_err(dev, "Failed to register regulator!\n");
299 			return PTR_ERR(rdev);
300 		}
301 	}
302 
303 	return 0;
304 }
305 
306 static const struct of_device_id mpq7920_of_match[] = {
307 	{ .compatible = "mps,mpq7920"},
308 	{},
309 };
310 MODULE_DEVICE_TABLE(of, mpq7920_of_match);
311 
312 static const struct i2c_device_id mpq7920_id[] = {
313 	{ "mpq7920", },
314 	{ },
315 };
316 MODULE_DEVICE_TABLE(i2c, mpq7920_id);
317 
318 static struct i2c_driver mpq7920_regulator_driver = {
319 	.driver = {
320 		.name = "mpq7920",
321 		.of_match_table = of_match_ptr(mpq7920_of_match),
322 	},
323 	.probe_new = mpq7920_i2c_probe,
324 	.id_table = mpq7920_id,
325 };
326 module_i2c_driver(mpq7920_regulator_driver);
327 
328 MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
329 MODULE_DESCRIPTION("MPQ7920 PMIC regulator driver");
330 MODULE_LICENSE("GPL");
331