blob: 946d31bbe415bf59b1614f1fc050edc8804119e4 [file] [log] [blame]
David Collins6f032ba2011-08-31 14:08:15 -07001/*
David Collins5779cea2012-01-05 15:09:21 -08002 * Copyright (c) 2010-2012, 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 Collins5779cea2012-01-05 15:09:21 -080049 [RPM_VREG_VERSION_8930] = get_config_8930,
David Collins6f032ba2011-08-31 14:08:15 -070050};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
David Collins6f032ba2011-08-31 14:08:15 -070052#define SET_PART(_vreg, _part, _val) \
53 _vreg->req[_vreg->part->_part.word].value \
54 = (_vreg->req[_vreg->part->_part.word].value \
David Collinsd8525e82011-11-21 14:54:25 -080055 & ~_vreg->part->_part.mask) \
56 | (((_val) << _vreg->part->_part.shift) \
57 & _vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058
David Collins6f032ba2011-08-31 14:08:15 -070059#define GET_PART(_vreg, _part) \
David Collinsd8525e82011-11-21 14:54:25 -080060 ((_vreg->req[_vreg->part->_part.word].value & _vreg->part->_part.mask) \
61 >> _vreg->part->_part.shift)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062
David Collinsd8525e82011-11-21 14:54:25 -080063#define USES_PART(_vreg, _part) (_vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064
David Collins6f032ba2011-08-31 14:08:15 -070065#define vreg_err(vreg, fmt, ...) \
66 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067
David Collins6f032ba2011-08-31 14:08:15 -070068#define RPM_VREG_PIN_CTRL_EN0 0x01
69#define RPM_VREG_PIN_CTRL_EN1 0x02
70#define RPM_VREG_PIN_CTRL_EN2 0x04
71#define RPM_VREG_PIN_CTRL_EN3 0x08
72#define RPM_VREG_PIN_CTRL_ALL 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073
David Collins6f032ba2011-08-31 14:08:15 -070074static const char *label_freq[] = {
75 [RPM_VREG_FREQ_NONE] = " N/A",
76 [RPM_VREG_FREQ_19p20] = "19.2",
77 [RPM_VREG_FREQ_9p60] = "9.60",
78 [RPM_VREG_FREQ_6p40] = "6.40",
79 [RPM_VREG_FREQ_4p80] = "4.80",
80 [RPM_VREG_FREQ_3p84] = "3.84",
81 [RPM_VREG_FREQ_3p20] = "3.20",
82 [RPM_VREG_FREQ_2p74] = "2.74",
83 [RPM_VREG_FREQ_2p40] = "2.40",
84 [RPM_VREG_FREQ_2p13] = "2.13",
85 [RPM_VREG_FREQ_1p92] = "1.92",
86 [RPM_VREG_FREQ_1p75] = "1.75",
87 [RPM_VREG_FREQ_1p60] = "1.60",
88 [RPM_VREG_FREQ_1p48] = "1.48",
89 [RPM_VREG_FREQ_1p37] = "1.37",
90 [RPM_VREG_FREQ_1p28] = "1.28",
91 [RPM_VREG_FREQ_1p20] = "1.20",
92};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093/*
94 * This is used when voting for LPM or HPM by subtracting or adding to the
95 * hpm_min_load of a regulator. It has units of uA.
96 */
David Collins6f032ba2011-08-31 14:08:15 -070097#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098
David Collins6f032ba2011-08-31 14:08:15 -070099/* rpm_version keeps track of the version for the currently running driver. */
100enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101
David Collins6f032ba2011-08-31 14:08:15 -0700102/* config holds all configuration data of the currently running driver. */
103static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104
David Collins6f032ba2011-08-31 14:08:15 -0700105/* These regulator ID values are specified in the board file. */
106static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107
David Collins6f032ba2011-08-31 14:08:15 -0700108static inline int vreg_id_is_vdd_mem_or_dig(int id)
109{
110 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
111}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112
David Collins6f032ba2011-08-31 14:08:15 -0700113#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114
David Collins6f032ba2011-08-31 14:08:15 -0700115static void rpm_regulator_req(struct vreg *vreg, int set)
116{
117 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
118 const char *pf_label = "", *fm_label = "", *pc_total = "";
119 const char *pc_en[4] = {"", "", "", ""};
120 const char *pm_label = "", *freq_label = "";
121 char buf[DEBUG_PRINT_BUFFER_SIZE];
122 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
123 int pos = 0;
124
125 /* Suppress VDD_MEM and VDD_DIG printing. */
126 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
127 && vreg_id_is_vdd_mem_or_dig(vreg->id))
128 return;
129
130 uV = GET_PART(vreg, uV);
131 mV = GET_PART(vreg, mV);
132 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
133 uV = -uV;
134 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 }
136
David Collins6f032ba2011-08-31 14:08:15 -0700137 fm = GET_PART(vreg, fm);
138 pm = GET_PART(vreg, pm);
139 pc = GET_PART(vreg, pc);
140 pf = GET_PART(vreg, pf);
141 pd = GET_PART(vreg, pd);
142 freq = GET_PART(vreg, freq);
143 state = GET_PART(vreg, enable_state);
144
145 if (pf >= 0 && pf < config->label_pin_func_len)
146 pf_label = config->label_pin_func[pf];
147
148 if (fm >= 0 && fm < config->label_force_mode_len)
149 fm_label = config->label_force_mode[fm];
150
151 if (pm >= 0 && pm < config->label_power_mode_len)
152 pm_label = config->label_power_mode[pm];
153
154 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
155 freq_label = label_freq[freq];
156
157 for (i = 0; i < config->label_pin_ctrl_len; i++)
158 if (pc & (1 << i))
159 pc_en[i] = config->label_pin_ctrl[i];
160
161 if (pc == RPM_VREG_PIN_CTRL_NONE)
162 pc_total = " none";
163
164 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
165 KERN_INFO, __func__);
166
167 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
168 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
169 vreg->rdesc.name,
170 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
171
172 if (USES_PART(vreg, uV))
173 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
174 if (USES_PART(vreg, mV))
175 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
176 if (USES_PART(vreg, enable_state))
177 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
178 (state == 1 ? "on" : "off"), state);
179 if (USES_PART(vreg, ip))
180 pos += scnprintf(buf + pos, buflen - pos,
181 ", ip=%4d mA", GET_PART(vreg, ip));
182 if (USES_PART(vreg, fm))
183 pos += scnprintf(buf + pos, buflen - pos,
184 ", fm=%s (%d)", fm_label, fm);
185 if (USES_PART(vreg, pc))
186 pos += scnprintf(buf + pos, buflen - pos,
187 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
188 pc_en[2], pc_en[3], pc_total, pc);
189 if (USES_PART(vreg, pf))
190 pos += scnprintf(buf + pos, buflen - pos,
191 ", pf=%s (%d)", pf_label, pf);
192 if (USES_PART(vreg, pd))
193 pos += scnprintf(buf + pos, buflen - pos,
194 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
195 if (USES_PART(vreg, ia))
196 pos += scnprintf(buf + pos, buflen - pos,
197 ", ia=%4d mA", GET_PART(vreg, ia));
198 if (USES_PART(vreg, freq)) {
199 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
200 pos += scnprintf(buf + pos, buflen - pos,
201 ", freq=%2d", freq);
202 else
203 pos += scnprintf(buf + pos, buflen - pos,
204 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205 }
David Collins6f032ba2011-08-31 14:08:15 -0700206 if (USES_PART(vreg, pm))
207 pos += scnprintf(buf + pos, buflen - pos,
208 ", pm=%s (%d)", pm_label, pm);
209 if (USES_PART(vreg, freq_clk_src))
210 pos += scnprintf(buf + pos, buflen - pos,
211 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
212 if (USES_PART(vreg, comp_mode))
213 pos += scnprintf(buf + pos, buflen - pos,
214 ", comp=%d", GET_PART(vreg, comp_mode));
215 if (USES_PART(vreg, hpm))
216 pos += scnprintf(buf + pos, buflen - pos,
217 ", hpm=%d", GET_PART(vreg, hpm));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218
David Collins6f032ba2011-08-31 14:08:15 -0700219 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
220 vreg->req[0].id, vreg->req[0].value);
221 if (vreg->part->request_len > 1)
222 pos += scnprintf(buf + pos, buflen - pos,
223 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
224 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225
David Collins6f032ba2011-08-31 14:08:15 -0700226 pos += scnprintf(buf + pos, buflen - pos, "\n");
227 printk(buf);
228}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229
David Collins6f032ba2011-08-31 14:08:15 -0700230static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
231 int set, int voter_uV, int aggregate_uV)
232{
233 /* Suppress VDD_MEM and VDD_DIG printing. */
234 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
235 && vreg_id_is_vdd_mem_or_dig(vreg->id))
236 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237
David Collins6f032ba2011-08-31 14:08:15 -0700238 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
239 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
240 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
241}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242
David Collins6f032ba2011-08-31 14:08:15 -0700243static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
244{
245 /* Suppress VDD_MEM and VDD_DIG printing. */
246 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
247 && vreg_id_is_vdd_mem_or_dig(vreg->id))
248 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249
David Collins6f032ba2011-08-31 14:08:15 -0700250 if (cnt == 2)
251 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
252 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
253 (set == 0 ? 'A' : 'S'),
254 vreg->req[0].id, vreg->req[0].value,
255 vreg->req[1].id, vreg->req[1].value);
256 else if (cnt == 1)
257 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
258 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
259 vreg->req[0].id, vreg->req[0].value);
260}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261
262/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700263static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264
265static int voltage_from_req(struct vreg *vreg)
266{
David Collins6f032ba2011-08-31 14:08:15 -0700267 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268
David Collins6f032ba2011-08-31 14:08:15 -0700269 if (vreg->part->uV.mask)
270 uV = GET_PART(vreg, uV);
271 else
272 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273
David Collins6f032ba2011-08-31 14:08:15 -0700274 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275}
276
David Collins6f032ba2011-08-31 14:08:15 -0700277static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278{
David Collins6f032ba2011-08-31 14:08:15 -0700279 if (vreg->part->uV.mask)
280 SET_PART(vreg, uV, uV);
281 else
282 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283}
284
285static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
286 int set, unsigned mask0, unsigned val0,
287 unsigned mask1, unsigned val1, unsigned cnt,
288 int update_voltage)
289{
290 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700291 int rc = 0, max_uV_vote = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700293 int *min_uV_vote;
294 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295
296 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700297 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 prev_req = vreg->prev_active_req;
299 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700300 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 prev_req = vreg->prev_sleep_req;
302 }
303
304 prev0 = vreg->req[0].value;
305 vreg->req[0].value &= ~mask0;
306 vreg->req[0].value |= val0 & mask0;
307
308 prev1 = vreg->req[1].value;
309 vreg->req[1].value &= ~mask1;
310 vreg->req[1].value |= val1 & mask1;
311
312 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700313 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314
315 /* Find the highest voltage voted for and use it. */
316 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700317 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
318 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319
320 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700321 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
322 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323
324 /* Ignore duplicate requests */
325 if (vreg->req[0].value != prev_req[0].value ||
326 vreg->req[1].value != prev_req[1].value) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
328 if (rc) {
329 vreg->req[0].value = prev0;
330 vreg->req[1].value = prev1;
331
David Collins6f032ba2011-08-31 14:08:15 -0700332 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
333 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
335 vreg->req[0].id, rc);
336 } else {
337 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700338 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
339 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 if (msm_rpm_vreg_debug_mask
341 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700342 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343 prev_req[0].value = vreg->req[0].value;
344 prev_req[1].value = vreg->req[1].value;
345 }
346 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700347 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 }
349
350 return rc;
351}
352
353static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
354 int sleep, unsigned mask0, unsigned val0,
355 unsigned mask1, unsigned val1, unsigned cnt,
356 int update_voltage)
357{
David Collins6f032ba2011-08-31 14:08:15 -0700358 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359 unsigned long flags;
360 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361
362 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
363 return -EINVAL;
364
David Collins6f032ba2011-08-31 14:08:15 -0700365 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366
367 /*
368 * Send sleep set request first so that subsequent set_mode, etc calls
369 * use the voltage from the active set.
370 */
371 if (sleep)
372 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
373 mask0, val0, mask1, val1, cnt, update_voltage);
374 else {
375 /*
376 * Vote for 0 V in the sleep set when active set-only is
377 * specified. This ensures that a disable vote will be issued
378 * at some point for the sleep set of the regulator.
379 */
David Collins6f032ba2011-08-31 14:08:15 -0700380 if (vreg->part->uV.mask) {
381 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
382 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
383 } else {
384 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
385 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 }
387
388 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700389 s_mask[0], s_val[0], s_mask[1], s_val[1],
390 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 }
392
393 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
394 mask1, val1, cnt, update_voltage);
395
David Collins6f032ba2011-08-31 14:08:15 -0700396 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397
398 return rc;
399}
400
401/**
402 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
403 * @vreg: ID for regulator
404 * @voter: ID for the voter
405 * @min_uV: minimum acceptable voltage (in uV) that is voted for
406 * @max_uV: maximum acceptable voltage (in uV) that is voted for
407 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
408 *
409 * Returns 0 on success or errno.
410 *
411 * This function is used to vote for the voltage of a regulator without
412 * using the regulator framework. It is needed by consumers which hold spin
413 * locks or have interrupts disabled because the regulator framework can sleep.
414 * It is also needed by consumers which wish to only vote for active set
415 * regulator voltage.
416 *
417 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
418 *
419 * This function may only be called for regulators which have the sleep flag
420 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700421 *
422 * Consumers can vote to disable a regulator with this function by passing
423 * min_uV = 0 and max_uV = 0.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 */
David Collins6f032ba2011-08-31 14:08:15 -0700425int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
426 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427{
David Collins6f032ba2011-08-31 14:08:15 -0700428 unsigned int mask[2] = {0}, val[2] = {0};
429 struct vreg_range *range;
430 struct vreg *vreg;
431 int uV = min_uV;
432 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433
David Collins6f032ba2011-08-31 14:08:15 -0700434 /*
435 * HACK: make this function a no-op for 8064 so that it can be called by
436 * consumers on 8064 before RPM capabilities are present. (needed for
437 * acpuclock driver)
438 */
439 if (cpu_is_apq8064())
440 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441
David Collins6f032ba2011-08-31 14:08:15 -0700442 if (!config) {
443 pr_err("rpm-regulator driver has not probed yet.\n");
444 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 }
446
David Collins6f032ba2011-08-31 14:08:15 -0700447 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
448 pr_err("invalid regulator id=%d\n", vreg_id);
449 return -EINVAL;
450 }
451
452 vreg = &config->vregs[vreg_id];
453 range = &vreg->set_points->range[0];
454
455 if (!vreg->pdata.sleep_selectable) {
456 vreg_err(vreg, "regulator is not marked sleep selectable\n");
457 return -EINVAL;
458 }
459
David Collins7462b9d2011-10-11 16:02:17 -0700460 /* Allow min_uV == max_uV == 0 to represent a disable request. */
461 if (min_uV != 0 || max_uV != 0) {
462 /*
463 * Check if request voltage is outside of allowed range. The
464 * regulator core has already checked that constraint range
465 * is inside of the physically allowed range.
466 */
467 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
468 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700469
David Collins7462b9d2011-10-11 16:02:17 -0700470 if (uV < lim_min_uV && max_uV >= lim_min_uV)
471 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700472
David Collins7462b9d2011-10-11 16:02:17 -0700473 if (uV < lim_min_uV || uV > lim_max_uV) {
474 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
475 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
476 lim_max_uV);
477 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700478 }
David Collins6f032ba2011-08-31 14:08:15 -0700479
David Collins7462b9d2011-10-11 16:02:17 -0700480 /* Find the range which uV is inside of. */
481 for (i = vreg->set_points->count - 1; i > 0; i--) {
482 if (uV > vreg->set_points->range[i - 1].max_uV) {
483 range = &vreg->set_points->range[i];
484 break;
485 }
486 }
487
488 /*
489 * Force uV to be an allowed set point and apply a ceiling
490 * function to non-set point values.
491 */
492 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
493 uV = uV * range->step_uV + range->min_uV;
David Collins3974b612011-11-21 15:07:36 -0800494
495 if (uV > max_uV) {
496 vreg_err(vreg,
497 "request v=[%d, %d] cannot be met by any set point; "
498 "next set point: %d\n",
499 min_uV, max_uV, uV);
500 return -EINVAL;
501 }
David Collins7462b9d2011-10-11 16:02:17 -0700502 }
David Collins6f032ba2011-08-31 14:08:15 -0700503
504 if (vreg->part->uV.mask) {
505 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
506 mask[vreg->part->uV.word] = vreg->part->uV.mask;
507 } else {
508 val[vreg->part->mV.word]
509 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
510 mask[vreg->part->mV.word] = vreg->part->mV.mask;
511 }
512
513 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
514 val[1], vreg->part->request_len, 1);
515 if (rc)
516 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517
518 return rc;
519}
520EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
521
522/**
523 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
524 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700525 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 *
527 * Returns 0 on success or errno.
528 */
David Collins6f032ba2011-08-31 14:08:15 -0700529int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530{
David Collins6f032ba2011-08-31 14:08:15 -0700531 unsigned int mask[2] = {0}, val[2] = {0};
532 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 int rc;
534
David Collins6f032ba2011-08-31 14:08:15 -0700535 /*
536 * HACK: make this function a no-op for 8064 so that it can be called by
537 * consumers on 8064 before RPM capabilities are present.
538 */
539 if (cpu_is_apq8064())
540 return 0;
541
542 if (!config) {
543 pr_err("rpm-regulator driver has not probed yet.\n");
544 return -ENODEV;
545 }
546
547 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
548 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 return -EINVAL;
550 }
551
David Collins6f032ba2011-08-31 14:08:15 -0700552 vreg = &config->vregs[vreg_id];
553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700555 vreg_err(vreg, "invalid frequency=%d\n", freq);
556 return -EINVAL;
557 }
558 if (!vreg->pdata.sleep_selectable) {
559 vreg_err(vreg, "regulator is not marked sleep selectable\n");
560 return -EINVAL;
561 }
562 if (!vreg->part->freq.mask) {
563 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 return -EINVAL;
565 }
566
David Collins6f032ba2011-08-31 14:08:15 -0700567 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
568 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569
David Collins6f032ba2011-08-31 14:08:15 -0700570 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
571 val[0], mask[1], val[1], vreg->part->request_len, 0);
572 if (rc)
573 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
575 return rc;
576}
577EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
578
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579static inline int vreg_hpm_min_uA(struct vreg *vreg)
580{
581 return vreg->hpm_min_load;
582}
583
584static inline int vreg_lpm_max_uA(struct vreg *vreg)
585{
586 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
587}
588
David Collins6f032ba2011-08-31 14:08:15 -0700589static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590{
David Collins6f032ba2011-08-31 14:08:15 -0700591 unsigned load_max
592 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
593
594 return (load_uA > load_max ? load_max : load_uA);
595}
596
597static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
598{
599 unsigned load_max
600 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
601 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602}
603
604/* Change vreg->req, but do not send it to the RPM. */
605static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
606 unsigned mask1, unsigned val1)
607{
608 unsigned long flags = 0;
609
David Collins6f032ba2011-08-31 14:08:15 -0700610 if (vreg->pdata.sleep_selectable)
611 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612
613 vreg->req[0].value &= ~mask0;
614 vreg->req[0].value |= val0 & mask0;
615
616 vreg->req[1].value &= ~mask1;
617 vreg->req[1].value |= val1 & mask1;
618
David Collins6f032ba2011-08-31 14:08:15 -0700619 if (vreg->pdata.sleep_selectable)
620 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621
622 return 0;
623}
624
625static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
626 unsigned mask1, unsigned val1, unsigned cnt)
627{
628 unsigned prev0 = 0, prev1 = 0;
629 int rc;
630
631 /*
632 * Bypass the normal route for regulators that can be called to change
633 * just the active set values.
634 */
David Collins6f032ba2011-08-31 14:08:15 -0700635 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
637 mask0, val0, mask1, val1, cnt, 1);
638
639 prev0 = vreg->req[0].value;
640 vreg->req[0].value &= ~mask0;
641 vreg->req[0].value |= val0 & mask0;
642
643 prev1 = vreg->req[1].value;
644 vreg->req[1].value &= ~mask1;
645 vreg->req[1].value |= val1 & mask1;
646
647 /* Ignore duplicate requests */
648 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
649 vreg->req[1].value == vreg->prev_active_req[1].value) {
650 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700651 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 return 0;
653 }
654
655 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
656 if (rc) {
657 vreg->req[0].value = prev0;
658 vreg->req[1].value = prev1;
659
David Collins6f032ba2011-08-31 14:08:15 -0700660 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
661 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 } else {
663 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700664 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 vreg->prev_active_req[0].value = vreg->req[0].value;
666 vreg->prev_active_req[1].value = vreg->req[1].value;
667 }
668
669 return rc;
670}
671
David Collins6f032ba2011-08-31 14:08:15 -0700672static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673{
David Collins6f032ba2011-08-31 14:08:15 -0700674 struct vreg *vreg = rdev_get_drvdata(rdev);
675 int enabled;
676
677 mutex_lock(&vreg->pc_lock);
678 enabled = vreg->is_enabled;
679 mutex_unlock(&vreg->pc_lock);
680
681 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682}
683
David Collins6f032ba2011-08-31 14:08:15 -0700684static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685{
David Collins6f032ba2011-08-31 14:08:15 -0700686 switch (vreg->type) {
687 case RPM_REGULATOR_TYPE_LDO:
688 case RPM_REGULATOR_TYPE_SMPS:
689 /* Enable by setting a voltage. */
690 if (vreg->part->uV.mask) {
691 val[vreg->part->uV.word]
692 |= vreg->save_uV << vreg->part->uV.shift;
693 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
694 } else {
695 val[vreg->part->mV.word]
696 |= MICRO_TO_MILLI(vreg->save_uV)
697 << vreg->part->mV.shift;
698 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
699 }
700 break;
701 case RPM_REGULATOR_TYPE_VS:
702 case RPM_REGULATOR_TYPE_NCP:
703 /* Enable by setting enable_state. */
704 val[vreg->part->enable_state.word]
705 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
706 mask[vreg->part->enable_state.word]
707 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709}
710
David Collins6f032ba2011-08-31 14:08:15 -0700711static int vreg_enable(struct regulator_dev *rdev)
712{
713 struct vreg *vreg = rdev_get_drvdata(rdev);
714 unsigned int mask[2] = {0}, val[2] = {0};
715 int rc = 0;
716
717 set_enable(vreg, mask, val);
718
719 mutex_lock(&vreg->pc_lock);
720
721 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
722 vreg->part->request_len);
723 if (!rc)
724 vreg->is_enabled = true;
725
726 mutex_unlock(&vreg->pc_lock);
727
728 if (rc)
729 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
730
731 return rc;
732}
733
734static void set_disable(struct vreg *vreg, unsigned int *mask,
735 unsigned int *val)
736{
737 switch (vreg->type) {
738 case RPM_REGULATOR_TYPE_LDO:
739 case RPM_REGULATOR_TYPE_SMPS:
740 /* Disable by setting a voltage of 0 uV. */
741 if (vreg->part->uV.mask) {
742 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
743 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
744 } else {
745 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
746 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
747 }
748 break;
749 case RPM_REGULATOR_TYPE_VS:
750 case RPM_REGULATOR_TYPE_NCP:
751 /* Disable by setting enable_state. */
752 val[vreg->part->enable_state.word]
753 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
754 mask[vreg->part->enable_state.word]
755 |= vreg->part->enable_state.mask;
756 }
757}
758
759static int vreg_disable(struct regulator_dev *rdev)
760{
761 struct vreg *vreg = rdev_get_drvdata(rdev);
762 unsigned int mask[2] = {0}, val[2] = {0};
763 int rc = 0;
764
765 set_disable(vreg, mask, val);
766
767 mutex_lock(&vreg->pc_lock);
768
769 /* Only disable if pin control is not in use. */
770 if (!vreg->is_enabled_pc)
771 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
772 vreg->part->request_len);
773
774 if (!rc)
775 vreg->is_enabled = false;
776
777 mutex_unlock(&vreg->pc_lock);
778
779 if (rc)
780 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
781
782 return rc;
783}
784
785static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786 unsigned *selector)
787{
David Collins6f032ba2011-08-31 14:08:15 -0700788 struct vreg *vreg = rdev_get_drvdata(rdev);
789 struct vreg_range *range = &vreg->set_points->range[0];
790 unsigned int mask[2] = {0}, val[2] = {0};
791 int rc = 0, uV = min_uV;
792 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793
David Collins6f032ba2011-08-31 14:08:15 -0700794 /* Check if request voltage is outside of physically settable range. */
795 lim_min_uV = vreg->set_points->range[0].min_uV;
796 lim_max_uV =
797 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798
David Collins6f032ba2011-08-31 14:08:15 -0700799 if (uV < lim_min_uV && max_uV >= lim_min_uV)
800 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801
David Collins6f032ba2011-08-31 14:08:15 -0700802 if (uV < lim_min_uV || uV > lim_max_uV) {
803 vreg_err(vreg,
804 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
805 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 return -EINVAL;
807 }
808
David Collins6f032ba2011-08-31 14:08:15 -0700809 /* Find the range which uV is inside of. */
810 for (i = vreg->set_points->count - 1; i > 0; i--) {
811 if (uV > vreg->set_points->range[i - 1].max_uV) {
812 range = &vreg->set_points->range[i];
813 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 }
815 }
816
David Collins6f032ba2011-08-31 14:08:15 -0700817 /*
818 * Force uV to be an allowed set point and apply a ceiling function
819 * to non-set point values.
820 */
821 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
822 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823
David Collins3974b612011-11-21 15:07:36 -0800824 if (uV > max_uV) {
825 vreg_err(vreg,
826 "request v=[%d, %d] cannot be met by any set point; "
827 "next set point: %d\n",
828 min_uV, max_uV, uV);
829 return -EINVAL;
830 }
831
David Collins6f032ba2011-08-31 14:08:15 -0700832 if (vreg->part->uV.mask) {
833 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
834 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700836 val[vreg->part->mV.word]
837 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
838 mask[vreg->part->mV.word] = vreg->part->mV.mask;
839 }
840
841 mutex_lock(&vreg->pc_lock);
842
843 /*
844 * Only send a request for a new voltage if the regulator is currently
845 * enabled. This will ensure that LDO and SMPS regulators are not
846 * inadvertently turned on because voltage > 0 is equivalent to
847 * enabling. For NCP, this just removes unnecessary RPM requests.
848 */
849 if (vreg->is_enabled) {
850 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
851 vreg->part->request_len);
852 if (rc)
853 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
854 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700856 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 }
858
David Collins6f032ba2011-08-31 14:08:15 -0700859 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
860 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861
David Collins6f032ba2011-08-31 14:08:15 -0700862 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863
864 return rc;
865}
866
David Collins6f032ba2011-08-31 14:08:15 -0700867static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868{
David Collins6f032ba2011-08-31 14:08:15 -0700869 struct vreg *vreg = rdev_get_drvdata(rdev);
870
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 return vreg->save_uV;
872}
873
David Collins6f032ba2011-08-31 14:08:15 -0700874static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875{
David Collins6f032ba2011-08-31 14:08:15 -0700876 struct vreg *vreg = rdev_get_drvdata(rdev);
877 int uV = 0;
878 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879
David Collins6f032ba2011-08-31 14:08:15 -0700880 if (!vreg->set_points) {
881 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 return -EINVAL;
883 }
884
David Collins6f032ba2011-08-31 14:08:15 -0700885 if (selector >= vreg->set_points->n_voltages)
886 return 0;
887
888 for (i = 0; i < vreg->set_points->count; i++) {
889 if (selector < vreg->set_points->range[i].n_voltages) {
890 uV = selector * vreg->set_points->range[i].step_uV
891 + vreg->set_points->range[i].min_uV;
892 break;
893 } else {
894 selector -= vreg->set_points->range[i].n_voltages;
895 }
896 }
897
898 return uV;
899}
900
901static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
902{
903 struct vreg *vreg = rdev_get_drvdata(rdev);
904 unsigned int mask[2] = {0}, val[2] = {0};
905 int rc = 0;
906 int peak_uA;
907
908 mutex_lock(&vreg->pc_lock);
909
910 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
911 & vreg->part->ip.mask) >> vreg->part->ip.shift);
912
913 if (mode == config->mode_hpm) {
914 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700916 val[vreg->part->ip.word]
917 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
918 << vreg->part->ip.shift;
919 mask[vreg->part->ip.word] = vreg->part->ip.mask;
920
921 if (config->ia_follows_ip) {
922 val[vreg->part->ia.word]
923 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
924 << vreg->part->ia.shift;
925 mask[vreg->part->ia.word]
926 |= vreg->part->ia.mask;
927 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 }
David Collins6f032ba2011-08-31 14:08:15 -0700929 } else if (mode == config->mode_lpm) {
930 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700932 val[vreg->part->ip.word]
933 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
934 << vreg->part->ip.shift;
935 mask[vreg->part->ip.word] = vreg->part->ip.mask;
936
937 if (config->ia_follows_ip) {
938 val[vreg->part->ia.word]
939 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
940 << vreg->part->ia.shift;
941 mask[vreg->part->ia.word]
942 |= vreg->part->ia.mask;
943 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 }
David Collins6f032ba2011-08-31 14:08:15 -0700945 } else {
946 vreg_err(vreg, "invalid mode: %u\n", mode);
947 mutex_unlock(&vreg->pc_lock);
948 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 }
950
David Collins6f032ba2011-08-31 14:08:15 -0700951 if (vreg->is_enabled) {
952 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
953 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 } else {
955 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700956 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 }
David Collins6f032ba2011-08-31 14:08:15 -0700958
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -0700960 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
961 else
962 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963
David Collins6f032ba2011-08-31 14:08:15 -0700964 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965
David Collins6f032ba2011-08-31 14:08:15 -0700966 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967}
968
David Collins6f032ba2011-08-31 14:08:15 -0700969static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970{
David Collins6f032ba2011-08-31 14:08:15 -0700971 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972
David Collins6f032ba2011-08-31 14:08:15 -0700973 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974}
975
David Collins6f032ba2011-08-31 14:08:15 -0700976static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
977 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978{
David Collins6f032ba2011-08-31 14:08:15 -0700979 struct vreg *vreg = rdev_get_drvdata(rdev);
980 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981
David Collins6f032ba2011-08-31 14:08:15 -0700982 load_uA += vreg->pdata.system_uA;
983
984 mutex_lock(&vreg->pc_lock);
985 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
986 if (config->ia_follows_ip)
987 SET_PART(vreg, ia,
988 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
989 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990
991 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -0700992 mode = config->mode_hpm;
993 else
994 mode = config->mode_lpm;
995
996 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997}
998
David Collins6f032ba2011-08-31 14:08:15 -0700999static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
1000 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001{
David Collins6f032ba2011-08-31 14:08:15 -07001002 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
David Collins6f032ba2011-08-31 14:08:15 -07001004 if (MICRO_TO_MILLI(load_uA) <= 0) {
1005 /*
1006 * vreg_legacy_get_optimum_mode is being called before consumers
1007 * have specified their load currents via
1008 * regulator_set_optimum_mode. Return whatever the existing mode
1009 * is.
1010 */
1011 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 }
1013
David Collins6f032ba2011-08-31 14:08:15 -07001014 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015}
1016
1017/*
David Collins6f032ba2011-08-31 14:08:15 -07001018 * Returns the logical pin control enable state because the pin control options
1019 * present in the hardware out of restart could be different from those desired
1020 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 */
David Collins6f032ba2011-08-31 14:08:15 -07001022static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023{
David Collins6f032ba2011-08-31 14:08:15 -07001024 struct vreg *vreg = rdev_get_drvdata(rdev);
1025
1026 return vreg->is_enabled_pc;
1027}
1028
1029static int vreg_pin_control_enable(struct regulator_dev *rdev)
1030{
1031 struct vreg *vreg = rdev_get_drvdata(rdev);
1032 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033 int rc;
1034
David Collins6f032ba2011-08-31 14:08:15 -07001035 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036
David Collins6f032ba2011-08-31 14:08:15 -07001037 val[vreg->part->pc.word]
1038 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1039 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040
David Collins6f032ba2011-08-31 14:08:15 -07001041 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1042 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043
David Collins6f032ba2011-08-31 14:08:15 -07001044 if (!vreg->is_enabled)
1045 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046
David Collins6f032ba2011-08-31 14:08:15 -07001047 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1048 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049
David Collins6f032ba2011-08-31 14:08:15 -07001050 if (!rc)
1051 vreg->is_enabled_pc = true;
1052
1053 mutex_unlock(&vreg->pc_lock);
1054
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001055 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001056 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057
David Collins6f032ba2011-08-31 14:08:15 -07001058 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059}
1060
David Collins6f032ba2011-08-31 14:08:15 -07001061static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062{
David Collins6f032ba2011-08-31 14:08:15 -07001063 struct vreg *vreg = rdev_get_drvdata(rdev);
1064 unsigned int mask[2] = {0}, val[2] = {0};
1065 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066
David Collins6f032ba2011-08-31 14:08:15 -07001067 mutex_lock(&vreg->pc_lock);
1068
1069 val[vreg->part->pc.word]
1070 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1071 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1072
1073 pin_fn = config->pin_func_none;
1074 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1075 pin_fn = config->pin_func_sleep_b;
1076 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1077 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1078
1079 if (!vreg->is_enabled)
1080 set_disable(vreg, mask, val);
1081
1082 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1083 vreg->part->request_len);
1084
1085 if (!rc)
1086 vreg->is_enabled_pc = false;
1087
1088 mutex_unlock(&vreg->pc_lock);
1089
1090 if (rc)
1091 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1092
1093 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094}
1095
David Collins6f032ba2011-08-31 14:08:15 -07001096static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097{
David Collins6f032ba2011-08-31 14:08:15 -07001098 struct vreg *vreg = rdev_get_drvdata(rdev);
1099
1100 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101}
1102
David Collins6f032ba2011-08-31 14:08:15 -07001103/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104static struct regulator_ops ldo_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001105 .enable = vreg_enable,
1106 .disable = vreg_disable,
1107 .is_enabled = vreg_is_enabled,
1108 .set_voltage = vreg_set_voltage,
1109 .get_voltage = vreg_get_voltage,
1110 .list_voltage = vreg_list_voltage,
1111 .set_mode = vreg_set_mode,
1112 .get_mode = vreg_get_mode,
1113 .get_optimum_mode = vreg_get_optimum_mode,
1114 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115};
1116
1117static struct regulator_ops smps_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001118 .enable = vreg_enable,
1119 .disable = vreg_disable,
1120 .is_enabled = vreg_is_enabled,
1121 .set_voltage = vreg_set_voltage,
1122 .get_voltage = vreg_get_voltage,
1123 .list_voltage = vreg_list_voltage,
1124 .set_mode = vreg_set_mode,
1125 .get_mode = vreg_get_mode,
1126 .get_optimum_mode = vreg_get_optimum_mode,
1127 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128};
1129
1130static struct regulator_ops switch_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001131 .enable = vreg_enable,
1132 .disable = vreg_disable,
1133 .is_enabled = vreg_is_enabled,
1134 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135};
1136
1137static struct regulator_ops ncp_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001138 .enable = vreg_enable,
1139 .disable = vreg_disable,
1140 .is_enabled = vreg_is_enabled,
1141 .set_voltage = vreg_set_voltage,
1142 .get_voltage = vreg_get_voltage,
1143 .list_voltage = vreg_list_voltage,
1144 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145};
1146
David Collins6f032ba2011-08-31 14:08:15 -07001147/* Pin control regulator operations. */
1148static struct regulator_ops pin_control_ops = {
1149 .enable = vreg_pin_control_enable,
1150 .disable = vreg_pin_control_disable,
1151 .is_enabled = vreg_pin_control_is_enabled,
1152};
1153
1154struct regulator_ops *vreg_ops[] = {
1155 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1156 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1157 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1158 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1159};
1160
1161static int __devinit
1162rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1163 struct device *dev)
1164{
1165 struct regulator_desc *rdesc = NULL;
1166 struct regulator_dev *rdev;
1167 struct vreg *vreg;
1168 unsigned pin_ctrl;
1169 int id, pin_fn;
1170 int rc = 0;
1171
1172 if (!pdata) {
1173 pr_err("platform data missing\n");
1174 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 }
1176
David Collins6f032ba2011-08-31 14:08:15 -07001177 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178
David Collins6f032ba2011-08-31 14:08:15 -07001179 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1180 pr_err("invalid regulator id: %d\n", id);
1181 return -ENODEV;
1182 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183
David Collins6f032ba2011-08-31 14:08:15 -07001184 if (!config->is_real_id(pdata->id))
1185 id = config->pc_id_to_real_id(pdata->id);
1186 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187
David Collins6f032ba2011-08-31 14:08:15 -07001188 if (config->is_real_id(pdata->id))
1189 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190 else
David Collins6f032ba2011-08-31 14:08:15 -07001191 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192
David Collins6f032ba2011-08-31 14:08:15 -07001193 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1194 pr_err("%s: invalid regulator type: %d\n",
1195 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001197 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198
David Collins6f032ba2011-08-31 14:08:15 -07001199 mutex_lock(&vreg->pc_lock);
1200
1201 if (vreg->set_points)
1202 rdesc->n_voltages = vreg->set_points->n_voltages;
1203 else
1204 rdesc->n_voltages = 0;
1205
1206 rdesc->id = pdata->id;
1207 rdesc->owner = THIS_MODULE;
1208 rdesc->type = REGULATOR_VOLTAGE;
1209
1210 if (config->is_real_id(pdata->id)) {
1211 /*
1212 * Real regulator; do not modify pin control and pin function
1213 * values.
1214 */
1215 rdesc->ops = vreg_ops[vreg->type];
1216 pin_ctrl = vreg->pdata.pin_ctrl;
1217 pin_fn = vreg->pdata.pin_fn;
1218 memcpy(&(vreg->pdata), pdata,
1219 sizeof(struct rpm_regulator_init_data));
1220 vreg->pdata.pin_ctrl = pin_ctrl;
1221 vreg->pdata.pin_fn = pin_fn;
1222
1223 vreg->save_uV = vreg->pdata.default_uV;
1224 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1225 vreg->mode = config->mode_hpm;
1226 else
1227 vreg->mode = config->mode_lpm;
1228
1229 /* Initialize the RPM request. */
1230 SET_PART(vreg, ip,
1231 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1232 SET_PART(vreg, fm, vreg->pdata.force_mode);
1233 SET_PART(vreg, pm, vreg->pdata.power_mode);
1234 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1235 SET_PART(vreg, ia,
1236 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1237 SET_PART(vreg, freq, vreg->pdata.freq);
1238 SET_PART(vreg, freq_clk_src, 0);
1239 SET_PART(vreg, comp_mode, 0);
1240 SET_PART(vreg, hpm, 0);
1241 if (!vreg->is_enabled_pc) {
1242 SET_PART(vreg, pf, config->pin_func_none);
1243 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1244 }
1245 } else {
1246 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1247 == RPM_VREG_PIN_CTRL_NONE
1248 && pdata->pin_fn != config->pin_func_sleep_b) {
1249 pr_err("%s: no pin control input specified\n",
1250 vreg->rdesc.name);
1251 mutex_unlock(&vreg->pc_lock);
1252 return -EINVAL;
1253 }
1254 rdesc->ops = &pin_control_ops;
1255 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1256 vreg->pdata.pin_fn = pdata->pin_fn;
1257
1258 /* Initialize the RPM request. */
1259 pin_fn = config->pin_func_none;
1260 /* Allow pf=sleep_b to be specified by platform data. */
1261 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1262 pin_fn = config->pin_func_sleep_b;
1263 SET_PART(vreg, pf, pin_fn);
1264 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1265 }
1266
1267 mutex_unlock(&vreg->pc_lock);
1268
1269 if (rc)
1270 goto bail;
1271
1272 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1273 if (IS_ERR(rdev)) {
1274 rc = PTR_ERR(rdev);
1275 pr_err("regulator_register failed: %s, rc=%d\n",
1276 vreg->rdesc.name, rc);
1277 return rc;
1278 } else {
1279 if (config->is_real_id(pdata->id))
1280 vreg->rdev = rdev;
1281 else
1282 vreg->rdev_pc = rdev;
1283 }
1284
1285bail:
1286 if (rc)
1287 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1288
1289 return rc;
1290}
1291
1292static void rpm_vreg_set_point_init(void)
1293{
1294 struct vreg_set_points **set_points;
1295 int i, j, temp;
1296
1297 set_points = config->set_points;
1298
1299 /* Calculate the number of set points available for each regulator. */
1300 for (i = 0; i < config->set_points_len; i++) {
1301 temp = 0;
1302 for (j = 0; j < set_points[i]->count; j++) {
1303 set_points[i]->range[j].n_voltages
1304 = (set_points[i]->range[j].max_uV
1305 - set_points[i]->range[j].min_uV)
1306 / set_points[i]->range[j].step_uV + 1;
1307 temp += set_points[i]->range[j].n_voltages;
1308 }
1309 set_points[i]->n_voltages = temp;
1310 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311}
1312
1313static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1314{
David Collins6f032ba2011-08-31 14:08:15 -07001315 struct rpm_regulator_platform_data *platform_data;
1316 int rc = 0;
1317 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318
David Collins6f032ba2011-08-31 14:08:15 -07001319 platform_data = pdev->dev.platform_data;
1320 if (!platform_data) {
1321 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001323 }
1324
David Collins6f032ba2011-08-31 14:08:15 -07001325 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1326 && platform_data->version != rpm_version) {
1327 pr_err("rpm version %d does not match previous version %d\n",
1328 platform_data->version, rpm_version);
1329 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 }
1331
David Collins6f032ba2011-08-31 14:08:15 -07001332 if (platform_data->version < 0
1333 || platform_data->version > RPM_VREG_VERSION_MAX) {
1334 pr_err("rpm version %d is invalid\n", platform_data->version);
1335 return -EINVAL;
1336 }
1337
1338 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1339 rpm_version = platform_data->version;
1340 config = get_config[platform_data->version]();
1341 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1342 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1343 if (!config) {
1344 pr_err("rpm version %d is not available\n",
1345 platform_data->version);
1346 return -ENODEV;
1347 }
1348 if (config->use_legacy_optimum_mode)
1349 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1350 vreg_ops[i]->get_optimum_mode
1351 = vreg_legacy_get_optimum_mode;
1352 rpm_vreg_set_point_init();
1353 /* First time probed; initialize pin control mutexes. */
1354 for (i = 0; i < config->vregs_len; i++)
1355 mutex_init(&config->vregs[i].pc_lock);
1356 }
1357
1358 /* Initialize all of the regulators listed in the platform data. */
1359 for (i = 0; i < platform_data->num_regulators; i++) {
1360 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1361 &pdev->dev);
1362 if (rc) {
1363 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1364 goto remove_regulators;
1365 }
1366 }
1367
1368 platform_set_drvdata(pdev, platform_data);
1369
1370 return rc;
1371
1372remove_regulators:
1373 /* Unregister all regulators added before the erroring one. */
1374 for (; i >= 0; i--) {
1375 id = platform_data->init_data[i].id;
1376 if (config->is_real_id(id)) {
1377 regulator_unregister(config->vregs[id].rdev);
1378 config->vregs[id].rdev = NULL;
1379 } else {
1380 regulator_unregister(config->vregs[
1381 config->pc_id_to_real_id(id)].rdev_pc);
1382 config->vregs[id].rdev_pc = NULL;
1383 }
1384 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385
1386 return rc;
1387}
1388
1389static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1390{
David Collins6f032ba2011-08-31 14:08:15 -07001391 struct rpm_regulator_platform_data *platform_data;
1392 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393
David Collins6f032ba2011-08-31 14:08:15 -07001394 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001395 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001396
1397 if (platform_data) {
1398 for (i = 0; i < platform_data->num_regulators; i++) {
1399 id = platform_data->init_data[i].id;
1400 if (config->is_real_id(id)) {
1401 regulator_unregister(config->vregs[id].rdev);
1402 config->vregs[id].rdev = NULL;
1403 } else {
1404 regulator_unregister(config->vregs[
1405 config->pc_id_to_real_id(id)].rdev_pc);
1406 config->vregs[id].rdev_pc = NULL;
1407 }
1408 }
1409 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410
1411 return 0;
1412}
1413
1414static struct platform_driver rpm_vreg_driver = {
1415 .probe = rpm_vreg_probe,
1416 .remove = __devexit_p(rpm_vreg_remove),
1417 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001418 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419 .owner = THIS_MODULE,
1420 },
1421};
1422
1423static int __init rpm_vreg_init(void)
1424{
1425 return platform_driver_register(&rpm_vreg_driver);
1426}
1427
1428static void __exit rpm_vreg_exit(void)
1429{
David Collins6f032ba2011-08-31 14:08:15 -07001430 int i;
1431
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001433
1434 for (i = 0; i < config->vregs_len; i++)
1435 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436}
1437
1438postcore_initcall(rpm_vreg_init);
1439module_exit(rpm_vreg_exit);
1440
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001441MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001442MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001444MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);