blob: b904cb547ffb064bbeee204373a959f5a059c356 [file] [log] [blame]
per.dalen@appeartv.comde779012011-03-03 14:13:21 -05001/*
2 * Driver for Linear Technology LTC4151 High Voltage I2C Current
3 * and Voltage Monitor
4 *
5 * Copyright (C) 2011 AppearTV AS
6 *
7 * Derived from:
8 *
9 * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot
10 * Swap Controller
11 * Copyright (C) 2010 Ericsson AB.
12 *
13 * Datasheet: http://www.linear.com/docs/Datasheet/4151fc.pdf
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
31#include <linux/kernel.h>
32#include <linux/module.h>
Daniel Golle30fe9762016-08-01 12:07:18 +020033#include <linux/of.h>
per.dalen@appeartv.comde779012011-03-03 14:13:21 -050034#include <linux/init.h>
35#include <linux/err.h>
36#include <linux/slab.h>
37#include <linux/i2c.h>
38#include <linux/hwmon.h>
39#include <linux/hwmon-sysfs.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020040#include <linux/jiffies.h>
per.dalen@appeartv.comde779012011-03-03 14:13:21 -050041
42/* chip registers */
43#define LTC4151_SENSE_H 0x00
44#define LTC4151_SENSE_L 0x01
45#define LTC4151_VIN_H 0x02
46#define LTC4151_VIN_L 0x03
47#define LTC4151_ADIN_H 0x04
48#define LTC4151_ADIN_L 0x05
49
50struct ltc4151_data {
Axel Lin31e38792014-06-05 07:47:12 +080051 struct i2c_client *client;
per.dalen@appeartv.comde779012011-03-03 14:13:21 -050052
53 struct mutex update_lock;
54 bool valid;
55 unsigned long last_updated; /* in jiffies */
Daniel Golle30fe9762016-08-01 12:07:18 +020056 unsigned int shunt; /* in micro ohms */
per.dalen@appeartv.comde779012011-03-03 14:13:21 -050057
58 /* Registers */
59 u8 regs[6];
60};
61
62static struct ltc4151_data *ltc4151_update_device(struct device *dev)
63{
Axel Lin31e38792014-06-05 07:47:12 +080064 struct ltc4151_data *data = dev_get_drvdata(dev);
65 struct i2c_client *client = data->client;
per.dalen@appeartv.comde779012011-03-03 14:13:21 -050066 struct ltc4151_data *ret = data;
67
68 mutex_lock(&data->update_lock);
69
70 /*
71 * The chip's A/D updates 6 times per second
72 * (Conversion Rate 6 - 9 Hz)
73 */
74 if (time_after(jiffies, data->last_updated + HZ / 6) || !data->valid) {
75 int i;
76
77 dev_dbg(&client->dev, "Starting ltc4151 update\n");
78
79 /* Read all registers */
80 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
81 int val;
82
83 val = i2c_smbus_read_byte_data(client, i);
84 if (unlikely(val < 0)) {
85 dev_dbg(dev,
86 "Failed to read ADC value: error %d\n",
87 val);
88 ret = ERR_PTR(val);
89 goto abort;
90 }
91 data->regs[i] = val;
92 }
93 data->last_updated = jiffies;
94 data->valid = 1;
95 }
96abort:
97 mutex_unlock(&data->update_lock);
98 return ret;
99}
100
101/* Return the voltage from the given register in mV */
102static int ltc4151_get_value(struct ltc4151_data *data, u8 reg)
103{
104 u32 val;
105
106 val = (data->regs[reg] << 4) + (data->regs[reg + 1] >> 4);
107
108 switch (reg) {
109 case LTC4151_ADIN_H:
110 /* 500uV resolution. Convert to mV. */
111 val = val * 500 / 1000;
112 break;
113 case LTC4151_SENSE_H:
114 /*
115 * 20uV resolution. Convert to current as measured with
Daniel Golle30fe9762016-08-01 12:07:18 +0200116 * a given sense resistor, in mA.
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500117 */
Daniel Golle30fe9762016-08-01 12:07:18 +0200118 val = val * 20 * 1000 / data->shunt;
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500119 break;
120 case LTC4151_VIN_H:
121 /* 25 mV per increment */
122 val = val * 25;
123 break;
124 default:
125 /* If we get here, the developer messed up */
126 WARN_ON_ONCE(1);
127 val = 0;
128 break;
129 }
130
131 return val;
132}
133
134static ssize_t ltc4151_show_value(struct device *dev,
135 struct device_attribute *da, char *buf)
136{
137 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
138 struct ltc4151_data *data = ltc4151_update_device(dev);
139 int value;
140
141 if (IS_ERR(data))
142 return PTR_ERR(data);
143
144 value = ltc4151_get_value(data, attr->index);
145 return snprintf(buf, PAGE_SIZE, "%d\n", value);
146}
147
148/*
149 * Input voltages.
150 */
Guenter Roeckbc0c5912013-01-19 21:18:41 -0800151static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4151_show_value, NULL,
152 LTC4151_VIN_H);
153static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4151_show_value, NULL,
154 LTC4151_ADIN_H);
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500155
156/* Currents (via sense resistor) */
Guenter Roeckbc0c5912013-01-19 21:18:41 -0800157static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4151_show_value, NULL,
158 LTC4151_SENSE_H);
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500159
Guenter Roeck1b05d222012-01-19 11:02:21 -0800160/*
161 * Finally, construct an array of pointers to members of the above objects,
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500162 * as required for sysfs_create_group()
163 */
Axel Lin31e38792014-06-05 07:47:12 +0800164static struct attribute *ltc4151_attrs[] = {
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500165 &sensor_dev_attr_in1_input.dev_attr.attr,
166 &sensor_dev_attr_in2_input.dev_attr.attr,
167
168 &sensor_dev_attr_curr1_input.dev_attr.attr,
169
170 NULL,
171};
Axel Lin31e38792014-06-05 07:47:12 +0800172ATTRIBUTE_GROUPS(ltc4151);
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500173
174static int ltc4151_probe(struct i2c_client *client,
175 const struct i2c_device_id *id)
176{
177 struct i2c_adapter *adapter = client->adapter;
Axel Lin31e38792014-06-05 07:47:12 +0800178 struct device *dev = &client->dev;
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500179 struct ltc4151_data *data;
Axel Lin31e38792014-06-05 07:47:12 +0800180 struct device *hwmon_dev;
Daniel Golle30fe9762016-08-01 12:07:18 +0200181 u32 shunt;
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500182
183 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
184 return -ENODEV;
185
Axel Lin31e38792014-06-05 07:47:12 +0800186 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Guenter Roeck78968642012-06-02 10:35:52 -0700187 if (!data)
188 return -ENOMEM;
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500189
Daniel Golle30fe9762016-08-01 12:07:18 +0200190 if (of_property_read_u32(client->dev.of_node,
191 "shunt-resistor-micro-ohms", &shunt))
192 shunt = 1000; /* 1 mOhm if not set via DT */
193
194 if (shunt == 0)
195 return -EINVAL;
196
197 data->shunt = shunt;
198
Axel Lin31e38792014-06-05 07:47:12 +0800199 data->client = client;
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500200 mutex_init(&data->update_lock);
201
Axel Lin31e38792014-06-05 07:47:12 +0800202 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
203 data,
204 ltc4151_groups);
205 return PTR_ERR_OR_ZERO(hwmon_dev);
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500206}
207
208static const struct i2c_device_id ltc4151_id[] = {
209 { "ltc4151", 0 },
210 { }
211};
212MODULE_DEVICE_TABLE(i2c, ltc4151_id);
213
Daniel Golle30fe9762016-08-01 12:07:18 +0200214static const struct of_device_id ltc4151_match[] = {
215 { .compatible = "lltc,ltc4151" },
216 {},
217};
Javier Martinez Canillasbb85ceb2017-01-16 11:52:27 -0300218MODULE_DEVICE_TABLE(of, ltc4151_match);
Daniel Golle30fe9762016-08-01 12:07:18 +0200219
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500220/* This is the driver that will be inserted */
221static struct i2c_driver ltc4151_driver = {
222 .driver = {
223 .name = "ltc4151",
Daniel Golle30fe9762016-08-01 12:07:18 +0200224 .of_match_table = of_match_ptr(ltc4151_match),
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500225 },
226 .probe = ltc4151_probe,
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500227 .id_table = ltc4151_id,
228};
229
Axel Linf0967ee2012-01-20 15:38:18 +0800230module_i2c_driver(ltc4151_driver);
per.dalen@appeartv.comde779012011-03-03 14:13:21 -0500231
232MODULE_AUTHOR("Per Dalen <per.dalen@appeartv.com>");
233MODULE_DESCRIPTION("LTC4151 driver");
234MODULE_LICENSE("GPL");