drivers/iio/amplifiers/hmc425a.c

Source file repositories/reference/linux-study-clean/drivers/iio/amplifiers/hmc425a.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/amplifiers/hmc425a.c
Extension
.c
Size
10517 bytes
Lines
426
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 hmc425a_chip_info {
	const char			*name;
	const struct iio_chan_spec	*channels;
	unsigned int			num_channels;
	unsigned int			num_gpios;
	int				gain_min;
	int				gain_max;
	int				default_gain;
	int				powerdown_val;
	bool				has_powerdown;

	int				(*gain_dB_to_code)(int gain, int *code);
	int				(*code_to_gain_dB)(int code, int *val, int *val2);
};

struct hmc425a_state {
	struct	mutex lock; /* protect sensor state */
	const struct	hmc425a_chip_info *chip_info;
	struct	gpio_descs *gpios;
	u32	gain;
	bool	powerdown;
};

static int gain_dB_to_code(struct hmc425a_state *st, int val, int val2, int *code)
{
	const struct hmc425a_chip_info *inf = st->chip_info;
	int gain;

	if (val < 0)
		gain = (val * 1000) - (val2 / 1000);
	else
		gain = (val * 1000) + (val2 / 1000);

	if (gain > inf->gain_max || gain < inf->gain_min)
		return -EINVAL;
	if (st->powerdown)
		return -EPERM;

	return st->chip_info->gain_dB_to_code(gain, code);
}

static int hmc425a_gain_dB_to_code(int gain, int *code)
{
	*code = ~((abs(gain) / 500) & 0x3F);
	return 0;
}

static int hmc540s_gain_dB_to_code(int gain, int *code)
{
	*code = ~((abs(gain) / 1000) & 0xF);
	return 0;
}

static int adrf5740_gain_dB_to_code(int gain, int *code)
{
	int temp = (abs(gain) / 2000) & 0xF;

	/* Bit [0-3]: 2dB 4dB 8dB 8dB */
	*code = temp & BIT(3) ? temp | BIT(2) : temp;
	return 0;
}

static int ltc6373_gain_dB_to_code(int gain, int *code)
{
	*code = ~(DIV_ROUND_CLOSEST(gain, LTC6373_CONVERSION_CONSTANT) + 3)
		& LTC6373_CONVERSION_MASK;
	return 0;
}

static int code_to_gain_dB(struct hmc425a_state *st, int *val, int *val2)
{
	if (st->powerdown)
		return -EPERM;
	return st->chip_info->code_to_gain_dB(st->gain, val, val2);
}

static int hmc425a_code_to_gain_dB(int code, int *val, int *val2)
{
	*val = (~code * -500) / 1000;
	*val2 = ((~code * -500) % 1000) * 1000;
	return 0;
}

static int hmc540s_code_to_gain_dB(int code, int *val, int *val2)
{
	*val = (~code * -1000) / 1000;
	*val2 = ((~code * -1000) % 1000) * 1000;
	return 0;
}

Annotation

Implementation Notes