1 /*
2  * Copyright (c) 2024 Arrow Electronics.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_TMP1075_TMP1075_H_
8 #define ZEPHYR_DRIVERS_SENSOR_TMP1075_TMP1075_H_
9 
10 #include <zephyr/device.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/drivers/i2c.h>
14 
15 /* Extended resolution is not supported on TMP1075 */
16 #define TMP1075_DATA_NORMAL_SHIFT 4
17 #define TMP1075_DATA_FRAC_SHIFT   4
18 #define TMP1075_DATA_FRAC_MASK    0x00F0
19 #define TMP1075_DATA_INTE_SHIFT   8
20 #define TMP1075_DATA_INTE_MASK    0xFF00
21 #define uCELSIUS_IN_CELSIUS       1000000
22 
23 #define TMP1075_REG_TEMPERATURE 0x00
24 #define TMP1075_REG_CONFIG      0x01
25 #define TMP1075_REG_TLOW        0x02
26 #define TMP1075_REG_THIGH       0x03
27 
28 /* Scale in micro degrees Celsius -> 0.0625°C per ADC bit resolution */
29 #define TMP1075_TEMP_SCALE 62500
30 
31 /* Macro to set or clear the TMP1075_OS (One-shot conversion mode) bit based on a boolean value */
32 #define TMP1075_SET_ONE_SHOT_CONVERSION(reg, enable)                                               \
33 	((reg) = ((reg) & ~(1 << 15)) | ((enable) << 15))
34 
35 /* Macro to set the TMP1075_R (Conversion rate) bits */
36 #define TMP1075_SET_CONVERSION_RATE(reg, rate) ((reg) |= ((rate) << 13))
37 
38 /* Macro to set the TMP1075_F (Consecutive fault measurements) bits */
39 #define TMP1075_SET_CONSECUTIVE_FAULT_MEASUREMENTS(reg, faults) ((reg) |= ((faults) << 11))
40 
41 /* Macro to set or clear the TMP1075_POL (Polarity of output pin) bit based on a boolean value */
42 #define TMP1075_SET_ALERT_PIN_POLARITY(reg, activeHigh)                                            \
43 	((reg) = ((reg) & ~(1 << 10)) | ((activeHigh) << 10))
44 
45 /* Macro to set or clear the TMP1075_TM (ALERT pin function) bit based on a boolean value */
46 #define TMP1075_SET_ALERT_PIN_FUNCTION(reg, interruptMode)                                         \
47 	((reg) = ((reg) & ~(1 << 9)) | ((interruptMode) << 9))
48 
49 /* Macro to set or clear the TMP1075_SD (Shutdown mode) bit based on a boolean value */
50 #define TMP1075_SET_SHUTDOWN_MODE(reg, shutdown) ((reg) = ((reg) & ~(1 << 8)) | ((shutdown) << 8))
51 
52 struct tmp1075_data {
53 	const struct device *tmp1075_dev;
54 	int16_t sample;
55 	uint16_t config_reg;
56 	const struct sensor_trigger *temp_alert_trigger;
57 	sensor_trigger_handler_t temp_alert_handler;
58 	struct gpio_callback temp_alert_gpio_cb;
59 	bool over_threshold;
60 };
61 
62 struct tmp1075_config {
63 	const struct i2c_dt_spec bus;
64 	const struct gpio_dt_spec alert_gpio;
65 	uint8_t cr;
66 	uint8_t cf;
67 	bool alert_pol: 1;
68 	bool one_shot: 1;
69 	bool interrupt_mode: 1;
70 	bool shutdown_mode: 1;
71 };
72 
73 int tmp1075_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
74 			sensor_trigger_handler_t handler);
75 
76 void tmp1075_trigger_handle_alert(const struct device *port, struct gpio_callback *cb,
77 				  gpio_port_pins_t pins);
78 
79 #endif
80