drivers/hwmon/gpio-fan.c
Source file repositories/reference/linux-study-clean/drivers/hwmon/gpio-fan.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hwmon/gpio-fan.c- Extension
.c- Size
- 16104 bytes
- Lines
- 686
- 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.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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/module.hlinux/init.hlinux/slab.hlinux/interrupt.hlinux/irq.hlinux/platform_device.hlinux/err.hlinux/kstrtox.hlinux/mutex.hlinux/hwmon.hlinux/gpio/consumer.hlinux/of.hlinux/of_platform.hlinux/pm.hlinux/pm_runtime.hlinux/regulator/consumer.hlinux/thermal.h
Detected Declarations
struct gpio_fan_speedstruct gpio_fan_datafunction fan_alarm_notifyfunction fan_alarm_irq_handlerfunction fan1_alarm_showfunction fan_alarm_initfunction __set_fan_ctrlfunction __get_fan_ctrlfunction set_fan_speedfunction get_fan_speed_indexfunction rpm_to_speed_indexfunction pwm1_showfunction pwm1_storefunction pwm1_enable_showfunction pwm1_enable_storefunction pwm1_mode_showfunction fan1_min_showfunction fan1_max_showfunction fan1_input_showfunction set_rpmfunction gpio_fan_is_visiblefunction fan_ctrl_initfunction gpio_fan_get_max_statefunction gpio_fan_get_cur_statefunction gpio_fan_set_cur_statefunction gpio_fan_get_of_datafunction gpio_fan_stopfunction gpio_fan_probefunction gpio_fan_shutdownfunction gpio_fan_runtime_suspendfunction gpio_fan_runtime_resumefunction gpio_fan_suspendfunction gpio_fan_resume
Annotated Snippet
struct gpio_fan_speed {
int rpm;
int ctrl_val;
};
struct gpio_fan_data {
struct device *dev;
struct device *hwmon_dev;
/* Cooling device if any */
struct thermal_cooling_device *cdev;
struct mutex lock; /* lock GPIOs operations. */
int num_gpios;
struct gpio_desc **gpios;
int num_speed;
struct gpio_fan_speed *speed;
int speed_index;
int resume_speed;
bool pwm_enable;
struct gpio_desc *alarm_gpio;
struct work_struct alarm_work;
struct regulator *supply;
};
/*
* Alarm GPIO.
*/
static void fan_alarm_notify(struct work_struct *ws)
{
struct gpio_fan_data *fan_data =
container_of(ws, struct gpio_fan_data, alarm_work);
sysfs_notify(&fan_data->hwmon_dev->kobj, NULL, "fan1_alarm");
kobject_uevent(&fan_data->hwmon_dev->kobj, KOBJ_CHANGE);
}
static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
{
struct gpio_fan_data *fan_data = dev_id;
schedule_work(&fan_data->alarm_work);
return IRQ_NONE;
}
static ssize_t fan1_alarm_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
return sprintf(buf, "%d\n",
gpiod_get_value_cansleep(fan_data->alarm_gpio));
}
static DEVICE_ATTR_RO(fan1_alarm);
static int fan_alarm_init(struct gpio_fan_data *fan_data)
{
int alarm_irq;
struct device *dev = fan_data->dev;
/*
* If the alarm GPIO don't support interrupts, just leave
* without initializing the fail notification support.
*/
alarm_irq = gpiod_to_irq(fan_data->alarm_gpio);
if (alarm_irq <= 0)
return 0;
INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
IRQF_SHARED, "GPIO fan alarm", fan_data);
}
/*
* Control GPIOs.
*/
/* Must be called with fan_data->lock held, except during initialization. */
static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
{
int i;
for (i = 0; i < fan_data->num_gpios; i++)
gpiod_set_value_cansleep(fan_data->gpios[i],
(ctrl_val >> i) & 1);
}
static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
Annotation
- Immediate include surface: `linux/module.h`, `linux/init.h`, `linux/slab.h`, `linux/interrupt.h`, `linux/irq.h`, `linux/platform_device.h`, `linux/err.h`, `linux/kstrtox.h`.
- Detected declarations: `struct gpio_fan_speed`, `struct gpio_fan_data`, `function fan_alarm_notify`, `function fan_alarm_irq_handler`, `function fan1_alarm_show`, `function fan_alarm_init`, `function __set_fan_ctrl`, `function __get_fan_ctrl`, `function set_fan_speed`, `function get_fan_speed_index`.
- 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.
- 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.