2019-05-20 10:19:02 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-25 22:59:54 +04:00
|
|
|
/*
|
|
|
|
* Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Ericsson AB.
|
|
|
|
*
|
|
|
|
* Derived from:
|
|
|
|
*
|
|
|
|
* Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
|
|
|
|
* Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
|
|
|
|
*
|
|
|
|
* Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/hwmon.h>
|
|
|
|
#include <linux/hwmon-sysfs.h>
|
2012-10-10 17:25:56 +04:00
|
|
|
#include <linux/jiffies.h>
|
2010-06-25 22:59:54 +04:00
|
|
|
|
|
|
|
/* chip registers */
|
|
|
|
#define LTC4261_STATUS 0x00 /* readonly */
|
|
|
|
#define LTC4261_FAULT 0x01
|
|
|
|
#define LTC4261_ALERT 0x02
|
|
|
|
#define LTC4261_CONTROL 0x03
|
|
|
|
#define LTC4261_SENSE_H 0x04
|
|
|
|
#define LTC4261_SENSE_L 0x05
|
|
|
|
#define LTC4261_ADIN2_H 0x06
|
|
|
|
#define LTC4261_ADIN2_L 0x07
|
|
|
|
#define LTC4261_ADIN_H 0x08
|
|
|
|
#define LTC4261_ADIN_L 0x09
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fault register bits
|
|
|
|
*/
|
|
|
|
#define FAULT_OV (1<<0)
|
|
|
|
#define FAULT_UV (1<<1)
|
|
|
|
#define FAULT_OC (1<<2)
|
|
|
|
|
|
|
|
struct ltc4261_data {
|
2013-09-05 19:52:41 +04:00
|
|
|
struct i2c_client *client;
|
2010-06-25 22:59:54 +04:00
|
|
|
|
|
|
|
struct mutex update_lock;
|
|
|
|
bool valid;
|
|
|
|
unsigned long last_updated; /* in jiffies */
|
|
|
|
|
|
|
|
/* Registers */
|
|
|
|
u8 regs[10];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ltc4261_data *ltc4261_update_device(struct device *dev)
|
|
|
|
{
|
2013-09-05 19:52:41 +04:00
|
|
|
struct ltc4261_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2010-06-25 22:59:54 +04:00
|
|
|
struct ltc4261_data *ret = data;
|
|
|
|
|
|
|
|
mutex_lock(&data->update_lock);
|
|
|
|
|
|
|
|
if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Read registers -- 0x00 to 0x09 */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = i2c_smbus_read_byte_data(client, i);
|
|
|
|
if (unlikely(val < 0)) {
|
|
|
|
dev_dbg(dev,
|
2010-10-27 02:59:21 +04:00
|
|
|
"Failed to read ADC value: error %d\n",
|
2010-06-25 22:59:54 +04:00
|
|
|
val);
|
|
|
|
ret = ERR_PTR(val);
|
2012-01-02 15:03:20 +04:00
|
|
|
data->valid = 0;
|
2010-06-25 22:59:54 +04:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
data->regs[i] = val;
|
|
|
|
}
|
|
|
|
data->last_updated = jiffies;
|
|
|
|
data->valid = 1;
|
|
|
|
}
|
|
|
|
abort:
|
|
|
|
mutex_unlock(&data->update_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the voltage from the given register in mV or mA */
|
|
|
|
static int ltc4261_get_value(struct ltc4261_data *data, u8 reg)
|
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6);
|
|
|
|
|
|
|
|
switch (reg) {
|
|
|
|
case LTC4261_ADIN_H:
|
|
|
|
case LTC4261_ADIN2_H:
|
|
|
|
/* 2.5mV resolution. Convert to mV. */
|
|
|
|
val = val * 25 / 10;
|
|
|
|
break;
|
|
|
|
case LTC4261_SENSE_H:
|
|
|
|
/*
|
|
|
|
* 62.5uV resolution. Convert to current as measured with
|
|
|
|
* an 1 mOhm sense resistor, in mA. If a different sense
|
|
|
|
* resistor is installed, calculate the actual current by
|
|
|
|
* dividing the reported current by the sense resistor value
|
|
|
|
* in mOhm.
|
|
|
|
*/
|
|
|
|
val = val * 625 / 10;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* If we get here, the developer messed up */
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
val = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:15 +03:00
|
|
|
static ssize_t ltc4261_value_show(struct device *dev,
|
2010-06-25 22:59:54 +04:00
|
|
|
struct device_attribute *da, char *buf)
|
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
|
|
|
|
struct ltc4261_data *data = ltc4261_update_device(dev);
|
|
|
|
int value;
|
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
|
|
|
value = ltc4261_get_value(data, attr->index);
|
hwmon: replace snprintf in show functions with sysfs_emit
coccicheck complains about the use of snprintf() in sysfs
show functions.
drivers/hwmon/ina3221.c:701:8-16: WARNING: use scnprintf or sprintf
This results in a large number of patch submissions. Fix it all in
one go using the following coccinelle rules. Use sysfs_emit instead
of scnprintf or sprintf since that makes more sense.
@depends on patch@
identifier show, dev, attr, buf;
@@
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
return
- snprintf(buf, \( PAGE_SIZE \| PAGE_SIZE - 1 \),
+ sysfs_emit(buf,
...);
...>
}
@depends on patch@
identifier show, dev, attr, buf, rc;
@@
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
rc =
- snprintf(buf, \( PAGE_SIZE \| PAGE_SIZE - 1 \),
+ sysfs_emit(buf,
...);
...>
}
While at it, remove unnecessary braces and as well as unnecessary
else after return statements to address checkpatch warnings in the
resulting patch.
Cc: Zihao Tang <tangzihao1@hisilicon.com>
Cc: Jay Fang <f.fangjian@huawei.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2021-03-22 06:49:10 +03:00
|
|
|
return sysfs_emit(buf, "%d\n", value);
|
2010-06-25 22:59:54 +04:00
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:15 +03:00
|
|
|
static ssize_t ltc4261_bool_show(struct device *dev,
|
2010-06-25 22:59:54 +04:00
|
|
|
struct device_attribute *da, char *buf)
|
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
|
|
|
|
struct ltc4261_data *data = ltc4261_update_device(dev);
|
|
|
|
u8 fault;
|
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
|
|
|
fault = data->regs[LTC4261_FAULT] & attr->index;
|
|
|
|
if (fault) /* Clear reported faults in chip register */
|
2013-09-05 19:52:41 +04:00
|
|
|
i2c_smbus_write_byte_data(data->client, LTC4261_FAULT, ~fault);
|
2010-06-25 22:59:54 +04:00
|
|
|
|
hwmon: replace snprintf in show functions with sysfs_emit
coccicheck complains about the use of snprintf() in sysfs
show functions.
drivers/hwmon/ina3221.c:701:8-16: WARNING: use scnprintf or sprintf
This results in a large number of patch submissions. Fix it all in
one go using the following coccinelle rules. Use sysfs_emit instead
of scnprintf or sprintf since that makes more sense.
@depends on patch@
identifier show, dev, attr, buf;
@@
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
return
- snprintf(buf, \( PAGE_SIZE \| PAGE_SIZE - 1 \),
+ sysfs_emit(buf,
...);
...>
}
@depends on patch@
identifier show, dev, attr, buf, rc;
@@
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
{
<...
rc =
- snprintf(buf, \( PAGE_SIZE \| PAGE_SIZE - 1 \),
+ sysfs_emit(buf,
...);
...>
}
While at it, remove unnecessary braces and as well as unnecessary
else after return statements to address checkpatch warnings in the
resulting patch.
Cc: Zihao Tang <tangzihao1@hisilicon.com>
Cc: Jay Fang <f.fangjian@huawei.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2021-03-22 06:49:10 +03:00
|
|
|
return sysfs_emit(buf, "%d\n", fault ? 1 : 0);
|
2010-06-25 22:59:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input voltages.
|
|
|
|
*/
|
2018-12-11 01:02:15 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4261_value, LTC4261_ADIN_H);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4261_value, LTC4261_ADIN2_H);
|
2010-06-25 22:59:54 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Voltage alarms. The chip has only one set of voltage alarm status bits,
|
|
|
|
* triggered by input voltage alarms. In many designs, those alarms are
|
|
|
|
* associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
|
|
|
|
* to the OV pin. ADIN2 is, however, not available on all chip variants.
|
|
|
|
* To ensure that the alarm condition is reported to the user, report it
|
|
|
|
* with both voltage sensors.
|
|
|
|
*/
|
2018-12-11 01:02:15 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4261_bool, FAULT_UV);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4261_bool, FAULT_OV);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4261_bool, FAULT_UV);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc4261_bool, FAULT_OV);
|
2010-06-25 22:59:54 +04:00
|
|
|
|
|
|
|
/* Currents (via sense resistor) */
|
2018-12-11 01:02:15 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4261_value, LTC4261_SENSE_H);
|
2010-06-25 22:59:54 +04:00
|
|
|
|
|
|
|
/* Overcurrent alarm */
|
2018-12-11 01:02:15 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4261_bool, FAULT_OC);
|
2010-06-25 22:59:54 +04:00
|
|
|
|
2013-09-05 19:52:41 +04:00
|
|
|
static struct attribute *ltc4261_attrs[] = {
|
2010-06-25 22:59:54 +04:00
|
|
|
&sensor_dev_attr_in1_input.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_in1_min_alarm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_in1_max_alarm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_in2_input.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_in2_min_alarm.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_in2_max_alarm.dev_attr.attr,
|
|
|
|
|
|
|
|
&sensor_dev_attr_curr1_input.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
};
|
2013-09-05 19:52:41 +04:00
|
|
|
ATTRIBUTE_GROUPS(ltc4261);
|
2010-06-25 22:59:54 +04:00
|
|
|
|
2020-08-13 19:02:22 +03:00
|
|
|
static int ltc4261_probe(struct i2c_client *client)
|
2010-06-25 22:59:54 +04:00
|
|
|
{
|
|
|
|
struct i2c_adapter *adapter = client->adapter;
|
2013-09-05 19:52:41 +04:00
|
|
|
struct device *dev = &client->dev;
|
2010-06-25 22:59:54 +04:00
|
|
|
struct ltc4261_data *data;
|
2013-09-05 19:52:41 +04:00
|
|
|
struct device *hwmon_dev;
|
2010-06-25 22:59:54 +04:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
|
2013-09-05 19:52:41 +04:00
|
|
|
dev_err(dev, "Failed to read status register\n");
|
2010-06-25 22:59:54 +04:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2013-09-05 19:52:41 +04:00
|
|
|
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
2012-02-22 20:56:49 +04:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2010-06-25 22:59:54 +04:00
|
|
|
|
2013-09-05 19:52:41 +04:00
|
|
|
data->client = client;
|
2010-06-25 22:59:54 +04:00
|
|
|
mutex_init(&data->update_lock);
|
|
|
|
|
|
|
|
/* Clear faults */
|
|
|
|
i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
|
|
|
|
|
2013-09-05 19:52:41 +04:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
|
|
|
|
data,
|
|
|
|
ltc4261_groups);
|
2013-09-18 07:54:16 +04:00
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2010-06-25 22:59:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id ltc4261_id[] = {
|
|
|
|
{"ltc4261", 0},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(i2c, ltc4261_id);
|
|
|
|
|
|
|
|
/* This is the driver that will be inserted */
|
|
|
|
static struct i2c_driver ltc4261_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ltc4261",
|
|
|
|
},
|
2020-08-13 19:02:22 +03:00
|
|
|
.probe_new = ltc4261_probe,
|
2010-06-25 22:59:54 +04:00
|
|
|
.id_table = ltc4261_id,
|
|
|
|
};
|
|
|
|
|
2012-01-20 11:38:18 +04:00
|
|
|
module_i2c_driver(ltc4261_driver);
|
2010-06-25 22:59:54 +04:00
|
|
|
|
2012-06-22 23:07:25 +04:00
|
|
|
MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
|
2010-06-25 22:59:54 +04:00
|
|
|
MODULE_DESCRIPTION("LTC4261 driver");
|
|
|
|
MODULE_LICENSE("GPL");
|