blob: 424a4fe2da8c3cf216bd8b05fea27b742dd922ee [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>
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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/spinlock.h>
David Collins379b4b72012-05-14 16:33:51 -070023#include <linux/string.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <linux/platform_device.h>
David Collins109a8e62012-07-31 15:21:35 -070025#include <linux/wakelock.h>
26#include <linux/workqueue.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/regulator/driver.h>
David Collins109a8e62012-07-31 15:21:35 -070028
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <mach/rpm.h>
30#include <mach/rpm-regulator.h>
David Collins379b4b72012-05-14 16:33:51 -070031#include <mach/rpm-regulator-smd.h>
David Collins6f032ba2011-08-31 14:08:15 -070032#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#include "rpm_resources.h"
David Collins6f032ba2011-08-31 14:08:15 -070035#include "rpm-regulator-private.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036
37/* Debug Definitions */
38
39enum {
40 MSM_RPM_VREG_DEBUG_REQUEST = BIT(0),
41 MSM_RPM_VREG_DEBUG_VOTE = BIT(1),
42 MSM_RPM_VREG_DEBUG_DUPLICATE = BIT(2),
David Collins6f032ba2011-08-31 14:08:15 -070043 MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG = BIT(3),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044};
45
46static int msm_rpm_vreg_debug_mask;
47module_param_named(
48 debug_mask, msm_rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR
49);
50
David Collins379b4b72012-05-14 16:33:51 -070051/* Used for access via the rpm_regulator_* API. */
52struct rpm_regulator {
53 int vreg_id;
54 enum rpm_vreg_voter voter;
55 int sleep_also;
56 int min_uV;
57 int max_uV;
58};
59
David Collins6f032ba2011-08-31 14:08:15 -070060struct vreg_config *(*get_config[])(void) = {
61 [RPM_VREG_VERSION_8660] = get_config_8660,
62 [RPM_VREG_VERSION_8960] = get_config_8960,
David Collins6ef12bf2011-08-31 14:08:15 -070063 [RPM_VREG_VERSION_9615] = get_config_9615,
David Collins5779cea2012-01-05 15:09:21 -080064 [RPM_VREG_VERSION_8930] = get_config_8930,
David Collins7afbd4c2012-08-13 12:31:04 -070065 [RPM_VREG_VERSION_8930_PM8917] = get_config_8930_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;
301static bool tcxo_workaround_noirq;
302static struct clk *tcxo_handle;
303static struct wake_lock tcxo_wake_lock;
304static DEFINE_MUTEX(tcxo_mutex);
305/* Spin lock needed for sleep-selectable regulators. */
306static DEFINE_SPINLOCK(tcxo_noirq_lock);
307static bool tcxo_is_enabled;
308/*
309 * TCXO must be kept on for at least the duration of its warmup (4 ms);
310 * otherwise, it will stay on when hardware disabling is attempted.
311 */
312#define TCXO_WARMUP_TIME_MS 4
313
314static void tcxo_get_handle(void)
315{
316 int rc;
317
318 if (!tcxo_handle) {
319 tcxo_handle = clk_get_sys("rpm-regulator", "vref_buff");
320 if (IS_ERR(tcxo_handle)) {
321 tcxo_handle = NULL;
322 } else {
323 rc = clk_prepare(tcxo_handle);
324 if (rc) {
325 clk_put(tcxo_handle);
326 tcxo_handle = NULL;
327 }
328 }
329 }
330}
331
332/*
333 * Perform best effort enable of CXO. Since the MSM clock drivers depend upon
334 * the rpm-regulator driver, any rpm-regulator devices that are configured with
335 * always_on == 1 will not be able to enable CXO during probe. This does not
336 * cause a problem though since CXO will be enabled by the boot loaders before
337 * Apps boots up.
338 */
339static bool tcxo_enable(void)
340{
341 int rc;
342
343 if (tcxo_handle && !tcxo_is_enabled) {
344 rc = clk_enable(tcxo_handle);
345 if (!rc) {
346 tcxo_is_enabled = true;
347 wake_lock(&tcxo_wake_lock);
348 return true;
349 }
350 }
351
352 return false;
353}
354
355static void tcxo_delayed_disable_work(struct work_struct *work)
356{
357 unsigned long flags = 0;
358
359 if (tcxo_workaround_noirq)
360 spin_lock_irqsave(&tcxo_noirq_lock, flags);
361 else
362 mutex_lock(&tcxo_mutex);
363
364 clk_disable(tcxo_handle);
365 tcxo_is_enabled = false;
366 wake_unlock(&tcxo_wake_lock);
367
368 if (tcxo_workaround_noirq)
369 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
370 else
371 mutex_unlock(&tcxo_mutex);
372}
373
374static DECLARE_DELAYED_WORK(tcxo_disable_work, tcxo_delayed_disable_work);
375
376static void tcxo_delayed_disable(void)
377{
378 /*
379 * The delay in jiffies has 1 added to it to ensure that at least
380 * one jiffy takes place before the work is enqueued. Without this,
381 * the work would be scheduled to run in the very next jiffy which could
382 * result in too little delay and TCXO being stuck on.
383 */
384 if (tcxo_handle)
385 schedule_delayed_work(&tcxo_disable_work,
386 msecs_to_jiffies(TCXO_WARMUP_TIME_MS) + 1);
387}
388
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700390static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391
392static int voltage_from_req(struct vreg *vreg)
393{
David Collins6f032ba2011-08-31 14:08:15 -0700394 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
David Collins6f032ba2011-08-31 14:08:15 -0700396 if (vreg->part->uV.mask)
397 uV = GET_PART(vreg, uV);
David Collins13397f22012-02-06 13:53:29 -0800398 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700399 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
David Collins13397f22012-02-06 13:53:29 -0800400 else if (vreg->part->enable_state.mask)
401 uV = GET_PART(vreg, enable_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402
David Collins6f032ba2011-08-31 14:08:15 -0700403 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404}
405
David Collins6f032ba2011-08-31 14:08:15 -0700406static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407{
David Collins6f032ba2011-08-31 14:08:15 -0700408 if (vreg->part->uV.mask)
409 SET_PART(vreg, uV, uV);
David Collins13397f22012-02-06 13:53:29 -0800410 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700411 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
David Collins13397f22012-02-06 13:53:29 -0800412 else if (vreg->part->enable_state.mask)
413 SET_PART(vreg, enable_state, uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414}
415
416static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
417 int set, unsigned mask0, unsigned val0,
418 unsigned mask1, unsigned val1, unsigned cnt,
419 int update_voltage)
420{
421 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700422 int rc = 0, max_uV_vote = 0;
David Collins109a8e62012-07-31 15:21:35 -0700423 unsigned long flags = 0;
424 bool tcxo_enabled = false;
425 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700427 int *min_uV_vote;
428 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429
430 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700431 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 prev_req = vreg->prev_active_req;
433 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700434 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 prev_req = vreg->prev_sleep_req;
436 }
437
438 prev0 = vreg->req[0].value;
439 vreg->req[0].value &= ~mask0;
440 vreg->req[0].value |= val0 & mask0;
441
442 prev1 = vreg->req[1].value;
443 vreg->req[1].value &= ~mask1;
444 vreg->req[1].value |= val1 & mask1;
445
David Collinsd1b7aea2012-03-19 10:27:55 -0700446 /* Set the force mode field based on which set is being requested. */
447 if (set == MSM_RPM_CTX_SET_0)
448 SET_PART(vreg, fm, vreg->pdata.force_mode);
449 else
450 SET_PART(vreg, fm, vreg->pdata.sleep_set_force_mode);
451
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700453 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454
455 /* Find the highest voltage voted for and use it. */
456 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700457 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
458 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459
460 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700461 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
462 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463
464 /* Ignore duplicate requests */
465 if (vreg->req[0].value != prev_req[0].value ||
466 vreg->req[1].value != prev_req[1].value) {
David Collins109a8e62012-07-31 15:21:35 -0700467
468 /* Enable CXO clock if necessary for TCXO workaround. */
469 if (requires_tcxo_workaround && vreg->requires_cxo
470 && (set == MSM_RPM_CTX_SET_0)
471 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
472 voltage_increased = true;
473 spin_lock_irqsave(&tcxo_noirq_lock, flags);
474 tcxo_enabled = tcxo_enable();
475 }
476
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
478 if (rc) {
479 vreg->req[0].value = prev0;
480 vreg->req[1].value = prev1;
481
David Collins6f032ba2011-08-31 14:08:15 -0700482 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
483 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
485 vreg->req[0].id, rc);
486 } else {
487 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700488 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
489 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 if (msm_rpm_vreg_debug_mask
491 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700492 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 prev_req[0].value = vreg->req[0].value;
494 prev_req[1].value = vreg->req[1].value;
495 }
David Collins109a8e62012-07-31 15:21:35 -0700496
497 /*
498 * Schedule CXO clock to be disabled after TCXO warmup time if
499 * TCXO workaround is applicable for this regulator.
500 */
501 if (voltage_increased) {
502 if (tcxo_enabled)
503 tcxo_delayed_disable();
504 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
505 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700507 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 }
509
510 return rc;
511}
512
513static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
514 int sleep, unsigned mask0, unsigned val0,
515 unsigned mask1, unsigned val1, unsigned cnt,
516 int update_voltage)
517{
David Collins6f032ba2011-08-31 14:08:15 -0700518 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 unsigned long flags;
520 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521
522 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
523 return -EINVAL;
524
David Collins6f032ba2011-08-31 14:08:15 -0700525 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526
527 /*
528 * Send sleep set request first so that subsequent set_mode, etc calls
529 * use the voltage from the active set.
530 */
531 if (sleep)
532 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
533 mask0, val0, mask1, val1, cnt, update_voltage);
534 else {
535 /*
536 * Vote for 0 V in the sleep set when active set-only is
537 * specified. This ensures that a disable vote will be issued
538 * at some point for the sleep set of the regulator.
539 */
David Collins6f032ba2011-08-31 14:08:15 -0700540 if (vreg->part->uV.mask) {
541 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
542 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800543 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700544 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
545 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800546 } else if (vreg->part->enable_state.mask) {
547 s_val[vreg->part->enable_state.word]
548 = 0 << vreg->part->enable_state.shift;
549 s_mask[vreg->part->enable_state.word]
550 = vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 }
552
553 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700554 s_mask[0], s_val[0], s_mask[1], s_val[1],
555 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 }
557
558 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
559 mask1, val1, cnt, update_voltage);
560
David Collins6f032ba2011-08-31 14:08:15 -0700561 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562
563 return rc;
564}
565
566/**
567 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
568 * @vreg: ID for regulator
569 * @voter: ID for the voter
570 * @min_uV: minimum acceptable voltage (in uV) that is voted for
571 * @max_uV: maximum acceptable voltage (in uV) that is voted for
572 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
573 *
574 * Returns 0 on success or errno.
575 *
576 * This function is used to vote for the voltage of a regulator without
577 * using the regulator framework. It is needed by consumers which hold spin
578 * locks or have interrupts disabled because the regulator framework can sleep.
579 * It is also needed by consumers which wish to only vote for active set
580 * regulator voltage.
581 *
582 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
583 *
584 * This function may only be called for regulators which have the sleep flag
585 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700586 *
587 * Consumers can vote to disable a regulator with this function by passing
588 * min_uV = 0 and max_uV = 0.
David Collins13397f22012-02-06 13:53:29 -0800589 *
590 * Voltage switch type regulators may be controlled via rpm_vreg_set_voltage
591 * as well. For this type of regulator, max_uV > 0 is treated as an enable
592 * request and max_uV == 0 is treated as a disable request.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 */
David Collins6f032ba2011-08-31 14:08:15 -0700594int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
595 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700596{
David Collins6f032ba2011-08-31 14:08:15 -0700597 unsigned int mask[2] = {0}, val[2] = {0};
598 struct vreg_range *range;
599 struct vreg *vreg;
600 int uV = min_uV;
601 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
David Collins6f032ba2011-08-31 14:08:15 -0700603 if (!config) {
604 pr_err("rpm-regulator driver has not probed yet.\n");
605 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 }
607
David Collins6f032ba2011-08-31 14:08:15 -0700608 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
609 pr_err("invalid regulator id=%d\n", vreg_id);
610 return -EINVAL;
611 }
612
613 vreg = &config->vregs[vreg_id];
David Collins6f032ba2011-08-31 14:08:15 -0700614
615 if (!vreg->pdata.sleep_selectable) {
616 vreg_err(vreg, "regulator is not marked sleep selectable\n");
617 return -EINVAL;
618 }
619
David Collins7462b9d2011-10-11 16:02:17 -0700620 /* Allow min_uV == max_uV == 0 to represent a disable request. */
David Collins13397f22012-02-06 13:53:29 -0800621 if ((min_uV != 0 || max_uV != 0)
622 && (vreg->part->uV.mask || vreg->part->mV.mask)) {
David Collins7462b9d2011-10-11 16:02:17 -0700623 /*
624 * Check if request voltage is outside of allowed range. The
625 * regulator core has already checked that constraint range
626 * is inside of the physically allowed range.
627 */
628 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
629 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700630
David Collins7462b9d2011-10-11 16:02:17 -0700631 if (uV < lim_min_uV && max_uV >= lim_min_uV)
632 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700633
David Collins7462b9d2011-10-11 16:02:17 -0700634 if (uV < lim_min_uV || uV > lim_max_uV) {
635 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
636 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
637 lim_max_uV);
638 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700639 }
David Collins6f032ba2011-08-31 14:08:15 -0700640
David Collins13397f22012-02-06 13:53:29 -0800641 range = &vreg->set_points->range[0];
David Collins7462b9d2011-10-11 16:02:17 -0700642 /* Find the range which uV is inside of. */
643 for (i = vreg->set_points->count - 1; i > 0; i--) {
644 if (uV > vreg->set_points->range[i - 1].max_uV) {
645 range = &vreg->set_points->range[i];
646 break;
647 }
648 }
649
650 /*
651 * Force uV to be an allowed set point and apply a ceiling
652 * function to non-set point values.
653 */
654 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
655 uV = uV * range->step_uV + range->min_uV;
David Collins3974b612011-11-21 15:07:36 -0800656
657 if (uV > max_uV) {
658 vreg_err(vreg,
659 "request v=[%d, %d] cannot be met by any set point; "
660 "next set point: %d\n",
661 min_uV, max_uV, uV);
662 return -EINVAL;
663 }
David Collins7462b9d2011-10-11 16:02:17 -0700664 }
David Collins6f032ba2011-08-31 14:08:15 -0700665
David Collins0ac31fe2012-02-08 13:53:34 -0800666 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
667 /*
668 * Translate from enum values which work as inputs in the
669 * rpm_vreg_set_voltage function to the actual corner values
670 * sent to the RPM.
671 */
672 if (uV > 0)
673 uV -= RPM_VREG_CORNER_NONE;
674 }
675
David Collins6f032ba2011-08-31 14:08:15 -0700676 if (vreg->part->uV.mask) {
677 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
678 mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800679 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700680 val[vreg->part->mV.word]
681 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
682 mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800683 } else if (vreg->part->enable_state.mask) {
684 /*
685 * Translate max_uV > 0 into an enable request for regulator
686 * types which to not support voltage setting, e.g. voltage
687 * switches.
688 */
689 val[vreg->part->enable_state.word]
690 = (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift;
691 mask[vreg->part->enable_state.word]
692 = vreg->part->enable_state.mask;
David Collins6f032ba2011-08-31 14:08:15 -0700693 }
694
695 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
696 val[1], vreg->part->request_len, 1);
697 if (rc)
698 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699
700 return rc;
701}
702EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
703
704/**
705 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
706 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700707 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 *
709 * Returns 0 on success or errno.
710 */
David Collins6f032ba2011-08-31 14:08:15 -0700711int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712{
David Collins6f032ba2011-08-31 14:08:15 -0700713 unsigned int mask[2] = {0}, val[2] = {0};
714 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 int rc;
716
David Collins6f032ba2011-08-31 14:08:15 -0700717 if (!config) {
718 pr_err("rpm-regulator driver has not probed yet.\n");
719 return -ENODEV;
720 }
721
722 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
723 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 return -EINVAL;
725 }
726
David Collins6f032ba2011-08-31 14:08:15 -0700727 vreg = &config->vregs[vreg_id];
728
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700730 vreg_err(vreg, "invalid frequency=%d\n", freq);
731 return -EINVAL;
732 }
733 if (!vreg->pdata.sleep_selectable) {
734 vreg_err(vreg, "regulator is not marked sleep selectable\n");
735 return -EINVAL;
736 }
737 if (!vreg->part->freq.mask) {
738 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 return -EINVAL;
740 }
741
David Collins6f032ba2011-08-31 14:08:15 -0700742 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
743 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744
David Collins6f032ba2011-08-31 14:08:15 -0700745 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
746 val[0], mask[1], val[1], vreg->part->request_len, 0);
747 if (rc)
748 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749
750 return rc;
751}
752EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
753
David Collins379b4b72012-05-14 16:33:51 -0700754#define MAX_NAME_LEN 64
755/**
756 * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
757 * @dev: device for regulator consumer
758 * @supply: supply name
759 *
760 * Returns a struct rpm_regulator corresponding to the regulator producer,
761 * or ERR_PTR() containing errno.
762 *
763 * This function may only be called from nonatomic context. The mapping between
764 * <dev, supply> tuples and rpm_regulators struct pointers is specified via
765 * rpm-regulator platform data.
766 */
767struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
768{
769 struct rpm_regulator_consumer_mapping *mapping = NULL;
770 const char *devname = NULL;
771 struct rpm_regulator *regulator;
772 int i;
773
774 if (!config) {
775 pr_err("rpm-regulator driver has not probed yet.\n");
776 return ERR_PTR(-ENODEV);
777 }
778
779 if (consumer_map == NULL || consumer_map_len == 0) {
780 pr_err("No private consumer mapping has been specified.\n");
781 return ERR_PTR(-ENODEV);
782 }
783
784 if (supply == NULL) {
785 pr_err("supply name must be specified\n");
786 return ERR_PTR(-EINVAL);
787 }
788
789 if (dev)
790 devname = dev_name(dev);
791
792 for (i = 0; i < consumer_map_len; i++) {
793 /* If the mapping has a device set up it must match */
794 if (consumer_map[i].dev_name &&
795 (!devname || strncmp(consumer_map[i].dev_name, devname,
796 MAX_NAME_LEN)))
797 continue;
798
799 if (strncmp(consumer_map[i].supply, supply, MAX_NAME_LEN)
800 == 0) {
801 mapping = &consumer_map[i];
802 break;
803 }
804 }
805
806 if (mapping == NULL) {
807 pr_err("could not find mapping for dev=%s, supply=%s\n",
808 (devname ? devname : "(null)"), supply);
809 return ERR_PTR(-ENODEV);
810 }
811
812 regulator = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
813 if (regulator == NULL) {
814 pr_err("could not allocate memory for regulator\n");
815 return ERR_PTR(-ENOMEM);
816 }
817
818 regulator->vreg_id = mapping->vreg_id;
819 regulator->voter = mapping->voter;
820 regulator->sleep_also = mapping->sleep_also;
821
822 return regulator;
823}
824EXPORT_SYMBOL_GPL(rpm_regulator_get);
825
826static int rpm_regulator_check_input(struct rpm_regulator *regulator)
827{
828 int rc = 0;
829
830 if (regulator == NULL) {
831 rc = -EINVAL;
832 pr_err("invalid (null) rpm_regulator pointer\n");
833 } else if (IS_ERR(regulator)) {
834 rc = PTR_ERR(regulator);
835 pr_err("invalid rpm_regulator pointer, rc=%d\n", rc);
836 }
837
838 return rc;
839}
840
841/**
842 * rpm_regulator_put() - free the RPM regulator handle
843 * @regulator: RPM regulator handle
844 *
845 * Parameter reaggregation does not take place when rpm_regulator_put is called.
846 * Therefore, regulator enable state and voltage must be configured
847 * appropriately before calling rpm_regulator_put.
848 *
849 * This function may be called from either atomic or nonatomic context.
850 */
851void rpm_regulator_put(struct rpm_regulator *regulator)
852{
853 kfree(regulator);
854}
855EXPORT_SYMBOL_GPL(rpm_regulator_put);
856
857/**
858 * rpm_regulator_enable() - enable regulator output
859 * @regulator: RPM regulator handle
860 *
861 * Returns 0 on success or errno on failure.
862 *
863 * This function may be called from either atomic or nonatomic context. This
864 * function may only be called for regulators which have the sleep_selectable
865 * flag set in their configuration data.
866 *
867 * rpm_regulator_set_voltage must be called before rpm_regulator_enable because
868 * enabling is defined by the RPM interface to be requesting the desired
869 * non-zero regulator output voltage.
870 */
871int rpm_regulator_enable(struct rpm_regulator *regulator)
872{
873 int rc = rpm_regulator_check_input(regulator);
874 struct vreg *vreg;
875
876 if (rc)
877 return rc;
878
879 if (regulator->vreg_id < config->vreg_id_min
880 || regulator->vreg_id > config->vreg_id_max) {
881 pr_err("invalid regulator id=%d\n", regulator->vreg_id);
882 return -EINVAL;
883 }
884
885 vreg = &config->vregs[regulator->vreg_id];
886
887 /*
888 * Handle voltage switches which can be enabled without
889 * rpm_regulator_set_voltage ever being called.
890 */
891 if (regulator->min_uV == 0 && regulator->max_uV == 0
892 && vreg->part->uV.mask == 0 && vreg->part->mV.mask == 0) {
893 regulator->min_uV = 1;
894 regulator->max_uV = 1;
895 }
896
897 if (regulator->min_uV == 0 && regulator->max_uV == 0) {
898 pr_err("Voltage must be set with rpm_regulator_set_voltage "
899 "before calling rpm_regulator_enable; vreg_id=%d, "
900 "voter=%d\n", regulator->vreg_id, regulator->voter);
901 return -EINVAL;
902 }
903
904 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter,
905 regulator->min_uV, regulator->max_uV, regulator->sleep_also);
906
907 if (rc)
908 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
909
910 return rc;
911}
912EXPORT_SYMBOL_GPL(rpm_regulator_enable);
913
914/**
915 * rpm_regulator_disable() - disable regulator output
916 * @regulator: RPM regulator handle
917 *
918 * Returns 0 on success or errno on failure.
919 *
920 * The enable state of the regulator is determined by aggregating the requests
921 * of all consumers. Therefore, it is possible that the regulator will remain
922 * enabled even after rpm_regulator_disable is called.
923 *
924 * This function may be called from either atomic or nonatomic context. This
925 * function may only be called for regulators which have the sleep_selectable
926 * flag set in their configuration data.
927 */
928int rpm_regulator_disable(struct rpm_regulator *regulator)
929{
930 int rc = rpm_regulator_check_input(regulator);
931
932 if (rc)
933 return rc;
934
935 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, 0, 0,
936 regulator->sleep_also);
937
938 if (rc)
939 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
940
941 return rc;
942}
943EXPORT_SYMBOL_GPL(rpm_regulator_disable);
944
945/**
946 * rpm_regulator_set_voltage() - set regulator output voltage
947 * @regulator: RPM regulator handle
948 * @min_uV: minimum required voltage in uV
949 * @max_uV: maximum acceptable voltage in uV
950 *
951 * Sets a voltage regulator to the desired output voltage. This can be set
952 * while the regulator is disabled or enabled. If the regulator is disabled,
953 * then rpm_regulator_set_voltage will both enable the regulator and set it to
954 * output at the requested voltage.
955 *
956 * The min_uV to max_uV voltage range requested must intersect with the
957 * voltage constraint range configured for the regulator.
958 *
959 * Returns 0 on success or errno on failure.
960 *
961 * The final voltage value that is sent to the RPM is aggregated based upon the
962 * values requested by all consumers of the regulator. This corresponds to the
963 * maximum min_uV value.
964 *
965 * This function may be called from either atomic or nonatomic context. This
966 * function may only be called for regulators which have the sleep_selectable
967 * flag set in their configuration data.
968 */
969int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
970 int max_uV)
971{
972 int rc = rpm_regulator_check_input(regulator);
973
974 if (rc)
975 return rc;
976
977 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, min_uV,
978 max_uV, regulator->sleep_also);
979
980 if (rc) {
981 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
982 } else {
983 regulator->min_uV = min_uV;
984 regulator->max_uV = max_uV;
985 }
986
987 return rc;
988}
989EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
990
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991static inline int vreg_hpm_min_uA(struct vreg *vreg)
992{
993 return vreg->hpm_min_load;
994}
995
996static inline int vreg_lpm_max_uA(struct vreg *vreg)
997{
998 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
999}
1000
David Collins6f032ba2011-08-31 14:08:15 -07001001static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002{
David Collins6f032ba2011-08-31 14:08:15 -07001003 unsigned load_max
1004 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
1005
1006 return (load_uA > load_max ? load_max : load_uA);
1007}
1008
1009static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
1010{
1011 unsigned load_max
1012 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
1013 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014}
1015
1016/* Change vreg->req, but do not send it to the RPM. */
1017static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
1018 unsigned mask1, unsigned val1)
1019{
1020 unsigned long flags = 0;
1021
David Collins6f032ba2011-08-31 14:08:15 -07001022 if (vreg->pdata.sleep_selectable)
1023 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024
1025 vreg->req[0].value &= ~mask0;
1026 vreg->req[0].value |= val0 & mask0;
1027
1028 vreg->req[1].value &= ~mask1;
1029 vreg->req[1].value |= val1 & mask1;
1030
David Collins6f032ba2011-08-31 14:08:15 -07001031 if (vreg->pdata.sleep_selectable)
1032 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033
1034 return 0;
1035}
1036
1037static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
1038 unsigned mask1, unsigned val1, unsigned cnt)
1039{
1040 unsigned prev0 = 0, prev1 = 0;
David Collins109a8e62012-07-31 15:21:35 -07001041 unsigned long flags = 0;
1042 bool tcxo_enabled = false;
1043 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 int rc;
1045
1046 /*
1047 * Bypass the normal route for regulators that can be called to change
1048 * just the active set values.
1049 */
David Collins6f032ba2011-08-31 14:08:15 -07001050 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
1052 mask0, val0, mask1, val1, cnt, 1);
1053
1054 prev0 = vreg->req[0].value;
1055 vreg->req[0].value &= ~mask0;
1056 vreg->req[0].value |= val0 & mask0;
1057
1058 prev1 = vreg->req[1].value;
1059 vreg->req[1].value &= ~mask1;
1060 vreg->req[1].value |= val1 & mask1;
1061
1062 /* Ignore duplicate requests */
1063 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
1064 vreg->req[1].value == vreg->prev_active_req[1].value) {
1065 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -07001066 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067 return 0;
1068 }
1069
David Collins109a8e62012-07-31 15:21:35 -07001070 /* Enable CXO clock if necessary for TCXO workaround. */
1071 if (requires_tcxo_workaround && vreg->requires_cxo
1072 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
1073 if (!tcxo_handle)
1074 tcxo_get_handle();
1075 if (tcxo_workaround_noirq)
1076 spin_lock_irqsave(&tcxo_noirq_lock, flags);
1077 else
1078 mutex_lock(&tcxo_mutex);
1079
1080 voltage_increased = true;
1081 tcxo_enabled = tcxo_enable();
1082 }
1083
1084 if (voltage_increased && tcxo_workaround_noirq)
1085 rc = msm_rpmrs_set_noirq(MSM_RPM_CTX_SET_0, vreg->req, cnt);
1086 else
1087 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
1088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 if (rc) {
1090 vreg->req[0].value = prev0;
1091 vreg->req[1].value = prev1;
1092
David Collins6f032ba2011-08-31 14:08:15 -07001093 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
1094 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095 } else {
1096 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -07001097 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 vreg->prev_active_req[0].value = vreg->req[0].value;
1099 vreg->prev_active_req[1].value = vreg->req[1].value;
1100 }
1101
David Collins109a8e62012-07-31 15:21:35 -07001102 /*
1103 * Schedule CXO clock to be disabled after TCXO warmup time if TCXO
1104 * workaround is applicable for this regulator.
1105 */
1106 if (voltage_increased) {
1107 if (tcxo_enabled)
1108 tcxo_delayed_disable();
1109
1110 if (tcxo_workaround_noirq)
1111 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
1112 else
1113 mutex_unlock(&tcxo_mutex);
1114 }
1115
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116 return rc;
1117}
1118
David Collins6f032ba2011-08-31 14:08:15 -07001119static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120{
David Collins6f032ba2011-08-31 14:08:15 -07001121 struct vreg *vreg = rdev_get_drvdata(rdev);
1122 int enabled;
1123
1124 mutex_lock(&vreg->pc_lock);
1125 enabled = vreg->is_enabled;
1126 mutex_unlock(&vreg->pc_lock);
1127
1128 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129}
1130
David Collins6f032ba2011-08-31 14:08:15 -07001131static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132{
David Collins6f032ba2011-08-31 14:08:15 -07001133 switch (vreg->type) {
1134 case RPM_REGULATOR_TYPE_LDO:
1135 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001136 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001137 /* Enable by setting a voltage. */
1138 if (vreg->part->uV.mask) {
1139 val[vreg->part->uV.word]
1140 |= vreg->save_uV << vreg->part->uV.shift;
1141 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1142 } else {
1143 val[vreg->part->mV.word]
1144 |= MICRO_TO_MILLI(vreg->save_uV)
1145 << vreg->part->mV.shift;
1146 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1147 }
1148 break;
1149 case RPM_REGULATOR_TYPE_VS:
1150 case RPM_REGULATOR_TYPE_NCP:
1151 /* Enable by setting enable_state. */
1152 val[vreg->part->enable_state.word]
1153 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
1154 mask[vreg->part->enable_state.word]
1155 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157}
1158
David Collins0ac31fe2012-02-08 13:53:34 -08001159static int rpm_vreg_enable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001160{
1161 struct vreg *vreg = rdev_get_drvdata(rdev);
1162 unsigned int mask[2] = {0}, val[2] = {0};
1163 int rc = 0;
1164
1165 set_enable(vreg, mask, val);
1166
1167 mutex_lock(&vreg->pc_lock);
1168
1169 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1170 vreg->part->request_len);
1171 if (!rc)
1172 vreg->is_enabled = true;
1173
1174 mutex_unlock(&vreg->pc_lock);
1175
1176 if (rc)
1177 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1178
1179 return rc;
1180}
1181
1182static void set_disable(struct vreg *vreg, unsigned int *mask,
1183 unsigned int *val)
1184{
1185 switch (vreg->type) {
1186 case RPM_REGULATOR_TYPE_LDO:
1187 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001188 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001189 /* Disable by setting a voltage of 0 uV. */
1190 if (vreg->part->uV.mask) {
1191 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
1192 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1193 } else {
1194 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
1195 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1196 }
1197 break;
1198 case RPM_REGULATOR_TYPE_VS:
1199 case RPM_REGULATOR_TYPE_NCP:
1200 /* Disable by setting enable_state. */
1201 val[vreg->part->enable_state.word]
1202 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
1203 mask[vreg->part->enable_state.word]
1204 |= vreg->part->enable_state.mask;
1205 }
1206}
1207
David Collins0ac31fe2012-02-08 13:53:34 -08001208static int rpm_vreg_disable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001209{
1210 struct vreg *vreg = rdev_get_drvdata(rdev);
1211 unsigned int mask[2] = {0}, val[2] = {0};
1212 int rc = 0;
1213
1214 set_disable(vreg, mask, val);
1215
1216 mutex_lock(&vreg->pc_lock);
1217
1218 /* Only disable if pin control is not in use. */
1219 if (!vreg->is_enabled_pc)
1220 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1221 vreg->part->request_len);
1222
1223 if (!rc)
1224 vreg->is_enabled = false;
1225
1226 mutex_unlock(&vreg->pc_lock);
1227
1228 if (rc)
1229 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1230
1231 return rc;
1232}
1233
1234static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 unsigned *selector)
1236{
David Collins6f032ba2011-08-31 14:08:15 -07001237 struct vreg *vreg = rdev_get_drvdata(rdev);
1238 struct vreg_range *range = &vreg->set_points->range[0];
1239 unsigned int mask[2] = {0}, val[2] = {0};
1240 int rc = 0, uV = min_uV;
1241 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001242
David Collins6f032ba2011-08-31 14:08:15 -07001243 /* Check if request voltage is outside of physically settable range. */
1244 lim_min_uV = vreg->set_points->range[0].min_uV;
1245 lim_max_uV =
1246 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247
David Collins6f032ba2011-08-31 14:08:15 -07001248 if (uV < lim_min_uV && max_uV >= lim_min_uV)
1249 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001250
David Collins6f032ba2011-08-31 14:08:15 -07001251 if (uV < lim_min_uV || uV > lim_max_uV) {
1252 vreg_err(vreg,
1253 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1254 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 return -EINVAL;
1256 }
1257
David Collins6f032ba2011-08-31 14:08:15 -07001258 /* Find the range which uV is inside of. */
1259 for (i = vreg->set_points->count - 1; i > 0; i--) {
1260 if (uV > vreg->set_points->range[i - 1].max_uV) {
1261 range = &vreg->set_points->range[i];
1262 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 }
1264 }
1265
David Collins6f032ba2011-08-31 14:08:15 -07001266 /*
1267 * Force uV to be an allowed set point and apply a ceiling function
1268 * to non-set point values.
1269 */
1270 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
1271 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272
David Collins3974b612011-11-21 15:07:36 -08001273 if (uV > max_uV) {
1274 vreg_err(vreg,
1275 "request v=[%d, %d] cannot be met by any set point; "
1276 "next set point: %d\n",
1277 min_uV, max_uV, uV);
1278 return -EINVAL;
1279 }
1280
David Collins0ac31fe2012-02-08 13:53:34 -08001281 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
1282 /*
1283 * Translate from enum values which work as inputs in the
1284 * regulator_set_voltage function to the actual corner values
1285 * sent to the RPM.
1286 */
1287 uV -= RPM_VREG_CORNER_NONE;
1288 }
1289
David Collins6f032ba2011-08-31 14:08:15 -07001290 if (vreg->part->uV.mask) {
1291 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
1292 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 } else {
David Collins6f032ba2011-08-31 14:08:15 -07001294 val[vreg->part->mV.word]
1295 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
1296 mask[vreg->part->mV.word] = vreg->part->mV.mask;
1297 }
1298
1299 mutex_lock(&vreg->pc_lock);
1300
1301 /*
1302 * Only send a request for a new voltage if the regulator is currently
1303 * enabled. This will ensure that LDO and SMPS regulators are not
1304 * inadvertently turned on because voltage > 0 is equivalent to
1305 * enabling. For NCP, this just removes unnecessary RPM requests.
1306 */
1307 if (vreg->is_enabled) {
1308 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1309 vreg->part->request_len);
1310 if (rc)
1311 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1312 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001314 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315 }
1316
David Collins6f032ba2011-08-31 14:08:15 -07001317 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
1318 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001319
David Collins6f032ba2011-08-31 14:08:15 -07001320 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321
1322 return rc;
1323}
1324
David Collins6f032ba2011-08-31 14:08:15 -07001325static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326{
David Collins6f032ba2011-08-31 14:08:15 -07001327 struct vreg *vreg = rdev_get_drvdata(rdev);
1328
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 return vreg->save_uV;
1330}
1331
David Collins6f032ba2011-08-31 14:08:15 -07001332static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333{
David Collins6f032ba2011-08-31 14:08:15 -07001334 struct vreg *vreg = rdev_get_drvdata(rdev);
1335 int uV = 0;
1336 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337
David Collins6f032ba2011-08-31 14:08:15 -07001338 if (!vreg->set_points) {
1339 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001340 return -EINVAL;
1341 }
1342
David Collins6f032ba2011-08-31 14:08:15 -07001343 if (selector >= vreg->set_points->n_voltages)
1344 return 0;
1345
1346 for (i = 0; i < vreg->set_points->count; i++) {
1347 if (selector < vreg->set_points->range[i].n_voltages) {
1348 uV = selector * vreg->set_points->range[i].step_uV
1349 + vreg->set_points->range[i].min_uV;
1350 break;
1351 } else {
1352 selector -= vreg->set_points->range[i].n_voltages;
1353 }
1354 }
1355
1356 return uV;
1357}
1358
1359static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
1360{
1361 struct vreg *vreg = rdev_get_drvdata(rdev);
1362 unsigned int mask[2] = {0}, val[2] = {0};
1363 int rc = 0;
1364 int peak_uA;
1365
1366 mutex_lock(&vreg->pc_lock);
1367
1368 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
1369 & vreg->part->ip.mask) >> vreg->part->ip.shift);
1370
1371 if (mode == config->mode_hpm) {
1372 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001373 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001374 val[vreg->part->ip.word]
1375 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1376 << vreg->part->ip.shift;
1377 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1378
1379 if (config->ia_follows_ip) {
1380 val[vreg->part->ia.word]
1381 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1382 << vreg->part->ia.shift;
1383 mask[vreg->part->ia.word]
1384 |= vreg->part->ia.mask;
1385 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 }
David Collins6f032ba2011-08-31 14:08:15 -07001387 } else if (mode == config->mode_lpm) {
1388 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001390 val[vreg->part->ip.word]
1391 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1392 << vreg->part->ip.shift;
1393 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1394
1395 if (config->ia_follows_ip) {
1396 val[vreg->part->ia.word]
1397 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1398 << vreg->part->ia.shift;
1399 mask[vreg->part->ia.word]
1400 |= vreg->part->ia.mask;
1401 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 }
David Collins6f032ba2011-08-31 14:08:15 -07001403 } else {
1404 vreg_err(vreg, "invalid mode: %u\n", mode);
1405 mutex_unlock(&vreg->pc_lock);
1406 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407 }
1408
David Collins6f032ba2011-08-31 14:08:15 -07001409 if (vreg->is_enabled) {
1410 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1411 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412 } else {
1413 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001414 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415 }
David Collins6f032ba2011-08-31 14:08:15 -07001416
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001418 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1419 else
1420 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421
David Collins6f032ba2011-08-31 14:08:15 -07001422 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423
David Collins6f032ba2011-08-31 14:08:15 -07001424 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001425}
1426
David Collins6f032ba2011-08-31 14:08:15 -07001427static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428{
David Collins6f032ba2011-08-31 14:08:15 -07001429 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430
David Collins6f032ba2011-08-31 14:08:15 -07001431 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432}
1433
David Collins6f032ba2011-08-31 14:08:15 -07001434static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
1435 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436{
David Collins6f032ba2011-08-31 14:08:15 -07001437 struct vreg *vreg = rdev_get_drvdata(rdev);
1438 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001439
David Collins6f032ba2011-08-31 14:08:15 -07001440 load_uA += vreg->pdata.system_uA;
1441
1442 mutex_lock(&vreg->pc_lock);
1443 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
1444 if (config->ia_follows_ip)
1445 SET_PART(vreg, ia,
1446 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
1447 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448
1449 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -07001450 mode = config->mode_hpm;
1451 else
1452 mode = config->mode_lpm;
1453
1454 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455}
1456
David Collins6f032ba2011-08-31 14:08:15 -07001457static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
1458 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001459{
David Collins6f032ba2011-08-31 14:08:15 -07001460 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001461
David Collins6f032ba2011-08-31 14:08:15 -07001462 if (MICRO_TO_MILLI(load_uA) <= 0) {
1463 /*
1464 * vreg_legacy_get_optimum_mode is being called before consumers
1465 * have specified their load currents via
1466 * regulator_set_optimum_mode. Return whatever the existing mode
1467 * is.
1468 */
1469 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001470 }
1471
David Collins6f032ba2011-08-31 14:08:15 -07001472 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001473}
1474
1475/*
David Collins6f032ba2011-08-31 14:08:15 -07001476 * Returns the logical pin control enable state because the pin control options
1477 * present in the hardware out of restart could be different from those desired
1478 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001479 */
David Collins6f032ba2011-08-31 14:08:15 -07001480static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481{
David Collins6f032ba2011-08-31 14:08:15 -07001482 struct vreg *vreg = rdev_get_drvdata(rdev);
1483
1484 return vreg->is_enabled_pc;
1485}
1486
1487static int vreg_pin_control_enable(struct regulator_dev *rdev)
1488{
1489 struct vreg *vreg = rdev_get_drvdata(rdev);
1490 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491 int rc;
1492
David Collins6f032ba2011-08-31 14:08:15 -07001493 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001494
David Collins6f032ba2011-08-31 14:08:15 -07001495 val[vreg->part->pc.word]
1496 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1497 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498
David Collins6f032ba2011-08-31 14:08:15 -07001499 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1500 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001501
David Collins6f032ba2011-08-31 14:08:15 -07001502 if (!vreg->is_enabled)
1503 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001504
David Collins6f032ba2011-08-31 14:08:15 -07001505 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1506 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507
David Collins6f032ba2011-08-31 14:08:15 -07001508 if (!rc)
1509 vreg->is_enabled_pc = true;
1510
1511 mutex_unlock(&vreg->pc_lock);
1512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001513 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001514 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515
David Collins6f032ba2011-08-31 14:08:15 -07001516 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001517}
1518
David Collins6f032ba2011-08-31 14:08:15 -07001519static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520{
David Collins6f032ba2011-08-31 14:08:15 -07001521 struct vreg *vreg = rdev_get_drvdata(rdev);
1522 unsigned int mask[2] = {0}, val[2] = {0};
1523 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001524
David Collins6f032ba2011-08-31 14:08:15 -07001525 mutex_lock(&vreg->pc_lock);
1526
1527 val[vreg->part->pc.word]
1528 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1529 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1530
1531 pin_fn = config->pin_func_none;
1532 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1533 pin_fn = config->pin_func_sleep_b;
1534 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1535 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1536
1537 if (!vreg->is_enabled)
1538 set_disable(vreg, mask, val);
1539
1540 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1541 vreg->part->request_len);
1542
1543 if (!rc)
1544 vreg->is_enabled_pc = false;
1545
1546 mutex_unlock(&vreg->pc_lock);
1547
1548 if (rc)
1549 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1550
1551 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001552}
1553
David Collins6f032ba2011-08-31 14:08:15 -07001554static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001555{
David Collins6f032ba2011-08-31 14:08:15 -07001556 struct vreg *vreg = rdev_get_drvdata(rdev);
1557
1558 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559}
1560
David Collins6f032ba2011-08-31 14:08:15 -07001561/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001562static struct regulator_ops ldo_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001563 .enable = rpm_vreg_enable,
1564 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001565 .is_enabled = vreg_is_enabled,
1566 .set_voltage = vreg_set_voltage,
1567 .get_voltage = vreg_get_voltage,
1568 .list_voltage = vreg_list_voltage,
1569 .set_mode = vreg_set_mode,
1570 .get_mode = vreg_get_mode,
1571 .get_optimum_mode = vreg_get_optimum_mode,
1572 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001573};
1574
1575static struct regulator_ops smps_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001576 .enable = rpm_vreg_enable,
1577 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001578 .is_enabled = vreg_is_enabled,
1579 .set_voltage = vreg_set_voltage,
1580 .get_voltage = vreg_get_voltage,
1581 .list_voltage = vreg_list_voltage,
1582 .set_mode = vreg_set_mode,
1583 .get_mode = vreg_get_mode,
1584 .get_optimum_mode = vreg_get_optimum_mode,
1585 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001586};
1587
1588static struct regulator_ops switch_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001589 .enable = rpm_vreg_enable,
1590 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001591 .is_enabled = vreg_is_enabled,
1592 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001593};
1594
1595static struct regulator_ops ncp_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001596 .enable = rpm_vreg_enable,
1597 .disable = rpm_vreg_disable,
1598 .is_enabled = vreg_is_enabled,
1599 .set_voltage = vreg_set_voltage,
1600 .get_voltage = vreg_get_voltage,
1601 .list_voltage = vreg_list_voltage,
1602 .enable_time = vreg_enable_time,
1603};
1604
1605static struct regulator_ops corner_ops = {
1606 .enable = rpm_vreg_enable,
1607 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001608 .is_enabled = vreg_is_enabled,
1609 .set_voltage = vreg_set_voltage,
1610 .get_voltage = vreg_get_voltage,
1611 .list_voltage = vreg_list_voltage,
1612 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001613};
1614
David Collins6f032ba2011-08-31 14:08:15 -07001615/* Pin control regulator operations. */
1616static struct regulator_ops pin_control_ops = {
1617 .enable = vreg_pin_control_enable,
1618 .disable = vreg_pin_control_disable,
1619 .is_enabled = vreg_pin_control_is_enabled,
1620};
1621
1622struct regulator_ops *vreg_ops[] = {
1623 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1624 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1625 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1626 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
David Collins0ac31fe2012-02-08 13:53:34 -08001627 [RPM_REGULATOR_TYPE_CORNER] = &corner_ops,
David Collins6f032ba2011-08-31 14:08:15 -07001628};
1629
David Collins109a8e62012-07-31 15:21:35 -07001630static struct vreg *rpm_vreg_get_vreg(int id)
1631{
1632 struct vreg *vreg;
1633
1634 if (id < config->vreg_id_min || id > config->vreg_id_max)
1635 return NULL;
1636
1637 if (!config->is_real_id(id))
1638 id = config->pc_id_to_real_id(id);
1639 vreg = &config->vregs[id];
1640
1641 return vreg;
1642}
1643
David Collins6f032ba2011-08-31 14:08:15 -07001644static int __devinit
1645rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1646 struct device *dev)
1647{
1648 struct regulator_desc *rdesc = NULL;
1649 struct regulator_dev *rdev;
1650 struct vreg *vreg;
1651 unsigned pin_ctrl;
David Collins109a8e62012-07-31 15:21:35 -07001652 int pin_fn;
David Collins6f032ba2011-08-31 14:08:15 -07001653 int rc = 0;
1654
1655 if (!pdata) {
1656 pr_err("platform data missing\n");
1657 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 }
1659
David Collins109a8e62012-07-31 15:21:35 -07001660 vreg = rpm_vreg_get_vreg(pdata->id);
1661 if (!vreg) {
1662 pr_err("invalid regulator id: %d\n", pdata->id);
David Collins6f032ba2011-08-31 14:08:15 -07001663 return -ENODEV;
1664 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665
David Collins6f032ba2011-08-31 14:08:15 -07001666 if (config->is_real_id(pdata->id))
1667 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668 else
David Collins6f032ba2011-08-31 14:08:15 -07001669 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670
David Collins6f032ba2011-08-31 14:08:15 -07001671 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1672 pr_err("%s: invalid regulator type: %d\n",
1673 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001675 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676
David Collins6f032ba2011-08-31 14:08:15 -07001677 mutex_lock(&vreg->pc_lock);
1678
1679 if (vreg->set_points)
1680 rdesc->n_voltages = vreg->set_points->n_voltages;
1681 else
1682 rdesc->n_voltages = 0;
1683
1684 rdesc->id = pdata->id;
1685 rdesc->owner = THIS_MODULE;
1686 rdesc->type = REGULATOR_VOLTAGE;
1687
1688 if (config->is_real_id(pdata->id)) {
1689 /*
1690 * Real regulator; do not modify pin control and pin function
1691 * values.
1692 */
1693 rdesc->ops = vreg_ops[vreg->type];
1694 pin_ctrl = vreg->pdata.pin_ctrl;
1695 pin_fn = vreg->pdata.pin_fn;
1696 memcpy(&(vreg->pdata), pdata,
1697 sizeof(struct rpm_regulator_init_data));
1698 vreg->pdata.pin_ctrl = pin_ctrl;
1699 vreg->pdata.pin_fn = pin_fn;
1700
1701 vreg->save_uV = vreg->pdata.default_uV;
1702 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1703 vreg->mode = config->mode_hpm;
1704 else
1705 vreg->mode = config->mode_lpm;
1706
1707 /* Initialize the RPM request. */
1708 SET_PART(vreg, ip,
1709 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1710 SET_PART(vreg, fm, vreg->pdata.force_mode);
1711 SET_PART(vreg, pm, vreg->pdata.power_mode);
1712 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1713 SET_PART(vreg, ia,
1714 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1715 SET_PART(vreg, freq, vreg->pdata.freq);
1716 SET_PART(vreg, freq_clk_src, 0);
1717 SET_PART(vreg, comp_mode, 0);
1718 SET_PART(vreg, hpm, 0);
1719 if (!vreg->is_enabled_pc) {
1720 SET_PART(vreg, pf, config->pin_func_none);
1721 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1722 }
1723 } else {
1724 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1725 == RPM_VREG_PIN_CTRL_NONE
1726 && pdata->pin_fn != config->pin_func_sleep_b) {
1727 pr_err("%s: no pin control input specified\n",
1728 vreg->rdesc.name);
1729 mutex_unlock(&vreg->pc_lock);
1730 return -EINVAL;
1731 }
1732 rdesc->ops = &pin_control_ops;
1733 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1734 vreg->pdata.pin_fn = pdata->pin_fn;
1735
1736 /* Initialize the RPM request. */
1737 pin_fn = config->pin_func_none;
1738 /* Allow pf=sleep_b to be specified by platform data. */
1739 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1740 pin_fn = config->pin_func_sleep_b;
1741 SET_PART(vreg, pf, pin_fn);
1742 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1743 }
1744
1745 mutex_unlock(&vreg->pc_lock);
1746
1747 if (rc)
1748 goto bail;
1749
Rajendra Nayak11eafc62011-11-18 16:47:19 +05301750 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001751 if (IS_ERR(rdev)) {
1752 rc = PTR_ERR(rdev);
1753 pr_err("regulator_register failed: %s, rc=%d\n",
1754 vreg->rdesc.name, rc);
1755 return rc;
1756 } else {
1757 if (config->is_real_id(pdata->id))
1758 vreg->rdev = rdev;
1759 else
1760 vreg->rdev_pc = rdev;
1761 }
1762
1763bail:
1764 if (rc)
1765 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1766
1767 return rc;
1768}
1769
1770static void rpm_vreg_set_point_init(void)
1771{
1772 struct vreg_set_points **set_points;
1773 int i, j, temp;
1774
1775 set_points = config->set_points;
1776
1777 /* Calculate the number of set points available for each regulator. */
1778 for (i = 0; i < config->set_points_len; i++) {
1779 temp = 0;
1780 for (j = 0; j < set_points[i]->count; j++) {
1781 set_points[i]->range[j].n_voltages
1782 = (set_points[i]->range[j].max_uV
1783 - set_points[i]->range[j].min_uV)
1784 / set_points[i]->range[j].step_uV + 1;
1785 temp += set_points[i]->range[j].n_voltages;
1786 }
1787 set_points[i]->n_voltages = temp;
1788 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001789}
1790
1791static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1792{
David Collins6f032ba2011-08-31 14:08:15 -07001793 struct rpm_regulator_platform_data *platform_data;
David Collins379b4b72012-05-14 16:33:51 -07001794 static struct rpm_regulator_consumer_mapping *prev_consumer_map;
1795 static int prev_consumer_map_len;
David Collins109a8e62012-07-31 15:21:35 -07001796 struct vreg *vreg;
David Collins6f032ba2011-08-31 14:08:15 -07001797 int rc = 0;
1798 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001799
David Collins6f032ba2011-08-31 14:08:15 -07001800 platform_data = pdev->dev.platform_data;
1801 if (!platform_data) {
1802 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001803 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804 }
1805
David Collins6f032ba2011-08-31 14:08:15 -07001806 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1807 && platform_data->version != rpm_version) {
1808 pr_err("rpm version %d does not match previous version %d\n",
1809 platform_data->version, rpm_version);
1810 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001811 }
1812
David Collins6f032ba2011-08-31 14:08:15 -07001813 if (platform_data->version < 0
1814 || platform_data->version > RPM_VREG_VERSION_MAX) {
1815 pr_err("rpm version %d is invalid\n", platform_data->version);
1816 return -EINVAL;
1817 }
1818
1819 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1820 rpm_version = platform_data->version;
1821 config = get_config[platform_data->version]();
1822 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1823 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1824 if (!config) {
1825 pr_err("rpm version %d is not available\n",
1826 platform_data->version);
1827 return -ENODEV;
1828 }
1829 if (config->use_legacy_optimum_mode)
1830 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1831 vreg_ops[i]->get_optimum_mode
1832 = vreg_legacy_get_optimum_mode;
1833 rpm_vreg_set_point_init();
1834 /* First time probed; initialize pin control mutexes. */
1835 for (i = 0; i < config->vregs_len; i++)
1836 mutex_init(&config->vregs[i].pc_lock);
1837 }
1838
David Collins379b4b72012-05-14 16:33:51 -07001839 /* Copy the list of private API consumers. */
1840 if (platform_data->consumer_map_len > 0) {
1841 if (consumer_map_len == 0) {
1842 consumer_map_len = platform_data->consumer_map_len;
1843 consumer_map = kmemdup(platform_data->consumer_map,
1844 sizeof(struct rpm_regulator_consumer_mapping)
1845 * consumer_map_len, GFP_KERNEL);
1846 if (consumer_map == NULL) {
1847 pr_err("memory allocation failed\n");
1848 consumer_map_len = 0;
1849 return -ENOMEM;
1850 }
1851 } else {
1852 /* Concatenate new map with the existing one. */
1853 prev_consumer_map = consumer_map;
1854 prev_consumer_map_len = consumer_map_len;
1855 consumer_map_len += platform_data->consumer_map_len;
1856 consumer_map = kmalloc(
1857 sizeof(struct rpm_regulator_consumer_mapping)
1858 * consumer_map_len, GFP_KERNEL);
1859 if (consumer_map == NULL) {
1860 pr_err("memory allocation failed\n");
1861 consumer_map_len = 0;
1862 return -ENOMEM;
1863 }
1864 memcpy(consumer_map, prev_consumer_map,
1865 sizeof(struct rpm_regulator_consumer_mapping)
1866 * prev_consumer_map_len);
1867 memcpy(&consumer_map[prev_consumer_map_len],
1868 platform_data->consumer_map,
1869 sizeof(struct rpm_regulator_consumer_mapping)
1870 * platform_data->consumer_map_len);
1871 }
1872
1873 }
1874
David Collins109a8e62012-07-31 15:21:35 -07001875 if (platform_data->requires_tcxo_workaround
1876 && !requires_tcxo_workaround) {
1877 requires_tcxo_workaround = true;
1878 wake_lock_init(&tcxo_wake_lock, WAKE_LOCK_SUSPEND,
1879 "rpm_regulator_tcxo");
1880 }
1881
1882 if (requires_tcxo_workaround && !tcxo_workaround_noirq) {
1883 for (i = 0; i < platform_data->num_regulators; i++) {
1884 vreg = rpm_vreg_get_vreg(
1885 platform_data->init_data[i].id);
1886 if (vreg && vreg->requires_cxo
1887 && platform_data->init_data[i].sleep_selectable) {
1888 tcxo_workaround_noirq = true;
1889 break;
1890 }
1891 }
1892 }
1893
David Collins6f032ba2011-08-31 14:08:15 -07001894 /* Initialize all of the regulators listed in the platform data. */
1895 for (i = 0; i < platform_data->num_regulators; i++) {
1896 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1897 &pdev->dev);
1898 if (rc) {
1899 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1900 goto remove_regulators;
1901 }
1902 }
1903
1904 platform_set_drvdata(pdev, platform_data);
1905
1906 return rc;
1907
1908remove_regulators:
1909 /* Unregister all regulators added before the erroring one. */
1910 for (; i >= 0; i--) {
1911 id = platform_data->init_data[i].id;
1912 if (config->is_real_id(id)) {
1913 regulator_unregister(config->vregs[id].rdev);
1914 config->vregs[id].rdev = NULL;
1915 } else {
1916 regulator_unregister(config->vregs[
1917 config->pc_id_to_real_id(id)].rdev_pc);
1918 config->vregs[id].rdev_pc = NULL;
1919 }
1920 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921
1922 return rc;
1923}
1924
1925static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1926{
David Collins6f032ba2011-08-31 14:08:15 -07001927 struct rpm_regulator_platform_data *platform_data;
1928 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929
David Collins6f032ba2011-08-31 14:08:15 -07001930 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001931 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001932
1933 if (platform_data) {
1934 for (i = 0; i < platform_data->num_regulators; i++) {
1935 id = platform_data->init_data[i].id;
1936 if (config->is_real_id(id)) {
1937 regulator_unregister(config->vregs[id].rdev);
1938 config->vregs[id].rdev = NULL;
1939 } else {
1940 regulator_unregister(config->vregs[
1941 config->pc_id_to_real_id(id)].rdev_pc);
1942 config->vregs[id].rdev_pc = NULL;
1943 }
1944 }
1945 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946
1947 return 0;
1948}
1949
1950static struct platform_driver rpm_vreg_driver = {
1951 .probe = rpm_vreg_probe,
1952 .remove = __devexit_p(rpm_vreg_remove),
1953 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001954 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 .owner = THIS_MODULE,
1956 },
1957};
1958
1959static int __init rpm_vreg_init(void)
1960{
1961 return platform_driver_register(&rpm_vreg_driver);
1962}
1963
1964static void __exit rpm_vreg_exit(void)
1965{
David Collins6f032ba2011-08-31 14:08:15 -07001966 int i;
1967
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001968 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001969
David Collins379b4b72012-05-14 16:33:51 -07001970 kfree(consumer_map);
1971
David Collins6f032ba2011-08-31 14:08:15 -07001972 for (i = 0; i < config->vregs_len; i++)
1973 mutex_destroy(&config->vregs[i].pc_lock);
David Collins109a8e62012-07-31 15:21:35 -07001974
1975 if (tcxo_handle)
1976 clk_put(tcxo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001977}
1978
1979postcore_initcall(rpm_vreg_init);
1980module_exit(rpm_vreg_exit);
1981
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001982MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001983MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001984MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001985MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);