drivers/iio/potentiometer/mcp41010.c

Source file repositories/reference/linux-study-clean/drivers/iio/potentiometer/mcp41010.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/potentiometer/mcp41010.c
Extension
.c
Size
5204 bytes
Lines
203
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 mcp41010_cfg {
	char name[16];
	int wipers;
	int kohms;
};

enum mcp41010_type {
	MCP41010,
	MCP41050,
	MCP41100,
	MCP42010,
	MCP42050,
	MCP42100,
};

static const struct mcp41010_cfg mcp41010_cfg[] = {
	[MCP41010] = { .name = "mcp41010", .wipers = 1, .kohms =  10, },
	[MCP41050] = { .name = "mcp41050", .wipers = 1, .kohms =  50, },
	[MCP41100] = { .name = "mcp41100", .wipers = 1, .kohms = 100, },
	[MCP42010] = { .name = "mcp42010", .wipers = 2, .kohms =  10, },
	[MCP42050] = { .name = "mcp42050", .wipers = 2, .kohms =  50, },
	[MCP42100] = { .name = "mcp42100", .wipers = 2, .kohms = 100, },
};

struct mcp41010_data {
	struct spi_device *spi;
	const struct mcp41010_cfg *cfg;
	struct mutex lock; /* Protect write sequences */
	unsigned int value[MCP41010_MAX_WIPERS]; /* Cache wiper values */
	u8 buf[2] __aligned(IIO_DMA_MINALIGN);
};

#define MCP41010_CHANNEL(ch) {					\
	.type = IIO_RESISTANCE,					\
	.indexed = 1,						\
	.output = 1,						\
	.channel = (ch),					\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
}

static const struct iio_chan_spec mcp41010_channels[] = {
	MCP41010_CHANNEL(0),
	MCP41010_CHANNEL(1),
};

static int mcp41010_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long mask)
{
	struct mcp41010_data *data = iio_priv(indio_dev);
	int channel = chan->channel;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		*val = data->value[channel];
		return IIO_VAL_INT;

	case IIO_CHAN_INFO_SCALE:
		*val = 1000 * data->cfg->kohms;
		*val2 = MCP41010_WIPER_MAX;
		return IIO_VAL_FRACTIONAL;
	}

	return -EINVAL;
}

static int mcp41010_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long mask)
{
	int err;
	struct mcp41010_data *data = iio_priv(indio_dev);
	int channel = chan->channel;

	if (mask != IIO_CHAN_INFO_RAW)
		return -EINVAL;

	if (val > MCP41010_WIPER_MAX || val < 0)
		return -EINVAL;

	mutex_lock(&data->lock);

	data->buf[0] = MCP41010_WIPER_CHANNEL << channel;
	data->buf[0] |= MCP41010_WRITE;
	data->buf[1] = val & 0xff;

	err = spi_write(data->spi, data->buf, sizeof(data->buf));
	if (!err)
		data->value[channel] = val;

Annotation

Implementation Notes