1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT ti_tmp007
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/util.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/sys/__assert.h>
17 #include <zephyr/logging/log.h>
18
19 #include "tmp007.h"
20
21 LOG_MODULE_REGISTER(TMP007, CONFIG_SENSOR_LOG_LEVEL);
22
tmp007_reg_read(const struct i2c_dt_spec * i2c,uint8_t reg,uint16_t * val)23 int tmp007_reg_read(const struct i2c_dt_spec *i2c, uint8_t reg, uint16_t *val)
24 {
25 if (i2c_burst_read_dt(i2c, reg, (uint8_t *) val, 2) < 0) {
26 LOG_ERR("I2C read failed");
27 return -EIO;
28 }
29
30 *val = sys_be16_to_cpu(*val);
31
32 return 0;
33 }
34
tmp007_reg_write(const struct i2c_dt_spec * i2c,uint8_t reg,uint16_t val)35 int tmp007_reg_write(const struct i2c_dt_spec *i2c, uint8_t reg, uint16_t val)
36 {
37 uint8_t tx_buf[3] = {reg, val >> 8, val & 0xFF};
38
39 return i2c_write_dt(i2c, tx_buf, sizeof(tx_buf));
40 }
41
tmp007_reg_update(const struct i2c_dt_spec * i2c,uint8_t reg,uint16_t mask,uint16_t val)42 int tmp007_reg_update(const struct i2c_dt_spec *i2c, uint8_t reg,
43 uint16_t mask, uint16_t val)
44 {
45 uint16_t old_val = 0U;
46 uint16_t new_val;
47
48 if (tmp007_reg_read(i2c, reg, &old_val) < 0) {
49 return -EIO;
50 }
51
52 new_val = old_val & ~mask;
53 new_val |= val & mask;
54
55 return tmp007_reg_write(i2c, reg, new_val);
56 }
57
tmp007_sample_fetch(const struct device * dev,enum sensor_channel chan)58 static int tmp007_sample_fetch(const struct device *dev,
59 enum sensor_channel chan)
60 {
61 struct tmp007_data *drv_data = dev->data;
62 const struct tmp007_config *cfg = dev->config;
63 uint16_t val;
64
65 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP);
66
67 if (tmp007_reg_read(&cfg->i2c, TMP007_REG_TOBJ, &val) < 0) {
68 return -EIO;
69 }
70
71 if (val & TMP007_DATA_INVALID_BIT) {
72 return -EIO;
73 }
74
75 drv_data->sample = arithmetic_shift_right((int16_t)val, 2);
76
77 return 0;
78 }
79
tmp007_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)80 static int tmp007_channel_get(const struct device *dev,
81 enum sensor_channel chan,
82 struct sensor_value *val)
83 {
84 struct tmp007_data *drv_data = dev->data;
85 int32_t uval;
86
87 if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
88 return -ENOTSUP;
89 }
90
91 uval = (int32_t)drv_data->sample * TMP007_TEMP_SCALE;
92 val->val1 = uval / 1000000;
93 val->val2 = uval % 1000000;
94
95 return 0;
96 }
97
98 static const struct sensor_driver_api tmp007_driver_api = {
99 #ifdef CONFIG_TMP007_TRIGGER
100 .attr_set = tmp007_attr_set,
101 .trigger_set = tmp007_trigger_set,
102 #endif
103 .sample_fetch = tmp007_sample_fetch,
104 .channel_get = tmp007_channel_get,
105 };
106
tmp007_init(const struct device * dev)107 int tmp007_init(const struct device *dev)
108 {
109 const struct tmp007_config *cfg = dev->config;
110
111 if (!device_is_ready(cfg->i2c.bus)) {
112 LOG_ERR("Bus device is not ready");
113 return -ENODEV;
114 }
115
116 #ifdef CONFIG_TMP007_TRIGGER
117 if (cfg->int_gpio.port) {
118 if (tmp007_init_interrupt(dev) < 0) {
119 LOG_DBG("Failed to initialize interrupt!");
120 return -EIO;
121 }
122 }
123 #endif
124
125 return 0;
126 }
127
128 #define TMP007_DEFINE(inst) \
129 static struct tmp007_data tmp007_data_##inst; \
130 \
131 static const struct tmp007_config tmp007_config_##inst = { \
132 .i2c = I2C_DT_SPEC_INST_GET(inst), \
133 IF_ENABLED(CONFIG_TMP007_TRIGGER, \
134 (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }),)) \
135 }; \
136 \
137 SENSOR_DEVICE_DT_INST_DEFINE(inst, tmp007_init, NULL, \
138 &tmp007_data_##inst, &tmp007_config_##inst, POST_KERNEL, \
139 CONFIG_SENSOR_INIT_PRIORITY, &tmp007_driver_api); \
140
141 DT_INST_FOREACH_STATUS_OKAY(TMP007_DEFINE)
142