drivers/hwmon/nsa320-hwmon.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/nsa320-hwmon.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/nsa320-hwmon.c- Extension
.c- Size
- 5478 bytes
- Lines
- 206
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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/bitops.hlinux/delay.hlinux/err.hlinux/gpio/consumer.hlinux/hwmon.hlinux/hwmon-sysfs.hlinux/jiffies.hlinux/module.hlinux/mutex.hlinux/of.hlinux/platform_device.h
Detected Declarations
struct nsa320_hwmonenum nsa320_inputsfunction activefunction label_showfunction temp1_input_showfunction fan1_input_showfunction nsa320_hwmon_probe
Annotated Snippet
struct nsa320_hwmon {
struct mutex update_lock; /* lock GPIO operations */
unsigned long last_updated; /* jiffies */
unsigned long mcu_data;
struct gpio_desc *act;
struct gpio_desc *clk;
struct gpio_desc *data;
};
enum nsa320_inputs {
NSA320_TEMP = 0,
NSA320_FAN = 1,
};
static const char * const nsa320_input_names[] = {
[NSA320_TEMP] = "System Temperature",
[NSA320_FAN] = "Chassis Fan",
};
/*
* Although this protocol looks similar to SPI the long delay
* between the active (aka chip select) signal and the shorter
* delay between clock pulses are needed for reliable operation.
* The delays provided are taken from the manufacturer kernel,
* testing suggest they probably incorporate a reasonable safety
* margin. (The single device tested became unreliable if the
* delay was reduced to 1/10th of this value.)
*/
static s32 nsa320_hwmon_update(struct device *dev)
{
u32 mcu_data;
u32 mask;
struct nsa320_hwmon *hwmon = dev_get_drvdata(dev);
mutex_lock(&hwmon->update_lock);
mcu_data = hwmon->mcu_data;
if (time_after(jiffies, hwmon->last_updated + HZ) || mcu_data == 0) {
gpiod_set_value(hwmon->act, 1);
msleep(100);
mcu_data = 0;
for (mask = BIT(31); mask; mask >>= 1) {
gpiod_set_value(hwmon->clk, 0);
usleep_range(100, 200);
gpiod_set_value(hwmon->clk, 1);
usleep_range(100, 200);
if (gpiod_get_value(hwmon->data))
mcu_data |= mask;
}
gpiod_set_value(hwmon->act, 0);
dev_dbg(dev, "Read raw MCU data %08x\n", mcu_data);
if ((mcu_data >> 24) != MAGIC_NUMBER) {
dev_dbg(dev, "Read invalid MCU data %08x\n", mcu_data);
mcu_data = -EIO;
} else {
hwmon->mcu_data = mcu_data;
hwmon->last_updated = jiffies;
}
}
mutex_unlock(&hwmon->update_lock);
return mcu_data;
}
static ssize_t label_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
int channel = to_sensor_dev_attr(attr)->index;
return sprintf(buf, "%s\n", nsa320_input_names[channel]);
}
static ssize_t temp1_input_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
s32 mcu_data = nsa320_hwmon_update(dev);
if (mcu_data < 0)
return mcu_data;
return sprintf(buf, "%d\n", (mcu_data & 0xffff) * 100);
}
static ssize_t fan1_input_show(struct device *dev,
struct device_attribute *attr, char *buf)
Annotation
- Immediate include surface: `linux/bitops.h`, `linux/delay.h`, `linux/err.h`, `linux/gpio/consumer.h`, `linux/hwmon.h`, `linux/hwmon-sysfs.h`, `linux/jiffies.h`, `linux/module.h`.
- Detected declarations: `struct nsa320_hwmon`, `enum nsa320_inputs`, `function active`, `function label_show`, `function temp1_input_show`, `function fan1_input_show`, `function nsa320_hwmon_probe`.
- Atlas domain: Driver Families / drivers/hwmon.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.