Skip to content

Commit 37c23f6

Browse files
committed
input: analog_axis: use k_sem for locking
Swap the mutex with a semaphore for locking the calibration data. Lighter data structure, no need to use a mutex here. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent f98fd62 commit 37c23f6

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

drivers/input/input_analog_axis.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct analog_axis_config {
3838
};
3939

4040
struct analog_axis_data {
41-
struct k_mutex cal_lock;
41+
struct k_sem cal_lock;
4242
analog_axis_raw_data_t raw_data_cb;
4343
struct k_timer timer;
4444
struct k_thread thread;
@@ -66,9 +66,9 @@ int analog_axis_calibration_get(const struct device *dev,
6666
return -EINVAL;
6767
}
6868

69-
k_mutex_lock(&data->cal_lock, K_FOREVER);
69+
k_sem_take(&data->cal_lock, K_FOREVER);
7070
memcpy(out_cal, cal, sizeof(struct analog_axis_calibration));
71-
k_mutex_unlock(&data->cal_lock);
71+
k_sem_give(&data->cal_lock);
7272

7373
return 0;
7474
}
@@ -77,9 +77,9 @@ void analog_axis_set_raw_data_cb(const struct device *dev, analog_axis_raw_data_
7777
{
7878
struct analog_axis_data *data = dev->data;
7979

80-
k_mutex_lock(&data->cal_lock, K_FOREVER);
80+
k_sem_take(&data->cal_lock, K_FOREVER);
8181
data->raw_data_cb = cb;
82-
k_mutex_unlock(&data->cal_lock);
82+
k_sem_give(&data->cal_lock);
8383
}
8484

8585
int analog_axis_calibration_set(const struct device *dev,
@@ -94,9 +94,9 @@ int analog_axis_calibration_set(const struct device *dev,
9494
return -EINVAL;
9595
}
9696

97-
k_mutex_lock(&data->cal_lock, K_FOREVER);
97+
k_sem_take(&data->cal_lock, K_FOREVER);
9898
memcpy(cal, new_cal, sizeof(struct analog_axis_calibration));
99-
k_mutex_unlock(&data->cal_lock);
99+
k_sem_give(&data->cal_lock);
100100

101101
return 0;
102102
}
@@ -171,7 +171,7 @@ static void analog_axis_loop(const struct device *dev)
171171
return;
172172
}
173173

174-
k_mutex_lock(&data->cal_lock, K_FOREVER);
174+
k_sem_take(&data->cal_lock, K_FOREVER);
175175

176176
for (i = 0; i < cfg->num_channels; i++) {
177177
const struct analog_axis_channel_config *axis_cfg = &cfg->channel_cfg[i];
@@ -203,7 +203,7 @@ static void analog_axis_loop(const struct device *dev)
203203
axis_data->last_out = out;
204204
}
205205

206-
k_mutex_unlock(&data->cal_lock);
206+
k_sem_give(&data->cal_lock);
207207
}
208208

209209
static void analog_axis_thread(void *arg1, void *arg2, void *arg3)
@@ -244,7 +244,7 @@ static int analog_axis_init(const struct device *dev)
244244
struct analog_axis_data *data = dev->data;
245245
k_tid_t tid;
246246

247-
k_mutex_init(&data->cal_lock);
247+
k_sem_init(&data->cal_lock, 1, 1);
248248

249249
tid = k_thread_create(&data->thread, data->thread_stack,
250250
K_KERNEL_STACK_SIZEOF(data->thread_stack),

0 commit comments

Comments
 (0)