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