blob: 0e836c7c628710f65407bcb4da56e81b7ac296c2 [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 Collins45b86ac2012-08-09 09:44:15 -0700271 VOLTAGE_RANGE(0, 0, 350000, 1275000, 5000),
272 VOLTAGE_RANGE(1, 0, 1280000, 2040000, 10000),
David Collinsd1ac2f12012-02-14 13:34:18 -0800273};
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;
Michael Bohan9328e492012-08-09 11:37:36 -0700985 struct device_node *node = vreg->spmi_dev->dev.of_node;
David Collinsd1ac2f12012-02-14 13:34:18 -0800986 int rc, i;
987 u8 raw_type[2], type, subtype;
Michael Bohan9328e492012-08-09 11:37:36 -0700988 u32 type_reg[2];
David Collinsd1ac2f12012-02-14 13:34:18 -0800989
Michael Bohan9328e492012-08-09 11:37:36 -0700990 rc = of_property_read_u32_array(node, "qcom,force-type",
991 type_reg, 2);
992 if (!rc) {
993 type = type_reg[0];
994 subtype = type_reg[1];
995 } else {
996 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_TYPE, raw_type, 2);
997 if (rc) {
998 vreg_err(vreg,
999 "could not read type register, rc=%d\n", rc);
1000 return rc;
1001 }
1002 type = raw_type[0];
1003 subtype = raw_type[1];
David Collinsd1ac2f12012-02-14 13:34:18 -08001004 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001005
1006 rc = -ENODEV;
1007 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1008 mapping = &supported_regulators[i];
1009 if (mapping->type == type && mapping->subtype == subtype) {
1010 vreg->logical_type = mapping->logical_type;
1011 vreg->set_points = mapping->set_points;
1012 vreg->hpm_min_load = mapping->hpm_min_load;
1013 vreg->rdesc.ops = mapping->ops;
1014 vreg->rdesc.n_voltages
1015 = mapping->set_points->n_voltages;
1016 rc = 0;
1017 break;
1018 }
1019 }
1020
1021 return rc;
1022}
1023
1024static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg,
1025 struct qpnp_regulator_platform_data *pdata)
1026{
1027 int rc, i;
1028 enum qpnp_regulator_logical_type type;
1029 u8 ctrl_reg[8], reg, mask;
1030
1031 type = vreg->logical_type;
1032
1033 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1034 vreg->ctrl_reg, 8);
1035 if (rc) {
1036 vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
1037 return rc;
1038 }
1039
1040 for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++)
1041 ctrl_reg[i] = vreg->ctrl_reg[i];
1042
1043 /* Set up enable pin control. */
1044 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1045 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1046 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1047 && !(pdata->pin_ctrl_enable
1048 & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1049 ctrl_reg[QPNP_COMMON_IDX_ENABLE] &=
1050 ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1051 ctrl_reg[QPNP_COMMON_IDX_ENABLE] |=
1052 pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1053 }
1054
1055 /* Set up auto mode control. */
1056 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1057 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1058 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1059 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
1060 && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
1061 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1062 ~QPNP_COMMON_MODE_AUTO_MASK;
1063 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1064 (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0);
1065 }
1066
1067 /* Set up mode pin control. */
1068 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1069 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO)
1070 && !(pdata->pin_ctrl_hpm
1071 & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1072 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1073 ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1074 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1075 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1076 }
1077
1078 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1079 && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1080 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1081 ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1082 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1083 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1084 }
1085
1086 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1087 && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1088 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1089 ~QPNP_COMMON_MODE_BYPASS_MASK;
1090 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1091 (pdata->bypass_mode_enable
1092 ? QPNP_COMMON_MODE_BYPASS_MASK : 0);
1093 }
1094
1095 /* Set boost current limit. */
1096 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
1097 && pdata->boost_current_limit
1098 != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) {
1099 ctrl_reg[QPNP_BOOST_IDX_CURRENT_LIMIT] &=
1100 ~QPNP_BOOST_CURRENT_LIMIT_MASK;
1101 ctrl_reg[QPNP_BOOST_IDX_CURRENT_LIMIT] |=
1102 pdata->boost_current_limit & QPNP_BOOST_CURRENT_LIMIT_MASK;
1103 }
1104
1105 /* Write back any control register values that were modified. */
1106 rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1107 ctrl_reg, vreg->ctrl_reg, 8);
1108 if (rc) {
1109 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1110 return rc;
1111 }
1112
1113 /* Set pull down. */
1114 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1115 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1116 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1117 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1118 reg = pdata->pull_down_enable
1119 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1120 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_PULL_DOWN, &reg, 1);
1121 if (rc) {
1122 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1123 return rc;
1124 }
1125 }
1126
1127 if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
1128 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1129 /* FTSMPS has other bits in the pull down control register. */
1130 reg = pdata->pull_down_enable
1131 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1132 rc = qpnp_vreg_masked_read_write(vreg,
1133 QPNP_COMMON_REG_PULL_DOWN, reg,
1134 QPNP_COMMON_PULL_DOWN_ENABLE_MASK);
1135 if (rc) {
1136 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1137 return rc;
1138 }
1139 }
1140
1141 /* Set soft start for LDO. */
1142 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1143 && pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1144 reg = pdata->soft_start_enable
1145 ? QPNP_LDO_SOFT_START_ENABLE_MASK : 0;
1146 rc = qpnp_vreg_write(vreg, QPNP_LDO_REG_SOFT_START, &reg, 1);
1147 if (rc) {
1148 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1149 return rc;
1150 }
1151 }
1152
1153 /* Set soft start strength and over current protection for VS. */
1154 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
1155 reg = 0;
1156 mask = 0;
1157 if (pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1158 reg |= pdata->soft_start_enable
1159 ? QPNP_VS_SOFT_START_ENABLE_MASK : 0;
1160 mask |= QPNP_VS_SOFT_START_ENABLE_MASK;
1161 }
1162 if (pdata->vs_soft_start_strength
1163 != QPNP_VS_SOFT_START_STR_HW_DEFAULT) {
1164 reg |= pdata->vs_soft_start_strength
1165 & QPNP_VS_SOFT_START_SEL_MASK;
1166 mask |= QPNP_VS_SOFT_START_SEL_MASK;
1167 }
1168 rc = qpnp_vreg_masked_read_write(vreg, QPNP_VS_REG_SOFT_START,
1169 reg, mask);
1170 if (rc) {
1171 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1172 return rc;
1173 }
1174
1175 if (pdata->ocp_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1176 reg = pdata->ocp_enable ? QPNP_VS_OCP_ENABLE_MASK : 0;
1177 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
1178 if (rc) {
1179 vreg_err(vreg, "spmi write failed, rc=%d\n",
1180 rc);
1181 return rc;
1182 }
1183 }
1184 }
1185
1186 return rc;
1187}
1188
1189/* Fill in pdata elements based on values found in device tree. */
1190static int qpnp_regulator_get_dt_config(struct spmi_device *spmi,
1191 struct qpnp_regulator_platform_data *pdata)
1192{
1193 struct resource *res;
1194 struct device_node *node = spmi->dev.of_node;
1195 int rc = 0;
1196
1197 pdata->init_data.constraints.input_uV
1198 = pdata->init_data.constraints.max_uV;
1199
Michael Bohan0e5534d2012-05-22 17:33:45 -07001200 res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
David Collinsd1ac2f12012-02-14 13:34:18 -08001201 if (!res) {
1202 dev_err(&spmi->dev, "%s: node is missing base address\n",
1203 __func__);
1204 return -EINVAL;
1205 }
1206 pdata->base_addr = res->start;
1207
1208 /*
1209 * Initialize configuration parameters to use hardware default in case
1210 * no value is specified via device tree.
1211 */
1212 pdata->auto_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1213 pdata->bypass_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1214 pdata->ocp_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1215 pdata->pull_down_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1216 pdata->soft_start_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1217 pdata->boost_current_limit = QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT;
1218 pdata->pin_ctrl_enable = QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1219 pdata->pin_ctrl_hpm = QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1220 pdata->vs_soft_start_strength = QPNP_VS_SOFT_START_STR_HW_DEFAULT;
1221
1222 /* These bindings are optional, so it is okay if they are not found. */
1223 of_property_read_u32(node, "qcom,auto-mode-enable",
1224 &pdata->auto_mode_enable);
1225 of_property_read_u32(node, "qcom,bypass-mode-enable",
1226 &pdata->bypass_mode_enable);
1227 of_property_read_u32(node, "qcom,ocp-enable", &pdata->ocp_enable);
1228 of_property_read_u32(node, "qcom,pull-down-enable",
1229 &pdata->pull_down_enable);
1230 of_property_read_u32(node, "qcom,soft-start-enable",
1231 &pdata->soft_start_enable);
1232 of_property_read_u32(node, "qcom,boost-current-limit",
1233 &pdata->boost_current_limit);
1234 of_property_read_u32(node, "qcom,pin-ctrl-enable",
1235 &pdata->pin_ctrl_enable);
1236 of_property_read_u32(node, "qcom,pin-ctrl-hpm", &pdata->pin_ctrl_hpm);
1237 of_property_read_u32(node, "qcom,vs-soft-start-strength",
1238 &pdata->vs_soft_start_strength);
1239 of_property_read_u32(node, "qcom,system-load", &pdata->system_load);
1240 of_property_read_u32(node, "qcom,enable-time", &pdata->enable_time);
1241 of_property_read_u32(node, "qcom,ocp-enable-time",
1242 &pdata->ocp_enable_time);
1243
1244 return rc;
1245}
1246
1247static struct of_device_id spmi_match_table[];
1248
1249#define MAX_NAME_LEN 127
1250
1251static int __devinit qpnp_regulator_probe(struct spmi_device *spmi)
1252{
1253 struct qpnp_regulator_platform_data *pdata;
1254 struct qpnp_regulator *vreg;
1255 struct regulator_desc *rdesc;
1256 struct qpnp_regulator_platform_data of_pdata;
1257 struct regulator_init_data *init_data;
1258 char *reg_name;
1259 int rc;
1260 bool is_dt;
1261
1262 vreg = kzalloc(sizeof(struct qpnp_regulator), GFP_KERNEL);
1263 if (!vreg) {
1264 dev_err(&spmi->dev, "%s: Can't allocate qpnp_regulator\n",
1265 __func__);
1266 return -ENOMEM;
1267 }
1268
1269 is_dt = of_match_device(spmi_match_table, &spmi->dev);
1270
1271 /* Check if device tree is in use. */
1272 if (is_dt) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001273 init_data = of_get_regulator_init_data(&spmi->dev,
1274 spmi->dev.of_node);
David Collinsd1ac2f12012-02-14 13:34:18 -08001275 if (!init_data) {
1276 dev_err(&spmi->dev, "%s: unable to allocate memory\n",
1277 __func__);
1278 kfree(vreg);
1279 return -ENOMEM;
1280 }
1281 memset(&of_pdata, 0,
1282 sizeof(struct qpnp_regulator_platform_data));
1283 memcpy(&of_pdata.init_data, init_data,
1284 sizeof(struct regulator_init_data));
1285
1286 if (of_get_property(spmi->dev.of_node, "parent-supply", NULL))
1287 of_pdata.init_data.supply_regulator = "parent";
1288
1289 rc = qpnp_regulator_get_dt_config(spmi, &of_pdata);
1290 if (rc) {
1291 dev_err(&spmi->dev, "%s: DT parsing failed, rc=%d\n",
1292 __func__, rc);
1293 kfree(vreg);
1294 return -ENOMEM;
1295 }
1296
1297 pdata = &of_pdata;
1298 } else {
1299 pdata = spmi->dev.platform_data;
1300 }
1301
1302 if (pdata == NULL) {
1303 dev_err(&spmi->dev, "%s: no platform data specified\n",
1304 __func__);
1305 kfree(vreg);
1306 return -EINVAL;
1307 }
1308
1309 vreg->spmi_dev = spmi;
1310 vreg->prev_write_count = -1;
1311 vreg->write_count = 0;
1312 vreg->base_addr = pdata->base_addr;
1313 vreg->enable_time = pdata->enable_time;
1314 vreg->system_load = pdata->system_load;
1315 vreg->ocp_enable = pdata->ocp_enable;
1316 vreg->ocp_enable_time = pdata->ocp_enable_time;
1317
1318 rdesc = &vreg->rdesc;
1319 rdesc->id = spmi->ctrl->nr;
1320 rdesc->owner = THIS_MODULE;
1321 rdesc->type = REGULATOR_VOLTAGE;
1322
1323 reg_name = kzalloc(strnlen(pdata->init_data.constraints.name,
1324 MAX_NAME_LEN) + 1, GFP_KERNEL);
1325 if (!reg_name) {
1326 dev_err(&spmi->dev, "%s: Can't allocate regulator name\n",
1327 __func__);
1328 kfree(vreg);
1329 return -ENOMEM;
1330 }
1331 strlcpy(reg_name, pdata->init_data.constraints.name,
1332 strnlen(pdata->init_data.constraints.name, MAX_NAME_LEN) + 1);
1333 rdesc->name = reg_name;
1334
1335 dev_set_drvdata(&spmi->dev, vreg);
1336
1337 rc = qpnp_regulator_match(vreg);
1338 if (rc) {
1339 vreg_err(vreg, "regulator type unknown, rc=%d\n", rc);
1340 goto bail;
1341 }
1342
1343 if (is_dt && rdesc->ops) {
1344 /* Fill in ops and mode masks when using device tree. */
1345 if (rdesc->ops->enable)
1346 pdata->init_data.constraints.valid_ops_mask
1347 |= REGULATOR_CHANGE_STATUS;
1348 if (rdesc->ops->get_voltage)
1349 pdata->init_data.constraints.valid_ops_mask
1350 |= REGULATOR_CHANGE_VOLTAGE;
1351 if (rdesc->ops->get_mode) {
1352 pdata->init_data.constraints.valid_ops_mask
1353 |= REGULATOR_CHANGE_MODE
1354 | REGULATOR_CHANGE_DRMS;
1355 pdata->init_data.constraints.valid_modes_mask
1356 = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1357 }
1358 }
1359
1360 rc = qpnp_regulator_init_registers(vreg, pdata);
1361 if (rc) {
1362 vreg_err(vreg, "common initialization failed, rc=%d\n", rc);
1363 goto bail;
1364 }
1365
1366 vreg->rdev = regulator_register(rdesc, &spmi->dev,
1367 &(pdata->init_data), vreg, spmi->dev.of_node);
1368 if (IS_ERR(vreg->rdev)) {
1369 rc = PTR_ERR(vreg->rdev);
1370 vreg_err(vreg, "regulator_register failed, rc=%d\n", rc);
1371 goto bail;
1372 }
1373
1374 qpnp_vreg_show_state(vreg->rdev, QPNP_REGULATOR_ACTION_INIT);
1375
1376 return 0;
1377
1378bail:
1379 if (rc)
1380 vreg_err(vreg, "probe failed, rc=%d\n", rc);
1381
1382 kfree(vreg->rdesc.name);
1383 kfree(vreg);
1384
1385 return rc;
1386}
1387
1388static int __devexit qpnp_regulator_remove(struct spmi_device *spmi)
1389{
1390 struct qpnp_regulator *vreg;
1391
1392 vreg = dev_get_drvdata(&spmi->dev);
1393 dev_set_drvdata(&spmi->dev, NULL);
1394
1395 if (vreg) {
1396 regulator_unregister(vreg->rdev);
1397 kfree(vreg->rdesc.name);
1398 kfree(vreg);
1399 }
1400
1401 return 0;
1402}
1403
1404static struct of_device_id spmi_match_table[] = {
1405 { .compatible = QPNP_REGULATOR_DRIVER_NAME, },
1406 {}
1407};
1408
1409static const struct spmi_device_id qpnp_regulator_id[] = {
1410 { QPNP_REGULATOR_DRIVER_NAME, 0 },
1411 { }
1412};
1413MODULE_DEVICE_TABLE(spmi, qpnp_regulator_id);
1414
1415static struct spmi_driver qpnp_regulator_driver = {
1416 .driver = {
1417 .name = QPNP_REGULATOR_DRIVER_NAME,
1418 .of_match_table = spmi_match_table,
1419 .owner = THIS_MODULE,
1420 },
1421 .probe = qpnp_regulator_probe,
1422 .remove = __devexit_p(qpnp_regulator_remove),
1423 .id_table = qpnp_regulator_id,
1424};
1425
1426/*
1427 * Pre-compute the number of set points available for each regulator type to
1428 * avoid unnecessary calculations later in runtime.
1429 */
1430static void qpnp_regulator_set_point_init(void)
1431{
1432 struct qpnp_voltage_set_points **set_points;
1433 int i, j, temp;
1434
1435 set_points = all_set_points;
1436
1437 for (i = 0; i < ARRAY_SIZE(all_set_points); i++) {
1438 temp = 0;
1439 for (j = 0; j < all_set_points[i]->count; j++) {
1440 all_set_points[i]->range[j].n_voltages
1441 = (all_set_points[i]->range[j].max_uV
1442 - all_set_points[i]->range[j].set_point_min_uV)
1443 / all_set_points[i]->range[j].step_uV + 1;
1444 temp += all_set_points[i]->range[j].n_voltages;
1445 }
1446 all_set_points[i]->n_voltages = temp;
1447 }
1448}
1449
1450/**
1451 * qpnp_regulator_init() - register spmi driver for qpnp-regulator
1452 *
1453 * This initialization function should be called in systems in which driver
1454 * registration ordering must be controlled precisely.
1455 */
1456int __init qpnp_regulator_init(void)
1457{
1458 static bool has_registered;
1459
1460 if (has_registered)
1461 return 0;
1462 else
1463 has_registered = true;
1464
1465 qpnp_regulator_set_point_init();
1466
1467 return spmi_driver_register(&qpnp_regulator_driver);
1468}
1469EXPORT_SYMBOL(qpnp_regulator_init);
1470
1471static void __exit qpnp_regulator_exit(void)
1472{
1473 spmi_driver_unregister(&qpnp_regulator_driver);
1474}
1475
1476MODULE_DESCRIPTION("QPNP PMIC regulator driver");
1477MODULE_LICENSE("GPL v2");
1478
1479arch_initcall(qpnp_regulator_init);
1480module_exit(qpnp_regulator_exit);