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