blob: c2c9f78e33960335ba74b346a837c4536fd09a9b [file] [log] [blame]
David Collinsd1ac2f12012-02-14 13:34:18 -08001/*
2 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#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,
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 Collinsbad606a2012-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 {
110 QPNP_BOOST_REG_CURRENT_LIMIT = 0x40,
111};
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
121enum qpnp_boost_control_register_index {
122 QPNP_BOOST_IDX_CURRENT_LIMIT = 0,
123};
124
125/* Common regulator control register layout */
126#define QPNP_COMMON_ENABLE_MASK 0x80
127#define QPNP_COMMON_ENABLE 0x80
128#define QPNP_COMMON_DISABLE 0x00
129#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
130#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
131#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
132#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
133#define QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0F
134
135/* Common regulator mode register layout */
136#define QPNP_COMMON_MODE_HPM_MASK 0x80
137#define QPNP_COMMON_MODE_AUTO_MASK 0x40
138#define QPNP_COMMON_MODE_BYPASS_MASK 0x20
139#define QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
140#define QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
141#define QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
142#define QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
143#define QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
144#define QPNP_COMMON_MODE_FOLLOW_ALL_MASK 0x1F
145
146/* Common regulator pull down control register layout */
147#define QPNP_COMMON_PULL_DOWN_ENABLE_MASK 0x80
148
149/* LDO regulator current limit control register layout */
150#define QPNP_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
151
152/* LDO regulator soft start control register layout */
153#define QPNP_LDO_SOFT_START_ENABLE_MASK 0x80
154
155/* VS regulator over current protection control register layout */
156#define QPNP_VS_OCP_ENABLE_MASK 0x80
157#define QPNP_VS_OCP_OVERRIDE_MASK 0x01
158#define QPNP_VS_OCP_DISABLE 0x00
159
160/* VS regulator soft start control register layout */
161#define QPNP_VS_SOFT_START_ENABLE_MASK 0x80
162#define QPNP_VS_SOFT_START_SEL_MASK 0x03
163
164/* Boost regulator current limit control register layout */
165#define QPNP_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
166#define QPNP_BOOST_CURRENT_LIMIT_MASK 0x07
167
David Collinsbdd32812012-05-10 13:22:56 -0700168/*
169 * This voltage in uV is returned by get_voltage functions when there is no way
170 * to determine the current voltage level. It is needed because the regulator
171 * framework treats a 0 uV voltage as an error.
172 */
173#define VOLTAGE_UNKNOWN 1
174
David Collinsd1ac2f12012-02-14 13:34:18 -0800175struct qpnp_voltage_range {
176 int min_uV;
177 int max_uV;
178 int step_uV;
179 int set_point_min_uV;
180 unsigned n_voltages;
181 u8 range_sel;
182};
183
184struct qpnp_voltage_set_points {
185 struct qpnp_voltage_range *range;
186 int count;
187 unsigned n_voltages;
188};
189
190struct qpnp_regulator_mapping {
191 enum qpnp_regulator_type type;
192 enum qpnp_regulator_subtype subtype;
193 enum qpnp_regulator_logical_type logical_type;
David Collinsbad606a2012-08-21 10:57:36 -0700194 u32 revision_min;
195 u32 revision_max;
David Collinsd1ac2f12012-02-14 13:34:18 -0800196 struct regulator_ops *ops;
197 struct qpnp_voltage_set_points *set_points;
198 int hpm_min_load;
199};
200
201struct qpnp_regulator {
202 struct regulator_desc rdesc;
203 struct spmi_device *spmi_dev;
204 struct regulator_dev *rdev;
205 struct qpnp_voltage_set_points *set_points;
206 enum qpnp_regulator_logical_type logical_type;
207 int enable_time;
208 int ocp_enable_time;
209 int ocp_enable;
210 int system_load;
211 int hpm_min_load;
212 u32 write_count;
213 u32 prev_write_count;
214 u16 base_addr;
215 /* ctrl_reg provides a shadow copy of register values 0x40 to 0x47. */
216 u8 ctrl_reg[8];
217};
218
David Collinsbad606a2012-08-21 10:57:36 -0700219#define QPNP_VREG_MAP(_type, _subtype, _dig_major_min, _dig_major_max, \
220 _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
David Collinsd1ac2f12012-02-14 13:34:18 -0800221 { \
222 .type = QPNP_REGULATOR_TYPE_##_type, \
223 .subtype = QPNP_REGULATOR_SUBTYPE_##_subtype, \
David Collinsbad606a2012-08-21 10:57:36 -0700224 .revision_min = _dig_major_min, \
225 .revision_max = _dig_major_max, \
David Collinsd1ac2f12012-02-14 13:34:18 -0800226 .logical_type = QPNP_REGULATOR_LOGICAL_TYPE_##_logical_type, \
227 .ops = &qpnp_##_ops_val##_ops, \
228 .set_points = &_set_points_val##_set_points, \
229 .hpm_min_load = _hpm_min_load, \
230 }
231
232#define VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, _max_uV, \
233 _step_uV) \
234 { \
235 .min_uV = _min_uV, \
236 .set_point_min_uV = _set_point_min_uV, \
237 .max_uV = _max_uV, \
238 .step_uV = _step_uV, \
239 .range_sel = _range_sel, \
240 }
241
242#define SET_POINTS(_ranges) \
243{ \
244 .range = _ranges, \
245 .count = ARRAY_SIZE(_ranges), \
246};
247
248/*
249 * These tables contain the physically available PMIC regulator voltage setpoint
250 * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
251 * to ensure that the setpoints available to software are monotonically
252 * increasing and unique. The set_voltage callback functions expect these
253 * properties to hold.
254 */
255static struct qpnp_voltage_range pldo_ranges[] = {
David Collinsbdd32812012-05-10 13:22:56 -0700256 VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
David Collinsd1ac2f12012-02-14 13:34:18 -0800257 VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 25000),
258 VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 50000),
259};
260
David Collinsbdd32812012-05-10 13:22:56 -0700261static struct qpnp_voltage_range nldo1_ranges[] = {
262 VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
263};
264
265static struct qpnp_voltage_range nldo2_ranges[] = {
266 VOLTAGE_RANGE(1, 375000, 375000, 768750, 6250),
267 VOLTAGE_RANGE(2, 750000, 775000, 1537500, 12500),
David Collinsd1ac2f12012-02-14 13:34:18 -0800268};
269
David Collinsbad606a2012-08-21 10:57:36 -0700270static struct qpnp_voltage_range nldo3_ranges[] = {
271 VOLTAGE_RANGE(0, 375000, 375000, 1537500, 12500),
272};
273
David Collinsd1ac2f12012-02-14 13:34:18 -0800274static struct qpnp_voltage_range smps_ranges[] = {
275 VOLTAGE_RANGE(0, 375000, 375000, 1562500, 12500),
276 VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 25000),
277};
278
279static struct qpnp_voltage_range ftsmps_ranges[] = {
David Collins45b86ac2012-08-09 09:44:15 -0700280 VOLTAGE_RANGE(0, 0, 350000, 1275000, 5000),
281 VOLTAGE_RANGE(1, 0, 1280000, 2040000, 10000),
David Collinsd1ac2f12012-02-14 13:34:18 -0800282};
283
284static struct qpnp_voltage_range boost_ranges[] = {
285 VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 50000),
286};
287
288static struct qpnp_voltage_set_points pldo_set_points = SET_POINTS(pldo_ranges);
David Collinsbdd32812012-05-10 13:22:56 -0700289static struct qpnp_voltage_set_points nldo1_set_points
290 = SET_POINTS(nldo1_ranges);
291static struct qpnp_voltage_set_points nldo2_set_points
292 = SET_POINTS(nldo2_ranges);
David Collinsbad606a2012-08-21 10:57:36 -0700293static struct qpnp_voltage_set_points nldo3_set_points
294 = SET_POINTS(nldo3_ranges);
David Collinsd1ac2f12012-02-14 13:34:18 -0800295static struct qpnp_voltage_set_points smps_set_points = SET_POINTS(smps_ranges);
296static struct qpnp_voltage_set_points ftsmps_set_points
297 = SET_POINTS(ftsmps_ranges);
298static struct qpnp_voltage_set_points boost_set_points
299 = SET_POINTS(boost_ranges);
300static struct qpnp_voltage_set_points none_set_points;
301
302static struct qpnp_voltage_set_points *all_set_points[] = {
303 &pldo_set_points,
David Collinsbdd32812012-05-10 13:22:56 -0700304 &nldo1_set_points,
305 &nldo2_set_points,
David Collinsbad606a2012-08-21 10:57:36 -0700306 &nldo3_set_points,
David Collinsd1ac2f12012-02-14 13:34:18 -0800307 &smps_set_points,
308 &ftsmps_set_points,
309 &boost_set_points,
310};
311
312/* Determines which label to add to a debug print statement. */
313enum qpnp_regulator_action {
314 QPNP_REGULATOR_ACTION_INIT,
315 QPNP_REGULATOR_ACTION_ENABLE,
316 QPNP_REGULATOR_ACTION_DISABLE,
317 QPNP_REGULATOR_ACTION_VOLTAGE,
318 QPNP_REGULATOR_ACTION_MODE,
319};
320
321static void qpnp_vreg_show_state(struct regulator_dev *rdev,
322 enum qpnp_regulator_action action);
323
324#define DEBUG_PRINT_BUFFER_SIZE 64
325static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len)
326{
327 int pos = 0;
328 int i;
329
330 for (i = 0; i < buf_len; i++) {
331 pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]);
332 if (i < buf_len - 1)
333 pos += scnprintf(str + pos, str_len - pos, ", ");
334 }
335}
336
337static inline int qpnp_vreg_read(struct qpnp_regulator *vreg, u16 addr, u8 *buf,
338 int len)
339{
340 char str[DEBUG_PRINT_BUFFER_SIZE];
341 int rc = 0;
342
343 rc = spmi_ext_register_readl(vreg->spmi_dev->ctrl, vreg->spmi_dev->sid,
344 vreg->base_addr + addr, buf, len);
345
346 if (!rc && (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_READS)) {
347 str[0] = '\0';
348 fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
349 pr_info(" %-11s: read(0x%04X), sid=%d, len=%d; %s\n",
350 vreg->rdesc.name, vreg->base_addr + addr,
351 vreg->spmi_dev->sid, len, str);
352 }
353
354 return rc;
355}
356
357static inline int qpnp_vreg_write(struct qpnp_regulator *vreg, u16 addr,
358 u8 *buf, int len)
359{
360 char str[DEBUG_PRINT_BUFFER_SIZE];
361 int rc = 0;
362
363 if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_WRITES) {
364 str[0] = '\0';
365 fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
366 pr_info("%-11s: write(0x%04X), sid=%d, len=%d; %s\n",
367 vreg->rdesc.name, vreg->base_addr + addr,
368 vreg->spmi_dev->sid, len, str);
369 }
370
371 rc = spmi_ext_register_writel(vreg->spmi_dev->ctrl,
372 vreg->spmi_dev->sid, vreg->base_addr + addr, buf, len);
373 if (!rc)
374 vreg->write_count += len;
375
376 return rc;
377}
378
379/*
380 * qpnp_vreg_write_optimized - write the minimum sized contiguous subset of buf
381 * @vreg: qpnp_regulator pointer for this regulator
382 * @addr: local SPMI address offset from this peripheral's base address
383 * @buf: new data to write into the SPMI registers
384 * @buf_save: old data in the registers
385 * @len: number of bytes to write
386 *
387 * This function checks for unchanged register values between buf and buf_save
388 * starting at both ends of buf. Only the contiguous subset in the middle of
389 * buf starting and ending with new values is sent.
390 *
391 * Consider the following example:
392 * buf offset: 0 1 2 3 4 5 6 7
393 * reg state: U U C C U C U U
394 * (U = unchanged, C = changed)
395 * In this example registers 2 through 5 will be written with a single
396 * transaction.
397 */
398static inline int qpnp_vreg_write_optimized(struct qpnp_regulator *vreg,
399 u16 addr, u8 *buf, u8 *buf_save, int len)
400{
401 int i, rc, start, end;
402
403 for (i = 0; i < len; i++)
404 if (buf[i] != buf_save[i])
405 break;
406 start = i;
407
408 for (i = len - 1; i >= 0; i--)
409 if (buf[i] != buf_save[i])
410 break;
411 end = i;
412
413 if (start > end) {
414 /* No modified register values present. */
415 return 0;
416 }
417
418 rc = qpnp_vreg_write(vreg, addr + start, &buf[start], end - start + 1);
419 if (!rc)
420 for (i = start; i <= end; i++)
421 buf_save[i] = buf[i];
422
423 return rc;
424}
425
426/*
427 * Perform a masked write to a PMIC register only if the new value differs
428 * from the last value written to the register. This removes redundant
429 * register writing.
430 */
431static int qpnp_vreg_masked_write(struct qpnp_regulator *vreg, u16 addr, u8 val,
432 u8 mask, u8 *reg_save)
433{
434 int rc = 0;
435 u8 reg;
436
437 reg = (*reg_save & ~mask) | (val & mask);
438 if (reg != *reg_save) {
439 rc = qpnp_vreg_write(vreg, addr, &reg, 1);
440
441 if (rc) {
442 vreg_err(vreg, "write failed; addr=0x%03X, rc=%d\n",
443 addr, rc);
444 } else {
445 *reg_save = reg;
446 }
447 }
448
449 return rc;
450}
451
452/*
453 * Perform a masked read-modify-write to a PMIC register only if the new value
454 * differs from the value currently in the register. This removes redundant
455 * register writing.
456 */
457static int qpnp_vreg_masked_read_write(struct qpnp_regulator *vreg, u16 addr,
458 u8 val, u8 mask)
459{
460 int rc;
461 u8 reg;
462
463 rc = qpnp_vreg_read(vreg, addr, &reg, 1);
464 if (rc) {
465 vreg_err(vreg, "read failed; addr=0x%03X, rc=%d\n", addr, rc);
466 return rc;
467 }
468
469 return qpnp_vreg_masked_write(vreg, addr, val, mask, &reg);
470}
471
472static int qpnp_regulator_common_is_enabled(struct regulator_dev *rdev)
473{
474 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
475
476 return (vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]
477 & QPNP_COMMON_ENABLE_MASK)
478 == QPNP_COMMON_ENABLE;
479}
480
481static int qpnp_regulator_common_enable(struct regulator_dev *rdev)
482{
483 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
484 int rc;
485
486 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
487 QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
488 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
489
490 if (rc)
491 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
492 else
493 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_ENABLE);
494
495 return rc;
496}
497
498static int qpnp_regulator_vs_enable(struct regulator_dev *rdev)
499{
500 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
501 int rc;
502 u8 reg;
503
504 if (vreg->ocp_enable == QPNP_REGULATOR_ENABLE) {
505 /* Disable OCP */
506 reg = QPNP_VS_OCP_DISABLE;
507 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
508 if (rc)
509 goto fail;
510 }
511
512 rc = qpnp_regulator_common_enable(rdev);
513 if (rc)
514 goto fail;
515
516 if (vreg->ocp_enable == QPNP_REGULATOR_ENABLE) {
517 /* Wait for inrush current to subsided, then enable OCP. */
518 udelay(vreg->ocp_enable_time);
519 reg = QPNP_VS_OCP_ENABLE_MASK;
520 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
521 if (rc)
522 goto fail;
523 }
524
525 return rc;
526fail:
527 vreg_err(vreg, "qpnp_vreg_write failed, rc=%d\n", rc);
528
529 return rc;
530}
531
532static int qpnp_regulator_common_disable(struct regulator_dev *rdev)
533{
534 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
535 int rc;
536
537 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
538 QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
539 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
540
541 if (rc)
542 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
543 else
544 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_DISABLE);
545
546 return rc;
547}
548
549static int qpnp_regulator_select_voltage(struct qpnp_regulator *vreg,
550 int min_uV, int max_uV, int *range_sel, int *voltage_sel)
551{
552 struct qpnp_voltage_range *range;
553 int uV = min_uV;
554 int lim_min_uV, lim_max_uV, i;
555
556 /* Check if request voltage is outside of physically settable range. */
557 lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
558 lim_max_uV =
559 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
560
561 if (uV < lim_min_uV && max_uV >= lim_min_uV)
562 uV = lim_min_uV;
563
564 if (uV < lim_min_uV || uV > lim_max_uV) {
565 vreg_err(vreg,
566 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
567 min_uV, max_uV, lim_min_uV, lim_max_uV);
568 return -EINVAL;
569 }
570
571 /* Find the range which uV is inside of. */
572 for (i = vreg->set_points->count - 1; i > 0; i--)
573 if (uV > vreg->set_points->range[i - 1].max_uV)
574 break;
575 range = &vreg->set_points->range[i];
576 *range_sel = range->range_sel;
577
578 /*
579 * Force uV to be an allowed set point by applying a ceiling function to
580 * the uV value.
581 */
582 *voltage_sel = (uV - range->min_uV + range->step_uV - 1)
583 / range->step_uV;
584 uV = *voltage_sel * range->step_uV + range->min_uV;
585
586 if (uV > max_uV) {
587 vreg_err(vreg,
588 "request v=[%d, %d] cannot be met by any set point; "
589 "next set point: %d\n",
590 min_uV, max_uV, uV);
591 return -EINVAL;
592 }
593
594 return 0;
595}
596
597static int qpnp_regulator_common_set_voltage(struct regulator_dev *rdev,
598 int min_uV, int max_uV, unsigned *selector)
599{
600 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
601 int rc, range_sel, voltage_sel;
602 u8 buf[2];
603
604 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
605 &voltage_sel);
606 if (rc) {
607 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
608 return rc;
609 }
610
611 buf[0] = range_sel;
612 buf[1] = voltage_sel;
613 if ((vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] != range_sel)
614 && (vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] == voltage_sel)) {
615 /* Handle latched range change. */
616 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
617 buf, 2);
618 if (!rc) {
619 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = buf[0];
620 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] = buf[1];
621 }
622 } else {
623 /* Either write can be optimized away safely. */
624 rc = qpnp_vreg_write_optimized(vreg,
625 QPNP_COMMON_REG_VOLTAGE_RANGE, buf,
626 &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE], 2);
627 }
628
629 if (rc)
630 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
631 else
632 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
633
634 return rc;
635}
636
637static int qpnp_regulator_common_get_voltage(struct regulator_dev *rdev)
638{
639 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
640 struct qpnp_voltage_range *range = NULL;
641 int range_sel, voltage_sel, i;
642
643 range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
644 voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
645
646 for (i = 0; i < vreg->set_points->count; i++) {
647 if (vreg->set_points->range[i].range_sel == range_sel) {
648 range = &vreg->set_points->range[i];
649 break;
650 }
651 }
652
653 if (!range) {
654 vreg_err(vreg, "voltage unknown, range %d is invalid\n",
655 range_sel);
David Collinsbdd32812012-05-10 13:22:56 -0700656 return VOLTAGE_UNKNOWN;
David Collinsd1ac2f12012-02-14 13:34:18 -0800657 }
658
659 return range->step_uV * voltage_sel + range->min_uV;
660}
661
662static int qpnp_regulator_boost_set_voltage(struct regulator_dev *rdev,
663 int min_uV, int max_uV, unsigned *selector)
664{
665 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
666 int rc, range_sel, voltage_sel;
667
668 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
669 &voltage_sel);
670 if (rc) {
671 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
672 return rc;
673 }
674
675 /*
676 * Boost type regulators do not have range select register so only
677 * voltage set register needs to be written.
678 */
679 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET,
680 voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]);
681
682 if (rc)
683 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
684 else
685 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
686
687 return rc;
688}
689
690static int qpnp_regulator_boost_get_voltage(struct regulator_dev *rdev)
691{
692 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
693 int voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
694
695 return boost_ranges[0].step_uV * voltage_sel + boost_ranges[0].min_uV;
696}
697
698static int qpnp_regulator_common_list_voltage(struct regulator_dev *rdev,
699 unsigned selector)
700{
701 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
702 int uV = 0;
703 int i;
704
705 if (selector >= vreg->set_points->n_voltages)
706 return 0;
707
708 for (i = 0; i < vreg->set_points->count; i++) {
709 if (selector < vreg->set_points->range[i].n_voltages) {
710 uV = selector * vreg->set_points->range[i].step_uV
711 + vreg->set_points->range[i].set_point_min_uV;
712 break;
713 } else {
714 selector -= vreg->set_points->range[i].n_voltages;
715 }
716 }
717
718 return uV;
719}
720
721static unsigned int qpnp_regulator_common_get_mode(struct regulator_dev *rdev)
722{
723 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
724
725 return (vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]
726 & QPNP_COMMON_MODE_HPM_MASK)
727 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
728}
729
730static int qpnp_regulator_common_set_mode(struct regulator_dev *rdev,
731 unsigned int mode)
732{
733 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
734 int rc = 0;
735 u8 val;
736
737 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
738 vreg_err(vreg, "invalid mode: %u\n", mode);
739 return -EINVAL;
740 }
741
742 val = (mode == REGULATOR_MODE_NORMAL ? QPNP_COMMON_MODE_HPM_MASK : 0);
743
744 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_MODE, val,
745 QPNP_COMMON_MODE_HPM_MASK,
746 &vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]);
747
748 if (rc)
749 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
750 else
751 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE);
752
753 return rc;
754}
755
756static unsigned int qpnp_regulator_common_get_optimum_mode(
757 struct regulator_dev *rdev, int input_uV, int output_uV,
758 int load_uA)
759{
760 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
761 unsigned int mode;
762
763 if (load_uA + vreg->system_load >= vreg->hpm_min_load)
764 mode = REGULATOR_MODE_NORMAL;
765 else
766 mode = REGULATOR_MODE_IDLE;
767
768 return mode;
769}
770
771static int qpnp_regulator_common_enable_time(struct regulator_dev *rdev)
772{
773 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
774
775 return vreg->enable_time;
776}
777
778static const char const *qpnp_print_actions[] = {
779 [QPNP_REGULATOR_ACTION_INIT] = "initial ",
780 [QPNP_REGULATOR_ACTION_ENABLE] = "enable ",
781 [QPNP_REGULATOR_ACTION_DISABLE] = "disable ",
782 [QPNP_REGULATOR_ACTION_VOLTAGE] = "set voltage",
783 [QPNP_REGULATOR_ACTION_MODE] = "set mode ",
784};
785
786static void qpnp_vreg_show_state(struct regulator_dev *rdev,
787 enum qpnp_regulator_action action)
788{
789 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
790 const char *action_label = qpnp_print_actions[action];
791 unsigned int mode = 0;
792 int uV = 0;
793 const char *mode_label = "";
794 enum qpnp_regulator_logical_type type;
795 const char *enable_label;
796 char pc_enable_label[5] = {'\0'};
797 char pc_mode_label[8] = {'\0'};
798 bool show_req, show_dupe, show_init, has_changed;
799 u8 en_reg, mode_reg;
800
801 /* Do not print unless appropriate flags are set. */
802 show_req = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_REQUEST;
803 show_dupe = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_DUPLICATE;
804 show_init = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT;
805 has_changed = vreg->write_count != vreg->prev_write_count;
806 if (!((show_init && action == QPNP_REGULATOR_ACTION_INIT)
807 || (show_req && (has_changed || show_dupe)))) {
808 return;
809 }
810
811 vreg->prev_write_count = vreg->write_count;
812
813 type = vreg->logical_type;
814
815 enable_label = qpnp_regulator_common_is_enabled(rdev) ? "on " : "off";
816
817 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
818 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
819 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
820 uV = qpnp_regulator_common_get_voltage(rdev);
821
822 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST)
823 uV = qpnp_regulator_boost_get_voltage(rdev);
824
825 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
826 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
827 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS) {
828 mode = qpnp_regulator_common_get_mode(rdev);
829 mode_label = mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM";
830 }
831
832 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
833 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
834 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
835 en_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE];
836 pc_enable_label[0] =
837 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK ? '3' : '_';
838 pc_enable_label[1] =
839 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK ? '2' : '_';
840 pc_enable_label[2] =
841 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK ? '1' : '_';
842 pc_enable_label[3] =
843 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK ? '0' : '_';
844 }
845
846 switch (type) {
847 case QPNP_REGULATOR_LOGICAL_TYPE_SMPS:
848 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
849 pc_mode_label[0] =
850 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
851 pc_mode_label[1] =
852 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
853 pc_mode_label[2] =
854 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
855 pc_mode_label[3] =
856 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
857 pc_mode_label[4] =
858 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
859 pc_mode_label[5] =
860 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
861
862 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
863 "alt_mode=%s\n",
864 action_label, vreg->rdesc.name, enable_label, uV,
865 mode_label, pc_enable_label, pc_mode_label);
866 break;
867 case QPNP_REGULATOR_LOGICAL_TYPE_LDO:
868 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
869 pc_mode_label[0] =
870 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
871 pc_mode_label[1] =
872 mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
873 pc_mode_label[2] =
874 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
875 pc_mode_label[3] =
876 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
877 pc_mode_label[4] =
878 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
879 pc_mode_label[5] =
880 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
881 pc_mode_label[6] =
882 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
883
884 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
885 "alt_mode=%s\n",
886 action_label, vreg->rdesc.name, enable_label, uV,
887 mode_label, pc_enable_label, pc_mode_label);
888 break;
889 case QPNP_REGULATOR_LOGICAL_TYPE_VS:
890 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
891 pc_mode_label[0] =
892 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
893 pc_mode_label[1] =
894 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
895
896 pr_info("%s %-11s: %s, pc_en=%s, alt_mode=%s\n",
897 action_label, vreg->rdesc.name, enable_label,
898 pc_enable_label, pc_mode_label);
899 break;
900 case QPNP_REGULATOR_LOGICAL_TYPE_BOOST:
901 pr_info("%s %-11s: %s, v=%7d uV\n",
902 action_label, vreg->rdesc.name, enable_label, uV);
903 break;
904 case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS:
905 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
906 pc_mode_label[0] =
907 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
908
909 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
910 action_label, vreg->rdesc.name, enable_label, uV,
911 mode_label, pc_mode_label);
912 break;
913 default:
914 break;
915 }
916}
917
918static struct regulator_ops qpnp_smps_ops = {
919 .enable = qpnp_regulator_common_enable,
920 .disable = qpnp_regulator_common_disable,
921 .is_enabled = qpnp_regulator_common_is_enabled,
922 .set_voltage = qpnp_regulator_common_set_voltage,
923 .get_voltage = qpnp_regulator_common_get_voltage,
924 .list_voltage = qpnp_regulator_common_list_voltage,
925 .set_mode = qpnp_regulator_common_set_mode,
926 .get_mode = qpnp_regulator_common_get_mode,
927 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
928 .enable_time = qpnp_regulator_common_enable_time,
929};
930
931static struct regulator_ops qpnp_ldo_ops = {
932 .enable = qpnp_regulator_common_enable,
933 .disable = qpnp_regulator_common_disable,
934 .is_enabled = qpnp_regulator_common_is_enabled,
935 .set_voltage = qpnp_regulator_common_set_voltage,
936 .get_voltage = qpnp_regulator_common_get_voltage,
937 .list_voltage = qpnp_regulator_common_list_voltage,
938 .set_mode = qpnp_regulator_common_set_mode,
939 .get_mode = qpnp_regulator_common_get_mode,
940 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
941 .enable_time = qpnp_regulator_common_enable_time,
942};
943
944static struct regulator_ops qpnp_vs_ops = {
945 .enable = qpnp_regulator_vs_enable,
946 .disable = qpnp_regulator_common_disable,
947 .is_enabled = qpnp_regulator_common_is_enabled,
948 .enable_time = qpnp_regulator_common_enable_time,
949};
950
951static struct regulator_ops qpnp_boost_ops = {
952 .enable = qpnp_regulator_common_enable,
953 .disable = qpnp_regulator_common_disable,
954 .is_enabled = qpnp_regulator_common_is_enabled,
955 .set_voltage = qpnp_regulator_boost_set_voltage,
956 .get_voltage = qpnp_regulator_boost_get_voltage,
957 .list_voltage = qpnp_regulator_common_list_voltage,
958 .enable_time = qpnp_regulator_common_enable_time,
959};
960
961static struct regulator_ops qpnp_ftsmps_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_common_set_voltage,
966 .get_voltage = qpnp_regulator_common_get_voltage,
967 .list_voltage = qpnp_regulator_common_list_voltage,
968 .set_mode = qpnp_regulator_common_set_mode,
969 .get_mode = qpnp_regulator_common_get_mode,
970 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
971 .enable_time = qpnp_regulator_common_enable_time,
972};
973
David Collinsbad606a2012-08-21 10:57:36 -0700974/* Maximum possible digital major revision value */
975#define INF 0xFF
976
David Collinsd1ac2f12012-02-14 13:34:18 -0800977static const struct qpnp_regulator_mapping supported_regulators[] = {
David Collinsbad606a2012-08-21 10:57:36 -0700978 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
979 QPNP_VREG_MAP(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
980 QPNP_VREG_MAP(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
981 QPNP_VREG_MAP(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
982 QPNP_VREG_MAP(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
983 QPNP_VREG_MAP(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
984 QPNP_VREG_MAP(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
985 QPNP_VREG_MAP(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
986 QPNP_VREG_MAP(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
987 QPNP_VREG_MAP(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
988 QPNP_VREG_MAP(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
989 QPNP_VREG_MAP(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
990 QPNP_VREG_MAP(VS, LV100, 0, INF, VS, vs, none, 0),
991 QPNP_VREG_MAP(VS, LV300, 0, INF, VS, vs, none, 0),
992 QPNP_VREG_MAP(VS, MV300, 0, INF, VS, vs, none, 0),
993 QPNP_VREG_MAP(VS, MV500, 0, INF, VS, vs, none, 0),
994 QPNP_VREG_MAP(VS, HDMI, 0, INF, VS, vs, none, 0),
995 QPNP_VREG_MAP(VS, OTG, 0, INF, VS, vs, none, 0),
996 QPNP_VREG_MAP(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
997 QPNP_VREG_MAP(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
David Collinsd1ac2f12012-02-14 13:34:18 -0800998};
999
1000static int qpnp_regulator_match(struct qpnp_regulator *vreg)
1001{
1002 const struct qpnp_regulator_mapping *mapping;
Michael Bohan9328e492012-08-09 11:37:36 -07001003 struct device_node *node = vreg->spmi_dev->dev.of_node;
David Collinsd1ac2f12012-02-14 13:34:18 -08001004 int rc, i;
David Collinsbad606a2012-08-21 10:57:36 -07001005 u32 type_reg[2], dig_major_rev;
1006 u8 version[QPNP_COMMON_REG_SUBTYPE - QPNP_COMMON_REG_DIG_MAJOR_REV + 1];
1007 u8 type, subtype;
David Collinsd1ac2f12012-02-14 13:34:18 -08001008
David Collinsbad606a2012-08-21 10:57:36 -07001009 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_DIG_MAJOR_REV, version,
1010 ARRAY_SIZE(version));
1011 if (rc) {
1012 vreg_err(vreg, "could not read version registers, rc=%d\n", rc);
1013 return rc;
1014 }
1015 dig_major_rev = version[QPNP_COMMON_REG_DIG_MAJOR_REV
1016 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1017 type = version[QPNP_COMMON_REG_TYPE
1018 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1019 subtype = version[QPNP_COMMON_REG_SUBTYPE
1020 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1021
1022 /*
1023 * Override type and subtype register values if qcom,force-type is
1024 * present in the device tree node.
1025 */
1026 rc = of_property_read_u32_array(node, "qcom,force-type", type_reg, 2);
Michael Bohan9328e492012-08-09 11:37:36 -07001027 if (!rc) {
1028 type = type_reg[0];
1029 subtype = type_reg[1];
David Collinsd1ac2f12012-02-14 13:34:18 -08001030 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001031
1032 rc = -ENODEV;
1033 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1034 mapping = &supported_regulators[i];
David Collinsbad606a2012-08-21 10:57:36 -07001035 if (mapping->type == type && mapping->subtype == subtype
1036 && mapping->revision_min <= dig_major_rev
1037 && mapping->revision_max >= dig_major_rev) {
David Collinsd1ac2f12012-02-14 13:34:18 -08001038 vreg->logical_type = mapping->logical_type;
1039 vreg->set_points = mapping->set_points;
1040 vreg->hpm_min_load = mapping->hpm_min_load;
1041 vreg->rdesc.ops = mapping->ops;
1042 vreg->rdesc.n_voltages
1043 = mapping->set_points->n_voltages;
1044 rc = 0;
1045 break;
1046 }
1047 }
1048
1049 return rc;
1050}
1051
1052static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg,
1053 struct qpnp_regulator_platform_data *pdata)
1054{
1055 int rc, i;
1056 enum qpnp_regulator_logical_type type;
1057 u8 ctrl_reg[8], reg, mask;
1058
1059 type = vreg->logical_type;
1060
1061 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1062 vreg->ctrl_reg, 8);
1063 if (rc) {
1064 vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
1065 return rc;
1066 }
1067
1068 for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++)
1069 ctrl_reg[i] = vreg->ctrl_reg[i];
1070
1071 /* Set up enable pin control. */
1072 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1073 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1074 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1075 && !(pdata->pin_ctrl_enable
1076 & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1077 ctrl_reg[QPNP_COMMON_IDX_ENABLE] &=
1078 ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1079 ctrl_reg[QPNP_COMMON_IDX_ENABLE] |=
1080 pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1081 }
1082
1083 /* Set up auto mode control. */
1084 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1085 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1086 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1087 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
1088 && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
1089 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1090 ~QPNP_COMMON_MODE_AUTO_MASK;
1091 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1092 (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0);
1093 }
1094
1095 /* Set up mode pin control. */
1096 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1097 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO)
1098 && !(pdata->pin_ctrl_hpm
1099 & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1100 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1101 ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1102 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1103 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1104 }
1105
1106 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1107 && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1108 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1109 ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1110 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1111 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1112 }
1113
1114 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1115 && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1116 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1117 ~QPNP_COMMON_MODE_BYPASS_MASK;
1118 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1119 (pdata->bypass_mode_enable
1120 ? QPNP_COMMON_MODE_BYPASS_MASK : 0);
1121 }
1122
1123 /* Set boost current limit. */
1124 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
1125 && pdata->boost_current_limit
1126 != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) {
1127 ctrl_reg[QPNP_BOOST_IDX_CURRENT_LIMIT] &=
1128 ~QPNP_BOOST_CURRENT_LIMIT_MASK;
1129 ctrl_reg[QPNP_BOOST_IDX_CURRENT_LIMIT] |=
1130 pdata->boost_current_limit & QPNP_BOOST_CURRENT_LIMIT_MASK;
1131 }
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);