blob: 85ebad34b6233ff93f6643cfac328b94b6ec04a6 [file] [log] [blame]
Srikrishna Iyer0e687042021-10-23 14:53:47 -07001// Copyright lowRISC contributors.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5#ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_
6#define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_
7
8/**
9 * @file
10 * @brief <a href="/hw/ip/adc_ctrl/doc/">ADC Controller</a> Device Interface
11 * Functions
12 */
13
Timothy Trippelebe223e2023-02-01 15:57:40 -080014#include "adc_ctrl_regs.h" // Generated.
Srikrishna Iyer0e687042021-10-23 14:53:47 -070015#include "sw/device/lib/dif/autogen/dif_adc_ctrl_autogen.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif // __cplusplus
20
Timothy Trippeleee5c3f2022-03-10 22:10:31 -080021/**
22 * Helper X macro for defining enums and case statements related to ADC
23 * Controller channels. If an additional channel is ever added to the hardware,
24 * this list can be updated.
25 */
26#define DIF_ADC_CTRL_CHANNEL_LIST(X) \
27 X(0) \
28 X(1)
29
30/**
31 * Helper X macro for defining enums and case statements related to ADC
32 * Controller filters. If an additional filter is ever added to the hardware,
33 * this list can be updated.
34 */
35#define DIF_ADC_CTRL_FILTER_LIST(X) \
36 X(0) \
37 X(1) \
38 X(2) \
39 X(3) \
40 X(4) \
41 X(5) \
42 X(6) \
43 X(7)
44
45/**
46 * Helper macro for defining a `dif_adc_ctrl_channel_t` enumeration constant.
47 * @channel_ ADC Controller channel of the enumeration constant.
48 */
49#define DIF_ADC_CTRL_CHANNEL_ENUM_INIT_(channel_) \
50 kDifAdcCtrlChannel##channel_ = channel_,
51
52/**
53 * An ADC Controller Channel.
54 */
55typedef enum dif_adc_ctrl_channel {
56 DIF_ADC_CTRL_CHANNEL_LIST(DIF_ADC_CTRL_CHANNEL_ENUM_INIT_)
57} dif_adc_ctrl_channel_t;
58
59#undef DIF_ADC_CTRL_CHANNEL_ENUM_INIT_
60
61/**
62 * Helper macro for defining a `dif_adc_ctrl_filter_t` enumeration constant.
63 * @filter_ ADC Controller filter of the enumeration constant.
64 */
65#define DIF_ADC_CTRL_FILTER_ENUM_INIT_(filter_) \
Timothy Trippel933bb162022-03-15 00:00:19 -070066 kDifAdcCtrlFilter##filter_ = filter_,
Timothy Trippeleee5c3f2022-03-10 22:10:31 -080067
68/**
69 * An ADC Controller filter.
70 *
71 * Each channel has a separate instance of each filter. For example, if there
72 * are two channels and eight filters, there would be a total of 16 filter
73 * instances that may be configured.
74 */
75typedef enum dif_adc_ctrl_filter {
76 DIF_ADC_CTRL_FILTER_LIST(DIF_ADC_CTRL_FILTER_ENUM_INIT_)
77} dif_adc_ctrl_filter_t;
78
79#undef DIF_ADC_CTRL_FILTER_ENUM_INIT_
80
81/**
82 * Helper macro for defining a `dif_adc_ctrl_irq_cause_t` enumeration constant.
83 * @filter_cause_ ADC Controller IRQ filter cause of the enumeration constant.
84 */
85#define DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_(filter_cause_) \
86 kDifAdcCtrlIrqCauseFilter##filter_cause_ = 1U << filter_cause_,
87
88/**
89 * An ADC Controller IRQ cause.
90 *
91 * The ADC Controller can only generate a single interrupt (the `debug_cable`
92 * interrupt). However, depending on how the ADC Controller is configured, there
93 * are several causes that could trigger this interrupt. These include filter
94 * matches (when in Normal Power Scan mode), or sample completion (when in
95 * Oneshot mode).
96 */
97typedef enum dif_adc_ctrl_irq_cause {
98 DIF_ADC_CTRL_FILTER_LIST(DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_)
99 /**
100 * Sample ready cause in Oneshot mode.
101 */
Timothy Trippelebe223e2023-02-01 15:57:40 -0800102 kDifAdcCtrlIrqCauseOneshot = 1U << ADC_CTRL_ADC_INTR_STATUS_ONESHOT_BIT,
Timothy Trippeld1dc5a92022-03-16 22:59:51 -0700103 /**
104 * All IRQ causes ORed together.
105 *
106 * This is useful when clearing all IRQ causes at once, to initialize the ADC
107 * Controller.
108 */
Timothy Trippelebe223e2023-02-01 15:57:40 -0800109 kDifAdcCtrlIrqCauseAll =
110 (1U << (ADC_CTRL_ADC_INTR_STATUS_ONESHOT_BIT + 1)) - 1,
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800111} dif_adc_ctrl_irq_cause_t;
112
113#undef DIF_ADC_CTRL_IRQ_CAUSE_ENUM_INIT_
114
115/**
116 * Operation mode of the ADC Controller.
117 */
118typedef enum dif_adc_ctrl_mode {
119 /**
120 * Low Power (Continuous) Scan mode.
121 *
122 * In Low Power Scan mode, the ADC periodically samples enabled channels, and
123 * upon matching a set of enabled filters, a set number of times, will
124 * transition to Normal Power Scan mode. If no filters are enabled, then the
125 * ADC controller will never transition to Normal Power Scan mode.
126 */
127 kDifAdcCtrlLowPowerScanMode = 0,
128 /**
129 * Normal Power (Continuous) Scan mode.
130 *
131 * In Normal Power Scan mode, the ADC samples enabled channels as fast as
132 * possible, and upon matching a set of enabled filters, a set number of
133 * consecutive times, may trigger a system wakeup and/or IRQ. Similar to Low
134 * Power Scan mode, if no filters are enabled, then a system wakeup and/or IRQ
135 * will never be triggered.
136 */
137 kDifAdcCtrlNormalPowerScanMode = 1,
138 /**
139 * Oneshot mode.
140 *
141 * In Oneshot mode, an ADC channel is triggered to take a single sample, upon
142 * being enabled, and optionally, raises an interrupt upon completion. Unlike
143 * the Scan modes, in Oneshot mode, the ADC Controller does not attempt to
144 * filter samples. Rather, an IRQ may be raised immediately upon the sample
145 * being ready, regardless of what the sample is. After the sample is
146 * completed the ADC is powered down, until another sample is triggered,
147 * either by toggling the channel's enable bit on and off, or by resetting the
148 * sampling FSM.
149 */
150 kDifAdcCtrlOneshotMode = 2,
151} dif_adc_ctrl_mode_t;
152
153/**
154 * Runtime configuration for an ADC Controller.
155 */
156typedef struct dif_adc_ctrl_config {
157 /**
158 * The sampling mode to configure the ADC Controller in.
159 */
160 dif_adc_ctrl_mode_t mode;
161 /**
162 * The time to allow the ADC to power up.
163 *
164 * Units: always-on clock cycles
165 */
166 uint8_t power_up_time_aon_cycles;
167 /**
168 * The sampling period when in Low Power Scan mode, i.e., how often the ADC
169 * Controller wakes up the ADC to take a sample.
170 *
171 * Units: always-on clock cycles
172 *
173 * Only relevant in Low Power Scan mode.
174 */
Timothy Trippel933bb162022-03-15 00:00:19 -0700175 uint32_t wake_up_time_aon_cycles;
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800176 /**
177 * The number of filter-matching samples to count in Low Power Scan mode
178 * before switching to Normal Power Scan mode.
179 *
180 * Only relevant in Low Power Scan mode.
181 */
182 uint8_t num_low_power_samples;
183 /**
184 * The number of filter-matching samples to count in Normal Power Scan mode
185 * before triggering a system wakeup and/or interrupt.
186 */
187 uint16_t num_normal_power_samples;
188} dif_adc_ctrl_config_t;
189
190/**
191 * Runtime configuration for an ADC Controller filter.
192 */
193typedef struct dif_adc_ctrl_filter_config {
194 /**
195 * The ADC Controller filter this configuration applies to.
196 */
197 dif_adc_ctrl_filter_t filter;
198 /**
199 * The minimum voltage (inclusive) of the range defined by this filter.
200 *
201 * Valid range: [0, 1024)
202 * Units: 2.148 mV (i.e., range / 2 ^ 10)
203 */
204 uint16_t min_voltage;
205 /**
206 * The maximum voltage (inclusive) of the range defined by this filter.
207 *
208 * Valid range: [0, 1024)
209 * Units: 2.148 mV (i.e., range / 2 ^ 10)
210 */
211 uint16_t max_voltage;
212 /**
213 * Where a filter hit is classfied as an (inclusive) in-range hit, or
214 * (exclusive) out-of-range hit.
215 */
216 bool in_range;
217 /**
218 * Whether to generate a system wakeup on a filter match after saturating the
219 * `num_normal_power_samples` threshold in Normal Power Scan mode.
220 */
221 bool generate_wakeup_on_match;
222 /**
223 * Whether to generate a `debug_cable` interrupt on a filter match after
224 * saturating the `num_normal_power_samples` threshold in Normal Power Scan
225 * mode.
226 */
227 bool generate_irq_on_match;
228} dif_adc_ctrl_filter_config_t;
229
230/**
231 * Configures an ADC Controller.
232 *
233 * @param adc_ctrl An adc_ctrl handle.
234 * @param config Runtime configuration parameters.
235 * @return The result of the operation.
236 */
237OT_WARN_UNUSED_RESULT
238dif_result_t dif_adc_ctrl_configure(const dif_adc_ctrl_t *adc_ctrl,
Timothy Trippel933bb162022-03-15 00:00:19 -0700239 dif_adc_ctrl_config_t config);
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800240
241/**
242 * Configures a channel filter.
243 *
Timothy Trippel933bb162022-03-15 00:00:19 -0700244 * This should be invoked for each desired filter _before_ the sampling sequence
245 * is enabled via `dif_adc_ctrl_set_enabled()`.
246 *
247 * This only applies in Low / Normal Power Scan sampling modes.
248 *
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800249 * @param adc_ctrl An adc_ctrl handle.
250 * @param channel The channel of the filter to configure.
251 * @param config Runtime configuration parameters for the filter.
252 * @param enabled The enablement state to configure the filter in.
253 * @return The result of the operation.
254 */
255OT_WARN_UNUSED_RESULT
256dif_result_t dif_adc_ctrl_configure_filter(const dif_adc_ctrl_t *adc_ctrl,
257 dif_adc_ctrl_channel_t channel,
258 dif_adc_ctrl_filter_config_t config,
259 dif_toggle_t enabled);
260
261/**
262 * Sets the enablement state of the ADC Controller.
263 *
264 * Enabling the ADC Controller powers it up, while disabling the ADC Controller
265 * powers it down and resets the sampling FSM. After powering up, sampling
266 * begins, regardless of the operation mode.
267 *
268 * @param adc_ctrl An adc_ctrl handle.
269 * @param enabled The enablement state to configure the ADC Controller in.
270 * @return The result of the operation.
271 */
272OT_WARN_UNUSED_RESULT
273dif_result_t dif_adc_ctrl_set_enabled(const dif_adc_ctrl_t *adc_ctrl,
274 dif_toggle_t enabled);
275
276/**
277 * Gets the enablement state of the ADC Controller.
278 *
279 * If the ADC Controller is enabled, it is powered up, or being powered up.
280 *
281 * @param adc_ctrl An adc_ctrl handle.
282 * @param[out] is_enabled The enablement state of the ADC Controller.
283 * @return The result of the operation.
284 */
285OT_WARN_UNUSED_RESULT
286dif_result_t dif_adc_ctrl_get_enabled(const dif_adc_ctrl_t *adc_ctrl,
287 dif_toggle_t *is_enabled);
288
289/**
290 * Sets the enablement state of the specified filter for the specified channel.
291 *
292 * @param adc_ctrl An adc_ctrl handle.
293 * @param channel The channel the filter resides in.
294 * @param filter The filter to set the enablement state of.
295 * @param enabled The enablement state to configure the filter in.
296 * @return The result of the operation.
297 */
298OT_WARN_UNUSED_RESULT
299dif_result_t dif_adc_ctrl_filter_set_enabled(const dif_adc_ctrl_t *adc_ctrl,
300 dif_adc_ctrl_channel_t channel,
301 dif_adc_ctrl_filter_t filter,
302 dif_toggle_t enabled);
303
304/**
305 * Gets the enablement state of the specified filter for the specified channel.
306 *
307 * @param adc_ctrl An adc_ctrl handle.
308 * @param channel The channel the filter resides in.
309 * @param filter The filter to get the enablement state of.
310 * @param[out] is_enabled The enablement state of the filter.
311 * @return The result of the operation.
312 */
313OT_WARN_UNUSED_RESULT
314dif_result_t dif_adc_ctrl_filter_get_enabled(const dif_adc_ctrl_t *adc_ctrl,
315 dif_adc_ctrl_channel_t channel,
316 dif_adc_ctrl_filter_t filter,
317 dif_toggle_t *enabled);
318
319/**
320 * Get the sampled value from the specified channel that triggered the IRQ.
321 *
322 * Values are 10-bits in the range from 0V to 2.2V. Based on this, the
323 * resolution (and units) of the sample are in increments of 2.148mV.
324 *
325 * @param adc_ctrl An adc_ctrl handle.
326 * @param channel The channel to read the sample from.
327 * @param[out] value The value of the sample.
328 * @return The result of the operation.
329 */
330OT_WARN_UNUSED_RESULT
331dif_result_t dif_adc_ctrl_get_triggered_value(const dif_adc_ctrl_t *adc_ctrl,
332 dif_adc_ctrl_channel_t channel,
333 uint16_t *value);
334
335/**
336 * Get the latest sampled value from the specified channel.
337 *
338 * Since in Normal Power Scan mode, sampling continues even after an IRQ has
339 * been raised, the value returned by this function may be different than the
340 * value returned by `dif_adc_ctrl_get_irq_value()`.
341 *
342 * Values are 10-bits in the range from 0V to 2.2V. Based on this, the
343 * resolution (and units) of the sample are in increments of 2.148mV.
344 *
345 * @param adc_ctrl An adc_ctrl handle.
346 * @param channel The channel to read the sample from.
347 * @param[out] value The value of the sample.
348 * @return The result of the operation.
349 */
350OT_WARN_UNUSED_RESULT
351dif_result_t dif_adc_ctrl_get_latest_value(const dif_adc_ctrl_t *adc_ctrl,
352 dif_adc_ctrl_channel_t channel,
353 uint16_t *value);
354
355/**
Timothy Trippel933bb162022-03-15 00:00:19 -0700356 * Reset all ADC Controller FSMs and counters, and if enabled, begin sampling
357 * sequence.
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800358 *
359 * @param adc_ctrl An adc_ctrl handle.
360 * @return The result of the operation.
361 */
362OT_WARN_UNUSED_RESULT
363dif_result_t dif_adc_ctrl_reset(const dif_adc_ctrl_t *adc_ctrl);
364
365/**
366 * Gets the cause(s) of a `debug_cable` IRQ.
367 *
368 * IRQs can be triggered by filter matches in Normal Power Scan mode (after
369 * saturating the `num_normal_power_samples` threshold), or after a single
370 * sample capture in Oneshot mode.
371 *
372 * @param adc_ctrl An adc_ctrl handle.
373 * @param[out] causes The causes of the IRQ (one or more
374 * `dif_adc_ctrl_irq_cause_t`s ORed together).
375 * @return The result of the operation.
376 */
377OT_WARN_UNUSED_RESULT
378dif_result_t dif_adc_ctrl_irq_get_causes(const dif_adc_ctrl_t *adc_ctrl,
379 uint32_t *causes);
380
381/**
382 * Clears the cause(s) of a `debug_cable` IRQ.
383 *
Timothy Trippel883ffd02022-03-29 13:22:11 -0700384 * TODO(lowRISC/opentitan:#11354): future releases of the HW should hide the
385 * filter and interrupt status registers behind the standardized IRQ registers.
386 * For now, the autogenerated `dif_adc_ctrl_irq_acknowledge[_all]()` DIF may be
387 * used to clear the main IRQ status register, while this DIF may be used to
388 * clear the local cause / filter status registers.
389 *
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800390 * @param adc_ctrl An adc_ctrl handle.
Timothy Trippel46f468f2022-03-17 00:28:34 -0700391 * @param causes The causes of the IRQ (one or more `dif_adc_ctrl_irq_cause_t`s
392 * ORed together).
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800393 * @return The result of the operation.
394 */
395OT_WARN_UNUSED_RESULT
396dif_result_t dif_adc_ctrl_irq_clear_causes(const dif_adc_ctrl_t *adc_ctrl,
397 uint32_t causes);
398
399/**
400 * Sets the enablement of generating system wakeups on a filter match.
401 *
402 * Only relevant in Normal Power Scan mode (and Low Power Scan mode, which can
403 * transition to Normal Power Scan mode).
404 *
405 * @param adc_ctrl An adc_ctrl handle.
406 * @param filter A filter to enable wakeup triggering for.
407 * @param enabled The enablement state to set.
408 * @return The result of the operation.
409 */
Guillermo Maturana59bfc512022-07-22 11:43:58 -0700410OT_WARN_UNUSED_RESULT
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800411dif_result_t dif_adc_ctrl_filter_match_wakeup_set_enabled(
412 const dif_adc_ctrl_t *adc_ctrl, dif_adc_ctrl_filter_t filter,
413 dif_toggle_t enabled);
414
415/**
416 * Gets the enablement of generating system wakeups on a filter match.
417 *
418 * @param adc_ctrl An adc_ctrl handle.
419 * @param filter A filter to enable wakeup triggering for.
420 * @param[out] is_enabled The enablement state retrieved.
421 * @return The result of the operation.
422 */
Guillermo Maturana59bfc512022-07-22 11:43:58 -0700423OT_WARN_UNUSED_RESULT
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800424dif_result_t dif_adc_ctrl_filter_match_wakeup_get_enabled(
425 const dif_adc_ctrl_t *adc_ctrl, dif_adc_ctrl_filter_t filter,
426 dif_toggle_t *is_enabled);
427
428/**
Timothy Trippel46f468f2022-03-17 00:28:34 -0700429 * Sets the enablement of generating a `debug_cable` IRQ for given cause(s).
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800430 *
431 * Causes can be filter matches (in Normal Power Scan mode), or when a sample is
432 * complete (in Oneshot mode).
433 *
434 * @param adc_ctrl An adc_ctrl handle.
Timothy Trippel46f468f2022-03-17 00:28:34 -0700435 * @param causes Causes (one or more `dif_adc_ctrl_irq_cause_t`s ORed together)
436 * to generate the `debug_cable` IRQ for.
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800437 * @param enabled The enablement state to set.
438 * @return The result of the operation.
439 */
Guillermo Maturana59bfc512022-07-22 11:43:58 -0700440OT_WARN_UNUSED_RESULT
Timothy Trippel46f468f2022-03-17 00:28:34 -0700441dif_result_t dif_adc_ctrl_irq_cause_set_enabled(const dif_adc_ctrl_t *adc_ctrl,
442 uint32_t causes,
443 dif_toggle_t enabled);
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800444
445/**
Timothy Trippel46f468f2022-03-17 00:28:34 -0700446 * Gets the causes that will generate a `debug_cable` IRQ.
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800447 *
448 * @param adc_ctrl An adc_ctrl handle.
Timothy Trippel46f468f2022-03-17 00:28:34 -0700449 * @param[out] enabled_causes Causes (one or more `dif_adc_ctrl_irq_cause_t`s
450 * ORed together) that will generate the
451 * `debug_cable` IRQ.
Timothy Trippeleee5c3f2022-03-10 22:10:31 -0800452 * @return The result of the operation.
453 */
Guillermo Maturana59bfc512022-07-22 11:43:58 -0700454OT_WARN_UNUSED_RESULT
Timothy Trippel46f468f2022-03-17 00:28:34 -0700455dif_result_t dif_adc_ctrl_irq_cause_get_enabled(const dif_adc_ctrl_t *adc_ctrl,
456 uint32_t *enabled_causes);
Srikrishna Iyer0e687042021-10-23 14:53:47 -0700457
458#ifdef __cplusplus
459} // extern "C"
460#endif // __cplusplus
461
462#endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_ADC_CTRL_H_