s5m8767.c 20.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * s5m8767.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd
 *              http://www.samsung.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;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/bug.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
S
Sangbeom Kim 已提交
22 23
#include <linux/mfd/samsung/core.h>
#include <linux/mfd/samsung/s5m8767.h>
24 25 26

struct s5m8767_info {
	struct device *dev;
27
	struct sec_pmic_dev *iodev;
28 29
	int num_regulators;
	struct regulator_dev **rdev;
30
	struct sec_opmode_data *opmode;
31 32 33 34 35 36 37 38 39 40 41 42 43

	int ramp_delay;
	bool buck2_ramp;
	bool buck3_ramp;
	bool buck4_ramp;

	bool buck2_gpiodvs;
	bool buck3_gpiodvs;
	bool buck4_gpiodvs;
	u8 buck2_vol[8];
	u8 buck3_vol[8];
	u8 buck4_vol[8];
	int buck_gpios[3];
44
	int buck_ds[3];
45 46 47
	int buck_gpioindex;
};

48
struct sec_voltage_desc {
49 50 51 52 53
	int max;
	int min;
	int step;
};

54
static const struct sec_voltage_desc buck_voltage_val1 = {
55 56 57 58 59
	.max = 2225000,
	.min =  650000,
	.step =   6250,
};

60
static const struct sec_voltage_desc buck_voltage_val2 = {
61 62 63 64 65
	.max = 1600000,
	.min =  600000,
	.step =   6250,
};

66
static const struct sec_voltage_desc buck_voltage_val3 = {
67 68 69 70 71
	.max = 3000000,
	.min =  750000,
	.step =  12500,
};

72
static const struct sec_voltage_desc ldo_voltage_val1 = {
73 74 75 76 77
	.max = 3950000,
	.min =  800000,
	.step =  50000,
};

78
static const struct sec_voltage_desc ldo_voltage_val2 = {
79 80 81 82 83
	.max = 2375000,
	.min =  800000,
	.step =  25000,
};

84
static const struct sec_voltage_desc *reg_voltage_map[] = {
85 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 113 114 115 116 117 118 119 120 121 122 123
	[S5M8767_LDO1] = &ldo_voltage_val2,
	[S5M8767_LDO2] = &ldo_voltage_val2,
	[S5M8767_LDO3] = &ldo_voltage_val1,
	[S5M8767_LDO4] = &ldo_voltage_val1,
	[S5M8767_LDO5] = &ldo_voltage_val1,
	[S5M8767_LDO6] = &ldo_voltage_val2,
	[S5M8767_LDO7] = &ldo_voltage_val2,
	[S5M8767_LDO8] = &ldo_voltage_val2,
	[S5M8767_LDO9] = &ldo_voltage_val1,
	[S5M8767_LDO10] = &ldo_voltage_val1,
	[S5M8767_LDO11] = &ldo_voltage_val1,
	[S5M8767_LDO12] = &ldo_voltage_val1,
	[S5M8767_LDO13] = &ldo_voltage_val1,
	[S5M8767_LDO14] = &ldo_voltage_val1,
	[S5M8767_LDO15] = &ldo_voltage_val2,
	[S5M8767_LDO16] = &ldo_voltage_val1,
	[S5M8767_LDO17] = &ldo_voltage_val1,
	[S5M8767_LDO18] = &ldo_voltage_val1,
	[S5M8767_LDO19] = &ldo_voltage_val1,
	[S5M8767_LDO20] = &ldo_voltage_val1,
	[S5M8767_LDO21] = &ldo_voltage_val1,
	[S5M8767_LDO22] = &ldo_voltage_val1,
	[S5M8767_LDO23] = &ldo_voltage_val1,
	[S5M8767_LDO24] = &ldo_voltage_val1,
	[S5M8767_LDO25] = &ldo_voltage_val1,
	[S5M8767_LDO26] = &ldo_voltage_val1,
	[S5M8767_LDO27] = &ldo_voltage_val1,
	[S5M8767_LDO28] = &ldo_voltage_val1,
	[S5M8767_BUCK1] = &buck_voltage_val1,
	[S5M8767_BUCK2] = &buck_voltage_val2,
	[S5M8767_BUCK3] = &buck_voltage_val2,
	[S5M8767_BUCK4] = &buck_voltage_val2,
	[S5M8767_BUCK5] = &buck_voltage_val1,
	[S5M8767_BUCK6] = &buck_voltage_val1,
	[S5M8767_BUCK7] = NULL,
	[S5M8767_BUCK8] = NULL,
	[S5M8767_BUCK9] = &buck_voltage_val3,
};

124
static unsigned int s5m8767_opmode_reg[][4] = {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	/* {OFF, ON, LOWPOWER, SUSPEND} */
	/* LDO1 ... LDO28 */
	{0x0, 0x3, 0x2, 0x1}, /* LDO1 */
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x0, 0x0, 0x0},
	{0x0, 0x3, 0x2, 0x1}, /* LDO5 */
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1}, /* LDO10 */
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1}, /* LDO15 */
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x0, 0x0, 0x0},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1}, /* LDO20 */
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x0, 0x0, 0x0},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1}, /* LDO25 */
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1},
	{0x0, 0x3, 0x2, 0x1}, /* LDO28 */

	/* BUCK1 ... BUCK9 */
	{0x0, 0x3, 0x1, 0x1}, /* BUCK1 */
	{0x0, 0x3, 0x1, 0x1},
	{0x0, 0x3, 0x1, 0x1},
	{0x0, 0x3, 0x1, 0x1},
	{0x0, 0x3, 0x2, 0x1}, /* BUCK5 */
	{0x0, 0x3, 0x1, 0x1},
	{0x0, 0x3, 0x1, 0x1},
	{0x0, 0x3, 0x1, 0x1},
	{0x0, 0x3, 0x1, 0x1}, /* BUCK9 */
};

static int s5m8767_get_register(struct regulator_dev *rdev, int *reg,
				int *enable_ctrl)
170
{
171
	int i, reg_id = rdev_get_id(rdev);
172 173
	unsigned int mode;
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

	switch (reg_id) {
	case S5M8767_LDO1 ... S5M8767_LDO2:
		*reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
		break;
	case S5M8767_LDO3 ... S5M8767_LDO28:
		*reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
		break;
	case S5M8767_BUCK1:
		*reg = S5M8767_REG_BUCK1CTRL1;
		break;
	case S5M8767_BUCK2 ... S5M8767_BUCK4:
		*reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
		break;
	case S5M8767_BUCK5:
		*reg = S5M8767_REG_BUCK5CTRL1;
		break;
	case S5M8767_BUCK6 ... S5M8767_BUCK9:
		*reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
		break;
	default:
		return -EINVAL;
	}

198 199 200 201 202 203 204 205 206 207 208
	for (i = 0; i < s5m8767->num_regulators; i++) {
		if (s5m8767->opmode[i].id == reg_id) {
			mode = s5m8767->opmode[i].mode;
			break;
		}
	}

	if (i < s5m8767->num_regulators)
		*enable_ctrl =
		s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT;

209 210 211 212 213 214 215
	return 0;
}

static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
	int ret, reg;
216
	int mask = 0xc0, enable_ctrl;
217 218
	u8 val;

219
	ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
220 221 222 223 224
	if (ret == -EINVAL)
		return 1;
	else if (ret)
		return ret;

225
	ret = sec_reg_read(s5m8767->iodev, reg, &val);
226 227 228
	if (ret)
		return ret;

229
	return (val & mask) == enable_ctrl;
230 231 232 233 234 235
}

static int s5m8767_reg_enable(struct regulator_dev *rdev)
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
	int ret, reg;
236
	int mask = 0xc0, enable_ctrl;
237

238
	ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
239 240 241
	if (ret)
		return ret;

242
	return sec_reg_update(s5m8767->iodev, reg, enable_ctrl, mask);
243 244 245 246 247 248
}

static int s5m8767_reg_disable(struct regulator_dev *rdev)
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
	int ret, reg;
249
	int  mask = 0xc0, enable_ctrl;
250

251
	ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
252 253 254
	if (ret)
		return ret;

255
	return sec_reg_update(s5m8767->iodev, reg, ~mask, mask);
256 257 258 259
}

static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
{
260
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
261
	int reg_id = rdev_get_id(rdev);
262 263 264 265 266 267 268 269 270 271 272 273 274
	int reg;

	switch (reg_id) {
	case S5M8767_LDO1 ... S5M8767_LDO2:
		reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
		break;
	case S5M8767_LDO3 ... S5M8767_LDO28:
		reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
		break;
	case S5M8767_BUCK1:
		reg = S5M8767_REG_BUCK1CTRL2;
		break;
	case S5M8767_BUCK2:
275
		reg = S5M8767_REG_BUCK2DVS2;
276 277
		if (s5m8767->buck2_gpiodvs)
			reg += s5m8767->buck_gpioindex;
278 279
		break;
	case S5M8767_BUCK3:
280
		reg = S5M8767_REG_BUCK3DVS2;
281 282
		if (s5m8767->buck3_gpiodvs)
			reg += s5m8767->buck_gpioindex;
283 284
		break;
	case S5M8767_BUCK4:
285
		reg = S5M8767_REG_BUCK4DVS2;
286 287
		if (s5m8767->buck4_gpiodvs)
			reg += s5m8767->buck_gpioindex;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		break;
	case S5M8767_BUCK5:
		reg = S5M8767_REG_BUCK5CTRL2;
		break;
	case S5M8767_BUCK6 ... S5M8767_BUCK9:
		reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
		break;
	default:
		return -EINVAL;
	}

	*_reg = reg;

	return 0;
}

static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
307
	int reg, mask, ret;
308
	int reg_id = rdev_get_id(rdev);
309 310 311 312 313 314
	u8 val;

	ret = s5m8767_get_voltage_register(rdev, &reg);
	if (ret)
		return ret;

315
	mask = (reg_id < S5M8767_BUCK1) ? 0x3f : 0xff;
316

317
	ret = sec_reg_read(s5m8767->iodev, reg, &val);
318 319 320 321 322 323 324 325
	if (ret)
		return ret;

	val &= mask;

	return val;
}

326
static int s5m8767_convert_voltage_to_sel(
327
		const struct sec_voltage_desc *desc,
328 329
		int min_vol, int max_vol)
{
330
	int selector = 0;
331 332 333 334 335 336 337

	if (desc == NULL)
		return -EINVAL;

	if (max_vol < desc->min || min_vol > desc->max)
		return -EINVAL;

338 339 340 341
	if (min_vol < desc->min)
		min_vol = desc->min;

	selector = DIV_ROUND_UP(min_vol - desc->min, desc->step);
342

343
	if (desc->min + desc->step * selector > max_vol)
344 345
		return -EINVAL;

346
	return selector;
347 348
}

349
static inline int s5m8767_set_high(struct s5m8767_info *s5m8767)
350 351 352 353 354 355
{
	int temp_index = s5m8767->buck_gpioindex;

	gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
	gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
	gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
356 357

	return 0;
358 359
}

360
static inline int s5m8767_set_low(struct s5m8767_info *s5m8767)
361 362 363 364 365 366
{
	int temp_index = s5m8767->buck_gpioindex;

	gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
	gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
	gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
367 368

	return 0;
369 370
}

371 372
static int s5m8767_set_voltage_sel(struct regulator_dev *rdev,
				   unsigned selector)
373 374
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
375
	int reg_id = rdev_get_id(rdev);
376
	int reg, mask, ret = 0, old_index, index = 0;
377
	u8 *buck234_vol = NULL;
378 379 380 381 382 383 384

	switch (reg_id) {
	case S5M8767_LDO1 ... S5M8767_LDO28:
		mask = 0x3f;
		break;
	case S5M8767_BUCK1 ... S5M8767_BUCK6:
		mask = 0xff;
385 386 387 388 389 390
		if (reg_id == S5M8767_BUCK2 && s5m8767->buck2_gpiodvs)
			buck234_vol = &s5m8767->buck2_vol[0];
		else if (reg_id == S5M8767_BUCK3 && s5m8767->buck3_gpiodvs)
			buck234_vol = &s5m8767->buck3_vol[0];
		else if (reg_id == S5M8767_BUCK4 && s5m8767->buck4_gpiodvs)
			buck234_vol = &s5m8767->buck4_vol[0];
391 392 393 394 395 396 397 398 399 400
		break;
	case S5M8767_BUCK7 ... S5M8767_BUCK8:
		return -EINVAL;
	case S5M8767_BUCK9:
		mask = 0xff;
		break;
	default:
		return -EINVAL;
	}

401 402
	/* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
	if (buck234_vol) {
403
		while (*buck234_vol != selector) {
404 405 406 407 408 409 410
			buck234_vol++;
			index++;
		}
		old_index = s5m8767->buck_gpioindex;
		s5m8767->buck_gpioindex = index;

		if (index > old_index)
411
			return s5m8767_set_high(s5m8767);
412
		else
413
			return s5m8767_set_low(s5m8767);
414 415 416 417 418
	} else {
		ret = s5m8767_get_voltage_register(rdev, &reg);
		if (ret)
			return ret;

419
		return sec_reg_update(s5m8767->iodev, reg, selector, mask);
420
	}
421 422 423 424 425 426 427
}

static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
					     unsigned int old_sel,
					     unsigned int new_sel)
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
428
	const struct sec_voltage_desc *desc;
429
	int reg_id = rdev_get_id(rdev);
430 431 432

	desc = reg_voltage_map[reg_id];

433
	if ((old_sel < new_sel) && s5m8767->ramp_delay)
434
		return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
435
					s5m8767->ramp_delay * 1000);
436
	return 0;
437 438
}

439
static struct regulator_ops s5m8767_ops = {
440
	.list_voltage		= regulator_list_voltage_linear,
441 442 443 444
	.is_enabled		= s5m8767_reg_is_enabled,
	.enable			= s5m8767_reg_enable,
	.disable		= s5m8767_reg_disable,
	.get_voltage_sel	= s5m8767_get_voltage_sel,
445
	.set_voltage_sel	= s5m8767_set_voltage_sel,
446 447 448
	.set_voltage_time_sel	= s5m8767_set_voltage_time_sel,
};

449 450 451 452 453 454
static struct regulator_ops s5m8767_buck78_ops = {
	.is_enabled		= s5m8767_reg_is_enabled,
	.enable			= s5m8767_reg_enable,
	.disable		= s5m8767_reg_disable,
};

455 456 457 458
#define s5m8767_regulator_desc(_name) {		\
	.name		= #_name,		\
	.id		= S5M8767_##_name,	\
	.ops		= &s5m8767_ops,		\
459 460 461 462
	.type		= REGULATOR_VOLTAGE,	\
	.owner		= THIS_MODULE,		\
}

463 464 465 466 467 468 469 470
#define s5m8767_regulator_buck78_desc(_name) {	\
	.name		= #_name,		\
	.id		= S5M8767_##_name,	\
	.ops		= &s5m8767_buck78_ops,	\
	.type		= REGULATOR_VOLTAGE,	\
	.owner		= THIS_MODULE,		\
}

471
static struct regulator_desc regulators[] = {
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
	s5m8767_regulator_desc(LDO1),
	s5m8767_regulator_desc(LDO2),
	s5m8767_regulator_desc(LDO3),
	s5m8767_regulator_desc(LDO4),
	s5m8767_regulator_desc(LDO5),
	s5m8767_regulator_desc(LDO6),
	s5m8767_regulator_desc(LDO7),
	s5m8767_regulator_desc(LDO8),
	s5m8767_regulator_desc(LDO9),
	s5m8767_regulator_desc(LDO10),
	s5m8767_regulator_desc(LDO11),
	s5m8767_regulator_desc(LDO12),
	s5m8767_regulator_desc(LDO13),
	s5m8767_regulator_desc(LDO14),
	s5m8767_regulator_desc(LDO15),
	s5m8767_regulator_desc(LDO16),
	s5m8767_regulator_desc(LDO17),
	s5m8767_regulator_desc(LDO18),
	s5m8767_regulator_desc(LDO19),
	s5m8767_regulator_desc(LDO20),
	s5m8767_regulator_desc(LDO21),
	s5m8767_regulator_desc(LDO22),
	s5m8767_regulator_desc(LDO23),
	s5m8767_regulator_desc(LDO24),
	s5m8767_regulator_desc(LDO25),
	s5m8767_regulator_desc(LDO26),
	s5m8767_regulator_desc(LDO27),
	s5m8767_regulator_desc(LDO28),
	s5m8767_regulator_desc(BUCK1),
	s5m8767_regulator_desc(BUCK2),
	s5m8767_regulator_desc(BUCK3),
	s5m8767_regulator_desc(BUCK4),
	s5m8767_regulator_desc(BUCK5),
	s5m8767_regulator_desc(BUCK6),
506 507
	s5m8767_regulator_buck78_desc(BUCK7),
	s5m8767_regulator_buck78_desc(BUCK8),
508
	s5m8767_regulator_desc(BUCK9),
509 510 511 512
};

static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
{
513 514
	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
	struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
515
	struct regulator_config config = { };
516 517
	struct regulator_dev **rdev;
	struct s5m8767_info *s5m8767;
518
	int i, ret, size, buck_init;
519 520 521 522 523 524

	if (!pdata) {
		dev_err(pdev->dev.parent, "Platform data not supplied\n");
		return -ENODEV;
	}

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	if (pdata->buck2_gpiodvs) {
		if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
			dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
			return -EINVAL;
		}
	}

	if (pdata->buck3_gpiodvs) {
		if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
			dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
			return -EINVAL;
		}
	}

	if (pdata->buck4_gpiodvs) {
		if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
			dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
			return -EINVAL;
		}
	}

546 547 548 549 550 551 552 553 554 555 556 557 558
	s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
				GFP_KERNEL);
	if (!s5m8767)
		return -ENOMEM;

	size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
	s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
	if (!s5m8767->rdev)
		return -ENOMEM;

	rdev = s5m8767->rdev;
	s5m8767->dev = &pdev->dev;
	s5m8767->iodev = iodev;
559
	s5m8767->num_regulators = pdata->num_regulators;
560 561 562 563 564 565 566 567 568
	platform_set_drvdata(pdev, s5m8767);

	s5m8767->buck_gpioindex = pdata->buck_default_idx;
	s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
	s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
	s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
	s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
	s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
	s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
569 570 571 572
	s5m8767->buck_ds[0] = pdata->buck_ds[0];
	s5m8767->buck_ds[1] = pdata->buck_ds[1];
	s5m8767->buck_ds[2] = pdata->buck_ds[2];

573 574 575 576
	s5m8767->ramp_delay = pdata->buck_ramp_delay;
	s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
	s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
	s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
577
	s5m8767->opmode = pdata->opmode;
578

579 580 581 582 583
	buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
						pdata->buck2_init,
						pdata->buck2_init +
						buck_voltage_val2.step);

584
	sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS2, buck_init);
585 586 587 588 589 590

	buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
						pdata->buck3_init,
						pdata->buck3_init +
						buck_voltage_val2.step);

591
	sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS2, buck_init);
592 593 594 595 596 597

	buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
						pdata->buck4_init,
						pdata->buck4_init +
						buck_voltage_val2.step);

598
	sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS2, buck_init);
599

600 601 602
	for (i = 0; i < 8; i++) {
		if (s5m8767->buck2_gpiodvs) {
			s5m8767->buck2_vol[i] =
603
				s5m8767_convert_voltage_to_sel(
604 605 606 607 608 609 610 611
						&buck_voltage_val2,
						pdata->buck2_voltage[i],
						pdata->buck2_voltage[i] +
						buck_voltage_val2.step);
		}

		if (s5m8767->buck3_gpiodvs) {
			s5m8767->buck3_vol[i] =
612
				s5m8767_convert_voltage_to_sel(
613 614 615 616 617 618 619 620
						&buck_voltage_val2,
						pdata->buck3_voltage[i],
						pdata->buck3_voltage[i] +
						buck_voltage_val2.step);
		}

		if (s5m8767->buck4_gpiodvs) {
			s5m8767->buck4_vol[i] =
621
				s5m8767_convert_voltage_to_sel(
622 623 624 625 626 627 628
						&buck_voltage_val2,
						pdata->buck4_voltage[i],
						pdata->buck4_voltage[i] +
						buck_voltage_val2.step);
		}
	}

629 630 631
	if (gpio_is_valid(pdata->buck_gpios[0]) &&
		gpio_is_valid(pdata->buck_gpios[1]) &&
		gpio_is_valid(pdata->buck_gpios[2])) {
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
		ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[0],
					"S5M8767 SET1");
		if (ret)
			return ret;

		ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[1],
					"S5M8767 SET2");
		if (ret)
			return ret;

		ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[2],
					"S5M8767 SET3");
		if (ret)
			return ret;

647 648 649 650 651 652 653 654 655 656 657 658 659
		/* SET1 GPIO */
		gpio_direction_output(pdata->buck_gpios[0],
				(s5m8767->buck_gpioindex >> 2) & 0x1);
		/* SET2 GPIO */
		gpio_direction_output(pdata->buck_gpios[1],
				(s5m8767->buck_gpioindex >> 1) & 0x1);
		/* SET3 GPIO */
		gpio_direction_output(pdata->buck_gpios[2],
				(s5m8767->buck_gpioindex >> 0) & 0x1);
	} else {
		dev_err(&pdev->dev, "GPIO NOT VALID\n");
		ret = -EINVAL;
		return ret;
660 661
	}

662 663 664
	ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[0], "S5M8767 DS2");
	if (ret)
		return ret;
665

666 667 668
	ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[1], "S5M8767 DS3");
	if (ret)
		return ret;
669

670 671 672
	ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[2], "S5M8767 DS4");
	if (ret)
		return ret;
673 674 675 676 677 678 679 680 681 682

	/* DS2 GPIO */
	gpio_direction_output(pdata->buck_ds[0], 0x0);
	/* DS3 GPIO */
	gpio_direction_output(pdata->buck_ds[1], 0x0);
	/* DS4 GPIO */
	gpio_direction_output(pdata->buck_ds[2], 0x0);

	if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
	   pdata->buck4_gpiodvs) {
683
		sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
684 685
				(pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1),
				1 << 1);
686
		sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
687 688
				(pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1),
				1 << 1);
689
		sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
690 691 692
				(pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1),
				1 << 1);
	}
693 694 695 696

	/* Initialize GPIO DVS registers */
	for (i = 0; i < 8; i++) {
		if (s5m8767->buck2_gpiodvs) {
697
			sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
698 699 700 701
					   s5m8767->buck2_vol[i]);
		}

		if (s5m8767->buck3_gpiodvs) {
702
			sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
703 704 705 706
					   s5m8767->buck3_vol[i]);
		}

		if (s5m8767->buck4_gpiodvs) {
707
			sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
708 709 710 711 712
					   s5m8767->buck4_vol[i]);
		}
	}

	if (s5m8767->buck2_ramp)
713
		sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
714 715

	if (s5m8767->buck3_ramp)
716
		sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
717 718

	if (s5m8767->buck4_ramp)
719
		sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
720 721 722 723

	if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
		|| s5m8767->buck4_ramp) {
		switch (s5m8767->ramp_delay) {
724
		case 5:
725
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
726 727 728
					0x40, 0xf0);
			break;
		case 10:
729
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
730
					0x90, 0xf0);
731
			break;
732
		case 25:
733
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
734
					0xd0, 0xf0);
735
			break;
736
		case 50:
737
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
738
					0xe0, 0xf0);
739
			break;
740
		case 100:
741
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
742
					0xf0, 0xf0);
743
			break;
744
		default:
745
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
746 747 748 749 750
					0x90, 0xf0);
		}
	}

	for (i = 0; i < pdata->num_regulators; i++) {
751
		const struct sec_voltage_desc *desc;
752 753 754
		int id = pdata->regulators[i].id;

		desc = reg_voltage_map[id];
755
		if (desc) {
756 757
			regulators[id].n_voltages =
				(desc->max - desc->min) / desc->step + 1;
758 759 760
			regulators[id].min_uV = desc->min;
			regulators[id].uV_step = desc->step;
		}
761

762 763 764 765 766
		config.dev = s5m8767->dev;
		config.init_data = pdata->regulators[i].initdata;
		config.driver_data = s5m8767;

		rdev[i] = regulator_register(&regulators[id], &config);
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
		if (IS_ERR(rdev[i])) {
			ret = PTR_ERR(rdev[i]);
			dev_err(s5m8767->dev, "regulator init failed for %d\n",
					id);
			rdev[i] = NULL;
			goto err;
		}
	}

	return 0;
err:
	for (i = 0; i < s5m8767->num_regulators; i++)
		if (rdev[i])
			regulator_unregister(rdev[i]);

	return ret;
}

static int __devexit s5m8767_pmic_remove(struct platform_device *pdev)
{
	struct s5m8767_info *s5m8767 = platform_get_drvdata(pdev);
	struct regulator_dev **rdev = s5m8767->rdev;
	int i;

	for (i = 0; i < s5m8767->num_regulators; i++)
		if (rdev[i])
			regulator_unregister(rdev[i]);

	return 0;
}

static const struct platform_device_id s5m8767_pmic_id[] = {
	{ "s5m8767-pmic", 0},
	{ },
};
MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);

static struct platform_driver s5m8767_pmic_driver = {
	.driver = {
		.name = "s5m8767-pmic",
		.owner = THIS_MODULE,
	},
	.probe = s5m8767_pmic_probe,
	.remove = __devexit_p(s5m8767_pmic_remove),
	.id_table = s5m8767_pmic_id,
};

static int __init s5m8767_pmic_init(void)
{
	return platform_driver_register(&s5m8767_pmic_driver);
}
subsys_initcall(s5m8767_pmic_init);

static void __exit s5m8767_pmic_exit(void)
{
	platform_driver_unregister(&s5m8767_pmic_driver);
}
module_exit(s5m8767_pmic_exit);

/* Module information */
MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
MODULE_LICENSE("GPL");