drivers/hwmon/mcp3021.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/mcp3021.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/mcp3021.c- Extension
.c- Size
- 4984 bytes
- Lines
- 209
- Domain
- Driver Families
- Bucket
- drivers/hwmon
- 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/kernel.hlinux/module.hlinux/hwmon.hlinux/slab.hlinux/i2c.hlinux/err.hlinux/device.hlinux/of.h
Detected Declarations
struct mcp3021_dataenum chipsfunction volts_from_regfunction mcp3021_readfunction mcp3021_is_visiblefunction mcp3021_probe
Annotated Snippet
struct mcp3021_data {
struct i2c_client *client;
u32 vdd; /* supply and reference voltage in millivolt */
u16 sar_shift;
u16 sar_mask;
u8 output_res;
};
static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
{
return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
}
static int mcp3021_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
struct mcp3021_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
__be16 buf;
u16 reg;
int ret;
if (type != hwmon_in)
return -EOPNOTSUPP;
ret = i2c_master_recv(client, (char *)&buf, 2);
if (ret < 0)
return ret;
if (ret != 2)
return -EIO;
/* The output code of the MCP3021 is transmitted with MSB first. */
reg = be16_to_cpu(buf);
/*
* The ten-bit output code is composed of the lower 4-bit of the
* first byte and the upper 6-bit of the second byte.
*/
reg = (reg >> data->sar_shift) & data->sar_mask;
*val = volts_from_reg(data, reg);
return 0;
}
static umode_t mcp3021_is_visible(const void *_data,
enum hwmon_sensor_types type,
u32 attr, int channel)
{
if (type != hwmon_in)
return 0;
if (attr != hwmon_in_input)
return 0;
return 0444;
}
static const struct hwmon_channel_info * const mcp3021_info[] = {
HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
NULL
};
static const struct hwmon_ops mcp3021_hwmon_ops = {
.is_visible = mcp3021_is_visible,
.read = mcp3021_read,
};
static const struct hwmon_chip_info mcp3021_chip_info = {
.ops = &mcp3021_hwmon_ops,
.info = mcp3021_info,
};
static int mcp3021_probe(struct i2c_client *client)
{
struct mcp3021_data *data = NULL;
struct device_node *np = client->dev.of_node;
struct device *hwmon_dev;
enum chips type;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return -ENODEV;
data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
GFP_KERNEL);
if (!data)
return -ENOMEM;
i2c_set_clientdata(client, data);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/hwmon.h`, `linux/slab.h`, `linux/i2c.h`, `linux/err.h`, `linux/device.h`, `linux/of.h`.
- Detected declarations: `struct mcp3021_data`, `enum chips`, `function volts_from_reg`, `function mcp3021_read`, `function mcp3021_is_visible`, `function mcp3021_probe`.
- Atlas domain: Driver Families / drivers/hwmon.
- 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.