18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Regulators driver for Maxim max8925
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Marvell International Ltd.
68c2ecf20Sopenharmony_ci *      Haojian Zhuang <haojian.zhuang@marvell.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/err.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
148c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
158c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
168c2ecf20Sopenharmony_ci#include <linux/mfd/max8925.h>
178c2ecf20Sopenharmony_ci#include <linux/of.h>
188c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define SD1_DVM_VMIN		850000
218c2ecf20Sopenharmony_ci#define SD1_DVM_VMAX		1000000
228c2ecf20Sopenharmony_ci#define SD1_DVM_STEP		50000
238c2ecf20Sopenharmony_ci#define SD1_DVM_SHIFT		5		/* SDCTL1 bit5 */
248c2ecf20Sopenharmony_ci#define SD1_DVM_EN		6		/* SDV1 bit 6 */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* bit definitions in LDO control registers */
278c2ecf20Sopenharmony_ci#define LDO_SEQ_I2C		0x7		/* Power U/D by i2c */
288c2ecf20Sopenharmony_ci#define LDO_SEQ_MASK		0x7		/* Power U/D sequence mask */
298c2ecf20Sopenharmony_ci#define LDO_SEQ_SHIFT		2		/* Power U/D sequence offset */
308c2ecf20Sopenharmony_ci#define LDO_I2C_EN		0x1		/* Enable by i2c */
318c2ecf20Sopenharmony_ci#define LDO_I2C_EN_MASK		0x1		/* Enable mask by i2c */
328c2ecf20Sopenharmony_ci#define LDO_I2C_EN_SHIFT	0		/* Enable offset by i2c */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct max8925_regulator_info {
358c2ecf20Sopenharmony_ci	struct regulator_desc	desc;
368c2ecf20Sopenharmony_ci	struct i2c_client	*i2c;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	int	vol_reg;
398c2ecf20Sopenharmony_ci	int	enable_reg;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic int max8925_set_voltage_sel(struct regulator_dev *rdev,
438c2ecf20Sopenharmony_ci				   unsigned int selector)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
468c2ecf20Sopenharmony_ci	unsigned char mask = rdev->desc->n_voltages - 1;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return max8925_set_bits(info->i2c, info->vol_reg, mask, selector);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int max8925_get_voltage_sel(struct regulator_dev *rdev)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
548c2ecf20Sopenharmony_ci	unsigned char data, mask;
558c2ecf20Sopenharmony_ci	int ret;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	ret = max8925_reg_read(info->i2c, info->vol_reg);
588c2ecf20Sopenharmony_ci	if (ret < 0)
598c2ecf20Sopenharmony_ci		return ret;
608c2ecf20Sopenharmony_ci	mask = rdev->desc->n_voltages - 1;
618c2ecf20Sopenharmony_ci	data = ret & mask;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return data;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int max8925_enable(struct regulator_dev *rdev)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return max8925_set_bits(info->i2c, info->enable_reg,
718c2ecf20Sopenharmony_ci				LDO_SEQ_MASK << LDO_SEQ_SHIFT |
728c2ecf20Sopenharmony_ci				LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
738c2ecf20Sopenharmony_ci				LDO_SEQ_I2C << LDO_SEQ_SHIFT |
748c2ecf20Sopenharmony_ci				LDO_I2C_EN << LDO_I2C_EN_SHIFT);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int max8925_disable(struct regulator_dev *rdev)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return max8925_set_bits(info->i2c, info->enable_reg,
828c2ecf20Sopenharmony_ci				LDO_SEQ_MASK << LDO_SEQ_SHIFT |
838c2ecf20Sopenharmony_ci				LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
848c2ecf20Sopenharmony_ci				LDO_SEQ_I2C << LDO_SEQ_SHIFT);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int max8925_is_enabled(struct regulator_dev *rdev)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
908c2ecf20Sopenharmony_ci	int ldo_seq, ret;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	ret = max8925_reg_read(info->i2c, info->enable_reg);
938c2ecf20Sopenharmony_ci	if (ret < 0)
948c2ecf20Sopenharmony_ci		return ret;
958c2ecf20Sopenharmony_ci	ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
968c2ecf20Sopenharmony_ci	if (ldo_seq != LDO_SEQ_I2C)
978c2ecf20Sopenharmony_ci		return 1;
988c2ecf20Sopenharmony_ci	else
998c2ecf20Sopenharmony_ci		return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
1058c2ecf20Sopenharmony_ci	unsigned char data, mask;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
1088c2ecf20Sopenharmony_ci		return -EINVAL;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
1118c2ecf20Sopenharmony_ci	data <<= SD1_DVM_SHIFT;
1128c2ecf20Sopenharmony_ci	mask = 3 << SD1_DVM_SHIFT;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int max8925_set_dvm_enable(struct regulator_dev *rdev)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
1228c2ecf20Sopenharmony_ci				1 << SD1_DVM_EN);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int max8925_set_dvm_disable(struct regulator_dev *rdev)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const struct regulator_ops max8925_regulator_sdv_ops = {
1338c2ecf20Sopenharmony_ci	.map_voltage		= regulator_map_voltage_linear,
1348c2ecf20Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
1358c2ecf20Sopenharmony_ci	.set_voltage_sel	= max8925_set_voltage_sel,
1368c2ecf20Sopenharmony_ci	.get_voltage_sel	= max8925_get_voltage_sel,
1378c2ecf20Sopenharmony_ci	.enable			= max8925_enable,
1388c2ecf20Sopenharmony_ci	.disable		= max8925_disable,
1398c2ecf20Sopenharmony_ci	.is_enabled		= max8925_is_enabled,
1408c2ecf20Sopenharmony_ci	.set_suspend_voltage	= max8925_set_dvm_voltage,
1418c2ecf20Sopenharmony_ci	.set_suspend_enable	= max8925_set_dvm_enable,
1428c2ecf20Sopenharmony_ci	.set_suspend_disable	= max8925_set_dvm_disable,
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic const struct regulator_ops max8925_regulator_ldo_ops = {
1468c2ecf20Sopenharmony_ci	.map_voltage		= regulator_map_voltage_linear,
1478c2ecf20Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear,
1488c2ecf20Sopenharmony_ci	.set_voltage_sel	= max8925_set_voltage_sel,
1498c2ecf20Sopenharmony_ci	.get_voltage_sel	= max8925_get_voltage_sel,
1508c2ecf20Sopenharmony_ci	.enable			= max8925_enable,
1518c2ecf20Sopenharmony_ci	.disable		= max8925_disable,
1528c2ecf20Sopenharmony_ci	.is_enabled		= max8925_is_enabled,
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#define MAX8925_SDV(_id, min, max, step)			\
1568c2ecf20Sopenharmony_ci{								\
1578c2ecf20Sopenharmony_ci	.desc	= {						\
1588c2ecf20Sopenharmony_ci		.name	= "SDV" #_id,				\
1598c2ecf20Sopenharmony_ci		.of_match = of_match_ptr("SDV" #_id),		\
1608c2ecf20Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),	\
1618c2ecf20Sopenharmony_ci		.ops	= &max8925_regulator_sdv_ops,		\
1628c2ecf20Sopenharmony_ci		.type	= REGULATOR_VOLTAGE,			\
1638c2ecf20Sopenharmony_ci		.id	= MAX8925_ID_SD##_id,			\
1648c2ecf20Sopenharmony_ci		.owner	= THIS_MODULE,				\
1658c2ecf20Sopenharmony_ci		.n_voltages = 64,				\
1668c2ecf20Sopenharmony_ci		.min_uV = min * 1000,				\
1678c2ecf20Sopenharmony_ci		.uV_step = step * 1000,				\
1688c2ecf20Sopenharmony_ci	},							\
1698c2ecf20Sopenharmony_ci	.vol_reg	= MAX8925_SDV##_id,			\
1708c2ecf20Sopenharmony_ci	.enable_reg	= MAX8925_SDCTL##_id,			\
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define MAX8925_LDO(_id, min, max, step)			\
1748c2ecf20Sopenharmony_ci{								\
1758c2ecf20Sopenharmony_ci	.desc	= {						\
1768c2ecf20Sopenharmony_ci		.name	= "LDO" #_id,				\
1778c2ecf20Sopenharmony_ci		.of_match = of_match_ptr("LDO" #_id),		\
1788c2ecf20Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),	\
1798c2ecf20Sopenharmony_ci		.ops	= &max8925_regulator_ldo_ops,		\
1808c2ecf20Sopenharmony_ci		.type	= REGULATOR_VOLTAGE,			\
1818c2ecf20Sopenharmony_ci		.id	= MAX8925_ID_LDO##_id,			\
1828c2ecf20Sopenharmony_ci		.owner	= THIS_MODULE,				\
1838c2ecf20Sopenharmony_ci		.n_voltages = 64,				\
1848c2ecf20Sopenharmony_ci		.min_uV = min * 1000,				\
1858c2ecf20Sopenharmony_ci		.uV_step = step * 1000,				\
1868c2ecf20Sopenharmony_ci	},							\
1878c2ecf20Sopenharmony_ci	.vol_reg	= MAX8925_LDOVOUT##_id,			\
1888c2ecf20Sopenharmony_ci	.enable_reg	= MAX8925_LDOCTL##_id,			\
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic struct max8925_regulator_info max8925_regulator_info[] = {
1928c2ecf20Sopenharmony_ci	MAX8925_SDV(1, 637.5, 1425, 12.5),
1938c2ecf20Sopenharmony_ci	MAX8925_SDV(2,   650, 2225,   25),
1948c2ecf20Sopenharmony_ci	MAX8925_SDV(3,   750, 3900,   50),
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	MAX8925_LDO(1,  750, 3900, 50),
1978c2ecf20Sopenharmony_ci	MAX8925_LDO(2,  650, 2250, 25),
1988c2ecf20Sopenharmony_ci	MAX8925_LDO(3,  650, 2250, 25),
1998c2ecf20Sopenharmony_ci	MAX8925_LDO(4,  750, 3900, 50),
2008c2ecf20Sopenharmony_ci	MAX8925_LDO(5,  750, 3900, 50),
2018c2ecf20Sopenharmony_ci	MAX8925_LDO(6,  750, 3900, 50),
2028c2ecf20Sopenharmony_ci	MAX8925_LDO(7,  750, 3900, 50),
2038c2ecf20Sopenharmony_ci	MAX8925_LDO(8,  750, 3900, 50),
2048c2ecf20Sopenharmony_ci	MAX8925_LDO(9,  750, 3900, 50),
2058c2ecf20Sopenharmony_ci	MAX8925_LDO(10, 750, 3900, 50),
2068c2ecf20Sopenharmony_ci	MAX8925_LDO(11, 750, 3900, 50),
2078c2ecf20Sopenharmony_ci	MAX8925_LDO(12, 750, 3900, 50),
2088c2ecf20Sopenharmony_ci	MAX8925_LDO(13, 750, 3900, 50),
2098c2ecf20Sopenharmony_ci	MAX8925_LDO(14, 750, 3900, 50),
2108c2ecf20Sopenharmony_ci	MAX8925_LDO(15, 750, 3900, 50),
2118c2ecf20Sopenharmony_ci	MAX8925_LDO(16, 750, 3900, 50),
2128c2ecf20Sopenharmony_ci	MAX8925_LDO(17, 650, 2250, 25),
2138c2ecf20Sopenharmony_ci	MAX8925_LDO(18, 650, 2250, 25),
2148c2ecf20Sopenharmony_ci	MAX8925_LDO(19, 750, 3900, 50),
2158c2ecf20Sopenharmony_ci	MAX8925_LDO(20, 750, 3900, 50),
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic int max8925_regulator_probe(struct platform_device *pdev)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
2218c2ecf20Sopenharmony_ci	struct regulator_init_data *pdata = dev_get_platdata(&pdev->dev);
2228c2ecf20Sopenharmony_ci	struct regulator_config config = { };
2238c2ecf20Sopenharmony_ci	struct max8925_regulator_info *ri;
2248c2ecf20Sopenharmony_ci	struct resource *res;
2258c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
2268c2ecf20Sopenharmony_ci	int i;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
2298c2ecf20Sopenharmony_ci	if (!res) {
2308c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "No REG resource!\n");
2318c2ecf20Sopenharmony_ci		return -EINVAL;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
2348c2ecf20Sopenharmony_ci		ri = &max8925_regulator_info[i];
2358c2ecf20Sopenharmony_ci		if (ri->vol_reg == res->start)
2368c2ecf20Sopenharmony_ci			break;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (i == ARRAY_SIZE(max8925_regulator_info)) {
2408c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to find regulator %llu\n",
2418c2ecf20Sopenharmony_ci			(unsigned long long)res->start);
2428c2ecf20Sopenharmony_ci		return -EINVAL;
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci	ri->i2c = chip->i2c;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	config.dev = chip->dev;
2478c2ecf20Sopenharmony_ci	config.driver_data = ri;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	if (pdata)
2508c2ecf20Sopenharmony_ci		config.init_data = pdata;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
2538c2ecf20Sopenharmony_ci	if (IS_ERR(rdev)) {
2548c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to register regulator %s\n",
2558c2ecf20Sopenharmony_ci				ri->desc.name);
2568c2ecf20Sopenharmony_ci		return PTR_ERR(rdev);
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, rdev);
2608c2ecf20Sopenharmony_ci	return 0;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic struct platform_driver max8925_regulator_driver = {
2648c2ecf20Sopenharmony_ci	.driver		= {
2658c2ecf20Sopenharmony_ci		.name	= "max8925-regulator",
2668c2ecf20Sopenharmony_ci	},
2678c2ecf20Sopenharmony_ci	.probe		= max8925_regulator_probe,
2688c2ecf20Sopenharmony_ci};
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic int __init max8925_regulator_init(void)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	return platform_driver_register(&max8925_regulator_driver);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_cisubsys_initcall(max8925_regulator_init);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic void __exit max8925_regulator_exit(void)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	platform_driver_unregister(&max8925_regulator_driver);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_cimodule_exit(max8925_regulator_exit);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2838c2ecf20Sopenharmony_ciMODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
2848c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
2858c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:max8925-regulator");
286