stub-regulator.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, 2016, 2018, 2020, The Linux Foundation.
  4. * All rights reserved.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/regulator/debug-regulator.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/regulator/of_regulator.h>
  19. struct regulator_stub {
  20. struct regulator_desc rdesc;
  21. int voltage;
  22. bool enabled;
  23. unsigned int mode;
  24. int hpm_min_load_uA;
  25. int system_load_uA;
  26. };
  27. static int regulator_stub_set_voltage(struct regulator_dev *rdev, int min_uV,
  28. int max_uV, unsigned int *selector)
  29. {
  30. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  31. vreg->voltage = min_uV;
  32. return 0;
  33. }
  34. static int regulator_stub_get_voltage(struct regulator_dev *rdev)
  35. {
  36. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  37. return vreg->voltage;
  38. }
  39. static int regulator_stub_list_voltage(struct regulator_dev *rdev,
  40. unsigned int selector)
  41. {
  42. struct regulation_constraints *constraints = rdev->constraints;
  43. if (selector >= 2)
  44. return -EINVAL;
  45. else if (selector == 0)
  46. return constraints->min_uV;
  47. else
  48. return constraints->max_uV;
  49. }
  50. static unsigned int regulator_stub_get_mode(struct regulator_dev *rdev)
  51. {
  52. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  53. return vreg->mode;
  54. }
  55. static int regulator_stub_set_mode(struct regulator_dev *rdev,
  56. unsigned int mode)
  57. {
  58. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  59. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
  60. dev_err(&rdev->dev, "%s: invalid mode requested %u\n",
  61. __func__, mode);
  62. return -EINVAL;
  63. }
  64. vreg->mode = mode;
  65. return 0;
  66. }
  67. static unsigned int regulator_stub_get_optimum_mode(struct regulator_dev *rdev,
  68. int input_uV, int output_uV, int load_uA)
  69. {
  70. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  71. unsigned int mode;
  72. if (load_uA + vreg->system_load_uA >= vreg->hpm_min_load_uA)
  73. mode = REGULATOR_MODE_NORMAL;
  74. else
  75. mode = REGULATOR_MODE_IDLE;
  76. return mode;
  77. }
  78. static int regulator_stub_enable(struct regulator_dev *rdev)
  79. {
  80. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  81. vreg->enabled = true;
  82. return 0;
  83. }
  84. static int regulator_stub_disable(struct regulator_dev *rdev)
  85. {
  86. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  87. vreg->enabled = false;
  88. return 0;
  89. }
  90. static int regulator_stub_is_enabled(struct regulator_dev *rdev)
  91. {
  92. struct regulator_stub *vreg = rdev_get_drvdata(rdev);
  93. return vreg->enabled;
  94. }
  95. static const struct regulator_ops regulator_stub_ops = {
  96. .enable = regulator_stub_enable,
  97. .disable = regulator_stub_disable,
  98. .is_enabled = regulator_stub_is_enabled,
  99. .set_voltage = regulator_stub_set_voltage,
  100. .get_voltage = regulator_stub_get_voltage,
  101. .list_voltage = regulator_stub_list_voltage,
  102. .set_mode = regulator_stub_set_mode,
  103. .get_mode = regulator_stub_get_mode,
  104. .get_optimum_mode = regulator_stub_get_optimum_mode,
  105. };
  106. static int regulator_stub_probe(struct platform_device *pdev)
  107. {
  108. struct regulator_config reg_config = {};
  109. struct regulator_init_data *init_data;
  110. struct device *dev = &pdev->dev;
  111. struct regulator_stub *vreg;
  112. struct regulator_dev *rdev;
  113. int rc;
  114. if (!dev->of_node) {
  115. dev_err(dev, "%s: device node missing\n", __func__);
  116. return -ENODEV;
  117. }
  118. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  119. if (!vreg)
  120. return -ENOMEM;
  121. init_data = of_get_regulator_init_data(dev, dev->of_node, &vreg->rdesc);
  122. if (!init_data)
  123. return -ENOMEM;
  124. if (!init_data->constraints.name) {
  125. dev_err(dev, "%s: regulator name not specified\n", __func__);
  126. return -EINVAL;
  127. }
  128. of_property_read_u32(dev->of_node, "qcom,system-load",
  129. &vreg->system_load_uA);
  130. of_property_read_u32(dev->of_node, "qcom,hpm-min-load",
  131. &vreg->hpm_min_load_uA);
  132. vreg->voltage = init_data->constraints.min_uV;
  133. if (vreg->system_load_uA >= vreg->hpm_min_load_uA)
  134. vreg->mode = REGULATOR_MODE_NORMAL;
  135. else
  136. vreg->mode = REGULATOR_MODE_IDLE;
  137. init_data->constraints.input_uV = init_data->constraints.max_uV;
  138. init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_MODE |
  139. REGULATOR_CHANGE_DRMS;
  140. init_data->constraints.valid_modes_mask
  141. = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
  142. /*
  143. * Ensure that voltage set points are handled correctly for regulators
  144. * which have a specified voltage constraint range, as well as those
  145. * that do not.
  146. */
  147. if (init_data->constraints.min_uV == 0 &&
  148. init_data->constraints.max_uV == 0)
  149. vreg->rdesc.n_voltages = 0;
  150. else
  151. vreg->rdesc.n_voltages = 2;
  152. vreg->rdesc.name = devm_kstrdup(dev, init_data->constraints.name,
  153. GFP_KERNEL);
  154. if (!vreg->rdesc.name)
  155. return -ENOMEM;
  156. vreg->rdesc.supply_name = "parent";
  157. vreg->rdesc.ops = &regulator_stub_ops;
  158. vreg->rdesc.owner = THIS_MODULE;
  159. vreg->rdesc.type = REGULATOR_VOLTAGE;
  160. reg_config.dev = dev;
  161. reg_config.init_data = init_data;
  162. reg_config.driver_data = vreg;
  163. reg_config.of_node = dev->of_node;
  164. rdev = devm_regulator_register(dev, &vreg->rdesc, &reg_config);
  165. if (IS_ERR(rdev)) {
  166. rc = PTR_ERR(rdev);
  167. if (rc != -EPROBE_DEFER)
  168. dev_err(dev, "%s: regulator_register failed\n",
  169. __func__);
  170. return rc;
  171. }
  172. rc = devm_regulator_debug_register(dev, rdev);
  173. if (rc)
  174. dev_err(dev, "failed to register debug regulator, rc=%d\n", rc);
  175. return 0;
  176. }
  177. static const struct of_device_id regulator_stub_match_table[] = {
  178. { .compatible = "qcom,stub-regulator", },
  179. {}
  180. };
  181. static struct platform_driver regulator_stub_driver = {
  182. .driver = {
  183. .name = "stub-regulator",
  184. .of_match_table = of_match_ptr(regulator_stub_match_table),
  185. },
  186. .probe = regulator_stub_probe,
  187. };
  188. int __init regulator_stub_init(void)
  189. {
  190. return platform_driver_register(&regulator_stub_driver);
  191. }
  192. postcore_initcall(regulator_stub_init);
  193. static void __exit regulator_stub_exit(void)
  194. {
  195. platform_driver_unregister(&regulator_stub_driver);
  196. }
  197. module_exit(regulator_stub_exit);
  198. MODULE_LICENSE("GPL v2");
  199. MODULE_DESCRIPTION("stub regulator driver");