xref: /openbmc/u-boot/drivers/adc/adc-uclass.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 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/lists.h>
11 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
13 #include <adc.h>
14 #include <power/regulator.h>
15 
16 #define ADC_UCLASS_PLATDATA_SIZE	sizeof(struct adc_uclass_platdata)
17 #define CHECK_NUMBER			true
18 #define CHECK_MASK			(!CHECK_NUMBER)
19 
20 /* TODO: add support for timer uclass (for early calls) */
21 #ifdef CONFIG_SANDBOX_ARCH
22 #define sdelay(x)	udelay(x)
23 #else
24 extern void sdelay(unsigned long loops);
25 #endif
26 
27 static int check_channel(struct udevice *dev, int value, bool number_or_mask,
28 			 const char *caller_function)
29 {
30 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
31 	unsigned mask = number_or_mask ? (1 << value) : value;
32 
33 	/* For the real ADC hardware, some ADC channels can be inactive.
34 	 * For example if device has 4 analog channels, and only channels
35 	 * 1-st and 3-rd are valid, then channel mask is: 0b1010, so request
36 	 * with mask 0b1110 should return an error.
37 	*/
38 	if ((uc_pdata->channel_mask >= mask) && (uc_pdata->channel_mask & mask))
39 		return 0;
40 
41 	printf("Error in %s/%s().\nWrong channel selection for device: %s\n",
42 	       __FILE__, caller_function, dev->name);
43 
44 	return -EINVAL;
45 }
46 
47 static int adc_supply_enable(struct udevice *dev)
48 {
49 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
50 	const char *supply_type;
51 	int ret = 0;
52 
53 	if (uc_pdata->vdd_supply) {
54 		supply_type = "vdd";
55 		ret = regulator_set_enable(uc_pdata->vdd_supply, true);
56 	}
57 
58 	if (!ret && uc_pdata->vss_supply) {
59 		supply_type = "vss";
60 		ret = regulator_set_enable(uc_pdata->vss_supply, true);
61 	}
62 
63 	if (ret)
64 		pr_err("%s: can't enable %s-supply!", dev->name, supply_type);
65 
66 	return ret;
67 }
68 
69 int adc_data_mask(struct udevice *dev, unsigned int *data_mask)
70 {
71 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
72 
73 	if (!uc_pdata)
74 		return -ENOSYS;
75 
76 	*data_mask = uc_pdata->data_mask;
77 	return 0;
78 }
79 
80 int adc_stop(struct udevice *dev)
81 {
82 	const struct adc_ops *ops = dev_get_driver_ops(dev);
83 
84 	if (!ops->stop)
85 		return -ENOSYS;
86 
87 	return ops->stop(dev);
88 }
89 
90 int adc_start_channel(struct udevice *dev, int channel)
91 {
92 	const struct adc_ops *ops = dev_get_driver_ops(dev);
93 	int ret;
94 
95 	if (!ops->start_channel)
96 		return -ENOSYS;
97 
98 	ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
99 	if (ret)
100 		return ret;
101 
102 	ret = adc_supply_enable(dev);
103 	if (ret)
104 		return ret;
105 
106 	return ops->start_channel(dev, channel);
107 }
108 
109 int adc_start_channels(struct udevice *dev, unsigned int channel_mask)
110 {
111 	const struct adc_ops *ops = dev_get_driver_ops(dev);
112 	int ret;
113 
114 	if (!ops->start_channels)
115 		return -ENOSYS;
116 
117 	ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
118 	if (ret)
119 		return ret;
120 
121 	ret = adc_supply_enable(dev);
122 	if (ret)
123 		return ret;
124 
125 	return ops->start_channels(dev, channel_mask);
126 }
127 
128 int adc_channel_data(struct udevice *dev, int channel, unsigned int *data)
129 {
130 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
131 	const struct adc_ops *ops = dev_get_driver_ops(dev);
132 	unsigned int timeout_us = uc_pdata->data_timeout_us;
133 	int ret;
134 
135 	if (!ops->channel_data)
136 		return -ENOSYS;
137 
138 	ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
139 	if (ret)
140 		return ret;
141 
142 	do {
143 		ret = ops->channel_data(dev, channel, data);
144 		if (!ret || ret != -EBUSY)
145 			break;
146 
147 		/* TODO: use timer uclass (for early calls). */
148 		sdelay(5);
149 	} while (timeout_us--);
150 
151 	return ret;
152 }
153 
154 int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
155 		      struct adc_channel *channels)
156 {
157 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
158 	unsigned int timeout_us = uc_pdata->multidata_timeout_us;
159 	const struct adc_ops *ops = dev_get_driver_ops(dev);
160 	int ret;
161 
162 	if (!ops->channels_data)
163 		return -ENOSYS;
164 
165 	ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
166 	if (ret)
167 		return ret;
168 
169 	do {
170 		ret = ops->channels_data(dev, channel_mask, channels);
171 		if (!ret || ret != -EBUSY)
172 			break;
173 
174 		/* TODO: use timer uclass (for early calls). */
175 		sdelay(5);
176 	} while (timeout_us--);
177 
178 	return ret;
179 }
180 
181 int adc_channel_single_shot(const char *name, int channel, unsigned int *data)
182 {
183 	struct udevice *dev;
184 	int ret;
185 
186 	ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
187 	if (ret)
188 		return ret;
189 
190 	ret = adc_start_channel(dev, channel);
191 	if (ret)
192 		return ret;
193 
194 	ret = adc_channel_data(dev, channel, data);
195 	if (ret)
196 		return ret;
197 
198 	return 0;
199 }
200 
201 static int _adc_channels_single_shot(struct udevice *dev,
202 				     unsigned int channel_mask,
203 				     struct adc_channel *channels)
204 {
205 	unsigned int data;
206 	int channel, ret;
207 
208 	for (channel = 0; channel <= ADC_MAX_CHANNEL; channel++) {
209 		/* Check channel bit. */
210 		if (!((channel_mask >> channel) & 0x1))
211 			continue;
212 
213 		ret = adc_start_channel(dev, channel);
214 		if (ret)
215 			return ret;
216 
217 		ret = adc_channel_data(dev, channel, &data);
218 		if (ret)
219 			return ret;
220 
221 		channels->id = channel;
222 		channels->data = data;
223 		channels++;
224 	}
225 
226 	return 0;
227 }
228 
229 int adc_channels_single_shot(const char *name, unsigned int channel_mask,
230 			     struct adc_channel *channels)
231 {
232 	struct udevice *dev;
233 	int ret;
234 
235 	ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
236 	if (ret)
237 		return ret;
238 
239 	ret = adc_start_channels(dev, channel_mask);
240 	if (ret)
241 		goto try_manual;
242 
243 	ret = adc_channels_data(dev, channel_mask, channels);
244 	if (ret)
245 		return ret;
246 
247 	return 0;
248 
249 try_manual:
250 	if (ret != -ENOSYS)
251 		return ret;
252 
253 	return _adc_channels_single_shot(dev, channel_mask, channels);
254 }
255 
256 static int adc_vdd_platdata_update(struct udevice *dev)
257 {
258 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
259 	int ret;
260 
261 	/* Warning!
262 	 * This function can't return supply device before its bind.
263 	 * Please pay attention to proper fdt scan sequence. If ADC device
264 	 * will bind before its supply regulator device, then the below 'get'
265 	 * will return an error.
266 	 */
267 	ret = device_get_supply_regulator(dev, "vdd-supply",
268 					  &uc_pdata->vdd_supply);
269 	if (ret)
270 		return ret;
271 
272 	ret = regulator_get_value(uc_pdata->vdd_supply);
273 	if (ret < 0)
274 		return ret;
275 
276 	uc_pdata->vdd_microvolts = ret;
277 
278 	return 0;
279 }
280 
281 static int adc_vss_platdata_update(struct udevice *dev)
282 {
283 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
284 	int ret;
285 
286 	ret = device_get_supply_regulator(dev, "vss-supply",
287 					  &uc_pdata->vss_supply);
288 	if (ret)
289 		return ret;
290 
291 	ret = regulator_get_value(uc_pdata->vss_supply);
292 	if (ret < 0)
293 		return ret;
294 
295 	uc_pdata->vss_microvolts = ret;
296 
297 	return 0;
298 }
299 
300 int adc_vdd_value(struct udevice *dev, int *uV)
301 {
302 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
303 	int ret, value_sign = uc_pdata->vdd_polarity_negative ? -1 : 1;
304 
305 	if (!uc_pdata->vdd_supply)
306 		goto nodev;
307 
308 	/* Update the regulator Value. */
309 	ret = adc_vdd_platdata_update(dev);
310 	if (ret)
311 		return ret;
312 nodev:
313 	if (uc_pdata->vdd_microvolts == -ENODATA)
314 		return -ENODATA;
315 
316 	*uV = uc_pdata->vdd_microvolts * value_sign;
317 
318 	return 0;
319 }
320 
321 int adc_vss_value(struct udevice *dev, int *uV)
322 {
323 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
324 	int ret, value_sign = uc_pdata->vss_polarity_negative ? -1 : 1;
325 
326 	if (!uc_pdata->vss_supply)
327 		goto nodev;
328 
329 	/* Update the regulator Value. */
330 	ret = adc_vss_platdata_update(dev);
331 	if (ret)
332 		return ret;
333 nodev:
334 	if (uc_pdata->vss_microvolts == -ENODATA)
335 		return -ENODATA;
336 
337 	*uV = uc_pdata->vss_microvolts * value_sign;
338 
339 	return 0;
340 }
341 
342 static int adc_vdd_platdata_set(struct udevice *dev)
343 {
344 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
345 	int ret;
346 	char *prop;
347 
348 	prop = "vdd-polarity-negative";
349 	uc_pdata->vdd_polarity_negative = dev_read_bool(dev, prop);
350 
351 	ret = adc_vdd_platdata_update(dev);
352 	if (ret != -ENOENT)
353 		return ret;
354 
355 	/* No vdd-supply phandle. */
356 	prop  = "vdd-microvolts";
357 	uc_pdata->vdd_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
358 
359 	return 0;
360 }
361 
362 static int adc_vss_platdata_set(struct udevice *dev)
363 {
364 	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
365 	int ret;
366 	char *prop;
367 
368 	prop = "vss-polarity-negative";
369 	uc_pdata->vss_polarity_negative = dev_read_bool(dev, prop);
370 
371 	ret = adc_vss_platdata_update(dev);
372 	if (ret != -ENOENT)
373 		return ret;
374 
375 	/* No vss-supply phandle. */
376 	prop = "vss-microvolts";
377 	uc_pdata->vss_microvolts = dev_read_u32_default(dev, prop, -ENODATA);
378 
379 	return 0;
380 }
381 
382 static int adc_pre_probe(struct udevice *dev)
383 {
384 	int ret;
385 
386 	/* Set ADC VDD platdata: polarity, uV, regulator (phandle). */
387 	ret = adc_vdd_platdata_set(dev);
388 	if (ret)
389 		pr_err("%s: Can't update Vdd. Error: %d", dev->name, ret);
390 
391 	/* Set ADC VSS platdata: polarity, uV, regulator (phandle). */
392 	ret = adc_vss_platdata_set(dev);
393 	if (ret)
394 		pr_err("%s: Can't update Vss. Error: %d", dev->name, ret);
395 
396 	return 0;
397 }
398 
399 UCLASS_DRIVER(adc) = {
400 	.id	= UCLASS_ADC,
401 	.name	= "adc",
402 	.pre_probe =  adc_pre_probe,
403 	.per_device_platdata_auto_alloc_size = ADC_UCLASS_PLATDATA_SIZE,
404 };
405