2019-05-20 10:19:02 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-03-03 22:13:21 +03:00
|
|
|
/*
|
|
|
|
* Driver for Linear Technology LTC4151 High Voltage I2C Current
|
|
|
|
* and Voltage Monitor
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 AppearTV AS
|
|
|
|
*
|
|
|
|
* Derived from:
|
|
|
|
*
|
|
|
|
* Driver for Linear Technology LTC4261 I2C Negative Voltage Hot
|
|
|
|
* Swap Controller
|
|
|
|
* Copyright (C) 2010 Ericsson AB.
|
|
|
|
*
|
|
|
|
* Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2016-08-01 13:07:18 +03:00
|
|
|
#include <linux/of.h>
|
2011-03-03 22:13:21 +03:00
|
|
|
#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>
|
2011-03-03 22:13:21 +03:00
|
|
|
|
|
|
|
/* chip registers */
|
|
|
|
#define LTC4151_SENSE_H 0x00
|
|
|
|
#define LTC4151_SENSE_L 0x01
|
|
|
|
#define LTC4151_VIN_H 0x02
|
|
|
|
#define LTC4151_VIN_L 0x03
|
|
|
|
#define LTC4151_ADIN_H 0x04
|
|
|
|
#define LTC4151_ADIN_L 0x05
|
|
|
|
|
|
|
|
struct ltc4151_data {
|
2014-06-05 03:47:12 +04:00
|
|
|
struct i2c_client *client;
|
2011-03-03 22:13:21 +03:00
|
|
|
|
|
|
|
struct mutex update_lock;
|
|
|
|
bool valid;
|
|
|
|
unsigned long last_updated; /* in jiffies */
|
2016-08-01 13:07:18 +03:00
|
|
|
unsigned int shunt; /* in micro ohms */
|
2011-03-03 22:13:21 +03:00
|
|
|
|
|
|
|
/* Registers */
|
|
|
|
u8 regs[6];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ltc4151_data *ltc4151_update_device(struct device *dev)
|
|
|
|
{
|
2014-06-05 03:47:12 +04:00
|
|
|
struct ltc4151_data *data = dev_get_drvdata(dev);
|
|
|
|
struct i2c_client *client = data->client;
|
2011-03-03 22:13:21 +03:00
|
|
|
struct ltc4151_data *ret = data;
|
|
|
|
|
|
|
|
mutex_lock(&data->update_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The chip's A/D updates 6 times per second
|
|
|
|
* (Conversion Rate 6 - 9 Hz)
|
|
|
|
*/
|
|
|
|
if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dev_dbg(&client->dev, "Starting ltc4151 update\n");
|
|
|
|
|
|
|
|
/* Read all registers */
|
|
|
|
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,
|
|
|
|
"Failed to read ADC value: error %d\n",
|
|
|
|
val);
|
|
|
|
ret = ERR_PTR(val);
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
data->regs[i] = val;
|
|
|
|
}
|
|
|
|
data->last_updated = jiffies;
|
2021-09-24 22:52:02 +03:00
|
|
|
data->valid = true;
|
2011-03-03 22:13:21 +03:00
|
|
|
}
|
|
|
|
abort:
|
|
|
|
mutex_unlock(&data->update_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the voltage from the given register in mV */
|
|
|
|
static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
|
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4);
|
|
|
|
|
|
|
|
switch (reg) {
|
|
|
|
case LTC4151_ADIN_H:
|
|
|
|
/* 500uV resolution. Convert to mV. */
|
|
|
|
val = val * 500 / 1000;
|
|
|
|
break;
|
|
|
|
case LTC4151_SENSE_H:
|
|
|
|
/*
|
|
|
|
* 20uV resolution. Convert to current as measured with
|
2016-08-01 13:07:18 +03:00
|
|
|
* a given sense resistor, in mA.
|
2011-03-03 22:13:21 +03:00
|
|
|
*/
|
2016-08-01 13:07:18 +03:00
|
|
|
val = val * 20 * 1000 / data->shunt;
|
2011-03-03 22:13:21 +03:00
|
|
|
break;
|
|
|
|
case LTC4151_VIN_H:
|
|
|
|
/* 25 mV per increment */
|
|
|
|
val = val * 25;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* If we get here, the developer messed up */
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
val = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2018-12-11 01:02:14 +03:00
|
|
|
static ssize_t ltc4151_value_show(struct device *dev,
|
2011-03-03 22:13:21 +03:00
|
|
|
struct device_attribute *da, char *buf)
|
|
|
|
{
|
|
|
|
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
|
|
|
|
struct ltc4151_data *data = ltc4151_update_device(dev);
|
|
|
|
int value;
|
|
|
|
|
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
|
|
|
|
value = ltc4151_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);
|
2011-03-03 22:13:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input voltages.
|
|
|
|
*/
|
2018-12-11 01:02:14 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4151_value, LTC4151_VIN_H);
|
|
|
|
static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4151_value, LTC4151_ADIN_H);
|
2011-03-03 22:13:21 +03:00
|
|
|
|
|
|
|
/* Currents (via sense resistor) */
|
2018-12-11 01:02:14 +03:00
|
|
|
static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4151_value, LTC4151_SENSE_H);
|
2011-03-03 22:13:21 +03:00
|
|
|
|
2012-01-19 23:02:21 +04:00
|
|
|
/*
|
|
|
|
* Finally, construct an array of pointers to members of the above objects,
|
2011-03-03 22:13:21 +03:00
|
|
|
* as required for sysfs_create_group()
|
|
|
|
*/
|
2014-06-05 03:47:12 +04:00
|
|
|
static struct attribute *ltc4151_attrs[] = {
|
2011-03-03 22:13:21 +03:00
|
|
|
&sensor_dev_attr_in1_input.dev_attr.attr,
|
|
|
|
&sensor_dev_attr_in2_input.dev_attr.attr,
|
|
|
|
|
|
|
|
&sensor_dev_attr_curr1_input.dev_attr.attr,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
};
|
2014-06-05 03:47:12 +04:00
|
|
|
ATTRIBUTE_GROUPS(ltc4151);
|
2011-03-03 22:13:21 +03:00
|
|
|
|
2020-08-13 19:02:22 +03:00
|
|
|
static int ltc4151_probe(struct i2c_client *client)
|
2011-03-03 22:13:21 +03:00
|
|
|
{
|
|
|
|
struct i2c_adapter *adapter = client->adapter;
|
2014-06-05 03:47:12 +04:00
|
|
|
struct device *dev = &client->dev;
|
2011-03-03 22:13:21 +03:00
|
|
|
struct ltc4151_data *data;
|
2014-06-05 03:47:12 +04:00
|
|
|
struct device *hwmon_dev;
|
2016-08-01 13:07:18 +03:00
|
|
|
u32 shunt;
|
2011-03-03 22:13:21 +03:00
|
|
|
|
|
|
|
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2014-06-05 03:47:12 +04:00
|
|
|
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
2012-06-02 21:35:52 +04:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2011-03-03 22:13:21 +03:00
|
|
|
|
2016-08-01 13:07:18 +03:00
|
|
|
if (of_property_read_u32(client->dev.of_node,
|
|
|
|
"shunt-resistor-micro-ohms", &shunt))
|
|
|
|
shunt = 1000; /* 1 mOhm if not set via DT */
|
|
|
|
|
|
|
|
if (shunt == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
data->shunt = shunt;
|
|
|
|
|
2014-06-05 03:47:12 +04:00
|
|
|
data->client = client;
|
2011-03-03 22:13:21 +03:00
|
|
|
mutex_init(&data->update_lock);
|
|
|
|
|
2014-06-05 03:47:12 +04:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
|
|
|
|
data,
|
|
|
|
ltc4151_groups);
|
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2011-03-03 22:13:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id ltc4151_id[] = {
|
|
|
|
{ "ltc4151", 0 },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, ltc4151_id);
|
|
|
|
|
2019-04-04 16:19:50 +03:00
|
|
|
static const struct of_device_id __maybe_unused ltc4151_match[] = {
|
2016-08-01 13:07:18 +03:00
|
|
|
{ .compatible = "lltc,ltc4151" },
|
|
|
|
{},
|
|
|
|
};
|
2017-01-16 17:52:27 +03:00
|
|
|
MODULE_DEVICE_TABLE(of, ltc4151_match);
|
2016-08-01 13:07:18 +03:00
|
|
|
|
2011-03-03 22:13:21 +03:00
|
|
|
/* This is the driver that will be inserted */
|
|
|
|
static struct i2c_driver ltc4151_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ltc4151",
|
2016-08-01 13:07:18 +03:00
|
|
|
.of_match_table = of_match_ptr(ltc4151_match),
|
2011-03-03 22:13:21 +03:00
|
|
|
},
|
2020-08-13 19:02:22 +03:00
|
|
|
.probe_new = ltc4151_probe,
|
2011-03-03 22:13:21 +03:00
|
|
|
.id_table = ltc4151_id,
|
|
|
|
};
|
|
|
|
|
2012-01-20 11:38:18 +04:00
|
|
|
module_i2c_driver(ltc4151_driver);
|
2011-03-03 22:13:21 +03:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
|
|
|
|
MODULE_DESCRIPTION("LTC4151 driver");
|
|
|
|
MODULE_LICENSE("GPL");
|