1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT hoperf_th02
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/device.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/util.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/sys/__assert.h>
16 #include <zephyr/logging/log.h>
17
18 #include "th02.h"
19
20 LOG_MODULE_REGISTER(TH02, CONFIG_SENSOR_LOG_LEVEL);
21
read8(const struct i2c_dt_spec * i2c,uint8_t d)22 static uint8_t read8(const struct i2c_dt_spec *i2c, uint8_t d)
23 {
24 uint8_t buf;
25
26 if (i2c_reg_read_byte_dt(i2c, d, &buf) < 0) {
27 LOG_ERR("Error reading register.");
28 }
29 return buf;
30 }
31
is_ready(const struct i2c_dt_spec * i2c)32 static int is_ready(const struct i2c_dt_spec *i2c)
33 {
34
35 uint8_t status;
36
37 if (i2c_reg_read_byte_dt(i2c, TH02_REG_STATUS, &status) < 0) {
38 LOG_ERR("error reading status register");
39 }
40
41 if (status & TH02_STATUS_RDY_MASK) {
42 return 0;
43 } else {
44 return 1;
45 }
46 }
47
get_humi(const struct i2c_dt_spec * i2c)48 static uint16_t get_humi(const struct i2c_dt_spec *i2c)
49 {
50 uint16_t humidity = 0U;
51
52 if (i2c_reg_write_byte_dt(i2c, TH02_REG_CONFIG, TH02_CMD_MEASURE_HUMI) < 0) {
53 LOG_ERR("Error writing register");
54 return 0;
55 }
56 while (!is_ready(i2c)) {
57 }
58
59 humidity = read8(i2c, TH02_REG_DATA_H) << 8;
60 humidity |= read8(i2c, TH02_REG_DATA_L);
61 humidity >>= 4;
62
63 return humidity;
64 }
65
get_temp(const struct i2c_dt_spec * i2c)66 uint16_t get_temp(const struct i2c_dt_spec *i2c)
67 {
68 uint16_t temperature = 0U;
69
70 if (i2c_reg_write_byte_dt(i2c, TH02_REG_CONFIG, TH02_CMD_MEASURE_TEMP) < 0) {
71 LOG_ERR("Error writing register");
72 return 0;
73 }
74 while (!is_ready(i2c)) {
75 }
76
77 temperature = read8(i2c, TH02_REG_DATA_H) << 8;
78 temperature |= read8(i2c, TH02_REG_DATA_L);
79 temperature >>= 2;
80
81 return temperature;
82 }
83
th02_sample_fetch(const struct device * dev,enum sensor_channel chan)84 static int th02_sample_fetch(const struct device *dev,
85 enum sensor_channel chan)
86 {
87 struct th02_data *drv_data = dev->data;
88 const struct th02_config *cfg = dev->config;
89
90 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
91
92 drv_data->t_sample = get_temp(&cfg->i2c);
93 LOG_INF("temp: %u", drv_data->t_sample);
94 drv_data->rh_sample = get_humi(&cfg->i2c);
95 LOG_INF("rh: %u", drv_data->rh_sample);
96
97 return 0;
98 }
99
th02_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)100 static int th02_channel_get(const struct device *dev,
101 enum sensor_channel chan,
102 struct sensor_value *val)
103 {
104 struct th02_data *drv_data = dev->data;
105
106 __ASSERT_NO_MSG(chan == SENSOR_CHAN_AMBIENT_TEMP ||
107 chan == SENSOR_CHAN_HUMIDITY);
108
109 if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
110 /* val = sample / 32 - 50 */
111 val->val1 = drv_data->t_sample / 32U - 50;
112 val->val2 = (drv_data->t_sample % 32) * (1000000 / 32);
113 } else if (chan == SENSOR_CHAN_HUMIDITY) {
114 /* val = sample / 16 -24 */
115 val->val1 = drv_data->rh_sample / 16U - 24;
116 val->val2 = (drv_data->rh_sample % 16) * (1000000 / 16);
117 } else {
118 return -ENOTSUP;
119 }
120
121 return 0;
122 }
123
124 static DEVICE_API(sensor, th02_driver_api) = {
125 .sample_fetch = th02_sample_fetch,
126 .channel_get = th02_channel_get,
127 };
128
th02_init(const struct device * dev)129 static int th02_init(const struct device *dev)
130 {
131 const struct th02_config *cfg = dev->config;
132
133 if (!device_is_ready(cfg->i2c.bus)) {
134 LOG_ERR("Bus device is not ready");
135 return -ENODEV;
136 }
137
138 return 0;
139 }
140
141 #define TH02_DEFINE(inst) \
142 static struct th02_data th02_data_##inst; \
143 \
144 static const struct th02_config th02_config_##inst = { \
145 .i2c = I2C_DT_SPEC_INST_GET(inst), \
146 }; \
147 \
148 SENSOR_DEVICE_DT_INST_DEFINE(inst, th02_init, NULL, \
149 &th02_data_##inst, &th02_config_##inst, POST_KERNEL, \
150 CONFIG_SENSOR_INIT_PRIORITY, &th02_driver_api); \
151
152 DT_INST_FOREACH_STATUS_OKAY(TH02_DEFINE)
153