-
Notifications
You must be signed in to change notification settings - Fork 8.3k
drivers: dac: Add drivers for TI DAC family X311 on SPI bus #90811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Snap-A
wants to merge
1
commit into
zephyrproject-rtos:main
Choose a base branch
from
Snap-A:add_ti_x311_driver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # DAC configuration options | ||
| # | ||
| # Copyright (c) 2025 Andreas Wolf <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| config DAC_TX311 | ||
| bool "TI DACx311 DAC driver" | ||
| default y | ||
| depends on DT_HAS_TI_DAC8311_ENABLED || DT_HAS_TI_DAC7311_ENABLED || \ | ||
| DT_HAS_TI_DAC6311_ENABLED || DT_HAS_TI_DAC5311_ENABLED | ||
| select SPI | ||
| help | ||
| Enable the driver for TI DACx311 chip. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| /* | ||
| * Copyright (c) 2025 Andreas Wolf <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * @file dac_tx311.c | ||
| * @brief Driver for the TI x311 single channel DAC chips. | ||
| * | ||
| * This driver supports multiple variants of the Texas Instrument DAC chip. | ||
| * | ||
| * DAC5311 : 8-bit resolution | ||
| * DAC6311 : 10-bit resolution | ||
| * DAC7311 : 12-bit resolution | ||
| * DAC8311 : 14-bit resolution | ||
| * | ||
| */ | ||
|
|
||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/drivers/spi.h> | ||
| #include <zephyr/drivers/dac.h> | ||
| #include <zephyr/sys/byteorder.h> | ||
| #include <zephyr/logging/log.h> | ||
|
|
||
| LOG_MODULE_REGISTER(dac_tx311, CONFIG_DAC_LOG_LEVEL); | ||
|
|
||
| #define DACX311_MIN_RESOLUTION 8U | ||
| #define DACX311_MAX_RESOLUTION 14U | ||
|
|
||
| #define DAC8311_RESOLUTION 14U | ||
| #define DAC7311_RESOLUTION 12U | ||
| #define DAC6311_RESOLUTION 10U | ||
| #define DAC5311_RESOLUTION 8U | ||
|
|
||
| #define DACX311_MAX_CHANNEL 1U | ||
|
|
||
| #define DACX311_SPI_HZ_MAX (5*1000*1000) | ||
|
|
||
| struct dacx311_config { | ||
| struct spi_dt_spec bus; | ||
| uint8_t resolution; | ||
| uint8_t power_down_mode; | ||
| }; | ||
|
|
||
| struct dacx311_data { | ||
| uint8_t resolution; | ||
| uint16_t power_down_mode; | ||
| uint8_t configured; | ||
| }; | ||
|
|
||
| static int dacx311_reg_write(const struct device *dev, uint16_t val) | ||
| { | ||
| const struct dacx311_config *cfg = dev->config; | ||
| uint8_t tx_bytes[2]; | ||
|
|
||
| /* Construct write buffer for SPI API */ | ||
| const struct spi_buf tx_buf[1] = { | ||
| { | ||
| .buf = tx_bytes, | ||
| .len = sizeof(tx_bytes) | ||
| } | ||
| }; | ||
| const struct spi_buf_set tx = { | ||
| .buffers = tx_buf, | ||
| .count = ARRAY_SIZE(tx_buf) | ||
| }; | ||
|
|
||
| /* Set register bits */ | ||
| tx_bytes[0] = val >> 8; | ||
| tx_bytes[1] = val & 0xFF; | ||
|
|
||
| /* Write to bus */ | ||
| return spi_write_dt(&cfg->bus, &tx); | ||
| } | ||
|
|
||
| static int dacx311_channel_setup(const struct device *dev, | ||
| const struct dac_channel_cfg *channel_cfg) | ||
| { | ||
| struct dacx311_data *data = dev->data; | ||
|
|
||
| /* Validate configuration */ | ||
| if (channel_cfg->channel_id > (DACX311_MAX_CHANNEL - 1)) { | ||
| LOG_ERR("Unsupported channel %d", channel_cfg->channel_id); | ||
| return -ENOTSUP; | ||
| } | ||
|
|
||
| if (channel_cfg->internal) { | ||
| LOG_ERR("Internal channels not supported"); | ||
| return -ENOTSUP; | ||
| } | ||
|
|
||
| if (data->configured & BIT(channel_cfg->channel_id)) { | ||
| LOG_DBG("Channel %d already configured", channel_cfg->channel_id); | ||
| return 0; | ||
| } | ||
|
|
||
| /* Mark channel as configured */ | ||
| data->configured |= BIT(channel_cfg->channel_id); | ||
|
|
||
| LOG_DBG("Channel %d initialized", channel_cfg->channel_id); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int dacx311_write_value(const struct device *dev, uint8_t channel, | ||
| uint32_t value) | ||
| { | ||
| struct dacx311_data *data = dev->data; | ||
| uint16_t regval; | ||
| uint8_t shift; | ||
| int ret; | ||
|
|
||
| const bool brdcast = (channel == DAC_CHANNEL_BROADCAST) ? 1 : 0; | ||
|
|
||
| if (!brdcast && (channel > (DACX311_MAX_CHANNEL - 1))) { | ||
| LOG_ERR("Unsupported channel %d", channel); | ||
| return -ENOTSUP; | ||
| } | ||
|
|
||
| /* | ||
| * Check if channel is initialized | ||
| * If broadcast channel is used, check if any channel is initialized | ||
| */ | ||
| if ((brdcast && !data->configured) || | ||
| (channel < DACX311_MAX_CHANNEL && !(data->configured & BIT(channel)))) { | ||
| LOG_ERR("Channel %d not initialized", channel); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| if (value >= (1 << (data->resolution))) { | ||
| LOG_ERR("Value %d out of range", value); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| /* | ||
| * (See https://www.ti.com/document-viewer/dac6311/datasheet) | ||
| * | ||
| * Shift given value to align MSB bit position to register bit 13. | ||
| * | ||
| * DAC output register format: | ||
| * | ||
| * | 15 14 | 13 12 11 10 9 8 7 6 5 4 3 2 1 0 | | ||
| * |-------|---------------------------------------------------| | ||
| * | Mode | 8311[13:0] / 7311[13:2] / 6311[13:4] / 5311[13:6] | | ||
| */ | ||
| shift = DACX311_MAX_RESOLUTION - data->resolution; | ||
| regval = value << shift; | ||
|
|
||
| /* | ||
| * Set mode bits to value taken from configuration. | ||
| * | ||
| * MODE = 0 0 -> Normal Operation | ||
| * 0 1 -> Output 1 kΩ to GND | ||
| * 1 0 -> Output 100 kΩ to GND | ||
| * 1 1 -> High-Z | ||
| */ | ||
| regval &= 0x3FFF; | ||
| regval |= data->power_down_mode; | ||
|
|
||
| /* Write to output */ | ||
| ret = dacx311_reg_write(dev, regval); | ||
| if (ret) { | ||
| LOG_ERR("Unable to set value %d on channel %d", value, channel); | ||
| return -EIO; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int dacx311_init(const struct device *dev) | ||
| { | ||
| const struct dacx311_config *config = dev->config; | ||
| struct dacx311_data *data = dev->data; | ||
|
|
||
| /* Set bit resolution for this chip variant */ | ||
| data->resolution = config->resolution; | ||
|
|
||
| /* Set the power mode (shifted to upper bits) */ | ||
| data->power_down_mode = FIELD_PREP(BIT_MASK(2) << DACX311_MAX_RESOLUTION, | ||
| config->power_down_mode); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static DEVICE_API(dac, dacx311_driver_api) = { | ||
| .channel_setup = dacx311_channel_setup, | ||
| .write_value = dacx311_write_value | ||
| }; | ||
|
|
||
| #define INST_DT_DACX311(inst, t) DT_INST(inst, ti_dac##t) | ||
|
|
||
| #define DACX311_DEVICE(t, n, res) \ | ||
| BUILD_ASSERT(DT_INST_ENUM_IDX(n, power_down_mode) <= 3, \ | ||
| "Invalid power down mode");\ | ||
| BUILD_ASSERT(DT_PROP(INST_DT_DACX311(n, t), spi_max_frequency) \ | ||
| <= DACX311_SPI_HZ_MAX, "Invalid SPI frequency");\ | ||
| static struct dacx311_data dac##t##_data_##n; \ | ||
| static const struct dacx311_config dac##t##_config_##n = { \ | ||
| .bus = SPI_DT_SPEC_GET(INST_DT_DACX311(n, t), \ | ||
| SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | \ | ||
| SPI_MODE_CPHA | \ | ||
| SPI_WORD_SET(16), 0), \ | ||
| .resolution = res, \ | ||
| .power_down_mode = DT_INST_ENUM_IDX(n, power_down_mode), \ | ||
| }; \ | ||
| DEVICE_DT_DEFINE(INST_DT_DACX311(n, t), \ | ||
| dacx311_init, NULL, \ | ||
| &dac##t##_data_##n, \ | ||
| &dac##t##_config_##n, POST_KERNEL, \ | ||
| CONFIG_DAC_INIT_PRIORITY, \ | ||
| &dacx311_driver_api) | ||
|
|
||
| /* | ||
| * DAC8311: 14-bit | ||
| */ | ||
| #define DAC8311_DEVICE(n) DACX311_DEVICE(8311, n, DAC8311_RESOLUTION) | ||
| /* | ||
| * DAC7311: 12-bit | ||
| */ | ||
| #define DAC7311_DEVICE(n) DACX311_DEVICE(7311, n, DAC7311_RESOLUTION) | ||
| /* | ||
| * DAC6311: 10-bit | ||
| */ | ||
| #define DAC6311_DEVICE(n) DACX311_DEVICE(6311, n, DAC6311_RESOLUTION) | ||
| /* | ||
| * DAC5311: 8-bit | ||
| */ | ||
| #define DAC5311_DEVICE(n) DACX311_DEVICE(5311, n, DAC5311_RESOLUTION) | ||
|
|
||
| #define CALL_WITH_ARG(arg, expr) expr(arg) | ||
|
|
||
| #define INST_DT_DACX311_FOREACH(t, inst_expr) \ | ||
| LISTIFY(DT_NUM_INST_STATUS_OKAY(ti_dac##t), \ | ||
| CALL_WITH_ARG, (), inst_expr) | ||
|
|
||
| #define DT_DRV_COMPAT ti_dac8311 | ||
| INST_DT_DACX311_FOREACH(8311, DAC8311_DEVICE); | ||
| #undef DT_DRV_COMPAT | ||
| #define DT_DRV_COMPAT ti_dac7311 | ||
| INST_DT_DACX311_FOREACH(7311, DAC7311_DEVICE); | ||
| #undef DT_DRV_COMPAT | ||
| #define DT_DRV_COMPAT ti_dac6311 | ||
| INST_DT_DACX311_FOREACH(6311, DAC6311_DEVICE); | ||
| #undef DT_DRV_COMPAT | ||
| #define DT_DRV_COMPAT ti_dac5311 | ||
| INST_DT_DACX311_FOREACH(5311, DAC5311_DEVICE); | ||
| #undef DT_DRV_COMPAT | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright (c) 2025 Andreas Wolf <[email protected]> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: Texas Instrument 8-Bit DAC 5311 | ||
|
|
||
| compatible: "ti,dac5311" | ||
|
|
||
| include: ti,dacx311-base.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright (c) 2025 Andreas Wolf <[email protected]> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: Texas Instrument 10-Bit DAC 6311 | ||
|
|
||
| compatible: "ti,dac6311" | ||
|
|
||
| include: ti,dacx311-base.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright (c) 2025 Andreas Wolf <[email protected]> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: Texas Instrument 12-Bit DAC 7311 | ||
|
|
||
| compatible: "ti,dac7311" | ||
|
|
||
| include: ti,dacx311-base.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright (c) 2025 Andreas Wolf <[email protected]> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: Texas Instrument 14-Bit DAC 8311 | ||
|
|
||
| compatible: "ti,dac8311" | ||
|
|
||
| include: ti,dacx311-base.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Copyright (c) 2025 Andreas Wolf <[email protected]> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| include: [dac-controller.yaml, spi-device.yaml] | ||
|
|
||
| properties: | ||
| "#io-channel-cells": | ||
| const: 1 | ||
|
|
||
| power-down-mode: | ||
| type: string | ||
| default: "normal" | ||
| enum: | ||
| - "normal" | ||
| - "power-down-1k" | ||
| - "power-down-100k" | ||
| - "power-down-3-state" | ||
| description: | | ||
| Power-down mode select. | ||
| - Normal mode (reg: 0). | ||
| - 1 kOhm output impedance (reg: 1). | ||
| - 100 kOhm output impedance (reg: 2). | ||
| - Three-state output impedance (reg: 3). | ||
| The default corresponds to the reset value of the register field. | ||
|
|
||
| io-channel-cells: | ||
| - output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is actually the max resolution supported by the chip, but we can configure it for lower resolution, right? Should we then name it
max_resolutionto make the meaning more clear?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each chip variant has a fixed resolution. The entry in this struct is present in order to use the same code logic for the different chip variants. This value is different for each variant.