blob: c10281b4ab5d7cd5a6f753c70d11583b00a6124c [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 \
David Collinsd8525e82011-11-21 14:54:25 -080054 & ~_vreg->part->_part.mask) \
55 | (((_val) << _vreg->part->_part.shift) \
56 & _vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057
David Collins6f032ba2011-08-31 14:08:15 -070058#define GET_PART(_vreg, _part) \
David Collinsd8525e82011-11-21 14:54:25 -080059 ((_vreg->req[_vreg->part->_part.word].value & _vreg->part->_part.mask) \
60 >> _vreg->part->_part.shift)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061
David Collinsd8525e82011-11-21 14:54:25 -080062#define USES_PART(_vreg, _part) (_vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063
David Collins6f032ba2011-08-31 14:08:15 -070064#define vreg_err(vreg, fmt, ...) \
65 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066
David Collins6f032ba2011-08-31 14:08:15 -070067#define RPM_VREG_PIN_CTRL_EN0 0x01
68#define RPM_VREG_PIN_CTRL_EN1 0x02
69#define RPM_VREG_PIN_CTRL_EN2 0x04
70#define RPM_VREG_PIN_CTRL_EN3 0x08
71#define RPM_VREG_PIN_CTRL_ALL 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072
David Collins6f032ba2011-08-31 14:08:15 -070073static const char *label_freq[] = {
74 [RPM_VREG_FREQ_NONE] = " N/A",
75 [RPM_VREG_FREQ_19p20] = "19.2",
76 [RPM_VREG_FREQ_9p60] = "9.60",
77 [RPM_VREG_FREQ_6p40] = "6.40",
78 [RPM_VREG_FREQ_4p80] = "4.80",
79 [RPM_VREG_FREQ_3p84] = "3.84",
80 [RPM_VREG_FREQ_3p20] = "3.20",
81 [RPM_VREG_FREQ_2p74] = "2.74",
82 [RPM_VREG_FREQ_2p40] = "2.40",
83 [RPM_VREG_FREQ_2p13] = "2.13",
84 [RPM_VREG_FREQ_1p92] = "1.92",
85 [RPM_VREG_FREQ_1p75] = "1.75",
86 [RPM_VREG_FREQ_1p60] = "1.60",
87 [RPM_VREG_FREQ_1p48] = "1.48",
88 [RPM_VREG_FREQ_1p37] = "1.37",
89 [RPM_VREG_FREQ_1p28] = "1.28",
90 [RPM_VREG_FREQ_1p20] = "1.20",
91};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092/*
93 * This is used when voting for LPM or HPM by subtracting or adding to the
94 * hpm_min_load of a regulator. It has units of uA.
95 */
David Collins6f032ba2011-08-31 14:08:15 -070096#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097
David Collins6f032ba2011-08-31 14:08:15 -070098/* rpm_version keeps track of the version for the currently running driver. */
99enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100
David Collins6f032ba2011-08-31 14:08:15 -0700101/* config holds all configuration data of the currently running driver. */
102static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103
David Collins6f032ba2011-08-31 14:08:15 -0700104/* These regulator ID values are specified in the board file. */
105static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106
David Collins6f032ba2011-08-31 14:08:15 -0700107static inline int vreg_id_is_vdd_mem_or_dig(int id)
108{
109 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
110}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111
David Collins6f032ba2011-08-31 14:08:15 -0700112#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113
David Collins6f032ba2011-08-31 14:08:15 -0700114static void rpm_regulator_req(struct vreg *vreg, int set)
115{
116 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
117 const char *pf_label = "", *fm_label = "", *pc_total = "";
118 const char *pc_en[4] = {"", "", "", ""};
119 const char *pm_label = "", *freq_label = "";
120 char buf[DEBUG_PRINT_BUFFER_SIZE];
121 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
122 int pos = 0;
123
124 /* Suppress VDD_MEM and VDD_DIG printing. */
125 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
126 && vreg_id_is_vdd_mem_or_dig(vreg->id))
127 return;
128
129 uV = GET_PART(vreg, uV);
130 mV = GET_PART(vreg, mV);
131 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
132 uV = -uV;
133 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134 }
135
David Collins6f032ba2011-08-31 14:08:15 -0700136 fm = GET_PART(vreg, fm);
137 pm = GET_PART(vreg, pm);
138 pc = GET_PART(vreg, pc);
139 pf = GET_PART(vreg, pf);
140 pd = GET_PART(vreg, pd);
141 freq = GET_PART(vreg, freq);
142 state = GET_PART(vreg, enable_state);
143
144 if (pf >= 0 && pf < config->label_pin_func_len)
145 pf_label = config->label_pin_func[pf];
146
147 if (fm >= 0 && fm < config->label_force_mode_len)
148 fm_label = config->label_force_mode[fm];
149
150 if (pm >= 0 && pm < config->label_power_mode_len)
151 pm_label = config->label_power_mode[pm];
152
153 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
154 freq_label = label_freq[freq];
155
156 for (i = 0; i < config->label_pin_ctrl_len; i++)
157 if (pc & (1 << i))
158 pc_en[i] = config->label_pin_ctrl[i];
159
160 if (pc == RPM_VREG_PIN_CTRL_NONE)
161 pc_total = " none";
162
163 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
164 KERN_INFO, __func__);
165
166 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
167 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
168 vreg->rdesc.name,
169 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
170
171 if (USES_PART(vreg, uV))
172 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
173 if (USES_PART(vreg, mV))
174 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
175 if (USES_PART(vreg, enable_state))
176 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
177 (state == 1 ? "on" : "off"), state);
178 if (USES_PART(vreg, ip))
179 pos += scnprintf(buf + pos, buflen - pos,
180 ", ip=%4d mA", GET_PART(vreg, ip));
181 if (USES_PART(vreg, fm))
182 pos += scnprintf(buf + pos, buflen - pos,
183 ", fm=%s (%d)", fm_label, fm);
184 if (USES_PART(vreg, pc))
185 pos += scnprintf(buf + pos, buflen - pos,
186 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
187 pc_en[2], pc_en[3], pc_total, pc);
188 if (USES_PART(vreg, pf))
189 pos += scnprintf(buf + pos, buflen - pos,
190 ", pf=%s (%d)", pf_label, pf);
191 if (USES_PART(vreg, pd))
192 pos += scnprintf(buf + pos, buflen - pos,
193 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
194 if (USES_PART(vreg, ia))
195 pos += scnprintf(buf + pos, buflen - pos,
196 ", ia=%4d mA", GET_PART(vreg, ia));
197 if (USES_PART(vreg, freq)) {
198 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
199 pos += scnprintf(buf + pos, buflen - pos,
200 ", freq=%2d", freq);
201 else
202 pos += scnprintf(buf + pos, buflen - pos,
203 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 }
David Collins6f032ba2011-08-31 14:08:15 -0700205 if (USES_PART(vreg, pm))
206 pos += scnprintf(buf + pos, buflen - pos,
207 ", pm=%s (%d)", pm_label, pm);
208 if (USES_PART(vreg, freq_clk_src))
209 pos += scnprintf(buf + pos, buflen - pos,
210 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
211 if (USES_PART(vreg, comp_mode))
212 pos += scnprintf(buf + pos, buflen - pos,
213 ", comp=%d", GET_PART(vreg, comp_mode));
214 if (USES_PART(vreg, hpm))
215 pos += scnprintf(buf + pos, buflen - pos,
216 ", hpm=%d", GET_PART(vreg, hpm));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217
David Collins6f032ba2011-08-31 14:08:15 -0700218 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
219 vreg->req[0].id, vreg->req[0].value);
220 if (vreg->part->request_len > 1)
221 pos += scnprintf(buf + pos, buflen - pos,
222 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
223 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224
David Collins6f032ba2011-08-31 14:08:15 -0700225 pos += scnprintf(buf + pos, buflen - pos, "\n");
226 printk(buf);
227}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228
David Collins6f032ba2011-08-31 14:08:15 -0700229static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
230 int set, int voter_uV, int aggregate_uV)
231{
232 /* Suppress VDD_MEM and VDD_DIG printing. */
233 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
234 && vreg_id_is_vdd_mem_or_dig(vreg->id))
235 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236
David Collins6f032ba2011-08-31 14:08:15 -0700237 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
238 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
239 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
240}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241
David Collins6f032ba2011-08-31 14:08:15 -0700242static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
243{
244 /* Suppress VDD_MEM and VDD_DIG printing. */
245 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
246 && vreg_id_is_vdd_mem_or_dig(vreg->id))
247 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248
David Collins6f032ba2011-08-31 14:08:15 -0700249 if (cnt == 2)
250 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
251 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
252 (set == 0 ? 'A' : 'S'),
253 vreg->req[0].id, vreg->req[0].value,
254 vreg->req[1].id, vreg->req[1].value);
255 else if (cnt == 1)
256 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
257 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
258 vreg->req[0].id, vreg->req[0].value);
259}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260
261/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700262static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263
264static int voltage_from_req(struct vreg *vreg)
265{
David Collins6f032ba2011-08-31 14:08:15 -0700266 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267
David Collins6f032ba2011-08-31 14:08:15 -0700268 if (vreg->part->uV.mask)
269 uV = GET_PART(vreg, uV);
270 else
271 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272
David Collins6f032ba2011-08-31 14:08:15 -0700273 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274}
275
David Collins6f032ba2011-08-31 14:08:15 -0700276static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277{
David Collins6f032ba2011-08-31 14:08:15 -0700278 if (vreg->part->uV.mask)
279 SET_PART(vreg, uV, uV);
280 else
281 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282}
283
284static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
285 int set, unsigned mask0, unsigned val0,
286 unsigned mask1, unsigned val1, unsigned cnt,
287 int update_voltage)
288{
289 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700290 int rc = 0, max_uV_vote = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700292 int *min_uV_vote;
293 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294
295 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700296 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 prev_req = vreg->prev_active_req;
298 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700299 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 prev_req = vreg->prev_sleep_req;
301 }
302
303 prev0 = vreg->req[0].value;
304 vreg->req[0].value &= ~mask0;
305 vreg->req[0].value |= val0 & mask0;
306
307 prev1 = vreg->req[1].value;
308 vreg->req[1].value &= ~mask1;
309 vreg->req[1].value |= val1 & mask1;
310
311 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700312 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313
314 /* Find the highest voltage voted for and use it. */
315 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700316 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
317 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318
319 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700320 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
321 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322
323 /* Ignore duplicate requests */
324 if (vreg->req[0].value != prev_req[0].value ||
325 vreg->req[1].value != prev_req[1].value) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
327 if (rc) {
328 vreg->req[0].value = prev0;
329 vreg->req[1].value = prev1;
330
David Collins6f032ba2011-08-31 14:08:15 -0700331 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
332 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
334 vreg->req[0].id, rc);
335 } else {
336 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700337 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
338 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 if (msm_rpm_vreg_debug_mask
340 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700341 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 prev_req[0].value = vreg->req[0].value;
343 prev_req[1].value = vreg->req[1].value;
344 }
345 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700346 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 }
348
349 return rc;
350}
351
352static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
353 int sleep, unsigned mask0, unsigned val0,
354 unsigned mask1, unsigned val1, unsigned cnt,
355 int update_voltage)
356{
David Collins6f032ba2011-08-31 14:08:15 -0700357 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 unsigned long flags;
359 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360
361 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
362 return -EINVAL;
363
David Collins6f032ba2011-08-31 14:08:15 -0700364 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365
366 /*
367 * Send sleep set request first so that subsequent set_mode, etc calls
368 * use the voltage from the active set.
369 */
370 if (sleep)
371 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
372 mask0, val0, mask1, val1, cnt, update_voltage);
373 else {
374 /*
375 * Vote for 0 V in the sleep set when active set-only is
376 * specified. This ensures that a disable vote will be issued
377 * at some point for the sleep set of the regulator.
378 */
David Collins6f032ba2011-08-31 14:08:15 -0700379 if (vreg->part->uV.mask) {
380 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
381 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
382 } else {
383 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
384 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385 }
386
387 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700388 s_mask[0], s_val[0], s_mask[1], s_val[1],
389 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390 }
391
392 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
393 mask1, val1, cnt, update_voltage);
394
David Collins6f032ba2011-08-31 14:08:15 -0700395 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396
397 return rc;
398}
399
400/**
401 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
402 * @vreg: ID for regulator
403 * @voter: ID for the voter
404 * @min_uV: minimum acceptable voltage (in uV) that is voted for
405 * @max_uV: maximum acceptable voltage (in uV) that is voted for
406 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
407 *
408 * Returns 0 on success or errno.
409 *
410 * This function is used to vote for the voltage of a regulator without
411 * using the regulator framework. It is needed by consumers which hold spin
412 * locks or have interrupts disabled because the regulator framework can sleep.
413 * It is also needed by consumers which wish to only vote for active set
414 * regulator voltage.
415 *
416 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
417 *
418 * This function may only be called for regulators which have the sleep flag
419 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700420 *
421 * Consumers can vote to disable a regulator with this function by passing
422 * min_uV = 0 and max_uV = 0.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 */
David Collins6f032ba2011-08-31 14:08:15 -0700424int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
425 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426{
David Collins6f032ba2011-08-31 14:08:15 -0700427 unsigned int mask[2] = {0}, val[2] = {0};
428 struct vreg_range *range;
429 struct vreg *vreg;
430 int uV = min_uV;
431 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432
David Collins6f032ba2011-08-31 14:08:15 -0700433 /*
434 * HACK: make this function a no-op for 8064 so that it can be called by
435 * consumers on 8064 before RPM capabilities are present. (needed for
436 * acpuclock driver)
437 */
438 if (cpu_is_apq8064())
439 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440
David Collins6f032ba2011-08-31 14:08:15 -0700441 if (!config) {
442 pr_err("rpm-regulator driver has not probed yet.\n");
443 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 }
445
David Collins6f032ba2011-08-31 14:08:15 -0700446 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
447 pr_err("invalid regulator id=%d\n", vreg_id);
448 return -EINVAL;
449 }
450
451 vreg = &config->vregs[vreg_id];
452 range = &vreg->set_points->range[0];
453
454 if (!vreg->pdata.sleep_selectable) {
455 vreg_err(vreg, "regulator is not marked sleep selectable\n");
456 return -EINVAL;
457 }
458
David Collins7462b9d2011-10-11 16:02:17 -0700459 /* Allow min_uV == max_uV == 0 to represent a disable request. */
460 if (min_uV != 0 || max_uV != 0) {
461 /*
462 * Check if request voltage is outside of allowed range. The
463 * regulator core has already checked that constraint range
464 * is inside of the physically allowed range.
465 */
466 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
467 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700468
David Collins7462b9d2011-10-11 16:02:17 -0700469 if (uV < lim_min_uV && max_uV >= lim_min_uV)
470 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700471
David Collins7462b9d2011-10-11 16:02:17 -0700472 if (uV < lim_min_uV || uV > lim_max_uV) {
473 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
474 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
475 lim_max_uV);
476 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700477 }
David Collins6f032ba2011-08-31 14:08:15 -0700478
David Collins7462b9d2011-10-11 16:02:17 -0700479 /* Find the range which uV is inside of. */
480 for (i = vreg->set_points->count - 1; i > 0; i--) {
481 if (uV > vreg->set_points->range[i - 1].max_uV) {
482 range = &vreg->set_points->range[i];
483 break;
484 }
485 }
486
487 /*
488 * Force uV to be an allowed set point and apply a ceiling
489 * function to non-set point values.
490 */
491 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
492 uV = uV * range->step_uV + range->min_uV;
493 }
David Collins6f032ba2011-08-31 14:08:15 -0700494
495 if (vreg->part->uV.mask) {
496 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
497 mask[vreg->part->uV.word] = vreg->part->uV.mask;
498 } else {
499 val[vreg->part->mV.word]
500 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
501 mask[vreg->part->mV.word] = vreg->part->mV.mask;
502 }
503
504 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
505 val[1], vreg->part->request_len, 1);
506 if (rc)
507 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508
509 return rc;
510}
511EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
512
513/**
514 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
515 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700516 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 *
518 * Returns 0 on success or errno.
519 */
David Collins6f032ba2011-08-31 14:08:15 -0700520int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521{
David Collins6f032ba2011-08-31 14:08:15 -0700522 unsigned int mask[2] = {0}, val[2] = {0};
523 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 int rc;
525
David Collins6f032ba2011-08-31 14:08:15 -0700526 /*
527 * HACK: make this function a no-op for 8064 so that it can be called by
528 * consumers on 8064 before RPM capabilities are present.
529 */
530 if (cpu_is_apq8064())
531 return 0;
532
533 if (!config) {
534 pr_err("rpm-regulator driver has not probed yet.\n");
535 return -ENODEV;
536 }
537
538 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
539 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540 return -EINVAL;
541 }
542
David Collins6f032ba2011-08-31 14:08:15 -0700543 vreg = &config->vregs[vreg_id];
544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700546 vreg_err(vreg, "invalid frequency=%d\n", freq);
547 return -EINVAL;
548 }
549 if (!vreg->pdata.sleep_selectable) {
550 vreg_err(vreg, "regulator is not marked sleep selectable\n");
551 return -EINVAL;
552 }
553 if (!vreg->part->freq.mask) {
554 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 return -EINVAL;
556 }
557
David Collins6f032ba2011-08-31 14:08:15 -0700558 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
559 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560
David Collins6f032ba2011-08-31 14:08:15 -0700561 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
562 val[0], mask[1], val[1], vreg->part->request_len, 0);
563 if (rc)
564 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565
566 return rc;
567}
568EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570static inline int vreg_hpm_min_uA(struct vreg *vreg)
571{
572 return vreg->hpm_min_load;
573}
574
575static inline int vreg_lpm_max_uA(struct vreg *vreg)
576{
577 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
578}
579
David Collins6f032ba2011-08-31 14:08:15 -0700580static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581{
David Collins6f032ba2011-08-31 14:08:15 -0700582 unsigned load_max
583 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
584
585 return (load_uA > load_max ? load_max : load_uA);
586}
587
588static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
589{
590 unsigned load_max
591 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
592 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593}
594
595/* Change vreg->req, but do not send it to the RPM. */
596static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
597 unsigned mask1, unsigned val1)
598{
599 unsigned long flags = 0;
600
David Collins6f032ba2011-08-31 14:08:15 -0700601 if (vreg->pdata.sleep_selectable)
602 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603
604 vreg->req[0].value &= ~mask0;
605 vreg->req[0].value |= val0 & mask0;
606
607 vreg->req[1].value &= ~mask1;
608 vreg->req[1].value |= val1 & mask1;
609
David Collins6f032ba2011-08-31 14:08:15 -0700610 if (vreg->pdata.sleep_selectable)
611 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612
613 return 0;
614}
615
616static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
617 unsigned mask1, unsigned val1, unsigned cnt)
618{
619 unsigned prev0 = 0, prev1 = 0;
620 int rc;
621
622 /*
623 * Bypass the normal route for regulators that can be called to change
624 * just the active set values.
625 */
David Collins6f032ba2011-08-31 14:08:15 -0700626 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
628 mask0, val0, mask1, val1, cnt, 1);
629
630 prev0 = vreg->req[0].value;
631 vreg->req[0].value &= ~mask0;
632 vreg->req[0].value |= val0 & mask0;
633
634 prev1 = vreg->req[1].value;
635 vreg->req[1].value &= ~mask1;
636 vreg->req[1].value |= val1 & mask1;
637
638 /* Ignore duplicate requests */
639 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
640 vreg->req[1].value == vreg->prev_active_req[1].value) {
641 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700642 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 return 0;
644 }
645
646 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
647 if (rc) {
648 vreg->req[0].value = prev0;
649 vreg->req[1].value = prev1;
650
David Collins6f032ba2011-08-31 14:08:15 -0700651 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
652 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 } else {
654 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700655 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 vreg->prev_active_req[0].value = vreg->req[0].value;
657 vreg->prev_active_req[1].value = vreg->req[1].value;
658 }
659
660 return rc;
661}
662
David Collins6f032ba2011-08-31 14:08:15 -0700663static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664{
David Collins6f032ba2011-08-31 14:08:15 -0700665 struct vreg *vreg = rdev_get_drvdata(rdev);
666 int enabled;
667
668 mutex_lock(&vreg->pc_lock);
669 enabled = vreg->is_enabled;
670 mutex_unlock(&vreg->pc_lock);
671
672 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673}
674
David Collins6f032ba2011-08-31 14:08:15 -0700675static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676{
David Collins6f032ba2011-08-31 14:08:15 -0700677 switch (vreg->type) {
678 case RPM_REGULATOR_TYPE_LDO:
679 case RPM_REGULATOR_TYPE_SMPS:
680 /* Enable by setting a voltage. */
681 if (vreg->part->uV.mask) {
682 val[vreg->part->uV.word]
683 |= vreg->save_uV << vreg->part->uV.shift;
684 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
685 } else {
686 val[vreg->part->mV.word]
687 |= MICRO_TO_MILLI(vreg->save_uV)
688 << vreg->part->mV.shift;
689 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
690 }
691 break;
692 case RPM_REGULATOR_TYPE_VS:
693 case RPM_REGULATOR_TYPE_NCP:
694 /* Enable by setting enable_state. */
695 val[vreg->part->enable_state.word]
696 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
697 mask[vreg->part->enable_state.word]
698 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700}
701
David Collins6f032ba2011-08-31 14:08:15 -0700702static int vreg_enable(struct regulator_dev *rdev)
703{
704 struct vreg *vreg = rdev_get_drvdata(rdev);
705 unsigned int mask[2] = {0}, val[2] = {0};
706 int rc = 0;
707
708 set_enable(vreg, mask, val);
709
710 mutex_lock(&vreg->pc_lock);
711
712 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
713 vreg->part->request_len);
714 if (!rc)
715 vreg->is_enabled = true;
716
717 mutex_unlock(&vreg->pc_lock);
718
719 if (rc)
720 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
721
722 return rc;
723}
724
725static void set_disable(struct vreg *vreg, unsigned int *mask,
726 unsigned int *val)
727{
728 switch (vreg->type) {
729 case RPM_REGULATOR_TYPE_LDO:
730 case RPM_REGULATOR_TYPE_SMPS:
731 /* Disable by setting a voltage of 0 uV. */
732 if (vreg->part->uV.mask) {
733 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
734 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
735 } else {
736 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
737 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
738 }
739 break;
740 case RPM_REGULATOR_TYPE_VS:
741 case RPM_REGULATOR_TYPE_NCP:
742 /* Disable by setting enable_state. */
743 val[vreg->part->enable_state.word]
744 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
745 mask[vreg->part->enable_state.word]
746 |= vreg->part->enable_state.mask;
747 }
748}
749
750static int vreg_disable(struct regulator_dev *rdev)
751{
752 struct vreg *vreg = rdev_get_drvdata(rdev);
753 unsigned int mask[2] = {0}, val[2] = {0};
754 int rc = 0;
755
756 set_disable(vreg, mask, val);
757
758 mutex_lock(&vreg->pc_lock);
759
760 /* Only disable if pin control is not in use. */
761 if (!vreg->is_enabled_pc)
762 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
763 vreg->part->request_len);
764
765 if (!rc)
766 vreg->is_enabled = false;
767
768 mutex_unlock(&vreg->pc_lock);
769
770 if (rc)
771 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
772
773 return rc;
774}
775
776static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 unsigned *selector)
778{
David Collins6f032ba2011-08-31 14:08:15 -0700779 struct vreg *vreg = rdev_get_drvdata(rdev);
780 struct vreg_range *range = &vreg->set_points->range[0];
781 unsigned int mask[2] = {0}, val[2] = {0};
782 int rc = 0, uV = min_uV;
783 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784
David Collins6f032ba2011-08-31 14:08:15 -0700785 /* Check if request voltage is outside of physically settable range. */
786 lim_min_uV = vreg->set_points->range[0].min_uV;
787 lim_max_uV =
788 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789
David Collins6f032ba2011-08-31 14:08:15 -0700790 if (uV < lim_min_uV && max_uV >= lim_min_uV)
791 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792
David Collins6f032ba2011-08-31 14:08:15 -0700793 if (uV < lim_min_uV || uV > lim_max_uV) {
794 vreg_err(vreg,
795 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
796 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 return -EINVAL;
798 }
799
David Collins6f032ba2011-08-31 14:08:15 -0700800 /* Find the range which uV is inside of. */
801 for (i = vreg->set_points->count - 1; i > 0; i--) {
802 if (uV > vreg->set_points->range[i - 1].max_uV) {
803 range = &vreg->set_points->range[i];
804 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 }
806 }
807
David Collins6f032ba2011-08-31 14:08:15 -0700808 /*
809 * Force uV to be an allowed set point and apply a ceiling function
810 * to non-set point values.
811 */
812 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
813 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814
David Collins6f032ba2011-08-31 14:08:15 -0700815 if (vreg->part->uV.mask) {
816 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
817 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700819 val[vreg->part->mV.word]
820 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
821 mask[vreg->part->mV.word] = vreg->part->mV.mask;
822 }
823
824 mutex_lock(&vreg->pc_lock);
825
826 /*
827 * Only send a request for a new voltage if the regulator is currently
828 * enabled. This will ensure that LDO and SMPS regulators are not
829 * inadvertently turned on because voltage > 0 is equivalent to
830 * enabling. For NCP, this just removes unnecessary RPM requests.
831 */
832 if (vreg->is_enabled) {
833 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
834 vreg->part->request_len);
835 if (rc)
836 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
837 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700839 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840 }
841
David Collins6f032ba2011-08-31 14:08:15 -0700842 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
843 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844
David Collins6f032ba2011-08-31 14:08:15 -0700845 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846
847 return rc;
848}
849
David Collins6f032ba2011-08-31 14:08:15 -0700850static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851{
David Collins6f032ba2011-08-31 14:08:15 -0700852 struct vreg *vreg = rdev_get_drvdata(rdev);
853
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 return vreg->save_uV;
855}
856
David Collins6f032ba2011-08-31 14:08:15 -0700857static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858{
David Collins6f032ba2011-08-31 14:08:15 -0700859 struct vreg *vreg = rdev_get_drvdata(rdev);
860 int uV = 0;
861 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862
David Collins6f032ba2011-08-31 14:08:15 -0700863 if (!vreg->set_points) {
864 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 return -EINVAL;
866 }
867
David Collins6f032ba2011-08-31 14:08:15 -0700868 if (selector >= vreg->set_points->n_voltages)
869 return 0;
870
871 for (i = 0; i < vreg->set_points->count; i++) {
872 if (selector < vreg->set_points->range[i].n_voltages) {
873 uV = selector * vreg->set_points->range[i].step_uV
874 + vreg->set_points->range[i].min_uV;
875 break;
876 } else {
877 selector -= vreg->set_points->range[i].n_voltages;
878 }
879 }
880
881 return uV;
882}
883
884static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
885{
886 struct vreg *vreg = rdev_get_drvdata(rdev);
887 unsigned int mask[2] = {0}, val[2] = {0};
888 int rc = 0;
889 int peak_uA;
890
891 mutex_lock(&vreg->pc_lock);
892
893 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
894 & vreg->part->ip.mask) >> vreg->part->ip.shift);
895
896 if (mode == config->mode_hpm) {
897 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700899 val[vreg->part->ip.word]
900 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
901 << vreg->part->ip.shift;
902 mask[vreg->part->ip.word] = vreg->part->ip.mask;
903
904 if (config->ia_follows_ip) {
905 val[vreg->part->ia.word]
906 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
907 << vreg->part->ia.shift;
908 mask[vreg->part->ia.word]
909 |= vreg->part->ia.mask;
910 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911 }
David Collins6f032ba2011-08-31 14:08:15 -0700912 } else if (mode == config->mode_lpm) {
913 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700915 val[vreg->part->ip.word]
916 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
917 << vreg->part->ip.shift;
918 mask[vreg->part->ip.word] = vreg->part->ip.mask;
919
920 if (config->ia_follows_ip) {
921 val[vreg->part->ia.word]
922 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
923 << vreg->part->ia.shift;
924 mask[vreg->part->ia.word]
925 |= vreg->part->ia.mask;
926 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 }
David Collins6f032ba2011-08-31 14:08:15 -0700928 } else {
929 vreg_err(vreg, "invalid mode: %u\n", mode);
930 mutex_unlock(&vreg->pc_lock);
931 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932 }
933
David Collins6f032ba2011-08-31 14:08:15 -0700934 if (vreg->is_enabled) {
935 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
936 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 } else {
938 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700939 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 }
David Collins6f032ba2011-08-31 14:08:15 -0700941
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -0700943 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
944 else
945 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946
David Collins6f032ba2011-08-31 14:08:15 -0700947 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948
David Collins6f032ba2011-08-31 14:08:15 -0700949 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950}
951
David Collins6f032ba2011-08-31 14:08:15 -0700952static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953{
David Collins6f032ba2011-08-31 14:08:15 -0700954 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955
David Collins6f032ba2011-08-31 14:08:15 -0700956 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957}
958
David Collins6f032ba2011-08-31 14:08:15 -0700959static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
960 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961{
David Collins6f032ba2011-08-31 14:08:15 -0700962 struct vreg *vreg = rdev_get_drvdata(rdev);
963 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964
David Collins6f032ba2011-08-31 14:08:15 -0700965 load_uA += vreg->pdata.system_uA;
966
967 mutex_lock(&vreg->pc_lock);
968 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
969 if (config->ia_follows_ip)
970 SET_PART(vreg, ia,
971 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
972 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973
974 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -0700975 mode = config->mode_hpm;
976 else
977 mode = config->mode_lpm;
978
979 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980}
981
David Collins6f032ba2011-08-31 14:08:15 -0700982static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
983 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984{
David Collins6f032ba2011-08-31 14:08:15 -0700985 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986
David Collins6f032ba2011-08-31 14:08:15 -0700987 if (MICRO_TO_MILLI(load_uA) <= 0) {
988 /*
989 * vreg_legacy_get_optimum_mode is being called before consumers
990 * have specified their load currents via
991 * regulator_set_optimum_mode. Return whatever the existing mode
992 * is.
993 */
994 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995 }
996
David Collins6f032ba2011-08-31 14:08:15 -0700997 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998}
999
1000/*
David Collins6f032ba2011-08-31 14:08:15 -07001001 * Returns the logical pin control enable state because the pin control options
1002 * present in the hardware out of restart could be different from those desired
1003 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004 */
David Collins6f032ba2011-08-31 14:08:15 -07001005static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006{
David Collins6f032ba2011-08-31 14:08:15 -07001007 struct vreg *vreg = rdev_get_drvdata(rdev);
1008
1009 return vreg->is_enabled_pc;
1010}
1011
1012static int vreg_pin_control_enable(struct regulator_dev *rdev)
1013{
1014 struct vreg *vreg = rdev_get_drvdata(rdev);
1015 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 int rc;
1017
David Collins6f032ba2011-08-31 14:08:15 -07001018 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019
David Collins6f032ba2011-08-31 14:08:15 -07001020 val[vreg->part->pc.word]
1021 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1022 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023
David Collins6f032ba2011-08-31 14:08:15 -07001024 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1025 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026
David Collins6f032ba2011-08-31 14:08:15 -07001027 if (!vreg->is_enabled)
1028 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029
David Collins6f032ba2011-08-31 14:08:15 -07001030 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1031 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032
David Collins6f032ba2011-08-31 14:08:15 -07001033 if (!rc)
1034 vreg->is_enabled_pc = true;
1035
1036 mutex_unlock(&vreg->pc_lock);
1037
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001039 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040
David Collins6f032ba2011-08-31 14:08:15 -07001041 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042}
1043
David Collins6f032ba2011-08-31 14:08:15 -07001044static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045{
David Collins6f032ba2011-08-31 14:08:15 -07001046 struct vreg *vreg = rdev_get_drvdata(rdev);
1047 unsigned int mask[2] = {0}, val[2] = {0};
1048 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049
David Collins6f032ba2011-08-31 14:08:15 -07001050 mutex_lock(&vreg->pc_lock);
1051
1052 val[vreg->part->pc.word]
1053 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1054 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1055
1056 pin_fn = config->pin_func_none;
1057 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1058 pin_fn = config->pin_func_sleep_b;
1059 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1060 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1061
1062 if (!vreg->is_enabled)
1063 set_disable(vreg, mask, val);
1064
1065 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1066 vreg->part->request_len);
1067
1068 if (!rc)
1069 vreg->is_enabled_pc = false;
1070
1071 mutex_unlock(&vreg->pc_lock);
1072
1073 if (rc)
1074 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1075
1076 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077}
1078
David Collins6f032ba2011-08-31 14:08:15 -07001079static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080{
David Collins6f032ba2011-08-31 14:08:15 -07001081 struct vreg *vreg = rdev_get_drvdata(rdev);
1082
1083 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084}
1085
David Collins6f032ba2011-08-31 14:08:15 -07001086/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087static struct regulator_ops ldo_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001088 .enable = vreg_enable,
1089 .disable = vreg_disable,
1090 .is_enabled = vreg_is_enabled,
1091 .set_voltage = vreg_set_voltage,
1092 .get_voltage = vreg_get_voltage,
1093 .list_voltage = vreg_list_voltage,
1094 .set_mode = vreg_set_mode,
1095 .get_mode = vreg_get_mode,
1096 .get_optimum_mode = vreg_get_optimum_mode,
1097 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098};
1099
1100static struct regulator_ops smps_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001101 .enable = vreg_enable,
1102 .disable = vreg_disable,
1103 .is_enabled = vreg_is_enabled,
1104 .set_voltage = vreg_set_voltage,
1105 .get_voltage = vreg_get_voltage,
1106 .list_voltage = vreg_list_voltage,
1107 .set_mode = vreg_set_mode,
1108 .get_mode = vreg_get_mode,
1109 .get_optimum_mode = vreg_get_optimum_mode,
1110 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111};
1112
1113static struct regulator_ops switch_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001114 .enable = vreg_enable,
1115 .disable = vreg_disable,
1116 .is_enabled = vreg_is_enabled,
1117 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118};
1119
1120static struct regulator_ops ncp_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001121 .enable = vreg_enable,
1122 .disable = vreg_disable,
1123 .is_enabled = vreg_is_enabled,
1124 .set_voltage = vreg_set_voltage,
1125 .get_voltage = vreg_get_voltage,
1126 .list_voltage = vreg_list_voltage,
1127 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128};
1129
David Collins6f032ba2011-08-31 14:08:15 -07001130/* Pin control regulator operations. */
1131static struct regulator_ops pin_control_ops = {
1132 .enable = vreg_pin_control_enable,
1133 .disable = vreg_pin_control_disable,
1134 .is_enabled = vreg_pin_control_is_enabled,
1135};
1136
1137struct regulator_ops *vreg_ops[] = {
1138 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1139 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1140 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1141 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1142};
1143
1144static int __devinit
1145rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1146 struct device *dev)
1147{
1148 struct regulator_desc *rdesc = NULL;
1149 struct regulator_dev *rdev;
1150 struct vreg *vreg;
1151 unsigned pin_ctrl;
1152 int id, pin_fn;
1153 int rc = 0;
1154
1155 if (!pdata) {
1156 pr_err("platform data missing\n");
1157 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158 }
1159
David Collins6f032ba2011-08-31 14:08:15 -07001160 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161
David Collins6f032ba2011-08-31 14:08:15 -07001162 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1163 pr_err("invalid regulator id: %d\n", id);
1164 return -ENODEV;
1165 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166
David Collins6f032ba2011-08-31 14:08:15 -07001167 if (!config->is_real_id(pdata->id))
1168 id = config->pc_id_to_real_id(pdata->id);
1169 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170
David Collins6f032ba2011-08-31 14:08:15 -07001171 if (config->is_real_id(pdata->id))
1172 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173 else
David Collins6f032ba2011-08-31 14:08:15 -07001174 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175
David Collins6f032ba2011-08-31 14:08:15 -07001176 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1177 pr_err("%s: invalid regulator type: %d\n",
1178 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001180 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181
David Collins6f032ba2011-08-31 14:08:15 -07001182 mutex_lock(&vreg->pc_lock);
1183
1184 if (vreg->set_points)
1185 rdesc->n_voltages = vreg->set_points->n_voltages;
1186 else
1187 rdesc->n_voltages = 0;
1188
1189 rdesc->id = pdata->id;
1190 rdesc->owner = THIS_MODULE;
1191 rdesc->type = REGULATOR_VOLTAGE;
1192
1193 if (config->is_real_id(pdata->id)) {
1194 /*
1195 * Real regulator; do not modify pin control and pin function
1196 * values.
1197 */
1198 rdesc->ops = vreg_ops[vreg->type];
1199 pin_ctrl = vreg->pdata.pin_ctrl;
1200 pin_fn = vreg->pdata.pin_fn;
1201 memcpy(&(vreg->pdata), pdata,
1202 sizeof(struct rpm_regulator_init_data));
1203 vreg->pdata.pin_ctrl = pin_ctrl;
1204 vreg->pdata.pin_fn = pin_fn;
1205
1206 vreg->save_uV = vreg->pdata.default_uV;
1207 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1208 vreg->mode = config->mode_hpm;
1209 else
1210 vreg->mode = config->mode_lpm;
1211
1212 /* Initialize the RPM request. */
1213 SET_PART(vreg, ip,
1214 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1215 SET_PART(vreg, fm, vreg->pdata.force_mode);
1216 SET_PART(vreg, pm, vreg->pdata.power_mode);
1217 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1218 SET_PART(vreg, ia,
1219 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1220 SET_PART(vreg, freq, vreg->pdata.freq);
1221 SET_PART(vreg, freq_clk_src, 0);
1222 SET_PART(vreg, comp_mode, 0);
1223 SET_PART(vreg, hpm, 0);
1224 if (!vreg->is_enabled_pc) {
1225 SET_PART(vreg, pf, config->pin_func_none);
1226 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1227 }
1228 } else {
1229 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1230 == RPM_VREG_PIN_CTRL_NONE
1231 && pdata->pin_fn != config->pin_func_sleep_b) {
1232 pr_err("%s: no pin control input specified\n",
1233 vreg->rdesc.name);
1234 mutex_unlock(&vreg->pc_lock);
1235 return -EINVAL;
1236 }
1237 rdesc->ops = &pin_control_ops;
1238 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1239 vreg->pdata.pin_fn = pdata->pin_fn;
1240
1241 /* Initialize the RPM request. */
1242 pin_fn = config->pin_func_none;
1243 /* Allow pf=sleep_b to be specified by platform data. */
1244 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1245 pin_fn = config->pin_func_sleep_b;
1246 SET_PART(vreg, pf, pin_fn);
1247 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1248 }
1249
1250 mutex_unlock(&vreg->pc_lock);
1251
1252 if (rc)
1253 goto bail;
1254
1255 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1256 if (IS_ERR(rdev)) {
1257 rc = PTR_ERR(rdev);
1258 pr_err("regulator_register failed: %s, rc=%d\n",
1259 vreg->rdesc.name, rc);
1260 return rc;
1261 } else {
1262 if (config->is_real_id(pdata->id))
1263 vreg->rdev = rdev;
1264 else
1265 vreg->rdev_pc = rdev;
1266 }
1267
1268bail:
1269 if (rc)
1270 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1271
1272 return rc;
1273}
1274
1275static void rpm_vreg_set_point_init(void)
1276{
1277 struct vreg_set_points **set_points;
1278 int i, j, temp;
1279
1280 set_points = config->set_points;
1281
1282 /* Calculate the number of set points available for each regulator. */
1283 for (i = 0; i < config->set_points_len; i++) {
1284 temp = 0;
1285 for (j = 0; j < set_points[i]->count; j++) {
1286 set_points[i]->range[j].n_voltages
1287 = (set_points[i]->range[j].max_uV
1288 - set_points[i]->range[j].min_uV)
1289 / set_points[i]->range[j].step_uV + 1;
1290 temp += set_points[i]->range[j].n_voltages;
1291 }
1292 set_points[i]->n_voltages = temp;
1293 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294}
1295
1296static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1297{
David Collins6f032ba2011-08-31 14:08:15 -07001298 struct rpm_regulator_platform_data *platform_data;
1299 int rc = 0;
1300 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301
David Collins6f032ba2011-08-31 14:08:15 -07001302 platform_data = pdev->dev.platform_data;
1303 if (!platform_data) {
1304 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 }
1307
David Collins6f032ba2011-08-31 14:08:15 -07001308 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1309 && platform_data->version != rpm_version) {
1310 pr_err("rpm version %d does not match previous version %d\n",
1311 platform_data->version, rpm_version);
1312 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 }
1314
David Collins6f032ba2011-08-31 14:08:15 -07001315 if (platform_data->version < 0
1316 || platform_data->version > RPM_VREG_VERSION_MAX) {
1317 pr_err("rpm version %d is invalid\n", platform_data->version);
1318 return -EINVAL;
1319 }
1320
1321 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1322 rpm_version = platform_data->version;
1323 config = get_config[platform_data->version]();
1324 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1325 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1326 if (!config) {
1327 pr_err("rpm version %d is not available\n",
1328 platform_data->version);
1329 return -ENODEV;
1330 }
1331 if (config->use_legacy_optimum_mode)
1332 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1333 vreg_ops[i]->get_optimum_mode
1334 = vreg_legacy_get_optimum_mode;
1335 rpm_vreg_set_point_init();
1336 /* First time probed; initialize pin control mutexes. */
1337 for (i = 0; i < config->vregs_len; i++)
1338 mutex_init(&config->vregs[i].pc_lock);
1339 }
1340
1341 /* Initialize all of the regulators listed in the platform data. */
1342 for (i = 0; i < platform_data->num_regulators; i++) {
1343 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1344 &pdev->dev);
1345 if (rc) {
1346 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1347 goto remove_regulators;
1348 }
1349 }
1350
1351 platform_set_drvdata(pdev, platform_data);
1352
1353 return rc;
1354
1355remove_regulators:
1356 /* Unregister all regulators added before the erroring one. */
1357 for (; i >= 0; i--) {
1358 id = platform_data->init_data[i].id;
1359 if (config->is_real_id(id)) {
1360 regulator_unregister(config->vregs[id].rdev);
1361 config->vregs[id].rdev = NULL;
1362 } else {
1363 regulator_unregister(config->vregs[
1364 config->pc_id_to_real_id(id)].rdev_pc);
1365 config->vregs[id].rdev_pc = NULL;
1366 }
1367 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001368
1369 return rc;
1370}
1371
1372static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1373{
David Collins6f032ba2011-08-31 14:08:15 -07001374 struct rpm_regulator_platform_data *platform_data;
1375 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001376
David Collins6f032ba2011-08-31 14:08:15 -07001377 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001379
1380 if (platform_data) {
1381 for (i = 0; i < platform_data->num_regulators; i++) {
1382 id = platform_data->init_data[i].id;
1383 if (config->is_real_id(id)) {
1384 regulator_unregister(config->vregs[id].rdev);
1385 config->vregs[id].rdev = NULL;
1386 } else {
1387 regulator_unregister(config->vregs[
1388 config->pc_id_to_real_id(id)].rdev_pc);
1389 config->vregs[id].rdev_pc = NULL;
1390 }
1391 }
1392 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393
1394 return 0;
1395}
1396
1397static struct platform_driver rpm_vreg_driver = {
1398 .probe = rpm_vreg_probe,
1399 .remove = __devexit_p(rpm_vreg_remove),
1400 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001401 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 .owner = THIS_MODULE,
1403 },
1404};
1405
1406static int __init rpm_vreg_init(void)
1407{
1408 return platform_driver_register(&rpm_vreg_driver);
1409}
1410
1411static void __exit rpm_vreg_exit(void)
1412{
David Collins6f032ba2011-08-31 14:08:15 -07001413 int i;
1414
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001416
1417 for (i = 0; i < config->vregs_len; i++)
1418 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419}
1420
1421postcore_initcall(rpm_vreg_init);
1422module_exit(rpm_vreg_exit);
1423
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001425MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001427MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);