1 /*
2  * Copyright (c) 2019 Manivannan Sadhasivam
3  * Copyright (c) 2020 Andreas Sandberg
4  * Copyright (c) 2021 Fabio Baltieri
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #ifndef ZEPHYR_DRIVERS_SX126X_COMMON_H_
10 #define ZEPHYR_DRIVERS_SX126X_COMMON_H_
11 
12 #include <zephyr/types.h>
13 #include <drivers/gpio.h>
14 #include <drivers/lora.h>
15 #include <drivers/spi.h>
16 
17 #include <sx126x/sx126x.h>
18 
19 #if DT_HAS_COMPAT_STATUS_OKAY(semtech_sx1261)
20 #define DT_DRV_COMPAT semtech_sx1261
21 #define SX126X_DEVICE_ID SX1261
22 #elif DT_HAS_COMPAT_STATUS_OKAY(semtech_sx1262)
23 #define DT_DRV_COMPAT semtech_sx1262
24 #define SX126X_DEVICE_ID SX1262
25 #elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32wl_subghz_radio)
26 #define DT_DRV_COMPAT st_stm32wl_subghz_radio
27 #define SX126X_DEVICE_ID SX1262
28 #else
29 #error No SX126x instance in device tree.
30 #endif
31 
32 #define HAVE_GPIO_CS		DT_INST_SPI_DEV_HAS_CS_GPIOS(0)
33 #define HAVE_GPIO_ANTENNA_ENABLE			\
34 	DT_INST_NODE_HAS_PROP(0, antenna_enable_gpios)
35 #define HAVE_GPIO_TX_ENABLE	DT_INST_NODE_HAS_PROP(0, tx_enable_gpios)
36 #define HAVE_GPIO_RX_ENABLE	DT_INST_NODE_HAS_PROP(0, rx_enable_gpios)
37 
38 #define GPIO_CS_LABEL		DT_INST_SPI_DEV_CS_GPIOS_LABEL(0)
39 #define GPIO_CS_PIN		DT_INST_SPI_DEV_CS_GPIOS_PIN(0)
40 #define GPIO_CS_FLAGS		DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0)
41 
42 #define GPIO_ANTENNA_ENABLE_PIN	DT_INST_GPIO_PIN(0, antenna_enable_gpios)
43 #define GPIO_TX_ENABLE_PIN	DT_INST_GPIO_PIN(0, tx_enable_gpios)
44 #define GPIO_RX_ENABLE_PIN	DT_INST_GPIO_PIN(0, rx_enable_gpios)
45 
46 struct sx126x_data {
47 	struct gpio_callback dio1_irq_callback;
48 	struct k_work dio1_irq_work;
49 	DioIrqHandler *radio_dio_irq;
50 #if HAVE_GPIO_ANTENNA_ENABLE
51 	const struct device *antenna_enable;
52 #endif
53 #if HAVE_GPIO_TX_ENABLE
54 	const struct device *tx_enable;
55 #endif
56 #if HAVE_GPIO_RX_ENABLE
57 	const struct device *rx_enable;
58 #endif
59 	const struct device *spi;
60 	struct spi_config spi_cfg;
61 #if HAVE_GPIO_CS
62 	struct spi_cs_control spi_cs;
63 #endif
64 	RadioOperatingModes_t mode;
65 };
66 
67 void sx126x_reset(struct sx126x_data *dev_data);
68 
69 bool sx126x_is_busy(struct sx126x_data *dev_data);
70 
71 uint32_t sx126x_get_dio1_pin_state(struct sx126x_data *dev_data);
72 
73 void sx126x_dio1_irq_enable(struct sx126x_data *dev_data);
74 
75 void sx126x_dio1_irq_disable(struct sx126x_data *dev_data);
76 
77 int sx126x_variant_init(const struct device *dev);
78 
79 #endif /* ZEPHYR_DRIVERS_SX126X_COMMON_H_ */
80