blob: 5c2bb8e9d5a0923444db82019b91bf2f3ef8870e [file] [log] [blame]
David Collins6f032ba2011-08-31 14:08:15 -07001/*
2 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 *
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 Collins6f032ba2011-08-31 14:08:15 -070014#define pr_fmt(fmt) "%s: " fmt, __func__
15
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070016#include <linux/module.h>
17#include <linux/err.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
David Collins6f032ba2011-08-31 14:08:15 -070020#include <linux/slab.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070021#include <linux/spinlock.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/driver.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <mach/rpm.h>
25#include <mach/rpm-regulator.h>
David Collins6f032ba2011-08-31 14:08:15 -070026#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#include "rpm_resources.h"
David Collins6f032ba2011-08-31 14:08:15 -070029#include "rpm-regulator-private.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31/* Debug Definitions */
32
33enum {
34 MSM_RPM_VREG_DEBUG_REQUEST = BIT(0),
35 MSM_RPM_VREG_DEBUG_VOTE = BIT(1),
36 MSM_RPM_VREG_DEBUG_DUPLICATE = BIT(2),
David Collins6f032ba2011-08-31 14:08:15 -070037 MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG = BIT(3),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038};
39
40static int msm_rpm_vreg_debug_mask;
41module_param_named(
42 debug_mask, msm_rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR
43);
44
David Collins6f032ba2011-08-31 14:08:15 -070045struct vreg_config *(*get_config[])(void) = {
46 [RPM_VREG_VERSION_8660] = get_config_8660,
47 [RPM_VREG_VERSION_8960] = get_config_8960,
David Collins6ef12bf2011-08-31 14:08:15 -070048 [RPM_VREG_VERSION_9615] = get_config_9615,
David Collins6f032ba2011-08-31 14:08:15 -070049};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
David Collins6f032ba2011-08-31 14:08:15 -070051#define SET_PART(_vreg, _part, _val) \
52 _vreg->req[_vreg->part->_part.word].value \
53 = (_vreg->req[_vreg->part->_part.word].value \
54 & ~vreg->part->_part.mask) \
55 | (((_val) << vreg->part->_part.shift) & vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056
David Collins6f032ba2011-08-31 14:08:15 -070057#define GET_PART(_vreg, _part) \
58 ((_vreg->req[_vreg->part->_part.word].value & vreg->part->_part.mask) \
59 >> vreg->part->_part.shift)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060
David Collins6f032ba2011-08-31 14:08:15 -070061#define USES_PART(_vreg, _part) (vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062
David Collins6f032ba2011-08-31 14:08:15 -070063#define vreg_err(vreg, fmt, ...) \
64 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065
David Collins6f032ba2011-08-31 14:08:15 -070066#define RPM_VREG_PIN_CTRL_EN0 0x01
67#define RPM_VREG_PIN_CTRL_EN1 0x02
68#define RPM_VREG_PIN_CTRL_EN2 0x04
69#define RPM_VREG_PIN_CTRL_EN3 0x08
70#define RPM_VREG_PIN_CTRL_ALL 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071
David Collins6f032ba2011-08-31 14:08:15 -070072static const char *label_freq[] = {
73 [RPM_VREG_FREQ_NONE] = " N/A",
74 [RPM_VREG_FREQ_19p20] = "19.2",
75 [RPM_VREG_FREQ_9p60] = "9.60",
76 [RPM_VREG_FREQ_6p40] = "6.40",
77 [RPM_VREG_FREQ_4p80] = "4.80",
78 [RPM_VREG_FREQ_3p84] = "3.84",
79 [RPM_VREG_FREQ_3p20] = "3.20",
80 [RPM_VREG_FREQ_2p74] = "2.74",
81 [RPM_VREG_FREQ_2p40] = "2.40",
82 [RPM_VREG_FREQ_2p13] = "2.13",
83 [RPM_VREG_FREQ_1p92] = "1.92",
84 [RPM_VREG_FREQ_1p75] = "1.75",
85 [RPM_VREG_FREQ_1p60] = "1.60",
86 [RPM_VREG_FREQ_1p48] = "1.48",
87 [RPM_VREG_FREQ_1p37] = "1.37",
88 [RPM_VREG_FREQ_1p28] = "1.28",
89 [RPM_VREG_FREQ_1p20] = "1.20",
90};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091/*
92 * This is used when voting for LPM or HPM by subtracting or adding to the
93 * hpm_min_load of a regulator. It has units of uA.
94 */
David Collins6f032ba2011-08-31 14:08:15 -070095#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096
David Collins6f032ba2011-08-31 14:08:15 -070097/* rpm_version keeps track of the version for the currently running driver. */
98enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099
David Collins6f032ba2011-08-31 14:08:15 -0700100/* config holds all configuration data of the currently running driver. */
101static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102
David Collins6f032ba2011-08-31 14:08:15 -0700103/* These regulator ID values are specified in the board file. */
104static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105
David Collins6f032ba2011-08-31 14:08:15 -0700106static inline int vreg_id_is_vdd_mem_or_dig(int id)
107{
108 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
109}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110
David Collins6f032ba2011-08-31 14:08:15 -0700111#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112
David Collins6f032ba2011-08-31 14:08:15 -0700113static void rpm_regulator_req(struct vreg *vreg, int set)
114{
115 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
116 const char *pf_label = "", *fm_label = "", *pc_total = "";
117 const char *pc_en[4] = {"", "", "", ""};
118 const char *pm_label = "", *freq_label = "";
119 char buf[DEBUG_PRINT_BUFFER_SIZE];
120 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
121 int pos = 0;
122
123 /* Suppress VDD_MEM and VDD_DIG printing. */
124 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
125 && vreg_id_is_vdd_mem_or_dig(vreg->id))
126 return;
127
128 uV = GET_PART(vreg, uV);
129 mV = GET_PART(vreg, mV);
130 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
131 uV = -uV;
132 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 }
134
David Collins6f032ba2011-08-31 14:08:15 -0700135 fm = GET_PART(vreg, fm);
136 pm = GET_PART(vreg, pm);
137 pc = GET_PART(vreg, pc);
138 pf = GET_PART(vreg, pf);
139 pd = GET_PART(vreg, pd);
140 freq = GET_PART(vreg, freq);
141 state = GET_PART(vreg, enable_state);
142
143 if (pf >= 0 && pf < config->label_pin_func_len)
144 pf_label = config->label_pin_func[pf];
145
146 if (fm >= 0 && fm < config->label_force_mode_len)
147 fm_label = config->label_force_mode[fm];
148
149 if (pm >= 0 && pm < config->label_power_mode_len)
150 pm_label = config->label_power_mode[pm];
151
152 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
153 freq_label = label_freq[freq];
154
155 for (i = 0; i < config->label_pin_ctrl_len; i++)
156 if (pc & (1 << i))
157 pc_en[i] = config->label_pin_ctrl[i];
158
159 if (pc == RPM_VREG_PIN_CTRL_NONE)
160 pc_total = " none";
161
162 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
163 KERN_INFO, __func__);
164
165 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
166 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
167 vreg->rdesc.name,
168 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
169
170 if (USES_PART(vreg, uV))
171 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
172 if (USES_PART(vreg, mV))
173 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
174 if (USES_PART(vreg, enable_state))
175 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
176 (state == 1 ? "on" : "off"), state);
177 if (USES_PART(vreg, ip))
178 pos += scnprintf(buf + pos, buflen - pos,
179 ", ip=%4d mA", GET_PART(vreg, ip));
180 if (USES_PART(vreg, fm))
181 pos += scnprintf(buf + pos, buflen - pos,
182 ", fm=%s (%d)", fm_label, fm);
183 if (USES_PART(vreg, pc))
184 pos += scnprintf(buf + pos, buflen - pos,
185 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
186 pc_en[2], pc_en[3], pc_total, pc);
187 if (USES_PART(vreg, pf))
188 pos += scnprintf(buf + pos, buflen - pos,
189 ", pf=%s (%d)", pf_label, pf);
190 if (USES_PART(vreg, pd))
191 pos += scnprintf(buf + pos, buflen - pos,
192 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
193 if (USES_PART(vreg, ia))
194 pos += scnprintf(buf + pos, buflen - pos,
195 ", ia=%4d mA", GET_PART(vreg, ia));
196 if (USES_PART(vreg, freq)) {
197 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
198 pos += scnprintf(buf + pos, buflen - pos,
199 ", freq=%2d", freq);
200 else
201 pos += scnprintf(buf + pos, buflen - pos,
202 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203 }
David Collins6f032ba2011-08-31 14:08:15 -0700204 if (USES_PART(vreg, pm))
205 pos += scnprintf(buf + pos, buflen - pos,
206 ", pm=%s (%d)", pm_label, pm);
207 if (USES_PART(vreg, freq_clk_src))
208 pos += scnprintf(buf + pos, buflen - pos,
209 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
210 if (USES_PART(vreg, comp_mode))
211 pos += scnprintf(buf + pos, buflen - pos,
212 ", comp=%d", GET_PART(vreg, comp_mode));
213 if (USES_PART(vreg, hpm))
214 pos += scnprintf(buf + pos, buflen - pos,
215 ", hpm=%d", GET_PART(vreg, hpm));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216
David Collins6f032ba2011-08-31 14:08:15 -0700217 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
218 vreg->req[0].id, vreg->req[0].value);
219 if (vreg->part->request_len > 1)
220 pos += scnprintf(buf + pos, buflen - pos,
221 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
222 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223
David Collins6f032ba2011-08-31 14:08:15 -0700224 pos += scnprintf(buf + pos, buflen - pos, "\n");
225 printk(buf);
226}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227
David Collins6f032ba2011-08-31 14:08:15 -0700228static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
229 int set, int voter_uV, int aggregate_uV)
230{
231 /* Suppress VDD_MEM and VDD_DIG printing. */
232 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
233 && vreg_id_is_vdd_mem_or_dig(vreg->id))
234 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235
David Collins6f032ba2011-08-31 14:08:15 -0700236 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
237 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
238 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
239}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240
David Collins6f032ba2011-08-31 14:08:15 -0700241static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
242{
243 /* Suppress VDD_MEM and VDD_DIG printing. */
244 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
245 && vreg_id_is_vdd_mem_or_dig(vreg->id))
246 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247
David Collins6f032ba2011-08-31 14:08:15 -0700248 if (cnt == 2)
249 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
250 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
251 (set == 0 ? 'A' : 'S'),
252 vreg->req[0].id, vreg->req[0].value,
253 vreg->req[1].id, vreg->req[1].value);
254 else if (cnt == 1)
255 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
256 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
257 vreg->req[0].id, vreg->req[0].value);
258}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259
260/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700261static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262
263static int voltage_from_req(struct vreg *vreg)
264{
David Collins6f032ba2011-08-31 14:08:15 -0700265 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266
David Collins6f032ba2011-08-31 14:08:15 -0700267 if (vreg->part->uV.mask)
268 uV = GET_PART(vreg, uV);
269 else
270 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271
David Collins6f032ba2011-08-31 14:08:15 -0700272 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273}
274
David Collins6f032ba2011-08-31 14:08:15 -0700275static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276{
David Collins6f032ba2011-08-31 14:08:15 -0700277 if (vreg->part->uV.mask)
278 SET_PART(vreg, uV, uV);
279 else
280 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281}
282
283static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
284 int set, unsigned mask0, unsigned val0,
285 unsigned mask1, unsigned val1, unsigned cnt,
286 int update_voltage)
287{
288 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700289 int rc = 0, max_uV_vote = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700291 int *min_uV_vote;
292 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293
294 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700295 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 prev_req = vreg->prev_active_req;
297 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700298 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 prev_req = vreg->prev_sleep_req;
300 }
301
302 prev0 = vreg->req[0].value;
303 vreg->req[0].value &= ~mask0;
304 vreg->req[0].value |= val0 & mask0;
305
306 prev1 = vreg->req[1].value;
307 vreg->req[1].value &= ~mask1;
308 vreg->req[1].value |= val1 & mask1;
309
310 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700311 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312
313 /* Find the highest voltage voted for and use it. */
314 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700315 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
316 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317
318 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700319 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
320 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321
322 /* Ignore duplicate requests */
323 if (vreg->req[0].value != prev_req[0].value ||
324 vreg->req[1].value != prev_req[1].value) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
326 if (rc) {
327 vreg->req[0].value = prev0;
328 vreg->req[1].value = prev1;
329
David Collins6f032ba2011-08-31 14:08:15 -0700330 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
331 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
333 vreg->req[0].id, rc);
334 } else {
335 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700336 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
337 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 if (msm_rpm_vreg_debug_mask
339 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700340 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341 prev_req[0].value = vreg->req[0].value;
342 prev_req[1].value = vreg->req[1].value;
343 }
344 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700345 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 }
347
348 return rc;
349}
350
351static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
352 int sleep, unsigned mask0, unsigned val0,
353 unsigned mask1, unsigned val1, unsigned cnt,
354 int update_voltage)
355{
David Collins6f032ba2011-08-31 14:08:15 -0700356 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 unsigned long flags;
358 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359
360 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
361 return -EINVAL;
362
David Collins6f032ba2011-08-31 14:08:15 -0700363 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364
365 /*
366 * Send sleep set request first so that subsequent set_mode, etc calls
367 * use the voltage from the active set.
368 */
369 if (sleep)
370 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
371 mask0, val0, mask1, val1, cnt, update_voltage);
372 else {
373 /*
374 * Vote for 0 V in the sleep set when active set-only is
375 * specified. This ensures that a disable vote will be issued
376 * at some point for the sleep set of the regulator.
377 */
David Collins6f032ba2011-08-31 14:08:15 -0700378 if (vreg->part->uV.mask) {
379 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
380 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
381 } else {
382 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
383 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384 }
385
386 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700387 s_mask[0], s_val[0], s_mask[1], s_val[1],
388 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 }
390
391 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
392 mask1, val1, cnt, update_voltage);
393
David Collins6f032ba2011-08-31 14:08:15 -0700394 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
396 return rc;
397}
398
399/**
400 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
401 * @vreg: ID for regulator
402 * @voter: ID for the voter
403 * @min_uV: minimum acceptable voltage (in uV) that is voted for
404 * @max_uV: maximum acceptable voltage (in uV) that is voted for
405 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
406 *
407 * Returns 0 on success or errno.
408 *
409 * This function is used to vote for the voltage of a regulator without
410 * using the regulator framework. It is needed by consumers which hold spin
411 * locks or have interrupts disabled because the regulator framework can sleep.
412 * It is also needed by consumers which wish to only vote for active set
413 * regulator voltage.
414 *
415 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
416 *
417 * This function may only be called for regulators which have the sleep flag
418 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700419 *
420 * Consumers can vote to disable a regulator with this function by passing
421 * min_uV = 0 and max_uV = 0.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 */
David Collins6f032ba2011-08-31 14:08:15 -0700423int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
424 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425{
David Collins6f032ba2011-08-31 14:08:15 -0700426 unsigned int mask[2] = {0}, val[2] = {0};
427 struct vreg_range *range;
428 struct vreg *vreg;
429 int uV = min_uV;
430 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431
David Collins6f032ba2011-08-31 14:08:15 -0700432 /*
433 * HACK: make this function a no-op for 8064 so that it can be called by
434 * consumers on 8064 before RPM capabilities are present. (needed for
435 * acpuclock driver)
436 */
437 if (cpu_is_apq8064())
438 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
David Collins6f032ba2011-08-31 14:08:15 -0700440 if (!config) {
441 pr_err("rpm-regulator driver has not probed yet.\n");
442 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 }
444
David Collins6f032ba2011-08-31 14:08:15 -0700445 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
446 pr_err("invalid regulator id=%d\n", vreg_id);
447 return -EINVAL;
448 }
449
450 vreg = &config->vregs[vreg_id];
451 range = &vreg->set_points->range[0];
452
453 if (!vreg->pdata.sleep_selectable) {
454 vreg_err(vreg, "regulator is not marked sleep selectable\n");
455 return -EINVAL;
456 }
457
David Collins7462b9d2011-10-11 16:02:17 -0700458 /* Allow min_uV == max_uV == 0 to represent a disable request. */
459 if (min_uV != 0 || max_uV != 0) {
460 /*
461 * Check if request voltage is outside of allowed range. The
462 * regulator core has already checked that constraint range
463 * is inside of the physically allowed range.
464 */
465 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
466 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700467
David Collins7462b9d2011-10-11 16:02:17 -0700468 if (uV < lim_min_uV && max_uV >= lim_min_uV)
469 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700470
David Collins7462b9d2011-10-11 16:02:17 -0700471 if (uV < lim_min_uV || uV > lim_max_uV) {
472 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
473 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
474 lim_max_uV);
475 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700476 }
David Collins6f032ba2011-08-31 14:08:15 -0700477
David Collins7462b9d2011-10-11 16:02:17 -0700478 /* Find the range which uV is inside of. */
479 for (i = vreg->set_points->count - 1; i > 0; i--) {
480 if (uV > vreg->set_points->range[i - 1].max_uV) {
481 range = &vreg->set_points->range[i];
482 break;
483 }
484 }
485
486 /*
487 * Force uV to be an allowed set point and apply a ceiling
488 * function to non-set point values.
489 */
490 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
491 uV = uV * range->step_uV + range->min_uV;
492 }
David Collins6f032ba2011-08-31 14:08:15 -0700493
494 if (vreg->part->uV.mask) {
495 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
496 mask[vreg->part->uV.word] = vreg->part->uV.mask;
497 } else {
498 val[vreg->part->mV.word]
499 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
500 mask[vreg->part->mV.word] = vreg->part->mV.mask;
501 }
502
503 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
504 val[1], vreg->part->request_len, 1);
505 if (rc)
506 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507
508 return rc;
509}
510EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
511
512/**
513 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
514 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700515 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 *
517 * Returns 0 on success or errno.
518 */
David Collins6f032ba2011-08-31 14:08:15 -0700519int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520{
David Collins6f032ba2011-08-31 14:08:15 -0700521 unsigned int mask[2] = {0}, val[2] = {0};
522 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 int rc;
524
David Collins6f032ba2011-08-31 14:08:15 -0700525 /*
526 * HACK: make this function a no-op for 8064 so that it can be called by
527 * consumers on 8064 before RPM capabilities are present.
528 */
529 if (cpu_is_apq8064())
530 return 0;
531
532 if (!config) {
533 pr_err("rpm-regulator driver has not probed yet.\n");
534 return -ENODEV;
535 }
536
537 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
538 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 return -EINVAL;
540 }
541
David Collins6f032ba2011-08-31 14:08:15 -0700542 vreg = &config->vregs[vreg_id];
543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700545 vreg_err(vreg, "invalid frequency=%d\n", freq);
546 return -EINVAL;
547 }
548 if (!vreg->pdata.sleep_selectable) {
549 vreg_err(vreg, "regulator is not marked sleep selectable\n");
550 return -EINVAL;
551 }
552 if (!vreg->part->freq.mask) {
553 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 return -EINVAL;
555 }
556
David Collins6f032ba2011-08-31 14:08:15 -0700557 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
558 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
David Collins6f032ba2011-08-31 14:08:15 -0700560 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
561 val[0], mask[1], val[1], vreg->part->request_len, 0);
562 if (rc)
563 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
565 return rc;
566}
567EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
568
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569static inline int vreg_hpm_min_uA(struct vreg *vreg)
570{
571 return vreg->hpm_min_load;
572}
573
574static inline int vreg_lpm_max_uA(struct vreg *vreg)
575{
576 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
577}
578
David Collins6f032ba2011-08-31 14:08:15 -0700579static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580{
David Collins6f032ba2011-08-31 14:08:15 -0700581 unsigned load_max
582 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
583
584 return (load_uA > load_max ? load_max : load_uA);
585}
586
587static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
588{
589 unsigned load_max
590 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
591 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592}
593
594/* Change vreg->req, but do not send it to the RPM. */
595static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
596 unsigned mask1, unsigned val1)
597{
598 unsigned long flags = 0;
599
David Collins6f032ba2011-08-31 14:08:15 -0700600 if (vreg->pdata.sleep_selectable)
601 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 vreg->req[0].value &= ~mask0;
604 vreg->req[0].value |= val0 & mask0;
605
606 vreg->req[1].value &= ~mask1;
607 vreg->req[1].value |= val1 & mask1;
608
David Collins6f032ba2011-08-31 14:08:15 -0700609 if (vreg->pdata.sleep_selectable)
610 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611
612 return 0;
613}
614
615static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
616 unsigned mask1, unsigned val1, unsigned cnt)
617{
618 unsigned prev0 = 0, prev1 = 0;
619 int rc;
620
621 /*
622 * Bypass the normal route for regulators that can be called to change
623 * just the active set values.
624 */
David Collins6f032ba2011-08-31 14:08:15 -0700625 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
627 mask0, val0, mask1, val1, cnt, 1);
628
629 prev0 = vreg->req[0].value;
630 vreg->req[0].value &= ~mask0;
631 vreg->req[0].value |= val0 & mask0;
632
633 prev1 = vreg->req[1].value;
634 vreg->req[1].value &= ~mask1;
635 vreg->req[1].value |= val1 & mask1;
636
637 /* Ignore duplicate requests */
638 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
639 vreg->req[1].value == vreg->prev_active_req[1].value) {
640 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700641 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 return 0;
643 }
644
645 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
646 if (rc) {
647 vreg->req[0].value = prev0;
648 vreg->req[1].value = prev1;
649
David Collins6f032ba2011-08-31 14:08:15 -0700650 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
651 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 } else {
653 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700654 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 vreg->prev_active_req[0].value = vreg->req[0].value;
656 vreg->prev_active_req[1].value = vreg->req[1].value;
657 }
658
659 return rc;
660}
661
David Collins6f032ba2011-08-31 14:08:15 -0700662static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663{
David Collins6f032ba2011-08-31 14:08:15 -0700664 struct vreg *vreg = rdev_get_drvdata(rdev);
665 int enabled;
666
667 mutex_lock(&vreg->pc_lock);
668 enabled = vreg->is_enabled;
669 mutex_unlock(&vreg->pc_lock);
670
671 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672}
673
David Collins6f032ba2011-08-31 14:08:15 -0700674static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675{
David Collins6f032ba2011-08-31 14:08:15 -0700676 switch (vreg->type) {
677 case RPM_REGULATOR_TYPE_LDO:
678 case RPM_REGULATOR_TYPE_SMPS:
679 /* Enable by setting a voltage. */
680 if (vreg->part->uV.mask) {
681 val[vreg->part->uV.word]
682 |= vreg->save_uV << vreg->part->uV.shift;
683 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
684 } else {
685 val[vreg->part->mV.word]
686 |= MICRO_TO_MILLI(vreg->save_uV)
687 << vreg->part->mV.shift;
688 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
689 }
690 break;
691 case RPM_REGULATOR_TYPE_VS:
692 case RPM_REGULATOR_TYPE_NCP:
693 /* Enable by setting enable_state. */
694 val[vreg->part->enable_state.word]
695 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
696 mask[vreg->part->enable_state.word]
697 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699}
700
David Collins6f032ba2011-08-31 14:08:15 -0700701static int vreg_enable(struct regulator_dev *rdev)
702{
703 struct vreg *vreg = rdev_get_drvdata(rdev);
704 unsigned int mask[2] = {0}, val[2] = {0};
705 int rc = 0;
706
707 set_enable(vreg, mask, val);
708
709 mutex_lock(&vreg->pc_lock);
710
711 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
712 vreg->part->request_len);
713 if (!rc)
714 vreg->is_enabled = true;
715
716 mutex_unlock(&vreg->pc_lock);
717
718 if (rc)
719 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
720
721 return rc;
722}
723
724static void set_disable(struct vreg *vreg, unsigned int *mask,
725 unsigned int *val)
726{
727 switch (vreg->type) {
728 case RPM_REGULATOR_TYPE_LDO:
729 case RPM_REGULATOR_TYPE_SMPS:
730 /* Disable by setting a voltage of 0 uV. */
731 if (vreg->part->uV.mask) {
732 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
733 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
734 } else {
735 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
736 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
737 }
738 break;
739 case RPM_REGULATOR_TYPE_VS:
740 case RPM_REGULATOR_TYPE_NCP:
741 /* Disable by setting enable_state. */
742 val[vreg->part->enable_state.word]
743 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
744 mask[vreg->part->enable_state.word]
745 |= vreg->part->enable_state.mask;
746 }
747}
748
749static int vreg_disable(struct regulator_dev *rdev)
750{
751 struct vreg *vreg = rdev_get_drvdata(rdev);
752 unsigned int mask[2] = {0}, val[2] = {0};
753 int rc = 0;
754
755 set_disable(vreg, mask, val);
756
757 mutex_lock(&vreg->pc_lock);
758
759 /* Only disable if pin control is not in use. */
760 if (!vreg->is_enabled_pc)
761 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
762 vreg->part->request_len);
763
764 if (!rc)
765 vreg->is_enabled = false;
766
767 mutex_unlock(&vreg->pc_lock);
768
769 if (rc)
770 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
771
772 return rc;
773}
774
775static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 unsigned *selector)
777{
David Collins6f032ba2011-08-31 14:08:15 -0700778 struct vreg *vreg = rdev_get_drvdata(rdev);
779 struct vreg_range *range = &vreg->set_points->range[0];
780 unsigned int mask[2] = {0}, val[2] = {0};
781 int rc = 0, uV = min_uV;
782 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783
David Collins6f032ba2011-08-31 14:08:15 -0700784 /* Check if request voltage is outside of physically settable range. */
785 lim_min_uV = vreg->set_points->range[0].min_uV;
786 lim_max_uV =
787 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788
David Collins6f032ba2011-08-31 14:08:15 -0700789 if (uV < lim_min_uV && max_uV >= lim_min_uV)
790 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791
David Collins6f032ba2011-08-31 14:08:15 -0700792 if (uV < lim_min_uV || uV > lim_max_uV) {
793 vreg_err(vreg,
794 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
795 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 return -EINVAL;
797 }
798
David Collins6f032ba2011-08-31 14:08:15 -0700799 /* Find the range which uV is inside of. */
800 for (i = vreg->set_points->count - 1; i > 0; i--) {
801 if (uV > vreg->set_points->range[i - 1].max_uV) {
802 range = &vreg->set_points->range[i];
803 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804 }
805 }
806
David Collins6f032ba2011-08-31 14:08:15 -0700807 /*
808 * Force uV to be an allowed set point and apply a ceiling function
809 * to non-set point values.
810 */
811 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
812 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
David Collins6f032ba2011-08-31 14:08:15 -0700814 if (vreg->part->uV.mask) {
815 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
816 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700818 val[vreg->part->mV.word]
819 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
820 mask[vreg->part->mV.word] = vreg->part->mV.mask;
821 }
822
823 mutex_lock(&vreg->pc_lock);
824
825 /*
826 * Only send a request for a new voltage if the regulator is currently
827 * enabled. This will ensure that LDO and SMPS regulators are not
828 * inadvertently turned on because voltage > 0 is equivalent to
829 * enabling. For NCP, this just removes unnecessary RPM requests.
830 */
831 if (vreg->is_enabled) {
832 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
833 vreg->part->request_len);
834 if (rc)
835 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
836 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700838 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839 }
840
David Collins6f032ba2011-08-31 14:08:15 -0700841 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
842 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843
David Collins6f032ba2011-08-31 14:08:15 -0700844 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845
846 return rc;
847}
848
David Collins6f032ba2011-08-31 14:08:15 -0700849static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850{
David Collins6f032ba2011-08-31 14:08:15 -0700851 struct vreg *vreg = rdev_get_drvdata(rdev);
852
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853 return vreg->save_uV;
854}
855
David Collins6f032ba2011-08-31 14:08:15 -0700856static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857{
David Collins6f032ba2011-08-31 14:08:15 -0700858 struct vreg *vreg = rdev_get_drvdata(rdev);
859 int uV = 0;
860 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861
David Collins6f032ba2011-08-31 14:08:15 -0700862 if (!vreg->set_points) {
863 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 return -EINVAL;
865 }
866
David Collins6f032ba2011-08-31 14:08:15 -0700867 if (selector >= vreg->set_points->n_voltages)
868 return 0;
869
870 for (i = 0; i < vreg->set_points->count; i++) {
871 if (selector < vreg->set_points->range[i].n_voltages) {
872 uV = selector * vreg->set_points->range[i].step_uV
873 + vreg->set_points->range[i].min_uV;
874 break;
875 } else {
876 selector -= vreg->set_points->range[i].n_voltages;
877 }
878 }
879
880 return uV;
881}
882
883static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
884{
885 struct vreg *vreg = rdev_get_drvdata(rdev);
886 unsigned int mask[2] = {0}, val[2] = {0};
887 int rc = 0;
888 int peak_uA;
889
890 mutex_lock(&vreg->pc_lock);
891
892 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
893 & vreg->part->ip.mask) >> vreg->part->ip.shift);
894
895 if (mode == config->mode_hpm) {
896 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700898 val[vreg->part->ip.word]
899 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
900 << vreg->part->ip.shift;
901 mask[vreg->part->ip.word] = vreg->part->ip.mask;
902
903 if (config->ia_follows_ip) {
904 val[vreg->part->ia.word]
905 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
906 << vreg->part->ia.shift;
907 mask[vreg->part->ia.word]
908 |= vreg->part->ia.mask;
909 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 }
David Collins6f032ba2011-08-31 14:08:15 -0700911 } else if (mode == config->mode_lpm) {
912 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700914 val[vreg->part->ip.word]
915 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
916 << vreg->part->ip.shift;
917 mask[vreg->part->ip.word] = vreg->part->ip.mask;
918
919 if (config->ia_follows_ip) {
920 val[vreg->part->ia.word]
921 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
922 << vreg->part->ia.shift;
923 mask[vreg->part->ia.word]
924 |= vreg->part->ia.mask;
925 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 }
David Collins6f032ba2011-08-31 14:08:15 -0700927 } else {
928 vreg_err(vreg, "invalid mode: %u\n", mode);
929 mutex_unlock(&vreg->pc_lock);
930 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931 }
932
David Collins6f032ba2011-08-31 14:08:15 -0700933 if (vreg->is_enabled) {
934 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
935 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 } else {
937 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700938 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 }
David Collins6f032ba2011-08-31 14:08:15 -0700940
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -0700942 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
943 else
944 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945
David Collins6f032ba2011-08-31 14:08:15 -0700946 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947
David Collins6f032ba2011-08-31 14:08:15 -0700948 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949}
950
David Collins6f032ba2011-08-31 14:08:15 -0700951static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952{
David Collins6f032ba2011-08-31 14:08:15 -0700953 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954
David Collins6f032ba2011-08-31 14:08:15 -0700955 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956}
957
David Collins6f032ba2011-08-31 14:08:15 -0700958static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
959 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960{
David Collins6f032ba2011-08-31 14:08:15 -0700961 struct vreg *vreg = rdev_get_drvdata(rdev);
962 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963
David Collins6f032ba2011-08-31 14:08:15 -0700964 load_uA += vreg->pdata.system_uA;
965
966 mutex_lock(&vreg->pc_lock);
967 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
968 if (config->ia_follows_ip)
969 SET_PART(vreg, ia,
970 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
971 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972
973 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -0700974 mode = config->mode_hpm;
975 else
976 mode = config->mode_lpm;
977
978 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979}
980
David Collins6f032ba2011-08-31 14:08:15 -0700981static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
982 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983{
David Collins6f032ba2011-08-31 14:08:15 -0700984 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985
David Collins6f032ba2011-08-31 14:08:15 -0700986 if (MICRO_TO_MILLI(load_uA) <= 0) {
987 /*
988 * vreg_legacy_get_optimum_mode is being called before consumers
989 * have specified their load currents via
990 * regulator_set_optimum_mode. Return whatever the existing mode
991 * is.
992 */
993 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 }
995
David Collins6f032ba2011-08-31 14:08:15 -0700996 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997}
998
999/*
David Collins6f032ba2011-08-31 14:08:15 -07001000 * Returns the logical pin control enable state because the pin control options
1001 * present in the hardware out of restart could be different from those desired
1002 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003 */
David Collins6f032ba2011-08-31 14:08:15 -07001004static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005{
David Collins6f032ba2011-08-31 14:08:15 -07001006 struct vreg *vreg = rdev_get_drvdata(rdev);
1007
1008 return vreg->is_enabled_pc;
1009}
1010
1011static int vreg_pin_control_enable(struct regulator_dev *rdev)
1012{
1013 struct vreg *vreg = rdev_get_drvdata(rdev);
1014 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 int rc;
1016
David Collins6f032ba2011-08-31 14:08:15 -07001017 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018
David Collins6f032ba2011-08-31 14:08:15 -07001019 val[vreg->part->pc.word]
1020 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1021 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022
David Collins6f032ba2011-08-31 14:08:15 -07001023 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1024 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
David Collins6f032ba2011-08-31 14:08:15 -07001026 if (!vreg->is_enabled)
1027 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028
David Collins6f032ba2011-08-31 14:08:15 -07001029 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1030 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031
David Collins6f032ba2011-08-31 14:08:15 -07001032 if (!rc)
1033 vreg->is_enabled_pc = true;
1034
1035 mutex_unlock(&vreg->pc_lock);
1036
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001038 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039
David Collins6f032ba2011-08-31 14:08:15 -07001040 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041}
1042
David Collins6f032ba2011-08-31 14:08:15 -07001043static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044{
David Collins6f032ba2011-08-31 14:08:15 -07001045 struct vreg *vreg = rdev_get_drvdata(rdev);
1046 unsigned int mask[2] = {0}, val[2] = {0};
1047 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048
David Collins6f032ba2011-08-31 14:08:15 -07001049 mutex_lock(&vreg->pc_lock);
1050
1051 val[vreg->part->pc.word]
1052 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1053 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1054
1055 pin_fn = config->pin_func_none;
1056 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1057 pin_fn = config->pin_func_sleep_b;
1058 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1059 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1060
1061 if (!vreg->is_enabled)
1062 set_disable(vreg, mask, val);
1063
1064 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1065 vreg->part->request_len);
1066
1067 if (!rc)
1068 vreg->is_enabled_pc = false;
1069
1070 mutex_unlock(&vreg->pc_lock);
1071
1072 if (rc)
1073 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1074
1075 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076}
1077
David Collins6f032ba2011-08-31 14:08:15 -07001078static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001079{
David Collins6f032ba2011-08-31 14:08:15 -07001080 struct vreg *vreg = rdev_get_drvdata(rdev);
1081
1082 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083}
1084
David Collins6f032ba2011-08-31 14:08:15 -07001085/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086static struct regulator_ops ldo_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001087 .enable = vreg_enable,
1088 .disable = vreg_disable,
1089 .is_enabled = vreg_is_enabled,
1090 .set_voltage = vreg_set_voltage,
1091 .get_voltage = vreg_get_voltage,
1092 .list_voltage = vreg_list_voltage,
1093 .set_mode = vreg_set_mode,
1094 .get_mode = vreg_get_mode,
1095 .get_optimum_mode = vreg_get_optimum_mode,
1096 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097};
1098
1099static struct regulator_ops smps_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001100 .enable = vreg_enable,
1101 .disable = vreg_disable,
1102 .is_enabled = vreg_is_enabled,
1103 .set_voltage = vreg_set_voltage,
1104 .get_voltage = vreg_get_voltage,
1105 .list_voltage = vreg_list_voltage,
1106 .set_mode = vreg_set_mode,
1107 .get_mode = vreg_get_mode,
1108 .get_optimum_mode = vreg_get_optimum_mode,
1109 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110};
1111
1112static struct regulator_ops switch_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001113 .enable = vreg_enable,
1114 .disable = vreg_disable,
1115 .is_enabled = vreg_is_enabled,
1116 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117};
1118
1119static struct regulator_ops ncp_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001120 .enable = vreg_enable,
1121 .disable = vreg_disable,
1122 .is_enabled = vreg_is_enabled,
1123 .set_voltage = vreg_set_voltage,
1124 .get_voltage = vreg_get_voltage,
1125 .list_voltage = vreg_list_voltage,
1126 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127};
1128
David Collins6f032ba2011-08-31 14:08:15 -07001129/* Pin control regulator operations. */
1130static struct regulator_ops pin_control_ops = {
1131 .enable = vreg_pin_control_enable,
1132 .disable = vreg_pin_control_disable,
1133 .is_enabled = vreg_pin_control_is_enabled,
1134};
1135
1136struct regulator_ops *vreg_ops[] = {
1137 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1138 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1139 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1140 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1141};
1142
1143static int __devinit
1144rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1145 struct device *dev)
1146{
1147 struct regulator_desc *rdesc = NULL;
1148 struct regulator_dev *rdev;
1149 struct vreg *vreg;
1150 unsigned pin_ctrl;
1151 int id, pin_fn;
1152 int rc = 0;
1153
1154 if (!pdata) {
1155 pr_err("platform data missing\n");
1156 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 }
1158
David Collins6f032ba2011-08-31 14:08:15 -07001159 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160
David Collins6f032ba2011-08-31 14:08:15 -07001161 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1162 pr_err("invalid regulator id: %d\n", id);
1163 return -ENODEV;
1164 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165
David Collins6f032ba2011-08-31 14:08:15 -07001166 if (!config->is_real_id(pdata->id))
1167 id = config->pc_id_to_real_id(pdata->id);
1168 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169
David Collins6f032ba2011-08-31 14:08:15 -07001170 if (config->is_real_id(pdata->id))
1171 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172 else
David Collins6f032ba2011-08-31 14:08:15 -07001173 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174
David Collins6f032ba2011-08-31 14:08:15 -07001175 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1176 pr_err("%s: invalid regulator type: %d\n",
1177 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001179 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180
David Collins6f032ba2011-08-31 14:08:15 -07001181 mutex_lock(&vreg->pc_lock);
1182
1183 if (vreg->set_points)
1184 rdesc->n_voltages = vreg->set_points->n_voltages;
1185 else
1186 rdesc->n_voltages = 0;
1187
1188 rdesc->id = pdata->id;
1189 rdesc->owner = THIS_MODULE;
1190 rdesc->type = REGULATOR_VOLTAGE;
1191
1192 if (config->is_real_id(pdata->id)) {
1193 /*
1194 * Real regulator; do not modify pin control and pin function
1195 * values.
1196 */
1197 rdesc->ops = vreg_ops[vreg->type];
1198 pin_ctrl = vreg->pdata.pin_ctrl;
1199 pin_fn = vreg->pdata.pin_fn;
1200 memcpy(&(vreg->pdata), pdata,
1201 sizeof(struct rpm_regulator_init_data));
1202 vreg->pdata.pin_ctrl = pin_ctrl;
1203 vreg->pdata.pin_fn = pin_fn;
1204
1205 vreg->save_uV = vreg->pdata.default_uV;
1206 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1207 vreg->mode = config->mode_hpm;
1208 else
1209 vreg->mode = config->mode_lpm;
1210
1211 /* Initialize the RPM request. */
1212 SET_PART(vreg, ip,
1213 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1214 SET_PART(vreg, fm, vreg->pdata.force_mode);
1215 SET_PART(vreg, pm, vreg->pdata.power_mode);
1216 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1217 SET_PART(vreg, ia,
1218 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1219 SET_PART(vreg, freq, vreg->pdata.freq);
1220 SET_PART(vreg, freq_clk_src, 0);
1221 SET_PART(vreg, comp_mode, 0);
1222 SET_PART(vreg, hpm, 0);
1223 if (!vreg->is_enabled_pc) {
1224 SET_PART(vreg, pf, config->pin_func_none);
1225 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1226 }
1227 } else {
1228 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1229 == RPM_VREG_PIN_CTRL_NONE
1230 && pdata->pin_fn != config->pin_func_sleep_b) {
1231 pr_err("%s: no pin control input specified\n",
1232 vreg->rdesc.name);
1233 mutex_unlock(&vreg->pc_lock);
1234 return -EINVAL;
1235 }
1236 rdesc->ops = &pin_control_ops;
1237 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1238 vreg->pdata.pin_fn = pdata->pin_fn;
1239
1240 /* Initialize the RPM request. */
1241 pin_fn = config->pin_func_none;
1242 /* Allow pf=sleep_b to be specified by platform data. */
1243 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1244 pin_fn = config->pin_func_sleep_b;
1245 SET_PART(vreg, pf, pin_fn);
1246 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1247 }
1248
1249 mutex_unlock(&vreg->pc_lock);
1250
1251 if (rc)
1252 goto bail;
1253
1254 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1255 if (IS_ERR(rdev)) {
1256 rc = PTR_ERR(rdev);
1257 pr_err("regulator_register failed: %s, rc=%d\n",
1258 vreg->rdesc.name, rc);
1259 return rc;
1260 } else {
1261 if (config->is_real_id(pdata->id))
1262 vreg->rdev = rdev;
1263 else
1264 vreg->rdev_pc = rdev;
1265 }
1266
1267bail:
1268 if (rc)
1269 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1270
1271 return rc;
1272}
1273
1274static void rpm_vreg_set_point_init(void)
1275{
1276 struct vreg_set_points **set_points;
1277 int i, j, temp;
1278
1279 set_points = config->set_points;
1280
1281 /* Calculate the number of set points available for each regulator. */
1282 for (i = 0; i < config->set_points_len; i++) {
1283 temp = 0;
1284 for (j = 0; j < set_points[i]->count; j++) {
1285 set_points[i]->range[j].n_voltages
1286 = (set_points[i]->range[j].max_uV
1287 - set_points[i]->range[j].min_uV)
1288 / set_points[i]->range[j].step_uV + 1;
1289 temp += set_points[i]->range[j].n_voltages;
1290 }
1291 set_points[i]->n_voltages = temp;
1292 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293}
1294
1295static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1296{
David Collins6f032ba2011-08-31 14:08:15 -07001297 struct rpm_regulator_platform_data *platform_data;
1298 int rc = 0;
1299 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300
David Collins6f032ba2011-08-31 14:08:15 -07001301 platform_data = pdev->dev.platform_data;
1302 if (!platform_data) {
1303 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305 }
1306
David Collins6f032ba2011-08-31 14:08:15 -07001307 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1308 && platform_data->version != rpm_version) {
1309 pr_err("rpm version %d does not match previous version %d\n",
1310 platform_data->version, rpm_version);
1311 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 }
1313
David Collins6f032ba2011-08-31 14:08:15 -07001314 if (platform_data->version < 0
1315 || platform_data->version > RPM_VREG_VERSION_MAX) {
1316 pr_err("rpm version %d is invalid\n", platform_data->version);
1317 return -EINVAL;
1318 }
1319
1320 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1321 rpm_version = platform_data->version;
1322 config = get_config[platform_data->version]();
1323 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1324 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1325 if (!config) {
1326 pr_err("rpm version %d is not available\n",
1327 platform_data->version);
1328 return -ENODEV;
1329 }
1330 if (config->use_legacy_optimum_mode)
1331 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1332 vreg_ops[i]->get_optimum_mode
1333 = vreg_legacy_get_optimum_mode;
1334 rpm_vreg_set_point_init();
1335 /* First time probed; initialize pin control mutexes. */
1336 for (i = 0; i < config->vregs_len; i++)
1337 mutex_init(&config->vregs[i].pc_lock);
1338 }
1339
1340 /* Initialize all of the regulators listed in the platform data. */
1341 for (i = 0; i < platform_data->num_regulators; i++) {
1342 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1343 &pdev->dev);
1344 if (rc) {
1345 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1346 goto remove_regulators;
1347 }
1348 }
1349
1350 platform_set_drvdata(pdev, platform_data);
1351
1352 return rc;
1353
1354remove_regulators:
1355 /* Unregister all regulators added before the erroring one. */
1356 for (; i >= 0; i--) {
1357 id = platform_data->init_data[i].id;
1358 if (config->is_real_id(id)) {
1359 regulator_unregister(config->vregs[id].rdev);
1360 config->vregs[id].rdev = NULL;
1361 } else {
1362 regulator_unregister(config->vregs[
1363 config->pc_id_to_real_id(id)].rdev_pc);
1364 config->vregs[id].rdev_pc = NULL;
1365 }
1366 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367
1368 return rc;
1369}
1370
1371static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1372{
David Collins6f032ba2011-08-31 14:08:15 -07001373 struct rpm_regulator_platform_data *platform_data;
1374 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375
David Collins6f032ba2011-08-31 14:08:15 -07001376 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001378
1379 if (platform_data) {
1380 for (i = 0; i < platform_data->num_regulators; i++) {
1381 id = platform_data->init_data[i].id;
1382 if (config->is_real_id(id)) {
1383 regulator_unregister(config->vregs[id].rdev);
1384 config->vregs[id].rdev = NULL;
1385 } else {
1386 regulator_unregister(config->vregs[
1387 config->pc_id_to_real_id(id)].rdev_pc);
1388 config->vregs[id].rdev_pc = NULL;
1389 }
1390 }
1391 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392
1393 return 0;
1394}
1395
1396static struct platform_driver rpm_vreg_driver = {
1397 .probe = rpm_vreg_probe,
1398 .remove = __devexit_p(rpm_vreg_remove),
1399 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001400 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001401 .owner = THIS_MODULE,
1402 },
1403};
1404
1405static int __init rpm_vreg_init(void)
1406{
1407 return platform_driver_register(&rpm_vreg_driver);
1408}
1409
1410static void __exit rpm_vreg_exit(void)
1411{
David Collins6f032ba2011-08-31 14:08:15 -07001412 int i;
1413
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001415
1416 for (i = 0; i < config->vregs_len; i++)
1417 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418}
1419
1420postcore_initcall(rpm_vreg_init);
1421module_exit(rpm_vreg_exit);
1422
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001424MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001425MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001426MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);