1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Regulator driver for TPS68470 PMIC
4 //
5 // Copyright (c) 2021 Red Hat Inc.
6 // Copyright (C) 2018 Intel Corporation
7 //
8 // Authors:
9 //	Hans de Goede <hdegoede@redhat.com>
10 //	Zaikuo Wang <zaikuo.wang@intel.com>
11 //	Tianshu Qiu <tian.shu.qiu@intel.com>
12 //	Jian Xu Zheng <jian.xu.zheng@intel.com>
13 //	Yuning Pu <yuning.pu@intel.com>
14 //	Rajmohan Mani <rajmohan.mani@intel.com>
15 
16 #include <linux/clk.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/mfd/tps68470.h>
22 #include <linux/module.h>
23 #include <linux/platform_data/tps68470.h>
24 #include <linux/platform_device.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
27 
28 struct tps68470_regulator_data {
29 	struct clk *clk;
30 };
31 
32 #define TPS68470_REGULATOR(_name, _id, _ops, _n,			\
33 			   _vr, _vm, _er, _em, _lr, _nlr)		\
34 	[TPS68470_ ## _name] = {					\
35 		.name			= # _name,			\
36 		.id			= _id,				\
37 		.ops			= &_ops,			\
38 		.n_voltages		= _n,				\
39 		.type			= REGULATOR_VOLTAGE,		\
40 		.owner			= THIS_MODULE,			\
41 		.vsel_reg		= _vr,				\
42 		.vsel_mask		= _vm,				\
43 		.enable_reg		= _er,				\
44 		.enable_mask		= _em,				\
45 		.linear_ranges		= _lr,				\
46 		.n_linear_ranges	= _nlr,				\
47 	}
48 
49 static const struct linear_range tps68470_ldo_ranges[] = {
50 	REGULATOR_LINEAR_RANGE(875000, 0, 125, 17800),
51 };
52 
53 static const struct linear_range tps68470_core_ranges[] = {
54 	REGULATOR_LINEAR_RANGE(900000, 0, 42, 25000),
55 };
56 
57 static int tps68470_regulator_enable(struct regulator_dev *rdev)
58 {
59 	struct tps68470_regulator_data *data = rdev->reg_data;
60 	int ret;
61 
62 	/* The Core buck regulator needs the PMIC's PLL to be enabled */
63 	if (rdev->desc->id == TPS68470_CORE) {
64 		ret = clk_prepare_enable(data->clk);
65 		if (ret) {
66 			dev_err(&rdev->dev, "Error enabling TPS68470 clock\n");
67 			return ret;
68 		}
69 	}
70 
71 	return regulator_enable_regmap(rdev);
72 }
73 
74 static int tps68470_regulator_disable(struct regulator_dev *rdev)
75 {
76 	struct tps68470_regulator_data *data = rdev->reg_data;
77 
78 	if (rdev->desc->id == TPS68470_CORE)
79 		clk_disable_unprepare(data->clk);
80 
81 	return regulator_disable_regmap(rdev);
82 }
83 
84 /* Operations permitted on DCDCx, LDO2, LDO3 and LDO4 */
85 static const struct regulator_ops tps68470_regulator_ops = {
86 	.is_enabled		= regulator_is_enabled_regmap,
87 	.enable			= tps68470_regulator_enable,
88 	.disable		= tps68470_regulator_disable,
89 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
90 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
91 	.list_voltage		= regulator_list_voltage_linear_range,
92 	.map_voltage		= regulator_map_voltage_linear_range,
93 };
94 
95 static const struct regulator_ops tps68470_always_on_reg_ops = {
96 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
97 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
98 	.list_voltage		= regulator_list_voltage_linear_range,
99 	.map_voltage		= regulator_map_voltage_linear_range,
100 };
101 
102 static const struct regulator_desc regulators[] = {
103 	TPS68470_REGULATOR(CORE, TPS68470_CORE, tps68470_regulator_ops, 43,
104 			   TPS68470_REG_VDVAL, TPS68470_VDVAL_DVOLT_MASK,
105 			   TPS68470_REG_VDCTL, TPS68470_VDCTL_EN_MASK,
106 			   tps68470_core_ranges, ARRAY_SIZE(tps68470_core_ranges)),
107 	TPS68470_REGULATOR(ANA, TPS68470_ANA, tps68470_regulator_ops, 126,
108 			   TPS68470_REG_VAVAL, TPS68470_VAVAL_AVOLT_MASK,
109 			   TPS68470_REG_VACTL, TPS68470_VACTL_EN_MASK,
110 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
111 	TPS68470_REGULATOR(VCM, TPS68470_VCM, tps68470_regulator_ops, 126,
112 			   TPS68470_REG_VCMVAL, TPS68470_VCMVAL_VCVOLT_MASK,
113 			   TPS68470_REG_VCMCTL, TPS68470_VCMCTL_EN_MASK,
114 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
115 	TPS68470_REGULATOR(VIO, TPS68470_VIO, tps68470_always_on_reg_ops, 126,
116 			   TPS68470_REG_VIOVAL, TPS68470_VIOVAL_IOVOLT_MASK,
117 			   0, 0,
118 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
119 /*
120  * (1) This regulator must have the same voltage as VIO if S_IO LDO is used to
121  *     power a sensor/VCM which I2C is daisy chained behind the PMIC.
122  * (2) If there is no I2C daisy chain it can be set freely.
123  */
124 	TPS68470_REGULATOR(VSIO, TPS68470_VSIO, tps68470_regulator_ops, 126,
125 			   TPS68470_REG_VSIOVAL, TPS68470_VSIOVAL_IOVOLT_MASK,
126 			   TPS68470_REG_S_I2C_CTL, TPS68470_S_I2C_CTL_EN_MASK,
127 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
128 	TPS68470_REGULATOR(AUX1, TPS68470_AUX1, tps68470_regulator_ops, 126,
129 			   TPS68470_REG_VAUX1VAL, TPS68470_VAUX1VAL_AUX1VOLT_MASK,
130 			   TPS68470_REG_VAUX1CTL, TPS68470_VAUX1CTL_EN_MASK,
131 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
132 	TPS68470_REGULATOR(AUX2, TPS68470_AUX2, tps68470_regulator_ops, 126,
133 			   TPS68470_REG_VAUX2VAL, TPS68470_VAUX2VAL_AUX2VOLT_MASK,
134 			   TPS68470_REG_VAUX2CTL, TPS68470_VAUX2CTL_EN_MASK,
135 			   tps68470_ldo_ranges, ARRAY_SIZE(tps68470_ldo_ranges)),
136 };
137 
138 static int tps68470_regulator_probe(struct platform_device *pdev)
139 {
140 	struct device *dev = &pdev->dev;
141 	struct tps68470_regulator_platform_data *pdata = dev_get_platdata(dev);
142 	struct tps68470_regulator_data *data;
143 	struct regulator_config config = { };
144 	struct regulator_dev *rdev;
145 	int i;
146 
147 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
148 	if (!data)
149 		return -ENOMEM;
150 
151 	data->clk = devm_clk_get(dev, "tps68470-clk");
152 	if (IS_ERR(data->clk))
153 		return dev_err_probe(dev, PTR_ERR(data->clk), "getting tps68470-clk\n");
154 
155 	config.dev = dev->parent;
156 	config.regmap = dev_get_drvdata(dev->parent);
157 	config.driver_data = data;
158 
159 	for (i = 0; i < TPS68470_NUM_REGULATORS; i++) {
160 		if (pdata)
161 			config.init_data = pdata->reg_init_data[i];
162 		else
163 			config.init_data = NULL;
164 
165 		rdev = devm_regulator_register(dev, &regulators[i], &config);
166 		if (IS_ERR(rdev))
167 			return dev_err_probe(dev, PTR_ERR(rdev),
168 					     "registering %s regulator\n",
169 					     regulators[i].name);
170 	}
171 
172 	return 0;
173 }
174 
175 static struct platform_driver tps68470_regulator_driver = {
176 	.driver = {
177 		.name = "tps68470-regulator",
178 	},
179 	.probe = tps68470_regulator_probe,
180 };
181 
182 /*
183  * The ACPI tps68470 probe-ordering depends on the clk/gpio/regulator drivers
184  * registering before the drivers for the camera-sensors which use them bind.
185  * subsys_initcall() ensures this when the drivers are builtin.
186  */
187 static int __init tps68470_regulator_init(void)
188 {
189 	return platform_driver_register(&tps68470_regulator_driver);
190 }
191 subsys_initcall(tps68470_regulator_init);
192 
193 static void __exit tps68470_regulator_exit(void)
194 {
195 	platform_driver_unregister(&tps68470_regulator_driver);
196 }
197 module_exit(tps68470_regulator_exit);
198 
199 MODULE_ALIAS("platform:tps68470-regulator");
200 MODULE_DESCRIPTION("TPS68470 voltage regulator driver");
201 MODULE_LICENSE("GPL v2");
202