1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2020 MediaTek Inc.
4 
5 #include <linux/err.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/of_.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/regulator/of_regulator.h>
12 #include <linux/soc/mediatek/mtk_dvfsrc.h>
13 
14 #define DVFSRC_ID_VCORE		0
15 #define DVFSRC_ID_VSCP		1
16 
17 #define MT_DVFSRC_REGULAR(match, _name,	_volt_table)	\
18 [DVFSRC_ID_##_name] = {					\
19 	.desc = {					\
20 		.name = match,				\
21 		.of_match = of_match_ptr(match),	\
22 		.ops = &dvfsrc_vcore_ops,		\
23 		.type = REGULATOR_VOLTAGE,		\
24 		.id = DVFSRC_ID_##_name,		\
25 		.owner = THIS_MODULE,			\
26 		.n_voltages = ARRAY_SIZE(_volt_table),	\
27 		.volt_table = _volt_table,		\
28 	},	\
29 }
30 
31 /*
32  * DVFSRC regulators' information
33  *
34  * @desc: standard fields of regulator description.
35  * @voltage_selector:  Selector used for get_voltage_sel() and
36  *			   set_voltage_sel() callbacks
37  */
38 
39 struct dvfsrc_regulator {
40 	struct regulator_desc	desc;
41 };
42 
43 /*
44  * MTK DVFSRC regulators' init data
45  *
46  * @size: num of regulators
47  * @regulator_info: regulator info.
48  */
49 struct dvfsrc_regulator_init_data {
50 	u32 size;
51 	struct dvfsrc_regulator *regulator_info;
52 };
53 
to_dvfsrc_dev(struct regulator_dev * rdev)54 static inline struct device *to_dvfsrc_dev(struct regulator_dev *rdev)
55 {
56 	return rdev_get_dev(rdev)->parent;
57 }
58 
dvfsrc_set_voltage_sel(struct regulator_dev * rdev,unsigned int selector)59 static int dvfsrc_set_voltage_sel(struct regulator_dev *rdev,
60 				  unsigned int selector)
61 {
62 	struct device *dvfsrc_dev = to_dvfsrc_dev(rdev);
63 	int id = rdev_get_id(rdev);
64 
65 	if (id == DVFSRC_ID_VCORE)
66 		mtk_dvfsrc_send_request(dvfsrc_dev,
67 					MTK_DVFSRC_CMD_VCORE_REQUEST,
68 					selector);
69 	else if (id == DVFSRC_ID_VSCP)
70 		mtk_dvfsrc_send_request(dvfsrc_dev,
71 					MTK_DVFSRC_CMD_VSCP_REQUEST,
72 					selector);
73 	else
74 		return -EINVAL;
75 
76 	return 0;
77 }
78 
dvfsrc_get_voltage_sel(struct regulator_dev * rdev)79 static int dvfsrc_get_voltage_sel(struct regulator_dev *rdev)
80 {
81 	struct device *dvfsrc_dev = to_dvfsrc_dev(rdev);
82 	int id = rdev_get_id(rdev);
83 	int val, ret;
84 
85 	if (id == DVFSRC_ID_VCORE)
86 		ret = mtk_dvfsrc_query_info(dvfsrc_dev,
87 					    MTK_DVFSRC_CMD_VCORE_LEVEL_QUERY,
88 					    &val);
89 	else if (id == DVFSRC_ID_VSCP)
90 		ret = mtk_dvfsrc_query_info(dvfsrc_dev,
91 					    MTK_DVFSRC_CMD_VSCP_LEVEL_QUERY,
92 					    &val);
93 	else
94 		return -EINVAL;
95 
96 	if (ret != 0)
97 		return ret;
98 
99 	return val;
100 }
101 
102 static const struct regulator_ops dvfsrc_vcore_ops = {
103 	.list_voltage = regulator_list_voltage_table,
104 	.get_voltage_sel = dvfsrc_get_voltage_sel,
105 	.set_voltage_sel = dvfsrc_set_voltage_sel,
106 };
107 
108 static const unsigned int mt8183_voltages[] = {
109 	725000,
110 	800000,
111 };
112 
113 static struct dvfsrc_regulator mt8183_regulators[] = {
114 	MT_DVFSRC_REGULAR("dvfsrc-vcore", VCORE,
115 			  mt8183_voltages),
116 };
117 
118 static const struct dvfsrc_regulator_init_data regulator_mt8183_data = {
119 	.size = ARRAY_SIZE(mt8183_regulators),
120 	.regulator_info = &mt8183_regulators[0],
121 };
122 
123 static const unsigned int mt6873_voltages[] = {
124 	575000,
125 	600000,
126 	650000,
127 	725000,
128 };
129 
130 static struct dvfsrc_regulator mt6873_regulators[] = {
131 	MT_DVFSRC_REGULAR("dvfsrc-vcore", VCORE,
132 			  mt6873_voltages),
133 	MT_DVFSRC_REGULAR("dvfsrc-vscp", VSCP,
134 			  mt6873_voltages),
135 };
136 
137 static const struct dvfsrc_regulator_init_data regulator_mt6873_data = {
138 	.size = ARRAY_SIZE(mt6873_regulators),
139 	.regulator_info = &mt6873_regulators[0],
140 };
141 
142 static const struct of_device_id mtk_dvfsrc_regulator_match[] = {
143 	{
144 		.compatible = "mediatek,mt8183-dvfsrc",
145 		.data = &regulator_mt8183_data,
146 	}, {
147 		.compatible = "mediatek,mt8192-dvfsrc",
148 		.data = &regulator_mt6873_data,
149 	}, {
150 		.compatible = "mediatek,mt6873-dvfsrc",
151 		.data = &regulator_mt6873_data,
152 	}, {
153 		/* sentinel */
154 	},
155 };
156 MODULE_DEVICE_TABLE(of, mtk_dvfsrc_regulator_match);
157 
dvfsrc_vcore_regulator_probe(struct platform_device * pdev)158 static int dvfsrc_vcore_regulator_probe(struct platform_device *pdev)
159 {
160 	const struct of_device_id *match;
161 	struct device *dev = &pdev->dev;
162 	struct regulator_config config = { };
163 	struct regulator_dev *rdev;
164 	const struct dvfsrc_regulator_init_data *regulator_init_data;
165 	struct dvfsrc_regulator *mt_regulators;
166 	int i;
167 
168 	match = of_match_node(mtk_dvfsrc_regulator_match, dev->parent->of_node);
169 
170 	if (!match) {
171 		dev_err(dev, "invalid compatible string\n");
172 		return -ENODEV;
173 	}
174 
175 	regulator_init_data = match->data;
176 
177 	mt_regulators = regulator_init_data->regulator_info;
178 	for (i = 0; i < regulator_init_data->size; i++) {
179 		config.dev = dev->parent;
180 		config.driver_data = (mt_regulators + i);
181 		rdev = devm_regulator_register(dev, &(mt_regulators + i)->desc,
182 					       &config);
183 		if (IS_ERR(rdev)) {
184 			dev_err(dev, "failed to register %s\n",
185 				(mt_regulators + i)->desc.name);
186 			return PTR_ERR(rdev);
187 		}
188 	}
189 
190 	return 0;
191 }
192 
193 static struct platform_driver mtk_dvfsrc_regulator_driver = {
194 	.driver = {
195 		.name  = "mtk-dvfsrc-regulator",
196 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
197 	},
198 	.probe = dvfsrc_vcore_regulator_probe,
199 };
200 
mtk_dvfsrc_regulator_init(void)201 static int __init mtk_dvfsrc_regulator_init(void)
202 {
203 	return platform_driver_register(&mtk_dvfsrc_regulator_driver);
204 }
205 subsys_initcall(mtk_dvfsrc_regulator_init);
206 
mtk_dvfsrc_regulator_exit(void)207 static void __exit mtk_dvfsrc_regulator_exit(void)
208 {
209 	platform_driver_unregister(&mtk_dvfsrc_regulator_driver);
210 }
211 module_exit(mtk_dvfsrc_regulator_exit);
212 
213 MODULE_AUTHOR("Arvin wang <arvin.wang@mediatek.com>");
214 MODULE_LICENSE("GPL v2");
215