blob: 68ff55bc71debf3f5d20bae3281f7e096b4317c8 [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 Collins6f032ba2011-08-31 14:08:15 -070065};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066
David Collins379b4b72012-05-14 16:33:51 -070067static struct rpm_regulator_consumer_mapping *consumer_map;
68static int consumer_map_len;
69
David Collins6f032ba2011-08-31 14:08:15 -070070#define SET_PART(_vreg, _part, _val) \
71 _vreg->req[_vreg->part->_part.word].value \
72 = (_vreg->req[_vreg->part->_part.word].value \
David Collinsd8525e82011-11-21 14:54:25 -080073 & ~_vreg->part->_part.mask) \
74 | (((_val) << _vreg->part->_part.shift) \
75 & _vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076
David Collins6f032ba2011-08-31 14:08:15 -070077#define GET_PART(_vreg, _part) \
David Collinsd8525e82011-11-21 14:54:25 -080078 ((_vreg->req[_vreg->part->_part.word].value & _vreg->part->_part.mask) \
79 >> _vreg->part->_part.shift)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080
David Collins109a8e62012-07-31 15:21:35 -070081#define GET_PART_PREV_ACT(_vreg, _part) \
82 ((_vreg->prev_active_req[_vreg->part->_part.word].value \
83 & _vreg->part->_part.mask) \
84 >> _vreg->part->_part.shift)
85
David Collinsd8525e82011-11-21 14:54:25 -080086#define USES_PART(_vreg, _part) (_vreg->part->_part.mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087
David Collins6f032ba2011-08-31 14:08:15 -070088#define vreg_err(vreg, fmt, ...) \
89 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090
David Collins6f032ba2011-08-31 14:08:15 -070091#define RPM_VREG_PIN_CTRL_EN0 0x01
92#define RPM_VREG_PIN_CTRL_EN1 0x02
93#define RPM_VREG_PIN_CTRL_EN2 0x04
94#define RPM_VREG_PIN_CTRL_EN3 0x08
95#define RPM_VREG_PIN_CTRL_ALL 0x0F
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096
David Collins6f032ba2011-08-31 14:08:15 -070097static const char *label_freq[] = {
98 [RPM_VREG_FREQ_NONE] = " N/A",
99 [RPM_VREG_FREQ_19p20] = "19.2",
100 [RPM_VREG_FREQ_9p60] = "9.60",
101 [RPM_VREG_FREQ_6p40] = "6.40",
102 [RPM_VREG_FREQ_4p80] = "4.80",
103 [RPM_VREG_FREQ_3p84] = "3.84",
104 [RPM_VREG_FREQ_3p20] = "3.20",
105 [RPM_VREG_FREQ_2p74] = "2.74",
106 [RPM_VREG_FREQ_2p40] = "2.40",
107 [RPM_VREG_FREQ_2p13] = "2.13",
108 [RPM_VREG_FREQ_1p92] = "1.92",
109 [RPM_VREG_FREQ_1p75] = "1.75",
110 [RPM_VREG_FREQ_1p60] = "1.60",
111 [RPM_VREG_FREQ_1p48] = "1.48",
112 [RPM_VREG_FREQ_1p37] = "1.37",
113 [RPM_VREG_FREQ_1p28] = "1.28",
114 [RPM_VREG_FREQ_1p20] = "1.20",
115};
David Collins0ac31fe2012-02-08 13:53:34 -0800116
117static const char *label_corner[] = {
118 [RPM_VREG_CORNER_NONE] = "NONE",
119 [RPM_VREG_CORNER_LOW] = "LOW",
120 [RPM_VREG_CORNER_NOMINAL] = "NOM",
121 [RPM_VREG_CORNER_HIGH] = "HIGH",
122};
123
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124/*
125 * This is used when voting for LPM or HPM by subtracting or adding to the
126 * hpm_min_load of a regulator. It has units of uA.
127 */
David Collins6f032ba2011-08-31 14:08:15 -0700128#define LOAD_THRESHOLD_STEP 1000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129
David Collins6f032ba2011-08-31 14:08:15 -0700130/* rpm_version keeps track of the version for the currently running driver. */
131enum rpm_vreg_version rpm_version = -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132
David Collins6f032ba2011-08-31 14:08:15 -0700133/* config holds all configuration data of the currently running driver. */
134static struct vreg_config *config;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135
David Collins6f032ba2011-08-31 14:08:15 -0700136/* These regulator ID values are specified in the board file. */
137static int vreg_id_vdd_mem, vreg_id_vdd_dig;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138
David Collins6f032ba2011-08-31 14:08:15 -0700139static inline int vreg_id_is_vdd_mem_or_dig(int id)
140{
141 return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
142}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143
David Collins6f032ba2011-08-31 14:08:15 -0700144#define DEBUG_PRINT_BUFFER_SIZE 512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145
David Collins6f032ba2011-08-31 14:08:15 -0700146static void rpm_regulator_req(struct vreg *vreg, int set)
147{
148 int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
149 const char *pf_label = "", *fm_label = "", *pc_total = "";
150 const char *pc_en[4] = {"", "", "", ""};
David Collins0ac31fe2012-02-08 13:53:34 -0800151 const char *pm_label = "", *freq_label = "", *corner_label = "";
David Collins6f032ba2011-08-31 14:08:15 -0700152 char buf[DEBUG_PRINT_BUFFER_SIZE];
153 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
154 int pos = 0;
155
156 /* Suppress VDD_MEM and VDD_DIG printing. */
157 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
158 && vreg_id_is_vdd_mem_or_dig(vreg->id))
159 return;
160
161 uV = GET_PART(vreg, uV);
162 mV = GET_PART(vreg, mV);
163 if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
164 uV = -uV;
165 mV = -mV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 }
167
David Collins6f032ba2011-08-31 14:08:15 -0700168 fm = GET_PART(vreg, fm);
169 pm = GET_PART(vreg, pm);
170 pc = GET_PART(vreg, pc);
171 pf = GET_PART(vreg, pf);
172 pd = GET_PART(vreg, pd);
173 freq = GET_PART(vreg, freq);
174 state = GET_PART(vreg, enable_state);
175
176 if (pf >= 0 && pf < config->label_pin_func_len)
177 pf_label = config->label_pin_func[pf];
178
179 if (fm >= 0 && fm < config->label_force_mode_len)
180 fm_label = config->label_force_mode[fm];
181
182 if (pm >= 0 && pm < config->label_power_mode_len)
183 pm_label = config->label_power_mode[pm];
184
185 if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
186 freq_label = label_freq[freq];
187
188 for (i = 0; i < config->label_pin_ctrl_len; i++)
189 if (pc & (1 << i))
190 pc_en[i] = config->label_pin_ctrl[i];
191
192 if (pc == RPM_VREG_PIN_CTRL_NONE)
193 pc_total = " none";
194
195 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
196 KERN_INFO, __func__);
197
198 pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
199 (set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
200 vreg->rdesc.name,
201 (set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
202
David Collins0ac31fe2012-02-08 13:53:34 -0800203 if (USES_PART(vreg, uV) && vreg->type != RPM_REGULATOR_TYPE_CORNER)
David Collins6f032ba2011-08-31 14:08:15 -0700204 pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
205 if (USES_PART(vreg, mV))
206 pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
207 if (USES_PART(vreg, enable_state))
208 pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
209 (state == 1 ? "on" : "off"), state);
210 if (USES_PART(vreg, ip))
211 pos += scnprintf(buf + pos, buflen - pos,
212 ", ip=%4d mA", GET_PART(vreg, ip));
213 if (USES_PART(vreg, fm))
214 pos += scnprintf(buf + pos, buflen - pos,
215 ", fm=%s (%d)", fm_label, fm);
216 if (USES_PART(vreg, pc))
217 pos += scnprintf(buf + pos, buflen - pos,
218 ", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
219 pc_en[2], pc_en[3], pc_total, pc);
220 if (USES_PART(vreg, pf))
221 pos += scnprintf(buf + pos, buflen - pos,
222 ", pf=%s (%d)", pf_label, pf);
223 if (USES_PART(vreg, pd))
224 pos += scnprintf(buf + pos, buflen - pos,
225 ", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
226 if (USES_PART(vreg, ia))
227 pos += scnprintf(buf + pos, buflen - pos,
228 ", ia=%4d mA", GET_PART(vreg, ia));
229 if (USES_PART(vreg, freq)) {
230 if (vreg->type == RPM_REGULATOR_TYPE_NCP)
231 pos += scnprintf(buf + pos, buflen - pos,
232 ", freq=%2d", freq);
233 else
234 pos += scnprintf(buf + pos, buflen - pos,
235 ", freq=%s MHz (%2d)", freq_label, freq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 }
David Collins6f032ba2011-08-31 14:08:15 -0700237 if (USES_PART(vreg, pm))
238 pos += scnprintf(buf + pos, buflen - pos,
239 ", pm=%s (%d)", pm_label, pm);
240 if (USES_PART(vreg, freq_clk_src))
241 pos += scnprintf(buf + pos, buflen - pos,
242 ", clk_src=%d", GET_PART(vreg, freq_clk_src));
243 if (USES_PART(vreg, comp_mode))
244 pos += scnprintf(buf + pos, buflen - pos,
245 ", comp=%d", GET_PART(vreg, comp_mode));
246 if (USES_PART(vreg, hpm))
247 pos += scnprintf(buf + pos, buflen - pos,
248 ", hpm=%d", GET_PART(vreg, hpm));
David Collins0ac31fe2012-02-08 13:53:34 -0800249 if (USES_PART(vreg, uV) && vreg->type == RPM_REGULATOR_TYPE_CORNER) {
250 if (uV >= 0 && uV < (ARRAY_SIZE(label_corner) - 1))
251 corner_label = label_corner[uV+1];
252 pos += scnprintf(buf + pos, buflen - pos, ", corner=%s (%d)",
253 corner_label, uV);
254 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255
David Collins6f032ba2011-08-31 14:08:15 -0700256 pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
257 vreg->req[0].id, vreg->req[0].value);
258 if (vreg->part->request_len > 1)
259 pos += scnprintf(buf + pos, buflen - pos,
260 ", req[1]={%d, 0x%08X}", vreg->req[1].id,
261 vreg->req[1].value);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262
David Collins6f032ba2011-08-31 14:08:15 -0700263 pos += scnprintf(buf + pos, buflen - pos, "\n");
264 printk(buf);
265}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266
David Collins6f032ba2011-08-31 14:08:15 -0700267static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
268 int set, int voter_uV, int aggregate_uV)
269{
270 /* Suppress VDD_MEM and VDD_DIG printing. */
271 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
272 && vreg_id_is_vdd_mem_or_dig(vreg->id))
273 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
David Collins6f032ba2011-08-31 14:08:15 -0700275 pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
276 "v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
277 (set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
278}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279
David Collins6f032ba2011-08-31 14:08:15 -0700280static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
281{
282 /* Suppress VDD_MEM and VDD_DIG printing. */
283 if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
284 && vreg_id_is_vdd_mem_or_dig(vreg->id))
285 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286
David Collins6f032ba2011-08-31 14:08:15 -0700287 if (cnt == 2)
288 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
289 "req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
290 (set == 0 ? 'A' : 'S'),
291 vreg->req[0].id, vreg->req[0].value,
292 vreg->req[1].id, vreg->req[1].value);
293 else if (cnt == 1)
294 pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
295 vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
296 vreg->req[0].id, vreg->req[0].value);
297}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298
David Collins109a8e62012-07-31 15:21:35 -0700299static bool requires_tcxo_workaround;
300static bool tcxo_workaround_noirq;
301static struct clk *tcxo_handle;
302static struct wake_lock tcxo_wake_lock;
303static DEFINE_MUTEX(tcxo_mutex);
304/* Spin lock needed for sleep-selectable regulators. */
305static DEFINE_SPINLOCK(tcxo_noirq_lock);
306static bool tcxo_is_enabled;
307/*
308 * TCXO must be kept on for at least the duration of its warmup (4 ms);
309 * otherwise, it will stay on when hardware disabling is attempted.
310 */
311#define TCXO_WARMUP_TIME_MS 4
312
313static void tcxo_get_handle(void)
314{
315 int rc;
316
317 if (!tcxo_handle) {
318 tcxo_handle = clk_get_sys("rpm-regulator", "vref_buff");
319 if (IS_ERR(tcxo_handle)) {
320 tcxo_handle = NULL;
321 } else {
322 rc = clk_prepare(tcxo_handle);
323 if (rc) {
324 clk_put(tcxo_handle);
325 tcxo_handle = NULL;
326 }
327 }
328 }
329}
330
331/*
332 * Perform best effort enable of CXO. Since the MSM clock drivers depend upon
333 * the rpm-regulator driver, any rpm-regulator devices that are configured with
334 * always_on == 1 will not be able to enable CXO during probe. This does not
335 * cause a problem though since CXO will be enabled by the boot loaders before
336 * Apps boots up.
337 */
338static bool tcxo_enable(void)
339{
340 int rc;
341
342 if (tcxo_handle && !tcxo_is_enabled) {
343 rc = clk_enable(tcxo_handle);
344 if (!rc) {
345 tcxo_is_enabled = true;
346 wake_lock(&tcxo_wake_lock);
347 return true;
348 }
349 }
350
351 return false;
352}
353
354static void tcxo_delayed_disable_work(struct work_struct *work)
355{
356 unsigned long flags = 0;
357
358 if (tcxo_workaround_noirq)
359 spin_lock_irqsave(&tcxo_noirq_lock, flags);
360 else
361 mutex_lock(&tcxo_mutex);
362
363 clk_disable(tcxo_handle);
364 tcxo_is_enabled = false;
365 wake_unlock(&tcxo_wake_lock);
366
367 if (tcxo_workaround_noirq)
368 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
369 else
370 mutex_unlock(&tcxo_mutex);
371}
372
373static DECLARE_DELAYED_WORK(tcxo_disable_work, tcxo_delayed_disable_work);
374
375static void tcxo_delayed_disable(void)
376{
377 /*
378 * The delay in jiffies has 1 added to it to ensure that at least
379 * one jiffy takes place before the work is enqueued. Without this,
380 * the work would be scheduled to run in the very next jiffy which could
381 * result in too little delay and TCXO being stuck on.
382 */
383 if (tcxo_handle)
384 schedule_delayed_work(&tcxo_disable_work,
385 msecs_to_jiffies(TCXO_WARMUP_TIME_MS) + 1);
386}
387
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388/* Spin lock needed for sleep-selectable regulators. */
David Collins6f032ba2011-08-31 14:08:15 -0700389static DEFINE_SPINLOCK(rpm_noirq_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390
391static int voltage_from_req(struct vreg *vreg)
392{
David Collins6f032ba2011-08-31 14:08:15 -0700393 int uV = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394
David Collins6f032ba2011-08-31 14:08:15 -0700395 if (vreg->part->uV.mask)
396 uV = GET_PART(vreg, uV);
David Collins13397f22012-02-06 13:53:29 -0800397 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700398 uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
David Collins13397f22012-02-06 13:53:29 -0800399 else if (vreg->part->enable_state.mask)
400 uV = GET_PART(vreg, enable_state);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401
David Collins6f032ba2011-08-31 14:08:15 -0700402 return uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403}
404
David Collins6f032ba2011-08-31 14:08:15 -0700405static void voltage_to_req(int uV, struct vreg *vreg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406{
David Collins6f032ba2011-08-31 14:08:15 -0700407 if (vreg->part->uV.mask)
408 SET_PART(vreg, uV, uV);
David Collins13397f22012-02-06 13:53:29 -0800409 else if (vreg->part->mV.mask)
David Collins6f032ba2011-08-31 14:08:15 -0700410 SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
David Collins13397f22012-02-06 13:53:29 -0800411 else if (vreg->part->enable_state.mask)
412 SET_PART(vreg, enable_state, uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413}
414
415static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
416 int set, unsigned mask0, unsigned val0,
417 unsigned mask1, unsigned val1, unsigned cnt,
418 int update_voltage)
419{
420 struct msm_rpm_iv_pair *prev_req;
David Collins6f032ba2011-08-31 14:08:15 -0700421 int rc = 0, max_uV_vote = 0;
David Collins109a8e62012-07-31 15:21:35 -0700422 unsigned long flags = 0;
423 bool tcxo_enabled = false;
424 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 unsigned prev0, prev1;
David Collins6f032ba2011-08-31 14:08:15 -0700426 int *min_uV_vote;
427 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428
429 if (set == MSM_RPM_CTX_SET_0) {
David Collins6f032ba2011-08-31 14:08:15 -0700430 min_uV_vote = vreg->active_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 prev_req = vreg->prev_active_req;
432 } else {
David Collins6f032ba2011-08-31 14:08:15 -0700433 min_uV_vote = vreg->sleep_min_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434 prev_req = vreg->prev_sleep_req;
435 }
436
437 prev0 = vreg->req[0].value;
438 vreg->req[0].value &= ~mask0;
439 vreg->req[0].value |= val0 & mask0;
440
441 prev1 = vreg->req[1].value;
442 vreg->req[1].value &= ~mask1;
443 vreg->req[1].value |= val1 & mask1;
444
David Collinsd1b7aea2012-03-19 10:27:55 -0700445 /* Set the force mode field based on which set is being requested. */
446 if (set == MSM_RPM_CTX_SET_0)
447 SET_PART(vreg, fm, vreg->pdata.force_mode);
448 else
449 SET_PART(vreg, fm, vreg->pdata.sleep_set_force_mode);
450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 if (update_voltage)
David Collins6f032ba2011-08-31 14:08:15 -0700452 min_uV_vote[voter] = voltage_from_req(vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
454 /* Find the highest voltage voted for and use it. */
455 for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
David Collins6f032ba2011-08-31 14:08:15 -0700456 max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
457 voltage_to_req(max_uV_vote, vreg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
459 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
David Collins6f032ba2011-08-31 14:08:15 -0700460 rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
461 max_uV_vote);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700462
463 /* Ignore duplicate requests */
464 if (vreg->req[0].value != prev_req[0].value ||
465 vreg->req[1].value != prev_req[1].value) {
David Collins109a8e62012-07-31 15:21:35 -0700466
467 /* Enable CXO clock if necessary for TCXO workaround. */
468 if (requires_tcxo_workaround && vreg->requires_cxo
469 && (set == MSM_RPM_CTX_SET_0)
470 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
471 voltage_increased = true;
472 spin_lock_irqsave(&tcxo_noirq_lock, flags);
473 tcxo_enabled = tcxo_enable();
474 }
475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476 rc = msm_rpmrs_set_noirq(set, vreg->req, cnt);
477 if (rc) {
478 vreg->req[0].value = prev0;
479 vreg->req[1].value = prev1;
480
David Collins6f032ba2011-08-31 14:08:15 -0700481 vreg_err(vreg, "msm_rpmrs_set_noirq failed - "
482 "set=%s, id=%d, rc=%d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 (set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
484 vreg->req[0].id, rc);
485 } else {
486 /* Only save if nonzero and active set. */
David Collins6f032ba2011-08-31 14:08:15 -0700487 if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
488 vreg->save_uV = max_uV_vote;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 if (msm_rpm_vreg_debug_mask
490 & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -0700491 rpm_regulator_req(vreg, set);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 prev_req[0].value = vreg->req[0].value;
493 prev_req[1].value = vreg->req[1].value;
494 }
David Collins109a8e62012-07-31 15:21:35 -0700495
496 /*
497 * Schedule CXO clock to be disabled after TCXO warmup time if
498 * TCXO workaround is applicable for this regulator.
499 */
500 if (voltage_increased) {
501 if (tcxo_enabled)
502 tcxo_delayed_disable();
503 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
504 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 } else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
David Collins6f032ba2011-08-31 14:08:15 -0700506 rpm_regulator_duplicate(vreg, set, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 }
508
509 return rc;
510}
511
512static int vreg_set_noirq(struct vreg *vreg, enum rpm_vreg_voter voter,
513 int sleep, unsigned mask0, unsigned val0,
514 unsigned mask1, unsigned val1, unsigned cnt,
515 int update_voltage)
516{
David Collins6f032ba2011-08-31 14:08:15 -0700517 unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518 unsigned long flags;
519 int rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520
521 if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
522 return -EINVAL;
523
David Collins6f032ba2011-08-31 14:08:15 -0700524 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525
526 /*
527 * Send sleep set request first so that subsequent set_mode, etc calls
528 * use the voltage from the active set.
529 */
530 if (sleep)
531 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
532 mask0, val0, mask1, val1, cnt, update_voltage);
533 else {
534 /*
535 * Vote for 0 V in the sleep set when active set-only is
536 * specified. This ensures that a disable vote will be issued
537 * at some point for the sleep set of the regulator.
538 */
David Collins6f032ba2011-08-31 14:08:15 -0700539 if (vreg->part->uV.mask) {
540 s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
541 s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800542 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700543 s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
544 s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800545 } else if (vreg->part->enable_state.mask) {
546 s_val[vreg->part->enable_state.word]
547 = 0 << vreg->part->enable_state.shift;
548 s_mask[vreg->part->enable_state.word]
549 = vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 }
551
552 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
David Collins6f032ba2011-08-31 14:08:15 -0700553 s_mask[0], s_val[0], s_mask[1], s_val[1],
554 cnt, update_voltage);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 }
556
557 rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
558 mask1, val1, cnt, update_voltage);
559
David Collins6f032ba2011-08-31 14:08:15 -0700560 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
562 return rc;
563}
564
565/**
566 * rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
567 * @vreg: ID for regulator
568 * @voter: ID for the voter
569 * @min_uV: minimum acceptable voltage (in uV) that is voted for
570 * @max_uV: maximum acceptable voltage (in uV) that is voted for
571 * @sleep_also: 0 for active set only, non-0 for active set and sleep set
572 *
573 * Returns 0 on success or errno.
574 *
575 * This function is used to vote for the voltage of a regulator without
576 * using the regulator framework. It is needed by consumers which hold spin
577 * locks or have interrupts disabled because the regulator framework can sleep.
578 * It is also needed by consumers which wish to only vote for active set
579 * regulator voltage.
580 *
581 * If sleep_also == 0, then a sleep-set value of 0V will be voted for.
582 *
583 * This function may only be called for regulators which have the sleep flag
584 * specified in their private data.
David Collins7462b9d2011-10-11 16:02:17 -0700585 *
586 * Consumers can vote to disable a regulator with this function by passing
587 * min_uV = 0 and max_uV = 0.
David Collins13397f22012-02-06 13:53:29 -0800588 *
589 * Voltage switch type regulators may be controlled via rpm_vreg_set_voltage
590 * as well. For this type of regulator, max_uV > 0 is treated as an enable
591 * request and max_uV == 0 is treated as a disable request.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592 */
David Collins6f032ba2011-08-31 14:08:15 -0700593int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
594 int max_uV, int sleep_also)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595{
David Collins6f032ba2011-08-31 14:08:15 -0700596 unsigned int mask[2] = {0}, val[2] = {0};
597 struct vreg_range *range;
598 struct vreg *vreg;
599 int uV = min_uV;
600 int lim_min_uV, lim_max_uV, i, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601
David Collins6f032ba2011-08-31 14:08:15 -0700602 if (!config) {
603 pr_err("rpm-regulator driver has not probed yet.\n");
604 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 }
606
David Collins6f032ba2011-08-31 14:08:15 -0700607 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
608 pr_err("invalid regulator id=%d\n", vreg_id);
609 return -EINVAL;
610 }
611
612 vreg = &config->vregs[vreg_id];
David Collins6f032ba2011-08-31 14:08:15 -0700613
614 if (!vreg->pdata.sleep_selectable) {
615 vreg_err(vreg, "regulator is not marked sleep selectable\n");
616 return -EINVAL;
617 }
618
David Collins7462b9d2011-10-11 16:02:17 -0700619 /* Allow min_uV == max_uV == 0 to represent a disable request. */
David Collins13397f22012-02-06 13:53:29 -0800620 if ((min_uV != 0 || max_uV != 0)
621 && (vreg->part->uV.mask || vreg->part->mV.mask)) {
David Collins7462b9d2011-10-11 16:02:17 -0700622 /*
623 * Check if request voltage is outside of allowed range. The
624 * regulator core has already checked that constraint range
625 * is inside of the physically allowed range.
626 */
627 lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
628 lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700629
David Collins7462b9d2011-10-11 16:02:17 -0700630 if (uV < lim_min_uV && max_uV >= lim_min_uV)
631 uV = lim_min_uV;
David Collins6f032ba2011-08-31 14:08:15 -0700632
David Collins7462b9d2011-10-11 16:02:17 -0700633 if (uV < lim_min_uV || uV > lim_max_uV) {
634 vreg_err(vreg, "request v=[%d, %d] is outside allowed "
635 "v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
636 lim_max_uV);
637 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -0700638 }
David Collins6f032ba2011-08-31 14:08:15 -0700639
David Collins13397f22012-02-06 13:53:29 -0800640 range = &vreg->set_points->range[0];
David Collins7462b9d2011-10-11 16:02:17 -0700641 /* Find the range which uV is inside of. */
642 for (i = vreg->set_points->count - 1; i > 0; i--) {
643 if (uV > vreg->set_points->range[i - 1].max_uV) {
644 range = &vreg->set_points->range[i];
645 break;
646 }
647 }
648
649 /*
650 * Force uV to be an allowed set point and apply a ceiling
651 * function to non-set point values.
652 */
653 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
654 uV = uV * range->step_uV + range->min_uV;
David Collins3974b612011-11-21 15:07:36 -0800655
656 if (uV > max_uV) {
657 vreg_err(vreg,
658 "request v=[%d, %d] cannot be met by any set point; "
659 "next set point: %d\n",
660 min_uV, max_uV, uV);
661 return -EINVAL;
662 }
David Collins7462b9d2011-10-11 16:02:17 -0700663 }
David Collins6f032ba2011-08-31 14:08:15 -0700664
David Collins0ac31fe2012-02-08 13:53:34 -0800665 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
666 /*
667 * Translate from enum values which work as inputs in the
668 * rpm_vreg_set_voltage function to the actual corner values
669 * sent to the RPM.
670 */
671 if (uV > 0)
672 uV -= RPM_VREG_CORNER_NONE;
673 }
674
David Collins6f032ba2011-08-31 14:08:15 -0700675 if (vreg->part->uV.mask) {
676 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
677 mask[vreg->part->uV.word] = vreg->part->uV.mask;
David Collins13397f22012-02-06 13:53:29 -0800678 } else if (vreg->part->mV.mask) {
David Collins6f032ba2011-08-31 14:08:15 -0700679 val[vreg->part->mV.word]
680 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
681 mask[vreg->part->mV.word] = vreg->part->mV.mask;
David Collins13397f22012-02-06 13:53:29 -0800682 } else if (vreg->part->enable_state.mask) {
683 /*
684 * Translate max_uV > 0 into an enable request for regulator
685 * types which to not support voltage setting, e.g. voltage
686 * switches.
687 */
688 val[vreg->part->enable_state.word]
689 = (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift;
690 mask[vreg->part->enable_state.word]
691 = vreg->part->enable_state.mask;
David Collins6f032ba2011-08-31 14:08:15 -0700692 }
693
694 rc = vreg_set_noirq(vreg, voter, sleep_also, mask[0], val[0], mask[1],
695 val[1], vreg->part->request_len, 1);
696 if (rc)
697 vreg_err(vreg, "vreg_set_noirq failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698
699 return rc;
700}
701EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
702
703/**
704 * rpm_vreg_set_frequency - sets the frequency of a switching regulator
705 * @vreg: ID for regulator
David Collins6f032ba2011-08-31 14:08:15 -0700706 * @freq: enum corresponding to desired frequency
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707 *
708 * Returns 0 on success or errno.
709 */
David Collins6f032ba2011-08-31 14:08:15 -0700710int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711{
David Collins6f032ba2011-08-31 14:08:15 -0700712 unsigned int mask[2] = {0}, val[2] = {0};
713 struct vreg *vreg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 int rc;
715
David Collins6f032ba2011-08-31 14:08:15 -0700716 if (!config) {
717 pr_err("rpm-regulator driver has not probed yet.\n");
718 return -ENODEV;
719 }
720
721 if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
722 pr_err("invalid regulator id=%d\n", vreg_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 return -EINVAL;
724 }
725
David Collins6f032ba2011-08-31 14:08:15 -0700726 vreg = &config->vregs[vreg_id];
727
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
David Collins6f032ba2011-08-31 14:08:15 -0700729 vreg_err(vreg, "invalid frequency=%d\n", freq);
730 return -EINVAL;
731 }
732 if (!vreg->pdata.sleep_selectable) {
733 vreg_err(vreg, "regulator is not marked sleep selectable\n");
734 return -EINVAL;
735 }
736 if (!vreg->part->freq.mask) {
737 vreg_err(vreg, "frequency not supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 return -EINVAL;
739 }
740
David Collins6f032ba2011-08-31 14:08:15 -0700741 val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
742 mask[vreg->part->freq.word] = vreg->part->freq.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743
David Collins6f032ba2011-08-31 14:08:15 -0700744 rc = vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
745 val[0], mask[1], val[1], vreg->part->request_len, 0);
746 if (rc)
747 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748
749 return rc;
750}
751EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
752
David Collins379b4b72012-05-14 16:33:51 -0700753#define MAX_NAME_LEN 64
754/**
755 * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
756 * @dev: device for regulator consumer
757 * @supply: supply name
758 *
759 * Returns a struct rpm_regulator corresponding to the regulator producer,
760 * or ERR_PTR() containing errno.
761 *
762 * This function may only be called from nonatomic context. The mapping between
763 * <dev, supply> tuples and rpm_regulators struct pointers is specified via
764 * rpm-regulator platform data.
765 */
766struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
767{
768 struct rpm_regulator_consumer_mapping *mapping = NULL;
769 const char *devname = NULL;
770 struct rpm_regulator *regulator;
771 int i;
772
773 if (!config) {
774 pr_err("rpm-regulator driver has not probed yet.\n");
775 return ERR_PTR(-ENODEV);
776 }
777
778 if (consumer_map == NULL || consumer_map_len == 0) {
779 pr_err("No private consumer mapping has been specified.\n");
780 return ERR_PTR(-ENODEV);
781 }
782
783 if (supply == NULL) {
784 pr_err("supply name must be specified\n");
785 return ERR_PTR(-EINVAL);
786 }
787
788 if (dev)
789 devname = dev_name(dev);
790
791 for (i = 0; i < consumer_map_len; i++) {
792 /* If the mapping has a device set up it must match */
793 if (consumer_map[i].dev_name &&
794 (!devname || strncmp(consumer_map[i].dev_name, devname,
795 MAX_NAME_LEN)))
796 continue;
797
798 if (strncmp(consumer_map[i].supply, supply, MAX_NAME_LEN)
799 == 0) {
800 mapping = &consumer_map[i];
801 break;
802 }
803 }
804
805 if (mapping == NULL) {
806 pr_err("could not find mapping for dev=%s, supply=%s\n",
807 (devname ? devname : "(null)"), supply);
808 return ERR_PTR(-ENODEV);
809 }
810
811 regulator = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
812 if (regulator == NULL) {
813 pr_err("could not allocate memory for regulator\n");
814 return ERR_PTR(-ENOMEM);
815 }
816
817 regulator->vreg_id = mapping->vreg_id;
818 regulator->voter = mapping->voter;
819 regulator->sleep_also = mapping->sleep_also;
820
821 return regulator;
822}
823EXPORT_SYMBOL_GPL(rpm_regulator_get);
824
825static int rpm_regulator_check_input(struct rpm_regulator *regulator)
826{
827 int rc = 0;
828
829 if (regulator == NULL) {
830 rc = -EINVAL;
831 pr_err("invalid (null) rpm_regulator pointer\n");
832 } else if (IS_ERR(regulator)) {
833 rc = PTR_ERR(regulator);
834 pr_err("invalid rpm_regulator pointer, rc=%d\n", rc);
835 }
836
837 return rc;
838}
839
840/**
841 * rpm_regulator_put() - free the RPM regulator handle
842 * @regulator: RPM regulator handle
843 *
844 * Parameter reaggregation does not take place when rpm_regulator_put is called.
845 * Therefore, regulator enable state and voltage must be configured
846 * appropriately before calling rpm_regulator_put.
847 *
848 * This function may be called from either atomic or nonatomic context.
849 */
850void rpm_regulator_put(struct rpm_regulator *regulator)
851{
852 kfree(regulator);
853}
854EXPORT_SYMBOL_GPL(rpm_regulator_put);
855
856/**
857 * rpm_regulator_enable() - enable regulator output
858 * @regulator: RPM regulator handle
859 *
860 * Returns 0 on success or errno on failure.
861 *
862 * This function may be called from either atomic or nonatomic context. This
863 * function may only be called for regulators which have the sleep_selectable
864 * flag set in their configuration data.
865 *
866 * rpm_regulator_set_voltage must be called before rpm_regulator_enable because
867 * enabling is defined by the RPM interface to be requesting the desired
868 * non-zero regulator output voltage.
869 */
870int rpm_regulator_enable(struct rpm_regulator *regulator)
871{
872 int rc = rpm_regulator_check_input(regulator);
873 struct vreg *vreg;
874
875 if (rc)
876 return rc;
877
878 if (regulator->vreg_id < config->vreg_id_min
879 || regulator->vreg_id > config->vreg_id_max) {
880 pr_err("invalid regulator id=%d\n", regulator->vreg_id);
881 return -EINVAL;
882 }
883
884 vreg = &config->vregs[regulator->vreg_id];
885
886 /*
887 * Handle voltage switches which can be enabled without
888 * rpm_regulator_set_voltage ever being called.
889 */
890 if (regulator->min_uV == 0 && regulator->max_uV == 0
891 && vreg->part->uV.mask == 0 && vreg->part->mV.mask == 0) {
892 regulator->min_uV = 1;
893 regulator->max_uV = 1;
894 }
895
896 if (regulator->min_uV == 0 && regulator->max_uV == 0) {
897 pr_err("Voltage must be set with rpm_regulator_set_voltage "
898 "before calling rpm_regulator_enable; vreg_id=%d, "
899 "voter=%d\n", regulator->vreg_id, regulator->voter);
900 return -EINVAL;
901 }
902
903 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter,
904 regulator->min_uV, regulator->max_uV, regulator->sleep_also);
905
906 if (rc)
907 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
908
909 return rc;
910}
911EXPORT_SYMBOL_GPL(rpm_regulator_enable);
912
913/**
914 * rpm_regulator_disable() - disable regulator output
915 * @regulator: RPM regulator handle
916 *
917 * Returns 0 on success or errno on failure.
918 *
919 * The enable state of the regulator is determined by aggregating the requests
920 * of all consumers. Therefore, it is possible that the regulator will remain
921 * enabled even after rpm_regulator_disable is called.
922 *
923 * This function may be called from either atomic or nonatomic context. This
924 * function may only be called for regulators which have the sleep_selectable
925 * flag set in their configuration data.
926 */
927int rpm_regulator_disable(struct rpm_regulator *regulator)
928{
929 int rc = rpm_regulator_check_input(regulator);
930
931 if (rc)
932 return rc;
933
934 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, 0, 0,
935 regulator->sleep_also);
936
937 if (rc)
938 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
939
940 return rc;
941}
942EXPORT_SYMBOL_GPL(rpm_regulator_disable);
943
944/**
945 * rpm_regulator_set_voltage() - set regulator output voltage
946 * @regulator: RPM regulator handle
947 * @min_uV: minimum required voltage in uV
948 * @max_uV: maximum acceptable voltage in uV
949 *
950 * Sets a voltage regulator to the desired output voltage. This can be set
951 * while the regulator is disabled or enabled. If the regulator is disabled,
952 * then rpm_regulator_set_voltage will both enable the regulator and set it to
953 * output at the requested voltage.
954 *
955 * The min_uV to max_uV voltage range requested must intersect with the
956 * voltage constraint range configured for the regulator.
957 *
958 * Returns 0 on success or errno on failure.
959 *
960 * The final voltage value that is sent to the RPM is aggregated based upon the
961 * values requested by all consumers of the regulator. This corresponds to the
962 * maximum min_uV value.
963 *
964 * This function may be called from either atomic or nonatomic context. This
965 * function may only be called for regulators which have the sleep_selectable
966 * flag set in their configuration data.
967 */
968int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
969 int max_uV)
970{
971 int rc = rpm_regulator_check_input(regulator);
972
973 if (rc)
974 return rc;
975
976 rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, min_uV,
977 max_uV, regulator->sleep_also);
978
979 if (rc) {
980 pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
981 } else {
982 regulator->min_uV = min_uV;
983 regulator->max_uV = max_uV;
984 }
985
986 return rc;
987}
988EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
989
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990static inline int vreg_hpm_min_uA(struct vreg *vreg)
991{
992 return vreg->hpm_min_load;
993}
994
995static inline int vreg_lpm_max_uA(struct vreg *vreg)
996{
997 return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
998}
999
David Collins6f032ba2011-08-31 14:08:15 -07001000static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001{
David Collins6f032ba2011-08-31 14:08:15 -07001002 unsigned load_max
1003 = MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
1004
1005 return (load_uA > load_max ? load_max : load_uA);
1006}
1007
1008static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
1009{
1010 unsigned load_max
1011 = MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
1012 return (load_uA > load_max ? load_max : load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013}
1014
1015/* Change vreg->req, but do not send it to the RPM. */
1016static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
1017 unsigned mask1, unsigned val1)
1018{
1019 unsigned long flags = 0;
1020
David Collins6f032ba2011-08-31 14:08:15 -07001021 if (vreg->pdata.sleep_selectable)
1022 spin_lock_irqsave(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023
1024 vreg->req[0].value &= ~mask0;
1025 vreg->req[0].value |= val0 & mask0;
1026
1027 vreg->req[1].value &= ~mask1;
1028 vreg->req[1].value |= val1 & mask1;
1029
David Collins6f032ba2011-08-31 14:08:15 -07001030 if (vreg->pdata.sleep_selectable)
1031 spin_unlock_irqrestore(&rpm_noirq_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032
1033 return 0;
1034}
1035
1036static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
1037 unsigned mask1, unsigned val1, unsigned cnt)
1038{
1039 unsigned prev0 = 0, prev1 = 0;
David Collins109a8e62012-07-31 15:21:35 -07001040 unsigned long flags = 0;
1041 bool tcxo_enabled = false;
1042 bool voltage_increased = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 int rc;
1044
1045 /*
1046 * Bypass the normal route for regulators that can be called to change
1047 * just the active set values.
1048 */
David Collins6f032ba2011-08-31 14:08:15 -07001049 if (vreg->pdata.sleep_selectable)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 return vreg_set_noirq(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
1051 mask0, val0, mask1, val1, cnt, 1);
1052
1053 prev0 = vreg->req[0].value;
1054 vreg->req[0].value &= ~mask0;
1055 vreg->req[0].value |= val0 & mask0;
1056
1057 prev1 = vreg->req[1].value;
1058 vreg->req[1].value &= ~mask1;
1059 vreg->req[1].value |= val1 & mask1;
1060
1061 /* Ignore duplicate requests */
1062 if (vreg->req[0].value == vreg->prev_active_req[0].value &&
1063 vreg->req[1].value == vreg->prev_active_req[1].value) {
1064 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
David Collins6f032ba2011-08-31 14:08:15 -07001065 rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066 return 0;
1067 }
1068
David Collins109a8e62012-07-31 15:21:35 -07001069 /* Enable CXO clock if necessary for TCXO workaround. */
1070 if (requires_tcxo_workaround && vreg->requires_cxo
1071 && (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
1072 if (!tcxo_handle)
1073 tcxo_get_handle();
1074 if (tcxo_workaround_noirq)
1075 spin_lock_irqsave(&tcxo_noirq_lock, flags);
1076 else
1077 mutex_lock(&tcxo_mutex);
1078
1079 voltage_increased = true;
1080 tcxo_enabled = tcxo_enable();
1081 }
1082
1083 if (voltage_increased && tcxo_workaround_noirq)
1084 rc = msm_rpmrs_set_noirq(MSM_RPM_CTX_SET_0, vreg->req, cnt);
1085 else
1086 rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
1087
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088 if (rc) {
1089 vreg->req[0].value = prev0;
1090 vreg->req[1].value = prev1;
1091
David Collins6f032ba2011-08-31 14:08:15 -07001092 vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
1093 vreg->req[0].id, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 } else {
1095 if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
David Collins6f032ba2011-08-31 14:08:15 -07001096 rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 vreg->prev_active_req[0].value = vreg->req[0].value;
1098 vreg->prev_active_req[1].value = vreg->req[1].value;
1099 }
1100
David Collins109a8e62012-07-31 15:21:35 -07001101 /*
1102 * Schedule CXO clock to be disabled after TCXO warmup time if TCXO
1103 * workaround is applicable for this regulator.
1104 */
1105 if (voltage_increased) {
1106 if (tcxo_enabled)
1107 tcxo_delayed_disable();
1108
1109 if (tcxo_workaround_noirq)
1110 spin_unlock_irqrestore(&tcxo_noirq_lock, flags);
1111 else
1112 mutex_unlock(&tcxo_mutex);
1113 }
1114
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115 return rc;
1116}
1117
David Collins6f032ba2011-08-31 14:08:15 -07001118static int vreg_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119{
David Collins6f032ba2011-08-31 14:08:15 -07001120 struct vreg *vreg = rdev_get_drvdata(rdev);
1121 int enabled;
1122
1123 mutex_lock(&vreg->pc_lock);
1124 enabled = vreg->is_enabled;
1125 mutex_unlock(&vreg->pc_lock);
1126
1127 return enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128}
1129
David Collins6f032ba2011-08-31 14:08:15 -07001130static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131{
David Collins6f032ba2011-08-31 14:08:15 -07001132 switch (vreg->type) {
1133 case RPM_REGULATOR_TYPE_LDO:
1134 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001135 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001136 /* Enable by setting a voltage. */
1137 if (vreg->part->uV.mask) {
1138 val[vreg->part->uV.word]
1139 |= vreg->save_uV << vreg->part->uV.shift;
1140 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1141 } else {
1142 val[vreg->part->mV.word]
1143 |= MICRO_TO_MILLI(vreg->save_uV)
1144 << vreg->part->mV.shift;
1145 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1146 }
1147 break;
1148 case RPM_REGULATOR_TYPE_VS:
1149 case RPM_REGULATOR_TYPE_NCP:
1150 /* Enable by setting enable_state. */
1151 val[vreg->part->enable_state.word]
1152 |= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
1153 mask[vreg->part->enable_state.word]
1154 |= vreg->part->enable_state.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001155 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156}
1157
David Collins0ac31fe2012-02-08 13:53:34 -08001158static int rpm_vreg_enable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001159{
1160 struct vreg *vreg = rdev_get_drvdata(rdev);
1161 unsigned int mask[2] = {0}, val[2] = {0};
1162 int rc = 0;
1163
1164 set_enable(vreg, mask, val);
1165
1166 mutex_lock(&vreg->pc_lock);
1167
1168 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1169 vreg->part->request_len);
1170 if (!rc)
1171 vreg->is_enabled = true;
1172
1173 mutex_unlock(&vreg->pc_lock);
1174
1175 if (rc)
1176 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1177
1178 return rc;
1179}
1180
1181static void set_disable(struct vreg *vreg, unsigned int *mask,
1182 unsigned int *val)
1183{
1184 switch (vreg->type) {
1185 case RPM_REGULATOR_TYPE_LDO:
1186 case RPM_REGULATOR_TYPE_SMPS:
David Collins0ac31fe2012-02-08 13:53:34 -08001187 case RPM_REGULATOR_TYPE_CORNER:
David Collins6f032ba2011-08-31 14:08:15 -07001188 /* Disable by setting a voltage of 0 uV. */
1189 if (vreg->part->uV.mask) {
1190 val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
1191 mask[vreg->part->uV.word] |= vreg->part->uV.mask;
1192 } else {
1193 val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
1194 mask[vreg->part->mV.word] |= vreg->part->mV.mask;
1195 }
1196 break;
1197 case RPM_REGULATOR_TYPE_VS:
1198 case RPM_REGULATOR_TYPE_NCP:
1199 /* Disable by setting enable_state. */
1200 val[vreg->part->enable_state.word]
1201 |= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
1202 mask[vreg->part->enable_state.word]
1203 |= vreg->part->enable_state.mask;
1204 }
1205}
1206
David Collins0ac31fe2012-02-08 13:53:34 -08001207static int rpm_vreg_disable(struct regulator_dev *rdev)
David Collins6f032ba2011-08-31 14:08:15 -07001208{
1209 struct vreg *vreg = rdev_get_drvdata(rdev);
1210 unsigned int mask[2] = {0}, val[2] = {0};
1211 int rc = 0;
1212
1213 set_disable(vreg, mask, val);
1214
1215 mutex_lock(&vreg->pc_lock);
1216
1217 /* Only disable if pin control is not in use. */
1218 if (!vreg->is_enabled_pc)
1219 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1220 vreg->part->request_len);
1221
1222 if (!rc)
1223 vreg->is_enabled = false;
1224
1225 mutex_unlock(&vreg->pc_lock);
1226
1227 if (rc)
1228 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1229
1230 return rc;
1231}
1232
1233static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234 unsigned *selector)
1235{
David Collins6f032ba2011-08-31 14:08:15 -07001236 struct vreg *vreg = rdev_get_drvdata(rdev);
1237 struct vreg_range *range = &vreg->set_points->range[0];
1238 unsigned int mask[2] = {0}, val[2] = {0};
1239 int rc = 0, uV = min_uV;
1240 int lim_min_uV, lim_max_uV, i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241
David Collins6f032ba2011-08-31 14:08:15 -07001242 /* Check if request voltage is outside of physically settable range. */
1243 lim_min_uV = vreg->set_points->range[0].min_uV;
1244 lim_max_uV =
1245 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246
David Collins6f032ba2011-08-31 14:08:15 -07001247 if (uV < lim_min_uV && max_uV >= lim_min_uV)
1248 uV = lim_min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001249
David Collins6f032ba2011-08-31 14:08:15 -07001250 if (uV < lim_min_uV || uV > lim_max_uV) {
1251 vreg_err(vreg,
1252 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
1253 min_uV, max_uV, lim_min_uV, lim_max_uV);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254 return -EINVAL;
1255 }
1256
David Collins6f032ba2011-08-31 14:08:15 -07001257 /* Find the range which uV is inside of. */
1258 for (i = vreg->set_points->count - 1; i > 0; i--) {
1259 if (uV > vreg->set_points->range[i - 1].max_uV) {
1260 range = &vreg->set_points->range[i];
1261 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262 }
1263 }
1264
David Collins6f032ba2011-08-31 14:08:15 -07001265 /*
1266 * Force uV to be an allowed set point and apply a ceiling function
1267 * to non-set point values.
1268 */
1269 uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
1270 uV = uV * range->step_uV + range->min_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271
David Collins3974b612011-11-21 15:07:36 -08001272 if (uV > max_uV) {
1273 vreg_err(vreg,
1274 "request v=[%d, %d] cannot be met by any set point; "
1275 "next set point: %d\n",
1276 min_uV, max_uV, uV);
1277 return -EINVAL;
1278 }
1279
David Collins0ac31fe2012-02-08 13:53:34 -08001280 if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
1281 /*
1282 * Translate from enum values which work as inputs in the
1283 * regulator_set_voltage function to the actual corner values
1284 * sent to the RPM.
1285 */
1286 uV -= RPM_VREG_CORNER_NONE;
1287 }
1288
David Collins6f032ba2011-08-31 14:08:15 -07001289 if (vreg->part->uV.mask) {
1290 val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
1291 mask[vreg->part->uV.word] = vreg->part->uV.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 } else {
David Collins6f032ba2011-08-31 14:08:15 -07001293 val[vreg->part->mV.word]
1294 = MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
1295 mask[vreg->part->mV.word] = vreg->part->mV.mask;
1296 }
1297
1298 mutex_lock(&vreg->pc_lock);
1299
1300 /*
1301 * Only send a request for a new voltage if the regulator is currently
1302 * enabled. This will ensure that LDO and SMPS regulators are not
1303 * inadvertently turned on because voltage > 0 is equivalent to
1304 * enabling. For NCP, this just removes unnecessary RPM requests.
1305 */
1306 if (vreg->is_enabled) {
1307 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1308 vreg->part->request_len);
1309 if (rc)
1310 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1311 } else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001313 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314 }
1315
David Collins6f032ba2011-08-31 14:08:15 -07001316 if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
1317 vreg->save_uV = uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318
David Collins6f032ba2011-08-31 14:08:15 -07001319 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320
1321 return rc;
1322}
1323
David Collins6f032ba2011-08-31 14:08:15 -07001324static int vreg_get_voltage(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325{
David Collins6f032ba2011-08-31 14:08:15 -07001326 struct vreg *vreg = rdev_get_drvdata(rdev);
1327
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 return vreg->save_uV;
1329}
1330
David Collins6f032ba2011-08-31 14:08:15 -07001331static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332{
David Collins6f032ba2011-08-31 14:08:15 -07001333 struct vreg *vreg = rdev_get_drvdata(rdev);
1334 int uV = 0;
1335 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001336
David Collins6f032ba2011-08-31 14:08:15 -07001337 if (!vreg->set_points) {
1338 vreg_err(vreg, "no voltages available\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339 return -EINVAL;
1340 }
1341
David Collins6f032ba2011-08-31 14:08:15 -07001342 if (selector >= vreg->set_points->n_voltages)
1343 return 0;
1344
1345 for (i = 0; i < vreg->set_points->count; i++) {
1346 if (selector < vreg->set_points->range[i].n_voltages) {
1347 uV = selector * vreg->set_points->range[i].step_uV
1348 + vreg->set_points->range[i].min_uV;
1349 break;
1350 } else {
1351 selector -= vreg->set_points->range[i].n_voltages;
1352 }
1353 }
1354
1355 return uV;
1356}
1357
1358static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
1359{
1360 struct vreg *vreg = rdev_get_drvdata(rdev);
1361 unsigned int mask[2] = {0}, val[2] = {0};
1362 int rc = 0;
1363 int peak_uA;
1364
1365 mutex_lock(&vreg->pc_lock);
1366
1367 peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
1368 & vreg->part->ip.mask) >> vreg->part->ip.shift);
1369
1370 if (mode == config->mode_hpm) {
1371 /* Make sure that request currents are in HPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001372 if (peak_uA < vreg_hpm_min_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001373 val[vreg->part->ip.word]
1374 = MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1375 << vreg->part->ip.shift;
1376 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1377
1378 if (config->ia_follows_ip) {
1379 val[vreg->part->ia.word]
1380 |= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
1381 << vreg->part->ia.shift;
1382 mask[vreg->part->ia.word]
1383 |= vreg->part->ia.mask;
1384 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385 }
David Collins6f032ba2011-08-31 14:08:15 -07001386 } else if (mode == config->mode_lpm) {
1387 /* Make sure that request currents are in LPM range. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388 if (peak_uA > vreg_lpm_max_uA(vreg)) {
David Collins6f032ba2011-08-31 14:08:15 -07001389 val[vreg->part->ip.word]
1390 = MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1391 << vreg->part->ip.shift;
1392 mask[vreg->part->ip.word] = vreg->part->ip.mask;
1393
1394 if (config->ia_follows_ip) {
1395 val[vreg->part->ia.word]
1396 |= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
1397 << vreg->part->ia.shift;
1398 mask[vreg->part->ia.word]
1399 |= vreg->part->ia.mask;
1400 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001401 }
David Collins6f032ba2011-08-31 14:08:15 -07001402 } else {
1403 vreg_err(vreg, "invalid mode: %u\n", mode);
1404 mutex_unlock(&vreg->pc_lock);
1405 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001406 }
1407
David Collins6f032ba2011-08-31 14:08:15 -07001408 if (vreg->is_enabled) {
1409 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1410 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411 } else {
1412 /* Regulator is disabled; store but don't send new request. */
David Collins6f032ba2011-08-31 14:08:15 -07001413 rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414 }
David Collins6f032ba2011-08-31 14:08:15 -07001415
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001417 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1418 else
1419 vreg->mode = mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420
David Collins6f032ba2011-08-31 14:08:15 -07001421 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001422
David Collins6f032ba2011-08-31 14:08:15 -07001423 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424}
1425
David Collins6f032ba2011-08-31 14:08:15 -07001426static unsigned int vreg_get_mode(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427{
David Collins6f032ba2011-08-31 14:08:15 -07001428 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429
David Collins6f032ba2011-08-31 14:08:15 -07001430 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431}
1432
David Collins6f032ba2011-08-31 14:08:15 -07001433static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
1434 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435{
David Collins6f032ba2011-08-31 14:08:15 -07001436 struct vreg *vreg = rdev_get_drvdata(rdev);
1437 unsigned int mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001438
David Collins6f032ba2011-08-31 14:08:15 -07001439 load_uA += vreg->pdata.system_uA;
1440
1441 mutex_lock(&vreg->pc_lock);
1442 SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
1443 if (config->ia_follows_ip)
1444 SET_PART(vreg, ia,
1445 MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
1446 mutex_unlock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447
1448 if (load_uA >= vreg->hpm_min_load)
David Collins6f032ba2011-08-31 14:08:15 -07001449 mode = config->mode_hpm;
1450 else
1451 mode = config->mode_lpm;
1452
1453 return mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001454}
1455
David Collins6f032ba2011-08-31 14:08:15 -07001456static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
1457 int input_uV, int output_uV, int load_uA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001458{
David Collins6f032ba2011-08-31 14:08:15 -07001459 struct vreg *vreg = rdev_get_drvdata(rdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460
David Collins6f032ba2011-08-31 14:08:15 -07001461 if (MICRO_TO_MILLI(load_uA) <= 0) {
1462 /*
1463 * vreg_legacy_get_optimum_mode is being called before consumers
1464 * have specified their load currents via
1465 * regulator_set_optimum_mode. Return whatever the existing mode
1466 * is.
1467 */
1468 return vreg->mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001469 }
1470
David Collins6f032ba2011-08-31 14:08:15 -07001471 return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001472}
1473
1474/*
David Collins6f032ba2011-08-31 14:08:15 -07001475 * Returns the logical pin control enable state because the pin control options
1476 * present in the hardware out of restart could be different from those desired
1477 * by the consumer.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478 */
David Collins6f032ba2011-08-31 14:08:15 -07001479static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480{
David Collins6f032ba2011-08-31 14:08:15 -07001481 struct vreg *vreg = rdev_get_drvdata(rdev);
1482
1483 return vreg->is_enabled_pc;
1484}
1485
1486static int vreg_pin_control_enable(struct regulator_dev *rdev)
1487{
1488 struct vreg *vreg = rdev_get_drvdata(rdev);
1489 unsigned int mask[2] = {0}, val[2] = {0};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 int rc;
1491
David Collins6f032ba2011-08-31 14:08:15 -07001492 mutex_lock(&vreg->pc_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001493
David Collins6f032ba2011-08-31 14:08:15 -07001494 val[vreg->part->pc.word]
1495 |= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
1496 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497
David Collins6f032ba2011-08-31 14:08:15 -07001498 val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
1499 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001500
David Collins6f032ba2011-08-31 14:08:15 -07001501 if (!vreg->is_enabled)
1502 set_enable(vreg, mask, val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503
David Collins6f032ba2011-08-31 14:08:15 -07001504 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1505 vreg->part->request_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506
David Collins6f032ba2011-08-31 14:08:15 -07001507 if (!rc)
1508 vreg->is_enabled_pc = true;
1509
1510 mutex_unlock(&vreg->pc_lock);
1511
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001512 if (rc)
David Collins6f032ba2011-08-31 14:08:15 -07001513 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001514
David Collins6f032ba2011-08-31 14:08:15 -07001515 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001516}
1517
David Collins6f032ba2011-08-31 14:08:15 -07001518static int vreg_pin_control_disable(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001519{
David Collins6f032ba2011-08-31 14:08:15 -07001520 struct vreg *vreg = rdev_get_drvdata(rdev);
1521 unsigned int mask[2] = {0}, val[2] = {0};
1522 int pin_fn, rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523
David Collins6f032ba2011-08-31 14:08:15 -07001524 mutex_lock(&vreg->pc_lock);
1525
1526 val[vreg->part->pc.word]
1527 |= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
1528 mask[vreg->part->pc.word] |= vreg->part->pc.mask;
1529
1530 pin_fn = config->pin_func_none;
1531 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1532 pin_fn = config->pin_func_sleep_b;
1533 val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
1534 mask[vreg->part->pf.word] |= vreg->part->pf.mask;
1535
1536 if (!vreg->is_enabled)
1537 set_disable(vreg, mask, val);
1538
1539 rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
1540 vreg->part->request_len);
1541
1542 if (!rc)
1543 vreg->is_enabled_pc = false;
1544
1545 mutex_unlock(&vreg->pc_lock);
1546
1547 if (rc)
1548 vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
1549
1550 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001551}
1552
David Collins6f032ba2011-08-31 14:08:15 -07001553static int vreg_enable_time(struct regulator_dev *rdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554{
David Collins6f032ba2011-08-31 14:08:15 -07001555 struct vreg *vreg = rdev_get_drvdata(rdev);
1556
1557 return vreg->pdata.enable_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558}
1559
David Collins6f032ba2011-08-31 14:08:15 -07001560/* Real regulator operations. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001561static struct regulator_ops ldo_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001562 .enable = rpm_vreg_enable,
1563 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001564 .is_enabled = vreg_is_enabled,
1565 .set_voltage = vreg_set_voltage,
1566 .get_voltage = vreg_get_voltage,
1567 .list_voltage = vreg_list_voltage,
1568 .set_mode = vreg_set_mode,
1569 .get_mode = vreg_get_mode,
1570 .get_optimum_mode = vreg_get_optimum_mode,
1571 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001572};
1573
1574static struct regulator_ops smps_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001575 .enable = rpm_vreg_enable,
1576 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001577 .is_enabled = vreg_is_enabled,
1578 .set_voltage = vreg_set_voltage,
1579 .get_voltage = vreg_get_voltage,
1580 .list_voltage = vreg_list_voltage,
1581 .set_mode = vreg_set_mode,
1582 .get_mode = vreg_get_mode,
1583 .get_optimum_mode = vreg_get_optimum_mode,
1584 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585};
1586
1587static struct regulator_ops switch_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001588 .enable = rpm_vreg_enable,
1589 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001590 .is_enabled = vreg_is_enabled,
1591 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001592};
1593
1594static struct regulator_ops ncp_ops = {
David Collins0ac31fe2012-02-08 13:53:34 -08001595 .enable = rpm_vreg_enable,
1596 .disable = rpm_vreg_disable,
1597 .is_enabled = vreg_is_enabled,
1598 .set_voltage = vreg_set_voltage,
1599 .get_voltage = vreg_get_voltage,
1600 .list_voltage = vreg_list_voltage,
1601 .enable_time = vreg_enable_time,
1602};
1603
1604static struct regulator_ops corner_ops = {
1605 .enable = rpm_vreg_enable,
1606 .disable = rpm_vreg_disable,
David Collins6f032ba2011-08-31 14:08:15 -07001607 .is_enabled = vreg_is_enabled,
1608 .set_voltage = vreg_set_voltage,
1609 .get_voltage = vreg_get_voltage,
1610 .list_voltage = vreg_list_voltage,
1611 .enable_time = vreg_enable_time,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001612};
1613
David Collins6f032ba2011-08-31 14:08:15 -07001614/* Pin control regulator operations. */
1615static struct regulator_ops pin_control_ops = {
1616 .enable = vreg_pin_control_enable,
1617 .disable = vreg_pin_control_disable,
1618 .is_enabled = vreg_pin_control_is_enabled,
1619};
1620
1621struct regulator_ops *vreg_ops[] = {
1622 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1623 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1624 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1625 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
David Collins0ac31fe2012-02-08 13:53:34 -08001626 [RPM_REGULATOR_TYPE_CORNER] = &corner_ops,
David Collins6f032ba2011-08-31 14:08:15 -07001627};
1628
David Collins109a8e62012-07-31 15:21:35 -07001629static struct vreg *rpm_vreg_get_vreg(int id)
1630{
1631 struct vreg *vreg;
1632
1633 if (id < config->vreg_id_min || id > config->vreg_id_max)
1634 return NULL;
1635
1636 if (!config->is_real_id(id))
1637 id = config->pc_id_to_real_id(id);
1638 vreg = &config->vregs[id];
1639
1640 return vreg;
1641}
1642
David Collins6f032ba2011-08-31 14:08:15 -07001643static int __devinit
1644rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
1645 struct device *dev)
1646{
1647 struct regulator_desc *rdesc = NULL;
1648 struct regulator_dev *rdev;
1649 struct vreg *vreg;
1650 unsigned pin_ctrl;
David Collins109a8e62012-07-31 15:21:35 -07001651 int pin_fn;
David Collins6f032ba2011-08-31 14:08:15 -07001652 int rc = 0;
1653
1654 if (!pdata) {
1655 pr_err("platform data missing\n");
1656 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001657 }
1658
David Collins109a8e62012-07-31 15:21:35 -07001659 vreg = rpm_vreg_get_vreg(pdata->id);
1660 if (!vreg) {
1661 pr_err("invalid regulator id: %d\n", pdata->id);
David Collins6f032ba2011-08-31 14:08:15 -07001662 return -ENODEV;
1663 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001664
David Collins6f032ba2011-08-31 14:08:15 -07001665 if (config->is_real_id(pdata->id))
1666 rdesc = &vreg->rdesc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667 else
David Collins6f032ba2011-08-31 14:08:15 -07001668 rdesc = &vreg->rdesc_pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001669
David Collins6f032ba2011-08-31 14:08:15 -07001670 if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
1671 pr_err("%s: invalid regulator type: %d\n",
1672 vreg->rdesc.name, vreg->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001673 return -EINVAL;
David Collins6f032ba2011-08-31 14:08:15 -07001674 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675
David Collins6f032ba2011-08-31 14:08:15 -07001676 mutex_lock(&vreg->pc_lock);
1677
1678 if (vreg->set_points)
1679 rdesc->n_voltages = vreg->set_points->n_voltages;
1680 else
1681 rdesc->n_voltages = 0;
1682
1683 rdesc->id = pdata->id;
1684 rdesc->owner = THIS_MODULE;
1685 rdesc->type = REGULATOR_VOLTAGE;
1686
1687 if (config->is_real_id(pdata->id)) {
1688 /*
1689 * Real regulator; do not modify pin control and pin function
1690 * values.
1691 */
1692 rdesc->ops = vreg_ops[vreg->type];
1693 pin_ctrl = vreg->pdata.pin_ctrl;
1694 pin_fn = vreg->pdata.pin_fn;
1695 memcpy(&(vreg->pdata), pdata,
1696 sizeof(struct rpm_regulator_init_data));
1697 vreg->pdata.pin_ctrl = pin_ctrl;
1698 vreg->pdata.pin_fn = pin_fn;
1699
1700 vreg->save_uV = vreg->pdata.default_uV;
1701 if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
1702 vreg->mode = config->mode_hpm;
1703 else
1704 vreg->mode = config->mode_lpm;
1705
1706 /* Initialize the RPM request. */
1707 SET_PART(vreg, ip,
1708 MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
1709 SET_PART(vreg, fm, vreg->pdata.force_mode);
1710 SET_PART(vreg, pm, vreg->pdata.power_mode);
1711 SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
1712 SET_PART(vreg, ia,
1713 MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
1714 SET_PART(vreg, freq, vreg->pdata.freq);
1715 SET_PART(vreg, freq_clk_src, 0);
1716 SET_PART(vreg, comp_mode, 0);
1717 SET_PART(vreg, hpm, 0);
1718 if (!vreg->is_enabled_pc) {
1719 SET_PART(vreg, pf, config->pin_func_none);
1720 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1721 }
1722 } else {
1723 if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
1724 == RPM_VREG_PIN_CTRL_NONE
1725 && pdata->pin_fn != config->pin_func_sleep_b) {
1726 pr_err("%s: no pin control input specified\n",
1727 vreg->rdesc.name);
1728 mutex_unlock(&vreg->pc_lock);
1729 return -EINVAL;
1730 }
1731 rdesc->ops = &pin_control_ops;
1732 vreg->pdata.pin_ctrl = pdata->pin_ctrl;
1733 vreg->pdata.pin_fn = pdata->pin_fn;
1734
1735 /* Initialize the RPM request. */
1736 pin_fn = config->pin_func_none;
1737 /* Allow pf=sleep_b to be specified by platform data. */
1738 if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
1739 pin_fn = config->pin_func_sleep_b;
1740 SET_PART(vreg, pf, pin_fn);
1741 SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
1742 }
1743
1744 mutex_unlock(&vreg->pc_lock);
1745
1746 if (rc)
1747 goto bail;
1748
Rajendra Nayak11eafc62011-11-18 16:47:19 +05301749 rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001750 if (IS_ERR(rdev)) {
1751 rc = PTR_ERR(rdev);
1752 pr_err("regulator_register failed: %s, rc=%d\n",
1753 vreg->rdesc.name, rc);
1754 return rc;
1755 } else {
1756 if (config->is_real_id(pdata->id))
1757 vreg->rdev = rdev;
1758 else
1759 vreg->rdev_pc = rdev;
1760 }
1761
1762bail:
1763 if (rc)
1764 pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
1765
1766 return rc;
1767}
1768
1769static void rpm_vreg_set_point_init(void)
1770{
1771 struct vreg_set_points **set_points;
1772 int i, j, temp;
1773
1774 set_points = config->set_points;
1775
1776 /* Calculate the number of set points available for each regulator. */
1777 for (i = 0; i < config->set_points_len; i++) {
1778 temp = 0;
1779 for (j = 0; j < set_points[i]->count; j++) {
1780 set_points[i]->range[j].n_voltages
1781 = (set_points[i]->range[j].max_uV
1782 - set_points[i]->range[j].min_uV)
1783 / set_points[i]->range[j].step_uV + 1;
1784 temp += set_points[i]->range[j].n_voltages;
1785 }
1786 set_points[i]->n_voltages = temp;
1787 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001788}
1789
1790static int __devinit rpm_vreg_probe(struct platform_device *pdev)
1791{
David Collins6f032ba2011-08-31 14:08:15 -07001792 struct rpm_regulator_platform_data *platform_data;
David Collins379b4b72012-05-14 16:33:51 -07001793 static struct rpm_regulator_consumer_mapping *prev_consumer_map;
1794 static int prev_consumer_map_len;
David Collins109a8e62012-07-31 15:21:35 -07001795 struct vreg *vreg;
David Collins6f032ba2011-08-31 14:08:15 -07001796 int rc = 0;
1797 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001798
David Collins6f032ba2011-08-31 14:08:15 -07001799 platform_data = pdev->dev.platform_data;
1800 if (!platform_data) {
1801 pr_err("rpm-regulator requires platform data\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001802 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001803 }
1804
David Collins6f032ba2011-08-31 14:08:15 -07001805 if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
1806 && platform_data->version != rpm_version) {
1807 pr_err("rpm version %d does not match previous version %d\n",
1808 platform_data->version, rpm_version);
1809 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810 }
1811
David Collins6f032ba2011-08-31 14:08:15 -07001812 if (platform_data->version < 0
1813 || platform_data->version > RPM_VREG_VERSION_MAX) {
1814 pr_err("rpm version %d is invalid\n", platform_data->version);
1815 return -EINVAL;
1816 }
1817
1818 if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
1819 rpm_version = platform_data->version;
1820 config = get_config[platform_data->version]();
1821 vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
1822 vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
1823 if (!config) {
1824 pr_err("rpm version %d is not available\n",
1825 platform_data->version);
1826 return -ENODEV;
1827 }
1828 if (config->use_legacy_optimum_mode)
1829 for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
1830 vreg_ops[i]->get_optimum_mode
1831 = vreg_legacy_get_optimum_mode;
1832 rpm_vreg_set_point_init();
1833 /* First time probed; initialize pin control mutexes. */
1834 for (i = 0; i < config->vregs_len; i++)
1835 mutex_init(&config->vregs[i].pc_lock);
1836 }
1837
David Collins379b4b72012-05-14 16:33:51 -07001838 /* Copy the list of private API consumers. */
1839 if (platform_data->consumer_map_len > 0) {
1840 if (consumer_map_len == 0) {
1841 consumer_map_len = platform_data->consumer_map_len;
1842 consumer_map = kmemdup(platform_data->consumer_map,
1843 sizeof(struct rpm_regulator_consumer_mapping)
1844 * consumer_map_len, GFP_KERNEL);
1845 if (consumer_map == NULL) {
1846 pr_err("memory allocation failed\n");
1847 consumer_map_len = 0;
1848 return -ENOMEM;
1849 }
1850 } else {
1851 /* Concatenate new map with the existing one. */
1852 prev_consumer_map = consumer_map;
1853 prev_consumer_map_len = consumer_map_len;
1854 consumer_map_len += platform_data->consumer_map_len;
1855 consumer_map = kmalloc(
1856 sizeof(struct rpm_regulator_consumer_mapping)
1857 * consumer_map_len, GFP_KERNEL);
1858 if (consumer_map == NULL) {
1859 pr_err("memory allocation failed\n");
1860 consumer_map_len = 0;
1861 return -ENOMEM;
1862 }
1863 memcpy(consumer_map, prev_consumer_map,
1864 sizeof(struct rpm_regulator_consumer_mapping)
1865 * prev_consumer_map_len);
1866 memcpy(&consumer_map[prev_consumer_map_len],
1867 platform_data->consumer_map,
1868 sizeof(struct rpm_regulator_consumer_mapping)
1869 * platform_data->consumer_map_len);
1870 }
1871
1872 }
1873
David Collins109a8e62012-07-31 15:21:35 -07001874 if (platform_data->requires_tcxo_workaround
1875 && !requires_tcxo_workaround) {
1876 requires_tcxo_workaround = true;
1877 wake_lock_init(&tcxo_wake_lock, WAKE_LOCK_SUSPEND,
1878 "rpm_regulator_tcxo");
1879 }
1880
1881 if (requires_tcxo_workaround && !tcxo_workaround_noirq) {
1882 for (i = 0; i < platform_data->num_regulators; i++) {
1883 vreg = rpm_vreg_get_vreg(
1884 platform_data->init_data[i].id);
1885 if (vreg && vreg->requires_cxo
1886 && platform_data->init_data[i].sleep_selectable) {
1887 tcxo_workaround_noirq = true;
1888 break;
1889 }
1890 }
1891 }
1892
David Collins6f032ba2011-08-31 14:08:15 -07001893 /* Initialize all of the regulators listed in the platform data. */
1894 for (i = 0; i < platform_data->num_regulators; i++) {
1895 rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
1896 &pdev->dev);
1897 if (rc) {
1898 pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
1899 goto remove_regulators;
1900 }
1901 }
1902
1903 platform_set_drvdata(pdev, platform_data);
1904
1905 return rc;
1906
1907remove_regulators:
1908 /* Unregister all regulators added before the erroring one. */
1909 for (; i >= 0; i--) {
1910 id = platform_data->init_data[i].id;
1911 if (config->is_real_id(id)) {
1912 regulator_unregister(config->vregs[id].rdev);
1913 config->vregs[id].rdev = NULL;
1914 } else {
1915 regulator_unregister(config->vregs[
1916 config->pc_id_to_real_id(id)].rdev_pc);
1917 config->vregs[id].rdev_pc = NULL;
1918 }
1919 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001920
1921 return rc;
1922}
1923
1924static int __devexit rpm_vreg_remove(struct platform_device *pdev)
1925{
David Collins6f032ba2011-08-31 14:08:15 -07001926 struct rpm_regulator_platform_data *platform_data;
1927 int i, id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928
David Collins6f032ba2011-08-31 14:08:15 -07001929 platform_data = platform_get_drvdata(pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 platform_set_drvdata(pdev, NULL);
David Collins6f032ba2011-08-31 14:08:15 -07001931
1932 if (platform_data) {
1933 for (i = 0; i < platform_data->num_regulators; i++) {
1934 id = platform_data->init_data[i].id;
1935 if (config->is_real_id(id)) {
1936 regulator_unregister(config->vregs[id].rdev);
1937 config->vregs[id].rdev = NULL;
1938 } else {
1939 regulator_unregister(config->vregs[
1940 config->pc_id_to_real_id(id)].rdev_pc);
1941 config->vregs[id].rdev_pc = NULL;
1942 }
1943 }
1944 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001945
1946 return 0;
1947}
1948
1949static struct platform_driver rpm_vreg_driver = {
1950 .probe = rpm_vreg_probe,
1951 .remove = __devexit_p(rpm_vreg_remove),
1952 .driver = {
David Collins6f032ba2011-08-31 14:08:15 -07001953 .name = RPM_REGULATOR_DEV_NAME,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954 .owner = THIS_MODULE,
1955 },
1956};
1957
1958static int __init rpm_vreg_init(void)
1959{
1960 return platform_driver_register(&rpm_vreg_driver);
1961}
1962
1963static void __exit rpm_vreg_exit(void)
1964{
David Collins6f032ba2011-08-31 14:08:15 -07001965 int i;
1966
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001967 platform_driver_unregister(&rpm_vreg_driver);
David Collins6f032ba2011-08-31 14:08:15 -07001968
David Collins379b4b72012-05-14 16:33:51 -07001969 kfree(consumer_map);
1970
David Collins6f032ba2011-08-31 14:08:15 -07001971 for (i = 0; i < config->vregs_len; i++)
1972 mutex_destroy(&config->vregs[i].pc_lock);
David Collins109a8e62012-07-31 15:21:35 -07001973
1974 if (tcxo_handle)
1975 clk_put(tcxo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001976}
1977
1978postcore_initcall(rpm_vreg_init);
1979module_exit(rpm_vreg_exit);
1980
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001981MODULE_LICENSE("GPL v2");
David Collins6f032ba2011-08-31 14:08:15 -07001982MODULE_DESCRIPTION("MSM RPM regulator driver");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983MODULE_VERSION("1.0");
David Collins6f032ba2011-08-31 14:08:15 -07001984MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);