2016-02-02 15:30:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/i2c/i2c_driver.c
|
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* Copyright (C) 2016-2017 Gregory Nutt. All rights reserved.
|
2016-02-02 15:30:54 +01:00
|
|
|
* 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 <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_I2C_DRIVER
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Device naming ************************************************************/
|
|
|
|
|
|
|
|
#define DEVNAME_FMT "/dev/i2c%d"
|
|
|
|
#define DEVNAME_FMTLEN (8 + 3 + 1)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Driver state structure */
|
|
|
|
|
|
|
|
struct i2c_driver_s
|
|
|
|
{
|
|
|
|
FAR struct i2c_master_s *i2c; /* Contained I2C lower half driver */
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
sem_t exclsem; /* Mutual exclusion */
|
|
|
|
int16_t crefs; /* Number of open references */
|
|
|
|
bool unlinked; /* True, driver has been unlinked */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-04-30 16:02:38 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2016-02-02 15:30:54 +01:00
|
|
|
static int i2cdrvr_open(FAR struct file *filep);
|
|
|
|
static int i2cdrvr_close(FAR struct file *filep);
|
2017-04-30 16:02:38 +02:00
|
|
|
#endif
|
2016-02-02 15:30:54 +01:00
|
|
|
static ssize_t i2cdrvr_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t i2cdrvr_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int i2cdrvr_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int i2cdrvr_unlink(FAR struct inode *inode);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations i2cdrvr_fops =
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
i2cdrvr_open, /* open */
|
|
|
|
i2cdrvr_close, /* close */
|
|
|
|
#else
|
|
|
|
0, /* open */
|
|
|
|
0, /* close */
|
|
|
|
#endif
|
|
|
|
i2cdrvr_read, /* read */
|
|
|
|
i2cdrvr_write, /* write */
|
|
|
|
0, /* seek */
|
|
|
|
i2cdrvr_ioctl /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, 0 /* poll */
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, i2cdrvr_unlink /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2cdrvr_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int i2cdrvr_open(FAR struct file *filep)
|
|
|
|
{
|
2018-03-01 15:36:23 +01:00
|
|
|
FAR struct inode *inode;
|
2016-02-02 15:30:54 +01:00
|
|
|
FAR struct i2c_driver_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get our private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
priv = (FAR struct i2c_driver_s *)inode->i_private;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Get exclusive access to the I2C driver state structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-02-02 15:30:54 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 15:36:23 +01:00
|
|
|
/* Increment the count of open references on the driver */
|
2016-02-02 15:30:54 +01:00
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
DEBUGASSERT(priv->crefs > 0);
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2cdrvr_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int i2cdrvr_close(FAR struct file *filep)
|
|
|
|
{
|
2018-03-01 15:36:23 +01:00
|
|
|
FAR struct inode *inode;
|
2016-02-02 15:30:54 +01:00
|
|
|
FAR struct i2c_driver_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get our private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
priv = (FAR struct i2c_driver_s *)inode->i_private;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Get exclusive access to the I2C driver state structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-02-02 15:30:54 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 15:36:23 +01:00
|
|
|
/* Decrement the count of open references on the driver */
|
2016-02-02 15:30:54 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs > 0);
|
|
|
|
priv->crefs--;
|
|
|
|
|
|
|
|
/* If the count has decremented to zero and the driver has been unlinked,
|
|
|
|
* then commit Hara-Kiri now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs <= 0 && priv->unlinked)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2cdrvr_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t i2cdrvr_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return 0; /* Return EOF */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2cdrvr_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t i2cdrvr_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return len; /* Say that everything was written */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2cdrvr_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int i2cdrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2018-03-01 15:36:23 +01:00
|
|
|
FAR struct inode *inode;
|
2016-02-03 14:32:25 +01:00
|
|
|
FAR struct i2c_driver_s *priv;
|
|
|
|
FAR struct i2c_transfer_s *transfer;
|
|
|
|
int ret;
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
i2cinfo("cmd=%d arg=%lu\n", cmd, arg);
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
/* Get our private data structure */
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
priv = (FAR struct i2c_driver_s *)inode->i_private;
|
|
|
|
DEBUGASSERT(priv);
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2017-04-30 16:02:38 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2016-02-03 14:32:25 +01:00
|
|
|
/* Get exclusive access to the I2C driver state structure */
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2016-02-03 14:32:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-02-03 14:32:25 +01:00
|
|
|
}
|
2017-04-30 16:02:38 +02:00
|
|
|
#endif
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
/* Process the IOCTL command */
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Command: I2CIOC_TRANSFER
|
|
|
|
* Description: Perform an I2C transfer
|
|
|
|
* Argument: A reference to an instance of struct i2c_transfer_s.
|
|
|
|
* Dependencies: CONFIG_I2C_DRIVER
|
|
|
|
*/
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
case I2CIOC_TRANSFER:
|
2016-02-02 15:30:54 +01:00
|
|
|
{
|
2016-02-03 14:32:25 +01:00
|
|
|
/* Get the reference to the i2c_transfer_s structure */
|
|
|
|
|
|
|
|
transfer = (FAR struct i2c_transfer_s *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(transfer != NULL);
|
|
|
|
|
|
|
|
/* Perform the transfer */
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, transfer->msgv, transfer->msgc);
|
2016-02-02 15:30:54 +01:00
|
|
|
}
|
2016-02-03 14:32:25 +01:00
|
|
|
break;
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
#ifdef CONFIG_I2C_RESET
|
|
|
|
/* Command: I2CIOC_RESET
|
|
|
|
* Description: Perform an I2C bus reset in an attempt to break loose
|
|
|
|
* stuck I2C devices.
|
|
|
|
* Argument: None
|
|
|
|
* Dependencies: CONFIG_I2C_DRIVER && CONFIG_I2C_RESET
|
|
|
|
*/
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
case I2CIOC_RESET:
|
|
|
|
{
|
|
|
|
ret = I2C_RESET(priv->i2c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2016-02-02 15:30:54 +01:00
|
|
|
|
2016-02-03 14:32:25 +01:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
2016-02-02 15:30:54 +01:00
|
|
|
}
|
2016-02-03 14:32:25 +01:00
|
|
|
|
2017-04-30 16:02:38 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2017-04-30 16:02:38 +02:00
|
|
|
#endif
|
2016-02-03 14:32:25 +01:00
|
|
|
return ret;
|
2016-02-02 15:30:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2cdrvr_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int i2cdrvr_unlink(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct i2c_driver_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get our private data structure */
|
|
|
|
|
2016-02-02 21:41:25 +01:00
|
|
|
DEBUGASSERT(inode != NULL && inode->i_private != NULL);
|
2016-02-02 15:30:54 +01:00
|
|
|
priv = (FAR struct i2c_driver_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the I2C driver state structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-02-02 15:30:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Are there open references to the driver data structure? */
|
|
|
|
|
|
|
|
if (priv->crefs <= 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No... just mark the driver as unlinked and free the resouces when the
|
|
|
|
* last client closes their reference to the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->unlinked = true;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2016-02-02 15:30:54 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create and register the I2C character driver.
|
|
|
|
*
|
|
|
|
* The I2C character driver is a simple character driver that supports I2C
|
|
|
|
* transfers. The intent of this driver is to support I2C testing. It is
|
|
|
|
* not suitable for use in any real driver application.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* i2c - An instance of the lower half I2C driver
|
|
|
|
* bus - The I2C bus number. This will be used as the I2C device minor
|
|
|
|
* number. The I2C character device will be registered as /dev/i2cN
|
|
|
|
* where N is the minor number
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK if the driver was successfully register; A negated errno value is
|
|
|
|
* returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int i2c_register(FAR struct i2c_master_s *i2c, int bus)
|
|
|
|
{
|
|
|
|
FAR struct i2c_driver_s *priv;
|
|
|
|
char devname[DEVNAME_FMTLEN];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(i2c != NULL && (unsigned)bus < 1000);
|
|
|
|
|
|
|
|
/* Allocate a I2C character device structure */
|
|
|
|
|
|
|
|
priv = (FAR struct i2c_driver_s *)kmm_zalloc(sizeof(struct i2c_driver_s));
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the I2C character device structure */
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->exclsem, 0, 1);
|
2016-02-02 15:30:54 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create the character device name */
|
|
|
|
|
|
|
|
snprintf(devname, DEVNAME_FMTLEN, DEVNAME_FMT, bus);
|
|
|
|
ret = register_driver(devname, &i2cdrvr_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Free the device structure if we failed to create the character
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the result of the registration */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2016-06-16 00:30:12 +02:00
|
|
|
#endif /* CONFIG_I2C_DRIVER */
|