drivers/iio/adc/ti-adc128s052.c

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

File Facts

System
Linux kernel
Corpus path
drivers/iio/adc/ti-adc128s052.c
Extension
.c
Size
7826 bytes
Lines
287
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 adc128_configuration {
	const struct iio_chan_spec	*channels;
	u8				num_channels;
	const char			*refname;
	int				num_other_regulators;
	const char * const		(*other_regulators)[];
};

struct adc128 {
	struct spi_device *spi;

	/*
	 * Serialize the SPI 'write-channel + read data' accesses and protect
	 * the shared buffer.
	 */
	struct mutex lock;
	int vref_mv;
	union {
		__be16 buffer16;
		u8 buffer[2];
	} __aligned(IIO_DMA_MINALIGN);
};

static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
{
	int ret;

	guard(mutex)(&adc->lock);

	adc->buffer[0] = channel << 3;
	adc->buffer[1] = 0;

	ret = spi_write(adc->spi, &adc->buffer, sizeof(adc->buffer));
	if (ret < 0)
		return ret;

	ret = spi_read(adc->spi, &adc->buffer16, sizeof(adc->buffer16));
	if (ret < 0)
		return ret;

	return be16_to_cpu(adc->buffer16) & 0xFFF;
}

static int adc128_read_raw(struct iio_dev *indio_dev,
			   struct iio_chan_spec const *channel, int *val,
			   int *val2, long mask)
{
	struct adc128 *adc = iio_priv(indio_dev);
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:

		ret = adc128_adc_conversion(adc, channel->channel);
		if (ret < 0)
			return ret;

		*val = ret;
		return IIO_VAL_INT;

	case IIO_CHAN_INFO_SCALE:

		*val = adc->vref_mv;
		*val2 = 12;
		return IIO_VAL_FRACTIONAL_LOG2;

	default:
		return -EINVAL;
	}

}

#define ADC128_VOLTAGE_CHANNEL(num)	\
	{ \
		.type = IIO_VOLTAGE, \
		.indexed = 1, \
		.channel = (num), \
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
	}

static const struct iio_chan_spec simple_1chan_adc_channels[] = {
	ADC128_VOLTAGE_CHANNEL(0),
};

static const struct iio_chan_spec simple_2chan_adc_channels[] = {
	ADC128_VOLTAGE_CHANNEL(0),
	ADC128_VOLTAGE_CHANNEL(1),
};

Annotation

Implementation Notes