blob: 4cdfaeb0737f716f75cb1a332a13b2d15dc2eef8 [file] [log] [blame]
David Collinsd1ac2f12012-02-14 13:34:18 -08001/*
Duy Truong790f06d2013-02-13 16:38:12 -08002 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
David Collinsd1ac2f12012-02-14 13:34:18 -08003 *
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#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/module.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/string.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/bitops.h>
23#include <linux/slab.h>
24#include <linux/spmi.h>
25#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/platform_device.h>
28#include <linux/regulator/driver.h>
29#include <linux/regulator/of_regulator.h>
30#include <linux/regulator/qpnp-regulator.h>
31
David Collinsd1ac2f12012-02-14 13:34:18 -080032/* Debug Flag Definitions */
33enum {
34 QPNP_VREG_DEBUG_REQUEST = BIT(0), /* Show requests */
35 QPNP_VREG_DEBUG_DUPLICATE = BIT(1), /* Show duplicate requests */
36 QPNP_VREG_DEBUG_INIT = BIT(2), /* Show state after probe */
37 QPNP_VREG_DEBUG_WRITES = BIT(3), /* Show SPMI writes */
38 QPNP_VREG_DEBUG_READS = BIT(4), /* Show SPMI reads */
39};
40
41static int qpnp_vreg_debug_mask;
42module_param_named(
43 debug_mask, qpnp_vreg_debug_mask, int, S_IRUSR | S_IWUSR
44);
45
46#define vreg_err(vreg, fmt, ...) \
47 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
48
49/* These types correspond to unique register layouts. */
50enum qpnp_regulator_logical_type {
51 QPNP_REGULATOR_LOGICAL_TYPE_SMPS,
52 QPNP_REGULATOR_LOGICAL_TYPE_LDO,
53 QPNP_REGULATOR_LOGICAL_TYPE_VS,
54 QPNP_REGULATOR_LOGICAL_TYPE_BOOST,
55 QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS,
56};
57
58enum qpnp_regulator_type {
David Collinsbad606a2012-08-21 10:57:36 -070059 QPNP_REGULATOR_TYPE_BUCK = 0x03,
David Collinsd1ac2f12012-02-14 13:34:18 -080060 QPNP_REGULATOR_TYPE_LDO = 0x04,
61 QPNP_REGULATOR_TYPE_VS = 0x05,
62 QPNP_REGULATOR_TYPE_BOOST = 0x1B,
63 QPNP_REGULATOR_TYPE_FTS = 0x1C,
64};
65
66enum qpnp_regulator_subtype {
67 QPNP_REGULATOR_SUBTYPE_GP_CTL = 0x08,
68 QPNP_REGULATOR_SUBTYPE_RF_CTL = 0x09,
69 QPNP_REGULATOR_SUBTYPE_N50 = 0x01,
70 QPNP_REGULATOR_SUBTYPE_N150 = 0x02,
71 QPNP_REGULATOR_SUBTYPE_N300 = 0x03,
72 QPNP_REGULATOR_SUBTYPE_N600 = 0x04,
73 QPNP_REGULATOR_SUBTYPE_N1200 = 0x05,
David Collins75ebd572012-08-29 14:08:10 -070074 QPNP_REGULATOR_SUBTYPE_N600_ST = 0x06,
75 QPNP_REGULATOR_SUBTYPE_N1200_ST = 0x07,
David Collinsd1ac2f12012-02-14 13:34:18 -080076 QPNP_REGULATOR_SUBTYPE_P50 = 0x08,
77 QPNP_REGULATOR_SUBTYPE_P150 = 0x09,
78 QPNP_REGULATOR_SUBTYPE_P300 = 0x0A,
79 QPNP_REGULATOR_SUBTYPE_P600 = 0x0B,
80 QPNP_REGULATOR_SUBTYPE_P1200 = 0x0C,
David Collins75ebd572012-08-29 14:08:10 -070081 QPNP_REGULATOR_SUBTYPE_LV_P50 = 0x28,
82 QPNP_REGULATOR_SUBTYPE_LV_P150 = 0x29,
83 QPNP_REGULATOR_SUBTYPE_LV_P300 = 0x2A,
84 QPNP_REGULATOR_SUBTYPE_LV_P600 = 0x2B,
85 QPNP_REGULATOR_SUBTYPE_LV_P1200 = 0x2C,
David Collinsd1ac2f12012-02-14 13:34:18 -080086 QPNP_REGULATOR_SUBTYPE_LV100 = 0x01,
87 QPNP_REGULATOR_SUBTYPE_LV300 = 0x02,
88 QPNP_REGULATOR_SUBTYPE_MV300 = 0x08,
89 QPNP_REGULATOR_SUBTYPE_MV500 = 0x09,
90 QPNP_REGULATOR_SUBTYPE_HDMI = 0x10,
91 QPNP_REGULATOR_SUBTYPE_OTG = 0x11,
92 QPNP_REGULATOR_SUBTYPE_5V_BOOST = 0x01,
93 QPNP_REGULATOR_SUBTYPE_FTS_CTL = 0x08,
94};
95
96enum qpnp_common_regulator_registers {
David Collinsbad606a2012-08-21 10:57:36 -070097 QPNP_COMMON_REG_DIG_MAJOR_REV = 0x01,
David Collinsd1ac2f12012-02-14 13:34:18 -080098 QPNP_COMMON_REG_TYPE = 0x04,
99 QPNP_COMMON_REG_SUBTYPE = 0x05,
100 QPNP_COMMON_REG_VOLTAGE_RANGE = 0x40,
101 QPNP_COMMON_REG_VOLTAGE_SET = 0x41,
102 QPNP_COMMON_REG_MODE = 0x45,
103 QPNP_COMMON_REG_ENABLE = 0x46,
104 QPNP_COMMON_REG_PULL_DOWN = 0x48,
105};
106
107enum qpnp_ldo_registers {
108 QPNP_LDO_REG_SOFT_START = 0x4C,
109};
110
111enum qpnp_vs_registers {
112 QPNP_VS_REG_OCP = 0x4A,
113 QPNP_VS_REG_SOFT_START = 0x4C,
114};
115
116enum qpnp_boost_registers {
David Collins6f846752012-08-17 17:59:28 -0700117 QPNP_BOOST_REG_CURRENT_LIMIT = 0x4A,
David Collinsd1ac2f12012-02-14 13:34:18 -0800118};
119
120/* Used for indexing into ctrl_reg. These are offets from 0x40 */
121enum qpnp_common_control_register_index {
122 QPNP_COMMON_IDX_VOLTAGE_RANGE = 0,
123 QPNP_COMMON_IDX_VOLTAGE_SET = 1,
124 QPNP_COMMON_IDX_MODE = 5,
125 QPNP_COMMON_IDX_ENABLE = 6,
126};
127
David Collinsd1ac2f12012-02-14 13:34:18 -0800128/* Common regulator control register layout */
129#define QPNP_COMMON_ENABLE_MASK 0x80
130#define QPNP_COMMON_ENABLE 0x80
131#define QPNP_COMMON_DISABLE 0x00
132#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
133#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
134#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
135#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
136#define QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0F
137
138/* Common regulator mode register layout */
139#define QPNP_COMMON_MODE_HPM_MASK 0x80
140#define QPNP_COMMON_MODE_AUTO_MASK 0x40
141#define QPNP_COMMON_MODE_BYPASS_MASK 0x20
142#define QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
143#define QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
144#define QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
145#define QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
146#define QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
147#define QPNP_COMMON_MODE_FOLLOW_ALL_MASK 0x1F
148
149/* Common regulator pull down control register layout */
150#define QPNP_COMMON_PULL_DOWN_ENABLE_MASK 0x80
151
152/* LDO regulator current limit control register layout */
153#define QPNP_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
154
155/* LDO regulator soft start control register layout */
156#define QPNP_LDO_SOFT_START_ENABLE_MASK 0x80
157
158/* VS regulator over current protection control register layout */
159#define QPNP_VS_OCP_ENABLE_MASK 0x80
160#define QPNP_VS_OCP_OVERRIDE_MASK 0x01
161#define QPNP_VS_OCP_DISABLE 0x00
162
163/* VS regulator soft start control register layout */
164#define QPNP_VS_SOFT_START_ENABLE_MASK 0x80
165#define QPNP_VS_SOFT_START_SEL_MASK 0x03
166
167/* Boost regulator current limit control register layout */
168#define QPNP_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
169#define QPNP_BOOST_CURRENT_LIMIT_MASK 0x07
170
David Collinsbdd32812012-05-10 13:22:56 -0700171/*
172 * This voltage in uV is returned by get_voltage functions when there is no way
173 * to determine the current voltage level. It is needed because the regulator
174 * framework treats a 0 uV voltage as an error.
175 */
176#define VOLTAGE_UNKNOWN 1
177
David Collinsd1ac2f12012-02-14 13:34:18 -0800178struct qpnp_voltage_range {
179 int min_uV;
180 int max_uV;
181 int step_uV;
182 int set_point_min_uV;
183 unsigned n_voltages;
184 u8 range_sel;
185};
186
187struct qpnp_voltage_set_points {
188 struct qpnp_voltage_range *range;
189 int count;
190 unsigned n_voltages;
191};
192
193struct qpnp_regulator_mapping {
194 enum qpnp_regulator_type type;
195 enum qpnp_regulator_subtype subtype;
196 enum qpnp_regulator_logical_type logical_type;
David Collinsbad606a2012-08-21 10:57:36 -0700197 u32 revision_min;
198 u32 revision_max;
David Collinsd1ac2f12012-02-14 13:34:18 -0800199 struct regulator_ops *ops;
200 struct qpnp_voltage_set_points *set_points;
201 int hpm_min_load;
202};
203
204struct qpnp_regulator {
205 struct regulator_desc rdesc;
206 struct spmi_device *spmi_dev;
207 struct regulator_dev *rdev;
208 struct qpnp_voltage_set_points *set_points;
209 enum qpnp_regulator_logical_type logical_type;
210 int enable_time;
211 int ocp_enable_time;
212 int ocp_enable;
213 int system_load;
214 int hpm_min_load;
215 u32 write_count;
216 u32 prev_write_count;
217 u16 base_addr;
218 /* ctrl_reg provides a shadow copy of register values 0x40 to 0x47. */
219 u8 ctrl_reg[8];
220};
221
David Collinsbad606a2012-08-21 10:57:36 -0700222#define QPNP_VREG_MAP(_type, _subtype, _dig_major_min, _dig_major_max, \
223 _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
David Collinsd1ac2f12012-02-14 13:34:18 -0800224 { \
225 .type = QPNP_REGULATOR_TYPE_##_type, \
226 .subtype = QPNP_REGULATOR_SUBTYPE_##_subtype, \
David Collinsbad606a2012-08-21 10:57:36 -0700227 .revision_min = _dig_major_min, \
228 .revision_max = _dig_major_max, \
David Collinsd1ac2f12012-02-14 13:34:18 -0800229 .logical_type = QPNP_REGULATOR_LOGICAL_TYPE_##_logical_type, \
230 .ops = &qpnp_##_ops_val##_ops, \
231 .set_points = &_set_points_val##_set_points, \
232 .hpm_min_load = _hpm_min_load, \
233 }
234
235#define VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, _max_uV, \
236 _step_uV) \
237 { \
238 .min_uV = _min_uV, \
239 .set_point_min_uV = _set_point_min_uV, \
240 .max_uV = _max_uV, \
241 .step_uV = _step_uV, \
242 .range_sel = _range_sel, \
243 }
244
245#define SET_POINTS(_ranges) \
246{ \
247 .range = _ranges, \
248 .count = ARRAY_SIZE(_ranges), \
249};
250
251/*
252 * These tables contain the physically available PMIC regulator voltage setpoint
253 * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
254 * to ensure that the setpoints available to software are monotonically
255 * increasing and unique. The set_voltage callback functions expect these
256 * properties to hold.
257 */
258static struct qpnp_voltage_range pldo_ranges[] = {
David Collinsbdd32812012-05-10 13:22:56 -0700259 VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
David Collinsd1ac2f12012-02-14 13:34:18 -0800260 VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 25000),
261 VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 50000),
262};
263
David Collinsbdd32812012-05-10 13:22:56 -0700264static struct qpnp_voltage_range nldo1_ranges[] = {
265 VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
266};
267
268static struct qpnp_voltage_range nldo2_ranges[] = {
269 VOLTAGE_RANGE(1, 375000, 375000, 768750, 6250),
270 VOLTAGE_RANGE(2, 750000, 775000, 1537500, 12500),
David Collinsd1ac2f12012-02-14 13:34:18 -0800271};
272
David Collinsbad606a2012-08-21 10:57:36 -0700273static struct qpnp_voltage_range nldo3_ranges[] = {
274 VOLTAGE_RANGE(0, 375000, 375000, 1537500, 12500),
275};
276
David Collinsd1ac2f12012-02-14 13:34:18 -0800277static struct qpnp_voltage_range smps_ranges[] = {
278 VOLTAGE_RANGE(0, 375000, 375000, 1562500, 12500),
279 VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 25000),
280};
281
282static struct qpnp_voltage_range ftsmps_ranges[] = {
David Collins45b86ac2012-08-09 09:44:15 -0700283 VOLTAGE_RANGE(0, 0, 350000, 1275000, 5000),
284 VOLTAGE_RANGE(1, 0, 1280000, 2040000, 10000),
David Collinsd1ac2f12012-02-14 13:34:18 -0800285};
286
287static struct qpnp_voltage_range boost_ranges[] = {
288 VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 50000),
289};
290
291static struct qpnp_voltage_set_points pldo_set_points = SET_POINTS(pldo_ranges);
David Collinsbdd32812012-05-10 13:22:56 -0700292static struct qpnp_voltage_set_points nldo1_set_points
293 = SET_POINTS(nldo1_ranges);
294static struct qpnp_voltage_set_points nldo2_set_points
295 = SET_POINTS(nldo2_ranges);
David Collinsbad606a2012-08-21 10:57:36 -0700296static struct qpnp_voltage_set_points nldo3_set_points
297 = SET_POINTS(nldo3_ranges);
David Collinsd1ac2f12012-02-14 13:34:18 -0800298static struct qpnp_voltage_set_points smps_set_points = SET_POINTS(smps_ranges);
299static struct qpnp_voltage_set_points ftsmps_set_points
300 = SET_POINTS(ftsmps_ranges);
301static struct qpnp_voltage_set_points boost_set_points
302 = SET_POINTS(boost_ranges);
303static struct qpnp_voltage_set_points none_set_points;
304
305static struct qpnp_voltage_set_points *all_set_points[] = {
306 &pldo_set_points,
David Collinsbdd32812012-05-10 13:22:56 -0700307 &nldo1_set_points,
308 &nldo2_set_points,
David Collinsbad606a2012-08-21 10:57:36 -0700309 &nldo3_set_points,
David Collinsd1ac2f12012-02-14 13:34:18 -0800310 &smps_set_points,
311 &ftsmps_set_points,
312 &boost_set_points,
313};
314
315/* Determines which label to add to a debug print statement. */
316enum qpnp_regulator_action {
317 QPNP_REGULATOR_ACTION_INIT,
318 QPNP_REGULATOR_ACTION_ENABLE,
319 QPNP_REGULATOR_ACTION_DISABLE,
320 QPNP_REGULATOR_ACTION_VOLTAGE,
321 QPNP_REGULATOR_ACTION_MODE,
322};
323
324static void qpnp_vreg_show_state(struct regulator_dev *rdev,
325 enum qpnp_regulator_action action);
326
327#define DEBUG_PRINT_BUFFER_SIZE 64
328static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len)
329{
330 int pos = 0;
331 int i;
332
333 for (i = 0; i < buf_len; i++) {
334 pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]);
335 if (i < buf_len - 1)
336 pos += scnprintf(str + pos, str_len - pos, ", ");
337 }
338}
339
340static inline int qpnp_vreg_read(struct qpnp_regulator *vreg, u16 addr, u8 *buf,
341 int len)
342{
343 char str[DEBUG_PRINT_BUFFER_SIZE];
344 int rc = 0;
345
346 rc = spmi_ext_register_readl(vreg->spmi_dev->ctrl, vreg->spmi_dev->sid,
347 vreg->base_addr + addr, buf, len);
348
349 if (!rc && (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_READS)) {
350 str[0] = '\0';
351 fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
352 pr_info(" %-11s: read(0x%04X), sid=%d, len=%d; %s\n",
353 vreg->rdesc.name, vreg->base_addr + addr,
354 vreg->spmi_dev->sid, len, str);
355 }
356
357 return rc;
358}
359
360static inline int qpnp_vreg_write(struct qpnp_regulator *vreg, u16 addr,
361 u8 *buf, int len)
362{
363 char str[DEBUG_PRINT_BUFFER_SIZE];
364 int rc = 0;
365
366 if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_WRITES) {
367 str[0] = '\0';
368 fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
369 pr_info("%-11s: write(0x%04X), sid=%d, len=%d; %s\n",
370 vreg->rdesc.name, vreg->base_addr + addr,
371 vreg->spmi_dev->sid, len, str);
372 }
373
374 rc = spmi_ext_register_writel(vreg->spmi_dev->ctrl,
375 vreg->spmi_dev->sid, vreg->base_addr + addr, buf, len);
376 if (!rc)
377 vreg->write_count += len;
378
379 return rc;
380}
381
382/*
383 * qpnp_vreg_write_optimized - write the minimum sized contiguous subset of buf
384 * @vreg: qpnp_regulator pointer for this regulator
385 * @addr: local SPMI address offset from this peripheral's base address
386 * @buf: new data to write into the SPMI registers
387 * @buf_save: old data in the registers
388 * @len: number of bytes to write
389 *
390 * This function checks for unchanged register values between buf and buf_save
391 * starting at both ends of buf. Only the contiguous subset in the middle of
392 * buf starting and ending with new values is sent.
393 *
394 * Consider the following example:
395 * buf offset: 0 1 2 3 4 5 6 7
396 * reg state: U U C C U C U U
397 * (U = unchanged, C = changed)
398 * In this example registers 2 through 5 will be written with a single
399 * transaction.
400 */
401static inline int qpnp_vreg_write_optimized(struct qpnp_regulator *vreg,
402 u16 addr, u8 *buf, u8 *buf_save, int len)
403{
404 int i, rc, start, end;
405
406 for (i = 0; i < len; i++)
407 if (buf[i] != buf_save[i])
408 break;
409 start = i;
410
411 for (i = len - 1; i >= 0; i--)
412 if (buf[i] != buf_save[i])
413 break;
414 end = i;
415
416 if (start > end) {
417 /* No modified register values present. */
418 return 0;
419 }
420
421 rc = qpnp_vreg_write(vreg, addr + start, &buf[start], end - start + 1);
422 if (!rc)
423 for (i = start; i <= end; i++)
424 buf_save[i] = buf[i];
425
426 return rc;
427}
428
429/*
430 * Perform a masked write to a PMIC register only if the new value differs
431 * from the last value written to the register. This removes redundant
432 * register writing.
433 */
434static int qpnp_vreg_masked_write(struct qpnp_regulator *vreg, u16 addr, u8 val,
435 u8 mask, u8 *reg_save)
436{
437 int rc = 0;
438 u8 reg;
439
440 reg = (*reg_save & ~mask) | (val & mask);
441 if (reg != *reg_save) {
442 rc = qpnp_vreg_write(vreg, addr, &reg, 1);
443
444 if (rc) {
445 vreg_err(vreg, "write failed; addr=0x%03X, rc=%d\n",
446 addr, rc);
447 } else {
448 *reg_save = reg;
449 }
450 }
451
452 return rc;
453}
454
455/*
456 * Perform a masked read-modify-write to a PMIC register only if the new value
457 * differs from the value currently in the register. This removes redundant
458 * register writing.
459 */
460static int qpnp_vreg_masked_read_write(struct qpnp_regulator *vreg, u16 addr,
461 u8 val, u8 mask)
462{
463 int rc;
464 u8 reg;
465
466 rc = qpnp_vreg_read(vreg, addr, &reg, 1);
467 if (rc) {
468 vreg_err(vreg, "read failed; addr=0x%03X, rc=%d\n", addr, rc);
469 return rc;
470 }
471
472 return qpnp_vreg_masked_write(vreg, addr, val, mask, &reg);
473}
474
475static int qpnp_regulator_common_is_enabled(struct regulator_dev *rdev)
476{
477 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
478
479 return (vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]
480 & QPNP_COMMON_ENABLE_MASK)
481 == QPNP_COMMON_ENABLE;
482}
483
484static int qpnp_regulator_common_enable(struct regulator_dev *rdev)
485{
486 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
487 int rc;
488
489 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
490 QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
491 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
492
493 if (rc)
494 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
495 else
496 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_ENABLE);
497
498 return rc;
499}
500
501static int qpnp_regulator_vs_enable(struct regulator_dev *rdev)
502{
503 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
504 int rc;
505 u8 reg;
506
507 if (vreg->ocp_enable == QPNP_REGULATOR_ENABLE) {
508 /* Disable OCP */
509 reg = QPNP_VS_OCP_DISABLE;
510 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
511 if (rc)
512 goto fail;
513 }
514
515 rc = qpnp_regulator_common_enable(rdev);
516 if (rc)
517 goto fail;
518
519 if (vreg->ocp_enable == QPNP_REGULATOR_ENABLE) {
520 /* Wait for inrush current to subsided, then enable OCP. */
521 udelay(vreg->ocp_enable_time);
522 reg = QPNP_VS_OCP_ENABLE_MASK;
523 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
524 if (rc)
525 goto fail;
526 }
527
528 return rc;
529fail:
530 vreg_err(vreg, "qpnp_vreg_write failed, rc=%d\n", rc);
531
532 return rc;
533}
534
535static int qpnp_regulator_common_disable(struct regulator_dev *rdev)
536{
537 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
538 int rc;
539
540 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
541 QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
542 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
543
544 if (rc)
545 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
546 else
547 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_DISABLE);
548
549 return rc;
550}
551
552static int qpnp_regulator_select_voltage(struct qpnp_regulator *vreg,
David Collinsdf5d5e72012-11-13 17:16:19 -0800553 int min_uV, int max_uV, int *range_sel, int *voltage_sel,
554 unsigned *selector)
David Collinsd1ac2f12012-02-14 13:34:18 -0800555{
556 struct qpnp_voltage_range *range;
557 int uV = min_uV;
David Collinsdf5d5e72012-11-13 17:16:19 -0800558 int lim_min_uV, lim_max_uV, i, range_id;
David Collinsd1ac2f12012-02-14 13:34:18 -0800559
560 /* Check if request voltage is outside of physically settable range. */
561 lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
562 lim_max_uV =
563 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
564
565 if (uV < lim_min_uV && max_uV >= lim_min_uV)
566 uV = lim_min_uV;
567
568 if (uV < lim_min_uV || uV > lim_max_uV) {
569 vreg_err(vreg,
570 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
571 min_uV, max_uV, lim_min_uV, lim_max_uV);
572 return -EINVAL;
573 }
574
575 /* Find the range which uV is inside of. */
576 for (i = vreg->set_points->count - 1; i > 0; i--)
577 if (uV > vreg->set_points->range[i - 1].max_uV)
578 break;
David Collinsdf5d5e72012-11-13 17:16:19 -0800579 range_id = i;
580 range = &vreg->set_points->range[range_id];
David Collinsd1ac2f12012-02-14 13:34:18 -0800581 *range_sel = range->range_sel;
582
583 /*
584 * Force uV to be an allowed set point by applying a ceiling function to
585 * the uV value.
586 */
587 *voltage_sel = (uV - range->min_uV + range->step_uV - 1)
588 / range->step_uV;
589 uV = *voltage_sel * range->step_uV + range->min_uV;
590
591 if (uV > max_uV) {
592 vreg_err(vreg,
593 "request v=[%d, %d] cannot be met by any set point; "
594 "next set point: %d\n",
595 min_uV, max_uV, uV);
596 return -EINVAL;
597 }
598
David Collinsdf5d5e72012-11-13 17:16:19 -0800599 *selector = 0;
600 for (i = 0; i < range_id; i++)
601 *selector += vreg->set_points->range[i].n_voltages;
602 *selector += (uV - range->set_point_min_uV) / range->step_uV;
603
David Collinsd1ac2f12012-02-14 13:34:18 -0800604 return 0;
605}
606
607static int qpnp_regulator_common_set_voltage(struct regulator_dev *rdev,
608 int min_uV, int max_uV, unsigned *selector)
609{
610 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
611 int rc, range_sel, voltage_sel;
612 u8 buf[2];
613
614 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
David Collinsdf5d5e72012-11-13 17:16:19 -0800615 &voltage_sel, selector);
David Collinsd1ac2f12012-02-14 13:34:18 -0800616 if (rc) {
617 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
618 return rc;
619 }
620
621 buf[0] = range_sel;
622 buf[1] = voltage_sel;
623 if ((vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] != range_sel)
624 && (vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] == voltage_sel)) {
625 /* Handle latched range change. */
626 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
627 buf, 2);
628 if (!rc) {
629 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = buf[0];
630 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] = buf[1];
631 }
632 } else {
633 /* Either write can be optimized away safely. */
634 rc = qpnp_vreg_write_optimized(vreg,
635 QPNP_COMMON_REG_VOLTAGE_RANGE, buf,
636 &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE], 2);
637 }
638
639 if (rc)
640 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
641 else
642 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
643
644 return rc;
645}
646
647static int qpnp_regulator_common_get_voltage(struct regulator_dev *rdev)
648{
649 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
650 struct qpnp_voltage_range *range = NULL;
651 int range_sel, voltage_sel, i;
652
653 range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
654 voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
655
656 for (i = 0; i < vreg->set_points->count; i++) {
657 if (vreg->set_points->range[i].range_sel == range_sel) {
658 range = &vreg->set_points->range[i];
659 break;
660 }
661 }
662
663 if (!range) {
664 vreg_err(vreg, "voltage unknown, range %d is invalid\n",
665 range_sel);
David Collinsbdd32812012-05-10 13:22:56 -0700666 return VOLTAGE_UNKNOWN;
David Collinsd1ac2f12012-02-14 13:34:18 -0800667 }
668
669 return range->step_uV * voltage_sel + range->min_uV;
670}
671
672static int qpnp_regulator_boost_set_voltage(struct regulator_dev *rdev,
673 int min_uV, int max_uV, unsigned *selector)
674{
675 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
676 int rc, range_sel, voltage_sel;
677
678 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
David Collinsdf5d5e72012-11-13 17:16:19 -0800679 &voltage_sel, selector);
David Collinsd1ac2f12012-02-14 13:34:18 -0800680 if (rc) {
681 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
682 return rc;
683 }
684
685 /*
686 * Boost type regulators do not have range select register so only
687 * voltage set register needs to be written.
688 */
689 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET,
690 voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]);
691
692 if (rc)
693 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
694 else
695 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
696
697 return rc;
698}
699
700static int qpnp_regulator_boost_get_voltage(struct regulator_dev *rdev)
701{
702 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
703 int voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
704
705 return boost_ranges[0].step_uV * voltage_sel + boost_ranges[0].min_uV;
706}
707
708static int qpnp_regulator_common_list_voltage(struct regulator_dev *rdev,
709 unsigned selector)
710{
711 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
712 int uV = 0;
713 int i;
714
715 if (selector >= vreg->set_points->n_voltages)
716 return 0;
717
718 for (i = 0; i < vreg->set_points->count; i++) {
719 if (selector < vreg->set_points->range[i].n_voltages) {
720 uV = selector * vreg->set_points->range[i].step_uV
721 + vreg->set_points->range[i].set_point_min_uV;
722 break;
723 } else {
724 selector -= vreg->set_points->range[i].n_voltages;
725 }
726 }
727
728 return uV;
729}
730
731static unsigned int qpnp_regulator_common_get_mode(struct regulator_dev *rdev)
732{
733 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
734
735 return (vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]
736 & QPNP_COMMON_MODE_HPM_MASK)
737 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
738}
739
740static int qpnp_regulator_common_set_mode(struct regulator_dev *rdev,
741 unsigned int mode)
742{
743 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
744 int rc = 0;
745 u8 val;
746
747 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
748 vreg_err(vreg, "invalid mode: %u\n", mode);
749 return -EINVAL;
750 }
751
752 val = (mode == REGULATOR_MODE_NORMAL ? QPNP_COMMON_MODE_HPM_MASK : 0);
753
754 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_MODE, val,
755 QPNP_COMMON_MODE_HPM_MASK,
756 &vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]);
757
758 if (rc)
759 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
760 else
761 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE);
762
763 return rc;
764}
765
766static unsigned int qpnp_regulator_common_get_optimum_mode(
767 struct regulator_dev *rdev, int input_uV, int output_uV,
768 int load_uA)
769{
770 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
771 unsigned int mode;
772
773 if (load_uA + vreg->system_load >= vreg->hpm_min_load)
774 mode = REGULATOR_MODE_NORMAL;
775 else
776 mode = REGULATOR_MODE_IDLE;
777
778 return mode;
779}
780
781static int qpnp_regulator_common_enable_time(struct regulator_dev *rdev)
782{
783 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
784
785 return vreg->enable_time;
786}
787
788static const char const *qpnp_print_actions[] = {
789 [QPNP_REGULATOR_ACTION_INIT] = "initial ",
790 [QPNP_REGULATOR_ACTION_ENABLE] = "enable ",
791 [QPNP_REGULATOR_ACTION_DISABLE] = "disable ",
792 [QPNP_REGULATOR_ACTION_VOLTAGE] = "set voltage",
793 [QPNP_REGULATOR_ACTION_MODE] = "set mode ",
794};
795
796static void qpnp_vreg_show_state(struct regulator_dev *rdev,
797 enum qpnp_regulator_action action)
798{
799 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
800 const char *action_label = qpnp_print_actions[action];
801 unsigned int mode = 0;
802 int uV = 0;
803 const char *mode_label = "";
804 enum qpnp_regulator_logical_type type;
805 const char *enable_label;
806 char pc_enable_label[5] = {'\0'};
807 char pc_mode_label[8] = {'\0'};
808 bool show_req, show_dupe, show_init, has_changed;
809 u8 en_reg, mode_reg;
810
811 /* Do not print unless appropriate flags are set. */
812 show_req = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_REQUEST;
813 show_dupe = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_DUPLICATE;
814 show_init = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT;
815 has_changed = vreg->write_count != vreg->prev_write_count;
816 if (!((show_init && action == QPNP_REGULATOR_ACTION_INIT)
817 || (show_req && (has_changed || show_dupe)))) {
818 return;
819 }
820
821 vreg->prev_write_count = vreg->write_count;
822
823 type = vreg->logical_type;
824
825 enable_label = qpnp_regulator_common_is_enabled(rdev) ? "on " : "off";
826
827 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
828 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
829 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
830 uV = qpnp_regulator_common_get_voltage(rdev);
831
832 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST)
833 uV = qpnp_regulator_boost_get_voltage(rdev);
834
835 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
836 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
837 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS) {
838 mode = qpnp_regulator_common_get_mode(rdev);
839 mode_label = mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM";
840 }
841
842 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
843 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
844 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
845 en_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE];
846 pc_enable_label[0] =
847 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK ? '3' : '_';
848 pc_enable_label[1] =
849 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK ? '2' : '_';
850 pc_enable_label[2] =
851 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK ? '1' : '_';
852 pc_enable_label[3] =
853 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK ? '0' : '_';
854 }
855
856 switch (type) {
857 case QPNP_REGULATOR_LOGICAL_TYPE_SMPS:
858 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
859 pc_mode_label[0] =
860 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
861 pc_mode_label[1] =
862 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
863 pc_mode_label[2] =
864 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
865 pc_mode_label[3] =
866 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
867 pc_mode_label[4] =
868 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
869 pc_mode_label[5] =
870 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
871
872 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
873 "alt_mode=%s\n",
874 action_label, vreg->rdesc.name, enable_label, uV,
875 mode_label, pc_enable_label, pc_mode_label);
876 break;
877 case QPNP_REGULATOR_LOGICAL_TYPE_LDO:
878 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
879 pc_mode_label[0] =
880 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
881 pc_mode_label[1] =
882 mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
883 pc_mode_label[2] =
884 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
885 pc_mode_label[3] =
886 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
887 pc_mode_label[4] =
888 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
889 pc_mode_label[5] =
890 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
891 pc_mode_label[6] =
892 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
893
894 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
895 "alt_mode=%s\n",
896 action_label, vreg->rdesc.name, enable_label, uV,
897 mode_label, pc_enable_label, pc_mode_label);
898 break;
899 case QPNP_REGULATOR_LOGICAL_TYPE_VS:
900 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
901 pc_mode_label[0] =
902 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
903 pc_mode_label[1] =
904 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
905
906 pr_info("%s %-11s: %s, pc_en=%s, alt_mode=%s\n",
907 action_label, vreg->rdesc.name, enable_label,
908 pc_enable_label, pc_mode_label);
909 break;
910 case QPNP_REGULATOR_LOGICAL_TYPE_BOOST:
911 pr_info("%s %-11s: %s, v=%7d uV\n",
912 action_label, vreg->rdesc.name, enable_label, uV);
913 break;
914 case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS:
915 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
916 pc_mode_label[0] =
917 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
918
919 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
920 action_label, vreg->rdesc.name, enable_label, uV,
921 mode_label, pc_mode_label);
922 break;
923 default:
924 break;
925 }
926}
927
928static struct regulator_ops qpnp_smps_ops = {
929 .enable = qpnp_regulator_common_enable,
930 .disable = qpnp_regulator_common_disable,
931 .is_enabled = qpnp_regulator_common_is_enabled,
932 .set_voltage = qpnp_regulator_common_set_voltage,
933 .get_voltage = qpnp_regulator_common_get_voltage,
934 .list_voltage = qpnp_regulator_common_list_voltage,
935 .set_mode = qpnp_regulator_common_set_mode,
936 .get_mode = qpnp_regulator_common_get_mode,
937 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
938 .enable_time = qpnp_regulator_common_enable_time,
939};
940
941static struct regulator_ops qpnp_ldo_ops = {
942 .enable = qpnp_regulator_common_enable,
943 .disable = qpnp_regulator_common_disable,
944 .is_enabled = qpnp_regulator_common_is_enabled,
945 .set_voltage = qpnp_regulator_common_set_voltage,
946 .get_voltage = qpnp_regulator_common_get_voltage,
947 .list_voltage = qpnp_regulator_common_list_voltage,
948 .set_mode = qpnp_regulator_common_set_mode,
949 .get_mode = qpnp_regulator_common_get_mode,
950 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
951 .enable_time = qpnp_regulator_common_enable_time,
952};
953
954static struct regulator_ops qpnp_vs_ops = {
955 .enable = qpnp_regulator_vs_enable,
956 .disable = qpnp_regulator_common_disable,
957 .is_enabled = qpnp_regulator_common_is_enabled,
958 .enable_time = qpnp_regulator_common_enable_time,
959};
960
961static struct regulator_ops qpnp_boost_ops = {
962 .enable = qpnp_regulator_common_enable,
963 .disable = qpnp_regulator_common_disable,
964 .is_enabled = qpnp_regulator_common_is_enabled,
965 .set_voltage = qpnp_regulator_boost_set_voltage,
966 .get_voltage = qpnp_regulator_boost_get_voltage,
967 .list_voltage = qpnp_regulator_common_list_voltage,
968 .enable_time = qpnp_regulator_common_enable_time,
969};
970
971static struct regulator_ops qpnp_ftsmps_ops = {
972 .enable = qpnp_regulator_common_enable,
973 .disable = qpnp_regulator_common_disable,
974 .is_enabled = qpnp_regulator_common_is_enabled,
975 .set_voltage = qpnp_regulator_common_set_voltage,
976 .get_voltage = qpnp_regulator_common_get_voltage,
977 .list_voltage = qpnp_regulator_common_list_voltage,
978 .set_mode = qpnp_regulator_common_set_mode,
979 .get_mode = qpnp_regulator_common_get_mode,
980 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
981 .enable_time = qpnp_regulator_common_enable_time,
982};
983
David Collinsbad606a2012-08-21 10:57:36 -0700984/* Maximum possible digital major revision value */
985#define INF 0xFF
986
David Collinsd1ac2f12012-02-14 13:34:18 -0800987static const struct qpnp_regulator_mapping supported_regulators[] = {
David Collinsbad606a2012-08-21 10:57:36 -0700988 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
989 QPNP_VREG_MAP(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
990 QPNP_VREG_MAP(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
991 QPNP_VREG_MAP(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
992 QPNP_VREG_MAP(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
993 QPNP_VREG_MAP(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
994 QPNP_VREG_MAP(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
David Collins75ebd572012-08-29 14:08:10 -0700995 QPNP_VREG_MAP(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000),
996 QPNP_VREG_MAP(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000),
997 QPNP_VREG_MAP(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000),
998 QPNP_VREG_MAP(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000),
David Collinsbad606a2012-08-21 10:57:36 -0700999 QPNP_VREG_MAP(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
1000 QPNP_VREG_MAP(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
1001 QPNP_VREG_MAP(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
1002 QPNP_VREG_MAP(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
1003 QPNP_VREG_MAP(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
David Collins75ebd572012-08-29 14:08:10 -07001004 QPNP_VREG_MAP(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000),
1005 QPNP_VREG_MAP(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000),
1006 QPNP_VREG_MAP(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000),
1007 QPNP_VREG_MAP(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000),
1008 QPNP_VREG_MAP(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000),
David Collinsbad606a2012-08-21 10:57:36 -07001009 QPNP_VREG_MAP(VS, LV100, 0, INF, VS, vs, none, 0),
1010 QPNP_VREG_MAP(VS, LV300, 0, INF, VS, vs, none, 0),
1011 QPNP_VREG_MAP(VS, MV300, 0, INF, VS, vs, none, 0),
1012 QPNP_VREG_MAP(VS, MV500, 0, INF, VS, vs, none, 0),
1013 QPNP_VREG_MAP(VS, HDMI, 0, INF, VS, vs, none, 0),
1014 QPNP_VREG_MAP(VS, OTG, 0, INF, VS, vs, none, 0),
1015 QPNP_VREG_MAP(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
1016 QPNP_VREG_MAP(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
David Collinsd1ac2f12012-02-14 13:34:18 -08001017};
1018
1019static int qpnp_regulator_match(struct qpnp_regulator *vreg)
1020{
1021 const struct qpnp_regulator_mapping *mapping;
Michael Bohan9328e492012-08-09 11:37:36 -07001022 struct device_node *node = vreg->spmi_dev->dev.of_node;
David Collinsd1ac2f12012-02-14 13:34:18 -08001023 int rc, i;
David Collinsbad606a2012-08-21 10:57:36 -07001024 u32 type_reg[2], dig_major_rev;
1025 u8 version[QPNP_COMMON_REG_SUBTYPE - QPNP_COMMON_REG_DIG_MAJOR_REV + 1];
1026 u8 type, subtype;
David Collinsd1ac2f12012-02-14 13:34:18 -08001027
David Collinsbad606a2012-08-21 10:57:36 -07001028 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_DIG_MAJOR_REV, version,
1029 ARRAY_SIZE(version));
1030 if (rc) {
1031 vreg_err(vreg, "could not read version registers, rc=%d\n", rc);
1032 return rc;
1033 }
1034 dig_major_rev = version[QPNP_COMMON_REG_DIG_MAJOR_REV
1035 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1036 type = version[QPNP_COMMON_REG_TYPE
1037 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1038 subtype = version[QPNP_COMMON_REG_SUBTYPE
1039 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1040
1041 /*
1042 * Override type and subtype register values if qcom,force-type is
1043 * present in the device tree node.
1044 */
1045 rc = of_property_read_u32_array(node, "qcom,force-type", type_reg, 2);
Michael Bohan9328e492012-08-09 11:37:36 -07001046 if (!rc) {
1047 type = type_reg[0];
1048 subtype = type_reg[1];
David Collinsd1ac2f12012-02-14 13:34:18 -08001049 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001050
1051 rc = -ENODEV;
1052 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1053 mapping = &supported_regulators[i];
David Collinsbad606a2012-08-21 10:57:36 -07001054 if (mapping->type == type && mapping->subtype == subtype
1055 && mapping->revision_min <= dig_major_rev
1056 && mapping->revision_max >= dig_major_rev) {
David Collinsd1ac2f12012-02-14 13:34:18 -08001057 vreg->logical_type = mapping->logical_type;
1058 vreg->set_points = mapping->set_points;
1059 vreg->hpm_min_load = mapping->hpm_min_load;
1060 vreg->rdesc.ops = mapping->ops;
1061 vreg->rdesc.n_voltages
1062 = mapping->set_points->n_voltages;
1063 rc = 0;
1064 break;
1065 }
1066 }
1067
1068 return rc;
1069}
1070
1071static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg,
1072 struct qpnp_regulator_platform_data *pdata)
1073{
1074 int rc, i;
1075 enum qpnp_regulator_logical_type type;
1076 u8 ctrl_reg[8], reg, mask;
1077
1078 type = vreg->logical_type;
1079
1080 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1081 vreg->ctrl_reg, 8);
1082 if (rc) {
1083 vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
1084 return rc;
1085 }
1086
1087 for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++)
1088 ctrl_reg[i] = vreg->ctrl_reg[i];
1089
1090 /* Set up enable pin control. */
1091 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1092 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1093 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1094 && !(pdata->pin_ctrl_enable
1095 & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1096 ctrl_reg[QPNP_COMMON_IDX_ENABLE] &=
1097 ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1098 ctrl_reg[QPNP_COMMON_IDX_ENABLE] |=
1099 pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1100 }
1101
1102 /* Set up auto mode control. */
1103 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1104 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1105 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1106 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
1107 && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
1108 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1109 ~QPNP_COMMON_MODE_AUTO_MASK;
1110 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1111 (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0);
1112 }
1113
1114 /* Set up mode pin control. */
1115 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1116 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO)
1117 && !(pdata->pin_ctrl_hpm
1118 & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1119 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1120 ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1121 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1122 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1123 }
1124
1125 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1126 && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1127 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1128 ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1129 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1130 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1131 }
1132
1133 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1134 && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1135 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1136 ~QPNP_COMMON_MODE_BYPASS_MASK;
1137 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1138 (pdata->bypass_mode_enable
1139 ? QPNP_COMMON_MODE_BYPASS_MASK : 0);
1140 }
1141
1142 /* Set boost current limit. */
1143 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
1144 && pdata->boost_current_limit
1145 != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) {
David Collins6f846752012-08-17 17:59:28 -07001146 reg = pdata->boost_current_limit;
1147 mask = QPNP_BOOST_CURRENT_LIMIT_MASK;
1148 rc = qpnp_vreg_masked_read_write(vreg,
1149 QPNP_BOOST_REG_CURRENT_LIMIT, reg, mask);
1150 if (rc) {
1151 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1152 return rc;
1153 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001154 }
1155
1156 /* Write back any control register values that were modified. */
1157 rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1158 ctrl_reg, vreg->ctrl_reg, 8);
1159 if (rc) {
1160 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1161 return rc;
1162 }
1163
1164 /* Set pull down. */
1165 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1166 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1167 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1168 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1169 reg = pdata->pull_down_enable
1170 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1171 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_PULL_DOWN, &reg, 1);
1172 if (rc) {
1173 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1174 return rc;
1175 }
1176 }
1177
1178 if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
1179 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1180 /* FTSMPS has other bits in the pull down control register. */
1181 reg = pdata->pull_down_enable
1182 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1183 rc = qpnp_vreg_masked_read_write(vreg,
1184 QPNP_COMMON_REG_PULL_DOWN, reg,
1185 QPNP_COMMON_PULL_DOWN_ENABLE_MASK);
1186 if (rc) {
1187 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1188 return rc;
1189 }
1190 }
1191
1192 /* Set soft start for LDO. */
1193 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1194 && pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1195 reg = pdata->soft_start_enable
1196 ? QPNP_LDO_SOFT_START_ENABLE_MASK : 0;
1197 rc = qpnp_vreg_write(vreg, QPNP_LDO_REG_SOFT_START, &reg, 1);
1198 if (rc) {
1199 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1200 return rc;
1201 }
1202 }
1203
1204 /* Set soft start strength and over current protection for VS. */
1205 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
1206 reg = 0;
1207 mask = 0;
1208 if (pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1209 reg |= pdata->soft_start_enable
1210 ? QPNP_VS_SOFT_START_ENABLE_MASK : 0;
1211 mask |= QPNP_VS_SOFT_START_ENABLE_MASK;
1212 }
1213 if (pdata->vs_soft_start_strength
1214 != QPNP_VS_SOFT_START_STR_HW_DEFAULT) {
1215 reg |= pdata->vs_soft_start_strength
1216 & QPNP_VS_SOFT_START_SEL_MASK;
1217 mask |= QPNP_VS_SOFT_START_SEL_MASK;
1218 }
1219 rc = qpnp_vreg_masked_read_write(vreg, QPNP_VS_REG_SOFT_START,
1220 reg, mask);
1221 if (rc) {
1222 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1223 return rc;
1224 }
1225
1226 if (pdata->ocp_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1227 reg = pdata->ocp_enable ? QPNP_VS_OCP_ENABLE_MASK : 0;
1228 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
1229 if (rc) {
1230 vreg_err(vreg, "spmi write failed, rc=%d\n",
1231 rc);
1232 return rc;
1233 }
1234 }
1235 }
1236
1237 return rc;
1238}
1239
1240/* Fill in pdata elements based on values found in device tree. */
1241static int qpnp_regulator_get_dt_config(struct spmi_device *spmi,
1242 struct qpnp_regulator_platform_data *pdata)
1243{
1244 struct resource *res;
1245 struct device_node *node = spmi->dev.of_node;
1246 int rc = 0;
1247
1248 pdata->init_data.constraints.input_uV
1249 = pdata->init_data.constraints.max_uV;
1250
Michael Bohan0e5534d2012-05-22 17:33:45 -07001251 res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
David Collinsd1ac2f12012-02-14 13:34:18 -08001252 if (!res) {
1253 dev_err(&spmi->dev, "%s: node is missing base address\n",
1254 __func__);
1255 return -EINVAL;
1256 }
1257 pdata->base_addr = res->start;
1258
1259 /*
1260 * Initialize configuration parameters to use hardware default in case
1261 * no value is specified via device tree.
1262 */
1263 pdata->auto_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1264 pdata->bypass_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1265 pdata->ocp_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1266 pdata->pull_down_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1267 pdata->soft_start_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1268 pdata->boost_current_limit = QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT;
1269 pdata->pin_ctrl_enable = QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1270 pdata->pin_ctrl_hpm = QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1271 pdata->vs_soft_start_strength = QPNP_VS_SOFT_START_STR_HW_DEFAULT;
1272
1273 /* These bindings are optional, so it is okay if they are not found. */
1274 of_property_read_u32(node, "qcom,auto-mode-enable",
1275 &pdata->auto_mode_enable);
1276 of_property_read_u32(node, "qcom,bypass-mode-enable",
1277 &pdata->bypass_mode_enable);
1278 of_property_read_u32(node, "qcom,ocp-enable", &pdata->ocp_enable);
1279 of_property_read_u32(node, "qcom,pull-down-enable",
1280 &pdata->pull_down_enable);
1281 of_property_read_u32(node, "qcom,soft-start-enable",
1282 &pdata->soft_start_enable);
1283 of_property_read_u32(node, "qcom,boost-current-limit",
1284 &pdata->boost_current_limit);
1285 of_property_read_u32(node, "qcom,pin-ctrl-enable",
1286 &pdata->pin_ctrl_enable);
1287 of_property_read_u32(node, "qcom,pin-ctrl-hpm", &pdata->pin_ctrl_hpm);
1288 of_property_read_u32(node, "qcom,vs-soft-start-strength",
1289 &pdata->vs_soft_start_strength);
1290 of_property_read_u32(node, "qcom,system-load", &pdata->system_load);
1291 of_property_read_u32(node, "qcom,enable-time", &pdata->enable_time);
1292 of_property_read_u32(node, "qcom,ocp-enable-time",
1293 &pdata->ocp_enable_time);
1294
1295 return rc;
1296}
1297
1298static struct of_device_id spmi_match_table[];
1299
1300#define MAX_NAME_LEN 127
1301
1302static int __devinit qpnp_regulator_probe(struct spmi_device *spmi)
1303{
1304 struct qpnp_regulator_platform_data *pdata;
1305 struct qpnp_regulator *vreg;
1306 struct regulator_desc *rdesc;
1307 struct qpnp_regulator_platform_data of_pdata;
1308 struct regulator_init_data *init_data;
1309 char *reg_name;
1310 int rc;
1311 bool is_dt;
1312
1313 vreg = kzalloc(sizeof(struct qpnp_regulator), GFP_KERNEL);
1314 if (!vreg) {
1315 dev_err(&spmi->dev, "%s: Can't allocate qpnp_regulator\n",
1316 __func__);
1317 return -ENOMEM;
1318 }
1319
1320 is_dt = of_match_device(spmi_match_table, &spmi->dev);
1321
1322 /* Check if device tree is in use. */
1323 if (is_dt) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001324 init_data = of_get_regulator_init_data(&spmi->dev,
1325 spmi->dev.of_node);
David Collinsd1ac2f12012-02-14 13:34:18 -08001326 if (!init_data) {
1327 dev_err(&spmi->dev, "%s: unable to allocate memory\n",
1328 __func__);
1329 kfree(vreg);
1330 return -ENOMEM;
1331 }
1332 memset(&of_pdata, 0,
1333 sizeof(struct qpnp_regulator_platform_data));
1334 memcpy(&of_pdata.init_data, init_data,
1335 sizeof(struct regulator_init_data));
1336
1337 if (of_get_property(spmi->dev.of_node, "parent-supply", NULL))
1338 of_pdata.init_data.supply_regulator = "parent";
1339
1340 rc = qpnp_regulator_get_dt_config(spmi, &of_pdata);
1341 if (rc) {
1342 dev_err(&spmi->dev, "%s: DT parsing failed, rc=%d\n",
1343 __func__, rc);
1344 kfree(vreg);
1345 return -ENOMEM;
1346 }
1347
1348 pdata = &of_pdata;
1349 } else {
1350 pdata = spmi->dev.platform_data;
1351 }
1352
1353 if (pdata == NULL) {
1354 dev_err(&spmi->dev, "%s: no platform data specified\n",
1355 __func__);
1356 kfree(vreg);
1357 return -EINVAL;
1358 }
1359
1360 vreg->spmi_dev = spmi;
1361 vreg->prev_write_count = -1;
1362 vreg->write_count = 0;
1363 vreg->base_addr = pdata->base_addr;
1364 vreg->enable_time = pdata->enable_time;
1365 vreg->system_load = pdata->system_load;
1366 vreg->ocp_enable = pdata->ocp_enable;
1367 vreg->ocp_enable_time = pdata->ocp_enable_time;
1368
1369 rdesc = &vreg->rdesc;
1370 rdesc->id = spmi->ctrl->nr;
1371 rdesc->owner = THIS_MODULE;
1372 rdesc->type = REGULATOR_VOLTAGE;
1373
1374 reg_name = kzalloc(strnlen(pdata->init_data.constraints.name,
1375 MAX_NAME_LEN) + 1, GFP_KERNEL);
1376 if (!reg_name) {
1377 dev_err(&spmi->dev, "%s: Can't allocate regulator name\n",
1378 __func__);
1379 kfree(vreg);
1380 return -ENOMEM;
1381 }
1382 strlcpy(reg_name, pdata->init_data.constraints.name,
1383 strnlen(pdata->init_data.constraints.name, MAX_NAME_LEN) + 1);
1384 rdesc->name = reg_name;
1385
1386 dev_set_drvdata(&spmi->dev, vreg);
1387
1388 rc = qpnp_regulator_match(vreg);
1389 if (rc) {
1390 vreg_err(vreg, "regulator type unknown, rc=%d\n", rc);
1391 goto bail;
1392 }
1393
1394 if (is_dt && rdesc->ops) {
1395 /* Fill in ops and mode masks when using device tree. */
1396 if (rdesc->ops->enable)
1397 pdata->init_data.constraints.valid_ops_mask
1398 |= REGULATOR_CHANGE_STATUS;
1399 if (rdesc->ops->get_voltage)
1400 pdata->init_data.constraints.valid_ops_mask
1401 |= REGULATOR_CHANGE_VOLTAGE;
1402 if (rdesc->ops->get_mode) {
1403 pdata->init_data.constraints.valid_ops_mask
1404 |= REGULATOR_CHANGE_MODE
1405 | REGULATOR_CHANGE_DRMS;
1406 pdata->init_data.constraints.valid_modes_mask
1407 = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1408 }
1409 }
1410
1411 rc = qpnp_regulator_init_registers(vreg, pdata);
1412 if (rc) {
1413 vreg_err(vreg, "common initialization failed, rc=%d\n", rc);
1414 goto bail;
1415 }
1416
1417 vreg->rdev = regulator_register(rdesc, &spmi->dev,
1418 &(pdata->init_data), vreg, spmi->dev.of_node);
1419 if (IS_ERR(vreg->rdev)) {
1420 rc = PTR_ERR(vreg->rdev);
1421 vreg_err(vreg, "regulator_register failed, rc=%d\n", rc);
1422 goto bail;
1423 }
1424
1425 qpnp_vreg_show_state(vreg->rdev, QPNP_REGULATOR_ACTION_INIT);
1426
1427 return 0;
1428
1429bail:
1430 if (rc)
1431 vreg_err(vreg, "probe failed, rc=%d\n", rc);
1432
1433 kfree(vreg->rdesc.name);
1434 kfree(vreg);
1435
1436 return rc;
1437}
1438
1439static int __devexit qpnp_regulator_remove(struct spmi_device *spmi)
1440{
1441 struct qpnp_regulator *vreg;
1442
1443 vreg = dev_get_drvdata(&spmi->dev);
1444 dev_set_drvdata(&spmi->dev, NULL);
1445
1446 if (vreg) {
1447 regulator_unregister(vreg->rdev);
1448 kfree(vreg->rdesc.name);
1449 kfree(vreg);
1450 }
1451
1452 return 0;
1453}
1454
1455static struct of_device_id spmi_match_table[] = {
1456 { .compatible = QPNP_REGULATOR_DRIVER_NAME, },
1457 {}
1458};
1459
1460static const struct spmi_device_id qpnp_regulator_id[] = {
1461 { QPNP_REGULATOR_DRIVER_NAME, 0 },
1462 { }
1463};
1464MODULE_DEVICE_TABLE(spmi, qpnp_regulator_id);
1465
1466static struct spmi_driver qpnp_regulator_driver = {
1467 .driver = {
1468 .name = QPNP_REGULATOR_DRIVER_NAME,
1469 .of_match_table = spmi_match_table,
1470 .owner = THIS_MODULE,
1471 },
1472 .probe = qpnp_regulator_probe,
1473 .remove = __devexit_p(qpnp_regulator_remove),
1474 .id_table = qpnp_regulator_id,
1475};
1476
1477/*
1478 * Pre-compute the number of set points available for each regulator type to
1479 * avoid unnecessary calculations later in runtime.
1480 */
1481static void qpnp_regulator_set_point_init(void)
1482{
1483 struct qpnp_voltage_set_points **set_points;
1484 int i, j, temp;
1485
1486 set_points = all_set_points;
1487
1488 for (i = 0; i < ARRAY_SIZE(all_set_points); i++) {
1489 temp = 0;
1490 for (j = 0; j < all_set_points[i]->count; j++) {
1491 all_set_points[i]->range[j].n_voltages
1492 = (all_set_points[i]->range[j].max_uV
1493 - all_set_points[i]->range[j].set_point_min_uV)
1494 / all_set_points[i]->range[j].step_uV + 1;
1495 temp += all_set_points[i]->range[j].n_voltages;
1496 }
1497 all_set_points[i]->n_voltages = temp;
1498 }
1499}
1500
1501/**
1502 * qpnp_regulator_init() - register spmi driver for qpnp-regulator
1503 *
1504 * This initialization function should be called in systems in which driver
1505 * registration ordering must be controlled precisely.
1506 */
1507int __init qpnp_regulator_init(void)
1508{
1509 static bool has_registered;
1510
1511 if (has_registered)
1512 return 0;
1513 else
1514 has_registered = true;
1515
1516 qpnp_regulator_set_point_init();
1517
1518 return spmi_driver_register(&qpnp_regulator_driver);
1519}
1520EXPORT_SYMBOL(qpnp_regulator_init);
1521
1522static void __exit qpnp_regulator_exit(void)
1523{
1524 spmi_driver_unregister(&qpnp_regulator_driver);
1525}
1526
1527MODULE_DESCRIPTION("QPNP PMIC regulator driver");
1528MODULE_LICENSE("GPL v2");
1529
1530arch_initcall(qpnp_regulator_init);
1531module_exit(qpnp_regulator_exit);