Skip to content

Commit 9c67002

Browse files
khoa-nguyen-18fabiobaltieri
authored andcommitted
drivers: dac: Initial DAC driver support for Renesas RA
Initial DAC driver support for Renesas RA Signed-off-by: Khoa Nguyen <khoa.nguyen.xh@renesas.com>
1 parent 913012a commit 9c67002

File tree

8 files changed

+260
-3
lines changed

8 files changed

+260
-3
lines changed

drivers/dac/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ zephyr_library_sources_ifdef(CONFIG_USERSPACE dac_handlers.c)
2626
zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_GAU dac_mcux_gau.c)
2727
zephyr_library_sources_ifdef(CONFIG_DAC_TEST dac_test.c)
2828
zephyr_library_sources_ifdef(CONFIG_DAC_MAX22017 dac_max22017.c)
29+
zephyr_library_sources_ifdef(CONFIG_DAC_RENESAS_RA dac_renesas_ra.c)

drivers/dac/Kconfig

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

6464
source "drivers/dac/Kconfig.max22017"
6565

66+
source "drivers/dac/Kconfig.renesas_ra"
67+
6668
endif # DAC

drivers/dac/Kconfig.renesas_ra

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config DAC_RENESAS_RA
5+
bool "Renesas RA DAC"
6+
default y
7+
depends on DT_HAS_RENESAS_RA_DAC_ENABLED
8+
select USE_RA_FSP_DAC
9+
select PINCTRL
10+
help
11+
Enable Renesas RA DAC Driver.
12+
13+
if DAC_RENESAS_RA
14+
config DAC_RENESAS_RA_DA_AD_SYNCHRONIZE
15+
bool "D/A A/D Synchronous Conversion"
16+
help
17+
- n: Do not synchronize DAC12 with ADC (unit 1) operation
18+
(disable interference reduction between D/A and A/D conversion).
19+
- y: Synchronize DAC12 with ADC (unit 1) operation
20+
(enable interference reduction between D/A and A/D conversion).
21+
22+
choice DAC_RENESAS_RA_DAVREFCR
23+
prompt "D/A Reference Voltage"
24+
depends on $(dt_nodelabel_bool_prop,dac_global,has-davrefcr)
25+
default DAC_RENESAS_RA_DAVREFCR_AVCC0_AVSS0
26+
27+
config DAC_RENESAS_RA_DAVREFCR_NONE
28+
bool "No reference voltage selected"
29+
help
30+
No reference voltage selected
31+
32+
config DAC_RENESAS_RA_DAVREFCR_AVCC0_AVSS0
33+
bool "Using AVCC0/AVSS0 for reference voltage source"
34+
help
35+
AVCC0/AVSS0 selected
36+
37+
config DAC_RENESAS_RA_DAVREFCR_VREFH_VREFL
38+
bool "Using VREFH/VREFL for reference voltage source"
39+
help
40+
VREFH/VREFL selected
41+
42+
endchoice #DAC_RENESAS_RA_DAVREFCR
43+
endif # DAC_RENESAS_RA

drivers/dac/dac_renesas_ra.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT renesas_ra_dac
8+
9+
#include <zephyr/drivers/dac.h>
10+
#include <zephyr/drivers/pinctrl.h>
11+
#include <zephyr/logging/log.h>
12+
#include "r_dac_api.h"
13+
#include "r_dac.h"
14+
15+
LOG_MODULE_REGISTER(dac_renesas_ra, CONFIG_DAC_LOG_LEVEL);
16+
17+
struct dac_renesas_ra_config {
18+
const struct pinctrl_dev_config *pcfg;
19+
};
20+
21+
struct dac_renesas_ra_data {
22+
const struct device *dev;
23+
dac_instance_ctrl_t dac;
24+
struct st_dac_cfg f_config;
25+
};
26+
27+
static int dac_renesas_ra_write_value(const struct device *dev, uint8_t channel, uint32_t value)
28+
{
29+
struct dac_renesas_ra_data *data = dev->data;
30+
fsp_err_t fsp_err;
31+
32+
if (channel != 0) {
33+
LOG_ERR("wrong channel id '%hhu'", channel);
34+
return -ENOTSUP;
35+
}
36+
37+
fsp_err = R_DAC_Write(&data->dac, value);
38+
if (FSP_SUCCESS != fsp_err) {
39+
return -EIO;
40+
}
41+
42+
return 0;
43+
}
44+
45+
static int dac_renesas_ra_channel_setup(const struct device *dev,
46+
const struct dac_channel_cfg *channel_cfg)
47+
{
48+
struct dac_renesas_ra_data *data = dev->data;
49+
dac_extended_cfg_t *config_extend = (dac_extended_cfg_t *)data->f_config.p_extend;
50+
fsp_err_t fsp_err;
51+
52+
if (channel_cfg->channel_id != 0) {
53+
LOG_ERR("wrong channel id '%hhu'", channel_cfg->channel_id);
54+
return -ENOTSUP;
55+
}
56+
57+
if (channel_cfg->resolution != 12) {
58+
LOG_ERR("Resolution not supported");
59+
return -ENOTSUP;
60+
}
61+
62+
if (data->dac.channel_opened != 0) {
63+
fsp_err = R_DAC_Close(&data->dac);
64+
if (FSP_SUCCESS != fsp_err) {
65+
return -EIO;
66+
}
67+
}
68+
69+
#if DT_PROP(DT_PARENT(DT_DRV_INST(0)), has_output_amplifier)
70+
config_extend->output_amplifier_enabled = channel_cfg->buffered;
71+
#elif DT_PROP(DT_PARENT(DT_DRV_INST(0)), has_chargepump)
72+
#if DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(moco))
73+
config_extend->enable_charge_pump = channel_cfg->buffered;
74+
#else
75+
if (channel_cfg->buffered) {
76+
LOG_ERR("Requires MOCO clock enabled to support the buffer feature");
77+
return -ENOTSUP;
78+
}
79+
#endif
80+
#else
81+
if (channel_cfg->buffered) {
82+
LOG_ERR("The MCU doesn't support the buffer feature");
83+
return -ENOTSUP;
84+
}
85+
#endif
86+
87+
#if DT_PROP(DT_PARENT(DT_DRV_INST(0)), has_internal_output)
88+
config_extend->internal_output_enabled = channel_cfg->internal;
89+
#else
90+
if (channel_cfg->internal) {
91+
LOG_ERR("The MCU doesn't support the internal output feature");
92+
return -ENOTSUP;
93+
}
94+
#endif
95+
96+
fsp_err = R_DAC_Open(&data->dac, &data->f_config);
97+
if (FSP_SUCCESS != fsp_err) {
98+
return -EIO;
99+
}
100+
101+
fsp_err = R_DAC_Start(&data->dac);
102+
if (FSP_SUCCESS != fsp_err) {
103+
return -EIO;
104+
}
105+
106+
return 0;
107+
}
108+
109+
static int dac_renesas_ra_init(const struct device *dev)
110+
{
111+
const struct dac_renesas_ra_config *config = dev->config;
112+
int ret;
113+
114+
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
115+
if (ret < 0) {
116+
return ret;
117+
}
118+
119+
return 0;
120+
}
121+
122+
static DEVICE_API(dac, dac_renesas_ra_api) = {
123+
.channel_setup = dac_renesas_ra_channel_setup,
124+
.write_value = dac_renesas_ra_write_value,
125+
};
126+
127+
#ifdef CONFIG_DAC_RENESAS_RA_DAVREFCR_AVCC0_AVSS0
128+
#define DAC_RENESAS_RA_DAVREFCR DAC_VREF_AVCC0_AVSS0
129+
#elif defined(CONFIG_DAC_RENESAS_RA_DAVREFCR_VREFH_VREFL)
130+
#define DAC_RENESAS_RA_DAVREFCR DAC_VREF_VREFH_VREFL
131+
#elif defined(CONFIG_DAC_RENESAS_RA_DAVREFCR_NONE)
132+
#define DAC_RENESAS_RA_DAVREFCR DAC_VREF_NONE
133+
#else
134+
#define DAC_RENESAS_RA_DAVREFCR 0
135+
#endif
136+
137+
#define DAC_RENESAS_RA_INIT(idx) \
138+
PINCTRL_DT_INST_DEFINE(idx); \
139+
static dac_extended_cfg_t g_dac_cfg_extend_##idx = { \
140+
.data_format = DAC_DATA_FORMAT_FLUSH_RIGHT, \
141+
.enable_charge_pump = true, \
142+
.output_amplifier_enabled = true, \
143+
.internal_output_enabled = false, \
144+
.ref_volt_sel = DAC_RENESAS_RA_DAVREFCR, \
145+
}; \
146+
static const struct dac_renesas_ra_config dac_renesas_ra_config_##idx = { \
147+
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
148+
}; \
149+
static struct dac_renesas_ra_data dac_renesas_ra_data_##idx = { \
150+
.dev = DEVICE_DT_INST_GET(idx), \
151+
.f_config = \
152+
{ \
153+
.channel = DT_INST_REG_ADDR(idx), \
154+
.ad_da_synchronized = \
155+
IS_ENABLED(CONFIG_DAC_RENESAS_RA_DA_AD_SYNCHRONIZE), \
156+
.p_extend = &g_dac_cfg_extend_##idx, \
157+
}, \
158+
}; \
159+
\
160+
DEVICE_DT_INST_DEFINE(idx, dac_renesas_ra_init, NULL, &dac_renesas_ra_data_##idx, \
161+
&dac_renesas_ra_config_##idx, POST_KERNEL, CONFIG_DAC_INIT_PRIORITY, \
162+
&dac_renesas_ra_api)
163+
164+
DT_INST_FOREACH_STATUS_OKAY(DAC_RENESAS_RA_INIT);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Renesas RA DAC Controller Global
5+
6+
compatible: "renesas,ra-dac-global"
7+
8+
include: [base.yaml]
9+
10+
properties:
11+
has-internal-output:
12+
type: boolean
13+
description: True if the SoC supports the internal output feature in the DAC HWIP
14+
15+
has-output-amplifier:
16+
type: boolean
17+
description: True if the SoC supports the output amplifier feature in the DAC HWIP
18+
19+
has-chargepump:
20+
type: boolean
21+
description: True if the SoC supports the chargepump feature in the DAC HWIP
22+
23+
has-davrefcr:
24+
type: boolean
25+
description: True if the SoC supports the D/A VREF configuration in the DAC HWIP

dts/bindings/dac/renesas,ra-dac.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Renesas RA DAC Controller
5+
6+
compatible: "renesas,ra-dac"
7+
8+
include: [dac-controller.yaml, pinctrl-device.yaml]
9+
10+
properties:
11+
"#io-channel-cells":
12+
const: 1
13+
14+
io-channel-cells:
15+
- output

include/zephyr/dt-bindings/pinctrl/renesas/pinctrl-ra.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Renesas Electronics Corporation
2+
* Copyright (c) 2024-2025 Renesas Electronics Corporation
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -14,6 +14,8 @@
1414
#define RA_PIN_NUM_MASK 0xf
1515

1616
#define RA_PSEL_HIZ_JTAG_SWD 0x0
17+
#define RA_PSEL_ADC 0x0
18+
#define RA_PSEL_DAC 0x0
1719
#define RA_PSEL_AGT 0x1
1820
#define RA_PSEL_GPT0 0x2
1921
#define RA_PSEL_GPT1 0x3
@@ -31,6 +33,7 @@
3133
#define RA_PSEL_I2C 0x7
3234
#define RA_PSEL_CLKOUT_RTC 0x9
3335
#define RA_PSEL_CAC_ADC 0xa
36+
#define RA_PSEL_CAC_DAC 0xa
3437
#define RA_PSEL_BUS 0xb
3538
#define RA_PSEL_CANFD 0x10
3639
#define RA_PSEL_QSPI 0x11
@@ -42,7 +45,6 @@
4245
#define RA_PSEL_ETH_RMII 0x17
4346
#define RA_PSEL_GLCDC 0x19
4447
#define RA_PSEL_OSPI 0x1c
45-
#define RA_PSEL_ADC 0x00
4648

4749
#define RA_PSEL_POS 8
4850
#define RA_PSEL_MASK 0x1f

modules/Kconfig.renesas_fsp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Renesas FSP HAL config
22

3-
# Copyright (c) 2024 Renesas Electronics Corporation
3+
# Copyright (c) 2024-2025 Renesas Electronics Corporation
44
# SPDX-License-Identifier: Apache-2.0
55

66
config HAS_RENESAS_RA_FSP
@@ -149,6 +149,11 @@ config USE_RA_FSP_SDHI
149149
help
150150
Enable RA FSP SDHI driver
151151

152+
config USE_RA_FSP_DAC
153+
bool
154+
help
155+
Enable RA FSP DAC driver
156+
152157
endif # HAS_RENESAS_RA_FSP
153158

154159
if HAS_RENESAS_RZ_FSP

0 commit comments

Comments
 (0)