1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018, The Linux Foundation. All rights reserved.
3 
4 #define pr_fmt(fmt) "%s: " fmt, __func__
5 
6 #include <linux/err.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/regulator/driver.h>
15 #include <linux/regulator/machine.h>
16 #include <linux/regulator/of_regulator.h>
17 
18 #include <soc/qcom/cmd-db.h>
19 #include <soc/qcom/rpmh.h>
20 
21 #include <dt-bindings/regulator/qcom,rpmh-regulator.h>
22 
23 /**
24  * enum rpmh_regulator_type - supported RPMh accelerator types
25  * %VRM:	RPMh VRM accelerator which supports voting on enable, voltage,
26  *		and mode of LDO, SMPS, and BOB type PMIC regulators.
27  * %XOB:	RPMh XOB accelerator which supports voting on the enable state
28  *		of PMIC regulators.
29  */
30 enum rpmh_regulator_type {
31 	VRM,
32 	XOB,
33 };
34 
35 #define RPMH_REGULATOR_REG_VRM_VOLTAGE		0x0
36 #define RPMH_REGULATOR_REG_ENABLE		0x4
37 #define RPMH_REGULATOR_REG_VRM_MODE		0x8
38 
39 #define PMIC4_LDO_MODE_RETENTION		4
40 #define PMIC4_LDO_MODE_LPM			5
41 #define PMIC4_LDO_MODE_HPM			7
42 
43 #define PMIC4_SMPS_MODE_RETENTION		4
44 #define PMIC4_SMPS_MODE_PFM			5
45 #define PMIC4_SMPS_MODE_AUTO			6
46 #define PMIC4_SMPS_MODE_PWM			7
47 
48 #define PMIC4_BOB_MODE_PASS			0
49 #define PMIC4_BOB_MODE_PFM			1
50 #define PMIC4_BOB_MODE_AUTO			2
51 #define PMIC4_BOB_MODE_PWM			3
52 
53 /**
54  * struct rpmh_vreg_hw_data - RPMh regulator hardware configurations
55  * @regulator_type:		RPMh accelerator type used to manage this
56  *				regulator
57  * @ops:			Pointer to regulator ops callback structure
58  * @voltage_range:		The single range of voltages supported by this
59  *				PMIC regulator type
60  * @n_voltages:			The number of unique voltage set points defined
61  *				by voltage_range
62  * @hpm_min_load_uA:		Minimum load current in microamps that requires
63  *				high power mode (HPM) operation.  This is used
64  *				for LDO hardware type regulators only.
65  * @pmic_mode_map:		Array indexed by regulator framework mode
66  *				containing PMIC hardware modes.  Must be large
67  *				enough to index all framework modes supported
68  *				by this regulator hardware type.
69  * @of_map_mode:		Maps an RPMH_REGULATOR_MODE_* mode value defined
70  *				in device tree to a regulator framework mode
71  */
72 struct rpmh_vreg_hw_data {
73 	enum rpmh_regulator_type		regulator_type;
74 	const struct regulator_ops		*ops;
75 	const struct regulator_linear_range	voltage_range;
76 	int					n_voltages;
77 	int					hpm_min_load_uA;
78 	const int				*pmic_mode_map;
79 	unsigned int			      (*of_map_mode)(unsigned int mode);
80 };
81 
82 /**
83  * struct rpmh_vreg - individual RPMh regulator data structure encapsulating a
84  *		single regulator device
85  * @dev:			Device pointer for the top-level PMIC RPMh
86  *				regulator parent device.  This is used as a
87  *				handle in RPMh write requests.
88  * @addr:			Base address of the regulator resource within
89  *				an RPMh accelerator
90  * @rdesc:			Regulator descriptor
91  * @hw_data:			PMIC regulator configuration data for this RPMh
92  *				regulator
93  * @always_wait_for_ack:	Boolean flag indicating if a request must always
94  *				wait for an ACK from RPMh before continuing even
95  *				if it corresponds to a strictly lower power
96  *				state (e.g. enabled --> disabled).
97  * @enabled:			Flag indicating if the regulator is enabled or
98  *				not
99  * @bypassed:			Boolean indicating if the regulator is in
100  *				bypass (pass-through) mode or not.  This is
101  *				only used by BOB rpmh-regulator resources.
102  * @voltage_selector:		Selector used for get_voltage_sel() and
103  *				set_voltage_sel() callbacks
104  * @mode:			RPMh VRM regulator current framework mode
105  */
106 struct rpmh_vreg {
107 	struct device			*dev;
108 	u32				addr;
109 	struct regulator_desc		rdesc;
110 	const struct rpmh_vreg_hw_data	*hw_data;
111 	bool				always_wait_for_ack;
112 
113 	int				enabled;
114 	bool				bypassed;
115 	int				voltage_selector;
116 	unsigned int			mode;
117 };
118 
119 /**
120  * struct rpmh_vreg_init_data - initialization data for an RPMh regulator
121  * @name:			Name for the regulator which also corresponds
122  *				to the device tree subnode name of the regulator
123  * @resource_name:		RPMh regulator resource name format string.
124  *				This must include exactly one field: '%s' which
125  *				is filled at run-time with the PMIC ID provided
126  *				by device tree property qcom,pmic-id.  Example:
127  *				"ldo%s1" for RPMh resource "ldoa1".
128  * @supply_name:		Parent supply regulator name
129  * @hw_data:			Configuration data for this PMIC regulator type
130  */
131 struct rpmh_vreg_init_data {
132 	const char			*name;
133 	const char			*resource_name;
134 	const char			*supply_name;
135 	const struct rpmh_vreg_hw_data	*hw_data;
136 };
137 
138 /**
139  * rpmh_regulator_send_request() - send the request to RPMh
140  * @vreg:		Pointer to the RPMh regulator
141  * @cmd:		Pointer to the RPMh command to send
142  * @wait_for_ack:	Boolean indicating if execution must wait until the
143  *			request has been acknowledged as complete
144  *
145  * Return: 0 on success, errno on failure
146  */
147 static int rpmh_regulator_send_request(struct rpmh_vreg *vreg,
148 			struct tcs_cmd *cmd, bool wait_for_ack)
149 {
150 	int ret;
151 
152 	if (wait_for_ack || vreg->always_wait_for_ack)
153 		ret = rpmh_write(vreg->dev, RPMH_ACTIVE_ONLY_STATE, cmd, 1);
154 	else
155 		ret = rpmh_write_async(vreg->dev, RPMH_ACTIVE_ONLY_STATE, cmd,
156 					1);
157 
158 	return ret;
159 }
160 
161 static int _rpmh_regulator_vrm_set_voltage_sel(struct regulator_dev *rdev,
162 				unsigned int selector, bool wait_for_ack)
163 {
164 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
165 	struct tcs_cmd cmd = {
166 		.addr = vreg->addr + RPMH_REGULATOR_REG_VRM_VOLTAGE,
167 	};
168 	int ret;
169 
170 	/* VRM voltage control register is set with voltage in millivolts. */
171 	cmd.data = DIV_ROUND_UP(regulator_list_voltage_linear_range(rdev,
172 							selector), 1000);
173 
174 	ret = rpmh_regulator_send_request(vreg, &cmd, wait_for_ack);
175 	if (!ret)
176 		vreg->voltage_selector = selector;
177 
178 	return ret;
179 }
180 
181 static int rpmh_regulator_vrm_set_voltage_sel(struct regulator_dev *rdev,
182 					unsigned int selector)
183 {
184 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
185 
186 	if (vreg->enabled == -EINVAL) {
187 		/*
188 		 * Cache the voltage and send it later when the regulator is
189 		 * enabled or disabled.
190 		 */
191 		vreg->voltage_selector = selector;
192 		return 0;
193 	}
194 
195 	return _rpmh_regulator_vrm_set_voltage_sel(rdev, selector,
196 					selector > vreg->voltage_selector);
197 }
198 
199 static int rpmh_regulator_vrm_get_voltage_sel(struct regulator_dev *rdev)
200 {
201 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
202 
203 	return vreg->voltage_selector;
204 }
205 
206 static int rpmh_regulator_is_enabled(struct regulator_dev *rdev)
207 {
208 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
209 
210 	return vreg->enabled;
211 }
212 
213 static int rpmh_regulator_set_enable_state(struct regulator_dev *rdev,
214 					bool enable)
215 {
216 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
217 	struct tcs_cmd cmd = {
218 		.addr = vreg->addr + RPMH_REGULATOR_REG_ENABLE,
219 		.data = enable,
220 	};
221 	int ret;
222 
223 	if (vreg->enabled == -EINVAL &&
224 	    vreg->voltage_selector != -ENOTRECOVERABLE) {
225 		ret = _rpmh_regulator_vrm_set_voltage_sel(rdev,
226 						vreg->voltage_selector, true);
227 		if (ret < 0)
228 			return ret;
229 	}
230 
231 	ret = rpmh_regulator_send_request(vreg, &cmd, enable);
232 	if (!ret)
233 		vreg->enabled = enable;
234 
235 	return ret;
236 }
237 
238 static int rpmh_regulator_enable(struct regulator_dev *rdev)
239 {
240 	return rpmh_regulator_set_enable_state(rdev, true);
241 }
242 
243 static int rpmh_regulator_disable(struct regulator_dev *rdev)
244 {
245 	return rpmh_regulator_set_enable_state(rdev, false);
246 }
247 
248 static int rpmh_regulator_vrm_set_mode_bypass(struct rpmh_vreg *vreg,
249 					unsigned int mode, bool bypassed)
250 {
251 	struct tcs_cmd cmd = {
252 		.addr = vreg->addr + RPMH_REGULATOR_REG_VRM_MODE,
253 	};
254 	int pmic_mode;
255 
256 	if (mode > REGULATOR_MODE_STANDBY)
257 		return -EINVAL;
258 
259 	pmic_mode = vreg->hw_data->pmic_mode_map[mode];
260 	if (pmic_mode < 0)
261 		return pmic_mode;
262 
263 	if (bypassed)
264 		cmd.data = PMIC4_BOB_MODE_PASS;
265 	else
266 		cmd.data = pmic_mode;
267 
268 	return rpmh_regulator_send_request(vreg, &cmd, true);
269 }
270 
271 static int rpmh_regulator_vrm_set_mode(struct regulator_dev *rdev,
272 					unsigned int mode)
273 {
274 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
275 	int ret;
276 
277 	if (mode == vreg->mode)
278 		return 0;
279 
280 	ret = rpmh_regulator_vrm_set_mode_bypass(vreg, mode, vreg->bypassed);
281 	if (!ret)
282 		vreg->mode = mode;
283 
284 	return ret;
285 }
286 
287 static unsigned int rpmh_regulator_vrm_get_mode(struct regulator_dev *rdev)
288 {
289 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
290 
291 	return vreg->mode;
292 }
293 
294 /**
295  * rpmh_regulator_vrm_set_load() - set the regulator mode based upon the load
296  *		current requested
297  * @rdev:		Regulator device pointer for the rpmh-regulator
298  * @load_uA:		Aggregated load current in microamps
299  *
300  * This function is used in the regulator_ops for VRM type RPMh regulator
301  * devices.
302  *
303  * Return: 0 on success, errno on failure
304  */
305 static int rpmh_regulator_vrm_set_load(struct regulator_dev *rdev, int load_uA)
306 {
307 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
308 	unsigned int mode;
309 
310 	if (load_uA >= vreg->hw_data->hpm_min_load_uA)
311 		mode = REGULATOR_MODE_NORMAL;
312 	else
313 		mode = REGULATOR_MODE_IDLE;
314 
315 	return rpmh_regulator_vrm_set_mode(rdev, mode);
316 }
317 
318 static int rpmh_regulator_vrm_set_bypass(struct regulator_dev *rdev,
319 				bool enable)
320 {
321 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
322 	int ret;
323 
324 	if (vreg->bypassed == enable)
325 		return 0;
326 
327 	ret = rpmh_regulator_vrm_set_mode_bypass(vreg, vreg->mode, enable);
328 	if (!ret)
329 		vreg->bypassed = enable;
330 
331 	return ret;
332 }
333 
334 static int rpmh_regulator_vrm_get_bypass(struct regulator_dev *rdev,
335 				bool *enable)
336 {
337 	struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
338 
339 	*enable = vreg->bypassed;
340 
341 	return 0;
342 }
343 
344 static const struct regulator_ops rpmh_regulator_vrm_ops = {
345 	.enable			= rpmh_regulator_enable,
346 	.disable		= rpmh_regulator_disable,
347 	.is_enabled		= rpmh_regulator_is_enabled,
348 	.set_voltage_sel	= rpmh_regulator_vrm_set_voltage_sel,
349 	.get_voltage_sel	= rpmh_regulator_vrm_get_voltage_sel,
350 	.list_voltage		= regulator_list_voltage_linear_range,
351 	.set_mode		= rpmh_regulator_vrm_set_mode,
352 	.get_mode		= rpmh_regulator_vrm_get_mode,
353 };
354 
355 static const struct regulator_ops rpmh_regulator_vrm_drms_ops = {
356 	.enable			= rpmh_regulator_enable,
357 	.disable		= rpmh_regulator_disable,
358 	.is_enabled		= rpmh_regulator_is_enabled,
359 	.set_voltage_sel	= rpmh_regulator_vrm_set_voltage_sel,
360 	.get_voltage_sel	= rpmh_regulator_vrm_get_voltage_sel,
361 	.list_voltage		= regulator_list_voltage_linear_range,
362 	.set_mode		= rpmh_regulator_vrm_set_mode,
363 	.get_mode		= rpmh_regulator_vrm_get_mode,
364 	.set_load		= rpmh_regulator_vrm_set_load,
365 };
366 
367 static const struct regulator_ops rpmh_regulator_vrm_bypass_ops = {
368 	.enable			= rpmh_regulator_enable,
369 	.disable		= rpmh_regulator_disable,
370 	.is_enabled		= rpmh_regulator_is_enabled,
371 	.set_voltage_sel	= rpmh_regulator_vrm_set_voltage_sel,
372 	.get_voltage_sel	= rpmh_regulator_vrm_get_voltage_sel,
373 	.list_voltage		= regulator_list_voltage_linear_range,
374 	.set_mode		= rpmh_regulator_vrm_set_mode,
375 	.get_mode		= rpmh_regulator_vrm_get_mode,
376 	.set_bypass		= rpmh_regulator_vrm_set_bypass,
377 	.get_bypass		= rpmh_regulator_vrm_get_bypass,
378 };
379 
380 static const struct regulator_ops rpmh_regulator_xob_ops = {
381 	.enable			= rpmh_regulator_enable,
382 	.disable		= rpmh_regulator_disable,
383 	.is_enabled		= rpmh_regulator_is_enabled,
384 };
385 
386 /**
387  * rpmh_regulator_init_vreg() - initialize all attributes of an rpmh-regulator
388  * vreg:		Pointer to the individual rpmh-regulator resource
389  * dev:			Pointer to the top level rpmh-regulator PMIC device
390  * node:		Pointer to the individual rpmh-regulator resource
391  *			device node
392  * pmic_id:		String used to identify the top level rpmh-regulator
393  *			PMIC device on the board
394  * pmic_rpmh_data:	Pointer to a null-terminated array of rpmh-regulator
395  *			resources defined for the top level PMIC device
396  *
397  * Return: 0 on success, errno on failure
398  */
399 static int rpmh_regulator_init_vreg(struct rpmh_vreg *vreg, struct device *dev,
400 			struct device_node *node, const char *pmic_id,
401 			const struct rpmh_vreg_init_data *pmic_rpmh_data)
402 {
403 	struct regulator_config reg_config = {};
404 	char rpmh_resource_name[20] = "";
405 	const struct rpmh_vreg_init_data *rpmh_data;
406 	struct regulator_init_data *init_data;
407 	struct regulator_dev *rdev;
408 	int ret;
409 
410 	vreg->dev = dev;
411 
412 	for (rpmh_data = pmic_rpmh_data; rpmh_data->name; rpmh_data++)
413 		if (of_node_name_eq(node, rpmh_data->name))
414 			break;
415 
416 	if (!rpmh_data->name) {
417 		dev_err(dev, "Unknown regulator %pOFn\n", node);
418 		return -EINVAL;
419 	}
420 
421 	scnprintf(rpmh_resource_name, sizeof(rpmh_resource_name),
422 		rpmh_data->resource_name, pmic_id);
423 
424 	vreg->addr = cmd_db_read_addr(rpmh_resource_name);
425 	if (!vreg->addr) {
426 		dev_err(dev, "%pOFn: could not find RPMh address for resource %s\n",
427 			node, rpmh_resource_name);
428 		return -ENODEV;
429 	}
430 
431 	vreg->rdesc.name = rpmh_data->name;
432 	vreg->rdesc.supply_name = rpmh_data->supply_name;
433 	vreg->hw_data = rpmh_data->hw_data;
434 
435 	vreg->enabled = -EINVAL;
436 	vreg->voltage_selector = -ENOTRECOVERABLE;
437 	vreg->mode = REGULATOR_MODE_INVALID;
438 
439 	if (rpmh_data->hw_data->n_voltages) {
440 		vreg->rdesc.linear_ranges = &rpmh_data->hw_data->voltage_range;
441 		vreg->rdesc.n_linear_ranges = 1;
442 		vreg->rdesc.n_voltages = rpmh_data->hw_data->n_voltages;
443 	}
444 
445 	vreg->always_wait_for_ack = of_property_read_bool(node,
446 						"qcom,always-wait-for-ack");
447 
448 	vreg->rdesc.owner	= THIS_MODULE;
449 	vreg->rdesc.type	= REGULATOR_VOLTAGE;
450 	vreg->rdesc.ops		= vreg->hw_data->ops;
451 	vreg->rdesc.of_map_mode	= vreg->hw_data->of_map_mode;
452 
453 	init_data = of_get_regulator_init_data(dev, node, &vreg->rdesc);
454 	if (!init_data)
455 		return -ENOMEM;
456 
457 	if (rpmh_data->hw_data->regulator_type == XOB &&
458 	    init_data->constraints.min_uV &&
459 	    init_data->constraints.min_uV == init_data->constraints.max_uV) {
460 		vreg->rdesc.fixed_uV = init_data->constraints.min_uV;
461 		vreg->rdesc.n_voltages = 1;
462 	}
463 
464 	reg_config.dev		= dev;
465 	reg_config.init_data	= init_data;
466 	reg_config.of_node	= node;
467 	reg_config.driver_data	= vreg;
468 
469 	rdev = devm_regulator_register(dev, &vreg->rdesc, &reg_config);
470 	if (IS_ERR(rdev)) {
471 		ret = PTR_ERR(rdev);
472 		dev_err(dev, "%pOFn: devm_regulator_register() failed, ret=%d\n",
473 			node, ret);
474 		return ret;
475 	}
476 
477 	dev_dbg(dev, "%pOFn regulator registered for RPMh resource %s @ 0x%05X\n",
478 		node, rpmh_resource_name, vreg->addr);
479 
480 	return 0;
481 }
482 
483 static const int pmic_mode_map_pmic4_ldo[REGULATOR_MODE_STANDBY + 1] = {
484 	[REGULATOR_MODE_INVALID] = -EINVAL,
485 	[REGULATOR_MODE_STANDBY] = PMIC4_LDO_MODE_RETENTION,
486 	[REGULATOR_MODE_IDLE]    = PMIC4_LDO_MODE_LPM,
487 	[REGULATOR_MODE_NORMAL]  = PMIC4_LDO_MODE_HPM,
488 	[REGULATOR_MODE_FAST]    = -EINVAL,
489 };
490 
491 static unsigned int rpmh_regulator_pmic4_ldo_of_map_mode(unsigned int rpmh_mode)
492 {
493 	unsigned int mode;
494 
495 	switch (rpmh_mode) {
496 	case RPMH_REGULATOR_MODE_HPM:
497 		mode = REGULATOR_MODE_NORMAL;
498 		break;
499 	case RPMH_REGULATOR_MODE_LPM:
500 		mode = REGULATOR_MODE_IDLE;
501 		break;
502 	case RPMH_REGULATOR_MODE_RET:
503 		mode = REGULATOR_MODE_STANDBY;
504 		break;
505 	default:
506 		mode = REGULATOR_MODE_INVALID;
507 		break;
508 	}
509 
510 	return mode;
511 }
512 
513 static const int pmic_mode_map_pmic4_smps[REGULATOR_MODE_STANDBY + 1] = {
514 	[REGULATOR_MODE_INVALID] = -EINVAL,
515 	[REGULATOR_MODE_STANDBY] = PMIC4_SMPS_MODE_RETENTION,
516 	[REGULATOR_MODE_IDLE]    = PMIC4_SMPS_MODE_PFM,
517 	[REGULATOR_MODE_NORMAL]  = PMIC4_SMPS_MODE_AUTO,
518 	[REGULATOR_MODE_FAST]    = PMIC4_SMPS_MODE_PWM,
519 };
520 
521 static unsigned int
522 rpmh_regulator_pmic4_smps_of_map_mode(unsigned int rpmh_mode)
523 {
524 	unsigned int mode;
525 
526 	switch (rpmh_mode) {
527 	case RPMH_REGULATOR_MODE_HPM:
528 		mode = REGULATOR_MODE_FAST;
529 		break;
530 	case RPMH_REGULATOR_MODE_AUTO:
531 		mode = REGULATOR_MODE_NORMAL;
532 		break;
533 	case RPMH_REGULATOR_MODE_LPM:
534 		mode = REGULATOR_MODE_IDLE;
535 		break;
536 	case RPMH_REGULATOR_MODE_RET:
537 		mode = REGULATOR_MODE_STANDBY;
538 		break;
539 	default:
540 		mode = REGULATOR_MODE_INVALID;
541 		break;
542 	}
543 
544 	return mode;
545 }
546 
547 static const int pmic_mode_map_pmic4_bob[REGULATOR_MODE_STANDBY + 1] = {
548 	[REGULATOR_MODE_INVALID] = -EINVAL,
549 	[REGULATOR_MODE_STANDBY] = -EINVAL,
550 	[REGULATOR_MODE_IDLE]    = PMIC4_BOB_MODE_PFM,
551 	[REGULATOR_MODE_NORMAL]  = PMIC4_BOB_MODE_AUTO,
552 	[REGULATOR_MODE_FAST]    = PMIC4_BOB_MODE_PWM,
553 };
554 
555 static unsigned int rpmh_regulator_pmic4_bob_of_map_mode(unsigned int rpmh_mode)
556 {
557 	unsigned int mode;
558 
559 	switch (rpmh_mode) {
560 	case RPMH_REGULATOR_MODE_HPM:
561 		mode = REGULATOR_MODE_FAST;
562 		break;
563 	case RPMH_REGULATOR_MODE_AUTO:
564 		mode = REGULATOR_MODE_NORMAL;
565 		break;
566 	case RPMH_REGULATOR_MODE_LPM:
567 		mode = REGULATOR_MODE_IDLE;
568 		break;
569 	default:
570 		mode = REGULATOR_MODE_INVALID;
571 		break;
572 	}
573 
574 	return mode;
575 }
576 
577 static const struct rpmh_vreg_hw_data pmic4_pldo = {
578 	.regulator_type = VRM,
579 	.ops = &rpmh_regulator_vrm_drms_ops,
580 	.voltage_range = REGULATOR_LINEAR_RANGE(1664000, 0, 255, 8000),
581 	.n_voltages = 256,
582 	.hpm_min_load_uA = 10000,
583 	.pmic_mode_map = pmic_mode_map_pmic4_ldo,
584 	.of_map_mode = rpmh_regulator_pmic4_ldo_of_map_mode,
585 };
586 
587 static const struct rpmh_vreg_hw_data pmic4_pldo_lv = {
588 	.regulator_type = VRM,
589 	.ops = &rpmh_regulator_vrm_drms_ops,
590 	.voltage_range = REGULATOR_LINEAR_RANGE(1256000, 0, 127, 8000),
591 	.n_voltages = 128,
592 	.hpm_min_load_uA = 10000,
593 	.pmic_mode_map = pmic_mode_map_pmic4_ldo,
594 	.of_map_mode = rpmh_regulator_pmic4_ldo_of_map_mode,
595 };
596 
597 static const struct rpmh_vreg_hw_data pmic4_nldo = {
598 	.regulator_type = VRM,
599 	.ops = &rpmh_regulator_vrm_drms_ops,
600 	.voltage_range = REGULATOR_LINEAR_RANGE(312000, 0, 127, 8000),
601 	.n_voltages = 128,
602 	.hpm_min_load_uA = 30000,
603 	.pmic_mode_map = pmic_mode_map_pmic4_ldo,
604 	.of_map_mode = rpmh_regulator_pmic4_ldo_of_map_mode,
605 };
606 
607 static const struct rpmh_vreg_hw_data pmic4_hfsmps3 = {
608 	.regulator_type = VRM,
609 	.ops = &rpmh_regulator_vrm_ops,
610 	.voltage_range = REGULATOR_LINEAR_RANGE(320000, 0, 215, 8000),
611 	.n_voltages = 216,
612 	.pmic_mode_map = pmic_mode_map_pmic4_smps,
613 	.of_map_mode = rpmh_regulator_pmic4_smps_of_map_mode,
614 };
615 
616 static const struct rpmh_vreg_hw_data pmic4_ftsmps426 = {
617 	.regulator_type = VRM,
618 	.ops = &rpmh_regulator_vrm_ops,
619 	.voltage_range = REGULATOR_LINEAR_RANGE(320000, 0, 258, 4000),
620 	.n_voltages = 259,
621 	.pmic_mode_map = pmic_mode_map_pmic4_smps,
622 	.of_map_mode = rpmh_regulator_pmic4_smps_of_map_mode,
623 };
624 
625 static const struct rpmh_vreg_hw_data pmic4_bob = {
626 	.regulator_type = VRM,
627 	.ops = &rpmh_regulator_vrm_bypass_ops,
628 	.voltage_range = REGULATOR_LINEAR_RANGE(1824000, 0, 83, 32000),
629 	.n_voltages = 84,
630 	.pmic_mode_map = pmic_mode_map_pmic4_bob,
631 	.of_map_mode = rpmh_regulator_pmic4_bob_of_map_mode,
632 };
633 
634 static const struct rpmh_vreg_hw_data pmic4_lvs = {
635 	.regulator_type = XOB,
636 	.ops = &rpmh_regulator_xob_ops,
637 	/* LVS hardware does not support voltage or mode configuration. */
638 };
639 
640 #define RPMH_VREG(_name, _resource_name, _hw_data, _supply_name) \
641 { \
642 	.name		= _name, \
643 	.resource_name	= _resource_name, \
644 	.hw_data	= _hw_data, \
645 	.supply_name	= _supply_name, \
646 }
647 
648 static const struct rpmh_vreg_init_data pm8998_vreg_data[] = {
649 	RPMH_VREG("smps1",  "smp%s1",  &pmic4_ftsmps426, "vdd-s1"),
650 	RPMH_VREG("smps2",  "smp%s2",  &pmic4_ftsmps426, "vdd-s2"),
651 	RPMH_VREG("smps3",  "smp%s3",  &pmic4_hfsmps3,   "vdd-s3"),
652 	RPMH_VREG("smps4",  "smp%s4",  &pmic4_hfsmps3,   "vdd-s4"),
653 	RPMH_VREG("smps5",  "smp%s5",  &pmic4_hfsmps3,   "vdd-s5"),
654 	RPMH_VREG("smps6",  "smp%s6",  &pmic4_ftsmps426, "vdd-s6"),
655 	RPMH_VREG("smps7",  "smp%s7",  &pmic4_ftsmps426, "vdd-s7"),
656 	RPMH_VREG("smps8",  "smp%s8",  &pmic4_ftsmps426, "vdd-s8"),
657 	RPMH_VREG("smps9",  "smp%s9",  &pmic4_ftsmps426, "vdd-s9"),
658 	RPMH_VREG("smps10", "smp%s10", &pmic4_ftsmps426, "vdd-s10"),
659 	RPMH_VREG("smps11", "smp%s11", &pmic4_ftsmps426, "vdd-s11"),
660 	RPMH_VREG("smps12", "smp%s12", &pmic4_ftsmps426, "vdd-s12"),
661 	RPMH_VREG("smps13", "smp%s13", &pmic4_ftsmps426, "vdd-s13"),
662 	RPMH_VREG("ldo1",   "ldo%s1",  &pmic4_nldo,      "vdd-l1-l27"),
663 	RPMH_VREG("ldo2",   "ldo%s2",  &pmic4_nldo,      "vdd-l2-l8-l17"),
664 	RPMH_VREG("ldo3",   "ldo%s3",  &pmic4_nldo,      "vdd-l3-l11"),
665 	RPMH_VREG("ldo4",   "ldo%s4",  &pmic4_nldo,      "vdd-l4-l5"),
666 	RPMH_VREG("ldo5",   "ldo%s5",  &pmic4_nldo,      "vdd-l4-l5"),
667 	RPMH_VREG("ldo6",   "ldo%s6",  &pmic4_pldo,      "vdd-l6"),
668 	RPMH_VREG("ldo7",   "ldo%s7",  &pmic4_pldo_lv,   "vdd-l7-l12-l14-l15"),
669 	RPMH_VREG("ldo8",   "ldo%s8",  &pmic4_nldo,      "vdd-l2-l8-l17"),
670 	RPMH_VREG("ldo9",   "ldo%s9",  &pmic4_pldo,      "vdd-l9"),
671 	RPMH_VREG("ldo10",  "ldo%s10", &pmic4_pldo,      "vdd-l10-l23-l25"),
672 	RPMH_VREG("ldo11",  "ldo%s11", &pmic4_nldo,      "vdd-l3-l11"),
673 	RPMH_VREG("ldo12",  "ldo%s12", &pmic4_pldo_lv,   "vdd-l7-l12-l14-l15"),
674 	RPMH_VREG("ldo13",  "ldo%s13", &pmic4_pldo,      "vdd-l13-l19-l21"),
675 	RPMH_VREG("ldo14",  "ldo%s14", &pmic4_pldo_lv,   "vdd-l7-l12-l14-l15"),
676 	RPMH_VREG("ldo15",  "ldo%s15", &pmic4_pldo_lv,   "vdd-l7-l12-l14-l15"),
677 	RPMH_VREG("ldo16",  "ldo%s16", &pmic4_pldo,      "vdd-l16-l28"),
678 	RPMH_VREG("ldo17",  "ldo%s17", &pmic4_nldo,      "vdd-l2-l8-l17"),
679 	RPMH_VREG("ldo18",  "ldo%s18", &pmic4_pldo,      "vdd-l18-l22"),
680 	RPMH_VREG("ldo19",  "ldo%s19", &pmic4_pldo,      "vdd-l13-l19-l21"),
681 	RPMH_VREG("ldo20",  "ldo%s20", &pmic4_pldo,      "vdd-l20-l24"),
682 	RPMH_VREG("ldo21",  "ldo%s21", &pmic4_pldo,      "vdd-l13-l19-l21"),
683 	RPMH_VREG("ldo22",  "ldo%s22", &pmic4_pldo,      "vdd-l18-l22"),
684 	RPMH_VREG("ldo23",  "ldo%s23", &pmic4_pldo,      "vdd-l10-l23-l25"),
685 	RPMH_VREG("ldo24",  "ldo%s24", &pmic4_pldo,      "vdd-l20-l24"),
686 	RPMH_VREG("ldo25",  "ldo%s25", &pmic4_pldo,      "vdd-l10-l23-l25"),
687 	RPMH_VREG("ldo26",  "ldo%s26", &pmic4_nldo,      "vdd-l26"),
688 	RPMH_VREG("ldo27",  "ldo%s27", &pmic4_nldo,      "vdd-l1-l27"),
689 	RPMH_VREG("ldo28",  "ldo%s28", &pmic4_pldo,      "vdd-l16-l28"),
690 	RPMH_VREG("lvs1",   "vs%s1",   &pmic4_lvs,       "vin-lvs-1-2"),
691 	RPMH_VREG("lvs2",   "vs%s2",   &pmic4_lvs,       "vin-lvs-1-2"),
692 	{},
693 };
694 
695 static const struct rpmh_vreg_init_data pmi8998_vreg_data[] = {
696 	RPMH_VREG("bob",    "bob%s1",  &pmic4_bob,       "vdd-bob"),
697 	{},
698 };
699 
700 static const struct rpmh_vreg_init_data pm8005_vreg_data[] = {
701 	RPMH_VREG("smps1",  "smp%s1",  &pmic4_ftsmps426, "vdd-s1"),
702 	RPMH_VREG("smps2",  "smp%s2",  &pmic4_ftsmps426, "vdd-s2"),
703 	RPMH_VREG("smps3",  "smp%s3",  &pmic4_ftsmps426, "vdd-s3"),
704 	RPMH_VREG("smps4",  "smp%s4",  &pmic4_ftsmps426, "vdd-s4"),
705 	{},
706 };
707 
708 static int rpmh_regulator_probe(struct platform_device *pdev)
709 {
710 	struct device *dev = &pdev->dev;
711 	const struct rpmh_vreg_init_data *vreg_data;
712 	struct device_node *node;
713 	struct rpmh_vreg *vreg;
714 	const char *pmic_id;
715 	int ret;
716 
717 	vreg_data = of_device_get_match_data(dev);
718 	if (!vreg_data)
719 		return -ENODEV;
720 
721 	ret = of_property_read_string(dev->of_node, "qcom,pmic-id", &pmic_id);
722 	if (ret < 0) {
723 		dev_err(dev, "qcom,pmic-id missing in DT node\n");
724 		return ret;
725 	}
726 
727 	for_each_available_child_of_node(dev->of_node, node) {
728 		vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
729 		if (!vreg) {
730 			of_node_put(node);
731 			return -ENOMEM;
732 		}
733 
734 		ret = rpmh_regulator_init_vreg(vreg, dev, node, pmic_id,
735 						vreg_data);
736 		if (ret < 0) {
737 			of_node_put(node);
738 			return ret;
739 		}
740 	}
741 
742 	return 0;
743 }
744 
745 static const struct of_device_id rpmh_regulator_match_table[] = {
746 	{
747 		.compatible = "qcom,pm8998-rpmh-regulators",
748 		.data = pm8998_vreg_data,
749 	},
750 	{
751 		.compatible = "qcom,pmi8998-rpmh-regulators",
752 		.data = pmi8998_vreg_data,
753 	},
754 	{
755 		.compatible = "qcom,pm8005-rpmh-regulators",
756 		.data = pm8005_vreg_data,
757 	},
758 	{}
759 };
760 MODULE_DEVICE_TABLE(of, rpmh_regulator_match_table);
761 
762 static struct platform_driver rpmh_regulator_driver = {
763 	.driver = {
764 		.name = "qcom-rpmh-regulator",
765 		.of_match_table	= of_match_ptr(rpmh_regulator_match_table),
766 	},
767 	.probe = rpmh_regulator_probe,
768 };
769 module_platform_driver(rpmh_regulator_driver);
770 
771 MODULE_DESCRIPTION("Qualcomm RPMh regulator driver");
772 MODULE_LICENSE("GPL v2");
773