blob: d859651802be2fba3ca3f96ea074b88284e98050 [file] [log] [blame]
David Collinsd1ac2f12012-02-14 13:34:18 -08001/*
David Collinsd1672c22013-03-12 15:21:47 -07002 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
David Collinsd1ac2f12012-02-14 13:34:18 -08003 *
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>
David Collins50f28e52013-04-05 13:44:50 -070022#include <linux/interrupt.h>
David Collinsd1ac2f12012-02-14 13:34:18 -080023#include <linux/bitops.h>
24#include <linux/slab.h>
25#include <linux/spmi.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/platform_device.h>
David Collins50f28e52013-04-05 13:44:50 -070029#include <linux/ktime.h>
David Collinsd1ac2f12012-02-14 13:34:18 -080030#include <linux/regulator/driver.h>
31#include <linux/regulator/of_regulator.h>
32#include <linux/regulator/qpnp-regulator.h>
33
David Collinsd1ac2f12012-02-14 13:34:18 -080034/* Debug Flag Definitions */
35enum {
36 QPNP_VREG_DEBUG_REQUEST = BIT(0), /* Show requests */
37 QPNP_VREG_DEBUG_DUPLICATE = BIT(1), /* Show duplicate requests */
38 QPNP_VREG_DEBUG_INIT = BIT(2), /* Show state after probe */
39 QPNP_VREG_DEBUG_WRITES = BIT(3), /* Show SPMI writes */
40 QPNP_VREG_DEBUG_READS = BIT(4), /* Show SPMI reads */
David Collins50f28e52013-04-05 13:44:50 -070041 QPNP_VREG_DEBUG_OCP = BIT(5), /* Show VS OCP IRQ events */
David Collinsd1ac2f12012-02-14 13:34:18 -080042};
43
44static int qpnp_vreg_debug_mask;
45module_param_named(
46 debug_mask, qpnp_vreg_debug_mask, int, S_IRUSR | S_IWUSR
47);
48
49#define vreg_err(vreg, fmt, ...) \
50 pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
51
52/* These types correspond to unique register layouts. */
53enum qpnp_regulator_logical_type {
54 QPNP_REGULATOR_LOGICAL_TYPE_SMPS,
55 QPNP_REGULATOR_LOGICAL_TYPE_LDO,
56 QPNP_REGULATOR_LOGICAL_TYPE_VS,
57 QPNP_REGULATOR_LOGICAL_TYPE_BOOST,
58 QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS,
59};
60
61enum qpnp_regulator_type {
David Collinsbad606a2012-08-21 10:57:36 -070062 QPNP_REGULATOR_TYPE_BUCK = 0x03,
David Collinsd1ac2f12012-02-14 13:34:18 -080063 QPNP_REGULATOR_TYPE_LDO = 0x04,
64 QPNP_REGULATOR_TYPE_VS = 0x05,
65 QPNP_REGULATOR_TYPE_BOOST = 0x1B,
66 QPNP_REGULATOR_TYPE_FTS = 0x1C,
67};
68
69enum qpnp_regulator_subtype {
70 QPNP_REGULATOR_SUBTYPE_GP_CTL = 0x08,
71 QPNP_REGULATOR_SUBTYPE_RF_CTL = 0x09,
72 QPNP_REGULATOR_SUBTYPE_N50 = 0x01,
73 QPNP_REGULATOR_SUBTYPE_N150 = 0x02,
74 QPNP_REGULATOR_SUBTYPE_N300 = 0x03,
75 QPNP_REGULATOR_SUBTYPE_N600 = 0x04,
76 QPNP_REGULATOR_SUBTYPE_N1200 = 0x05,
David Collins75ebd572012-08-29 14:08:10 -070077 QPNP_REGULATOR_SUBTYPE_N600_ST = 0x06,
78 QPNP_REGULATOR_SUBTYPE_N1200_ST = 0x07,
David Collinsd1ac2f12012-02-14 13:34:18 -080079 QPNP_REGULATOR_SUBTYPE_P50 = 0x08,
80 QPNP_REGULATOR_SUBTYPE_P150 = 0x09,
81 QPNP_REGULATOR_SUBTYPE_P300 = 0x0A,
82 QPNP_REGULATOR_SUBTYPE_P600 = 0x0B,
83 QPNP_REGULATOR_SUBTYPE_P1200 = 0x0C,
David Collins75ebd572012-08-29 14:08:10 -070084 QPNP_REGULATOR_SUBTYPE_LV_P50 = 0x28,
85 QPNP_REGULATOR_SUBTYPE_LV_P150 = 0x29,
86 QPNP_REGULATOR_SUBTYPE_LV_P300 = 0x2A,
87 QPNP_REGULATOR_SUBTYPE_LV_P600 = 0x2B,
88 QPNP_REGULATOR_SUBTYPE_LV_P1200 = 0x2C,
David Collinsd1ac2f12012-02-14 13:34:18 -080089 QPNP_REGULATOR_SUBTYPE_LV100 = 0x01,
90 QPNP_REGULATOR_SUBTYPE_LV300 = 0x02,
91 QPNP_REGULATOR_SUBTYPE_MV300 = 0x08,
92 QPNP_REGULATOR_SUBTYPE_MV500 = 0x09,
93 QPNP_REGULATOR_SUBTYPE_HDMI = 0x10,
94 QPNP_REGULATOR_SUBTYPE_OTG = 0x11,
95 QPNP_REGULATOR_SUBTYPE_5V_BOOST = 0x01,
96 QPNP_REGULATOR_SUBTYPE_FTS_CTL = 0x08,
97};
98
99enum qpnp_common_regulator_registers {
David Collinsbad606a2012-08-21 10:57:36 -0700100 QPNP_COMMON_REG_DIG_MAJOR_REV = 0x01,
David Collinsd1ac2f12012-02-14 13:34:18 -0800101 QPNP_COMMON_REG_TYPE = 0x04,
102 QPNP_COMMON_REG_SUBTYPE = 0x05,
103 QPNP_COMMON_REG_VOLTAGE_RANGE = 0x40,
104 QPNP_COMMON_REG_VOLTAGE_SET = 0x41,
105 QPNP_COMMON_REG_MODE = 0x45,
106 QPNP_COMMON_REG_ENABLE = 0x46,
107 QPNP_COMMON_REG_PULL_DOWN = 0x48,
108};
109
110enum qpnp_ldo_registers {
111 QPNP_LDO_REG_SOFT_START = 0x4C,
112};
113
114enum qpnp_vs_registers {
115 QPNP_VS_REG_OCP = 0x4A,
116 QPNP_VS_REG_SOFT_START = 0x4C,
117};
118
119enum qpnp_boost_registers {
David Collins6f846752012-08-17 17:59:28 -0700120 QPNP_BOOST_REG_CURRENT_LIMIT = 0x4A,
David Collinsd1ac2f12012-02-14 13:34:18 -0800121};
122
123/* Used for indexing into ctrl_reg. These are offets from 0x40 */
124enum qpnp_common_control_register_index {
125 QPNP_COMMON_IDX_VOLTAGE_RANGE = 0,
126 QPNP_COMMON_IDX_VOLTAGE_SET = 1,
127 QPNP_COMMON_IDX_MODE = 5,
128 QPNP_COMMON_IDX_ENABLE = 6,
129};
130
David Collinsd1ac2f12012-02-14 13:34:18 -0800131/* Common regulator control register layout */
132#define QPNP_COMMON_ENABLE_MASK 0x80
133#define QPNP_COMMON_ENABLE 0x80
134#define QPNP_COMMON_DISABLE 0x00
135#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
136#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
137#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
138#define QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
139#define QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0F
140
141/* Common regulator mode register layout */
142#define QPNP_COMMON_MODE_HPM_MASK 0x80
143#define QPNP_COMMON_MODE_AUTO_MASK 0x40
144#define QPNP_COMMON_MODE_BYPASS_MASK 0x20
145#define QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
146#define QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
147#define QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
148#define QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
149#define QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
150#define QPNP_COMMON_MODE_FOLLOW_ALL_MASK 0x1F
151
152/* Common regulator pull down control register layout */
153#define QPNP_COMMON_PULL_DOWN_ENABLE_MASK 0x80
154
155/* LDO regulator current limit control register layout */
156#define QPNP_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
157
158/* LDO regulator soft start control register layout */
159#define QPNP_LDO_SOFT_START_ENABLE_MASK 0x80
160
161/* VS regulator over current protection control register layout */
David Collins30dac622013-03-12 15:16:23 -0700162#define QPNP_VS_OCP_OVERRIDE 0x01
163#define QPNP_VS_OCP_NO_OVERRIDE 0x00
David Collinsd1ac2f12012-02-14 13:34:18 -0800164
165/* VS regulator soft start control register layout */
166#define QPNP_VS_SOFT_START_ENABLE_MASK 0x80
167#define QPNP_VS_SOFT_START_SEL_MASK 0x03
168
169/* Boost regulator current limit control register layout */
170#define QPNP_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
171#define QPNP_BOOST_CURRENT_LIMIT_MASK 0x07
172
David Collins50f28e52013-04-05 13:44:50 -0700173#define QPNP_VS_OCP_DEFAULT_MAX_RETRIES 10
174#define QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS 30
175#define QPNP_VS_OCP_FALL_DELAY_US 90
176#define QPNP_VS_OCP_FAULT_DELAY_US 20000
177
David Collinsbdd32812012-05-10 13:22:56 -0700178/*
179 * This voltage in uV is returned by get_voltage functions when there is no way
180 * to determine the current voltage level. It is needed because the regulator
181 * framework treats a 0 uV voltage as an error.
182 */
183#define VOLTAGE_UNKNOWN 1
184
David Collinsd1ac2f12012-02-14 13:34:18 -0800185struct qpnp_voltage_range {
186 int min_uV;
187 int max_uV;
188 int step_uV;
189 int set_point_min_uV;
190 unsigned n_voltages;
191 u8 range_sel;
192};
193
194struct qpnp_voltage_set_points {
195 struct qpnp_voltage_range *range;
196 int count;
197 unsigned n_voltages;
198};
199
200struct qpnp_regulator_mapping {
201 enum qpnp_regulator_type type;
202 enum qpnp_regulator_subtype subtype;
203 enum qpnp_regulator_logical_type logical_type;
David Collinsbad606a2012-08-21 10:57:36 -0700204 u32 revision_min;
205 u32 revision_max;
David Collinsd1ac2f12012-02-14 13:34:18 -0800206 struct regulator_ops *ops;
207 struct qpnp_voltage_set_points *set_points;
208 int hpm_min_load;
209};
210
211struct qpnp_regulator {
212 struct regulator_desc rdesc;
David Collins50f28e52013-04-05 13:44:50 -0700213 struct delayed_work ocp_work;
David Collinsd1ac2f12012-02-14 13:34:18 -0800214 struct spmi_device *spmi_dev;
215 struct regulator_dev *rdev;
216 struct qpnp_voltage_set_points *set_points;
217 enum qpnp_regulator_logical_type logical_type;
218 int enable_time;
David Collinsd1ac2f12012-02-14 13:34:18 -0800219 int ocp_enable;
David Collins50f28e52013-04-05 13:44:50 -0700220 int ocp_irq;
221 int ocp_count;
222 int ocp_max_retries;
223 int ocp_retry_delay_ms;
David Collinsd1ac2f12012-02-14 13:34:18 -0800224 int system_load;
225 int hpm_min_load;
226 u32 write_count;
227 u32 prev_write_count;
David Collins50f28e52013-04-05 13:44:50 -0700228 ktime_t vs_enable_time;
David Collinsd1ac2f12012-02-14 13:34:18 -0800229 u16 base_addr;
230 /* ctrl_reg provides a shadow copy of register values 0x40 to 0x47. */
231 u8 ctrl_reg[8];
232};
233
David Collinsbad606a2012-08-21 10:57:36 -0700234#define QPNP_VREG_MAP(_type, _subtype, _dig_major_min, _dig_major_max, \
235 _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
David Collinsd1ac2f12012-02-14 13:34:18 -0800236 { \
237 .type = QPNP_REGULATOR_TYPE_##_type, \
238 .subtype = QPNP_REGULATOR_SUBTYPE_##_subtype, \
David Collinsbad606a2012-08-21 10:57:36 -0700239 .revision_min = _dig_major_min, \
240 .revision_max = _dig_major_max, \
David Collinsd1ac2f12012-02-14 13:34:18 -0800241 .logical_type = QPNP_REGULATOR_LOGICAL_TYPE_##_logical_type, \
242 .ops = &qpnp_##_ops_val##_ops, \
243 .set_points = &_set_points_val##_set_points, \
244 .hpm_min_load = _hpm_min_load, \
245 }
246
247#define VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, _max_uV, \
248 _step_uV) \
249 { \
250 .min_uV = _min_uV, \
251 .set_point_min_uV = _set_point_min_uV, \
252 .max_uV = _max_uV, \
253 .step_uV = _step_uV, \
254 .range_sel = _range_sel, \
255 }
256
257#define SET_POINTS(_ranges) \
258{ \
259 .range = _ranges, \
260 .count = ARRAY_SIZE(_ranges), \
261};
262
263/*
264 * These tables contain the physically available PMIC regulator voltage setpoint
265 * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
266 * to ensure that the setpoints available to software are monotonically
267 * increasing and unique. The set_voltage callback functions expect these
268 * properties to hold.
269 */
270static struct qpnp_voltage_range pldo_ranges[] = {
David Collinsbdd32812012-05-10 13:22:56 -0700271 VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
David Collinsd1ac2f12012-02-14 13:34:18 -0800272 VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 25000),
273 VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 50000),
274};
275
David Collinsbdd32812012-05-10 13:22:56 -0700276static struct qpnp_voltage_range nldo1_ranges[] = {
277 VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
278};
279
280static struct qpnp_voltage_range nldo2_ranges[] = {
281 VOLTAGE_RANGE(1, 375000, 375000, 768750, 6250),
282 VOLTAGE_RANGE(2, 750000, 775000, 1537500, 12500),
David Collinsd1ac2f12012-02-14 13:34:18 -0800283};
284
David Collinsbad606a2012-08-21 10:57:36 -0700285static struct qpnp_voltage_range nldo3_ranges[] = {
286 VOLTAGE_RANGE(0, 375000, 375000, 1537500, 12500),
287};
288
David Collinsd1ac2f12012-02-14 13:34:18 -0800289static struct qpnp_voltage_range smps_ranges[] = {
290 VOLTAGE_RANGE(0, 375000, 375000, 1562500, 12500),
291 VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 25000),
292};
293
294static struct qpnp_voltage_range ftsmps_ranges[] = {
David Collins45b86ac2012-08-09 09:44:15 -0700295 VOLTAGE_RANGE(0, 0, 350000, 1275000, 5000),
296 VOLTAGE_RANGE(1, 0, 1280000, 2040000, 10000),
David Collinsd1ac2f12012-02-14 13:34:18 -0800297};
298
299static struct qpnp_voltage_range boost_ranges[] = {
300 VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 50000),
301};
302
303static struct qpnp_voltage_set_points pldo_set_points = SET_POINTS(pldo_ranges);
David Collinsbdd32812012-05-10 13:22:56 -0700304static struct qpnp_voltage_set_points nldo1_set_points
305 = SET_POINTS(nldo1_ranges);
306static struct qpnp_voltage_set_points nldo2_set_points
307 = SET_POINTS(nldo2_ranges);
David Collinsbad606a2012-08-21 10:57:36 -0700308static struct qpnp_voltage_set_points nldo3_set_points
309 = SET_POINTS(nldo3_ranges);
David Collinsd1ac2f12012-02-14 13:34:18 -0800310static struct qpnp_voltage_set_points smps_set_points = SET_POINTS(smps_ranges);
311static struct qpnp_voltage_set_points ftsmps_set_points
312 = SET_POINTS(ftsmps_ranges);
313static struct qpnp_voltage_set_points boost_set_points
314 = SET_POINTS(boost_ranges);
315static struct qpnp_voltage_set_points none_set_points;
316
317static struct qpnp_voltage_set_points *all_set_points[] = {
318 &pldo_set_points,
David Collinsbdd32812012-05-10 13:22:56 -0700319 &nldo1_set_points,
320 &nldo2_set_points,
David Collinsbad606a2012-08-21 10:57:36 -0700321 &nldo3_set_points,
David Collinsd1ac2f12012-02-14 13:34:18 -0800322 &smps_set_points,
323 &ftsmps_set_points,
324 &boost_set_points,
325};
326
327/* Determines which label to add to a debug print statement. */
328enum qpnp_regulator_action {
329 QPNP_REGULATOR_ACTION_INIT,
330 QPNP_REGULATOR_ACTION_ENABLE,
331 QPNP_REGULATOR_ACTION_DISABLE,
332 QPNP_REGULATOR_ACTION_VOLTAGE,
333 QPNP_REGULATOR_ACTION_MODE,
334};
335
336static void qpnp_vreg_show_state(struct regulator_dev *rdev,
337 enum qpnp_regulator_action action);
338
339#define DEBUG_PRINT_BUFFER_SIZE 64
340static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len)
341{
342 int pos = 0;
343 int i;
344
345 for (i = 0; i < buf_len; i++) {
346 pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]);
347 if (i < buf_len - 1)
348 pos += scnprintf(str + pos, str_len - pos, ", ");
349 }
350}
351
352static inline int qpnp_vreg_read(struct qpnp_regulator *vreg, u16 addr, u8 *buf,
353 int len)
354{
355 char str[DEBUG_PRINT_BUFFER_SIZE];
356 int rc = 0;
357
358 rc = spmi_ext_register_readl(vreg->spmi_dev->ctrl, vreg->spmi_dev->sid,
359 vreg->base_addr + addr, buf, len);
360
361 if (!rc && (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_READS)) {
362 str[0] = '\0';
363 fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
364 pr_info(" %-11s: read(0x%04X), sid=%d, len=%d; %s\n",
365 vreg->rdesc.name, vreg->base_addr + addr,
366 vreg->spmi_dev->sid, len, str);
367 }
368
369 return rc;
370}
371
372static inline int qpnp_vreg_write(struct qpnp_regulator *vreg, u16 addr,
373 u8 *buf, int len)
374{
375 char str[DEBUG_PRINT_BUFFER_SIZE];
376 int rc = 0;
377
378 if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_WRITES) {
379 str[0] = '\0';
380 fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
381 pr_info("%-11s: write(0x%04X), sid=%d, len=%d; %s\n",
382 vreg->rdesc.name, vreg->base_addr + addr,
383 vreg->spmi_dev->sid, len, str);
384 }
385
386 rc = spmi_ext_register_writel(vreg->spmi_dev->ctrl,
387 vreg->spmi_dev->sid, vreg->base_addr + addr, buf, len);
388 if (!rc)
389 vreg->write_count += len;
390
391 return rc;
392}
393
394/*
395 * qpnp_vreg_write_optimized - write the minimum sized contiguous subset of buf
396 * @vreg: qpnp_regulator pointer for this regulator
397 * @addr: local SPMI address offset from this peripheral's base address
398 * @buf: new data to write into the SPMI registers
399 * @buf_save: old data in the registers
400 * @len: number of bytes to write
401 *
402 * This function checks for unchanged register values between buf and buf_save
403 * starting at both ends of buf. Only the contiguous subset in the middle of
404 * buf starting and ending with new values is sent.
405 *
406 * Consider the following example:
407 * buf offset: 0 1 2 3 4 5 6 7
408 * reg state: U U C C U C U U
409 * (U = unchanged, C = changed)
410 * In this example registers 2 through 5 will be written with a single
411 * transaction.
412 */
413static inline int qpnp_vreg_write_optimized(struct qpnp_regulator *vreg,
414 u16 addr, u8 *buf, u8 *buf_save, int len)
415{
416 int i, rc, start, end;
417
418 for (i = 0; i < len; i++)
419 if (buf[i] != buf_save[i])
420 break;
421 start = i;
422
423 for (i = len - 1; i >= 0; i--)
424 if (buf[i] != buf_save[i])
425 break;
426 end = i;
427
428 if (start > end) {
429 /* No modified register values present. */
430 return 0;
431 }
432
433 rc = qpnp_vreg_write(vreg, addr + start, &buf[start], end - start + 1);
434 if (!rc)
435 for (i = start; i <= end; i++)
436 buf_save[i] = buf[i];
437
438 return rc;
439}
440
441/*
442 * Perform a masked write to a PMIC register only if the new value differs
443 * from the last value written to the register. This removes redundant
444 * register writing.
445 */
446static int qpnp_vreg_masked_write(struct qpnp_regulator *vreg, u16 addr, u8 val,
447 u8 mask, u8 *reg_save)
448{
449 int rc = 0;
450 u8 reg;
451
452 reg = (*reg_save & ~mask) | (val & mask);
453 if (reg != *reg_save) {
454 rc = qpnp_vreg_write(vreg, addr, &reg, 1);
455
456 if (rc) {
457 vreg_err(vreg, "write failed; addr=0x%03X, rc=%d\n",
458 addr, rc);
459 } else {
460 *reg_save = reg;
461 }
462 }
463
464 return rc;
465}
466
467/*
468 * Perform a masked read-modify-write to a PMIC register only if the new value
469 * differs from the value currently in the register. This removes redundant
470 * register writing.
471 */
472static int qpnp_vreg_masked_read_write(struct qpnp_regulator *vreg, u16 addr,
473 u8 val, u8 mask)
474{
475 int rc;
476 u8 reg;
477
478 rc = qpnp_vreg_read(vreg, addr, &reg, 1);
479 if (rc) {
480 vreg_err(vreg, "read failed; addr=0x%03X, rc=%d\n", addr, rc);
481 return rc;
482 }
483
484 return qpnp_vreg_masked_write(vreg, addr, val, mask, &reg);
485}
486
487static int qpnp_regulator_common_is_enabled(struct regulator_dev *rdev)
488{
489 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
490
491 return (vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]
492 & QPNP_COMMON_ENABLE_MASK)
493 == QPNP_COMMON_ENABLE;
494}
495
496static int qpnp_regulator_common_enable(struct regulator_dev *rdev)
497{
498 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
499 int rc;
500
501 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
502 QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
503 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
504
505 if (rc)
506 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
507 else
508 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_ENABLE);
509
510 return rc;
511}
512
David Collins50f28e52013-04-05 13:44:50 -0700513static int qpnp_regulator_vs_enable(struct regulator_dev *rdev)
514{
515 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
516
David Collinsde964302013-05-09 09:56:01 -0700517 if (vreg->ocp_irq) {
518 vreg->ocp_count = 0;
David Collins50f28e52013-04-05 13:44:50 -0700519 vreg->vs_enable_time = ktime_get();
David Collinsde964302013-05-09 09:56:01 -0700520 }
David Collins50f28e52013-04-05 13:44:50 -0700521
522 return qpnp_regulator_common_enable(rdev);
523}
524
David Collinsd1ac2f12012-02-14 13:34:18 -0800525static int qpnp_regulator_common_disable(struct regulator_dev *rdev)
526{
527 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
528 int rc;
529
530 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
531 QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
532 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
533
534 if (rc)
535 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
536 else
537 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_DISABLE);
538
539 return rc;
540}
541
542static int qpnp_regulator_select_voltage(struct qpnp_regulator *vreg,
David Collinsdf5d5e72012-11-13 17:16:19 -0800543 int min_uV, int max_uV, int *range_sel, int *voltage_sel,
544 unsigned *selector)
David Collinsd1ac2f12012-02-14 13:34:18 -0800545{
546 struct qpnp_voltage_range *range;
547 int uV = min_uV;
David Collinsdf5d5e72012-11-13 17:16:19 -0800548 int lim_min_uV, lim_max_uV, i, range_id;
David Collinsd1ac2f12012-02-14 13:34:18 -0800549
550 /* Check if request voltage is outside of physically settable range. */
551 lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
552 lim_max_uV =
553 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
554
555 if (uV < lim_min_uV && max_uV >= lim_min_uV)
556 uV = lim_min_uV;
557
558 if (uV < lim_min_uV || uV > lim_max_uV) {
559 vreg_err(vreg,
560 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
561 min_uV, max_uV, lim_min_uV, lim_max_uV);
562 return -EINVAL;
563 }
564
565 /* Find the range which uV is inside of. */
566 for (i = vreg->set_points->count - 1; i > 0; i--)
567 if (uV > vreg->set_points->range[i - 1].max_uV)
568 break;
David Collinsdf5d5e72012-11-13 17:16:19 -0800569 range_id = i;
570 range = &vreg->set_points->range[range_id];
David Collinsd1ac2f12012-02-14 13:34:18 -0800571 *range_sel = range->range_sel;
572
573 /*
574 * Force uV to be an allowed set point by applying a ceiling function to
575 * the uV value.
576 */
577 *voltage_sel = (uV - range->min_uV + range->step_uV - 1)
578 / range->step_uV;
579 uV = *voltage_sel * range->step_uV + range->min_uV;
580
581 if (uV > max_uV) {
582 vreg_err(vreg,
583 "request v=[%d, %d] cannot be met by any set point; "
584 "next set point: %d\n",
585 min_uV, max_uV, uV);
586 return -EINVAL;
587 }
588
David Collinsdf5d5e72012-11-13 17:16:19 -0800589 *selector = 0;
590 for (i = 0; i < range_id; i++)
591 *selector += vreg->set_points->range[i].n_voltages;
592 *selector += (uV - range->set_point_min_uV) / range->step_uV;
593
David Collinsd1ac2f12012-02-14 13:34:18 -0800594 return 0;
595}
596
597static int qpnp_regulator_common_set_voltage(struct regulator_dev *rdev,
598 int min_uV, int max_uV, unsigned *selector)
599{
600 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
601 int rc, range_sel, voltage_sel;
602 u8 buf[2];
603
604 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
David Collinsdf5d5e72012-11-13 17:16:19 -0800605 &voltage_sel, selector);
David Collinsd1ac2f12012-02-14 13:34:18 -0800606 if (rc) {
607 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
608 return rc;
609 }
610
611 buf[0] = range_sel;
612 buf[1] = voltage_sel;
613 if ((vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] != range_sel)
614 && (vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] == voltage_sel)) {
615 /* Handle latched range change. */
616 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
617 buf, 2);
618 if (!rc) {
619 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = buf[0];
620 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] = buf[1];
621 }
622 } else {
623 /* Either write can be optimized away safely. */
624 rc = qpnp_vreg_write_optimized(vreg,
625 QPNP_COMMON_REG_VOLTAGE_RANGE, buf,
626 &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE], 2);
627 }
628
629 if (rc)
630 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
631 else
632 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
633
634 return rc;
635}
636
637static int qpnp_regulator_common_get_voltage(struct regulator_dev *rdev)
638{
639 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
640 struct qpnp_voltage_range *range = NULL;
641 int range_sel, voltage_sel, i;
642
643 range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
644 voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
645
646 for (i = 0; i < vreg->set_points->count; i++) {
647 if (vreg->set_points->range[i].range_sel == range_sel) {
648 range = &vreg->set_points->range[i];
649 break;
650 }
651 }
652
653 if (!range) {
654 vreg_err(vreg, "voltage unknown, range %d is invalid\n",
655 range_sel);
David Collinsbdd32812012-05-10 13:22:56 -0700656 return VOLTAGE_UNKNOWN;
David Collinsd1ac2f12012-02-14 13:34:18 -0800657 }
658
659 return range->step_uV * voltage_sel + range->min_uV;
660}
661
662static int qpnp_regulator_boost_set_voltage(struct regulator_dev *rdev,
663 int min_uV, int max_uV, unsigned *selector)
664{
665 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
666 int rc, range_sel, voltage_sel;
667
668 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
David Collinsdf5d5e72012-11-13 17:16:19 -0800669 &voltage_sel, selector);
David Collinsd1ac2f12012-02-14 13:34:18 -0800670 if (rc) {
671 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
672 return rc;
673 }
674
675 /*
676 * Boost type regulators do not have range select register so only
677 * voltage set register needs to be written.
678 */
679 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET,
680 voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]);
681
682 if (rc)
683 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
684 else
685 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
686
687 return rc;
688}
689
690static int qpnp_regulator_boost_get_voltage(struct regulator_dev *rdev)
691{
692 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
693 int voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
694
695 return boost_ranges[0].step_uV * voltage_sel + boost_ranges[0].min_uV;
696}
697
698static int qpnp_regulator_common_list_voltage(struct regulator_dev *rdev,
699 unsigned selector)
700{
701 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
702 int uV = 0;
703 int i;
704
705 if (selector >= vreg->set_points->n_voltages)
706 return 0;
707
708 for (i = 0; i < vreg->set_points->count; i++) {
709 if (selector < vreg->set_points->range[i].n_voltages) {
710 uV = selector * vreg->set_points->range[i].step_uV
711 + vreg->set_points->range[i].set_point_min_uV;
712 break;
713 } else {
714 selector -= vreg->set_points->range[i].n_voltages;
715 }
716 }
717
718 return uV;
719}
720
721static unsigned int qpnp_regulator_common_get_mode(struct regulator_dev *rdev)
722{
723 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
724
725 return (vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]
726 & QPNP_COMMON_MODE_HPM_MASK)
727 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
728}
729
730static int qpnp_regulator_common_set_mode(struct regulator_dev *rdev,
731 unsigned int mode)
732{
733 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
734 int rc = 0;
735 u8 val;
736
737 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
738 vreg_err(vreg, "invalid mode: %u\n", mode);
739 return -EINVAL;
740 }
741
742 val = (mode == REGULATOR_MODE_NORMAL ? QPNP_COMMON_MODE_HPM_MASK : 0);
743
744 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_MODE, val,
745 QPNP_COMMON_MODE_HPM_MASK,
746 &vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]);
747
748 if (rc)
749 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
750 else
751 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE);
752
753 return rc;
754}
755
756static unsigned int qpnp_regulator_common_get_optimum_mode(
757 struct regulator_dev *rdev, int input_uV, int output_uV,
758 int load_uA)
759{
760 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
761 unsigned int mode;
762
763 if (load_uA + vreg->system_load >= vreg->hpm_min_load)
764 mode = REGULATOR_MODE_NORMAL;
765 else
766 mode = REGULATOR_MODE_IDLE;
767
768 return mode;
769}
770
771static int qpnp_regulator_common_enable_time(struct regulator_dev *rdev)
772{
773 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
774
775 return vreg->enable_time;
776}
777
David Collins50f28e52013-04-05 13:44:50 -0700778static int qpnp_regulator_vs_clear_ocp(struct qpnp_regulator *vreg)
779{
780 int rc;
781
782 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
783 QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
784 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
785 if (rc)
786 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
787
788 vreg->vs_enable_time = ktime_get();
789
790 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
791 QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
792 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
793 if (rc)
794 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
795
796 if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
797 pr_info("%s: switch state toggled after OCP event\n",
798 vreg->rdesc.name);
799 }
800
801 return rc;
802}
803
804static void qpnp_regulator_vs_ocp_work(struct work_struct *work)
805{
806 struct delayed_work *dwork
807 = container_of(work, struct delayed_work, work);
808 struct qpnp_regulator *vreg
809 = container_of(dwork, struct qpnp_regulator, ocp_work);
810
811 qpnp_regulator_vs_clear_ocp(vreg);
812
813 return;
814}
815
816static irqreturn_t qpnp_regulator_vs_ocp_isr(int irq, void *data)
817{
818 struct qpnp_regulator *vreg = data;
819 ktime_t ocp_irq_time;
820 s64 ocp_trigger_delay_us;
821
822 ocp_irq_time = ktime_get();
823 ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
824 vreg->vs_enable_time);
825
826 /*
827 * Reset the OCP count if there is a large delay between switch enable
828 * and when OCP triggers. This is indicative of a hotplug event as
829 * opposed to a fault.
830 */
831 if (ocp_trigger_delay_us > QPNP_VS_OCP_FAULT_DELAY_US)
832 vreg->ocp_count = 0;
833
834 /* Wait for switch output to settle back to 0 V after OCP triggered. */
835 udelay(QPNP_VS_OCP_FALL_DELAY_US);
836
837 vreg->ocp_count++;
838
839 if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
840 pr_info("%s: VS OCP triggered, count = %d, delay = %lld us\n",
841 vreg->rdesc.name, vreg->ocp_count,
842 ocp_trigger_delay_us);
843 }
844
845 if (vreg->ocp_count == 1) {
846 /* Immediately clear the over current condition. */
847 qpnp_regulator_vs_clear_ocp(vreg);
848 } else if (vreg->ocp_count <= vreg->ocp_max_retries) {
849 /* Schedule the over current clear task to run later. */
850 schedule_delayed_work(&vreg->ocp_work,
851 msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
852 } else {
853 vreg_err(vreg, "OCP triggered %d times; no further retries\n",
854 vreg->ocp_count);
855 }
856
857 return IRQ_HANDLED;
858}
859
David Collinsd1ac2f12012-02-14 13:34:18 -0800860static const char const *qpnp_print_actions[] = {
861 [QPNP_REGULATOR_ACTION_INIT] = "initial ",
862 [QPNP_REGULATOR_ACTION_ENABLE] = "enable ",
863 [QPNP_REGULATOR_ACTION_DISABLE] = "disable ",
864 [QPNP_REGULATOR_ACTION_VOLTAGE] = "set voltage",
865 [QPNP_REGULATOR_ACTION_MODE] = "set mode ",
866};
867
868static void qpnp_vreg_show_state(struct regulator_dev *rdev,
869 enum qpnp_regulator_action action)
870{
871 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
872 const char *action_label = qpnp_print_actions[action];
873 unsigned int mode = 0;
874 int uV = 0;
875 const char *mode_label = "";
876 enum qpnp_regulator_logical_type type;
877 const char *enable_label;
878 char pc_enable_label[5] = {'\0'};
879 char pc_mode_label[8] = {'\0'};
880 bool show_req, show_dupe, show_init, has_changed;
881 u8 en_reg, mode_reg;
882
883 /* Do not print unless appropriate flags are set. */
884 show_req = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_REQUEST;
885 show_dupe = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_DUPLICATE;
886 show_init = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT;
887 has_changed = vreg->write_count != vreg->prev_write_count;
888 if (!((show_init && action == QPNP_REGULATOR_ACTION_INIT)
889 || (show_req && (has_changed || show_dupe)))) {
890 return;
891 }
892
893 vreg->prev_write_count = vreg->write_count;
894
895 type = vreg->logical_type;
896
897 enable_label = qpnp_regulator_common_is_enabled(rdev) ? "on " : "off";
898
899 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
900 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
901 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
902 uV = qpnp_regulator_common_get_voltage(rdev);
903
904 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST)
905 uV = qpnp_regulator_boost_get_voltage(rdev);
906
907 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
908 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
David Collinsd1672c22013-03-12 15:21:47 -0700909 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
910 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
David Collinsd1ac2f12012-02-14 13:34:18 -0800911 mode = qpnp_regulator_common_get_mode(rdev);
912 mode_label = mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM";
913 }
914
915 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
916 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
917 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
918 en_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE];
919 pc_enable_label[0] =
920 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK ? '3' : '_';
921 pc_enable_label[1] =
922 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK ? '2' : '_';
923 pc_enable_label[2] =
924 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK ? '1' : '_';
925 pc_enable_label[3] =
926 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK ? '0' : '_';
927 }
928
929 switch (type) {
930 case QPNP_REGULATOR_LOGICAL_TYPE_SMPS:
931 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
932 pc_mode_label[0] =
933 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
934 pc_mode_label[1] =
935 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
936 pc_mode_label[2] =
937 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
938 pc_mode_label[3] =
939 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
940 pc_mode_label[4] =
941 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
942 pc_mode_label[5] =
943 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
944
945 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
946 "alt_mode=%s\n",
947 action_label, vreg->rdesc.name, enable_label, uV,
948 mode_label, pc_enable_label, pc_mode_label);
949 break;
950 case QPNP_REGULATOR_LOGICAL_TYPE_LDO:
951 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
952 pc_mode_label[0] =
953 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
954 pc_mode_label[1] =
955 mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
956 pc_mode_label[2] =
957 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
958 pc_mode_label[3] =
959 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
960 pc_mode_label[4] =
961 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
962 pc_mode_label[5] =
963 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
964 pc_mode_label[6] =
965 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
966
967 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
968 "alt_mode=%s\n",
969 action_label, vreg->rdesc.name, enable_label, uV,
970 mode_label, pc_enable_label, pc_mode_label);
971 break;
972 case QPNP_REGULATOR_LOGICAL_TYPE_VS:
973 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
974 pc_mode_label[0] =
975 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
976 pc_mode_label[1] =
977 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
978
David Collinsd1672c22013-03-12 15:21:47 -0700979 pr_info("%s %-11s: %s, mode=%s, pc_en=%s, alt_mode=%s\n",
David Collinsd1ac2f12012-02-14 13:34:18 -0800980 action_label, vreg->rdesc.name, enable_label,
David Collinsd1672c22013-03-12 15:21:47 -0700981 mode_label, pc_enable_label, pc_mode_label);
David Collinsd1ac2f12012-02-14 13:34:18 -0800982 break;
983 case QPNP_REGULATOR_LOGICAL_TYPE_BOOST:
984 pr_info("%s %-11s: %s, v=%7d uV\n",
985 action_label, vreg->rdesc.name, enable_label, uV);
986 break;
987 case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS:
988 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
989 pc_mode_label[0] =
990 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
991
992 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
993 action_label, vreg->rdesc.name, enable_label, uV,
994 mode_label, pc_mode_label);
995 break;
996 default:
997 break;
998 }
999}
1000
1001static struct regulator_ops qpnp_smps_ops = {
1002 .enable = qpnp_regulator_common_enable,
1003 .disable = qpnp_regulator_common_disable,
1004 .is_enabled = qpnp_regulator_common_is_enabled,
1005 .set_voltage = qpnp_regulator_common_set_voltage,
1006 .get_voltage = qpnp_regulator_common_get_voltage,
1007 .list_voltage = qpnp_regulator_common_list_voltage,
1008 .set_mode = qpnp_regulator_common_set_mode,
1009 .get_mode = qpnp_regulator_common_get_mode,
1010 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
1011 .enable_time = qpnp_regulator_common_enable_time,
1012};
1013
1014static struct regulator_ops qpnp_ldo_ops = {
1015 .enable = qpnp_regulator_common_enable,
1016 .disable = qpnp_regulator_common_disable,
1017 .is_enabled = qpnp_regulator_common_is_enabled,
1018 .set_voltage = qpnp_regulator_common_set_voltage,
1019 .get_voltage = qpnp_regulator_common_get_voltage,
1020 .list_voltage = qpnp_regulator_common_list_voltage,
1021 .set_mode = qpnp_regulator_common_set_mode,
1022 .get_mode = qpnp_regulator_common_get_mode,
1023 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
1024 .enable_time = qpnp_regulator_common_enable_time,
1025};
1026
1027static struct regulator_ops qpnp_vs_ops = {
David Collins50f28e52013-04-05 13:44:50 -07001028 .enable = qpnp_regulator_vs_enable,
David Collinsd1ac2f12012-02-14 13:34:18 -08001029 .disable = qpnp_regulator_common_disable,
1030 .is_enabled = qpnp_regulator_common_is_enabled,
1031 .enable_time = qpnp_regulator_common_enable_time,
1032};
1033
1034static struct regulator_ops qpnp_boost_ops = {
1035 .enable = qpnp_regulator_common_enable,
1036 .disable = qpnp_regulator_common_disable,
1037 .is_enabled = qpnp_regulator_common_is_enabled,
1038 .set_voltage = qpnp_regulator_boost_set_voltage,
1039 .get_voltage = qpnp_regulator_boost_get_voltage,
1040 .list_voltage = qpnp_regulator_common_list_voltage,
1041 .enable_time = qpnp_regulator_common_enable_time,
1042};
1043
1044static struct regulator_ops qpnp_ftsmps_ops = {
1045 .enable = qpnp_regulator_common_enable,
1046 .disable = qpnp_regulator_common_disable,
1047 .is_enabled = qpnp_regulator_common_is_enabled,
1048 .set_voltage = qpnp_regulator_common_set_voltage,
1049 .get_voltage = qpnp_regulator_common_get_voltage,
1050 .list_voltage = qpnp_regulator_common_list_voltage,
1051 .set_mode = qpnp_regulator_common_set_mode,
1052 .get_mode = qpnp_regulator_common_get_mode,
1053 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
1054 .enable_time = qpnp_regulator_common_enable_time,
1055};
1056
David Collinsbad606a2012-08-21 10:57:36 -07001057/* Maximum possible digital major revision value */
1058#define INF 0xFF
1059
David Collinsd1ac2f12012-02-14 13:34:18 -08001060static const struct qpnp_regulator_mapping supported_regulators[] = {
David Collinsbad606a2012-08-21 10:57:36 -07001061 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
1062 QPNP_VREG_MAP(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
1063 QPNP_VREG_MAP(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
1064 QPNP_VREG_MAP(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
1065 QPNP_VREG_MAP(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
1066 QPNP_VREG_MAP(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
1067 QPNP_VREG_MAP(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
David Collins75ebd572012-08-29 14:08:10 -07001068 QPNP_VREG_MAP(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000),
1069 QPNP_VREG_MAP(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000),
1070 QPNP_VREG_MAP(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000),
1071 QPNP_VREG_MAP(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000),
David Collinsbad606a2012-08-21 10:57:36 -07001072 QPNP_VREG_MAP(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
1073 QPNP_VREG_MAP(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
1074 QPNP_VREG_MAP(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
1075 QPNP_VREG_MAP(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
1076 QPNP_VREG_MAP(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
David Collins75ebd572012-08-29 14:08:10 -07001077 QPNP_VREG_MAP(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000),
1078 QPNP_VREG_MAP(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000),
1079 QPNP_VREG_MAP(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000),
1080 QPNP_VREG_MAP(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000),
1081 QPNP_VREG_MAP(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000),
David Collinsbad606a2012-08-21 10:57:36 -07001082 QPNP_VREG_MAP(VS, LV100, 0, INF, VS, vs, none, 0),
1083 QPNP_VREG_MAP(VS, LV300, 0, INF, VS, vs, none, 0),
1084 QPNP_VREG_MAP(VS, MV300, 0, INF, VS, vs, none, 0),
1085 QPNP_VREG_MAP(VS, MV500, 0, INF, VS, vs, none, 0),
1086 QPNP_VREG_MAP(VS, HDMI, 0, INF, VS, vs, none, 0),
1087 QPNP_VREG_MAP(VS, OTG, 0, INF, VS, vs, none, 0),
1088 QPNP_VREG_MAP(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
1089 QPNP_VREG_MAP(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
David Collinsd1ac2f12012-02-14 13:34:18 -08001090};
1091
1092static int qpnp_regulator_match(struct qpnp_regulator *vreg)
1093{
1094 const struct qpnp_regulator_mapping *mapping;
Michael Bohan9328e492012-08-09 11:37:36 -07001095 struct device_node *node = vreg->spmi_dev->dev.of_node;
David Collinsd1ac2f12012-02-14 13:34:18 -08001096 int rc, i;
David Collinsbad606a2012-08-21 10:57:36 -07001097 u32 type_reg[2], dig_major_rev;
1098 u8 version[QPNP_COMMON_REG_SUBTYPE - QPNP_COMMON_REG_DIG_MAJOR_REV + 1];
1099 u8 type, subtype;
David Collinsd1ac2f12012-02-14 13:34:18 -08001100
David Collinsbad606a2012-08-21 10:57:36 -07001101 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_DIG_MAJOR_REV, version,
1102 ARRAY_SIZE(version));
1103 if (rc) {
1104 vreg_err(vreg, "could not read version registers, rc=%d\n", rc);
1105 return rc;
1106 }
1107 dig_major_rev = version[QPNP_COMMON_REG_DIG_MAJOR_REV
1108 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1109 type = version[QPNP_COMMON_REG_TYPE
1110 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1111 subtype = version[QPNP_COMMON_REG_SUBTYPE
1112 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1113
1114 /*
1115 * Override type and subtype register values if qcom,force-type is
1116 * present in the device tree node.
1117 */
1118 rc = of_property_read_u32_array(node, "qcom,force-type", type_reg, 2);
Michael Bohan9328e492012-08-09 11:37:36 -07001119 if (!rc) {
1120 type = type_reg[0];
1121 subtype = type_reg[1];
David Collinsd1ac2f12012-02-14 13:34:18 -08001122 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001123
1124 rc = -ENODEV;
1125 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1126 mapping = &supported_regulators[i];
David Collinsbad606a2012-08-21 10:57:36 -07001127 if (mapping->type == type && mapping->subtype == subtype
1128 && mapping->revision_min <= dig_major_rev
1129 && mapping->revision_max >= dig_major_rev) {
David Collinsd1ac2f12012-02-14 13:34:18 -08001130 vreg->logical_type = mapping->logical_type;
1131 vreg->set_points = mapping->set_points;
1132 vreg->hpm_min_load = mapping->hpm_min_load;
1133 vreg->rdesc.ops = mapping->ops;
1134 vreg->rdesc.n_voltages
1135 = mapping->set_points->n_voltages;
1136 rc = 0;
1137 break;
1138 }
1139 }
1140
1141 return rc;
1142}
1143
1144static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg,
1145 struct qpnp_regulator_platform_data *pdata)
1146{
1147 int rc, i;
1148 enum qpnp_regulator_logical_type type;
1149 u8 ctrl_reg[8], reg, mask;
1150
1151 type = vreg->logical_type;
1152
1153 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1154 vreg->ctrl_reg, 8);
1155 if (rc) {
1156 vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
1157 return rc;
1158 }
1159
1160 for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++)
1161 ctrl_reg[i] = vreg->ctrl_reg[i];
1162
1163 /* Set up enable pin control. */
1164 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1165 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1166 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1167 && !(pdata->pin_ctrl_enable
1168 & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1169 ctrl_reg[QPNP_COMMON_IDX_ENABLE] &=
1170 ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1171 ctrl_reg[QPNP_COMMON_IDX_ENABLE] |=
1172 pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1173 }
1174
David Collinsd1672c22013-03-12 15:21:47 -07001175 /* Set up HPM control. */
1176 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1177 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1178 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1179 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
1180 && (pdata->hpm_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
1181 ctrl_reg[QPNP_COMMON_IDX_MODE] &= ~QPNP_COMMON_MODE_HPM_MASK;
1182 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1183 (pdata->hpm_enable ? QPNP_COMMON_MODE_HPM_MASK : 0);
1184 }
1185
David Collinsd1ac2f12012-02-14 13:34:18 -08001186 /* Set up auto mode control. */
1187 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1188 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1189 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1190 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
1191 && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
1192 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1193 ~QPNP_COMMON_MODE_AUTO_MASK;
1194 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1195 (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0);
1196 }
1197
1198 /* Set up mode pin control. */
1199 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1200 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO)
1201 && !(pdata->pin_ctrl_hpm
1202 & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1203 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1204 ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1205 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1206 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1207 }
1208
1209 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1210 && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1211 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1212 ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1213 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1214 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1215 }
1216
1217 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1218 && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1219 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1220 ~QPNP_COMMON_MODE_BYPASS_MASK;
1221 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1222 (pdata->bypass_mode_enable
1223 ? QPNP_COMMON_MODE_BYPASS_MASK : 0);
1224 }
1225
1226 /* Set boost current limit. */
1227 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
1228 && pdata->boost_current_limit
1229 != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) {
David Collins6f846752012-08-17 17:59:28 -07001230 reg = pdata->boost_current_limit;
1231 mask = QPNP_BOOST_CURRENT_LIMIT_MASK;
1232 rc = qpnp_vreg_masked_read_write(vreg,
1233 QPNP_BOOST_REG_CURRENT_LIMIT, reg, mask);
1234 if (rc) {
1235 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1236 return rc;
1237 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001238 }
1239
1240 /* Write back any control register values that were modified. */
1241 rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1242 ctrl_reg, vreg->ctrl_reg, 8);
1243 if (rc) {
1244 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1245 return rc;
1246 }
1247
1248 /* Set pull down. */
1249 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1250 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1251 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1252 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1253 reg = pdata->pull_down_enable
1254 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1255 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_PULL_DOWN, &reg, 1);
1256 if (rc) {
1257 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1258 return rc;
1259 }
1260 }
1261
1262 if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
1263 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1264 /* FTSMPS has other bits in the pull down control register. */
1265 reg = pdata->pull_down_enable
1266 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1267 rc = qpnp_vreg_masked_read_write(vreg,
1268 QPNP_COMMON_REG_PULL_DOWN, reg,
1269 QPNP_COMMON_PULL_DOWN_ENABLE_MASK);
1270 if (rc) {
1271 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1272 return rc;
1273 }
1274 }
1275
1276 /* Set soft start for LDO. */
1277 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1278 && pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1279 reg = pdata->soft_start_enable
1280 ? QPNP_LDO_SOFT_START_ENABLE_MASK : 0;
1281 rc = qpnp_vreg_write(vreg, QPNP_LDO_REG_SOFT_START, &reg, 1);
1282 if (rc) {
1283 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1284 return rc;
1285 }
1286 }
1287
1288 /* Set soft start strength and over current protection for VS. */
1289 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
1290 reg = 0;
1291 mask = 0;
1292 if (pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1293 reg |= pdata->soft_start_enable
1294 ? QPNP_VS_SOFT_START_ENABLE_MASK : 0;
1295 mask |= QPNP_VS_SOFT_START_ENABLE_MASK;
1296 }
1297 if (pdata->vs_soft_start_strength
1298 != QPNP_VS_SOFT_START_STR_HW_DEFAULT) {
1299 reg |= pdata->vs_soft_start_strength
1300 & QPNP_VS_SOFT_START_SEL_MASK;
1301 mask |= QPNP_VS_SOFT_START_SEL_MASK;
1302 }
1303 rc = qpnp_vreg_masked_read_write(vreg, QPNP_VS_REG_SOFT_START,
1304 reg, mask);
1305 if (rc) {
1306 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1307 return rc;
1308 }
1309
1310 if (pdata->ocp_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
David Collins30dac622013-03-12 15:16:23 -07001311 reg = pdata->ocp_enable ? QPNP_VS_OCP_NO_OVERRIDE
1312 : QPNP_VS_OCP_OVERRIDE;
David Collinsd1ac2f12012-02-14 13:34:18 -08001313 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
1314 if (rc) {
1315 vreg_err(vreg, "spmi write failed, rc=%d\n",
1316 rc);
1317 return rc;
1318 }
1319 }
1320 }
1321
1322 return rc;
1323}
1324
1325/* Fill in pdata elements based on values found in device tree. */
1326static int qpnp_regulator_get_dt_config(struct spmi_device *spmi,
1327 struct qpnp_regulator_platform_data *pdata)
1328{
1329 struct resource *res;
1330 struct device_node *node = spmi->dev.of_node;
1331 int rc = 0;
1332
1333 pdata->init_data.constraints.input_uV
1334 = pdata->init_data.constraints.max_uV;
1335
Michael Bohan0e5534d2012-05-22 17:33:45 -07001336 res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
David Collinsd1ac2f12012-02-14 13:34:18 -08001337 if (!res) {
1338 dev_err(&spmi->dev, "%s: node is missing base address\n",
1339 __func__);
1340 return -EINVAL;
1341 }
1342 pdata->base_addr = res->start;
1343
David Collins50f28e52013-04-05 13:44:50 -07001344 /* OCP IRQ is optional so ignore get errors. */
1345 pdata->ocp_irq = spmi_get_irq_byname(spmi, NULL, "ocp");
1346 if (pdata->ocp_irq < 0)
1347 pdata->ocp_irq = 0;
1348
David Collinsd1ac2f12012-02-14 13:34:18 -08001349 /*
1350 * Initialize configuration parameters to use hardware default in case
1351 * no value is specified via device tree.
1352 */
1353 pdata->auto_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1354 pdata->bypass_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1355 pdata->ocp_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1356 pdata->pull_down_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1357 pdata->soft_start_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1358 pdata->boost_current_limit = QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT;
1359 pdata->pin_ctrl_enable = QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1360 pdata->pin_ctrl_hpm = QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1361 pdata->vs_soft_start_strength = QPNP_VS_SOFT_START_STR_HW_DEFAULT;
David Collinsd1672c22013-03-12 15:21:47 -07001362 pdata->hpm_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
David Collinsd1ac2f12012-02-14 13:34:18 -08001363
1364 /* These bindings are optional, so it is okay if they are not found. */
1365 of_property_read_u32(node, "qcom,auto-mode-enable",
1366 &pdata->auto_mode_enable);
1367 of_property_read_u32(node, "qcom,bypass-mode-enable",
1368 &pdata->bypass_mode_enable);
1369 of_property_read_u32(node, "qcom,ocp-enable", &pdata->ocp_enable);
David Collins50f28e52013-04-05 13:44:50 -07001370 of_property_read_u32(node, "qcom,ocp-max-retries",
1371 &pdata->ocp_max_retries);
1372 of_property_read_u32(node, "qcom,ocp-retry-delay",
1373 &pdata->ocp_retry_delay_ms);
David Collinsd1ac2f12012-02-14 13:34:18 -08001374 of_property_read_u32(node, "qcom,pull-down-enable",
1375 &pdata->pull_down_enable);
1376 of_property_read_u32(node, "qcom,soft-start-enable",
1377 &pdata->soft_start_enable);
1378 of_property_read_u32(node, "qcom,boost-current-limit",
1379 &pdata->boost_current_limit);
1380 of_property_read_u32(node, "qcom,pin-ctrl-enable",
1381 &pdata->pin_ctrl_enable);
1382 of_property_read_u32(node, "qcom,pin-ctrl-hpm", &pdata->pin_ctrl_hpm);
David Collinsd1672c22013-03-12 15:21:47 -07001383 of_property_read_u32(node, "qcom,hpm-enable", &pdata->hpm_enable);
David Collinsd1ac2f12012-02-14 13:34:18 -08001384 of_property_read_u32(node, "qcom,vs-soft-start-strength",
1385 &pdata->vs_soft_start_strength);
1386 of_property_read_u32(node, "qcom,system-load", &pdata->system_load);
1387 of_property_read_u32(node, "qcom,enable-time", &pdata->enable_time);
David Collinsd1ac2f12012-02-14 13:34:18 -08001388
1389 return rc;
1390}
1391
1392static struct of_device_id spmi_match_table[];
1393
1394#define MAX_NAME_LEN 127
1395
1396static int __devinit qpnp_regulator_probe(struct spmi_device *spmi)
1397{
1398 struct qpnp_regulator_platform_data *pdata;
1399 struct qpnp_regulator *vreg;
1400 struct regulator_desc *rdesc;
1401 struct qpnp_regulator_platform_data of_pdata;
1402 struct regulator_init_data *init_data;
1403 char *reg_name;
1404 int rc;
1405 bool is_dt;
1406
1407 vreg = kzalloc(sizeof(struct qpnp_regulator), GFP_KERNEL);
1408 if (!vreg) {
1409 dev_err(&spmi->dev, "%s: Can't allocate qpnp_regulator\n",
1410 __func__);
1411 return -ENOMEM;
1412 }
1413
1414 is_dt = of_match_device(spmi_match_table, &spmi->dev);
1415
1416 /* Check if device tree is in use. */
1417 if (is_dt) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001418 init_data = of_get_regulator_init_data(&spmi->dev,
1419 spmi->dev.of_node);
David Collinsd1ac2f12012-02-14 13:34:18 -08001420 if (!init_data) {
1421 dev_err(&spmi->dev, "%s: unable to allocate memory\n",
1422 __func__);
1423 kfree(vreg);
1424 return -ENOMEM;
1425 }
1426 memset(&of_pdata, 0,
1427 sizeof(struct qpnp_regulator_platform_data));
1428 memcpy(&of_pdata.init_data, init_data,
1429 sizeof(struct regulator_init_data));
1430
1431 if (of_get_property(spmi->dev.of_node, "parent-supply", NULL))
1432 of_pdata.init_data.supply_regulator = "parent";
1433
1434 rc = qpnp_regulator_get_dt_config(spmi, &of_pdata);
1435 if (rc) {
1436 dev_err(&spmi->dev, "%s: DT parsing failed, rc=%d\n",
1437 __func__, rc);
1438 kfree(vreg);
1439 return -ENOMEM;
1440 }
1441
1442 pdata = &of_pdata;
1443 } else {
1444 pdata = spmi->dev.platform_data;
1445 }
1446
1447 if (pdata == NULL) {
1448 dev_err(&spmi->dev, "%s: no platform data specified\n",
1449 __func__);
1450 kfree(vreg);
1451 return -EINVAL;
1452 }
1453
1454 vreg->spmi_dev = spmi;
1455 vreg->prev_write_count = -1;
1456 vreg->write_count = 0;
1457 vreg->base_addr = pdata->base_addr;
1458 vreg->enable_time = pdata->enable_time;
1459 vreg->system_load = pdata->system_load;
1460 vreg->ocp_enable = pdata->ocp_enable;
David Collins50f28e52013-04-05 13:44:50 -07001461 vreg->ocp_irq = pdata->ocp_irq;
1462 vreg->ocp_max_retries = pdata->ocp_max_retries;
1463 vreg->ocp_retry_delay_ms = pdata->ocp_retry_delay_ms;
1464
1465 if (vreg->ocp_max_retries == 0)
1466 vreg->ocp_max_retries = QPNP_VS_OCP_DEFAULT_MAX_RETRIES;
1467 if (vreg->ocp_retry_delay_ms == 0)
1468 vreg->ocp_retry_delay_ms = QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS;
David Collinsd1ac2f12012-02-14 13:34:18 -08001469
1470 rdesc = &vreg->rdesc;
1471 rdesc->id = spmi->ctrl->nr;
1472 rdesc->owner = THIS_MODULE;
1473 rdesc->type = REGULATOR_VOLTAGE;
1474
1475 reg_name = kzalloc(strnlen(pdata->init_data.constraints.name,
1476 MAX_NAME_LEN) + 1, GFP_KERNEL);
1477 if (!reg_name) {
1478 dev_err(&spmi->dev, "%s: Can't allocate regulator name\n",
1479 __func__);
1480 kfree(vreg);
1481 return -ENOMEM;
1482 }
1483 strlcpy(reg_name, pdata->init_data.constraints.name,
1484 strnlen(pdata->init_data.constraints.name, MAX_NAME_LEN) + 1);
1485 rdesc->name = reg_name;
1486
1487 dev_set_drvdata(&spmi->dev, vreg);
1488
1489 rc = qpnp_regulator_match(vreg);
1490 if (rc) {
1491 vreg_err(vreg, "regulator type unknown, rc=%d\n", rc);
1492 goto bail;
1493 }
1494
1495 if (is_dt && rdesc->ops) {
1496 /* Fill in ops and mode masks when using device tree. */
1497 if (rdesc->ops->enable)
1498 pdata->init_data.constraints.valid_ops_mask
1499 |= REGULATOR_CHANGE_STATUS;
1500 if (rdesc->ops->get_voltage)
1501 pdata->init_data.constraints.valid_ops_mask
1502 |= REGULATOR_CHANGE_VOLTAGE;
1503 if (rdesc->ops->get_mode) {
1504 pdata->init_data.constraints.valid_ops_mask
1505 |= REGULATOR_CHANGE_MODE
1506 | REGULATOR_CHANGE_DRMS;
1507 pdata->init_data.constraints.valid_modes_mask
1508 = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1509 }
1510 }
1511
1512 rc = qpnp_regulator_init_registers(vreg, pdata);
1513 if (rc) {
1514 vreg_err(vreg, "common initialization failed, rc=%d\n", rc);
1515 goto bail;
1516 }
1517
David Collins50f28e52013-04-05 13:44:50 -07001518 if (vreg->logical_type != QPNP_REGULATOR_LOGICAL_TYPE_VS)
1519 vreg->ocp_irq = 0;
1520
1521 if (vreg->ocp_irq) {
1522 rc = devm_request_irq(&spmi->dev, vreg->ocp_irq,
1523 qpnp_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
1524 vreg);
1525 if (rc < 0) {
1526 vreg_err(vreg, "failed to request irq %d, rc=%d\n",
1527 vreg->ocp_irq, rc);
1528 goto bail;
1529 }
1530
1531 INIT_DELAYED_WORK(&vreg->ocp_work, qpnp_regulator_vs_ocp_work);
1532 }
1533
David Collinsd1ac2f12012-02-14 13:34:18 -08001534 vreg->rdev = regulator_register(rdesc, &spmi->dev,
1535 &(pdata->init_data), vreg, spmi->dev.of_node);
1536 if (IS_ERR(vreg->rdev)) {
1537 rc = PTR_ERR(vreg->rdev);
David Collins03d222f2013-06-25 11:02:22 -07001538 if (rc != -EPROBE_DEFER)
1539 vreg_err(vreg, "regulator_register failed, rc=%d\n",
1540 rc);
David Collins50f28e52013-04-05 13:44:50 -07001541 goto cancel_ocp_work;
David Collinsd1ac2f12012-02-14 13:34:18 -08001542 }
1543
1544 qpnp_vreg_show_state(vreg->rdev, QPNP_REGULATOR_ACTION_INIT);
1545
1546 return 0;
1547
David Collins50f28e52013-04-05 13:44:50 -07001548cancel_ocp_work:
1549 if (vreg->ocp_irq)
1550 cancel_delayed_work_sync(&vreg->ocp_work);
David Collinsd1ac2f12012-02-14 13:34:18 -08001551bail:
David Collins03d222f2013-06-25 11:02:22 -07001552 if (rc && rc != -EPROBE_DEFER)
David Collinsd1ac2f12012-02-14 13:34:18 -08001553 vreg_err(vreg, "probe failed, rc=%d\n", rc);
1554
1555 kfree(vreg->rdesc.name);
1556 kfree(vreg);
1557
1558 return rc;
1559}
1560
1561static int __devexit qpnp_regulator_remove(struct spmi_device *spmi)
1562{
1563 struct qpnp_regulator *vreg;
1564
1565 vreg = dev_get_drvdata(&spmi->dev);
1566 dev_set_drvdata(&spmi->dev, NULL);
1567
1568 if (vreg) {
1569 regulator_unregister(vreg->rdev);
David Collins50f28e52013-04-05 13:44:50 -07001570 if (vreg->ocp_irq)
1571 cancel_delayed_work_sync(&vreg->ocp_work);
David Collinsd1ac2f12012-02-14 13:34:18 -08001572 kfree(vreg->rdesc.name);
1573 kfree(vreg);
1574 }
1575
1576 return 0;
1577}
1578
1579static struct of_device_id spmi_match_table[] = {
1580 { .compatible = QPNP_REGULATOR_DRIVER_NAME, },
1581 {}
1582};
1583
1584static const struct spmi_device_id qpnp_regulator_id[] = {
1585 { QPNP_REGULATOR_DRIVER_NAME, 0 },
1586 { }
1587};
1588MODULE_DEVICE_TABLE(spmi, qpnp_regulator_id);
1589
1590static struct spmi_driver qpnp_regulator_driver = {
1591 .driver = {
1592 .name = QPNP_REGULATOR_DRIVER_NAME,
1593 .of_match_table = spmi_match_table,
1594 .owner = THIS_MODULE,
1595 },
1596 .probe = qpnp_regulator_probe,
1597 .remove = __devexit_p(qpnp_regulator_remove),
1598 .id_table = qpnp_regulator_id,
1599};
1600
1601/*
1602 * Pre-compute the number of set points available for each regulator type to
1603 * avoid unnecessary calculations later in runtime.
1604 */
1605static void qpnp_regulator_set_point_init(void)
1606{
1607 struct qpnp_voltage_set_points **set_points;
1608 int i, j, temp;
1609
1610 set_points = all_set_points;
1611
1612 for (i = 0; i < ARRAY_SIZE(all_set_points); i++) {
1613 temp = 0;
1614 for (j = 0; j < all_set_points[i]->count; j++) {
1615 all_set_points[i]->range[j].n_voltages
1616 = (all_set_points[i]->range[j].max_uV
1617 - all_set_points[i]->range[j].set_point_min_uV)
1618 / all_set_points[i]->range[j].step_uV + 1;
1619 temp += all_set_points[i]->range[j].n_voltages;
1620 }
1621 all_set_points[i]->n_voltages = temp;
1622 }
1623}
1624
1625/**
1626 * qpnp_regulator_init() - register spmi driver for qpnp-regulator
1627 *
1628 * This initialization function should be called in systems in which driver
1629 * registration ordering must be controlled precisely.
1630 */
1631int __init qpnp_regulator_init(void)
1632{
1633 static bool has_registered;
1634
1635 if (has_registered)
1636 return 0;
1637 else
1638 has_registered = true;
1639
1640 qpnp_regulator_set_point_init();
1641
1642 return spmi_driver_register(&qpnp_regulator_driver);
1643}
1644EXPORT_SYMBOL(qpnp_regulator_init);
1645
1646static void __exit qpnp_regulator_exit(void)
1647{
1648 spmi_driver_unregister(&qpnp_regulator_driver);
1649}
1650
1651MODULE_DESCRIPTION("QPNP PMIC regulator driver");
1652MODULE_LICENSE("GPL v2");
1653
1654arch_initcall(qpnp_regulator_init);
1655module_exit(qpnp_regulator_exit);