s5m8767.c 24.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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>
17
#include <linux/of_gpio.h>
18 19 20 21 22
#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 已提交
23 24
#include <linux/mfd/samsung/core.h>
#include <linux/mfd/samsung/s5m8767.h>
25 26 27
#include <linux/regulator/of_regulator.h>

#define S5M8767_OPMODE_NORMAL_MODE 0x1
28 29 30

struct s5m8767_info {
	struct device *dev;
31
	struct sec_pmic_dev *iodev;
32 33
	int num_regulators;
	struct regulator_dev **rdev;
34
	struct sec_opmode_data *opmode;
35 36 37 38 39 40 41 42 43 44 45 46 47

	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];
48
	int buck_ds[3];
49 50 51
	int buck_gpioindex;
};

52
struct sec_voltage_desc {
53 54 55 56 57
	int max;
	int min;
	int step;
};

58
static const struct sec_voltage_desc buck_voltage_val1 = {
59 60 61 62 63
	.max = 2225000,
	.min =  650000,
	.step =   6250,
};

64
static const struct sec_voltage_desc buck_voltage_val2 = {
65 66 67 68 69
	.max = 1600000,
	.min =  600000,
	.step =   6250,
};

70
static const struct sec_voltage_desc buck_voltage_val3 = {
71 72 73 74 75
	.max = 3000000,
	.min =  750000,
	.step =  12500,
};

76
static const struct sec_voltage_desc ldo_voltage_val1 = {
77 78 79 80 81
	.max = 3950000,
	.min =  800000,
	.step =  50000,
};

82
static const struct sec_voltage_desc ldo_voltage_val2 = {
83 84 85 86 87
	.max = 2375000,
	.min =  800000,
	.step =  25000,
};

88
static const struct sec_voltage_desc *reg_voltage_map[] = {
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 124 125 126 127
	[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,
};

128
static unsigned int s5m8767_opmode_reg[][4] = {
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 170 171 172 173
	/* {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)
174
{
175
	int i, reg_id = rdev_get_id(rdev);
176 177
	unsigned int mode;
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

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

202 203 204 205 206 207 208 209 210 211 212
	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;

213 214 215 216 217 218 219
	return 0;
}

static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
	int ret, reg;
220
	int mask = 0xc0, enable_ctrl;
221
	unsigned int val;
222

223
	ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
224 225 226 227 228
	if (ret == -EINVAL)
		return 1;
	else if (ret)
		return ret;

229
	ret = sec_reg_read(s5m8767->iodev, reg, &val);
230 231 232
	if (ret)
		return ret;

233
	return (val & mask) == enable_ctrl;
234 235 236 237 238 239
}

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

242
	ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
243 244 245
	if (ret)
		return ret;

246
	return sec_reg_update(s5m8767->iodev, reg, enable_ctrl, mask);
247 248 249 250 251 252
}

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

255
	ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
256 257 258
	if (ret)
		return ret;

259
	return sec_reg_update(s5m8767->iodev, reg, ~mask, mask);
260 261
}

262
static int s5m8767_get_vsel_reg(int reg_id, struct s5m8767_info *s5m8767)
263 264 265 266 267 268 269 270 271 272 273 274 275 276
{
	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:
277
		reg = S5M8767_REG_BUCK2DVS1;
278 279
		if (s5m8767->buck2_gpiodvs)
			reg += s5m8767->buck_gpioindex;
280 281
		break;
	case S5M8767_BUCK3:
282
		reg = S5M8767_REG_BUCK3DVS1;
283 284
		if (s5m8767->buck3_gpiodvs)
			reg += s5m8767->buck_gpioindex;
285 286
		break;
	case S5M8767_BUCK4:
287
		reg = S5M8767_REG_BUCK4DVS1;
288 289
		if (s5m8767->buck4_gpiodvs)
			reg += s5m8767->buck_gpioindex;
290 291 292 293 294 295 296 297 298 299 300
		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;
	}

301
	return reg;
302 303
}

304 305
static int s5m8767_convert_voltage_to_sel(const struct sec_voltage_desc *desc,
					  int min_vol)
306
{
307
	int selector = 0;
308 309 310 311

	if (desc == NULL)
		return -EINVAL;

312
	if (min_vol > desc->max)
313 314
		return -EINVAL;

315 316 317 318
	if (min_vol < desc->min)
		min_vol = desc->min;

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

320
	if (desc->min + desc->step * selector > desc->max)
321 322
		return -EINVAL;

323
	return selector;
324 325
}

326
static inline int s5m8767_set_high(struct s5m8767_info *s5m8767)
327 328 329 330 331 332
{
	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);
333 334

	return 0;
335 336
}

337
static inline int s5m8767_set_low(struct s5m8767_info *s5m8767)
338 339 340 341 342 343
{
	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);
344 345

	return 0;
346 347
}

348 349
static int s5m8767_set_voltage_sel(struct regulator_dev *rdev,
				   unsigned selector)
350 351
{
	struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
352
	int reg_id = rdev_get_id(rdev);
353
	int old_index, index = 0;
354
	u8 *buck234_vol = NULL;
355 356 357 358 359

	switch (reg_id) {
	case S5M8767_LDO1 ... S5M8767_LDO28:
		break;
	case S5M8767_BUCK1 ... S5M8767_BUCK6:
360 361 362 363 364 365
		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];
366 367 368 369 370 371 372 373 374
		break;
	case S5M8767_BUCK7 ... S5M8767_BUCK8:
		return -EINVAL;
	case S5M8767_BUCK9:
		break;
	default:
		return -EINVAL;
	}

375 376
	/* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
	if (buck234_vol) {
377
		while (*buck234_vol != selector) {
378 379 380 381 382 383 384
			buck234_vol++;
			index++;
		}
		old_index = s5m8767->buck_gpioindex;
		s5m8767->buck_gpioindex = index;

		if (index > old_index)
385
			return s5m8767_set_high(s5m8767);
386
		else
387
			return s5m8767_set_low(s5m8767);
388
	} else {
389
		return regulator_set_voltage_sel_regmap(rdev, selector);
390
	}
391 392 393 394 395 396 397
}

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);
398
	const struct sec_voltage_desc *desc;
399
	int reg_id = rdev_get_id(rdev);
400 401 402

	desc = reg_voltage_map[reg_id];

403
	if ((old_sel < new_sel) && s5m8767->ramp_delay)
404
		return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
405
					s5m8767->ramp_delay * 1000);
406
	return 0;
407 408
}

409
static struct regulator_ops s5m8767_ops = {
410
	.list_voltage		= regulator_list_voltage_linear,
411 412 413
	.is_enabled		= s5m8767_reg_is_enabled,
	.enable			= s5m8767_reg_enable,
	.disable		= s5m8767_reg_disable,
414
	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
415
	.set_voltage_sel	= s5m8767_set_voltage_sel,
416 417 418
	.set_voltage_time_sel	= s5m8767_set_voltage_time_sel,
};

419 420 421 422 423 424
static struct regulator_ops s5m8767_buck78_ops = {
	.is_enabled		= s5m8767_reg_is_enabled,
	.enable			= s5m8767_reg_enable,
	.disable		= s5m8767_reg_disable,
};

425 426 427 428
#define s5m8767_regulator_desc(_name) {		\
	.name		= #_name,		\
	.id		= S5M8767_##_name,	\
	.ops		= &s5m8767_ops,		\
429 430 431 432
	.type		= REGULATOR_VOLTAGE,	\
	.owner		= THIS_MODULE,		\
}

433 434 435 436 437 438 439 440
#define s5m8767_regulator_buck78_desc(_name) {	\
	.name		= #_name,		\
	.id		= S5M8767_##_name,	\
	.ops		= &s5m8767_buck78_ops,	\
	.type		= REGULATOR_VOLTAGE,	\
	.owner		= THIS_MODULE,		\
}

441
static struct regulator_desc regulators[] = {
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	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),
476 477
	s5m8767_regulator_buck78_desc(BUCK7),
	s5m8767_regulator_buck78_desc(BUCK8),
478
	s5m8767_regulator_desc(BUCK9),
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 509 510 511 512 513 514 515 516 517
#ifdef CONFIG_OF
static int s5m8767_pmic_dt_parse_dvs_gpio(struct sec_pmic_dev *iodev,
			struct sec_platform_data *pdata,
			struct device_node *pmic_np)
{
	int i, gpio;

	for (i = 0; i < 3; i++) {
		gpio = of_get_named_gpio(pmic_np,
					"s5m8767,pmic-buck-dvs-gpios", i);
		if (!gpio_is_valid(gpio)) {
			dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
			return -EINVAL;
		}
		pdata->buck_gpios[i] = gpio;
	}
	return 0;
}

static int s5m8767_pmic_dt_parse_ds_gpio(struct sec_pmic_dev *iodev,
			struct sec_platform_data *pdata,
			struct device_node *pmic_np)
{
	int i, gpio;

	for (i = 0; i < 3; i++) {
		gpio = of_get_named_gpio(pmic_np,
					"s5m8767,pmic-buck-ds-gpios", i);
		if (!gpio_is_valid(gpio)) {
			dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
			return -EINVAL;
		}
		pdata->buck_ds[i] = gpio;
	}
	return 0;
}

518
static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
519 520
					struct sec_platform_data *pdata)
{
521
	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
522 523 524
	struct device_node *pmic_np, *regulators_np, *reg_np;
	struct sec_regulator_data *rdata;
	struct sec_opmode_data *rmode;
525
	unsigned int i, dvs_voltage_nr = 8, ret;
526 527 528 529 530 531 532 533 534 535 536 537 538 539

	pmic_np = iodev->dev->of_node;
	if (!pmic_np) {
		dev_err(iodev->dev, "could not find pmic sub-node\n");
		return -ENODEV;
	}

	regulators_np = of_find_node_by_name(pmic_np, "regulators");
	if (!regulators_np) {
		dev_err(iodev->dev, "could not find regulators sub-node\n");
		return -EINVAL;
	}

	/* count the number of regulators to be supported in pmic */
540
	pdata->num_regulators = of_get_child_count(regulators_np);
541

542
	rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
543 544 545 546 547 548 549
				pdata->num_regulators, GFP_KERNEL);
	if (!rdata) {
		dev_err(iodev->dev,
			"could not allocate memory for regulator data\n");
		return -ENOMEM;
	}

550
	rmode = devm_kzalloc(&pdev->dev, sizeof(*rmode) *
551
				pdata->num_regulators, GFP_KERNEL);
552
	if (!rmode) {
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
		dev_err(iodev->dev,
			"could not allocate memory for regulator mode\n");
		return -ENOMEM;
	}

	pdata->regulators = rdata;
	pdata->opmode = rmode;
	for_each_child_of_node(regulators_np, reg_np) {
		for (i = 0; i < ARRAY_SIZE(regulators); i++)
			if (!of_node_cmp(reg_np->name, regulators[i].name))
				break;

		if (i == ARRAY_SIZE(regulators)) {
			dev_warn(iodev->dev,
			"don't know how to configure regulator %s\n",
			reg_np->name);
			continue;
		}

		rdata->id = i;
		rdata->initdata = of_get_regulator_init_data(
574
						&pdev->dev, reg_np);
575 576 577 578 579 580 581 582 583 584 585 586 587 588
		rdata->reg_node = reg_np;
		rdata++;
		rmode->id = i;
		if (of_property_read_u32(reg_np, "op_mode",
				&rmode->mode)) {
			dev_warn(iodev->dev,
				"no op_mode property property at %s\n",
				reg_np->full_name);

			rmode->mode = S5M8767_OPMODE_NORMAL_MODE;
		}
		rmode++;
	}

589
	if (of_get_property(pmic_np, "s5m8767,pmic-buck2-uses-gpio-dvs", NULL)) {
590 591
		pdata->buck2_gpiodvs = true;

592 593 594 595 596 597 598 599 600
		if (of_property_read_u32_array(pmic_np,
				"s5m8767,pmic-buck2-dvs-voltage",
				pdata->buck2_voltage, dvs_voltage_nr)) {
			dev_err(iodev->dev, "buck2 voltages not specified\n");
			return -EINVAL;
		}
	}

	if (of_get_property(pmic_np, "s5m8767,pmic-buck3-uses-gpio-dvs", NULL)) {
601 602
		pdata->buck3_gpiodvs = true;

603 604 605 606 607 608 609 610 611
		if (of_property_read_u32_array(pmic_np,
				"s5m8767,pmic-buck3-dvs-voltage",
				pdata->buck3_voltage, dvs_voltage_nr)) {
			dev_err(iodev->dev, "buck3 voltages not specified\n");
			return -EINVAL;
		}
	}

	if (of_get_property(pmic_np, "s5m8767,pmic-buck4-uses-gpio-dvs", NULL)) {
612 613
		pdata->buck4_gpiodvs = true;

614 615 616 617 618 619 620 621
		if (of_property_read_u32_array(pmic_np,
				"s5m8767,pmic-buck4-dvs-voltage",
				pdata->buck4_voltage, dvs_voltage_nr)) {
			dev_err(iodev->dev, "buck4 voltages not specified\n");
			return -EINVAL;
		}
	}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
	if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
						pdata->buck4_gpiodvs) {
		ret = s5m8767_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
		if (ret)
			return -EINVAL;

		if (of_property_read_u32(pmic_np,
				"s5m8767,pmic-buck-default-dvs-idx",
				&pdata->buck_default_idx)) {
			pdata->buck_default_idx = 0;
		} else {
			if (pdata->buck_default_idx >= 8) {
				pdata->buck_default_idx = 0;
				dev_info(iodev->dev,
				"invalid value for default dvs index, use 0\n");
			}
		}
	}

	ret = s5m8767_pmic_dt_parse_ds_gpio(iodev, pdata, pmic_np);
	if (ret)
		return -EINVAL;

645 646
	if (of_get_property(pmic_np, "s5m8767,pmic-buck2-ramp-enable", NULL))
		pdata->buck2_ramp_enable = true;
647

648 649
	if (of_get_property(pmic_np, "s5m8767,pmic-buck3-ramp-enable", NULL))
		pdata->buck3_ramp_enable = true;
650

651 652 653 654 655 656 657 658
	if (of_get_property(pmic_np, "s5m8767,pmic-buck4-ramp-enable", NULL))
		pdata->buck4_ramp_enable = true;

	if (pdata->buck2_ramp_enable || pdata->buck3_ramp_enable
			|| pdata->buck4_ramp_enable) {
		if (of_property_read_u32(pmic_np, "s5m8767,pmic-buck-ramp-delay",
				&pdata->buck_ramp_delay))
			pdata->buck_ramp_delay = 0;
659 660 661 662 663
	}

	return 0;
}
#else
664
static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
665 666 667 668 669 670
					struct sec_platform_data *pdata)
{
	return 0;
}
#endif /* CONFIG_OF */

671
static int s5m8767_pmic_probe(struct platform_device *pdev)
672
{
673
	struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
674
	struct sec_platform_data *pdata = iodev->pdata;
675
	struct regulator_config config = { };
676 677
	struct regulator_dev **rdev;
	struct s5m8767_info *s5m8767;
678
	int i, ret, size, buck_init;
679

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

685
	if (iodev->dev->of_node) {
686
		ret = s5m8767_pmic_dt_parse_pdata(pdev, pdata);
687 688 689 690
		if (ret)
			return ret;
	}

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	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;
		}
	}

712 713 714 715 716 717 718 719 720 721 722 723 724
	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;
725
	s5m8767->num_regulators = pdata->num_regulators;
726 727 728 729 730 731 732 733 734
	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];
735 736 737 738
	s5m8767->buck_ds[0] = pdata->buck_ds[0];
	s5m8767->buck_ds[1] = pdata->buck_ds[1];
	s5m8767->buck_ds[2] = pdata->buck_ds[2];

739 740 741 742
	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;
743
	s5m8767->opmode = pdata->opmode;
744

745
	buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
746
						   pdata->buck2_init);
747

748
	sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS2, buck_init);
749 750

	buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
751
						   pdata->buck3_init);
752

753
	sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS2, buck_init);
754 755

	buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
756
						   pdata->buck4_init);
757

758
	sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS2, buck_init);
759

760 761 762
	for (i = 0; i < 8; i++) {
		if (s5m8767->buck2_gpiodvs) {
			s5m8767->buck2_vol[i] =
763
				s5m8767_convert_voltage_to_sel(
764
						&buck_voltage_val2,
765
						pdata->buck2_voltage[i]);
766 767 768 769
		}

		if (s5m8767->buck3_gpiodvs) {
			s5m8767->buck3_vol[i] =
770
				s5m8767_convert_voltage_to_sel(
771
						&buck_voltage_val2,
772
						pdata->buck3_voltage[i]);
773 774 775 776
		}

		if (s5m8767->buck4_gpiodvs) {
			s5m8767->buck4_vol[i] =
777
				s5m8767_convert_voltage_to_sel(
778
						&buck_voltage_val2,
779
						pdata->buck4_voltage[i]);
780 781 782
		}
	}

783 784 785 786 787 788 789 790 791 792
	if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
						pdata->buck4_gpiodvs) {

		if (!gpio_is_valid(pdata->buck_gpios[0]) ||
			!gpio_is_valid(pdata->buck_gpios[1]) ||
			!gpio_is_valid(pdata->buck_gpios[2])) {
			dev_err(&pdev->dev, "GPIO NOT VALID\n");
			return -EINVAL;
		}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
		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;

808 809 810 811 812 813 814 815 816
		/* 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);
817 818
	}

819 820 821
	ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[0], "S5M8767 DS2");
	if (ret)
		return ret;
822

823 824 825
	ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[1], "S5M8767 DS3");
	if (ret)
		return ret;
826

827 828 829
	ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[2], "S5M8767 DS4");
	if (ret)
		return ret;
830 831 832 833 834 835 836 837 838 839

	/* 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) {
840
		sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
841 842
				(pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1),
				1 << 1);
843
		sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
844 845
				(pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1),
				1 << 1);
846
		sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
847 848 849
				(pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1),
				1 << 1);
	}
850 851 852 853

	/* Initialize GPIO DVS registers */
	for (i = 0; i < 8; i++) {
		if (s5m8767->buck2_gpiodvs) {
854
			sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
855 856 857 858
					   s5m8767->buck2_vol[i]);
		}

		if (s5m8767->buck3_gpiodvs) {
859
			sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
860 861 862 863
					   s5m8767->buck3_vol[i]);
		}

		if (s5m8767->buck4_gpiodvs) {
864
			sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
865 866 867 868 869
					   s5m8767->buck4_vol[i]);
		}
	}

	if (s5m8767->buck2_ramp)
870
		sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
871 872

	if (s5m8767->buck3_ramp)
873
		sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
874 875

	if (s5m8767->buck4_ramp)
876
		sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
877 878 879 880

	if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
		|| s5m8767->buck4_ramp) {
		switch (s5m8767->ramp_delay) {
881
		case 5:
882
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
883 884 885
					0x40, 0xf0);
			break;
		case 10:
886
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
887
					0x90, 0xf0);
888
			break;
889
		case 25:
890
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
891
					0xd0, 0xf0);
892
			break;
893
		case 50:
894
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
895
					0xe0, 0xf0);
896
			break;
897
		case 100:
898
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
899
					0xf0, 0xf0);
900
			break;
901
		default:
902
			sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
903 904 905 906 907
					0x90, 0xf0);
		}
	}

	for (i = 0; i < pdata->num_regulators; i++) {
908
		const struct sec_voltage_desc *desc;
909 910 911
		int id = pdata->regulators[i].id;

		desc = reg_voltage_map[id];
912
		if (desc) {
913 914
			regulators[id].n_voltages =
				(desc->max - desc->min) / desc->step + 1;
915 916
			regulators[id].min_uV = desc->min;
			regulators[id].uV_step = desc->step;
917 918 919 920 921 922
			regulators[id].vsel_reg =
				s5m8767_get_vsel_reg(id, s5m8767);
			if (id < S5M8767_BUCK1)
				regulators[id].vsel_mask = 0x3f;
			else
				regulators[id].vsel_mask = 0xff;
923
		}
924

925 926 927
		config.dev = s5m8767->dev;
		config.init_data = pdata->regulators[i].initdata;
		config.driver_data = s5m8767;
928
		config.regmap = iodev->regmap_pmic;
929
		config.of_node = pdata->regulators[i].reg_node;
930

931 932
		rdev[i] = devm_regulator_register(&pdev->dev, &regulators[id],
						  &config);
933 934 935 936
		if (IS_ERR(rdev[i])) {
			ret = PTR_ERR(rdev[i]);
			dev_err(s5m8767->dev, "regulator init failed for %d\n",
					id);
937
			return ret;
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
		}
	}

	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,
	.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");