summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/adc.c
blob: 6f5a7d58de745332ce7c106c43990aa8aea254e9 (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
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "adc.h"
#include <drivers/adc.h>

#define ADC_DEV DT_LABEL(DT_NODELABEL(adc0))
const struct device *adc_dev;

#if DT_NODE_EXISTS(DT_INST(0, named_adc_channels))
#define ADC_CHANNEL_COMMA(node_id)                     \
	[node_id] = {                                  \
		.name = DT_LABEL(node_id),             \
		.input_ch = DT_PROP(node_id, channel), \
	},
const struct adc_t adc_channels[] = {
	DT_FOREACH_CHILD(DT_INST(0, named_adc_channels), ADC_CHANNEL_COMMA)
	};
#endif /* named_adc_channels */

static int init_device_bindings(const struct device *device)
{
	ARG_UNUSED(device);
	adc_dev = device_get_binding(ADC_DEV);
	return 0;
}
SYS_INIT(init_device_bindings, POST_KERNEL, 51);

int adc_read_channel(enum adc_channel ch)
{
	int ret = 0;
	struct adc_sequence seq = {
		.options = NULL,
		.channels = BIT(adc_channels[ch].input_ch),
		.buffer = &ret,
		.buffer_size = sizeof(ret),
		.resolution = CONFIG_PLATFORM_EC_ADC_RESOLUTION,
		.oversampling = CONFIG_PLATFORM_EC_ADC_OVERSAMPLING,
		.calibrate = false,
	};

	adc_read(adc_dev, &seq);
	adc_raw_to_millivolts(adc_ref_internal(adc_dev), ADC_GAIN_1,
			      CONFIG_PLATFORM_EC_ADC_RESOLUTION, &ret);
	return ret;
}