blob: f370d2b44b0b8f9e65f07b134b305c7036500e0a [file] [log] [blame]
David Collinsd86a1722017-02-07 15:42:22 -08001/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
David Collinse1b43192016-10-12 17:27:22 -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/bitops.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/string.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/machine.h>
26#include <linux/regulator/of_regulator.h>
27#include <soc/qcom/cmd-db.h>
28#include <soc/qcom/rpmh.h>
29#include <dt-bindings/regulator/qcom,rpmh-regulator.h>
30
31/**
32 * enum rpmh_regulator_type - supported RPMh accelerator types
33 * %RPMH_REGULATOR_TYPE_VRM: RPMh VRM accelerator which supports voting on
34 * enable, voltage, mode, and headroom voltage of
35 * LDO, SMPS, VS, and BOB type PMIC regulators.
36 * %RPMH_REGULATOR_TYPE_ARC: RPMh ARC accelerator which supports voting on
37 * the CPR managed voltage level of LDO and SMPS
38 * type PMIC regulators.
David Collins3bd8e102017-10-30 16:37:55 -070039 * %RPMH_REGULATOR_TYPE_XOB: RPMh XOB accelerator which supports voting on
40 * the enable state of PMIC regulators.
David Collinse1b43192016-10-12 17:27:22 -070041 */
42enum rpmh_regulator_type {
43 RPMH_REGULATOR_TYPE_VRM,
44 RPMH_REGULATOR_TYPE_ARC,
David Collins3bd8e102017-10-30 16:37:55 -070045 RPMH_REGULATOR_TYPE_XOB,
David Collinse1b43192016-10-12 17:27:22 -070046};
47
48/**
David Collinsf676d3d2017-12-12 18:23:33 -080049 * enum rpmh_regulator_hw_type - supported PMIC regulator hardware types
50 * This enum defines the specific regulator type along with its PMIC family.
51 */
52enum rpmh_regulator_hw_type {
53 RPMH_REGULATOR_HW_TYPE_UNKNOWN,
54 RPMH_REGULATOR_HW_TYPE_PMIC4_LDO,
55 RPMH_REGULATOR_HW_TYPE_PMIC4_HFSMPS,
56 RPMH_REGULATOR_HW_TYPE_PMIC4_FTSMPS,
57 RPMH_REGULATOR_HW_TYPE_PMIC4_BOB,
58 RPMH_REGULATOR_HW_TYPE_PMIC5_LDO,
59 RPMH_REGULATOR_HW_TYPE_PMIC5_HFSMPS,
60 RPMH_REGULATOR_HW_TYPE_PMIC5_FTSMPS,
61 RPMH_REGULATOR_HW_TYPE_PMIC5_BOB,
62 RPMH_REGULATOR_HW_TYPE_MAX,
63};
64
65/**
David Collinse1b43192016-10-12 17:27:22 -070066 * enum rpmh_regulator_reg_index - RPMh accelerator register indices
67 * %RPMH_REGULATOR_REG_VRM_VOLTAGE: VRM voltage voting register index
68 * %RPMH_REGULATOR_REG_ARC_LEVEL: ARC voltage level voting register index
69 * %RPMH_REGULATOR_REG_VRM_ENABLE: VRM enable voltage voting register index
70 * %RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE: Place-holder for enable aggregation.
71 * ARC does not have a specific register
72 * for enable voting. Instead, ARC level
73 * 0 corresponds to "disabled" for a given
74 * ARC regulator resource if supported.
David Collins3bd8e102017-10-30 16:37:55 -070075 * %RPMH_REGULATOR_REG_XOB_ENABLE: XOB enable voting register index
David Collinse1b43192016-10-12 17:27:22 -070076 * %RPMH_REGULATOR_REG_ENABLE: Common enable index used in callback
77 * functions for both ARC and VRM.
78 * %RPMH_REGULATOR_REG_VRM_MODE: VRM regulator mode voting register index
79 * %RPMH_REGULATOR_REG_VRM_HEADROOM: VRM headroom voltage voting register
80 * index
81 * %RPMH_REGULATOR_REG_ARC_REAL_MAX: Upper limit of real existent ARC
82 * register indices
83 * %RPMH_REGULATOR_REG_ARC_MAX: Exclusive upper limit of ARC register
84 * indices
David Collins3bd8e102017-10-30 16:37:55 -070085 * %RPMH_REGULATOR_REG_XOB_MAX: Exclusive upper limit of XOB register
86 * indices
David Collinse1b43192016-10-12 17:27:22 -070087 * %RPMH_REGULATOR_REG_VRM_MAX: Exclusive upper limit of VRM register
88 * indices
89 * %RPMH_REGULATOR_REG_MAX: Combined exclusive upper limit of ARC
90 * and VRM register indices
91 *
92 * Register addresses are calculated as: base_addr + sizeof(u32) * reg_index
93 */
94enum rpmh_regulator_reg_index {
95 RPMH_REGULATOR_REG_VRM_VOLTAGE = 0,
96 RPMH_REGULATOR_REG_ARC_LEVEL = 0,
97 RPMH_REGULATOR_REG_VRM_ENABLE = 1,
98 RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE = RPMH_REGULATOR_REG_VRM_ENABLE,
David Collins3bd8e102017-10-30 16:37:55 -070099 RPMH_REGULATOR_REG_XOB_ENABLE = RPMH_REGULATOR_REG_VRM_ENABLE,
David Collinse1b43192016-10-12 17:27:22 -0700100 RPMH_REGULATOR_REG_ENABLE = RPMH_REGULATOR_REG_VRM_ENABLE,
101 RPMH_REGULATOR_REG_VRM_MODE = 2,
102 RPMH_REGULATOR_REG_VRM_HEADROOM = 3,
103 RPMH_REGULATOR_REG_ARC_REAL_MAX = 1,
104 RPMH_REGULATOR_REG_ARC_MAX = 2,
David Collins3bd8e102017-10-30 16:37:55 -0700105 RPMH_REGULATOR_REG_XOB_MAX = 2,
David Collinse1b43192016-10-12 17:27:22 -0700106 RPMH_REGULATOR_REG_VRM_MAX = 4,
107 RPMH_REGULATOR_REG_MAX = 4,
108};
109
110/*
111 * This is the number of bytes used for each command DB aux data entry of an
112 * ARC resource.
113 */
114#define RPMH_ARC_LEVEL_SIZE 2
115
116/*
117 * This is the maximum number of voltage levels that may be defined for an ARC
118 * resource.
119 */
120#define RPMH_ARC_MAX_LEVELS 16
121
122/* Min and max limits of VRM resource request parameters */
123#define RPMH_VRM_MIN_UV 0
124#define RPMH_VRM_MAX_UV 8191000
125
126#define RPMH_VRM_HEADROOM_MIN_UV 0
127#define RPMH_VRM_HEADROOM_MAX_UV 511000
128
129#define RPMH_VRM_MODE_MIN 0
130#define RPMH_VRM_MODE_MAX 7
131
David Collins3bd8e102017-10-30 16:37:55 -0700132/* XOB voting registers are found in the VRM hardware module */
133#define CMD_DB_HW_XOB CMD_DB_HW_VRM
134
David Collinse1b43192016-10-12 17:27:22 -0700135/**
136 * struct rpmh_regulator_request - rpmh request data
137 * @reg: Array of RPMh accelerator register values
138 * @valid: Bitmask identifying which of the register values
139 * are valid/initialized
140 */
141struct rpmh_regulator_request {
142 u32 reg[RPMH_REGULATOR_REG_MAX];
143 u32 valid;
144};
145
146/**
147 * struct rpmh_regulator_mode - RPMh VRM mode attributes
148 * @pmic_mode: Raw PMIC mode value written into VRM mode voting
149 * register (i.e. RPMH_REGULATOR_MODE_*)
150 * @framework_mode: Regulator framework mode value
151 * (i.e. REGULATOR_MODE_*)
152 * @min_load_ua: The minimum load current in microamps which
153 * would utilize this mode
154 *
155 * Software selects the lowest mode for which aggr_load_ua >= min_load_ua.
156 */
157struct rpmh_regulator_mode {
158 u32 pmic_mode;
159 u32 framework_mode;
160 int min_load_ua;
161};
162
163struct rpmh_vreg;
164
165/**
166 * struct rpmh_aggr_vreg - top level aggregated rpmh regulator resource data
167 * structure
168 * @dev: Device pointer to the rpmh aggregated regulator
169 * device
170 * @resource_name: Name of rpmh regulator resource which is mapped
171 * to an RPMh accelerator address via command DB.
172 * This name must match to one that is defined by
173 * the bootloader.
174 * @addr: Base address of the regulator resource within
175 * an RPMh accelerator
176 * @rpmh_client: Handle used for rpmh communications
177 * @lock: Mutex lock used for locking between regulators
178 * common to a single aggregated resource
179 * @regulator_type: RPMh accelerator type for this regulator
180 * resource
David Collinsf676d3d2017-12-12 18:23:33 -0800181 * @regulator_hw_type: The regulator hardware type (e.g. LDO or SMPS)
182 * along with PMIC family (i.e. PMIC4 or PMIC5)
David Collinse1b43192016-10-12 17:27:22 -0700183 * @level: Mapping from ARC resource specific voltage
184 * levels (0 to RPMH_ARC_MAX_LEVELS - 1) to common
185 * consumer voltage levels (i.e.
186 * RPMH_REGULATOR_LEVEL_*). These values are read
187 * out of the AUX data found in command DB for a
188 * given ARC resource. Note that the values in
189 * this array are equal to those read from AUX data
190 * + RPMH_REGULATOR_LEVEL_OFFSET.
191 * @level_count: The number of valid entries in the level array
192 * @always_wait_for_ack: Boolean flag indicating if a request must always
193 * wait for an ACK from RPMh before continuing even
194 * if it corresponds to a strictly lower power
195 * state (e.g. enabled --> disabled).
196 * @next_wait_for_ack: Boolean flag indicating that the next request
197 * sent must wait for an ACK. This is used to
198 * ensure that the driver waits for the voltage to
199 * slew down in the case that the requested max_uV
200 * value is lower than the last requested voltage.
201 * @sleep_request_sent: Boolean flag indicating that a sleep set request
202 * has been sent at some point due to it diverging
203 * from the active set request. After that point,
204 * the sleep set requests must always be sent for
205 * a given resource.
206 * @use_awake_state: Boolean flag indicating that active set requests
207 * should be made using the awake state instead of
208 * the active-only state. This should be used for
209 * RSC's which do not have an AMC.
210 * @vreg: Array of rpmh regulator structs representing the
211 * individual regulators sharing the aggregated
212 * regulator resource.
213 * @vreg_count: The number of entries in the vreg array.
214 * @mode: An array of modes supported by an RPMh VRM
215 * regulator resource.
216 * @mode_count: The number of entries in the mode array.
217 * @aggr_req_active: Aggregated active set RPMh accelerator register
218 * request
219 * @aggr_req_sleep: Aggregated sleep set RPMh accelerator register
220 * request
221 */
222struct rpmh_aggr_vreg {
223 struct device *dev;
224 const char *resource_name;
225 u32 addr;
226 struct rpmh_client *rpmh_client;
227 struct mutex lock;
228 enum rpmh_regulator_type regulator_type;
David Collinsf676d3d2017-12-12 18:23:33 -0800229 enum rpmh_regulator_hw_type regulator_hw_type;
David Collinse1b43192016-10-12 17:27:22 -0700230 u32 level[RPMH_ARC_MAX_LEVELS];
231 int level_count;
232 bool always_wait_for_ack;
233 bool next_wait_for_ack;
234 bool sleep_request_sent;
235 bool use_awake_state;
236 struct rpmh_vreg *vreg;
237 int vreg_count;
238 struct rpmh_regulator_mode *mode;
239 int mode_count;
240 struct rpmh_regulator_request aggr_req_active;
241 struct rpmh_regulator_request aggr_req_sleep;
242};
243
244/**
245 * struct rpmh_vreg - individual rpmh regulator data structure encapsulating a
246 * regulator framework regulator device and its corresponding
247 * rpmh request
248 * @of_node: Device node pointer for the individual rpmh
249 * regulator
250 * @name: Name of the regulator
251 * @rdesc: Regulator descriptor
252 * @rdev: Regulator device pointer returned by
253 * devm_regulator_register()
254 * @aggr_vreg: Pointer to the aggregated rpmh regulator
255 * resource
256 * @set_active: Boolean flag indicating that requests made by
257 * this regulator should take affect in the active
258 * set
259 * @set_sleep: Boolean flag indicating that requests made by
260 * this regulator should take affect in the sleep
261 * set
262 * @req: RPMh accelerator register request
263 * @mode_index: RPMh VRM regulator mode selected by index into
264 * aggr_vreg->mode
265 */
266struct rpmh_vreg {
267 struct device_node *of_node;
268 struct regulator_desc rdesc;
269 struct regulator_dev *rdev;
270 struct rpmh_aggr_vreg *aggr_vreg;
271 bool set_active;
272 bool set_sleep;
273 struct rpmh_regulator_request req;
274 int mode_index;
275};
276
David Collinsf676d3d2017-12-12 18:23:33 -0800277#define RPMH_REGULATOR_MODE_COUNT 5
278
279#define RPMH_REGULATOR_MODE_PMIC4_LDO_RM 4
280#define RPMH_REGULATOR_MODE_PMIC4_LDO_LPM 5
281#define RPMH_REGULATOR_MODE_PMIC4_LDO_HPM 7
282
283#define RPMH_REGULATOR_MODE_PMIC4_SMPS_RM 4
284#define RPMH_REGULATOR_MODE_PMIC4_SMPS_PFM 5
285#define RPMH_REGULATOR_MODE_PMIC4_SMPS_AUTO 6
286#define RPMH_REGULATOR_MODE_PMIC4_SMPS_PWM 7
287
288#define RPMH_REGULATOR_MODE_PMIC4_BOB_PASS 0
289#define RPMH_REGULATOR_MODE_PMIC4_BOB_PFM 1
290#define RPMH_REGULATOR_MODE_PMIC4_BOB_AUTO 2
291#define RPMH_REGULATOR_MODE_PMIC4_BOB_PWM 3
292
293#define RPMH_REGULATOR_MODE_PMIC5_LDO_RM 3
294#define RPMH_REGULATOR_MODE_PMIC5_LDO_LPM 4
295#define RPMH_REGULATOR_MODE_PMIC5_LDO_HPM 7
296
297#define RPMH_REGULATOR_MODE_PMIC5_HFSMPS_RM 3
298#define RPMH_REGULATOR_MODE_PMIC5_HFSMPS_PFM 4
299#define RPMH_REGULATOR_MODE_PMIC5_HFSMPS_AUTO 6
300#define RPMH_REGULATOR_MODE_PMIC5_HFSMPS_PWM 7
301
302#define RPMH_REGULATOR_MODE_PMIC5_FTSMPS_RM 3
303#define RPMH_REGULATOR_MODE_PMIC5_FTSMPS_PWM 7
304
305#define RPMH_REGULATOR_MODE_PMIC5_BOB_PASS 2
306#define RPMH_REGULATOR_MODE_PMIC5_BOB_PFM 4
307#define RPMH_REGULATOR_MODE_PMIC5_BOB_AUTO 6
308#define RPMH_REGULATOR_MODE_PMIC5_BOB_PWM 7
309
310/*
311 * Mappings from RPMh generic modes to VRM accelerator modes and regulator
312 * framework modes for each regulator type.
313 */
314static const struct rpmh_regulator_mode
315rpmh_regulator_mode_map_pmic4_ldo[RPMH_REGULATOR_MODE_COUNT] = {
316 [RPMH_REGULATOR_MODE_RET] = {
317 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_LDO_RM,
318 .framework_mode = REGULATOR_MODE_STANDBY,
319 },
320 [RPMH_REGULATOR_MODE_LPM] = {
321 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_LDO_LPM,
322 .framework_mode = REGULATOR_MODE_IDLE,
323 },
324 [RPMH_REGULATOR_MODE_HPM] = {
325 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_LDO_HPM,
326 .framework_mode = REGULATOR_MODE_FAST,
327 },
328};
329
330static const struct rpmh_regulator_mode
331rpmh_regulator_mode_map_pmic4_smps[RPMH_REGULATOR_MODE_COUNT] = {
332 [RPMH_REGULATOR_MODE_RET] = {
333 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_SMPS_RM,
334 .framework_mode = REGULATOR_MODE_STANDBY,
335 },
336 [RPMH_REGULATOR_MODE_LPM] = {
337 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_SMPS_PFM,
338 .framework_mode = REGULATOR_MODE_IDLE,
339 },
340 [RPMH_REGULATOR_MODE_AUTO] = {
341 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_SMPS_AUTO,
342 .framework_mode = REGULATOR_MODE_NORMAL,
343 },
344 [RPMH_REGULATOR_MODE_HPM] = {
345 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_SMPS_PWM,
346 .framework_mode = REGULATOR_MODE_FAST,
347 },
348};
349
350static const struct rpmh_regulator_mode
351rpmh_regulator_mode_map_pmic4_bob[RPMH_REGULATOR_MODE_COUNT] = {
352 [RPMH_REGULATOR_MODE_PASS] = {
353 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_BOB_PASS,
354 .framework_mode = REGULATOR_MODE_STANDBY,
355 },
356 [RPMH_REGULATOR_MODE_LPM] = {
357 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_BOB_PFM,
358 .framework_mode = REGULATOR_MODE_IDLE,
359 },
360 [RPMH_REGULATOR_MODE_AUTO] = {
361 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_BOB_AUTO,
362 .framework_mode = REGULATOR_MODE_NORMAL,
363 },
364 [RPMH_REGULATOR_MODE_HPM] = {
365 .pmic_mode = RPMH_REGULATOR_MODE_PMIC4_BOB_PWM,
366 .framework_mode = REGULATOR_MODE_FAST,
367 },
368};
369
370static const struct rpmh_regulator_mode
371rpmh_regulator_mode_map_pmic5_ldo[RPMH_REGULATOR_MODE_COUNT] = {
372 [RPMH_REGULATOR_MODE_RET] = {
373 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_LDO_RM,
374 .framework_mode = REGULATOR_MODE_STANDBY,
375 },
376 [RPMH_REGULATOR_MODE_LPM] = {
377 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_LDO_LPM,
378 .framework_mode = REGULATOR_MODE_IDLE,
379 },
380 [RPMH_REGULATOR_MODE_HPM] = {
381 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_LDO_HPM,
382 .framework_mode = REGULATOR_MODE_FAST,
383 },
384};
385
386static const struct rpmh_regulator_mode
387rpmh_regulator_mode_map_pmic5_hfsmps[RPMH_REGULATOR_MODE_COUNT] = {
388 [RPMH_REGULATOR_MODE_RET] = {
389 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_HFSMPS_RM,
390 .framework_mode = REGULATOR_MODE_STANDBY,
391 },
392 [RPMH_REGULATOR_MODE_LPM] = {
393 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_HFSMPS_PFM,
394 .framework_mode = REGULATOR_MODE_IDLE,
395 },
396 [RPMH_REGULATOR_MODE_AUTO] = {
397 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_HFSMPS_AUTO,
398 .framework_mode = REGULATOR_MODE_NORMAL,
399 },
400 [RPMH_REGULATOR_MODE_HPM] = {
401 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_HFSMPS_PWM,
402 .framework_mode = REGULATOR_MODE_FAST,
403 },
404};
405
406static const struct rpmh_regulator_mode
407rpmh_regulator_mode_map_pmic5_ftsmps[RPMH_REGULATOR_MODE_COUNT] = {
408 [RPMH_REGULATOR_MODE_RET] = {
409 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_FTSMPS_RM,
410 .framework_mode = REGULATOR_MODE_STANDBY,
411 },
412 [RPMH_REGULATOR_MODE_HPM] = {
413 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_FTSMPS_PWM,
414 .framework_mode = REGULATOR_MODE_FAST,
415 },
416};
417
418static const struct rpmh_regulator_mode
419rpmh_regulator_mode_map_pmic5_bob[RPMH_REGULATOR_MODE_COUNT] = {
420 [RPMH_REGULATOR_MODE_PASS] = {
421 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_BOB_PASS,
422 .framework_mode = REGULATOR_MODE_STANDBY,
423 },
424 [RPMH_REGULATOR_MODE_LPM] = {
425 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_BOB_PFM,
426 .framework_mode = REGULATOR_MODE_IDLE,
427 },
428 [RPMH_REGULATOR_MODE_AUTO] = {
429 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_BOB_AUTO,
430 .framework_mode = REGULATOR_MODE_NORMAL,
431 },
432 [RPMH_REGULATOR_MODE_HPM] = {
433 .pmic_mode = RPMH_REGULATOR_MODE_PMIC5_BOB_PWM,
434 .framework_mode = REGULATOR_MODE_FAST,
435 },
436};
437
438static const struct rpmh_regulator_mode * const
439rpmh_regulator_mode_map[RPMH_REGULATOR_HW_TYPE_MAX] = {
440 [RPMH_REGULATOR_HW_TYPE_PMIC4_LDO]
441 = rpmh_regulator_mode_map_pmic4_ldo,
442 [RPMH_REGULATOR_HW_TYPE_PMIC4_HFSMPS]
443 = rpmh_regulator_mode_map_pmic4_smps,
444 [RPMH_REGULATOR_HW_TYPE_PMIC4_FTSMPS]
445 = rpmh_regulator_mode_map_pmic4_smps,
446 [RPMH_REGULATOR_HW_TYPE_PMIC4_BOB]
447 = rpmh_regulator_mode_map_pmic4_bob,
448 [RPMH_REGULATOR_HW_TYPE_PMIC5_LDO]
449 = rpmh_regulator_mode_map_pmic5_ldo,
450 [RPMH_REGULATOR_HW_TYPE_PMIC5_HFSMPS]
451 = rpmh_regulator_mode_map_pmic5_hfsmps,
452 [RPMH_REGULATOR_HW_TYPE_PMIC5_FTSMPS]
453 = rpmh_regulator_mode_map_pmic5_ftsmps,
454 [RPMH_REGULATOR_HW_TYPE_PMIC5_BOB]
455 = rpmh_regulator_mode_map_pmic5_bob,
456};
457
David Collinse1b43192016-10-12 17:27:22 -0700458/*
459 * This voltage in uV is returned by get_voltage functions when there is no way
460 * to determine the current voltage level. It is needed because the regulator
461 * framework treats a 0 uV voltage as an error.
462 */
463#define VOLTAGE_UNKNOWN 1
464
465#define vreg_err(vreg, message, ...) \
466 pr_err("%s: " message, (vreg)->rdesc.name, ##__VA_ARGS__)
467#define vreg_info(vreg, message, ...) \
468 pr_info("%s: " message, (vreg)->rdesc.name, ##__VA_ARGS__)
469#define vreg_debug(vreg, message, ...) \
470 pr_debug("%s: " message, (vreg)->rdesc.name, ##__VA_ARGS__)
471
472#define aggr_vreg_err(aggr_vreg, message, ...) \
473 pr_err("%s: " message, (aggr_vreg)->resource_name, ##__VA_ARGS__)
474#define aggr_vreg_info(aggr_vreg, message, ...) \
475 pr_info("%s: " message, (aggr_vreg)->resource_name, ##__VA_ARGS__)
476#define aggr_vreg_debug(aggr_vreg, message, ...) \
477 pr_debug("%s: " message, (aggr_vreg)->resource_name, ##__VA_ARGS__)
478
479#define DEBUG_PRINT_BUFFER_SIZE 256
480static const char *const rpmh_regulator_state_names[] = {
481 [RPMH_SLEEP_STATE] = "sleep ",
482 [RPMH_WAKE_ONLY_STATE] = "wake ",
483 [RPMH_ACTIVE_ONLY_STATE] = "active",
484 [RPMH_AWAKE_STATE] = "awake ",
485};
486
487static const char *const rpmh_regulator_vrm_param_names[] = {
488 [RPMH_REGULATOR_REG_VRM_VOLTAGE] = "mv",
489 [RPMH_REGULATOR_REG_VRM_ENABLE] = "en",
490 [RPMH_REGULATOR_REG_VRM_MODE] = "mode",
491 [RPMH_REGULATOR_REG_VRM_HEADROOM] = "hr_mv",
492};
493
494static const char *const rpmh_regulator_arc_param_names[] = {
495 [RPMH_REGULATOR_REG_ARC_LEVEL] = "hlvl",
496};
497
David Collins3bd8e102017-10-30 16:37:55 -0700498static const char *const rpmh_regulator_xob_param_names[] = {
499 [RPMH_REGULATOR_REG_XOB_ENABLE] = "en",
500};
501
David Collinse1b43192016-10-12 17:27:22 -0700502/**
503 * rpmh_regulator_req() - print the rpmh regulator request to the kernel log
504 * @vreg: Pointer to the RPMh regulator
505 * @current_req: Pointer to the new request
506 * @prev_req: Pointer to the last request
507 * @sent_mask: Bitmask which specifies the parameters sent in this
508 * request
509 * @state: The rpmh state that the request was sent for
510 *
511 * Return: none
512 */
513static void rpmh_regulator_req(struct rpmh_vreg *vreg,
514 struct rpmh_regulator_request *current_req,
515 struct rpmh_regulator_request *prev_req,
516 u32 sent_mask,
517 enum rpmh_state state)
518{
519 struct rpmh_aggr_vreg *aggr_vreg = vreg->aggr_vreg;
520 char buf[DEBUG_PRINT_BUFFER_SIZE];
521 size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
522 const char *const *param_name;
523 int i, max_reg_index;
524 int pos = 0;
525 u32 valid;
526 bool first;
527
David Collins3bd8e102017-10-30 16:37:55 -0700528 switch (aggr_vreg->regulator_type) {
529 case RPMH_REGULATOR_TYPE_VRM:
530 max_reg_index = RPMH_REGULATOR_REG_VRM_MAX;
531 param_name = rpmh_regulator_vrm_param_names;
532 break;
533 case RPMH_REGULATOR_TYPE_ARC:
534 max_reg_index = RPMH_REGULATOR_REG_ARC_REAL_MAX;
535 param_name = rpmh_regulator_arc_param_names;
536 break;
537 case RPMH_REGULATOR_TYPE_XOB:
538 max_reg_index = RPMH_REGULATOR_REG_XOB_MAX;
539 param_name = rpmh_regulator_xob_param_names;
540 break;
541 default:
542 return;
543 }
David Collinse1b43192016-10-12 17:27:22 -0700544
545 pos += scnprintf(buf + pos, buflen - pos,
546 "%s (%s), addr=0x%05X: s=%s; sent: ",
547 aggr_vreg->resource_name, vreg->rdesc.name,
548 aggr_vreg->addr, rpmh_regulator_state_names[state]);
549
550 valid = sent_mask;
551 first = true;
552 for (i = 0; i < max_reg_index; i++) {
553 if (valid & BIT(i)) {
554 pos += scnprintf(buf + pos, buflen - pos, "%s%s=%u",
555 (first ? "" : ", "), param_name[i],
556 current_req->reg[i]);
557 first = false;
558 if (aggr_vreg->regulator_type
559 == RPMH_REGULATOR_TYPE_ARC
560 && i == RPMH_REGULATOR_REG_ARC_LEVEL)
561 pos += scnprintf(buf + pos, buflen - pos,
562 " (vlvl=%u)",
563 aggr_vreg->level[current_req->reg[i]]);
564 }
565 }
566
567 valid = prev_req->valid & ~sent_mask;
568
569 if (valid)
570 pos += scnprintf(buf + pos, buflen - pos, "; prev: ");
571 first = true;
572 for (i = 0; i < max_reg_index; i++) {
573 if (valid & BIT(i)) {
574 pos += scnprintf(buf + pos, buflen - pos, "%s%s=%u",
575 (first ? "" : ", "), param_name[i],
576 current_req->reg[i]);
577 first = false;
578 if (aggr_vreg->regulator_type
579 == RPMH_REGULATOR_TYPE_ARC
580 && i == RPMH_REGULATOR_REG_ARC_LEVEL)
581 pos += scnprintf(buf + pos, buflen - pos,
582 " (vlvl=%u)",
583 aggr_vreg->level[current_req->reg[i]]);
584 }
585 }
586
587 pr_debug("%s\n", buf);
588}
589
590/**
591 * rpmh_regulator_handle_arc_enable() - handle masking of the voltage level
592 * request based on the pseudo-enable value
593 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
594 * @req Pointer to the newly aggregated request
595 *
596 * Return: none
597 */
598static void rpmh_regulator_handle_arc_enable(struct rpmh_aggr_vreg *aggr_vreg,
599 struct rpmh_regulator_request *req)
600{
601 if (aggr_vreg->regulator_type != RPMH_REGULATOR_TYPE_ARC)
602 return;
603
604 /*
605 * Mask the voltage level if "off" level is supported and the regulator
606 * has not been enabled.
607 */
David Collins1e892eac2017-05-30 17:55:17 -0700608 if (aggr_vreg->level[0] == RPMH_REGULATOR_LEVEL_OFF) {
609 if (req->valid & BIT(RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE)) {
610 if (!req->reg[RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE])
611 req->reg[RPMH_REGULATOR_REG_ARC_LEVEL] = 0;
612 } else {
613 /* Invalidate voltage level if enable is invalid. */
614 req->valid &= ~BIT(RPMH_REGULATOR_REG_ARC_LEVEL);
615 }
616 }
David Collinse1b43192016-10-12 17:27:22 -0700617
618 /*
619 * Mark the pseudo enable bit as invalid so that it is not accidentally
620 * included in an RPMh command.
621 */
622 req->valid &= ~BIT(RPMH_REGULATOR_REG_ARC_PSEUDO_ENABLE);
623}
624
625/**
626 * rpmh_regulator_send_aggregate_requests() - aggregate the requests from all
627 * regulators associated with an RPMh resource and send the request
628 * to RPMh
629 * @vreg: Pointer to the RPMh regulator
630 *
631 * This function aggregates the requests from the different regulators
632 * associated with the aggr_vreg resource independently in both the active set
633 * and sleep set. The requests are only sent for the sleep set if they differ,
634 * or have differed in the past, from those of the active set.
635 *
636 * Return: 0 on success, errno on failure
637 */
638static int
639rpmh_regulator_send_aggregate_requests(struct rpmh_vreg *vreg)
640{
641 struct rpmh_aggr_vreg *aggr_vreg = vreg->aggr_vreg;
642 struct rpmh_regulator_request req_active = { {0} };
643 struct rpmh_regulator_request req_sleep = { {0} };
644 struct tcs_cmd cmd[RPMH_REGULATOR_REG_MAX] = { {0} };
645 bool sleep_set_differs = aggr_vreg->sleep_request_sent;
646 bool wait_for_ack = aggr_vreg->always_wait_for_ack
647 || aggr_vreg->next_wait_for_ack;
David Collins5f2e44f2017-08-08 13:35:49 -0700648 bool resend_active = false;
David Collinse1b43192016-10-12 17:27:22 -0700649 int i, j, max_reg_index, rc;
650 enum rpmh_state state;
651 u32 sent_mask;
652
David Collins3bd8e102017-10-30 16:37:55 -0700653 switch (aggr_vreg->regulator_type) {
654 case RPMH_REGULATOR_TYPE_VRM:
655 max_reg_index = RPMH_REGULATOR_REG_VRM_MAX;
656 break;
657 case RPMH_REGULATOR_TYPE_ARC:
658 max_reg_index = RPMH_REGULATOR_REG_ARC_MAX;
659 break;
660 case RPMH_REGULATOR_TYPE_XOB:
661 max_reg_index = RPMH_REGULATOR_REG_XOB_MAX;
662 break;
663 default:
664 return -EINVAL;
665 }
666
David Collinse1b43192016-10-12 17:27:22 -0700667 /*
668 * Perform max aggregration of each register value across all regulators
669 * which use this RPMh resource.
670 */
671 for (i = 0; i < aggr_vreg->vreg_count; i++) {
672 if (aggr_vreg->vreg[i].set_active) {
673 for (j = 0; j < max_reg_index; j++)
674 req_active.reg[j] = max(req_active.reg[j],
675 aggr_vreg->vreg[i].req.reg[j]);
676 req_active.valid |= aggr_vreg->vreg[i].req.valid;
677 }
678 if (aggr_vreg->vreg[i].set_sleep) {
679 for (j = 0; j < max_reg_index; j++)
680 req_sleep.reg[j] = max(req_sleep.reg[j],
681 aggr_vreg->vreg[i].req.reg[j]);
682 req_sleep.valid |= aggr_vreg->vreg[i].req.valid;
683 }
684 }
685
686 rpmh_regulator_handle_arc_enable(aggr_vreg, &req_active);
687 rpmh_regulator_handle_arc_enable(aggr_vreg, &req_sleep);
688
689 /*
690 * Check if the aggregated sleep set parameter values differ from the
691 * aggregated active set parameter values.
692 */
693 if (!aggr_vreg->sleep_request_sent) {
694 for (i = 0; i < max_reg_index; i++) {
695 if ((req_active.reg[i] != req_sleep.reg[i])
696 && (req_sleep.valid & BIT(i))) {
697 sleep_set_differs = true;
David Collins5f2e44f2017-08-08 13:35:49 -0700698 /*
699 * Resend full active set request so that
700 * all parameters are specified in the wake-only
701 * state request.
702 */
703 resend_active = !aggr_vreg->use_awake_state;
David Collinse1b43192016-10-12 17:27:22 -0700704 break;
705 }
706 }
707 }
708
709 if (sleep_set_differs) {
710 /*
711 * Generate an rpmh command consisting of only those registers
712 * which have new values or which have never been touched before
713 * (i.e. those that were previously not valid).
714 */
715 sent_mask = 0;
716 for (i = 0, j = 0; i < max_reg_index; i++) {
717 if ((req_sleep.valid & BIT(i))
718 && (!(aggr_vreg->aggr_req_sleep.valid & BIT(i))
719 || aggr_vreg->aggr_req_sleep.reg[i]
720 != req_sleep.reg[i])) {
721 cmd[j].addr = aggr_vreg->addr + i * 4;
722 cmd[j].data = req_sleep.reg[i];
723 j++;
724 sent_mask |= BIT(i);
725 }
726 }
727
728 /* Send the rpmh command if any register values differ. */
729 if (j > 0) {
730 rc = rpmh_write_async(aggr_vreg->rpmh_client,
731 RPMH_SLEEP_STATE, cmd, j);
732 if (rc) {
733 aggr_vreg_err(aggr_vreg, "sleep state rpmh_write_async() failed, rc=%d\n",
734 rc);
735 return rc;
736 }
737 rpmh_regulator_req(vreg, &req_sleep,
738 &aggr_vreg->aggr_req_sleep,
739 sent_mask,
740 RPMH_SLEEP_STATE);
741 aggr_vreg->sleep_request_sent = true;
742 aggr_vreg->aggr_req_sleep = req_sleep;
743 }
744 }
745
746 /*
747 * Generate an rpmh command consisting of only those registers
748 * which have new values or which have never been touched before
749 * (i.e. those that were previously not valid).
750 */
751 sent_mask = 0;
752 for (i = 0, j = 0; i < max_reg_index; i++) {
753 if ((req_active.valid & BIT(i))
754 && (!(aggr_vreg->aggr_req_active.valid & BIT(i))
755 || aggr_vreg->aggr_req_active.reg[i]
David Collins5f2e44f2017-08-08 13:35:49 -0700756 != req_active.reg[i] || resend_active)) {
David Collinse1b43192016-10-12 17:27:22 -0700757 cmd[j].addr = aggr_vreg->addr + i * 4;
758 cmd[j].data = req_active.reg[i];
759 j++;
760 sent_mask |= BIT(i);
761
762 /*
763 * Must wait for ACK from RPMh if power state is
764 * increasing
765 */
766 if (req_active.reg[i]
767 > aggr_vreg->aggr_req_active.reg[i])
768 wait_for_ack = true;
769 }
770 }
771
772 /* Send the rpmh command if any register values differ. */
773 if (j > 0) {
774 if (sleep_set_differs && !aggr_vreg->use_awake_state) {
775 state = RPMH_WAKE_ONLY_STATE;
776 rc = rpmh_write_async(aggr_vreg->rpmh_client, state,
777 cmd, j);
778 if (rc) {
779 aggr_vreg_err(aggr_vreg, "%s state rpmh_write_async() failed, rc=%d\n",
780 rpmh_regulator_state_names[state], rc);
781 return rc;
782 }
783 rpmh_regulator_req(vreg, &req_active,
784 &aggr_vreg->aggr_req_active, sent_mask, state);
785 }
786
787 state = aggr_vreg->use_awake_state ? RPMH_AWAKE_STATE
788 : RPMH_ACTIVE_ONLY_STATE;
789 if (wait_for_ack)
790 rc = rpmh_write(aggr_vreg->rpmh_client, state, cmd, j);
791 else
792 rc = rpmh_write_async(aggr_vreg->rpmh_client, state,
793 cmd, j);
794 if (rc) {
795 aggr_vreg_err(aggr_vreg, "%s state rpmh_write() failed, rc=%d\n",
796 rpmh_regulator_state_names[state], rc);
797 return rc;
798 }
799 rpmh_regulator_req(vreg, &req_active,
800 &aggr_vreg->aggr_req_active, sent_mask, state);
801
802 aggr_vreg->aggr_req_active = req_active;
803 aggr_vreg->next_wait_for_ack = false;
804 }
805
806 return 0;
807}
808
809/**
810 * rpmh_regulator_set_reg() - set a register value within the request for an
811 * RPMh regulator and return the previous value
812 * @vreg: Pointer to the RPMh regulator
813 * @reg_index: Index of the register value to update
814 * @value: New register value to set
815 *
816 * Return: old register value
817 */
818static u32 rpmh_regulator_set_reg(struct rpmh_vreg *vreg, int reg_index,
819 u32 value)
820{
821 u32 old_value;
822
823 old_value = vreg->req.reg[reg_index];
824 vreg->req.reg[reg_index] = value;
825 vreg->req.valid |= BIT(reg_index);
826
827 return old_value;
828}
829
830/**
831 * rpmh_regulator_check_param_max() - sets if the next request must wait for
832 * an ACK based on the previously sent reg[index] value and the new
833 * max value
834 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
835 * @index: Register index
836 * @new_max: Newly requested maximum allowed value for the parameter
837 *
838 * This function is used to handle the case when a consumer makes a new
839 * (min_uv, max_uv) range request in which the new max_uv is lower than the
840 * previously requested min_uv. In this case, the driver must wait for an ACK
841 * from RPMh to ensure that the voltage has completed reducing to the new min_uv
842 * value since the consumer cannot operate at the old min_uv value.
843 *
844 * Return: none
845 */
846static void rpmh_regulator_check_param_max(struct rpmh_aggr_vreg *aggr_vreg,
847 int index, u32 new_max)
848{
849 if ((aggr_vreg->aggr_req_active.valid & BIT(index))
850 && aggr_vreg->aggr_req_active.reg[index] > new_max)
851 aggr_vreg->next_wait_for_ack = true;
852}
853
854/**
855 * rpmh_regulator_is_enabled() - return the enable state of the RPMh
856 * regulator
857 * @rdev: Regulator device pointer for the rpmh-regulator
858 *
859 * This function is passed as a callback function into the regulator ops that
860 * are registered for each rpmh-regulator device.
861 *
862 * Note that for ARC resources, this value is effectively a flag indicating if
863 * the requested voltage level is masked or unmasked since "disabled" = voltage
864 * level 0 (if supported).
865 *
866 * Return: true if regulator is enabled, false if regulator is disabled
867 */
868static int rpmh_regulator_is_enabled(struct regulator_dev *rdev)
869{
870 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
871
872 return !!vreg->req.reg[RPMH_REGULATOR_REG_ENABLE];
873}
874
875/**
876 * rpmh_regulator_enable() - enable the RPMh regulator
877 * @rdev: Regulator device pointer for the rpmh-regulator
878 *
879 * This function is passed as a callback function into the regulator ops that
880 * are registered for each rpmh-regulator device.
881 *
882 * Note that for ARC devices the enable state is handled via the voltage level
883 * parameter. Therefore, this enable value effectively masks or unmasks the
884 * enabled voltage level.
885 *
886 * Return: 0 on success, errno on failure
887 */
888static int rpmh_regulator_enable(struct regulator_dev *rdev)
889{
890 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
891 u32 prev_enable;
892 int rc;
893
894 mutex_lock(&vreg->aggr_vreg->lock);
895
896 prev_enable
897 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE, 1);
898
899 rc = rpmh_regulator_send_aggregate_requests(vreg);
900 if (rc) {
901 vreg_err(vreg, "enable failed, rc=%d\n", rc);
902 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE,
903 prev_enable);
904 }
905
906 mutex_unlock(&vreg->aggr_vreg->lock);
907
908 return rc;
909}
910
911/**
912 * rpmh_regulator_disable() - disable the RPMh regulator
913 * @rdev: Regulator device pointer for the rpmh-regulator
914 *
915 * This function is passed as a callback function into the regulator ops that
916 * are registered for each rpmh-regulator device.
917 *
918 * Note that for ARC devices the enable state is handled via the voltage level
919 * parameter. Therefore, this enable value effectively masks or unmasks the
920 * enabled voltage level.
921 *
922 * Return: 0 on success, errno on failure
923 */
924static int rpmh_regulator_disable(struct regulator_dev *rdev)
925{
926 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
927 u32 prev_enable;
928 int rc;
929
930 mutex_lock(&vreg->aggr_vreg->lock);
931
932 prev_enable
933 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE, 0);
934
935 rc = rpmh_regulator_send_aggregate_requests(vreg);
936 if (rc) {
937 vreg_err(vreg, "disable failed, rc=%d\n", rc);
938 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ENABLE,
939 prev_enable);
940 }
941
942 mutex_unlock(&vreg->aggr_vreg->lock);
943
944 return rc;
945}
946
947/**
948 * rpmh_regulator_vrm_set_voltage() - set the voltage of the VRM rpmh-regulator
949 * @rdev: Regulator device pointer for the rpmh-regulator
950 * @min_uv: New voltage in microvolts to set
951 * @max_uv: Maximum voltage in microvolts allowed
952 * @selector: Unused
953 *
954 * This function is passed as a callback function into the regulator ops that
955 * are registered for each VRM rpmh-regulator device.
956 *
957 * Return: 0 on success, errno on failure
958 */
959static int rpmh_regulator_vrm_set_voltage(struct regulator_dev *rdev,
960 int min_uv, int max_uv, unsigned int *selector)
961{
962 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
963 u32 prev_voltage;
964 int mv;
965 int rc = 0;
966
967 mv = DIV_ROUND_UP(min_uv, 1000);
968 if (mv * 1000 > max_uv) {
969 vreg_err(vreg, "no set points available in range %d-%d uV\n",
970 min_uv, max_uv);
971 return -EINVAL;
972 }
973
974 mutex_lock(&vreg->aggr_vreg->lock);
975
976 prev_voltage
977 = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_VOLTAGE, mv);
978 rpmh_regulator_check_param_max(vreg->aggr_vreg,
979 RPMH_REGULATOR_REG_VRM_VOLTAGE, max_uv);
980
981 rc = rpmh_regulator_send_aggregate_requests(vreg);
982 if (rc) {
983 vreg_err(vreg, "set voltage=%d mV failed, rc=%d\n", mv, rc);
984 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_VOLTAGE,
985 prev_voltage);
986 }
987
988 mutex_unlock(&vreg->aggr_vreg->lock);
989
990 return rc;
991}
992
993/**
994 * rpmh_regulator_vrm_get_voltage() - get the voltage of the VRM rpmh-regulator
995 * @rdev: Regulator device pointer for the rpmh-regulator
996 *
997 * This function is passed as a callback function into the regulator ops that
998 * are registered for each VRM rpmh-regulator device.
999 *
1000 * Return: regulator voltage in microvolts
1001 */
1002static int rpmh_regulator_vrm_get_voltage(struct regulator_dev *rdev)
1003{
1004 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
1005 int uv;
1006
1007 uv = vreg->req.reg[RPMH_REGULATOR_REG_VRM_VOLTAGE] * 1000;
1008 if (uv == 0)
1009 uv = VOLTAGE_UNKNOWN;
1010
1011 return uv;
1012}
1013
1014/**
1015 * rpmh_regulator_vrm_set_mode_index() - set the mode of a VRM regulator to the
1016 * mode mapped to mode_index
1017 * @vreg: Pointer to the RPMh regulator
1018 * @mode_index: Index into aggr_vreg->mode[] array
1019 *
1020 * Return: 0 on success, errno on failure
1021 */
1022static int rpmh_regulator_vrm_set_mode_index(struct rpmh_vreg *vreg,
1023 int mode_index)
1024{
1025 u32 prev_mode;
1026 int rc;
1027
1028 mutex_lock(&vreg->aggr_vreg->lock);
1029
1030 prev_mode = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_MODE,
1031 vreg->aggr_vreg->mode[mode_index].pmic_mode);
1032
1033 rc = rpmh_regulator_send_aggregate_requests(vreg);
1034 if (rc) {
1035 vreg_err(vreg, "set mode=%u failed, rc=%d\n",
1036 vreg->req.reg[RPMH_REGULATOR_REG_VRM_MODE],
1037 rc);
1038 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_VRM_MODE,
1039 prev_mode);
1040 } else {
1041 vreg->mode_index = mode_index;
1042 }
1043
1044 mutex_unlock(&vreg->aggr_vreg->lock);
1045
1046 return rc;
1047}
1048
1049/**
1050 * rpmh_regulator_vrm_set_mode() - set the mode of the VRM rpmh-regulator
1051 * @rdev: Regulator device pointer for the rpmh-regulator
1052 * @mode: The regulator framework mode to set
1053 *
1054 * This function is passed as a callback function into the regulator ops that
1055 * are registered for each VRM rpmh-regulator device.
1056 *
1057 * This function sets the PMIC mode corresponding to the specified framework
1058 * mode. The set of PMIC modes allowed is defined in device tree for a given
David Collinsf676d3d2017-12-12 18:23:33 -08001059 * RPMh regulator resource. The full mapping from generic modes to PMIC modes
1060 * and framework modes is defined in the rpmh_regulator_mode_map[] array. The
1061 * RPMh resource specific mapping is defined in the aggr_vreg->mode[] array.
David Collinse1b43192016-10-12 17:27:22 -07001062 *
1063 * Return: 0 on success, errno on failure
1064 */
1065static int rpmh_regulator_vrm_set_mode(struct regulator_dev *rdev,
1066 unsigned int mode)
1067{
1068 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
1069 int i;
1070
1071 for (i = 0; i < vreg->aggr_vreg->mode_count; i++)
1072 if (vreg->aggr_vreg->mode[i].framework_mode == mode)
1073 break;
1074 if (i >= vreg->aggr_vreg->mode_count) {
1075 vreg_err(vreg, "invalid mode=%u\n", mode);
1076 return -EINVAL;
1077 }
1078
1079 return rpmh_regulator_vrm_set_mode_index(vreg, i);
1080}
1081
1082/**
1083 * rpmh_regulator_vrm_get_mode() - get the mode of the VRM rpmh-regulator
1084 * @rdev: Regulator device pointer for the rpmh-regulator
1085 *
1086 * This function is passed as a callback function into the regulator ops that
1087 * are registered for each VRM rpmh-regulator device.
1088 *
1089 * Return: the regulator framework mode of the regulator
1090 */
1091static unsigned int rpmh_regulator_vrm_get_mode(struct regulator_dev *rdev)
1092{
1093 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
1094
1095 return vreg->aggr_vreg->mode[vreg->mode_index].framework_mode;
1096}
1097
1098/**
1099 * rpmh_regulator_vrm_set_load() - set the PMIC mode based upon the maximum load
1100 * required from the VRM rpmh-regulator
1101 * @rdev: Regulator device pointer for the rpmh-regulator
1102 * @load_ua: Maximum current required from all consumers in microamps
1103 *
1104 * This function is passed as a callback function into the regulator ops that
1105 * are registered for each VRM rpmh-regulator device.
1106 *
1107 * This function sets the mode of the regulator to that which has the highest
1108 * min support load less than or equal to load_ua. Example:
1109 * mode_count = 3
1110 * mode[].min_load_ua = 0, 100000, 6000000
1111 *
1112 * load_ua = 10000 --> mode_index = 0
1113 * load_ua = 250000 --> mode_index = 1
1114 * load_ua = 7000000 --> mode_index = 2
1115 *
1116 * Return: 0 on success, errno on failure
1117 */
1118static int rpmh_regulator_vrm_set_load(struct regulator_dev *rdev, int load_ua)
1119{
1120 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
1121 int i;
1122
1123 /* No need to check element 0 as it will be the default. */
1124 for (i = vreg->aggr_vreg->mode_count - 1; i > 0; i--)
1125 if (vreg->aggr_vreg->mode[i].min_load_ua <= load_ua)
1126 break;
1127
1128 return rpmh_regulator_vrm_set_mode_index(vreg, i);
1129}
1130
1131/**
1132 * rpmh_regulator_arc_set_voltage_sel() - set the voltage level of the ARC
1133 * rpmh-regulator device
1134 * @rdev: Regulator device pointer for the rpmh-regulator
1135 * @selector: ARC voltage level to set
1136 *
1137 * This function is passed as a callback function into the regulator ops that
1138 * are registered for each ARC rpmh-regulator device.
1139 *
1140 * Return: 0 on success, errno on failure
1141 */
1142static int rpmh_regulator_arc_set_voltage_sel(struct regulator_dev *rdev,
1143 unsigned int selector)
1144{
1145 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
1146 u32 prev_level;
1147 int rc;
1148
1149 mutex_lock(&vreg->aggr_vreg->lock);
1150
1151 prev_level = rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ARC_LEVEL,
1152 selector);
1153
1154 rc = rpmh_regulator_send_aggregate_requests(vreg);
1155 if (rc) {
1156 vreg_err(vreg, "set level=%d failed, rc=%d\n",
1157 vreg->req.reg[RPMH_REGULATOR_REG_ARC_LEVEL],
1158 rc);
1159 rpmh_regulator_set_reg(vreg, RPMH_REGULATOR_REG_ARC_LEVEL,
1160 prev_level);
1161 }
1162
1163 mutex_unlock(&vreg->aggr_vreg->lock);
1164
1165 return rc;
1166}
1167
1168/**
1169 * rpmh_regulator_arc_get_voltage_sel() - get the voltage level of the ARC
1170 * rpmh-regulator device
1171 * @rdev: Regulator device pointer for the rpmh-regulator
1172 *
1173 * This function is passed as a callback function into the regulator ops that
1174 * are registered for each ARC rpmh-regulator device.
1175 *
1176 * Return: ARC voltage level
1177 */
1178static int rpmh_regulator_arc_get_voltage_sel(struct regulator_dev *rdev)
1179{
1180 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
1181
1182 return vreg->req.reg[RPMH_REGULATOR_REG_ARC_LEVEL];
1183}
1184
1185/**
1186 * rpmh_regulator_arc_list_voltage() - return the consumer voltage level mapped
1187 * to a given ARC voltage level
1188 * @rdev: Regulator device pointer for the rpmh-regulator
1189 * @selector: ARC voltage level
1190 *
1191 * This function is passed as a callback function into the regulator ops that
1192 * are registered for each ARC rpmh-regulator device.
1193 *
1194 * Data ranges:
1195 * ARC voltage level: 0 - 15 (fixed in hardware)
1196 * Consumer voltage level: 1 - 513 (could be expanded to larger values)
1197 *
1198 * Return: consumer voltage level
1199 */
1200static int rpmh_regulator_arc_list_voltage(struct regulator_dev *rdev,
1201 unsigned int selector)
1202{
1203 struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
1204
1205 if (selector >= vreg->aggr_vreg->level_count)
1206 return 0;
1207
1208 return vreg->aggr_vreg->level[selector];
1209}
1210
1211static const struct regulator_ops rpmh_regulator_vrm_ops = {
1212 .enable = rpmh_regulator_enable,
1213 .disable = rpmh_regulator_disable,
1214 .is_enabled = rpmh_regulator_is_enabled,
1215 .set_voltage = rpmh_regulator_vrm_set_voltage,
1216 .get_voltage = rpmh_regulator_vrm_get_voltage,
1217 .set_mode = rpmh_regulator_vrm_set_mode,
1218 .get_mode = rpmh_regulator_vrm_get_mode,
1219 .set_load = rpmh_regulator_vrm_set_load,
1220};
1221
1222static const struct regulator_ops rpmh_regulator_arc_ops = {
1223 .enable = rpmh_regulator_enable,
1224 .disable = rpmh_regulator_disable,
1225 .is_enabled = rpmh_regulator_is_enabled,
1226 .set_voltage_sel = rpmh_regulator_arc_set_voltage_sel,
1227 .get_voltage_sel = rpmh_regulator_arc_get_voltage_sel,
1228 .list_voltage = rpmh_regulator_arc_list_voltage,
1229};
1230
David Collins3bd8e102017-10-30 16:37:55 -07001231static const struct regulator_ops rpmh_regulator_xob_ops = {
1232 .enable = rpmh_regulator_enable,
1233 .disable = rpmh_regulator_disable,
1234 .is_enabled = rpmh_regulator_is_enabled,
1235};
1236
David Collinse1b43192016-10-12 17:27:22 -07001237static const struct regulator_ops *rpmh_regulator_ops[] = {
1238 [RPMH_REGULATOR_TYPE_VRM] = &rpmh_regulator_vrm_ops,
1239 [RPMH_REGULATOR_TYPE_ARC] = &rpmh_regulator_arc_ops,
David Collins3bd8e102017-10-30 16:37:55 -07001240 [RPMH_REGULATOR_TYPE_XOB] = &rpmh_regulator_xob_ops,
David Collinse1b43192016-10-12 17:27:22 -07001241};
1242
1243/**
1244 * rpmh_regulator_load_arc_level_mapping() - load the RPMh ARC resource's
1245 * voltage level mapping from command db
1246 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1247 *
1248 * The set of supported RPMH_REGULATOR_LEVEL_* voltage levels (0 - ~512) that
1249 * map to ARC operating levels (0 - 15) is defined in aux data per ARC resource
1250 * in the command db SMEM data structure. It is in a u16 array with 1 to 16
1251 * elements. Note that the aux data array may be zero padded at the end for
1252 * data alignment purposes. Such padding entries are invalid and must be
1253 * ignored.
1254 *
1255 * Return: 0 on success, errno on failure
1256 */
1257static int
1258rpmh_regulator_load_arc_level_mapping(struct rpmh_aggr_vreg *aggr_vreg)
1259{
1260 int i, j, len, rc;
1261 u8 *buf;
1262
1263 len = cmd_db_get_aux_data_len(aggr_vreg->resource_name);
1264 if (len < 0) {
1265 aggr_vreg_err(aggr_vreg, "could not get ARC aux data len, rc=%d\n",
1266 len);
1267 return len;
1268 } else if (len == 0) {
1269 aggr_vreg_err(aggr_vreg, "ARC level mapping data missing in command db\n");
1270 return -EINVAL;
1271 } else if (len > RPMH_ARC_MAX_LEVELS * RPMH_ARC_LEVEL_SIZE) {
1272 aggr_vreg_err(aggr_vreg, "more ARC levels defined than allowed: %d > %d\n",
1273 len, RPMH_ARC_MAX_LEVELS * RPMH_ARC_LEVEL_SIZE);
1274 return -EINVAL;
1275 } else if (len % RPMH_ARC_LEVEL_SIZE) {
1276 aggr_vreg_err(aggr_vreg, "invalid ARC aux data size: %d\n",
1277 len);
1278 return -EINVAL;
1279 }
1280
1281 buf = kzalloc(len, GFP_KERNEL);
1282 if (!buf)
1283 return -ENOMEM;
1284
1285 rc = cmd_db_get_aux_data(aggr_vreg->resource_name, buf, len);
1286 if (rc < 0) {
1287 aggr_vreg_err(aggr_vreg, "could not retrieve ARC aux data, rc=%d\n",
1288 rc);
1289 goto done;
1290 } else if (rc != len) {
1291 aggr_vreg_err(aggr_vreg, "could not retrieve all ARC aux data, %d != %d\n",
1292 rc, len);
1293 rc = -EINVAL;
1294 goto done;
1295 }
1296 rc = 0;
1297
1298 aggr_vreg->level_count = len / RPMH_ARC_LEVEL_SIZE;
1299
1300 for (i = 0; i < aggr_vreg->level_count; i++) {
1301 for (j = 0; j < RPMH_ARC_LEVEL_SIZE; j++)
1302 aggr_vreg->level[i] |=
1303 buf[i * RPMH_ARC_LEVEL_SIZE + j] << (8 * j);
1304
1305 /*
1306 * The AUX data may be zero padded. These 0 valued entries at
1307 * the end of the map must be ignored.
1308 */
1309 if (i > 0 && aggr_vreg->level[i] == 0) {
1310 aggr_vreg->level_count = i;
1311 break;
1312 }
1313
1314 /* Add consumer offset to avoid voltage = 0. */
1315 aggr_vreg->level[i] += RPMH_REGULATOR_LEVEL_OFFSET;
1316 aggr_vreg_debug(aggr_vreg, "ARC hlvl=%2d --> vlvl=%4u\n",
1317 i, aggr_vreg->level[i]);
1318 }
1319
1320done:
1321 kfree(buf);
1322 return rc;
1323}
1324
1325/**
1326 * rpmh_regulator_parse_vrm_modes() - parse the supported mode configurations
1327 * for a VRM RPMh resource from device tree
1328 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1329 *
1330 * This function initializes the mode[] array of aggr_vreg based upon the values
1331 * of optional device tree properties.
1332 *
1333 * Return: 0 on success, errno on failure
1334 */
1335static int rpmh_regulator_parse_vrm_modes(struct rpmh_aggr_vreg *aggr_vreg)
1336{
1337 struct device_node *node = aggr_vreg->dev->of_node;
David Collinsf676d3d2017-12-12 18:23:33 -08001338 const char *type = "";
1339 const struct rpmh_regulator_mode *map;
1340 const char *prop;
David Collinse1b43192016-10-12 17:27:22 -07001341 int i, len, rc;
1342 u32 *buf;
1343
David Collinsf676d3d2017-12-12 18:23:33 -08001344 aggr_vreg->regulator_hw_type = RPMH_REGULATOR_HW_TYPE_UNKNOWN;
1345
1346 /* qcom,regulator-type is optional */
1347 prop = "qcom,regulator-type";
1348 if (!of_find_property(node, prop, &len))
1349 return 0;
1350
1351 rc = of_property_read_string(node, prop, &type);
1352 if (rc) {
1353 aggr_vreg_err(aggr_vreg, "unable to read %s, rc=%d\n",
1354 prop, rc);
1355 return rc;
1356 }
1357
1358 if (!strcmp(type, "pmic4-ldo")) {
1359 aggr_vreg->regulator_hw_type
1360 = RPMH_REGULATOR_HW_TYPE_PMIC4_LDO;
1361 } else if (!strcmp(type, "pmic4-hfsmps")) {
1362 aggr_vreg->regulator_hw_type
1363 = RPMH_REGULATOR_HW_TYPE_PMIC4_HFSMPS;
1364 } else if (!strcmp(type, "pmic4-ftsmps")) {
1365 aggr_vreg->regulator_hw_type
1366 = RPMH_REGULATOR_HW_TYPE_PMIC4_FTSMPS;
1367 } else if (!strcmp(type, "pmic4-bob")) {
1368 aggr_vreg->regulator_hw_type
1369 = RPMH_REGULATOR_HW_TYPE_PMIC4_BOB;
1370 } else if (!strcmp(type, "pmic5-ldo")) {
1371 aggr_vreg->regulator_hw_type
1372 = RPMH_REGULATOR_HW_TYPE_PMIC5_LDO;
1373 } else if (!strcmp(type, "pmic5-hfsmps")) {
1374 aggr_vreg->regulator_hw_type
1375 = RPMH_REGULATOR_HW_TYPE_PMIC5_HFSMPS;
1376 } else if (!strcmp(type, "pmic5-ftsmps")) {
1377 aggr_vreg->regulator_hw_type
1378 = RPMH_REGULATOR_HW_TYPE_PMIC5_FTSMPS;
1379 } else if (!strcmp(type, "pmic5-bob")) {
1380 aggr_vreg->regulator_hw_type
1381 = RPMH_REGULATOR_HW_TYPE_PMIC5_BOB;
1382 } else {
1383 aggr_vreg_err(aggr_vreg, "unknown %s = %s\n",
1384 prop, type);
1385 return -EINVAL;
1386 }
1387
1388 map = rpmh_regulator_mode_map[aggr_vreg->regulator_hw_type];
1389
David Collinse1b43192016-10-12 17:27:22 -07001390 /* qcom,supported-modes is optional */
David Collinsf676d3d2017-12-12 18:23:33 -08001391 prop = "qcom,supported-modes";
David Collinse1b43192016-10-12 17:27:22 -07001392 if (!of_find_property(node, prop, &len))
1393 return 0;
1394
1395 len /= sizeof(u32);
1396 aggr_vreg->mode = devm_kcalloc(aggr_vreg->dev, len,
1397 sizeof(*aggr_vreg->mode), GFP_KERNEL);
1398 if (!aggr_vreg->mode)
1399 return -ENOMEM;
1400 aggr_vreg->mode_count = len;
1401
1402
1403 buf = kcalloc(len, sizeof(*buf), GFP_KERNEL);
1404 if (!buf)
1405 return -ENOMEM;
1406
1407 rc = of_property_read_u32_array(node, prop, buf, len);
1408 if (rc) {
1409 aggr_vreg_err(aggr_vreg, "unable to read %s, rc=%d\n",
1410 prop, rc);
1411 goto done;
1412 }
1413
1414 for (i = 0; i < len; i++) {
David Collinsf676d3d2017-12-12 18:23:33 -08001415 if (buf[i] >= RPMH_REGULATOR_MODE_COUNT) {
David Collinse1b43192016-10-12 17:27:22 -07001416 aggr_vreg_err(aggr_vreg, "element %d of %s = %u is invalid\n",
1417 i, prop, buf[i]);
1418 rc = -EINVAL;
1419 goto done;
1420 }
David Collinsf676d3d2017-12-12 18:23:33 -08001421
1422 if (!map[buf[i]].framework_mode) {
1423 aggr_vreg_err(aggr_vreg, "element %d of %s = %u is invalid for regulator type = %s\n",
1424 i, prop, buf[i], type);
1425 rc = -EINVAL;
1426 goto done;
1427 }
1428
1429 aggr_vreg->mode[i].pmic_mode = map[buf[i]].pmic_mode;
1430 aggr_vreg->mode[i].framework_mode = map[buf[i]].framework_mode;
David Collinse1b43192016-10-12 17:27:22 -07001431
1432 if (i > 0 && aggr_vreg->mode[i].pmic_mode
1433 <= aggr_vreg->mode[i - 1].pmic_mode) {
1434 aggr_vreg_err(aggr_vreg, "%s elements are not in ascending order\n",
1435 prop);
1436 rc = -EINVAL;
1437 goto done;
1438 }
1439 }
1440
1441 prop = "qcom,mode-threshold-currents";
1442
1443 rc = of_property_read_u32_array(node, prop, buf, len);
1444 if (rc) {
1445 aggr_vreg_err(aggr_vreg, "unable to read %s, rc=%d\n",
1446 prop, rc);
1447 goto done;
1448 }
1449
1450 for (i = 0; i < len; i++) {
1451 aggr_vreg->mode[i].min_load_ua = buf[i];
1452
1453 if (i > 0 && aggr_vreg->mode[i].min_load_ua
1454 <= aggr_vreg->mode[i - 1].min_load_ua) {
1455 aggr_vreg_err(aggr_vreg, "%s elements are not in ascending order\n",
1456 prop);
1457 rc = -EINVAL;
1458 goto done;
1459 }
1460 }
1461
1462done:
1463 kfree(buf);
1464 return rc;
1465}
1466
1467/**
1468 * rpmh_regulator_allocate_vreg() - allocate space for the regulators associated
1469 * with the RPMh regulator resource and initialize important
1470 * pointers for each regulator
1471 * @aggr_vreg: Pointer to the aggregated rpmh regulator resource
1472 *
1473 * Return: 0 on success, errno on failure
1474 */
1475static int rpmh_regulator_allocate_vreg(struct rpmh_aggr_vreg *aggr_vreg)
1476{
1477 struct device_node *node;
1478 int i, rc;
1479
1480 aggr_vreg->vreg_count = 0;
1481
1482 for_each_available_child_of_node(aggr_vreg->dev->of_node, node) {
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001483 /* Skip child nodes handled by other drivers. */
1484 if (of_find_property(node, "compatible", NULL))
1485 continue;
David Collinse1b43192016-10-12 17:27:22 -07001486 aggr_vreg->vreg_count++;
1487 }
1488
1489 if (aggr_vreg->vreg_count == 0) {
1490 aggr_vreg_err(aggr_vreg, "could not find any regulator subnodes\n");
1491 return -ENODEV;
1492 }
1493
1494 aggr_vreg->vreg = devm_kcalloc(aggr_vreg->dev, aggr_vreg->vreg_count,
1495 sizeof(*aggr_vreg->vreg), GFP_KERNEL);
1496 if (!aggr_vreg->vreg)
1497 return -ENOMEM;
1498
1499 i = 0;
1500 for_each_available_child_of_node(aggr_vreg->dev->of_node, node) {
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001501 /* Skip child nodes handled by other drivers. */
1502 if (of_find_property(node, "compatible", NULL))
1503 continue;
1504
David Collinse1b43192016-10-12 17:27:22 -07001505 aggr_vreg->vreg[i].of_node = node;
1506 aggr_vreg->vreg[i].aggr_vreg = aggr_vreg;
1507
1508 rc = of_property_read_string(node, "regulator-name",
1509 &aggr_vreg->vreg[i].rdesc.name);
1510 if (rc) {
1511 aggr_vreg_err(aggr_vreg, "could not read regulator-name property, rc=%d\n",
1512 rc);
1513 return rc;
1514 }
1515
1516 i++;
1517 }
1518
1519 return 0;
1520}
1521
1522/**
1523 * rpmh_regulator_load_default_parameters() - initialize the RPMh resource
1524 * request for this regulator based on optional device tree
1525 * properties
1526 * @vreg: Pointer to the RPMh regulator
1527 *
1528 * Return: 0 on success, errno on failure
1529 */
1530static int rpmh_regulator_load_default_parameters(struct rpmh_vreg *vreg)
1531{
1532 enum rpmh_regulator_type type = vreg->aggr_vreg->regulator_type;
David Collinsf676d3d2017-12-12 18:23:33 -08001533 const struct rpmh_regulator_mode *map;
David Collinse1b43192016-10-12 17:27:22 -07001534 const char *prop;
1535 int i, rc;
1536 u32 temp;
1537
1538 if (type == RPMH_REGULATOR_TYPE_ARC) {
1539 prop = "qcom,init-voltage-level";
1540 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1541 if (!rc) {
1542 for (i = 0; i < vreg->aggr_vreg->level_count; i++)
1543 if (temp <= vreg->aggr_vreg->level[i])
1544 break;
1545 if (i < vreg->aggr_vreg->level_count) {
1546 rpmh_regulator_set_reg(vreg,
1547 RPMH_REGULATOR_REG_ARC_LEVEL, i);
1548 } else {
1549 vreg_err(vreg, "%s=%u is invalid\n",
1550 prop, temp);
1551 return -EINVAL;
1552 }
1553 }
David Collinsd86a1722017-02-07 15:42:22 -08001554
1555 prop = "qcom,min-dropout-voltage-level";
1556 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1557 if (!rc)
1558 vreg->rdesc.min_dropout_uV = temp;
David Collinse1b43192016-10-12 17:27:22 -07001559 } else if (type == RPMH_REGULATOR_TYPE_VRM) {
1560 prop = "qcom,init-enable";
1561 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1562 if (!rc)
1563 rpmh_regulator_set_reg(vreg,
1564 RPMH_REGULATOR_REG_VRM_ENABLE,
1565 !!temp);
1566
1567 prop = "qcom,init-voltage";
1568 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1569 if (!rc) {
1570 if (temp < RPMH_VRM_MIN_UV || temp > RPMH_VRM_MAX_UV) {
1571 vreg_err(vreg, "%s=%u is invalid\n",
1572 prop, temp);
1573 return -EINVAL;
1574 }
1575 rpmh_regulator_set_reg(vreg,
1576 RPMH_REGULATOR_REG_VRM_VOLTAGE,
1577 temp / 1000);
1578 }
1579
1580 prop = "qcom,init-mode";
1581 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1582 if (!rc) {
David Collinsf676d3d2017-12-12 18:23:33 -08001583 if (temp >= RPMH_REGULATOR_MODE_COUNT) {
David Collinse1b43192016-10-12 17:27:22 -07001584 vreg_err(vreg, "%s=%u is invalid\n",
1585 prop, temp);
1586 return -EINVAL;
David Collinsf676d3d2017-12-12 18:23:33 -08001587 } else if (vreg->aggr_vreg->regulator_hw_type
1588 == RPMH_REGULATOR_HW_TYPE_UNKNOWN) {
1589 vreg_err(vreg, "qcom,regulator-type missing so %s cannot be used\n",
1590 prop);
1591 return -EINVAL;
David Collinse1b43192016-10-12 17:27:22 -07001592 }
David Collinsf676d3d2017-12-12 18:23:33 -08001593
1594 map = rpmh_regulator_mode_map[
1595 vreg->aggr_vreg->regulator_hw_type];
1596 if (!map[temp].framework_mode) {
1597 vreg_err(vreg, "%s=%u is not supported by type = %d\n",
1598 prop, temp,
1599 vreg->aggr_vreg->regulator_hw_type);
1600 return -EINVAL;
1601 }
1602
David Collinse1b43192016-10-12 17:27:22 -07001603 rpmh_regulator_set_reg(vreg,
1604 RPMH_REGULATOR_REG_VRM_MODE,
David Collinsf676d3d2017-12-12 18:23:33 -08001605 map[temp].pmic_mode);
1606 for (i = 0; i < vreg->aggr_vreg->mode_count; i++) {
1607 if (vreg->aggr_vreg->mode[i].pmic_mode
1608 == map[temp].pmic_mode) {
1609 vreg->mode_index = i;
1610 break;
1611 }
1612 }
David Collinse1b43192016-10-12 17:27:22 -07001613 }
1614
1615 prop = "qcom,init-headroom-voltage";
1616 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1617 if (!rc) {
1618 if (temp < RPMH_VRM_HEADROOM_MIN_UV ||
1619 temp > RPMH_VRM_HEADROOM_MAX_UV) {
1620 vreg_err(vreg, "%s=%u is invalid\n",
1621 prop, temp);
1622 return -EINVAL;
1623 }
1624 rpmh_regulator_set_reg(vreg,
1625 RPMH_REGULATOR_REG_VRM_HEADROOM,
1626 temp / 1000);
1627 }
David Collinsd86a1722017-02-07 15:42:22 -08001628
1629 prop = "qcom,min-dropout-voltage";
1630 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1631 if (!rc)
1632 vreg->rdesc.min_dropout_uV = temp;
David Collins3bd8e102017-10-30 16:37:55 -07001633 } else if (type == RPMH_REGULATOR_TYPE_XOB) {
1634 prop = "qcom,init-enable";
1635 rc = of_property_read_u32(vreg->of_node, prop, &temp);
1636 if (!rc)
1637 rpmh_regulator_set_reg(vreg,
1638 RPMH_REGULATOR_REG_XOB_ENABLE,
1639 !!temp);
David Collinse1b43192016-10-12 17:27:22 -07001640 }
1641
1642 return 0;
1643}
1644
1645/**
1646 * rpmh_regulator_init_vreg_supply() - initialize the regulator's parent supply
1647 * mapping based on optional DT parent supply property
1648 * @vreg: Pointer to the RPMh regulator
1649 *
1650 * Return: 0 on success, errno on failure
1651 */
1652static int rpmh_regulator_init_vreg_supply(struct rpmh_vreg *vreg)
1653{
1654 char *buf;
1655 size_t len;
1656
1657 len = strlen(vreg->rdesc.name) + 16;
1658 buf = kzalloc(len, GFP_KERNEL);
1659 if (!buf)
1660 return -ENOMEM;
1661 scnprintf(buf, len, "%s-parent-supply", vreg->rdesc.name);
1662
1663 if (of_find_property(vreg->aggr_vreg->dev->of_node, buf, NULL)) {
1664 kfree(buf);
1665
1666 len = strlen(vreg->rdesc.name) + 10;
1667 buf = devm_kzalloc(vreg->aggr_vreg->dev, len, GFP_KERNEL);
1668 if (!buf)
1669 return -ENOMEM;
1670 scnprintf(buf, len, "%s-parent", vreg->rdesc.name);
1671
1672 vreg->rdesc.supply_name = buf;
1673 } else {
1674 kfree(buf);
1675 }
1676
1677 return 0;
1678}
1679
1680/**
1681 * rpmh_regulator_init_vreg() - initialize all abbributes of an rpmh-regulator
1682 * @vreg: Pointer to the RPMh regulator
1683 *
1684 * Return: 0 on success, errno on failure
1685 */
1686static int rpmh_regulator_init_vreg(struct rpmh_vreg *vreg)
1687{
1688 struct device *dev = vreg->aggr_vreg->dev;
1689 enum rpmh_regulator_type type = vreg->aggr_vreg->regulator_type;
1690 struct regulator_config reg_config = {};
1691 struct regulator_init_data *init_data;
1692 struct regulator_ops *ops;
1693 int rc, i;
1694 u32 set;
1695
1696 ops = devm_kzalloc(dev, sizeof(*ops), GFP_KERNEL);
1697 if (!ops)
1698 return -ENOMEM;
1699
1700 *ops = *rpmh_regulator_ops[type];
1701 vreg->rdesc.owner = THIS_MODULE;
1702 vreg->rdesc.type = REGULATOR_VOLTAGE;
1703 vreg->rdesc.ops = ops;
1704
1705 init_data = of_get_regulator_init_data(dev,
1706 vreg->of_node, &vreg->rdesc);
1707 if (init_data == NULL)
1708 return -ENOMEM;
1709
1710 init_data->constraints.input_uV = init_data->constraints.max_uV;
1711 if (type == RPMH_REGULATOR_TYPE_VRM) {
1712 init_data->constraints.min_uV
1713 = max(init_data->constraints.min_uV, RPMH_VRM_MIN_UV);
1714 init_data->constraints.min_uV
1715 = min(init_data->constraints.min_uV, RPMH_VRM_MAX_UV);
1716 init_data->constraints.max_uV
1717 = max(init_data->constraints.max_uV, RPMH_VRM_MIN_UV);
1718 init_data->constraints.max_uV
1719 = min(init_data->constraints.max_uV, RPMH_VRM_MAX_UV);
1720 }
1721
1722 if (ops->set_voltage || ops->set_voltage_sel)
1723 init_data->constraints.valid_ops_mask
1724 |= REGULATOR_CHANGE_VOLTAGE;
1725
David Collins3bd8e102017-10-30 16:37:55 -07001726 if (type == RPMH_REGULATOR_TYPE_XOB
1727 && init_data->constraints.min_uV == init_data->constraints.max_uV)
1728 vreg->rdesc.fixed_uV = init_data->constraints.min_uV;
1729
David Collinse1b43192016-10-12 17:27:22 -07001730 if (vreg->aggr_vreg->mode_count) {
1731 init_data->constraints.valid_ops_mask
1732 |= REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS;
1733 for (i = 0; i < vreg->aggr_vreg->mode_count; i++)
1734 init_data->constraints.valid_modes_mask
1735 |= vreg->aggr_vreg->mode[i].framework_mode;
1736 } else {
1737 ops->get_mode = NULL;
1738 ops->set_mode = NULL;
1739 ops->set_load = NULL;
1740 }
1741
1742 /*
1743 * Remove enable state control if the ARC resource does not support the
1744 * off level.
1745 */
1746 if (type == RPMH_REGULATOR_TYPE_ARC
1747 && vreg->aggr_vreg->level[0] != RPMH_REGULATOR_LEVEL_OFF) {
1748 ops->enable = NULL;
1749 ops->disable = NULL;
1750 ops->is_enabled = NULL;
1751 }
1752 if (ops->enable)
1753 init_data->constraints.valid_ops_mask
1754 |= REGULATOR_CHANGE_STATUS;
1755
David Collins3bd8e102017-10-30 16:37:55 -07001756 switch (type) {
1757 case RPMH_REGULATOR_TYPE_VRM:
1758 vreg->rdesc.n_voltages = 2;
1759 break;
1760 case RPMH_REGULATOR_TYPE_ARC:
1761 vreg->rdesc.n_voltages = vreg->aggr_vreg->level_count;
1762 break;
1763 case RPMH_REGULATOR_TYPE_XOB:
1764 vreg->rdesc.n_voltages = 1;
1765 break;
1766 default:
1767 return -EINVAL;
1768 }
David Collinse1b43192016-10-12 17:27:22 -07001769
1770 rc = of_property_read_u32(vreg->of_node, "qcom,set", &set);
1771 if (rc) {
1772 vreg_err(vreg, "qcom,set property missing, rc=%d\n", rc);
1773 return rc;
1774 } else if (!(set & RPMH_REGULATOR_SET_ALL)) {
1775 vreg_err(vreg, "qcom,set=%u property is invalid\n", set);
1776 return rc;
1777 }
1778
1779 vreg->set_active = !!(set & RPMH_REGULATOR_SET_ACTIVE);
1780 vreg->set_sleep = !!(set & RPMH_REGULATOR_SET_SLEEP);
1781
1782 rc = rpmh_regulator_init_vreg_supply(vreg);
1783 if (rc) {
1784 vreg_err(vreg, "unable to initialize regulator supply name, rc=%d\n",
1785 rc);
1786 return rc;
1787 }
1788
1789 reg_config.dev = dev;
1790 reg_config.init_data = init_data;
1791 reg_config.of_node = vreg->of_node;
1792 reg_config.driver_data = vreg;
1793
1794 rc = rpmh_regulator_load_default_parameters(vreg);
1795 if (rc) {
1796 vreg_err(vreg, "unable to load default parameters, rc=%d\n",
1797 rc);
1798 return rc;
1799 }
1800
1801 vreg->rdev = devm_regulator_register(dev, &vreg->rdesc, &reg_config);
1802 if (IS_ERR(vreg->rdev)) {
1803 rc = PTR_ERR(vreg->rdev);
1804 vreg->rdev = NULL;
1805 vreg_err(vreg, "devm_regulator_register() failed, rc=%d\n", rc);
1806 return rc;
1807 }
1808
1809 vreg_debug(vreg, "successfully registered; set=%s\n",
1810 vreg->set_active && vreg->set_sleep
1811 ? "active + sleep"
1812 : vreg->set_active ? "active" : "sleep");
1813
1814 return rc;
1815}
1816
1817static const struct of_device_id rpmh_regulator_match_table[] = {
1818 {
1819 .compatible = "qcom,rpmh-vrm-regulator",
1820 .data = (void *)(uintptr_t)RPMH_REGULATOR_TYPE_VRM,
1821 },
1822 {
1823 .compatible = "qcom,rpmh-arc-regulator",
1824 .data = (void *)(uintptr_t)RPMH_REGULATOR_TYPE_ARC,
1825 },
David Collins3bd8e102017-10-30 16:37:55 -07001826 {
1827 .compatible = "qcom,rpmh-xob-regulator",
1828 .data = (void *)(uintptr_t)RPMH_REGULATOR_TYPE_XOB,
1829 },
David Collinse1b43192016-10-12 17:27:22 -07001830 {}
1831};
1832
1833/**
1834 * rpmh_regulator_probe() - probe an aggregated RPMh regulator resource and
1835 * register regulators for each of the regulator nodes associated
1836 * with it
1837 * @pdev: Pointer to the platform device of the aggregated rpmh
1838 * regulator resource
1839 *
1840 * Return: 0 on success, errno on failure
1841 */
1842static int rpmh_regulator_probe(struct platform_device *pdev)
1843{
1844 struct device *dev = &pdev->dev;
1845 const struct of_device_id *match;
1846 struct rpmh_aggr_vreg *aggr_vreg;
1847 struct device_node *node;
1848 int rc, i, sid;
1849
1850 node = dev->of_node;
1851
1852 if (!node) {
1853 dev_err(dev, "Device tree node is missing\n");
1854 return -EINVAL;
1855 }
1856
1857 aggr_vreg = devm_kzalloc(dev, sizeof(*aggr_vreg), GFP_KERNEL);
1858 if (!aggr_vreg)
1859 return -ENOMEM;
1860
1861 aggr_vreg->dev = dev;
1862 mutex_init(&aggr_vreg->lock);
1863
1864 match = of_match_node(rpmh_regulator_match_table, node);
1865 if (match) {
1866 aggr_vreg->regulator_type = (uintptr_t)match->data;
1867 } else {
1868 dev_err(dev, "could not find compatible string match\n");
1869 return -ENODEV;
1870 }
1871
1872 rc = of_property_read_string(node, "qcom,resource-name",
1873 &aggr_vreg->resource_name);
1874 if (rc) {
1875 dev_err(dev, "qcom,resource-name missing in DT node\n");
1876 return rc;
1877 }
1878
1879 aggr_vreg->use_awake_state = of_property_read_bool(node,
1880 "qcom,use-awake-state");
1881
1882 rc = cmd_db_ready();
1883 if (rc) {
1884 if (rc != -EPROBE_DEFER)
1885 aggr_vreg_err(aggr_vreg, "Command DB not available, rc=%d\n",
1886 rc);
1887 return rc;
1888 }
1889
1890 aggr_vreg->addr = cmd_db_get_addr(aggr_vreg->resource_name);
1891 if (!aggr_vreg->addr) {
1892 aggr_vreg_err(aggr_vreg, "could not find RPMh address for resource\n");
1893 return -ENODEV;
1894 }
1895
1896 sid = cmd_db_get_slave_id(aggr_vreg->resource_name);
1897 if (sid < 0) {
1898 aggr_vreg_err(aggr_vreg, "could not find RPMh slave id for resource, rc=%d\n",
1899 sid);
1900 return sid;
1901 }
1902
1903 /* Confirm slave ID listed in command DB matches DT configuration. */
1904 if ((aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
1905 && sid != CMD_DB_HW_ARC)
1906 || (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM
David Collins3bd8e102017-10-30 16:37:55 -07001907 && sid != CMD_DB_HW_VRM)
1908 || (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_XOB
1909 && sid != CMD_DB_HW_XOB)) {
David Collinse1b43192016-10-12 17:27:22 -07001910 aggr_vreg_err(aggr_vreg, "RPMh slave ID mismatch; config=%d (%s) != cmd-db=%d\n",
1911 aggr_vreg->regulator_type,
1912 aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
David Collins3bd8e102017-10-30 16:37:55 -07001913 ? "ARC" : (aggr_vreg->regulator_type
1914 == RPMH_REGULATOR_TYPE_VRM
1915 ? "VRM" : "XOB"),
David Collinse1b43192016-10-12 17:27:22 -07001916 sid);
1917 return -EINVAL;
1918 }
1919
1920 aggr_vreg->rpmh_client = rpmh_get_byindex(pdev, 0);
1921 if (IS_ERR(aggr_vreg->rpmh_client)) {
1922 rc = PTR_ERR(aggr_vreg->rpmh_client);
1923 if (rc != -EPROBE_DEFER)
1924 aggr_vreg_err(aggr_vreg, "failed to request RPMh client, rc=%d\n",
1925 rc);
1926 return rc;
1927 }
1928
1929 if (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC) {
1930 rc = rpmh_regulator_load_arc_level_mapping(aggr_vreg);
1931 if (rc) {
1932 aggr_vreg_err(aggr_vreg, "could not load arc level mapping, rc=%d\n",
1933 rc);
1934 goto free_client;
1935 }
1936 } else if (aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_VRM) {
1937 rc = rpmh_regulator_parse_vrm_modes(aggr_vreg);
1938 if (rc) {
1939 aggr_vreg_err(aggr_vreg, "could not parse vrm mode mapping, rc=%d\n",
1940 rc);
1941 goto free_client;
1942 }
1943 }
1944
1945 aggr_vreg->always_wait_for_ack
1946 = of_property_read_bool(node, "qcom,always-wait-for-ack");
1947
1948 rc = rpmh_regulator_allocate_vreg(aggr_vreg);
1949 if (rc) {
1950 aggr_vreg_err(aggr_vreg, "failed to allocate regulator subnode array, rc=%d\n",
1951 rc);
1952 goto free_client;
1953 }
1954
1955 for (i = 0; i < aggr_vreg->vreg_count; i++) {
1956 rc = rpmh_regulator_init_vreg(&aggr_vreg->vreg[i]);
1957 if (rc) {
1958 pr_err("unable to initialize rpmh-regulator vreg %s for resource %s, rc=%d\n",
1959 aggr_vreg->vreg[i].rdesc.name,
1960 aggr_vreg->resource_name, rc);
1961 goto free_client;
1962 }
1963 }
1964
1965 if (of_property_read_bool(node, "qcom,send-defaults")) {
1966 mutex_lock(&aggr_vreg->lock);
1967 rc = rpmh_regulator_send_aggregate_requests(
1968 &aggr_vreg->vreg[0]);
1969 if (rc) {
1970 aggr_vreg_err(aggr_vreg, "error while sending default request, rc=%d\n",
1971 rc);
1972 mutex_unlock(&aggr_vreg->lock);
1973 goto free_client;
1974 }
1975 mutex_unlock(&aggr_vreg->lock);
1976 }
1977
Ram Chandrasekar641d4412017-05-18 13:53:06 -06001978 of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
David Collinse1b43192016-10-12 17:27:22 -07001979 platform_set_drvdata(pdev, aggr_vreg);
1980
1981 aggr_vreg_debug(aggr_vreg, "successfully probed; addr=0x%05X, type=%s\n",
1982 aggr_vreg->addr,
1983 aggr_vreg->regulator_type == RPMH_REGULATOR_TYPE_ARC
David Collins3bd8e102017-10-30 16:37:55 -07001984 ? "ARC"
1985 : (aggr_vreg->regulator_type
1986 == RPMH_REGULATOR_TYPE_VRM
1987 ? "VRM" : "XOB"));
David Collinse1b43192016-10-12 17:27:22 -07001988
1989 return rc;
1990
1991free_client:
1992 rpmh_release(aggr_vreg->rpmh_client);
1993
1994 return rc;
1995}
1996
1997static int rpmh_regulator_remove(struct platform_device *pdev)
1998{
1999 struct rpmh_aggr_vreg *aggr_vreg = platform_get_drvdata(pdev);
2000
2001 rpmh_release(aggr_vreg->rpmh_client);
2002
2003 return 0;
2004}
2005
2006static struct platform_driver rpmh_regulator_driver = {
2007 .driver = {
2008 .name = "qcom,rpmh-regulator",
2009 .of_match_table = rpmh_regulator_match_table,
2010 .owner = THIS_MODULE,
2011 },
2012 .probe = rpmh_regulator_probe,
2013 .remove = rpmh_regulator_remove,
2014};
2015
2016static int rpmh_regulator_init(void)
2017{
2018 return platform_driver_register(&rpmh_regulator_driver);
2019}
2020
2021static void rpmh_regulator_exit(void)
2022{
2023 platform_driver_unregister(&rpmh_regulator_driver);
2024}
2025
2026MODULE_DESCRIPTION("RPMh regulator driver");
2027MODULE_LICENSE("GPL v2");
2028
2029arch_initcall(rpmh_regulator_init);
2030module_exit(rpmh_regulator_exit);