1 /*
2  * Copyright (c) 2021 Laird Connectivity
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #define DT_DRV_COMPAT microchip_mcp4725
7 
8 #include <zephyr/kernel.h>
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/drivers/dac.h>
11 #include <zephyr/logging/log.h>
12 
13 LOG_MODULE_REGISTER(dac_mcp4725, CONFIG_DAC_LOG_LEVEL);
14 
15 /* Information in this file comes from MCP4725 datasheet revision D
16  * found at https://ww1.microchip.com/downloads/en/DeviceDoc/22039d.pdf
17  */
18 
19 /* Defines for field values in MCP4725 DAC register */
20 #define MCP4725_DAC_MAX_VAL			0xFFF
21 
22 #define MCP4725_FAST_MODE_POWER_DOWN_POS	4U
23 #define MCP4725_FAST_MODE_DAC_UPPER_VAL_POS	8U
24 #define MCP4725_FAST_MODE_DAC_UPPER_VAL_MASK	0xF
25 #define MCP4725_FAST_MODE_DAC_LOWER_VAL_MASK	0xFF
26 
27 #define MCP4725_READ_RDY_POS			7U
28 #define MCP4725_READ_RDY_MASK			(0x1 << MCP4725_READ_RDY_POS)
29 
30 /* After writing eeprom, the MCP4725 can be in a busy state for 25 - 50ms
31  * See section 1.0 of MCP4725 datasheet, 'Electrical Characteristics'
32  */
33 #define MCP4725_BUSY_TIMEOUT_MS			60U
34 
35 struct mcp4725_config {
36 	struct i2c_dt_spec i2c;
37 };
38 
39 /* Read mcp4725 and check RDY status bit */
mcp4725_wait_until_ready(const struct device * dev)40 static int mcp4725_wait_until_ready(const struct device *dev)
41 {
42 	const struct mcp4725_config *config = dev->config;
43 	uint8_t rx_data[5];
44 	bool mcp4725_ready = false;
45 	int ret;
46 	int32_t timeout = k_uptime_get_32() + MCP4725_BUSY_TIMEOUT_MS;
47 
48 	/* Wait until RDY bit is set or return error if timer exceeds MCP4725_BUSY_TIMEOUT_MS */
49 	while (!mcp4725_ready) {
50 		ret = i2c_read_dt(&config->i2c, rx_data, sizeof(rx_data));
51 
52 		if (ret == 0) {
53 			mcp4725_ready = rx_data[0] & MCP4725_READ_RDY_MASK;
54 		} else {
55 			/* I2C error */
56 			return ret;
57 		}
58 
59 		if (k_uptime_get_32() > timeout) {
60 			return -ETIMEDOUT;
61 		}
62 	}
63 
64 	return 0;
65 }
66 
67 /* MCP4725 is a single channel 12 bit DAC */
mcp4725_channel_setup(const struct device * dev,const struct dac_channel_cfg * channel_cfg)68 static int mcp4725_channel_setup(const struct device *dev,
69 				   const struct dac_channel_cfg *channel_cfg)
70 {
71 	if (channel_cfg->channel_id != 0) {
72 		return -EINVAL;
73 	}
74 
75 	if (channel_cfg->resolution != 12) {
76 		return -ENOTSUP;
77 	}
78 
79 	if (channel_cfg->internal) {
80 		return -ENOTSUP;
81 	}
82 
83 	return 0;
84 }
85 
mcp4725_write_value(const struct device * dev,uint8_t channel,uint32_t value)86 static int mcp4725_write_value(const struct device *dev, uint8_t channel,
87 				uint32_t value)
88 {
89 	const struct mcp4725_config *config = dev->config;
90 	uint8_t tx_data[2];
91 	int ret;
92 
93 	if (channel != 0) {
94 		return -EINVAL;
95 	}
96 
97 	/* Check value isn't over 12 bits */
98 	if (value > MCP4725_DAC_MAX_VAL) {
99 		return -ENOTSUP;
100 	}
101 
102 	/* WRITE_MODE_FAST message format (2 bytes):
103 	 *
104 	 * ||     15 14     |        13 12        |    11 10 9 8    || 7 6 5 4 3 2 1 0 ||
105 	 * || Fast mode (0) | Power-down bits (0) | DAC value[11:8] || DAC value[7:0]  ||
106 	 */
107 	tx_data[0] = ((value >> MCP4725_FAST_MODE_DAC_UPPER_VAL_POS) &
108 		MCP4725_FAST_MODE_DAC_UPPER_VAL_MASK);
109 	tx_data[1] = (value & MCP4725_FAST_MODE_DAC_LOWER_VAL_MASK);
110 	ret = i2c_write_dt(&config->i2c, tx_data, sizeof(tx_data));
111 
112 	return ret;
113 }
114 
dac_mcp4725_init(const struct device * dev)115 static int dac_mcp4725_init(const struct device *dev)
116 {
117 	const struct mcp4725_config *config = dev->config;
118 
119 	if (!device_is_ready(config->i2c.bus)) {
120 		LOG_ERR("I2C device not found");
121 		return -EINVAL;
122 	}
123 
124 	/* Check we can read a 'RDY' bit from this device */
125 	if (mcp4725_wait_until_ready(dev)) {
126 		return -EBUSY;
127 	}
128 
129 	return 0;
130 }
131 
132 static DEVICE_API(dac, mcp4725_driver_api) = {
133 	.channel_setup = mcp4725_channel_setup,
134 	.write_value = mcp4725_write_value,
135 };
136 
137 
138 #define INST_DT_MCP4725(index)						\
139 	static const struct mcp4725_config mcp4725_config_##index = {	\
140 		.i2c = I2C_DT_SPEC_INST_GET(index),			\
141 	};								\
142 									\
143 	DEVICE_DT_INST_DEFINE(index, dac_mcp4725_init,			\
144 			    NULL,					\
145 			    NULL,					\
146 			    &mcp4725_config_##index, POST_KERNEL,	\
147 			    CONFIG_DAC_MCP4725_INIT_PRIORITY,		\
148 			    &mcp4725_driver_api);
149 
150 DT_INST_FOREACH_STATUS_OKAY(INST_DT_MCP4725);
151