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

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

commit fccc2163619d97988f0cbdaca8b1ce8586e9b7b2
Author: Parthiban Nallathambi <[email protected]>
AuthorDate: Mon Nov 23 13:36:59 2026 +0530

    arch/tricore: Add a GPIO driver for the AURIX ports.
    
    Add tricore_gpio_port_initialize, which creates the GPIO driver of the
    16 pins of a port on top of the GPIO interface when CONFIG_IOEXPANDER is
    set.  It implements the NuttX I/O expander interface, so kernel drivers
    can use the pins and gpio_lower_half registers them as /dev/gpioN for
    the applications.  A pin can be a floating or pulled input or a
    push-pull or open drain output, and an output starts with the level last
    written to the pin.  Pin interrupts are not supported.
    
    Signed-off-by: Parthiban Nallathambi <[email protected]>
---
 arch/tricore/src/common/aurix/CMakeLists.txt      |   4 +
 arch/tricore/src/common/aurix/Make.defs           |   4 +
 arch/tricore/src/common/aurix/tricore_gpio_port.c | 335 ++++++++++++++++++++++
 arch/tricore/src/common/aurix/tricore_gpio_port.h |  63 ++++
 4 files changed, 406 insertions(+)

diff --git a/arch/tricore/src/common/aurix/CMakeLists.txt 
b/arch/tricore/src/common/aurix/CMakeLists.txt
index ff2535d8ba1..cf534e4fb64 100644
--- a/arch/tricore/src/common/aurix/CMakeLists.txt
+++ b/arch/tricore/src/common/aurix/CMakeLists.txt
@@ -24,3 +24,7 @@
 # of each family
 
 target_sources(arch PRIVATE tricore_serial.c)
+
+if(CONFIG_IOEXPANDER)
+  target_sources(arch PRIVATE tricore_gpio_port.c)
+endif()
diff --git a/arch/tricore/src/common/aurix/Make.defs 
b/arch/tricore/src/common/aurix/Make.defs
index cb45158ae6e..4613f726bf0 100644
--- a/arch/tricore/src/common/aurix/Make.defs
+++ b/arch/tricore/src/common/aurix/Make.defs
@@ -25,4 +25,8 @@
 
 CHIP_CSRCS += tricore_serial.c
 
+ifeq ($(CONFIG_IOEXPANDER),y)
+CHIP_CSRCS += tricore_gpio_port.c
+endif
+
 VPATH += common$(DELIM)aurix
diff --git a/arch/tricore/src/common/aurix/tricore_gpio_port.c 
b/arch/tricore/src/common/aurix/tricore_gpio_port.c
new file mode 100644
index 00000000000..98e7aba4b26
--- /dev/null
+++ b/arch/tricore/src/common/aurix/tricore_gpio_port.c
@@ -0,0 +1,335 @@
+/****************************************************************************
+ * arch/tricore/src/common/aurix/tricore_gpio_port.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 <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <errno.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/ioexpander/ioexpander.h>
+
+#include "tricore_gpio.h"
+#include "tricore_gpio_port.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TRICORE_GPIO_PORT_NPINS  16   /* Pins of a port */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct tricore_gpio_port_s
+{
+  struct ioexpander_dev_s dev;  /* Driver interface, must be first */
+  uint8_t port;                 /* Port number */
+  uint16_t out;                 /* Output level written to each pin */
+  spinlock_t lock;              /* Protects out */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int tricore_gpio_port_direction(FAR struct ioexpander_dev_s *dev,
+                                       uint8_t pin, int direction);
+static int tricore_gpio_port_option(FAR struct ioexpander_dev_s *dev,
+                                    uint8_t pin, int opt, FAR void *val);
+static int tricore_gpio_port_writepin(FAR struct ioexpander_dev_s *dev,
+                                      uint8_t pin, bool value);
+static int tricore_gpio_port_readpin(FAR struct ioexpander_dev_s *dev,
+                                     uint8_t pin, FAR bool *value);
+static int tricore_gpio_port_readbuf(FAR struct ioexpander_dev_s *dev,
+                                     uint8_t pin, FAR bool *value);
+#ifdef CONFIG_IOEXPANDER_INT_ENABLE
+static FAR void *tricore_gpio_port_attach(FAR struct ioexpander_dev_s *dev,
+                                          ioe_pinset_t pinset,
+                                          ioe_callback_t callback,
+                                          FAR void *arg);
+static int tricore_gpio_port_detach(FAR struct ioexpander_dev_s *dev,
+                                    FAR void *handle);
+#endif
+static int tricore_gpio_port_toggle(FAR struct ioexpander_dev_s *dev,
+                                    uint8_t pin, FAR bool *value);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct ioexpander_ops_s g_tricore_gpio_port_ops =
+{
+  .ioe_direction = tricore_gpio_port_direction,
+  .ioe_option    = tricore_gpio_port_option,
+  .ioe_writepin  = tricore_gpio_port_writepin,
+  .ioe_readpin   = tricore_gpio_port_readpin,
+  .ioe_readbuf   = tricore_gpio_port_readbuf,
+#ifdef CONFIG_IOEXPANDER_INT_ENABLE
+  .ioe_attach    = tricore_gpio_port_attach,
+  .ioe_detach    = tricore_gpio_port_detach,
+#endif
+  .ioe_toggle    = tricore_gpio_port_toggle,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tricore_gpio_port_pinset
+ *
+ * Description:
+ *   Return the pin set of a pin of the port.
+ *
+ ****************************************************************************/
+
+static uint32_t
+tricore_gpio_port_pinset(FAR struct tricore_gpio_port_s *priv, uint8_t pin)
+{
+  return GPIO_PORT(priv->port) | GPIO_PIN(pin);
+}
+
+/****************************************************************************
+ * Name: tricore_gpio_port_direction
+ *
+ * Description:
+ *   Make a pin a GPIO input or output.  An output starts with the level
+ *   last written to the pin, low if none was written.
+ *
+ ****************************************************************************/
+
+static int tricore_gpio_port_direction(FAR struct ioexpander_dev_s *dev,
+                                       uint8_t pin, int direction)
+{
+  FAR struct tricore_gpio_port_s *priv =
+    (FAR struct tricore_gpio_port_s *)dev;
+  uint32_t pinset;
+
+  if (pin >= TRICORE_GPIO_PORT_NPINS)
+    {
+      return -EINVAL;
+    }
+
+  pinset = tricore_gpio_port_pinset(priv, pin);
+
+  switch (direction)
+    {
+      case IOEXPANDER_DIRECTION_IN:
+        pinset |= GPIO_INPUT | GPIO_FLOAT;
+        break;
+
+      case IOEXPANDER_DIRECTION_IN_PULLUP:
+        pinset |= GPIO_INPUT | GPIO_PULLUP;
+        break;
+
+      case IOEXPANDER_DIRECTION_IN_PULLDOWN:
+        pinset |= GPIO_INPUT | GPIO_PULLDOWN;
+        break;
+
+      case IOEXPANDER_DIRECTION_OUT:
+        pinset |= GPIO_OUTPUT;
+        break;
+
+      case IOEXPANDER_DIRECTION_OUT_OPENDRAIN:
+        pinset |= GPIO_OUTPUT | GPIO_OPENDRAIN;
+        break;
+
+      default:
+        return -EINVAL;
+    }
+
+  if ((priv->out & (1 << pin)) != 0)
+    {
+      pinset |= GPIO_OUTPUT_SET;
+    }
+
+  return tricore_gpio_config(pinset);
+}
+
+/****************************************************************************
+ * Name: tricore_gpio_port_option
+ *
+ * Description:
+ *   The pins have no interrupts yet, so only the options that disable an
+ *   interrupt are accepted.
+ *
+ ****************************************************************************/
+
+static int tricore_gpio_port_option(FAR struct ioexpander_dev_s *dev,
+                                    uint8_t pin, int opt, FAR void *val)
+{
+  if (pin >= TRICORE_GPIO_PORT_NPINS)
+    {
+      return -EINVAL;
+    }
+
+  switch (opt)
+    {
+      case IOEXPANDER_OPTION_INTCFG:
+      case IOEXPANDER_OPTION_WAKEUPCFG:
+        return (uintptr_t)val == IOEXPANDER_VAL_DISABLE ? OK : -ENOTSUP;
+
+      default:
+        return -ENOTSUP;
+    }
+}
+
+static int tricore_gpio_port_writepin(FAR struct ioexpander_dev_s *dev,
+                                      uint8_t pin, bool value)
+{
+  FAR struct tricore_gpio_port_s *priv =
+    (FAR struct tricore_gpio_port_s *)dev;
+  irqstate_t flags;
+
+  if (pin >= TRICORE_GPIO_PORT_NPINS)
+    {
+      return -EINVAL;
+    }
+
+  flags = spin_lock_irqsave(&priv->lock);
+
+  if (value)
+    {
+      priv->out |= 1 << pin;
+    }
+  else
+    {
+      priv->out &= ~(1 << pin);
+    }
+
+  tricore_gpio_write(tricore_gpio_port_pinset(priv, pin), value);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  return OK;
+}
+
+static int tricore_gpio_port_readpin(FAR struct ioexpander_dev_s *dev,
+                                     uint8_t pin, FAR bool *value)
+{
+  FAR struct tricore_gpio_port_s *priv =
+    (FAR struct tricore_gpio_port_s *)dev;
+
+  if (pin >= TRICORE_GPIO_PORT_NPINS)
+    {
+      return -EINVAL;
+    }
+
+  *value = tricore_gpio_read(tricore_gpio_port_pinset(priv, pin));
+  return OK;
+}
+
+static int tricore_gpio_port_readbuf(FAR struct ioexpander_dev_s *dev,
+                                     uint8_t pin, FAR bool *value)
+{
+  FAR struct tricore_gpio_port_s *priv =
+    (FAR struct tricore_gpio_port_s *)dev;
+
+  if (pin >= TRICORE_GPIO_PORT_NPINS)
+    {
+      return -EINVAL;
+    }
+
+  *value = (priv->out & (1 << pin)) != 0;
+  return OK;
+}
+
+#ifdef CONFIG_IOEXPANDER_INT_ENABLE
+static FAR void *tricore_gpio_port_attach(FAR struct ioexpander_dev_s *dev,
+                                          ioe_pinset_t pinset,
+                                          ioe_callback_t callback,
+                                          FAR void *arg)
+{
+  return NULL;
+}
+
+static int tricore_gpio_port_detach(FAR struct ioexpander_dev_s *dev,
+                                    FAR void *handle)
+{
+  return -ENOTSUP;
+}
+#endif
+
+static int tricore_gpio_port_toggle(FAR struct ioexpander_dev_s *dev,
+                                    uint8_t pin, FAR bool *value)
+{
+  FAR struct tricore_gpio_port_s *priv =
+    (FAR struct tricore_gpio_port_s *)dev;
+  irqstate_t flags;
+  bool level;
+
+  if (pin >= TRICORE_GPIO_PORT_NPINS)
+    {
+      return -EINVAL;
+    }
+
+  flags = spin_lock_irqsave(&priv->lock);
+
+  priv->out ^= 1 << pin;
+  level = (priv->out & (1 << pin)) != 0;
+  tricore_gpio_write(tricore_gpio_port_pinset(priv, pin), level);
+
+  spin_unlock_irqrestore(&priv->lock, flags);
+
+  if (value != NULL)
+    {
+      *value = level;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tricore_gpio_port_initialize
+ ****************************************************************************/
+
+FAR struct ioexpander_dev_s *tricore_gpio_port_initialize(int port)
+{
+  FAR struct tricore_gpio_port_s *priv;
+
+  if (port < 0 || port > GPIO_PORT_OF(GPIO_PORT_MASK))
+    {
+      return NULL;
+    }
+
+  priv = kmm_zalloc(sizeof(struct tricore_gpio_port_s));
+  if (priv == NULL)
+    {
+      return NULL;
+    }
+
+  priv->dev.ops = &g_tricore_gpio_port_ops;
+  priv->port    = port;
+  spin_lock_init(&priv->lock);
+
+  return &priv->dev;
+}
diff --git a/arch/tricore/src/common/aurix/tricore_gpio_port.h 
b/arch/tricore/src/common/aurix/tricore_gpio_port.h
new file mode 100644
index 00000000000..a42d6abfcf6
--- /dev/null
+++ b/arch/tricore/src/common/aurix/tricore_gpio_port.h
@@ -0,0 +1,63 @@
+/****************************************************************************
+ * arch/tricore/src/common/aurix/tricore_gpio_port.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_TRICORE_SRC_COMMON_AURIX_TRICORE_GPIO_PORT_H
+#define __ARCH_TRICORE_SRC_COMMON_AURIX_TRICORE_GPIO_PORT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/ioexpander/ioexpander.h>
+
+#ifdef CONFIG_IOEXPANDER
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tricore_gpio_port_initialize
+ *
+ * Description:
+ *   Create the GPIO driver of the 16 pins of a port.  It implements the
+ *   NuttX I/O expander interface, so that kernel drivers can use the pins
+ *   and gpio_lower_half can register them as /dev/gpioN.  The pins keep
+ *   their configuration until a direction is set; an output then starts
+ *   with the level last written to the pin.  Pin interrupts are not
+ *   supported.
+ *
+ * Input Parameters:
+ *   port - Port number, for example 3 for P03
+ *
+ * Returned Value:
+ *   The driver of the port, or NULL if the port is not valid or no memory
+ *   is left.
+ *
+ ****************************************************************************/
+
+FAR struct ioexpander_dev_s *tricore_gpio_port_initialize(int port);
+
+#endif /* CONFIG_IOEXPANDER */
+#endif /* __ARCH_TRICORE_SRC_COMMON_AURIX_TRICORE_GPIO_PORT_H */

Reply via email to