drivers/iio/adc/ad7191.c

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

File Facts

System
Linux kernel
Corpus path
drivers/iio/adc/ad7191.c
Extension
.c
Size
13764 bytes
Lines
555
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 ad7191_state {
	struct ad_sigma_delta		sd;
	struct mutex			lock; /* Protect device state */

	struct gpio_descs		*odr_gpios;
	struct gpio_descs		*pga_gpios;
	struct gpio_desc		*temp_gpio;
	struct gpio_desc		*chan_gpio;

	u16				int_vref_mv;
	const u32			(*scale_avail)[2];
	size_t				scale_avail_size;
	u32				scale_index;
	const u32			*samp_freq_avail;
	size_t				samp_freq_avail_size;
	u32				samp_freq_index;

	struct clk			*mclk;
};

static int ad7191_set_channel(struct ad_sigma_delta *sd, unsigned int address)
{
	struct ad7191_state *st = ad_sigma_delta_to_ad7191(sd);
	u8 temp_gpio_val, chan_gpio_val;

	if (!FIELD_FIT(AD7191_CHAN_MASK | AD7191_TEMP_MASK, address))
		return -EINVAL;

	chan_gpio_val = FIELD_GET(AD7191_CHAN_MASK, address);
	temp_gpio_val = FIELD_GET(AD7191_TEMP_MASK, address);

	gpiod_set_value(st->chan_gpio, chan_gpio_val);
	gpiod_set_value(st->temp_gpio, temp_gpio_val);

	return 0;
}

static int ad7191_set_cs(struct ad_sigma_delta *sigma_delta, int assert)
{
	struct spi_transfer t = {
		.len = 0,
		.cs_change = assert,
	};
	struct spi_message m;

	spi_message_init_with_transfers(&m, &t, 1);

	return spi_sync_locked(sigma_delta->spi, &m);
}

static int ad7191_set_mode(struct ad_sigma_delta *sd,
			   enum ad_sigma_delta_mode mode)
{
	struct ad7191_state *st = ad_sigma_delta_to_ad7191(sd);

	switch (mode) {
	case AD_SD_MODE_CONTINUOUS:
	case AD_SD_MODE_SINGLE:
		return ad7191_set_cs(&st->sd, 1);
	case AD_SD_MODE_IDLE:
		return ad7191_set_cs(&st->sd, 0);
	default:
		return -EINVAL;
	}
}

static const struct ad_sigma_delta_info ad7191_sigma_delta_info = {
	.set_channel = ad7191_set_channel,
	.set_mode = ad7191_set_mode,
	.has_registers = false,
};

static int ad7191_init_regulators(struct iio_dev *indio_dev)
{
	struct ad7191_state *st = iio_priv(indio_dev);
	struct device *dev = &st->sd.spi->dev;
	int ret;

	ret = devm_regulator_get_enable(dev, "avdd");
	if (ret)
		return dev_err_probe(dev, ret, "Failed to enable specified AVdd supply\n");

	ret = devm_regulator_get_enable(dev, "dvdd");
	if (ret)
		return dev_err_probe(dev, ret, "Failed to enable specified DVdd supply\n");

	ret = devm_regulator_get_enable_read_voltage(dev, "vref");
	if (ret < 0)
		return dev_err_probe(dev, ret, "Failed to get Vref voltage\n");

Annotation

Implementation Notes