18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Regulator driver for DA9063 PMIC series
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright 2012 Dialog Semiconductors Ltd.
68c2ecf20Sopenharmony_ci// Copyright 2013 Philipp Zabel, Pengutronix
78c2ecf20Sopenharmony_ci//
88c2ecf20Sopenharmony_ci// Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/err.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
198c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
208c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
218c2ecf20Sopenharmony_ci#include <linux/mfd/da9063/core.h>
228c2ecf20Sopenharmony_ci#include <linux/mfd/da9063/registers.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Definition for registering regmap bit fields using a mask */
268c2ecf20Sopenharmony_ci#define BFIELD(_reg, _mask) \
278c2ecf20Sopenharmony_ci	REG_FIELD(_reg, __builtin_ffs((int)_mask) - 1, \
288c2ecf20Sopenharmony_ci		sizeof(unsigned int) * 8 - __builtin_clz((_mask)) - 1)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* DA9063 and DA9063L regulator IDs */
318c2ecf20Sopenharmony_cienum {
328c2ecf20Sopenharmony_ci	/* BUCKs */
338c2ecf20Sopenharmony_ci	DA9063_ID_BCORE1,
348c2ecf20Sopenharmony_ci	DA9063_ID_BCORE2,
358c2ecf20Sopenharmony_ci	DA9063_ID_BPRO,
368c2ecf20Sopenharmony_ci	DA9063_ID_BMEM,
378c2ecf20Sopenharmony_ci	DA9063_ID_BIO,
388c2ecf20Sopenharmony_ci	DA9063_ID_BPERI,
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/* BCORE1 and BCORE2 in merged mode */
418c2ecf20Sopenharmony_ci	DA9063_ID_BCORES_MERGED,
428c2ecf20Sopenharmony_ci	/* BMEM and BIO in merged mode */
438c2ecf20Sopenharmony_ci	DA9063_ID_BMEM_BIO_MERGED,
448c2ecf20Sopenharmony_ci	/* When two BUCKs are merged, they cannot be reused separately */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* LDOs on both DA9063 and DA9063L */
478c2ecf20Sopenharmony_ci	DA9063_ID_LDO3,
488c2ecf20Sopenharmony_ci	DA9063_ID_LDO7,
498c2ecf20Sopenharmony_ci	DA9063_ID_LDO8,
508c2ecf20Sopenharmony_ci	DA9063_ID_LDO9,
518c2ecf20Sopenharmony_ci	DA9063_ID_LDO11,
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	/* DA9063-only LDOs */
548c2ecf20Sopenharmony_ci	DA9063_ID_LDO1,
558c2ecf20Sopenharmony_ci	DA9063_ID_LDO2,
568c2ecf20Sopenharmony_ci	DA9063_ID_LDO4,
578c2ecf20Sopenharmony_ci	DA9063_ID_LDO5,
588c2ecf20Sopenharmony_ci	DA9063_ID_LDO6,
598c2ecf20Sopenharmony_ci	DA9063_ID_LDO10,
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* Old regulator platform data */
638c2ecf20Sopenharmony_cistruct da9063_regulator_data {
648c2ecf20Sopenharmony_ci	int				id;
658c2ecf20Sopenharmony_ci	struct regulator_init_data	*initdata;
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistruct da9063_regulators_pdata {
698c2ecf20Sopenharmony_ci	unsigned int			n_regulators;
708c2ecf20Sopenharmony_ci	struct da9063_regulator_data	*regulator_data;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/* Regulator capabilities and registers description */
748c2ecf20Sopenharmony_cistruct da9063_regulator_info {
758c2ecf20Sopenharmony_ci	struct regulator_desc desc;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* DA9063 main register fields */
788c2ecf20Sopenharmony_ci	struct reg_field mode;		/* buck mode of operation */
798c2ecf20Sopenharmony_ci	struct reg_field suspend;
808c2ecf20Sopenharmony_ci	struct reg_field sleep;
818c2ecf20Sopenharmony_ci	struct reg_field suspend_sleep;
828c2ecf20Sopenharmony_ci	unsigned int suspend_vsel_reg;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* DA9063 event detection bit */
858c2ecf20Sopenharmony_ci	struct reg_field oc_event;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* Macros for LDO */
898c2ecf20Sopenharmony_ci#define DA9063_LDO(chip, regl_name, min_mV, step_mV, max_mV) \
908c2ecf20Sopenharmony_ci	.desc.id = chip##_ID_##regl_name, \
918c2ecf20Sopenharmony_ci	.desc.name = __stringify(chip##_##regl_name), \
928c2ecf20Sopenharmony_ci	.desc.ops = &da9063_ldo_ops, \
938c2ecf20Sopenharmony_ci	.desc.min_uV = (min_mV) * 1000, \
948c2ecf20Sopenharmony_ci	.desc.uV_step = (step_mV) * 1000, \
958c2ecf20Sopenharmony_ci	.desc.n_voltages = (((max_mV) - (min_mV))/(step_mV) + 1 \
968c2ecf20Sopenharmony_ci		+ (DA9063_V##regl_name##_BIAS)), \
978c2ecf20Sopenharmony_ci	.desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
988c2ecf20Sopenharmony_ci	.desc.enable_mask = DA9063_LDO_EN, \
998c2ecf20Sopenharmony_ci	.desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
1008c2ecf20Sopenharmony_ci	.desc.vsel_mask = DA9063_V##regl_name##_MASK, \
1018c2ecf20Sopenharmony_ci	.desc.linear_min_sel = DA9063_V##regl_name##_BIAS, \
1028c2ecf20Sopenharmony_ci	.sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_LDO_SL), \
1038c2ecf20Sopenharmony_ci	.suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_LDO_CONF), \
1048c2ecf20Sopenharmony_ci	.suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_LDO_SL), \
1058c2ecf20Sopenharmony_ci	.suspend_vsel_reg = DA9063_REG_V##regl_name##_B
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* Macros for voltage DC/DC converters (BUCKs) */
1088c2ecf20Sopenharmony_ci#define DA9063_BUCK(chip, regl_name, min_mV, step_mV, max_mV, limits_array, \
1098c2ecf20Sopenharmony_ci		    creg, cmask) \
1108c2ecf20Sopenharmony_ci	.desc.id = chip##_ID_##regl_name, \
1118c2ecf20Sopenharmony_ci	.desc.name = __stringify(chip##_##regl_name), \
1128c2ecf20Sopenharmony_ci	.desc.ops = &da9063_buck_ops, \
1138c2ecf20Sopenharmony_ci	.desc.min_uV = (min_mV) * 1000, \
1148c2ecf20Sopenharmony_ci	.desc.uV_step = (step_mV) * 1000, \
1158c2ecf20Sopenharmony_ci	.desc.n_voltages = ((max_mV) - (min_mV))/(step_mV) + 1, \
1168c2ecf20Sopenharmony_ci	.desc.csel_reg = (creg), \
1178c2ecf20Sopenharmony_ci	.desc.csel_mask = (cmask), \
1188c2ecf20Sopenharmony_ci	.desc.curr_table = limits_array, \
1198c2ecf20Sopenharmony_ci	.desc.n_current_limits = ARRAY_SIZE(limits_array)
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define DA9063_BUCK_COMMON_FIELDS(regl_name) \
1228c2ecf20Sopenharmony_ci	.desc.enable_reg = DA9063_REG_##regl_name##_CONT, \
1238c2ecf20Sopenharmony_ci	.desc.enable_mask = DA9063_BUCK_EN, \
1248c2ecf20Sopenharmony_ci	.desc.vsel_reg = DA9063_REG_V##regl_name##_A, \
1258c2ecf20Sopenharmony_ci	.desc.vsel_mask = DA9063_VBUCK_MASK, \
1268c2ecf20Sopenharmony_ci	.desc.linear_min_sel = DA9063_VBUCK_BIAS, \
1278c2ecf20Sopenharmony_ci	.sleep = BFIELD(DA9063_REG_V##regl_name##_A, DA9063_BUCK_SL), \
1288c2ecf20Sopenharmony_ci	.suspend = BFIELD(DA9063_REG_##regl_name##_CONT, DA9063_BUCK_CONF), \
1298c2ecf20Sopenharmony_ci	.suspend_sleep = BFIELD(DA9063_REG_V##regl_name##_B, DA9063_BUCK_SL), \
1308c2ecf20Sopenharmony_ci	.suspend_vsel_reg = DA9063_REG_V##regl_name##_B, \
1318c2ecf20Sopenharmony_ci	.mode = BFIELD(DA9063_REG_##regl_name##_CFG, DA9063_BUCK_MODE_MASK)
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/* Defines asignment of regulators info table to chip model */
1348c2ecf20Sopenharmony_cistruct da9063_dev_model {
1358c2ecf20Sopenharmony_ci	const struct da9063_regulator_info	*regulator_info;
1368c2ecf20Sopenharmony_ci	unsigned int				n_regulators;
1378c2ecf20Sopenharmony_ci	enum da9063_type			type;
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci/* Single regulator settings */
1418c2ecf20Sopenharmony_cistruct da9063_regulator {
1428c2ecf20Sopenharmony_ci	struct regulator_desc			desc;
1438c2ecf20Sopenharmony_ci	struct regulator_dev			*rdev;
1448c2ecf20Sopenharmony_ci	struct da9063				*hw;
1458c2ecf20Sopenharmony_ci	const struct da9063_regulator_info	*info;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	struct regmap_field			*mode;
1488c2ecf20Sopenharmony_ci	struct regmap_field			*suspend;
1498c2ecf20Sopenharmony_ci	struct regmap_field			*sleep;
1508c2ecf20Sopenharmony_ci	struct regmap_field			*suspend_sleep;
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/* Encapsulates all information for the regulators driver */
1548c2ecf20Sopenharmony_cistruct da9063_regulators {
1558c2ecf20Sopenharmony_ci	unsigned int				n_regulators;
1568c2ecf20Sopenharmony_ci	/* Array size to be defined during init. Keep at end. */
1578c2ecf20Sopenharmony_ci	struct da9063_regulator			regulator[];
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/* BUCK modes for DA9063 */
1618c2ecf20Sopenharmony_cienum {
1628c2ecf20Sopenharmony_ci	BUCK_MODE_MANUAL,	/* 0 */
1638c2ecf20Sopenharmony_ci	BUCK_MODE_SLEEP,	/* 1 */
1648c2ecf20Sopenharmony_ci	BUCK_MODE_SYNC,		/* 2 */
1658c2ecf20Sopenharmony_ci	BUCK_MODE_AUTO		/* 3 */
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* Regulator operations */
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci/*
1718c2ecf20Sopenharmony_ci * Current limits array (in uA) for BCORE1, BCORE2, BPRO.
1728c2ecf20Sopenharmony_ci * Entry indexes corresponds to register values.
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_cistatic const unsigned int da9063_buck_a_limits[] = {
1758c2ecf20Sopenharmony_ci	 500000,  600000,  700000,  800000,  900000, 1000000, 1100000, 1200000,
1768c2ecf20Sopenharmony_ci	1300000, 1400000, 1500000, 1600000, 1700000, 1800000, 1900000, 2000000
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/*
1808c2ecf20Sopenharmony_ci * Current limits array (in uA) for BMEM, BIO, BPERI.
1818c2ecf20Sopenharmony_ci * Entry indexes corresponds to register values.
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_cistatic const unsigned int da9063_buck_b_limits[] = {
1848c2ecf20Sopenharmony_ci	1500000, 1600000, 1700000, 1800000, 1900000, 2000000, 2100000, 2200000,
1858c2ecf20Sopenharmony_ci	2300000, 2400000, 2500000, 2600000, 2700000, 2800000, 2900000, 3000000
1868c2ecf20Sopenharmony_ci};
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/*
1898c2ecf20Sopenharmony_ci * Current limits array (in uA) for merged BCORE1 and BCORE2.
1908c2ecf20Sopenharmony_ci * Entry indexes corresponds to register values.
1918c2ecf20Sopenharmony_ci */
1928c2ecf20Sopenharmony_cistatic const unsigned int da9063_bcores_merged_limits[] = {
1938c2ecf20Sopenharmony_ci	1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2200000, 2400000,
1948c2ecf20Sopenharmony_ci	2600000, 2800000, 3000000, 3200000, 3400000, 3600000, 3800000, 4000000
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/*
1988c2ecf20Sopenharmony_ci * Current limits array (in uA) for merged BMEM and BIO.
1998c2ecf20Sopenharmony_ci * Entry indexes corresponds to register values.
2008c2ecf20Sopenharmony_ci */
2018c2ecf20Sopenharmony_cistatic const unsigned int da9063_bmem_bio_merged_limits[] = {
2028c2ecf20Sopenharmony_ci	3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
2038c2ecf20Sopenharmony_ci	4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int da9063_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
2098c2ecf20Sopenharmony_ci	unsigned int val;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	switch (mode) {
2128c2ecf20Sopenharmony_ci	case REGULATOR_MODE_FAST:
2138c2ecf20Sopenharmony_ci		val = BUCK_MODE_SYNC;
2148c2ecf20Sopenharmony_ci		break;
2158c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
2168c2ecf20Sopenharmony_ci		val = BUCK_MODE_AUTO;
2178c2ecf20Sopenharmony_ci		break;
2188c2ecf20Sopenharmony_ci	case REGULATOR_MODE_STANDBY:
2198c2ecf20Sopenharmony_ci		val = BUCK_MODE_SLEEP;
2208c2ecf20Sopenharmony_ci		break;
2218c2ecf20Sopenharmony_ci	default:
2228c2ecf20Sopenharmony_ci		return -EINVAL;
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return regmap_field_write(regl->mode, val);
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/*
2298c2ecf20Sopenharmony_ci * Bucks use single mode register field for normal operation
2308c2ecf20Sopenharmony_ci * and suspend state.
2318c2ecf20Sopenharmony_ci * There are 3 modes to map to: FAST, NORMAL, and STANDBY.
2328c2ecf20Sopenharmony_ci */
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic unsigned int da9063_buck_get_mode(struct regulator_dev *rdev)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
2378c2ecf20Sopenharmony_ci	unsigned int val;
2388c2ecf20Sopenharmony_ci	int ret;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	ret = regmap_field_read(regl->mode, &val);
2418c2ecf20Sopenharmony_ci	if (ret < 0)
2428c2ecf20Sopenharmony_ci		return ret;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	switch (val) {
2458c2ecf20Sopenharmony_ci	default:
2468c2ecf20Sopenharmony_ci	case BUCK_MODE_MANUAL:
2478c2ecf20Sopenharmony_ci		/* Sleep flag bit decides the mode */
2488c2ecf20Sopenharmony_ci		break;
2498c2ecf20Sopenharmony_ci	case BUCK_MODE_SLEEP:
2508c2ecf20Sopenharmony_ci		return REGULATOR_MODE_STANDBY;
2518c2ecf20Sopenharmony_ci	case BUCK_MODE_SYNC:
2528c2ecf20Sopenharmony_ci		return REGULATOR_MODE_FAST;
2538c2ecf20Sopenharmony_ci	case BUCK_MODE_AUTO:
2548c2ecf20Sopenharmony_ci		return REGULATOR_MODE_NORMAL;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	ret = regmap_field_read(regl->sleep, &val);
2588c2ecf20Sopenharmony_ci	if (ret < 0)
2598c2ecf20Sopenharmony_ci		return 0;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if (val)
2628c2ecf20Sopenharmony_ci		return REGULATOR_MODE_STANDBY;
2638c2ecf20Sopenharmony_ci	else
2648c2ecf20Sopenharmony_ci		return REGULATOR_MODE_FAST;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/*
2688c2ecf20Sopenharmony_ci * LDOs use sleep flags - one for normal and one for suspend state.
2698c2ecf20Sopenharmony_ci * There are 2 modes to map to: NORMAL and STANDBY (sleep) for each state.
2708c2ecf20Sopenharmony_ci */
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic int da9063_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
2758c2ecf20Sopenharmony_ci	unsigned int val;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	switch (mode) {
2788c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
2798c2ecf20Sopenharmony_ci		val = 0;
2808c2ecf20Sopenharmony_ci		break;
2818c2ecf20Sopenharmony_ci	case REGULATOR_MODE_STANDBY:
2828c2ecf20Sopenharmony_ci		val = 1;
2838c2ecf20Sopenharmony_ci		break;
2848c2ecf20Sopenharmony_ci	default:
2858c2ecf20Sopenharmony_ci		return -EINVAL;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	return regmap_field_write(regl->sleep, val);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic unsigned int da9063_ldo_get_mode(struct regulator_dev *rdev)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
2948c2ecf20Sopenharmony_ci	int ret, val;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	ret = regmap_field_read(regl->sleep, &val);
2978c2ecf20Sopenharmony_ci	if (ret < 0)
2988c2ecf20Sopenharmony_ci		return 0;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	if (val)
3018c2ecf20Sopenharmony_ci		return REGULATOR_MODE_STANDBY;
3028c2ecf20Sopenharmony_ci	else
3038c2ecf20Sopenharmony_ci		return REGULATOR_MODE_NORMAL;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic int da9063_buck_get_status(struct regulator_dev *rdev)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	int ret = regulator_is_enabled_regmap(rdev);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (ret == 0) {
3118c2ecf20Sopenharmony_ci		ret = REGULATOR_STATUS_OFF;
3128c2ecf20Sopenharmony_ci	} else if (ret > 0) {
3138c2ecf20Sopenharmony_ci		ret = da9063_buck_get_mode(rdev);
3148c2ecf20Sopenharmony_ci		if (ret > 0)
3158c2ecf20Sopenharmony_ci			ret = regulator_mode_to_status(ret);
3168c2ecf20Sopenharmony_ci		else if (ret == 0)
3178c2ecf20Sopenharmony_ci			ret = -EIO;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return ret;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic int da9063_ldo_get_status(struct regulator_dev *rdev)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	int ret = regulator_is_enabled_regmap(rdev);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (ret == 0) {
3288c2ecf20Sopenharmony_ci		ret = REGULATOR_STATUS_OFF;
3298c2ecf20Sopenharmony_ci	} else if (ret > 0) {
3308c2ecf20Sopenharmony_ci		ret = da9063_ldo_get_mode(rdev);
3318c2ecf20Sopenharmony_ci		if (ret > 0)
3328c2ecf20Sopenharmony_ci			ret = regulator_mode_to_status(ret);
3338c2ecf20Sopenharmony_ci		else if (ret == 0)
3348c2ecf20Sopenharmony_ci			ret = -EIO;
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	return ret;
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic int da9063_set_suspend_voltage(struct regulator_dev *rdev, int uV)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
3438c2ecf20Sopenharmony_ci	const struct da9063_regulator_info *rinfo = regl->info;
3448c2ecf20Sopenharmony_ci	int ret, sel;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	sel = regulator_map_voltage_linear(rdev, uV, uV);
3478c2ecf20Sopenharmony_ci	if (sel < 0)
3488c2ecf20Sopenharmony_ci		return sel;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	sel <<= ffs(rdev->desc->vsel_mask) - 1;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	ret = regmap_update_bits(regl->hw->regmap, rinfo->suspend_vsel_reg,
3538c2ecf20Sopenharmony_ci				 rdev->desc->vsel_mask, sel);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	return ret;
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic int da9063_suspend_enable(struct regulator_dev *rdev)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	return regmap_field_write(regl->suspend, 1);
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic int da9063_suspend_disable(struct regulator_dev *rdev)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	return regmap_field_write(regl->suspend, 0);
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic int da9063_buck_set_suspend_mode(struct regulator_dev *rdev,
3738c2ecf20Sopenharmony_ci				unsigned int mode)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
3768c2ecf20Sopenharmony_ci	int val;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	switch (mode) {
3798c2ecf20Sopenharmony_ci	case REGULATOR_MODE_FAST:
3808c2ecf20Sopenharmony_ci		val = BUCK_MODE_SYNC;
3818c2ecf20Sopenharmony_ci		break;
3828c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
3838c2ecf20Sopenharmony_ci		val = BUCK_MODE_AUTO;
3848c2ecf20Sopenharmony_ci		break;
3858c2ecf20Sopenharmony_ci	case REGULATOR_MODE_STANDBY:
3868c2ecf20Sopenharmony_ci		val = BUCK_MODE_SLEEP;
3878c2ecf20Sopenharmony_ci		break;
3888c2ecf20Sopenharmony_ci	default:
3898c2ecf20Sopenharmony_ci		return -EINVAL;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return regmap_field_write(regl->mode, val);
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic int da9063_ldo_set_suspend_mode(struct regulator_dev *rdev,
3968c2ecf20Sopenharmony_ci				unsigned int mode)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	struct da9063_regulator *regl = rdev_get_drvdata(rdev);
3998c2ecf20Sopenharmony_ci	unsigned int val;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	switch (mode) {
4028c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
4038c2ecf20Sopenharmony_ci		val = 0;
4048c2ecf20Sopenharmony_ci		break;
4058c2ecf20Sopenharmony_ci	case REGULATOR_MODE_STANDBY:
4068c2ecf20Sopenharmony_ci		val = 1;
4078c2ecf20Sopenharmony_ci		break;
4088c2ecf20Sopenharmony_ci	default:
4098c2ecf20Sopenharmony_ci		return -EINVAL;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	return regmap_field_write(regl->suspend_sleep, val);
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_cistatic const struct regulator_ops da9063_buck_ops = {
4168c2ecf20Sopenharmony_ci	.enable			= regulator_enable_regmap,
4178c2ecf20Sopenharmony_ci	.disable		= regulator_disable_regmap,
4188c2ecf20Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
4198c2ecf20Sopenharmony_ci	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
4208c2ecf20Sopenharmony_ci	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
4218c2ecf20Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
4228c2ecf20Sopenharmony_ci	.set_current_limit	= regulator_set_current_limit_regmap,
4238c2ecf20Sopenharmony_ci	.get_current_limit	= regulator_get_current_limit_regmap,
4248c2ecf20Sopenharmony_ci	.set_mode		= da9063_buck_set_mode,
4258c2ecf20Sopenharmony_ci	.get_mode		= da9063_buck_get_mode,
4268c2ecf20Sopenharmony_ci	.get_status		= da9063_buck_get_status,
4278c2ecf20Sopenharmony_ci	.set_suspend_voltage	= da9063_set_suspend_voltage,
4288c2ecf20Sopenharmony_ci	.set_suspend_enable	= da9063_suspend_enable,
4298c2ecf20Sopenharmony_ci	.set_suspend_disable	= da9063_suspend_disable,
4308c2ecf20Sopenharmony_ci	.set_suspend_mode	= da9063_buck_set_suspend_mode,
4318c2ecf20Sopenharmony_ci};
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic const struct regulator_ops da9063_ldo_ops = {
4348c2ecf20Sopenharmony_ci	.enable			= regulator_enable_regmap,
4358c2ecf20Sopenharmony_ci	.disable		= regulator_disable_regmap,
4368c2ecf20Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
4378c2ecf20Sopenharmony_ci	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
4388c2ecf20Sopenharmony_ci	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
4398c2ecf20Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
4408c2ecf20Sopenharmony_ci	.set_mode		= da9063_ldo_set_mode,
4418c2ecf20Sopenharmony_ci	.get_mode		= da9063_ldo_get_mode,
4428c2ecf20Sopenharmony_ci	.get_status		= da9063_ldo_get_status,
4438c2ecf20Sopenharmony_ci	.set_suspend_voltage	= da9063_set_suspend_voltage,
4448c2ecf20Sopenharmony_ci	.set_suspend_enable	= da9063_suspend_enable,
4458c2ecf20Sopenharmony_ci	.set_suspend_disable	= da9063_suspend_disable,
4468c2ecf20Sopenharmony_ci	.set_suspend_mode	= da9063_ldo_set_suspend_mode,
4478c2ecf20Sopenharmony_ci};
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci/* Info of regulators for DA9063 */
4508c2ecf20Sopenharmony_cistatic const struct da9063_regulator_info da9063_regulator_info[] = {
4518c2ecf20Sopenharmony_ci	{
4528c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BCORE1, 300, 10, 1570,
4538c2ecf20Sopenharmony_ci			    da9063_buck_a_limits,
4548c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
4558c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BCORE1),
4568c2ecf20Sopenharmony_ci	},
4578c2ecf20Sopenharmony_ci	{
4588c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BCORE2, 300, 10, 1570,
4598c2ecf20Sopenharmony_ci			    da9063_buck_a_limits,
4608c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE2_ILIM_MASK),
4618c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BCORE2),
4628c2ecf20Sopenharmony_ci	},
4638c2ecf20Sopenharmony_ci	{
4648c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BPRO, 530, 10, 1800,
4658c2ecf20Sopenharmony_ci			    da9063_buck_a_limits,
4668c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_B, DA9063_BPRO_ILIM_MASK),
4678c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BPRO),
4688c2ecf20Sopenharmony_ci	},
4698c2ecf20Sopenharmony_ci	{
4708c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BMEM, 800, 20, 3340,
4718c2ecf20Sopenharmony_ci			    da9063_buck_b_limits,
4728c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
4738c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BMEM),
4748c2ecf20Sopenharmony_ci	},
4758c2ecf20Sopenharmony_ci	{
4768c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BIO, 800, 20, 3340,
4778c2ecf20Sopenharmony_ci			    da9063_buck_b_limits,
4788c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_A, DA9063_BIO_ILIM_MASK),
4798c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BIO),
4808c2ecf20Sopenharmony_ci	},
4818c2ecf20Sopenharmony_ci	{
4828c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BPERI, 800, 20, 3340,
4838c2ecf20Sopenharmony_ci			    da9063_buck_b_limits,
4848c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_B, DA9063_BPERI_ILIM_MASK),
4858c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BPERI),
4868c2ecf20Sopenharmony_ci	},
4878c2ecf20Sopenharmony_ci	{
4888c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BCORES_MERGED, 300, 10, 1570,
4898c2ecf20Sopenharmony_ci			    da9063_bcores_merged_limits,
4908c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_C, DA9063_BCORE1_ILIM_MASK),
4918c2ecf20Sopenharmony_ci		/* BCORES_MERGED uses the same register fields as BCORE1 */
4928c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BCORE1),
4938c2ecf20Sopenharmony_ci	},
4948c2ecf20Sopenharmony_ci	{
4958c2ecf20Sopenharmony_ci		DA9063_BUCK(DA9063, BMEM_BIO_MERGED, 800, 20, 3340,
4968c2ecf20Sopenharmony_ci			    da9063_bmem_bio_merged_limits,
4978c2ecf20Sopenharmony_ci			    DA9063_REG_BUCK_ILIM_A, DA9063_BMEM_ILIM_MASK),
4988c2ecf20Sopenharmony_ci		/* BMEM_BIO_MERGED uses the same register fields as BMEM */
4998c2ecf20Sopenharmony_ci		DA9063_BUCK_COMMON_FIELDS(BMEM),
5008c2ecf20Sopenharmony_ci	},
5018c2ecf20Sopenharmony_ci	{
5028c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO3, 900, 20, 3440),
5038c2ecf20Sopenharmony_ci		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO3_LIM),
5048c2ecf20Sopenharmony_ci	},
5058c2ecf20Sopenharmony_ci	{
5068c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO7, 900, 50, 3600),
5078c2ecf20Sopenharmony_ci		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO7_LIM),
5088c2ecf20Sopenharmony_ci	},
5098c2ecf20Sopenharmony_ci	{
5108c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO8, 900, 50, 3600),
5118c2ecf20Sopenharmony_ci		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO8_LIM),
5128c2ecf20Sopenharmony_ci	},
5138c2ecf20Sopenharmony_ci	{
5148c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO9, 950, 50, 3600),
5158c2ecf20Sopenharmony_ci	},
5168c2ecf20Sopenharmony_ci	{
5178c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO11, 900, 50, 3600),
5188c2ecf20Sopenharmony_ci		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO11_LIM),
5198c2ecf20Sopenharmony_ci	},
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* The following LDOs are present only on DA9063, not on DA9063L */
5228c2ecf20Sopenharmony_ci	{
5238c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO1, 600, 20, 1860),
5248c2ecf20Sopenharmony_ci	},
5258c2ecf20Sopenharmony_ci	{
5268c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO2, 600, 20, 1860),
5278c2ecf20Sopenharmony_ci	},
5288c2ecf20Sopenharmony_ci	{
5298c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO4, 900, 20, 3440),
5308c2ecf20Sopenharmony_ci		.oc_event = BFIELD(DA9063_REG_STATUS_D, DA9063_LDO4_LIM),
5318c2ecf20Sopenharmony_ci	},
5328c2ecf20Sopenharmony_ci	{
5338c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO5, 900, 50, 3600),
5348c2ecf20Sopenharmony_ci	},
5358c2ecf20Sopenharmony_ci	{
5368c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO6, 900, 50, 3600),
5378c2ecf20Sopenharmony_ci	},
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	{
5408c2ecf20Sopenharmony_ci		DA9063_LDO(DA9063, LDO10, 900, 50, 3600),
5418c2ecf20Sopenharmony_ci	},
5428c2ecf20Sopenharmony_ci};
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci/* Link chip model with regulators info table */
5458c2ecf20Sopenharmony_cistatic struct da9063_dev_model regulators_models[] = {
5468c2ecf20Sopenharmony_ci	{
5478c2ecf20Sopenharmony_ci		.regulator_info = da9063_regulator_info,
5488c2ecf20Sopenharmony_ci		.n_regulators = ARRAY_SIZE(da9063_regulator_info),
5498c2ecf20Sopenharmony_ci		.type = PMIC_TYPE_DA9063,
5508c2ecf20Sopenharmony_ci	},
5518c2ecf20Sopenharmony_ci	{
5528c2ecf20Sopenharmony_ci		.regulator_info = da9063_regulator_info,
5538c2ecf20Sopenharmony_ci		.n_regulators = ARRAY_SIZE(da9063_regulator_info) - 6,
5548c2ecf20Sopenharmony_ci		.type = PMIC_TYPE_DA9063L,
5558c2ecf20Sopenharmony_ci	},
5568c2ecf20Sopenharmony_ci	{ }
5578c2ecf20Sopenharmony_ci};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci/* Regulator interrupt handlers */
5608c2ecf20Sopenharmony_cistatic irqreturn_t da9063_ldo_lim_event(int irq, void *data)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	struct da9063_regulators *regulators = data;
5638c2ecf20Sopenharmony_ci	struct da9063 *hw = regulators->regulator[0].hw;
5648c2ecf20Sopenharmony_ci	struct da9063_regulator *regl;
5658c2ecf20Sopenharmony_ci	int bits, i, ret;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	ret = regmap_read(hw->regmap, DA9063_REG_STATUS_D, &bits);
5688c2ecf20Sopenharmony_ci	if (ret < 0)
5698c2ecf20Sopenharmony_ci		return IRQ_NONE;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	for (i = regulators->n_regulators - 1; i >= 0; i--) {
5728c2ecf20Sopenharmony_ci		regl = &regulators->regulator[i];
5738c2ecf20Sopenharmony_ci		if (regl->info->oc_event.reg != DA9063_REG_STATUS_D)
5748c2ecf20Sopenharmony_ci			continue;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci		if (BIT(regl->info->oc_event.lsb) & bits) {
5778c2ecf20Sopenharmony_ci			regulator_notifier_call_chain(regl->rdev,
5788c2ecf20Sopenharmony_ci					REGULATOR_EVENT_OVER_CURRENT, NULL);
5798c2ecf20Sopenharmony_ci		}
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci/*
5868c2ecf20Sopenharmony_ci * Probing and Initialisation functions
5878c2ecf20Sopenharmony_ci */
5888c2ecf20Sopenharmony_cistatic const struct regulator_init_data *da9063_get_regulator_initdata(
5898c2ecf20Sopenharmony_ci		const struct da9063_regulators_pdata *regl_pdata, int id)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	int i;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	for (i = 0; i < regl_pdata->n_regulators; i++) {
5948c2ecf20Sopenharmony_ci		if (id == regl_pdata->regulator_data[i].id)
5958c2ecf20Sopenharmony_ci			return regl_pdata->regulator_data[i].initdata;
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return NULL;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cistatic struct of_regulator_match da9063_matches[] = {
6028c2ecf20Sopenharmony_ci	[DA9063_ID_BCORE1]           = { .name = "bcore1"           },
6038c2ecf20Sopenharmony_ci	[DA9063_ID_BCORE2]           = { .name = "bcore2"           },
6048c2ecf20Sopenharmony_ci	[DA9063_ID_BPRO]             = { .name = "bpro",            },
6058c2ecf20Sopenharmony_ci	[DA9063_ID_BMEM]             = { .name = "bmem",            },
6068c2ecf20Sopenharmony_ci	[DA9063_ID_BIO]              = { .name = "bio",             },
6078c2ecf20Sopenharmony_ci	[DA9063_ID_BPERI]            = { .name = "bperi",           },
6088c2ecf20Sopenharmony_ci	[DA9063_ID_BCORES_MERGED]    = { .name = "bcores-merged"    },
6098c2ecf20Sopenharmony_ci	[DA9063_ID_BMEM_BIO_MERGED]  = { .name = "bmem-bio-merged", },
6108c2ecf20Sopenharmony_ci	[DA9063_ID_LDO3]             = { .name = "ldo3",            },
6118c2ecf20Sopenharmony_ci	[DA9063_ID_LDO7]             = { .name = "ldo7",            },
6128c2ecf20Sopenharmony_ci	[DA9063_ID_LDO8]             = { .name = "ldo8",            },
6138c2ecf20Sopenharmony_ci	[DA9063_ID_LDO9]             = { .name = "ldo9",            },
6148c2ecf20Sopenharmony_ci	[DA9063_ID_LDO11]            = { .name = "ldo11",           },
6158c2ecf20Sopenharmony_ci	/* The following LDOs are present only on DA9063, not on DA9063L */
6168c2ecf20Sopenharmony_ci	[DA9063_ID_LDO1]             = { .name = "ldo1",            },
6178c2ecf20Sopenharmony_ci	[DA9063_ID_LDO2]             = { .name = "ldo2",            },
6188c2ecf20Sopenharmony_ci	[DA9063_ID_LDO4]             = { .name = "ldo4",            },
6198c2ecf20Sopenharmony_ci	[DA9063_ID_LDO5]             = { .name = "ldo5",            },
6208c2ecf20Sopenharmony_ci	[DA9063_ID_LDO6]             = { .name = "ldo6",            },
6218c2ecf20Sopenharmony_ci	[DA9063_ID_LDO10]            = { .name = "ldo10",           },
6228c2ecf20Sopenharmony_ci};
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic struct da9063_regulators_pdata *da9063_parse_regulators_dt(
6258c2ecf20Sopenharmony_ci		struct platform_device *pdev,
6268c2ecf20Sopenharmony_ci		struct of_regulator_match **da9063_reg_matches)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
6298c2ecf20Sopenharmony_ci	struct da9063_regulators_pdata *pdata;
6308c2ecf20Sopenharmony_ci	struct da9063_regulator_data *rdata;
6318c2ecf20Sopenharmony_ci	struct device_node *node;
6328c2ecf20Sopenharmony_ci	int da9063_matches_len = ARRAY_SIZE(da9063_matches);
6338c2ecf20Sopenharmony_ci	int i, n, num;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (da9063->type == PMIC_TYPE_DA9063L)
6368c2ecf20Sopenharmony_ci		da9063_matches_len -= 6;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	node = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
6398c2ecf20Sopenharmony_ci	if (!node) {
6408c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Regulators device node not found\n");
6418c2ecf20Sopenharmony_ci		return ERR_PTR(-ENODEV);
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	num = of_regulator_match(&pdev->dev, node, da9063_matches,
6458c2ecf20Sopenharmony_ci				 da9063_matches_len);
6468c2ecf20Sopenharmony_ci	of_node_put(node);
6478c2ecf20Sopenharmony_ci	if (num < 0) {
6488c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to match regulators\n");
6498c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
6508c2ecf20Sopenharmony_ci	}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
6538c2ecf20Sopenharmony_ci	if (!pdata)
6548c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	pdata->regulator_data = devm_kcalloc(&pdev->dev,
6578c2ecf20Sopenharmony_ci					num, sizeof(*pdata->regulator_data),
6588c2ecf20Sopenharmony_ci					GFP_KERNEL);
6598c2ecf20Sopenharmony_ci	if (!pdata->regulator_data)
6608c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
6618c2ecf20Sopenharmony_ci	pdata->n_regulators = num;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	n = 0;
6648c2ecf20Sopenharmony_ci	for (i = 0; i < da9063_matches_len; i++) {
6658c2ecf20Sopenharmony_ci		if (!da9063_matches[i].init_data)
6668c2ecf20Sopenharmony_ci			continue;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci		rdata = &pdata->regulator_data[n];
6698c2ecf20Sopenharmony_ci		rdata->id = i;
6708c2ecf20Sopenharmony_ci		rdata->initdata = da9063_matches[i].init_data;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		n++;
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	*da9063_reg_matches = da9063_matches;
6768c2ecf20Sopenharmony_ci	return pdata;
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic int da9063_regulator_probe(struct platform_device *pdev)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	struct da9063 *da9063 = dev_get_drvdata(pdev->dev.parent);
6828c2ecf20Sopenharmony_ci	struct of_regulator_match *da9063_reg_matches = NULL;
6838c2ecf20Sopenharmony_ci	struct da9063_regulators_pdata *regl_pdata;
6848c2ecf20Sopenharmony_ci	const struct da9063_dev_model *model;
6858c2ecf20Sopenharmony_ci	struct da9063_regulators *regulators;
6868c2ecf20Sopenharmony_ci	struct da9063_regulator *regl;
6878c2ecf20Sopenharmony_ci	struct regulator_config config;
6888c2ecf20Sopenharmony_ci	bool bcores_merged, bmem_bio_merged;
6898c2ecf20Sopenharmony_ci	int id, irq, n, n_regulators, ret, val;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	regl_pdata = da9063_parse_regulators_dt(pdev, &da9063_reg_matches);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	if (IS_ERR(regl_pdata) || regl_pdata->n_regulators == 0) {
6948c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
6958c2ecf20Sopenharmony_ci			"No regulators defined for the platform\n");
6968c2ecf20Sopenharmony_ci		return -ENODEV;
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	/* Find regulators set for particular device model */
7008c2ecf20Sopenharmony_ci	for (model = regulators_models; model->regulator_info; model++) {
7018c2ecf20Sopenharmony_ci		if (model->type == da9063->type)
7028c2ecf20Sopenharmony_ci			break;
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci	if (!model->regulator_info) {
7058c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Chip model not recognised (%u)\n",
7068c2ecf20Sopenharmony_ci			da9063->type);
7078c2ecf20Sopenharmony_ci		return -ENODEV;
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	ret = regmap_read(da9063->regmap, DA9063_REG_CONFIG_H, &val);
7118c2ecf20Sopenharmony_ci	if (ret < 0) {
7128c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
7138c2ecf20Sopenharmony_ci			"Error while reading BUCKs configuration\n");
7148c2ecf20Sopenharmony_ci		return ret;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci	bcores_merged = val & DA9063_BCORE_MERGE;
7178c2ecf20Sopenharmony_ci	bmem_bio_merged = val & DA9063_BUCK_MERGE;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	n_regulators = model->n_regulators;
7208c2ecf20Sopenharmony_ci	if (bcores_merged)
7218c2ecf20Sopenharmony_ci		n_regulators -= 2; /* remove BCORE1, BCORE2 */
7228c2ecf20Sopenharmony_ci	else
7238c2ecf20Sopenharmony_ci		n_regulators--;    /* remove BCORES_MERGED */
7248c2ecf20Sopenharmony_ci	if (bmem_bio_merged)
7258c2ecf20Sopenharmony_ci		n_regulators -= 2; /* remove BMEM, BIO */
7268c2ecf20Sopenharmony_ci	else
7278c2ecf20Sopenharmony_ci		n_regulators--;    /* remove BMEM_BIO_MERGED */
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	/* Allocate memory required by usable regulators */
7308c2ecf20Sopenharmony_ci	regulators = devm_kzalloc(&pdev->dev, struct_size(regulators,
7318c2ecf20Sopenharmony_ci				  regulator, n_regulators), GFP_KERNEL);
7328c2ecf20Sopenharmony_ci	if (!regulators)
7338c2ecf20Sopenharmony_ci		return -ENOMEM;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	regulators->n_regulators = n_regulators;
7368c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, regulators);
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	/* Register all regulators declared in platform information */
7398c2ecf20Sopenharmony_ci	n = 0;
7408c2ecf20Sopenharmony_ci	id = 0;
7418c2ecf20Sopenharmony_ci	while (n < regulators->n_regulators) {
7428c2ecf20Sopenharmony_ci		/* Skip regulator IDs depending on merge mode configuration */
7438c2ecf20Sopenharmony_ci		switch (id) {
7448c2ecf20Sopenharmony_ci		case DA9063_ID_BCORE1:
7458c2ecf20Sopenharmony_ci		case DA9063_ID_BCORE2:
7468c2ecf20Sopenharmony_ci			if (bcores_merged) {
7478c2ecf20Sopenharmony_ci				id++;
7488c2ecf20Sopenharmony_ci				continue;
7498c2ecf20Sopenharmony_ci			}
7508c2ecf20Sopenharmony_ci			break;
7518c2ecf20Sopenharmony_ci		case DA9063_ID_BMEM:
7528c2ecf20Sopenharmony_ci		case DA9063_ID_BIO:
7538c2ecf20Sopenharmony_ci			if (bmem_bio_merged) {
7548c2ecf20Sopenharmony_ci				id++;
7558c2ecf20Sopenharmony_ci				continue;
7568c2ecf20Sopenharmony_ci			}
7578c2ecf20Sopenharmony_ci			break;
7588c2ecf20Sopenharmony_ci		case DA9063_ID_BCORES_MERGED:
7598c2ecf20Sopenharmony_ci			if (!bcores_merged) {
7608c2ecf20Sopenharmony_ci				id++;
7618c2ecf20Sopenharmony_ci				continue;
7628c2ecf20Sopenharmony_ci			}
7638c2ecf20Sopenharmony_ci			break;
7648c2ecf20Sopenharmony_ci		case DA9063_ID_BMEM_BIO_MERGED:
7658c2ecf20Sopenharmony_ci			if (!bmem_bio_merged) {
7668c2ecf20Sopenharmony_ci				id++;
7678c2ecf20Sopenharmony_ci				continue;
7688c2ecf20Sopenharmony_ci			}
7698c2ecf20Sopenharmony_ci			break;
7708c2ecf20Sopenharmony_ci		}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci		/* Initialise regulator structure */
7738c2ecf20Sopenharmony_ci		regl = &regulators->regulator[n];
7748c2ecf20Sopenharmony_ci		regl->hw = da9063;
7758c2ecf20Sopenharmony_ci		regl->info = &model->regulator_info[id];
7768c2ecf20Sopenharmony_ci		regl->desc = regl->info->desc;
7778c2ecf20Sopenharmony_ci		regl->desc.type = REGULATOR_VOLTAGE;
7788c2ecf20Sopenharmony_ci		regl->desc.owner = THIS_MODULE;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci		if (regl->info->mode.reg) {
7818c2ecf20Sopenharmony_ci			regl->mode = devm_regmap_field_alloc(&pdev->dev,
7828c2ecf20Sopenharmony_ci					da9063->regmap, regl->info->mode);
7838c2ecf20Sopenharmony_ci			if (IS_ERR(regl->mode))
7848c2ecf20Sopenharmony_ci				return PTR_ERR(regl->mode);
7858c2ecf20Sopenharmony_ci		}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci		if (regl->info->suspend.reg) {
7888c2ecf20Sopenharmony_ci			regl->suspend = devm_regmap_field_alloc(&pdev->dev,
7898c2ecf20Sopenharmony_ci					da9063->regmap, regl->info->suspend);
7908c2ecf20Sopenharmony_ci			if (IS_ERR(regl->suspend))
7918c2ecf20Sopenharmony_ci				return PTR_ERR(regl->suspend);
7928c2ecf20Sopenharmony_ci		}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci		if (regl->info->sleep.reg) {
7958c2ecf20Sopenharmony_ci			regl->sleep = devm_regmap_field_alloc(&pdev->dev,
7968c2ecf20Sopenharmony_ci					da9063->regmap, regl->info->sleep);
7978c2ecf20Sopenharmony_ci			if (IS_ERR(regl->sleep))
7988c2ecf20Sopenharmony_ci				return PTR_ERR(regl->sleep);
7998c2ecf20Sopenharmony_ci		}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci		if (regl->info->suspend_sleep.reg) {
8028c2ecf20Sopenharmony_ci			regl->suspend_sleep = devm_regmap_field_alloc(&pdev->dev,
8038c2ecf20Sopenharmony_ci				da9063->regmap, regl->info->suspend_sleep);
8048c2ecf20Sopenharmony_ci			if (IS_ERR(regl->suspend_sleep))
8058c2ecf20Sopenharmony_ci				return PTR_ERR(regl->suspend_sleep);
8068c2ecf20Sopenharmony_ci		}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci		/* Register regulator */
8098c2ecf20Sopenharmony_ci		memset(&config, 0, sizeof(config));
8108c2ecf20Sopenharmony_ci		config.dev = &pdev->dev;
8118c2ecf20Sopenharmony_ci		config.init_data = da9063_get_regulator_initdata(regl_pdata, id);
8128c2ecf20Sopenharmony_ci		config.driver_data = regl;
8138c2ecf20Sopenharmony_ci		if (da9063_reg_matches)
8148c2ecf20Sopenharmony_ci			config.of_node = da9063_reg_matches[id].of_node;
8158c2ecf20Sopenharmony_ci		config.regmap = da9063->regmap;
8168c2ecf20Sopenharmony_ci		regl->rdev = devm_regulator_register(&pdev->dev, &regl->desc,
8178c2ecf20Sopenharmony_ci						     &config);
8188c2ecf20Sopenharmony_ci		if (IS_ERR(regl->rdev)) {
8198c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
8208c2ecf20Sopenharmony_ci				"Failed to register %s regulator\n",
8218c2ecf20Sopenharmony_ci				regl->desc.name);
8228c2ecf20Sopenharmony_ci			return PTR_ERR(regl->rdev);
8238c2ecf20Sopenharmony_ci		}
8248c2ecf20Sopenharmony_ci		id++;
8258c2ecf20Sopenharmony_ci		n++;
8268c2ecf20Sopenharmony_ci	}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	/* LDOs overcurrent event support */
8298c2ecf20Sopenharmony_ci	irq = platform_get_irq_byname(pdev, "LDO_LIM");
8308c2ecf20Sopenharmony_ci	if (irq < 0)
8318c2ecf20Sopenharmony_ci		return irq;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(&pdev->dev, irq,
8348c2ecf20Sopenharmony_ci				NULL, da9063_ldo_lim_event,
8358c2ecf20Sopenharmony_ci				IRQF_TRIGGER_LOW | IRQF_ONESHOT,
8368c2ecf20Sopenharmony_ci				"LDO_LIM", regulators);
8378c2ecf20Sopenharmony_ci	if (ret)
8388c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to request LDO_LIM IRQ.\n");
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	return ret;
8418c2ecf20Sopenharmony_ci}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_cistatic struct platform_driver da9063_regulator_driver = {
8448c2ecf20Sopenharmony_ci	.driver = {
8458c2ecf20Sopenharmony_ci		.name = DA9063_DRVNAME_REGULATORS,
8468c2ecf20Sopenharmony_ci	},
8478c2ecf20Sopenharmony_ci	.probe = da9063_regulator_probe,
8488c2ecf20Sopenharmony_ci};
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_cistatic int __init da9063_regulator_init(void)
8518c2ecf20Sopenharmony_ci{
8528c2ecf20Sopenharmony_ci	return platform_driver_register(&da9063_regulator_driver);
8538c2ecf20Sopenharmony_ci}
8548c2ecf20Sopenharmony_cisubsys_initcall(da9063_regulator_init);
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_cistatic void __exit da9063_regulator_cleanup(void)
8578c2ecf20Sopenharmony_ci{
8588c2ecf20Sopenharmony_ci	platform_driver_unregister(&da9063_regulator_driver);
8598c2ecf20Sopenharmony_ci}
8608c2ecf20Sopenharmony_cimodule_exit(da9063_regulator_cleanup);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci/* Module information */
8648c2ecf20Sopenharmony_ciMODULE_AUTHOR("Krystian Garbaciak <krystian.garbaciak@diasemi.com>");
8658c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DA9063 regulators driver");
8668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
8678c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DA9063_DRVNAME_REGULATORS);
868