2012-01-22 19:03:13 +01:00
|
|
|
/****************************************************************************
|
2015-09-19 19:00:14 +02:00
|
|
|
* drivers/power/battery_gauge_gauge.c
|
|
|
|
* Upper-half, character driver for battery fuel gauge.
|
2012-01-22 19:03:13 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 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>
|
|
|
|
|
2012-01-23 19:37:45 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <semaphore.h>
|
2012-01-22 19:03:13 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-09-19 19:00:14 +02:00
|
|
|
#include <nuttx/power/battery_gauge.h>
|
2015-09-19 21:58:49 +02:00
|
|
|
#include <nuttx/power/battery_ioctl.h>
|
2012-01-22 19:03:13 +01:00
|
|
|
|
|
|
|
/* This driver requires:
|
|
|
|
*
|
|
|
|
* CONFIG_BATTERY - Upper half battery driver support
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_BATTERY)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2015-09-19 19:00:14 +02:00
|
|
|
static int bat_gauge_open(FAR struct file *filep);
|
|
|
|
static int bat_gauge_close(FAR struct file *filep);
|
2015-10-10 18:41:00 +02:00
|
|
|
static ssize_t bat_gauge_read(FAR struct file *filep, FAR char *buflen,
|
|
|
|
size_t buflen);
|
2015-09-19 19:00:14 +02:00
|
|
|
static ssize_t bat_gauge_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int bat_gauge_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2012-01-22 19:03:13 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-23 19:37:45 +01:00
|
|
|
static const struct file_operations g_batteryops =
|
2012-01-22 19:03:13 +01:00
|
|
|
{
|
2015-09-19 19:00:14 +02:00
|
|
|
bat_gauge_open,
|
|
|
|
bat_gauge_close,
|
|
|
|
bat_gauge_read,
|
|
|
|
bat_gauge_write,
|
2012-01-22 19:03:13 +01:00
|
|
|
0,
|
2015-09-19 19:00:14 +02:00
|
|
|
bat_gauge_ioctl
|
2012-01-22 19:03:13 +01:00
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, 0
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2015-09-19 19:00:14 +02:00
|
|
|
* Name: bat_gauge_open
|
2012-01-22 19:03:13 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-22 20:22:51 +01:00
|
|
|
* This function is called whenever the battery device is opened.
|
2012-01-22 19:03:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-19 19:00:14 +02:00
|
|
|
static int bat_gauge_open(FAR struct file *filep)
|
2012-01-22 19:03:13 +01:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-09-19 19:00:14 +02:00
|
|
|
* Name: bat_gauge_close
|
2012-01-22 19:03:13 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-22 20:22:51 +01:00
|
|
|
* This routine is called when the battery device is closed.
|
2012-01-22 19:03:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-19 19:00:14 +02:00
|
|
|
static int bat_gauge_close(FAR struct file *filep)
|
2012-01-22 19:03:13 +01:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-09-19 19:00:14 +02:00
|
|
|
* Name: bat_gauge_read
|
2012-01-22 19:03:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-19 19:00:14 +02:00
|
|
|
static ssize_t bat_gauge_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
2012-01-22 19:03:13 +01:00
|
|
|
{
|
2012-01-22 20:22:51 +01:00
|
|
|
/* Return nothing read */
|
|
|
|
|
2012-01-22 19:03:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-09-19 19:00:14 +02:00
|
|
|
* Name: bat_gauge_write
|
2012-01-22 19:03:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-19 19:00:14 +02:00
|
|
|
static ssize_t bat_gauge_write(FAR struct file *filep, FAR const char *buffer,
|
2012-01-22 19:03:13 +01:00
|
|
|
size_t buflen)
|
|
|
|
{
|
2012-01-22 20:22:51 +01:00
|
|
|
/* Return nothing written */
|
|
|
|
|
|
|
|
return 0;
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-09-19 19:00:14 +02:00
|
|
|
* Name: bat_gauge_ioctl
|
2012-01-22 19:03:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-19 19:00:14 +02:00
|
|
|
static int bat_gauge_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2012-01-22 19:03:13 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2015-09-19 19:00:14 +02:00
|
|
|
FAR struct battery_gauge_dev_s *dev = inode->i_private;
|
2014-02-11 01:08:49 +01:00
|
|
|
int ret;
|
2012-01-22 19:03:13 +01:00
|
|
|
|
2012-01-22 20:22:51 +01:00
|
|
|
/* Inforce mutually exclusive access to the battery driver */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->batsem);
|
2012-01-22 20:22:51 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret; /* Probably -EINTR */
|
2012-01-22 20:22:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Procss the IOCTL command */
|
|
|
|
|
|
|
|
ret = -EINVAL; /* Assume a bad argument */
|
2012-01-22 19:03:13 +01:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case BATIOC_STATE:
|
|
|
|
{
|
2012-01-23 19:37:45 +01:00
|
|
|
FAR int *ptr = (FAR int *)((uintptr_t)arg);
|
2012-01-22 19:03:13 +01:00
|
|
|
if (ptr)
|
|
|
|
{
|
2012-01-23 18:19:43 +01:00
|
|
|
ret = dev->ops->state(dev, ptr);
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BATIOC_ONLINE:
|
2012-01-23 19:37:45 +01:00
|
|
|
{
|
|
|
|
FAR bool *ptr = (FAR bool *)((uintptr_t)arg);
|
2012-01-22 19:03:13 +01:00
|
|
|
if (ptr)
|
|
|
|
{
|
2012-01-23 18:19:43 +01:00
|
|
|
ret = dev->ops->online(dev, ptr);
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
2012-01-23 19:37:45 +01:00
|
|
|
}
|
2012-01-22 19:03:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BATIOC_VOLTAGE:
|
|
|
|
{
|
2012-01-23 19:37:45 +01:00
|
|
|
FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg);
|
2012-01-22 19:03:13 +01:00
|
|
|
if (ptr)
|
|
|
|
{
|
2012-01-23 18:19:43 +01:00
|
|
|
ret = dev->ops->voltage(dev, ptr);
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BATIOC_CAPACITY:
|
|
|
|
{
|
2012-01-23 19:37:45 +01:00
|
|
|
FAR b16_t *ptr = (FAR b16_t *)((uintptr_t)arg);
|
2012-01-22 19:03:13 +01:00
|
|
|
if (ptr)
|
|
|
|
{
|
2012-01-23 18:19:43 +01:00
|
|
|
ret = dev->ops->capacity(dev, ptr);
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-16 20:33:32 +02:00
|
|
|
_err("ERROR: Unrecognized cmd: %d\n", cmd);
|
2012-01-22 19:03:13 +01:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->batsem);
|
2012-01-22 19:03:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-09-19 19:00:14 +02:00
|
|
|
* Name: battery_gauge_register
|
2012-01-22 19:03:13 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a lower half battery driver with the common, upper-half
|
|
|
|
* battery driver.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2012-01-22 19:03:13 +01:00
|
|
|
* devpath - The location in the pseudo-filesystem to create the driver.
|
|
|
|
* Recommended standard is "/dev/bat0", "/dev/bat1", etc.
|
2012-01-22 20:22:51 +01:00
|
|
|
* dev - An instance of the battery state structure .
|
2012-01-22 19:03:13 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-01-22 19:03:13 +01:00
|
|
|
* Zero on success or a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-19 19:00:14 +02:00
|
|
|
int battery_gauge_register(FAR const char *devpath,
|
|
|
|
FAR struct battery_gauge_dev_s *dev)
|
2012-01-22 19:03:13 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
2012-01-22 20:22:51 +01:00
|
|
|
ret = register_driver(devpath, &g_batteryops, 0555, dev);
|
2012-01-22 19:03:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-16 20:33:32 +02:00
|
|
|
_err("ERROR: Failed to register driver: %d\n", ret);
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
2014-10-22 15:26:31 +02:00
|
|
|
|
2012-01-22 19:03:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_BATTERY */
|