David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1 | /* |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010-2012, The Linux Foundation. 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> |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 17 | #include <linux/clk.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/init.h> |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 21 | #include <linux/slab.h> |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 22 | #include <linux/string.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 24 | #include <linux/wakelock.h> |
| 25 | #include <linux/workqueue.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | #include <linux/regulator/driver.h> |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 27 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | #include <mach/rpm.h> |
| 29 | #include <mach/rpm-regulator.h> |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 30 | #include <mach/rpm-regulator-smd.h> |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 31 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | |
| 33 | #include "rpm_resources.h" |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 34 | #include "rpm-regulator-private.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | |
| 36 | /* Debug Definitions */ |
| 37 | |
| 38 | enum { |
| 39 | MSM_RPM_VREG_DEBUG_REQUEST = BIT(0), |
| 40 | MSM_RPM_VREG_DEBUG_VOTE = BIT(1), |
| 41 | MSM_RPM_VREG_DEBUG_DUPLICATE = BIT(2), |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 42 | MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG = BIT(3), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static int msm_rpm_vreg_debug_mask; |
| 46 | module_param_named( |
| 47 | debug_mask, msm_rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR |
| 48 | ); |
| 49 | |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 50 | /* Used for access via the rpm_regulator_* API. */ |
| 51 | struct rpm_regulator { |
| 52 | int vreg_id; |
| 53 | enum rpm_vreg_voter voter; |
| 54 | int sleep_also; |
| 55 | int min_uV; |
| 56 | int max_uV; |
| 57 | }; |
| 58 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 59 | struct vreg_config *(*get_config[])(void) = { |
| 60 | [RPM_VREG_VERSION_8660] = get_config_8660, |
| 61 | [RPM_VREG_VERSION_8960] = get_config_8960, |
David Collins | 6ef12bf | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 62 | [RPM_VREG_VERSION_9615] = get_config_9615, |
David Collins | 5779cea | 2012-01-05 15:09:21 -0800 | [diff] [blame] | 63 | [RPM_VREG_VERSION_8930] = get_config_8930, |
David Collins | d2a4fef | 2012-08-13 12:31:04 -0700 | [diff] [blame] | 64 | [RPM_VREG_VERSION_8930_PM8917] = get_config_8930_pm8917, |
David Collins | 2ac36d5 | 2012-10-02 14:56:21 -0700 | [diff] [blame] | 65 | [RPM_VREG_VERSION_8960_PM8917] = get_config_8960_pm8917, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 66 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 67 | |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 68 | static struct rpm_regulator_consumer_mapping *consumer_map; |
| 69 | static int consumer_map_len; |
| 70 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 71 | #define SET_PART(_vreg, _part, _val) \ |
| 72 | _vreg->req[_vreg->part->_part.word].value \ |
| 73 | = (_vreg->req[_vreg->part->_part.word].value \ |
David Collins | d8525e8 | 2011-11-21 14:54:25 -0800 | [diff] [blame] | 74 | & ~_vreg->part->_part.mask) \ |
| 75 | | (((_val) << _vreg->part->_part.shift) \ |
| 76 | & _vreg->part->_part.mask) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 77 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 78 | #define GET_PART(_vreg, _part) \ |
David Collins | d8525e8 | 2011-11-21 14:54:25 -0800 | [diff] [blame] | 79 | ((_vreg->req[_vreg->part->_part.word].value & _vreg->part->_part.mask) \ |
| 80 | >> _vreg->part->_part.shift) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 82 | #define GET_PART_PREV_ACT(_vreg, _part) \ |
| 83 | ((_vreg->prev_active_req[_vreg->part->_part.word].value \ |
| 84 | & _vreg->part->_part.mask) \ |
| 85 | >> _vreg->part->_part.shift) |
| 86 | |
David Collins | d8525e8 | 2011-11-21 14:54:25 -0800 | [diff] [blame] | 87 | #define USES_PART(_vreg, _part) (_vreg->part->_part.mask) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 89 | #define vreg_err(vreg, fmt, ...) \ |
| 90 | pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 91 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 92 | #define RPM_VREG_PIN_CTRL_EN0 0x01 |
| 93 | #define RPM_VREG_PIN_CTRL_EN1 0x02 |
| 94 | #define RPM_VREG_PIN_CTRL_EN2 0x04 |
| 95 | #define RPM_VREG_PIN_CTRL_EN3 0x08 |
| 96 | #define RPM_VREG_PIN_CTRL_ALL 0x0F |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 97 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 98 | static const char *label_freq[] = { |
| 99 | [RPM_VREG_FREQ_NONE] = " N/A", |
| 100 | [RPM_VREG_FREQ_19p20] = "19.2", |
| 101 | [RPM_VREG_FREQ_9p60] = "9.60", |
| 102 | [RPM_VREG_FREQ_6p40] = "6.40", |
| 103 | [RPM_VREG_FREQ_4p80] = "4.80", |
| 104 | [RPM_VREG_FREQ_3p84] = "3.84", |
| 105 | [RPM_VREG_FREQ_3p20] = "3.20", |
| 106 | [RPM_VREG_FREQ_2p74] = "2.74", |
| 107 | [RPM_VREG_FREQ_2p40] = "2.40", |
| 108 | [RPM_VREG_FREQ_2p13] = "2.13", |
| 109 | [RPM_VREG_FREQ_1p92] = "1.92", |
| 110 | [RPM_VREG_FREQ_1p75] = "1.75", |
| 111 | [RPM_VREG_FREQ_1p60] = "1.60", |
| 112 | [RPM_VREG_FREQ_1p48] = "1.48", |
| 113 | [RPM_VREG_FREQ_1p37] = "1.37", |
| 114 | [RPM_VREG_FREQ_1p28] = "1.28", |
| 115 | [RPM_VREG_FREQ_1p20] = "1.20", |
| 116 | }; |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 117 | |
| 118 | static const char *label_corner[] = { |
| 119 | [RPM_VREG_CORNER_NONE] = "NONE", |
| 120 | [RPM_VREG_CORNER_LOW] = "LOW", |
| 121 | [RPM_VREG_CORNER_NOMINAL] = "NOM", |
| 122 | [RPM_VREG_CORNER_HIGH] = "HIGH", |
| 123 | }; |
| 124 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 125 | /* |
| 126 | * This is used when voting for LPM or HPM by subtracting or adding to the |
| 127 | * hpm_min_load of a regulator. It has units of uA. |
| 128 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 129 | #define LOAD_THRESHOLD_STEP 1000 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 130 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 131 | /* rpm_version keeps track of the version for the currently running driver. */ |
| 132 | enum rpm_vreg_version rpm_version = -1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 134 | /* config holds all configuration data of the currently running driver. */ |
| 135 | static struct vreg_config *config; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 136 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 137 | /* These regulator ID values are specified in the board file. */ |
| 138 | static int vreg_id_vdd_mem, vreg_id_vdd_dig; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 140 | static inline int vreg_id_is_vdd_mem_or_dig(int id) |
| 141 | { |
| 142 | return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig; |
| 143 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 144 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 145 | #define DEBUG_PRINT_BUFFER_SIZE 512 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 147 | static void rpm_regulator_req(struct vreg *vreg, int set) |
| 148 | { |
| 149 | int uV, mV, fm, pm, pc, pf, pd, freq, state, i; |
| 150 | const char *pf_label = "", *fm_label = "", *pc_total = ""; |
| 151 | const char *pc_en[4] = {"", "", "", ""}; |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 152 | const char *pm_label = "", *freq_label = "", *corner_label = ""; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 153 | char buf[DEBUG_PRINT_BUFFER_SIZE]; |
| 154 | size_t buflen = DEBUG_PRINT_BUFFER_SIZE; |
| 155 | int pos = 0; |
| 156 | |
| 157 | /* Suppress VDD_MEM and VDD_DIG printing. */ |
| 158 | if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG) |
| 159 | && vreg_id_is_vdd_mem_or_dig(vreg->id)) |
| 160 | return; |
| 161 | |
| 162 | uV = GET_PART(vreg, uV); |
| 163 | mV = GET_PART(vreg, mV); |
| 164 | if (vreg->type == RPM_REGULATOR_TYPE_NCP) { |
| 165 | uV = -uV; |
| 166 | mV = -mV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | } |
| 168 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 169 | fm = GET_PART(vreg, fm); |
| 170 | pm = GET_PART(vreg, pm); |
| 171 | pc = GET_PART(vreg, pc); |
| 172 | pf = GET_PART(vreg, pf); |
| 173 | pd = GET_PART(vreg, pd); |
| 174 | freq = GET_PART(vreg, freq); |
| 175 | state = GET_PART(vreg, enable_state); |
| 176 | |
| 177 | if (pf >= 0 && pf < config->label_pin_func_len) |
| 178 | pf_label = config->label_pin_func[pf]; |
| 179 | |
| 180 | if (fm >= 0 && fm < config->label_force_mode_len) |
| 181 | fm_label = config->label_force_mode[fm]; |
| 182 | |
| 183 | if (pm >= 0 && pm < config->label_power_mode_len) |
| 184 | pm_label = config->label_power_mode[pm]; |
| 185 | |
| 186 | if (freq >= 0 && freq < ARRAY_SIZE(label_freq)) |
| 187 | freq_label = label_freq[freq]; |
| 188 | |
| 189 | for (i = 0; i < config->label_pin_ctrl_len; i++) |
| 190 | if (pc & (1 << i)) |
| 191 | pc_en[i] = config->label_pin_ctrl[i]; |
| 192 | |
| 193 | if (pc == RPM_VREG_PIN_CTRL_NONE) |
| 194 | pc_total = " none"; |
| 195 | |
| 196 | pos += scnprintf(buf + pos, buflen - pos, "%s%s: ", |
| 197 | KERN_INFO, __func__); |
| 198 | |
| 199 | pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c", |
| 200 | (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"), |
| 201 | vreg->rdesc.name, |
| 202 | (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S')); |
| 203 | |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 204 | if (USES_PART(vreg, uV) && vreg->type != RPM_REGULATOR_TYPE_CORNER) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 205 | pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV); |
| 206 | if (USES_PART(vreg, mV)) |
| 207 | pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV); |
| 208 | if (USES_PART(vreg, enable_state)) |
| 209 | pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)", |
| 210 | (state == 1 ? "on" : "off"), state); |
| 211 | if (USES_PART(vreg, ip)) |
| 212 | pos += scnprintf(buf + pos, buflen - pos, |
| 213 | ", ip=%4d mA", GET_PART(vreg, ip)); |
| 214 | if (USES_PART(vreg, fm)) |
| 215 | pos += scnprintf(buf + pos, buflen - pos, |
| 216 | ", fm=%s (%d)", fm_label, fm); |
| 217 | if (USES_PART(vreg, pc)) |
| 218 | pos += scnprintf(buf + pos, buflen - pos, |
| 219 | ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1], |
| 220 | pc_en[2], pc_en[3], pc_total, pc); |
| 221 | if (USES_PART(vreg, pf)) |
| 222 | pos += scnprintf(buf + pos, buflen - pos, |
| 223 | ", pf=%s (%d)", pf_label, pf); |
| 224 | if (USES_PART(vreg, pd)) |
| 225 | pos += scnprintf(buf + pos, buflen - pos, |
| 226 | ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd); |
| 227 | if (USES_PART(vreg, ia)) |
| 228 | pos += scnprintf(buf + pos, buflen - pos, |
| 229 | ", ia=%4d mA", GET_PART(vreg, ia)); |
| 230 | if (USES_PART(vreg, freq)) { |
| 231 | if (vreg->type == RPM_REGULATOR_TYPE_NCP) |
| 232 | pos += scnprintf(buf + pos, buflen - pos, |
| 233 | ", freq=%2d", freq); |
| 234 | else |
| 235 | pos += scnprintf(buf + pos, buflen - pos, |
| 236 | ", freq=%s MHz (%2d)", freq_label, freq); |
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 | if (USES_PART(vreg, pm)) |
| 239 | pos += scnprintf(buf + pos, buflen - pos, |
| 240 | ", pm=%s (%d)", pm_label, pm); |
| 241 | if (USES_PART(vreg, freq_clk_src)) |
| 242 | pos += scnprintf(buf + pos, buflen - pos, |
| 243 | ", clk_src=%d", GET_PART(vreg, freq_clk_src)); |
| 244 | if (USES_PART(vreg, comp_mode)) |
| 245 | pos += scnprintf(buf + pos, buflen - pos, |
| 246 | ", comp=%d", GET_PART(vreg, comp_mode)); |
| 247 | if (USES_PART(vreg, hpm)) |
| 248 | pos += scnprintf(buf + pos, buflen - pos, |
| 249 | ", hpm=%d", GET_PART(vreg, hpm)); |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 250 | if (USES_PART(vreg, uV) && vreg->type == RPM_REGULATOR_TYPE_CORNER) { |
| 251 | if (uV >= 0 && uV < (ARRAY_SIZE(label_corner) - 1)) |
| 252 | corner_label = label_corner[uV+1]; |
| 253 | pos += scnprintf(buf + pos, buflen - pos, ", corner=%s (%d)", |
| 254 | corner_label, 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 | pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}", |
| 258 | vreg->req[0].id, vreg->req[0].value); |
| 259 | if (vreg->part->request_len > 1) |
| 260 | pos += scnprintf(buf + pos, buflen - pos, |
| 261 | ", req[1]={%d, 0x%08X}", vreg->req[1].id, |
| 262 | vreg->req[1].value); |
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 | pos += scnprintf(buf + pos, buflen - pos, "\n"); |
| 265 | printk(buf); |
| 266 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 267 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 268 | static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter, |
| 269 | int set, int voter_uV, int aggregate_uV) |
| 270 | { |
| 271 | /* Suppress VDD_MEM and VDD_DIG printing. */ |
| 272 | if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG) |
| 273 | && vreg_id_is_vdd_mem_or_dig(vreg->id)) |
| 274 | return; |
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 | pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, " |
| 277 | "v_aggregate=%7d uV\n", vreg->rdesc.name, voter, |
| 278 | (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV); |
| 279 | } |
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 | static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt) |
| 282 | { |
| 283 | /* Suppress VDD_MEM and VDD_DIG printing. */ |
| 284 | if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG) |
| 285 | && vreg_id_is_vdd_mem_or_dig(vreg->id)) |
| 286 | return; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 287 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 288 | if (cnt == 2) |
| 289 | pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, " |
| 290 | "req[1]={%d, 0x%08X}\n", vreg->rdesc.name, |
| 291 | (set == 0 ? 'A' : 'S'), |
| 292 | vreg->req[0].id, vreg->req[0].value, |
| 293 | vreg->req[1].id, vreg->req[1].value); |
| 294 | else if (cnt == 1) |
| 295 | pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n", |
| 296 | vreg->rdesc.name, (set == 0 ? 'A' : 'S'), |
| 297 | vreg->req[0].id, vreg->req[0].value); |
| 298 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 300 | static bool requires_tcxo_workaround; |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 301 | static struct clk *tcxo_handle; |
| 302 | static struct wake_lock tcxo_wake_lock; |
| 303 | static DEFINE_MUTEX(tcxo_mutex); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 304 | static bool tcxo_is_enabled; |
| 305 | /* |
| 306 | * TCXO must be kept on for at least the duration of its warmup (4 ms); |
| 307 | * otherwise, it will stay on when hardware disabling is attempted. |
| 308 | */ |
| 309 | #define TCXO_WARMUP_TIME_MS 4 |
| 310 | |
| 311 | static void tcxo_get_handle(void) |
| 312 | { |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 313 | if (!tcxo_handle) { |
| 314 | tcxo_handle = clk_get_sys("rpm-regulator", "vref_buff"); |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 315 | if (IS_ERR(tcxo_handle)) |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 316 | tcxo_handle = NULL; |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Perform best effort enable of CXO. Since the MSM clock drivers depend upon |
| 322 | * the rpm-regulator driver, any rpm-regulator devices that are configured with |
| 323 | * always_on == 1 will not be able to enable CXO during probe. This does not |
| 324 | * cause a problem though since CXO will be enabled by the boot loaders before |
| 325 | * Apps boots up. |
| 326 | */ |
| 327 | static bool tcxo_enable(void) |
| 328 | { |
| 329 | int rc; |
| 330 | |
| 331 | if (tcxo_handle && !tcxo_is_enabled) { |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 332 | rc = clk_prepare_enable(tcxo_handle); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 333 | if (!rc) { |
| 334 | tcxo_is_enabled = true; |
| 335 | wake_lock(&tcxo_wake_lock); |
| 336 | return true; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | static void tcxo_delayed_disable_work(struct work_struct *work) |
| 344 | { |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 345 | mutex_lock(&tcxo_mutex); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 346 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 347 | clk_disable_unprepare(tcxo_handle); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 348 | tcxo_is_enabled = false; |
| 349 | wake_unlock(&tcxo_wake_lock); |
| 350 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 351 | mutex_unlock(&tcxo_mutex); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static DECLARE_DELAYED_WORK(tcxo_disable_work, tcxo_delayed_disable_work); |
| 355 | |
| 356 | static void tcxo_delayed_disable(void) |
| 357 | { |
| 358 | /* |
| 359 | * The delay in jiffies has 1 added to it to ensure that at least |
| 360 | * one jiffy takes place before the work is enqueued. Without this, |
| 361 | * the work would be scheduled to run in the very next jiffy which could |
| 362 | * result in too little delay and TCXO being stuck on. |
| 363 | */ |
| 364 | if (tcxo_handle) |
| 365 | schedule_delayed_work(&tcxo_disable_work, |
| 366 | msecs_to_jiffies(TCXO_WARMUP_TIME_MS) + 1); |
| 367 | } |
| 368 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 369 | /* Mutex lock needed for sleep-selectable regulators. */ |
| 370 | static DEFINE_MUTEX(rpm_sleep_sel_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 371 | |
| 372 | static int voltage_from_req(struct vreg *vreg) |
| 373 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 374 | int uV = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 375 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 376 | if (vreg->part->uV.mask) |
| 377 | uV = GET_PART(vreg, uV); |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 378 | else if (vreg->part->mV.mask) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 379 | uV = MILLI_TO_MICRO(GET_PART(vreg, mV)); |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 380 | else if (vreg->part->enable_state.mask) |
| 381 | uV = GET_PART(vreg, enable_state); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 383 | return uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 384 | } |
| 385 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 386 | static void voltage_to_req(int uV, struct vreg *vreg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 387 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 388 | if (vreg->part->uV.mask) |
| 389 | SET_PART(vreg, uV, uV); |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 390 | else if (vreg->part->mV.mask) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 391 | SET_PART(vreg, mV, MICRO_TO_MILLI(uV)); |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 392 | else if (vreg->part->enable_state.mask) |
| 393 | SET_PART(vreg, enable_state, uV); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter, |
| 397 | int set, unsigned mask0, unsigned val0, |
| 398 | unsigned mask1, unsigned val1, unsigned cnt, |
| 399 | int update_voltage) |
| 400 | { |
| 401 | struct msm_rpm_iv_pair *prev_req; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 402 | int rc = 0, max_uV_vote = 0; |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 403 | bool tcxo_enabled = false; |
| 404 | bool voltage_increased = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 405 | unsigned prev0, prev1; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 406 | int *min_uV_vote; |
| 407 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | |
| 409 | if (set == MSM_RPM_CTX_SET_0) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 410 | min_uV_vote = vreg->active_min_uV_vote; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 411 | prev_req = vreg->prev_active_req; |
| 412 | } else { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 413 | min_uV_vote = vreg->sleep_min_uV_vote; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 414 | prev_req = vreg->prev_sleep_req; |
| 415 | } |
| 416 | |
| 417 | prev0 = vreg->req[0].value; |
| 418 | vreg->req[0].value &= ~mask0; |
| 419 | vreg->req[0].value |= val0 & mask0; |
| 420 | |
| 421 | prev1 = vreg->req[1].value; |
| 422 | vreg->req[1].value &= ~mask1; |
| 423 | vreg->req[1].value |= val1 & mask1; |
| 424 | |
David Collins | d1b7aea | 2012-03-19 10:27:55 -0700 | [diff] [blame] | 425 | /* Set the force mode field based on which set is being requested. */ |
| 426 | if (set == MSM_RPM_CTX_SET_0) |
| 427 | SET_PART(vreg, fm, vreg->pdata.force_mode); |
| 428 | else |
| 429 | SET_PART(vreg, fm, vreg->pdata.sleep_set_force_mode); |
| 430 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 431 | if (update_voltage) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 432 | min_uV_vote[voter] = voltage_from_req(vreg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 433 | |
| 434 | /* Find the highest voltage voted for and use it. */ |
| 435 | for (i = 0; i < RPM_VREG_VOTER_COUNT; i++) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 436 | max_uV_vote = max(max_uV_vote, min_uV_vote[i]); |
| 437 | voltage_to_req(max_uV_vote, vreg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 438 | |
| 439 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 440 | rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter], |
| 441 | max_uV_vote); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 442 | |
| 443 | /* Ignore duplicate requests */ |
| 444 | if (vreg->req[0].value != prev_req[0].value || |
| 445 | vreg->req[1].value != prev_req[1].value) { |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 446 | |
| 447 | /* Enable CXO clock if necessary for TCXO workaround. */ |
| 448 | if (requires_tcxo_workaround && vreg->requires_cxo |
| 449 | && (set == MSM_RPM_CTX_SET_0) |
| 450 | && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) { |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 451 | mutex_lock(&tcxo_mutex); |
| 452 | if (!tcxo_handle) |
| 453 | tcxo_get_handle(); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 454 | voltage_increased = true; |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 455 | tcxo_enabled = tcxo_enable(); |
| 456 | } |
| 457 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 458 | rc = msm_rpmrs_set(set, vreg->req, cnt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 459 | if (rc) { |
| 460 | vreg->req[0].value = prev0; |
| 461 | vreg->req[1].value = prev1; |
| 462 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 463 | vreg_err(vreg, "msm_rpmrs_set failed - " |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 464 | "set=%s, id=%d, rc=%d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 465 | (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"), |
| 466 | vreg->req[0].id, rc); |
| 467 | } else { |
| 468 | /* Only save if nonzero and active set. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 469 | if (max_uV_vote && (set == MSM_RPM_CTX_SET_0)) |
| 470 | vreg->save_uV = max_uV_vote; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 471 | if (msm_rpm_vreg_debug_mask |
| 472 | & MSM_RPM_VREG_DEBUG_REQUEST) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 473 | rpm_regulator_req(vreg, set); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | prev_req[0].value = vreg->req[0].value; |
| 475 | prev_req[1].value = vreg->req[1].value; |
| 476 | } |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * Schedule CXO clock to be disabled after TCXO warmup time if |
| 480 | * TCXO workaround is applicable for this regulator. |
| 481 | */ |
| 482 | if (voltage_increased) { |
| 483 | if (tcxo_enabled) |
| 484 | tcxo_delayed_disable(); |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 485 | mutex_unlock(&tcxo_mutex); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 486 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 487 | } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 488 | rpm_regulator_duplicate(vreg, set, cnt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return rc; |
| 492 | } |
| 493 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 494 | static int vreg_set_sleep_sel(struct vreg *vreg, enum rpm_vreg_voter voter, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 495 | int sleep, unsigned mask0, unsigned val0, |
| 496 | unsigned mask1, unsigned val1, unsigned cnt, |
| 497 | int update_voltage) |
| 498 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 499 | unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1}; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 500 | int rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 501 | |
| 502 | if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT) |
| 503 | return -EINVAL; |
| 504 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 505 | mutex_lock(&rpm_sleep_sel_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * Send sleep set request first so that subsequent set_mode, etc calls |
| 509 | * use the voltage from the active set. |
| 510 | */ |
| 511 | if (sleep) |
| 512 | rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP, |
| 513 | mask0, val0, mask1, val1, cnt, update_voltage); |
| 514 | else { |
| 515 | /* |
| 516 | * Vote for 0 V in the sleep set when active set-only is |
| 517 | * specified. This ensures that a disable vote will be issued |
| 518 | * at some point for the sleep set of the regulator. |
| 519 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 520 | if (vreg->part->uV.mask) { |
| 521 | s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift; |
| 522 | s_mask[vreg->part->uV.word] = vreg->part->uV.mask; |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 523 | } else if (vreg->part->mV.mask) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 524 | s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift; |
| 525 | s_mask[vreg->part->mV.word] = vreg->part->mV.mask; |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 526 | } else if (vreg->part->enable_state.mask) { |
| 527 | s_val[vreg->part->enable_state.word] |
| 528 | = 0 << vreg->part->enable_state.shift; |
| 529 | s_mask[vreg->part->enable_state.word] |
| 530 | = vreg->part->enable_state.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 534 | s_mask[0], s_val[0], s_mask[1], s_val[1], |
| 535 | cnt, update_voltage); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0, |
| 539 | mask1, val1, cnt, update_voltage); |
| 540 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 541 | mutex_unlock(&rpm_sleep_sel_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 542 | |
| 543 | return rc; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor |
| 548 | * @vreg: ID for regulator |
| 549 | * @voter: ID for the voter |
| 550 | * @min_uV: minimum acceptable voltage (in uV) that is voted for |
| 551 | * @max_uV: maximum acceptable voltage (in uV) that is voted for |
| 552 | * @sleep_also: 0 for active set only, non-0 for active set and sleep set |
| 553 | * |
| 554 | * Returns 0 on success or errno. |
| 555 | * |
| 556 | * This function is used to vote for the voltage of a regulator without |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 557 | * using the regulator framework. It is needed for consumers which wish to only |
| 558 | * vote for active set regulator voltage. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | * |
| 560 | * If sleep_also == 0, then a sleep-set value of 0V will be voted for. |
| 561 | * |
| 562 | * This function may only be called for regulators which have the sleep flag |
| 563 | * specified in their private data. |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 564 | * |
| 565 | * Consumers can vote to disable a regulator with this function by passing |
| 566 | * min_uV = 0 and max_uV = 0. |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 567 | * |
| 568 | * Voltage switch type regulators may be controlled via rpm_vreg_set_voltage |
| 569 | * as well. For this type of regulator, max_uV > 0 is treated as an enable |
| 570 | * request and max_uV == 0 is treated as a disable request. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 572 | int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV, |
| 573 | int max_uV, int sleep_also) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 575 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 576 | struct vreg_range *range; |
| 577 | struct vreg *vreg; |
| 578 | int uV = min_uV; |
| 579 | int lim_min_uV, lim_max_uV, i, rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 581 | if (!config) { |
| 582 | pr_err("rpm-regulator driver has not probed yet.\n"); |
| 583 | return -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 584 | } |
| 585 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 586 | if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) { |
| 587 | pr_err("invalid regulator id=%d\n", vreg_id); |
| 588 | return -EINVAL; |
| 589 | } |
| 590 | |
| 591 | vreg = &config->vregs[vreg_id]; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 592 | |
| 593 | if (!vreg->pdata.sleep_selectable) { |
| 594 | vreg_err(vreg, "regulator is not marked sleep selectable\n"); |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 598 | /* Allow min_uV == max_uV == 0 to represent a disable request. */ |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 599 | if ((min_uV != 0 || max_uV != 0) |
| 600 | && (vreg->part->uV.mask || vreg->part->mV.mask)) { |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 601 | /* |
| 602 | * Check if request voltage is outside of allowed range. The |
| 603 | * regulator core has already checked that constraint range |
| 604 | * is inside of the physically allowed range. |
| 605 | */ |
| 606 | lim_min_uV = vreg->pdata.init_data.constraints.min_uV; |
| 607 | lim_max_uV = vreg->pdata.init_data.constraints.max_uV; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 608 | |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 609 | if (uV < lim_min_uV && max_uV >= lim_min_uV) |
| 610 | uV = lim_min_uV; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 611 | |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 612 | if (uV < lim_min_uV || uV > lim_max_uV) { |
| 613 | vreg_err(vreg, "request v=[%d, %d] is outside allowed " |
| 614 | "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV, |
| 615 | lim_max_uV); |
| 616 | return -EINVAL; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 617 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 618 | |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 619 | range = &vreg->set_points->range[0]; |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 620 | /* Find the range which uV is inside of. */ |
| 621 | for (i = vreg->set_points->count - 1; i > 0; i--) { |
| 622 | if (uV > vreg->set_points->range[i - 1].max_uV) { |
| 623 | range = &vreg->set_points->range[i]; |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Force uV to be an allowed set point and apply a ceiling |
| 630 | * function to non-set point values. |
| 631 | */ |
| 632 | uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV; |
| 633 | uV = uV * range->step_uV + range->min_uV; |
David Collins | 3974b61 | 2011-11-21 15:07:36 -0800 | [diff] [blame] | 634 | |
| 635 | if (uV > max_uV) { |
| 636 | vreg_err(vreg, |
| 637 | "request v=[%d, %d] cannot be met by any set point; " |
| 638 | "next set point: %d\n", |
| 639 | min_uV, max_uV, uV); |
| 640 | return -EINVAL; |
| 641 | } |
David Collins | 7462b9d | 2011-10-11 16:02:17 -0700 | [diff] [blame] | 642 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 643 | |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 644 | if (vreg->type == RPM_REGULATOR_TYPE_CORNER) { |
| 645 | /* |
| 646 | * Translate from enum values which work as inputs in the |
| 647 | * rpm_vreg_set_voltage function to the actual corner values |
| 648 | * sent to the RPM. |
| 649 | */ |
| 650 | if (uV > 0) |
| 651 | uV -= RPM_VREG_CORNER_NONE; |
| 652 | } |
| 653 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 654 | if (vreg->part->uV.mask) { |
| 655 | val[vreg->part->uV.word] = uV << vreg->part->uV.shift; |
| 656 | mask[vreg->part->uV.word] = vreg->part->uV.mask; |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 657 | } else if (vreg->part->mV.mask) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 658 | val[vreg->part->mV.word] |
| 659 | = MICRO_TO_MILLI(uV) << vreg->part->mV.shift; |
| 660 | mask[vreg->part->mV.word] = vreg->part->mV.mask; |
David Collins | 13397f2 | 2012-02-06 13:53:29 -0800 | [diff] [blame] | 661 | } else if (vreg->part->enable_state.mask) { |
| 662 | /* |
| 663 | * Translate max_uV > 0 into an enable request for regulator |
| 664 | * types which to not support voltage setting, e.g. voltage |
| 665 | * switches. |
| 666 | */ |
| 667 | val[vreg->part->enable_state.word] |
| 668 | = (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift; |
| 669 | mask[vreg->part->enable_state.word] |
| 670 | = vreg->part->enable_state.mask; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 671 | } |
| 672 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 673 | rc = vreg_set_sleep_sel(vreg, voter, sleep_also, mask[0], val[0], |
| 674 | mask[1], val[1], vreg->part->request_len, 1); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 675 | if (rc) |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 676 | vreg_err(vreg, "vreg_set_sleep_sel failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 677 | |
| 678 | return rc; |
| 679 | } |
| 680 | EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage); |
| 681 | |
| 682 | /** |
| 683 | * rpm_vreg_set_frequency - sets the frequency of a switching regulator |
| 684 | * @vreg: ID for regulator |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 685 | * @freq: enum corresponding to desired frequency |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 686 | * |
| 687 | * Returns 0 on success or errno. |
| 688 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 689 | 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] | 690 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 691 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 692 | struct vreg *vreg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 693 | int rc; |
| 694 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 695 | if (!config) { |
| 696 | pr_err("rpm-regulator driver has not probed yet.\n"); |
| 697 | return -ENODEV; |
| 698 | } |
| 699 | |
| 700 | if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) { |
| 701 | pr_err("invalid regulator id=%d\n", vreg_id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 702 | return -EINVAL; |
| 703 | } |
| 704 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 705 | vreg = &config->vregs[vreg_id]; |
| 706 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 707 | if (freq < 0 || freq > RPM_VREG_FREQ_1p20) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 708 | vreg_err(vreg, "invalid frequency=%d\n", freq); |
| 709 | return -EINVAL; |
| 710 | } |
| 711 | if (!vreg->pdata.sleep_selectable) { |
| 712 | vreg_err(vreg, "regulator is not marked sleep selectable\n"); |
| 713 | return -EINVAL; |
| 714 | } |
| 715 | if (!vreg->part->freq.mask) { |
| 716 | vreg_err(vreg, "frequency not supported\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 717 | return -EINVAL; |
| 718 | } |
| 719 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 720 | val[vreg->part->freq.word] = freq << vreg->part->freq.shift; |
| 721 | mask[vreg->part->freq.word] = vreg->part->freq.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 722 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 723 | rc = vreg_set_sleep_sel(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0], |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 724 | val[0], mask[1], val[1], vreg->part->request_len, 0); |
| 725 | if (rc) |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 726 | vreg_err(vreg, "vreg_set_sleep_sel failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 727 | |
| 728 | return rc; |
| 729 | } |
| 730 | EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency); |
| 731 | |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 732 | #define MAX_NAME_LEN 64 |
| 733 | /** |
| 734 | * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator |
| 735 | * @dev: device for regulator consumer |
| 736 | * @supply: supply name |
| 737 | * |
| 738 | * Returns a struct rpm_regulator corresponding to the regulator producer, |
| 739 | * or ERR_PTR() containing errno. |
| 740 | * |
| 741 | * This function may only be called from nonatomic context. The mapping between |
| 742 | * <dev, supply> tuples and rpm_regulators struct pointers is specified via |
| 743 | * rpm-regulator platform data. |
| 744 | */ |
| 745 | struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply) |
| 746 | { |
| 747 | struct rpm_regulator_consumer_mapping *mapping = NULL; |
| 748 | const char *devname = NULL; |
| 749 | struct rpm_regulator *regulator; |
| 750 | int i; |
| 751 | |
| 752 | if (!config) { |
| 753 | pr_err("rpm-regulator driver has not probed yet.\n"); |
| 754 | return ERR_PTR(-ENODEV); |
| 755 | } |
| 756 | |
| 757 | if (consumer_map == NULL || consumer_map_len == 0) { |
| 758 | pr_err("No private consumer mapping has been specified.\n"); |
| 759 | return ERR_PTR(-ENODEV); |
| 760 | } |
| 761 | |
| 762 | if (supply == NULL) { |
| 763 | pr_err("supply name must be specified\n"); |
| 764 | return ERR_PTR(-EINVAL); |
| 765 | } |
| 766 | |
| 767 | if (dev) |
| 768 | devname = dev_name(dev); |
| 769 | |
| 770 | for (i = 0; i < consumer_map_len; i++) { |
| 771 | /* If the mapping has a device set up it must match */ |
| 772 | if (consumer_map[i].dev_name && |
| 773 | (!devname || strncmp(consumer_map[i].dev_name, devname, |
| 774 | MAX_NAME_LEN))) |
| 775 | continue; |
| 776 | |
| 777 | if (strncmp(consumer_map[i].supply, supply, MAX_NAME_LEN) |
| 778 | == 0) { |
| 779 | mapping = &consumer_map[i]; |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | if (mapping == NULL) { |
| 785 | pr_err("could not find mapping for dev=%s, supply=%s\n", |
| 786 | (devname ? devname : "(null)"), supply); |
| 787 | return ERR_PTR(-ENODEV); |
| 788 | } |
| 789 | |
| 790 | regulator = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL); |
| 791 | if (regulator == NULL) { |
| 792 | pr_err("could not allocate memory for regulator\n"); |
| 793 | return ERR_PTR(-ENOMEM); |
| 794 | } |
| 795 | |
| 796 | regulator->vreg_id = mapping->vreg_id; |
| 797 | regulator->voter = mapping->voter; |
| 798 | regulator->sleep_also = mapping->sleep_also; |
| 799 | |
| 800 | return regulator; |
| 801 | } |
| 802 | EXPORT_SYMBOL_GPL(rpm_regulator_get); |
| 803 | |
| 804 | static int rpm_regulator_check_input(struct rpm_regulator *regulator) |
| 805 | { |
| 806 | int rc = 0; |
| 807 | |
| 808 | if (regulator == NULL) { |
| 809 | rc = -EINVAL; |
| 810 | pr_err("invalid (null) rpm_regulator pointer\n"); |
| 811 | } else if (IS_ERR(regulator)) { |
| 812 | rc = PTR_ERR(regulator); |
| 813 | pr_err("invalid rpm_regulator pointer, rc=%d\n", rc); |
| 814 | } |
| 815 | |
| 816 | return rc; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * rpm_regulator_put() - free the RPM regulator handle |
| 821 | * @regulator: RPM regulator handle |
| 822 | * |
| 823 | * Parameter reaggregation does not take place when rpm_regulator_put is called. |
| 824 | * Therefore, regulator enable state and voltage must be configured |
| 825 | * appropriately before calling rpm_regulator_put. |
| 826 | * |
| 827 | * This function may be called from either atomic or nonatomic context. |
| 828 | */ |
| 829 | void rpm_regulator_put(struct rpm_regulator *regulator) |
| 830 | { |
| 831 | kfree(regulator); |
| 832 | } |
| 833 | EXPORT_SYMBOL_GPL(rpm_regulator_put); |
| 834 | |
| 835 | /** |
| 836 | * rpm_regulator_enable() - enable regulator output |
| 837 | * @regulator: RPM regulator handle |
| 838 | * |
| 839 | * Returns 0 on success or errno on failure. |
| 840 | * |
| 841 | * This function may be called from either atomic or nonatomic context. This |
| 842 | * function may only be called for regulators which have the sleep_selectable |
| 843 | * flag set in their configuration data. |
| 844 | * |
| 845 | * rpm_regulator_set_voltage must be called before rpm_regulator_enable because |
| 846 | * enabling is defined by the RPM interface to be requesting the desired |
| 847 | * non-zero regulator output voltage. |
| 848 | */ |
| 849 | int rpm_regulator_enable(struct rpm_regulator *regulator) |
| 850 | { |
| 851 | int rc = rpm_regulator_check_input(regulator); |
| 852 | struct vreg *vreg; |
| 853 | |
| 854 | if (rc) |
| 855 | return rc; |
| 856 | |
| 857 | if (regulator->vreg_id < config->vreg_id_min |
| 858 | || regulator->vreg_id > config->vreg_id_max) { |
| 859 | pr_err("invalid regulator id=%d\n", regulator->vreg_id); |
| 860 | return -EINVAL; |
| 861 | } |
| 862 | |
| 863 | vreg = &config->vregs[regulator->vreg_id]; |
| 864 | |
| 865 | /* |
| 866 | * Handle voltage switches which can be enabled without |
| 867 | * rpm_regulator_set_voltage ever being called. |
| 868 | */ |
| 869 | if (regulator->min_uV == 0 && regulator->max_uV == 0 |
| 870 | && vreg->part->uV.mask == 0 && vreg->part->mV.mask == 0) { |
| 871 | regulator->min_uV = 1; |
| 872 | regulator->max_uV = 1; |
| 873 | } |
| 874 | |
| 875 | if (regulator->min_uV == 0 && regulator->max_uV == 0) { |
| 876 | pr_err("Voltage must be set with rpm_regulator_set_voltage " |
| 877 | "before calling rpm_regulator_enable; vreg_id=%d, " |
| 878 | "voter=%d\n", regulator->vreg_id, regulator->voter); |
| 879 | return -EINVAL; |
| 880 | } |
| 881 | |
| 882 | rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, |
| 883 | regulator->min_uV, regulator->max_uV, regulator->sleep_also); |
| 884 | |
| 885 | if (rc) |
| 886 | pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc); |
| 887 | |
| 888 | return rc; |
| 889 | } |
| 890 | EXPORT_SYMBOL_GPL(rpm_regulator_enable); |
| 891 | |
| 892 | /** |
| 893 | * rpm_regulator_disable() - disable regulator output |
| 894 | * @regulator: RPM regulator handle |
| 895 | * |
| 896 | * Returns 0 on success or errno on failure. |
| 897 | * |
| 898 | * The enable state of the regulator is determined by aggregating the requests |
| 899 | * of all consumers. Therefore, it is possible that the regulator will remain |
| 900 | * enabled even after rpm_regulator_disable is called. |
| 901 | * |
| 902 | * This function may be called from either atomic or nonatomic context. This |
| 903 | * function may only be called for regulators which have the sleep_selectable |
| 904 | * flag set in their configuration data. |
| 905 | */ |
| 906 | int rpm_regulator_disable(struct rpm_regulator *regulator) |
| 907 | { |
| 908 | int rc = rpm_regulator_check_input(regulator); |
| 909 | |
| 910 | if (rc) |
| 911 | return rc; |
| 912 | |
| 913 | rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, 0, 0, |
| 914 | regulator->sleep_also); |
| 915 | |
| 916 | if (rc) |
| 917 | pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc); |
| 918 | |
| 919 | return rc; |
| 920 | } |
| 921 | EXPORT_SYMBOL_GPL(rpm_regulator_disable); |
| 922 | |
| 923 | /** |
| 924 | * rpm_regulator_set_voltage() - set regulator output voltage |
| 925 | * @regulator: RPM regulator handle |
| 926 | * @min_uV: minimum required voltage in uV |
| 927 | * @max_uV: maximum acceptable voltage in uV |
| 928 | * |
| 929 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 930 | * while the regulator is disabled or enabled. If the regulator is disabled, |
| 931 | * then rpm_regulator_set_voltage will both enable the regulator and set it to |
| 932 | * output at the requested voltage. |
| 933 | * |
| 934 | * The min_uV to max_uV voltage range requested must intersect with the |
| 935 | * voltage constraint range configured for the regulator. |
| 936 | * |
| 937 | * Returns 0 on success or errno on failure. |
| 938 | * |
| 939 | * The final voltage value that is sent to the RPM is aggregated based upon the |
| 940 | * values requested by all consumers of the regulator. This corresponds to the |
| 941 | * maximum min_uV value. |
| 942 | * |
| 943 | * This function may be called from either atomic or nonatomic context. This |
| 944 | * function may only be called for regulators which have the sleep_selectable |
| 945 | * flag set in their configuration data. |
| 946 | */ |
| 947 | int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV, |
| 948 | int max_uV) |
| 949 | { |
| 950 | int rc = rpm_regulator_check_input(regulator); |
| 951 | |
| 952 | if (rc) |
| 953 | return rc; |
| 954 | |
| 955 | rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, min_uV, |
| 956 | max_uV, regulator->sleep_also); |
| 957 | |
| 958 | if (rc) { |
| 959 | pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc); |
| 960 | } else { |
| 961 | regulator->min_uV = min_uV; |
| 962 | regulator->max_uV = max_uV; |
| 963 | } |
| 964 | |
| 965 | return rc; |
| 966 | } |
| 967 | EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage); |
| 968 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 969 | static inline int vreg_hpm_min_uA(struct vreg *vreg) |
| 970 | { |
| 971 | return vreg->hpm_min_load; |
| 972 | } |
| 973 | |
| 974 | static inline int vreg_lpm_max_uA(struct vreg *vreg) |
| 975 | { |
| 976 | return vreg->hpm_min_load - LOAD_THRESHOLD_STEP; |
| 977 | } |
| 978 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 979 | static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 980 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 981 | unsigned load_max |
| 982 | = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift); |
| 983 | |
| 984 | return (load_uA > load_max ? load_max : load_uA); |
| 985 | } |
| 986 | |
| 987 | static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA) |
| 988 | { |
| 989 | unsigned load_max |
| 990 | = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift); |
| 991 | return (load_uA > load_max ? load_max : load_uA); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /* Change vreg->req, but do not send it to the RPM. */ |
| 995 | static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0, |
| 996 | unsigned mask1, unsigned val1) |
| 997 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 998 | if (vreg->pdata.sleep_selectable) |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 999 | mutex_lock(&rpm_sleep_sel_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1000 | |
| 1001 | vreg->req[0].value &= ~mask0; |
| 1002 | vreg->req[0].value |= val0 & mask0; |
| 1003 | |
| 1004 | vreg->req[1].value &= ~mask1; |
| 1005 | vreg->req[1].value |= val1 & mask1; |
| 1006 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1007 | if (vreg->pdata.sleep_selectable) |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 1008 | mutex_unlock(&rpm_sleep_sel_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1009 | |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0, |
| 1014 | unsigned mask1, unsigned val1, unsigned cnt) |
| 1015 | { |
| 1016 | unsigned prev0 = 0, prev1 = 0; |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1017 | bool tcxo_enabled = false; |
| 1018 | bool voltage_increased = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1019 | int rc; |
| 1020 | |
| 1021 | /* |
| 1022 | * Bypass the normal route for regulators that can be called to change |
| 1023 | * just the active set values. |
| 1024 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1025 | if (vreg->pdata.sleep_selectable) |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 1026 | return vreg_set_sleep_sel(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1027 | mask0, val0, mask1, val1, cnt, 1); |
| 1028 | |
| 1029 | prev0 = vreg->req[0].value; |
| 1030 | vreg->req[0].value &= ~mask0; |
| 1031 | vreg->req[0].value |= val0 & mask0; |
| 1032 | |
| 1033 | prev1 = vreg->req[1].value; |
| 1034 | vreg->req[1].value &= ~mask1; |
| 1035 | vreg->req[1].value |= val1 & mask1; |
| 1036 | |
| 1037 | /* Ignore duplicate requests */ |
| 1038 | if (vreg->req[0].value == vreg->prev_active_req[0].value && |
| 1039 | vreg->req[1].value == vreg->prev_active_req[1].value) { |
| 1040 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1041 | rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1042 | return 0; |
| 1043 | } |
| 1044 | |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1045 | /* Enable CXO clock if necessary for TCXO workaround. */ |
| 1046 | if (requires_tcxo_workaround && vreg->requires_cxo |
| 1047 | && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) { |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 1048 | mutex_lock(&tcxo_mutex); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1049 | if (!tcxo_handle) |
| 1050 | tcxo_get_handle(); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1051 | voltage_increased = true; |
| 1052 | tcxo_enabled = tcxo_enable(); |
| 1053 | } |
| 1054 | |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 1055 | rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1056 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1057 | if (rc) { |
| 1058 | vreg->req[0].value = prev0; |
| 1059 | vreg->req[1].value = prev1; |
| 1060 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1061 | vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n", |
| 1062 | vreg->req[0].id, rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1063 | } else { |
| 1064 | if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1065 | rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1066 | vreg->prev_active_req[0].value = vreg->req[0].value; |
| 1067 | vreg->prev_active_req[1].value = vreg->req[1].value; |
| 1068 | } |
| 1069 | |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1070 | /* |
| 1071 | * Schedule CXO clock to be disabled after TCXO warmup time if TCXO |
| 1072 | * workaround is applicable for this regulator. |
| 1073 | */ |
| 1074 | if (voltage_increased) { |
| 1075 | if (tcxo_enabled) |
| 1076 | tcxo_delayed_disable(); |
David Collins | 65b5fa6 | 2012-10-17 17:02:11 -0700 | [diff] [blame] | 1077 | mutex_unlock(&tcxo_mutex); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1080 | return rc; |
| 1081 | } |
| 1082 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1083 | static int vreg_is_enabled(struct regulator_dev *rdev) |
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 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1086 | int enabled; |
| 1087 | |
| 1088 | mutex_lock(&vreg->pc_lock); |
| 1089 | enabled = vreg->is_enabled; |
| 1090 | mutex_unlock(&vreg->pc_lock); |
| 1091 | |
| 1092 | return enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1095 | static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1096 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1097 | switch (vreg->type) { |
| 1098 | case RPM_REGULATOR_TYPE_LDO: |
| 1099 | case RPM_REGULATOR_TYPE_SMPS: |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1100 | case RPM_REGULATOR_TYPE_CORNER: |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1101 | /* Enable by setting a voltage. */ |
| 1102 | if (vreg->part->uV.mask) { |
| 1103 | val[vreg->part->uV.word] |
| 1104 | |= vreg->save_uV << vreg->part->uV.shift; |
| 1105 | mask[vreg->part->uV.word] |= vreg->part->uV.mask; |
| 1106 | } else { |
| 1107 | val[vreg->part->mV.word] |
| 1108 | |= MICRO_TO_MILLI(vreg->save_uV) |
| 1109 | << vreg->part->mV.shift; |
| 1110 | mask[vreg->part->mV.word] |= vreg->part->mV.mask; |
| 1111 | } |
| 1112 | break; |
| 1113 | case RPM_REGULATOR_TYPE_VS: |
| 1114 | case RPM_REGULATOR_TYPE_NCP: |
| 1115 | /* Enable by setting enable_state. */ |
| 1116 | val[vreg->part->enable_state.word] |
| 1117 | |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift; |
| 1118 | mask[vreg->part->enable_state.word] |
| 1119 | |= vreg->part->enable_state.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1120 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1123 | static int rpm_vreg_enable(struct regulator_dev *rdev) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1124 | { |
| 1125 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1126 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 1127 | int rc = 0; |
| 1128 | |
| 1129 | set_enable(vreg, mask, val); |
| 1130 | |
| 1131 | mutex_lock(&vreg->pc_lock); |
| 1132 | |
| 1133 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1134 | vreg->part->request_len); |
| 1135 | if (!rc) |
| 1136 | vreg->is_enabled = true; |
| 1137 | |
| 1138 | mutex_unlock(&vreg->pc_lock); |
| 1139 | |
| 1140 | if (rc) |
| 1141 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 1142 | |
| 1143 | return rc; |
| 1144 | } |
| 1145 | |
| 1146 | static void set_disable(struct vreg *vreg, unsigned int *mask, |
| 1147 | unsigned int *val) |
| 1148 | { |
| 1149 | switch (vreg->type) { |
| 1150 | case RPM_REGULATOR_TYPE_LDO: |
| 1151 | case RPM_REGULATOR_TYPE_SMPS: |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1152 | case RPM_REGULATOR_TYPE_CORNER: |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1153 | /* Disable by setting a voltage of 0 uV. */ |
| 1154 | if (vreg->part->uV.mask) { |
| 1155 | val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift; |
| 1156 | mask[vreg->part->uV.word] |= vreg->part->uV.mask; |
| 1157 | } else { |
| 1158 | val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift; |
| 1159 | mask[vreg->part->mV.word] |= vreg->part->mV.mask; |
| 1160 | } |
| 1161 | break; |
| 1162 | case RPM_REGULATOR_TYPE_VS: |
| 1163 | case RPM_REGULATOR_TYPE_NCP: |
| 1164 | /* Disable by setting enable_state. */ |
| 1165 | val[vreg->part->enable_state.word] |
| 1166 | |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift; |
| 1167 | mask[vreg->part->enable_state.word] |
| 1168 | |= vreg->part->enable_state.mask; |
| 1169 | } |
| 1170 | } |
| 1171 | |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1172 | static int rpm_vreg_disable(struct regulator_dev *rdev) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1173 | { |
| 1174 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1175 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 1176 | int rc = 0; |
| 1177 | |
| 1178 | set_disable(vreg, mask, val); |
| 1179 | |
| 1180 | mutex_lock(&vreg->pc_lock); |
| 1181 | |
| 1182 | /* Only disable if pin control is not in use. */ |
| 1183 | if (!vreg->is_enabled_pc) |
| 1184 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1185 | vreg->part->request_len); |
| 1186 | |
| 1187 | if (!rc) |
| 1188 | vreg->is_enabled = false; |
| 1189 | |
| 1190 | mutex_unlock(&vreg->pc_lock); |
| 1191 | |
| 1192 | if (rc) |
| 1193 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 1194 | |
| 1195 | return rc; |
| 1196 | } |
| 1197 | |
| 1198 | 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] | 1199 | unsigned *selector) |
| 1200 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1201 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1202 | struct vreg_range *range = &vreg->set_points->range[0]; |
| 1203 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 1204 | int rc = 0, uV = min_uV; |
| 1205 | int lim_min_uV, lim_max_uV, i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1206 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1207 | /* Check if request voltage is outside of physically settable range. */ |
| 1208 | lim_min_uV = vreg->set_points->range[0].min_uV; |
| 1209 | lim_max_uV = |
| 1210 | vreg->set_points->range[vreg->set_points->count - 1].max_uV; |
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 (uV < lim_min_uV && max_uV >= lim_min_uV) |
| 1213 | uV = lim_min_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1214 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1215 | if (uV < lim_min_uV || uV > lim_max_uV) { |
| 1216 | vreg_err(vreg, |
| 1217 | "request v=[%d, %d] is outside possible v=[%d, %d]\n", |
| 1218 | min_uV, max_uV, lim_min_uV, lim_max_uV); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1219 | return -EINVAL; |
| 1220 | } |
| 1221 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1222 | /* Find the range which uV is inside of. */ |
| 1223 | for (i = vreg->set_points->count - 1; i > 0; i--) { |
| 1224 | if (uV > vreg->set_points->range[i - 1].max_uV) { |
| 1225 | range = &vreg->set_points->range[i]; |
| 1226 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1230 | /* |
| 1231 | * Force uV to be an allowed set point and apply a ceiling function |
| 1232 | * to non-set point values. |
| 1233 | */ |
| 1234 | uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV; |
| 1235 | uV = uV * range->step_uV + range->min_uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1236 | |
David Collins | 3974b61 | 2011-11-21 15:07:36 -0800 | [diff] [blame] | 1237 | if (uV > max_uV) { |
| 1238 | vreg_err(vreg, |
| 1239 | "request v=[%d, %d] cannot be met by any set point; " |
| 1240 | "next set point: %d\n", |
| 1241 | min_uV, max_uV, uV); |
| 1242 | return -EINVAL; |
| 1243 | } |
| 1244 | |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1245 | if (vreg->type == RPM_REGULATOR_TYPE_CORNER) { |
| 1246 | /* |
| 1247 | * Translate from enum values which work as inputs in the |
| 1248 | * regulator_set_voltage function to the actual corner values |
| 1249 | * sent to the RPM. |
| 1250 | */ |
| 1251 | uV -= RPM_VREG_CORNER_NONE; |
| 1252 | } |
| 1253 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1254 | if (vreg->part->uV.mask) { |
| 1255 | val[vreg->part->uV.word] = uV << vreg->part->uV.shift; |
| 1256 | mask[vreg->part->uV.word] = vreg->part->uV.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1257 | } else { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1258 | val[vreg->part->mV.word] |
| 1259 | = MICRO_TO_MILLI(uV) << vreg->part->mV.shift; |
| 1260 | mask[vreg->part->mV.word] = vreg->part->mV.mask; |
| 1261 | } |
| 1262 | |
| 1263 | mutex_lock(&vreg->pc_lock); |
| 1264 | |
| 1265 | /* |
| 1266 | * Only send a request for a new voltage if the regulator is currently |
| 1267 | * enabled. This will ensure that LDO and SMPS regulators are not |
| 1268 | * inadvertently turned on because voltage > 0 is equivalent to |
| 1269 | * enabling. For NCP, this just removes unnecessary RPM requests. |
| 1270 | */ |
| 1271 | if (vreg->is_enabled) { |
| 1272 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1273 | vreg->part->request_len); |
| 1274 | if (rc) |
| 1275 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 1276 | } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1277 | /* Regulator is disabled; store but don't send new request. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1278 | rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1281 | if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled)) |
| 1282 | vreg->save_uV = uV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1283 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1284 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1285 | |
| 1286 | return rc; |
| 1287 | } |
| 1288 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1289 | static int vreg_get_voltage(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1290 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1291 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1292 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1293 | return vreg->save_uV; |
| 1294 | } |
| 1295 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1296 | static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1297 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1298 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1299 | int uV = 0; |
| 1300 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1301 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1302 | if (!vreg->set_points) { |
| 1303 | vreg_err(vreg, "no voltages available\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1304 | return -EINVAL; |
| 1305 | } |
| 1306 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1307 | if (selector >= vreg->set_points->n_voltages) |
| 1308 | return 0; |
| 1309 | |
| 1310 | for (i = 0; i < vreg->set_points->count; i++) { |
| 1311 | if (selector < vreg->set_points->range[i].n_voltages) { |
| 1312 | uV = selector * vreg->set_points->range[i].step_uV |
| 1313 | + vreg->set_points->range[i].min_uV; |
| 1314 | break; |
| 1315 | } else { |
| 1316 | selector -= vreg->set_points->range[i].n_voltages; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | return uV; |
| 1321 | } |
| 1322 | |
| 1323 | static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 1324 | { |
| 1325 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1326 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 1327 | int rc = 0; |
| 1328 | int peak_uA; |
| 1329 | |
| 1330 | mutex_lock(&vreg->pc_lock); |
| 1331 | |
| 1332 | peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value |
| 1333 | & vreg->part->ip.mask) >> vreg->part->ip.shift); |
| 1334 | |
| 1335 | if (mode == config->mode_hpm) { |
| 1336 | /* Make sure that request currents are in HPM range. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1337 | if (peak_uA < vreg_hpm_min_uA(vreg)) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1338 | val[vreg->part->ip.word] |
| 1339 | = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg)) |
| 1340 | << vreg->part->ip.shift; |
| 1341 | mask[vreg->part->ip.word] = vreg->part->ip.mask; |
| 1342 | |
| 1343 | if (config->ia_follows_ip) { |
| 1344 | val[vreg->part->ia.word] |
| 1345 | |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg)) |
| 1346 | << vreg->part->ia.shift; |
| 1347 | mask[vreg->part->ia.word] |
| 1348 | |= vreg->part->ia.mask; |
| 1349 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1350 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1351 | } else if (mode == config->mode_lpm) { |
| 1352 | /* Make sure that request currents are in LPM range. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1353 | if (peak_uA > vreg_lpm_max_uA(vreg)) { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1354 | val[vreg->part->ip.word] |
| 1355 | = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg)) |
| 1356 | << vreg->part->ip.shift; |
| 1357 | mask[vreg->part->ip.word] = vreg->part->ip.mask; |
| 1358 | |
| 1359 | if (config->ia_follows_ip) { |
| 1360 | val[vreg->part->ia.word] |
| 1361 | |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg)) |
| 1362 | << vreg->part->ia.shift; |
| 1363 | mask[vreg->part->ia.word] |
| 1364 | |= vreg->part->ia.mask; |
| 1365 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1366 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1367 | } else { |
| 1368 | vreg_err(vreg, "invalid mode: %u\n", mode); |
| 1369 | mutex_unlock(&vreg->pc_lock); |
| 1370 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1373 | if (vreg->is_enabled) { |
| 1374 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1375 | vreg->part->request_len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1376 | } else { |
| 1377 | /* Regulator is disabled; store but don't send new request. */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1378 | rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1379 | } |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1380 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1381 | if (rc) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1382 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 1383 | else |
| 1384 | vreg->mode = mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1385 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1386 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1387 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1388 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1391 | static unsigned int vreg_get_mode(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1392 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1393 | struct vreg *vreg = rdev_get_drvdata(rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1394 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1395 | return vreg->mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1398 | static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev, |
| 1399 | int input_uV, int output_uV, int load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1400 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1401 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1402 | unsigned int mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1403 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1404 | load_uA += vreg->pdata.system_uA; |
| 1405 | |
| 1406 | mutex_lock(&vreg->pc_lock); |
| 1407 | SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA))); |
| 1408 | if (config->ia_follows_ip) |
| 1409 | SET_PART(vreg, ia, |
| 1410 | MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA))); |
| 1411 | mutex_unlock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1412 | |
| 1413 | if (load_uA >= vreg->hpm_min_load) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1414 | mode = config->mode_hpm; |
| 1415 | else |
| 1416 | mode = config->mode_lpm; |
| 1417 | |
| 1418 | return mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1421 | static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev, |
| 1422 | int input_uV, int output_uV, int load_uA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1423 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1424 | struct vreg *vreg = rdev_get_drvdata(rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1425 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1426 | if (MICRO_TO_MILLI(load_uA) <= 0) { |
| 1427 | /* |
| 1428 | * vreg_legacy_get_optimum_mode is being called before consumers |
| 1429 | * have specified their load currents via |
| 1430 | * regulator_set_optimum_mode. Return whatever the existing mode |
| 1431 | * is. |
| 1432 | */ |
| 1433 | return vreg->mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1436 | return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /* |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1440 | * Returns the logical pin control enable state because the pin control options |
| 1441 | * present in the hardware out of restart could be different from those desired |
| 1442 | * by the consumer. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1443 | */ |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1444 | static int vreg_pin_control_is_enabled(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1445 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1446 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1447 | |
| 1448 | return vreg->is_enabled_pc; |
| 1449 | } |
| 1450 | |
| 1451 | static int vreg_pin_control_enable(struct regulator_dev *rdev) |
| 1452 | { |
| 1453 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1454 | unsigned int mask[2] = {0}, val[2] = {0}; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1455 | int rc; |
| 1456 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1457 | mutex_lock(&vreg->pc_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1458 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1459 | val[vreg->part->pc.word] |
| 1460 | |= vreg->pdata.pin_ctrl << vreg->part->pc.shift; |
| 1461 | mask[vreg->part->pc.word] |= vreg->part->pc.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1462 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1463 | val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift; |
| 1464 | mask[vreg->part->pf.word] |= vreg->part->pf.mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1465 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1466 | if (!vreg->is_enabled) |
| 1467 | set_enable(vreg, mask, val); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1468 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1469 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1470 | vreg->part->request_len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1471 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1472 | if (!rc) |
| 1473 | vreg->is_enabled_pc = true; |
| 1474 | |
| 1475 | mutex_unlock(&vreg->pc_lock); |
| 1476 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1477 | if (rc) |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1478 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1479 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1480 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1483 | static int vreg_pin_control_disable(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1484 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1485 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1486 | unsigned int mask[2] = {0}, val[2] = {0}; |
| 1487 | int pin_fn, rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1488 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1489 | mutex_lock(&vreg->pc_lock); |
| 1490 | |
| 1491 | val[vreg->part->pc.word] |
| 1492 | |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift; |
| 1493 | mask[vreg->part->pc.word] |= vreg->part->pc.mask; |
| 1494 | |
| 1495 | pin_fn = config->pin_func_none; |
| 1496 | if (vreg->pdata.pin_fn == config->pin_func_sleep_b) |
| 1497 | pin_fn = config->pin_func_sleep_b; |
| 1498 | val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift; |
| 1499 | mask[vreg->part->pf.word] |= vreg->part->pf.mask; |
| 1500 | |
| 1501 | if (!vreg->is_enabled) |
| 1502 | set_disable(vreg, mask, val); |
| 1503 | |
| 1504 | rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1], |
| 1505 | vreg->part->request_len); |
| 1506 | |
| 1507 | if (!rc) |
| 1508 | vreg->is_enabled_pc = false; |
| 1509 | |
| 1510 | mutex_unlock(&vreg->pc_lock); |
| 1511 | |
| 1512 | if (rc) |
| 1513 | vreg_err(vreg, "vreg_set failed, rc=%d\n", rc); |
| 1514 | |
| 1515 | return rc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1518 | static int vreg_enable_time(struct regulator_dev *rdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1519 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1520 | struct vreg *vreg = rdev_get_drvdata(rdev); |
| 1521 | |
| 1522 | return vreg->pdata.enable_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1525 | /* Real regulator operations. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1526 | static struct regulator_ops ldo_ops = { |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1527 | .enable = rpm_vreg_enable, |
| 1528 | .disable = rpm_vreg_disable, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1529 | .is_enabled = vreg_is_enabled, |
| 1530 | .set_voltage = vreg_set_voltage, |
| 1531 | .get_voltage = vreg_get_voltage, |
| 1532 | .list_voltage = vreg_list_voltage, |
| 1533 | .set_mode = vreg_set_mode, |
| 1534 | .get_mode = vreg_get_mode, |
| 1535 | .get_optimum_mode = vreg_get_optimum_mode, |
| 1536 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1537 | }; |
| 1538 | |
| 1539 | static struct regulator_ops smps_ops = { |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1540 | .enable = rpm_vreg_enable, |
| 1541 | .disable = rpm_vreg_disable, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1542 | .is_enabled = vreg_is_enabled, |
| 1543 | .set_voltage = vreg_set_voltage, |
| 1544 | .get_voltage = vreg_get_voltage, |
| 1545 | .list_voltage = vreg_list_voltage, |
| 1546 | .set_mode = vreg_set_mode, |
| 1547 | .get_mode = vreg_get_mode, |
| 1548 | .get_optimum_mode = vreg_get_optimum_mode, |
| 1549 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1550 | }; |
| 1551 | |
| 1552 | static struct regulator_ops switch_ops = { |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1553 | .enable = rpm_vreg_enable, |
| 1554 | .disable = rpm_vreg_disable, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1555 | .is_enabled = vreg_is_enabled, |
| 1556 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1557 | }; |
| 1558 | |
| 1559 | static struct regulator_ops ncp_ops = { |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1560 | .enable = rpm_vreg_enable, |
| 1561 | .disable = rpm_vreg_disable, |
| 1562 | .is_enabled = vreg_is_enabled, |
| 1563 | .set_voltage = vreg_set_voltage, |
| 1564 | .get_voltage = vreg_get_voltage, |
| 1565 | .list_voltage = vreg_list_voltage, |
| 1566 | .enable_time = vreg_enable_time, |
| 1567 | }; |
| 1568 | |
| 1569 | static struct regulator_ops corner_ops = { |
| 1570 | .enable = rpm_vreg_enable, |
| 1571 | .disable = rpm_vreg_disable, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1572 | .is_enabled = vreg_is_enabled, |
| 1573 | .set_voltage = vreg_set_voltage, |
| 1574 | .get_voltage = vreg_get_voltage, |
| 1575 | .list_voltage = vreg_list_voltage, |
| 1576 | .enable_time = vreg_enable_time, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1577 | }; |
| 1578 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1579 | /* Pin control regulator operations. */ |
| 1580 | static struct regulator_ops pin_control_ops = { |
| 1581 | .enable = vreg_pin_control_enable, |
| 1582 | .disable = vreg_pin_control_disable, |
| 1583 | .is_enabled = vreg_pin_control_is_enabled, |
| 1584 | }; |
| 1585 | |
| 1586 | struct regulator_ops *vreg_ops[] = { |
| 1587 | [RPM_REGULATOR_TYPE_LDO] = &ldo_ops, |
| 1588 | [RPM_REGULATOR_TYPE_SMPS] = &smps_ops, |
| 1589 | [RPM_REGULATOR_TYPE_VS] = &switch_ops, |
| 1590 | [RPM_REGULATOR_TYPE_NCP] = &ncp_ops, |
David Collins | 0ac31fe | 2012-02-08 13:53:34 -0800 | [diff] [blame] | 1591 | [RPM_REGULATOR_TYPE_CORNER] = &corner_ops, |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1592 | }; |
| 1593 | |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1594 | static struct vreg *rpm_vreg_get_vreg(int id) |
| 1595 | { |
| 1596 | struct vreg *vreg; |
| 1597 | |
| 1598 | if (id < config->vreg_id_min || id > config->vreg_id_max) |
| 1599 | return NULL; |
| 1600 | |
| 1601 | if (!config->is_real_id(id)) |
| 1602 | id = config->pc_id_to_real_id(id); |
| 1603 | vreg = &config->vregs[id]; |
| 1604 | |
| 1605 | return vreg; |
| 1606 | } |
| 1607 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1608 | static int __devinit |
| 1609 | rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata, |
| 1610 | struct device *dev) |
| 1611 | { |
| 1612 | struct regulator_desc *rdesc = NULL; |
| 1613 | struct regulator_dev *rdev; |
| 1614 | struct vreg *vreg; |
| 1615 | unsigned pin_ctrl; |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1616 | int pin_fn; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1617 | int rc = 0; |
| 1618 | |
| 1619 | if (!pdata) { |
| 1620 | pr_err("platform data missing\n"); |
| 1621 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1624 | vreg = rpm_vreg_get_vreg(pdata->id); |
| 1625 | if (!vreg) { |
| 1626 | pr_err("invalid regulator id: %d\n", pdata->id); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1627 | return -ENODEV; |
| 1628 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1629 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1630 | if (config->is_real_id(pdata->id)) |
| 1631 | rdesc = &vreg->rdesc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1632 | else |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1633 | rdesc = &vreg->rdesc_pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1634 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1635 | if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) { |
| 1636 | pr_err("%s: invalid regulator type: %d\n", |
| 1637 | vreg->rdesc.name, vreg->type); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1638 | return -EINVAL; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1639 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1640 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1641 | mutex_lock(&vreg->pc_lock); |
| 1642 | |
| 1643 | if (vreg->set_points) |
| 1644 | rdesc->n_voltages = vreg->set_points->n_voltages; |
| 1645 | else |
| 1646 | rdesc->n_voltages = 0; |
| 1647 | |
| 1648 | rdesc->id = pdata->id; |
| 1649 | rdesc->owner = THIS_MODULE; |
| 1650 | rdesc->type = REGULATOR_VOLTAGE; |
| 1651 | |
| 1652 | if (config->is_real_id(pdata->id)) { |
| 1653 | /* |
| 1654 | * Real regulator; do not modify pin control and pin function |
| 1655 | * values. |
| 1656 | */ |
| 1657 | rdesc->ops = vreg_ops[vreg->type]; |
| 1658 | pin_ctrl = vreg->pdata.pin_ctrl; |
| 1659 | pin_fn = vreg->pdata.pin_fn; |
| 1660 | memcpy(&(vreg->pdata), pdata, |
| 1661 | sizeof(struct rpm_regulator_init_data)); |
| 1662 | vreg->pdata.pin_ctrl = pin_ctrl; |
| 1663 | vreg->pdata.pin_fn = pin_fn; |
| 1664 | |
| 1665 | vreg->save_uV = vreg->pdata.default_uV; |
| 1666 | if (vreg->pdata.peak_uA >= vreg->hpm_min_load) |
| 1667 | vreg->mode = config->mode_hpm; |
| 1668 | else |
| 1669 | vreg->mode = config->mode_lpm; |
| 1670 | |
| 1671 | /* Initialize the RPM request. */ |
| 1672 | SET_PART(vreg, ip, |
| 1673 | MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA))); |
| 1674 | SET_PART(vreg, fm, vreg->pdata.force_mode); |
| 1675 | SET_PART(vreg, pm, vreg->pdata.power_mode); |
| 1676 | SET_PART(vreg, pd, vreg->pdata.pull_down_enable); |
| 1677 | SET_PART(vreg, ia, |
| 1678 | MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA))); |
| 1679 | SET_PART(vreg, freq, vreg->pdata.freq); |
| 1680 | SET_PART(vreg, freq_clk_src, 0); |
| 1681 | SET_PART(vreg, comp_mode, 0); |
| 1682 | SET_PART(vreg, hpm, 0); |
| 1683 | if (!vreg->is_enabled_pc) { |
| 1684 | SET_PART(vreg, pf, config->pin_func_none); |
| 1685 | SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE); |
| 1686 | } |
| 1687 | } else { |
| 1688 | if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL) |
| 1689 | == RPM_VREG_PIN_CTRL_NONE |
| 1690 | && pdata->pin_fn != config->pin_func_sleep_b) { |
| 1691 | pr_err("%s: no pin control input specified\n", |
| 1692 | vreg->rdesc.name); |
| 1693 | mutex_unlock(&vreg->pc_lock); |
| 1694 | return -EINVAL; |
| 1695 | } |
| 1696 | rdesc->ops = &pin_control_ops; |
| 1697 | vreg->pdata.pin_ctrl = pdata->pin_ctrl; |
| 1698 | vreg->pdata.pin_fn = pdata->pin_fn; |
| 1699 | |
| 1700 | /* Initialize the RPM request. */ |
| 1701 | pin_fn = config->pin_func_none; |
| 1702 | /* Allow pf=sleep_b to be specified by platform data. */ |
| 1703 | if (vreg->pdata.pin_fn == config->pin_func_sleep_b) |
| 1704 | pin_fn = config->pin_func_sleep_b; |
| 1705 | SET_PART(vreg, pf, pin_fn); |
| 1706 | SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE); |
| 1707 | } |
| 1708 | |
| 1709 | mutex_unlock(&vreg->pc_lock); |
| 1710 | |
| 1711 | if (rc) |
| 1712 | goto bail; |
| 1713 | |
Rajendra Nayak | 11eafc6 | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 1714 | rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg, NULL); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1715 | if (IS_ERR(rdev)) { |
| 1716 | rc = PTR_ERR(rdev); |
| 1717 | pr_err("regulator_register failed: %s, rc=%d\n", |
| 1718 | vreg->rdesc.name, rc); |
| 1719 | return rc; |
| 1720 | } else { |
| 1721 | if (config->is_real_id(pdata->id)) |
| 1722 | vreg->rdev = rdev; |
| 1723 | else |
| 1724 | vreg->rdev_pc = rdev; |
| 1725 | } |
| 1726 | |
| 1727 | bail: |
| 1728 | if (rc) |
| 1729 | pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc); |
| 1730 | |
| 1731 | return rc; |
| 1732 | } |
| 1733 | |
| 1734 | static void rpm_vreg_set_point_init(void) |
| 1735 | { |
| 1736 | struct vreg_set_points **set_points; |
| 1737 | int i, j, temp; |
| 1738 | |
| 1739 | set_points = config->set_points; |
| 1740 | |
| 1741 | /* Calculate the number of set points available for each regulator. */ |
| 1742 | for (i = 0; i < config->set_points_len; i++) { |
| 1743 | temp = 0; |
| 1744 | for (j = 0; j < set_points[i]->count; j++) { |
| 1745 | set_points[i]->range[j].n_voltages |
| 1746 | = (set_points[i]->range[j].max_uV |
| 1747 | - set_points[i]->range[j].min_uV) |
| 1748 | / set_points[i]->range[j].step_uV + 1; |
| 1749 | temp += set_points[i]->range[j].n_voltages; |
| 1750 | } |
| 1751 | set_points[i]->n_voltages = temp; |
| 1752 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | static int __devinit rpm_vreg_probe(struct platform_device *pdev) |
| 1756 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1757 | struct rpm_regulator_platform_data *platform_data; |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 1758 | static struct rpm_regulator_consumer_mapping *prev_consumer_map; |
| 1759 | static int prev_consumer_map_len; |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1760 | int rc = 0; |
| 1761 | int i, id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1762 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1763 | platform_data = pdev->dev.platform_data; |
| 1764 | if (!platform_data) { |
| 1765 | pr_err("rpm-regulator requires platform data\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1766 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1769 | if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX |
| 1770 | && platform_data->version != rpm_version) { |
| 1771 | pr_err("rpm version %d does not match previous version %d\n", |
| 1772 | platform_data->version, rpm_version); |
| 1773 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1776 | if (platform_data->version < 0 |
| 1777 | || platform_data->version > RPM_VREG_VERSION_MAX) { |
| 1778 | pr_err("rpm version %d is invalid\n", platform_data->version); |
| 1779 | return -EINVAL; |
| 1780 | } |
| 1781 | |
| 1782 | if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) { |
| 1783 | rpm_version = platform_data->version; |
| 1784 | config = get_config[platform_data->version](); |
| 1785 | vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem; |
| 1786 | vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig; |
| 1787 | if (!config) { |
| 1788 | pr_err("rpm version %d is not available\n", |
| 1789 | platform_data->version); |
| 1790 | return -ENODEV; |
| 1791 | } |
| 1792 | if (config->use_legacy_optimum_mode) |
| 1793 | for (i = 0; i < ARRAY_SIZE(vreg_ops); i++) |
| 1794 | vreg_ops[i]->get_optimum_mode |
| 1795 | = vreg_legacy_get_optimum_mode; |
| 1796 | rpm_vreg_set_point_init(); |
| 1797 | /* First time probed; initialize pin control mutexes. */ |
| 1798 | for (i = 0; i < config->vregs_len; i++) |
| 1799 | mutex_init(&config->vregs[i].pc_lock); |
| 1800 | } |
| 1801 | |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 1802 | /* Copy the list of private API consumers. */ |
| 1803 | if (platform_data->consumer_map_len > 0) { |
| 1804 | if (consumer_map_len == 0) { |
| 1805 | consumer_map_len = platform_data->consumer_map_len; |
| 1806 | consumer_map = kmemdup(platform_data->consumer_map, |
| 1807 | sizeof(struct rpm_regulator_consumer_mapping) |
| 1808 | * consumer_map_len, GFP_KERNEL); |
| 1809 | if (consumer_map == NULL) { |
| 1810 | pr_err("memory allocation failed\n"); |
| 1811 | consumer_map_len = 0; |
| 1812 | return -ENOMEM; |
| 1813 | } |
| 1814 | } else { |
| 1815 | /* Concatenate new map with the existing one. */ |
| 1816 | prev_consumer_map = consumer_map; |
| 1817 | prev_consumer_map_len = consumer_map_len; |
| 1818 | consumer_map_len += platform_data->consumer_map_len; |
| 1819 | consumer_map = kmalloc( |
| 1820 | sizeof(struct rpm_regulator_consumer_mapping) |
| 1821 | * consumer_map_len, GFP_KERNEL); |
| 1822 | if (consumer_map == NULL) { |
| 1823 | pr_err("memory allocation failed\n"); |
| 1824 | consumer_map_len = 0; |
| 1825 | return -ENOMEM; |
| 1826 | } |
| 1827 | memcpy(consumer_map, prev_consumer_map, |
| 1828 | sizeof(struct rpm_regulator_consumer_mapping) |
| 1829 | * prev_consumer_map_len); |
| 1830 | memcpy(&consumer_map[prev_consumer_map_len], |
| 1831 | platform_data->consumer_map, |
| 1832 | sizeof(struct rpm_regulator_consumer_mapping) |
| 1833 | * platform_data->consumer_map_len); |
| 1834 | } |
| 1835 | |
| 1836 | } |
| 1837 | |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1838 | if (platform_data->requires_tcxo_workaround |
| 1839 | && !requires_tcxo_workaround) { |
| 1840 | requires_tcxo_workaround = true; |
| 1841 | wake_lock_init(&tcxo_wake_lock, WAKE_LOCK_SUSPEND, |
| 1842 | "rpm_regulator_tcxo"); |
| 1843 | } |
| 1844 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1845 | /* Initialize all of the regulators listed in the platform data. */ |
| 1846 | for (i = 0; i < platform_data->num_regulators; i++) { |
| 1847 | rc = rpm_vreg_init_regulator(&platform_data->init_data[i], |
| 1848 | &pdev->dev); |
| 1849 | if (rc) { |
| 1850 | pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc); |
| 1851 | goto remove_regulators; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | platform_set_drvdata(pdev, platform_data); |
| 1856 | |
| 1857 | return rc; |
| 1858 | |
| 1859 | remove_regulators: |
| 1860 | /* Unregister all regulators added before the erroring one. */ |
| 1861 | for (; i >= 0; i--) { |
| 1862 | id = platform_data->init_data[i].id; |
| 1863 | if (config->is_real_id(id)) { |
| 1864 | regulator_unregister(config->vregs[id].rdev); |
| 1865 | config->vregs[id].rdev = NULL; |
| 1866 | } else { |
| 1867 | regulator_unregister(config->vregs[ |
| 1868 | config->pc_id_to_real_id(id)].rdev_pc); |
| 1869 | config->vregs[id].rdev_pc = NULL; |
| 1870 | } |
| 1871 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1872 | |
| 1873 | return rc; |
| 1874 | } |
| 1875 | |
| 1876 | static int __devexit rpm_vreg_remove(struct platform_device *pdev) |
| 1877 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1878 | struct rpm_regulator_platform_data *platform_data; |
| 1879 | int i, id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1880 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1881 | platform_data = platform_get_drvdata(pdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1882 | platform_set_drvdata(pdev, NULL); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1883 | |
| 1884 | if (platform_data) { |
| 1885 | for (i = 0; i < platform_data->num_regulators; i++) { |
| 1886 | id = platform_data->init_data[i].id; |
| 1887 | if (config->is_real_id(id)) { |
| 1888 | regulator_unregister(config->vregs[id].rdev); |
| 1889 | config->vregs[id].rdev = NULL; |
| 1890 | } else { |
| 1891 | regulator_unregister(config->vregs[ |
| 1892 | config->pc_id_to_real_id(id)].rdev_pc); |
| 1893 | config->vregs[id].rdev_pc = NULL; |
| 1894 | } |
| 1895 | } |
| 1896 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1897 | |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | static struct platform_driver rpm_vreg_driver = { |
| 1902 | .probe = rpm_vreg_probe, |
| 1903 | .remove = __devexit_p(rpm_vreg_remove), |
| 1904 | .driver = { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1905 | .name = RPM_REGULATOR_DEV_NAME, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1906 | .owner = THIS_MODULE, |
| 1907 | }, |
| 1908 | }; |
| 1909 | |
| 1910 | static int __init rpm_vreg_init(void) |
| 1911 | { |
| 1912 | return platform_driver_register(&rpm_vreg_driver); |
| 1913 | } |
| 1914 | |
| 1915 | static void __exit rpm_vreg_exit(void) |
| 1916 | { |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1917 | int i; |
| 1918 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1919 | platform_driver_unregister(&rpm_vreg_driver); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1920 | |
David Collins | 379b4b7 | 2012-05-14 16:33:51 -0700 | [diff] [blame] | 1921 | kfree(consumer_map); |
| 1922 | |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1923 | for (i = 0; i < config->vregs_len; i++) |
| 1924 | mutex_destroy(&config->vregs[i].pc_lock); |
David Collins | 109a8e6 | 2012-07-31 15:21:35 -0700 | [diff] [blame] | 1925 | |
| 1926 | if (tcxo_handle) |
| 1927 | clk_put(tcxo_handle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | postcore_initcall(rpm_vreg_init); |
| 1931 | module_exit(rpm_vreg_exit); |
| 1932 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1933 | MODULE_LICENSE("GPL v2"); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1934 | MODULE_DESCRIPTION("MSM RPM regulator driver"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1935 | MODULE_VERSION("1.0"); |
David Collins | 6f032ba | 2011-08-31 14:08:15 -0700 | [diff] [blame] | 1936 | MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME); |