linux/drivers/hwmon/ina238.c

// SPDX-License-Identifier: GPL-2.0-only
/*
 * Driver for Texas Instruments INA238 power monitor chip
 * Datasheet: https://www.ti.com/product/ina238
 *
 * Copyright (C) 2021 Nathan Rossi <[email protected]>
 */

#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regmap.h>

#include <linux/platform_data/ina2xx.h>

/* INA238 register definitions */
#define INA238_CONFIG
#define INA238_ADC_CONFIG
#define INA238_SHUNT_CALIBRATION
#define INA238_SHUNT_VOLTAGE
#define INA238_BUS_VOLTAGE
#define INA238_DIE_TEMP
#define INA238_CURRENT
#define INA238_POWER
#define INA238_DIAG_ALERT
#define INA238_SHUNT_OVER_VOLTAGE
#define INA238_SHUNT_UNDER_VOLTAGE
#define INA238_BUS_OVER_VOLTAGE
#define INA238_BUS_UNDER_VOLTAGE
#define INA238_TEMP_LIMIT
#define INA238_POWER_LIMIT
#define INA238_DEVICE_ID

#define INA238_CONFIG_ADCRANGE

#define INA238_DIAG_ALERT_TMPOL
#define INA238_DIAG_ALERT_SHNTOL
#define INA238_DIAG_ALERT_SHNTUL
#define INA238_DIAG_ALERT_BUSOL
#define INA238_DIAG_ALERT_BUSUL
#define INA238_DIAG_ALERT_POL

#define INA238_REGISTERS

#define INA238_RSHUNT_DEFAULT

/* Default configuration of device on reset. */
#define INA238_CONFIG_DEFAULT
/* 16 sample averaging, 1052us conversion time, continuous mode */
#define INA238_ADC_CONFIG_DEFAULT
/* Configure alerts to be based on averaged value (SLOWALERT) */
#define INA238_DIAG_ALERT_DEFAULT
/*
 * This driver uses a fixed calibration value in order to scale current/power
 * based on a fixed shunt resistor value. This allows for conversion within the
 * device to avoid integer limits whilst current/power accuracy is scaled
 * relative to the shunt resistor value within the driver. This is similar to
 * how the ina2xx driver handles current/power scaling.
 *
 * The end result of this is that increasing shunt values (from a fixed 20 mOhm
 * shunt) increase the effective current/power accuracy whilst limiting the
 * range and decreasing shunt values decrease the effective accuracy but
 * increase the range.
 *
 * The value of the Current register is calculated given the following:
 *   Current (A) = (shunt voltage register * 5) * calibration / 81920
 *
 * The maximum shunt voltage is 163.835 mV (0x7fff, ADC_RANGE = 0, gain = 4).
 * With the maximum current value of 0x7fff and a fixed shunt value results in
 * a calibration value of 16384 (0x4000).
 *
 *   0x7fff = (0x7fff * 5) * calibration / 81920
 *   calibration = 0x4000
 *
 * Equivalent calibration is applied for the Power register (maximum value for
 * bus voltage is 102396.875 mV, 0x7fff), where the maximum power that can
 * occur is ~16776192 uW (register value 0x147a8):
 *
 * This scaling means the resulting values for Current and Power registers need
 * to be scaled by the difference between the fixed shunt resistor and the
 * actual shunt resistor:
 *
 *  shunt = 0x4000 / (819.2 * 10^6) / 0.001 = 20000 uOhms (with 1mA/lsb)
 *
 *  Current (mA) = register value * 20000 / rshunt / 4 * gain
 *  Power (W) = 0.2 * register value * 20000 / rshunt / 4 * gain
 */
#define INA238_CALIBRATION_VALUE
#define INA238_FIXED_SHUNT

#define INA238_SHUNT_VOLTAGE_LSB
#define INA238_BUS_VOLTAGE_LSB
#define INA238_DIE_TEMP_LSB

static const struct regmap_config ina238_regmap_config =;

struct ina238_data {};

static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 *val)
{}

static int ina238_read_in(struct device *dev, u32 attr, int channel,
			  long *val)
{}

static int ina238_write_in(struct device *dev, u32 attr, int channel,
			   long val)
{}

static int ina238_read_current(struct device *dev, u32 attr, long *val)
{}

static int ina238_read_power(struct device *dev, u32 attr, long *val)
{}

static int ina238_write_power(struct device *dev, u32 attr, long val)
{}

static int ina238_read_temp(struct device *dev, u32 attr, long *val)
{}

static int ina238_write_temp(struct device *dev, u32 attr, long val)
{}

static int ina238_read(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long *val)
{}

static int ina238_write(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long val)
{}

static umode_t ina238_is_visible(const void *drvdata,
				 enum hwmon_sensor_types type,
				 u32 attr, int channel)
{}

#define INA238_HWMON_IN_CONFIG

static const struct hwmon_channel_info * const ina238_info[] =;

static const struct hwmon_ops ina238_hwmon_ops =;

static const struct hwmon_chip_info ina238_chip_info =;

static int ina238_probe(struct i2c_client *client)
{}

static const struct i2c_device_id ina238_id[] =;
MODULE_DEVICE_TABLE(i2c, ina238_id);

static const struct of_device_id __maybe_unused ina238_of_match[] =;
MODULE_DEVICE_TABLE(of, ina238_of_match);

static struct i2c_driver ina238_driver =;

module_i2c_driver();

MODULE_AUTHOR();
MODULE_DESCRIPTION();
MODULE_LICENSE();