blob: e2ebbd483fce7c7ca5af59275bcdbe091dad6ebe [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;
David Collins3974b612011-11-21 15:07:36 -0800493
494 if (uV > max_uV) {
495 vreg_err(vreg,
496 "request v=[%d, %d] cannot be met by any set point; "
497 "next set point: %d\n",
498 min_uV, max_uV, uV);
499 return -EINVAL;
500 }
David Collins7462b9d2011-10-11 16:02:17 -0700501 }
David Collins6f032ba2011-08-31 14:08:15 -0700502
503 if (vreg->part->uV.mask) {
504 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
505 mask[vreg->part->uV.word] = vreg->part->uV.mask;
506 } else {
507 val[vreg->part->mV.word]
508 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
509 mask[vreg->part->mV.word] = vreg->part->mV.mask;
510 }
511
512 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
513 val[1], vreg->part->request_len, 1);
514 if (rc)
515 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516
517 return rc;
518}
519EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
520
521/**
522 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
523 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700524 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 *
526 * Returns 0 on success or errno.
527 */
David Collins6f032ba2011-08-31 14:08:15 -0700528int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529{
David Collins6f032ba2011-08-31 14:08:15 -0700530 unsigned int mask[2] = {0}, val[2] = {0};
531 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 int rc;
533
David Collins6f032ba2011-08-31 14:08:15 -0700534 /*
535 * HACK: make this function a no-op for 8064 so that it can be called by
536 * consumers on 8064 before RPM capabilities are present.
537 */
538 if (cpu_is_apq8064())
539 return 0;
540
541 if (!config) {
542 pr_err("rpm-regulator driver has not probed yet.\n");
543 return -ENODEV;
544 }
545
546 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
547 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548 return -EINVAL;
549 }
550
David Collins6f032ba2011-08-31 14:08:15 -0700551 vreg = &config->vregs[vreg_id];
552
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700554 vreg_err(vreg, "invalid frequency=%d\n", freq);
555 return -EINVAL;
556 }
557 if (!vreg->pdata.sleep_selectable) {
558 vreg_err(vreg, "regulator is not marked sleep selectable\n");
559 return -EINVAL;
560 }
561 if (!vreg->part->freq.mask) {
562 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563 return -EINVAL;
564 }
565
David Collins6f032ba2011-08-31 14:08:15 -0700566 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
567 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568
David Collins6f032ba2011-08-31 14:08:15 -0700569 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
570 val[0], mask[1], val[1], vreg->part->request_len, 0);
571 if (rc)
572 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573
574 return rc;
575}
576EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
577
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578static inline int vreg_hpm_min_uA(struct vreg *vreg)
579{
580 return vreg->hpm_min_load;
581}
582
583static inline int vreg_lpm_max_uA(struct vreg *vreg)
584{
585 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
586}
587
David Collins6f032ba2011-08-31 14:08:15 -0700588static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589{
David Collins6f032ba2011-08-31 14:08:15 -0700590 unsigned load_max
591 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
592
593 return (load_uA > load_max ? load_max : load_uA);
594}
595
596static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
597{
598 unsigned load_max
599 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
600 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601}
602
603/* Change vreg->req, but do not send it to the RPM. */
604static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
605 unsigned mask1, unsigned val1)
606{
607 unsigned long flags = 0;
608
David Collins6f032ba2011-08-31 14:08:15 -0700609 if (vreg->pdata.sleep_selectable)
610 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611
612 vreg->req[0].value &= ~mask0;
613 vreg->req[0].value |= val0 & mask0;
614
615 vreg->req[1].value &= ~mask1;
616 vreg->req[1].value |= val1 & mask1;
617
David Collins6f032ba2011-08-31 14:08:15 -0700618 if (vreg->pdata.sleep_selectable)
619 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620
621 return 0;
622}
623
624static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
625 unsigned mask1, unsigned val1, unsigned cnt)
626{
627 unsigned prev0 = 0, prev1 = 0;
628 int rc;
629
630 /*
631 * Bypass the normal route for regulators that can be called to change
632 * just the active set values.
633 */
David Collins6f032ba2011-08-31 14:08:15 -0700634 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
636 mask0, val0, mask1, val1, cnt, 1);
637
638 prev0 = vreg->req[0].value;
639 vreg->req[0].value &= ~mask0;
640 vreg->req[0].value |= val0 & mask0;
641
642 prev1 = vreg->req[1].value;
643 vreg->req[1].value &= ~mask1;
644 vreg->req[1].value |= val1 & mask1;
645
646 /* Ignore duplicate requests */
647 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
648 vreg->req[1].value == vreg->prev_active_req[1].value) {
649 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700650 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 return 0;
652 }
653
654 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
655 if (rc) {
656 vreg->req[0].value = prev0;
657 vreg->req[1].value = prev1;
658
David Collins6f032ba2011-08-31 14:08:15 -0700659 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
660 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 } else {
662 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700663 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 vreg->prev_active_req[0].value = vreg->req[0].value;
665 vreg->prev_active_req[1].value = vreg->req[1].value;
666 }
667
668 return rc;
669}
670
David Collins6f032ba2011-08-31 14:08:15 -0700671static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672{
David Collins6f032ba2011-08-31 14:08:15 -0700673 struct vreg *vreg = rdev_get_drvdata(rdev);
674 int enabled;
675
676 mutex_lock(&vreg->pc_lock);
677 enabled = vreg->is_enabled;
678 mutex_unlock(&vreg->pc_lock);
679
680 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681}
682
David Collins6f032ba2011-08-31 14:08:15 -0700683static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684{
David Collins6f032ba2011-08-31 14:08:15 -0700685 switch (vreg->type) {
686 case RPM_REGULATOR_TYPE_LDO:
687 case RPM_REGULATOR_TYPE_SMPS:
688 /* Enable by setting a voltage. */
689 if (vreg->part->uV.mask) {
690 val[vreg->part->uV.word]
691 |= vreg->save_uV << vreg->part->uV.shift;
692 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
693 } else {
694 val[vreg->part->mV.word]
695 |= MICRO_TO_MILLI(vreg->save_uV)
696 << vreg->part->mV.shift;
697 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
698 }
699 break;
700 case RPM_REGULATOR_TYPE_VS:
701 case RPM_REGULATOR_TYPE_NCP:
702 /* Enable by setting enable_state. */
703 val[vreg->part->enable_state.word]
704 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
705 mask[vreg->part->enable_state.word]
706 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708}
709
David Collins6f032ba2011-08-31 14:08:15 -0700710static int vreg_enable(struct regulator_dev *rdev)
711{
712 struct vreg *vreg = rdev_get_drvdata(rdev);
713 unsigned int mask[2] = {0}, val[2] = {0};
714 int rc = 0;
715
716 set_enable(vreg, mask, val);
717
718 mutex_lock(&vreg->pc_lock);
719
720 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
721 vreg->part->request_len);
722 if (!rc)
723 vreg->is_enabled = true;
724
725 mutex_unlock(&vreg->pc_lock);
726
727 if (rc)
728 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
729
730 return rc;
731}
732
733static void set_disable(struct vreg *vreg, unsigned int *mask,
734 unsigned int *val)
735{
736 switch (vreg->type) {
737 case RPM_REGULATOR_TYPE_LDO:
738 case RPM_REGULATOR_TYPE_SMPS:
739 /* Disable by setting a voltage of 0 uV. */
740 if (vreg->part->uV.mask) {
741 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
742 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
743 } else {
744 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
745 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
746 }
747 break;
748 case RPM_REGULATOR_TYPE_VS:
749 case RPM_REGULATOR_TYPE_NCP:
750 /* Disable by setting enable_state. */
751 val[vreg->part->enable_state.word]
752 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
753 mask[vreg->part->enable_state.word]
754 |= vreg->part->enable_state.mask;
755 }
756}
757
758static int vreg_disable(struct regulator_dev *rdev)
759{
760 struct vreg *vreg = rdev_get_drvdata(rdev);
761 unsigned int mask[2] = {0}, val[2] = {0};
762 int rc = 0;
763
764 set_disable(vreg, mask, val);
765
766 mutex_lock(&vreg->pc_lock);
767
768 /* Only disable if pin control is not in use. */
769 if (!vreg->is_enabled_pc)
770 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
771 vreg->part->request_len);
772
773 if (!rc)
774 vreg->is_enabled = false;
775
776 mutex_unlock(&vreg->pc_lock);
777
778 if (rc)
779 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
780
781 return rc;
782}
783
784static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785 unsigned *selector)
786{
David Collins6f032ba2011-08-31 14:08:15 -0700787 struct vreg *vreg = rdev_get_drvdata(rdev);
788 struct vreg_range *range = &vreg->set_points->range[0];
789 unsigned int mask[2] = {0}, val[2] = {0};
790 int rc = 0, uV = min_uV;
791 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792
David Collins6f032ba2011-08-31 14:08:15 -0700793 /* Check if request voltage is outside of physically settable range. */
794 lim_min_uV = vreg->set_points->range[0].min_uV;
795 lim_max_uV =
796 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797
David Collins6f032ba2011-08-31 14:08:15 -0700798 if (uV < lim_min_uV && max_uV >= lim_min_uV)
799 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700800
David Collins6f032ba2011-08-31 14:08:15 -0700801 if (uV < lim_min_uV || uV > lim_max_uV) {
802 vreg_err(vreg,
803 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
804 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 return -EINVAL;
806 }
807
David Collins6f032ba2011-08-31 14:08:15 -0700808 /* Find the range which uV is inside of. */
809 for (i = vreg->set_points->count - 1; i > 0; i--) {
810 if (uV > vreg->set_points->range[i - 1].max_uV) {
811 range = &vreg->set_points->range[i];
812 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 }
814 }
815
David Collins6f032ba2011-08-31 14:08:15 -0700816 /*
817 * Force uV to be an allowed set point and apply a ceiling function
818 * to non-set point values.
819 */
820 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
821 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822
David Collins3974b612011-11-21 15:07:36 -0800823 if (uV > max_uV) {
824 vreg_err(vreg,
825 "request v=[%d, %d] cannot be met by any set point; "
826 "next set point: %d\n",
827 min_uV, max_uV, uV);
828 return -EINVAL;
829 }
830
David Collins6f032ba2011-08-31 14:08:15 -0700831 if (vreg->part->uV.mask) {
832 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
833 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700835 val[vreg->part->mV.word]
836 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
837 mask[vreg->part->mV.word] = vreg->part->mV.mask;
838 }
839
840 mutex_lock(&vreg->pc_lock);
841
842 /*
843 * Only send a request for a new voltage if the regulator is currently
844 * enabled. This will ensure that LDO and SMPS regulators are not
845 * inadvertently turned on because voltage > 0 is equivalent to
846 * enabling. For NCP, this just removes unnecessary RPM requests.
847 */
848 if (vreg->is_enabled) {
849 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
850 vreg->part->request_len);
851 if (rc)
852 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
853 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700855 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856 }
857
David Collins6f032ba2011-08-31 14:08:15 -0700858 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
859 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860
David Collins6f032ba2011-08-31 14:08:15 -0700861 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862
863 return rc;
864}
865
David Collins6f032ba2011-08-31 14:08:15 -0700866static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867{
David Collins6f032ba2011-08-31 14:08:15 -0700868 struct vreg *vreg = rdev_get_drvdata(rdev);
869
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 return vreg->save_uV;
871}
872
David Collins6f032ba2011-08-31 14:08:15 -0700873static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874{
David Collins6f032ba2011-08-31 14:08:15 -0700875 struct vreg *vreg = rdev_get_drvdata(rdev);
876 int uV = 0;
877 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878
David Collins6f032ba2011-08-31 14:08:15 -0700879 if (!vreg->set_points) {
880 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 return -EINVAL;
882 }
883
David Collins6f032ba2011-08-31 14:08:15 -0700884 if (selector >= vreg->set_points->n_voltages)
885 return 0;
886
887 for (i = 0; i < vreg->set_points->count; i++) {
888 if (selector < vreg->set_points->range[i].n_voltages) {
889 uV = selector * vreg->set_points->range[i].step_uV
890 + vreg->set_points->range[i].min_uV;
891 break;
892 } else {
893 selector -= vreg->set_points->range[i].n_voltages;
894 }
895 }
896
897 return uV;
898}
899
900static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
901{
902 struct vreg *vreg = rdev_get_drvdata(rdev);
903 unsigned int mask[2] = {0}, val[2] = {0};
904 int rc = 0;
905 int peak_uA;
906
907 mutex_lock(&vreg->pc_lock);
908
909 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
910 & vreg->part->ip.mask) >> vreg->part->ip.shift);
911
912 if (mode == config->mode_hpm) {
913 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700915 val[vreg->part->ip.word]
916 = MICRO_TO_MILLI(vreg_hpm_min_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_hpm_min_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 if (mode == config->mode_lpm) {
929 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700931 val[vreg->part->ip.word]
932 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
933 << vreg->part->ip.shift;
934 mask[vreg->part->ip.word] = vreg->part->ip.mask;
935
936 if (config->ia_follows_ip) {
937 val[vreg->part->ia.word]
938 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
939 << vreg->part->ia.shift;
940 mask[vreg->part->ia.word]
941 |= vreg->part->ia.mask;
942 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 }
David Collins6f032ba2011-08-31 14:08:15 -0700944 } else {
945 vreg_err(vreg, "invalid mode: %u\n", mode);
946 mutex_unlock(&vreg->pc_lock);
947 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 }
949
David Collins6f032ba2011-08-31 14:08:15 -0700950 if (vreg->is_enabled) {
951 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
952 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 } else {
954 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700955 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 }
David Collins6f032ba2011-08-31 14:08:15 -0700957
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -0700959 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
960 else
961 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962
David Collins6f032ba2011-08-31 14:08:15 -0700963 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964
David Collins6f032ba2011-08-31 14:08:15 -0700965 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966}
967
David Collins6f032ba2011-08-31 14:08:15 -0700968static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969{
David Collins6f032ba2011-08-31 14:08:15 -0700970 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971
David Collins6f032ba2011-08-31 14:08:15 -0700972 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973}
974
David Collins6f032ba2011-08-31 14:08:15 -0700975static unsigned int vreg_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);
979 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980
David Collins6f032ba2011-08-31 14:08:15 -0700981 load_uA += vreg->pdata.system_uA;
982
983 mutex_lock(&vreg->pc_lock);
984 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
985 if (config->ia_follows_ip)
986 SET_PART(vreg, ia,
987 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
988 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989
990 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -0700991 mode = config->mode_hpm;
992 else
993 mode = config->mode_lpm;
994
995 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996}
997
David Collins6f032ba2011-08-31 14:08:15 -0700998static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
999 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000{
David Collins6f032ba2011-08-31 14:08:15 -07001001 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002
David Collins6f032ba2011-08-31 14:08:15 -07001003 if (MICRO_TO_MILLI(load_uA) <= 0) {
1004 /*
1005 * vreg_legacy_get_optimum_mode is being called before consumers
1006 * have specified their load currents via
1007 * regulator_set_optimum_mode. Return whatever the existing mode
1008 * is.
1009 */
1010 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 }
1012
David Collins6f032ba2011-08-31 14:08:15 -07001013 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014}
1015
1016/*
David Collins6f032ba2011-08-31 14:08:15 -07001017 * Returns the logical pin control enable state because the pin control options
1018 * present in the hardware out of restart could be different from those desired
1019 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 */
David Collins6f032ba2011-08-31 14:08:15 -07001021static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022{
David Collins6f032ba2011-08-31 14:08:15 -07001023 struct vreg *vreg = rdev_get_drvdata(rdev);
1024
1025 return vreg->is_enabled_pc;
1026}
1027
1028static int vreg_pin_control_enable(struct regulator_dev *rdev)
1029{
1030 struct vreg *vreg = rdev_get_drvdata(rdev);
1031 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 int rc;
1033
David Collins6f032ba2011-08-31 14:08:15 -07001034 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035
David Collins6f032ba2011-08-31 14:08:15 -07001036 val[vreg->part->pc.word]
1037 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1038 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039
David Collins6f032ba2011-08-31 14:08:15 -07001040 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1041 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042
David Collins6f032ba2011-08-31 14:08:15 -07001043 if (!vreg->is_enabled)
1044 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045
David Collins6f032ba2011-08-31 14:08:15 -07001046 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1047 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048
David Collins6f032ba2011-08-31 14:08:15 -07001049 if (!rc)
1050 vreg->is_enabled_pc = true;
1051
1052 mutex_unlock(&vreg->pc_lock);
1053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001055 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056
David Collins6f032ba2011-08-31 14:08:15 -07001057 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058}
1059
David Collins6f032ba2011-08-31 14:08:15 -07001060static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061{
David Collins6f032ba2011-08-31 14:08:15 -07001062 struct vreg *vreg = rdev_get_drvdata(rdev);
1063 unsigned int mask[2] = {0}, val[2] = {0};
1064 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065
David Collins6f032ba2011-08-31 14:08:15 -07001066 mutex_lock(&vreg->pc_lock);
1067
1068 val[vreg->part->pc.word]
1069 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1070 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1071
1072 pin_fn = config->pin_func_none;
1073 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1074 pin_fn = config->pin_func_sleep_b;
1075 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1076 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1077
1078 if (!vreg->is_enabled)
1079 set_disable(vreg, mask, val);
1080
1081 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1082 vreg->part->request_len);
1083
1084 if (!rc)
1085 vreg->is_enabled_pc = false;
1086
1087 mutex_unlock(&vreg->pc_lock);
1088
1089 if (rc)
1090 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1091
1092 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093}
1094
David Collins6f032ba2011-08-31 14:08:15 -07001095static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096{
David Collins6f032ba2011-08-31 14:08:15 -07001097 struct vreg *vreg = rdev_get_drvdata(rdev);
1098
1099 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100}
1101
David Collins6f032ba2011-08-31 14:08:15 -07001102/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103static struct regulator_ops ldo_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001104 .enable = vreg_enable,
1105 .disable = vreg_disable,
1106 .is_enabled = vreg_is_enabled,
1107 .set_voltage = vreg_set_voltage,
1108 .get_voltage = vreg_get_voltage,
1109 .list_voltage = vreg_list_voltage,
1110 .set_mode = vreg_set_mode,
1111 .get_mode = vreg_get_mode,
1112 .get_optimum_mode = vreg_get_optimum_mode,
1113 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114};
1115
1116static struct regulator_ops smps_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001117 .enable = vreg_enable,
1118 .disable = vreg_disable,
1119 .is_enabled = vreg_is_enabled,
1120 .set_voltage = vreg_set_voltage,
1121 .get_voltage = vreg_get_voltage,
1122 .list_voltage = vreg_list_voltage,
1123 .set_mode = vreg_set_mode,
1124 .get_mode = vreg_get_mode,
1125 .get_optimum_mode = vreg_get_optimum_mode,
1126 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127};
1128
1129static struct regulator_ops switch_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001130 .enable = vreg_enable,
1131 .disable = vreg_disable,
1132 .is_enabled = vreg_is_enabled,
1133 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001134};
1135
1136static struct regulator_ops ncp_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001137 .enable = vreg_enable,
1138 .disable = vreg_disable,
1139 .is_enabled = vreg_is_enabled,
1140 .set_voltage = vreg_set_voltage,
1141 .get_voltage = vreg_get_voltage,
1142 .list_voltage = vreg_list_voltage,
1143 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144};
1145
David Collins6f032ba2011-08-31 14:08:15 -07001146/* Pin control regulator operations. */
1147static struct regulator_ops pin_control_ops = {
1148 .enable = vreg_pin_control_enable,
1149 .disable = vreg_pin_control_disable,
1150 .is_enabled = vreg_pin_control_is_enabled,
1151};
1152
1153struct regulator_ops *vreg_ops[] = {
1154 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1155 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1156 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1157 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1158};
1159
1160static int __devinit
1161rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1162 struct device *dev)
1163{
1164 struct regulator_desc *rdesc = NULL;
1165 struct regulator_dev *rdev;
1166 struct vreg *vreg;
1167 unsigned pin_ctrl;
1168 int id, pin_fn;
1169 int rc = 0;
1170
1171 if (!pdata) {
1172 pr_err("platform data missing\n");
1173 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 }
1175
David Collins6f032ba2011-08-31 14:08:15 -07001176 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177
David Collins6f032ba2011-08-31 14:08:15 -07001178 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1179 pr_err("invalid regulator id: %d\n", id);
1180 return -ENODEV;
1181 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182
David Collins6f032ba2011-08-31 14:08:15 -07001183 if (!config->is_real_id(pdata->id))
1184 id = config->pc_id_to_real_id(pdata->id);
1185 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186
David Collins6f032ba2011-08-31 14:08:15 -07001187 if (config->is_real_id(pdata->id))
1188 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 else
David Collins6f032ba2011-08-31 14:08:15 -07001190 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191
David Collins6f032ba2011-08-31 14:08:15 -07001192 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1193 pr_err("%s: invalid regulator type: %d\n",
1194 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001196 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001197
David Collins6f032ba2011-08-31 14:08:15 -07001198 mutex_lock(&vreg->pc_lock);
1199
1200 if (vreg->set_points)
1201 rdesc->n_voltages = vreg->set_points->n_voltages;
1202 else
1203 rdesc->n_voltages = 0;
1204
1205 rdesc->id = pdata->id;
1206 rdesc->owner = THIS_MODULE;
1207 rdesc->type = REGULATOR_VOLTAGE;
1208
1209 if (config->is_real_id(pdata->id)) {
1210 /*
1211 * Real regulator; do not modify pin control and pin function
1212 * values.
1213 */
1214 rdesc->ops = vreg_ops[vreg->type];
1215 pin_ctrl = vreg->pdata.pin_ctrl;
1216 pin_fn = vreg->pdata.pin_fn;
1217 memcpy(&(vreg->pdata), pdata,
1218 sizeof(struct rpm_regulator_init_data));
1219 vreg->pdata.pin_ctrl = pin_ctrl;
1220 vreg->pdata.pin_fn = pin_fn;
1221
1222 vreg->save_uV = vreg->pdata.default_uV;
1223 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1224 vreg->mode = config->mode_hpm;
1225 else
1226 vreg->mode = config->mode_lpm;
1227
1228 /* Initialize the RPM request. */
1229 SET_PART(vreg, ip,
1230 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1231 SET_PART(vreg, fm, vreg->pdata.force_mode);
1232 SET_PART(vreg, pm, vreg->pdata.power_mode);
1233 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1234 SET_PART(vreg, ia,
1235 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1236 SET_PART(vreg, freq, vreg->pdata.freq);
1237 SET_PART(vreg, freq_clk_src, 0);
1238 SET_PART(vreg, comp_mode, 0);
1239 SET_PART(vreg, hpm, 0);
1240 if (!vreg->is_enabled_pc) {
1241 SET_PART(vreg, pf, config->pin_func_none);
1242 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1243 }
1244 } else {
1245 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1246 == RPM_VREG_PIN_CTRL_NONE
1247 && pdata->pin_fn != config->pin_func_sleep_b) {
1248 pr_err("%s: no pin control input specified\n",
1249 vreg->rdesc.name);
1250 mutex_unlock(&vreg->pc_lock);
1251 return -EINVAL;
1252 }
1253 rdesc->ops = &pin_control_ops;
1254 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1255 vreg->pdata.pin_fn = pdata->pin_fn;
1256
1257 /* Initialize the RPM request. */
1258 pin_fn = config->pin_func_none;
1259 /* Allow pf=sleep_b to be specified by platform data. */
1260 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1261 pin_fn = config->pin_func_sleep_b;
1262 SET_PART(vreg, pf, pin_fn);
1263 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1264 }
1265
1266 mutex_unlock(&vreg->pc_lock);
1267
1268 if (rc)
1269 goto bail;
1270
1271 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1272 if (IS_ERR(rdev)) {
1273 rc = PTR_ERR(rdev);
1274 pr_err("regulator_register failed: %s, rc=%d\n",
1275 vreg->rdesc.name, rc);
1276 return rc;
1277 } else {
1278 if (config->is_real_id(pdata->id))
1279 vreg->rdev = rdev;
1280 else
1281 vreg->rdev_pc = rdev;
1282 }
1283
1284bail:
1285 if (rc)
1286 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1287
1288 return rc;
1289}
1290
1291static void rpm_vreg_set_point_init(void)
1292{
1293 struct vreg_set_points **set_points;
1294 int i, j, temp;
1295
1296 set_points = config->set_points;
1297
1298 /* Calculate the number of set points available for each regulator. */
1299 for (i = 0; i < config->set_points_len; i++) {
1300 temp = 0;
1301 for (j = 0; j < set_points[i]->count; j++) {
1302 set_points[i]->range[j].n_voltages
1303 = (set_points[i]->range[j].max_uV
1304 - set_points[i]->range[j].min_uV)
1305 / set_points[i]->range[j].step_uV + 1;
1306 temp += set_points[i]->range[j].n_voltages;
1307 }
1308 set_points[i]->n_voltages = temp;
1309 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310}
1311
1312static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1313{
David Collins6f032ba2011-08-31 14:08:15 -07001314 struct rpm_regulator_platform_data *platform_data;
1315 int rc = 0;
1316 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317
David Collins6f032ba2011-08-31 14:08:15 -07001318 platform_data = pdev->dev.platform_data;
1319 if (!platform_data) {
1320 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322 }
1323
David Collins6f032ba2011-08-31 14:08:15 -07001324 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1325 && platform_data->version != rpm_version) {
1326 pr_err("rpm version %d does not match previous version %d\n",
1327 platform_data->version, rpm_version);
1328 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 }
1330
David Collins6f032ba2011-08-31 14:08:15 -07001331 if (platform_data->version < 0
1332 || platform_data->version > RPM_VREG_VERSION_MAX) {
1333 pr_err("rpm version %d is invalid\n", platform_data->version);
1334 return -EINVAL;
1335 }
1336
1337 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1338 rpm_version = platform_data->version;
1339 config = get_config[platform_data->version]();
1340 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1341 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1342 if (!config) {
1343 pr_err("rpm version %d is not available\n",
1344 platform_data->version);
1345 return -ENODEV;
1346 }
1347 if (config->use_legacy_optimum_mode)
1348 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1349 vreg_ops[i]->get_optimum_mode
1350 = vreg_legacy_get_optimum_mode;
1351 rpm_vreg_set_point_init();
1352 /* First time probed; initialize pin control mutexes. */
1353 for (i = 0; i < config->vregs_len; i++)
1354 mutex_init(&config->vregs[i].pc_lock);
1355 }
1356
1357 /* Initialize all of the regulators listed in the platform data. */
1358 for (i = 0; i < platform_data->num_regulators; i++) {
1359 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1360 &pdev->dev);
1361 if (rc) {
1362 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1363 goto remove_regulators;
1364 }
1365 }
1366
1367 platform_set_drvdata(pdev, platform_data);
1368
1369 return rc;
1370
1371remove_regulators:
1372 /* Unregister all regulators added before the erroring one. */
1373 for (; i >= 0; i--) {
1374 id = platform_data->init_data[i].id;
1375 if (config->is_real_id(id)) {
1376 regulator_unregister(config->vregs[id].rdev);
1377 config->vregs[id].rdev = NULL;
1378 } else {
1379 regulator_unregister(config->vregs[
1380 config->pc_id_to_real_id(id)].rdev_pc);
1381 config->vregs[id].rdev_pc = NULL;
1382 }
1383 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384
1385 return rc;
1386}
1387
1388static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1389{
David Collins6f032ba2011-08-31 14:08:15 -07001390 struct rpm_regulator_platform_data *platform_data;
1391 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392
David Collins6f032ba2011-08-31 14:08:15 -07001393 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001395
1396 if (platform_data) {
1397 for (i = 0; i < platform_data->num_regulators; i++) {
1398 id = platform_data->init_data[i].id;
1399 if (config->is_real_id(id)) {
1400 regulator_unregister(config->vregs[id].rdev);
1401 config->vregs[id].rdev = NULL;
1402 } else {
1403 regulator_unregister(config->vregs[
1404 config->pc_id_to_real_id(id)].rdev_pc);
1405 config->vregs[id].rdev_pc = NULL;
1406 }
1407 }
1408 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409
1410 return 0;
1411}
1412
1413static struct platform_driver rpm_vreg_driver = {
1414 .probe = rpm_vreg_probe,
1415 .remove = __devexit_p(rpm_vreg_remove),
1416 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001417 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418 .owner = THIS_MODULE,
1419 },
1420};
1421
1422static int __init rpm_vreg_init(void)
1423{
1424 return platform_driver_register(&rpm_vreg_driver);
1425}
1426
1427static void __exit rpm_vreg_exit(void)
1428{
David Collins6f032ba2011-08-31 14:08:15 -07001429 int i;
1430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001432
1433 for (i = 0; i < config->vregs_len; i++)
1434 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435}
1436
1437postcore_initcall(rpm_vreg_init);
1438module_exit(rpm_vreg_exit);
1439
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001440MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001441MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001442MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001443MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);