1 /*
2  * Copyright (c) 2022 Würth Elektronik eiSos GmbH & Co. KG
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_SENSOR_WSEN_HIDS_WSEN_HIDS_H_
8 #define ZEPHYR_DRIVERS_SENSOR_WSEN_HIDS_WSEN_HIDS_H_
9 
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/sensor.h>
12 
13 #include <weplatform.h>
14 
15 #include "WSEN_HIDS_2523020210001.h"
16 
17 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
18 #include <zephyr/drivers/spi.h>
19 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
20 
21 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
22 #include <zephyr/drivers/i2c.h>
23 #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
24 
25 struct hids_data {
26 	/* WE sensor interface configuration */
27 	WE_sensorInterface_t sensor_interface;
28 
29 	/* Last humidity sample */
30 	uint16_t humidity;
31 
32 	/* Last temperature sample */
33 	int16_t temperature;
34 
35 #ifdef CONFIG_WSEN_HIDS_TRIGGER
36 	const struct device *dev;
37 	struct gpio_callback data_ready_cb;
38 
39 	const struct sensor_trigger *data_ready_trigger;
40 	sensor_trigger_handler_t data_ready_handler;
41 
42 #if defined(CONFIG_WSEN_HIDS_TRIGGER_OWN_THREAD)
43 	K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_WSEN_HIDS_THREAD_STACK_SIZE);
44 	struct k_thread thread;
45 	struct k_sem drdy_sem;
46 #elif defined(CONFIG_WSEN_HIDS_TRIGGER_GLOBAL_THREAD)
47 	struct k_work work;
48 #endif
49 #endif /* CONFIG_WSEN_HIDS_TRIGGER */
50 };
51 
52 struct hids_config {
53 	union {
54 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
55 		const struct i2c_dt_spec i2c;
56 #endif
57 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
58 		const struct spi_dt_spec spi;
59 #endif
60 	} bus_cfg;
61 
62 	/* Output data rate */
63 	HIDS_outputDataRate_t odr;
64 
65 #ifdef CONFIG_WSEN_HIDS_TRIGGER
66 	/* Data-ready interrupt pin */
67 	const struct gpio_dt_spec gpio_drdy;
68 #endif /* CONFIG_WSEN_HIDS_TRIGGER */
69 };
70 
71 #ifdef CONFIG_WSEN_HIDS_TRIGGER
72 int hids_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
73 		     sensor_trigger_handler_t handler);
74 
75 int hids_init_interrupt(const struct device *dev);
76 #endif /* CONFIG_WSEN_HIDS_TRIGGER */
77 
78 int hids_spi_init(const struct device *dev);
79 int hids_i2c_init(const struct device *dev);
80 
81 #endif /* ZEPHYR_DRIVERS_SENSOR_WSEN_HIDS_WSEN_HIDS_H_ */
82