2012-01-22 19:03:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/power/battery.c
|
|
|
|
* Upper-half, character driver for batteries.
|
|
|
|
*
|
|
|
|
* 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>
|
2012-01-22 19:03:13 +01:00
|
|
|
#include <nuttx/power/battery.h>
|
|
|
|
|
|
|
|
/* This driver requires:
|
|
|
|
*
|
|
|
|
* CONFIG_BATTERY - Upper half battery driver support
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_BATTERY)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static int bat_open(FAR struct file *filep);
|
|
|
|
static int bat_close(FAR struct file *filep);
|
2012-01-23 19:37:45 +01:00
|
|
|
static ssize_t bat_read(FAR struct file *, FAR char *, size_t nbytes);
|
2012-01-22 19:03:13 +01:00
|
|
|
static ssize_t bat_write(FAR struct file *filep, FAR const char *buffer, size_t buflen);
|
|
|
|
static int bat_ioctl(FAR struct file *filep,int cmd,unsigned long arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
{
|
|
|
|
bat_open,
|
|
|
|
bat_close,
|
|
|
|
bat_read,
|
|
|
|
bat_write,
|
|
|
|
0,
|
|
|
|
bat_ioctl
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
, 0
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bat_open
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bat_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bat_close
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bat_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bat_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t bat_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
|
|
|
|
{
|
2012-01-22 20:22:51 +01:00
|
|
|
/* Return nothing read */
|
|
|
|
|
2012-01-22 19:03:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bat_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t bat_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
2012-01-22 20:22:51 +01:00
|
|
|
/* Return nothing written */
|
|
|
|
|
|
|
|
return 0;
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bat_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bat_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2012-01-22 20:22:51 +01:00
|
|
|
FAR struct battery_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 */
|
|
|
|
|
|
|
|
ret = sem_wait(&dev->batsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return -errno; /* Probably EINTR */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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:
|
2012-01-22 20:22:51 +01:00
|
|
|
dbg("Unrecognized cmd: %d\n", cmd);
|
2012-01-22 19:03:13 +01:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-22 20:22:51 +01:00
|
|
|
sem_post(&dev->batsem);
|
2012-01-22 19:03:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: battery_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a lower half battery driver with the common, upper-half
|
|
|
|
* battery driver.
|
|
|
|
*
|
|
|
|
* Input parameters:
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* Returned value:
|
|
|
|
* Zero on success or a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-22 20:22:51 +01:00
|
|
|
int battery_register(FAR const char *devpath, FAR struct battery_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)
|
|
|
|
{
|
2012-01-22 20:22:51 +01:00
|
|
|
dbg("Failed to register driver: %d\n", ret);
|
2012-01-22 19:03:13 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_BATTERY */
|