blob: 6430789b8347b1353be7ef79dbab10bb314ce7bc [file] [log] [blame]
Laxman Dewangan1a0bb672012-11-11 20:42:01 +05301/*
2 * tps80031-regulator.c -- TI TPS80031 regulator driver.
3 *
Axel Lin1a7ae582012-11-22 16:30:44 +08004 * Regulator driver for TI TPS80031/TPS80032 Fully Integrated Power
Laxman Dewangan1a0bb672012-11-11 20:42:01 +05305 * 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
70struct 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
86struct 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
96static inline struct device *to_tps80031_dev(struct regulator_dev *rdev)
97{
98 return rdev_get_dev(rdev)->parent->parent;
99}
100
101static 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
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530108 if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ)
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530109 return true;
110
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530111 ret = tps80031_read(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg,
112 &reg_val);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530113 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 }
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530118 return ((reg_val & TPS80031_STATE_MASK) == TPS80031_STATE_ON);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530119}
120
121static 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
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530127 if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ)
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530128 return 0;
129
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530130 ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg,
131 TPS80031_STATE_ON, TPS80031_STATE_MASK);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530132 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
140static 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
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530146 if (ri->ext_ctrl_flag & TPS80031_EXT_PWR_REQ)
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530147 return 0;
148
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530149 ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->state_reg,
150 TPS80031_STATE_OFF, TPS80031_STATE_MASK);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530151 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 */
158static 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
165static 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
178static 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, &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
212static 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
Axel Lin58fa6ab2013-04-17 21:42:23 +0800241static int tps80031_ldo_list_voltage(struct regulator_dev *rdev,
242 unsigned int sel)
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530243{
244 struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
245 struct device *parent = to_tps80031_dev(rdev);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530246
247 /* Check for valid setting for TPS80031 or TPS80032-ES1.0 */
248 if ((ri->rinfo->desc.id == TPS80031_REGULATOR_LDO2) &&
249 (ri->device_flags & TRACK_MODE_ENABLE)) {
250 unsigned nvsel = (sel) & 0x1F;
251 if (((tps80031_get_chip_info(parent) == TPS80031) ||
252 ((tps80031_get_chip_info(parent) == TPS80032) &&
253 (tps80031_get_pmu_version(parent) == 0x0))) &&
254 ((nvsel == 0x0) || (nvsel >= 0x19 && nvsel <= 0x1F))) {
255 dev_err(ri->dev,
256 "Invalid sel %d in track mode LDO2\n",
257 nvsel);
258 return -EINVAL;
259 }
260 }
261
Axel Lin58fa6ab2013-04-17 21:42:23 +0800262 return regulator_list_voltage_linear(rdev, sel);
263}
264
265static int tps80031_ldo_map_voltage(struct regulator_dev *rdev,
266 int min_uV, int max_uV)
267{
268 struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
269 struct device *parent = to_tps80031_dev(rdev);
270
271 /* Check for valid setting for TPS80031 or TPS80032-ES1.0 */
272 if ((ri->rinfo->desc.id == TPS80031_REGULATOR_LDO2) &&
273 (ri->device_flags & TRACK_MODE_ENABLE)) {
274 if (((tps80031_get_chip_info(parent) == TPS80031) ||
275 ((tps80031_get_chip_info(parent) == TPS80032) &&
276 (tps80031_get_pmu_version(parent) == 0x0)))) {
277 return regulator_map_voltage_iterate(rdev, min_uV,
278 max_uV);
279 }
280 }
281
282 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
283}
284
285static int tps80031_ldo_set_voltage_sel(struct regulator_dev *rdev,
286 unsigned sel)
287{
288 struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
289 struct device *parent = to_tps80031_dev(rdev);
290 int ret;
291
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530292 ret = tps80031_write(parent, ri->rinfo->volt_id,
293 ri->rinfo->volt_reg, sel);
294 if (ret < 0)
295 dev_err(ri->dev, "Error in writing reg 0x%02x, e = %d\n",
296 ri->rinfo->volt_reg, ret);
297 return ret;
298}
299
300static int tps80031_ldo_get_voltage_sel(struct regulator_dev *rdev)
301{
302 struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
303 struct device *parent = to_tps80031_dev(rdev);
304 uint8_t vsel;
305 int ret;
306
307 ret = tps80031_read(parent, ri->rinfo->volt_id,
308 ri->rinfo->volt_reg, &vsel);
309 if (ret < 0) {
310 dev_err(ri->dev, "Error in writing the Voltage register\n");
311 return ret;
312 }
313 return vsel & rdev->desc->vsel_mask;
314}
315
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530316static int tps80031_vbus_is_enabled(struct regulator_dev *rdev)
317{
318 struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
319 struct device *parent = to_tps80031_dev(rdev);
320 int ret = -EIO;
321 uint8_t ctrl1 = 0;
322 uint8_t ctrl3 = 0;
323
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530324 ret = tps80031_read(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530325 TPS80031_CHARGERUSB_CTRL1, &ctrl1);
326 if (ret < 0) {
327 dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
328 TPS80031_CHARGERUSB_CTRL1, ret);
329 return ret;
330 }
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530331 ret = tps80031_read(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530332 TPS80031_CHARGERUSB_CTRL3, &ctrl3);
333 if (ret < 0) {
334 dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
Axel Lin1a7ae582012-11-22 16:30:44 +0800335 TPS80031_CHARGERUSB_CTRL3, ret);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530336 return ret;
337 }
338 if ((ctrl1 & OPA_MODE_EN) && (ctrl3 & BOOST_HW_PWR_EN))
339 return 1;
340 return ret;
341}
342
343static int tps80031_vbus_enable(struct regulator_dev *rdev)
344{
345 struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
346 struct device *parent = to_tps80031_dev(rdev);
347 int ret;
348
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530349 ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530350 TPS80031_CHARGERUSB_CTRL1, OPA_MODE_EN);
351 if (ret < 0) {
352 dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
353 TPS80031_CHARGERUSB_CTRL1, ret);
354 return ret;
355 }
356
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530357 ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530358 TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN);
359 if (ret < 0) {
360 dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
361 TPS80031_CHARGERUSB_CTRL3, ret);
362 return ret;
363 }
364 return ret;
365}
366
367static int tps80031_vbus_disable(struct regulator_dev *rdev)
368{
369 struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
370 struct device *parent = to_tps80031_dev(rdev);
371 int ret = 0;
372
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530373 if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) {
374 ret = tps80031_write(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530375 USB_VBUS_CTRL_SET, VBUS_DISCHRG);
376 if (ret < 0) {
377 dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n",
378 USB_VBUS_CTRL_SET, ret);
379 return ret;
380 }
381 }
382
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530383 ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530384 TPS80031_CHARGERUSB_CTRL1, OPA_MODE_EN);
385 if (ret < 0) {
386 dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n",
387 TPS80031_CHARGERUSB_CTRL1, ret);
388 return ret;
389 }
390
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530391 ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530392 TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN);
393 if (ret < 0) {
394 dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n",
395 TPS80031_CHARGERUSB_CTRL3, ret);
396 return ret;
397 }
398
399 mdelay(DIV_ROUND_UP(ri->rinfo->desc.enable_time, 1000));
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530400 if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) {
401 ret = tps80031_write(parent, TPS80031_SLAVE_ID2,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530402 USB_VBUS_CTRL_CLR, VBUS_DISCHRG);
403 if (ret < 0) {
404 dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n",
405 USB_VBUS_CTRL_CLR, ret);
406 return ret;
407 }
408 }
409 return ret;
410}
411
412static struct regulator_ops tps80031_dcdc_ops = {
413 .list_voltage = tps80031_dcdc_list_voltage,
414 .set_voltage_sel = tps80031_dcdc_set_voltage_sel,
415 .get_voltage_sel = tps80031_dcdc_get_voltage_sel,
416 .enable = tps80031_reg_enable,
417 .disable = tps80031_reg_disable,
418 .is_enabled = tps80031_reg_is_enabled,
419};
420
421static struct regulator_ops tps80031_ldo_ops = {
Axel Lin58fa6ab2013-04-17 21:42:23 +0800422 .list_voltage = tps80031_ldo_list_voltage,
423 .map_voltage = tps80031_ldo_map_voltage,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530424 .set_voltage_sel = tps80031_ldo_set_voltage_sel,
425 .get_voltage_sel = tps80031_ldo_get_voltage_sel,
426 .enable = tps80031_reg_enable,
427 .disable = tps80031_reg_disable,
428 .is_enabled = tps80031_reg_is_enabled,
429};
430
431static struct regulator_ops tps80031_vbus_sw_ops = {
Axel Linbf0caae2012-11-23 09:25:54 +0800432 .list_voltage = regulator_list_voltage_linear,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530433 .enable = tps80031_vbus_enable,
434 .disable = tps80031_vbus_disable,
435 .is_enabled = tps80031_vbus_is_enabled,
436};
437
438static struct regulator_ops tps80031_vbus_hw_ops = {
Axel Linbf0caae2012-11-23 09:25:54 +0800439 .list_voltage = regulator_list_voltage_linear,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530440};
441
442static struct regulator_ops tps80031_ext_reg_ops = {
Axel Linbf0caae2012-11-23 09:25:54 +0800443 .list_voltage = regulator_list_voltage_linear,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530444 .enable = tps80031_reg_enable,
445 .disable = tps80031_reg_disable,
446 .is_enabled = tps80031_reg_is_enabled,
447};
448
449/* Non-exiting default definition for some register */
450#define TPS80031_SMPS3_CFG_FORCE 0
451#define TPS80031_SMPS4_CFG_FORCE 0
452
453#define TPS80031_VBUS_CFG_TRANS 0
454#define TPS80031_VBUS_CFG_STATE 0
455
456#define TPS80031_REG_SMPS(_id, _volt_id, _pbit) \
457{ \
458 .trans_reg = TPS80031_##_id##_CFG_TRANS, \
459 .state_reg = TPS80031_##_id##_CFG_STATE, \
460 .force_reg = TPS80031_##_id##_CFG_FORCE, \
461 .volt_reg = TPS80031_##_id##_CFG_VOLTAGE, \
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530462 .volt_id = TPS80031_SLAVE_##_volt_id, \
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530463 .preq_bit = _pbit, \
464 .desc = { \
465 .name = "tps80031_"#_id, \
466 .id = TPS80031_REGULATOR_##_id, \
467 .n_voltages = 63, \
468 .ops = &tps80031_dcdc_ops, \
469 .type = REGULATOR_VOLTAGE, \
470 .owner = THIS_MODULE, \
471 .enable_time = 500, \
472 }, \
473}
474
475#define TPS80031_REG_LDO(_id, _preq_bit) \
476{ \
477 .trans_reg = TPS80031_##_id##_CFG_TRANS, \
478 .state_reg = TPS80031_##_id##_CFG_STATE, \
479 .volt_reg = TPS80031_##_id##_CFG_VOLTAGE, \
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530480 .volt_id = TPS80031_SLAVE_ID1, \
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530481 .preq_bit = _preq_bit, \
482 .desc = { \
483 .owner = THIS_MODULE, \
484 .name = "tps80031_"#_id, \
485 .id = TPS80031_REGULATOR_##_id, \
486 .ops = &tps80031_ldo_ops, \
487 .type = REGULATOR_VOLTAGE, \
488 .min_uV = 1000000, \
489 .uV_step = 100000, \
Axel Lin7fa8a592012-12-06 08:24:11 +0800490 .linear_min_sel = 1, \
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530491 .n_voltages = 25, \
492 .vsel_mask = LDO_VSEL_MASK, \
493 .enable_time = 500, \
494 }, \
495}
496
497#define TPS80031_REG_FIXED(_id, max_mV, _ops, _delay, _pbit) \
498{ \
499 .trans_reg = TPS80031_##_id##_CFG_TRANS, \
500 .state_reg = TPS80031_##_id##_CFG_STATE, \
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530501 .volt_id = TPS80031_SLAVE_ID1, \
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530502 .preq_bit = _pbit, \
503 .desc = { \
504 .name = "tps80031_"#_id, \
505 .id = TPS80031_REGULATOR_##_id, \
Axel Linbf0caae2012-11-23 09:25:54 +0800506 .min_uV = max_mV * 1000, \
507 .n_voltages = 1, \
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530508 .ops = &_ops, \
509 .type = REGULATOR_VOLTAGE, \
510 .owner = THIS_MODULE, \
511 .enable_time = _delay, \
512 }, \
513}
514
515static struct tps80031_regulator_info tps80031_rinfo[TPS80031_REGULATOR_MAX] = {
516 TPS80031_REG_SMPS(VIO, ID0, 4),
517 TPS80031_REG_SMPS(SMPS1, ID0, 0),
518 TPS80031_REG_SMPS(SMPS2, ID0, 1),
519 TPS80031_REG_SMPS(SMPS3, ID1, 2),
520 TPS80031_REG_SMPS(SMPS4, ID1, 3),
521 TPS80031_REG_LDO(VANA, -1),
522 TPS80031_REG_LDO(LDO1, 8),
523 TPS80031_REG_LDO(LDO2, 9),
524 TPS80031_REG_LDO(LDO3, 10),
525 TPS80031_REG_LDO(LDO4, 11),
526 TPS80031_REG_LDO(LDO5, 12),
527 TPS80031_REG_LDO(LDO6, 13),
528 TPS80031_REG_LDO(LDO7, 14),
529 TPS80031_REG_LDO(LDOLN, 15),
530 TPS80031_REG_LDO(LDOUSB, 5),
531 TPS80031_REG_FIXED(VBUS, 5000, tps80031_vbus_hw_ops, 100000, -1),
532 TPS80031_REG_FIXED(REGEN1, 3300, tps80031_ext_reg_ops, 0, 16),
533 TPS80031_REG_FIXED(REGEN2, 3300, tps80031_ext_reg_ops, 0, 17),
534 TPS80031_REG_FIXED(SYSEN, 3300, tps80031_ext_reg_ops, 0, 18),
535};
536
537static int tps80031_power_req_config(struct device *parent,
538 struct tps80031_regulator *ri,
539 struct tps80031_regulator_platform_data *tps80031_pdata)
540{
541 int ret = 0;
542
543 if (ri->rinfo->preq_bit < 0)
544 goto skip_pwr_req_config;
545
546 ret = tps80031_ext_power_req_config(parent, ri->ext_ctrl_flag,
547 ri->rinfo->preq_bit, ri->rinfo->state_reg,
548 ri->rinfo->trans_reg);
549 if (ret < 0) {
550 dev_err(ri->dev, "ext powerreq config failed, err = %d\n", ret);
551 return ret;
552 }
553
554skip_pwr_req_config:
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530555 if (tps80031_pdata->ext_ctrl_flag & TPS80031_PWR_ON_ON_SLEEP) {
556 ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
557 ri->rinfo->trans_reg, TPS80031_TRANS_SLEEP_ON,
558 TPS80031_TRANS_SLEEP_MASK);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530559 if (ret < 0) {
560 dev_err(ri->dev, "Reg 0x%02x update failed, e %d\n",
561 ri->rinfo->trans_reg, ret);
562 return ret;
563 }
564 }
565 return ret;
566}
567
568static int tps80031_regulator_config(struct device *parent,
569 struct tps80031_regulator *ri,
570 struct tps80031_regulator_platform_data *tps80031_pdata)
571{
572 int ret = 0;
573
574 switch (ri->rinfo->desc.id) {
575 case TPS80031_REGULATOR_LDOUSB:
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530576 if (ri->config_flags & (TPS80031_USBLDO_INPUT_VSYS |
577 TPS80031_USBLDO_INPUT_PMID)) {
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530578 unsigned val = 0;
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530579 if (ri->config_flags & TPS80031_USBLDO_INPUT_VSYS)
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530580 val = MISC2_LDOUSB_IN_VSYS;
581 else
582 val = MISC2_LDOUSB_IN_PMID;
583
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530584 ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
585 TPS80031_MISC2, val,
586 MISC2_LDOUSB_IN_MASK);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530587 if (ret < 0) {
588 dev_err(ri->dev,
589 "LDOUSB config failed, e= %d\n", ret);
590 return ret;
591 }
592 }
593 break;
594
595 case TPS80031_REGULATOR_LDO3:
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530596 if (ri->config_flags & TPS80031_LDO3_OUTPUT_VIB) {
597 ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530598 TPS80031_MISC2, MISC2_LDO3_SEL_VIB_VAL,
599 MISC2_LDO3_SEL_VIB_MASK);
600 if (ret < 0) {
601 dev_err(ri->dev,
602 "LDO3 config failed, e = %d\n", ret);
603 return ret;
604 }
605 }
606 break;
607
608 case TPS80031_REGULATOR_VBUS:
609 /* Provide SW control Ops if VBUS is SW control */
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530610 if (!(ri->config_flags & TPS80031_VBUS_SW_ONLY))
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530611 ri->rinfo->desc.ops = &tps80031_vbus_sw_ops;
612 break;
613 default:
614 break;
615 }
616
617 /* Configure Active state to ON, SLEEP to OFF and OFF_state to OFF */
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530618 ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->trans_reg,
619 TPS80031_TRANS_ACTIVE_ON | TPS80031_TRANS_SLEEP_OFF |
620 TPS80031_TRANS_OFF_OFF, TPS80031_TRANS_ACTIVE_MASK |
621 TPS80031_TRANS_SLEEP_MASK | TPS80031_TRANS_OFF_MASK);
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530622 if (ret < 0) {
623 dev_err(ri->dev, "trans reg update failed, e %d\n", ret);
624 return ret;
625 }
626
627 return ret;
628}
629
630static int check_smps_mode_mult(struct device *parent,
631 struct tps80031_regulator *ri)
632{
633 int mult_offset;
634 int ret;
635 u8 smps_offset;
636 u8 smps_mult;
637
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530638 ret = tps80031_read(parent, TPS80031_SLAVE_ID1,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530639 TPS80031_SMPS_OFFSET, &smps_offset);
640 if (ret < 0) {
641 dev_err(parent, "Error in reading smps offset register\n");
642 return ret;
643 }
644
Laxman Dewanganb92f7872012-11-14 21:09:29 +0530645 ret = tps80031_read(parent, TPS80031_SLAVE_ID1,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530646 TPS80031_SMPS_MULT, &smps_mult);
647 if (ret < 0) {
648 dev_err(parent, "Error in reading smps mult register\n");
649 return ret;
650 }
651
652 switch (ri->rinfo->desc.id) {
653 case TPS80031_REGULATOR_VIO:
654 mult_offset = SMPS_MULTOFFSET_VIO;
655 break;
656 case TPS80031_REGULATOR_SMPS1:
657 mult_offset = SMPS_MULTOFFSET_SMPS1;
658 break;
659 case TPS80031_REGULATOR_SMPS2:
660 mult_offset = SMPS_MULTOFFSET_SMPS2;
661 break;
662 case TPS80031_REGULATOR_SMPS3:
663 mult_offset = SMPS_MULTOFFSET_SMPS3;
664 break;
665 case TPS80031_REGULATOR_SMPS4:
666 mult_offset = SMPS_MULTOFFSET_SMPS4;
667 break;
668 case TPS80031_REGULATOR_LDO2:
669 ri->device_flags = smps_mult & BIT(5) ? TRACK_MODE_ENABLE : 0;
670 /* TRACK mode the ldo2 varies from 600mV to 1300mV */
671 if (ri->device_flags & TRACK_MODE_ENABLE) {
672 ri->rinfo->desc.min_uV = 600000;
673 ri->rinfo->desc.uV_step = 12500;
674 ri->rinfo->desc.n_voltages = 57;
675 ri->rinfo->desc.vsel_mask = LDO_TRACK_VSEL_MASK;
676 }
677 return 0;
678 default:
679 return 0;
680 }
681
682 ri->device_flags = (smps_offset & mult_offset) ? DCDC_OFFSET_EN : 0;
683 ri->device_flags |= (smps_mult & mult_offset) ? DCDC_EXTENDED_EN : 0;
684 switch (ri->device_flags) {
685 case 0:
686 ri->rinfo->desc.min_uV = 607700;
687 ri->rinfo->desc.uV_step = 12660;
688 break;
689 case DCDC_OFFSET_EN:
690 ri->rinfo->desc.min_uV = 700000;
691 ri->rinfo->desc.uV_step = 12500;
692 break;
693 case DCDC_EXTENDED_EN:
694 ri->rinfo->desc.min_uV = 1852000;
695 ri->rinfo->desc.uV_step = 38600;
696 break;
697 case DCDC_OFFSET_EN | DCDC_EXTENDED_EN:
698 ri->rinfo->desc.min_uV = 2161000;
699 ri->rinfo->desc.uV_step = 38600;
700 break;
701 }
702 return 0;
703}
704
Bill Pembertona5023572012-11-19 13:22:22 -0500705static int tps80031_regulator_probe(struct platform_device *pdev)
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530706{
707 struct tps80031_platform_data *pdata;
708 struct tps80031_regulator_platform_data *tps_pdata;
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530709 struct tps80031_regulator *ri;
710 struct tps80031_regulator *pmic;
711 struct regulator_dev *rdev;
712 struct regulator_config config = { };
713 int ret;
714 int num;
715
716 pdata = dev_get_platdata(pdev->dev.parent);
717
718 if (!pdata) {
719 dev_err(&pdev->dev, "No platform data\n");
720 return -EINVAL;
721 }
722
723 pmic = devm_kzalloc(&pdev->dev,
724 TPS80031_REGULATOR_MAX * sizeof(*pmic), GFP_KERNEL);
725 if (!pmic) {
726 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
727 return -ENOMEM;
728 }
729
730 for (num = 0; num < TPS80031_REGULATOR_MAX; ++num) {
731 tps_pdata = pdata->regulator_pdata[num];
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530732 ri = &pmic[num];
Axel Lin1a7ae582012-11-22 16:30:44 +0800733 ri->rinfo = &tps80031_rinfo[num];
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530734 ri->dev = &pdev->dev;
735
736 check_smps_mode_mult(pdev->dev.parent, ri);
737 config.dev = &pdev->dev;
738 config.init_data = NULL;
739 config.driver_data = ri;
740 if (tps_pdata) {
741 config.init_data = tps_pdata->reg_init_data;
742 ri->config_flags = tps_pdata->config_flags;
743 ri->ext_ctrl_flag = tps_pdata->ext_ctrl_flag;
744 ret = tps80031_regulator_config(pdev->dev.parent,
745 ri, tps_pdata);
746 if (ret < 0) {
747 dev_err(&pdev->dev,
748 "regulator config failed, e %d\n", ret);
749 goto fail;
750 }
751
752 ret = tps80031_power_req_config(pdev->dev.parent,
753 ri, tps_pdata);
754 if (ret < 0) {
755 dev_err(&pdev->dev,
756 "pwr_req config failed, err %d\n", ret);
757 goto fail;
758 }
759 }
760 rdev = regulator_register(&ri->rinfo->desc, &config);
Axel Lind4cbca92013-01-25 21:29:41 +0800761 if (IS_ERR(rdev)) {
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530762 dev_err(&pdev->dev,
763 "register regulator failed %s\n",
764 ri->rinfo->desc.name);
765 ret = PTR_ERR(rdev);
766 goto fail;
767 }
768 ri->rdev = rdev;
769 }
770
771 platform_set_drvdata(pdev, pmic);
772 return 0;
773fail:
774 while (--num >= 0) {
775 ri = &pmic[num];
776 regulator_unregister(ri->rdev);
777 }
778 return ret;
779}
780
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500781static int tps80031_regulator_remove(struct platform_device *pdev)
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530782{
783 struct tps80031_regulator *pmic = platform_get_drvdata(pdev);
784 struct tps80031_regulator *ri = NULL;
785 int num;
786
787 for (num = 0; num < TPS80031_REGULATOR_MAX; ++num) {
788 ri = &pmic[num];
789 regulator_unregister(ri->rdev);
790 }
791 return 0;
792}
793
794static struct platform_driver tps80031_regulator_driver = {
795 .driver = {
796 .name = "tps80031-pmic",
797 .owner = THIS_MODULE,
798 },
799 .probe = tps80031_regulator_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500800 .remove = tps80031_regulator_remove,
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530801};
802
803static int __init tps80031_regulator_init(void)
804{
805 return platform_driver_register(&tps80031_regulator_driver);
806}
807subsys_initcall(tps80031_regulator_init);
808
809static void __exit tps80031_regulator_exit(void)
810{
811 platform_driver_unregister(&tps80031_regulator_driver);
812}
813module_exit(tps80031_regulator_exit);
814
815MODULE_ALIAS("platform:tps80031-regulator");
Axel Lin1a7ae582012-11-22 16:30:44 +0800816MODULE_DESCRIPTION("Regulator Driver for TI TPS80031/TPS80032 PMIC");
Laxman Dewangan1a0bb672012-11-11 20:42:01 +0530817MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
818MODULE_LICENSE("GPL v2");