1
2
3
4
5
6#include <linux/device.h>
7#include <linux/err.h>
8#include <linux/log2.h>
9#include <linux/regulator/consumer.h>
10
11#include <linux/mmc/host.h>
12
13#include "core.h"
14#include "host.h"
15
16#ifdef CONFIG_REGULATOR
17
18
19
20
21
22
23
24
25
26
27static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV)
28{
29 int tmp;
30
31 if (!vdd_bit)
32 return -EINVAL;
33
34
35
36
37
38
39
40 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
41 if (tmp == 0) {
42 *min_uV = 1650 * 1000;
43 *max_uV = 1950 * 1000;
44 } else {
45 *min_uV = 1900 * 1000 + tmp * 100 * 1000;
46 *max_uV = *min_uV + 100 * 1000;
47 }
48
49 return 0;
50}
51
52
53
54
55
56
57
58
59
60
61static int mmc_regulator_get_ocrmask(struct regulator *supply)
62{
63 int result = 0;
64 int count;
65 int i;
66 int vdd_uV;
67 int vdd_mV;
68
69 count = regulator_count_voltages(supply);
70 if (count < 0)
71 return count;
72
73 for (i = 0; i < count; i++) {
74 vdd_uV = regulator_list_voltage(supply, i);
75 if (vdd_uV <= 0)
76 continue;
77
78 vdd_mV = vdd_uV / 1000;
79 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
80 }
81
82 if (!result) {
83 vdd_uV = regulator_get_voltage(supply);
84 if (vdd_uV <= 0)
85 return vdd_uV;
86
87 vdd_mV = vdd_uV / 1000;
88 result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
89 }
90
91 return result;
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106int mmc_regulator_set_ocr(struct mmc_host *mmc,
107 struct regulator *supply,
108 unsigned short vdd_bit)
109{
110 int result = 0;
111 int min_uV, max_uV;
112
113 if (vdd_bit) {
114 mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
115
116 result = regulator_set_voltage(supply, min_uV, max_uV);
117 if (result == 0 && !mmc->regulator_enabled) {
118 result = regulator_enable(supply);
119 if (!result)
120 mmc->regulator_enabled = true;
121 }
122 } else if (mmc->regulator_enabled) {
123 result = regulator_disable(supply);
124 if (result == 0)
125 mmc->regulator_enabled = false;
126 }
127
128 if (result)
129 dev_err(mmc_dev(mmc),
130 "could not set regulator OCR (%d)\n", result);
131 return result;
132}
133EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
134
135static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
136 int min_uV, int target_uV,
137 int max_uV)
138{
139 int current_uV;
140
141
142
143
144
145 if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
146 return -EINVAL;
147
148
149
150
151
152 current_uV = regulator_get_voltage(regulator);
153 if (current_uV == target_uV)
154 return 1;
155
156 return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
157 max_uV);
158}
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
180{
181 struct device *dev = mmc_dev(mmc);
182 int ret, volt, min_uV, max_uV;
183
184
185 if (IS_ERR(mmc->supply.vqmmc))
186 return -EINVAL;
187
188 switch (ios->signal_voltage) {
189 case MMC_SIGNAL_VOLTAGE_120:
190 return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
191 1100000, 1200000, 1300000);
192 case MMC_SIGNAL_VOLTAGE_180:
193 return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
194 1700000, 1800000, 1950000);
195 case MMC_SIGNAL_VOLTAGE_330:
196 ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
197 if (ret < 0)
198 return ret;
199
200 dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
201 __func__, volt, max_uV);
202
203 min_uV = max(volt - 300000, 2700000);
204 max_uV = min(max_uV + 200000, 3600000);
205
206
207
208
209
210
211
212
213 ret = mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
214 min_uV, volt, max_uV);
215 if (ret >= 0)
216 return ret;
217
218 return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
219 2700000, volt, 3600000);
220 default:
221 return -EINVAL;
222 }
223}
224EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
225
226#else
227
228static inline int mmc_regulator_get_ocrmask(struct regulator *supply)
229{
230 return 0;
231}
232
233#endif
234
235
236
237
238
239
240
241
242
243
244
245int mmc_regulator_get_supply(struct mmc_host *mmc)
246{
247 struct device *dev = mmc_dev(mmc);
248 int ret;
249
250 mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
251 mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
252
253 if (IS_ERR(mmc->supply.vmmc)) {
254 if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
255 return -EPROBE_DEFER;
256 dev_dbg(dev, "No vmmc regulator found\n");
257 } else {
258 ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
259 if (ret > 0)
260 mmc->ocr_avail = ret;
261 else
262 dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
263 }
264
265 if (IS_ERR(mmc->supply.vqmmc)) {
266 if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
267 return -EPROBE_DEFER;
268 dev_dbg(dev, "No vqmmc regulator found\n");
269 }
270
271 return 0;
272}
273EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
274