1 /*
2 * Copyright (c) 2023 Grinn
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT adi_ad559x_dac
7
8 #include <zephyr/drivers/dac.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/byteorder.h>
11
12 #include <zephyr/drivers/mfd/ad559x.h>
13
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(dac_ad559x, CONFIG_DAC_LOG_LEVEL);
16
17 #define AD559X_DAC_RESOLUTION 12
18 #define AD559X_DAC_WR_POINTER 0x10
19 #define AD559X_DAC_WR_MSB_BIT BIT(15)
20 #define AD559X_DAC_CHANNEL_SHIFT_VAL 12
21
22 struct dac_ad559x_config {
23 const struct device *mfd_dev;
24 };
25
26 struct dac_ad559x_data {
27 uint8_t dac_conf;
28 };
29
dac_ad559x_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)30 static int dac_ad559x_channel_setup(const struct device *dev,
31 const struct dac_channel_cfg *channel_cfg)
32 {
33 const struct dac_ad559x_config *config = dev->config;
34 struct dac_ad559x_data *data = dev->data;
35
36 if (channel_cfg->channel_id >= AD559X_PIN_MAX) {
37 LOG_ERR("Invalid channel number %d", channel_cfg->channel_id);
38 return -EINVAL;
39 }
40
41 if (channel_cfg->resolution != AD559X_DAC_RESOLUTION) {
42 LOG_ERR("Invalid resolution %d", channel_cfg->resolution);
43 return -EINVAL;
44 }
45
46 data->dac_conf |= BIT(channel_cfg->channel_id);
47
48 return mfd_ad559x_write_reg(config->mfd_dev, AD559X_REG_LDAC_EN, data->dac_conf);
49 }
50
dac_ad559x_write_value(const struct device * dev,uint8_t channel,uint32_t value)51 static int dac_ad559x_write_value(const struct device *dev, uint8_t channel, uint32_t value)
52 {
53 const struct dac_ad559x_config *config = dev->config;
54 uint16_t msg;
55
56 if (channel >= AD559X_PIN_MAX) {
57 LOG_ERR("Invalid channel number %d", channel);
58 return -EINVAL;
59 }
60
61 if (value >= (1 << AD559X_DAC_RESOLUTION)) {
62 LOG_ERR("Value %d out of range", value);
63 return -EINVAL;
64 }
65
66 if (mfd_ad559x_has_pointer_byte_map(config->mfd_dev)) {
67 return mfd_ad559x_write_reg(config->mfd_dev, AD559X_DAC_WR_POINTER | channel,
68 value);
69 } else {
70 msg = sys_cpu_to_be16(AD559X_DAC_WR_MSB_BIT |
71 channel << AD559X_DAC_CHANNEL_SHIFT_VAL | value);
72
73 return mfd_ad559x_write_raw(config->mfd_dev, (uint8_t *)&msg, sizeof(msg));
74 }
75 }
76
77 static const struct dac_driver_api dac_ad559x_api = {
78 .channel_setup = dac_ad559x_channel_setup,
79 .write_value = dac_ad559x_write_value,
80 };
81
dac_ad559x_init(const struct device * dev)82 static int dac_ad559x_init(const struct device *dev)
83 {
84 const struct dac_ad559x_config *config = dev->config;
85 int ret;
86
87 if (!device_is_ready(config->mfd_dev)) {
88 return -ENODEV;
89 }
90
91 ret = mfd_ad559x_write_reg(config->mfd_dev, AD559X_REG_PD_REF_CTRL, AD559X_EN_REF);
92 if (ret < 0) {
93 return ret;
94 }
95
96 return 0;
97 }
98
99 #define DAC_AD559X_DEFINE(inst) \
100 static const struct dac_ad559x_config dac_ad559x_config##inst = { \
101 .mfd_dev = DEVICE_DT_GET(DT_INST_PARENT(inst)), \
102 }; \
103 \
104 struct dac_ad559x_data dac_ad559x_data##inst; \
105 \
106 DEVICE_DT_INST_DEFINE(inst, dac_ad559x_init, NULL, &dac_ad559x_data##inst, \
107 &dac_ad559x_config##inst, POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, \
108 &dac_ad559x_api);
109
110 DT_INST_FOREACH_STATUS_OKAY(DAC_AD559X_DEFINE)
111