2016-08-05 19:07:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/spi/spi_driver.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-05 19:07:35 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-05 19:07:35 +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-05 19:07:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2016-08-05 19:07:35 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2016-08-05 19:07:35 +02:00
|
|
|
#include <nuttx/spi/spi_transfer.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPI_DRIVER
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Device naming ************************************************************/
|
|
|
|
|
|
|
|
#define DEVNAME_FMT "/dev/spi%d"
|
|
|
|
#define DEVNAME_FMTLEN (8 + 3 + 1)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Driver state structure */
|
|
|
|
|
|
|
|
struct spi_driver_s
|
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi; /* Contained SPI lower half driver */
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock; /* Mutual exclusion */
|
2016-08-05 19:07:35 +02:00
|
|
|
int16_t crefs; /* Number of open references */
|
|
|
|
bool unlinked; /* True, driver has been unlinked */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-01 15:36:23 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2016-08-05 19:07:35 +02:00
|
|
|
static int spidrvr_open(FAR struct file *filep);
|
|
|
|
static int spidrvr_close(FAR struct file *filep);
|
2018-03-01 15:36:23 +01:00
|
|
|
#endif
|
2016-08-05 19:07:35 +02:00
|
|
|
static ssize_t spidrvr_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t spidrvr_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int spidrvr_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int spidrvr_unlink(FAR struct inode *inode);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations spidrvr_fops =
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
spidrvr_open, /* open */
|
|
|
|
spidrvr_close, /* close */
|
|
|
|
#else
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2016-08-05 19:07:35 +02:00
|
|
|
#endif
|
|
|
|
spidrvr_read, /* read */
|
|
|
|
spidrvr_write, /* write */
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* seek */
|
|
|
|
spidrvr_ioctl, /* ioctl */
|
|
|
|
NULL /* poll */
|
2016-08-05 19:07:35 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, spidrvr_unlink /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spidrvr_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int spidrvr_open(FAR struct file *filep)
|
|
|
|
{
|
2018-03-01 15:36:23 +01:00
|
|
|
FAR struct inode *inode;
|
2016-08-05 19:07:35 +02:00
|
|
|
FAR struct spi_driver_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get our private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
priv = (FAR struct spi_driver_s *)inode->i_private;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Get exclusive access to the SPI driver state structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-08-05 19:07:35 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 15:36:23 +01:00
|
|
|
/* Increment the count of open references on the driver */
|
2016-08-05 19:07:35 +02:00
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
DEBUGASSERT(priv->crefs > 0);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spidrvr_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int spidrvr_close(FAR struct file *filep)
|
|
|
|
{
|
2018-03-01 15:36:23 +01:00
|
|
|
FAR struct inode *inode;
|
2016-08-05 19:07:35 +02:00
|
|
|
FAR struct spi_driver_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get our private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
priv = (FAR struct spi_driver_s *)inode->i_private;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Get exclusive access to the SPI driver state structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-08-05 19:07:35 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 15:36:23 +01:00
|
|
|
/* Decrement the count of open references on the driver */
|
2016-08-05 19:07:35 +02: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)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spidrvr_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t spidrvr_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return 0; /* Return EOF */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spidrvr_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t spidrvr_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
return len; /* Say that everything was written */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spidrvr_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int spidrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2018-03-01 15:36:23 +01:00
|
|
|
FAR struct inode *inode;
|
2016-08-05 19:07:35 +02:00
|
|
|
FAR struct spi_driver_s *priv;
|
|
|
|
FAR struct spi_sequence_s *seq;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
spiinfo("cmd=%d arg=%lu\n", cmd, arg);
|
|
|
|
|
|
|
|
/* Get our private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
priv = (FAR struct spi_driver_s *)inode->i_private;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Get exclusive access to the SPI driver state structure */
|
|
|
|
|
2018-03-01 15:36:23 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-08-05 19:07:35 +02:00
|
|
|
}
|
2018-03-01 15:36:23 +01:00
|
|
|
#endif
|
2016-08-05 19:07:35 +02:00
|
|
|
|
|
|
|
/* Process the IOCTL command */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Command: SPIIOC_TRANSFER
|
|
|
|
* Description: Perform a sequence of SPI transfers
|
|
|
|
* Argument: A reference to an instance of struct spi_sequence_s.
|
|
|
|
* Dependencies: CONFIG_SPI_DRIVER
|
|
|
|
*/
|
|
|
|
|
|
|
|
case SPIIOC_TRANSFER:
|
|
|
|
{
|
|
|
|
/* Get the reference to the spi_transfer_s structure */
|
|
|
|
|
|
|
|
seq = (FAR struct spi_sequence_s *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(seq != NULL);
|
|
|
|
|
|
|
|
/* Perform the transfer */
|
|
|
|
|
|
|
|
ret = spi_transfer(priv->spi, seq);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-01 15:36:23 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2018-03-01 15:36:23 +01:00
|
|
|
#endif
|
2016-08-05 19:07:35 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spidrvr_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int spidrvr_unlink(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct spi_driver_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get our private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(inode != NULL && inode->i_private != NULL);
|
|
|
|
priv = (FAR struct spi_driver_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the SPI driver state structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2016-08-05 19:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Are there open references to the driver data structure? */
|
|
|
|
|
|
|
|
if (priv->crefs <= 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-02-25 14:34:37 +01:00
|
|
|
/* No... just mark the driver as unlinked and free the resources when the
|
2016-08-05 19:07:35 +02:00
|
|
|
* last client closes their reference to the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->unlinked = true;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: spi_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create and register the SPI character driver.
|
|
|
|
*
|
|
|
|
* The SPI character driver is a simple character driver that supports SPI
|
|
|
|
* transfers. The intent of this driver is to support SPI testing. It is
|
|
|
|
* not suitable for use in any real driver application.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* spi - An instance of the lower half SPI driver
|
|
|
|
* bus - The SPI bus number. This will be used as the SPI device minor
|
|
|
|
* number. The SPI character device will be registered as /dev/spiN
|
|
|
|
* 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 spi_register(FAR struct spi_dev_s *spi, int bus)
|
|
|
|
{
|
|
|
|
FAR struct spi_driver_s *priv;
|
|
|
|
char devname[DEVNAME_FMTLEN];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(spi != NULL && (unsigned)bus < 1000);
|
|
|
|
|
|
|
|
/* Allocate a SPI character device structure */
|
|
|
|
|
|
|
|
priv = (FAR struct spi_driver_s *)kmm_zalloc(sizeof(struct spi_driver_s));
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the SPI character device structure */
|
|
|
|
|
|
|
|
priv->spi = spi;
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->lock);
|
2016-08-05 19:07:35 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create the character device name */
|
|
|
|
|
|
|
|
snprintf(devname, DEVNAME_FMTLEN, DEVNAME_FMT, bus);
|
|
|
|
ret = register_driver(devname, &spidrvr_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Free the device structure if we failed to create the character
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
2022-10-16 18:50:59 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
nxmutex_destroy(&priv->lock);
|
|
|
|
#endif
|
2016-08-05 19:07:35 +02:00
|
|
|
kmm_free(priv);
|
2019-11-04 02:31:49 +01:00
|
|
|
return ret;
|
2016-08-05 19:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the result of the registration */
|
|
|
|
|
2016-08-06 16:07:30 +02:00
|
|
|
return ret;
|
2016-08-05 19:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SPI_DRIVER */
|