1 /*
2  * Copyright (c) 2019 Thomas Schmid <tom@lfence.de>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __SENSOR_MS5607_H__
8 #define __SENSOR_MS5607_H__
9 
10 #include <zephyr/types.h>
11 #include <zephyr/device.h>
12 
13 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
14 #include <zephyr/drivers/i2c.h>
15 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
16 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
17 #include <zephyr/drivers/spi.h>
18 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
19 
20 #define MS5607_CMD_RESET 0x1E
21 #define MS5607_CMD_CONV_P_256 0x40
22 #define MS5607_CMD_CONV_P_512 0x42
23 #define MS5607_CMD_CONV_P_1024 0x44
24 #define MS5607_CMD_CONV_P_2048 0x46
25 #define MS5607_CMD_CONV_P_4096 0x48
26 
27 #define MS5607_CMD_CONV_T_256 0x50
28 #define MS5607_CMD_CONV_T_512 0x52
29 #define MS5607_CMD_CONV_T_1024 0x54
30 #define MS5607_CMD_CONV_T_2048 0x56
31 #define MS5607_CMD_CONV_T_4096 0x58
32 
33 #define MS5607_CMD_CONV_READ_ADC 0x00
34 
35 #define MS5607_CMD_CONV_READ_SENSE_T1 0xA2
36 #define MS5607_CMD_CONV_READ_OFF_T1 0xA4
37 #define MS5607_CMD_CONV_READ_TCS 0xA6
38 #define MS5607_CMD_CONV_READ_TCO 0xA8
39 #define MS5607_CMD_CONV_READ_T_REF 0xAA
40 #define MS5607_CMD_CONV_READ_TEMPSENS 0xAC
41 #define MS5607_CMD_CONV_READ_CRC 0xAE
42 
43 #if defined(CONFIG_MS5607_PRES_OVER_256X)
44 	#define MS5607_PRES_OVER_DEFAULT 256
45 #elif defined(CONFIG_MS5607_PRES_OVER_512X)
46 	#define MS5607_PRES_OVER_DEFAULT 512
47 #elif defined(CONFIG_MS5607_PRES_OVER_1024X)
48 	#define MS5607_PRES_OVER_DEFAULT 1024
49 #elif defined(CONFIG_MS5607_PRES_OVER_2048X)
50 	#define MS5607_PRES_OVER_DEFAULT 2048
51 #elif defined(CONFIG_MS5607_PRES_OVER_4096X)
52 	#define MS5607_PRES_OVER_DEFAULT 4096
53 #else
54 	#define MS5607_PRES_OVER_DEFAULT 2048
55 #endif
56 
57 #if defined(CONFIG_MS5607_TEMP_OVER_256X)
58 	#define MS5607_TEMP_OVER_DEFAULT 256
59 #elif defined(CONFIG_MS5607_TEMP_OVER_512X)
60 	#define MS5607_TEMP_OVER_DEFAULT 512
61 #elif defined(CONFIG_MS5607_TEMP_OVER_1024X)
62 	#define MS5607_TEMP_OVER_DEFAULT 1024
63 #elif defined(CONFIG_MS5607_TEMP_OVER_2048X)
64 	#define MS5607_TEMP_OVER_DEFAULT 2048
65 #elif defined(CONFIG_MS5607_TEMP_OVER_4096X)
66 	#define MS5607_TEMP_OVER_DEFAULT 4096
67 #else
68 	#define MS5607_TEMP_OVER_DEFAULT 2048
69 #endif
70 
71 /* Forward declaration */
72 struct ms5607_config;
73 
74 struct ms5607_transfer_function {
75 	int (*bus_check)(const struct ms5607_config *cfg);
76 	int (*reset)(const struct ms5607_config *cfg);
77 	int (*read_prom)(const struct ms5607_config *cfg, uint8_t cmd, uint16_t *val);
78 	int (*start_conversion)(const struct ms5607_config *cfg, uint8_t cmd);
79 	int (*read_adc)(const struct ms5607_config *cfg, uint32_t *val);
80 };
81 
82 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
83 extern const struct ms5607_transfer_function ms5607_i2c_transfer_function;
84 #endif
85 
86 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
87 extern const struct ms5607_transfer_function ms5607_spi_transfer_function;
88 #endif
89 
90 struct ms5607_config {
91 	const struct ms5607_transfer_function *tf;
92 	union {
93 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
94 		struct i2c_dt_spec i2c;
95 #endif
96 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
97 		struct spi_dt_spec spi;
98 #endif
99 	} bus_cfg;
100 };
101 
102 struct ms5607_data {
103 	/* Calibration values */
104 	uint16_t sens_t1;
105 	uint16_t off_t1;
106 	uint16_t tcs;
107 	uint16_t tco;
108 	uint16_t t_ref;
109 	uint16_t tempsens;
110 
111 	/* Measured values */
112 	int32_t pressure;
113 	int32_t temperature;
114 
115 	/* conversion commands */
116 	uint8_t pressure_conv_cmd;
117 	uint8_t temperature_conv_cmd;
118 
119 	uint8_t pressure_conv_delay;
120 	uint8_t temperature_conv_delay;
121 };
122 
123 #endif /* __SENSOR_MS607_H__*/
124