Skip to content

Commit 9860d34

Browse files
drivers: dac: implement the silabs_vdac compatible driver
This implements the DAC driver for silabs VDAC peripherals using the silabs,vdac compatible binding. Signed-off-by: Bastien Beauchamp <[email protected]>
1 parent 4c04964 commit 9860d34

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

drivers/dac/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_GAU dac_mcux_gau.c)
3030
zephyr_library_sources_ifdef(CONFIG_DAC_TEST dac_test.c)
3131
zephyr_library_sources_ifdef(CONFIG_DAC_MAX22017 dac_max22017.c)
3232
zephyr_library_sources_ifdef(CONFIG_DAC_RENESAS_RA dac_renesas_ra.c)
33+
zephyr_library_sources_ifdef(CONFIG_DAC_SILABS_VDAC dac_silabs_vdac.c)

drivers/dac/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ source "drivers/dac/Kconfig.renesas_ra"
6969

7070
source "drivers/dac/Kconfig.samd5x"
7171

72+
source "drivers/dac/Kconfig.silabs"
73+
7274
endif # DAC

drivers/dac/Kconfig.silabs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Silicon Laboratories Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config DAC_SILABS_VDAC
5+
bool "Silabs DAC driver for VDAC"
6+
default y
7+
depends on DT_HAS_SILABS_VDAC_ENABLED
8+
select PINCTRL
9+
select SILABS_SISDK_VDAC
10+
help
11+
Enable the Digital-to-Analog Converter driver for the
12+
VDAC hardware block present on Silicon Labs devices.

drivers/dac/dac_silabs_vdac.c

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright (c) 2025 Silicon Laboratories Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <em_device.h>
8+
#include <sl_hal_vdac.h>
9+
#include <zephyr/device.h>
10+
#include <zephyr/drivers/dac.h>
11+
#include <zephyr/drivers/clock_control.h>
12+
#include <zephyr/drivers/clock_control/clock_control_silabs.h>
13+
#include <zephyr/drivers/pinctrl.h>
14+
#include <zephyr/irq.h>
15+
16+
#include <zephyr/logging/log.h>
17+
LOG_MODULE_REGISTER(silabs_vdac, CONFIG_DAC_LOG_LEVEL);
18+
19+
#define DT_DRV_COMPAT silabs_vdac
20+
21+
#define NUM_CHANNELS 2
22+
#define MAX_FREQUENCY 1000000
23+
24+
/* Read-only driver configuration */
25+
struct vdac_config {
26+
VDAC_TypeDef *base;
27+
const struct pinctrl_dev_config *pincfg;
28+
const struct device *clock_dev;
29+
const struct silabs_clock_control_cmu_config clock_cfg;
30+
sl_hal_vdac_init_t init;
31+
sl_hal_vdac_init_channel_t channel_init[NUM_CHANNELS];
32+
};
33+
34+
static int vdac_init(const struct device *dev)
35+
{
36+
const struct vdac_config *config = dev->config;
37+
sl_hal_vdac_init_t init = config->init;
38+
uint32_t freq;
39+
int err;
40+
41+
/* Configure pinctrl */
42+
err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
43+
if (err < 0 && err != -ENOENT) {
44+
LOG_ERR("failed to allocate silabs,analog-bus via pinctrl");
45+
return err;
46+
}
47+
48+
/* Enable VDAC Clock */
49+
err = clock_control_on(config->clock_dev,
50+
(clock_control_subsys_t)&config->clock_cfg);
51+
if (err < 0) {
52+
LOG_ERR("failed to enable clocks via clock_control");
53+
return err;
54+
}
55+
56+
/* Calculate clock prescaler */
57+
err = clock_control_get_rate(config->clock_dev,
58+
(clock_control_subsys_t)&config->clock_cfg, &freq);
59+
if (err < 0) {
60+
LOG_ERR("failed to get clock rate via clock_control");
61+
return err;
62+
}
63+
init.prescaler = sl_hal_vdac_calculate_prescaler(config->base, MAX_FREQUENCY, freq);
64+
65+
/* Initialize VDAC */
66+
sl_hal_vdac_init(config->base, &init);
67+
68+
return 0;
69+
}
70+
71+
static int vdac_channel_setup(const struct device *dev, const struct dac_channel_cfg *channel_cfg)
72+
{
73+
const struct vdac_config *config = dev->config;
74+
75+
if (channel_cfg->channel_id >= NUM_CHANNELS) {
76+
LOG_ERR("unsupported channel %d", channel_cfg->channel_id);
77+
return -ENOTSUP;
78+
}
79+
80+
if (channel_cfg->resolution != VDAC_RESOLUTION(VDAC_NUM(config->base))) {
81+
LOG_ERR("unsupported resolution %d", channel_cfg->resolution);
82+
return -ENOTSUP;
83+
}
84+
85+
if (channel_cfg->internal) {
86+
LOG_ERR("internal channels not supported");
87+
return -ENOTSUP;
88+
}
89+
90+
/* Configure channel */
91+
sl_hal_vdac_init_channel(config->base,
92+
&config->channel_init[channel_cfg->channel_id],
93+
channel_cfg->channel_id);
94+
95+
/* Start channel */
96+
sl_hal_vdac_enable_channel(config->base, channel_cfg->channel_id);
97+
98+
return 0;
99+
}
100+
101+
static int vdac_write_value(const struct device *dev, uint8_t channel, uint32_t value)
102+
{
103+
const struct vdac_config *config = dev->config;
104+
105+
if (channel >= NUM_CHANNELS) {
106+
LOG_ERR("unsupported channel %d", channel);
107+
return -ENOTSUP;
108+
}
109+
110+
/* Write value to VDAC channel */
111+
sl_hal_vdac_set_output_channel(config->base, channel, value);
112+
113+
return 0;
114+
}
115+
116+
static DEVICE_API(dac, vdac_api) = {
117+
.channel_setup = vdac_channel_setup,
118+
.write_value = vdac_write_value,
119+
};
120+
121+
#define VDAC_CHANNEL(inst) \
122+
.channel_init[DT_REG_ADDR(inst)] = { \
123+
.main_out_enable = DT_PROP(inst, main_output), \
124+
.aux_out_enable = DT_NODE_HAS_PROP(inst, aux_output), \
125+
.short_output = DT_PROP(inst, short_output), \
126+
.power_mode = DT_PROP(inst, low_power_mode), \
127+
.high_cap_load_enable = DT_PROP(inst, high_capacitance_load), \
128+
.port = DT_PROP(inst, aux_output) >> 4, \
129+
.pin = DT_PROP(inst, aux_output) & 0xF, \
130+
.sample_off_mode = DT_PROP(inst, sample_off_mode), \
131+
.hold_out_time = DT_PROP(inst, output_hold_cycles), \
132+
.ch_refresh_source = DT_PROP(inst, refresh_timer), \
133+
.trigger_mode = SL_HAL_VDAC_TRIGGER_MODE_SW, \
134+
},
135+
136+
#define VDAC_DEVICE(inst) \
137+
\
138+
PINCTRL_DT_INST_DEFINE(inst); \
139+
\
140+
static const struct vdac_config vdac_config_##inst = { \
141+
.base = (VDAC_TypeDef *)DT_INST_REG_ADDR(inst), \
142+
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
143+
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)), \
144+
.clock_cfg = SILABS_DT_INST_CLOCK_CFG(inst), \
145+
.init = SL_HAL_VDAC_INIT_DEFAULT, \
146+
.init.reference = DT_INST_ENUM_IDX(inst, voltage_reference), \
147+
.init.warmup_time = DT_INST_PROP(inst, warmup_cycles), \
148+
.init.refresh = DT_INST_ENUM_IDX(inst, refresh_period_cycles), \
149+
DT_INST_FOREACH_CHILD(inst, VDAC_CHANNEL) \
150+
}; \
151+
\
152+
DEVICE_DT_INST_DEFINE(inst, &vdac_init, PM_DEVICE_DT_INST_GET(inst), \
153+
NULL, &vdac_config_##inst, POST_KERNEL, \
154+
CONFIG_DAC_INIT_PRIORITY, &vdac_api);
155+
156+
DT_INST_FOREACH_STATUS_OKAY(VDAC_DEVICE)

0 commit comments

Comments
 (0)