Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0+
   2//
   3// Regulator device driver for DA9061 and DA9062.
   4// Copyright (C) 2015-2017  Dialog Semiconductor
   5
 
 
 
 
 
 
 
 
 
   6#include <linux/kernel.h>
   7#include <linux/module.h>
   8#include <linux/init.h>
   9#include <linux/err.h>
  10#include <linux/slab.h>
  11#include <linux/of.h>
  12#include <linux/platform_device.h>
  13#include <linux/regmap.h>
  14#include <linux/regulator/driver.h>
  15#include <linux/regulator/machine.h>
  16#include <linux/regulator/of_regulator.h>
  17#include <linux/mfd/da9062/core.h>
  18#include <linux/mfd/da9062/registers.h>
  19#include <dt-bindings/regulator/dlg,da9063-regulator.h>
  20
  21/* Regulator IDs */
  22enum {
  23	DA9061_ID_BUCK1,
  24	DA9061_ID_BUCK2,
  25	DA9061_ID_BUCK3,
  26	DA9061_ID_LDO1,
  27	DA9061_ID_LDO2,
  28	DA9061_ID_LDO3,
  29	DA9061_ID_LDO4,
  30	DA9061_MAX_REGULATORS,
  31};
  32
  33enum {
  34	DA9062_ID_BUCK1,
  35	DA9062_ID_BUCK2,
  36	DA9062_ID_BUCK3,
  37	DA9062_ID_BUCK4,
  38	DA9062_ID_LDO1,
  39	DA9062_ID_LDO2,
  40	DA9062_ID_LDO3,
  41	DA9062_ID_LDO4,
  42	DA9062_MAX_REGULATORS,
  43};
  44
  45/* Regulator capabilities and registers description */
  46struct da9062_regulator_info {
  47	struct regulator_desc desc;
 
 
 
  48	/* Main register fields */
  49	struct reg_field mode;
  50	struct reg_field suspend;
  51	struct reg_field sleep;
  52	struct reg_field suspend_sleep;
  53	unsigned int suspend_vsel_reg;
 
  54	/* Event detection bit */
  55	struct reg_field oc_event;
  56};
  57
  58/* Single regulator settings */
  59struct da9062_regulator {
  60	struct regulator_desc			desc;
  61	struct regulator_dev			*rdev;
  62	struct da9062				*hw;
  63	const struct da9062_regulator_info	*info;
  64
  65	struct regmap_field			*mode;
  66	struct regmap_field			*suspend;
  67	struct regmap_field			*sleep;
  68	struct regmap_field			*suspend_sleep;
 
  69};
  70
  71/* Encapsulates all information for the regulators driver */
  72struct da9062_regulators {
  73	int					irq_ldo_lim;
  74	unsigned				n_regulators;
  75	/* Array size to be defined during init. Keep at end. */
  76	struct da9062_regulator			regulator[] __counted_by(n_regulators);
 
 
 
 
 
 
 
 
  77};
  78
  79/* Regulator operations */
  80
  81/* Current limits array (in uA)
  82 * - DA9061_ID_[BUCK1|BUCK3]
  83 * - DA9062_ID_[BUCK1|BUCK2|BUCK4]
  84 * Entry indexes corresponds to register values.
  85 */
  86static const unsigned int da9062_buck_a_limits[] = {
  87	 500000,  600000,  700000,  800000,  900000, 1000000, 1100000, 1200000,
  88	1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
  89};
  90
  91/* Current limits array (in uA)
  92 * - DA9061_ID_BUCK2
  93 * - DA9062_ID_BUCK3
  94 * Entry indexes corresponds to register values.
  95 */
  96static const unsigned int da9062_buck_b_limits[] = {
  97	1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
  98	2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
  99};
 100
 101static unsigned int da9062_map_buck_mode(unsigned int mode)
 
 102{
 103	switch (mode) {
 104	case DA9063_BUCK_MODE_SLEEP:
 105		return REGULATOR_MODE_STANDBY;
 106	case DA9063_BUCK_MODE_SYNC:
 107		return REGULATOR_MODE_FAST;
 108	case DA9063_BUCK_MODE_AUTO:
 109		return REGULATOR_MODE_NORMAL;
 110	default:
 111		return REGULATOR_MODE_INVALID;
 112	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 113}
 114
 115static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
 116{
 117	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 118	unsigned val;
 119
 120	switch (mode) {
 121	case REGULATOR_MODE_FAST:
 122		val = DA9063_BUCK_MODE_SYNC;
 123		break;
 124	case REGULATOR_MODE_NORMAL:
 125		val = DA9063_BUCK_MODE_AUTO;
 126		break;
 127	case REGULATOR_MODE_STANDBY:
 128		val = DA9063_BUCK_MODE_SLEEP;
 129		break;
 130	default:
 131		return -EINVAL;
 132	}
 133
 134	return regmap_field_write(regl->mode, val);
 135}
 136
 137/*
 138 * Bucks use single mode register field for normal operation
 139 * and suspend state.
 140 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
 141 */
 142
 143static unsigned da9062_buck_get_mode(struct regulator_dev *rdev)
 144{
 145	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 146	unsigned int val;
 
 147	int ret;
 148
 149	ret = regmap_field_read(regl->mode, &val);
 150	if (ret < 0)
 151		return ret;
 152
 153	switch (val) {
 154	default:
 
 
 155		/* Sleep flag bit decides the mode */
 156		break;
 157	case DA9063_BUCK_MODE_SLEEP:
 158		return REGULATOR_MODE_STANDBY;
 159	case DA9063_BUCK_MODE_SYNC:
 160		return REGULATOR_MODE_FAST;
 161	case DA9063_BUCK_MODE_AUTO:
 162		return REGULATOR_MODE_NORMAL;
 163	}
 164
 165	ret = regmap_field_read(regl->sleep, &val);
 
 166	if (ret < 0)
 167		return 0;
 168
 
 169	if (val)
 170		return REGULATOR_MODE_STANDBY;
 171	else
 172		return REGULATOR_MODE_FAST;
 
 
 
 
 
 
 
 
 
 
 
 173}
 174
 175/*
 176 * LDOs use sleep flags - one for normal and one for suspend state.
 177 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
 178 */
 179
 180static int da9062_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
 181{
 182	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 183	unsigned val;
 184
 185	switch (mode) {
 186	case REGULATOR_MODE_NORMAL:
 187		val = 0;
 188		break;
 189	case REGULATOR_MODE_STANDBY:
 190		val = 1;
 191		break;
 192	default:
 193		return -EINVAL;
 194	}
 195
 196	return regmap_field_write(regl->sleep, val);
 197}
 198
 199static unsigned da9062_ldo_get_mode(struct regulator_dev *rdev)
 200{
 201	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 
 202	int ret, val;
 203
 204	ret = regmap_field_read(regl->sleep, &val);
 
 
 
 
 
 
 
 
 
 
 
 205	if (ret < 0)
 206		return 0;
 207
 208	if (val)
 209		return REGULATOR_MODE_STANDBY;
 210	else
 211		return REGULATOR_MODE_NORMAL;
 212}
 213
 214static int da9062_buck_get_status(struct regulator_dev *rdev)
 215{
 216	int ret = regulator_is_enabled_regmap(rdev);
 217
 218	if (ret == 0) {
 219		ret = REGULATOR_STATUS_OFF;
 220	} else if (ret > 0) {
 221		ret = da9062_buck_get_mode(rdev);
 222		if (ret > 0)
 223			ret = regulator_mode_to_status(ret);
 224		else if (ret == 0)
 225			ret = -EIO;
 226	}
 227
 228	return ret;
 229}
 230
 231static int da9062_ldo_get_status(struct regulator_dev *rdev)
 232{
 233	int ret = regulator_is_enabled_regmap(rdev);
 234
 235	if (ret == 0) {
 236		ret = REGULATOR_STATUS_OFF;
 237	} else if (ret > 0) {
 238		ret = da9062_ldo_get_mode(rdev);
 239		if (ret > 0)
 240			ret = regulator_mode_to_status(ret);
 241		else if (ret == 0)
 242			ret = -EIO;
 243	}
 244
 245	return ret;
 246}
 247
 248static int da9062_set_suspend_voltage(struct regulator_dev *rdev, int uv)
 249{
 250	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 251	const struct da9062_regulator_info *rinfo = regl->info;
 252	int ret, sel;
 253
 254	sel = regulator_map_voltage_linear(rdev, uv, uv);
 255	if (sel < 0)
 256		return sel;
 257
 258	sel <<= ffs(rdev->desc->vsel_mask) - 1;
 259
 260	ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
 261				 rdev->desc->vsel_mask, sel);
 262
 263	return ret;
 264}
 265
 266static int da9062_suspend_enable(struct regulator_dev *rdev)
 267{
 268	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 269
 270	return regmap_field_write(regl->suspend, 1);
 271}
 272
 273static int da9062_suspend_disable(struct regulator_dev *rdev)
 274{
 275	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 276
 277	return regmap_field_write(regl->suspend, 0);
 278}
 279
 280static int da9062_buck_set_suspend_mode(struct regulator_dev *rdev,
 281					unsigned mode)
 282{
 283	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 284	int val;
 285
 286	switch (mode) {
 287	case REGULATOR_MODE_FAST:
 288		val = DA9063_BUCK_MODE_SYNC;
 289		break;
 290	case REGULATOR_MODE_NORMAL:
 291		val = DA9063_BUCK_MODE_AUTO;
 292		break;
 293	case REGULATOR_MODE_STANDBY:
 294		val = DA9063_BUCK_MODE_SLEEP;
 295		break;
 296	default:
 297		return -EINVAL;
 298	}
 299
 300	return regmap_field_write(regl->mode, val);
 301}
 302
 303static int da9062_ldo_set_suspend_mode(struct regulator_dev *rdev,
 304						unsigned mode)
 305{
 306	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
 307	unsigned val;
 308
 309	switch (mode) {
 310	case REGULATOR_MODE_NORMAL:
 311		val = 0;
 312		break;
 313	case REGULATOR_MODE_STANDBY:
 314		val = 1;
 315		break;
 316	default:
 317		return -EINVAL;
 318	}
 319
 320	return regmap_field_write(regl->suspend_sleep, val);
 321}
 322
 323static const struct regulator_ops da9062_buck_ops = {
 324	.enable			= regulator_enable_regmap,
 325	.disable		= regulator_disable_regmap,
 326	.is_enabled		= regulator_is_enabled_regmap,
 327	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
 328	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
 329	.list_voltage		= regulator_list_voltage_linear,
 330	.set_current_limit	= regulator_set_current_limit_regmap,
 331	.get_current_limit	= regulator_get_current_limit_regmap,
 332	.set_mode		= da9062_buck_set_mode,
 333	.get_mode		= da9062_buck_get_mode,
 334	.get_status		= da9062_buck_get_status,
 335	.set_suspend_voltage	= da9062_set_suspend_voltage,
 336	.set_suspend_enable	= da9062_suspend_enable,
 337	.set_suspend_disable	= da9062_suspend_disable,
 338	.set_suspend_mode	= da9062_buck_set_suspend_mode,
 339};
 340
 341static const struct regulator_ops da9062_ldo_ops = {
 342	.enable			= regulator_enable_regmap,
 343	.disable		= regulator_disable_regmap,
 344	.is_enabled		= regulator_is_enabled_regmap,
 345	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
 346	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
 347	.list_voltage		= regulator_list_voltage_linear,
 348	.set_mode		= da9062_ldo_set_mode,
 349	.get_mode		= da9062_ldo_get_mode,
 350	.get_status		= da9062_ldo_get_status,
 351	.set_suspend_voltage	= da9062_set_suspend_voltage,
 352	.set_suspend_enable	= da9062_suspend_enable,
 353	.set_suspend_disable	= da9062_suspend_disable,
 354	.set_suspend_mode	= da9062_ldo_set_suspend_mode,
 355};
 356
 357/* DA9061 Regulator information */
 358static const struct da9062_regulator_info local_da9061_regulator_info[] = {
 359	{
 360		.desc.id = DA9061_ID_BUCK1,
 361		.desc.name = "DA9061 BUCK1",
 362		.desc.of_match = of_match_ptr("buck1"),
 363		.desc.regulators_node = of_match_ptr("regulators"),
 364		.desc.ops = &da9062_buck_ops,
 365		.desc.min_uV = (300) * 1000,
 366		.desc.uV_step = (10) * 1000,
 367		.desc.n_voltages = ((1570) - (300))/(10) + 1,
 368		.desc.curr_table = da9062_buck_a_limits,
 369		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 370		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
 371		.desc.csel_mask = DA9062AA_BUCK1_ILIM_MASK,
 372		.desc.enable_reg = DA9062AA_BUCK1_CONT,
 373		.desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
 374		.desc.vsel_reg = DA9062AA_VBUCK1_A,
 375		.desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
 376		.desc.linear_min_sel = 0,
 377		.desc.of_map_mode = da9062_map_buck_mode,
 378		.sleep = REG_FIELD(DA9062AA_VBUCK1_A,
 379			__builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
 380			sizeof(unsigned int) * 8 -
 381			__builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
 382		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
 383			__builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
 384			sizeof(unsigned int) * 8 -
 385			__builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
 386		.suspend_vsel_reg = DA9062AA_VBUCK1_B,
 387		.mode = REG_FIELD(DA9062AA_BUCK1_CFG,
 388			__builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
 389			sizeof(unsigned int) * 8 -
 390			__builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
 391		.suspend = REG_FIELD(DA9062AA_BUCK1_CONT,
 392			__builtin_ffs((int)DA9062AA_BUCK1_CONF_MASK) - 1,
 393			sizeof(unsigned int) * 8 -
 394			__builtin_clz(DA9062AA_BUCK1_CONF_MASK) - 1),
 395	},
 396	{
 397		.desc.id = DA9061_ID_BUCK2,
 398		.desc.name = "DA9061 BUCK2",
 399		.desc.of_match = of_match_ptr("buck2"),
 400		.desc.regulators_node = of_match_ptr("regulators"),
 401		.desc.ops = &da9062_buck_ops,
 402		.desc.min_uV = (800) * 1000,
 403		.desc.uV_step = (20) * 1000,
 404		.desc.n_voltages = ((3340) - (800))/(20) + 1,
 405		.desc.curr_table = da9062_buck_b_limits,
 406		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
 407		.desc.csel_reg = DA9062AA_BUCK_ILIM_A,
 408		.desc.csel_mask = DA9062AA_BUCK3_ILIM_MASK,
 409		.desc.enable_reg = DA9062AA_BUCK3_CONT,
 410		.desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
 411		.desc.vsel_reg = DA9062AA_VBUCK3_A,
 412		.desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
 413		.desc.linear_min_sel = 0,
 414		.desc.of_map_mode = da9062_map_buck_mode,
 415		.sleep = REG_FIELD(DA9062AA_VBUCK3_A,
 416			__builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
 417			sizeof(unsigned int) * 8 -
 418			__builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
 419		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
 420			__builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
 421			sizeof(unsigned int) * 8 -
 422			__builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
 423		.suspend_vsel_reg = DA9062AA_VBUCK3_B,
 424		.mode = REG_FIELD(DA9062AA_BUCK3_CFG,
 425			__builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
 426			sizeof(unsigned int) * 8 -
 427			__builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
 428		.suspend = REG_FIELD(DA9062AA_BUCK3_CONT,
 429			__builtin_ffs((int)DA9062AA_BUCK3_CONF_MASK) - 1,
 430			sizeof(unsigned int) * 8 -
 431			__builtin_clz(DA9062AA_BUCK3_CONF_MASK) - 1),
 432	},
 433	{
 434		.desc.id = DA9061_ID_BUCK3,
 435		.desc.name = "DA9061 BUCK3",
 436		.desc.of_match = of_match_ptr("buck3"),
 437		.desc.regulators_node = of_match_ptr("regulators"),
 438		.desc.ops = &da9062_buck_ops,
 439		.desc.min_uV = (530) * 1000,
 440		.desc.uV_step = (10) * 1000,
 441		.desc.n_voltages = ((1800) - (530))/(10) + 1,
 442		.desc.curr_table = da9062_buck_a_limits,
 443		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 444		.desc.csel_reg = DA9062AA_BUCK_ILIM_B,
 445		.desc.csel_mask = DA9062AA_BUCK4_ILIM_MASK,
 446		.desc.enable_reg = DA9062AA_BUCK4_CONT,
 447		.desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
 448		.desc.vsel_reg = DA9062AA_VBUCK4_A,
 449		.desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
 450		.desc.linear_min_sel = 0,
 451		.desc.of_map_mode = da9062_map_buck_mode,
 452		.sleep = REG_FIELD(DA9062AA_VBUCK4_A,
 453			__builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
 454			sizeof(unsigned int) * 8 -
 455			__builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
 456		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
 457			__builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
 458			sizeof(unsigned int) * 8 -
 459			__builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
 460		.suspend_vsel_reg = DA9062AA_VBUCK4_B,
 461		.mode = REG_FIELD(DA9062AA_BUCK4_CFG,
 462			__builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
 463			sizeof(unsigned int) * 8 -
 464			__builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
 465		.suspend = REG_FIELD(DA9062AA_BUCK4_CONT,
 466			__builtin_ffs((int)DA9062AA_BUCK4_CONF_MASK) - 1,
 467			sizeof(unsigned int) * 8 -
 468			__builtin_clz(DA9062AA_BUCK4_CONF_MASK) - 1),
 469	},
 470	{
 471		.desc.id = DA9061_ID_LDO1,
 472		.desc.name = "DA9061 LDO1",
 473		.desc.of_match = of_match_ptr("ldo1"),
 474		.desc.regulators_node = of_match_ptr("regulators"),
 475		.desc.ops = &da9062_ldo_ops,
 476		.desc.min_uV = (900) * 1000,
 477		.desc.uV_step = (50) * 1000,
 478		.desc.n_voltages = ((3600) - (900))/(50) + 1
 479				+ DA9062AA_VLDO_A_MIN_SEL,
 480		.desc.enable_reg = DA9062AA_LDO1_CONT,
 481		.desc.enable_mask = DA9062AA_LDO1_EN_MASK,
 482		.desc.vsel_reg = DA9062AA_VLDO1_A,
 483		.desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
 484		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 485		.sleep = REG_FIELD(DA9062AA_VLDO1_A,
 486			__builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
 487			sizeof(unsigned int) * 8 -
 488			__builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
 489		.suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
 490			__builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
 491			sizeof(unsigned int) * 8 -
 492			__builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
 493		.suspend_vsel_reg = DA9062AA_VLDO1_B,
 494		.suspend = REG_FIELD(DA9062AA_LDO1_CONT,
 495			__builtin_ffs((int)DA9062AA_LDO1_CONF_MASK) - 1,
 496			sizeof(unsigned int) * 8 -
 497			__builtin_clz(DA9062AA_LDO1_CONF_MASK) - 1),
 498		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 499			__builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
 500			sizeof(unsigned int) * 8 -
 501			__builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
 502	},
 503	{
 504		.desc.id = DA9061_ID_LDO2,
 505		.desc.name = "DA9061 LDO2",
 506		.desc.of_match = of_match_ptr("ldo2"),
 507		.desc.regulators_node = of_match_ptr("regulators"),
 508		.desc.ops = &da9062_ldo_ops,
 509		.desc.min_uV = (900) * 1000,
 510		.desc.uV_step = (50) * 1000,
 511		.desc.n_voltages = ((3600) - (900))/(50) + 1
 512				+ DA9062AA_VLDO_A_MIN_SEL,
 513		.desc.enable_reg = DA9062AA_LDO2_CONT,
 514		.desc.enable_mask = DA9062AA_LDO2_EN_MASK,
 515		.desc.vsel_reg = DA9062AA_VLDO2_A,
 516		.desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
 517		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 518		.sleep = REG_FIELD(DA9062AA_VLDO2_A,
 519			__builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
 520			sizeof(unsigned int) * 8 -
 521			__builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
 522		.suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
 523			__builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
 524			sizeof(unsigned int) * 8 -
 525			__builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
 526		.suspend_vsel_reg = DA9062AA_VLDO2_B,
 527		.suspend = REG_FIELD(DA9062AA_LDO2_CONT,
 528			__builtin_ffs((int)DA9062AA_LDO2_CONF_MASK) - 1,
 529			sizeof(unsigned int) * 8 -
 530			__builtin_clz(DA9062AA_LDO2_CONF_MASK) - 1),
 531		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 532			__builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
 533			sizeof(unsigned int) * 8 -
 534			__builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
 535	},
 536	{
 537		.desc.id = DA9061_ID_LDO3,
 538		.desc.name = "DA9061 LDO3",
 539		.desc.of_match = of_match_ptr("ldo3"),
 540		.desc.regulators_node = of_match_ptr("regulators"),
 541		.desc.ops = &da9062_ldo_ops,
 542		.desc.min_uV = (900) * 1000,
 543		.desc.uV_step = (50) * 1000,
 544		.desc.n_voltages = ((3600) - (900))/(50) + 1
 545				+ DA9062AA_VLDO_A_MIN_SEL,
 546		.desc.enable_reg = DA9062AA_LDO3_CONT,
 547		.desc.enable_mask = DA9062AA_LDO3_EN_MASK,
 548		.desc.vsel_reg = DA9062AA_VLDO3_A,
 549		.desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
 550		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 551		.sleep = REG_FIELD(DA9062AA_VLDO3_A,
 552			__builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
 553			sizeof(unsigned int) * 8 -
 554			__builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
 555		.suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
 556			__builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
 557			sizeof(unsigned int) * 8 -
 558			__builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
 559		.suspend_vsel_reg = DA9062AA_VLDO3_B,
 560		.suspend = REG_FIELD(DA9062AA_LDO3_CONT,
 561			__builtin_ffs((int)DA9062AA_LDO3_CONF_MASK) - 1,
 562			sizeof(unsigned int) * 8 -
 563			__builtin_clz(DA9062AA_LDO3_CONF_MASK) - 1),
 564		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 565			__builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
 566			sizeof(unsigned int) * 8 -
 567			__builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
 568	},
 569	{
 570		.desc.id = DA9061_ID_LDO4,
 571		.desc.name = "DA9061 LDO4",
 572		.desc.of_match = of_match_ptr("ldo4"),
 573		.desc.regulators_node = of_match_ptr("regulators"),
 574		.desc.ops = &da9062_ldo_ops,
 575		.desc.min_uV = (900) * 1000,
 576		.desc.uV_step = (50) * 1000,
 577		.desc.n_voltages = ((3600) - (900))/(50) + 1
 578				+ DA9062AA_VLDO_A_MIN_SEL,
 579		.desc.enable_reg = DA9062AA_LDO4_CONT,
 580		.desc.enable_mask = DA9062AA_LDO4_EN_MASK,
 581		.desc.vsel_reg = DA9062AA_VLDO4_A,
 582		.desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
 583		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 584		.sleep = REG_FIELD(DA9062AA_VLDO4_A,
 585			__builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
 586			sizeof(unsigned int) * 8 -
 587			__builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
 588		.suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
 589			__builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
 590			sizeof(unsigned int) * 8 -
 591			__builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
 592		.suspend_vsel_reg = DA9062AA_VLDO4_B,
 593		.suspend = REG_FIELD(DA9062AA_LDO4_CONT,
 594			__builtin_ffs((int)DA9062AA_LDO4_CONF_MASK) - 1,
 595			sizeof(unsigned int) * 8 -
 596			__builtin_clz(DA9062AA_LDO4_CONF_MASK) - 1),
 597		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 598			__builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
 599			sizeof(unsigned int) * 8 -
 600			__builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
 601	},
 602};
 603
 604/* DA9062 Regulator information */
 605static const struct da9062_regulator_info local_da9062_regulator_info[] = {
 606	{
 607		.desc.id = DA9062_ID_BUCK1,
 608		.desc.name = "DA9062 BUCK1",
 609		.desc.of_match = of_match_ptr("buck1"),
 610		.desc.regulators_node = of_match_ptr("regulators"),
 611		.desc.ops = &da9062_buck_ops,
 612		.desc.min_uV = (300) * 1000,
 613		.desc.uV_step = (10) * 1000,
 614		.desc.n_voltages = ((1570) - (300))/(10) + 1,
 615		.desc.curr_table = da9062_buck_a_limits,
 616		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 617		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
 618		.desc.csel_mask = DA9062AA_BUCK1_ILIM_MASK,
 619		.desc.enable_reg = DA9062AA_BUCK1_CONT,
 620		.desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
 621		.desc.vsel_reg = DA9062AA_VBUCK1_A,
 622		.desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
 623		.desc.linear_min_sel = 0,
 624		.desc.of_map_mode = da9062_map_buck_mode,
 625		.sleep = REG_FIELD(DA9062AA_VBUCK1_A,
 626			__builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
 627			sizeof(unsigned int) * 8 -
 628			__builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
 629		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
 630			__builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
 631			sizeof(unsigned int) * 8 -
 632			__builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
 633		.suspend_vsel_reg = DA9062AA_VBUCK1_B,
 634		.mode = REG_FIELD(DA9062AA_BUCK1_CFG,
 635			__builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
 636			sizeof(unsigned int) * 8 -
 637			__builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
 638		.suspend = REG_FIELD(DA9062AA_BUCK1_CONT,
 639			__builtin_ffs((int)DA9062AA_BUCK1_CONF_MASK) - 1,
 
 
 
 
 640			sizeof(unsigned int) * 8 -
 641			__builtin_clz(DA9062AA_BUCK1_CONF_MASK) - 1),
 642	},
 643	{
 644		.desc.id = DA9062_ID_BUCK2,
 645		.desc.name = "DA9062 BUCK2",
 646		.desc.of_match = of_match_ptr("buck2"),
 647		.desc.regulators_node = of_match_ptr("regulators"),
 648		.desc.ops = &da9062_buck_ops,
 649		.desc.min_uV = (300) * 1000,
 650		.desc.uV_step = (10) * 1000,
 651		.desc.n_voltages = ((1570) - (300))/(10) + 1,
 652		.desc.curr_table = da9062_buck_a_limits,
 653		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 654		.desc.csel_reg = DA9062AA_BUCK_ILIM_C,
 655		.desc.csel_mask = DA9062AA_BUCK2_ILIM_MASK,
 656		.desc.enable_reg = DA9062AA_BUCK2_CONT,
 657		.desc.enable_mask = DA9062AA_BUCK2_EN_MASK,
 658		.desc.vsel_reg = DA9062AA_VBUCK2_A,
 659		.desc.vsel_mask = DA9062AA_VBUCK2_A_MASK,
 660		.desc.linear_min_sel = 0,
 661		.desc.of_map_mode = da9062_map_buck_mode,
 662		.sleep = REG_FIELD(DA9062AA_VBUCK2_A,
 663			__builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1,
 664			sizeof(unsigned int) * 8 -
 665			__builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1),
 666		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B,
 667			__builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1,
 668			sizeof(unsigned int) * 8 -
 669			__builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1),
 670		.suspend_vsel_reg = DA9062AA_VBUCK2_B,
 671		.mode = REG_FIELD(DA9062AA_BUCK2_CFG,
 672			__builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1,
 673			sizeof(unsigned int) * 8 -
 674			__builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1),
 675		.suspend = REG_FIELD(DA9062AA_BUCK2_CONT,
 676			__builtin_ffs((int)DA9062AA_BUCK2_CONF_MASK) - 1,
 677			sizeof(unsigned int) * 8 -
 678			__builtin_clz(DA9062AA_BUCK2_CONF_MASK) - 1),
 
 
 
 
 679	},
 680	{
 681		.desc.id = DA9062_ID_BUCK3,
 682		.desc.name = "DA9062 BUCK3",
 683		.desc.of_match = of_match_ptr("buck3"),
 684		.desc.regulators_node = of_match_ptr("regulators"),
 685		.desc.ops = &da9062_buck_ops,
 686		.desc.min_uV = (800) * 1000,
 687		.desc.uV_step = (20) * 1000,
 688		.desc.n_voltages = ((3340) - (800))/(20) + 1,
 689		.desc.curr_table = da9062_buck_b_limits,
 690		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
 691		.desc.csel_reg = DA9062AA_BUCK_ILIM_A,
 692		.desc.csel_mask = DA9062AA_BUCK3_ILIM_MASK,
 693		.desc.enable_reg = DA9062AA_BUCK3_CONT,
 694		.desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
 695		.desc.vsel_reg = DA9062AA_VBUCK3_A,
 696		.desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
 697		.desc.linear_min_sel = 0,
 698		.desc.of_map_mode = da9062_map_buck_mode,
 699		.sleep = REG_FIELD(DA9062AA_VBUCK3_A,
 700			__builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
 701			sizeof(unsigned int) * 8 -
 702			__builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
 703		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
 704			__builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
 705			sizeof(unsigned int) * 8 -
 706			__builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
 707		.suspend_vsel_reg = DA9062AA_VBUCK3_B,
 708		.mode = REG_FIELD(DA9062AA_BUCK3_CFG,
 709			__builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
 710			sizeof(unsigned int) * 8 -
 711			__builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
 712		.suspend = REG_FIELD(DA9062AA_BUCK3_CONT,
 713			__builtin_ffs((int)DA9062AA_BUCK3_CONF_MASK) - 1,
 
 
 
 
 714			sizeof(unsigned int) * 8 -
 715			__builtin_clz(DA9062AA_BUCK3_CONF_MASK) - 1),
 716	},
 717	{
 718		.desc.id = DA9062_ID_BUCK4,
 719		.desc.name = "DA9062 BUCK4",
 720		.desc.of_match = of_match_ptr("buck4"),
 721		.desc.regulators_node = of_match_ptr("regulators"),
 722		.desc.ops = &da9062_buck_ops,
 723		.desc.min_uV = (530) * 1000,
 724		.desc.uV_step = (10) * 1000,
 725		.desc.n_voltages = ((1800) - (530))/(10) + 1,
 726		.desc.curr_table = da9062_buck_a_limits,
 727		.desc.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 728		.desc.csel_reg = DA9062AA_BUCK_ILIM_B,
 729		.desc.csel_mask = DA9062AA_BUCK4_ILIM_MASK,
 730		.desc.enable_reg = DA9062AA_BUCK4_CONT,
 731		.desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
 732		.desc.vsel_reg = DA9062AA_VBUCK4_A,
 733		.desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
 734		.desc.linear_min_sel = 0,
 735		.desc.of_map_mode = da9062_map_buck_mode,
 736		.sleep = REG_FIELD(DA9062AA_VBUCK4_A,
 737			__builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
 738			sizeof(unsigned int) * 8 -
 739			__builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
 740		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
 741			__builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
 742			sizeof(unsigned int) * 8 -
 743			__builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
 744		.suspend_vsel_reg = DA9062AA_VBUCK4_B,
 745		.mode = REG_FIELD(DA9062AA_BUCK4_CFG,
 746			__builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
 747			sizeof(unsigned int) * 8 -
 748			__builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
 749		.suspend = REG_FIELD(DA9062AA_BUCK4_CONT,
 750			__builtin_ffs((int)DA9062AA_BUCK4_CONF_MASK) - 1,
 751			sizeof(unsigned int) * 8 -
 752			__builtin_clz(DA9062AA_BUCK4_CONF_MASK) - 1),
 
 
 
 
 753	},
 754	{
 755		.desc.id = DA9062_ID_LDO1,
 756		.desc.name = "DA9062 LDO1",
 757		.desc.of_match = of_match_ptr("ldo1"),
 758		.desc.regulators_node = of_match_ptr("regulators"),
 759		.desc.ops = &da9062_ldo_ops,
 760		.desc.min_uV = (900) * 1000,
 761		.desc.uV_step = (50) * 1000,
 762		.desc.n_voltages = ((3600) - (900))/(50) + 1
 763				+ DA9062AA_VLDO_A_MIN_SEL,
 764		.desc.enable_reg = DA9062AA_LDO1_CONT,
 765		.desc.enable_mask = DA9062AA_LDO1_EN_MASK,
 766		.desc.vsel_reg = DA9062AA_VLDO1_A,
 767		.desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
 768		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 769		.sleep = REG_FIELD(DA9062AA_VLDO1_A,
 770			__builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
 771			sizeof(unsigned int) * 8 -
 772			__builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
 773		.suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
 774			__builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
 775			sizeof(unsigned int) * 8 -
 776			__builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
 777		.suspend_vsel_reg = DA9062AA_VLDO1_B,
 778		.suspend = REG_FIELD(DA9062AA_LDO1_CONT,
 779			__builtin_ffs((int)DA9062AA_LDO1_CONF_MASK) - 1,
 780			sizeof(unsigned int) * 8 -
 781			__builtin_clz(DA9062AA_LDO1_CONF_MASK) - 1),
 782		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 783			__builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
 784			sizeof(unsigned int) * 8 -
 785			__builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
 786	},
 787	{
 788		.desc.id = DA9062_ID_LDO2,
 789		.desc.name = "DA9062 LDO2",
 790		.desc.of_match = of_match_ptr("ldo2"),
 791		.desc.regulators_node = of_match_ptr("regulators"),
 792		.desc.ops = &da9062_ldo_ops,
 793		.desc.min_uV = (900) * 1000,
 794		.desc.uV_step = (50) * 1000,
 795		.desc.n_voltages = ((3600) - (900))/(50) + 1
 796				+ DA9062AA_VLDO_A_MIN_SEL,
 797		.desc.enable_reg = DA9062AA_LDO2_CONT,
 798		.desc.enable_mask = DA9062AA_LDO2_EN_MASK,
 799		.desc.vsel_reg = DA9062AA_VLDO2_A,
 800		.desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
 801		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 802		.sleep = REG_FIELD(DA9062AA_VLDO2_A,
 803			__builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
 804			sizeof(unsigned int) * 8 -
 805			__builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
 806		.suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
 807			__builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
 808			sizeof(unsigned int) * 8 -
 809			__builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
 810		.suspend_vsel_reg = DA9062AA_VLDO2_B,
 811		.suspend = REG_FIELD(DA9062AA_LDO2_CONT,
 812			__builtin_ffs((int)DA9062AA_LDO2_CONF_MASK) - 1,
 813			sizeof(unsigned int) * 8 -
 814			__builtin_clz(DA9062AA_LDO2_CONF_MASK) - 1),
 815		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 816			__builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
 817			sizeof(unsigned int) * 8 -
 818			__builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
 819	},
 820	{
 821		.desc.id = DA9062_ID_LDO3,
 822		.desc.name = "DA9062 LDO3",
 823		.desc.of_match = of_match_ptr("ldo3"),
 824		.desc.regulators_node = of_match_ptr("regulators"),
 825		.desc.ops = &da9062_ldo_ops,
 826		.desc.min_uV = (900) * 1000,
 827		.desc.uV_step = (50) * 1000,
 828		.desc.n_voltages = ((3600) - (900))/(50) + 1
 829				+ DA9062AA_VLDO_A_MIN_SEL,
 830		.desc.enable_reg = DA9062AA_LDO3_CONT,
 831		.desc.enable_mask = DA9062AA_LDO3_EN_MASK,
 832		.desc.vsel_reg = DA9062AA_VLDO3_A,
 833		.desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
 834		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 835		.sleep = REG_FIELD(DA9062AA_VLDO3_A,
 836			__builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
 837			sizeof(unsigned int) * 8 -
 838			__builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
 839		.suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
 840			__builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
 841			sizeof(unsigned int) * 8 -
 842			__builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
 843		.suspend_vsel_reg = DA9062AA_VLDO3_B,
 844		.suspend = REG_FIELD(DA9062AA_LDO3_CONT,
 845			__builtin_ffs((int)DA9062AA_LDO3_CONF_MASK) - 1,
 846			sizeof(unsigned int) * 8 -
 847			__builtin_clz(DA9062AA_LDO3_CONF_MASK) - 1),
 848		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 849			__builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
 850			sizeof(unsigned int) * 8 -
 851			__builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
 852	},
 853	{
 854		.desc.id = DA9062_ID_LDO4,
 855		.desc.name = "DA9062 LDO4",
 856		.desc.of_match = of_match_ptr("ldo4"),
 857		.desc.regulators_node = of_match_ptr("regulators"),
 858		.desc.ops = &da9062_ldo_ops,
 859		.desc.min_uV = (900) * 1000,
 860		.desc.uV_step = (50) * 1000,
 861		.desc.n_voltages = ((3600) - (900))/(50) + 1
 862				+ DA9062AA_VLDO_A_MIN_SEL,
 863		.desc.enable_reg = DA9062AA_LDO4_CONT,
 864		.desc.enable_mask = DA9062AA_LDO4_EN_MASK,
 865		.desc.vsel_reg = DA9062AA_VLDO4_A,
 866		.desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
 867		.desc.linear_min_sel = DA9062AA_VLDO_A_MIN_SEL,
 868		.sleep = REG_FIELD(DA9062AA_VLDO4_A,
 869			__builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
 870			sizeof(unsigned int) * 8 -
 871			__builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
 872		.suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
 873			__builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
 874			sizeof(unsigned int) * 8 -
 875			__builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
 876		.suspend_vsel_reg = DA9062AA_VLDO4_B,
 877		.suspend = REG_FIELD(DA9062AA_LDO4_CONT,
 878			__builtin_ffs((int)DA9062AA_LDO4_CONF_MASK) - 1,
 879			sizeof(unsigned int) * 8 -
 880			__builtin_clz(DA9062AA_LDO4_CONF_MASK) - 1),
 881		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
 882			__builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
 883			sizeof(unsigned int) * 8 -
 884			__builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
 885	},
 886};
 887
 888/* Regulator interrupt handlers */
 889static irqreturn_t da9062_ldo_lim_event(int irq, void *data)
 890{
 891	struct da9062_regulators *regulators = data;
 892	struct da9062 *hw = regulators->regulator[0].hw;
 893	struct da9062_regulator *regl;
 894	int handled = IRQ_NONE;
 895	int bits, i, ret;
 896
 897	ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits);
 898	if (ret < 0) {
 899		dev_err(hw->dev,
 900			"Failed to read LDO overcurrent indicator\n");
 901		goto ldo_lim_error;
 902	}
 903
 904	for (i = regulators->n_regulators - 1; i >= 0; i--) {
 905		regl = &regulators->regulator[i];
 906		if (regl->info->oc_event.reg != DA9062AA_STATUS_D)
 907			continue;
 908
 909		if (BIT(regl->info->oc_event.lsb) & bits) {
 910			regulator_notifier_call_chain(regl->rdev,
 911					REGULATOR_EVENT_OVER_CURRENT, NULL);
 912			handled = IRQ_HANDLED;
 913		}
 914	}
 915
 916ldo_lim_error:
 917	return handled;
 918}
 919
 920static int da9062_regulator_probe(struct platform_device *pdev)
 921{
 922	struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
 923	struct da9062_regulators *regulators;
 924	struct da9062_regulator *regl;
 925	struct regulator_config config = { };
 926	const struct da9062_regulator_info *rinfo;
 927	int n, ret;
 928	int max_regulators;
 929
 930	switch (chip->chip_type) {
 931	case COMPAT_TYPE_DA9061:
 932		max_regulators = DA9061_MAX_REGULATORS;
 933		rinfo = local_da9061_regulator_info;
 934		break;
 935	case COMPAT_TYPE_DA9062:
 936		max_regulators = DA9062_MAX_REGULATORS;
 937		rinfo = local_da9062_regulator_info;
 938		break;
 939	default:
 940		dev_err(chip->dev, "Unrecognised chip type\n");
 941		return -ENODEV;
 942	}
 943
 944	/* Allocate memory required by usable regulators */
 945	regulators = devm_kzalloc(&pdev->dev, struct_size(regulators, regulator,
 946				  max_regulators), GFP_KERNEL);
 
 947	if (!regulators)
 948		return -ENOMEM;
 949
 950	regulators->n_regulators = max_regulators;
 951	platform_set_drvdata(pdev, regulators);
 952
 953	for (n = 0; n < regulators->n_regulators; n++) {
 
 954		/* Initialise regulator structure */
 955		regl = &regulators->regulator[n];
 956		regl->hw = chip;
 957		regl->info = &rinfo[n];
 958		regl->desc = regl->info->desc;
 959		regl->desc.type = REGULATOR_VOLTAGE;
 960		regl->desc.owner = THIS_MODULE;
 961
 962		if (regl->info->mode.reg) {
 963			regl->mode = devm_regmap_field_alloc(
 964					&pdev->dev,
 965					chip->regmap,
 966					regl->info->mode);
 967			if (IS_ERR(regl->mode))
 968				return PTR_ERR(regl->mode);
 969		}
 970
 971		if (regl->info->suspend.reg) {
 972			regl->suspend = devm_regmap_field_alloc(
 973					&pdev->dev,
 974					chip->regmap,
 975					regl->info->suspend);
 976			if (IS_ERR(regl->suspend))
 977				return PTR_ERR(regl->suspend);
 978		}
 979
 980		if (regl->info->sleep.reg) {
 981			regl->sleep = devm_regmap_field_alloc(
 982					&pdev->dev,
 983					chip->regmap,
 984					regl->info->sleep);
 985			if (IS_ERR(regl->sleep))
 986				return PTR_ERR(regl->sleep);
 987		}
 988
 989		if (regl->info->suspend_sleep.reg) {
 990			regl->suspend_sleep = devm_regmap_field_alloc(
 991					&pdev->dev,
 992					chip->regmap,
 993					regl->info->suspend_sleep);
 994			if (IS_ERR(regl->suspend_sleep))
 995				return PTR_ERR(regl->suspend_sleep);
 996		}
 
 
 997
 998		/* Register regulator */
 999		memset(&config, 0, sizeof(config));
1000		config.dev = chip->dev;
1001		config.driver_data = regl;
1002		config.regmap = chip->regmap;
1003
1004		regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
1005						     &config);
1006		if (IS_ERR(regl->rdev)) {
1007			dev_err(&pdev->dev,
1008				"Failed to register %s regulator\n",
1009				regl->desc.name);
1010			return PTR_ERR(regl->rdev);
1011		}
 
 
1012	}
1013
1014	/* LDOs overcurrent event support */
1015	regulators->irq_ldo_lim = platform_get_irq_byname_optional(pdev, "LDO_LIM");
1016	if (regulators->irq_ldo_lim < 0)
1017		return 0;
 
 
 
1018
1019	ret = devm_request_threaded_irq(&pdev->dev, regulators->irq_ldo_lim,
1020					NULL, da9062_ldo_lim_event,
1021					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1022					"LDO_LIM", regulators);
1023	if (ret) {
1024		dev_warn(&pdev->dev,
1025			 "Failed to request LDO_LIM IRQ.\n");
1026		regulators->irq_ldo_lim = -ENXIO;
1027	}
1028
1029	return 0;
1030}
1031
1032static struct platform_driver da9062_regulator_driver = {
1033	.driver = {
1034		.name = "da9062-regulators",
1035		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1036	},
1037	.probe = da9062_regulator_probe,
1038};
1039
1040static int __init da9062_regulator_init(void)
1041{
1042	return platform_driver_register(&da9062_regulator_driver);
1043}
1044subsys_initcall(da9062_regulator_init);
1045
1046static void __exit da9062_regulator_cleanup(void)
1047{
1048	platform_driver_unregister(&da9062_regulator_driver);
1049}
1050module_exit(da9062_regulator_cleanup);
1051
1052/* Module information */
1053MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
1054MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062 and DA9061");
1055MODULE_LICENSE("GPL");
1056MODULE_ALIAS("platform:da9062-regulators");
v4.6
  1/*
  2 * da9062-regulator.c - REGULATOR device driver for DA9062
  3 * Copyright (C) 2015  Dialog Semiconductor Ltd.
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or (at your option) any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 */
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/init.h>
 18#include <linux/err.h>
 19#include <linux/slab.h>
 20#include <linux/of.h>
 21#include <linux/platform_device.h>
 22#include <linux/regmap.h>
 23#include <linux/regulator/driver.h>
 24#include <linux/regulator/machine.h>
 25#include <linux/regulator/of_regulator.h>
 26#include <linux/mfd/da9062/core.h>
 27#include <linux/mfd/da9062/registers.h>
 
 28
 29/* Regulator IDs */
 30enum {
 
 
 
 
 
 
 
 
 
 
 
 31	DA9062_ID_BUCK1,
 32	DA9062_ID_BUCK2,
 33	DA9062_ID_BUCK3,
 34	DA9062_ID_BUCK4,
 35	DA9062_ID_LDO1,
 36	DA9062_ID_LDO2,
 37	DA9062_ID_LDO3,
 38	DA9062_ID_LDO4,
 39	DA9062_MAX_REGULATORS,
 40};
 41
 42/* Regulator capabilities and registers description */
 43struct da9062_regulator_info {
 44	struct regulator_desc desc;
 45	/* Current limiting */
 46	unsigned int n_current_limits;
 47	const int *current_limits;
 48	/* Main register fields */
 49	struct reg_field mode;
 50	struct reg_field suspend;
 51	struct reg_field sleep;
 52	struct reg_field suspend_sleep;
 53	unsigned int suspend_vsel_reg;
 54	struct reg_field ilimit;
 55	/* Event detection bit */
 56	struct reg_field oc_event;
 57};
 58
 59/* Single regulator settings */
 60struct da9062_regulator {
 61	struct regulator_desc			desc;
 62	struct regulator_dev			*rdev;
 63	struct da9062				*hw;
 64	const struct da9062_regulator_info	*info;
 65
 66	struct regmap_field			*mode;
 67	struct regmap_field			*suspend;
 68	struct regmap_field			*sleep;
 69	struct regmap_field			*suspend_sleep;
 70	struct regmap_field			*ilimit;
 71};
 72
 73/* Encapsulates all information for the regulators driver */
 74struct da9062_regulators {
 75	int					irq_ldo_lim;
 76	unsigned				n_regulators;
 77	/* Array size to be defined during init. Keep at end. */
 78	struct da9062_regulator			regulator[0];
 79};
 80
 81/* BUCK modes */
 82enum {
 83	BUCK_MODE_MANUAL,	/* 0 */
 84	BUCK_MODE_SLEEP,	/* 1 */
 85	BUCK_MODE_SYNC,		/* 2 */
 86	BUCK_MODE_AUTO		/* 3 */
 87};
 88
 89/* Regulator operations */
 90
 91/* Current limits array (in uA) BUCK1 and BUCK3.
 92   Entry indexes corresponds to register values. */
 93static const int da9062_buck_a_limits[] = {
 
 
 
 94	 500000,  600000,  700000,  800000,  900000, 1000000, 1100000, 1200000,
 95	1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
 96};
 97
 98/* Current limits array (in uA) for BUCK2.
 99   Entry indexes corresponds to register values. */
100static const int da9062_buck_b_limits[] = {
 
 
 
101	1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
102	2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
103};
104
105static int da9062_set_current_limit(struct regulator_dev *rdev,
106				    int min_ua, int max_ua)
107{
108	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
109	const struct da9062_regulator_info *rinfo = regl->info;
110	int n, tval;
111
112	for (n = 0; n < rinfo->n_current_limits; n++) {
113		tval = rinfo->current_limits[n];
114		if (tval >= min_ua && tval <= max_ua)
115			return regmap_field_write(regl->ilimit, n);
 
116	}
117
118	return -EINVAL;
119}
120
121static int da9062_get_current_limit(struct regulator_dev *rdev)
122{
123	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
124	const struct da9062_regulator_info *rinfo = regl->info;
125	unsigned int sel;
126	int ret;
127
128	ret = regmap_field_read(regl->ilimit, &sel);
129	if (ret < 0)
130		return ret;
131
132	if (sel >= rinfo->n_current_limits)
133		sel = rinfo->n_current_limits - 1;
134
135	return rinfo->current_limits[sel];
136}
137
138static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
139{
140	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
141	unsigned val;
142
143	switch (mode) {
144	case REGULATOR_MODE_FAST:
145		val = BUCK_MODE_SYNC;
146		break;
147	case REGULATOR_MODE_NORMAL:
148		val = BUCK_MODE_AUTO;
149		break;
150	case REGULATOR_MODE_STANDBY:
151		val = BUCK_MODE_SLEEP;
152		break;
153	default:
154		return -EINVAL;
155	}
156
157	return regmap_field_write(regl->mode, val);
158}
159
160/*
161 * Bucks use single mode register field for normal operation
162 * and suspend state.
163 * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
164 */
165
166static unsigned da9062_buck_get_mode(struct regulator_dev *rdev)
167{
168	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
169	struct regmap_field *field;
170	unsigned int val, mode = 0;
171	int ret;
172
173	ret = regmap_field_read(regl->mode, &val);
174	if (ret < 0)
175		return ret;
176
177	switch (val) {
178	default:
179	case BUCK_MODE_MANUAL:
180		mode = REGULATOR_MODE_FAST | REGULATOR_MODE_STANDBY;
181		/* Sleep flag bit decides the mode */
182		break;
183	case BUCK_MODE_SLEEP:
184		return REGULATOR_MODE_STANDBY;
185	case BUCK_MODE_SYNC:
186		return REGULATOR_MODE_FAST;
187	case BUCK_MODE_AUTO:
188		return REGULATOR_MODE_NORMAL;
189	}
190
191	/* Detect current regulator state */
192	ret = regmap_field_read(regl->suspend, &val);
193	if (ret < 0)
194		return 0;
195
196	/* Read regulator mode from proper register, depending on state */
197	if (val)
198		field = regl->suspend_sleep;
199	else
200		field = regl->sleep;
201
202	ret = regmap_field_read(field, &val);
203	if (ret < 0)
204		return 0;
205
206	if (val)
207		mode &= REGULATOR_MODE_STANDBY;
208	else
209		mode &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_FAST;
210
211	return mode;
212}
213
214/*
215 * LDOs use sleep flags - one for normal and one for suspend state.
216 * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
217 */
218
219static int da9062_ldo_set_mode(struct regulator_dev *rdev, unsigned mode)
220{
221	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
222	unsigned val;
223
224	switch (mode) {
225	case REGULATOR_MODE_NORMAL:
226		val = 0;
227		break;
228	case REGULATOR_MODE_STANDBY:
229		val = 1;
230		break;
231	default:
232		return -EINVAL;
233	}
234
235	return regmap_field_write(regl->sleep, val);
236}
237
238static unsigned da9062_ldo_get_mode(struct regulator_dev *rdev)
239{
240	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
241	struct regmap_field *field;
242	int ret, val;
243
244	/* Detect current regulator state */
245	ret = regmap_field_read(regl->suspend, &val);
246	if (ret < 0)
247		return 0;
248
249	/* Read regulator mode from proper register, depending on state */
250	if (val)
251		field = regl->suspend_sleep;
252	else
253		field = regl->sleep;
254
255	ret = regmap_field_read(field, &val);
256	if (ret < 0)
257		return 0;
258
259	if (val)
260		return REGULATOR_MODE_STANDBY;
261	else
262		return REGULATOR_MODE_NORMAL;
263}
264
265static int da9062_buck_get_status(struct regulator_dev *rdev)
266{
267	int ret = regulator_is_enabled_regmap(rdev);
268
269	if (ret == 0) {
270		ret = REGULATOR_STATUS_OFF;
271	} else if (ret > 0) {
272		ret = da9062_buck_get_mode(rdev);
273		if (ret > 0)
274			ret = regulator_mode_to_status(ret);
275		else if (ret == 0)
276			ret = -EIO;
277	}
278
279	return ret;
280}
281
282static int da9062_ldo_get_status(struct regulator_dev *rdev)
283{
284	int ret = regulator_is_enabled_regmap(rdev);
285
286	if (ret == 0) {
287		ret = REGULATOR_STATUS_OFF;
288	} else if (ret > 0) {
289		ret = da9062_ldo_get_mode(rdev);
290		if (ret > 0)
291			ret = regulator_mode_to_status(ret);
292		else if (ret == 0)
293			ret = -EIO;
294	}
295
296	return ret;
297}
298
299static int da9062_set_suspend_voltage(struct regulator_dev *rdev, int uv)
300{
301	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
302	const struct da9062_regulator_info *rinfo = regl->info;
303	int ret, sel;
304
305	sel = regulator_map_voltage_linear(rdev, uv, uv);
306	if (sel < 0)
307		return sel;
308
309	sel <<= ffs(rdev->desc->vsel_mask) - 1;
310
311	ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
312				 rdev->desc->vsel_mask, sel);
313
314	return ret;
315}
316
317static int da9062_suspend_enable(struct regulator_dev *rdev)
318{
319	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
320
321	return regmap_field_write(regl->suspend, 1);
322}
323
324static int da9062_suspend_disable(struct regulator_dev *rdev)
325{
326	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
327
328	return regmap_field_write(regl->suspend, 0);
329}
330
331static int da9062_buck_set_suspend_mode(struct regulator_dev *rdev,
332					unsigned mode)
333{
334	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
335	int val;
336
337	switch (mode) {
338	case REGULATOR_MODE_FAST:
339		val = BUCK_MODE_SYNC;
340		break;
341	case REGULATOR_MODE_NORMAL:
342		val = BUCK_MODE_AUTO;
343		break;
344	case REGULATOR_MODE_STANDBY:
345		val = BUCK_MODE_SLEEP;
346		break;
347	default:
348		return -EINVAL;
349	}
350
351	return regmap_field_write(regl->mode, val);
352}
353
354static int da9062_ldo_set_suspend_mode(struct regulator_dev *rdev,
355						unsigned mode)
356{
357	struct da9062_regulator *regl = rdev_get_drvdata(rdev);
358	unsigned val;
359
360	switch (mode) {
361	case REGULATOR_MODE_NORMAL:
362		val = 0;
363		break;
364	case REGULATOR_MODE_STANDBY:
365		val = 1;
366		break;
367	default:
368		return -EINVAL;
369	}
370
371	return regmap_field_write(regl->suspend_sleep, val);
372}
373
374static const struct regulator_ops da9062_buck_ops = {
375	.enable			= regulator_enable_regmap,
376	.disable		= regulator_disable_regmap,
377	.is_enabled		= regulator_is_enabled_regmap,
378	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
379	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
380	.list_voltage		= regulator_list_voltage_linear,
381	.set_current_limit	= da9062_set_current_limit,
382	.get_current_limit	= da9062_get_current_limit,
383	.set_mode		= da9062_buck_set_mode,
384	.get_mode		= da9062_buck_get_mode,
385	.get_status		= da9062_buck_get_status,
386	.set_suspend_voltage	= da9062_set_suspend_voltage,
387	.set_suspend_enable	= da9062_suspend_enable,
388	.set_suspend_disable	= da9062_suspend_disable,
389	.set_suspend_mode	= da9062_buck_set_suspend_mode,
390};
391
392static const struct regulator_ops da9062_ldo_ops = {
393	.enable			= regulator_enable_regmap,
394	.disable		= regulator_disable_regmap,
395	.is_enabled		= regulator_is_enabled_regmap,
396	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
397	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
398	.list_voltage		= regulator_list_voltage_linear,
399	.set_mode		= da9062_ldo_set_mode,
400	.get_mode		= da9062_ldo_get_mode,
401	.get_status		= da9062_ldo_get_status,
402	.set_suspend_voltage	= da9062_set_suspend_voltage,
403	.set_suspend_enable	= da9062_suspend_enable,
404	.set_suspend_disable	= da9062_suspend_disable,
405	.set_suspend_mode	= da9062_ldo_set_suspend_mode,
406};
407
408/* Regulator information */
409static const struct da9062_regulator_info local_regulator_info[] = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410	{
411		.desc.id = DA9062_ID_BUCK1,
412		.desc.name = "DA9062 BUCK1",
413		.desc.of_match = of_match_ptr("buck1"),
414		.desc.regulators_node = of_match_ptr("regulators"),
415		.desc.ops = &da9062_buck_ops,
416		.desc.min_uV = (300) * 1000,
417		.desc.uV_step = (10) * 1000,
418		.desc.n_voltages = ((1570) - (300))/(10) + 1,
419		.current_limits = da9062_buck_a_limits,
420		.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 
 
421		.desc.enable_reg = DA9062AA_BUCK1_CONT,
422		.desc.enable_mask = DA9062AA_BUCK1_EN_MASK,
423		.desc.vsel_reg = DA9062AA_VBUCK1_A,
424		.desc.vsel_mask = DA9062AA_VBUCK1_A_MASK,
425		.desc.linear_min_sel = 0,
 
426		.sleep = REG_FIELD(DA9062AA_VBUCK1_A,
427			__builtin_ffs((int)DA9062AA_BUCK1_SL_A_MASK) - 1,
428			sizeof(unsigned int) * 8 -
429			__builtin_clz((DA9062AA_BUCK1_SL_A_MASK)) - 1),
430		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK1_B,
431			__builtin_ffs((int)DA9062AA_BUCK1_SL_B_MASK) - 1,
432			sizeof(unsigned int) * 8 -
433			__builtin_clz((DA9062AA_BUCK1_SL_B_MASK)) - 1),
434		.suspend_vsel_reg = DA9062AA_VBUCK1_B,
435		.mode = REG_FIELD(DA9062AA_BUCK1_CFG,
436			__builtin_ffs((int)DA9062AA_BUCK1_MODE_MASK) - 1,
437			sizeof(unsigned int) * 8 -
438			__builtin_clz((DA9062AA_BUCK1_MODE_MASK)) - 1),
439		.suspend = REG_FIELD(DA9062AA_DVC_1,
440			__builtin_ffs((int)DA9062AA_VBUCK1_SEL_MASK) - 1,
441			sizeof(unsigned int) * 8 -
442			__builtin_clz((DA9062AA_VBUCK1_SEL_MASK)) - 1),
443		.ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
444			__builtin_ffs((int)DA9062AA_BUCK1_ILIM_MASK) - 1,
445			sizeof(unsigned int) * 8 -
446			__builtin_clz((DA9062AA_BUCK1_ILIM_MASK)) - 1),
447	},
448	{
449		.desc.id = DA9062_ID_BUCK2,
450		.desc.name = "DA9062 BUCK2",
451		.desc.of_match = of_match_ptr("buck2"),
452		.desc.regulators_node = of_match_ptr("regulators"),
453		.desc.ops = &da9062_buck_ops,
454		.desc.min_uV = (300) * 1000,
455		.desc.uV_step = (10) * 1000,
456		.desc.n_voltages = ((1570) - (300))/(10) + 1,
457		.current_limits = da9062_buck_a_limits,
458		.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 
 
459		.desc.enable_reg = DA9062AA_BUCK2_CONT,
460		.desc.enable_mask = DA9062AA_BUCK2_EN_MASK,
461		.desc.vsel_reg = DA9062AA_VBUCK2_A,
462		.desc.vsel_mask = DA9062AA_VBUCK2_A_MASK,
463		.desc.linear_min_sel = 0,
 
464		.sleep = REG_FIELD(DA9062AA_VBUCK2_A,
465			__builtin_ffs((int)DA9062AA_BUCK2_SL_A_MASK) - 1,
466			sizeof(unsigned int) * 8 -
467			__builtin_clz((DA9062AA_BUCK2_SL_A_MASK)) - 1),
468		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK2_B,
469			__builtin_ffs((int)DA9062AA_BUCK2_SL_B_MASK) - 1,
470			sizeof(unsigned int) * 8 -
471			__builtin_clz((DA9062AA_BUCK2_SL_B_MASK)) - 1),
472		.suspend_vsel_reg = DA9062AA_VBUCK2_B,
473		.mode = REG_FIELD(DA9062AA_BUCK2_CFG,
474			__builtin_ffs((int)DA9062AA_BUCK2_MODE_MASK) - 1,
475			sizeof(unsigned int) * 8 -
476			__builtin_clz((DA9062AA_BUCK2_MODE_MASK)) - 1),
477		.suspend = REG_FIELD(DA9062AA_DVC_1,
478			__builtin_ffs((int)DA9062AA_VBUCK2_SEL_MASK) - 1,
479			sizeof(unsigned int) * 8 -
480			__builtin_clz((DA9062AA_VBUCK2_SEL_MASK)) - 1),
481		.ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_C,
482			__builtin_ffs((int)DA9062AA_BUCK2_ILIM_MASK) - 1,
483			sizeof(unsigned int) * 8 -
484			__builtin_clz((DA9062AA_BUCK2_ILIM_MASK)) - 1),
485	},
486	{
487		.desc.id = DA9062_ID_BUCK3,
488		.desc.name = "DA9062 BUCK3",
489		.desc.of_match = of_match_ptr("buck3"),
490		.desc.regulators_node = of_match_ptr("regulators"),
491		.desc.ops = &da9062_buck_ops,
492		.desc.min_uV = (800) * 1000,
493		.desc.uV_step = (20) * 1000,
494		.desc.n_voltages = ((3340) - (800))/(20) + 1,
495		.current_limits = da9062_buck_b_limits,
496		.n_current_limits = ARRAY_SIZE(da9062_buck_b_limits),
 
 
497		.desc.enable_reg = DA9062AA_BUCK3_CONT,
498		.desc.enable_mask = DA9062AA_BUCK3_EN_MASK,
499		.desc.vsel_reg = DA9062AA_VBUCK3_A,
500		.desc.vsel_mask = DA9062AA_VBUCK3_A_MASK,
501		.desc.linear_min_sel = 0,
 
502		.sleep = REG_FIELD(DA9062AA_VBUCK3_A,
503			__builtin_ffs((int)DA9062AA_BUCK3_SL_A_MASK) - 1,
504			sizeof(unsigned int) * 8 -
505			__builtin_clz((DA9062AA_BUCK3_SL_A_MASK)) - 1),
506		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK3_B,
507			__builtin_ffs((int)DA9062AA_BUCK3_SL_B_MASK) - 1,
508			sizeof(unsigned int) * 8 -
509			__builtin_clz((DA9062AA_BUCK3_SL_B_MASK)) - 1),
510		.suspend_vsel_reg = DA9062AA_VBUCK3_B,
511		.mode = REG_FIELD(DA9062AA_BUCK3_CFG,
512			__builtin_ffs((int)DA9062AA_BUCK3_MODE_MASK) - 1,
513			sizeof(unsigned int) * 8 -
514			__builtin_clz((DA9062AA_BUCK3_MODE_MASK)) - 1),
515		.suspend = REG_FIELD(DA9062AA_DVC_1,
516			__builtin_ffs((int)DA9062AA_VBUCK3_SEL_MASK) - 1,
517			sizeof(unsigned int) * 8 -
518			__builtin_clz((DA9062AA_VBUCK3_SEL_MASK)) - 1),
519		.ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_A,
520			__builtin_ffs((int)DA9062AA_BUCK3_ILIM_MASK) - 1,
521			sizeof(unsigned int) * 8 -
522			__builtin_clz((DA9062AA_BUCK3_ILIM_MASK)) - 1),
523	},
524	{
525		.desc.id = DA9062_ID_BUCK4,
526		.desc.name = "DA9062 BUCK4",
527		.desc.of_match = of_match_ptr("buck4"),
528		.desc.regulators_node = of_match_ptr("regulators"),
529		.desc.ops = &da9062_buck_ops,
530		.desc.min_uV = (530) * 1000,
531		.desc.uV_step = (10) * 1000,
532		.desc.n_voltages = ((1800) - (530))/(10) + 1,
533		.current_limits = da9062_buck_a_limits,
534		.n_current_limits = ARRAY_SIZE(da9062_buck_a_limits),
 
 
535		.desc.enable_reg = DA9062AA_BUCK4_CONT,
536		.desc.enable_mask = DA9062AA_BUCK4_EN_MASK,
537		.desc.vsel_reg = DA9062AA_VBUCK4_A,
538		.desc.vsel_mask = DA9062AA_VBUCK4_A_MASK,
539		.desc.linear_min_sel = 0,
 
540		.sleep = REG_FIELD(DA9062AA_VBUCK4_A,
541			__builtin_ffs((int)DA9062AA_BUCK4_SL_A_MASK) - 1,
542			sizeof(unsigned int) * 8 -
543			__builtin_clz((DA9062AA_BUCK4_SL_A_MASK)) - 1),
544		.suspend_sleep = REG_FIELD(DA9062AA_VBUCK4_B,
545			__builtin_ffs((int)DA9062AA_BUCK4_SL_B_MASK) - 1,
546			sizeof(unsigned int) * 8 -
547			__builtin_clz((DA9062AA_BUCK4_SL_B_MASK)) - 1),
548		.suspend_vsel_reg = DA9062AA_VBUCK4_B,
549		.mode = REG_FIELD(DA9062AA_BUCK4_CFG,
550			__builtin_ffs((int)DA9062AA_BUCK4_MODE_MASK) - 1,
551			sizeof(unsigned int) * 8 -
552			__builtin_clz((DA9062AA_BUCK4_MODE_MASK)) - 1),
553		.suspend = REG_FIELD(DA9062AA_DVC_1,
554			__builtin_ffs((int)DA9062AA_VBUCK4_SEL_MASK) - 1,
555			sizeof(unsigned int) * 8 -
556			__builtin_clz((DA9062AA_VBUCK4_SEL_MASK)) - 1),
557		.ilimit = REG_FIELD(DA9062AA_BUCK_ILIM_B,
558			__builtin_ffs((int)DA9062AA_BUCK4_ILIM_MASK) - 1,
559			sizeof(unsigned int) * 8 -
560			__builtin_clz((DA9062AA_BUCK4_ILIM_MASK)) - 1),
561	},
562	{
563		.desc.id = DA9062_ID_LDO1,
564		.desc.name = "DA9062 LDO1",
565		.desc.of_match = of_match_ptr("ldo1"),
566		.desc.regulators_node = of_match_ptr("regulators"),
567		.desc.ops = &da9062_ldo_ops,
568		.desc.min_uV = (900) * 1000,
569		.desc.uV_step = (50) * 1000,
570		.desc.n_voltages = ((3600) - (900))/(50) + 1,
 
571		.desc.enable_reg = DA9062AA_LDO1_CONT,
572		.desc.enable_mask = DA9062AA_LDO1_EN_MASK,
573		.desc.vsel_reg = DA9062AA_VLDO1_A,
574		.desc.vsel_mask = DA9062AA_VLDO1_A_MASK,
575		.desc.linear_min_sel = 0,
576		.sleep = REG_FIELD(DA9062AA_VLDO1_A,
577			__builtin_ffs((int)DA9062AA_LDO1_SL_A_MASK) - 1,
578			sizeof(unsigned int) * 8 -
579			__builtin_clz((DA9062AA_LDO1_SL_A_MASK)) - 1),
580		.suspend_sleep = REG_FIELD(DA9062AA_VLDO1_B,
581			__builtin_ffs((int)DA9062AA_LDO1_SL_B_MASK) - 1,
582			sizeof(unsigned int) * 8 -
583			__builtin_clz((DA9062AA_LDO1_SL_B_MASK)) - 1),
584		.suspend_vsel_reg = DA9062AA_VLDO1_B,
585		.suspend = REG_FIELD(DA9062AA_DVC_1,
586			__builtin_ffs((int)DA9062AA_VLDO1_SEL_MASK) - 1,
587			sizeof(unsigned int) * 8 -
588			__builtin_clz((DA9062AA_VLDO1_SEL_MASK)) - 1),
589		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
590			__builtin_ffs((int)DA9062AA_LDO1_ILIM_MASK) - 1,
591			sizeof(unsigned int) * 8 -
592			__builtin_clz((DA9062AA_LDO1_ILIM_MASK)) - 1),
593	},
594	{
595		.desc.id = DA9062_ID_LDO2,
596		.desc.name = "DA9062 LDO2",
597		.desc.of_match = of_match_ptr("ldo2"),
598		.desc.regulators_node = of_match_ptr("regulators"),
599		.desc.ops = &da9062_ldo_ops,
600		.desc.min_uV = (900) * 1000,
601		.desc.uV_step = (50) * 1000,
602		.desc.n_voltages = ((3600) - (600))/(50) + 1,
 
603		.desc.enable_reg = DA9062AA_LDO2_CONT,
604		.desc.enable_mask = DA9062AA_LDO2_EN_MASK,
605		.desc.vsel_reg = DA9062AA_VLDO2_A,
606		.desc.vsel_mask = DA9062AA_VLDO2_A_MASK,
607		.desc.linear_min_sel = 0,
608		.sleep = REG_FIELD(DA9062AA_VLDO2_A,
609			__builtin_ffs((int)DA9062AA_LDO2_SL_A_MASK) - 1,
610			sizeof(unsigned int) * 8 -
611			__builtin_clz((DA9062AA_LDO2_SL_A_MASK)) - 1),
612		.suspend_sleep = REG_FIELD(DA9062AA_VLDO2_B,
613			__builtin_ffs((int)DA9062AA_LDO2_SL_B_MASK) - 1,
614			sizeof(unsigned int) * 8 -
615			__builtin_clz((DA9062AA_LDO2_SL_B_MASK)) - 1),
616		.suspend_vsel_reg = DA9062AA_VLDO2_B,
617		.suspend = REG_FIELD(DA9062AA_DVC_1,
618			__builtin_ffs((int)DA9062AA_VLDO2_SEL_MASK) - 1,
619			sizeof(unsigned int) * 8 -
620			__builtin_clz((DA9062AA_VLDO2_SEL_MASK)) - 1),
621		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
622			__builtin_ffs((int)DA9062AA_LDO2_ILIM_MASK) - 1,
623			sizeof(unsigned int) * 8 -
624			__builtin_clz((DA9062AA_LDO2_ILIM_MASK)) - 1),
625	},
626	{
627		.desc.id = DA9062_ID_LDO3,
628		.desc.name = "DA9062 LDO3",
629		.desc.of_match = of_match_ptr("ldo3"),
630		.desc.regulators_node = of_match_ptr("regulators"),
631		.desc.ops = &da9062_ldo_ops,
632		.desc.min_uV = (900) * 1000,
633		.desc.uV_step = (50) * 1000,
634		.desc.n_voltages = ((3600) - (900))/(50) + 1,
 
635		.desc.enable_reg = DA9062AA_LDO3_CONT,
636		.desc.enable_mask = DA9062AA_LDO3_EN_MASK,
637		.desc.vsel_reg = DA9062AA_VLDO3_A,
638		.desc.vsel_mask = DA9062AA_VLDO3_A_MASK,
639		.desc.linear_min_sel = 0,
640		.sleep = REG_FIELD(DA9062AA_VLDO3_A,
641			__builtin_ffs((int)DA9062AA_LDO3_SL_A_MASK) - 1,
642			sizeof(unsigned int) * 8 -
643			__builtin_clz((DA9062AA_LDO3_SL_A_MASK)) - 1),
644		.suspend_sleep = REG_FIELD(DA9062AA_VLDO3_B,
645			__builtin_ffs((int)DA9062AA_LDO3_SL_B_MASK) - 1,
646			sizeof(unsigned int) * 8 -
647			__builtin_clz((DA9062AA_LDO3_SL_B_MASK)) - 1),
648		.suspend_vsel_reg = DA9062AA_VLDO3_B,
649		.suspend = REG_FIELD(DA9062AA_DVC_1,
650			__builtin_ffs((int)DA9062AA_VLDO3_SEL_MASK) - 1,
651			sizeof(unsigned int) * 8 -
652			__builtin_clz((DA9062AA_VLDO3_SEL_MASK)) - 1),
653		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
654			__builtin_ffs((int)DA9062AA_LDO3_ILIM_MASK) - 1,
655			sizeof(unsigned int) * 8 -
656			__builtin_clz((DA9062AA_LDO3_ILIM_MASK)) - 1),
657	},
658	{
659		.desc.id = DA9062_ID_LDO4,
660		.desc.name = "DA9062 LDO4",
661		.desc.of_match = of_match_ptr("ldo4"),
662		.desc.regulators_node = of_match_ptr("regulators"),
663		.desc.ops = &da9062_ldo_ops,
664		.desc.min_uV = (900) * 1000,
665		.desc.uV_step = (50) * 1000,
666		.desc.n_voltages = ((3600) - (900))/(50) + 1,
 
667		.desc.enable_reg = DA9062AA_LDO4_CONT,
668		.desc.enable_mask = DA9062AA_LDO4_EN_MASK,
669		.desc.vsel_reg = DA9062AA_VLDO4_A,
670		.desc.vsel_mask = DA9062AA_VLDO4_A_MASK,
671		.desc.linear_min_sel = 0,
672		.sleep = REG_FIELD(DA9062AA_VLDO4_A,
673			__builtin_ffs((int)DA9062AA_LDO4_SL_A_MASK) - 1,
674			sizeof(unsigned int) * 8 -
675			__builtin_clz((DA9062AA_LDO4_SL_A_MASK)) - 1),
676		.suspend_sleep = REG_FIELD(DA9062AA_VLDO4_B,
677			__builtin_ffs((int)DA9062AA_LDO4_SL_B_MASK) - 1,
678			sizeof(unsigned int) * 8 -
679			__builtin_clz((DA9062AA_LDO4_SL_B_MASK)) - 1),
680		.suspend_vsel_reg = DA9062AA_VLDO4_B,
681		.suspend = REG_FIELD(DA9062AA_DVC_1,
682			__builtin_ffs((int)DA9062AA_VLDO4_SEL_MASK) - 1,
683			sizeof(unsigned int) * 8 -
684			__builtin_clz((DA9062AA_VLDO4_SEL_MASK)) - 1),
685		.oc_event = REG_FIELD(DA9062AA_STATUS_D,
686			__builtin_ffs((int)DA9062AA_LDO4_ILIM_MASK) - 1,
687			sizeof(unsigned int) * 8 -
688			__builtin_clz((DA9062AA_LDO4_ILIM_MASK)) - 1),
689	},
690};
691
692/* Regulator interrupt handlers */
693static irqreturn_t da9062_ldo_lim_event(int irq, void *data)
694{
695	struct da9062_regulators *regulators = data;
696	struct da9062 *hw = regulators->regulator[0].hw;
697	struct da9062_regulator *regl;
698	int handled = IRQ_NONE;
699	int bits, i, ret;
700
701	ret = regmap_read(hw->regmap, DA9062AA_STATUS_D, &bits);
702	if (ret < 0) {
703		dev_err(hw->dev,
704			"Failed to read LDO overcurrent indicator\n");
705		goto ldo_lim_error;
706	}
707
708	for (i = regulators->n_regulators - 1; i >= 0; i--) {
709		regl = &regulators->regulator[i];
710		if (regl->info->oc_event.reg != DA9062AA_STATUS_D)
711			continue;
712
713		if (BIT(regl->info->oc_event.lsb) & bits) {
714			regulator_notifier_call_chain(regl->rdev,
715					REGULATOR_EVENT_OVER_CURRENT, NULL);
716			handled = IRQ_HANDLED;
717		}
718	}
719
720ldo_lim_error:
721	return handled;
722}
723
724static int da9062_regulator_probe(struct platform_device *pdev)
725{
726	struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
727	struct da9062_regulators *regulators;
728	struct da9062_regulator *regl;
729	struct regulator_config config = { };
730	int irq, n, ret;
731	size_t size;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
732
733	/* Allocate memory required by usable regulators */
734	size = sizeof(struct da9062_regulators) +
735		DA9062_MAX_REGULATORS * sizeof(struct da9062_regulator);
736	regulators = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
737	if (!regulators)
738		return -ENOMEM;
739
740	regulators->n_regulators = DA9062_MAX_REGULATORS;
741	platform_set_drvdata(pdev, regulators);
742
743	n = 0;
744	while (n < regulators->n_regulators) {
745		/* Initialise regulator structure */
746		regl = &regulators->regulator[n];
747		regl->hw = chip;
748		regl->info = &local_regulator_info[n];
749		regl->desc = regl->info->desc;
750		regl->desc.type = REGULATOR_VOLTAGE;
751		regl->desc.owner = THIS_MODULE;
752
753		if (regl->info->mode.reg)
754			regl->mode = devm_regmap_field_alloc(
755					&pdev->dev,
756					chip->regmap,
757					regl->info->mode);
758		if (regl->info->suspend.reg)
 
 
 
 
759			regl->suspend = devm_regmap_field_alloc(
760					&pdev->dev,
761					chip->regmap,
762					regl->info->suspend);
763		if (regl->info->sleep.reg)
 
 
 
 
764			regl->sleep = devm_regmap_field_alloc(
765					&pdev->dev,
766					chip->regmap,
767					regl->info->sleep);
768		if (regl->info->suspend_sleep.reg)
 
 
 
 
769			regl->suspend_sleep = devm_regmap_field_alloc(
770					&pdev->dev,
771					chip->regmap,
772					regl->info->suspend_sleep);
773		if (regl->info->ilimit.reg)
774			regl->ilimit = devm_regmap_field_alloc(
775					&pdev->dev,
776					chip->regmap,
777					regl->info->ilimit);
778
779		/* Register regulator */
780		memset(&config, 0, sizeof(config));
781		config.dev = chip->dev;
782		config.driver_data = regl;
783		config.regmap = chip->regmap;
784
785		regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
786						     &config);
787		if (IS_ERR(regl->rdev)) {
788			dev_err(&pdev->dev,
789				"Failed to register %s regulator\n",
790				regl->desc.name);
791			return PTR_ERR(regl->rdev);
792		}
793
794		n++;
795	}
796
797	/* LDOs overcurrent event support */
798	irq = platform_get_irq_byname(pdev, "LDO_LIM");
799	if (irq < 0) {
800		dev_err(&pdev->dev, "Failed to get IRQ.\n");
801		return irq;
802	}
803	regulators->irq_ldo_lim = irq;
804
805	ret = devm_request_threaded_irq(&pdev->dev, irq,
806					NULL, da9062_ldo_lim_event,
807					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
808					"LDO_LIM", regulators);
809	if (ret) {
810		dev_warn(&pdev->dev,
811			 "Failed to request LDO_LIM IRQ.\n");
812		regulators->irq_ldo_lim = -ENXIO;
813	}
814
815	return 0;
816}
817
818static struct platform_driver da9062_regulator_driver = {
819	.driver = {
820		.name = "da9062-regulators",
 
821	},
822	.probe = da9062_regulator_probe,
823};
824
825static int __init da9062_regulator_init(void)
826{
827	return platform_driver_register(&da9062_regulator_driver);
828}
829subsys_initcall(da9062_regulator_init);
830
831static void __exit da9062_regulator_cleanup(void)
832{
833	platform_driver_unregister(&da9062_regulator_driver);
834}
835module_exit(da9062_regulator_cleanup);
836
837/* Module information */
838MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
839MODULE_DESCRIPTION("REGULATOR device driver for Dialog DA9062");
840MODULE_LICENSE("GPL");
841MODULE_ALIAS("platform:da9062-regulators");