18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// pv88090-regulator.c - Regulator device driver for PV88090
48c2ecf20Sopenharmony_ci// Copyright (C) 2015  Powerventure Semiconductor Ltd.
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/err.h>
78c2ecf20Sopenharmony_ci#include <linux/i2c.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/init.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
128c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
138c2ecf20Sopenharmony_ci#include <linux/regmap.h>
148c2ecf20Sopenharmony_ci#include <linux/irq.h>
158c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
168c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
178c2ecf20Sopenharmony_ci#include "pv88090-regulator.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define PV88090_MAX_REGULATORS	5
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* PV88090 REGULATOR IDs */
228c2ecf20Sopenharmony_cienum {
238c2ecf20Sopenharmony_ci	/* BUCKs */
248c2ecf20Sopenharmony_ci	PV88090_ID_BUCK1,
258c2ecf20Sopenharmony_ci	PV88090_ID_BUCK2,
268c2ecf20Sopenharmony_ci	PV88090_ID_BUCK3,
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	/* LDOs */
298c2ecf20Sopenharmony_ci	PV88090_ID_LDO1,
308c2ecf20Sopenharmony_ci	PV88090_ID_LDO2,
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistruct pv88090_regulator {
348c2ecf20Sopenharmony_ci	struct regulator_desc desc;
358c2ecf20Sopenharmony_ci	unsigned int conf;
368c2ecf20Sopenharmony_ci	unsigned int conf2;
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct pv88090 {
408c2ecf20Sopenharmony_ci	struct device *dev;
418c2ecf20Sopenharmony_ci	struct regmap *regmap;
428c2ecf20Sopenharmony_ci	struct regulator_dev *rdev[PV88090_MAX_REGULATORS];
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct pv88090_buck_voltage {
468c2ecf20Sopenharmony_ci	int min_uV;
478c2ecf20Sopenharmony_ci	int max_uV;
488c2ecf20Sopenharmony_ci	int uV_step;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic const struct regmap_config pv88090_regmap_config = {
528c2ecf20Sopenharmony_ci	.reg_bits = 8,
538c2ecf20Sopenharmony_ci	.val_bits = 8,
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
578c2ecf20Sopenharmony_ci *  Entry indexes corresponds to register values.
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic const unsigned int pv88090_buck1_limits[] = {
618c2ecf20Sopenharmony_ci	 220000,  440000,  660000,  880000, 1100000, 1320000, 1540000, 1760000,
628c2ecf20Sopenharmony_ci	1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
638c2ecf20Sopenharmony_ci	3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
648c2ecf20Sopenharmony_ci	5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic const unsigned int pv88090_buck23_limits[] = {
688c2ecf20Sopenharmony_ci	1496000, 2393000, 3291000, 4189000
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic const struct pv88090_buck_voltage pv88090_buck_vol[3] = {
728c2ecf20Sopenharmony_ci	{
738c2ecf20Sopenharmony_ci		.min_uV = 600000,
748c2ecf20Sopenharmony_ci		.max_uV = 1393750,
758c2ecf20Sopenharmony_ci		.uV_step = 6250,
768c2ecf20Sopenharmony_ci	},
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	{
798c2ecf20Sopenharmony_ci		.min_uV = 1400000,
808c2ecf20Sopenharmony_ci		.max_uV = 2193750,
818c2ecf20Sopenharmony_ci		.uV_step = 6250,
828c2ecf20Sopenharmony_ci	},
838c2ecf20Sopenharmony_ci	{
848c2ecf20Sopenharmony_ci		.min_uV = 1250000,
858c2ecf20Sopenharmony_ci		.max_uV = 2837500,
868c2ecf20Sopenharmony_ci		.uV_step = 12500,
878c2ecf20Sopenharmony_ci	},
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic unsigned int pv88090_buck_get_mode(struct regulator_dev *rdev)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct pv88090_regulator *info = rdev_get_drvdata(rdev);
938c2ecf20Sopenharmony_ci	unsigned int data;
948c2ecf20Sopenharmony_ci	int ret, mode = 0;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	ret = regmap_read(rdev->regmap, info->conf, &data);
978c2ecf20Sopenharmony_ci	if (ret < 0)
988c2ecf20Sopenharmony_ci		return ret;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	switch (data & PV88090_BUCK1_MODE_MASK) {
1018c2ecf20Sopenharmony_ci	case PV88090_BUCK_MODE_SYNC:
1028c2ecf20Sopenharmony_ci		mode = REGULATOR_MODE_FAST;
1038c2ecf20Sopenharmony_ci		break;
1048c2ecf20Sopenharmony_ci	case PV88090_BUCK_MODE_AUTO:
1058c2ecf20Sopenharmony_ci		mode = REGULATOR_MODE_NORMAL;
1068c2ecf20Sopenharmony_ci		break;
1078c2ecf20Sopenharmony_ci	case PV88090_BUCK_MODE_SLEEP:
1088c2ecf20Sopenharmony_ci		mode = REGULATOR_MODE_STANDBY;
1098c2ecf20Sopenharmony_ci		break;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return mode;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int pv88090_buck_set_mode(struct regulator_dev *rdev,
1168c2ecf20Sopenharmony_ci					unsigned int mode)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct pv88090_regulator *info = rdev_get_drvdata(rdev);
1198c2ecf20Sopenharmony_ci	int val = 0;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	switch (mode) {
1228c2ecf20Sopenharmony_ci	case REGULATOR_MODE_FAST:
1238c2ecf20Sopenharmony_ci		val = PV88090_BUCK_MODE_SYNC;
1248c2ecf20Sopenharmony_ci		break;
1258c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
1268c2ecf20Sopenharmony_ci		val = PV88090_BUCK_MODE_AUTO;
1278c2ecf20Sopenharmony_ci		break;
1288c2ecf20Sopenharmony_ci	case REGULATOR_MODE_STANDBY:
1298c2ecf20Sopenharmony_ci		val = PV88090_BUCK_MODE_SLEEP;
1308c2ecf20Sopenharmony_ci		break;
1318c2ecf20Sopenharmony_ci	default:
1328c2ecf20Sopenharmony_ci		return -EINVAL;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return regmap_update_bits(rdev->regmap, info->conf,
1368c2ecf20Sopenharmony_ci					PV88090_BUCK1_MODE_MASK, val);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic const struct regulator_ops pv88090_buck_ops = {
1408c2ecf20Sopenharmony_ci	.get_mode = pv88090_buck_get_mode,
1418c2ecf20Sopenharmony_ci	.set_mode = pv88090_buck_set_mode,
1428c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
1438c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
1448c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
1458c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
1468c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
1478c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1488c2ecf20Sopenharmony_ci	.set_current_limit = regulator_set_current_limit_regmap,
1498c2ecf20Sopenharmony_ci	.get_current_limit = regulator_get_current_limit_regmap,
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct regulator_ops pv88090_ldo_ops = {
1538c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
1548c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
1558c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
1568c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
1578c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
1588c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
1628c2ecf20Sopenharmony_ci{\
1638c2ecf20Sopenharmony_ci	.desc	=	{\
1648c2ecf20Sopenharmony_ci		.id = chip##_ID_##regl_name,\
1658c2ecf20Sopenharmony_ci		.name = __stringify(chip##_##regl_name),\
1668c2ecf20Sopenharmony_ci		.of_match = of_match_ptr(#regl_name),\
1678c2ecf20Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),\
1688c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,\
1698c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,\
1708c2ecf20Sopenharmony_ci		.ops = &pv88090_buck_ops,\
1718c2ecf20Sopenharmony_ci		.min_uV = min, \
1728c2ecf20Sopenharmony_ci		.uV_step = step, \
1738c2ecf20Sopenharmony_ci		.n_voltages = ((max) - (min))/(step) + 1, \
1748c2ecf20Sopenharmony_ci		.enable_reg = PV88090_REG_##regl_name##_CONF0, \
1758c2ecf20Sopenharmony_ci		.enable_mask = PV88090_##regl_name##_EN, \
1768c2ecf20Sopenharmony_ci		.vsel_reg = PV88090_REG_##regl_name##_CONF0, \
1778c2ecf20Sopenharmony_ci		.vsel_mask = PV88090_V##regl_name##_MASK, \
1788c2ecf20Sopenharmony_ci		.curr_table = limits_array, \
1798c2ecf20Sopenharmony_ci		.n_current_limits = ARRAY_SIZE(limits_array), \
1808c2ecf20Sopenharmony_ci		.csel_reg = PV88090_REG_##regl_name##_CONF1, \
1818c2ecf20Sopenharmony_ci		.csel_mask = PV88090_##regl_name##_ILIM_MASK, \
1828c2ecf20Sopenharmony_ci	},\
1838c2ecf20Sopenharmony_ci	.conf = PV88090_REG_##regl_name##_CONF1, \
1848c2ecf20Sopenharmony_ci	.conf2 = PV88090_REG_##regl_name##_CONF2, \
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci#define PV88090_LDO(chip, regl_name, min, step, max) \
1888c2ecf20Sopenharmony_ci{\
1898c2ecf20Sopenharmony_ci	.desc	=	{\
1908c2ecf20Sopenharmony_ci		.id = chip##_ID_##regl_name,\
1918c2ecf20Sopenharmony_ci		.name = __stringify(chip##_##regl_name),\
1928c2ecf20Sopenharmony_ci		.of_match = of_match_ptr(#regl_name),\
1938c2ecf20Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),\
1948c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,\
1958c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,\
1968c2ecf20Sopenharmony_ci		.ops = &pv88090_ldo_ops,\
1978c2ecf20Sopenharmony_ci		.min_uV = min, \
1988c2ecf20Sopenharmony_ci		.uV_step = step, \
1998c2ecf20Sopenharmony_ci		.n_voltages = ((max) - (min))/(step) + 1, \
2008c2ecf20Sopenharmony_ci		.enable_reg = PV88090_REG_##regl_name##_CONT, \
2018c2ecf20Sopenharmony_ci		.enable_mask = PV88090_##regl_name##_EN, \
2028c2ecf20Sopenharmony_ci		.vsel_reg = PV88090_REG_##regl_name##_CONT, \
2038c2ecf20Sopenharmony_ci		.vsel_mask = PV88090_V##regl_name##_MASK, \
2048c2ecf20Sopenharmony_ci	},\
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic struct pv88090_regulator pv88090_regulator_info[] = {
2088c2ecf20Sopenharmony_ci	PV88090_BUCK(PV88090, BUCK1, 600000, 6250, 1393750,
2098c2ecf20Sopenharmony_ci		pv88090_buck1_limits),
2108c2ecf20Sopenharmony_ci	PV88090_BUCK(PV88090, BUCK2, 600000, 6250, 1393750,
2118c2ecf20Sopenharmony_ci		pv88090_buck23_limits),
2128c2ecf20Sopenharmony_ci	PV88090_BUCK(PV88090, BUCK3, 600000, 6250, 1393750,
2138c2ecf20Sopenharmony_ci		pv88090_buck23_limits),
2148c2ecf20Sopenharmony_ci	PV88090_LDO(PV88090, LDO1, 1200000, 50000, 4350000),
2158c2ecf20Sopenharmony_ci	PV88090_LDO(PV88090, LDO2,  650000, 25000, 2225000),
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic irqreturn_t pv88090_irq_handler(int irq, void *data)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct pv88090 *chip = data;
2218c2ecf20Sopenharmony_ci	int i, reg_val, err, ret = IRQ_NONE;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	err = regmap_read(chip->regmap, PV88090_REG_EVENT_A, &reg_val);
2248c2ecf20Sopenharmony_ci	if (err < 0)
2258c2ecf20Sopenharmony_ci		goto error_i2c;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (reg_val & PV88090_E_VDD_FLT) {
2288c2ecf20Sopenharmony_ci		for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
2298c2ecf20Sopenharmony_ci			if (chip->rdev[i] != NULL)
2308c2ecf20Sopenharmony_ci				regulator_notifier_call_chain(chip->rdev[i],
2318c2ecf20Sopenharmony_ci					REGULATOR_EVENT_UNDER_VOLTAGE,
2328c2ecf20Sopenharmony_ci					NULL);
2338c2ecf20Sopenharmony_ci		}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
2368c2ecf20Sopenharmony_ci			PV88090_E_VDD_FLT);
2378c2ecf20Sopenharmony_ci		if (err < 0)
2388c2ecf20Sopenharmony_ci			goto error_i2c;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		ret = IRQ_HANDLED;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	if (reg_val & PV88090_E_OVER_TEMP) {
2448c2ecf20Sopenharmony_ci		for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
2458c2ecf20Sopenharmony_ci			if (chip->rdev[i] != NULL)
2468c2ecf20Sopenharmony_ci				regulator_notifier_call_chain(chip->rdev[i],
2478c2ecf20Sopenharmony_ci					REGULATOR_EVENT_OVER_TEMP,
2488c2ecf20Sopenharmony_ci					NULL);
2498c2ecf20Sopenharmony_ci		}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci		err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
2528c2ecf20Sopenharmony_ci			PV88090_E_OVER_TEMP);
2538c2ecf20Sopenharmony_ci		if (err < 0)
2548c2ecf20Sopenharmony_ci			goto error_i2c;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci		ret = IRQ_HANDLED;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	return ret;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cierror_i2c:
2628c2ecf20Sopenharmony_ci	dev_err(chip->dev, "I2C error : %d\n", err);
2638c2ecf20Sopenharmony_ci	return IRQ_NONE;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci/*
2678c2ecf20Sopenharmony_ci * I2C driver interface functions
2688c2ecf20Sopenharmony_ci */
2698c2ecf20Sopenharmony_cistatic int pv88090_i2c_probe(struct i2c_client *i2c)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
2728c2ecf20Sopenharmony_ci	struct pv88090 *chip;
2738c2ecf20Sopenharmony_ci	struct regulator_config config = { };
2748c2ecf20Sopenharmony_ci	int error, i, ret = 0;
2758c2ecf20Sopenharmony_ci	unsigned int conf2, range, index;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88090), GFP_KERNEL);
2788c2ecf20Sopenharmony_ci	if (!chip)
2798c2ecf20Sopenharmony_ci		return -ENOMEM;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	chip->dev = &i2c->dev;
2828c2ecf20Sopenharmony_ci	chip->regmap = devm_regmap_init_i2c(i2c, &pv88090_regmap_config);
2838c2ecf20Sopenharmony_ci	if (IS_ERR(chip->regmap)) {
2848c2ecf20Sopenharmony_ci		error = PTR_ERR(chip->regmap);
2858c2ecf20Sopenharmony_ci		dev_err(chip->dev, "Failed to allocate register map: %d\n",
2868c2ecf20Sopenharmony_ci			error);
2878c2ecf20Sopenharmony_ci		return error;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, chip);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (i2c->irq != 0) {
2938c2ecf20Sopenharmony_ci		ret = regmap_write(chip->regmap, PV88090_REG_MASK_A, 0xFF);
2948c2ecf20Sopenharmony_ci		if (ret < 0) {
2958c2ecf20Sopenharmony_ci			dev_err(chip->dev,
2968c2ecf20Sopenharmony_ci				"Failed to mask A reg: %d\n", ret);
2978c2ecf20Sopenharmony_ci			return ret;
2988c2ecf20Sopenharmony_ci		}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci		ret = regmap_write(chip->regmap, PV88090_REG_MASK_B, 0xFF);
3018c2ecf20Sopenharmony_ci		if (ret < 0) {
3028c2ecf20Sopenharmony_ci			dev_err(chip->dev,
3038c2ecf20Sopenharmony_ci				"Failed to mask B reg: %d\n", ret);
3048c2ecf20Sopenharmony_ci			return ret;
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
3088c2ecf20Sopenharmony_ci					pv88090_irq_handler,
3098c2ecf20Sopenharmony_ci					IRQF_TRIGGER_LOW|IRQF_ONESHOT,
3108c2ecf20Sopenharmony_ci					"pv88090", chip);
3118c2ecf20Sopenharmony_ci		if (ret != 0) {
3128c2ecf20Sopenharmony_ci			dev_err(chip->dev, "Failed to request IRQ: %d\n",
3138c2ecf20Sopenharmony_ci				i2c->irq);
3148c2ecf20Sopenharmony_ci			return ret;
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		ret = regmap_update_bits(chip->regmap, PV88090_REG_MASK_A,
3188c2ecf20Sopenharmony_ci			PV88090_M_VDD_FLT | PV88090_M_OVER_TEMP, 0);
3198c2ecf20Sopenharmony_ci		if (ret < 0) {
3208c2ecf20Sopenharmony_ci			dev_err(chip->dev,
3218c2ecf20Sopenharmony_ci				"Failed to update mask reg: %d\n", ret);
3228c2ecf20Sopenharmony_ci			return ret;
3238c2ecf20Sopenharmony_ci		}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	} else {
3268c2ecf20Sopenharmony_ci		dev_warn(chip->dev, "No IRQ configured\n");
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	config.dev = chip->dev;
3308c2ecf20Sopenharmony_ci	config.regmap = chip->regmap;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
3338c2ecf20Sopenharmony_ci		if (init_data)
3348c2ecf20Sopenharmony_ci			config.init_data = &init_data[i];
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci		if (i == PV88090_ID_BUCK2 || i == PV88090_ID_BUCK3) {
3378c2ecf20Sopenharmony_ci			ret = regmap_read(chip->regmap,
3388c2ecf20Sopenharmony_ci				pv88090_regulator_info[i].conf2, &conf2);
3398c2ecf20Sopenharmony_ci			if (ret < 0)
3408c2ecf20Sopenharmony_ci				return ret;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci			conf2 = (conf2 >> PV88090_BUCK_VDAC_RANGE_SHIFT) &
3438c2ecf20Sopenharmony_ci				PV88090_BUCK_VDAC_RANGE_MASK;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci			ret = regmap_read(chip->regmap,
3468c2ecf20Sopenharmony_ci				PV88090_REG_BUCK_FOLD_RANGE, &range);
3478c2ecf20Sopenharmony_ci			if (ret < 0)
3488c2ecf20Sopenharmony_ci				return ret;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci			range = (range >>
3518c2ecf20Sopenharmony_ci				 (PV88090_BUCK_VRANGE_GAIN_SHIFT + i - 1)) &
3528c2ecf20Sopenharmony_ci				PV88090_BUCK_VRANGE_GAIN_MASK;
3538c2ecf20Sopenharmony_ci			index = ((range << 1) | conf2);
3548c2ecf20Sopenharmony_ci			if (index > PV88090_ID_BUCK3) {
3558c2ecf20Sopenharmony_ci				dev_err(chip->dev,
3568c2ecf20Sopenharmony_ci					"Invalid index(%d)\n", index);
3578c2ecf20Sopenharmony_ci				return -EINVAL;
3588c2ecf20Sopenharmony_ci			}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci			pv88090_regulator_info[i].desc.min_uV
3618c2ecf20Sopenharmony_ci				= pv88090_buck_vol[index].min_uV;
3628c2ecf20Sopenharmony_ci			pv88090_regulator_info[i].desc.uV_step
3638c2ecf20Sopenharmony_ci				= pv88090_buck_vol[index].uV_step;
3648c2ecf20Sopenharmony_ci			pv88090_regulator_info[i].desc.n_voltages
3658c2ecf20Sopenharmony_ci				= ((pv88090_buck_vol[index].max_uV)
3668c2ecf20Sopenharmony_ci				- (pv88090_buck_vol[index].min_uV))
3678c2ecf20Sopenharmony_ci				/(pv88090_buck_vol[index].uV_step) + 1;
3688c2ecf20Sopenharmony_ci		}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci		config.driver_data = (void *)&pv88090_regulator_info[i];
3718c2ecf20Sopenharmony_ci		chip->rdev[i] = devm_regulator_register(chip->dev,
3728c2ecf20Sopenharmony_ci			&pv88090_regulator_info[i].desc, &config);
3738c2ecf20Sopenharmony_ci		if (IS_ERR(chip->rdev[i])) {
3748c2ecf20Sopenharmony_ci			dev_err(chip->dev,
3758c2ecf20Sopenharmony_ci				"Failed to register PV88090 regulator\n");
3768c2ecf20Sopenharmony_ci			return PTR_ERR(chip->rdev[i]);
3778c2ecf20Sopenharmony_ci		}
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return 0;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic const struct i2c_device_id pv88090_i2c_id[] = {
3848c2ecf20Sopenharmony_ci	{"pv88090", 0},
3858c2ecf20Sopenharmony_ci	{},
3868c2ecf20Sopenharmony_ci};
3878c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, pv88090_i2c_id);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
3908c2ecf20Sopenharmony_cistatic const struct of_device_id pv88090_dt_ids[] = {
3918c2ecf20Sopenharmony_ci	{ .compatible = "pvs,pv88090", .data = &pv88090_i2c_id[0] },
3928c2ecf20Sopenharmony_ci	{},
3938c2ecf20Sopenharmony_ci};
3948c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, pv88090_dt_ids);
3958c2ecf20Sopenharmony_ci#endif
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic struct i2c_driver pv88090_regulator_driver = {
3988c2ecf20Sopenharmony_ci	.driver = {
3998c2ecf20Sopenharmony_ci		.name = "pv88090",
4008c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(pv88090_dt_ids),
4018c2ecf20Sopenharmony_ci	},
4028c2ecf20Sopenharmony_ci	.probe_new = pv88090_i2c_probe,
4038c2ecf20Sopenharmony_ci	.id_table = pv88090_i2c_id,
4048c2ecf20Sopenharmony_ci};
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cimodule_i2c_driver(pv88090_regulator_driver);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ciMODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
4098c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
4108c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
411