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; |
David Collins | 3974b61 | 2011-11-21 15:07:36 -0800 | [diff] [blame^] | 493 | |
| 494 | if (uV > max_uV) { |
| 495 | vreg_err(vreg, |
| 496 | "request v=[%d, %d] cannot be met by any set point; " |
| 497 | "next set point: %d\n", |
| 498 | min_uV, max_uV, uV); |
| 499 | return -EINVAL; |
| 500 | } |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 501 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 502 | |
| 503 | if (vreg->part->uV.mask) { |
| 504 | val[vreg->part->uV.word] = uV << vreg->part->uV.shift; |
| 505 | mask[vreg->part->uV.word] = vreg->part->uV.mask; |
| 506 | } else { |
| 507 | val[vreg->part->mV.word] |
| 508 | = MICRO_TO_MILLI(uV) << vreg->part->mV.shift; |
| 509 | mask[vreg->part->mV.word] = vreg->part->mV.mask; |
| 510 | } |
| 511 | |
| 512 | rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1], |
| 513 | val[1], vreg->part->request_len, 1); |
| 514 | if (rc) |
| 515 | vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 516 | |
| 517 | return rc; |
| 518 | } |
| 519 | EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage); |
| 520 | |
| 521 | /** |
| 522 | * rpm_vreg_set_frequency - sets the frequency of a switching regulator |
| 523 | * @vreg: ID for regulator |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 524 | * @freq: enum corresponding to desired frequency |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 525 | * |
| 526 | * Returns 0 on success or errno. |
| 527 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 528 | 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] | 529 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 530 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 531 | struct vreg *vreg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 532 | int rc; |
| 533 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 534 | /* |
| 535 | * HACK: make this function a no-op for 8064 so that it can be called by |
| 536 | * consumers on 8064 before RPM capabilities are present. |
| 537 | */ |
| 538 | if (cpu_is_apq8064()) |
| 539 | return 0; |
| 540 | |
| 541 | if (!config) { |
| 542 | pr_err("rpm-regulator driver has not probed yet.\n"); |
| 543 | return -ENODEV; |
| 544 | } |
| 545 | |
| 546 | if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) { |
| 547 | pr_err("invalid regulator id=%d\n", vreg_id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 548 | return -EINVAL; |
| 549 | } |
| 550 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 551 | vreg = &config->vregs[vreg_id]; |
| 552 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 553 | if (freq < 0 || freq > RPM_VREG_FREQ_1p20) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 554 | vreg_err(vreg, "invalid frequency=%d\n", freq); |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | if (!vreg->pdata.sleep_selectable) { |
| 558 | vreg_err(vreg, "regulator is not marked sleep selectable\n"); |
| 559 | return -EINVAL; |
| 560 | } |
| 561 | if (!vreg->part->freq.mask) { |
| 562 | vreg_err(vreg, "frequency not supported\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 563 | return -EINVAL; |
| 564 | } |
| 565 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 566 | val[vreg->part->freq.word] = freq << vreg->part->freq.shift; |
| 567 | mask[vreg->part->freq.word] = vreg->part->freq.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 568 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 569 | rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0], |
| 570 | val[0], mask[1], val[1], vreg->part->request_len, 0); |
| 571 | if (rc) |
| 572 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 573 | |
| 574 | return rc; |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency); |
| 577 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 578 | static inline int vreg_hpm_min_uA(struct vreg *vreg) |
| 579 | { |
| 580 | return vreg->hpm_min_load; |
| 581 | } |
| 582 | |
| 583 | static inline int vreg_lpm_max_uA(struct vreg *vreg) |
| 584 | { |
| 585 | return vreg->hpm_min_load - LOAD_THRESHOLD_STEP; |
| 586 | } |
| 587 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 588 | static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 589 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 590 | unsigned load_max |
| 591 | = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift); |
| 592 | |
| 593 | return (load_uA > load_max ? load_max : load_uA); |
| 594 | } |
| 595 | |
| 596 | static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA) |
| 597 | { |
| 598 | unsigned load_max |
| 599 | = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift); |
| 600 | return (load_uA > load_max ? load_max : load_uA); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* Change vreg->req, but do not send it to the RPM. */ |
| 604 | static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0, |
| 605 | unsigned mask1, unsigned val1) |
| 606 | { |
| 607 | unsigned long flags = 0; |
| 608 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 609 | if (vreg->pdata.sleep_selectable) |
| 610 | spin_lock_irqsave(&rpm_noirq_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | |
| 612 | vreg->req[0].value &= ~mask0; |
| 613 | vreg->req[0].value |= val0 & mask0; |
| 614 | |
| 615 | vreg->req[1].value &= ~mask1; |
| 616 | vreg->req[1].value |= val1 & mask1; |
| 617 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 618 | if (vreg->pdata.sleep_selectable) |
| 619 | spin_unlock_irqrestore(&rpm_noirq_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0, |
| 625 | unsigned mask1, unsigned val1, unsigned cnt) |
| 626 | { |
| 627 | unsigned prev0 = 0, prev1 = 0; |
| 628 | int rc; |
| 629 | |
| 630 | /* |
| 631 | * Bypass the normal route for regulators that can be called to change |
| 632 | * just the active set values. |
| 633 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 634 | if (vreg->pdata.sleep_selectable) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 635 | return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, |
| 636 | mask0, val0, mask1, val1, cnt, 1); |
| 637 | |
| 638 | prev0 = vreg->req[0].value; |
| 639 | vreg->req[0].value &= ~mask0; |
| 640 | vreg->req[0].value |= val0 & mask0; |
| 641 | |
| 642 | prev1 = vreg->req[1].value; |
| 643 | vreg->req[1].value &= ~mask1; |
| 644 | vreg->req[1].value |= val1 & mask1; |
| 645 | |
| 646 | /* Ignore duplicate requests */ |
| 647 | if (vreg->req[0].value == vreg->prev_active_req[0].value && |
| 648 | vreg->req[1].value == vreg->prev_active_req[1].value) { |
| 649 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 650 | rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt); |
| 655 | if (rc) { |
| 656 | vreg->req[0].value = prev0; |
| 657 | vreg->req[1].value = prev1; |
| 658 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 659 | vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n", |
| 660 | vreg->req[0].id, rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 661 | } else { |
| 662 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 663 | rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 664 | vreg->prev_active_req[0].value = vreg->req[0].value; |
| 665 | vreg->prev_active_req[1].value = vreg->req[1].value; |
| 666 | } |
| 667 | |
| 668 | return rc; |
| 669 | } |
| 670 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 671 | static int vreg_is_enabled(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 672 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 673 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 674 | int enabled; |
| 675 | |
| 676 | mutex_lock(&vreg->pc_lock); |
| 677 | enabled = vreg->is_enabled; |
| 678 | mutex_unlock(&vreg->pc_lock); |
| 679 | |
| 680 | return enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 681 | } |
| 682 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 683 | 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] | 684 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 685 | switch (vreg->type) { |
| 686 | case RPM_REGULATOR_TYPE_LDO: |
| 687 | case RPM_REGULATOR_TYPE_SMPS: |
| 688 | /* Enable by setting a voltage. */ |
| 689 | if (vreg->part->uV.mask) { |
| 690 | val[vreg->part->uV.word] |
| 691 | |= vreg->save_uV << vreg->part->uV.shift; |
| 692 | mask[vreg->part->uV.word] |= vreg->part->uV.mask; |
| 693 | } else { |
| 694 | val[vreg->part->mV.word] |
| 695 | |= MICRO_TO_MILLI(vreg->save_uV) |
| 696 | << vreg->part->mV.shift; |
| 697 | mask[vreg->part->mV.word] |= vreg->part->mV.mask; |
| 698 | } |
| 699 | break; |
| 700 | case RPM_REGULATOR_TYPE_VS: |
| 701 | case RPM_REGULATOR_TYPE_NCP: |
| 702 | /* Enable by setting enable_state. */ |
| 703 | val[vreg->part->enable_state.word] |
| 704 | |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift; |
| 705 | mask[vreg->part->enable_state.word] |
| 706 | |= vreg->part->enable_state.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 707 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 708 | } |
| 709 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 710 | static int vreg_enable(struct regulator_dev *rdev) |
| 711 | { |
| 712 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 713 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 714 | int rc = 0; |
| 715 | |
| 716 | set_enable(vreg, mask, val); |
| 717 | |
| 718 | mutex_lock(&vreg->pc_lock); |
| 719 | |
| 720 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 721 | vreg->part->request_len); |
| 722 | if (!rc) |
| 723 | vreg->is_enabled = true; |
| 724 | |
| 725 | mutex_unlock(&vreg->pc_lock); |
| 726 | |
| 727 | if (rc) |
| 728 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 729 | |
| 730 | return rc; |
| 731 | } |
| 732 | |
| 733 | static void set_disable(struct vreg *vreg, unsigned int *mask, |
| 734 | unsigned int *val) |
| 735 | { |
| 736 | switch (vreg->type) { |
| 737 | case RPM_REGULATOR_TYPE_LDO: |
| 738 | case RPM_REGULATOR_TYPE_SMPS: |
| 739 | /* Disable by setting a voltage of 0 uV. */ |
| 740 | if (vreg->part->uV.mask) { |
| 741 | val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift; |
| 742 | mask[vreg->part->uV.word] |= vreg->part->uV.mask; |
| 743 | } else { |
| 744 | val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift; |
| 745 | mask[vreg->part->mV.word] |= vreg->part->mV.mask; |
| 746 | } |
| 747 | break; |
| 748 | case RPM_REGULATOR_TYPE_VS: |
| 749 | case RPM_REGULATOR_TYPE_NCP: |
| 750 | /* Disable by setting enable_state. */ |
| 751 | val[vreg->part->enable_state.word] |
| 752 | |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift; |
| 753 | mask[vreg->part->enable_state.word] |
| 754 | |= vreg->part->enable_state.mask; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | static int vreg_disable(struct regulator_dev *rdev) |
| 759 | { |
| 760 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 761 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 762 | int rc = 0; |
| 763 | |
| 764 | set_disable(vreg, mask, val); |
| 765 | |
| 766 | mutex_lock(&vreg->pc_lock); |
| 767 | |
| 768 | /* Only disable if pin control is not in use. */ |
| 769 | if (!vreg->is_enabled_pc) |
| 770 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 771 | vreg->part->request_len); |
| 772 | |
| 773 | if (!rc) |
| 774 | vreg->is_enabled = false; |
| 775 | |
| 776 | mutex_unlock(&vreg->pc_lock); |
| 777 | |
| 778 | if (rc) |
| 779 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 780 | |
| 781 | return rc; |
| 782 | } |
| 783 | |
| 784 | 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] | 785 | unsigned *selector) |
| 786 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 787 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 788 | struct vreg_range *range = &vreg->set_points->range[0]; |
| 789 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 790 | int rc = 0, uV = min_uV; |
| 791 | int lim_min_uV, lim_max_uV, i; |
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 | /* Check if request voltage is outside of physically settable range. */ |
| 794 | lim_min_uV = vreg->set_points->range[0].min_uV; |
| 795 | lim_max_uV = |
| 796 | vreg->set_points->range[vreg->set_points->count - 1].max_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 797 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 798 | if (uV < lim_min_uV && max_uV >= lim_min_uV) |
| 799 | uV = lim_min_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 800 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 801 | if (uV < lim_min_uV || uV > lim_max_uV) { |
| 802 | vreg_err(vreg, |
| 803 | "request v=[%d, %d] is outside possible v=[%d, %d]\n", |
| 804 | min_uV, max_uV, lim_min_uV, lim_max_uV); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 805 | return -EINVAL; |
| 806 | } |
| 807 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 808 | /* Find the range which uV is inside of. */ |
| 809 | for (i = vreg->set_points->count - 1; i > 0; i--) { |
| 810 | if (uV > vreg->set_points->range[i - 1].max_uV) { |
| 811 | range = &vreg->set_points->range[i]; |
| 812 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 816 | /* |
| 817 | * Force uV to be an allowed set point and apply a ceiling function |
| 818 | * to non-set point values. |
| 819 | */ |
| 820 | uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV; |
| 821 | uV = uV * range->step_uV + range->min_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 822 | |
David Collins | 3974b61 | 2011-11-21 15:07:36 -0800 | [diff] [blame^] | 823 | if (uV > max_uV) { |
| 824 | vreg_err(vreg, |
| 825 | "request v=[%d, %d] cannot be met by any set point; " |
| 826 | "next set point: %d\n", |
| 827 | min_uV, max_uV, uV); |
| 828 | return -EINVAL; |
| 829 | } |
| 830 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 831 | if (vreg->part->uV.mask) { |
| 832 | val[vreg->part->uV.word] = uV << vreg->part->uV.shift; |
| 833 | mask[vreg->part->uV.word] = vreg->part->uV.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 834 | } else { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 835 | val[vreg->part->mV.word] |
| 836 | = MICRO_TO_MILLI(uV) << vreg->part->mV.shift; |
| 837 | mask[vreg->part->mV.word] = vreg->part->mV.mask; |
| 838 | } |
| 839 | |
| 840 | mutex_lock(&vreg->pc_lock); |
| 841 | |
| 842 | /* |
| 843 | * Only send a request for a new voltage if the regulator is currently |
| 844 | * enabled. This will ensure that LDO and SMPS regulators are not |
| 845 | * inadvertently turned on because voltage > 0 is equivalent to |
| 846 | * enabling. For NCP, this just removes unnecessary RPM requests. |
| 847 | */ |
| 848 | if (vreg->is_enabled) { |
| 849 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 850 | vreg->part->request_len); |
| 851 | if (rc) |
| 852 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 853 | } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 854 | /* Regulator is disabled; store but don't send new request. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 855 | rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 856 | } |
| 857 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 858 | if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled)) |
| 859 | vreg->save_uV = uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 860 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 861 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 862 | |
| 863 | return rc; |
| 864 | } |
| 865 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 866 | static int vreg_get_voltage(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 867 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 868 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 869 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 870 | return vreg->save_uV; |
| 871 | } |
| 872 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 873 | static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 874 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 875 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 876 | int uV = 0; |
| 877 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 878 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 879 | if (!vreg->set_points) { |
| 880 | vreg_err(vreg, "no voltages available\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 881 | return -EINVAL; |
| 882 | } |
| 883 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 884 | if (selector >= vreg->set_points->n_voltages) |
| 885 | return 0; |
| 886 | |
| 887 | for (i = 0; i < vreg->set_points->count; i++) { |
| 888 | if (selector < vreg->set_points->range[i].n_voltages) { |
| 889 | uV = selector * vreg->set_points->range[i].step_uV |
| 890 | + vreg->set_points->range[i].min_uV; |
| 891 | break; |
| 892 | } else { |
| 893 | selector -= vreg->set_points->range[i].n_voltages; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | return uV; |
| 898 | } |
| 899 | |
| 900 | static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 901 | { |
| 902 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 903 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 904 | int rc = 0; |
| 905 | int peak_uA; |
| 906 | |
| 907 | mutex_lock(&vreg->pc_lock); |
| 908 | |
| 909 | peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value |
| 910 | & vreg->part->ip.mask) >> vreg->part->ip.shift); |
| 911 | |
| 912 | if (mode == config->mode_hpm) { |
| 913 | /* Make sure that request currents are in HPM range. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 914 | if (peak_uA < vreg_hpm_min_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_hpm_min_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_hpm_min_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 if (mode == config->mode_lpm) { |
| 929 | /* Make sure that request currents are in LPM range. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 930 | if (peak_uA > vreg_lpm_max_uA(vreg)) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 931 | val[vreg->part->ip.word] |
| 932 | = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg)) |
| 933 | << vreg->part->ip.shift; |
| 934 | mask[vreg->part->ip.word] = vreg->part->ip.mask; |
| 935 | |
| 936 | if (config->ia_follows_ip) { |
| 937 | val[vreg->part->ia.word] |
| 938 | |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg)) |
| 939 | << vreg->part->ia.shift; |
| 940 | mask[vreg->part->ia.word] |
| 941 | |= vreg->part->ia.mask; |
| 942 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 943 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 944 | } else { |
| 945 | vreg_err(vreg, "invalid mode: %u\n", mode); |
| 946 | mutex_unlock(&vreg->pc_lock); |
| 947 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 948 | } |
| 949 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 950 | if (vreg->is_enabled) { |
| 951 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 952 | vreg->part->request_len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 953 | } else { |
| 954 | /* Regulator is disabled; store but don't send new request. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 955 | rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 956 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 957 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 958 | if (rc) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 959 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 960 | else |
| 961 | vreg->mode = mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 962 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 963 | mutex_unlock(&vreg->pc_lock); |
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 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 966 | } |
| 967 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 968 | static unsigned int vreg_get_mode(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 969 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 970 | struct vreg *vreg = rdev_get_drvdata(rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 971 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 972 | return vreg->mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 973 | } |
| 974 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 975 | static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev, |
| 976 | int input_uV, int output_uV, int load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 977 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 978 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 979 | unsigned int mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 980 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 981 | load_uA += vreg->pdata.system_uA; |
| 982 | |
| 983 | mutex_lock(&vreg->pc_lock); |
| 984 | SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA))); |
| 985 | if (config->ia_follows_ip) |
| 986 | SET_PART(vreg, ia, |
| 987 | MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA))); |
| 988 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 989 | |
| 990 | if (load_uA >= vreg->hpm_min_load) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 991 | mode = config->mode_hpm; |
| 992 | else |
| 993 | mode = config->mode_lpm; |
| 994 | |
| 995 | return mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 996 | } |
| 997 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 998 | static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev, |
| 999 | int input_uV, int output_uV, int load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1000 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1001 | struct vreg *vreg = rdev_get_drvdata(rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1002 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1003 | if (MICRO_TO_MILLI(load_uA) <= 0) { |
| 1004 | /* |
| 1005 | * vreg_legacy_get_optimum_mode is being called before consumers |
| 1006 | * have specified their load currents via |
| 1007 | * regulator_set_optimum_mode. Return whatever the existing mode |
| 1008 | * is. |
| 1009 | */ |
| 1010 | return vreg->mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1013 | return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1017 | * Returns the logical pin control enable state because the pin control options |
| 1018 | * present in the hardware out of restart could be different from those desired |
| 1019 | * by the consumer. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1020 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1021 | static int vreg_pin_control_is_enabled(struct regulator_dev *rdev) |
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 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1024 | |
| 1025 | return vreg->is_enabled_pc; |
| 1026 | } |
| 1027 | |
| 1028 | static int vreg_pin_control_enable(struct regulator_dev *rdev) |
| 1029 | { |
| 1030 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1031 | unsigned int mask[2] = {0}, val[2] = {0}; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1032 | int rc; |
| 1033 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1034 | mutex_lock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1035 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1036 | val[vreg->part->pc.word] |
| 1037 | |= vreg->pdata.pin_ctrl << vreg->part->pc.shift; |
| 1038 | mask[vreg->part->pc.word] |= vreg->part->pc.mask; |
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 | val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift; |
| 1041 | mask[vreg->part->pf.word] |= vreg->part->pf.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1042 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1043 | if (!vreg->is_enabled) |
| 1044 | set_enable(vreg, mask, val); |
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 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1047 | vreg->part->request_len); |
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 | if (!rc) |
| 1050 | vreg->is_enabled_pc = true; |
| 1051 | |
| 1052 | mutex_unlock(&vreg->pc_lock); |
| 1053 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1054 | if (rc) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1055 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1056 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1057 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1060 | static int vreg_pin_control_disable(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1061 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1062 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1063 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 1064 | int pin_fn, rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1065 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1066 | mutex_lock(&vreg->pc_lock); |
| 1067 | |
| 1068 | val[vreg->part->pc.word] |
| 1069 | |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift; |
| 1070 | mask[vreg->part->pc.word] |= vreg->part->pc.mask; |
| 1071 | |
| 1072 | pin_fn = config->pin_func_none; |
| 1073 | if (vreg->pdata.pin_fn == config->pin_func_sleep_b) |
| 1074 | pin_fn = config->pin_func_sleep_b; |
| 1075 | val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift; |
| 1076 | mask[vreg->part->pf.word] |= vreg->part->pf.mask; |
| 1077 | |
| 1078 | if (!vreg->is_enabled) |
| 1079 | set_disable(vreg, mask, val); |
| 1080 | |
| 1081 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1082 | vreg->part->request_len); |
| 1083 | |
| 1084 | if (!rc) |
| 1085 | vreg->is_enabled_pc = false; |
| 1086 | |
| 1087 | mutex_unlock(&vreg->pc_lock); |
| 1088 | |
| 1089 | if (rc) |
| 1090 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 1091 | |
| 1092 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1095 | static int vreg_enable_time(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1096 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1097 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1098 | |
| 1099 | return vreg->pdata.enable_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1102 | /* Real regulator operations. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1103 | static struct regulator_ops ldo_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1104 | .enable = vreg_enable, |
| 1105 | .disable = vreg_disable, |
| 1106 | .is_enabled = vreg_is_enabled, |
| 1107 | .set_voltage = vreg_set_voltage, |
| 1108 | .get_voltage = vreg_get_voltage, |
| 1109 | .list_voltage = vreg_list_voltage, |
| 1110 | .set_mode = vreg_set_mode, |
| 1111 | .get_mode = vreg_get_mode, |
| 1112 | .get_optimum_mode = vreg_get_optimum_mode, |
| 1113 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1114 | }; |
| 1115 | |
| 1116 | static struct regulator_ops smps_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1117 | .enable = vreg_enable, |
| 1118 | .disable = vreg_disable, |
| 1119 | .is_enabled = vreg_is_enabled, |
| 1120 | .set_voltage = vreg_set_voltage, |
| 1121 | .get_voltage = vreg_get_voltage, |
| 1122 | .list_voltage = vreg_list_voltage, |
| 1123 | .set_mode = vreg_set_mode, |
| 1124 | .get_mode = vreg_get_mode, |
| 1125 | .get_optimum_mode = vreg_get_optimum_mode, |
| 1126 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1127 | }; |
| 1128 | |
| 1129 | static struct regulator_ops switch_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1130 | .enable = vreg_enable, |
| 1131 | .disable = vreg_disable, |
| 1132 | .is_enabled = vreg_is_enabled, |
| 1133 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1134 | }; |
| 1135 | |
| 1136 | static struct regulator_ops ncp_ops = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1137 | .enable = vreg_enable, |
| 1138 | .disable = vreg_disable, |
| 1139 | .is_enabled = vreg_is_enabled, |
| 1140 | .set_voltage = vreg_set_voltage, |
| 1141 | .get_voltage = vreg_get_voltage, |
| 1142 | .list_voltage = vreg_list_voltage, |
| 1143 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1144 | }; |
| 1145 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1146 | /* Pin control regulator operations. */ |
| 1147 | static struct regulator_ops pin_control_ops = { |
| 1148 | .enable = vreg_pin_control_enable, |
| 1149 | .disable = vreg_pin_control_disable, |
| 1150 | .is_enabled = vreg_pin_control_is_enabled, |
| 1151 | }; |
| 1152 | |
| 1153 | struct regulator_ops *vreg_ops[] = { |
| 1154 | [RPM_REGULATOR_TYPE_LDO] = &ldo_ops, |
| 1155 | [RPM_REGULATOR_TYPE_SMPS] = &smps_ops, |
| 1156 | [RPM_REGULATOR_TYPE_VS] = &switch_ops, |
| 1157 | [RPM_REGULATOR_TYPE_NCP] = &ncp_ops, |
| 1158 | }; |
| 1159 | |
| 1160 | static int __devinit |
| 1161 | rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata, |
| 1162 | struct device *dev) |
| 1163 | { |
| 1164 | struct regulator_desc *rdesc = NULL; |
| 1165 | struct regulator_dev *rdev; |
| 1166 | struct vreg *vreg; |
| 1167 | unsigned pin_ctrl; |
| 1168 | int id, pin_fn; |
| 1169 | int rc = 0; |
| 1170 | |
| 1171 | if (!pdata) { |
| 1172 | pr_err("platform data missing\n"); |
| 1173 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1176 | id = pdata->id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1177 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1178 | if (id < config->vreg_id_min || id > config->vreg_id_max) { |
| 1179 | pr_err("invalid regulator id: %d\n", id); |
| 1180 | return -ENODEV; |
| 1181 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1182 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1183 | if (!config->is_real_id(pdata->id)) |
| 1184 | id = config->pc_id_to_real_id(pdata->id); |
| 1185 | vreg = &config->vregs[id]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1186 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1187 | if (config->is_real_id(pdata->id)) |
| 1188 | rdesc = &vreg->rdesc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1189 | else |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1190 | rdesc = &vreg->rdesc_pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1191 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1192 | if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) { |
| 1193 | pr_err("%s: invalid regulator type: %d\n", |
| 1194 | vreg->rdesc.name, vreg->type); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1195 | return -EINVAL; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1196 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1197 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1198 | mutex_lock(&vreg->pc_lock); |
| 1199 | |
| 1200 | if (vreg->set_points) |
| 1201 | rdesc->n_voltages = vreg->set_points->n_voltages; |
| 1202 | else |
| 1203 | rdesc->n_voltages = 0; |
| 1204 | |
| 1205 | rdesc->id = pdata->id; |
| 1206 | rdesc->owner = THIS_MODULE; |
| 1207 | rdesc->type = REGULATOR_VOLTAGE; |
| 1208 | |
| 1209 | if (config->is_real_id(pdata->id)) { |
| 1210 | /* |
| 1211 | * Real regulator; do not modify pin control and pin function |
| 1212 | * values. |
| 1213 | */ |
| 1214 | rdesc->ops = vreg_ops[vreg->type]; |
| 1215 | pin_ctrl = vreg->pdata.pin_ctrl; |
| 1216 | pin_fn = vreg->pdata.pin_fn; |
| 1217 | memcpy(&(vreg->pdata), pdata, |
| 1218 | sizeof(struct rpm_regulator_init_data)); |
| 1219 | vreg->pdata.pin_ctrl = pin_ctrl; |
| 1220 | vreg->pdata.pin_fn = pin_fn; |
| 1221 | |
| 1222 | vreg->save_uV = vreg->pdata.default_uV; |
| 1223 | if (vreg->pdata.peak_uA >= vreg->hpm_min_load) |
| 1224 | vreg->mode = config->mode_hpm; |
| 1225 | else |
| 1226 | vreg->mode = config->mode_lpm; |
| 1227 | |
| 1228 | /* Initialize the RPM request. */ |
| 1229 | SET_PART(vreg, ip, |
| 1230 | MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA))); |
| 1231 | SET_PART(vreg, fm, vreg->pdata.force_mode); |
| 1232 | SET_PART(vreg, pm, vreg->pdata.power_mode); |
| 1233 | SET_PART(vreg, pd, vreg->pdata.pull_down_enable); |
| 1234 | SET_PART(vreg, ia, |
| 1235 | MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA))); |
| 1236 | SET_PART(vreg, freq, vreg->pdata.freq); |
| 1237 | SET_PART(vreg, freq_clk_src, 0); |
| 1238 | SET_PART(vreg, comp_mode, 0); |
| 1239 | SET_PART(vreg, hpm, 0); |
| 1240 | if (!vreg->is_enabled_pc) { |
| 1241 | SET_PART(vreg, pf, config->pin_func_none); |
| 1242 | SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE); |
| 1243 | } |
| 1244 | } else { |
| 1245 | if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL) |
| 1246 | == RPM_VREG_PIN_CTRL_NONE |
| 1247 | && pdata->pin_fn != config->pin_func_sleep_b) { |
| 1248 | pr_err("%s: no pin control input specified\n", |
| 1249 | vreg->rdesc.name); |
| 1250 | mutex_unlock(&vreg->pc_lock); |
| 1251 | return -EINVAL; |
| 1252 | } |
| 1253 | rdesc->ops = &pin_control_ops; |
| 1254 | vreg->pdata.pin_ctrl = pdata->pin_ctrl; |
| 1255 | vreg->pdata.pin_fn = pdata->pin_fn; |
| 1256 | |
| 1257 | /* Initialize the RPM request. */ |
| 1258 | pin_fn = config->pin_func_none; |
| 1259 | /* Allow pf=sleep_b to be specified by platform data. */ |
| 1260 | if (vreg->pdata.pin_fn == config->pin_func_sleep_b) |
| 1261 | pin_fn = config->pin_func_sleep_b; |
| 1262 | SET_PART(vreg, pf, pin_fn); |
| 1263 | SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE); |
| 1264 | } |
| 1265 | |
| 1266 | mutex_unlock(&vreg->pc_lock); |
| 1267 | |
| 1268 | if (rc) |
| 1269 | goto bail; |
| 1270 | |
| 1271 | rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg); |
| 1272 | if (IS_ERR(rdev)) { |
| 1273 | rc = PTR_ERR(rdev); |
| 1274 | pr_err("regulator_register failed: %s, rc=%d\n", |
| 1275 | vreg->rdesc.name, rc); |
| 1276 | return rc; |
| 1277 | } else { |
| 1278 | if (config->is_real_id(pdata->id)) |
| 1279 | vreg->rdev = rdev; |
| 1280 | else |
| 1281 | vreg->rdev_pc = rdev; |
| 1282 | } |
| 1283 | |
| 1284 | bail: |
| 1285 | if (rc) |
| 1286 | pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc); |
| 1287 | |
| 1288 | return rc; |
| 1289 | } |
| 1290 | |
| 1291 | static void rpm_vreg_set_point_init(void) |
| 1292 | { |
| 1293 | struct vreg_set_points **set_points; |
| 1294 | int i, j, temp; |
| 1295 | |
| 1296 | set_points = config->set_points; |
| 1297 | |
| 1298 | /* Calculate the number of set points available for each regulator. */ |
| 1299 | for (i = 0; i < config->set_points_len; i++) { |
| 1300 | temp = 0; |
| 1301 | for (j = 0; j < set_points[i]->count; j++) { |
| 1302 | set_points[i]->range[j].n_voltages |
| 1303 | = (set_points[i]->range[j].max_uV |
| 1304 | - set_points[i]->range[j].min_uV) |
| 1305 | / set_points[i]->range[j].step_uV + 1; |
| 1306 | temp += set_points[i]->range[j].n_voltages; |
| 1307 | } |
| 1308 | set_points[i]->n_voltages = temp; |
| 1309 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static int __devinit rpm_vreg_probe(struct platform_device *pdev) |
| 1313 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1314 | struct rpm_regulator_platform_data *platform_data; |
| 1315 | int rc = 0; |
| 1316 | int i, id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1317 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1318 | platform_data = pdev->dev.platform_data; |
| 1319 | if (!platform_data) { |
| 1320 | pr_err("rpm-regulator requires platform data\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1321 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1324 | if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX |
| 1325 | && platform_data->version != rpm_version) { |
| 1326 | pr_err("rpm version %d does not match previous version %d\n", |
| 1327 | platform_data->version, rpm_version); |
| 1328 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1331 | if (platform_data->version < 0 |
| 1332 | || platform_data->version > RPM_VREG_VERSION_MAX) { |
| 1333 | pr_err("rpm version %d is invalid\n", platform_data->version); |
| 1334 | return -EINVAL; |
| 1335 | } |
| 1336 | |
| 1337 | if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) { |
| 1338 | rpm_version = platform_data->version; |
| 1339 | config = get_config[platform_data->version](); |
| 1340 | vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem; |
| 1341 | vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig; |
| 1342 | if (!config) { |
| 1343 | pr_err("rpm version %d is not available\n", |
| 1344 | platform_data->version); |
| 1345 | return -ENODEV; |
| 1346 | } |
| 1347 | if (config->use_legacy_optimum_mode) |
| 1348 | for (i = 0; i < ARRAY_SIZE(vreg_ops); i++) |
| 1349 | vreg_ops[i]->get_optimum_mode |
| 1350 | = vreg_legacy_get_optimum_mode; |
| 1351 | rpm_vreg_set_point_init(); |
| 1352 | /* First time probed; initialize pin control mutexes. */ |
| 1353 | for (i = 0; i < config->vregs_len; i++) |
| 1354 | mutex_init(&config->vregs[i].pc_lock); |
| 1355 | } |
| 1356 | |
| 1357 | /* Initialize all of the regulators listed in the platform data. */ |
| 1358 | for (i = 0; i < platform_data->num_regulators; i++) { |
| 1359 | rc = rpm_vreg_init_regulator(&platform_data->init_data[i], |
| 1360 | &pdev->dev); |
| 1361 | if (rc) { |
| 1362 | pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc); |
| 1363 | goto remove_regulators; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | platform_set_drvdata(pdev, platform_data); |
| 1368 | |
| 1369 | return rc; |
| 1370 | |
| 1371 | remove_regulators: |
| 1372 | /* Unregister all regulators added before the erroring one. */ |
| 1373 | for (; i >= 0; i--) { |
| 1374 | id = platform_data->init_data[i].id; |
| 1375 | if (config->is_real_id(id)) { |
| 1376 | regulator_unregister(config->vregs[id].rdev); |
| 1377 | config->vregs[id].rdev = NULL; |
| 1378 | } else { |
| 1379 | regulator_unregister(config->vregs[ |
| 1380 | config->pc_id_to_real_id(id)].rdev_pc); |
| 1381 | config->vregs[id].rdev_pc = NULL; |
| 1382 | } |
| 1383 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1384 | |
| 1385 | return rc; |
| 1386 | } |
| 1387 | |
| 1388 | static int __devexit rpm_vreg_remove(struct platform_device *pdev) |
| 1389 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1390 | struct rpm_regulator_platform_data *platform_data; |
| 1391 | int i, id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1392 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1393 | platform_data = platform_get_drvdata(pdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1394 | platform_set_drvdata(pdev, NULL); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1395 | |
| 1396 | if (platform_data) { |
| 1397 | for (i = 0; i < platform_data->num_regulators; i++) { |
| 1398 | id = platform_data->init_data[i].id; |
| 1399 | if (config->is_real_id(id)) { |
| 1400 | regulator_unregister(config->vregs[id].rdev); |
| 1401 | config->vregs[id].rdev = NULL; |
| 1402 | } else { |
| 1403 | regulator_unregister(config->vregs[ |
| 1404 | config->pc_id_to_real_id(id)].rdev_pc); |
| 1405 | config->vregs[id].rdev_pc = NULL; |
| 1406 | } |
| 1407 | } |
| 1408 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | static struct platform_driver rpm_vreg_driver = { |
| 1414 | .probe = rpm_vreg_probe, |
| 1415 | .remove = __devexit_p(rpm_vreg_remove), |
| 1416 | .driver = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1417 | .name = RPM_REGULATOR_DEV_NAME, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1418 | .owner = THIS_MODULE, |
| 1419 | }, |
| 1420 | }; |
| 1421 | |
| 1422 | static int __init rpm_vreg_init(void) |
| 1423 | { |
| 1424 | return platform_driver_register(&rpm_vreg_driver); |
| 1425 | } |
| 1426 | |
| 1427 | static void __exit rpm_vreg_exit(void) |
| 1428 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1429 | int i; |
| 1430 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1431 | platform_driver_unregister(&rpm_vreg_driver); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1432 | |
| 1433 | for (i = 0; i < config->vregs_len; i++) |
| 1434 | mutex_destroy(&config->vregs[i].pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | postcore_initcall(rpm_vreg_init); |
| 1438 | module_exit(rpm_vreg_exit); |
| 1439 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1440 | MODULE_LICENSE("GPL v2"); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1441 | MODULE_DESCRIPTION("MSM RPM regulator driver"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1442 | MODULE_VERSION("1.0"); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1443 | MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME); |