1 /*
2  * Copyright (c) 2019 Centaur Analytics, Inc
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_TMP11X_TMP11X_H_
8 #define ZEPHYR_DRIVERS_SENSOR_TMP11X_TMP11X_H_
9 
10 #include <zephyr/sys/util_macro.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/drivers/i2c.h>
13 
14 #define TMP11X_REG_TEMP        0x0
15 #define TMP11X_REG_CFGR        0x1
16 #define TMP11X_REG_HIGH_LIM    0x2
17 #define TMP11X_REG_LOW_LIM     0x3
18 #define TMP11X_REG_EEPROM_UL   0x4
19 #define TMP11X_REG_EEPROM1     0x5
20 #define TMP11X_REG_EEPROM2     0x6
21 #define TMP11X_REG_EEPROM3     0x7
22 #define TMP117_REG_TEMP_OFFSET 0x7
23 #define TMP11X_REG_EEPROM4     0x8
24 #define TMP11X_REG_DEVICE_ID   0xF
25 
26 #define TMP11X_RESOLUTION     78125 /* in tens of uCelsius*/
27 #define TMP11X_RESOLUTION_DIV 10000000
28 
29 #define TMP116_DEVICE_ID 0x1116
30 #define TMP117_DEVICE_ID 0x0117
31 #define TMP119_DEVICE_ID 0x2117
32 
33 #define TMP11X_CFGR_RESET       BIT(1)
34 #define TMP11X_CFGR_AVG         (BIT(5) | BIT(6))
35 #define TMP11X_CFGR_CONV        (BIT(7) | BIT(8) | BIT(9))
36 #define TMP11X_CFGR_MODE        (BIT(10) | BIT(11))
37 #define TMP11X_CFGR_DATA_READY  BIT(13)
38 #define TMP11X_EEPROM_UL_UNLOCK BIT(15)
39 #define TMP11X_EEPROM_UL_BUSY   BIT(14)
40 
41 /* Alert pin configuration bits */
42 #define TMP11X_CFGR_ALERT_DR_SEL  BIT(2) /* ALERT pin select (1=Data ready) (0=alert) */
43 #define TMP11X_CFGR_ALERT_PIN_POL BIT(3) /* Alert pin polarity */
44 #define TMP11X_CFGR_ALERT_MODE    BIT(4) /* Alert pin mode (1=therm, 0=alert) */
45 
46 #define TMP11X_AVG_1_SAMPLE    0
47 #define TMP11X_AVG_8_SAMPLES   BIT(5)
48 #define TMP11X_AVG_32_SAMPLES  BIT(6)
49 #define TMP11X_AVG_64_SAMPLES  (BIT(5) | BIT(6))
50 #define TMP11X_MODE_CONTINUOUS 0
51 #define TMP11X_MODE_SHUTDOWN   BIT(10)
52 #define TMP11X_MODE_ONE_SHOT   (BIT(10) | BIT(11))
53 
54 struct tmp11x_data {
55 	uint16_t sample;
56 	uint16_t id;
57 #ifdef CONFIG_TMP11X_TRIGGER
58 	const struct device *dev;
59 	struct gpio_callback alert_cb;
60 	sensor_trigger_handler_t alert_handler;
61 	const struct sensor_trigger *alert_trigger;
62 
63 #if defined(CONFIG_TMP11X_TRIGGER_OWN_THREAD)
64 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_TMP11X_THREAD_STACK_SIZE);
65 	struct k_thread thread;
66 	struct k_sem gpio_sem;
67 #elif defined(CONFIG_TMP11X_TRIGGER_GLOBAL_THREAD)
68 	struct k_work work;
69 #endif /* CONFIG_TMP11X_TRIGGER_OWN_THREAD */
70 #endif /* CONFIG_TMP11X_TRIGGER */
71 };
72 
73 struct tmp11x_dev_config {
74 	struct i2c_dt_spec bus;
75 	uint16_t odr;
76 	uint16_t oversampling;
77 	bool alert_pin_polarity;
78 	bool alert_mode;
79 	bool alert_dr_sel;
80 	bool store_attr_values;
81 #ifdef CONFIG_TMP11X_TRIGGER
82 	struct gpio_dt_spec alert_gpio;
83 #endif
84 };
85 
86 /* Function declarations */
87 int tmp11x_write_config(const struct device *dev, uint16_t mask, uint16_t conf);
88 int tmp11x_reg_read(const struct device *dev, uint8_t reg, uint16_t *val);
89 
90 #ifdef CONFIG_TMP11X_TRIGGER
91 int tmp11x_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
92 		       sensor_trigger_handler_t handler);
93 int tmp11x_init_interrupt(const struct device *dev);
94 #endif
95 
96 #endif /*  ZEPHYR_DRIVERS_SENSOR_TMP11X_TMP11X_H_ */
97