blob: 1ba892698be49cf2f87e814f6ccd58af0369e4a4 [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;
436 int i, j, max_reg_index, rc;
437 enum rpmh_state state;
438 u32 sent_mask;
439
440 max_reg_index = aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM
441 ? RPMH_REGULATOR_REG_VRM_MAX
442 : RPMH_REGULATOR_REG_ARC_MAX;
443 /*
444 * Perform max aggregration of each register value across all regulators
445 * which use this RPMh resource.
446 */
447 for (i = 0; i < aggr_vreg->vreg_count; i++) {
448 if (aggr_vreg->vreg[i].set_active) {
449 for (j = 0; j < max_reg_index; j++)
450 req_active.reg[j] = max(req_active.reg[j],
451 aggr_vreg->vreg[i].req.reg[j]);
452 req_active.valid |= aggr_vreg->vreg[i].req.valid;
453 }
454 if (aggr_vreg->vreg[i].set_sleep) {
455 for (j = 0; j < max_reg_index; j++)
456 req_sleep.reg[j] = max(req_sleep.reg[j],
457 aggr_vreg->vreg[i].req.reg[j]);
458 req_sleep.valid |= aggr_vreg->vreg[i].req.valid;
459 }
460 }
461
462 rpmh_regulator_handle_arc_enable(aggr_vreg, &req_active);
463 rpmh_regulator_handle_arc_enable(aggr_vreg, &req_sleep);
464
465 /*
466 * Check if the aggregated sleep set parameter values differ from the
467 * aggregated active set parameter values.
468 */
469 if (!aggr_vreg->sleep_request_sent) {
470 for (i = 0; i < max_reg_index; i++) {
471 if ((req_active.reg[i] != req_sleep.reg[i])
472 && (req_sleep.valid & BIT(i))) {
473 sleep_set_differs = true;
474 break;
475 }
476 }
477 }
478
479 if (sleep_set_differs) {
480 /*
481 * Generate an rpmh command consisting of only those registers
482 * which have new values or which have never been touched before
483 * (i.e. those that were previously not valid).
484 */
485 sent_mask = 0;
486 for (i = 0, j = 0; i < max_reg_index; i++) {
487 if ((req_sleep.valid & BIT(i))
488 && (!(aggr_vreg->aggr_req_sleep.valid & BIT(i))
489 || aggr_vreg->aggr_req_sleep.reg[i]
490 != req_sleep.reg[i])) {
491 cmd[j].addr = aggr_vreg->addr + i * 4;
492 cmd[j].data = req_sleep.reg[i];
493 j++;
494 sent_mask |= BIT(i);
495 }
496 }
497
498 /* Send the rpmh command if any register values differ. */
499 if (j > 0) {
500 rc = rpmh_write_async(aggr_vreg->rpmh_client,
501 RPMH_SLEEP_STATE, cmd, j);
502 if (rc) {
503 aggr_vreg_err(aggr_vreg, "sleep state rpmh_write_async() failed, rc=%d\n",
504 rc);
505 return rc;
506 }
507 rpmh_regulator_req(vreg, &req_sleep,
508 &aggr_vreg->aggr_req_sleep,
509 sent_mask,
510 RPMH_SLEEP_STATE);
511 aggr_vreg->sleep_request_sent = true;
512 aggr_vreg->aggr_req_sleep = req_sleep;
513 }
514 }
515
516 /*
517 * Generate an rpmh command consisting of only those registers
518 * which have new values or which have never been touched before
519 * (i.e. those that were previously not valid).
520 */
521 sent_mask = 0;
522 for (i = 0, j = 0; i < max_reg_index; i++) {
523 if ((req_active.valid & BIT(i))
524 && (!(aggr_vreg->aggr_req_active.valid & BIT(i))
525 || aggr_vreg->aggr_req_active.reg[i]
526 != req_active.reg[i])) {
527 cmd[j].addr = aggr_vreg->addr + i * 4;
528 cmd[j].data = req_active.reg[i];
529 j++;
530 sent_mask |= BIT(i);
531
532 /*
533 * Must wait for ACK from RPMh if power state is
534 * increasing
535 */
536 if (req_active.reg[i]
537 > aggr_vreg->aggr_req_active.reg[i])
538 wait_for_ack = true;
539 }
540 }
541
542 /* Send the rpmh command if any register values differ. */
543 if (j > 0) {
544 if (sleep_set_differs && !aggr_vreg->use_awake_state) {
545 state = RPMH_WAKE_ONLY_STATE;
546 rc = rpmh_write_async(aggr_vreg->rpmh_client, state,
547 cmd, j);
548 if (rc) {
549 aggr_vreg_err(aggr_vreg, "%s state rpmh_write_async() failed, rc=%d\n",
550 rpmh_regulator_state_names[state], rc);
551 return rc;
552 }
553 rpmh_regulator_req(vreg, &req_active,
554 &aggr_vreg->aggr_req_active, sent_mask, state);
555 }
556
557 state = aggr_vreg->use_awake_state ? RPMH_AWAKE_STATE
558 : RPMH_ACTIVE_ONLY_STATE;
559 if (wait_for_ack)
560 rc = rpmh_write(aggr_vreg->rpmh_client, state, cmd, j);
561 else
562 rc = rpmh_write_async(aggr_vreg->rpmh_client, state,
563 cmd, j);
564 if (rc) {
565 aggr_vreg_err(aggr_vreg, "%s state rpmh_write() failed, rc=%d\n",
566 rpmh_regulator_state_names[state], rc);
567 return rc;
568 }
569 rpmh_regulator_req(vreg, &req_active,
570 &aggr_vreg->aggr_req_active, sent_mask, state);
571
572 aggr_vreg->aggr_req_active = req_active;
573 aggr_vreg->next_wait_for_ack = false;
574 }
575
576 return 0;
577}
578
579/**
580 * rpmh_regulator_set_reg() - set a register value within the request for an
581 * RPMh regulator and return the previous value
582 * @vreg: Pointer to the RPMh regulator
583 * @reg_index: Index of the register value to update
584 * @value: New register value to set
585 *
586 * Return: old register value
587 */
588static u32 rpmh_regulator_set_reg(struct rpmh_vreg *vreg, int reg_index,
589 u32 value)
590{
591 u32 old_value;
592
593 old_value = vreg->req.reg[reg_index];
594 vreg->req.reg[reg_index] = value;
595 vreg->req.valid |= BIT(reg_index);
596
597 return old_value;
598}
599
600/**
601 * rpmh_regulator_check_param_max() - sets if the next request must wait for
602 * an ACK based on the previously sent reg[index] value and the new
603 * max value
604 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
605 * @index: Register index
606 * @new_max: Newly requested maximum allowed value for the parameter
607 *
608 * This function is used to handle the case when a consumer makes a new
609 * (min_uv, max_uv) range request in which the new max_uv is lower than the
610 * previously requested min_uv. In this case, the driver must wait for an ACK
611 * from RPMh to ensure that the voltage has completed reducing to the new min_uv
612 * value since the consumer cannot operate at the old min_uv value.
613 *
614 * Return: none
615 */
616static void rpmh_regulator_check_param_max(struct rpmh_aggr_vreg *aggr_vreg,
617 int index, u32 new_max)
618{
619 if ((aggr_vreg->aggr_req_active.valid & BIT(index))
620 && aggr_vreg->aggr_req_active.reg[index] > new_max)
621 aggr_vreg->next_wait_for_ack = true;
622}
623
624/**
625 * rpmh_regulator_is_enabled() - return the enable state of the RPMh
626 * regulator
627 * @rdev: Regulator device pointer for the rpmh-regulator
628 *
629 * This function is passed as a callback function into the regulator ops that
630 * are registered for each rpmh-regulator device.
631 *
632 * Note that for ARC resources, this value is effectively a flag indicating if
633 * the requested voltage level is masked or unmasked since "disabled" = voltage
634 * level 0 (if supported).
635 *
636 * Return: true if regulator is enabled, false if regulator is disabled
637 */
638static int rpmh_regulator_is_enabled(struct regulator_dev *rdev)
639{
640 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
641
642 return !!vreg->req.reg[RPMH_REGULATOR_REG_ENABLE];
643}
644
645/**
646 * rpmh_regulator_enable() - enable the RPMh regulator
647 * @rdev: Regulator device pointer for the rpmh-regulator
648 *
649 * This function is passed as a callback function into the regulator ops that
650 * are registered for each rpmh-regulator device.
651 *
652 * Note that for ARC devices the enable state is handled via the voltage level
653 * parameter. Therefore, this enable value effectively masks or unmasks the
654 * enabled voltage level.
655 *
656 * Return: 0 on success, errno on failure
657 */
658static int rpmh_regulator_enable(struct regulator_dev *rdev)
659{
660 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
661 u32 prev_enable;
662 int rc;
663
664 mutex_lock(&vreg->aggr_vreg->lock);
665
666 prev_enable
667 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE, 1);
668
669 rc = rpmh_regulator_send_aggregate_requests(vreg);
670 if (rc) {
671 vreg_err(vreg, "enable failed, rc=%d\n", rc);
672 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE,
673 prev_enable);
674 }
675
676 mutex_unlock(&vreg->aggr_vreg->lock);
677
678 return rc;
679}
680
681/**
682 * rpmh_regulator_disable() - disable the RPMh regulator
683 * @rdev: Regulator device pointer for the rpmh-regulator
684 *
685 * This function is passed as a callback function into the regulator ops that
686 * are registered for each rpmh-regulator device.
687 *
688 * Note that for ARC devices the enable state is handled via the voltage level
689 * parameter. Therefore, this enable value effectively masks or unmasks the
690 * enabled voltage level.
691 *
692 * Return: 0 on success, errno on failure
693 */
694static int rpmh_regulator_disable(struct regulator_dev *rdev)
695{
696 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
697 u32 prev_enable;
698 int rc;
699
700 mutex_lock(&vreg->aggr_vreg->lock);
701
702 prev_enable
703 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE, 0);
704
705 rc = rpmh_regulator_send_aggregate_requests(vreg);
706 if (rc) {
707 vreg_err(vreg, "disable failed, rc=%d\n", rc);
708 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE,
709 prev_enable);
710 }
711
712 mutex_unlock(&vreg->aggr_vreg->lock);
713
714 return rc;
715}
716
717/**
718 * rpmh_regulator_vrm_set_voltage() - set the voltage of the VRM rpmh-regulator
719 * @rdev: Regulator device pointer for the rpmh-regulator
720 * @min_uv: New voltage in microvolts to set
721 * @max_uv: Maximum voltage in microvolts allowed
722 * @selector: Unused
723 *
724 * This function is passed as a callback function into the regulator ops that
725 * are registered for each VRM rpmh-regulator device.
726 *
727 * Return: 0 on success, errno on failure
728 */
729static int rpmh_regulator_vrm_set_voltage(struct regulator_dev *rdev,
730 int min_uv, int max_uv, unsigned int *selector)
731{
732 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
733 u32 prev_voltage;
734 int mv;
735 int rc = 0;
736
737 mv = DIV_ROUND_UP(min_uv, 1000);
738 if (mv * 1000 > max_uv) {
739 vreg_err(vreg, "no set points available in range %d-%d uV\n",
740 min_uv, max_uv);
741 return -EINVAL;
742 }
743
744 mutex_lock(&vreg->aggr_vreg->lock);
745
746 prev_voltage
747 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_VOLTAGE, mv);
748 rpmh_regulator_check_param_max(vreg->aggr_vreg,
749 RPMH_REGULATOR_REG_VRM_VOLTAGE, max_uv);
750
751 rc = rpmh_regulator_send_aggregate_requests(vreg);
752 if (rc) {
753 vreg_err(vreg, "set voltage=%d mV failed, rc=%d\n", mv, rc);
754 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_VOLTAGE,
755 prev_voltage);
756 }
757
758 mutex_unlock(&vreg->aggr_vreg->lock);
759
760 return rc;
761}
762
763/**
764 * rpmh_regulator_vrm_get_voltage() - get the voltage of the VRM rpmh-regulator
765 * @rdev: Regulator device pointer for the rpmh-regulator
766 *
767 * This function is passed as a callback function into the regulator ops that
768 * are registered for each VRM rpmh-regulator device.
769 *
770 * Return: regulator voltage in microvolts
771 */
772static int rpmh_regulator_vrm_get_voltage(struct regulator_dev *rdev)
773{
774 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
775 int uv;
776
777 uv = vreg->req.reg[RPMH_REGULATOR_REG_VRM_VOLTAGE] * 1000;
778 if (uv == 0)
779 uv = VOLTAGE_UNKNOWN;
780
781 return uv;
782}
783
784/**
785 * rpmh_regulator_vrm_set_mode_index() - set the mode of a VRM regulator to the
786 * mode mapped to mode_index
787 * @vreg: Pointer to the RPMh regulator
788 * @mode_index: Index into aggr_vreg->mode[] array
789 *
790 * Return: 0 on success, errno on failure
791 */
792static int rpmh_regulator_vrm_set_mode_index(struct rpmh_vreg *vreg,
793 int mode_index)
794{
795 u32 prev_mode;
796 int rc;
797
798 mutex_lock(&vreg->aggr_vreg->lock);
799
800 prev_mode = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_MODE,
801 vreg->aggr_vreg->mode[mode_index].pmic_mode);
802
803 rc = rpmh_regulator_send_aggregate_requests(vreg);
804 if (rc) {
805 vreg_err(vreg, "set mode=%u failed, rc=%d\n",
806 vreg->req.reg[RPMH_REGULATOR_REG_VRM_MODE],
807 rc);
808 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_MODE,
809 prev_mode);
810 } else {
811 vreg->mode_index = mode_index;
812 }
813
814 mutex_unlock(&vreg->aggr_vreg->lock);
815
816 return rc;
817}
818
819/**
820 * rpmh_regulator_vrm_set_mode() - set the mode of the VRM rpmh-regulator
821 * @rdev: Regulator device pointer for the rpmh-regulator
822 * @mode: The regulator framework mode to set
823 *
824 * This function is passed as a callback function into the regulator ops that
825 * are registered for each VRM rpmh-regulator device.
826 *
827 * This function sets the PMIC mode corresponding to the specified framework
828 * mode. The set of PMIC modes allowed is defined in device tree for a given
829 * RPMh regulator resource. The full mapping from PMIC modes to framework modes
830 * is defined in the rpmh_regulator_mode_map[] array. The RPMh resource
831 * specific mapping is defined in the aggr_vreg->mode[] array.
832 *
833 * Return: 0 on success, errno on failure
834 */
835static int rpmh_regulator_vrm_set_mode(struct regulator_dev *rdev,
836 unsigned int mode)
837{
838 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
839 int i;
840
841 for (i = 0; i < vreg->aggr_vreg->mode_count; i++)
842 if (vreg->aggr_vreg->mode[i].framework_mode == mode)
843 break;
844 if (i >= vreg->aggr_vreg->mode_count) {
845 vreg_err(vreg, "invalid mode=%u\n", mode);
846 return -EINVAL;
847 }
848
849 return rpmh_regulator_vrm_set_mode_index(vreg, i);
850}
851
852/**
853 * rpmh_regulator_vrm_get_mode() - get the mode of the VRM rpmh-regulator
854 * @rdev: Regulator device pointer for the rpmh-regulator
855 *
856 * This function is passed as a callback function into the regulator ops that
857 * are registered for each VRM rpmh-regulator device.
858 *
859 * Return: the regulator framework mode of the regulator
860 */
861static unsigned int rpmh_regulator_vrm_get_mode(struct regulator_dev *rdev)
862{
863 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
864
865 return vreg->aggr_vreg->mode[vreg->mode_index].framework_mode;
866}
867
868/**
869 * rpmh_regulator_vrm_set_load() - set the PMIC mode based upon the maximum load
870 * required from the VRM rpmh-regulator
871 * @rdev: Regulator device pointer for the rpmh-regulator
872 * @load_ua: Maximum current required from all consumers in microamps
873 *
874 * This function is passed as a callback function into the regulator ops that
875 * are registered for each VRM rpmh-regulator device.
876 *
877 * This function sets the mode of the regulator to that which has the highest
878 * min support load less than or equal to load_ua. Example:
879 * mode_count = 3
880 * mode[].min_load_ua = 0, 100000, 6000000
881 *
882 * load_ua = 10000 --> mode_index = 0
883 * load_ua = 250000 --> mode_index = 1
884 * load_ua = 7000000 --> mode_index = 2
885 *
886 * Return: 0 on success, errno on failure
887 */
888static int rpmh_regulator_vrm_set_load(struct regulator_dev *rdev, int load_ua)
889{
890 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
891 int i;
892
893 /* No need to check element 0 as it will be the default. */
894 for (i = vreg->aggr_vreg->mode_count - 1; i > 0; i--)
895 if (vreg->aggr_vreg->mode[i].min_load_ua <= load_ua)
896 break;
897
898 return rpmh_regulator_vrm_set_mode_index(vreg, i);
899}
900
901/**
902 * rpmh_regulator_arc_set_voltage_sel() - set the voltage level of the ARC
903 * rpmh-regulator device
904 * @rdev: Regulator device pointer for the rpmh-regulator
905 * @selector: ARC voltage level to set
906 *
907 * This function is passed as a callback function into the regulator ops that
908 * are registered for each ARC rpmh-regulator device.
909 *
910 * Return: 0 on success, errno on failure
911 */
912static int rpmh_regulator_arc_set_voltage_sel(struct regulator_dev *rdev,
913 unsigned int selector)
914{
915 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
916 u32 prev_level;
917 int rc;
918
919 mutex_lock(&vreg->aggr_vreg->lock);
920
921 prev_level = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ARC_LEVEL,
922 selector);
923
924 rc = rpmh_regulator_send_aggregate_requests(vreg);
925 if (rc) {
926 vreg_err(vreg, "set level=%d failed, rc=%d\n",
927 vreg->req.reg[RPMH_REGULATOR_REG_ARC_LEVEL],
928 rc);
929 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ARC_LEVEL,
930 prev_level);
931 }
932
933 mutex_unlock(&vreg->aggr_vreg->lock);
934
935 return rc;
936}
937
938/**
939 * rpmh_regulator_arc_get_voltage_sel() - get the voltage level of the ARC
940 * rpmh-regulator device
941 * @rdev: Regulator device pointer for the rpmh-regulator
942 *
943 * This function is passed as a callback function into the regulator ops that
944 * are registered for each ARC rpmh-regulator device.
945 *
946 * Return: ARC voltage level
947 */
948static int rpmh_regulator_arc_get_voltage_sel(struct regulator_dev *rdev)
949{
950 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
951
952 return vreg->req.reg[RPMH_REGULATOR_REG_ARC_LEVEL];
953}
954
955/**
956 * rpmh_regulator_arc_list_voltage() - return the consumer voltage level mapped
957 * to a given ARC voltage level
958 * @rdev: Regulator device pointer for the rpmh-regulator
959 * @selector: ARC voltage level
960 *
961 * This function is passed as a callback function into the regulator ops that
962 * are registered for each ARC rpmh-regulator device.
963 *
964 * Data ranges:
965 * ARC voltage level: 0 - 15 (fixed in hardware)
966 * Consumer voltage level: 1 - 513 (could be expanded to larger values)
967 *
968 * Return: consumer voltage level
969 */
970static int rpmh_regulator_arc_list_voltage(struct regulator_dev *rdev,
971 unsigned int selector)
972{
973 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
974
975 if (selector >= vreg->aggr_vreg->level_count)
976 return 0;
977
978 return vreg->aggr_vreg->level[selector];
979}
980
981static const struct regulator_ops rpmh_regulator_vrm_ops = {
982 .enable = rpmh_regulator_enable,
983 .disable = rpmh_regulator_disable,
984 .is_enabled = rpmh_regulator_is_enabled,
985 .set_voltage = rpmh_regulator_vrm_set_voltage,
986 .get_voltage = rpmh_regulator_vrm_get_voltage,
987 .set_mode = rpmh_regulator_vrm_set_mode,
988 .get_mode = rpmh_regulator_vrm_get_mode,
989 .set_load = rpmh_regulator_vrm_set_load,
990};
991
992static const struct regulator_ops rpmh_regulator_arc_ops = {
993 .enable = rpmh_regulator_enable,
994 .disable = rpmh_regulator_disable,
995 .is_enabled = rpmh_regulator_is_enabled,
996 .set_voltage_sel = rpmh_regulator_arc_set_voltage_sel,
997 .get_voltage_sel = rpmh_regulator_arc_get_voltage_sel,
998 .list_voltage = rpmh_regulator_arc_list_voltage,
999};
1000
1001static const struct regulator_ops *rpmh_regulator_ops[] = {
1002 [RPMH_REGULATOR_TYPE_VRM] = &rpmh_regulator_vrm_ops,
1003 [RPMH_REGULATOR_TYPE_ARC] = &rpmh_regulator_arc_ops,
1004};
1005
1006/**
1007 * rpmh_regulator_load_arc_level_mapping() - load the RPMh ARC resource's
1008 * voltage level mapping from command db
1009 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1010 *
1011 * The set of supported RPMH_REGULATOR_LEVEL_* voltage levels (0 - ~512) that
1012 * map to ARC operating levels (0 - 15) is defined in aux data per ARC resource
1013 * in the command db SMEM data structure. It is in a u16 array with 1 to 16
1014 * elements. Note that the aux data array may be zero padded at the end for
1015 * data alignment purposes. Such padding entries are invalid and must be
1016 * ignored.
1017 *
1018 * Return: 0 on success, errno on failure
1019 */
1020static int
1021rpmh_regulator_load_arc_level_mapping(struct rpmh_aggr_vreg *aggr_vreg)
1022{
1023 int i, j, len, rc;
1024 u8 *buf;
1025
1026 len = cmd_db_get_aux_data_len(aggr_vreg->resource_name);
1027 if (len < 0) {
1028 aggr_vreg_err(aggr_vreg, "could not get ARC aux data len, rc=%d\n",
1029 len);
1030 return len;
1031 } else if (len == 0) {
1032 aggr_vreg_err(aggr_vreg, "ARC level mapping data missing in command db\n");
1033 return -EINVAL;
1034 } else if (len > RPMH_ARC_MAX_LEVELS * RPMH_ARC_LEVEL_SIZE) {
1035 aggr_vreg_err(aggr_vreg, "more ARC levels defined than allowed: %d > %d\n",
1036 len, RPMH_ARC_MAX_LEVELS * RPMH_ARC_LEVEL_SIZE);
1037 return -EINVAL;
1038 } else if (len % RPMH_ARC_LEVEL_SIZE) {
1039 aggr_vreg_err(aggr_vreg, "invalid ARC aux data size: %d\n",
1040 len);
1041 return -EINVAL;
1042 }
1043
1044 buf = kzalloc(len, GFP_KERNEL);
1045 if (!buf)
1046 return -ENOMEM;
1047
1048 rc = cmd_db_get_aux_data(aggr_vreg->resource_name, buf, len);
1049 if (rc < 0) {
1050 aggr_vreg_err(aggr_vreg, "could not retrieve ARC aux data, rc=%d\n",
1051 rc);
1052 goto done;
1053 } else if (rc != len) {
1054 aggr_vreg_err(aggr_vreg, "could not retrieve all ARC aux data, %d != %d\n",
1055 rc, len);
1056 rc = -EINVAL;
1057 goto done;
1058 }
1059 rc = 0;
1060
1061 aggr_vreg->level_count = len / RPMH_ARC_LEVEL_SIZE;
1062
1063 for (i = 0; i < aggr_vreg->level_count; i++) {
1064 for (j = 0; j < RPMH_ARC_LEVEL_SIZE; j++)
1065 aggr_vreg->level[i] |=
1066 buf[i * RPMH_ARC_LEVEL_SIZE + j] << (8 * j);
1067
1068 /*
1069 * The AUX data may be zero padded. These 0 valued entries at
1070 * the end of the map must be ignored.
1071 */
1072 if (i > 0 && aggr_vreg->level[i] == 0) {
1073 aggr_vreg->level_count = i;
1074 break;
1075 }
1076
1077 /* Add consumer offset to avoid voltage = 0. */
1078 aggr_vreg->level[i] += RPMH_REGULATOR_LEVEL_OFFSET;
1079 aggr_vreg_debug(aggr_vreg, "ARC hlvl=%2d --> vlvl=%4u\n",
1080 i, aggr_vreg->level[i]);
1081 }
1082
1083done:
1084 kfree(buf);
1085 return rc;
1086}
1087
1088/**
1089 * rpmh_regulator_parse_vrm_modes() - parse the supported mode configurations
1090 * for a VRM RPMh resource from device tree
1091 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1092 *
1093 * This function initializes the mode[] array of aggr_vreg based upon the values
1094 * of optional device tree properties.
1095 *
1096 * Return: 0 on success, errno on failure
1097 */
1098static int rpmh_regulator_parse_vrm_modes(struct rpmh_aggr_vreg *aggr_vreg)
1099{
1100 struct device_node *node = aggr_vreg->dev->of_node;
1101 const char *prop = "qcom,supported-modes";
1102 int i, len, rc;
1103 u32 *buf;
1104
1105 /* qcom,supported-modes is optional */
1106 if (!of_find_property(node, prop, &len))
1107 return 0;
1108
1109 len /= sizeof(u32);
1110 aggr_vreg->mode = devm_kcalloc(aggr_vreg->dev, len,
1111 sizeof(*aggr_vreg->mode), GFP_KERNEL);
1112 if (!aggr_vreg->mode)
1113 return -ENOMEM;
1114 aggr_vreg->mode_count = len;
1115
1116
1117 buf = kcalloc(len, sizeof(*buf), GFP_KERNEL);
1118 if (!buf)
1119 return -ENOMEM;
1120
1121 rc = of_property_read_u32_array(node, prop, buf, len);
1122 if (rc) {
1123 aggr_vreg_err(aggr_vreg, "unable to read %s, rc=%d\n",
1124 prop, rc);
1125 goto done;
1126 }
1127
1128 for (i = 0; i < len; i++) {
1129 if (buf[i] >= ARRAY_SIZE(rpmh_regulator_mode_map)) {
1130 aggr_vreg_err(aggr_vreg, "element %d of %s = %u is invalid\n",
1131 i, prop, buf[i]);
1132 rc = -EINVAL;
1133 goto done;
1134 }
1135 aggr_vreg->mode[i].pmic_mode = buf[i];
1136 aggr_vreg->mode[i].framework_mode
1137 = rpmh_regulator_mode_map[buf[i]];
1138
1139 if (i > 0 && aggr_vreg->mode[i].pmic_mode
1140 <= aggr_vreg->mode[i - 1].pmic_mode) {
1141 aggr_vreg_err(aggr_vreg, "%s elements are not in ascending order\n",
1142 prop);
1143 rc = -EINVAL;
1144 goto done;
1145 }
1146 }
1147
1148 prop = "qcom,mode-threshold-currents";
1149
1150 rc = of_property_read_u32_array(node, prop, buf, len);
1151 if (rc) {
1152 aggr_vreg_err(aggr_vreg, "unable to read %s, rc=%d\n",
1153 prop, rc);
1154 goto done;
1155 }
1156
1157 for (i = 0; i < len; i++) {
1158 aggr_vreg->mode[i].min_load_ua = buf[i];
1159
1160 if (i > 0 && aggr_vreg->mode[i].min_load_ua
1161 <= aggr_vreg->mode[i - 1].min_load_ua) {
1162 aggr_vreg_err(aggr_vreg, "%s elements are not in ascending order\n",
1163 prop);
1164 rc = -EINVAL;
1165 goto done;
1166 }
1167 }
1168
1169done:
1170 kfree(buf);
1171 return rc;
1172}
1173
1174/**
1175 * rpmh_regulator_allocate_vreg() - allocate space for the regulators associated
1176 * with the RPMh regulator resource and initialize important
1177 * pointers for each regulator
1178 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1179 *
1180 * Return: 0 on success, errno on failure
1181 */
1182static int rpmh_regulator_allocate_vreg(struct rpmh_aggr_vreg *aggr_vreg)
1183{
1184 struct device_node *node;
1185 int i, rc;
1186
1187 aggr_vreg->vreg_count = 0;
1188
1189 for_each_available_child_of_node(aggr_vreg->dev->of_node, node) {
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001190 /* Skip child nodes handled by other drivers. */
1191 if (of_find_property(node, "compatible", NULL))
1192 continue;
David Collinse1b43192016-10-12 17:27:22 -07001193 aggr_vreg->vreg_count++;
1194 }
1195
1196 if (aggr_vreg->vreg_count == 0) {
1197 aggr_vreg_err(aggr_vreg, "could not find any regulator subnodes\n");
1198 return -ENODEV;
1199 }
1200
1201 aggr_vreg->vreg = devm_kcalloc(aggr_vreg->dev, aggr_vreg->vreg_count,
1202 sizeof(*aggr_vreg->vreg), GFP_KERNEL);
1203 if (!aggr_vreg->vreg)
1204 return -ENOMEM;
1205
1206 i = 0;
1207 for_each_available_child_of_node(aggr_vreg->dev->of_node, node) {
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001208 /* Skip child nodes handled by other drivers. */
1209 if (of_find_property(node, "compatible", NULL))
1210 continue;
1211
David Collinse1b43192016-10-12 17:27:22 -07001212 aggr_vreg->vreg[i].of_node = node;
1213 aggr_vreg->vreg[i].aggr_vreg = aggr_vreg;
1214
1215 rc = of_property_read_string(node, "regulator-name",
1216 &aggr_vreg->vreg[i].rdesc.name);
1217 if (rc) {
1218 aggr_vreg_err(aggr_vreg, "could not read regulator-name property, rc=%d\n",
1219 rc);
1220 return rc;
1221 }
1222
1223 i++;
1224 }
1225
1226 return 0;
1227}
1228
1229/**
1230 * rpmh_regulator_load_default_parameters() - initialize the RPMh resource
1231 * request for this regulator based on optional device tree
1232 * properties
1233 * @vreg: Pointer to the RPMh regulator
1234 *
1235 * Return: 0 on success, errno on failure
1236 */
1237static int rpmh_regulator_load_default_parameters(struct rpmh_vreg *vreg)
1238{
1239 enum rpmh_regulator_type type = vreg->aggr_vreg->regulator_type;
1240 const char *prop;
1241 int i, rc;
1242 u32 temp;
1243
1244 if (type == RPMH_REGULATOR_TYPE_ARC) {
1245 prop = "qcom,init-voltage-level";
1246 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1247 if (!rc) {
1248 for (i = 0; i < vreg->aggr_vreg->level_count; i++)
1249 if (temp <= vreg->aggr_vreg->level[i])
1250 break;
1251 if (i < vreg->aggr_vreg->level_count) {
1252 rpmh_regulator_set_reg(vreg,
1253 RPMH_REGULATOR_REG_ARC_LEVEL, i);
1254 } else {
1255 vreg_err(vreg, "%s=%u is invalid\n",
1256 prop, temp);
1257 return -EINVAL;
1258 }
1259 }
David Collinsd86a1722017-02-07 15:42:22 -08001260
1261 prop = "qcom,min-dropout-voltage-level";
1262 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1263 if (!rc)
1264 vreg->rdesc.min_dropout_uV = temp;
David Collinse1b43192016-10-12 17:27:22 -07001265 } else if (type == RPMH_REGULATOR_TYPE_VRM) {
1266 prop = "qcom,init-enable";
1267 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1268 if (!rc)
1269 rpmh_regulator_set_reg(vreg,
1270 RPMH_REGULATOR_REG_VRM_ENABLE,
1271 !!temp);
1272
1273 prop = "qcom,init-voltage";
1274 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1275 if (!rc) {
1276 if (temp < RPMH_VRM_MIN_UV || temp > RPMH_VRM_MAX_UV) {
1277 vreg_err(vreg, "%s=%u is invalid\n",
1278 prop, temp);
1279 return -EINVAL;
1280 }
1281 rpmh_regulator_set_reg(vreg,
1282 RPMH_REGULATOR_REG_VRM_VOLTAGE,
1283 temp / 1000);
1284 }
1285
1286 prop = "qcom,init-mode";
1287 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1288 if (!rc) {
1289 if (temp < RPMH_VRM_MODE_MIN ||
1290 temp > RPMH_VRM_MODE_MAX) {
1291 vreg_err(vreg, "%s=%u is invalid\n",
1292 prop, temp);
1293 return -EINVAL;
1294 }
1295 rpmh_regulator_set_reg(vreg,
1296 RPMH_REGULATOR_REG_VRM_MODE,
1297 temp);
1298 }
1299
1300 prop = "qcom,init-headroom-voltage";
1301 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1302 if (!rc) {
1303 if (temp < RPMH_VRM_HEADROOM_MIN_UV ||
1304 temp > RPMH_VRM_HEADROOM_MAX_UV) {
1305 vreg_err(vreg, "%s=%u is invalid\n",
1306 prop, temp);
1307 return -EINVAL;
1308 }
1309 rpmh_regulator_set_reg(vreg,
1310 RPMH_REGULATOR_REG_VRM_HEADROOM,
1311 temp / 1000);
1312 }
David Collinsd86a1722017-02-07 15:42:22 -08001313
1314 prop = "qcom,min-dropout-voltage";
1315 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1316 if (!rc)
1317 vreg->rdesc.min_dropout_uV = temp;
David Collinse1b43192016-10-12 17:27:22 -07001318 }
1319
1320 return 0;
1321}
1322
1323/**
1324 * rpmh_regulator_init_vreg_supply() - initialize the regulator's parent supply
1325 * mapping based on optional DT parent supply property
1326 * @vreg: Pointer to the RPMh regulator
1327 *
1328 * Return: 0 on success, errno on failure
1329 */
1330static int rpmh_regulator_init_vreg_supply(struct rpmh_vreg *vreg)
1331{
1332 char *buf;
1333 size_t len;
1334
1335 len = strlen(vreg->rdesc.name) + 16;
1336 buf = kzalloc(len, GFP_KERNEL);
1337 if (!buf)
1338 return -ENOMEM;
1339 scnprintf(buf, len, "%s-parent-supply", vreg->rdesc.name);
1340
1341 if (of_find_property(vreg->aggr_vreg->dev->of_node, buf, NULL)) {
1342 kfree(buf);
1343
1344 len = strlen(vreg->rdesc.name) + 10;
1345 buf = devm_kzalloc(vreg->aggr_vreg->dev, len, GFP_KERNEL);
1346 if (!buf)
1347 return -ENOMEM;
1348 scnprintf(buf, len, "%s-parent", vreg->rdesc.name);
1349
1350 vreg->rdesc.supply_name = buf;
1351 } else {
1352 kfree(buf);
1353 }
1354
1355 return 0;
1356}
1357
1358/**
1359 * rpmh_regulator_init_vreg() - initialize all abbributes of an rpmh-regulator
1360 * @vreg: Pointer to the RPMh regulator
1361 *
1362 * Return: 0 on success, errno on failure
1363 */
1364static int rpmh_regulator_init_vreg(struct rpmh_vreg *vreg)
1365{
1366 struct device *dev = vreg->aggr_vreg->dev;
1367 enum rpmh_regulator_type type = vreg->aggr_vreg->regulator_type;
1368 struct regulator_config reg_config = {};
1369 struct regulator_init_data *init_data;
1370 struct regulator_ops *ops;
1371 int rc, i;
1372 u32 set;
1373
1374 ops = devm_kzalloc(dev, sizeof(*ops), GFP_KERNEL);
1375 if (!ops)
1376 return -ENOMEM;
1377
1378 *ops = *rpmh_regulator_ops[type];
1379 vreg->rdesc.owner = THIS_MODULE;
1380 vreg->rdesc.type = REGULATOR_VOLTAGE;
1381 vreg->rdesc.ops = ops;
1382
1383 init_data = of_get_regulator_init_data(dev,
1384 vreg->of_node, &vreg->rdesc);
1385 if (init_data == NULL)
1386 return -ENOMEM;
1387
1388 init_data->constraints.input_uV = init_data->constraints.max_uV;
1389 if (type == RPMH_REGULATOR_TYPE_VRM) {
1390 init_data->constraints.min_uV
1391 = max(init_data->constraints.min_uV, RPMH_VRM_MIN_UV);
1392 init_data->constraints.min_uV
1393 = min(init_data->constraints.min_uV, RPMH_VRM_MAX_UV);
1394 init_data->constraints.max_uV
1395 = max(init_data->constraints.max_uV, RPMH_VRM_MIN_UV);
1396 init_data->constraints.max_uV
1397 = min(init_data->constraints.max_uV, RPMH_VRM_MAX_UV);
1398 }
1399
1400 if (ops->set_voltage || ops->set_voltage_sel)
1401 init_data->constraints.valid_ops_mask
1402 |= REGULATOR_CHANGE_VOLTAGE;
1403
1404 if (vreg->aggr_vreg->mode_count) {
1405 init_data->constraints.valid_ops_mask
1406 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1407 for (i = 0; i < vreg->aggr_vreg->mode_count; i++)
1408 init_data->constraints.valid_modes_mask
1409 |= vreg->aggr_vreg->mode[i].framework_mode;
1410 } else {
1411 ops->get_mode = NULL;
1412 ops->set_mode = NULL;
1413 ops->set_load = NULL;
1414 }
1415
1416 /*
1417 * Remove enable state control if the ARC resource does not support the
1418 * off level.
1419 */
1420 if (type == RPMH_REGULATOR_TYPE_ARC
1421 && vreg->aggr_vreg->level[0] != RPMH_REGULATOR_LEVEL_OFF) {
1422 ops->enable = NULL;
1423 ops->disable = NULL;
1424 ops->is_enabled = NULL;
1425 }
1426 if (ops->enable)
1427 init_data->constraints.valid_ops_mask
1428 |= REGULATOR_CHANGE_STATUS;
1429
1430 vreg->rdesc.n_voltages = type == RPMH_REGULATOR_TYPE_ARC ?
1431 vreg->aggr_vreg->level_count : 2;
1432
1433 rc = of_property_read_u32(vreg->of_node, "qcom,set", &set);
1434 if (rc) {
1435 vreg_err(vreg, "qcom,set property missing, rc=%d\n", rc);
1436 return rc;
1437 } else if (!(set & RPMH_REGULATOR_SET_ALL)) {
1438 vreg_err(vreg, "qcom,set=%u property is invalid\n", set);
1439 return rc;
1440 }
1441
1442 vreg->set_active = !!(set & RPMH_REGULATOR_SET_ACTIVE);
1443 vreg->set_sleep = !!(set & RPMH_REGULATOR_SET_SLEEP);
1444
1445 rc = rpmh_regulator_init_vreg_supply(vreg);
1446 if (rc) {
1447 vreg_err(vreg, "unable to initialize regulator supply name, rc=%d\n",
1448 rc);
1449 return rc;
1450 }
1451
1452 reg_config.dev = dev;
1453 reg_config.init_data = init_data;
1454 reg_config.of_node = vreg->of_node;
1455 reg_config.driver_data = vreg;
1456
1457 rc = rpmh_regulator_load_default_parameters(vreg);
1458 if (rc) {
1459 vreg_err(vreg, "unable to load default parameters, rc=%d\n",
1460 rc);
1461 return rc;
1462 }
1463
1464 vreg->rdev = devm_regulator_register(dev, &vreg->rdesc, &reg_config);
1465 if (IS_ERR(vreg->rdev)) {
1466 rc = PTR_ERR(vreg->rdev);
1467 vreg->rdev = NULL;
1468 vreg_err(vreg, "devm_regulator_register() failed, rc=%d\n", rc);
1469 return rc;
1470 }
1471
1472 vreg_debug(vreg, "successfully registered; set=%s\n",
1473 vreg->set_active && vreg->set_sleep
1474 ? "active + sleep"
1475 : vreg->set_active ? "active" : "sleep");
1476
1477 return rc;
1478}
1479
1480static const struct of_device_id rpmh_regulator_match_table[] = {
1481 {
1482 .compatible = "qcom,rpmh-vrm-regulator",
1483 .data = (void *)(uintptr_t)RPMH_REGULATOR_TYPE_VRM,
1484 },
1485 {
1486 .compatible = "qcom,rpmh-arc-regulator",
1487 .data = (void *)(uintptr_t)RPMH_REGULATOR_TYPE_ARC,
1488 },
1489 {}
1490};
1491
1492/**
1493 * rpmh_regulator_probe() - probe an aggregated RPMh regulator resource and
1494 * register regulators for each of the regulator nodes associated
1495 * with it
1496 * @pdev: Pointer to the platform device of the aggregated rpmh
1497 * regulator resource
1498 *
1499 * Return: 0 on success, errno on failure
1500 */
1501static int rpmh_regulator_probe(struct platform_device *pdev)
1502{
1503 struct device *dev = &pdev->dev;
1504 const struct of_device_id *match;
1505 struct rpmh_aggr_vreg *aggr_vreg;
1506 struct device_node *node;
1507 int rc, i, sid;
1508
1509 node = dev->of_node;
1510
1511 if (!node) {
1512 dev_err(dev, "Device tree node is missing\n");
1513 return -EINVAL;
1514 }
1515
1516 aggr_vreg = devm_kzalloc(dev, sizeof(*aggr_vreg), GFP_KERNEL);
1517 if (!aggr_vreg)
1518 return -ENOMEM;
1519
1520 aggr_vreg->dev = dev;
1521 mutex_init(&aggr_vreg->lock);
1522
1523 match = of_match_node(rpmh_regulator_match_table, node);
1524 if (match) {
1525 aggr_vreg->regulator_type = (uintptr_t)match->data;
1526 } else {
1527 dev_err(dev, "could not find compatible string match\n");
1528 return -ENODEV;
1529 }
1530
1531 rc = of_property_read_string(node, "qcom,resource-name",
1532 &aggr_vreg->resource_name);
1533 if (rc) {
1534 dev_err(dev, "qcom,resource-name missing in DT node\n");
1535 return rc;
1536 }
1537
1538 aggr_vreg->use_awake_state = of_property_read_bool(node,
1539 "qcom,use-awake-state");
1540
1541 rc = cmd_db_ready();
1542 if (rc) {
1543 if (rc != -EPROBE_DEFER)
1544 aggr_vreg_err(aggr_vreg, "Command DB not available, rc=%d\n",
1545 rc);
1546 return rc;
1547 }
1548
1549 aggr_vreg->addr = cmd_db_get_addr(aggr_vreg->resource_name);
1550 if (!aggr_vreg->addr) {
1551 aggr_vreg_err(aggr_vreg, "could not find RPMh address for resource\n");
1552 return -ENODEV;
1553 }
1554
1555 sid = cmd_db_get_slave_id(aggr_vreg->resource_name);
1556 if (sid < 0) {
1557 aggr_vreg_err(aggr_vreg, "could not find RPMh slave id for resource, rc=%d\n",
1558 sid);
1559 return sid;
1560 }
1561
1562 /* Confirm slave ID listed in command DB matches DT configuration. */
1563 if ((aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
1564 && sid != CMD_DB_HW_ARC)
1565 || (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM
1566 && sid != CMD_DB_HW_VRM)) {
1567 aggr_vreg_err(aggr_vreg, "RPMh slave ID mismatch; config=%d (%s) != cmd-db=%d\n",
1568 aggr_vreg->regulator_type,
1569 aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
1570 ? "ARC" : "VRM",
1571 sid);
1572 return -EINVAL;
1573 }
1574
1575 aggr_vreg->rpmh_client = rpmh_get_byindex(pdev, 0);
1576 if (IS_ERR(aggr_vreg->rpmh_client)) {
1577 rc = PTR_ERR(aggr_vreg->rpmh_client);
1578 if (rc != -EPROBE_DEFER)
1579 aggr_vreg_err(aggr_vreg, "failed to request RPMh client, rc=%d\n",
1580 rc);
1581 return rc;
1582 }
1583
1584 if (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC) {
1585 rc = rpmh_regulator_load_arc_level_mapping(aggr_vreg);
1586 if (rc) {
1587 aggr_vreg_err(aggr_vreg, "could not load arc level mapping, rc=%d\n",
1588 rc);
1589 goto free_client;
1590 }
1591 } else if (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM) {
1592 rc = rpmh_regulator_parse_vrm_modes(aggr_vreg);
1593 if (rc) {
1594 aggr_vreg_err(aggr_vreg, "could not parse vrm mode mapping, rc=%d\n",
1595 rc);
1596 goto free_client;
1597 }
1598 }
1599
1600 aggr_vreg->always_wait_for_ack
1601 = of_property_read_bool(node, "qcom,always-wait-for-ack");
1602
1603 rc = rpmh_regulator_allocate_vreg(aggr_vreg);
1604 if (rc) {
1605 aggr_vreg_err(aggr_vreg, "failed to allocate regulator subnode array, rc=%d\n",
1606 rc);
1607 goto free_client;
1608 }
1609
1610 for (i = 0; i < aggr_vreg->vreg_count; i++) {
1611 rc = rpmh_regulator_init_vreg(&aggr_vreg->vreg[i]);
1612 if (rc) {
1613 pr_err("unable to initialize rpmh-regulator vreg %s for resource %s, rc=%d\n",
1614 aggr_vreg->vreg[i].rdesc.name,
1615 aggr_vreg->resource_name, rc);
1616 goto free_client;
1617 }
1618 }
1619
1620 if (of_property_read_bool(node, "qcom,send-defaults")) {
1621 mutex_lock(&aggr_vreg->lock);
1622 rc = rpmh_regulator_send_aggregate_requests(
1623 &aggr_vreg->vreg[0]);
1624 if (rc) {
1625 aggr_vreg_err(aggr_vreg, "error while sending default request, rc=%d\n",
1626 rc);
1627 mutex_unlock(&aggr_vreg->lock);
1628 goto free_client;
1629 }
1630 mutex_unlock(&aggr_vreg->lock);
1631 }
1632
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001633 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
David Collinse1b43192016-10-12 17:27:22 -07001634 platform_set_drvdata(pdev, aggr_vreg);
1635
1636 aggr_vreg_debug(aggr_vreg, "successfully probed; addr=0x%05X, type=%s\n",
1637 aggr_vreg->addr,
1638 aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
1639 ? "ARC" : "VRM");
1640
1641 return rc;
1642
1643free_client:
1644 rpmh_release(aggr_vreg->rpmh_client);
1645
1646 return rc;
1647}
1648
1649static int rpmh_regulator_remove(struct platform_device *pdev)
1650{
1651 struct rpmh_aggr_vreg *aggr_vreg = platform_get_drvdata(pdev);
1652
1653 rpmh_release(aggr_vreg->rpmh_client);
1654
1655 return 0;
1656}
1657
1658static struct platform_driver rpmh_regulator_driver = {
1659 .driver = {
1660 .name = "qcom,rpmh-regulator",
1661 .of_match_table = rpmh_regulator_match_table,
1662 .owner = THIS_MODULE,
1663 },
1664 .probe = rpmh_regulator_probe,
1665 .remove = rpmh_regulator_remove,
1666};
1667
1668static int rpmh_regulator_init(void)
1669{
1670 return platform_driver_register(&rpmh_regulator_driver);
1671}
1672
1673static void rpmh_regulator_exit(void)
1674{
1675 platform_driver_unregister(&rpmh_regulator_driver);
1676}
1677
1678MODULE_DESCRIPTION("RPMh regulator driver");
1679MODULE_LICENSE("GPL v2");
1680
1681arch_initcall(rpmh_regulator_init);
1682module_exit(rpmh_regulator_exit);