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