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