drivers/platform/x86/gpd-pocket-fan.c

Source file repositories/reference/linux-study-clean/drivers/platform/x86/gpd-pocket-fan.c

File Facts

System
Linux kernel
Corpus path
drivers/platform/x86/gpd-pocket-fan.c
Extension
.c
Size
5816 bytes
Lines
225
Domain
Driver Families
Bucket
drivers/platform
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct gpd_pocket_fan_data {
	struct device *dev;
	struct thermal_zone_device *dts0;
	struct thermal_zone_device *dts1;
	struct gpio_desc *gpio0;
	struct gpio_desc *gpio1;
	struct delayed_work work;
	int last_speed;
};

static void gpd_pocket_fan_set_speed(struct gpd_pocket_fan_data *fan, int speed)
{
	if (speed == fan->last_speed)
		return;

	gpiod_direction_output(fan->gpio0, !!(speed & 1));
	gpiod_direction_output(fan->gpio1, !!(speed & 2));

	fan->last_speed = speed;
}

static int gpd_pocket_fan_min_speed(void)
{
	if (power_supply_is_system_supplied())
		return speed_on_ac;
	else
		return 0;
}

static void gpd_pocket_fan_worker(struct work_struct *work)
{
	struct gpd_pocket_fan_data *fan =
		container_of(work, struct gpd_pocket_fan_data, work.work);
	int t0, t1, temp, speed, min_speed, i;

	if (thermal_zone_get_temp(fan->dts0, &t0) ||
	    thermal_zone_get_temp(fan->dts1, &t1)) {
		dev_warn(fan->dev, "Error getting temperature\n");
		speed = MAX_SPEED;
		goto set_speed;
	}

	temp = max(t0, t1);

	speed = fan->last_speed;
	min_speed = gpd_pocket_fan_min_speed();

	/* Determine minimum speed */
	for (i = min_speed; i < ARRAY_SIZE(temp_limits); i++) {
		if (temp < temp_limits[i])
			break;
	}
	if (speed < i)
		speed = i;

	/* Use hysteresis before lowering speed again */
	for (i = min_speed; i < ARRAY_SIZE(temp_limits); i++) {
		if (temp <= (temp_limits[i] - hysteresis))
			break;
	}
	if (speed > i)
		speed = i;

	if (fan->last_speed <= 0 && speed)
		speed = MAX_SPEED; /* kick start motor */

set_speed:
	gpd_pocket_fan_set_speed(fan, speed);

	/* When mostly idle (low temp/speed), slow down the poll interval. */
	queue_delayed_work(system_percpu_wq, &fan->work,
			   msecs_to_jiffies(4000 / (speed + 1)));
}

static void gpd_pocket_fan_force_update(struct gpd_pocket_fan_data *fan)
{
	fan->last_speed = -1;
	mod_delayed_work(system_percpu_wq, &fan->work, 0);
}

static int gpd_pocket_fan_probe(struct platform_device *pdev)
{
	struct gpd_pocket_fan_data *fan;
	int i, ret;

	for (i = 0; i < ARRAY_SIZE(temp_limits); i++) {
		if (temp_limits[i] < 20000 || temp_limits[i] > 90000) {
			dev_err(&pdev->dev, "Invalid temp-limit %d (must be between 20000 and 90000)\n",
				temp_limits[i]);
			temp_limits[0] = TEMP_LIMIT0_DEFAULT;

Annotation

Implementation Notes