Skip to content

Commit 9dbbe3b

Browse files
silabs-BastienBcfriedt
authored andcommitted
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 e138061 commit 9dbbe3b

File tree

4 files changed

+175
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)