18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Regulator driver for TI TPS65912x PMICs
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
58c2ecf20Sopenharmony_ci *	Andrew F. Davis <afd@ti.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
88c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License version 2 as
98c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any
128c2ecf20Sopenharmony_ci * kind, whether expressed or implied; without even the implied warranty
138c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148c2ecf20Sopenharmony_ci * GNU General Public License version 2 for more details.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Based on the TPS65218 driver and the previous TPS65912 driver by
178c2ecf20Sopenharmony_ci * Margarita Olaya Cabrera <magi@slimlogic.co.uk>
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
238c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/mfd/tps65912.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cienum tps65912_regulators { DCDC1, DCDC2, DCDC3, DCDC4, LDO1, LDO2, LDO3,
288c2ecf20Sopenharmony_ci	LDO4, LDO5, LDO6, LDO7, LDO8, LDO9, LDO10 };
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define TPS65912_REGULATOR(_name, _id, _of_match, _ops, _vr, _er, _lr)	\
318c2ecf20Sopenharmony_ci	[_id] = {							\
328c2ecf20Sopenharmony_ci		.name			= _name,			\
338c2ecf20Sopenharmony_ci		.of_match		= _of_match,			\
348c2ecf20Sopenharmony_ci		.regulators_node	= "regulators",			\
358c2ecf20Sopenharmony_ci		.id			= _id,				\
368c2ecf20Sopenharmony_ci		.ops			= &_ops,			\
378c2ecf20Sopenharmony_ci		.n_voltages		= 64,				\
388c2ecf20Sopenharmony_ci		.type			= REGULATOR_VOLTAGE,		\
398c2ecf20Sopenharmony_ci		.owner			= THIS_MODULE,			\
408c2ecf20Sopenharmony_ci		.vsel_reg		= _vr,				\
418c2ecf20Sopenharmony_ci		.vsel_mask		= 0x3f,				\
428c2ecf20Sopenharmony_ci		.enable_reg		= _er,				\
438c2ecf20Sopenharmony_ci		.enable_mask		= BIT(7),			\
448c2ecf20Sopenharmony_ci		.volt_table		= NULL,				\
458c2ecf20Sopenharmony_ci		.linear_ranges		= _lr,				\
468c2ecf20Sopenharmony_ci		.n_linear_ranges	= ARRAY_SIZE(_lr),		\
478c2ecf20Sopenharmony_ci	}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic const struct linear_range tps65912_dcdc_ranges[] = {
508c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(500000, 0x0, 0x3f, 50000),
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic const struct linear_range tps65912_ldo_ranges[] = {
548c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(800000, 0x0, 0x20, 25000),
558c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(1650000, 0x21, 0x3c, 50000),
568c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(3100000, 0x3d, 0x3f, 100000),
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* Operations permitted on DCDCx */
608c2ecf20Sopenharmony_cistatic const struct regulator_ops tps65912_ops_dcdc = {
618c2ecf20Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
628c2ecf20Sopenharmony_ci	.enable			= regulator_enable_regmap,
638c2ecf20Sopenharmony_ci	.disable		= regulator_disable_regmap,
648c2ecf20Sopenharmony_ci	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
658c2ecf20Sopenharmony_ci	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
668c2ecf20Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear_range,
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* Operations permitted on LDOx */
708c2ecf20Sopenharmony_cistatic const struct regulator_ops tps65912_ops_ldo = {
718c2ecf20Sopenharmony_ci	.is_enabled		= regulator_is_enabled_regmap,
728c2ecf20Sopenharmony_ci	.enable			= regulator_enable_regmap,
738c2ecf20Sopenharmony_ci	.disable		= regulator_disable_regmap,
748c2ecf20Sopenharmony_ci	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
758c2ecf20Sopenharmony_ci	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
768c2ecf20Sopenharmony_ci	.list_voltage		= regulator_list_voltage_linear_range,
778c2ecf20Sopenharmony_ci	.map_voltage		= regulator_map_voltage_linear_range,
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic const struct regulator_desc regulators[] = {
818c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("DCDC1", DCDC1, "dcdc1", tps65912_ops_dcdc,
828c2ecf20Sopenharmony_ci			   TPS65912_DCDC1_OP, TPS65912_DCDC1_CTRL,
838c2ecf20Sopenharmony_ci			   tps65912_dcdc_ranges),
848c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("DCDC2", DCDC2, "dcdc2", tps65912_ops_dcdc,
858c2ecf20Sopenharmony_ci			   TPS65912_DCDC2_OP, TPS65912_DCDC2_CTRL,
868c2ecf20Sopenharmony_ci			   tps65912_dcdc_ranges),
878c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("DCDC3", DCDC3, "dcdc3", tps65912_ops_dcdc,
888c2ecf20Sopenharmony_ci			   TPS65912_DCDC3_OP, TPS65912_DCDC3_CTRL,
898c2ecf20Sopenharmony_ci			   tps65912_dcdc_ranges),
908c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("DCDC4", DCDC4, "dcdc4", tps65912_ops_dcdc,
918c2ecf20Sopenharmony_ci			   TPS65912_DCDC4_OP, TPS65912_DCDC4_CTRL,
928c2ecf20Sopenharmony_ci			   tps65912_dcdc_ranges),
938c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO1", LDO1, "ldo1", tps65912_ops_ldo,
948c2ecf20Sopenharmony_ci			   TPS65912_LDO1_OP, TPS65912_LDO1_AVS,
958c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
968c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO2", LDO2, "ldo2", tps65912_ops_ldo,
978c2ecf20Sopenharmony_ci			   TPS65912_LDO2_OP, TPS65912_LDO2_AVS,
988c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
998c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO3", LDO3, "ldo3", tps65912_ops_ldo,
1008c2ecf20Sopenharmony_ci			   TPS65912_LDO3_OP, TPS65912_LDO3_AVS,
1018c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1028c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO4", LDO4, "ldo4", tps65912_ops_ldo,
1038c2ecf20Sopenharmony_ci			   TPS65912_LDO4_OP, TPS65912_LDO4_AVS,
1048c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1058c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO5", LDO5, "ldo5", tps65912_ops_ldo,
1068c2ecf20Sopenharmony_ci			   TPS65912_LDO5, TPS65912_LDO5,
1078c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1088c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO6", LDO6, "ldo6", tps65912_ops_ldo,
1098c2ecf20Sopenharmony_ci			   TPS65912_LDO6, TPS65912_LDO6,
1108c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1118c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO7", LDO7, "ldo7", tps65912_ops_ldo,
1128c2ecf20Sopenharmony_ci			   TPS65912_LDO7, TPS65912_LDO7,
1138c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1148c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO8", LDO8, "ldo8", tps65912_ops_ldo,
1158c2ecf20Sopenharmony_ci			   TPS65912_LDO8, TPS65912_LDO8,
1168c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1178c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO9", LDO9, "ldo9", tps65912_ops_ldo,
1188c2ecf20Sopenharmony_ci			   TPS65912_LDO9, TPS65912_LDO9,
1198c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1208c2ecf20Sopenharmony_ci	TPS65912_REGULATOR("LDO10", LDO10, "ldo10", tps65912_ops_ldo,
1218c2ecf20Sopenharmony_ci			   TPS65912_LDO10, TPS65912_LDO10,
1228c2ecf20Sopenharmony_ci			   tps65912_ldo_ranges),
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int tps65912_regulator_probe(struct platform_device *pdev)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct tps65912 *tps = dev_get_drvdata(pdev->dev.parent);
1288c2ecf20Sopenharmony_ci	struct regulator_config config = { };
1298c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
1308c2ecf20Sopenharmony_ci	int i;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, tps);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	config.dev = &pdev->dev;
1358c2ecf20Sopenharmony_ci	config.driver_data = tps;
1368c2ecf20Sopenharmony_ci	config.dev->of_node = tps->dev->of_node;
1378c2ecf20Sopenharmony_ci	config.regmap = tps->regmap;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
1408c2ecf20Sopenharmony_ci		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
1418c2ecf20Sopenharmony_ci					       &config);
1428c2ecf20Sopenharmony_ci		if (IS_ERR(rdev)) {
1438c2ecf20Sopenharmony_ci			dev_err(tps->dev, "failed to register %s regulator\n",
1448c2ecf20Sopenharmony_ci				pdev->name);
1458c2ecf20Sopenharmony_ci			return PTR_ERR(rdev);
1468c2ecf20Sopenharmony_ci		}
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return 0;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct platform_device_id tps65912_regulator_id_table[] = {
1538c2ecf20Sopenharmony_ci	{ "tps65912-regulator", },
1548c2ecf20Sopenharmony_ci	{ /* sentinel */ }
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, tps65912_regulator_id_table);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic struct platform_driver tps65912_regulator_driver = {
1598c2ecf20Sopenharmony_ci	.driver = {
1608c2ecf20Sopenharmony_ci		.name = "tps65912-regulator",
1618c2ecf20Sopenharmony_ci	},
1628c2ecf20Sopenharmony_ci	.probe = tps65912_regulator_probe,
1638c2ecf20Sopenharmony_ci	.id_table = tps65912_regulator_id_table,
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_cimodule_platform_driver(tps65912_regulator_driver);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
1688c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TPS65912 voltage regulator driver");
1698c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
170