1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Linear Technology LTC4151 High Voltage I2C Current
6 * Copyright (C) 2011 AppearTV AS
10 * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot
12 * Copyright (C) 2010 Ericsson AB.
14 * Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/err.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include <linux/hwmon.h>
25 #include <linux/hwmon-sysfs.h>
26 #include <linux/jiffies.h>
29 #define LTC4151_SENSE_H 0x00
30 #define LTC4151_SENSE_L 0x01
31 #define LTC4151_VIN_H 0x02
32 #define LTC4151_VIN_L 0x03
33 #define LTC4151_ADIN_H 0x04
34 #define LTC4151_ADIN_L 0x05
37 struct i2c_client
*client
;
39 struct mutex update_lock
;
41 unsigned long last_updated
; /* in jiffies */
42 unsigned int shunt
; /* in micro ohms */
48 static struct ltc4151_data
*ltc4151_update_device(struct device
*dev
)
50 struct ltc4151_data
*data
= dev_get_drvdata(dev
);
51 struct i2c_client
*client
= data
->client
;
52 struct ltc4151_data
*ret
= data
;
54 mutex_lock(&data
->update_lock
);
57 * The chip's A/D updates 6 times per second
58 * (Conversion Rate 6 - 9 Hz)
60 if (time_after(jiffies
, data
->last_updated
+ HZ
/ 6) || !data
->valid
) {
63 dev_dbg(&client
->dev
, "Starting ltc4151 update\n");
65 /* Read all registers */
66 for (i
= 0; i
< ARRAY_SIZE(data
->regs
); i
++) {
69 val
= i2c_smbus_read_byte_data(client
, i
);
70 if (unlikely(val
< 0)) {
72 "Failed to read ADC value: error %d\n",
79 data
->last_updated
= jiffies
;
83 mutex_unlock(&data
->update_lock
);
87 /* Return the voltage from the given register in mV */
88 static int ltc4151_get_value(struct ltc4151_data
*data
, u8 reg
)
92 val
= (data
->regs
[reg
] << 4) + (data
->regs
[reg
+ 1] >> 4);
96 /* 500uV resolution. Convert to mV. */
97 val
= val
* 500 / 1000;
101 * 20uV resolution. Convert to current as measured with
102 * a given sense resistor, in mA.
104 val
= val
* 20 * 1000 / data
->shunt
;
107 /* 25 mV per increment */
111 /* If we get here, the developer messed up */
120 static ssize_t
ltc4151_value_show(struct device
*dev
,
121 struct device_attribute
*da
, char *buf
)
123 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(da
);
124 struct ltc4151_data
*data
= ltc4151_update_device(dev
);
128 return PTR_ERR(data
);
130 value
= ltc4151_get_value(data
, attr
->index
);
131 return snprintf(buf
, PAGE_SIZE
, "%d\n", value
);
137 static SENSOR_DEVICE_ATTR_RO(in1_input
, ltc4151_value
, LTC4151_VIN_H
);
138 static SENSOR_DEVICE_ATTR_RO(in2_input
, ltc4151_value
, LTC4151_ADIN_H
);
140 /* Currents (via sense resistor) */
141 static SENSOR_DEVICE_ATTR_RO(curr1_input
, ltc4151_value
, LTC4151_SENSE_H
);
144 * Finally, construct an array of pointers to members of the above objects,
145 * as required for sysfs_create_group()
147 static struct attribute
*ltc4151_attrs
[] = {
148 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
149 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
151 &sensor_dev_attr_curr1_input
.dev_attr
.attr
,
155 ATTRIBUTE_GROUPS(ltc4151
);
157 static int ltc4151_probe(struct i2c_client
*client
,
158 const struct i2c_device_id
*id
)
160 struct i2c_adapter
*adapter
= client
->adapter
;
161 struct device
*dev
= &client
->dev
;
162 struct ltc4151_data
*data
;
163 struct device
*hwmon_dev
;
166 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
169 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
173 if (of_property_read_u32(client
->dev
.of_node
,
174 "shunt-resistor-micro-ohms", &shunt
))
175 shunt
= 1000; /* 1 mOhm if not set via DT */
182 data
->client
= client
;
183 mutex_init(&data
->update_lock
);
185 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
188 return PTR_ERR_OR_ZERO(hwmon_dev
);
191 static const struct i2c_device_id ltc4151_id
[] = {
195 MODULE_DEVICE_TABLE(i2c
, ltc4151_id
);
197 static const struct of_device_id __maybe_unused ltc4151_match
[] = {
198 { .compatible
= "lltc,ltc4151" },
201 MODULE_DEVICE_TABLE(of
, ltc4151_match
);
203 /* This is the driver that will be inserted */
204 static struct i2c_driver ltc4151_driver
= {
207 .of_match_table
= of_match_ptr(ltc4151_match
),
209 .probe
= ltc4151_probe
,
210 .id_table
= ltc4151_id
,
213 module_i2c_driver(ltc4151_driver
);
215 MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
216 MODULE_DESCRIPTION("LTC4151 driver");
217 MODULE_LICENSE("GPL");