drivers/iio/adc/ina2xx-adc.c

Source file repositories/reference/linux-study-clean/drivers/iio/adc/ina2xx-adc.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/adc/ina2xx-adc.c
Extension
.c
Size
29083 bytes
Lines
1134
Domain
Driver Families
Bucket
drivers/iio
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct ina2xx_config {
	const char *name;
	u16 config_default;
	int calibration_value;
	int shunt_voltage_lsb;	/* nV */
	int bus_voltage_shift;	/* position of lsb */
	int bus_voltage_lsb;	/* uV */
	/* fixed relation between current and power lsb, uW/uA */
	int power_lsb_factor;
	enum ina2xx_ids chip_id;
};

struct ina2xx_chip_info {
	struct regmap *regmap;
	struct task_struct *task;
	const struct ina2xx_config *config;
	struct mutex state_lock;
	unsigned int shunt_resistor_uohm;
	int avg;
	int int_time_vbus; /* Bus voltage integration time uS */
	int int_time_vshunt; /* Shunt voltage integration time uS */
	int range_vbus; /* Bus voltage maximum in V */
	int pga_gain_vshunt; /* Shunt voltage PGA gain */
	bool allow_async_readout;
	/* data buffer needs space for channel data and timestamp */
	struct {
		u16 chan[4];
		aligned_s64 ts;
	} scan;
};

static const struct ina2xx_config ina2xx_config[] = {
	[ina219] = {
		.name = "ina219",
		.config_default = INA219_CONFIG_DEFAULT,
		.calibration_value = 4096,
		.shunt_voltage_lsb = 10000,
		.bus_voltage_shift = INA219_BUS_VOLTAGE_SHIFT,
		.bus_voltage_lsb = 4000,
		.power_lsb_factor = 20,
		.chip_id = ina219,
	},
	[ina226] = {
		.name = "ina226",
		.config_default = INA226_CONFIG_DEFAULT,
		.calibration_value = 2048,
		.shunt_voltage_lsb = 2500,
		.bus_voltage_shift = 0,
		.bus_voltage_lsb = 1250,
		.power_lsb_factor = 25,
		.chip_id = ina226,
	},
	[ina236] = {
		.name = "ina236",
		.config_default = INA226_CONFIG_DEFAULT,
		.calibration_value = 2048,
		.shunt_voltage_lsb = 2500,
		.bus_voltage_shift = 0,
		.bus_voltage_lsb = 1600,
		.power_lsb_factor = 32,
		.chip_id = ina236,
	},
};

static int ina2xx_read_raw(struct iio_dev *indio_dev,
			   struct iio_chan_spec const *chan,
			   int *val, int *val2, long mask)
{
	int ret;
	struct ina2xx_chip_info *chip = iio_priv(indio_dev);
	unsigned int regval;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		ret = regmap_read(chip->regmap, chan->address, &regval);
		if (ret)
			return ret;

		if (is_signed_reg(chan->address))
			*val = (s16) regval;
		else
			*val  = regval;

		if (chan->address == INA2XX_BUS_VOLTAGE)
			*val >>= chip->config->bus_voltage_shift;

		return IIO_VAL_INT;

	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
		*val = chip->avg;

Annotation

Implementation Notes