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