David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/init.h> |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regulator/driver.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include <mach/rpm.h> |
| 25 | #include <mach/rpm-regulator.h> |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 26 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | |
| 28 | #include "rpm_resources.h" |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 29 | #include "rpm-regulator-private.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 30 | |
| 31 | /* Debug Definitions */ |
| 32 | |
| 33 | enum { |
| 34 | MSM_RPM_VREG_DEBUG_REQUEST = BIT(0), |
| 35 | MSM_RPM_VREG_DEBUG_VOTE = BIT(1), |
| 36 | MSM_RPM_VREG_DEBUG_DUPLICATE = BIT(2), |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 37 | MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG = BIT(3), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static int msm_rpm_vreg_debug_mask; |
| 41 | module_param_named( |
| 42 | debug_mask, msm_rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR |
| 43 | ); |
| 44 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 45 | struct vreg_config *(*get_config[])(void) = { |
| 46 | [RPM_VREG_VERSION_8660] = get_config_8660, |
| 47 | [RPM_VREG_VERSION_8960] = get_config_8960, |
David Collins | 6ef12bf | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 48 | [RPM_VREG_VERSION_9615] = get_config_9615, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 49 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 51 | #define SET_PART(_vreg, _part, _val) \ |
| 52 | _vreg->req[_vreg->part->_part.word].value \ |
| 53 | = (_vreg->req[_vreg->part->_part.word].value \ |
| 54 | & ~vreg->part->_part.mask) \ |
| 55 | | (((_val) << vreg->part->_part.shift) & vreg->part->_part.mask) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 56 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 57 | #define GET_PART(_vreg, _part) \ |
| 58 | ((_vreg->req[_vreg->part->_part.word].value & vreg->part->_part.mask) \ |
| 59 | >> vreg->part->_part.shift) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 60 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 61 | #define USES_PART(_vreg, _part) (vreg->part->_part.mask) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 62 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 63 | #define vreg_err(vreg, fmt, ...) \ |
| 64 | pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 65 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 66 | #define RPM_VREG_PIN_CTRL_EN0 0x01 |
| 67 | #define RPM_VREG_PIN_CTRL_EN1 0x02 |
| 68 | #define RPM_VREG_PIN_CTRL_EN2 0x04 |
| 69 | #define RPM_VREG_PIN_CTRL_EN3 0x08 |
| 70 | #define RPM_VREG_PIN_CTRL_ALL 0x0F |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 71 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 72 | static const char *label_freq[] = { |
| 73 | [RPM_VREG_FREQ_NONE] = " N/A", |
| 74 | [RPM_VREG_FREQ_19p20] = "19.2", |
| 75 | [RPM_VREG_FREQ_9p60] = "9.60", |
| 76 | [RPM_VREG_FREQ_6p40] = "6.40", |
| 77 | [RPM_VREG_FREQ_4p80] = "4.80", |
| 78 | [RPM_VREG_FREQ_3p84] = "3.84", |
| 79 | [RPM_VREG_FREQ_3p20] = "3.20", |
| 80 | [RPM_VREG_FREQ_2p74] = "2.74", |
| 81 | [RPM_VREG_FREQ_2p40] = "2.40", |
| 82 | [RPM_VREG_FREQ_2p13] = "2.13", |
| 83 | [RPM_VREG_FREQ_1p92] = "1.92", |
| 84 | [RPM_VREG_FREQ_1p75] = "1.75", |
| 85 | [RPM_VREG_FREQ_1p60] = "1.60", |
| 86 | [RPM_VREG_FREQ_1p48] = "1.48", |
| 87 | [RPM_VREG_FREQ_1p37] = "1.37", |
| 88 | [RPM_VREG_FREQ_1p28] = "1.28", |
| 89 | [RPM_VREG_FREQ_1p20] = "1.20", |
| 90 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | /* |
| 92 | * This is used when voting for LPM or HPM by subtracting or adding to the |
| 93 | * hpm_min_load of a regulator. It has units of uA. |
| 94 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 95 | #define LOAD_THRESHOLD_STEP 1000 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 97 | /* rpm_version keeps track of the version for the currently running driver. */ |
| 98 | enum rpm_vreg_version rpm_version = -1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 99 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 100 | /* config holds all configuration data of the currently running driver. */ |
| 101 | static struct vreg_config *config; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 102 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 103 | /* These regulator ID values are specified in the board file. */ |
| 104 | static int vreg_id_vdd_mem, vreg_id_vdd_dig; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 105 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 106 | static inline int vreg_id_is_vdd_mem_or_dig(int id) |
| 107 | { |
| 108 | return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig; |
| 109 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 110 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 111 | #define DEBUG_PRINT_BUFFER_SIZE 512 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 113 | static void rpm_regulator_req(struct vreg *vreg, int set) |
| 114 | { |
| 115 | int uV, mV, fm, pm, pc, pf, pd, freq, state, i; |
| 116 | const char *pf_label = "", *fm_label = "", *pc_total = ""; |
| 117 | const char *pc_en[4] = {"", "", "", ""}; |
| 118 | const char *pm_label = "", *freq_label = ""; |
| 119 | char buf[DEBUG_PRINT_BUFFER_SIZE]; |
| 120 | size_t buflen = DEBUG_PRINT_BUFFER_SIZE; |
| 121 | int pos = 0; |
| 122 | |
| 123 | /* Suppress VDD_MEM and VDD_DIG printing. */ |
| 124 | if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG) |
| 125 | && vreg_id_is_vdd_mem_or_dig(vreg->id)) |
| 126 | return; |
| 127 | |
| 128 | uV = GET_PART(vreg, uV); |
| 129 | mV = GET_PART(vreg, mV); |
| 130 | if (vreg->type == RPM_REGULATOR_TYPE_NCP) { |
| 131 | uV = -uV; |
| 132 | mV = -mV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 135 | fm = GET_PART(vreg, fm); |
| 136 | pm = GET_PART(vreg, pm); |
| 137 | pc = GET_PART(vreg, pc); |
| 138 | pf = GET_PART(vreg, pf); |
| 139 | pd = GET_PART(vreg, pd); |
| 140 | freq = GET_PART(vreg, freq); |
| 141 | state = GET_PART(vreg, enable_state); |
| 142 | |
| 143 | if (pf >= 0 && pf < config->label_pin_func_len) |
| 144 | pf_label = config->label_pin_func[pf]; |
| 145 | |
| 146 | if (fm >= 0 && fm < config->label_force_mode_len) |
| 147 | fm_label = config->label_force_mode[fm]; |
| 148 | |
| 149 | if (pm >= 0 && pm < config->label_power_mode_len) |
| 150 | pm_label = config->label_power_mode[pm]; |
| 151 | |
| 152 | if (freq >= 0 && freq < ARRAY_SIZE(label_freq)) |
| 153 | freq_label = label_freq[freq]; |
| 154 | |
| 155 | for (i = 0; i < config->label_pin_ctrl_len; i++) |
| 156 | if (pc & (1 << i)) |
| 157 | pc_en[i] = config->label_pin_ctrl[i]; |
| 158 | |
| 159 | if (pc == RPM_VREG_PIN_CTRL_NONE) |
| 160 | pc_total = " none"; |
| 161 | |
| 162 | pos += scnprintf(buf + pos, buflen - pos, "%s%s: ", |
| 163 | KERN_INFO, __func__); |
| 164 | |
| 165 | pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c", |
| 166 | (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"), |
| 167 | vreg->rdesc.name, |
| 168 | (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S')); |
| 169 | |
| 170 | if (USES_PART(vreg, uV)) |
| 171 | pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV); |
| 172 | if (USES_PART(vreg, mV)) |
| 173 | pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV); |
| 174 | if (USES_PART(vreg, enable_state)) |
| 175 | pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)", |
| 176 | (state == 1 ? "on" : "off"), state); |
| 177 | if (USES_PART(vreg, ip)) |
| 178 | pos += scnprintf(buf + pos, buflen - pos, |
| 179 | ", ip=%4d mA", GET_PART(vreg, ip)); |
| 180 | if (USES_PART(vreg, fm)) |
| 181 | pos += scnprintf(buf + pos, buflen - pos, |
| 182 | ", fm=%s (%d)", fm_label, fm); |
| 183 | if (USES_PART(vreg, pc)) |
| 184 | pos += scnprintf(buf + pos, buflen - pos, |
| 185 | ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1], |
| 186 | pc_en[2], pc_en[3], pc_total, pc); |
| 187 | if (USES_PART(vreg, pf)) |
| 188 | pos += scnprintf(buf + pos, buflen - pos, |
| 189 | ", pf=%s (%d)", pf_label, pf); |
| 190 | if (USES_PART(vreg, pd)) |
| 191 | pos += scnprintf(buf + pos, buflen - pos, |
| 192 | ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd); |
| 193 | if (USES_PART(vreg, ia)) |
| 194 | pos += scnprintf(buf + pos, buflen - pos, |
| 195 | ", ia=%4d mA", GET_PART(vreg, ia)); |
| 196 | if (USES_PART(vreg, freq)) { |
| 197 | if (vreg->type == RPM_REGULATOR_TYPE_NCP) |
| 198 | pos += scnprintf(buf + pos, buflen - pos, |
| 199 | ", freq=%2d", freq); |
| 200 | else |
| 201 | pos += scnprintf(buf + pos, buflen - pos, |
| 202 | ", freq=%s MHz (%2d)", freq_label, freq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 203 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 204 | if (USES_PART(vreg, pm)) |
| 205 | pos += scnprintf(buf + pos, buflen - pos, |
| 206 | ", pm=%s (%d)", pm_label, pm); |
| 207 | if (USES_PART(vreg, freq_clk_src)) |
| 208 | pos += scnprintf(buf + pos, buflen - pos, |
| 209 | ", clk_src=%d", GET_PART(vreg, freq_clk_src)); |
| 210 | if (USES_PART(vreg, comp_mode)) |
| 211 | pos += scnprintf(buf + pos, buflen - pos, |
| 212 | ", comp=%d", GET_PART(vreg, comp_mode)); |
| 213 | if (USES_PART(vreg, hpm)) |
| 214 | pos += scnprintf(buf + pos, buflen - pos, |
| 215 | ", hpm=%d", GET_PART(vreg, hpm)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 216 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 217 | pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}", |
| 218 | vreg->req[0].id, vreg->req[0].value); |
| 219 | if (vreg->part->request_len > 1) |
| 220 | pos += scnprintf(buf + pos, buflen - pos, |
| 221 | ", req[1]={%d, 0x%08X}", vreg->req[1].id, |
| 222 | vreg->req[1].value); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 223 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 224 | pos += scnprintf(buf + pos, buflen - pos, "\n"); |
| 225 | printk(buf); |
| 226 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 227 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 228 | static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter, |
| 229 | int set, int voter_uV, int aggregate_uV) |
| 230 | { |
| 231 | /* Suppress VDD_MEM and VDD_DIG printing. */ |
| 232 | if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG) |
| 233 | && vreg_id_is_vdd_mem_or_dig(vreg->id)) |
| 234 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 236 | pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, " |
| 237 | "v_aggregate=%7d uV\n", vreg->rdesc.name, voter, |
| 238 | (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV); |
| 239 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 241 | static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt) |
| 242 | { |
| 243 | /* Suppress VDD_MEM and VDD_DIG printing. */ |
| 244 | if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG) |
| 245 | && vreg_id_is_vdd_mem_or_dig(vreg->id)) |
| 246 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 247 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 248 | if (cnt == 2) |
| 249 | pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, " |
| 250 | "req[1]={%d, 0x%08X}\n", vreg->rdesc.name, |
| 251 | (set == 0 ? 'A' : 'S'), |
| 252 | vreg->req[0].id, vreg->req[0].value, |
| 253 | vreg->req[1].id, vreg->req[1].value); |
| 254 | else if (cnt == 1) |
| 255 | pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n", |
| 256 | vreg->rdesc.name, (set == 0 ? 'A' : 'S'), |
| 257 | vreg->req[0].id, vreg->req[0].value); |
| 258 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | |
| 260 | /* Spin lock needed for sleep-selectable regulators. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 261 | static DEFINE_SPINLOCK(rpm_noirq_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 262 | |
| 263 | static int voltage_from_req(struct vreg *vreg) |
| 264 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 265 | int uV = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 266 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 267 | if (vreg->part->uV.mask) |
| 268 | uV = GET_PART(vreg, uV); |
| 269 | else |
| 270 | uV = MILLI_TO_MICRO(GET_PART(vreg, mV)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 271 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 272 | return uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 273 | } |
| 274 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 275 | static void voltage_to_req(int uV, struct vreg *vreg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 276 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 277 | if (vreg->part->uV.mask) |
| 278 | SET_PART(vreg, uV, uV); |
| 279 | else |
| 280 | SET_PART(vreg, mV, MICRO_TO_MILLI(uV)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter, |
| 284 | int set, unsigned mask0, unsigned val0, |
| 285 | unsigned mask1, unsigned val1, unsigned cnt, |
| 286 | int update_voltage) |
| 287 | { |
| 288 | struct msm_rpm_iv_pair *prev_req; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 289 | int rc = 0, max_uV_vote = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 290 | unsigned prev0, prev1; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 291 | int *min_uV_vote; |
| 292 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 293 | |
| 294 | if (set == MSM_RPM_CTX_SET_0) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 295 | min_uV_vote = vreg->active_min_uV_vote; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 296 | prev_req = vreg->prev_active_req; |
| 297 | } else { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 298 | min_uV_vote = vreg->sleep_min_uV_vote; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | prev_req = vreg->prev_sleep_req; |
| 300 | } |
| 301 | |
| 302 | prev0 = vreg->req[0].value; |
| 303 | vreg->req[0].value &= ~mask0; |
| 304 | vreg->req[0].value |= val0 & mask0; |
| 305 | |
| 306 | prev1 = vreg->req[1].value; |
| 307 | vreg->req[1].value &= ~mask1; |
| 308 | vreg->req[1].value |= val1 & mask1; |
| 309 | |
| 310 | if (update_voltage) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 311 | min_uV_vote[voter] = voltage_from_req(vreg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 312 | |
| 313 | /* Find the highest voltage voted for and use it. */ |
| 314 | for (i = 0; i < RPM_VREG_VOTER_COUNT; i++) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 315 | max_uV_vote = max(max_uV_vote, min_uV_vote[i]); |
| 316 | voltage_to_req(max_uV_vote, vreg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 317 | |
| 318 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 319 | rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter], |
| 320 | max_uV_vote); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 321 | |
| 322 | /* Ignore duplicate requests */ |
| 323 | if (vreg->req[0].value != prev_req[0].value || |
| 324 | vreg->req[1].value != prev_req[1].value) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 325 | rc = msm_rpmrs_set_noirq(set, vreg->req, cnt); |
| 326 | if (rc) { |
| 327 | vreg->req[0].value = prev0; |
| 328 | vreg->req[1].value = prev1; |
| 329 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 330 | vreg_err(vreg, "msm_rpmrs_set_noirq failed - " |
| 331 | "set=%s, id=%d, rc=%d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 332 | (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"), |
| 333 | vreg->req[0].id, rc); |
| 334 | } else { |
| 335 | /* Only save if nonzero and active set. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 336 | if (max_uV_vote && (set == MSM_RPM_CTX_SET_0)) |
| 337 | vreg->save_uV = max_uV_vote; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 338 | if (msm_rpm_vreg_debug_mask |
| 339 | & MSM_RPM_VREG_DEBUG_REQUEST) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 340 | rpm_regulator_req(vreg, set); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 341 | prev_req[0].value = vreg->req[0].value; |
| 342 | prev_req[1].value = vreg->req[1].value; |
| 343 | } |
| 344 | } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 345 | rpm_regulator_duplicate(vreg, set, cnt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | return rc; |
| 349 | } |
| 350 | |
| 351 | static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter, |
| 352 | int sleep, unsigned mask0, unsigned val0, |
| 353 | unsigned mask1, unsigned val1, unsigned cnt, |
| 354 | int update_voltage) |
| 355 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 356 | unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1}; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 357 | unsigned long flags; |
| 358 | int rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 359 | |
| 360 | if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT) |
| 361 | return -EINVAL; |
| 362 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 363 | spin_lock_irqsave(&rpm_noirq_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 364 | |
| 365 | /* |
| 366 | * Send sleep set request first so that subsequent set_mode, etc calls |
| 367 | * use the voltage from the active set. |
| 368 | */ |
| 369 | if (sleep) |
| 370 | rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP, |
| 371 | mask0, val0, mask1, val1, cnt, update_voltage); |
| 372 | else { |
| 373 | /* |
| 374 | * Vote for 0 V in the sleep set when active set-only is |
| 375 | * specified. This ensures that a disable vote will be issued |
| 376 | * at some point for the sleep set of the regulator. |
| 377 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 378 | if (vreg->part->uV.mask) { |
| 379 | s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift; |
| 380 | s_mask[vreg->part->uV.word] = vreg->part->uV.mask; |
| 381 | } else { |
| 382 | s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift; |
| 383 | s_mask[vreg->part->mV.word] = vreg->part->mV.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 387 | s_mask[0], s_val[0], s_mask[1], s_val[1], |
| 388 | cnt, update_voltage); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0, |
| 392 | mask1, val1, cnt, update_voltage); |
| 393 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 394 | spin_unlock_irqrestore(&rpm_noirq_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 395 | |
| 396 | return rc; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor |
| 401 | * @vreg: ID for regulator |
| 402 | * @voter: ID for the voter |
| 403 | * @min_uV: minimum acceptable voltage (in uV) that is voted for |
| 404 | * @max_uV: maximum acceptable voltage (in uV) that is voted for |
| 405 | * @sleep_also: 0 for active set only, non-0 for active set and sleep set |
| 406 | * |
| 407 | * Returns 0 on success or errno. |
| 408 | * |
| 409 | * This function is used to vote for the voltage of a regulator without |
| 410 | * using the regulator framework. It is needed by consumers which hold spin |
| 411 | * locks or have interrupts disabled because the regulator framework can sleep. |
| 412 | * It is also needed by consumers which wish to only vote for active set |
| 413 | * regulator voltage. |
| 414 | * |
| 415 | * If sleep_also == 0, then a sleep-set value of 0V will be voted for. |
| 416 | * |
| 417 | * This function may only be called for regulators which have the sleep flag |
| 418 | * specified in their private data. |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame^] | 419 | * |
| 420 | * Consumers can vote to disable a regulator with this function by passing |
| 421 | * min_uV = 0 and max_uV = 0. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 422 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 423 | int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV, |
| 424 | int max_uV, int sleep_also) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 425 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 426 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 427 | struct vreg_range *range; |
| 428 | struct vreg *vreg; |
| 429 | int uV = min_uV; |
| 430 | int lim_min_uV, lim_max_uV, i, rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 431 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 432 | /* |
| 433 | * HACK: make this function a no-op for 8064 so that it can be called by |
| 434 | * consumers on 8064 before RPM capabilities are present. (needed for |
| 435 | * acpuclock driver) |
| 436 | */ |
| 437 | if (cpu_is_apq8064()) |
| 438 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 439 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 440 | if (!config) { |
| 441 | pr_err("rpm-regulator driver has not probed yet.\n"); |
| 442 | return -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | } |
| 444 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 445 | if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) { |
| 446 | pr_err("invalid regulator id=%d\n", vreg_id); |
| 447 | return -EINVAL; |
| 448 | } |
| 449 | |
| 450 | vreg = &config->vregs[vreg_id]; |
| 451 | range = &vreg->set_points->range[0]; |
| 452 | |
| 453 | if (!vreg->pdata.sleep_selectable) { |
| 454 | vreg_err(vreg, "regulator is not marked sleep selectable\n"); |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame^] | 458 | /* Allow min_uV == max_uV == 0 to represent a disable request. */ |
| 459 | if (min_uV != 0 || max_uV != 0) { |
| 460 | /* |
| 461 | * Check if request voltage is outside of allowed range. The |
| 462 | * regulator core has already checked that constraint range |
| 463 | * is inside of the physically allowed range. |
| 464 | */ |
| 465 | lim_min_uV = vreg->pdata.init_data.constraints.min_uV; |
| 466 | lim_max_uV = vreg->pdata.init_data.constraints.max_uV; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 467 | |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame^] | 468 | if (uV < lim_min_uV && max_uV >= lim_min_uV) |
| 469 | uV = lim_min_uV; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 470 | |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame^] | 471 | if (uV < lim_min_uV || uV > lim_max_uV) { |
| 472 | vreg_err(vreg, "request v=[%d, %d] is outside allowed " |
| 473 | "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV, |
| 474 | lim_max_uV); |
| 475 | return -EINVAL; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 476 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 477 | |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame^] | 478 | /* Find the range which uV is inside of. */ |
| 479 | for (i = vreg->set_points->count - 1; i > 0; i--) { |
| 480 | if (uV > vreg->set_points->range[i - 1].max_uV) { |
| 481 | range = &vreg->set_points->range[i]; |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Force uV to be an allowed set point and apply a ceiling |
| 488 | * function to non-set point values. |
| 489 | */ |
| 490 | uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV; |
| 491 | uV = uV * range->step_uV + range->min_uV; |
| 492 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 493 | |
| 494 | if (vreg->part->uV.mask) { |
| 495 | val[vreg->part->uV.word] = uV << vreg->part->uV.shift; |
| 496 | mask[vreg->part->uV.word] = vreg->part->uV.mask; |
| 497 | } else { |
| 498 | val[vreg->part->mV.word] |
| 499 | = MICRO_TO_MILLI(uV) << vreg->part->mV.shift; |
| 500 | mask[vreg->part->mV.word] = vreg->part->mV.mask; |
| 501 | } |
| 502 | |
| 503 | rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1], |
| 504 | val[1], vreg->part->request_len, 1); |
| 505 | if (rc) |
| 506 | vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 507 | |
| 508 | return rc; |
| 509 | } |
| 510 | EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage); |
| 511 | |
| 512 | /** |
| 513 | * rpm_vreg_set_frequency - sets the frequency of a switching regulator |
| 514 | * @vreg: ID for regulator |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 515 | * @freq: enum corresponding to desired frequency |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 516 | * |
| 517 | * Returns 0 on success or errno. |
| 518 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 519 | int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 520 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 521 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 522 | struct vreg *vreg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 523 | int rc; |
| 524 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 525 | /* |
| 526 | * HACK: make this function a no-op for 8064 so that it can be called by |
| 527 | * consumers on 8064 before RPM capabilities are present. |
| 528 | */ |
| 529 | if (cpu_is_apq8064()) |
| 530 | return 0; |
| 531 | |
| 532 | if (!config) { |
| 533 | pr_err("rpm-regulator driver has not probed yet.\n"); |
| 534 | return -ENODEV; |
| 535 | } |
| 536 | |
| 537 | if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) { |
| 538 | pr_err("invalid regulator id=%d\n", vreg_id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 539 | return -EINVAL; |
| 540 | } |
| 541 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 542 | vreg = &config->vregs[vreg_id]; |
| 543 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 544 | if (freq < 0 || freq > RPM_VREG_FREQ_1p20) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 545 | vreg_err(vreg, "invalid frequency=%d\n", freq); |
| 546 | return -EINVAL; |
| 547 | } |
| 548 | if (!vreg->pdata.sleep_selectable) { |
| 549 | vreg_err(vreg, "regulator is not marked sleep selectable\n"); |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | if (!vreg->part->freq.mask) { |
| 553 | vreg_err(vreg, "frequency not supported\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 554 | return -EINVAL; |
| 555 | } |
| 556 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 557 | val[vreg->part->freq.word] = freq << vreg->part->freq.shift; |
| 558 | mask[vreg->part->freq.word] = vreg->part->freq.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 560 | rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0], |
| 561 | val[0], mask[1], val[1], vreg->part->request_len, 0); |
| 562 | if (rc) |
| 563 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 564 | |
| 565 | return rc; |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency); |
| 568 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 569 | static inline int vreg_hpm_min_uA(struct vreg *vreg) |
| 570 | { |
| 571 | return vreg->hpm_min_load; |
| 572 | } |
| 573 | |
| 574 | static inline int vreg_lpm_max_uA(struct vreg *vreg) |
| 575 | { |
| 576 | return vreg->hpm_min_load - LOAD_THRESHOLD_STEP; |
| 577 | } |
| 578 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 579 | static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 581 | unsigned load_max |
| 582 | = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift); |
| 583 | |
| 584 | return (load_uA > load_max ? load_max : load_uA); |
| 585 | } |
| 586 | |
| 587 | static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA) |
| 588 | { |
| 589 | unsigned load_max |
| 590 | = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift); |
| 591 | return (load_uA > load_max ? load_max : load_uA); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* Change vreg->req, but do not send it to the RPM. */ |
| 595 | static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0, |
| 596 | unsigned mask1, unsigned val1) |
| 597 | { |
| 598 | unsigned long flags = 0; |
| 599 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 600 | if (vreg->pdata.sleep_selectable) |
| 601 | spin_lock_irqsave(&rpm_noirq_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 602 | |
| 603 | vreg->req[0].value &= ~mask0; |
| 604 | vreg->req[0].value |= val0 & mask0; |
| 605 | |
| 606 | vreg->req[1].value &= ~mask1; |
| 607 | vreg->req[1].value |= val1 & mask1; |
| 608 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 609 | if (vreg->pdata.sleep_selectable) |
| 610 | spin_unlock_irqrestore(&rpm_noirq_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0, |
| 616 | unsigned mask1, unsigned val1, unsigned cnt) |
| 617 | { |
| 618 | unsigned prev0 = 0, prev1 = 0; |
| 619 | int rc; |
| 620 | |
| 621 | /* |
| 622 | * Bypass the normal route for regulators that can be called to change |
| 623 | * just the active set values. |
| 624 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 625 | if (vreg->pdata.sleep_selectable) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 626 | return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, |
| 627 | mask0, val0, mask1, val1, cnt, 1); |
| 628 | |
| 629 | prev0 = vreg->req[0].value; |
| 630 | vreg->req[0].value &= ~mask0; |
| 631 | vreg->req[0].value |= val0 & mask0; |
| 632 | |
| 633 | prev1 = vreg->req[1].value; |
| 634 | vreg->req[1].value &= ~mask1; |
| 635 | vreg->req[1].value |= val1 & mask1; |
| 636 | |
| 637 | /* Ignore duplicate requests */ |
| 638 | if (vreg->req[0].value == vreg->prev_active_req[0].value && |
| 639 | vreg->req[1].value == vreg->prev_active_req[1].value) { |
| 640 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 641 | rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt); |
| 646 | if (rc) { |
| 647 | vreg->req[0].value = prev0; |
| 648 | vreg->req[1].value = prev1; |
| 649 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 650 | vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n", |
| 651 | vreg->req[0].id, rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | } else { |
| 653 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 654 | rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 655 | vreg->prev_active_req[0].value = vreg->req[0].value; |
| 656 | vreg->prev_active_req[1].value = vreg->req[1].value; |
| 657 | } |
| 658 | |
| 659 | return rc; |
| 660 | } |
| 661 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 662 | static int vreg_is_enabled(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 663 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 664 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 665 | int enabled; |
| 666 | |
| 667 | mutex_lock(&vreg->pc_lock); |
| 668 | enabled = vreg->is_enabled; |
| 669 | mutex_unlock(&vreg->pc_lock); |
| 670 | |
| 671 | return enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 672 | } |
| 673 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 674 | static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 675 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 676 | switch (vreg->type) { |
| 677 | case RPM_REGULATOR_TYPE_LDO: |
| 678 | case RPM_REGULATOR_TYPE_SMPS: |
| 679 | /* Enable by setting a voltage. */ |
| 680 | if (vreg->part->uV.mask) { |
| 681 | val[vreg->part->uV.word] |
| 682 | |= vreg->save_uV << vreg->part->uV.shift; |
| 683 | mask[vreg->part->uV.word] |= vreg->part->uV.mask; |
| 684 | } else { |
| 685 | val[vreg->part->mV.word] |
| 686 | |= MICRO_TO_MILLI(vreg->save_uV) |
| 687 | << vreg->part->mV.shift; |
| 688 | mask[vreg->part->mV.word] |= vreg->part->mV.mask; |
| 689 | } |
| 690 | break; |
| 691 | case RPM_REGULATOR_TYPE_VS: |
| 692 | case RPM_REGULATOR_TYPE_NCP: |
| 693 | /* Enable by setting enable_state. */ |
| 694 | val[vreg->part->enable_state.word] |
| 695 | |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift; |
| 696 | mask[vreg->part->enable_state.word] |
| 697 | |= vreg->part->enable_state.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 698 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 699 | } |
| 700 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 701 | static int vreg_enable(struct regulator_dev *rdev) |
| 702 | { |
| 703 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 704 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 705 | int rc = 0; |
| 706 | |
| 707 | set_enable(vreg, mask, val); |
| 708 | |
| 709 | mutex_lock(&vreg->pc_lock); |
| 710 | |
| 711 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 712 | vreg->part->request_len); |
| 713 | if (!rc) |
| 714 | vreg->is_enabled = true; |
| 715 | |
| 716 | mutex_unlock(&vreg->pc_lock); |
| 717 | |
| 718 | if (rc) |
| 719 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 720 | |
| 721 | return rc; |
| 722 | } |
| 723 | |
| 724 | static void set_disable(struct vreg *vreg, unsigned int *mask, |
| 725 | unsigned int *val) |
| 726 | { |
| 727 | switch (vreg->type) { |
| 728 | case RPM_REGULATOR_TYPE_LDO: |
| 729 | case RPM_REGULATOR_TYPE_SMPS: |
| 730 | /* Disable by setting a voltage of 0 uV. */ |
| 731 | if (vreg->part->uV.mask) { |
| 732 | val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift; |
| 733 | mask[vreg->part->uV.word] |= vreg->part->uV.mask; |
| 734 | } else { |
| 735 | val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift; |
| 736 | mask[vreg->part->mV.word] |= vreg->part->mV.mask; |
| 737 | } |
| 738 | break; |
| 739 | case RPM_REGULATOR_TYPE_VS: |
| 740 | case RPM_REGULATOR_TYPE_NCP: |
| 741 | /* Disable by setting enable_state. */ |
| 742 | val[vreg->part->enable_state.word] |
| 743 | |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift; |
| 744 | mask[vreg->part->enable_state.word] |
| 745 | |= vreg->part->enable_state.mask; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | static int vreg_disable(struct regulator_dev *rdev) |
| 750 | { |
| 751 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 752 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 753 | int rc = 0; |
| 754 | |
| 755 | set_disable(vreg, mask, val); |
| 756 | |
| 757 | mutex_lock(&vreg->pc_lock); |
| 758 | |
| 759 | /* Only disable if pin control is not in use. */ |
| 760 | if (!vreg->is_enabled_pc) |
| 761 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 762 | vreg->part->request_len); |
| 763 | |
| 764 | if (!rc) |
| 765 | vreg->is_enabled = false; |
| 766 | |
| 767 | mutex_unlock(&vreg->pc_lock); |
| 768 | |
| 769 | if (rc) |
| 770 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 771 | |
| 772 | return rc; |
| 773 | } |
| 774 | |
| 775 | static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 776 | unsigned *selector) |
| 777 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 778 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 779 | struct vreg_range *range = &vreg->set_points->range[0]; |
| 780 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 781 | int rc = 0, uV = min_uV; |
| 782 | int lim_min_uV, lim_max_uV, i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 783 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 784 | /* Check if request voltage is outside of physically settable range. */ |
| 785 | lim_min_uV = vreg->set_points->range[0].min_uV; |
| 786 | lim_max_uV = |
| 787 | vreg->set_points->range[vreg->set_points->count - 1].max_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 788 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 789 | if (uV < lim_min_uV && max_uV >= lim_min_uV) |
| 790 | uV = lim_min_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 791 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 792 | if (uV < lim_min_uV || uV > lim_max_uV) { |
| 793 | vreg_err(vreg, |
| 794 | "request v=[%d, %d] is outside possible v=[%d, %d]\n", |
| 795 | min_uV, max_uV, lim_min_uV, lim_max_uV); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 796 | return -EINVAL; |
| 797 | } |
| 798 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 799 | /* Find the range which uV is inside of. */ |
| 800 | for (i = vreg->set_points->count - 1; i > 0; i--) { |
| 801 | if (uV > vreg->set_points->range[i - 1].max_uV) { |
| 802 | range = &vreg->set_points->range[i]; |
| 803 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 807 | /* |
| 808 | * Force uV to be an allowed set point and apply a ceiling function |
| 809 | * to non-set point values. |
| 810 | */ |
| 811 | uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV; |
| 812 | uV = uV * range->step_uV + range->min_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 813 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 814 | if (vreg->part->uV.mask) { |
| 815 | val[vreg->part->uV.word] = uV << vreg->part->uV.shift; |
| 816 | mask[vreg->part->uV.word] = vreg->part->uV.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 817 | } else { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 818 | val[vreg->part->mV.word] |
| 819 | = MICRO_TO_MILLI(uV) << vreg->part->mV.shift; |
| 820 | mask[vreg->part->mV.word] = vreg->part->mV.mask; |
| 821 | } |
| 822 | |
| 823 | mutex_lock(&vreg->pc_lock); |
| 824 | |
| 825 | /* |
| 826 | * Only send a request for a new voltage if the regulator is currently |
| 827 | * enabled. This will ensure that LDO and SMPS regulators are not |
| 828 | * inadvertently turned on because voltage > 0 is equivalent to |
| 829 | * enabling. For NCP, this just removes unnecessary RPM requests. |
| 830 | */ |
| 831 | if (vreg->is_enabled) { |
| 832 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 833 | vreg->part->request_len); |
| 834 | if (rc) |
| 835 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 836 | } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 837 | /* Regulator is disabled; store but don't send new request. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 838 | rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 839 | } |
| 840 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 841 | if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled)) |
| 842 | vreg->save_uV = uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 843 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 844 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 845 | |
| 846 | return rc; |
| 847 | } |
| 848 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 849 | static int vreg_get_voltage(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 850 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 851 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 852 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 853 | return vreg->save_uV; |
| 854 | } |
| 855 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 856 | static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 857 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 858 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 859 | int uV = 0; |
| 860 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 861 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 862 | if (!vreg->set_points) { |
| 863 | vreg_err(vreg, "no voltages available\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 864 | return -EINVAL; |
| 865 | } |
| 866 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 867 | if (selector >= vreg->set_points->n_voltages) |
| 868 | return 0; |
| 869 | |
| 870 | for (i = 0; i < vreg->set_points->count; i++) { |
| 871 | if (selector < vreg->set_points->range[i].n_voltages) { |
| 872 | uV = selector * vreg->set_points->range[i].step_uV |
| 873 | + vreg->set_points->range[i].min_uV; |
| 874 | break; |
| 875 | } else { |
| 876 | selector -= vreg->set_points->range[i].n_voltages; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | return uV; |
| 881 | } |
| 882 | |
| 883 | static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 884 | { |
| 885 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 886 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 887 | int rc = 0; |
| 888 | int peak_uA; |
| 889 | |
| 890 | mutex_lock(&vreg->pc_lock); |
| 891 | |
| 892 | peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value |
| 893 | & vreg->part->ip.mask) >> vreg->part->ip.shift); |
| 894 | |
| 895 | if (mode == config->mode_hpm) { |
| 896 | /* Make sure that request currents are in HPM range. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 897 | if (peak_uA < vreg_hpm_min_uA(vreg)) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 898 | val[vreg->part->ip.word] |
| 899 | = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg)) |
| 900 | << vreg->part->ip.shift; |
| 901 | mask[vreg->part->ip.word] = vreg->part->ip.mask; |
| 902 | |
| 903 | if (config->ia_follows_ip) { |
| 904 | val[vreg->part->ia.word] |
| 905 | |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg)) |
| 906 | << vreg->part->ia.shift; |
| 907 | mask[vreg->part->ia.word] |
| 908 | |= vreg->part->ia.mask; |
| 909 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 910 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 911 | } else if (mode == config->mode_lpm) { |
| 912 | /* Make sure that request currents are in LPM range. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 913 | if (peak_uA > vreg_lpm_max_uA(vreg)) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 914 | val[vreg->part->ip.word] |
| 915 | = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg)) |
| 916 | << vreg->part->ip.shift; |
| 917 | mask[vreg->part->ip.word] = vreg->part->ip.mask; |
| 918 | |
| 919 | if (config->ia_follows_ip) { |
| 920 | val[vreg->part->ia.word] |
| 921 | |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg)) |
| 922 | << vreg->part->ia.shift; |
| 923 | mask[vreg->part->ia.word] |
| 924 | |= vreg->part->ia.mask; |
| 925 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 926 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 927 | } else { |
| 928 | vreg_err(vreg, "invalid mode: %u\n", mode); |
| 929 | mutex_unlock(&vreg->pc_lock); |
| 930 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 931 | } |
| 932 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 933 | if (vreg->is_enabled) { |
| 934 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 935 | vreg->part->request_len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 936 | } else { |
| 937 | /* Regulator is disabled; store but don't send new request. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 938 | rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 939 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 940 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 941 | if (rc) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 942 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 943 | else |
| 944 | vreg->mode = mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 945 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 946 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 947 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 948 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 949 | } |
| 950 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 951 | static unsigned int vreg_get_mode(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 952 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 953 | struct vreg *vreg = rdev_get_drvdata(rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 954 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 955 | return vreg->mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 956 | } |
| 957 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 958 | static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev, |
| 959 | int input_uV, int output_uV, int load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 960 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 961 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 962 | unsigned int mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 963 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 964 | load_uA += vreg->pdata.system_uA; |
| 965 | |
| 966 | mutex_lock(&vreg->pc_lock); |
| 967 | SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA))); |
| 968 | if (config->ia_follows_ip) |
| 969 | SET_PART(vreg, ia, |
| 970 | MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA))); |
| 971 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 972 | |
| 973 | if (load_uA >= vreg->hpm_min_load) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 974 | mode = config->mode_hpm; |
| 975 | else |
| 976 | mode = config->mode_lpm; |
| 977 | |
| 978 | return mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 979 | } |
| 980 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 981 | static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev, |
| 982 | int input_uV, int output_uV, int load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 983 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 984 | struct vreg *vreg = rdev_get_drvdata(rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 985 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 986 | if (MICRO_TO_MILLI(load_uA) <= 0) { |
| 987 | /* |
| 988 | * vreg_legacy_get_optimum_mode is being called before consumers |
| 989 | * have specified their load currents via |
| 990 | * regulator_set_optimum_mode. Return whatever the existing mode |
| 991 | * is. |
| 992 | */ |
| 993 | return vreg->mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 994 | } |
| 995 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 996 | return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1000 | * Returns the logical pin control enable state because the pin control options |
| 1001 | * present in the hardware out of restart could be different from those desired |
| 1002 | * by the consumer. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1003 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1004 | static int vreg_pin_control_is_enabled(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1005 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1006 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1007 | |
| 1008 | return vreg->is_enabled_pc; |
| 1009 | } |
| 1010 | |
| 1011 | static int vreg_pin_control_enable(struct regulator_dev *rdev) |
| 1012 | { |
| 1013 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1014 | unsigned int mask[2] = {0}, val[2] = {0}; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1015 | int rc; |
| 1016 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1017 | mutex_lock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1018 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1019 | val[vreg->part->pc.word] |
| 1020 | |= vreg->pdata.pin_ctrl << vreg->part->pc.shift; |
| 1021 | mask[vreg->part->pc.word] |= vreg->part->pc.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1022 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1023 | val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift; |
| 1024 | mask[vreg->part->pf.word] |= vreg->part->pf.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1025 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1026 | if (!vreg->is_enabled) |
| 1027 | set_enable(vreg, mask, val); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1028 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1029 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1030 | vreg->part->request_len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1031 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1032 | if (!rc) |
| 1033 | vreg->is_enabled_pc = true; |
| 1034 | |
| 1035 | mutex_unlock(&vreg->pc_lock); |
| 1036 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1037 | if (rc) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1038 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1039 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1040 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1043 | static int vreg_pin_control_disable(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1044 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1045 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1046 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 1047 | int pin_fn, rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1048 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1049 | mutex_lock(&vreg->pc_lock); |
| 1050 | |
| 1051 | val[vreg->part->pc.word] |
| 1052 | |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift; |
| 1053 | mask[vreg->part->pc.word] |= vreg->part->pc.mask; |
| 1054 | |
| 1055 | pin_fn = config->pin_func_none; |
| 1056 | if (vreg->pdata.pin_fn == config->pin_func_sleep_b) |
| 1057 | pin_fn = config->pin_func_sleep_b; |
| 1058 | val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift; |
| 1059 | mask[vreg->part->pf.word] |= vreg->part->pf.mask; |
| 1060 | |
| 1061 | if (!vreg->is_enabled) |
| 1062 | set_disable(vreg, mask, val); |
| 1063 | |
| 1064 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1065 | vreg->part->request_len); |
| 1066 | |
| 1067 | if (!rc) |
| 1068 | vreg->is_enabled_pc = false; |
| 1069 | |
| 1070 | mutex_unlock(&vreg->pc_lock); |
| 1071 | |
| 1072 | if (rc) |
| 1073 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 1074 | |
| 1075 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1078 | static int vreg_enable_time(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1079 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1080 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1081 | |
| 1082 | return vreg->pdata.enable_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1085 | /* Real regulator operations. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1086 | static struct regulator_ops ldo_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1087 | .enable = vreg_enable, |
| 1088 | .disable = vreg_disable, |
| 1089 | .is_enabled = vreg_is_enabled, |
| 1090 | .set_voltage = vreg_set_voltage, |
| 1091 | .get_voltage = vreg_get_voltage, |
| 1092 | .list_voltage = vreg_list_voltage, |
| 1093 | .set_mode = vreg_set_mode, |
| 1094 | .get_mode = vreg_get_mode, |
| 1095 | .get_optimum_mode = vreg_get_optimum_mode, |
| 1096 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1097 | }; |
| 1098 | |
| 1099 | static struct regulator_ops smps_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1100 | .enable = vreg_enable, |
| 1101 | .disable = vreg_disable, |
| 1102 | .is_enabled = vreg_is_enabled, |
| 1103 | .set_voltage = vreg_set_voltage, |
| 1104 | .get_voltage = vreg_get_voltage, |
| 1105 | .list_voltage = vreg_list_voltage, |
| 1106 | .set_mode = vreg_set_mode, |
| 1107 | .get_mode = vreg_get_mode, |
| 1108 | .get_optimum_mode = vreg_get_optimum_mode, |
| 1109 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1110 | }; |
| 1111 | |
| 1112 | static struct regulator_ops switch_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1113 | .enable = vreg_enable, |
| 1114 | .disable = vreg_disable, |
| 1115 | .is_enabled = vreg_is_enabled, |
| 1116 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1117 | }; |
| 1118 | |
| 1119 | static struct regulator_ops ncp_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1120 | .enable = vreg_enable, |
| 1121 | .disable = vreg_disable, |
| 1122 | .is_enabled = vreg_is_enabled, |
| 1123 | .set_voltage = vreg_set_voltage, |
| 1124 | .get_voltage = vreg_get_voltage, |
| 1125 | .list_voltage = vreg_list_voltage, |
| 1126 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1127 | }; |
| 1128 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1129 | /* Pin control regulator operations. */ |
| 1130 | static struct regulator_ops pin_control_ops = { |
| 1131 | .enable = vreg_pin_control_enable, |
| 1132 | .disable = vreg_pin_control_disable, |
| 1133 | .is_enabled = vreg_pin_control_is_enabled, |
| 1134 | }; |
| 1135 | |
| 1136 | struct regulator_ops *vreg_ops[] = { |
| 1137 | [RPM_REGULATOR_TYPE_LDO] = &ldo_ops, |
| 1138 | [RPM_REGULATOR_TYPE_SMPS] = &smps_ops, |
| 1139 | [RPM_REGULATOR_TYPE_VS] = &switch_ops, |
| 1140 | [RPM_REGULATOR_TYPE_NCP] = &ncp_ops, |
| 1141 | }; |
| 1142 | |
| 1143 | static int __devinit |
| 1144 | rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata, |
| 1145 | struct device *dev) |
| 1146 | { |
| 1147 | struct regulator_desc *rdesc = NULL; |
| 1148 | struct regulator_dev *rdev; |
| 1149 | struct vreg *vreg; |
| 1150 | unsigned pin_ctrl; |
| 1151 | int id, pin_fn; |
| 1152 | int rc = 0; |
| 1153 | |
| 1154 | if (!pdata) { |
| 1155 | pr_err("platform data missing\n"); |
| 1156 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1159 | id = pdata->id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1160 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1161 | if (id < config->vreg_id_min || id > config->vreg_id_max) { |
| 1162 | pr_err("invalid regulator id: %d\n", id); |
| 1163 | return -ENODEV; |
| 1164 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1165 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1166 | if (!config->is_real_id(pdata->id)) |
| 1167 | id = config->pc_id_to_real_id(pdata->id); |
| 1168 | vreg = &config->vregs[id]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1169 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1170 | if (config->is_real_id(pdata->id)) |
| 1171 | rdesc = &vreg->rdesc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1172 | else |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1173 | rdesc = &vreg->rdesc_pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1174 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1175 | if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) { |
| 1176 | pr_err("%s: invalid regulator type: %d\n", |
| 1177 | vreg->rdesc.name, vreg->type); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1178 | return -EINVAL; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1179 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1180 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1181 | mutex_lock(&vreg->pc_lock); |
| 1182 | |
| 1183 | if (vreg->set_points) |
| 1184 | rdesc->n_voltages = vreg->set_points->n_voltages; |
| 1185 | else |
| 1186 | rdesc->n_voltages = 0; |
| 1187 | |
| 1188 | rdesc->id = pdata->id; |
| 1189 | rdesc->owner = THIS_MODULE; |
| 1190 | rdesc->type = REGULATOR_VOLTAGE; |
| 1191 | |
| 1192 | if (config->is_real_id(pdata->id)) { |
| 1193 | /* |
| 1194 | * Real regulator; do not modify pin control and pin function |
| 1195 | * values. |
| 1196 | */ |
| 1197 | rdesc->ops = vreg_ops[vreg->type]; |
| 1198 | pin_ctrl = vreg->pdata.pin_ctrl; |
| 1199 | pin_fn = vreg->pdata.pin_fn; |
| 1200 | memcpy(&(vreg->pdata), pdata, |
| 1201 | sizeof(struct rpm_regulator_init_data)); |
| 1202 | vreg->pdata.pin_ctrl = pin_ctrl; |
| 1203 | vreg->pdata.pin_fn = pin_fn; |
| 1204 | |
| 1205 | vreg->save_uV = vreg->pdata.default_uV; |
| 1206 | if (vreg->pdata.peak_uA >= vreg->hpm_min_load) |
| 1207 | vreg->mode = config->mode_hpm; |
| 1208 | else |
| 1209 | vreg->mode = config->mode_lpm; |
| 1210 | |
| 1211 | /* Initialize the RPM request. */ |
| 1212 | SET_PART(vreg, ip, |
| 1213 | MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA))); |
| 1214 | SET_PART(vreg, fm, vreg->pdata.force_mode); |
| 1215 | SET_PART(vreg, pm, vreg->pdata.power_mode); |
| 1216 | SET_PART(vreg, pd, vreg->pdata.pull_down_enable); |
| 1217 | SET_PART(vreg, ia, |
| 1218 | MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA))); |
| 1219 | SET_PART(vreg, freq, vreg->pdata.freq); |
| 1220 | SET_PART(vreg, freq_clk_src, 0); |
| 1221 | SET_PART(vreg, comp_mode, 0); |
| 1222 | SET_PART(vreg, hpm, 0); |
| 1223 | if (!vreg->is_enabled_pc) { |
| 1224 | SET_PART(vreg, pf, config->pin_func_none); |
| 1225 | SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE); |
| 1226 | } |
| 1227 | } else { |
| 1228 | if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL) |
| 1229 | == RPM_VREG_PIN_CTRL_NONE |
| 1230 | && pdata->pin_fn != config->pin_func_sleep_b) { |
| 1231 | pr_err("%s: no pin control input specified\n", |
| 1232 | vreg->rdesc.name); |
| 1233 | mutex_unlock(&vreg->pc_lock); |
| 1234 | return -EINVAL; |
| 1235 | } |
| 1236 | rdesc->ops = &pin_control_ops; |
| 1237 | vreg->pdata.pin_ctrl = pdata->pin_ctrl; |
| 1238 | vreg->pdata.pin_fn = pdata->pin_fn; |
| 1239 | |
| 1240 | /* Initialize the RPM request. */ |
| 1241 | pin_fn = config->pin_func_none; |
| 1242 | /* Allow pf=sleep_b to be specified by platform data. */ |
| 1243 | if (vreg->pdata.pin_fn == config->pin_func_sleep_b) |
| 1244 | pin_fn = config->pin_func_sleep_b; |
| 1245 | SET_PART(vreg, pf, pin_fn); |
| 1246 | SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE); |
| 1247 | } |
| 1248 | |
| 1249 | mutex_unlock(&vreg->pc_lock); |
| 1250 | |
| 1251 | if (rc) |
| 1252 | goto bail; |
| 1253 | |
| 1254 | rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg); |
| 1255 | if (IS_ERR(rdev)) { |
| 1256 | rc = PTR_ERR(rdev); |
| 1257 | pr_err("regulator_register failed: %s, rc=%d\n", |
| 1258 | vreg->rdesc.name, rc); |
| 1259 | return rc; |
| 1260 | } else { |
| 1261 | if (config->is_real_id(pdata->id)) |
| 1262 | vreg->rdev = rdev; |
| 1263 | else |
| 1264 | vreg->rdev_pc = rdev; |
| 1265 | } |
| 1266 | |
| 1267 | bail: |
| 1268 | if (rc) |
| 1269 | pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc); |
| 1270 | |
| 1271 | return rc; |
| 1272 | } |
| 1273 | |
| 1274 | static void rpm_vreg_set_point_init(void) |
| 1275 | { |
| 1276 | struct vreg_set_points **set_points; |
| 1277 | int i, j, temp; |
| 1278 | |
| 1279 | set_points = config->set_points; |
| 1280 | |
| 1281 | /* Calculate the number of set points available for each regulator. */ |
| 1282 | for (i = 0; i < config->set_points_len; i++) { |
| 1283 | temp = 0; |
| 1284 | for (j = 0; j < set_points[i]->count; j++) { |
| 1285 | set_points[i]->range[j].n_voltages |
| 1286 | = (set_points[i]->range[j].max_uV |
| 1287 | - set_points[i]->range[j].min_uV) |
| 1288 | / set_points[i]->range[j].step_uV + 1; |
| 1289 | temp += set_points[i]->range[j].n_voltages; |
| 1290 | } |
| 1291 | set_points[i]->n_voltages = temp; |
| 1292 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | static int __devinit rpm_vreg_probe(struct platform_device *pdev) |
| 1296 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1297 | struct rpm_regulator_platform_data *platform_data; |
| 1298 | int rc = 0; |
| 1299 | int i, id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1300 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1301 | platform_data = pdev->dev.platform_data; |
| 1302 | if (!platform_data) { |
| 1303 | pr_err("rpm-regulator requires platform data\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1304 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1307 | if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX |
| 1308 | && platform_data->version != rpm_version) { |
| 1309 | pr_err("rpm version %d does not match previous version %d\n", |
| 1310 | platform_data->version, rpm_version); |
| 1311 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1314 | if (platform_data->version < 0 |
| 1315 | || platform_data->version > RPM_VREG_VERSION_MAX) { |
| 1316 | pr_err("rpm version %d is invalid\n", platform_data->version); |
| 1317 | return -EINVAL; |
| 1318 | } |
| 1319 | |
| 1320 | if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) { |
| 1321 | rpm_version = platform_data->version; |
| 1322 | config = get_config[platform_data->version](); |
| 1323 | vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem; |
| 1324 | vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig; |
| 1325 | if (!config) { |
| 1326 | pr_err("rpm version %d is not available\n", |
| 1327 | platform_data->version); |
| 1328 | return -ENODEV; |
| 1329 | } |
| 1330 | if (config->use_legacy_optimum_mode) |
| 1331 | for (i = 0; i < ARRAY_SIZE(vreg_ops); i++) |
| 1332 | vreg_ops[i]->get_optimum_mode |
| 1333 | = vreg_legacy_get_optimum_mode; |
| 1334 | rpm_vreg_set_point_init(); |
| 1335 | /* First time probed; initialize pin control mutexes. */ |
| 1336 | for (i = 0; i < config->vregs_len; i++) |
| 1337 | mutex_init(&config->vregs[i].pc_lock); |
| 1338 | } |
| 1339 | |
| 1340 | /* Initialize all of the regulators listed in the platform data. */ |
| 1341 | for (i = 0; i < platform_data->num_regulators; i++) { |
| 1342 | rc = rpm_vreg_init_regulator(&platform_data->init_data[i], |
| 1343 | &pdev->dev); |
| 1344 | if (rc) { |
| 1345 | pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc); |
| 1346 | goto remove_regulators; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | platform_set_drvdata(pdev, platform_data); |
| 1351 | |
| 1352 | return rc; |
| 1353 | |
| 1354 | remove_regulators: |
| 1355 | /* Unregister all regulators added before the erroring one. */ |
| 1356 | for (; i >= 0; i--) { |
| 1357 | id = platform_data->init_data[i].id; |
| 1358 | if (config->is_real_id(id)) { |
| 1359 | regulator_unregister(config->vregs[id].rdev); |
| 1360 | config->vregs[id].rdev = NULL; |
| 1361 | } else { |
| 1362 | regulator_unregister(config->vregs[ |
| 1363 | config->pc_id_to_real_id(id)].rdev_pc); |
| 1364 | config->vregs[id].rdev_pc = NULL; |
| 1365 | } |
| 1366 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1367 | |
| 1368 | return rc; |
| 1369 | } |
| 1370 | |
| 1371 | static int __devexit rpm_vreg_remove(struct platform_device *pdev) |
| 1372 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1373 | struct rpm_regulator_platform_data *platform_data; |
| 1374 | int i, id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1375 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1376 | platform_data = platform_get_drvdata(pdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1377 | platform_set_drvdata(pdev, NULL); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1378 | |
| 1379 | if (platform_data) { |
| 1380 | for (i = 0; i < platform_data->num_regulators; i++) { |
| 1381 | id = platform_data->init_data[i].id; |
| 1382 | if (config->is_real_id(id)) { |
| 1383 | regulator_unregister(config->vregs[id].rdev); |
| 1384 | config->vregs[id].rdev = NULL; |
| 1385 | } else { |
| 1386 | regulator_unregister(config->vregs[ |
| 1387 | config->pc_id_to_real_id(id)].rdev_pc); |
| 1388 | config->vregs[id].rdev_pc = NULL; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | static struct platform_driver rpm_vreg_driver = { |
| 1397 | .probe = rpm_vreg_probe, |
| 1398 | .remove = __devexit_p(rpm_vreg_remove), |
| 1399 | .driver = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1400 | .name = RPM_REGULATOR_DEV_NAME, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1401 | .owner = THIS_MODULE, |
| 1402 | }, |
| 1403 | }; |
| 1404 | |
| 1405 | static int __init rpm_vreg_init(void) |
| 1406 | { |
| 1407 | return platform_driver_register(&rpm_vreg_driver); |
| 1408 | } |
| 1409 | |
| 1410 | static void __exit rpm_vreg_exit(void) |
| 1411 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1412 | int i; |
| 1413 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1414 | platform_driver_unregister(&rpm_vreg_driver); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1415 | |
| 1416 | for (i = 0; i < config->vregs_len; i++) |
| 1417 | mutex_destroy(&config->vregs[i].pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | postcore_initcall(rpm_vreg_init); |
| 1421 | module_exit(rpm_vreg_exit); |
| 1422 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1423 | MODULE_LICENSE("GPL v2"); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1424 | MODULE_DESCRIPTION("MSM RPM regulator driver"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1425 | MODULE_VERSION("1.0"); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1426 | MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME); |