1/*
2 * tps80031-regulator.c -- TI TPS80031 regulator driver.
3 *
4 * Regulator driver for TI TPS80031/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
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
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				&reg_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
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
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
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
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 */
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
241static int tps80031_ldo_list_voltage(struct regulator_dev *rdev,
242				     unsigned int sel)
243{
244	struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
245	struct device *parent = to_tps80031_dev(rdev);
246
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
262	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_vbus_is_enabled(struct regulator_dev *rdev)
286{
287	struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
288	struct device *parent = to_tps80031_dev(rdev);
289	int ret = -EIO;
290	uint8_t ctrl1 = 0;
291	uint8_t ctrl3 = 0;
292
293	ret = tps80031_read(parent, TPS80031_SLAVE_ID2,
294			TPS80031_CHARGERUSB_CTRL1, &ctrl1);
295	if (ret < 0) {
296		dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
297			TPS80031_CHARGERUSB_CTRL1, ret);
298		return ret;
299	}
300	ret = tps80031_read(parent, TPS80031_SLAVE_ID2,
301				TPS80031_CHARGERUSB_CTRL3, &ctrl3);
302	if (ret < 0) {
303		dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
304			TPS80031_CHARGERUSB_CTRL3, ret);
305		return ret;
306	}
307	if ((ctrl1 & OPA_MODE_EN) && (ctrl3 & BOOST_HW_PWR_EN))
308		return 1;
309	return ret;
310}
311
312static int tps80031_vbus_enable(struct regulator_dev *rdev)
313{
314	struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
315	struct device *parent = to_tps80031_dev(rdev);
316	int ret;
317
318	ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2,
319				TPS80031_CHARGERUSB_CTRL1, OPA_MODE_EN);
320	if (ret < 0) {
321		dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
322					TPS80031_CHARGERUSB_CTRL1, ret);
323		return ret;
324	}
325
326	ret = tps80031_set_bits(parent, TPS80031_SLAVE_ID2,
327				TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN);
328	if (ret < 0) {
329		dev_err(ri->dev, "reg 0x%02x read failed, e = %d\n",
330			TPS80031_CHARGERUSB_CTRL3, ret);
331		return ret;
332	}
333	return ret;
334}
335
336static int tps80031_vbus_disable(struct regulator_dev *rdev)
337{
338	struct tps80031_regulator *ri = rdev_get_drvdata(rdev);
339	struct device *parent = to_tps80031_dev(rdev);
340	int ret = 0;
341
342	if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) {
343		ret = tps80031_write(parent, TPS80031_SLAVE_ID2,
344			USB_VBUS_CTRL_SET, VBUS_DISCHRG);
345		if (ret < 0) {
346			dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n",
347				USB_VBUS_CTRL_SET, ret);
348			return ret;
349		}
350	}
351
352	ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2,
353			TPS80031_CHARGERUSB_CTRL1,  OPA_MODE_EN);
354	if (ret < 0) {
355		dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n",
356				TPS80031_CHARGERUSB_CTRL1, ret);
357		return ret;
358	}
359
360	ret = tps80031_clr_bits(parent, TPS80031_SLAVE_ID2,
361				TPS80031_CHARGERUSB_CTRL3, BOOST_HW_PWR_EN);
362	if (ret < 0) {
363		dev_err(ri->dev, "reg 0x%02x clearbit failed, e = %d\n",
364				TPS80031_CHARGERUSB_CTRL3, ret);
365		return ret;
366	}
367
368	mdelay(DIV_ROUND_UP(ri->rinfo->desc.enable_time, 1000));
369	if (ri->config_flags & TPS80031_VBUS_DISCHRG_EN_PDN) {
370		ret = tps80031_write(parent, TPS80031_SLAVE_ID2,
371			USB_VBUS_CTRL_CLR, VBUS_DISCHRG);
372		if (ret < 0) {
373			dev_err(ri->dev, "reg 0x%02x write failed, e = %d\n",
374					USB_VBUS_CTRL_CLR, ret);
375			return ret;
376		}
377	}
378	return ret;
379}
380
381static struct regulator_ops tps80031_dcdc_ops = {
382	.list_voltage		= tps80031_dcdc_list_voltage,
383	.set_voltage_sel	= tps80031_dcdc_set_voltage_sel,
384	.get_voltage_sel	= tps80031_dcdc_get_voltage_sel,
385	.enable		= tps80031_reg_enable,
386	.disable	= tps80031_reg_disable,
387	.is_enabled	= tps80031_reg_is_enabled,
388};
389
390static struct regulator_ops tps80031_ldo_ops = {
391	.list_voltage		= tps80031_ldo_list_voltage,
392	.map_voltage		= tps80031_ldo_map_voltage,
393	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
394	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
395	.enable			= tps80031_reg_enable,
396	.disable		= tps80031_reg_disable,
397	.is_enabled		= tps80031_reg_is_enabled,
398};
399
400static struct regulator_ops tps80031_vbus_sw_ops = {
401	.list_voltage	= regulator_list_voltage_linear,
402	.enable		= tps80031_vbus_enable,
403	.disable	= tps80031_vbus_disable,
404	.is_enabled	= tps80031_vbus_is_enabled,
405};
406
407static struct regulator_ops tps80031_vbus_hw_ops = {
408	.list_voltage	= regulator_list_voltage_linear,
409};
410
411static struct regulator_ops tps80031_ext_reg_ops = {
412	.list_voltage	= regulator_list_voltage_linear,
413	.enable		= tps80031_reg_enable,
414	.disable	= tps80031_reg_disable,
415	.is_enabled	= tps80031_reg_is_enabled,
416};
417
418/* Non-exiting default definition for some register */
419#define TPS80031_SMPS3_CFG_FORCE	0
420#define TPS80031_SMPS4_CFG_FORCE	0
421
422#define TPS80031_VBUS_CFG_TRANS		0
423#define TPS80031_VBUS_CFG_STATE		0
424
425#define TPS80031_REG_SMPS(_id, _volt_id, _pbit)	\
426{								\
427	.trans_reg = TPS80031_##_id##_CFG_TRANS,		\
428	.state_reg = TPS80031_##_id##_CFG_STATE,		\
429	.force_reg = TPS80031_##_id##_CFG_FORCE,		\
430	.volt_reg = TPS80031_##_id##_CFG_VOLTAGE,		\
431	.volt_id = TPS80031_SLAVE_##_volt_id,			\
432	.preq_bit = _pbit,					\
433	.desc = {						\
434		.name = "tps80031_"#_id,			\
435		.id = TPS80031_REGULATOR_##_id,			\
436		.n_voltages = 63,				\
437		.ops = &tps80031_dcdc_ops,			\
438		.type = REGULATOR_VOLTAGE,			\
439		.owner = THIS_MODULE,				\
440		.enable_time = 500,				\
441	},							\
442}
443
444#define TPS80031_REG_LDO(_id, _preq_bit)			\
445{								\
446	.trans_reg = TPS80031_##_id##_CFG_TRANS,		\
447	.state_reg = TPS80031_##_id##_CFG_STATE,		\
448	.volt_reg = TPS80031_##_id##_CFG_VOLTAGE,		\
449	.volt_id = TPS80031_SLAVE_ID1,				\
450	.preq_bit = _preq_bit,					\
451	.desc = {						\
452		.owner = THIS_MODULE,				\
453		.name = "tps80031_"#_id,			\
454		.id = TPS80031_REGULATOR_##_id,			\
455		.ops = &tps80031_ldo_ops,			\
456		.type = REGULATOR_VOLTAGE,			\
457		.min_uV = 1000000,				\
458		.uV_step = 100000,				\
459		.linear_min_sel = 1,				\
460		.n_voltages = 25,				\
461		.vsel_reg = TPS80031_##_id##_CFG_VOLTAGE,	\
462		.vsel_mask = LDO_VSEL_MASK,			\
463		.enable_time = 500,				\
464	},							\
465}
466
467#define TPS80031_REG_FIXED(_id, max_mV, _ops, _delay, _pbit)	\
468{								\
469	.trans_reg = TPS80031_##_id##_CFG_TRANS,		\
470	.state_reg = TPS80031_##_id##_CFG_STATE,		\
471	.volt_id = TPS80031_SLAVE_ID1,				\
472	.preq_bit = _pbit,					\
473	.desc = {						\
474		.name = "tps80031_"#_id,			\
475		.id = TPS80031_REGULATOR_##_id,			\
476		.min_uV = max_mV * 1000,			\
477		.n_voltages = 1,				\
478		.ops = &_ops,					\
479		.type = REGULATOR_VOLTAGE,			\
480		.owner = THIS_MODULE,				\
481		.enable_time = _delay,				\
482	},							\
483}
484
485static struct tps80031_regulator_info tps80031_rinfo[TPS80031_REGULATOR_MAX] = {
486	TPS80031_REG_SMPS(VIO,   ID0, 4),
487	TPS80031_REG_SMPS(SMPS1, ID0, 0),
488	TPS80031_REG_SMPS(SMPS2, ID0, 1),
489	TPS80031_REG_SMPS(SMPS3, ID1, 2),
490	TPS80031_REG_SMPS(SMPS4, ID1, 3),
491	TPS80031_REG_LDO(VANA,   -1),
492	TPS80031_REG_LDO(LDO1,   8),
493	TPS80031_REG_LDO(LDO2,   9),
494	TPS80031_REG_LDO(LDO3,   10),
495	TPS80031_REG_LDO(LDO4,   11),
496	TPS80031_REG_LDO(LDO5,   12),
497	TPS80031_REG_LDO(LDO6,   13),
498	TPS80031_REG_LDO(LDO7,   14),
499	TPS80031_REG_LDO(LDOLN,  15),
500	TPS80031_REG_LDO(LDOUSB, 5),
501	TPS80031_REG_FIXED(VBUS,   5000, tps80031_vbus_hw_ops, 100000, -1),
502	TPS80031_REG_FIXED(REGEN1, 3300, tps80031_ext_reg_ops, 0, 16),
503	TPS80031_REG_FIXED(REGEN2, 3300, tps80031_ext_reg_ops, 0, 17),
504	TPS80031_REG_FIXED(SYSEN,  3300, tps80031_ext_reg_ops, 0, 18),
505};
506
507static int tps80031_power_req_config(struct device *parent,
508		struct tps80031_regulator *ri,
509		struct tps80031_regulator_platform_data *tps80031_pdata)
510{
511	int ret = 0;
512
513	if (ri->rinfo->preq_bit < 0)
514		goto skip_pwr_req_config;
515
516	ret = tps80031_ext_power_req_config(parent, ri->ext_ctrl_flag,
517			ri->rinfo->preq_bit, ri->rinfo->state_reg,
518			ri->rinfo->trans_reg);
519	if (ret < 0) {
520		dev_err(ri->dev, "ext powerreq config failed, err = %d\n", ret);
521		return ret;
522	}
523
524skip_pwr_req_config:
525	if (tps80031_pdata->ext_ctrl_flag & TPS80031_PWR_ON_ON_SLEEP) {
526		ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
527				ri->rinfo->trans_reg, TPS80031_TRANS_SLEEP_ON,
528				TPS80031_TRANS_SLEEP_MASK);
529		if (ret < 0) {
530			dev_err(ri->dev, "Reg 0x%02x update failed, e %d\n",
531					ri->rinfo->trans_reg, ret);
532			return ret;
533		}
534	}
535	return ret;
536}
537
538static int tps80031_regulator_config(struct device *parent,
539		struct tps80031_regulator *ri,
540		struct tps80031_regulator_platform_data *tps80031_pdata)
541{
542	int ret = 0;
543
544	switch (ri->rinfo->desc.id) {
545	case TPS80031_REGULATOR_LDOUSB:
546		if (ri->config_flags & (TPS80031_USBLDO_INPUT_VSYS |
547			TPS80031_USBLDO_INPUT_PMID)) {
548			unsigned val = 0;
549			if (ri->config_flags & TPS80031_USBLDO_INPUT_VSYS)
550				val = MISC2_LDOUSB_IN_VSYS;
551			else
552				val = MISC2_LDOUSB_IN_PMID;
553
554			ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
555				TPS80031_MISC2, val,
556				MISC2_LDOUSB_IN_MASK);
557			if (ret < 0) {
558				dev_err(ri->dev,
559					"LDOUSB config failed, e= %d\n", ret);
560				return ret;
561			}
562		}
563		break;
564
565	case TPS80031_REGULATOR_LDO3:
566		if (ri->config_flags & TPS80031_LDO3_OUTPUT_VIB) {
567			ret = tps80031_update(parent, TPS80031_SLAVE_ID1,
568				TPS80031_MISC2, MISC2_LDO3_SEL_VIB_VAL,
569				MISC2_LDO3_SEL_VIB_MASK);
570			if (ret < 0) {
571				dev_err(ri->dev,
572					"LDO3 config failed, e = %d\n", ret);
573				return ret;
574			}
575		}
576		break;
577
578	case TPS80031_REGULATOR_VBUS:
579		/* Provide SW control Ops if VBUS is SW control */
580		if (!(ri->config_flags & TPS80031_VBUS_SW_ONLY))
581			ri->rinfo->desc.ops = &tps80031_vbus_sw_ops;
582		break;
583	default:
584		break;
585	}
586
587	/* Configure Active state to ON, SLEEP to OFF and OFF_state to OFF */
588	ret = tps80031_update(parent, TPS80031_SLAVE_ID1, ri->rinfo->trans_reg,
589		TPS80031_TRANS_ACTIVE_ON | TPS80031_TRANS_SLEEP_OFF |
590		TPS80031_TRANS_OFF_OFF, TPS80031_TRANS_ACTIVE_MASK |
591		TPS80031_TRANS_SLEEP_MASK | TPS80031_TRANS_OFF_MASK);
592	if (ret < 0) {
593		dev_err(ri->dev, "trans reg update failed, e %d\n", ret);
594		return ret;
595	}
596
597	return ret;
598}
599
600static int check_smps_mode_mult(struct device *parent,
601	struct tps80031_regulator *ri)
602{
603	int mult_offset;
604	int ret;
605	u8 smps_offset;
606	u8 smps_mult;
607
608	ret = tps80031_read(parent, TPS80031_SLAVE_ID1,
609			TPS80031_SMPS_OFFSET, &smps_offset);
610	if (ret < 0) {
611		dev_err(parent, "Error in reading smps offset register\n");
612		return ret;
613	}
614
615	ret = tps80031_read(parent, TPS80031_SLAVE_ID1,
616			TPS80031_SMPS_MULT, &smps_mult);
617	if (ret < 0) {
618		dev_err(parent, "Error in reading smps mult register\n");
619		return ret;
620	}
621
622	switch (ri->rinfo->desc.id) {
623	case TPS80031_REGULATOR_VIO:
624		mult_offset = SMPS_MULTOFFSET_VIO;
625		break;
626	case TPS80031_REGULATOR_SMPS1:
627		mult_offset = SMPS_MULTOFFSET_SMPS1;
628		break;
629	case TPS80031_REGULATOR_SMPS2:
630		mult_offset = SMPS_MULTOFFSET_SMPS2;
631		break;
632	case TPS80031_REGULATOR_SMPS3:
633		mult_offset = SMPS_MULTOFFSET_SMPS3;
634		break;
635	case TPS80031_REGULATOR_SMPS4:
636		mult_offset = SMPS_MULTOFFSET_SMPS4;
637		break;
638	case TPS80031_REGULATOR_LDO2:
639		ri->device_flags = smps_mult & BIT(5) ? TRACK_MODE_ENABLE : 0;
640		/* TRACK mode the ldo2 varies from 600mV to 1300mV */
641		if (ri->device_flags & TRACK_MODE_ENABLE) {
642			ri->rinfo->desc.min_uV = 600000;
643			ri->rinfo->desc.uV_step = 12500;
644			ri->rinfo->desc.n_voltages = 57;
645			ri->rinfo->desc.vsel_mask = LDO_TRACK_VSEL_MASK;
646		}
647		return 0;
648	default:
649		return 0;
650	}
651
652	ri->device_flags = (smps_offset & mult_offset) ? DCDC_OFFSET_EN : 0;
653	ri->device_flags |= (smps_mult & mult_offset) ? DCDC_EXTENDED_EN : 0;
654	switch (ri->device_flags) {
655	case 0:
656		ri->rinfo->desc.min_uV = 607700;
657		ri->rinfo->desc.uV_step = 12660;
658		break;
659	case DCDC_OFFSET_EN:
660		ri->rinfo->desc.min_uV = 700000;
661		ri->rinfo->desc.uV_step = 12500;
662		break;
663	case DCDC_EXTENDED_EN:
664		ri->rinfo->desc.min_uV = 1852000;
665		ri->rinfo->desc.uV_step = 38600;
666		break;
667	case DCDC_OFFSET_EN | DCDC_EXTENDED_EN:
668		ri->rinfo->desc.min_uV = 2161000;
669		ri->rinfo->desc.uV_step = 38600;
670		break;
671	}
672	return 0;
673}
674
675static int tps80031_regulator_probe(struct platform_device *pdev)
676{
677	struct tps80031_platform_data *pdata;
678	struct tps80031_regulator_platform_data *tps_pdata;
679	struct tps80031_regulator *ri;
680	struct tps80031_regulator *pmic;
681	struct regulator_dev *rdev;
682	struct regulator_config config = { };
683	struct tps80031 *tps80031_mfd = dev_get_drvdata(pdev->dev.parent);
684	int ret;
685	int num;
686
687	pdata = dev_get_platdata(pdev->dev.parent);
688
689	if (!pdata) {
690		dev_err(&pdev->dev, "No platform data\n");
691		return -EINVAL;
692	}
693
694	pmic = devm_kzalloc(&pdev->dev,
695			TPS80031_REGULATOR_MAX * sizeof(*pmic), GFP_KERNEL);
696	if (!pmic)
697		return -ENOMEM;
698
699	for (num = 0; num < TPS80031_REGULATOR_MAX; ++num) {
700		tps_pdata = pdata->regulator_pdata[num];
701		ri = &pmic[num];
702		ri->rinfo = &tps80031_rinfo[num];
703		ri->dev = &pdev->dev;
704
705		check_smps_mode_mult(pdev->dev.parent, ri);
706		config.dev = &pdev->dev;
707		config.init_data = NULL;
708		config.driver_data = ri;
709		config.regmap = tps80031_mfd->regmap[ri->rinfo->volt_id];
710
711		if (tps_pdata) {
712			config.init_data = tps_pdata->reg_init_data;
713			ri->config_flags = tps_pdata->config_flags;
714			ri->ext_ctrl_flag = tps_pdata->ext_ctrl_flag;
715			ret = tps80031_regulator_config(pdev->dev.parent,
716					ri, tps_pdata);
717			if (ret < 0) {
718				dev_err(&pdev->dev,
719					"regulator config failed, e %d\n", ret);
720				return ret;
721			}
722
723			ret = tps80031_power_req_config(pdev->dev.parent,
724					ri, tps_pdata);
725			if (ret < 0) {
726				dev_err(&pdev->dev,
727					"pwr_req config failed, err %d\n", ret);
728				return ret;
729			}
730		}
731		rdev = devm_regulator_register(&pdev->dev, &ri->rinfo->desc,
732					       &config);
733		if (IS_ERR(rdev)) {
734			dev_err(&pdev->dev,
735				"register regulator failed %s\n",
736					ri->rinfo->desc.name);
737			return PTR_ERR(rdev);
738		}
739		ri->rdev = rdev;
740	}
741
742	platform_set_drvdata(pdev, pmic);
743	return 0;
744}
745
746static struct platform_driver tps80031_regulator_driver = {
747	.driver	= {
748		.name	= "tps80031-pmic",
749		.owner	= THIS_MODULE,
750	},
751	.probe		= tps80031_regulator_probe,
752};
753
754static int __init tps80031_regulator_init(void)
755{
756	return platform_driver_register(&tps80031_regulator_driver);
757}
758subsys_initcall(tps80031_regulator_init);
759
760static void __exit tps80031_regulator_exit(void)
761{
762	platform_driver_unregister(&tps80031_regulator_driver);
763}
764module_exit(tps80031_regulator_exit);
765
766MODULE_ALIAS("platform:tps80031-regulator");
767MODULE_DESCRIPTION("Regulator Driver for TI TPS80031/TPS80032 PMIC");
768MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
769MODULE_LICENSE("GPL v2");
770