Commit 3b60bb91 authored by Sean Kyer's avatar Sean Kyer Committed by Henrik Brix Andersen
Browse files

native: cpu_freq: Add CPU freq support to native_sim



Define P-states for native_sim and add mock cpu_freq
driver.

Signed-off-by: default avatarSean Kyer <Sean.Kyer@analog.com>
parent e4fb01c6
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -236,4 +236,22 @@
		compatible = "zephyr,bt-hci-userchan";
		status = "okay";
	};

	performance-states {
		pstate_0: pstate_0 {
			compatible = "zephyr,native-sim-pstate";
			load-threshold = <50>;
			pstate-id = <0>;
		};
		pstate_1: pstate_1 {
			compatible = "zephyr,native-sim-pstate";
			load-threshold = <20>;
			pstate-id = <1>;
		};
		pstate_2: pstate_2 {
			compatible = "zephyr,native-sim-pstate";
			load-threshold = <0>;
			pstate-id = <2>;
		};
	};
};
+16 −0
Original line number Diff line number Diff line
# Copyright (c) 2025 Analog Devices, Inc.
#
# SPDX-License-Identifier: Apache-2.0

description: Native Sim mock implementation of custom properties for performance state (pstate)

compatible: "zephyr,native-sim-pstate"

include: "zephyr,pstate.yaml"

properties:
  pstate-id:
    type: int
    required: true
    description: |
      Identifier of performance state.
+2 −0
Original line number Diff line number Diff line
@@ -17,3 +17,5 @@ zephyr_library_include_directories(
  )

set(SOC_LINKER_SCRIPT ${ZEPHYR_BASE}/include/zephyr/arch/posix/linker.ld CACHE INTERNAL "")

zephyr_library_sources_ifdef(CONFIG_CPU_FREQ cpu_freq.c)
+1 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
config SOC_POSIX
	select ARCH_POSIX
	select CPU_HAS_FPU
	select HAS_CPU_FREQ

if SOC_POSIX

+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2025 Analog Devices, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/kernel.h>
#include <zephyr/devicetree.h>
#include <zephyr/cpu_freq/cpu_freq.h>
#include <zephyr/cpu_freq/pstate.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(native_sim_cpu_freq, CONFIG_CPU_FREQ_LOG_LEVEL);

struct native_sim_config {
	int state_id;
};

int cpu_freq_pstate_set(const struct pstate *state)
{
	if (state == NULL) {
		LOG_ERR("pstate is NULL");
		return -EINVAL;
	}

	int state_id = ((const struct native_sim_config *)state->config)->state_id;

	LOG_DBG("Setting performance state: %d", state_id);

	switch (state_id) {
	case 0:
		LOG_DBG("Setting P-state 0: Nominal Mode");
		break;
	case 1:
		LOG_DBG("Setting P-state 1: Low Power Mode");
		break;
	case 2:
		LOG_DBG("Setting P-state 2: Ultra-low Power Mode");
		break;
	default:
		LOG_ERR("Unsupported P-state: %d", state_id);
		return -1;
	}

	return 0;
}

#define DEFINE_NATIVE_SIM_CONFIG(node_id)                                                          \
	static const struct native_sim_config _CONCAT(native_sim_config_, node_id) = {             \
		.state_id = DT_PROP(node_id, pstate_id),                                           \
	};                                                                                         \
	PSTATE_DT_DEFINE(node_id, &_CONCAT(native_sim_config_, node_id))

DT_FOREACH_CHILD_STATUS_OKAY(DT_PATH(performance_states), DEFINE_NATIVE_SIM_CONFIG)