blob: 152b6e5f3ae3fabc209e187219b2713fd1d60ea2 [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);
David Collins29ba0282012-06-18 10:01:52 -0700765 if (IS_ERR(regulator)) {
766 pr_err("could not find regulator for: dev=%s, supply=%s, rc=%ld\n",
767 (dev ? dev_name(dev) : ""), (supply ? supply : ""),
768 PTR_ERR(regulator));
769 return ERR_CAST(regulator);
David Collinsc7642322012-04-04 10:19:12 -0700770 }
771
772 framework_reg = regulator_get_drvdata(regulator);
773 if (framework_reg == NULL) {
774 pr_err("regulator structure not found.\n");
775 regulator_put(regulator);
776 return ERR_PTR(-ENODEV);
777 }
778 regulator_put(regulator);
779
780 rpm_vreg = framework_reg->rpm_vreg;
781
782 priv_reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
783 if (priv_reg == NULL) {
784 vreg_err(framework_reg, "could not allocate memory for "
785 "regulator\n");
786 rpm_vreg_unlock(rpm_vreg);
787 return ERR_PTR(-ENOMEM);
788 }
789
790 /*
791 * Allocate a regulator_dev struct so that framework callback functions
792 * can be called from the private API functions.
793 */
794 priv_reg->rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
795 if (priv_reg->rdev == NULL) {
796 vreg_err(framework_reg, "could not allocate memory for "
797 "regulator_dev\n");
798 kfree(priv_reg);
799 rpm_vreg_unlock(rpm_vreg);
800 return ERR_PTR(-ENOMEM);
801 }
802 priv_reg->rdev->reg_data = priv_reg;
803 priv_reg->rpm_vreg = rpm_vreg;
804 priv_reg->rdesc.name = framework_reg->rdesc.name;
805 priv_reg->set_active = framework_reg->set_active;
806 priv_reg->set_sleep = framework_reg->set_sleep;
807 priv_reg->min_uV = framework_reg->min_uV;
808 priv_reg->max_uV = framework_reg->max_uV;
809 priv_reg->system_load = framework_reg->system_load;
810
811 might_sleep_if(!rpm_vreg->allow_atomic);
812 rpm_vreg_lock(rpm_vreg);
813 list_add(&priv_reg->list, &rpm_vreg->reg_list);
814 rpm_vreg_unlock(rpm_vreg);
815
816 return priv_reg;
817}
818EXPORT_SYMBOL_GPL(rpm_regulator_get);
819
820static int rpm_regulator_check_input(struct rpm_regulator *regulator)
821{
David Collins29ba0282012-06-18 10:01:52 -0700822 if (IS_ERR_OR_NULL(regulator) || regulator->rpm_vreg == NULL) {
David Collinsc7642322012-04-04 10:19:12 -0700823 pr_err("invalid rpm_regulator pointer\n");
824 return -EINVAL;
825 }
826
827 might_sleep_if(!regulator->rpm_vreg->allow_atomic);
828
829 return 0;
830}
831
832/**
833 * rpm_regulator_put() - free the RPM regulator handle
834 * @regulator: RPM regulator handle
835 *
836 * Parameter reaggregation does not take place when rpm_regulator_put is called.
837 * Therefore, regulator enable state and voltage must be configured
838 * appropriately before calling rpm_regulator_put.
839 *
840 * This function may be called from either atomic or nonatomic context. If this
841 * function is called from atomic context, then the regulator being operated on
842 * must be configured via device tree with qcom,allow-atomic == 1.
843 */
844void rpm_regulator_put(struct rpm_regulator *regulator)
845{
846 struct rpm_vreg *rpm_vreg;
847 int rc = rpm_regulator_check_input(regulator);
848
849 if (rc)
850 return;
851
852 rpm_vreg = regulator->rpm_vreg;
853
854 might_sleep_if(!rpm_vreg->allow_atomic);
855 rpm_vreg_lock(rpm_vreg);
856 list_del(&regulator->list);
857 rpm_vreg_unlock(rpm_vreg);
858
859 kfree(regulator->rdev);
860 kfree(regulator);
861}
862EXPORT_SYMBOL_GPL(rpm_regulator_put);
863
864/**
865 * rpm_regulator_enable() - enable regulator output
866 * @regulator: RPM regulator handle
867 *
868 * Returns 0 on success or errno on failure.
869 *
870 * This function may be called from either atomic or nonatomic context. If this
871 * function is called from atomic context, then the regulator being operated on
872 * must be configured via device tree with qcom,allow-atomic == 1.
873 */
874int rpm_regulator_enable(struct rpm_regulator *regulator)
875{
876 int rc = rpm_regulator_check_input(regulator);
877
878 if (rc)
879 return rc;
880
881 return rpm_vreg_enable(regulator->rdev);
882}
883EXPORT_SYMBOL_GPL(rpm_regulator_enable);
884
885/**
886 * rpm_regulator_disable() - disable regulator output
887 * @regulator: RPM regulator handle
888 *
889 * Returns 0 on success or errno on failure.
890 *
891 * The enable state of the regulator is determined by aggregating the requests
892 * of all consumers. Therefore, it is possible that the regulator will remain
893 * enabled even after rpm_regulator_disable is called.
894 *
895 * This function may be called from either atomic or nonatomic context. If this
896 * function is called from atomic context, then the regulator being operated on
897 * must be configured via device tree with qcom,allow-atomic == 1.
898 */
899int rpm_regulator_disable(struct rpm_regulator *regulator)
900{
901 int rc = rpm_regulator_check_input(regulator);
902
903 if (rc)
904 return rc;
905
906 return rpm_vreg_disable(regulator->rdev);
907}
908EXPORT_SYMBOL_GPL(rpm_regulator_disable);
909
910/**
911 * rpm_regulator_set_voltage() - set regulator output voltage
912 * @regulator: RPM regulator handle
913 * @min_uV: minimum required voltage in uV
914 * @max_uV: maximum acceptable voltage in uV
915 *
916 * Sets a voltage regulator to the desired output voltage. This can be set
917 * while the regulator is disabled or enabled. If the regulator is enabled then
918 * the voltage will change to the new value immediately; otherwise, if the
919 * regulator is disabled, then the regulator will output at the new voltage when
920 * enabled.
921 *
922 * The min_uV to max_uV voltage range requested must intersect with the
923 * voltage constraint range configured for the regulator.
924 *
925 * Returns 0 on success or errno on failure.
926 *
927 * The final voltage value that is sent to the RPM is aggregated based upon the
928 * values requested by all consumers of the regulator. This corresponds to the
929 * maximum min_uV value.
930 *
931 * This function may be called from either atomic or nonatomic context. If this
932 * function is called from atomic context, then the regulator being operated on
933 * must be configured via device tree with qcom,allow-atomic == 1.
934 */
935int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
936 int max_uV)
937{
938 int rc = rpm_regulator_check_input(regulator);
939 int uV = min_uV;
940
941 if (rc)
942 return rc;
943
944 if (regulator->rpm_vreg->regulator_type == RPM_REGULATOR_SMD_TYPE_VS) {
945 vreg_err(regulator, "unsupported regulator type: %d\n",
946 regulator->rpm_vreg->regulator_type);
947 return -EINVAL;
948 }
949
950 if (min_uV > max_uV) {
951 vreg_err(regulator, "min_uV=%d must be less than max_uV=%d\n",
952 min_uV, max_uV);
953 return -EINVAL;
954 }
955
956 if (uV < regulator->min_uV && max_uV >= regulator->min_uV)
957 uV = regulator->min_uV;
958
959 if (uV < regulator->min_uV || uV > regulator->max_uV) {
960 vreg_err(regulator, "request v=[%d, %d] is outside allowed "
961 "v=[%d, %d]\n", min_uV, max_uV, regulator->min_uV,
962 regulator->max_uV);
963 return -EINVAL;
964 }
965
966 return rpm_vreg_set_voltage(regulator->rdev, uV, uV, NULL);
967}
968EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
969
970static struct regulator_ops ldo_ops = {
971 .enable = rpm_vreg_enable,
972 .disable = rpm_vreg_disable,
973 .is_enabled = rpm_vreg_is_enabled,
974 .set_voltage = rpm_vreg_set_voltage,
975 .get_voltage = rpm_vreg_get_voltage,
976 .list_voltage = rpm_vreg_list_voltage,
977 .set_mode = rpm_vreg_set_mode,
978 .get_mode = rpm_vreg_get_mode,
979 .get_optimum_mode = rpm_vreg_get_optimum_mode,
980 .enable_time = rpm_vreg_enable_time,
981};
982
983static struct regulator_ops smps_ops = {
984 .enable = rpm_vreg_enable,
985 .disable = rpm_vreg_disable,
986 .is_enabled = rpm_vreg_is_enabled,
987 .set_voltage = rpm_vreg_set_voltage,
988 .get_voltage = rpm_vreg_get_voltage,
989 .list_voltage = rpm_vreg_list_voltage,
990 .set_mode = rpm_vreg_set_mode,
991 .get_mode = rpm_vreg_get_mode,
992 .get_optimum_mode = rpm_vreg_get_optimum_mode,
993 .enable_time = rpm_vreg_enable_time,
994};
995
996static struct regulator_ops switch_ops = {
997 .enable = rpm_vreg_enable,
998 .disable = rpm_vreg_disable,
999 .is_enabled = rpm_vreg_is_enabled,
1000 .enable_time = rpm_vreg_enable_time,
1001};
1002
1003static struct regulator_ops ncp_ops = {
1004 .enable = rpm_vreg_enable,
1005 .disable = rpm_vreg_disable,
1006 .is_enabled = rpm_vreg_is_enabled,
1007 .set_voltage = rpm_vreg_set_voltage,
1008 .get_voltage = rpm_vreg_get_voltage,
1009 .list_voltage = rpm_vreg_list_voltage,
1010 .enable_time = rpm_vreg_enable_time,
1011};
1012
1013static struct regulator_ops *vreg_ops[] = {
1014 [RPM_REGULATOR_SMD_TYPE_LDO] = &ldo_ops,
1015 [RPM_REGULATOR_SMD_TYPE_SMPS] = &smps_ops,
1016 [RPM_REGULATOR_SMD_TYPE_VS] = &switch_ops,
1017 [RPM_REGULATOR_SMD_TYPE_NCP] = &ncp_ops,
1018};
1019
1020static int __devexit rpm_vreg_device_remove(struct platform_device *pdev)
1021{
1022 struct device *dev = &pdev->dev;
1023 struct rpm_regulator *reg;
1024
1025 reg = platform_get_drvdata(pdev);
1026 if (reg) {
1027 rpm_vreg_lock(reg->rpm_vreg);
1028 regulator_unregister(reg->rdev);
1029 list_del(&reg->list);
1030 kfree(reg);
1031 rpm_vreg_unlock(reg->rpm_vreg);
1032 } else {
1033 dev_err(dev, "%s: drvdata missing\n", __func__);
1034 return -EINVAL;
1035 }
1036
1037 platform_set_drvdata(pdev, NULL);
1038
1039 return 0;
1040}
1041
1042static int __devexit rpm_vreg_resource_remove(struct platform_device *pdev)
1043{
1044 struct device *dev = &pdev->dev;
1045 struct rpm_regulator *reg, *reg_temp;
1046 struct rpm_vreg *rpm_vreg;
1047
1048 rpm_vreg = platform_get_drvdata(pdev);
1049 if (rpm_vreg) {
1050 rpm_vreg_lock(rpm_vreg);
1051 list_for_each_entry_safe(reg, reg_temp, &rpm_vreg->reg_list,
1052 list) {
1053 /* Only touch data for private consumers. */
1054 if (reg->rdev->desc == NULL) {
1055 list_del(&reg->list);
1056 kfree(reg->rdev);
1057 kfree(reg);
1058 } else {
1059 dev_err(dev, "%s: not all child devices have "
1060 "been removed\n", __func__);
1061 }
1062 }
1063 rpm_vreg_unlock(rpm_vreg);
1064
1065 msm_rpm_free_request(rpm_vreg->handle_active);
1066 msm_rpm_free_request(rpm_vreg->handle_sleep);
1067
1068 kfree(rpm_vreg);
1069 } else {
1070 dev_err(dev, "%s: drvdata missing\n", __func__);
1071 return -EINVAL;
1072 }
1073
1074 platform_set_drvdata(pdev, NULL);
1075
1076 return 0;
1077}
1078
1079/*
1080 * This probe is called for child rpm-regulator devices which have
1081 * properties which are required to configure individual regulator
1082 * framework regulators for a given RPM regulator resource.
1083 */
1084static int __devinit rpm_vreg_device_probe(struct platform_device *pdev)
1085{
1086 struct device *dev = &pdev->dev;
1087 struct device_node *node = dev->of_node;
1088 struct regulator_init_data *init_data;
1089 struct rpm_vreg *rpm_vreg;
1090 struct rpm_regulator *reg;
1091 int rc = 0;
1092 int i, regulator_type;
1093 u32 val;
1094
1095 if (!dev->of_node) {
1096 dev_err(dev, "%s: device tree information missing\n", __func__);
1097 return -ENODEV;
1098 }
1099
1100 if (pdev->dev.parent == NULL) {
1101 dev_err(dev, "%s: parent device missing\n", __func__);
1102 return -ENODEV;
1103 }
1104
1105 rpm_vreg = dev_get_drvdata(pdev->dev.parent);
1106 if (rpm_vreg == NULL) {
1107 dev_err(dev, "%s: rpm_vreg not found in parent device\n",
1108 __func__);
1109 return -ENODEV;
1110 }
1111
1112 reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
1113 if (reg == NULL) {
1114 dev_err(dev, "%s: could not allocate memory for reg\n",
1115 __func__);
1116 return -ENOMEM;
1117 }
1118
1119 regulator_type = rpm_vreg->regulator_type;
1120 reg->rpm_vreg = rpm_vreg;
1121 reg->rdesc.ops = vreg_ops[regulator_type];
1122 reg->rdesc.owner = THIS_MODULE;
1123 reg->rdesc.type = REGULATOR_VOLTAGE;
1124
1125 if (regulator_type == RPM_REGULATOR_SMD_TYPE_VS)
1126 reg->rdesc.n_voltages = 0;
1127 else
1128 reg->rdesc.n_voltages = 2;
1129
1130 rc = of_property_read_u32(node, "qcom,set", &val);
1131 if (rc) {
1132 dev_err(dev, "%s: sleep set and/or active set must be "
1133 "configured via qcom,set property, rc=%d\n", __func__,
1134 rc);
1135 goto fail_free_reg;
1136 } else if (!(val & RPM_SET_CONFIG_BOTH)) {
1137 dev_err(dev, "%s: qcom,set=%u property is invalid\n", __func__,
1138 val);
1139 rc = -EINVAL;
1140 goto fail_free_reg;
1141 }
1142
1143 reg->set_active = !!(val & RPM_SET_CONFIG_ACTIVE);
1144 reg->set_sleep = !!(val & RPM_SET_CONFIG_SLEEP);
1145
David Collins4853ae42012-06-12 09:37:19 -07001146 init_data = of_get_regulator_init_data(dev, node);
David Collinsc7642322012-04-04 10:19:12 -07001147 if (init_data == NULL) {
1148 dev_err(dev, "%s: unable to allocate memory\n", __func__);
1149 rc = -ENOMEM;
1150 goto fail_free_reg;
1151 }
1152 if (init_data->constraints.name == NULL) {
1153 dev_err(dev, "%s: regulator name not specified\n", __func__);
1154 rc = -EINVAL;
1155 goto fail_free_reg;
1156 }
1157
1158 init_data->constraints.input_uV = init_data->constraints.max_uV;
1159
1160 if (of_get_property(node, "parent-supply", NULL))
1161 init_data->supply_regulator = "parent";
1162
1163 /*
1164 * Fill in ops and mode masks based on callbacks specified for
1165 * this type of regulator.
1166 */
1167 if (reg->rdesc.ops->enable)
1168 init_data->constraints.valid_ops_mask
1169 |= REGULATOR_CHANGE_STATUS;
1170 if (reg->rdesc.ops->get_voltage)
1171 init_data->constraints.valid_ops_mask
1172 |= REGULATOR_CHANGE_VOLTAGE;
1173 if (reg->rdesc.ops->get_mode) {
1174 init_data->constraints.valid_ops_mask
1175 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1176 init_data->constraints.valid_modes_mask
1177 |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1178 }
1179
1180 reg->rdesc.name = init_data->constraints.name;
1181 reg->min_uV = init_data->constraints.min_uV;
1182 reg->max_uV = init_data->constraints.max_uV;
1183
1184 /* Initialize the param array based on optional properties. */
1185 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
1186 rc = of_property_read_u32(node, params[i].property_name, &val);
1187 if (rc == 0) {
1188 if (params[i].supported_regulator_types
1189 & BIT(regulator_type)) {
1190 if (val < params[i].min
1191 || val > params[i].max) {
1192 pr_warn("%s: device tree property: "
1193 "%s=%u is outsided allowed "
1194 "range [%u, %u]\n",
1195 reg->rdesc.name,
1196 params[i].property_name, val,
1197 params[i].min, params[i].max);
1198 continue;
1199 }
1200 reg->req.param[i] = val;
1201 reg->req.modified |= BIT(i);
1202 } else {
1203 pr_warn("%s: regulator type=%d does not support"
1204 " device tree property: %s\n",
1205 reg->rdesc.name, regulator_type,
1206 params[i].property_name);
1207 }
1208 }
1209 }
1210
1211 of_property_read_u32(node, "qcom,system_load", &reg->system_load);
1212
1213 rpm_vreg_lock(rpm_vreg);
1214 list_add(&reg->list, &rpm_vreg->reg_list);
1215 rpm_vreg_unlock(rpm_vreg);
1216
1217 reg->rdev = regulator_register(&reg->rdesc, dev, init_data, reg, node);
1218 if (IS_ERR(reg->rdev)) {
1219 rc = PTR_ERR(reg->rdev);
1220 reg->rdev = NULL;
1221 pr_err("regulator_register failed: %s, rc=%d\n",
1222 reg->rdesc.name, rc);
1223 goto fail_remove_from_list;
1224 }
1225
1226 platform_set_drvdata(pdev, reg);
1227
1228 pr_debug("successfully probed: %s\n", reg->rdesc.name);
1229
1230 return 0;
1231
1232fail_remove_from_list:
1233 rpm_vreg_lock(rpm_vreg);
1234 list_del(&reg->list);
1235 rpm_vreg_unlock(rpm_vreg);
1236
1237fail_free_reg:
1238 kfree(reg);
1239 return rc;
1240}
1241
1242/*
1243 * This probe is called for parent rpm-regulator devices which have
1244 * properties which are required to identify a given RPM resource.
1245 */
1246static int __devinit rpm_vreg_resource_probe(struct platform_device *pdev)
1247{
1248 struct device *dev = &pdev->dev;
1249 struct device_node *node = dev->of_node;
1250 struct rpm_vreg *rpm_vreg;
1251 int val = 0;
1252 u32 resource_type;
1253 int rc;
1254
1255 if (!dev->of_node) {
1256 dev_err(dev, "%s: device tree information missing\n", __func__);
1257 return -ENODEV;
1258 }
1259
1260 /* Create new rpm_vreg entry. */
1261 rpm_vreg = kzalloc(sizeof(struct rpm_vreg), GFP_KERNEL);
1262 if (rpm_vreg == NULL) {
1263 dev_err(dev, "%s: could not allocate memory for vreg\n",
1264 __func__);
1265 return -ENOMEM;
1266 }
1267
1268 /* Required device tree properties: */
1269 rc = of_property_read_string(node, "qcom,resource-name",
1270 &rpm_vreg->resource_name);
1271 if (rc) {
1272 dev_err(dev, "%s: qcom,resource-name missing in DT node\n",
1273 __func__);
1274 goto fail_free_vreg;
1275 }
1276 resource_type = rpm_vreg_string_to_int(rpm_vreg->resource_name);
1277
1278 rc = of_property_read_u32(node, "qcom,resource-id",
1279 &rpm_vreg->resource_id);
1280 if (rc) {
1281 dev_err(dev, "%s: qcom,resource-id missing in DT node\n",
1282 __func__);
1283 goto fail_free_vreg;
1284 }
1285
1286 rc = of_property_read_u32(node, "qcom,regulator-type",
1287 &rpm_vreg->regulator_type);
1288 if (rc) {
1289 dev_err(dev, "%s: qcom,regulator-type missing in DT node\n",
1290 __func__);
1291 goto fail_free_vreg;
1292 }
1293
1294 if ((rpm_vreg->regulator_type < 0)
1295 || (rpm_vreg->regulator_type >= RPM_REGULATOR_SMD_TYPE_MAX)) {
1296 dev_err(dev, "%s: invalid regulator type: %d\n", __func__,
1297 rpm_vreg->regulator_type);
1298 rc = -EINVAL;
1299 goto fail_free_vreg;
1300 }
1301
1302 /* Optional device tree properties: */
1303 of_property_read_u32(node, "qcom,allow-atomic", &val);
1304 rpm_vreg->allow_atomic = !!val;
1305 of_property_read_u32(node, "qcom,enable-time", &rpm_vreg->enable_time);
1306 of_property_read_u32(node, "qcom,hpm-min-load",
1307 &rpm_vreg->hpm_min_load);
1308
1309 rpm_vreg->handle_active = msm_rpm_create_request(RPM_SET_ACTIVE,
1310 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1311 if (rpm_vreg->handle_active == NULL
1312 || IS_ERR(rpm_vreg->handle_active)) {
1313 rc = PTR_ERR(rpm_vreg->handle_active);
1314 dev_err(dev, "%s: failed to create active RPM handle, rc=%d\n",
1315 __func__, rc);
1316 goto fail_free_vreg;
1317 }
1318
1319 rpm_vreg->handle_sleep = msm_rpm_create_request(RPM_SET_SLEEP,
1320 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1321 if (rpm_vreg->handle_sleep == NULL || IS_ERR(rpm_vreg->handle_sleep)) {
1322 rc = PTR_ERR(rpm_vreg->handle_sleep);
1323 dev_err(dev, "%s: failed to create sleep RPM handle, rc=%d\n",
1324 __func__, rc);
1325 goto fail_free_handle_active;
1326 }
1327
1328 INIT_LIST_HEAD(&rpm_vreg->reg_list);
1329
1330 if (rpm_vreg->allow_atomic)
1331 spin_lock_init(&rpm_vreg->slock);
1332 else
1333 mutex_init(&rpm_vreg->mlock);
1334
1335 platform_set_drvdata(pdev, rpm_vreg);
1336
1337 rc = of_platform_populate(node, NULL, NULL, dev);
1338 if (rc) {
1339 dev_err(dev, "%s: failed to add child nodes, rc=%d\n", __func__,
1340 rc);
1341 goto fail_unset_drvdata;
1342 }
1343
1344 pr_debug("successfully probed: %s (%08X) %u\n", rpm_vreg->resource_name,
1345 resource_type, rpm_vreg->resource_id);
1346
1347 return rc;
1348
1349fail_unset_drvdata:
1350 platform_set_drvdata(pdev, NULL);
1351 msm_rpm_free_request(rpm_vreg->handle_sleep);
1352
1353fail_free_handle_active:
1354 msm_rpm_free_request(rpm_vreg->handle_active);
1355
1356fail_free_vreg:
1357 kfree(rpm_vreg);
1358
1359 return rc;
1360}
1361
1362static struct of_device_id rpm_vreg_match_table_device[] = {
1363 { .compatible = "qcom,rpm-regulator-smd", },
1364 {}
1365};
1366
1367static struct of_device_id rpm_vreg_match_table_resource[] = {
1368 { .compatible = "qcom,rpm-regulator-smd-resource", },
1369 {}
1370};
1371
1372static struct platform_driver rpm_vreg_device_driver = {
1373 .probe = rpm_vreg_device_probe,
1374 .remove = __devexit_p(rpm_vreg_device_remove),
1375 .driver = {
1376 .name = "qcom,rpm-regulator-smd",
1377 .owner = THIS_MODULE,
1378 .of_match_table = rpm_vreg_match_table_device,
1379 },
1380};
1381
1382static struct platform_driver rpm_vreg_resource_driver = {
1383 .probe = rpm_vreg_resource_probe,
1384 .remove = __devexit_p(rpm_vreg_resource_remove),
1385 .driver = {
1386 .name = "qcom,rpm-regulator-smd-resource",
1387 .owner = THIS_MODULE,
1388 .of_match_table = rpm_vreg_match_table_resource,
1389 },
1390};
1391
1392/**
1393 * rpm_regulator_smd_driver_init() - initialized SMD RPM regulator driver
1394 *
1395 * This function registers the SMD RPM regulator platform drivers.
1396 *
1397 * Returns 0 on success or errno on failure.
1398 */
1399int __init rpm_regulator_smd_driver_init(void)
1400{
1401 static bool initialized;
1402 int i, rc;
1403
1404 if (initialized)
1405 return 0;
1406 else
1407 initialized = true;
1408
1409 /* Store parameter string names as integers */
1410 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++)
1411 params[i].key = rpm_vreg_string_to_int(params[i].name);
1412
1413 rc = platform_driver_register(&rpm_vreg_device_driver);
1414 if (rc)
1415 return rc;
1416
1417 return platform_driver_register(&rpm_vreg_resource_driver);
1418}
1419EXPORT_SYMBOL_GPL(rpm_regulator_smd_driver_init);
1420
1421static void __exit rpm_vreg_exit(void)
1422{
1423 platform_driver_unregister(&rpm_vreg_device_driver);
1424 platform_driver_unregister(&rpm_vreg_resource_driver);
1425}
1426
1427module_init(rpm_regulator_smd_driver_init);
1428module_exit(rpm_vreg_exit);
1429
1430MODULE_LICENSE("GPL v2");
1431MODULE_DESCRIPTION("MSM SMD RPM regulator driver");