virtual.c 8.4 KB

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