<commit_before>/*
 * Copyright 2012 Texas Instruments
 *
 * Author: Milo(Woogyom) Kim <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/regulator/lp872x.h>
#include <linux/regulator/driver.h>
#include <linux/platform_device.h>

/* Registers : LP8720/8725 shared */
#define LP872X_GENERAL_CFG		0x00
#define LP872X_LDO1_VOUT		0x01
#define LP872X_LDO2_VOUT		0x02
#define LP872X_LDO3_VOUT		0x03
#define LP872X_LDO4_VOUT		0x04
#define LP872X_LDO5_VOUT		0x05

/* Registers : LP8720 */
#define LP8720_BUCK_VOUT1		0x06
#define LP8720_BUCK_VOUT2		0x07
#define LP8720_ENABLE			0x08

/* Registers : LP8725 */
#define LP8725_LILO1_VOUT		0x06
#define LP8725_LILO2_VOUT		0x07
#define LP8725_BUCK1_VOUT1		0x08
#define LP8725_BUCK1_VOUT2		0x09
#define LP8725_BUCK2_VOUT1		0x0A
#define LP8725_BUCK2_VOUT2		0x0B
#define LP8725_BUCK_CTRL		0x0C
#define LP8725_LDO_CTRL			0x0D

/* Mask/shift : LP8720/LP8725 shared */
#define LP872X_VOUT_M			0x1F
#define LP872X_START_DELAY_M		0xE0
#define LP872X_START_DELAY_S		5
#define LP872X_EN_LDO1_M		BIT(0)
#define LP872X_EN_LDO2_M		BIT(1)
#define LP872X_EN_LDO3_M		BIT(2)
#define LP872X_EN_LDO4_M		BIT(3)
#define LP872X_EN_LDO5_M		BIT(4)

/* Mask/shift : LP8720 */
#define LP8720_TIMESTEP_S		0		/* Addr 00h */
#define LP8720_TIMESTEP_M		BIT(0)
#define LP8720_EXT_DVS_M		BIT(2)
#define LP8720_BUCK_FPWM_S		5		/* Addr 07h */
#define LP8720_BUCK_FPWM_M		BIT(5)
#define LP8720_EN_BUCK_M		BIT(5)		/* Addr 08h */
#define LP8720_DVS_SEL_M		BIT(7)

/* Mask/shift : LP8725 */
#define LP8725_TIMESTEP_M		0xC0		/* Addr 00h */
#define LP8725_TIMESTEP_S		6
#define LP8725_BUCK1_EN_M		BIT(0)
#define LP8725_DVS1_M			BIT(2)
#define LP8725_DVS2_M			BIT(3)
#define LP8725_BUCK2_EN_M		BIT(4)
#define LP8725_BUCK_CL_M		0xC0		/* Addr 09h, 0Bh */
#define LP8725_BUCK_CL_S		6
#define LP8725_BUCK1_FPWM_S		1		/* Addr 0Ch */
#define LP8725_BUCK1_FPWM_M		BIT(1)
#define LP8725_BUCK2_FPWM_S		5
#define LP8725_BUCK2_FPWM_M		BIT(5)
#define LP8725_EN_LILO1_M		BIT(5)		/* Addr 0Dh */
#define LP8725_EN_LILO2_M		BIT(6)

/* PWM mode */
#define LP872X_FORCE_PWM		1
#define LP872X_AUTO_PWM			0

#define LP8720_NUM_REGULATORS		6
#define LP8725_NUM_REGULATORS		9
#define EXTERN_DVS_USED			0
#define MAX_DELAY			6

/* Default DVS Mode */
#define LP8720_DEFAULT_DVS		0
#define LP8725_DEFAULT_DVS		BIT(2)

/* dump registers in regmap-debugfs */
#define MAX_REGISTERS			0x0F

enum lp872x_id {
	LP8720,
	LP8725,
};

struct lp872x {
	struct regmap *regmap;
	struct device *dev;
	enum lp872x_id chipid;
	struct lp872x_platform_data *pdata;
	struct regulator_dev **regulators;
	int num_regulators;
	enum lp872x_dvs_state dvs_pin;
	int dvs_gpio;
};

/* LP8720/LP8725 shared voltage table for LDOs */
static const unsigned int lp872x_ldo_vtbl[] = {
	1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
	1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
	2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
	2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
};

/* LP8720 LDO4 voltage table */
static const unsigned int lp8720_ldo4_vtbl[] = {
	 800000,  850000,  900000, 1000000, 1100000, 1200000, 1250000, 1300000,
	1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
	1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
	2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
};

/* LP8725 LILO(Low Input Low Output) voltage table */
static const unsigned int lp8725_lilo_vtbl[] = {
	 800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
	1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
	2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
};

/* LP8720 BUCK voltage table */
#define EXT_R		0	/* external resistor divider */
static const unsigned int lp8720_buck_vtbl[] = {
	  EXT_R,  800000,  850000,  900000,  950000, 1000000, 1050000, 1100000,
	1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
	1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
	1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
};

/* LP8725 BUCK voltage table */
static const unsigned int lp8725_buck_vtbl[] = {
	 800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
	1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
	2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
};

/* LP8725 BUCK current limit */
static const unsigned int lp8725_buck_uA[] = {
	460000, 780000, 1050000, 1370000,
};

static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
{
	int ret;
	unsigned int val;

	ret = regmap_read(lp->regmap, addr, &val);
	if (ret < 0) {
		dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
		return ret;
	}

	*data = (u8)val;
	return 0;
}

static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
{
	return regmap_write(lp->regmap, addr, data);
}

static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
				unsigned int mask, u8 data)
{
	return regmap_update_bits(lp->regmap, addr, mask, data);
}

static int _rdev_to_offset(struct regulator_dev *rdev)
{
	enum lp872x_regulator_id id = rdev_get_id(rdev);

	switch (id) {
	case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
		return id;
	case LP8725_ID_LDO1 ... LP8725_ID_BUCK2:
		return id - LP8725_ID_BASE;
	default:
		return -EINVAL;
	}
}

static int lp872x_get_timestep_usec(struct lp872x *lp)
{
	enum lp872x_id chip = lp->chipid;
	u8 val, mask, shift;
	int *time_usec, size, ret;
	int lp8720_time_usec[] = { 25, 50 };
	int lp8725_time_usec[] = { 32, 64, 128, 256 };

	switch (chip) {
	case LP8720:
		mask = LP8720_TIMESTEP_M;
		shift = LP8720_TIMESTEP_S;
		time_usec = &lp8720_time_usec[0];
		size = ARRAY_SIZE(lp8720_time_usec);
		break;
	case LP8725:
		mask = LP8725_TIMESTEP_M;
		shift = LP8725_TIMESTEP_S;
		time_usec = &lp8725_time_usec[0];
		size = ARRAY_SIZE(lp8725_time_usec);
		break;
	default:
		return -EINVAL;
	}

	ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
	if (ret)
		return -EINVAL;

	val = (val & mask) >> shift;
	if (val >= size)
		return -EINVAL;

	return *(time_usec + val);
}

static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
{
	struct lp872x *lp = rdev_get_drvdata(rdev);
	enum lp872x_regulator_id regulator = rdev_get_id(rdev);
	int time_step_us = lp872x_get_timestep_usec(lp);
	int ret, offset;
	u8 addr, val;

	if (time_step_us < 0)
		return -EINVAL;

	switch (regulator) {
	case LP8720_ID_LDO1 ... LP8720_ID_LDO5:
	case LP8725_ID_LDO1 ... LP8725_ID_LILO2:
		offset = _rdev_to_offset(rdev);
		if (offset < 0)
			return -EINVAL;

		addr = LP872X_LDO1_VOUT + offset;
		break;
	case LP8720_ID_BUCK:
		addr = LP8720_BUCK_VOUT1;
		break;
	case LP8725_ID_BUCK1:
		addr = LP8725_BUCK1_VOUT1;
		break;
	case LP8725_ID_BUCK2:
		addr = LP8725_BUCK2_VOUT1;
		break;
	default:
		return -EINVAL;
	}

	ret = lp872x_read_byte(lp, addr, &val);
	if (ret)
		return ret;

	val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;

	return val > MAX_DELAY ? 0 : val * time_step_us;
}

static void lp872x_set_dvs(struct lp872x *lp, int gpio)
{
	enum lp872x_dvs_sel dvs_sel = lp->pdata->dvs->vsel;
	enum lp872x_dvs_state state;

	state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
	gpio_set_value(gpio, state);
	lp->dvs_pin = state;
}

static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
				enum lp872x_regulator_id buck)
{
	u8 val, addr;

	if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
		return 0;

	switch (buck) {
	case LP8720_ID_BUCK:
		if (val & LP8720_EXT_DVS_M) {
			addr = (lp->dvs_pin == DVS_HIGH) ?
				LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
		} else {
			if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
				return 0;

			addr = val & LP8720_DVS_SEL_M ?
				LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
		}
		break;
	case LP8725_ID_BUCK1:
		if (val & LP8725_DVS1_M)
			addr = LP8725_BUCK1_VOUT1;
		else
			addr = (lp->dvs_pin == DVS_HIGH) ?
				LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
		break;
	case LP8725_ID_BUCK2:
		addr =  val & LP8725_DVS2_M ?
			LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
		break;
	default:
		return 0;
	}

	return addr;
}

static bool lp872x_is_valid_buck_addr(u8 addr)
{
	switch (addr) {
	case LP8720_BUCK_VOUT1:
	case LP8720_BUCK_VOUT2:
	case LP8725_BUCK1_VOUT1:
	case LP8725_BUCK1_VOUT2:
	case LP8725_BUCK2_VOUT1:
	case LP8725_BUCK2_VOUT2:
		return true;
	default:
		return false;
	}
}

static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
					unsigned selector)
{
	struct lp872x *lp = rdev_get_drvdata(rdev);
	enum lp872x_regulator_id buck = rdev_get_id(rdev);
	u8 addr, mask = LP872X_VOUT_M;
	struct lp872x_dvs *dvs = lp->pdata->dvs;

	if (dvs && gpio_is_valid(dvs->gpio))
		lp872x_set_dvs(lp, dvs->gpio);

	addr = lp872x_select_buck_vout_addr(lp, buck);
	if (!lp872x_is_valid_buck_addr(addr))
		return -EINVAL;

	return lp872x_update_bits(lp, addr, mask, selector);
}

static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
{
	struct lp872x *lp = rdev_get_drvdata(rdev);
	enum lp872x_regulator_id buck = rdev_get_id(rdev);
	u8 addr, val;
	int ret;

	addr = lp872x_select_buck_vout_addr(lp, buck);
	if (!lp872x_is_valid_buck_addr(addr))
		return -EINVAL;<commit_msg>regulator: lp872x: fix NULL pointer access problem<commit_after>/*
 * Copyright 2012 Texas Instruments
 *
 * Author: Milo(Woogyom) Kim <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/regulator/lp872x.h>
#include <linux/regulator/driver.h>
#include <linux/platform_device.h>

/* Registers : LP8720/8725 shared */
#define LP872X_GENERAL_CFG		0x00
#define LP872X_LDO1_VOUT		0x01
#define LP872X_LDO2_VOUT		0x02
#define LP872X_LDO3_VOUT		0x03
#define LP872X_LDO4_VOUT		0x04
#define LP872X_LDO5_VOUT		0x05

/* Registers : LP8720 */
#define LP8720_BUCK_VOUT1		0x06
#define LP8720_BUCK_VOUT2		0x07
#define LP8720_ENABLE			0x08

/* Registers : LP8725 */
#define LP8725_LILO1_VOUT		0x06
#define LP8725_LILO2_VOUT		0x07
#define LP8725_BUCK1_VOUT1		0x08
#define LP8725_BUCK1_VOUT2		0x09
#define LP8725_BUCK2_VOUT1		0x0A
#define LP8725_BUCK2_VOUT2		0x0B
#define LP8725_BUCK_CTRL		0x0C
#define LP8725_LDO_CTRL			0x0D

/* Mask/shift : LP8720/LP8725 shared */
#define LP872X_VOUT_M			0x1F
#define LP872X_START_DELAY_M		0xE0
#define LP872X_START_DELAY_S		5
#define LP872X_EN_LDO1_M		BIT(0)
#define LP872X_EN_LDO2_M		BIT(1)
#define LP872X_EN_LDO3_M		BIT(2)
#define LP872X_EN_LDO4_M		BIT(3)
#define LP872X_EN_LDO5_M		BIT(4)

/* Mask/shift : LP8720 */
#define LP8720_TIMESTEP_S		0		/* Addr 00h */
#define LP8720_TIMESTEP_M		BIT(0)
#define LP8720_EXT_DVS_M		BIT(2)
#define LP8720_BUCK_FPWM_S		5		/* Addr 07h */
#define LP8720_BUCK_FPWM_M		BIT(5)
#define LP8720_EN_BUCK_M		BIT(5)		/* Addr 08h */
#define LP8720_DVS_SEL_M		BIT(7)

/* Mask/shift : LP8725 */
#define LP8725_TIMESTEP_M		0xC0		/* Addr 00h */
#define LP8725_TIMESTEP_S		6
#define LP8725_BUCK1_EN_M		BIT(0)
#define LP8725_DVS1_M			BIT(2)
#define LP8725_DVS2_M			BIT(3)
#define LP8725_BUCK2_EN_M		BIT(4)
#define LP8725_BUCK_CL_M		0xC0		/* Addr 09h, 0Bh */
#define LP8725_BUCK_CL_S		6
#define LP8725_BUCK1_FPWM_S		1		/* Addr 0Ch */
#define LP8725_BUCK1_FPWM_M		BIT(1)
#define LP8725_BUCK2_FPWM_S		5
#define LP8725_BUCK2_FPWM_M		BIT(5)
#define LP8725_EN_LILO1_M		BIT(5)		/* Addr 0Dh */
#define LP8725_EN_LILO2_M		BIT(6)

/* PWM mode */
#define LP872X_FORCE_PWM		1
#define LP872X_AUTO_PWM			0

#define LP8720_NUM_REGULATORS		6
#define LP8725_NUM_REGULATORS		9
#define EXTERN_DVS_USED			0
#define MAX_DELAY			6

/* Default DVS Mode */
#define LP8720_DEFAULT_DVS		0
#define LP8725_DEFAULT_DVS		BIT(2)

/* dump registers in regmap-debugfs */
#define MAX_REGISTERS			0x0F

enum lp872x_id {
	LP8720,
	LP8725,
};

struct lp872x {
	struct regmap *regmap;
	struct device *dev;
	enum lp872x_id chipid;
	struct lp872x_platform_data *pdata;
	struct regulator_dev **regulators;
	int num_regulators;
	enum lp872x_dvs_state dvs_pin;
	int dvs_gpio;
};

/* LP8720/LP8725 shared voltage table for LDOs */
static const unsigned int lp872x_ldo_vtbl[] = {
	1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
	1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
	2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
	2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
};

/* LP8720 LDO4 voltage table */
static const unsigned int lp8720_ldo4_vtbl[] = {
	 800000,  850000,  900000, 1000000, 1100000, 1200000, 1250000, 1300000,
	1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
	1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
	2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
};

/* LP8725 LILO(Low Input Low Output) voltage table */
static const unsigned int lp8725_lilo_vtbl[] = {
	 800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
	1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
	2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
};

/* LP8720 BUCK voltage table */
#define EXT_R		0	/* external resistor divider */
static const unsigned int lp8720_buck_vtbl[] = {
	  EXT_R,  800000,  850000,  900000,  950000, 1000000, 1050000, 1100000,
	1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
	1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
	1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
};

/* LP8725 BUCK voltage table */
static const unsigned int lp8725_buck_vtbl[] = {
	 800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
	1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
	1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
	2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
};

/* LP8725 BUCK current limit */
static const unsigned int lp8725_buck_uA[] = {
	460000, 780000, 1050000, 1370000,
};

static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
{
	int ret;
	unsigned int val;

	ret = regmap_read(lp->regmap, addr, &val);
	if (ret < 0) {
		dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
		return ret;
	}

	*data = (u8)val;
	return 0;
}

static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
{
	return regmap_write(lp->regmap, addr, data);
}

static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
				unsigned int mask, u8 data)
{
	return regmap_update_bits(lp->regmap, addr, mask, data);
}

static int _rdev_to_offset(struct regulator_dev *rdev)
{
	enum lp872x_regulator_id id = rdev_get_id(rdev);

	switch (id) {
	case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
		return id;
	case LP8725_ID_LDO1 ... LP8725_ID_BUCK2:
		return id - LP8725_ID_BASE;
	default:
		return -EINVAL;
	}
}

static int lp872x_get_timestep_usec(struct lp872x *lp)
{
	enum lp872x_id chip = lp->chipid;
	u8 val, mask, shift;
	int *time_usec, size, ret;
	int lp8720_time_usec[] = { 25, 50 };
	int lp8725_time_usec[] = { 32, 64, 128, 256 };

	switch (chip) {
	case LP8720:
		mask = LP8720_TIMESTEP_M;
		shift = LP8720_TIMESTEP_S;
		time_usec = &lp8720_time_usec[0];
		size = ARRAY_SIZE(lp8720_time_usec);
		break;
	case LP8725:
		mask = LP8725_TIMESTEP_M;
		shift = LP8725_TIMESTEP_S;
		time_usec = &lp8725_time_usec[0];
		size = ARRAY_SIZE(lp8725_time_usec);
		break;
	default:
		return -EINVAL;
	}

	ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
	if (ret)
		return -EINVAL;

	val = (val & mask) >> shift;
	if (val >= size)
		return -EINVAL;

	return *(time_usec + val);
}

static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
{
	struct lp872x *lp = rdev_get_drvdata(rdev);
	enum lp872x_regulator_id regulator = rdev_get_id(rdev);
	int time_step_us = lp872x_get_timestep_usec(lp);
	int ret, offset;
	u8 addr, val;

	if (time_step_us < 0)
		return -EINVAL;

	switch (regulator) {
	case LP8720_ID_LDO1 ... LP8720_ID_LDO5:
	case LP8725_ID_LDO1 ... LP8725_ID_LILO2:
		offset = _rdev_to_offset(rdev);
		if (offset < 0)
			return -EINVAL;

		addr = LP872X_LDO1_VOUT + offset;
		break;
	case LP8720_ID_BUCK:
		addr = LP8720_BUCK_VOUT1;
		break;
	case LP8725_ID_BUCK1:
		addr = LP8725_BUCK1_VOUT1;
		break;
	case LP8725_ID_BUCK2:
		addr = LP8725_BUCK2_VOUT1;
		break;
	default:
		return -EINVAL;
	}

	ret = lp872x_read_byte(lp, addr, &val);
	if (ret)
		return ret;

	val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;

	return val > MAX_DELAY ? 0 : val * time_step_us;
}

static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
			int gpio)
{
	enum lp872x_dvs_state state;

	state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
	gpio_set_value(gpio, state);
	lp->dvs_pin = state;
}

static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
				enum lp872x_regulator_id buck)
{
	u8 val, addr;

	if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
		return 0;

	switch (buck) {
	case LP8720_ID_BUCK:
		if (val & LP8720_EXT_DVS_M) {
			addr = (lp->dvs_pin == DVS_HIGH) ?
				LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
		} else {
			if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
				return 0;

			addr = val & LP8720_DVS_SEL_M ?
				LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
		}
		break;
	case LP8725_ID_BUCK1:
		if (val & LP8725_DVS1_M)
			addr = LP8725_BUCK1_VOUT1;
		else
			addr = (lp->dvs_pin == DVS_HIGH) ?
				LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
		break;
	case LP8725_ID_BUCK2:
		addr =  val & LP8725_DVS2_M ?
			LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
		break;
	default:
		return 0;
	}

	return addr;
}

static bool lp872x_is_valid_buck_addr(u8 addr)
{
	switch (addr) {
	case LP8720_BUCK_VOUT1:
	case LP8720_BUCK_VOUT2:
	case LP8725_BUCK1_VOUT1:
	case LP8725_BUCK1_VOUT2:
	case LP8725_BUCK2_VOUT1:
	case LP8725_BUCK2_VOUT2:
		return true;
	default:
		return false;
	}
}

static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
					unsigned selector)
{
	struct lp872x *lp = rdev_get_drvdata(rdev);
	enum lp872x_regulator_id buck = rdev_get_id(rdev);
	u8 addr, mask = LP872X_VOUT_M;
	struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;

	if (dvs && gpio_is_valid(dvs->gpio))
		lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);

	addr = lp872x_select_buck_vout_addr(lp, buck);
	if (!lp872x_is_valid_buck_addr(addr))
		return -EINVAL;

	return lp872x_update_bits(lp, addr, mask, selector);
}

static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
{
	struct lp872x *lp = rdev_get_drvdata(rdev);
	enum lp872x_regulator_id buck = rdev_get_id(rdev);
	u8 addr, val;
	int ret;

	addr = lp872x_select_buck_vout_addr(lp, buck);
	if (!lp872x_is_valid_buck_addr(addr))
		return -EINVAL;
{'fraction_non_alphanumeric': 0.1021815188310352, 'fraction_numerical': 0.2047231940728545, 'mean_word_length': 4.79344262295082, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942940', 'n_tokens_mistral': 11594, 'n_tokens_neox': 8680, 'n_words': 1893}