blob: cded6e4c29f3b4a758b171e3d34beb2ffb55a729 [file] [log] [blame]
Kiran Gundacdb64c22019-11-27 18:23:35 +05301/* Copyright (c) 2012-2015, 2019 The Linux Foundation. All rights reserved.
Kiran Gundaf9d484ef2017-10-16 12:15:30 +05302 *
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 <linux/regulator/rpm-smd-regulator.h>
29#include <soc/qcom/rpm-smd.h>
30
31/* Debug Definitions */
32
33enum {
34 RPM_VREG_DEBUG_REQUEST = BIT(0),
35 RPM_VREG_DEBUG_FULL_REQUEST = BIT(1),
36 RPM_VREG_DEBUG_DUPLICATE = BIT(2),
37};
38
39static int rpm_vreg_debug_mask;
40module_param_named(
41 debug_mask, rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR
42);
43
44#define vreg_err(req, fmt, ...) \
45 pr_err("%s: " fmt, req->rdesc.name, ##__VA_ARGS__)
46
47/* RPM regulator request types */
48enum rpm_regulator_type {
49 RPM_REGULATOR_TYPE_LDO,
50 RPM_REGULATOR_TYPE_SMPS,
51 RPM_REGULATOR_TYPE_VS,
52 RPM_REGULATOR_TYPE_NCP,
53 RPM_REGULATOR_TYPE_BOB,
54 RPM_REGULATOR_TYPE_MAX,
55};
56
57/* RPM resource parameters */
58enum rpm_regulator_param_index {
59 RPM_REGULATOR_PARAM_ENABLE,
60 RPM_REGULATOR_PARAM_VOLTAGE,
61 RPM_REGULATOR_PARAM_CURRENT,
62 RPM_REGULATOR_PARAM_MODE_LDO,
63 RPM_REGULATOR_PARAM_MODE_SMPS,
64 RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE,
65 RPM_REGULATOR_PARAM_PIN_CTRL_MODE,
66 RPM_REGULATOR_PARAM_FREQUENCY,
67 RPM_REGULATOR_PARAM_HEAD_ROOM,
68 RPM_REGULATOR_PARAM_QUIET_MODE,
69 RPM_REGULATOR_PARAM_FREQ_REASON,
70 RPM_REGULATOR_PARAM_CORNER,
71 RPM_REGULATOR_PARAM_BYPASS,
72 RPM_REGULATOR_PARAM_FLOOR_CORNER,
73 RPM_REGULATOR_PARAM_LEVEL,
74 RPM_REGULATOR_PARAM_FLOOR_LEVEL,
75 RPM_REGULATOR_PARAM_MODE_BOB,
76 RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE1,
77 RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE2,
78 RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE3,
79 RPM_REGULATOR_PARAM_MAX,
80};
81
82enum rpm_regulator_smps_mode {
83 RPM_REGULATOR_SMPS_MODE_AUTO = 0,
84 RPM_REGULATOR_SMPS_MODE_IPEAK = 1,
85 RPM_REGULATOR_SMPS_MODE_PWM = 2,
86};
87
88enum rpm_regulator_ldo_mode {
89 RPM_REGULATOR_LDO_MODE_IPEAK = 0,
90 RPM_REGULATOR_LDO_MODE_HPM = 1,
91};
92
93enum rpm_regulator_bob_mode {
94 RPM_REGULATOR_BOB_MODE_PASS = 0,
95 RPM_REGULATOR_BOB_MODE_PFM = 1,
96 RPM_REGULATOR_BOB_MODE_AUTO = 2,
97 RPM_REGULATOR_BOB_MODE_PWM = 3,
98};
99
100#define RPM_SET_CONFIG_ACTIVE BIT(0)
101#define RPM_SET_CONFIG_SLEEP BIT(1)
102#define RPM_SET_CONFIG_BOTH (RPM_SET_CONFIG_ACTIVE \
103 | RPM_SET_CONFIG_SLEEP)
104struct rpm_regulator_param {
105 char *name;
106 char *property_name;
107 u32 key;
108 u32 min;
109 u32 max;
110 u32 supported_regulator_types;
111};
112
113#define PARAM(_idx, _support_ldo, _support_smps, _support_vs, _support_ncp, \
114 _support_bob, _name, _min, _max, _property_name) \
115 [RPM_REGULATOR_PARAM_##_idx] = { \
116 .name = _name, \
117 .property_name = _property_name, \
118 .min = _min, \
119 .max = _max, \
120 .supported_regulator_types = \
121 _support_ldo << RPM_REGULATOR_TYPE_LDO | \
122 _support_smps << RPM_REGULATOR_TYPE_SMPS | \
123 _support_vs << RPM_REGULATOR_TYPE_VS | \
124 _support_ncp << RPM_REGULATOR_TYPE_NCP | \
125 _support_bob << RPM_REGULATOR_TYPE_BOB, \
126 }
127
128static struct rpm_regulator_param params[RPM_REGULATOR_PARAM_MAX] = {
129 /* ID LDO SMPS VS NCP BOB name min max property-name */
130 PARAM(ENABLE, 1, 1, 1, 1, 1, "swen", 0, 1, "qcom,init-enable"),
131 PARAM(VOLTAGE, 1, 1, 0, 1, 1, "uv", 0, 0x7FFFFFF, "qcom,init-voltage"),
132 PARAM(CURRENT, 1, 1, 0, 0, 0, "ma", 0, 0x1FFF, "qcom,init-current"),
133 PARAM(MODE_LDO, 1, 0, 0, 0, 0, "lsmd", 0, 1, "qcom,init-ldo-mode"),
134 PARAM(MODE_SMPS, 0, 1, 0, 0, 0, "ssmd", 0, 2, "qcom,init-smps-mode"),
135 PARAM(PIN_CTRL_ENABLE, 1, 1, 1, 0, 0, "pcen", 0, 0xF, "qcom,init-pin-ctrl-enable"),
136 PARAM(PIN_CTRL_MODE, 1, 1, 1, 0, 0, "pcmd", 0, 0x1F, "qcom,init-pin-ctrl-mode"),
137 PARAM(FREQUENCY, 0, 1, 0, 1, 0, "freq", 0, 31, "qcom,init-frequency"),
138 PARAM(HEAD_ROOM, 1, 0, 0, 1, 0, "hr", 0, 0x7FFFFFFF, "qcom,init-head-room"),
139 PARAM(QUIET_MODE, 0, 1, 0, 0, 0, "qm", 0, 2, "qcom,init-quiet-mode"),
140 PARAM(FREQ_REASON, 0, 1, 0, 1, 0, "resn", 0, 8, "qcom,init-freq-reason"),
141 PARAM(CORNER, 1, 1, 0, 0, 0, "corn", 0, 6, "qcom,init-voltage-corner"),
142 PARAM(BYPASS, 1, 0, 0, 0, 0, "bypa", 0, 1, "qcom,init-disallow-bypass"),
143 PARAM(FLOOR_CORNER, 1, 1, 0, 0, 0, "vfc", 0, 6, "qcom,init-voltage-floor-corner"),
144 PARAM(LEVEL, 1, 1, 0, 0, 0, "vlvl", 0, 0xFFFF, "qcom,init-voltage-level"),
145 PARAM(FLOOR_LEVEL, 1, 1, 0, 0, 0, "vfl", 0, 0xFFFF, "qcom,init-voltage-floor-level"),
146 PARAM(MODE_BOB, 0, 0, 0, 0, 1, "bobm", 0, 3, "qcom,init-bob-mode"),
147 PARAM(PIN_CTRL_VOLTAGE1, 0, 0, 0, 0, 1, "pcv1", 0, 0x7FFFFFF, "qcom,init-pin-ctrl-voltage1"),
148 PARAM(PIN_CTRL_VOLTAGE2, 0, 0, 0, 0, 1, "pcv2", 0, 0x7FFFFFF, "qcom,init-pin-ctrl-voltage2"),
149 PARAM(PIN_CTRL_VOLTAGE3, 0, 0, 0, 0, 1, "pcv3", 0, 0x7FFFFFF, "qcom,init-pin-ctrl-voltage3"),
150};
151
152struct rpm_regulator_mode_map {
153 int ldo_mode;
154 int smps_mode;
155};
156
157static struct rpm_regulator_mode_map mode_mapping[] = {
158 [RPM_REGULATOR_MODE_AUTO]
159 = {-1, RPM_REGULATOR_SMPS_MODE_AUTO},
160 [RPM_REGULATOR_MODE_IPEAK]
161 = {RPM_REGULATOR_LDO_MODE_IPEAK, RPM_REGULATOR_SMPS_MODE_IPEAK},
162 [RPM_REGULATOR_MODE_HPM]
163 = {RPM_REGULATOR_LDO_MODE_HPM, RPM_REGULATOR_SMPS_MODE_PWM},
164};
165
166/* Indices for use with pin control enable via enable/disable feature. */
167#define RPM_VREG_PIN_CTRL_STATE_DISABLE 0
168#define RPM_VREG_PIN_CTRL_STATE_ENABLE 1
169#define RPM_VREG_PIN_CTRL_STATE_COUNT 2
170
171struct rpm_vreg_request {
172 u32 param[RPM_REGULATOR_PARAM_MAX];
173 u32 valid;
174 u32 modified;
175};
176
177struct rpm_vreg {
178 struct rpm_vreg_request aggr_req_active;
179 struct rpm_vreg_request aggr_req_sleep;
180 struct list_head reg_list;
181 const char *resource_name;
182 u32 resource_id;
183 bool allow_atomic;
184 int regulator_type;
185 int hpm_min_load;
186 int enable_time;
187 spinlock_t slock;
188 struct mutex mlock;
189 unsigned long flags;
190 bool sleep_request_sent;
191 bool wait_for_ack_active;
192 bool wait_for_ack_sleep;
193 bool always_wait_for_ack;
194 bool apps_only;
195 struct msm_rpm_request *handle_active;
196 struct msm_rpm_request *handle_sleep;
197};
198
199struct rpm_regulator {
200 struct regulator_desc rdesc;
201 struct regulator_dev *rdev;
202 struct rpm_vreg *rpm_vreg;
203 struct list_head list;
204 bool set_active;
205 bool set_sleep;
206 bool always_send_voltage;
207 bool always_send_current;
208 bool use_pin_ctrl_for_enable;
209 struct rpm_vreg_request req;
210 int system_load;
211 int min_uV;
212 int max_uV;
213 u32 pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_COUNT];
214 enum rpm_regulator_param_index voltage_index;
215 int voltage_offset;
216};
217
218/*
219 * This voltage in uV is returned by get_voltage functions when there is no way
220 * to determine the current voltage level. It is needed because the regulator
221 * framework treats a 0 uV voltage as an error.
222 */
223#define VOLTAGE_UNKNOWN 1
224
225/*
226 * Regulator requests sent in the active set take effect immediately. Requests
227 * sent in the sleep set take effect when the Apps processor transitions into
228 * RPM assisted power collapse. For any given regulator, if an active set
229 * request is present, but not a sleep set request, then the active set request
230 * is used at all times, even when the Apps processor is power collapsed.
231 *
232 * The rpm-regulator-smd takes advantage of this default usage of the active set
233 * request by only sending a sleep set request if it differs from the
234 * corresponding active set request.
235 */
236#define RPM_SET_ACTIVE MSM_RPM_CTX_ACTIVE_SET
237#define RPM_SET_SLEEP MSM_RPM_CTX_SLEEP_SET
238
239static u32 rpm_vreg_string_to_int(const u8 *str)
240{
241 int i, len;
242 u32 output = 0;
243
244 len = strnlen(str, sizeof(u32));
245 for (i = 0; i < len; i++)
246 output |= str[i] << (i * 8);
247
248 return output;
249}
250
251static inline void rpm_vreg_lock(struct rpm_vreg *rpm_vreg)
252{
253 if (rpm_vreg->allow_atomic)
254 spin_lock_irqsave(&rpm_vreg->slock, rpm_vreg->flags);
255 else
256 mutex_lock(&rpm_vreg->mlock);
257}
258
259static inline void rpm_vreg_unlock(struct rpm_vreg *rpm_vreg)
260{
261 if (rpm_vreg->allow_atomic)
262 spin_unlock_irqrestore(&rpm_vreg->slock, rpm_vreg->flags);
263 else
264 mutex_unlock(&rpm_vreg->mlock);
265}
266
267static inline bool rpm_vreg_active_or_sleep_enabled(struct rpm_vreg *rpm_vreg)
268{
269 return (rpm_vreg->aggr_req_active.param[RPM_REGULATOR_PARAM_ENABLE]
270 && (rpm_vreg->aggr_req_active.valid
271 & BIT(RPM_REGULATOR_PARAM_ENABLE)))
272 || ((rpm_vreg->aggr_req_sleep.param[RPM_REGULATOR_PARAM_ENABLE])
273 && (rpm_vreg->aggr_req_sleep.valid
274 & BIT(RPM_REGULATOR_PARAM_ENABLE)));
275}
276
277static inline bool rpm_vreg_shared_active_or_sleep_enabled_valid
278 (struct rpm_vreg *rpm_vreg)
279{
280 return !rpm_vreg->apps_only &&
281 ((rpm_vreg->aggr_req_active.valid
282 & BIT(RPM_REGULATOR_PARAM_ENABLE))
283 || (rpm_vreg->aggr_req_sleep.valid
284 & BIT(RPM_REGULATOR_PARAM_ENABLE)));
285}
286
287static const u32 power_level_params =
288 BIT(RPM_REGULATOR_PARAM_ENABLE) |
289 BIT(RPM_REGULATOR_PARAM_VOLTAGE) |
290 BIT(RPM_REGULATOR_PARAM_CURRENT) |
291 BIT(RPM_REGULATOR_PARAM_CORNER) |
292 BIT(RPM_REGULATOR_PARAM_BYPASS) |
293 BIT(RPM_REGULATOR_PARAM_FLOOR_CORNER) |
294 BIT(RPM_REGULATOR_PARAM_LEVEL) |
295 BIT(RPM_REGULATOR_PARAM_FLOOR_LEVEL);
296
297static bool rpm_vreg_ack_required(struct rpm_vreg *rpm_vreg, u32 set,
298 const u32 *prev_param, const u32 *param,
299 u32 prev_valid, u32 modified)
300{
301 u32 mask;
302 int i;
303
304 if (rpm_vreg->always_wait_for_ack
305 || (set == RPM_SET_ACTIVE && rpm_vreg->wait_for_ack_active)
306 || (set == RPM_SET_SLEEP && rpm_vreg->wait_for_ack_sleep))
307 return true;
308
309 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
310 mask = BIT(i);
311 if (modified & mask) {
312 if ((prev_valid & mask) && (power_level_params & mask)
313 && (param[i] <= prev_param[i]))
314 continue;
315 else
316 return true;
317 }
318 }
319
320 return false;
321}
322
323static void rpm_vreg_check_param_max(struct rpm_regulator *regulator, int index,
324 u32 new_max)
325{
326 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
327
328 if (regulator->set_active
329 && (rpm_vreg->aggr_req_active.valid & BIT(index))
330 && rpm_vreg->aggr_req_active.param[index] > new_max)
331 rpm_vreg->wait_for_ack_active = true;
332
333 if (regulator->set_sleep
334 && (rpm_vreg->aggr_req_sleep.valid & BIT(index))
335 && rpm_vreg->aggr_req_sleep.param[index] > new_max)
336 rpm_vreg->wait_for_ack_sleep = true;
337}
338
339/*
340 * This is used when voting for LPM or HPM by subtracting or adding to the
341 * hpm_min_load of a regulator. It has units of uA.
342 */
343#define LOAD_THRESHOLD_STEP 1000
344
345static inline int rpm_vreg_hpm_min_uA(struct rpm_vreg *rpm_vreg)
346{
347 return rpm_vreg->hpm_min_load;
348}
349
350static inline int rpm_vreg_lpm_max_uA(struct rpm_vreg *rpm_vreg)
351{
352 return rpm_vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
353}
354
355#define MICRO_TO_MILLI(uV) ((uV) / 1000)
356#define MILLI_TO_MICRO(uV) ((uV) * 1000)
357
358#define DEBUG_PRINT_BUFFER_SIZE 512
359#define REQ_SENT 0
360#define REQ_PREV 1
361#define REQ_CACHED 2
362#define REQ_TYPES 3
363
364static void rpm_regulator_req(struct rpm_regulator *regulator, int set,
365 bool sent)
366{
367 char buf[DEBUG_PRINT_BUFFER_SIZE];
368 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
369 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
370 struct rpm_vreg_request *aggr;
371 bool first;
372 u32 mask[REQ_TYPES] = {0, 0, 0};
373 const char *req_names[REQ_TYPES] = {"sent", "prev", "cached"};
374 int pos = 0;
375 int i, j;
376
377 aggr = (set == RPM_SET_ACTIVE)
378 ? &rpm_vreg->aggr_req_active : &rpm_vreg->aggr_req_sleep;
379
380 if (rpm_vreg_debug_mask & RPM_VREG_DEBUG_DUPLICATE) {
381 mask[REQ_SENT] = aggr->modified;
382 mask[REQ_PREV] = aggr->valid & ~aggr->modified;
383 } else if (sent
384 && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_FULL_REQUEST)) {
385 mask[REQ_SENT] = aggr->modified;
386 mask[REQ_PREV] = aggr->valid & ~aggr->modified;
387 } else if (sent && (rpm_vreg_debug_mask & RPM_VREG_DEBUG_REQUEST)) {
388 mask[REQ_SENT] = aggr->modified;
389 }
390
391 if (!(mask[REQ_SENT] | mask[REQ_PREV]))
392 return;
393
394 if (set == RPM_SET_SLEEP && !rpm_vreg->sleep_request_sent) {
395 mask[REQ_CACHED] = mask[REQ_SENT] | mask[REQ_PREV];
396 mask[REQ_SENT] = 0;
397 mask[REQ_PREV] = 0;
398 }
399
400 pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
401 KERN_INFO, __func__);
402
403 pos += scnprintf(buf + pos, buflen - pos, "%s %u (%s): s=%s",
404 rpm_vreg->resource_name, rpm_vreg->resource_id,
405 regulator->rdesc.name,
406 (set == RPM_SET_ACTIVE ? "act" : "slp"));
407
408 for (i = 0; i < REQ_TYPES; i++) {
409 if (mask[i])
410 pos += scnprintf(buf + pos, buflen - pos, "; %s: ",
411 req_names[i]);
412
413 first = true;
414 for (j = 0; j < RPM_REGULATOR_PARAM_MAX; j++) {
415 if (mask[i] & BIT(j)) {
416 pos += scnprintf(buf + pos, buflen - pos,
417 "%s%s=%u", (first ? "" : ", "),
418 params[j].name, aggr->param[j]);
419 first = false;
420 }
421 }
422 }
423
424 pos += scnprintf(buf + pos, buflen - pos, "\n");
425 printk(buf);
426}
427
428#define RPM_VREG_SET_PARAM(_regulator, _param, _val) \
429{ \
430 (_regulator)->req.param[RPM_REGULATOR_PARAM_##_param] = _val; \
431 (_regulator)->req.modified |= BIT(RPM_REGULATOR_PARAM_##_param); \
432} \
433
434static int rpm_vreg_add_kvp_to_request(struct rpm_vreg *rpm_vreg,
435 const u32 *param, int idx, u32 set)
436{
437 struct msm_rpm_request *handle;
438
439 handle = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active
440 : rpm_vreg->handle_sleep);
441
442 if (rpm_vreg->allow_atomic)
443 return msm_rpm_add_kvp_data_noirq(handle, params[idx].key,
444 (u8 *)&param[idx], 4);
445 else
446 return msm_rpm_add_kvp_data(handle, params[idx].key,
447 (u8 *)&param[idx], 4);
448}
449
450static void rpm_vreg_check_modified_requests(const u32 *prev_param,
451 const u32 *param, u32 prev_valid, u32 *modified)
452{
453 u32 value_changed = 0;
454 int i;
455
456 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
457 if (param[i] != prev_param[i])
458 value_changed |= BIT(i);
459 }
460
461 /*
462 * Only keep bits that are for changed parameters or previously
463 * invalid parameters.
464 */
465 *modified &= value_changed | ~prev_valid;
466}
467
468static int rpm_vreg_add_modified_requests(struct rpm_regulator *regulator,
469 u32 set, const u32 *param, u32 modified)
470{
471 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
472 int rc = 0;
473 int i;
474
475 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
476 /* Only send requests for modified parameters. */
477 if (modified & BIT(i)) {
478 rc = rpm_vreg_add_kvp_to_request(rpm_vreg, param, i,
479 set);
480 if (rc) {
481 vreg_err(regulator,
482 "add KVP failed: %s %u; %s, rc=%d\n",
483 rpm_vreg->resource_name,
484 rpm_vreg->resource_id, params[i].name,
485 rc);
486 return rc;
487 }
488 }
489 }
490
491 return rc;
492}
493
494static int rpm_vreg_send_request(struct rpm_regulator *regulator, u32 set,
495 bool wait_for_ack)
496{
497 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
498 struct msm_rpm_request *handle
499 = (set == RPM_SET_ACTIVE ? rpm_vreg->handle_active
500 : rpm_vreg->handle_sleep);
501 int rc = 0;
502 void *temp;
503
504 if (unlikely(rpm_vreg->allow_atomic)) {
505 rc = msm_rpm_wait_for_ack_noirq(msm_rpm_send_request_noirq(
506 handle));
507 } else if (wait_for_ack) {
508 rc = msm_rpm_wait_for_ack(msm_rpm_send_request(handle));
509 } else {
510 temp = msm_rpm_send_request_noack(handle);
511 if (IS_ERR(temp))
512 rc = PTR_ERR(temp);
513 }
514
515 if (rc)
516 vreg_err(regulator,
517 "msm rpm send failed: %s %u; set=%s, rc=%d\n",
518 rpm_vreg->resource_name,
519 rpm_vreg->resource_id,
520 (set == RPM_SET_ACTIVE ? "act" : "slp"), rc);
521
522 return rc;
523}
524
525#define RPM_VREG_AGGR_MIN(_idx, _param_aggr, _param_reg) \
526{ \
527 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
528 = min(_param_aggr[RPM_REGULATOR_PARAM_##_idx], \
529 _param_reg[RPM_REGULATOR_PARAM_##_idx]); \
530}
531
532#define RPM_VREG_AGGR_MAX(_idx, _param_aggr, _param_reg) \
533{ \
534 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
535 = max(_param_aggr[RPM_REGULATOR_PARAM_##_idx], \
536 _param_reg[RPM_REGULATOR_PARAM_##_idx]); \
537}
538
539#define RPM_VREG_AGGR_SUM(_idx, _param_aggr, _param_reg) \
540{ \
541 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
542 += _param_reg[RPM_REGULATOR_PARAM_##_idx]; \
543}
544
545#define RPM_VREG_AGGR_OR(_idx, _param_aggr, _param_reg) \
546{ \
547 _param_aggr[RPM_REGULATOR_PARAM_##_idx] \
548 |= _param_reg[RPM_REGULATOR_PARAM_##_idx]; \
549}
550
551/*
552 * Aggregation is performed on each parameter based on the way that the RPM
553 * aggregates that type internally between RPM masters.
554 */
555static void rpm_vreg_aggregate_params(u32 *param_aggr, const u32 *param_reg)
556{
557 RPM_VREG_AGGR_MAX(ENABLE, param_aggr, param_reg);
558 RPM_VREG_AGGR_MAX(VOLTAGE, param_aggr, param_reg);
559 RPM_VREG_AGGR_SUM(CURRENT, param_aggr, param_reg);
560 RPM_VREG_AGGR_MAX(MODE_LDO, param_aggr, param_reg);
561 RPM_VREG_AGGR_MAX(MODE_SMPS, param_aggr, param_reg);
562 RPM_VREG_AGGR_OR(PIN_CTRL_ENABLE, param_aggr, param_reg);
563 RPM_VREG_AGGR_OR(PIN_CTRL_MODE, param_aggr, param_reg);
564 RPM_VREG_AGGR_MIN(FREQUENCY, param_aggr, param_reg);
565 RPM_VREG_AGGR_MAX(HEAD_ROOM, param_aggr, param_reg);
566 RPM_VREG_AGGR_MAX(QUIET_MODE, param_aggr, param_reg);
567 RPM_VREG_AGGR_MAX(FREQ_REASON, param_aggr, param_reg);
568 RPM_VREG_AGGR_MAX(CORNER, param_aggr, param_reg);
569 RPM_VREG_AGGR_MAX(BYPASS, param_aggr, param_reg);
570 RPM_VREG_AGGR_MAX(FLOOR_CORNER, param_aggr, param_reg);
571 RPM_VREG_AGGR_MAX(LEVEL, param_aggr, param_reg);
572 RPM_VREG_AGGR_MAX(FLOOR_LEVEL, param_aggr, param_reg);
573 RPM_VREG_AGGR_MAX(MODE_BOB, param_aggr, param_reg);
574 RPM_VREG_AGGR_MAX(PIN_CTRL_VOLTAGE1, param_aggr, param_reg);
575 RPM_VREG_AGGR_MAX(PIN_CTRL_VOLTAGE2, param_aggr, param_reg);
576 RPM_VREG_AGGR_MAX(PIN_CTRL_VOLTAGE3, param_aggr, param_reg);
577}
578
579static int rpm_vreg_aggregate_requests(struct rpm_regulator *regulator)
580{
581 struct rpm_vreg *rpm_vreg = regulator->rpm_vreg;
582 u32 param_active[RPM_REGULATOR_PARAM_MAX];
583 u32 param_sleep[RPM_REGULATOR_PARAM_MAX];
584 u32 modified_active, modified_sleep;
585 struct rpm_regulator *reg;
586 bool sleep_set_differs = false;
587 bool send_active = false;
588 bool send_sleep = false;
589 bool wait_for_ack;
590 int rc = 0;
591 int i;
592
593 memset(param_active, 0, sizeof(param_active));
594 memset(param_sleep, 0, sizeof(param_sleep));
595 modified_active = rpm_vreg->aggr_req_active.modified;
596 modified_sleep = rpm_vreg->aggr_req_sleep.modified;
597
598 /*
599 * Aggregate all of the requests for this regulator in both active
600 * and sleep sets.
601 */
602 list_for_each_entry(reg, &rpm_vreg->reg_list, list) {
603 if (reg->set_active) {
604 rpm_vreg_aggregate_params(param_active, reg->req.param);
605 modified_active |= reg->req.modified;
606 }
607 if (reg->set_sleep) {
608 rpm_vreg_aggregate_params(param_sleep, reg->req.param);
609 modified_sleep |= reg->req.modified;
610 }
611 }
612
613 /*
614 * Check if the aggregated sleep set parameter values differ from the
615 * aggregated active set parameter values.
616 */
617 if (!rpm_vreg->sleep_request_sent) {
618 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
619 if ((param_active[i] != param_sleep[i])
620 && (modified_sleep & BIT(i))) {
621 sleep_set_differs = true;
622 break;
623 }
624 }
625 }
626
627 /* Add KVPs to the active set RPM request if they have new values. */
628 rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_active.param,
629 param_active, rpm_vreg->aggr_req_active.valid,
630 &modified_active);
631 rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_ACTIVE,
632 param_active, modified_active);
633 if (rc)
634 return rc;
635 send_active = modified_active;
636
637 /*
638 * Sleep set configurations are only sent if they differ from the
639 * active set values. This is because the active set values will take
640 * effect during rpm assisted power collapse in the absence of sleep set
641 * values.
642 *
643 * However, once a sleep set request is sent for a given regulator,
644 * additional sleep set requests must be sent in the future even if they
645 * match the corresponding active set requests.
646 */
647 if (rpm_vreg->sleep_request_sent || sleep_set_differs) {
648 /* Add KVPs to the sleep set RPM request if they are new. */
649 rpm_vreg_check_modified_requests(rpm_vreg->aggr_req_sleep.param,
650 param_sleep, rpm_vreg->aggr_req_sleep.valid,
651 &modified_sleep);
652 rc = rpm_vreg_add_modified_requests(regulator, RPM_SET_SLEEP,
653 param_sleep, modified_sleep);
654 if (rc)
655 return rc;
656 send_sleep = modified_sleep;
657 }
658
659 /* Send active set request to the RPM if it contains new KVPs. */
660 if (send_active) {
661 wait_for_ack = rpm_vreg_ack_required(rpm_vreg, RPM_SET_ACTIVE,
662 rpm_vreg->aggr_req_active.param,
663 param_active,
664 rpm_vreg->aggr_req_active.valid,
665 modified_active);
666 rc = rpm_vreg_send_request(regulator, RPM_SET_ACTIVE,
667 wait_for_ack);
668 if (rc)
669 return rc;
670 rpm_vreg->aggr_req_active.valid |= modified_active;
671 rpm_vreg->wait_for_ack_active = false;
672 }
673 /* Store the results of the aggregation. */
674 rpm_vreg->aggr_req_active.modified = modified_active;
675 memcpy(rpm_vreg->aggr_req_active.param, param_active,
676 sizeof(param_active));
677
678 /* Handle debug printing of the active set request. */
679 rpm_regulator_req(regulator, RPM_SET_ACTIVE, send_active);
680 if (send_active)
681 rpm_vreg->aggr_req_active.modified = 0;
682
683 /* Send sleep set request to the RPM if it contains new KVPs. */
684 if (send_sleep) {
685 wait_for_ack = rpm_vreg_ack_required(rpm_vreg, RPM_SET_SLEEP,
686 rpm_vreg->aggr_req_sleep.param,
687 param_sleep,
688 rpm_vreg->aggr_req_sleep.valid,
689 modified_sleep);
690 rc = rpm_vreg_send_request(regulator, RPM_SET_SLEEP,
691 wait_for_ack);
692 if (rc)
693 return rc;
694 else
695 rpm_vreg->sleep_request_sent = true;
696 rpm_vreg->aggr_req_sleep.valid |= modified_sleep;
697 rpm_vreg->wait_for_ack_sleep = false;
698 }
699 /* Store the results of the aggregation. */
700 rpm_vreg->aggr_req_sleep.modified = modified_sleep;
701 memcpy(rpm_vreg->aggr_req_sleep.param, param_sleep,
702 sizeof(param_sleep));
703
704 /* Handle debug printing of the sleep set request. */
705 rpm_regulator_req(regulator, RPM_SET_SLEEP, send_sleep);
706 if (send_sleep)
707 rpm_vreg->aggr_req_sleep.modified = 0;
708
709 /*
710 * Loop over all requests for this regulator to update the valid and
711 * modified values for use in future aggregation.
712 */
713 list_for_each_entry(reg, &rpm_vreg->reg_list, list) {
714 reg->req.valid |= reg->req.modified;
715 reg->req.modified = 0;
716 }
717
718 return rc;
719}
720
721static int rpm_vreg_is_enabled(struct regulator_dev *rdev)
722{
723 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
724
725 if (likely(!reg->use_pin_ctrl_for_enable))
726 return reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
727 else
728 return reg->req.param[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE]
729 == reg->pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_ENABLE];
730}
731
732static int rpm_vreg_enable(struct regulator_dev *rdev)
733{
734 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
735 int rc;
736 u32 prev_enable;
737
738 rpm_vreg_lock(reg->rpm_vreg);
739
740 if (likely(!reg->use_pin_ctrl_for_enable)) {
741 /* Enable using swen KVP. */
742 prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
743 RPM_VREG_SET_PARAM(reg, ENABLE, 1);
744 rc = rpm_vreg_aggregate_requests(reg);
745 if (rc) {
746 vreg_err(reg, "enable failed, rc=%d", rc);
747 RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable);
748 }
749 } else {
750 /* Enable using pcen KVP. */
751 prev_enable
752 = reg->req.param[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE];
753 RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE,
754 reg->pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_ENABLE]);
755 rc = rpm_vreg_aggregate_requests(reg);
756 if (rc) {
757 vreg_err(reg, "enable failed, rc=%d", rc);
758 RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE, prev_enable);
759 }
760 }
761
762 rpm_vreg_unlock(reg->rpm_vreg);
763
764 return rc;
765}
766
767static int rpm_vreg_disable(struct regulator_dev *rdev)
768{
769 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
770 int rc;
771 u32 prev_enable;
772
773 rpm_vreg_lock(reg->rpm_vreg);
774
775 if (likely(!reg->use_pin_ctrl_for_enable)) {
776 /* Disable using swen KVP. */
777 prev_enable = reg->req.param[RPM_REGULATOR_PARAM_ENABLE];
778 RPM_VREG_SET_PARAM(reg, ENABLE, 0);
779 rc = rpm_vreg_aggregate_requests(reg);
780 if (rc) {
781 vreg_err(reg, "disable failed, rc=%d", rc);
782 RPM_VREG_SET_PARAM(reg, ENABLE, prev_enable);
783 }
784 } else {
785 /* Disable using pcen KVP. */
786 prev_enable
787 = reg->req.param[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE];
788 RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE,
789 reg->pin_ctrl_mask[RPM_VREG_PIN_CTRL_STATE_DISABLE]);
790 rc = rpm_vreg_aggregate_requests(reg);
791 if (rc) {
792 vreg_err(reg, "disable failed, rc=%d", rc);
793 RPM_VREG_SET_PARAM(reg, PIN_CTRL_ENABLE, prev_enable);
794 }
795 }
796
797 rpm_vreg_unlock(reg->rpm_vreg);
798
799 return rc;
800}
801
802#define RPM_VREG_SET_VOLTAGE(_regulator, _val) \
803{ \
804 (_regulator)->req.param[(_regulator)->voltage_index] = _val; \
805 (_regulator)->req.modified |= BIT((_regulator)->voltage_index); \
806} \
807
808static int rpm_vreg_set_voltage(struct regulator_dev *rdev, int min_uV,
809 int max_uV, unsigned *selector)
810{
811 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
812 int rc = 0;
813 int voltage;
814 u32 prev_voltage;
815
816 voltage = min_uV - reg->voltage_offset;
817
818 if (voltage < params[reg->voltage_index].min
819 || voltage > params[reg->voltage_index].max) {
820 vreg_err(reg, "voltage=%d for key=%s is not within allowed range: [%u, %u]\n",
821 voltage, params[reg->voltage_index].name,
822 params[reg->voltage_index].min,
823 params[reg->voltage_index].max);
824 return -EINVAL;
825 }
826
827 rpm_vreg_lock(reg->rpm_vreg);
828
829 prev_voltage = reg->req.param[reg->voltage_index];
830 RPM_VREG_SET_VOLTAGE(reg, voltage);
831
832 rpm_vreg_check_param_max(reg, reg->voltage_index,
833 max_uV - reg->voltage_offset);
834
835 /*
836 * Only send a new voltage if the regulator is currently enabled or
837 * if the regulator has been configured to always send voltage updates.
838 */
839 if (reg->always_send_voltage
840 || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg)
841 || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg))
842 rc = rpm_vreg_aggregate_requests(reg);
843
844 if (rc) {
845 vreg_err(reg, "set voltage for key=%s failed, rc=%d",
846 params[reg->voltage_index].name, rc);
847 RPM_VREG_SET_VOLTAGE(reg, prev_voltage);
848 }
849
850 rpm_vreg_unlock(reg->rpm_vreg);
851
852 return rc;
853}
854
855static int rpm_vreg_get_voltage(struct regulator_dev *rdev)
856{
857 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
858 int uV;
859
860 uV = reg->req.param[reg->voltage_index] + reg->voltage_offset;
861 if (uV == 0)
862 uV = VOLTAGE_UNKNOWN;
863
864 return uV;
865}
866
867static int rpm_vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
868{
869 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
870 int rc = 0;
871 u32 prev_current;
872 int prev_uA;
873
874 rpm_vreg_lock(reg->rpm_vreg);
875
876 prev_current = reg->req.param[RPM_REGULATOR_PARAM_CURRENT];
877 prev_uA = MILLI_TO_MICRO(prev_current);
878
879 if (mode == REGULATOR_MODE_NORMAL) {
880 /* Make sure that request current is in HPM range. */
881 if (prev_uA < rpm_vreg_hpm_min_uA(reg->rpm_vreg))
882 RPM_VREG_SET_PARAM(reg, CURRENT,
883 MICRO_TO_MILLI(rpm_vreg_hpm_min_uA(reg->rpm_vreg)));
884 } else if (REGULATOR_MODE_IDLE) {
885 /* Make sure that request current is in LPM range. */
886 if (prev_uA > rpm_vreg_lpm_max_uA(reg->rpm_vreg))
887 RPM_VREG_SET_PARAM(reg, CURRENT,
888 MICRO_TO_MILLI(rpm_vreg_lpm_max_uA(reg->rpm_vreg)));
889 } else {
890 vreg_err(reg, "invalid mode: %u\n", mode);
891 rpm_vreg_unlock(reg->rpm_vreg);
892 return -EINVAL;
893 }
894
895 /*
896 * Only send a new load current value if the regulator is currently
897 * enabled or if the regulator has been configured to always send
898 * current updates.
899 */
900 if (reg->always_send_current
901 || rpm_vreg_active_or_sleep_enabled(reg->rpm_vreg)
902 || rpm_vreg_shared_active_or_sleep_enabled_valid(reg->rpm_vreg))
903 rc = rpm_vreg_aggregate_requests(reg);
904
905 if (rc) {
906 vreg_err(reg, "set mode failed, rc=%d\n", rc);
907 RPM_VREG_SET_PARAM(reg, CURRENT, prev_current);
908 }
909
910 rpm_vreg_unlock(reg->rpm_vreg);
911
912 return rc;
913}
914
915static unsigned int rpm_vreg_get_mode(struct regulator_dev *rdev)
916{
917 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
918
919 return (reg->req.param[RPM_REGULATOR_PARAM_CURRENT]
920 >= MICRO_TO_MILLI(reg->rpm_vreg->hpm_min_load))
921 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
922}
923
924static unsigned int rpm_vreg_get_optimum_mode(struct regulator_dev *rdev,
925 int input_uV, int output_uV, int load_uA)
926{
927 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
928 u32 load_mA;
929
930 load_uA += reg->system_load;
931
932 load_mA = MICRO_TO_MILLI(load_uA);
933 if (load_mA > params[RPM_REGULATOR_PARAM_CURRENT].max)
934 load_mA = params[RPM_REGULATOR_PARAM_CURRENT].max;
935
Kiran Gundaf9d484ef2017-10-16 12:15:30 +0530936 return (load_uA >= reg->rpm_vreg->hpm_min_load)
937 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
938}
939
940static int rpm_vreg_set_bob_mode(struct regulator_dev *rdev, unsigned int mode)
941{
942 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
943 int rc;
944 u32 prev_mode;
945
946 rpm_vreg_lock(reg->rpm_vreg);
947
948 prev_mode = reg->req.param[RPM_REGULATOR_PARAM_MODE_BOB];
949
950 switch (mode) {
951 case REGULATOR_MODE_FAST:
952 RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_PWM);
953 break;
954 case REGULATOR_MODE_NORMAL:
955 RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_AUTO);
956 break;
957 case REGULATOR_MODE_IDLE:
958 RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_PFM);
959 break;
960 case REGULATOR_MODE_STANDBY:
961 RPM_VREG_SET_PARAM(reg, MODE_BOB, RPM_REGULATOR_BOB_MODE_PASS);
962 break;
963 default:
964 vreg_err(reg, "invalid mode: %u\n", mode);
965 rpm_vreg_unlock(reg->rpm_vreg);
966 return -EINVAL;
967 }
968
969 rc = rpm_vreg_aggregate_requests(reg);
970 if (rc) {
971 vreg_err(reg, "set BoB mode failed, rc=%d\n", rc);
972 RPM_VREG_SET_PARAM(reg, MODE_BOB, prev_mode);
973 }
974
975 rpm_vreg_unlock(reg->rpm_vreg);
976
977 return rc;
978}
979
980static unsigned int rpm_vreg_get_bob_mode(struct regulator_dev *rdev)
981{
982 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
983 unsigned int mode;
984
985 switch (reg->req.param[RPM_REGULATOR_PARAM_MODE_BOB]) {
986 case RPM_REGULATOR_BOB_MODE_PWM:
987 mode = REGULATOR_MODE_FAST;
988 break;
989 case RPM_REGULATOR_BOB_MODE_AUTO:
990 mode = REGULATOR_MODE_NORMAL;
991 break;
992 case RPM_REGULATOR_BOB_MODE_PFM:
993 mode = REGULATOR_MODE_IDLE;
994 break;
995 case RPM_REGULATOR_BOB_MODE_PASS:
996 mode = REGULATOR_MODE_STANDBY;
997 break;
998 default:
999 vreg_err(reg, "BoB mode unknown\n");
1000 mode = REGULATOR_MODE_NORMAL;
1001 }
1002
1003 return mode;
1004}
1005
1006static int rpm_vreg_enable_time(struct regulator_dev *rdev)
1007{
1008 struct rpm_regulator *reg = rdev_get_drvdata(rdev);
1009
1010 return reg->rpm_vreg->enable_time;
1011}
1012
1013static int rpm_vreg_send_defaults(struct rpm_regulator *reg)
1014{
1015 int rc;
1016
1017 rpm_vreg_lock(reg->rpm_vreg);
1018 rc = rpm_vreg_aggregate_requests(reg);
1019 if (rc)
1020 vreg_err(reg, "RPM request failed, rc=%d", rc);
1021 rpm_vreg_unlock(reg->rpm_vreg);
1022
1023 return rc;
1024}
1025
1026static int rpm_vreg_configure_pin_control_enable(struct rpm_regulator *reg,
1027 struct device_node *node)
1028{
1029 struct rpm_regulator_param *pcen_param =
1030 &params[RPM_REGULATOR_PARAM_PIN_CTRL_ENABLE];
1031 int rc, i;
1032
1033 if (!of_find_property(node, "qcom,enable-with-pin-ctrl", NULL))
1034 return 0;
1035
1036 if (pcen_param->supported_regulator_types
1037 & BIT(reg->rpm_vreg->regulator_type)) {
1038 rc = of_property_read_u32_array(node,
1039 "qcom,enable-with-pin-ctrl", reg->pin_ctrl_mask,
1040 RPM_VREG_PIN_CTRL_STATE_COUNT);
1041 if (rc) {
1042 vreg_err(reg, "could not read qcom,enable-with-pin-ctrl, rc=%d\n",
1043 rc);
1044 return rc;
1045 }
1046
1047 /* Verify that the mask values are valid. */
1048 for (i = 0; i < RPM_VREG_PIN_CTRL_STATE_COUNT; i++) {
1049 if (reg->pin_ctrl_mask[i] < pcen_param->min
1050 || reg->pin_ctrl_mask[i] > pcen_param->max) {
1051 vreg_err(reg, "device tree property: qcom,enable-with-pin-ctrl[%d]=%u is outside allowed range [%u, %u]\n",
1052 i, reg->pin_ctrl_mask[i],
1053 pcen_param->min, pcen_param->max);
1054 return -EINVAL;
1055 }
1056 }
1057
1058 reg->use_pin_ctrl_for_enable = true;
1059 } else {
1060 pr_warn("%s: regulator type=%d does not support device tree property: qcom,enable-with-pin-ctrl\n",
1061 reg->rdesc.name, reg->rpm_vreg->regulator_type);
1062 }
1063
1064 return 0;
1065}
1066
1067/**
1068 * rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
1069 * @dev: device for regulator consumer
1070 * @supply: supply name
1071 *
1072 * Returns a struct rpm_regulator corresponding to the regulator producer,
1073 * or ERR_PTR() containing errno.
1074 *
1075 * This function may only be called from nonatomic context.
1076 */
1077struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
1078{
1079 struct rpm_regulator *framework_reg;
1080 struct rpm_regulator *priv_reg = NULL;
1081 struct regulator *regulator;
1082 struct rpm_vreg *rpm_vreg;
1083
1084 regulator = regulator_get(dev, supply);
1085 if (IS_ERR(regulator)) {
1086 pr_err("could not find regulator for: dev=%s, supply=%s, rc=%ld\n",
1087 (dev ? dev_name(dev) : ""), (supply ? supply : ""),
1088 PTR_ERR(regulator));
1089 return ERR_CAST(regulator);
1090 }
1091
1092 framework_reg = regulator_get_drvdata(regulator);
1093 if (framework_reg == NULL) {
1094 pr_err("regulator structure not found.\n");
1095 regulator_put(regulator);
1096 return ERR_PTR(-ENODEV);
1097 }
1098 regulator_put(regulator);
1099
1100 rpm_vreg = framework_reg->rpm_vreg;
1101
1102 priv_reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
1103 if (priv_reg == NULL) {
1104 vreg_err(framework_reg,
1105 "could not allocate memory for regulator\n");
1106 return ERR_PTR(-ENOMEM);
1107 }
1108
1109 /*
1110 * Allocate a regulator_dev struct so that framework callback functions
1111 * can be called from the private API functions.
1112 */
1113 priv_reg->rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1114 if (priv_reg->rdev == NULL) {
1115 vreg_err(framework_reg,
1116 "could not allocate memory for regulator_dev\n");
1117 kfree(priv_reg);
1118 return ERR_PTR(-ENOMEM);
1119 }
1120 priv_reg->rdev->reg_data = priv_reg;
1121 priv_reg->rpm_vreg = rpm_vreg;
1122 priv_reg->rdesc.name = framework_reg->rdesc.name;
1123 priv_reg->rdesc.ops = framework_reg->rdesc.ops;
1124 priv_reg->set_active = framework_reg->set_active;
1125 priv_reg->set_sleep = framework_reg->set_sleep;
1126 priv_reg->min_uV = framework_reg->min_uV;
1127 priv_reg->max_uV = framework_reg->max_uV;
1128 priv_reg->system_load = framework_reg->system_load;
1129
1130 might_sleep_if(!rpm_vreg->allow_atomic);
1131 rpm_vreg_lock(rpm_vreg);
1132 list_add(&priv_reg->list, &rpm_vreg->reg_list);
1133 rpm_vreg_unlock(rpm_vreg);
1134
1135 return priv_reg;
1136}
1137EXPORT_SYMBOL(rpm_regulator_get);
1138
1139static int rpm_regulator_check_input(struct rpm_regulator *regulator)
1140{
1141 if (IS_ERR_OR_NULL(regulator) || regulator->rpm_vreg == NULL) {
1142 pr_err("invalid rpm_regulator pointer\n");
1143 return -EINVAL;
1144 }
1145
1146 might_sleep_if(!regulator->rpm_vreg->allow_atomic);
1147
1148 return 0;
1149}
1150
1151/**
1152 * rpm_regulator_put() - free the RPM regulator handle
1153 * @regulator: RPM regulator handle
1154 *
1155 * Parameter reaggregation does not take place when rpm_regulator_put is called.
1156 * Therefore, regulator enable state and voltage must be configured
1157 * appropriately before calling rpm_regulator_put.
1158 *
1159 * This function may be called from either atomic or nonatomic context. If this
1160 * function is called from atomic context, then the regulator being operated on
1161 * must be configured via device tree with qcom,allow-atomic == 1.
1162 */
1163void rpm_regulator_put(struct rpm_regulator *regulator)
1164{
1165 struct rpm_vreg *rpm_vreg;
1166 int rc = rpm_regulator_check_input(regulator);
1167
1168 if (rc)
1169 return;
1170
1171 rpm_vreg = regulator->rpm_vreg;
1172
1173 might_sleep_if(!rpm_vreg->allow_atomic);
1174 rpm_vreg_lock(rpm_vreg);
1175 list_del(&regulator->list);
1176 rpm_vreg_unlock(rpm_vreg);
1177
1178 kfree(regulator->rdev);
1179 kfree(regulator);
1180}
1181EXPORT_SYMBOL(rpm_regulator_put);
1182
1183/**
1184 * rpm_regulator_enable() - enable regulator output
1185 * @regulator: RPM regulator handle
1186 *
1187 * Returns 0 on success or errno on failure.
1188 *
1189 * This function may be called from either atomic or nonatomic context. If this
1190 * function is called from atomic context, then the regulator being operated on
1191 * must be configured via device tree with qcom,allow-atomic == 1.
1192 */
1193int rpm_regulator_enable(struct rpm_regulator *regulator)
1194{
1195 int rc = rpm_regulator_check_input(regulator);
1196
1197 if (rc)
1198 return rc;
1199
1200 return rpm_vreg_enable(regulator->rdev);
1201}
1202EXPORT_SYMBOL(rpm_regulator_enable);
1203
1204/**
1205 * rpm_regulator_disable() - disable regulator output
1206 * @regulator: RPM regulator handle
1207 *
1208 * Returns 0 on success or errno on failure.
1209 *
1210 * The enable state of the regulator is determined by aggregating the requests
1211 * of all consumers. Therefore, it is possible that the regulator will remain
1212 * enabled even after rpm_regulator_disable is called.
1213 *
1214 * This function may be called from either atomic or nonatomic context. If this
1215 * function is called from atomic context, then the regulator being operated on
1216 * must be configured via device tree with qcom,allow-atomic == 1.
1217 */
1218int rpm_regulator_disable(struct rpm_regulator *regulator)
1219{
1220 int rc = rpm_regulator_check_input(regulator);
1221
1222 if (rc)
1223 return rc;
1224
1225 return rpm_vreg_disable(regulator->rdev);
1226}
1227EXPORT_SYMBOL(rpm_regulator_disable);
1228
1229/**
1230 * rpm_regulator_set_voltage() - set regulator output voltage
1231 * @regulator: RPM regulator handle
1232 * @min_uV: minimum required voltage in uV
1233 * @max_uV: maximum acceptable voltage in uV
1234 *
1235 * Sets a voltage regulator to the desired output voltage. This can be set
1236 * while the regulator is disabled or enabled. If the regulator is enabled then
1237 * the voltage will change to the new value immediately; otherwise, if the
1238 * regulator is disabled, then the regulator will output at the new voltage when
1239 * enabled.
1240 *
1241 * The min_uV to max_uV voltage range requested must intersect with the
1242 * voltage constraint range configured for the regulator.
1243 *
1244 * Returns 0 on success or errno on failure.
1245 *
1246 * The final voltage value that is sent to the RPM is aggregated based upon the
1247 * values requested by all consumers of the regulator. This corresponds to the
1248 * maximum min_uV value.
1249 *
1250 * This function may be called from either atomic or nonatomic context. If this
1251 * function is called from atomic context, then the regulator being operated on
1252 * must be configured via device tree with qcom,allow-atomic == 1.
1253 */
1254int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
1255 int max_uV)
1256{
1257 int rc = rpm_regulator_check_input(regulator);
1258 int uV = min_uV;
1259
1260 if (rc)
1261 return rc;
1262
1263 if (regulator->rpm_vreg->regulator_type == RPM_REGULATOR_TYPE_VS) {
1264 vreg_err(regulator, "unsupported regulator type: %d\n",
1265 regulator->rpm_vreg->regulator_type);
1266 return -EINVAL;
1267 }
1268
1269 if (min_uV > max_uV) {
1270 vreg_err(regulator, "min_uV=%d must be less than max_uV=%d\n",
1271 min_uV, max_uV);
1272 return -EINVAL;
1273 }
1274
1275 if (uV < regulator->min_uV && max_uV >= regulator->min_uV)
1276 uV = regulator->min_uV;
1277
1278 if (uV < regulator->min_uV || uV > regulator->max_uV) {
1279 vreg_err(regulator,
1280 "request v=[%d, %d] is outside allowed v=[%d, %d]\n",
1281 min_uV, max_uV, regulator->min_uV, regulator->max_uV);
1282 return -EINVAL;
1283 }
1284
1285 return regulator->rdesc.ops->set_voltage(regulator->rdev, uV, uV, NULL);
1286}
1287EXPORT_SYMBOL(rpm_regulator_set_voltage);
1288
1289/**
1290 * rpm_regulator_set_mode() - set regulator operating mode
1291 * @regulator: RPM regulator handle
1292 * @mode: operating mode requested for the regulator
1293 *
1294 * Requests that the mode of the regulator be set to the mode specified. This
1295 * parameter is aggregated using a max function such that AUTO < IPEAK < HPM.
1296 *
1297 * Returns 0 on success or errno on failure.
1298 */
1299int rpm_regulator_set_mode(struct rpm_regulator *regulator,
1300 enum rpm_regulator_mode mode)
1301{
1302 int index = 0;
1303 u32 new_mode, prev_mode;
1304 int rc;
1305
1306 rc = rpm_regulator_check_input(regulator);
1307 if (rc)
1308 return rc;
1309
1310 if (mode < 0 || mode >= ARRAY_SIZE(mode_mapping)) {
1311 vreg_err(regulator, "invalid mode requested: %d\n", mode);
1312 return -EINVAL;
1313 }
1314
1315 switch (regulator->rpm_vreg->regulator_type) {
1316 case RPM_REGULATOR_TYPE_SMPS:
1317 index = RPM_REGULATOR_PARAM_MODE_SMPS;
1318 new_mode = mode_mapping[mode].smps_mode;
1319 break;
1320 case RPM_REGULATOR_TYPE_LDO:
1321 index = RPM_REGULATOR_PARAM_MODE_LDO;
1322 new_mode = mode_mapping[mode].ldo_mode;
1323 break;
1324 default:
1325 vreg_err(regulator, "unsupported regulator type: %d\n",
1326 regulator->rpm_vreg->regulator_type);
1327 return -EINVAL;
1328 };
1329
1330 if (new_mode < params[index].min || new_mode > params[index].max) {
1331 vreg_err(regulator, "invalid mode requested: %d for type: %d\n",
1332 mode, regulator->rpm_vreg->regulator_type);
1333 return -EINVAL;
1334 }
1335
1336 rpm_vreg_lock(regulator->rpm_vreg);
1337
1338 prev_mode = regulator->req.param[index];
1339 regulator->req.param[index] = new_mode;
1340 regulator->req.modified |= BIT(index);
1341
1342 rc = rpm_vreg_aggregate_requests(regulator);
1343 if (rc) {
1344 vreg_err(regulator, "set mode failed, rc=%d", rc);
1345 regulator->req.param[index] = prev_mode;
1346 }
1347
1348 rpm_vreg_unlock(regulator->rpm_vreg);
1349
1350 return rc;
1351}
1352EXPORT_SYMBOL(rpm_regulator_set_mode);
1353
1354static struct regulator_ops ldo_ops = {
1355 .enable = rpm_vreg_enable,
1356 .disable = rpm_vreg_disable,
1357 .is_enabled = rpm_vreg_is_enabled,
1358 .set_voltage = rpm_vreg_set_voltage,
1359 .get_voltage = rpm_vreg_get_voltage,
1360 .set_mode = rpm_vreg_set_mode,
1361 .get_mode = rpm_vreg_get_mode,
1362 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1363 .enable_time = rpm_vreg_enable_time,
1364};
1365
1366static struct regulator_ops smps_ops = {
1367 .enable = rpm_vreg_enable,
1368 .disable = rpm_vreg_disable,
1369 .is_enabled = rpm_vreg_is_enabled,
1370 .set_voltage = rpm_vreg_set_voltage,
1371 .get_voltage = rpm_vreg_get_voltage,
1372 .set_mode = rpm_vreg_set_mode,
1373 .get_mode = rpm_vreg_get_mode,
1374 .get_optimum_mode = rpm_vreg_get_optimum_mode,
1375 .enable_time = rpm_vreg_enable_time,
1376};
1377
1378static struct regulator_ops switch_ops = {
1379 .enable = rpm_vreg_enable,
1380 .disable = rpm_vreg_disable,
1381 .is_enabled = rpm_vreg_is_enabled,
1382 .enable_time = rpm_vreg_enable_time,
1383};
1384
1385static struct regulator_ops ncp_ops = {
1386 .enable = rpm_vreg_enable,
1387 .disable = rpm_vreg_disable,
1388 .is_enabled = rpm_vreg_is_enabled,
1389 .set_voltage = rpm_vreg_set_voltage,
1390 .get_voltage = rpm_vreg_get_voltage,
1391 .enable_time = rpm_vreg_enable_time,
1392};
1393
1394static struct regulator_ops bob_ops = {
1395 .enable = rpm_vreg_enable,
1396 .disable = rpm_vreg_disable,
1397 .is_enabled = rpm_vreg_is_enabled,
1398 .set_voltage = rpm_vreg_set_voltage,
1399 .get_voltage = rpm_vreg_get_voltage,
1400 .set_mode = rpm_vreg_set_bob_mode,
1401 .get_mode = rpm_vreg_get_bob_mode,
1402 .enable_time = rpm_vreg_enable_time,
1403};
1404
1405static struct regulator_ops *vreg_ops[] = {
1406 [RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
1407 [RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
1408 [RPM_REGULATOR_TYPE_VS] = &switch_ops,
1409 [RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
1410 [RPM_REGULATOR_TYPE_BOB] = &bob_ops,
1411};
1412
1413static int rpm_vreg_device_remove(struct platform_device *pdev)
1414{
1415 struct device *dev = &pdev->dev;
1416 struct rpm_regulator *reg;
1417 struct rpm_vreg *rpm_vreg;
1418
1419 reg = platform_get_drvdata(pdev);
1420 if (reg) {
1421 rpm_vreg = reg->rpm_vreg;
1422 rpm_vreg_lock(rpm_vreg);
1423 regulator_unregister(reg->rdev);
1424 list_del(&reg->list);
1425 kfree(reg);
1426 rpm_vreg_unlock(rpm_vreg);
1427 } else {
1428 dev_err(dev, "%s: drvdata missing\n", __func__);
1429 return -EINVAL;
1430 }
1431
1432 platform_set_drvdata(pdev, NULL);
1433
1434 return 0;
1435}
1436
1437static int rpm_vreg_resource_remove(struct platform_device *pdev)
1438{
1439 struct device *dev = &pdev->dev;
1440 struct rpm_regulator *reg, *reg_temp;
1441 struct rpm_vreg *rpm_vreg;
1442
1443 rpm_vreg = platform_get_drvdata(pdev);
1444 if (rpm_vreg) {
1445 rpm_vreg_lock(rpm_vreg);
1446 list_for_each_entry_safe(reg, reg_temp, &rpm_vreg->reg_list,
1447 list) {
1448 /* Only touch data for private consumers. */
1449 if (reg->rdev->desc == NULL) {
1450 list_del(&reg->list);
1451 kfree(reg->rdev);
1452 kfree(reg);
1453 } else {
1454 dev_err(dev, "%s: not all child devices have been removed\n",
1455 __func__);
1456 }
1457 }
1458 rpm_vreg_unlock(rpm_vreg);
1459
1460 msm_rpm_free_request(rpm_vreg->handle_active);
1461 msm_rpm_free_request(rpm_vreg->handle_sleep);
1462
1463 kfree(rpm_vreg);
1464 } else {
1465 dev_err(dev, "%s: drvdata missing\n", __func__);
1466 return -EINVAL;
1467 }
1468
1469 platform_set_drvdata(pdev, NULL);
1470
1471 return 0;
1472}
1473
1474static int rpm_vreg_set_smps_ldo_voltage_index(struct device *dev,
1475 struct rpm_regulator *reg)
1476{
1477 struct device_node *node = dev->of_node;
1478 int chosen = 0;
1479
1480 if (of_property_read_bool(node, "qcom,use-voltage-corner")) {
1481 reg->voltage_index = RPM_REGULATOR_PARAM_CORNER;
1482 reg->voltage_offset = RPM_REGULATOR_CORNER_NONE;
1483 chosen++;
1484 }
1485
1486 if (of_property_read_bool(node, "qcom,use-voltage-floor-corner")) {
1487 reg->voltage_index = RPM_REGULATOR_PARAM_FLOOR_CORNER;
1488 reg->voltage_offset = RPM_REGULATOR_CORNER_NONE;
1489 chosen++;
1490 }
1491
1492 if (of_property_read_bool(node, "qcom,use-voltage-level")) {
1493 reg->voltage_index = RPM_REGULATOR_PARAM_LEVEL;
1494 chosen++;
1495 }
1496
1497 if (of_property_read_bool(node, "qcom,use-voltage-floor-level")) {
1498 reg->voltage_index = RPM_REGULATOR_PARAM_FLOOR_LEVEL;
1499 chosen++;
1500 }
1501
1502 if (chosen > 1) {
1503 dev_err(dev, "only one qcom,use-voltage-* may be specified\n");
1504 return -EINVAL;
1505 }
1506
1507 return 0;
1508}
1509
1510static int rpm_vreg_set_bob_voltage_index(struct device *dev,
1511 struct rpm_regulator *reg)
1512{
1513 struct device_node *node = dev->of_node;
1514 int chosen = 0;
1515
1516 if (of_property_read_bool(node, "qcom,use-pin-ctrl-voltage1")) {
1517 reg->voltage_index = RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE1;
1518 chosen++;
1519 }
1520
1521 if (of_property_read_bool(node, "qcom,use-pin-ctrl-voltage2")) {
1522 reg->voltage_index = RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE2;
1523 chosen++;
1524 }
1525
1526 if (of_property_read_bool(node, "qcom,use-pin-ctrl-voltage3")) {
1527 reg->voltage_index = RPM_REGULATOR_PARAM_PIN_CTRL_VOLTAGE3;
1528 chosen++;
1529 }
1530
1531 if (chosen > 1) {
1532 dev_err(dev, "only one qcom,use-pin-ctrl-voltage* may be specified\n");
1533 return -EINVAL;
1534 }
1535
1536 return 0;
1537}
1538
1539static int rpm_vreg_device_set_voltage_index(struct device *dev,
1540 struct rpm_regulator *reg, int type)
1541{
1542 int rc = 0;
1543
1544 reg->voltage_index = RPM_REGULATOR_PARAM_VOLTAGE;
1545
1546 switch (type) {
1547 case RPM_REGULATOR_TYPE_SMPS:
1548 case RPM_REGULATOR_TYPE_LDO:
1549 rc = rpm_vreg_set_smps_ldo_voltage_index(dev, reg);
1550 break;
1551 case RPM_REGULATOR_TYPE_BOB:
1552 rc = rpm_vreg_set_bob_voltage_index(dev, reg);
1553 break;
1554 }
1555
1556 return rc;
1557}
1558
1559/*
1560 * This probe is called for child rpm-regulator devices which have
1561 * properties which are required to configure individual regulator
1562 * framework regulators for a given RPM regulator resource.
1563 */
1564static int rpm_vreg_device_probe(struct platform_device *pdev)
1565{
1566 struct device *dev = &pdev->dev;
1567 struct device_node *node = dev->of_node;
1568 struct regulator_init_data *init_data;
1569 struct rpm_vreg *rpm_vreg;
1570 struct rpm_regulator *reg;
1571 struct regulator_config reg_config = {};
1572 int rc = 0;
1573 int i, regulator_type;
1574 u32 val;
1575
1576 if (!dev->of_node) {
1577 dev_err(dev, "%s: device tree information missing\n", __func__);
1578 return -ENODEV;
1579 }
1580
1581 if (pdev->dev.parent == NULL) {
1582 dev_err(dev, "%s: parent device missing\n", __func__);
1583 return -ENODEV;
1584 }
1585
1586 rpm_vreg = dev_get_drvdata(pdev->dev.parent);
1587 if (rpm_vreg == NULL) {
1588 dev_err(dev, "%s: rpm_vreg not found in parent device\n",
1589 __func__);
1590 return -ENODEV;
1591 }
1592
1593 reg = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
1594 if (reg == NULL) {
1595 dev_err(dev, "%s: could not allocate memory for reg\n",
1596 __func__);
1597 return -ENOMEM;
1598 }
1599
1600 regulator_type = rpm_vreg->regulator_type;
1601 reg->rpm_vreg = rpm_vreg;
1602 reg->rdesc.owner = THIS_MODULE;
1603 reg->rdesc.type = REGULATOR_VOLTAGE;
1604 reg->rdesc.ops = vreg_ops[regulator_type];
1605
1606 rc = rpm_vreg_device_set_voltage_index(dev, reg, regulator_type);
1607 if (rc)
1608 goto fail_free_reg;
1609
1610 reg->always_send_voltage
1611 = of_property_read_bool(node, "qcom,always-send-voltage");
1612 reg->always_send_current
1613 = of_property_read_bool(node, "qcom,always-send-current");
1614
1615 if (regulator_type == RPM_REGULATOR_TYPE_VS)
1616 reg->rdesc.n_voltages = 0;
1617 else
1618 reg->rdesc.n_voltages = 2;
1619
1620 rc = of_property_read_u32(node, "qcom,set", &val);
1621 if (rc) {
1622 dev_err(dev, "%s: sleep set and/or active set must be configured via qcom,set property, rc=%d\n",
1623 __func__, rc);
1624 goto fail_free_reg;
1625 } else if (!(val & RPM_SET_CONFIG_BOTH)) {
1626 dev_err(dev, "%s: qcom,set=%u property is invalid\n", __func__,
1627 val);
1628 rc = -EINVAL;
1629 goto fail_free_reg;
1630 }
1631
1632 reg->set_active = !!(val & RPM_SET_CONFIG_ACTIVE);
1633 reg->set_sleep = !!(val & RPM_SET_CONFIG_SLEEP);
1634
1635 init_data = of_get_regulator_init_data(dev, node, &reg->rdesc);
1636 if (init_data == NULL) {
1637 dev_err(dev, "%s: unable to allocate memory\n", __func__);
1638 rc = -ENOMEM;
1639 goto fail_free_reg;
1640 }
1641 if (init_data->constraints.name == NULL) {
1642 dev_err(dev, "%s: regulator name not specified\n", __func__);
1643 rc = -EINVAL;
1644 goto fail_free_reg;
1645 }
1646
1647 init_data->constraints.input_uV = init_data->constraints.max_uV;
1648
1649 if (of_get_property(node, "parent-supply", NULL))
1650 init_data->supply_regulator = "parent";
1651
1652 /*
1653 * Fill in ops and mode masks based on callbacks specified for
1654 * this type of regulator.
1655 */
1656 if (reg->rdesc.ops->enable)
1657 init_data->constraints.valid_ops_mask
1658 |= REGULATOR_CHANGE_STATUS;
1659 if (reg->rdesc.ops->get_voltage)
1660 init_data->constraints.valid_ops_mask
1661 |= REGULATOR_CHANGE_VOLTAGE;
1662 if (reg->rdesc.ops->get_mode) {
1663 init_data->constraints.valid_ops_mask
1664 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1665 init_data->constraints.valid_modes_mask
1666 |= REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1667 }
1668
1669 reg->rdesc.name = init_data->constraints.name;
1670 reg->min_uV = init_data->constraints.min_uV;
1671 reg->max_uV = init_data->constraints.max_uV;
1672
1673 /* Initialize the param array based on optional properties. */
1674 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++) {
1675 rc = of_property_read_u32(node, params[i].property_name, &val);
1676 if (rc == 0) {
1677 if (params[i].supported_regulator_types
1678 & BIT(regulator_type)) {
1679 if (val < params[i].min
1680 || val > params[i].max) {
1681 pr_warn("%s: device tree property: %s=%u is outsided allowed range [%u, %u]\n",
1682 reg->rdesc.name,
1683 params[i].property_name, val,
1684 params[i].min, params[i].max);
1685 continue;
1686 }
1687 reg->req.param[i] = val;
1688 reg->req.modified |= BIT(i);
1689 } else {
1690 pr_warn("%s: regulator type=%d does not support device tree property: %s\n",
1691 reg->rdesc.name, regulator_type,
1692 params[i].property_name);
1693 }
1694 }
1695 }
1696
1697 of_property_read_u32(node, "qcom,system-load", &reg->system_load);
1698
1699 rc = rpm_vreg_configure_pin_control_enable(reg, node);
1700 if (rc) {
1701 vreg_err(reg, "could not configure pin control enable, rc=%d\n",
1702 rc);
1703 goto fail_free_reg;
1704 }
1705
1706 rpm_vreg_lock(rpm_vreg);
1707 list_add(&reg->list, &rpm_vreg->reg_list);
1708 rpm_vreg_unlock(rpm_vreg);
1709
1710 if (of_property_read_bool(node, "qcom,send-defaults")) {
1711 rc = rpm_vreg_send_defaults(reg);
1712 if (rc) {
1713 vreg_err(reg, "could not send defaults, rc=%d\n", rc);
1714 goto fail_remove_from_list;
1715 }
1716 }
1717
1718 reg_config.dev = dev;
1719 reg_config.init_data = init_data;
1720 reg_config.of_node = node;
1721 reg_config.driver_data = reg;
1722 reg->rdev = regulator_register(&reg->rdesc, &reg_config);
1723 if (IS_ERR(reg->rdev)) {
1724 rc = PTR_ERR(reg->rdev);
1725 reg->rdev = NULL;
1726 pr_err("regulator_register failed: %s, rc=%d\n",
1727 reg->rdesc.name, rc);
1728 goto fail_remove_from_list;
1729 }
1730
1731 platform_set_drvdata(pdev, reg);
1732
1733 pr_debug("successfully probed: %s\n", reg->rdesc.name);
1734
1735 return 0;
1736
1737fail_remove_from_list:
1738 rpm_vreg_lock(rpm_vreg);
1739 list_del(&reg->list);
1740 rpm_vreg_unlock(rpm_vreg);
1741
1742fail_free_reg:
1743 kfree(reg);
1744 return rc;
1745}
1746
1747/*
1748 * This probe is called for parent rpm-regulator devices which have
1749 * properties which are required to identify a given RPM resource.
1750 */
1751static int rpm_vreg_resource_probe(struct platform_device *pdev)
1752{
1753 struct device *dev = &pdev->dev;
1754 struct device_node *node = dev->of_node;
1755 struct rpm_vreg *rpm_vreg;
1756 int val = 0;
1757 u32 resource_type;
1758 int rc;
1759
1760 if (!dev->of_node) {
1761 dev_err(dev, "%s: device tree information missing\n", __func__);
1762 return -ENODEV;
1763 }
1764
1765 /* Create new rpm_vreg entry. */
1766 rpm_vreg = kzalloc(sizeof(struct rpm_vreg), GFP_KERNEL);
1767 if (rpm_vreg == NULL) {
1768 dev_err(dev, "%s: could not allocate memory for vreg\n",
1769 __func__);
1770 return -ENOMEM;
1771 }
1772
1773 /* Required device tree properties: */
1774 rc = of_property_read_string(node, "qcom,resource-name",
1775 &rpm_vreg->resource_name);
1776 if (rc) {
1777 dev_err(dev, "%s: qcom,resource-name missing in DT node\n",
1778 __func__);
1779 goto fail_free_vreg;
1780 }
1781 resource_type = rpm_vreg_string_to_int(rpm_vreg->resource_name);
1782
1783 rc = of_property_read_u32(node, "qcom,resource-id",
1784 &rpm_vreg->resource_id);
1785 if (rc) {
1786 dev_err(dev, "%s: qcom,resource-id missing in DT node\n",
1787 __func__);
1788 goto fail_free_vreg;
1789 }
1790
1791 rc = of_property_read_u32(node, "qcom,regulator-type",
1792 &rpm_vreg->regulator_type);
1793 if (rc) {
1794 dev_err(dev, "%s: qcom,regulator-type missing in DT node\n",
1795 __func__);
1796 goto fail_free_vreg;
1797 }
1798
1799 if ((rpm_vreg->regulator_type < 0)
1800 || (rpm_vreg->regulator_type >= RPM_REGULATOR_TYPE_MAX)) {
1801 dev_err(dev, "%s: invalid regulator type: %d\n", __func__,
1802 rpm_vreg->regulator_type);
1803 rc = -EINVAL;
1804 goto fail_free_vreg;
1805 }
1806
1807 /* Optional device tree properties: */
1808 of_property_read_u32(node, "qcom,allow-atomic", &val);
1809 rpm_vreg->allow_atomic = !!val;
1810 of_property_read_u32(node, "qcom,enable-time", &rpm_vreg->enable_time);
1811 of_property_read_u32(node, "qcom,hpm-min-load",
1812 &rpm_vreg->hpm_min_load);
1813 rpm_vreg->apps_only = of_property_read_bool(node, "qcom,apps-only");
1814 rpm_vreg->always_wait_for_ack
1815 = of_property_read_bool(node, "qcom,always-wait-for-ack");
1816
1817 rpm_vreg->handle_active = msm_rpm_create_request(RPM_SET_ACTIVE,
1818 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1819 if (rpm_vreg->handle_active == NULL
1820 || IS_ERR(rpm_vreg->handle_active)) {
1821 rc = PTR_ERR(rpm_vreg->handle_active);
1822 if (rc != -EPROBE_DEFER)
1823 dev_err(dev, "%s: failed to create active RPM handle, rc=%d\n",
1824 __func__, rc);
1825 goto fail_free_vreg;
1826 }
1827
1828 rpm_vreg->handle_sleep = msm_rpm_create_request(RPM_SET_SLEEP,
1829 resource_type, rpm_vreg->resource_id, RPM_REGULATOR_PARAM_MAX);
1830 if (rpm_vreg->handle_sleep == NULL || IS_ERR(rpm_vreg->handle_sleep)) {
1831 rc = PTR_ERR(rpm_vreg->handle_sleep);
1832 if (rc != -EPROBE_DEFER)
1833 dev_err(dev, "%s: failed to create sleep RPM handle, rc=%d\n",
1834 __func__, rc);
1835 goto fail_free_handle_active;
1836 }
1837
1838 INIT_LIST_HEAD(&rpm_vreg->reg_list);
1839
1840 if (rpm_vreg->allow_atomic)
1841 spin_lock_init(&rpm_vreg->slock);
1842 else
1843 mutex_init(&rpm_vreg->mlock);
1844
1845 platform_set_drvdata(pdev, rpm_vreg);
1846
1847 rc = of_platform_populate(node, NULL, NULL, dev);
1848 if (rc) {
1849 dev_err(dev, "%s: failed to add child nodes, rc=%d\n", __func__,
1850 rc);
1851 goto fail_unset_drvdata;
1852 }
1853
1854 pr_debug("successfully probed: %s (%08X) %u\n", rpm_vreg->resource_name,
1855 resource_type, rpm_vreg->resource_id);
1856
1857 return rc;
1858
1859fail_unset_drvdata:
1860 platform_set_drvdata(pdev, NULL);
1861 msm_rpm_free_request(rpm_vreg->handle_sleep);
1862
1863fail_free_handle_active:
1864 msm_rpm_free_request(rpm_vreg->handle_active);
1865
1866fail_free_vreg:
1867 kfree(rpm_vreg);
1868
1869 return rc;
1870}
1871
1872static struct of_device_id rpm_vreg_match_table_device[] = {
1873 { .compatible = "qcom,rpm-smd-regulator", },
1874 {}
1875};
1876
1877static struct of_device_id rpm_vreg_match_table_resource[] = {
1878 { .compatible = "qcom,rpm-smd-regulator-resource", },
1879 {}
1880};
1881
1882static struct platform_driver rpm_vreg_device_driver = {
1883 .probe = rpm_vreg_device_probe,
1884 .remove = rpm_vreg_device_remove,
1885 .driver = {
1886 .name = "qcom,rpm-smd-regulator",
1887 .owner = THIS_MODULE,
1888 .of_match_table = rpm_vreg_match_table_device,
1889 },
1890};
1891
1892static struct platform_driver rpm_vreg_resource_driver = {
1893 .probe = rpm_vreg_resource_probe,
1894 .remove = rpm_vreg_resource_remove,
1895 .driver = {
1896 .name = "qcom,rpm-smd-regulator-resource",
1897 .owner = THIS_MODULE,
1898 .of_match_table = rpm_vreg_match_table_resource,
1899 },
1900};
1901
1902/**
1903 * rpm_smd_regulator_driver_init() - initialize the RPM SMD regulator drivers
1904 *
1905 * This function registers the RPM SMD regulator platform drivers.
1906 *
1907 * Returns 0 on success or errno on failure.
1908 */
1909int __init rpm_smd_regulator_driver_init(void)
1910{
1911 static bool initialized;
1912 int i, rc;
1913
1914 if (initialized)
1915 return 0;
1916 else
1917 initialized = true;
1918
1919 /* Store parameter string names as integers */
1920 for (i = 0; i < RPM_REGULATOR_PARAM_MAX; i++)
1921 params[i].key = rpm_vreg_string_to_int(params[i].name);
1922
1923 rc = platform_driver_register(&rpm_vreg_device_driver);
1924 if (rc)
1925 return rc;
1926
1927 return platform_driver_register(&rpm_vreg_resource_driver);
1928}
1929EXPORT_SYMBOL(rpm_smd_regulator_driver_init);
1930
1931static void __exit rpm_vreg_exit(void)
1932{
1933 platform_driver_unregister(&rpm_vreg_device_driver);
1934 platform_driver_unregister(&rpm_vreg_resource_driver);
1935}
1936
1937arch_initcall(rpm_smd_regulator_driver_init);
1938module_exit(rpm_vreg_exit);
1939
1940MODULE_LICENSE("GPL v2");
1941MODULE_DESCRIPTION("MSM RPM SMD regulator driver");