blob: 249496924cef87716f6f5b04555293b9bcfa39e4 [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.
419 */
David Collins6f032ba2011-08-31 14:08:15 -0700420int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
421 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422{
David Collins6f032ba2011-08-31 14:08:15 -0700423 unsigned int mask[2] = {0}, val[2] = {0};
424 struct vreg_range *range;
425 struct vreg *vreg;
426 int uV = min_uV;
427 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428
David Collins6f032ba2011-08-31 14:08:15 -0700429 /*
430 * HACK: make this function a no-op for 8064 so that it can be called by
431 * consumers on 8064 before RPM capabilities are present. (needed for
432 * acpuclock driver)
433 */
434 if (cpu_is_apq8064())
435 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436
David Collins6f032ba2011-08-31 14:08:15 -0700437 if (!config) {
438 pr_err("rpm-regulator driver has not probed yet.\n");
439 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 }
441
David Collins6f032ba2011-08-31 14:08:15 -0700442 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
443 pr_err("invalid regulator id=%d\n", vreg_id);
444 return -EINVAL;
445 }
446
447 vreg = &config->vregs[vreg_id];
448 range = &vreg->set_points->range[0];
449
450 if (!vreg->pdata.sleep_selectable) {
451 vreg_err(vreg, "regulator is not marked sleep selectable\n");
452 return -EINVAL;
453 }
454
455 /*
456 * Check if request voltage is outside of allowed range. The regulator
457 * core has already checked that constraint range is inside of the
458 * physically allowed range.
459 */
460 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
461 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
462
463 if (uV < lim_min_uV && max_uV >= lim_min_uV)
464 uV = lim_min_uV;
465
466 if (uV < lim_min_uV || uV > lim_max_uV) {
467 vreg_err(vreg,
468 "request v=[%d, %d] is outside allowed v=[%d, %d]\n",
469 min_uV, max_uV, lim_min_uV, lim_max_uV);
470 return -EINVAL;
471 }
472
473 /* Find the range which uV is inside of. */
474 for (i = vreg->set_points->count - 1; i > 0; i--) {
475 if (uV > vreg->set_points->range[i - 1].max_uV) {
476 range = &vreg->set_points->range[i];
477 break;
478 }
479 }
480
481 /*
482 * Force uV to be an allowed set point and apply a ceiling function
483 * to non-set point values.
484 */
485 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
486 uV = uV * range->step_uV + range->min_uV;
487
488 if (vreg->part->uV.mask) {
489 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
490 mask[vreg->part->uV.word] = vreg->part->uV.mask;
491 } else {
492 val[vreg->part->mV.word]
493 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
494 mask[vreg->part->mV.word] = vreg->part->mV.mask;
495 }
496
497 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
498 val[1], vreg->part->request_len, 1);
499 if (rc)
500 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501
502 return rc;
503}
504EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
505
506/**
507 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
508 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700509 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 *
511 * Returns 0 on success or errno.
512 */
David Collins6f032ba2011-08-31 14:08:15 -0700513int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514{
David Collins6f032ba2011-08-31 14:08:15 -0700515 unsigned int mask[2] = {0}, val[2] = {0};
516 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 int rc;
518
David Collins6f032ba2011-08-31 14:08:15 -0700519 /*
520 * HACK: make this function a no-op for 8064 so that it can be called by
521 * consumers on 8064 before RPM capabilities are present.
522 */
523 if (cpu_is_apq8064())
524 return 0;
525
526 if (!config) {
527 pr_err("rpm-regulator driver has not probed yet.\n");
528 return -ENODEV;
529 }
530
531 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
532 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 return -EINVAL;
534 }
535
David Collins6f032ba2011-08-31 14:08:15 -0700536 vreg = &config->vregs[vreg_id];
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700539 vreg_err(vreg, "invalid frequency=%d\n", freq);
540 return -EINVAL;
541 }
542 if (!vreg->pdata.sleep_selectable) {
543 vreg_err(vreg, "regulator is not marked sleep selectable\n");
544 return -EINVAL;
545 }
546 if (!vreg->part->freq.mask) {
547 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548 return -EINVAL;
549 }
550
David Collins6f032ba2011-08-31 14:08:15 -0700551 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
552 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553
David Collins6f032ba2011-08-31 14:08:15 -0700554 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
555 val[0], mask[1], val[1], vreg->part->request_len, 0);
556 if (rc)
557 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558
559 return rc;
560}
561EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
562
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563static inline int vreg_hpm_min_uA(struct vreg *vreg)
564{
565 return vreg->hpm_min_load;
566}
567
568static inline int vreg_lpm_max_uA(struct vreg *vreg)
569{
570 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
571}
572
David Collins6f032ba2011-08-31 14:08:15 -0700573static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574{
David Collins6f032ba2011-08-31 14:08:15 -0700575 unsigned load_max
576 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
577
578 return (load_uA > load_max ? load_max : load_uA);
579}
580
581static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
582{
583 unsigned load_max
584 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
585 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586}
587
588/* Change vreg->req, but do not send it to the RPM. */
589static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
590 unsigned mask1, unsigned val1)
591{
592 unsigned long flags = 0;
593
David Collins6f032ba2011-08-31 14:08:15 -0700594 if (vreg->pdata.sleep_selectable)
595 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596
597 vreg->req[0].value &= ~mask0;
598 vreg->req[0].value |= val0 & mask0;
599
600 vreg->req[1].value &= ~mask1;
601 vreg->req[1].value |= val1 & mask1;
602
David Collins6f032ba2011-08-31 14:08:15 -0700603 if (vreg->pdata.sleep_selectable)
604 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
606 return 0;
607}
608
609static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
610 unsigned mask1, unsigned val1, unsigned cnt)
611{
612 unsigned prev0 = 0, prev1 = 0;
613 int rc;
614
615 /*
616 * Bypass the normal route for regulators that can be called to change
617 * just the active set values.
618 */
David Collins6f032ba2011-08-31 14:08:15 -0700619 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
621 mask0, val0, mask1, val1, cnt, 1);
622
623 prev0 = vreg->req[0].value;
624 vreg->req[0].value &= ~mask0;
625 vreg->req[0].value |= val0 & mask0;
626
627 prev1 = vreg->req[1].value;
628 vreg->req[1].value &= ~mask1;
629 vreg->req[1].value |= val1 & mask1;
630
631 /* Ignore duplicate requests */
632 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
633 vreg->req[1].value == vreg->prev_active_req[1].value) {
634 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700635 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 return 0;
637 }
638
639 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
640 if (rc) {
641 vreg->req[0].value = prev0;
642 vreg->req[1].value = prev1;
643
David Collins6f032ba2011-08-31 14:08:15 -0700644 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
645 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 } else {
647 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700648 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 vreg->prev_active_req[0].value = vreg->req[0].value;
650 vreg->prev_active_req[1].value = vreg->req[1].value;
651 }
652
653 return rc;
654}
655
David Collins6f032ba2011-08-31 14:08:15 -0700656static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657{
David Collins6f032ba2011-08-31 14:08:15 -0700658 struct vreg *vreg = rdev_get_drvdata(rdev);
659 int enabled;
660
661 mutex_lock(&vreg->pc_lock);
662 enabled = vreg->is_enabled;
663 mutex_unlock(&vreg->pc_lock);
664
665 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666}
667
David Collins6f032ba2011-08-31 14:08:15 -0700668static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669{
David Collins6f032ba2011-08-31 14:08:15 -0700670 switch (vreg->type) {
671 case RPM_REGULATOR_TYPE_LDO:
672 case RPM_REGULATOR_TYPE_SMPS:
673 /* Enable by setting a voltage. */
674 if (vreg->part->uV.mask) {
675 val[vreg->part->uV.word]
676 |= vreg->save_uV << vreg->part->uV.shift;
677 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
678 } else {
679 val[vreg->part->mV.word]
680 |= MICRO_TO_MILLI(vreg->save_uV)
681 << vreg->part->mV.shift;
682 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
683 }
684 break;
685 case RPM_REGULATOR_TYPE_VS:
686 case RPM_REGULATOR_TYPE_NCP:
687 /* Enable by setting enable_state. */
688 val[vreg->part->enable_state.word]
689 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
690 mask[vreg->part->enable_state.word]
691 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693}
694
David Collins6f032ba2011-08-31 14:08:15 -0700695static int vreg_enable(struct regulator_dev *rdev)
696{
697 struct vreg *vreg = rdev_get_drvdata(rdev);
698 unsigned int mask[2] = {0}, val[2] = {0};
699 int rc = 0;
700
701 set_enable(vreg, mask, val);
702
703 mutex_lock(&vreg->pc_lock);
704
705 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
706 vreg->part->request_len);
707 if (!rc)
708 vreg->is_enabled = true;
709
710 mutex_unlock(&vreg->pc_lock);
711
712 if (rc)
713 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
714
715 return rc;
716}
717
718static void set_disable(struct vreg *vreg, unsigned int *mask,
719 unsigned int *val)
720{
721 switch (vreg->type) {
722 case RPM_REGULATOR_TYPE_LDO:
723 case RPM_REGULATOR_TYPE_SMPS:
724 /* Disable by setting a voltage of 0 uV. */
725 if (vreg->part->uV.mask) {
726 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
727 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
728 } else {
729 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
730 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
731 }
732 break;
733 case RPM_REGULATOR_TYPE_VS:
734 case RPM_REGULATOR_TYPE_NCP:
735 /* Disable by setting enable_state. */
736 val[vreg->part->enable_state.word]
737 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
738 mask[vreg->part->enable_state.word]
739 |= vreg->part->enable_state.mask;
740 }
741}
742
743static int vreg_disable(struct regulator_dev *rdev)
744{
745 struct vreg *vreg = rdev_get_drvdata(rdev);
746 unsigned int mask[2] = {0}, val[2] = {0};
747 int rc = 0;
748
749 set_disable(vreg, mask, val);
750
751 mutex_lock(&vreg->pc_lock);
752
753 /* Only disable if pin control is not in use. */
754 if (!vreg->is_enabled_pc)
755 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
756 vreg->part->request_len);
757
758 if (!rc)
759 vreg->is_enabled = false;
760
761 mutex_unlock(&vreg->pc_lock);
762
763 if (rc)
764 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
765
766 return rc;
767}
768
769static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 unsigned *selector)
771{
David Collins6f032ba2011-08-31 14:08:15 -0700772 struct vreg *vreg = rdev_get_drvdata(rdev);
773 struct vreg_range *range = &vreg->set_points->range[0];
774 unsigned int mask[2] = {0}, val[2] = {0};
775 int rc = 0, uV = min_uV;
776 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777
David Collins6f032ba2011-08-31 14:08:15 -0700778 /* Check if request voltage is outside of physically settable range. */
779 lim_min_uV = vreg->set_points->range[0].min_uV;
780 lim_max_uV =
781 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782
David Collins6f032ba2011-08-31 14:08:15 -0700783 if (uV < lim_min_uV && max_uV >= lim_min_uV)
784 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785
David Collins6f032ba2011-08-31 14:08:15 -0700786 if (uV < lim_min_uV || uV > lim_max_uV) {
787 vreg_err(vreg,
788 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
789 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790 return -EINVAL;
791 }
792
David Collins6f032ba2011-08-31 14:08:15 -0700793 /* Find the range which uV is inside of. */
794 for (i = vreg->set_points->count - 1; i > 0; i--) {
795 if (uV > vreg->set_points->range[i - 1].max_uV) {
796 range = &vreg->set_points->range[i];
797 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 }
799 }
800
David Collins6f032ba2011-08-31 14:08:15 -0700801 /*
802 * Force uV to be an allowed set point and apply a ceiling function
803 * to non-set point values.
804 */
805 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
806 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807
David Collins6f032ba2011-08-31 14:08:15 -0700808 if (vreg->part->uV.mask) {
809 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
810 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700812 val[vreg->part->mV.word]
813 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
814 mask[vreg->part->mV.word] = vreg->part->mV.mask;
815 }
816
817 mutex_lock(&vreg->pc_lock);
818
819 /*
820 * Only send a request for a new voltage if the regulator is currently
821 * enabled. This will ensure that LDO and SMPS regulators are not
822 * inadvertently turned on because voltage > 0 is equivalent to
823 * enabling. For NCP, this just removes unnecessary RPM requests.
824 */
825 if (vreg->is_enabled) {
826 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
827 vreg->part->request_len);
828 if (rc)
829 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
830 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700832 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 }
834
David Collins6f032ba2011-08-31 14:08:15 -0700835 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
836 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837
David Collins6f032ba2011-08-31 14:08:15 -0700838 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839
840 return rc;
841}
842
David Collins6f032ba2011-08-31 14:08:15 -0700843static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844{
David Collins6f032ba2011-08-31 14:08:15 -0700845 struct vreg *vreg = rdev_get_drvdata(rdev);
846
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 return vreg->save_uV;
848}
849
David Collins6f032ba2011-08-31 14:08:15 -0700850static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851{
David Collins6f032ba2011-08-31 14:08:15 -0700852 struct vreg *vreg = rdev_get_drvdata(rdev);
853 int uV = 0;
854 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855
David Collins6f032ba2011-08-31 14:08:15 -0700856 if (!vreg->set_points) {
857 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 return -EINVAL;
859 }
860
David Collins6f032ba2011-08-31 14:08:15 -0700861 if (selector >= vreg->set_points->n_voltages)
862 return 0;
863
864 for (i = 0; i < vreg->set_points->count; i++) {
865 if (selector < vreg->set_points->range[i].n_voltages) {
866 uV = selector * vreg->set_points->range[i].step_uV
867 + vreg->set_points->range[i].min_uV;
868 break;
869 } else {
870 selector -= vreg->set_points->range[i].n_voltages;
871 }
872 }
873
874 return uV;
875}
876
877static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
878{
879 struct vreg *vreg = rdev_get_drvdata(rdev);
880 unsigned int mask[2] = {0}, val[2] = {0};
881 int rc = 0;
882 int peak_uA;
883
884 mutex_lock(&vreg->pc_lock);
885
886 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
887 & vreg->part->ip.mask) >> vreg->part->ip.shift);
888
889 if (mode == config->mode_hpm) {
890 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700892 val[vreg->part->ip.word]
893 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
894 << vreg->part->ip.shift;
895 mask[vreg->part->ip.word] = vreg->part->ip.mask;
896
897 if (config->ia_follows_ip) {
898 val[vreg->part->ia.word]
899 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
900 << vreg->part->ia.shift;
901 mask[vreg->part->ia.word]
902 |= vreg->part->ia.mask;
903 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 }
David Collins6f032ba2011-08-31 14:08:15 -0700905 } else if (mode == config->mode_lpm) {
906 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700908 val[vreg->part->ip.word]
909 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
910 << vreg->part->ip.shift;
911 mask[vreg->part->ip.word] = vreg->part->ip.mask;
912
913 if (config->ia_follows_ip) {
914 val[vreg->part->ia.word]
915 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
916 << vreg->part->ia.shift;
917 mask[vreg->part->ia.word]
918 |= vreg->part->ia.mask;
919 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 }
David Collins6f032ba2011-08-31 14:08:15 -0700921 } else {
922 vreg_err(vreg, "invalid mode: %u\n", mode);
923 mutex_unlock(&vreg->pc_lock);
924 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 }
926
David Collins6f032ba2011-08-31 14:08:15 -0700927 if (vreg->is_enabled) {
928 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
929 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930 } else {
931 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700932 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933 }
David Collins6f032ba2011-08-31 14:08:15 -0700934
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -0700936 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
937 else
938 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939
David Collins6f032ba2011-08-31 14:08:15 -0700940 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
David Collins6f032ba2011-08-31 14:08:15 -0700942 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943}
944
David Collins6f032ba2011-08-31 14:08:15 -0700945static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946{
David Collins6f032ba2011-08-31 14:08:15 -0700947 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948
David Collins6f032ba2011-08-31 14:08:15 -0700949 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950}
951
David Collins6f032ba2011-08-31 14:08:15 -0700952static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
953 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954{
David Collins6f032ba2011-08-31 14:08:15 -0700955 struct vreg *vreg = rdev_get_drvdata(rdev);
956 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957
David Collins6f032ba2011-08-31 14:08:15 -0700958 load_uA += vreg->pdata.system_uA;
959
960 mutex_lock(&vreg->pc_lock);
961 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
962 if (config->ia_follows_ip)
963 SET_PART(vreg, ia,
964 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
965 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966
967 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -0700968 mode = config->mode_hpm;
969 else
970 mode = config->mode_lpm;
971
972 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973}
974
David Collins6f032ba2011-08-31 14:08:15 -0700975static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
976 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977{
David Collins6f032ba2011-08-31 14:08:15 -0700978 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979
David Collins6f032ba2011-08-31 14:08:15 -0700980 if (MICRO_TO_MILLI(load_uA) <= 0) {
981 /*
982 * vreg_legacy_get_optimum_mode is being called before consumers
983 * have specified their load currents via
984 * regulator_set_optimum_mode. Return whatever the existing mode
985 * is.
986 */
987 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 }
989
David Collins6f032ba2011-08-31 14:08:15 -0700990 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991}
992
993/*
David Collins6f032ba2011-08-31 14:08:15 -0700994 * Returns the logical pin control enable state because the pin control options
995 * present in the hardware out of restart could be different from those desired
996 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 */
David Collins6f032ba2011-08-31 14:08:15 -0700998static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999{
David Collins6f032ba2011-08-31 14:08:15 -07001000 struct vreg *vreg = rdev_get_drvdata(rdev);
1001
1002 return vreg->is_enabled_pc;
1003}
1004
1005static int vreg_pin_control_enable(struct regulator_dev *rdev)
1006{
1007 struct vreg *vreg = rdev_get_drvdata(rdev);
1008 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 int rc;
1010
David Collins6f032ba2011-08-31 14:08:15 -07001011 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012
David Collins6f032ba2011-08-31 14:08:15 -07001013 val[vreg->part->pc.word]
1014 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1015 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016
David Collins6f032ba2011-08-31 14:08:15 -07001017 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1018 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019
David Collins6f032ba2011-08-31 14:08:15 -07001020 if (!vreg->is_enabled)
1021 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022
David Collins6f032ba2011-08-31 14:08:15 -07001023 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1024 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
David Collins6f032ba2011-08-31 14:08:15 -07001026 if (!rc)
1027 vreg->is_enabled_pc = true;
1028
1029 mutex_unlock(&vreg->pc_lock);
1030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001032 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033
David Collins6f032ba2011-08-31 14:08:15 -07001034 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035}
1036
David Collins6f032ba2011-08-31 14:08:15 -07001037static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038{
David Collins6f032ba2011-08-31 14:08:15 -07001039 struct vreg *vreg = rdev_get_drvdata(rdev);
1040 unsigned int mask[2] = {0}, val[2] = {0};
1041 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042
David Collins6f032ba2011-08-31 14:08:15 -07001043 mutex_lock(&vreg->pc_lock);
1044
1045 val[vreg->part->pc.word]
1046 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1047 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1048
1049 pin_fn = config->pin_func_none;
1050 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1051 pin_fn = config->pin_func_sleep_b;
1052 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1053 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1054
1055 if (!vreg->is_enabled)
1056 set_disable(vreg, mask, val);
1057
1058 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1059 vreg->part->request_len);
1060
1061 if (!rc)
1062 vreg->is_enabled_pc = false;
1063
1064 mutex_unlock(&vreg->pc_lock);
1065
1066 if (rc)
1067 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1068
1069 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070}
1071
David Collins6f032ba2011-08-31 14:08:15 -07001072static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073{
David Collins6f032ba2011-08-31 14:08:15 -07001074 struct vreg *vreg = rdev_get_drvdata(rdev);
1075
1076 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077}
1078
David Collins6f032ba2011-08-31 14:08:15 -07001079/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080static struct regulator_ops ldo_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001081 .enable = vreg_enable,
1082 .disable = vreg_disable,
1083 .is_enabled = vreg_is_enabled,
1084 .set_voltage = vreg_set_voltage,
1085 .get_voltage = vreg_get_voltage,
1086 .list_voltage = vreg_list_voltage,
1087 .set_mode = vreg_set_mode,
1088 .get_mode = vreg_get_mode,
1089 .get_optimum_mode = vreg_get_optimum_mode,
1090 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091};
1092
1093static struct regulator_ops smps_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001094 .enable = vreg_enable,
1095 .disable = vreg_disable,
1096 .is_enabled = vreg_is_enabled,
1097 .set_voltage = vreg_set_voltage,
1098 .get_voltage = vreg_get_voltage,
1099 .list_voltage = vreg_list_voltage,
1100 .set_mode = vreg_set_mode,
1101 .get_mode = vreg_get_mode,
1102 .get_optimum_mode = vreg_get_optimum_mode,
1103 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104};
1105
1106static struct regulator_ops switch_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001107 .enable = vreg_enable,
1108 .disable = vreg_disable,
1109 .is_enabled = vreg_is_enabled,
1110 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111};
1112
1113static struct regulator_ops ncp_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001114 .enable = vreg_enable,
1115 .disable = vreg_disable,
1116 .is_enabled = vreg_is_enabled,
1117 .set_voltage = vreg_set_voltage,
1118 .get_voltage = vreg_get_voltage,
1119 .list_voltage = vreg_list_voltage,
1120 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121};
1122
David Collins6f032ba2011-08-31 14:08:15 -07001123/* Pin control regulator operations. */
1124static struct regulator_ops pin_control_ops = {
1125 .enable = vreg_pin_control_enable,
1126 .disable = vreg_pin_control_disable,
1127 .is_enabled = vreg_pin_control_is_enabled,
1128};
1129
1130struct regulator_ops *vreg_ops[] = {
1131 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1132 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1133 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1134 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1135};
1136
1137static int __devinit
1138rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1139 struct device *dev)
1140{
1141 struct regulator_desc *rdesc = NULL;
1142 struct regulator_dev *rdev;
1143 struct vreg *vreg;
1144 unsigned pin_ctrl;
1145 int id, pin_fn;
1146 int rc = 0;
1147
1148 if (!pdata) {
1149 pr_err("platform data missing\n");
1150 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 }
1152
David Collins6f032ba2011-08-31 14:08:15 -07001153 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154
David Collins6f032ba2011-08-31 14:08:15 -07001155 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1156 pr_err("invalid regulator id: %d\n", id);
1157 return -ENODEV;
1158 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159
David Collins6f032ba2011-08-31 14:08:15 -07001160 if (!config->is_real_id(pdata->id))
1161 id = config->pc_id_to_real_id(pdata->id);
1162 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163
David Collins6f032ba2011-08-31 14:08:15 -07001164 if (config->is_real_id(pdata->id))
1165 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166 else
David Collins6f032ba2011-08-31 14:08:15 -07001167 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168
David Collins6f032ba2011-08-31 14:08:15 -07001169 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1170 pr_err("%s: invalid regulator type: %d\n",
1171 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001173 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174
David Collins6f032ba2011-08-31 14:08:15 -07001175 mutex_lock(&vreg->pc_lock);
1176
1177 if (vreg->set_points)
1178 rdesc->n_voltages = vreg->set_points->n_voltages;
1179 else
1180 rdesc->n_voltages = 0;
1181
1182 rdesc->id = pdata->id;
1183 rdesc->owner = THIS_MODULE;
1184 rdesc->type = REGULATOR_VOLTAGE;
1185
1186 if (config->is_real_id(pdata->id)) {
1187 /*
1188 * Real regulator; do not modify pin control and pin function
1189 * values.
1190 */
1191 rdesc->ops = vreg_ops[vreg->type];
1192 pin_ctrl = vreg->pdata.pin_ctrl;
1193 pin_fn = vreg->pdata.pin_fn;
1194 memcpy(&(vreg->pdata), pdata,
1195 sizeof(struct rpm_regulator_init_data));
1196 vreg->pdata.pin_ctrl = pin_ctrl;
1197 vreg->pdata.pin_fn = pin_fn;
1198
1199 vreg->save_uV = vreg->pdata.default_uV;
1200 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1201 vreg->mode = config->mode_hpm;
1202 else
1203 vreg->mode = config->mode_lpm;
1204
1205 /* Initialize the RPM request. */
1206 SET_PART(vreg, ip,
1207 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1208 SET_PART(vreg, fm, vreg->pdata.force_mode);
1209 SET_PART(vreg, pm, vreg->pdata.power_mode);
1210 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1211 SET_PART(vreg, ia,
1212 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1213 SET_PART(vreg, freq, vreg->pdata.freq);
1214 SET_PART(vreg, freq_clk_src, 0);
1215 SET_PART(vreg, comp_mode, 0);
1216 SET_PART(vreg, hpm, 0);
1217 if (!vreg->is_enabled_pc) {
1218 SET_PART(vreg, pf, config->pin_func_none);
1219 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1220 }
1221 } else {
1222 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1223 == RPM_VREG_PIN_CTRL_NONE
1224 && pdata->pin_fn != config->pin_func_sleep_b) {
1225 pr_err("%s: no pin control input specified\n",
1226 vreg->rdesc.name);
1227 mutex_unlock(&vreg->pc_lock);
1228 return -EINVAL;
1229 }
1230 rdesc->ops = &pin_control_ops;
1231 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1232 vreg->pdata.pin_fn = pdata->pin_fn;
1233
1234 /* Initialize the RPM request. */
1235 pin_fn = config->pin_func_none;
1236 /* Allow pf=sleep_b to be specified by platform data. */
1237 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1238 pin_fn = config->pin_func_sleep_b;
1239 SET_PART(vreg, pf, pin_fn);
1240 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1241 }
1242
1243 mutex_unlock(&vreg->pc_lock);
1244
1245 if (rc)
1246 goto bail;
1247
1248 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1249 if (IS_ERR(rdev)) {
1250 rc = PTR_ERR(rdev);
1251 pr_err("regulator_register failed: %s, rc=%d\n",
1252 vreg->rdesc.name, rc);
1253 return rc;
1254 } else {
1255 if (config->is_real_id(pdata->id))
1256 vreg->rdev = rdev;
1257 else
1258 vreg->rdev_pc = rdev;
1259 }
1260
1261bail:
1262 if (rc)
1263 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1264
1265 return rc;
1266}
1267
1268static void rpm_vreg_set_point_init(void)
1269{
1270 struct vreg_set_points **set_points;
1271 int i, j, temp;
1272
1273 set_points = config->set_points;
1274
1275 /* Calculate the number of set points available for each regulator. */
1276 for (i = 0; i < config->set_points_len; i++) {
1277 temp = 0;
1278 for (j = 0; j < set_points[i]->count; j++) {
1279 set_points[i]->range[j].n_voltages
1280 = (set_points[i]->range[j].max_uV
1281 - set_points[i]->range[j].min_uV)
1282 / set_points[i]->range[j].step_uV + 1;
1283 temp += set_points[i]->range[j].n_voltages;
1284 }
1285 set_points[i]->n_voltages = temp;
1286 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287}
1288
1289static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1290{
David Collins6f032ba2011-08-31 14:08:15 -07001291 struct rpm_regulator_platform_data *platform_data;
1292 int rc = 0;
1293 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294
David Collins6f032ba2011-08-31 14:08:15 -07001295 platform_data = pdev->dev.platform_data;
1296 if (!platform_data) {
1297 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299 }
1300
David Collins6f032ba2011-08-31 14:08:15 -07001301 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1302 && platform_data->version != rpm_version) {
1303 pr_err("rpm version %d does not match previous version %d\n",
1304 platform_data->version, rpm_version);
1305 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 }
1307
David Collins6f032ba2011-08-31 14:08:15 -07001308 if (platform_data->version < 0
1309 || platform_data->version > RPM_VREG_VERSION_MAX) {
1310 pr_err("rpm version %d is invalid\n", platform_data->version);
1311 return -EINVAL;
1312 }
1313
1314 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1315 rpm_version = platform_data->version;
1316 config = get_config[platform_data->version]();
1317 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1318 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1319 if (!config) {
1320 pr_err("rpm version %d is not available\n",
1321 platform_data->version);
1322 return -ENODEV;
1323 }
1324 if (config->use_legacy_optimum_mode)
1325 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1326 vreg_ops[i]->get_optimum_mode
1327 = vreg_legacy_get_optimum_mode;
1328 rpm_vreg_set_point_init();
1329 /* First time probed; initialize pin control mutexes. */
1330 for (i = 0; i < config->vregs_len; i++)
1331 mutex_init(&config->vregs[i].pc_lock);
1332 }
1333
1334 /* Initialize all of the regulators listed in the platform data. */
1335 for (i = 0; i < platform_data->num_regulators; i++) {
1336 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1337 &pdev->dev);
1338 if (rc) {
1339 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1340 goto remove_regulators;
1341 }
1342 }
1343
1344 platform_set_drvdata(pdev, platform_data);
1345
1346 return rc;
1347
1348remove_regulators:
1349 /* Unregister all regulators added before the erroring one. */
1350 for (; i >= 0; i--) {
1351 id = platform_data->init_data[i].id;
1352 if (config->is_real_id(id)) {
1353 regulator_unregister(config->vregs[id].rdev);
1354 config->vregs[id].rdev = NULL;
1355 } else {
1356 regulator_unregister(config->vregs[
1357 config->pc_id_to_real_id(id)].rdev_pc);
1358 config->vregs[id].rdev_pc = NULL;
1359 }
1360 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361
1362 return rc;
1363}
1364
1365static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1366{
David Collins6f032ba2011-08-31 14:08:15 -07001367 struct rpm_regulator_platform_data *platform_data;
1368 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001369
David Collins6f032ba2011-08-31 14:08:15 -07001370 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001372
1373 if (platform_data) {
1374 for (i = 0; i < platform_data->num_regulators; i++) {
1375 id = platform_data->init_data[i].id;
1376 if (config->is_real_id(id)) {
1377 regulator_unregister(config->vregs[id].rdev);
1378 config->vregs[id].rdev = NULL;
1379 } else {
1380 regulator_unregister(config->vregs[
1381 config->pc_id_to_real_id(id)].rdev_pc);
1382 config->vregs[id].rdev_pc = NULL;
1383 }
1384 }
1385 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386
1387 return 0;
1388}
1389
1390static struct platform_driver rpm_vreg_driver = {
1391 .probe = rpm_vreg_probe,
1392 .remove = __devexit_p(rpm_vreg_remove),
1393 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001394 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001395 .owner = THIS_MODULE,
1396 },
1397};
1398
1399static int __init rpm_vreg_init(void)
1400{
1401 return platform_driver_register(&rpm_vreg_driver);
1402}
1403
1404static void __exit rpm_vreg_exit(void)
1405{
David Collins6f032ba2011-08-31 14:08:15 -07001406 int i;
1407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001408 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001409
1410 for (i = 0; i < config->vregs_len; i++)
1411 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412}
1413
1414postcore_initcall(rpm_vreg_init);
1415module_exit(rpm_vreg_exit);
1416
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001418MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001420MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);