drivers/iio/adc/mcp3564.c

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

File Facts

System
Linux kernel
Corpus path
drivers/iio/adc/mcp3564.c
Extension
.c
Size
42036 bytes
Lines
1484
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 mcp3564_chip_info {
	const char	*name;
	unsigned int	num_channels;
	unsigned int	resolution;
	bool		have_vref;
};

/**
 * struct mcp3564_state - working data for a ADC device
 * @chip_info:		chip specific data
 * @spi:		SPI device structure
 * @vref_mv:		voltage reference value in millivolts
 * @lock:		synchronize access to driver's state members
 * @dev_addr:		hardware device address
 * @oversampling:	the index inside oversampling list of the ADC
 * @hwgain:		the index inside hardware gain list of the ADC
 * @scale_tbls:		table with precalculated scale
 * @calib_bias:		calibration bias value
 * @calib_scale:	calibration scale value
 * @current_boost_mode:	the index inside current boost list of the ADC
 * @burnout_mode:	the index inside current bias list of the ADC
 * @auto_zeroing_mux:	set if ADC auto-zeroing algorithm is enabled
 * @auto_zeroing_ref:	set if ADC auto-Zeroing Reference Buffer Setting is enabled
 * @have_vref:		does the ADC have an internal voltage reference?
 * @labels:		table with channels labels
 */
struct mcp3564_state {
	const struct mcp3564_chip_info	*chip_info;
	struct spi_device		*spi;
	unsigned short			vref_mv;
	struct mutex			lock; /* Synchronize access to driver's state members */
	u8				dev_addr;
	enum mcp3564_oversampling	oversampling;
	unsigned int			hwgain;
	unsigned int			scale_tbls[MCP3564_MAX_PGA][2];
	int				calib_bias;
	int				calib_scale;
	unsigned int			current_boost_mode;
	enum mcp3564_burnout		burnout_mode;
	bool				auto_zeroing_mux;
	bool				auto_zeroing_ref;
	bool				have_vref;
	const char			*labels[MCP3564_MAX_CHANNELS];
};

static inline u8 mcp3564_cmd_write(u8 chip_addr, u8 reg)
{
	return FIELD_PREP(MCP3564_CMD_HW_ADDR_MASK, chip_addr) |
	       FIELD_PREP(MCP3564_CMD_ADDR_MASK, reg) |
	       BIT(1);
}

static inline u8 mcp3564_cmd_read(u8 chip_addr, u8 reg)
{
	return FIELD_PREP(MCP3564_CMD_HW_ADDR_MASK, chip_addr) |
	       FIELD_PREP(MCP3564_CMD_ADDR_MASK, reg) |
	       BIT(0);
}

static int mcp3564_read_8bits(struct mcp3564_state *adc, u8 reg, u8 *val)
{
	int ret;
	u8 tx_buf;
	u8 rx_buf;

	tx_buf = mcp3564_cmd_read(adc->dev_addr, reg);

	ret = spi_write_then_read(adc->spi, &tx_buf, sizeof(tx_buf),
				  &rx_buf, sizeof(rx_buf));
	*val = rx_buf;

	return ret;
}

static int mcp3564_read_16bits(struct mcp3564_state *adc, u8 reg, u16 *val)
{
	int ret;
	u8 tx_buf;
	__be16 rx_buf;

	tx_buf = mcp3564_cmd_read(adc->dev_addr, reg);

	ret = spi_write_then_read(adc->spi, &tx_buf, sizeof(tx_buf),
				  &rx_buf, sizeof(rx_buf));
	*val = be16_to_cpu(rx_buf);

	return ret;
}

static int mcp3564_read_32bits(struct mcp3564_state *adc, u8 reg, u32 *val)

Annotation

Implementation Notes