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