1 /*
2  * Copyright (C) 2014-2015 Samsung Electronics
3  * Przemyslaw Marczak <p.marczak@samsung.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <fdtdec.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <dm/uclass-internal.h>
12 #include <power/pmic.h>
13 #include <power/regulator.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep)
18 {
19 	struct dm_regulator_uclass_platdata *uc_pdata;
20 
21 	*modep = NULL;
22 
23 	uc_pdata = dev_get_uclass_platdata(dev);
24 	if (!uc_pdata)
25 		return -ENXIO;
26 
27 	*modep = uc_pdata->mode;
28 	return uc_pdata->mode_count;
29 }
30 
31 int regulator_get_value(struct udevice *dev)
32 {
33 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
34 
35 	if (!ops || !ops->get_value)
36 		return -ENOSYS;
37 
38 	return ops->get_value(dev);
39 }
40 
41 int regulator_set_value(struct udevice *dev, int uV)
42 {
43 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
44 
45 	if (!ops || !ops->set_value)
46 		return -ENOSYS;
47 
48 	return ops->set_value(dev, uV);
49 }
50 
51 int regulator_get_current(struct udevice *dev)
52 {
53 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
54 
55 	if (!ops || !ops->get_current)
56 		return -ENOSYS;
57 
58 	return ops->get_current(dev);
59 }
60 
61 int regulator_set_current(struct udevice *dev, int uA)
62 {
63 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
64 
65 	if (!ops || !ops->set_current)
66 		return -ENOSYS;
67 
68 	return ops->set_current(dev, uA);
69 }
70 
71 bool regulator_get_enable(struct udevice *dev)
72 {
73 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
74 
75 	if (!ops || !ops->get_enable)
76 		return -ENOSYS;
77 
78 	return ops->get_enable(dev);
79 }
80 
81 int regulator_set_enable(struct udevice *dev, bool enable)
82 {
83 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
84 
85 	if (!ops || !ops->set_enable)
86 		return -ENOSYS;
87 
88 	return ops->set_enable(dev, enable);
89 }
90 
91 int regulator_get_mode(struct udevice *dev)
92 {
93 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
94 
95 	if (!ops || !ops->get_mode)
96 		return -ENOSYS;
97 
98 	return ops->get_mode(dev);
99 }
100 
101 int regulator_set_mode(struct udevice *dev, int mode)
102 {
103 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
104 
105 	if (!ops || !ops->set_mode)
106 		return -ENOSYS;
107 
108 	return ops->set_mode(dev, mode);
109 }
110 
111 int regulator_get_by_platname(const char *plat_name, struct udevice **devp)
112 {
113 	struct dm_regulator_uclass_platdata *uc_pdata;
114 	struct udevice *dev;
115 	int ret;
116 
117 	*devp = NULL;
118 
119 	for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
120 	     ret = uclass_find_next_device(&dev)) {
121 		if (ret)
122 			continue;
123 
124 		uc_pdata = dev_get_uclass_platdata(dev);
125 		if (!uc_pdata || strcmp(plat_name, uc_pdata->name))
126 			continue;
127 
128 		return uclass_get_device_tail(dev, 0, devp);
129 	}
130 
131 	debug("%s: can't find: %s\n", __func__, plat_name);
132 
133 	return -ENODEV;
134 }
135 
136 int regulator_get_by_devname(const char *devname, struct udevice **devp)
137 {
138 	return uclass_get_device_by_name(UCLASS_REGULATOR, devname, devp);
139 }
140 
141 static int failed(int ret, bool verbose, const char *fmt, ...)
142 {
143 	va_list args;
144 	char buf[64];
145 
146 	if (verbose == false)
147 		return ret;
148 
149 	va_start(args, fmt);
150 	vscnprintf(buf, sizeof(buf), fmt, args);
151 	va_end(args);
152 
153 	printf(buf);
154 
155 	if (!ret)
156 		return 0;
157 
158 	printf(" (ret: %d)", ret);
159 
160 	return ret;
161 }
162 
163 int regulator_autoset(const char *platname,
164 		      struct udevice **devp,
165 		      bool verbose)
166 {
167 	struct dm_regulator_uclass_platdata *uc_pdata;
168 	struct udevice *dev;
169 	int ret;
170 
171 	if (devp)
172 		*devp = NULL;
173 
174 	ret = regulator_get_by_platname(platname, &dev);
175 	if (ret) {
176 		error("Can get the regulator: %s!", platname);
177 		return ret;
178 	}
179 
180 	uc_pdata = dev_get_uclass_platdata(dev);
181 	if (!uc_pdata) {
182 		error("Can get the regulator %s uclass platdata!", platname);
183 		return -ENXIO;
184 	}
185 
186 	if (!uc_pdata->always_on && !uc_pdata->boot_on)
187 		goto retdev;
188 
189 	if (verbose)
190 		printf("%s@%s: ", dev->name, uc_pdata->name);
191 
192 	/* Those values are optional (-ENODATA if unset) */
193 	if ((uc_pdata->min_uV != -ENODATA) &&
194 	    (uc_pdata->max_uV != -ENODATA) &&
195 	    (uc_pdata->min_uV == uc_pdata->max_uV)) {
196 		ret = regulator_set_value(dev, uc_pdata->min_uV);
197 		if (failed(ret, verbose, "set %d uV", uc_pdata->min_uV))
198 			goto exit;
199 	}
200 
201 	/* Those values are optional (-ENODATA if unset) */
202 	if ((uc_pdata->min_uA != -ENODATA) &&
203 	    (uc_pdata->max_uA != -ENODATA) &&
204 	    (uc_pdata->min_uA == uc_pdata->max_uA)) {
205 		ret = regulator_set_current(dev, uc_pdata->min_uA);
206 		if (failed(ret, verbose, "; set %d uA", uc_pdata->min_uA))
207 			goto exit;
208 	}
209 
210 	ret = regulator_set_enable(dev, true);
211 	if (failed(ret, verbose, "; enabling", uc_pdata->min_uA))
212 		goto exit;
213 
214 retdev:
215 	if (devp)
216 		*devp = dev;
217 exit:
218 	if (verbose)
219 		printf("\n");
220 
221 	return ret;
222 }
223 
224 int regulator_list_autoset(const char *list_platname[],
225 			   struct udevice *list_devp[],
226 			   bool verbose)
227 {
228 	struct udevice *dev;
229 	int error = 0, i = 0, ret;
230 
231 	while (list_platname[i]) {
232 		ret = regulator_autoset(list_platname[i], &dev, verbose);
233 		if (ret & !error)
234 			error = ret;
235 
236 		if (list_devp)
237 			list_devp[i] = dev;
238 
239 		i++;
240 	}
241 
242 	return error;
243 }
244 
245 static bool regulator_name_is_unique(struct udevice *check_dev,
246 				     const char *check_name)
247 {
248 	struct dm_regulator_uclass_platdata *uc_pdata;
249 	struct udevice *dev;
250 	int check_len = strlen(check_name);
251 	int ret;
252 	int len;
253 
254 	for (ret = uclass_find_first_device(UCLASS_REGULATOR, &dev); dev;
255 	     ret = uclass_find_next_device(&dev)) {
256 		if (ret || dev == check_dev)
257 			continue;
258 
259 		uc_pdata = dev_get_uclass_platdata(dev);
260 		len = strlen(uc_pdata->name);
261 		if (len != check_len)
262 			continue;
263 
264 		if (!strcmp(uc_pdata->name, check_name))
265 			return false;
266 	}
267 
268 	return true;
269 }
270 
271 static int regulator_post_bind(struct udevice *dev)
272 {
273 	struct dm_regulator_uclass_platdata *uc_pdata;
274 	int offset = dev->of_offset;
275 	const void *blob = gd->fdt_blob;
276 	const char *property = "regulator-name";
277 
278 	uc_pdata = dev_get_uclass_platdata(dev);
279 	if (!uc_pdata)
280 		return -ENXIO;
281 
282 	/* Regulator's mandatory constraint */
283 	uc_pdata->name = fdt_getprop(blob, offset, property, NULL);
284 	if (!uc_pdata->name) {
285 		debug("%s: dev: %s has no property 'regulator-name'\n",
286 		      __func__, dev->name);
287 		return -EINVAL;
288 	}
289 
290 	if (regulator_name_is_unique(dev, uc_pdata->name))
291 		return 0;
292 
293 	error("\"%s\" of dev: \"%s\", has nonunique value: \"%s\"",
294 	      property, dev->name, uc_pdata->name);
295 
296 	return -EINVAL;
297 }
298 
299 static int regulator_pre_probe(struct udevice *dev)
300 {
301 	struct dm_regulator_uclass_platdata *uc_pdata;
302 	int offset = dev->of_offset;
303 
304 	uc_pdata = dev_get_uclass_platdata(dev);
305 	if (!uc_pdata)
306 		return -ENXIO;
307 
308 	/* Regulator's optional constraints */
309 	uc_pdata->min_uV = fdtdec_get_int(gd->fdt_blob, offset,
310 					  "regulator-min-microvolt", -ENODATA);
311 	uc_pdata->max_uV = fdtdec_get_int(gd->fdt_blob, offset,
312 					  "regulator-max-microvolt", -ENODATA);
313 	uc_pdata->min_uA = fdtdec_get_int(gd->fdt_blob, offset,
314 					  "regulator-min-microamp", -ENODATA);
315 	uc_pdata->max_uA = fdtdec_get_int(gd->fdt_blob, offset,
316 					  "regulator-max-microamp", -ENODATA);
317 	uc_pdata->always_on = fdtdec_get_bool(gd->fdt_blob, offset,
318 					      "regulator-always-on");
319 	uc_pdata->boot_on = fdtdec_get_bool(gd->fdt_blob, offset,
320 					    "regulator-boot-on");
321 
322 	return 0;
323 }
324 
325 UCLASS_DRIVER(regulator) = {
326 	.id		= UCLASS_REGULATOR,
327 	.name		= "regulator",
328 	.post_bind	= regulator_post_bind,
329 	.pre_probe	= regulator_pre_probe,
330 	.per_device_platdata_auto_alloc_size =
331 				sizeof(struct dm_regulator_uclass_platdata),
332 };
333