Source
// SPDX-License-Identifier: GPL-2.0-only
/*
* Driver for Linear Technology LTC4215 I2C Hot Swap Controller
*
* Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
*
* Datasheet:
* http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
*/
/* Here are names of the chip's registers (a.k.a. commands) */
enum ltc4215_cmd {
LTC4215_CONTROL = 0x00, /* rw */
LTC4215_ALERT = 0x01, /* rw */
LTC4215_STATUS = 0x02, /* ro */
LTC4215_FAULT = 0x03, /* rw */
LTC4215_SENSE = 0x04, /* rw */
LTC4215_SOURCE = 0x05, /* rw */
LTC4215_ADIN = 0x06, /* rw */
};
struct ltc4215_data {
struct i2c_client *client;
struct mutex update_lock;
bool valid;
unsigned long last_updated; /* in jiffies */
/* Registers */
u8 regs[7];
};
static struct ltc4215_data *ltc4215_update_device(struct device *dev)
{
struct ltc4215_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
s32 val;
int i;
mutex_lock(&data->update_lock);
/* The chip's A/D updates 10 times per second */
if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
dev_dbg(&client->dev, "Starting ltc4215 update\n");
/* Read all registers */
for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
val = i2c_smbus_read_byte_data(client, i);
if (unlikely(val < 0))
data->regs[i] = 0;
else
data->regs[i] = val;
}
data->last_updated = jiffies;
data->valid = 1;
}
mutex_unlock(&data->update_lock);
return data;
}
/* Return the voltage from the given register in millivolts */
static int ltc4215_get_voltage(struct device *dev, u8 reg)
{
struct ltc4215_data *data = ltc4215_update_device(dev);
const u8 regval = data->regs[reg];
u32 voltage = 0;
switch (reg) {
case LTC4215_SENSE:
/* 151 uV per increment */
voltage = regval * 151 / 1000;
break;
case LTC4215_SOURCE:
/* 60.5 mV per increment */
voltage = regval * 605 / 10;
break;
case LTC4215_ADIN:
/*