forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadt7420.c
230 lines (187 loc) · 5.23 KB
/
adt7420.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2018 Analog Devices Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <drivers/i2c.h>
#include <sys/byteorder.h>
#include <sys/util.h>
#include <kernel.h>
#include <drivers/sensor.h>
#include <sys/__assert.h>
#include "adt7420.h"
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(ADT7420);
static int adt7420_temp_reg_read(struct device *dev, u8_t reg, s16_t *val)
{
struct adt7420_data *drv_data = dev->driver_data;
const struct adt7420_dev_config *cfg = dev->config->config_info;
if (i2c_burst_read(drv_data->i2c, cfg->i2c_addr,
reg, (u8_t *) val, 2) < 0) {
return -EIO;
}
*val = sys_be16_to_cpu(*val);
return 0;
}
static int adt7420_temp_reg_write(struct device *dev, u8_t reg, s16_t val)
{
struct adt7420_data *drv_data = dev->driver_data;
const struct adt7420_dev_config *cfg = dev->config->config_info;
u8_t buf[3] = {reg, val >> 8, val & 0xFF};
return i2c_write(drv_data->i2c, buf, sizeof(buf), cfg->i2c_addr);
}
static int adt7420_attr_set(struct device *dev,
enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
struct adt7420_data *drv_data = dev->driver_data;
const struct adt7420_dev_config *cfg = dev->config->config_info;
u8_t val8, reg = 0U;
u16_t rate;
s64_t value;
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
return -ENOTSUP;
}
switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY:
rate = val->val1 * 1000 + val->val2 / 1000; /* rate in mHz */
switch (rate) {
case 240:
val8 = ADT7420_OP_MODE_CONT_CONV;
break;
case 1000:
val8 = ADT7420_OP_MODE_1_SPS;
break;
default:
return -EINVAL;
}
if (i2c_reg_update_byte(drv_data->i2c, cfg->i2c_addr,
ADT7420_REG_CONFIG,
ADT7420_CONFIG_OP_MODE(~0),
ADT7420_CONFIG_OP_MODE(val8)) < 0) {
LOG_DBG("Failed to set attribute!");
return -EIO;
}
return 0;
case SENSOR_ATTR_UPPER_THRESH:
reg = ADT7420_REG_T_HIGH_MSB;
/* Fallthrough */
case SENSOR_ATTR_LOWER_THRESH:
if (!reg) {
reg = ADT7420_REG_T_LOW_MSB;
}
if ((val->val1 > 150) || (val->val1 < -40)) {
return -EINVAL;
}
value = (s64_t)val->val1 * 1000000 + val->val2;
value = (value / ADT7420_TEMP_SCALE) << 1;
if (adt7420_temp_reg_write(dev, reg, value) < 0) {
LOG_DBG("Failed to set attribute!");
return -EIO;
}
return 0;
default:
return -ENOTSUP;
}
return 0;
}
static int adt7420_sample_fetch(struct device *dev, enum sensor_channel chan)
{
struct adt7420_data *drv_data = dev->driver_data;
s16_t value;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL ||
chan == SENSOR_CHAN_AMBIENT_TEMP);
if (adt7420_temp_reg_read(dev, ADT7420_REG_TEMP_MSB, &value) < 0) {
return -EIO;
}
drv_data->sample = value >> 1; /* use 15-bit only */
return 0;
}
static int adt7420_channel_get(struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
{
struct adt7420_data *drv_data = dev->driver_data;
s32_t value;
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
return -ENOTSUP;
}
value = (s32_t)drv_data->sample * ADT7420_TEMP_SCALE;
val->val1 = value / 1000000;
val->val2 = value % 1000000;
return 0;
}
static const struct sensor_driver_api adt7420_driver_api = {
.attr_set = adt7420_attr_set,
.sample_fetch = adt7420_sample_fetch,
.channel_get = adt7420_channel_get,
#ifdef CONFIG_ADT7420_TRIGGER
.trigger_set = adt7420_trigger_set,
#endif
};
static int adt7420_probe(struct device *dev)
{
struct adt7420_data *drv_data = dev->driver_data;
const struct adt7420_dev_config *cfg = dev->config->config_info;
u8_t value;
int ret;
ret = i2c_reg_read_byte(drv_data->i2c, cfg->i2c_addr,
ADT7420_REG_ID, &value);
if (ret) {
return ret;
}
if (value != ADT7420_DEFAULT_ID) {
return -ENODEV;
}
ret = i2c_reg_write_byte(drv_data->i2c, cfg->i2c_addr,
ADT7420_REG_CONFIG, ADT7420_CONFIG_RESOLUTION |
ADT7420_CONFIG_OP_MODE(ADT7420_OP_MODE_CONT_CONV));
if (ret) {
return ret;
}
ret = i2c_reg_write_byte(drv_data->i2c, cfg->i2c_addr,
ADT7420_REG_HIST, CONFIG_ADT7420_TEMP_HYST);
if (ret) {
return ret;
}
ret = adt7420_temp_reg_write(dev, ADT7420_REG_T_CRIT_MSB,
(CONFIG_ADT7420_TEMP_CRIT * 1000000 /
ADT7420_TEMP_SCALE) << 1);
if (ret) {
return ret;
}
#ifdef CONFIG_ADT7420_TRIGGER
if (adt7420_init_interrupt(dev) < 0) {
LOG_ERR("Failed to initialize interrupt!");
return -EIO;
}
#endif
return 0;
}
static int adt7420_init(struct device *dev)
{
struct adt7420_data *drv_data = dev->driver_data;
const struct adt7420_dev_config *cfg = dev->config->config_info;
drv_data->i2c = device_get_binding(cfg->i2c_port);
if (drv_data->i2c == NULL) {
LOG_DBG("Failed to get pointer to %s device!",
cfg->i2c_port);
return -EINVAL;
}
return adt7420_probe(dev);
}
static struct adt7420_data adt7420_driver;
static const struct adt7420_dev_config adt7420_config = {
.i2c_port = DT_INST_0_ADI_ADT7420_BUS_NAME,
.i2c_addr = DT_INST_0_ADI_ADT7420_BASE_ADDRESS,
#ifdef CONFIG_ADT7420_TRIGGER
.gpio_port = DT_INST_0_ADI_ADT7420_INT_GPIOS_CONTROLLER,
.int_gpio = DT_INST_0_ADI_ADT7420_INT_GPIOS_PIN,
#endif
};
DEVICE_AND_API_INIT(adt7420, DT_INST_0_ADI_ADT7420_LABEL, adt7420_init, &adt7420_driver,
&adt7420_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
&adt7420_driver_api);