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