|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2025 Nordic Semiconductor ASA |
| 3 | + * Copyright (c) 2016 Vinayak Kariappa Chettimada |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <soc.h> |
| 9 | +#include <zephyr/sys/onoff.h> |
| 10 | +#include <zephyr/drivers/clock_control.h> |
| 11 | +#include <zephyr/drivers/clock_control/nrf_clock_control.h> |
| 12 | +#include "clock_control_nrf_common.h" |
| 13 | +#include <nrfx_clock_xo24m.h> |
| 14 | +#include <zephyr/logging/log.h> |
| 15 | +#include <zephyr/shell/shell.h> |
| 16 | +#include <zephyr/irq.h> |
| 17 | + |
| 18 | +LOG_MODULE_REGISTER(clock_control_xo24m, CONFIG_CLOCK_CONTROL_LOG_LEVEL); |
| 19 | + |
| 20 | +#define DT_DRV_COMPAT nordic_nrf_clock_xo24m |
| 21 | + |
| 22 | +#define CLOCK_DEVICE_XO24M DEVICE_DT_GET(DT_NODELABEL(xo24m)) |
| 23 | + |
| 24 | +#define CTX_ONOFF BIT(6) |
| 25 | +#define CTX_API BIT(7) |
| 26 | +#define CTX_MASK (CTX_ONOFF | CTX_API) |
| 27 | + |
| 28 | +#define STATUS_MASK 0x7 |
| 29 | +#define GET_STATUS(flags) (flags & STATUS_MASK) |
| 30 | +#define GET_CTX(flags) (flags & CTX_MASK) |
| 31 | + |
| 32 | +/* Helper logging macros which prepends subsys name to the log. */ |
| 33 | +#ifdef CONFIG_LOG |
| 34 | +#define CLOCK_LOG(lvl, dev, ...) \ |
| 35 | + LOG_##lvl("%s: " GET_ARG_N(1, __VA_ARGS__), \ |
| 36 | + "xo24m" \ |
| 37 | + COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__),\ |
| 38 | + (), (, GET_ARGS_LESS_N(1, __VA_ARGS__)))) |
| 39 | +#else |
| 40 | +#define CLOCK_LOG(...) |
| 41 | +#endif |
| 42 | + |
| 43 | +#define ERR(dev, ...) CLOCK_LOG(ERR, dev, __VA_ARGS__) |
| 44 | +#define WRN(dev, ...) CLOCK_LOG(WRN, dev, __VA_ARGS__) |
| 45 | +#define INF(dev, ...) CLOCK_LOG(INF, dev, __VA_ARGS__) |
| 46 | +#define DBG(dev, ...) CLOCK_LOG(DBG, dev, __VA_ARGS__) |
| 47 | + |
| 48 | +typedef void (*clk_ctrl_func_t)(void); |
| 49 | + |
| 50 | +typedef struct { |
| 51 | + struct onoff_manager mgr; |
| 52 | + clock_control_cb_t cb; |
| 53 | + void *user_data; |
| 54 | + uint32_t flags; |
| 55 | +} xo24m_data_t; |
| 56 | + |
| 57 | +typedef struct { |
| 58 | + clk_ctrl_func_t start; /* Clock start function */ |
| 59 | + clk_ctrl_func_t stop; /* Clock stop function */ |
| 60 | +} xo24m_config_t; |
| 61 | + |
| 62 | +static int set_off_state(uint32_t *flags, uint32_t ctx) |
| 63 | +{ |
| 64 | + int err = 0; |
| 65 | + unsigned int key = irq_lock(); |
| 66 | + uint32_t current_ctx = GET_CTX(*flags); |
| 67 | + |
| 68 | + if ((current_ctx != 0) && (current_ctx != ctx)) { |
| 69 | + err = -EPERM; |
| 70 | + } else { |
| 71 | + *flags = CLOCK_CONTROL_STATUS_OFF; |
| 72 | + } |
| 73 | + |
| 74 | + irq_unlock(key); |
| 75 | + |
| 76 | + return err; |
| 77 | +} |
| 78 | + |
| 79 | +static int set_starting_state(uint32_t *flags, uint32_t ctx) |
| 80 | +{ |
| 81 | + int err = 0; |
| 82 | + unsigned int key = irq_lock(); |
| 83 | + uint32_t current_ctx = GET_CTX(*flags); |
| 84 | + |
| 85 | + if ((*flags & (STATUS_MASK)) == CLOCK_CONTROL_STATUS_OFF) { |
| 86 | + *flags = CLOCK_CONTROL_STATUS_STARTING | ctx; |
| 87 | + } else if (current_ctx != ctx) { |
| 88 | + err = -EPERM; |
| 89 | + } else { |
| 90 | + err = -EALREADY; |
| 91 | + } |
| 92 | + |
| 93 | + irq_unlock(key); |
| 94 | + |
| 95 | + return err; |
| 96 | +} |
| 97 | + |
| 98 | +static void set_on_state(uint32_t *flags) |
| 99 | +{ |
| 100 | + unsigned int key = irq_lock(); |
| 101 | + |
| 102 | + *flags = CLOCK_CONTROL_STATUS_ON | GET_CTX(*flags); |
| 103 | + irq_unlock(key); |
| 104 | +} |
| 105 | + |
| 106 | +static void clkstarted_handle(const struct device *dev) |
| 107 | +{ |
| 108 | + clock_control_cb_t callback = ((xo24m_data_t *)dev->data)->cb; |
| 109 | + void *user_data = ((xo24m_data_t *)dev->data)->user_data; |
| 110 | + |
| 111 | + ((xo24m_data_t *)dev->data)->cb = NULL; |
| 112 | + set_on_state(&((xo24m_data_t *)dev->data)->flags); |
| 113 | + DBG(dev, "Clock started"); |
| 114 | + |
| 115 | + if (callback) { |
| 116 | + callback(dev, NULL, user_data); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +static void xo24m_start(void) |
| 121 | +{ |
| 122 | + nrfx_clock_xo24m_start(); |
| 123 | +} |
| 124 | + |
| 125 | +static void xo24m_stop(void) |
| 126 | +{ |
| 127 | + nrfx_clock_xo24m_stop(); |
| 128 | +} |
| 129 | + |
| 130 | +static int stop(const struct device *dev, uint32_t ctx) |
| 131 | +{ |
| 132 | + int err; |
| 133 | + |
| 134 | + err = set_off_state(&((xo24m_data_t *)dev->data)->flags, ctx); |
| 135 | + if (err < 0) { |
| 136 | + return err; |
| 137 | + } |
| 138 | + |
| 139 | + ((xo24m_config_t *)dev->config)->stop(); |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
| 144 | +static int async_start(const struct device *dev, clock_control_cb_t cb, void *user_data, |
| 145 | + uint32_t ctx) |
| 146 | +{ |
| 147 | + int err; |
| 148 | + |
| 149 | + err = set_starting_state(&((xo24m_data_t *)dev->data)->flags, ctx); |
| 150 | + if (err < 0) { |
| 151 | + return err; |
| 152 | + } |
| 153 | + |
| 154 | + ((xo24m_data_t *)dev->data)->cb = cb; |
| 155 | + ((xo24m_data_t *)dev->data)->user_data = user_data; |
| 156 | + |
| 157 | + ((xo24m_config_t *)dev->config)->start(); |
| 158 | + |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +static void blocking_start_callback(const struct device *dev, |
| 163 | + clock_control_subsys_t subsys, |
| 164 | + void *user_data) |
| 165 | +{ |
| 166 | + struct k_sem *sem = user_data; |
| 167 | + |
| 168 | + k_sem_give(sem); |
| 169 | +} |
| 170 | + |
| 171 | +static void onoff_stop(struct onoff_manager *mgr, |
| 172 | + onoff_notify_fn notify) |
| 173 | +{ |
| 174 | + int res; |
| 175 | + |
| 176 | + res = stop(CLOCK_DEVICE_XO24M, CTX_ONOFF); |
| 177 | + notify(mgr, res); |
| 178 | +} |
| 179 | + |
| 180 | +static void onoff_started_callback(const struct device *dev, |
| 181 | + clock_control_subsys_t sys, |
| 182 | + void *user_data) |
| 183 | +{ |
| 184 | + ARG_UNUSED(sys); |
| 185 | + |
| 186 | + onoff_notify_fn notify = user_data; |
| 187 | + |
| 188 | + notify(&((xo24m_data_t *)dev->data)->mgr, 0); |
| 189 | +} |
| 190 | + |
| 191 | +static void onoff_start(struct onoff_manager *mgr, |
| 192 | + onoff_notify_fn notify) |
| 193 | +{ |
| 194 | + int err; |
| 195 | + |
| 196 | + err = async_start(CLOCK_DEVICE_XO24M, onoff_started_callback, notify, CTX_ONOFF); |
| 197 | + if (err < 0) { |
| 198 | + notify(mgr, err); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +static void clock_event_handler(void) |
| 203 | +{ |
| 204 | + const struct device *dev = CLOCK_DEVICE_XO24M; |
| 205 | + |
| 206 | + clkstarted_handle(dev); |
| 207 | +} |
| 208 | + |
| 209 | +static int api_start(const struct device *dev, clock_control_subsys_t subsys, |
| 210 | + clock_control_cb_t cb, void *user_data) |
| 211 | +{ |
| 212 | + ARG_UNUSED(subsys); |
| 213 | + |
| 214 | + return async_start(dev, cb, user_data, CTX_API); |
| 215 | +} |
| 216 | + |
| 217 | +static int api_blocking_start(const struct device *dev, |
| 218 | + clock_control_subsys_t subsys) |
| 219 | +{ |
| 220 | + struct k_sem sem = Z_SEM_INITIALIZER(sem, 0, 1); |
| 221 | + int err; |
| 222 | + |
| 223 | + if (!IS_ENABLED(CONFIG_MULTITHREADING)) { |
| 224 | + return -ENOTSUP; |
| 225 | + } |
| 226 | + |
| 227 | + err = api_start(dev, subsys, blocking_start_callback, &sem); |
| 228 | + if (err < 0) { |
| 229 | + return err; |
| 230 | + } |
| 231 | + |
| 232 | + return k_sem_take(&sem, K_MSEC(500)); |
| 233 | +} |
| 234 | + |
| 235 | +static int api_stop(const struct device *dev, clock_control_subsys_t subsys) |
| 236 | +{ |
| 237 | + ARG_UNUSED(subsys); |
| 238 | + |
| 239 | + return stop(dev, CTX_API); |
| 240 | +} |
| 241 | + |
| 242 | +static enum clock_control_status api_get_status(const struct device *dev, |
| 243 | + clock_control_subsys_t subsys) |
| 244 | +{ |
| 245 | + ARG_UNUSED(subsys); |
| 246 | + |
| 247 | + return GET_STATUS(((xo24m_data_t *)dev->data)->flags); |
| 248 | +} |
| 249 | + |
| 250 | +static int api_request(const struct device *dev, |
| 251 | + const struct nrf_clock_spec *spec, |
| 252 | + struct onoff_client *cli) |
| 253 | +{ |
| 254 | + xo24m_data_t *dev_data = dev->data; |
| 255 | + |
| 256 | + ARG_UNUSED(spec); |
| 257 | + |
| 258 | + return onoff_request(&dev_data->mgr, cli); |
| 259 | +} |
| 260 | + |
| 261 | +static int api_release(const struct device *dev, |
| 262 | + const struct nrf_clock_spec *spec) |
| 263 | +{ |
| 264 | + xo24m_data_t *dev_data = dev->data; |
| 265 | + |
| 266 | + ARG_UNUSED(spec); |
| 267 | + |
| 268 | + return onoff_release(&dev_data->mgr); |
| 269 | +} |
| 270 | + |
| 271 | +static int api_cancel_or_release(const struct device *dev, |
| 272 | + const struct nrf_clock_spec *spec, |
| 273 | + struct onoff_client *cli) |
| 274 | +{ |
| 275 | + xo24m_data_t *dev_data = dev->data; |
| 276 | + |
| 277 | + ARG_UNUSED(spec); |
| 278 | + |
| 279 | + return onoff_cancel_or_release(&dev_data->mgr, cli); |
| 280 | +} |
| 281 | + |
| 282 | +static int clk_init(const struct device *dev) |
| 283 | +{ |
| 284 | + int err; |
| 285 | + static const struct onoff_transitions transitions = { |
| 286 | + .start = onoff_start, |
| 287 | + .stop = onoff_stop |
| 288 | + }; |
| 289 | + |
| 290 | + clock_control_nrf_common_connect_irq(); |
| 291 | + |
| 292 | + if (nrfx_clock_xo24m_init(clock_event_handler) != 0) { |
| 293 | + return -EIO; |
| 294 | + } |
| 295 | + |
| 296 | + err = onoff_manager_init(&((xo24m_data_t *)dev->data)->mgr, |
| 297 | + &transitions); |
| 298 | + if (err < 0) { |
| 299 | + return err; |
| 300 | + } |
| 301 | + |
| 302 | + ((xo24m_data_t *)dev->data)->flags = CLOCK_CONTROL_STATUS_OFF; |
| 303 | + |
| 304 | + return 0; |
| 305 | +} |
| 306 | + |
| 307 | +CLOCK_CONTROL_NRF_IRQ_HANDLERS_ITERABLE(clock_control_nrf_xo24m, |
| 308 | + &nrfx_clock_xo24m_irq_handler); |
| 309 | + |
| 310 | +static DEVICE_API(nrf_clock_control, clock_control_api) = { |
| 311 | + .std_api = { |
| 312 | + .on = api_blocking_start, |
| 313 | + .off = api_stop, |
| 314 | + .async_on = api_start, |
| 315 | + .get_status = api_get_status, |
| 316 | + }, |
| 317 | + .request = api_request, |
| 318 | + .release = api_release, |
| 319 | + .cancel_or_release = api_cancel_or_release, |
| 320 | +}; |
| 321 | + |
| 322 | +static xo24m_data_t data; |
| 323 | + |
| 324 | +static const xo24m_config_t config = { |
| 325 | + .start = xo24m_start, |
| 326 | + .stop = xo24m_stop, |
| 327 | +}; |
| 328 | + |
| 329 | +DEVICE_DT_DEFINE(DT_NODELABEL(xo24m), clk_init, NULL, |
| 330 | + &data, &config, |
| 331 | + PRE_KERNEL_1, CONFIG_CLOCK_CONTROL_INIT_PRIORITY, |
| 332 | + &clock_control_api); |
0 commit comments