summaryrefslogtreecommitdiff
path: root/chip/it83xx/adc_chip.h
blob: 15a8e68e942b9b1067866b421ddbb417a35c3b29 (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
/* Copyright 2014 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.
 */

/* IT83xx ADC module for Chrome EC */

#ifndef __CROS_EC_ADC_CHIP_H
#define __CROS_EC_ADC_CHIP_H

#include <stdint.h>

#include "common.h"

/*
 * Maximum time we allow for an ADC conversion.
 * NOTE:
 * Because this setting greater than "SLEEP_SET_HTIMER_DELAY_USEC" in clock.c,
 * so we enabled sleep mask to prevent going in to deep sleep while ADC
 * converting.
 */
#define ADC_TIMEOUT_US MSEC

/* Minimum and maximum values returned by adc_read_channel(). */
#define ADC_READ_MIN 0
#define ADC_READ_MAX 1023
#define ADC_MAX_MVOLT 3000

/* List of ADC channels. */
enum chip_adc_channel {
	CHIP_ADC_CH0 = 0,
	CHIP_ADC_CH1,
	CHIP_ADC_CH2,
	CHIP_ADC_CH3,
	CHIP_ADC_CH4,
	CHIP_ADC_CH5,
	CHIP_ADC_CH6,
	CHIP_ADC_CH7,
	CHIP_ADC_CH13,
	CHIP_ADC_CH14,
	CHIP_ADC_CH15,
	CHIP_ADC_CH16,
	CHIP_ADC_COUNT,
};

/* List of voltage comparator. */
enum chip_vcmp {
	CHIP_VCMP0 = 0,
	CHIP_VCMP1,
	CHIP_VCMP2,
	CHIP_VCMP3,
	CHIP_VCMP4,
	CHIP_VCMP5,
	CHIP_VCMP_COUNT,
};

/* List of voltage comparator scan period times. */
enum vcmp_scan_period {
	VCMP_SCAN_PERIOD_100US = 0x10,
	VCMP_SCAN_PERIOD_200US = 0x20,
	VCMP_SCAN_PERIOD_400US = 0x30,
	VCMP_SCAN_PERIOD_600US = 0x40,
	VCMP_SCAN_PERIOD_800US = 0x50,
	VCMP_SCAN_PERIOD_1MS   = 0x60,
	VCMP_SCAN_PERIOD_1_5MS = 0x70,
	VCMP_SCAN_PERIOD_2MS   = 0x80,
	VCMP_SCAN_PERIOD_2_5MS = 0x90,
	VCMP_SCAN_PERIOD_3MS   = 0xA0,
	VCMP_SCAN_PERIOD_4MS   = 0xB0,
	VCMP_SCAN_PERIOD_5MS   = 0xC0,
};

/* Data structure to define ADC channel control registers. */
struct adc_ctrl_t {
	volatile uint8_t *adc_ctrl;
	volatile uint8_t *adc_datm;
	volatile uint8_t *adc_datl;
};

/* Data structure to define ADC channels. */
struct adc_t {
	const char *name;
	int factor_mul;
	int factor_div;
	int shift;
	enum chip_adc_channel channel;
};

/* Data structure to define voltage comparator control registers. */
struct vcmp_ctrl_t {
	volatile uint8_t *vcmp_ctrl;
	volatile uint8_t *vcmp_adc_chm;
	volatile uint8_t *vcmp_datm;
	volatile uint8_t *vcmp_datl;
};

/* supported flags (member "flag" in struct vcmp_t) for voltage comparator */
#define GREATER_THRESHOLD         BIT(0)
#define LESS_EQUAL_THRESHOLD      BIT(1)

/* Data structure for board to define voltage comparator list. */
struct vcmp_t {
	const char *name;
	int threshold;
	/*
	 * Select greater/less equal threshold.
	 * NOTE: once edge trigger interrupt fires, we need disable the voltage
	 *       comparator, or the matching threshold level will infinitely
	 *       triggers interrupt.
	 */
	char flag;
	/* Called when the interrupt fires */
	void (*vcmp_thresh_cb)(void);
	/*
	 * Select "all voltage comparator" scan period time.
	 * The power consumption is positively relative with scan frequency.
	 */
	enum vcmp_scan_period scan_period;
	/*
	 * Select which ADC channel output voltage into comparator and we
	 * should set the ADC channel pin in alternate mode via adc_channels[].
	 */
	enum chip_adc_channel adc_ch;
};

#ifdef CONFIG_ADC_VOLTAGE_COMPARATOR
/*
 * Boards must provide this list of voltage comparator definitions.
 * This must match the enum board_vcmp list provided by the board.
 */
extern const struct vcmp_t vcmp_list[];
#endif
void vcmp_enable(int index, int enable);

#endif /* __CROS_EC_ADC_CHIP_H */