drivers/iio/adc/mcp3911.c
Source file repositories/reference/linux-study-clean/drivers/iio/adc/mcp3911.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/adc/mcp3911.c- Extension
.c- Size
- 25739 bytes
- Lines
- 956
- 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.
- 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.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitfield.hlinux/bitops.hlinux/cleanup.hlinux/clk.hlinux/delay.hlinux/dev_printk.hlinux/err.hlinux/gpio/consumer.hlinux/module.hlinux/mod_devicetable.hlinux/property.hlinux/regulator/consumer.hlinux/spi/spi.hlinux/iio/iio.hlinux/iio/buffer.hlinux/iio/triggered_buffer.hlinux/iio/trigger_consumer.hlinux/iio/trigger.hlinux/unaligned.h
Detected Declarations
struct mcp3911struct mcp3911_chip_infostruct mcp3911enum mcp3911_idfunction mcp3911_readfunction mcp3911_writefunction mcp3911_updatefunction mcp3911_read_s24function mcp3910_enable_offsetfunction mcp3910_get_offsetfunction mcp3910_set_offsetfunction mcp3910_get_rawfunction mcp3911_enable_offsetfunction mcp3911_get_offsetfunction mcp3911_set_offsetfunction mcp3911_get_rawfunction mcp3910_get_osrfunction mcp3910_set_osrfunction mcp3911_set_osrfunction mcp3911_get_osrfunction mcp3910_set_scalefunction mcp3911_set_scalefunction mcp3911_write_raw_get_fmtfunction mcp3911_read_availfunction mcp3911_read_rawfunction mcp3911_write_rawfunction mcp3911_calc_scale_tablefunction mcp3911_trigger_handlerfunction iio_for_each_active_channelfunction mcp3911_configfunction mcp3910_configfunction mcp3911_set_trigger_statefunction mcp3911_probe
Annotated Snippet
struct mcp3911_chip_info {
const struct iio_chan_spec *channels;
unsigned int num_channels;
int (*config)(struct mcp3911 *adc, bool external_vref);
int (*get_osr)(struct mcp3911 *adc, u32 *val);
int (*set_osr)(struct mcp3911 *adc, u32 val);
int (*enable_offset)(struct mcp3911 *adc, bool enable);
int (*get_offset)(struct mcp3911 *adc, int channel, int *val);
int (*set_offset)(struct mcp3911 *adc, int channel, int val);
int (*set_scale)(struct mcp3911 *adc, int channel, u32 val);
int (*get_raw)(struct mcp3911 *adc, int channel, int *val);
};
struct mcp3911 {
struct spi_device *spi;
struct mutex lock;
struct clk *clki;
u32 dev_addr;
struct iio_trigger *trig;
u32 gain[MCP39XX_MAX_NUM_CHANNELS];
const struct mcp3911_chip_info *chip;
struct {
u32 channels[MCP39XX_MAX_NUM_CHANNELS];
aligned_s64 ts;
} scan;
u8 tx_buf __aligned(IIO_DMA_MINALIGN);
u8 rx_buf[MCP39XX_MAX_NUM_CHANNELS * 3];
};
static int mcp3911_read(struct mcp3911 *adc, u8 reg, u32 *val, u8 len)
{
int ret;
reg = MCP3911_REG_READ(reg, adc->dev_addr);
ret = spi_write_then_read(adc->spi, ®, 1, val, len);
if (ret < 0)
return ret;
be32_to_cpus(val);
*val >>= ((4 - len) * 8);
dev_dbg(&adc->spi->dev, "reading 0x%x from register 0x%lx\n", *val,
FIELD_GET(MCP3911_REG_MASK, reg));
return ret;
}
static int mcp3911_write(struct mcp3911 *adc, u8 reg, u32 val, u8 len)
{
dev_dbg(&adc->spi->dev, "writing 0x%x to register 0x%x\n", val, reg);
val <<= (3 - len) * 8;
cpu_to_be32s(&val);
val |= MCP3911_REG_WRITE(reg, adc->dev_addr);
return spi_write(adc->spi, &val, len + 1);
}
static int mcp3911_update(struct mcp3911 *adc, u8 reg, u32 mask, u32 val, u8 len)
{
u32 tmp;
int ret;
ret = mcp3911_read(adc, reg, &tmp, len);
if (ret)
return ret;
val &= mask;
val |= tmp & ~mask;
return mcp3911_write(adc, reg, val, len);
}
static int mcp3911_read_s24(struct mcp3911 *const adc, u8 const reg, s32 *const val)
{
u32 uval;
int const ret = mcp3911_read(adc, reg, &uval, 3);
if (ret)
return ret;
*val = sign_extend32(uval, 23);
return ret;
}
static int mcp3910_enable_offset(struct mcp3911 *adc, bool enable)
{
unsigned int mask = MCP3910_CONFIG0_EN_OFFCAL;
unsigned int value = enable ? mask : 0;
return mcp3911_update(adc, MCP3910_REG_CONFIG0, mask, value, 3);
Annotation
- Immediate include surface: `linux/bitfield.h`, `linux/bitops.h`, `linux/cleanup.h`, `linux/clk.h`, `linux/delay.h`, `linux/dev_printk.h`, `linux/err.h`, `linux/gpio/consumer.h`.
- Detected declarations: `struct mcp3911`, `struct mcp3911_chip_info`, `struct mcp3911`, `enum mcp3911_id`, `function mcp3911_read`, `function mcp3911_write`, `function mcp3911_update`, `function mcp3911_read_s24`, `function mcp3910_enable_offset`, `function mcp3910_get_offset`.
- Atlas domain: Driver Families / drivers/iio.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.