1 /*
2  * Copyright (c) 2024, Croxel Inc
3  * Copyright (c) 2024, Embeint Inc
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_DRIVERS_I2C_I2C_NRFX_TWIM_COMMON_H_
9 #define ZEPHYR_DRIVERS_I2C_I2C_NRFX_TWIM_COMMON_H_
10 
11 #include <zephyr/device.h>
12 #include <zephyr/pm/device.h>
13 #include <nrfx_twim.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #define I2C_NRFX_TWIM_INVALID_FREQUENCY ((nrf_twim_frequency_t)-1)
20 #define I2C_NRFX_TWIM_FREQUENCY(bitrate)                                                           \
21 	(bitrate == I2C_BITRATE_STANDARD ? NRF_TWIM_FREQ_100K                                      \
22 	 : bitrate == 250000             ? NRF_TWIM_FREQ_250K                                      \
23 	 : bitrate == I2C_BITRATE_FAST                                                             \
24 		 ? NRF_TWIM_FREQ_400K                                                              \
25 		 : IF_ENABLED(NRF_TWIM_HAS_1000_KHZ_FREQ,                                          \
26 			      (bitrate == I2C_BITRATE_FAST_PLUS ? NRF_TWIM_FREQ_1000K :))          \
27 			   I2C_NRFX_TWIM_INVALID_FREQUENCY)
28 
29 #define I2C(idx)                DT_NODELABEL(i2c##idx)
30 #define I2C_HAS_PROP(idx, prop) DT_NODE_HAS_PROP(I2C(idx), prop)
31 #define I2C_FREQUENCY(idx)      I2C_NRFX_TWIM_FREQUENCY(DT_PROP_OR(I2C(idx), clock_frequency,      \
32 								   I2C_BITRATE_STANDARD))
33 
34 struct i2c_nrfx_twim_common_config {
35 	nrfx_twim_t twim;
36 	nrfx_twim_config_t twim_config;
37 	nrfx_twim_evt_handler_t event_handler;
38 	uint16_t msg_buf_size;
39 	void (*irq_connect)(void);
40 	const struct pinctrl_dev_config *pcfg;
41 	uint8_t *msg_buf;
42 	uint16_t max_transfer_size;
43 };
44 
45 int i2c_nrfx_twim_common_init(const struct device *dev);
46 int i2c_nrfx_twim_configure(const struct device *dev, uint32_t i2c_config);
47 int i2c_nrfx_twim_recover_bus(const struct device *dev);
48 int i2c_nrfx_twim_msg_transfer(const struct device *dev, uint8_t flags, uint8_t *buf,
49 			       size_t buf_len, uint16_t i2c_addr);
50 
51 #ifdef CONFIG_PM_DEVICE
52 int twim_nrfx_pm_action(const struct device *dev, enum pm_device_action action);
53 #endif
54 
55 #ifdef __cplusplus
56 }
57 #endif
58 
59 #endif /* ZEPHYR_DRIVERS_I2C_I2C_NRFX_TWIM_COMMON_H_ */
60