|
| 1 | +/* |
| 2 | + * Copyright 2025 NXP |
| 3 | + * |
| 4 | + * Based on adc_mcux_sar_adc.c, which is: |
| 5 | + * Copyright 2023-2024 NXP |
| 6 | + * Copyright (c) 2020 Toby Firth |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + */ |
| 10 | + |
| 11 | +#define DT_DRV_COMPAT nxp_sar_adc |
| 12 | + |
| 13 | +#include <errno.h> |
| 14 | +#include <zephyr/drivers/adc.h> |
| 15 | +#include <zephyr/sys/util.h> |
| 16 | +#include <zephyr/drivers/clock_control.h> |
| 17 | +#include <zephyr/drivers/pinctrl.h> |
| 18 | + |
| 19 | +#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL |
| 20 | +#include <zephyr/logging/log.h> |
| 21 | +#include <zephyr/irq.h> |
| 22 | +#include <fsl_sar_adc.h> |
| 23 | +LOG_MODULE_REGISTER(adc_mcux_sar_adc); |
| 24 | + |
| 25 | +#define ADC_CONTEXT_USES_KERNEL_TIMER |
| 26 | +#include "adc_context.h" |
| 27 | + |
| 28 | +struct mcux_sar_adc_config { |
| 29 | + ADC_Type *base; |
| 30 | + void (*irq_config_func)(const struct device *dev); |
| 31 | + const struct device *clock_dev; |
| 32 | + clock_control_subsys_t clock_subsys; |
| 33 | +}; |
| 34 | + |
| 35 | +struct mcux_sar_adc_data { |
| 36 | + const struct device *dev; |
| 37 | + struct adc_context ctx; |
| 38 | + uint16_t *buffer; |
| 39 | + uint16_t *repeat_buffer; |
| 40 | + uint32_t channels; |
| 41 | +}; |
| 42 | + |
| 43 | +static int mcux_sar_adc_channel_setup(const struct device *dev, |
| 44 | + const struct adc_channel_cfg *channel_cfg) |
| 45 | +{ |
| 46 | + /* User may configure maximum number of active channels */ |
| 47 | + if (channel_cfg->channel_id >= CONFIG_SAR_ADC_CHANNEL_COUNT) { |
| 48 | + LOG_ERR("Channel %d is not valid", channel_cfg->channel_id); |
| 49 | + return -EINVAL; |
| 50 | + } |
| 51 | + |
| 52 | + if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) { |
| 53 | + LOG_ERR("Unsupported channel acquisition time"); |
| 54 | + return -ENOTSUP; |
| 55 | + } |
| 56 | + |
| 57 | + if (channel_cfg->gain != ADC_GAIN_1) { |
| 58 | + LOG_ERR("Unsupported channel gain %d", channel_cfg->gain); |
| 59 | + return -ENOTSUP; |
| 60 | + } |
| 61 | + |
| 62 | + if (channel_cfg->reference != ADC_REF_INTERNAL) { |
| 63 | + LOG_ERR("Unsupported channel reference"); |
| 64 | + return -ENOTSUP; |
| 65 | + } |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +static int mcux_sar_adc_start_read(const struct device *dev, const struct adc_sequence *sequence) |
| 71 | +{ |
| 72 | + const struct mcux_sar_adc_config *config = dev->config; |
| 73 | + struct mcux_sar_adc_data *data = dev->data; |
| 74 | + ADC_Type *base = config->base; |
| 75 | + uint8_t channel_id; |
| 76 | + |
| 77 | + if (sequence->resolution != 12) { |
| 78 | + LOG_ERR("Unsupported resolution %d", sequence->resolution); |
| 79 | + return -ENOTSUP; |
| 80 | + } |
| 81 | + |
| 82 | + channel_id = CONFIG_SAR_ADC_CHANNEL_COUNT; |
| 83 | + while (channel_id-- > 0) { |
| 84 | + if (sequence->channels & BIT(channel_id)) { |
| 85 | + ADC_EnableSpecificChannelNormalConv(base, channel_id); |
| 86 | + } else { |
| 87 | + ADC_DisableSpecificChannelNormalConv(base, channel_id); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + data->buffer = sequence->buffer; |
| 92 | + |
| 93 | + adc_context_start_read(&data->ctx, sequence); |
| 94 | + int error = adc_context_wait_for_completion(&data->ctx); |
| 95 | + |
| 96 | + return error; |
| 97 | +} |
| 98 | + |
| 99 | +static int mcux_sar_adc_read_async(const struct device *dev, const struct adc_sequence *sequence, |
| 100 | + struct k_poll_signal *async) |
| 101 | +{ |
| 102 | + struct mcux_sar_adc_data *data = dev->data; |
| 103 | + int error; |
| 104 | + |
| 105 | + adc_context_lock(&data->ctx, async ? true : false, async); |
| 106 | + error = mcux_sar_adc_start_read(dev, sequence); |
| 107 | + adc_context_release(&data->ctx, error); |
| 108 | + |
| 109 | + return error; |
| 110 | +} |
| 111 | + |
| 112 | +static int mcux_sar_adc_read(const struct device *dev, const struct adc_sequence *sequence) |
| 113 | +{ |
| 114 | + return mcux_sar_adc_read_async(dev, sequence, NULL); |
| 115 | +} |
| 116 | + |
| 117 | +static void adc_context_start_sampling(struct adc_context *ctx) |
| 118 | +{ |
| 119 | + struct mcux_sar_adc_data *data = CONTAINER_OF(ctx, struct mcux_sar_adc_data, ctx); |
| 120 | + const struct mcux_sar_adc_config *config = data->dev->config; |
| 121 | + ADC_Type *base = config->base; |
| 122 | + |
| 123 | + data->channels = ctx->sequence.channels; |
| 124 | + data->repeat_buffer = data->buffer; |
| 125 | + |
| 126 | + ADC_StartConvChain(base, kADC_NormalConvOneShotMode); |
| 127 | +} |
| 128 | + |
| 129 | +static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling) |
| 130 | +{ |
| 131 | + struct mcux_sar_adc_data *data = CONTAINER_OF(ctx, struct mcux_sar_adc_data, ctx); |
| 132 | + |
| 133 | + if (repeat_sampling) { |
| 134 | + data->buffer = data->repeat_buffer; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +static void mcux_sar_adc_isr(const struct device *dev) |
| 139 | +{ |
| 140 | + const struct mcux_sar_adc_config *config = dev->config; |
| 141 | + struct mcux_sar_adc_data *data = dev->data; |
| 142 | + ADC_Type *base = config->base; |
| 143 | + adc_conv_result_t conv_result; |
| 144 | + uint16_t channel_id; |
| 145 | + |
| 146 | + if (((ADC_GetConvIntStatus(base) & kADC_NormalConvChainEndIntFlag))) { |
| 147 | + ADC_ClearConvIntStatus(base, kADC_NormalConvChainEndIntFlag); |
| 148 | + } |
| 149 | + |
| 150 | + for (channel_id = 0; channel_id < CONFIG_SAR_ADC_CHANNEL_COUNT; channel_id++) { |
| 151 | + if (ADC_GetChannelConvResult(base, &conv_result, channel_id)) { |
| 152 | + data->channels &= ~BIT(channel_id); |
| 153 | + *(data->buffer++) = conv_result.convData; |
| 154 | + if (data->channels == 0) { |
| 155 | + adc_context_on_sampling_done(&data->ctx, dev); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +static int mcux_sar_adc_init(const struct device *dev) |
| 162 | +{ |
| 163 | + const struct mcux_sar_adc_config *config = dev->config; |
| 164 | + struct mcux_sar_adc_data *data = dev->data; |
| 165 | + ADC_Type *base = config->base; |
| 166 | + adc_config_t adc_config; |
| 167 | + adc_calibration_config_t calibrationConfig; |
| 168 | + |
| 169 | + ADC_GetDefaultConfig(&adc_config); |
| 170 | + ADC_Init(base, &adc_config); |
| 171 | + ADC_SetConvMode(base, kADC_NormalConvOneShotMode); |
| 172 | + ADC_EnableConvInt(base, (uint32_t)kADC_NormalConvChainEndIntEnable); |
| 173 | + |
| 174 | + /* Do calibration to reduce or eliminate the various error contribution effects. */ |
| 175 | + calibrationConfig.enableAverage = true; |
| 176 | + calibrationConfig.sampleTime = kADC_SampleTime22; |
| 177 | +#if (defined(FSL_FEATURE_ADC_HAS_CALBISTREG) && (FSL_FEATURE_ADC_HAS_CALBISTREG == 1U)) |
| 178 | + calibrationConfig.averageSampleNumbers = kADC_AverageSampleNumbers32; |
| 179 | +#else |
| 180 | + calibrationConfig.averageSampleNumbers = kADC_AverageSampleNumbers512; |
| 181 | +#endif /* FSL_FEATURE_ADC_HAS_CALBISTREG */ |
| 182 | + |
| 183 | + if (!(ADC_DoCalibration(base, &calibrationConfig))) { |
| 184 | + LOG_WRN("Calibration failed."); |
| 185 | + } |
| 186 | + |
| 187 | + config->irq_config_func(dev); |
| 188 | + data->dev = dev; |
| 189 | + |
| 190 | + adc_context_unlock_unconditionally(&data->ctx); |
| 191 | + |
| 192 | + return 0; |
| 193 | +} |
| 194 | + |
| 195 | +static DEVICE_API(adc, mcux_sar_adc_driver_api) = { |
| 196 | + .channel_setup = mcux_sar_adc_channel_setup, |
| 197 | + .read = mcux_sar_adc_read, |
| 198 | +#ifdef CONFIG_ADC_ASYNC |
| 199 | + .read_async = mcux_sar_adc_read_async, |
| 200 | +#endif |
| 201 | +}; |
| 202 | + |
| 203 | +#define SAR_ADC_MCUX_INIT(n) \ |
| 204 | + \ |
| 205 | + static void mcux_sar_adc_config_func_##n(const struct device *dev); \ |
| 206 | + \ |
| 207 | + static const struct mcux_sar_adc_config mcux_sar_adc_config_##n = { \ |
| 208 | + .base = (ADC_Type *)DT_INST_REG_ADDR(n), \ |
| 209 | + .irq_config_func = mcux_sar_adc_config_func_##n, \ |
| 210 | + .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \ |
| 211 | + .clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name), \ |
| 212 | + }; \ |
| 213 | + static struct mcux_sar_adc_data mcux_sar_adc_data_##n = { \ |
| 214 | + ADC_CONTEXT_INIT_TIMER(mcux_sar_adc_data_##n, ctx), \ |
| 215 | + ADC_CONTEXT_INIT_LOCK(mcux_sar_adc_data_##n, ctx), \ |
| 216 | + ADC_CONTEXT_INIT_SYNC(mcux_sar_adc_data_##n, ctx), \ |
| 217 | + }; \ |
| 218 | + \ |
| 219 | + DEVICE_DT_INST_DEFINE(n, &mcux_sar_adc_init, NULL, &mcux_sar_adc_data_##n, \ |
| 220 | + &mcux_sar_adc_config_##n, POST_KERNEL, CONFIG_ADC_INIT_PRIORITY, \ |
| 221 | + &mcux_sar_adc_driver_api); \ |
| 222 | + \ |
| 223 | + static void mcux_sar_adc_config_func_##n(const struct device *dev) \ |
| 224 | + { \ |
| 225 | + IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), mcux_sar_adc_isr, \ |
| 226 | + DEVICE_DT_INST_GET(n), 0); \ |
| 227 | + \ |
| 228 | + irq_enable(DT_INST_IRQN(n)); \ |
| 229 | + } |
| 230 | + |
| 231 | +DT_INST_FOREACH_STATUS_OKAY(SAR_ADC_MCUX_INIT) |
0 commit comments