Skip to content

Commit

Permalink
ITE: drivers/i2c: it8xxx2: add mutex lock
Browse files Browse the repository at this point in the history
Add mutex lock to prevent race conditions.

Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com>
  • Loading branch information
GTLin08 authored and nashif committed Jul 3, 2021
1 parent fe505d3 commit 91a6b28
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions drivers/i2c/i2c_ite_it8xxx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum i2c_ch_status {
struct i2c_it8xxx2_data {
enum i2c_ch_status i2ccs;
struct i2c_msg *msgs;
struct k_mutex mutex;
struct k_sem device_sync_sem;
/* Index into output data */
size_t widx;
Expand Down Expand Up @@ -732,6 +733,8 @@ static int i2c_it8xxx2_transfer(const struct device *dev, struct i2c_msg *msgs,
return -EINVAL;
}

/* Lock mutex of i2c controller */
k_mutex_lock(&data->mutex, K_FOREVER);
/*
* If the transaction of write to read is divided into two
* transfers, the repeat start transfer uses this flag to
Expand All @@ -749,6 +752,8 @@ static int i2c_it8xxx2_transfer(const struct device *dev, struct i2c_msg *msgs,
* (No external pull-up), drop the transaction.
*/
if (i2c_bus_not_available(dev)) {
/* Unlock mutex of i2c controller */
k_mutex_unlock(&data->mutex);
return -EIO;
}
}
Expand Down Expand Up @@ -796,6 +801,8 @@ static int i2c_it8xxx2_transfer(const struct device *dev, struct i2c_msg *msgs,
if (data->err || (msgs->flags & I2C_MSG_STOP)) {
data->i2ccs = I2C_CH_NORMAL;
}
/* Unlock mutex of i2c controller */
k_mutex_unlock(&data->mutex);

return data->err;
}
Expand All @@ -821,6 +828,8 @@ static int i2c_it8xxx2_init(const struct device *dev)
uint32_t bitrate_cfg, offset = 0;
int error;

/* Initialize mutex and semaphore */
k_mutex_init(&data->mutex);
k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT);

switch ((uint32_t)base) {
Expand Down

0 comments on commit 91a6b28

Please sign in to comment.