Skip to content

Commit 92c1c55

Browse files
nordic-krchmstasiaknordic
authored andcommitted
[nrf fromlist] drivers: clock_control: nrf: Add API for synchronous request
Add API for synchronous request for clock attributes. Upstream PR #: 82133 Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 13689c1 commit 92c1c55

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

drivers/clock_control/clock_control_nrf2_common.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include "clock_control_nrf2_common.h"
7+
#include <zephyr/drivers/clock_control/nrf_clock_control.h>
78

89
#include <zephyr/logging/log.h>
910
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);
2425
*/
2526
STRUCT_CLOCK_CONFIG(generic, ONOFF_CNT_MAX);
2627

28+
/* Structure used for synchronous clock request. */
29+
struct sync_req {
30+
struct onoff_client cli;
31+
struct k_sem sem;
32+
int res;
33+
};
34+
2735
static void update_config(struct clock_config_generic *cfg)
2836
{
2937
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)
159167

160168
return -ENOSYS;
161169
}
170+
171+
static void sync_cb(struct onoff_manager *mgr, struct onoff_client *cli, uint32_t state, int res)
172+
{
173+
struct sync_req *req = CONTAINER_OF(cli, struct sync_req, cli);
174+
175+
req->res = res;
176+
k_sem_give(&req->sem);
177+
}
178+
179+
int nrf_clock_control_request_sync(const struct device *dev,
180+
const struct nrf_clock_spec *spec,
181+
k_timeout_t timeout)
182+
{
183+
struct sync_req req = {
184+
.sem = Z_SEM_INITIALIZER(req.sem, 0, 1)
185+
};
186+
int err;
187+
188+
if (k_is_in_isr()) {
189+
return -EWOULDBLOCK;
190+
}
191+
192+
sys_notify_init_callback(&req.cli.notify, sync_cb);
193+
194+
err = nrf_clock_control_request(dev, spec, &req.cli);
195+
if (err < 0) {
196+
return err;
197+
}
198+
199+
err = k_sem_take(&req.sem, timeout);
200+
if (err < 0) {
201+
return err;
202+
}
203+
204+
return req.res;
205+
}

include/zephyr/drivers/clock_control/nrf_clock_control.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ int nrf_clock_control_request(const struct device *dev,
235235
return api->request(dev, spec, cli);
236236
}
237237

238+
/**
239+
* @brief Synchronously request a reservation to use a given clock with specified attributes.
240+
*
241+
* @see nrf_clock_control_request().
242+
*
243+
* @param dev pointer to the clock device structure.
244+
* @param spec See nrf_clock_control_request().
245+
* @param timeout Request timeout.
246+
*
247+
* @retval 0 if request is fulfilled.
248+
* @retval -EWOULDBLOCK if request is called from the interrupt context.
249+
* @retval negative See error codes returned by nrf_clock_control_request().
250+
*/
251+
int nrf_clock_control_request_sync(const struct device *dev,
252+
const struct nrf_clock_spec *spec,
253+
k_timeout_t timeout);
254+
238255
/**
239256
* @brief Release a reserved use of a clock.
240257
*

0 commit comments

Comments
 (0)