blob: 3de01794797e8abbf64bd3a9a17496cd86f02cc8 [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,
48};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
David Collins6f032ba2011-08-31 14:08:15 -070050#define SET_PART(_vreg, _part, _val) \
51 _vreg->req[_vreg->part->_part.word].value \
52 = (_vreg->req[_vreg->part->_part.word].value \
53 & ~vreg->part->_part.mask) \
54 | (((_val) << vreg->part->_part.shift) & vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055
David Collins6f032ba2011-08-31 14:08:15 -070056#define GET_PART(_vreg, _part) \
57 ((_vreg->req[_vreg->part->_part.word].value & vreg->part->_part.mask) \
58 >> vreg->part->_part.shift)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059
David Collins6f032ba2011-08-31 14:08:15 -070060#define USES_PART(_vreg, _part) (vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061
David Collins6f032ba2011-08-31 14:08:15 -070062#define vreg_err(vreg, fmt, ...) \
63 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064
David Collins6f032ba2011-08-31 14:08:15 -070065#define RPM_VREG_PIN_CTRL_EN0 0x01
66#define RPM_VREG_PIN_CTRL_EN1 0x02
67#define RPM_VREG_PIN_CTRL_EN2 0x04
68#define RPM_VREG_PIN_CTRL_EN3 0x08
69#define RPM_VREG_PIN_CTRL_ALL 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070
David Collins6f032ba2011-08-31 14:08:15 -070071static const char *label_freq[] = {
72 [RPM_VREG_FREQ_NONE] = " N/A",
73 [RPM_VREG_FREQ_19p20] = "19.2",
74 [RPM_VREG_FREQ_9p60] = "9.60",
75 [RPM_VREG_FREQ_6p40] = "6.40",
76 [RPM_VREG_FREQ_4p80] = "4.80",
77 [RPM_VREG_FREQ_3p84] = "3.84",
78 [RPM_VREG_FREQ_3p20] = "3.20",
79 [RPM_VREG_FREQ_2p74] = "2.74",
80 [RPM_VREG_FREQ_2p40] = "2.40",
81 [RPM_VREG_FREQ_2p13] = "2.13",
82 [RPM_VREG_FREQ_1p92] = "1.92",
83 [RPM_VREG_FREQ_1p75] = "1.75",
84 [RPM_VREG_FREQ_1p60] = "1.60",
85 [RPM_VREG_FREQ_1p48] = "1.48",
86 [RPM_VREG_FREQ_1p37] = "1.37",
87 [RPM_VREG_FREQ_1p28] = "1.28",
88 [RPM_VREG_FREQ_1p20] = "1.20",
89};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090/*
91 * This is used when voting for LPM or HPM by subtracting or adding to the
92 * hpm_min_load of a regulator. It has units of uA.
93 */
David Collins6f032ba2011-08-31 14:08:15 -070094#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095
David Collins6f032ba2011-08-31 14:08:15 -070096/* rpm_version keeps track of the version for the currently running driver. */
97enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098
David Collins6f032ba2011-08-31 14:08:15 -070099/* config holds all configuration data of the currently running driver. */
100static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101
David Collins6f032ba2011-08-31 14:08:15 -0700102/* These regulator ID values are specified in the board file. */
103static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104
David Collins6f032ba2011-08-31 14:08:15 -0700105static inline int vreg_id_is_vdd_mem_or_dig(int id)
106{
107 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
108}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
David Collins6f032ba2011-08-31 14:08:15 -0700110#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111
David Collins6f032ba2011-08-31 14:08:15 -0700112static void rpm_regulator_req(struct vreg *vreg, int set)
113{
114 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
115 const char *pf_label = "", *fm_label = "", *pc_total = "";
116 const char *pc_en[4] = {"", "", "", ""};
117 const char *pm_label = "", *freq_label = "";
118 char buf[DEBUG_PRINT_BUFFER_SIZE];
119 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
120 int pos = 0;
121
122 /* Suppress VDD_MEM and VDD_DIG printing. */
123 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
124 && vreg_id_is_vdd_mem_or_dig(vreg->id))
125 return;
126
127 uV = GET_PART(vreg, uV);
128 mV = GET_PART(vreg, mV);
129 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
130 uV = -uV;
131 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 }
133
David Collins6f032ba2011-08-31 14:08:15 -0700134 fm = GET_PART(vreg, fm);
135 pm = GET_PART(vreg, pm);
136 pc = GET_PART(vreg, pc);
137 pf = GET_PART(vreg, pf);
138 pd = GET_PART(vreg, pd);
139 freq = GET_PART(vreg, freq);
140 state = GET_PART(vreg, enable_state);
141
142 if (pf >= 0 && pf < config->label_pin_func_len)
143 pf_label = config->label_pin_func[pf];
144
145 if (fm >= 0 && fm < config->label_force_mode_len)
146 fm_label = config->label_force_mode[fm];
147
148 if (pm >= 0 && pm < config->label_power_mode_len)
149 pm_label = config->label_power_mode[pm];
150
151 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
152 freq_label = label_freq[freq];
153
154 for (i = 0; i < config->label_pin_ctrl_len; i++)
155 if (pc & (1 << i))
156 pc_en[i] = config->label_pin_ctrl[i];
157
158 if (pc == RPM_VREG_PIN_CTRL_NONE)
159 pc_total = " none";
160
161 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
162 KERN_INFO, __func__);
163
164 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
165 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
166 vreg->rdesc.name,
167 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
168
169 if (USES_PART(vreg, uV))
170 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
171 if (USES_PART(vreg, mV))
172 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
173 if (USES_PART(vreg, enable_state))
174 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
175 (state == 1 ? "on" : "off"), state);
176 if (USES_PART(vreg, ip))
177 pos += scnprintf(buf + pos, buflen - pos,
178 ", ip=%4d mA", GET_PART(vreg, ip));
179 if (USES_PART(vreg, fm))
180 pos += scnprintf(buf + pos, buflen - pos,
181 ", fm=%s (%d)", fm_label, fm);
182 if (USES_PART(vreg, pc))
183 pos += scnprintf(buf + pos, buflen - pos,
184 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
185 pc_en[2], pc_en[3], pc_total, pc);
186 if (USES_PART(vreg, pf))
187 pos += scnprintf(buf + pos, buflen - pos,
188 ", pf=%s (%d)", pf_label, pf);
189 if (USES_PART(vreg, pd))
190 pos += scnprintf(buf + pos, buflen - pos,
191 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
192 if (USES_PART(vreg, ia))
193 pos += scnprintf(buf + pos, buflen - pos,
194 ", ia=%4d mA", GET_PART(vreg, ia));
195 if (USES_PART(vreg, freq)) {
196 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
197 pos += scnprintf(buf + pos, buflen - pos,
198 ", freq=%2d", freq);
199 else
200 pos += scnprintf(buf + pos, buflen - pos,
201 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 }
David Collins6f032ba2011-08-31 14:08:15 -0700203 if (USES_PART(vreg, pm))
204 pos += scnprintf(buf + pos, buflen - pos,
205 ", pm=%s (%d)", pm_label, pm);
206 if (USES_PART(vreg, freq_clk_src))
207 pos += scnprintf(buf + pos, buflen - pos,
208 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
209 if (USES_PART(vreg, comp_mode))
210 pos += scnprintf(buf + pos, buflen - pos,
211 ", comp=%d", GET_PART(vreg, comp_mode));
212 if (USES_PART(vreg, hpm))
213 pos += scnprintf(buf + pos, buflen - pos,
214 ", hpm=%d", GET_PART(vreg, hpm));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215
David Collins6f032ba2011-08-31 14:08:15 -0700216 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
217 vreg->req[0].id, vreg->req[0].value);
218 if (vreg->part->request_len > 1)
219 pos += scnprintf(buf + pos, buflen - pos,
220 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
221 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222
David Collins6f032ba2011-08-31 14:08:15 -0700223 pos += scnprintf(buf + pos, buflen - pos, "\n");
224 printk(buf);
225}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226
David Collins6f032ba2011-08-31 14:08:15 -0700227static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
228 int set, int voter_uV, int aggregate_uV)
229{
230 /* Suppress VDD_MEM and VDD_DIG printing. */
231 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
232 && vreg_id_is_vdd_mem_or_dig(vreg->id))
233 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234
David Collins6f032ba2011-08-31 14:08:15 -0700235 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
236 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
237 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
238}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239
David Collins6f032ba2011-08-31 14:08:15 -0700240static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
241{
242 /* Suppress VDD_MEM and VDD_DIG printing. */
243 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
244 && vreg_id_is_vdd_mem_or_dig(vreg->id))
245 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246
David Collins6f032ba2011-08-31 14:08:15 -0700247 if (cnt == 2)
248 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
249 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
250 (set == 0 ? 'A' : 'S'),
251 vreg->req[0].id, vreg->req[0].value,
252 vreg->req[1].id, vreg->req[1].value);
253 else if (cnt == 1)
254 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
255 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
256 vreg->req[0].id, vreg->req[0].value);
257}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258
259/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700260static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261
262static int voltage_from_req(struct vreg *vreg)
263{
David Collins6f032ba2011-08-31 14:08:15 -0700264 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265
David Collins6f032ba2011-08-31 14:08:15 -0700266 if (vreg->part->uV.mask)
267 uV = GET_PART(vreg, uV);
268 else
269 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270
David Collins6f032ba2011-08-31 14:08:15 -0700271 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272}
273
David Collins6f032ba2011-08-31 14:08:15 -0700274static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275{
David Collins6f032ba2011-08-31 14:08:15 -0700276 if (vreg->part->uV.mask)
277 SET_PART(vreg, uV, uV);
278 else
279 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280}
281
282static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
283 int set, unsigned mask0, unsigned val0,
284 unsigned mask1, unsigned val1, unsigned cnt,
285 int update_voltage)
286{
287 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700288 int rc = 0, max_uV_vote = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700290 int *min_uV_vote;
291 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292
293 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700294 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 prev_req = vreg->prev_active_req;
296 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700297 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 prev_req = vreg->prev_sleep_req;
299 }
300
301 prev0 = vreg->req[0].value;
302 vreg->req[0].value &= ~mask0;
303 vreg->req[0].value |= val0 & mask0;
304
305 prev1 = vreg->req[1].value;
306 vreg->req[1].value &= ~mask1;
307 vreg->req[1].value |= val1 & mask1;
308
309 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700310 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311
312 /* Find the highest voltage voted for and use it. */
313 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700314 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
315 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316
317 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700318 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
319 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320
321 /* Ignore duplicate requests */
322 if (vreg->req[0].value != prev_req[0].value ||
323 vreg->req[1].value != prev_req[1].value) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
325 if (rc) {
326 vreg->req[0].value = prev0;
327 vreg->req[1].value = prev1;
328
David Collins6f032ba2011-08-31 14:08:15 -0700329 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
330 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
332 vreg->req[0].id, rc);
333 } else {
334 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700335 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
336 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 if (msm_rpm_vreg_debug_mask
338 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700339 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 prev_req[0].value = vreg->req[0].value;
341 prev_req[1].value = vreg->req[1].value;
342 }
343 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700344 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345 }
346
347 return rc;
348}
349
350static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
351 int sleep, unsigned mask0, unsigned val0,
352 unsigned mask1, unsigned val1, unsigned cnt,
353 int update_voltage)
354{
David Collins6f032ba2011-08-31 14:08:15 -0700355 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 unsigned long flags;
357 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358
359 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
360 return -EINVAL;
361
David Collins6f032ba2011-08-31 14:08:15 -0700362 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363
364 /*
365 * Send sleep set request first so that subsequent set_mode, etc calls
366 * use the voltage from the active set.
367 */
368 if (sleep)
369 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
370 mask0, val0, mask1, val1, cnt, update_voltage);
371 else {
372 /*
373 * Vote for 0 V in the sleep set when active set-only is
374 * specified. This ensures that a disable vote will be issued
375 * at some point for the sleep set of the regulator.
376 */
David Collins6f032ba2011-08-31 14:08:15 -0700377 if (vreg->part->uV.mask) {
378 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
379 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
380 } else {
381 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
382 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 }
384
385 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700386 s_mask[0], s_val[0], s_mask[1], s_val[1],
387 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 }
389
390 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
391 mask1, val1, cnt, update_voltage);
392
David Collins6f032ba2011-08-31 14:08:15 -0700393 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394
395 return rc;
396}
397
398/**
399 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
400 * @vreg: ID for regulator
401 * @voter: ID for the voter
402 * @min_uV: minimum acceptable voltage (in uV) that is voted for
403 * @max_uV: maximum acceptable voltage (in uV) that is voted for
404 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
405 *
406 * Returns 0 on success or errno.
407 *
408 * This function is used to vote for the voltage of a regulator without
409 * using the regulator framework. It is needed by consumers which hold spin
410 * locks or have interrupts disabled because the regulator framework can sleep.
411 * It is also needed by consumers which wish to only vote for active set
412 * regulator voltage.
413 *
414 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
415 *
416 * This function may only be called for regulators which have the sleep flag
417 * specified in their private data.
418 */
David Collins6f032ba2011-08-31 14:08:15 -0700419int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
420 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421{
David Collins6f032ba2011-08-31 14:08:15 -0700422 unsigned int mask[2] = {0}, val[2] = {0};
423 struct vreg_range *range;
424 struct vreg *vreg;
425 int uV = min_uV;
426 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427
David Collins6f032ba2011-08-31 14:08:15 -0700428 /*
429 * HACK: make this function a no-op for 8064 so that it can be called by
430 * consumers on 8064 before RPM capabilities are present. (needed for
431 * acpuclock driver)
432 */
433 if (cpu_is_apq8064())
434 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435
David Collins6f032ba2011-08-31 14:08:15 -0700436 if (!config) {
437 pr_err("rpm-regulator driver has not probed yet.\n");
438 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439 }
440
David Collins6f032ba2011-08-31 14:08:15 -0700441 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
442 pr_err("invalid regulator id=%d\n", vreg_id);
443 return -EINVAL;
444 }
445
446 vreg = &config->vregs[vreg_id];
447 range = &vreg->set_points->range[0];
448
449 if (!vreg->pdata.sleep_selectable) {
450 vreg_err(vreg, "regulator is not marked sleep selectable\n");
451 return -EINVAL;
452 }
453
454 /*
455 * Check if request voltage is outside of allowed range. The regulator
456 * core has already checked that constraint range is inside of the
457 * physically allowed range.
458 */
459 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
460 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
461
462 if (uV < lim_min_uV && max_uV >= lim_min_uV)
463 uV = lim_min_uV;
464
465 if (uV < lim_min_uV || uV > lim_max_uV) {
466 vreg_err(vreg,
467 "request v=[%d, %d] is outside allowed v=[%d, %d]\n",
468 min_uV, max_uV, lim_min_uV, lim_max_uV);
469 return -EINVAL;
470 }
471
472 /* Find the range which uV is inside of. */
473 for (i = vreg->set_points->count - 1; i > 0; i--) {
474 if (uV > vreg->set_points->range[i - 1].max_uV) {
475 range = &vreg->set_points->range[i];
476 break;
477 }
478 }
479
480 /*
481 * Force uV to be an allowed set point and apply a ceiling function
482 * to non-set point values.
483 */
484 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
485 uV = uV * range->step_uV + range->min_uV;
486
487 if (vreg->part->uV.mask) {
488 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
489 mask[vreg->part->uV.word] = vreg->part->uV.mask;
490 } else {
491 val[vreg->part->mV.word]
492 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
493 mask[vreg->part->mV.word] = vreg->part->mV.mask;
494 }
495
496 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
497 val[1], vreg->part->request_len, 1);
498 if (rc)
499 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500
501 return rc;
502}
503EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
504
505/**
506 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
507 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700508 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 *
510 * Returns 0 on success or errno.
511 */
David Collins6f032ba2011-08-31 14:08:15 -0700512int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513{
David Collins6f032ba2011-08-31 14:08:15 -0700514 unsigned int mask[2] = {0}, val[2] = {0};
515 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 int rc;
517
David Collins6f032ba2011-08-31 14:08:15 -0700518 /*
519 * HACK: make this function a no-op for 8064 so that it can be called by
520 * consumers on 8064 before RPM capabilities are present.
521 */
522 if (cpu_is_apq8064())
523 return 0;
524
525 if (!config) {
526 pr_err("rpm-regulator driver has not probed yet.\n");
527 return -ENODEV;
528 }
529
530 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
531 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 return -EINVAL;
533 }
534
David Collins6f032ba2011-08-31 14:08:15 -0700535 vreg = &config->vregs[vreg_id];
536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700538 vreg_err(vreg, "invalid frequency=%d\n", freq);
539 return -EINVAL;
540 }
541 if (!vreg->pdata.sleep_selectable) {
542 vreg_err(vreg, "regulator is not marked sleep selectable\n");
543 return -EINVAL;
544 }
545 if (!vreg->part->freq.mask) {
546 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547 return -EINVAL;
548 }
549
David Collins6f032ba2011-08-31 14:08:15 -0700550 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
551 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552
David Collins6f032ba2011-08-31 14:08:15 -0700553 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
554 val[0], mask[1], val[1], vreg->part->request_len, 0);
555 if (rc)
556 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557
558 return rc;
559}
560EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
561
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562static inline int vreg_hpm_min_uA(struct vreg *vreg)
563{
564 return vreg->hpm_min_load;
565}
566
567static inline int vreg_lpm_max_uA(struct vreg *vreg)
568{
569 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
570}
571
David Collins6f032ba2011-08-31 14:08:15 -0700572static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573{
David Collins6f032ba2011-08-31 14:08:15 -0700574 unsigned load_max
575 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
576
577 return (load_uA > load_max ? load_max : load_uA);
578}
579
580static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
581{
582 unsigned load_max
583 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
584 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585}
586
587/* Change vreg->req, but do not send it to the RPM. */
588static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
589 unsigned mask1, unsigned val1)
590{
591 unsigned long flags = 0;
592
David Collins6f032ba2011-08-31 14:08:15 -0700593 if (vreg->pdata.sleep_selectable)
594 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
596 vreg->req[0].value &= ~mask0;
597 vreg->req[0].value |= val0 & mask0;
598
599 vreg->req[1].value &= ~mask1;
600 vreg->req[1].value |= val1 & mask1;
601
David Collins6f032ba2011-08-31 14:08:15 -0700602 if (vreg->pdata.sleep_selectable)
603 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
605 return 0;
606}
607
608static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
609 unsigned mask1, unsigned val1, unsigned cnt)
610{
611 unsigned prev0 = 0, prev1 = 0;
612 int rc;
613
614 /*
615 * Bypass the normal route for regulators that can be called to change
616 * just the active set values.
617 */
David Collins6f032ba2011-08-31 14:08:15 -0700618 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
620 mask0, val0, mask1, val1, cnt, 1);
621
622 prev0 = vreg->req[0].value;
623 vreg->req[0].value &= ~mask0;
624 vreg->req[0].value |= val0 & mask0;
625
626 prev1 = vreg->req[1].value;
627 vreg->req[1].value &= ~mask1;
628 vreg->req[1].value |= val1 & mask1;
629
630 /* Ignore duplicate requests */
631 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
632 vreg->req[1].value == vreg->prev_active_req[1].value) {
633 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700634 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 return 0;
636 }
637
638 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
639 if (rc) {
640 vreg->req[0].value = prev0;
641 vreg->req[1].value = prev1;
642
David Collins6f032ba2011-08-31 14:08:15 -0700643 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
644 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 } else {
646 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700647 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 vreg->prev_active_req[0].value = vreg->req[0].value;
649 vreg->prev_active_req[1].value = vreg->req[1].value;
650 }
651
652 return rc;
653}
654
David Collins6f032ba2011-08-31 14:08:15 -0700655static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656{
David Collins6f032ba2011-08-31 14:08:15 -0700657 struct vreg *vreg = rdev_get_drvdata(rdev);
658 int enabled;
659
660 mutex_lock(&vreg->pc_lock);
661 enabled = vreg->is_enabled;
662 mutex_unlock(&vreg->pc_lock);
663
664 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665}
666
David Collins6f032ba2011-08-31 14:08:15 -0700667static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668{
David Collins6f032ba2011-08-31 14:08:15 -0700669 switch (vreg->type) {
670 case RPM_REGULATOR_TYPE_LDO:
671 case RPM_REGULATOR_TYPE_SMPS:
672 /* Enable by setting a voltage. */
673 if (vreg->part->uV.mask) {
674 val[vreg->part->uV.word]
675 |= vreg->save_uV << vreg->part->uV.shift;
676 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
677 } else {
678 val[vreg->part->mV.word]
679 |= MICRO_TO_MILLI(vreg->save_uV)
680 << vreg->part->mV.shift;
681 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
682 }
683 break;
684 case RPM_REGULATOR_TYPE_VS:
685 case RPM_REGULATOR_TYPE_NCP:
686 /* Enable by setting enable_state. */
687 val[vreg->part->enable_state.word]
688 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
689 mask[vreg->part->enable_state.word]
690 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692}
693
David Collins6f032ba2011-08-31 14:08:15 -0700694static int vreg_enable(struct regulator_dev *rdev)
695{
696 struct vreg *vreg = rdev_get_drvdata(rdev);
697 unsigned int mask[2] = {0}, val[2] = {0};
698 int rc = 0;
699
700 set_enable(vreg, mask, val);
701
702 mutex_lock(&vreg->pc_lock);
703
704 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
705 vreg->part->request_len);
706 if (!rc)
707 vreg->is_enabled = true;
708
709 mutex_unlock(&vreg->pc_lock);
710
711 if (rc)
712 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
713
714 return rc;
715}
716
717static void set_disable(struct vreg *vreg, unsigned int *mask,
718 unsigned int *val)
719{
720 switch (vreg->type) {
721 case RPM_REGULATOR_TYPE_LDO:
722 case RPM_REGULATOR_TYPE_SMPS:
723 /* Disable by setting a voltage of 0 uV. */
724 if (vreg->part->uV.mask) {
725 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
726 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
727 } else {
728 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
729 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
730 }
731 break;
732 case RPM_REGULATOR_TYPE_VS:
733 case RPM_REGULATOR_TYPE_NCP:
734 /* Disable by setting enable_state. */
735 val[vreg->part->enable_state.word]
736 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
737 mask[vreg->part->enable_state.word]
738 |= vreg->part->enable_state.mask;
739 }
740}
741
742static int vreg_disable(struct regulator_dev *rdev)
743{
744 struct vreg *vreg = rdev_get_drvdata(rdev);
745 unsigned int mask[2] = {0}, val[2] = {0};
746 int rc = 0;
747
748 set_disable(vreg, mask, val);
749
750 mutex_lock(&vreg->pc_lock);
751
752 /* Only disable if pin control is not in use. */
753 if (!vreg->is_enabled_pc)
754 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
755 vreg->part->request_len);
756
757 if (!rc)
758 vreg->is_enabled = false;
759
760 mutex_unlock(&vreg->pc_lock);
761
762 if (rc)
763 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
764
765 return rc;
766}
767
768static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 unsigned *selector)
770{
David Collins6f032ba2011-08-31 14:08:15 -0700771 struct vreg *vreg = rdev_get_drvdata(rdev);
772 struct vreg_range *range = &vreg->set_points->range[0];
773 unsigned int mask[2] = {0}, val[2] = {0};
774 int rc = 0, uV = min_uV;
775 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776
David Collins6f032ba2011-08-31 14:08:15 -0700777 /* Check if request voltage is outside of physically settable range. */
778 lim_min_uV = vreg->set_points->range[0].min_uV;
779 lim_max_uV =
780 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781
David Collins6f032ba2011-08-31 14:08:15 -0700782 if (uV < lim_min_uV && max_uV >= lim_min_uV)
783 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784
David Collins6f032ba2011-08-31 14:08:15 -0700785 if (uV < lim_min_uV || uV > lim_max_uV) {
786 vreg_err(vreg,
787 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
788 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 return -EINVAL;
790 }
791
David Collins6f032ba2011-08-31 14:08:15 -0700792 /* Find the range which uV is inside of. */
793 for (i = vreg->set_points->count - 1; i > 0; i--) {
794 if (uV > vreg->set_points->range[i - 1].max_uV) {
795 range = &vreg->set_points->range[i];
796 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 }
798 }
799
David Collins6f032ba2011-08-31 14:08:15 -0700800 /*
801 * Force uV to be an allowed set point and apply a ceiling function
802 * to non-set point values.
803 */
804 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
805 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806
David Collins6f032ba2011-08-31 14:08:15 -0700807 if (vreg->part->uV.mask) {
808 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
809 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700811 val[vreg->part->mV.word]
812 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
813 mask[vreg->part->mV.word] = vreg->part->mV.mask;
814 }
815
816 mutex_lock(&vreg->pc_lock);
817
818 /*
819 * Only send a request for a new voltage if the regulator is currently
820 * enabled. This will ensure that LDO and SMPS regulators are not
821 * inadvertently turned on because voltage > 0 is equivalent to
822 * enabling. For NCP, this just removes unnecessary RPM requests.
823 */
824 if (vreg->is_enabled) {
825 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
826 vreg->part->request_len);
827 if (rc)
828 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
829 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700831 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 }
833
David Collins6f032ba2011-08-31 14:08:15 -0700834 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
835 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836
David Collins6f032ba2011-08-31 14:08:15 -0700837 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838
839 return rc;
840}
841
David Collins6f032ba2011-08-31 14:08:15 -0700842static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843{
David Collins6f032ba2011-08-31 14:08:15 -0700844 struct vreg *vreg = rdev_get_drvdata(rdev);
845
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700846 return vreg->save_uV;
847}
848
David Collins6f032ba2011-08-31 14:08:15 -0700849static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850{
David Collins6f032ba2011-08-31 14:08:15 -0700851 struct vreg *vreg = rdev_get_drvdata(rdev);
852 int uV = 0;
853 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854
David Collins6f032ba2011-08-31 14:08:15 -0700855 if (!vreg->set_points) {
856 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 return -EINVAL;
858 }
859
David Collins6f032ba2011-08-31 14:08:15 -0700860 if (selector >= vreg->set_points->n_voltages)
861 return 0;
862
863 for (i = 0; i < vreg->set_points->count; i++) {
864 if (selector < vreg->set_points->range[i].n_voltages) {
865 uV = selector * vreg->set_points->range[i].step_uV
866 + vreg->set_points->range[i].min_uV;
867 break;
868 } else {
869 selector -= vreg->set_points->range[i].n_voltages;
870 }
871 }
872
873 return uV;
874}
875
876static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
877{
878 struct vreg *vreg = rdev_get_drvdata(rdev);
879 unsigned int mask[2] = {0}, val[2] = {0};
880 int rc = 0;
881 int peak_uA;
882
883 mutex_lock(&vreg->pc_lock);
884
885 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
886 & vreg->part->ip.mask) >> vreg->part->ip.shift);
887
888 if (mode == config->mode_hpm) {
889 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700891 val[vreg->part->ip.word]
892 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
893 << vreg->part->ip.shift;
894 mask[vreg->part->ip.word] = vreg->part->ip.mask;
895
896 if (config->ia_follows_ip) {
897 val[vreg->part->ia.word]
898 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
899 << vreg->part->ia.shift;
900 mask[vreg->part->ia.word]
901 |= vreg->part->ia.mask;
902 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 }
David Collins6f032ba2011-08-31 14:08:15 -0700904 } else if (mode == config->mode_lpm) {
905 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700907 val[vreg->part->ip.word]
908 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
909 << vreg->part->ip.shift;
910 mask[vreg->part->ip.word] = vreg->part->ip.mask;
911
912 if (config->ia_follows_ip) {
913 val[vreg->part->ia.word]
914 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
915 << vreg->part->ia.shift;
916 mask[vreg->part->ia.word]
917 |= vreg->part->ia.mask;
918 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 }
David Collins6f032ba2011-08-31 14:08:15 -0700920 } else {
921 vreg_err(vreg, "invalid mode: %u\n", mode);
922 mutex_unlock(&vreg->pc_lock);
923 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 }
925
David Collins6f032ba2011-08-31 14:08:15 -0700926 if (vreg->is_enabled) {
927 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
928 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 } else {
930 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700931 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932 }
David Collins6f032ba2011-08-31 14:08:15 -0700933
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -0700935 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
936 else
937 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938
David Collins6f032ba2011-08-31 14:08:15 -0700939 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940
David Collins6f032ba2011-08-31 14:08:15 -0700941 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942}
943
David Collins6f032ba2011-08-31 14:08:15 -0700944static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945{
David Collins6f032ba2011-08-31 14:08:15 -0700946 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947
David Collins6f032ba2011-08-31 14:08:15 -0700948 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949}
950
David Collins6f032ba2011-08-31 14:08:15 -0700951static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
952 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953{
David Collins6f032ba2011-08-31 14:08:15 -0700954 struct vreg *vreg = rdev_get_drvdata(rdev);
955 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956
David Collins6f032ba2011-08-31 14:08:15 -0700957 load_uA += vreg->pdata.system_uA;
958
959 mutex_lock(&vreg->pc_lock);
960 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
961 if (config->ia_follows_ip)
962 SET_PART(vreg, ia,
963 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
964 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965
966 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -0700967 mode = config->mode_hpm;
968 else
969 mode = config->mode_lpm;
970
971 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972}
973
David Collins6f032ba2011-08-31 14:08:15 -0700974static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
975 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976{
David Collins6f032ba2011-08-31 14:08:15 -0700977 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978
David Collins6f032ba2011-08-31 14:08:15 -0700979 if (MICRO_TO_MILLI(load_uA) <= 0) {
980 /*
981 * vreg_legacy_get_optimum_mode is being called before consumers
982 * have specified their load currents via
983 * regulator_set_optimum_mode. Return whatever the existing mode
984 * is.
985 */
986 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987 }
988
David Collins6f032ba2011-08-31 14:08:15 -0700989 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990}
991
992/*
David Collins6f032ba2011-08-31 14:08:15 -0700993 * Returns the logical pin control enable state because the pin control options
994 * present in the hardware out of restart could be different from those desired
995 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996 */
David Collins6f032ba2011-08-31 14:08:15 -0700997static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998{
David Collins6f032ba2011-08-31 14:08:15 -0700999 struct vreg *vreg = rdev_get_drvdata(rdev);
1000
1001 return vreg->is_enabled_pc;
1002}
1003
1004static int vreg_pin_control_enable(struct regulator_dev *rdev)
1005{
1006 struct vreg *vreg = rdev_get_drvdata(rdev);
1007 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 int rc;
1009
David Collins6f032ba2011-08-31 14:08:15 -07001010 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011
David Collins6f032ba2011-08-31 14:08:15 -07001012 val[vreg->part->pc.word]
1013 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1014 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015
David Collins6f032ba2011-08-31 14:08:15 -07001016 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1017 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018
David Collins6f032ba2011-08-31 14:08:15 -07001019 if (!vreg->is_enabled)
1020 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021
David Collins6f032ba2011-08-31 14:08:15 -07001022 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1023 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024
David Collins6f032ba2011-08-31 14:08:15 -07001025 if (!rc)
1026 vreg->is_enabled_pc = true;
1027
1028 mutex_unlock(&vreg->pc_lock);
1029
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001030 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001031 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032
David Collins6f032ba2011-08-31 14:08:15 -07001033 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034}
1035
David Collins6f032ba2011-08-31 14:08:15 -07001036static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037{
David Collins6f032ba2011-08-31 14:08:15 -07001038 struct vreg *vreg = rdev_get_drvdata(rdev);
1039 unsigned int mask[2] = {0}, val[2] = {0};
1040 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041
David Collins6f032ba2011-08-31 14:08:15 -07001042 mutex_lock(&vreg->pc_lock);
1043
1044 val[vreg->part->pc.word]
1045 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1046 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1047
1048 pin_fn = config->pin_func_none;
1049 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1050 pin_fn = config->pin_func_sleep_b;
1051 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1052 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1053
1054 if (!vreg->is_enabled)
1055 set_disable(vreg, mask, val);
1056
1057 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1058 vreg->part->request_len);
1059
1060 if (!rc)
1061 vreg->is_enabled_pc = false;
1062
1063 mutex_unlock(&vreg->pc_lock);
1064
1065 if (rc)
1066 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1067
1068 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069}
1070
David Collins6f032ba2011-08-31 14:08:15 -07001071static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072{
David Collins6f032ba2011-08-31 14:08:15 -07001073 struct vreg *vreg = rdev_get_drvdata(rdev);
1074
1075 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076}
1077
David Collins6f032ba2011-08-31 14:08:15 -07001078/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001079static struct regulator_ops ldo_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001080 .enable = vreg_enable,
1081 .disable = vreg_disable,
1082 .is_enabled = vreg_is_enabled,
1083 .set_voltage = vreg_set_voltage,
1084 .get_voltage = vreg_get_voltage,
1085 .list_voltage = vreg_list_voltage,
1086 .set_mode = vreg_set_mode,
1087 .get_mode = vreg_get_mode,
1088 .get_optimum_mode = vreg_get_optimum_mode,
1089 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090};
1091
1092static struct regulator_ops smps_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001093 .enable = vreg_enable,
1094 .disable = vreg_disable,
1095 .is_enabled = vreg_is_enabled,
1096 .set_voltage = vreg_set_voltage,
1097 .get_voltage = vreg_get_voltage,
1098 .list_voltage = vreg_list_voltage,
1099 .set_mode = vreg_set_mode,
1100 .get_mode = vreg_get_mode,
1101 .get_optimum_mode = vreg_get_optimum_mode,
1102 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103};
1104
1105static struct regulator_ops switch_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001106 .enable = vreg_enable,
1107 .disable = vreg_disable,
1108 .is_enabled = vreg_is_enabled,
1109 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110};
1111
1112static struct regulator_ops ncp_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001113 .enable = vreg_enable,
1114 .disable = vreg_disable,
1115 .is_enabled = vreg_is_enabled,
1116 .set_voltage = vreg_set_voltage,
1117 .get_voltage = vreg_get_voltage,
1118 .list_voltage = vreg_list_voltage,
1119 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120};
1121
David Collins6f032ba2011-08-31 14:08:15 -07001122/* Pin control regulator operations. */
1123static struct regulator_ops pin_control_ops = {
1124 .enable = vreg_pin_control_enable,
1125 .disable = vreg_pin_control_disable,
1126 .is_enabled = vreg_pin_control_is_enabled,
1127};
1128
1129struct regulator_ops *vreg_ops[] = {
1130 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1131 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1132 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1133 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1134};
1135
1136static int __devinit
1137rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1138 struct device *dev)
1139{
1140 struct regulator_desc *rdesc = NULL;
1141 struct regulator_dev *rdev;
1142 struct vreg *vreg;
1143 unsigned pin_ctrl;
1144 int id, pin_fn;
1145 int rc = 0;
1146
1147 if (!pdata) {
1148 pr_err("platform data missing\n");
1149 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001150 }
1151
David Collins6f032ba2011-08-31 14:08:15 -07001152 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153
David Collins6f032ba2011-08-31 14:08:15 -07001154 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1155 pr_err("invalid regulator id: %d\n", id);
1156 return -ENODEV;
1157 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158
David Collins6f032ba2011-08-31 14:08:15 -07001159 if (!config->is_real_id(pdata->id))
1160 id = config->pc_id_to_real_id(pdata->id);
1161 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162
David Collins6f032ba2011-08-31 14:08:15 -07001163 if (config->is_real_id(pdata->id))
1164 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165 else
David Collins6f032ba2011-08-31 14:08:15 -07001166 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167
David Collins6f032ba2011-08-31 14:08:15 -07001168 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1169 pr_err("%s: invalid regulator type: %d\n",
1170 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001172 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173
David Collins6f032ba2011-08-31 14:08:15 -07001174 mutex_lock(&vreg->pc_lock);
1175
1176 if (vreg->set_points)
1177 rdesc->n_voltages = vreg->set_points->n_voltages;
1178 else
1179 rdesc->n_voltages = 0;
1180
1181 rdesc->id = pdata->id;
1182 rdesc->owner = THIS_MODULE;
1183 rdesc->type = REGULATOR_VOLTAGE;
1184
1185 if (config->is_real_id(pdata->id)) {
1186 /*
1187 * Real regulator; do not modify pin control and pin function
1188 * values.
1189 */
1190 rdesc->ops = vreg_ops[vreg->type];
1191 pin_ctrl = vreg->pdata.pin_ctrl;
1192 pin_fn = vreg->pdata.pin_fn;
1193 memcpy(&(vreg->pdata), pdata,
1194 sizeof(struct rpm_regulator_init_data));
1195 vreg->pdata.pin_ctrl = pin_ctrl;
1196 vreg->pdata.pin_fn = pin_fn;
1197
1198 vreg->save_uV = vreg->pdata.default_uV;
1199 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1200 vreg->mode = config->mode_hpm;
1201 else
1202 vreg->mode = config->mode_lpm;
1203
1204 /* Initialize the RPM request. */
1205 SET_PART(vreg, ip,
1206 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1207 SET_PART(vreg, fm, vreg->pdata.force_mode);
1208 SET_PART(vreg, pm, vreg->pdata.power_mode);
1209 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1210 SET_PART(vreg, ia,
1211 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1212 SET_PART(vreg, freq, vreg->pdata.freq);
1213 SET_PART(vreg, freq_clk_src, 0);
1214 SET_PART(vreg, comp_mode, 0);
1215 SET_PART(vreg, hpm, 0);
1216 if (!vreg->is_enabled_pc) {
1217 SET_PART(vreg, pf, config->pin_func_none);
1218 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1219 }
1220 } else {
1221 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1222 == RPM_VREG_PIN_CTRL_NONE
1223 && pdata->pin_fn != config->pin_func_sleep_b) {
1224 pr_err("%s: no pin control input specified\n",
1225 vreg->rdesc.name);
1226 mutex_unlock(&vreg->pc_lock);
1227 return -EINVAL;
1228 }
1229 rdesc->ops = &pin_control_ops;
1230 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1231 vreg->pdata.pin_fn = pdata->pin_fn;
1232
1233 /* Initialize the RPM request. */
1234 pin_fn = config->pin_func_none;
1235 /* Allow pf=sleep_b to be specified by platform data. */
1236 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1237 pin_fn = config->pin_func_sleep_b;
1238 SET_PART(vreg, pf, pin_fn);
1239 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1240 }
1241
1242 mutex_unlock(&vreg->pc_lock);
1243
1244 if (rc)
1245 goto bail;
1246
1247 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1248 if (IS_ERR(rdev)) {
1249 rc = PTR_ERR(rdev);
1250 pr_err("regulator_register failed: %s, rc=%d\n",
1251 vreg->rdesc.name, rc);
1252 return rc;
1253 } else {
1254 if (config->is_real_id(pdata->id))
1255 vreg->rdev = rdev;
1256 else
1257 vreg->rdev_pc = rdev;
1258 }
1259
1260bail:
1261 if (rc)
1262 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1263
1264 return rc;
1265}
1266
1267static void rpm_vreg_set_point_init(void)
1268{
1269 struct vreg_set_points **set_points;
1270 int i, j, temp;
1271
1272 set_points = config->set_points;
1273
1274 /* Calculate the number of set points available for each regulator. */
1275 for (i = 0; i < config->set_points_len; i++) {
1276 temp = 0;
1277 for (j = 0; j < set_points[i]->count; j++) {
1278 set_points[i]->range[j].n_voltages
1279 = (set_points[i]->range[j].max_uV
1280 - set_points[i]->range[j].min_uV)
1281 / set_points[i]->range[j].step_uV + 1;
1282 temp += set_points[i]->range[j].n_voltages;
1283 }
1284 set_points[i]->n_voltages = temp;
1285 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286}
1287
1288static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1289{
David Collins6f032ba2011-08-31 14:08:15 -07001290 struct rpm_regulator_platform_data *platform_data;
1291 int rc = 0;
1292 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293
David Collins6f032ba2011-08-31 14:08:15 -07001294 platform_data = pdev->dev.platform_data;
1295 if (!platform_data) {
1296 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298 }
1299
David Collins6f032ba2011-08-31 14:08:15 -07001300 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1301 && platform_data->version != rpm_version) {
1302 pr_err("rpm version %d does not match previous version %d\n",
1303 platform_data->version, rpm_version);
1304 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305 }
1306
David Collins6f032ba2011-08-31 14:08:15 -07001307 if (platform_data->version < 0
1308 || platform_data->version > RPM_VREG_VERSION_MAX) {
1309 pr_err("rpm version %d is invalid\n", platform_data->version);
1310 return -EINVAL;
1311 }
1312
1313 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1314 rpm_version = platform_data->version;
1315 config = get_config[platform_data->version]();
1316 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1317 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1318 if (!config) {
1319 pr_err("rpm version %d is not available\n",
1320 platform_data->version);
1321 return -ENODEV;
1322 }
1323 if (config->use_legacy_optimum_mode)
1324 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1325 vreg_ops[i]->get_optimum_mode
1326 = vreg_legacy_get_optimum_mode;
1327 rpm_vreg_set_point_init();
1328 /* First time probed; initialize pin control mutexes. */
1329 for (i = 0; i < config->vregs_len; i++)
1330 mutex_init(&config->vregs[i].pc_lock);
1331 }
1332
1333 /* Initialize all of the regulators listed in the platform data. */
1334 for (i = 0; i < platform_data->num_regulators; i++) {
1335 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1336 &pdev->dev);
1337 if (rc) {
1338 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1339 goto remove_regulators;
1340 }
1341 }
1342
1343 platform_set_drvdata(pdev, platform_data);
1344
1345 return rc;
1346
1347remove_regulators:
1348 /* Unregister all regulators added before the erroring one. */
1349 for (; i >= 0; i--) {
1350 id = platform_data->init_data[i].id;
1351 if (config->is_real_id(id)) {
1352 regulator_unregister(config->vregs[id].rdev);
1353 config->vregs[id].rdev = NULL;
1354 } else {
1355 regulator_unregister(config->vregs[
1356 config->pc_id_to_real_id(id)].rdev_pc);
1357 config->vregs[id].rdev_pc = NULL;
1358 }
1359 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360
1361 return rc;
1362}
1363
1364static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1365{
David Collins6f032ba2011-08-31 14:08:15 -07001366 struct rpm_regulator_platform_data *platform_data;
1367 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001368
David Collins6f032ba2011-08-31 14:08:15 -07001369 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001370 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001371
1372 if (platform_data) {
1373 for (i = 0; i < platform_data->num_regulators; 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 }
1384 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385
1386 return 0;
1387}
1388
1389static struct platform_driver rpm_vreg_driver = {
1390 .probe = rpm_vreg_probe,
1391 .remove = __devexit_p(rpm_vreg_remove),
1392 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001393 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394 .owner = THIS_MODULE,
1395 },
1396};
1397
1398static int __init rpm_vreg_init(void)
1399{
1400 return platform_driver_register(&rpm_vreg_driver);
1401}
1402
1403static void __exit rpm_vreg_exit(void)
1404{
David Collins6f032ba2011-08-31 14:08:15 -07001405 int i;
1406
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001408
1409 for (i = 0; i < config->vregs_len; i++)
1410 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411}
1412
1413postcore_initcall(rpm_vreg_init);
1414module_exit(rpm_vreg_exit);
1415
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001417MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001419MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);