David Collins | fbd2dfd | 2013-06-10 15:38:04 -0700 | [diff] [blame] | 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 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/bitops.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/regulator/consumer.h> |
| 25 | #include <linux/regulator/proxy-consumer.h> |
| 26 | |
| 27 | struct proxy_consumer { |
| 28 | struct list_head list; |
| 29 | struct regulator *reg; |
| 30 | bool enable; |
| 31 | int min_uV; |
| 32 | int max_uV; |
| 33 | u32 current_uA; |
| 34 | }; |
| 35 | |
| 36 | static DEFINE_MUTEX(proxy_consumer_list_mutex); |
| 37 | static LIST_HEAD(proxy_consumer_list); |
| 38 | static bool proxy_consumers_removed; |
| 39 | |
| 40 | /** |
| 41 | * regulator_proxy_consumer_register() - conditionally register a proxy consumer |
| 42 | * for the specified regulator and set its boot time parameters |
| 43 | * @reg_dev: Device pointer of the regulator |
| 44 | * @reg_node: Device node pointer of the regulator |
| 45 | * |
| 46 | * Returns a struct proxy_consumer pointer corresponding to the regulator on |
| 47 | * success, ERR_PTR() if an error occurred, or NULL if no proxy consumer is |
| 48 | * needed for the regulator. This function calls |
| 49 | * regulator_get(reg_dev, "proxy") after first checking if any proxy consumer |
| 50 | * properties are present in the reg_node device node. After that, the voltage, |
| 51 | * minimum current, and/or the enable state will be set based upon the device |
| 52 | * node property values. |
| 53 | */ |
| 54 | struct proxy_consumer *regulator_proxy_consumer_register(struct device *reg_dev, |
| 55 | struct device_node *reg_node) |
| 56 | { |
| 57 | struct proxy_consumer *consumer = NULL; |
| 58 | const char *reg_name = ""; |
| 59 | u32 voltage[2] = {0}; |
| 60 | int rc; |
| 61 | |
| 62 | /* Return immediately if no proxy consumer properties are specified. */ |
| 63 | if (!of_find_property(reg_node, "qcom,proxy-consumer-enable", NULL) |
| 64 | && !of_find_property(reg_node, "qcom,proxy-consumer-voltage", NULL) |
| 65 | && !of_find_property(reg_node, "qcom,proxy-consumer-current", NULL)) |
| 66 | return NULL; |
| 67 | |
| 68 | mutex_lock(&proxy_consumer_list_mutex); |
| 69 | |
| 70 | /* Do not register new consumers if they cannot be removed later. */ |
| 71 | if (proxy_consumers_removed) { |
| 72 | rc = -EPERM; |
| 73 | goto unlock; |
| 74 | } |
| 75 | |
| 76 | if (dev_name(reg_dev)) |
| 77 | reg_name = dev_name(reg_dev); |
| 78 | |
| 79 | consumer = kzalloc(sizeof(*consumer), GFP_KERNEL); |
| 80 | if (!consumer) { |
| 81 | rc = -ENOMEM; |
| 82 | goto unlock; |
| 83 | } |
| 84 | |
| 85 | consumer->enable |
| 86 | = of_property_read_bool(reg_node, "qcom,proxy-consumer-enable"); |
| 87 | of_property_read_u32(reg_node, "qcom,proxy-consumer-current", |
| 88 | &consumer->current_uA); |
| 89 | rc = of_property_read_u32_array(reg_node, "qcom,proxy-consumer-voltage", |
| 90 | voltage, 2); |
| 91 | if (!rc) { |
| 92 | consumer->min_uV = voltage[0]; |
| 93 | consumer->max_uV = voltage[1]; |
| 94 | } |
| 95 | |
| 96 | dev_dbg(reg_dev, "proxy consumer request: enable=%d, voltage_range=[%d, %d] uV, min_current=%d uA\n", |
| 97 | consumer->enable, consumer->min_uV, consumer->max_uV, |
| 98 | consumer->current_uA); |
| 99 | |
| 100 | consumer->reg = regulator_get(reg_dev, "proxy"); |
| 101 | if (IS_ERR_OR_NULL(consumer->reg)) { |
| 102 | rc = PTR_ERR(consumer->reg); |
| 103 | pr_err("regulator_get() failed for %s, rc=%d\n", reg_name, rc); |
| 104 | goto unlock; |
| 105 | } |
| 106 | |
| 107 | if (consumer->max_uV > 0 && consumer->min_uV <= consumer->max_uV) { |
| 108 | rc = regulator_set_voltage(consumer->reg, consumer->min_uV, |
| 109 | consumer->max_uV); |
| 110 | if (rc) { |
| 111 | pr_err("regulator_set_voltage %s failed, rc=%d\n", |
| 112 | reg_name, rc); |
| 113 | goto free_regulator; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (consumer->current_uA > 0) { |
| 118 | rc = regulator_set_load(consumer->reg, |
| 119 | consumer->current_uA); |
| 120 | if (rc < 0) { |
| 121 | pr_err("regulator_set_load %s failed, rc=%d\n", |
| 122 | reg_name, rc); |
| 123 | goto remove_voltage; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (consumer->enable) { |
| 128 | rc = regulator_enable(consumer->reg); |
| 129 | if (rc) { |
| 130 | pr_err("regulator_enable %s failed, rc=%d\n", reg_name, |
| 131 | rc); |
| 132 | goto remove_current; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | list_add(&consumer->list, &proxy_consumer_list); |
| 137 | mutex_unlock(&proxy_consumer_list_mutex); |
| 138 | |
| 139 | return consumer; |
| 140 | |
| 141 | remove_current: |
| 142 | regulator_set_load(consumer->reg, 0); |
| 143 | remove_voltage: |
| 144 | regulator_set_voltage(consumer->reg, 0, INT_MAX); |
| 145 | free_regulator: |
| 146 | regulator_put(consumer->reg); |
| 147 | unlock: |
| 148 | kfree(consumer); |
| 149 | mutex_unlock(&proxy_consumer_list_mutex); |
| 150 | return ERR_PTR(rc); |
| 151 | } |
| 152 | |
| 153 | /* proxy_consumer_list_mutex must be held by caller. */ |
| 154 | static int regulator_proxy_consumer_remove(struct proxy_consumer *consumer) |
| 155 | { |
| 156 | int rc = 0; |
| 157 | |
| 158 | if (consumer->enable) { |
| 159 | rc = regulator_disable(consumer->reg); |
| 160 | if (rc) |
| 161 | pr_err("regulator_disable failed, rc=%d\n", rc); |
| 162 | } |
| 163 | |
| 164 | if (consumer->current_uA > 0) { |
| 165 | rc = regulator_set_load(consumer->reg, 0); |
| 166 | if (rc < 0) |
| 167 | pr_err("regulator_set_load failed, rc=%d\n", |
| 168 | rc); |
| 169 | } |
| 170 | |
| 171 | if (consumer->max_uV > 0 && consumer->min_uV <= consumer->max_uV) { |
| 172 | rc = regulator_set_voltage(consumer->reg, 0, INT_MAX); |
| 173 | if (rc) |
| 174 | pr_err("regulator_set_voltage failed, rc=%d\n", rc); |
| 175 | } |
| 176 | |
| 177 | regulator_put(consumer->reg); |
| 178 | list_del(&consumer->list); |
| 179 | kfree(consumer); |
| 180 | |
| 181 | return rc; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * regulator_proxy_consumer_unregister() - unregister a proxy consumer and |
| 186 | * remove its boot time requests |
| 187 | * @consumer: Pointer to proxy_consumer to be removed |
| 188 | * |
| 189 | * Returns 0 on success or errno on failure. This function removes all requests |
| 190 | * made by the proxy consumer in regulator_proxy_consumer_register() and then |
| 191 | * frees the consumer's resources. |
| 192 | */ |
| 193 | int regulator_proxy_consumer_unregister(struct proxy_consumer *consumer) |
| 194 | { |
| 195 | int rc = 0; |
| 196 | |
| 197 | if (IS_ERR_OR_NULL(consumer)) |
| 198 | return 0; |
| 199 | |
| 200 | mutex_lock(&proxy_consumer_list_mutex); |
| 201 | if (!proxy_consumers_removed) |
| 202 | rc = regulator_proxy_consumer_remove(consumer); |
| 203 | mutex_unlock(&proxy_consumer_list_mutex); |
| 204 | |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Remove all proxy requests at late_initcall_sync. The assumption is that all |
| 210 | * devices have probed at this point and made their own regulator requests. |
| 211 | */ |
| 212 | static int __init regulator_proxy_consumer_remove_all(void) |
| 213 | { |
| 214 | struct proxy_consumer *consumer; |
| 215 | struct proxy_consumer *temp; |
| 216 | |
| 217 | mutex_lock(&proxy_consumer_list_mutex); |
| 218 | proxy_consumers_removed = true; |
| 219 | |
| 220 | if (!list_empty(&proxy_consumer_list)) |
| 221 | pr_info("removing regulator proxy consumer requests\n"); |
| 222 | |
| 223 | list_for_each_entry_safe(consumer, temp, &proxy_consumer_list, list) { |
| 224 | regulator_proxy_consumer_remove(consumer); |
| 225 | } |
| 226 | mutex_unlock(&proxy_consumer_list_mutex); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | late_initcall_sync(regulator_proxy_consumer_remove_all); |