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 	return 0;
80 }
81 
mcp4725_write_value(const struct device * dev,uint8_t channel,uint32_t value)82 static int mcp4725_write_value(const struct device *dev, uint8_t channel,
83 				uint32_t value)
84 {
85 	const struct mcp4725_config *config = dev->config;
86 	uint8_t tx_data[2];
87 	int ret;
88 
89 	if (channel != 0) {
90 		return -EINVAL;
91 	}
92 
93 	/* Check value isn't over 12 bits */
94 	if (value > MCP4725_DAC_MAX_VAL) {
95 		return -ENOTSUP;
96 	}
97 
98 	/* WRITE_MODE_FAST message format (2 bytes):
99 	 *
100 	 * ||     15 14     |        13 12        |    11 10 9 8    || 7 6 5 4 3 2 1 0 ||
101 	 * || Fast mode (0) | Power-down bits (0) | DAC value[11:8] || DAC value[7:0]  ||
102 	 */
103 	tx_data[0] = ((value >> MCP4725_FAST_MODE_DAC_UPPER_VAL_POS) &
104 		MCP4725_FAST_MODE_DAC_UPPER_VAL_MASK);
105 	tx_data[1] = (value & MCP4725_FAST_MODE_DAC_LOWER_VAL_MASK);
106 	ret = i2c_write_dt(&config->i2c, tx_data, sizeof(tx_data));
107 
108 	return ret;
109 }
110 
dac_mcp4725_init(const struct device * dev)111 static int dac_mcp4725_init(const struct device *dev)
112 {
113 	const struct mcp4725_config *config = dev->config;
114 
115 	if (!device_is_ready(config->i2c.bus)) {
116 		LOG_ERR("I2C device not found");
117 		return -EINVAL;
118 	}
119 
120 	/* Check we can read a 'RDY' bit from this device */
121 	if (mcp4725_wait_until_ready(dev)) {
122 		return -EBUSY;
123 	}
124 
125 	return 0;
126 }
127 
128 static const struct dac_driver_api mcp4725_driver_api = {
129 	.channel_setup = mcp4725_channel_setup,
130 	.write_value = mcp4725_write_value,
131 };
132 
133 
134 #define INST_DT_MCP4725(index)						\
135 	static const struct mcp4725_config mcp4725_config_##index = {	\
136 		.i2c = I2C_DT_SPEC_INST_GET(index),			\
137 	};								\
138 									\
139 	DEVICE_DT_INST_DEFINE(index, dac_mcp4725_init,			\
140 			    NULL,					\
141 			    NULL,					\
142 			    &mcp4725_config_##index, POST_KERNEL,	\
143 			    CONFIG_DAC_MCP4725_INIT_PRIORITY,		\
144 			    &mcp4725_driver_api);
145 
146 DT_INST_FOREACH_STATUS_OKAY(INST_DT_MCP4725);
147