virtual.c 8.9 KB

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