tps6507x-regulator.c 11.3 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
/*
 * tps6507x-regulator.c
 *
 * Regulator driver for TPS65073 PMIC
 *
 * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
 *
 * 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 version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
 * whether express or implied; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#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>
25
#include <linux/regulator/tps6507x.h>
26
#include <linux/slab.h>
27
#include <linux/mfd/tps6507x.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

/* DCDC's */
#define TPS6507X_DCDC_1				0
#define TPS6507X_DCDC_2				1
#define TPS6507X_DCDC_3				2
/* LDOs */
#define TPS6507X_LDO_1				3
#define TPS6507X_LDO_2				4

#define TPS6507X_MAX_REG_ID			TPS6507X_LDO_2

/* Number of step-down converters available */
#define TPS6507X_NUM_DCDC			3
/* Number of LDO voltage regulators  available */
#define TPS6507X_NUM_LDO			2
/* Number of total regulators available */
#define TPS6507X_NUM_REGULATOR		(TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/* Supported voltage values for regulators (in microVolts) */
static const unsigned int VDCDCx_VSEL_table[] = {
	725000, 750000, 775000, 800000,
	825000, 850000, 875000, 900000,
	925000, 950000, 975000, 1000000,
	1025000, 1050000, 1075000, 1100000,
	1125000, 1150000, 1175000, 1200000,
	1225000, 1250000, 1275000, 1300000,
	1325000, 1350000, 1375000, 1400000,
	1425000, 1450000, 1475000, 1500000,
	1550000, 1600000, 1650000, 1700000,
	1750000, 1800000, 1850000, 1900000,
	1950000, 2000000, 2050000, 2100000,
	2150000, 2200000, 2250000, 2300000,
	2350000, 2400000, 2450000, 2500000,
	2550000, 2600000, 2650000, 2700000,
	2750000, 2800000, 2850000, 2900000,
	3000000, 3100000, 3200000, 3300000,
64 65
};

66 67 68 69 70
static const unsigned int LDO1_VSEL_table[] = {
	1000000, 1100000, 1200000, 1250000,
	1300000, 1350000, 1400000, 1500000,
	1600000, 1800000, 2500000, 2750000,
	2800000, 3000000, 3100000, 3300000,
71 72
};

73 74
/* The voltage mapping table for LDO2 is the same as VDCDCx */
#define LDO2_VSEL_table VDCDCx_VSEL_table
75 76 77 78

struct tps_info {
	const char *name;
	u8 table_len;
79
	const unsigned int *table;
80 81 82

	/* Does DCDC high or the low register defines output voltage? */
	bool defdcdc_default;
83 84
};

85
static struct tps_info tps6507x_pmic_regs[] = {
T
Todd Fischer 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	{
		.name = "VDCDC1",
		.table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
		.table = VDCDCx_VSEL_table,
	},
	{
		.name = "VDCDC2",
		.table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
		.table = VDCDCx_VSEL_table,
	},
	{
		.name = "VDCDC3",
		.table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
		.table = VDCDCx_VSEL_table,
	},
	{
		.name = "LDO1",
		.table_len = ARRAY_SIZE(LDO1_VSEL_table),
		.table = LDO1_VSEL_table,
	},
	{
		.name = "LDO2",
		.table_len = ARRAY_SIZE(LDO2_VSEL_table),
		.table = LDO2_VSEL_table,
	},
};

113
struct tps6507x_pmic {
114
	struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
T
Todd Fischer 已提交
115
	struct tps6507x_dev *mfd;
116
	struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
117
	struct tps_info *info[TPS6507X_NUM_REGULATOR];
118 119
	struct mutex io_lock;
};
120
static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
121
{
T
Todd Fischer 已提交
122 123 124 125 126 127 128 129 130
	u8 val;
	int err;

	err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);

	if (err)
		return err;

	return val;
131 132
}

133
static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
134
{
T
Todd Fischer 已提交
135
	return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
136 137
}

138
static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
139 140 141 142 143
{
	int err, data;

	mutex_lock(&tps->io_lock);

144
	data = tps6507x_pmic_read(tps, reg);
145
	if (data < 0) {
T
Todd Fischer 已提交
146
		dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
147 148 149 150 151
		err = data;
		goto out;
	}

	data |= mask;
152
	err = tps6507x_pmic_write(tps, reg, data);
153
	if (err)
T
Todd Fischer 已提交
154
		dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
155 156 157 158 159 160

out:
	mutex_unlock(&tps->io_lock);
	return err;
}

161
static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
162 163 164 165 166
{
	int err, data;

	mutex_lock(&tps->io_lock);

167
	data = tps6507x_pmic_read(tps, reg);
168
	if (data < 0) {
T
Todd Fischer 已提交
169
		dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
170 171 172 173 174
		err = data;
		goto out;
	}

	data &= ~mask;
175
	err = tps6507x_pmic_write(tps, reg, data);
176
	if (err)
T
Todd Fischer 已提交
177
		dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
178 179 180 181 182 183

out:
	mutex_unlock(&tps->io_lock);
	return err;
}

184
static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
185 186 187 188 189
{
	int data;

	mutex_lock(&tps->io_lock);

190
	data = tps6507x_pmic_read(tps, reg);
191
	if (data < 0)
T
Todd Fischer 已提交
192
		dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
193 194 195 196 197

	mutex_unlock(&tps->io_lock);
	return data;
}

198
static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
199 200 201 202 203
{
	int err;

	mutex_lock(&tps->io_lock);

204
	err = tps6507x_pmic_write(tps, reg, val);
205
	if (err < 0)
T
Todd Fischer 已提交
206
		dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
207 208 209 210 211

	mutex_unlock(&tps->io_lock);
	return err;
}

212
static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
213
{
214
	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
215
	int data, rid = rdev_get_id(dev);
216 217
	u8 shift;

218
	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
219 220
		return -EINVAL;

221
	shift = TPS6507X_MAX_REG_ID - rid;
222
	data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
223 224 225 226 227 228 229

	if (data < 0)
		return data;
	else
		return (data & 1<<shift) ? 1 : 0;
}

230
static int tps6507x_pmic_enable(struct regulator_dev *dev)
231
{
232
	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
233
	int rid = rdev_get_id(dev);
234 235
	u8 shift;

236
	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
237 238
		return -EINVAL;

239
	shift = TPS6507X_MAX_REG_ID - rid;
240
	return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
241 242
}

243
static int tps6507x_pmic_disable(struct regulator_dev *dev)
244
{
245
	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
246
	int rid = rdev_get_id(dev);
247 248
	u8 shift;

249
	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
250 251
		return -EINVAL;

252
	shift = TPS6507X_MAX_REG_ID - rid;
253 254
	return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
					1 << shift);
255 256
}

257
static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
258
{
259
	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
260 261
	int data, rid = rdev_get_id(dev);
	u8 reg, mask;
262

263
	switch (rid) {
264 265
	case TPS6507X_DCDC_1:
		reg = TPS6507X_REG_DEFDCDC1;
266
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
267 268
		break;
	case TPS6507X_DCDC_2:
269
		if (tps->info[rid]->defdcdc_default)
270 271 272
			reg = TPS6507X_REG_DEFDCDC2_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC2_LOW;
273
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
274 275
		break;
	case TPS6507X_DCDC_3:
276
		if (tps->info[rid]->defdcdc_default)
277 278 279
			reg = TPS6507X_REG_DEFDCDC3_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC3_LOW;
280 281 282 283 284 285 286 287 288
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
		break;
	case TPS6507X_LDO_1:
		reg = TPS6507X_REG_LDO_CTRL1;
		mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
		break;
	case TPS6507X_LDO_2:
		reg = TPS6507X_REG_DEFLDO2;
		mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
289 290 291 292 293
		break;
	default:
		return -EINVAL;
	}

294
	data = tps6507x_pmic_reg_read(tps, reg);
295 296 297
	if (data < 0)
		return data;

298
	data &= mask;
299
	return data;
300 301
}

302 303
static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
					  unsigned selector)
304
{
305
	struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
306
	int data, rid = rdev_get_id(dev);
307
	u8 reg, mask;
308

309
	switch (rid) {
310 311
	case TPS6507X_DCDC_1:
		reg = TPS6507X_REG_DEFDCDC1;
312
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
313 314
		break;
	case TPS6507X_DCDC_2:
315
		if (tps->info[rid]->defdcdc_default)
316 317 318
			reg = TPS6507X_REG_DEFDCDC2_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC2_LOW;
319
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
320 321
		break;
	case TPS6507X_DCDC_3:
322
		if (tps->info[rid]->defdcdc_default)
323 324 325
			reg = TPS6507X_REG_DEFDCDC3_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC3_LOW;
326 327 328 329 330 331 332 333 334
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
		break;
	case TPS6507X_LDO_1:
		reg = TPS6507X_REG_LDO_CTRL1;
		mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
		break;
	case TPS6507X_LDO_2:
		reg = TPS6507X_REG_DEFLDO2;
		mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
335 336 337 338 339
		break;
	default:
		return -EINVAL;
	}

340
	data = tps6507x_pmic_reg_read(tps, reg);
341 342 343 344
	if (data < 0)
		return data;

	data &= ~mask;
345
	data |= selector;
346

347
	return tps6507x_pmic_reg_write(tps, reg, data);
348 349
}

350 351 352 353
static struct regulator_ops tps6507x_pmic_ops = {
	.is_enabled = tps6507x_pmic_is_enabled,
	.enable = tps6507x_pmic_enable,
	.disable = tps6507x_pmic_disable,
354
	.get_voltage_sel = tps6507x_pmic_get_voltage_sel,
355
	.set_voltage_sel = tps6507x_pmic_set_voltage_sel,
356
	.list_voltage = regulator_list_voltage_table,
357 358
};

359
static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
360
{
T
Todd Fischer 已提交
361
	struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
362
	struct tps_info *info = &tps6507x_pmic_regs[0];
363
	struct regulator_config config = { };
364 365
	struct regulator_init_data *init_data;
	struct regulator_dev *rdev;
366
	struct tps6507x_pmic *tps;
367
	struct tps6507x_board *tps_board;
368
	int i;
369
	int error;
370

371 372 373 374 375
	/**
	 * tps_board points to pmic related constants
	 * coming from the board-evm file.
	 */

T
Todd Fischer 已提交
376
	tps_board = dev_get_platdata(tps6507x_dev->dev);
377 378 379
	if (!tps_board)
		return -EINVAL;

380 381 382 383
	/**
	 * init_data points to array of regulator_init structures
	 * coming from the board-evm file.
	 */
384
	init_data = tps_board->tps6507x_pmic_init_data;
385
	if (!init_data)
386
		return -EINVAL;
387

388
	tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
389 390 391 392 393 394
	if (!tps)
		return -ENOMEM;

	mutex_init(&tps->io_lock);

	/* common for all regulators */
T
Todd Fischer 已提交
395
	tps->mfd = tps6507x_dev;
396 397 398 399

	for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
		/* Register the regulators */
		tps->info[i] = info;
400 401 402 403 404 405
		if (init_data->driver_data) {
			struct tps6507x_reg_platform_data *data =
							init_data->driver_data;
			tps->info[i]->defdcdc_default = data->defdcdc_default;
		}

406
		tps->desc[i].name = info->name;
407
		tps->desc[i].id = i;
408
		tps->desc[i].n_voltages = info->table_len;
409
		tps->desc[i].volt_table = info->table;
410
		tps->desc[i].ops = &tps6507x_pmic_ops;
411 412 413
		tps->desc[i].type = REGULATOR_VOLTAGE;
		tps->desc[i].owner = THIS_MODULE;

414 415 416 417 418
		config.dev = tps6507x_dev->dev;
		config.init_data = init_data;
		config.driver_data = tps;

		rdev = regulator_register(&tps->desc[i], &config);
419
		if (IS_ERR(rdev)) {
T
Todd Fischer 已提交
420 421 422
			dev_err(tps6507x_dev->dev,
				"failed to register %s regulator\n",
				pdev->name);
423 424
			error = PTR_ERR(rdev);
			goto fail;
425 426 427 428 429 430
		}

		/* Save regulator for cleanup */
		tps->rdev[i] = rdev;
	}

T
Todd Fischer 已提交
431
	tps6507x_dev->pmic = tps;
432
	platform_set_drvdata(pdev, tps6507x_dev);
433 434

	return 0;
435 436 437 438 439

fail:
	while (--i >= 0)
		regulator_unregister(tps->rdev[i]);
	return error;
440 441
}

T
Todd Fischer 已提交
442
static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
443
{
T
Todd Fischer 已提交
444 445
	struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
	struct tps6507x_pmic *tps = tps6507x_dev->pmic;
446 447 448 449 450 451 452
	int i;

	for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
		regulator_unregister(tps->rdev[i]);
	return 0;
}

T
Todd Fischer 已提交
453
static struct platform_driver tps6507x_pmic_driver = {
454
	.driver = {
T
Todd Fischer 已提交
455
		.name = "tps6507x-pmic",
456 457
		.owner = THIS_MODULE,
	},
458 459
	.probe = tps6507x_pmic_probe,
	.remove = __devexit_p(tps6507x_pmic_remove),
460 461
};

462
static int __init tps6507x_pmic_init(void)
463
{
T
Todd Fischer 已提交
464
	return platform_driver_register(&tps6507x_pmic_driver);
465
}
466
subsys_initcall(tps6507x_pmic_init);
467

468
static void __exit tps6507x_pmic_cleanup(void)
469
{
T
Todd Fischer 已提交
470
	platform_driver_unregister(&tps6507x_pmic_driver);
471
}
472
module_exit(tps6507x_pmic_cleanup);
473 474 475

MODULE_AUTHOR("Texas Instruments");
MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
476
MODULE_LICENSE("GPL v2");
T
Todd Fischer 已提交
477
MODULE_ALIAS("platform:tps6507x-pmic");