2016-08-01 16:43:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/ioexpander/gpio_lower_half.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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
|
2016-08-01 16:43:10 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-01 16:43:10 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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.
|
2016-08-01 16:43:10 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <assert.h>
|
2021-06-23 14:14:22 +02:00
|
|
|
#include <debug.h>
|
2016-08-01 16:43:10 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/ioexpander/ioexpander.h>
|
|
|
|
#include <nuttx/ioexpander/gpio.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_GPIO_LOWER_HALF
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* GPIO lower half driver state */
|
|
|
|
|
|
|
|
struct gplh_dev_s
|
|
|
|
{
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Publicly visible lower-half state */
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
struct gpio_dev_s gpio;
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-08-02 16:37:30 +02:00
|
|
|
/* Private lower half data follows */
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
uint8_t pin; /* I/O expander pin ID */
|
|
|
|
FAR struct ioexpander_dev_s *ioe; /* Contain I/O expander interface */
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2016-08-01 16:43:10 +02:00
|
|
|
FAR void *handle; /* Interrupt attach handle */
|
|
|
|
pin_interrupt_t callback; /* Interrupt callback */
|
2016-08-01 17:06:58 +02:00
|
|
|
#endif
|
2016-08-01 16:43:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2016-08-01 16:43:10 +02:00
|
|
|
static int gplh_handler(FAR struct ioexpander_dev_s *ioe,
|
|
|
|
ioe_pinset_t pinset, FAR void *arg);
|
2016-08-01 17:06:58 +02:00
|
|
|
#endif
|
2016-08-01 16:43:10 +02:00
|
|
|
|
2016-08-02 16:37:30 +02:00
|
|
|
/* GPIO lower half interface methods */
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
static int gplh_read(FAR struct gpio_dev_s *gpio, FAR bool *value);
|
|
|
|
static int gplh_write(FAR struct gpio_dev_s *gpio, bool value);
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2020-07-02 12:47:58 +02:00
|
|
|
static int gplh_attach(FAR struct gpio_dev_s *gpio,
|
|
|
|
pin_interrupt_t callback);
|
2016-08-01 16:43:10 +02:00
|
|
|
static int gplh_enable(FAR struct gpio_dev_s *gpio, bool enable);
|
2016-08-01 17:06:58 +02:00
|
|
|
#endif
|
2018-08-27 17:25:11 +02:00
|
|
|
static int gplh_setpintype(FAR struct gpio_dev_s *gpio,
|
|
|
|
enum gpio_pintype_e pintype);
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-02 16:37:30 +02:00
|
|
|
/* GPIO Lower Half interface operations */
|
|
|
|
|
2016-08-01 16:43:10 +02:00
|
|
|
static const struct gpio_operations_s g_gplh_ops =
|
|
|
|
{
|
|
|
|
gplh_read, /* read */
|
|
|
|
gplh_write, /* write */
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2016-08-01 16:43:10 +02:00
|
|
|
gplh_attach, /* attach */
|
|
|
|
gplh_enable, /* enable */
|
2016-08-01 17:06:58 +02:00
|
|
|
#else
|
|
|
|
NULL, /* attach */
|
|
|
|
NULL, /* enable */
|
|
|
|
#endif
|
2018-08-27 17:25:11 +02:00
|
|
|
gplh_setpintype,
|
|
|
|
};
|
|
|
|
|
2022-08-14 22:47:03 +02:00
|
|
|
/* Identifies the type of the GPIO pin */
|
2018-08-27 17:25:11 +02:00
|
|
|
|
2022-08-14 22:47:03 +02:00
|
|
|
static const uint32_t g_gplh_inttype[GPIO_NPINTYPES] =
|
2018-08-27 17:25:11 +02:00
|
|
|
{
|
2022-08-14 22:47:03 +02:00
|
|
|
IOEXPANDER_VAL_DISABLE, /* GPIO_INPUT_PIN */
|
|
|
|
IOEXPANDER_VAL_DISABLE, /* GPIO_INPUT_PIN_PULLUP */
|
|
|
|
IOEXPANDER_VAL_DISABLE, /* GPIO_INPUT_PIN_PULLDOWN */
|
|
|
|
IOEXPANDER_VAL_DISABLE, /* GPIO_OUTPUT_PIN */
|
|
|
|
IOEXPANDER_VAL_DISABLE, /* GPIO_OUTPUT_PIN_OPENDRAIN */
|
|
|
|
CONFIG_GPIO_LOWER_HALF_INTTYPE, /* GPIO_INTERRUPT_PIN */
|
|
|
|
IOEXPANDER_VAL_HIGH, /* GPIO_INTERRUPT_HIGH_PIN */
|
|
|
|
IOEXPANDER_VAL_LOW, /* GPIO_INTERRUPT_LOW_PIN */
|
|
|
|
IOEXPANDER_VAL_RISING, /* GPIO_INTERRUPT_RISING_PIN */
|
|
|
|
IOEXPANDER_VAL_FALLING, /* GPIO_INTERRUPT_FALLING_PIN */
|
|
|
|
IOEXPANDER_VAL_BOTH, /* GPIO_INTERRUPT_BOTH_PIN */
|
2023-11-01 10:01:15 +01:00
|
|
|
CONFIG_GPIO_LOWER_HALF_INTTYPE, /* GPIO_INTERRUPT_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_VAL_HIGH, /* GPIO_INTERRUPT_HIGH_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_VAL_LOW, /* GPIO_INTERRUPT_LOW_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_VAL_RISING, /* GPIO_INTERRUPT_RISING_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_VAL_FALLING, /* GPIO_INTERRUPT_FALLING_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_VAL_BOTH, /* GPIO_INTERRUPT_BOTH_PIN_WAKEUP */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint32_t g_gplh_wakeuptype[GPIO_NPINTYPES] =
|
|
|
|
{
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INPUT_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INPUT_PIN_PULLUP */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INPUT_PIN_PULLDOWN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_OUTPUT_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_OUTPUT_PIN_OPENDRAIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INTERRUPT_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INTERRUPT_HIGH_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INTERRUPT_LOW_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INTERRUPT_RISING_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INTERRUPT_FALLING_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_DISABLE, /* GPIO_INTERRUPT_BOTH_PIN */
|
|
|
|
IOEXPANDER_WAKEUP_ENABLE, /* GPIO_INTERRUPT_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_WAKEUP_ENABLE, /* GPIO_INTERRUPT_HIGH_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_WAKEUP_ENABLE, /* GPIO_INTERRUPT_LOW_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_WAKEUP_ENABLE, /* GPIO_INTERRUPT_RISING_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_WAKEUP_ENABLE, /* GPIO_INTERRUPT_FALLING_PIN_WAKEUP */
|
|
|
|
IOEXPANDER_WAKEUP_ENABLE, /* GPIO_INTERRUPT_BOTH_PIN_WAKEUP */
|
2016-08-01 16:43:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gplh_handler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* I/O expander interrupt callback function.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2016-08-01 16:43:10 +02:00
|
|
|
static int gplh_handler(FAR struct ioexpander_dev_s *ioe,
|
|
|
|
ioe_pinset_t pinset, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv = (FAR struct gplh_dev_s *)arg;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->callback != NULL);
|
|
|
|
|
2016-08-01 16:49:15 +02:00
|
|
|
gpioinfo("pin%u: pinset: %lx callback=%p\n",
|
|
|
|
priv->pin, (unsigned long)pinset, priv->callback);
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
/* We received the callback from the I/O expander, forward this to the
|
|
|
|
* upper half GPIO driver via its callback.
|
|
|
|
*/
|
|
|
|
|
2018-08-27 17:25:11 +02:00
|
|
|
return priv->callback(&priv->gpio, priv->pin);
|
2016-08-01 16:43:10 +02:00
|
|
|
}
|
2016-08-01 17:06:58 +02:00
|
|
|
#endif
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gplh_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the value of the I/O expander pin.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int gplh_read(FAR struct gpio_dev_s *gpio, FAR bool *value)
|
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv = (FAR struct gplh_dev_s *)gpio;
|
|
|
|
|
2016-08-03 20:46:54 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->ioe != NULL && value != NULL);
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
gpioinfo("pin%u: value=%p\n", priv->pin, value);
|
|
|
|
|
2016-08-02 16:37:30 +02:00
|
|
|
/* Return the value from the I/O expander */
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
return IOEXP_READPIN(priv->ioe, priv->pin, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gplh_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the value of an I/O expander output pin
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int gplh_write(FAR struct gpio_dev_s *gpio, bool value)
|
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv = (FAR struct gplh_dev_s *)gpio;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->ioe != NULL);
|
|
|
|
|
|
|
|
gpioinfo("pin%u: value=%u\n", priv->pin, value);
|
|
|
|
|
|
|
|
/* Write the value using the I/O expander */
|
|
|
|
|
|
|
|
return IOEXP_WRITEPIN(priv->ioe, priv->pin, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gplh_attach
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Detach and disable any current interrupt on the pin. Save the callback
|
|
|
|
* information for use when the pin interrupt is enabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2016-08-01 16:43:10 +02:00
|
|
|
static int gplh_attach(FAR struct gpio_dev_s *gpio, pin_interrupt_t callback)
|
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv = (FAR struct gplh_dev_s *)gpio;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->ioe != NULL);
|
|
|
|
|
|
|
|
gpioinfo("pin%u: callback=%p\n", priv->pin, callback);
|
|
|
|
|
|
|
|
/* Detach and disable any current interrupt on the pin. */
|
|
|
|
|
|
|
|
if (priv->handle != NULL)
|
|
|
|
{
|
2016-08-01 16:49:15 +02:00
|
|
|
gpioinfo("pin%u: Detaching handle %p\n", priv->pin, priv->handle);
|
2020-01-02 17:49:34 +01:00
|
|
|
IOEP_DETACH(priv->ioe, priv->handle);
|
2016-08-01 16:43:10 +02:00
|
|
|
priv->handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the callback function pointer for use when the pin interrupt
|
|
|
|
* is enabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->callback = callback;
|
|
|
|
return OK;
|
|
|
|
}
|
2016-08-01 17:06:58 +02:00
|
|
|
#endif
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gplh_enable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Enable or disable the I/O expander pin interrupt
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2016-08-01 16:43:10 +02:00
|
|
|
static int gplh_enable(FAR struct gpio_dev_s *gpio, bool enable)
|
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv = (FAR struct gplh_dev_s *)gpio;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->ioe != NULL);
|
|
|
|
|
2016-08-01 16:49:15 +02:00
|
|
|
gpioinfo("pin%u: %s callback=%p handle=%p\n",
|
|
|
|
priv->pin, enable ? "Enabling" : "Disabling",
|
|
|
|
priv->callback, priv->handle);
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
/* Are we enabling or disabling the pin interrupt? */
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
/* We are enabling the pin interrupt. Make certain that there is
|
|
|
|
* an interrupt handler already attached.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->callback == NULL)
|
|
|
|
{
|
|
|
|
/* No callback has been attached */
|
|
|
|
|
|
|
|
gpiowarn("WARNING: pin%u: Attempt to enable before attaching\n",
|
|
|
|
priv->pin);
|
|
|
|
ret = -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the interrupt is already attached and enabled */
|
|
|
|
|
|
|
|
else if (priv->handle == NULL)
|
|
|
|
{
|
2018-08-27 17:25:11 +02:00
|
|
|
#if CONFIG_IOEXPANDER_NPINS <= 64
|
2016-08-01 16:43:10 +02:00
|
|
|
ioe_pinset_t pinset = ((ioe_pinset_t)1 << priv->pin);
|
2018-08-27 17:25:11 +02:00
|
|
|
#else
|
|
|
|
ioe_pinset_t pinset = ((ioe_pinset_t)priv->pin);
|
|
|
|
#endif
|
2016-08-01 16:43:10 +02:00
|
|
|
|
|
|
|
/* We have a callback and the callback is not yet attached.
|
|
|
|
* do it now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
gpioinfo("pin%u: Attaching %p\n", priv->pin, priv->callback);
|
|
|
|
|
|
|
|
priv->handle = IOEP_ATTACH(priv->ioe, pinset, gplh_handler, priv);
|
|
|
|
if (priv->handle == NULL)
|
|
|
|
{
|
|
|
|
gpioerr("ERROR: pin%u: IOEP_ATTACH() failed\n", priv->pin);
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check if we are already detached */
|
|
|
|
|
|
|
|
if (priv->handle == NULL)
|
|
|
|
{
|
|
|
|
gpiowarn("WARNING: pin%u: Already detached\n", priv->pin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gpioinfo("pin%u: Detaching handle=%p\n", priv->pin, priv->handle);
|
|
|
|
ret = IOEP_DETACH(priv->ioe, priv->handle);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-12-27 04:02:47 +01:00
|
|
|
gpioerr("ERROR: pin%u: IOEP_DETACH() failed %d\n",
|
2016-08-01 16:43:10 +02:00
|
|
|
priv->pin, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are no longer attached */
|
|
|
|
|
|
|
|
priv->handle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-01 17:06:58 +02:00
|
|
|
#endif
|
2016-08-01 16:43:10 +02:00
|
|
|
|
2018-08-27 17:25:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: gplh_setpintype
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set I/O expander pin to an appointed gpiopintype
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-02 12:47:58 +02:00
|
|
|
static int gplh_setpintype(FAR struct gpio_dev_s *gpio,
|
|
|
|
enum gpio_pintype_e pintype)
|
2018-08-27 17:25:11 +02:00
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv = (FAR struct gplh_dev_s *)gpio;
|
|
|
|
FAR struct ioexpander_dev_s *ioe = priv->ioe;
|
|
|
|
uint8_t pin = priv->pin;
|
|
|
|
|
|
|
|
if (pintype >= GPIO_NPINTYPES)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
else if (pintype == GPIO_OUTPUT_PIN)
|
|
|
|
{
|
|
|
|
IOEXP_SETDIRECTION(ioe, pin, IOEXPANDER_DIRECTION_OUT);
|
|
|
|
}
|
2020-07-02 12:47:58 +02:00
|
|
|
else if (pintype == GPIO_OUTPUT_PIN_OPENDRAIN)
|
|
|
|
{
|
|
|
|
IOEXP_SETDIRECTION(ioe, pin, IOEXPANDER_DIRECTION_OUT_OPENDRAIN);
|
|
|
|
}
|
2018-08-27 17:25:11 +02:00
|
|
|
else
|
|
|
|
{
|
2020-07-02 12:47:58 +02:00
|
|
|
if (pintype == GPIO_INPUT_PIN)
|
|
|
|
{
|
|
|
|
IOEXP_SETDIRECTION(ioe, pin, IOEXPANDER_DIRECTION_IN);
|
|
|
|
}
|
|
|
|
else if (pintype == GPIO_INPUT_PIN_PULLUP)
|
|
|
|
{
|
|
|
|
IOEXP_SETDIRECTION(ioe, pin, IOEXPANDER_DIRECTION_IN_PULLUP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IOEXP_SETDIRECTION(ioe, pin, IOEXPANDER_DIRECTION_IN_PULLDOWN);
|
|
|
|
}
|
|
|
|
|
2018-08-27 17:25:11 +02:00
|
|
|
IOEXP_SETOPTION(ioe, pin, IOEXPANDER_OPTION_INTCFG,
|
2022-01-07 03:33:25 +01:00
|
|
|
(FAR void *)(uintptr_t)g_gplh_inttype[pintype]);
|
2023-11-01 10:01:15 +01:00
|
|
|
|
|
|
|
IOEXP_SETOPTION(ioe, pin, IOEXPANDER_OPTION_WAKEUPCFG,
|
|
|
|
(FAR void *)(uintptr_t)g_gplh_wakeuptype[pintype]);
|
2018-08-27 17:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gpio->gp_pintype = pintype;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-08-01 16:43:10 +02:00
|
|
|
/****************************************************************************
|
2023-08-02 11:25:29 +02:00
|
|
|
* Name: gpio_lower_half_internal
|
2016-08-01 16:43:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2023-08-02 11:25:29 +02:00
|
|
|
* Internal handler for gpio_lower_half and gpio_lower_half_byname
|
|
|
|
* functions. Initializes gplh_dev_s structure and sets pin type.
|
2016-08-01 16:43:10 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-02 11:25:29 +02:00
|
|
|
static FAR struct gplh_dev_s *
|
|
|
|
gpio_lower_half_internal(FAR struct ioexpander_dev_s *ioe,
|
|
|
|
unsigned int pin,
|
|
|
|
enum gpio_pintype_e pintype)
|
2016-08-01 16:43:10 +02:00
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv;
|
|
|
|
FAR struct gpio_dev_s *gpio;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(ioe != NULL && pin < CONFIG_IOEXPANDER_NPINS &&
|
|
|
|
(unsigned int)pintype < GPIO_NPINTYPES);
|
|
|
|
|
2016-08-01 17:06:58 +02:00
|
|
|
#ifndef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
/* If there is no I/O expander interrupt support, then we cannot handle
|
|
|
|
* interrupting pin types.
|
|
|
|
*/
|
|
|
|
|
2019-09-09 15:36:17 +02:00
|
|
|
DEBUGASSERT(pintype < GPIO_INTERRUPT_PIN);
|
2016-08-01 17:06:58 +02:00
|
|
|
#endif
|
|
|
|
|
2016-08-01 16:43:10 +02:00
|
|
|
/* Allocate an new instance of the GPIO lower half driver */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct gplh_dev_s));
|
2016-08-01 16:43:10 +02:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2023-08-02 11:25:29 +02:00
|
|
|
gpioerr("ERROR: Failed to allocate driver state %d\n", -ENOMEM);
|
|
|
|
return NULL;
|
2016-08-01 16:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the non-zero elements of the newly allocated instance */
|
|
|
|
|
2016-08-02 16:37:30 +02:00
|
|
|
priv->pin = (uint8_t)pin;
|
2016-08-03 20:46:54 +02:00
|
|
|
priv->ioe = ioe;
|
2016-08-02 16:37:30 +02:00
|
|
|
gpio = &priv->gpio;
|
|
|
|
gpio->gp_ops = &g_gplh_ops;
|
2016-08-01 16:43:10 +02:00
|
|
|
|
2019-09-09 15:36:17 +02:00
|
|
|
/* Set pintype */
|
|
|
|
|
|
|
|
ret = gplh_setpintype(gpio, pintype);
|
|
|
|
if (ret < 0)
|
2018-08-27 17:25:11 +02:00
|
|
|
{
|
2019-09-09 15:36:17 +02:00
|
|
|
gpioerr("ERROR: gplh_setpintype() failed: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
2023-08-02 11:25:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_lower_half_byname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a GPIO pin device driver instance for an I/O expander pin.
|
|
|
|
* The I/O expander pin must have already been configured by the caller
|
|
|
|
* for the particular pintype.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ioe - An instance of the I/O expander interface
|
|
|
|
* pin - The I/O expander pin number for the driver
|
|
|
|
* pintype - See enum gpio_pintype_e
|
|
|
|
* name - gpio device name
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int gpio_lower_half_byname(FAR struct ioexpander_dev_s *ioe,
|
|
|
|
unsigned int pin, enum gpio_pintype_e pintype,
|
|
|
|
FAR char *name)
|
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv;
|
|
|
|
FAR struct gpio_dev_s *gpio;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(name != NULL);
|
|
|
|
|
|
|
|
/* Initialize gplh_dev_s structure and set pin type */
|
|
|
|
|
|
|
|
priv = gpio_lower_half_internal(ioe, pin, pintype);
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio = &priv->gpio;
|
|
|
|
gpio->gp_ops = &g_gplh_ops;
|
|
|
|
|
|
|
|
/* Register GPIO device by name */
|
|
|
|
|
|
|
|
ret = gpio_pin_register_byname(gpio, name);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
gpioerr("ERROR: gpio_pin_register_byname() failed: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_lower_half
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a GPIO pin device driver instance for an I/O expander pin.
|
|
|
|
* The I/O expander pin must have already been configured by the caller
|
|
|
|
* for the particular pintype.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ioe - An instance of the I/O expander interface
|
|
|
|
* pin - The I/O expander pin number for the driver
|
|
|
|
* pintype - See enum gpio_pintype_e
|
|
|
|
* minor - The minor device number to use when registering the device,
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int gpio_lower_half(FAR struct ioexpander_dev_s *ioe, unsigned int pin,
|
|
|
|
enum gpio_pintype_e pintype, int minor)
|
|
|
|
{
|
|
|
|
FAR struct gplh_dev_s *priv;
|
|
|
|
FAR struct gpio_dev_s *gpio;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize gplh_dev_s structure and set pin type */
|
|
|
|
|
|
|
|
priv = gpio_lower_half_internal(ioe, pin, pintype);
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
2018-08-27 17:25:11 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 11:25:29 +02:00
|
|
|
gpio = &priv->gpio;
|
|
|
|
gpio->gp_ops = &g_gplh_ops;
|
|
|
|
|
2016-08-01 16:43:10 +02:00
|
|
|
/* Register the GPIO driver */
|
|
|
|
|
|
|
|
ret = gpio_pin_register(gpio, minor);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
gpioerr("ERROR: gpio_pin_register() failed: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_GPIO_LOWER_HALF */
|