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