drivers/iio/accel/mc3230.c

Source file repositories/reference/linux-study-clean/drivers/iio/accel/mc3230.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/accel/mc3230.c
Extension
.c
Size
6302 bytes
Lines
262
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 mc3230_chip_info {
	const char *name;
	const u8 chip_id;
	const u8 product_code;
	const int scale;
};

static const struct mc3230_chip_info mc3230_chip_info = {
	.name = "mc3230",
	.chip_id = 0x01,
	.product_code = 0x19,
	/* (1.5 + 1.5) * 9.81 / (2^8 - 1) = 0.115411765 */
	.scale = 115411765,
};

static const struct mc3230_chip_info mc3510c_chip_info = {
	.name = "mc3510c",
	.chip_id = 0x23,
	.product_code = 0x10,
	/* Was obtained empirically */
	.scale = 625000000,
};

#define MC3230_CHANNEL(reg, axis) {	\
	.type = IIO_ACCEL,	\
	.address = reg,	\
	.modified = 1,	\
	.channel2 = IIO_MOD_##axis,	\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
	.ext_info = mc3230_ext_info, \
}

struct mc3230_data {
	const struct mc3230_chip_info *chip_info;
	struct i2c_client *client;
	struct iio_mount_matrix orientation;
};

static const struct iio_mount_matrix *
mc3230_get_mount_matrix(const struct iio_dev *indio_dev,
			const struct iio_chan_spec *chan)
{
	struct mc3230_data *data = iio_priv(indio_dev);

	return &data->orientation;
}

static const struct iio_chan_spec_ext_info mc3230_ext_info[] = {
	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, mc3230_get_mount_matrix),
	{ }
};

static const struct iio_chan_spec mc3230_channels[] = {
	MC3230_CHANNEL(MC3230_REG_XOUT, X),
	MC3230_CHANNEL(MC3230_REG_YOUT, Y),
	MC3230_CHANNEL(MC3230_REG_ZOUT, Z),
};

static int mc3230_set_opcon(struct mc3230_data *data, int opcon)
{
	int ret;
	struct i2c_client *client = data->client;

	ret = i2c_smbus_read_byte_data(client, MC3230_REG_MODE);
	if (ret < 0) {
		dev_err(&client->dev, "failed to read mode reg: %d\n", ret);
		return ret;
	}

	ret &= ~MC3230_MODE_OPCON_MASK;
	ret |= opcon;

	ret = i2c_smbus_write_byte_data(client, MC3230_REG_MODE, ret);
	if (ret < 0) {
		dev_err(&client->dev, "failed to write mode reg: %d\n", ret);
		return ret;
	}

	return 0;
}

static int mc3230_read_raw(struct iio_dev *indio_dev,
				struct iio_chan_spec const *chan,
				int *val, int *val2, long mask)
{
	struct mc3230_data *data = iio_priv(indio_dev);
	int ret;

	switch (mask) {

Annotation

Implementation Notes