virtual.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * reg-virtual-consumer.c
  4. *
  5. * Copyright 2008 Wolfson Microelectronics PLC.
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/mutex.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. struct virtual_consumer_data {
  16. struct mutex lock;
  17. struct regulator *regulator;
  18. bool enabled;
  19. int min_uV;
  20. int max_uV;
  21. int min_uA;
  22. int max_uA;
  23. unsigned int mode;
  24. };
  25. static void update_voltage_constraints(struct device *dev,
  26. struct virtual_consumer_data *data)
  27. {
  28. int ret;
  29. if (data->min_uV && data->max_uV
  30. && data->min_uV <= data->max_uV) {
  31. dev_dbg(dev, "Requesting %d-%duV\n",
  32. data->min_uV, data->max_uV);
  33. ret = regulator_set_voltage(data->regulator,
  34. data->min_uV, data->max_uV);
  35. if (ret != 0) {
  36. dev_err(dev,
  37. "regulator_set_voltage() failed: %d\n", ret);
  38. return;
  39. }
  40. }
  41. if (data->min_uV && data->max_uV && !data->enabled) {
  42. dev_dbg(dev, "Enabling regulator\n");
  43. ret = regulator_enable(data->regulator);
  44. if (ret == 0)
  45. data->enabled = true;
  46. else
  47. dev_err(dev, "regulator_enable() failed: %d\n",
  48. ret);
  49. }
  50. if (!(data->min_uV && data->max_uV) && data->enabled) {
  51. dev_dbg(dev, "Disabling regulator\n");
  52. ret = regulator_disable(data->regulator);
  53. if (ret == 0)
  54. data->enabled = false;
  55. else
  56. dev_err(dev, "regulator_disable() failed: %d\n",
  57. ret);
  58. }
  59. }
  60. static void update_current_limit_constraints(struct device *dev,
  61. struct virtual_consumer_data *data)
  62. {
  63. int ret;
  64. if (data->max_uA
  65. && data->min_uA <= data->max_uA) {
  66. dev_dbg(dev, "Requesting %d-%duA\n",
  67. data->min_uA, data->max_uA);
  68. ret = regulator_set_current_limit(data->regulator,
  69. data->min_uA, data->max_uA);
  70. if (ret != 0) {
  71. dev_err(dev,
  72. "regulator_set_current_limit() failed: %d\n",
  73. ret);
  74. return;
  75. }
  76. }
  77. if (data->max_uA && !data->enabled) {
  78. dev_dbg(dev, "Enabling regulator\n");
  79. ret = regulator_enable(data->regulator);
  80. if (ret == 0)
  81. data->enabled = true;
  82. else
  83. dev_err(dev, "regulator_enable() failed: %d\n",
  84. ret);
  85. }
  86. if (!(data->min_uA && data->max_uA) && data->enabled) {
  87. dev_dbg(dev, "Disabling regulator\n");
  88. ret = regulator_disable(data->regulator);
  89. if (ret == 0)
  90. data->enabled = false;
  91. else
  92. dev_err(dev, "regulator_disable() failed: %d\n",
  93. ret);
  94. }
  95. }
  96. static ssize_t show_min_uV(struct device *dev,
  97. struct device_attribute *attr, char *buf)
  98. {
  99. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  100. return sprintf(buf, "%d\n", data->min_uV);
  101. }
  102. static ssize_t set_min_uV(struct device *dev, struct device_attribute *attr,
  103. const char *buf, size_t count)
  104. {
  105. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  106. long val;
  107. if (kstrtol(buf, 10, &val) != 0)
  108. return count;
  109. mutex_lock(&data->lock);
  110. data->min_uV = val;
  111. update_voltage_constraints(dev, data);
  112. mutex_unlock(&data->lock);
  113. return count;
  114. }
  115. static ssize_t show_max_uV(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  119. return sprintf(buf, "%d\n", data->max_uV);
  120. }
  121. static ssize_t set_max_uV(struct device *dev, struct device_attribute *attr,
  122. const char *buf, size_t count)
  123. {
  124. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  125. long val;
  126. if (kstrtol(buf, 10, &val) != 0)
  127. return count;
  128. mutex_lock(&data->lock);
  129. data->max_uV = val;
  130. update_voltage_constraints(dev, data);
  131. mutex_unlock(&data->lock);
  132. return count;
  133. }
  134. static ssize_t show_min_uA(struct device *dev,
  135. struct device_attribute *attr, char *buf)
  136. {
  137. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  138. return sprintf(buf, "%d\n", data->min_uA);
  139. }
  140. static ssize_t set_min_uA(struct device *dev, struct device_attribute *attr,
  141. const char *buf, size_t count)
  142. {
  143. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  144. long val;
  145. if (kstrtol(buf, 10, &val) != 0)
  146. return count;
  147. mutex_lock(&data->lock);
  148. data->min_uA = val;
  149. update_current_limit_constraints(dev, data);
  150. mutex_unlock(&data->lock);
  151. return count;
  152. }
  153. static ssize_t show_max_uA(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  157. return sprintf(buf, "%d\n", data->max_uA);
  158. }
  159. static ssize_t set_max_uA(struct device *dev, struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  163. long val;
  164. if (kstrtol(buf, 10, &val) != 0)
  165. return count;
  166. mutex_lock(&data->lock);
  167. data->max_uA = val;
  168. update_current_limit_constraints(dev, data);
  169. mutex_unlock(&data->lock);
  170. return count;
  171. }
  172. static ssize_t show_mode(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  176. switch (data->mode) {
  177. case REGULATOR_MODE_FAST:
  178. return sprintf(buf, "fast\n");
  179. case REGULATOR_MODE_NORMAL:
  180. return sprintf(buf, "normal\n");
  181. case REGULATOR_MODE_IDLE:
  182. return sprintf(buf, "idle\n");
  183. case REGULATOR_MODE_STANDBY:
  184. return sprintf(buf, "standby\n");
  185. default:
  186. return sprintf(buf, "unknown\n");
  187. }
  188. }
  189. static ssize_t set_mode(struct device *dev, struct device_attribute *attr,
  190. const char *buf, size_t count)
  191. {
  192. struct virtual_consumer_data *data = dev_get_drvdata(dev);
  193. unsigned int mode;
  194. int ret;
  195. /*
  196. * sysfs_streq() doesn't need the \n's, but we add them so the strings
  197. * will be shared with show_mode(), above.
  198. */
  199. if (sysfs_streq(buf, "fast\n"))
  200. mode = REGULATOR_MODE_FAST;
  201. else if (sysfs_streq(buf, "normal\n"))
  202. mode = REGULATOR_MODE_NORMAL;
  203. else if (sysfs_streq(buf, "idle\n"))
  204. mode = REGULATOR_MODE_IDLE;
  205. else if (sysfs_streq(buf, "standby\n"))
  206. mode = REGULATOR_MODE_STANDBY;
  207. else {
  208. dev_err(dev, "Configuring invalid mode\n");
  209. return count;
  210. }
  211. mutex_lock(&data->lock);
  212. ret = regulator_set_mode(data->regulator, mode);
  213. if (ret == 0)
  214. data->mode = mode;
  215. else
  216. dev_err(dev, "Failed to configure mode: %d\n", ret);
  217. mutex_unlock(&data->lock);
  218. return count;
  219. }
  220. static DEVICE_ATTR(min_microvolts, 0664, show_min_uV, set_min_uV);
  221. static DEVICE_ATTR(max_microvolts, 0664, show_max_uV, set_max_uV);
  222. static DEVICE_ATTR(min_microamps, 0664, show_min_uA, set_min_uA);
  223. static DEVICE_ATTR(max_microamps, 0664, show_max_uA, set_max_uA);
  224. static DEVICE_ATTR(mode, 0664, show_mode, set_mode);
  225. static struct attribute *regulator_virtual_attributes[] = {
  226. &dev_attr_min_microvolts.attr,
  227. &dev_attr_max_microvolts.attr,
  228. &dev_attr_min_microamps.attr,
  229. &dev_attr_max_microamps.attr,
  230. &dev_attr_mode.attr,
  231. NULL
  232. };
  233. static const struct attribute_group regulator_virtual_attr_group = {
  234. .attrs = regulator_virtual_attributes,
  235. };
  236. static int regulator_virtual_probe(struct platform_device *pdev)
  237. {
  238. char *reg_id = dev_get_platdata(&pdev->dev);
  239. struct virtual_consumer_data *drvdata;
  240. int ret;
  241. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct virtual_consumer_data),
  242. GFP_KERNEL);
  243. if (drvdata == NULL)
  244. return -ENOMEM;
  245. mutex_init(&drvdata->lock);
  246. drvdata->regulator = devm_regulator_get(&pdev->dev, reg_id);
  247. if (IS_ERR(drvdata->regulator)) {
  248. ret = PTR_ERR(drvdata->regulator);
  249. dev_err(&pdev->dev, "Failed to obtain supply '%s': %d\n",
  250. reg_id, ret);
  251. return ret;
  252. }
  253. ret = sysfs_create_group(&pdev->dev.kobj,
  254. &regulator_virtual_attr_group);
  255. if (ret != 0) {
  256. dev_err(&pdev->dev,
  257. "Failed to create attribute group: %d\n", ret);
  258. return ret;
  259. }
  260. drvdata->mode = regulator_get_mode(drvdata->regulator);
  261. platform_set_drvdata(pdev, drvdata);
  262. return 0;
  263. }
  264. static int regulator_virtual_remove(struct platform_device *pdev)
  265. {
  266. struct virtual_consumer_data *drvdata = platform_get_drvdata(pdev);
  267. sysfs_remove_group(&pdev->dev.kobj, &regulator_virtual_attr_group);
  268. if (drvdata->enabled)
  269. regulator_disable(drvdata->regulator);
  270. return 0;
  271. }
  272. static struct platform_driver regulator_virtual_consumer_driver = {
  273. .probe = regulator_virtual_probe,
  274. .remove = regulator_virtual_remove,
  275. .driver = {
  276. .name = "reg-virt-consumer",
  277. },
  278. };
  279. module_platform_driver(regulator_virtual_consumer_driver);
  280. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  281. MODULE_DESCRIPTION("Virtual regulator consumer");
  282. MODULE_LICENSE("GPL");
  283. MODULE_ALIAS("platform:reg-virt-consumer");