Skip to content

Commit

Permalink
drivers: dac: add driver for AD5592
Browse files Browse the repository at this point in the history
Add MFD subdriver for the built-in DAC controller
in AD5592 chip.

Signed-off-by: Bartosz Bilas <[email protected]>
  • Loading branch information
bbilas committed Nov 7, 2023
1 parent bc37e15 commit 6dd963e
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
/drivers/display/display_ili9342c.* @extremegtx
/drivers/dac/ @martinjaeger
/drivers/dac/*ad56xx* @benediktibk
/drivers/dac/dac_ad5592.c @bbilas
/drivers/dai/ @kv2019i @marcinszkudlinski @abonislawski
/drivers/dai/intel/ @kv2019i @marcinszkudlinski @abonislawski
/drivers/dai/intel/ssp/ @kv2019i @marcinszkudlinski @abonislawski
Expand Down
1 change: 1 addition & 0 deletions drivers/dac/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ zephyr_library_sources_ifdef(CONFIG_DAC_MCP4725 dac_mcp4725.c)
zephyr_library_sources_ifdef(CONFIG_DAC_MCP4728 dac_mcp4728.c)
zephyr_library_sources_ifdef(CONFIG_DAC_GD32 dac_gd32.c)
zephyr_library_sources_ifdef(CONFIG_DAC_ESP32 dac_esp32.c)
zephyr_library_sources_ifdef(CONFIG_DAC_AD5592 dac_ad5592.c)
zephyr_library_sources_ifdef(CONFIG_DAC_AD56XX dac_ad56xx.c)
zephyr_library_sources_ifdef(CONFIG_USERSPACE dac_handlers.c)
2 changes: 2 additions & 0 deletions drivers/dac/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ source "drivers/dac/Kconfig.esp32"

source "drivers/dac/Kconfig.ad56xx"

source "drivers/dac/Kconfig.ad5592"

endif # DAC
17 changes: 17 additions & 0 deletions drivers/dac/Kconfig.ad5592
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2023 Grinn
# SPDX -License-Identifier: Apache-2.0

config DAC_AD5592
bool "AD5592 DAC driver"
default y
depends on DT_HAS_ADI_AD5592_DAC_ENABLED
select MFD
help
Enable the AD5592 DAC driver.

config DAC_AD5592_INIT_PRIORITY
int "AD5592 DAC driver initialization priority"
depends on DAC_AD5592
default 85
help
Initialization priority for the AD5592 DAC driver.
114 changes: 114 additions & 0 deletions drivers/dac/dac_ad5592.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2023 Grinn
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT adi_ad5592_dac

#include <zephyr/drivers/dac.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>

#include <zephyr/drivers/mfd/ad5592.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(dac_ad5592, CONFIG_DAC_LOG_LEVEL);

#define AD5592_DAC_RESOLUTION 12
#define AD5592_DAC_WR_MSB_BIT BIT(15)
#define AD5592_DAC_CHANNEL_SHIFT_VAL 12

struct dac_ad5592_config {
const struct device *mfd_dev;
};

struct dac_ad5592_data {
uint8_t dac_conf;
};

static int dac_ad5592_channel_setup(const struct device *dev,
const struct dac_channel_cfg *channel_cfg)
{
const struct dac_ad5592_config *config = dev->config;
struct dac_ad5592_data *data = dev->data;

if (k_is_in_isr()) {
return -EWOULDBLOCK;
}

if (channel_cfg->channel_id >= AD5592_PIN_MAX) {
LOG_ERR("Invalid channel number %d", channel_cfg->channel_id);
return -EINVAL;
}

if (channel_cfg->resolution != AD5592_DAC_RESOLUTION) {
LOG_ERR("Invalid resolution %d", channel_cfg->resolution);
return -EINVAL;
}

data->dac_conf |= BIT(channel_cfg->channel_id);

return mfd_ad5592_write_reg(config->mfd_dev, AD5592_REG_LDAC_EN, data->dac_conf);
}

static int dac_ad5592_write_value(const struct device *dev, uint8_t channel,
uint32_t value)
{
uint16_t msg;

if (k_is_in_isr()) {
return -EWOULDBLOCK;
}

if (channel >= AD5592_PIN_MAX) {
LOG_ERR("Invalid channel number %d", channel);
return -EINVAL;
}

if (value >= (1 << AD5592_DAC_RESOLUTION)) {
LOG_ERR("Value %d out of range", value);
return -EINVAL;
}

msg = sys_cpu_to_be16(AD5592_DAC_WR_MSB_BIT |
channel << AD5592_DAC_CHANNEL_SHIFT_VAL |
value);

return mfd_ad5592_write_raw(dev, msg);
}

static const struct dac_driver_api dac_ad5592_api = {
.channel_setup = dac_ad5592_channel_setup,
.write_value = dac_ad5592_write_value,
};

static int dac_ad5592_init(const struct device *dev)
{
const struct dac_ad5592_config *config = dev->config;
int ret;

if (!device_is_ready(config->mfd_dev)) {
return -ENODEV;
}

ret = mfd_ad5592_write_reg(config->mfd_dev, AD5592_REG_PD_REF_CTRL, AD5592_EN_REF);
if (ret < 0) {
return ret;
}

return 0;
}

#define DAC_AD5592_DEFINE(inst) \
static const struct dac_ad5592_config dac_ad5592_config##inst = { \
.mfd_dev = DEVICE_DT_GET(DT_INST_PARENT(inst)), \
}; \
\
struct dac_ad5592_data dac_ad5592_data##inst; \
\
DEVICE_DT_INST_DEFINE(inst, dac_ad5592_init, NULL, \
&dac_ad5592_data##inst, &dac_ad5592_config##inst, \
POST_KERNEL, CONFIG_DAC_AD5592_INIT_PRIORITY, \
&dac_ad5592_api);

DT_INST_FOREACH_STATUS_OKAY(DAC_AD5592_DEFINE)
15 changes: 15 additions & 0 deletions dts/bindings/dac/adi,ad5592-dac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) 2023 Grinn
# SPDX-License-Identifier: Apache-2.0

description: AD5592 DAC Controller

compatible: "adi,ad5592-dac"

include: dac-controller.yaml

properties:
"#io-channel-cells":
const: 1

io-channel-cells:
- output
14 changes: 14 additions & 0 deletions tests/drivers/build_all/dac/app.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>,
<&test_gpio 0 0>;

test_spi_dac60508: dac60508@0 {
Expand Down Expand Up @@ -233,6 +234,19 @@
#io-channel-cells = <1>;
reset-gpios = <&test_gpio 0 0>;
};

test_spi_ad5592: ad5592@10 {
compatible = "adi,ad5592";
status = "okay";
reg = <0x10>;
spi-max-frequency = <0>;
reset-gpios = <&test_gpio 0 0>;

ad5592_dac: dac-controller {
compatible = "adi,ad5592-dac";
#io-channel-cells = <1>;
};
};
};
};
};

0 comments on commit 6dd963e

Please sign in to comment.