blob: 01543a289ebaeb52864a983418338ccd1432bb1f [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 Collinsfafa9522012-10-02 14:56:21 -070066 [RPM_VREG_VERSION_8960_PM8917] = get_config_8960_pm8917,
David Collins6f032ba2011-08-31 14:08:15 -070067};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
David Collins379b4b72012-05-14 16:33:51 -070069static struct rpm_regulator_consumer_mapping *consumer_map;
70static int consumer_map_len;
71
David Collins6f032ba2011-08-31 14:08:15 -070072#define SET_PART(_vreg, _part, _val) \
73 _vreg->req[_vreg->part->_part.word].value \
74 = (_vreg->req[_vreg->part->_part.word].value \
David Collinsd8525e82011-11-21 14:54:25 -080075 & ~_vreg->part->_part.mask) \
76 | (((_val) << _vreg->part->_part.shift) \
77 & _vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078
David Collins6f032ba2011-08-31 14:08:15 -070079#define GET_PART(_vreg, _part) \
David Collinsd8525e82011-11-21 14:54:25 -080080 ((_vreg->req[_vreg->part->_part.word].value & _vreg->part->_part.mask) \
81 >> _vreg->part->_part.shift)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082
David Collins109a8e62012-07-31 15:21:35 -070083#define GET_PART_PREV_ACT(_vreg, _part) \
84 ((_vreg->prev_active_req[_vreg->part->_part.word].value \
85 & _vreg->part->_part.mask) \
86 >> _vreg->part->_part.shift)
87
David Collinsd8525e82011-11-21 14:54:25 -080088#define USES_PART(_vreg, _part) (_vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089
David Collins6f032ba2011-08-31 14:08:15 -070090#define vreg_err(vreg, fmt, ...) \
91 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092
David Collins6f032ba2011-08-31 14:08:15 -070093#define RPM_VREG_PIN_CTRL_EN0 0x01
94#define RPM_VREG_PIN_CTRL_EN1 0x02
95#define RPM_VREG_PIN_CTRL_EN2 0x04
96#define RPM_VREG_PIN_CTRL_EN3 0x08
97#define RPM_VREG_PIN_CTRL_ALL 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098
David Collins6f032ba2011-08-31 14:08:15 -070099static const char *label_freq[] = {
100 [RPM_VREG_FREQ_NONE] = " N/A",
101 [RPM_VREG_FREQ_19p20] = "19.2",
102 [RPM_VREG_FREQ_9p60] = "9.60",
103 [RPM_VREG_FREQ_6p40] = "6.40",
104 [RPM_VREG_FREQ_4p80] = "4.80",
105 [RPM_VREG_FREQ_3p84] = "3.84",
106 [RPM_VREG_FREQ_3p20] = "3.20",
107 [RPM_VREG_FREQ_2p74] = "2.74",
108 [RPM_VREG_FREQ_2p40] = "2.40",
109 [RPM_VREG_FREQ_2p13] = "2.13",
110 [RPM_VREG_FREQ_1p92] = "1.92",
111 [RPM_VREG_FREQ_1p75] = "1.75",
112 [RPM_VREG_FREQ_1p60] = "1.60",
113 [RPM_VREG_FREQ_1p48] = "1.48",
114 [RPM_VREG_FREQ_1p37] = "1.37",
115 [RPM_VREG_FREQ_1p28] = "1.28",
116 [RPM_VREG_FREQ_1p20] = "1.20",
117};
David Collins0ac31fe2012-02-08 13:53:34 -0800118
119static const char *label_corner[] = {
120 [RPM_VREG_CORNER_NONE] = "NONE",
121 [RPM_VREG_CORNER_LOW] = "LOW",
122 [RPM_VREG_CORNER_NOMINAL] = "NOM",
123 [RPM_VREG_CORNER_HIGH] = "HIGH",
124};
125
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126/*
127 * This is used when voting for LPM or HPM by subtracting or adding to the
128 * hpm_min_load of a regulator. It has units of uA.
129 */
David Collins6f032ba2011-08-31 14:08:15 -0700130#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131
David Collins6f032ba2011-08-31 14:08:15 -0700132/* rpm_version keeps track of the version for the currently running driver. */
133enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
David Collins6f032ba2011-08-31 14:08:15 -0700135/* config holds all configuration data of the currently running driver. */
136static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137
David Collins6f032ba2011-08-31 14:08:15 -0700138/* These regulator ID values are specified in the board file. */
139static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140
David Collins6f032ba2011-08-31 14:08:15 -0700141static inline int vreg_id_is_vdd_mem_or_dig(int id)
142{
143 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
144}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145
David Collins6f032ba2011-08-31 14:08:15 -0700146#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147
David Collins6f032ba2011-08-31 14:08:15 -0700148static void rpm_regulator_req(struct vreg *vreg, int set)
149{
150 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
151 const char *pf_label = "", *fm_label = "", *pc_total = "";
152 const char *pc_en[4] = {"", "", "", ""};
David Collins0ac31fe2012-02-08 13:53:34 -0800153 const char *pm_label = "", *freq_label = "", *corner_label = "";
David Collins6f032ba2011-08-31 14:08:15 -0700154 char buf[DEBUG_PRINT_BUFFER_SIZE];
155 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
156 int pos = 0;
157
158 /* Suppress VDD_MEM and VDD_DIG printing. */
159 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
160 && vreg_id_is_vdd_mem_or_dig(vreg->id))
161 return;
162
163 uV = GET_PART(vreg, uV);
164 mV = GET_PART(vreg, mV);
165 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
166 uV = -uV;
167 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 }
169
David Collins6f032ba2011-08-31 14:08:15 -0700170 fm = GET_PART(vreg, fm);
171 pm = GET_PART(vreg, pm);
172 pc = GET_PART(vreg, pc);
173 pf = GET_PART(vreg, pf);
174 pd = GET_PART(vreg, pd);
175 freq = GET_PART(vreg, freq);
176 state = GET_PART(vreg, enable_state);
177
178 if (pf >= 0 && pf < config->label_pin_func_len)
179 pf_label = config->label_pin_func[pf];
180
181 if (fm >= 0 && fm < config->label_force_mode_len)
182 fm_label = config->label_force_mode[fm];
183
184 if (pm >= 0 && pm < config->label_power_mode_len)
185 pm_label = config->label_power_mode[pm];
186
187 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
188 freq_label = label_freq[freq];
189
190 for (i = 0; i < config->label_pin_ctrl_len; i++)
191 if (pc & (1 << i))
192 pc_en[i] = config->label_pin_ctrl[i];
193
194 if (pc == RPM_VREG_PIN_CTRL_NONE)
195 pc_total = " none";
196
197 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
198 KERN_INFO, __func__);
199
200 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
201 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
202 vreg->rdesc.name,
203 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
204
David Collins0ac31fe2012-02-08 13:53:34 -0800205 if (USES_PART(vreg, uV) && vreg->type != RPM_REGULATOR_TYPE_CORNER)
David Collins6f032ba2011-08-31 14:08:15 -0700206 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
207 if (USES_PART(vreg, mV))
208 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
209 if (USES_PART(vreg, enable_state))
210 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
211 (state == 1 ? "on" : "off"), state);
212 if (USES_PART(vreg, ip))
213 pos += scnprintf(buf + pos, buflen - pos,
214 ", ip=%4d mA", GET_PART(vreg, ip));
215 if (USES_PART(vreg, fm))
216 pos += scnprintf(buf + pos, buflen - pos,
217 ", fm=%s (%d)", fm_label, fm);
218 if (USES_PART(vreg, pc))
219 pos += scnprintf(buf + pos, buflen - pos,
220 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
221 pc_en[2], pc_en[3], pc_total, pc);
222 if (USES_PART(vreg, pf))
223 pos += scnprintf(buf + pos, buflen - pos,
224 ", pf=%s (%d)", pf_label, pf);
225 if (USES_PART(vreg, pd))
226 pos += scnprintf(buf + pos, buflen - pos,
227 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
228 if (USES_PART(vreg, ia))
229 pos += scnprintf(buf + pos, buflen - pos,
230 ", ia=%4d mA", GET_PART(vreg, ia));
231 if (USES_PART(vreg, freq)) {
232 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
233 pos += scnprintf(buf + pos, buflen - pos,
234 ", freq=%2d", freq);
235 else
236 pos += scnprintf(buf + pos, buflen - pos,
237 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 }
David Collins6f032ba2011-08-31 14:08:15 -0700239 if (USES_PART(vreg, pm))
240 pos += scnprintf(buf + pos, buflen - pos,
241 ", pm=%s (%d)", pm_label, pm);
242 if (USES_PART(vreg, freq_clk_src))
243 pos += scnprintf(buf + pos, buflen - pos,
244 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
245 if (USES_PART(vreg, comp_mode))
246 pos += scnprintf(buf + pos, buflen - pos,
247 ", comp=%d", GET_PART(vreg, comp_mode));
248 if (USES_PART(vreg, hpm))
249 pos += scnprintf(buf + pos, buflen - pos,
250 ", hpm=%d", GET_PART(vreg, hpm));
David Collins0ac31fe2012-02-08 13:53:34 -0800251 if (USES_PART(vreg, uV) && vreg->type == RPM_REGULATOR_TYPE_CORNER) {
252 if (uV >= 0 && uV < (ARRAY_SIZE(label_corner) - 1))
253 corner_label = label_corner[uV+1];
254 pos += scnprintf(buf + pos, buflen - pos, ", corner=%s (%d)",
255 corner_label, uV);
256 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257
David Collins6f032ba2011-08-31 14:08:15 -0700258 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
259 vreg->req[0].id, vreg->req[0].value);
260 if (vreg->part->request_len > 1)
261 pos += scnprintf(buf + pos, buflen - pos,
262 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
263 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264
David Collins6f032ba2011-08-31 14:08:15 -0700265 pos += scnprintf(buf + pos, buflen - pos, "\n");
266 printk(buf);
267}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268
David Collins6f032ba2011-08-31 14:08:15 -0700269static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
270 int set, int voter_uV, int aggregate_uV)
271{
272 /* Suppress VDD_MEM and VDD_DIG printing. */
273 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
274 && vreg_id_is_vdd_mem_or_dig(vreg->id))
275 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276
David Collins6f032ba2011-08-31 14:08:15 -0700277 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
278 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
279 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
280}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281
David Collins6f032ba2011-08-31 14:08:15 -0700282static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
283{
284 /* Suppress VDD_MEM and VDD_DIG printing. */
285 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
286 && vreg_id_is_vdd_mem_or_dig(vreg->id))
287 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288
David Collins6f032ba2011-08-31 14:08:15 -0700289 if (cnt == 2)
290 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
291 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
292 (set == 0 ? 'A' : 'S'),
293 vreg->req[0].id, vreg->req[0].value,
294 vreg->req[1].id, vreg->req[1].value);
295 else if (cnt == 1)
296 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
297 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
298 vreg->req[0].id, vreg->req[0].value);
299}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300
David Collins109a8e62012-07-31 15:21:35 -0700301static bool requires_tcxo_workaround;
302static bool tcxo_workaround_noirq;
303static struct clk *tcxo_handle;
304static struct wake_lock tcxo_wake_lock;
305static DEFINE_MUTEX(tcxo_mutex);
306/* Spin lock needed for sleep-selectable regulators. */
307static DEFINE_SPINLOCK(tcxo_noirq_lock);
308static bool tcxo_is_enabled;
309/*
310 * TCXO must be kept on for at least the duration of its warmup (4 ms);
311 * otherwise, it will stay on when hardware disabling is attempted.
312 */
313#define TCXO_WARMUP_TIME_MS 4
314
315static void tcxo_get_handle(void)
316{
317 int rc;
318
319 if (!tcxo_handle) {
320 tcxo_handle = clk_get_sys("rpm-regulator", "vref_buff");
321 if (IS_ERR(tcxo_handle)) {
322 tcxo_handle = NULL;
323 } else {
324 rc = clk_prepare(tcxo_handle);
325 if (rc) {
326 clk_put(tcxo_handle);
327 tcxo_handle = NULL;
328 }
329 }
330 }
331}
332
333/*
334 * Perform best effort enable of CXO. Since the MSM clock drivers depend upon
335 * the rpm-regulator driver, any rpm-regulator devices that are configured with
336 * always_on == 1 will not be able to enable CXO during probe. This does not
337 * cause a problem though since CXO will be enabled by the boot loaders before
338 * Apps boots up.
339 */
340static bool tcxo_enable(void)
341{
342 int rc;
343
344 if (tcxo_handle && !tcxo_is_enabled) {
345 rc = clk_enable(tcxo_handle);
346 if (!rc) {
347 tcxo_is_enabled = true;
348 wake_lock(&tcxo_wake_lock);
349 return true;
350 }
351 }
352
353 return false;
354}
355
356static void tcxo_delayed_disable_work(struct work_struct *work)
357{
358 unsigned long flags = 0;
359
360 if (tcxo_workaround_noirq)
361 spin_lock_irqsave(&tcxo_noirq_lock, flags);
362 else
363 mutex_lock(&tcxo_mutex);
364
365 clk_disable(tcxo_handle);
366 tcxo_is_enabled = false;
367 wake_unlock(&tcxo_wake_lock);
368
369 if (tcxo_workaround_noirq)
370 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
371 else
372 mutex_unlock(&tcxo_mutex);
373}
374
375static DECLARE_DELAYED_WORK(tcxo_disable_work, tcxo_delayed_disable_work);
376
377static void tcxo_delayed_disable(void)
378{
379 /*
380 * The delay in jiffies has 1 added to it to ensure that at least
381 * one jiffy takes place before the work is enqueued. Without this,
382 * the work would be scheduled to run in the very next jiffy which could
383 * result in too little delay and TCXO being stuck on.
384 */
385 if (tcxo_handle)
386 schedule_delayed_work(&tcxo_disable_work,
387 msecs_to_jiffies(TCXO_WARMUP_TIME_MS) + 1);
388}
389
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700391static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392
393static int voltage_from_req(struct vreg *vreg)
394{
David Collins6f032ba2011-08-31 14:08:15 -0700395 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396
David Collins6f032ba2011-08-31 14:08:15 -0700397 if (vreg->part->uV.mask)
398 uV = GET_PART(vreg, uV);
David Collins13397f22012-02-06 13:53:29 -0800399 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700400 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
David Collins13397f22012-02-06 13:53:29 -0800401 else if (vreg->part->enable_state.mask)
402 uV = GET_PART(vreg, enable_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403
David Collins6f032ba2011-08-31 14:08:15 -0700404 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405}
406
David Collins6f032ba2011-08-31 14:08:15 -0700407static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408{
David Collins6f032ba2011-08-31 14:08:15 -0700409 if (vreg->part->uV.mask)
410 SET_PART(vreg, uV, uV);
David Collins13397f22012-02-06 13:53:29 -0800411 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700412 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
David Collins13397f22012-02-06 13:53:29 -0800413 else if (vreg->part->enable_state.mask)
414 SET_PART(vreg, enable_state, uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415}
416
417static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
418 int set, unsigned mask0, unsigned val0,
419 unsigned mask1, unsigned val1, unsigned cnt,
420 int update_voltage)
421{
422 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700423 int rc = 0, max_uV_vote = 0;
David Collins109a8e62012-07-31 15:21:35 -0700424 unsigned long flags = 0;
425 bool tcxo_enabled = false;
426 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700428 int *min_uV_vote;
429 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430
431 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700432 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 prev_req = vreg->prev_active_req;
434 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700435 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 prev_req = vreg->prev_sleep_req;
437 }
438
439 prev0 = vreg->req[0].value;
440 vreg->req[0].value &= ~mask0;
441 vreg->req[0].value |= val0 & mask0;
442
443 prev1 = vreg->req[1].value;
444 vreg->req[1].value &= ~mask1;
445 vreg->req[1].value |= val1 & mask1;
446
David Collinsd1b7aea2012-03-19 10:27:55 -0700447 /* Set the force mode field based on which set is being requested. */
448 if (set == MSM_RPM_CTX_SET_0)
449 SET_PART(vreg, fm, vreg->pdata.force_mode);
450 else
451 SET_PART(vreg, fm, vreg->pdata.sleep_set_force_mode);
452
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700454 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455
456 /* Find the highest voltage voted for and use it. */
457 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700458 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
459 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460
461 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700462 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
463 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464
465 /* Ignore duplicate requests */
466 if (vreg->req[0].value != prev_req[0].value ||
467 vreg->req[1].value != prev_req[1].value) {
David Collins109a8e62012-07-31 15:21:35 -0700468
469 /* Enable CXO clock if necessary for TCXO workaround. */
470 if (requires_tcxo_workaround && vreg->requires_cxo
471 && (set == MSM_RPM_CTX_SET_0)
472 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
473 voltage_increased = true;
474 spin_lock_irqsave(&tcxo_noirq_lock, flags);
475 tcxo_enabled = tcxo_enable();
476 }
477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
479 if (rc) {
480 vreg->req[0].value = prev0;
481 vreg->req[1].value = prev1;
482
David Collins6f032ba2011-08-31 14:08:15 -0700483 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
484 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
486 vreg->req[0].id, rc);
487 } else {
488 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700489 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
490 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 if (msm_rpm_vreg_debug_mask
492 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700493 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 prev_req[0].value = vreg->req[0].value;
495 prev_req[1].value = vreg->req[1].value;
496 }
David Collins109a8e62012-07-31 15:21:35 -0700497
498 /*
499 * Schedule CXO clock to be disabled after TCXO warmup time if
500 * TCXO workaround is applicable for this regulator.
501 */
502 if (voltage_increased) {
503 if (tcxo_enabled)
504 tcxo_delayed_disable();
505 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
506 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700508 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 }
510
511 return rc;
512}
513
514static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
515 int sleep, unsigned mask0, unsigned val0,
516 unsigned mask1, unsigned val1, unsigned cnt,
517 int update_voltage)
518{
David Collins6f032ba2011-08-31 14:08:15 -0700519 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 unsigned long flags;
521 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522
523 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
524 return -EINVAL;
525
David Collins6f032ba2011-08-31 14:08:15 -0700526 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527
528 /*
529 * Send sleep set request first so that subsequent set_mode, etc calls
530 * use the voltage from the active set.
531 */
532 if (sleep)
533 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
534 mask0, val0, mask1, val1, cnt, update_voltage);
535 else {
536 /*
537 * Vote for 0 V in the sleep set when active set-only is
538 * specified. This ensures that a disable vote will be issued
539 * at some point for the sleep set of the regulator.
540 */
David Collins6f032ba2011-08-31 14:08:15 -0700541 if (vreg->part->uV.mask) {
542 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
543 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800544 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700545 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
546 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800547 } else if (vreg->part->enable_state.mask) {
548 s_val[vreg->part->enable_state.word]
549 = 0 << vreg->part->enable_state.shift;
550 s_mask[vreg->part->enable_state.word]
551 = vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 }
553
554 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700555 s_mask[0], s_val[0], s_mask[1], s_val[1],
556 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 }
558
559 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
560 mask1, val1, cnt, update_voltage);
561
David Collins6f032ba2011-08-31 14:08:15 -0700562 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
564 return rc;
565}
566
567/**
568 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
569 * @vreg: ID for regulator
570 * @voter: ID for the voter
571 * @min_uV: minimum acceptable voltage (in uV) that is voted for
572 * @max_uV: maximum acceptable voltage (in uV) that is voted for
573 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
574 *
575 * Returns 0 on success or errno.
576 *
577 * This function is used to vote for the voltage of a regulator without
578 * using the regulator framework. It is needed by consumers which hold spin
579 * locks or have interrupts disabled because the regulator framework can sleep.
580 * It is also needed by consumers which wish to only vote for active set
581 * regulator voltage.
582 *
583 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
584 *
585 * This function may only be called for regulators which have the sleep flag
586 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700587 *
588 * Consumers can vote to disable a regulator with this function by passing
589 * min_uV = 0 and max_uV = 0.
David Collins13397f22012-02-06 13:53:29 -0800590 *
591 * Voltage switch type regulators may be controlled via rpm_vreg_set_voltage
592 * as well. For this type of regulator, max_uV > 0 is treated as an enable
593 * request and max_uV == 0 is treated as a disable request.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 */
David Collins6f032ba2011-08-31 14:08:15 -0700595int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
596 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597{
David Collins6f032ba2011-08-31 14:08:15 -0700598 unsigned int mask[2] = {0}, val[2] = {0};
599 struct vreg_range *range;
600 struct vreg *vreg;
601 int uV = min_uV;
602 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603
David Collins6f032ba2011-08-31 14:08:15 -0700604 if (!config) {
605 pr_err("rpm-regulator driver has not probed yet.\n");
606 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 }
608
David Collins6f032ba2011-08-31 14:08:15 -0700609 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
610 pr_err("invalid regulator id=%d\n", vreg_id);
611 return -EINVAL;
612 }
613
614 vreg = &config->vregs[vreg_id];
David Collins6f032ba2011-08-31 14:08:15 -0700615
616 if (!vreg->pdata.sleep_selectable) {
617 vreg_err(vreg, "regulator is not marked sleep selectable\n");
618 return -EINVAL;
619 }
620
David Collins7462b9d2011-10-11 16:02:17 -0700621 /* Allow min_uV == max_uV == 0 to represent a disable request. */
David Collins13397f22012-02-06 13:53:29 -0800622 if ((min_uV != 0 || max_uV != 0)
623 && (vreg->part->uV.mask || vreg->part->mV.mask)) {
David Collins7462b9d2011-10-11 16:02:17 -0700624 /*
625 * Check if request voltage is outside of allowed range. The
626 * regulator core has already checked that constraint range
627 * is inside of the physically allowed range.
628 */
629 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
630 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700631
David Collins7462b9d2011-10-11 16:02:17 -0700632 if (uV < lim_min_uV && max_uV >= lim_min_uV)
633 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700634
David Collins7462b9d2011-10-11 16:02:17 -0700635 if (uV < lim_min_uV || uV > lim_max_uV) {
636 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
637 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
638 lim_max_uV);
639 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700640 }
David Collins6f032ba2011-08-31 14:08:15 -0700641
David Collins13397f22012-02-06 13:53:29 -0800642 range = &vreg->set_points->range[0];
David Collins7462b9d2011-10-11 16:02:17 -0700643 /* Find the range which uV is inside of. */
644 for (i = vreg->set_points->count - 1; i > 0; i--) {
645 if (uV > vreg->set_points->range[i - 1].max_uV) {
646 range = &vreg->set_points->range[i];
647 break;
648 }
649 }
650
651 /*
652 * Force uV to be an allowed set point and apply a ceiling
653 * function to non-set point values.
654 */
655 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
656 uV = uV * range->step_uV + range->min_uV;
David Collins3974b612011-11-21 15:07:36 -0800657
658 if (uV > max_uV) {
659 vreg_err(vreg,
660 "request v=[%d, %d] cannot be met by any set point; "
661 "next set point: %d\n",
662 min_uV, max_uV, uV);
663 return -EINVAL;
664 }
David Collins7462b9d2011-10-11 16:02:17 -0700665 }
David Collins6f032ba2011-08-31 14:08:15 -0700666
David Collins0ac31fe2012-02-08 13:53:34 -0800667 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
668 /*
669 * Translate from enum values which work as inputs in the
670 * rpm_vreg_set_voltage function to the actual corner values
671 * sent to the RPM.
672 */
673 if (uV > 0)
674 uV -= RPM_VREG_CORNER_NONE;
675 }
676
David Collins6f032ba2011-08-31 14:08:15 -0700677 if (vreg->part->uV.mask) {
678 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
679 mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800680 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700681 val[vreg->part->mV.word]
682 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
683 mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800684 } else if (vreg->part->enable_state.mask) {
685 /*
686 * Translate max_uV > 0 into an enable request for regulator
687 * types which to not support voltage setting, e.g. voltage
688 * switches.
689 */
690 val[vreg->part->enable_state.word]
691 = (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift;
692 mask[vreg->part->enable_state.word]
693 = vreg->part->enable_state.mask;
David Collins6f032ba2011-08-31 14:08:15 -0700694 }
695
696 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
697 val[1], vreg->part->request_len, 1);
698 if (rc)
699 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700
701 return rc;
702}
703EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
704
705/**
706 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
707 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700708 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709 *
710 * Returns 0 on success or errno.
711 */
David Collins6f032ba2011-08-31 14:08:15 -0700712int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713{
David Collins6f032ba2011-08-31 14:08:15 -0700714 unsigned int mask[2] = {0}, val[2] = {0};
715 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 int rc;
717
David Collins6f032ba2011-08-31 14:08:15 -0700718 if (!config) {
719 pr_err("rpm-regulator driver has not probed yet.\n");
720 return -ENODEV;
721 }
722
723 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
724 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 return -EINVAL;
726 }
727
David Collins6f032ba2011-08-31 14:08:15 -0700728 vreg = &config->vregs[vreg_id];
729
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700731 vreg_err(vreg, "invalid frequency=%d\n", freq);
732 return -EINVAL;
733 }
734 if (!vreg->pdata.sleep_selectable) {
735 vreg_err(vreg, "regulator is not marked sleep selectable\n");
736 return -EINVAL;
737 }
738 if (!vreg->part->freq.mask) {
739 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 return -EINVAL;
741 }
742
David Collins6f032ba2011-08-31 14:08:15 -0700743 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
744 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745
David Collins6f032ba2011-08-31 14:08:15 -0700746 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
747 val[0], mask[1], val[1], vreg->part->request_len, 0);
748 if (rc)
749 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750
751 return rc;
752}
753EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
754
David Collins379b4b72012-05-14 16:33:51 -0700755#define MAX_NAME_LEN 64
756/**
757 * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
758 * @dev: device for regulator consumer
759 * @supply: supply name
760 *
761 * Returns a struct rpm_regulator corresponding to the regulator producer,
762 * or ERR_PTR() containing errno.
763 *
764 * This function may only be called from nonatomic context. The mapping between
765 * <dev, supply> tuples and rpm_regulators struct pointers is specified via
766 * rpm-regulator platform data.
767 */
768struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
769{
770 struct rpm_regulator_consumer_mapping *mapping = NULL;
771 const char *devname = NULL;
772 struct rpm_regulator *regulator;
773 int i;
774
775 if (!config) {
776 pr_err("rpm-regulator driver has not probed yet.\n");
777 return ERR_PTR(-ENODEV);
778 }
779
780 if (consumer_map == NULL || consumer_map_len == 0) {
781 pr_err("No private consumer mapping has been specified.\n");
782 return ERR_PTR(-ENODEV);
783 }
784
785 if (supply == NULL) {
786 pr_err("supply name must be specified\n");
787 return ERR_PTR(-EINVAL);
788 }
789
790 if (dev)
791 devname = dev_name(dev);
792
793 for (i = 0; i < consumer_map_len; i++) {
794 /* If the mapping has a device set up it must match */
795 if (consumer_map[i].dev_name &&
796 (!devname || strncmp(consumer_map[i].dev_name, devname,
797 MAX_NAME_LEN)))
798 continue;
799
800 if (strncmp(consumer_map[i].supply, supply, MAX_NAME_LEN)
801 == 0) {
802 mapping = &consumer_map[i];
803 break;
804 }
805 }
806
807 if (mapping == NULL) {
808 pr_err("could not find mapping for dev=%s, supply=%s\n",
809 (devname ? devname : "(null)"), supply);
810 return ERR_PTR(-ENODEV);
811 }
812
813 regulator = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
814 if (regulator == NULL) {
815 pr_err("could not allocate memory for regulator\n");
816 return ERR_PTR(-ENOMEM);
817 }
818
819 regulator->vreg_id = mapping->vreg_id;
820 regulator->voter = mapping->voter;
821 regulator->sleep_also = mapping->sleep_also;
822
823 return regulator;
824}
825EXPORT_SYMBOL_GPL(rpm_regulator_get);
826
827static int rpm_regulator_check_input(struct rpm_regulator *regulator)
828{
829 int rc = 0;
830
831 if (regulator == NULL) {
832 rc = -EINVAL;
833 pr_err("invalid (null) rpm_regulator pointer\n");
834 } else if (IS_ERR(regulator)) {
835 rc = PTR_ERR(regulator);
836 pr_err("invalid rpm_regulator pointer, rc=%d\n", rc);
837 }
838
839 return rc;
840}
841
842/**
843 * rpm_regulator_put() - free the RPM regulator handle
844 * @regulator: RPM regulator handle
845 *
846 * Parameter reaggregation does not take place when rpm_regulator_put is called.
847 * Therefore, regulator enable state and voltage must be configured
848 * appropriately before calling rpm_regulator_put.
849 *
850 * This function may be called from either atomic or nonatomic context.
851 */
852void rpm_regulator_put(struct rpm_regulator *regulator)
853{
854 kfree(regulator);
855}
856EXPORT_SYMBOL_GPL(rpm_regulator_put);
857
858/**
859 * rpm_regulator_enable() - enable regulator output
860 * @regulator: RPM regulator handle
861 *
862 * Returns 0 on success or errno on failure.
863 *
864 * This function may be called from either atomic or nonatomic context. This
865 * function may only be called for regulators which have the sleep_selectable
866 * flag set in their configuration data.
867 *
868 * rpm_regulator_set_voltage must be called before rpm_regulator_enable because
869 * enabling is defined by the RPM interface to be requesting the desired
870 * non-zero regulator output voltage.
871 */
872int rpm_regulator_enable(struct rpm_regulator *regulator)
873{
874 int rc = rpm_regulator_check_input(regulator);
875 struct vreg *vreg;
876
877 if (rc)
878 return rc;
879
880 if (regulator->vreg_id < config->vreg_id_min
881 || regulator->vreg_id > config->vreg_id_max) {
882 pr_err("invalid regulator id=%d\n", regulator->vreg_id);
883 return -EINVAL;
884 }
885
886 vreg = &config->vregs[regulator->vreg_id];
887
888 /*
889 * Handle voltage switches which can be enabled without
890 * rpm_regulator_set_voltage ever being called.
891 */
892 if (regulator->min_uV == 0 && regulator->max_uV == 0
893 && vreg->part->uV.mask == 0 && vreg->part->mV.mask == 0) {
894 regulator->min_uV = 1;
895 regulator->max_uV = 1;
896 }
897
898 if (regulator->min_uV == 0 && regulator->max_uV == 0) {
899 pr_err("Voltage must be set with rpm_regulator_set_voltage "
900 "before calling rpm_regulator_enable; vreg_id=%d, "
901 "voter=%d\n", regulator->vreg_id, regulator->voter);
902 return -EINVAL;
903 }
904
905 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter,
906 regulator->min_uV, regulator->max_uV, regulator->sleep_also);
907
908 if (rc)
909 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
910
911 return rc;
912}
913EXPORT_SYMBOL_GPL(rpm_regulator_enable);
914
915/**
916 * rpm_regulator_disable() - disable regulator output
917 * @regulator: RPM regulator handle
918 *
919 * Returns 0 on success or errno on failure.
920 *
921 * The enable state of the regulator is determined by aggregating the requests
922 * of all consumers. Therefore, it is possible that the regulator will remain
923 * enabled even after rpm_regulator_disable is called.
924 *
925 * This function may be called from either atomic or nonatomic context. This
926 * function may only be called for regulators which have the sleep_selectable
927 * flag set in their configuration data.
928 */
929int rpm_regulator_disable(struct rpm_regulator *regulator)
930{
931 int rc = rpm_regulator_check_input(regulator);
932
933 if (rc)
934 return rc;
935
936 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, 0, 0,
937 regulator->sleep_also);
938
939 if (rc)
940 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
941
942 return rc;
943}
944EXPORT_SYMBOL_GPL(rpm_regulator_disable);
945
946/**
947 * rpm_regulator_set_voltage() - set regulator output voltage
948 * @regulator: RPM regulator handle
949 * @min_uV: minimum required voltage in uV
950 * @max_uV: maximum acceptable voltage in uV
951 *
952 * Sets a voltage regulator to the desired output voltage. This can be set
953 * while the regulator is disabled or enabled. If the regulator is disabled,
954 * then rpm_regulator_set_voltage will both enable the regulator and set it to
955 * output at the requested voltage.
956 *
957 * The min_uV to max_uV voltage range requested must intersect with the
958 * voltage constraint range configured for the regulator.
959 *
960 * Returns 0 on success or errno on failure.
961 *
962 * The final voltage value that is sent to the RPM is aggregated based upon the
963 * values requested by all consumers of the regulator. This corresponds to the
964 * maximum min_uV value.
965 *
966 * This function may be called from either atomic or nonatomic context. This
967 * function may only be called for regulators which have the sleep_selectable
968 * flag set in their configuration data.
969 */
970int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
971 int max_uV)
972{
973 int rc = rpm_regulator_check_input(regulator);
974
975 if (rc)
976 return rc;
977
978 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, min_uV,
979 max_uV, regulator->sleep_also);
980
981 if (rc) {
982 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
983 } else {
984 regulator->min_uV = min_uV;
985 regulator->max_uV = max_uV;
986 }
987
988 return rc;
989}
990EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
991
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992static inline int vreg_hpm_min_uA(struct vreg *vreg)
993{
994 return vreg->hpm_min_load;
995}
996
997static inline int vreg_lpm_max_uA(struct vreg *vreg)
998{
999 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
1000}
1001
David Collins6f032ba2011-08-31 14:08:15 -07001002static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003{
David Collins6f032ba2011-08-31 14:08:15 -07001004 unsigned load_max
1005 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
1006
1007 return (load_uA > load_max ? load_max : load_uA);
1008}
1009
1010static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
1011{
1012 unsigned load_max
1013 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
1014 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015}
1016
1017/* Change vreg->req, but do not send it to the RPM. */
1018static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
1019 unsigned mask1, unsigned val1)
1020{
1021 unsigned long flags = 0;
1022
David Collins6f032ba2011-08-31 14:08:15 -07001023 if (vreg->pdata.sleep_selectable)
1024 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
1026 vreg->req[0].value &= ~mask0;
1027 vreg->req[0].value |= val0 & mask0;
1028
1029 vreg->req[1].value &= ~mask1;
1030 vreg->req[1].value |= val1 & mask1;
1031
David Collins6f032ba2011-08-31 14:08:15 -07001032 if (vreg->pdata.sleep_selectable)
1033 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034
1035 return 0;
1036}
1037
1038static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
1039 unsigned mask1, unsigned val1, unsigned cnt)
1040{
1041 unsigned prev0 = 0, prev1 = 0;
David Collins109a8e62012-07-31 15:21:35 -07001042 unsigned long flags = 0;
1043 bool tcxo_enabled = false;
1044 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 int rc;
1046
1047 /*
1048 * Bypass the normal route for regulators that can be called to change
1049 * just the active set values.
1050 */
David Collins6f032ba2011-08-31 14:08:15 -07001051 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
1053 mask0, val0, mask1, val1, cnt, 1);
1054
1055 prev0 = vreg->req[0].value;
1056 vreg->req[0].value &= ~mask0;
1057 vreg->req[0].value |= val0 & mask0;
1058
1059 prev1 = vreg->req[1].value;
1060 vreg->req[1].value &= ~mask1;
1061 vreg->req[1].value |= val1 & mask1;
1062
1063 /* Ignore duplicate requests */
1064 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
1065 vreg->req[1].value == vreg->prev_active_req[1].value) {
1066 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -07001067 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 return 0;
1069 }
1070
David Collins109a8e62012-07-31 15:21:35 -07001071 /* Enable CXO clock if necessary for TCXO workaround. */
1072 if (requires_tcxo_workaround && vreg->requires_cxo
1073 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
1074 if (!tcxo_handle)
1075 tcxo_get_handle();
1076 if (tcxo_workaround_noirq)
1077 spin_lock_irqsave(&tcxo_noirq_lock, flags);
1078 else
1079 mutex_lock(&tcxo_mutex);
1080
1081 voltage_increased = true;
1082 tcxo_enabled = tcxo_enable();
1083 }
1084
1085 if (voltage_increased && tcxo_workaround_noirq)
1086 rc = msm_rpmrs_set_noirq(MSM_RPM_CTX_SET_0, vreg->req, cnt);
1087 else
1088 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
1089
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 if (rc) {
1091 vreg->req[0].value = prev0;
1092 vreg->req[1].value = prev1;
1093
David Collins6f032ba2011-08-31 14:08:15 -07001094 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
1095 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096 } else {
1097 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -07001098 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 vreg->prev_active_req[0].value = vreg->req[0].value;
1100 vreg->prev_active_req[1].value = vreg->req[1].value;
1101 }
1102
David Collins109a8e62012-07-31 15:21:35 -07001103 /*
1104 * Schedule CXO clock to be disabled after TCXO warmup time if TCXO
1105 * workaround is applicable for this regulator.
1106 */
1107 if (voltage_increased) {
1108 if (tcxo_enabled)
1109 tcxo_delayed_disable();
1110
1111 if (tcxo_workaround_noirq)
1112 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
1113 else
1114 mutex_unlock(&tcxo_mutex);
1115 }
1116
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117 return rc;
1118}
1119
David Collins6f032ba2011-08-31 14:08:15 -07001120static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121{
David Collins6f032ba2011-08-31 14:08:15 -07001122 struct vreg *vreg = rdev_get_drvdata(rdev);
1123 int enabled;
1124
1125 mutex_lock(&vreg->pc_lock);
1126 enabled = vreg->is_enabled;
1127 mutex_unlock(&vreg->pc_lock);
1128
1129 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001130}
1131
David Collins6f032ba2011-08-31 14:08:15 -07001132static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001133{
David Collins6f032ba2011-08-31 14:08:15 -07001134 switch (vreg->type) {
1135 case RPM_REGULATOR_TYPE_LDO:
1136 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001137 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001138 /* Enable by setting a voltage. */
1139 if (vreg->part->uV.mask) {
1140 val[vreg->part->uV.word]
1141 |= vreg->save_uV << vreg->part->uV.shift;
1142 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1143 } else {
1144 val[vreg->part->mV.word]
1145 |= MICRO_TO_MILLI(vreg->save_uV)
1146 << vreg->part->mV.shift;
1147 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1148 }
1149 break;
1150 case RPM_REGULATOR_TYPE_VS:
1151 case RPM_REGULATOR_TYPE_NCP:
1152 /* Enable by setting enable_state. */
1153 val[vreg->part->enable_state.word]
1154 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
1155 mask[vreg->part->enable_state.word]
1156 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158}
1159
David Collins0ac31fe2012-02-08 13:53:34 -08001160static int rpm_vreg_enable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001161{
1162 struct vreg *vreg = rdev_get_drvdata(rdev);
1163 unsigned int mask[2] = {0}, val[2] = {0};
1164 int rc = 0;
1165
1166 set_enable(vreg, mask, val);
1167
1168 mutex_lock(&vreg->pc_lock);
1169
1170 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1171 vreg->part->request_len);
1172 if (!rc)
1173 vreg->is_enabled = true;
1174
1175 mutex_unlock(&vreg->pc_lock);
1176
1177 if (rc)
1178 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1179
1180 return rc;
1181}
1182
1183static void set_disable(struct vreg *vreg, unsigned int *mask,
1184 unsigned int *val)
1185{
1186 switch (vreg->type) {
1187 case RPM_REGULATOR_TYPE_LDO:
1188 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001189 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001190 /* Disable by setting a voltage of 0 uV. */
1191 if (vreg->part->uV.mask) {
1192 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
1193 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1194 } else {
1195 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
1196 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1197 }
1198 break;
1199 case RPM_REGULATOR_TYPE_VS:
1200 case RPM_REGULATOR_TYPE_NCP:
1201 /* Disable by setting enable_state. */
1202 val[vreg->part->enable_state.word]
1203 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
1204 mask[vreg->part->enable_state.word]
1205 |= vreg->part->enable_state.mask;
1206 }
1207}
1208
David Collins0ac31fe2012-02-08 13:53:34 -08001209static int rpm_vreg_disable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001210{
1211 struct vreg *vreg = rdev_get_drvdata(rdev);
1212 unsigned int mask[2] = {0}, val[2] = {0};
1213 int rc = 0;
1214
1215 set_disable(vreg, mask, val);
1216
1217 mutex_lock(&vreg->pc_lock);
1218
1219 /* Only disable if pin control is not in use. */
1220 if (!vreg->is_enabled_pc)
1221 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1222 vreg->part->request_len);
1223
1224 if (!rc)
1225 vreg->is_enabled = false;
1226
1227 mutex_unlock(&vreg->pc_lock);
1228
1229 if (rc)
1230 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1231
1232 return rc;
1233}
1234
1235static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236 unsigned *selector)
1237{
David Collins6f032ba2011-08-31 14:08:15 -07001238 struct vreg *vreg = rdev_get_drvdata(rdev);
1239 struct vreg_range *range = &vreg->set_points->range[0];
1240 unsigned int mask[2] = {0}, val[2] = {0};
1241 int rc = 0, uV = min_uV;
1242 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001243
David Collins6f032ba2011-08-31 14:08:15 -07001244 /* Check if request voltage is outside of physically settable range. */
1245 lim_min_uV = vreg->set_points->range[0].min_uV;
1246 lim_max_uV =
1247 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248
David Collins6f032ba2011-08-31 14:08:15 -07001249 if (uV < lim_min_uV && max_uV >= lim_min_uV)
1250 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251
David Collins6f032ba2011-08-31 14:08:15 -07001252 if (uV < lim_min_uV || uV > lim_max_uV) {
1253 vreg_err(vreg,
1254 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1255 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001256 return -EINVAL;
1257 }
1258
David Collins6f032ba2011-08-31 14:08:15 -07001259 /* Find the range which uV is inside of. */
1260 for (i = vreg->set_points->count - 1; i > 0; i--) {
1261 if (uV > vreg->set_points->range[i - 1].max_uV) {
1262 range = &vreg->set_points->range[i];
1263 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001264 }
1265 }
1266
David Collins6f032ba2011-08-31 14:08:15 -07001267 /*
1268 * Force uV to be an allowed set point and apply a ceiling function
1269 * to non-set point values.
1270 */
1271 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
1272 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273
David Collins3974b612011-11-21 15:07:36 -08001274 if (uV > max_uV) {
1275 vreg_err(vreg,
1276 "request v=[%d, %d] cannot be met by any set point; "
1277 "next set point: %d\n",
1278 min_uV, max_uV, uV);
1279 return -EINVAL;
1280 }
1281
David Collins0ac31fe2012-02-08 13:53:34 -08001282 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
1283 /*
1284 * Translate from enum values which work as inputs in the
1285 * regulator_set_voltage function to the actual corner values
1286 * sent to the RPM.
1287 */
1288 uV -= RPM_VREG_CORNER_NONE;
1289 }
1290
David Collins6f032ba2011-08-31 14:08:15 -07001291 if (vreg->part->uV.mask) {
1292 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
1293 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001294 } else {
David Collins6f032ba2011-08-31 14:08:15 -07001295 val[vreg->part->mV.word]
1296 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
1297 mask[vreg->part->mV.word] = vreg->part->mV.mask;
1298 }
1299
1300 mutex_lock(&vreg->pc_lock);
1301
1302 /*
1303 * Only send a request for a new voltage if the regulator is currently
1304 * enabled. This will ensure that LDO and SMPS regulators are not
1305 * inadvertently turned on because voltage > 0 is equivalent to
1306 * enabling. For NCP, this just removes unnecessary RPM requests.
1307 */
1308 if (vreg->is_enabled) {
1309 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1310 vreg->part->request_len);
1311 if (rc)
1312 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1313 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001315 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316 }
1317
David Collins6f032ba2011-08-31 14:08:15 -07001318 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
1319 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320
David Collins6f032ba2011-08-31 14:08:15 -07001321 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322
1323 return rc;
1324}
1325
David Collins6f032ba2011-08-31 14:08:15 -07001326static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327{
David Collins6f032ba2011-08-31 14:08:15 -07001328 struct vreg *vreg = rdev_get_drvdata(rdev);
1329
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 return vreg->save_uV;
1331}
1332
David Collins6f032ba2011-08-31 14:08:15 -07001333static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334{
David Collins6f032ba2011-08-31 14:08:15 -07001335 struct vreg *vreg = rdev_get_drvdata(rdev);
1336 int uV = 0;
1337 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338
David Collins6f032ba2011-08-31 14:08:15 -07001339 if (!vreg->set_points) {
1340 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 return -EINVAL;
1342 }
1343
David Collins6f032ba2011-08-31 14:08:15 -07001344 if (selector >= vreg->set_points->n_voltages)
1345 return 0;
1346
1347 for (i = 0; i < vreg->set_points->count; i++) {
1348 if (selector < vreg->set_points->range[i].n_voltages) {
1349 uV = selector * vreg->set_points->range[i].step_uV
1350 + vreg->set_points->range[i].min_uV;
1351 break;
1352 } else {
1353 selector -= vreg->set_points->range[i].n_voltages;
1354 }
1355 }
1356
1357 return uV;
1358}
1359
1360static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
1361{
1362 struct vreg *vreg = rdev_get_drvdata(rdev);
1363 unsigned int mask[2] = {0}, val[2] = {0};
1364 int rc = 0;
1365 int peak_uA;
1366
1367 mutex_lock(&vreg->pc_lock);
1368
1369 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
1370 & vreg->part->ip.mask) >> vreg->part->ip.shift);
1371
1372 if (mode == config->mode_hpm) {
1373 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001375 val[vreg->part->ip.word]
1376 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1377 << vreg->part->ip.shift;
1378 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1379
1380 if (config->ia_follows_ip) {
1381 val[vreg->part->ia.word]
1382 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1383 << vreg->part->ia.shift;
1384 mask[vreg->part->ia.word]
1385 |= vreg->part->ia.mask;
1386 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001387 }
David Collins6f032ba2011-08-31 14:08:15 -07001388 } else if (mode == config->mode_lpm) {
1389 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001390 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001391 val[vreg->part->ip.word]
1392 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1393 << vreg->part->ip.shift;
1394 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1395
1396 if (config->ia_follows_ip) {
1397 val[vreg->part->ia.word]
1398 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1399 << vreg->part->ia.shift;
1400 mask[vreg->part->ia.word]
1401 |= vreg->part->ia.mask;
1402 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 }
David Collins6f032ba2011-08-31 14:08:15 -07001404 } else {
1405 vreg_err(vreg, "invalid mode: %u\n", mode);
1406 mutex_unlock(&vreg->pc_lock);
1407 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001408 }
1409
David Collins6f032ba2011-08-31 14:08:15 -07001410 if (vreg->is_enabled) {
1411 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1412 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413 } else {
1414 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001415 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416 }
David Collins6f032ba2011-08-31 14:08:15 -07001417
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001419 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1420 else
1421 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001422
David Collins6f032ba2011-08-31 14:08:15 -07001423 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424
David Collins6f032ba2011-08-31 14:08:15 -07001425 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426}
1427
David Collins6f032ba2011-08-31 14:08:15 -07001428static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429{
David Collins6f032ba2011-08-31 14:08:15 -07001430 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431
David Collins6f032ba2011-08-31 14:08:15 -07001432 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433}
1434
David Collins6f032ba2011-08-31 14:08:15 -07001435static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
1436 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437{
David Collins6f032ba2011-08-31 14:08:15 -07001438 struct vreg *vreg = rdev_get_drvdata(rdev);
1439 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001440
David Collins6f032ba2011-08-31 14:08:15 -07001441 load_uA += vreg->pdata.system_uA;
1442
1443 mutex_lock(&vreg->pc_lock);
1444 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
1445 if (config->ia_follows_ip)
1446 SET_PART(vreg, ia,
1447 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
1448 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001449
1450 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -07001451 mode = config->mode_hpm;
1452 else
1453 mode = config->mode_lpm;
1454
1455 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456}
1457
David Collins6f032ba2011-08-31 14:08:15 -07001458static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
1459 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460{
David Collins6f032ba2011-08-31 14:08:15 -07001461 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001462
David Collins6f032ba2011-08-31 14:08:15 -07001463 if (MICRO_TO_MILLI(load_uA) <= 0) {
1464 /*
1465 * vreg_legacy_get_optimum_mode is being called before consumers
1466 * have specified their load currents via
1467 * regulator_set_optimum_mode. Return whatever the existing mode
1468 * is.
1469 */
1470 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001471 }
1472
David Collins6f032ba2011-08-31 14:08:15 -07001473 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474}
1475
1476/*
David Collins6f032ba2011-08-31 14:08:15 -07001477 * Returns the logical pin control enable state because the pin control options
1478 * present in the hardware out of restart could be different from those desired
1479 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480 */
David Collins6f032ba2011-08-31 14:08:15 -07001481static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482{
David Collins6f032ba2011-08-31 14:08:15 -07001483 struct vreg *vreg = rdev_get_drvdata(rdev);
1484
1485 return vreg->is_enabled_pc;
1486}
1487
1488static int vreg_pin_control_enable(struct regulator_dev *rdev)
1489{
1490 struct vreg *vreg = rdev_get_drvdata(rdev);
1491 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001492 int rc;
1493
David Collins6f032ba2011-08-31 14:08:15 -07001494 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495
David Collins6f032ba2011-08-31 14:08:15 -07001496 val[vreg->part->pc.word]
1497 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1498 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001499
David Collins6f032ba2011-08-31 14:08:15 -07001500 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1501 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001502
David Collins6f032ba2011-08-31 14:08:15 -07001503 if (!vreg->is_enabled)
1504 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001505
David Collins6f032ba2011-08-31 14:08:15 -07001506 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1507 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001508
David Collins6f032ba2011-08-31 14:08:15 -07001509 if (!rc)
1510 vreg->is_enabled_pc = true;
1511
1512 mutex_unlock(&vreg->pc_lock);
1513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001514 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001515 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001516
David Collins6f032ba2011-08-31 14:08:15 -07001517 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001518}
1519
David Collins6f032ba2011-08-31 14:08:15 -07001520static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001521{
David Collins6f032ba2011-08-31 14:08:15 -07001522 struct vreg *vreg = rdev_get_drvdata(rdev);
1523 unsigned int mask[2] = {0}, val[2] = {0};
1524 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001525
David Collins6f032ba2011-08-31 14:08:15 -07001526 mutex_lock(&vreg->pc_lock);
1527
1528 val[vreg->part->pc.word]
1529 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1530 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1531
1532 pin_fn = config->pin_func_none;
1533 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1534 pin_fn = config->pin_func_sleep_b;
1535 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1536 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1537
1538 if (!vreg->is_enabled)
1539 set_disable(vreg, mask, val);
1540
1541 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1542 vreg->part->request_len);
1543
1544 if (!rc)
1545 vreg->is_enabled_pc = false;
1546
1547 mutex_unlock(&vreg->pc_lock);
1548
1549 if (rc)
1550 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1551
1552 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553}
1554
David Collins6f032ba2011-08-31 14:08:15 -07001555static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001556{
David Collins6f032ba2011-08-31 14:08:15 -07001557 struct vreg *vreg = rdev_get_drvdata(rdev);
1558
1559 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001560}
1561
David Collins6f032ba2011-08-31 14:08:15 -07001562/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001563static struct regulator_ops ldo_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001564 .enable = rpm_vreg_enable,
1565 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001566 .is_enabled = vreg_is_enabled,
1567 .set_voltage = vreg_set_voltage,
1568 .get_voltage = vreg_get_voltage,
1569 .list_voltage = vreg_list_voltage,
1570 .set_mode = vreg_set_mode,
1571 .get_mode = vreg_get_mode,
1572 .get_optimum_mode = vreg_get_optimum_mode,
1573 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001574};
1575
1576static struct regulator_ops smps_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001577 .enable = rpm_vreg_enable,
1578 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001579 .is_enabled = vreg_is_enabled,
1580 .set_voltage = vreg_set_voltage,
1581 .get_voltage = vreg_get_voltage,
1582 .list_voltage = vreg_list_voltage,
1583 .set_mode = vreg_set_mode,
1584 .get_mode = vreg_get_mode,
1585 .get_optimum_mode = vreg_get_optimum_mode,
1586 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001587};
1588
1589static struct regulator_ops switch_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001590 .enable = rpm_vreg_enable,
1591 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001592 .is_enabled = vreg_is_enabled,
1593 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001594};
1595
1596static struct regulator_ops ncp_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001597 .enable = rpm_vreg_enable,
1598 .disable = rpm_vreg_disable,
1599 .is_enabled = vreg_is_enabled,
1600 .set_voltage = vreg_set_voltage,
1601 .get_voltage = vreg_get_voltage,
1602 .list_voltage = vreg_list_voltage,
1603 .enable_time = vreg_enable_time,
1604};
1605
1606static struct regulator_ops corner_ops = {
1607 .enable = rpm_vreg_enable,
1608 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001609 .is_enabled = vreg_is_enabled,
1610 .set_voltage = vreg_set_voltage,
1611 .get_voltage = vreg_get_voltage,
1612 .list_voltage = vreg_list_voltage,
1613 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614};
1615
David Collins6f032ba2011-08-31 14:08:15 -07001616/* Pin control regulator operations. */
1617static struct regulator_ops pin_control_ops = {
1618 .enable = vreg_pin_control_enable,
1619 .disable = vreg_pin_control_disable,
1620 .is_enabled = vreg_pin_control_is_enabled,
1621};
1622
1623struct regulator_ops *vreg_ops[] = {
1624 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1625 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1626 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1627 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
David Collins0ac31fe2012-02-08 13:53:34 -08001628 [RPM_REGULATOR_TYPE_CORNER] = &corner_ops,
David Collins6f032ba2011-08-31 14:08:15 -07001629};
1630
David Collins109a8e62012-07-31 15:21:35 -07001631static struct vreg *rpm_vreg_get_vreg(int id)
1632{
1633 struct vreg *vreg;
1634
1635 if (id < config->vreg_id_min || id > config->vreg_id_max)
1636 return NULL;
1637
1638 if (!config->is_real_id(id))
1639 id = config->pc_id_to_real_id(id);
1640 vreg = &config->vregs[id];
1641
1642 return vreg;
1643}
1644
David Collins6f032ba2011-08-31 14:08:15 -07001645static int __devinit
1646rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1647 struct device *dev)
1648{
1649 struct regulator_desc *rdesc = NULL;
1650 struct regulator_dev *rdev;
1651 struct vreg *vreg;
1652 unsigned pin_ctrl;
David Collins109a8e62012-07-31 15:21:35 -07001653 int pin_fn;
David Collins6f032ba2011-08-31 14:08:15 -07001654 int rc = 0;
1655
1656 if (!pdata) {
1657 pr_err("platform data missing\n");
1658 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001659 }
1660
David Collins109a8e62012-07-31 15:21:35 -07001661 vreg = rpm_vreg_get_vreg(pdata->id);
1662 if (!vreg) {
1663 pr_err("invalid regulator id: %d\n", pdata->id);
David Collins6f032ba2011-08-31 14:08:15 -07001664 return -ENODEV;
1665 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666
David Collins6f032ba2011-08-31 14:08:15 -07001667 if (config->is_real_id(pdata->id))
1668 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001669 else
David Collins6f032ba2011-08-31 14:08:15 -07001670 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001671
David Collins6f032ba2011-08-31 14:08:15 -07001672 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1673 pr_err("%s: invalid regulator type: %d\n",
1674 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001676 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677
David Collins6f032ba2011-08-31 14:08:15 -07001678 mutex_lock(&vreg->pc_lock);
1679
1680 if (vreg->set_points)
1681 rdesc->n_voltages = vreg->set_points->n_voltages;
1682 else
1683 rdesc->n_voltages = 0;
1684
1685 rdesc->id = pdata->id;
1686 rdesc->owner = THIS_MODULE;
1687 rdesc->type = REGULATOR_VOLTAGE;
1688
1689 if (config->is_real_id(pdata->id)) {
1690 /*
1691 * Real regulator; do not modify pin control and pin function
1692 * values.
1693 */
1694 rdesc->ops = vreg_ops[vreg->type];
1695 pin_ctrl = vreg->pdata.pin_ctrl;
1696 pin_fn = vreg->pdata.pin_fn;
1697 memcpy(&(vreg->pdata), pdata,
1698 sizeof(struct rpm_regulator_init_data));
1699 vreg->pdata.pin_ctrl = pin_ctrl;
1700 vreg->pdata.pin_fn = pin_fn;
1701
1702 vreg->save_uV = vreg->pdata.default_uV;
1703 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1704 vreg->mode = config->mode_hpm;
1705 else
1706 vreg->mode = config->mode_lpm;
1707
1708 /* Initialize the RPM request. */
1709 SET_PART(vreg, ip,
1710 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1711 SET_PART(vreg, fm, vreg->pdata.force_mode);
1712 SET_PART(vreg, pm, vreg->pdata.power_mode);
1713 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1714 SET_PART(vreg, ia,
1715 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1716 SET_PART(vreg, freq, vreg->pdata.freq);
1717 SET_PART(vreg, freq_clk_src, 0);
1718 SET_PART(vreg, comp_mode, 0);
1719 SET_PART(vreg, hpm, 0);
1720 if (!vreg->is_enabled_pc) {
1721 SET_PART(vreg, pf, config->pin_func_none);
1722 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1723 }
1724 } else {
1725 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1726 == RPM_VREG_PIN_CTRL_NONE
1727 && pdata->pin_fn != config->pin_func_sleep_b) {
1728 pr_err("%s: no pin control input specified\n",
1729 vreg->rdesc.name);
1730 mutex_unlock(&vreg->pc_lock);
1731 return -EINVAL;
1732 }
1733 rdesc->ops = &pin_control_ops;
1734 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1735 vreg->pdata.pin_fn = pdata->pin_fn;
1736
1737 /* Initialize the RPM request. */
1738 pin_fn = config->pin_func_none;
1739 /* Allow pf=sleep_b to be specified by platform data. */
1740 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1741 pin_fn = config->pin_func_sleep_b;
1742 SET_PART(vreg, pf, pin_fn);
1743 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1744 }
1745
1746 mutex_unlock(&vreg->pc_lock);
1747
1748 if (rc)
1749 goto bail;
1750
Rajendra Nayak11eafc62011-11-18 16:47:19 +05301751 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001752 if (IS_ERR(rdev)) {
1753 rc = PTR_ERR(rdev);
1754 pr_err("regulator_register failed: %s, rc=%d\n",
1755 vreg->rdesc.name, rc);
1756 return rc;
1757 } else {
1758 if (config->is_real_id(pdata->id))
1759 vreg->rdev = rdev;
1760 else
1761 vreg->rdev_pc = rdev;
1762 }
1763
1764bail:
1765 if (rc)
1766 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1767
1768 return rc;
1769}
1770
1771static void rpm_vreg_set_point_init(void)
1772{
1773 struct vreg_set_points **set_points;
1774 int i, j, temp;
1775
1776 set_points = config->set_points;
1777
1778 /* Calculate the number of set points available for each regulator. */
1779 for (i = 0; i < config->set_points_len; i++) {
1780 temp = 0;
1781 for (j = 0; j < set_points[i]->count; j++) {
1782 set_points[i]->range[j].n_voltages
1783 = (set_points[i]->range[j].max_uV
1784 - set_points[i]->range[j].min_uV)
1785 / set_points[i]->range[j].step_uV + 1;
1786 temp += set_points[i]->range[j].n_voltages;
1787 }
1788 set_points[i]->n_voltages = temp;
1789 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790}
1791
1792static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1793{
David Collins6f032ba2011-08-31 14:08:15 -07001794 struct rpm_regulator_platform_data *platform_data;
David Collins379b4b72012-05-14 16:33:51 -07001795 static struct rpm_regulator_consumer_mapping *prev_consumer_map;
1796 static int prev_consumer_map_len;
David Collins109a8e62012-07-31 15:21:35 -07001797 struct vreg *vreg;
David Collins6f032ba2011-08-31 14:08:15 -07001798 int rc = 0;
1799 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001800
David Collins6f032ba2011-08-31 14:08:15 -07001801 platform_data = pdev->dev.platform_data;
1802 if (!platform_data) {
1803 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001805 }
1806
David Collins6f032ba2011-08-31 14:08:15 -07001807 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1808 && platform_data->version != rpm_version) {
1809 pr_err("rpm version %d does not match previous version %d\n",
1810 platform_data->version, rpm_version);
1811 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 }
1813
David Collins6f032ba2011-08-31 14:08:15 -07001814 if (platform_data->version < 0
1815 || platform_data->version > RPM_VREG_VERSION_MAX) {
1816 pr_err("rpm version %d is invalid\n", platform_data->version);
1817 return -EINVAL;
1818 }
1819
1820 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1821 rpm_version = platform_data->version;
1822 config = get_config[platform_data->version]();
1823 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1824 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1825 if (!config) {
1826 pr_err("rpm version %d is not available\n",
1827 platform_data->version);
1828 return -ENODEV;
1829 }
1830 if (config->use_legacy_optimum_mode)
1831 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1832 vreg_ops[i]->get_optimum_mode
1833 = vreg_legacy_get_optimum_mode;
1834 rpm_vreg_set_point_init();
1835 /* First time probed; initialize pin control mutexes. */
1836 for (i = 0; i < config->vregs_len; i++)
1837 mutex_init(&config->vregs[i].pc_lock);
1838 }
1839
David Collins379b4b72012-05-14 16:33:51 -07001840 /* Copy the list of private API consumers. */
1841 if (platform_data->consumer_map_len > 0) {
1842 if (consumer_map_len == 0) {
1843 consumer_map_len = platform_data->consumer_map_len;
1844 consumer_map = kmemdup(platform_data->consumer_map,
1845 sizeof(struct rpm_regulator_consumer_mapping)
1846 * consumer_map_len, GFP_KERNEL);
1847 if (consumer_map == NULL) {
1848 pr_err("memory allocation failed\n");
1849 consumer_map_len = 0;
1850 return -ENOMEM;
1851 }
1852 } else {
1853 /* Concatenate new map with the existing one. */
1854 prev_consumer_map = consumer_map;
1855 prev_consumer_map_len = consumer_map_len;
1856 consumer_map_len += platform_data->consumer_map_len;
1857 consumer_map = kmalloc(
1858 sizeof(struct rpm_regulator_consumer_mapping)
1859 * consumer_map_len, GFP_KERNEL);
1860 if (consumer_map == NULL) {
1861 pr_err("memory allocation failed\n");
1862 consumer_map_len = 0;
1863 return -ENOMEM;
1864 }
1865 memcpy(consumer_map, prev_consumer_map,
1866 sizeof(struct rpm_regulator_consumer_mapping)
1867 * prev_consumer_map_len);
1868 memcpy(&consumer_map[prev_consumer_map_len],
1869 platform_data->consumer_map,
1870 sizeof(struct rpm_regulator_consumer_mapping)
1871 * platform_data->consumer_map_len);
1872 }
1873
1874 }
1875
David Collins109a8e62012-07-31 15:21:35 -07001876 if (platform_data->requires_tcxo_workaround
1877 && !requires_tcxo_workaround) {
1878 requires_tcxo_workaround = true;
1879 wake_lock_init(&tcxo_wake_lock, WAKE_LOCK_SUSPEND,
1880 "rpm_regulator_tcxo");
1881 }
1882
1883 if (requires_tcxo_workaround && !tcxo_workaround_noirq) {
1884 for (i = 0; i < platform_data->num_regulators; i++) {
1885 vreg = rpm_vreg_get_vreg(
1886 platform_data->init_data[i].id);
1887 if (vreg && vreg->requires_cxo
1888 && platform_data->init_data[i].sleep_selectable) {
1889 tcxo_workaround_noirq = true;
1890 break;
1891 }
1892 }
1893 }
1894
David Collins6f032ba2011-08-31 14:08:15 -07001895 /* Initialize all of the regulators listed in the platform data. */
1896 for (i = 0; i < platform_data->num_regulators; i++) {
1897 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1898 &pdev->dev);
1899 if (rc) {
1900 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1901 goto remove_regulators;
1902 }
1903 }
1904
1905 platform_set_drvdata(pdev, platform_data);
1906
1907 return rc;
1908
1909remove_regulators:
1910 /* Unregister all regulators added before the erroring one. */
1911 for (; i >= 0; i--) {
1912 id = platform_data->init_data[i].id;
1913 if (config->is_real_id(id)) {
1914 regulator_unregister(config->vregs[id].rdev);
1915 config->vregs[id].rdev = NULL;
1916 } else {
1917 regulator_unregister(config->vregs[
1918 config->pc_id_to_real_id(id)].rdev_pc);
1919 config->vregs[id].rdev_pc = NULL;
1920 }
1921 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922
1923 return rc;
1924}
1925
1926static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1927{
David Collins6f032ba2011-08-31 14:08:15 -07001928 struct rpm_regulator_platform_data *platform_data;
1929 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930
David Collins6f032ba2011-08-31 14:08:15 -07001931 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001932 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001933
1934 if (platform_data) {
1935 for (i = 0; i < platform_data->num_regulators; i++) {
1936 id = platform_data->init_data[i].id;
1937 if (config->is_real_id(id)) {
1938 regulator_unregister(config->vregs[id].rdev);
1939 config->vregs[id].rdev = NULL;
1940 } else {
1941 regulator_unregister(config->vregs[
1942 config->pc_id_to_real_id(id)].rdev_pc);
1943 config->vregs[id].rdev_pc = NULL;
1944 }
1945 }
1946 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001947
1948 return 0;
1949}
1950
1951static struct platform_driver rpm_vreg_driver = {
1952 .probe = rpm_vreg_probe,
1953 .remove = __devexit_p(rpm_vreg_remove),
1954 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001955 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956 .owner = THIS_MODULE,
1957 },
1958};
1959
1960static int __init rpm_vreg_init(void)
1961{
1962 return platform_driver_register(&rpm_vreg_driver);
1963}
1964
1965static void __exit rpm_vreg_exit(void)
1966{
David Collins6f032ba2011-08-31 14:08:15 -07001967 int i;
1968
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001969 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001970
David Collins379b4b72012-05-14 16:33:51 -07001971 kfree(consumer_map);
1972
David Collins6f032ba2011-08-31 14:08:15 -07001973 for (i = 0; i < config->vregs_len; i++)
1974 mutex_destroy(&config->vregs[i].pc_lock);
David Collins109a8e62012-07-31 15:21:35 -07001975
1976 if (tcxo_handle)
1977 clk_put(tcxo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001978}
1979
1980postcore_initcall(rpm_vreg_init);
1981module_exit(rpm_vreg_exit);
1982
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001984MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001985MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001986MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);