blob: 562b05a8fdc798e502ec7a3549d2266332f01121 [file] [log] [blame]
David Collinsd86a1722017-02-07 15:42:22 -08001/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
David Collinse1b43192016-10-12 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/bitops.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/string.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/machine.h>
26#include <linux/regulator/of_regulator.h>
27#include <soc/qcom/cmd-db.h>
28#include <soc/qcom/rpmh.h>
29#include <dt-bindings/regulator/qcom,rpmh-regulator.h>
30
31/**
32 * enum rpmh_regulator_type - supported RPMh accelerator types
33 * %RPMH_REGULATOR_TYPE_VRM: RPMh VRM accelerator which supports voting on
34 * enable, voltage, mode, and headroom voltage of
35 * LDO, SMPS, VS, and BOB type PMIC regulators.
36 * %RPMH_REGULATOR_TYPE_ARC: RPMh ARC accelerator which supports voting on
37 * the CPR managed voltage level of LDO and SMPS
38 * type PMIC regulators.
39 */
40enum rpmh_regulator_type {
41 RPMH_REGULATOR_TYPE_VRM,
42 RPMH_REGULATOR_TYPE_ARC,
43};
44
45/**
46 * enum rpmh_regulator_reg_index - RPMh accelerator register indices
47 * %RPMH_REGULATOR_REG_VRM_VOLTAGE: VRM voltage voting register index
48 * %RPMH_REGULATOR_REG_ARC_LEVEL: ARC voltage level voting register index
49 * %RPMH_REGULATOR_REG_VRM_ENABLE: VRM enable voltage voting register index
50 * %RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE: Place-holder for enable aggregation.
51 * ARC does not have a specific register
52 * for enable voting. Instead, ARC level
53 * 0 corresponds to "disabled" for a given
54 * ARC regulator resource if supported.
55 * %RPMH_REGULATOR_REG_ENABLE: Common enable index used in callback
56 * functions for both ARC and VRM.
57 * %RPMH_REGULATOR_REG_VRM_MODE: VRM regulator mode voting register index
58 * %RPMH_REGULATOR_REG_VRM_HEADROOM: VRM headroom voltage voting register
59 * index
60 * %RPMH_REGULATOR_REG_ARC_REAL_MAX: Upper limit of real existent ARC
61 * register indices
62 * %RPMH_REGULATOR_REG_ARC_MAX: Exclusive upper limit of ARC register
63 * indices
64 * %RPMH_REGULATOR_REG_VRM_MAX: Exclusive upper limit of VRM register
65 * indices
66 * %RPMH_REGULATOR_REG_MAX: Combined exclusive upper limit of ARC
67 * and VRM register indices
68 *
69 * Register addresses are calculated as: base_addr + sizeof(u32) * reg_index
70 */
71enum rpmh_regulator_reg_index {
72 RPMH_REGULATOR_REG_VRM_VOLTAGE = 0,
73 RPMH_REGULATOR_REG_ARC_LEVEL = 0,
74 RPMH_REGULATOR_REG_VRM_ENABLE = 1,
75 RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE = RPMH_REGULATOR_REG_VRM_ENABLE,
76 RPMH_REGULATOR_REG_ENABLE = RPMH_REGULATOR_REG_VRM_ENABLE,
77 RPMH_REGULATOR_REG_VRM_MODE = 2,
78 RPMH_REGULATOR_REG_VRM_HEADROOM = 3,
79 RPMH_REGULATOR_REG_ARC_REAL_MAX = 1,
80 RPMH_REGULATOR_REG_ARC_MAX = 2,
81 RPMH_REGULATOR_REG_VRM_MAX = 4,
82 RPMH_REGULATOR_REG_MAX = 4,
83};
84
85/*
86 * This is the number of bytes used for each command DB aux data entry of an
87 * ARC resource.
88 */
89#define RPMH_ARC_LEVEL_SIZE 2
90
91/*
92 * This is the maximum number of voltage levels that may be defined for an ARC
93 * resource.
94 */
95#define RPMH_ARC_MAX_LEVELS 16
96
97/* Min and max limits of VRM resource request parameters */
98#define RPMH_VRM_MIN_UV 0
99#define RPMH_VRM_MAX_UV 8191000
100
101#define RPMH_VRM_HEADROOM_MIN_UV 0
102#define RPMH_VRM_HEADROOM_MAX_UV 511000
103
104#define RPMH_VRM_MODE_MIN 0
105#define RPMH_VRM_MODE_MAX 7
106
107/*
108 * Mapping from RPMh VRM accelerator modes to regulator framework modes
109 * Assumes that SMPS PFM mode == LDO LPM mode and SMPS PWM mode == LDO HPM mode
110 */
111static const int rpmh_regulator_mode_map[] = {
112 [RPMH_REGULATOR_MODE_SMPS_PFM] = REGULATOR_MODE_IDLE,
113 [RPMH_REGULATOR_MODE_SMPS_AUTO] = REGULATOR_MODE_NORMAL,
114 [RPMH_REGULATOR_MODE_SMPS_PWM] = REGULATOR_MODE_FAST,
115 [RPMH_REGULATOR_MODE_BOB_PASS] = REGULATOR_MODE_STANDBY,
116 [RPMH_REGULATOR_MODE_BOB_PFM] = REGULATOR_MODE_IDLE,
117 [RPMH_REGULATOR_MODE_BOB_AUTO] = REGULATOR_MODE_NORMAL,
118 [RPMH_REGULATOR_MODE_BOB_PWM] = REGULATOR_MODE_FAST,
119};
120
121/**
122 * struct rpmh_regulator_request - rpmh request data
123 * @reg: Array of RPMh accelerator register values
124 * @valid: Bitmask identifying which of the register values
125 * are valid/initialized
126 */
127struct rpmh_regulator_request {
128 u32 reg[RPMH_REGULATOR_REG_MAX];
129 u32 valid;
130};
131
132/**
133 * struct rpmh_regulator_mode - RPMh VRM mode attributes
134 * @pmic_mode: Raw PMIC mode value written into VRM mode voting
135 * register (i.e. RPMH_REGULATOR_MODE_*)
136 * @framework_mode: Regulator framework mode value
137 * (i.e. REGULATOR_MODE_*)
138 * @min_load_ua: The minimum load current in microamps which
139 * would utilize this mode
140 *
141 * Software selects the lowest mode for which aggr_load_ua >= min_load_ua.
142 */
143struct rpmh_regulator_mode {
144 u32 pmic_mode;
145 u32 framework_mode;
146 int min_load_ua;
147};
148
149struct rpmh_vreg;
150
151/**
152 * struct rpmh_aggr_vreg - top level aggregated rpmh regulator resource data
153 * structure
154 * @dev: Device pointer to the rpmh aggregated regulator
155 * device
156 * @resource_name: Name of rpmh regulator resource which is mapped
157 * to an RPMh accelerator address via command DB.
158 * This name must match to one that is defined by
159 * the bootloader.
160 * @addr: Base address of the regulator resource within
161 * an RPMh accelerator
162 * @rpmh_client: Handle used for rpmh communications
163 * @lock: Mutex lock used for locking between regulators
164 * common to a single aggregated resource
165 * @regulator_type: RPMh accelerator type for this regulator
166 * resource
167 * @level: Mapping from ARC resource specific voltage
168 * levels (0 to RPMH_ARC_MAX_LEVELS - 1) to common
169 * consumer voltage levels (i.e.
170 * RPMH_REGULATOR_LEVEL_*). These values are read
171 * out of the AUX data found in command DB for a
172 * given ARC resource. Note that the values in
173 * this array are equal to those read from AUX data
174 * + RPMH_REGULATOR_LEVEL_OFFSET.
175 * @level_count: The number of valid entries in the level array
176 * @always_wait_for_ack: Boolean flag indicating if a request must always
177 * wait for an ACK from RPMh before continuing even
178 * if it corresponds to a strictly lower power
179 * state (e.g. enabled --> disabled).
180 * @next_wait_for_ack: Boolean flag indicating that the next request
181 * sent must wait for an ACK. This is used to
182 * ensure that the driver waits for the voltage to
183 * slew down in the case that the requested max_uV
184 * value is lower than the last requested voltage.
185 * @sleep_request_sent: Boolean flag indicating that a sleep set request
186 * has been sent at some point due to it diverging
187 * from the active set request. After that point,
188 * the sleep set requests must always be sent for
189 * a given resource.
190 * @use_awake_state: Boolean flag indicating that active set requests
191 * should be made using the awake state instead of
192 * the active-only state. This should be used for
193 * RSC's which do not have an AMC.
194 * @vreg: Array of rpmh regulator structs representing the
195 * individual regulators sharing the aggregated
196 * regulator resource.
197 * @vreg_count: The number of entries in the vreg array.
198 * @mode: An array of modes supported by an RPMh VRM
199 * regulator resource.
200 * @mode_count: The number of entries in the mode array.
201 * @aggr_req_active: Aggregated active set RPMh accelerator register
202 * request
203 * @aggr_req_sleep: Aggregated sleep set RPMh accelerator register
204 * request
205 */
206struct rpmh_aggr_vreg {
207 struct device *dev;
208 const char *resource_name;
209 u32 addr;
210 struct rpmh_client *rpmh_client;
211 struct mutex lock;
212 enum rpmh_regulator_type regulator_type;
213 u32 level[RPMH_ARC_MAX_LEVELS];
214 int level_count;
215 bool always_wait_for_ack;
216 bool next_wait_for_ack;
217 bool sleep_request_sent;
218 bool use_awake_state;
219 struct rpmh_vreg *vreg;
220 int vreg_count;
221 struct rpmh_regulator_mode *mode;
222 int mode_count;
223 struct rpmh_regulator_request aggr_req_active;
224 struct rpmh_regulator_request aggr_req_sleep;
225};
226
227/**
228 * struct rpmh_vreg - individual rpmh regulator data structure encapsulating a
229 * regulator framework regulator device and its corresponding
230 * rpmh request
231 * @of_node: Device node pointer for the individual rpmh
232 * regulator
233 * @name: Name of the regulator
234 * @rdesc: Regulator descriptor
235 * @rdev: Regulator device pointer returned by
236 * devm_regulator_register()
237 * @aggr_vreg: Pointer to the aggregated rpmh regulator
238 * resource
239 * @set_active: Boolean flag indicating that requests made by
240 * this regulator should take affect in the active
241 * set
242 * @set_sleep: Boolean flag indicating that requests made by
243 * this regulator should take affect in the sleep
244 * set
245 * @req: RPMh accelerator register request
246 * @mode_index: RPMh VRM regulator mode selected by index into
247 * aggr_vreg->mode
248 */
249struct rpmh_vreg {
250 struct device_node *of_node;
251 struct regulator_desc rdesc;
252 struct regulator_dev *rdev;
253 struct rpmh_aggr_vreg *aggr_vreg;
254 bool set_active;
255 bool set_sleep;
256 struct rpmh_regulator_request req;
257 int mode_index;
258};
259
260/*
261 * This voltage in uV is returned by get_voltage functions when there is no way
262 * to determine the current voltage level. It is needed because the regulator
263 * framework treats a 0 uV voltage as an error.
264 */
265#define VOLTAGE_UNKNOWN 1
266
267#define vreg_err(vreg, message, ...) \
268 pr_err("%s: " message, (vreg)->rdesc.name, ##__VA_ARGS__)
269#define vreg_info(vreg, message, ...) \
270 pr_info("%s: " message, (vreg)->rdesc.name, ##__VA_ARGS__)
271#define vreg_debug(vreg, message, ...) \
272 pr_debug("%s: " message, (vreg)->rdesc.name, ##__VA_ARGS__)
273
274#define aggr_vreg_err(aggr_vreg, message, ...) \
275 pr_err("%s: " message, (aggr_vreg)->resource_name, ##__VA_ARGS__)
276#define aggr_vreg_info(aggr_vreg, message, ...) \
277 pr_info("%s: " message, (aggr_vreg)->resource_name, ##__VA_ARGS__)
278#define aggr_vreg_debug(aggr_vreg, message, ...) \
279 pr_debug("%s: " message, (aggr_vreg)->resource_name, ##__VA_ARGS__)
280
281#define DEBUG_PRINT_BUFFER_SIZE 256
282static const char *const rpmh_regulator_state_names[] = {
283 [RPMH_SLEEP_STATE] = "sleep ",
284 [RPMH_WAKE_ONLY_STATE] = "wake ",
285 [RPMH_ACTIVE_ONLY_STATE] = "active",
286 [RPMH_AWAKE_STATE] = "awake ",
287};
288
289static const char *const rpmh_regulator_vrm_param_names[] = {
290 [RPMH_REGULATOR_REG_VRM_VOLTAGE] = "mv",
291 [RPMH_REGULATOR_REG_VRM_ENABLE] = "en",
292 [RPMH_REGULATOR_REG_VRM_MODE] = "mode",
293 [RPMH_REGULATOR_REG_VRM_HEADROOM] = "hr_mv",
294};
295
296static const char *const rpmh_regulator_arc_param_names[] = {
297 [RPMH_REGULATOR_REG_ARC_LEVEL] = "hlvl",
298};
299
300/**
301 * rpmh_regulator_req() - print the rpmh regulator request to the kernel log
302 * @vreg: Pointer to the RPMh regulator
303 * @current_req: Pointer to the new request
304 * @prev_req: Pointer to the last request
305 * @sent_mask: Bitmask which specifies the parameters sent in this
306 * request
307 * @state: The rpmh state that the request was sent for
308 *
309 * Return: none
310 */
311static void rpmh_regulator_req(struct rpmh_vreg *vreg,
312 struct rpmh_regulator_request *current_req,
313 struct rpmh_regulator_request *prev_req,
314 u32 sent_mask,
315 enum rpmh_state state)
316{
317 struct rpmh_aggr_vreg *aggr_vreg = vreg->aggr_vreg;
318 char buf[DEBUG_PRINT_BUFFER_SIZE];
319 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
320 const char *const *param_name;
321 int i, max_reg_index;
322 int pos = 0;
323 u32 valid;
324 bool first;
325
326 max_reg_index = aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM
327 ? RPMH_REGULATOR_REG_VRM_MAX
328 : RPMH_REGULATOR_REG_ARC_REAL_MAX;
329 param_name = aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM
330 ? rpmh_regulator_vrm_param_names
331 : rpmh_regulator_arc_param_names;
332
333 pos += scnprintf(buf + pos, buflen - pos,
334 "%s (%s), addr=0x%05X: s=%s; sent: ",
335 aggr_vreg->resource_name, vreg->rdesc.name,
336 aggr_vreg->addr, rpmh_regulator_state_names[state]);
337
338 valid = sent_mask;
339 first = true;
340 for (i = 0; i < max_reg_index; i++) {
341 if (valid & BIT(i)) {
342 pos += scnprintf(buf + pos, buflen - pos, "%s%s=%u",
343 (first ? "" : ", "), param_name[i],
344 current_req->reg[i]);
345 first = false;
346 if (aggr_vreg->regulator_type
347 == RPMH_REGULATOR_TYPE_ARC
348 && i == RPMH_REGULATOR_REG_ARC_LEVEL)
349 pos += scnprintf(buf + pos, buflen - pos,
350 " (vlvl=%u)",
351 aggr_vreg->level[current_req->reg[i]]);
352 }
353 }
354
355 valid = prev_req->valid & ~sent_mask;
356
357 if (valid)
358 pos += scnprintf(buf + pos, buflen - pos, "; prev: ");
359 first = true;
360 for (i = 0; i < max_reg_index; i++) {
361 if (valid & BIT(i)) {
362 pos += scnprintf(buf + pos, buflen - pos, "%s%s=%u",
363 (first ? "" : ", "), param_name[i],
364 current_req->reg[i]);
365 first = false;
366 if (aggr_vreg->regulator_type
367 == RPMH_REGULATOR_TYPE_ARC
368 && i == RPMH_REGULATOR_REG_ARC_LEVEL)
369 pos += scnprintf(buf + pos, buflen - pos,
370 " (vlvl=%u)",
371 aggr_vreg->level[current_req->reg[i]]);
372 }
373 }
374
375 pr_debug("%s\n", buf);
376}
377
378/**
379 * rpmh_regulator_handle_arc_enable() - handle masking of the voltage level
380 * request based on the pseudo-enable value
381 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
382 * @req Pointer to the newly aggregated request
383 *
384 * Return: none
385 */
386static void rpmh_regulator_handle_arc_enable(struct rpmh_aggr_vreg *aggr_vreg,
387 struct rpmh_regulator_request *req)
388{
389 if (aggr_vreg->regulator_type != RPMH_REGULATOR_TYPE_ARC)
390 return;
391
392 /*
393 * Mask the voltage level if "off" level is supported and the regulator
394 * has not been enabled.
395 */
David Collins1e892eac2017-05-30 17:55:17 -0700396 if (aggr_vreg->level[0] == RPMH_REGULATOR_LEVEL_OFF) {
397 if (req->valid & BIT(RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE)) {
398 if (!req->reg[RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE])
399 req->reg[RPMH_REGULATOR_REG_ARC_LEVEL] = 0;
400 } else {
401 /* Invalidate voltage level if enable is invalid. */
402 req->valid &= ~BIT(RPMH_REGULATOR_REG_ARC_LEVEL);
403 }
404 }
David Collinse1b43192016-10-12 17:27:22 -0700405
406 /*
407 * Mark the pseudo enable bit as invalid so that it is not accidentally
408 * included in an RPMh command.
409 */
410 req->valid &= ~BIT(RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE);
411}
412
413/**
414 * rpmh_regulator_send_aggregate_requests() - aggregate the requests from all
415 * regulators associated with an RPMh resource and send the request
416 * to RPMh
417 * @vreg: Pointer to the RPMh regulator
418 *
419 * This function aggregates the requests from the different regulators
420 * associated with the aggr_vreg resource independently in both the active set
421 * and sleep set. The requests are only sent for the sleep set if they differ,
422 * or have differed in the past, from those of the active set.
423 *
424 * Return: 0 on success, errno on failure
425 */
426static int
427rpmh_regulator_send_aggregate_requests(struct rpmh_vreg *vreg)
428{
429 struct rpmh_aggr_vreg *aggr_vreg = vreg->aggr_vreg;
430 struct rpmh_regulator_request req_active = { {0} };
431 struct rpmh_regulator_request req_sleep = { {0} };
432 struct tcs_cmd cmd[RPMH_REGULATOR_REG_MAX] = { {0} };
433 bool sleep_set_differs = aggr_vreg->sleep_request_sent;
434 bool wait_for_ack = aggr_vreg->always_wait_for_ack
435 || aggr_vreg->next_wait_for_ack;
David Collins5f2e44f2017-08-08 13:35:49 -0700436 bool resend_active = false;
David Collinse1b43192016-10-12 17:27:22 -0700437 int i, j, max_reg_index, rc;
438 enum rpmh_state state;
439 u32 sent_mask;
440
441 max_reg_index = aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM
442 ? RPMH_REGULATOR_REG_VRM_MAX
443 : RPMH_REGULATOR_REG_ARC_MAX;
444 /*
445 * Perform max aggregration of each register value across all regulators
446 * which use this RPMh resource.
447 */
448 for (i = 0; i < aggr_vreg->vreg_count; i++) {
449 if (aggr_vreg->vreg[i].set_active) {
450 for (j = 0; j < max_reg_index; j++)
451 req_active.reg[j] = max(req_active.reg[j],
452 aggr_vreg->vreg[i].req.reg[j]);
453 req_active.valid |= aggr_vreg->vreg[i].req.valid;
454 }
455 if (aggr_vreg->vreg[i].set_sleep) {
456 for (j = 0; j < max_reg_index; j++)
457 req_sleep.reg[j] = max(req_sleep.reg[j],
458 aggr_vreg->vreg[i].req.reg[j]);
459 req_sleep.valid |= aggr_vreg->vreg[i].req.valid;
460 }
461 }
462
463 rpmh_regulator_handle_arc_enable(aggr_vreg, &req_active);
464 rpmh_regulator_handle_arc_enable(aggr_vreg, &req_sleep);
465
466 /*
467 * Check if the aggregated sleep set parameter values differ from the
468 * aggregated active set parameter values.
469 */
470 if (!aggr_vreg->sleep_request_sent) {
471 for (i = 0; i < max_reg_index; i++) {
472 if ((req_active.reg[i] != req_sleep.reg[i])
473 && (req_sleep.valid & BIT(i))) {
474 sleep_set_differs = true;
David Collins5f2e44f2017-08-08 13:35:49 -0700475 /*
476 * Resend full active set request so that
477 * all parameters are specified in the wake-only
478 * state request.
479 */
480 resend_active = !aggr_vreg->use_awake_state;
David Collinse1b43192016-10-12 17:27:22 -0700481 break;
482 }
483 }
484 }
485
486 if (sleep_set_differs) {
487 /*
488 * Generate an rpmh command consisting of only those registers
489 * which have new values or which have never been touched before
490 * (i.e. those that were previously not valid).
491 */
492 sent_mask = 0;
493 for (i = 0, j = 0; i < max_reg_index; i++) {
494 if ((req_sleep.valid & BIT(i))
495 && (!(aggr_vreg->aggr_req_sleep.valid & BIT(i))
496 || aggr_vreg->aggr_req_sleep.reg[i]
497 != req_sleep.reg[i])) {
498 cmd[j].addr = aggr_vreg->addr + i * 4;
499 cmd[j].data = req_sleep.reg[i];
500 j++;
501 sent_mask |= BIT(i);
502 }
503 }
504
505 /* Send the rpmh command if any register values differ. */
506 if (j > 0) {
507 rc = rpmh_write_async(aggr_vreg->rpmh_client,
508 RPMH_SLEEP_STATE, cmd, j);
509 if (rc) {
510 aggr_vreg_err(aggr_vreg, "sleep state rpmh_write_async() failed, rc=%d\n",
511 rc);
512 return rc;
513 }
514 rpmh_regulator_req(vreg, &req_sleep,
515 &aggr_vreg->aggr_req_sleep,
516 sent_mask,
517 RPMH_SLEEP_STATE);
518 aggr_vreg->sleep_request_sent = true;
519 aggr_vreg->aggr_req_sleep = req_sleep;
520 }
521 }
522
523 /*
524 * Generate an rpmh command consisting of only those registers
525 * which have new values or which have never been touched before
526 * (i.e. those that were previously not valid).
527 */
528 sent_mask = 0;
529 for (i = 0, j = 0; i < max_reg_index; i++) {
530 if ((req_active.valid & BIT(i))
531 && (!(aggr_vreg->aggr_req_active.valid & BIT(i))
532 || aggr_vreg->aggr_req_active.reg[i]
David Collins5f2e44f2017-08-08 13:35:49 -0700533 != req_active.reg[i] || resend_active)) {
David Collinse1b43192016-10-12 17:27:22 -0700534 cmd[j].addr = aggr_vreg->addr + i * 4;
535 cmd[j].data = req_active.reg[i];
536 j++;
537 sent_mask |= BIT(i);
538
539 /*
540 * Must wait for ACK from RPMh if power state is
541 * increasing
542 */
543 if (req_active.reg[i]
544 > aggr_vreg->aggr_req_active.reg[i])
545 wait_for_ack = true;
546 }
547 }
548
549 /* Send the rpmh command if any register values differ. */
550 if (j > 0) {
551 if (sleep_set_differs && !aggr_vreg->use_awake_state) {
552 state = RPMH_WAKE_ONLY_STATE;
553 rc = rpmh_write_async(aggr_vreg->rpmh_client, state,
554 cmd, j);
555 if (rc) {
556 aggr_vreg_err(aggr_vreg, "%s state rpmh_write_async() failed, rc=%d\n",
557 rpmh_regulator_state_names[state], rc);
558 return rc;
559 }
560 rpmh_regulator_req(vreg, &req_active,
561 &aggr_vreg->aggr_req_active, sent_mask, state);
562 }
563
564 state = aggr_vreg->use_awake_state ? RPMH_AWAKE_STATE
565 : RPMH_ACTIVE_ONLY_STATE;
566 if (wait_for_ack)
567 rc = rpmh_write(aggr_vreg->rpmh_client, state, cmd, j);
568 else
569 rc = rpmh_write_async(aggr_vreg->rpmh_client, state,
570 cmd, j);
571 if (rc) {
572 aggr_vreg_err(aggr_vreg, "%s state rpmh_write() failed, rc=%d\n",
573 rpmh_regulator_state_names[state], rc);
574 return rc;
575 }
576 rpmh_regulator_req(vreg, &req_active,
577 &aggr_vreg->aggr_req_active, sent_mask, state);
578
579 aggr_vreg->aggr_req_active = req_active;
580 aggr_vreg->next_wait_for_ack = false;
581 }
582
583 return 0;
584}
585
586/**
587 * rpmh_regulator_set_reg() - set a register value within the request for an
588 * RPMh regulator and return the previous value
589 * @vreg: Pointer to the RPMh regulator
590 * @reg_index: Index of the register value to update
591 * @value: New register value to set
592 *
593 * Return: old register value
594 */
595static u32 rpmh_regulator_set_reg(struct rpmh_vreg *vreg, int reg_index,
596 u32 value)
597{
598 u32 old_value;
599
600 old_value = vreg->req.reg[reg_index];
601 vreg->req.reg[reg_index] = value;
602 vreg->req.valid |= BIT(reg_index);
603
604 return old_value;
605}
606
607/**
608 * rpmh_regulator_check_param_max() - sets if the next request must wait for
609 * an ACK based on the previously sent reg[index] value and the new
610 * max value
611 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
612 * @index: Register index
613 * @new_max: Newly requested maximum allowed value for the parameter
614 *
615 * This function is used to handle the case when a consumer makes a new
616 * (min_uv, max_uv) range request in which the new max_uv is lower than the
617 * previously requested min_uv. In this case, the driver must wait for an ACK
618 * from RPMh to ensure that the voltage has completed reducing to the new min_uv
619 * value since the consumer cannot operate at the old min_uv value.
620 *
621 * Return: none
622 */
623static void rpmh_regulator_check_param_max(struct rpmh_aggr_vreg *aggr_vreg,
624 int index, u32 new_max)
625{
626 if ((aggr_vreg->aggr_req_active.valid & BIT(index))
627 && aggr_vreg->aggr_req_active.reg[index] > new_max)
628 aggr_vreg->next_wait_for_ack = true;
629}
630
631/**
632 * rpmh_regulator_is_enabled() - return the enable state of the RPMh
633 * regulator
634 * @rdev: Regulator device pointer for the rpmh-regulator
635 *
636 * This function is passed as a callback function into the regulator ops that
637 * are registered for each rpmh-regulator device.
638 *
639 * Note that for ARC resources, this value is effectively a flag indicating if
640 * the requested voltage level is masked or unmasked since "disabled" = voltage
641 * level 0 (if supported).
642 *
643 * Return: true if regulator is enabled, false if regulator is disabled
644 */
645static int rpmh_regulator_is_enabled(struct regulator_dev *rdev)
646{
647 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
648
649 return !!vreg->req.reg[RPMH_REGULATOR_REG_ENABLE];
650}
651
652/**
653 * rpmh_regulator_enable() - enable the RPMh regulator
654 * @rdev: Regulator device pointer for the rpmh-regulator
655 *
656 * This function is passed as a callback function into the regulator ops that
657 * are registered for each rpmh-regulator device.
658 *
659 * Note that for ARC devices the enable state is handled via the voltage level
660 * parameter. Therefore, this enable value effectively masks or unmasks the
661 * enabled voltage level.
662 *
663 * Return: 0 on success, errno on failure
664 */
665static int rpmh_regulator_enable(struct regulator_dev *rdev)
666{
667 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
668 u32 prev_enable;
669 int rc;
670
671 mutex_lock(&vreg->aggr_vreg->lock);
672
673 prev_enable
674 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE, 1);
675
676 rc = rpmh_regulator_send_aggregate_requests(vreg);
677 if (rc) {
678 vreg_err(vreg, "enable failed, rc=%d\n", rc);
679 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE,
680 prev_enable);
681 }
682
683 mutex_unlock(&vreg->aggr_vreg->lock);
684
685 return rc;
686}
687
688/**
689 * rpmh_regulator_disable() - disable the RPMh regulator
690 * @rdev: Regulator device pointer for the rpmh-regulator
691 *
692 * This function is passed as a callback function into the regulator ops that
693 * are registered for each rpmh-regulator device.
694 *
695 * Note that for ARC devices the enable state is handled via the voltage level
696 * parameter. Therefore, this enable value effectively masks or unmasks the
697 * enabled voltage level.
698 *
699 * Return: 0 on success, errno on failure
700 */
701static int rpmh_regulator_disable(struct regulator_dev *rdev)
702{
703 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
704 u32 prev_enable;
705 int rc;
706
707 mutex_lock(&vreg->aggr_vreg->lock);
708
709 prev_enable
710 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE, 0);
711
712 rc = rpmh_regulator_send_aggregate_requests(vreg);
713 if (rc) {
714 vreg_err(vreg, "disable failed, rc=%d\n", rc);
715 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE,
716 prev_enable);
717 }
718
719 mutex_unlock(&vreg->aggr_vreg->lock);
720
721 return rc;
722}
723
724/**
725 * rpmh_regulator_vrm_set_voltage() - set the voltage of the VRM rpmh-regulator
726 * @rdev: Regulator device pointer for the rpmh-regulator
727 * @min_uv: New voltage in microvolts to set
728 * @max_uv: Maximum voltage in microvolts allowed
729 * @selector: Unused
730 *
731 * This function is passed as a callback function into the regulator ops that
732 * are registered for each VRM rpmh-regulator device.
733 *
734 * Return: 0 on success, errno on failure
735 */
736static int rpmh_regulator_vrm_set_voltage(struct regulator_dev *rdev,
737 int min_uv, int max_uv, unsigned int *selector)
738{
739 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
740 u32 prev_voltage;
741 int mv;
742 int rc = 0;
743
744 mv = DIV_ROUND_UP(min_uv, 1000);
745 if (mv * 1000 > max_uv) {
746 vreg_err(vreg, "no set points available in range %d-%d uV\n",
747 min_uv, max_uv);
748 return -EINVAL;
749 }
750
751 mutex_lock(&vreg->aggr_vreg->lock);
752
753 prev_voltage
754 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_VOLTAGE, mv);
755 rpmh_regulator_check_param_max(vreg->aggr_vreg,
756 RPMH_REGULATOR_REG_VRM_VOLTAGE, max_uv);
757
758 rc = rpmh_regulator_send_aggregate_requests(vreg);
759 if (rc) {
760 vreg_err(vreg, "set voltage=%d mV failed, rc=%d\n", mv, rc);
761 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_VOLTAGE,
762 prev_voltage);
763 }
764
765 mutex_unlock(&vreg->aggr_vreg->lock);
766
767 return rc;
768}
769
770/**
771 * rpmh_regulator_vrm_get_voltage() - get the voltage of the VRM rpmh-regulator
772 * @rdev: Regulator device pointer for the rpmh-regulator
773 *
774 * This function is passed as a callback function into the regulator ops that
775 * are registered for each VRM rpmh-regulator device.
776 *
777 * Return: regulator voltage in microvolts
778 */
779static int rpmh_regulator_vrm_get_voltage(struct regulator_dev *rdev)
780{
781 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
782 int uv;
783
784 uv = vreg->req.reg[RPMH_REGULATOR_REG_VRM_VOLTAGE] * 1000;
785 if (uv == 0)
786 uv = VOLTAGE_UNKNOWN;
787
788 return uv;
789}
790
791/**
792 * rpmh_regulator_vrm_set_mode_index() - set the mode of a VRM regulator to the
793 * mode mapped to mode_index
794 * @vreg: Pointer to the RPMh regulator
795 * @mode_index: Index into aggr_vreg->mode[] array
796 *
797 * Return: 0 on success, errno on failure
798 */
799static int rpmh_regulator_vrm_set_mode_index(struct rpmh_vreg *vreg,
800 int mode_index)
801{
802 u32 prev_mode;
803 int rc;
804
805 mutex_lock(&vreg->aggr_vreg->lock);
806
807 prev_mode = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_MODE,
808 vreg->aggr_vreg->mode[mode_index].pmic_mode);
809
810 rc = rpmh_regulator_send_aggregate_requests(vreg);
811 if (rc) {
812 vreg_err(vreg, "set mode=%u failed, rc=%d\n",
813 vreg->req.reg[RPMH_REGULATOR_REG_VRM_MODE],
814 rc);
815 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_MODE,
816 prev_mode);
817 } else {
818 vreg->mode_index = mode_index;
819 }
820
821 mutex_unlock(&vreg->aggr_vreg->lock);
822
823 return rc;
824}
825
826/**
827 * rpmh_regulator_vrm_set_mode() - set the mode of the VRM rpmh-regulator
828 * @rdev: Regulator device pointer for the rpmh-regulator
829 * @mode: The regulator framework mode to set
830 *
831 * This function is passed as a callback function into the regulator ops that
832 * are registered for each VRM rpmh-regulator device.
833 *
834 * This function sets the PMIC mode corresponding to the specified framework
835 * mode. The set of PMIC modes allowed is defined in device tree for a given
836 * RPMh regulator resource. The full mapping from PMIC modes to framework modes
837 * is defined in the rpmh_regulator_mode_map[] array. The RPMh resource
838 * specific mapping is defined in the aggr_vreg->mode[] array.
839 *
840 * Return: 0 on success, errno on failure
841 */
842static int rpmh_regulator_vrm_set_mode(struct regulator_dev *rdev,
843 unsigned int mode)
844{
845 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
846 int i;
847
848 for (i = 0; i < vreg->aggr_vreg->mode_count; i++)
849 if (vreg->aggr_vreg->mode[i].framework_mode == mode)
850 break;
851 if (i >= vreg->aggr_vreg->mode_count) {
852 vreg_err(vreg, "invalid mode=%u\n", mode);
853 return -EINVAL;
854 }
855
856 return rpmh_regulator_vrm_set_mode_index(vreg, i);
857}
858
859/**
860 * rpmh_regulator_vrm_get_mode() - get the mode of the VRM rpmh-regulator
861 * @rdev: Regulator device pointer for the rpmh-regulator
862 *
863 * This function is passed as a callback function into the regulator ops that
864 * are registered for each VRM rpmh-regulator device.
865 *
866 * Return: the regulator framework mode of the regulator
867 */
868static unsigned int rpmh_regulator_vrm_get_mode(struct regulator_dev *rdev)
869{
870 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
871
872 return vreg->aggr_vreg->mode[vreg->mode_index].framework_mode;
873}
874
875/**
876 * rpmh_regulator_vrm_set_load() - set the PMIC mode based upon the maximum load
877 * required from the VRM rpmh-regulator
878 * @rdev: Regulator device pointer for the rpmh-regulator
879 * @load_ua: Maximum current required from all consumers in microamps
880 *
881 * This function is passed as a callback function into the regulator ops that
882 * are registered for each VRM rpmh-regulator device.
883 *
884 * This function sets the mode of the regulator to that which has the highest
885 * min support load less than or equal to load_ua. Example:
886 * mode_count = 3
887 * mode[].min_load_ua = 0, 100000, 6000000
888 *
889 * load_ua = 10000 --> mode_index = 0
890 * load_ua = 250000 --> mode_index = 1
891 * load_ua = 7000000 --> mode_index = 2
892 *
893 * Return: 0 on success, errno on failure
894 */
895static int rpmh_regulator_vrm_set_load(struct regulator_dev *rdev, int load_ua)
896{
897 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
898 int i;
899
900 /* No need to check element 0 as it will be the default. */
901 for (i = vreg->aggr_vreg->mode_count - 1; i > 0; i--)
902 if (vreg->aggr_vreg->mode[i].min_load_ua <= load_ua)
903 break;
904
905 return rpmh_regulator_vrm_set_mode_index(vreg, i);
906}
907
908/**
909 * rpmh_regulator_arc_set_voltage_sel() - set the voltage level of the ARC
910 * rpmh-regulator device
911 * @rdev: Regulator device pointer for the rpmh-regulator
912 * @selector: ARC voltage level to set
913 *
914 * This function is passed as a callback function into the regulator ops that
915 * are registered for each ARC rpmh-regulator device.
916 *
917 * Return: 0 on success, errno on failure
918 */
919static int rpmh_regulator_arc_set_voltage_sel(struct regulator_dev *rdev,
920 unsigned int selector)
921{
922 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
923 u32 prev_level;
924 int rc;
925
926 mutex_lock(&vreg->aggr_vreg->lock);
927
928 prev_level = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ARC_LEVEL,
929 selector);
930
931 rc = rpmh_regulator_send_aggregate_requests(vreg);
932 if (rc) {
933 vreg_err(vreg, "set level=%d failed, rc=%d\n",
934 vreg->req.reg[RPMH_REGULATOR_REG_ARC_LEVEL],
935 rc);
936 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ARC_LEVEL,
937 prev_level);
938 }
939
940 mutex_unlock(&vreg->aggr_vreg->lock);
941
942 return rc;
943}
944
945/**
946 * rpmh_regulator_arc_get_voltage_sel() - get the voltage level of the ARC
947 * rpmh-regulator device
948 * @rdev: Regulator device pointer for the rpmh-regulator
949 *
950 * This function is passed as a callback function into the regulator ops that
951 * are registered for each ARC rpmh-regulator device.
952 *
953 * Return: ARC voltage level
954 */
955static int rpmh_regulator_arc_get_voltage_sel(struct regulator_dev *rdev)
956{
957 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
958
959 return vreg->req.reg[RPMH_REGULATOR_REG_ARC_LEVEL];
960}
961
962/**
963 * rpmh_regulator_arc_list_voltage() - return the consumer voltage level mapped
964 * to a given ARC voltage level
965 * @rdev: Regulator device pointer for the rpmh-regulator
966 * @selector: ARC voltage level
967 *
968 * This function is passed as a callback function into the regulator ops that
969 * are registered for each ARC rpmh-regulator device.
970 *
971 * Data ranges:
972 * ARC voltage level: 0 - 15 (fixed in hardware)
973 * Consumer voltage level: 1 - 513 (could be expanded to larger values)
974 *
975 * Return: consumer voltage level
976 */
977static int rpmh_regulator_arc_list_voltage(struct regulator_dev *rdev,
978 unsigned int selector)
979{
980 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
981
982 if (selector >= vreg->aggr_vreg->level_count)
983 return 0;
984
985 return vreg->aggr_vreg->level[selector];
986}
987
988static const struct regulator_ops rpmh_regulator_vrm_ops = {
989 .enable = rpmh_regulator_enable,
990 .disable = rpmh_regulator_disable,
991 .is_enabled = rpmh_regulator_is_enabled,
992 .set_voltage = rpmh_regulator_vrm_set_voltage,
993 .get_voltage = rpmh_regulator_vrm_get_voltage,
994 .set_mode = rpmh_regulator_vrm_set_mode,
995 .get_mode = rpmh_regulator_vrm_get_mode,
996 .set_load = rpmh_regulator_vrm_set_load,
997};
998
999static const struct regulator_ops rpmh_regulator_arc_ops = {
1000 .enable = rpmh_regulator_enable,
1001 .disable = rpmh_regulator_disable,
1002 .is_enabled = rpmh_regulator_is_enabled,
1003 .set_voltage_sel = rpmh_regulator_arc_set_voltage_sel,
1004 .get_voltage_sel = rpmh_regulator_arc_get_voltage_sel,
1005 .list_voltage = rpmh_regulator_arc_list_voltage,
1006};
1007
1008static const struct regulator_ops *rpmh_regulator_ops[] = {
1009 [RPMH_REGULATOR_TYPE_VRM] = &rpmh_regulator_vrm_ops,
1010 [RPMH_REGULATOR_TYPE_ARC] = &rpmh_regulator_arc_ops,
1011};
1012
1013/**
1014 * rpmh_regulator_load_arc_level_mapping() - load the RPMh ARC resource's
1015 * voltage level mapping from command db
1016 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1017 *
1018 * The set of supported RPMH_REGULATOR_LEVEL_* voltage levels (0 - ~512) that
1019 * map to ARC operating levels (0 - 15) is defined in aux data per ARC resource
1020 * in the command db SMEM data structure. It is in a u16 array with 1 to 16
1021 * elements. Note that the aux data array may be zero padded at the end for
1022 * data alignment purposes. Such padding entries are invalid and must be
1023 * ignored.
1024 *
1025 * Return: 0 on success, errno on failure
1026 */
1027static int
1028rpmh_regulator_load_arc_level_mapping(struct rpmh_aggr_vreg *aggr_vreg)
1029{
1030 int i, j, len, rc;
1031 u8 *buf;
1032
1033 len = cmd_db_get_aux_data_len(aggr_vreg->resource_name);
1034 if (len < 0) {
1035 aggr_vreg_err(aggr_vreg, "could not get ARC aux data len, rc=%d\n",
1036 len);
1037 return len;
1038 } else if (len == 0) {
1039 aggr_vreg_err(aggr_vreg, "ARC level mapping data missing in command db\n");
1040 return -EINVAL;
1041 } else if (len > RPMH_ARC_MAX_LEVELS * RPMH_ARC_LEVEL_SIZE) {
1042 aggr_vreg_err(aggr_vreg, "more ARC levels defined than allowed: %d > %d\n",
1043 len, RPMH_ARC_MAX_LEVELS * RPMH_ARC_LEVEL_SIZE);
1044 return -EINVAL;
1045 } else if (len % RPMH_ARC_LEVEL_SIZE) {
1046 aggr_vreg_err(aggr_vreg, "invalid ARC aux data size: %d\n",
1047 len);
1048 return -EINVAL;
1049 }
1050
1051 buf = kzalloc(len, GFP_KERNEL);
1052 if (!buf)
1053 return -ENOMEM;
1054
1055 rc = cmd_db_get_aux_data(aggr_vreg->resource_name, buf, len);
1056 if (rc < 0) {
1057 aggr_vreg_err(aggr_vreg, "could not retrieve ARC aux data, rc=%d\n",
1058 rc);
1059 goto done;
1060 } else if (rc != len) {
1061 aggr_vreg_err(aggr_vreg, "could not retrieve all ARC aux data, %d != %d\n",
1062 rc, len);
1063 rc = -EINVAL;
1064 goto done;
1065 }
1066 rc = 0;
1067
1068 aggr_vreg->level_count = len / RPMH_ARC_LEVEL_SIZE;
1069
1070 for (i = 0; i < aggr_vreg->level_count; i++) {
1071 for (j = 0; j < RPMH_ARC_LEVEL_SIZE; j++)
1072 aggr_vreg->level[i] |=
1073 buf[i * RPMH_ARC_LEVEL_SIZE + j] << (8 * j);
1074
1075 /*
1076 * The AUX data may be zero padded. These 0 valued entries at
1077 * the end of the map must be ignored.
1078 */
1079 if (i > 0 && aggr_vreg->level[i] == 0) {
1080 aggr_vreg->level_count = i;
1081 break;
1082 }
1083
1084 /* Add consumer offset to avoid voltage = 0. */
1085 aggr_vreg->level[i] += RPMH_REGULATOR_LEVEL_OFFSET;
1086 aggr_vreg_debug(aggr_vreg, "ARC hlvl=%2d --> vlvl=%4u\n",
1087 i, aggr_vreg->level[i]);
1088 }
1089
1090done:
1091 kfree(buf);
1092 return rc;
1093}
1094
1095/**
1096 * rpmh_regulator_parse_vrm_modes() - parse the supported mode configurations
1097 * for a VRM RPMh resource from device tree
1098 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1099 *
1100 * This function initializes the mode[] array of aggr_vreg based upon the values
1101 * of optional device tree properties.
1102 *
1103 * Return: 0 on success, errno on failure
1104 */
1105static int rpmh_regulator_parse_vrm_modes(struct rpmh_aggr_vreg *aggr_vreg)
1106{
1107 struct device_node *node = aggr_vreg->dev->of_node;
1108 const char *prop = "qcom,supported-modes";
1109 int i, len, rc;
1110 u32 *buf;
1111
1112 /* qcom,supported-modes is optional */
1113 if (!of_find_property(node, prop, &len))
1114 return 0;
1115
1116 len /= sizeof(u32);
1117 aggr_vreg->mode = devm_kcalloc(aggr_vreg->dev, len,
1118 sizeof(*aggr_vreg->mode), GFP_KERNEL);
1119 if (!aggr_vreg->mode)
1120 return -ENOMEM;
1121 aggr_vreg->mode_count = len;
1122
1123
1124 buf = kcalloc(len, sizeof(*buf), GFP_KERNEL);
1125 if (!buf)
1126 return -ENOMEM;
1127
1128 rc = of_property_read_u32_array(node, prop, buf, len);
1129 if (rc) {
1130 aggr_vreg_err(aggr_vreg, "unable to read %s, rc=%d\n",
1131 prop, rc);
1132 goto done;
1133 }
1134
1135 for (i = 0; i < len; i++) {
1136 if (buf[i] >= ARRAY_SIZE(rpmh_regulator_mode_map)) {
1137 aggr_vreg_err(aggr_vreg, "element %d of %s = %u is invalid\n",
1138 i, prop, buf[i]);
1139 rc = -EINVAL;
1140 goto done;
1141 }
1142 aggr_vreg->mode[i].pmic_mode = buf[i];
1143 aggr_vreg->mode[i].framework_mode
1144 = rpmh_regulator_mode_map[buf[i]];
1145
1146 if (i > 0 && aggr_vreg->mode[i].pmic_mode
1147 <= aggr_vreg->mode[i - 1].pmic_mode) {
1148 aggr_vreg_err(aggr_vreg, "%s elements are not in ascending order\n",
1149 prop);
1150 rc = -EINVAL;
1151 goto done;
1152 }
1153 }
1154
1155 prop = "qcom,mode-threshold-currents";
1156
1157 rc = of_property_read_u32_array(node, prop, buf, len);
1158 if (rc) {
1159 aggr_vreg_err(aggr_vreg, "unable to read %s, rc=%d\n",
1160 prop, rc);
1161 goto done;
1162 }
1163
1164 for (i = 0; i < len; i++) {
1165 aggr_vreg->mode[i].min_load_ua = buf[i];
1166
1167 if (i > 0 && aggr_vreg->mode[i].min_load_ua
1168 <= aggr_vreg->mode[i - 1].min_load_ua) {
1169 aggr_vreg_err(aggr_vreg, "%s elements are not in ascending order\n",
1170 prop);
1171 rc = -EINVAL;
1172 goto done;
1173 }
1174 }
1175
1176done:
1177 kfree(buf);
1178 return rc;
1179}
1180
1181/**
1182 * rpmh_regulator_allocate_vreg() - allocate space for the regulators associated
1183 * with the RPMh regulator resource and initialize important
1184 * pointers for each regulator
1185 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1186 *
1187 * Return: 0 on success, errno on failure
1188 */
1189static int rpmh_regulator_allocate_vreg(struct rpmh_aggr_vreg *aggr_vreg)
1190{
1191 struct device_node *node;
1192 int i, rc;
1193
1194 aggr_vreg->vreg_count = 0;
1195
1196 for_each_available_child_of_node(aggr_vreg->dev->of_node, node) {
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001197 /* Skip child nodes handled by other drivers. */
1198 if (of_find_property(node, "compatible", NULL))
1199 continue;
David Collinse1b43192016-10-12 17:27:22 -07001200 aggr_vreg->vreg_count++;
1201 }
1202
1203 if (aggr_vreg->vreg_count == 0) {
1204 aggr_vreg_err(aggr_vreg, "could not find any regulator subnodes\n");
1205 return -ENODEV;
1206 }
1207
1208 aggr_vreg->vreg = devm_kcalloc(aggr_vreg->dev, aggr_vreg->vreg_count,
1209 sizeof(*aggr_vreg->vreg), GFP_KERNEL);
1210 if (!aggr_vreg->vreg)
1211 return -ENOMEM;
1212
1213 i = 0;
1214 for_each_available_child_of_node(aggr_vreg->dev->of_node, node) {
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001215 /* Skip child nodes handled by other drivers. */
1216 if (of_find_property(node, "compatible", NULL))
1217 continue;
1218
David Collinse1b43192016-10-12 17:27:22 -07001219 aggr_vreg->vreg[i].of_node = node;
1220 aggr_vreg->vreg[i].aggr_vreg = aggr_vreg;
1221
1222 rc = of_property_read_string(node, "regulator-name",
1223 &aggr_vreg->vreg[i].rdesc.name);
1224 if (rc) {
1225 aggr_vreg_err(aggr_vreg, "could not read regulator-name property, rc=%d\n",
1226 rc);
1227 return rc;
1228 }
1229
1230 i++;
1231 }
1232
1233 return 0;
1234}
1235
1236/**
1237 * rpmh_regulator_load_default_parameters() - initialize the RPMh resource
1238 * request for this regulator based on optional device tree
1239 * properties
1240 * @vreg: Pointer to the RPMh regulator
1241 *
1242 * Return: 0 on success, errno on failure
1243 */
1244static int rpmh_regulator_load_default_parameters(struct rpmh_vreg *vreg)
1245{
1246 enum rpmh_regulator_type type = vreg->aggr_vreg->regulator_type;
1247 const char *prop;
1248 int i, rc;
1249 u32 temp;
1250
1251 if (type == RPMH_REGULATOR_TYPE_ARC) {
1252 prop = "qcom,init-voltage-level";
1253 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1254 if (!rc) {
1255 for (i = 0; i < vreg->aggr_vreg->level_count; i++)
1256 if (temp <= vreg->aggr_vreg->level[i])
1257 break;
1258 if (i < vreg->aggr_vreg->level_count) {
1259 rpmh_regulator_set_reg(vreg,
1260 RPMH_REGULATOR_REG_ARC_LEVEL, i);
1261 } else {
1262 vreg_err(vreg, "%s=%u is invalid\n",
1263 prop, temp);
1264 return -EINVAL;
1265 }
1266 }
David Collinsd86a1722017-02-07 15:42:22 -08001267
1268 prop = "qcom,min-dropout-voltage-level";
1269 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1270 if (!rc)
1271 vreg->rdesc.min_dropout_uV = temp;
David Collinse1b43192016-10-12 17:27:22 -07001272 } else if (type == RPMH_REGULATOR_TYPE_VRM) {
1273 prop = "qcom,init-enable";
1274 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1275 if (!rc)
1276 rpmh_regulator_set_reg(vreg,
1277 RPMH_REGULATOR_REG_VRM_ENABLE,
1278 !!temp);
1279
1280 prop = "qcom,init-voltage";
1281 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1282 if (!rc) {
1283 if (temp < RPMH_VRM_MIN_UV || temp > RPMH_VRM_MAX_UV) {
1284 vreg_err(vreg, "%s=%u is invalid\n",
1285 prop, temp);
1286 return -EINVAL;
1287 }
1288 rpmh_regulator_set_reg(vreg,
1289 RPMH_REGULATOR_REG_VRM_VOLTAGE,
1290 temp / 1000);
1291 }
1292
1293 prop = "qcom,init-mode";
1294 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1295 if (!rc) {
1296 if (temp < RPMH_VRM_MODE_MIN ||
1297 temp > RPMH_VRM_MODE_MAX) {
1298 vreg_err(vreg, "%s=%u is invalid\n",
1299 prop, temp);
1300 return -EINVAL;
1301 }
1302 rpmh_regulator_set_reg(vreg,
1303 RPMH_REGULATOR_REG_VRM_MODE,
1304 temp);
1305 }
1306
1307 prop = "qcom,init-headroom-voltage";
1308 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1309 if (!rc) {
1310 if (temp < RPMH_VRM_HEADROOM_MIN_UV ||
1311 temp > RPMH_VRM_HEADROOM_MAX_UV) {
1312 vreg_err(vreg, "%s=%u is invalid\n",
1313 prop, temp);
1314 return -EINVAL;
1315 }
1316 rpmh_regulator_set_reg(vreg,
1317 RPMH_REGULATOR_REG_VRM_HEADROOM,
1318 temp / 1000);
1319 }
David Collinsd86a1722017-02-07 15:42:22 -08001320
1321 prop = "qcom,min-dropout-voltage";
1322 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1323 if (!rc)
1324 vreg->rdesc.min_dropout_uV = temp;
David Collinse1b43192016-10-12 17:27:22 -07001325 }
1326
1327 return 0;
1328}
1329
1330/**
1331 * rpmh_regulator_init_vreg_supply() - initialize the regulator's parent supply
1332 * mapping based on optional DT parent supply property
1333 * @vreg: Pointer to the RPMh regulator
1334 *
1335 * Return: 0 on success, errno on failure
1336 */
1337static int rpmh_regulator_init_vreg_supply(struct rpmh_vreg *vreg)
1338{
1339 char *buf;
1340 size_t len;
1341
1342 len = strlen(vreg->rdesc.name) + 16;
1343 buf = kzalloc(len, GFP_KERNEL);
1344 if (!buf)
1345 return -ENOMEM;
1346 scnprintf(buf, len, "%s-parent-supply", vreg->rdesc.name);
1347
1348 if (of_find_property(vreg->aggr_vreg->dev->of_node, buf, NULL)) {
1349 kfree(buf);
1350
1351 len = strlen(vreg->rdesc.name) + 10;
1352 buf = devm_kzalloc(vreg->aggr_vreg->dev, len, GFP_KERNEL);
1353 if (!buf)
1354 return -ENOMEM;
1355 scnprintf(buf, len, "%s-parent", vreg->rdesc.name);
1356
1357 vreg->rdesc.supply_name = buf;
1358 } else {
1359 kfree(buf);
1360 }
1361
1362 return 0;
1363}
1364
1365/**
1366 * rpmh_regulator_init_vreg() - initialize all abbributes of an rpmh-regulator
1367 * @vreg: Pointer to the RPMh regulator
1368 *
1369 * Return: 0 on success, errno on failure
1370 */
1371static int rpmh_regulator_init_vreg(struct rpmh_vreg *vreg)
1372{
1373 struct device *dev = vreg->aggr_vreg->dev;
1374 enum rpmh_regulator_type type = vreg->aggr_vreg->regulator_type;
1375 struct regulator_config reg_config = {};
1376 struct regulator_init_data *init_data;
1377 struct regulator_ops *ops;
1378 int rc, i;
1379 u32 set;
1380
1381 ops = devm_kzalloc(dev, sizeof(*ops), GFP_KERNEL);
1382 if (!ops)
1383 return -ENOMEM;
1384
1385 *ops = *rpmh_regulator_ops[type];
1386 vreg->rdesc.owner = THIS_MODULE;
1387 vreg->rdesc.type = REGULATOR_VOLTAGE;
1388 vreg->rdesc.ops = ops;
1389
1390 init_data = of_get_regulator_init_data(dev,
1391 vreg->of_node, &vreg->rdesc);
1392 if (init_data == NULL)
1393 return -ENOMEM;
1394
1395 init_data->constraints.input_uV = init_data->constraints.max_uV;
1396 if (type == RPMH_REGULATOR_TYPE_VRM) {
1397 init_data->constraints.min_uV
1398 = max(init_data->constraints.min_uV, RPMH_VRM_MIN_UV);
1399 init_data->constraints.min_uV
1400 = min(init_data->constraints.min_uV, RPMH_VRM_MAX_UV);
1401 init_data->constraints.max_uV
1402 = max(init_data->constraints.max_uV, RPMH_VRM_MIN_UV);
1403 init_data->constraints.max_uV
1404 = min(init_data->constraints.max_uV, RPMH_VRM_MAX_UV);
1405 }
1406
1407 if (ops->set_voltage || ops->set_voltage_sel)
1408 init_data->constraints.valid_ops_mask
1409 |= REGULATOR_CHANGE_VOLTAGE;
1410
1411 if (vreg->aggr_vreg->mode_count) {
1412 init_data->constraints.valid_ops_mask
1413 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1414 for (i = 0; i < vreg->aggr_vreg->mode_count; i++)
1415 init_data->constraints.valid_modes_mask
1416 |= vreg->aggr_vreg->mode[i].framework_mode;
1417 } else {
1418 ops->get_mode = NULL;
1419 ops->set_mode = NULL;
1420 ops->set_load = NULL;
1421 }
1422
1423 /*
1424 * Remove enable state control if the ARC resource does not support the
1425 * off level.
1426 */
1427 if (type == RPMH_REGULATOR_TYPE_ARC
1428 && vreg->aggr_vreg->level[0] != RPMH_REGULATOR_LEVEL_OFF) {
1429 ops->enable = NULL;
1430 ops->disable = NULL;
1431 ops->is_enabled = NULL;
1432 }
1433 if (ops->enable)
1434 init_data->constraints.valid_ops_mask
1435 |= REGULATOR_CHANGE_STATUS;
1436
1437 vreg->rdesc.n_voltages = type == RPMH_REGULATOR_TYPE_ARC ?
1438 vreg->aggr_vreg->level_count : 2;
1439
1440 rc = of_property_read_u32(vreg->of_node, "qcom,set", &set);
1441 if (rc) {
1442 vreg_err(vreg, "qcom,set property missing, rc=%d\n", rc);
1443 return rc;
1444 } else if (!(set & RPMH_REGULATOR_SET_ALL)) {
1445 vreg_err(vreg, "qcom,set=%u property is invalid\n", set);
1446 return rc;
1447 }
1448
1449 vreg->set_active = !!(set & RPMH_REGULATOR_SET_ACTIVE);
1450 vreg->set_sleep = !!(set & RPMH_REGULATOR_SET_SLEEP);
1451
1452 rc = rpmh_regulator_init_vreg_supply(vreg);
1453 if (rc) {
1454 vreg_err(vreg, "unable to initialize regulator supply name, rc=%d\n",
1455 rc);
1456 return rc;
1457 }
1458
1459 reg_config.dev = dev;
1460 reg_config.init_data = init_data;
1461 reg_config.of_node = vreg->of_node;
1462 reg_config.driver_data = vreg;
1463
1464 rc = rpmh_regulator_load_default_parameters(vreg);
1465 if (rc) {
1466 vreg_err(vreg, "unable to load default parameters, rc=%d\n",
1467 rc);
1468 return rc;
1469 }
1470
1471 vreg->rdev = devm_regulator_register(dev, &vreg->rdesc, &reg_config);
1472 if (IS_ERR(vreg->rdev)) {
1473 rc = PTR_ERR(vreg->rdev);
1474 vreg->rdev = NULL;
1475 vreg_err(vreg, "devm_regulator_register() failed, rc=%d\n", rc);
1476 return rc;
1477 }
1478
1479 vreg_debug(vreg, "successfully registered; set=%s\n",
1480 vreg->set_active && vreg->set_sleep
1481 ? "active + sleep"
1482 : vreg->set_active ? "active" : "sleep");
1483
1484 return rc;
1485}
1486
1487static const struct of_device_id rpmh_regulator_match_table[] = {
1488 {
1489 .compatible = "qcom,rpmh-vrm-regulator",
1490 .data = (void *)(uintptr_t)RPMH_REGULATOR_TYPE_VRM,
1491 },
1492 {
1493 .compatible = "qcom,rpmh-arc-regulator",
1494 .data = (void *)(uintptr_t)RPMH_REGULATOR_TYPE_ARC,
1495 },
1496 {}
1497};
1498
1499/**
1500 * rpmh_regulator_probe() - probe an aggregated RPMh regulator resource and
1501 * register regulators for each of the regulator nodes associated
1502 * with it
1503 * @pdev: Pointer to the platform device of the aggregated rpmh
1504 * regulator resource
1505 *
1506 * Return: 0 on success, errno on failure
1507 */
1508static int rpmh_regulator_probe(struct platform_device *pdev)
1509{
1510 struct device *dev = &pdev->dev;
1511 const struct of_device_id *match;
1512 struct rpmh_aggr_vreg *aggr_vreg;
1513 struct device_node *node;
1514 int rc, i, sid;
1515
1516 node = dev->of_node;
1517
1518 if (!node) {
1519 dev_err(dev, "Device tree node is missing\n");
1520 return -EINVAL;
1521 }
1522
1523 aggr_vreg = devm_kzalloc(dev, sizeof(*aggr_vreg), GFP_KERNEL);
1524 if (!aggr_vreg)
1525 return -ENOMEM;
1526
1527 aggr_vreg->dev = dev;
1528 mutex_init(&aggr_vreg->lock);
1529
1530 match = of_match_node(rpmh_regulator_match_table, node);
1531 if (match) {
1532 aggr_vreg->regulator_type = (uintptr_t)match->data;
1533 } else {
1534 dev_err(dev, "could not find compatible string match\n");
1535 return -ENODEV;
1536 }
1537
1538 rc = of_property_read_string(node, "qcom,resource-name",
1539 &aggr_vreg->resource_name);
1540 if (rc) {
1541 dev_err(dev, "qcom,resource-name missing in DT node\n");
1542 return rc;
1543 }
1544
1545 aggr_vreg->use_awake_state = of_property_read_bool(node,
1546 "qcom,use-awake-state");
1547
1548 rc = cmd_db_ready();
1549 if (rc) {
1550 if (rc != -EPROBE_DEFER)
1551 aggr_vreg_err(aggr_vreg, "Command DB not available, rc=%d\n",
1552 rc);
1553 return rc;
1554 }
1555
1556 aggr_vreg->addr = cmd_db_get_addr(aggr_vreg->resource_name);
1557 if (!aggr_vreg->addr) {
1558 aggr_vreg_err(aggr_vreg, "could not find RPMh address for resource\n");
1559 return -ENODEV;
1560 }
1561
1562 sid = cmd_db_get_slave_id(aggr_vreg->resource_name);
1563 if (sid < 0) {
1564 aggr_vreg_err(aggr_vreg, "could not find RPMh slave id for resource, rc=%d\n",
1565 sid);
1566 return sid;
1567 }
1568
1569 /* Confirm slave ID listed in command DB matches DT configuration. */
1570 if ((aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
1571 && sid != CMD_DB_HW_ARC)
1572 || (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM
1573 && sid != CMD_DB_HW_VRM)) {
1574 aggr_vreg_err(aggr_vreg, "RPMh slave ID mismatch; config=%d (%s) != cmd-db=%d\n",
1575 aggr_vreg->regulator_type,
1576 aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
1577 ? "ARC" : "VRM",
1578 sid);
1579 return -EINVAL;
1580 }
1581
1582 aggr_vreg->rpmh_client = rpmh_get_byindex(pdev, 0);
1583 if (IS_ERR(aggr_vreg->rpmh_client)) {
1584 rc = PTR_ERR(aggr_vreg->rpmh_client);
1585 if (rc != -EPROBE_DEFER)
1586 aggr_vreg_err(aggr_vreg, "failed to request RPMh client, rc=%d\n",
1587 rc);
1588 return rc;
1589 }
1590
1591 if (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC) {
1592 rc = rpmh_regulator_load_arc_level_mapping(aggr_vreg);
1593 if (rc) {
1594 aggr_vreg_err(aggr_vreg, "could not load arc level mapping, rc=%d\n",
1595 rc);
1596 goto free_client;
1597 }
1598 } else if (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM) {
1599 rc = rpmh_regulator_parse_vrm_modes(aggr_vreg);
1600 if (rc) {
1601 aggr_vreg_err(aggr_vreg, "could not parse vrm mode mapping, rc=%d\n",
1602 rc);
1603 goto free_client;
1604 }
1605 }
1606
1607 aggr_vreg->always_wait_for_ack
1608 = of_property_read_bool(node, "qcom,always-wait-for-ack");
1609
1610 rc = rpmh_regulator_allocate_vreg(aggr_vreg);
1611 if (rc) {
1612 aggr_vreg_err(aggr_vreg, "failed to allocate regulator subnode array, rc=%d\n",
1613 rc);
1614 goto free_client;
1615 }
1616
1617 for (i = 0; i < aggr_vreg->vreg_count; i++) {
1618 rc = rpmh_regulator_init_vreg(&aggr_vreg->vreg[i]);
1619 if (rc) {
1620 pr_err("unable to initialize rpmh-regulator vreg %s for resource %s, rc=%d\n",
1621 aggr_vreg->vreg[i].rdesc.name,
1622 aggr_vreg->resource_name, rc);
1623 goto free_client;
1624 }
1625 }
1626
1627 if (of_property_read_bool(node, "qcom,send-defaults")) {
1628 mutex_lock(&aggr_vreg->lock);
1629 rc = rpmh_regulator_send_aggregate_requests(
1630 &aggr_vreg->vreg[0]);
1631 if (rc) {
1632 aggr_vreg_err(aggr_vreg, "error while sending default request, rc=%d\n",
1633 rc);
1634 mutex_unlock(&aggr_vreg->lock);
1635 goto free_client;
1636 }
1637 mutex_unlock(&aggr_vreg->lock);
1638 }
1639
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001640 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
David Collinse1b43192016-10-12 17:27:22 -07001641 platform_set_drvdata(pdev, aggr_vreg);
1642
1643 aggr_vreg_debug(aggr_vreg, "successfully probed; addr=0x%05X, type=%s\n",
1644 aggr_vreg->addr,
1645 aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
1646 ? "ARC" : "VRM");
1647
1648 return rc;
1649
1650free_client:
1651 rpmh_release(aggr_vreg->rpmh_client);
1652
1653 return rc;
1654}
1655
1656static int rpmh_regulator_remove(struct platform_device *pdev)
1657{
1658 struct rpmh_aggr_vreg *aggr_vreg = platform_get_drvdata(pdev);
1659
1660 rpmh_release(aggr_vreg->rpmh_client);
1661
1662 return 0;
1663}
1664
1665static struct platform_driver rpmh_regulator_driver = {
1666 .driver = {
1667 .name = "qcom,rpmh-regulator",
1668 .of_match_table = rpmh_regulator_match_table,
1669 .owner = THIS_MODULE,
1670 },
1671 .probe = rpmh_regulator_probe,
1672 .remove = rpmh_regulator_remove,
1673};
1674
1675static int rpmh_regulator_init(void)
1676{
1677 return platform_driver_register(&rpmh_regulator_driver);
1678}
1679
1680static void rpmh_regulator_exit(void)
1681{
1682 platform_driver_unregister(&rpmh_regulator_driver);
1683}
1684
1685MODULE_DESCRIPTION("RPMh regulator driver");
1686MODULE_LICENSE("GPL v2");
1687
1688arch_initcall(rpmh_regulator_init);
1689module_exit(rpmh_regulator_exit);