blob: c4c9566b511c647bb8d500f7b2093111459bfbe1 [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
David Collinsc7642322012-04-04 10:19:12 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/module.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <linux/string.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27#include <linux/regulator/of_regulator.h>
28#include <mach/rpm-smd.h>
29#include <mach/rpm-regulator-smd.h>
30#include <mach/socinfo.h>
31
32/* Debug Definitions */
33
34enum {
35 RPM_VREG_DEBUG_REQUEST = BIT(0),
36 RPM_VREG_DEBUG_FULL_REQUEST = BIT(1),
37 RPM_VREG_DEBUG_DUPLICATE = BIT(2),
38};
39
40static int rpm_vreg_debug_mask;
41module_param_named(
42 debug_mask, rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR
43);
44
45#define vreg_err(req, fmt, ...) \
46 pr_err("%s: " fmt, req->rdesc.name, ##__VA_ARGS__)
47
48/* RPM regulator request types */
49enum rpm_regulator_smd_type {
50 RPM_REGULATOR_SMD_TYPE_LDO,
51 RPM_REGULATOR_SMD_TYPE_SMPS,
52 RPM_REGULATOR_SMD_TYPE_VS,
53 RPM_REGULATOR_SMD_TYPE_NCP,
54 RPM_REGULATOR_SMD_TYPE_MAX,
55};
56
57/* RPM resource parameters */
58enum rpm_regulator_param_index {
59 RPM_REGULATOR_PARAM_ENABLE,
60 RPM_REGULATOR_PARAM_VOLTAGE,
61 RPM_REGULATOR_PARAM_CURRENT,
62 RPM_REGULATOR_PARAM_MODE_LDO,
63 RPM_REGULATOR_PARAM_MODE_SMPS,
64 RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE,
65 RPM_REGULATOR_PARAM_PIN_CTRL_MODE,
66 RPM_REGULATOR_PARAM_FREQUENCY,
67 RPM_REGULATOR_PARAM_HEAD_ROOM,
68 RPM_REGULATOR_PARAM_QUIET_MODE,
69 RPM_REGULATOR_PARAM_FREQ_REASON,
David Collins4208ce32012-06-15 13:33:13 -070070 RPM_REGULATOR_PARAM_CORNER,
David Collins85b71992012-07-18 12:00:14 -070071 RPM_REGULATOR_PARAM_BYPASS,
David 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 Collins71c18992012-07-18 11:25:24 -0700115 PARAM(CORNER, 0, 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
1040static struct regulator_ops smps_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,
1045 .get_voltage = rpm_vreg_get_voltage,
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 Collins4208ce32012-06-15 13:33:13 -07001053static struct regulator_ops smps_corner_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_corner,
1058 .get_voltage = rpm_vreg_get_voltage_corner,
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 Collinsc7642322012-04-04 10:19:12 -07001066static struct regulator_ops switch_ops = {
1067 .enable = rpm_vreg_enable,
1068 .disable = rpm_vreg_disable,
1069 .is_enabled = rpm_vreg_is_enabled,
1070 .enable_time = rpm_vreg_enable_time,
1071};
1072
1073static struct regulator_ops ncp_ops = {
1074 .enable = rpm_vreg_enable,
1075 .disable = rpm_vreg_disable,
1076 .is_enabled = rpm_vreg_is_enabled,
1077 .set_voltage = rpm_vreg_set_voltage,
1078 .get_voltage = rpm_vreg_get_voltage,
1079 .list_voltage = rpm_vreg_list_voltage,
1080 .enable_time = rpm_vreg_enable_time,
1081};
1082
1083static struct regulator_ops *vreg_ops[] = {
1084 [RPM_REGULATOR_SMD_TYPE_LDO] = &ldo_ops,
1085 [RPM_REGULATOR_SMD_TYPE_SMPS] = &smps_ops,
1086 [RPM_REGULATOR_SMD_TYPE_VS] = &switch_ops,
1087 [RPM_REGULATOR_SMD_TYPE_NCP] = &ncp_ops,
1088};
1089
1090static int __devexit rpm_vreg_device_remove(struct platform_device *pdev)
1091{
1092 struct device *dev = &pdev->dev;
1093 struct rpm_regulator *reg;
1094
1095 reg = platform_get_drvdata(pdev);
1096 if (reg) {
1097 rpm_vreg_lock(reg->rpm_vreg);
1098 regulator_unregister(reg->rdev);
1099 list_del(&reg->list);
1100 kfree(reg);
1101 rpm_vreg_unlock(reg->rpm_vreg);
1102 } else {
1103 dev_err(dev, "%s: drvdata missing\n", __func__);
1104 return -EINVAL;
1105 }
1106
1107 platform_set_drvdata(pdev, NULL);
1108
1109 return 0;
1110}
1111
1112static int __devexit rpm_vreg_resource_remove(struct platform_device *pdev)
1113{
1114 struct device *dev = &pdev->dev;
1115 struct rpm_regulator *reg, *reg_temp;
1116 struct rpm_vreg *rpm_vreg;
1117
1118 rpm_vreg = platform_get_drvdata(pdev);
1119 if (rpm_vreg) {
1120 rpm_vreg_lock(rpm_vreg);
1121 list_for_each_entry_safe(reg, reg_temp, &rpm_vreg->reg_list,
1122 list) {
1123 /* Only touch data for private consumers. */
1124 if (reg->rdev->desc == NULL) {
1125 list_del(&reg->list);
1126 kfree(reg->rdev);
1127 kfree(reg);
1128 } else {
1129 dev_err(dev, "%s: not all child devices have "
1130 "been removed\n", __func__);
1131 }
1132 }
1133 rpm_vreg_unlock(rpm_vreg);
1134
1135 msm_rpm_free_request(rpm_vreg->handle_active);
1136 msm_rpm_free_request(rpm_vreg->handle_sleep);
1137
1138 kfree(rpm_vreg);
1139 } else {
1140 dev_err(dev, "%s: drvdata missing\n", __func__);
1141 return -EINVAL;
1142 }
1143
1144 platform_set_drvdata(pdev, NULL);
1145
1146 return 0;
1147}
1148
1149/*
1150 * This probe is called for child rpm-regulator devices which have
1151 * properties which are required to configure individual regulator
1152 * framework regulators for a given RPM regulator resource.
1153 */
1154static int __devinit rpm_vreg_device_probe(struct platform_device *pdev)
1155{
1156 struct device *dev = &pdev->dev;
1157 struct device_node *node = dev->of_node;
1158 struct regulator_init_data *init_data;
1159 struct rpm_vreg *rpm_vreg;
1160 struct rpm_regulator *reg;
1161 int rc = 0;
1162 int i, regulator_type;
1163 u32 val;
1164
1165 if (!dev->of_node) {
1166 dev_err(dev, "%s: device tree information missing\n", __func__);
1167 return -ENODEV;
1168 }
1169
1170 if (pdev->dev.parent == NULL) {
1171 dev_err(dev, "%s: parent device missing\n", __func__);
1172 return -ENODEV;
1173 }
1174
1175 rpm_vreg = dev_get_drvdata(pdev->dev.parent);
1176 if (rpm_vreg == NULL) {
1177 dev_err(dev, "%s: rpm_vreg not found in parent device\n",
1178 __func__);
1179 return -ENODEV;
1180 }
1181
1182 reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
1183 if (reg == NULL) {
1184 dev_err(dev, "%s: could not allocate memory for reg\n",
1185 __func__);
1186 return -ENOMEM;
1187 }
1188
1189 regulator_type = rpm_vreg->regulator_type;
1190 reg->rpm_vreg = rpm_vreg;
1191 reg->rdesc.ops = vreg_ops[regulator_type];
1192 reg->rdesc.owner = THIS_MODULE;
1193 reg->rdesc.type = REGULATOR_VOLTAGE;
1194
David Collins4208ce32012-06-15 13:33:13 -07001195 /*
1196 * Switch to voltage corner regulator ops if qcom,use-voltage-corner
1197 * is specified in the device node (SMPS only).
1198 */
1199 if (of_find_property(node, "qcom,use-voltage-corner", NULL)
1200 && regulator_type == RPM_REGULATOR_SMD_TYPE_SMPS)
1201 reg->rdesc.ops = &smps_corner_ops;
1202
David Collinsc7642322012-04-04 10:19:12 -07001203 if (regulator_type == RPM_REGULATOR_SMD_TYPE_VS)
1204 reg->rdesc.n_voltages = 0;
1205 else
1206 reg->rdesc.n_voltages = 2;
1207
1208 rc = of_property_read_u32(node, "qcom,set", &val);
1209 if (rc) {
1210 dev_err(dev, "%s: sleep set and/or active set must be "
1211 "configured via qcom,set property, rc=%d\n", __func__,
1212 rc);
1213 goto fail_free_reg;
1214 } else if (!(val & RPM_SET_CONFIG_BOTH)) {
1215 dev_err(dev, "%s: qcom,set=%u property is invalid\n", __func__,
1216 val);
1217 rc = -EINVAL;
1218 goto fail_free_reg;
1219 }
1220
1221 reg->set_active = !!(val & RPM_SET_CONFIG_ACTIVE);
1222 reg->set_sleep = !!(val & RPM_SET_CONFIG_SLEEP);
1223
David Collins4853ae42012-06-12 09:37:19 -07001224 init_data = of_get_regulator_init_data(dev, node);
David Collinsc7642322012-04-04 10:19:12 -07001225 if (init_data == NULL) {
1226 dev_err(dev, "%s: unable to allocate memory\n", __func__);
1227 rc = -ENOMEM;
1228 goto fail_free_reg;
1229 }
1230 if (init_data->constraints.name == NULL) {
1231 dev_err(dev, "%s: regulator name not specified\n", __func__);
1232 rc = -EINVAL;
1233 goto fail_free_reg;
1234 }
1235
1236 init_data->constraints.input_uV = init_data->constraints.max_uV;
1237
1238 if (of_get_property(node, "parent-supply", NULL))
1239 init_data->supply_regulator = "parent";
1240
1241 /*
1242 * Fill in ops and mode masks based on callbacks specified for
1243 * this type of regulator.
1244 */
1245 if (reg->rdesc.ops->enable)
1246 init_data->constraints.valid_ops_mask
1247 |= REGULATOR_CHANGE_STATUS;
1248 if (reg->rdesc.ops->get_voltage)
1249 init_data->constraints.valid_ops_mask
1250 |= REGULATOR_CHANGE_VOLTAGE;
1251 if (reg->rdesc.ops->get_mode) {
1252 init_data->constraints.valid_ops_mask
1253 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1254 init_data->constraints.valid_modes_mask
1255 |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1256 }
1257
1258 reg->rdesc.name = init_data->constraints.name;
1259 reg->min_uV = init_data->constraints.min_uV;
1260 reg->max_uV = init_data->constraints.max_uV;
1261
1262 /* Initialize the param array based on optional properties. */
1263 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
1264 rc = of_property_read_u32(node, params[i].property_name, &val);
1265 if (rc == 0) {
1266 if (params[i].supported_regulator_types
1267 & BIT(regulator_type)) {
1268 if (val < params[i].min
1269 || val > params[i].max) {
1270 pr_warn("%s: device tree property: "
1271 "%s=%u is outsided allowed "
1272 "range [%u, %u]\n",
1273 reg->rdesc.name,
1274 params[i].property_name, val,
1275 params[i].min, params[i].max);
1276 continue;
1277 }
1278 reg->req.param[i] = val;
1279 reg->req.modified |= BIT(i);
1280 } else {
1281 pr_warn("%s: regulator type=%d does not support"
1282 " device tree property: %s\n",
1283 reg->rdesc.name, regulator_type,
1284 params[i].property_name);
1285 }
1286 }
1287 }
1288
1289 of_property_read_u32(node, "qcom,system_load", &reg->system_load);
1290
1291 rpm_vreg_lock(rpm_vreg);
1292 list_add(&reg->list, &rpm_vreg->reg_list);
1293 rpm_vreg_unlock(rpm_vreg);
1294
1295 reg->rdev = regulator_register(&reg->rdesc, dev, init_data, reg, node);
1296 if (IS_ERR(reg->rdev)) {
1297 rc = PTR_ERR(reg->rdev);
1298 reg->rdev = NULL;
1299 pr_err("regulator_register failed: %s, rc=%d\n",
1300 reg->rdesc.name, rc);
1301 goto fail_remove_from_list;
1302 }
1303
1304 platform_set_drvdata(pdev, reg);
1305
1306 pr_debug("successfully probed: %s\n", reg->rdesc.name);
1307
1308 return 0;
1309
1310fail_remove_from_list:
1311 rpm_vreg_lock(rpm_vreg);
1312 list_del(&reg->list);
1313 rpm_vreg_unlock(rpm_vreg);
1314
1315fail_free_reg:
1316 kfree(reg);
1317 return rc;
1318}
1319
1320/*
1321 * This probe is called for parent rpm-regulator devices which have
1322 * properties which are required to identify a given RPM resource.
1323 */
1324static int __devinit rpm_vreg_resource_probe(struct platform_device *pdev)
1325{
1326 struct device *dev = &pdev->dev;
1327 struct device_node *node = dev->of_node;
1328 struct rpm_vreg *rpm_vreg;
1329 int val = 0;
1330 u32 resource_type;
1331 int rc;
1332
1333 if (!dev->of_node) {
1334 dev_err(dev, "%s: device tree information missing\n", __func__);
1335 return -ENODEV;
1336 }
1337
1338 /* Create new rpm_vreg entry. */
1339 rpm_vreg = kzalloc(sizeof(struct rpm_vreg), GFP_KERNEL);
1340 if (rpm_vreg == NULL) {
1341 dev_err(dev, "%s: could not allocate memory for vreg\n",
1342 __func__);
1343 return -ENOMEM;
1344 }
1345
1346 /* Required device tree properties: */
1347 rc = of_property_read_string(node, "qcom,resource-name",
1348 &rpm_vreg->resource_name);
1349 if (rc) {
1350 dev_err(dev, "%s: qcom,resource-name missing in DT node\n",
1351 __func__);
1352 goto fail_free_vreg;
1353 }
1354 resource_type = rpm_vreg_string_to_int(rpm_vreg->resource_name);
1355
1356 rc = of_property_read_u32(node, "qcom,resource-id",
1357 &rpm_vreg->resource_id);
1358 if (rc) {
1359 dev_err(dev, "%s: qcom,resource-id missing in DT node\n",
1360 __func__);
1361 goto fail_free_vreg;
1362 }
1363
1364 rc = of_property_read_u32(node, "qcom,regulator-type",
1365 &rpm_vreg->regulator_type);
1366 if (rc) {
1367 dev_err(dev, "%s: qcom,regulator-type missing in DT node\n",
1368 __func__);
1369 goto fail_free_vreg;
1370 }
1371
1372 if ((rpm_vreg->regulator_type < 0)
1373 || (rpm_vreg->regulator_type >= RPM_REGULATOR_SMD_TYPE_MAX)) {
1374 dev_err(dev, "%s: invalid regulator type: %d\n", __func__,
1375 rpm_vreg->regulator_type);
1376 rc = -EINVAL;
1377 goto fail_free_vreg;
1378 }
1379
1380 /* Optional device tree properties: */
1381 of_property_read_u32(node, "qcom,allow-atomic", &val);
1382 rpm_vreg->allow_atomic = !!val;
1383 of_property_read_u32(node, "qcom,enable-time", &rpm_vreg->enable_time);
1384 of_property_read_u32(node, "qcom,hpm-min-load",
1385 &rpm_vreg->hpm_min_load);
1386
1387 rpm_vreg->handle_active = msm_rpm_create_request(RPM_SET_ACTIVE,
1388 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1389 if (rpm_vreg->handle_active == NULL
1390 || IS_ERR(rpm_vreg->handle_active)) {
1391 rc = PTR_ERR(rpm_vreg->handle_active);
1392 dev_err(dev, "%s: failed to create active RPM handle, rc=%d\n",
1393 __func__, rc);
1394 goto fail_free_vreg;
1395 }
1396
1397 rpm_vreg->handle_sleep = msm_rpm_create_request(RPM_SET_SLEEP,
1398 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1399 if (rpm_vreg->handle_sleep == NULL || IS_ERR(rpm_vreg->handle_sleep)) {
1400 rc = PTR_ERR(rpm_vreg->handle_sleep);
1401 dev_err(dev, "%s: failed to create sleep RPM handle, rc=%d\n",
1402 __func__, rc);
1403 goto fail_free_handle_active;
1404 }
1405
1406 INIT_LIST_HEAD(&rpm_vreg->reg_list);
1407
1408 if (rpm_vreg->allow_atomic)
1409 spin_lock_init(&rpm_vreg->slock);
1410 else
1411 mutex_init(&rpm_vreg->mlock);
1412
1413 platform_set_drvdata(pdev, rpm_vreg);
1414
1415 rc = of_platform_populate(node, NULL, NULL, dev);
1416 if (rc) {
1417 dev_err(dev, "%s: failed to add child nodes, rc=%d\n", __func__,
1418 rc);
1419 goto fail_unset_drvdata;
1420 }
1421
1422 pr_debug("successfully probed: %s (%08X) %u\n", rpm_vreg->resource_name,
1423 resource_type, rpm_vreg->resource_id);
1424
1425 return rc;
1426
1427fail_unset_drvdata:
1428 platform_set_drvdata(pdev, NULL);
1429 msm_rpm_free_request(rpm_vreg->handle_sleep);
1430
1431fail_free_handle_active:
1432 msm_rpm_free_request(rpm_vreg->handle_active);
1433
1434fail_free_vreg:
1435 kfree(rpm_vreg);
1436
1437 return rc;
1438}
1439
1440static struct of_device_id rpm_vreg_match_table_device[] = {
1441 { .compatible = "qcom,rpm-regulator-smd", },
1442 {}
1443};
1444
1445static struct of_device_id rpm_vreg_match_table_resource[] = {
1446 { .compatible = "qcom,rpm-regulator-smd-resource", },
1447 {}
1448};
1449
1450static struct platform_driver rpm_vreg_device_driver = {
1451 .probe = rpm_vreg_device_probe,
1452 .remove = __devexit_p(rpm_vreg_device_remove),
1453 .driver = {
1454 .name = "qcom,rpm-regulator-smd",
1455 .owner = THIS_MODULE,
1456 .of_match_table = rpm_vreg_match_table_device,
1457 },
1458};
1459
1460static struct platform_driver rpm_vreg_resource_driver = {
1461 .probe = rpm_vreg_resource_probe,
1462 .remove = __devexit_p(rpm_vreg_resource_remove),
1463 .driver = {
1464 .name = "qcom,rpm-regulator-smd-resource",
1465 .owner = THIS_MODULE,
1466 .of_match_table = rpm_vreg_match_table_resource,
1467 },
1468};
1469
1470/**
1471 * rpm_regulator_smd_driver_init() - initialized SMD RPM regulator driver
1472 *
1473 * This function registers the SMD RPM regulator platform drivers.
1474 *
1475 * Returns 0 on success or errno on failure.
1476 */
1477int __init rpm_regulator_smd_driver_init(void)
1478{
1479 static bool initialized;
1480 int i, rc;
1481
1482 if (initialized)
1483 return 0;
1484 else
1485 initialized = true;
1486
1487 /* Store parameter string names as integers */
1488 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++)
1489 params[i].key = rpm_vreg_string_to_int(params[i].name);
1490
1491 rc = platform_driver_register(&rpm_vreg_device_driver);
1492 if (rc)
1493 return rc;
1494
1495 return platform_driver_register(&rpm_vreg_resource_driver);
1496}
1497EXPORT_SYMBOL_GPL(rpm_regulator_smd_driver_init);
1498
1499static void __exit rpm_vreg_exit(void)
1500{
1501 platform_driver_unregister(&rpm_vreg_device_driver);
1502 platform_driver_unregister(&rpm_vreg_resource_driver);
1503}
1504
1505module_init(rpm_regulator_smd_driver_init);
1506module_exit(rpm_vreg_exit);
1507
1508MODULE_LICENSE("GPL v2");
1509MODULE_DESCRIPTION("MSM SMD RPM regulator driver");