blob: cfc09ba9f8dab8f0739cf36c2830c1f9082a08aa [file] [log] [blame]
David Collins7370f1a2017-01-18 16:21:53 -08001/*
2 * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/bitops.h>
17#include <linux/debugfs.h>
18#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/platform_device.h>
28#include <linux/pm_opp.h>
29#include <linux/slab.h>
30#include <linux/string.h>
31#include <linux/uaccess.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
34#include <linux/regulator/of_regulator.h>
35
36#include "cpr3-regulator.h"
37
38#define MSM8953_APSS_FUSE_CORNERS 4
39
40/**
41 * struct cpr4_msm8953_apss_fuses - APSS specific fuse data for MSM8953
42 * @ro_sel: Ring oscillator select fuse parameter value for each
43 * fuse corner
44 * @init_voltage: Initial (i.e. open-loop) voltage fuse parameter value
45 * for each fuse corner (raw, not converted to a voltage)
46 * @target_quot: CPR target quotient fuse parameter value for each fuse
47 * corner
48 * @quot_offset: CPR target quotient offset fuse parameter value for each
49 * fuse corner (raw, not unpacked) used for target quotient
50 * interpolation
51 * @speed_bin: Application processor speed bin fuse parameter value for
52 * the given chip
53 * @cpr_fusing_rev: CPR fusing revision fuse parameter value
54 * @boost_cfg: CPR boost configuration fuse parameter value
55 * @boost_voltage: CPR boost voltage fuse parameter value (raw, not
56 * converted to a voltage)
57 *
58 * This struct holds the values for all of the fuses read from memory.
59 */
60struct cpr4_msm8953_apss_fuses {
61 u64 ro_sel[MSM8953_APSS_FUSE_CORNERS];
62 u64 init_voltage[MSM8953_APSS_FUSE_CORNERS];
63 u64 target_quot[MSM8953_APSS_FUSE_CORNERS];
64 u64 quot_offset[MSM8953_APSS_FUSE_CORNERS];
65 u64 speed_bin;
66 u64 cpr_fusing_rev;
67 u64 boost_cfg;
68 u64 boost_voltage;
69 u64 misc;
70};
71
72/*
73 * fuse combo = fusing revision + 8 * (speed bin)
74 * where: fusing revision = 0 - 7 and speed bin = 0 - 7
75 */
76#define CPR4_MSM8953_APSS_FUSE_COMBO_COUNT 64
77
78/*
79 * Constants which define the name of each fuse corner.
80 */
81enum cpr4_msm8953_apss_fuse_corner {
82 CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS = 0,
83 CPR4_MSM8953_APSS_FUSE_CORNER_SVS = 1,
84 CPR4_MSM8953_APSS_FUSE_CORNER_NOM = 2,
85 CPR4_MSM8953_APSS_FUSE_CORNER_TURBO_L1 = 3,
86};
87
88static const char * const cpr4_msm8953_apss_fuse_corner_name[] = {
89 [CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS] = "LowSVS",
90 [CPR4_MSM8953_APSS_FUSE_CORNER_SVS] = "SVS",
91 [CPR4_MSM8953_APSS_FUSE_CORNER_NOM] = "NOM",
92 [CPR4_MSM8953_APSS_FUSE_CORNER_TURBO_L1] = "TURBO_L1",
93};
94
95/*
96 * MSM8953 APSS fuse parameter locations:
97 *
98 * Structs are organized with the following dimensions:
99 * Outer: 0 to 3 for fuse corners from lowest to highest corner
100 * Inner: large enough to hold the longest set of parameter segments which
101 * fully defines a fuse parameter, +1 (for NULL termination).
102 * Each segment corresponds to a contiguous group of bits from a
103 * single fuse row. These segments are concatentated together in
104 * order to form the full fuse parameter value. The segments for
105 * a given parameter may correspond to different fuse rows.
106 */
107static const struct cpr3_fuse_param
108msm8953_apss_ro_sel_param[MSM8953_APSS_FUSE_CORNERS][2] = {
109 {{73, 12, 15}, {} },
110 {{73, 8, 11}, {} },
111 {{73, 4, 7}, {} },
112 {{73, 0, 3}, {} },
113};
114
115static const struct cpr3_fuse_param
116msm8953_apss_init_voltage_param[MSM8953_APSS_FUSE_CORNERS][2] = {
117 {{71, 24, 29}, {} },
118 {{71, 18, 23}, {} },
119 {{71, 12, 17}, {} },
120 {{71, 6, 11}, {} },
121};
122
123static const struct cpr3_fuse_param
124msm8953_apss_target_quot_param[MSM8953_APSS_FUSE_CORNERS][2] = {
125 {{72, 44, 55}, {} },
126 {{72, 32, 43}, {} },
127 {{72, 20, 31}, {} },
128 {{72, 8, 19}, {} },
129};
130
131static const struct cpr3_fuse_param
132msm8953_apss_quot_offset_param[MSM8953_APSS_FUSE_CORNERS][2] = {
133 {{} },
134 {{71, 46, 52}, {} },
135 {{71, 39, 45}, {} },
136 {{71, 32, 38}, {} },
137};
138
139static const struct cpr3_fuse_param msm8953_cpr_fusing_rev_param[] = {
140 {71, 53, 55},
141 {},
142};
143
144static const struct cpr3_fuse_param msm8953_apss_speed_bin_param[] = {
145 {36, 40, 42},
146 {},
147};
148
149static const struct cpr3_fuse_param msm8953_cpr_boost_fuse_cfg_param[] = {
150 {36, 43, 45},
151 {},
152};
153
154static const struct cpr3_fuse_param msm8953_apss_boost_fuse_volt_param[] = {
155 {71, 0, 5},
156 {},
157};
158
159static const struct cpr3_fuse_param msm8953_misc_fuse_volt_adj_param[] = {
160 {36, 54, 54},
161 {},
162};
163
164/*
165 * The number of possible values for misc fuse is
166 * 2^(#bits defined for misc fuse)
167 */
168#define MSM8953_MISC_FUSE_VAL_COUNT BIT(1)
169
170/*
171 * Open loop voltage fuse reference voltages in microvolts for MSM8953
172 */
173static const int msm8953_apss_fuse_ref_volt
174 [MSM8953_APSS_FUSE_CORNERS] = {
175 645000,
176 720000,
177 865000,
178 1065000,
179};
180
181#define MSM8953_APSS_FUSE_STEP_VOLT 10000
182#define MSM8953_APSS_VOLTAGE_FUSE_SIZE 6
183#define MSM8953_APSS_QUOT_OFFSET_SCALE 5
184
185#define MSM8953_APSS_CPR_SENSOR_COUNT 13
186
187#define MSM8953_APSS_CPR_CLOCK_RATE 19200000
188
189#define MSM8953_APSS_MAX_TEMP_POINTS 3
190#define MSM8953_APSS_TEMP_SENSOR_ID_START 4
191#define MSM8953_APSS_TEMP_SENSOR_ID_END 13
192/*
193 * Boost voltage fuse reference and ceiling voltages in microvolts for
194 * MSM8953.
195 */
196#define MSM8953_APSS_BOOST_FUSE_REF_VOLT 1140000
197#define MSM8953_APSS_BOOST_CEILING_VOLT 1140000
198#define MSM8953_APSS_BOOST_FLOOR_VOLT 900000
199#define MAX_BOOST_CONFIG_FUSE_VALUE 8
200
201#define MSM8953_APSS_CPR_SDELTA_CORE_COUNT 15
202
203/*
204 * Array of integer values mapped to each of the boost config fuse values to
205 * indicate boost enable/disable status.
206 */
207static bool boost_fuse[MAX_BOOST_CONFIG_FUSE_VALUE] = {0, 1, 1, 1, 1, 1, 1, 1};
208
209/**
210 * cpr4_msm8953_apss_read_fuse_data() - load APSS specific fuse parameter values
211 * @vreg: Pointer to the CPR3 regulator
212 *
213 * This function allocates a cpr4_msm8953_apss_fuses struct, fills it with
214 * values read out of hardware fuses, and finally copies common fuse values
215 * into the CPR3 regulator struct.
216 *
217 * Return: 0 on success, errno on failure
218 */
219static int cpr4_msm8953_apss_read_fuse_data(struct cpr3_regulator *vreg)
220{
221 void __iomem *base = vreg->thread->ctrl->fuse_base;
222 struct cpr4_msm8953_apss_fuses *fuse;
223 int i, rc;
224
225 fuse = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*fuse), GFP_KERNEL);
226 if (!fuse)
227 return -ENOMEM;
228
229 rc = cpr3_read_fuse_param(base, msm8953_apss_speed_bin_param,
230 &fuse->speed_bin);
231 if (rc) {
232 cpr3_err(vreg, "Unable to read speed bin fuse, rc=%d\n", rc);
233 return rc;
234 }
235 cpr3_info(vreg, "speed bin = %llu\n", fuse->speed_bin);
236
237 rc = cpr3_read_fuse_param(base, msm8953_cpr_fusing_rev_param,
238 &fuse->cpr_fusing_rev);
239 if (rc) {
240 cpr3_err(vreg, "Unable to read CPR fusing revision fuse, rc=%d\n",
241 rc);
242 return rc;
243 }
244 cpr3_info(vreg, "CPR fusing revision = %llu\n", fuse->cpr_fusing_rev);
245
246 rc = cpr3_read_fuse_param(base, msm8953_misc_fuse_volt_adj_param,
247 &fuse->misc);
248 if (rc) {
249 cpr3_err(vreg, "Unable to read misc voltage adjustment fuse, rc=%d\n",
250 rc);
251 return rc;
252 }
253 cpr3_info(vreg, "CPR misc fuse value = %llu\n", fuse->misc);
254 if (fuse->misc >= MSM8953_MISC_FUSE_VAL_COUNT) {
255 cpr3_err(vreg, "CPR misc fuse value = %llu, should be < %lu\n",
256 fuse->misc, MSM8953_MISC_FUSE_VAL_COUNT);
257 return -EINVAL;
258 }
259
260 for (i = 0; i < MSM8953_APSS_FUSE_CORNERS; i++) {
261 rc = cpr3_read_fuse_param(base,
262 msm8953_apss_init_voltage_param[i],
263 &fuse->init_voltage[i]);
264 if (rc) {
265 cpr3_err(vreg, "Unable to read fuse-corner %d initial voltage fuse, rc=%d\n",
266 i, rc);
267 return rc;
268 }
269
270 rc = cpr3_read_fuse_param(base,
271 msm8953_apss_target_quot_param[i],
272 &fuse->target_quot[i]);
273 if (rc) {
274 cpr3_err(vreg, "Unable to read fuse-corner %d target quotient fuse, rc=%d\n",
275 i, rc);
276 return rc;
277 }
278
279 rc = cpr3_read_fuse_param(base,
280 msm8953_apss_ro_sel_param[i],
281 &fuse->ro_sel[i]);
282 if (rc) {
283 cpr3_err(vreg, "Unable to read fuse-corner %d RO select fuse, rc=%d\n",
284 i, rc);
285 return rc;
286 }
287
288 rc = cpr3_read_fuse_param(base,
289 msm8953_apss_quot_offset_param[i],
290 &fuse->quot_offset[i]);
291 if (rc) {
292 cpr3_err(vreg, "Unable to read fuse-corner %d quotient offset fuse, rc=%d\n",
293 i, rc);
294 return rc;
295 }
296 }
297
298 rc = cpr3_read_fuse_param(base, msm8953_cpr_boost_fuse_cfg_param,
299 &fuse->boost_cfg);
300 if (rc) {
301 cpr3_err(vreg, "Unable to read CPR boost config fuse, rc=%d\n",
302 rc);
303 return rc;
304 }
305 cpr3_info(vreg, "Voltage boost fuse config = %llu boost = %s\n",
306 fuse->boost_cfg, boost_fuse[fuse->boost_cfg]
307 ? "enable" : "disable");
308
309 rc = cpr3_read_fuse_param(base,
310 msm8953_apss_boost_fuse_volt_param,
311 &fuse->boost_voltage);
312 if (rc) {
313 cpr3_err(vreg, "failed to read boost fuse voltage, rc=%d\n",
314 rc);
315 return rc;
316 }
317
318 vreg->fuse_combo = fuse->cpr_fusing_rev + 8 * fuse->speed_bin;
319 if (vreg->fuse_combo >= CPR4_MSM8953_APSS_FUSE_COMBO_COUNT) {
320 cpr3_err(vreg, "invalid CPR fuse combo = %d found\n",
321 vreg->fuse_combo);
322 return -EINVAL;
323 }
324
325 vreg->speed_bin_fuse = fuse->speed_bin;
326 vreg->cpr_rev_fuse = fuse->cpr_fusing_rev;
327 vreg->fuse_corner_count = MSM8953_APSS_FUSE_CORNERS;
328 vreg->platform_fuses = fuse;
329
330 return 0;
331}
332
333/**
334 * cpr4_apss_parse_corner_data() - parse APSS corner data from device tree
335 * properties of the CPR3 regulator's device node
336 * @vreg: Pointer to the CPR3 regulator
337 *
338 * Return: 0 on success, errno on failure
339 */
340static int cpr4_apss_parse_corner_data(struct cpr3_regulator *vreg)
341{
342 int rc;
343
344 rc = cpr3_parse_common_corner_data(vreg);
345 if (rc) {
346 cpr3_err(vreg, "error reading corner data, rc=%d\n", rc);
347 return rc;
348 }
349
350 return rc;
351}
352
353/**
354 * cpr4_apss_parse_misc_fuse_voltage_adjustments() - fill an array from a
355 * portion of the voltage adjustments specified based on
356 * miscellaneous fuse bits.
357 * @vreg: Pointer to the CPR3 regulator
358 * @volt_adjust: Voltage adjustment output data array which must be
359 * of size vreg->corner_count
360 *
361 * cpr3_parse_common_corner_data() must be called for vreg before this function
362 * is called so that speed bin size elements are initialized.
363 *
364 * Two formats are supported for the device tree property:
365 * 1. Length == tuple_list_size * vreg->corner_count
366 * (reading begins at index 0)
367 * 2. Length == tuple_list_size * vreg->speed_bin_corner_sum
368 * (reading begins at index tuple_list_size * vreg->speed_bin_offset)
369 *
370 * Here, tuple_list_size is the number of possible values for misc fuse.
371 * All other property lengths are treated as errors.
372 *
373 * Return: 0 on success, errno on failure
374 */
375static int cpr4_apss_parse_misc_fuse_voltage_adjustments(
376 struct cpr3_regulator *vreg, u32 *volt_adjust)
377{
378 struct device_node *node = vreg->of_node;
379 struct cpr4_msm8953_apss_fuses *fuse = vreg->platform_fuses;
380 int tuple_list_size = MSM8953_MISC_FUSE_VAL_COUNT;
381 int i, offset, rc, len = 0;
382 const char *prop_name = "qcom,cpr-misc-fuse-voltage-adjustment";
383
384 if (!of_find_property(node, prop_name, &len)) {
385 cpr3_err(vreg, "property %s is missing\n", prop_name);
386 return -EINVAL;
387 }
388
389 if (len == tuple_list_size * vreg->corner_count * sizeof(u32)) {
390 offset = 0;
391 } else if (vreg->speed_bin_corner_sum > 0 &&
392 len == tuple_list_size * vreg->speed_bin_corner_sum
393 * sizeof(u32)) {
394 offset = tuple_list_size * vreg->speed_bin_offset
395 + fuse->misc * vreg->corner_count;
396 } else {
397 if (vreg->speed_bin_corner_sum > 0)
398 cpr3_err(vreg, "property %s has invalid length=%d, should be %zu or %zu\n",
399 prop_name, len,
400 tuple_list_size * vreg->corner_count
401 * sizeof(u32),
402 tuple_list_size * vreg->speed_bin_corner_sum
403 * sizeof(u32));
404 else
405 cpr3_err(vreg, "property %s has invalid length=%d, should be %zu\n",
406 prop_name, len,
407 tuple_list_size * vreg->corner_count
408 * sizeof(u32));
409 return -EINVAL;
410 }
411
412 for (i = 0; i < vreg->corner_count; i++) {
413 rc = of_property_read_u32_index(node, prop_name, offset + i,
414 &volt_adjust[i]);
415 if (rc) {
416 cpr3_err(vreg, "error reading property %s, rc=%d\n",
417 prop_name, rc);
418 return rc;
419 }
420 }
421
422 return 0;
423}
424
425/**
426 * cpr4_msm8953_apss_calculate_open_loop_voltages() - calculate the open-loop
427 * voltage for each corner of a CPR3 regulator
428 * @vreg: Pointer to the CPR3 regulator
429 *
430 * If open-loop voltage interpolation is allowed in device tree, then
431 * this function calculates the open-loop voltage for a given corner using
432 * linear interpolation. This interpolation is performed using the processor
433 * frequencies of the lower and higher Fmax corners along with their fused
434 * open-loop voltages.
435 *
436 * If open-loop voltage interpolation is not allowed, then this function uses
437 * the Fmax fused open-loop voltage for all of the corners associated with a
438 * given fuse corner.
439 *
440 * Return: 0 on success, errno on failure
441 */
442static int cpr4_msm8953_apss_calculate_open_loop_voltages(
443 struct cpr3_regulator *vreg)
444{
445 struct device_node *node = vreg->of_node;
446 struct cpr4_msm8953_apss_fuses *fuse = vreg->platform_fuses;
447 int i, j, rc = 0;
448 bool allow_interpolation;
449 u64 freq_low, volt_low, freq_high, volt_high;
450 int *fuse_volt, *misc_adj_volt;
451 int *fmax_corner;
452
453 fuse_volt = kcalloc(vreg->fuse_corner_count, sizeof(*fuse_volt),
454 GFP_KERNEL);
455 fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner),
456 GFP_KERNEL);
457 if (!fuse_volt || !fmax_corner) {
458 rc = -ENOMEM;
459 goto done;
460 }
461
462 for (i = 0; i < vreg->fuse_corner_count; i++) {
463 fuse_volt[i] = cpr3_convert_open_loop_voltage_fuse(
464 msm8953_apss_fuse_ref_volt[i],
465 MSM8953_APSS_FUSE_STEP_VOLT, fuse->init_voltage[i],
466 MSM8953_APSS_VOLTAGE_FUSE_SIZE);
467
468 /* Log fused open-loop voltage values for debugging purposes. */
469 cpr3_info(vreg, "fused %8s: open-loop=%7d uV\n",
470 cpr4_msm8953_apss_fuse_corner_name[i],
471 fuse_volt[i]);
472 }
473
474 rc = cpr3_adjust_fused_open_loop_voltages(vreg, fuse_volt);
475 if (rc) {
476 cpr3_err(vreg, "fused open-loop voltage adjustment failed, rc=%d\n",
477 rc);
478 goto done;
479 }
480
481 allow_interpolation = of_property_read_bool(node,
482 "qcom,allow-voltage-interpolation");
483
484 for (i = 1; i < vreg->fuse_corner_count; i++) {
485 if (fuse_volt[i] < fuse_volt[i - 1]) {
486 cpr3_info(vreg, "fuse corner %d voltage=%d uV < fuse corner %d voltage=%d uV; overriding: fuse corner %d voltage=%d\n",
487 i, fuse_volt[i], i - 1, fuse_volt[i - 1],
488 i, fuse_volt[i - 1]);
489 fuse_volt[i] = fuse_volt[i - 1];
490 }
491 }
492
493 if (!allow_interpolation) {
494 /* Use fused open-loop voltage for lower frequencies. */
495 for (i = 0; i < vreg->corner_count; i++)
496 vreg->corner[i].open_loop_volt
497 = fuse_volt[vreg->corner[i].cpr_fuse_corner];
498 goto done;
499 }
500
501 /* Determine highest corner mapped to each fuse corner */
502 j = vreg->fuse_corner_count - 1;
503 for (i = vreg->corner_count - 1; i >= 0; i--) {
504 if (vreg->corner[i].cpr_fuse_corner == j) {
505 fmax_corner[j] = i;
506 j--;
507 }
508 }
509 if (j >= 0) {
510 cpr3_err(vreg, "invalid fuse corner mapping\n");
511 rc = -EINVAL;
512 goto done;
513 }
514
515 /*
516 * Interpolation is not possible for corners mapped to the lowest fuse
517 * corner so use the fuse corner value directly.
518 */
519 for (i = 0; i <= fmax_corner[0]; i++)
520 vreg->corner[i].open_loop_volt = fuse_volt[0];
521
522 /* Interpolate voltages for the higher fuse corners. */
523 for (i = 1; i < vreg->fuse_corner_count; i++) {
524 freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq;
525 volt_low = fuse_volt[i - 1];
526 freq_high = vreg->corner[fmax_corner[i]].proc_freq;
527 volt_high = fuse_volt[i];
528
529 for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++)
530 vreg->corner[j].open_loop_volt = cpr3_interpolate(
531 freq_low, volt_low, freq_high, volt_high,
532 vreg->corner[j].proc_freq);
533 }
534
535done:
536 if (rc == 0) {
537 cpr3_debug(vreg, "unadjusted per-corner open-loop voltages:\n");
538 for (i = 0; i < vreg->corner_count; i++)
539 cpr3_debug(vreg, "open-loop[%2d] = %d uV\n", i,
540 vreg->corner[i].open_loop_volt);
541
542 rc = cpr3_adjust_open_loop_voltages(vreg);
543 if (rc)
544 cpr3_err(vreg, "open-loop voltage adjustment failed, rc=%d\n",
545 rc);
546
547 if (of_find_property(node,
548 "qcom,cpr-misc-fuse-voltage-adjustment",
549 NULL)) {
550 misc_adj_volt = kcalloc(vreg->corner_count,
551 sizeof(*misc_adj_volt), GFP_KERNEL);
552 if (!misc_adj_volt) {
553 rc = -ENOMEM;
554 goto _exit;
555 }
556
557 rc = cpr4_apss_parse_misc_fuse_voltage_adjustments(vreg,
558 misc_adj_volt);
559 if (rc) {
560 cpr3_err(vreg, "qcom,cpr-misc-fuse-voltage-adjustment reading failed, rc=%d\n",
561 rc);
562 kfree(misc_adj_volt);
563 goto _exit;
564 }
565
566 for (i = 0; i < vreg->corner_count; i++)
567 vreg->corner[i].open_loop_volt
568 += misc_adj_volt[i];
569 kfree(misc_adj_volt);
570 }
571 }
572
573_exit:
574 kfree(fuse_volt);
575 kfree(fmax_corner);
576 return rc;
577}
578
579/**
580 * cpr4_msm8953_apss_set_no_interpolation_quotients() - use the fused target
581 * quotient values for lower frequencies.
582 * @vreg: Pointer to the CPR3 regulator
583 * @volt_adjust: Pointer to array of per-corner closed-loop adjustment
584 * voltages
585 * @volt_adjust_fuse: Pointer to array of per-fuse-corner closed-loop
586 * adjustment voltages
587 * @ro_scale: Pointer to array of per-fuse-corner RO scaling factor
588 * values with units of QUOT/V
589 *
590 * Return: 0 on success, errno on failure
591 */
592static int cpr4_msm8953_apss_set_no_interpolation_quotients(
593 struct cpr3_regulator *vreg, int *volt_adjust,
594 int *volt_adjust_fuse, int *ro_scale)
595{
596 struct cpr4_msm8953_apss_fuses *fuse = vreg->platform_fuses;
597 u32 quot, ro;
598 int quot_adjust;
599 int i, fuse_corner;
600
601 for (i = 0; i < vreg->corner_count; i++) {
602 fuse_corner = vreg->corner[i].cpr_fuse_corner;
603 quot = fuse->target_quot[fuse_corner];
604 quot_adjust = cpr3_quot_adjustment(ro_scale[fuse_corner],
605 volt_adjust_fuse[fuse_corner] +
606 volt_adjust[i]);
607 ro = fuse->ro_sel[fuse_corner];
608 vreg->corner[i].target_quot[ro] = quot + quot_adjust;
609 cpr3_debug(vreg, "corner=%d RO=%u target quot=%u\n",
610 i, ro, quot);
611
612 if (quot_adjust)
613 cpr3_debug(vreg, "adjusted corner %d RO%u target quot: %u --> %u (%d uV)\n",
614 i, ro, quot, vreg->corner[i].target_quot[ro],
615 volt_adjust_fuse[fuse_corner] +
616 volt_adjust[i]);
617 }
618
619 return 0;
620}
621
622/**
623 * cpr4_msm8953_apss_calculate_target_quotients() - calculate the CPR target
624 * quotient for each corner of a CPR3 regulator
625 * @vreg: Pointer to the CPR3 regulator
626 *
627 * If target quotient interpolation is allowed in device tree, then this
628 * function calculates the target quotient for a given corner using linear
629 * interpolation. This interpolation is performed using the processor
630 * frequencies of the lower and higher Fmax corners along with the fused
631 * target quotient and quotient offset of the higher Fmax corner.
632 *
633 * If target quotient interpolation is not allowed, then this function uses
634 * the Fmax fused target quotient for all of the corners associated with a
635 * given fuse corner.
636 *
637 * Return: 0 on success, errno on failure
638 */
639static int cpr4_msm8953_apss_calculate_target_quotients(
640 struct cpr3_regulator *vreg)
641{
642 struct cpr4_msm8953_apss_fuses *fuse = vreg->platform_fuses;
643 int rc;
644 bool allow_interpolation;
645 u64 freq_low, freq_high, prev_quot;
646 u64 *quot_low;
647 u64 *quot_high;
648 u32 quot, ro;
649 int i, j, fuse_corner, quot_adjust;
650 int *fmax_corner;
651 int *volt_adjust, *volt_adjust_fuse, *ro_scale;
652 int *voltage_adj_misc;
653
654 /* Log fused quotient values for debugging purposes. */
655 cpr3_info(vreg, "fused LowSVS: quot[%2llu]=%4llu\n",
656 fuse->ro_sel[CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS],
657 fuse->target_quot[CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS]);
658 for (i = CPR4_MSM8953_APSS_FUSE_CORNER_SVS;
659 i <= CPR4_MSM8953_APSS_FUSE_CORNER_TURBO_L1; i++)
660 cpr3_info(vreg, "fused %8s: quot[%2llu]=%4llu, quot_offset[%2llu]=%4llu\n",
661 cpr4_msm8953_apss_fuse_corner_name[i],
662 fuse->ro_sel[i], fuse->target_quot[i],
663 fuse->ro_sel[i], fuse->quot_offset[i] *
664 MSM8953_APSS_QUOT_OFFSET_SCALE);
665
666 allow_interpolation = of_property_read_bool(vreg->of_node,
667 "qcom,allow-quotient-interpolation");
668
669 volt_adjust = kcalloc(vreg->corner_count, sizeof(*volt_adjust),
670 GFP_KERNEL);
671 volt_adjust_fuse = kcalloc(vreg->fuse_corner_count,
672 sizeof(*volt_adjust_fuse), GFP_KERNEL);
673 ro_scale = kcalloc(vreg->fuse_corner_count, sizeof(*ro_scale),
674 GFP_KERNEL);
675 fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner),
676 GFP_KERNEL);
677 quot_low = kcalloc(vreg->fuse_corner_count, sizeof(*quot_low),
678 GFP_KERNEL);
679 quot_high = kcalloc(vreg->fuse_corner_count, sizeof(*quot_high),
680 GFP_KERNEL);
681 if (!volt_adjust || !volt_adjust_fuse || !ro_scale ||
682 !fmax_corner || !quot_low || !quot_high) {
683 rc = -ENOMEM;
684 goto done;
685 }
686
687 rc = cpr3_parse_closed_loop_voltage_adjustments(vreg, &fuse->ro_sel[0],
688 volt_adjust, volt_adjust_fuse, ro_scale);
689 if (rc) {
690 cpr3_err(vreg, "could not load closed-loop voltage adjustments, rc=%d\n",
691 rc);
692 goto done;
693 }
694
695 if (of_find_property(vreg->of_node,
696 "qcom,cpr-misc-fuse-voltage-adjustment", NULL)) {
697 voltage_adj_misc = kcalloc(vreg->corner_count,
698 sizeof(*voltage_adj_misc), GFP_KERNEL);
699 if (!voltage_adj_misc) {
700 rc = -ENOMEM;
701 goto done;
702 }
703
704 rc = cpr4_apss_parse_misc_fuse_voltage_adjustments(vreg,
705 voltage_adj_misc);
706 if (rc) {
707 cpr3_err(vreg, "qcom,cpr-misc-fuse-voltage-adjustment reading failed, rc=%d\n",
708 rc);
709 kfree(voltage_adj_misc);
710 goto done;
711 }
712
713 for (i = 0; i < vreg->corner_count; i++)
714 volt_adjust[i] += voltage_adj_misc[i];
715
716 kfree(voltage_adj_misc);
717 }
718
719 if (!allow_interpolation) {
720 /* Use fused target quotients for lower frequencies. */
721 return cpr4_msm8953_apss_set_no_interpolation_quotients(
722 vreg, volt_adjust, volt_adjust_fuse, ro_scale);
723 }
724
725 /* Determine highest corner mapped to each fuse corner */
726 j = vreg->fuse_corner_count - 1;
727 for (i = vreg->corner_count - 1; i >= 0; i--) {
728 if (vreg->corner[i].cpr_fuse_corner == j) {
729 fmax_corner[j] = i;
730 j--;
731 }
732 }
733 if (j >= 0) {
734 cpr3_err(vreg, "invalid fuse corner mapping\n");
735 rc = -EINVAL;
736 goto done;
737 }
738
739 /*
740 * Interpolation is not possible for corners mapped to the lowest fuse
741 * corner so use the fuse corner value directly.
742 */
743 i = CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS;
744 quot_adjust = cpr3_quot_adjustment(ro_scale[i], volt_adjust_fuse[i]);
745 quot = fuse->target_quot[i] + quot_adjust;
746 quot_high[i] = quot_low[i] = quot;
747 ro = fuse->ro_sel[i];
748 if (quot_adjust)
749 cpr3_debug(vreg, "adjusted fuse corner %d RO%u target quot: %llu --> %u (%d uV)\n",
750 i, ro, fuse->target_quot[i], quot, volt_adjust_fuse[i]);
751
752 for (i = 0; i <= fmax_corner[CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS];
753 i++)
754 vreg->corner[i].target_quot[ro] = quot;
755
756 for (i = CPR4_MSM8953_APSS_FUSE_CORNER_SVS;
757 i < vreg->fuse_corner_count; i++) {
758 quot_high[i] = fuse->target_quot[i];
759 if (fuse->ro_sel[i] == fuse->ro_sel[i - 1])
760 quot_low[i] = quot_high[i - 1];
761 else
762 quot_low[i] = quot_high[i]
763 - fuse->quot_offset[i]
764 * MSM8953_APSS_QUOT_OFFSET_SCALE;
765 if (quot_high[i] < quot_low[i]) {
766 cpr3_debug(vreg, "quot_high[%d]=%llu < quot_low[%d]=%llu; overriding: quot_high[%d]=%llu\n",
767 i, quot_high[i], i, quot_low[i],
768 i, quot_low[i]);
769 quot_high[i] = quot_low[i];
770 }
771 }
772
773 /* Perform per-fuse-corner target quotient adjustment */
774 for (i = 1; i < vreg->fuse_corner_count; i++) {
775 quot_adjust = cpr3_quot_adjustment(ro_scale[i],
776 volt_adjust_fuse[i]);
777 if (quot_adjust) {
778 prev_quot = quot_high[i];
779 quot_high[i] += quot_adjust;
780 cpr3_debug(vreg, "adjusted fuse corner %d RO%llu target quot: %llu --> %llu (%d uV)\n",
781 i, fuse->ro_sel[i], prev_quot, quot_high[i],
782 volt_adjust_fuse[i]);
783 }
784
785 if (fuse->ro_sel[i] == fuse->ro_sel[i - 1])
786 quot_low[i] = quot_high[i - 1];
787 else
788 quot_low[i] += cpr3_quot_adjustment(ro_scale[i],
789 volt_adjust_fuse[i - 1]);
790
791 if (quot_high[i] < quot_low[i]) {
792 cpr3_debug(vreg, "quot_high[%d]=%llu < quot_low[%d]=%llu after adjustment; overriding: quot_high[%d]=%llu\n",
793 i, quot_high[i], i, quot_low[i],
794 i, quot_low[i]);
795 quot_high[i] = quot_low[i];
796 }
797 }
798
799 /* Interpolate voltages for the higher fuse corners. */
800 for (i = 1; i < vreg->fuse_corner_count; i++) {
801 freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq;
802 freq_high = vreg->corner[fmax_corner[i]].proc_freq;
803
804 ro = fuse->ro_sel[i];
805 for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++)
806 vreg->corner[j].target_quot[ro] = cpr3_interpolate(
807 freq_low, quot_low[i], freq_high, quot_high[i],
808 vreg->corner[j].proc_freq);
809 }
810
811 /* Perform per-corner target quotient adjustment */
812 for (i = 0; i < vreg->corner_count; i++) {
813 fuse_corner = vreg->corner[i].cpr_fuse_corner;
814 ro = fuse->ro_sel[fuse_corner];
815 quot_adjust = cpr3_quot_adjustment(ro_scale[fuse_corner],
816 volt_adjust[i]);
817 if (quot_adjust) {
818 prev_quot = vreg->corner[i].target_quot[ro];
819 vreg->corner[i].target_quot[ro] += quot_adjust;
820 cpr3_debug(vreg, "adjusted corner %d RO%u target quot: %llu --> %u (%d uV)\n",
821 i, ro, prev_quot,
822 vreg->corner[i].target_quot[ro],
823 volt_adjust[i]);
824 }
825 }
826
827 /* Ensure that target quotients increase monotonically */
828 for (i = 1; i < vreg->corner_count; i++) {
829 ro = fuse->ro_sel[vreg->corner[i].cpr_fuse_corner];
830 if (fuse->ro_sel[vreg->corner[i - 1].cpr_fuse_corner] == ro
831 && vreg->corner[i].target_quot[ro]
832 < vreg->corner[i - 1].target_quot[ro]) {
833 cpr3_debug(vreg, "adjusted corner %d RO%u target quot=%u < adjusted corner %d RO%u target quot=%u; overriding: corner %d RO%u target quot=%u\n",
834 i, ro, vreg->corner[i].target_quot[ro],
835 i - 1, ro, vreg->corner[i - 1].target_quot[ro],
836 i, ro, vreg->corner[i - 1].target_quot[ro]);
837 vreg->corner[i].target_quot[ro]
838 = vreg->corner[i - 1].target_quot[ro];
839 }
840 }
841
842done:
843 kfree(volt_adjust);
844 kfree(volt_adjust_fuse);
845 kfree(ro_scale);
846 kfree(fmax_corner);
847 kfree(quot_low);
848 kfree(quot_high);
849 return rc;
850}
851
852/**
853 * cpr4_apss_print_settings() - print out APSS CPR configuration settings into
854 * the kernel log for debugging purposes
855 * @vreg: Pointer to the CPR3 regulator
856 */
857static void cpr4_apss_print_settings(struct cpr3_regulator *vreg)
858{
859 struct cpr3_corner *corner;
860 int i;
861
862 cpr3_debug(vreg, "Corner: Frequency (Hz), Fuse Corner, Floor (uV), Open-Loop (uV), Ceiling (uV)\n");
863 for (i = 0; i < vreg->corner_count; i++) {
864 corner = &vreg->corner[i];
865 cpr3_debug(vreg, "%3d: %10u, %2d, %7d, %7d, %7d\n",
866 i, corner->proc_freq, corner->cpr_fuse_corner,
867 corner->floor_volt, corner->open_loop_volt,
868 corner->ceiling_volt);
869 }
870
871 if (vreg->thread->ctrl->apm)
872 cpr3_debug(vreg, "APM threshold = %d uV, APM adjust = %d uV\n",
873 vreg->thread->ctrl->apm_threshold_volt,
874 vreg->thread->ctrl->apm_adj_volt);
875}
876
877/**
878 * cpr4_apss_init_thread() - perform steps necessary to initialize the
879 * configuration data for a CPR3 thread
880 * @thread: Pointer to the CPR3 thread
881 *
882 * Return: 0 on success, errno on failure
883 */
884static int cpr4_apss_init_thread(struct cpr3_thread *thread)
885{
886 int rc;
887
888 rc = cpr3_parse_common_thread_data(thread);
889 if (rc) {
890 cpr3_err(thread->ctrl, "thread %u unable to read CPR thread data from device tree, rc=%d\n",
891 thread->thread_id, rc);
892 return rc;
893 }
894
895 return 0;
896}
897
898/**
899 * cpr4_apss_parse_temp_adj_properties() - parse temperature based
900 * adjustment properties from device tree.
901 * @ctrl: Pointer to the CPR3 controller
902 *
903 * Return: 0 on success, errno on failure
904 */
905static int cpr4_apss_parse_temp_adj_properties(struct cpr3_controller *ctrl)
906{
907 struct device_node *of_node = ctrl->dev->of_node;
908 int rc, i, len, temp_point_count;
909
910 if (!of_find_property(of_node, "qcom,cpr-temp-point-map", &len)) {
911 /*
912 * Temperature based adjustments are not defined. Single
913 * temperature band is still valid for per-online-core
914 * adjustments.
915 */
916 ctrl->temp_band_count = 1;
917 return 0;
918 }
919
920 temp_point_count = len / sizeof(u32);
921 if (temp_point_count <= 0
922 || temp_point_count > MSM8953_APSS_MAX_TEMP_POINTS) {
923 cpr3_err(ctrl, "invalid number of temperature points %d > %d (max)\n",
924 temp_point_count, MSM8953_APSS_MAX_TEMP_POINTS);
925 return -EINVAL;
926 }
927
928 ctrl->temp_points = devm_kcalloc(ctrl->dev, temp_point_count,
929 sizeof(*ctrl->temp_points), GFP_KERNEL);
930 if (!ctrl->temp_points)
931 return -ENOMEM;
932
933 rc = of_property_read_u32_array(of_node, "qcom,cpr-temp-point-map",
934 ctrl->temp_points, temp_point_count);
935 if (rc) {
936 cpr3_err(ctrl, "error reading property qcom,cpr-temp-point-map, rc=%d\n",
937 rc);
938 return rc;
939 }
940
941 for (i = 0; i < temp_point_count; i++)
942 cpr3_debug(ctrl, "Temperature Point %d=%d\n", i,
943 ctrl->temp_points[i]);
944
945 /*
946 * If t1, t2, and t3 are the temperature points, then the temperature
947 * bands are: (-inf, t1], (t1, t2], (t2, t3], and (t3, inf).
948 */
949 ctrl->temp_band_count = temp_point_count + 1;
950 cpr3_debug(ctrl, "Number of temp bands =%d\n", ctrl->temp_band_count);
951
952 rc = of_property_read_u32(of_node, "qcom,cpr-initial-temp-band",
953 &ctrl->initial_temp_band);
954 if (rc) {
955 cpr3_err(ctrl, "error reading qcom,cpr-initial-temp-band, rc=%d\n",
956 rc);
957 return rc;
958 }
959
960 if (ctrl->initial_temp_band >= ctrl->temp_band_count) {
961 cpr3_err(ctrl, "Initial temperature band value %d should be in range [0 - %d]\n",
962 ctrl->initial_temp_band, ctrl->temp_band_count - 1);
963 return -EINVAL;
964 }
965
966 ctrl->temp_sensor_id_start = MSM8953_APSS_TEMP_SENSOR_ID_START;
967 ctrl->temp_sensor_id_end = MSM8953_APSS_TEMP_SENSOR_ID_END;
968 ctrl->allow_temp_adj = true;
969 return rc;
970}
971
972/**
973 * cpr4_apss_parse_boost_properties() - parse configuration data for boost
974 * voltage adjustment for CPR3 regulator from device tree.
975 * @vreg: Pointer to the CPR3 regulator
976 *
977 * Return: 0 on success, errno on failure
978 */
979static int cpr4_apss_parse_boost_properties(struct cpr3_regulator *vreg)
980{
981 struct cpr3_controller *ctrl = vreg->thread->ctrl;
982 struct cpr4_msm8953_apss_fuses *fuse = vreg->platform_fuses;
983 struct cpr3_corner *corner;
984 int i, boost_voltage, final_boost_volt, rc = 0;
985 int *boost_table = NULL, *boost_temp_adj = NULL;
986 int boost_voltage_adjust = 0, boost_num_cores = 0;
987 u32 boost_allowed = 0;
988
989 if (!boost_fuse[fuse->boost_cfg])
990 /* Voltage boost is disabled in fuse */
991 return 0;
992
993 if (of_find_property(vreg->of_node, "qcom,allow-boost", NULL)) {
994 rc = cpr3_parse_array_property(vreg, "qcom,allow-boost", 1,
995 &boost_allowed);
996 if (rc)
997 return rc;
998 }
999
1000 if (!boost_allowed) {
1001 /* Voltage boost is not enabled for this regulator */
1002 return 0;
1003 }
1004
1005 boost_voltage = cpr3_convert_open_loop_voltage_fuse(
1006 MSM8953_APSS_BOOST_FUSE_REF_VOLT,
1007 MSM8953_APSS_FUSE_STEP_VOLT,
1008 fuse->boost_voltage,
1009 MSM8953_APSS_VOLTAGE_FUSE_SIZE);
1010
1011 /* Log boost voltage value for debugging purposes. */
1012 cpr3_info(vreg, "Boost open-loop=%7d uV\n", boost_voltage);
1013
1014 if (of_find_property(vreg->of_node,
1015 "qcom,cpr-boost-voltage-fuse-adjustment", NULL)) {
1016 rc = cpr3_parse_array_property(vreg,
1017 "qcom,cpr-boost-voltage-fuse-adjustment",
1018 1, &boost_voltage_adjust);
1019 if (rc) {
1020 cpr3_err(vreg, "qcom,cpr-boost-voltage-fuse-adjustment reading failed, rc=%d\n",
1021 rc);
1022 return rc;
1023 }
1024
1025 boost_voltage += boost_voltage_adjust;
1026 /* Log boost voltage value for debugging purposes. */
1027 cpr3_info(vreg, "Adjusted boost open-loop=%7d uV\n",
1028 boost_voltage);
1029 }
1030
1031 /* Limit boost voltage value between ceiling and floor voltage limits */
1032 boost_voltage = min(boost_voltage, MSM8953_APSS_BOOST_CEILING_VOLT);
1033 boost_voltage = max(boost_voltage, MSM8953_APSS_BOOST_FLOOR_VOLT);
1034
1035 /*
1036 * The boost feature can only be used for the highest voltage corner.
1037 * Also, keep core-count adjustments disabled when the boost feature
1038 * is enabled.
1039 */
1040 corner = &vreg->corner[vreg->corner_count - 1];
1041 if (!corner->sdelta) {
1042 /*
1043 * If core-count/temp adjustments are not defined, the cpr4
1044 * sdelta for this corner will not be allocated. Allocate it
1045 * here for boost configuration.
1046 */
1047 corner->sdelta = devm_kzalloc(ctrl->dev,
1048 sizeof(*corner->sdelta), GFP_KERNEL);
1049 if (!corner->sdelta)
1050 return -ENOMEM;
1051 }
1052 corner->sdelta->temp_band_count = ctrl->temp_band_count;
1053
1054 rc = of_property_read_u32(vreg->of_node, "qcom,cpr-num-boost-cores",
1055 &boost_num_cores);
1056 if (rc) {
1057 cpr3_err(vreg, "qcom,cpr-num-boost-cores reading failed, rc=%d\n",
1058 rc);
1059 return rc;
1060 }
1061
1062 if (boost_num_cores <= 0
1063 || boost_num_cores > MSM8953_APSS_CPR_SDELTA_CORE_COUNT) {
1064 cpr3_err(vreg, "Invalid boost number of cores = %d\n",
1065 boost_num_cores);
1066 return -EINVAL;
1067 }
1068 corner->sdelta->boost_num_cores = boost_num_cores;
1069
1070 boost_table = devm_kcalloc(ctrl->dev, corner->sdelta->temp_band_count,
1071 sizeof(*boost_table), GFP_KERNEL);
1072 if (!boost_table)
1073 return -ENOMEM;
1074
1075 if (of_find_property(vreg->of_node,
1076 "qcom,cpr-boost-temp-adjustment", NULL)) {
1077 boost_temp_adj = kcalloc(corner->sdelta->temp_band_count,
1078 sizeof(*boost_temp_adj), GFP_KERNEL);
1079 if (!boost_temp_adj)
1080 return -ENOMEM;
1081
1082 rc = cpr3_parse_array_property(vreg,
1083 "qcom,cpr-boost-temp-adjustment",
1084 corner->sdelta->temp_band_count,
1085 boost_temp_adj);
1086 if (rc) {
1087 cpr3_err(vreg, "qcom,cpr-boost-temp-adjustment reading failed, rc=%d\n",
1088 rc);
1089 goto done;
1090 }
1091 }
1092
1093 for (i = 0; i < corner->sdelta->temp_band_count; i++) {
1094 /* Apply static adjustments to boost voltage */
1095 final_boost_volt = boost_voltage + (boost_temp_adj == NULL
1096 ? 0 : boost_temp_adj[i]);
1097 /*
1098 * Limit final adjusted boost voltage value between ceiling
1099 * and floor voltage limits
1100 */
1101 final_boost_volt = min(final_boost_volt,
1102 MSM8953_APSS_BOOST_CEILING_VOLT);
1103 final_boost_volt = max(final_boost_volt,
1104 MSM8953_APSS_BOOST_FLOOR_VOLT);
1105
1106 boost_table[i] = (corner->open_loop_volt - final_boost_volt)
1107 / ctrl->step_volt;
1108 cpr3_debug(vreg, "Adjusted boost voltage margin for temp band %d = %d steps\n",
1109 i, boost_table[i]);
1110 }
1111
1112 corner->ceiling_volt = MSM8953_APSS_BOOST_CEILING_VOLT;
1113 corner->sdelta->boost_table = boost_table;
1114 corner->sdelta->allow_boost = true;
1115 corner->sdelta->allow_core_count_adj = false;
1116 vreg->allow_boost = true;
1117 ctrl->allow_boost = true;
1118done:
1119 kfree(boost_temp_adj);
1120 return rc;
1121}
1122
1123/**
1124 * cpr4_apss_init_regulator() - perform all steps necessary to initialize the
1125 * configuration data for a CPR3 regulator
1126 * @vreg: Pointer to the CPR3 regulator
1127 *
1128 * Return: 0 on success, errno on failure
1129 */
1130static int cpr4_apss_init_regulator(struct cpr3_regulator *vreg)
1131{
1132 struct cpr4_msm8953_apss_fuses *fuse;
1133 int rc;
1134
1135 rc = cpr4_msm8953_apss_read_fuse_data(vreg);
1136 if (rc) {
1137 cpr3_err(vreg, "unable to read CPR fuse data, rc=%d\n", rc);
1138 return rc;
1139 }
1140
1141 fuse = vreg->platform_fuses;
1142
1143 rc = cpr4_apss_parse_corner_data(vreg);
1144 if (rc) {
1145 cpr3_err(vreg, "unable to read CPR corner data from device tree, rc=%d\n",
1146 rc);
1147 return rc;
1148 }
1149
1150 rc = cpr3_mem_acc_init(vreg);
1151 if (rc) {
1152 if (rc != -EPROBE_DEFER)
1153 cpr3_err(vreg, "unable to initialize mem-acc regulator settings, rc=%d\n",
1154 rc);
1155 return rc;
1156 }
1157
1158 rc = cpr4_msm8953_apss_calculate_open_loop_voltages(vreg);
1159 if (rc) {
1160 cpr3_err(vreg, "unable to calculate open-loop voltages, rc=%d\n",
1161 rc);
1162 return rc;
1163 }
1164
1165 rc = cpr3_limit_open_loop_voltages(vreg);
1166 if (rc) {
1167 cpr3_err(vreg, "unable to limit open-loop voltages, rc=%d\n",
1168 rc);
1169 return rc;
1170 }
1171
1172 cpr3_open_loop_voltage_as_ceiling(vreg);
1173
1174 rc = cpr3_limit_floor_voltages(vreg);
1175 if (rc) {
1176 cpr3_err(vreg, "unable to limit floor voltages, rc=%d\n", rc);
1177 return rc;
1178 }
1179
1180 rc = cpr4_msm8953_apss_calculate_target_quotients(vreg);
1181 if (rc) {
1182 cpr3_err(vreg, "unable to calculate target quotients, rc=%d\n",
1183 rc);
1184 return rc;
1185 }
1186
1187 rc = cpr4_parse_core_count_temp_voltage_adj(vreg, false);
1188 if (rc) {
1189 cpr3_err(vreg, "unable to parse temperature and core count voltage adjustments, rc=%d\n",
1190 rc);
1191 return rc;
1192 }
1193
1194 if (vreg->allow_core_count_adj && (vreg->max_core_count <= 0
1195 || vreg->max_core_count >
1196 MSM8953_APSS_CPR_SDELTA_CORE_COUNT)) {
1197 cpr3_err(vreg, "qcom,max-core-count has invalid value = %d\n",
1198 vreg->max_core_count);
1199 return -EINVAL;
1200 }
1201
1202 rc = cpr4_apss_parse_boost_properties(vreg);
1203 if (rc) {
1204 cpr3_err(vreg, "unable to parse boost adjustments, rc=%d\n",
1205 rc);
1206 return rc;
1207 }
1208
1209 cpr4_apss_print_settings(vreg);
1210
1211 return rc;
1212}
1213
1214/**
1215 * cpr4_apss_init_controller() - perform APSS CPR4 controller specific
1216 * initializations
1217 * @ctrl: Pointer to the CPR3 controller
1218 *
1219 * Return: 0 on success, errno on failure
1220 */
1221static int cpr4_apss_init_controller(struct cpr3_controller *ctrl)
1222{
1223 int rc;
1224
1225 rc = cpr3_parse_common_ctrl_data(ctrl);
1226 if (rc) {
1227 if (rc != -EPROBE_DEFER)
1228 cpr3_err(ctrl, "unable to parse common controller data, rc=%d\n",
1229 rc);
1230 return rc;
1231 }
1232
1233 rc = of_property_read_u32(ctrl->dev->of_node,
1234 "qcom,cpr-down-error-step-limit",
1235 &ctrl->down_error_step_limit);
1236 if (rc) {
1237 cpr3_err(ctrl, "error reading qcom,cpr-down-error-step-limit, rc=%d\n",
1238 rc);
1239 return rc;
1240 }
1241
1242 rc = of_property_read_u32(ctrl->dev->of_node,
1243 "qcom,cpr-up-error-step-limit",
1244 &ctrl->up_error_step_limit);
1245 if (rc) {
1246 cpr3_err(ctrl, "error reading qcom,cpr-up-error-step-limit, rc=%d\n",
1247 rc);
1248 return rc;
1249 }
1250
1251 /*
1252 * Use fixed step quotient if specified otherwise use dynamic
1253 * calculated per RO step quotient
1254 */
1255 of_property_read_u32(ctrl->dev->of_node, "qcom,cpr-step-quot-fixed",
1256 &ctrl->step_quot_fixed);
1257 ctrl->use_dynamic_step_quot = ctrl->step_quot_fixed ? false : true;
1258
1259 ctrl->saw_use_unit_mV = of_property_read_bool(ctrl->dev->of_node,
1260 "qcom,cpr-saw-use-unit-mV");
1261
1262 of_property_read_u32(ctrl->dev->of_node,
1263 "qcom,cpr-voltage-settling-time",
1264 &ctrl->voltage_settling_time);
1265
1266 ctrl->vdd_limit_regulator = devm_regulator_get(ctrl->dev, "vdd-limit");
1267 if (IS_ERR(ctrl->vdd_limit_regulator)) {
1268 rc = PTR_ERR(ctrl->vdd_limit_regulator);
1269 if (rc != -EPROBE_DEFER)
1270 cpr3_err(ctrl, "unable to request vdd-limit regulator, rc=%d\n",
1271 rc);
1272 return rc;
1273 }
1274
1275 rc = cpr3_apm_init(ctrl);
1276 if (rc) {
1277 if (rc != -EPROBE_DEFER)
1278 cpr3_err(ctrl, "unable to initialize APM settings, rc=%d\n",
1279 rc);
1280 return rc;
1281 }
1282
1283 rc = cpr4_apss_parse_temp_adj_properties(ctrl);
1284 if (rc) {
1285 cpr3_err(ctrl, "unable to parse temperature adjustment properties, rc=%d\n",
1286 rc);
1287 return rc;
1288 }
1289
1290 ctrl->sensor_count = MSM8953_APSS_CPR_SENSOR_COUNT;
1291
1292 /*
1293 * APSS only has one thread (0) per controller so the zeroed
1294 * array does not need further modification.
1295 */
1296 ctrl->sensor_owner = devm_kcalloc(ctrl->dev, ctrl->sensor_count,
1297 sizeof(*ctrl->sensor_owner), GFP_KERNEL);
1298 if (!ctrl->sensor_owner)
1299 return -ENOMEM;
1300
1301 ctrl->cpr_clock_rate = MSM8953_APSS_CPR_CLOCK_RATE;
1302 ctrl->ctrl_type = CPR_CTRL_TYPE_CPR4;
1303 ctrl->supports_hw_closed_loop = true;
1304 ctrl->use_hw_closed_loop = of_property_read_bool(ctrl->dev->of_node,
1305 "qcom,cpr-hw-closed-loop");
1306 return 0;
1307}
1308
1309static int cpr4_apss_regulator_suspend(struct platform_device *pdev,
1310 pm_message_t state)
1311{
1312 struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
1313
1314 return cpr3_regulator_suspend(ctrl);
1315}
1316
1317static int cpr4_apss_regulator_resume(struct platform_device *pdev)
1318{
1319 struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
1320
1321 return cpr3_regulator_resume(ctrl);
1322}
1323
1324static int cpr4_apss_regulator_probe(struct platform_device *pdev)
1325{
1326 struct device *dev = &pdev->dev;
1327 struct cpr3_controller *ctrl;
1328 int i, rc;
1329
1330 if (!dev->of_node) {
1331 dev_err(dev, "Device tree node is missing\n");
1332 return -EINVAL;
1333 }
1334
1335 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
1336 if (!ctrl)
1337 return -ENOMEM;
1338
1339 ctrl->dev = dev;
1340 /* Set to false later if anything precludes CPR operation. */
1341 ctrl->cpr_allowed_hw = true;
1342
1343 rc = of_property_read_string(dev->of_node, "qcom,cpr-ctrl-name",
1344 &ctrl->name);
1345 if (rc) {
1346 cpr3_err(ctrl, "unable to read qcom,cpr-ctrl-name, rc=%d\n",
1347 rc);
1348 return rc;
1349 }
1350
1351 rc = cpr3_map_fuse_base(ctrl, pdev);
1352 if (rc) {
1353 cpr3_err(ctrl, "could not map fuse base address\n");
1354 return rc;
1355 }
1356
1357 rc = cpr3_allocate_threads(ctrl, 0, 0);
1358 if (rc) {
1359 cpr3_err(ctrl, "failed to allocate CPR thread array, rc=%d\n",
1360 rc);
1361 return rc;
1362 }
1363
1364 if (ctrl->thread_count != 1) {
1365 cpr3_err(ctrl, "expected 1 thread but found %d\n",
1366 ctrl->thread_count);
1367 return -EINVAL;
1368 }
1369
1370 rc = cpr4_apss_init_controller(ctrl);
1371 if (rc) {
1372 if (rc != -EPROBE_DEFER)
1373 cpr3_err(ctrl, "failed to initialize CPR controller parameters, rc=%d\n",
1374 rc);
1375 return rc;
1376 }
1377
1378 rc = cpr4_apss_init_thread(&ctrl->thread[0]);
1379 if (rc) {
1380 cpr3_err(ctrl, "thread initialization failed, rc=%d\n", rc);
1381 return rc;
1382 }
1383
1384 for (i = 0; i < ctrl->thread[0].vreg_count; i++) {
1385 rc = cpr4_apss_init_regulator(&ctrl->thread[0].vreg[i]);
1386 if (rc) {
1387 cpr3_err(&ctrl->thread[0].vreg[i], "regulator initialization failed, rc=%d\n",
1388 rc);
1389 return rc;
1390 }
1391 }
1392
1393 platform_set_drvdata(pdev, ctrl);
1394
1395 return cpr3_regulator_register(pdev, ctrl);
1396}
1397
1398static int cpr4_apss_regulator_remove(struct platform_device *pdev)
1399{
1400 struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
1401
1402 return cpr3_regulator_unregister(ctrl);
1403}
1404
1405static const struct of_device_id cpr4_regulator_match_table[] = {
1406 { .compatible = "qcom,cpr4-msm8953-apss-regulator", },
1407 {}
1408};
1409
1410static struct platform_driver cpr4_apss_regulator_driver = {
1411 .driver = {
1412 .name = "qcom,cpr4-apss-regulator",
1413 .of_match_table = cpr4_regulator_match_table,
1414 .owner = THIS_MODULE,
1415 },
1416 .probe = cpr4_apss_regulator_probe,
1417 .remove = cpr4_apss_regulator_remove,
1418 .suspend = cpr4_apss_regulator_suspend,
1419 .resume = cpr4_apss_regulator_resume,
1420};
1421
1422static int cpr4_regulator_init(void)
1423{
1424 return platform_driver_register(&cpr4_apss_regulator_driver);
1425}
1426
1427static void cpr4_regulator_exit(void)
1428{
1429 platform_driver_unregister(&cpr4_apss_regulator_driver);
1430}
1431
1432MODULE_DESCRIPTION("CPR4 APSS regulator driver");
1433MODULE_LICENSE("GPL v2");
1434
1435arch_initcall(cpr4_regulator_init);
1436module_exit(cpr4_regulator_exit);