drivers/iio/magnetometer/ak8974.c
Source file repositories/reference/linux-study-clean/drivers/iio/magnetometer/ak8974.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/magnetometer/ak8974.c- Extension
.c- Size
- 27022 bytes
- Lines
- 1051
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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/module.hlinux/mod_devicetable.hlinux/kernel.hlinux/i2c.hlinux/interrupt.hlinux/irq.hlinux/completion.hlinux/err.hlinux/mutex.hlinux/delay.hlinux/bitops.hlinux/random.hlinux/regmap.hlinux/regulator/consumer.hlinux/pm_runtime.hlinux/iio/iio.hlinux/iio/sysfs.hlinux/iio/buffer.hlinux/iio/trigger.hlinux/iio/trigger_consumer.hlinux/iio/triggered_buffer.h
Detected Declarations
struct ak8974function ak8974_get_u16_valfunction ak8974_set_u16_valfunction ak8974_set_powerfunction ak8974_resetfunction ak8974_configurefunction ak8974_trigmeasfunction ak8974_await_drdyfunction ak8974_getresultfunction ak8974_drdy_irqfunction ak8974_drdy_irq_threadfunction ak8974_selftestfunction ak8974_read_calib_datafunction ak8974_detectfunction ak8974_measure_channelfunction ak8974_read_rawfunction ak8974_fill_bufferfunction ak8974_handle_triggerfunction ak8974_get_mount_matrixfunction ak8974_writeable_regfunction ak8974_precious_regfunction ak8974_probefunction ak8974_removefunction ak8974_runtime_suspendfunction ak8974_runtime_resume
Annotated Snippet
struct ak8974 {
struct i2c_client *i2c;
struct iio_mount_matrix orientation;
struct regmap *map;
struct regulator_bulk_data regs[2];
const char *name;
u8 variant;
struct mutex lock;
bool drdy_irq;
struct completion drdy_complete;
bool drdy_active_low;
/* Ensure timestamp is naturally aligned */
struct {
__le16 channels[3];
aligned_s64 ts;
} scan;
};
static const char ak8974_reg_avdd[] = "avdd";
static const char ak8974_reg_dvdd[] = "dvdd";
static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val)
{
int ret;
__le16 bulk;
ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2);
if (ret)
return ret;
*val = le16_to_cpu(bulk);
return 0;
}
static int ak8974_set_u16_val(struct ak8974 *ak8974, u8 reg, u16 val)
{
__le16 bulk = cpu_to_le16(val);
return regmap_bulk_write(ak8974->map, reg, &bulk, 2);
}
static int ak8974_set_power(struct ak8974 *ak8974, bool mode)
{
int ret;
u8 val;
val = mode ? AK8974_CTRL1_POWER : 0;
val |= AK8974_CTRL1_FORCE_EN;
ret = regmap_write(ak8974->map, AK8974_CTRL1, val);
if (ret < 0)
return ret;
if (mode)
msleep(AK8974_ACTIVATE_DELAY);
return 0;
}
static int ak8974_reset(struct ak8974 *ak8974)
{
int ret;
/* Power on to get register access. Sets CTRL1 reg to reset state */
ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
if (ret)
return ret;
ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF);
if (ret)
return ret;
ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF);
if (ret)
return ret;
if (ak8974->variant != AK8974_WHOAMI_VALUE_HSCDTD008A) {
ret = regmap_write(ak8974->map, AK8974_INT_CTRL,
AK8974_INT_CTRL_RESDEF);
if (ret)
return ret;
} else {
ret = regmap_write(ak8974->map, HSCDTD008A_CTRL4,
HSCDTD008A_CTRL4_RESDEF);
if (ret)
return ret;
}
/* After reset, power off is default state */
return ak8974_set_power(ak8974, AK8974_PWR_OFF);
}
static int ak8974_configure(struct ak8974 *ak8974)
{
Annotation
- Immediate include surface: `linux/module.h`, `linux/mod_devicetable.h`, `linux/kernel.h`, `linux/i2c.h`, `linux/interrupt.h`, `linux/irq.h`, `linux/completion.h`, `linux/err.h`.
- Detected declarations: `struct ak8974`, `function ak8974_get_u16_val`, `function ak8974_set_u16_val`, `function ak8974_set_power`, `function ak8974_reset`, `function ak8974_configure`, `function ak8974_trigmeas`, `function ak8974_await_drdy`, `function ak8974_getresult`, `function ak8974_drdy_irq`.
- Atlas domain: Driver Families / drivers/iio.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- 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.