Skip to content

Commit 949222c

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 b87e340 commit 949222c

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-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_vdac"
73+
7274
endif # DAC

drivers/dac/Kconfig.silabs_vdac

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 SOC_SILABS_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: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
int err;
39+
40+
/* Configure pinctrl */
41+
err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
42+
if (err < 0 && err != -ENOENT) {
43+
LOG_ERR("failed to allocate silabs,analog-bus via pinctrl");
44+
return err;
45+
}
46+
47+
/* Enable VDAC Clock */
48+
err = clock_control_on(config->clock_dev, (clock_control_subsys_t)&config->clock_cfg);
49+
if (err < 0) {
50+
LOG_ERR("failed to enable clocks via clock_control");
51+
return err;
52+
}
53+
54+
/* Calculate clock prescaler */
55+
uint32_t freq;
56+
err = clock_control_get_rate(config->clock_dev, (clock_control_subsys_t)&config->clock_cfg, &freq);
57+
if (err < 0) {
58+
LOG_ERR("failed to get clock rate via clock_control");
59+
return err;
60+
}
61+
init.prescaler = sl_hal_vdac_calculate_prescaler(config->base, MAX_FREQUENCY, freq);
62+
63+
/* Initialize VDAC */
64+
sl_hal_vdac_init(config->base, &init);
65+
66+
return 0;
67+
}
68+
69+
static int vdac_channel_setup(const struct device *dev, const struct dac_channel_cfg *channel_cfg)
70+
{
71+
const struct vdac_config *config = dev->config;
72+
73+
if (channel_cfg->channel_id >= NUM_CHANNELS) {
74+
LOG_ERR("unsupported channel %d", channel_cfg->channel_id);
75+
return -ENOTSUP;
76+
}
77+
78+
if (channel_cfg->resolution != VDAC_RESOLUTION(VDAC_NUM(config->base))) {
79+
LOG_ERR("unsupported resolution %d", channel_cfg->resolution);
80+
return -ENOTSUP;
81+
}
82+
83+
if (channel_cfg->internal) {
84+
LOG_ERR("internal channels not supported");
85+
return -ENOTSUP;
86+
}
87+
88+
/* Configure channel */
89+
sl_hal_vdac_init_channel(config->base,
90+
&config->channel_init[channel_cfg->channel_id],
91+
channel_cfg->channel_id);
92+
93+
/* Start channel */
94+
sl_hal_vdac_enable_channel(config->base, channel_cfg->channel_id);
95+
96+
return 0;
97+
}
98+
99+
static int vdac_write_value(const struct device *dev, uint8_t channel, uint32_t value)
100+
{
101+
const struct vdac_config *config = dev->config;
102+
103+
if (channel >= NUM_CHANNELS) {
104+
LOG_ERR("unsupported channel %d", channel);
105+
return -ENOTSUP;
106+
}
107+
108+
/* Write value to VDAC channel */
109+
sl_hal_vdac_set_output_channel(config->base, channel, value);
110+
111+
return 0;
112+
}
113+
114+
static DEVICE_API(dac, vdac_api) = {
115+
.channel_setup = vdac_channel_setup,
116+
.write_value = vdac_write_value,
117+
};
118+
119+
#define VDAC_CHANNEL(inst) \
120+
.channel_init[DT_NODE_CHILD_IDX(inst)] = { \
121+
.main_out_enable = DT_PROP(inst, main_output), \
122+
.aux_out_enable = DT_PROP(inst, aux_output), \
123+
.short_output = DT_PROP(inst, short_output), \
124+
.power_mode = DT_PROP(inst, low_power_mode), \
125+
.high_cap_load_enable = DT_PROP(inst, high_capacitance_load), \
126+
.port = DT_ENUM_IDX(inst, aux_output_port), \
127+
.pin = DT_PROP(inst, aux_output_pin), \
128+
.sample_off_mode = DT_PROP(inst, sample_off_mode), \
129+
.hold_out_time = DT_PROP(inst, output_hold_cycles), \
130+
.ch_refresh_source = DT_PROP(inst, refresh_timer), \
131+
.trigger_mode = SL_HAL_VDAC_TRIGGER_MODE_SW, \
132+
},
133+
134+
#define VDAC_DEVICE(inst) \
135+
\
136+
PINCTRL_DT_INST_DEFINE(inst); \
137+
\
138+
static const struct vdac_config vdac_config_##inst = { \
139+
.base = (VDAC_TypeDef *)DT_INST_REG_ADDR(inst), \
140+
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
141+
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)), \
142+
.clock_cfg = SILABS_DT_INST_CLOCK_CFG(inst), \
143+
.init = SL_HAL_VDAC_INIT_DEFAULT, \
144+
.init.reference = DT_INST_ENUM_IDX(inst, voltage_reference), \
145+
.init.warmup_time = DT_INST_PROP(inst, warmup_cycles), \
146+
.init.refresh = DT_INST_ENUM_IDX(inst, refresh_period_cycles), \
147+
DT_INST_FOREACH_CHILD(inst, VDAC_CHANNEL) \
148+
}; \
149+
\
150+
DEVICE_DT_INST_DEFINE(inst, &vdac_init, PM_DEVICE_DT_INST_GET(inst), \
151+
NULL, &vdac_config_##inst, POST_KERNEL, \
152+
CONFIG_DAC_INIT_PRIORITY, &vdac_api);
153+
154+
DT_INST_FOREACH_STATUS_OKAY(VDAC_DEVICE)

0 commit comments

Comments
 (0)