proxy-consumer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2013-2014, 2016, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/bitops.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/slab.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/regulator/proxy-consumer.h>
  24. struct proxy_consumer {
  25. struct list_head list;
  26. struct regulator *reg;
  27. bool enable;
  28. int min_uV;
  29. int max_uV;
  30. u32 current_uA;
  31. };
  32. static DEFINE_MUTEX(proxy_consumer_list_mutex);
  33. static LIST_HEAD(proxy_consumer_list);
  34. static bool proxy_consumers_removed;
  35. /**
  36. * regulator_proxy_consumer_register() - conditionally register a proxy consumer
  37. * for the specified regulator and set its boot time parameters
  38. * @reg_dev: Device pointer of the regulator
  39. * @reg_node: Device node pointer of the regulator
  40. *
  41. * Returns a struct proxy_consumer pointer corresponding to the regulator on
  42. * success, ERR_PTR() if an error occurred, or NULL if no proxy consumer is
  43. * needed for the regulator. This function calls
  44. * regulator_get(reg_dev, "proxy") after first checking if any proxy consumer
  45. * properties are present in the reg_node device node. After that, the voltage,
  46. * minimum current, and/or the enable state will be set based upon the device
  47. * node property values.
  48. */
  49. struct proxy_consumer *regulator_proxy_consumer_register(struct device *reg_dev,
  50. struct device_node *reg_node)
  51. {
  52. struct proxy_consumer *consumer = NULL;
  53. const char *reg_name = "";
  54. u32 voltage[2] = {0};
  55. int rc;
  56. /* Return immediately if no proxy consumer properties are specified. */
  57. if (!of_find_property(reg_node, "qcom,proxy-consumer-enable", NULL)
  58. && !of_find_property(reg_node, "qcom,proxy-consumer-voltage", NULL)
  59. && !of_find_property(reg_node, "qcom,proxy-consumer-current", NULL))
  60. return NULL;
  61. mutex_lock(&proxy_consumer_list_mutex);
  62. /* Do not register new consumers if they cannot be removed later. */
  63. if (proxy_consumers_removed) {
  64. rc = -EPERM;
  65. goto unlock;
  66. }
  67. if (dev_name(reg_dev))
  68. reg_name = dev_name(reg_dev);
  69. consumer = kzalloc(sizeof(*consumer), GFP_KERNEL);
  70. if (!consumer) {
  71. rc = -ENOMEM;
  72. goto unlock;
  73. }
  74. consumer->enable
  75. = of_property_read_bool(reg_node, "qcom,proxy-consumer-enable");
  76. of_property_read_u32(reg_node, "qcom,proxy-consumer-current",
  77. &consumer->current_uA);
  78. rc = of_property_read_u32_array(reg_node, "qcom,proxy-consumer-voltage",
  79. voltage, 2);
  80. if (!rc) {
  81. consumer->min_uV = voltage[0];
  82. consumer->max_uV = voltage[1];
  83. }
  84. dev_dbg(reg_dev, "proxy consumer request: enable=%d, voltage_range=[%d, %d] uV, min_current=%d uA\n",
  85. consumer->enable, consumer->min_uV, consumer->max_uV,
  86. consumer->current_uA);
  87. consumer->reg = regulator_get(reg_dev, "proxy");
  88. if (IS_ERR_OR_NULL(consumer->reg)) {
  89. rc = PTR_ERR(consumer->reg);
  90. pr_err("regulator_get() failed for %s, rc=%d\n", reg_name, rc);
  91. goto unlock;
  92. }
  93. if (consumer->max_uV > 0 && consumer->min_uV <= consumer->max_uV) {
  94. rc = regulator_set_voltage(consumer->reg, consumer->min_uV,
  95. consumer->max_uV);
  96. if (rc) {
  97. pr_err("regulator_set_voltage %s failed, rc=%d\n",
  98. reg_name, rc);
  99. goto free_regulator;
  100. }
  101. }
  102. if (consumer->current_uA > 0) {
  103. rc = regulator_set_load(consumer->reg,
  104. consumer->current_uA);
  105. if (rc < 0) {
  106. pr_err("regulator_set_load %s failed, rc=%d\n",
  107. reg_name, rc);
  108. goto remove_voltage;
  109. }
  110. }
  111. if (consumer->enable) {
  112. rc = regulator_enable(consumer->reg);
  113. if (rc) {
  114. pr_err("regulator_enable %s failed, rc=%d\n", reg_name,
  115. rc);
  116. goto remove_current;
  117. }
  118. }
  119. list_add(&consumer->list, &proxy_consumer_list);
  120. mutex_unlock(&proxy_consumer_list_mutex);
  121. return consumer;
  122. remove_current:
  123. regulator_set_load(consumer->reg, 0);
  124. remove_voltage:
  125. regulator_set_voltage(consumer->reg, 0, INT_MAX);
  126. free_regulator:
  127. regulator_put(consumer->reg);
  128. unlock:
  129. kfree(consumer);
  130. mutex_unlock(&proxy_consumer_list_mutex);
  131. return ERR_PTR(rc);
  132. }
  133. /* proxy_consumer_list_mutex must be held by caller. */
  134. static int regulator_proxy_consumer_remove(struct proxy_consumer *consumer)
  135. {
  136. int rc = 0;
  137. if (consumer->enable) {
  138. rc = regulator_disable(consumer->reg);
  139. if (rc)
  140. pr_err("regulator_disable failed, rc=%d\n", rc);
  141. }
  142. if (consumer->current_uA > 0) {
  143. rc = regulator_set_load(consumer->reg, 0);
  144. if (rc < 0)
  145. pr_err("regulator_set_load failed, rc=%d\n",
  146. rc);
  147. }
  148. if (consumer->max_uV > 0 && consumer->min_uV <= consumer->max_uV) {
  149. rc = regulator_set_voltage(consumer->reg, 0, INT_MAX);
  150. if (rc)
  151. pr_err("regulator_set_voltage failed, rc=%d\n", rc);
  152. }
  153. regulator_put(consumer->reg);
  154. list_del(&consumer->list);
  155. kfree(consumer);
  156. return rc;
  157. }
  158. /**
  159. * regulator_proxy_consumer_unregister() - unregister a proxy consumer and
  160. * remove its boot time requests
  161. * @consumer: Pointer to proxy_consumer to be removed
  162. *
  163. * Returns 0 on success or errno on failure. This function removes all requests
  164. * made by the proxy consumer in regulator_proxy_consumer_register() and then
  165. * frees the consumer's resources.
  166. */
  167. int regulator_proxy_consumer_unregister(struct proxy_consumer *consumer)
  168. {
  169. int rc = 0;
  170. if (IS_ERR_OR_NULL(consumer))
  171. return 0;
  172. mutex_lock(&proxy_consumer_list_mutex);
  173. if (!proxy_consumers_removed)
  174. rc = regulator_proxy_consumer_remove(consumer);
  175. mutex_unlock(&proxy_consumer_list_mutex);
  176. return rc;
  177. }
  178. /*
  179. * Remove all proxy requests at late_initcall_sync. The assumption is that all
  180. * devices have probed at this point and made their own regulator requests.
  181. */
  182. static int __init regulator_proxy_consumer_remove_all(void)
  183. {
  184. struct proxy_consumer *consumer;
  185. struct proxy_consumer *temp;
  186. mutex_lock(&proxy_consumer_list_mutex);
  187. proxy_consumers_removed = true;
  188. if (!list_empty(&proxy_consumer_list))
  189. pr_info("removing regulator proxy consumer requests\n");
  190. list_for_each_entry_safe(consumer, temp, &proxy_consumer_list, list) {
  191. regulator_proxy_consumer_remove(consumer);
  192. }
  193. mutex_unlock(&proxy_consumer_list_mutex);
  194. return 0;
  195. }
  196. late_initcall_sync(regulator_proxy_consumer_remove_all);