drivers/iio/imu/smi330/smi330_i2c.c
Source file repositories/reference/linux-study-clean/drivers/iio/imu/smi330/smi330_i2c.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/imu/smi330/smi330_i2c.c- Extension
.c- Size
- 3654 bytes
- Lines
- 134
- 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.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/i2c.hlinux/mod_devicetable.hlinux/module.hlinux/regmap.hsmi330.h
Detected Declarations
struct smi330_i2c_privfunction smi330_regmap_i2c_readfunction smi330_regmap_i2c_writefunction smi330_i2c_probe
Annotated Snippet
struct smi330_i2c_priv {
struct i2c_client *i2c;
u8 rx_buffer[SMI330_I2C_MAX_RX_BUFFER_SIZE];
};
static int smi330_regmap_i2c_read(void *context, const void *reg_buf,
size_t reg_size, void *val_buf,
size_t val_size)
{
struct smi330_i2c_priv *priv = context;
int ret;
if (SMI330_NUM_DUMMY_BYTES + val_size > SMI330_I2C_MAX_RX_BUFFER_SIZE)
return -EINVAL;
/*
* SMI330 I2C read frame:
* <Slave address[6:0], RnW> <x, Register address[6:0]>
* <Slave address[6:0], RnW> <Dummy[7:0]> <Dummy[7:0]> <Data_0[7:0]> <Data_1[15:8]>...
* <Data_N[7:0]> <Data_N[15:8]>
* Remark: Slave address is not considered part of the frame in the following definitions
*/
struct i2c_msg msgs[] = {
{
.addr = priv->i2c->addr,
.flags = priv->i2c->flags,
.len = reg_size,
.buf = (u8 *)reg_buf,
},
{
.addr = priv->i2c->addr,
.flags = priv->i2c->flags | I2C_M_RD,
.len = SMI330_NUM_DUMMY_BYTES + val_size,
.buf = priv->rx_buffer,
},
};
ret = i2c_transfer(priv->i2c->adapter, msgs, ARRAY_SIZE(msgs));
if (ret < 0)
return ret;
memcpy(val_buf, priv->rx_buffer + SMI330_NUM_DUMMY_BYTES, val_size);
return 0;
}
static int smi330_regmap_i2c_write(void *context, const void *data,
size_t count)
{
struct smi330_i2c_priv *priv = context;
u8 reg;
/*
* SMI330 I2C write frame:
* <Slave address[6:0], RnW> <x, Register address[6:0]> <Data_0[7:0]> <Data_1[15:8]>...
* <Data_N[7:0]> <Data_N[15:8]>
* Remark: Slave address is not considered part of the frame in the following definitions
*/
reg = *(u8 *)data;
return i2c_smbus_write_i2c_block_data(priv->i2c, reg,
count - sizeof(u8),
data + sizeof(u8));
}
static const struct regmap_bus smi330_regmap_bus = {
.read = smi330_regmap_i2c_read,
.write = smi330_regmap_i2c_write,
};
static int smi330_i2c_probe(struct i2c_client *i2c)
{
struct device *dev = &i2c->dev;
struct smi330_i2c_priv *priv;
struct regmap *regmap;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->i2c = i2c;
regmap = devm_regmap_init(dev, &smi330_regmap_bus, priv,
&smi330_regmap_config);
if (IS_ERR(regmap))
return dev_err_probe(dev, PTR_ERR(regmap),
"Failed to initialize I2C Regmap\n");
return smi330_core_probe(dev, regmap);
}
static const struct i2c_device_id smi330_i2c_device_id[] = {
Annotation
- Immediate include surface: `linux/i2c.h`, `linux/mod_devicetable.h`, `linux/module.h`, `linux/regmap.h`, `smi330.h`.
- Detected declarations: `struct smi330_i2c_priv`, `function smi330_regmap_i2c_read`, `function smi330_regmap_i2c_write`, `function smi330_i2c_probe`.
- Atlas domain: Driver Families / drivers/iio.
- Implementation status: source implementation candidate.
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.