David Collins | 7370f1a | 2017-01-18 16:21:53 -0800 | [diff] [blame] | 1 | /* |
| 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 MSM8996_MMSS_FUSE_CORNERS 4 |
| 39 | |
| 40 | /** |
| 41 | * struct cpr3_msm8996_mmss_fuses - MMSS specific fuse data for MSM8996 |
| 42 | * @init_voltage: Initial (i.e. open-loop) voltage fuse parameter value |
| 43 | * for each fuse corner (raw, not converted to a voltage) |
| 44 | * @offset_voltage: The closed-loop voltage margin adjustment fuse parameter |
| 45 | * value for each fuse corner (raw, not converted to a |
| 46 | * voltage) |
| 47 | * @speed_bin: Graphics processor speed bin fuse parameter value for |
| 48 | * the given chip |
| 49 | * @cpr_fusing_rev: CPR fusing revision fuse parameter value |
| 50 | * @limitation: CPR limitation select fuse parameter value |
| 51 | * @aging_init_quot_diff: Initial quotient difference between CPR aging |
| 52 | * min and max sensors measured at time of manufacturing |
| 53 | * @force_highest_corner: Flag indicating that all corners must operate |
| 54 | * at the voltage of the highest corner. This is |
| 55 | * applicable to MSM8998 only. |
| 56 | * |
| 57 | * This struct holds the values for all of the fuses read from memory. |
| 58 | */ |
| 59 | struct cpr3_msm8996_mmss_fuses { |
| 60 | u64 init_voltage[MSM8996_MMSS_FUSE_CORNERS]; |
| 61 | u64 offset_voltage[MSM8996_MMSS_FUSE_CORNERS]; |
| 62 | u64 speed_bin; |
| 63 | u64 cpr_fusing_rev; |
| 64 | u64 limitation; |
| 65 | u64 aging_init_quot_diff; |
| 66 | u64 force_highest_corner; |
| 67 | }; |
| 68 | |
| 69 | /* Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 */ |
| 70 | #define CPR3_MSM8996_MMSS_FUSE_COMBO_COUNT 8 |
| 71 | |
| 72 | /* |
| 73 | * Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 with speed bin fuse = 0. |
| 74 | * Fuse combos 8 - 15 map to CPR fusing revision 0 - 7 with speed bin fuse = 1. |
| 75 | */ |
| 76 | #define CPR3_MSM8996PRO_MMSS_FUSE_COMBO_COUNT 16 |
| 77 | |
| 78 | /* Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 */ |
| 79 | #define CPR3_MSM8998_MMSS_FUSE_COMBO_COUNT 8 |
| 80 | |
| 81 | /* |
| 82 | * MSM8996 MMSS fuse parameter locations: |
| 83 | * |
| 84 | * Structs are organized with the following dimensions: |
| 85 | * Outer: 0 to 3 for fuse corners from lowest to highest corner |
| 86 | * Inner: large enough to hold the longest set of parameter segments which |
| 87 | * fully defines a fuse parameter, +1 (for NULL termination). |
| 88 | * Each segment corresponds to a contiguous group of bits from a |
| 89 | * single fuse row. These segments are concatentated together in |
| 90 | * order to form the full fuse parameter value. The segments for |
| 91 | * a given parameter may correspond to different fuse rows. |
| 92 | */ |
| 93 | static const struct cpr3_fuse_param |
| 94 | msm8996_mmss_init_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = { |
| 95 | {{63, 55, 59}, {} }, |
| 96 | {{63, 50, 54}, {} }, |
| 97 | {{63, 45, 49}, {} }, |
| 98 | {{63, 40, 44}, {} }, |
| 99 | }; |
| 100 | |
| 101 | static const struct cpr3_fuse_param msm8996_cpr_fusing_rev_param[] = { |
| 102 | {39, 48, 50}, |
| 103 | {}, |
| 104 | }; |
| 105 | |
| 106 | static const struct cpr3_fuse_param msm8996_cpr_limitation_param[] = { |
| 107 | {41, 31, 32}, |
| 108 | {}, |
| 109 | }; |
| 110 | |
| 111 | static const struct cpr3_fuse_param |
| 112 | msm8996_mmss_aging_init_quot_diff_param[] = { |
| 113 | {68, 26, 31}, |
| 114 | {}, |
| 115 | }; |
| 116 | |
| 117 | /* Offset voltages are defined for SVS and Turbo fuse corners only */ |
| 118 | static const struct cpr3_fuse_param |
| 119 | msm8996_mmss_offset_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = { |
| 120 | {{} }, |
| 121 | {{66, 42, 44}, {} }, |
| 122 | {{} }, |
| 123 | {{64, 58, 61}, {} }, |
| 124 | }; |
| 125 | |
| 126 | static const struct cpr3_fuse_param msm8996pro_mmss_speed_bin_param[] = { |
| 127 | {39, 60, 61}, |
| 128 | {}, |
| 129 | }; |
| 130 | |
| 131 | /* MSM8998 MMSS fuse parameter locations: */ |
| 132 | static const struct cpr3_fuse_param |
| 133 | msm8998_mmss_init_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = { |
| 134 | {{65, 39, 43}, {} }, |
| 135 | {{65, 34, 38}, {} }, |
| 136 | {{65, 29, 33}, {} }, |
| 137 | {{65, 24, 28}, {} }, |
| 138 | }; |
| 139 | |
| 140 | static const struct cpr3_fuse_param msm8998_cpr_fusing_rev_param[] = { |
| 141 | {39, 48, 50}, |
| 142 | {}, |
| 143 | }; |
| 144 | |
| 145 | static const struct cpr3_fuse_param msm8998_cpr_limitation_param[] = { |
| 146 | {41, 46, 47}, |
| 147 | {}, |
| 148 | }; |
| 149 | |
| 150 | static const struct cpr3_fuse_param |
| 151 | msm8998_mmss_aging_init_quot_diff_param[] = { |
| 152 | {65, 60, 63}, |
| 153 | {66, 0, 3}, |
| 154 | {}, |
| 155 | }; |
| 156 | |
| 157 | static const struct cpr3_fuse_param |
| 158 | msm8998_mmss_offset_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = { |
| 159 | {{65, 56, 59}, {} }, |
| 160 | {{65, 52, 55}, {} }, |
| 161 | {{65, 48, 51}, {} }, |
| 162 | {{65, 44, 47}, {} }, |
| 163 | }; |
| 164 | |
| 165 | static const struct cpr3_fuse_param |
| 166 | msm8998_cpr_force_highest_corner_param[] = { |
| 167 | {100, 45, 45}, |
| 168 | {}, |
| 169 | }; |
| 170 | |
| 171 | #define MSM8996PRO_SOC_ID 4 |
| 172 | #define MSM8998_V1_SOC_ID 5 |
| 173 | #define MSM8998_V2_SOC_ID 6 |
| 174 | |
| 175 | /* |
| 176 | * Some initial msm8996 parts cannot be used in a meaningful way by software. |
| 177 | * Other parts can only be used when operating with CPR disabled (i.e. at the |
| 178 | * fused open-loop voltage) when no voltage interpolation is applied. A fuse |
| 179 | * parameter is provided so that software can properly handle these limitations. |
| 180 | */ |
| 181 | enum msm8996_cpr_limitation { |
| 182 | MSM8996_CPR_LIMITATION_NONE = 0, |
| 183 | MSM8996_CPR_LIMITATION_UNSUPPORTED = 2, |
| 184 | MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION = 3, |
| 185 | }; |
| 186 | |
| 187 | /* Additional MSM8996 specific data: */ |
| 188 | |
| 189 | /* Open loop voltage fuse reference voltages in microvolts */ |
| 190 | static const int msm8996_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = { |
| 191 | 670000, |
| 192 | 745000, |
| 193 | 905000, |
| 194 | 1015000, |
| 195 | }; |
| 196 | |
| 197 | static const int msm8996pro_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = { |
| 198 | 670000, |
| 199 | 745000, |
| 200 | 905000, |
| 201 | 1065000, |
| 202 | }; |
| 203 | |
| 204 | static const int msm8998_v1_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = { |
| 205 | 528000, |
| 206 | 656000, |
| 207 | 812000, |
| 208 | 932000, |
| 209 | }; |
| 210 | |
| 211 | static const int |
| 212 | msm8998_v1_rev0_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = { |
| 213 | 632000, |
| 214 | 768000, |
| 215 | 896000, |
| 216 | 1032000, |
| 217 | }; |
| 218 | |
| 219 | static const int msm8998_v2_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = { |
| 220 | 516000, |
| 221 | 628000, |
| 222 | 752000, |
| 223 | 924000, |
| 224 | }; |
| 225 | |
| 226 | static const int |
| 227 | msm8998_v2_rev0_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = { |
| 228 | 616000, |
| 229 | 740000, |
| 230 | 828000, |
| 231 | 1024000, |
| 232 | }; |
| 233 | |
| 234 | #define MSM8996_MMSS_FUSE_STEP_VOLT 10000 |
| 235 | #define MSM8996_MMSS_OFFSET_FUSE_STEP_VOLT 10000 |
| 236 | #define MSM8996_MMSS_VOLTAGE_FUSE_SIZE 5 |
| 237 | #define MSM8996_MMSS_MIN_VOLTAGE_FUSE_VAL 0x1F |
| 238 | #define MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SCALE 2 |
| 239 | #define MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SIZE 6 |
| 240 | |
| 241 | #define MSM8996_MMSS_CPR_SENSOR_COUNT 35 |
| 242 | |
| 243 | #define MSM8996_MMSS_CPR_CLOCK_RATE 19200000 |
| 244 | |
| 245 | #define MSM8996_MMSS_AGING_SENSOR_ID 29 |
| 246 | #define MSM8996_MMSS_AGING_BYPASS_MASK0 (GENMASK(23, 0)) |
| 247 | |
| 248 | #define MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SCALE 1 |
| 249 | #define MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SIZE 8 |
| 250 | |
| 251 | #define MSM8998_MMSS_CPR_SENSOR_COUNT 35 |
| 252 | |
| 253 | #define MSM8998_MMSS_AGING_SENSOR_ID 29 |
| 254 | #define MSM8998_MMSS_AGING_BYPASS_MASK0 (GENMASK(23, 0)) |
| 255 | |
| 256 | #define MSM8998_MMSS_MAX_TEMP_POINTS 3 |
| 257 | #define MSM8998_MMSS_TEMP_SENSOR_ID_START 12 |
| 258 | #define MSM8998_MMSS_TEMP_SENSOR_ID_END 13 |
| 259 | |
| 260 | /* |
| 261 | * Some initial msm8998 parts cannot be operated at low voltages. The |
| 262 | * open-loop voltage fuses are reused to identify these parts so that software |
| 263 | * can properly handle the limitation. 0xF means that the next higher fuse |
| 264 | * corner should be used. 0xE means that the next higher fuse corner which |
| 265 | * does not have a voltage limitation should be used. |
| 266 | */ |
| 267 | enum msm8998_cpr_partial_binning { |
| 268 | MSM8998_CPR_PARTIAL_BINNING_NEXT_CORNER = 0xF, |
| 269 | MSM8998_CPR_PARTIAL_BINNING_SAFE_CORNER = 0xE, |
| 270 | }; |
| 271 | |
| 272 | /* |
| 273 | * The partial binning open-loop voltage fuse values only apply to the lowest |
| 274 | * two fuse corners (0 and 1, i.e. MinSVS and SVS). |
| 275 | */ |
| 276 | #define MSM8998_CPR_PARTIAL_BINNING_MAX_FUSE_CORNER 1 |
| 277 | |
| 278 | static inline bool cpr3_ctrl_is_msm8998(const struct cpr3_controller *ctrl) |
| 279 | { |
| 280 | return ctrl->soc_revision == MSM8998_V1_SOC_ID || |
| 281 | ctrl->soc_revision == MSM8998_V2_SOC_ID; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * cpr3_msm8996_mmss_read_fuse_data() - load MMSS specific fuse parameter values |
| 286 | * @vreg: Pointer to the CPR3 regulator |
| 287 | * |
| 288 | * This function allocates a cpr3_msm8996_mmss_fuses struct, fills it with |
| 289 | * values read out of hardware fuses, and finally copies common fuse values |
| 290 | * into the regulator struct. |
| 291 | * |
| 292 | * Return: 0 on success, errno on failure |
| 293 | */ |
| 294 | static int cpr3_msm8996_mmss_read_fuse_data(struct cpr3_regulator *vreg) |
| 295 | { |
| 296 | void __iomem *base = vreg->thread->ctrl->fuse_base; |
| 297 | struct cpr3_msm8996_mmss_fuses *fuse; |
| 298 | int i, rc, combo_max; |
| 299 | |
| 300 | fuse = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*fuse), GFP_KERNEL); |
| 301 | if (!fuse) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | if (vreg->thread->ctrl->soc_revision == MSM8996PRO_SOC_ID) { |
| 305 | rc = cpr3_read_fuse_param(base, msm8996pro_mmss_speed_bin_param, |
| 306 | &fuse->speed_bin); |
| 307 | if (rc) { |
| 308 | cpr3_err(vreg, "Unable to read speed bin fuse, rc=%d\n", |
| 309 | rc); |
| 310 | return rc; |
| 311 | } |
| 312 | cpr3_info(vreg, "speed bin = %llu\n", fuse->speed_bin); |
| 313 | } |
| 314 | |
| 315 | rc = cpr3_read_fuse_param(base, |
| 316 | cpr3_ctrl_is_msm8998(vreg->thread->ctrl) |
| 317 | ? msm8998_cpr_fusing_rev_param |
| 318 | : msm8996_cpr_fusing_rev_param, |
| 319 | &fuse->cpr_fusing_rev); |
| 320 | if (rc) { |
| 321 | cpr3_err(vreg, "Unable to read CPR fusing revision fuse, rc=%d\n", |
| 322 | rc); |
| 323 | return rc; |
| 324 | } |
| 325 | cpr3_info(vreg, "CPR fusing revision = %llu\n", fuse->cpr_fusing_rev); |
| 326 | |
| 327 | rc = cpr3_read_fuse_param(base, |
| 328 | cpr3_ctrl_is_msm8998(vreg->thread->ctrl) |
| 329 | ? msm8998_cpr_limitation_param |
| 330 | : msm8996_cpr_limitation_param, |
| 331 | &fuse->limitation); |
| 332 | if (rc) { |
| 333 | cpr3_err(vreg, "Unable to read CPR limitation fuse, rc=%d\n", |
| 334 | rc); |
| 335 | return rc; |
| 336 | } |
| 337 | cpr3_info(vreg, "CPR limitation = %s\n", |
| 338 | fuse->limitation == MSM8996_CPR_LIMITATION_UNSUPPORTED |
| 339 | ? "unsupported chip" : fuse->limitation |
| 340 | == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION |
| 341 | ? "CPR disabled and no interpolation" : "none"); |
| 342 | |
| 343 | rc = cpr3_read_fuse_param(base, |
| 344 | cpr3_ctrl_is_msm8998(vreg->thread->ctrl) |
| 345 | ? msm8998_mmss_aging_init_quot_diff_param |
| 346 | : msm8996_mmss_aging_init_quot_diff_param, |
| 347 | &fuse->aging_init_quot_diff); |
| 348 | if (rc) { |
| 349 | cpr3_err(vreg, "Unable to read aging initial quotient difference fuse, rc=%d\n", |
| 350 | rc); |
| 351 | return rc; |
| 352 | } |
| 353 | |
| 354 | for (i = 0; i < MSM8996_MMSS_FUSE_CORNERS; i++) { |
| 355 | rc = cpr3_read_fuse_param(base, |
| 356 | cpr3_ctrl_is_msm8998(vreg->thread->ctrl) |
| 357 | ? msm8998_mmss_init_voltage_param[i] |
| 358 | : msm8996_mmss_init_voltage_param[i], |
| 359 | &fuse->init_voltage[i]); |
| 360 | if (rc) { |
| 361 | cpr3_err(vreg, "Unable to read fuse-corner %d initial voltage fuse, rc=%d\n", |
| 362 | i, rc); |
| 363 | return rc; |
| 364 | } |
| 365 | |
| 366 | rc = cpr3_read_fuse_param(base, |
| 367 | cpr3_ctrl_is_msm8998(vreg->thread->ctrl) |
| 368 | ? msm8998_mmss_offset_voltage_param[i] |
| 369 | : msm8996_mmss_offset_voltage_param[i], |
| 370 | &fuse->offset_voltage[i]); |
| 371 | if (rc) { |
| 372 | cpr3_err(vreg, "Unable to read fuse-corner %d offset voltage fuse, rc=%d\n", |
| 373 | i, rc); |
| 374 | return rc; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (cpr3_ctrl_is_msm8998(vreg->thread->ctrl)) { |
| 379 | rc = cpr3_read_fuse_param(base, |
| 380 | msm8998_cpr_force_highest_corner_param, |
| 381 | &fuse->force_highest_corner); |
| 382 | if (rc) { |
| 383 | cpr3_err(vreg, "Unable to read CPR force highest corner fuse, rc=%d\n", |
| 384 | rc); |
| 385 | return rc; |
| 386 | } |
| 387 | if (fuse->force_highest_corner) |
| 388 | cpr3_info(vreg, "Fusing requires all operation at the highest corner\n"); |
| 389 | } |
| 390 | |
| 391 | if (cpr3_ctrl_is_msm8998(vreg->thread->ctrl)) { |
| 392 | combo_max = CPR3_MSM8998_MMSS_FUSE_COMBO_COUNT; |
| 393 | vreg->fuse_combo = fuse->cpr_fusing_rev; |
| 394 | } else if (vreg->thread->ctrl->soc_revision == MSM8996PRO_SOC_ID) { |
| 395 | combo_max = CPR3_MSM8996PRO_MMSS_FUSE_COMBO_COUNT; |
| 396 | vreg->fuse_combo = fuse->cpr_fusing_rev + 8 * fuse->speed_bin; |
| 397 | } else { |
| 398 | combo_max = CPR3_MSM8996_MMSS_FUSE_COMBO_COUNT; |
| 399 | vreg->fuse_combo = fuse->cpr_fusing_rev; |
| 400 | } |
| 401 | |
| 402 | if (vreg->fuse_combo >= combo_max) { |
| 403 | cpr3_err(vreg, "invalid CPR fuse combo = %d found, not in range 0 - %d\n", |
| 404 | vreg->fuse_combo, combo_max - 1); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | |
| 408 | vreg->speed_bin_fuse = fuse->speed_bin; |
| 409 | vreg->cpr_rev_fuse = fuse->cpr_fusing_rev; |
| 410 | vreg->fuse_corner_count = MSM8996_MMSS_FUSE_CORNERS; |
| 411 | vreg->platform_fuses = fuse; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * cpr3_mmss_parse_corner_data() - parse MMSS corner data from device tree |
| 418 | * properties of the regulator's device node |
| 419 | * @vreg: Pointer to the CPR3 regulator |
| 420 | * |
| 421 | * Return: 0 on success, errno on failure |
| 422 | */ |
| 423 | static int cpr3_mmss_parse_corner_data(struct cpr3_regulator *vreg) |
| 424 | { |
| 425 | int i, rc; |
| 426 | u32 *temp; |
| 427 | |
| 428 | rc = cpr3_parse_common_corner_data(vreg); |
| 429 | if (rc) { |
| 430 | cpr3_err(vreg, "error reading corner data, rc=%d\n", rc); |
| 431 | return rc; |
| 432 | } |
| 433 | |
| 434 | temp = kcalloc(vreg->corner_count * CPR3_RO_COUNT, sizeof(*temp), |
| 435 | GFP_KERNEL); |
| 436 | if (!temp) |
| 437 | return -ENOMEM; |
| 438 | |
| 439 | rc = cpr3_parse_corner_array_property(vreg, "qcom,cpr-target-quotients", |
| 440 | CPR3_RO_COUNT, temp); |
| 441 | if (rc) { |
| 442 | cpr3_err(vreg, "could not load target quotients, rc=%d\n", rc); |
| 443 | goto done; |
| 444 | } |
| 445 | |
| 446 | for (i = 0; i < vreg->corner_count; i++) |
| 447 | memcpy(vreg->corner[i].target_quot, &temp[i * CPR3_RO_COUNT], |
| 448 | sizeof(*temp) * CPR3_RO_COUNT); |
| 449 | |
| 450 | done: |
| 451 | kfree(temp); |
| 452 | return rc; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * cpr3_msm8996_mmss_adjust_target_quotients() - adjust the target quotients |
| 457 | * for each corner according to device tree values and fuse values |
| 458 | * @vreg: Pointer to the CPR3 regulator |
| 459 | * |
| 460 | * Return: 0 on success, errno on failure |
| 461 | */ |
| 462 | static int cpr3_msm8996_mmss_adjust_target_quotients( |
| 463 | struct cpr3_regulator *vreg) |
| 464 | { |
| 465 | struct cpr3_msm8996_mmss_fuses *fuse = vreg->platform_fuses; |
| 466 | const struct cpr3_fuse_param (*offset_param)[2]; |
| 467 | int *volt_offset; |
| 468 | int i, fuse_len, rc = 0; |
| 469 | |
| 470 | volt_offset = kcalloc(vreg->fuse_corner_count, sizeof(*volt_offset), |
| 471 | GFP_KERNEL); |
| 472 | if (!volt_offset) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | offset_param = cpr3_ctrl_is_msm8998(vreg->thread->ctrl) |
| 476 | ? msm8998_mmss_offset_voltage_param |
| 477 | : msm8996_mmss_offset_voltage_param; |
| 478 | for (i = 0; i < vreg->fuse_corner_count; i++) { |
| 479 | fuse_len = offset_param[i][0].bit_end + 1 |
| 480 | - offset_param[i][0].bit_start; |
| 481 | volt_offset[i] = cpr3_convert_open_loop_voltage_fuse( |
| 482 | 0, MSM8996_MMSS_OFFSET_FUSE_STEP_VOLT, |
| 483 | fuse->offset_voltage[i], fuse_len); |
| 484 | if (volt_offset[i]) |
| 485 | cpr3_info(vreg, "fuse_corner[%d] offset=%7d uV\n", |
| 486 | i, volt_offset[i]); |
| 487 | } |
| 488 | |
| 489 | rc = cpr3_adjust_target_quotients(vreg, volt_offset); |
| 490 | if (rc) |
| 491 | cpr3_err(vreg, "adjust target quotients failed, rc=%d\n", rc); |
| 492 | |
| 493 | kfree(volt_offset); |
| 494 | return rc; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * cpr3_msm8996_mmss_calculate_open_loop_voltages() - calculate the open-loop |
| 499 | * voltage for each corner of a CPR3 regulator |
| 500 | * @vreg: Pointer to the CPR3 regulator |
| 501 | * |
| 502 | * If open-loop voltage interpolation is allowed in both device tree and in |
| 503 | * hardware fuses, then this function calculates the open-loop voltage for a |
| 504 | * given corner using linear interpolation. This interpolation is performed |
| 505 | * using the processor frequencies of the lower and higher Fmax corners along |
| 506 | * with their fused open-loop voltages. |
| 507 | * |
| 508 | * If open-loop voltage interpolation is not allowed, then this function uses |
| 509 | * the Fmax fused open-loop voltage for all of the corners associated with a |
| 510 | * given fuse corner. |
| 511 | * |
| 512 | * Return: 0 on success, errno on failure |
| 513 | */ |
| 514 | static int cpr3_msm8996_mmss_calculate_open_loop_voltages( |
| 515 | struct cpr3_regulator *vreg) |
| 516 | { |
| 517 | struct device_node *node = vreg->of_node; |
| 518 | struct cpr3_msm8996_mmss_fuses *fuse = vreg->platform_fuses; |
| 519 | bool is_msm8998 = cpr3_ctrl_is_msm8998(vreg->thread->ctrl); |
| 520 | int rc = 0; |
| 521 | bool allow_interpolation; |
| 522 | u64 freq_low, volt_low, freq_high, volt_high, volt_init; |
| 523 | int i, j; |
| 524 | const int *ref_volt; |
| 525 | int *fuse_volt; |
| 526 | int *fmax_corner; |
| 527 | |
| 528 | fuse_volt = kcalloc(vreg->fuse_corner_count, sizeof(*fuse_volt), |
| 529 | GFP_KERNEL); |
| 530 | fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner), |
| 531 | GFP_KERNEL); |
| 532 | if (!fuse_volt || !fmax_corner) { |
| 533 | rc = -ENOMEM; |
| 534 | goto done; |
| 535 | } |
| 536 | |
| 537 | if (vreg->thread->ctrl->soc_revision == MSM8998_V2_SOC_ID |
| 538 | && fuse->cpr_fusing_rev == 0) |
| 539 | ref_volt = msm8998_v2_rev0_mmss_fuse_ref_volt; |
| 540 | else if (vreg->thread->ctrl->soc_revision == MSM8998_V2_SOC_ID) |
| 541 | ref_volt = msm8998_v2_mmss_fuse_ref_volt; |
| 542 | else if (vreg->thread->ctrl->soc_revision == MSM8998_V1_SOC_ID |
| 543 | && fuse->cpr_fusing_rev == 0) |
| 544 | ref_volt = msm8998_v1_rev0_mmss_fuse_ref_volt; |
| 545 | else if (vreg->thread->ctrl->soc_revision == MSM8998_V1_SOC_ID) |
| 546 | ref_volt = msm8998_v1_mmss_fuse_ref_volt; |
| 547 | else if (vreg->thread->ctrl->soc_revision == MSM8996PRO_SOC_ID) |
| 548 | ref_volt = msm8996pro_mmss_fuse_ref_volt; |
| 549 | else |
| 550 | ref_volt = msm8996_mmss_fuse_ref_volt; |
| 551 | |
| 552 | for (i = 0; i < vreg->fuse_corner_count; i++) { |
| 553 | volt_init = fuse->init_voltage[i]; |
| 554 | /* |
| 555 | * Handle partial binning on MSM8998 where the initial voltage |
| 556 | * fuse is reused as a flag for partial binning needs. Set the |
| 557 | * open-loop voltage to the minimum possible value so that it |
| 558 | * does not result in higher fuse corners getting forced to |
| 559 | * higher open-loop voltages after monotonicity enforcement. |
| 560 | */ |
| 561 | if (is_msm8998 && |
| 562 | (volt_init == MSM8998_CPR_PARTIAL_BINNING_NEXT_CORNER || |
| 563 | volt_init == MSM8998_CPR_PARTIAL_BINNING_SAFE_CORNER) && |
| 564 | i <= MSM8998_CPR_PARTIAL_BINNING_MAX_FUSE_CORNER) |
| 565 | volt_init = MSM8996_MMSS_MIN_VOLTAGE_FUSE_VAL; |
| 566 | |
| 567 | fuse_volt[i] = cpr3_convert_open_loop_voltage_fuse(ref_volt[i], |
| 568 | MSM8996_MMSS_FUSE_STEP_VOLT, volt_init, |
| 569 | MSM8996_MMSS_VOLTAGE_FUSE_SIZE); |
| 570 | cpr3_info(vreg, "fuse_corner[%d] open-loop=%7d uV\n", |
| 571 | i, fuse_volt[i]); |
| 572 | } |
| 573 | |
| 574 | rc = cpr3_adjust_fused_open_loop_voltages(vreg, fuse_volt); |
| 575 | if (rc) { |
| 576 | cpr3_err(vreg, "fused open-loop voltage adjustment failed, rc=%d\n", |
| 577 | rc); |
| 578 | goto done; |
| 579 | } |
| 580 | |
| 581 | allow_interpolation = of_property_read_bool(node, |
| 582 | "qcom,allow-voltage-interpolation"); |
| 583 | |
| 584 | for (i = 1; i < vreg->fuse_corner_count; i++) { |
| 585 | if (fuse_volt[i] < fuse_volt[i - 1]) { |
| 586 | cpr3_debug(vreg, "fuse corner %d voltage=%d uV < fuse corner %d voltage=%d uV; overriding: fuse corner %d voltage=%d\n", |
| 587 | i, fuse_volt[i], i - 1, fuse_volt[i - 1], |
| 588 | i, fuse_volt[i - 1]); |
| 589 | fuse_volt[i] = fuse_volt[i - 1]; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if (fuse->limitation == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION) |
| 594 | allow_interpolation = false; |
| 595 | |
| 596 | if (!allow_interpolation) { |
| 597 | /* Use fused open-loop voltage for lower frequencies. */ |
| 598 | for (i = 0; i < vreg->corner_count; i++) |
| 599 | vreg->corner[i].open_loop_volt |
| 600 | = fuse_volt[vreg->corner[i].cpr_fuse_corner]; |
| 601 | goto done; |
| 602 | } |
| 603 | |
| 604 | /* Determine highest corner mapped to each fuse corner */ |
| 605 | j = vreg->fuse_corner_count - 1; |
| 606 | for (i = vreg->corner_count - 1; i >= 0; i--) { |
| 607 | if (vreg->corner[i].cpr_fuse_corner == j) { |
| 608 | fmax_corner[j] = i; |
| 609 | j--; |
| 610 | } |
| 611 | } |
| 612 | if (j >= 0) { |
| 613 | cpr3_err(vreg, "invalid fuse corner mapping\n"); |
| 614 | rc = -EINVAL; |
| 615 | goto done; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Interpolation is not possible for corners mapped to the lowest fuse |
| 620 | * corner so use the fuse corner value directly. |
| 621 | */ |
| 622 | for (i = 0; i <= fmax_corner[0]; i++) |
| 623 | vreg->corner[i].open_loop_volt = fuse_volt[0]; |
| 624 | |
| 625 | /* Interpolate voltages for the higher fuse corners. */ |
| 626 | for (i = 1; i < vreg->fuse_corner_count; i++) { |
| 627 | freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq; |
| 628 | volt_low = fuse_volt[i - 1]; |
| 629 | freq_high = vreg->corner[fmax_corner[i]].proc_freq; |
| 630 | volt_high = fuse_volt[i]; |
| 631 | |
| 632 | for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++) |
| 633 | vreg->corner[j].open_loop_volt = cpr3_interpolate( |
| 634 | freq_low, volt_low, freq_high, volt_high, |
| 635 | vreg->corner[j].proc_freq); |
| 636 | } |
| 637 | |
| 638 | done: |
| 639 | if (rc == 0) { |
| 640 | cpr3_debug(vreg, "unadjusted per-corner open-loop voltages:\n"); |
| 641 | for (i = 0; i < vreg->corner_count; i++) |
| 642 | cpr3_debug(vreg, "open-loop[%2d] = %d uV\n", i, |
| 643 | vreg->corner[i].open_loop_volt); |
| 644 | |
| 645 | rc = cpr3_adjust_open_loop_voltages(vreg); |
| 646 | if (rc) |
| 647 | cpr3_err(vreg, "open-loop voltage adjustment failed, rc=%d\n", |
| 648 | rc); |
| 649 | } |
| 650 | |
| 651 | kfree(fuse_volt); |
| 652 | kfree(fmax_corner); |
| 653 | return rc; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * cpr3_msm8998_partial_binning_override() - override the voltage and quotient |
| 658 | * settings for low corners based upon the special partial binning |
| 659 | * open-loop voltage fuse values |
| 660 | * @vreg: Pointer to the CPR3 regulator |
| 661 | * |
| 662 | * Some parts are not able to operate at low voltages. The partial binning |
| 663 | * open-loop voltage fuse values specify if a given part has such limitations. |
| 664 | * |
| 665 | * Return: 0 on success, errno on failure |
| 666 | */ |
| 667 | static int cpr3_msm8998_partial_binning_override(struct cpr3_regulator *vreg) |
| 668 | { |
| 669 | struct cpr3_msm8996_mmss_fuses *fuse = vreg->platform_fuses; |
| 670 | u64 next = MSM8998_CPR_PARTIAL_BINNING_NEXT_CORNER; |
| 671 | u64 safe = MSM8998_CPR_PARTIAL_BINNING_SAFE_CORNER; |
| 672 | u32 proc_freq; |
| 673 | struct cpr3_corner *corner; |
| 674 | struct cpr3_corner *safe_corner; |
| 675 | int i, j, low, high, safe_fuse_corner, max_fuse_corner; |
| 676 | |
| 677 | if (!cpr3_ctrl_is_msm8998(vreg->thread->ctrl)) |
| 678 | return 0; |
| 679 | |
| 680 | /* Handle the force highest corner fuse. */ |
| 681 | if (fuse->force_highest_corner) { |
| 682 | cpr3_info(vreg, "overriding CPR parameters for corners 0 to %d with quotients and voltages of corner %d\n", |
| 683 | vreg->corner_count - 2, vreg->corner_count - 1); |
| 684 | corner = &vreg->corner[vreg->corner_count - 1]; |
| 685 | for (i = 0; i < vreg->corner_count - 1; i++) { |
| 686 | proc_freq = vreg->corner[i].proc_freq; |
| 687 | vreg->corner[i] = *corner; |
| 688 | vreg->corner[i].proc_freq = proc_freq; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Return since the potential partial binning fuse values are |
| 693 | * superceded by the force highest corner fuse value. |
| 694 | */ |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Allow up to the max corner which can be fused with partial |
| 700 | * binning values. |
| 701 | */ |
| 702 | max_fuse_corner = min(MSM8998_CPR_PARTIAL_BINNING_MAX_FUSE_CORNER, |
| 703 | vreg->fuse_corner_count - 2); |
| 704 | |
| 705 | for (i = 0; i <= max_fuse_corner; i++) { |
| 706 | /* Determine which higher corners to override with (if any). */ |
| 707 | if (fuse->init_voltage[i] != next |
| 708 | && fuse->init_voltage[i] != safe) |
| 709 | continue; |
| 710 | |
| 711 | for (j = i + 1; j <= max_fuse_corner; j++) |
| 712 | if (fuse->init_voltage[j] != next |
| 713 | && fuse->init_voltage[j] != safe) |
| 714 | break; |
| 715 | safe_fuse_corner = j; |
| 716 | |
| 717 | j = fuse->init_voltage[i] == next ? i + 1 : safe_fuse_corner; |
| 718 | |
| 719 | low = i > 0 ? vreg->fuse_corner_map[i] : 0; |
| 720 | high = vreg->fuse_corner_map[i + 1] - 1; |
| 721 | |
| 722 | cpr3_info(vreg, "overriding CPR parameters for corners %d to %d with quotients of corner %d and voltages of corner %d\n", |
| 723 | low, high, vreg->fuse_corner_map[j], |
| 724 | vreg->fuse_corner_map[safe_fuse_corner]); |
| 725 | |
| 726 | corner = &vreg->corner[vreg->fuse_corner_map[j]]; |
| 727 | safe_corner |
| 728 | = &vreg->corner[vreg->fuse_corner_map[safe_fuse_corner]]; |
| 729 | |
| 730 | for (j = low; j <= high; j++) { |
| 731 | proc_freq = vreg->corner[j].proc_freq; |
| 732 | vreg->corner[j] = *corner; |
| 733 | vreg->corner[j].proc_freq = proc_freq; |
| 734 | |
| 735 | vreg->corner[j].floor_volt |
| 736 | = safe_corner->floor_volt; |
| 737 | vreg->corner[j].ceiling_volt |
| 738 | = safe_corner->ceiling_volt; |
| 739 | vreg->corner[j].open_loop_volt |
| 740 | = safe_corner->open_loop_volt; |
| 741 | vreg->corner[j].abs_ceiling_volt |
| 742 | = safe_corner->abs_ceiling_volt; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * cpr3_mmss_print_settings() - print out MMSS CPR configuration settings into |
| 751 | * the kernel log for debugging purposes |
| 752 | * @vreg: Pointer to the CPR3 regulator |
| 753 | */ |
| 754 | static void cpr3_mmss_print_settings(struct cpr3_regulator *vreg) |
| 755 | { |
| 756 | struct cpr3_corner *corner; |
| 757 | int i; |
| 758 | |
| 759 | cpr3_debug(vreg, "Corner: Frequency (Hz), Fuse Corner, Floor (uV), Open-Loop (uV), Ceiling (uV)\n"); |
| 760 | for (i = 0; i < vreg->corner_count; i++) { |
| 761 | corner = &vreg->corner[i]; |
| 762 | cpr3_debug(vreg, "%3d: %10u, %2d, %7d, %7d, %7d\n", |
| 763 | i, corner->proc_freq, corner->cpr_fuse_corner, |
| 764 | corner->floor_volt, corner->open_loop_volt, |
| 765 | corner->ceiling_volt); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * cpr3_mmss_init_aging() - perform MMSS CPR3 controller specific |
| 771 | * aging initializations |
| 772 | * @ctrl: Pointer to the CPR3 controller |
| 773 | * |
| 774 | * Return: 0 on success, errno on failure |
| 775 | */ |
| 776 | static int cpr3_mmss_init_aging(struct cpr3_controller *ctrl) |
| 777 | { |
| 778 | struct cpr3_msm8996_mmss_fuses *fuse; |
| 779 | struct cpr3_regulator *vreg; |
| 780 | u32 aging_ro_scale; |
| 781 | int rc; |
| 782 | |
| 783 | vreg = &ctrl->thread[0].vreg[0]; |
| 784 | |
| 785 | ctrl->aging_required = vreg->aging_allowed; |
| 786 | fuse = vreg->platform_fuses; |
| 787 | |
| 788 | if (!ctrl->aging_required || !fuse) |
| 789 | return 0; |
| 790 | |
| 791 | rc = cpr3_parse_array_property(vreg, "qcom,cpr-aging-ro-scaling-factor", |
| 792 | 1, &aging_ro_scale); |
| 793 | if (rc) |
| 794 | return rc; |
| 795 | |
| 796 | if (aging_ro_scale == 0) { |
| 797 | cpr3_err(ctrl, "aging RO scaling factor is invalid: %u\n", |
| 798 | aging_ro_scale); |
| 799 | return -EINVAL; |
| 800 | } |
| 801 | |
| 802 | ctrl->aging_vdd_mode = REGULATOR_MODE_NORMAL; |
| 803 | ctrl->aging_complete_vdd_mode = REGULATOR_MODE_IDLE; |
| 804 | |
| 805 | ctrl->aging_sensor_count = 1; |
| 806 | ctrl->aging_sensor = kzalloc(sizeof(*ctrl->aging_sensor), GFP_KERNEL); |
| 807 | if (!ctrl->aging_sensor) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | ctrl->aging_sensor->ro_scale = aging_ro_scale; |
| 811 | |
| 812 | if (cpr3_ctrl_is_msm8998(ctrl)) { |
| 813 | ctrl->aging_sensor->sensor_id = MSM8998_MMSS_AGING_SENSOR_ID; |
| 814 | ctrl->aging_sensor->bypass_mask[0] |
| 815 | = MSM8998_MMSS_AGING_BYPASS_MASK0; |
| 816 | ctrl->aging_sensor->init_quot_diff |
| 817 | = cpr3_convert_open_loop_voltage_fuse(0, |
| 818 | MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SCALE, |
| 819 | fuse->aging_init_quot_diff, |
| 820 | MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SIZE); |
| 821 | } else { |
| 822 | ctrl->aging_sensor->sensor_id = MSM8996_MMSS_AGING_SENSOR_ID; |
| 823 | ctrl->aging_sensor->bypass_mask[0] |
| 824 | = MSM8996_MMSS_AGING_BYPASS_MASK0; |
| 825 | ctrl->aging_sensor->init_quot_diff |
| 826 | = cpr3_convert_open_loop_voltage_fuse(0, |
| 827 | MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SCALE, |
| 828 | fuse->aging_init_quot_diff, |
| 829 | MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SIZE); |
| 830 | } |
| 831 | |
| 832 | cpr3_debug(ctrl, "sensor %u aging init quotient diff = %d, aging RO scale = %u QUOT/V\n", |
| 833 | ctrl->aging_sensor->sensor_id, |
| 834 | ctrl->aging_sensor->init_quot_diff, |
| 835 | ctrl->aging_sensor->ro_scale); |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * cpr3_mmss_init_thread() - perform all steps necessary to initialize the |
| 842 | * configuration data for a CPR3 thread |
| 843 | * @thread: Pointer to the CPR3 thread |
| 844 | * |
| 845 | * Return: 0 on success, errno on failure |
| 846 | */ |
| 847 | static int cpr3_mmss_init_thread(struct cpr3_thread *thread) |
| 848 | { |
| 849 | struct cpr3_regulator *vreg = &thread->vreg[0]; |
| 850 | struct cpr3_msm8996_mmss_fuses *fuse; |
| 851 | int rc; |
| 852 | |
| 853 | rc = cpr3_parse_common_thread_data(thread); |
| 854 | if (rc) { |
| 855 | cpr3_err(vreg, "unable to read CPR thread data from device tree, rc=%d\n", |
| 856 | rc); |
| 857 | return rc; |
| 858 | } |
| 859 | |
| 860 | rc = cpr3_msm8996_mmss_read_fuse_data(vreg); |
| 861 | if (rc) { |
| 862 | cpr3_err(vreg, "unable to read CPR fuse data, rc=%d\n", rc); |
| 863 | return rc; |
| 864 | } |
| 865 | |
| 866 | fuse = vreg->platform_fuses; |
| 867 | if (fuse->limitation == MSM8996_CPR_LIMITATION_UNSUPPORTED) { |
| 868 | cpr3_err(vreg, "this chip requires an unsupported voltage\n"); |
| 869 | return -EPERM; |
| 870 | } else if (fuse->limitation |
| 871 | == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION) { |
| 872 | thread->ctrl->cpr_allowed_hw = false; |
| 873 | } |
| 874 | |
| 875 | rc = cpr3_mmss_parse_corner_data(vreg); |
| 876 | if (rc) { |
| 877 | cpr3_err(vreg, "unable to read CPR corner data from device tree, rc=%d\n", |
| 878 | rc); |
| 879 | return rc; |
| 880 | } |
| 881 | |
| 882 | rc = cpr3_msm8996_mmss_adjust_target_quotients(vreg); |
| 883 | if (rc) { |
| 884 | cpr3_err(vreg, "unable to adjust target quotients, rc=%d\n", |
| 885 | rc); |
| 886 | return rc; |
| 887 | } |
| 888 | |
| 889 | rc = cpr3_msm8996_mmss_calculate_open_loop_voltages(vreg); |
| 890 | if (rc) { |
| 891 | cpr3_err(vreg, "unable to calculate open-loop voltages, rc=%d\n", |
| 892 | rc); |
| 893 | return rc; |
| 894 | } |
| 895 | |
| 896 | rc = cpr3_limit_open_loop_voltages(vreg); |
| 897 | if (rc) { |
| 898 | cpr3_err(vreg, "unable to limit open-loop voltages, rc=%d\n", |
| 899 | rc); |
| 900 | return rc; |
| 901 | } |
| 902 | |
| 903 | cpr3_open_loop_voltage_as_ceiling(vreg); |
| 904 | |
| 905 | rc = cpr3_limit_floor_voltages(vreg); |
| 906 | if (rc) { |
| 907 | cpr3_err(vreg, "unable to limit floor voltages, rc=%d\n", rc); |
| 908 | return rc; |
| 909 | } |
| 910 | |
| 911 | if (cpr3_ctrl_is_msm8998(thread->ctrl)) { |
| 912 | rc = cpr4_parse_core_count_temp_voltage_adj(vreg, false); |
| 913 | if (rc) { |
| 914 | cpr3_err(vreg, "unable to parse temperature based voltage adjustments, rc=%d\n", |
| 915 | rc); |
| 916 | return rc; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | rc = cpr3_msm8998_partial_binning_override(vreg); |
| 921 | if (rc) { |
| 922 | cpr3_err(vreg, "unable to override CPR parameters based on partial binning fuse values, rc=%d\n", |
| 923 | rc); |
| 924 | return rc; |
| 925 | } |
| 926 | |
| 927 | cpr3_mmss_print_settings(vreg); |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * cpr4_mmss_parse_temp_adj_properties() - parse temperature based |
| 934 | * adjustment properties from device tree |
| 935 | * @ctrl: Pointer to the CPR3 controller |
| 936 | * |
| 937 | * Return: 0 on success, errno on failure |
| 938 | */ |
| 939 | static int cpr4_mmss_parse_temp_adj_properties(struct cpr3_controller *ctrl) |
| 940 | { |
| 941 | struct device_node *of_node = ctrl->dev->of_node; |
| 942 | int rc, len, temp_point_count; |
| 943 | |
| 944 | if (!of_find_property(of_node, "qcom,cpr-temp-point-map", &len)) |
| 945 | return 0; |
| 946 | |
| 947 | temp_point_count = len / sizeof(u32); |
| 948 | if (temp_point_count <= 0 |
| 949 | || temp_point_count > MSM8998_MMSS_MAX_TEMP_POINTS) { |
| 950 | cpr3_err(ctrl, "invalid number of temperature points %d > %d (max)\n", |
| 951 | temp_point_count, MSM8998_MMSS_MAX_TEMP_POINTS); |
| 952 | return -EINVAL; |
| 953 | } |
| 954 | |
| 955 | ctrl->temp_points = devm_kcalloc(ctrl->dev, temp_point_count, |
| 956 | sizeof(*ctrl->temp_points), GFP_KERNEL); |
| 957 | if (!ctrl->temp_points) |
| 958 | return -ENOMEM; |
| 959 | |
| 960 | rc = of_property_read_u32_array(of_node, "qcom,cpr-temp-point-map", |
| 961 | ctrl->temp_points, temp_point_count); |
| 962 | if (rc) { |
| 963 | cpr3_err(ctrl, "error reading property qcom,cpr-temp-point-map, rc=%d\n", |
| 964 | rc); |
| 965 | return rc; |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * If t1, t2, and t3 are the temperature points, then the temperature |
| 970 | * bands are: (-inf, t1], (t1, t2], (t2, t3], and (t3, inf). |
| 971 | */ |
| 972 | ctrl->temp_band_count = temp_point_count + 1; |
| 973 | |
| 974 | rc = of_property_read_u32(of_node, "qcom,cpr-initial-temp-band", |
| 975 | &ctrl->initial_temp_band); |
| 976 | if (rc) { |
| 977 | cpr3_err(ctrl, "error reading qcom,cpr-initial-temp-band, rc=%d\n", |
| 978 | rc); |
| 979 | return rc; |
| 980 | } |
| 981 | |
| 982 | if (ctrl->initial_temp_band >= ctrl->temp_band_count) { |
| 983 | cpr3_err(ctrl, "Initial temperature band value %d should be in range [0 - %d]\n", |
| 984 | ctrl->initial_temp_band, ctrl->temp_band_count - 1); |
| 985 | return -EINVAL; |
| 986 | } |
| 987 | |
| 988 | ctrl->temp_sensor_id_start = MSM8998_MMSS_TEMP_SENSOR_ID_START; |
| 989 | ctrl->temp_sensor_id_end = MSM8998_MMSS_TEMP_SENSOR_ID_END; |
| 990 | ctrl->allow_temp_adj = true; |
| 991 | |
| 992 | return rc; |
| 993 | } |
| 994 | |
| 995 | /** |
| 996 | * cpr3_mmss_init_controller() - perform MMSS CPR3 controller specific |
| 997 | * initializations |
| 998 | * @ctrl: Pointer to the CPR3 controller |
| 999 | * |
| 1000 | * Return: 0 on success, errno on failure |
| 1001 | */ |
| 1002 | static int cpr3_mmss_init_controller(struct cpr3_controller *ctrl) |
| 1003 | { |
| 1004 | int rc; |
| 1005 | |
| 1006 | rc = cpr3_parse_common_ctrl_data(ctrl); |
| 1007 | if (rc) { |
| 1008 | if (rc != -EPROBE_DEFER) |
| 1009 | cpr3_err(ctrl, "unable to parse common controller data, rc=%d\n", |
| 1010 | rc); |
| 1011 | return rc; |
| 1012 | } |
| 1013 | |
| 1014 | if (cpr3_ctrl_is_msm8998(ctrl)) { |
| 1015 | rc = cpr4_mmss_parse_temp_adj_properties(ctrl); |
| 1016 | if (rc) |
| 1017 | return rc; |
| 1018 | } |
| 1019 | |
| 1020 | ctrl->sensor_count = cpr3_ctrl_is_msm8998(ctrl) |
| 1021 | ? MSM8998_MMSS_CPR_SENSOR_COUNT |
| 1022 | : MSM8996_MMSS_CPR_SENSOR_COUNT; |
| 1023 | |
| 1024 | /* |
| 1025 | * MMSS only has one thread (0) so the zeroed array does not need |
| 1026 | * further modification. |
| 1027 | */ |
| 1028 | ctrl->sensor_owner = devm_kcalloc(ctrl->dev, ctrl->sensor_count, |
| 1029 | sizeof(*ctrl->sensor_owner), GFP_KERNEL); |
| 1030 | if (!ctrl->sensor_owner) |
| 1031 | return -ENOMEM; |
| 1032 | |
| 1033 | ctrl->cpr_clock_rate = MSM8996_MMSS_CPR_CLOCK_RATE; |
| 1034 | ctrl->ctrl_type = cpr3_ctrl_is_msm8998(ctrl) |
| 1035 | ? CPR_CTRL_TYPE_CPR4 : CPR_CTRL_TYPE_CPR3; |
| 1036 | |
| 1037 | if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) { |
| 1038 | /* |
| 1039 | * Use fixed step quotient if specified otherwise use dynamic |
| 1040 | * calculated per RO step quotient |
| 1041 | */ |
| 1042 | of_property_read_u32(ctrl->dev->of_node, |
| 1043 | "qcom,cpr-step-quot-fixed", |
| 1044 | &ctrl->step_quot_fixed); |
| 1045 | ctrl->use_dynamic_step_quot = !ctrl->step_quot_fixed; |
| 1046 | } |
| 1047 | |
| 1048 | ctrl->iface_clk = devm_clk_get(ctrl->dev, "iface_clk"); |
| 1049 | if (IS_ERR(ctrl->iface_clk)) { |
| 1050 | rc = PTR_ERR(ctrl->iface_clk); |
| 1051 | if (cpr3_ctrl_is_msm8998(ctrl)) { |
| 1052 | /* iface_clk is optional for msm8998 */ |
| 1053 | ctrl->iface_clk = NULL; |
| 1054 | } else if (rc == -EPROBE_DEFER) { |
| 1055 | return rc; |
| 1056 | } else { |
| 1057 | cpr3_err(ctrl, "unable to request interface clock, rc=%d\n", |
| 1058 | rc); |
| 1059 | return rc; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | ctrl->bus_clk = devm_clk_get(ctrl->dev, "bus_clk"); |
| 1064 | if (IS_ERR(ctrl->bus_clk)) { |
| 1065 | rc = PTR_ERR(ctrl->bus_clk); |
| 1066 | if (rc != -EPROBE_DEFER) |
| 1067 | cpr3_err(ctrl, "unable request bus clock, rc=%d\n", |
| 1068 | rc); |
| 1069 | return rc; |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | static int cpr3_mmss_regulator_suspend(struct platform_device *pdev, |
| 1076 | pm_message_t state) |
| 1077 | { |
| 1078 | struct cpr3_controller *ctrl = platform_get_drvdata(pdev); |
| 1079 | |
| 1080 | return cpr3_regulator_suspend(ctrl); |
| 1081 | } |
| 1082 | |
| 1083 | static int cpr3_mmss_regulator_resume(struct platform_device *pdev) |
| 1084 | { |
| 1085 | struct cpr3_controller *ctrl = platform_get_drvdata(pdev); |
| 1086 | |
| 1087 | return cpr3_regulator_resume(ctrl); |
| 1088 | } |
| 1089 | |
| 1090 | /* Data corresponds to the SoC revision */ |
| 1091 | static const struct of_device_id cpr_regulator_match_table[] = { |
| 1092 | { |
| 1093 | .compatible = "qcom,cpr3-msm8996-v1-mmss-regulator", |
| 1094 | .data = (void *)(uintptr_t)1, |
| 1095 | }, |
| 1096 | { |
| 1097 | .compatible = "qcom,cpr3-msm8996-v2-mmss-regulator", |
| 1098 | .data = (void *)(uintptr_t)2, |
| 1099 | }, |
| 1100 | { |
| 1101 | .compatible = "qcom,cpr3-msm8996-v3-mmss-regulator", |
| 1102 | .data = (void *)(uintptr_t)3, |
| 1103 | }, |
| 1104 | { |
| 1105 | .compatible = "qcom,cpr3-msm8996-mmss-regulator", |
| 1106 | .data = (void *)(uintptr_t)3, |
| 1107 | }, |
| 1108 | { |
| 1109 | .compatible = "qcom,cpr3-msm8996pro-mmss-regulator", |
| 1110 | .data = (void *)(uintptr_t)MSM8996PRO_SOC_ID, |
| 1111 | }, |
| 1112 | { |
| 1113 | .compatible = "qcom,cpr4-msm8998-v1-mmss-regulator", |
| 1114 | .data = (void *)(uintptr_t)MSM8998_V1_SOC_ID, |
| 1115 | }, |
| 1116 | { |
| 1117 | .compatible = "qcom,cpr4-msm8998-v2-mmss-regulator", |
| 1118 | .data = (void *)(uintptr_t)MSM8998_V2_SOC_ID, |
| 1119 | }, |
| 1120 | { |
| 1121 | .compatible = "qcom,cpr4-msm8998-mmss-regulator", |
| 1122 | .data = (void *)(uintptr_t)MSM8998_V2_SOC_ID, |
| 1123 | }, |
| 1124 | {} |
| 1125 | }; |
| 1126 | |
| 1127 | static int cpr3_mmss_regulator_probe(struct platform_device *pdev) |
| 1128 | { |
| 1129 | struct device *dev = &pdev->dev; |
| 1130 | const struct of_device_id *match; |
| 1131 | struct cpr3_controller *ctrl; |
| 1132 | int rc; |
| 1133 | |
| 1134 | if (!dev->of_node) { |
| 1135 | dev_err(dev, "Device tree node is missing\n"); |
| 1136 | return -EINVAL; |
| 1137 | } |
| 1138 | |
| 1139 | ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL); |
| 1140 | if (!ctrl) |
| 1141 | return -ENOMEM; |
| 1142 | |
| 1143 | ctrl->dev = dev; |
| 1144 | /* Set to false later if anything precludes CPR operation. */ |
| 1145 | ctrl->cpr_allowed_hw = true; |
| 1146 | |
| 1147 | rc = of_property_read_string(dev->of_node, "qcom,cpr-ctrl-name", |
| 1148 | &ctrl->name); |
| 1149 | if (rc) { |
| 1150 | cpr3_err(ctrl, "unable to read qcom,cpr-ctrl-name, rc=%d\n", |
| 1151 | rc); |
| 1152 | return rc; |
| 1153 | } |
| 1154 | |
| 1155 | match = of_match_node(cpr_regulator_match_table, dev->of_node); |
| 1156 | if (match) |
| 1157 | ctrl->soc_revision = (uintptr_t)match->data; |
| 1158 | else |
| 1159 | cpr3_err(ctrl, "could not find compatible string match\n"); |
| 1160 | |
| 1161 | rc = cpr3_map_fuse_base(ctrl, pdev); |
| 1162 | if (rc) { |
| 1163 | cpr3_err(ctrl, "could not map fuse base address\n"); |
| 1164 | return rc; |
| 1165 | } |
| 1166 | |
| 1167 | rc = cpr3_allocate_threads(ctrl, 0, 0); |
| 1168 | if (rc) { |
| 1169 | cpr3_err(ctrl, "failed to allocate CPR thread array, rc=%d\n", |
| 1170 | rc); |
| 1171 | return rc; |
| 1172 | } |
| 1173 | |
| 1174 | if (ctrl->thread_count != 1) { |
| 1175 | cpr3_err(ctrl, "expected 1 thread but found %d\n", |
| 1176 | ctrl->thread_count); |
| 1177 | return -EINVAL; |
| 1178 | } else if (ctrl->thread[0].vreg_count != 1) { |
| 1179 | cpr3_err(ctrl, "expected 1 regulator but found %d\n", |
| 1180 | ctrl->thread[0].vreg_count); |
| 1181 | return -EINVAL; |
| 1182 | } |
| 1183 | |
| 1184 | rc = cpr3_mmss_init_controller(ctrl); |
| 1185 | if (rc) { |
| 1186 | if (rc != -EPROBE_DEFER) |
| 1187 | cpr3_err(ctrl, "failed to initialize CPR controller parameters, rc=%d\n", |
| 1188 | rc); |
| 1189 | return rc; |
| 1190 | } |
| 1191 | |
| 1192 | rc = cpr3_mmss_init_thread(&ctrl->thread[0]); |
| 1193 | if (rc) { |
| 1194 | cpr3_err(&ctrl->thread[0].vreg[0], "thread initialization failed, rc=%d\n", |
| 1195 | rc); |
| 1196 | return rc; |
| 1197 | } |
| 1198 | |
| 1199 | rc = cpr3_mem_acc_init(&ctrl->thread[0].vreg[0]); |
| 1200 | if (rc) { |
| 1201 | cpr3_err(ctrl, "failed to initialize mem-acc configuration, rc=%d\n", |
| 1202 | rc); |
| 1203 | return rc; |
| 1204 | } |
| 1205 | |
| 1206 | rc = cpr3_mmss_init_aging(ctrl); |
| 1207 | if (rc) { |
| 1208 | cpr3_err(ctrl, "failed to initialize aging configurations, rc=%d\n", |
| 1209 | rc); |
| 1210 | return rc; |
| 1211 | } |
| 1212 | |
| 1213 | platform_set_drvdata(pdev, ctrl); |
| 1214 | |
| 1215 | return cpr3_regulator_register(pdev, ctrl); |
| 1216 | } |
| 1217 | |
| 1218 | static int cpr3_mmss_regulator_remove(struct platform_device *pdev) |
| 1219 | { |
| 1220 | struct cpr3_controller *ctrl = platform_get_drvdata(pdev); |
| 1221 | |
| 1222 | return cpr3_regulator_unregister(ctrl); |
| 1223 | } |
| 1224 | |
| 1225 | static struct platform_driver cpr3_mmss_regulator_driver = { |
| 1226 | .driver = { |
| 1227 | .name = "qcom,cpr3-mmss-regulator", |
| 1228 | .of_match_table = cpr_regulator_match_table, |
| 1229 | .owner = THIS_MODULE, |
| 1230 | }, |
| 1231 | .probe = cpr3_mmss_regulator_probe, |
| 1232 | .remove = cpr3_mmss_regulator_remove, |
| 1233 | .suspend = cpr3_mmss_regulator_suspend, |
| 1234 | .resume = cpr3_mmss_regulator_resume, |
| 1235 | }; |
| 1236 | |
| 1237 | static int cpr_regulator_init(void) |
| 1238 | { |
| 1239 | return platform_driver_register(&cpr3_mmss_regulator_driver); |
| 1240 | } |
| 1241 | |
| 1242 | static void cpr_regulator_exit(void) |
| 1243 | { |
| 1244 | platform_driver_unregister(&cpr3_mmss_regulator_driver); |
| 1245 | } |
| 1246 | |
| 1247 | MODULE_DESCRIPTION("CPR3 MMSS regulator driver"); |
| 1248 | MODULE_LICENSE("GPL v2"); |
| 1249 | |
| 1250 | arch_initcall(cpr_regulator_init); |
| 1251 | module_exit(cpr_regulator_exit); |