blob: fdff2316ae9901faf48e84b059fa9f526b822b3c [file] [log] [blame]
David Collinsc7642322012-04-04 10:19:12 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
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 Collinsc7642322012-04-04 10:19:12 -070071 RPM_REGULATOR_PARAM_MAX,
72};
73
74#define RPM_SET_CONFIG_ACTIVE BIT(0)
75#define RPM_SET_CONFIG_SLEEP BIT(1)
76#define RPM_SET_CONFIG_BOTH (RPM_SET_CONFIG_ACTIVE \
77 | RPM_SET_CONFIG_SLEEP)
78struct rpm_regulator_param {
79 char *name;
80 char *property_name;
81 u32 key;
82 u32 min;
83 u32 max;
84 u32 supported_regulator_types;
85};
86
87#define PARAM(_idx, _support_ldo, _support_smps, _support_vs, _support_ncp, \
88 _name, _min, _max, _property_name) \
89 [RPM_REGULATOR_PARAM_##_idx] = { \
90 .name = _name, \
91 .property_name = _property_name, \
92 .min = _min, \
93 .max = _max, \
94 .supported_regulator_types = \
95 _support_ldo << RPM_REGULATOR_SMD_TYPE_LDO | \
96 _support_smps << RPM_REGULATOR_SMD_TYPE_SMPS | \
97 _support_vs << RPM_REGULATOR_SMD_TYPE_VS | \
98 _support_ncp << RPM_REGULATOR_SMD_TYPE_NCP, \
99 }
100
101static struct rpm_regulator_param params[RPM_REGULATOR_PARAM_MAX] = {
102 /* ID LDO SMPS VS NCP name min max property-name */
103 PARAM(ENABLE, 1, 1, 1, 1, "swen", 0, 1, "qcom,init-enable"),
104 PARAM(VOLTAGE, 1, 1, 0, 1, "uv", 0, 0x7FFFFFF, "qcom,init-voltage"),
105 PARAM(CURRENT, 1, 1, 0, 0, "ma", 0, 0x1FFF, "qcom,init-current"),
106 PARAM(MODE_LDO, 1, 0, 0, 0, "lsmd", 0, 1, "qcom,init-ldo-mode"),
107 PARAM(MODE_SMPS, 0, 1, 0, 0, "ssmd", 0, 2, "qcom,init-smps-mode"),
108 PARAM(PIN_CTRL_ENABLE, 1, 1, 1, 0, "pcen", 0, 0xF, "qcom,init-pin-ctrl-enable"),
109 PARAM(PIN_CTRL_MODE, 1, 1, 1, 0, "pcmd", 0, 0x1F, "qcom,init-pin-ctrl-mode"),
110 PARAM(FREQUENCY, 0, 1, 0, 1, "freq", 0, 16, "qcom,init-frequency"),
111 PARAM(HEAD_ROOM, 1, 0, 0, 1, "hr", 0, 0x7FFFFFFF, "qcom,init-head-room"),
112 PARAM(QUIET_MODE, 0, 1, 0, 0, "qm", 0, 2, "qcom,init-quiet-mode"),
113 PARAM(FREQ_REASON, 0, 1, 0, 1, "resn", 0, 8, "qcom,init-freq-reason"),
David Collins4208ce32012-06-15 13:33:13 -0700114 PARAM(CORNER, 0, 1, 0, 0, "corn", 0, 5, "qcom,init-voltage-corner"),
David Collinsc7642322012-04-04 10:19:12 -0700115};
116
117struct rpm_vreg_request {
118 u32 param[RPM_REGULATOR_PARAM_MAX];
119 u32 valid;
120 u32 modified;
121};
122
123struct rpm_vreg {
124 struct rpm_vreg_request aggr_req_active;
125 struct rpm_vreg_request aggr_req_sleep;
126 struct list_head reg_list;
127 const char *resource_name;
128 u32 resource_id;
129 bool allow_atomic;
130 int regulator_type;
131 int hpm_min_load;
132 int enable_time;
133 struct spinlock slock;
134 struct mutex mlock;
135 unsigned long flags;
136 bool sleep_request_sent;
137 struct msm_rpm_request *handle_active;
138 struct msm_rpm_request *handle_sleep;
139};
140
141struct rpm_regulator {
142 struct regulator_desc rdesc;
143 struct regulator_dev *rdev;
144 struct rpm_vreg *rpm_vreg;
145 struct list_head list;
146 bool set_active;
147 bool set_sleep;
148 struct rpm_vreg_request req;
149 int system_load;
150 int min_uV;
151 int max_uV;
152};
153
154/*
155 * This voltage in uV is returned by get_voltage functions when there is no way
156 * to determine the current voltage level. It is needed because the regulator
157 * framework treats a 0 uV voltage as an error.
158 */
159#define VOLTAGE_UNKNOWN 1
160
161/*
162 * Regulator requests sent in the active set take effect immediately. Requests
163 * sent in the sleep set take effect when the Apps processor transitions into
164 * RPM assisted power collapse. For any given regulator, if an active set
165 * request is present, but not a sleep set request, then the active set request
166 * is used at all times, even when the Apps processor is power collapsed.
167 *
168 * The rpm-regulator-smd takes advantage of this default usage of the active set
169 * request by only sending a sleep set request if it differs from the
170 * corresponding active set request.
171 */
172#define RPM_SET_ACTIVE MSM_RPM_CTX_ACTIVE_SET
173#define RPM_SET_SLEEP MSM_RPM_CTX_SLEEP_SET
174
175static u32 rpm_vreg_string_to_int(const u8 *str)
176{
177 int i, len;
178 u32 output = 0;
179
180 len = strnlen(str, sizeof(u32));
181 for (i = 0; i < len; i++)
182 output |= str[i] << (i * 8);
183
184 return output;
185}
186
187static inline void rpm_vreg_lock(struct rpm_vreg *rpm_vreg)
188{
189 if (rpm_vreg->allow_atomic)
190 spin_lock_irqsave(&rpm_vreg->slock, rpm_vreg->flags);
191 else
192 mutex_lock(&rpm_vreg->mlock);
193}
194
195static inline void rpm_vreg_unlock(struct rpm_vreg *rpm_vreg)
196{
197 if (rpm_vreg->allow_atomic)
198 spin_unlock_irqrestore(&rpm_vreg->slock, rpm_vreg->flags);
199 else
200 mutex_unlock(&rpm_vreg->mlock);
201}
202
203static inline bool rpm_vreg_active_or_sleep_enabled(struct rpm_vreg *rpm_vreg)
204{
205 return (rpm_vreg->aggr_req_active.param[RPM_REGULATOR_PARAM_ENABLE]
206 && (rpm_vreg->aggr_req_active.valid
207 & BIT(RPM_REGULATOR_PARAM_ENABLE)))
208 || ((rpm_vreg->aggr_req_sleep.param[RPM_REGULATOR_PARAM_ENABLE])
209 && (rpm_vreg->aggr_req_sleep.valid
210 & BIT(RPM_REGULATOR_PARAM_ENABLE)));
211}
212
213/*
214 * This is used when voting for LPM or HPM by subtracting or adding to the
215 * hpm_min_load of a regulator. It has units of uA.
216 */
217#define LOAD_THRESHOLD_STEP 1000
218
219static inline int rpm_vreg_hpm_min_uA(struct rpm_vreg *rpm_vreg)
220{
221 return rpm_vreg->hpm_min_load;
222}
223
224static inline int rpm_vreg_lpm_max_uA(struct rpm_vreg *rpm_vreg)
225{
226 return rpm_vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
227}
228
229#define MICRO_TO_MILLI(uV) ((uV) / 1000)
230#define MILLI_TO_MICRO(uV) ((uV) * 1000)
231
232#define DEBUG_PRINT_BUFFER_SIZE 512
233#define REQ_SENT 0
234#define REQ_PREV 1
235#define REQ_CACHED 2
236#define REQ_TYPES 3
237
238static void rpm_regulator_req(struct rpm_regulator *regulator, int set,
239 bool sent)
240{
241 char buf[DEBUG_PRINT_BUFFER_SIZE];
242 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
243 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
244 struct rpm_vreg_request *aggr;
245 bool first;
246 u32 mask[REQ_TYPES] = {0, 0, 0};
247 const char *req_names[REQ_TYPES] = {"sent", "prev", "cached"};
248 int pos = 0;
249 int i, j;
250
251 aggr = (set == RPM_SET_ACTIVE)
252 ? &rpm_vreg->aggr_req_active : &rpm_vreg->aggr_req_sleep;
253
254 if (rpm_vreg_debug_mask & RPM_VREG_DEBUG_DUPLICATE) {
255 mask[REQ_SENT] = aggr->modified;
256 mask[REQ_PREV] = aggr->valid & ~aggr->modified;
257 } else if (sent
258 && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_FULL_REQUEST)) {
259 mask[REQ_SENT] = aggr->modified;
260 mask[REQ_PREV] = aggr->valid & ~aggr->modified;
261 } else if (sent && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_REQUEST)) {
262 mask[REQ_SENT] = aggr->modified;
263 }
264
265 if (!(mask[REQ_SENT] | mask[REQ_PREV]))
266 return;
267
268 if (set == RPM_SET_SLEEP && !rpm_vreg->sleep_request_sent) {
269 mask[REQ_CACHED] = mask[REQ_SENT] | mask[REQ_PREV];
270 mask[REQ_SENT] = 0;
271 mask[REQ_PREV] = 0;
272 }
273
274 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
275 KERN_INFO, __func__);
276
277 pos += scnprintf(buf + pos, buflen - pos, "%s %u (%s): s=%s",
278 rpm_vreg->resource_name, rpm_vreg->resource_id,
279 regulator->rdesc.name,
280 (set == RPM_SET_ACTIVE ? "act" : "slp"));
281
282 for (i = 0; i < REQ_TYPES; i++) {
283 if (mask[i])
284 pos += scnprintf(buf + pos, buflen - pos, "; %s: ",
285 req_names[i]);
286
287 first = true;
288 for (j = 0; j < RPM_REGULATOR_PARAM_MAX; j++) {
289 if (mask[i] & BIT(j)) {
290 pos += scnprintf(buf + pos, buflen - pos,
291 "%s%s=%u", (first ? "" : ", "),
292 params[j].name, aggr->param[j]);
293 first = false;
294 }
295 }
296 }
297
298 pos += scnprintf(buf + pos, buflen - pos, "\n");
299 printk(buf);
300}
301
302#define RPM_VREG_SET_PARAM(_regulator, _param, _val) \
303{ \
304 (_regulator)->req.param[RPM_REGULATOR_PARAM_##_param] = _val; \
305 (_regulator)->req.modified |= BIT(RPM_REGULATOR_PARAM_##_param); \
306} \
307
308static int rpm_vreg_add_kvp_to_request(struct rpm_vreg *rpm_vreg,
309 const u32 *param, int idx, u32 set)
310{
311 struct msm_rpm_request *handle;
312
313 handle = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active
314 : rpm_vreg->handle_sleep);
315
316 if (rpm_vreg->allow_atomic)
317 return msm_rpm_add_kvp_data_noirq(handle, params[idx].key,
318 (u8 *)&param[idx], 4);
319 else
320 return msm_rpm_add_kvp_data(handle, params[idx].key,
321 (u8 *)&param[idx], 4);
322}
323
324static void rpm_vreg_check_modified_requests(const u32 *prev_param,
325 const u32 *param, u32 prev_valid, u32 *modified)
326{
327 u32 value_changed = 0;
328 int i;
329
330 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
331 if (param[i] != prev_param[i])
332 value_changed |= BIT(i);
333 }
334
335 /*
336 * Only keep bits that are for changed parameters or previously
337 * invalid parameters.
338 */
339 *modified &= value_changed | ~prev_valid;
340}
341
342static int rpm_vreg_add_modified_requests(struct rpm_regulator *regulator,
343 u32 set, const u32 *param, u32 modified)
344{
345 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
346 int rc = 0;
347 int i;
348
349 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
350 /* Only send requests for modified parameters. */
351 if (modified & BIT(i)) {
352 rc = rpm_vreg_add_kvp_to_request(rpm_vreg, param, i,
353 set);
354 if (rc) {
355 vreg_err(regulator,
356 "add KVP failed: %s %u; %s, rc=%d\n",
357 rpm_vreg->resource_name,
358 rpm_vreg->resource_id, params[i].name,
359 rc);
360 return rc;
361 }
362 }
363 }
364
365 return rc;
366}
367
368static int rpm_vreg_send_request(struct rpm_regulator *regulator, u32 set)
369{
370 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
371 struct msm_rpm_request *handle
372 = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active
373 : rpm_vreg->handle_sleep);
374 int rc;
375
376 if (rpm_vreg->allow_atomic)
377 rc = msm_rpm_wait_for_ack_noirq(msm_rpm_send_request_noirq(
378 handle));
379 else
380 rc = msm_rpm_wait_for_ack(msm_rpm_send_request(handle));
381
382 if (rc)
383 vreg_err(regulator, "msm rpm send failed: %s %u; set=%s, "
384 "rc=%d\n", rpm_vreg->resource_name,
385 rpm_vreg->resource_id,
386 (set == RPM_SET_ACTIVE ? "act" : "slp"), rc);
387
388 return rc;
389}
390
391#define RPM_VREG_AGGR_MAX(_idx, _param_aggr, _param_reg) \
392{ \
393 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
394 = max(_param_aggr[RPM_REGULATOR_PARAM_##_idx], \
395 _param_reg[RPM_REGULATOR_PARAM_##_idx]); \
396}
397
398#define RPM_VREG_AGGR_SUM(_idx, _param_aggr, _param_reg) \
399{ \
400 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
401 += _param_reg[RPM_REGULATOR_PARAM_##_idx]; \
402}
403
404#define RPM_VREG_AGGR_OR(_idx, _param_aggr, _param_reg) \
405{ \
406 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
407 |= _param_reg[RPM_REGULATOR_PARAM_##_idx]; \
408}
409
410/*
411 * The RPM treats freq=0 as a special value meaning that this consumer does not
412 * care what the SMPS switching freqency is.
413 */
414#define RPM_REGULATOR_FREQ_DONT_CARE 0
415
416static inline void rpm_vreg_freqency_aggr(u32 *freq, u32 consumer_freq)
417{
418 if (consumer_freq != RPM_REGULATOR_FREQ_DONT_CARE
419 && (consumer_freq < *freq
420 || *freq == RPM_REGULATOR_FREQ_DONT_CARE))
421 *freq = consumer_freq;
422}
423
424/*
425 * Aggregation is performed on each parameter based on the way that the RPM
426 * aggregates that type internally between RPM masters.
427 */
428static void rpm_vreg_aggregate_params(u32 *param_aggr, const u32 *param_reg)
429{
430 RPM_VREG_AGGR_MAX(ENABLE, param_aggr, param_reg);
431 RPM_VREG_AGGR_MAX(VOLTAGE, param_aggr, param_reg);
432 RPM_VREG_AGGR_SUM(CURRENT, param_aggr, param_reg);
433 RPM_VREG_AGGR_MAX(MODE_LDO, param_aggr, param_reg);
434 RPM_VREG_AGGR_MAX(MODE_SMPS, param_aggr, param_reg);
435 RPM_VREG_AGGR_OR(PIN_CTRL_ENABLE, param_aggr, param_reg);
436 RPM_VREG_AGGR_OR(PIN_CTRL_MODE, param_aggr, param_reg);
437 rpm_vreg_freqency_aggr(&param_aggr[RPM_REGULATOR_PARAM_FREQUENCY],
438 param_reg[RPM_REGULATOR_PARAM_FREQUENCY]);
439 RPM_VREG_AGGR_MAX(HEAD_ROOM, param_aggr, param_reg);
440 RPM_VREG_AGGR_MAX(QUIET_MODE, param_aggr, param_reg);
441 RPM_VREG_AGGR_MAX(FREQ_REASON, param_aggr, param_reg);
David Collins4208ce32012-06-15 13:33:13 -0700442 RPM_VREG_AGGR_MAX(CORNER, param_aggr, param_reg);
David Collinsc7642322012-04-04 10:19:12 -0700443}
444
445static int rpm_vreg_aggregate_requests(struct rpm_regulator *regulator)
446{
447 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
448 u32 param_active[RPM_REGULATOR_PARAM_MAX];
449 u32 param_sleep[RPM_REGULATOR_PARAM_MAX];
450 u32 modified_active, modified_sleep;
451 struct rpm_regulator *reg;
452 bool sleep_set_differs = false;
453 bool send_active = false;
454 bool send_sleep = false;
455 int rc = 0;
456 int i;
457
458 memset(param_active, 0, sizeof(param_active));
459 memset(param_sleep, 0, sizeof(param_sleep));
460 modified_active = rpm_vreg->aggr_req_active.modified;
461 modified_sleep = rpm_vreg->aggr_req_sleep.modified;
462
463 /*
464 * Aggregate all of the requests for this regulator in both active
465 * and sleep sets.
466 */
467 list_for_each_entry(reg, &rpm_vreg->reg_list, list) {
468 if (reg->set_active) {
469 rpm_vreg_aggregate_params(param_active, reg->req.param);
470 modified_active |= reg->req.modified;
471 }
472 if (reg->set_sleep) {
473 rpm_vreg_aggregate_params(param_sleep, reg->req.param);
474 modified_sleep |= reg->req.modified;
475 }
476 }
477
478 /*
479 * Check if the aggregated sleep set parameter values differ from the
480 * aggregated active set parameter values.
481 */
482 if (!rpm_vreg->sleep_request_sent) {
483 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
484 if ((param_active[i] != param_sleep[i])
485 && (modified_sleep & BIT(i))) {
486 sleep_set_differs = true;
487 break;
488 }
489 }
490 }
491
492 /* Add KVPs to the active set RPM request if they have new values. */
493 rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_active.param,
494 param_active, rpm_vreg->aggr_req_active.valid,
495 &modified_active);
496 rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_ACTIVE,
497 param_active, modified_active);
498 if (rc)
499 return rc;
500 send_active = modified_active;
501
502 /*
503 * Sleep set configurations are only sent if they differ from the
504 * active set values. This is because the active set values will take
505 * effect during rpm assisted power collapse in the absence of sleep set
506 * values.
507 *
508 * However, once a sleep set request is sent for a given regulator,
509 * additional sleep set requests must be sent in the future even if they
510 * match the corresponding active set requests.
511 */
512 if (rpm_vreg->sleep_request_sent || sleep_set_differs) {
513 /* Add KVPs to the sleep set RPM request if they are new. */
514 rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_sleep.param,
515 param_sleep, rpm_vreg->aggr_req_sleep.valid,
516 &modified_sleep);
517 rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_SLEEP,
518 param_sleep, modified_sleep);
519 if (rc)
520 return rc;
521 send_sleep = modified_sleep;
522 }
523
524 /* Send active set request to the RPM if it contains new KVPs. */
525 if (send_active) {
526 rc = rpm_vreg_send_request(regulator, RPM_SET_ACTIVE);
527 if (rc)
528 return rc;
529 rpm_vreg->aggr_req_active.valid |= modified_active;
530 }
531 /* Store the results of the aggregation. */
532 rpm_vreg->aggr_req_active.modified = modified_active;
533 memcpy(rpm_vreg->aggr_req_active.param, param_active,
534 sizeof(param_active));
535
536 /* Handle debug printing of the active set request. */
537 rpm_regulator_req(regulator, RPM_SET_ACTIVE, send_active);
538 if (send_active)
539 rpm_vreg->aggr_req_active.modified = 0;
540
541 /* Send sleep set request to the RPM if it contains new KVPs. */
542 if (send_sleep) {
543 rc = rpm_vreg_send_request(regulator, RPM_SET_SLEEP);
544 if (rc)
545 return rc;
546 else
547 rpm_vreg->sleep_request_sent = true;
548 rpm_vreg->aggr_req_sleep.valid |= modified_sleep;
549 }
550 /* Store the results of the aggregation. */
551 rpm_vreg->aggr_req_sleep.modified = modified_sleep;
552 memcpy(rpm_vreg->aggr_req_sleep.param, param_sleep,
553 sizeof(param_sleep));
554
555 /* Handle debug printing of the sleep set request. */
556 rpm_regulator_req(regulator, RPM_SET_SLEEP, send_sleep);
557 if (send_sleep)
558 rpm_vreg->aggr_req_sleep.modified = 0;
559
560 /*
561 * Loop over all requests for this regulator to update the valid and
562 * modified values for use in future aggregation.
563 */
564 list_for_each_entry(reg, &rpm_vreg->reg_list, list) {
565 reg->req.valid |= reg->req.modified;
566 reg->req.modified = 0;
567 }
568
569 return rc;
570}
571
572static int rpm_vreg_is_enabled(struct regulator_dev *rdev)
573{
574 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
575
576 return reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
577}
578
579static int rpm_vreg_enable(struct regulator_dev *rdev)
580{
581 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
582 int rc;
583 u32 prev_enable;
584
585 rpm_vreg_lock(reg->rpm_vreg);
586
587 prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
588 RPM_VREG_SET_PARAM(reg, ENABLE, 1);
589 rc = rpm_vreg_aggregate_requests(reg);
590 if (rc) {
591 vreg_err(reg, "enable failed, rc=%d", rc);
592 RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable);
593 }
594
595 rpm_vreg_unlock(reg->rpm_vreg);
596
597 return rc;
598}
599
600static int rpm_vreg_disable(struct regulator_dev *rdev)
601{
602 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
603 int rc;
604 u32 prev_enable;
605
606 rpm_vreg_lock(reg->rpm_vreg);
607
608 prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
609 RPM_VREG_SET_PARAM(reg, ENABLE, 0);
610 rc = rpm_vreg_aggregate_requests(reg);
611 if (rc) {
612 vreg_err(reg, "enable failed, rc=%d", rc);
613 RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable);
614 }
615
616 rpm_vreg_unlock(reg->rpm_vreg);
617
618 return rc;
619}
620
621static int rpm_vreg_set_voltage(struct regulator_dev *rdev, int min_uV,
622 int max_uV, unsigned *selector)
623{
624 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
625 int rc = 0;
626 u32 prev_voltage;
627
628 rpm_vreg_lock(reg->rpm_vreg);
629
630 prev_voltage = reg->req.param[RPM_REGULATOR_PARAM_VOLTAGE];
631 RPM_VREG_SET_PARAM(reg, VOLTAGE, min_uV);
632
633 /* Only send a new voltage if the regulator is currently enabled. */
634 if (rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg))
635 rc = rpm_vreg_aggregate_requests(reg);
636
637 if (rc) {
638 vreg_err(reg, "set voltage failed, rc=%d", rc);
639 RPM_VREG_SET_PARAM(reg, VOLTAGE, prev_voltage);
640 }
641
642 rpm_vreg_unlock(reg->rpm_vreg);
643
644 return rc;
645}
646
647static int rpm_vreg_get_voltage(struct regulator_dev *rdev)
648{
649 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
650 int uV;
651
652 uV = reg->req.param[RPM_REGULATOR_PARAM_VOLTAGE];
653 if (uV == 0)
654 uV = VOLTAGE_UNKNOWN;
655
656 return uV;
657}
658
659static int rpm_vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
660{
661 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
662 int uV = 0;
663
664 if (selector == 0)
665 uV = reg->min_uV;
666 else if (selector == 1)
667 uV = reg->max_uV;
668
669 return uV;
670}
671
David Collins4208ce32012-06-15 13:33:13 -0700672static int rpm_vreg_set_voltage_corner(struct regulator_dev *rdev, int min_uV,
673 int max_uV, unsigned *selector)
674{
675 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
676 int rc = 0;
677 int corner;
678 u32 prev_corner;
679
680 /*
681 * Translate from values which work as inputs in the
682 * regulator_set_voltage function to the actual corner values
683 * sent to the RPM.
684 */
685 corner = min_uV - RPM_REGULATOR_CORNER_RETENTION;
686
687 if (corner < params[RPM_REGULATOR_PARAM_CORNER].min
688 || corner > params[RPM_REGULATOR_PARAM_CORNER].max) {
689 vreg_err(reg, "corner=%d is not within allowed range: [%u, %u]\n",
690 corner, params[RPM_REGULATOR_PARAM_CORNER].min,
691 params[RPM_REGULATOR_PARAM_CORNER].max);
692 return -EINVAL;
693 }
694
695 rpm_vreg_lock(reg->rpm_vreg);
696
697 prev_corner = reg->req.param[RPM_REGULATOR_PARAM_CORNER];
698 RPM_VREG_SET_PARAM(reg, CORNER, corner);
699
700 /* Only send a new voltage if the regulator is currently enabled. */
701 if (rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg))
702 rc = rpm_vreg_aggregate_requests(reg);
703
704 if (rc) {
705 vreg_err(reg, "set voltage corner failed, rc=%d", rc);
706 RPM_VREG_SET_PARAM(reg, CORNER, prev_corner);
707 }
708
709 rpm_vreg_unlock(reg->rpm_vreg);
710
711 return rc;
712}
713
714static int rpm_vreg_get_voltage_corner(struct regulator_dev *rdev)
715{
716 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
717
718 return reg->req.param[RPM_REGULATOR_PARAM_CORNER]
719 + RPM_REGULATOR_CORNER_RETENTION;
720}
721
David Collinsc7642322012-04-04 10:19:12 -0700722static int rpm_vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
723{
724 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
725 int rc = 0;
726 u32 prev_current;
727 int prev_uA;
728
729 rpm_vreg_lock(reg->rpm_vreg);
730
731 prev_current = reg->req.param[RPM_REGULATOR_PARAM_CURRENT];
732 prev_uA = MILLI_TO_MICRO(prev_current);
733
734 if (mode == REGULATOR_MODE_NORMAL) {
735 /* Make sure that request current is in HPM range. */
736 if (prev_uA < rpm_vreg_hpm_min_uA(reg->rpm_vreg))
737 RPM_VREG_SET_PARAM(reg, CURRENT,
738 MICRO_TO_MILLI(rpm_vreg_hpm_min_uA(reg->rpm_vreg)));
739 } else if (REGULATOR_MODE_IDLE) {
740 /* Make sure that request current is in LPM range. */
741 if (prev_uA > rpm_vreg_lpm_max_uA(reg->rpm_vreg))
742 RPM_VREG_SET_PARAM(reg, CURRENT,
743 MICRO_TO_MILLI(rpm_vreg_lpm_max_uA(reg->rpm_vreg)));
744 } else {
745 vreg_err(reg, "invalid mode: %u\n", mode);
746 rpm_vreg_unlock(reg->rpm_vreg);
747 return -EINVAL;
748 }
749
750 /* Only send a new mode value if the regulator is currently enabled. */
751 if (rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg))
752 rc = rpm_vreg_aggregate_requests(reg);
753
754 if (rc) {
755 vreg_err(reg, "set mode failed, rc=%d", rc);
756 RPM_VREG_SET_PARAM(reg, CURRENT, prev_current);
757 }
758
759 rpm_vreg_unlock(reg->rpm_vreg);
760
761 return rc;
762}
763
764static unsigned int rpm_vreg_get_mode(struct regulator_dev *rdev)
765{
766 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
767
768 return (reg->req.param[RPM_REGULATOR_PARAM_CURRENT]
769 >= MICRO_TO_MILLI(reg->rpm_vreg->hpm_min_load))
770 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
771}
772
773static unsigned int rpm_vreg_get_optimum_mode(struct regulator_dev *rdev,
774 int input_uV, int output_uV, int load_uA)
775{
776 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
777 u32 load_mA;
778
779 load_uA += reg->system_load;
780
781 load_mA = MICRO_TO_MILLI(load_uA);
782 if (load_mA > params[RPM_REGULATOR_PARAM_CURRENT].max)
783 load_mA = params[RPM_REGULATOR_PARAM_CURRENT].max;
784
785 rpm_vreg_lock(reg->rpm_vreg);
786 RPM_VREG_SET_PARAM(reg, CURRENT, MICRO_TO_MILLI(load_uA));
787 rpm_vreg_unlock(reg->rpm_vreg);
788
789 return (load_uA >= reg->rpm_vreg->hpm_min_load)
790 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
791}
792
793static int rpm_vreg_enable_time(struct regulator_dev *rdev)
794{
795 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
796
797 return reg->rpm_vreg->enable_time;
798}
799
800/**
801 * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
802 * @dev: device for regulator consumer
803 * @supply: supply name
804 *
805 * Returns a struct rpm_regulator corresponding to the regulator producer,
806 * or ERR_PTR() containing errno.
807 *
808 * This function may only be called from nonatomic context.
809 */
810struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
811{
812 struct rpm_regulator *framework_reg;
813 struct rpm_regulator *priv_reg = NULL;
814 struct regulator *regulator;
815 struct rpm_vreg *rpm_vreg;
816
817 regulator = regulator_get(dev, supply);
David Collins29ba0282012-06-18 10:01:52 -0700818 if (IS_ERR(regulator)) {
819 pr_err("could not find regulator for: dev=%s, supply=%s, rc=%ld\n",
820 (dev ? dev_name(dev) : ""), (supply ? supply : ""),
821 PTR_ERR(regulator));
822 return ERR_CAST(regulator);
David Collinsc7642322012-04-04 10:19:12 -0700823 }
824
825 framework_reg = regulator_get_drvdata(regulator);
826 if (framework_reg == NULL) {
827 pr_err("regulator structure not found.\n");
828 regulator_put(regulator);
829 return ERR_PTR(-ENODEV);
830 }
831 regulator_put(regulator);
832
833 rpm_vreg = framework_reg->rpm_vreg;
834
835 priv_reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
836 if (priv_reg == NULL) {
837 vreg_err(framework_reg, "could not allocate memory for "
838 "regulator\n");
839 rpm_vreg_unlock(rpm_vreg);
840 return ERR_PTR(-ENOMEM);
841 }
842
843 /*
844 * Allocate a regulator_dev struct so that framework callback functions
845 * can be called from the private API functions.
846 */
847 priv_reg->rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
848 if (priv_reg->rdev == NULL) {
849 vreg_err(framework_reg, "could not allocate memory for "
850 "regulator_dev\n");
851 kfree(priv_reg);
852 rpm_vreg_unlock(rpm_vreg);
853 return ERR_PTR(-ENOMEM);
854 }
855 priv_reg->rdev->reg_data = priv_reg;
856 priv_reg->rpm_vreg = rpm_vreg;
857 priv_reg->rdesc.name = framework_reg->rdesc.name;
David Collins4208ce32012-06-15 13:33:13 -0700858 priv_reg->rdesc.ops = framework_reg->rdesc.ops;
David Collinsc7642322012-04-04 10:19:12 -0700859 priv_reg->set_active = framework_reg->set_active;
860 priv_reg->set_sleep = framework_reg->set_sleep;
861 priv_reg->min_uV = framework_reg->min_uV;
862 priv_reg->max_uV = framework_reg->max_uV;
863 priv_reg->system_load = framework_reg->system_load;
864
865 might_sleep_if(!rpm_vreg->allow_atomic);
866 rpm_vreg_lock(rpm_vreg);
867 list_add(&priv_reg->list, &rpm_vreg->reg_list);
868 rpm_vreg_unlock(rpm_vreg);
869
870 return priv_reg;
871}
872EXPORT_SYMBOL_GPL(rpm_regulator_get);
873
874static int rpm_regulator_check_input(struct rpm_regulator *regulator)
875{
David Collins29ba0282012-06-18 10:01:52 -0700876 if (IS_ERR_OR_NULL(regulator) || regulator->rpm_vreg == NULL) {
David Collinsc7642322012-04-04 10:19:12 -0700877 pr_err("invalid rpm_regulator pointer\n");
878 return -EINVAL;
879 }
880
881 might_sleep_if(!regulator->rpm_vreg->allow_atomic);
882
883 return 0;
884}
885
886/**
887 * rpm_regulator_put() - free the RPM regulator handle
888 * @regulator: RPM regulator handle
889 *
890 * Parameter reaggregation does not take place when rpm_regulator_put is called.
891 * Therefore, regulator enable state and voltage must be configured
892 * appropriately before calling rpm_regulator_put.
893 *
894 * This function may be called from either atomic or nonatomic context. If this
895 * function is called from atomic context, then the regulator being operated on
896 * must be configured via device tree with qcom,allow-atomic == 1.
897 */
898void rpm_regulator_put(struct rpm_regulator *regulator)
899{
900 struct rpm_vreg *rpm_vreg;
901 int rc = rpm_regulator_check_input(regulator);
902
903 if (rc)
904 return;
905
906 rpm_vreg = regulator->rpm_vreg;
907
908 might_sleep_if(!rpm_vreg->allow_atomic);
909 rpm_vreg_lock(rpm_vreg);
910 list_del(&regulator->list);
911 rpm_vreg_unlock(rpm_vreg);
912
913 kfree(regulator->rdev);
914 kfree(regulator);
915}
916EXPORT_SYMBOL_GPL(rpm_regulator_put);
917
918/**
919 * rpm_regulator_enable() - enable regulator output
920 * @regulator: RPM regulator handle
921 *
922 * Returns 0 on success or errno on failure.
923 *
924 * This function may be called from either atomic or nonatomic context. If this
925 * function is called from atomic context, then the regulator being operated on
926 * must be configured via device tree with qcom,allow-atomic == 1.
927 */
928int rpm_regulator_enable(struct rpm_regulator *regulator)
929{
930 int rc = rpm_regulator_check_input(regulator);
931
932 if (rc)
933 return rc;
934
935 return rpm_vreg_enable(regulator->rdev);
936}
937EXPORT_SYMBOL_GPL(rpm_regulator_enable);
938
939/**
940 * rpm_regulator_disable() - disable regulator output
941 * @regulator: RPM regulator handle
942 *
943 * Returns 0 on success or errno on failure.
944 *
945 * The enable state of the regulator is determined by aggregating the requests
946 * of all consumers. Therefore, it is possible that the regulator will remain
947 * enabled even after rpm_regulator_disable is called.
948 *
949 * This function may be called from either atomic or nonatomic context. If this
950 * function is called from atomic context, then the regulator being operated on
951 * must be configured via device tree with qcom,allow-atomic == 1.
952 */
953int rpm_regulator_disable(struct rpm_regulator *regulator)
954{
955 int rc = rpm_regulator_check_input(regulator);
956
957 if (rc)
958 return rc;
959
960 return rpm_vreg_disable(regulator->rdev);
961}
962EXPORT_SYMBOL_GPL(rpm_regulator_disable);
963
964/**
965 * rpm_regulator_set_voltage() - set regulator output voltage
966 * @regulator: RPM regulator handle
967 * @min_uV: minimum required voltage in uV
968 * @max_uV: maximum acceptable voltage in uV
969 *
970 * Sets a voltage regulator to the desired output voltage. This can be set
971 * while the regulator is disabled or enabled. If the regulator is enabled then
972 * the voltage will change to the new value immediately; otherwise, if the
973 * regulator is disabled, then the regulator will output at the new voltage when
974 * enabled.
975 *
976 * The min_uV to max_uV voltage range requested must intersect with the
977 * voltage constraint range configured for the regulator.
978 *
979 * Returns 0 on success or errno on failure.
980 *
981 * The final voltage value that is sent to the RPM is aggregated based upon the
982 * values requested by all consumers of the regulator. This corresponds to the
983 * maximum min_uV value.
984 *
985 * This function may be called from either atomic or nonatomic context. If this
986 * function is called from atomic context, then the regulator being operated on
987 * must be configured via device tree with qcom,allow-atomic == 1.
988 */
989int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
990 int max_uV)
991{
992 int rc = rpm_regulator_check_input(regulator);
993 int uV = min_uV;
994
995 if (rc)
996 return rc;
997
998 if (regulator->rpm_vreg->regulator_type == RPM_REGULATOR_SMD_TYPE_VS) {
999 vreg_err(regulator, "unsupported regulator type: %d\n",
1000 regulator->rpm_vreg->regulator_type);
1001 return -EINVAL;
1002 }
1003
1004 if (min_uV > max_uV) {
1005 vreg_err(regulator, "min_uV=%d must be less than max_uV=%d\n",
1006 min_uV, max_uV);
1007 return -EINVAL;
1008 }
1009
1010 if (uV < regulator->min_uV && max_uV >= regulator->min_uV)
1011 uV = regulator->min_uV;
1012
1013 if (uV < regulator->min_uV || uV > regulator->max_uV) {
1014 vreg_err(regulator, "request v=[%d, %d] is outside allowed "
1015 "v=[%d, %d]\n", min_uV, max_uV, regulator->min_uV,
1016 regulator->max_uV);
1017 return -EINVAL;
1018 }
1019
David Collins4208ce32012-06-15 13:33:13 -07001020 return regulator->rdesc.ops->set_voltage(regulator->rdev, uV, uV, NULL);
David Collinsc7642322012-04-04 10:19:12 -07001021}
1022EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
1023
1024static struct regulator_ops ldo_ops = {
1025 .enable = rpm_vreg_enable,
1026 .disable = rpm_vreg_disable,
1027 .is_enabled = rpm_vreg_is_enabled,
1028 .set_voltage = rpm_vreg_set_voltage,
1029 .get_voltage = rpm_vreg_get_voltage,
1030 .list_voltage = rpm_vreg_list_voltage,
1031 .set_mode = rpm_vreg_set_mode,
1032 .get_mode = rpm_vreg_get_mode,
1033 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1034 .enable_time = rpm_vreg_enable_time,
1035};
1036
1037static struct regulator_ops smps_ops = {
1038 .enable = rpm_vreg_enable,
1039 .disable = rpm_vreg_disable,
1040 .is_enabled = rpm_vreg_is_enabled,
1041 .set_voltage = rpm_vreg_set_voltage,
1042 .get_voltage = rpm_vreg_get_voltage,
1043 .list_voltage = rpm_vreg_list_voltage,
1044 .set_mode = rpm_vreg_set_mode,
1045 .get_mode = rpm_vreg_get_mode,
1046 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1047 .enable_time = rpm_vreg_enable_time,
1048};
1049
David Collins4208ce32012-06-15 13:33:13 -07001050static struct regulator_ops smps_corner_ops = {
1051 .enable = rpm_vreg_enable,
1052 .disable = rpm_vreg_disable,
1053 .is_enabled = rpm_vreg_is_enabled,
1054 .set_voltage = rpm_vreg_set_voltage_corner,
1055 .get_voltage = rpm_vreg_get_voltage_corner,
1056 .list_voltage = rpm_vreg_list_voltage,
1057 .set_mode = rpm_vreg_set_mode,
1058 .get_mode = rpm_vreg_get_mode,
1059 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1060 .enable_time = rpm_vreg_enable_time,
1061};
1062
David Collinsc7642322012-04-04 10:19:12 -07001063static struct regulator_ops switch_ops = {
1064 .enable = rpm_vreg_enable,
1065 .disable = rpm_vreg_disable,
1066 .is_enabled = rpm_vreg_is_enabled,
1067 .enable_time = rpm_vreg_enable_time,
1068};
1069
1070static struct regulator_ops ncp_ops = {
1071 .enable = rpm_vreg_enable,
1072 .disable = rpm_vreg_disable,
1073 .is_enabled = rpm_vreg_is_enabled,
1074 .set_voltage = rpm_vreg_set_voltage,
1075 .get_voltage = rpm_vreg_get_voltage,
1076 .list_voltage = rpm_vreg_list_voltage,
1077 .enable_time = rpm_vreg_enable_time,
1078};
1079
1080static struct regulator_ops *vreg_ops[] = {
1081 [RPM_REGULATOR_SMD_TYPE_LDO] = &ldo_ops,
1082 [RPM_REGULATOR_SMD_TYPE_SMPS] = &smps_ops,
1083 [RPM_REGULATOR_SMD_TYPE_VS] = &switch_ops,
1084 [RPM_REGULATOR_SMD_TYPE_NCP] = &ncp_ops,
1085};
1086
1087static int __devexit rpm_vreg_device_remove(struct platform_device *pdev)
1088{
1089 struct device *dev = &pdev->dev;
1090 struct rpm_regulator *reg;
1091
1092 reg = platform_get_drvdata(pdev);
1093 if (reg) {
1094 rpm_vreg_lock(reg->rpm_vreg);
1095 regulator_unregister(reg->rdev);
1096 list_del(&reg->list);
1097 kfree(reg);
1098 rpm_vreg_unlock(reg->rpm_vreg);
1099 } else {
1100 dev_err(dev, "%s: drvdata missing\n", __func__);
1101 return -EINVAL;
1102 }
1103
1104 platform_set_drvdata(pdev, NULL);
1105
1106 return 0;
1107}
1108
1109static int __devexit rpm_vreg_resource_remove(struct platform_device *pdev)
1110{
1111 struct device *dev = &pdev->dev;
1112 struct rpm_regulator *reg, *reg_temp;
1113 struct rpm_vreg *rpm_vreg;
1114
1115 rpm_vreg = platform_get_drvdata(pdev);
1116 if (rpm_vreg) {
1117 rpm_vreg_lock(rpm_vreg);
1118 list_for_each_entry_safe(reg, reg_temp, &rpm_vreg->reg_list,
1119 list) {
1120 /* Only touch data for private consumers. */
1121 if (reg->rdev->desc == NULL) {
1122 list_del(&reg->list);
1123 kfree(reg->rdev);
1124 kfree(reg);
1125 } else {
1126 dev_err(dev, "%s: not all child devices have "
1127 "been removed\n", __func__);
1128 }
1129 }
1130 rpm_vreg_unlock(rpm_vreg);
1131
1132 msm_rpm_free_request(rpm_vreg->handle_active);
1133 msm_rpm_free_request(rpm_vreg->handle_sleep);
1134
1135 kfree(rpm_vreg);
1136 } else {
1137 dev_err(dev, "%s: drvdata missing\n", __func__);
1138 return -EINVAL;
1139 }
1140
1141 platform_set_drvdata(pdev, NULL);
1142
1143 return 0;
1144}
1145
1146/*
1147 * This probe is called for child rpm-regulator devices which have
1148 * properties which are required to configure individual regulator
1149 * framework regulators for a given RPM regulator resource.
1150 */
1151static int __devinit rpm_vreg_device_probe(struct platform_device *pdev)
1152{
1153 struct device *dev = &pdev->dev;
1154 struct device_node *node = dev->of_node;
1155 struct regulator_init_data *init_data;
1156 struct rpm_vreg *rpm_vreg;
1157 struct rpm_regulator *reg;
1158 int rc = 0;
1159 int i, regulator_type;
1160 u32 val;
1161
1162 if (!dev->of_node) {
1163 dev_err(dev, "%s: device tree information missing\n", __func__);
1164 return -ENODEV;
1165 }
1166
1167 if (pdev->dev.parent == NULL) {
1168 dev_err(dev, "%s: parent device missing\n", __func__);
1169 return -ENODEV;
1170 }
1171
1172 rpm_vreg = dev_get_drvdata(pdev->dev.parent);
1173 if (rpm_vreg == NULL) {
1174 dev_err(dev, "%s: rpm_vreg not found in parent device\n",
1175 __func__);
1176 return -ENODEV;
1177 }
1178
1179 reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
1180 if (reg == NULL) {
1181 dev_err(dev, "%s: could not allocate memory for reg\n",
1182 __func__);
1183 return -ENOMEM;
1184 }
1185
1186 regulator_type = rpm_vreg->regulator_type;
1187 reg->rpm_vreg = rpm_vreg;
1188 reg->rdesc.ops = vreg_ops[regulator_type];
1189 reg->rdesc.owner = THIS_MODULE;
1190 reg->rdesc.type = REGULATOR_VOLTAGE;
1191
David Collins4208ce32012-06-15 13:33:13 -07001192 /*
1193 * Switch to voltage corner regulator ops if qcom,use-voltage-corner
1194 * is specified in the device node (SMPS only).
1195 */
1196 if (of_find_property(node, "qcom,use-voltage-corner", NULL)
1197 && regulator_type == RPM_REGULATOR_SMD_TYPE_SMPS)
1198 reg->rdesc.ops = &smps_corner_ops;
1199
David Collinsc7642322012-04-04 10:19:12 -07001200 if (regulator_type == RPM_REGULATOR_SMD_TYPE_VS)
1201 reg->rdesc.n_voltages = 0;
1202 else
1203 reg->rdesc.n_voltages = 2;
1204
1205 rc = of_property_read_u32(node, "qcom,set", &val);
1206 if (rc) {
1207 dev_err(dev, "%s: sleep set and/or active set must be "
1208 "configured via qcom,set property, rc=%d\n", __func__,
1209 rc);
1210 goto fail_free_reg;
1211 } else if (!(val & RPM_SET_CONFIG_BOTH)) {
1212 dev_err(dev, "%s: qcom,set=%u property is invalid\n", __func__,
1213 val);
1214 rc = -EINVAL;
1215 goto fail_free_reg;
1216 }
1217
1218 reg->set_active = !!(val & RPM_SET_CONFIG_ACTIVE);
1219 reg->set_sleep = !!(val & RPM_SET_CONFIG_SLEEP);
1220
David Collins4853ae42012-06-12 09:37:19 -07001221 init_data = of_get_regulator_init_data(dev, node);
David Collinsc7642322012-04-04 10:19:12 -07001222 if (init_data == NULL) {
1223 dev_err(dev, "%s: unable to allocate memory\n", __func__);
1224 rc = -ENOMEM;
1225 goto fail_free_reg;
1226 }
1227 if (init_data->constraints.name == NULL) {
1228 dev_err(dev, "%s: regulator name not specified\n", __func__);
1229 rc = -EINVAL;
1230 goto fail_free_reg;
1231 }
1232
1233 init_data->constraints.input_uV = init_data->constraints.max_uV;
1234
1235 if (of_get_property(node, "parent-supply", NULL))
1236 init_data->supply_regulator = "parent";
1237
1238 /*
1239 * Fill in ops and mode masks based on callbacks specified for
1240 * this type of regulator.
1241 */
1242 if (reg->rdesc.ops->enable)
1243 init_data->constraints.valid_ops_mask
1244 |= REGULATOR_CHANGE_STATUS;
1245 if (reg->rdesc.ops->get_voltage)
1246 init_data->constraints.valid_ops_mask
1247 |= REGULATOR_CHANGE_VOLTAGE;
1248 if (reg->rdesc.ops->get_mode) {
1249 init_data->constraints.valid_ops_mask
1250 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1251 init_data->constraints.valid_modes_mask
1252 |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1253 }
1254
1255 reg->rdesc.name = init_data->constraints.name;
1256 reg->min_uV = init_data->constraints.min_uV;
1257 reg->max_uV = init_data->constraints.max_uV;
1258
1259 /* Initialize the param array based on optional properties. */
1260 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
1261 rc = of_property_read_u32(node, params[i].property_name, &val);
1262 if (rc == 0) {
1263 if (params[i].supported_regulator_types
1264 & BIT(regulator_type)) {
1265 if (val < params[i].min
1266 || val > params[i].max) {
1267 pr_warn("%s: device tree property: "
1268 "%s=%u is outsided allowed "
1269 "range [%u, %u]\n",
1270 reg->rdesc.name,
1271 params[i].property_name, val,
1272 params[i].min, params[i].max);
1273 continue;
1274 }
1275 reg->req.param[i] = val;
1276 reg->req.modified |= BIT(i);
1277 } else {
1278 pr_warn("%s: regulator type=%d does not support"
1279 " device tree property: %s\n",
1280 reg->rdesc.name, regulator_type,
1281 params[i].property_name);
1282 }
1283 }
1284 }
1285
1286 of_property_read_u32(node, "qcom,system_load", &reg->system_load);
1287
1288 rpm_vreg_lock(rpm_vreg);
1289 list_add(&reg->list, &rpm_vreg->reg_list);
1290 rpm_vreg_unlock(rpm_vreg);
1291
1292 reg->rdev = regulator_register(&reg->rdesc, dev, init_data, reg, node);
1293 if (IS_ERR(reg->rdev)) {
1294 rc = PTR_ERR(reg->rdev);
1295 reg->rdev = NULL;
1296 pr_err("regulator_register failed: %s, rc=%d\n",
1297 reg->rdesc.name, rc);
1298 goto fail_remove_from_list;
1299 }
1300
1301 platform_set_drvdata(pdev, reg);
1302
1303 pr_debug("successfully probed: %s\n", reg->rdesc.name);
1304
1305 return 0;
1306
1307fail_remove_from_list:
1308 rpm_vreg_lock(rpm_vreg);
1309 list_del(&reg->list);
1310 rpm_vreg_unlock(rpm_vreg);
1311
1312fail_free_reg:
1313 kfree(reg);
1314 return rc;
1315}
1316
1317/*
1318 * This probe is called for parent rpm-regulator devices which have
1319 * properties which are required to identify a given RPM resource.
1320 */
1321static int __devinit rpm_vreg_resource_probe(struct platform_device *pdev)
1322{
1323 struct device *dev = &pdev->dev;
1324 struct device_node *node = dev->of_node;
1325 struct rpm_vreg *rpm_vreg;
1326 int val = 0;
1327 u32 resource_type;
1328 int rc;
1329
1330 if (!dev->of_node) {
1331 dev_err(dev, "%s: device tree information missing\n", __func__);
1332 return -ENODEV;
1333 }
1334
1335 /* Create new rpm_vreg entry. */
1336 rpm_vreg = kzalloc(sizeof(struct rpm_vreg), GFP_KERNEL);
1337 if (rpm_vreg == NULL) {
1338 dev_err(dev, "%s: could not allocate memory for vreg\n",
1339 __func__);
1340 return -ENOMEM;
1341 }
1342
1343 /* Required device tree properties: */
1344 rc = of_property_read_string(node, "qcom,resource-name",
1345 &rpm_vreg->resource_name);
1346 if (rc) {
1347 dev_err(dev, "%s: qcom,resource-name missing in DT node\n",
1348 __func__);
1349 goto fail_free_vreg;
1350 }
1351 resource_type = rpm_vreg_string_to_int(rpm_vreg->resource_name);
1352
1353 rc = of_property_read_u32(node, "qcom,resource-id",
1354 &rpm_vreg->resource_id);
1355 if (rc) {
1356 dev_err(dev, "%s: qcom,resource-id missing in DT node\n",
1357 __func__);
1358 goto fail_free_vreg;
1359 }
1360
1361 rc = of_property_read_u32(node, "qcom,regulator-type",
1362 &rpm_vreg->regulator_type);
1363 if (rc) {
1364 dev_err(dev, "%s: qcom,regulator-type missing in DT node\n",
1365 __func__);
1366 goto fail_free_vreg;
1367 }
1368
1369 if ((rpm_vreg->regulator_type < 0)
1370 || (rpm_vreg->regulator_type >= RPM_REGULATOR_SMD_TYPE_MAX)) {
1371 dev_err(dev, "%s: invalid regulator type: %d\n", __func__,
1372 rpm_vreg->regulator_type);
1373 rc = -EINVAL;
1374 goto fail_free_vreg;
1375 }
1376
1377 /* Optional device tree properties: */
1378 of_property_read_u32(node, "qcom,allow-atomic", &val);
1379 rpm_vreg->allow_atomic = !!val;
1380 of_property_read_u32(node, "qcom,enable-time", &rpm_vreg->enable_time);
1381 of_property_read_u32(node, "qcom,hpm-min-load",
1382 &rpm_vreg->hpm_min_load);
1383
1384 rpm_vreg->handle_active = msm_rpm_create_request(RPM_SET_ACTIVE,
1385 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1386 if (rpm_vreg->handle_active == NULL
1387 || IS_ERR(rpm_vreg->handle_active)) {
1388 rc = PTR_ERR(rpm_vreg->handle_active);
1389 dev_err(dev, "%s: failed to create active RPM handle, rc=%d\n",
1390 __func__, rc);
1391 goto fail_free_vreg;
1392 }
1393
1394 rpm_vreg->handle_sleep = msm_rpm_create_request(RPM_SET_SLEEP,
1395 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1396 if (rpm_vreg->handle_sleep == NULL || IS_ERR(rpm_vreg->handle_sleep)) {
1397 rc = PTR_ERR(rpm_vreg->handle_sleep);
1398 dev_err(dev, "%s: failed to create sleep RPM handle, rc=%d\n",
1399 __func__, rc);
1400 goto fail_free_handle_active;
1401 }
1402
1403 INIT_LIST_HEAD(&rpm_vreg->reg_list);
1404
1405 if (rpm_vreg->allow_atomic)
1406 spin_lock_init(&rpm_vreg->slock);
1407 else
1408 mutex_init(&rpm_vreg->mlock);
1409
1410 platform_set_drvdata(pdev, rpm_vreg);
1411
1412 rc = of_platform_populate(node, NULL, NULL, dev);
1413 if (rc) {
1414 dev_err(dev, "%s: failed to add child nodes, rc=%d\n", __func__,
1415 rc);
1416 goto fail_unset_drvdata;
1417 }
1418
1419 pr_debug("successfully probed: %s (%08X) %u\n", rpm_vreg->resource_name,
1420 resource_type, rpm_vreg->resource_id);
1421
1422 return rc;
1423
1424fail_unset_drvdata:
1425 platform_set_drvdata(pdev, NULL);
1426 msm_rpm_free_request(rpm_vreg->handle_sleep);
1427
1428fail_free_handle_active:
1429 msm_rpm_free_request(rpm_vreg->handle_active);
1430
1431fail_free_vreg:
1432 kfree(rpm_vreg);
1433
1434 return rc;
1435}
1436
1437static struct of_device_id rpm_vreg_match_table_device[] = {
1438 { .compatible = "qcom,rpm-regulator-smd", },
1439 {}
1440};
1441
1442static struct of_device_id rpm_vreg_match_table_resource[] = {
1443 { .compatible = "qcom,rpm-regulator-smd-resource", },
1444 {}
1445};
1446
1447static struct platform_driver rpm_vreg_device_driver = {
1448 .probe = rpm_vreg_device_probe,
1449 .remove = __devexit_p(rpm_vreg_device_remove),
1450 .driver = {
1451 .name = "qcom,rpm-regulator-smd",
1452 .owner = THIS_MODULE,
1453 .of_match_table = rpm_vreg_match_table_device,
1454 },
1455};
1456
1457static struct platform_driver rpm_vreg_resource_driver = {
1458 .probe = rpm_vreg_resource_probe,
1459 .remove = __devexit_p(rpm_vreg_resource_remove),
1460 .driver = {
1461 .name = "qcom,rpm-regulator-smd-resource",
1462 .owner = THIS_MODULE,
1463 .of_match_table = rpm_vreg_match_table_resource,
1464 },
1465};
1466
1467/**
1468 * rpm_regulator_smd_driver_init() - initialized SMD RPM regulator driver
1469 *
1470 * This function registers the SMD RPM regulator platform drivers.
1471 *
1472 * Returns 0 on success or errno on failure.
1473 */
1474int __init rpm_regulator_smd_driver_init(void)
1475{
1476 static bool initialized;
1477 int i, rc;
1478
1479 if (initialized)
1480 return 0;
1481 else
1482 initialized = true;
1483
1484 /* Store parameter string names as integers */
1485 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++)
1486 params[i].key = rpm_vreg_string_to_int(params[i].name);
1487
1488 rc = platform_driver_register(&rpm_vreg_device_driver);
1489 if (rc)
1490 return rc;
1491
1492 return platform_driver_register(&rpm_vreg_resource_driver);
1493}
1494EXPORT_SYMBOL_GPL(rpm_regulator_smd_driver_init);
1495
1496static void __exit rpm_vreg_exit(void)
1497{
1498 platform_driver_unregister(&rpm_vreg_device_driver);
1499 platform_driver_unregister(&rpm_vreg_resource_driver);
1500}
1501
1502module_init(rpm_regulator_smd_driver_init);
1503module_exit(rpm_vreg_exit);
1504
1505MODULE_LICENSE("GPL v2");
1506MODULE_DESCRIPTION("MSM SMD RPM regulator driver");