blob: fdea94c5a016ff4bd3bca910bae285de590c8e07 [file] [log] [blame]
Stephen Boyde92a4042015-06-12 15:47:10 -07001/*
2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/module.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/interrupt.h>
19#include <linux/bitops.h>
20#include <linux/slab.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/platform_device.h>
24#include <linux/ktime.h>
25#include <linux/regulator/driver.h>
26#include <linux/regmap.h>
27#include <linux/list.h>
28
Stephen Boyde2adfac2015-07-17 14:41:55 -070029/* Pin control enable input pins. */
30#define SPMI_REGULATOR_PIN_CTRL_ENABLE_NONE 0x00
31#define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN0 0x01
32#define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN1 0x02
33#define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN2 0x04
34#define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN3 0x08
35#define SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT 0x10
36
37/* Pin control high power mode input pins. */
38#define SPMI_REGULATOR_PIN_CTRL_HPM_NONE 0x00
39#define SPMI_REGULATOR_PIN_CTRL_HPM_EN0 0x01
40#define SPMI_REGULATOR_PIN_CTRL_HPM_EN1 0x02
41#define SPMI_REGULATOR_PIN_CTRL_HPM_EN2 0x04
42#define SPMI_REGULATOR_PIN_CTRL_HPM_EN3 0x08
43#define SPMI_REGULATOR_PIN_CTRL_HPM_SLEEP_B 0x10
44#define SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT 0x20
45
46/*
47 * Used with enable parameters to specify that hardware default register values
48 * should be left unaltered.
49 */
50#define SPMI_REGULATOR_USE_HW_DEFAULT 2
51
52/* Soft start strength of a voltage switch type regulator */
53enum spmi_vs_soft_start_str {
54 SPMI_VS_SOFT_START_STR_0P05_UA = 0,
55 SPMI_VS_SOFT_START_STR_0P25_UA,
56 SPMI_VS_SOFT_START_STR_0P55_UA,
57 SPMI_VS_SOFT_START_STR_0P75_UA,
58 SPMI_VS_SOFT_START_STR_HW_DEFAULT,
59};
60
61/**
62 * struct spmi_regulator_init_data - spmi-regulator initialization data
63 * @pin_ctrl_enable: Bit mask specifying which hardware pins should be
64 * used to enable the regulator, if any
65 * Value should be an ORing of
66 * SPMI_REGULATOR_PIN_CTRL_ENABLE_* constants. If
67 * the bit specified by
68 * SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT is
69 * set, then pin control enable hardware registers
70 * will not be modified.
71 * @pin_ctrl_hpm: Bit mask specifying which hardware pins should be
72 * used to force the regulator into high power
73 * mode, if any
74 * Value should be an ORing of
75 * SPMI_REGULATOR_PIN_CTRL_HPM_* constants. If
76 * the bit specified by
77 * SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT is
78 * set, then pin control mode hardware registers
79 * will not be modified.
80 * @vs_soft_start_strength: This parameter sets the soft start strength for
81 * voltage switch type regulators. Its value
82 * should be one of SPMI_VS_SOFT_START_STR_*. If
83 * its value is SPMI_VS_SOFT_START_STR_HW_DEFAULT,
84 * then the soft start strength will be left at its
85 * default hardware value.
86 */
87struct spmi_regulator_init_data {
88 unsigned pin_ctrl_enable;
89 unsigned pin_ctrl_hpm;
90 enum spmi_vs_soft_start_str vs_soft_start_strength;
91};
92
Stephen Boyde92a4042015-06-12 15:47:10 -070093/* These types correspond to unique register layouts. */
94enum spmi_regulator_logical_type {
95 SPMI_REGULATOR_LOGICAL_TYPE_SMPS,
96 SPMI_REGULATOR_LOGICAL_TYPE_LDO,
97 SPMI_REGULATOR_LOGICAL_TYPE_VS,
98 SPMI_REGULATOR_LOGICAL_TYPE_BOOST,
99 SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS,
100 SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP,
101 SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO,
102 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS,
103 SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS,
104 SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO,
105};
106
107enum spmi_regulator_type {
108 SPMI_REGULATOR_TYPE_BUCK = 0x03,
109 SPMI_REGULATOR_TYPE_LDO = 0x04,
110 SPMI_REGULATOR_TYPE_VS = 0x05,
111 SPMI_REGULATOR_TYPE_BOOST = 0x1b,
112 SPMI_REGULATOR_TYPE_FTS = 0x1c,
113 SPMI_REGULATOR_TYPE_BOOST_BYP = 0x1f,
114 SPMI_REGULATOR_TYPE_ULT_LDO = 0x21,
115 SPMI_REGULATOR_TYPE_ULT_BUCK = 0x22,
116};
117
118enum spmi_regulator_subtype {
119 SPMI_REGULATOR_SUBTYPE_GP_CTL = 0x08,
120 SPMI_REGULATOR_SUBTYPE_RF_CTL = 0x09,
121 SPMI_REGULATOR_SUBTYPE_N50 = 0x01,
122 SPMI_REGULATOR_SUBTYPE_N150 = 0x02,
123 SPMI_REGULATOR_SUBTYPE_N300 = 0x03,
124 SPMI_REGULATOR_SUBTYPE_N600 = 0x04,
125 SPMI_REGULATOR_SUBTYPE_N1200 = 0x05,
126 SPMI_REGULATOR_SUBTYPE_N600_ST = 0x06,
127 SPMI_REGULATOR_SUBTYPE_N1200_ST = 0x07,
128 SPMI_REGULATOR_SUBTYPE_N900_ST = 0x14,
129 SPMI_REGULATOR_SUBTYPE_N300_ST = 0x15,
130 SPMI_REGULATOR_SUBTYPE_P50 = 0x08,
131 SPMI_REGULATOR_SUBTYPE_P150 = 0x09,
132 SPMI_REGULATOR_SUBTYPE_P300 = 0x0a,
133 SPMI_REGULATOR_SUBTYPE_P600 = 0x0b,
134 SPMI_REGULATOR_SUBTYPE_P1200 = 0x0c,
135 SPMI_REGULATOR_SUBTYPE_LN = 0x10,
136 SPMI_REGULATOR_SUBTYPE_LV_P50 = 0x28,
137 SPMI_REGULATOR_SUBTYPE_LV_P150 = 0x29,
138 SPMI_REGULATOR_SUBTYPE_LV_P300 = 0x2a,
139 SPMI_REGULATOR_SUBTYPE_LV_P600 = 0x2b,
140 SPMI_REGULATOR_SUBTYPE_LV_P1200 = 0x2c,
141 SPMI_REGULATOR_SUBTYPE_LV_P450 = 0x2d,
142 SPMI_REGULATOR_SUBTYPE_LV100 = 0x01,
143 SPMI_REGULATOR_SUBTYPE_LV300 = 0x02,
144 SPMI_REGULATOR_SUBTYPE_MV300 = 0x08,
145 SPMI_REGULATOR_SUBTYPE_MV500 = 0x09,
146 SPMI_REGULATOR_SUBTYPE_HDMI = 0x10,
147 SPMI_REGULATOR_SUBTYPE_OTG = 0x11,
148 SPMI_REGULATOR_SUBTYPE_5V_BOOST = 0x01,
149 SPMI_REGULATOR_SUBTYPE_FTS_CTL = 0x08,
150 SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL = 0x09,
151 SPMI_REGULATOR_SUBTYPE_BB_2A = 0x01,
152 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1 = 0x0d,
153 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2 = 0x0e,
154 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3 = 0x0f,
155 SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4 = 0x10,
156};
157
158enum spmi_common_regulator_registers {
159 SPMI_COMMON_REG_DIG_MAJOR_REV = 0x01,
160 SPMI_COMMON_REG_TYPE = 0x04,
161 SPMI_COMMON_REG_SUBTYPE = 0x05,
162 SPMI_COMMON_REG_VOLTAGE_RANGE = 0x40,
163 SPMI_COMMON_REG_VOLTAGE_SET = 0x41,
164 SPMI_COMMON_REG_MODE = 0x45,
165 SPMI_COMMON_REG_ENABLE = 0x46,
166 SPMI_COMMON_REG_PULL_DOWN = 0x48,
167 SPMI_COMMON_REG_SOFT_START = 0x4c,
168 SPMI_COMMON_REG_STEP_CTRL = 0x61,
169};
170
171enum spmi_vs_registers {
172 SPMI_VS_REG_OCP = 0x4a,
173 SPMI_VS_REG_SOFT_START = 0x4c,
174};
175
176enum spmi_boost_registers {
177 SPMI_BOOST_REG_CURRENT_LIMIT = 0x4a,
178};
179
180enum spmi_boost_byp_registers {
181 SPMI_BOOST_BYP_REG_CURRENT_LIMIT = 0x4b,
182};
183
184/* Used for indexing into ctrl_reg. These are offets from 0x40 */
185enum spmi_common_control_register_index {
186 SPMI_COMMON_IDX_VOLTAGE_RANGE = 0,
187 SPMI_COMMON_IDX_VOLTAGE_SET = 1,
188 SPMI_COMMON_IDX_MODE = 5,
189 SPMI_COMMON_IDX_ENABLE = 6,
190};
191
192/* Common regulator control register layout */
193#define SPMI_COMMON_ENABLE_MASK 0x80
194#define SPMI_COMMON_ENABLE 0x80
195#define SPMI_COMMON_DISABLE 0x00
196#define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
197#define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
198#define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
199#define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
200#define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0f
201
202/* Common regulator mode register layout */
203#define SPMI_COMMON_MODE_HPM_MASK 0x80
204#define SPMI_COMMON_MODE_AUTO_MASK 0x40
205#define SPMI_COMMON_MODE_BYPASS_MASK 0x20
206#define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
207#define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
208#define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
209#define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
210#define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
211#define SPMI_COMMON_MODE_FOLLOW_ALL_MASK 0x1f
212
213/* Common regulator pull down control register layout */
214#define SPMI_COMMON_PULL_DOWN_ENABLE_MASK 0x80
215
216/* LDO regulator current limit control register layout */
217#define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
218
219/* LDO regulator soft start control register layout */
220#define SPMI_LDO_SOFT_START_ENABLE_MASK 0x80
221
222/* VS regulator over current protection control register layout */
223#define SPMI_VS_OCP_OVERRIDE 0x01
224#define SPMI_VS_OCP_NO_OVERRIDE 0x00
225
226/* VS regulator soft start control register layout */
227#define SPMI_VS_SOFT_START_ENABLE_MASK 0x80
228#define SPMI_VS_SOFT_START_SEL_MASK 0x03
229
230/* Boost regulator current limit control register layout */
231#define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
232#define SPMI_BOOST_CURRENT_LIMIT_MASK 0x07
233
234#define SPMI_VS_OCP_DEFAULT_MAX_RETRIES 10
235#define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS 30
236#define SPMI_VS_OCP_FALL_DELAY_US 90
237#define SPMI_VS_OCP_FAULT_DELAY_US 20000
238
239#define SPMI_FTSMPS_STEP_CTRL_STEP_MASK 0x18
240#define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT 3
241#define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK 0x07
242#define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT 0
243
244/* Clock rate in kHz of the FTSMPS regulator reference clock. */
245#define SPMI_FTSMPS_CLOCK_RATE 19200
246
247/* Minimum voltage stepper delay for each step. */
248#define SPMI_FTSMPS_STEP_DELAY 8
Stephen Boyd2cf7b992016-03-30 18:57:49 -0700249#define SPMI_DEFAULT_STEP_DELAY 20
Stephen Boyde92a4042015-06-12 15:47:10 -0700250
251/*
252 * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to
253 * adjust the step rate in order to account for oscillator variance.
254 */
255#define SPMI_FTSMPS_STEP_MARGIN_NUM 4
256#define SPMI_FTSMPS_STEP_MARGIN_DEN 5
257
258/*
259 * This voltage in uV is returned by get_voltage functions when there is no way
260 * to determine the current voltage level. It is needed because the regulator
261 * framework treats a 0 uV voltage as an error.
262 */
263#define VOLTAGE_UNKNOWN 1
264
265/* VSET value to decide the range of ULT SMPS */
266#define ULT_SMPS_RANGE_SPLIT 0x60
267
268/**
269 * struct spmi_voltage_range - regulator set point voltage mapping description
270 * @min_uV: Minimum programmable output voltage resulting from
271 * set point register value 0x00
272 * @max_uV: Maximum programmable output voltage
273 * @step_uV: Output voltage increase resulting from the set point
274 * register value increasing by 1
275 * @set_point_min_uV: Minimum allowed voltage
276 * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order
277 * to pick which range should be used in the case of
278 * overlapping set points.
279 * @n_voltages: Number of preferred voltage set points present in this
280 * range
281 * @range_sel: Voltage range register value corresponding to this range
282 *
283 * The following relationships must be true for the values used in this struct:
284 * (max_uV - min_uV) % step_uV == 0
285 * (set_point_min_uV - min_uV) % step_uV == 0*
286 * (set_point_max_uV - min_uV) % step_uV == 0*
287 * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
288 *
289 * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
290 * specify that the voltage range has meaning, but is not preferred.
291 */
292struct spmi_voltage_range {
293 int min_uV;
294 int max_uV;
295 int step_uV;
296 int set_point_min_uV;
297 int set_point_max_uV;
298 unsigned n_voltages;
299 u8 range_sel;
300};
301
302/*
303 * The ranges specified in the spmi_voltage_set_points struct must be listed
304 * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
305 */
306struct spmi_voltage_set_points {
307 struct spmi_voltage_range *range;
308 int count;
309 unsigned n_voltages;
310};
311
312struct spmi_regulator {
313 struct regulator_desc desc;
314 struct device *dev;
315 struct delayed_work ocp_work;
316 struct regmap *regmap;
317 struct spmi_voltage_set_points *set_points;
318 enum spmi_regulator_logical_type logical_type;
319 int ocp_irq;
320 int ocp_count;
321 int ocp_max_retries;
322 int ocp_retry_delay_ms;
323 int hpm_min_load;
324 int slew_rate;
325 ktime_t vs_enable_time;
326 u16 base;
327 struct list_head node;
328};
329
330struct spmi_regulator_mapping {
331 enum spmi_regulator_type type;
332 enum spmi_regulator_subtype subtype;
333 enum spmi_regulator_logical_type logical_type;
334 u32 revision_min;
335 u32 revision_max;
336 struct regulator_ops *ops;
337 struct spmi_voltage_set_points *set_points;
338 int hpm_min_load;
339};
340
341struct spmi_regulator_data {
342 const char *name;
343 u16 base;
344 const char *supply;
345 const char *ocp;
346 u16 force_type;
347};
348
349#define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \
350 _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
351 { \
352 .type = SPMI_REGULATOR_TYPE_##_type, \
353 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
354 .revision_min = _dig_major_min, \
355 .revision_max = _dig_major_max, \
356 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \
357 .ops = &spmi_##_ops_val##_ops, \
358 .set_points = &_set_points_val##_set_points, \
359 .hpm_min_load = _hpm_min_load, \
360 }
361
362#define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \
363 { \
364 .type = SPMI_REGULATOR_TYPE_VS, \
365 .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
366 .revision_min = _dig_major_min, \
367 .revision_max = _dig_major_max, \
368 .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_VS, \
369 .ops = &spmi_vs_ops, \
370 }
371
372#define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
373 _set_point_max_uV, _max_uV, _step_uV) \
374 { \
375 .min_uV = _min_uV, \
376 .max_uV = _max_uV, \
377 .set_point_min_uV = _set_point_min_uV, \
378 .set_point_max_uV = _set_point_max_uV, \
379 .step_uV = _step_uV, \
380 .range_sel = _range_sel, \
381 }
382
383#define DEFINE_SPMI_SET_POINTS(name) \
384struct spmi_voltage_set_points name##_set_points = { \
385 .range = name##_ranges, \
386 .count = ARRAY_SIZE(name##_ranges), \
387}
388
389/*
390 * These tables contain the physically available PMIC regulator voltage setpoint
391 * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
392 * to ensure that the setpoints available to software are monotonically
393 * increasing and unique. The set_voltage callback functions expect these
394 * properties to hold.
395 */
396static struct spmi_voltage_range pldo_ranges[] = {
397 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
398 SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
399 SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
400};
401
402static struct spmi_voltage_range nldo1_ranges[] = {
403 SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
404};
405
406static struct spmi_voltage_range nldo2_ranges[] = {
407 SPMI_VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500),
408 SPMI_VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250),
409 SPMI_VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500),
410};
411
412static struct spmi_voltage_range nldo3_ranges[] = {
413 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
414 SPMI_VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500),
415 SPMI_VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500),
416};
417
418static struct spmi_voltage_range ln_ldo_ranges[] = {
419 SPMI_VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000),
420 SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
421};
422
423static struct spmi_voltage_range smps_ranges[] = {
424 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
425 SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
426};
427
428static struct spmi_voltage_range ftsmps_ranges[] = {
429 SPMI_VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000),
430 SPMI_VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000),
431};
432
433static struct spmi_voltage_range ftsmps2p5_ranges[] = {
434 SPMI_VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000),
435 SPMI_VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000),
436};
437
438static struct spmi_voltage_range boost_ranges[] = {
439 SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
440};
441
442static struct spmi_voltage_range boost_byp_ranges[] = {
443 SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
444};
445
446static struct spmi_voltage_range ult_lo_smps_ranges[] = {
447 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
448 SPMI_VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000),
449};
450
451static struct spmi_voltage_range ult_ho_smps_ranges[] = {
452 SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
453};
454
455static struct spmi_voltage_range ult_nldo_ranges[] = {
456 SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
457};
458
459static struct spmi_voltage_range ult_pldo_ranges[] = {
460 SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
461};
462
463static DEFINE_SPMI_SET_POINTS(pldo);
464static DEFINE_SPMI_SET_POINTS(nldo1);
465static DEFINE_SPMI_SET_POINTS(nldo2);
466static DEFINE_SPMI_SET_POINTS(nldo3);
467static DEFINE_SPMI_SET_POINTS(ln_ldo);
468static DEFINE_SPMI_SET_POINTS(smps);
469static DEFINE_SPMI_SET_POINTS(ftsmps);
470static DEFINE_SPMI_SET_POINTS(ftsmps2p5);
471static DEFINE_SPMI_SET_POINTS(boost);
472static DEFINE_SPMI_SET_POINTS(boost_byp);
473static DEFINE_SPMI_SET_POINTS(ult_lo_smps);
474static DEFINE_SPMI_SET_POINTS(ult_ho_smps);
475static DEFINE_SPMI_SET_POINTS(ult_nldo);
476static DEFINE_SPMI_SET_POINTS(ult_pldo);
477
478static inline int spmi_vreg_read(struct spmi_regulator *vreg, u16 addr, u8 *buf,
479 int len)
480{
481 return regmap_bulk_read(vreg->regmap, vreg->base + addr, buf, len);
482}
483
484static inline int spmi_vreg_write(struct spmi_regulator *vreg, u16 addr,
485 u8 *buf, int len)
486{
487 return regmap_bulk_write(vreg->regmap, vreg->base + addr, buf, len);
488}
489
490static int spmi_vreg_update_bits(struct spmi_regulator *vreg, u16 addr, u8 val,
491 u8 mask)
492{
493 return regmap_update_bits(vreg->regmap, vreg->base + addr, mask, val);
494}
495
496static int spmi_regulator_common_is_enabled(struct regulator_dev *rdev)
497{
498 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
499 u8 reg;
500
501 spmi_vreg_read(vreg, SPMI_COMMON_REG_ENABLE, &reg, 1);
502
503 return (reg & SPMI_COMMON_ENABLE_MASK) == SPMI_COMMON_ENABLE;
504}
505
506static int spmi_regulator_common_enable(struct regulator_dev *rdev)
507{
508 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
509
510 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
511 SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK);
512}
513
514static int spmi_regulator_vs_enable(struct regulator_dev *rdev)
515{
516 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
517
518 if (vreg->ocp_irq) {
519 vreg->ocp_count = 0;
520 vreg->vs_enable_time = ktime_get();
521 }
522
523 return spmi_regulator_common_enable(rdev);
524}
525
Stephen Boyde2adfac2015-07-17 14:41:55 -0700526static int spmi_regulator_vs_ocp(struct regulator_dev *rdev)
527{
528 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
529 u8 reg = SPMI_VS_OCP_OVERRIDE;
530
531 return spmi_vreg_write(vreg, SPMI_VS_REG_OCP, &reg, 1);
532}
533
Stephen Boyde92a4042015-06-12 15:47:10 -0700534static int spmi_regulator_common_disable(struct regulator_dev *rdev)
535{
536 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
537
538 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
539 SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK);
540}
541
542static int spmi_regulator_select_voltage(struct spmi_regulator *vreg,
543 int min_uV, int max_uV, u8 *range_sel, u8 *voltage_sel,
544 unsigned *selector)
545{
546 const struct spmi_voltage_range *range;
547 int uV = min_uV;
548 int lim_min_uV, lim_max_uV, i, range_id, range_max_uV;
549
550 /* Check if request voltage is outside of physically settable range. */
551 lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
552 lim_max_uV =
553 vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV;
554
555 if (uV < lim_min_uV && max_uV >= lim_min_uV)
556 uV = lim_min_uV;
557
558 if (uV < lim_min_uV || uV > lim_max_uV) {
559 dev_err(vreg->dev,
560 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
561 min_uV, max_uV, lim_min_uV, lim_max_uV);
562 return -EINVAL;
563 }
564
565 /* Find the range which uV is inside of. */
566 for (i = vreg->set_points->count - 1; i > 0; i--) {
567 range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV;
568 if (uV > range_max_uV && range_max_uV > 0)
569 break;
570 }
571
572 range_id = i;
573 range = &vreg->set_points->range[range_id];
574 *range_sel = range->range_sel;
575
576 /*
577 * Force uV to be an allowed set point by applying a ceiling function to
578 * the uV value.
579 */
Axel Lin5d506a52015-07-09 16:48:50 +0800580 *voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
Stephen Boyde92a4042015-06-12 15:47:10 -0700581 uV = *voltage_sel * range->step_uV + range->min_uV;
582
583 if (uV > max_uV) {
584 dev_err(vreg->dev,
585 "request v=[%d, %d] cannot be met by any set point; "
586 "next set point: %d\n",
587 min_uV, max_uV, uV);
588 return -EINVAL;
589 }
590
591 *selector = 0;
592 for (i = 0; i < range_id; i++)
593 *selector += vreg->set_points->range[i].n_voltages;
594 *selector += (uV - range->set_point_min_uV) / range->step_uV;
595
596 return 0;
597}
598
599static const struct spmi_voltage_range *
600spmi_regulator_find_range(struct spmi_regulator *vreg)
601{
602 u8 range_sel;
603 const struct spmi_voltage_range *range, *end;
604
605 range = vreg->set_points->range;
606 end = range + vreg->set_points->count;
607
608 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, &range_sel, 1);
609
610 for (; range < end; range++)
611 if (range->range_sel == range_sel)
612 return range;
613
614 return NULL;
615}
616
617static int spmi_regulator_select_voltage_same_range(struct spmi_regulator *vreg,
618 int min_uV, int max_uV, u8 *range_sel, u8 *voltage_sel,
619 unsigned *selector)
620{
621 const struct spmi_voltage_range *range;
622 int uV = min_uV;
623 int i;
624
625 range = spmi_regulator_find_range(vreg);
626 if (!range)
627 goto different_range;
628
629 if (uV < range->min_uV && max_uV >= range->min_uV)
630 uV = range->min_uV;
631
632 if (uV < range->min_uV || uV > range->max_uV) {
633 /* Current range doesn't support the requested voltage. */
634 goto different_range;
635 }
636
637 /*
638 * Force uV to be an allowed set point by applying a ceiling function to
639 * the uV value.
640 */
641 *voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
642 uV = *voltage_sel * range->step_uV + range->min_uV;
643
644 if (uV > max_uV) {
645 /*
646 * No set point in the current voltage range is within the
647 * requested min_uV to max_uV range.
648 */
649 goto different_range;
650 }
651
652 *selector = 0;
653 for (i = 0; i < vreg->set_points->count; i++) {
654 if (uV >= vreg->set_points->range[i].set_point_min_uV
Stephen Boyd9b2dfee2015-06-16 11:11:22 -0700655 && uV <= vreg->set_points->range[i].set_point_max_uV) {
Stephen Boyde92a4042015-06-12 15:47:10 -0700656 *selector +=
657 (uV - vreg->set_points->range[i].set_point_min_uV)
658 / vreg->set_points->range[i].step_uV;
659 break;
Stephen Boyd9b2dfee2015-06-16 11:11:22 -0700660 }
Stephen Boyde92a4042015-06-12 15:47:10 -0700661
662 *selector += vreg->set_points->range[i].n_voltages;
663 }
664
665 if (*selector >= vreg->set_points->n_voltages)
666 goto different_range;
667
668 return 0;
669
670different_range:
671 return spmi_regulator_select_voltage(vreg, min_uV, max_uV,
672 range_sel, voltage_sel, selector);
673}
674
675static int spmi_regulator_common_set_voltage(struct regulator_dev *rdev,
676 int min_uV, int max_uV, unsigned *selector)
677{
678 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
679 int ret;
680 u8 buf[2];
681 u8 range_sel, voltage_sel;
682
683 /*
684 * Favor staying in the current voltage range if possible. This avoids
685 * voltage spikes that occur when changing the voltage range.
686 */
687 ret = spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV,
688 &range_sel, &voltage_sel, selector);
689 if (ret)
690 return ret;
691
692 buf[0] = range_sel;
693 buf[1] = voltage_sel;
694 return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, buf, 2);
695}
696
697static int spmi_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
698 unsigned int old_selector, unsigned int new_selector)
699{
700 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
701 const struct spmi_voltage_range *range;
702 int diff_uV;
703
704 range = spmi_regulator_find_range(vreg);
705 if (!range)
706 return -EINVAL;
707
708 diff_uV = abs(new_selector - old_selector) * range->step_uV;
709
710 return DIV_ROUND_UP(diff_uV, vreg->slew_rate);
711}
712
713static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev)
714{
715 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
716 const struct spmi_voltage_range *range;
717 u8 voltage_sel;
718
719 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
720
721 range = spmi_regulator_find_range(vreg);
722 if (!range)
723 return VOLTAGE_UNKNOWN;
724
725 return range->step_uV * voltage_sel + range->min_uV;
726}
727
728static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev,
729 int min_uV, int max_uV, unsigned *selector)
730{
731 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
732 int ret;
733 u8 range_sel, sel;
734
735 ret = spmi_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
736 &sel, selector);
737 if (ret) {
738 dev_err(vreg->dev, "could not set voltage, ret=%d\n", ret);
739 return ret;
740 }
741
742 /*
743 * Certain types of regulators do not have a range select register so
744 * only voltage set register needs to be written.
745 */
746 return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1);
747}
748
749static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev)
750{
751 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
752 const struct spmi_voltage_range *range = vreg->set_points->range;
753 u8 voltage_sel;
754
755 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
756
757 return range->step_uV * voltage_sel + range->min_uV;
758}
759
760static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev,
761 int min_uV, int max_uV, unsigned *selector)
762{
763 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
764 int ret;
765 u8 range_sel, voltage_sel;
766
767 /*
768 * Favor staying in the current voltage range if possible. This avoids
769 * voltage spikes that occur when changing the voltage range.
770 */
771 ret = spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV,
772 &range_sel, &voltage_sel, selector);
773 if (ret)
774 return ret;
775
776 /*
777 * Calculate VSET based on range
778 * In case of range 0: voltage_sel is a 7 bit value, can be written
779 * witout any modification.
780 * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
781 * [011].
782 */
783 if (range_sel == 1)
784 voltage_sel |= ULT_SMPS_RANGE_SPLIT;
785
Julia Lawall0f94bff2015-06-16 06:45:14 -0700786 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET,
Stephen Boyde92a4042015-06-12 15:47:10 -0700787 voltage_sel, 0xff);
Stephen Boyde92a4042015-06-12 15:47:10 -0700788}
789
790static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev)
791{
792 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
793 const struct spmi_voltage_range *range;
794 u8 voltage_sel;
795
796 spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
797
798 range = spmi_regulator_find_range(vreg);
799 if (!range)
800 return VOLTAGE_UNKNOWN;
801
802 if (range->range_sel == 1)
803 voltage_sel &= ~ULT_SMPS_RANGE_SPLIT;
804
805 return range->step_uV * voltage_sel + range->min_uV;
806}
807
808static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev,
809 unsigned selector)
810{
811 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
812 int uV = 0;
813 int i;
814
815 if (selector >= vreg->set_points->n_voltages)
816 return 0;
817
818 for (i = 0; i < vreg->set_points->count; i++) {
Stephen Boyd9b2dfee2015-06-16 11:11:22 -0700819 if (selector < vreg->set_points->range[i].n_voltages) {
Stephen Boyde92a4042015-06-12 15:47:10 -0700820 uV = selector * vreg->set_points->range[i].step_uV
821 + vreg->set_points->range[i].set_point_min_uV;
822 break;
Stephen Boyd9b2dfee2015-06-16 11:11:22 -0700823 }
Stephen Boyde92a4042015-06-12 15:47:10 -0700824
825 selector -= vreg->set_points->range[i].n_voltages;
826 }
827
828 return uV;
829}
830
831static int
832spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable)
833{
834 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
835 u8 mask = SPMI_COMMON_MODE_BYPASS_MASK;
836 u8 val = 0;
837
838 if (enable)
839 val = mask;
840
841 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
842}
843
844static int
845spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable)
846{
847 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
848 u8 val;
849 int ret;
850
851 ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1);
852 *enable = val & SPMI_COMMON_MODE_BYPASS_MASK;
853
854 return ret;
855}
856
857static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev)
858{
859 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
860 u8 reg;
861
862 spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &reg, 1);
863
864 if (reg & SPMI_COMMON_MODE_HPM_MASK)
865 return REGULATOR_MODE_NORMAL;
866
Stephen Boyde2adfac2015-07-17 14:41:55 -0700867 if (reg & SPMI_COMMON_MODE_AUTO_MASK)
868 return REGULATOR_MODE_FAST;
869
Stephen Boyde92a4042015-06-12 15:47:10 -0700870 return REGULATOR_MODE_IDLE;
871}
872
873static int
874spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode)
875{
876 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
Stephen Boyde2adfac2015-07-17 14:41:55 -0700877 u8 mask = SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK;
Stephen Boyde92a4042015-06-12 15:47:10 -0700878 u8 val = 0;
879
880 if (mode == REGULATOR_MODE_NORMAL)
Stephen Boyde2adfac2015-07-17 14:41:55 -0700881 val = SPMI_COMMON_MODE_HPM_MASK;
882 else if (mode == REGULATOR_MODE_FAST)
883 val = SPMI_COMMON_MODE_AUTO_MASK;
Stephen Boyde92a4042015-06-12 15:47:10 -0700884
885 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
886}
887
888static int
889spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA)
890{
891 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
892 unsigned int mode;
893
894 if (load_uA >= vreg->hpm_min_load)
895 mode = REGULATOR_MODE_NORMAL;
896 else
897 mode = REGULATOR_MODE_IDLE;
898
899 return spmi_regulator_common_set_mode(rdev, mode);
900}
901
902static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev)
903{
904 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
905 unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK;
906
907 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN,
908 mask, mask);
909}
910
911static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev)
912{
913 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
914 unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK;
915
916 return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START,
917 mask, mask);
918}
919
920static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA)
921{
922 struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
923 enum spmi_regulator_logical_type type = vreg->logical_type;
924 unsigned int current_reg;
925 u8 reg;
926 u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK |
927 SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
928 int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500;
929
930 if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST)
931 current_reg = SPMI_BOOST_REG_CURRENT_LIMIT;
932 else
933 current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT;
934
935 if (ilim_uA > max || ilim_uA <= 0)
936 return -EINVAL;
937
938 reg = (ilim_uA - 1) / 500;
939 reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
940
941 return spmi_vreg_update_bits(vreg, current_reg, reg, mask);
942}
943
944static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg)
945{
946 int ret;
947
948 ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
949 SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK);
950
951 vreg->vs_enable_time = ktime_get();
952
953 ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
954 SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK);
955
956 return ret;
957}
958
959static void spmi_regulator_vs_ocp_work(struct work_struct *work)
960{
961 struct delayed_work *dwork = to_delayed_work(work);
962 struct spmi_regulator *vreg
963 = container_of(dwork, struct spmi_regulator, ocp_work);
964
965 spmi_regulator_vs_clear_ocp(vreg);
966}
967
968static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data)
969{
970 struct spmi_regulator *vreg = data;
971 ktime_t ocp_irq_time;
972 s64 ocp_trigger_delay_us;
973
974 ocp_irq_time = ktime_get();
975 ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
976 vreg->vs_enable_time);
977
978 /*
979 * Reset the OCP count if there is a large delay between switch enable
980 * and when OCP triggers. This is indicative of a hotplug event as
981 * opposed to a fault.
982 */
983 if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US)
984 vreg->ocp_count = 0;
985
986 /* Wait for switch output to settle back to 0 V after OCP triggered. */
987 udelay(SPMI_VS_OCP_FALL_DELAY_US);
988
989 vreg->ocp_count++;
990
991 if (vreg->ocp_count == 1) {
992 /* Immediately clear the over current condition. */
993 spmi_regulator_vs_clear_ocp(vreg);
994 } else if (vreg->ocp_count <= vreg->ocp_max_retries) {
995 /* Schedule the over current clear task to run later. */
996 schedule_delayed_work(&vreg->ocp_work,
997 msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
998 } else {
999 dev_err(vreg->dev,
1000 "OCP triggered %d times; no further retries\n",
1001 vreg->ocp_count);
1002 }
1003
1004 return IRQ_HANDLED;
1005}
1006
1007static struct regulator_ops spmi_smps_ops = {
1008 .enable = spmi_regulator_common_enable,
1009 .disable = spmi_regulator_common_disable,
1010 .is_enabled = spmi_regulator_common_is_enabled,
1011 .set_voltage = spmi_regulator_common_set_voltage,
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001012 .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
Stephen Boyde92a4042015-06-12 15:47:10 -07001013 .get_voltage = spmi_regulator_common_get_voltage,
1014 .list_voltage = spmi_regulator_common_list_voltage,
1015 .set_mode = spmi_regulator_common_set_mode,
1016 .get_mode = spmi_regulator_common_get_mode,
1017 .set_load = spmi_regulator_common_set_load,
1018 .set_pull_down = spmi_regulator_common_set_pull_down,
1019};
1020
1021static struct regulator_ops spmi_ldo_ops = {
1022 .enable = spmi_regulator_common_enable,
1023 .disable = spmi_regulator_common_disable,
1024 .is_enabled = spmi_regulator_common_is_enabled,
1025 .set_voltage = spmi_regulator_common_set_voltage,
1026 .get_voltage = spmi_regulator_common_get_voltage,
1027 .list_voltage = spmi_regulator_common_list_voltage,
1028 .set_mode = spmi_regulator_common_set_mode,
1029 .get_mode = spmi_regulator_common_get_mode,
1030 .set_load = spmi_regulator_common_set_load,
1031 .set_bypass = spmi_regulator_common_set_bypass,
1032 .get_bypass = spmi_regulator_common_get_bypass,
1033 .set_pull_down = spmi_regulator_common_set_pull_down,
1034 .set_soft_start = spmi_regulator_common_set_soft_start,
1035};
1036
1037static struct regulator_ops spmi_ln_ldo_ops = {
1038 .enable = spmi_regulator_common_enable,
1039 .disable = spmi_regulator_common_disable,
1040 .is_enabled = spmi_regulator_common_is_enabled,
1041 .set_voltage = spmi_regulator_common_set_voltage,
1042 .get_voltage = spmi_regulator_common_get_voltage,
1043 .list_voltage = spmi_regulator_common_list_voltage,
1044 .set_bypass = spmi_regulator_common_set_bypass,
1045 .get_bypass = spmi_regulator_common_get_bypass,
1046};
1047
1048static struct regulator_ops spmi_vs_ops = {
1049 .enable = spmi_regulator_vs_enable,
1050 .disable = spmi_regulator_common_disable,
1051 .is_enabled = spmi_regulator_common_is_enabled,
1052 .set_pull_down = spmi_regulator_common_set_pull_down,
1053 .set_soft_start = spmi_regulator_common_set_soft_start,
Stephen Boyde2adfac2015-07-17 14:41:55 -07001054 .set_over_current_protection = spmi_regulator_vs_ocp,
Stephen Boyde92a4042015-06-12 15:47:10 -07001055};
1056
1057static struct regulator_ops spmi_boost_ops = {
1058 .enable = spmi_regulator_common_enable,
1059 .disable = spmi_regulator_common_disable,
1060 .is_enabled = spmi_regulator_common_is_enabled,
1061 .set_voltage = spmi_regulator_single_range_set_voltage,
1062 .get_voltage = spmi_regulator_single_range_get_voltage,
1063 .list_voltage = spmi_regulator_common_list_voltage,
1064 .set_input_current_limit = spmi_regulator_set_ilim,
1065};
1066
1067static struct regulator_ops spmi_ftsmps_ops = {
1068 .enable = spmi_regulator_common_enable,
1069 .disable = spmi_regulator_common_disable,
1070 .is_enabled = spmi_regulator_common_is_enabled,
1071 .set_voltage = spmi_regulator_common_set_voltage,
1072 .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
1073 .get_voltage = spmi_regulator_common_get_voltage,
1074 .list_voltage = spmi_regulator_common_list_voltage,
1075 .set_mode = spmi_regulator_common_set_mode,
1076 .get_mode = spmi_regulator_common_get_mode,
1077 .set_load = spmi_regulator_common_set_load,
1078 .set_pull_down = spmi_regulator_common_set_pull_down,
1079};
1080
1081static struct regulator_ops spmi_ult_lo_smps_ops = {
1082 .enable = spmi_regulator_common_enable,
1083 .disable = spmi_regulator_common_disable,
1084 .is_enabled = spmi_regulator_common_is_enabled,
1085 .set_voltage = spmi_regulator_ult_lo_smps_set_voltage,
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001086 .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
Stephen Boyde92a4042015-06-12 15:47:10 -07001087 .get_voltage = spmi_regulator_ult_lo_smps_get_voltage,
1088 .list_voltage = spmi_regulator_common_list_voltage,
1089 .set_mode = spmi_regulator_common_set_mode,
1090 .get_mode = spmi_regulator_common_get_mode,
1091 .set_load = spmi_regulator_common_set_load,
1092 .set_pull_down = spmi_regulator_common_set_pull_down,
1093};
1094
1095static struct regulator_ops spmi_ult_ho_smps_ops = {
1096 .enable = spmi_regulator_common_enable,
1097 .disable = spmi_regulator_common_disable,
1098 .is_enabled = spmi_regulator_common_is_enabled,
1099 .set_voltage = spmi_regulator_single_range_set_voltage,
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001100 .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
Stephen Boyde92a4042015-06-12 15:47:10 -07001101 .get_voltage = spmi_regulator_single_range_get_voltage,
1102 .list_voltage = spmi_regulator_common_list_voltage,
1103 .set_mode = spmi_regulator_common_set_mode,
1104 .get_mode = spmi_regulator_common_get_mode,
1105 .set_load = spmi_regulator_common_set_load,
1106 .set_pull_down = spmi_regulator_common_set_pull_down,
1107};
1108
1109static struct regulator_ops spmi_ult_ldo_ops = {
1110 .enable = spmi_regulator_common_enable,
1111 .disable = spmi_regulator_common_disable,
1112 .is_enabled = spmi_regulator_common_is_enabled,
1113 .set_voltage = spmi_regulator_single_range_set_voltage,
1114 .get_voltage = spmi_regulator_single_range_get_voltage,
1115 .list_voltage = spmi_regulator_common_list_voltage,
1116 .set_mode = spmi_regulator_common_set_mode,
1117 .get_mode = spmi_regulator_common_get_mode,
1118 .set_load = spmi_regulator_common_set_load,
1119 .set_bypass = spmi_regulator_common_set_bypass,
1120 .get_bypass = spmi_regulator_common_get_bypass,
1121 .set_pull_down = spmi_regulator_common_set_pull_down,
1122 .set_soft_start = spmi_regulator_common_set_soft_start,
1123};
1124
1125/* Maximum possible digital major revision value */
1126#define INF 0xFF
1127
1128static const struct spmi_regulator_mapping supported_regulators[] = {
1129 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
1130 SPMI_VREG(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
1131 SPMI_VREG(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
1132 SPMI_VREG(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
1133 SPMI_VREG(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
1134 SPMI_VREG(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
1135 SPMI_VREG(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
1136 SPMI_VREG(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000),
1137 SPMI_VREG(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000),
1138 SPMI_VREG(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000),
1139 SPMI_VREG(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000),
1140 SPMI_VREG(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
1141 SPMI_VREG(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
1142 SPMI_VREG(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
1143 SPMI_VREG(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
1144 SPMI_VREG(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
1145 SPMI_VREG(LDO, LN, 0, INF, LN_LDO, ln_ldo, ln_ldo, 0),
1146 SPMI_VREG(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000),
1147 SPMI_VREG(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000),
1148 SPMI_VREG(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000),
1149 SPMI_VREG(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000),
1150 SPMI_VREG(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000),
1151 SPMI_VREG_VS(LV100, 0, INF),
1152 SPMI_VREG_VS(LV300, 0, INF),
1153 SPMI_VREG_VS(MV300, 0, INF),
1154 SPMI_VREG_VS(MV500, 0, INF),
1155 SPMI_VREG_VS(HDMI, 0, INF),
1156 SPMI_VREG_VS(OTG, 0, INF),
1157 SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
1158 SPMI_VREG(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
1159 SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000),
1160 SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0),
1161 SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1162 ult_lo_smps, 100000),
1163 SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1164 ult_lo_smps, 100000),
1165 SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps,
1166 ult_lo_smps, 100000),
1167 SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps,
1168 ult_ho_smps, 100000),
1169 SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1170 SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1171 SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1172 SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
1173 SPMI_VREG(ULT_LDO, LV_P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1174 SPMI_VREG(ULT_LDO, LV_P300, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1175 SPMI_VREG(ULT_LDO, LV_P450, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1176 SPMI_VREG(ULT_LDO, P600, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1177 SPMI_VREG(ULT_LDO, P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
1178 SPMI_VREG(ULT_LDO, P50, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000),
1179};
1180
1181static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points)
1182{
1183 unsigned int n;
1184 struct spmi_voltage_range *range = points->range;
1185
1186 for (; range < points->range + points->count; range++) {
1187 n = 0;
1188 if (range->set_point_max_uV) {
1189 n = range->set_point_max_uV - range->set_point_min_uV;
Axel Lin419d06a2015-06-18 08:50:39 +08001190 n = (n / range->step_uV) + 1;
Stephen Boyde92a4042015-06-12 15:47:10 -07001191 }
1192 range->n_voltages = n;
1193 points->n_voltages += n;
1194 }
1195}
1196
1197static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type)
1198{
1199 const struct spmi_regulator_mapping *mapping;
1200 int ret, i;
1201 u32 dig_major_rev;
1202 u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1];
1203 u8 type, subtype;
1204
1205 ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version,
1206 ARRAY_SIZE(version));
1207 if (ret) {
Stephen Boyd6ee5c042016-03-25 14:35:09 -07001208 dev_dbg(vreg->dev, "could not read version registers\n");
Stephen Boyde92a4042015-06-12 15:47:10 -07001209 return ret;
1210 }
1211 dig_major_rev = version[SPMI_COMMON_REG_DIG_MAJOR_REV
1212 - SPMI_COMMON_REG_DIG_MAJOR_REV];
1213 if (!force_type) {
1214 type = version[SPMI_COMMON_REG_TYPE -
1215 SPMI_COMMON_REG_DIG_MAJOR_REV];
1216 subtype = version[SPMI_COMMON_REG_SUBTYPE -
1217 SPMI_COMMON_REG_DIG_MAJOR_REV];
1218 } else {
1219 type = force_type >> 8;
1220 subtype = force_type;
1221 }
1222
1223 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1224 mapping = &supported_regulators[i];
1225 if (mapping->type == type && mapping->subtype == subtype
1226 && mapping->revision_min <= dig_major_rev
1227 && mapping->revision_max >= dig_major_rev)
1228 goto found;
1229 }
1230
1231 dev_err(vreg->dev,
1232 "unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
1233 vreg->desc.name, type, subtype, dig_major_rev);
1234
1235 return -ENODEV;
1236
1237found:
1238 vreg->logical_type = mapping->logical_type;
1239 vreg->set_points = mapping->set_points;
1240 vreg->hpm_min_load = mapping->hpm_min_load;
1241 vreg->desc.ops = mapping->ops;
1242
1243 if (mapping->set_points) {
1244 if (!mapping->set_points->n_voltages)
1245 spmi_calculate_num_voltages(mapping->set_points);
1246 vreg->desc.n_voltages = mapping->set_points->n_voltages;
1247 }
1248
1249 return 0;
1250}
1251
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001252static int spmi_regulator_init_slew_rate(struct spmi_regulator *vreg)
Stephen Boyde92a4042015-06-12 15:47:10 -07001253{
1254 int ret;
1255 u8 reg = 0;
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001256 int step, delay, slew_rate, step_delay;
Stephen Boyde92a4042015-06-12 15:47:10 -07001257 const struct spmi_voltage_range *range;
1258
1259 ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, &reg, 1);
1260 if (ret) {
1261 dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret);
1262 return ret;
1263 }
1264
1265 range = spmi_regulator_find_range(vreg);
1266 if (!range)
1267 return -EINVAL;
1268
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001269 switch (vreg->logical_type) {
1270 case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
1271 step_delay = SPMI_FTSMPS_STEP_DELAY;
1272 break;
1273 default:
1274 step_delay = SPMI_DEFAULT_STEP_DELAY;
1275 break;
1276 }
1277
Stephen Boyde92a4042015-06-12 15:47:10 -07001278 step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK;
1279 step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT;
1280
1281 delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK;
1282 delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT;
1283
1284 /* slew_rate has units of uV/us */
1285 slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step);
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001286 slew_rate /= 1000 * (step_delay << delay);
Stephen Boyde92a4042015-06-12 15:47:10 -07001287 slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM;
1288 slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN;
1289
1290 /* Ensure that the slew rate is greater than 0 */
1291 vreg->slew_rate = max(slew_rate, 1);
1292
1293 return ret;
1294}
1295
Stephen Boyde2adfac2015-07-17 14:41:55 -07001296static int spmi_regulator_init_registers(struct spmi_regulator *vreg,
1297 const struct spmi_regulator_init_data *data)
1298{
1299 int ret;
1300 enum spmi_regulator_logical_type type;
1301 u8 ctrl_reg[8], reg, mask;
1302
1303 type = vreg->logical_type;
1304
1305 ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1306 if (ret)
1307 return ret;
1308
1309 /* Set up enable pin control. */
1310 if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1311 || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO
1312 || type == SPMI_REGULATOR_LOGICAL_TYPE_VS)
1313 && !(data->pin_ctrl_enable
1314 & SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1315 ctrl_reg[SPMI_COMMON_IDX_ENABLE] &=
1316 ~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1317 ctrl_reg[SPMI_COMMON_IDX_ENABLE] |=
1318 data->pin_ctrl_enable & SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
1319 }
1320
1321 /* Set up mode pin control. */
1322 if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
1323 || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO)
1324 && !(data->pin_ctrl_hpm
1325 & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1326 ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1327 ~SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1328 ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1329 data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
1330 }
1331
1332 if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS
1333 && !(data->pin_ctrl_hpm & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1334 ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1335 ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1336 ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1337 data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1338 }
1339
1340 if ((type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
1341 || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
1342 || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO)
1343 && !(data->pin_ctrl_hpm
1344 & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1345 ctrl_reg[SPMI_COMMON_IDX_MODE] &=
1346 ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1347 ctrl_reg[SPMI_COMMON_IDX_MODE] |=
1348 data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
1349 }
1350
1351 /* Write back any control register values that were modified. */
1352 ret = spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
1353 if (ret)
1354 return ret;
1355
1356 /* Set soft start strength and over current protection for VS. */
1357 if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS) {
1358 if (data->vs_soft_start_strength
1359 != SPMI_VS_SOFT_START_STR_HW_DEFAULT) {
1360 reg = data->vs_soft_start_strength
1361 & SPMI_VS_SOFT_START_SEL_MASK;
1362 mask = SPMI_VS_SOFT_START_SEL_MASK;
1363 return spmi_vreg_update_bits(vreg,
1364 SPMI_VS_REG_SOFT_START,
1365 reg, mask);
1366 }
1367 }
1368
1369 return 0;
1370}
1371
1372static void spmi_regulator_get_dt_config(struct spmi_regulator *vreg,
1373 struct device_node *node, struct spmi_regulator_init_data *data)
1374{
1375 /*
1376 * Initialize configuration parameters to use hardware default in case
1377 * no value is specified via device tree.
1378 */
1379 data->pin_ctrl_enable = SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1380 data->pin_ctrl_hpm = SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1381 data->vs_soft_start_strength = SPMI_VS_SOFT_START_STR_HW_DEFAULT;
1382
1383 /* These bindings are optional, so it is okay if they aren't found. */
1384 of_property_read_u32(node, "qcom,ocp-max-retries",
1385 &vreg->ocp_max_retries);
1386 of_property_read_u32(node, "qcom,ocp-retry-delay",
1387 &vreg->ocp_retry_delay_ms);
1388 of_property_read_u32(node, "qcom,pin-ctrl-enable",
1389 &data->pin_ctrl_enable);
1390 of_property_read_u32(node, "qcom,pin-ctrl-hpm", &data->pin_ctrl_hpm);
1391 of_property_read_u32(node, "qcom,vs-soft-start-strength",
1392 &data->vs_soft_start_strength);
1393}
1394
Stephen Boyde92a4042015-06-12 15:47:10 -07001395static unsigned int spmi_regulator_of_map_mode(unsigned int mode)
1396{
Stephen Boyde2adfac2015-07-17 14:41:55 -07001397 if (mode == 1)
Stephen Boyde92a4042015-06-12 15:47:10 -07001398 return REGULATOR_MODE_NORMAL;
Stephen Boyde2adfac2015-07-17 14:41:55 -07001399 if (mode == 2)
1400 return REGULATOR_MODE_FAST;
Stephen Boyde92a4042015-06-12 15:47:10 -07001401
1402 return REGULATOR_MODE_IDLE;
1403}
1404
1405static int spmi_regulator_of_parse(struct device_node *node,
1406 const struct regulator_desc *desc,
1407 struct regulator_config *config)
1408{
Stephen Boyde2adfac2015-07-17 14:41:55 -07001409 struct spmi_regulator_init_data data = { };
Stephen Boyde92a4042015-06-12 15:47:10 -07001410 struct spmi_regulator *vreg = config->driver_data;
1411 struct device *dev = config->dev;
1412 int ret;
1413
Stephen Boyde2adfac2015-07-17 14:41:55 -07001414 spmi_regulator_get_dt_config(vreg, node, &data);
1415
1416 if (!vreg->ocp_max_retries)
1417 vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES;
1418 if (!vreg->ocp_retry_delay_ms)
1419 vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS;
1420
1421 ret = spmi_regulator_init_registers(vreg, &data);
1422 if (ret) {
1423 dev_err(dev, "common initialization failed, ret=%d\n", ret);
1424 return ret;
1425 }
Stephen Boyde92a4042015-06-12 15:47:10 -07001426
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001427 switch (vreg->logical_type) {
1428 case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
1429 case SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS:
1430 case SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS:
1431 case SPMI_REGULATOR_LOGICAL_TYPE_SMPS:
1432 ret = spmi_regulator_init_slew_rate(vreg);
Stephen Boyde92a4042015-06-12 15:47:10 -07001433 if (ret)
1434 return ret;
Stephen Boyd2cf7b992016-03-30 18:57:49 -07001435 default:
1436 break;
Stephen Boyde92a4042015-06-12 15:47:10 -07001437 }
1438
1439 if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS)
1440 vreg->ocp_irq = 0;
1441
1442 if (vreg->ocp_irq) {
1443 ret = devm_request_irq(dev, vreg->ocp_irq,
1444 spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
1445 vreg);
1446 if (ret < 0) {
1447 dev_err(dev, "failed to request irq %d, ret=%d\n",
1448 vreg->ocp_irq, ret);
1449 return ret;
1450 }
1451
1452 INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work);
1453 }
1454
1455 return 0;
1456}
1457
1458static const struct spmi_regulator_data pm8941_regulators[] = {
1459 { "s1", 0x1400, "vdd_s1", },
1460 { "s2", 0x1700, "vdd_s2", },
1461 { "s3", 0x1a00, "vdd_s3", },
1462 { "l1", 0x4000, "vdd_l1_l3", },
1463 { "l2", 0x4100, "vdd_l2_lvs_1_2_3", },
1464 { "l3", 0x4200, "vdd_l1_l3", },
1465 { "l4", 0x4300, "vdd_l4_l11", },
1466 { "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 },
1467 { "l6", 0x4500, "vdd_l6_l12_l14_l15", },
1468 { "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 },
1469 { "l8", 0x4700, "vdd_l8_l16_l18_19", },
1470 { "l9", 0x4800, "vdd_l9_l10_l17_l22", },
1471 { "l10", 0x4900, "vdd_l9_l10_l17_l22", },
1472 { "l11", 0x4a00, "vdd_l4_l11", },
1473 { "l12", 0x4b00, "vdd_l6_l12_l14_l15", },
1474 { "l13", 0x4c00, "vdd_l13_l20_l23_l24", },
1475 { "l14", 0x4d00, "vdd_l6_l12_l14_l15", },
1476 { "l15", 0x4e00, "vdd_l6_l12_l14_l15", },
1477 { "l16", 0x4f00, "vdd_l8_l16_l18_19", },
1478 { "l17", 0x5000, "vdd_l9_l10_l17_l22", },
1479 { "l18", 0x5100, "vdd_l8_l16_l18_19", },
1480 { "l19", 0x5200, "vdd_l8_l16_l18_19", },
1481 { "l20", 0x5300, "vdd_l13_l20_l23_l24", },
1482 { "l21", 0x5400, "vdd_l21", },
1483 { "l22", 0x5500, "vdd_l9_l10_l17_l22", },
1484 { "l23", 0x5600, "vdd_l13_l20_l23_l24", },
1485 { "l24", 0x5700, "vdd_l13_l20_l23_l24", },
1486 { "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", },
1487 { "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", },
1488 { "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", },
1489 { "mvs1", 0x8300, "vin_5vs", },
1490 { "mvs2", 0x8400, "vin_5vs", },
1491 { }
1492};
1493
1494static const struct spmi_regulator_data pm8841_regulators[] = {
1495 { "s1", 0x1400, "vdd_s1", },
1496 { "s2", 0x1700, "vdd_s2", NULL, 0x1c08 },
1497 { "s3", 0x1a00, "vdd_s3", },
1498 { "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 },
1499 { "s5", 0x2000, "vdd_s5", NULL, 0x1c08 },
1500 { "s6", 0x2300, "vdd_s6", NULL, 0x1c08 },
1501 { "s7", 0x2600, "vdd_s7", NULL, 0x1c08 },
1502 { "s8", 0x2900, "vdd_s8", NULL, 0x1c08 },
1503 { }
1504};
1505
1506static const struct spmi_regulator_data pm8916_regulators[] = {
1507 { "s1", 0x1400, "vdd_s1", },
1508 { "s2", 0x1700, "vdd_s2", },
1509 { "s3", 0x1a00, "vdd_s3", },
1510 { "s4", 0x1d00, "vdd_s4", },
1511 { "l1", 0x4000, "vdd_l1_l3", },
1512 { "l2", 0x4100, "vdd_l2", },
1513 { "l3", 0x4200, "vdd_l1_l3", },
1514 { "l4", 0x4300, "vdd_l4_l5_l6", },
1515 { "l5", 0x4400, "vdd_l4_l5_l6", },
1516 { "l6", 0x4500, "vdd_l4_l5_l6", },
1517 { "l7", 0x4600, "vdd_l7", },
1518 { "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", },
1519 { "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", },
1520 { "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", },
1521 { "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", },
1522 { "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", },
1523 { "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", },
1524 { "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", },
1525 { "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", },
1526 { "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", },
1527 { "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", },
1528 { "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", },
1529 { }
1530};
1531
Stephen Boyd50314e52016-03-25 14:35:08 -07001532static const struct spmi_regulator_data pm8994_regulators[] = {
1533 { "s1", 0x1400, "vdd_s1", },
1534 { "s2", 0x1700, "vdd_s2", },
1535 { "s3", 0x1a00, "vdd_s3", },
1536 { "s4", 0x1d00, "vdd_s4", },
1537 { "s5", 0x2000, "vdd_s5", },
1538 { "s6", 0x2300, "vdd_s6", },
1539 { "s7", 0x2600, "vdd_s7", },
1540 { "s8", 0x2900, "vdd_s8", },
1541 { "s9", 0x2c00, "vdd_s9", },
1542 { "s10", 0x2f00, "vdd_s10", },
1543 { "s11", 0x3200, "vdd_s11", },
1544 { "s12", 0x3500, "vdd_s12", },
1545 { "l1", 0x4000, "vdd_l1", },
1546 { "l2", 0x4100, "vdd_l2_l26_l28", },
1547 { "l3", 0x4200, "vdd_l3_l11", },
1548 { "l4", 0x4300, "vdd_l4_l27_l31", },
1549 { "l5", 0x4400, "vdd_l5_l7", },
1550 { "l6", 0x4500, "vdd_l6_l12_l32", },
1551 { "l7", 0x4600, "vdd_l5_l7", },
1552 { "l8", 0x4700, "vdd_l8_l16_l30", },
1553 { "l9", 0x4800, "vdd_l9_l10_l18_l22", },
1554 { "l10", 0x4900, "vdd_l9_l10_l18_l22", },
1555 { "l11", 0x4a00, "vdd_l3_l11", },
1556 { "l12", 0x4b00, "vdd_l6_l12_l32", },
1557 { "l13", 0x4c00, "vdd_l13_l19_l23_l24", },
1558 { "l14", 0x4d00, "vdd_l14_l15", },
1559 { "l15", 0x4e00, "vdd_l14_l15", },
1560 { "l16", 0x4f00, "vdd_l8_l16_l30", },
1561 { "l17", 0x5000, "vdd_l17_l29", },
1562 { "l18", 0x5100, "vdd_l9_l10_l18_l22", },
1563 { "l19", 0x5200, "vdd_l13_l19_l23_l24", },
1564 { "l20", 0x5300, "vdd_l20_l21", },
1565 { "l21", 0x5400, "vdd_l20_l21", },
1566 { "l22", 0x5500, "vdd_l9_l10_l18_l22", },
1567 { "l23", 0x5600, "vdd_l13_l19_l23_l24", },
1568 { "l24", 0x5700, "vdd_l13_l19_l23_l24", },
1569 { "l25", 0x5800, "vdd_l25", },
1570 { "l26", 0x5900, "vdd_l2_l26_l28", },
1571 { "l27", 0x5a00, "vdd_l4_l27_l31", },
1572 { "l28", 0x5b00, "vdd_l2_l26_l28", },
1573 { "l29", 0x5c00, "vdd_l17_l29", },
1574 { "l30", 0x5d00, "vdd_l8_l16_l30", },
1575 { "l31", 0x5e00, "vdd_l4_l27_l31", },
1576 { "l32", 0x5f00, "vdd_l6_l12_l32", },
1577 { "lvs1", 0x8000, "vdd_lvs_1_2", },
1578 { "lvs2", 0x8100, "vdd_lvs_1_2", },
1579 { }
1580};
1581
Stephen Boyde92a4042015-06-12 15:47:10 -07001582static const struct of_device_id qcom_spmi_regulator_match[] = {
1583 { .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators },
1584 { .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators },
1585 { .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators },
Stephen Boyd50314e52016-03-25 14:35:08 -07001586 { .compatible = "qcom,pm8994-regulators", .data = &pm8994_regulators },
Stephen Boyde92a4042015-06-12 15:47:10 -07001587 { }
1588};
1589MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match);
1590
1591static int qcom_spmi_regulator_probe(struct platform_device *pdev)
1592{
1593 const struct spmi_regulator_data *reg;
1594 const struct of_device_id *match;
1595 struct regulator_config config = { };
1596 struct regulator_dev *rdev;
1597 struct spmi_regulator *vreg;
1598 struct regmap *regmap;
1599 const char *name;
1600 struct device *dev = &pdev->dev;
1601 int ret;
1602 struct list_head *vreg_list;
1603
1604 vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL);
1605 if (!vreg_list)
1606 return -ENOMEM;
1607 INIT_LIST_HEAD(vreg_list);
1608 platform_set_drvdata(pdev, vreg_list);
1609
1610 regmap = dev_get_regmap(dev->parent, NULL);
1611 if (!regmap)
1612 return -ENODEV;
1613
1614 match = of_match_device(qcom_spmi_regulator_match, &pdev->dev);
1615 if (!match)
1616 return -ENODEV;
1617
1618 for (reg = match->data; reg->name; reg++) {
1619 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1620 if (!vreg)
1621 return -ENOMEM;
1622
1623 vreg->dev = dev;
1624 vreg->base = reg->base;
1625 vreg->regmap = regmap;
1626
1627 if (reg->ocp) {
1628 vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp);
1629 if (vreg->ocp_irq < 0) {
1630 ret = vreg->ocp_irq;
1631 goto err;
1632 }
1633 }
1634
1635 vreg->desc.id = -1;
1636 vreg->desc.owner = THIS_MODULE;
1637 vreg->desc.type = REGULATOR_VOLTAGE;
1638 vreg->desc.name = name = reg->name;
1639 vreg->desc.supply_name = reg->supply;
1640 vreg->desc.of_match = reg->name;
1641 vreg->desc.of_parse_cb = spmi_regulator_of_parse;
1642 vreg->desc.of_map_mode = spmi_regulator_of_map_mode;
1643
1644 ret = spmi_regulator_match(vreg, reg->force_type);
1645 if (ret)
Stephen Boyd6ee5c042016-03-25 14:35:09 -07001646 continue;
Stephen Boyde92a4042015-06-12 15:47:10 -07001647
1648 config.dev = dev;
1649 config.driver_data = vreg;
1650 rdev = devm_regulator_register(dev, &vreg->desc, &config);
1651 if (IS_ERR(rdev)) {
1652 dev_err(dev, "failed to register %s\n", name);
1653 ret = PTR_ERR(rdev);
1654 goto err;
1655 }
1656
1657 INIT_LIST_HEAD(&vreg->node);
1658 list_add(&vreg->node, vreg_list);
1659 }
1660
1661 return 0;
1662
1663err:
1664 list_for_each_entry(vreg, vreg_list, node)
1665 if (vreg->ocp_irq)
1666 cancel_delayed_work_sync(&vreg->ocp_work);
1667 return ret;
1668}
1669
1670static int qcom_spmi_regulator_remove(struct platform_device *pdev)
1671{
1672 struct spmi_regulator *vreg;
1673 struct list_head *vreg_list = platform_get_drvdata(pdev);
1674
1675 list_for_each_entry(vreg, vreg_list, node)
1676 if (vreg->ocp_irq)
1677 cancel_delayed_work_sync(&vreg->ocp_work);
1678
1679 return 0;
1680}
1681
1682static struct platform_driver qcom_spmi_regulator_driver = {
1683 .driver = {
1684 .name = "qcom-spmi-regulator",
1685 .of_match_table = qcom_spmi_regulator_match,
1686 },
1687 .probe = qcom_spmi_regulator_probe,
1688 .remove = qcom_spmi_regulator_remove,
1689};
1690module_platform_driver(qcom_spmi_regulator_driver);
1691
1692MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver");
1693MODULE_LICENSE("GPL v2");
1694MODULE_ALIAS("platform:qcom-spmi-regulator");