19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26ffc3270SLaxman Dewangan /*
36ffc3270SLaxman Dewangan  * Regulator driver for RICOH RC5T583 power management chip.
46ffc3270SLaxman Dewangan  *
56ffc3270SLaxman Dewangan  * Copyright (c) 2011-2012, NVIDIA CORPORATION.  All rights reserved.
66ffc3270SLaxman Dewangan  * Author: Laxman dewangan <ldewangan@nvidia.com>
76ffc3270SLaxman Dewangan  *
86ffc3270SLaxman Dewangan  * based on code
96ffc3270SLaxman Dewangan  *      Copyright (C) 2011 RICOH COMPANY,LTD
106ffc3270SLaxman Dewangan  */
116ffc3270SLaxman Dewangan 
126ffc3270SLaxman Dewangan #include <linux/module.h>
136ffc3270SLaxman Dewangan #include <linux/init.h>
146ffc3270SLaxman Dewangan #include <linux/slab.h>
156ffc3270SLaxman Dewangan #include <linux/err.h>
166ffc3270SLaxman Dewangan #include <linux/platform_device.h>
176ffc3270SLaxman Dewangan #include <linux/regulator/driver.h>
186ffc3270SLaxman Dewangan #include <linux/regulator/machine.h>
196ffc3270SLaxman Dewangan #include <linux/mfd/rc5t583.h>
206ffc3270SLaxman Dewangan 
216ffc3270SLaxman Dewangan struct rc5t583_regulator_info {
226ffc3270SLaxman Dewangan 	int			deepsleep_id;
236ffc3270SLaxman Dewangan 
246ffc3270SLaxman Dewangan 	/* Regulator register address.*/
256ffc3270SLaxman Dewangan 	uint8_t			reg_disc_reg;
266ffc3270SLaxman Dewangan 	uint8_t			disc_bit;
276ffc3270SLaxman Dewangan 	uint8_t			deepsleep_reg;
286ffc3270SLaxman Dewangan 
296ffc3270SLaxman Dewangan 	/* Regulator specific turn-on delay  and voltage settling time*/
306ffc3270SLaxman Dewangan 	int			enable_uv_per_us;
316ffc3270SLaxman Dewangan 
326ffc3270SLaxman Dewangan 	/* Used by regulator core */
336ffc3270SLaxman Dewangan 	struct regulator_desc	desc;
346ffc3270SLaxman Dewangan };
356ffc3270SLaxman Dewangan 
rc5t583_regulator_enable_time(struct regulator_dev * rdev)366ffc3270SLaxman Dewangan static int rc5t583_regulator_enable_time(struct regulator_dev *rdev)
376ffc3270SLaxman Dewangan {
38c07608f7SAxel Lin 	struct rc5t583_regulator_info *reg_info = rdev_get_drvdata(rdev);
3915b397d7SAxel Lin 	int vsel = regulator_get_voltage_sel_regmap(rdev);
403209be17SAxel Lin 	int curr_uV = regulator_list_voltage_linear(rdev, vsel);
4115b397d7SAxel Lin 
42c07608f7SAxel Lin 	return DIV_ROUND_UP(curr_uV, reg_info->enable_uv_per_us);
436ffc3270SLaxman Dewangan }
446ffc3270SLaxman Dewangan 
45fb74036bSBhumika Goyal static const struct regulator_ops rc5t583_ops = {
465bb6936cSAxel Lin 	.is_enabled		= regulator_is_enabled_regmap,
475bb6936cSAxel Lin 	.enable			= regulator_enable_regmap,
485bb6936cSAxel Lin 	.disable		= regulator_disable_regmap,
496ffc3270SLaxman Dewangan 	.enable_time		= rc5t583_regulator_enable_time,
5015b397d7SAxel Lin 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
5170e5f645SAxel Lin 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
523209be17SAxel Lin 	.list_voltage		= regulator_list_voltage_linear,
5370e5f645SAxel Lin 	.map_voltage		= regulator_map_voltage_linear,
542f6ae6efSAxel Lin 	.set_voltage_time_sel	= regulator_set_voltage_time_sel,
556ffc3270SLaxman Dewangan };
566ffc3270SLaxman Dewangan 
5795e301baSAxel Lin #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \
5895e301baSAxel Lin 		_vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \
596ffc3270SLaxman Dewangan {								\
606ffc3270SLaxman Dewangan 	.reg_disc_reg	= RC5T583_REG_##_disc_reg,		\
616ffc3270SLaxman Dewangan 	.disc_bit	= _disc_bit,				\
6295e301baSAxel Lin 	.deepsleep_reg	= RC5T583_REG_##_id##DAC_DS,		\
636ffc3270SLaxman Dewangan 	.enable_uv_per_us = _enable_mv * 1000,			\
646ffc3270SLaxman Dewangan 	.deepsleep_id	= RC5T583_DS_##_id,			\
656ffc3270SLaxman Dewangan 	.desc = {						\
666ffc3270SLaxman Dewangan 		.name = "rc5t583-regulator-"#_id,		\
676ffc3270SLaxman Dewangan 		.id = RC5T583_REGULATOR_##_id,			\
68e3a7384cSAxel Lin 		.n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \
696ffc3270SLaxman Dewangan 		.ops = &rc5t583_ops,				\
706ffc3270SLaxman Dewangan 		.type = REGULATOR_VOLTAGE,			\
716ffc3270SLaxman Dewangan 		.owner = THIS_MODULE,				\
7215b397d7SAxel Lin 		.vsel_reg = RC5T583_REG_##_id##DAC,		\
7315b397d7SAxel Lin 		.vsel_mask = _vout_mask,			\
745bb6936cSAxel Lin 		.enable_reg = RC5T583_REG_##_en_reg,		\
755bb6936cSAxel Lin 		.enable_mask = BIT(_en_bit),			\
763209be17SAxel Lin 		.min_uV	= _min_mv * 1000,			\
773209be17SAxel Lin 		.uV_step = _step_uV,				\
782f6ae6efSAxel Lin 		.ramp_delay = 40 * 1000,			\
796ffc3270SLaxman Dewangan 	},							\
806ffc3270SLaxman Dewangan }
816ffc3270SLaxman Dewangan 
826ffc3270SLaxman Dewangan static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = {
8395e301baSAxel Lin 	RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4),
8495e301baSAxel Lin 	RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14),
8595e301baSAxel Lin 	RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14),
8695e301baSAxel Lin 	RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14),
8795e301baSAxel Lin 	RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160),
8895e301baSAxel Lin 	RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160),
8995e301baSAxel Lin 	RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160),
9095e301baSAxel Lin 	RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160),
9195e301baSAxel Lin 	RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133),
9295e301baSAxel Lin 	RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267),
9395e301baSAxel Lin 	RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133),
9495e301baSAxel Lin 	RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233),
9595e301baSAxel Lin 	RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233),
9695e301baSAxel Lin 	RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133),
976ffc3270SLaxman Dewangan };
986ffc3270SLaxman Dewangan 
rc5t583_regulator_probe(struct platform_device * pdev)99a5023574SBill Pemberton static int rc5t583_regulator_probe(struct platform_device *pdev)
1006ffc3270SLaxman Dewangan {
1016ffc3270SLaxman Dewangan 	struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
1026ffc3270SLaxman Dewangan 	struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
103c172708dSMark Brown 	struct regulator_config config = { };
1046ffc3270SLaxman Dewangan 	struct regulator_dev *rdev;
1056ffc3270SLaxman Dewangan 	struct rc5t583_regulator_info *ri;
1066ffc3270SLaxman Dewangan 	int ret;
1076ffc3270SLaxman Dewangan 	int id;
1086ffc3270SLaxman Dewangan 
1096ffc3270SLaxman Dewangan 	if (!pdata) {
1106ffc3270SLaxman Dewangan 		dev_err(&pdev->dev, "No platform data, exiting...\n");
1116ffc3270SLaxman Dewangan 		return -ENODEV;
1126ffc3270SLaxman Dewangan 	}
1136ffc3270SLaxman Dewangan 
1146ffc3270SLaxman Dewangan 	for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) {
1156ffc3270SLaxman Dewangan 		ri = &rc5t583_reg_info[id];
1166ffc3270SLaxman Dewangan 
1176ffc3270SLaxman Dewangan 		if (ri->deepsleep_id == RC5T583_DS_NONE)
1186ffc3270SLaxman Dewangan 			goto skip_ext_pwr_config;
1196ffc3270SLaxman Dewangan 
1206ffc3270SLaxman Dewangan 		ret = rc5t583_ext_power_req_config(rc5t583->dev,
1216ffc3270SLaxman Dewangan 				ri->deepsleep_id,
1226ffc3270SLaxman Dewangan 				pdata->regulator_ext_pwr_control[id],
1236ffc3270SLaxman Dewangan 				pdata->regulator_deepsleep_slot[id]);
1246ffc3270SLaxman Dewangan 		/*
1256ffc3270SLaxman Dewangan 		 * Configuring external control is not a major issue,
1266ffc3270SLaxman Dewangan 		 * just give warning.
1276ffc3270SLaxman Dewangan 		 */
1286ffc3270SLaxman Dewangan 		if (ret < 0)
1296ffc3270SLaxman Dewangan 			dev_warn(&pdev->dev,
1306ffc3270SLaxman Dewangan 				"Failed to configure ext control %d\n", id);
1316ffc3270SLaxman Dewangan 
1326ffc3270SLaxman Dewangan skip_ext_pwr_config:
133c172708dSMark Brown 		config.dev = &pdev->dev;
1343f24bf70SAxel Lin 		config.init_data = pdata->reg_init_data[id];
135c07608f7SAxel Lin 		config.driver_data = ri;
1365bb6936cSAxel Lin 		config.regmap = rc5t583->regmap;
137c172708dSMark Brown 
13813775309SSachin Kamat 		rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
139a69df8a1SAxel Lin 		if (IS_ERR(rdev)) {
1406ffc3270SLaxman Dewangan 			dev_err(&pdev->dev, "Failed to register regulator %s\n",
1416ffc3270SLaxman Dewangan 						ri->desc.name);
14213775309SSachin Kamat 			return PTR_ERR(rdev);
1436ffc3270SLaxman Dewangan 		}
1446ffc3270SLaxman Dewangan 	}
1456ffc3270SLaxman Dewangan 	return 0;
1466ffc3270SLaxman Dewangan }
1476ffc3270SLaxman Dewangan 
1486ffc3270SLaxman Dewangan static struct platform_driver rc5t583_regulator_driver = {
1496ffc3270SLaxman Dewangan 	.driver	= {
1506ffc3270SLaxman Dewangan 		.name	= "rc5t583-regulator",
151*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1526ffc3270SLaxman Dewangan 	},
1536ffc3270SLaxman Dewangan 	.probe		= rc5t583_regulator_probe,
1546ffc3270SLaxman Dewangan };
1556ffc3270SLaxman Dewangan 
rc5t583_regulator_init(void)1566ffc3270SLaxman Dewangan static int __init rc5t583_regulator_init(void)
1576ffc3270SLaxman Dewangan {
1586ffc3270SLaxman Dewangan 	return platform_driver_register(&rc5t583_regulator_driver);
1596ffc3270SLaxman Dewangan }
1606ffc3270SLaxman Dewangan subsys_initcall(rc5t583_regulator_init);
1616ffc3270SLaxman Dewangan 
rc5t583_regulator_exit(void)1626ffc3270SLaxman Dewangan static void __exit rc5t583_regulator_exit(void)
1636ffc3270SLaxman Dewangan {
1646ffc3270SLaxman Dewangan 	platform_driver_unregister(&rc5t583_regulator_driver);
1656ffc3270SLaxman Dewangan }
1666ffc3270SLaxman Dewangan module_exit(rc5t583_regulator_exit);
1676ffc3270SLaxman Dewangan 
1686ffc3270SLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1696ffc3270SLaxman Dewangan MODULE_DESCRIPTION("RC5T583 regulator driver");
1706ffc3270SLaxman Dewangan MODULE_ALIAS("platform:rc5t583-regulator");
1714eb06453SLaxman Dewangan MODULE_LICENSE("GPL v2");
172