/*
* Regulator device driver for DA9061 and DA9062.
* Copyright (C) 2015-2017 Dialog Semiconductor
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/of_regulator.h>
#include <linux/mfd/da9062/core.h>
#include <linux/mfd/da9062/registers.h>
/* Regulator IDs */
enum {
DA9061_ID_BUCK1,
DA9061_ID_BUCK2,
DA9061_ID_BUCK3,
DA9061_ID_LDO1,
DA9061_ID_LDO2,
DA9061_ID_LDO3,
DA9061_ID_LDO4,
DA9061_MAX_REGULATORS,
};
enum {
DA9062_ID_BUCK1,
DA9062_ID_BUCK2,
DA9062_ID_BUCK3,
DA9062_ID_BUCK4,
DA9062_ID_LDO1,
DA9062_ID_LDO2,
DA9062_ID_LDO3,
DA9062_ID_LDO4,
DA9062_MAX_REGULATORS,
};
/* Regulator capabilities and registers description */
struct da9062_regulator_info {
struct regulator_desc desc;
/* Current limiting */
unsigned int n_current_limits;
const int *current_limits;
/* Main register fields */
struct reg_field mode;
struct reg_field suspend;
struct reg_field sleep;
struct reg_field suspend_sleep;
unsigned int suspend_vsel_reg;
struct reg_field ilimit;
/* Event detection bit */
struct reg_field oc_event;
};
/* Single regulator settings */
struct da9062_regulator {
struct regulator_desc desc;
struct regulator_dev *rdev;
struct da9062 *hw;
const struct da9062_regulator_info *info;
struct regmap_field *mode;
struct regmap_field *suspend;
struct regmap_field *sleep;
struct regmap_field *suspend_sleep;
struct regmap_field *ilimit;
};
/* Encapsulates all information for the regulators driver */
struct da9062_regulators {
int irq_ldo_lim;
unsigned n_regulators;
/* Array size to be defined during init. Keep at end. */
struct da9062_regulator regulator[0];
};
/* BUCK modes */
enum {
BUCK_MODE_MANUAL, /* 0 */
BUCK_MODE_SLEEP, /* 1 */
BUCK_MODE_SYNC, /* 2 */
BUCK_MODE_AUTO /* 3 */
};
/* Regulator operations */
/* Current limits array (in uA)
* - DA9061_ID_[BUCK1|BUCK3]
* - DA9062_ID_[BUCK1|BUCK2|BUCK4]
* Entry indexes corresponds to register values.
*/
static const int da9062_buck_a_limits[] = {
500000, 600000, 700000, 800000, 900000, 1000000, 1100000, 1200000,
1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
};
/* Current limits array (in uA)
* - DA9061_ID_BUCK2
* - DA9062_ID_BUCK3
* Entry indexes corresponds to register values.
*/
static const int da9062_buck_b_limits[] = {
1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
};
static int da9062_set_current_limit(struct regulator_dev *rdev,
int min_ua, int max_ua)
{
struct da9062_regulator *regl = rdev_get_drvdata(rdev);
const struct da9062_regulator_info *rinfo = regl->info;
int n, tval;
for (n = 0; n < rinfo->n_current_limits; n++) {
tval = rinfo->current_limits[n];
if (tval >= min_ua && tval <= max_ua)
return regmap_field_write(regl->ilimit, n);
}
return -EINVAL;
}
static int da9062_get_current_limit(struct regulator_dev *rdev)
{
struct da9062_regulator *regl = rdev_get_drvdata(rdev);
const struct da9062_regulator_info *rinfo = regl->info;
unsigned int sel;
int ret;
ret = regmap_field_read(regl->ilimit, &sel);
if (ret < 0)
return ret;
if (sel >= rinfo->n_current_limits)
sel = rinfo->n_current_limits - 1;
return rinfo->current_limits[sel];
}
static int da9062_buck_set_mode(struct regulator_dev *rdev, unsigned mode)
{
struct da9062_regulator *regl = rdev_get_drvdata(rdev);
unsigned val;
switch (mode) {
case REGULATOR_MODE_FAST:
val = BUCK_MODE_SYNC;
break;
case REGULATOR_MODE_NORMAL:
val = BUCK_MODE_AUTO;
break;
case REGULATOR_MODE_STANDBY:
val = BUCK_MODE_SLEEP;
break;
default:
return -EINVAL;
}
return regmap_field_write(regl->mode, val);
}
/*
* Bucks use single mode register field for normal operation
* and suspend state.
* There are 3 modes to map to: FAST, NORMAL, and STANDBY.
*/
static unsigned da9062_buck_get_mode(struct regulator_dev *rdev)
{