2016-06-24 17:43:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/ioexpander/gpio.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
2016-07-23 21:53:06 +02:00
|
|
|
#include <signal.h>
|
2016-06-24 17:43:49 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/ioexpander/gpio.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEV_GPIO
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
static int gpio_handler(FAR struct gpio_dev_s *dev);
|
2016-06-24 17:43:49 +02:00
|
|
|
static int gpio_open(FAR struct file *filep);
|
|
|
|
static int gpio_close(FAR struct file *filep);
|
|
|
|
static ssize_t gpio_read(FAR struct file *filep, FAR char *buffer,
|
2016-07-05 21:47:11 +02:00
|
|
|
size_t buflen);
|
2016-06-24 17:43:49 +02:00
|
|
|
static ssize_t gpio_write(FAR struct file *filep, FAR const char *buffer,
|
2016-07-05 21:47:11 +02:00
|
|
|
size_t buflen);
|
2016-06-24 17:43:49 +02:00
|
|
|
static int gpio_ioctl(FAR struct file *filep, int cmd,
|
2016-07-05 21:47:11 +02:00
|
|
|
unsigned long arg);
|
2016-06-24 17:43:49 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
static const struct file_operations g_gpio_drvrops =
|
2016-06-24 17:43:49 +02:00
|
|
|
{
|
|
|
|
gpio_open, /* open */
|
|
|
|
gpio_close, /* close */
|
2016-06-26 19:24:35 +02:00
|
|
|
gpio_read, /* read */
|
2016-06-24 17:43:49 +02:00
|
|
|
gpio_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
gpio_ioctl /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, NULL /* poll */
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_handler
|
|
|
|
*
|
|
|
|
* Description:
|
2016-07-24 20:48:37 +02:00
|
|
|
* GPIO interrupt callback function.
|
2016-07-23 21:53:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int gpio_handler(FAR struct gpio_dev_s *dev)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dev != NULL);
|
2016-07-23 21:53:37 +02:00
|
|
|
(void)kill(dev->gp_pid, dev->gp_signo);
|
2016-07-23 21:53:06 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-06-24 17:43:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver open method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int gpio_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver close method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int gpio_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver read method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t gpio_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver write method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t gpio_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return (ssize_t)buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: gpio_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver ioctl method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
2016-07-23 21:53:06 +02:00
|
|
|
FAR struct gpio_dev_s *dev;
|
2016-07-05 21:47:11 +02:00
|
|
|
int ret;
|
2016-06-24 17:43:49 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode->i_private != NULL);
|
|
|
|
dev = inode->i_private;
|
|
|
|
|
|
|
|
switch (cmd)
|
2016-07-05 21:47:11 +02:00
|
|
|
{
|
2016-07-23 21:53:06 +02:00
|
|
|
/* Command: GPIOC_WRITE
|
2016-07-05 21:47:11 +02:00
|
|
|
* Description: Set the value of an output GPIO
|
|
|
|
* Argument: 0=output a low value; 1=outut a high value
|
|
|
|
*/
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
case GPIOC_WRITE:
|
|
|
|
if (dev->gp_pintype == GPIO_OUTPUT_PIN)
|
2016-07-05 21:47:11 +02:00
|
|
|
{
|
2016-07-23 21:53:06 +02:00
|
|
|
DEBUGASSERT(arg == 0ul || arg == 1ul);
|
2016-08-01 16:43:10 +02:00
|
|
|
ret = dev->gp_ops->go_write(dev, (bool)arg);
|
2016-07-05 21:47:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EACCES;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
/* Command: GPIOC_READ
|
2016-07-05 21:47:11 +02:00
|
|
|
* Description: Read the value of an input or output GPIO
|
2016-08-01 16:43:10 +02:00
|
|
|
* Argument: A pointer to an bool value to receive the result:
|
|
|
|
* false=low value; true=high value.
|
2016-07-05 21:47:11 +02:00
|
|
|
*/
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
case GPIOC_READ:
|
2016-07-05 21:47:11 +02:00
|
|
|
{
|
2016-08-01 16:43:10 +02:00
|
|
|
FAR bool *ptr = (FAR bool *)((uintptr_t)arg);
|
2016-07-05 21:47:11 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
ret = dev->gp_ops->go_read(dev, ptr);
|
|
|
|
DEBUGASSERT(ret < 0 || *ptr == 0 || *ptr == 1);
|
|
|
|
}
|
|
|
|
break;
|
2016-07-05 21:47:11 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
/* Command: GPIOC_REGISTER
|
|
|
|
* Description: Register to receive a signal whenever there an
|
|
|
|
* interrupt is received on an input gpio pin. This
|
|
|
|
* feature, of course, depends upon interrupt GPIO
|
|
|
|
* support from the platform.
|
|
|
|
* Argument: The number of signal to be generated when the
|
|
|
|
* interrupt occurs.
|
|
|
|
*/
|
2016-07-05 21:47:11 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
case GPIOC_REGISTER:
|
|
|
|
if (dev->gp_pintype == GPIO_INTERRUPT_PIN)
|
|
|
|
{
|
|
|
|
/* Make sure that the pin interrupt is disabled */
|
2016-07-05 21:47:11 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
ret = dev->gp_ops->go_enable(dev, false);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Save signal information */
|
|
|
|
|
|
|
|
DEBUGASSERT(GOOD_SIGNO(arg));
|
|
|
|
|
|
|
|
dev->gp_pid = getpid();
|
|
|
|
dev->gp_signo = (uint8_t)arg;
|
|
|
|
|
|
|
|
/* Register our handler */
|
|
|
|
|
|
|
|
ret = dev->gp_ops->go_attach(dev,
|
|
|
|
(pin_interrupt_t)gpio_handler);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Enable pin interrupts */
|
|
|
|
|
|
|
|
ret = dev->gp_ops->go_enable(dev, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EACCES;
|
|
|
|
}
|
2016-07-05 21:47:11 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Unrecognized command */
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-24 17:43:49 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-07-23 21:53:06 +02:00
|
|
|
* Name: gpio_pin_register
|
2016-06-24 17:43:49 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-07-23 21:53:06 +02:00
|
|
|
* Register GPIO pin device driver.
|
2016-06-24 17:43:49 +02:00
|
|
|
*
|
2016-07-24 20:48:37 +02:00
|
|
|
* - Input pin types will be registered at /dev/gpinN
|
|
|
|
* - Output pin types will be registered at /dev/gpoutN
|
|
|
|
* - Interrupt pin types will be registered at /dev/gpintN
|
|
|
|
*
|
|
|
|
* Where N is the provided minor number in the range of 0-99.
|
|
|
|
*
|
2016-06-24 17:43:49 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor)
|
2016-06-24 17:43:49 +02:00
|
|
|
{
|
2016-07-23 21:53:06 +02:00
|
|
|
FAR const char *fmt;
|
2016-06-24 17:43:49 +02:00
|
|
|
char devname[16];
|
2016-07-23 21:53:06 +02:00
|
|
|
int ret;
|
2016-06-24 17:43:49 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
DEBUGASSERT(dev != NULL && dev->gp_ops != NULL && (unsigned int)minor < 100);
|
2016-06-24 17:43:49 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
switch (dev->gp_pintype)
|
|
|
|
{
|
|
|
|
case GPIO_INPUT_PIN:
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dev->gp_ops->go_read != NULL);
|
|
|
|
fmt = "/dev/gpin%u";
|
|
|
|
}
|
|
|
|
break;
|
2016-06-24 17:43:49 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
case GPIO_OUTPUT_PIN:
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dev->gp_ops->go_read != NULL &&
|
|
|
|
dev->gp_ops->go_write != NULL);
|
|
|
|
fmt = "/dev/gpout%u";
|
2016-07-31 19:09:02 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 17:43:49 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
case GPIO_INTERRUPT_PIN:
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dev->gp_ops->go_read != NULL &&
|
|
|
|
dev->gp_ops->go_attach != NULL &&
|
|
|
|
dev->gp_ops->go_enable != NULL);
|
2016-06-24 17:43:49 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
/* Make sure that the pin interrupt is disabled */
|
|
|
|
|
|
|
|
ret = dev->gp_ops->go_enable(dev, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-31 19:09:02 +02:00
|
|
|
fmt = "/dev/gpint%u";
|
2016-07-23 21:53:06 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-07-31 19:09:02 +02:00
|
|
|
return -EINVAL;
|
2016-07-23 21:53:06 +02:00
|
|
|
}
|
2016-06-24 17:43:49 +02:00
|
|
|
|
2016-07-23 21:53:06 +02:00
|
|
|
snprintf(devname, 16, fmt, (unsigned int)minor);
|
|
|
|
return register_driver(devname, &g_gpio_drvrops, 0666, dev);
|
2016-06-24 17:43:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DEV_GPIO */
|