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