mirror of
https://github.com/apache/nuttx.git
synced 2026-05-30 21:36:28 +08:00
ioexpander: add option to register GPIO by name for IOexpander
Function gpio_lower_half_byname() was added to support registration of GPIO pin by name as supported for IOexpander. Some of the code is wrapped in new static function gpio_lower_half_internal() to avoid code duplication. Signed-off-by: Michal Lenc <michallenc@seznam.cz>
This commit is contained in:
@@ -364,30 +364,18 @@ static int gplh_setpintype(FAR struct gpio_dev_s *gpio,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Name: gpio_lower_half_internal
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: gpio_lower_half
|
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Create a GPIO pin device driver instance for an I/O expander pin.
|
* Internal handler for gpio_lower_half and gpio_lower_half_byname
|
||||||
* The I/O expander pin must have already been configured by the caller
|
* functions. Initializes gplh_dev_s structure and sets pin type.
|
||||||
* 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,
|
static FAR struct gplh_dev_s *
|
||||||
enum gpio_pintype_e pintype, int minor)
|
gpio_lower_half_internal(FAR struct ioexpander_dev_s *ioe,
|
||||||
|
unsigned int pin,
|
||||||
|
enum gpio_pintype_e pintype)
|
||||||
{
|
{
|
||||||
FAR struct gplh_dev_s *priv;
|
FAR struct gplh_dev_s *priv;
|
||||||
FAR struct gpio_dev_s *gpio;
|
FAR struct gpio_dev_s *gpio;
|
||||||
@@ -409,8 +397,8 @@ int gpio_lower_half(FAR struct ioexpander_dev_s *ioe, unsigned int pin,
|
|||||||
priv = (FAR struct gplh_dev_s *)kmm_zalloc(sizeof(struct gplh_dev_s));
|
priv = (FAR struct gplh_dev_s *)kmm_zalloc(sizeof(struct gplh_dev_s));
|
||||||
if (priv == NULL)
|
if (priv == NULL)
|
||||||
{
|
{
|
||||||
gpioerr("ERROR: Failed to allocate driver state\n");
|
gpioerr("ERROR: Failed to allocate driver state %d\n", -ENOMEM);
|
||||||
return -ENOMEM;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the non-zero elements of the newly allocated instance */
|
/* Initialize the non-zero elements of the newly allocated instance */
|
||||||
@@ -427,9 +415,105 @@ int gpio_lower_half(FAR struct ioexpander_dev_s *ioe, unsigned int pin,
|
|||||||
{
|
{
|
||||||
gpioerr("ERROR: gplh_setpintype() failed: %d\n", ret);
|
gpioerr("ERROR: gplh_setpintype() failed: %d\n", ret);
|
||||||
kmm_free(priv);
|
kmm_free(priv);
|
||||||
return ret;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
gpio = &priv->gpio;
|
||||||
|
gpio->gp_ops = &g_gplh_ops;
|
||||||
|
|
||||||
/* Register the GPIO driver */
|
/* Register the GPIO driver */
|
||||||
|
|
||||||
ret = gpio_pin_register(gpio, minor);
|
ret = gpio_pin_register(gpio, minor);
|
||||||
|
|||||||
@@ -271,6 +271,32 @@ int gpio_pin_unregister(FAR struct gpio_dev_s *dev, int minor);
|
|||||||
int gpio_pin_unregister_byname(FAR struct gpio_dev_s *dev,
|
int gpio_pin_unregister_byname(FAR struct gpio_dev_s *dev,
|
||||||
FAR const char *pinname);
|
FAR const char *pinname);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifdef CONFIG_GPIO_LOWER_HALF
|
||||||
|
struct ioexpander_dev_s;
|
||||||
|
int gpio_lower_half_byname(FAR struct ioexpander_dev_s *ioe,
|
||||||
|
unsigned int pin, enum gpio_pintype_e pintype,
|
||||||
|
FAR char *name);
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: gpio_lower_half
|
* Name: gpio_lower_half
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user