blob: fa369b3d32345d3e65d29f5fbe6a9a769d31ce0e [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};
David Collins0ac31fe2012-02-08 13:53:34 -080093
94static const char *label_corner[] = {
95 [RPM_VREG_CORNER_NONE] = "NONE",
96 [RPM_VREG_CORNER_LOW] = "LOW",
97 [RPM_VREG_CORNER_NOMINAL] = "NOM",
98 [RPM_VREG_CORNER_HIGH] = "HIGH",
99};
100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101/*
102 * This is used when voting for LPM or HPM by subtracting or adding to the
103 * hpm_min_load of a regulator. It has units of uA.
104 */
David Collins6f032ba2011-08-31 14:08:15 -0700105#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106
David Collins6f032ba2011-08-31 14:08:15 -0700107/* rpm_version keeps track of the version for the currently running driver. */
108enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
David Collins6f032ba2011-08-31 14:08:15 -0700110/* config holds all configuration data of the currently running driver. */
111static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112
David Collins6f032ba2011-08-31 14:08:15 -0700113/* These regulator ID values are specified in the board file. */
114static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115
David Collins6f032ba2011-08-31 14:08:15 -0700116static inline int vreg_id_is_vdd_mem_or_dig(int id)
117{
118 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
119}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120
David Collins6f032ba2011-08-31 14:08:15 -0700121#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122
David Collins6f032ba2011-08-31 14:08:15 -0700123static void rpm_regulator_req(struct vreg *vreg, int set)
124{
125 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
126 const char *pf_label = "", *fm_label = "", *pc_total = "";
127 const char *pc_en[4] = {"", "", "", ""};
David Collins0ac31fe2012-02-08 13:53:34 -0800128 const char *pm_label = "", *freq_label = "", *corner_label = "";
David Collins6f032ba2011-08-31 14:08:15 -0700129 char buf[DEBUG_PRINT_BUFFER_SIZE];
130 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
131 int pos = 0;
132
133 /* Suppress VDD_MEM and VDD_DIG printing. */
134 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
135 && vreg_id_is_vdd_mem_or_dig(vreg->id))
136 return;
137
138 uV = GET_PART(vreg, uV);
139 mV = GET_PART(vreg, mV);
140 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
141 uV = -uV;
142 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 }
144
David Collins6f032ba2011-08-31 14:08:15 -0700145 fm = GET_PART(vreg, fm);
146 pm = GET_PART(vreg, pm);
147 pc = GET_PART(vreg, pc);
148 pf = GET_PART(vreg, pf);
149 pd = GET_PART(vreg, pd);
150 freq = GET_PART(vreg, freq);
151 state = GET_PART(vreg, enable_state);
152
153 if (pf >= 0 && pf < config->label_pin_func_len)
154 pf_label = config->label_pin_func[pf];
155
156 if (fm >= 0 && fm < config->label_force_mode_len)
157 fm_label = config->label_force_mode[fm];
158
159 if (pm >= 0 && pm < config->label_power_mode_len)
160 pm_label = config->label_power_mode[pm];
161
162 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
163 freq_label = label_freq[freq];
164
165 for (i = 0; i < config->label_pin_ctrl_len; i++)
166 if (pc & (1 << i))
167 pc_en[i] = config->label_pin_ctrl[i];
168
169 if (pc == RPM_VREG_PIN_CTRL_NONE)
170 pc_total = " none";
171
172 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
173 KERN_INFO, __func__);
174
175 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
176 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
177 vreg->rdesc.name,
178 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
179
David Collins0ac31fe2012-02-08 13:53:34 -0800180 if (USES_PART(vreg, uV) && vreg->type != RPM_REGULATOR_TYPE_CORNER)
David Collins6f032ba2011-08-31 14:08:15 -0700181 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
182 if (USES_PART(vreg, mV))
183 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
184 if (USES_PART(vreg, enable_state))
185 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
186 (state == 1 ? "on" : "off"), state);
187 if (USES_PART(vreg, ip))
188 pos += scnprintf(buf + pos, buflen - pos,
189 ", ip=%4d mA", GET_PART(vreg, ip));
190 if (USES_PART(vreg, fm))
191 pos += scnprintf(buf + pos, buflen - pos,
192 ", fm=%s (%d)", fm_label, fm);
193 if (USES_PART(vreg, pc))
194 pos += scnprintf(buf + pos, buflen - pos,
195 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
196 pc_en[2], pc_en[3], pc_total, pc);
197 if (USES_PART(vreg, pf))
198 pos += scnprintf(buf + pos, buflen - pos,
199 ", pf=%s (%d)", pf_label, pf);
200 if (USES_PART(vreg, pd))
201 pos += scnprintf(buf + pos, buflen - pos,
202 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
203 if (USES_PART(vreg, ia))
204 pos += scnprintf(buf + pos, buflen - pos,
205 ", ia=%4d mA", GET_PART(vreg, ia));
206 if (USES_PART(vreg, freq)) {
207 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
208 pos += scnprintf(buf + pos, buflen - pos,
209 ", freq=%2d", freq);
210 else
211 pos += scnprintf(buf + pos, buflen - pos,
212 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 }
David Collins6f032ba2011-08-31 14:08:15 -0700214 if (USES_PART(vreg, pm))
215 pos += scnprintf(buf + pos, buflen - pos,
216 ", pm=%s (%d)", pm_label, pm);
217 if (USES_PART(vreg, freq_clk_src))
218 pos += scnprintf(buf + pos, buflen - pos,
219 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
220 if (USES_PART(vreg, comp_mode))
221 pos += scnprintf(buf + pos, buflen - pos,
222 ", comp=%d", GET_PART(vreg, comp_mode));
223 if (USES_PART(vreg, hpm))
224 pos += scnprintf(buf + pos, buflen - pos,
225 ", hpm=%d", GET_PART(vreg, hpm));
David Collins0ac31fe2012-02-08 13:53:34 -0800226 if (USES_PART(vreg, uV) && vreg->type == RPM_REGULATOR_TYPE_CORNER) {
227 if (uV >= 0 && uV < (ARRAY_SIZE(label_corner) - 1))
228 corner_label = label_corner[uV+1];
229 pos += scnprintf(buf + pos, buflen - pos, ", corner=%s (%d)",
230 corner_label, uV);
231 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232
David Collins6f032ba2011-08-31 14:08:15 -0700233 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
234 vreg->req[0].id, vreg->req[0].value);
235 if (vreg->part->request_len > 1)
236 pos += scnprintf(buf + pos, buflen - pos,
237 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
238 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239
David Collins6f032ba2011-08-31 14:08:15 -0700240 pos += scnprintf(buf + pos, buflen - pos, "\n");
241 printk(buf);
242}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243
David Collins6f032ba2011-08-31 14:08:15 -0700244static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
245 int set, int voter_uV, int aggregate_uV)
246{
247 /* Suppress VDD_MEM and VDD_DIG printing. */
248 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
249 && vreg_id_is_vdd_mem_or_dig(vreg->id))
250 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251
David Collins6f032ba2011-08-31 14:08:15 -0700252 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
253 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
254 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
255}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256
David Collins6f032ba2011-08-31 14:08:15 -0700257static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
258{
259 /* Suppress VDD_MEM and VDD_DIG printing. */
260 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
261 && vreg_id_is_vdd_mem_or_dig(vreg->id))
262 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263
David Collins6f032ba2011-08-31 14:08:15 -0700264 if (cnt == 2)
265 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
266 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
267 (set == 0 ? 'A' : 'S'),
268 vreg->req[0].id, vreg->req[0].value,
269 vreg->req[1].id, vreg->req[1].value);
270 else if (cnt == 1)
271 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
272 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
273 vreg->req[0].id, vreg->req[0].value);
274}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
276/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700277static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278
279static int voltage_from_req(struct vreg *vreg)
280{
David Collins6f032ba2011-08-31 14:08:15 -0700281 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282
David Collins6f032ba2011-08-31 14:08:15 -0700283 if (vreg->part->uV.mask)
284 uV = GET_PART(vreg, uV);
David Collins13397f22012-02-06 13:53:29 -0800285 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700286 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
David Collins13397f22012-02-06 13:53:29 -0800287 else if (vreg->part->enable_state.mask)
288 uV = GET_PART(vreg, enable_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289
David Collins6f032ba2011-08-31 14:08:15 -0700290 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291}
292
David Collins6f032ba2011-08-31 14:08:15 -0700293static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294{
David Collins6f032ba2011-08-31 14:08:15 -0700295 if (vreg->part->uV.mask)
296 SET_PART(vreg, uV, uV);
David Collins13397f22012-02-06 13:53:29 -0800297 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700298 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
David Collins13397f22012-02-06 13:53:29 -0800299 else if (vreg->part->enable_state.mask)
300 SET_PART(vreg, enable_state, uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301}
302
303static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
304 int set, unsigned mask0, unsigned val0,
305 unsigned mask1, unsigned val1, unsigned cnt,
306 int update_voltage)
307{
308 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700309 int rc = 0, max_uV_vote = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700311 int *min_uV_vote;
312 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313
314 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700315 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 prev_req = vreg->prev_active_req;
317 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700318 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 prev_req = vreg->prev_sleep_req;
320 }
321
322 prev0 = vreg->req[0].value;
323 vreg->req[0].value &= ~mask0;
324 vreg->req[0].value |= val0 & mask0;
325
326 prev1 = vreg->req[1].value;
327 vreg->req[1].value &= ~mask1;
328 vreg->req[1].value |= val1 & mask1;
329
330 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700331 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332
333 /* Find the highest voltage voted for and use it. */
334 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700335 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
336 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337
338 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700339 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
340 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341
342 /* Ignore duplicate requests */
343 if (vreg->req[0].value != prev_req[0].value ||
344 vreg->req[1].value != prev_req[1].value) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
346 if (rc) {
347 vreg->req[0].value = prev0;
348 vreg->req[1].value = prev1;
349
David Collins6f032ba2011-08-31 14:08:15 -0700350 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
351 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
353 vreg->req[0].id, rc);
354 } else {
355 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700356 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
357 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 if (msm_rpm_vreg_debug_mask
359 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700360 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 prev_req[0].value = vreg->req[0].value;
362 prev_req[1].value = vreg->req[1].value;
363 }
364 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700365 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366 }
367
368 return rc;
369}
370
371static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
372 int sleep, unsigned mask0, unsigned val0,
373 unsigned mask1, unsigned val1, unsigned cnt,
374 int update_voltage)
375{
David Collins6f032ba2011-08-31 14:08:15 -0700376 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 unsigned long flags;
378 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379
380 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
381 return -EINVAL;
382
David Collins6f032ba2011-08-31 14:08:15 -0700383 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384
385 /*
386 * Send sleep set request first so that subsequent set_mode, etc calls
387 * use the voltage from the active set.
388 */
389 if (sleep)
390 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
391 mask0, val0, mask1, val1, cnt, update_voltage);
392 else {
393 /*
394 * Vote for 0 V in the sleep set when active set-only is
395 * specified. This ensures that a disable vote will be issued
396 * at some point for the sleep set of the regulator.
397 */
David Collins6f032ba2011-08-31 14:08:15 -0700398 if (vreg->part->uV.mask) {
399 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
400 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800401 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700402 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
403 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800404 } else if (vreg->part->enable_state.mask) {
405 s_val[vreg->part->enable_state.word]
406 = 0 << vreg->part->enable_state.shift;
407 s_mask[vreg->part->enable_state.word]
408 = vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409 }
410
411 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700412 s_mask[0], s_val[0], s_mask[1], s_val[1],
413 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414 }
415
416 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
417 mask1, val1, cnt, update_voltage);
418
David Collins6f032ba2011-08-31 14:08:15 -0700419 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420
421 return rc;
422}
423
424/**
425 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
426 * @vreg: ID for regulator
427 * @voter: ID for the voter
428 * @min_uV: minimum acceptable voltage (in uV) that is voted for
429 * @max_uV: maximum acceptable voltage (in uV) that is voted for
430 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
431 *
432 * Returns 0 on success or errno.
433 *
434 * This function is used to vote for the voltage of a regulator without
435 * using the regulator framework. It is needed by consumers which hold spin
436 * locks or have interrupts disabled because the regulator framework can sleep.
437 * It is also needed by consumers which wish to only vote for active set
438 * regulator voltage.
439 *
440 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
441 *
442 * This function may only be called for regulators which have the sleep flag
443 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700444 *
445 * Consumers can vote to disable a regulator with this function by passing
446 * min_uV = 0 and max_uV = 0.
David Collins13397f22012-02-06 13:53:29 -0800447 *
448 * Voltage switch type regulators may be controlled via rpm_vreg_set_voltage
449 * as well. For this type of regulator, max_uV > 0 is treated as an enable
450 * request and max_uV == 0 is treated as a disable request.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 */
David Collins6f032ba2011-08-31 14:08:15 -0700452int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
453 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454{
David Collins6f032ba2011-08-31 14:08:15 -0700455 unsigned int mask[2] = {0}, val[2] = {0};
456 struct vreg_range *range;
457 struct vreg *vreg;
458 int uV = min_uV;
459 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460
David Collins6f032ba2011-08-31 14:08:15 -0700461 if (!config) {
462 pr_err("rpm-regulator driver has not probed yet.\n");
463 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 }
465
David Collins6f032ba2011-08-31 14:08:15 -0700466 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
467 pr_err("invalid regulator id=%d\n", vreg_id);
468 return -EINVAL;
469 }
470
471 vreg = &config->vregs[vreg_id];
David Collins6f032ba2011-08-31 14:08:15 -0700472
473 if (!vreg->pdata.sleep_selectable) {
474 vreg_err(vreg, "regulator is not marked sleep selectable\n");
475 return -EINVAL;
476 }
477
David Collins7462b9d2011-10-11 16:02:17 -0700478 /* Allow min_uV == max_uV == 0 to represent a disable request. */
David Collins13397f22012-02-06 13:53:29 -0800479 if ((min_uV != 0 || max_uV != 0)
480 && (vreg->part->uV.mask || vreg->part->mV.mask)) {
David Collins7462b9d2011-10-11 16:02:17 -0700481 /*
482 * Check if request voltage is outside of allowed range. The
483 * regulator core has already checked that constraint range
484 * is inside of the physically allowed range.
485 */
486 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
487 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700488
David Collins7462b9d2011-10-11 16:02:17 -0700489 if (uV < lim_min_uV && max_uV >= lim_min_uV)
490 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700491
David Collins7462b9d2011-10-11 16:02:17 -0700492 if (uV < lim_min_uV || uV > lim_max_uV) {
493 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
494 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
495 lim_max_uV);
496 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700497 }
David Collins6f032ba2011-08-31 14:08:15 -0700498
David Collins13397f22012-02-06 13:53:29 -0800499 range = &vreg->set_points->range[0];
David Collins7462b9d2011-10-11 16:02:17 -0700500 /* Find the range which uV is inside of. */
501 for (i = vreg->set_points->count - 1; i > 0; i--) {
502 if (uV > vreg->set_points->range[i - 1].max_uV) {
503 range = &vreg->set_points->range[i];
504 break;
505 }
506 }
507
508 /*
509 * Force uV to be an allowed set point and apply a ceiling
510 * function to non-set point values.
511 */
512 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
513 uV = uV * range->step_uV + range->min_uV;
David Collins3974b612011-11-21 15:07:36 -0800514
515 if (uV > max_uV) {
516 vreg_err(vreg,
517 "request v=[%d, %d] cannot be met by any set point; "
518 "next set point: %d\n",
519 min_uV, max_uV, uV);
520 return -EINVAL;
521 }
David Collins7462b9d2011-10-11 16:02:17 -0700522 }
David Collins6f032ba2011-08-31 14:08:15 -0700523
David Collins0ac31fe2012-02-08 13:53:34 -0800524 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
525 /*
526 * Translate from enum values which work as inputs in the
527 * rpm_vreg_set_voltage function to the actual corner values
528 * sent to the RPM.
529 */
530 if (uV > 0)
531 uV -= RPM_VREG_CORNER_NONE;
532 }
533
David Collins6f032ba2011-08-31 14:08:15 -0700534 if (vreg->part->uV.mask) {
535 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
536 mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800537 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700538 val[vreg->part->mV.word]
539 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
540 mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800541 } else if (vreg->part->enable_state.mask) {
542 /*
543 * Translate max_uV > 0 into an enable request for regulator
544 * types which to not support voltage setting, e.g. voltage
545 * switches.
546 */
547 val[vreg->part->enable_state.word]
548 = (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift;
549 mask[vreg->part->enable_state.word]
550 = vreg->part->enable_state.mask;
David Collins6f032ba2011-08-31 14:08:15 -0700551 }
552
553 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
554 val[1], vreg->part->request_len, 1);
555 if (rc)
556 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557
558 return rc;
559}
560EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
561
562/**
563 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
564 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700565 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 *
567 * Returns 0 on success or errno.
568 */
David Collins6f032ba2011-08-31 14:08:15 -0700569int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570{
David Collins6f032ba2011-08-31 14:08:15 -0700571 unsigned int mask[2] = {0}, val[2] = {0};
572 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 int rc;
574
David Collins6f032ba2011-08-31 14:08:15 -0700575 if (!config) {
576 pr_err("rpm-regulator driver has not probed yet.\n");
577 return -ENODEV;
578 }
579
580 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
581 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 return -EINVAL;
583 }
584
David Collins6f032ba2011-08-31 14:08:15 -0700585 vreg = &config->vregs[vreg_id];
586
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700588 vreg_err(vreg, "invalid frequency=%d\n", freq);
589 return -EINVAL;
590 }
591 if (!vreg->pdata.sleep_selectable) {
592 vreg_err(vreg, "regulator is not marked sleep selectable\n");
593 return -EINVAL;
594 }
595 if (!vreg->part->freq.mask) {
596 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 return -EINVAL;
598 }
599
David Collins6f032ba2011-08-31 14:08:15 -0700600 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
601 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
David Collins6f032ba2011-08-31 14:08:15 -0700603 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
604 val[0], mask[1], val[1], vreg->part->request_len, 0);
605 if (rc)
606 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607
608 return rc;
609}
610EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
611
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612static inline int vreg_hpm_min_uA(struct vreg *vreg)
613{
614 return vreg->hpm_min_load;
615}
616
617static inline int vreg_lpm_max_uA(struct vreg *vreg)
618{
619 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
620}
621
David Collins6f032ba2011-08-31 14:08:15 -0700622static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623{
David Collins6f032ba2011-08-31 14:08:15 -0700624 unsigned load_max
625 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
626
627 return (load_uA > load_max ? load_max : load_uA);
628}
629
630static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
631{
632 unsigned load_max
633 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
634 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635}
636
637/* Change vreg->req, but do not send it to the RPM. */
638static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
639 unsigned mask1, unsigned val1)
640{
641 unsigned long flags = 0;
642
David Collins6f032ba2011-08-31 14:08:15 -0700643 if (vreg->pdata.sleep_selectable)
644 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645
646 vreg->req[0].value &= ~mask0;
647 vreg->req[0].value |= val0 & mask0;
648
649 vreg->req[1].value &= ~mask1;
650 vreg->req[1].value |= val1 & mask1;
651
David Collins6f032ba2011-08-31 14:08:15 -0700652 if (vreg->pdata.sleep_selectable)
653 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654
655 return 0;
656}
657
658static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
659 unsigned mask1, unsigned val1, unsigned cnt)
660{
661 unsigned prev0 = 0, prev1 = 0;
662 int rc;
663
664 /*
665 * Bypass the normal route for regulators that can be called to change
666 * just the active set values.
667 */
David Collins6f032ba2011-08-31 14:08:15 -0700668 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
670 mask0, val0, mask1, val1, cnt, 1);
671
672 prev0 = vreg->req[0].value;
673 vreg->req[0].value &= ~mask0;
674 vreg->req[0].value |= val0 & mask0;
675
676 prev1 = vreg->req[1].value;
677 vreg->req[1].value &= ~mask1;
678 vreg->req[1].value |= val1 & mask1;
679
680 /* Ignore duplicate requests */
681 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
682 vreg->req[1].value == vreg->prev_active_req[1].value) {
683 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -0700684 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 return 0;
686 }
687
688 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
689 if (rc) {
690 vreg->req[0].value = prev0;
691 vreg->req[1].value = prev1;
692
David Collins6f032ba2011-08-31 14:08:15 -0700693 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
694 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 } else {
696 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700697 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 vreg->prev_active_req[0].value = vreg->req[0].value;
699 vreg->prev_active_req[1].value = vreg->req[1].value;
700 }
701
702 return rc;
703}
704
David Collins6f032ba2011-08-31 14:08:15 -0700705static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706{
David Collins6f032ba2011-08-31 14:08:15 -0700707 struct vreg *vreg = rdev_get_drvdata(rdev);
708 int enabled;
709
710 mutex_lock(&vreg->pc_lock);
711 enabled = vreg->is_enabled;
712 mutex_unlock(&vreg->pc_lock);
713
714 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715}
716
David Collins6f032ba2011-08-31 14:08:15 -0700717static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718{
David Collins6f032ba2011-08-31 14:08:15 -0700719 switch (vreg->type) {
720 case RPM_REGULATOR_TYPE_LDO:
721 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -0800722 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -0700723 /* Enable by setting a voltage. */
724 if (vreg->part->uV.mask) {
725 val[vreg->part->uV.word]
726 |= vreg->save_uV << vreg->part->uV.shift;
727 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
728 } else {
729 val[vreg->part->mV.word]
730 |= MICRO_TO_MILLI(vreg->save_uV)
731 << vreg->part->mV.shift;
732 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
733 }
734 break;
735 case RPM_REGULATOR_TYPE_VS:
736 case RPM_REGULATOR_TYPE_NCP:
737 /* Enable by setting enable_state. */
738 val[vreg->part->enable_state.word]
739 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
740 mask[vreg->part->enable_state.word]
741 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743}
744
David Collins0ac31fe2012-02-08 13:53:34 -0800745static int rpm_vreg_enable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -0700746{
747 struct vreg *vreg = rdev_get_drvdata(rdev);
748 unsigned int mask[2] = {0}, val[2] = {0};
749 int rc = 0;
750
751 set_enable(vreg, mask, val);
752
753 mutex_lock(&vreg->pc_lock);
754
755 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
756 vreg->part->request_len);
757 if (!rc)
758 vreg->is_enabled = true;
759
760 mutex_unlock(&vreg->pc_lock);
761
762 if (rc)
763 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
764
765 return rc;
766}
767
768static void set_disable(struct vreg *vreg, unsigned int *mask,
769 unsigned int *val)
770{
771 switch (vreg->type) {
772 case RPM_REGULATOR_TYPE_LDO:
773 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -0800774 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -0700775 /* Disable by setting a voltage of 0 uV. */
776 if (vreg->part->uV.mask) {
777 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
778 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
779 } else {
780 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
781 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
782 }
783 break;
784 case RPM_REGULATOR_TYPE_VS:
785 case RPM_REGULATOR_TYPE_NCP:
786 /* Disable by setting enable_state. */
787 val[vreg->part->enable_state.word]
788 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
789 mask[vreg->part->enable_state.word]
790 |= vreg->part->enable_state.mask;
791 }
792}
793
David Collins0ac31fe2012-02-08 13:53:34 -0800794static int rpm_vreg_disable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -0700795{
796 struct vreg *vreg = rdev_get_drvdata(rdev);
797 unsigned int mask[2] = {0}, val[2] = {0};
798 int rc = 0;
799
800 set_disable(vreg, mask, val);
801
802 mutex_lock(&vreg->pc_lock);
803
804 /* Only disable if pin control is not in use. */
805 if (!vreg->is_enabled_pc)
806 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
807 vreg->part->request_len);
808
809 if (!rc)
810 vreg->is_enabled = false;
811
812 mutex_unlock(&vreg->pc_lock);
813
814 if (rc)
815 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
816
817 return rc;
818}
819
820static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 unsigned *selector)
822{
David Collins6f032ba2011-08-31 14:08:15 -0700823 struct vreg *vreg = rdev_get_drvdata(rdev);
824 struct vreg_range *range = &vreg->set_points->range[0];
825 unsigned int mask[2] = {0}, val[2] = {0};
826 int rc = 0, uV = min_uV;
827 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828
David Collins6f032ba2011-08-31 14:08:15 -0700829 /* Check if request voltage is outside of physically settable range. */
830 lim_min_uV = vreg->set_points->range[0].min_uV;
831 lim_max_uV =
832 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833
David Collins6f032ba2011-08-31 14:08:15 -0700834 if (uV < lim_min_uV && max_uV >= lim_min_uV)
835 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836
David Collins6f032ba2011-08-31 14:08:15 -0700837 if (uV < lim_min_uV || uV > lim_max_uV) {
838 vreg_err(vreg,
839 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
840 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 return -EINVAL;
842 }
843
David Collins6f032ba2011-08-31 14:08:15 -0700844 /* Find the range which uV is inside of. */
845 for (i = vreg->set_points->count - 1; i > 0; i--) {
846 if (uV > vreg->set_points->range[i - 1].max_uV) {
847 range = &vreg->set_points->range[i];
848 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 }
850 }
851
David Collins6f032ba2011-08-31 14:08:15 -0700852 /*
853 * Force uV to be an allowed set point and apply a ceiling function
854 * to non-set point values.
855 */
856 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
857 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858
David Collins3974b612011-11-21 15:07:36 -0800859 if (uV > max_uV) {
860 vreg_err(vreg,
861 "request v=[%d, %d] cannot be met by any set point; "
862 "next set point: %d\n",
863 min_uV, max_uV, uV);
864 return -EINVAL;
865 }
866
David Collins0ac31fe2012-02-08 13:53:34 -0800867 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
868 /*
869 * Translate from enum values which work as inputs in the
870 * regulator_set_voltage function to the actual corner values
871 * sent to the RPM.
872 */
873 uV -= RPM_VREG_CORNER_NONE;
874 }
875
David Collins6f032ba2011-08-31 14:08:15 -0700876 if (vreg->part->uV.mask) {
877 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
878 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700880 val[vreg->part->mV.word]
881 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
882 mask[vreg->part->mV.word] = vreg->part->mV.mask;
883 }
884
885 mutex_lock(&vreg->pc_lock);
886
887 /*
888 * Only send a request for a new voltage if the regulator is currently
889 * enabled. This will ensure that LDO and SMPS regulators are not
890 * inadvertently turned on because voltage > 0 is equivalent to
891 * enabling. For NCP, this just removes unnecessary RPM requests.
892 */
893 if (vreg->is_enabled) {
894 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
895 vreg->part->request_len);
896 if (rc)
897 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
898 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -0700900 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 }
902
David Collins6f032ba2011-08-31 14:08:15 -0700903 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
904 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905
David Collins6f032ba2011-08-31 14:08:15 -0700906 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907
908 return rc;
909}
910
David Collins6f032ba2011-08-31 14:08:15 -0700911static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912{
David Collins6f032ba2011-08-31 14:08:15 -0700913 struct vreg *vreg = rdev_get_drvdata(rdev);
914
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 return vreg->save_uV;
916}
917
David Collins6f032ba2011-08-31 14:08:15 -0700918static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919{
David Collins6f032ba2011-08-31 14:08:15 -0700920 struct vreg *vreg = rdev_get_drvdata(rdev);
921 int uV = 0;
922 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923
David Collins6f032ba2011-08-31 14:08:15 -0700924 if (!vreg->set_points) {
925 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 return -EINVAL;
927 }
928
David Collins6f032ba2011-08-31 14:08:15 -0700929 if (selector >= vreg->set_points->n_voltages)
930 return 0;
931
932 for (i = 0; i < vreg->set_points->count; i++) {
933 if (selector < vreg->set_points->range[i].n_voltages) {
934 uV = selector * vreg->set_points->range[i].step_uV
935 + vreg->set_points->range[i].min_uV;
936 break;
937 } else {
938 selector -= vreg->set_points->range[i].n_voltages;
939 }
940 }
941
942 return uV;
943}
944
945static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
946{
947 struct vreg *vreg = rdev_get_drvdata(rdev);
948 unsigned int mask[2] = {0}, val[2] = {0};
949 int rc = 0;
950 int peak_uA;
951
952 mutex_lock(&vreg->pc_lock);
953
954 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
955 & vreg->part->ip.mask) >> vreg->part->ip.shift);
956
957 if (mode == config->mode_hpm) {
958 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700960 val[vreg->part->ip.word]
961 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
962 << vreg->part->ip.shift;
963 mask[vreg->part->ip.word] = vreg->part->ip.mask;
964
965 if (config->ia_follows_ip) {
966 val[vreg->part->ia.word]
967 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
968 << vreg->part->ia.shift;
969 mask[vreg->part->ia.word]
970 |= vreg->part->ia.mask;
971 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 }
David Collins6f032ba2011-08-31 14:08:15 -0700973 } else if (mode == config->mode_lpm) {
974 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -0700976 val[vreg->part->ip.word]
977 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
978 << vreg->part->ip.shift;
979 mask[vreg->part->ip.word] = vreg->part->ip.mask;
980
981 if (config->ia_follows_ip) {
982 val[vreg->part->ia.word]
983 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
984 << vreg->part->ia.shift;
985 mask[vreg->part->ia.word]
986 |= vreg->part->ia.mask;
987 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 }
David Collins6f032ba2011-08-31 14:08:15 -0700989 } else {
990 vreg_err(vreg, "invalid mode: %u\n", mode);
991 mutex_unlock(&vreg->pc_lock);
992 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 }
994
David Collins6f032ba2011-08-31 14:08:15 -0700995 if (vreg->is_enabled) {
996 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
997 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 } else {
999 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001000 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001 }
David Collins6f032ba2011-08-31 14:08:15 -07001002
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001004 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1005 else
1006 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007
David Collins6f032ba2011-08-31 14:08:15 -07001008 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009
David Collins6f032ba2011-08-31 14:08:15 -07001010 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011}
1012
David Collins6f032ba2011-08-31 14:08:15 -07001013static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014{
David Collins6f032ba2011-08-31 14:08:15 -07001015 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016
David Collins6f032ba2011-08-31 14:08:15 -07001017 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018}
1019
David Collins6f032ba2011-08-31 14:08:15 -07001020static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
1021 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022{
David Collins6f032ba2011-08-31 14:08:15 -07001023 struct vreg *vreg = rdev_get_drvdata(rdev);
1024 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
David Collins6f032ba2011-08-31 14:08:15 -07001026 load_uA += vreg->pdata.system_uA;
1027
1028 mutex_lock(&vreg->pc_lock);
1029 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
1030 if (config->ia_follows_ip)
1031 SET_PART(vreg, ia,
1032 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
1033 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034
1035 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -07001036 mode = config->mode_hpm;
1037 else
1038 mode = config->mode_lpm;
1039
1040 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041}
1042
David Collins6f032ba2011-08-31 14:08:15 -07001043static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
1044 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045{
David Collins6f032ba2011-08-31 14:08:15 -07001046 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047
David Collins6f032ba2011-08-31 14:08:15 -07001048 if (MICRO_TO_MILLI(load_uA) <= 0) {
1049 /*
1050 * vreg_legacy_get_optimum_mode is being called before consumers
1051 * have specified their load currents via
1052 * regulator_set_optimum_mode. Return whatever the existing mode
1053 * is.
1054 */
1055 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 }
1057
David Collins6f032ba2011-08-31 14:08:15 -07001058 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059}
1060
1061/*
David Collins6f032ba2011-08-31 14:08:15 -07001062 * Returns the logical pin control enable state because the pin control options
1063 * present in the hardware out of restart could be different from those desired
1064 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065 */
David Collins6f032ba2011-08-31 14:08:15 -07001066static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067{
David Collins6f032ba2011-08-31 14:08:15 -07001068 struct vreg *vreg = rdev_get_drvdata(rdev);
1069
1070 return vreg->is_enabled_pc;
1071}
1072
1073static int vreg_pin_control_enable(struct regulator_dev *rdev)
1074{
1075 struct vreg *vreg = rdev_get_drvdata(rdev);
1076 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 int rc;
1078
David Collins6f032ba2011-08-31 14:08:15 -07001079 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080
David Collins6f032ba2011-08-31 14:08:15 -07001081 val[vreg->part->pc.word]
1082 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1083 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084
David Collins6f032ba2011-08-31 14:08:15 -07001085 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1086 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087
David Collins6f032ba2011-08-31 14:08:15 -07001088 if (!vreg->is_enabled)
1089 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090
David Collins6f032ba2011-08-31 14:08:15 -07001091 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1092 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093
David Collins6f032ba2011-08-31 14:08:15 -07001094 if (!rc)
1095 vreg->is_enabled_pc = true;
1096
1097 mutex_unlock(&vreg->pc_lock);
1098
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001100 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101
David Collins6f032ba2011-08-31 14:08:15 -07001102 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103}
1104
David Collins6f032ba2011-08-31 14:08:15 -07001105static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106{
David Collins6f032ba2011-08-31 14:08:15 -07001107 struct vreg *vreg = rdev_get_drvdata(rdev);
1108 unsigned int mask[2] = {0}, val[2] = {0};
1109 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110
David Collins6f032ba2011-08-31 14:08:15 -07001111 mutex_lock(&vreg->pc_lock);
1112
1113 val[vreg->part->pc.word]
1114 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1115 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1116
1117 pin_fn = config->pin_func_none;
1118 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1119 pin_fn = config->pin_func_sleep_b;
1120 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1121 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1122
1123 if (!vreg->is_enabled)
1124 set_disable(vreg, mask, val);
1125
1126 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1127 vreg->part->request_len);
1128
1129 if (!rc)
1130 vreg->is_enabled_pc = false;
1131
1132 mutex_unlock(&vreg->pc_lock);
1133
1134 if (rc)
1135 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1136
1137 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138}
1139
David Collins6f032ba2011-08-31 14:08:15 -07001140static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141{
David Collins6f032ba2011-08-31 14:08:15 -07001142 struct vreg *vreg = rdev_get_drvdata(rdev);
1143
1144 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145}
1146
David Collins6f032ba2011-08-31 14:08:15 -07001147/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148static struct regulator_ops ldo_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001149 .enable = rpm_vreg_enable,
1150 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001151 .is_enabled = vreg_is_enabled,
1152 .set_voltage = vreg_set_voltage,
1153 .get_voltage = vreg_get_voltage,
1154 .list_voltage = vreg_list_voltage,
1155 .set_mode = vreg_set_mode,
1156 .get_mode = vreg_get_mode,
1157 .get_optimum_mode = vreg_get_optimum_mode,
1158 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159};
1160
1161static struct regulator_ops smps_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001162 .enable = rpm_vreg_enable,
1163 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001164 .is_enabled = vreg_is_enabled,
1165 .set_voltage = vreg_set_voltage,
1166 .get_voltage = vreg_get_voltage,
1167 .list_voltage = vreg_list_voltage,
1168 .set_mode = vreg_set_mode,
1169 .get_mode = vreg_get_mode,
1170 .get_optimum_mode = vreg_get_optimum_mode,
1171 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172};
1173
1174static struct regulator_ops switch_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001175 .enable = rpm_vreg_enable,
1176 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001177 .is_enabled = vreg_is_enabled,
1178 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179};
1180
1181static struct regulator_ops ncp_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001182 .enable = rpm_vreg_enable,
1183 .disable = rpm_vreg_disable,
1184 .is_enabled = vreg_is_enabled,
1185 .set_voltage = vreg_set_voltage,
1186 .get_voltage = vreg_get_voltage,
1187 .list_voltage = vreg_list_voltage,
1188 .enable_time = vreg_enable_time,
1189};
1190
1191static struct regulator_ops corner_ops = {
1192 .enable = rpm_vreg_enable,
1193 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001194 .is_enabled = vreg_is_enabled,
1195 .set_voltage = vreg_set_voltage,
1196 .get_voltage = vreg_get_voltage,
1197 .list_voltage = vreg_list_voltage,
1198 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199};
1200
David Collins6f032ba2011-08-31 14:08:15 -07001201/* Pin control regulator operations. */
1202static struct regulator_ops pin_control_ops = {
1203 .enable = vreg_pin_control_enable,
1204 .disable = vreg_pin_control_disable,
1205 .is_enabled = vreg_pin_control_is_enabled,
1206};
1207
1208struct regulator_ops *vreg_ops[] = {
1209 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1210 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1211 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1212 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
David Collins0ac31fe2012-02-08 13:53:34 -08001213 [RPM_REGULATOR_TYPE_CORNER] = &corner_ops,
David Collins6f032ba2011-08-31 14:08:15 -07001214};
1215
1216static int __devinit
1217rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1218 struct device *dev)
1219{
1220 struct regulator_desc *rdesc = NULL;
1221 struct regulator_dev *rdev;
1222 struct vreg *vreg;
1223 unsigned pin_ctrl;
1224 int id, pin_fn;
1225 int rc = 0;
1226
1227 if (!pdata) {
1228 pr_err("platform data missing\n");
1229 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 }
1231
David Collins6f032ba2011-08-31 14:08:15 -07001232 id = pdata->id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233
David Collins6f032ba2011-08-31 14:08:15 -07001234 if (id < config->vreg_id_min || id > config->vreg_id_max) {
1235 pr_err("invalid regulator id: %d\n", id);
1236 return -ENODEV;
1237 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238
David Collins6f032ba2011-08-31 14:08:15 -07001239 if (!config->is_real_id(pdata->id))
1240 id = config->pc_id_to_real_id(pdata->id);
1241 vreg = &config->vregs[id];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242
David Collins6f032ba2011-08-31 14:08:15 -07001243 if (config->is_real_id(pdata->id))
1244 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245 else
David Collins6f032ba2011-08-31 14:08:15 -07001246 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247
David Collins6f032ba2011-08-31 14:08:15 -07001248 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1249 pr_err("%s: invalid regulator type: %d\n",
1250 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001252 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253
David Collins6f032ba2011-08-31 14:08:15 -07001254 mutex_lock(&vreg->pc_lock);
1255
1256 if (vreg->set_points)
1257 rdesc->n_voltages = vreg->set_points->n_voltages;
1258 else
1259 rdesc->n_voltages = 0;
1260
1261 rdesc->id = pdata->id;
1262 rdesc->owner = THIS_MODULE;
1263 rdesc->type = REGULATOR_VOLTAGE;
1264
1265 if (config->is_real_id(pdata->id)) {
1266 /*
1267 * Real regulator; do not modify pin control and pin function
1268 * values.
1269 */
1270 rdesc->ops = vreg_ops[vreg->type];
1271 pin_ctrl = vreg->pdata.pin_ctrl;
1272 pin_fn = vreg->pdata.pin_fn;
1273 memcpy(&(vreg->pdata), pdata,
1274 sizeof(struct rpm_regulator_init_data));
1275 vreg->pdata.pin_ctrl = pin_ctrl;
1276 vreg->pdata.pin_fn = pin_fn;
1277
1278 vreg->save_uV = vreg->pdata.default_uV;
1279 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1280 vreg->mode = config->mode_hpm;
1281 else
1282 vreg->mode = config->mode_lpm;
1283
1284 /* Initialize the RPM request. */
1285 SET_PART(vreg, ip,
1286 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1287 SET_PART(vreg, fm, vreg->pdata.force_mode);
1288 SET_PART(vreg, pm, vreg->pdata.power_mode);
1289 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1290 SET_PART(vreg, ia,
1291 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1292 SET_PART(vreg, freq, vreg->pdata.freq);
1293 SET_PART(vreg, freq_clk_src, 0);
1294 SET_PART(vreg, comp_mode, 0);
1295 SET_PART(vreg, hpm, 0);
1296 if (!vreg->is_enabled_pc) {
1297 SET_PART(vreg, pf, config->pin_func_none);
1298 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1299 }
1300 } else {
1301 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1302 == RPM_VREG_PIN_CTRL_NONE
1303 && pdata->pin_fn != config->pin_func_sleep_b) {
1304 pr_err("%s: no pin control input specified\n",
1305 vreg->rdesc.name);
1306 mutex_unlock(&vreg->pc_lock);
1307 return -EINVAL;
1308 }
1309 rdesc->ops = &pin_control_ops;
1310 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1311 vreg->pdata.pin_fn = pdata->pin_fn;
1312
1313 /* Initialize the RPM request. */
1314 pin_fn = config->pin_func_none;
1315 /* Allow pf=sleep_b to be specified by platform data. */
1316 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1317 pin_fn = config->pin_func_sleep_b;
1318 SET_PART(vreg, pf, pin_fn);
1319 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1320 }
1321
1322 mutex_unlock(&vreg->pc_lock);
1323
1324 if (rc)
1325 goto bail;
1326
1327 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg);
1328 if (IS_ERR(rdev)) {
1329 rc = PTR_ERR(rdev);
1330 pr_err("regulator_register failed: %s, rc=%d\n",
1331 vreg->rdesc.name, rc);
1332 return rc;
1333 } else {
1334 if (config->is_real_id(pdata->id))
1335 vreg->rdev = rdev;
1336 else
1337 vreg->rdev_pc = rdev;
1338 }
1339
1340bail:
1341 if (rc)
1342 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1343
1344 return rc;
1345}
1346
1347static void rpm_vreg_set_point_init(void)
1348{
1349 struct vreg_set_points **set_points;
1350 int i, j, temp;
1351
1352 set_points = config->set_points;
1353
1354 /* Calculate the number of set points available for each regulator. */
1355 for (i = 0; i < config->set_points_len; i++) {
1356 temp = 0;
1357 for (j = 0; j < set_points[i]->count; j++) {
1358 set_points[i]->range[j].n_voltages
1359 = (set_points[i]->range[j].max_uV
1360 - set_points[i]->range[j].min_uV)
1361 / set_points[i]->range[j].step_uV + 1;
1362 temp += set_points[i]->range[j].n_voltages;
1363 }
1364 set_points[i]->n_voltages = temp;
1365 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001366}
1367
1368static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1369{
David Collins6f032ba2011-08-31 14:08:15 -07001370 struct rpm_regulator_platform_data *platform_data;
1371 int rc = 0;
1372 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001373
David Collins6f032ba2011-08-31 14:08:15 -07001374 platform_data = pdev->dev.platform_data;
1375 if (!platform_data) {
1376 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 }
1379
David Collins6f032ba2011-08-31 14:08:15 -07001380 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1381 && platform_data->version != rpm_version) {
1382 pr_err("rpm version %d does not match previous version %d\n",
1383 platform_data->version, rpm_version);
1384 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385 }
1386
David Collins6f032ba2011-08-31 14:08:15 -07001387 if (platform_data->version < 0
1388 || platform_data->version > RPM_VREG_VERSION_MAX) {
1389 pr_err("rpm version %d is invalid\n", platform_data->version);
1390 return -EINVAL;
1391 }
1392
1393 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1394 rpm_version = platform_data->version;
1395 config = get_config[platform_data->version]();
1396 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1397 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1398 if (!config) {
1399 pr_err("rpm version %d is not available\n",
1400 platform_data->version);
1401 return -ENODEV;
1402 }
1403 if (config->use_legacy_optimum_mode)
1404 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1405 vreg_ops[i]->get_optimum_mode
1406 = vreg_legacy_get_optimum_mode;
1407 rpm_vreg_set_point_init();
1408 /* First time probed; initialize pin control mutexes. */
1409 for (i = 0; i < config->vregs_len; i++)
1410 mutex_init(&config->vregs[i].pc_lock);
1411 }
1412
1413 /* Initialize all of the regulators listed in the platform data. */
1414 for (i = 0; i < platform_data->num_regulators; i++) {
1415 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1416 &pdev->dev);
1417 if (rc) {
1418 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1419 goto remove_regulators;
1420 }
1421 }
1422
1423 platform_set_drvdata(pdev, platform_data);
1424
1425 return rc;
1426
1427remove_regulators:
1428 /* Unregister all regulators added before the erroring one. */
1429 for (; i >= 0; i--) {
1430 id = platform_data->init_data[i].id;
1431 if (config->is_real_id(id)) {
1432 regulator_unregister(config->vregs[id].rdev);
1433 config->vregs[id].rdev = NULL;
1434 } else {
1435 regulator_unregister(config->vregs[
1436 config->pc_id_to_real_id(id)].rdev_pc);
1437 config->vregs[id].rdev_pc = NULL;
1438 }
1439 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001440
1441 return rc;
1442}
1443
1444static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1445{
David Collins6f032ba2011-08-31 14:08:15 -07001446 struct rpm_regulator_platform_data *platform_data;
1447 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448
David Collins6f032ba2011-08-31 14:08:15 -07001449 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001450 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001451
1452 if (platform_data) {
1453 for (i = 0; i < platform_data->num_regulators; i++) {
1454 id = platform_data->init_data[i].id;
1455 if (config->is_real_id(id)) {
1456 regulator_unregister(config->vregs[id].rdev);
1457 config->vregs[id].rdev = NULL;
1458 } else {
1459 regulator_unregister(config->vregs[
1460 config->pc_id_to_real_id(id)].rdev_pc);
1461 config->vregs[id].rdev_pc = NULL;
1462 }
1463 }
1464 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001465
1466 return 0;
1467}
1468
1469static struct platform_driver rpm_vreg_driver = {
1470 .probe = rpm_vreg_probe,
1471 .remove = __devexit_p(rpm_vreg_remove),
1472 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001473 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474 .owner = THIS_MODULE,
1475 },
1476};
1477
1478static int __init rpm_vreg_init(void)
1479{
1480 return platform_driver_register(&rpm_vreg_driver);
1481}
1482
1483static void __exit rpm_vreg_exit(void)
1484{
David Collins6f032ba2011-08-31 14:08:15 -07001485 int i;
1486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001488
1489 for (i = 0; i < config->vregs_len; i++)
1490 mutex_destroy(&config->vregs[i].pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491}
1492
1493postcore_initcall(rpm_vreg_init);
1494module_exit(rpm_vreg_exit);
1495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001496MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001497MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001499MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);