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>
9 #include <linux/of_device.h>
10 #include <linux/of_platform.h>
11 #include <linux/regulator/driver.h>
12 #include <linux/regulator/of_regulator.h>
13 #include <linux/soc/mediatek/mtk_dvfsrc.h>
15 #define DVFSRC_ID_VCORE 0
16 #define DVFSRC_ID_VSCP 1
18 #define MT_DVFSRC_REGULAR(match, _name, _volt_table) \
19 [DVFSRC_ID_##_name] = { \
22 .of_match = of_match_ptr(match), \
23 .ops = &dvfsrc_vcore_ops, \
24 .type = REGULATOR_VOLTAGE, \
25 .id = DVFSRC_ID_##_name, \
26 .owner = THIS_MODULE, \
27 .n_voltages = ARRAY_SIZE(_volt_table), \
28 .volt_table = _volt_table, \
33 * DVFSRC regulators' information
35 * @desc: standard fields of regulator description.
36 * @voltage_selector: Selector used for get_voltage_sel() and
37 * set_voltage_sel() callbacks
40 struct dvfsrc_regulator {
41 struct regulator_desc desc;
45 * MTK DVFSRC regulators' init data
47 * @size: num of regulators
48 * @regulator_info: regulator info.
50 struct dvfsrc_regulator_init_data {
52 struct dvfsrc_regulator *regulator_info;
55 static inline struct device *to_dvfsrc_dev(struct regulator_dev *rdev)
57 return rdev_get_dev(rdev)->parent;
60 static int dvfsrc_set_voltage_sel(struct regulator_dev *rdev,
61 unsigned int selector)
63 struct device *dvfsrc_dev = to_dvfsrc_dev(rdev);
64 int id = rdev_get_id(rdev);
66 if (id == DVFSRC_ID_VCORE)
67 mtk_dvfsrc_send_request(dvfsrc_dev,
68 MTK_DVFSRC_CMD_VCORE_REQUEST,
70 else if (id == DVFSRC_ID_VSCP)
71 mtk_dvfsrc_send_request(dvfsrc_dev,
72 MTK_DVFSRC_CMD_VSCP_REQUEST,
80 static int dvfsrc_get_voltage_sel(struct regulator_dev *rdev)
82 struct device *dvfsrc_dev = to_dvfsrc_dev(rdev);
83 int id = rdev_get_id(rdev);
86 if (id == DVFSRC_ID_VCORE)
87 ret = mtk_dvfsrc_query_info(dvfsrc_dev,
88 MTK_DVFSRC_CMD_VCORE_LEVEL_QUERY,
90 else if (id == DVFSRC_ID_VSCP)
91 ret = mtk_dvfsrc_query_info(dvfsrc_dev,
92 MTK_DVFSRC_CMD_VSCP_LEVEL_QUERY,
103 static const struct regulator_ops dvfsrc_vcore_ops = {
104 .list_voltage = regulator_list_voltage_table,
105 .get_voltage_sel = dvfsrc_get_voltage_sel,
106 .set_voltage_sel = dvfsrc_set_voltage_sel,
109 static const unsigned int mt8183_voltages[] = {
114 static struct dvfsrc_regulator mt8183_regulators[] = {
115 MT_DVFSRC_REGULAR("dvfsrc-vcore", VCORE,
119 static const struct dvfsrc_regulator_init_data regulator_mt8183_data = {
120 .size = ARRAY_SIZE(mt8183_regulators),
121 .regulator_info = &mt8183_regulators[0],
124 static const unsigned int mt6873_voltages[] = {
131 static struct dvfsrc_regulator mt6873_regulators[] = {
132 MT_DVFSRC_REGULAR("dvfsrc-vcore", VCORE,
134 MT_DVFSRC_REGULAR("dvfsrc-vscp", VSCP,
138 static const struct dvfsrc_regulator_init_data regulator_mt6873_data = {
139 .size = ARRAY_SIZE(mt6873_regulators),
140 .regulator_info = &mt6873_regulators[0],
143 static const struct of_device_id mtk_dvfsrc_regulator_match[] = {
145 .compatible = "mediatek,mt8183-dvfsrc",
146 .data = ®ulator_mt8183_data,
148 .compatible = "mediatek,mt8192-dvfsrc",
149 .data = ®ulator_mt6873_data,
151 .compatible = "mediatek,mt6873-dvfsrc",
152 .data = ®ulator_mt6873_data,
157 MODULE_DEVICE_TABLE(of, mtk_dvfsrc_regulator_match);
159 static int dvfsrc_vcore_regulator_probe(struct platform_device *pdev)
161 const struct of_device_id *match;
162 struct device *dev = &pdev->dev;
163 struct regulator_config config = { };
164 struct regulator_dev *rdev;
165 const struct dvfsrc_regulator_init_data *regulator_init_data;
166 struct dvfsrc_regulator *mt_regulators;
169 match = of_match_node(mtk_dvfsrc_regulator_match, dev->parent->of_node);
172 dev_err(dev, "invalid compatible string\n");
176 regulator_init_data = match->data;
178 mt_regulators = regulator_init_data->regulator_info;
179 for (i = 0; i < regulator_init_data->size; i++) {
180 config.dev = dev->parent;
181 config.driver_data = (mt_regulators + i);
182 rdev = devm_regulator_register(dev, &(mt_regulators + i)->desc,
185 dev_err(dev, "failed to register %s\n",
186 (mt_regulators + i)->desc.name);
187 return PTR_ERR(rdev);
194 static struct platform_driver mtk_dvfsrc_regulator_driver = {
196 .name = "mtk-dvfsrc-regulator",
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");