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 d500d9adabc2481a6554e90da76cb1310ab99050
Author: yangsong8 <[email protected]>
AuthorDate: Fri Sep 4 16:49:19 2026 +0800

    drivers/devicetree: register i.MX GPT ICU/OCU from device tree
    
    Add fdt_imx_gpt_devices_register(), which walks nodes with compatible
    "fsl,imx-gpt", takes the MMIO base, IRQ and clock-frequency from each
    node and brings up its ICU/OCU channels through imx_gpt_initialize().
    
    Assisted-by: Kiro:claude-opus-5-5
    Signed-off-by: yangsong8 <[email protected]>
---
 drivers/devicetree/CMakeLists.txt |   3 ++
 drivers/devicetree/Make.defs      |   3 ++
 drivers/devicetree/fdt_imx_gpt.c  | 111 ++++++++++++++++++++++++++++++++++++++
 include/nuttx/fdt.h               |  21 ++++++++
 4 files changed, 138 insertions(+)

diff --git a/drivers/devicetree/CMakeLists.txt 
b/drivers/devicetree/CMakeLists.txt
index e4c4c47c4ea..9779cfecf83 100644
--- a/drivers/devicetree/CMakeLists.txt
+++ b/drivers/devicetree/CMakeLists.txt
@@ -37,6 +37,9 @@ if(CONFIG_DEVICE_TREE)
   if(CONFIG_TIMER_SP804 AND CONFIG_TIMER)
     list(APPEND SRCS fdt_sp804.c)
   endif()
+  if(CONFIG_TIMER_IMX_GPT)
+    list(APPEND SRCS fdt_imx_gpt.c)
+  endif()
 
   if(CONFIG_WATCHDOG_CMSDK)
     list(APPEND SRCS fdt_cmsdk_wdt.c)
diff --git a/drivers/devicetree/Make.defs b/drivers/devicetree/Make.defs
index 500bc312f6e..3d294f421d4 100644
--- a/drivers/devicetree/Make.defs
+++ b/drivers/devicetree/Make.defs
@@ -41,6 +41,9 @@ ifeq ($(CONFIG_TIMER),y)
   CSRCS += fdt_sp804.c
 endif
 endif
+ifeq ($(CONFIG_TIMER_IMX_GPT),y)
+  CSRCS += fdt_imx_gpt.c
+endif
 
 ifeq ($(CONFIG_WATCHDOG_CMSDK),y)
   CSRCS += fdt_cmsdk_wdt.c
diff --git a/drivers/devicetree/fdt_imx_gpt.c b/drivers/devicetree/fdt_imx_gpt.c
new file mode 100644
index 00000000000..b4e74383a36
--- /dev/null
+++ b/drivers/devicetree/fdt_imx_gpt.c
@@ -0,0 +1,111 @@
+/****************************************************************************
+ * drivers/devicetree/fdt_imx_gpt.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 <nuttx/fdt.h>
+#include <nuttx/timers/imx_gpt.h>
+
+#include <libfdt.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_imx_gpt_devices_register
+ *
+ * Description:
+ *   Scan the device tree for "fsl,imx-gpt" nodes and bring each one up via
+ *   imx_gpt_initialize(), registering its OCU channels as /dev/ocuN and its
+ *   ICU channels as /dev/capN.
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number (added to the FDT-supplied SPI index)
+ *
+ * Returned Value:
+ *   Return OK on success; a negated errno on failure.
+ *
+ ****************************************************************************/
+
+int fdt_imx_gpt_devices_register(FAR const void *fdt, int irqbase)
+{
+  uintptr_t addr;
+  uint32_t freq;
+  int offset = -1;
+  int irqnum;
+  int ocu_bus = 0;
+  int cap_bus = 0;
+
+  for (; ; )
+    {
+      offset = fdt_node_offset_by_compatible(fdt, offset, "fsl,imx-gpt");
+      if (offset == -FDT_ERR_NOTFOUND)
+        {
+          break;
+        }
+
+      if (!fdt_device_is_available(fdt, offset))
+        {
+          continue;
+        }
+
+      addr   = fdt_get_reg_base(fdt, offset, 0);
+      irqnum = fdt_get_irq(fdt, offset, 1, irqbase);
+
+      /* clock-frequency is a single u32 cell.  Read it directly with
+       * fdt_load_prop_u32() rather than fdt_get_clock_frequency(), which
+       * decodes the value using the parent's #address-cells and would
+       * mis-read the 32-bit scalar on a 2-address-cell machine.
+       */
+
+      freq = 0;
+      fdt_load_prop_u32(fdt, offset, "clock-frequency", 0, &freq);
+      if (addr == 0 || irqnum < 0)
+        {
+          continue;
+        }
+
+      if (imx_gpt_initialize(addr, irqnum, freq, ocu_bus, cap_bus) < 0)
+        {
+          syslog(LOG_ERR, "imx_gpt_initialize failed @0x%" PRIxPTR "\n",
+                 addr);
+          continue;
+        }
+
+#ifdef CONFIG_TIMER_IMX_GPT_OCU
+      ocu_bus += IMX_GPT_NUM_OCU;
+#endif
+#ifdef CONFIG_TIMER_IMX_GPT_ICU
+      cap_bus += IMX_GPT_NUM_ICU;
+#endif
+    }
+
+  return OK;
+}
diff --git a/include/nuttx/fdt.h b/include/nuttx/fdt.h
index 44909c25e20..f4b4fe98254 100644
--- a/include/nuttx/fdt.h
+++ b/include/nuttx/fdt.h
@@ -530,6 +530,27 @@ int fdt_cfi_register(FAR const void *fdt);
 int fdt_sp804_devices_register(FAR const void *fdt, int irqbase);
 #endif
 
+/****************************************************************************
+ * Name: fdt_imx_gpt_devices_register
+ *
+ * Description:
+ *   This function scans the device tree for nodes with compatible
+ *   "fsl,imx-gpt" and registers each one's OCU channels as /dev/ocuN and
+ *   ICU channels as /dev/capN via imx_gpt_initialize().
+ *
+ * Input Parameters:
+ *   fdt      - Device tree handle
+ *   irqbase  - Interrupt base number
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_TIMER_IMX_GPT
+int fdt_imx_gpt_devices_register(FAR const void *fdt, int irqbase);
+#endif
+
 /****************************************************************************
  * Name: fdt_cmsdk_wdt_devices_register
  *

Reply via email to