blob: 2d10f8980d5f75495c54ae40c9b0bd34040c8e6c [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
517 if (vreg->ocp_irq)
518 vreg->vs_enable_time = ktime_get();
519
520 return qpnp_regulator_common_enable(rdev);
521}
522
David Collinsd1ac2f12012-02-14 13:34:18 -0800523static int qpnp_regulator_common_disable(struct regulator_dev *rdev)
524{
525 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
526 int rc;
527
528 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
529 QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
530 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
531
532 if (rc)
533 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
534 else
535 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_DISABLE);
536
537 return rc;
538}
539
540static int qpnp_regulator_select_voltage(struct qpnp_regulator *vreg,
David Collinsdf5d5e72012-11-13 17:16:19 -0800541 int min_uV, int max_uV, int *range_sel, int *voltage_sel,
542 unsigned *selector)
David Collinsd1ac2f12012-02-14 13:34:18 -0800543{
544 struct qpnp_voltage_range *range;
545 int uV = min_uV;
David Collinsdf5d5e72012-11-13 17:16:19 -0800546 int lim_min_uV, lim_max_uV, i, range_id;
David Collinsd1ac2f12012-02-14 13:34:18 -0800547
548 /* Check if request voltage is outside of physically settable range. */
549 lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
550 lim_max_uV =
551 vreg->set_points->range[vreg->set_points->count - 1].max_uV;
552
553 if (uV < lim_min_uV && max_uV >= lim_min_uV)
554 uV = lim_min_uV;
555
556 if (uV < lim_min_uV || uV > lim_max_uV) {
557 vreg_err(vreg,
558 "request v=[%d, %d] is outside possible v=[%d, %d]\n",
559 min_uV, max_uV, lim_min_uV, lim_max_uV);
560 return -EINVAL;
561 }
562
563 /* Find the range which uV is inside of. */
564 for (i = vreg->set_points->count - 1; i > 0; i--)
565 if (uV > vreg->set_points->range[i - 1].max_uV)
566 break;
David Collinsdf5d5e72012-11-13 17:16:19 -0800567 range_id = i;
568 range = &vreg->set_points->range[range_id];
David Collinsd1ac2f12012-02-14 13:34:18 -0800569 *range_sel = range->range_sel;
570
571 /*
572 * Force uV to be an allowed set point by applying a ceiling function to
573 * the uV value.
574 */
575 *voltage_sel = (uV - range->min_uV + range->step_uV - 1)
576 / range->step_uV;
577 uV = *voltage_sel * range->step_uV + range->min_uV;
578
579 if (uV > max_uV) {
580 vreg_err(vreg,
581 "request v=[%d, %d] cannot be met by any set point; "
582 "next set point: %d\n",
583 min_uV, max_uV, uV);
584 return -EINVAL;
585 }
586
David Collinsdf5d5e72012-11-13 17:16:19 -0800587 *selector = 0;
588 for (i = 0; i < range_id; i++)
589 *selector += vreg->set_points->range[i].n_voltages;
590 *selector += (uV - range->set_point_min_uV) / range->step_uV;
591
David Collinsd1ac2f12012-02-14 13:34:18 -0800592 return 0;
593}
594
595static int qpnp_regulator_common_set_voltage(struct regulator_dev *rdev,
596 int min_uV, int max_uV, unsigned *selector)
597{
598 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
599 int rc, range_sel, voltage_sel;
600 u8 buf[2];
601
602 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
David Collinsdf5d5e72012-11-13 17:16:19 -0800603 &voltage_sel, selector);
David Collinsd1ac2f12012-02-14 13:34:18 -0800604 if (rc) {
605 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
606 return rc;
607 }
608
609 buf[0] = range_sel;
610 buf[1] = voltage_sel;
611 if ((vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] != range_sel)
612 && (vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] == voltage_sel)) {
613 /* Handle latched range change. */
614 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
615 buf, 2);
616 if (!rc) {
617 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = buf[0];
618 vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] = buf[1];
619 }
620 } else {
621 /* Either write can be optimized away safely. */
622 rc = qpnp_vreg_write_optimized(vreg,
623 QPNP_COMMON_REG_VOLTAGE_RANGE, buf,
624 &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE], 2);
625 }
626
627 if (rc)
628 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
629 else
630 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
631
632 return rc;
633}
634
635static int qpnp_regulator_common_get_voltage(struct regulator_dev *rdev)
636{
637 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
638 struct qpnp_voltage_range *range = NULL;
639 int range_sel, voltage_sel, i;
640
641 range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
642 voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
643
644 for (i = 0; i < vreg->set_points->count; i++) {
645 if (vreg->set_points->range[i].range_sel == range_sel) {
646 range = &vreg->set_points->range[i];
647 break;
648 }
649 }
650
651 if (!range) {
652 vreg_err(vreg, "voltage unknown, range %d is invalid\n",
653 range_sel);
David Collinsbdd32812012-05-10 13:22:56 -0700654 return VOLTAGE_UNKNOWN;
David Collinsd1ac2f12012-02-14 13:34:18 -0800655 }
656
657 return range->step_uV * voltage_sel + range->min_uV;
658}
659
660static int qpnp_regulator_boost_set_voltage(struct regulator_dev *rdev,
661 int min_uV, int max_uV, unsigned *selector)
662{
663 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
664 int rc, range_sel, voltage_sel;
665
666 rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
David Collinsdf5d5e72012-11-13 17:16:19 -0800667 &voltage_sel, selector);
David Collinsd1ac2f12012-02-14 13:34:18 -0800668 if (rc) {
669 vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
670 return rc;
671 }
672
673 /*
674 * Boost type regulators do not have range select register so only
675 * voltage set register needs to be written.
676 */
677 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET,
678 voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]);
679
680 if (rc)
681 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
682 else
683 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
684
685 return rc;
686}
687
688static int qpnp_regulator_boost_get_voltage(struct regulator_dev *rdev)
689{
690 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
691 int voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
692
693 return boost_ranges[0].step_uV * voltage_sel + boost_ranges[0].min_uV;
694}
695
696static int qpnp_regulator_common_list_voltage(struct regulator_dev *rdev,
697 unsigned selector)
698{
699 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
700 int uV = 0;
701 int i;
702
703 if (selector >= vreg->set_points->n_voltages)
704 return 0;
705
706 for (i = 0; i < vreg->set_points->count; i++) {
707 if (selector < vreg->set_points->range[i].n_voltages) {
708 uV = selector * vreg->set_points->range[i].step_uV
709 + vreg->set_points->range[i].set_point_min_uV;
710 break;
711 } else {
712 selector -= vreg->set_points->range[i].n_voltages;
713 }
714 }
715
716 return uV;
717}
718
719static unsigned int qpnp_regulator_common_get_mode(struct regulator_dev *rdev)
720{
721 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
722
723 return (vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]
724 & QPNP_COMMON_MODE_HPM_MASK)
725 ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
726}
727
728static int qpnp_regulator_common_set_mode(struct regulator_dev *rdev,
729 unsigned int mode)
730{
731 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
732 int rc = 0;
733 u8 val;
734
735 if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
736 vreg_err(vreg, "invalid mode: %u\n", mode);
737 return -EINVAL;
738 }
739
740 val = (mode == REGULATOR_MODE_NORMAL ? QPNP_COMMON_MODE_HPM_MASK : 0);
741
742 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_MODE, val,
743 QPNP_COMMON_MODE_HPM_MASK,
744 &vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]);
745
746 if (rc)
747 vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
748 else
749 qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE);
750
751 return rc;
752}
753
754static unsigned int qpnp_regulator_common_get_optimum_mode(
755 struct regulator_dev *rdev, int input_uV, int output_uV,
756 int load_uA)
757{
758 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
759 unsigned int mode;
760
761 if (load_uA + vreg->system_load >= vreg->hpm_min_load)
762 mode = REGULATOR_MODE_NORMAL;
763 else
764 mode = REGULATOR_MODE_IDLE;
765
766 return mode;
767}
768
769static int qpnp_regulator_common_enable_time(struct regulator_dev *rdev)
770{
771 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
772
773 return vreg->enable_time;
774}
775
David Collins50f28e52013-04-05 13:44:50 -0700776static int qpnp_regulator_vs_clear_ocp(struct qpnp_regulator *vreg)
777{
778 int rc;
779
780 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
781 QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
782 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
783 if (rc)
784 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
785
786 vreg->vs_enable_time = ktime_get();
787
788 rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
789 QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
790 &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
791 if (rc)
792 vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
793
794 if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
795 pr_info("%s: switch state toggled after OCP event\n",
796 vreg->rdesc.name);
797 }
798
799 return rc;
800}
801
802static void qpnp_regulator_vs_ocp_work(struct work_struct *work)
803{
804 struct delayed_work *dwork
805 = container_of(work, struct delayed_work, work);
806 struct qpnp_regulator *vreg
807 = container_of(dwork, struct qpnp_regulator, ocp_work);
808
809 qpnp_regulator_vs_clear_ocp(vreg);
810
811 return;
812}
813
814static irqreturn_t qpnp_regulator_vs_ocp_isr(int irq, void *data)
815{
816 struct qpnp_regulator *vreg = data;
817 ktime_t ocp_irq_time;
818 s64 ocp_trigger_delay_us;
819
820 ocp_irq_time = ktime_get();
821 ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
822 vreg->vs_enable_time);
823
824 /*
825 * Reset the OCP count if there is a large delay between switch enable
826 * and when OCP triggers. This is indicative of a hotplug event as
827 * opposed to a fault.
828 */
829 if (ocp_trigger_delay_us > QPNP_VS_OCP_FAULT_DELAY_US)
830 vreg->ocp_count = 0;
831
832 /* Wait for switch output to settle back to 0 V after OCP triggered. */
833 udelay(QPNP_VS_OCP_FALL_DELAY_US);
834
835 vreg->ocp_count++;
836
837 if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
838 pr_info("%s: VS OCP triggered, count = %d, delay = %lld us\n",
839 vreg->rdesc.name, vreg->ocp_count,
840 ocp_trigger_delay_us);
841 }
842
843 if (vreg->ocp_count == 1) {
844 /* Immediately clear the over current condition. */
845 qpnp_regulator_vs_clear_ocp(vreg);
846 } else if (vreg->ocp_count <= vreg->ocp_max_retries) {
847 /* Schedule the over current clear task to run later. */
848 schedule_delayed_work(&vreg->ocp_work,
849 msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
850 } else {
851 vreg_err(vreg, "OCP triggered %d times; no further retries\n",
852 vreg->ocp_count);
853 }
854
855 return IRQ_HANDLED;
856}
857
David Collinsd1ac2f12012-02-14 13:34:18 -0800858static const char const *qpnp_print_actions[] = {
859 [QPNP_REGULATOR_ACTION_INIT] = "initial ",
860 [QPNP_REGULATOR_ACTION_ENABLE] = "enable ",
861 [QPNP_REGULATOR_ACTION_DISABLE] = "disable ",
862 [QPNP_REGULATOR_ACTION_VOLTAGE] = "set voltage",
863 [QPNP_REGULATOR_ACTION_MODE] = "set mode ",
864};
865
866static void qpnp_vreg_show_state(struct regulator_dev *rdev,
867 enum qpnp_regulator_action action)
868{
869 struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
870 const char *action_label = qpnp_print_actions[action];
871 unsigned int mode = 0;
872 int uV = 0;
873 const char *mode_label = "";
874 enum qpnp_regulator_logical_type type;
875 const char *enable_label;
876 char pc_enable_label[5] = {'\0'};
877 char pc_mode_label[8] = {'\0'};
878 bool show_req, show_dupe, show_init, has_changed;
879 u8 en_reg, mode_reg;
880
881 /* Do not print unless appropriate flags are set. */
882 show_req = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_REQUEST;
883 show_dupe = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_DUPLICATE;
884 show_init = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT;
885 has_changed = vreg->write_count != vreg->prev_write_count;
886 if (!((show_init && action == QPNP_REGULATOR_ACTION_INIT)
887 || (show_req && (has_changed || show_dupe)))) {
888 return;
889 }
890
891 vreg->prev_write_count = vreg->write_count;
892
893 type = vreg->logical_type;
894
895 enable_label = qpnp_regulator_common_is_enabled(rdev) ? "on " : "off";
896
897 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
898 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
899 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
900 uV = qpnp_regulator_common_get_voltage(rdev);
901
902 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST)
903 uV = qpnp_regulator_boost_get_voltage(rdev);
904
905 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
906 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
David Collinsd1672c22013-03-12 15:21:47 -0700907 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
908 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
David Collinsd1ac2f12012-02-14 13:34:18 -0800909 mode = qpnp_regulator_common_get_mode(rdev);
910 mode_label = mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM";
911 }
912
913 if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
914 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
915 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
916 en_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE];
917 pc_enable_label[0] =
918 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK ? '3' : '_';
919 pc_enable_label[1] =
920 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK ? '2' : '_';
921 pc_enable_label[2] =
922 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK ? '1' : '_';
923 pc_enable_label[3] =
924 en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK ? '0' : '_';
925 }
926
927 switch (type) {
928 case QPNP_REGULATOR_LOGICAL_TYPE_SMPS:
929 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
930 pc_mode_label[0] =
931 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
932 pc_mode_label[1] =
933 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
934 pc_mode_label[2] =
935 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
936 pc_mode_label[3] =
937 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
938 pc_mode_label[4] =
939 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
940 pc_mode_label[5] =
941 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
942
943 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
944 "alt_mode=%s\n",
945 action_label, vreg->rdesc.name, enable_label, uV,
946 mode_label, pc_enable_label, pc_mode_label);
947 break;
948 case QPNP_REGULATOR_LOGICAL_TYPE_LDO:
949 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
950 pc_mode_label[0] =
951 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
952 pc_mode_label[1] =
953 mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
954 pc_mode_label[2] =
955 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
956 pc_mode_label[3] =
957 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
958 pc_mode_label[4] =
959 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
960 pc_mode_label[5] =
961 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
962 pc_mode_label[6] =
963 mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
964
965 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
966 "alt_mode=%s\n",
967 action_label, vreg->rdesc.name, enable_label, uV,
968 mode_label, pc_enable_label, pc_mode_label);
969 break;
970 case QPNP_REGULATOR_LOGICAL_TYPE_VS:
971 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
972 pc_mode_label[0] =
973 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
974 pc_mode_label[1] =
975 mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
976
David Collinsd1672c22013-03-12 15:21:47 -0700977 pr_info("%s %-11s: %s, mode=%s, pc_en=%s, alt_mode=%s\n",
David Collinsd1ac2f12012-02-14 13:34:18 -0800978 action_label, vreg->rdesc.name, enable_label,
David Collinsd1672c22013-03-12 15:21:47 -0700979 mode_label, pc_enable_label, pc_mode_label);
David Collinsd1ac2f12012-02-14 13:34:18 -0800980 break;
981 case QPNP_REGULATOR_LOGICAL_TYPE_BOOST:
982 pr_info("%s %-11s: %s, v=%7d uV\n",
983 action_label, vreg->rdesc.name, enable_label, uV);
984 break;
985 case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS:
986 mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
987 pc_mode_label[0] =
988 mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
989
990 pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
991 action_label, vreg->rdesc.name, enable_label, uV,
992 mode_label, pc_mode_label);
993 break;
994 default:
995 break;
996 }
997}
998
999static struct regulator_ops qpnp_smps_ops = {
1000 .enable = qpnp_regulator_common_enable,
1001 .disable = qpnp_regulator_common_disable,
1002 .is_enabled = qpnp_regulator_common_is_enabled,
1003 .set_voltage = qpnp_regulator_common_set_voltage,
1004 .get_voltage = qpnp_regulator_common_get_voltage,
1005 .list_voltage = qpnp_regulator_common_list_voltage,
1006 .set_mode = qpnp_regulator_common_set_mode,
1007 .get_mode = qpnp_regulator_common_get_mode,
1008 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
1009 .enable_time = qpnp_regulator_common_enable_time,
1010};
1011
1012static struct regulator_ops qpnp_ldo_ops = {
1013 .enable = qpnp_regulator_common_enable,
1014 .disable = qpnp_regulator_common_disable,
1015 .is_enabled = qpnp_regulator_common_is_enabled,
1016 .set_voltage = qpnp_regulator_common_set_voltage,
1017 .get_voltage = qpnp_regulator_common_get_voltage,
1018 .list_voltage = qpnp_regulator_common_list_voltage,
1019 .set_mode = qpnp_regulator_common_set_mode,
1020 .get_mode = qpnp_regulator_common_get_mode,
1021 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
1022 .enable_time = qpnp_regulator_common_enable_time,
1023};
1024
1025static struct regulator_ops qpnp_vs_ops = {
David Collins50f28e52013-04-05 13:44:50 -07001026 .enable = qpnp_regulator_vs_enable,
David Collinsd1ac2f12012-02-14 13:34:18 -08001027 .disable = qpnp_regulator_common_disable,
1028 .is_enabled = qpnp_regulator_common_is_enabled,
1029 .enable_time = qpnp_regulator_common_enable_time,
1030};
1031
1032static struct regulator_ops qpnp_boost_ops = {
1033 .enable = qpnp_regulator_common_enable,
1034 .disable = qpnp_regulator_common_disable,
1035 .is_enabled = qpnp_regulator_common_is_enabled,
1036 .set_voltage = qpnp_regulator_boost_set_voltage,
1037 .get_voltage = qpnp_regulator_boost_get_voltage,
1038 .list_voltage = qpnp_regulator_common_list_voltage,
1039 .enable_time = qpnp_regulator_common_enable_time,
1040};
1041
1042static struct regulator_ops qpnp_ftsmps_ops = {
1043 .enable = qpnp_regulator_common_enable,
1044 .disable = qpnp_regulator_common_disable,
1045 .is_enabled = qpnp_regulator_common_is_enabled,
1046 .set_voltage = qpnp_regulator_common_set_voltage,
1047 .get_voltage = qpnp_regulator_common_get_voltage,
1048 .list_voltage = qpnp_regulator_common_list_voltage,
1049 .set_mode = qpnp_regulator_common_set_mode,
1050 .get_mode = qpnp_regulator_common_get_mode,
1051 .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
1052 .enable_time = qpnp_regulator_common_enable_time,
1053};
1054
David Collinsbad606a2012-08-21 10:57:36 -07001055/* Maximum possible digital major revision value */
1056#define INF 0xFF
1057
David Collinsd1ac2f12012-02-14 13:34:18 -08001058static const struct qpnp_regulator_mapping supported_regulators[] = {
David Collinsbad606a2012-08-21 10:57:36 -07001059 /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
1060 QPNP_VREG_MAP(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
1061 QPNP_VREG_MAP(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
1062 QPNP_VREG_MAP(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
1063 QPNP_VREG_MAP(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
1064 QPNP_VREG_MAP(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
1065 QPNP_VREG_MAP(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
David Collins75ebd572012-08-29 14:08:10 -07001066 QPNP_VREG_MAP(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000),
1067 QPNP_VREG_MAP(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000),
1068 QPNP_VREG_MAP(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000),
1069 QPNP_VREG_MAP(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000),
David Collinsbad606a2012-08-21 10:57:36 -07001070 QPNP_VREG_MAP(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
1071 QPNP_VREG_MAP(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
1072 QPNP_VREG_MAP(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
1073 QPNP_VREG_MAP(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
1074 QPNP_VREG_MAP(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
David Collins75ebd572012-08-29 14:08:10 -07001075 QPNP_VREG_MAP(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000),
1076 QPNP_VREG_MAP(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000),
1077 QPNP_VREG_MAP(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000),
1078 QPNP_VREG_MAP(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000),
1079 QPNP_VREG_MAP(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000),
David Collinsbad606a2012-08-21 10:57:36 -07001080 QPNP_VREG_MAP(VS, LV100, 0, INF, VS, vs, none, 0),
1081 QPNP_VREG_MAP(VS, LV300, 0, INF, VS, vs, none, 0),
1082 QPNP_VREG_MAP(VS, MV300, 0, INF, VS, vs, none, 0),
1083 QPNP_VREG_MAP(VS, MV500, 0, INF, VS, vs, none, 0),
1084 QPNP_VREG_MAP(VS, HDMI, 0, INF, VS, vs, none, 0),
1085 QPNP_VREG_MAP(VS, OTG, 0, INF, VS, vs, none, 0),
1086 QPNP_VREG_MAP(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
1087 QPNP_VREG_MAP(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
David Collinsd1ac2f12012-02-14 13:34:18 -08001088};
1089
1090static int qpnp_regulator_match(struct qpnp_regulator *vreg)
1091{
1092 const struct qpnp_regulator_mapping *mapping;
Michael Bohan9328e492012-08-09 11:37:36 -07001093 struct device_node *node = vreg->spmi_dev->dev.of_node;
David Collinsd1ac2f12012-02-14 13:34:18 -08001094 int rc, i;
David Collinsbad606a2012-08-21 10:57:36 -07001095 u32 type_reg[2], dig_major_rev;
1096 u8 version[QPNP_COMMON_REG_SUBTYPE - QPNP_COMMON_REG_DIG_MAJOR_REV + 1];
1097 u8 type, subtype;
David Collinsd1ac2f12012-02-14 13:34:18 -08001098
David Collinsbad606a2012-08-21 10:57:36 -07001099 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_DIG_MAJOR_REV, version,
1100 ARRAY_SIZE(version));
1101 if (rc) {
1102 vreg_err(vreg, "could not read version registers, rc=%d\n", rc);
1103 return rc;
1104 }
1105 dig_major_rev = version[QPNP_COMMON_REG_DIG_MAJOR_REV
1106 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1107 type = version[QPNP_COMMON_REG_TYPE
1108 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1109 subtype = version[QPNP_COMMON_REG_SUBTYPE
1110 - QPNP_COMMON_REG_DIG_MAJOR_REV];
1111
1112 /*
1113 * Override type and subtype register values if qcom,force-type is
1114 * present in the device tree node.
1115 */
1116 rc = of_property_read_u32_array(node, "qcom,force-type", type_reg, 2);
Michael Bohan9328e492012-08-09 11:37:36 -07001117 if (!rc) {
1118 type = type_reg[0];
1119 subtype = type_reg[1];
David Collinsd1ac2f12012-02-14 13:34:18 -08001120 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001121
1122 rc = -ENODEV;
1123 for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
1124 mapping = &supported_regulators[i];
David Collinsbad606a2012-08-21 10:57:36 -07001125 if (mapping->type == type && mapping->subtype == subtype
1126 && mapping->revision_min <= dig_major_rev
1127 && mapping->revision_max >= dig_major_rev) {
David Collinsd1ac2f12012-02-14 13:34:18 -08001128 vreg->logical_type = mapping->logical_type;
1129 vreg->set_points = mapping->set_points;
1130 vreg->hpm_min_load = mapping->hpm_min_load;
1131 vreg->rdesc.ops = mapping->ops;
1132 vreg->rdesc.n_voltages
1133 = mapping->set_points->n_voltages;
1134 rc = 0;
1135 break;
1136 }
1137 }
1138
1139 return rc;
1140}
1141
1142static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg,
1143 struct qpnp_regulator_platform_data *pdata)
1144{
1145 int rc, i;
1146 enum qpnp_regulator_logical_type type;
1147 u8 ctrl_reg[8], reg, mask;
1148
1149 type = vreg->logical_type;
1150
1151 rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1152 vreg->ctrl_reg, 8);
1153 if (rc) {
1154 vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
1155 return rc;
1156 }
1157
1158 for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++)
1159 ctrl_reg[i] = vreg->ctrl_reg[i];
1160
1161 /* Set up enable pin control. */
1162 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1163 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1164 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1165 && !(pdata->pin_ctrl_enable
1166 & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
1167 ctrl_reg[QPNP_COMMON_IDX_ENABLE] &=
1168 ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1169 ctrl_reg[QPNP_COMMON_IDX_ENABLE] |=
1170 pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
1171 }
1172
David Collinsd1672c22013-03-12 15:21:47 -07001173 /* Set up HPM control. */
1174 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1175 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1176 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1177 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
1178 && (pdata->hpm_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
1179 ctrl_reg[QPNP_COMMON_IDX_MODE] &= ~QPNP_COMMON_MODE_HPM_MASK;
1180 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1181 (pdata->hpm_enable ? QPNP_COMMON_MODE_HPM_MASK : 0);
1182 }
1183
David Collinsd1ac2f12012-02-14 13:34:18 -08001184 /* Set up auto mode control. */
1185 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1186 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1187 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1188 || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
1189 && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
1190 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1191 ~QPNP_COMMON_MODE_AUTO_MASK;
1192 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1193 (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0);
1194 }
1195
1196 /* Set up mode pin control. */
1197 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1198 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO)
1199 && !(pdata->pin_ctrl_hpm
1200 & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1201 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1202 ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1203 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1204 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
1205 }
1206
1207 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS
1208 && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
1209 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1210 ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1211 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1212 pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
1213 }
1214
1215 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1216 && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1217 ctrl_reg[QPNP_COMMON_IDX_MODE] &=
1218 ~QPNP_COMMON_MODE_BYPASS_MASK;
1219 ctrl_reg[QPNP_COMMON_IDX_MODE] |=
1220 (pdata->bypass_mode_enable
1221 ? QPNP_COMMON_MODE_BYPASS_MASK : 0);
1222 }
1223
1224 /* Set boost current limit. */
1225 if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
1226 && pdata->boost_current_limit
1227 != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) {
David Collins6f846752012-08-17 17:59:28 -07001228 reg = pdata->boost_current_limit;
1229 mask = QPNP_BOOST_CURRENT_LIMIT_MASK;
1230 rc = qpnp_vreg_masked_read_write(vreg,
1231 QPNP_BOOST_REG_CURRENT_LIMIT, reg, mask);
1232 if (rc) {
1233 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1234 return rc;
1235 }
David Collinsd1ac2f12012-02-14 13:34:18 -08001236 }
1237
1238 /* Write back any control register values that were modified. */
1239 rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
1240 ctrl_reg, vreg->ctrl_reg, 8);
1241 if (rc) {
1242 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1243 return rc;
1244 }
1245
1246 /* Set pull down. */
1247 if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
1248 || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1249 || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
1250 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1251 reg = pdata->pull_down_enable
1252 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1253 rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_PULL_DOWN, &reg, 1);
1254 if (rc) {
1255 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1256 return rc;
1257 }
1258 }
1259
1260 if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
1261 && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1262 /* FTSMPS has other bits in the pull down control register. */
1263 reg = pdata->pull_down_enable
1264 ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
1265 rc = qpnp_vreg_masked_read_write(vreg,
1266 QPNP_COMMON_REG_PULL_DOWN, reg,
1267 QPNP_COMMON_PULL_DOWN_ENABLE_MASK);
1268 if (rc) {
1269 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1270 return rc;
1271 }
1272 }
1273
1274 /* Set soft start for LDO. */
1275 if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
1276 && pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1277 reg = pdata->soft_start_enable
1278 ? QPNP_LDO_SOFT_START_ENABLE_MASK : 0;
1279 rc = qpnp_vreg_write(vreg, QPNP_LDO_REG_SOFT_START, &reg, 1);
1280 if (rc) {
1281 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1282 return rc;
1283 }
1284 }
1285
1286 /* Set soft start strength and over current protection for VS. */
1287 if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
1288 reg = 0;
1289 mask = 0;
1290 if (pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
1291 reg |= pdata->soft_start_enable
1292 ? QPNP_VS_SOFT_START_ENABLE_MASK : 0;
1293 mask |= QPNP_VS_SOFT_START_ENABLE_MASK;
1294 }
1295 if (pdata->vs_soft_start_strength
1296 != QPNP_VS_SOFT_START_STR_HW_DEFAULT) {
1297 reg |= pdata->vs_soft_start_strength
1298 & QPNP_VS_SOFT_START_SEL_MASK;
1299 mask |= QPNP_VS_SOFT_START_SEL_MASK;
1300 }
1301 rc = qpnp_vreg_masked_read_write(vreg, QPNP_VS_REG_SOFT_START,
1302 reg, mask);
1303 if (rc) {
1304 vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
1305 return rc;
1306 }
1307
1308 if (pdata->ocp_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
David Collins30dac622013-03-12 15:16:23 -07001309 reg = pdata->ocp_enable ? QPNP_VS_OCP_NO_OVERRIDE
1310 : QPNP_VS_OCP_OVERRIDE;
David Collinsd1ac2f12012-02-14 13:34:18 -08001311 rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
1312 if (rc) {
1313 vreg_err(vreg, "spmi write failed, rc=%d\n",
1314 rc);
1315 return rc;
1316 }
1317 }
1318 }
1319
1320 return rc;
1321}
1322
1323/* Fill in pdata elements based on values found in device tree. */
1324static int qpnp_regulator_get_dt_config(struct spmi_device *spmi,
1325 struct qpnp_regulator_platform_data *pdata)
1326{
1327 struct resource *res;
1328 struct device_node *node = spmi->dev.of_node;
1329 int rc = 0;
1330
1331 pdata->init_data.constraints.input_uV
1332 = pdata->init_data.constraints.max_uV;
1333
Michael Bohan0e5534d2012-05-22 17:33:45 -07001334 res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
David Collinsd1ac2f12012-02-14 13:34:18 -08001335 if (!res) {
1336 dev_err(&spmi->dev, "%s: node is missing base address\n",
1337 __func__);
1338 return -EINVAL;
1339 }
1340 pdata->base_addr = res->start;
1341
David Collins50f28e52013-04-05 13:44:50 -07001342 /* OCP IRQ is optional so ignore get errors. */
1343 pdata->ocp_irq = spmi_get_irq_byname(spmi, NULL, "ocp");
1344 if (pdata->ocp_irq < 0)
1345 pdata->ocp_irq = 0;
1346
David Collinsd1ac2f12012-02-14 13:34:18 -08001347 /*
1348 * Initialize configuration parameters to use hardware default in case
1349 * no value is specified via device tree.
1350 */
1351 pdata->auto_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1352 pdata->bypass_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1353 pdata->ocp_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1354 pdata->pull_down_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1355 pdata->soft_start_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
1356 pdata->boost_current_limit = QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT;
1357 pdata->pin_ctrl_enable = QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
1358 pdata->pin_ctrl_hpm = QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
1359 pdata->vs_soft_start_strength = QPNP_VS_SOFT_START_STR_HW_DEFAULT;
David Collinsd1672c22013-03-12 15:21:47 -07001360 pdata->hpm_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
David Collinsd1ac2f12012-02-14 13:34:18 -08001361
1362 /* These bindings are optional, so it is okay if they are not found. */
1363 of_property_read_u32(node, "qcom,auto-mode-enable",
1364 &pdata->auto_mode_enable);
1365 of_property_read_u32(node, "qcom,bypass-mode-enable",
1366 &pdata->bypass_mode_enable);
1367 of_property_read_u32(node, "qcom,ocp-enable", &pdata->ocp_enable);
David Collins50f28e52013-04-05 13:44:50 -07001368 of_property_read_u32(node, "qcom,ocp-max-retries",
1369 &pdata->ocp_max_retries);
1370 of_property_read_u32(node, "qcom,ocp-retry-delay",
1371 &pdata->ocp_retry_delay_ms);
David Collinsd1ac2f12012-02-14 13:34:18 -08001372 of_property_read_u32(node, "qcom,pull-down-enable",
1373 &pdata->pull_down_enable);
1374 of_property_read_u32(node, "qcom,soft-start-enable",
1375 &pdata->soft_start_enable);
1376 of_property_read_u32(node, "qcom,boost-current-limit",
1377 &pdata->boost_current_limit);
1378 of_property_read_u32(node, "qcom,pin-ctrl-enable",
1379 &pdata->pin_ctrl_enable);
1380 of_property_read_u32(node, "qcom,pin-ctrl-hpm", &pdata->pin_ctrl_hpm);
David Collinsd1672c22013-03-12 15:21:47 -07001381 of_property_read_u32(node, "qcom,hpm-enable", &pdata->hpm_enable);
David Collinsd1ac2f12012-02-14 13:34:18 -08001382 of_property_read_u32(node, "qcom,vs-soft-start-strength",
1383 &pdata->vs_soft_start_strength);
1384 of_property_read_u32(node, "qcom,system-load", &pdata->system_load);
1385 of_property_read_u32(node, "qcom,enable-time", &pdata->enable_time);
David Collinsd1ac2f12012-02-14 13:34:18 -08001386
1387 return rc;
1388}
1389
1390static struct of_device_id spmi_match_table[];
1391
1392#define MAX_NAME_LEN 127
1393
1394static int __devinit qpnp_regulator_probe(struct spmi_device *spmi)
1395{
1396 struct qpnp_regulator_platform_data *pdata;
1397 struct qpnp_regulator *vreg;
1398 struct regulator_desc *rdesc;
1399 struct qpnp_regulator_platform_data of_pdata;
1400 struct regulator_init_data *init_data;
1401 char *reg_name;
1402 int rc;
1403 bool is_dt;
1404
1405 vreg = kzalloc(sizeof(struct qpnp_regulator), GFP_KERNEL);
1406 if (!vreg) {
1407 dev_err(&spmi->dev, "%s: Can't allocate qpnp_regulator\n",
1408 __func__);
1409 return -ENOMEM;
1410 }
1411
1412 is_dt = of_match_device(spmi_match_table, &spmi->dev);
1413
1414 /* Check if device tree is in use. */
1415 if (is_dt) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001416 init_data = of_get_regulator_init_data(&spmi->dev,
1417 spmi->dev.of_node);
David Collinsd1ac2f12012-02-14 13:34:18 -08001418 if (!init_data) {
1419 dev_err(&spmi->dev, "%s: unable to allocate memory\n",
1420 __func__);
1421 kfree(vreg);
1422 return -ENOMEM;
1423 }
1424 memset(&of_pdata, 0,
1425 sizeof(struct qpnp_regulator_platform_data));
1426 memcpy(&of_pdata.init_data, init_data,
1427 sizeof(struct regulator_init_data));
1428
1429 if (of_get_property(spmi->dev.of_node, "parent-supply", NULL))
1430 of_pdata.init_data.supply_regulator = "parent";
1431
1432 rc = qpnp_regulator_get_dt_config(spmi, &of_pdata);
1433 if (rc) {
1434 dev_err(&spmi->dev, "%s: DT parsing failed, rc=%d\n",
1435 __func__, rc);
1436 kfree(vreg);
1437 return -ENOMEM;
1438 }
1439
1440 pdata = &of_pdata;
1441 } else {
1442 pdata = spmi->dev.platform_data;
1443 }
1444
1445 if (pdata == NULL) {
1446 dev_err(&spmi->dev, "%s: no platform data specified\n",
1447 __func__);
1448 kfree(vreg);
1449 return -EINVAL;
1450 }
1451
1452 vreg->spmi_dev = spmi;
1453 vreg->prev_write_count = -1;
1454 vreg->write_count = 0;
1455 vreg->base_addr = pdata->base_addr;
1456 vreg->enable_time = pdata->enable_time;
1457 vreg->system_load = pdata->system_load;
1458 vreg->ocp_enable = pdata->ocp_enable;
David Collins50f28e52013-04-05 13:44:50 -07001459 vreg->ocp_irq = pdata->ocp_irq;
1460 vreg->ocp_max_retries = pdata->ocp_max_retries;
1461 vreg->ocp_retry_delay_ms = pdata->ocp_retry_delay_ms;
1462
1463 if (vreg->ocp_max_retries == 0)
1464 vreg->ocp_max_retries = QPNP_VS_OCP_DEFAULT_MAX_RETRIES;
1465 if (vreg->ocp_retry_delay_ms == 0)
1466 vreg->ocp_retry_delay_ms = QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS;
David Collinsd1ac2f12012-02-14 13:34:18 -08001467
1468 rdesc = &vreg->rdesc;
1469 rdesc->id = spmi->ctrl->nr;
1470 rdesc->owner = THIS_MODULE;
1471 rdesc->type = REGULATOR_VOLTAGE;
1472
1473 reg_name = kzalloc(strnlen(pdata->init_data.constraints.name,
1474 MAX_NAME_LEN) + 1, GFP_KERNEL);
1475 if (!reg_name) {
1476 dev_err(&spmi->dev, "%s: Can't allocate regulator name\n",
1477 __func__);
1478 kfree(vreg);
1479 return -ENOMEM;
1480 }
1481 strlcpy(reg_name, pdata->init_data.constraints.name,
1482 strnlen(pdata->init_data.constraints.name, MAX_NAME_LEN) + 1);
1483 rdesc->name = reg_name;
1484
1485 dev_set_drvdata(&spmi->dev, vreg);
1486
1487 rc = qpnp_regulator_match(vreg);
1488 if (rc) {
1489 vreg_err(vreg, "regulator type unknown, rc=%d\n", rc);
1490 goto bail;
1491 }
1492
1493 if (is_dt && rdesc->ops) {
1494 /* Fill in ops and mode masks when using device tree. */
1495 if (rdesc->ops->enable)
1496 pdata->init_data.constraints.valid_ops_mask
1497 |= REGULATOR_CHANGE_STATUS;
1498 if (rdesc->ops->get_voltage)
1499 pdata->init_data.constraints.valid_ops_mask
1500 |= REGULATOR_CHANGE_VOLTAGE;
1501 if (rdesc->ops->get_mode) {
1502 pdata->init_data.constraints.valid_ops_mask
1503 |= REGULATOR_CHANGE_MODE
1504 | REGULATOR_CHANGE_DRMS;
1505 pdata->init_data.constraints.valid_modes_mask
1506 = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
1507 }
1508 }
1509
1510 rc = qpnp_regulator_init_registers(vreg, pdata);
1511 if (rc) {
1512 vreg_err(vreg, "common initialization failed, rc=%d\n", rc);
1513 goto bail;
1514 }
1515
David Collins50f28e52013-04-05 13:44:50 -07001516 if (vreg->logical_type != QPNP_REGULATOR_LOGICAL_TYPE_VS)
1517 vreg->ocp_irq = 0;
1518
1519 if (vreg->ocp_irq) {
1520 rc = devm_request_irq(&spmi->dev, vreg->ocp_irq,
1521 qpnp_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
1522 vreg);
1523 if (rc < 0) {
1524 vreg_err(vreg, "failed to request irq %d, rc=%d\n",
1525 vreg->ocp_irq, rc);
1526 goto bail;
1527 }
1528
1529 INIT_DELAYED_WORK(&vreg->ocp_work, qpnp_regulator_vs_ocp_work);
1530 }
1531
David Collinsd1ac2f12012-02-14 13:34:18 -08001532 vreg->rdev = regulator_register(rdesc, &spmi->dev,
1533 &(pdata->init_data), vreg, spmi->dev.of_node);
1534 if (IS_ERR(vreg->rdev)) {
1535 rc = PTR_ERR(vreg->rdev);
1536 vreg_err(vreg, "regulator_register failed, rc=%d\n", rc);
David Collins50f28e52013-04-05 13:44:50 -07001537 goto cancel_ocp_work;
David Collinsd1ac2f12012-02-14 13:34:18 -08001538 }
1539
1540 qpnp_vreg_show_state(vreg->rdev, QPNP_REGULATOR_ACTION_INIT);
1541
1542 return 0;
1543
David Collins50f28e52013-04-05 13:44:50 -07001544cancel_ocp_work:
1545 if (vreg->ocp_irq)
1546 cancel_delayed_work_sync(&vreg->ocp_work);
David Collinsd1ac2f12012-02-14 13:34:18 -08001547bail:
1548 if (rc)
1549 vreg_err(vreg, "probe failed, rc=%d\n", rc);
1550
1551 kfree(vreg->rdesc.name);
1552 kfree(vreg);
1553
1554 return rc;
1555}
1556
1557static int __devexit qpnp_regulator_remove(struct spmi_device *spmi)
1558{
1559 struct qpnp_regulator *vreg;
1560
1561 vreg = dev_get_drvdata(&spmi->dev);
1562 dev_set_drvdata(&spmi->dev, NULL);
1563
1564 if (vreg) {
1565 regulator_unregister(vreg->rdev);
David Collins50f28e52013-04-05 13:44:50 -07001566 if (vreg->ocp_irq)
1567 cancel_delayed_work_sync(&vreg->ocp_work);
David Collinsd1ac2f12012-02-14 13:34:18 -08001568 kfree(vreg->rdesc.name);
1569 kfree(vreg);
1570 }
1571
1572 return 0;
1573}
1574
1575static struct of_device_id spmi_match_table[] = {
1576 { .compatible = QPNP_REGULATOR_DRIVER_NAME, },
1577 {}
1578};
1579
1580static const struct spmi_device_id qpnp_regulator_id[] = {
1581 { QPNP_REGULATOR_DRIVER_NAME, 0 },
1582 { }
1583};
1584MODULE_DEVICE_TABLE(spmi, qpnp_regulator_id);
1585
1586static struct spmi_driver qpnp_regulator_driver = {
1587 .driver = {
1588 .name = QPNP_REGULATOR_DRIVER_NAME,
1589 .of_match_table = spmi_match_table,
1590 .owner = THIS_MODULE,
1591 },
1592 .probe = qpnp_regulator_probe,
1593 .remove = __devexit_p(qpnp_regulator_remove),
1594 .id_table = qpnp_regulator_id,
1595};
1596
1597/*
1598 * Pre-compute the number of set points available for each regulator type to
1599 * avoid unnecessary calculations later in runtime.
1600 */
1601static void qpnp_regulator_set_point_init(void)
1602{
1603 struct qpnp_voltage_set_points **set_points;
1604 int i, j, temp;
1605
1606 set_points = all_set_points;
1607
1608 for (i = 0; i < ARRAY_SIZE(all_set_points); i++) {
1609 temp = 0;
1610 for (j = 0; j < all_set_points[i]->count; j++) {
1611 all_set_points[i]->range[j].n_voltages
1612 = (all_set_points[i]->range[j].max_uV
1613 - all_set_points[i]->range[j].set_point_min_uV)
1614 / all_set_points[i]->range[j].step_uV + 1;
1615 temp += all_set_points[i]->range[j].n_voltages;
1616 }
1617 all_set_points[i]->n_voltages = temp;
1618 }
1619}
1620
1621/**
1622 * qpnp_regulator_init() - register spmi driver for qpnp-regulator
1623 *
1624 * This initialization function should be called in systems in which driver
1625 * registration ordering must be controlled precisely.
1626 */
1627int __init qpnp_regulator_init(void)
1628{
1629 static bool has_registered;
1630
1631 if (has_registered)
1632 return 0;
1633 else
1634 has_registered = true;
1635
1636 qpnp_regulator_set_point_init();
1637
1638 return spmi_driver_register(&qpnp_regulator_driver);
1639}
1640EXPORT_SYMBOL(qpnp_regulator_init);
1641
1642static void __exit qpnp_regulator_exit(void)
1643{
1644 spmi_driver_unregister(&qpnp_regulator_driver);
1645}
1646
1647MODULE_DESCRIPTION("QPNP PMIC regulator driver");
1648MODULE_LICENSE("GPL v2");
1649
1650arch_initcall(qpnp_regulator_init);
1651module_exit(qpnp_regulator_exit);