18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * max8907-regulator.c -- support regulators in max8907
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2010-2012, NVIDIA CORPORATION. All rights reserved.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Portions based on drivers/regulator/tps65910-regulator.c,
98c2ecf20Sopenharmony_ci *     Copyright 2010 Texas Instruments Inc.
108c2ecf20Sopenharmony_ci *     Author: Graeme Gregory <gg@slimlogic.co.uk>
118c2ecf20Sopenharmony_ci *     Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/err.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/mfd/core.h>
178c2ecf20Sopenharmony_ci#include <linux/mfd/max8907.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/of.h>
208c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
218c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
228c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
238c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
248c2ecf20Sopenharmony_ci#include <linux/regmap.h>
258c2ecf20Sopenharmony_ci#include <linux/slab.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define MAX8907_II2RR_VERSION_MASK	0xF0
288c2ecf20Sopenharmony_ci#define MAX8907_II2RR_VERSION_REV_A	0x00
298c2ecf20Sopenharmony_ci#define MAX8907_II2RR_VERSION_REV_B	0x10
308c2ecf20Sopenharmony_ci#define MAX8907_II2RR_VERSION_REV_C	0x30
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct max8907_regulator {
338c2ecf20Sopenharmony_ci	struct regulator_desc desc[MAX8907_NUM_REGULATORS];
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define REG_MBATT() \
378c2ecf20Sopenharmony_ci	[MAX8907_MBATT] = { \
388c2ecf20Sopenharmony_ci		.name = "MBATT", \
398c2ecf20Sopenharmony_ci		.supply_name = "mbatt", \
408c2ecf20Sopenharmony_ci		.id = MAX8907_MBATT, \
418c2ecf20Sopenharmony_ci		.ops = &max8907_mbatt_ops, \
428c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE, \
438c2ecf20Sopenharmony_ci		.owner = THIS_MODULE, \
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define REG_LDO(ids, supply, base, min, max, step) \
478c2ecf20Sopenharmony_ci	[MAX8907_##ids] = { \
488c2ecf20Sopenharmony_ci		.name = #ids, \
498c2ecf20Sopenharmony_ci		.supply_name = supply, \
508c2ecf20Sopenharmony_ci		.id = MAX8907_##ids, \
518c2ecf20Sopenharmony_ci		.n_voltages = ((max) - (min)) / (step) + 1, \
528c2ecf20Sopenharmony_ci		.ops = &max8907_ldo_ops, \
538c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE, \
548c2ecf20Sopenharmony_ci		.owner = THIS_MODULE, \
558c2ecf20Sopenharmony_ci		.min_uV = (min), \
568c2ecf20Sopenharmony_ci		.uV_step = (step), \
578c2ecf20Sopenharmony_ci		.vsel_reg = (base) + MAX8907_VOUT, \
588c2ecf20Sopenharmony_ci		.vsel_mask = 0x3f, \
598c2ecf20Sopenharmony_ci		.enable_reg = (base) + MAX8907_CTL, \
608c2ecf20Sopenharmony_ci		.enable_mask = MAX8907_MASK_LDO_EN, \
618c2ecf20Sopenharmony_ci	}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define REG_FIXED(ids, supply, voltage) \
648c2ecf20Sopenharmony_ci	[MAX8907_##ids] = { \
658c2ecf20Sopenharmony_ci		.name = #ids, \
668c2ecf20Sopenharmony_ci		.supply_name = supply, \
678c2ecf20Sopenharmony_ci		.id = MAX8907_##ids, \
688c2ecf20Sopenharmony_ci		.n_voltages = 1, \
698c2ecf20Sopenharmony_ci		.ops = &max8907_fixed_ops, \
708c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE, \
718c2ecf20Sopenharmony_ci		.owner = THIS_MODULE, \
728c2ecf20Sopenharmony_ci		.min_uV = (voltage), \
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define REG_OUT5V(ids, supply, base, voltage) \
768c2ecf20Sopenharmony_ci	[MAX8907_##ids] = { \
778c2ecf20Sopenharmony_ci		.name = #ids, \
788c2ecf20Sopenharmony_ci		.supply_name = supply, \
798c2ecf20Sopenharmony_ci		.id = MAX8907_##ids, \
808c2ecf20Sopenharmony_ci		.n_voltages = 1, \
818c2ecf20Sopenharmony_ci		.ops = &max8907_out5v_ops, \
828c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE, \
838c2ecf20Sopenharmony_ci		.owner = THIS_MODULE, \
848c2ecf20Sopenharmony_ci		.min_uV = (voltage), \
858c2ecf20Sopenharmony_ci		.enable_reg = (base), \
868c2ecf20Sopenharmony_ci		.enable_mask = MAX8907_MASK_OUT5V_EN, \
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define REG_BBAT(ids, supply, base, min, max, step) \
908c2ecf20Sopenharmony_ci	[MAX8907_##ids] = { \
918c2ecf20Sopenharmony_ci		.name = #ids, \
928c2ecf20Sopenharmony_ci		.supply_name = supply, \
938c2ecf20Sopenharmony_ci		.id = MAX8907_##ids, \
948c2ecf20Sopenharmony_ci		.n_voltages = ((max) - (min)) / (step) + 1, \
958c2ecf20Sopenharmony_ci		.ops = &max8907_bbat_ops, \
968c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE, \
978c2ecf20Sopenharmony_ci		.owner = THIS_MODULE, \
988c2ecf20Sopenharmony_ci		.min_uV = (min), \
998c2ecf20Sopenharmony_ci		.uV_step = (step), \
1008c2ecf20Sopenharmony_ci		.vsel_reg = (base), \
1018c2ecf20Sopenharmony_ci		.vsel_mask = MAX8907_MASK_VBBATTCV, \
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define LDO_750_50(id, supply, base) REG_LDO(id, supply, (base), \
1058c2ecf20Sopenharmony_ci			750000, 3900000, 50000)
1068c2ecf20Sopenharmony_ci#define LDO_650_25(id, supply, base) REG_LDO(id, supply, (base), \
1078c2ecf20Sopenharmony_ci			650000, 2225000, 25000)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic const struct regulator_ops max8907_mbatt_ops = {
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic const struct regulator_ops max8907_ldo_ops = {
1138c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1148c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
1158c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
1168c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
1178c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
1188c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic const struct regulator_ops max8907_ldo_hwctl_ops = {
1228c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1238c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
1248c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic const struct regulator_ops max8907_fixed_ops = {
1288c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic const struct regulator_ops max8907_out5v_ops = {
1328c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1338c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
1348c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
1358c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic const struct regulator_ops max8907_out5v_hwctl_ops = {
1398c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic const struct regulator_ops max8907_bbat_ops = {
1438c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1448c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
1458c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic const struct regulator_desc max8907_regulators[] = {
1498c2ecf20Sopenharmony_ci	REG_MBATT(),
1508c2ecf20Sopenharmony_ci	REG_LDO(SD1, "in-v1", MAX8907_REG_SDCTL1, 650000, 2225000, 25000),
1518c2ecf20Sopenharmony_ci	REG_LDO(SD2, "in-v2", MAX8907_REG_SDCTL2, 637500, 1425000, 12500),
1528c2ecf20Sopenharmony_ci	REG_LDO(SD3, "in-v3", MAX8907_REG_SDCTL3, 750000, 3900000, 50000),
1538c2ecf20Sopenharmony_ci	LDO_750_50(LDO1, "in1", MAX8907_REG_LDOCTL1),
1548c2ecf20Sopenharmony_ci	LDO_650_25(LDO2, "in2", MAX8907_REG_LDOCTL2),
1558c2ecf20Sopenharmony_ci	LDO_650_25(LDO3, "in3", MAX8907_REG_LDOCTL3),
1568c2ecf20Sopenharmony_ci	LDO_750_50(LDO4, "in4", MAX8907_REG_LDOCTL4),
1578c2ecf20Sopenharmony_ci	LDO_750_50(LDO5, "in5", MAX8907_REG_LDOCTL5),
1588c2ecf20Sopenharmony_ci	LDO_750_50(LDO6, "in6", MAX8907_REG_LDOCTL6),
1598c2ecf20Sopenharmony_ci	LDO_750_50(LDO7, "in7", MAX8907_REG_LDOCTL7),
1608c2ecf20Sopenharmony_ci	LDO_750_50(LDO8, "in8", MAX8907_REG_LDOCTL8),
1618c2ecf20Sopenharmony_ci	LDO_750_50(LDO9, "in9", MAX8907_REG_LDOCTL9),
1628c2ecf20Sopenharmony_ci	LDO_750_50(LDO10, "in10", MAX8907_REG_LDOCTL10),
1638c2ecf20Sopenharmony_ci	LDO_750_50(LDO11, "in11", MAX8907_REG_LDOCTL11),
1648c2ecf20Sopenharmony_ci	LDO_750_50(LDO12, "in12", MAX8907_REG_LDOCTL12),
1658c2ecf20Sopenharmony_ci	LDO_750_50(LDO13, "in13", MAX8907_REG_LDOCTL13),
1668c2ecf20Sopenharmony_ci	LDO_750_50(LDO14, "in14", MAX8907_REG_LDOCTL14),
1678c2ecf20Sopenharmony_ci	LDO_750_50(LDO15, "in15", MAX8907_REG_LDOCTL15),
1688c2ecf20Sopenharmony_ci	LDO_750_50(LDO16, "in16", MAX8907_REG_LDOCTL16),
1698c2ecf20Sopenharmony_ci	LDO_650_25(LDO17, "in17", MAX8907_REG_LDOCTL17),
1708c2ecf20Sopenharmony_ci	LDO_650_25(LDO18, "in18", MAX8907_REG_LDOCTL18),
1718c2ecf20Sopenharmony_ci	LDO_750_50(LDO19, "in19", MAX8907_REG_LDOCTL19),
1728c2ecf20Sopenharmony_ci	LDO_750_50(LDO20, "in20", MAX8907_REG_LDOCTL20),
1738c2ecf20Sopenharmony_ci	REG_OUT5V(OUT5V, "mbatt", MAX8907_REG_OUT5VEN, 5000000),
1748c2ecf20Sopenharmony_ci	REG_OUT5V(OUT33V, "mbatt",  MAX8907_REG_OUT33VEN, 3300000),
1758c2ecf20Sopenharmony_ci	REG_BBAT(BBAT, "MBATT", MAX8907_REG_BBAT_CNFG,
1768c2ecf20Sopenharmony_ci						2400000, 3000000, 200000),
1778c2ecf20Sopenharmony_ci	REG_FIXED(SDBY, "MBATT", 1200000),
1788c2ecf20Sopenharmony_ci	REG_FIXED(VRTC, "MBATT", 3300000),
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci#define MATCH(_name, _id) \
1848c2ecf20Sopenharmony_ci	[MAX8907_##_id] = { \
1858c2ecf20Sopenharmony_ci		.name = #_name, \
1868c2ecf20Sopenharmony_ci		.driver_data = (void *)&max8907_regulators[MAX8907_##_id], \
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic struct of_regulator_match max8907_matches[] = {
1908c2ecf20Sopenharmony_ci	MATCH(mbatt, MBATT),
1918c2ecf20Sopenharmony_ci	MATCH(sd1, SD1),
1928c2ecf20Sopenharmony_ci	MATCH(sd2, SD2),
1938c2ecf20Sopenharmony_ci	MATCH(sd3, SD3),
1948c2ecf20Sopenharmony_ci	MATCH(ldo1, LDO1),
1958c2ecf20Sopenharmony_ci	MATCH(ldo2, LDO2),
1968c2ecf20Sopenharmony_ci	MATCH(ldo3, LDO3),
1978c2ecf20Sopenharmony_ci	MATCH(ldo4, LDO4),
1988c2ecf20Sopenharmony_ci	MATCH(ldo5, LDO5),
1998c2ecf20Sopenharmony_ci	MATCH(ldo6, LDO6),
2008c2ecf20Sopenharmony_ci	MATCH(ldo7, LDO7),
2018c2ecf20Sopenharmony_ci	MATCH(ldo8, LDO8),
2028c2ecf20Sopenharmony_ci	MATCH(ldo9, LDO9),
2038c2ecf20Sopenharmony_ci	MATCH(ldo10, LDO10),
2048c2ecf20Sopenharmony_ci	MATCH(ldo11, LDO11),
2058c2ecf20Sopenharmony_ci	MATCH(ldo12, LDO12),
2068c2ecf20Sopenharmony_ci	MATCH(ldo13, LDO13),
2078c2ecf20Sopenharmony_ci	MATCH(ldo14, LDO14),
2088c2ecf20Sopenharmony_ci	MATCH(ldo15, LDO15),
2098c2ecf20Sopenharmony_ci	MATCH(ldo16, LDO16),
2108c2ecf20Sopenharmony_ci	MATCH(ldo17, LDO17),
2118c2ecf20Sopenharmony_ci	MATCH(ldo18, LDO18),
2128c2ecf20Sopenharmony_ci	MATCH(ldo19, LDO19),
2138c2ecf20Sopenharmony_ci	MATCH(ldo20, LDO20),
2148c2ecf20Sopenharmony_ci	MATCH(out5v, OUT5V),
2158c2ecf20Sopenharmony_ci	MATCH(out33v, OUT33V),
2168c2ecf20Sopenharmony_ci	MATCH(bbat, BBAT),
2178c2ecf20Sopenharmony_ci	MATCH(sdby, SDBY),
2188c2ecf20Sopenharmony_ci	MATCH(vrtc, VRTC),
2198c2ecf20Sopenharmony_ci};
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic int max8907_regulator_parse_dt(struct platform_device *pdev)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	struct device_node *np, *regulators;
2248c2ecf20Sopenharmony_ci	int ret;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	np = pdev->dev.parent->of_node;
2278c2ecf20Sopenharmony_ci	if (!np)
2288c2ecf20Sopenharmony_ci		return 0;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	regulators = of_get_child_by_name(np, "regulators");
2318c2ecf20Sopenharmony_ci	if (!regulators) {
2328c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "regulators node not found\n");
2338c2ecf20Sopenharmony_ci		return -EINVAL;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ret = of_regulator_match(&pdev->dev, regulators, max8907_matches,
2378c2ecf20Sopenharmony_ci				 ARRAY_SIZE(max8907_matches));
2388c2ecf20Sopenharmony_ci	of_node_put(regulators);
2398c2ecf20Sopenharmony_ci	if (ret < 0) {
2408c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
2418c2ecf20Sopenharmony_ci			ret);
2428c2ecf20Sopenharmony_ci		return ret;
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	return 0;
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic inline struct regulator_init_data *match_init_data(int index)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	return max8907_matches[index].init_data;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic inline struct device_node *match_of_node(int index)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	return max8907_matches[index].of_node;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci#else
2588c2ecf20Sopenharmony_cistatic int max8907_regulator_parse_dt(struct platform_device *pdev)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	return 0;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic inline struct regulator_init_data *match_init_data(int index)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	return NULL;
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic inline struct device_node *match_of_node(int index)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	return NULL;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci#endif
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic int max8907_regulator_probe(struct platform_device *pdev)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct max8907 *max8907 = dev_get_drvdata(pdev->dev.parent);
2778c2ecf20Sopenharmony_ci	struct max8907_platform_data *pdata = dev_get_platdata(max8907->dev);
2788c2ecf20Sopenharmony_ci	int ret;
2798c2ecf20Sopenharmony_ci	struct max8907_regulator *pmic;
2808c2ecf20Sopenharmony_ci	unsigned int val;
2818c2ecf20Sopenharmony_ci	int i;
2828c2ecf20Sopenharmony_ci	struct regulator_config config = {};
2838c2ecf20Sopenharmony_ci	struct regulator_init_data *idata;
2848c2ecf20Sopenharmony_ci	const char *mbatt_rail_name = NULL;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	ret = max8907_regulator_parse_dt(pdev);
2878c2ecf20Sopenharmony_ci	if (ret)
2888c2ecf20Sopenharmony_ci		return ret;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
2918c2ecf20Sopenharmony_ci	if (!pmic)
2928c2ecf20Sopenharmony_ci		return -ENOMEM;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, pmic);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	memcpy(pmic->desc, max8907_regulators, sizeof(pmic->desc));
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/* Backwards compatibility with MAX8907B; SD1 uses different voltages */
2998c2ecf20Sopenharmony_ci	ret = regmap_read(max8907->regmap_gen, MAX8907_REG_II2RR, &val);
3008c2ecf20Sopenharmony_ci	if (ret)
3018c2ecf20Sopenharmony_ci		return ret;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if ((val & MAX8907_II2RR_VERSION_MASK) ==
3048c2ecf20Sopenharmony_ci	    MAX8907_II2RR_VERSION_REV_B) {
3058c2ecf20Sopenharmony_ci		pmic->desc[MAX8907_SD1].min_uV = 637500;
3068c2ecf20Sopenharmony_ci		pmic->desc[MAX8907_SD1].uV_step = 12500;
3078c2ecf20Sopenharmony_ci		pmic->desc[MAX8907_SD1].n_voltages =
3088c2ecf20Sopenharmony_ci						(1425000 - 637500) / 12500 + 1;
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	for (i = 0; i < MAX8907_NUM_REGULATORS; i++) {
3128c2ecf20Sopenharmony_ci		struct regulator_dev *rdev;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci		config.dev = pdev->dev.parent;
3158c2ecf20Sopenharmony_ci		if (pdata)
3168c2ecf20Sopenharmony_ci			idata = pdata->init_data[i];
3178c2ecf20Sopenharmony_ci		else
3188c2ecf20Sopenharmony_ci			idata = match_init_data(i);
3198c2ecf20Sopenharmony_ci		config.init_data = idata;
3208c2ecf20Sopenharmony_ci		config.driver_data = pmic;
3218c2ecf20Sopenharmony_ci		config.regmap = max8907->regmap_gen;
3228c2ecf20Sopenharmony_ci		config.of_node = match_of_node(i);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci		switch (pmic->desc[i].id) {
3258c2ecf20Sopenharmony_ci		case MAX8907_MBATT:
3268c2ecf20Sopenharmony_ci			if (idata && idata->constraints.name)
3278c2ecf20Sopenharmony_ci				mbatt_rail_name = idata->constraints.name;
3288c2ecf20Sopenharmony_ci			else
3298c2ecf20Sopenharmony_ci				mbatt_rail_name = pmic->desc[i].name;
3308c2ecf20Sopenharmony_ci			break;
3318c2ecf20Sopenharmony_ci		case MAX8907_BBAT:
3328c2ecf20Sopenharmony_ci		case MAX8907_SDBY:
3338c2ecf20Sopenharmony_ci		case MAX8907_VRTC:
3348c2ecf20Sopenharmony_ci			idata->supply_regulator = mbatt_rail_name;
3358c2ecf20Sopenharmony_ci			break;
3368c2ecf20Sopenharmony_ci		}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci		if (pmic->desc[i].ops == &max8907_ldo_ops) {
3398c2ecf20Sopenharmony_ci			ret = regmap_read(config.regmap, pmic->desc[i].enable_reg,
3408c2ecf20Sopenharmony_ci				    &val);
3418c2ecf20Sopenharmony_ci			if (ret)
3428c2ecf20Sopenharmony_ci				return ret;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci			if ((val & MAX8907_MASK_LDO_SEQ) !=
3458c2ecf20Sopenharmony_ci			    MAX8907_MASK_LDO_SEQ)
3468c2ecf20Sopenharmony_ci				pmic->desc[i].ops = &max8907_ldo_hwctl_ops;
3478c2ecf20Sopenharmony_ci		} else if (pmic->desc[i].ops == &max8907_out5v_ops) {
3488c2ecf20Sopenharmony_ci			ret = regmap_read(config.regmap, pmic->desc[i].enable_reg,
3498c2ecf20Sopenharmony_ci				    &val);
3508c2ecf20Sopenharmony_ci			if (ret)
3518c2ecf20Sopenharmony_ci				return ret;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci			if ((val & (MAX8907_MASK_OUT5V_VINEN |
3548c2ecf20Sopenharmony_ci						MAX8907_MASK_OUT5V_ENSRC)) !=
3558c2ecf20Sopenharmony_ci			    MAX8907_MASK_OUT5V_ENSRC)
3568c2ecf20Sopenharmony_ci				pmic->desc[i].ops = &max8907_out5v_hwctl_ops;
3578c2ecf20Sopenharmony_ci		}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev,
3608c2ecf20Sopenharmony_ci						&pmic->desc[i], &config);
3618c2ecf20Sopenharmony_ci		if (IS_ERR(rdev)) {
3628c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
3638c2ecf20Sopenharmony_ci				"failed to register %s regulator\n",
3648c2ecf20Sopenharmony_ci				pmic->desc[i].name);
3658c2ecf20Sopenharmony_ci			return PTR_ERR(rdev);
3668c2ecf20Sopenharmony_ci		}
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	return 0;
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic struct platform_driver max8907_regulator_driver = {
3738c2ecf20Sopenharmony_ci	.driver = {
3748c2ecf20Sopenharmony_ci		   .name = "max8907-regulator",
3758c2ecf20Sopenharmony_ci		   },
3768c2ecf20Sopenharmony_ci	.probe = max8907_regulator_probe,
3778c2ecf20Sopenharmony_ci};
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic int __init max8907_regulator_init(void)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	return platform_driver_register(&max8907_regulator_driver);
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cisubsys_initcall(max8907_regulator_init);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic void __exit max8907_reg_exit(void)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	platform_driver_unregister(&max8907_regulator_driver);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cimodule_exit(max8907_reg_exit);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MAX8907 regulator driver");
3948c2ecf20Sopenharmony_ciMODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>");
3958c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
3968c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:max8907-regulator");
397