ab8500.c 26.0 KB
Newer Older
1 2 3 4 5
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * License Terms: GNU General Public License v2
 *
6 7
 * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
 *          Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
8 9 10
 *
 * AB8500 peripheral regulators
 *
11
 * AB8500 supports the following regulators:
12
 *   VAUX1/2/3, VINTCORE, VTVOUT, VUSB, VAUDIO, VAMIC1/2, VDMIC, VANA
13 14 15
 */
#include <linux/init.h>
#include <linux/kernel.h>
16
#include <linux/module.h>
17 18
#include <linux/err.h>
#include <linux/platform_device.h>
19
#include <linux/mfd/abx500.h>
20
#include <linux/mfd/abx500/ab8500.h>
21 22
#include <linux/of.h>
#include <linux/regulator/of_regulator.h>
23 24 25
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/ab8500.h>
26
#include <linux/slab.h>
27 28 29

/**
 * struct ab8500_regulator_info - ab8500 regulator information
30
 * @dev: device pointer
31 32
 * @desc: regulator description
 * @regulator_dev: regulator device
33
 * @is_enabled: status of regulator (on/off)
34
 * @load_lp_uA: maximum load in idle (low power) mode
35
 * @update_bank: bank to control on/off
36
 * @update_reg: register to control on/off
37 38 39 40
 * @update_mask: mask to enable/disable and set mode of regulator
 * @update_val: bits holding the regulator current mode
 * @update_val_idle: bits to enable the regulator in idle (low power) mode
 * @update_val_normal: bits to enable the regulator in normal (high power) mode
41
 * @voltage_bank: bank to control regulator voltage
42 43
 * @voltage_reg: register to control regulator voltage
 * @voltage_mask: mask to control regulator voltage
44
 * @voltage_shift: shift to control regulator voltage
45
 * @delay: startup/set voltage delay in us
46 47 48 49 50
 */
struct ab8500_regulator_info {
	struct device		*dev;
	struct regulator_desc	desc;
	struct regulator_dev	*regulator;
51
	bool is_enabled;
52
	int load_lp_uA;
53 54
	u8 update_bank;
	u8 update_reg;
55
	u8 update_mask;
56 57 58
	u8 update_val;
	u8 update_val_idle;
	u8 update_val_normal;
59 60 61
	u8 voltage_bank;
	u8 voltage_reg;
	u8 voltage_mask;
62
	u8 voltage_shift;
63
	unsigned int delay;
64 65 66
};

/* voltage tables for the vauxn/vintcore supplies */
67
static const unsigned int ldo_vauxn_voltages[] = {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	1100000,
	1200000,
	1300000,
	1400000,
	1500000,
	1800000,
	1850000,
	1900000,
	2500000,
	2650000,
	2700000,
	2750000,
	2800000,
	2900000,
	3000000,
	3300000,
};

86
static const unsigned int ldo_vaux3_voltages[] = {
87 88 89 90 91 92 93 94 95 96
	1200000,
	1500000,
	1800000,
	2100000,
	2500000,
	2750000,
	2790000,
	2910000,
};

97
static const unsigned int ldo_vintcore_voltages[] = {
98 99 100 101 102 103 104 105 106 107 108
	1200000,
	1225000,
	1250000,
	1275000,
	1300000,
	1325000,
	1350000,
};

static int ab8500_regulator_enable(struct regulator_dev *rdev)
{
109
	int ret;
110 111
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

112 113
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
114
		return -EINVAL;
115
	}
116

117
	ret = abx500_mask_and_set_register_interruptible(info->dev,
118
		info->update_bank, info->update_reg,
119
		info->update_mask, info->update_val);
120
	if (ret < 0) {
121 122
		dev_err(rdev_get_dev(rdev),
			"couldn't set enable bits for regulator\n");
123 124
		return ret;
	}
125

126 127
	info->is_enabled = true;

128 129 130
	dev_vdbg(rdev_get_dev(rdev),
		"%s-enable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
		info->desc.name, info->update_bank, info->update_reg,
131
		info->update_mask, info->update_val);
132

133 134 135 136 137
	return ret;
}

static int ab8500_regulator_disable(struct regulator_dev *rdev)
{
138
	int ret;
139 140
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

141 142
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
143
		return -EINVAL;
144
	}
145

146
	ret = abx500_mask_and_set_register_interruptible(info->dev,
147 148
		info->update_bank, info->update_reg,
		info->update_mask, 0x0);
149
	if (ret < 0) {
150 151
		dev_err(rdev_get_dev(rdev),
			"couldn't set disable bits for regulator\n");
152 153
		return ret;
	}
154

155 156
	info->is_enabled = false;

157 158 159 160 161
	dev_vdbg(rdev_get_dev(rdev),
		"%s-disable (bank, reg, mask, value): 0x%x, 0x%x, 0x%x, 0x%x\n",
		info->desc.name, info->update_bank, info->update_reg,
		info->update_mask, 0x0);

162 163 164
	return ret;
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
static unsigned int ab8500_regulator_get_optimum_mode(
		struct regulator_dev *rdev, int input_uV,
		int output_uV, int load_uA)
{
	unsigned int mode;

	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
		return -EINVAL;
	}

	if (load_uA <= info->load_lp_uA)
		mode = REGULATOR_MODE_IDLE;
	else
		mode = REGULATOR_MODE_NORMAL;

	return mode;
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static int ab8500_regulator_set_mode(struct regulator_dev *rdev,
				     unsigned int mode)
{
	int ret = 0;

	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
		return -EINVAL;
	}

	switch (mode) {
	case REGULATOR_MODE_NORMAL:
		info->update_val = info->update_val_normal;
		break;
	case REGULATOR_MODE_IDLE:
		info->update_val = info->update_val_idle;
		break;
	default:
		return -EINVAL;
	}

	if (info->is_enabled) {
		ret = abx500_mask_and_set_register_interruptible(info->dev,
			info->update_bank, info->update_reg,
			info->update_mask, info->update_val);
		if (ret < 0)
			dev_err(rdev_get_dev(rdev),
				"couldn't set regulator mode\n");
216 217 218 219 220 221

		dev_vdbg(rdev_get_dev(rdev),
			"%s-set_mode (bank, reg, mask, value): "
			"0x%x, 0x%x, 0x%x, 0x%x\n",
			info->desc.name, info->update_bank, info->update_reg,
			info->update_mask, info->update_val);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	}

	return ret;
}

static unsigned int ab8500_regulator_get_mode(struct regulator_dev *rdev)
{
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
	int ret;

	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
		return -EINVAL;
	}

	if (info->update_val == info->update_val_normal)
		ret = REGULATOR_MODE_NORMAL;
	else if (info->update_val == info->update_val_idle)
		ret = REGULATOR_MODE_IDLE;
	else
		ret = -EINVAL;

	return ret;
}

247 248
static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
{
249
	int ret;
250
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
251
	u8 regval;
252

253 254
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
255
		return -EINVAL;
256
	}
257

258
	ret = abx500_get_register_interruptible(info->dev,
259
		info->update_bank, info->update_reg, &regval);
260 261 262 263 264 265
	if (ret < 0) {
		dev_err(rdev_get_dev(rdev),
			"couldn't read 0x%x register\n", info->update_reg);
		return ret;
	}

266 267 268 269 270 271 272
	dev_vdbg(rdev_get_dev(rdev),
		"%s-is_enabled (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
		" 0x%x\n",
		info->desc.name, info->update_bank, info->update_reg,
		info->update_mask, regval);

	if (regval & info->update_mask)
273
		info->is_enabled = true;
274
	else
275 276 277
		info->is_enabled = false;

	return info->is_enabled;
278 279
}

280
static int ab8500_regulator_get_voltage_sel(struct regulator_dev *rdev)
281
{
282
	int ret, val;
283
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
284
	u8 regval;
285

286 287
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
288
		return -EINVAL;
289
	}
290

291 292
	ret = abx500_get_register_interruptible(info->dev,
			info->voltage_bank, info->voltage_reg, &regval);
293 294 295 296 297 298
	if (ret < 0) {
		dev_err(rdev_get_dev(rdev),
			"couldn't read voltage reg for regulator\n");
		return ret;
	}

299
	dev_vdbg(rdev_get_dev(rdev),
300 301 302 303 304
		"%s-get_voltage (bank, reg, mask, shift, value): "
		"0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
		info->desc.name, info->voltage_bank,
		info->voltage_reg, info->voltage_mask,
		info->voltage_shift, regval);
305 306

	val = regval & info->voltage_mask;
307
	return val >> info->voltage_shift;
308 309
}

310 311
static int ab8500_regulator_set_voltage_sel(struct regulator_dev *rdev,
					    unsigned selector)
312
{
313
	int ret;
314
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
315
	u8 regval;
316

317 318
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
319
		return -EINVAL;
320
	}
321 322

	/* set the registers for the request */
323
	regval = (u8)selector << info->voltage_shift;
324
	ret = abx500_mask_and_set_register_interruptible(info->dev,
325 326
			info->voltage_bank, info->voltage_reg,
			info->voltage_mask, regval);
327 328 329 330
	if (ret < 0)
		dev_err(rdev_get_dev(rdev),
		"couldn't set voltage reg for regulator\n");

331 332 333 334 335 336
	dev_vdbg(rdev_get_dev(rdev),
		"%s-set_voltage (bank, reg, mask, value): 0x%x, 0x%x, 0x%x,"
		" 0x%x\n",
		info->desc.name, info->voltage_bank, info->voltage_reg,
		info->voltage_mask, regval);

337 338 339
	return ret;
}

340 341 342 343 344 345 346 347 348
static int ab8500_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
					     unsigned int old_sel,
					     unsigned int new_sel)
{
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

	return info->delay;
}

349 350 351 352 353 354 355 356 357 358 359
static struct regulator_ops ab8500_regulator_volt_mode_ops = {
	.enable			= ab8500_regulator_enable,
	.disable		= ab8500_regulator_disable,
	.is_enabled		= ab8500_regulator_is_enabled,
	.get_optimum_mode	= ab8500_regulator_get_optimum_mode,
	.set_mode		= ab8500_regulator_set_mode,
	.get_mode		= ab8500_regulator_get_mode,
	.get_voltage_sel 	= ab8500_regulator_get_voltage_sel,
	.set_voltage_sel	= ab8500_regulator_set_voltage_sel,
	.list_voltage		= regulator_list_voltage_table,
	.set_voltage_time_sel	= ab8500_regulator_set_voltage_time_sel,
360 361
};

362 363 364 365 366 367 368 369
static struct regulator_ops ab8500_regulator_mode_ops = {
	.enable			= ab8500_regulator_enable,
	.disable		= ab8500_regulator_disable,
	.is_enabled		= ab8500_regulator_is_enabled,
	.get_optimum_mode	= ab8500_regulator_get_optimum_mode,
	.set_mode		= ab8500_regulator_set_mode,
	.get_mode		= ab8500_regulator_get_mode,
	.get_voltage_sel 	= ab8500_regulator_get_voltage_sel,
370
	.list_voltage		= regulator_list_voltage_linear,
371 372 373 374 375 376 377
};

static struct regulator_ops ab8500_regulator_ops = {
	.enable			= ab8500_regulator_enable,
	.disable		= ab8500_regulator_disable,
	.is_enabled		= ab8500_regulator_is_enabled,
	.get_voltage_sel 	= ab8500_regulator_get_voltage_sel,
378
	.list_voltage		= regulator_list_voltage_linear,
379 380
};

381 382
static struct ab8500_regulator_info
		ab8500_regulator_info[AB8500_NUM_REGULATORS] = {
383
	/*
384 385 386
	 * Variable Voltage Regulators
	 *   name, min mV, max mV,
	 *   update bank, reg, mask, enable val
387
	 *   volt bank, reg, mask
388
	 */
389 390 391
	[AB8500_LDO_AUX1] = {
		.desc = {
			.name		= "LDO-AUX1",
392
			.ops		= &ab8500_regulator_volt_mode_ops,
393 394 395 396
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_AUX1,
			.owner		= THIS_MODULE,
			.n_voltages	= ARRAY_SIZE(ldo_vauxn_voltages),
397
			.volt_table	= ldo_vauxn_voltages,
398
			.enable_time	= 200,
399
		},
400
		.load_lp_uA		= 5000,
401 402 403
		.update_bank		= 0x04,
		.update_reg		= 0x09,
		.update_mask		= 0x03,
404 405 406
		.update_val		= 0x01,
		.update_val_idle	= 0x03,
		.update_val_normal	= 0x01,
407 408 409 410 411 412 413
		.voltage_bank		= 0x04,
		.voltage_reg		= 0x1f,
		.voltage_mask		= 0x0f,
	},
	[AB8500_LDO_AUX2] = {
		.desc = {
			.name		= "LDO-AUX2",
414
			.ops		= &ab8500_regulator_volt_mode_ops,
415 416 417 418
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_AUX2,
			.owner		= THIS_MODULE,
			.n_voltages	= ARRAY_SIZE(ldo_vauxn_voltages),
419
			.volt_table	= ldo_vauxn_voltages,
420
			.enable_time	= 200,
421
		},
422
		.load_lp_uA		= 5000,
423 424 425
		.update_bank		= 0x04,
		.update_reg		= 0x09,
		.update_mask		= 0x0c,
426 427 428
		.update_val		= 0x04,
		.update_val_idle	= 0x0c,
		.update_val_normal	= 0x04,
429 430 431 432 433 434 435
		.voltage_bank		= 0x04,
		.voltage_reg		= 0x20,
		.voltage_mask		= 0x0f,
	},
	[AB8500_LDO_AUX3] = {
		.desc = {
			.name		= "LDO-AUX3",
436
			.ops		= &ab8500_regulator_volt_mode_ops,
437 438 439 440
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_AUX3,
			.owner		= THIS_MODULE,
			.n_voltages	= ARRAY_SIZE(ldo_vaux3_voltages),
441
			.volt_table	= ldo_vaux3_voltages,
442
			.enable_time	= 450,
443
		},
444
		.load_lp_uA		= 5000,
445 446 447
		.update_bank		= 0x04,
		.update_reg		= 0x0a,
		.update_mask		= 0x03,
448 449 450
		.update_val		= 0x01,
		.update_val_idle	= 0x03,
		.update_val_normal	= 0x01,
451 452 453 454 455 456 457
		.voltage_bank		= 0x04,
		.voltage_reg		= 0x21,
		.voltage_mask		= 0x07,
	},
	[AB8500_LDO_INTCORE] = {
		.desc = {
			.name		= "LDO-INTCORE",
458
			.ops		= &ab8500_regulator_volt_mode_ops,
459 460 461 462
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_INTCORE,
			.owner		= THIS_MODULE,
			.n_voltages	= ARRAY_SIZE(ldo_vintcore_voltages),
463
			.volt_table	= ldo_vintcore_voltages,
464
			.enable_time	= 750,
465
		},
466
		.load_lp_uA		= 5000,
467 468 469
		.update_bank		= 0x03,
		.update_reg		= 0x80,
		.update_mask		= 0x44,
470 471 472
		.update_val		= 0x04,
		.update_val_idle	= 0x44,
		.update_val_normal	= 0x04,
473 474 475
		.voltage_bank		= 0x03,
		.voltage_reg		= 0x80,
		.voltage_mask		= 0x38,
476
		.voltage_shift		= 3,
477
	},
478 479

	/*
480 481 482
	 * Fixed Voltage Regulators
	 *   name, fixed mV,
	 *   update bank, reg, mask, enable val
483
	 */
484 485 486
	[AB8500_LDO_TVOUT] = {
		.desc = {
			.name		= "LDO-TVOUT",
487
			.ops		= &ab8500_regulator_mode_ops,
488 489 490 491
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_TVOUT,
			.owner		= THIS_MODULE,
			.n_voltages	= 1,
492
			.min_uV		= 2000000,
493
			.enable_time	= 10000,
494
		},
495
		.delay			= 10000,
496
		.load_lp_uA		= 1000,
497 498 499
		.update_bank		= 0x03,
		.update_reg		= 0x80,
		.update_mask		= 0x82,
500
		.update_val		= 0x02,
501 502
		.update_val_idle	= 0x82,
		.update_val_normal	= 0x02,
503
	},
A
Axel Lin 已提交
504 505 506 507

	/*
	 * Regulators with fixed voltage and normal mode
	 */
508 509 510
	[AB8500_LDO_USB] = {
		.desc = {
			.name           = "LDO-USB",
A
Axel Lin 已提交
511
			.ops            = &ab8500_regulator_ops,
512 513 514 515
			.type           = REGULATOR_VOLTAGE,
			.id             = AB8500_LDO_USB,
			.owner          = THIS_MODULE,
			.n_voltages     = 1,
516
			.min_uV		= 3300000,
517
			.enable_time	= 150,
518 519 520 521 522
		},
		.update_bank            = 0x03,
		.update_reg             = 0x82,
		.update_mask            = 0x03,
	},
523 524 525
	[AB8500_LDO_AUDIO] = {
		.desc = {
			.name		= "LDO-AUDIO",
526
			.ops		= &ab8500_regulator_ops,
527 528 529 530
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_AUDIO,
			.owner		= THIS_MODULE,
			.n_voltages	= 1,
531
			.min_uV		= 2000000,
532
			.enable_time	= 140,
533 534 535 536
		},
		.update_bank		= 0x03,
		.update_reg		= 0x83,
		.update_mask		= 0x02,
537
		.update_val		= 0x02,
538 539 540 541
	},
	[AB8500_LDO_ANAMIC1] = {
		.desc = {
			.name		= "LDO-ANAMIC1",
542
			.ops		= &ab8500_regulator_ops,
543 544 545 546
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_ANAMIC1,
			.owner		= THIS_MODULE,
			.n_voltages	= 1,
547
			.min_uV		= 2050000,
548
			.enable_time	= 500,
549 550 551 552
		},
		.update_bank		= 0x03,
		.update_reg		= 0x83,
		.update_mask		= 0x08,
553
		.update_val		= 0x08,
554 555 556 557
	},
	[AB8500_LDO_ANAMIC2] = {
		.desc = {
			.name		= "LDO-ANAMIC2",
558
			.ops		= &ab8500_regulator_ops,
559 560 561 562
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_ANAMIC2,
			.owner		= THIS_MODULE,
			.n_voltages	= 1,
563
			.min_uV		= 2050000,
564
			.enable_time	= 500,
565 566 567 568
		},
		.update_bank		= 0x03,
		.update_reg		= 0x83,
		.update_mask		= 0x10,
569
		.update_val		= 0x10,
570 571 572 573
	},
	[AB8500_LDO_DMIC] = {
		.desc = {
			.name		= "LDO-DMIC",
574
			.ops		= &ab8500_regulator_ops,
575 576 577 578
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_DMIC,
			.owner		= THIS_MODULE,
			.n_voltages	= 1,
579
			.min_uV		= 1800000,
580
			.enable_time	= 420,
581 582 583 584
		},
		.update_bank		= 0x03,
		.update_reg		= 0x83,
		.update_mask		= 0x04,
585
		.update_val		= 0x04,
586
	},
587 588 589 590

	/*
	 * Regulators with fixed voltage and normal/idle modes
	 */
591 592 593
	[AB8500_LDO_ANA] = {
		.desc = {
			.name		= "LDO-ANA",
594
			.ops		= &ab8500_regulator_mode_ops,
595 596 597 598
			.type		= REGULATOR_VOLTAGE,
			.id		= AB8500_LDO_ANA,
			.owner		= THIS_MODULE,
			.n_voltages	= 1,
599
			.min_uV		= 1200000,
600
			.enable_time	= 140,
601
		},
602
		.load_lp_uA		= 1000,
603 604 605
		.update_bank		= 0x04,
		.update_reg		= 0x06,
		.update_mask		= 0x0c,
606
		.update_val		= 0x04,
607 608
		.update_val_idle	= 0x0c,
		.update_val_normal	= 0x04,
609 610 611
	},


612 613
};

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
struct ab8500_reg_init {
	u8 bank;
	u8 addr;
	u8 mask;
};

#define REG_INIT(_id, _bank, _addr, _mask)	\
	[_id] = {				\
		.bank = _bank,			\
		.addr = _addr,			\
		.mask = _mask,			\
	}

static struct ab8500_reg_init ab8500_reg_init[] = {
	/*
629 630 631 632 633 634 635 636
	 * 0x03, VarmRequestCtrl
	 * 0x0c, VapeRequestCtrl
	 * 0x30, Vsmps1RequestCtrl
	 * 0xc0, Vsmps2RequestCtrl
	 */
	REG_INIT(AB8500_REGUREQUESTCTRL1,	0x03, 0x03, 0xff),
	/*
	 * 0x03, Vsmps3RequestCtrl
637
	 * 0x0c, VpllRequestCtrl
638
	 * 0x30, VanaRequestCtrl
639 640
	 * 0xc0, VextSupply1RequestCtrl
	 */
641
	REG_INIT(AB8500_REGUREQUESTCTRL2,	0x03, 0x04, 0xff),
642 643 644 645 646 647 648 649 650 651 652 653 654
	/*
	 * 0x03, VextSupply2RequestCtrl
	 * 0x0c, VextSupply3RequestCtrl
	 * 0x30, Vaux1RequestCtrl
	 * 0xc0, Vaux2RequestCtrl
	 */
	REG_INIT(AB8500_REGUREQUESTCTRL3,	0x03, 0x05, 0xff),
	/*
	 * 0x03, Vaux3RequestCtrl
	 * 0x04, SwHPReq
	 */
	REG_INIT(AB8500_REGUREQUESTCTRL4,	0x03, 0x06, 0x07),
	/*
655 656 657
	 * 0x01, Vsmps1SysClkReq1HPValid
	 * 0x02, Vsmps2SysClkReq1HPValid
	 * 0x04, Vsmps3SysClkReq1HPValid
658
	 * 0x08, VanaSysClkReq1HPValid
659
	 * 0x10, VpllSysClkReq1HPValid
660 661 662 663
	 * 0x20, Vaux1SysClkReq1HPValid
	 * 0x40, Vaux2SysClkReq1HPValid
	 * 0x80, Vaux3SysClkReq1HPValid
	 */
664
	REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID1,	0x03, 0x07, 0xff),
665
	/*
666 667 668 669
	 * 0x01, VapeSysClkReq1HPValid
	 * 0x02, VarmSysClkReq1HPValid
	 * 0x04, VbbSysClkReq1HPValid
	 * 0x08, VmodSysClkReq1HPValid
670 671 672 673
	 * 0x10, VextSupply1SysClkReq1HPValid
	 * 0x20, VextSupply2SysClkReq1HPValid
	 * 0x40, VextSupply3SysClkReq1HPValid
	 */
674
	REG_INIT(AB8500_REGUSYSCLKREQ1HPVALID2,	0x03, 0x08, 0x7f),
675
	/*
676 677 678
	 * 0x01, Vsmps1HwHPReq1Valid
	 * 0x02, Vsmps2HwHPReq1Valid
	 * 0x04, Vsmps3HwHPReq1Valid
679
	 * 0x08, VanaHwHPReq1Valid
680
	 * 0x10, VpllHwHPReq1Valid
681 682 683 684
	 * 0x20, Vaux1HwHPReq1Valid
	 * 0x40, Vaux2HwHPReq1Valid
	 * 0x80, Vaux3HwHPReq1Valid
	 */
685
	REG_INIT(AB8500_REGUHWHPREQ1VALID1,	0x03, 0x09, 0xff),
686 687 688 689
	/*
	 * 0x01, VextSupply1HwHPReq1Valid
	 * 0x02, VextSupply2HwHPReq1Valid
	 * 0x04, VextSupply3HwHPReq1Valid
690
	 * 0x08, VmodHwHPReq1Valid
691
	 */
692
	REG_INIT(AB8500_REGUHWHPREQ1VALID2,	0x03, 0x0a, 0x0f),
693
	/*
694 695 696
	 * 0x01, Vsmps1HwHPReq2Valid
	 * 0x02, Vsmps2HwHPReq2Valid
	 * 0x03, Vsmps3HwHPReq2Valid
697
	 * 0x08, VanaHwHPReq2Valid
698
	 * 0x10, VpllHwHPReq2Valid
699 700 701 702
	 * 0x20, Vaux1HwHPReq2Valid
	 * 0x40, Vaux2HwHPReq2Valid
	 * 0x80, Vaux3HwHPReq2Valid
	 */
703
	REG_INIT(AB8500_REGUHWHPREQ2VALID1,	0x03, 0x0b, 0xff),
704 705 706 707
	/*
	 * 0x01, VextSupply1HwHPReq2Valid
	 * 0x02, VextSupply2HwHPReq2Valid
	 * 0x04, VextSupply3HwHPReq2Valid
708
	 * 0x08, VmodHwHPReq2Valid
709
	 */
710
	REG_INIT(AB8500_REGUHWHPREQ2VALID2,	0x03, 0x0c, 0x0f),
711
	/*
712 713 714 715 716
	 * 0x01, VapeSwHPReqValid
	 * 0x02, VarmSwHPReqValid
	 * 0x04, Vsmps1SwHPReqValid
	 * 0x08, Vsmps2SwHPReqValid
	 * 0x10, Vsmps3SwHPReqValid
717
	 * 0x20, VanaSwHPReqValid
718
	 * 0x40, VpllSwHPReqValid
719 720
	 * 0x80, Vaux1SwHPReqValid
	 */
721
	REG_INIT(AB8500_REGUSWHPREQVALID1,	0x03, 0x0d, 0xff),
722 723 724 725 726 727
	/*
	 * 0x01, Vaux2SwHPReqValid
	 * 0x02, Vaux3SwHPReqValid
	 * 0x04, VextSupply1SwHPReqValid
	 * 0x08, VextSupply2SwHPReqValid
	 * 0x10, VextSupply3SwHPReqValid
728
	 * 0x20, VmodSwHPReqValid
729
	 */
730
	REG_INIT(AB8500_REGUSWHPREQVALID2,	0x03, 0x0e, 0x3f),
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	/*
	 * 0x02, SysClkReq2Valid1
	 * ...
	 * 0x80, SysClkReq8Valid1
	 */
	REG_INIT(AB8500_REGUSYSCLKREQVALID1,	0x03, 0x0f, 0xfe),
	/*
	 * 0x02, SysClkReq2Valid2
	 * ...
	 * 0x80, SysClkReq8Valid2
	 */
	REG_INIT(AB8500_REGUSYSCLKREQVALID2,	0x03, 0x10, 0xfe),
	/*
	 * 0x02, VTVoutEna
	 * 0x04, Vintcore12Ena
	 * 0x38, Vintcore12Sel
	 * 0x40, Vintcore12LP
	 * 0x80, VTVoutLP
	 */
	REG_INIT(AB8500_REGUMISC1,		0x03, 0x80, 0xfe),
	/*
	 * 0x02, VaudioEna
	 * 0x04, VdmicEna
	 * 0x08, Vamic1Ena
	 * 0x10, Vamic2Ena
	 */
	REG_INIT(AB8500_VAUDIOSUPPLY,		0x03, 0x83, 0x1e),
	/*
	 * 0x01, Vamic1_dzout
	 * 0x02, Vamic2_dzout
	 */
	REG_INIT(AB8500_REGUCTRL1VAMIC,		0x03, 0x84, 0x03),
763 764 765
	/*
	 * 0x03, Vsmps1Regu
	 * 0x0c, Vsmps1SelCtrl
766 767
	 * 0x10, Vsmps1AutoMode
	 * 0x20, Vsmps1PWMMode
768
	 */
769
	REG_INIT(AB8500_VSMPS1REGU,		0x04, 0x03, 0x3f),
770 771 772
	/*
	 * 0x03, Vsmps2Regu
	 * 0x0c, Vsmps2SelCtrl
773 774
	 * 0x10, Vsmps2AutoMode
	 * 0x20, Vsmps2PWMMode
775
	 */
776
	REG_INIT(AB8500_VSMPS2REGU,		0x04, 0x04, 0x3f),
777 778
	/*
	 * 0x03, VpllRegu
779
	 * 0x0c, VanaRegu
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	 */
	REG_INIT(AB8500_VPLLVANAREGU,		0x04, 0x06, 0x0f),
	/*
	 * 0x01, VrefDDREna
	 * 0x02, VrefDDRSleepMode
	 */
	REG_INIT(AB8500_VREFDDR,		0x04, 0x07, 0x03),
	/*
	 * 0x03, VextSupply1Regu
	 * 0x0c, VextSupply2Regu
	 * 0x30, VextSupply3Regu
	 * 0x40, ExtSupply2Bypass
	 * 0x80, ExtSupply3Bypass
	 */
	REG_INIT(AB8500_EXTSUPPLYREGU,		0x04, 0x08, 0xff),
	/*
	 * 0x03, Vaux1Regu
	 * 0x0c, Vaux2Regu
	 */
	REG_INIT(AB8500_VAUX12REGU,		0x04, 0x09, 0x0f),
	/*
801
	 * 0x0c, Vrf1Regu
802 803
	 * 0x03, Vaux3Regu
	 */
804
	REG_INIT(AB8500_VRF1VAUX3REGU,		0x04, 0x0a, 0x0f),
805 806 807 808 809 810 811 812 813 814 815 816 817 818
	/*
	 * 0x3f, Vsmps1Sel1
	 */
	REG_INIT(AB8500_VSMPS1SEL1,		0x04, 0x13, 0x3f),
	/*
	 * 0x0f, Vaux1Sel
	 */
	REG_INIT(AB8500_VAUX1SEL,		0x04, 0x1f, 0x0f),
	/*
	 * 0x0f, Vaux2Sel
	 */
	REG_INIT(AB8500_VAUX2SEL,		0x04, 0x20, 0x0f),
	/*
	 * 0x07, Vaux3Sel
819
	 * 0x30, Vrf1Sel
820
	 */
821
	REG_INIT(AB8500_VRF1VAUX3SEL,		0x04, 0x21, 0x37),
822 823 824 825 826
	/*
	 * 0x01, VextSupply12LP
	 */
	REG_INIT(AB8500_REGUCTRL2SPARE,		0x04, 0x22, 0x01),
	/*
827 828
	 * 0x01, VpllDisch
	 * 0x02, Vrf1Disch
829 830 831 832 833 834 835
	 * 0x04, Vaux1Disch
	 * 0x08, Vaux2Disch
	 * 0x10, Vaux3Disch
	 * 0x20, Vintcore12Disch
	 * 0x40, VTVoutDisch
	 * 0x80, VaudioDisch
	 */
836
	REG_INIT(AB8500_REGUCTRLDISCH,		0x04, 0x43, 0xff),
837
	/*
838
	 * 0x01, VsimDisch
839 840
	 * 0x02, VanaDisch
	 * 0x04, VdmicPullDownEna
841
	 * 0x08, VpllPullDownEna
842 843
	 * 0x10, VdmicDisch
	 */
844
	REG_INIT(AB8500_REGUCTRLDISCH2,		0x04, 0x44, 0x1f),
845 846
};

847 848
static int ab8500_regulator_init_registers(struct platform_device *pdev,
					   int id, int mask, int value)
849 850 851
{
	int err;

852 853
	BUG_ON(value & ~mask);
	BUG_ON(mask & ~ab8500_reg_init[id].mask);
854

855
	/* initialize register */
856 857 858 859
	err = abx500_mask_and_set_register_interruptible(
		&pdev->dev,
		ab8500_reg_init[id].bank,
		ab8500_reg_init[id].addr,
860
		mask, value);
861 862 863 864 865 866 867 868
	if (err < 0) {
		dev_err(&pdev->dev,
			"Failed to initialize 0x%02x, 0x%02x.\n",
			ab8500_reg_init[id].bank,
			ab8500_reg_init[id].addr);
		return err;
	}
	dev_vdbg(&pdev->dev,
869 870 871 872
		 "  init: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
		 ab8500_reg_init[id].bank,
		 ab8500_reg_init[id].addr,
		 mask, value);
873 874 875 876

	return 0;
}

877
static int ab8500_regulator_register(struct platform_device *pdev,
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
					struct regulator_init_data *init_data,
					int id,
					struct device_node *np)
{
	struct ab8500_regulator_info *info = NULL;
	struct regulator_config config = { };
	int err;

	/* assign per-regulator data */
	info = &ab8500_regulator_info[id];
	info->dev = &pdev->dev;

	config.dev = &pdev->dev;
	config.init_data = init_data;
	config.driver_data = info;
	config.of_node = np;

	/* fix for hardware before ab8500v2.0 */
	if (abx500_get_chip_id(info->dev) < 0x20) {
		if (info->desc.id == AB8500_LDO_AUX3) {
			info->desc.n_voltages =
				ARRAY_SIZE(ldo_vauxn_voltages);
900
			info->desc.volt_table = ldo_vauxn_voltages;
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
			info->voltage_mask = 0xf;
		}
	}

	/* register regulator with framework */
	info->regulator = regulator_register(&info->desc, &config);
	if (IS_ERR(info->regulator)) {
		err = PTR_ERR(info->regulator);
		dev_err(&pdev->dev, "failed to register regulator %s\n",
			info->desc.name);
		/* when we fail, un-register all earlier regulators */
		while (--id >= 0) {
			info = &ab8500_regulator_info[id];
			regulator_unregister(info->regulator);
		}
		return err;
	}

	return 0;
}

922
static struct of_regulator_match ab8500_regulator_matches[] = {
923 924 925 926 927 928 929 930 931 932 933
	{ .name	= "ab8500_ldo_aux1",    .driver_data = (void *) AB8500_LDO_AUX1, },
	{ .name	= "ab8500_ldo_aux2",    .driver_data = (void *) AB8500_LDO_AUX2, },
	{ .name	= "ab8500_ldo_aux3",    .driver_data = (void *) AB8500_LDO_AUX3, },
	{ .name	= "ab8500_ldo_intcore", .driver_data = (void *) AB8500_LDO_INTCORE, },
	{ .name	= "ab8500_ldo_tvout",   .driver_data = (void *) AB8500_LDO_TVOUT, },
	{ .name = "ab8500_ldo_usb",     .driver_data = (void *) AB8500_LDO_USB, },
	{ .name = "ab8500_ldo_audio",   .driver_data = (void *) AB8500_LDO_AUDIO, },
	{ .name	= "ab8500_ldo_anamic1", .driver_data = (void *) AB8500_LDO_ANAMIC1, },
	{ .name	= "ab8500_ldo_amamic2", .driver_data = (void *) AB8500_LDO_ANAMIC2, },
	{ .name	= "ab8500_ldo_dmic",    .driver_data = (void *) AB8500_LDO_DMIC, },
	{ .name	= "ab8500_ldo_ana",     .driver_data = (void *) AB8500_LDO_ANA, },
934 935
};

936
static int
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
ab8500_regulator_of_probe(struct platform_device *pdev, struct device_node *np)
{
	int err, i;

	for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
		err = ab8500_regulator_register(
			pdev, ab8500_regulator_matches[i].init_data,
			i, ab8500_regulator_matches[i].of_node);
		if (err)
			return err;
	}

	return 0;
}

952
static int ab8500_regulator_probe(struct platform_device *pdev)
953 954
{
	struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
955
	struct device_node *np = pdev->dev.of_node;
956 957
	struct ab8500_platform_data *ppdata;
	struct ab8500_regulator_platform_data *pdata;
958 959
	int i, err;

960 961 962 963 964 965 966 967 968 969 970 971 972 973
	if (np) {
		err = of_regulator_match(&pdev->dev, np,
					ab8500_regulator_matches,
					ARRAY_SIZE(ab8500_regulator_matches));
		if (err < 0) {
			dev_err(&pdev->dev,
				"Error parsing regulator init data: %d\n", err);
			return err;
		}

		err = ab8500_regulator_of_probe(pdev, np);
		return err;
	}

974 975 976 977
	if (!ab8500) {
		dev_err(&pdev->dev, "null mfd parent\n");
		return -EINVAL;
	}
978 979 980 981 982 983 984 985

	ppdata = dev_get_platdata(ab8500->dev);
	if (!ppdata) {
		dev_err(&pdev->dev, "null parent pdata\n");
		return -EINVAL;
	}

	pdata = ppdata->regulator;
986 987 988 989
	if (!pdata) {
		dev_err(&pdev->dev, "null pdata\n");
		return -EINVAL;
	}
990

991 992
	/* make sure the platform data has the correct size */
	if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
993
		dev_err(&pdev->dev, "Configuration error: size mismatch.\n");
994 995 996
		return -EINVAL;
	}

997
	/* initialize registers */
998
	for (i = 0; i < pdata->num_reg_init; i++) {
999
		int id, mask, value;
1000

1001 1002 1003
		id = pdata->reg_init[i].id;
		mask = pdata->reg_init[i].mask;
		value = pdata->reg_init[i].value;
1004 1005

		/* check for configuration errors */
1006
		BUG_ON(id >= AB8500_NUM_REGULATOR_REGISTERS);
1007

1008
		err = ab8500_regulator_init_registers(pdev, id, mask, value);
1009
		if (err < 0)
1010 1011 1012
			return err;
	}

1013 1014
	/* register all regulators */
	for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
1015 1016
		err = ab8500_regulator_register(pdev, &pdata->regulator[i], i, NULL);
		if (err < 0)
1017 1018 1019 1020 1021 1022
			return err;
	}

	return 0;
}

1023
static int ab8500_regulator_remove(struct platform_device *pdev)
1024 1025 1026 1027 1028 1029
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
		struct ab8500_regulator_info *info = NULL;
		info = &ab8500_regulator_info[i];
1030 1031 1032 1033

		dev_vdbg(rdev_get_dev(info->regulator),
			"%s-remove\n", info->desc.name);

1034 1035 1036 1037 1038 1039 1040 1041
		regulator_unregister(info->regulator);
	}

	return 0;
}

static struct platform_driver ab8500_regulator_driver = {
	.probe = ab8500_regulator_probe,
1042
	.remove = ab8500_regulator_remove,
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	.driver         = {
		.name   = "ab8500-regulator",
		.owner  = THIS_MODULE,
	},
};

static int __init ab8500_regulator_init(void)
{
	int ret;

	ret = platform_driver_register(&ab8500_regulator_driver);
	if (ret != 0)
		pr_err("Failed to register ab8500 regulator: %d\n", ret);

	return ret;
}
subsys_initcall(ab8500_regulator_init);

static void __exit ab8500_regulator_exit(void)
{
	platform_driver_unregister(&ab8500_regulator_driver);
}
module_exit(ab8500_regulator_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Sundar Iyer <sundar.iyer@stericsson.com>");
1069
MODULE_AUTHOR("Bengt Jonsson <bengt.g.jonsson@stericsson.com>");
1070 1071
MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
MODULE_ALIAS("platform:ab8500-regulator");