blob: 12b139295a2decb377ec0e9decb8fc7e52f76b94 [file] [log] [blame]
David Collins6f032ba2011-08-31 14:08:15 -07001/*
Duy Truong790f06d2013-02-13 16:38:12 -08002 * Copyright (c) 2010-2012, The Linux Foundation. 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>
David Collins109a8e62012-07-31 15:21:35 -070017#include <linux/clk.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018#include <linux/err.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
David Collins6f032ba2011-08-31 14:08:15 -070021#include <linux/slab.h>
David Collins379b4b72012-05-14 16:33:51 -070022#include <linux/string.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/platform_device.h>
David Collins109a8e62012-07-31 15:21:35 -070024#include <linux/wakelock.h>
25#include <linux/workqueue.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/regulator/driver.h>
David Collins109a8e62012-07-31 15:21:35 -070027
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <mach/rpm.h>
29#include <mach/rpm-regulator.h>
David Collins379b4b72012-05-14 16:33:51 -070030#include <mach/rpm-regulator-smd.h>
David Collins6f032ba2011-08-31 14:08:15 -070031#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032
33#include "rpm_resources.h"
David Collins6f032ba2011-08-31 14:08:15 -070034#include "rpm-regulator-private.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
36/* Debug Definitions */
37
38enum {
39 MSM_RPM_VREG_DEBUG_REQUEST = BIT(0),
40 MSM_RPM_VREG_DEBUG_VOTE = BIT(1),
41 MSM_RPM_VREG_DEBUG_DUPLICATE = BIT(2),
David Collins6f032ba2011-08-31 14:08:15 -070042 MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG = BIT(3),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043};
44
45static int msm_rpm_vreg_debug_mask;
46module_param_named(
47 debug_mask, msm_rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR
48);
49
David Collins379b4b72012-05-14 16:33:51 -070050/* Used for access via the rpm_regulator_* API. */
51struct rpm_regulator {
52 int vreg_id;
53 enum rpm_vreg_voter voter;
54 int sleep_also;
55 int min_uV;
56 int max_uV;
57};
58
David Collins6f032ba2011-08-31 14:08:15 -070059struct vreg_config *(*get_config[])(void) = {
60 [RPM_VREG_VERSION_8660] = get_config_8660,
61 [RPM_VREG_VERSION_8960] = get_config_8960,
David Collins6ef12bf2011-08-31 14:08:15 -070062 [RPM_VREG_VERSION_9615] = get_config_9615,
David Collins5779cea2012-01-05 15:09:21 -080063 [RPM_VREG_VERSION_8930] = get_config_8930,
David Collinsd2a4fef2012-08-13 12:31:04 -070064 [RPM_VREG_VERSION_8930_PM8917] = get_config_8930_pm8917,
David Collins2ac36d52012-10-02 14:56:21 -070065 [RPM_VREG_VERSION_8960_PM8917] = get_config_8960_pm8917,
David Collins6f032ba2011-08-31 14:08:15 -070066};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067
David Collins379b4b72012-05-14 16:33:51 -070068static struct rpm_regulator_consumer_mapping *consumer_map;
69static int consumer_map_len;
70
David Collins6f032ba2011-08-31 14:08:15 -070071#define SET_PART(_vreg, _part, _val) \
72 _vreg->req[_vreg->part->_part.word].value \
73 = (_vreg->req[_vreg->part->_part.word].value \
David Collinsd8525e82011-11-21 14:54:25 -080074 & ~_vreg->part->_part.mask) \
75 | (((_val) << _vreg->part->_part.shift) \
76 & _vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077
David Collins6f032ba2011-08-31 14:08:15 -070078#define GET_PART(_vreg, _part) \
David Collinsd8525e82011-11-21 14:54:25 -080079 ((_vreg->req[_vreg->part->_part.word].value & _vreg->part->_part.mask) \
80 >> _vreg->part->_part.shift)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081
David Collins109a8e62012-07-31 15:21:35 -070082#define GET_PART_PREV_ACT(_vreg, _part) \
83 ((_vreg->prev_active_req[_vreg->part->_part.word].value \
84 & _vreg->part->_part.mask) \
85 >> _vreg->part->_part.shift)
86
David Collinsd8525e82011-11-21 14:54:25 -080087#define USES_PART(_vreg, _part) (_vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088
David Collins6f032ba2011-08-31 14:08:15 -070089#define vreg_err(vreg, fmt, ...) \
90 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091
David Collins6f032ba2011-08-31 14:08:15 -070092#define RPM_VREG_PIN_CTRL_EN0 0x01
93#define RPM_VREG_PIN_CTRL_EN1 0x02
94#define RPM_VREG_PIN_CTRL_EN2 0x04
95#define RPM_VREG_PIN_CTRL_EN3 0x08
96#define RPM_VREG_PIN_CTRL_ALL 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097
David Collins6f032ba2011-08-31 14:08:15 -070098static const char *label_freq[] = {
99 [RPM_VREG_FREQ_NONE] = " N/A",
100 [RPM_VREG_FREQ_19p20] = "19.2",
101 [RPM_VREG_FREQ_9p60] = "9.60",
102 [RPM_VREG_FREQ_6p40] = "6.40",
103 [RPM_VREG_FREQ_4p80] = "4.80",
104 [RPM_VREG_FREQ_3p84] = "3.84",
105 [RPM_VREG_FREQ_3p20] = "3.20",
106 [RPM_VREG_FREQ_2p74] = "2.74",
107 [RPM_VREG_FREQ_2p40] = "2.40",
108 [RPM_VREG_FREQ_2p13] = "2.13",
109 [RPM_VREG_FREQ_1p92] = "1.92",
110 [RPM_VREG_FREQ_1p75] = "1.75",
111 [RPM_VREG_FREQ_1p60] = "1.60",
112 [RPM_VREG_FREQ_1p48] = "1.48",
113 [RPM_VREG_FREQ_1p37] = "1.37",
114 [RPM_VREG_FREQ_1p28] = "1.28",
115 [RPM_VREG_FREQ_1p20] = "1.20",
116};
David Collins0ac31fe2012-02-08 13:53:34 -0800117
118static const char *label_corner[] = {
119 [RPM_VREG_CORNER_NONE] = "NONE",
120 [RPM_VREG_CORNER_LOW] = "LOW",
121 [RPM_VREG_CORNER_NOMINAL] = "NOM",
122 [RPM_VREG_CORNER_HIGH] = "HIGH",
123};
124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125/*
126 * This is used when voting for LPM or HPM by subtracting or adding to the
127 * hpm_min_load of a regulator. It has units of uA.
128 */
David Collins6f032ba2011-08-31 14:08:15 -0700129#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
David Collins6f032ba2011-08-31 14:08:15 -0700131/* rpm_version keeps track of the version for the currently running driver. */
132enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133
David Collins6f032ba2011-08-31 14:08:15 -0700134/* config holds all configuration data of the currently running driver. */
135static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136
David Collins6f032ba2011-08-31 14:08:15 -0700137/* These regulator ID values are specified in the board file. */
138static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139
David Collins6f032ba2011-08-31 14:08:15 -0700140static inline int vreg_id_is_vdd_mem_or_dig(int id)
141{
142 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
143}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144
David Collins6f032ba2011-08-31 14:08:15 -0700145#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146
David Collins6f032ba2011-08-31 14:08:15 -0700147static void rpm_regulator_req(struct vreg *vreg, int set)
148{
149 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
150 const char *pf_label = "", *fm_label = "", *pc_total = "";
151 const char *pc_en[4] = {"", "", "", ""};
David Collins0ac31fe2012-02-08 13:53:34 -0800152 const char *pm_label = "", *freq_label = "", *corner_label = "";
David Collins6f032ba2011-08-31 14:08:15 -0700153 char buf[DEBUG_PRINT_BUFFER_SIZE];
154 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
155 int pos = 0;
156
157 /* Suppress VDD_MEM and VDD_DIG printing. */
158 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
159 && vreg_id_is_vdd_mem_or_dig(vreg->id))
160 return;
161
162 uV = GET_PART(vreg, uV);
163 mV = GET_PART(vreg, mV);
164 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
165 uV = -uV;
166 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 }
168
David Collins6f032ba2011-08-31 14:08:15 -0700169 fm = GET_PART(vreg, fm);
170 pm = GET_PART(vreg, pm);
171 pc = GET_PART(vreg, pc);
172 pf = GET_PART(vreg, pf);
173 pd = GET_PART(vreg, pd);
174 freq = GET_PART(vreg, freq);
175 state = GET_PART(vreg, enable_state);
176
177 if (pf >= 0 && pf < config->label_pin_func_len)
178 pf_label = config->label_pin_func[pf];
179
180 if (fm >= 0 && fm < config->label_force_mode_len)
181 fm_label = config->label_force_mode[fm];
182
183 if (pm >= 0 && pm < config->label_power_mode_len)
184 pm_label = config->label_power_mode[pm];
185
186 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
187 freq_label = label_freq[freq];
188
189 for (i = 0; i < config->label_pin_ctrl_len; i++)
190 if (pc & (1 << i))
191 pc_en[i] = config->label_pin_ctrl[i];
192
193 if (pc == RPM_VREG_PIN_CTRL_NONE)
194 pc_total = " none";
195
196 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
197 KERN_INFO, __func__);
198
199 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
200 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
201 vreg->rdesc.name,
202 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
203
David Collins0ac31fe2012-02-08 13:53:34 -0800204 if (USES_PART(vreg, uV) && vreg->type != RPM_REGULATOR_TYPE_CORNER)
David Collins6f032ba2011-08-31 14:08:15 -0700205 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
206 if (USES_PART(vreg, mV))
207 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
208 if (USES_PART(vreg, enable_state))
209 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
210 (state == 1 ? "on" : "off"), state);
211 if (USES_PART(vreg, ip))
212 pos += scnprintf(buf + pos, buflen - pos,
213 ", ip=%4d mA", GET_PART(vreg, ip));
214 if (USES_PART(vreg, fm))
215 pos += scnprintf(buf + pos, buflen - pos,
216 ", fm=%s (%d)", fm_label, fm);
217 if (USES_PART(vreg, pc))
218 pos += scnprintf(buf + pos, buflen - pos,
219 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
220 pc_en[2], pc_en[3], pc_total, pc);
221 if (USES_PART(vreg, pf))
222 pos += scnprintf(buf + pos, buflen - pos,
223 ", pf=%s (%d)", pf_label, pf);
224 if (USES_PART(vreg, pd))
225 pos += scnprintf(buf + pos, buflen - pos,
226 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
227 if (USES_PART(vreg, ia))
228 pos += scnprintf(buf + pos, buflen - pos,
229 ", ia=%4d mA", GET_PART(vreg, ia));
230 if (USES_PART(vreg, freq)) {
231 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
232 pos += scnprintf(buf + pos, buflen - pos,
233 ", freq=%2d", freq);
234 else
235 pos += scnprintf(buf + pos, buflen - pos,
236 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 }
David Collins6f032ba2011-08-31 14:08:15 -0700238 if (USES_PART(vreg, pm))
239 pos += scnprintf(buf + pos, buflen - pos,
240 ", pm=%s (%d)", pm_label, pm);
241 if (USES_PART(vreg, freq_clk_src))
242 pos += scnprintf(buf + pos, buflen - pos,
243 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
244 if (USES_PART(vreg, comp_mode))
245 pos += scnprintf(buf + pos, buflen - pos,
246 ", comp=%d", GET_PART(vreg, comp_mode));
247 if (USES_PART(vreg, hpm))
248 pos += scnprintf(buf + pos, buflen - pos,
249 ", hpm=%d", GET_PART(vreg, hpm));
David Collins0ac31fe2012-02-08 13:53:34 -0800250 if (USES_PART(vreg, uV) && vreg->type == RPM_REGULATOR_TYPE_CORNER) {
251 if (uV >= 0 && uV < (ARRAY_SIZE(label_corner) - 1))
252 corner_label = label_corner[uV+1];
253 pos += scnprintf(buf + pos, buflen - pos, ", corner=%s (%d)",
254 corner_label, uV);
255 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256
David Collins6f032ba2011-08-31 14:08:15 -0700257 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
258 vreg->req[0].id, vreg->req[0].value);
259 if (vreg->part->request_len > 1)
260 pos += scnprintf(buf + pos, buflen - pos,
261 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
262 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263
David Collins6f032ba2011-08-31 14:08:15 -0700264 pos += scnprintf(buf + pos, buflen - pos, "\n");
265 printk(buf);
266}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267
David Collins6f032ba2011-08-31 14:08:15 -0700268static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
269 int set, int voter_uV, int aggregate_uV)
270{
271 /* Suppress VDD_MEM and VDD_DIG printing. */
272 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
273 && vreg_id_is_vdd_mem_or_dig(vreg->id))
274 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
David Collins6f032ba2011-08-31 14:08:15 -0700276 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
277 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
278 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
279}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280
David Collins6f032ba2011-08-31 14:08:15 -0700281static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
282{
283 /* Suppress VDD_MEM and VDD_DIG printing. */
284 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
285 && vreg_id_is_vdd_mem_or_dig(vreg->id))
286 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287
David Collins6f032ba2011-08-31 14:08:15 -0700288 if (cnt == 2)
289 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
290 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
291 (set == 0 ? 'A' : 'S'),
292 vreg->req[0].id, vreg->req[0].value,
293 vreg->req[1].id, vreg->req[1].value);
294 else if (cnt == 1)
295 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
296 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
297 vreg->req[0].id, vreg->req[0].value);
298}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299
David Collins109a8e62012-07-31 15:21:35 -0700300static bool requires_tcxo_workaround;
David Collins109a8e62012-07-31 15:21:35 -0700301static struct clk *tcxo_handle;
302static struct wake_lock tcxo_wake_lock;
303static DEFINE_MUTEX(tcxo_mutex);
David Collins109a8e62012-07-31 15:21:35 -0700304static bool tcxo_is_enabled;
305/*
306 * TCXO must be kept on for at least the duration of its warmup (4 ms);
307 * otherwise, it will stay on when hardware disabling is attempted.
308 */
309#define TCXO_WARMUP_TIME_MS 4
310
311static void tcxo_get_handle(void)
312{
David Collins109a8e62012-07-31 15:21:35 -0700313 if (!tcxo_handle) {
314 tcxo_handle = clk_get_sys("rpm-regulator", "vref_buff");
David Collins65b5fa62012-10-17 17:02:11 -0700315 if (IS_ERR(tcxo_handle))
David Collins109a8e62012-07-31 15:21:35 -0700316 tcxo_handle = NULL;
David Collins109a8e62012-07-31 15:21:35 -0700317 }
318}
319
320/*
321 * Perform best effort enable of CXO. Since the MSM clock drivers depend upon
322 * the rpm-regulator driver, any rpm-regulator devices that are configured with
323 * always_on == 1 will not be able to enable CXO during probe. This does not
324 * cause a problem though since CXO will be enabled by the boot loaders before
325 * Apps boots up.
326 */
327static bool tcxo_enable(void)
328{
329 int rc;
330
331 if (tcxo_handle && !tcxo_is_enabled) {
David Collins65b5fa62012-10-17 17:02:11 -0700332 rc = clk_prepare_enable(tcxo_handle);
David Collins109a8e62012-07-31 15:21:35 -0700333 if (!rc) {
334 tcxo_is_enabled = true;
335 wake_lock(&tcxo_wake_lock);
336 return true;
337 }
338 }
339
340 return false;
341}
342
343static void tcxo_delayed_disable_work(struct work_struct *work)
344{
David Collins65b5fa62012-10-17 17:02:11 -0700345 mutex_lock(&tcxo_mutex);
David Collins109a8e62012-07-31 15:21:35 -0700346
David Collins65b5fa62012-10-17 17:02:11 -0700347 clk_disable_unprepare(tcxo_handle);
David Collins109a8e62012-07-31 15:21:35 -0700348 tcxo_is_enabled = false;
349 wake_unlock(&tcxo_wake_lock);
350
David Collins65b5fa62012-10-17 17:02:11 -0700351 mutex_unlock(&tcxo_mutex);
David Collins109a8e62012-07-31 15:21:35 -0700352}
353
354static DECLARE_DELAYED_WORK(tcxo_disable_work, tcxo_delayed_disable_work);
355
356static void tcxo_delayed_disable(void)
357{
358 /*
359 * The delay in jiffies has 1 added to it to ensure that at least
360 * one jiffy takes place before the work is enqueued. Without this,
361 * the work would be scheduled to run in the very next jiffy which could
362 * result in too little delay and TCXO being stuck on.
363 */
364 if (tcxo_handle)
365 schedule_delayed_work(&tcxo_disable_work,
366 msecs_to_jiffies(TCXO_WARMUP_TIME_MS) + 1);
367}
368
David Collins65b5fa62012-10-17 17:02:11 -0700369/* Mutex lock needed for sleep-selectable regulators. */
370static DEFINE_MUTEX(rpm_sleep_sel_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371
372static int voltage_from_req(struct vreg *vreg)
373{
David Collins6f032ba2011-08-31 14:08:15 -0700374 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375
David Collins6f032ba2011-08-31 14:08:15 -0700376 if (vreg->part->uV.mask)
377 uV = GET_PART(vreg, uV);
David Collins13397f22012-02-06 13:53:29 -0800378 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700379 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
David Collins13397f22012-02-06 13:53:29 -0800380 else if (vreg->part->enable_state.mask)
381 uV = GET_PART(vreg, enable_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382
David Collins6f032ba2011-08-31 14:08:15 -0700383 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384}
385
David Collins6f032ba2011-08-31 14:08:15 -0700386static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387{
David Collins6f032ba2011-08-31 14:08:15 -0700388 if (vreg->part->uV.mask)
389 SET_PART(vreg, uV, uV);
David Collins13397f22012-02-06 13:53:29 -0800390 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700391 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
David Collins13397f22012-02-06 13:53:29 -0800392 else if (vreg->part->enable_state.mask)
393 SET_PART(vreg, enable_state, uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394}
395
396static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
397 int set, unsigned mask0, unsigned val0,
398 unsigned mask1, unsigned val1, unsigned cnt,
399 int update_voltage)
400{
401 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700402 int rc = 0, max_uV_vote = 0;
David Collins109a8e62012-07-31 15:21:35 -0700403 bool tcxo_enabled = false;
404 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700406 int *min_uV_vote;
407 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408
409 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700410 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411 prev_req = vreg->prev_active_req;
412 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700413 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414 prev_req = vreg->prev_sleep_req;
415 }
416
417 prev0 = vreg->req[0].value;
418 vreg->req[0].value &= ~mask0;
419 vreg->req[0].value |= val0 & mask0;
420
421 prev1 = vreg->req[1].value;
422 vreg->req[1].value &= ~mask1;
423 vreg->req[1].value |= val1 & mask1;
424
David Collinsd1b7aea2012-03-19 10:27:55 -0700425 /* Set the force mode field based on which set is being requested. */
426 if (set == MSM_RPM_CTX_SET_0)
427 SET_PART(vreg, fm, vreg->pdata.force_mode);
428 else
429 SET_PART(vreg, fm, vreg->pdata.sleep_set_force_mode);
430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700432 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433
434 /* Find the highest voltage voted for and use it. */
435 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700436 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
437 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438
439 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700440 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
441 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442
443 /* Ignore duplicate requests */
444 if (vreg->req[0].value != prev_req[0].value ||
445 vreg->req[1].value != prev_req[1].value) {
David Collins109a8e62012-07-31 15:21:35 -0700446
447 /* Enable CXO clock if necessary for TCXO workaround. */
448 if (requires_tcxo_workaround && vreg->requires_cxo
449 && (set == MSM_RPM_CTX_SET_0)
450 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
David Collins65b5fa62012-10-17 17:02:11 -0700451 mutex_lock(&tcxo_mutex);
452 if (!tcxo_handle)
453 tcxo_get_handle();
David Collins109a8e62012-07-31 15:21:35 -0700454 voltage_increased = true;
David Collins109a8e62012-07-31 15:21:35 -0700455 tcxo_enabled = tcxo_enable();
456 }
457
David Collins65b5fa62012-10-17 17:02:11 -0700458 rc = msm_rpmrs_set(set, vreg->req, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 if (rc) {
460 vreg->req[0].value = prev0;
461 vreg->req[1].value = prev1;
462
David Collins65b5fa62012-10-17 17:02:11 -0700463 vreg_err(vreg, "msm_rpmrs_set failed - "
David Collins6f032ba2011-08-31 14:08:15 -0700464 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
466 vreg->req[0].id, rc);
467 } else {
468 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700469 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
470 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 if (msm_rpm_vreg_debug_mask
472 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700473 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 prev_req[0].value = vreg->req[0].value;
475 prev_req[1].value = vreg->req[1].value;
476 }
David Collins109a8e62012-07-31 15:21:35 -0700477
478 /*
479 * Schedule CXO clock to be disabled after TCXO warmup time if
480 * TCXO workaround is applicable for this regulator.
481 */
482 if (voltage_increased) {
483 if (tcxo_enabled)
484 tcxo_delayed_disable();
David Collins65b5fa62012-10-17 17:02:11 -0700485 mutex_unlock(&tcxo_mutex);
David Collins109a8e62012-07-31 15:21:35 -0700486 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700488 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 }
490
491 return rc;
492}
493
David Collins65b5fa62012-10-17 17:02:11 -0700494static int vreg_set_sleep_sel(struct vreg *vreg, enum rpm_vreg_voter voter,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 int sleep, unsigned mask0, unsigned val0,
496 unsigned mask1, unsigned val1, unsigned cnt,
497 int update_voltage)
498{
David Collins6f032ba2011-08-31 14:08:15 -0700499 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501
502 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
503 return -EINVAL;
504
David Collins65b5fa62012-10-17 17:02:11 -0700505 mutex_lock(&rpm_sleep_sel_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506
507 /*
508 * Send sleep set request first so that subsequent set_mode, etc calls
509 * use the voltage from the active set.
510 */
511 if (sleep)
512 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
513 mask0, val0, mask1, val1, cnt, update_voltage);
514 else {
515 /*
516 * Vote for 0 V in the sleep set when active set-only is
517 * specified. This ensures that a disable vote will be issued
518 * at some point for the sleep set of the regulator.
519 */
David Collins6f032ba2011-08-31 14:08:15 -0700520 if (vreg->part->uV.mask) {
521 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
522 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800523 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700524 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
525 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800526 } else if (vreg->part->enable_state.mask) {
527 s_val[vreg->part->enable_state.word]
528 = 0 << vreg->part->enable_state.shift;
529 s_mask[vreg->part->enable_state.word]
530 = vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 }
532
533 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700534 s_mask[0], s_val[0], s_mask[1], s_val[1],
535 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 }
537
538 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
539 mask1, val1, cnt, update_voltage);
540
David Collins65b5fa62012-10-17 17:02:11 -0700541 mutex_unlock(&rpm_sleep_sel_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542
543 return rc;
544}
545
546/**
547 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
548 * @vreg: ID for regulator
549 * @voter: ID for the voter
550 * @min_uV: minimum acceptable voltage (in uV) that is voted for
551 * @max_uV: maximum acceptable voltage (in uV) that is voted for
552 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
553 *
554 * Returns 0 on success or errno.
555 *
556 * This function is used to vote for the voltage of a regulator without
David Collins65b5fa62012-10-17 17:02:11 -0700557 * using the regulator framework. It is needed for consumers which wish to only
558 * vote for active set regulator voltage.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 *
560 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
561 *
562 * This function may only be called for regulators which have the sleep flag
563 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700564 *
565 * Consumers can vote to disable a regulator with this function by passing
566 * min_uV = 0 and max_uV = 0.
David Collins13397f22012-02-06 13:53:29 -0800567 *
568 * Voltage switch type regulators may be controlled via rpm_vreg_set_voltage
569 * as well. For this type of regulator, max_uV > 0 is treated as an enable
570 * request and max_uV == 0 is treated as a disable request.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 */
David Collins6f032ba2011-08-31 14:08:15 -0700572int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
573 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574{
David Collins6f032ba2011-08-31 14:08:15 -0700575 unsigned int mask[2] = {0}, val[2] = {0};
576 struct vreg_range *range;
577 struct vreg *vreg;
578 int uV = min_uV;
579 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
David Collins6f032ba2011-08-31 14:08:15 -0700581 if (!config) {
582 pr_err("rpm-regulator driver has not probed yet.\n");
583 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 }
585
David Collins6f032ba2011-08-31 14:08:15 -0700586 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
587 pr_err("invalid regulator id=%d\n", vreg_id);
588 return -EINVAL;
589 }
590
591 vreg = &config->vregs[vreg_id];
David Collins6f032ba2011-08-31 14:08:15 -0700592
593 if (!vreg->pdata.sleep_selectable) {
594 vreg_err(vreg, "regulator is not marked sleep selectable\n");
595 return -EINVAL;
596 }
597
David Collins7462b9d2011-10-11 16:02:17 -0700598 /* Allow min_uV == max_uV == 0 to represent a disable request. */
David Collins13397f22012-02-06 13:53:29 -0800599 if ((min_uV != 0 || max_uV != 0)
600 && (vreg->part->uV.mask || vreg->part->mV.mask)) {
David Collins7462b9d2011-10-11 16:02:17 -0700601 /*
602 * Check if request voltage is outside of allowed range. The
603 * regulator core has already checked that constraint range
604 * is inside of the physically allowed range.
605 */
606 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
607 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700608
David Collins7462b9d2011-10-11 16:02:17 -0700609 if (uV < lim_min_uV && max_uV >= lim_min_uV)
610 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700611
David Collins7462b9d2011-10-11 16:02:17 -0700612 if (uV < lim_min_uV || uV > lim_max_uV) {
613 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
614 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
615 lim_max_uV);
616 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700617 }
David Collins6f032ba2011-08-31 14:08:15 -0700618
David Collins13397f22012-02-06 13:53:29 -0800619 range = &vreg->set_points->range[0];
David Collins7462b9d2011-10-11 16:02:17 -0700620 /* Find the range which uV is inside of. */
621 for (i = vreg->set_points->count - 1; i > 0; i--) {
622 if (uV > vreg->set_points->range[i - 1].max_uV) {
623 range = &vreg->set_points->range[i];
624 break;
625 }
626 }
627
628 /*
629 * Force uV to be an allowed set point and apply a ceiling
630 * function to non-set point values.
631 */
632 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
633 uV = uV * range->step_uV + range->min_uV;
David Collins3974b612011-11-21 15:07:36 -0800634
635 if (uV > max_uV) {
636 vreg_err(vreg,
637 "request v=[%d, %d] cannot be met by any set point; "
638 "next set point: %d\n",
639 min_uV, max_uV, uV);
640 return -EINVAL;
641 }
David Collins7462b9d2011-10-11 16:02:17 -0700642 }
David Collins6f032ba2011-08-31 14:08:15 -0700643
David Collins0ac31fe2012-02-08 13:53:34 -0800644 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
645 /*
646 * Translate from enum values which work as inputs in the
647 * rpm_vreg_set_voltage function to the actual corner values
648 * sent to the RPM.
649 */
650 if (uV > 0)
651 uV -= RPM_VREG_CORNER_NONE;
652 }
653
David Collins6f032ba2011-08-31 14:08:15 -0700654 if (vreg->part->uV.mask) {
655 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
656 mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800657 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700658 val[vreg->part->mV.word]
659 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
660 mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800661 } else if (vreg->part->enable_state.mask) {
662 /*
663 * Translate max_uV > 0 into an enable request for regulator
664 * types which to not support voltage setting, e.g. voltage
665 * switches.
666 */
667 val[vreg->part->enable_state.word]
668 = (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift;
669 mask[vreg->part->enable_state.word]
670 = vreg->part->enable_state.mask;
David Collins6f032ba2011-08-31 14:08:15 -0700671 }
672
David Collins65b5fa62012-10-17 17:02:11 -0700673 rc = vreg_set_sleep_sel(vreg, voter, sleep_also, mask[0], val[0],
674 mask[1], val[1], vreg->part->request_len, 1);
David Collins6f032ba2011-08-31 14:08:15 -0700675 if (rc)
David Collins65b5fa62012-10-17 17:02:11 -0700676 vreg_err(vreg, "vreg_set_sleep_sel failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677
678 return rc;
679}
680EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
681
682/**
683 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
684 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700685 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 *
687 * Returns 0 on success or errno.
688 */
David Collins6f032ba2011-08-31 14:08:15 -0700689int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690{
David Collins6f032ba2011-08-31 14:08:15 -0700691 unsigned int mask[2] = {0}, val[2] = {0};
692 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 int rc;
694
David Collins6f032ba2011-08-31 14:08:15 -0700695 if (!config) {
696 pr_err("rpm-regulator driver has not probed yet.\n");
697 return -ENODEV;
698 }
699
700 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
701 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 return -EINVAL;
703 }
704
David Collins6f032ba2011-08-31 14:08:15 -0700705 vreg = &config->vregs[vreg_id];
706
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700708 vreg_err(vreg, "invalid frequency=%d\n", freq);
709 return -EINVAL;
710 }
711 if (!vreg->pdata.sleep_selectable) {
712 vreg_err(vreg, "regulator is not marked sleep selectable\n");
713 return -EINVAL;
714 }
715 if (!vreg->part->freq.mask) {
716 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 return -EINVAL;
718 }
719
David Collins6f032ba2011-08-31 14:08:15 -0700720 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
721 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722
David Collins65b5fa62012-10-17 17:02:11 -0700723 rc = vreg_set_sleep_sel(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
David Collins6f032ba2011-08-31 14:08:15 -0700724 val[0], mask[1], val[1], vreg->part->request_len, 0);
725 if (rc)
David Collins65b5fa62012-10-17 17:02:11 -0700726 vreg_err(vreg, "vreg_set_sleep_sel failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727
728 return rc;
729}
730EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
731
David Collins379b4b72012-05-14 16:33:51 -0700732#define MAX_NAME_LEN 64
733/**
734 * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
735 * @dev: device for regulator consumer
736 * @supply: supply name
737 *
738 * Returns a struct rpm_regulator corresponding to the regulator producer,
739 * or ERR_PTR() containing errno.
740 *
741 * This function may only be called from nonatomic context. The mapping between
742 * <dev, supply> tuples and rpm_regulators struct pointers is specified via
743 * rpm-regulator platform data.
744 */
745struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
746{
747 struct rpm_regulator_consumer_mapping *mapping = NULL;
748 const char *devname = NULL;
749 struct rpm_regulator *regulator;
750 int i;
751
752 if (!config) {
753 pr_err("rpm-regulator driver has not probed yet.\n");
754 return ERR_PTR(-ENODEV);
755 }
756
757 if (consumer_map == NULL || consumer_map_len == 0) {
758 pr_err("No private consumer mapping has been specified.\n");
759 return ERR_PTR(-ENODEV);
760 }
761
762 if (supply == NULL) {
763 pr_err("supply name must be specified\n");
764 return ERR_PTR(-EINVAL);
765 }
766
767 if (dev)
768 devname = dev_name(dev);
769
770 for (i = 0; i < consumer_map_len; i++) {
771 /* If the mapping has a device set up it must match */
772 if (consumer_map[i].dev_name &&
773 (!devname || strncmp(consumer_map[i].dev_name, devname,
774 MAX_NAME_LEN)))
775 continue;
776
777 if (strncmp(consumer_map[i].supply, supply, MAX_NAME_LEN)
778 == 0) {
779 mapping = &consumer_map[i];
780 break;
781 }
782 }
783
784 if (mapping == NULL) {
785 pr_err("could not find mapping for dev=%s, supply=%s\n",
786 (devname ? devname : "(null)"), supply);
787 return ERR_PTR(-ENODEV);
788 }
789
790 regulator = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
791 if (regulator == NULL) {
792 pr_err("could not allocate memory for regulator\n");
793 return ERR_PTR(-ENOMEM);
794 }
795
796 regulator->vreg_id = mapping->vreg_id;
797 regulator->voter = mapping->voter;
798 regulator->sleep_also = mapping->sleep_also;
799
800 return regulator;
801}
802EXPORT_SYMBOL_GPL(rpm_regulator_get);
803
804static int rpm_regulator_check_input(struct rpm_regulator *regulator)
805{
806 int rc = 0;
807
808 if (regulator == NULL) {
809 rc = -EINVAL;
810 pr_err("invalid (null) rpm_regulator pointer\n");
811 } else if (IS_ERR(regulator)) {
812 rc = PTR_ERR(regulator);
813 pr_err("invalid rpm_regulator pointer, rc=%d\n", rc);
814 }
815
816 return rc;
817}
818
819/**
820 * rpm_regulator_put() - free the RPM regulator handle
821 * @regulator: RPM regulator handle
822 *
823 * Parameter reaggregation does not take place when rpm_regulator_put is called.
824 * Therefore, regulator enable state and voltage must be configured
825 * appropriately before calling rpm_regulator_put.
826 *
827 * This function may be called from either atomic or nonatomic context.
828 */
829void rpm_regulator_put(struct rpm_regulator *regulator)
830{
831 kfree(regulator);
832}
833EXPORT_SYMBOL_GPL(rpm_regulator_put);
834
835/**
836 * rpm_regulator_enable() - enable regulator output
837 * @regulator: RPM regulator handle
838 *
839 * Returns 0 on success or errno on failure.
840 *
841 * This function may be called from either atomic or nonatomic context. This
842 * function may only be called for regulators which have the sleep_selectable
843 * flag set in their configuration data.
844 *
845 * rpm_regulator_set_voltage must be called before rpm_regulator_enable because
846 * enabling is defined by the RPM interface to be requesting the desired
847 * non-zero regulator output voltage.
848 */
849int rpm_regulator_enable(struct rpm_regulator *regulator)
850{
851 int rc = rpm_regulator_check_input(regulator);
852 struct vreg *vreg;
853
854 if (rc)
855 return rc;
856
857 if (regulator->vreg_id < config->vreg_id_min
858 || regulator->vreg_id > config->vreg_id_max) {
859 pr_err("invalid regulator id=%d\n", regulator->vreg_id);
860 return -EINVAL;
861 }
862
863 vreg = &config->vregs[regulator->vreg_id];
864
865 /*
866 * Handle voltage switches which can be enabled without
867 * rpm_regulator_set_voltage ever being called.
868 */
869 if (regulator->min_uV == 0 && regulator->max_uV == 0
870 && vreg->part->uV.mask == 0 && vreg->part->mV.mask == 0) {
871 regulator->min_uV = 1;
872 regulator->max_uV = 1;
873 }
874
875 if (regulator->min_uV == 0 && regulator->max_uV == 0) {
876 pr_err("Voltage must be set with rpm_regulator_set_voltage "
877 "before calling rpm_regulator_enable; vreg_id=%d, "
878 "voter=%d\n", regulator->vreg_id, regulator->voter);
879 return -EINVAL;
880 }
881
882 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter,
883 regulator->min_uV, regulator->max_uV, regulator->sleep_also);
884
885 if (rc)
886 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
887
888 return rc;
889}
890EXPORT_SYMBOL_GPL(rpm_regulator_enable);
891
892/**
893 * rpm_regulator_disable() - disable regulator output
894 * @regulator: RPM regulator handle
895 *
896 * Returns 0 on success or errno on failure.
897 *
898 * The enable state of the regulator is determined by aggregating the requests
899 * of all consumers. Therefore, it is possible that the regulator will remain
900 * enabled even after rpm_regulator_disable is called.
901 *
902 * This function may be called from either atomic or nonatomic context. This
903 * function may only be called for regulators which have the sleep_selectable
904 * flag set in their configuration data.
905 */
906int rpm_regulator_disable(struct rpm_regulator *regulator)
907{
908 int rc = rpm_regulator_check_input(regulator);
909
910 if (rc)
911 return rc;
912
913 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, 0, 0,
914 regulator->sleep_also);
915
916 if (rc)
917 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
918
919 return rc;
920}
921EXPORT_SYMBOL_GPL(rpm_regulator_disable);
922
923/**
924 * rpm_regulator_set_voltage() - set regulator output voltage
925 * @regulator: RPM regulator handle
926 * @min_uV: minimum required voltage in uV
927 * @max_uV: maximum acceptable voltage in uV
928 *
929 * Sets a voltage regulator to the desired output voltage. This can be set
930 * while the regulator is disabled or enabled. If the regulator is disabled,
931 * then rpm_regulator_set_voltage will both enable the regulator and set it to
932 * output at the requested voltage.
933 *
934 * The min_uV to max_uV voltage range requested must intersect with the
935 * voltage constraint range configured for the regulator.
936 *
937 * Returns 0 on success or errno on failure.
938 *
939 * The final voltage value that is sent to the RPM is aggregated based upon the
940 * values requested by all consumers of the regulator. This corresponds to the
941 * maximum min_uV value.
942 *
943 * This function may be called from either atomic or nonatomic context. This
944 * function may only be called for regulators which have the sleep_selectable
945 * flag set in their configuration data.
946 */
947int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
948 int max_uV)
949{
950 int rc = rpm_regulator_check_input(regulator);
951
952 if (rc)
953 return rc;
954
955 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, min_uV,
956 max_uV, regulator->sleep_also);
957
958 if (rc) {
959 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
960 } else {
961 regulator->min_uV = min_uV;
962 regulator->max_uV = max_uV;
963 }
964
965 return rc;
966}
967EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
968
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969static inline int vreg_hpm_min_uA(struct vreg *vreg)
970{
971 return vreg->hpm_min_load;
972}
973
974static inline int vreg_lpm_max_uA(struct vreg *vreg)
975{
976 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
977}
978
David Collins6f032ba2011-08-31 14:08:15 -0700979static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980{
David Collins6f032ba2011-08-31 14:08:15 -0700981 unsigned load_max
982 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
983
984 return (load_uA > load_max ? load_max : load_uA);
985}
986
987static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
988{
989 unsigned load_max
990 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
991 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992}
993
994/* Change vreg->req, but do not send it to the RPM. */
995static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
996 unsigned mask1, unsigned val1)
997{
David Collins6f032ba2011-08-31 14:08:15 -0700998 if (vreg->pdata.sleep_selectable)
David Collins65b5fa62012-10-17 17:02:11 -0700999 mutex_lock(&rpm_sleep_sel_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
1001 vreg->req[0].value &= ~mask0;
1002 vreg->req[0].value |= val0 & mask0;
1003
1004 vreg->req[1].value &= ~mask1;
1005 vreg->req[1].value |= val1 & mask1;
1006
David Collins6f032ba2011-08-31 14:08:15 -07001007 if (vreg->pdata.sleep_selectable)
David Collins65b5fa62012-10-17 17:02:11 -07001008 mutex_unlock(&rpm_sleep_sel_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009
1010 return 0;
1011}
1012
1013static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
1014 unsigned mask1, unsigned val1, unsigned cnt)
1015{
1016 unsigned prev0 = 0, prev1 = 0;
David Collins109a8e62012-07-31 15:21:35 -07001017 bool tcxo_enabled = false;
1018 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 int rc;
1020
1021 /*
1022 * Bypass the normal route for regulators that can be called to change
1023 * just the active set values.
1024 */
David Collins6f032ba2011-08-31 14:08:15 -07001025 if (vreg->pdata.sleep_selectable)
David Collins65b5fa62012-10-17 17:02:11 -07001026 return vreg_set_sleep_sel(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 mask0, val0, mask1, val1, cnt, 1);
1028
1029 prev0 = vreg->req[0].value;
1030 vreg->req[0].value &= ~mask0;
1031 vreg->req[0].value |= val0 & mask0;
1032
1033 prev1 = vreg->req[1].value;
1034 vreg->req[1].value &= ~mask1;
1035 vreg->req[1].value |= val1 & mask1;
1036
1037 /* Ignore duplicate requests */
1038 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
1039 vreg->req[1].value == vreg->prev_active_req[1].value) {
1040 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -07001041 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042 return 0;
1043 }
1044
David Collins109a8e62012-07-31 15:21:35 -07001045 /* Enable CXO clock if necessary for TCXO workaround. */
1046 if (requires_tcxo_workaround && vreg->requires_cxo
1047 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
David Collins65b5fa62012-10-17 17:02:11 -07001048 mutex_lock(&tcxo_mutex);
David Collins109a8e62012-07-31 15:21:35 -07001049 if (!tcxo_handle)
1050 tcxo_get_handle();
David Collins109a8e62012-07-31 15:21:35 -07001051 voltage_increased = true;
1052 tcxo_enabled = tcxo_enable();
1053 }
1054
David Collins65b5fa62012-10-17 17:02:11 -07001055 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
David Collins109a8e62012-07-31 15:21:35 -07001056
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 if (rc) {
1058 vreg->req[0].value = prev0;
1059 vreg->req[1].value = prev1;
1060
David Collins6f032ba2011-08-31 14:08:15 -07001061 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
1062 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063 } else {
1064 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -07001065 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066 vreg->prev_active_req[0].value = vreg->req[0].value;
1067 vreg->prev_active_req[1].value = vreg->req[1].value;
1068 }
1069
David Collins109a8e62012-07-31 15:21:35 -07001070 /*
1071 * Schedule CXO clock to be disabled after TCXO warmup time if TCXO
1072 * workaround is applicable for this regulator.
1073 */
1074 if (voltage_increased) {
1075 if (tcxo_enabled)
1076 tcxo_delayed_disable();
David Collins65b5fa62012-10-17 17:02:11 -07001077 mutex_unlock(&tcxo_mutex);
David Collins109a8e62012-07-31 15:21:35 -07001078 }
1079
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 return rc;
1081}
1082
David Collins6f032ba2011-08-31 14:08:15 -07001083static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084{
David Collins6f032ba2011-08-31 14:08:15 -07001085 struct vreg *vreg = rdev_get_drvdata(rdev);
1086 int enabled;
1087
1088 mutex_lock(&vreg->pc_lock);
1089 enabled = vreg->is_enabled;
1090 mutex_unlock(&vreg->pc_lock);
1091
1092 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093}
1094
David Collins6f032ba2011-08-31 14:08:15 -07001095static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096{
David Collins6f032ba2011-08-31 14:08:15 -07001097 switch (vreg->type) {
1098 case RPM_REGULATOR_TYPE_LDO:
1099 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001100 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001101 /* Enable by setting a voltage. */
1102 if (vreg->part->uV.mask) {
1103 val[vreg->part->uV.word]
1104 |= vreg->save_uV << vreg->part->uV.shift;
1105 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1106 } else {
1107 val[vreg->part->mV.word]
1108 |= MICRO_TO_MILLI(vreg->save_uV)
1109 << vreg->part->mV.shift;
1110 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1111 }
1112 break;
1113 case RPM_REGULATOR_TYPE_VS:
1114 case RPM_REGULATOR_TYPE_NCP:
1115 /* Enable by setting enable_state. */
1116 val[vreg->part->enable_state.word]
1117 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
1118 mask[vreg->part->enable_state.word]
1119 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121}
1122
David Collins0ac31fe2012-02-08 13:53:34 -08001123static int rpm_vreg_enable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001124{
1125 struct vreg *vreg = rdev_get_drvdata(rdev);
1126 unsigned int mask[2] = {0}, val[2] = {0};
1127 int rc = 0;
1128
1129 set_enable(vreg, mask, val);
1130
1131 mutex_lock(&vreg->pc_lock);
1132
1133 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1134 vreg->part->request_len);
1135 if (!rc)
1136 vreg->is_enabled = true;
1137
1138 mutex_unlock(&vreg->pc_lock);
1139
1140 if (rc)
1141 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1142
1143 return rc;
1144}
1145
1146static void set_disable(struct vreg *vreg, unsigned int *mask,
1147 unsigned int *val)
1148{
1149 switch (vreg->type) {
1150 case RPM_REGULATOR_TYPE_LDO:
1151 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001152 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001153 /* Disable by setting a voltage of 0 uV. */
1154 if (vreg->part->uV.mask) {
1155 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
1156 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1157 } else {
1158 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
1159 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1160 }
1161 break;
1162 case RPM_REGULATOR_TYPE_VS:
1163 case RPM_REGULATOR_TYPE_NCP:
1164 /* Disable by setting enable_state. */
1165 val[vreg->part->enable_state.word]
1166 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
1167 mask[vreg->part->enable_state.word]
1168 |= vreg->part->enable_state.mask;
1169 }
1170}
1171
David Collins0ac31fe2012-02-08 13:53:34 -08001172static int rpm_vreg_disable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001173{
1174 struct vreg *vreg = rdev_get_drvdata(rdev);
1175 unsigned int mask[2] = {0}, val[2] = {0};
1176 int rc = 0;
1177
1178 set_disable(vreg, mask, val);
1179
1180 mutex_lock(&vreg->pc_lock);
1181
1182 /* Only disable if pin control is not in use. */
1183 if (!vreg->is_enabled_pc)
1184 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1185 vreg->part->request_len);
1186
1187 if (!rc)
1188 vreg->is_enabled = false;
1189
1190 mutex_unlock(&vreg->pc_lock);
1191
1192 if (rc)
1193 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1194
1195 return rc;
1196}
1197
1198static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001199 unsigned *selector)
1200{
David Collins6f032ba2011-08-31 14:08:15 -07001201 struct vreg *vreg = rdev_get_drvdata(rdev);
1202 struct vreg_range *range = &vreg->set_points->range[0];
1203 unsigned int mask[2] = {0}, val[2] = {0};
1204 int rc = 0, uV = min_uV;
1205 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206
David Collins6f032ba2011-08-31 14:08:15 -07001207 /* Check if request voltage is outside of physically settable range. */
1208 lim_min_uV = vreg->set_points->range[0].min_uV;
1209 lim_max_uV =
1210 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211
David Collins6f032ba2011-08-31 14:08:15 -07001212 if (uV < lim_min_uV && max_uV >= lim_min_uV)
1213 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214
David Collins6f032ba2011-08-31 14:08:15 -07001215 if (uV < lim_min_uV || uV > lim_max_uV) {
1216 vreg_err(vreg,
1217 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1218 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219 return -EINVAL;
1220 }
1221
David Collins6f032ba2011-08-31 14:08:15 -07001222 /* Find the range which uV is inside of. */
1223 for (i = vreg->set_points->count - 1; i > 0; i--) {
1224 if (uV > vreg->set_points->range[i - 1].max_uV) {
1225 range = &vreg->set_points->range[i];
1226 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 }
1228 }
1229
David Collins6f032ba2011-08-31 14:08:15 -07001230 /*
1231 * Force uV to be an allowed set point and apply a ceiling function
1232 * to non-set point values.
1233 */
1234 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
1235 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236
David Collins3974b612011-11-21 15:07:36 -08001237 if (uV > max_uV) {
1238 vreg_err(vreg,
1239 "request v=[%d, %d] cannot be met by any set point; "
1240 "next set point: %d\n",
1241 min_uV, max_uV, uV);
1242 return -EINVAL;
1243 }
1244
David Collins0ac31fe2012-02-08 13:53:34 -08001245 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
1246 /*
1247 * Translate from enum values which work as inputs in the
1248 * regulator_set_voltage function to the actual corner values
1249 * sent to the RPM.
1250 */
1251 uV -= RPM_VREG_CORNER_NONE;
1252 }
1253
David Collins6f032ba2011-08-31 14:08:15 -07001254 if (vreg->part->uV.mask) {
1255 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
1256 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 } else {
David Collins6f032ba2011-08-31 14:08:15 -07001258 val[vreg->part->mV.word]
1259 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
1260 mask[vreg->part->mV.word] = vreg->part->mV.mask;
1261 }
1262
1263 mutex_lock(&vreg->pc_lock);
1264
1265 /*
1266 * Only send a request for a new voltage if the regulator is currently
1267 * enabled. This will ensure that LDO and SMPS regulators are not
1268 * inadvertently turned on because voltage > 0 is equivalent to
1269 * enabling. For NCP, this just removes unnecessary RPM requests.
1270 */
1271 if (vreg->is_enabled) {
1272 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1273 vreg->part->request_len);
1274 if (rc)
1275 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1276 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001278 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001279 }
1280
David Collins6f032ba2011-08-31 14:08:15 -07001281 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
1282 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001283
David Collins6f032ba2011-08-31 14:08:15 -07001284 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285
1286 return rc;
1287}
1288
David Collins6f032ba2011-08-31 14:08:15 -07001289static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290{
David Collins6f032ba2011-08-31 14:08:15 -07001291 struct vreg *vreg = rdev_get_drvdata(rdev);
1292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 return vreg->save_uV;
1294}
1295
David Collins6f032ba2011-08-31 14:08:15 -07001296static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001297{
David Collins6f032ba2011-08-31 14:08:15 -07001298 struct vreg *vreg = rdev_get_drvdata(rdev);
1299 int uV = 0;
1300 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301
David Collins6f032ba2011-08-31 14:08:15 -07001302 if (!vreg->set_points) {
1303 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 return -EINVAL;
1305 }
1306
David Collins6f032ba2011-08-31 14:08:15 -07001307 if (selector >= vreg->set_points->n_voltages)
1308 return 0;
1309
1310 for (i = 0; i < vreg->set_points->count; i++) {
1311 if (selector < vreg->set_points->range[i].n_voltages) {
1312 uV = selector * vreg->set_points->range[i].step_uV
1313 + vreg->set_points->range[i].min_uV;
1314 break;
1315 } else {
1316 selector -= vreg->set_points->range[i].n_voltages;
1317 }
1318 }
1319
1320 return uV;
1321}
1322
1323static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
1324{
1325 struct vreg *vreg = rdev_get_drvdata(rdev);
1326 unsigned int mask[2] = {0}, val[2] = {0};
1327 int rc = 0;
1328 int peak_uA;
1329
1330 mutex_lock(&vreg->pc_lock);
1331
1332 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
1333 & vreg->part->ip.mask) >> vreg->part->ip.shift);
1334
1335 if (mode == config->mode_hpm) {
1336 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001338 val[vreg->part->ip.word]
1339 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1340 << vreg->part->ip.shift;
1341 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1342
1343 if (config->ia_follows_ip) {
1344 val[vreg->part->ia.word]
1345 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1346 << vreg->part->ia.shift;
1347 mask[vreg->part->ia.word]
1348 |= vreg->part->ia.mask;
1349 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350 }
David Collins6f032ba2011-08-31 14:08:15 -07001351 } else if (mode == config->mode_lpm) {
1352 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001353 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001354 val[vreg->part->ip.word]
1355 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1356 << vreg->part->ip.shift;
1357 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1358
1359 if (config->ia_follows_ip) {
1360 val[vreg->part->ia.word]
1361 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1362 << vreg->part->ia.shift;
1363 mask[vreg->part->ia.word]
1364 |= vreg->part->ia.mask;
1365 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001366 }
David Collins6f032ba2011-08-31 14:08:15 -07001367 } else {
1368 vreg_err(vreg, "invalid mode: %u\n", mode);
1369 mutex_unlock(&vreg->pc_lock);
1370 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371 }
1372
David Collins6f032ba2011-08-31 14:08:15 -07001373 if (vreg->is_enabled) {
1374 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1375 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001376 } else {
1377 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001378 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001379 }
David Collins6f032ba2011-08-31 14:08:15 -07001380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001381 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001382 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1383 else
1384 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385
David Collins6f032ba2011-08-31 14:08:15 -07001386 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001387
David Collins6f032ba2011-08-31 14:08:15 -07001388 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389}
1390
David Collins6f032ba2011-08-31 14:08:15 -07001391static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392{
David Collins6f032ba2011-08-31 14:08:15 -07001393 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394
David Collins6f032ba2011-08-31 14:08:15 -07001395 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001396}
1397
David Collins6f032ba2011-08-31 14:08:15 -07001398static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
1399 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001400{
David Collins6f032ba2011-08-31 14:08:15 -07001401 struct vreg *vreg = rdev_get_drvdata(rdev);
1402 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403
David Collins6f032ba2011-08-31 14:08:15 -07001404 load_uA += vreg->pdata.system_uA;
1405
1406 mutex_lock(&vreg->pc_lock);
1407 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
1408 if (config->ia_follows_ip)
1409 SET_PART(vreg, ia,
1410 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
1411 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412
1413 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -07001414 mode = config->mode_hpm;
1415 else
1416 mode = config->mode_lpm;
1417
1418 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419}
1420
David Collins6f032ba2011-08-31 14:08:15 -07001421static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
1422 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423{
David Collins6f032ba2011-08-31 14:08:15 -07001424 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001425
David Collins6f032ba2011-08-31 14:08:15 -07001426 if (MICRO_TO_MILLI(load_uA) <= 0) {
1427 /*
1428 * vreg_legacy_get_optimum_mode is being called before consumers
1429 * have specified their load currents via
1430 * regulator_set_optimum_mode. Return whatever the existing mode
1431 * is.
1432 */
1433 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001434 }
1435
David Collins6f032ba2011-08-31 14:08:15 -07001436 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437}
1438
1439/*
David Collins6f032ba2011-08-31 14:08:15 -07001440 * Returns the logical pin control enable state because the pin control options
1441 * present in the hardware out of restart could be different from those desired
1442 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443 */
David Collins6f032ba2011-08-31 14:08:15 -07001444static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445{
David Collins6f032ba2011-08-31 14:08:15 -07001446 struct vreg *vreg = rdev_get_drvdata(rdev);
1447
1448 return vreg->is_enabled_pc;
1449}
1450
1451static int vreg_pin_control_enable(struct regulator_dev *rdev)
1452{
1453 struct vreg *vreg = rdev_get_drvdata(rdev);
1454 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455 int rc;
1456
David Collins6f032ba2011-08-31 14:08:15 -07001457 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001458
David Collins6f032ba2011-08-31 14:08:15 -07001459 val[vreg->part->pc.word]
1460 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1461 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001462
David Collins6f032ba2011-08-31 14:08:15 -07001463 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1464 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001465
David Collins6f032ba2011-08-31 14:08:15 -07001466 if (!vreg->is_enabled)
1467 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001468
David Collins6f032ba2011-08-31 14:08:15 -07001469 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1470 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001471
David Collins6f032ba2011-08-31 14:08:15 -07001472 if (!rc)
1473 vreg->is_enabled_pc = true;
1474
1475 mutex_unlock(&vreg->pc_lock);
1476
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001477 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001478 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001479
David Collins6f032ba2011-08-31 14:08:15 -07001480 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481}
1482
David Collins6f032ba2011-08-31 14:08:15 -07001483static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001484{
David Collins6f032ba2011-08-31 14:08:15 -07001485 struct vreg *vreg = rdev_get_drvdata(rdev);
1486 unsigned int mask[2] = {0}, val[2] = {0};
1487 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001488
David Collins6f032ba2011-08-31 14:08:15 -07001489 mutex_lock(&vreg->pc_lock);
1490
1491 val[vreg->part->pc.word]
1492 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1493 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1494
1495 pin_fn = config->pin_func_none;
1496 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1497 pin_fn = config->pin_func_sleep_b;
1498 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1499 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1500
1501 if (!vreg->is_enabled)
1502 set_disable(vreg, mask, val);
1503
1504 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1505 vreg->part->request_len);
1506
1507 if (!rc)
1508 vreg->is_enabled_pc = false;
1509
1510 mutex_unlock(&vreg->pc_lock);
1511
1512 if (rc)
1513 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1514
1515 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001516}
1517
David Collins6f032ba2011-08-31 14:08:15 -07001518static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001519{
David Collins6f032ba2011-08-31 14:08:15 -07001520 struct vreg *vreg = rdev_get_drvdata(rdev);
1521
1522 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523}
1524
David Collins6f032ba2011-08-31 14:08:15 -07001525/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001526static struct regulator_ops ldo_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001527 .enable = rpm_vreg_enable,
1528 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001529 .is_enabled = vreg_is_enabled,
1530 .set_voltage = vreg_set_voltage,
1531 .get_voltage = vreg_get_voltage,
1532 .list_voltage = vreg_list_voltage,
1533 .set_mode = vreg_set_mode,
1534 .get_mode = vreg_get_mode,
1535 .get_optimum_mode = vreg_get_optimum_mode,
1536 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001537};
1538
1539static struct regulator_ops smps_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001540 .enable = rpm_vreg_enable,
1541 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001542 .is_enabled = vreg_is_enabled,
1543 .set_voltage = vreg_set_voltage,
1544 .get_voltage = vreg_get_voltage,
1545 .list_voltage = vreg_list_voltage,
1546 .set_mode = vreg_set_mode,
1547 .get_mode = vreg_get_mode,
1548 .get_optimum_mode = vreg_get_optimum_mode,
1549 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001550};
1551
1552static struct regulator_ops switch_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001553 .enable = rpm_vreg_enable,
1554 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001555 .is_enabled = vreg_is_enabled,
1556 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001557};
1558
1559static struct regulator_ops ncp_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001560 .enable = rpm_vreg_enable,
1561 .disable = rpm_vreg_disable,
1562 .is_enabled = vreg_is_enabled,
1563 .set_voltage = vreg_set_voltage,
1564 .get_voltage = vreg_get_voltage,
1565 .list_voltage = vreg_list_voltage,
1566 .enable_time = vreg_enable_time,
1567};
1568
1569static struct regulator_ops corner_ops = {
1570 .enable = rpm_vreg_enable,
1571 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001572 .is_enabled = vreg_is_enabled,
1573 .set_voltage = vreg_set_voltage,
1574 .get_voltage = vreg_get_voltage,
1575 .list_voltage = vreg_list_voltage,
1576 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001577};
1578
David Collins6f032ba2011-08-31 14:08:15 -07001579/* Pin control regulator operations. */
1580static struct regulator_ops pin_control_ops = {
1581 .enable = vreg_pin_control_enable,
1582 .disable = vreg_pin_control_disable,
1583 .is_enabled = vreg_pin_control_is_enabled,
1584};
1585
1586struct regulator_ops *vreg_ops[] = {
1587 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1588 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1589 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1590 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
David Collins0ac31fe2012-02-08 13:53:34 -08001591 [RPM_REGULATOR_TYPE_CORNER] = &corner_ops,
David Collins6f032ba2011-08-31 14:08:15 -07001592};
1593
David Collins109a8e62012-07-31 15:21:35 -07001594static struct vreg *rpm_vreg_get_vreg(int id)
1595{
1596 struct vreg *vreg;
1597
1598 if (id < config->vreg_id_min || id > config->vreg_id_max)
1599 return NULL;
1600
1601 if (!config->is_real_id(id))
1602 id = config->pc_id_to_real_id(id);
1603 vreg = &config->vregs[id];
1604
1605 return vreg;
1606}
1607
David Collins6f032ba2011-08-31 14:08:15 -07001608static int __devinit
1609rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1610 struct device *dev)
1611{
1612 struct regulator_desc *rdesc = NULL;
1613 struct regulator_dev *rdev;
1614 struct vreg *vreg;
1615 unsigned pin_ctrl;
David Collins109a8e62012-07-31 15:21:35 -07001616 int pin_fn;
David Collins6f032ba2011-08-31 14:08:15 -07001617 int rc = 0;
1618
1619 if (!pdata) {
1620 pr_err("platform data missing\n");
1621 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622 }
1623
David Collins109a8e62012-07-31 15:21:35 -07001624 vreg = rpm_vreg_get_vreg(pdata->id);
1625 if (!vreg) {
1626 pr_err("invalid regulator id: %d\n", pdata->id);
David Collins6f032ba2011-08-31 14:08:15 -07001627 return -ENODEV;
1628 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629
David Collins6f032ba2011-08-31 14:08:15 -07001630 if (config->is_real_id(pdata->id))
1631 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001632 else
David Collins6f032ba2011-08-31 14:08:15 -07001633 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001634
David Collins6f032ba2011-08-31 14:08:15 -07001635 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1636 pr_err("%s: invalid regulator type: %d\n",
1637 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001638 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001639 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001640
David Collins6f032ba2011-08-31 14:08:15 -07001641 mutex_lock(&vreg->pc_lock);
1642
1643 if (vreg->set_points)
1644 rdesc->n_voltages = vreg->set_points->n_voltages;
1645 else
1646 rdesc->n_voltages = 0;
1647
1648 rdesc->id = pdata->id;
1649 rdesc->owner = THIS_MODULE;
1650 rdesc->type = REGULATOR_VOLTAGE;
1651
1652 if (config->is_real_id(pdata->id)) {
1653 /*
1654 * Real regulator; do not modify pin control and pin function
1655 * values.
1656 */
1657 rdesc->ops = vreg_ops[vreg->type];
1658 pin_ctrl = vreg->pdata.pin_ctrl;
1659 pin_fn = vreg->pdata.pin_fn;
1660 memcpy(&(vreg->pdata), pdata,
1661 sizeof(struct rpm_regulator_init_data));
1662 vreg->pdata.pin_ctrl = pin_ctrl;
1663 vreg->pdata.pin_fn = pin_fn;
1664
1665 vreg->save_uV = vreg->pdata.default_uV;
1666 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1667 vreg->mode = config->mode_hpm;
1668 else
1669 vreg->mode = config->mode_lpm;
1670
1671 /* Initialize the RPM request. */
1672 SET_PART(vreg, ip,
1673 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1674 SET_PART(vreg, fm, vreg->pdata.force_mode);
1675 SET_PART(vreg, pm, vreg->pdata.power_mode);
1676 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1677 SET_PART(vreg, ia,
1678 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1679 SET_PART(vreg, freq, vreg->pdata.freq);
1680 SET_PART(vreg, freq_clk_src, 0);
1681 SET_PART(vreg, comp_mode, 0);
1682 SET_PART(vreg, hpm, 0);
1683 if (!vreg->is_enabled_pc) {
1684 SET_PART(vreg, pf, config->pin_func_none);
1685 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1686 }
1687 } else {
1688 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1689 == RPM_VREG_PIN_CTRL_NONE
1690 && pdata->pin_fn != config->pin_func_sleep_b) {
1691 pr_err("%s: no pin control input specified\n",
1692 vreg->rdesc.name);
1693 mutex_unlock(&vreg->pc_lock);
1694 return -EINVAL;
1695 }
1696 rdesc->ops = &pin_control_ops;
1697 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1698 vreg->pdata.pin_fn = pdata->pin_fn;
1699
1700 /* Initialize the RPM request. */
1701 pin_fn = config->pin_func_none;
1702 /* Allow pf=sleep_b to be specified by platform data. */
1703 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1704 pin_fn = config->pin_func_sleep_b;
1705 SET_PART(vreg, pf, pin_fn);
1706 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1707 }
1708
1709 mutex_unlock(&vreg->pc_lock);
1710
1711 if (rc)
1712 goto bail;
1713
Rajendra Nayak11eafc62011-11-18 16:47:19 +05301714 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001715 if (IS_ERR(rdev)) {
1716 rc = PTR_ERR(rdev);
1717 pr_err("regulator_register failed: %s, rc=%d\n",
1718 vreg->rdesc.name, rc);
1719 return rc;
1720 } else {
1721 if (config->is_real_id(pdata->id))
1722 vreg->rdev = rdev;
1723 else
1724 vreg->rdev_pc = rdev;
1725 }
1726
1727bail:
1728 if (rc)
1729 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1730
1731 return rc;
1732}
1733
1734static void rpm_vreg_set_point_init(void)
1735{
1736 struct vreg_set_points **set_points;
1737 int i, j, temp;
1738
1739 set_points = config->set_points;
1740
1741 /* Calculate the number of set points available for each regulator. */
1742 for (i = 0; i < config->set_points_len; i++) {
1743 temp = 0;
1744 for (j = 0; j < set_points[i]->count; j++) {
1745 set_points[i]->range[j].n_voltages
1746 = (set_points[i]->range[j].max_uV
1747 - set_points[i]->range[j].min_uV)
1748 / set_points[i]->range[j].step_uV + 1;
1749 temp += set_points[i]->range[j].n_voltages;
1750 }
1751 set_points[i]->n_voltages = temp;
1752 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001753}
1754
1755static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1756{
David Collins6f032ba2011-08-31 14:08:15 -07001757 struct rpm_regulator_platform_data *platform_data;
David Collins379b4b72012-05-14 16:33:51 -07001758 static struct rpm_regulator_consumer_mapping *prev_consumer_map;
1759 static int prev_consumer_map_len;
David Collins6f032ba2011-08-31 14:08:15 -07001760 int rc = 0;
1761 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001762
David Collins6f032ba2011-08-31 14:08:15 -07001763 platform_data = pdev->dev.platform_data;
1764 if (!platform_data) {
1765 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001766 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001767 }
1768
David Collins6f032ba2011-08-31 14:08:15 -07001769 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1770 && platform_data->version != rpm_version) {
1771 pr_err("rpm version %d does not match previous version %d\n",
1772 platform_data->version, rpm_version);
1773 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001774 }
1775
David Collins6f032ba2011-08-31 14:08:15 -07001776 if (platform_data->version < 0
1777 || platform_data->version > RPM_VREG_VERSION_MAX) {
1778 pr_err("rpm version %d is invalid\n", platform_data->version);
1779 return -EINVAL;
1780 }
1781
1782 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1783 rpm_version = platform_data->version;
1784 config = get_config[platform_data->version]();
1785 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1786 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1787 if (!config) {
1788 pr_err("rpm version %d is not available\n",
1789 platform_data->version);
1790 return -ENODEV;
1791 }
1792 if (config->use_legacy_optimum_mode)
1793 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1794 vreg_ops[i]->get_optimum_mode
1795 = vreg_legacy_get_optimum_mode;
1796 rpm_vreg_set_point_init();
1797 /* First time probed; initialize pin control mutexes. */
1798 for (i = 0; i < config->vregs_len; i++)
1799 mutex_init(&config->vregs[i].pc_lock);
1800 }
1801
David Collins379b4b72012-05-14 16:33:51 -07001802 /* Copy the list of private API consumers. */
1803 if (platform_data->consumer_map_len > 0) {
1804 if (consumer_map_len == 0) {
1805 consumer_map_len = platform_data->consumer_map_len;
1806 consumer_map = kmemdup(platform_data->consumer_map,
1807 sizeof(struct rpm_regulator_consumer_mapping)
1808 * consumer_map_len, GFP_KERNEL);
1809 if (consumer_map == NULL) {
1810 pr_err("memory allocation failed\n");
1811 consumer_map_len = 0;
1812 return -ENOMEM;
1813 }
1814 } else {
1815 /* Concatenate new map with the existing one. */
1816 prev_consumer_map = consumer_map;
1817 prev_consumer_map_len = consumer_map_len;
1818 consumer_map_len += platform_data->consumer_map_len;
1819 consumer_map = kmalloc(
1820 sizeof(struct rpm_regulator_consumer_mapping)
1821 * consumer_map_len, GFP_KERNEL);
1822 if (consumer_map == NULL) {
1823 pr_err("memory allocation failed\n");
1824 consumer_map_len = 0;
1825 return -ENOMEM;
1826 }
1827 memcpy(consumer_map, prev_consumer_map,
1828 sizeof(struct rpm_regulator_consumer_mapping)
1829 * prev_consumer_map_len);
1830 memcpy(&consumer_map[prev_consumer_map_len],
1831 platform_data->consumer_map,
1832 sizeof(struct rpm_regulator_consumer_mapping)
1833 * platform_data->consumer_map_len);
1834 }
1835
1836 }
1837
David Collins109a8e62012-07-31 15:21:35 -07001838 if (platform_data->requires_tcxo_workaround
1839 && !requires_tcxo_workaround) {
1840 requires_tcxo_workaround = true;
1841 wake_lock_init(&tcxo_wake_lock, WAKE_LOCK_SUSPEND,
1842 "rpm_regulator_tcxo");
1843 }
1844
David Collins6f032ba2011-08-31 14:08:15 -07001845 /* Initialize all of the regulators listed in the platform data. */
1846 for (i = 0; i < platform_data->num_regulators; i++) {
1847 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1848 &pdev->dev);
1849 if (rc) {
1850 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1851 goto remove_regulators;
1852 }
1853 }
1854
1855 platform_set_drvdata(pdev, platform_data);
1856
1857 return rc;
1858
1859remove_regulators:
1860 /* Unregister all regulators added before the erroring one. */
1861 for (; i >= 0; i--) {
1862 id = platform_data->init_data[i].id;
1863 if (config->is_real_id(id)) {
1864 regulator_unregister(config->vregs[id].rdev);
1865 config->vregs[id].rdev = NULL;
1866 } else {
1867 regulator_unregister(config->vregs[
1868 config->pc_id_to_real_id(id)].rdev_pc);
1869 config->vregs[id].rdev_pc = NULL;
1870 }
1871 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001872
1873 return rc;
1874}
1875
1876static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1877{
David Collins6f032ba2011-08-31 14:08:15 -07001878 struct rpm_regulator_platform_data *platform_data;
1879 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880
David Collins6f032ba2011-08-31 14:08:15 -07001881 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001883
1884 if (platform_data) {
1885 for (i = 0; i < platform_data->num_regulators; i++) {
1886 id = platform_data->init_data[i].id;
1887 if (config->is_real_id(id)) {
1888 regulator_unregister(config->vregs[id].rdev);
1889 config->vregs[id].rdev = NULL;
1890 } else {
1891 regulator_unregister(config->vregs[
1892 config->pc_id_to_real_id(id)].rdev_pc);
1893 config->vregs[id].rdev_pc = NULL;
1894 }
1895 }
1896 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897
1898 return 0;
1899}
1900
1901static struct platform_driver rpm_vreg_driver = {
1902 .probe = rpm_vreg_probe,
1903 .remove = __devexit_p(rpm_vreg_remove),
1904 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001905 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001906 .owner = THIS_MODULE,
1907 },
1908};
1909
1910static int __init rpm_vreg_init(void)
1911{
1912 return platform_driver_register(&rpm_vreg_driver);
1913}
1914
1915static void __exit rpm_vreg_exit(void)
1916{
David Collins6f032ba2011-08-31 14:08:15 -07001917 int i;
1918
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001920
David Collins379b4b72012-05-14 16:33:51 -07001921 kfree(consumer_map);
1922
David Collins6f032ba2011-08-31 14:08:15 -07001923 for (i = 0; i < config->vregs_len; i++)
1924 mutex_destroy(&config->vregs[i].pc_lock);
David Collins109a8e62012-07-31 15:21:35 -07001925
1926 if (tcxo_handle)
1927 clk_put(tcxo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928}
1929
1930postcore_initcall(rpm_vreg_init);
1931module_exit(rpm_vreg_exit);
1932
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001933MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001934MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001935MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001936MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);