mtk-dvfsrc-regulator.c 5.0 KB

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