drivers/iio/adc/ltc2497.c

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

File Facts

System
Linux kernel
Corpus path
drivers/iio/adc/ltc2497.c
Extension
.c
Size
4627 bytes
Lines
172
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 ltc2497_driverdata {
	/* this must be the first member */
	struct ltc2497core_driverdata common_ddata;
	struct i2c_client *client;
	u32 recv_size;
	/*
	 * DMA (thus cache coherency maintenance) may require the
	 * transfer buffers to live in their own cache lines.
	 */
	union {
		__be32 d32;
		u8 d8[3];
	} data __aligned(IIO_DMA_MINALIGN);
};

static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
				      u8 address, int *val)
{
	struct ltc2497_driverdata *st =
		container_of(ddata, struct ltc2497_driverdata, common_ddata);
	int ret;

	if (val) {
		if (st->recv_size == 3)
			ret = i2c_master_recv(st->client, (char *)&st->data.d8,
					      st->recv_size);
		else
			ret = i2c_master_recv(st->client, (char *)&st->data.d32,
					      st->recv_size);
		if (ret < 0) {
			dev_err(&st->client->dev, "i2c_master_recv failed\n");
			return ret;
		}

		/*
		 * The data format is 16/24 bit 2s complement, but with an upper sign bit on the
		 * resolution + 1 position, which is set for positive values only. Given this
		 * bit's value, subtracting BIT(resolution + 1) from the ADC's result is
		 * equivalent to a sign extension.
		 */
		if (st->recv_size == 3) {
			*val = (get_unaligned_be24(st->data.d8) >> 6)
				- BIT(ddata->chip_info->resolution + 1);
		} else {
			*val = (be32_to_cpu(st->data.d32) >> 6)
				- BIT(ddata->chip_info->resolution + 1);
		}

		/*
		 * The part started a new conversion at the end of the above i2c
		 * transfer, so if the address didn't change since the last call
		 * everything is fine and we can return early.
		 * If not (which should only happen when some sort of bulk
		 * conversion is implemented) we have to program the new
		 * address. Note that this probably fails as the conversion that
		 * was triggered above is like not complete yet and the two
		 * operations have to be done in a single transfer.
		 */
		if (ddata->addr_prev == address)
			return 0;
	}

	ret = i2c_smbus_write_byte(st->client,
				   LTC2497_ENABLE | address);
	if (ret)
		dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
			ERR_PTR(ret));
	return ret;
}

static int ltc2497_probe(struct i2c_client *client)
{
	const struct ltc2497_chip_info *chip_info;
	struct iio_dev *indio_dev;
	struct ltc2497_driverdata *st;
	struct device *dev = &client->dev;
	u32 resolution;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
				     I2C_FUNC_SMBUS_WRITE_BYTE))
		return -EOPNOTSUPP;

	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
	if (!indio_dev)
		return -ENOMEM;

	st = iio_priv(indio_dev);
	i2c_set_clientdata(client, indio_dev);
	st->client = client;
	st->common_ddata.result_and_measure = ltc2497_result_and_measure;

Annotation

Implementation Notes