diff --git a/drivers/hwspinlock/CMakeLists.txt b/drivers/hwspinlock/CMakeLists.txt index 12eb4386273..6de410b190e 100644 --- a/drivers/hwspinlock/CMakeLists.txt +++ b/drivers/hwspinlock/CMakeLists.txt @@ -3,3 +3,4 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_SQN_HWSPINLOCK sqn_hwspinlock.c) +zephyr_library_sources_ifdef(CONFIG_HWSPINLOCK_TEST hwspinlock_test.c) diff --git a/drivers/hwspinlock/Kconfig b/drivers/hwspinlock/Kconfig index 22e58528c57..dbdd2d6d753 100644 --- a/drivers/hwspinlock/Kconfig +++ b/drivers/hwspinlock/Kconfig @@ -22,6 +22,7 @@ config HWSPINLOCK_INIT_PRIORITY # zephyr-keep-sorted-start source "drivers/hwspinlock/Kconfig.sqn" +source "drivers/hwspinlock/Kconfig.test" # zephyr-keep-sorted-stop endif diff --git a/drivers/hwspinlock/Kconfig.test b/drivers/hwspinlock/Kconfig.test new file mode 100644 index 00000000000..65fab8133e6 --- /dev/null +++ b/drivers/hwspinlock/Kconfig.test @@ -0,0 +1,6 @@ +# Copyright (c) 2025 Analog Devices, Inc. +# SPDX-License-Identifier: Apache-2.0 +# +config HWSPINLOCK_TEST + def_bool DT_HAS_VND_HWSPINLOCK_ENABLED + depends on DT_HAS_VND_HWSPINLOCK_ENABLED diff --git a/drivers/hwspinlock/hwspinlock_test.c b/drivers/hwspinlock/hwspinlock_test.c new file mode 100644 index 00000000000..215403c8bec --- /dev/null +++ b/drivers/hwspinlock/hwspinlock_test.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 Analog Devices, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#define DT_DRV_COMPAT vnd_hwspinlock + +static void vnd_hwspinlock_lock(const struct device *dev, uint32_t id) +{ +} + +static void vnd_hwspinlock_unlock(const struct device *dev, uint32_t id) +{ +} + +static uint32_t vnd_hwspinlock_get_max_id(const struct device *dev) +{ + return 0; +} + +static DEVICE_API(hwspinlock, vnd_hwspinlock_api) = { + .lock = vnd_hwspinlock_lock, + .unlock = vnd_hwspinlock_unlock, + .get_max_id = vnd_hwspinlock_get_max_id, +}; + +#define VND_HWSPINLOCK_INIT(idx) \ + DEVICE_DT_INST_DEFINE(idx, NULL, NULL, NULL, NULL, POST_KERNEL, \ + CONFIG_HWSPINLOCK_INIT_PRIORITY, \ + &vnd_hwspinlock_api) + +DT_INST_FOREACH_STATUS_OKAY(VND_HWSPINLOCK_INIT); diff --git a/dts/bindings/test/vnd,hwspinlock-device.yaml b/dts/bindings/test/vnd,hwspinlock-device.yaml new file mode 100644 index 00000000000..0d2ad5bb709 --- /dev/null +++ b/dts/bindings/test/vnd,hwspinlock-device.yaml @@ -0,0 +1,12 @@ +# Copyright (c) 2025 Analog Devices, Inc. +# SPDX-License-Identifier: Apache-2.0 + +description: Test hardware spinlock device node + +compatible: "vnd,hwspinlock-device" + +include: base.yaml + +properties: + hwlocks: + required: true diff --git a/dts/bindings/test/vnd,hwspinlock.yaml b/dts/bindings/test/vnd,hwspinlock.yaml new file mode 100644 index 00000000000..0248a6f2734 --- /dev/null +++ b/dts/bindings/test/vnd,hwspinlock.yaml @@ -0,0 +1,11 @@ +# Copyright (c) 2025 Analog Devices, Inc. +# SPDX-License-Identifier: Apache-2.0 + +description: Test hardware spinlock node + +compatible: "vnd,hwspinlock" + +include: [base.yaml, hwspinlock-controller.yaml] + +hwlock-cells: + - id diff --git a/tests/lib/devicetree/api_ext/app.overlay b/tests/lib/devicetree/api_ext/app.overlay index cd270ec51d1..660be4d0b01 100644 --- a/tests/lib/devicetree/api_ext/app.overlay +++ b/tests/lib/devicetree/api_ext/app.overlay @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Commonwealth Scientific and Industrial Research * Organisation (CSIRO) ABN 41 687 119 230. + * Copyright (c) 2025 Analog Devices, Inc. * * SPDX-License-Identifier: Apache-2.0 * @@ -75,5 +76,18 @@ mboxes = <&test_mbox 1>, <&test_mbox 2>, <&test_mbox_zero_cell>; mbox-names = "tx", "rx", "zero"; }; + + test_hwspinlock: hwspinlock { + compatible = "vnd,hwspinlock"; + #hwlock-cells = <1>; + status = "okay"; + }; + + test_hwspinlock_dev: hwspinlock-dev { + compatible = "vnd,hwspinlock-device"; + hwlocks = <&test_hwspinlock 1>, <&test_hwspinlock 2>; + hwlock-names = "rd", "wr"; + status = "okay"; + }; }; }; diff --git a/tests/lib/devicetree/api_ext/prj.conf b/tests/lib/devicetree/api_ext/prj.conf index 80789f1bf70..bf7098573c9 100644 --- a/tests/lib/devicetree/api_ext/prj.conf +++ b/tests/lib/devicetree/api_ext/prj.conf @@ -1,3 +1,5 @@ CONFIG_ZTEST=y CONFIG_ADC=y CONFIG_MBOX=y +CONFIG_HWSPINLOCK=y +CONFIG_SPIN_VALIDATE=n diff --git a/tests/lib/devicetree/api_ext/src/main.c b/tests/lib/devicetree/api_ext/src/main.c index d3f742990d5..3be0ce60720 100644 --- a/tests/lib/devicetree/api_ext/src/main.c +++ b/tests/lib/devicetree/api_ext/src/main.c @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Commonwealth Scientific and Industrial Research * Organisation (CSIRO) ABN 41 687 119 230. + * Copyright (c) 2025 Analog Devices, Inc. * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,6 +12,7 @@ #include #include #include +#include #include @@ -59,4 +61,37 @@ ZTEST(devicetree_api_ext, test_mbox_dt_spec) zassert_equal(channel_zero.channel_id, 0, ""); } +#define TEST_HWSPINLOCK \ + DT_NODELABEL(test_hwspinlock) + +#define TEST_HWSPINLOCK_DEV \ + DT_NODELABEL(test_hwspinlock_dev) + +#define HWSPINLOCK_BY_IDX(node_id, prop, idx) \ + HWSPINLOCK_DT_SPEC_GET_BY_IDX(node_id, idx) + +static const struct hwspinlock_dt_spec spec[] = { + DT_FOREACH_PROP_ELEM_SEP(TEST_HWSPINLOCK_DEV, hwlocks, HWSPINLOCK_BY_IDX, (,)) +}; + +static const struct hwspinlock_dt_spec rd = + HWSPINLOCK_DT_SPEC_GET_BY_NAME(TEST_HWSPINLOCK_DEV, rd); + +static const struct hwspinlock_dt_spec wr = + HWSPINLOCK_DT_SPEC_GET_BY_NAME(TEST_HWSPINLOCK_DEV, wr); + +ZTEST(devicetree_api_ext, test_hwspinlock_dt_spec) +{ + for (int i = 0; i < ARRAY_SIZE(spec); i++) { + zassert_equal(spec[i].dev, DEVICE_DT_GET(TEST_HWSPINLOCK)); + zassert_equal(spec[i].id, i + 1); + } + + zassert_equal(rd.dev, DEVICE_DT_GET(TEST_HWSPINLOCK)); + zassert_equal(rd.id, 1); + + zassert_equal(wr.dev, DEVICE_DT_GET(TEST_HWSPINLOCK)); + zassert_equal(wr.id, 2); +} + ZTEST_SUITE(devicetree_api_ext, NULL, NULL, NULL, NULL, NULL);