This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 5d888f2c722afb2b575b8c436ad2539d1b79b36b
Author: qinwei1 <[email protected]>
AuthorDate: Fri Sep 4 20:13:19 2026 +0800

    drivers/devicetree: register SiFive PWMs found in the device tree
    
    - fdt_sifive_pwm_devices_register() walks nodes with compatible
      "sifive,pwm0", takes the MMIO base from the node (clock rate
       falls back to the model default when absent)
    -  registers each instance as /dev/pwmN through sifive_pwm_initialize().
    -  Gated by PWM_SIFIVE in CMakeLists/Make.defs, with the prototype
       declared in include/nuttx/fdt.h.
    
    Signed-off-by: qinwei1 <[email protected]>
---
 drivers/devicetree/CMakeLists.txt   |   4 +
 drivers/devicetree/Make.defs        |   4 +
 drivers/devicetree/fdt_sifive_pwm.c | 170 ++++++++++++++++++++++++++++++++++++
 include/nuttx/fdt.h                 |  20 +++++
 4 files changed, 198 insertions(+)

diff --git a/drivers/devicetree/CMakeLists.txt 
b/drivers/devicetree/CMakeLists.txt
index e97e1ddaa79..e4c4c47c4ea 100644
--- a/drivers/devicetree/CMakeLists.txt
+++ b/drivers/devicetree/CMakeLists.txt
@@ -46,6 +46,10 @@ if(CONFIG_DEVICE_TREE)
     list(APPEND SRCS fdt_qemu_fwcfg.c)
   endif()
 
+  if(CONFIG_PWM_SIFIVE)
+    list(APPEND SRCS fdt_sifive_pwm.c)
+  endif()
+
   target_include_directories(drivers
                              PRIVATE ${NUTTX_DIR}/libs/libc/fdt/dtc/libfdt)
   target_sources(drivers PRIVATE ${SRCS})
diff --git a/drivers/devicetree/Make.defs b/drivers/devicetree/Make.defs
index 0d186b2c23b..500bc312f6e 100644
--- a/drivers/devicetree/Make.defs
+++ b/drivers/devicetree/Make.defs
@@ -50,6 +50,10 @@ ifeq ($(CONFIG_QEMU_FWCFG),y)
   CSRCS += fdt_qemu_fwcfg.c
 endif
 
+ifeq ($(CONFIG_PWM_SIFIVE),y)
+  CSRCS += fdt_sifive_pwm.c
+endif
+
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)libs$(DELIM)libc$(DELIM)fdt$(DELIM)dtc$(DELIM)libfdt
 
 DEPPATH += --dep-path devicetree
diff --git a/drivers/devicetree/fdt_sifive_pwm.c 
b/drivers/devicetree/fdt_sifive_pwm.c
new file mode 100644
index 00000000000..640045b3f86
--- /dev/null
+++ b/drivers/devicetree/fdt_sifive_pwm.c
@@ -0,0 +1,170 @@
+/****************************************************************************
+ * drivers/devicetree/fdt_sifive_pwm.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdio.h>
+
+#include <nuttx/fdt.h>
+#include <nuttx/timers/pwm.h>
+#include <nuttx/timers/sifive_pwm.h>
+
+#include <libfdt.h>
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Read a node's single-cell "clock-frequency" (Hz).  The QEMU virt board
+ * does not emit the property for this device (the model keeps the rate as a
+ * QOM property), so fall back to the model default.  Parsed explicitly for
+ * the same reason as in fdt_sp804.c: the generic helper loads the value
+ * using the parent's #address-cells, which corrupts a 1-cell value.
+ */
+
+static uint32_t fdt_sifive_pwm_clock_hz(FAR const void *fdt, int offset)
+{
+  int len;
+  FAR const fdt32_t *pv = fdt_getprop(fdt, offset, "clock-frequency", &len);
+  uint32_t freq;
+
+  if (pv == NULL || len < (int)sizeof(fdt32_t))
+    {
+      return SIFIVE_PWM_DEFAULT_FREQ;
+    }
+
+  freq = fdt32_ld(pv);
+  return freq != 0 ? freq : SIFIVE_PWM_DEFAULT_FREQ;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_sifive_pwm_devices_register
+ *
+ * Description:
+ *   Scan the device tree for "sifive,pwm0" nodes and register each as a PWM
+ *   character device (/dev/pwm0, /dev/pwm1, ...).  The compare-match
+ *   interrupts the node advertises are not used: the lower-half only
+ *   programs the period and duty registers.
+ *
+ *   Every matching node is independent: the lower-half allocates its own
+ *   state per call, so a board may expose any number of controllers (the
+ *   arm virt board mounts one, sifive_u mounts two).  A node that cannot be
+ *   brought up is reported and skipped rather than aborting the scan, so one
+ *   malformed node does not cost the guest the controllers that follow it.
+ *
+ * Input Parameters:
+ *   fdt - Device tree handle
+ *
+ * Returned Value:
+ *   OK when every matching node was registered, or when the device tree
+ *   carries no "sifive,pwm0" node at all.  A negated errno from the first
+ *   node that failed otherwise.
+ *
+ ****************************************************************************/
+
+int fdt_sifive_pwm_devices_register(FAR const void *fdt)
+{
+  FAR struct pwm_lowerhalf_s *lower;
+  uintptr_t addr;
+  uint32_t freq;
+  char devpath[32];
+  int offset = -1;
+  int minor = 0;
+  int err = OK;
+  int ret;
+
+  for (; ; )
+    {
+      offset = fdt_node_offset_by_compatible(fdt, offset, "sifive,pwm0");
+      if (offset < 0)
+        {
+          /* -FDT_ERR_NOTFOUND ends the scan; anything else means the blob
+           * itself is unusable and must not be fed back as an offset.
+           */
+
+          if (offset != -FDT_ERR_NOTFOUND && err == OK)
+            {
+              pwmerr("ERROR: fdt scan failed: %d\n", offset);
+              err = -EINVAL;
+            }
+
+          break;
+        }
+
+      addr = fdt_get_reg_base(fdt, offset, 0);
+      if (addr == 0)
+        {
+          pwmerr("ERROR: node at offset %d has no usable reg\n", offset);
+          if (err == OK)
+            {
+              err = -EINVAL;
+            }
+
+          continue;
+        }
+
+      freq = fdt_sifive_pwm_clock_hz(fdt, offset);
+
+      lower = sifive_pwm_initialize(addr, freq);
+      if (lower == NULL)
+        {
+          pwmerr("ERROR: init failed for 0x%" PRIxPTR "\n", addr);
+          if (err == OK)
+            {
+              err = -ENOMEM;
+            }
+
+          continue;
+        }
+
+      /* Claim the lowest free minor.  Minors are not derived from the node
+       * address, so a skipped node does not leave a hole in the numbering.
+       */
+
+      snprintf(devpath, sizeof(devpath), "/dev/pwm%d", minor);
+      ret = pwm_register(devpath, lower);
+      if (ret < 0)
+        {
+          pwmerr("ERROR: %s register failed: %d\n", devpath, ret);
+          sifive_pwm_uninitialize(lower);
+          if (err == OK)
+            {
+              err = ret;
+            }
+
+          continue;
+        }
+
+      minor++;
+    }
+
+  return err;
+}
diff --git a/include/nuttx/fdt.h b/include/nuttx/fdt.h
index cd5867d52c1..44909c25e20 100644
--- a/include/nuttx/fdt.h
+++ b/include/nuttx/fdt.h
@@ -569,4 +569,24 @@ int fdt_cmsdk_wdt_devices_register(FAR const void *fdt, 
int irqbase);
 #ifdef CONFIG_QEMU_FWCFG
 int fdt_qemu_fwcfg_register(FAR const void *fdt);
 #endif
+
+/****************************************************************************
+ * Name: fdt_sifive_pwm_devices_register
+ *
+ * Description:
+ *   This function scans the device tree for nodes with compatible
+ *   "sifive,pwm0" and registers each as a PWM character device
+ *   (/dev/pwmN).
+ *
+ * Input Parameters:
+ *   fdt - Device tree handle
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_PWM_SIFIVE
+int fdt_sifive_pwm_devices_register(FAR const void *fdt);
+#endif
 #endif /* __INCLUDE_NUTTX_FDT_H */

Reply via email to