1 /*
2 * Copyright 2021 Grinn
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT microchip_mcp4728
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/drivers/dac.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/logging/log.h>
13
14 LOG_MODULE_REGISTER(dac_mcp4728, CONFIG_DAC_LOG_LEVEL);
15
16 #define MCP4728_MAX_CHANNEL 4U
17 #define MCP4728_RESOLUTION 0xC
18 #define MCP4728_DAC_MAX_VAL ((1U << MCP4728_RESOLUTION) - 1U)
19 #define MCP4728_MULTI_WRITE_CMD_VAL 8U
20 #define MCP4728_MULTI_WRITE_CMD_POS 3U
21 #define MCP4728_MULTI_WRITE_CHANNEL_POS 1U
22 #define MCP4728_MULTI_WRITE_REFERENCE_POS 7U
23 #define MCP4728_MULTI_WRITE_POWER_DOWN_POS 5U
24 #define MCP4728_MULTI_WRITE_POWER_DOWN_MASK 0X3
25 #define MCP4728_MULTI_WRITE_GAIN_POS 4U
26 #define MCP4728_MULTI_WRITE_DAC_UPPER_VAL_POS 8U
27 #define MCP4728_MULTI_WRITE_DAC_UPPER_VAL_MASK 0xF
28 #define MCP4728_MULTI_WRITE_DAC_LOWER_VAL_MASK 0xFF
29
30 struct mcp4728_config {
31 struct i2c_dt_spec bus;
32 uint8_t power_down[MCP4728_MAX_CHANNEL];
33 uint8_t voltage_reference[MCP4728_MAX_CHANNEL];
34 uint8_t gain[MCP4728_MAX_CHANNEL];
35 };
36
mcp4728_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)37 static int mcp4728_channel_setup(const struct device *dev,
38 const struct dac_channel_cfg *channel_cfg)
39 {
40 if (channel_cfg->channel_id >= MCP4728_MAX_CHANNEL) {
41 return -ENOTSUP;
42 }
43
44 if (channel_cfg->resolution != MCP4728_RESOLUTION) {
45 return -ENOTSUP;
46 }
47
48 if (channel_cfg->internal) {
49 return -ENOTSUP;
50 }
51
52 return 0;
53 }
54
mcp4728_write_value(const struct device * dev,uint8_t channel,uint32_t value)55 static int mcp4728_write_value(const struct device *dev, uint8_t channel, uint32_t value)
56 {
57 const struct mcp4728_config *config = (struct mcp4728_config *)dev->config;
58 uint8_t tx_data[3];
59 int ret;
60
61 if (channel >= MCP4728_MAX_CHANNEL) {
62 return -ENOTSUP;
63 }
64
65 if (value > MCP4728_DAC_MAX_VAL) {
66 return -ENOTSUP;
67 }
68
69 /* Multi-Write Command: Write Multiple DAC Input Registers. */
70 tx_data[0] = (MCP4728_MULTI_WRITE_CMD_VAL << MCP4728_MULTI_WRITE_CMD_POS) |
71 (channel << MCP4728_MULTI_WRITE_CHANNEL_POS);
72 tx_data[1] = (config->voltage_reference[channel] << MCP4728_MULTI_WRITE_REFERENCE_POS) |
73 ((config->power_down[channel] & MCP4728_MULTI_WRITE_POWER_DOWN_MASK)
74 << MCP4728_MULTI_WRITE_POWER_DOWN_POS) |
75 ((config->gain[channel] << MCP4728_MULTI_WRITE_GAIN_POS)) |
76 ((value >> MCP4728_MULTI_WRITE_DAC_UPPER_VAL_POS) &
77 MCP4728_MULTI_WRITE_DAC_UPPER_VAL_MASK);
78 tx_data[2] = (value & MCP4728_MULTI_WRITE_DAC_LOWER_VAL_MASK);
79 ret = i2c_write_dt(&config->bus, tx_data, sizeof(tx_data));
80
81 return ret;
82 }
83
dac_mcp4728_init(const struct device * dev)84 static int dac_mcp4728_init(const struct device *dev)
85 {
86 const struct mcp4728_config *config = dev->config;
87
88 if (!device_is_ready(config->bus.bus)) {
89 LOG_ERR("%s device not found", config->bus.bus->name);
90 return -ENODEV;
91 }
92 return 0;
93 }
94
95 static DEVICE_API(dac, mcp4728_driver_api) = {
96 .channel_setup = mcp4728_channel_setup,
97 .write_value = mcp4728_write_value,
98 };
99
100 #define INST_DT_MCP4728(index) \
101 static const struct mcp4728_config mcp4728_config_##index = { \
102 .bus = I2C_DT_SPEC_INST_GET(index), \
103 .power_down = DT_INST_PROP(index, power_down_mode), \
104 .voltage_reference = DT_INST_PROP(index, voltage_reference), \
105 .gain = DT_INST_PROP_OR(index, gain, {0}), \
106 }; \
107 \
108 DEVICE_DT_INST_DEFINE(index, dac_mcp4728_init, NULL, NULL, \
109 &mcp4728_config_##index, \
110 POST_KERNEL, \
111 CONFIG_DAC_MCP4728_INIT_PRIORITY, \
112 &mcp4728_driver_api);
113
114 DT_INST_FOREACH_STATUS_OKAY(INST_DT_MCP4728);
115