Kiran Gunda | f9d484ef | 2017-10-16 12:15:30 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2012-2015, 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/module.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/regulator/driver.h> |
| 26 | #include <linux/regulator/machine.h> |
| 27 | #include <linux/regulator/of_regulator.h> |
| 28 | #include <linux/regulator/rpm-smd-regulator.h> |
| 29 | #include <soc/qcom/rpm-smd.h> |
| 30 | |
| 31 | /* Debug Definitions */ |
| 32 | |
| 33 | enum { |
| 34 | RPM_VREG_DEBUG_REQUEST = BIT(0), |
| 35 | RPM_VREG_DEBUG_FULL_REQUEST = BIT(1), |
| 36 | RPM_VREG_DEBUG_DUPLICATE = BIT(2), |
| 37 | }; |
| 38 | |
| 39 | static int rpm_vreg_debug_mask; |
| 40 | module_param_named( |
| 41 | debug_mask, rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR |
| 42 | ); |
| 43 | |
| 44 | #define vreg_err(req, fmt, ...) \ |
| 45 | pr_err("%s: " fmt, req->rdesc.name, ##__VA_ARGS__) |
| 46 | |
| 47 | /* RPM regulator request types */ |
| 48 | enum rpm_regulator_type { |
| 49 | RPM_REGULATOR_TYPE_LDO, |
| 50 | RPM_REGULATOR_TYPE_SMPS, |
| 51 | RPM_REGULATOR_TYPE_VS, |
| 52 | RPM_REGULATOR_TYPE_NCP, |
| 53 | RPM_REGULATOR_TYPE_BOB, |
| 54 | RPM_REGULATOR_TYPE_MAX, |
| 55 | }; |
| 56 | |
| 57 | /* RPM resource parameters */ |
| 58 | enum rpm_regulator_param_index { |
| 59 | RPM_REGULATOR_PARAM_ENABLE, |
| 60 | RPM_REGULATOR_PARAM_VOLTAGE, |
| 61 | RPM_REGULATOR_PARAM_CURRENT, |
| 62 | RPM_REGULATOR_PARAM_MODE_LDO, |
| 63 | RPM_REGULATOR_PARAM_MODE_SMPS, |
| 64 | RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE, |
| 65 | RPM_REGULATOR_PARAM_PIN_CTRL_MODE, |
| 66 | RPM_REGULATOR_PARAM_FREQUENCY, |
| 67 | RPM_REGULATOR_PARAM_HEAD_ROOM, |
| 68 | RPM_REGULATOR_PARAM_QUIET_MODE, |
| 69 | RPM_REGULATOR_PARAM_FREQ_REASON, |
| 70 | RPM_REGULATOR_PARAM_CORNER, |
| 71 | RPM_REGULATOR_PARAM_BYPASS, |
| 72 | RPM_REGULATOR_PARAM_FLOOR_CORNER, |
| 73 | RPM_REGULATOR_PARAM_LEVEL, |
| 74 | RPM_REGULATOR_PARAM_FLOOR_LEVEL, |
| 75 | RPM_REGULATOR_PARAM_MODE_BOB, |
| 76 | RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE1, |
| 77 | RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE2, |
| 78 | RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE3, |
| 79 | RPM_REGULATOR_PARAM_MAX, |
| 80 | }; |
| 81 | |
| 82 | enum rpm_regulator_smps_mode { |
| 83 | RPM_REGULATOR_SMPS_MODE_AUTO = 0, |
| 84 | RPM_REGULATOR_SMPS_MODE_IPEAK = 1, |
| 85 | RPM_REGULATOR_SMPS_MODE_PWM = 2, |
| 86 | }; |
| 87 | |
| 88 | enum rpm_regulator_ldo_mode { |
| 89 | RPM_REGULATOR_LDO_MODE_IPEAK = 0, |
| 90 | RPM_REGULATOR_LDO_MODE_HPM = 1, |
| 91 | }; |
| 92 | |
| 93 | enum rpm_regulator_bob_mode { |
| 94 | RPM_REGULATOR_BOB_MODE_PASS = 0, |
| 95 | RPM_REGULATOR_BOB_MODE_PFM = 1, |
| 96 | RPM_REGULATOR_BOB_MODE_AUTO = 2, |
| 97 | RPM_REGULATOR_BOB_MODE_PWM = 3, |
| 98 | }; |
| 99 | |
| 100 | #define RPM_SET_CONFIG_ACTIVE BIT(0) |
| 101 | #define RPM_SET_CONFIG_SLEEP BIT(1) |
| 102 | #define RPM_SET_CONFIG_BOTH (RPM_SET_CONFIG_ACTIVE \ |
| 103 | | RPM_SET_CONFIG_SLEEP) |
| 104 | struct rpm_regulator_param { |
| 105 | char *name; |
| 106 | char *property_name; |
| 107 | u32 key; |
| 108 | u32 min; |
| 109 | u32 max; |
| 110 | u32 supported_regulator_types; |
| 111 | }; |
| 112 | |
| 113 | #define PARAM(_idx, _support_ldo, _support_smps, _support_vs, _support_ncp, \ |
| 114 | _support_bob, _name, _min, _max, _property_name) \ |
| 115 | [RPM_REGULATOR_PARAM_##_idx] = { \ |
| 116 | .name = _name, \ |
| 117 | .property_name = _property_name, \ |
| 118 | .min = _min, \ |
| 119 | .max = _max, \ |
| 120 | .supported_regulator_types = \ |
| 121 | _support_ldo << RPM_REGULATOR_TYPE_LDO | \ |
| 122 | _support_smps << RPM_REGULATOR_TYPE_SMPS | \ |
| 123 | _support_vs << RPM_REGULATOR_TYPE_VS | \ |
| 124 | _support_ncp << RPM_REGULATOR_TYPE_NCP | \ |
| 125 | _support_bob << RPM_REGULATOR_TYPE_BOB, \ |
| 126 | } |
| 127 | |
| 128 | static struct rpm_regulator_param params[RPM_REGULATOR_PARAM_MAX] = { |
| 129 | /* ID LDO SMPS VS NCP BOB name min max property-name */ |
| 130 | PARAM(ENABLE, 1, 1, 1, 1, 1, "swen", 0, 1, "qcom,init-enable"), |
| 131 | PARAM(VOLTAGE, 1, 1, 0, 1, 1, "uv", 0, 0x7FFFFFF, "qcom,init-voltage"), |
| 132 | PARAM(CURRENT, 1, 1, 0, 0, 0, "ma", 0, 0x1FFF, "qcom,init-current"), |
| 133 | PARAM(MODE_LDO, 1, 0, 0, 0, 0, "lsmd", 0, 1, "qcom,init-ldo-mode"), |
| 134 | PARAM(MODE_SMPS, 0, 1, 0, 0, 0, "ssmd", 0, 2, "qcom,init-smps-mode"), |
| 135 | PARAM(PIN_CTRL_ENABLE, 1, 1, 1, 0, 0, "pcen", 0, 0xF, "qcom,init-pin-ctrl-enable"), |
| 136 | PARAM(PIN_CTRL_MODE, 1, 1, 1, 0, 0, "pcmd", 0, 0x1F, "qcom,init-pin-ctrl-mode"), |
| 137 | PARAM(FREQUENCY, 0, 1, 0, 1, 0, "freq", 0, 31, "qcom,init-frequency"), |
| 138 | PARAM(HEAD_ROOM, 1, 0, 0, 1, 0, "hr", 0, 0x7FFFFFFF, "qcom,init-head-room"), |
| 139 | PARAM(QUIET_MODE, 0, 1, 0, 0, 0, "qm", 0, 2, "qcom,init-quiet-mode"), |
| 140 | PARAM(FREQ_REASON, 0, 1, 0, 1, 0, "resn", 0, 8, "qcom,init-freq-reason"), |
| 141 | PARAM(CORNER, 1, 1, 0, 0, 0, "corn", 0, 6, "qcom,init-voltage-corner"), |
| 142 | PARAM(BYPASS, 1, 0, 0, 0, 0, "bypa", 0, 1, "qcom,init-disallow-bypass"), |
| 143 | PARAM(FLOOR_CORNER, 1, 1, 0, 0, 0, "vfc", 0, 6, "qcom,init-voltage-floor-corner"), |
| 144 | PARAM(LEVEL, 1, 1, 0, 0, 0, "vlvl", 0, 0xFFFF, "qcom,init-voltage-level"), |
| 145 | PARAM(FLOOR_LEVEL, 1, 1, 0, 0, 0, "vfl", 0, 0xFFFF, "qcom,init-voltage-floor-level"), |
| 146 | PARAM(MODE_BOB, 0, 0, 0, 0, 1, "bobm", 0, 3, "qcom,init-bob-mode"), |
| 147 | PARAM(PIN_CTRL_VOLTAGE1, 0, 0, 0, 0, 1, "pcv1", 0, 0x7FFFFFF, "qcom,init-pin-ctrl-voltage1"), |
| 148 | PARAM(PIN_CTRL_VOLTAGE2, 0, 0, 0, 0, 1, "pcv2", 0, 0x7FFFFFF, "qcom,init-pin-ctrl-voltage2"), |
| 149 | PARAM(PIN_CTRL_VOLTAGE3, 0, 0, 0, 0, 1, "pcv3", 0, 0x7FFFFFF, "qcom,init-pin-ctrl-voltage3"), |
| 150 | }; |
| 151 | |
| 152 | struct rpm_regulator_mode_map { |
| 153 | int ldo_mode; |
| 154 | int smps_mode; |
| 155 | }; |
| 156 | |
| 157 | static struct rpm_regulator_mode_map mode_mapping[] = { |
| 158 | [RPM_REGULATOR_MODE_AUTO] |
| 159 | = {-1, RPM_REGULATOR_SMPS_MODE_AUTO}, |
| 160 | [RPM_REGULATOR_MODE_IPEAK] |
| 161 | = {RPM_REGULATOR_LDO_MODE_IPEAK, RPM_REGULATOR_SMPS_MODE_IPEAK}, |
| 162 | [RPM_REGULATOR_MODE_HPM] |
| 163 | = {RPM_REGULATOR_LDO_MODE_HPM, RPM_REGULATOR_SMPS_MODE_PWM}, |
| 164 | }; |
| 165 | |
| 166 | /* Indices for use with pin control enable via enable/disable feature. */ |
| 167 | #define RPM_VREG_PIN_CTRL_STATE_DISABLE 0 |
| 168 | #define RPM_VREG_PIN_CTRL_STATE_ENABLE 1 |
| 169 | #define RPM_VREG_PIN_CTRL_STATE_COUNT 2 |
| 170 | |
| 171 | struct rpm_vreg_request { |
| 172 | u32 param[RPM_REGULATOR_PARAM_MAX]; |
| 173 | u32 valid; |
| 174 | u32 modified; |
| 175 | }; |
| 176 | |
| 177 | struct rpm_vreg { |
| 178 | struct rpm_vreg_request aggr_req_active; |
| 179 | struct rpm_vreg_request aggr_req_sleep; |
| 180 | struct list_head reg_list; |
| 181 | const char *resource_name; |
| 182 | u32 resource_id; |
| 183 | bool allow_atomic; |
| 184 | int regulator_type; |
| 185 | int hpm_min_load; |
| 186 | int enable_time; |
| 187 | spinlock_t slock; |
| 188 | struct mutex mlock; |
| 189 | unsigned long flags; |
| 190 | bool sleep_request_sent; |
| 191 | bool wait_for_ack_active; |
| 192 | bool wait_for_ack_sleep; |
| 193 | bool always_wait_for_ack; |
| 194 | bool apps_only; |
| 195 | struct msm_rpm_request *handle_active; |
| 196 | struct msm_rpm_request *handle_sleep; |
| 197 | }; |
| 198 | |
| 199 | struct rpm_regulator { |
| 200 | struct regulator_desc rdesc; |
| 201 | struct regulator_dev *rdev; |
| 202 | struct rpm_vreg *rpm_vreg; |
| 203 | struct list_head list; |
| 204 | bool set_active; |
| 205 | bool set_sleep; |
| 206 | bool always_send_voltage; |
| 207 | bool always_send_current; |
| 208 | bool use_pin_ctrl_for_enable; |
| 209 | struct rpm_vreg_request req; |
| 210 | int system_load; |
| 211 | int min_uV; |
| 212 | int max_uV; |
| 213 | u32 pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_COUNT]; |
| 214 | enum rpm_regulator_param_index voltage_index; |
| 215 | int voltage_offset; |
| 216 | }; |
| 217 | |
| 218 | /* |
| 219 | * This voltage in uV is returned by get_voltage functions when there is no way |
| 220 | * to determine the current voltage level. It is needed because the regulator |
| 221 | * framework treats a 0 uV voltage as an error. |
| 222 | */ |
| 223 | #define VOLTAGE_UNKNOWN 1 |
| 224 | |
| 225 | /* |
| 226 | * Regulator requests sent in the active set take effect immediately. Requests |
| 227 | * sent in the sleep set take effect when the Apps processor transitions into |
| 228 | * RPM assisted power collapse. For any given regulator, if an active set |
| 229 | * request is present, but not a sleep set request, then the active set request |
| 230 | * is used at all times, even when the Apps processor is power collapsed. |
| 231 | * |
| 232 | * The rpm-regulator-smd takes advantage of this default usage of the active set |
| 233 | * request by only sending a sleep set request if it differs from the |
| 234 | * corresponding active set request. |
| 235 | */ |
| 236 | #define RPM_SET_ACTIVE MSM_RPM_CTX_ACTIVE_SET |
| 237 | #define RPM_SET_SLEEP MSM_RPM_CTX_SLEEP_SET |
| 238 | |
| 239 | static u32 rpm_vreg_string_to_int(const u8 *str) |
| 240 | { |
| 241 | int i, len; |
| 242 | u32 output = 0; |
| 243 | |
| 244 | len = strnlen(str, sizeof(u32)); |
| 245 | for (i = 0; i < len; i++) |
| 246 | output |= str[i] << (i * 8); |
| 247 | |
| 248 | return output; |
| 249 | } |
| 250 | |
| 251 | static inline void rpm_vreg_lock(struct rpm_vreg *rpm_vreg) |
| 252 | { |
| 253 | if (rpm_vreg->allow_atomic) |
| 254 | spin_lock_irqsave(&rpm_vreg->slock, rpm_vreg->flags); |
| 255 | else |
| 256 | mutex_lock(&rpm_vreg->mlock); |
| 257 | } |
| 258 | |
| 259 | static inline void rpm_vreg_unlock(struct rpm_vreg *rpm_vreg) |
| 260 | { |
| 261 | if (rpm_vreg->allow_atomic) |
| 262 | spin_unlock_irqrestore(&rpm_vreg->slock, rpm_vreg->flags); |
| 263 | else |
| 264 | mutex_unlock(&rpm_vreg->mlock); |
| 265 | } |
| 266 | |
| 267 | static inline bool rpm_vreg_active_or_sleep_enabled(struct rpm_vreg *rpm_vreg) |
| 268 | { |
| 269 | return (rpm_vreg->aggr_req_active.param[RPM_REGULATOR_PARAM_ENABLE] |
| 270 | && (rpm_vreg->aggr_req_active.valid |
| 271 | & BIT(RPM_REGULATOR_PARAM_ENABLE))) |
| 272 | || ((rpm_vreg->aggr_req_sleep.param[RPM_REGULATOR_PARAM_ENABLE]) |
| 273 | && (rpm_vreg->aggr_req_sleep.valid |
| 274 | & BIT(RPM_REGULATOR_PARAM_ENABLE))); |
| 275 | } |
| 276 | |
| 277 | static inline bool rpm_vreg_shared_active_or_sleep_enabled_valid |
| 278 | (struct rpm_vreg *rpm_vreg) |
| 279 | { |
| 280 | return !rpm_vreg->apps_only && |
| 281 | ((rpm_vreg->aggr_req_active.valid |
| 282 | & BIT(RPM_REGULATOR_PARAM_ENABLE)) |
| 283 | || (rpm_vreg->aggr_req_sleep.valid |
| 284 | & BIT(RPM_REGULATOR_PARAM_ENABLE))); |
| 285 | } |
| 286 | |
| 287 | static const u32 power_level_params = |
| 288 | BIT(RPM_REGULATOR_PARAM_ENABLE) | |
| 289 | BIT(RPM_REGULATOR_PARAM_VOLTAGE) | |
| 290 | BIT(RPM_REGULATOR_PARAM_CURRENT) | |
| 291 | BIT(RPM_REGULATOR_PARAM_CORNER) | |
| 292 | BIT(RPM_REGULATOR_PARAM_BYPASS) | |
| 293 | BIT(RPM_REGULATOR_PARAM_FLOOR_CORNER) | |
| 294 | BIT(RPM_REGULATOR_PARAM_LEVEL) | |
| 295 | BIT(RPM_REGULATOR_PARAM_FLOOR_LEVEL); |
| 296 | |
| 297 | static bool rpm_vreg_ack_required(struct rpm_vreg *rpm_vreg, u32 set, |
| 298 | const u32 *prev_param, const u32 *param, |
| 299 | u32 prev_valid, u32 modified) |
| 300 | { |
| 301 | u32 mask; |
| 302 | int i; |
| 303 | |
| 304 | if (rpm_vreg->always_wait_for_ack |
| 305 | || (set == RPM_SET_ACTIVE && rpm_vreg->wait_for_ack_active) |
| 306 | || (set == RPM_SET_SLEEP && rpm_vreg->wait_for_ack_sleep)) |
| 307 | return true; |
| 308 | |
| 309 | for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) { |
| 310 | mask = BIT(i); |
| 311 | if (modified & mask) { |
| 312 | if ((prev_valid & mask) && (power_level_params & mask) |
| 313 | && (param[i] <= prev_param[i])) |
| 314 | continue; |
| 315 | else |
| 316 | return true; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | static void rpm_vreg_check_param_max(struct rpm_regulator *regulator, int index, |
| 324 | u32 new_max) |
| 325 | { |
| 326 | struct rpm_vreg *rpm_vreg = regulator->rpm_vreg; |
| 327 | |
| 328 | if (regulator->set_active |
| 329 | && (rpm_vreg->aggr_req_active.valid & BIT(index)) |
| 330 | && rpm_vreg->aggr_req_active.param[index] > new_max) |
| 331 | rpm_vreg->wait_for_ack_active = true; |
| 332 | |
| 333 | if (regulator->set_sleep |
| 334 | && (rpm_vreg->aggr_req_sleep.valid & BIT(index)) |
| 335 | && rpm_vreg->aggr_req_sleep.param[index] > new_max) |
| 336 | rpm_vreg->wait_for_ack_sleep = true; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * This is used when voting for LPM or HPM by subtracting or adding to the |
| 341 | * hpm_min_load of a regulator. It has units of uA. |
| 342 | */ |
| 343 | #define LOAD_THRESHOLD_STEP 1000 |
| 344 | |
| 345 | static inline int rpm_vreg_hpm_min_uA(struct rpm_vreg *rpm_vreg) |
| 346 | { |
| 347 | return rpm_vreg->hpm_min_load; |
| 348 | } |
| 349 | |
| 350 | static inline int rpm_vreg_lpm_max_uA(struct rpm_vreg *rpm_vreg) |
| 351 | { |
| 352 | return rpm_vreg->hpm_min_load - LOAD_THRESHOLD_STEP; |
| 353 | } |
| 354 | |
| 355 | #define MICRO_TO_MILLI(uV) ((uV) / 1000) |
| 356 | #define MILLI_TO_MICRO(uV) ((uV) * 1000) |
| 357 | |
| 358 | #define DEBUG_PRINT_BUFFER_SIZE 512 |
| 359 | #define REQ_SENT 0 |
| 360 | #define REQ_PREV 1 |
| 361 | #define REQ_CACHED 2 |
| 362 | #define REQ_TYPES 3 |
| 363 | |
| 364 | static void rpm_regulator_req(struct rpm_regulator *regulator, int set, |
| 365 | bool sent) |
| 366 | { |
| 367 | char buf[DEBUG_PRINT_BUFFER_SIZE]; |
| 368 | size_t buflen = DEBUG_PRINT_BUFFER_SIZE; |
| 369 | struct rpm_vreg *rpm_vreg = regulator->rpm_vreg; |
| 370 | struct rpm_vreg_request *aggr; |
| 371 | bool first; |
| 372 | u32 mask[REQ_TYPES] = {0, 0, 0}; |
| 373 | const char *req_names[REQ_TYPES] = {"sent", "prev", "cached"}; |
| 374 | int pos = 0; |
| 375 | int i, j; |
| 376 | |
| 377 | aggr = (set == RPM_SET_ACTIVE) |
| 378 | ? &rpm_vreg->aggr_req_active : &rpm_vreg->aggr_req_sleep; |
| 379 | |
| 380 | if (rpm_vreg_debug_mask & RPM_VREG_DEBUG_DUPLICATE) { |
| 381 | mask[REQ_SENT] = aggr->modified; |
| 382 | mask[REQ_PREV] = aggr->valid & ~aggr->modified; |
| 383 | } else if (sent |
| 384 | && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_FULL_REQUEST)) { |
| 385 | mask[REQ_SENT] = aggr->modified; |
| 386 | mask[REQ_PREV] = aggr->valid & ~aggr->modified; |
| 387 | } else if (sent && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_REQUEST)) { |
| 388 | mask[REQ_SENT] = aggr->modified; |
| 389 | } |
| 390 | |
| 391 | if (!(mask[REQ_SENT] | mask[REQ_PREV])) |
| 392 | return; |
| 393 | |
| 394 | if (set == RPM_SET_SLEEP && !rpm_vreg->sleep_request_sent) { |
| 395 | mask[REQ_CACHED] = mask[REQ_SENT] | mask[REQ_PREV]; |
| 396 | mask[REQ_SENT] = 0; |
| 397 | mask[REQ_PREV] = 0; |
| 398 | } |
| 399 | |
| 400 | pos += scnprintf(buf + pos, buflen - pos, "%s%s: ", |
| 401 | KERN_INFO, __func__); |
| 402 | |
| 403 | pos += scnprintf(buf + pos, buflen - pos, "%s %u (%s): s=%s", |
| 404 | rpm_vreg->resource_name, rpm_vreg->resource_id, |
| 405 | regulator->rdesc.name, |
| 406 | (set == RPM_SET_ACTIVE ? "act" : "slp")); |
| 407 | |
| 408 | for (i = 0; i < REQ_TYPES; i++) { |
| 409 | if (mask[i]) |
| 410 | pos += scnprintf(buf + pos, buflen - pos, "; %s: ", |
| 411 | req_names[i]); |
| 412 | |
| 413 | first = true; |
| 414 | for (j = 0; j < RPM_REGULATOR_PARAM_MAX; j++) { |
| 415 | if (mask[i] & BIT(j)) { |
| 416 | pos += scnprintf(buf + pos, buflen - pos, |
| 417 | "%s%s=%u", (first ? "" : ", "), |
| 418 | params[j].name, aggr->param[j]); |
| 419 | first = false; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | pos += scnprintf(buf + pos, buflen - pos, "\n"); |
| 425 | printk(buf); |
| 426 | } |
| 427 | |
| 428 | #define RPM_VREG_SET_PARAM(_regulator, _param, _val) \ |
| 429 | { \ |
| 430 | (_regulator)->req.param[RPM_REGULATOR_PARAM_##_param] = _val; \ |
| 431 | (_regulator)->req.modified |= BIT(RPM_REGULATOR_PARAM_##_param); \ |
| 432 | } \ |
| 433 | |
| 434 | static int rpm_vreg_add_kvp_to_request(struct rpm_vreg *rpm_vreg, |
| 435 | const u32 *param, int idx, u32 set) |
| 436 | { |
| 437 | struct msm_rpm_request *handle; |
| 438 | |
| 439 | handle = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active |
| 440 | : rpm_vreg->handle_sleep); |
| 441 | |
| 442 | if (rpm_vreg->allow_atomic) |
| 443 | return msm_rpm_add_kvp_data_noirq(handle, params[idx].key, |
| 444 | (u8 *)¶m[idx], 4); |
| 445 | else |
| 446 | return msm_rpm_add_kvp_data(handle, params[idx].key, |
| 447 | (u8 *)¶m[idx], 4); |
| 448 | } |
| 449 | |
| 450 | static void rpm_vreg_check_modified_requests(const u32 *prev_param, |
| 451 | const u32 *param, u32 prev_valid, u32 *modified) |
| 452 | { |
| 453 | u32 value_changed = 0; |
| 454 | int i; |
| 455 | |
| 456 | for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) { |
| 457 | if (param[i] != prev_param[i]) |
| 458 | value_changed |= BIT(i); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Only keep bits that are for changed parameters or previously |
| 463 | * invalid parameters. |
| 464 | */ |
| 465 | *modified &= value_changed | ~prev_valid; |
| 466 | } |
| 467 | |
| 468 | static int rpm_vreg_add_modified_requests(struct rpm_regulator *regulator, |
| 469 | u32 set, const u32 *param, u32 modified) |
| 470 | { |
| 471 | struct rpm_vreg *rpm_vreg = regulator->rpm_vreg; |
| 472 | int rc = 0; |
| 473 | int i; |
| 474 | |
| 475 | for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) { |
| 476 | /* Only send requests for modified parameters. */ |
| 477 | if (modified & BIT(i)) { |
| 478 | rc = rpm_vreg_add_kvp_to_request(rpm_vreg, param, i, |
| 479 | set); |
| 480 | if (rc) { |
| 481 | vreg_err(regulator, |
| 482 | "add KVP failed: %s %u; %s, rc=%d\n", |
| 483 | rpm_vreg->resource_name, |
| 484 | rpm_vreg->resource_id, params[i].name, |
| 485 | rc); |
| 486 | return rc; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return rc; |
| 492 | } |
| 493 | |
| 494 | static int rpm_vreg_send_request(struct rpm_regulator *regulator, u32 set, |
| 495 | bool wait_for_ack) |
| 496 | { |
| 497 | struct rpm_vreg *rpm_vreg = regulator->rpm_vreg; |
| 498 | struct msm_rpm_request *handle |
| 499 | = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active |
| 500 | : rpm_vreg->handle_sleep); |
| 501 | int rc = 0; |
| 502 | void *temp; |
| 503 | |
| 504 | if (unlikely(rpm_vreg->allow_atomic)) { |
| 505 | rc = msm_rpm_wait_for_ack_noirq(msm_rpm_send_request_noirq( |
| 506 | handle)); |
| 507 | } else if (wait_for_ack) { |
| 508 | rc = msm_rpm_wait_for_ack(msm_rpm_send_request(handle)); |
| 509 | } else { |
| 510 | temp = msm_rpm_send_request_noack(handle); |
| 511 | if (IS_ERR(temp)) |
| 512 | rc = PTR_ERR(temp); |
| 513 | } |
| 514 | |
| 515 | if (rc) |
| 516 | vreg_err(regulator, |
| 517 | "msm rpm send failed: %s %u; set=%s, rc=%d\n", |
| 518 | rpm_vreg->resource_name, |
| 519 | rpm_vreg->resource_id, |
| 520 | (set == RPM_SET_ACTIVE ? "act" : "slp"), rc); |
| 521 | |
| 522 | return rc; |
| 523 | } |
| 524 | |
| 525 | #define RPM_VREG_AGGR_MIN(_idx, _param_aggr, _param_reg) \ |
| 526 | { \ |
| 527 | _param_aggr[RPM_REGULATOR_PARAM_##_idx] \ |
| 528 | = min(_param_aggr[RPM_REGULATOR_PARAM_##_idx], \ |
| 529 | _param_reg[RPM_REGULATOR_PARAM_##_idx]); \ |
| 530 | } |
| 531 | |
| 532 | #define RPM_VREG_AGGR_MAX(_idx, _param_aggr, _param_reg) \ |
| 533 | { \ |
| 534 | _param_aggr[RPM_REGULATOR_PARAM_##_idx] \ |
| 535 | = max(_param_aggr[RPM_REGULATOR_PARAM_##_idx], \ |
| 536 | _param_reg[RPM_REGULATOR_PARAM_##_idx]); \ |
| 537 | } |
| 538 | |
| 539 | #define RPM_VREG_AGGR_SUM(_idx, _param_aggr, _param_reg) \ |
| 540 | { \ |
| 541 | _param_aggr[RPM_REGULATOR_PARAM_##_idx] \ |
| 542 | += _param_reg[RPM_REGULATOR_PARAM_##_idx]; \ |
| 543 | } |
| 544 | |
| 545 | #define RPM_VREG_AGGR_OR(_idx, _param_aggr, _param_reg) \ |
| 546 | { \ |
| 547 | _param_aggr[RPM_REGULATOR_PARAM_##_idx] \ |
| 548 | |= _param_reg[RPM_REGULATOR_PARAM_##_idx]; \ |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * Aggregation is performed on each parameter based on the way that the RPM |
| 553 | * aggregates that type internally between RPM masters. |
| 554 | */ |
| 555 | static void rpm_vreg_aggregate_params(u32 *param_aggr, const u32 *param_reg) |
| 556 | { |
| 557 | RPM_VREG_AGGR_MAX(ENABLE, param_aggr, param_reg); |
| 558 | RPM_VREG_AGGR_MAX(VOLTAGE, param_aggr, param_reg); |
| 559 | RPM_VREG_AGGR_SUM(CURRENT, param_aggr, param_reg); |
| 560 | RPM_VREG_AGGR_MAX(MODE_LDO, param_aggr, param_reg); |
| 561 | RPM_VREG_AGGR_MAX(MODE_SMPS, param_aggr, param_reg); |
| 562 | RPM_VREG_AGGR_OR(PIN_CTRL_ENABLE, param_aggr, param_reg); |
| 563 | RPM_VREG_AGGR_OR(PIN_CTRL_MODE, param_aggr, param_reg); |
| 564 | RPM_VREG_AGGR_MIN(FREQUENCY, param_aggr, param_reg); |
| 565 | RPM_VREG_AGGR_MAX(HEAD_ROOM, param_aggr, param_reg); |
| 566 | RPM_VREG_AGGR_MAX(QUIET_MODE, param_aggr, param_reg); |
| 567 | RPM_VREG_AGGR_MAX(FREQ_REASON, param_aggr, param_reg); |
| 568 | RPM_VREG_AGGR_MAX(CORNER, param_aggr, param_reg); |
| 569 | RPM_VREG_AGGR_MAX(BYPASS, param_aggr, param_reg); |
| 570 | RPM_VREG_AGGR_MAX(FLOOR_CORNER, param_aggr, param_reg); |
| 571 | RPM_VREG_AGGR_MAX(LEVEL, param_aggr, param_reg); |
| 572 | RPM_VREG_AGGR_MAX(FLOOR_LEVEL, param_aggr, param_reg); |
| 573 | RPM_VREG_AGGR_MAX(MODE_BOB, param_aggr, param_reg); |
| 574 | RPM_VREG_AGGR_MAX(PIN_CTRL_VOLTAGE1, param_aggr, param_reg); |
| 575 | RPM_VREG_AGGR_MAX(PIN_CTRL_VOLTAGE2, param_aggr, param_reg); |
| 576 | RPM_VREG_AGGR_MAX(PIN_CTRL_VOLTAGE3, param_aggr, param_reg); |
| 577 | } |
| 578 | |
| 579 | static int rpm_vreg_aggregate_requests(struct rpm_regulator *regulator) |
| 580 | { |
| 581 | struct rpm_vreg *rpm_vreg = regulator->rpm_vreg; |
| 582 | u32 param_active[RPM_REGULATOR_PARAM_MAX]; |
| 583 | u32 param_sleep[RPM_REGULATOR_PARAM_MAX]; |
| 584 | u32 modified_active, modified_sleep; |
| 585 | struct rpm_regulator *reg; |
| 586 | bool sleep_set_differs = false; |
| 587 | bool send_active = false; |
| 588 | bool send_sleep = false; |
| 589 | bool wait_for_ack; |
| 590 | int rc = 0; |
| 591 | int i; |
| 592 | |
| 593 | memset(param_active, 0, sizeof(param_active)); |
| 594 | memset(param_sleep, 0, sizeof(param_sleep)); |
| 595 | modified_active = rpm_vreg->aggr_req_active.modified; |
| 596 | modified_sleep = rpm_vreg->aggr_req_sleep.modified; |
| 597 | |
| 598 | /* |
| 599 | * Aggregate all of the requests for this regulator in both active |
| 600 | * and sleep sets. |
| 601 | */ |
| 602 | list_for_each_entry(reg, &rpm_vreg->reg_list, list) { |
| 603 | if (reg->set_active) { |
| 604 | rpm_vreg_aggregate_params(param_active, reg->req.param); |
| 605 | modified_active |= reg->req.modified; |
| 606 | } |
| 607 | if (reg->set_sleep) { |
| 608 | rpm_vreg_aggregate_params(param_sleep, reg->req.param); |
| 609 | modified_sleep |= reg->req.modified; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Check if the aggregated sleep set parameter values differ from the |
| 615 | * aggregated active set parameter values. |
| 616 | */ |
| 617 | if (!rpm_vreg->sleep_request_sent) { |
| 618 | for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) { |
| 619 | if ((param_active[i] != param_sleep[i]) |
| 620 | && (modified_sleep & BIT(i))) { |
| 621 | sleep_set_differs = true; |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* Add KVPs to the active set RPM request if they have new values. */ |
| 628 | rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_active.param, |
| 629 | param_active, rpm_vreg->aggr_req_active.valid, |
| 630 | &modified_active); |
| 631 | rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_ACTIVE, |
| 632 | param_active, modified_active); |
| 633 | if (rc) |
| 634 | return rc; |
| 635 | send_active = modified_active; |
| 636 | |
| 637 | /* |
| 638 | * Sleep set configurations are only sent if they differ from the |
| 639 | * active set values. This is because the active set values will take |
| 640 | * effect during rpm assisted power collapse in the absence of sleep set |
| 641 | * values. |
| 642 | * |
| 643 | * However, once a sleep set request is sent for a given regulator, |
| 644 | * additional sleep set requests must be sent in the future even if they |
| 645 | * match the corresponding active set requests. |
| 646 | */ |
| 647 | if (rpm_vreg->sleep_request_sent || sleep_set_differs) { |
| 648 | /* Add KVPs to the sleep set RPM request if they are new. */ |
| 649 | rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_sleep.param, |
| 650 | param_sleep, rpm_vreg->aggr_req_sleep.valid, |
| 651 | &modified_sleep); |
| 652 | rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_SLEEP, |
| 653 | param_sleep, modified_sleep); |
| 654 | if (rc) |
| 655 | return rc; |
| 656 | send_sleep = modified_sleep; |
| 657 | } |
| 658 | |
| 659 | /* Send active set request to the RPM if it contains new KVPs. */ |
| 660 | if (send_active) { |
| 661 | wait_for_ack = rpm_vreg_ack_required(rpm_vreg, RPM_SET_ACTIVE, |
| 662 | rpm_vreg->aggr_req_active.param, |
| 663 | param_active, |
| 664 | rpm_vreg->aggr_req_active.valid, |
| 665 | modified_active); |
| 666 | rc = rpm_vreg_send_request(regulator, RPM_SET_ACTIVE, |
| 667 | wait_for_ack); |
| 668 | if (rc) |
| 669 | return rc; |
| 670 | rpm_vreg->aggr_req_active.valid |= modified_active; |
| 671 | rpm_vreg->wait_for_ack_active = false; |
| 672 | } |
| 673 | /* Store the results of the aggregation. */ |
| 674 | rpm_vreg->aggr_req_active.modified = modified_active; |
| 675 | memcpy(rpm_vreg->aggr_req_active.param, param_active, |
| 676 | sizeof(param_active)); |
| 677 | |
| 678 | /* Handle debug printing of the active set request. */ |
| 679 | rpm_regulator_req(regulator, RPM_SET_ACTIVE, send_active); |
| 680 | if (send_active) |
| 681 | rpm_vreg->aggr_req_active.modified = 0; |
| 682 | |
| 683 | /* Send sleep set request to the RPM if it contains new KVPs. */ |
| 684 | if (send_sleep) { |
| 685 | wait_for_ack = rpm_vreg_ack_required(rpm_vreg, RPM_SET_SLEEP, |
| 686 | rpm_vreg->aggr_req_sleep.param, |
| 687 | param_sleep, |
| 688 | rpm_vreg->aggr_req_sleep.valid, |
| 689 | modified_sleep); |
| 690 | rc = rpm_vreg_send_request(regulator, RPM_SET_SLEEP, |
| 691 | wait_for_ack); |
| 692 | if (rc) |
| 693 | return rc; |
| 694 | else |
| 695 | rpm_vreg->sleep_request_sent = true; |
| 696 | rpm_vreg->aggr_req_sleep.valid |= modified_sleep; |
| 697 | rpm_vreg->wait_for_ack_sleep = false; |
| 698 | } |
| 699 | /* Store the results of the aggregation. */ |
| 700 | rpm_vreg->aggr_req_sleep.modified = modified_sleep; |
| 701 | memcpy(rpm_vreg->aggr_req_sleep.param, param_sleep, |
| 702 | sizeof(param_sleep)); |
| 703 | |
| 704 | /* Handle debug printing of the sleep set request. */ |
| 705 | rpm_regulator_req(regulator, RPM_SET_SLEEP, send_sleep); |
| 706 | if (send_sleep) |
| 707 | rpm_vreg->aggr_req_sleep.modified = 0; |
| 708 | |
| 709 | /* |
| 710 | * Loop over all requests for this regulator to update the valid and |
| 711 | * modified values for use in future aggregation. |
| 712 | */ |
| 713 | list_for_each_entry(reg, &rpm_vreg->reg_list, list) { |
| 714 | reg->req.valid |= reg->req.modified; |
| 715 | reg->req.modified = 0; |
| 716 | } |
| 717 | |
| 718 | return rc; |
| 719 | } |
| 720 | |
| 721 | static int rpm_vreg_is_enabled(struct regulator_dev *rdev) |
| 722 | { |
| 723 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 724 | |
| 725 | if (likely(!reg->use_pin_ctrl_for_enable)) |
| 726 | return reg->req.param[RPM_REGULATOR_PARAM_ENABLE]; |
| 727 | else |
| 728 | return reg->req.param[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE] |
| 729 | == reg->pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_ENABLE]; |
| 730 | } |
| 731 | |
| 732 | static int rpm_vreg_enable(struct regulator_dev *rdev) |
| 733 | { |
| 734 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 735 | int rc; |
| 736 | u32 prev_enable; |
| 737 | |
| 738 | rpm_vreg_lock(reg->rpm_vreg); |
| 739 | |
| 740 | if (likely(!reg->use_pin_ctrl_for_enable)) { |
| 741 | /* Enable using swen KVP. */ |
| 742 | prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE]; |
| 743 | RPM_VREG_SET_PARAM(reg, ENABLE, 1); |
| 744 | rc = rpm_vreg_aggregate_requests(reg); |
| 745 | if (rc) { |
| 746 | vreg_err(reg, "enable failed, rc=%d", rc); |
| 747 | RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable); |
| 748 | } |
| 749 | } else { |
| 750 | /* Enable using pcen KVP. */ |
| 751 | prev_enable |
| 752 | = reg->req.param[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE]; |
| 753 | RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE, |
| 754 | reg->pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_ENABLE]); |
| 755 | rc = rpm_vreg_aggregate_requests(reg); |
| 756 | if (rc) { |
| 757 | vreg_err(reg, "enable failed, rc=%d", rc); |
| 758 | RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE, prev_enable); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | rpm_vreg_unlock(reg->rpm_vreg); |
| 763 | |
| 764 | return rc; |
| 765 | } |
| 766 | |
| 767 | static int rpm_vreg_disable(struct regulator_dev *rdev) |
| 768 | { |
| 769 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 770 | int rc; |
| 771 | u32 prev_enable; |
| 772 | |
| 773 | rpm_vreg_lock(reg->rpm_vreg); |
| 774 | |
| 775 | if (likely(!reg->use_pin_ctrl_for_enable)) { |
| 776 | /* Disable using swen KVP. */ |
| 777 | prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE]; |
| 778 | RPM_VREG_SET_PARAM(reg, ENABLE, 0); |
| 779 | rc = rpm_vreg_aggregate_requests(reg); |
| 780 | if (rc) { |
| 781 | vreg_err(reg, "disable failed, rc=%d", rc); |
| 782 | RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable); |
| 783 | } |
| 784 | } else { |
| 785 | /* Disable using pcen KVP. */ |
| 786 | prev_enable |
| 787 | = reg->req.param[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE]; |
| 788 | RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE, |
| 789 | reg->pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_DISABLE]); |
| 790 | rc = rpm_vreg_aggregate_requests(reg); |
| 791 | if (rc) { |
| 792 | vreg_err(reg, "disable failed, rc=%d", rc); |
| 793 | RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE, prev_enable); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | rpm_vreg_unlock(reg->rpm_vreg); |
| 798 | |
| 799 | return rc; |
| 800 | } |
| 801 | |
| 802 | #define RPM_VREG_SET_VOLTAGE(_regulator, _val) \ |
| 803 | { \ |
| 804 | (_regulator)->req.param[(_regulator)->voltage_index] = _val; \ |
| 805 | (_regulator)->req.modified |= BIT((_regulator)->voltage_index); \ |
| 806 | } \ |
| 807 | |
| 808 | static int rpm_vreg_set_voltage(struct regulator_dev *rdev, int min_uV, |
| 809 | int max_uV, unsigned *selector) |
| 810 | { |
| 811 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 812 | int rc = 0; |
| 813 | int voltage; |
| 814 | u32 prev_voltage; |
| 815 | |
| 816 | voltage = min_uV - reg->voltage_offset; |
| 817 | |
| 818 | if (voltage < params[reg->voltage_index].min |
| 819 | || voltage > params[reg->voltage_index].max) { |
| 820 | vreg_err(reg, "voltage=%d for key=%s is not within allowed range: [%u, %u]\n", |
| 821 | voltage, params[reg->voltage_index].name, |
| 822 | params[reg->voltage_index].min, |
| 823 | params[reg->voltage_index].max); |
| 824 | return -EINVAL; |
| 825 | } |
| 826 | |
| 827 | rpm_vreg_lock(reg->rpm_vreg); |
| 828 | |
| 829 | prev_voltage = reg->req.param[reg->voltage_index]; |
| 830 | RPM_VREG_SET_VOLTAGE(reg, voltage); |
| 831 | |
| 832 | rpm_vreg_check_param_max(reg, reg->voltage_index, |
| 833 | max_uV - reg->voltage_offset); |
| 834 | |
| 835 | /* |
| 836 | * Only send a new voltage if the regulator is currently enabled or |
| 837 | * if the regulator has been configured to always send voltage updates. |
| 838 | */ |
| 839 | if (reg->always_send_voltage |
| 840 | || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg) |
| 841 | || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg)) |
| 842 | rc = rpm_vreg_aggregate_requests(reg); |
| 843 | |
| 844 | if (rc) { |
| 845 | vreg_err(reg, "set voltage for key=%s failed, rc=%d", |
| 846 | params[reg->voltage_index].name, rc); |
| 847 | RPM_VREG_SET_VOLTAGE(reg, prev_voltage); |
| 848 | } |
| 849 | |
| 850 | rpm_vreg_unlock(reg->rpm_vreg); |
| 851 | |
| 852 | return rc; |
| 853 | } |
| 854 | |
| 855 | static int rpm_vreg_get_voltage(struct regulator_dev *rdev) |
| 856 | { |
| 857 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 858 | int uV; |
| 859 | |
| 860 | uV = reg->req.param[reg->voltage_index] + reg->voltage_offset; |
| 861 | if (uV == 0) |
| 862 | uV = VOLTAGE_UNKNOWN; |
| 863 | |
| 864 | return uV; |
| 865 | } |
| 866 | |
| 867 | static int rpm_vreg_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 868 | { |
| 869 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 870 | int rc = 0; |
| 871 | u32 prev_current; |
| 872 | int prev_uA; |
| 873 | |
| 874 | rpm_vreg_lock(reg->rpm_vreg); |
| 875 | |
| 876 | prev_current = reg->req.param[RPM_REGULATOR_PARAM_CURRENT]; |
| 877 | prev_uA = MILLI_TO_MICRO(prev_current); |
| 878 | |
| 879 | if (mode == REGULATOR_MODE_NORMAL) { |
| 880 | /* Make sure that request current is in HPM range. */ |
| 881 | if (prev_uA < rpm_vreg_hpm_min_uA(reg->rpm_vreg)) |
| 882 | RPM_VREG_SET_PARAM(reg, CURRENT, |
| 883 | MICRO_TO_MILLI(rpm_vreg_hpm_min_uA(reg->rpm_vreg))); |
| 884 | } else if (REGULATOR_MODE_IDLE) { |
| 885 | /* Make sure that request current is in LPM range. */ |
| 886 | if (prev_uA > rpm_vreg_lpm_max_uA(reg->rpm_vreg)) |
| 887 | RPM_VREG_SET_PARAM(reg, CURRENT, |
| 888 | MICRO_TO_MILLI(rpm_vreg_lpm_max_uA(reg->rpm_vreg))); |
| 889 | } else { |
| 890 | vreg_err(reg, "invalid mode: %u\n", mode); |
| 891 | rpm_vreg_unlock(reg->rpm_vreg); |
| 892 | return -EINVAL; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Only send a new load current value if the regulator is currently |
| 897 | * enabled or if the regulator has been configured to always send |
| 898 | * current updates. |
| 899 | */ |
| 900 | if (reg->always_send_current |
| 901 | || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg) |
| 902 | || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg)) |
| 903 | rc = rpm_vreg_aggregate_requests(reg); |
| 904 | |
| 905 | if (rc) { |
| 906 | vreg_err(reg, "set mode failed, rc=%d\n", rc); |
| 907 | RPM_VREG_SET_PARAM(reg, CURRENT, prev_current); |
| 908 | } |
| 909 | |
| 910 | rpm_vreg_unlock(reg->rpm_vreg); |
| 911 | |
| 912 | return rc; |
| 913 | } |
| 914 | |
| 915 | static unsigned int rpm_vreg_get_mode(struct regulator_dev *rdev) |
| 916 | { |
| 917 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 918 | |
| 919 | return (reg->req.param[RPM_REGULATOR_PARAM_CURRENT] |
| 920 | >= MICRO_TO_MILLI(reg->rpm_vreg->hpm_min_load)) |
| 921 | ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE; |
| 922 | } |
| 923 | |
| 924 | static unsigned int rpm_vreg_get_optimum_mode(struct regulator_dev *rdev, |
| 925 | int input_uV, int output_uV, int load_uA) |
| 926 | { |
| 927 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 928 | u32 load_mA; |
| 929 | |
| 930 | load_uA += reg->system_load; |
| 931 | |
| 932 | load_mA = MICRO_TO_MILLI(load_uA); |
| 933 | if (load_mA > params[RPM_REGULATOR_PARAM_CURRENT].max) |
| 934 | load_mA = params[RPM_REGULATOR_PARAM_CURRENT].max; |
| 935 | |
| 936 | rpm_vreg_lock(reg->rpm_vreg); |
| 937 | RPM_VREG_SET_PARAM(reg, CURRENT, load_mA); |
| 938 | rpm_vreg_unlock(reg->rpm_vreg); |
| 939 | |
| 940 | return (load_uA >= reg->rpm_vreg->hpm_min_load) |
| 941 | ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE; |
| 942 | } |
| 943 | |
| 944 | static int rpm_vreg_set_bob_mode(struct regulator_dev *rdev, unsigned int mode) |
| 945 | { |
| 946 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 947 | int rc; |
| 948 | u32 prev_mode; |
| 949 | |
| 950 | rpm_vreg_lock(reg->rpm_vreg); |
| 951 | |
| 952 | prev_mode = reg->req.param[RPM_REGULATOR_PARAM_MODE_BOB]; |
| 953 | |
| 954 | switch (mode) { |
| 955 | case REGULATOR_MODE_FAST: |
| 956 | RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_PWM); |
| 957 | break; |
| 958 | case REGULATOR_MODE_NORMAL: |
| 959 | RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_AUTO); |
| 960 | break; |
| 961 | case REGULATOR_MODE_IDLE: |
| 962 | RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_PFM); |
| 963 | break; |
| 964 | case REGULATOR_MODE_STANDBY: |
| 965 | RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_PASS); |
| 966 | break; |
| 967 | default: |
| 968 | vreg_err(reg, "invalid mode: %u\n", mode); |
| 969 | rpm_vreg_unlock(reg->rpm_vreg); |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | |
| 973 | rc = rpm_vreg_aggregate_requests(reg); |
| 974 | if (rc) { |
| 975 | vreg_err(reg, "set BoB mode failed, rc=%d\n", rc); |
| 976 | RPM_VREG_SET_PARAM(reg, MODE_BOB, prev_mode); |
| 977 | } |
| 978 | |
| 979 | rpm_vreg_unlock(reg->rpm_vreg); |
| 980 | |
| 981 | return rc; |
| 982 | } |
| 983 | |
| 984 | static unsigned int rpm_vreg_get_bob_mode(struct regulator_dev *rdev) |
| 985 | { |
| 986 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 987 | unsigned int mode; |
| 988 | |
| 989 | switch (reg->req.param[RPM_REGULATOR_PARAM_MODE_BOB]) { |
| 990 | case RPM_REGULATOR_BOB_MODE_PWM: |
| 991 | mode = REGULATOR_MODE_FAST; |
| 992 | break; |
| 993 | case RPM_REGULATOR_BOB_MODE_AUTO: |
| 994 | mode = REGULATOR_MODE_NORMAL; |
| 995 | break; |
| 996 | case RPM_REGULATOR_BOB_MODE_PFM: |
| 997 | mode = REGULATOR_MODE_IDLE; |
| 998 | break; |
| 999 | case RPM_REGULATOR_BOB_MODE_PASS: |
| 1000 | mode = REGULATOR_MODE_STANDBY; |
| 1001 | break; |
| 1002 | default: |
| 1003 | vreg_err(reg, "BoB mode unknown\n"); |
| 1004 | mode = REGULATOR_MODE_NORMAL; |
| 1005 | } |
| 1006 | |
| 1007 | return mode; |
| 1008 | } |
| 1009 | |
| 1010 | static int rpm_vreg_enable_time(struct regulator_dev *rdev) |
| 1011 | { |
| 1012 | struct rpm_regulator *reg = rdev_get_drvdata(rdev); |
| 1013 | |
| 1014 | return reg->rpm_vreg->enable_time; |
| 1015 | } |
| 1016 | |
| 1017 | static int rpm_vreg_send_defaults(struct rpm_regulator *reg) |
| 1018 | { |
| 1019 | int rc; |
| 1020 | |
| 1021 | rpm_vreg_lock(reg->rpm_vreg); |
| 1022 | rc = rpm_vreg_aggregate_requests(reg); |
| 1023 | if (rc) |
| 1024 | vreg_err(reg, "RPM request failed, rc=%d", rc); |
| 1025 | rpm_vreg_unlock(reg->rpm_vreg); |
| 1026 | |
| 1027 | return rc; |
| 1028 | } |
| 1029 | |
| 1030 | static int rpm_vreg_configure_pin_control_enable(struct rpm_regulator *reg, |
| 1031 | struct device_node *node) |
| 1032 | { |
| 1033 | struct rpm_regulator_param *pcen_param = |
| 1034 | ¶ms[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE]; |
| 1035 | int rc, i; |
| 1036 | |
| 1037 | if (!of_find_property(node, "qcom,enable-with-pin-ctrl", NULL)) |
| 1038 | return 0; |
| 1039 | |
| 1040 | if (pcen_param->supported_regulator_types |
| 1041 | & BIT(reg->rpm_vreg->regulator_type)) { |
| 1042 | rc = of_property_read_u32_array(node, |
| 1043 | "qcom,enable-with-pin-ctrl", reg->pin_ctrl_mask, |
| 1044 | RPM_VREG_PIN_CTRL_STATE_COUNT); |
| 1045 | if (rc) { |
| 1046 | vreg_err(reg, "could not read qcom,enable-with-pin-ctrl, rc=%d\n", |
| 1047 | rc); |
| 1048 | return rc; |
| 1049 | } |
| 1050 | |
| 1051 | /* Verify that the mask values are valid. */ |
| 1052 | for (i = 0; i < RPM_VREG_PIN_CTRL_STATE_COUNT; i++) { |
| 1053 | if (reg->pin_ctrl_mask[i] < pcen_param->min |
| 1054 | || reg->pin_ctrl_mask[i] > pcen_param->max) { |
| 1055 | vreg_err(reg, "device tree property: qcom,enable-with-pin-ctrl[%d]=%u is outside allowed range [%u, %u]\n", |
| 1056 | i, reg->pin_ctrl_mask[i], |
| 1057 | pcen_param->min, pcen_param->max); |
| 1058 | return -EINVAL; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | reg->use_pin_ctrl_for_enable = true; |
| 1063 | } else { |
| 1064 | pr_warn("%s: regulator type=%d does not support device tree property: qcom,enable-with-pin-ctrl\n", |
| 1065 | reg->rdesc.name, reg->rpm_vreg->regulator_type); |
| 1066 | } |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator |
| 1073 | * @dev: device for regulator consumer |
| 1074 | * @supply: supply name |
| 1075 | * |
| 1076 | * Returns a struct rpm_regulator corresponding to the regulator producer, |
| 1077 | * or ERR_PTR() containing errno. |
| 1078 | * |
| 1079 | * This function may only be called from nonatomic context. |
| 1080 | */ |
| 1081 | struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply) |
| 1082 | { |
| 1083 | struct rpm_regulator *framework_reg; |
| 1084 | struct rpm_regulator *priv_reg = NULL; |
| 1085 | struct regulator *regulator; |
| 1086 | struct rpm_vreg *rpm_vreg; |
| 1087 | |
| 1088 | regulator = regulator_get(dev, supply); |
| 1089 | if (IS_ERR(regulator)) { |
| 1090 | pr_err("could not find regulator for: dev=%s, supply=%s, rc=%ld\n", |
| 1091 | (dev ? dev_name(dev) : ""), (supply ? supply : ""), |
| 1092 | PTR_ERR(regulator)); |
| 1093 | return ERR_CAST(regulator); |
| 1094 | } |
| 1095 | |
| 1096 | framework_reg = regulator_get_drvdata(regulator); |
| 1097 | if (framework_reg == NULL) { |
| 1098 | pr_err("regulator structure not found.\n"); |
| 1099 | regulator_put(regulator); |
| 1100 | return ERR_PTR(-ENODEV); |
| 1101 | } |
| 1102 | regulator_put(regulator); |
| 1103 | |
| 1104 | rpm_vreg = framework_reg->rpm_vreg; |
| 1105 | |
| 1106 | priv_reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL); |
| 1107 | if (priv_reg == NULL) { |
| 1108 | vreg_err(framework_reg, |
| 1109 | "could not allocate memory for regulator\n"); |
| 1110 | return ERR_PTR(-ENOMEM); |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Allocate a regulator_dev struct so that framework callback functions |
| 1115 | * can be called from the private API functions. |
| 1116 | */ |
| 1117 | priv_reg->rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
| 1118 | if (priv_reg->rdev == NULL) { |
| 1119 | vreg_err(framework_reg, |
| 1120 | "could not allocate memory for regulator_dev\n"); |
| 1121 | kfree(priv_reg); |
| 1122 | return ERR_PTR(-ENOMEM); |
| 1123 | } |
| 1124 | priv_reg->rdev->reg_data = priv_reg; |
| 1125 | priv_reg->rpm_vreg = rpm_vreg; |
| 1126 | priv_reg->rdesc.name = framework_reg->rdesc.name; |
| 1127 | priv_reg->rdesc.ops = framework_reg->rdesc.ops; |
| 1128 | priv_reg->set_active = framework_reg->set_active; |
| 1129 | priv_reg->set_sleep = framework_reg->set_sleep; |
| 1130 | priv_reg->min_uV = framework_reg->min_uV; |
| 1131 | priv_reg->max_uV = framework_reg->max_uV; |
| 1132 | priv_reg->system_load = framework_reg->system_load; |
| 1133 | |
| 1134 | might_sleep_if(!rpm_vreg->allow_atomic); |
| 1135 | rpm_vreg_lock(rpm_vreg); |
| 1136 | list_add(&priv_reg->list, &rpm_vreg->reg_list); |
| 1137 | rpm_vreg_unlock(rpm_vreg); |
| 1138 | |
| 1139 | return priv_reg; |
| 1140 | } |
| 1141 | EXPORT_SYMBOL(rpm_regulator_get); |
| 1142 | |
| 1143 | static int rpm_regulator_check_input(struct rpm_regulator *regulator) |
| 1144 | { |
| 1145 | if (IS_ERR_OR_NULL(regulator) || regulator->rpm_vreg == NULL) { |
| 1146 | pr_err("invalid rpm_regulator pointer\n"); |
| 1147 | return -EINVAL; |
| 1148 | } |
| 1149 | |
| 1150 | might_sleep_if(!regulator->rpm_vreg->allow_atomic); |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * rpm_regulator_put() - free the RPM regulator handle |
| 1157 | * @regulator: RPM regulator handle |
| 1158 | * |
| 1159 | * Parameter reaggregation does not take place when rpm_regulator_put is called. |
| 1160 | * Therefore, regulator enable state and voltage must be configured |
| 1161 | * appropriately before calling rpm_regulator_put. |
| 1162 | * |
| 1163 | * This function may be called from either atomic or nonatomic context. If this |
| 1164 | * function is called from atomic context, then the regulator being operated on |
| 1165 | * must be configured via device tree with qcom,allow-atomic == 1. |
| 1166 | */ |
| 1167 | void rpm_regulator_put(struct rpm_regulator *regulator) |
| 1168 | { |
| 1169 | struct rpm_vreg *rpm_vreg; |
| 1170 | int rc = rpm_regulator_check_input(regulator); |
| 1171 | |
| 1172 | if (rc) |
| 1173 | return; |
| 1174 | |
| 1175 | rpm_vreg = regulator->rpm_vreg; |
| 1176 | |
| 1177 | might_sleep_if(!rpm_vreg->allow_atomic); |
| 1178 | rpm_vreg_lock(rpm_vreg); |
| 1179 | list_del(®ulator->list); |
| 1180 | rpm_vreg_unlock(rpm_vreg); |
| 1181 | |
| 1182 | kfree(regulator->rdev); |
| 1183 | kfree(regulator); |
| 1184 | } |
| 1185 | EXPORT_SYMBOL(rpm_regulator_put); |
| 1186 | |
| 1187 | /** |
| 1188 | * rpm_regulator_enable() - enable regulator output |
| 1189 | * @regulator: RPM regulator handle |
| 1190 | * |
| 1191 | * Returns 0 on success or errno on failure. |
| 1192 | * |
| 1193 | * This function may be called from either atomic or nonatomic context. If this |
| 1194 | * function is called from atomic context, then the regulator being operated on |
| 1195 | * must be configured via device tree with qcom,allow-atomic == 1. |
| 1196 | */ |
| 1197 | int rpm_regulator_enable(struct rpm_regulator *regulator) |
| 1198 | { |
| 1199 | int rc = rpm_regulator_check_input(regulator); |
| 1200 | |
| 1201 | if (rc) |
| 1202 | return rc; |
| 1203 | |
| 1204 | return rpm_vreg_enable(regulator->rdev); |
| 1205 | } |
| 1206 | EXPORT_SYMBOL(rpm_regulator_enable); |
| 1207 | |
| 1208 | /** |
| 1209 | * rpm_regulator_disable() - disable regulator output |
| 1210 | * @regulator: RPM regulator handle |
| 1211 | * |
| 1212 | * Returns 0 on success or errno on failure. |
| 1213 | * |
| 1214 | * The enable state of the regulator is determined by aggregating the requests |
| 1215 | * of all consumers. Therefore, it is possible that the regulator will remain |
| 1216 | * enabled even after rpm_regulator_disable is called. |
| 1217 | * |
| 1218 | * This function may be called from either atomic or nonatomic context. If this |
| 1219 | * function is called from atomic context, then the regulator being operated on |
| 1220 | * must be configured via device tree with qcom,allow-atomic == 1. |
| 1221 | */ |
| 1222 | int rpm_regulator_disable(struct rpm_regulator *regulator) |
| 1223 | { |
| 1224 | int rc = rpm_regulator_check_input(regulator); |
| 1225 | |
| 1226 | if (rc) |
| 1227 | return rc; |
| 1228 | |
| 1229 | return rpm_vreg_disable(regulator->rdev); |
| 1230 | } |
| 1231 | EXPORT_SYMBOL(rpm_regulator_disable); |
| 1232 | |
| 1233 | /** |
| 1234 | * rpm_regulator_set_voltage() - set regulator output voltage |
| 1235 | * @regulator: RPM regulator handle |
| 1236 | * @min_uV: minimum required voltage in uV |
| 1237 | * @max_uV: maximum acceptable voltage in uV |
| 1238 | * |
| 1239 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 1240 | * while the regulator is disabled or enabled. If the regulator is enabled then |
| 1241 | * the voltage will change to the new value immediately; otherwise, if the |
| 1242 | * regulator is disabled, then the regulator will output at the new voltage when |
| 1243 | * enabled. |
| 1244 | * |
| 1245 | * The min_uV to max_uV voltage range requested must intersect with the |
| 1246 | * voltage constraint range configured for the regulator. |
| 1247 | * |
| 1248 | * Returns 0 on success or errno on failure. |
| 1249 | * |
| 1250 | * The final voltage value that is sent to the RPM is aggregated based upon the |
| 1251 | * values requested by all consumers of the regulator. This corresponds to the |
| 1252 | * maximum min_uV value. |
| 1253 | * |
| 1254 | * This function may be called from either atomic or nonatomic context. If this |
| 1255 | * function is called from atomic context, then the regulator being operated on |
| 1256 | * must be configured via device tree with qcom,allow-atomic == 1. |
| 1257 | */ |
| 1258 | int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV, |
| 1259 | int max_uV) |
| 1260 | { |
| 1261 | int rc = rpm_regulator_check_input(regulator); |
| 1262 | int uV = min_uV; |
| 1263 | |
| 1264 | if (rc) |
| 1265 | return rc; |
| 1266 | |
| 1267 | if (regulator->rpm_vreg->regulator_type == RPM_REGULATOR_TYPE_VS) { |
| 1268 | vreg_err(regulator, "unsupported regulator type: %d\n", |
| 1269 | regulator->rpm_vreg->regulator_type); |
| 1270 | return -EINVAL; |
| 1271 | } |
| 1272 | |
| 1273 | if (min_uV > max_uV) { |
| 1274 | vreg_err(regulator, "min_uV=%d must be less than max_uV=%d\n", |
| 1275 | min_uV, max_uV); |
| 1276 | return -EINVAL; |
| 1277 | } |
| 1278 | |
| 1279 | if (uV < regulator->min_uV && max_uV >= regulator->min_uV) |
| 1280 | uV = regulator->min_uV; |
| 1281 | |
| 1282 | if (uV < regulator->min_uV || uV > regulator->max_uV) { |
| 1283 | vreg_err(regulator, |
| 1284 | "request v=[%d, %d] is outside allowed v=[%d, %d]\n", |
| 1285 | min_uV, max_uV, regulator->min_uV, regulator->max_uV); |
| 1286 | return -EINVAL; |
| 1287 | } |
| 1288 | |
| 1289 | return regulator->rdesc.ops->set_voltage(regulator->rdev, uV, uV, NULL); |
| 1290 | } |
| 1291 | EXPORT_SYMBOL(rpm_regulator_set_voltage); |
| 1292 | |
| 1293 | /** |
| 1294 | * rpm_regulator_set_mode() - set regulator operating mode |
| 1295 | * @regulator: RPM regulator handle |
| 1296 | * @mode: operating mode requested for the regulator |
| 1297 | * |
| 1298 | * Requests that the mode of the regulator be set to the mode specified. This |
| 1299 | * parameter is aggregated using a max function such that AUTO < IPEAK < HPM. |
| 1300 | * |
| 1301 | * Returns 0 on success or errno on failure. |
| 1302 | */ |
| 1303 | int rpm_regulator_set_mode(struct rpm_regulator *regulator, |
| 1304 | enum rpm_regulator_mode mode) |
| 1305 | { |
| 1306 | int index = 0; |
| 1307 | u32 new_mode, prev_mode; |
| 1308 | int rc; |
| 1309 | |
| 1310 | rc = rpm_regulator_check_input(regulator); |
| 1311 | if (rc) |
| 1312 | return rc; |
| 1313 | |
| 1314 | if (mode < 0 || mode >= ARRAY_SIZE(mode_mapping)) { |
| 1315 | vreg_err(regulator, "invalid mode requested: %d\n", mode); |
| 1316 | return -EINVAL; |
| 1317 | } |
| 1318 | |
| 1319 | switch (regulator->rpm_vreg->regulator_type) { |
| 1320 | case RPM_REGULATOR_TYPE_SMPS: |
| 1321 | index = RPM_REGULATOR_PARAM_MODE_SMPS; |
| 1322 | new_mode = mode_mapping[mode].smps_mode; |
| 1323 | break; |
| 1324 | case RPM_REGULATOR_TYPE_LDO: |
| 1325 | index = RPM_REGULATOR_PARAM_MODE_LDO; |
| 1326 | new_mode = mode_mapping[mode].ldo_mode; |
| 1327 | break; |
| 1328 | default: |
| 1329 | vreg_err(regulator, "unsupported regulator type: %d\n", |
| 1330 | regulator->rpm_vreg->regulator_type); |
| 1331 | return -EINVAL; |
| 1332 | }; |
| 1333 | |
| 1334 | if (new_mode < params[index].min || new_mode > params[index].max) { |
| 1335 | vreg_err(regulator, "invalid mode requested: %d for type: %d\n", |
| 1336 | mode, regulator->rpm_vreg->regulator_type); |
| 1337 | return -EINVAL; |
| 1338 | } |
| 1339 | |
| 1340 | rpm_vreg_lock(regulator->rpm_vreg); |
| 1341 | |
| 1342 | prev_mode = regulator->req.param[index]; |
| 1343 | regulator->req.param[index] = new_mode; |
| 1344 | regulator->req.modified |= BIT(index); |
| 1345 | |
| 1346 | rc = rpm_vreg_aggregate_requests(regulator); |
| 1347 | if (rc) { |
| 1348 | vreg_err(regulator, "set mode failed, rc=%d", rc); |
| 1349 | regulator->req.param[index] = prev_mode; |
| 1350 | } |
| 1351 | |
| 1352 | rpm_vreg_unlock(regulator->rpm_vreg); |
| 1353 | |
| 1354 | return rc; |
| 1355 | } |
| 1356 | EXPORT_SYMBOL(rpm_regulator_set_mode); |
| 1357 | |
| 1358 | static struct regulator_ops ldo_ops = { |
| 1359 | .enable = rpm_vreg_enable, |
| 1360 | .disable = rpm_vreg_disable, |
| 1361 | .is_enabled = rpm_vreg_is_enabled, |
| 1362 | .set_voltage = rpm_vreg_set_voltage, |
| 1363 | .get_voltage = rpm_vreg_get_voltage, |
| 1364 | .set_mode = rpm_vreg_set_mode, |
| 1365 | .get_mode = rpm_vreg_get_mode, |
| 1366 | .get_optimum_mode = rpm_vreg_get_optimum_mode, |
| 1367 | .enable_time = rpm_vreg_enable_time, |
| 1368 | }; |
| 1369 | |
| 1370 | static struct regulator_ops smps_ops = { |
| 1371 | .enable = rpm_vreg_enable, |
| 1372 | .disable = rpm_vreg_disable, |
| 1373 | .is_enabled = rpm_vreg_is_enabled, |
| 1374 | .set_voltage = rpm_vreg_set_voltage, |
| 1375 | .get_voltage = rpm_vreg_get_voltage, |
| 1376 | .set_mode = rpm_vreg_set_mode, |
| 1377 | .get_mode = rpm_vreg_get_mode, |
| 1378 | .get_optimum_mode = rpm_vreg_get_optimum_mode, |
| 1379 | .enable_time = rpm_vreg_enable_time, |
| 1380 | }; |
| 1381 | |
| 1382 | static struct regulator_ops switch_ops = { |
| 1383 | .enable = rpm_vreg_enable, |
| 1384 | .disable = rpm_vreg_disable, |
| 1385 | .is_enabled = rpm_vreg_is_enabled, |
| 1386 | .enable_time = rpm_vreg_enable_time, |
| 1387 | }; |
| 1388 | |
| 1389 | static struct regulator_ops ncp_ops = { |
| 1390 | .enable = rpm_vreg_enable, |
| 1391 | .disable = rpm_vreg_disable, |
| 1392 | .is_enabled = rpm_vreg_is_enabled, |
| 1393 | .set_voltage = rpm_vreg_set_voltage, |
| 1394 | .get_voltage = rpm_vreg_get_voltage, |
| 1395 | .enable_time = rpm_vreg_enable_time, |
| 1396 | }; |
| 1397 | |
| 1398 | static struct regulator_ops bob_ops = { |
| 1399 | .enable = rpm_vreg_enable, |
| 1400 | .disable = rpm_vreg_disable, |
| 1401 | .is_enabled = rpm_vreg_is_enabled, |
| 1402 | .set_voltage = rpm_vreg_set_voltage, |
| 1403 | .get_voltage = rpm_vreg_get_voltage, |
| 1404 | .set_mode = rpm_vreg_set_bob_mode, |
| 1405 | .get_mode = rpm_vreg_get_bob_mode, |
| 1406 | .enable_time = rpm_vreg_enable_time, |
| 1407 | }; |
| 1408 | |
| 1409 | static struct regulator_ops *vreg_ops[] = { |
| 1410 | [RPM_REGULATOR_TYPE_LDO] = &ldo_ops, |
| 1411 | [RPM_REGULATOR_TYPE_SMPS] = &smps_ops, |
| 1412 | [RPM_REGULATOR_TYPE_VS] = &switch_ops, |
| 1413 | [RPM_REGULATOR_TYPE_NCP] = &ncp_ops, |
| 1414 | [RPM_REGULATOR_TYPE_BOB] = &bob_ops, |
| 1415 | }; |
| 1416 | |
| 1417 | static int rpm_vreg_device_remove(struct platform_device *pdev) |
| 1418 | { |
| 1419 | struct device *dev = &pdev->dev; |
| 1420 | struct rpm_regulator *reg; |
| 1421 | struct rpm_vreg *rpm_vreg; |
| 1422 | |
| 1423 | reg = platform_get_drvdata(pdev); |
| 1424 | if (reg) { |
| 1425 | rpm_vreg = reg->rpm_vreg; |
| 1426 | rpm_vreg_lock(rpm_vreg); |
| 1427 | regulator_unregister(reg->rdev); |
| 1428 | list_del(®->list); |
| 1429 | kfree(reg); |
| 1430 | rpm_vreg_unlock(rpm_vreg); |
| 1431 | } else { |
| 1432 | dev_err(dev, "%s: drvdata missing\n", __func__); |
| 1433 | return -EINVAL; |
| 1434 | } |
| 1435 | |
| 1436 | platform_set_drvdata(pdev, NULL); |
| 1437 | |
| 1438 | return 0; |
| 1439 | } |
| 1440 | |
| 1441 | static int rpm_vreg_resource_remove(struct platform_device *pdev) |
| 1442 | { |
| 1443 | struct device *dev = &pdev->dev; |
| 1444 | struct rpm_regulator *reg, *reg_temp; |
| 1445 | struct rpm_vreg *rpm_vreg; |
| 1446 | |
| 1447 | rpm_vreg = platform_get_drvdata(pdev); |
| 1448 | if (rpm_vreg) { |
| 1449 | rpm_vreg_lock(rpm_vreg); |
| 1450 | list_for_each_entry_safe(reg, reg_temp, &rpm_vreg->reg_list, |
| 1451 | list) { |
| 1452 | /* Only touch data for private consumers. */ |
| 1453 | if (reg->rdev->desc == NULL) { |
| 1454 | list_del(®->list); |
| 1455 | kfree(reg->rdev); |
| 1456 | kfree(reg); |
| 1457 | } else { |
| 1458 | dev_err(dev, "%s: not all child devices have been removed\n", |
| 1459 | __func__); |
| 1460 | } |
| 1461 | } |
| 1462 | rpm_vreg_unlock(rpm_vreg); |
| 1463 | |
| 1464 | msm_rpm_free_request(rpm_vreg->handle_active); |
| 1465 | msm_rpm_free_request(rpm_vreg->handle_sleep); |
| 1466 | |
| 1467 | kfree(rpm_vreg); |
| 1468 | } else { |
| 1469 | dev_err(dev, "%s: drvdata missing\n", __func__); |
| 1470 | return -EINVAL; |
| 1471 | } |
| 1472 | |
| 1473 | platform_set_drvdata(pdev, NULL); |
| 1474 | |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | static int rpm_vreg_set_smps_ldo_voltage_index(struct device *dev, |
| 1479 | struct rpm_regulator *reg) |
| 1480 | { |
| 1481 | struct device_node *node = dev->of_node; |
| 1482 | int chosen = 0; |
| 1483 | |
| 1484 | if (of_property_read_bool(node, "qcom,use-voltage-corner")) { |
| 1485 | reg->voltage_index = RPM_REGULATOR_PARAM_CORNER; |
| 1486 | reg->voltage_offset = RPM_REGULATOR_CORNER_NONE; |
| 1487 | chosen++; |
| 1488 | } |
| 1489 | |
| 1490 | if (of_property_read_bool(node, "qcom,use-voltage-floor-corner")) { |
| 1491 | reg->voltage_index = RPM_REGULATOR_PARAM_FLOOR_CORNER; |
| 1492 | reg->voltage_offset = RPM_REGULATOR_CORNER_NONE; |
| 1493 | chosen++; |
| 1494 | } |
| 1495 | |
| 1496 | if (of_property_read_bool(node, "qcom,use-voltage-level")) { |
| 1497 | reg->voltage_index = RPM_REGULATOR_PARAM_LEVEL; |
| 1498 | chosen++; |
| 1499 | } |
| 1500 | |
| 1501 | if (of_property_read_bool(node, "qcom,use-voltage-floor-level")) { |
| 1502 | reg->voltage_index = RPM_REGULATOR_PARAM_FLOOR_LEVEL; |
| 1503 | chosen++; |
| 1504 | } |
| 1505 | |
| 1506 | if (chosen > 1) { |
| 1507 | dev_err(dev, "only one qcom,use-voltage-* may be specified\n"); |
| 1508 | return -EINVAL; |
| 1509 | } |
| 1510 | |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static int rpm_vreg_set_bob_voltage_index(struct device *dev, |
| 1515 | struct rpm_regulator *reg) |
| 1516 | { |
| 1517 | struct device_node *node = dev->of_node; |
| 1518 | int chosen = 0; |
| 1519 | |
| 1520 | if (of_property_read_bool(node, "qcom,use-pin-ctrl-voltage1")) { |
| 1521 | reg->voltage_index = RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE1; |
| 1522 | chosen++; |
| 1523 | } |
| 1524 | |
| 1525 | if (of_property_read_bool(node, "qcom,use-pin-ctrl-voltage2")) { |
| 1526 | reg->voltage_index = RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE2; |
| 1527 | chosen++; |
| 1528 | } |
| 1529 | |
| 1530 | if (of_property_read_bool(node, "qcom,use-pin-ctrl-voltage3")) { |
| 1531 | reg->voltage_index = RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE3; |
| 1532 | chosen++; |
| 1533 | } |
| 1534 | |
| 1535 | if (chosen > 1) { |
| 1536 | dev_err(dev, "only one qcom,use-pin-ctrl-voltage* may be specified\n"); |
| 1537 | return -EINVAL; |
| 1538 | } |
| 1539 | |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | static int rpm_vreg_device_set_voltage_index(struct device *dev, |
| 1544 | struct rpm_regulator *reg, int type) |
| 1545 | { |
| 1546 | int rc = 0; |
| 1547 | |
| 1548 | reg->voltage_index = RPM_REGULATOR_PARAM_VOLTAGE; |
| 1549 | |
| 1550 | switch (type) { |
| 1551 | case RPM_REGULATOR_TYPE_SMPS: |
| 1552 | case RPM_REGULATOR_TYPE_LDO: |
| 1553 | rc = rpm_vreg_set_smps_ldo_voltage_index(dev, reg); |
| 1554 | break; |
| 1555 | case RPM_REGULATOR_TYPE_BOB: |
| 1556 | rc = rpm_vreg_set_bob_voltage_index(dev, reg); |
| 1557 | break; |
| 1558 | } |
| 1559 | |
| 1560 | return rc; |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * This probe is called for child rpm-regulator devices which have |
| 1565 | * properties which are required to configure individual regulator |
| 1566 | * framework regulators for a given RPM regulator resource. |
| 1567 | */ |
| 1568 | static int rpm_vreg_device_probe(struct platform_device *pdev) |
| 1569 | { |
| 1570 | struct device *dev = &pdev->dev; |
| 1571 | struct device_node *node = dev->of_node; |
| 1572 | struct regulator_init_data *init_data; |
| 1573 | struct rpm_vreg *rpm_vreg; |
| 1574 | struct rpm_regulator *reg; |
| 1575 | struct regulator_config reg_config = {}; |
| 1576 | int rc = 0; |
| 1577 | int i, regulator_type; |
| 1578 | u32 val; |
| 1579 | |
| 1580 | if (!dev->of_node) { |
| 1581 | dev_err(dev, "%s: device tree information missing\n", __func__); |
| 1582 | return -ENODEV; |
| 1583 | } |
| 1584 | |
| 1585 | if (pdev->dev.parent == NULL) { |
| 1586 | dev_err(dev, "%s: parent device missing\n", __func__); |
| 1587 | return -ENODEV; |
| 1588 | } |
| 1589 | |
| 1590 | rpm_vreg = dev_get_drvdata(pdev->dev.parent); |
| 1591 | if (rpm_vreg == NULL) { |
| 1592 | dev_err(dev, "%s: rpm_vreg not found in parent device\n", |
| 1593 | __func__); |
| 1594 | return -ENODEV; |
| 1595 | } |
| 1596 | |
| 1597 | reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL); |
| 1598 | if (reg == NULL) { |
| 1599 | dev_err(dev, "%s: could not allocate memory for reg\n", |
| 1600 | __func__); |
| 1601 | return -ENOMEM; |
| 1602 | } |
| 1603 | |
| 1604 | regulator_type = rpm_vreg->regulator_type; |
| 1605 | reg->rpm_vreg = rpm_vreg; |
| 1606 | reg->rdesc.owner = THIS_MODULE; |
| 1607 | reg->rdesc.type = REGULATOR_VOLTAGE; |
| 1608 | reg->rdesc.ops = vreg_ops[regulator_type]; |
| 1609 | |
| 1610 | rc = rpm_vreg_device_set_voltage_index(dev, reg, regulator_type); |
| 1611 | if (rc) |
| 1612 | goto fail_free_reg; |
| 1613 | |
| 1614 | reg->always_send_voltage |
| 1615 | = of_property_read_bool(node, "qcom,always-send-voltage"); |
| 1616 | reg->always_send_current |
| 1617 | = of_property_read_bool(node, "qcom,always-send-current"); |
| 1618 | |
| 1619 | if (regulator_type == RPM_REGULATOR_TYPE_VS) |
| 1620 | reg->rdesc.n_voltages = 0; |
| 1621 | else |
| 1622 | reg->rdesc.n_voltages = 2; |
| 1623 | |
| 1624 | rc = of_property_read_u32(node, "qcom,set", &val); |
| 1625 | if (rc) { |
| 1626 | dev_err(dev, "%s: sleep set and/or active set must be configured via qcom,set property, rc=%d\n", |
| 1627 | __func__, rc); |
| 1628 | goto fail_free_reg; |
| 1629 | } else if (!(val & RPM_SET_CONFIG_BOTH)) { |
| 1630 | dev_err(dev, "%s: qcom,set=%u property is invalid\n", __func__, |
| 1631 | val); |
| 1632 | rc = -EINVAL; |
| 1633 | goto fail_free_reg; |
| 1634 | } |
| 1635 | |
| 1636 | reg->set_active = !!(val & RPM_SET_CONFIG_ACTIVE); |
| 1637 | reg->set_sleep = !!(val & RPM_SET_CONFIG_SLEEP); |
| 1638 | |
| 1639 | init_data = of_get_regulator_init_data(dev, node, ®->rdesc); |
| 1640 | if (init_data == NULL) { |
| 1641 | dev_err(dev, "%s: unable to allocate memory\n", __func__); |
| 1642 | rc = -ENOMEM; |
| 1643 | goto fail_free_reg; |
| 1644 | } |
| 1645 | if (init_data->constraints.name == NULL) { |
| 1646 | dev_err(dev, "%s: regulator name not specified\n", __func__); |
| 1647 | rc = -EINVAL; |
| 1648 | goto fail_free_reg; |
| 1649 | } |
| 1650 | |
| 1651 | init_data->constraints.input_uV = init_data->constraints.max_uV; |
| 1652 | |
| 1653 | if (of_get_property(node, "parent-supply", NULL)) |
| 1654 | init_data->supply_regulator = "parent"; |
| 1655 | |
| 1656 | /* |
| 1657 | * Fill in ops and mode masks based on callbacks specified for |
| 1658 | * this type of regulator. |
| 1659 | */ |
| 1660 | if (reg->rdesc.ops->enable) |
| 1661 | init_data->constraints.valid_ops_mask |
| 1662 | |= REGULATOR_CHANGE_STATUS; |
| 1663 | if (reg->rdesc.ops->get_voltage) |
| 1664 | init_data->constraints.valid_ops_mask |
| 1665 | |= REGULATOR_CHANGE_VOLTAGE; |
| 1666 | if (reg->rdesc.ops->get_mode) { |
| 1667 | init_data->constraints.valid_ops_mask |
| 1668 | |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS; |
| 1669 | init_data->constraints.valid_modes_mask |
| 1670 | |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE; |
| 1671 | } |
| 1672 | |
| 1673 | reg->rdesc.name = init_data->constraints.name; |
| 1674 | reg->min_uV = init_data->constraints.min_uV; |
| 1675 | reg->max_uV = init_data->constraints.max_uV; |
| 1676 | |
| 1677 | /* Initialize the param array based on optional properties. */ |
| 1678 | for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) { |
| 1679 | rc = of_property_read_u32(node, params[i].property_name, &val); |
| 1680 | if (rc == 0) { |
| 1681 | if (params[i].supported_regulator_types |
| 1682 | & BIT(regulator_type)) { |
| 1683 | if (val < params[i].min |
| 1684 | || val > params[i].max) { |
| 1685 | pr_warn("%s: device tree property: %s=%u is outsided allowed range [%u, %u]\n", |
| 1686 | reg->rdesc.name, |
| 1687 | params[i].property_name, val, |
| 1688 | params[i].min, params[i].max); |
| 1689 | continue; |
| 1690 | } |
| 1691 | reg->req.param[i] = val; |
| 1692 | reg->req.modified |= BIT(i); |
| 1693 | } else { |
| 1694 | pr_warn("%s: regulator type=%d does not support device tree property: %s\n", |
| 1695 | reg->rdesc.name, regulator_type, |
| 1696 | params[i].property_name); |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | of_property_read_u32(node, "qcom,system-load", ®->system_load); |
| 1702 | |
| 1703 | rc = rpm_vreg_configure_pin_control_enable(reg, node); |
| 1704 | if (rc) { |
| 1705 | vreg_err(reg, "could not configure pin control enable, rc=%d\n", |
| 1706 | rc); |
| 1707 | goto fail_free_reg; |
| 1708 | } |
| 1709 | |
| 1710 | rpm_vreg_lock(rpm_vreg); |
| 1711 | list_add(®->list, &rpm_vreg->reg_list); |
| 1712 | rpm_vreg_unlock(rpm_vreg); |
| 1713 | |
| 1714 | if (of_property_read_bool(node, "qcom,send-defaults")) { |
| 1715 | rc = rpm_vreg_send_defaults(reg); |
| 1716 | if (rc) { |
| 1717 | vreg_err(reg, "could not send defaults, rc=%d\n", rc); |
| 1718 | goto fail_remove_from_list; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | reg_config.dev = dev; |
| 1723 | reg_config.init_data = init_data; |
| 1724 | reg_config.of_node = node; |
| 1725 | reg_config.driver_data = reg; |
| 1726 | reg->rdev = regulator_register(®->rdesc, ®_config); |
| 1727 | if (IS_ERR(reg->rdev)) { |
| 1728 | rc = PTR_ERR(reg->rdev); |
| 1729 | reg->rdev = NULL; |
| 1730 | pr_err("regulator_register failed: %s, rc=%d\n", |
| 1731 | reg->rdesc.name, rc); |
| 1732 | goto fail_remove_from_list; |
| 1733 | } |
| 1734 | |
| 1735 | platform_set_drvdata(pdev, reg); |
| 1736 | |
| 1737 | pr_debug("successfully probed: %s\n", reg->rdesc.name); |
| 1738 | |
| 1739 | return 0; |
| 1740 | |
| 1741 | fail_remove_from_list: |
| 1742 | rpm_vreg_lock(rpm_vreg); |
| 1743 | list_del(®->list); |
| 1744 | rpm_vreg_unlock(rpm_vreg); |
| 1745 | |
| 1746 | fail_free_reg: |
| 1747 | kfree(reg); |
| 1748 | return rc; |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * This probe is called for parent rpm-regulator devices which have |
| 1753 | * properties which are required to identify a given RPM resource. |
| 1754 | */ |
| 1755 | static int rpm_vreg_resource_probe(struct platform_device *pdev) |
| 1756 | { |
| 1757 | struct device *dev = &pdev->dev; |
| 1758 | struct device_node *node = dev->of_node; |
| 1759 | struct rpm_vreg *rpm_vreg; |
| 1760 | int val = 0; |
| 1761 | u32 resource_type; |
| 1762 | int rc; |
| 1763 | |
| 1764 | if (!dev->of_node) { |
| 1765 | dev_err(dev, "%s: device tree information missing\n", __func__); |
| 1766 | return -ENODEV; |
| 1767 | } |
| 1768 | |
| 1769 | /* Create new rpm_vreg entry. */ |
| 1770 | rpm_vreg = kzalloc(sizeof(struct rpm_vreg), GFP_KERNEL); |
| 1771 | if (rpm_vreg == NULL) { |
| 1772 | dev_err(dev, "%s: could not allocate memory for vreg\n", |
| 1773 | __func__); |
| 1774 | return -ENOMEM; |
| 1775 | } |
| 1776 | |
| 1777 | /* Required device tree properties: */ |
| 1778 | rc = of_property_read_string(node, "qcom,resource-name", |
| 1779 | &rpm_vreg->resource_name); |
| 1780 | if (rc) { |
| 1781 | dev_err(dev, "%s: qcom,resource-name missing in DT node\n", |
| 1782 | __func__); |
| 1783 | goto fail_free_vreg; |
| 1784 | } |
| 1785 | resource_type = rpm_vreg_string_to_int(rpm_vreg->resource_name); |
| 1786 | |
| 1787 | rc = of_property_read_u32(node, "qcom,resource-id", |
| 1788 | &rpm_vreg->resource_id); |
| 1789 | if (rc) { |
| 1790 | dev_err(dev, "%s: qcom,resource-id missing in DT node\n", |
| 1791 | __func__); |
| 1792 | goto fail_free_vreg; |
| 1793 | } |
| 1794 | |
| 1795 | rc = of_property_read_u32(node, "qcom,regulator-type", |
| 1796 | &rpm_vreg->regulator_type); |
| 1797 | if (rc) { |
| 1798 | dev_err(dev, "%s: qcom,regulator-type missing in DT node\n", |
| 1799 | __func__); |
| 1800 | goto fail_free_vreg; |
| 1801 | } |
| 1802 | |
| 1803 | if ((rpm_vreg->regulator_type < 0) |
| 1804 | || (rpm_vreg->regulator_type >= RPM_REGULATOR_TYPE_MAX)) { |
| 1805 | dev_err(dev, "%s: invalid regulator type: %d\n", __func__, |
| 1806 | rpm_vreg->regulator_type); |
| 1807 | rc = -EINVAL; |
| 1808 | goto fail_free_vreg; |
| 1809 | } |
| 1810 | |
| 1811 | /* Optional device tree properties: */ |
| 1812 | of_property_read_u32(node, "qcom,allow-atomic", &val); |
| 1813 | rpm_vreg->allow_atomic = !!val; |
| 1814 | of_property_read_u32(node, "qcom,enable-time", &rpm_vreg->enable_time); |
| 1815 | of_property_read_u32(node, "qcom,hpm-min-load", |
| 1816 | &rpm_vreg->hpm_min_load); |
| 1817 | rpm_vreg->apps_only = of_property_read_bool(node, "qcom,apps-only"); |
| 1818 | rpm_vreg->always_wait_for_ack |
| 1819 | = of_property_read_bool(node, "qcom,always-wait-for-ack"); |
| 1820 | |
| 1821 | rpm_vreg->handle_active = msm_rpm_create_request(RPM_SET_ACTIVE, |
| 1822 | resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX); |
| 1823 | if (rpm_vreg->handle_active == NULL |
| 1824 | || IS_ERR(rpm_vreg->handle_active)) { |
| 1825 | rc = PTR_ERR(rpm_vreg->handle_active); |
| 1826 | if (rc != -EPROBE_DEFER) |
| 1827 | dev_err(dev, "%s: failed to create active RPM handle, rc=%d\n", |
| 1828 | __func__, rc); |
| 1829 | goto fail_free_vreg; |
| 1830 | } |
| 1831 | |
| 1832 | rpm_vreg->handle_sleep = msm_rpm_create_request(RPM_SET_SLEEP, |
| 1833 | resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX); |
| 1834 | if (rpm_vreg->handle_sleep == NULL || IS_ERR(rpm_vreg->handle_sleep)) { |
| 1835 | rc = PTR_ERR(rpm_vreg->handle_sleep); |
| 1836 | if (rc != -EPROBE_DEFER) |
| 1837 | dev_err(dev, "%s: failed to create sleep RPM handle, rc=%d\n", |
| 1838 | __func__, rc); |
| 1839 | goto fail_free_handle_active; |
| 1840 | } |
| 1841 | |
| 1842 | INIT_LIST_HEAD(&rpm_vreg->reg_list); |
| 1843 | |
| 1844 | if (rpm_vreg->allow_atomic) |
| 1845 | spin_lock_init(&rpm_vreg->slock); |
| 1846 | else |
| 1847 | mutex_init(&rpm_vreg->mlock); |
| 1848 | |
| 1849 | platform_set_drvdata(pdev, rpm_vreg); |
| 1850 | |
| 1851 | rc = of_platform_populate(node, NULL, NULL, dev); |
| 1852 | if (rc) { |
| 1853 | dev_err(dev, "%s: failed to add child nodes, rc=%d\n", __func__, |
| 1854 | rc); |
| 1855 | goto fail_unset_drvdata; |
| 1856 | } |
| 1857 | |
| 1858 | pr_debug("successfully probed: %s (%08X) %u\n", rpm_vreg->resource_name, |
| 1859 | resource_type, rpm_vreg->resource_id); |
| 1860 | |
| 1861 | return rc; |
| 1862 | |
| 1863 | fail_unset_drvdata: |
| 1864 | platform_set_drvdata(pdev, NULL); |
| 1865 | msm_rpm_free_request(rpm_vreg->handle_sleep); |
| 1866 | |
| 1867 | fail_free_handle_active: |
| 1868 | msm_rpm_free_request(rpm_vreg->handle_active); |
| 1869 | |
| 1870 | fail_free_vreg: |
| 1871 | kfree(rpm_vreg); |
| 1872 | |
| 1873 | return rc; |
| 1874 | } |
| 1875 | |
| 1876 | static struct of_device_id rpm_vreg_match_table_device[] = { |
| 1877 | { .compatible = "qcom,rpm-smd-regulator", }, |
| 1878 | {} |
| 1879 | }; |
| 1880 | |
| 1881 | static struct of_device_id rpm_vreg_match_table_resource[] = { |
| 1882 | { .compatible = "qcom,rpm-smd-regulator-resource", }, |
| 1883 | {} |
| 1884 | }; |
| 1885 | |
| 1886 | static struct platform_driver rpm_vreg_device_driver = { |
| 1887 | .probe = rpm_vreg_device_probe, |
| 1888 | .remove = rpm_vreg_device_remove, |
| 1889 | .driver = { |
| 1890 | .name = "qcom,rpm-smd-regulator", |
| 1891 | .owner = THIS_MODULE, |
| 1892 | .of_match_table = rpm_vreg_match_table_device, |
| 1893 | }, |
| 1894 | }; |
| 1895 | |
| 1896 | static struct platform_driver rpm_vreg_resource_driver = { |
| 1897 | .probe = rpm_vreg_resource_probe, |
| 1898 | .remove = rpm_vreg_resource_remove, |
| 1899 | .driver = { |
| 1900 | .name = "qcom,rpm-smd-regulator-resource", |
| 1901 | .owner = THIS_MODULE, |
| 1902 | .of_match_table = rpm_vreg_match_table_resource, |
| 1903 | }, |
| 1904 | }; |
| 1905 | |
| 1906 | /** |
| 1907 | * rpm_smd_regulator_driver_init() - initialize the RPM SMD regulator drivers |
| 1908 | * |
| 1909 | * This function registers the RPM SMD regulator platform drivers. |
| 1910 | * |
| 1911 | * Returns 0 on success or errno on failure. |
| 1912 | */ |
| 1913 | int __init rpm_smd_regulator_driver_init(void) |
| 1914 | { |
| 1915 | static bool initialized; |
| 1916 | int i, rc; |
| 1917 | |
| 1918 | if (initialized) |
| 1919 | return 0; |
| 1920 | else |
| 1921 | initialized = true; |
| 1922 | |
| 1923 | /* Store parameter string names as integers */ |
| 1924 | for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) |
| 1925 | params[i].key = rpm_vreg_string_to_int(params[i].name); |
| 1926 | |
| 1927 | rc = platform_driver_register(&rpm_vreg_device_driver); |
| 1928 | if (rc) |
| 1929 | return rc; |
| 1930 | |
| 1931 | return platform_driver_register(&rpm_vreg_resource_driver); |
| 1932 | } |
| 1933 | EXPORT_SYMBOL(rpm_smd_regulator_driver_init); |
| 1934 | |
| 1935 | static void __exit rpm_vreg_exit(void) |
| 1936 | { |
| 1937 | platform_driver_unregister(&rpm_vreg_device_driver); |
| 1938 | platform_driver_unregister(&rpm_vreg_resource_driver); |
| 1939 | } |
| 1940 | |
| 1941 | arch_initcall(rpm_smd_regulator_driver_init); |
| 1942 | module_exit(rpm_vreg_exit); |
| 1943 | |
| 1944 | MODULE_LICENSE("GPL v2"); |
| 1945 | MODULE_DESCRIPTION("MSM RPM SMD regulator driver"); |