1 /*
2  * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "freertos/FreeRTOS.h"
8 #include "esp_private/io_mux.h"
9 #include "hal/gpio_ll.h"
10 
11 static portMUX_TYPE s_io_mux_spinlock = portMUX_INITIALIZER_UNLOCKED;
12 static soc_module_clk_t s_io_mux_clk_src = 0; // by default, the clock source is not set explicitly by any consumer (e.g. SDM, Filter)
13 
io_mux_set_clock_source(soc_module_clk_t clk_src)14 esp_err_t io_mux_set_clock_source(soc_module_clk_t clk_src)
15 {
16     bool clk_conflict = false;
17     // check is the IO MUX has been set to another clock source
18     portENTER_CRITICAL(&s_io_mux_spinlock);
19     if (s_io_mux_clk_src != 0 && s_io_mux_clk_src != clk_src) {
20         clk_conflict = true;
21     } else {
22         s_io_mux_clk_src = clk_src;
23     }
24     portEXIT_CRITICAL(&s_io_mux_spinlock);
25 
26     if (clk_conflict) {
27         return ESP_ERR_INVALID_STATE;
28     }
29 
30     gpio_ll_iomux_set_clk_src(clk_src);
31     return ESP_OK;
32 }
33