212 lines
6.1 KiB
C
212 lines
6.1 KiB
C
|
/***************************************************************************
|
||
|
* Copyright (C) 05/2009 by Olaf Rempel *
|
||
|
* razzor@kopf-tisch.de *
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; version 2 of the License *
|
||
|
* *
|
||
|
* This program is distributed in the hope that it will be useful, *
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||
|
* GNU General Public License for more details. *
|
||
|
* *
|
||
|
* You should have received a copy of the GNU General Public License *
|
||
|
* along with this program; if not, write to the *
|
||
|
* Free Software Foundation, Inc., *
|
||
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||
|
***************************************************************************/
|
||
|
#include <unistd.h>
|
||
|
#include <stdint.h>
|
||
|
|
||
|
#include <fcntl.h>
|
||
|
#include <sys/ioctl.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <sys/types.h>
|
||
|
|
||
|
#include "i2c-dev.h"
|
||
|
|
||
|
#include "configfile.h"
|
||
|
#include "logging.h"
|
||
|
#include "usvdevice.h"
|
||
|
|
||
|
/* eeprom parameters / thresholds & defaults */
|
||
|
#include "../eeprom.h"
|
||
|
|
||
|
enum {
|
||
|
REG_STATUS = 0x00,
|
||
|
|
||
|
REG_CURRENT = 0x10,
|
||
|
REG_UBAT = 0x11,
|
||
|
REG_UIN = 0x12,
|
||
|
|
||
|
REG_UIN_LOSS = 0x20,
|
||
|
REG_UIN_RESTORE = 0x21,
|
||
|
REG_UBAT_FULL = 0x22,
|
||
|
REG_UBAT_LOW = 0x23,
|
||
|
REG_UBAT_CRIT = 0x24,
|
||
|
REG_IBAT_FULL = 0x25,
|
||
|
REG_CRC = 0x26,
|
||
|
};
|
||
|
|
||
|
static const char *i2c_device;
|
||
|
static int i2c_address;
|
||
|
|
||
|
static int _usvdev_open(void)
|
||
|
{
|
||
|
int fd = open(i2c_device, O_RDWR);
|
||
|
if (fd < 0)
|
||
|
return -1;
|
||
|
|
||
|
unsigned long funcs;
|
||
|
if (ioctl(fd, I2C_FUNCS, &funcs)) {
|
||
|
close(fd);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if ((funcs & I2C_FUNC_SMBUS_WORD_DATA) != I2C_FUNC_SMBUS_WORD_DATA) {
|
||
|
close(fd);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (ioctl(fd, I2C_SLAVE, i2c_address) < 0) {
|
||
|
close(fd);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return fd;
|
||
|
}
|
||
|
|
||
|
static int usvdev_open(void)
|
||
|
{
|
||
|
static int failcnt = 0;
|
||
|
|
||
|
int dev = _usvdev_open();
|
||
|
if (dev < 0) {
|
||
|
if (failcnt++ == 0)
|
||
|
log_print(LOG_WARN, "failed to open %s:0x%02x", i2c_device, i2c_address);
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (failcnt != 0) {
|
||
|
log_print(LOG_WARN, "opened %s:0x%02x after %d failed attempts",
|
||
|
i2c_device, i2c_address, failcnt);
|
||
|
|
||
|
failcnt = 0;
|
||
|
}
|
||
|
return dev;
|
||
|
}
|
||
|
|
||
|
int usvdev_getstatus(struct usvdev_status *status)
|
||
|
{
|
||
|
int dev = usvdev_open();
|
||
|
if (dev < 0)
|
||
|
return -1;
|
||
|
|
||
|
status->state = i2c_smbus_read_word_data(dev, REG_STATUS);
|
||
|
status->ibat = i2c_smbus_read_word_data(dev, REG_CURRENT);
|
||
|
status->ubat = i2c_smbus_read_word_data(dev, REG_UBAT);
|
||
|
status->uin = i2c_smbus_read_word_data(dev, REG_UIN);
|
||
|
|
||
|
close(dev);
|
||
|
|
||
|
if (status->state < 0 || status->ibat < 0 || status->ubat < 0 || status->uin < 0) {
|
||
|
status->state = -1;
|
||
|
status->ibat = 0;
|
||
|
status->ubat = 0;
|
||
|
status->uin = 0;
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int usvdev_setstate(int state)
|
||
|
{
|
||
|
int dev = usvdev_open();
|
||
|
if (dev < 0)
|
||
|
return -1;
|
||
|
|
||
|
int retval = i2c_smbus_write_word_data(dev, REG_STATUS, state);
|
||
|
|
||
|
close(dev);
|
||
|
return retval;
|
||
|
}
|
||
|
|
||
|
int usvdev_init(void)
|
||
|
{
|
||
|
i2c_device = config_get_string("global", "i2c-device", "/dev/i2c-0");
|
||
|
i2c_address = config_get_int("global", "i2c-address" , 16);
|
||
|
|
||
|
int dev = usvdev_open();
|
||
|
if (dev < 0) {
|
||
|
log_print(LOG_WARN, "could not check thresholds");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
struct ee_param defaults = DEFAULT_PARAMETERS;
|
||
|
|
||
|
struct ee_param config;
|
||
|
config.uin_loss = config_get_int("thresholds", "uin_loss" , defaults.uin_loss);
|
||
|
config.uin_restore = config_get_int("thresholds", "uin_restore" , defaults.uin_restore);
|
||
|
config.ubat_full = config_get_int("thresholds", "ubat_full" , defaults.ubat_full);
|
||
|
config.ubat_low = config_get_int("thresholds", "ubat_low" , defaults.ubat_low);
|
||
|
config.ubat_critical = config_get_int("thresholds", "ubat_critical" , defaults.ubat_critical);
|
||
|
config.ibat_full = config_get_int("thresholds", "ibat_full" , defaults.ibat_full);
|
||
|
|
||
|
struct ee_param device;
|
||
|
device.uin_loss = i2c_smbus_read_word_data(dev, REG_UIN_LOSS);
|
||
|
device.uin_restore = i2c_smbus_read_word_data(dev, REG_UIN_RESTORE);
|
||
|
device.ubat_full = i2c_smbus_read_word_data(dev, REG_UBAT_FULL);
|
||
|
device.ubat_low = i2c_smbus_read_word_data(dev, REG_UBAT_LOW);
|
||
|
device.ubat_critical = i2c_smbus_read_word_data(dev, REG_UBAT_CRIT);
|
||
|
device.ibat_full = i2c_smbus_read_word_data(dev, REG_IBAT_FULL);
|
||
|
device.crc16 = 0x0000;
|
||
|
|
||
|
int changed = 0;
|
||
|
|
||
|
if (device.uin_loss != config.uin_loss) {
|
||
|
log_print(LOG_INFO, "update UIN_LOSS: %d => %d", device.uin_loss, config.uin_loss);
|
||
|
i2c_smbus_write_word_data(dev, REG_UIN_LOSS, config.uin_loss);
|
||
|
changed = 1;
|
||
|
}
|
||
|
|
||
|
if (device.uin_restore != config.uin_restore) {
|
||
|
log_print(LOG_INFO, "update UIN_RESTORE: %d => %d", device.uin_restore, config.uin_restore);
|
||
|
i2c_smbus_write_word_data(dev, REG_UIN_RESTORE, config.uin_restore);
|
||
|
changed = 1;
|
||
|
}
|
||
|
|
||
|
if (device.ubat_full != config.ubat_full) {
|
||
|
log_print(LOG_INFO, "update UBAT_FULL: %d => %d", device.ubat_full, config.ubat_full);
|
||
|
i2c_smbus_write_word_data(dev, REG_UBAT_FULL, config.ubat_full);
|
||
|
changed = 1;
|
||
|
}
|
||
|
|
||
|
if (device.ubat_low != config.ubat_low) {
|
||
|
log_print(LOG_INFO, "update UBAT_LOW: %d => %d", device.ubat_low, config.ubat_low);
|
||
|
i2c_smbus_write_word_data(dev, REG_UBAT_LOW, config.ubat_low);
|
||
|
changed = 1;
|
||
|
}
|
||
|
|
||
|
if (device.ubat_critical != config.ubat_critical) {
|
||
|
log_print(LOG_INFO, "update UBAT_CRIT: %d => %d", device.ubat_critical, config.ubat_critical);
|
||
|
i2c_smbus_write_word_data(dev, REG_UBAT_CRIT, config.ubat_critical);
|
||
|
changed = 1;
|
||
|
}
|
||
|
|
||
|
if (device.ibat_full != config.ibat_full) {
|
||
|
log_print(LOG_INFO, "update IBAT_FULL: %d => %d", device.ibat_full, config.ibat_full);
|
||
|
i2c_smbus_write_word_data(dev, REG_IBAT_FULL, config.ibat_full);
|
||
|
changed = 1;
|
||
|
}
|
||
|
|
||
|
if (changed == 1) {
|
||
|
i2c_smbus_write_word_data(dev, REG_CRC, 0x0000);
|
||
|
log_print(LOG_INFO, "committing threshold changes");
|
||
|
}
|
||
|
|
||
|
close(dev);
|
||
|
return 0;
|
||
|
}
|