drivers: clock_control: nrf: Add API for synchronous request

Add API for synchronous request for clock attributes.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruściński 2024-12-11 09:51:59 +01:00 committed by Benjamin Cabé
commit fe0e2dbc60
2 changed files with 62 additions and 0 deletions

View file

@ -4,6 +4,7 @@
*/
#include "clock_control_nrf2_common.h"
#include <zephyr/drivers/clock_control/nrf_clock_control.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(clock_control_nrf2, CONFIG_CLOCK_CONTROL_LOG_LEVEL);
@ -24,6 +25,13 @@ LOG_MODULE_REGISTER(clock_control_nrf2, CONFIG_CLOCK_CONTROL_LOG_LEVEL);
*/
STRUCT_CLOCK_CONFIG(generic, ONOFF_CNT_MAX);
/* Structure used for synchronous clock request. */
struct sync_req {
struct onoff_client cli;
struct k_sem sem;
int res;
};
static void update_config(struct clock_config_generic *cfg)
{
atomic_val_t prev_flags = atomic_or(&cfg->flags, FLAG_UPDATE_NEEDED);
@ -159,3 +167,39 @@ int api_nosys_on_off(const struct device *dev, clock_control_subsys_t sys)
return -ENOSYS;
}
static void sync_cb(struct onoff_manager *mgr, struct onoff_client *cli, uint32_t state, int res)
{
struct sync_req *req = CONTAINER_OF(cli, struct sync_req, cli);
req->res = res;
k_sem_give(&req->sem);
}
int nrf_clock_control_request_sync(const struct device *dev,
const struct nrf_clock_spec *spec,
k_timeout_t timeout)
{
struct sync_req req = {
.sem = Z_SEM_INITIALIZER(req.sem, 0, 1)
};
int err;
if (k_is_in_isr()) {
return -EWOULDBLOCK;
}
sys_notify_init_callback(&req.cli.notify, sync_cb);
err = nrf_clock_control_request(dev, spec, &req.cli);
if (err < 0) {
return err;
}
err = k_sem_take(&req.sem, timeout);
if (err < 0) {
return err;
}
return req.res;
}

View file

@ -242,6 +242,24 @@ int nrf_clock_control_request(const struct device *dev,
return api->request(dev, spec, cli);
}
/**
* @brief Synchronously request a reservation to use a given clock with specified attributes.
*
* Function can only be called from thread context as it blocks until request is completed.
* @see nrf_clock_control_request().
*
* @param dev pointer to the clock device structure.
* @param spec See nrf_clock_control_request().
* @param timeout Request timeout.
*
* @retval 0 if request is fulfilled.
* @retval -EWOULDBLOCK if request is called from the interrupt context.
* @retval negative See error codes returned by nrf_clock_control_request().
*/
int nrf_clock_control_request_sync(const struct device *dev,
const struct nrf_clock_spec *spec,
k_timeout_t timeout);
/**
* @brief Release a reserved use of a clock.
*