1 /* mpr.c - Driver for Honeywell MPR pressure sensor series */
2
3 /*
4 * Copyright (c) 2020 Sven Herrmann
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define DT_DRV_COMPAT honeywell_mpr
10
11 #include <errno.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/__assert.h>
14 #include <zephyr/drivers/sensor.h>
15 #include <zephyr/drivers/i2c.h>
16 #include <zephyr/sys/byteorder.h>
17 #include <zephyr/init.h>
18 #include <zephyr/logging/log.h>
19 #include "mpr.h"
20 #include "mpr_configuration.h"
21
22 LOG_MODULE_REGISTER(MPR, CONFIG_SENSOR_LOG_LEVEL);
23
mpr_init(const struct device * dev)24 static int mpr_init(const struct device *dev)
25 {
26 const struct mpr_config *cfg = dev->config;
27
28 if (!device_is_ready(cfg->i2c.bus)) {
29 LOG_ERR("Bus device is not ready");
30 return -ENODEV;
31 }
32
33 return 0;
34 }
35
mpr_read_reg(const struct device * dev)36 static int mpr_read_reg(const struct device *dev)
37 {
38 struct mpr_data *data = dev->data;
39 const struct mpr_config *cfg = dev->config;
40
41 uint8_t write_buf[] = { MPR_OUTPUT_MEASUREMENT_COMMAND, 0x00, 0x00 };
42 uint8_t read_buf[4] = { 0x0 };
43
44 int rc = i2c_write_dt(&cfg->i2c, write_buf, sizeof(write_buf));
45
46 if (rc < 0) {
47 return rc;
48 }
49
50 uint8_t retries = MPR_REG_READ_MAX_RETRIES;
51
52 for (; retries > 0; retries--) {
53 k_sleep(K_MSEC(MPR_REG_READ_DATA_CONV_DELAY_MS));
54
55 rc = i2c_read_dt(&cfg->i2c, read_buf, sizeof(read_buf));
56 if (rc < 0) {
57 return rc;
58 }
59
60 if (!(*read_buf & MPR_STATUS_MASK_POWER_ON)
61 || (*read_buf & MPR_STATUS_MASK_INTEGRITY_TEST_FAILED)
62 || (*read_buf & MPR_STATUS_MASK_MATH_SATURATION)) {
63 return -EIO;
64 }
65
66 if (!(*read_buf & MPR_STATUS_MASK_BUSY)) {
67 break;
68 }
69 }
70
71 if (retries == 0) {
72 return -EIO;
73 }
74
75 data->reg_val = (read_buf[1] << 16)
76 | (read_buf[2] << 8)
77 | read_buf[3];
78
79 return 0;
80 }
81
82 /* (reg_value - out_min) * (p_max - p_min)
83 * pressure = --------------------------------------- + p_min
84 * out_max - out_min
85 *
86 * returns pressure [kPa] * 10^6
87 */
mpr_convert_reg(const uint32_t * reg,uint64_t * value)88 static inline void mpr_convert_reg(const uint32_t *reg, uint64_t *value)
89 {
90 if (*reg > MPR_OUTPUT_MIN) {
91 *value = (uint64_t)(*reg - MPR_OUTPUT_MIN) * (MPR_P_MAX - MPR_P_MIN);
92 *value *= MPR_CONVERSION_FACTOR;
93 *value /= MPR_OUTPUT_RANGE;
94 *value += MPR_P_MIN;
95 } else {
96 *value = MPR_P_MIN;
97 }
98 }
99
mpr_sample_fetch(const struct device * dev,enum sensor_channel chan)100 static int mpr_sample_fetch(const struct device *dev,
101 enum sensor_channel chan)
102 {
103 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_PRESS);
104
105 return mpr_read_reg(dev);
106 }
107
mpr_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)108 static int mpr_channel_get(const struct device *dev,
109 enum sensor_channel chan,
110 struct sensor_value *val)
111 {
112 const struct mpr_data *data = dev->data;
113
114 __ASSERT_NO_MSG(chan == SENSOR_CHAN_PRESS);
115
116 if (chan != SENSOR_CHAN_PRESS) {
117 return -ENOTSUP;
118 }
119
120 uint64_t value;
121
122 mpr_convert_reg(&data->reg_val, &value);
123
124 val->val1 = value / 1000000;
125 val->val2 = value % 1000000;
126
127 return 0;
128 }
129
130 static const struct sensor_driver_api mpr_api_funcs = {
131 .sample_fetch = mpr_sample_fetch,
132 .channel_get = mpr_channel_get,
133 };
134
135 #define MPR_DEFINE(inst) \
136 static struct mpr_data mpr_data_##inst; \
137 \
138 static const struct mpr_config mpr_config_##inst = { \
139 .i2c = I2C_DT_SPEC_INST_GET(inst), \
140 }; \
141 \
142 SENSOR_DEVICE_DT_INST_DEFINE(inst, mpr_init, NULL, \
143 &mpr_data_##inst, &mpr_config_##inst, POST_KERNEL, \
144 CONFIG_SENSOR_INIT_PRIORITY, &mpr_api_funcs); \
145
146 DT_INST_FOREACH_STATUS_OKAY(MPR_DEFINE)
147