1 /*
2  * Copyright (c) 2024 Benedikt Schmidt <benediktibk@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT onnn_nct75
8 
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/drivers/sensor.h>
11 #include <zephyr/logging/log.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/util.h>
14 
15 LOG_MODULE_REGISTER(NCT75, CONFIG_SENSOR_LOG_LEVEL);
16 
17 #define NCT75REGISTER_STOREDTEMPERATUREVALUE 0x00
18 #define NCT75REGISTER_CONFIGURATION          0x01
19 #define NCT75REGISTER_ONESHOT                0x04
20 
21 #define NCT75_CONFIGURATION_ONESHOTMODE_POS 5
22 
23 #define NCT75_TEMPERATURE_CONVERSION_TIME_US      48500
24 #define NCT75_TEMPERATURE_CONVERSION_WAIT_TIME_US (NCT75_TEMPERATURE_CONVERSION_TIME_US + 1000)
25 
26 struct nct75_config {
27 	struct i2c_dt_spec i2c;
28 };
29 
30 struct nct75_data {
31 	/* temperature in 1e-6 °C */
32 	int64_t value;
33 };
34 
nct75_sample_fetch(const struct device * dev,enum sensor_channel chan)35 static int nct75_sample_fetch(const struct device *dev, enum sensor_channel chan)
36 {
37 	const struct nct75_config *config = dev->config;
38 	struct nct75_data *data = dev->data;
39 	int result;
40 	uint8_t write_buffer = 0;
41 	uint16_t read_buffer;
42 	int16_t raw_value;
43 
44 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
45 
46 	result = i2c_reg_write_byte_dt(&config->i2c, NCT75REGISTER_ONESHOT, 0);
47 	if (result != 0) {
48 		LOG_ERR("%s: unable to trigger temperature one shot measurement", dev->name);
49 		return result;
50 	}
51 
52 	k_sleep(K_USEC(NCT75_TEMPERATURE_CONVERSION_WAIT_TIME_US));
53 
54 	write_buffer = NCT75REGISTER_STOREDTEMPERATUREVALUE;
55 	result = i2c_write_read_dt(&config->i2c, &write_buffer, sizeof(write_buffer), &read_buffer,
56 				   sizeof(read_buffer));
57 	if (result != 0) {
58 		LOG_ERR("%s: unable to read temperature", dev->name);
59 		return result;
60 	}
61 
62 	raw_value = arithmetic_shift_right(sys_be16_to_cpu(read_buffer), 4);
63 	data->value = (raw_value * 1000 * 1000) * 100 / 0x640;
64 
65 	return 0;
66 }
67 
nct75_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)68 static int nct75_channel_get(const struct device *dev, enum sensor_channel chan,
69 			     struct sensor_value *val)
70 {
71 	struct nct75_data *data = dev->data;
72 
73 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
74 		LOG_ERR("%s: requesting unsupported channel %i", dev->name, chan);
75 		return -ENOTSUP;
76 	}
77 
78 	val->val1 = data->value / (1000 * 1000);
79 	val->val2 = data->value - val->val1 * 1000 * 1000;
80 	return 0;
81 }
82 
83 static DEVICE_API(sensor, nct75_api) = {
84 	.sample_fetch = nct75_sample_fetch,
85 	.channel_get = nct75_channel_get,
86 };
87 
nct75_init(const struct device * dev)88 static int nct75_init(const struct device *dev)
89 {
90 	const struct nct75_config *config = dev->config;
91 	uint8_t configuration_register_value = BIT(NCT75_CONFIGURATION_ONESHOTMODE_POS);
92 	int result;
93 
94 	if (!i2c_is_ready_dt(&config->i2c)) {
95 		LOG_ERR("I2C device not ready");
96 		return -ENODEV;
97 	}
98 
99 	result = i2c_reg_write_byte_dt(&config->i2c, NCT75REGISTER_CONFIGURATION,
100 				       configuration_register_value);
101 	if (result != 0) {
102 		LOG_ERR("%s: unable to configure temperature sensor", dev->name);
103 		return result;
104 	}
105 
106 	return 0;
107 }
108 
109 #define NCT75_INIT(inst)                                                                           \
110 	static const struct nct75_config nct75_##inst##_config = {                                 \
111 		.i2c = I2C_DT_SPEC_INST_GET(inst),                                                 \
112 	};                                                                                         \
113                                                                                                    \
114 	static struct nct75_data nct75_##inst##_data;                                              \
115                                                                                                    \
116 	SENSOR_DEVICE_DT_INST_DEFINE(inst, nct75_init, NULL, &nct75_##inst##_data,                 \
117 				     &nct75_##inst##_config, POST_KERNEL,                          \
118 				     CONFIG_SENSOR_INIT_PRIORITY, &nct75_api);
119 
120 DT_INST_FOREACH_STATUS_OKAY(NCT75_INIT);
121