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