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