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