blob: c7e9e75e73f128a9849c6181ef7c7a624dd5f2d7 [file] [log] [blame]
David Collins9deea5b2013-03-05 10:16:01 -08001/* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
David Collinsc7642322012-04-04 10:19:12 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/module.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <linux/string.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27#include <linux/regulator/of_regulator.h>
28#include <mach/rpm-smd.h>
29#include <mach/rpm-regulator-smd.h>
30#include <mach/socinfo.h>
31
32/* Debug Definitions */
33
34enum {
35 RPM_VREG_DEBUG_REQUEST = BIT(0),
36 RPM_VREG_DEBUG_FULL_REQUEST = BIT(1),
37 RPM_VREG_DEBUG_DUPLICATE = BIT(2),
38};
39
40static int rpm_vreg_debug_mask;
41module_param_named(
42 debug_mask, rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR
43);
44
45#define vreg_err(req, fmt, ...) \
46 pr_err("%s: " fmt, req->rdesc.name, ##__VA_ARGS__)
47
48/* RPM regulator request types */
49enum rpm_regulator_smd_type {
50 RPM_REGULATOR_SMD_TYPE_LDO,
51 RPM_REGULATOR_SMD_TYPE_SMPS,
52 RPM_REGULATOR_SMD_TYPE_VS,
53 RPM_REGULATOR_SMD_TYPE_NCP,
54 RPM_REGULATOR_SMD_TYPE_MAX,
55};
56
57/* RPM resource parameters */
58enum rpm_regulator_param_index {
59 RPM_REGULATOR_PARAM_ENABLE,
60 RPM_REGULATOR_PARAM_VOLTAGE,
61 RPM_REGULATOR_PARAM_CURRENT,
62 RPM_REGULATOR_PARAM_MODE_LDO,
63 RPM_REGULATOR_PARAM_MODE_SMPS,
64 RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE,
65 RPM_REGULATOR_PARAM_PIN_CTRL_MODE,
66 RPM_REGULATOR_PARAM_FREQUENCY,
67 RPM_REGULATOR_PARAM_HEAD_ROOM,
68 RPM_REGULATOR_PARAM_QUIET_MODE,
69 RPM_REGULATOR_PARAM_FREQ_REASON,
David Collins4208ce32012-06-15 13:33:13 -070070 RPM_REGULATOR_PARAM_CORNER,
David Collins85b71992012-07-18 12:00:14 -070071 RPM_REGULATOR_PARAM_BYPASS,
David Collins9deea5b2013-03-05 10:16:01 -080072 RPM_REGULATOR_PARAM_FLOOR_CORNER,
David Collinsc7642322012-04-04 10:19:12 -070073 RPM_REGULATOR_PARAM_MAX,
74};
75
David Collinsf0a12792013-03-26 14:49:12 -070076enum rpm_regulator_smps_mode {
77 RPM_REGULATOR_SMPS_MODE_AUTO = 0,
78 RPM_REGULATOR_SMPS_MODE_IPEAK = 1,
79 RPM_REGULATOR_SMPS_MODE_PWM = 2,
80};
81
82enum rpm_regulator_ldo_mode {
83 RPM_REGULATOR_LDO_MODE_IPEAK = 0,
84 RPM_REGULATOR_LDO_MODE_HPM = 1,
85};
86
David Collinsc7642322012-04-04 10:19:12 -070087#define RPM_SET_CONFIG_ACTIVE BIT(0)
88#define RPM_SET_CONFIG_SLEEP BIT(1)
89#define RPM_SET_CONFIG_BOTH (RPM_SET_CONFIG_ACTIVE \
90 | RPM_SET_CONFIG_SLEEP)
91struct rpm_regulator_param {
92 char *name;
93 char *property_name;
94 u32 key;
95 u32 min;
96 u32 max;
97 u32 supported_regulator_types;
98};
99
100#define PARAM(_idx, _support_ldo, _support_smps, _support_vs, _support_ncp, \
101 _name, _min, _max, _property_name) \
102 [RPM_REGULATOR_PARAM_##_idx] = { \
103 .name = _name, \
104 .property_name = _property_name, \
105 .min = _min, \
106 .max = _max, \
107 .supported_regulator_types = \
108 _support_ldo << RPM_REGULATOR_SMD_TYPE_LDO | \
109 _support_smps << RPM_REGULATOR_SMD_TYPE_SMPS | \
110 _support_vs << RPM_REGULATOR_SMD_TYPE_VS | \
111 _support_ncp << RPM_REGULATOR_SMD_TYPE_NCP, \
112 }
113
114static struct rpm_regulator_param params[RPM_REGULATOR_PARAM_MAX] = {
115 /* ID LDO SMPS VS NCP name min max property-name */
116 PARAM(ENABLE, 1, 1, 1, 1, "swen", 0, 1, "qcom,init-enable"),
117 PARAM(VOLTAGE, 1, 1, 0, 1, "uv", 0, 0x7FFFFFF, "qcom,init-voltage"),
118 PARAM(CURRENT, 1, 1, 0, 0, "ma", 0, 0x1FFF, "qcom,init-current"),
119 PARAM(MODE_LDO, 1, 0, 0, 0, "lsmd", 0, 1, "qcom,init-ldo-mode"),
120 PARAM(MODE_SMPS, 0, 1, 0, 0, "ssmd", 0, 2, "qcom,init-smps-mode"),
121 PARAM(PIN_CTRL_ENABLE, 1, 1, 1, 0, "pcen", 0, 0xF, "qcom,init-pin-ctrl-enable"),
122 PARAM(PIN_CTRL_MODE, 1, 1, 1, 0, "pcmd", 0, 0x1F, "qcom,init-pin-ctrl-mode"),
David Collinsd1247e02013-03-05 11:15:30 -0800123 PARAM(FREQUENCY, 0, 1, 0, 1, "freq", 0, 31, "qcom,init-frequency"),
David Collinsc7642322012-04-04 10:19:12 -0700124 PARAM(HEAD_ROOM, 1, 0, 0, 1, "hr", 0, 0x7FFFFFFF, "qcom,init-head-room"),
125 PARAM(QUIET_MODE, 0, 1, 0, 0, "qm", 0, 2, "qcom,init-quiet-mode"),
126 PARAM(FREQ_REASON, 0, 1, 0, 1, "resn", 0, 8, "qcom,init-freq-reason"),
David Collins23c96cd2012-10-05 17:12:00 -0700127 PARAM(CORNER, 1, 1, 0, 0, "corn", 0, 6, "qcom,init-voltage-corner"),
David Collins85b71992012-07-18 12:00:14 -0700128 PARAM(BYPASS, 1, 0, 0, 0, "bypa", 0, 1, "qcom,init-disallow-bypass"),
David Collins9deea5b2013-03-05 10:16:01 -0800129 PARAM(FLOOR_CORNER, 1, 1, 0, 0, "vfc", 0, 6, "qcom,init-voltage-floor-corner"),
David Collinsc7642322012-04-04 10:19:12 -0700130};
131
David Collinsf0a12792013-03-26 14:49:12 -0700132struct rpm_regulator_mode_map {
133 int ldo_mode;
134 int smps_mode;
135};
136
137static struct rpm_regulator_mode_map mode_mapping[] = {
138 [RPM_REGULATOR_MODE_AUTO]
139 = {-1, RPM_REGULATOR_SMPS_MODE_AUTO},
140 [RPM_REGULATOR_MODE_IPEAK]
141 = {RPM_REGULATOR_LDO_MODE_IPEAK, RPM_REGULATOR_SMPS_MODE_IPEAK},
142 [RPM_REGULATOR_MODE_HPM]
143 = {RPM_REGULATOR_LDO_MODE_HPM, RPM_REGULATOR_SMPS_MODE_PWM},
144};
145
David Collinsc7642322012-04-04 10:19:12 -0700146struct rpm_vreg_request {
147 u32 param[RPM_REGULATOR_PARAM_MAX];
148 u32 valid;
149 u32 modified;
150};
151
152struct rpm_vreg {
153 struct rpm_vreg_request aggr_req_active;
154 struct rpm_vreg_request aggr_req_sleep;
155 struct list_head reg_list;
156 const char *resource_name;
157 u32 resource_id;
158 bool allow_atomic;
159 int regulator_type;
160 int hpm_min_load;
161 int enable_time;
162 struct spinlock slock;
163 struct mutex mlock;
164 unsigned long flags;
165 bool sleep_request_sent;
Ashay Jaiswala2923902013-08-31 19:39:57 +0530166 bool apps_only;
David Collinsc7642322012-04-04 10:19:12 -0700167 struct msm_rpm_request *handle_active;
168 struct msm_rpm_request *handle_sleep;
169};
170
171struct rpm_regulator {
172 struct regulator_desc rdesc;
173 struct regulator_dev *rdev;
174 struct rpm_vreg *rpm_vreg;
175 struct list_head list;
176 bool set_active;
177 bool set_sleep;
David Collins409a9fa2013-03-05 10:54:25 -0800178 bool always_send_voltage;
179 bool always_send_current;
David Collinsc7642322012-04-04 10:19:12 -0700180 struct rpm_vreg_request req;
181 int system_load;
182 int min_uV;
183 int max_uV;
184};
185
186/*
187 * This voltage in uV is returned by get_voltage functions when there is no way
188 * to determine the current voltage level. It is needed because the regulator
189 * framework treats a 0 uV voltage as an error.
190 */
191#define VOLTAGE_UNKNOWN 1
192
193/*
194 * Regulator requests sent in the active set take effect immediately. Requests
195 * sent in the sleep set take effect when the Apps processor transitions into
196 * RPM assisted power collapse. For any given regulator, if an active set
197 * request is present, but not a sleep set request, then the active set request
198 * is used at all times, even when the Apps processor is power collapsed.
199 *
200 * The rpm-regulator-smd takes advantage of this default usage of the active set
201 * request by only sending a sleep set request if it differs from the
202 * corresponding active set request.
203 */
204#define RPM_SET_ACTIVE MSM_RPM_CTX_ACTIVE_SET
205#define RPM_SET_SLEEP MSM_RPM_CTX_SLEEP_SET
206
207static u32 rpm_vreg_string_to_int(const u8 *str)
208{
209 int i, len;
210 u32 output = 0;
211
212 len = strnlen(str, sizeof(u32));
213 for (i = 0; i < len; i++)
214 output |= str[i] << (i * 8);
215
216 return output;
217}
218
219static inline void rpm_vreg_lock(struct rpm_vreg *rpm_vreg)
220{
221 if (rpm_vreg->allow_atomic)
222 spin_lock_irqsave(&rpm_vreg->slock, rpm_vreg->flags);
223 else
224 mutex_lock(&rpm_vreg->mlock);
225}
226
227static inline void rpm_vreg_unlock(struct rpm_vreg *rpm_vreg)
228{
229 if (rpm_vreg->allow_atomic)
230 spin_unlock_irqrestore(&rpm_vreg->slock, rpm_vreg->flags);
231 else
232 mutex_unlock(&rpm_vreg->mlock);
233}
234
235static inline bool rpm_vreg_active_or_sleep_enabled(struct rpm_vreg *rpm_vreg)
236{
237 return (rpm_vreg->aggr_req_active.param[RPM_REGULATOR_PARAM_ENABLE]
238 && (rpm_vreg->aggr_req_active.valid
239 & BIT(RPM_REGULATOR_PARAM_ENABLE)))
240 || ((rpm_vreg->aggr_req_sleep.param[RPM_REGULATOR_PARAM_ENABLE])
241 && (rpm_vreg->aggr_req_sleep.valid
242 & BIT(RPM_REGULATOR_PARAM_ENABLE)));
243}
244
Ashay Jaiswala2923902013-08-31 19:39:57 +0530245static inline bool rpm_vreg_shared_active_or_sleep_enabled_valid
246 (struct rpm_vreg *rpm_vreg)
247{
248 return !rpm_vreg->apps_only &&
249 ((rpm_vreg->aggr_req_active.valid
250 & BIT(RPM_REGULATOR_PARAM_ENABLE))
251 || (rpm_vreg->aggr_req_sleep.valid
252 & BIT(RPM_REGULATOR_PARAM_ENABLE)));
253}
254
David Collinsc7642322012-04-04 10:19:12 -0700255/*
256 * This is used when voting for LPM or HPM by subtracting or adding to the
257 * hpm_min_load of a regulator. It has units of uA.
258 */
259#define LOAD_THRESHOLD_STEP 1000
260
261static inline int rpm_vreg_hpm_min_uA(struct rpm_vreg *rpm_vreg)
262{
263 return rpm_vreg->hpm_min_load;
264}
265
266static inline int rpm_vreg_lpm_max_uA(struct rpm_vreg *rpm_vreg)
267{
268 return rpm_vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
269}
270
271#define MICRO_TO_MILLI(uV) ((uV) / 1000)
272#define MILLI_TO_MICRO(uV) ((uV) * 1000)
273
274#define DEBUG_PRINT_BUFFER_SIZE 512
275#define REQ_SENT 0
276#define REQ_PREV 1
277#define REQ_CACHED 2
278#define REQ_TYPES 3
279
280static void rpm_regulator_req(struct rpm_regulator *regulator, int set,
281 bool sent)
282{
283 char buf[DEBUG_PRINT_BUFFER_SIZE];
284 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
285 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
286 struct rpm_vreg_request *aggr;
287 bool first;
288 u32 mask[REQ_TYPES] = {0, 0, 0};
289 const char *req_names[REQ_TYPES] = {"sent", "prev", "cached"};
290 int pos = 0;
291 int i, j;
292
293 aggr = (set == RPM_SET_ACTIVE)
294 ? &rpm_vreg->aggr_req_active : &rpm_vreg->aggr_req_sleep;
295
296 if (rpm_vreg_debug_mask & RPM_VREG_DEBUG_DUPLICATE) {
297 mask[REQ_SENT] = aggr->modified;
298 mask[REQ_PREV] = aggr->valid & ~aggr->modified;
299 } else if (sent
300 && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_FULL_REQUEST)) {
301 mask[REQ_SENT] = aggr->modified;
302 mask[REQ_PREV] = aggr->valid & ~aggr->modified;
303 } else if (sent && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_REQUEST)) {
304 mask[REQ_SENT] = aggr->modified;
305 }
306
307 if (!(mask[REQ_SENT] | mask[REQ_PREV]))
308 return;
309
310 if (set == RPM_SET_SLEEP && !rpm_vreg->sleep_request_sent) {
311 mask[REQ_CACHED] = mask[REQ_SENT] | mask[REQ_PREV];
312 mask[REQ_SENT] = 0;
313 mask[REQ_PREV] = 0;
314 }
315
316 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
317 KERN_INFO, __func__);
318
319 pos += scnprintf(buf + pos, buflen - pos, "%s %u (%s): s=%s",
320 rpm_vreg->resource_name, rpm_vreg->resource_id,
321 regulator->rdesc.name,
322 (set == RPM_SET_ACTIVE ? "act" : "slp"));
323
324 for (i = 0; i < REQ_TYPES; i++) {
325 if (mask[i])
326 pos += scnprintf(buf + pos, buflen - pos, "; %s: ",
327 req_names[i]);
328
329 first = true;
330 for (j = 0; j < RPM_REGULATOR_PARAM_MAX; j++) {
331 if (mask[i] & BIT(j)) {
332 pos += scnprintf(buf + pos, buflen - pos,
333 "%s%s=%u", (first ? "" : ", "),
334 params[j].name, aggr->param[j]);
335 first = false;
336 }
337 }
338 }
339
340 pos += scnprintf(buf + pos, buflen - pos, "\n");
341 printk(buf);
342}
343
344#define RPM_VREG_SET_PARAM(_regulator, _param, _val) \
345{ \
346 (_regulator)->req.param[RPM_REGULATOR_PARAM_##_param] = _val; \
347 (_regulator)->req.modified |= BIT(RPM_REGULATOR_PARAM_##_param); \
348} \
349
350static int rpm_vreg_add_kvp_to_request(struct rpm_vreg *rpm_vreg,
351 const u32 *param, int idx, u32 set)
352{
353 struct msm_rpm_request *handle;
354
355 handle = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active
356 : rpm_vreg->handle_sleep);
357
358 if (rpm_vreg->allow_atomic)
359 return msm_rpm_add_kvp_data_noirq(handle, params[idx].key,
360 (u8 *)&param[idx], 4);
361 else
362 return msm_rpm_add_kvp_data(handle, params[idx].key,
363 (u8 *)&param[idx], 4);
364}
365
366static void rpm_vreg_check_modified_requests(const u32 *prev_param,
367 const u32 *param, u32 prev_valid, u32 *modified)
368{
369 u32 value_changed = 0;
370 int i;
371
372 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
373 if (param[i] != prev_param[i])
374 value_changed |= BIT(i);
375 }
376
377 /*
378 * Only keep bits that are for changed parameters or previously
379 * invalid parameters.
380 */
381 *modified &= value_changed | ~prev_valid;
382}
383
384static int rpm_vreg_add_modified_requests(struct rpm_regulator *regulator,
385 u32 set, const u32 *param, u32 modified)
386{
387 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
388 int rc = 0;
389 int i;
390
391 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
392 /* Only send requests for modified parameters. */
393 if (modified & BIT(i)) {
394 rc = rpm_vreg_add_kvp_to_request(rpm_vreg, param, i,
395 set);
396 if (rc) {
397 vreg_err(regulator,
398 "add KVP failed: %s %u; %s, rc=%d\n",
399 rpm_vreg->resource_name,
400 rpm_vreg->resource_id, params[i].name,
401 rc);
402 return rc;
403 }
404 }
405 }
406
407 return rc;
408}
409
410static int rpm_vreg_send_request(struct rpm_regulator *regulator, u32 set)
411{
412 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
413 struct msm_rpm_request *handle
414 = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active
415 : rpm_vreg->handle_sleep);
416 int rc;
417
418 if (rpm_vreg->allow_atomic)
419 rc = msm_rpm_wait_for_ack_noirq(msm_rpm_send_request_noirq(
420 handle));
421 else
422 rc = msm_rpm_wait_for_ack(msm_rpm_send_request(handle));
423
424 if (rc)
425 vreg_err(regulator, "msm rpm send failed: %s %u; set=%s, "
426 "rc=%d\n", rpm_vreg->resource_name,
427 rpm_vreg->resource_id,
428 (set == RPM_SET_ACTIVE ? "act" : "slp"), rc);
429
430 return rc;
431}
432
David Collinsd1247e02013-03-05 11:15:30 -0800433#define RPM_VREG_AGGR_MIN(_idx, _param_aggr, _param_reg) \
434{ \
435 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
436 = min(_param_aggr[RPM_REGULATOR_PARAM_##_idx], \
437 _param_reg[RPM_REGULATOR_PARAM_##_idx]); \
438}
439
David Collinsc7642322012-04-04 10:19:12 -0700440#define RPM_VREG_AGGR_MAX(_idx, _param_aggr, _param_reg) \
441{ \
442 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
443 = max(_param_aggr[RPM_REGULATOR_PARAM_##_idx], \
444 _param_reg[RPM_REGULATOR_PARAM_##_idx]); \
445}
446
447#define RPM_VREG_AGGR_SUM(_idx, _param_aggr, _param_reg) \
448{ \
449 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
450 += _param_reg[RPM_REGULATOR_PARAM_##_idx]; \
451}
452
453#define RPM_VREG_AGGR_OR(_idx, _param_aggr, _param_reg) \
454{ \
455 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
456 |= _param_reg[RPM_REGULATOR_PARAM_##_idx]; \
457}
458
459/*
David Collinsc7642322012-04-04 10:19:12 -0700460 * Aggregation is performed on each parameter based on the way that the RPM
461 * aggregates that type internally between RPM masters.
462 */
463static void rpm_vreg_aggregate_params(u32 *param_aggr, const u32 *param_reg)
464{
465 RPM_VREG_AGGR_MAX(ENABLE, param_aggr, param_reg);
466 RPM_VREG_AGGR_MAX(VOLTAGE, param_aggr, param_reg);
467 RPM_VREG_AGGR_SUM(CURRENT, param_aggr, param_reg);
468 RPM_VREG_AGGR_MAX(MODE_LDO, param_aggr, param_reg);
469 RPM_VREG_AGGR_MAX(MODE_SMPS, param_aggr, param_reg);
470 RPM_VREG_AGGR_OR(PIN_CTRL_ENABLE, param_aggr, param_reg);
471 RPM_VREG_AGGR_OR(PIN_CTRL_MODE, param_aggr, param_reg);
David Collinsd1247e02013-03-05 11:15:30 -0800472 RPM_VREG_AGGR_MIN(FREQUENCY, param_aggr, param_reg);
David Collinsc7642322012-04-04 10:19:12 -0700473 RPM_VREG_AGGR_MAX(HEAD_ROOM, param_aggr, param_reg);
474 RPM_VREG_AGGR_MAX(QUIET_MODE, param_aggr, param_reg);
475 RPM_VREG_AGGR_MAX(FREQ_REASON, param_aggr, param_reg);
David Collins4208ce32012-06-15 13:33:13 -0700476 RPM_VREG_AGGR_MAX(CORNER, param_aggr, param_reg);
David Collins85b71992012-07-18 12:00:14 -0700477 RPM_VREG_AGGR_MAX(BYPASS, param_aggr, param_reg);
David Collins9deea5b2013-03-05 10:16:01 -0800478 RPM_VREG_AGGR_MAX(FLOOR_CORNER, param_aggr, param_reg);
David Collinsc7642322012-04-04 10:19:12 -0700479}
480
481static int rpm_vreg_aggregate_requests(struct rpm_regulator *regulator)
482{
483 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
484 u32 param_active[RPM_REGULATOR_PARAM_MAX];
485 u32 param_sleep[RPM_REGULATOR_PARAM_MAX];
486 u32 modified_active, modified_sleep;
487 struct rpm_regulator *reg;
488 bool sleep_set_differs = false;
489 bool send_active = false;
490 bool send_sleep = false;
491 int rc = 0;
492 int i;
493
494 memset(param_active, 0, sizeof(param_active));
495 memset(param_sleep, 0, sizeof(param_sleep));
496 modified_active = rpm_vreg->aggr_req_active.modified;
497 modified_sleep = rpm_vreg->aggr_req_sleep.modified;
498
499 /*
500 * Aggregate all of the requests for this regulator in both active
501 * and sleep sets.
502 */
503 list_for_each_entry(reg, &rpm_vreg->reg_list, list) {
504 if (reg->set_active) {
505 rpm_vreg_aggregate_params(param_active, reg->req.param);
506 modified_active |= reg->req.modified;
507 }
508 if (reg->set_sleep) {
509 rpm_vreg_aggregate_params(param_sleep, reg->req.param);
510 modified_sleep |= reg->req.modified;
511 }
512 }
513
514 /*
515 * Check if the aggregated sleep set parameter values differ from the
516 * aggregated active set parameter values.
517 */
518 if (!rpm_vreg->sleep_request_sent) {
519 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
520 if ((param_active[i] != param_sleep[i])
521 && (modified_sleep & BIT(i))) {
522 sleep_set_differs = true;
523 break;
524 }
525 }
526 }
527
528 /* Add KVPs to the active set RPM request if they have new values. */
529 rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_active.param,
530 param_active, rpm_vreg->aggr_req_active.valid,
531 &modified_active);
532 rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_ACTIVE,
533 param_active, modified_active);
534 if (rc)
535 return rc;
536 send_active = modified_active;
537
538 /*
539 * Sleep set configurations are only sent if they differ from the
540 * active set values. This is because the active set values will take
541 * effect during rpm assisted power collapse in the absence of sleep set
542 * values.
543 *
544 * However, once a sleep set request is sent for a given regulator,
545 * additional sleep set requests must be sent in the future even if they
546 * match the corresponding active set requests.
547 */
548 if (rpm_vreg->sleep_request_sent || sleep_set_differs) {
549 /* Add KVPs to the sleep set RPM request if they are new. */
550 rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_sleep.param,
551 param_sleep, rpm_vreg->aggr_req_sleep.valid,
552 &modified_sleep);
553 rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_SLEEP,
554 param_sleep, modified_sleep);
555 if (rc)
556 return rc;
557 send_sleep = modified_sleep;
558 }
559
560 /* Send active set request to the RPM if it contains new KVPs. */
561 if (send_active) {
562 rc = rpm_vreg_send_request(regulator, RPM_SET_ACTIVE);
563 if (rc)
564 return rc;
565 rpm_vreg->aggr_req_active.valid |= modified_active;
566 }
567 /* Store the results of the aggregation. */
568 rpm_vreg->aggr_req_active.modified = modified_active;
569 memcpy(rpm_vreg->aggr_req_active.param, param_active,
570 sizeof(param_active));
571
572 /* Handle debug printing of the active set request. */
573 rpm_regulator_req(regulator, RPM_SET_ACTIVE, send_active);
574 if (send_active)
575 rpm_vreg->aggr_req_active.modified = 0;
576
577 /* Send sleep set request to the RPM if it contains new KVPs. */
578 if (send_sleep) {
579 rc = rpm_vreg_send_request(regulator, RPM_SET_SLEEP);
580 if (rc)
581 return rc;
582 else
583 rpm_vreg->sleep_request_sent = true;
584 rpm_vreg->aggr_req_sleep.valid |= modified_sleep;
585 }
586 /* Store the results of the aggregation. */
587 rpm_vreg->aggr_req_sleep.modified = modified_sleep;
588 memcpy(rpm_vreg->aggr_req_sleep.param, param_sleep,
589 sizeof(param_sleep));
590
591 /* Handle debug printing of the sleep set request. */
592 rpm_regulator_req(regulator, RPM_SET_SLEEP, send_sleep);
593 if (send_sleep)
594 rpm_vreg->aggr_req_sleep.modified = 0;
595
596 /*
597 * Loop over all requests for this regulator to update the valid and
598 * modified values for use in future aggregation.
599 */
600 list_for_each_entry(reg, &rpm_vreg->reg_list, list) {
601 reg->req.valid |= reg->req.modified;
602 reg->req.modified = 0;
603 }
604
605 return rc;
606}
607
608static int rpm_vreg_is_enabled(struct regulator_dev *rdev)
609{
610 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
611
612 return reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
613}
614
615static int rpm_vreg_enable(struct regulator_dev *rdev)
616{
617 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
618 int rc;
619 u32 prev_enable;
620
621 rpm_vreg_lock(reg->rpm_vreg);
622
623 prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
624 RPM_VREG_SET_PARAM(reg, ENABLE, 1);
625 rc = rpm_vreg_aggregate_requests(reg);
626 if (rc) {
627 vreg_err(reg, "enable failed, rc=%d", rc);
628 RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable);
629 }
630
631 rpm_vreg_unlock(reg->rpm_vreg);
632
633 return rc;
634}
635
636static int rpm_vreg_disable(struct regulator_dev *rdev)
637{
638 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
639 int rc;
640 u32 prev_enable;
641
642 rpm_vreg_lock(reg->rpm_vreg);
643
644 prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
645 RPM_VREG_SET_PARAM(reg, ENABLE, 0);
646 rc = rpm_vreg_aggregate_requests(reg);
647 if (rc) {
648 vreg_err(reg, "enable failed, rc=%d", rc);
649 RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable);
650 }
651
652 rpm_vreg_unlock(reg->rpm_vreg);
653
654 return rc;
655}
656
657static int rpm_vreg_set_voltage(struct regulator_dev *rdev, int min_uV,
658 int max_uV, unsigned *selector)
659{
660 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
661 int rc = 0;
662 u32 prev_voltage;
663
664 rpm_vreg_lock(reg->rpm_vreg);
665
666 prev_voltage = reg->req.param[RPM_REGULATOR_PARAM_VOLTAGE];
667 RPM_VREG_SET_PARAM(reg, VOLTAGE, min_uV);
668
David Collins409a9fa2013-03-05 10:54:25 -0800669 /*
670 * Only send a new voltage if the regulator is currently enabled or
671 * if the regulator has been configured to always send voltage updates.
672 */
673 if (reg->always_send_voltage
Ashay Jaiswala2923902013-08-31 19:39:57 +0530674 || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg)
675 || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg))
David Collinsc7642322012-04-04 10:19:12 -0700676 rc = rpm_vreg_aggregate_requests(reg);
677
678 if (rc) {
679 vreg_err(reg, "set voltage failed, rc=%d", rc);
680 RPM_VREG_SET_PARAM(reg, VOLTAGE, prev_voltage);
681 }
682
683 rpm_vreg_unlock(reg->rpm_vreg);
684
685 return rc;
686}
687
688static int rpm_vreg_get_voltage(struct regulator_dev *rdev)
689{
690 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
691 int uV;
692
693 uV = reg->req.param[RPM_REGULATOR_PARAM_VOLTAGE];
694 if (uV == 0)
695 uV = VOLTAGE_UNKNOWN;
696
697 return uV;
698}
699
David Collins4208ce32012-06-15 13:33:13 -0700700static int rpm_vreg_set_voltage_corner(struct regulator_dev *rdev, int min_uV,
701 int max_uV, unsigned *selector)
702{
703 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
704 int rc = 0;
705 int corner;
706 u32 prev_corner;
707
708 /*
709 * Translate from values which work as inputs in the
710 * regulator_set_voltage function to the actual corner values
711 * sent to the RPM.
712 */
David Collins71c18992012-07-18 11:25:24 -0700713 corner = min_uV - RPM_REGULATOR_CORNER_NONE;
David Collins4208ce32012-06-15 13:33:13 -0700714
715 if (corner < params[RPM_REGULATOR_PARAM_CORNER].min
716 || corner > params[RPM_REGULATOR_PARAM_CORNER].max) {
717 vreg_err(reg, "corner=%d is not within allowed range: [%u, %u]\n",
718 corner, params[RPM_REGULATOR_PARAM_CORNER].min,
719 params[RPM_REGULATOR_PARAM_CORNER].max);
720 return -EINVAL;
721 }
722
723 rpm_vreg_lock(reg->rpm_vreg);
724
725 prev_corner = reg->req.param[RPM_REGULATOR_PARAM_CORNER];
726 RPM_VREG_SET_PARAM(reg, CORNER, corner);
727
David Collins409a9fa2013-03-05 10:54:25 -0800728 /*
729 * Only send a new voltage corner if the regulator is currently enabled
730 * or if the regulator has been configured to always send voltage
731 * updates.
732 */
733 if (reg->always_send_voltage
Ashay Jaiswala2923902013-08-31 19:39:57 +0530734 || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg)
735 || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg))
David Collins4208ce32012-06-15 13:33:13 -0700736 rc = rpm_vreg_aggregate_requests(reg);
737
738 if (rc) {
739 vreg_err(reg, "set voltage corner failed, rc=%d", rc);
740 RPM_VREG_SET_PARAM(reg, CORNER, prev_corner);
741 }
742
743 rpm_vreg_unlock(reg->rpm_vreg);
744
745 return rc;
746}
747
748static int rpm_vreg_get_voltage_corner(struct regulator_dev *rdev)
749{
750 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
751
752 return reg->req.param[RPM_REGULATOR_PARAM_CORNER]
David Collins71c18992012-07-18 11:25:24 -0700753 + RPM_REGULATOR_CORNER_NONE;
David Collins4208ce32012-06-15 13:33:13 -0700754}
755
David Collins9deea5b2013-03-05 10:16:01 -0800756static int rpm_vreg_set_voltage_floor_corner(struct regulator_dev *rdev,
757 int min_uV, int max_uV, unsigned *selector)
758{
759 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
760 int rc = 0;
761 int corner;
762 u32 prev_corner;
763
764 /*
765 * Translate from values which work as inputs in the
766 * regulator_set_voltage function to the actual corner values
767 * sent to the RPM.
768 */
769 corner = min_uV - RPM_REGULATOR_CORNER_NONE;
770
771 if (corner < params[RPM_REGULATOR_PARAM_FLOOR_CORNER].min
772 || corner > params[RPM_REGULATOR_PARAM_FLOOR_CORNER].max) {
773 vreg_err(reg, "corner=%d is not within allowed range: [%u, %u]\n",
774 corner, params[RPM_REGULATOR_PARAM_FLOOR_CORNER].min,
775 params[RPM_REGULATOR_PARAM_FLOOR_CORNER].max);
776 return -EINVAL;
777 }
778
779 rpm_vreg_lock(reg->rpm_vreg);
780
781 prev_corner = reg->req.param[RPM_REGULATOR_PARAM_FLOOR_CORNER];
782 RPM_VREG_SET_PARAM(reg, FLOOR_CORNER, corner);
783
David Collins409a9fa2013-03-05 10:54:25 -0800784 /*
785 * Only send a new voltage floor corner if the regulator is currently
786 * enabled or if the regulator has been configured to always send
787 * voltage updates.
788 */
789 if (reg->always_send_voltage
Ashay Jaiswala2923902013-08-31 19:39:57 +0530790 || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg)
791 || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg))
David Collins9deea5b2013-03-05 10:16:01 -0800792 rc = rpm_vreg_aggregate_requests(reg);
793
794 if (rc) {
795 vreg_err(reg, "set voltage corner failed, rc=%d", rc);
796 RPM_VREG_SET_PARAM(reg, FLOOR_CORNER, prev_corner);
797 }
798
799 rpm_vreg_unlock(reg->rpm_vreg);
800
801 return rc;
802}
803
804static int rpm_vreg_get_voltage_floor_corner(struct regulator_dev *rdev)
805{
806 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
807
808 return reg->req.param[RPM_REGULATOR_PARAM_FLOOR_CORNER]
809 + RPM_REGULATOR_CORNER_NONE;
810}
811
David Collinsc7642322012-04-04 10:19:12 -0700812static int rpm_vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
813{
814 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
815 int rc = 0;
816 u32 prev_current;
817 int prev_uA;
818
819 rpm_vreg_lock(reg->rpm_vreg);
820
821 prev_current = reg->req.param[RPM_REGULATOR_PARAM_CURRENT];
822 prev_uA = MILLI_TO_MICRO(prev_current);
823
824 if (mode == REGULATOR_MODE_NORMAL) {
825 /* Make sure that request current is in HPM range. */
826 if (prev_uA < rpm_vreg_hpm_min_uA(reg->rpm_vreg))
827 RPM_VREG_SET_PARAM(reg, CURRENT,
828 MICRO_TO_MILLI(rpm_vreg_hpm_min_uA(reg->rpm_vreg)));
829 } else if (REGULATOR_MODE_IDLE) {
830 /* Make sure that request current is in LPM range. */
831 if (prev_uA > rpm_vreg_lpm_max_uA(reg->rpm_vreg))
832 RPM_VREG_SET_PARAM(reg, CURRENT,
833 MICRO_TO_MILLI(rpm_vreg_lpm_max_uA(reg->rpm_vreg)));
834 } else {
835 vreg_err(reg, "invalid mode: %u\n", mode);
836 rpm_vreg_unlock(reg->rpm_vreg);
837 return -EINVAL;
838 }
839
David Collins409a9fa2013-03-05 10:54:25 -0800840 /*
841 * Only send a new load current value if the regulator is currently
842 * enabled or if the regulator has been configured to always send
843 * current updates.
844 */
845 if (reg->always_send_current
Ashay Jaiswala2923902013-08-31 19:39:57 +0530846 || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg)
847 || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg))
David Collinsc7642322012-04-04 10:19:12 -0700848 rc = rpm_vreg_aggregate_requests(reg);
849
850 if (rc) {
851 vreg_err(reg, "set mode failed, rc=%d", rc);
852 RPM_VREG_SET_PARAM(reg, CURRENT, prev_current);
853 }
854
855 rpm_vreg_unlock(reg->rpm_vreg);
856
857 return rc;
858}
859
860static unsigned int rpm_vreg_get_mode(struct regulator_dev *rdev)
861{
862 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
863
864 return (reg->req.param[RPM_REGULATOR_PARAM_CURRENT]
865 >= MICRO_TO_MILLI(reg->rpm_vreg->hpm_min_load))
866 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
867}
868
869static unsigned int rpm_vreg_get_optimum_mode(struct regulator_dev *rdev,
870 int input_uV, int output_uV, int load_uA)
871{
872 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
873 u32 load_mA;
874
875 load_uA += reg->system_load;
876
877 load_mA = MICRO_TO_MILLI(load_uA);
878 if (load_mA > params[RPM_REGULATOR_PARAM_CURRENT].max)
879 load_mA = params[RPM_REGULATOR_PARAM_CURRENT].max;
880
881 rpm_vreg_lock(reg->rpm_vreg);
David Collins5161f0d2013-12-06 14:42:47 -0800882 RPM_VREG_SET_PARAM(reg, CURRENT, load_mA);
David Collinsc7642322012-04-04 10:19:12 -0700883 rpm_vreg_unlock(reg->rpm_vreg);
884
885 return (load_uA >= reg->rpm_vreg->hpm_min_load)
886 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
887}
888
889static int rpm_vreg_enable_time(struct regulator_dev *rdev)
890{
891 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
892
893 return reg->rpm_vreg->enable_time;
894}
895
896/**
897 * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
898 * @dev: device for regulator consumer
899 * @supply: supply name
900 *
901 * Returns a struct rpm_regulator corresponding to the regulator producer,
902 * or ERR_PTR() containing errno.
903 *
904 * This function may only be called from nonatomic context.
905 */
906struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
907{
908 struct rpm_regulator *framework_reg;
909 struct rpm_regulator *priv_reg = NULL;
910 struct regulator *regulator;
911 struct rpm_vreg *rpm_vreg;
912
913 regulator = regulator_get(dev, supply);
David Collins29ba0282012-06-18 10:01:52 -0700914 if (IS_ERR(regulator)) {
915 pr_err("could not find regulator for: dev=%s, supply=%s, rc=%ld\n",
916 (dev ? dev_name(dev) : ""), (supply ? supply : ""),
917 PTR_ERR(regulator));
918 return ERR_CAST(regulator);
David Collinsc7642322012-04-04 10:19:12 -0700919 }
920
921 framework_reg = regulator_get_drvdata(regulator);
922 if (framework_reg == NULL) {
923 pr_err("regulator structure not found.\n");
924 regulator_put(regulator);
925 return ERR_PTR(-ENODEV);
926 }
927 regulator_put(regulator);
928
929 rpm_vreg = framework_reg->rpm_vreg;
930
931 priv_reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
932 if (priv_reg == NULL) {
933 vreg_err(framework_reg, "could not allocate memory for "
934 "regulator\n");
David Collinsc7642322012-04-04 10:19:12 -0700935 return ERR_PTR(-ENOMEM);
936 }
937
938 /*
939 * Allocate a regulator_dev struct so that framework callback functions
940 * can be called from the private API functions.
941 */
942 priv_reg->rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
943 if (priv_reg->rdev == NULL) {
944 vreg_err(framework_reg, "could not allocate memory for "
945 "regulator_dev\n");
946 kfree(priv_reg);
David Collinsc7642322012-04-04 10:19:12 -0700947 return ERR_PTR(-ENOMEM);
948 }
949 priv_reg->rdev->reg_data = priv_reg;
950 priv_reg->rpm_vreg = rpm_vreg;
951 priv_reg->rdesc.name = framework_reg->rdesc.name;
David Collins4208ce32012-06-15 13:33:13 -0700952 priv_reg->rdesc.ops = framework_reg->rdesc.ops;
David Collinsc7642322012-04-04 10:19:12 -0700953 priv_reg->set_active = framework_reg->set_active;
954 priv_reg->set_sleep = framework_reg->set_sleep;
955 priv_reg->min_uV = framework_reg->min_uV;
956 priv_reg->max_uV = framework_reg->max_uV;
957 priv_reg->system_load = framework_reg->system_load;
958
959 might_sleep_if(!rpm_vreg->allow_atomic);
960 rpm_vreg_lock(rpm_vreg);
961 list_add(&priv_reg->list, &rpm_vreg->reg_list);
962 rpm_vreg_unlock(rpm_vreg);
963
964 return priv_reg;
965}
966EXPORT_SYMBOL_GPL(rpm_regulator_get);
967
968static int rpm_regulator_check_input(struct rpm_regulator *regulator)
969{
David Collins29ba0282012-06-18 10:01:52 -0700970 if (IS_ERR_OR_NULL(regulator) || regulator->rpm_vreg == NULL) {
David Collinsc7642322012-04-04 10:19:12 -0700971 pr_err("invalid rpm_regulator pointer\n");
972 return -EINVAL;
973 }
974
975 might_sleep_if(!regulator->rpm_vreg->allow_atomic);
976
977 return 0;
978}
979
980/**
981 * rpm_regulator_put() - free the RPM regulator handle
982 * @regulator: RPM regulator handle
983 *
984 * Parameter reaggregation does not take place when rpm_regulator_put is called.
985 * Therefore, regulator enable state and voltage must be configured
986 * appropriately before calling rpm_regulator_put.
987 *
988 * This function may be called from either atomic or nonatomic context. If this
989 * function is called from atomic context, then the regulator being operated on
990 * must be configured via device tree with qcom,allow-atomic == 1.
991 */
992void rpm_regulator_put(struct rpm_regulator *regulator)
993{
994 struct rpm_vreg *rpm_vreg;
995 int rc = rpm_regulator_check_input(regulator);
996
997 if (rc)
998 return;
999
1000 rpm_vreg = regulator->rpm_vreg;
1001
1002 might_sleep_if(!rpm_vreg->allow_atomic);
1003 rpm_vreg_lock(rpm_vreg);
1004 list_del(&regulator->list);
1005 rpm_vreg_unlock(rpm_vreg);
1006
1007 kfree(regulator->rdev);
1008 kfree(regulator);
1009}
1010EXPORT_SYMBOL_GPL(rpm_regulator_put);
1011
1012/**
1013 * rpm_regulator_enable() - enable regulator output
1014 * @regulator: RPM regulator handle
1015 *
1016 * Returns 0 on success or errno on failure.
1017 *
1018 * This function may be called from either atomic or nonatomic context. If this
1019 * function is called from atomic context, then the regulator being operated on
1020 * must be configured via device tree with qcom,allow-atomic == 1.
1021 */
1022int rpm_regulator_enable(struct rpm_regulator *regulator)
1023{
1024 int rc = rpm_regulator_check_input(regulator);
1025
1026 if (rc)
1027 return rc;
1028
1029 return rpm_vreg_enable(regulator->rdev);
1030}
1031EXPORT_SYMBOL_GPL(rpm_regulator_enable);
1032
1033/**
1034 * rpm_regulator_disable() - disable regulator output
1035 * @regulator: RPM regulator handle
1036 *
1037 * Returns 0 on success or errno on failure.
1038 *
1039 * The enable state of the regulator is determined by aggregating the requests
1040 * of all consumers. Therefore, it is possible that the regulator will remain
1041 * enabled even after rpm_regulator_disable is called.
1042 *
1043 * This function may be called from either atomic or nonatomic context. If this
1044 * function is called from atomic context, then the regulator being operated on
1045 * must be configured via device tree with qcom,allow-atomic == 1.
1046 */
1047int rpm_regulator_disable(struct rpm_regulator *regulator)
1048{
1049 int rc = rpm_regulator_check_input(regulator);
1050
1051 if (rc)
1052 return rc;
1053
1054 return rpm_vreg_disable(regulator->rdev);
1055}
1056EXPORT_SYMBOL_GPL(rpm_regulator_disable);
1057
1058/**
1059 * rpm_regulator_set_voltage() - set regulator output voltage
1060 * @regulator: RPM regulator handle
1061 * @min_uV: minimum required voltage in uV
1062 * @max_uV: maximum acceptable voltage in uV
1063 *
1064 * Sets a voltage regulator to the desired output voltage. This can be set
1065 * while the regulator is disabled or enabled. If the regulator is enabled then
1066 * the voltage will change to the new value immediately; otherwise, if the
1067 * regulator is disabled, then the regulator will output at the new voltage when
1068 * enabled.
1069 *
1070 * The min_uV to max_uV voltage range requested must intersect with the
1071 * voltage constraint range configured for the regulator.
1072 *
1073 * Returns 0 on success or errno on failure.
1074 *
1075 * The final voltage value that is sent to the RPM is aggregated based upon the
1076 * values requested by all consumers of the regulator. This corresponds to the
1077 * maximum min_uV value.
1078 *
1079 * This function may be called from either atomic or nonatomic context. If this
1080 * function is called from atomic context, then the regulator being operated on
1081 * must be configured via device tree with qcom,allow-atomic == 1.
1082 */
1083int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
1084 int max_uV)
1085{
1086 int rc = rpm_regulator_check_input(regulator);
1087 int uV = min_uV;
1088
1089 if (rc)
1090 return rc;
1091
1092 if (regulator->rpm_vreg->regulator_type == RPM_REGULATOR_SMD_TYPE_VS) {
1093 vreg_err(regulator, "unsupported regulator type: %d\n",
1094 regulator->rpm_vreg->regulator_type);
1095 return -EINVAL;
1096 }
1097
1098 if (min_uV > max_uV) {
1099 vreg_err(regulator, "min_uV=%d must be less than max_uV=%d\n",
1100 min_uV, max_uV);
1101 return -EINVAL;
1102 }
1103
1104 if (uV < regulator->min_uV && max_uV >= regulator->min_uV)
1105 uV = regulator->min_uV;
1106
1107 if (uV < regulator->min_uV || uV > regulator->max_uV) {
1108 vreg_err(regulator, "request v=[%d, %d] is outside allowed "
1109 "v=[%d, %d]\n", min_uV, max_uV, regulator->min_uV,
1110 regulator->max_uV);
1111 return -EINVAL;
1112 }
1113
David Collins4208ce32012-06-15 13:33:13 -07001114 return regulator->rdesc.ops->set_voltage(regulator->rdev, uV, uV, NULL);
David Collinsc7642322012-04-04 10:19:12 -07001115}
1116EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
1117
David Collinsf0a12792013-03-26 14:49:12 -07001118/**
1119 * rpm_regulator_set_mode() - set regulator operating mode
1120 * @regulator: RPM regulator handle
1121 * @mode: operating mode requested for the regulator
1122 *
1123 * Requests that the mode of the regulator be set to the mode specified. This
1124 * parameter is aggregated using a max function such that AUTO < IPEAK < HPM.
1125 *
1126 * Returns 0 on success or errno on failure.
1127 */
1128int rpm_regulator_set_mode(struct rpm_regulator *regulator,
1129 enum rpm_regulator_mode mode)
1130{
1131 int index = 0;
1132 u32 new_mode, prev_mode;
1133 int rc;
1134
1135 rc = rpm_regulator_check_input(regulator);
1136 if (rc)
1137 return rc;
1138
1139 if (mode < 0 || mode >= ARRAY_SIZE(mode_mapping)) {
1140 vreg_err(regulator, "invalid mode requested: %d\n", mode);
1141 return -EINVAL;
1142 }
1143
1144 switch (regulator->rpm_vreg->regulator_type) {
1145 case RPM_REGULATOR_SMD_TYPE_SMPS:
1146 index = RPM_REGULATOR_PARAM_MODE_SMPS;
1147 new_mode = mode_mapping[mode].smps_mode;
1148 break;
1149 case RPM_REGULATOR_SMD_TYPE_LDO:
1150 index = RPM_REGULATOR_PARAM_MODE_LDO;
1151 new_mode = mode_mapping[mode].ldo_mode;
1152 break;
1153 default:
1154 vreg_err(regulator, "unsupported regulator type: %d\n",
1155 regulator->rpm_vreg->regulator_type);
1156 return -EINVAL;
1157 };
1158
1159 if (new_mode < params[index].min || new_mode > params[index].max) {
1160 vreg_err(regulator, "invalid mode requested: %d for type: %d\n",
1161 mode, regulator->rpm_vreg->regulator_type);
1162 return -EINVAL;
1163 }
1164
1165 rpm_vreg_lock(regulator->rpm_vreg);
1166
1167 prev_mode = regulator->req.param[index];
1168 regulator->req.param[index] = new_mode;
1169 regulator->req.modified |= BIT(index);
1170
1171 rc = rpm_vreg_aggregate_requests(regulator);
1172 if (rc) {
1173 vreg_err(regulator, "set mode failed, rc=%d", rc);
1174 regulator->req.param[index] = prev_mode;
1175 }
1176
1177 rpm_vreg_unlock(regulator->rpm_vreg);
1178
1179 return rc;
1180}
1181EXPORT_SYMBOL_GPL(rpm_regulator_set_mode);
1182
David Collinsc7642322012-04-04 10:19:12 -07001183static struct regulator_ops ldo_ops = {
1184 .enable = rpm_vreg_enable,
1185 .disable = rpm_vreg_disable,
1186 .is_enabled = rpm_vreg_is_enabled,
1187 .set_voltage = rpm_vreg_set_voltage,
1188 .get_voltage = rpm_vreg_get_voltage,
David Collinsc7642322012-04-04 10:19:12 -07001189 .set_mode = rpm_vreg_set_mode,
1190 .get_mode = rpm_vreg_get_mode,
1191 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1192 .enable_time = rpm_vreg_enable_time,
1193};
1194
David Collins23c96cd2012-10-05 17:12:00 -07001195static struct regulator_ops ldo_corner_ops = {
1196 .enable = rpm_vreg_enable,
1197 .disable = rpm_vreg_disable,
1198 .is_enabled = rpm_vreg_is_enabled,
1199 .set_voltage = rpm_vreg_set_voltage_corner,
1200 .get_voltage = rpm_vreg_get_voltage_corner,
David Collins23c96cd2012-10-05 17:12:00 -07001201 .set_mode = rpm_vreg_set_mode,
1202 .get_mode = rpm_vreg_get_mode,
1203 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1204 .enable_time = rpm_vreg_enable_time,
1205};
1206
David Collins9deea5b2013-03-05 10:16:01 -08001207static struct regulator_ops ldo_floor_corner_ops = {
1208 .enable = rpm_vreg_enable,
1209 .disable = rpm_vreg_disable,
1210 .is_enabled = rpm_vreg_is_enabled,
1211 .set_voltage = rpm_vreg_set_voltage_floor_corner,
1212 .get_voltage = rpm_vreg_get_voltage_floor_corner,
1213 .set_mode = rpm_vreg_set_mode,
1214 .get_mode = rpm_vreg_get_mode,
1215 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1216 .enable_time = rpm_vreg_enable_time,
1217};
1218
David Collinsc7642322012-04-04 10:19:12 -07001219static struct regulator_ops smps_ops = {
1220 .enable = rpm_vreg_enable,
1221 .disable = rpm_vreg_disable,
1222 .is_enabled = rpm_vreg_is_enabled,
1223 .set_voltage = rpm_vreg_set_voltage,
1224 .get_voltage = rpm_vreg_get_voltage,
David Collinsc7642322012-04-04 10:19:12 -07001225 .set_mode = rpm_vreg_set_mode,
1226 .get_mode = rpm_vreg_get_mode,
1227 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1228 .enable_time = rpm_vreg_enable_time,
1229};
1230
David Collins4208ce32012-06-15 13:33:13 -07001231static struct regulator_ops smps_corner_ops = {
1232 .enable = rpm_vreg_enable,
1233 .disable = rpm_vreg_disable,
1234 .is_enabled = rpm_vreg_is_enabled,
1235 .set_voltage = rpm_vreg_set_voltage_corner,
1236 .get_voltage = rpm_vreg_get_voltage_corner,
David Collins4208ce32012-06-15 13:33:13 -07001237 .set_mode = rpm_vreg_set_mode,
1238 .get_mode = rpm_vreg_get_mode,
1239 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1240 .enable_time = rpm_vreg_enable_time,
1241};
1242
David Collins9deea5b2013-03-05 10:16:01 -08001243static struct regulator_ops smps_floor_corner_ops = {
1244 .enable = rpm_vreg_enable,
1245 .disable = rpm_vreg_disable,
1246 .is_enabled = rpm_vreg_is_enabled,
1247 .set_voltage = rpm_vreg_set_voltage_floor_corner,
1248 .get_voltage = rpm_vreg_get_voltage_floor_corner,
1249 .set_mode = rpm_vreg_set_mode,
1250 .get_mode = rpm_vreg_get_mode,
1251 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1252 .enable_time = rpm_vreg_enable_time,
1253};
1254
David Collinsc7642322012-04-04 10:19:12 -07001255static struct regulator_ops switch_ops = {
1256 .enable = rpm_vreg_enable,
1257 .disable = rpm_vreg_disable,
1258 .is_enabled = rpm_vreg_is_enabled,
1259 .enable_time = rpm_vreg_enable_time,
1260};
1261
1262static struct regulator_ops ncp_ops = {
1263 .enable = rpm_vreg_enable,
1264 .disable = rpm_vreg_disable,
1265 .is_enabled = rpm_vreg_is_enabled,
1266 .set_voltage = rpm_vreg_set_voltage,
1267 .get_voltage = rpm_vreg_get_voltage,
David Collinsc7642322012-04-04 10:19:12 -07001268 .enable_time = rpm_vreg_enable_time,
1269};
1270
1271static struct regulator_ops *vreg_ops[] = {
1272 [RPM_REGULATOR_SMD_TYPE_LDO] = &ldo_ops,
1273 [RPM_REGULATOR_SMD_TYPE_SMPS] = &smps_ops,
1274 [RPM_REGULATOR_SMD_TYPE_VS] = &switch_ops,
1275 [RPM_REGULATOR_SMD_TYPE_NCP] = &ncp_ops,
1276};
1277
1278static int __devexit rpm_vreg_device_remove(struct platform_device *pdev)
1279{
1280 struct device *dev = &pdev->dev;
1281 struct rpm_regulator *reg;
David Collins1a0e8a62013-10-25 09:51:42 -07001282 struct rpm_vreg *rpm_vreg;
David Collinsc7642322012-04-04 10:19:12 -07001283
1284 reg = platform_get_drvdata(pdev);
1285 if (reg) {
David Collins1a0e8a62013-10-25 09:51:42 -07001286 rpm_vreg = reg->rpm_vreg;
1287 rpm_vreg_lock(rpm_vreg);
David Collinsc7642322012-04-04 10:19:12 -07001288 regulator_unregister(reg->rdev);
1289 list_del(&reg->list);
1290 kfree(reg);
David Collins1a0e8a62013-10-25 09:51:42 -07001291 rpm_vreg_unlock(rpm_vreg);
David Collinsc7642322012-04-04 10:19:12 -07001292 } else {
1293 dev_err(dev, "%s: drvdata missing\n", __func__);
1294 return -EINVAL;
1295 }
1296
1297 platform_set_drvdata(pdev, NULL);
1298
1299 return 0;
1300}
1301
1302static int __devexit rpm_vreg_resource_remove(struct platform_device *pdev)
1303{
1304 struct device *dev = &pdev->dev;
1305 struct rpm_regulator *reg, *reg_temp;
1306 struct rpm_vreg *rpm_vreg;
1307
1308 rpm_vreg = platform_get_drvdata(pdev);
1309 if (rpm_vreg) {
1310 rpm_vreg_lock(rpm_vreg);
1311 list_for_each_entry_safe(reg, reg_temp, &rpm_vreg->reg_list,
1312 list) {
1313 /* Only touch data for private consumers. */
1314 if (reg->rdev->desc == NULL) {
1315 list_del(&reg->list);
1316 kfree(reg->rdev);
1317 kfree(reg);
1318 } else {
1319 dev_err(dev, "%s: not all child devices have "
1320 "been removed\n", __func__);
1321 }
1322 }
1323 rpm_vreg_unlock(rpm_vreg);
1324
1325 msm_rpm_free_request(rpm_vreg->handle_active);
1326 msm_rpm_free_request(rpm_vreg->handle_sleep);
1327
1328 kfree(rpm_vreg);
1329 } else {
1330 dev_err(dev, "%s: drvdata missing\n", __func__);
1331 return -EINVAL;
1332 }
1333
1334 platform_set_drvdata(pdev, NULL);
1335
1336 return 0;
1337}
1338
1339/*
1340 * This probe is called for child rpm-regulator devices which have
1341 * properties which are required to configure individual regulator
1342 * framework regulators for a given RPM regulator resource.
1343 */
1344static int __devinit rpm_vreg_device_probe(struct platform_device *pdev)
1345{
1346 struct device *dev = &pdev->dev;
1347 struct device_node *node = dev->of_node;
1348 struct regulator_init_data *init_data;
1349 struct rpm_vreg *rpm_vreg;
1350 struct rpm_regulator *reg;
1351 int rc = 0;
1352 int i, regulator_type;
1353 u32 val;
1354
1355 if (!dev->of_node) {
1356 dev_err(dev, "%s: device tree information missing\n", __func__);
1357 return -ENODEV;
1358 }
1359
1360 if (pdev->dev.parent == NULL) {
1361 dev_err(dev, "%s: parent device missing\n", __func__);
1362 return -ENODEV;
1363 }
1364
1365 rpm_vreg = dev_get_drvdata(pdev->dev.parent);
1366 if (rpm_vreg == NULL) {
1367 dev_err(dev, "%s: rpm_vreg not found in parent device\n",
1368 __func__);
1369 return -ENODEV;
1370 }
1371
1372 reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
1373 if (reg == NULL) {
1374 dev_err(dev, "%s: could not allocate memory for reg\n",
1375 __func__);
1376 return -ENOMEM;
1377 }
1378
1379 regulator_type = rpm_vreg->regulator_type;
1380 reg->rpm_vreg = rpm_vreg;
1381 reg->rdesc.ops = vreg_ops[regulator_type];
1382 reg->rdesc.owner = THIS_MODULE;
1383 reg->rdesc.type = REGULATOR_VOLTAGE;
1384
David Collins4208ce32012-06-15 13:33:13 -07001385 /*
1386 * Switch to voltage corner regulator ops if qcom,use-voltage-corner
David Collins23c96cd2012-10-05 17:12:00 -07001387 * is specified in the device node (SMPS and LDO only).
David Collins4208ce32012-06-15 13:33:13 -07001388 */
David Collins23c96cd2012-10-05 17:12:00 -07001389 if (of_property_read_bool(node, "qcom,use-voltage-corner")) {
David Collins9deea5b2013-03-05 10:16:01 -08001390 if (of_property_read_bool(node,
1391 "qcom,use-voltage-floor-corner")) {
1392 dev_err(dev, "%s: invalid properties: both qcom,use-voltage-corner and qcom,use-voltage-floor-corner specified\n",
1393 __func__);
1394 goto fail_free_reg;
1395 }
1396
David Collins23c96cd2012-10-05 17:12:00 -07001397 if (regulator_type == RPM_REGULATOR_SMD_TYPE_SMPS)
1398 reg->rdesc.ops = &smps_corner_ops;
1399 else if (regulator_type == RPM_REGULATOR_SMD_TYPE_LDO)
1400 reg->rdesc.ops = &ldo_corner_ops;
David Collins9deea5b2013-03-05 10:16:01 -08001401 } else if (of_property_read_bool(node,
1402 "qcom,use-voltage-floor-corner")) {
1403 if (regulator_type == RPM_REGULATOR_SMD_TYPE_SMPS)
1404 reg->rdesc.ops = &smps_floor_corner_ops;
1405 else if (regulator_type == RPM_REGULATOR_SMD_TYPE_LDO)
1406 reg->rdesc.ops = &ldo_floor_corner_ops;
David Collins23c96cd2012-10-05 17:12:00 -07001407 }
David Collins4208ce32012-06-15 13:33:13 -07001408
David Collins409a9fa2013-03-05 10:54:25 -08001409 reg->always_send_voltage
1410 = of_property_read_bool(node, "qcom,always-send-voltage");
1411 reg->always_send_current
1412 = of_property_read_bool(node, "qcom,always-send-current");
1413
David Collinsc7642322012-04-04 10:19:12 -07001414 if (regulator_type == RPM_REGULATOR_SMD_TYPE_VS)
1415 reg->rdesc.n_voltages = 0;
1416 else
1417 reg->rdesc.n_voltages = 2;
1418
1419 rc = of_property_read_u32(node, "qcom,set", &val);
1420 if (rc) {
1421 dev_err(dev, "%s: sleep set and/or active set must be "
1422 "configured via qcom,set property, rc=%d\n", __func__,
1423 rc);
1424 goto fail_free_reg;
1425 } else if (!(val & RPM_SET_CONFIG_BOTH)) {
1426 dev_err(dev, "%s: qcom,set=%u property is invalid\n", __func__,
1427 val);
1428 rc = -EINVAL;
1429 goto fail_free_reg;
1430 }
1431
1432 reg->set_active = !!(val & RPM_SET_CONFIG_ACTIVE);
1433 reg->set_sleep = !!(val & RPM_SET_CONFIG_SLEEP);
1434
David Collins4853ae42012-06-12 09:37:19 -07001435 init_data = of_get_regulator_init_data(dev, node);
David Collinsc7642322012-04-04 10:19:12 -07001436 if (init_data == NULL) {
1437 dev_err(dev, "%s: unable to allocate memory\n", __func__);
1438 rc = -ENOMEM;
1439 goto fail_free_reg;
1440 }
1441 if (init_data->constraints.name == NULL) {
1442 dev_err(dev, "%s: regulator name not specified\n", __func__);
1443 rc = -EINVAL;
1444 goto fail_free_reg;
1445 }
1446
1447 init_data->constraints.input_uV = init_data->constraints.max_uV;
1448
1449 if (of_get_property(node, "parent-supply", NULL))
1450 init_data->supply_regulator = "parent";
1451
1452 /*
1453 * Fill in ops and mode masks based on callbacks specified for
1454 * this type of regulator.
1455 */
1456 if (reg->rdesc.ops->enable)
1457 init_data->constraints.valid_ops_mask
1458 |= REGULATOR_CHANGE_STATUS;
1459 if (reg->rdesc.ops->get_voltage)
1460 init_data->constraints.valid_ops_mask
1461 |= REGULATOR_CHANGE_VOLTAGE;
1462 if (reg->rdesc.ops->get_mode) {
1463 init_data->constraints.valid_ops_mask
1464 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1465 init_data->constraints.valid_modes_mask
1466 |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1467 }
1468
1469 reg->rdesc.name = init_data->constraints.name;
1470 reg->min_uV = init_data->constraints.min_uV;
1471 reg->max_uV = init_data->constraints.max_uV;
1472
1473 /* Initialize the param array based on optional properties. */
1474 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
1475 rc = of_property_read_u32(node, params[i].property_name, &val);
1476 if (rc == 0) {
1477 if (params[i].supported_regulator_types
1478 & BIT(regulator_type)) {
1479 if (val < params[i].min
1480 || val > params[i].max) {
1481 pr_warn("%s: device tree property: "
1482 "%s=%u is outsided allowed "
1483 "range [%u, %u]\n",
1484 reg->rdesc.name,
1485 params[i].property_name, val,
1486 params[i].min, params[i].max);
1487 continue;
1488 }
1489 reg->req.param[i] = val;
1490 reg->req.modified |= BIT(i);
1491 } else {
1492 pr_warn("%s: regulator type=%d does not support"
1493 " device tree property: %s\n",
1494 reg->rdesc.name, regulator_type,
1495 params[i].property_name);
1496 }
1497 }
1498 }
1499
David Collinsa3e70de2012-10-05 14:38:13 -07001500 of_property_read_u32(node, "qcom,system-load", &reg->system_load);
David Collinsc7642322012-04-04 10:19:12 -07001501
1502 rpm_vreg_lock(rpm_vreg);
1503 list_add(&reg->list, &rpm_vreg->reg_list);
1504 rpm_vreg_unlock(rpm_vreg);
1505
1506 reg->rdev = regulator_register(&reg->rdesc, dev, init_data, reg, node);
1507 if (IS_ERR(reg->rdev)) {
1508 rc = PTR_ERR(reg->rdev);
1509 reg->rdev = NULL;
1510 pr_err("regulator_register failed: %s, rc=%d\n",
1511 reg->rdesc.name, rc);
1512 goto fail_remove_from_list;
1513 }
1514
1515 platform_set_drvdata(pdev, reg);
1516
1517 pr_debug("successfully probed: %s\n", reg->rdesc.name);
1518
1519 return 0;
1520
1521fail_remove_from_list:
1522 rpm_vreg_lock(rpm_vreg);
1523 list_del(&reg->list);
1524 rpm_vreg_unlock(rpm_vreg);
1525
1526fail_free_reg:
1527 kfree(reg);
1528 return rc;
1529}
1530
1531/*
1532 * This probe is called for parent rpm-regulator devices which have
1533 * properties which are required to identify a given RPM resource.
1534 */
1535static int __devinit rpm_vreg_resource_probe(struct platform_device *pdev)
1536{
1537 struct device *dev = &pdev->dev;
1538 struct device_node *node = dev->of_node;
1539 struct rpm_vreg *rpm_vreg;
1540 int val = 0;
1541 u32 resource_type;
1542 int rc;
1543
1544 if (!dev->of_node) {
1545 dev_err(dev, "%s: device tree information missing\n", __func__);
1546 return -ENODEV;
1547 }
1548
1549 /* Create new rpm_vreg entry. */
1550 rpm_vreg = kzalloc(sizeof(struct rpm_vreg), GFP_KERNEL);
1551 if (rpm_vreg == NULL) {
1552 dev_err(dev, "%s: could not allocate memory for vreg\n",
1553 __func__);
1554 return -ENOMEM;
1555 }
1556
1557 /* Required device tree properties: */
1558 rc = of_property_read_string(node, "qcom,resource-name",
1559 &rpm_vreg->resource_name);
1560 if (rc) {
1561 dev_err(dev, "%s: qcom,resource-name missing in DT node\n",
1562 __func__);
1563 goto fail_free_vreg;
1564 }
1565 resource_type = rpm_vreg_string_to_int(rpm_vreg->resource_name);
1566
1567 rc = of_property_read_u32(node, "qcom,resource-id",
1568 &rpm_vreg->resource_id);
1569 if (rc) {
1570 dev_err(dev, "%s: qcom,resource-id missing in DT node\n",
1571 __func__);
1572 goto fail_free_vreg;
1573 }
1574
1575 rc = of_property_read_u32(node, "qcom,regulator-type",
1576 &rpm_vreg->regulator_type);
1577 if (rc) {
1578 dev_err(dev, "%s: qcom,regulator-type missing in DT node\n",
1579 __func__);
1580 goto fail_free_vreg;
1581 }
1582
1583 if ((rpm_vreg->regulator_type < 0)
1584 || (rpm_vreg->regulator_type >= RPM_REGULATOR_SMD_TYPE_MAX)) {
1585 dev_err(dev, "%s: invalid regulator type: %d\n", __func__,
1586 rpm_vreg->regulator_type);
1587 rc = -EINVAL;
1588 goto fail_free_vreg;
1589 }
1590
1591 /* Optional device tree properties: */
1592 of_property_read_u32(node, "qcom,allow-atomic", &val);
1593 rpm_vreg->allow_atomic = !!val;
1594 of_property_read_u32(node, "qcom,enable-time", &rpm_vreg->enable_time);
1595 of_property_read_u32(node, "qcom,hpm-min-load",
1596 &rpm_vreg->hpm_min_load);
Ashay Jaiswala2923902013-08-31 19:39:57 +05301597 rpm_vreg->apps_only = of_property_read_bool(node, "qcom,apps-only");
David Collinsc7642322012-04-04 10:19:12 -07001598
1599 rpm_vreg->handle_active = msm_rpm_create_request(RPM_SET_ACTIVE,
1600 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1601 if (rpm_vreg->handle_active == NULL
1602 || IS_ERR(rpm_vreg->handle_active)) {
1603 rc = PTR_ERR(rpm_vreg->handle_active);
1604 dev_err(dev, "%s: failed to create active RPM handle, rc=%d\n",
1605 __func__, rc);
1606 goto fail_free_vreg;
1607 }
1608
1609 rpm_vreg->handle_sleep = msm_rpm_create_request(RPM_SET_SLEEP,
1610 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1611 if (rpm_vreg->handle_sleep == NULL || IS_ERR(rpm_vreg->handle_sleep)) {
1612 rc = PTR_ERR(rpm_vreg->handle_sleep);
1613 dev_err(dev, "%s: failed to create sleep RPM handle, rc=%d\n",
1614 __func__, rc);
1615 goto fail_free_handle_active;
1616 }
1617
1618 INIT_LIST_HEAD(&rpm_vreg->reg_list);
1619
1620 if (rpm_vreg->allow_atomic)
1621 spin_lock_init(&rpm_vreg->slock);
1622 else
1623 mutex_init(&rpm_vreg->mlock);
1624
1625 platform_set_drvdata(pdev, rpm_vreg);
1626
1627 rc = of_platform_populate(node, NULL, NULL, dev);
1628 if (rc) {
1629 dev_err(dev, "%s: failed to add child nodes, rc=%d\n", __func__,
1630 rc);
1631 goto fail_unset_drvdata;
1632 }
1633
1634 pr_debug("successfully probed: %s (%08X) %u\n", rpm_vreg->resource_name,
1635 resource_type, rpm_vreg->resource_id);
1636
1637 return rc;
1638
1639fail_unset_drvdata:
1640 platform_set_drvdata(pdev, NULL);
1641 msm_rpm_free_request(rpm_vreg->handle_sleep);
1642
1643fail_free_handle_active:
1644 msm_rpm_free_request(rpm_vreg->handle_active);
1645
1646fail_free_vreg:
1647 kfree(rpm_vreg);
1648
1649 return rc;
1650}
1651
1652static struct of_device_id rpm_vreg_match_table_device[] = {
1653 { .compatible = "qcom,rpm-regulator-smd", },
1654 {}
1655};
1656
1657static struct of_device_id rpm_vreg_match_table_resource[] = {
1658 { .compatible = "qcom,rpm-regulator-smd-resource", },
1659 {}
1660};
1661
1662static struct platform_driver rpm_vreg_device_driver = {
1663 .probe = rpm_vreg_device_probe,
1664 .remove = __devexit_p(rpm_vreg_device_remove),
1665 .driver = {
1666 .name = "qcom,rpm-regulator-smd",
1667 .owner = THIS_MODULE,
1668 .of_match_table = rpm_vreg_match_table_device,
1669 },
1670};
1671
1672static struct platform_driver rpm_vreg_resource_driver = {
1673 .probe = rpm_vreg_resource_probe,
1674 .remove = __devexit_p(rpm_vreg_resource_remove),
1675 .driver = {
1676 .name = "qcom,rpm-regulator-smd-resource",
1677 .owner = THIS_MODULE,
1678 .of_match_table = rpm_vreg_match_table_resource,
1679 },
1680};
1681
1682/**
1683 * rpm_regulator_smd_driver_init() - initialized SMD RPM regulator driver
1684 *
1685 * This function registers the SMD RPM regulator platform drivers.
1686 *
1687 * Returns 0 on success or errno on failure.
1688 */
1689int __init rpm_regulator_smd_driver_init(void)
1690{
1691 static bool initialized;
1692 int i, rc;
1693
1694 if (initialized)
1695 return 0;
1696 else
1697 initialized = true;
1698
1699 /* Store parameter string names as integers */
1700 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++)
1701 params[i].key = rpm_vreg_string_to_int(params[i].name);
1702
1703 rc = platform_driver_register(&rpm_vreg_device_driver);
1704 if (rc)
1705 return rc;
1706
1707 return platform_driver_register(&rpm_vreg_resource_driver);
1708}
1709EXPORT_SYMBOL_GPL(rpm_regulator_smd_driver_init);
1710
1711static void __exit rpm_vreg_exit(void)
1712{
1713 platform_driver_unregister(&rpm_vreg_device_driver);
1714 platform_driver_unregister(&rpm_vreg_resource_driver);
1715}
1716
1717module_init(rpm_regulator_smd_driver_init);
1718module_exit(rpm_vreg_exit);
1719
1720MODULE_LICENSE("GPL v2");
1721MODULE_DESCRIPTION("MSM SMD RPM regulator driver");