1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2020 MediaTek Inc.
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/regulator/of_regulator.h>
12 #include <linux/soc/mediatek/mtk_dvfsrc.h>
14 #define DVFSRC_ID_VCORE 0
15 #define DVFSRC_ID_VSCP 1
17 #define MT_DVFSRC_REGULAR(match, _name, _volt_table) \
18 [DVFSRC_ID_##_name] = { \
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, \
32 * DVFSRC regulators' information
34 * @desc: standard fields of regulator description.
35 * @voltage_selector: Selector used for get_voltage_sel() and
36 * set_voltage_sel() callbacks
39 struct dvfsrc_regulator {
40 struct regulator_desc desc;
44 * MTK DVFSRC regulators' init data
46 * @size: num of regulators
47 * @regulator_info: regulator info.
49 struct dvfsrc_regulator_init_data {
51 struct dvfsrc_regulator *regulator_info;
54 static inline struct device *to_dvfsrc_dev(struct regulator_dev *rdev)
56 return rdev_get_dev(rdev)->parent;
59 static int dvfsrc_set_voltage_sel(struct regulator_dev *rdev,
60 unsigned int selector)
62 struct device *dvfsrc_dev = to_dvfsrc_dev(rdev);
63 int id = rdev_get_id(rdev);
65 if (id == DVFSRC_ID_VCORE)
66 mtk_dvfsrc_send_request(dvfsrc_dev,
67 MTK_DVFSRC_CMD_VCORE_REQUEST,
69 else if (id == DVFSRC_ID_VSCP)
70 mtk_dvfsrc_send_request(dvfsrc_dev,
71 MTK_DVFSRC_CMD_VSCP_REQUEST,
79 static int dvfsrc_get_voltage_sel(struct regulator_dev *rdev)
81 struct device *dvfsrc_dev = to_dvfsrc_dev(rdev);
82 int id = rdev_get_id(rdev);
85 if (id == DVFSRC_ID_VCORE)
86 ret = mtk_dvfsrc_query_info(dvfsrc_dev,
87 MTK_DVFSRC_CMD_VCORE_LEVEL_QUERY,
89 else if (id == DVFSRC_ID_VSCP)
90 ret = mtk_dvfsrc_query_info(dvfsrc_dev,
91 MTK_DVFSRC_CMD_VSCP_LEVEL_QUERY,
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,
108 static const unsigned int mt8183_voltages[] = {
113 static struct dvfsrc_regulator mt8183_regulators[] = {
114 MT_DVFSRC_REGULAR("dvfsrc-vcore", VCORE,
118 static const struct dvfsrc_regulator_init_data regulator_mt8183_data = {
119 .size = ARRAY_SIZE(mt8183_regulators),
120 .regulator_info = &mt8183_regulators[0],
123 static const unsigned int mt6873_voltages[] = {
130 static struct dvfsrc_regulator mt6873_regulators[] = {
131 MT_DVFSRC_REGULAR("dvfsrc-vcore", VCORE,
133 MT_DVFSRC_REGULAR("dvfsrc-vscp", VSCP,
137 static const struct dvfsrc_regulator_init_data regulator_mt6873_data = {
138 .size = ARRAY_SIZE(mt6873_regulators),
139 .regulator_info = &mt6873_regulators[0],
142 static const struct of_device_id mtk_dvfsrc_regulator_match[] = {
144 .compatible = "mediatek,mt8183-dvfsrc",
145 .data = ®ulator_mt8183_data,
147 .compatible = "mediatek,mt8192-dvfsrc",
148 .data = ®ulator_mt6873_data,
150 .compatible = "mediatek,mt6873-dvfsrc",
151 .data = ®ulator_mt6873_data,
156 MODULE_DEVICE_TABLE(of, mtk_dvfsrc_regulator_match);
158 static int dvfsrc_vcore_regulator_probe(struct platform_device *pdev)
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;
168 match = of_match_node(mtk_dvfsrc_regulator_match, dev->parent->of_node);
171 dev_err(dev, "invalid compatible string\n");
175 regulator_init_data = match->data;
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,
184 dev_err(dev, "failed to register %s\n",
185 (mt_regulators + i)->desc.name);
186 return PTR_ERR(rdev);
193 static struct platform_driver mtk_dvfsrc_regulator_driver = {
195 .name = "mtk-dvfsrc-regulator",
196 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
198 .probe = dvfsrc_vcore_regulator_probe,
201 static int __init mtk_dvfsrc_regulator_init(void)
203 return platform_driver_register(&mtk_dvfsrc_regulator_driver);
205 subsys_initcall(mtk_dvfsrc_regulator_init);
207 static void __exit mtk_dvfsrc_regulator_exit(void)
209 platform_driver_unregister(&mtk_dvfsrc_regulator_driver);
211 module_exit(mtk_dvfsrc_regulator_exit);
213 MODULE_AUTHOR("Arvin wang <arvin.wang@mediatek.com>");
214 MODULE_LICENSE("GPL v2");