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