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