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