1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DRIVERS_COMP_NRF_COMP_H_
8 #define ZEPHYR_INCLUDE_DRIVERS_COMP_NRF_COMP_H_
9 
10 #include <zephyr/drivers/comparator.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /** Positive input selection */
17 enum comp_nrf_comp_psel {
18 	/** AIN0 external input */
19 	COMP_NRF_COMP_PSEL_AIN0,
20 	/** AIN1 external input */
21 	COMP_NRF_COMP_PSEL_AIN1,
22 	/** AIN2 external input */
23 	COMP_NRF_COMP_PSEL_AIN2,
24 	/** AIN3 external input */
25 	COMP_NRF_COMP_PSEL_AIN3,
26 	/** AIN4 external input */
27 	COMP_NRF_COMP_PSEL_AIN4,
28 	/** AIN5 external input */
29 	COMP_NRF_COMP_PSEL_AIN5,
30 	/** AIN6 external input */
31 	COMP_NRF_COMP_PSEL_AIN6,
32 	/** AIN7 external input */
33 	COMP_NRF_COMP_PSEL_AIN7,
34 	/** VDD / 2 */
35 	COMP_NRF_COMP_PSEL_VDD_DIV2,
36 	/** VDDH / 5 */
37 	COMP_NRF_COMP_PSEL_VDDH_DIV5,
38 };
39 
40 /** External reference selection */
41 enum comp_nrf_comp_extrefsel {
42 	/** AIN0 external input */
43 	COMP_NRF_COMP_EXTREFSEL_AIN0,
44 	/** AIN1 external input */
45 	COMP_NRF_COMP_EXTREFSEL_AIN1,
46 	/** AIN2 external input */
47 	COMP_NRF_COMP_EXTREFSEL_AIN2,
48 	/** AIN3 external input */
49 	COMP_NRF_COMP_EXTREFSEL_AIN3,
50 	/** AIN4 external input */
51 	COMP_NRF_COMP_EXTREFSEL_AIN4,
52 	/** AIN5 external input */
53 	COMP_NRF_COMP_EXTREFSEL_AIN5,
54 	/** AIN6 external input */
55 	COMP_NRF_COMP_EXTREFSEL_AIN6,
56 	/** AIN7 external input */
57 	COMP_NRF_COMP_EXTREFSEL_AIN7,
58 };
59 
60 /** Reference selection */
61 enum comp_nrf_comp_refsel {
62 	/** Internal 1.2V reference */
63 	COMP_NRF_COMP_REFSEL_INT_1V2,
64 	/** Internal 1.8V reference */
65 	COMP_NRF_COMP_REFSEL_INT_1V8,
66 	/** Internal 2.4V reference */
67 	COMP_NRF_COMP_REFSEL_INT_2V4,
68 	/** AVDD 1.8V reference */
69 	COMP_NRF_COMP_REFSEL_AVDDAO1V8,
70 	/** VDD reference */
71 	COMP_NRF_COMP_REFSEL_VDD,
72 	/** Use external analog reference */
73 	COMP_NRF_COMP_REFSEL_AREF,
74 };
75 
76 /** Speed mode selection */
77 enum comp_nrf_comp_sp_mode {
78 	/** Low-power mode */
79 	COMP_NRF_COMP_SP_MODE_LOW,
80 	/** Normal mode */
81 	COMP_NRF_COMP_SP_MODE_NORMAL,
82 	/** High-speed mode */
83 	COMP_NRF_COMP_SP_MODE_HIGH,
84 };
85 
86 /** Current source configuration */
87 enum comp_nrf_comp_isource {
88 	/** Current source disabled */
89 	COMP_NRF_COMP_ISOURCE_DISABLED,
90 	/** 2.5uA current source enabled */
91 	COMP_NRF_COMP_ISOURCE_2UA5,
92 	/** 5uA current source enabled */
93 	COMP_NRF_COMP_ISOURCE_5UA,
94 	/** 10uA current source enabled */
95 	COMP_NRF_COMP_ISOURCE_10UA,
96 };
97 
98 /**
99  * @brief Single-ended mode configuration structure
100  *
101  * @note extrefsel is only used if refsel == COMP_NRF_COMP_REFSEL_AREF
102  * @note Hysteresis down in volts = ((th_down + 1) / 64) * ref
103  * @note Hysteresis up in volts = ((th_up + 1) / 64) * ref
104  */
105 struct comp_nrf_comp_se_config {
106 	/** Positive input selection */
107 	enum comp_nrf_comp_psel psel;
108 	/** Speed mode selection */
109 	enum comp_nrf_comp_sp_mode sp_mode;
110 	/** Current source configuration */
111 	enum comp_nrf_comp_isource isource;
112 	/** External reference selection */
113 	enum comp_nrf_comp_extrefsel extrefsel;
114 	/** Reference selection */
115 	enum comp_nrf_comp_refsel refsel;
116 	/** Hysteresis down threshold configuration */
117 	uint8_t th_down;
118 	/** Hysteresis up threshold configuration */
119 	uint8_t th_up;
120 };
121 
122 /**
123  * @brief Configure comparator in single-ended mode
124  *
125  * @param dev Comparator device instance
126  * @param config Single-ended mode configuration
127  *
128  * @retval 0 if successful
129  * @retval negative errno-code otherwise
130  */
131 int comp_nrf_comp_configure_se(const struct device *dev,
132 			       const struct comp_nrf_comp_se_config *config);
133 
134 /** Differential mode configuration structure */
135 struct comp_nrf_comp_diff_config {
136 	/** Positive input selection */
137 	enum comp_nrf_comp_psel psel;
138 	/** Speed mode selection */
139 	enum comp_nrf_comp_sp_mode sp_mode;
140 	/** Current source configuration */
141 	enum comp_nrf_comp_isource isource;
142 	/** Negative input selection */
143 	enum comp_nrf_comp_extrefsel extrefsel;
144 	/** Hysteresis configuration */
145 	bool enable_hyst;
146 };
147 
148 /**
149  * @brief Configure comparator in differential mode
150  *
151  * @param dev Comparator device instance
152  * @param config Differential mode configuration
153  *
154  * @retval 0 if successful
155  * @retval negative errno-code otherwise
156  */
157 int comp_nrf_comp_configure_diff(const struct device *dev,
158 				 const struct comp_nrf_comp_diff_config *config);
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif /* ZEPHYR_INCLUDE_DRIVERS_COMP_NRF_COMP_H_ */
165