1 /*
2  * Copyright (c) 2019 Peter Bigot Consulting, LLC
3  * Copyright (c) 2016 Intel Corporation
4  * Copyright (c) 2024 Vogl Electronic GmbH
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define DT_DRV_COMPAT jedec_jc_42_4_temp
10 
11 #include <errno.h>
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/i2c.h>
15 #include <zephyr/init.h>
16 #include <zephyr/sys/byteorder.h>
17 #include <zephyr/sys/__assert.h>
18 #include <zephyr/logging/log.h>
19 
20 #include "jc42.h"
21 
22 LOG_MODULE_REGISTER(JC42, CONFIG_SENSOR_LOG_LEVEL);
23 
jc42_reg_read(const struct device * dev,uint8_t reg,uint16_t * val)24 int jc42_reg_read(const struct device *dev, uint8_t reg, uint16_t *val)
25 {
26 	const struct jc42_config *cfg = dev->config;
27 	int rc = i2c_write_read_dt(&cfg->i2c, &reg, sizeof(reg), val, sizeof(*val));
28 
29 	if (rc == 0) {
30 		*val = sys_be16_to_cpu(*val);
31 	}
32 
33 	return rc;
34 }
35 
jc42_reg_write_16bit(const struct device * dev,uint8_t reg,uint16_t val)36 int jc42_reg_write_16bit(const struct device *dev, uint8_t reg, uint16_t val)
37 {
38 	const struct jc42_config *cfg = dev->config;
39 
40 	uint8_t buf[3];
41 
42 	buf[0] = reg;
43 	sys_put_be16(val, &buf[1]);
44 
45 	return i2c_write_dt(&cfg->i2c, buf, sizeof(buf));
46 }
47 
jc42_reg_write_8bit(const struct device * dev,uint8_t reg,uint8_t val)48 int jc42_reg_write_8bit(const struct device *dev, uint8_t reg, uint8_t val)
49 {
50 	const struct jc42_config *cfg = dev->config;
51 	uint8_t buf[2] = {
52 		reg,
53 		val,
54 	};
55 
56 	return i2c_write_dt(&cfg->i2c, buf, sizeof(buf));
57 }
58 
jc42_set_temperature_resolution(const struct device * dev,uint8_t resolution)59 static int jc42_set_temperature_resolution(const struct device *dev, uint8_t resolution)
60 {
61 	return jc42_reg_write_8bit(dev, JC42_REG_RESOLUTION, resolution);
62 }
63 
jc42_sample_fetch(const struct device * dev,enum sensor_channel chan)64 static int jc42_sample_fetch(const struct device *dev, enum sensor_channel chan)
65 {
66 	struct jc42_data *data = dev->data;
67 
68 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
69 
70 	return jc42_reg_read(dev, JC42_REG_TEMP_AMB, &data->reg_val);
71 }
72 
jc42_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)73 static int jc42_channel_get(const struct device *dev, enum sensor_channel chan,
74 			    struct sensor_value *val)
75 {
76 	const struct jc42_data *data = dev->data;
77 	int temp = jc42_temp_signed_from_reg(data->reg_val);
78 
79 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_AMBIENT_TEMP);
80 
81 	if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
82 		return -ENOTSUP;
83 	}
84 
85 	val->val1 = temp / JC42_TEMP_SCALE_CEL;
86 	temp -= val->val1 * JC42_TEMP_SCALE_CEL;
87 	val->val2 = (temp * 1000000) / JC42_TEMP_SCALE_CEL;
88 
89 	return 0;
90 }
91 
92 static DEVICE_API(sensor, jc42_api_funcs) = {
93 	.sample_fetch = jc42_sample_fetch,
94 	.channel_get = jc42_channel_get,
95 #ifdef CONFIG_JC42_TRIGGER
96 	.attr_set = jc42_attr_set,
97 	.trigger_set = jc42_trigger_set,
98 #endif /* CONFIG_JC42_TRIGGER */
99 };
100 
jc42_init(const struct device * dev)101 int jc42_init(const struct device *dev)
102 {
103 	const struct jc42_config *cfg = dev->config;
104 	int rc = 0;
105 
106 	if (!device_is_ready(cfg->i2c.bus)) {
107 		LOG_ERR("Bus device is not ready");
108 		return -ENODEV;
109 	}
110 
111 	rc = jc42_set_temperature_resolution(dev, cfg->resolution);
112 	if (rc) {
113 		LOG_ERR("Could not set the resolution of jc42 module");
114 		return rc;
115 	}
116 
117 #ifdef CONFIG_JC42_TRIGGER
118 	if (cfg->int_gpio.port) {
119 		rc = jc42_setup_interrupt(dev);
120 	}
121 #endif /* CONFIG_JC42_TRIGGER */
122 
123 	return rc;
124 }
125 
126 #define JC42_DEFINE(inst)                                                                          \
127 	static struct jc42_data jc42_data_##inst;                                                  \
128                                                                                                    \
129 	static const struct jc42_config jc42_config_##inst = {                                     \
130 		.i2c = I2C_DT_SPEC_INST_GET(inst),                                                 \
131 		.resolution = DT_INST_PROP(inst, resolution),                                      \
132 		IF_ENABLED(CONFIG_JC42_TRIGGER,                                                    \
133 			   (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, {0}),))};        \
134                                                                                                    \
135 	SENSOR_DEVICE_DT_INST_DEFINE(inst, jc42_init, NULL, &jc42_data_##inst,                     \
136 				     &jc42_config_##inst, POST_KERNEL,                             \
137 				     CONFIG_SENSOR_INIT_PRIORITY, &jc42_api_funcs);
138 
139 DT_INST_FOREACH_STATUS_OKAY(JC42_DEFINE)
140