@@ -30,9 +30,14 @@ struct i2c_litex_litei2c_config {
3030 uint32_t bitrate ;
3131};
3232
33+ struct i2c_litex_litei2c_data {
34+ struct k_mutex mutex ;
35+ };
36+
3337static int i2c_litex_configure (const struct device * dev , uint32_t dev_config )
3438{
3539 const struct i2c_litex_litei2c_config * config = dev -> config ;
40+ struct i2c_litex_litei2c_data * data = dev -> data ;
3641
3742 if (I2C_ADDR_10_BITS & dev_config ) {
3843 return - ENOTSUP ;
@@ -42,6 +47,8 @@ static int i2c_litex_configure(const struct device *dev, uint32_t dev_config)
4247 return - ENOTSUP ;
4348 }
4449
50+ k_mutex_lock (& data -> mutex , K_FOREVER );
51+
4552 /* Setup speed to use */
4653 switch (I2C_SPEED_GET (dev_config )) {
4754 case I2C_SPEED_STANDARD :
@@ -54,9 +61,12 @@ static int i2c_litex_configure(const struct device *dev, uint32_t dev_config)
5461 litex_write8 (2 , config -> phy_speed_mode_addr );
5562 break ;
5663 default :
64+ k_mutex_unlock (& data -> mutex );
5765 return - ENOTSUP ;
5866 }
5967
68+ k_mutex_unlock (& data -> mutex );
69+
6070 return 0 ;
6171}
6272
@@ -99,6 +109,7 @@ static int i2c_litex_transfer(const struct device *dev, struct i2c_msg *msgs, ui
99109 uint16_t addr )
100110{
101111 const struct i2c_litex_litei2c_config * config = dev -> config ;
112+ struct i2c_litex_litei2c_data * data = dev -> data ;
102113 uint32_t len_tx_buf = 0 ;
103114 uint32_t len_rx_buf = 0 ;
104115 uint8_t len_tx = 0 ;
@@ -115,6 +126,8 @@ static int i2c_litex_transfer(const struct device *dev, struct i2c_msg *msgs, ui
115126
116127 int ret = 0 ;
117128
129+ k_mutex_lock (& data -> mutex , K_FOREVER );
130+
118131 litex_write8 (1 , config -> master_active_addr );
119132
120133 LOG_DBG ("addr: 0x%x" , addr );
@@ -256,12 +269,17 @@ static int i2c_litex_transfer(const struct device *dev, struct i2c_msg *msgs, ui
256269
257270 litex_write8 (0 , config -> master_active_addr );
258271
272+ k_mutex_unlock (& data -> mutex );
273+
259274 return ret ;
260275}
261276
262277static int i2c_litex_recover_bus (const struct device * dev )
263278{
264279 const struct i2c_litex_litei2c_config * config = dev -> config ;
280+ struct i2c_litex_litei2c_data * data = dev -> data ;
281+
282+ k_mutex_lock (& data -> mutex , K_FOREVER );
265283
266284 litex_write8 (1 , config -> master_active_addr );
267285
@@ -281,14 +299,19 @@ static int i2c_litex_recover_bus(const struct device *dev)
281299
282300 litex_write8 (0 , config -> master_active_addr );
283301
302+ k_mutex_unlock (& data -> mutex );
303+
284304 return 0 ;
285305}
286306
287307static int i2c_litex_init (const struct device * dev )
288308{
289309 const struct i2c_litex_litei2c_config * config = dev -> config ;
310+ struct i2c_litex_litei2c_data * data = dev -> data ;
290311 int ret ;
291312
313+ k_mutex_init (& data -> mutex );
314+
292315 ret = i2c_litex_configure (dev , I2C_MODE_CONTROLLER | i2c_map_dt_bitrate (config -> bitrate ));
293316 if (ret != 0 ) {
294317 LOG_ERR ("failed to configure I2C: %d" , ret );
@@ -310,6 +333,8 @@ static DEVICE_API(i2c, i2c_litex_litei2c_driver_api) = {
310333/* Device Instantiation */
311334
312335#define I2C_LITEX_INIT (n ) \
336+ static struct i2c_litex_litei2c_data i2c_litex_litei2c_data_##n; \
337+ \
313338 static const struct i2c_litex_litei2c_config i2c_litex_litei2c_config_##n = { \
314339 .phy_speed_mode_addr = DT_INST_REG_ADDR_BY_NAME(n, phy_speed_mode), \
315340 .master_active_addr = DT_INST_REG_ADDR_BY_NAME(n, master_active), \
@@ -320,7 +345,7 @@ static DEVICE_API(i2c, i2c_litex_litei2c_driver_api) = {
320345 .bitrate = DT_INST_PROP(n, clock_frequency), \
321346 }; \
322347 \
323- I2C_DEVICE_DT_INST_DEFINE(n, i2c_litex_init, NULL, NULL, \
348+ I2C_DEVICE_DT_INST_DEFINE(n, i2c_litex_init, NULL, &i2c_litex_litei2c_data_##n, \
324349 &i2c_litex_litei2c_config_##n, POST_KERNEL, \
325350 CONFIG_I2C_INIT_PRIORITY, &i2c_litex_litei2c_driver_api);
326351
0 commit comments