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