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

/* 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)

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/* 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,
66 67
};

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

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

struct tps_info {
	const char *name;
	u8 table_len;
81
	const unsigned int *table;
82 83 84

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

87
static struct tps_info tps6507x_pmic_regs[] = {
T
Todd Fischer 已提交
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 113 114
	{
		.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,
	},
};

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

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

	if (err)
		return err;

	return val;
133 134
}

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

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

	mutex_lock(&tps->io_lock);

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

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

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

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

	mutex_lock(&tps->io_lock);

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

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

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

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

	mutex_lock(&tps->io_lock);

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

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

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

	mutex_lock(&tps->io_lock);

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

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

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

220
	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
221 222
		return -EINVAL;

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

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

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

238
	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
239 240
		return -EINVAL;

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

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

251
	if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
252 253
		return -EINVAL;

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

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

265
	switch (rid) {
266 267
	case TPS6507X_DCDC_1:
		reg = TPS6507X_REG_DEFDCDC1;
268
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
269 270
		break;
	case TPS6507X_DCDC_2:
271
		if (tps->info[rid]->defdcdc_default)
272 273 274
			reg = TPS6507X_REG_DEFDCDC2_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC2_LOW;
275
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
276 277
		break;
	case TPS6507X_DCDC_3:
278
		if (tps->info[rid]->defdcdc_default)
279 280 281
			reg = TPS6507X_REG_DEFDCDC3_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC3_LOW;
282 283 284 285 286 287 288 289 290
		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;
291 292 293 294 295
		break;
	default:
		return -EINVAL;
	}

296
	data = tps6507x_pmic_reg_read(tps, reg);
297 298 299
	if (data < 0)
		return data;

300
	data &= mask;
301
	return data;
302 303
}

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

311
	switch (rid) {
312 313
	case TPS6507X_DCDC_1:
		reg = TPS6507X_REG_DEFDCDC1;
314
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
315 316
		break;
	case TPS6507X_DCDC_2:
317
		if (tps->info[rid]->defdcdc_default)
318 319 320
			reg = TPS6507X_REG_DEFDCDC2_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC2_LOW;
321
		mask = TPS6507X_DEFDCDCX_DCDC_MASK;
322 323
		break;
	case TPS6507X_DCDC_3:
324
		if (tps->info[rid]->defdcdc_default)
325 326 327
			reg = TPS6507X_REG_DEFDCDC3_HIGH;
		else
			reg = TPS6507X_REG_DEFDCDC3_LOW;
328 329 330 331 332 333 334 335 336
		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;
337 338 339 340 341
		break;
	default:
		return -EINVAL;
	}

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

	data &= ~mask;
347
	data |= selector;
348

349
	return tps6507x_pmic_reg_write(tps, reg, data);
350 351
}

352 353 354 355
static struct regulator_ops tps6507x_pmic_ops = {
	.is_enabled = tps6507x_pmic_is_enabled,
	.enable = tps6507x_pmic_enable,
	.disable = tps6507x_pmic_disable,
356
	.get_voltage_sel = tps6507x_pmic_get_voltage_sel,
357
	.set_voltage_sel = tps6507x_pmic_set_voltage_sel,
358
	.list_voltage = regulator_list_voltage_table,
359
	.map_voltage = regulator_map_voltage_ascend,
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 394 395 396 397
#ifdef CONFIG_OF
static struct of_regulator_match tps6507x_matches[] = {
	{ .name = "VDCDC1"},
	{ .name = "VDCDC2"},
	{ .name = "VDCDC3"},
	{ .name = "LDO1"},
	{ .name = "LDO2"},
};

static struct tps6507x_board *tps6507x_parse_dt_reg_data(
		struct platform_device *pdev,
		struct of_regulator_match **tps6507x_reg_matches)
{
	struct tps6507x_board *tps_board;
	struct device_node *np = pdev->dev.parent->of_node;
	struct device_node *regulators;
	struct of_regulator_match *matches;
	static struct regulator_init_data *reg_data;
	int idx = 0, count, ret;

	tps_board = devm_kzalloc(&pdev->dev, sizeof(*tps_board),
					GFP_KERNEL);
	if (!tps_board) {
		dev_err(&pdev->dev, "Failure to alloc pdata for regulators.\n");
		return NULL;
	}

	regulators = of_find_node_by_name(np, "regulators");
	if (!regulators) {
		dev_err(&pdev->dev, "regulator node not found\n");
		return NULL;
	}

	count = ARRAY_SIZE(tps6507x_matches);
	matches = tps6507x_matches;

398
	ret = of_regulator_match(&pdev->dev, regulators, matches, count);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	if (ret < 0) {
		dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
			ret);
		return NULL;
	}

	*tps6507x_reg_matches = matches;

	reg_data = devm_kzalloc(&pdev->dev, (sizeof(struct regulator_init_data)
					* TPS6507X_NUM_REGULATOR), GFP_KERNEL);
	if (!reg_data) {
		dev_err(&pdev->dev, "Failure to alloc init data for regulators.\n");
		return NULL;
	}

	tps_board->tps6507x_pmic_init_data = reg_data;

	for (idx = 0; idx < count; idx++) {
		if (!matches[idx].init_data || !matches[idx].of_node)
			continue;

		memcpy(&reg_data[idx], matches[idx].init_data,
				sizeof(struct regulator_init_data));

	}

	return tps_board;
}
#else
static inline struct tps6507x_board *tps6507x_parse_dt_reg_data(
			struct platform_device *pdev,
			struct of_regulator_match **tps6507x_reg_matches)
{
	*tps6507x_reg_matches = NULL;
	return NULL;
}
#endif
436
static int tps6507x_pmic_probe(struct platform_device *pdev)
437
{
T
Todd Fischer 已提交
438
	struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
439
	struct tps_info *info = &tps6507x_pmic_regs[0];
440
	struct regulator_config config = { };
441 442
	struct regulator_init_data *init_data;
	struct regulator_dev *rdev;
443
	struct tps6507x_pmic *tps;
444
	struct tps6507x_board *tps_board;
445
	struct of_regulator_match *tps6507x_reg_matches = NULL;
446
	int i;
447
	int error;
448
	unsigned int prop;
449

450 451 452 453 454
	/**
	 * tps_board points to pmic related constants
	 * coming from the board-evm file.
	 */

T
Todd Fischer 已提交
455
	tps_board = dev_get_platdata(tps6507x_dev->dev);
456 457 458
	if (!tps_board && tps6507x_dev->dev->of_node)
		tps_board = tps6507x_parse_dt_reg_data(pdev,
						&tps6507x_reg_matches);
459 460 461
	if (!tps_board)
		return -EINVAL;

462 463 464 465
	/**
	 * init_data points to array of regulator_init structures
	 * coming from the board-evm file.
	 */
466
	init_data = tps_board->tps6507x_pmic_init_data;
467
	if (!init_data)
468
		return -EINVAL;
469

470
	tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
471 472 473 474 475 476
	if (!tps)
		return -ENOMEM;

	mutex_init(&tps->io_lock);

	/* common for all regulators */
T
Todd Fischer 已提交
477
	tps->mfd = tps6507x_dev;
478 479 480 481

	for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
		/* Register the regulators */
		tps->info[i] = info;
482 483 484 485 486 487
		if (init_data->driver_data) {
			struct tps6507x_reg_platform_data *data =
							init_data->driver_data;
			tps->info[i]->defdcdc_default = data->defdcdc_default;
		}

488
		tps->desc[i].name = info->name;
489
		tps->desc[i].id = i;
490
		tps->desc[i].n_voltages = info->table_len;
491
		tps->desc[i].volt_table = info->table;
492
		tps->desc[i].ops = &tps6507x_pmic_ops;
493 494 495
		tps->desc[i].type = REGULATOR_VOLTAGE;
		tps->desc[i].owner = THIS_MODULE;

496 497 498 499
		config.dev = tps6507x_dev->dev;
		config.init_data = init_data;
		config.driver_data = tps;

500 501 502 503 504 505 506 507 508 509 510
		if (tps6507x_reg_matches) {
			error = of_property_read_u32(
				tps6507x_reg_matches[i].of_node,
					"ti,defdcdc_default", &prop);

			if (!error)
				tps->info[i]->defdcdc_default = prop;

			config.of_node = tps6507x_reg_matches[i].of_node;
		}

511
		rdev = regulator_register(&tps->desc[i], &config);
512
		if (IS_ERR(rdev)) {
T
Todd Fischer 已提交
513 514 515
			dev_err(tps6507x_dev->dev,
				"failed to register %s regulator\n",
				pdev->name);
516 517
			error = PTR_ERR(rdev);
			goto fail;
518 519 520 521 522 523
		}

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

T
Todd Fischer 已提交
524
	tps6507x_dev->pmic = tps;
525
	platform_set_drvdata(pdev, tps6507x_dev);
526 527

	return 0;
528 529 530 531 532

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

535
static int tps6507x_pmic_remove(struct platform_device *pdev)
536
{
T
Todd Fischer 已提交
537 538
	struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
	struct tps6507x_pmic *tps = tps6507x_dev->pmic;
539 540 541 542 543 544 545
	int i;

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

T
Todd Fischer 已提交
546
static struct platform_driver tps6507x_pmic_driver = {
547
	.driver = {
T
Todd Fischer 已提交
548
		.name = "tps6507x-pmic",
549 550
		.owner = THIS_MODULE,
	},
551
	.probe = tps6507x_pmic_probe,
552
	.remove = tps6507x_pmic_remove,
553 554
};

555
static int __init tps6507x_pmic_init(void)
556
{
T
Todd Fischer 已提交
557
	return platform_driver_register(&tps6507x_pmic_driver);
558
}
559
subsys_initcall(tps6507x_pmic_init);
560

561
static void __exit tps6507x_pmic_cleanup(void)
562
{
T
Todd Fischer 已提交
563
	platform_driver_unregister(&tps6507x_pmic_driver);
564
}
565
module_exit(tps6507x_pmic_cleanup);
566 567 568

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