1 /*
2 * Copyright (c) 2020 Henrik Brix Andersen <henrik@brixandersen.dk>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nxp_kinetis_dac32
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/drivers/dac.h>
11 #include <zephyr/logging/log.h>
12 #include <zephyr/drivers/pinctrl.h>
13
14 #include <fsl_dac32.h>
15
16 LOG_MODULE_REGISTER(dac_mcux_dac32, CONFIG_DAC_LOG_LEVEL);
17
18 struct mcux_dac32_config {
19 DAC_Type *base;
20 dac32_reference_voltage_source_t reference;
21 bool buffered;
22 bool low_power;
23 const struct pinctrl_dev_config *pincfg;
24 };
25
26 struct mcux_dac32_data {
27 bool configured;
28 };
29
mcux_dac32_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)30 static int mcux_dac32_channel_setup(const struct device *dev,
31 const struct dac_channel_cfg *channel_cfg)
32 {
33 const struct mcux_dac32_config *config = dev->config;
34 struct mcux_dac32_data *data = dev->data;
35 dac32_config_t dac32_config;
36
37 if (channel_cfg->channel_id != 0) {
38 LOG_ERR("unsupported channel %d", channel_cfg->channel_id);
39 return -ENOTSUP;
40 }
41
42 if (channel_cfg->resolution != 12) {
43 LOG_ERR("unsupported resolution %d", channel_cfg->resolution);
44 return -ENOTSUP;
45 }
46
47 if (channel_cfg->internal) {
48 LOG_ERR("Internal channels not supported");
49 return -ENOTSUP;
50 }
51
52 DAC32_GetDefaultConfig(&dac32_config);
53 dac32_config.enableLowPowerMode = config->low_power;
54 dac32_config.referenceVoltageSource = config->reference;
55
56 DAC32_Init(config->base, &dac32_config);
57 DAC32_EnableBufferOutput(config->base, config->buffered);
58
59 DAC32_EnableTestOutput(config->base,
60 IS_ENABLED(CONFIG_DAC_MCUX_DAC32_TESTOUT));
61
62 data->configured = true;
63
64 return 0;
65 }
66
mcux_dac32_write_value(const struct device * dev,uint8_t channel,uint32_t value)67 static int mcux_dac32_write_value(const struct device *dev, uint8_t channel,
68 uint32_t value)
69 {
70 const struct mcux_dac32_config *config = dev->config;
71 struct mcux_dac32_data *data = dev->data;
72
73 if (!data->configured) {
74 LOG_ERR("channel not initialized");
75 return -EINVAL;
76 }
77
78 if (channel != 0) {
79 LOG_ERR("unsupported channel %d", channel);
80 return -ENOTSUP;
81 }
82
83 if (value >= 4096) {
84 LOG_ERR("value %d out of range", value);
85 return -EINVAL;
86 }
87
88 /* Static operation */
89 DAC32_EnableBuffer(config->base, false);
90
91 DAC32_SetBufferValue(config->base, 0, value);
92 DAC32_Enable(config->base, true);
93
94 return 0;
95 }
96
mcux_dac32_init(const struct device * dev)97 static int mcux_dac32_init(const struct device *dev)
98 {
99 const struct mcux_dac32_config *config = dev->config;
100
101 return pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
102 }
103
104 static DEVICE_API(dac, mcux_dac32_driver_api) = {
105 .channel_setup = mcux_dac32_channel_setup,
106 .write_value = mcux_dac32_write_value,
107 };
108
109 #define TO_DAC32_VREF_SRC(val) \
110 _DO_CONCAT(kDAC32_ReferenceVoltageSourceVref, val)
111
112 #define MCUX_DAC32_INIT(n) \
113 static struct mcux_dac32_data mcux_dac32_data_##n; \
114 \
115 PINCTRL_DT_INST_DEFINE(n); \
116 \
117 static const struct mcux_dac32_config mcux_dac32_config_##n = { \
118 .base = (DAC_Type *)DT_INST_REG_ADDR(n), \
119 .reference = \
120 TO_DAC32_VREF_SRC(DT_INST_PROP(n, voltage_reference)), \
121 .buffered = DT_INST_PROP(n, buffered), \
122 .low_power = DT_INST_PROP(n, low_power_mode), \
123 .pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
124 }; \
125 \
126 DEVICE_DT_INST_DEFINE(n, mcux_dac32_init, NULL, \
127 &mcux_dac32_data_##n, \
128 &mcux_dac32_config_##n, \
129 POST_KERNEL, CONFIG_DAC_INIT_PRIORITY, \
130 &mcux_dac32_driver_api);
131
132 DT_INST_FOREACH_STATUS_OKAY(MCUX_DAC32_INIT)
133