forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dac_mcux_lpdac.c
112 lines (91 loc) · 3.3 KB
/
dac_mcux_lpdac.c
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
/*
* Copyright (c) 2020 Henrik Brix Andersen <[email protected]>
* Copyright (c) 2023, NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_lpdac
#include <zephyr/kernel.h>
#include <zephyr/drivers/dac.h>
#include <zephyr/logging/log.h>
#include <fsl_dac.h>
LOG_MODULE_REGISTER(dac_mcux_lpdac, CONFIG_DAC_LOG_LEVEL);
struct mcux_lpdac_config {
LPDAC_Type *base;
dac_reference_voltage_source_t ref_voltage;
bool low_power;
};
struct mcux_lpdac_data {
bool configured;
};
static int mcux_lpdac_channel_setup(const struct device *dev,
const struct dac_channel_cfg *channel_cfg)
{
const struct mcux_lpdac_config *config = dev->config;
struct mcux_lpdac_data *data = dev->data;
dac_config_t dac_config;
if (channel_cfg->channel_id != 0) {
LOG_ERR("unsupported channel %d", channel_cfg->channel_id);
return -ENOTSUP;
}
if (channel_cfg->resolution != 12) {
LOG_ERR("unsupported resolution %d", channel_cfg->resolution);
return -ENOTSUP;
}
if (channel_cfg->internal) {
LOG_ERR("Internal channels not supported");
return -ENOTSUP;
}
DAC_GetDefaultConfig(&dac_config);
dac_config.referenceVoltageSource = config->ref_voltage;
#if defined(FSL_FEATURE_LPDAC_HAS_GCR_BUF_SPD_CTRL) && FSL_FEATURE_LPDAC_HAS_GCR_BUF_SPD_CTRL
dac_config.enableLowerLowPowerMode = config->low_power;
#else
dac_config.enableLowPowerMode = config->low_power;
#endif
DAC_Init(config->base, &dac_config);
DAC_Enable(config->base, false);
data->configured = true;
return 0;
}
static int mcux_lpdac_write_value(const struct device *dev, uint8_t channel, uint32_t value)
{
const struct mcux_lpdac_config *config = dev->config;
struct mcux_lpdac_data *data = dev->data;
if (!data->configured) {
LOG_ERR("channel not initialized");
return -EINVAL;
}
if (channel != 0) {
LOG_ERR("unsupported channel %d", channel);
return -ENOTSUP;
}
if (value >= 4096) {
LOG_ERR("unsupported value %d", value);
return -EINVAL;
}
DAC_Enable(config->base, true);
DAC_SetData(config->base, value);
return 0;
}
static int mcux_lpdac_init(const struct device *dev)
{
return 0;
}
static const struct dac_driver_api mcux_lpdac_driver_api = {
.channel_setup = mcux_lpdac_channel_setup,
.write_value = mcux_lpdac_write_value,
};
#define MCUX_LPDAC_INIT(n) \
static struct mcux_lpdac_data mcux_lpdac_data_##n; \
\
static const struct mcux_lpdac_config mcux_lpdac_config_##n = { \
.base = (LPDAC_Type *)DT_INST_REG_ADDR(n), \
.ref_voltage = DT_INST_PROP(n, voltage_reference), \
.low_power = DT_INST_PROP(n, low_power_mode), \
}; \
\
DEVICE_DT_INST_DEFINE(n, mcux_lpdac_init, NULL, &mcux_lpdac_data_##n, \
&mcux_lpdac_config_##n, POST_KERNEL, CONFIG_DAC_INIT_PRIORITY, \
&mcux_lpdac_driver_api);
DT_INST_FOREACH_STATUS_OKAY(MCUX_LPDAC_INIT)