ab8500.c 13.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 12
 * AB8500 supports the following regulators:
 *   VAUX1/2/3, VINTCORE, VTVOUT, VAUDIO, VAMIC1/2, VDMIC, VANA
13 14 15 16 17 18
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/mfd/ab8500.h>
19
#include <linux/mfd/abx500.h>
20 21 22 23 24 25
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/ab8500.h>

/**
 * struct ab8500_regulator_info - ab8500 regulator information
26
 * @dev: device pointer
27 28 29 30 31
 * @desc: regulator description
 * @regulator_dev: regulator device
 * @max_uV: maximum voltage (for variable voltage supplies)
 * @min_uV: minimum voltage (for variable voltage supplies)
 * @fixed_uV: typical voltage (for fixed voltage supplies)
32
 * @update_bank: bank to control on/off
33
 * @update_reg: register to control on/off
34 35
 * @update_mask: mask to enable/disable regulator
 * @update_val_enable: bits to enable the regulator in normal (high power) mode
36
 * @voltage_bank: bank to control regulator voltage
37 38
 * @voltage_reg: register to control regulator voltage
 * @voltage_mask: mask to control regulator voltage
39
 * @voltages: supported voltage table
40 41 42 43 44 45 46 47 48
 * @voltages_len: number of supported voltages for the regulator
 */
struct ab8500_regulator_info {
	struct device		*dev;
	struct regulator_desc	desc;
	struct regulator_dev	*regulator;
	int max_uV;
	int min_uV;
	int fixed_uV;
49 50
	u8 update_bank;
	u8 update_reg;
51 52
	u8 update_mask;
	u8 update_val_enable;
53 54 55
	u8 voltage_bank;
	u8 voltage_reg;
	u8 voltage_mask;
56
	int const *voltages;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	int voltages_len;
};

/* voltage tables for the vauxn/vintcore supplies */
static const int ldo_vauxn_voltages[] = {
	1100000,
	1200000,
	1300000,
	1400000,
	1500000,
	1800000,
	1850000,
	1900000,
	2500000,
	2650000,
	2700000,
	2750000,
	2800000,
	2900000,
	3000000,
	3300000,
};

80 81 82 83 84 85 86 87 88 89 90
static const int ldo_vaux3_voltages[] = {
	1200000,
	1500000,
	1800000,
	2100000,
	2500000,
	2750000,
	2790000,
	2910000,
};

91 92 93 94 95 96 97 98 99 100 101 102
static const int ldo_vintcore_voltages[] = {
	1200000,
	1225000,
	1250000,
	1275000,
	1300000,
	1325000,
	1350000,
};

static int ab8500_regulator_enable(struct regulator_dev *rdev)
{
103
	int ret;
104 105
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

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

111
	ret = abx500_mask_and_set_register_interruptible(info->dev,
112 113
		info->update_bank, info->update_reg,
		info->update_mask, info->update_val_enable);
114 115 116
	if (ret < 0)
		dev_err(rdev_get_dev(rdev),
			"couldn't set enable bits for regulator\n");
117 118 119 120 121 122

	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,
		info->update_mask, info->update_val_enable);

123 124 125 126 127
	return ret;
}

static int ab8500_regulator_disable(struct regulator_dev *rdev)
{
128
	int ret;
129 130
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

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

136
	ret = abx500_mask_and_set_register_interruptible(info->dev,
137 138
		info->update_bank, info->update_reg,
		info->update_mask, 0x0);
139 140 141
	if (ret < 0)
		dev_err(rdev_get_dev(rdev),
			"couldn't set disable bits for regulator\n");
142 143 144 145 146 147

	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);

148 149 150 151 152
	return ret;
}

static int ab8500_regulator_is_enabled(struct regulator_dev *rdev)
{
153
	int ret;
154
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
155
	u8 regval;
156

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

162
	ret = abx500_get_register_interruptible(info->dev,
163
		info->update_bank, info->update_reg, &regval);
164 165 166 167 168 169
	if (ret < 0) {
		dev_err(rdev_get_dev(rdev),
			"couldn't read 0x%x register\n", info->update_reg);
		return ret;
	}

170 171 172 173 174 175 176
	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)
177 178 179 180 181 182 183 184 185
		return true;
	else
		return false;
}

static int ab8500_list_voltage(struct regulator_dev *rdev, unsigned selector)
{
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

186 187
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
188
		return -EINVAL;
189
	}
190 191 192 193 194

	/* return the uV for the fixed regulators */
	if (info->fixed_uV)
		return info->fixed_uV;

195
	if (selector >= info->voltages_len)
196 197
		return -EINVAL;

198
	return info->voltages[selector];
199 200 201 202
}

static int ab8500_regulator_get_voltage(struct regulator_dev *rdev)
{
203
	int ret, val;
204
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
205
	u8 regval;
206

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

212 213
	ret = abx500_get_register_interruptible(info->dev,
			info->voltage_bank, info->voltage_reg, &regval);
214 215 216 217 218 219
	if (ret < 0) {
		dev_err(rdev_get_dev(rdev),
			"couldn't read voltage reg for regulator\n");
		return ret;
	}

220 221 222 223 224 225
	dev_vdbg(rdev_get_dev(rdev),
		"%s-get_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);

226
	/* vintcore has a different layout */
227
	val = regval & info->voltage_mask;
228
	if (info->desc.id == AB8500_LDO_INTCORE)
229
		ret = info->voltages[val >> 0x3];
230
	else
231
		ret = info->voltages[val];
232 233 234 235 236 237 238 239 240 241 242 243

	return ret;
}

static int ab8500_get_best_voltage_index(struct regulator_dev *rdev,
		int min_uV, int max_uV)
{
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
	int i;

	/* check the supported voltage */
	for (i = 0; i < info->voltages_len; i++) {
244 245
		if ((info->voltages[i] >= min_uV) &&
		    (info->voltages[i] <= max_uV))
246 247 248 249 250 251 252
			return i;
	}

	return -EINVAL;
}

static int ab8500_regulator_set_voltage(struct regulator_dev *rdev,
253 254
					int min_uV, int max_uV,
					unsigned *selector)
255
{
256
	int ret;
257
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);
258
	u8 regval;
259

260 261
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
262
		return -EINVAL;
263
	}
264 265 266 267 268 269 270 271 272

	/* get the appropriate voltages within the range */
	ret = ab8500_get_best_voltage_index(rdev, min_uV, max_uV);
	if (ret < 0) {
		dev_err(rdev_get_dev(rdev),
				"couldn't get best voltage for regulator\n");
		return ret;
	}

273 274
	*selector = ret;

275
	/* set the registers for the request */
276
	regval = (u8)ret;
277
	ret = abx500_mask_and_set_register_interruptible(info->dev,
278 279
			info->voltage_bank, info->voltage_reg,
			info->voltage_mask, regval);
280 281 282 283
	if (ret < 0)
		dev_err(rdev_get_dev(rdev),
		"couldn't set voltage reg for regulator\n");

284 285 286 287 288 289
	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);

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	return ret;
}

static struct regulator_ops ab8500_regulator_ops = {
	.enable		= ab8500_regulator_enable,
	.disable	= ab8500_regulator_disable,
	.is_enabled	= ab8500_regulator_is_enabled,
	.get_voltage	= ab8500_regulator_get_voltage,
	.set_voltage	= ab8500_regulator_set_voltage,
	.list_voltage	= ab8500_list_voltage,
};

static int ab8500_fixed_get_voltage(struct regulator_dev *rdev)
{
	struct ab8500_regulator_info *info = rdev_get_drvdata(rdev);

306 307
	if (info == NULL) {
		dev_err(rdev_get_dev(rdev), "regulator info null pointer\n");
308
		return -EINVAL;
309
	}
310 311 312 313 314 315 316 317 318 319 320 321

	return info->fixed_uV;
}

static struct regulator_ops ab8500_ldo_fixed_ops = {
	.enable		= ab8500_regulator_enable,
	.disable	= ab8500_regulator_disable,
	.is_enabled	= ab8500_regulator_is_enabled,
	.get_voltage	= ab8500_fixed_get_voltage,
	.list_voltage	= ab8500_list_voltage,
};

322 323 324 325
#define AB8500_LDO(_id, _min_mV, _max_mV,			\
	_u_bank, _u_reg, _u_mask, _u_val_enable,		\
	_v_bank, _v_reg, _v_mask, _v_table, _v_table_len)	\
[AB8500_LDO_##_id] = {						\
326
	.desc	= {						\
327 328 329 330 331
		.name		= "LDO-" #_id,			\
		.ops		= &ab8500_regulator_ops,	\
		.type		= REGULATOR_VOLTAGE,		\
		.id		= AB8500_LDO_##_id,		\
		.owner		= THIS_MODULE,			\
332
	},							\
333 334 335 336 337 338 339 340 341 342 343 344
	.min_uV			= (_min_mV) * 1000,		\
	.max_uV			= (_max_mV) * 1000,		\
	.update_bank		= _u_bank,			\
	.update_reg		= _u_reg,			\
	.update_mask		= _u_mask,			\
	.update_val_enable	= _u_val_enable,		\
	.voltage_bank		= _v_bank,			\
	.voltage_reg		= _v_reg,			\
	.voltage_mask		= _v_mask,			\
	.voltages		= _v_table,			\
	.voltages_len		= _v_table_len,			\
	.fixed_uV		= 0,				\
345 346
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
#define AB8500_FIXED_LDO(_id, _fixed_mV,			\
	_u_bank, _u_reg, _u_mask, _u_val_enable)		\
[AB8500_LDO_##_id] = {						\
	.desc	= {						\
		.name		= "LDO-" #_id,			\
		.ops		= &ab8500_ldo_fixed_ops,	\
		.type		= REGULATOR_VOLTAGE,		\
		.id		= AB8500_LDO_##_id,		\
		.owner		= THIS_MODULE,			\
	},							\
	.fixed_uV		= (_fixed_mV) * 1000,		\
	.update_bank		= _u_bank,			\
	.update_reg		= _u_reg,			\
	.update_mask		= _u_mask,			\
	.update_val_enable	= _u_val_enable,		\
362 363 364 365
}

static struct ab8500_regulator_info ab8500_regulator_info[] = {
	/*
366 367 368 369
	 * Variable Voltage Regulators
	 *   name, min mV, max mV,
	 *   update bank, reg, mask, enable val
	 *   volt bank, reg, mask, table, table length
370
	 */
371 372 373 374 375 376 377 378 379 380 381
	AB8500_LDO(AUX1, 1100, 3300,
		0x04, 0x09, 0x03, 0x01, 0x04, 0x1f, 0x0f,
		ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
	AB8500_LDO(AUX2, 1100, 3300,
		0x04, 0x09, 0x0c, 0x04, 0x04, 0x20, 0x0f,
		ldo_vauxn_voltages, ARRAY_SIZE(ldo_vauxn_voltages)),
	AB8500_LDO(AUX3, 1100, 3300,
		0x04, 0x0a, 0x03, 0x01, 0x04, 0x21, 0x07,
		ldo_vaux3_voltages, ARRAY_SIZE(ldo_vaux3_voltages)),
	AB8500_LDO(INTCORE, 1100, 3300,
		0x03, 0x80, 0x44, 0x04, 0x03, 0x80, 0x38,
382 383 384
		ldo_vintcore_voltages, ARRAY_SIZE(ldo_vintcore_voltages)),

	/*
385 386 387
	 * Fixed Voltage Regulators
	 *   name, fixed mV,
	 *   update bank, reg, mask, enable val
388
	 */
389 390 391 392 393 394
	AB8500_FIXED_LDO(TVOUT,	  2000, 0x03, 0x80, 0x82, 0x02),
	AB8500_FIXED_LDO(AUDIO,   2000, 0x03, 0x83, 0x02, 0x02),
	AB8500_FIXED_LDO(ANAMIC1, 2050, 0x03, 0x83, 0x08, 0x08),
	AB8500_FIXED_LDO(ANAMIC2, 2050, 0x03, 0x83, 0x10, 0x10),
	AB8500_FIXED_LDO(DMIC,    1800, 0x03, 0x83, 0x04, 0x04),
	AB8500_FIXED_LDO(ANA,     1200, 0x04, 0x06, 0x0c, 0x04),
395 396 397 398 399
};

static __devinit int ab8500_regulator_probe(struct platform_device *pdev)
{
	struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
400
	struct ab8500_platform_data *pdata;
401 402 403 404 405 406
	int i, err;

	if (!ab8500) {
		dev_err(&pdev->dev, "null mfd parent\n");
		return -EINVAL;
	}
407
	pdata = dev_get_platdata(ab8500->dev);
408 409 410 411
	if (!pdata) {
		dev_err(&pdev->dev, "null pdata\n");
		return -EINVAL;
	}
412

413 414 415 416 417 418
	/* make sure the platform data has the correct size */
	if (pdata->num_regulator != ARRAY_SIZE(ab8500_regulator_info)) {
		dev_err(&pdev->dev, "platform configuration error\n");
		return -EINVAL;
	}

419 420 421 422 423 424 425 426
	/* register all regulators */
	for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
		struct ab8500_regulator_info *info = NULL;

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

427 428 429 430 431
		/* 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);
432
				info->voltages = ldo_vauxn_voltages;
433 434 435 436 437 438 439
				info->voltages_len =
					ARRAY_SIZE(ldo_vauxn_voltages);
				info->voltage_mask = 0xf;
			}
		}

		/* register regulator with framework */
440
		info->regulator = regulator_register(&info->desc, &pdev->dev,
441
				&pdata->regulator[i], info);
442 443 444 445 446
		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 */
447
			while (--i >= 0) {
448 449 450 451 452
				info = &ab8500_regulator_info[i];
				regulator_unregister(info->regulator);
			}
			return err;
		}
453 454 455

		dev_vdbg(rdev_get_dev(info->regulator),
			"%s-probed\n", info->desc.name);
456 457 458 459 460 461 462 463 464 465 466 467
	}

	return 0;
}

static __devexit int ab8500_regulator_remove(struct platform_device *pdev)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ab8500_regulator_info); i++) {
		struct ab8500_regulator_info *info = NULL;
		info = &ab8500_regulator_info[i];
468 469 470 471

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

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
		regulator_unregister(info->regulator);
	}

	return 0;
}

static struct platform_driver ab8500_regulator_driver = {
	.probe = ab8500_regulator_probe,
	.remove = __devexit_p(ab8500_regulator_remove),
	.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>");
MODULE_DESCRIPTION("Regulator Driver for ST-Ericsson AB8500 Mixed-Sig PMIC");
MODULE_ALIAS("platform:ab8500-regulator");