xref: /linux/drivers/hwmon/ina209.c (revision 1975d167869ef03102771d6267582623d6e07058)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ceeadc5cSGuenter Roeck /*
3ceeadc5cSGuenter Roeck  * Driver for the Texas Instruments / Burr Brown INA209
4ceeadc5cSGuenter Roeck  * Bidirectional Current/Power Monitor
5ceeadc5cSGuenter Roeck  *
6ceeadc5cSGuenter Roeck  * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
7ceeadc5cSGuenter Roeck  *
8ceeadc5cSGuenter Roeck  * Derived from Ira W. Snyder's original driver submission
9ceeadc5cSGuenter Roeck  *	Copyright (C) 2008 Paul Hays <Paul.Hays@cattail.ca>
10ceeadc5cSGuenter Roeck  *	Copyright (C) 2008-2009 Ira W. Snyder <iws@ovro.caltech.edu>
11ceeadc5cSGuenter Roeck  *
12ceeadc5cSGuenter Roeck  * Aligned with ina2xx driver
13ceeadc5cSGuenter Roeck  *	Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
14ceeadc5cSGuenter Roeck  *	Thanks to Jan Volkering
15ceeadc5cSGuenter Roeck  *
16ceeadc5cSGuenter Roeck  * Datasheet:
17009f429fSAlexander A. Klimov  * https://www.ti.com/lit/gpn/ina209
18ceeadc5cSGuenter Roeck  */
19ceeadc5cSGuenter Roeck 
20ceeadc5cSGuenter Roeck #include <linux/kernel.h>
21ceeadc5cSGuenter Roeck #include <linux/module.h>
22ceeadc5cSGuenter Roeck #include <linux/init.h>
23ceeadc5cSGuenter Roeck #include <linux/err.h>
24ceeadc5cSGuenter Roeck #include <linux/slab.h>
25ceeadc5cSGuenter Roeck #include <linux/bug.h>
26ceeadc5cSGuenter Roeck #include <linux/i2c.h>
27ceeadc5cSGuenter Roeck #include <linux/hwmon.h>
28ceeadc5cSGuenter Roeck #include <linux/hwmon-sysfs.h>
29ceeadc5cSGuenter Roeck 
30ceeadc5cSGuenter Roeck #include <linux/platform_data/ina2xx.h>
31ceeadc5cSGuenter Roeck 
32ceeadc5cSGuenter Roeck /* register definitions */
33ceeadc5cSGuenter Roeck #define INA209_CONFIGURATION		0x00
34ceeadc5cSGuenter Roeck #define INA209_STATUS			0x01
35ceeadc5cSGuenter Roeck #define INA209_STATUS_MASK		0x02
36ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE		0x03
37ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE		0x04
38ceeadc5cSGuenter Roeck #define INA209_POWER			0x05
39ceeadc5cSGuenter Roeck #define INA209_CURRENT			0x06
40ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_POS_PEAK	0x07
41ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_NEG_PEAK	0x08
42ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_MAX_PEAK	0x09
43ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_MIN_PEAK	0x0a
44ceeadc5cSGuenter Roeck #define INA209_POWER_PEAK		0x0b
45ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_POS_WARN	0x0c
46ceeadc5cSGuenter Roeck #define INA209_SHUNT_VOLTAGE_NEG_WARN	0x0d
47ceeadc5cSGuenter Roeck #define INA209_POWER_WARN		0x0e
48ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_OVER_WARN	0x0f
49ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_UNDER_WARN	0x10
50ceeadc5cSGuenter Roeck #define INA209_POWER_OVER_LIMIT		0x11
51ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_OVER_LIMIT	0x12
52ceeadc5cSGuenter Roeck #define INA209_BUS_VOLTAGE_UNDER_LIMIT	0x13
53ceeadc5cSGuenter Roeck #define INA209_CRITICAL_DAC_POS		0x14
54ceeadc5cSGuenter Roeck #define INA209_CRITICAL_DAC_NEG		0x15
55ceeadc5cSGuenter Roeck #define INA209_CALIBRATION		0x16
56ceeadc5cSGuenter Roeck 
57ceeadc5cSGuenter Roeck #define INA209_REGISTERS		0x17
58ceeadc5cSGuenter Roeck 
59ceeadc5cSGuenter Roeck #define INA209_CONFIG_DEFAULT		0x3c47	/* PGA=8, full range */
60ceeadc5cSGuenter Roeck #define INA209_SHUNT_DEFAULT		10000	/* uOhm */
61ceeadc5cSGuenter Roeck 
62ceeadc5cSGuenter Roeck struct ina209_data {
6389de7344SGuenter Roeck 	struct i2c_client *client;
64ceeadc5cSGuenter Roeck 
65ceeadc5cSGuenter Roeck 	struct mutex update_lock;
66ceeadc5cSGuenter Roeck 	bool valid;
67ceeadc5cSGuenter Roeck 	unsigned long last_updated;	/* in jiffies */
68ceeadc5cSGuenter Roeck 
69ceeadc5cSGuenter Roeck 	u16 regs[INA209_REGISTERS];	/* All chip registers */
70ceeadc5cSGuenter Roeck 
71ceeadc5cSGuenter Roeck 	u16 config_orig;		/* Original configuration */
72ceeadc5cSGuenter Roeck 	u16 calibration_orig;		/* Original calibration */
73ceeadc5cSGuenter Roeck 	u16 update_interval;
74ceeadc5cSGuenter Roeck };
75ceeadc5cSGuenter Roeck 
76ceeadc5cSGuenter Roeck static struct ina209_data *ina209_update_device(struct device *dev)
77ceeadc5cSGuenter Roeck {
7889de7344SGuenter Roeck 	struct ina209_data *data = dev_get_drvdata(dev);
7989de7344SGuenter Roeck 	struct i2c_client *client = data->client;
80ceeadc5cSGuenter Roeck 	struct ina209_data *ret = data;
81ceeadc5cSGuenter Roeck 	s32 val;
82ceeadc5cSGuenter Roeck 	int i;
83ceeadc5cSGuenter Roeck 
84ceeadc5cSGuenter Roeck 	mutex_lock(&data->update_lock);
85ceeadc5cSGuenter Roeck 
86ceeadc5cSGuenter Roeck 	if (!data->valid ||
87ceeadc5cSGuenter Roeck 	    time_after(jiffies, data->last_updated + data->update_interval)) {
88ceeadc5cSGuenter Roeck 		for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
89ceeadc5cSGuenter Roeck 			val = i2c_smbus_read_word_swapped(client, i);
90ceeadc5cSGuenter Roeck 			if (val < 0) {
91ceeadc5cSGuenter Roeck 				ret = ERR_PTR(val);
92ceeadc5cSGuenter Roeck 				goto abort;
93ceeadc5cSGuenter Roeck 			}
94ceeadc5cSGuenter Roeck 			data->regs[i] = val;
95ceeadc5cSGuenter Roeck 		}
96ceeadc5cSGuenter Roeck 		data->last_updated = jiffies;
97ceeadc5cSGuenter Roeck 		data->valid = true;
98ceeadc5cSGuenter Roeck 	}
99ceeadc5cSGuenter Roeck abort:
100ceeadc5cSGuenter Roeck 	mutex_unlock(&data->update_lock);
101ceeadc5cSGuenter Roeck 	return ret;
102ceeadc5cSGuenter Roeck }
103ceeadc5cSGuenter Roeck 
104ceeadc5cSGuenter Roeck /*
105ceeadc5cSGuenter Roeck  * Read a value from a device register and convert it to the
106ceeadc5cSGuenter Roeck  * appropriate sysfs units
107ceeadc5cSGuenter Roeck  */
108ceeadc5cSGuenter Roeck static long ina209_from_reg(const u8 reg, const u16 val)
109ceeadc5cSGuenter Roeck {
110ceeadc5cSGuenter Roeck 	switch (reg) {
111ceeadc5cSGuenter Roeck 	case INA209_SHUNT_VOLTAGE:
112ceeadc5cSGuenter Roeck 	case INA209_SHUNT_VOLTAGE_POS_PEAK:
113ceeadc5cSGuenter Roeck 	case INA209_SHUNT_VOLTAGE_NEG_PEAK:
114ceeadc5cSGuenter Roeck 	case INA209_SHUNT_VOLTAGE_POS_WARN:
115ceeadc5cSGuenter Roeck 	case INA209_SHUNT_VOLTAGE_NEG_WARN:
116ceeadc5cSGuenter Roeck 		/* LSB=10 uV. Convert to mV. */
117bdaf9c40SJoe Schaack 		return DIV_ROUND_CLOSEST((s16)val, 100);
118ceeadc5cSGuenter Roeck 
119ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE:
120ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_MAX_PEAK:
121ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_MIN_PEAK:
122ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_OVER_WARN:
123ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_UNDER_WARN:
124ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_OVER_LIMIT:
125ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_UNDER_LIMIT:
126ceeadc5cSGuenter Roeck 		/* LSB=4 mV, last 3 bits unused */
127ceeadc5cSGuenter Roeck 		return (val >> 3) * 4;
128ceeadc5cSGuenter Roeck 
129ceeadc5cSGuenter Roeck 	case INA209_CRITICAL_DAC_POS:
130ceeadc5cSGuenter Roeck 		/* LSB=1 mV, in the upper 8 bits */
131ceeadc5cSGuenter Roeck 		return val >> 8;
132ceeadc5cSGuenter Roeck 
133ceeadc5cSGuenter Roeck 	case INA209_CRITICAL_DAC_NEG:
134ceeadc5cSGuenter Roeck 		/* LSB=1 mV, in the upper 8 bits */
135ceeadc5cSGuenter Roeck 		return -1 * (val >> 8);
136ceeadc5cSGuenter Roeck 
137ceeadc5cSGuenter Roeck 	case INA209_POWER:
138ceeadc5cSGuenter Roeck 	case INA209_POWER_PEAK:
139ceeadc5cSGuenter Roeck 	case INA209_POWER_WARN:
140ceeadc5cSGuenter Roeck 	case INA209_POWER_OVER_LIMIT:
141ceeadc5cSGuenter Roeck 		/* LSB=20 mW. Convert to uW */
142ceeadc5cSGuenter Roeck 		return val * 20 * 1000L;
143ceeadc5cSGuenter Roeck 
144ceeadc5cSGuenter Roeck 	case INA209_CURRENT:
145ceeadc5cSGuenter Roeck 		/* LSB=1 mA (selected). Is in mA */
146bdaf9c40SJoe Schaack 		return (s16)val;
147ceeadc5cSGuenter Roeck 	}
148ceeadc5cSGuenter Roeck 
149ceeadc5cSGuenter Roeck 	/* programmer goofed */
150ceeadc5cSGuenter Roeck 	WARN_ON_ONCE(1);
151ceeadc5cSGuenter Roeck 	return 0;
152ceeadc5cSGuenter Roeck }
153ceeadc5cSGuenter Roeck 
154ceeadc5cSGuenter Roeck /*
155ceeadc5cSGuenter Roeck  * Take a value and convert it to register format, clamping the value
156ceeadc5cSGuenter Roeck  * to the appropriate range.
157ceeadc5cSGuenter Roeck  */
158ceeadc5cSGuenter Roeck static int ina209_to_reg(u8 reg, u16 old, long val)
159ceeadc5cSGuenter Roeck {
160ceeadc5cSGuenter Roeck 	switch (reg) {
161ceeadc5cSGuenter Roeck 	case INA209_SHUNT_VOLTAGE_POS_WARN:
162ceeadc5cSGuenter Roeck 	case INA209_SHUNT_VOLTAGE_NEG_WARN:
163ceeadc5cSGuenter Roeck 		/* Limit to +- 320 mV, 10 uV LSB */
164ceeadc5cSGuenter Roeck 		return clamp_val(val, -320, 320) * 100;
165ceeadc5cSGuenter Roeck 
166ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_OVER_WARN:
167ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_UNDER_WARN:
168ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_OVER_LIMIT:
169ceeadc5cSGuenter Roeck 	case INA209_BUS_VOLTAGE_UNDER_LIMIT:
170ceeadc5cSGuenter Roeck 		/*
171ceeadc5cSGuenter Roeck 		 * Limit to 0-32000 mV, 4 mV LSB
172ceeadc5cSGuenter Roeck 		 *
173ceeadc5cSGuenter Roeck 		 * The last three bits aren't part of the value, but we'll
174ceeadc5cSGuenter Roeck 		 * preserve them in their original state.
175ceeadc5cSGuenter Roeck 		 */
176ceeadc5cSGuenter Roeck 		return (DIV_ROUND_CLOSEST(clamp_val(val, 0, 32000), 4) << 3)
177ceeadc5cSGuenter Roeck 		  | (old & 0x7);
178ceeadc5cSGuenter Roeck 
179ceeadc5cSGuenter Roeck 	case INA209_CRITICAL_DAC_NEG:
180ceeadc5cSGuenter Roeck 		/*
181ceeadc5cSGuenter Roeck 		 * Limit to -255-0 mV, 1 mV LSB
182ceeadc5cSGuenter Roeck 		 * Convert the value to a positive value for the register
183ceeadc5cSGuenter Roeck 		 *
184ceeadc5cSGuenter Roeck 		 * The value lives in the top 8 bits only, be careful
185ceeadc5cSGuenter Roeck 		 * and keep original value of other bits.
186ceeadc5cSGuenter Roeck 		 */
187ceeadc5cSGuenter Roeck 		return (clamp_val(-val, 0, 255) << 8) | (old & 0xff);
188ceeadc5cSGuenter Roeck 
189ceeadc5cSGuenter Roeck 	case INA209_CRITICAL_DAC_POS:
190ceeadc5cSGuenter Roeck 		/*
191ceeadc5cSGuenter Roeck 		 * Limit to 0-255 mV, 1 mV LSB
192ceeadc5cSGuenter Roeck 		 *
193ceeadc5cSGuenter Roeck 		 * The value lives in the top 8 bits only, be careful
194ceeadc5cSGuenter Roeck 		 * and keep original value of other bits.
195ceeadc5cSGuenter Roeck 		 */
196ceeadc5cSGuenter Roeck 		return (clamp_val(val, 0, 255) << 8) | (old & 0xff);
197ceeadc5cSGuenter Roeck 
198ceeadc5cSGuenter Roeck 	case INA209_POWER_WARN:
199ceeadc5cSGuenter Roeck 	case INA209_POWER_OVER_LIMIT:
200ceeadc5cSGuenter Roeck 		/* 20 mW LSB */
201ceeadc5cSGuenter Roeck 		return DIV_ROUND_CLOSEST(val, 20 * 1000);
202ceeadc5cSGuenter Roeck 	}
203ceeadc5cSGuenter Roeck 
204ceeadc5cSGuenter Roeck 	/* Other registers are read-only, return access error */
205ceeadc5cSGuenter Roeck 	return -EACCES;
206ceeadc5cSGuenter Roeck }
207ceeadc5cSGuenter Roeck 
208ceeadc5cSGuenter Roeck static int ina209_interval_from_reg(u16 reg)
209ceeadc5cSGuenter Roeck {
210ceeadc5cSGuenter Roeck 	return 68 >> (15 - ((reg >> 3) & 0x0f));
211ceeadc5cSGuenter Roeck }
212ceeadc5cSGuenter Roeck 
213ceeadc5cSGuenter Roeck static u16 ina209_reg_from_interval(u16 config, long interval)
214ceeadc5cSGuenter Roeck {
215ceeadc5cSGuenter Roeck 	int i, adc;
216ceeadc5cSGuenter Roeck 
217ceeadc5cSGuenter Roeck 	if (interval <= 0) {
218ceeadc5cSGuenter Roeck 		adc = 8;
219ceeadc5cSGuenter Roeck 	} else {
220ceeadc5cSGuenter Roeck 		adc = 15;
221ceeadc5cSGuenter Roeck 		for (i = 34 + 34 / 2; i; i >>= 1) {
222ceeadc5cSGuenter Roeck 			if (i < interval)
223ceeadc5cSGuenter Roeck 				break;
224ceeadc5cSGuenter Roeck 			adc--;
225ceeadc5cSGuenter Roeck 		}
226ceeadc5cSGuenter Roeck 	}
227ceeadc5cSGuenter Roeck 	return (config & 0xf807) | (adc << 3) | (adc << 7);
228ceeadc5cSGuenter Roeck }
229ceeadc5cSGuenter Roeck 
23046dce7a1SGuenter Roeck static ssize_t ina209_interval_store(struct device *dev,
231ceeadc5cSGuenter Roeck 				     struct device_attribute *da,
232ceeadc5cSGuenter Roeck 				     const char *buf, size_t count)
233ceeadc5cSGuenter Roeck {
234ceeadc5cSGuenter Roeck 	struct ina209_data *data = ina209_update_device(dev);
235ceeadc5cSGuenter Roeck 	long val;
236ceeadc5cSGuenter Roeck 	u16 regval;
237ceeadc5cSGuenter Roeck 	int ret;
238ceeadc5cSGuenter Roeck 
239ceeadc5cSGuenter Roeck 	if (IS_ERR(data))
240ceeadc5cSGuenter Roeck 		return PTR_ERR(data);
241ceeadc5cSGuenter Roeck 
242ceeadc5cSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
243ceeadc5cSGuenter Roeck 	if (ret < 0)
244ceeadc5cSGuenter Roeck 		return ret;
245ceeadc5cSGuenter Roeck 
246ceeadc5cSGuenter Roeck 	mutex_lock(&data->update_lock);
247ceeadc5cSGuenter Roeck 	regval = ina209_reg_from_interval(data->regs[INA209_CONFIGURATION],
248ceeadc5cSGuenter Roeck 					  val);
24989de7344SGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, INA209_CONFIGURATION,
25089de7344SGuenter Roeck 				     regval);
251ceeadc5cSGuenter Roeck 	data->regs[INA209_CONFIGURATION] = regval;
252ceeadc5cSGuenter Roeck 	data->update_interval = ina209_interval_from_reg(regval);
253ceeadc5cSGuenter Roeck 	mutex_unlock(&data->update_lock);
254ceeadc5cSGuenter Roeck 	return count;
255ceeadc5cSGuenter Roeck }
256ceeadc5cSGuenter Roeck 
25746dce7a1SGuenter Roeck static ssize_t ina209_interval_show(struct device *dev,
258ceeadc5cSGuenter Roeck 				    struct device_attribute *da, char *buf)
259ceeadc5cSGuenter Roeck {
26089de7344SGuenter Roeck 	struct ina209_data *data = dev_get_drvdata(dev);
261ceeadc5cSGuenter Roeck 
2621f4d4af4SGuenter Roeck 	return sysfs_emit(buf, "%d\n", data->update_interval);
263ceeadc5cSGuenter Roeck }
264ceeadc5cSGuenter Roeck 
265ceeadc5cSGuenter Roeck /*
266ceeadc5cSGuenter Roeck  * History is reset by writing 1 into bit 0 of the respective peak register.
267ceeadc5cSGuenter Roeck  * Since more than one peak register may be affected by the scope of a
268ceeadc5cSGuenter Roeck  * reset_history attribute write, use a bit mask in attr->index to identify
269ceeadc5cSGuenter Roeck  * which registers are affected.
270ceeadc5cSGuenter Roeck  */
271ceeadc5cSGuenter Roeck static u16 ina209_reset_history_regs[] = {
272ceeadc5cSGuenter Roeck 	INA209_SHUNT_VOLTAGE_POS_PEAK,
273ceeadc5cSGuenter Roeck 	INA209_SHUNT_VOLTAGE_NEG_PEAK,
274ceeadc5cSGuenter Roeck 	INA209_BUS_VOLTAGE_MAX_PEAK,
275ceeadc5cSGuenter Roeck 	INA209_BUS_VOLTAGE_MIN_PEAK,
276ceeadc5cSGuenter Roeck 	INA209_POWER_PEAK
277ceeadc5cSGuenter Roeck };
278ceeadc5cSGuenter Roeck 
27946dce7a1SGuenter Roeck static ssize_t ina209_history_store(struct device *dev,
280ceeadc5cSGuenter Roeck 				    struct device_attribute *da,
28146dce7a1SGuenter Roeck 				    const char *buf, size_t count)
282ceeadc5cSGuenter Roeck {
283ceeadc5cSGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
28489de7344SGuenter Roeck 	struct ina209_data *data = dev_get_drvdata(dev);
28589de7344SGuenter Roeck 	struct i2c_client *client = data->client;
286ceeadc5cSGuenter Roeck 	u32 mask = attr->index;
287ceeadc5cSGuenter Roeck 	long val;
288ceeadc5cSGuenter Roeck 	int i, ret;
289ceeadc5cSGuenter Roeck 
290ceeadc5cSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
291ceeadc5cSGuenter Roeck 	if (ret < 0)
292ceeadc5cSGuenter Roeck 		return ret;
293ceeadc5cSGuenter Roeck 
294ceeadc5cSGuenter Roeck 	mutex_lock(&data->update_lock);
295ceeadc5cSGuenter Roeck 	for (i = 0; i < ARRAY_SIZE(ina209_reset_history_regs); i++) {
296ceeadc5cSGuenter Roeck 		if (mask & (1 << i))
297ceeadc5cSGuenter Roeck 			i2c_smbus_write_word_swapped(client,
298ceeadc5cSGuenter Roeck 					ina209_reset_history_regs[i], 1);
299ceeadc5cSGuenter Roeck 	}
300ceeadc5cSGuenter Roeck 	data->valid = false;
301ceeadc5cSGuenter Roeck 	mutex_unlock(&data->update_lock);
302ceeadc5cSGuenter Roeck 	return count;
303ceeadc5cSGuenter Roeck }
304ceeadc5cSGuenter Roeck 
30546dce7a1SGuenter Roeck static ssize_t ina209_value_store(struct device *dev,
306ceeadc5cSGuenter Roeck 				  struct device_attribute *da,
30746dce7a1SGuenter Roeck 				  const char *buf, size_t count)
308ceeadc5cSGuenter Roeck {
309ceeadc5cSGuenter Roeck 	struct ina209_data *data = ina209_update_device(dev);
310ceeadc5cSGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
311ceeadc5cSGuenter Roeck 	int reg = attr->index;
312ceeadc5cSGuenter Roeck 	long val;
313ceeadc5cSGuenter Roeck 	int ret;
314ceeadc5cSGuenter Roeck 
315ceeadc5cSGuenter Roeck 	if (IS_ERR(data))
316ceeadc5cSGuenter Roeck 		return PTR_ERR(data);
317ceeadc5cSGuenter Roeck 
318ceeadc5cSGuenter Roeck 	ret = kstrtol(buf, 10, &val);
319ceeadc5cSGuenter Roeck 	if (ret < 0)
320ceeadc5cSGuenter Roeck 		return ret;
321ceeadc5cSGuenter Roeck 
322ceeadc5cSGuenter Roeck 	mutex_lock(&data->update_lock);
323ceeadc5cSGuenter Roeck 	ret = ina209_to_reg(reg, data->regs[reg], val);
324ceeadc5cSGuenter Roeck 	if (ret < 0) {
325ceeadc5cSGuenter Roeck 		count = ret;
326ceeadc5cSGuenter Roeck 		goto abort;
327ceeadc5cSGuenter Roeck 	}
32889de7344SGuenter Roeck 	i2c_smbus_write_word_swapped(data->client, reg, ret);
329ceeadc5cSGuenter Roeck 	data->regs[reg] = ret;
330ceeadc5cSGuenter Roeck abort:
331ceeadc5cSGuenter Roeck 	mutex_unlock(&data->update_lock);
332ceeadc5cSGuenter Roeck 	return count;
333ceeadc5cSGuenter Roeck }
334ceeadc5cSGuenter Roeck 
33546dce7a1SGuenter Roeck static ssize_t ina209_value_show(struct device *dev,
33646dce7a1SGuenter Roeck 				 struct device_attribute *da, char *buf)
337ceeadc5cSGuenter Roeck {
338ceeadc5cSGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
339ceeadc5cSGuenter Roeck 	struct ina209_data *data = ina209_update_device(dev);
340ceeadc5cSGuenter Roeck 	long val;
341ceeadc5cSGuenter Roeck 
342ceeadc5cSGuenter Roeck 	if (IS_ERR(data))
343ceeadc5cSGuenter Roeck 		return PTR_ERR(data);
344ceeadc5cSGuenter Roeck 
345ceeadc5cSGuenter Roeck 	val = ina209_from_reg(attr->index, data->regs[attr->index]);
3461f4d4af4SGuenter Roeck 	return sysfs_emit(buf, "%ld\n", val);
347ceeadc5cSGuenter Roeck }
348ceeadc5cSGuenter Roeck 
34946dce7a1SGuenter Roeck static ssize_t ina209_alarm_show(struct device *dev,
35046dce7a1SGuenter Roeck 				 struct device_attribute *da, char *buf)
351ceeadc5cSGuenter Roeck {
352ceeadc5cSGuenter Roeck 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
353ceeadc5cSGuenter Roeck 	struct ina209_data *data = ina209_update_device(dev);
354ceeadc5cSGuenter Roeck 	const unsigned int mask = attr->index;
355ceeadc5cSGuenter Roeck 	u16 status;
356ceeadc5cSGuenter Roeck 
357ceeadc5cSGuenter Roeck 	if (IS_ERR(data))
358ceeadc5cSGuenter Roeck 		return PTR_ERR(data);
359ceeadc5cSGuenter Roeck 
360ceeadc5cSGuenter Roeck 	status = data->regs[INA209_STATUS];
361ceeadc5cSGuenter Roeck 
362ceeadc5cSGuenter Roeck 	/*
363ceeadc5cSGuenter Roeck 	 * All alarms are in the INA209_STATUS register. To avoid a long
364ceeadc5cSGuenter Roeck 	 * switch statement, the mask is passed in attr->index
365ceeadc5cSGuenter Roeck 	 */
3661f4d4af4SGuenter Roeck 	return sysfs_emit(buf, "%u\n", !!(status & mask));
367ceeadc5cSGuenter Roeck }
368ceeadc5cSGuenter Roeck 
369ceeadc5cSGuenter Roeck /* Shunt voltage, history, limits, alarms */
37046dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_input, ina209_value, INA209_SHUNT_VOLTAGE);
37146dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_input_highest, ina209_value,
372ceeadc5cSGuenter Roeck 			     INA209_SHUNT_VOLTAGE_POS_PEAK);
37346dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_input_lowest, ina209_value,
374ceeadc5cSGuenter Roeck 			     INA209_SHUNT_VOLTAGE_NEG_PEAK);
37546dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_WO(in0_reset_history, ina209_history,
37646dce7a1SGuenter Roeck 			     (1 << 0) | (1 << 1));
37746dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in0_max, ina209_value,
37846dce7a1SGuenter Roeck 			     INA209_SHUNT_VOLTAGE_POS_WARN);
37946dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in0_min, ina209_value,
38046dce7a1SGuenter Roeck 			     INA209_SHUNT_VOLTAGE_NEG_WARN);
38146dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in0_crit_max, ina209_value,
38246dce7a1SGuenter Roeck 			     INA209_CRITICAL_DAC_POS);
38346dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in0_crit_min, ina209_value,
38446dce7a1SGuenter Roeck 			     INA209_CRITICAL_DAC_NEG);
385ceeadc5cSGuenter Roeck 
38646dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_min_alarm, ina209_alarm, 1 << 11);
38746dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_max_alarm, ina209_alarm, 1 << 12);
38846dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_crit_min_alarm, ina209_alarm, 1 << 6);
38946dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in0_crit_max_alarm, ina209_alarm, 1 << 7);
390ceeadc5cSGuenter Roeck 
391ceeadc5cSGuenter Roeck /* Bus voltage, history, limits, alarms */
39246dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input, ina209_value, INA209_BUS_VOLTAGE);
39346dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input_highest, ina209_value,
394ceeadc5cSGuenter Roeck 			     INA209_BUS_VOLTAGE_MAX_PEAK);
39546dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input_lowest, ina209_value,
396ceeadc5cSGuenter Roeck 			     INA209_BUS_VOLTAGE_MIN_PEAK);
39746dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_WO(in1_reset_history, ina209_history,
39846dce7a1SGuenter Roeck 			     (1 << 2) | (1 << 3));
39946dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in1_max, ina209_value,
40046dce7a1SGuenter Roeck 			     INA209_BUS_VOLTAGE_OVER_WARN);
40146dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in1_min, ina209_value,
40246dce7a1SGuenter Roeck 			     INA209_BUS_VOLTAGE_UNDER_WARN);
40346dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in1_crit_max, ina209_value,
40446dce7a1SGuenter Roeck 			     INA209_BUS_VOLTAGE_OVER_LIMIT);
40546dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(in1_crit_min, ina209_value,
40646dce7a1SGuenter Roeck 			     INA209_BUS_VOLTAGE_UNDER_LIMIT);
407ceeadc5cSGuenter Roeck 
40846dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ina209_alarm, 1 << 14);
40946dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ina209_alarm, 1 << 15);
41046dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_crit_min_alarm, ina209_alarm, 1 << 9);
41146dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_crit_max_alarm, ina209_alarm, 1 << 10);
412ceeadc5cSGuenter Roeck 
413ceeadc5cSGuenter Roeck /* Power */
41446dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(power1_input, ina209_value, INA209_POWER);
41546dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(power1_input_highest, ina209_value,
41646dce7a1SGuenter Roeck 			     INA209_POWER_PEAK);
41746dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_WO(power1_reset_history, ina209_history, 1 << 4);
41846dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(power1_max, ina209_value, INA209_POWER_WARN);
41946dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(power1_crit, ina209_value,
42046dce7a1SGuenter Roeck 			     INA209_POWER_OVER_LIMIT);
421ceeadc5cSGuenter Roeck 
42246dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(power1_max_alarm, ina209_alarm, 1 << 13);
42346dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina209_alarm, 1 << 8);
424ceeadc5cSGuenter Roeck 
425ceeadc5cSGuenter Roeck /* Current */
42646dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_input, ina209_value, INA209_CURRENT);
427ceeadc5cSGuenter Roeck 
42846dce7a1SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(update_interval, ina209_interval, 0);
429ceeadc5cSGuenter Roeck 
430ceeadc5cSGuenter Roeck /*
431ceeadc5cSGuenter Roeck  * Finally, construct an array of pointers to members of the above objects,
432ceeadc5cSGuenter Roeck  * as required for sysfs_create_group()
433ceeadc5cSGuenter Roeck  */
43489de7344SGuenter Roeck static struct attribute *ina209_attrs[] = {
435ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_input.dev_attr.attr,
436ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_input_highest.dev_attr.attr,
437ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_input_lowest.dev_attr.attr,
438ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_reset_history.dev_attr.attr,
439ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_max.dev_attr.attr,
440ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_min.dev_attr.attr,
441ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_crit_max.dev_attr.attr,
442ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_crit_min.dev_attr.attr,
443ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_max_alarm.dev_attr.attr,
444ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_min_alarm.dev_attr.attr,
445ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_crit_max_alarm.dev_attr.attr,
446ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in0_crit_min_alarm.dev_attr.attr,
447ceeadc5cSGuenter Roeck 
448ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_input.dev_attr.attr,
449ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_input_highest.dev_attr.attr,
450ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_input_lowest.dev_attr.attr,
451ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_reset_history.dev_attr.attr,
452ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_max.dev_attr.attr,
453ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_min.dev_attr.attr,
454ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_crit_max.dev_attr.attr,
455ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_crit_min.dev_attr.attr,
456ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_max_alarm.dev_attr.attr,
457ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_min_alarm.dev_attr.attr,
458ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_crit_max_alarm.dev_attr.attr,
459ceeadc5cSGuenter Roeck 	&sensor_dev_attr_in1_crit_min_alarm.dev_attr.attr,
460ceeadc5cSGuenter Roeck 
461ceeadc5cSGuenter Roeck 	&sensor_dev_attr_power1_input.dev_attr.attr,
462ceeadc5cSGuenter Roeck 	&sensor_dev_attr_power1_input_highest.dev_attr.attr,
463ceeadc5cSGuenter Roeck 	&sensor_dev_attr_power1_reset_history.dev_attr.attr,
464ceeadc5cSGuenter Roeck 	&sensor_dev_attr_power1_max.dev_attr.attr,
465ceeadc5cSGuenter Roeck 	&sensor_dev_attr_power1_crit.dev_attr.attr,
466ceeadc5cSGuenter Roeck 	&sensor_dev_attr_power1_max_alarm.dev_attr.attr,
467ceeadc5cSGuenter Roeck 	&sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
468ceeadc5cSGuenter Roeck 
469ceeadc5cSGuenter Roeck 	&sensor_dev_attr_curr1_input.dev_attr.attr,
470ceeadc5cSGuenter Roeck 
471ceeadc5cSGuenter Roeck 	&sensor_dev_attr_update_interval.dev_attr.attr,
472ceeadc5cSGuenter Roeck 
473ceeadc5cSGuenter Roeck 	NULL,
474ceeadc5cSGuenter Roeck };
47589de7344SGuenter Roeck ATTRIBUTE_GROUPS(ina209);
476ceeadc5cSGuenter Roeck 
477ceeadc5cSGuenter Roeck static void ina209_restore_conf(struct i2c_client *client,
478ceeadc5cSGuenter Roeck 				struct ina209_data *data)
479ceeadc5cSGuenter Roeck {
480ceeadc5cSGuenter Roeck 	/* Restore initial configuration */
481ceeadc5cSGuenter Roeck 	i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
482ceeadc5cSGuenter Roeck 				     data->config_orig);
483ceeadc5cSGuenter Roeck 	i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
484ceeadc5cSGuenter Roeck 				     data->calibration_orig);
485ceeadc5cSGuenter Roeck }
486ceeadc5cSGuenter Roeck 
487ceeadc5cSGuenter Roeck static int ina209_init_client(struct i2c_client *client,
488ceeadc5cSGuenter Roeck 			      struct ina209_data *data)
489ceeadc5cSGuenter Roeck {
490ceeadc5cSGuenter Roeck 	struct ina2xx_platform_data *pdata = dev_get_platdata(&client->dev);
491ceeadc5cSGuenter Roeck 	u32 shunt;
492ceeadc5cSGuenter Roeck 	int reg;
493ceeadc5cSGuenter Roeck 
494ceeadc5cSGuenter Roeck 	reg = i2c_smbus_read_word_swapped(client, INA209_CALIBRATION);
495ceeadc5cSGuenter Roeck 	if (reg < 0)
496ceeadc5cSGuenter Roeck 		return reg;
497ceeadc5cSGuenter Roeck 	data->calibration_orig = reg;
498ceeadc5cSGuenter Roeck 
499ceeadc5cSGuenter Roeck 	reg = i2c_smbus_read_word_swapped(client, INA209_CONFIGURATION);
500ceeadc5cSGuenter Roeck 	if (reg < 0)
501ceeadc5cSGuenter Roeck 		return reg;
502ceeadc5cSGuenter Roeck 	data->config_orig = reg;
503ceeadc5cSGuenter Roeck 
504ceeadc5cSGuenter Roeck 	if (pdata) {
505ceeadc5cSGuenter Roeck 		if (pdata->shunt_uohms <= 0)
506ceeadc5cSGuenter Roeck 			return -EINVAL;
507ceeadc5cSGuenter Roeck 		shunt = pdata->shunt_uohms;
508ceeadc5cSGuenter Roeck 	} else if (!of_property_read_u32(client->dev.of_node, "shunt-resistor",
509ceeadc5cSGuenter Roeck 					 &shunt)) {
510ceeadc5cSGuenter Roeck 		if (shunt == 0)
511ceeadc5cSGuenter Roeck 			return -EINVAL;
512ceeadc5cSGuenter Roeck 	} else {
513ceeadc5cSGuenter Roeck 		shunt = data->calibration_orig ?
514ceeadc5cSGuenter Roeck 		  40960000 / data->calibration_orig : INA209_SHUNT_DEFAULT;
515ceeadc5cSGuenter Roeck 	}
516ceeadc5cSGuenter Roeck 
517ceeadc5cSGuenter Roeck 	i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
518ceeadc5cSGuenter Roeck 				     INA209_CONFIG_DEFAULT);
519ceeadc5cSGuenter Roeck 	data->update_interval = ina209_interval_from_reg(INA209_CONFIG_DEFAULT);
520ceeadc5cSGuenter Roeck 
521ceeadc5cSGuenter Roeck 	/*
522ceeadc5cSGuenter Roeck 	 * Calibrate current LSB to 1mA. Shunt is in uOhms.
523ceeadc5cSGuenter Roeck 	 * See equation 13 in datasheet.
524ceeadc5cSGuenter Roeck 	 */
525ceeadc5cSGuenter Roeck 	i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
526ceeadc5cSGuenter Roeck 				     clamp_val(40960000 / shunt, 1, 65535));
527ceeadc5cSGuenter Roeck 
528ceeadc5cSGuenter Roeck 	/* Clear status register */
529ceeadc5cSGuenter Roeck 	i2c_smbus_read_word_swapped(client, INA209_STATUS);
530ceeadc5cSGuenter Roeck 
531ceeadc5cSGuenter Roeck 	return 0;
532ceeadc5cSGuenter Roeck }
533ceeadc5cSGuenter Roeck 
53467487038SStephen Kitt static int ina209_probe(struct i2c_client *client)
535ceeadc5cSGuenter Roeck {
536ceeadc5cSGuenter Roeck 	struct i2c_adapter *adapter = client->adapter;
537ceeadc5cSGuenter Roeck 	struct ina209_data *data;
53889de7344SGuenter Roeck 	struct device *hwmon_dev;
539ceeadc5cSGuenter Roeck 	int ret;
540ceeadc5cSGuenter Roeck 
541ceeadc5cSGuenter Roeck 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
542ceeadc5cSGuenter Roeck 		return -ENODEV;
543ceeadc5cSGuenter Roeck 
544ceeadc5cSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
545ceeadc5cSGuenter Roeck 	if (!data)
546ceeadc5cSGuenter Roeck 		return -ENOMEM;
547ceeadc5cSGuenter Roeck 
548ceeadc5cSGuenter Roeck 	i2c_set_clientdata(client, data);
54989de7344SGuenter Roeck 	data->client = client;
550ceeadc5cSGuenter Roeck 	mutex_init(&data->update_lock);
551ceeadc5cSGuenter Roeck 
552ceeadc5cSGuenter Roeck 	ret = ina209_init_client(client, data);
553ceeadc5cSGuenter Roeck 	if (ret)
554ceeadc5cSGuenter Roeck 		return ret;
555ceeadc5cSGuenter Roeck 
55689de7344SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
55789de7344SGuenter Roeck 							   client->name,
55889de7344SGuenter Roeck 							   data, ina209_groups);
55989de7344SGuenter Roeck 	if (IS_ERR(hwmon_dev)) {
56089de7344SGuenter Roeck 		ret = PTR_ERR(hwmon_dev);
561ceeadc5cSGuenter Roeck 		goto out_restore_conf;
562ceeadc5cSGuenter Roeck 	}
563ceeadc5cSGuenter Roeck 
564ceeadc5cSGuenter Roeck 	return 0;
565ceeadc5cSGuenter Roeck 
566ceeadc5cSGuenter Roeck out_restore_conf:
567ceeadc5cSGuenter Roeck 	ina209_restore_conf(client, data);
568ceeadc5cSGuenter Roeck 	return ret;
569ceeadc5cSGuenter Roeck }
570ceeadc5cSGuenter Roeck 
571ed5c2f5fSUwe Kleine-König static void ina209_remove(struct i2c_client *client)
572ceeadc5cSGuenter Roeck {
573ceeadc5cSGuenter Roeck 	struct ina209_data *data = i2c_get_clientdata(client);
574ceeadc5cSGuenter Roeck 
575ceeadc5cSGuenter Roeck 	ina209_restore_conf(client, data);
576ceeadc5cSGuenter Roeck }
577ceeadc5cSGuenter Roeck 
578ceeadc5cSGuenter Roeck static const struct i2c_device_id ina209_id[] = {
579ceeadc5cSGuenter Roeck 	{ "ina209", 0 },
580ceeadc5cSGuenter Roeck 	{ }
581ceeadc5cSGuenter Roeck };
582ceeadc5cSGuenter Roeck MODULE_DEVICE_TABLE(i2c, ina209_id);
583ceeadc5cSGuenter Roeck 
58432c2d402SGuenter Roeck static const struct of_device_id __maybe_unused ina209_of_match[] = {
5850b7abe59SJavier Martinez Canillas 	{ .compatible = "ti,ina209" },
5860b7abe59SJavier Martinez Canillas 	{ },
5870b7abe59SJavier Martinez Canillas };
5880b7abe59SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, ina209_of_match);
5890b7abe59SJavier Martinez Canillas 
590ceeadc5cSGuenter Roeck /* This is the driver that will be inserted */
591ceeadc5cSGuenter Roeck static struct i2c_driver ina209_driver = {
592ceeadc5cSGuenter Roeck 	.class		= I2C_CLASS_HWMON,
593ceeadc5cSGuenter Roeck 	.driver = {
594ceeadc5cSGuenter Roeck 		.name	= "ina209",
5950b7abe59SJavier Martinez Canillas 		.of_match_table = of_match_ptr(ina209_of_match),
596ceeadc5cSGuenter Roeck 	},
597*1975d167SUwe Kleine-König 	.probe		= ina209_probe,
598ceeadc5cSGuenter Roeck 	.remove		= ina209_remove,
599ceeadc5cSGuenter Roeck 	.id_table	= ina209_id,
600ceeadc5cSGuenter Roeck };
601ceeadc5cSGuenter Roeck 
602ceeadc5cSGuenter Roeck module_i2c_driver(ina209_driver);
603ceeadc5cSGuenter Roeck 
604ceeadc5cSGuenter Roeck MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>, Paul Hays <Paul.Hays@cattail.ca>, Guenter Roeck <linux@roeck-us.net>");
605ceeadc5cSGuenter Roeck MODULE_DESCRIPTION("INA209 driver");
606ceeadc5cSGuenter Roeck MODULE_LICENSE("GPL");
607