* drivers/analog/opamp.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/fs/fs.h>
#include <nuttx/analog/opamp.h>
#include <nuttx/irq.h>
* Private Function Prototypes
****************************************************************************/
static int opamp_open(FAR struct file *filep);
static int opamp_close(FAR struct file *filep);
static int opamp_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
* Private Data
****************************************************************************/
static const struct file_operations g_opamp_fops =
{
opamp_open,
opamp_close,
NULL,
NULL,
NULL,
opamp_ioctl,
};
* Private Functions
****************************************************************************/
* Name: opamp_open
*
* Description:
* This function is called whenever the OPAMP device is opened.
*
****************************************************************************/
static int opamp_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct opamp_dev_s *dev = inode->i_private;
uint8_t tmp;
int ret;
* finished.
*/
ret = nxmutex_lock(&dev->ad_lock);
if (ret >= 0)
{
* first time that the driver has been opened for this device, then
* initialize the device.
*/
tmp = dev->ad_ocount + 1;
if (tmp == 0)
{
ret = -EMFILE;
}
else
{
* opened.
*/
if (tmp == 1)
{
ret = dev->ad_ops->ao_setup(dev);
if (ret == OK)
{
dev->ad_ocount = tmp;
}
}
}
nxmutex_unlock(&dev->ad_lock);
}
return ret;
}
* Name: opamp_close
*
* Description:
* This routine is called when the OPAMP device is closed.
* It waits for the last remaining data to be sent.
*
****************************************************************************/
static int opamp_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct opamp_dev_s *dev = inode->i_private;
int ret;
ret = nxmutex_lock(&dev->ad_lock);
if (ret >= 0)
{
* decrement to 0, then uninitialize the driver.
*/
if (dev->ad_ocount > 1)
{
dev->ad_ocount--;
nxmutex_unlock(&dev->ad_lock);
}
else
{
dev->ad_ocount = 0;
dev->ad_ops->ao_shutdown(dev);
nxmutex_unlock(&dev->ad_lock);
}
}
return ret;
}
* Name: opamp_ioctl
****************************************************************************/
static int opamp_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct opamp_dev_s *dev = inode->i_private;
int ret;
ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
return ret;
}
* Public Functions
****************************************************************************/
* Name: opamp_register
****************************************************************************/
int opamp_register(FAR const char *path, FAR struct opamp_dev_s *dev)
{
int ret;
dev->ad_ocount = 0;
nxmutex_init(&dev->ad_lock);
ret = register_driver(path, &g_opamp_fops, 0444, dev);
if (ret < 0)
{
nxmutex_destroy(&dev->ad_lock);
}
return ret;
}