1 /*
2 * Copyright (c) 2023 Grinn
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #define DT_DRV_COMPAT adi_ad5592_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/ad5592.h>
13
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(dac_ad5592, CONFIG_DAC_LOG_LEVEL);
16
17 #define AD5592_DAC_RESOLUTION 12
18 #define AD5592_DAC_WR_MSB_BIT BIT(15)
19 #define AD5592_DAC_CHANNEL_SHIFT_VAL 12
20
21 struct dac_ad5592_config {
22 const struct device *mfd_dev;
23 };
24
25 struct dac_ad5592_data {
26 uint8_t dac_conf;
27 };
28
dac_ad5592_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)29 static int dac_ad5592_channel_setup(const struct device *dev,
30 const struct dac_channel_cfg *channel_cfg)
31 {
32 const struct dac_ad5592_config *config = dev->config;
33 struct dac_ad5592_data *data = dev->data;
34
35 if (channel_cfg->channel_id >= AD5592_PIN_MAX) {
36 LOG_ERR("Invalid channel number %d", channel_cfg->channel_id);
37 return -EINVAL;
38 }
39
40 if (channel_cfg->resolution != AD5592_DAC_RESOLUTION) {
41 LOG_ERR("Invalid resolution %d", channel_cfg->resolution);
42 return -EINVAL;
43 }
44
45 data->dac_conf |= BIT(channel_cfg->channel_id);
46
47 return mfd_ad5592_write_reg(config->mfd_dev, AD5592_REG_LDAC_EN, data->dac_conf);
48 }
49
dac_ad5592_write_value(const struct device * dev,uint8_t channel,uint32_t value)50 static int dac_ad5592_write_value(const struct device *dev, uint8_t channel,
51 uint32_t value)
52 {
53 const struct dac_ad5592_config *config = dev->config;
54 uint16_t msg;
55
56 if (channel >= AD5592_PIN_MAX) {
57 LOG_ERR("Invalid channel number %d", channel);
58 return -EINVAL;
59 }
60
61 if (value >= (1 << AD5592_DAC_RESOLUTION)) {
62 LOG_ERR("Value %d out of range", value);
63 return -EINVAL;
64 }
65
66 msg = sys_cpu_to_be16(AD5592_DAC_WR_MSB_BIT |
67 channel << AD5592_DAC_CHANNEL_SHIFT_VAL |
68 value);
69
70 return mfd_ad5592_write_raw(config->mfd_dev, msg);
71 }
72
73 static const struct dac_driver_api dac_ad5592_api = {
74 .channel_setup = dac_ad5592_channel_setup,
75 .write_value = dac_ad5592_write_value,
76 };
77
dac_ad5592_init(const struct device * dev)78 static int dac_ad5592_init(const struct device *dev)
79 {
80 const struct dac_ad5592_config *config = dev->config;
81 int ret;
82
83 if (!device_is_ready(config->mfd_dev)) {
84 return -ENODEV;
85 }
86
87 ret = mfd_ad5592_write_reg(config->mfd_dev, AD5592_REG_PD_REF_CTRL, AD5592_EN_REF);
88 if (ret < 0) {
89 return ret;
90 }
91
92 return 0;
93 }
94
95 #define DAC_AD5592_DEFINE(inst) \
96 static const struct dac_ad5592_config dac_ad5592_config##inst = { \
97 .mfd_dev = DEVICE_DT_GET(DT_INST_PARENT(inst)), \
98 }; \
99 \
100 struct dac_ad5592_data dac_ad5592_data##inst; \
101 \
102 DEVICE_DT_INST_DEFINE(inst, dac_ad5592_init, NULL, \
103 &dac_ad5592_data##inst, &dac_ad5592_config##inst, \
104 POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, \
105 &dac_ad5592_api);
106
107 DT_INST_FOREACH_STATUS_OKAY(DAC_AD5592_DEFINE)
108