Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for Texas Instruments INA219, INA226 power monitor chips
  4 *
  5 * INA219:
  6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
  7 * Datasheet: https://www.ti.com/product/ina219
  8 *
  9 * INA220:
 10 * Bi-Directional Current/Power Monitor with I2C Interface
 11 * Datasheet: https://www.ti.com/product/ina220
 12 *
 13 * INA226:
 14 * Bi-Directional Current/Power Monitor with I2C Interface
 15 * Datasheet: https://www.ti.com/product/ina226
 16 *
 17 * INA230:
 18 * Bi-directional Current/Power Monitor with I2C Interface
 19 * Datasheet: https://www.ti.com/product/ina230
 20 *
 21 * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
 22 * Thanks to Jan Volkering
 23 */
 24
 25#include <linux/kernel.h>
 26#include <linux/module.h>
 27#include <linux/init.h>
 28#include <linux/err.h>
 29#include <linux/slab.h>
 30#include <linux/i2c.h>
 31#include <linux/hwmon.h>
 32#include <linux/hwmon-sysfs.h>
 33#include <linux/jiffies.h>
 
 34#include <linux/of.h>
 35#include <linux/delay.h>
 36#include <linux/util_macros.h>
 37#include <linux/regmap.h>
 38
 39#include <linux/platform_data/ina2xx.h>
 40
 41/* common register definitions */
 42#define INA2XX_CONFIG			0x00
 43#define INA2XX_SHUNT_VOLTAGE		0x01 /* readonly */
 44#define INA2XX_BUS_VOLTAGE		0x02 /* readonly */
 45#define INA2XX_POWER			0x03 /* readonly */
 46#define INA2XX_CURRENT			0x04 /* readonly */
 47#define INA2XX_CALIBRATION		0x05
 48
 49/* INA226 register definitions */
 50#define INA226_MASK_ENABLE		0x06
 51#define INA226_ALERT_LIMIT		0x07
 52#define INA226_DIE_ID			0xFF
 53
 54/* register count */
 55#define INA219_REGISTERS		6
 56#define INA226_REGISTERS		8
 57
 58#define INA2XX_MAX_REGISTERS		8
 59
 60/* settings - depend on use case */
 61#define INA219_CONFIG_DEFAULT		0x399F	/* PGA=8 */
 62#define INA226_CONFIG_DEFAULT		0x4527	/* averages=16 */
 63
 64/* worst case is 68.10 ms (~14.6Hz, ina219) */
 65#define INA2XX_CONVERSION_RATE		15
 66#define INA2XX_MAX_DELAY		69 /* worst case delay in ms */
 67
 68#define INA2XX_RSHUNT_DEFAULT		10000
 69
 70/* bit mask for reading the averaging setting in the configuration register */
 71#define INA226_AVG_RD_MASK		0x0E00
 72
 73#define INA226_READ_AVG(reg)		(((reg) & INA226_AVG_RD_MASK) >> 9)
 74#define INA226_SHIFT_AVG(val)		((val) << 9)
 75
 76/* bit number of alert functions in Mask/Enable Register */
 77#define INA226_SHUNT_OVER_VOLTAGE_BIT	15
 78#define INA226_SHUNT_UNDER_VOLTAGE_BIT	14
 79#define INA226_BUS_OVER_VOLTAGE_BIT	13
 80#define INA226_BUS_UNDER_VOLTAGE_BIT	12
 81#define INA226_POWER_OVER_LIMIT_BIT	11
 82
 83/* bit mask for alert config bits of Mask/Enable Register */
 84#define INA226_ALERT_CONFIG_MASK	0xFC00
 85#define INA226_ALERT_FUNCTION_FLAG	BIT(4)
 86
 87/* common attrs, ina226 attrs and NULL */
 88#define INA2XX_MAX_ATTRIBUTE_GROUPS	3
 89
 90/*
 91 * Both bus voltage and shunt voltage conversion times for ina226 are set
 92 * to 0b0100 on POR, which translates to 2200 microseconds in total.
 93 */
 94#define INA226_TOTAL_CONV_TIME_DEFAULT	2200
 95
 96static struct regmap_config ina2xx_regmap_config = {
 97	.reg_bits = 8,
 98	.val_bits = 16,
 99};
100
101enum ina2xx_ids { ina219, ina226 };
102
103struct ina2xx_config {
104	u16 config_default;
105	int calibration_value;
106	int registers;
107	int shunt_div;
108	int bus_voltage_shift;
109	int bus_voltage_lsb;	/* uV */
110	int power_lsb_factor;
111};
112
113struct ina2xx_data {
114	const struct ina2xx_config *config;
115
116	long rshunt;
117	long current_lsb_uA;
118	long power_lsb_uW;
119	struct mutex config_lock;
120	struct regmap *regmap;
121
122	const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
123};
124
125static const struct ina2xx_config ina2xx_config[] = {
126	[ina219] = {
127		.config_default = INA219_CONFIG_DEFAULT,
128		.calibration_value = 4096,
129		.registers = INA219_REGISTERS,
130		.shunt_div = 100,
131		.bus_voltage_shift = 3,
132		.bus_voltage_lsb = 4000,
133		.power_lsb_factor = 20,
134	},
135	[ina226] = {
136		.config_default = INA226_CONFIG_DEFAULT,
137		.calibration_value = 2048,
138		.registers = INA226_REGISTERS,
139		.shunt_div = 400,
140		.bus_voltage_shift = 0,
141		.bus_voltage_lsb = 1250,
142		.power_lsb_factor = 25,
143	},
144};
145
146/*
147 * Available averaging rates for ina226. The indices correspond with
148 * the bit values expected by the chip (according to the ina226 datasheet,
149 * table 3 AVG bit settings, found at
150 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
151 */
152static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
153
154static int ina226_reg_to_interval(u16 config)
155{
156	int avg = ina226_avg_tab[INA226_READ_AVG(config)];
157
158	/*
159	 * Multiply the total conversion time by the number of averages.
160	 * Return the result in milliseconds.
161	 */
162	return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
163}
164
165/*
166 * Return the new, shifted AVG field value of CONFIG register,
167 * to use with regmap_update_bits
168 */
169static u16 ina226_interval_to_reg(int interval)
170{
171	int avg, avg_bits;
172
173	avg = DIV_ROUND_CLOSEST(interval * 1000,
174				INA226_TOTAL_CONV_TIME_DEFAULT);
175	avg_bits = find_closest(avg, ina226_avg_tab,
176				ARRAY_SIZE(ina226_avg_tab));
177
178	return INA226_SHIFT_AVG(avg_bits);
179}
180
181/*
182 * Calibration register is set to the best value, which eliminates
183 * truncation errors on calculating current register in hardware.
184 * According to datasheet (eq. 3) the best values are 2048 for
185 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
186 */
187static int ina2xx_calibrate(struct ina2xx_data *data)
188{
189	return regmap_write(data->regmap, INA2XX_CALIBRATION,
190			    data->config->calibration_value);
191}
192
193/*
194 * Initialize the configuration and calibration registers.
195 */
196static int ina2xx_init(struct ina2xx_data *data)
197{
198	int ret = regmap_write(data->regmap, INA2XX_CONFIG,
199			       data->config->config_default);
200	if (ret < 0)
201		return ret;
202
203	return ina2xx_calibrate(data);
204}
205
206static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
207{
208	struct ina2xx_data *data = dev_get_drvdata(dev);
209	int ret, retry;
210
211	dev_dbg(dev, "Starting register %d read\n", reg);
212
213	for (retry = 5; retry; retry--) {
214
215		ret = regmap_read(data->regmap, reg, regval);
216		if (ret < 0)
217			return ret;
218
219		dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
220
221		/*
222		 * If the current value in the calibration register is 0, the
223		 * power and current registers will also remain at 0. In case
224		 * the chip has been reset let's check the calibration
225		 * register and reinitialize if needed.
226		 * We do that extra read of the calibration register if there
227		 * is some hint of a chip reset.
228		 */
229		if (*regval == 0) {
230			unsigned int cal;
231
232			ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
233					  &cal);
234			if (ret < 0)
235				return ret;
236
237			if (cal == 0) {
238				dev_warn(dev, "chip not calibrated, reinitializing\n");
239
240				ret = ina2xx_init(data);
241				if (ret < 0)
242					return ret;
243				/*
244				 * Let's make sure the power and current
245				 * registers have been updated before trying
246				 * again.
247				 */
248				msleep(INA2XX_MAX_DELAY);
249				continue;
250			}
251		}
252		return 0;
253	}
254
255	/*
256	 * If we're here then although all write operations succeeded, the
257	 * chip still returns 0 in the calibration register. Nothing more we
258	 * can do here.
259	 */
260	dev_err(dev, "unable to reinitialize the chip\n");
261	return -ENODEV;
262}
263
264static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
265			    unsigned int regval)
266{
267	int val;
268
269	switch (reg) {
270	case INA2XX_SHUNT_VOLTAGE:
271		/* signed register */
272		val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
273		break;
274	case INA2XX_BUS_VOLTAGE:
275		val = (regval >> data->config->bus_voltage_shift)
276		  * data->config->bus_voltage_lsb;
277		val = DIV_ROUND_CLOSEST(val, 1000);
278		break;
279	case INA2XX_POWER:
280		val = regval * data->power_lsb_uW;
281		break;
282	case INA2XX_CURRENT:
283		/* signed register, result in mA */
284		val = (s16)regval * data->current_lsb_uA;
285		val = DIV_ROUND_CLOSEST(val, 1000);
286		break;
287	case INA2XX_CALIBRATION:
288		val = regval;
289		break;
290	default:
291		/* programmer goofed */
292		WARN_ON_ONCE(1);
293		val = 0;
294		break;
295	}
296
297	return val;
298}
299
300static ssize_t ina2xx_value_show(struct device *dev,
301				 struct device_attribute *da, char *buf)
302{
303	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
304	struct ina2xx_data *data = dev_get_drvdata(dev);
305	unsigned int regval;
306
307	int err = ina2xx_read_reg(dev, attr->index, &regval);
308
309	if (err < 0)
310		return err;
311
312	return sysfs_emit(buf, "%d\n", ina2xx_get_value(data, attr->index, regval));
 
313}
314
315static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
316{
317	int reg;
318
319	switch (bit) {
320	case INA226_SHUNT_OVER_VOLTAGE_BIT:
321	case INA226_SHUNT_UNDER_VOLTAGE_BIT:
322		reg = INA2XX_SHUNT_VOLTAGE;
323		break;
324	case INA226_BUS_OVER_VOLTAGE_BIT:
325	case INA226_BUS_UNDER_VOLTAGE_BIT:
326		reg = INA2XX_BUS_VOLTAGE;
327		break;
328	case INA226_POWER_OVER_LIMIT_BIT:
329		reg = INA2XX_POWER;
330		break;
331	default:
332		/* programmer goofed */
333		WARN_ON_ONCE(1);
334		return 0;
335	}
336
337	return ina2xx_get_value(data, reg, regval);
338}
339
340/*
341 * Turns alert limit values into register values.
342 * Opposite of the formula in ina2xx_get_value().
343 */
344static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
345{
346	switch (bit) {
347	case INA226_SHUNT_OVER_VOLTAGE_BIT:
348	case INA226_SHUNT_UNDER_VOLTAGE_BIT:
349		val *= data->config->shunt_div;
350		return clamp_val(val, SHRT_MIN, SHRT_MAX);
351	case INA226_BUS_OVER_VOLTAGE_BIT:
352	case INA226_BUS_UNDER_VOLTAGE_BIT:
353		val = (val * 1000) << data->config->bus_voltage_shift;
354		val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
355		return clamp_val(val, 0, SHRT_MAX);
356	case INA226_POWER_OVER_LIMIT_BIT:
357		val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
358		return clamp_val(val, 0, USHRT_MAX);
359	default:
360		/* programmer goofed */
361		WARN_ON_ONCE(1);
362		return 0;
363	}
364}
365
366static ssize_t ina226_alert_show(struct device *dev,
367				 struct device_attribute *da, char *buf)
368{
369	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
370	struct ina2xx_data *data = dev_get_drvdata(dev);
371	int regval;
372	int val = 0;
373	int ret;
374
375	mutex_lock(&data->config_lock);
376	ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
377	if (ret)
378		goto abort;
379
380	if (regval & BIT(attr->index)) {
381		ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, &regval);
382		if (ret)
383			goto abort;
384		val = ina226_reg_to_alert(data, attr->index, regval);
385	}
386
387	ret = sysfs_emit(buf, "%d\n", val);
388abort:
389	mutex_unlock(&data->config_lock);
390	return ret;
391}
392
393static ssize_t ina226_alert_store(struct device *dev,
394				  struct device_attribute *da,
395				  const char *buf, size_t count)
396{
397	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
398	struct ina2xx_data *data = dev_get_drvdata(dev);
399	unsigned long val;
400	int ret;
401
402	ret = kstrtoul(buf, 10, &val);
403	if (ret < 0)
404		return ret;
405
406	/*
407	 * Clear all alerts first to avoid accidentally triggering ALERT pin
408	 * due to register write sequence. Then, only enable the alert
409	 * if the value is non-zero.
410	 */
411	mutex_lock(&data->config_lock);
412	ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
413				 INA226_ALERT_CONFIG_MASK, 0);
414	if (ret < 0)
415		goto abort;
416
417	ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
418			   ina226_alert_to_reg(data, attr->index, val));
419	if (ret < 0)
420		goto abort;
421
422	if (val != 0) {
423		ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
424					 INA226_ALERT_CONFIG_MASK,
425					 BIT(attr->index));
426		if (ret < 0)
427			goto abort;
428	}
429
430	ret = count;
431abort:
432	mutex_unlock(&data->config_lock);
433	return ret;
434}
435
436static ssize_t ina226_alarm_show(struct device *dev,
437				 struct device_attribute *da, char *buf)
438{
439	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
440	struct ina2xx_data *data = dev_get_drvdata(dev);
441	int regval;
442	int alarm = 0;
443	int ret;
444
445	ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
446	if (ret)
447		return ret;
448
449	alarm = (regval & BIT(attr->index)) &&
450		(regval & INA226_ALERT_FUNCTION_FLAG);
451	return sysfs_emit(buf, "%d\n", alarm);
452}
453
454/*
455 * In order to keep calibration register value fixed, the product
456 * of current_lsb and shunt_resistor should also be fixed and equal
457 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
458 * to keep the scale.
459 */
460static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
461{
462	unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
463						  data->config->shunt_div);
464	if (val <= 0 || val > dividend)
465		return -EINVAL;
466
467	mutex_lock(&data->config_lock);
468	data->rshunt = val;
469	data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
470	data->power_lsb_uW = data->config->power_lsb_factor *
471			     data->current_lsb_uA;
472	mutex_unlock(&data->config_lock);
473
474	return 0;
475}
476
477static ssize_t ina2xx_shunt_show(struct device *dev,
478				 struct device_attribute *da, char *buf)
479{
480	struct ina2xx_data *data = dev_get_drvdata(dev);
481
482	return sysfs_emit(buf, "%li\n", data->rshunt);
483}
484
485static ssize_t ina2xx_shunt_store(struct device *dev,
486				  struct device_attribute *da,
487				  const char *buf, size_t count)
488{
489	unsigned long val;
490	int status;
491	struct ina2xx_data *data = dev_get_drvdata(dev);
492
493	status = kstrtoul(buf, 10, &val);
494	if (status < 0)
495		return status;
496
497	status = ina2xx_set_shunt(data, val);
498	if (status < 0)
499		return status;
500	return count;
501}
502
503static ssize_t ina226_interval_store(struct device *dev,
504				     struct device_attribute *da,
505				     const char *buf, size_t count)
506{
507	struct ina2xx_data *data = dev_get_drvdata(dev);
508	unsigned long val;
509	int status;
510
511	status = kstrtoul(buf, 10, &val);
512	if (status < 0)
513		return status;
514
515	if (val > INT_MAX || val == 0)
516		return -EINVAL;
517
518	status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
519				    INA226_AVG_RD_MASK,
520				    ina226_interval_to_reg(val));
521	if (status < 0)
522		return status;
523
524	return count;
525}
526
527static ssize_t ina226_interval_show(struct device *dev,
528				    struct device_attribute *da, char *buf)
529{
530	struct ina2xx_data *data = dev_get_drvdata(dev);
531	int status;
532	unsigned int regval;
533
534	status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
535	if (status)
536		return status;
537
538	return sysfs_emit(buf, "%d\n", ina226_reg_to_interval(regval));
539}
540
541/* shunt voltage */
542static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
543/* shunt voltage over/under voltage alert setting and alarm */
544static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
545			     INA226_SHUNT_OVER_VOLTAGE_BIT);
546static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
547			     INA226_SHUNT_UNDER_VOLTAGE_BIT);
548static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
549			     INA226_SHUNT_OVER_VOLTAGE_BIT);
550static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
551			     INA226_SHUNT_UNDER_VOLTAGE_BIT);
552
553/* bus voltage */
554static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
555/* bus voltage over/under voltage alert setting and alarm */
556static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
557			     INA226_BUS_OVER_VOLTAGE_BIT);
558static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
559			     INA226_BUS_UNDER_VOLTAGE_BIT);
560static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
561			     INA226_BUS_OVER_VOLTAGE_BIT);
562static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
563			     INA226_BUS_UNDER_VOLTAGE_BIT);
564
565/* calculated current */
566static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
567
568/* calculated power */
569static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
570/* over-limit power alert setting and alarm */
571static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
572			     INA226_POWER_OVER_LIMIT_BIT);
573static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
574			     INA226_POWER_OVER_LIMIT_BIT);
575
576/* shunt resistance */
577static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
578
579/* update interval (ina226 only) */
580static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
581
582/* pointers to created device attributes */
583static struct attribute *ina2xx_attrs[] = {
584	&sensor_dev_attr_in0_input.dev_attr.attr,
585	&sensor_dev_attr_in1_input.dev_attr.attr,
586	&sensor_dev_attr_curr1_input.dev_attr.attr,
587	&sensor_dev_attr_power1_input.dev_attr.attr,
588	&sensor_dev_attr_shunt_resistor.dev_attr.attr,
589	NULL,
590};
591
592static const struct attribute_group ina2xx_group = {
593	.attrs = ina2xx_attrs,
594};
595
596static struct attribute *ina226_attrs[] = {
597	&sensor_dev_attr_in0_crit.dev_attr.attr,
598	&sensor_dev_attr_in0_lcrit.dev_attr.attr,
599	&sensor_dev_attr_in0_crit_alarm.dev_attr.attr,
600	&sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr,
601	&sensor_dev_attr_in1_crit.dev_attr.attr,
602	&sensor_dev_attr_in1_lcrit.dev_attr.attr,
603	&sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
604	&sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr,
605	&sensor_dev_attr_power1_crit.dev_attr.attr,
606	&sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
607	&sensor_dev_attr_update_interval.dev_attr.attr,
608	NULL,
609};
610
611static const struct attribute_group ina226_group = {
612	.attrs = ina226_attrs,
613};
614
615static const struct i2c_device_id ina2xx_id[];
616
617static int ina2xx_probe(struct i2c_client *client)
618{
619	struct device *dev = &client->dev;
620	struct ina2xx_data *data;
621	struct device *hwmon_dev;
622	u32 val;
623	int ret, group = 0;
624	enum ina2xx_ids chip;
625
626	if (client->dev.of_node)
627		chip = (uintptr_t)of_device_get_match_data(&client->dev);
628	else
629		chip = i2c_match_id(ina2xx_id, client)->driver_data;
630
631	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
632	if (!data)
633		return -ENOMEM;
634
635	/* set the device type */
636	data->config = &ina2xx_config[chip];
637	mutex_init(&data->config_lock);
638
639	if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
640		struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
641
642		if (pdata)
643			val = pdata->shunt_uohms;
644		else
645			val = INA2XX_RSHUNT_DEFAULT;
646	}
647
648	ina2xx_set_shunt(data, val);
649
650	ina2xx_regmap_config.max_register = data->config->registers;
651
652	data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
653	if (IS_ERR(data->regmap)) {
654		dev_err(dev, "failed to allocate register map\n");
655		return PTR_ERR(data->regmap);
656	}
657
658	ret = devm_regulator_get_enable(dev, "vs");
659	if (ret)
660		return dev_err_probe(dev, ret, "failed to enable vs regulator\n");
661
662	ret = ina2xx_init(data);
663	if (ret < 0) {
664		dev_err(dev, "error configuring the device: %d\n", ret);
665		return -ENODEV;
666	}
667
668	data->groups[group++] = &ina2xx_group;
669	if (chip == ina226)
670		data->groups[group++] = &ina226_group;
671
672	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
673							   data, data->groups);
674	if (IS_ERR(hwmon_dev))
675		return PTR_ERR(hwmon_dev);
676
677	dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
678		 client->name, data->rshunt);
679
680	return 0;
681}
682
683static const struct i2c_device_id ina2xx_id[] = {
684	{ "ina219", ina219 },
685	{ "ina220", ina219 },
686	{ "ina226", ina226 },
687	{ "ina230", ina226 },
688	{ "ina231", ina226 },
689	{ }
690};
691MODULE_DEVICE_TABLE(i2c, ina2xx_id);
692
693static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
694	{
695		.compatible = "ti,ina219",
696		.data = (void *)ina219
697	},
698	{
699		.compatible = "ti,ina220",
700		.data = (void *)ina219
701	},
702	{
703		.compatible = "ti,ina226",
704		.data = (void *)ina226
705	},
706	{
707		.compatible = "ti,ina230",
708		.data = (void *)ina226
709	},
710	{
711		.compatible = "ti,ina231",
712		.data = (void *)ina226
713	},
714	{ },
715};
716MODULE_DEVICE_TABLE(of, ina2xx_of_match);
717
718static struct i2c_driver ina2xx_driver = {
719	.driver = {
720		.name	= "ina2xx",
721		.of_match_table = of_match_ptr(ina2xx_of_match),
722	},
723	.probe		= ina2xx_probe,
724	.id_table	= ina2xx_id,
725};
726
727module_i2c_driver(ina2xx_driver);
728
729MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
730MODULE_DESCRIPTION("ina2xx driver");
731MODULE_LICENSE("GPL");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for Texas Instruments INA219, INA226 power monitor chips
  4 *
  5 * INA219:
  6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
  7 * Datasheet: https://www.ti.com/product/ina219
  8 *
  9 * INA220:
 10 * Bi-Directional Current/Power Monitor with I2C Interface
 11 * Datasheet: https://www.ti.com/product/ina220
 12 *
 13 * INA226:
 14 * Bi-Directional Current/Power Monitor with I2C Interface
 15 * Datasheet: https://www.ti.com/product/ina226
 16 *
 17 * INA230:
 18 * Bi-directional Current/Power Monitor with I2C Interface
 19 * Datasheet: https://www.ti.com/product/ina230
 20 *
 21 * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
 22 * Thanks to Jan Volkering
 23 */
 24
 25#include <linux/kernel.h>
 26#include <linux/module.h>
 27#include <linux/init.h>
 28#include <linux/err.h>
 29#include <linux/slab.h>
 30#include <linux/i2c.h>
 31#include <linux/hwmon.h>
 32#include <linux/hwmon-sysfs.h>
 33#include <linux/jiffies.h>
 34#include <linux/of_device.h>
 35#include <linux/of.h>
 36#include <linux/delay.h>
 37#include <linux/util_macros.h>
 38#include <linux/regmap.h>
 39
 40#include <linux/platform_data/ina2xx.h>
 41
 42/* common register definitions */
 43#define INA2XX_CONFIG			0x00
 44#define INA2XX_SHUNT_VOLTAGE		0x01 /* readonly */
 45#define INA2XX_BUS_VOLTAGE		0x02 /* readonly */
 46#define INA2XX_POWER			0x03 /* readonly */
 47#define INA2XX_CURRENT			0x04 /* readonly */
 48#define INA2XX_CALIBRATION		0x05
 49
 50/* INA226 register definitions */
 51#define INA226_MASK_ENABLE		0x06
 52#define INA226_ALERT_LIMIT		0x07
 53#define INA226_DIE_ID			0xFF
 54
 55/* register count */
 56#define INA219_REGISTERS		6
 57#define INA226_REGISTERS		8
 58
 59#define INA2XX_MAX_REGISTERS		8
 60
 61/* settings - depend on use case */
 62#define INA219_CONFIG_DEFAULT		0x399F	/* PGA=8 */
 63#define INA226_CONFIG_DEFAULT		0x4527	/* averages=16 */
 64
 65/* worst case is 68.10 ms (~14.6Hz, ina219) */
 66#define INA2XX_CONVERSION_RATE		15
 67#define INA2XX_MAX_DELAY		69 /* worst case delay in ms */
 68
 69#define INA2XX_RSHUNT_DEFAULT		10000
 70
 71/* bit mask for reading the averaging setting in the configuration register */
 72#define INA226_AVG_RD_MASK		0x0E00
 73
 74#define INA226_READ_AVG(reg)		(((reg) & INA226_AVG_RD_MASK) >> 9)
 75#define INA226_SHIFT_AVG(val)		((val) << 9)
 76
 77/* bit number of alert functions in Mask/Enable Register */
 78#define INA226_SHUNT_OVER_VOLTAGE_BIT	15
 79#define INA226_SHUNT_UNDER_VOLTAGE_BIT	14
 80#define INA226_BUS_OVER_VOLTAGE_BIT	13
 81#define INA226_BUS_UNDER_VOLTAGE_BIT	12
 82#define INA226_POWER_OVER_LIMIT_BIT	11
 83
 84/* bit mask for alert config bits of Mask/Enable Register */
 85#define INA226_ALERT_CONFIG_MASK	0xFC00
 86#define INA226_ALERT_FUNCTION_FLAG	BIT(4)
 87
 88/* common attrs, ina226 attrs and NULL */
 89#define INA2XX_MAX_ATTRIBUTE_GROUPS	3
 90
 91/*
 92 * Both bus voltage and shunt voltage conversion times for ina226 are set
 93 * to 0b0100 on POR, which translates to 2200 microseconds in total.
 94 */
 95#define INA226_TOTAL_CONV_TIME_DEFAULT	2200
 96
 97static struct regmap_config ina2xx_regmap_config = {
 98	.reg_bits = 8,
 99	.val_bits = 16,
100};
101
102enum ina2xx_ids { ina219, ina226 };
103
104struct ina2xx_config {
105	u16 config_default;
106	int calibration_value;
107	int registers;
108	int shunt_div;
109	int bus_voltage_shift;
110	int bus_voltage_lsb;	/* uV */
111	int power_lsb_factor;
112};
113
114struct ina2xx_data {
115	const struct ina2xx_config *config;
116
117	long rshunt;
118	long current_lsb_uA;
119	long power_lsb_uW;
120	struct mutex config_lock;
121	struct regmap *regmap;
122
123	const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
124};
125
126static const struct ina2xx_config ina2xx_config[] = {
127	[ina219] = {
128		.config_default = INA219_CONFIG_DEFAULT,
129		.calibration_value = 4096,
130		.registers = INA219_REGISTERS,
131		.shunt_div = 100,
132		.bus_voltage_shift = 3,
133		.bus_voltage_lsb = 4000,
134		.power_lsb_factor = 20,
135	},
136	[ina226] = {
137		.config_default = INA226_CONFIG_DEFAULT,
138		.calibration_value = 2048,
139		.registers = INA226_REGISTERS,
140		.shunt_div = 400,
141		.bus_voltage_shift = 0,
142		.bus_voltage_lsb = 1250,
143		.power_lsb_factor = 25,
144	},
145};
146
147/*
148 * Available averaging rates for ina226. The indices correspond with
149 * the bit values expected by the chip (according to the ina226 datasheet,
150 * table 3 AVG bit settings, found at
151 * https://www.ti.com/lit/ds/symlink/ina226.pdf.
152 */
153static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
154
155static int ina226_reg_to_interval(u16 config)
156{
157	int avg = ina226_avg_tab[INA226_READ_AVG(config)];
158
159	/*
160	 * Multiply the total conversion time by the number of averages.
161	 * Return the result in milliseconds.
162	 */
163	return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
164}
165
166/*
167 * Return the new, shifted AVG field value of CONFIG register,
168 * to use with regmap_update_bits
169 */
170static u16 ina226_interval_to_reg(int interval)
171{
172	int avg, avg_bits;
173
174	avg = DIV_ROUND_CLOSEST(interval * 1000,
175				INA226_TOTAL_CONV_TIME_DEFAULT);
176	avg_bits = find_closest(avg, ina226_avg_tab,
177				ARRAY_SIZE(ina226_avg_tab));
178
179	return INA226_SHIFT_AVG(avg_bits);
180}
181
182/*
183 * Calibration register is set to the best value, which eliminates
184 * truncation errors on calculating current register in hardware.
185 * According to datasheet (eq. 3) the best values are 2048 for
186 * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
187 */
188static int ina2xx_calibrate(struct ina2xx_data *data)
189{
190	return regmap_write(data->regmap, INA2XX_CALIBRATION,
191			    data->config->calibration_value);
192}
193
194/*
195 * Initialize the configuration and calibration registers.
196 */
197static int ina2xx_init(struct ina2xx_data *data)
198{
199	int ret = regmap_write(data->regmap, INA2XX_CONFIG,
200			       data->config->config_default);
201	if (ret < 0)
202		return ret;
203
204	return ina2xx_calibrate(data);
205}
206
207static int ina2xx_read_reg(struct device *dev, int reg, unsigned int *regval)
208{
209	struct ina2xx_data *data = dev_get_drvdata(dev);
210	int ret, retry;
211
212	dev_dbg(dev, "Starting register %d read\n", reg);
213
214	for (retry = 5; retry; retry--) {
215
216		ret = regmap_read(data->regmap, reg, regval);
217		if (ret < 0)
218			return ret;
219
220		dev_dbg(dev, "read %d, val = 0x%04x\n", reg, *regval);
221
222		/*
223		 * If the current value in the calibration register is 0, the
224		 * power and current registers will also remain at 0. In case
225		 * the chip has been reset let's check the calibration
226		 * register and reinitialize if needed.
227		 * We do that extra read of the calibration register if there
228		 * is some hint of a chip reset.
229		 */
230		if (*regval == 0) {
231			unsigned int cal;
232
233			ret = regmap_read(data->regmap, INA2XX_CALIBRATION,
234					  &cal);
235			if (ret < 0)
236				return ret;
237
238			if (cal == 0) {
239				dev_warn(dev, "chip not calibrated, reinitializing\n");
240
241				ret = ina2xx_init(data);
242				if (ret < 0)
243					return ret;
244				/*
245				 * Let's make sure the power and current
246				 * registers have been updated before trying
247				 * again.
248				 */
249				msleep(INA2XX_MAX_DELAY);
250				continue;
251			}
252		}
253		return 0;
254	}
255
256	/*
257	 * If we're here then although all write operations succeeded, the
258	 * chip still returns 0 in the calibration register. Nothing more we
259	 * can do here.
260	 */
261	dev_err(dev, "unable to reinitialize the chip\n");
262	return -ENODEV;
263}
264
265static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
266			    unsigned int regval)
267{
268	int val;
269
270	switch (reg) {
271	case INA2XX_SHUNT_VOLTAGE:
272		/* signed register */
273		val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
274		break;
275	case INA2XX_BUS_VOLTAGE:
276		val = (regval >> data->config->bus_voltage_shift)
277		  * data->config->bus_voltage_lsb;
278		val = DIV_ROUND_CLOSEST(val, 1000);
279		break;
280	case INA2XX_POWER:
281		val = regval * data->power_lsb_uW;
282		break;
283	case INA2XX_CURRENT:
284		/* signed register, result in mA */
285		val = (s16)regval * data->current_lsb_uA;
286		val = DIV_ROUND_CLOSEST(val, 1000);
287		break;
288	case INA2XX_CALIBRATION:
289		val = regval;
290		break;
291	default:
292		/* programmer goofed */
293		WARN_ON_ONCE(1);
294		val = 0;
295		break;
296	}
297
298	return val;
299}
300
301static ssize_t ina2xx_value_show(struct device *dev,
302				 struct device_attribute *da, char *buf)
303{
304	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
305	struct ina2xx_data *data = dev_get_drvdata(dev);
306	unsigned int regval;
307
308	int err = ina2xx_read_reg(dev, attr->index, &regval);
309
310	if (err < 0)
311		return err;
312
313	return snprintf(buf, PAGE_SIZE, "%d\n",
314			ina2xx_get_value(data, attr->index, regval));
315}
316
317static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
318{
319	int reg;
320
321	switch (bit) {
322	case INA226_SHUNT_OVER_VOLTAGE_BIT:
323	case INA226_SHUNT_UNDER_VOLTAGE_BIT:
324		reg = INA2XX_SHUNT_VOLTAGE;
325		break;
326	case INA226_BUS_OVER_VOLTAGE_BIT:
327	case INA226_BUS_UNDER_VOLTAGE_BIT:
328		reg = INA2XX_BUS_VOLTAGE;
329		break;
330	case INA226_POWER_OVER_LIMIT_BIT:
331		reg = INA2XX_POWER;
332		break;
333	default:
334		/* programmer goofed */
335		WARN_ON_ONCE(1);
336		return 0;
337	}
338
339	return ina2xx_get_value(data, reg, regval);
340}
341
342/*
343 * Turns alert limit values into register values.
344 * Opposite of the formula in ina2xx_get_value().
345 */
346static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
347{
348	switch (bit) {
349	case INA226_SHUNT_OVER_VOLTAGE_BIT:
350	case INA226_SHUNT_UNDER_VOLTAGE_BIT:
351		val *= data->config->shunt_div;
352		return clamp_val(val, SHRT_MIN, SHRT_MAX);
353	case INA226_BUS_OVER_VOLTAGE_BIT:
354	case INA226_BUS_UNDER_VOLTAGE_BIT:
355		val = (val * 1000) << data->config->bus_voltage_shift;
356		val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
357		return clamp_val(val, 0, SHRT_MAX);
358	case INA226_POWER_OVER_LIMIT_BIT:
359		val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
360		return clamp_val(val, 0, USHRT_MAX);
361	default:
362		/* programmer goofed */
363		WARN_ON_ONCE(1);
364		return 0;
365	}
366}
367
368static ssize_t ina226_alert_show(struct device *dev,
369				 struct device_attribute *da, char *buf)
370{
371	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
372	struct ina2xx_data *data = dev_get_drvdata(dev);
373	int regval;
374	int val = 0;
375	int ret;
376
377	mutex_lock(&data->config_lock);
378	ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
379	if (ret)
380		goto abort;
381
382	if (regval & BIT(attr->index)) {
383		ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, &regval);
384		if (ret)
385			goto abort;
386		val = ina226_reg_to_alert(data, attr->index, regval);
387	}
388
389	ret = snprintf(buf, PAGE_SIZE, "%d\n", val);
390abort:
391	mutex_unlock(&data->config_lock);
392	return ret;
393}
394
395static ssize_t ina226_alert_store(struct device *dev,
396				  struct device_attribute *da,
397				  const char *buf, size_t count)
398{
399	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
400	struct ina2xx_data *data = dev_get_drvdata(dev);
401	unsigned long val;
402	int ret;
403
404	ret = kstrtoul(buf, 10, &val);
405	if (ret < 0)
406		return ret;
407
408	/*
409	 * Clear all alerts first to avoid accidentally triggering ALERT pin
410	 * due to register write sequence. Then, only enable the alert
411	 * if the value is non-zero.
412	 */
413	mutex_lock(&data->config_lock);
414	ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
415				 INA226_ALERT_CONFIG_MASK, 0);
416	if (ret < 0)
417		goto abort;
418
419	ret = regmap_write(data->regmap, INA226_ALERT_LIMIT,
420			   ina226_alert_to_reg(data, attr->index, val));
421	if (ret < 0)
422		goto abort;
423
424	if (val != 0) {
425		ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
426					 INA226_ALERT_CONFIG_MASK,
427					 BIT(attr->index));
428		if (ret < 0)
429			goto abort;
430	}
431
432	ret = count;
433abort:
434	mutex_unlock(&data->config_lock);
435	return ret;
436}
437
438static ssize_t ina226_alarm_show(struct device *dev,
439				 struct device_attribute *da, char *buf)
440{
441	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
442	struct ina2xx_data *data = dev_get_drvdata(dev);
443	int regval;
444	int alarm = 0;
445	int ret;
446
447	ret = regmap_read(data->regmap, INA226_MASK_ENABLE, &regval);
448	if (ret)
449		return ret;
450
451	alarm = (regval & BIT(attr->index)) &&
452		(regval & INA226_ALERT_FUNCTION_FLAG);
453	return snprintf(buf, PAGE_SIZE, "%d\n", alarm);
454}
455
456/*
457 * In order to keep calibration register value fixed, the product
458 * of current_lsb and shunt_resistor should also be fixed and equal
459 * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
460 * to keep the scale.
461 */
462static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
463{
464	unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
465						  data->config->shunt_div);
466	if (val <= 0 || val > dividend)
467		return -EINVAL;
468
469	mutex_lock(&data->config_lock);
470	data->rshunt = val;
471	data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
472	data->power_lsb_uW = data->config->power_lsb_factor *
473			     data->current_lsb_uA;
474	mutex_unlock(&data->config_lock);
475
476	return 0;
477}
478
479static ssize_t ina2xx_shunt_show(struct device *dev,
480				 struct device_attribute *da, char *buf)
481{
482	struct ina2xx_data *data = dev_get_drvdata(dev);
483
484	return snprintf(buf, PAGE_SIZE, "%li\n", data->rshunt);
485}
486
487static ssize_t ina2xx_shunt_store(struct device *dev,
488				  struct device_attribute *da,
489				  const char *buf, size_t count)
490{
491	unsigned long val;
492	int status;
493	struct ina2xx_data *data = dev_get_drvdata(dev);
494
495	status = kstrtoul(buf, 10, &val);
496	if (status < 0)
497		return status;
498
499	status = ina2xx_set_shunt(data, val);
500	if (status < 0)
501		return status;
502	return count;
503}
504
505static ssize_t ina226_interval_store(struct device *dev,
506				     struct device_attribute *da,
507				     const char *buf, size_t count)
508{
509	struct ina2xx_data *data = dev_get_drvdata(dev);
510	unsigned long val;
511	int status;
512
513	status = kstrtoul(buf, 10, &val);
514	if (status < 0)
515		return status;
516
517	if (val > INT_MAX || val == 0)
518		return -EINVAL;
519
520	status = regmap_update_bits(data->regmap, INA2XX_CONFIG,
521				    INA226_AVG_RD_MASK,
522				    ina226_interval_to_reg(val));
523	if (status < 0)
524		return status;
525
526	return count;
527}
528
529static ssize_t ina226_interval_show(struct device *dev,
530				    struct device_attribute *da, char *buf)
531{
532	struct ina2xx_data *data = dev_get_drvdata(dev);
533	int status;
534	unsigned int regval;
535
536	status = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
537	if (status)
538		return status;
539
540	return snprintf(buf, PAGE_SIZE, "%d\n", ina226_reg_to_interval(regval));
541}
542
543/* shunt voltage */
544static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
545/* shunt voltage over/under voltage alert setting and alarm */
546static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
547			     INA226_SHUNT_OVER_VOLTAGE_BIT);
548static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
549			     INA226_SHUNT_UNDER_VOLTAGE_BIT);
550static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
551			     INA226_SHUNT_OVER_VOLTAGE_BIT);
552static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
553			     INA226_SHUNT_UNDER_VOLTAGE_BIT);
554
555/* bus voltage */
556static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
557/* bus voltage over/under voltage alert setting and alarm */
558static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
559			     INA226_BUS_OVER_VOLTAGE_BIT);
560static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
561			     INA226_BUS_UNDER_VOLTAGE_BIT);
562static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
563			     INA226_BUS_OVER_VOLTAGE_BIT);
564static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
565			     INA226_BUS_UNDER_VOLTAGE_BIT);
566
567/* calculated current */
568static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
569
570/* calculated power */
571static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
572/* over-limit power alert setting and alarm */
573static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
574			     INA226_POWER_OVER_LIMIT_BIT);
575static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
576			     INA226_POWER_OVER_LIMIT_BIT);
577
578/* shunt resistance */
579static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
580
581/* update interval (ina226 only) */
582static SENSOR_DEVICE_ATTR_RW(update_interval, ina226_interval, 0);
583
584/* pointers to created device attributes */
585static struct attribute *ina2xx_attrs[] = {
586	&sensor_dev_attr_in0_input.dev_attr.attr,
587	&sensor_dev_attr_in1_input.dev_attr.attr,
588	&sensor_dev_attr_curr1_input.dev_attr.attr,
589	&sensor_dev_attr_power1_input.dev_attr.attr,
590	&sensor_dev_attr_shunt_resistor.dev_attr.attr,
591	NULL,
592};
593
594static const struct attribute_group ina2xx_group = {
595	.attrs = ina2xx_attrs,
596};
597
598static struct attribute *ina226_attrs[] = {
599	&sensor_dev_attr_in0_crit.dev_attr.attr,
600	&sensor_dev_attr_in0_lcrit.dev_attr.attr,
601	&sensor_dev_attr_in0_crit_alarm.dev_attr.attr,
602	&sensor_dev_attr_in0_lcrit_alarm.dev_attr.attr,
603	&sensor_dev_attr_in1_crit.dev_attr.attr,
604	&sensor_dev_attr_in1_lcrit.dev_attr.attr,
605	&sensor_dev_attr_in1_crit_alarm.dev_attr.attr,
606	&sensor_dev_attr_in1_lcrit_alarm.dev_attr.attr,
607	&sensor_dev_attr_power1_crit.dev_attr.attr,
608	&sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
609	&sensor_dev_attr_update_interval.dev_attr.attr,
610	NULL,
611};
612
613static const struct attribute_group ina226_group = {
614	.attrs = ina226_attrs,
615};
616
617static int ina2xx_probe(struct i2c_client *client,
618			const struct i2c_device_id *id)
 
619{
620	struct device *dev = &client->dev;
621	struct ina2xx_data *data;
622	struct device *hwmon_dev;
623	u32 val;
624	int ret, group = 0;
625	enum ina2xx_ids chip;
626
627	if (client->dev.of_node)
628		chip = (enum ina2xx_ids)of_device_get_match_data(&client->dev);
629	else
630		chip = id->driver_data;
631
632	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
633	if (!data)
634		return -ENOMEM;
635
636	/* set the device type */
637	data->config = &ina2xx_config[chip];
638	mutex_init(&data->config_lock);
639
640	if (of_property_read_u32(dev->of_node, "shunt-resistor", &val) < 0) {
641		struct ina2xx_platform_data *pdata = dev_get_platdata(dev);
642
643		if (pdata)
644			val = pdata->shunt_uohms;
645		else
646			val = INA2XX_RSHUNT_DEFAULT;
647	}
648
649	ina2xx_set_shunt(data, val);
650
651	ina2xx_regmap_config.max_register = data->config->registers;
652
653	data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
654	if (IS_ERR(data->regmap)) {
655		dev_err(dev, "failed to allocate register map\n");
656		return PTR_ERR(data->regmap);
657	}
 
 
 
 
658
659	ret = ina2xx_init(data);
660	if (ret < 0) {
661		dev_err(dev, "error configuring the device: %d\n", ret);
662		return -ENODEV;
663	}
664
665	data->groups[group++] = &ina2xx_group;
666	if (chip == ina226)
667		data->groups[group++] = &ina226_group;
668
669	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
670							   data, data->groups);
671	if (IS_ERR(hwmon_dev))
672		return PTR_ERR(hwmon_dev);
673
674	dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
675		 client->name, data->rshunt);
676
677	return 0;
678}
679
680static const struct i2c_device_id ina2xx_id[] = {
681	{ "ina219", ina219 },
682	{ "ina220", ina219 },
683	{ "ina226", ina226 },
684	{ "ina230", ina226 },
685	{ "ina231", ina226 },
686	{ }
687};
688MODULE_DEVICE_TABLE(i2c, ina2xx_id);
689
690static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
691	{
692		.compatible = "ti,ina219",
693		.data = (void *)ina219
694	},
695	{
696		.compatible = "ti,ina220",
697		.data = (void *)ina219
698	},
699	{
700		.compatible = "ti,ina226",
701		.data = (void *)ina226
702	},
703	{
704		.compatible = "ti,ina230",
705		.data = (void *)ina226
706	},
707	{
708		.compatible = "ti,ina231",
709		.data = (void *)ina226
710	},
711	{ },
712};
713MODULE_DEVICE_TABLE(of, ina2xx_of_match);
714
715static struct i2c_driver ina2xx_driver = {
716	.driver = {
717		.name	= "ina2xx",
718		.of_match_table = of_match_ptr(ina2xx_of_match),
719	},
720	.probe		= ina2xx_probe,
721	.id_table	= ina2xx_id,
722};
723
724module_i2c_driver(ina2xx_driver);
725
726MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
727MODULE_DESCRIPTION("ina2xx driver");
728MODULE_LICENSE("GPL");