1 /*
2  * Copyright (c) 2018, Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include <zephyr/drivers/i2c.h>
9 #include <zephyr/dt-bindings/i2c/i2c.h>
10 #include <zephyr/pm/device.h>
11 #include <zephyr/drivers/pinctrl.h>
12 #include <soc.h>
13 #include <nrfx_twi.h>
14 #include "i2c_nrfx_twi_common.h"
15 
16 #include <zephyr/logging/log.h>
17 #include <zephyr/irq.h>
18 LOG_MODULE_REGISTER(i2c_nrfx_twi, CONFIG_I2C_LOG_LEVEL);
19 
20 #if CONFIG_I2C_NRFX_TRANSFER_TIMEOUT
21 #define I2C_TRANSFER_TIMEOUT_MSEC K_MSEC(CONFIG_I2C_NRFX_TRANSFER_TIMEOUT)
22 #else
23 #define I2C_TRANSFER_TIMEOUT_MSEC K_FOREVER
24 #endif
25 
26 struct i2c_nrfx_twi_data {
27 	uint32_t dev_config;
28 	struct k_sem transfer_sync;
29 	struct k_sem completion_sync;
30 	volatile nrfx_err_t res;
31 };
32 
33 /* Enforce dev_config matches the same offset as the common structure,
34  * otherwise common API won't be compatible with i2c_nrfx_twi.
35  */
36 BUILD_ASSERT(
37 	offsetof(struct i2c_nrfx_twi_data, dev_config) ==
38 	offsetof(struct i2c_nrfx_twi_common_data, dev_config)
39 );
40 
i2c_nrfx_twi_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)41 static int i2c_nrfx_twi_transfer(const struct device *dev,
42 				 struct i2c_msg *msgs,
43 				 uint8_t num_msgs, uint16_t addr)
44 {
45 	const struct i2c_nrfx_twi_config *config = dev->config;
46 	struct i2c_nrfx_twi_data *data = dev->data;
47 	int ret = 0;
48 
49 	k_sem_take(&data->transfer_sync, K_FOREVER);
50 
51 	/* Dummy take on completion_sync sem to be sure that it is empty */
52 	k_sem_take(&data->completion_sync, K_NO_WAIT);
53 
54 	nrfx_twi_enable(&config->twi);
55 
56 	for (size_t i = 0; i < num_msgs; i++) {
57 		bool more_msgs = ((i < (num_msgs - 1)) &&
58 				  !(msgs[i + 1].flags & I2C_MSG_RESTART));
59 
60 		ret = i2c_nrfx_twi_msg_transfer(dev, msgs[i].flags,
61 						msgs[i].buf,
62 						msgs[i].len, addr,
63 						more_msgs);
64 		if (ret) {
65 			break;
66 		}
67 
68 		ret = k_sem_take(&data->completion_sync,
69 				 I2C_TRANSFER_TIMEOUT_MSEC);
70 		if (ret != 0) {
71 			/* Whatever the frequency, completion_sync should have
72 			 * been given by the event handler.
73 			 *
74 			 * If it hasn't, it's probably due to an hardware issue
75 			 * on the I2C line, for example a short between SDA and
76 			 * GND.
77 			 * This is issue has also been when trying to use the
78 			 * I2C bus during MCU internal flash erase.
79 			 *
80 			 * In many situation, a retry is sufficient.
81 			 * However, some time the I2C device get stuck and need
82 			 * help to recover.
83 			 * Therefore we always call i2c_nrfx_twi_recover_bus()
84 			 * to make sure everything has been done to restore the
85 			 * bus from this error.
86 			 */
87 			nrfx_twi_disable(&config->twi);
88 			(void)i2c_nrfx_twi_recover_bus(dev);
89 			ret = -EIO;
90 			break;
91 		}
92 
93 		if (data->res != NRFX_SUCCESS) {
94 			ret = -EIO;
95 			break;
96 		}
97 	}
98 
99 	nrfx_twi_disable(&config->twi);
100 	k_sem_give(&data->transfer_sync);
101 
102 	return ret;
103 }
104 
event_handler(nrfx_twi_evt_t const * p_event,void * p_context)105 static void event_handler(nrfx_twi_evt_t const *p_event, void *p_context)
106 {
107 	const struct device *dev = p_context;
108 	struct i2c_nrfx_twi_data *dev_data = (struct i2c_nrfx_twi_data *)dev->data;
109 
110 	switch (p_event->type) {
111 	case NRFX_TWI_EVT_DONE:
112 		dev_data->res = NRFX_SUCCESS;
113 		break;
114 	case NRFX_TWI_EVT_ADDRESS_NACK:
115 		dev_data->res = NRFX_ERROR_DRV_TWI_ERR_ANACK;
116 		break;
117 	case NRFX_TWI_EVT_DATA_NACK:
118 		dev_data->res = NRFX_ERROR_DRV_TWI_ERR_DNACK;
119 		break;
120 	default:
121 		dev_data->res = NRFX_ERROR_INTERNAL;
122 		break;
123 	}
124 
125 	k_sem_give(&dev_data->completion_sync);
126 }
127 
128 static DEVICE_API(i2c, i2c_nrfx_twi_driver_api) = {
129 	.configure   = i2c_nrfx_twi_configure,
130 	.transfer    = i2c_nrfx_twi_transfer,
131 	.recover_bus = i2c_nrfx_twi_recover_bus,
132 };
133 
134 #define I2C_NRFX_TWI_DEVICE(idx)					       \
135 	NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx));			       \
136 	BUILD_ASSERT(I2C_FREQUENCY(idx)	!=				       \
137 		     I2C_NRFX_TWI_INVALID_FREQUENCY,			       \
138 		     "Wrong I2C " #idx " frequency setting in dts");	       \
139 	static int twi_##idx##_init(const struct device *dev)		       \
140 	{								       \
141 		IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority),     \
142 			    nrfx_isr, nrfx_twi_##idx##_irq_handler, 0);	       \
143 		const struct i2c_nrfx_twi_config *config = dev->config;	       \
144 		int err = pinctrl_apply_state(config->pcfg,		       \
145 					      PINCTRL_STATE_DEFAULT);	       \
146 		if (err < 0) {						       \
147 			return err;					       \
148 		}							       \
149 		return i2c_nrfx_twi_init(dev);				       \
150 	}								       \
151 	static struct i2c_nrfx_twi_data twi_##idx##_data = {		       \
152 		.transfer_sync = Z_SEM_INITIALIZER(                            \
153 			twi_##idx##_data.transfer_sync, 1, 1),                 \
154 		.completion_sync = Z_SEM_INITIALIZER(                          \
155 			twi_##idx##_data.completion_sync, 0, 1)		       \
156 	};								       \
157 	PINCTRL_DT_DEFINE(I2C(idx));					       \
158 	static const struct i2c_nrfx_twi_config twi_##idx##z_config = {	       \
159 		.twi = NRFX_TWI_INSTANCE(idx),				       \
160 		.config = {						       \
161 			.skip_gpio_cfg = true,				       \
162 			.skip_psel_cfg = true,				       \
163 			.frequency = I2C_FREQUENCY(idx),		       \
164 		},							       \
165 		.event_handler = event_handler,				       \
166 		.pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)),		       \
167 	};								       \
168 	PM_DEVICE_DT_DEFINE(I2C(idx), twi_nrfx_pm_action);		       \
169 	I2C_DEVICE_DT_DEFINE(I2C(idx),					       \
170 		      twi_##idx##_init,					       \
171 		      PM_DEVICE_DT_GET(I2C(idx)),			       \
172 		      &twi_##idx##_data,				       \
173 		      &twi_##idx##z_config,				       \
174 		      POST_KERNEL,					       \
175 		      CONFIG_I2C_INIT_PRIORITY,				       \
176 		      &i2c_nrfx_twi_driver_api)
177 
178 #ifdef CONFIG_HAS_HW_NRF_TWI0
179 I2C_NRFX_TWI_DEVICE(0);
180 #endif
181 
182 #ifdef CONFIG_HAS_HW_NRF_TWI1
183 I2C_NRFX_TWI_DEVICE(1);
184 #endif
185