drivers/iio/dac/max5522.c

Source file repositories/reference/linux-study-clean/drivers/iio/dac/max5522.c

File Facts

System
Linux kernel
Corpus path
drivers/iio/dac/max5522.c
Extension
.c
Size
4494 bytes
Lines
195
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 max5522_chip_info {
	const char *name;
	const struct iio_chan_spec *channels;
	unsigned int num_channels;
};

struct max5522_state {
	struct regmap *regmap;
	const struct max5522_chip_info *chip_info;
	unsigned short dac_cache[2];
	int vref_mV;
};

#define MAX5522_CHANNEL(chan) {	\
	.type = IIO_VOLTAGE, \
	.indexed = 1, \
	.output = 1, \
	.channel = chan, \
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
			      BIT(IIO_CHAN_INFO_SCALE), \
	.scan_type = { \
		.sign = 'u', \
		.realbits = 10, \
		.storagebits = 16, \
		.shift = 2, \
	} \
}

static const struct iio_chan_spec max5522_channels[] = {
	MAX5522_CHANNEL(0),
	MAX5522_CHANNEL(1),
};

enum max5522_type {
	ID_MAX5522,
};

static const struct max5522_chip_info max5522_chip_info_tbl[] = {
	[ID_MAX5522] = {
		.name = "max5522",
		.channels = max5522_channels,
		.num_channels = 2,
	},
};

static inline int max5522_info_to_reg(struct iio_chan_spec const *chan)
{
	return MAX5522_REG_DATA(chan->channel);
}

static int max5522_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long info)
{
	struct max5522_state *state = iio_priv(indio_dev);

	switch (info) {
	case IIO_CHAN_INFO_RAW:
		*val = state->dac_cache[chan->channel];
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_SCALE:
		*val = state->vref_mV;
		*val2 = 10;
		return IIO_VAL_FRACTIONAL_LOG2;
	default:
		return -EINVAL;
	}

	return -EINVAL;
}

static int max5522_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long info)
{
	struct max5522_state *state = iio_priv(indio_dev);
	int rval;

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

	rval = regmap_write(state->regmap, max5522_info_to_reg(chan),
			    val << chan->scan_type.shift);
	if (rval < 0)
		return rval;

	state->dac_cache[chan->channel] = val;

	return 0;
}

Annotation

Implementation Notes