1 /*
2  * Copyright (c) 2016 BayLibre, SAS
3  * Copyright (c) 2017 Linaro Ltd
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  */
8 
9 #ifndef ZEPHYR_DRIVERS_I2C_I2C_LL_STM32_H_
10 #define ZEPHYR_DRIVERS_I2C_I2C_LL_STM32_H_
11 
12 typedef void (*irq_config_func_t)(const struct device *port);
13 
14 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_i2c_v2)
15 /**
16  * @brief structure to convey optional i2c timings settings
17  */
18 struct i2c_config_timing {
19 	/* i2c peripheral clock in Hz */
20 	uint32_t periph_clock;
21 	/* i2c bus speed in Hz */
22 	uint32_t i2c_speed;
23 	/* I2C_TIMINGR register value of i2c v2 peripheral */
24 	uint32_t timing_setting;
25 };
26 #endif
27 
28 struct i2c_stm32_config {
29 #ifdef CONFIG_I2C_STM32_INTERRUPT
30 	irq_config_func_t irq_config_func;
31 #endif
32 	struct stm32_pclken pclken;
33 	I2C_TypeDef *i2c;
34 	uint32_t bitrate;
35 	const struct soc_gpio_pinctrl *pinctrl_list;
36 	size_t pinctrl_list_size;
37 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_i2c_v2)
38 	const struct i2c_config_timing *timings;
39 	size_t n_timings;
40 #endif
41 };
42 
43 struct i2c_stm32_data {
44 #ifdef CONFIG_I2C_STM32_INTERRUPT
45 	struct k_sem device_sync_sem;
46 #endif
47 	struct k_sem bus_mutex;
48 	uint32_t dev_config;
49 #ifdef CONFIG_I2C_STM32_V1
50 	uint16_t slave_address;
51 #endif
52 	struct {
53 #ifdef CONFIG_I2C_STM32_V1
54 		unsigned int is_restart;
55 		unsigned int flags;
56 #endif
57 		unsigned int is_write;
58 		unsigned int is_arlo;
59 		unsigned int is_nack;
60 		unsigned int is_err;
61 		struct i2c_msg *msg;
62 		unsigned int len;
63 		uint8_t *buf;
64 	} current;
65 #ifdef CONFIG_I2C_SLAVE
66 	bool master_active;
67 	struct i2c_slave_config *slave_cfg;
68 	bool slave_attached;
69 #endif
70 };
71 
72 int32_t stm32_i2c_msg_write(const struct device *dev, struct i2c_msg *msg,
73 			    uint8_t *flg,
74 			    uint16_t sadr);
75 int32_t stm32_i2c_msg_read(const struct device *dev, struct i2c_msg *msg,
76 			   uint8_t *flg,
77 			   uint16_t sadr);
78 int32_t stm32_i2c_configure_timing(const struct device *dev, uint32_t clk);
79 int i2c_stm32_runtime_configure(const struct device *dev, uint32_t config);
80 
81 void stm32_i2c_event_isr(void *arg);
82 void stm32_i2c_error_isr(void *arg);
83 #ifdef CONFIG_I2C_STM32_COMBINED_INTERRUPT
84 void stm32_i2c_combined_isr(void *arg);
85 #endif
86 
87 #ifdef CONFIG_I2C_SLAVE
88 int i2c_stm32_slave_register(const struct device *dev,
89 			     struct i2c_slave_config *config);
90 int i2c_stm32_slave_unregister(const struct device *dev,
91 			       struct i2c_slave_config *config);
92 #endif
93 
94 #define DEV_DATA(dev) ((struct i2c_stm32_data * const)(dev)->data)
95 #define DEV_CFG(dev)	\
96 ((const struct i2c_stm32_config * const)(dev)->config)
97 
98 #endif	/* ZEPHYR_DRIVERS_I2C_I2C_LL_STM32_H_ */
99