summaryrefslogtreecommitdiff
path: root/drivers/adc/adc-uclass.c
blob: a5ef722d2137d7e089ee69f727035f4df8d6f89f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright (C) 2015 Samsung Electronics
 * Przemyslaw Marczak <p.marczak@samsung.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <errno.h>
#include <dm.h>
#include <dm/lists.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>
#include <adc.h>
#include <power/regulator.h>

DECLARE_GLOBAL_DATA_PTR;

#define ADC_UCLASS_PLATDATA_SIZE	sizeof(struct adc_uclass_platdata)
#define CHECK_NUMBER			true
#define CHECK_MASK			(!CHECK_NUMBER)

/* TODO: add support for timer uclass (for early calls) */
#ifdef CONFIG_SANDBOX_ARCH
#define sdelay(x)	udelay(x)
#else
extern void sdelay(unsigned long loops);
#endif

static int check_channel(struct udevice *dev, int value, bool number_or_mask,
			 const char *caller_function)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	unsigned mask = number_or_mask ? (1 << value) : value;

	/* For the real ADC hardware, some ADC channels can be inactive.
	 * For example if device has 4 analog channels, and only channels
	 * 1-st and 3-rd are valid, then channel mask is: 0b1010, so request
	 * with mask 0b1110 should return an error.
	*/
	if ((uc_pdata->channel_mask >= mask) && (uc_pdata->channel_mask & mask))
		return 0;

	printf("Error in %s/%s().\nWrong channel selection for device: %s\n",
	       __FILE__, caller_function, dev->name);

	return -EINVAL;
}

static int adc_supply_enable(struct udevice *dev)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	const char *supply_type;
	int ret = 0;

	if (uc_pdata->vdd_supply) {
		supply_type = "vdd";
		ret = regulator_set_enable(uc_pdata->vdd_supply, true);
	}

	if (!ret && uc_pdata->vss_supply) {
		supply_type = "vss";
		ret = regulator_set_enable(uc_pdata->vss_supply, true);
	}

	if (ret)
		error("%s: can't enable %s-supply!", dev->name, supply_type);

	return ret;
}

int adc_data_mask(struct udevice *dev, unsigned int *data_mask)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);

	if (!uc_pdata)
		return -ENOSYS;

	*data_mask = uc_pdata->data_mask;
	return 0;
}

int adc_stop(struct udevice *dev)
{
	const struct adc_ops *ops = dev_get_driver_ops(dev);

	if (!ops->stop)
		return -ENOSYS;

	return ops->stop(dev);
}

int adc_start_channel(struct udevice *dev, int channel)
{
	const struct adc_ops *ops = dev_get_driver_ops(dev);
	int ret;

	if (!ops->start_channel)
		return -ENOSYS;

	ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
	if (ret)
		return ret;

	ret = adc_supply_enable(dev);
	if (ret)
		return ret;

	return ops->start_channel(dev, channel);
}

int adc_start_channels(struct udevice *dev, unsigned int channel_mask)
{
	const struct adc_ops *ops = dev_get_driver_ops(dev);
	int ret;

	if (!ops->start_channels)
		return -ENOSYS;

	ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
	if (ret)
		return ret;

	ret = adc_supply_enable(dev);
	if (ret)
		return ret;

	return ops->start_channels(dev, channel_mask);
}

int adc_channel_data(struct udevice *dev, int channel, unsigned int *data)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	const struct adc_ops *ops = dev_get_driver_ops(dev);
	unsigned int timeout_us = uc_pdata->data_timeout_us;
	int ret;

	if (!ops->channel_data)
		return -ENOSYS;

	ret = check_channel(dev, channel, CHECK_NUMBER, __func__);
	if (ret)
		return ret;

	do {
		ret = ops->channel_data(dev, channel, data);
		if (!ret || ret != -EBUSY)
			break;

		/* TODO: use timer uclass (for early calls). */
		sdelay(5);
	} while (timeout_us--);

	return ret;
}

int adc_channels_data(struct udevice *dev, unsigned int channel_mask,
		      struct adc_channel *channels)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	unsigned int timeout_us = uc_pdata->multidata_timeout_us;
	const struct adc_ops *ops = dev_get_driver_ops(dev);
	int ret;

	if (!ops->channels_data)
		return -ENOSYS;

	ret = check_channel(dev, channel_mask, CHECK_MASK, __func__);
	if (ret)
		return ret;

	do {
		ret = ops->channels_data(dev, channel_mask, channels);
		if (!ret || ret != -EBUSY)
			break;

		/* TODO: use timer uclass (for early calls). */
		sdelay(5);
	} while (timeout_us--);

	return ret;
}

int adc_channel_single_shot(const char *name, int channel, unsigned int *data)
{
	struct udevice *dev;
	int ret;

	ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
	if (ret)
		return ret;

	ret = adc_start_channel(dev, channel);
	if (ret)
		return ret;

	ret = adc_channel_data(dev, channel, data);
	if (ret)
		return ret;

	return 0;
}

static int _adc_channels_single_shot(struct udevice *dev,
				     unsigned int channel_mask,
				     struct adc_channel *channels)
{
	unsigned int data;
	int channel, ret;

	for (channel = 0; channel <= ADC_MAX_CHANNEL; channel++) {
		/* Check channel bit. */
		if (!((channel_mask >> channel) & 0x1))
			continue;

		ret = adc_start_channel(dev, channel);
		if (ret)
			return ret;

		ret = adc_channel_data(dev, channel, &data);
		if (ret)
			return ret;

		channels->id = channel;
		channels->data = data;
		channels++;
	}

	return 0;
}

int adc_channels_single_shot(const char *name, unsigned int channel_mask,
			     struct adc_channel *channels)
{
	struct udevice *dev;
	int ret;

	ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
	if (ret)
		return ret;

	ret = adc_start_channels(dev, channel_mask);
	if (ret)
		goto try_manual;

	ret = adc_channels_data(dev, channel_mask, channels);
	if (ret)
		return ret;

	return 0;

try_manual:
	if (ret != -ENOSYS)
		return ret;

	return _adc_channels_single_shot(dev, channel_mask, channels);
}

static int adc_vdd_platdata_update(struct udevice *dev)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	int ret;

	/* Warning!
	 * This function can't return supply device before its bind.
	 * Please pay attention to proper fdt scan sequence. If ADC device
	 * will bind before its supply regulator device, then the below 'get'
	 * will return an error.
	 */
	ret = device_get_supply_regulator(dev, "vdd-supply",
					  &uc_pdata->vdd_supply);
	if (ret)
		return ret;

	ret = regulator_get_value(uc_pdata->vdd_supply);
	if (ret < 0)
		return ret;

	uc_pdata->vdd_microvolts = ret;

	return 0;
}

static int adc_vss_platdata_update(struct udevice *dev)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	int ret;

	ret = device_get_supply_regulator(dev, "vss-supply",
					  &uc_pdata->vss_supply);
	if (ret)
		return ret;

	ret = regulator_get_value(uc_pdata->vss_supply);
	if (ret < 0)
		return ret;

	uc_pdata->vss_microvolts = ret;

	return 0;
}

int adc_vdd_value(struct udevice *dev, int *uV)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	int ret, value_sign = uc_pdata->vdd_polarity_negative ? -1 : 1;

	if (!uc_pdata->vdd_supply)
		goto nodev;

	/* Update the regulator Value. */
	ret = adc_vdd_platdata_update(dev);
	if (ret)
		return ret;
nodev:
	if (uc_pdata->vdd_microvolts == -ENODATA)
		return -ENODATA;

	*uV = uc_pdata->vdd_microvolts * value_sign;

	return 0;
}

int adc_vss_value(struct udevice *dev, int *uV)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	int ret, value_sign = uc_pdata->vss_polarity_negative ? -1 : 1;

	if (!uc_pdata->vss_supply)
		goto nodev;

	/* Update the regulator Value. */
	ret = adc_vss_platdata_update(dev);
	if (ret)
		return ret;
nodev:
	if (uc_pdata->vss_microvolts == -ENODATA)
		return -ENODATA;

	*uV = uc_pdata->vss_microvolts * value_sign;

	return 0;
}

static int adc_vdd_platdata_set(struct udevice *dev)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	int ret;
	char *prop;

	prop = "vdd-polarity-negative";
	uc_pdata->vdd_polarity_negative = dev_read_bool(dev, prop);

	ret = adc_vdd_platdata_update(dev);
	if (ret != -ENOENT)
		return ret;

	/* No vdd-supply phandle. */
	prop  = "vdd-microvolts";
	uc_pdata->vdd_microvolts = dev_read_u32_default(dev, prop, -ENODATA);

	return 0;
}

static int adc_vss_platdata_set(struct udevice *dev)
{
	struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
	int ret;
	char *prop;

	prop = "vss-polarity-negative";
	uc_pdata->vss_polarity_negative = dev_read_bool(dev, prop);

	ret = adc_vss_platdata_update(dev);
	if (ret != -ENOENT)
		return ret;

	/* No vss-supply phandle. */
	prop = "vss-microvolts";
	uc_pdata->vss_microvolts = dev_read_u32_default(dev, prop, -ENODATA);

	return 0;
}

static int adc_pre_probe(struct udevice *dev)
{
	int ret;

	/* Set ADC VDD platdata: polarity, uV, regulator (phandle). */
	ret = adc_vdd_platdata_set(dev);
	if (ret)
		error("%s: Can't update Vdd. Error: %d", dev->name, ret);

	/* Set ADC VSS platdata: polarity, uV, regulator (phandle). */
	ret = adc_vss_platdata_set(dev);
	if (ret)
		error("%s: Can't update Vss. Error: %d", dev->name, ret);

	return 0;
}

UCLASS_DRIVER(adc) = {
	.id	= UCLASS_ADC,
	.name	= "adc",
	.pre_probe =  adc_pre_probe,
	.per_device_platdata_auto_alloc_size = ADC_UCLASS_PLATDATA_SIZE,
};