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