blob: c472f9471d6bbb6f5f55875b6275c1c80dbb6475 [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);
David Collins13397f22012-02-06 13:53:29 -0800271 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700272 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
David Collins13397f22012-02-06 13:53:29 -0800273 else if (vreg->part->enable_state.mask)
274 uV = GET_PART(vreg, enable_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
David Collins6f032ba2011-08-31 14:08:15 -0700276 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277}
278
David Collins6f032ba2011-08-31 14:08:15 -0700279static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280{
David Collins6f032ba2011-08-31 14:08:15 -0700281 if (vreg->part->uV.mask)
282 SET_PART(vreg, uV, uV);
David Collins13397f22012-02-06 13:53:29 -0800283 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700284 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
David Collins13397f22012-02-06 13:53:29 -0800285 else if (vreg->part->enable_state.mask)
286 SET_PART(vreg, enable_state, uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287}
288
289static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
290 int set, unsigned mask0, unsigned val0,
291 unsigned mask1, unsigned val1, unsigned cnt,
292 int update_voltage)
293{
294 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700295 int rc = 0, max_uV_vote = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700297 int *min_uV_vote;
298 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299
300 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700301 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 prev_req = vreg->prev_active_req;
303 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700304 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 prev_req = vreg->prev_sleep_req;
306 }
307
308 prev0 = vreg->req[0].value;
309 vreg->req[0].value &= ~mask0;
310 vreg->req[0].value |= val0 & mask0;
311
312 prev1 = vreg->req[1].value;
313 vreg->req[1].value &= ~mask1;
314 vreg->req[1].value |= val1 & mask1;
315
316 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700317 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318
319 /* Find the highest voltage voted for and use it. */
320 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700321 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
322 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323
324 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700325 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
326 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
328 /* Ignore duplicate requests */
329 if (vreg->req[0].value != prev_req[0].value ||
330 vreg->req[1].value != prev_req[1].value) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
332 if (rc) {
333 vreg->req[0].value = prev0;
334 vreg->req[1].value = prev1;
335
David Collins6f032ba2011-08-31 14:08:15 -0700336 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
337 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
339 vreg->req[0].id, rc);
340 } else {
341 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700342 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
343 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344 if (msm_rpm_vreg_debug_mask
345 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700346 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 prev_req[0].value = vreg->req[0].value;
348 prev_req[1].value = vreg->req[1].value;
349 }
350 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700351 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 }
353
354 return rc;
355}
356
357static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
358 int sleep, unsigned mask0, unsigned val0,
359 unsigned mask1, unsigned val1, unsigned cnt,
360 int update_voltage)
361{
David Collins6f032ba2011-08-31 14:08:15 -0700362 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700363 unsigned long flags;
364 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365
366 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
367 return -EINVAL;
368
David Collins6f032ba2011-08-31 14:08:15 -0700369 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370
371 /*
372 * Send sleep set request first so that subsequent set_mode, etc calls
373 * use the voltage from the active set.
374 */
375 if (sleep)
376 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
377 mask0, val0, mask1, val1, cnt, update_voltage);
378 else {
379 /*
380 * Vote for 0 V in the sleep set when active set-only is
381 * specified. This ensures that a disable vote will be issued
382 * at some point for the sleep set of the regulator.
383 */
David Collins6f032ba2011-08-31 14:08:15 -0700384 if (vreg->part->uV.mask) {
385 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
386 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800387 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700388 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
389 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800390 } else if (vreg->part->enable_state.mask) {
391 s_val[vreg->part->enable_state.word]
392 = 0 << vreg->part->enable_state.shift;
393 s_mask[vreg->part->enable_state.word]
394 = vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395 }
396
397 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700398 s_mask[0], s_val[0], s_mask[1], s_val[1],
399 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 }
401
402 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
403 mask1, val1, cnt, update_voltage);
404
David Collins6f032ba2011-08-31 14:08:15 -0700405 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406
407 return rc;
408}
409
410/**
411 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
412 * @vreg: ID for regulator
413 * @voter: ID for the voter
414 * @min_uV: minimum acceptable voltage (in uV) that is voted for
415 * @max_uV: maximum acceptable voltage (in uV) that is voted for
416 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
417 *
418 * Returns 0 on success or errno.
419 *
420 * This function is used to vote for the voltage of a regulator without
421 * using the regulator framework. It is needed by consumers which hold spin
422 * locks or have interrupts disabled because the regulator framework can sleep.
423 * It is also needed by consumers which wish to only vote for active set
424 * regulator voltage.
425 *
426 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
427 *
428 * This function may only be called for regulators which have the sleep flag
429 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700430 *
431 * Consumers can vote to disable a regulator with this function by passing
432 * min_uV = 0 and max_uV = 0.
David Collins13397f22012-02-06 13:53:29 -0800433 *
434 * Voltage switch type regulators may be controlled via rpm_vreg_set_voltage
435 * as well. For this type of regulator, max_uV > 0 is treated as an enable
436 * request and max_uV == 0 is treated as a disable request.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437 */
David Collins6f032ba2011-08-31 14:08:15 -0700438int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
439 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440{
David Collins6f032ba2011-08-31 14:08:15 -0700441 unsigned int mask[2] = {0}, val[2] = {0};
442 struct vreg_range *range;
443 struct vreg *vreg;
444 int uV = min_uV;
445 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
David Collins6f032ba2011-08-31 14:08:15 -0700447 /*
448 * HACK: make this function a no-op for 8064 so that it can be called by
449 * consumers on 8064 before RPM capabilities are present. (needed for
450 * acpuclock driver)
451 */
452 if (cpu_is_apq8064())
453 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454
David Collins6f032ba2011-08-31 14:08:15 -0700455 if (!config) {
456 pr_err("rpm-regulator driver has not probed yet.\n");
457 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 }
459
David Collins6f032ba2011-08-31 14:08:15 -0700460 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
461 pr_err("invalid regulator id=%d\n", vreg_id);
462 return -EINVAL;
463 }
464
465 vreg = &config->vregs[vreg_id];
David Collins6f032ba2011-08-31 14:08:15 -0700466
467 if (!vreg->pdata.sleep_selectable) {
468 vreg_err(vreg, "regulator is not marked sleep selectable\n");
469 return -EINVAL;
470 }
471
David Collins7462b9d2011-10-11 16:02:17 -0700472 /* Allow min_uV == max_uV == 0 to represent a disable request. */
David Collins13397f22012-02-06 13:53:29 -0800473 if ((min_uV != 0 || max_uV != 0)
474 && (vreg->part->uV.mask || vreg->part->mV.mask)) {
David Collins7462b9d2011-10-11 16:02:17 -0700475 /*
476 * Check if request voltage is outside of allowed range. The
477 * regulator core has already checked that constraint range
478 * is inside of the physically allowed range.
479 */
480 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
481 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700482
David Collins7462b9d2011-10-11 16:02:17 -0700483 if (uV < lim_min_uV && max_uV >= lim_min_uV)
484 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700485
David Collins7462b9d2011-10-11 16:02:17 -0700486 if (uV < lim_min_uV || uV > lim_max_uV) {
487 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
488 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
489 lim_max_uV);
490 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700491 }
David Collins6f032ba2011-08-31 14:08:15 -0700492
David Collins13397f22012-02-06 13:53:29 -0800493 range = &vreg->set_points->range[0];
David Collins7462b9d2011-10-11 16:02:17 -0700494 /* Find the range which uV is inside of. */
495 for (i = vreg->set_points->count - 1; i > 0; i--) {
496 if (uV > vreg->set_points->range[i - 1].max_uV) {
497 range = &vreg->set_points->range[i];
498 break;
499 }
500 }
501
502 /*
503 * Force uV to be an allowed set point and apply a ceiling
504 * function to non-set point values.
505 */
506 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
507 uV = uV * range->step_uV + range->min_uV;
David Collins3974b612011-11-21 15:07:36 -0800508
509 if (uV > max_uV) {
510 vreg_err(vreg,
511 "request v=[%d, %d] cannot be met by any set point; "
512 "next set point: %d\n",
513 min_uV, max_uV, uV);
514 return -EINVAL;
515 }
David Collins7462b9d2011-10-11 16:02:17 -0700516 }
David Collins6f032ba2011-08-31 14:08:15 -0700517
518 if (vreg->part->uV.mask) {
519 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
520 mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800521 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700522 val[vreg->part->mV.word]
523 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
524 mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800525 } else if (vreg->part->enable_state.mask) {
526 /*
527 * Translate max_uV > 0 into an enable request for regulator
528 * types which to not support voltage setting, e.g. voltage
529 * switches.
530 */
531 val[vreg->part->enable_state.word]
532 = (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift;
533 mask[vreg->part->enable_state.word]
534 = vreg->part->enable_state.mask;
David Collins6f032ba2011-08-31 14:08:15 -0700535 }
536
537 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
538 val[1], vreg->part->request_len, 1);
539 if (rc)
540 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541
542 return rc;
543}
544EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
545
546/**
547 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
548 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700549 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 *
551 * Returns 0 on success or errno.
552 */
David Collins6f032ba2011-08-31 14:08:15 -0700553int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554{
David Collins6f032ba2011-08-31 14:08:15 -0700555 unsigned int mask[2] = {0}, val[2] = {0};
556 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 int rc;
558
David Collins6f032ba2011-08-31 14:08:15 -0700559 /*
560 * HACK: make this function a no-op for 8064 so that it can be called by
561 * consumers on 8064 before RPM capabilities are present.
562 */
563 if (cpu_is_apq8064())
564 return 0;
565
566 if (!config) {
567 pr_err("rpm-regulator driver has not probed yet.\n");
568 return -ENODEV;
569 }
570
571 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
572 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 return -EINVAL;
574 }
575
David Collins6f032ba2011-08-31 14:08:15 -0700576 vreg = &config->vregs[vreg_id];
577
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700579 vreg_err(vreg, "invalid frequency=%d\n", freq);
580 return -EINVAL;
581 }
582 if (!vreg->pdata.sleep_selectable) {
583 vreg_err(vreg, "regulator is not marked sleep selectable\n");
584 return -EINVAL;
585 }
586 if (!vreg->part->freq.mask) {
587 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588 return -EINVAL;
589 }
590
David Collins6f032ba2011-08-31 14:08:15 -0700591 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
592 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
David Collins6f032ba2011-08-31 14:08:15 -0700594 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
595 val[0], mask[1], val[1], vreg->part->request_len, 0);
596 if (rc)
597 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598
599 return rc;
600}
601EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603static inline int vreg_hpm_min_uA(struct vreg *vreg)
604{
605 return vreg->hpm_min_load;
606}
607
608static inline int vreg_lpm_max_uA(struct vreg *vreg)
609{
610 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
611}
612
David Collins6f032ba2011-08-31 14:08:15 -0700613static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614{
David Collins6f032ba2011-08-31 14:08:15 -0700615 unsigned load_max
616 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
617
618 return (load_uA > load_max ? load_max : load_uA);
619}
620
621static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
622{
623 unsigned load_max
624 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
625 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626}
627
628/* Change vreg->req, but do not send it to the RPM. */
629static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
630 unsigned mask1, unsigned val1)
631{
632 unsigned long flags = 0;
633
David Collins6f032ba2011-08-31 14:08:15 -0700634 if (vreg->pdata.sleep_selectable)
635 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636
637 vreg->req[0].value &= ~mask0;
638 vreg->req[0].value |= val0 & mask0;
639
640 vreg->req[1].value &= ~mask1;
641 vreg->req[1].value |= val1 & mask1;
642
David Collins6f032ba2011-08-31 14:08:15 -0700643 if (vreg->pdata.sleep_selectable)
644 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645
646 return 0;
647}
648
649static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
650 unsigned mask1, unsigned val1, unsigned cnt)
651{
652 unsigned prev0 = 0, prev1 = 0;
653 int rc;
654
655 /*
656 * Bypass the normal route for regulators that can be called to change
657 * just the active set values.
658 */
David Collins6f032ba2011-08-31 14:08:15 -0700659 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
661 mask0, val0, mask1, val1, cnt, 1);
662
663 prev0 = vreg->req[0].value;
664 vreg->req[0].value &= ~mask0;
665 vreg->req[0].value |= val0 & mask0;
666
667 prev1 = vreg->req[1].value;
668 vreg->req[1].value &= ~mask1;
669 vreg->req[1].value |= val1 & mask1;
670
671 /* Ignore duplicate requests */
672 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
673 vreg->req[1].value == vreg->prev_active_req[1].value) {
674 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700675 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 return 0;
677 }
678
679 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
680 if (rc) {
681 vreg->req[0].value = prev0;
682 vreg->req[1].value = prev1;
683
David Collins6f032ba2011-08-31 14:08:15 -0700684 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
685 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 } else {
687 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700688 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 vreg->prev_active_req[0].value = vreg->req[0].value;
690 vreg->prev_active_req[1].value = vreg->req[1].value;
691 }
692
693 return rc;
694}
695
David Collins6f032ba2011-08-31 14:08:15 -0700696static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697{
David Collins6f032ba2011-08-31 14:08:15 -0700698 struct vreg *vreg = rdev_get_drvdata(rdev);
699 int enabled;
700
701 mutex_lock(&vreg->pc_lock);
702 enabled = vreg->is_enabled;
703 mutex_unlock(&vreg->pc_lock);
704
705 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706}
707
David Collins6f032ba2011-08-31 14:08:15 -0700708static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709{
David Collins6f032ba2011-08-31 14:08:15 -0700710 switch (vreg->type) {
711 case RPM_REGULATOR_TYPE_LDO:
712 case RPM_REGULATOR_TYPE_SMPS:
713 /* Enable by setting a voltage. */
714 if (vreg->part->uV.mask) {
715 val[vreg->part->uV.word]
716 |= vreg->save_uV << vreg->part->uV.shift;
717 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
718 } else {
719 val[vreg->part->mV.word]
720 |= MICRO_TO_MILLI(vreg->save_uV)
721 << vreg->part->mV.shift;
722 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
723 }
724 break;
725 case RPM_REGULATOR_TYPE_VS:
726 case RPM_REGULATOR_TYPE_NCP:
727 /* Enable by setting enable_state. */
728 val[vreg->part->enable_state.word]
729 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
730 mask[vreg->part->enable_state.word]
731 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733}
734
David Collins6f032ba2011-08-31 14:08:15 -0700735static int vreg_enable(struct regulator_dev *rdev)
736{
737 struct vreg *vreg = rdev_get_drvdata(rdev);
738 unsigned int mask[2] = {0}, val[2] = {0};
739 int rc = 0;
740
741 set_enable(vreg, mask, val);
742
743 mutex_lock(&vreg->pc_lock);
744
745 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
746 vreg->part->request_len);
747 if (!rc)
748 vreg->is_enabled = true;
749
750 mutex_unlock(&vreg->pc_lock);
751
752 if (rc)
753 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
754
755 return rc;
756}
757
758static void set_disable(struct vreg *vreg, unsigned int *mask,
759 unsigned int *val)
760{
761 switch (vreg->type) {
762 case RPM_REGULATOR_TYPE_LDO:
763 case RPM_REGULATOR_TYPE_SMPS:
764 /* Disable by setting a voltage of 0 uV. */
765 if (vreg->part->uV.mask) {
766 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
767 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
768 } else {
769 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
770 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
771 }
772 break;
773 case RPM_REGULATOR_TYPE_VS:
774 case RPM_REGULATOR_TYPE_NCP:
775 /* Disable by setting enable_state. */
776 val[vreg->part->enable_state.word]
777 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
778 mask[vreg->part->enable_state.word]
779 |= vreg->part->enable_state.mask;
780 }
781}
782
783static int vreg_disable(struct regulator_dev *rdev)
784{
785 struct vreg *vreg = rdev_get_drvdata(rdev);
786 unsigned int mask[2] = {0}, val[2] = {0};
787 int rc = 0;
788
789 set_disable(vreg, mask, val);
790
791 mutex_lock(&vreg->pc_lock);
792
793 /* Only disable if pin control is not in use. */
794 if (!vreg->is_enabled_pc)
795 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
796 vreg->part->request_len);
797
798 if (!rc)
799 vreg->is_enabled = false;
800
801 mutex_unlock(&vreg->pc_lock);
802
803 if (rc)
804 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
805
806 return rc;
807}
808
809static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810 unsigned *selector)
811{
David Collins6f032ba2011-08-31 14:08:15 -0700812 struct vreg *vreg = rdev_get_drvdata(rdev);
813 struct vreg_range *range = &vreg->set_points->range[0];
814 unsigned int mask[2] = {0}, val[2] = {0};
815 int rc = 0, uV = min_uV;
816 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817
David Collins6f032ba2011-08-31 14:08:15 -0700818 /* Check if request voltage is outside of physically settable range. */
819 lim_min_uV = vreg->set_points->range[0].min_uV;
820 lim_max_uV =
821 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822
David Collins6f032ba2011-08-31 14:08:15 -0700823 if (uV < lim_min_uV && max_uV >= lim_min_uV)
824 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825
David Collins6f032ba2011-08-31 14:08:15 -0700826 if (uV < lim_min_uV || uV > lim_max_uV) {
827 vreg_err(vreg,
828 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
829 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 return -EINVAL;
831 }
832
David Collins6f032ba2011-08-31 14:08:15 -0700833 /* Find the range which uV is inside of. */
834 for (i = vreg->set_points->count - 1; i > 0; i--) {
835 if (uV > vreg->set_points->range[i - 1].max_uV) {
836 range = &vreg->set_points->range[i];
837 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 }
839 }
840
David Collins6f032ba2011-08-31 14:08:15 -0700841 /*
842 * Force uV to be an allowed set point and apply a ceiling function
843 * to non-set point values.
844 */
845 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
846 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847
David Collins3974b612011-11-21 15:07:36 -0800848 if (uV > max_uV) {
849 vreg_err(vreg,
850 "request v=[%d, %d] cannot be met by any set point; "
851 "next set point: %d\n",
852 min_uV, max_uV, uV);
853 return -EINVAL;
854 }
855
David Collins6f032ba2011-08-31 14:08:15 -0700856 if (vreg->part->uV.mask) {
857 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
858 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700860 val[vreg->part->mV.word]
861 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
862 mask[vreg->part->mV.word] = vreg->part->mV.mask;
863 }
864
865 mutex_lock(&vreg->pc_lock);
866
867 /*
868 * Only send a request for a new voltage if the regulator is currently
869 * enabled. This will ensure that LDO and SMPS regulators are not
870 * inadvertently turned on because voltage > 0 is equivalent to
871 * enabling. For NCP, this just removes unnecessary RPM requests.
872 */
873 if (vreg->is_enabled) {
874 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
875 vreg->part->request_len);
876 if (rc)
877 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
878 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700880 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 }
882
David Collins6f032ba2011-08-31 14:08:15 -0700883 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
884 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885
David Collins6f032ba2011-08-31 14:08:15 -0700886 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887
888 return rc;
889}
890
David Collins6f032ba2011-08-31 14:08:15 -0700891static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892{
David Collins6f032ba2011-08-31 14:08:15 -0700893 struct vreg *vreg = rdev_get_drvdata(rdev);
894
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 return vreg->save_uV;
896}
897
David Collins6f032ba2011-08-31 14:08:15 -0700898static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899{
David Collins6f032ba2011-08-31 14:08:15 -0700900 struct vreg *vreg = rdev_get_drvdata(rdev);
901 int uV = 0;
902 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903
David Collins6f032ba2011-08-31 14:08:15 -0700904 if (!vreg->set_points) {
905 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 return -EINVAL;
907 }
908
David Collins6f032ba2011-08-31 14:08:15 -0700909 if (selector >= vreg->set_points->n_voltages)
910 return 0;
911
912 for (i = 0; i < vreg->set_points->count; i++) {
913 if (selector < vreg->set_points->range[i].n_voltages) {
914 uV = selector * vreg->set_points->range[i].step_uV
915 + vreg->set_points->range[i].min_uV;
916 break;
917 } else {
918 selector -= vreg->set_points->range[i].n_voltages;
919 }
920 }
921
922 return uV;
923}
924
925static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
926{
927 struct vreg *vreg = rdev_get_drvdata(rdev);
928 unsigned int mask[2] = {0}, val[2] = {0};
929 int rc = 0;
930 int peak_uA;
931
932 mutex_lock(&vreg->pc_lock);
933
934 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
935 & vreg->part->ip.mask) >> vreg->part->ip.shift);
936
937 if (mode == config->mode_hpm) {
938 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700940 val[vreg->part->ip.word]
941 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
942 << vreg->part->ip.shift;
943 mask[vreg->part->ip.word] = vreg->part->ip.mask;
944
945 if (config->ia_follows_ip) {
946 val[vreg->part->ia.word]
947 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
948 << vreg->part->ia.shift;
949 mask[vreg->part->ia.word]
950 |= vreg->part->ia.mask;
951 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 }
David Collins6f032ba2011-08-31 14:08:15 -0700953 } else if (mode == config->mode_lpm) {
954 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700956 val[vreg->part->ip.word]
957 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
958 << vreg->part->ip.shift;
959 mask[vreg->part->ip.word] = vreg->part->ip.mask;
960
961 if (config->ia_follows_ip) {
962 val[vreg->part->ia.word]
963 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
964 << vreg->part->ia.shift;
965 mask[vreg->part->ia.word]
966 |= vreg->part->ia.mask;
967 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 }
David Collins6f032ba2011-08-31 14:08:15 -0700969 } else {
970 vreg_err(vreg, "invalid mode: %u\n", mode);
971 mutex_unlock(&vreg->pc_lock);
972 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 }
974
David Collins6f032ba2011-08-31 14:08:15 -0700975 if (vreg->is_enabled) {
976 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
977 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978 } else {
979 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700980 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 }
David Collins6f032ba2011-08-31 14:08:15 -0700982
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -0700984 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
985 else
986 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987
David Collins6f032ba2011-08-31 14:08:15 -0700988 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989
David Collins6f032ba2011-08-31 14:08:15 -0700990 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991}
992
David Collins6f032ba2011-08-31 14:08:15 -0700993static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994{
David Collins6f032ba2011-08-31 14:08:15 -0700995 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996
David Collins6f032ba2011-08-31 14:08:15 -0700997 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998}
999
David Collins6f032ba2011-08-31 14:08:15 -07001000static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
1001 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002{
David Collins6f032ba2011-08-31 14:08:15 -07001003 struct vreg *vreg = rdev_get_drvdata(rdev);
1004 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005
David Collins6f032ba2011-08-31 14:08:15 -07001006 load_uA += vreg->pdata.system_uA;
1007
1008 mutex_lock(&vreg->pc_lock);
1009 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
1010 if (config->ia_follows_ip)
1011 SET_PART(vreg, ia,
1012 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
1013 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014
1015 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -07001016 mode = config->mode_hpm;
1017 else
1018 mode = config->mode_lpm;
1019
1020 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021}
1022
David Collins6f032ba2011-08-31 14:08:15 -07001023static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
1024 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025{
David Collins6f032ba2011-08-31 14:08:15 -07001026 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027
David Collins6f032ba2011-08-31 14:08:15 -07001028 if (MICRO_TO_MILLI(load_uA) <= 0) {
1029 /*
1030 * vreg_legacy_get_optimum_mode is being called before consumers
1031 * have specified their load currents via
1032 * regulator_set_optimum_mode. Return whatever the existing mode
1033 * is.
1034 */
1035 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 }
1037
David Collins6f032ba2011-08-31 14:08:15 -07001038 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039}
1040
1041/*
David Collins6f032ba2011-08-31 14:08:15 -07001042 * Returns the logical pin control enable state because the pin control options
1043 * present in the hardware out of restart could be different from those desired
1044 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 */
David Collins6f032ba2011-08-31 14:08:15 -07001046static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047{
David Collins6f032ba2011-08-31 14:08:15 -07001048 struct vreg *vreg = rdev_get_drvdata(rdev);
1049
1050 return vreg->is_enabled_pc;
1051}
1052
1053static int vreg_pin_control_enable(struct regulator_dev *rdev)
1054{
1055 struct vreg *vreg = rdev_get_drvdata(rdev);
1056 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 int rc;
1058
David Collins6f032ba2011-08-31 14:08:15 -07001059 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060
David Collins6f032ba2011-08-31 14:08:15 -07001061 val[vreg->part->pc.word]
1062 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1063 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064
David Collins6f032ba2011-08-31 14:08:15 -07001065 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1066 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067
David Collins6f032ba2011-08-31 14:08:15 -07001068 if (!vreg->is_enabled)
1069 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070
David Collins6f032ba2011-08-31 14:08:15 -07001071 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1072 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073
David Collins6f032ba2011-08-31 14:08:15 -07001074 if (!rc)
1075 vreg->is_enabled_pc = true;
1076
1077 mutex_unlock(&vreg->pc_lock);
1078
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001079 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001080 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001081
David Collins6f032ba2011-08-31 14:08:15 -07001082 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083}
1084
David Collins6f032ba2011-08-31 14:08:15 -07001085static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086{
David Collins6f032ba2011-08-31 14:08:15 -07001087 struct vreg *vreg = rdev_get_drvdata(rdev);
1088 unsigned int mask[2] = {0}, val[2] = {0};
1089 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090
David Collins6f032ba2011-08-31 14:08:15 -07001091 mutex_lock(&vreg->pc_lock);
1092
1093 val[vreg->part->pc.word]
1094 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1095 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1096
1097 pin_fn = config->pin_func_none;
1098 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1099 pin_fn = config->pin_func_sleep_b;
1100 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1101 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1102
1103 if (!vreg->is_enabled)
1104 set_disable(vreg, mask, val);
1105
1106 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1107 vreg->part->request_len);
1108
1109 if (!rc)
1110 vreg->is_enabled_pc = false;
1111
1112 mutex_unlock(&vreg->pc_lock);
1113
1114 if (rc)
1115 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1116
1117 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118}
1119
David Collins6f032ba2011-08-31 14:08:15 -07001120static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121{
David Collins6f032ba2011-08-31 14:08:15 -07001122 struct vreg *vreg = rdev_get_drvdata(rdev);
1123
1124 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125}
1126
David Collins6f032ba2011-08-31 14:08:15 -07001127/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128static struct regulator_ops ldo_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001129 .enable = vreg_enable,
1130 .disable = vreg_disable,
1131 .is_enabled = vreg_is_enabled,
1132 .set_voltage = vreg_set_voltage,
1133 .get_voltage = vreg_get_voltage,
1134 .list_voltage = vreg_list_voltage,
1135 .set_mode = vreg_set_mode,
1136 .get_mode = vreg_get_mode,
1137 .get_optimum_mode = vreg_get_optimum_mode,
1138 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139};
1140
1141static struct regulator_ops smps_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001142 .enable = vreg_enable,
1143 .disable = vreg_disable,
1144 .is_enabled = vreg_is_enabled,
1145 .set_voltage = vreg_set_voltage,
1146 .get_voltage = vreg_get_voltage,
1147 .list_voltage = vreg_list_voltage,
1148 .set_mode = vreg_set_mode,
1149 .get_mode = vreg_get_mode,
1150 .get_optimum_mode = vreg_get_optimum_mode,
1151 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152};
1153
1154static struct regulator_ops switch_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001155 .enable = vreg_enable,
1156 .disable = vreg_disable,
1157 .is_enabled = vreg_is_enabled,
1158 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159};
1160
1161static struct regulator_ops ncp_ops = {
David Collins6f032ba2011-08-31 14:08:15 -07001162 .enable = vreg_enable,
1163 .disable = vreg_disable,
1164 .is_enabled = vreg_is_enabled,
1165 .set_voltage = vreg_set_voltage,
1166 .get_voltage = vreg_get_voltage,
1167 .list_voltage = vreg_list_voltage,
1168 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169};
1170
David Collins6f032ba2011-08-31 14:08:15 -07001171/* Pin control regulator operations. */
1172static struct regulator_ops pin_control_ops = {
1173 .enable = vreg_pin_control_enable,
1174 .disable = vreg_pin_control_disable,
1175 .is_enabled = vreg_pin_control_is_enabled,
1176};
1177
1178struct regulator_ops *vreg_ops[] = {
1179 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1180 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1181 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1182 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1183};
1184
1185static int __devinit
1186rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1187 struct device *dev)
1188{
1189 struct regulator_desc *rdesc = NULL;
1190 struct regulator_dev *rdev;
1191 struct vreg *vreg;
1192 unsigned pin_ctrl;
1193 int id, pin_fn;
1194 int rc = 0;
1195
1196 if (!pdata) {
1197 pr_err("platform data missing\n");
1198 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199 }
1200
David Collins6f032ba2011-08-31 14:08:15 -07001201 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202
David Collins6f032ba2011-08-31 14:08:15 -07001203 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1204 pr_err("invalid regulator id: %d\n", id);
1205 return -ENODEV;
1206 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207
David Collins6f032ba2011-08-31 14:08:15 -07001208 if (!config->is_real_id(pdata->id))
1209 id = config->pc_id_to_real_id(pdata->id);
1210 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211
David Collins6f032ba2011-08-31 14:08:15 -07001212 if (config->is_real_id(pdata->id))
1213 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214 else
David Collins6f032ba2011-08-31 14:08:15 -07001215 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216
David Collins6f032ba2011-08-31 14:08:15 -07001217 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1218 pr_err("%s: invalid regulator type: %d\n",
1219 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001221 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222
David Collins6f032ba2011-08-31 14:08:15 -07001223 mutex_lock(&vreg->pc_lock);
1224
1225 if (vreg->set_points)
1226 rdesc->n_voltages = vreg->set_points->n_voltages;
1227 else
1228 rdesc->n_voltages = 0;
1229
1230 rdesc->id = pdata->id;
1231 rdesc->owner = THIS_MODULE;
1232 rdesc->type = REGULATOR_VOLTAGE;
1233
1234 if (config->is_real_id(pdata->id)) {
1235 /*
1236 * Real regulator; do not modify pin control and pin function
1237 * values.
1238 */
1239 rdesc->ops = vreg_ops[vreg->type];
1240 pin_ctrl = vreg->pdata.pin_ctrl;
1241 pin_fn = vreg->pdata.pin_fn;
1242 memcpy(&(vreg->pdata), pdata,
1243 sizeof(struct rpm_regulator_init_data));
1244 vreg->pdata.pin_ctrl = pin_ctrl;
1245 vreg->pdata.pin_fn = pin_fn;
1246
1247 vreg->save_uV = vreg->pdata.default_uV;
1248 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1249 vreg->mode = config->mode_hpm;
1250 else
1251 vreg->mode = config->mode_lpm;
1252
1253 /* Initialize the RPM request. */
1254 SET_PART(vreg, ip,
1255 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1256 SET_PART(vreg, fm, vreg->pdata.force_mode);
1257 SET_PART(vreg, pm, vreg->pdata.power_mode);
1258 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1259 SET_PART(vreg, ia,
1260 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1261 SET_PART(vreg, freq, vreg->pdata.freq);
1262 SET_PART(vreg, freq_clk_src, 0);
1263 SET_PART(vreg, comp_mode, 0);
1264 SET_PART(vreg, hpm, 0);
1265 if (!vreg->is_enabled_pc) {
1266 SET_PART(vreg, pf, config->pin_func_none);
1267 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1268 }
1269 } else {
1270 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1271 == RPM_VREG_PIN_CTRL_NONE
1272 && pdata->pin_fn != config->pin_func_sleep_b) {
1273 pr_err("%s: no pin control input specified\n",
1274 vreg->rdesc.name);
1275 mutex_unlock(&vreg->pc_lock);
1276 return -EINVAL;
1277 }
1278 rdesc->ops = &pin_control_ops;
1279 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1280 vreg->pdata.pin_fn = pdata->pin_fn;
1281
1282 /* Initialize the RPM request. */
1283 pin_fn = config->pin_func_none;
1284 /* Allow pf=sleep_b to be specified by platform data. */
1285 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1286 pin_fn = config->pin_func_sleep_b;
1287 SET_PART(vreg, pf, pin_fn);
1288 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1289 }
1290
1291 mutex_unlock(&vreg->pc_lock);
1292
1293 if (rc)
1294 goto bail;
1295
1296 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1297 if (IS_ERR(rdev)) {
1298 rc = PTR_ERR(rdev);
1299 pr_err("regulator_register failed: %s, rc=%d\n",
1300 vreg->rdesc.name, rc);
1301 return rc;
1302 } else {
1303 if (config->is_real_id(pdata->id))
1304 vreg->rdev = rdev;
1305 else
1306 vreg->rdev_pc = rdev;
1307 }
1308
1309bail:
1310 if (rc)
1311 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1312
1313 return rc;
1314}
1315
1316static void rpm_vreg_set_point_init(void)
1317{
1318 struct vreg_set_points **set_points;
1319 int i, j, temp;
1320
1321 set_points = config->set_points;
1322
1323 /* Calculate the number of set points available for each regulator. */
1324 for (i = 0; i < config->set_points_len; i++) {
1325 temp = 0;
1326 for (j = 0; j < set_points[i]->count; j++) {
1327 set_points[i]->range[j].n_voltages
1328 = (set_points[i]->range[j].max_uV
1329 - set_points[i]->range[j].min_uV)
1330 / set_points[i]->range[j].step_uV + 1;
1331 temp += set_points[i]->range[j].n_voltages;
1332 }
1333 set_points[i]->n_voltages = temp;
1334 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335}
1336
1337static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1338{
David Collins6f032ba2011-08-31 14:08:15 -07001339 struct rpm_regulator_platform_data *platform_data;
1340 int rc = 0;
1341 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342
David Collins6f032ba2011-08-31 14:08:15 -07001343 platform_data = pdev->dev.platform_data;
1344 if (!platform_data) {
1345 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347 }
1348
David Collins6f032ba2011-08-31 14:08:15 -07001349 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1350 && platform_data->version != rpm_version) {
1351 pr_err("rpm version %d does not match previous version %d\n",
1352 platform_data->version, rpm_version);
1353 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001354 }
1355
David Collins6f032ba2011-08-31 14:08:15 -07001356 if (platform_data->version < 0
1357 || platform_data->version > RPM_VREG_VERSION_MAX) {
1358 pr_err("rpm version %d is invalid\n", platform_data->version);
1359 return -EINVAL;
1360 }
1361
1362 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1363 rpm_version = platform_data->version;
1364 config = get_config[platform_data->version]();
1365 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1366 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1367 if (!config) {
1368 pr_err("rpm version %d is not available\n",
1369 platform_data->version);
1370 return -ENODEV;
1371 }
1372 if (config->use_legacy_optimum_mode)
1373 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1374 vreg_ops[i]->get_optimum_mode
1375 = vreg_legacy_get_optimum_mode;
1376 rpm_vreg_set_point_init();
1377 /* First time probed; initialize pin control mutexes. */
1378 for (i = 0; i < config->vregs_len; i++)
1379 mutex_init(&config->vregs[i].pc_lock);
1380 }
1381
1382 /* Initialize all of the regulators listed in the platform data. */
1383 for (i = 0; i < platform_data->num_regulators; i++) {
1384 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1385 &pdev->dev);
1386 if (rc) {
1387 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1388 goto remove_regulators;
1389 }
1390 }
1391
1392 platform_set_drvdata(pdev, platform_data);
1393
1394 return rc;
1395
1396remove_regulators:
1397 /* Unregister all regulators added before the erroring one. */
1398 for (; i >= 0; 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 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409
1410 return rc;
1411}
1412
1413static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1414{
David Collins6f032ba2011-08-31 14:08:15 -07001415 struct rpm_regulator_platform_data *platform_data;
1416 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417
David Collins6f032ba2011-08-31 14:08:15 -07001418 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001420
1421 if (platform_data) {
1422 for (i = 0; i < platform_data->num_regulators; i++) {
1423 id = platform_data->init_data[i].id;
1424 if (config->is_real_id(id)) {
1425 regulator_unregister(config->vregs[id].rdev);
1426 config->vregs[id].rdev = NULL;
1427 } else {
1428 regulator_unregister(config->vregs[
1429 config->pc_id_to_real_id(id)].rdev_pc);
1430 config->vregs[id].rdev_pc = NULL;
1431 }
1432 }
1433 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001434
1435 return 0;
1436}
1437
1438static struct platform_driver rpm_vreg_driver = {
1439 .probe = rpm_vreg_probe,
1440 .remove = __devexit_p(rpm_vreg_remove),
1441 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001442 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443 .owner = THIS_MODULE,
1444 },
1445};
1446
1447static int __init rpm_vreg_init(void)
1448{
1449 return platform_driver_register(&rpm_vreg_driver);
1450}
1451
1452static void __exit rpm_vreg_exit(void)
1453{
David Collins6f032ba2011-08-31 14:08:15 -07001454 int i;
1455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001457
1458 for (i = 0; i < config->vregs_len; i++)
1459 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460}
1461
1462postcore_initcall(rpm_vreg_init);
1463module_exit(rpm_vreg_exit);
1464
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001465MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001466MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001468MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);