1 /*
2  * Copyright (c) 2020 Henrik Brix Andersen <henrik@brixandersen.dk>
3  * Copyright (c) 2023, NXP
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 #define DT_DRV_COMPAT nxp_lpdac
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/drivers/dac.h>
11 #include <zephyr/logging/log.h>
12 
13 #include <fsl_dac.h>
14 
15 LOG_MODULE_REGISTER(dac_mcux_lpdac, CONFIG_DAC_LOG_LEVEL);
16 
17 struct mcux_lpdac_config {
18 	LPDAC_Type *base;
19 	dac_reference_voltage_source_t ref_voltage;
20 	bool low_power;
21 };
22 
23 struct mcux_lpdac_data {
24 	bool configured;
25 };
26 
mcux_lpdac_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)27 static int mcux_lpdac_channel_setup(const struct device *dev,
28 				    const struct dac_channel_cfg *channel_cfg)
29 {
30 	const struct mcux_lpdac_config *config = dev->config;
31 	struct mcux_lpdac_data *data = dev->data;
32 	dac_config_t dac_config;
33 
34 	if (channel_cfg->channel_id != 0) {
35 		LOG_ERR("unsupported channel %d", channel_cfg->channel_id);
36 		return -ENOTSUP;
37 	}
38 
39 	if (channel_cfg->resolution != 12) {
40 		LOG_ERR("unsupported resolution %d", channel_cfg->resolution);
41 		return -ENOTSUP;
42 	}
43 
44 	if (channel_cfg->internal) {
45 		LOG_ERR("Internal channels not supported");
46 		return -ENOTSUP;
47 	}
48 
49 	DAC_GetDefaultConfig(&dac_config);
50 	dac_config.referenceVoltageSource = config->ref_voltage;
51 #if defined(FSL_FEATURE_LPDAC_HAS_GCR_BUF_SPD_CTRL) && FSL_FEATURE_LPDAC_HAS_GCR_BUF_SPD_CTRL
52 	dac_config.enableLowerLowPowerMode = config->low_power;
53 #else
54 	dac_config.enableLowPowerMode = config->low_power;
55 #endif
56 	DAC_Init(config->base, &dac_config);
57 	DAC_Enable(config->base, false);
58 	data->configured = true;
59 
60 	return 0;
61 }
62 
mcux_lpdac_write_value(const struct device * dev,uint8_t channel,uint32_t value)63 static int mcux_lpdac_write_value(const struct device *dev, uint8_t channel, uint32_t value)
64 {
65 	const struct mcux_lpdac_config *config = dev->config;
66 	struct mcux_lpdac_data *data = dev->data;
67 
68 	if (!data->configured) {
69 		LOG_ERR("channel not initialized");
70 		return -EINVAL;
71 	}
72 
73 	if (channel != 0) {
74 		LOG_ERR("unsupported channel %d", channel);
75 		return -ENOTSUP;
76 	}
77 
78 	if (value >= 4096) {
79 		LOG_ERR("unsupported value %d", value);
80 		return -EINVAL;
81 	}
82 
83 	DAC_Enable(config->base, true);
84 	DAC_SetData(config->base, value);
85 
86 	return 0;
87 }
88 
mcux_lpdac_init(const struct device * dev)89 static int mcux_lpdac_init(const struct device *dev)
90 {
91 	return 0;
92 }
93 
94 static DEVICE_API(dac, mcux_lpdac_driver_api) = {
95 	.channel_setup = mcux_lpdac_channel_setup,
96 	.write_value = mcux_lpdac_write_value,
97 };
98 
99 #define MCUX_LPDAC_INIT(n)                                                                         \
100 	static struct mcux_lpdac_data mcux_lpdac_data_##n;                                         \
101                                                                                                    \
102 	static const struct mcux_lpdac_config mcux_lpdac_config_##n = {                            \
103 		.base = (LPDAC_Type *)DT_INST_REG_ADDR(n),                                         \
104 		.ref_voltage = DT_INST_PROP(n, voltage_reference),                                 \
105 		.low_power = DT_INST_PROP(n, low_power_mode),                                      \
106 	};                                                                                         \
107                                                                                                    \
108 	DEVICE_DT_INST_DEFINE(n, mcux_lpdac_init, NULL, &mcux_lpdac_data_##n,                      \
109 			      &mcux_lpdac_config_##n, POST_KERNEL, CONFIG_DAC_INIT_PRIORITY,       \
110 			      &mcux_lpdac_driver_api);
111 
112 DT_INST_FOREACH_STATUS_OKAY(MCUX_LPDAC_INIT)
113