tps65910-regulator.c 32.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * tps65910.c  --  TI tps65910
 *
 * Copyright 2010 Texas Instruments Inc.
 *
 * Author: Graeme Gregory <gg@slimlogic.co.uk>
 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under  the terms of the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the License, or (at your
 *  option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/mfd/tps65910.h>
26
#include <linux/regulator/of_regulator.h>
27 28

#define TPS65910_SUPPLY_STATE_ENABLED	0x1
29 30
#define EXT_SLEEP_CONTROL (TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1 |	\
			TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2 |		\
31 32
			TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3 |		\
			TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
33

34 35 36
/* supported VIO voltages in microvolts */
static const unsigned int VIO_VSEL_table[] = {
	1500000, 1800000, 2500000, 3300000,
37 38
};

39 40
/* VSEL tables for TPS65910 specific LDOs and dcdc's */

41 42 43 44 45
/* supported VRTC voltages in microvolts */
static const unsigned int VRTC_VSEL_table[] = {
	1800000,
};

46 47 48
/* supported VDD3 voltages in microvolts */
static const unsigned int VDD3_VSEL_table[] = {
	5000000,
49 50
};

51 52 53
/* supported VDIG1 voltages in microvolts */
static const unsigned int VDIG1_VSEL_table[] = {
	1200000, 1500000, 1800000, 2700000,
54 55
};

56 57 58
/* supported VDIG2 voltages in microvolts */
static const unsigned int VDIG2_VSEL_table[] = {
	1000000, 1100000, 1200000, 1800000,
59 60
};

61 62 63
/* supported VPLL voltages in microvolts */
static const unsigned int VPLL_VSEL_table[] = {
	1000000, 1100000, 1800000, 2500000,
64 65
};

66 67 68
/* supported VDAC voltages in microvolts */
static const unsigned int VDAC_VSEL_table[] = {
	1800000, 2600000, 2800000, 2850000,
69 70
};

71 72 73
/* supported VAUX1 voltages in microvolts */
static const unsigned int VAUX1_VSEL_table[] = {
	1800000, 2500000, 2800000, 2850000,
74 75
};

76 77 78
/* supported VAUX2 voltages in microvolts */
static const unsigned int VAUX2_VSEL_table[] = {
	1800000, 2800000, 2900000, 3300000,
79 80
};

81 82 83
/* supported VAUX33 voltages in microvolts */
static const unsigned int VAUX33_VSEL_table[] = {
	1800000, 2000000, 2800000, 3300000,
84 85
};

86 87 88
/* supported VMMC voltages in microvolts */
static const unsigned int VMMC_VSEL_table[] = {
	1800000, 2800000, 3000000, 3300000,
89 90
};

91 92 93 94 95
/* supported BBCH voltages in microvolts */
static const unsigned int VBB_VSEL_table[] = {
	3000000, 2520000, 3150000, 5000000,
};

96 97
struct tps_info {
	const char *name;
98
	const char *vin_name;
99
	u8 n_voltages;
100
	const unsigned int *voltage_table;
101
	int enable_time_us;
102 103 104 105
};

static struct tps_info tps65910_regs[] = {
	{
106
		.name = "vrtc",
107
		.vin_name = "vcc7",
108 109
		.n_voltages = ARRAY_SIZE(VRTC_VSEL_table),
		.voltage_table = VRTC_VSEL_table,
110
		.enable_time_us = 2200,
111 112
	},
	{
113
		.name = "vio",
114
		.vin_name = "vccio",
115 116
		.n_voltages = ARRAY_SIZE(VIO_VSEL_table),
		.voltage_table = VIO_VSEL_table,
117
		.enable_time_us = 350,
118 119
	},
	{
120
		.name = "vdd1",
121
		.vin_name = "vcc1",
122
		.enable_time_us = 350,
123 124
	},
	{
125
		.name = "vdd2",
126
		.vin_name = "vcc2",
127
		.enable_time_us = 350,
128 129
	},
	{
130
		.name = "vdd3",
131 132
		.n_voltages = ARRAY_SIZE(VDD3_VSEL_table),
		.voltage_table = VDD3_VSEL_table,
133
		.enable_time_us = 200,
134 135
	},
	{
136
		.name = "vdig1",
137
		.vin_name = "vcc6",
138 139
		.n_voltages = ARRAY_SIZE(VDIG1_VSEL_table),
		.voltage_table = VDIG1_VSEL_table,
140
		.enable_time_us = 100,
141 142
	},
	{
143
		.name = "vdig2",
144
		.vin_name = "vcc6",
145 146
		.n_voltages = ARRAY_SIZE(VDIG2_VSEL_table),
		.voltage_table = VDIG2_VSEL_table,
147
		.enable_time_us = 100,
148 149
	},
	{
150
		.name = "vpll",
151
		.vin_name = "vcc5",
152 153
		.n_voltages = ARRAY_SIZE(VPLL_VSEL_table),
		.voltage_table = VPLL_VSEL_table,
154
		.enable_time_us = 100,
155 156
	},
	{
157
		.name = "vdac",
158
		.vin_name = "vcc5",
159 160
		.n_voltages = ARRAY_SIZE(VDAC_VSEL_table),
		.voltage_table = VDAC_VSEL_table,
161
		.enable_time_us = 100,
162 163
	},
	{
164
		.name = "vaux1",
165
		.vin_name = "vcc4",
166 167
		.n_voltages = ARRAY_SIZE(VAUX1_VSEL_table),
		.voltage_table = VAUX1_VSEL_table,
168
		.enable_time_us = 100,
169 170
	},
	{
171
		.name = "vaux2",
172
		.vin_name = "vcc4",
173 174
		.n_voltages = ARRAY_SIZE(VAUX2_VSEL_table),
		.voltage_table = VAUX2_VSEL_table,
175
		.enable_time_us = 100,
176 177
	},
	{
178
		.name = "vaux33",
179
		.vin_name = "vcc3",
180 181
		.n_voltages = ARRAY_SIZE(VAUX33_VSEL_table),
		.voltage_table = VAUX33_VSEL_table,
182
		.enable_time_us = 100,
183 184
	},
	{
185
		.name = "vmmc",
186
		.vin_name = "vcc3",
187 188
		.n_voltages = ARRAY_SIZE(VMMC_VSEL_table),
		.voltage_table = VMMC_VSEL_table,
189
		.enable_time_us = 100,
190
	},
191 192 193 194 195 196
	{
		.name = "vbb",
		.vin_name = "vcc7",
		.n_voltages = ARRAY_SIZE(VBB_VSEL_table),
		.voltage_table = VBB_VSEL_table,
	},
197 198
};

199
static struct tps_info tps65911_regs[] = {
200
	{
201
		.name = "vrtc",
202
		.vin_name = "vcc7",
203
		.enable_time_us = 2200,
204
	},
205
	{
206
		.name = "vio",
207
		.vin_name = "vccio",
208 209
		.n_voltages = ARRAY_SIZE(VIO_VSEL_table),
		.voltage_table = VIO_VSEL_table,
210
		.enable_time_us = 350,
211 212
	},
	{
213
		.name = "vdd1",
214
		.vin_name = "vcc1",
215
		.n_voltages = 0x4C,
216
		.enable_time_us = 350,
217 218
	},
	{
219
		.name = "vdd2",
220
		.vin_name = "vcc2",
221
		.n_voltages = 0x4C,
222
		.enable_time_us = 350,
223 224
	},
	{
225
		.name = "vddctrl",
226
		.n_voltages = 0x44,
227
		.enable_time_us = 900,
228 229
	},
	{
230
		.name = "ldo1",
231
		.vin_name = "vcc6",
232
		.n_voltages = 0x33,
233
		.enable_time_us = 420,
234 235
	},
	{
236
		.name = "ldo2",
237
		.vin_name = "vcc6",
238
		.n_voltages = 0x33,
239
		.enable_time_us = 420,
240 241
	},
	{
242
		.name = "ldo3",
243
		.vin_name = "vcc5",
244
		.n_voltages = 0x1A,
245
		.enable_time_us = 230,
246 247
	},
	{
248
		.name = "ldo4",
249
		.vin_name = "vcc5",
250
		.n_voltages = 0x33,
251
		.enable_time_us = 230,
252 253
	},
	{
254
		.name = "ldo5",
255
		.vin_name = "vcc4",
256
		.n_voltages = 0x1A,
257
		.enable_time_us = 230,
258 259
	},
	{
260
		.name = "ldo6",
261
		.vin_name = "vcc3",
262
		.n_voltages = 0x1A,
263
		.enable_time_us = 230,
264 265
	},
	{
266
		.name = "ldo7",
267
		.vin_name = "vcc3",
268
		.n_voltages = 0x1A,
269
		.enable_time_us = 230,
270 271
	},
	{
272
		.name = "ldo8",
273
		.vin_name = "vcc3",
274
		.n_voltages = 0x1A,
275
		.enable_time_us = 230,
276 277 278
	},
};

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
#define EXT_CONTROL_REG_BITS(id, regs_offs, bits) (((regs_offs) << 8) | (bits))
static unsigned int tps65910_ext_sleep_control[] = {
	0,
	EXT_CONTROL_REG_BITS(VIO,    1, 0),
	EXT_CONTROL_REG_BITS(VDD1,   1, 1),
	EXT_CONTROL_REG_BITS(VDD2,   1, 2),
	EXT_CONTROL_REG_BITS(VDD3,   1, 3),
	EXT_CONTROL_REG_BITS(VDIG1,  0, 1),
	EXT_CONTROL_REG_BITS(VDIG2,  0, 2),
	EXT_CONTROL_REG_BITS(VPLL,   0, 6),
	EXT_CONTROL_REG_BITS(VDAC,   0, 7),
	EXT_CONTROL_REG_BITS(VAUX1,  0, 3),
	EXT_CONTROL_REG_BITS(VAUX2,  0, 4),
	EXT_CONTROL_REG_BITS(VAUX33, 0, 5),
	EXT_CONTROL_REG_BITS(VMMC,   0, 0),
};

static unsigned int tps65911_ext_sleep_control[] = {
	0,
	EXT_CONTROL_REG_BITS(VIO,     1, 0),
	EXT_CONTROL_REG_BITS(VDD1,    1, 1),
	EXT_CONTROL_REG_BITS(VDD2,    1, 2),
	EXT_CONTROL_REG_BITS(VDDCTRL, 1, 3),
	EXT_CONTROL_REG_BITS(LDO1,    0, 1),
	EXT_CONTROL_REG_BITS(LDO2,    0, 2),
	EXT_CONTROL_REG_BITS(LDO3,    0, 7),
	EXT_CONTROL_REG_BITS(LDO4,    0, 6),
	EXT_CONTROL_REG_BITS(LDO5,    0, 3),
	EXT_CONTROL_REG_BITS(LDO6,    0, 0),
	EXT_CONTROL_REG_BITS(LDO7,    0, 5),
	EXT_CONTROL_REG_BITS(LDO8,    0, 4),
};

312
struct tps65910_reg {
313
	struct regulator_desc *desc;
314
	struct tps65910 *mfd;
315 316 317
	struct regulator_dev **rdev;
	struct tps_info **info;
	int num_regulators;
318
	int mode;
319
	int  (*get_ctrl_reg)(int);
320 321
	unsigned int *ext_sleep_control;
	unsigned int board_ext_control[TPS65910_NUM_REGS];
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
};

static int tps65910_get_ctrl_register(int id)
{
	switch (id) {
	case TPS65910_REG_VRTC:
		return TPS65910_VRTC;
	case TPS65910_REG_VIO:
		return TPS65910_VIO;
	case TPS65910_REG_VDD1:
		return TPS65910_VDD1;
	case TPS65910_REG_VDD2:
		return TPS65910_VDD2;
	case TPS65910_REG_VDD3:
		return TPS65910_VDD3;
	case TPS65910_REG_VDIG1:
		return TPS65910_VDIG1;
	case TPS65910_REG_VDIG2:
		return TPS65910_VDIG2;
	case TPS65910_REG_VPLL:
		return TPS65910_VPLL;
	case TPS65910_REG_VDAC:
		return TPS65910_VDAC;
	case TPS65910_REG_VAUX1:
		return TPS65910_VAUX1;
	case TPS65910_REG_VAUX2:
		return TPS65910_VAUX2;
	case TPS65910_REG_VAUX33:
		return TPS65910_VAUX33;
	case TPS65910_REG_VMMC:
		return TPS65910_VMMC;
353 354
	case TPS65910_REG_VBB:
		return TPS65910_BBCH;
355 356 357 358 359
	default:
		return -EINVAL;
	}
}

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
static int tps65911_get_ctrl_register(int id)
{
	switch (id) {
	case TPS65910_REG_VRTC:
		return TPS65910_VRTC;
	case TPS65910_REG_VIO:
		return TPS65910_VIO;
	case TPS65910_REG_VDD1:
		return TPS65910_VDD1;
	case TPS65910_REG_VDD2:
		return TPS65910_VDD2;
	case TPS65911_REG_VDDCTRL:
		return TPS65911_VDDCTRL;
	case TPS65911_REG_LDO1:
		return TPS65911_LDO1;
	case TPS65911_REG_LDO2:
		return TPS65911_LDO2;
	case TPS65911_REG_LDO3:
		return TPS65911_LDO3;
	case TPS65911_REG_LDO4:
		return TPS65911_LDO4;
	case TPS65911_REG_LDO5:
		return TPS65911_LDO5;
	case TPS65911_REG_LDO6:
		return TPS65911_LDO6;
	case TPS65911_REG_LDO7:
		return TPS65911_LDO7;
	case TPS65911_REG_LDO8:
		return TPS65911_LDO8;
	default:
		return -EINVAL;
	}
}

394 395 396 397 398
static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	struct tps65910 *mfd = pmic->mfd;
	int reg, value, id = rdev_get_id(dev);
399 400

	reg = pmic->get_ctrl_reg(id);
401 402 403 404 405
	if (reg < 0)
		return reg;

	switch (mode) {
	case REGULATOR_MODE_NORMAL:
406 407 408
		return tps65910_reg_update_bits(pmic->mfd, reg,
						LDO_ST_MODE_BIT | LDO_ST_ON_BIT,
						LDO_ST_ON_BIT);
409 410
	case REGULATOR_MODE_IDLE:
		value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
411
		return tps65910_reg_set_bits(mfd, reg, value);
412
	case REGULATOR_MODE_STANDBY:
413
		return tps65910_reg_clear_bits(mfd, reg, LDO_ST_ON_BIT);
414 415 416 417 418 419 420 421
	}

	return -EINVAL;
}

static unsigned int tps65910_get_mode(struct regulator_dev *dev)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
422
	int ret, reg, value, id = rdev_get_id(dev);
423

424
	reg = pmic->get_ctrl_reg(id);
425 426 427
	if (reg < 0)
		return reg;

428 429 430
	ret = tps65910_reg_read(pmic->mfd, reg, &value);
	if (ret < 0)
		return ret;
431

432
	if (!(value & LDO_ST_ON_BIT))
433 434 435 436 437 438 439
		return REGULATOR_MODE_STANDBY;
	else if (value & LDO_ST_MODE_BIT)
		return REGULATOR_MODE_IDLE;
	else
		return REGULATOR_MODE_NORMAL;
}

440
static int tps65910_get_voltage_dcdc_sel(struct regulator_dev *dev)
441 442
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
443
	int ret, id = rdev_get_id(dev);
444
	int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;
445 446 447

	switch (id) {
	case TPS65910_REG_VDD1:
448 449 450 451 452 453
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1_OP, &opvsel);
		if (ret < 0)
			return ret;
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1, &mult);
		if (ret < 0)
			return ret;
454
		mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
455 456 457
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD1_SR, &srvsel);
		if (ret < 0)
			return ret;
458 459 460
		sr = opvsel & VDD1_OP_CMD_MASK;
		opvsel &= VDD1_OP_SEL_MASK;
		srvsel &= VDD1_SR_SEL_MASK;
461
		vselmax = 75;
462 463
		break;
	case TPS65910_REG_VDD2:
464 465 466 467 468 469
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2_OP, &opvsel);
		if (ret < 0)
			return ret;
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2, &mult);
		if (ret < 0)
			return ret;
470
		mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
471 472 473
		ret = tps65910_reg_read(pmic->mfd, TPS65910_VDD2_SR, &srvsel);
		if (ret < 0)
			return ret;
474 475 476
		sr = opvsel & VDD2_OP_CMD_MASK;
		opvsel &= VDD2_OP_SEL_MASK;
		srvsel &= VDD2_SR_SEL_MASK;
477 478 479
		vselmax = 75;
		break;
	case TPS65911_REG_VDDCTRL:
480 481 482 483 484 485 486 487
		ret = tps65910_reg_read(pmic->mfd, TPS65911_VDDCTRL_OP,
					&opvsel);
		if (ret < 0)
			return ret;
		ret = tps65910_reg_read(pmic->mfd, TPS65911_VDDCTRL_SR,
					&srvsel);
		if (ret < 0)
			return ret;
488 489 490 491
		sr = opvsel & VDDCTRL_OP_CMD_MASK;
		opvsel &= VDDCTRL_OP_SEL_MASK;
		srvsel &= VDDCTRL_SR_SEL_MASK;
		vselmax = 64;
492 493 494 495 496
		break;
	}

	/* multiplier 0 == 1 but 2,3 normal */
	if (!mult)
497
		mult = 1;
498 499

	if (sr) {
500 501 502 503 504
		/* normalise to valid range */
		if (srvsel < 3)
			srvsel = 3;
		if (srvsel > vselmax)
			srvsel = vselmax;
505
		return srvsel - 3;
506 507
	} else {

508 509 510 511 512
		/* normalise to valid range*/
		if (opvsel < 3)
			opvsel = 3;
		if (opvsel > vselmax)
			opvsel = vselmax;
513
		return opvsel - 3;
514
	}
515
	return -EINVAL;
516 517
}

518
static int tps65910_get_voltage_sel(struct regulator_dev *dev)
519 520
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
521
	int ret, reg, value, id = rdev_get_id(dev);
522

523
	reg = pmic->get_ctrl_reg(id);
524 525 526
	if (reg < 0)
		return reg;

527 528 529
	ret = tps65910_reg_read(pmic->mfd, reg, &value);
	if (ret < 0)
		return ret;
530 531 532 533 534 535 536 537 538 539 540 541 542 543

	switch (id) {
	case TPS65910_REG_VIO:
	case TPS65910_REG_VDIG1:
	case TPS65910_REG_VDIG2:
	case TPS65910_REG_VPLL:
	case TPS65910_REG_VDAC:
	case TPS65910_REG_VAUX1:
	case TPS65910_REG_VAUX2:
	case TPS65910_REG_VAUX33:
	case TPS65910_REG_VMMC:
		value &= LDO_SEL_MASK;
		value >>= LDO_SEL_SHIFT;
		break;
544 545 546 547
	case TPS65910_REG_VBB:
		value &= BBCH_BBSEL_MASK;
		value >>= BBCH_BBSEL_SHIFT;
		break;
548 549 550 551
	default:
		return -EINVAL;
	}

552
	return value;
553 554 555 556
}

static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
{
557
	return dev->desc->volt_table[0];
558 559
}

560
static int tps65911_get_voltage_sel(struct regulator_dev *dev)
561 562
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
563 564
	int ret, id = rdev_get_id(dev);
	unsigned int value, reg;
565 566 567

	reg = pmic->get_ctrl_reg(id);

568 569 570
	ret = tps65910_reg_read(pmic->mfd, reg, &value);
	if (ret < 0)
		return ret;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

	switch (id) {
	case TPS65911_REG_LDO1:
	case TPS65911_REG_LDO2:
	case TPS65911_REG_LDO4:
		value &= LDO1_SEL_MASK;
		value >>= LDO_SEL_SHIFT;
		break;
	case TPS65911_REG_LDO3:
	case TPS65911_REG_LDO5:
	case TPS65911_REG_LDO6:
	case TPS65911_REG_LDO7:
	case TPS65911_REG_LDO8:
		value &= LDO3_SEL_MASK;
		value >>= LDO_SEL_SHIFT;
		break;
	case TPS65910_REG_VIO:
588 589
		value &= LDO_SEL_MASK;
		value >>= LDO_SEL_SHIFT;
590
		break;
591 592 593 594
	default:
		return -EINVAL;
	}

595
	return value;
596 597
}

598 599
static int tps65910_set_voltage_dcdc_sel(struct regulator_dev *dev,
					 unsigned selector)
600 601 602
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int id = rdev_get_id(dev), vsel;
603
	int dcdc_mult = 0;
604

605 606
	switch (id) {
	case TPS65910_REG_VDD1:
607
		dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
608 609
		if (dcdc_mult == 1)
			dcdc_mult--;
610
		vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
611

612 613 614 615
		tps65910_reg_update_bits(pmic->mfd, TPS65910_VDD1,
					 VDD1_VGAIN_SEL_MASK,
					 dcdc_mult << VDD1_VGAIN_SEL_SHIFT);
		tps65910_reg_write(pmic->mfd, TPS65910_VDD1_OP, vsel);
616 617
		break;
	case TPS65910_REG_VDD2:
618
		dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
619 620
		if (dcdc_mult == 1)
			dcdc_mult--;
621
		vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
622

623 624 625 626
		tps65910_reg_update_bits(pmic->mfd, TPS65910_VDD2,
					 VDD1_VGAIN_SEL_MASK,
					 dcdc_mult << VDD2_VGAIN_SEL_SHIFT);
		tps65910_reg_write(pmic->mfd, TPS65910_VDD2_OP, vsel);
627 628
		break;
	case TPS65911_REG_VDDCTRL:
629
		vsel = selector + 3;
630
		tps65910_reg_write(pmic->mfd, TPS65911_VDDCTRL_OP, vsel);
631 632 633 634 635
	}

	return 0;
}

636 637
static int tps65910_set_voltage_sel(struct regulator_dev *dev,
				    unsigned selector)
638 639 640 641
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int reg, id = rdev_get_id(dev);

642
	reg = pmic->get_ctrl_reg(id);
643 644 645 646 647 648 649 650 651 652 653 654 655
	if (reg < 0)
		return reg;

	switch (id) {
	case TPS65910_REG_VIO:
	case TPS65910_REG_VDIG1:
	case TPS65910_REG_VDIG2:
	case TPS65910_REG_VPLL:
	case TPS65910_REG_VDAC:
	case TPS65910_REG_VAUX1:
	case TPS65910_REG_VAUX2:
	case TPS65910_REG_VAUX33:
	case TPS65910_REG_VMMC:
656 657
		return tps65910_reg_update_bits(pmic->mfd, reg, LDO_SEL_MASK,
						selector << LDO_SEL_SHIFT);
658 659 660
	case TPS65910_REG_VBB:
		return tps65910_reg_update_bits(pmic->mfd, reg, BBCH_BBSEL_MASK,
						selector << BBCH_BBSEL_SHIFT);
661 662 663 664 665
	}

	return -EINVAL;
}

666 667
static int tps65911_set_voltage_sel(struct regulator_dev *dev,
				    unsigned selector)
668 669 670 671 672 673 674 675 676 677 678 679
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int reg, id = rdev_get_id(dev);

	reg = pmic->get_ctrl_reg(id);
	if (reg < 0)
		return reg;

	switch (id) {
	case TPS65911_REG_LDO1:
	case TPS65911_REG_LDO2:
	case TPS65911_REG_LDO4:
680 681
		return tps65910_reg_update_bits(pmic->mfd, reg, LDO1_SEL_MASK,
						selector << LDO_SEL_SHIFT);
682 683 684 685 686
	case TPS65911_REG_LDO3:
	case TPS65911_REG_LDO5:
	case TPS65911_REG_LDO6:
	case TPS65911_REG_LDO7:
	case TPS65911_REG_LDO8:
687 688
		return tps65910_reg_update_bits(pmic->mfd, reg, LDO3_SEL_MASK,
						selector << LDO_SEL_SHIFT);
689
	case TPS65910_REG_VIO:
690 691
		return tps65910_reg_update_bits(pmic->mfd, reg, LDO_SEL_MASK,
						selector << LDO_SEL_SHIFT);
692 693 694
	case TPS65910_REG_VBB:
		return tps65910_reg_update_bits(pmic->mfd, reg, BBCH_BBSEL_MASK,
						selector << BBCH_BBSEL_SHIFT);
695 696 697 698 699 700
	}

	return -EINVAL;
}


701 702 703
static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
					unsigned selector)
{
704
	int volt, mult = 1, id = rdev_get_id(dev);
705

706 707 708
	switch (id) {
	case TPS65910_REG_VDD1:
	case TPS65910_REG_VDD2:
709
		mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
710
		volt = VDD1_2_MIN_VOLT +
711
			(selector % VDD1_2_NUM_VOLT_FINE) * VDD1_2_OFFSET;
712
		break;
713 714
	case TPS65911_REG_VDDCTRL:
		volt = VDDCTRL_MIN_VOLT + (selector * VDDCTRL_OFFSET);
715 716 717 718
		break;
	default:
		BUG();
		return -EINVAL;
719
	}
720 721 722 723

	return  volt * 100 * mult;
}

724 725 726 727 728
static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
{
	struct tps65910_reg *pmic = rdev_get_drvdata(dev);
	int step_mv = 0, id = rdev_get_id(dev);

729
	switch (id) {
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	case TPS65911_REG_LDO1:
	case TPS65911_REG_LDO2:
	case TPS65911_REG_LDO4:
		/* The first 5 values of the selector correspond to 1V */
		if (selector < 5)
			selector = 0;
		else
			selector -= 4;

		step_mv = 50;
		break;
	case TPS65911_REG_LDO3:
	case TPS65911_REG_LDO5:
	case TPS65911_REG_LDO6:
	case TPS65911_REG_LDO7:
	case TPS65911_REG_LDO8:
		/* The first 3 values of the selector correspond to 1V */
		if (selector < 3)
			selector = 0;
		else
			selector -= 2;

		step_mv = 100;
		break;
	case TPS65910_REG_VIO:
755
		return pmic->info[id]->voltage_table[selector];
756 757 758 759 760 761 762
	default:
		return -EINVAL;
	}

	return (LDO_MIN_VOLT + selector * step_mv) * 1000;
}

763 764
/* Regulator ops (except VRTC) */
static struct regulator_ops tps65910_ops_dcdc = {
765 766 767
	.is_enabled		= regulator_is_enabled_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
768 769
	.set_mode		= tps65910_set_mode,
	.get_mode		= tps65910_get_mode,
770
	.get_voltage_sel	= tps65910_get_voltage_dcdc_sel,
771
	.set_voltage_sel	= tps65910_set_voltage_dcdc_sel,
772
	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
773
	.list_voltage		= tps65910_list_voltage_dcdc,
774
	.map_voltage		= regulator_map_voltage_ascend,
775 776 777
};

static struct regulator_ops tps65910_ops_vdd3 = {
778 779 780
	.is_enabled		= regulator_is_enabled_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
781 782 783
	.set_mode		= tps65910_set_mode,
	.get_mode		= tps65910_get_mode,
	.get_voltage		= tps65910_get_voltage_vdd3,
784
	.list_voltage		= regulator_list_voltage_table,
785
	.map_voltage		= regulator_map_voltage_ascend,
786 787
};

788 789 790 791 792 793 794 795 796 797 798 799
static struct regulator_ops tps65910_ops_vbb = {
	.is_enabled		= regulator_is_enabled_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
	.set_mode		= tps65910_set_mode,
	.get_mode		= tps65910_get_mode,
	.get_voltage_sel	= tps65910_get_voltage_sel,
	.set_voltage_sel	= tps65910_set_voltage_sel,
	.list_voltage		= regulator_list_voltage_table,
	.map_voltage		= regulator_map_voltage_iterate,
};

800
static struct regulator_ops tps65910_ops = {
801 802 803
	.is_enabled		= regulator_is_enabled_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
804 805
	.set_mode		= tps65910_set_mode,
	.get_mode		= tps65910_get_mode,
806
	.get_voltage_sel	= tps65910_get_voltage_sel,
807
	.set_voltage_sel	= tps65910_set_voltage_sel,
808
	.list_voltage		= regulator_list_voltage_table,
809
	.map_voltage		= regulator_map_voltage_ascend,
810 811
};

812
static struct regulator_ops tps65911_ops = {
813 814 815
	.is_enabled		= regulator_is_enabled_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
816 817
	.set_mode		= tps65910_set_mode,
	.get_mode		= tps65910_get_mode,
818
	.get_voltage_sel	= tps65911_get_voltage_sel,
819
	.set_voltage_sel	= tps65911_set_voltage_sel,
820
	.list_voltage		= tps65911_list_voltage,
821
	.map_voltage		= regulator_map_voltage_ascend,
822 823
};

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
static int tps65910_set_ext_sleep_config(struct tps65910_reg *pmic,
		int id, int ext_sleep_config)
{
	struct tps65910 *mfd = pmic->mfd;
	u8 regoffs = (pmic->ext_sleep_control[id] >> 8) & 0xFF;
	u8 bit_pos = (1 << pmic->ext_sleep_control[id] & 0xFF);
	int ret;

	/*
	 * Regulator can not be control from multiple external input EN1, EN2
	 * and EN3 together.
	 */
	if (ext_sleep_config & EXT_SLEEP_CONTROL) {
		int en_count;
		en_count = ((ext_sleep_config &
				TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1) != 0);
		en_count += ((ext_sleep_config &
				TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2) != 0);
		en_count += ((ext_sleep_config &
				TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3) != 0);
844 845
		en_count += ((ext_sleep_config &
				TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP) != 0);
846 847 848 849 850 851 852 853 854 855 856
		if (en_count > 1) {
			dev_err(mfd->dev,
				"External sleep control flag is not proper\n");
			return -EINVAL;
		}
	}

	pmic->board_ext_control[id] = ext_sleep_config;

	/* External EN1 control */
	if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1)
857
		ret = tps65910_reg_set_bits(mfd,
858 859
				TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
	else
860
		ret = tps65910_reg_clear_bits(mfd,
861 862 863 864 865 866 867 868 869
				TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
	if (ret < 0) {
		dev_err(mfd->dev,
			"Error in configuring external control EN1\n");
		return ret;
	}

	/* External EN2 control */
	if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2)
870
		ret = tps65910_reg_set_bits(mfd,
871 872
				TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
	else
873
		ret = tps65910_reg_clear_bits(mfd,
874 875 876 877 878 879 880 881 882 883 884
				TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
	if (ret < 0) {
		dev_err(mfd->dev,
			"Error in configuring external control EN2\n");
		return ret;
	}

	/* External EN3 control for TPS65910 LDO only */
	if ((tps65910_chip_id(mfd) == TPS65910) &&
			(id >= TPS65910_REG_VDIG1)) {
		if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3)
885
			ret = tps65910_reg_set_bits(mfd,
886 887
				TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
		else
888
			ret = tps65910_reg_clear_bits(mfd,
889 890 891 892 893 894 895 896 897 898 899
				TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
		if (ret < 0) {
			dev_err(mfd->dev,
				"Error in configuring external control EN3\n");
			return ret;
		}
	}

	/* Return if no external control is selected */
	if (!(ext_sleep_config & EXT_SLEEP_CONTROL)) {
		/* Clear all sleep controls */
900
		ret = tps65910_reg_clear_bits(mfd,
901 902
			TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
		if (!ret)
903
			ret = tps65910_reg_clear_bits(mfd,
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
				TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
		if (ret < 0)
			dev_err(mfd->dev,
				"Error in configuring SLEEP register\n");
		return ret;
	}

	/*
	 * For regulator that has separate operational and sleep register make
	 * sure that operational is used and clear sleep register to turn
	 * regulator off when external control is inactive
	 */
	if ((id == TPS65910_REG_VDD1) ||
		(id == TPS65910_REG_VDD2) ||
			((id == TPS65911_REG_VDDCTRL) &&
				(tps65910_chip_id(mfd) == TPS65911))) {
		int op_reg_add = pmic->get_ctrl_reg(id) + 1;
		int sr_reg_add = pmic->get_ctrl_reg(id) + 2;
922 923 924 925 926 927 928 929 930
		int opvsel, srvsel;

		ret = tps65910_reg_read(pmic->mfd, op_reg_add, &opvsel);
		if (ret < 0)
			return ret;
		ret = tps65910_reg_read(pmic->mfd, sr_reg_add, &srvsel);
		if (ret < 0)
			return ret;

931 932
		if (opvsel & VDD1_OP_CMD_MASK) {
			u8 reg_val = srvsel & VDD1_OP_SEL_MASK;
933 934 935

			ret = tps65910_reg_write(pmic->mfd, op_reg_add,
						 reg_val);
936 937 938 939 940 941
			if (ret < 0) {
				dev_err(mfd->dev,
					"Error in configuring op register\n");
				return ret;
			}
		}
942
		ret = tps65910_reg_write(pmic->mfd, sr_reg_add, 0);
943
		if (ret < 0) {
M
Masanari Iida 已提交
944
			dev_err(mfd->dev, "Error in setting sr register\n");
945 946 947 948
			return ret;
		}
	}

949
	ret = tps65910_reg_clear_bits(mfd,
950
			TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
951 952
	if (!ret) {
		if (ext_sleep_config & TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
953
			ret = tps65910_reg_set_bits(mfd,
954 955
				TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
		else
956
			ret = tps65910_reg_clear_bits(mfd,
957 958
				TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
	}
959 960 961
	if (ret < 0)
		dev_err(mfd->dev,
			"Error in configuring SLEEP register\n");
962

963 964 965
	return ret;
}

966 967 968
#ifdef CONFIG_OF

static struct of_regulator_match tps65910_matches[] = {
969 970 971 972 973 974 975 976 977 978 979 980 981
	{ .name = "vrtc",	.driver_data = (void *) &tps65910_regs[0] },
	{ .name = "vio",	.driver_data = (void *) &tps65910_regs[1] },
	{ .name = "vdd1",	.driver_data = (void *) &tps65910_regs[2] },
	{ .name = "vdd2",	.driver_data = (void *) &tps65910_regs[3] },
	{ .name = "vdd3",	.driver_data = (void *) &tps65910_regs[4] },
	{ .name = "vdig1",	.driver_data = (void *) &tps65910_regs[5] },
	{ .name = "vdig2",	.driver_data = (void *) &tps65910_regs[6] },
	{ .name = "vpll",	.driver_data = (void *) &tps65910_regs[7] },
	{ .name = "vdac",	.driver_data = (void *) &tps65910_regs[8] },
	{ .name = "vaux1",	.driver_data = (void *) &tps65910_regs[9] },
	{ .name = "vaux2",	.driver_data = (void *) &tps65910_regs[10] },
	{ .name = "vaux33",	.driver_data = (void *) &tps65910_regs[11] },
	{ .name = "vmmc",	.driver_data = (void *) &tps65910_regs[12] },
982
	{ .name = "vbb",	.driver_data = (void *) &tps65910_regs[13] },
983 984 985
};

static struct of_regulator_match tps65911_matches[] = {
986 987 988 989 990 991 992 993 994 995 996 997 998
	{ .name = "vrtc",	.driver_data = (void *) &tps65911_regs[0] },
	{ .name = "vio",	.driver_data = (void *) &tps65911_regs[1] },
	{ .name = "vdd1",	.driver_data = (void *) &tps65911_regs[2] },
	{ .name = "vdd2",	.driver_data = (void *) &tps65911_regs[3] },
	{ .name = "vddctrl",	.driver_data = (void *) &tps65911_regs[4] },
	{ .name = "ldo1",	.driver_data = (void *) &tps65911_regs[5] },
	{ .name = "ldo2",	.driver_data = (void *) &tps65911_regs[6] },
	{ .name = "ldo3",	.driver_data = (void *) &tps65911_regs[7] },
	{ .name = "ldo4",	.driver_data = (void *) &tps65911_regs[8] },
	{ .name = "ldo5",	.driver_data = (void *) &tps65911_regs[9] },
	{ .name = "ldo6",	.driver_data = (void *) &tps65911_regs[10] },
	{ .name = "ldo7",	.driver_data = (void *) &tps65911_regs[11] },
	{ .name = "ldo8",	.driver_data = (void *) &tps65911_regs[12] },
999 1000 1001
};

static struct tps65910_board *tps65910_parse_dt_reg_data(
1002 1003
		struct platform_device *pdev,
		struct of_regulator_match **tps65910_reg_matches)
1004 1005 1006
{
	struct tps65910_board *pmic_plat_data;
	struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
A
Axel Lin 已提交
1007
	struct device_node *np, *regulators;
1008 1009 1010 1011 1012 1013
	struct of_regulator_match *matches;
	unsigned int prop;
	int idx = 0, ret, count;

	pmic_plat_data = devm_kzalloc(&pdev->dev, sizeof(*pmic_plat_data),
					GFP_KERNEL);
1014
	if (!pmic_plat_data)
1015 1016
		return NULL;

1017
	np = pdev->dev.parent->of_node;
1018
	regulators = of_get_child_by_name(np, "regulators");
1019 1020 1021 1022
	if (!regulators) {
		dev_err(&pdev->dev, "regulator node not found\n");
		return NULL;
	}
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

	switch (tps65910_chip_id(tps65910)) {
	case TPS65910:
		count = ARRAY_SIZE(tps65910_matches);
		matches = tps65910_matches;
		break;
	case TPS65911:
		count = ARRAY_SIZE(tps65911_matches);
		matches = tps65911_matches;
		break;
	default:
A
Axel Lin 已提交
1034
		of_node_put(regulators);
1035
		dev_err(&pdev->dev, "Invalid tps chip version\n");
1036 1037 1038
		return NULL;
	}

1039
	ret = of_regulator_match(&pdev->dev, regulators, matches, count);
A
Axel Lin 已提交
1040
	of_node_put(regulators);
1041 1042 1043 1044 1045 1046
	if (ret < 0) {
		dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
			ret);
		return NULL;
	}

1047 1048
	*tps65910_reg_matches = matches;

1049
	for (idx = 0; idx < count; idx++) {
1050
		if (!matches[idx].of_node)
1051 1052 1053 1054 1055 1056 1057 1058 1059
			continue;

		pmic_plat_data->tps65910_pmic_init_data[idx] =
							matches[idx].init_data;

		ret = of_property_read_u32(matches[idx].of_node,
				"ti,regulator-ext-sleep-control", &prop);
		if (!ret)
			pmic_plat_data->regulator_ext_sleep_control[idx] = prop;
1060

1061 1062 1063 1064 1065 1066
	}

	return pmic_plat_data;
}
#else
static inline struct tps65910_board *tps65910_parse_dt_reg_data(
1067 1068
			struct platform_device *pdev,
			struct of_regulator_match **tps65910_reg_matches)
1069
{
1070
	*tps65910_reg_matches = NULL;
1071
	return NULL;
1072 1073 1074
}
#endif

1075
static int tps65910_probe(struct platform_device *pdev)
1076 1077
{
	struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
1078
	struct regulator_config config = { };
1079
	struct tps_info *info;
1080 1081 1082
	struct regulator_dev *rdev;
	struct tps65910_reg *pmic;
	struct tps65910_board *pmic_plat_data;
1083
	struct of_regulator_match *tps65910_reg_matches = NULL;
1084 1085 1086
	int i, err;

	pmic_plat_data = dev_get_platdata(tps65910->dev);
1087
	if (!pmic_plat_data && tps65910->dev->of_node)
1088 1089
		pmic_plat_data = tps65910_parse_dt_reg_data(pdev,
						&tps65910_reg_matches);
1090

1091 1092
	if (!pmic_plat_data) {
		dev_err(&pdev->dev, "Platform data not found\n");
1093
		return -EINVAL;
1094
	}
1095

1096
	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1097
	if (!pmic)
1098 1099 1100 1101 1102 1103
		return -ENOMEM;

	pmic->mfd = tps65910;
	platform_set_drvdata(pdev, pmic);

	/* Give control of all register to control port */
1104
	tps65910_reg_set_bits(pmic->mfd, TPS65910_DEVCTRL,
1105 1106
				DEVCTRL_SR_CTL_I2C_SEL_MASK);

1107
	switch (tps65910_chip_id(tps65910)) {
1108 1109
	case TPS65910:
		pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
1110
		pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
1111
		pmic->ext_sleep_control = tps65910_ext_sleep_control;
1112
		info = tps65910_regs;
1113
		break;
1114 1115
	case TPS65911:
		pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
1116
		pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
1117
		pmic->ext_sleep_control = tps65911_ext_sleep_control;
1118
		info = tps65911_regs;
1119
		break;
1120
	default:
1121
		dev_err(&pdev->dev, "Invalid tps chip version\n");
1122 1123 1124
		return -ENODEV;
	}

1125
	pmic->desc = devm_kzalloc(&pdev->dev, pmic->num_regulators *
1126
			sizeof(struct regulator_desc), GFP_KERNEL);
1127
	if (!pmic->desc)
1128
		return -ENOMEM;
1129

1130
	pmic->info = devm_kzalloc(&pdev->dev, pmic->num_regulators *
1131
			sizeof(struct tps_info *), GFP_KERNEL);
1132
	if (!pmic->info)
1133
		return -ENOMEM;
1134

1135
	pmic->rdev = devm_kzalloc(&pdev->dev, pmic->num_regulators *
1136
			sizeof(struct regulator_dev *), GFP_KERNEL);
1137
	if (!pmic->rdev)
1138
		return -ENOMEM;
1139

1140 1141
	for (i = 0; i < pmic->num_regulators && i < TPS65910_NUM_REGS;
			i++, info++) {
1142 1143 1144 1145
		/* Register the regulators */
		pmic->info[i] = info;

		pmic->desc[i].name = info->name;
1146
		pmic->desc[i].supply_name = info->vin_name;
1147
		pmic->desc[i].id = i;
1148
		pmic->desc[i].n_voltages = info->n_voltages;
1149
		pmic->desc[i].enable_time = info->enable_time_us;
1150

1151
		if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
1152
			pmic->desc[i].ops = &tps65910_ops_dcdc;
1153 1154
			pmic->desc[i].n_voltages = VDD1_2_NUM_VOLT_FINE *
							VDD1_2_NUM_VOLT_COARSE;
1155
			pmic->desc[i].ramp_delay = 12500;
1156
		} else if (i == TPS65910_REG_VDD3) {
1157
			if (tps65910_chip_id(tps65910) == TPS65910) {
1158
				pmic->desc[i].ops = &tps65910_ops_vdd3;
1159
				pmic->desc[i].volt_table = info->voltage_table;
1160
			} else {
1161
				pmic->desc[i].ops = &tps65910_ops_dcdc;
1162 1163
				pmic->desc[i].ramp_delay = 5000;
			}
1164 1165 1166 1167
		} else if (i == TPS65910_REG_VBB &&
				tps65910_chip_id(tps65910) == TPS65910) {
			pmic->desc[i].ops = &tps65910_ops_vbb;
			pmic->desc[i].volt_table = info->voltage_table;
1168
		} else {
1169
			if (tps65910_chip_id(tps65910) == TPS65910) {
1170
				pmic->desc[i].ops = &tps65910_ops;
1171 1172
				pmic->desc[i].volt_table = info->voltage_table;
			} else {
1173
				pmic->desc[i].ops = &tps65911_ops;
1174
			}
1175
		}
1176

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
		err = tps65910_set_ext_sleep_config(pmic, i,
				pmic_plat_data->regulator_ext_sleep_control[i]);
		/*
		 * Failing on regulator for configuring externally control
		 * is not a serious issue, just throw warning.
		 */
		if (err < 0)
			dev_warn(tps65910->dev,
				"Failed to initialise ext control config\n");

1187 1188
		pmic->desc[i].type = REGULATOR_VOLTAGE;
		pmic->desc[i].owner = THIS_MODULE;
1189
		pmic->desc[i].enable_reg = pmic->get_ctrl_reg(i);
1190
		pmic->desc[i].enable_mask = TPS65910_SUPPLY_STATE_ENABLED;
1191

1192
		config.dev = tps65910->dev;
1193
		config.init_data = pmic_plat_data->tps65910_pmic_init_data[i];
1194
		config.driver_data = pmic;
1195
		config.regmap = tps65910->regmap;
1196

1197 1198
		if (tps65910_reg_matches)
			config.of_node = tps65910_reg_matches[i].of_node;
1199

1200 1201
		rdev = devm_regulator_register(&pdev->dev, &pmic->desc[i],
					       &config);
1202 1203 1204 1205
		if (IS_ERR(rdev)) {
			dev_err(tps65910->dev,
				"failed to register %s regulator\n",
				pdev->name);
1206
			return PTR_ERR(rdev);
1207 1208 1209 1210 1211 1212 1213 1214
		}

		/* Save regulator for cleanup */
		pmic->rdev[i] = rdev;
	}
	return 0;
}

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
static void tps65910_shutdown(struct platform_device *pdev)
{
	struct tps65910_reg *pmic = platform_get_drvdata(pdev);
	int i;

	/*
	 * Before bootloader jumps to kernel, it makes sure that required
	 * external control signals are in desired state so that given rails
	 * can be configure accordingly.
	 * If rails are configured to be controlled from external control
	 * then before shutting down/rebooting the system, the external
	 * control configuration need to be remove from the rails so that
	 * its output will be available as per register programming even
	 * if external controls are removed. This is require when the POR
	 * value of the control signals are not in active state and before
	 * bootloader initializes it, the system requires the rail output
	 * to be active for booting.
	 */
	for (i = 0; i < pmic->num_regulators; i++) {
		int err;
		if (!pmic->rdev[i])
			continue;

		err = tps65910_set_ext_sleep_config(pmic, i, 0);
		if (err < 0)
			dev_err(&pdev->dev,
				"Error in clearing external control\n");
	}
}

1245 1246 1247 1248 1249 1250
static struct platform_driver tps65910_driver = {
	.driver = {
		.name = "tps65910-pmic",
		.owner = THIS_MODULE,
	},
	.probe = tps65910_probe,
1251
	.shutdown = tps65910_shutdown,
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
};

static int __init tps65910_init(void)
{
	return platform_driver_register(&tps65910_driver);
}
subsys_initcall(tps65910_init);

static void __exit tps65910_cleanup(void)
{
	platform_driver_unregister(&tps65910_driver);
}
module_exit(tps65910_cleanup);

MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1267
MODULE_DESCRIPTION("TPS65910/TPS65911 voltage regulator driver");
1268 1269
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:tps65910-pmic");