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