diff options
Diffstat (limited to 'drivers/regulator/tps80031-regulator.c')
-rw-r--r-- | drivers/regulator/tps80031-regulator.c | 793 |
1 files changed, 793 insertions, 0 deletions
diff --git a/drivers/regulator/tps80031-regulator.c b/drivers/regulator/tps80031-regulator.c new file mode 100644 index 000000000000..a37ede80abbf --- /dev/null +++ b/drivers/regulator/tps80031-regulator.c | |||
@@ -0,0 +1,793 @@ | |||
1 | /* | ||
2 | * tps80031-regulator.c -- TI TPS80031 regulator driver. | ||
3 | * | ||
4 | * Regulator driver for TITPS80031/TPS80032 Fully Integrated Power | ||
5 | * Management with Power Path and Battery Charger. | ||
6 | * | ||
7 | * Copyright (c) 2012, NVIDIA Corporation. | ||
8 | * | ||
9 | * Author: Laxman Dewangan <ldewangan@nvidia.com> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or | ||
12 | * modify it under the terms of the GNU General Public License as | ||
13 | * published by the Free Software Foundation version 2. | ||
14 | * | ||
15 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind, | ||
16 | * whether express or implied; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public License | ||
21 | * along with this program; if not, write to the Free Software | ||
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
23 | * 02111-1307, USA | ||
24 | */ | ||
25 | |||
26 | #include <linux/delay.h> | ||
27 | #include <linux/err.h> | ||
28 | #include <linux/init.h> | ||
29 | #include <linux/kernel.h> | ||
30 | #include <linux/mfd/tps80031.h> | ||
31 | #include <linux/module.h> | ||
32 | #include <linux/platform_device.h> | ||
33 | #include <linux/regulator/driver.h> | ||
34 | #include <linux/regulator/machine.h> | ||
35 | #include <linux/slab.h> | ||
36 | |||
37 | /* Flags for DCDC Voltage reading */ | ||
38 | #define DCDC_OFFSET_EN BIT(0) | ||
39 | #define DCDC_EXTENDED_EN BIT(1) | ||
40 | #define TRACK_MODE_ENABLE BIT(2) | ||
41 | |||
42 | #define SMPS_MULTOFFSET_VIO BIT(1) | ||
43 | #define SMPS_MULTOFFSET_SMPS1 BIT(3) | ||
44 | #define SMPS_MULTOFFSET_SMPS2 BIT(4) | ||
45 | #define SMPS_MULTOFFSET_SMPS3 BIT(6) | ||
46 | #define SMPS_MULTOFFSET_SMPS4 BIT(0) | ||
47 | |||
48 | #define SMPS_CMD_MASK 0xC0 | ||
49 | #define SMPS_VSEL_MASK 0x3F | ||
50 | #define LDO_VSEL_MASK 0x1F | ||
51 | #define LDO_TRACK_VSEL_MASK 0x3F | ||
52 | |||
53 | #define MISC2_LDOUSB_IN_VSYS BIT(4) | ||
54 | #define MISC2_LDOUSB_IN_PMID BIT(3) | ||
55 | #define MISC2_LDOUSB_IN_MASK 0x18 | ||
56 | |||
57 | #define MISC2_LDO3_SEL_VIB_VAL BIT(0) | ||
58 | #define MISC2_LDO3_SEL_VIB_MASK 0x1 | ||
59 | |||
60 | #define BOOST_HW_PWR_EN BIT(5) | ||
61 | #define BOOST_HW_PWR_EN_MASK BIT(5) | ||
62 | |||
63 | #define OPA_MODE_EN BIT(6) | ||
64 | #define OPA_MODE_EN_MASK BIT(6) | ||
65 | |||
66 | #define USB_VBUS_CTRL_SET 0x04 | ||
67 | #define USB_VBUS_CTRL_CLR 0x05 | ||
68 | #define VBUS_DISCHRG 0x20 | ||
69 | |||
70 | struct tps80031_regulator_info { | ||
71 | /* Regulator register address.*/ | ||
72 | u8 trans_reg; | ||
73 | u8 state_reg; | ||
74 | u8 force_reg; | ||
75 | u8 volt_reg; | ||
76 | u8 volt_id; | ||
77 | |||
78 | /*Power request bits */ | ||
79 | int preq_bit; | ||
80 | |||
81 | /* used by regulator core */ | ||
82 | struct regulator_desc desc; | ||
83 | |||
84 | }; | ||
85 | |||
86 | struct tps80031_regulator { | ||
87 | struct device *dev; | ||
88 | struct regulator_dev *rdev; | ||
89 | struct tps80031_regulator_info *rinfo; | ||
90 | |||
91 | u8 device_flags; | ||
92 | unsigned int config_flags; | ||
93 | unsigned int ext_ctrl_flag; | ||
94 | }; | ||
95 | |||
96 | static inline struct device *to_tps80031_dev(struct regulator_dev *rdev) | ||
97 | { | ||
98 | return rdev_get_dev(rdev)->parent->parent; | ||
99 | } | ||
100 | |||
101 | static int tps80031_reg_is_enabled(struct regulator_dev *rdev) | ||
102 | { | ||
103 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
104 | struct device *parent = to_tps80031_dev(rdev); | ||
105 | u8 reg_val; | ||
106 | int ret; | ||
107 | |||
108 | if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ) | ||
109 | return true; | ||
110 | |||
111 | ret = tps80031_read(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg, | ||
112 | ®_val); | ||
113 | if (ret < 0) { | ||
114 | dev_err(&rdev->dev, "Reg 0x%02x read failed, err = %d\n", | ||
115 | ri->rinfo->state_reg, ret); | ||
116 | return ret; | ||
117 | } | ||
118 | return ((reg_val & TPS80031_STATE_MASK) == TPS80031_STATE_ON); | ||
119 | } | ||
120 | |||
121 | static int tps80031_reg_enable(struct regulator_dev *rdev) | ||
122 | { | ||
123 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
124 | struct device *parent = to_tps80031_dev(rdev); | ||
125 | int ret; | ||
126 | |||
127 | if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ) | ||
128 | return 0; | ||
129 | |||
130 | ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg, | ||
131 | TPS80031_STATE_ON, TPS80031_STATE_MASK); | ||
132 | if (ret < 0) { | ||
133 | dev_err(&rdev->dev, "Reg 0x%02x update failed, err = %d\n", | ||
134 | ri->rinfo->state_reg, ret); | ||
135 | return ret; | ||
136 | } | ||
137 | return ret; | ||
138 | } | ||
139 | |||
140 | static int tps80031_reg_disable(struct regulator_dev *rdev) | ||
141 | { | ||
142 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
143 | struct device *parent = to_tps80031_dev(rdev); | ||
144 | int ret; | ||
145 | |||
146 | if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ) | ||
147 | return 0; | ||
148 | |||
149 | ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg, | ||
150 | TPS80031_STATE_OFF, TPS80031_STATE_MASK); | ||
151 | if (ret < 0) | ||
152 | dev_err(&rdev->dev, "Reg 0x%02x update failed, err = %d\n", | ||
153 | ri->rinfo->state_reg, ret); | ||
154 | return ret; | ||
155 | } | ||
156 | |||
157 | /* DCDC voltages for the selector of 58 to 63 */ | ||
158 | static int tps80031_dcdc_voltages[4][5] = { | ||
159 | { 1350, 1500, 1800, 1900, 2100}, | ||
160 | { 1350, 1500, 1800, 1900, 2100}, | ||
161 | { 2084, 2315, 2778, 2932, 3241}, | ||
162 | { 4167, 2315, 2778, 2932, 3241}, | ||
163 | }; | ||
164 | |||
165 | static int tps80031_dcdc_list_voltage(struct regulator_dev *rdev, unsigned sel) | ||
166 | { | ||
167 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
168 | int volt_index = ri->device_flags & 0x3; | ||
169 | |||
170 | if (sel == 0) | ||
171 | return 0; | ||
172 | else if (sel < 58) | ||
173 | return regulator_list_voltage_linear(rdev, sel - 1); | ||
174 | else | ||
175 | return tps80031_dcdc_voltages[volt_index][sel - 58] * 1000; | ||
176 | } | ||
177 | |||
178 | static int tps80031_dcdc_set_voltage_sel(struct regulator_dev *rdev, | ||
179 | unsigned vsel) | ||
180 | { | ||
181 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
182 | struct device *parent = to_tps80031_dev(rdev); | ||
183 | int ret; | ||
184 | u8 reg_val; | ||
185 | |||
186 | if (ri->rinfo->force_reg) { | ||
187 | ret = tps80031_read(parent, ri->rinfo->volt_id, | ||
188 | ri->rinfo->force_reg, ®_val); | ||
189 | if (ret < 0) { | ||
190 | dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n", | ||
191 | ri->rinfo->force_reg, ret); | ||
192 | return ret; | ||
193 | } | ||
194 | if (!(reg_val & SMPS_CMD_MASK)) { | ||
195 | ret = tps80031_update(parent, ri->rinfo->volt_id, | ||
196 | ri->rinfo->force_reg, vsel, SMPS_VSEL_MASK); | ||
197 | if (ret < 0) | ||
198 | dev_err(ri->dev, | ||
199 | "reg 0x%02x update failed, e = %d\n", | ||
200 | ri->rinfo->force_reg, ret); | ||
201 | return ret; | ||
202 | } | ||
203 | } | ||
204 | ret = tps80031_update(parent, ri->rinfo->volt_id, | ||
205 | ri->rinfo->volt_reg, vsel, SMPS_VSEL_MASK); | ||
206 | if (ret < 0) | ||
207 | dev_err(ri->dev, "reg 0x%02x update failed, e = %d\n", | ||
208 | ri->rinfo->volt_reg, ret); | ||
209 | return ret; | ||
210 | } | ||
211 | |||
212 | static int tps80031_dcdc_get_voltage_sel(struct regulator_dev *rdev) | ||
213 | { | ||
214 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
215 | struct device *parent = to_tps80031_dev(rdev); | ||
216 | uint8_t vsel = 0; | ||
217 | int ret; | ||
218 | |||
219 | if (ri->rinfo->force_reg) { | ||
220 | ret = tps80031_read(parent, ri->rinfo->volt_id, | ||
221 | ri->rinfo->force_reg, &vsel); | ||
222 | if (ret < 0) { | ||
223 | dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n", | ||
224 | ri->rinfo->force_reg, ret); | ||
225 | return ret; | ||
226 | } | ||
227 | |||
228 | if (!(vsel & SMPS_CMD_MASK)) | ||
229 | return vsel & SMPS_VSEL_MASK; | ||
230 | } | ||
231 | ret = tps80031_read(parent, ri->rinfo->volt_id, | ||
232 | ri->rinfo->volt_reg, &vsel); | ||
233 | if (ret < 0) { | ||
234 | dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n", | ||
235 | ri->rinfo->volt_reg, ret); | ||
236 | return ret; | ||
237 | } | ||
238 | return vsel & SMPS_VSEL_MASK; | ||
239 | } | ||
240 | |||
241 | static int tps80031_ldo_set_voltage_sel(struct regulator_dev *rdev, | ||
242 | unsigned sel) | ||
243 | { | ||
244 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
245 | struct device *parent = to_tps80031_dev(rdev); | ||
246 | int ret; | ||
247 | |||
248 | /* Check for valid setting for TPS80031 or TPS80032-ES1.0 */ | ||
249 | if ((ri->rinfo->desc.id == TPS80031_REGULATOR_LDO2) && | ||
250 | (ri->device_flags & TRACK_MODE_ENABLE)) { | ||
251 | unsigned nvsel = (sel) & 0x1F; | ||
252 | if (((tps80031_get_chip_info(parent) == TPS80031) || | ||
253 | ((tps80031_get_chip_info(parent) == TPS80032) && | ||
254 | (tps80031_get_pmu_version(parent) == 0x0))) && | ||
255 | ((nvsel == 0x0) || (nvsel >= 0x19 && nvsel <= 0x1F))) { | ||
256 | dev_err(ri->dev, | ||
257 | "Invalid sel %d in track mode LDO2\n", | ||
258 | nvsel); | ||
259 | return -EINVAL; | ||
260 | } | ||
261 | } | ||
262 | |||
263 | ret = tps80031_write(parent, ri->rinfo->volt_id, | ||
264 | ri->rinfo->volt_reg, sel); | ||
265 | if (ret < 0) | ||
266 | dev_err(ri->dev, "Error in writing reg 0x%02x, e = %d\n", | ||
267 | ri->rinfo->volt_reg, ret); | ||
268 | return ret; | ||
269 | } | ||
270 | |||
271 | static int tps80031_ldo_get_voltage_sel(struct regulator_dev *rdev) | ||
272 | { | ||
273 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
274 | struct device *parent = to_tps80031_dev(rdev); | ||
275 | uint8_t vsel; | ||
276 | int ret; | ||
277 | |||
278 | ret = tps80031_read(parent, ri->rinfo->volt_id, | ||
279 | ri->rinfo->volt_reg, &vsel); | ||
280 | if (ret < 0) { | ||
281 | dev_err(ri->dev, "Error in writing the Voltage register\n"); | ||
282 | return ret; | ||
283 | } | ||
284 | return vsel & rdev->desc->vsel_mask; | ||
285 | } | ||
286 | |||
287 | static int tps80031_ldo_list_voltage(struct regulator_dev *rdev, unsigned sel) | ||
288 | { | ||
289 | if (sel == 0) | ||
290 | return 0; | ||
291 | else | ||
292 | return regulator_list_voltage_linear(rdev, sel - 1); | ||
293 | } | ||
294 | |||
295 | static int tps80031_vbus_is_enabled(struct regulator_dev *rdev) | ||
296 | { | ||
297 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
298 | struct device *parent = to_tps80031_dev(rdev); | ||
299 | int ret = -EIO; | ||
300 | uint8_t ctrl1 = 0; | ||
301 | uint8_t ctrl3 = 0; | ||
302 | |||
303 | ret = tps80031_read(parent, TPS80031_SLAVE_ID2, | ||
304 | TPS80031_CHARGERUSB_CTRL1, &ctrl1); | ||
305 | if (ret < 0) { | ||
306 | dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n", | ||
307 | TPS80031_CHARGERUSB_CTRL1, ret); | ||
308 | return ret; | ||
309 | } | ||
310 | ret = tps80031_read(parent, TPS80031_SLAVE_ID2, | ||
311 | TPS80031_CHARGERUSB_CTRL3, &ctrl3); | ||
312 | if (ret < 0) { | ||
313 | dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n", | ||
314 | TPS80031_CHARGERUSB_CTRL1, ret); | ||
315 | return ret; | ||
316 | } | ||
317 | if ((ctrl1 & OPA_MODE_EN) && (ctrl3 & BOOST_HW_PWR_EN)) | ||
318 | return 1; | ||
319 | return ret; | ||
320 | } | ||
321 | |||
322 | static int tps80031_vbus_enable(struct regulator_dev *rdev) | ||
323 | { | ||
324 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
325 | struct device *parent = to_tps80031_dev(rdev); | ||
326 | int ret; | ||
327 | |||
328 | ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2, | ||
329 | TPS80031_CHARGERUSB_CTRL1, OPA_MODE_EN); | ||
330 | if (ret < 0) { | ||
331 | dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n", | ||
332 | TPS80031_CHARGERUSB_CTRL1, ret); | ||
333 | return ret; | ||
334 | } | ||
335 | |||
336 | ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2, | ||
337 | TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN); | ||
338 | if (ret < 0) { | ||
339 | dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n", | ||
340 | TPS80031_CHARGERUSB_CTRL3, ret); | ||
341 | return ret; | ||
342 | } | ||
343 | return ret; | ||
344 | } | ||
345 | |||
346 | static int tps80031_vbus_disable(struct regulator_dev *rdev) | ||
347 | { | ||
348 | struct tps80031_regulator *ri = rdev_get_drvdata(rdev); | ||
349 | struct device *parent = to_tps80031_dev(rdev); | ||
350 | int ret = 0; | ||
351 | |||
352 | if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) { | ||
353 | ret = tps80031_write(parent, TPS80031_SLAVE_ID2, | ||
354 | USB_VBUS_CTRL_SET, VBUS_DISCHRG); | ||
355 | if (ret < 0) { | ||
356 | dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n", | ||
357 | USB_VBUS_CTRL_SET, ret); | ||
358 | return ret; | ||
359 | } | ||
360 | } | ||
361 | |||
362 | ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2, | ||
363 | TPS80031_CHARGERUSB_CTRL1, OPA_MODE_EN); | ||
364 | if (ret < 0) { | ||
365 | dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n", | ||
366 | TPS80031_CHARGERUSB_CTRL1, ret); | ||
367 | return ret; | ||
368 | } | ||
369 | |||
370 | ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2, | ||
371 | TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN); | ||
372 | if (ret < 0) { | ||
373 | dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n", | ||
374 | TPS80031_CHARGERUSB_CTRL3, ret); | ||
375 | return ret; | ||
376 | } | ||
377 | |||
378 | mdelay(DIV_ROUND_UP(ri->rinfo->desc.enable_time, 1000)); | ||
379 | if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) { | ||
380 | ret = tps80031_write(parent, TPS80031_SLAVE_ID2, | ||
381 | USB_VBUS_CTRL_CLR, VBUS_DISCHRG); | ||
382 | if (ret < 0) { | ||
383 | dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n", | ||
384 | USB_VBUS_CTRL_CLR, ret); | ||
385 | return ret; | ||
386 | } | ||
387 | } | ||
388 | return ret; | ||
389 | } | ||
390 | |||
391 | static struct regulator_ops tps80031_dcdc_ops = { | ||
392 | .list_voltage = tps80031_dcdc_list_voltage, | ||
393 | .set_voltage_sel = tps80031_dcdc_set_voltage_sel, | ||
394 | .get_voltage_sel = tps80031_dcdc_get_voltage_sel, | ||
395 | .enable = tps80031_reg_enable, | ||
396 | .disable = tps80031_reg_disable, | ||
397 | .is_enabled = tps80031_reg_is_enabled, | ||
398 | }; | ||
399 | |||
400 | static struct regulator_ops tps80031_ldo_ops = { | ||
401 | .list_voltage = tps80031_ldo_list_voltage, | ||
402 | .set_voltage_sel = tps80031_ldo_set_voltage_sel, | ||
403 | .get_voltage_sel = tps80031_ldo_get_voltage_sel, | ||
404 | .enable = tps80031_reg_enable, | ||
405 | .disable = tps80031_reg_disable, | ||
406 | .is_enabled = tps80031_reg_is_enabled, | ||
407 | }; | ||
408 | |||
409 | static struct regulator_ops tps80031_vbus_sw_ops = { | ||
410 | .enable = tps80031_vbus_enable, | ||
411 | .disable = tps80031_vbus_disable, | ||
412 | .is_enabled = tps80031_vbus_is_enabled, | ||
413 | }; | ||
414 | |||
415 | static struct regulator_ops tps80031_vbus_hw_ops = { | ||
416 | }; | ||
417 | |||
418 | static struct regulator_ops tps80031_ext_reg_ops = { | ||
419 | .enable = tps80031_reg_enable, | ||
420 | .disable = tps80031_reg_disable, | ||
421 | .is_enabled = tps80031_reg_is_enabled, | ||
422 | }; | ||
423 | |||
424 | /* Non-exiting default definition for some register */ | ||
425 | #define TPS80031_SMPS3_CFG_FORCE 0 | ||
426 | #define TPS80031_SMPS4_CFG_FORCE 0 | ||
427 | |||
428 | #define TPS80031_VBUS_CFG_TRANS 0 | ||
429 | #define TPS80031_VBUS_CFG_STATE 0 | ||
430 | |||
431 | #define TPS80031_REG_SMPS(_id, _volt_id, _pbit) \ | ||
432 | { \ | ||
433 | .trans_reg = TPS80031_##_id##_CFG_TRANS, \ | ||
434 | .state_reg = TPS80031_##_id##_CFG_STATE, \ | ||
435 | .force_reg = TPS80031_##_id##_CFG_FORCE, \ | ||
436 | .volt_reg = TPS80031_##_id##_CFG_VOLTAGE, \ | ||
437 | .volt_id = TPS80031_SLAVE_##_volt_id, \ | ||
438 | .preq_bit = _pbit, \ | ||
439 | .desc = { \ | ||
440 | .name = "tps80031_"#_id, \ | ||
441 | .id = TPS80031_REGULATOR_##_id, \ | ||
442 | .n_voltages = 63, \ | ||
443 | .ops = &tps80031_dcdc_ops, \ | ||
444 | .type = REGULATOR_VOLTAGE, \ | ||
445 | .owner = THIS_MODULE, \ | ||
446 | .enable_time = 500, \ | ||
447 | }, \ | ||
448 | } | ||
449 | |||
450 | #define TPS80031_REG_LDO(_id, _preq_bit) \ | ||
451 | { \ | ||
452 | .trans_reg = TPS80031_##_id##_CFG_TRANS, \ | ||
453 | .state_reg = TPS80031_##_id##_CFG_STATE, \ | ||
454 | .volt_reg = TPS80031_##_id##_CFG_VOLTAGE, \ | ||
455 | .volt_id = TPS80031_SLAVE_ID1, \ | ||
456 | .preq_bit = _preq_bit, \ | ||
457 | .desc = { \ | ||
458 | .owner = THIS_MODULE, \ | ||
459 | .name = "tps80031_"#_id, \ | ||
460 | .id = TPS80031_REGULATOR_##_id, \ | ||
461 | .ops = &tps80031_ldo_ops, \ | ||
462 | .type = REGULATOR_VOLTAGE, \ | ||
463 | .min_uV = 1000000, \ | ||
464 | .uV_step = 100000, \ | ||
465 | .n_voltages = 25, \ | ||
466 | .vsel_mask = LDO_VSEL_MASK, \ | ||
467 | .enable_time = 500, \ | ||
468 | }, \ | ||
469 | } | ||
470 | |||
471 | #define TPS80031_REG_FIXED(_id, max_mV, _ops, _delay, _pbit) \ | ||
472 | { \ | ||
473 | .trans_reg = TPS80031_##_id##_CFG_TRANS, \ | ||
474 | .state_reg = TPS80031_##_id##_CFG_STATE, \ | ||
475 | .volt_id = TPS80031_SLAVE_ID1, \ | ||
476 | .preq_bit = _pbit, \ | ||
477 | .desc = { \ | ||
478 | .name = "tps80031_"#_id, \ | ||
479 | .id = TPS80031_REGULATOR_##_id, \ | ||
480 | .n_voltages = 2, \ | ||
481 | .ops = &_ops, \ | ||
482 | .type = REGULATOR_VOLTAGE, \ | ||
483 | .owner = THIS_MODULE, \ | ||
484 | .enable_time = _delay, \ | ||
485 | }, \ | ||
486 | } | ||
487 | |||
488 | static struct tps80031_regulator_info tps80031_rinfo[TPS80031_REGULATOR_MAX] = { | ||
489 | TPS80031_REG_SMPS(VIO, ID0, 4), | ||
490 | TPS80031_REG_SMPS(SMPS1, ID0, 0), | ||
491 | TPS80031_REG_SMPS(SMPS2, ID0, 1), | ||
492 | TPS80031_REG_SMPS(SMPS3, ID1, 2), | ||
493 | TPS80031_REG_SMPS(SMPS4, ID1, 3), | ||
494 | TPS80031_REG_LDO(VANA, -1), | ||
495 | TPS80031_REG_LDO(LDO1, 8), | ||
496 | TPS80031_REG_LDO(LDO2, 9), | ||
497 | TPS80031_REG_LDO(LDO3, 10), | ||
498 | TPS80031_REG_LDO(LDO4, 11), | ||
499 | TPS80031_REG_LDO(LDO5, 12), | ||
500 | TPS80031_REG_LDO(LDO6, 13), | ||
501 | TPS80031_REG_LDO(LDO7, 14), | ||
502 | TPS80031_REG_LDO(LDOLN, 15), | ||
503 | TPS80031_REG_LDO(LDOUSB, 5), | ||
504 | TPS80031_REG_FIXED(VBUS, 5000, tps80031_vbus_hw_ops, 100000, -1), | ||
505 | TPS80031_REG_FIXED(REGEN1, 3300, tps80031_ext_reg_ops, 0, 16), | ||
506 | TPS80031_REG_FIXED(REGEN2, 3300, tps80031_ext_reg_ops, 0, 17), | ||
507 | TPS80031_REG_FIXED(SYSEN, 3300, tps80031_ext_reg_ops, 0, 18), | ||
508 | }; | ||
509 | |||
510 | static int tps80031_power_req_config(struct device *parent, | ||
511 | struct tps80031_regulator *ri, | ||
512 | struct tps80031_regulator_platform_data *tps80031_pdata) | ||
513 | { | ||
514 | int ret = 0; | ||
515 | |||
516 | if (ri->rinfo->preq_bit < 0) | ||
517 | goto skip_pwr_req_config; | ||
518 | |||
519 | ret = tps80031_ext_power_req_config(parent, ri->ext_ctrl_flag, | ||
520 | ri->rinfo->preq_bit, ri->rinfo->state_reg, | ||
521 | ri->rinfo->trans_reg); | ||
522 | if (ret < 0) { | ||
523 | dev_err(ri->dev, "ext powerreq config failed, err = %d\n", ret); | ||
524 | return ret; | ||
525 | } | ||
526 | |||
527 | skip_pwr_req_config: | ||
528 | if (tps80031_pdata->ext_ctrl_flag & TPS80031_PWR_ON_ON_SLEEP) { | ||
529 | ret = tps80031_update(parent, TPS80031_SLAVE_ID1, | ||
530 | ri->rinfo->trans_reg, TPS80031_TRANS_SLEEP_ON, | ||
531 | TPS80031_TRANS_SLEEP_MASK); | ||
532 | if (ret < 0) { | ||
533 | dev_err(ri->dev, "Reg 0x%02x update failed, e %d\n", | ||
534 | ri->rinfo->trans_reg, ret); | ||
535 | return ret; | ||
536 | } | ||
537 | } | ||
538 | return ret; | ||
539 | } | ||
540 | |||
541 | static int tps80031_regulator_config(struct device *parent, | ||
542 | struct tps80031_regulator *ri, | ||
543 | struct tps80031_regulator_platform_data *tps80031_pdata) | ||
544 | { | ||
545 | int ret = 0; | ||
546 | |||
547 | switch (ri->rinfo->desc.id) { | ||
548 | case TPS80031_REGULATOR_LDOUSB: | ||
549 | if (ri->config_flags & (TPS80031_USBLDO_INPUT_VSYS | | ||
550 | TPS80031_USBLDO_INPUT_PMID)) { | ||
551 | unsigned val = 0; | ||
552 | if (ri->config_flags & TPS80031_USBLDO_INPUT_VSYS) | ||
553 | val = MISC2_LDOUSB_IN_VSYS; | ||
554 | else | ||
555 | val = MISC2_LDOUSB_IN_PMID; | ||
556 | |||
557 | ret = tps80031_update(parent, TPS80031_SLAVE_ID1, | ||
558 | TPS80031_MISC2, val, | ||
559 | MISC2_LDOUSB_IN_MASK); | ||
560 | if (ret < 0) { | ||
561 | dev_err(ri->dev, | ||
562 | "LDOUSB config failed, e= %d\n", ret); | ||
563 | return ret; | ||
564 | } | ||
565 | } | ||
566 | break; | ||
567 | |||
568 | case TPS80031_REGULATOR_LDO3: | ||
569 | if (ri->config_flags & TPS80031_LDO3_OUTPUT_VIB) { | ||
570 | ret = tps80031_update(parent, TPS80031_SLAVE_ID1, | ||
571 | TPS80031_MISC2, MISC2_LDO3_SEL_VIB_VAL, | ||
572 | MISC2_LDO3_SEL_VIB_MASK); | ||
573 | if (ret < 0) { | ||
574 | dev_err(ri->dev, | ||
575 | "LDO3 config failed, e = %d\n", ret); | ||
576 | return ret; | ||
577 | } | ||
578 | } | ||
579 | break; | ||
580 | |||
581 | case TPS80031_REGULATOR_VBUS: | ||
582 | /* Provide SW control Ops if VBUS is SW control */ | ||
583 | if (!(ri->config_flags & TPS80031_VBUS_SW_ONLY)) | ||
584 | ri->rinfo->desc.ops = &tps80031_vbus_sw_ops; | ||
585 | break; | ||
586 | default: | ||
587 | break; | ||
588 | } | ||
589 | |||
590 | /* Configure Active state to ON, SLEEP to OFF and OFF_state to OFF */ | ||
591 | ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->trans_reg, | ||
592 | TPS80031_TRANS_ACTIVE_ON | TPS80031_TRANS_SLEEP_OFF | | ||
593 | TPS80031_TRANS_OFF_OFF, TPS80031_TRANS_ACTIVE_MASK | | ||
594 | TPS80031_TRANS_SLEEP_MASK | TPS80031_TRANS_OFF_MASK); | ||
595 | if (ret < 0) { | ||
596 | dev_err(ri->dev, "trans reg update failed, e %d\n", ret); | ||
597 | return ret; | ||
598 | } | ||
599 | |||
600 | return ret; | ||
601 | } | ||
602 | |||
603 | static int check_smps_mode_mult(struct device *parent, | ||
604 | struct tps80031_regulator *ri) | ||
605 | { | ||
606 | int mult_offset; | ||
607 | int ret; | ||
608 | u8 smps_offset; | ||
609 | u8 smps_mult; | ||
610 | |||
611 | ret = tps80031_read(parent, TPS80031_SLAVE_ID1, | ||
612 | TPS80031_SMPS_OFFSET, &smps_offset); | ||
613 | if (ret < 0) { | ||
614 | dev_err(parent, "Error in reading smps offset register\n"); | ||
615 | return ret; | ||
616 | } | ||
617 | |||
618 | ret = tps80031_read(parent, TPS80031_SLAVE_ID1, | ||
619 | TPS80031_SMPS_MULT, &smps_mult); | ||
620 | if (ret < 0) { | ||
621 | dev_err(parent, "Error in reading smps mult register\n"); | ||
622 | return ret; | ||
623 | } | ||
624 | |||
625 | switch (ri->rinfo->desc.id) { | ||
626 | case TPS80031_REGULATOR_VIO: | ||
627 | mult_offset = SMPS_MULTOFFSET_VIO; | ||
628 | break; | ||
629 | case TPS80031_REGULATOR_SMPS1: | ||
630 | mult_offset = SMPS_MULTOFFSET_SMPS1; | ||
631 | break; | ||
632 | case TPS80031_REGULATOR_SMPS2: | ||
633 | mult_offset = SMPS_MULTOFFSET_SMPS2; | ||
634 | break; | ||
635 | case TPS80031_REGULATOR_SMPS3: | ||
636 | mult_offset = SMPS_MULTOFFSET_SMPS3; | ||
637 | break; | ||
638 | case TPS80031_REGULATOR_SMPS4: | ||
639 | mult_offset = SMPS_MULTOFFSET_SMPS4; | ||
640 | break; | ||
641 | case TPS80031_REGULATOR_LDO2: | ||
642 | ri->device_flags = smps_mult & BIT(5) ? TRACK_MODE_ENABLE : 0; | ||
643 | /* TRACK mode the ldo2 varies from 600mV to 1300mV */ | ||
644 | if (ri->device_flags & TRACK_MODE_ENABLE) { | ||
645 | ri->rinfo->desc.min_uV = 600000; | ||
646 | ri->rinfo->desc.uV_step = 12500; | ||
647 | ri->rinfo->desc.n_voltages = 57; | ||
648 | ri->rinfo->desc.vsel_mask = LDO_TRACK_VSEL_MASK; | ||
649 | } | ||
650 | return 0; | ||
651 | default: | ||
652 | return 0; | ||
653 | } | ||
654 | |||
655 | ri->device_flags = (smps_offset & mult_offset) ? DCDC_OFFSET_EN : 0; | ||
656 | ri->device_flags |= (smps_mult & mult_offset) ? DCDC_EXTENDED_EN : 0; | ||
657 | switch (ri->device_flags) { | ||
658 | case 0: | ||
659 | ri->rinfo->desc.min_uV = 607700; | ||
660 | ri->rinfo->desc.uV_step = 12660; | ||
661 | break; | ||
662 | case DCDC_OFFSET_EN: | ||
663 | ri->rinfo->desc.min_uV = 700000; | ||
664 | ri->rinfo->desc.uV_step = 12500; | ||
665 | break; | ||
666 | case DCDC_EXTENDED_EN: | ||
667 | ri->rinfo->desc.min_uV = 1852000; | ||
668 | ri->rinfo->desc.uV_step = 38600; | ||
669 | break; | ||
670 | case DCDC_OFFSET_EN | DCDC_EXTENDED_EN: | ||
671 | ri->rinfo->desc.min_uV = 2161000; | ||
672 | ri->rinfo->desc.uV_step = 38600; | ||
673 | break; | ||
674 | } | ||
675 | return 0; | ||
676 | } | ||
677 | |||
678 | static int __devinit tps80031_regulator_probe(struct platform_device *pdev) | ||
679 | { | ||
680 | struct tps80031_platform_data *pdata; | ||
681 | struct tps80031_regulator_platform_data *tps_pdata; | ||
682 | struct tps80031_regulator_info *rinfo; | ||
683 | struct tps80031_regulator *ri; | ||
684 | struct tps80031_regulator *pmic; | ||
685 | struct regulator_dev *rdev; | ||
686 | struct regulator_config config = { }; | ||
687 | int ret; | ||
688 | int num; | ||
689 | |||
690 | pdata = dev_get_platdata(pdev->dev.parent); | ||
691 | |||
692 | if (!pdata) { | ||
693 | dev_err(&pdev->dev, "No platform data\n"); | ||
694 | return -EINVAL; | ||
695 | } | ||
696 | |||
697 | pmic = devm_kzalloc(&pdev->dev, | ||
698 | TPS80031_REGULATOR_MAX * sizeof(*pmic), GFP_KERNEL); | ||
699 | if (!pmic) { | ||
700 | dev_err(&pdev->dev, "mem alloc for pmic failed\n"); | ||
701 | return -ENOMEM; | ||
702 | } | ||
703 | |||
704 | for (num = 0; num < TPS80031_REGULATOR_MAX; ++num) { | ||
705 | tps_pdata = pdata->regulator_pdata[num]; | ||
706 | rinfo = &tps80031_rinfo[num]; | ||
707 | ri = &pmic[num]; | ||
708 | ri->rinfo = rinfo; | ||
709 | ri->dev = &pdev->dev; | ||
710 | |||
711 | check_smps_mode_mult(pdev->dev.parent, ri); | ||
712 | config.dev = &pdev->dev; | ||
713 | config.init_data = NULL; | ||
714 | config.driver_data = ri; | ||
715 | if (tps_pdata) { | ||
716 | config.init_data = tps_pdata->reg_init_data; | ||
717 | ri->config_flags = tps_pdata->config_flags; | ||
718 | ri->ext_ctrl_flag = tps_pdata->ext_ctrl_flag; | ||
719 | ret = tps80031_regulator_config(pdev->dev.parent, | ||
720 | ri, tps_pdata); | ||
721 | if (ret < 0) { | ||
722 | dev_err(&pdev->dev, | ||
723 | "regulator config failed, e %d\n", ret); | ||
724 | goto fail; | ||
725 | } | ||
726 | |||
727 | ret = tps80031_power_req_config(pdev->dev.parent, | ||
728 | ri, tps_pdata); | ||
729 | if (ret < 0) { | ||
730 | dev_err(&pdev->dev, | ||
731 | "pwr_req config failed, err %d\n", ret); | ||
732 | goto fail; | ||
733 | } | ||
734 | } | ||
735 | rdev = regulator_register(&ri->rinfo->desc, &config); | ||
736 | if (IS_ERR_OR_NULL(rdev)) { | ||
737 | dev_err(&pdev->dev, | ||
738 | "register regulator failed %s\n", | ||
739 | ri->rinfo->desc.name); | ||
740 | ret = PTR_ERR(rdev); | ||
741 | goto fail; | ||
742 | } | ||
743 | ri->rdev = rdev; | ||
744 | } | ||
745 | |||
746 | platform_set_drvdata(pdev, pmic); | ||
747 | return 0; | ||
748 | fail: | ||
749 | while (--num >= 0) { | ||
750 | ri = &pmic[num]; | ||
751 | regulator_unregister(ri->rdev); | ||
752 | } | ||
753 | return ret; | ||
754 | } | ||
755 | |||
756 | static int __devexit tps80031_regulator_remove(struct platform_device *pdev) | ||
757 | { | ||
758 | struct tps80031_regulator *pmic = platform_get_drvdata(pdev); | ||
759 | struct tps80031_regulator *ri = NULL; | ||
760 | int num; | ||
761 | |||
762 | for (num = 0; num < TPS80031_REGULATOR_MAX; ++num) { | ||
763 | ri = &pmic[num]; | ||
764 | regulator_unregister(ri->rdev); | ||
765 | } | ||
766 | return 0; | ||
767 | } | ||
768 | |||
769 | static struct platform_driver tps80031_regulator_driver = { | ||
770 | .driver = { | ||
771 | .name = "tps80031-pmic", | ||
772 | .owner = THIS_MODULE, | ||
773 | }, | ||
774 | .probe = tps80031_regulator_probe, | ||
775 | .remove = __devexit_p(tps80031_regulator_remove), | ||
776 | }; | ||
777 | |||
778 | static int __init tps80031_regulator_init(void) | ||
779 | { | ||
780 | return platform_driver_register(&tps80031_regulator_driver); | ||
781 | } | ||
782 | subsys_initcall(tps80031_regulator_init); | ||
783 | |||
784 | static void __exit tps80031_regulator_exit(void) | ||
785 | { | ||
786 | platform_driver_unregister(&tps80031_regulator_driver); | ||
787 | } | ||
788 | module_exit(tps80031_regulator_exit); | ||
789 | |||
790 | MODULE_ALIAS("platform:tps80031-regulator"); | ||
791 | MODULE_DESCRIPTION("Regulator Driver for TI TPS80031 PMIC"); | ||
792 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); | ||
793 | MODULE_LICENSE("GPL v2"); | ||