1
1
/*
2
2
* Copyright (c) 2016 Freescale Semiconductor, Inc.
3
- * Copyright (c) 2019 NXP
3
+ * Copyright (c) 2019, 2022 NXP
4
4
*
5
5
* SPDX-License-Identifier: Apache-2.0
6
6
*/
@@ -35,6 +35,7 @@ struct mcux_flexcomm_config {
35
35
struct mcux_flexcomm_data {
36
36
i2c_master_handle_t handle ;
37
37
struct k_sem device_sync_sem ;
38
+ struct k_sem lock ;
38
39
status_t callback_status ;
39
40
#ifdef CONFIG_I2C_TARGET
40
41
i2c_slave_handle_t target_handle ;
@@ -50,6 +51,7 @@ static int mcux_flexcomm_configure(const struct device *dev,
50
51
uint32_t dev_config_raw )
51
52
{
52
53
const struct mcux_flexcomm_config * config = dev -> config ;
54
+ struct mcux_flexcomm_data * data = dev -> data ;
53
55
I2C_Type * base = config -> base ;
54
56
uint32_t clock_freq ;
55
57
uint32_t baudrate ;
@@ -82,7 +84,9 @@ static int mcux_flexcomm_configure(const struct device *dev,
82
84
return - EINVAL ;
83
85
}
84
86
87
+ k_sem_take (& data -> lock , K_FOREVER );
85
88
I2C_MasterSetBaudRate (base , baudrate , clock_freq );
89
+ k_sem_give (& data -> lock );
86
90
87
91
return 0 ;
88
92
}
@@ -125,11 +129,15 @@ static int mcux_flexcomm_transfer(const struct device *dev,
125
129
I2C_Type * base = config -> base ;
126
130
i2c_master_transfer_t transfer ;
127
131
status_t status ;
132
+ int ret = 0 ;
133
+
134
+ k_sem_take (& data -> lock , K_FOREVER );
128
135
129
136
/* Iterate over all the messages */
130
137
for (int i = 0 ; i < num_msgs ; i ++ ) {
131
138
if (I2C_MSG_ADDR_10_BITS & msgs -> flags ) {
132
- return - ENOTSUP ;
139
+ ret = - ENOTSUP ;
140
+ break ;
133
141
}
134
142
135
143
/* Initialize the transfer descriptor */
@@ -159,7 +167,8 @@ static int mcux_flexcomm_transfer(const struct device *dev,
159
167
*/
160
168
if (status != kStatus_Success ) {
161
169
I2C_MasterTransferAbort (base , & data -> handle );
162
- return - EIO ;
170
+ ret = - EIO ;
171
+ break ;
163
172
}
164
173
165
174
/* Wait for the transfer to complete */
@@ -170,14 +179,17 @@ static int mcux_flexcomm_transfer(const struct device *dev,
170
179
*/
171
180
if (data -> callback_status != kStatus_Success ) {
172
181
I2C_MasterTransferAbort (base , & data -> handle );
173
- return - EIO ;
182
+ ret = - EIO ;
183
+ break ;
174
184
}
175
185
176
186
/* Move to the next message */
177
187
msgs ++ ;
178
188
}
179
189
180
- return 0 ;
190
+ k_sem_give (& data -> lock );
191
+
192
+ return ret ;
181
193
}
182
194
183
195
#if defined(CONFIG_I2C_TARGET )
@@ -334,6 +346,7 @@ static int mcux_flexcomm_init(const struct device *dev)
334
346
}
335
347
#endif
336
348
349
+ k_sem_init (& data -> lock , 1 , 1 );
337
350
k_sem_init (& data -> device_sync_sem , 0 , K_SEM_MAX_LIMIT );
338
351
339
352
if (!device_is_ready (config -> clock_dev )) {
0 commit comments