1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * GPD Pocket fan controller driver
4  *
5  * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/thermal.h>
15 #include <linux/workqueue.h>
16 
17 #define MAX_SPEED 3
18 
19 #define TEMP_LIMIT0_DEFAULT	55000
20 #define TEMP_LIMIT1_DEFAULT	60000
21 #define TEMP_LIMIT2_DEFAULT	65000
22 
23 #define HYSTERESIS_DEFAULT	3000
24 
25 #define SPEED_ON_AC_DEFAULT	2
26 
27 static int temp_limits[3] = {
28 	TEMP_LIMIT0_DEFAULT, TEMP_LIMIT1_DEFAULT, TEMP_LIMIT2_DEFAULT,
29 };
30 module_param_array(temp_limits, int, NULL, 0444);
31 MODULE_PARM_DESC(temp_limits,
32 		 "Millicelsius values above which the fan speed increases");
33 
34 static int hysteresis = HYSTERESIS_DEFAULT;
35 module_param(hysteresis, int, 0444);
36 MODULE_PARM_DESC(hysteresis,
37 		 "Hysteresis in millicelsius before lowering the fan speed");
38 
39 static int speed_on_ac = SPEED_ON_AC_DEFAULT;
40 module_param(speed_on_ac, int, 0444);
41 MODULE_PARM_DESC(speed_on_ac,
42 		 "minimum fan speed to allow when system is powered by AC");
43 
44 struct gpd_pocket_fan_data {
45 	struct device *dev;
46 	struct thermal_zone_device *dts0;
47 	struct thermal_zone_device *dts1;
48 	struct gpio_desc *gpio0;
49 	struct gpio_desc *gpio1;
50 	struct delayed_work work;
51 	int last_speed;
52 };
53 
gpd_pocket_fan_set_speed(struct gpd_pocket_fan_data * fan,int speed)54 static void gpd_pocket_fan_set_speed(struct gpd_pocket_fan_data *fan, int speed)
55 {
56 	if (speed == fan->last_speed)
57 		return;
58 
59 	gpiod_direction_output(fan->gpio0, !!(speed & 1));
60 	gpiod_direction_output(fan->gpio1, !!(speed & 2));
61 
62 	fan->last_speed = speed;
63 }
64 
gpd_pocket_fan_min_speed(void)65 static int gpd_pocket_fan_min_speed(void)
66 {
67 	if (power_supply_is_system_supplied())
68 		return speed_on_ac;
69 	else
70 		return 0;
71 }
72 
gpd_pocket_fan_worker(struct work_struct * work)73 static void gpd_pocket_fan_worker(struct work_struct *work)
74 {
75 	struct gpd_pocket_fan_data *fan =
76 		container_of(work, struct gpd_pocket_fan_data, work.work);
77 	int t0, t1, temp, speed, min_speed, i;
78 
79 	if (thermal_zone_get_temp(fan->dts0, &t0) ||
80 	    thermal_zone_get_temp(fan->dts1, &t1)) {
81 		dev_warn(fan->dev, "Error getting temperature\n");
82 		speed = MAX_SPEED;
83 		goto set_speed;
84 	}
85 
86 	temp = max(t0, t1);
87 
88 	speed = fan->last_speed;
89 	min_speed = gpd_pocket_fan_min_speed();
90 
91 	/* Determine minimum speed */
92 	for (i = min_speed; i < ARRAY_SIZE(temp_limits); i++) {
93 		if (temp < temp_limits[i])
94 			break;
95 	}
96 	if (speed < i)
97 		speed = i;
98 
99 	/* Use hysteresis before lowering speed again */
100 	for (i = min_speed; i < ARRAY_SIZE(temp_limits); i++) {
101 		if (temp <= (temp_limits[i] - hysteresis))
102 			break;
103 	}
104 	if (speed > i)
105 		speed = i;
106 
107 	if (fan->last_speed <= 0 && speed)
108 		speed = MAX_SPEED; /* kick start motor */
109 
110 set_speed:
111 	gpd_pocket_fan_set_speed(fan, speed);
112 
113 	/* When mostly idle (low temp/speed), slow down the poll interval. */
114 	queue_delayed_work(system_wq, &fan->work,
115 			   msecs_to_jiffies(4000 / (speed + 1)));
116 }
117 
gpd_pocket_fan_force_update(struct gpd_pocket_fan_data * fan)118 static void gpd_pocket_fan_force_update(struct gpd_pocket_fan_data *fan)
119 {
120 	fan->last_speed = -1;
121 	mod_delayed_work(system_wq, &fan->work, 0);
122 }
123 
gpd_pocket_fan_probe(struct platform_device * pdev)124 static int gpd_pocket_fan_probe(struct platform_device *pdev)
125 {
126 	struct gpd_pocket_fan_data *fan;
127 	int i;
128 
129 	for (i = 0; i < ARRAY_SIZE(temp_limits); i++) {
130 		if (temp_limits[i] < 20000 || temp_limits[i] > 90000) {
131 			dev_err(&pdev->dev, "Invalid temp-limit %d (must be between 20000 and 90000)\n",
132 				temp_limits[i]);
133 			temp_limits[0] = TEMP_LIMIT0_DEFAULT;
134 			temp_limits[1] = TEMP_LIMIT1_DEFAULT;
135 			temp_limits[2] = TEMP_LIMIT2_DEFAULT;
136 			break;
137 		}
138 	}
139 	if (hysteresis < 1000 || hysteresis > 10000) {
140 		dev_err(&pdev->dev, "Invalid hysteresis %d (must be between 1000 and 10000)\n",
141 			hysteresis);
142 		hysteresis = HYSTERESIS_DEFAULT;
143 	}
144 	if (speed_on_ac < 0 || speed_on_ac > MAX_SPEED) {
145 		dev_err(&pdev->dev, "Invalid speed_on_ac %d (must be between 0 and 3)\n",
146 			speed_on_ac);
147 		speed_on_ac = SPEED_ON_AC_DEFAULT;
148 	}
149 
150 	fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
151 	if (!fan)
152 		return -ENOMEM;
153 
154 	fan->dev = &pdev->dev;
155 	INIT_DELAYED_WORK(&fan->work, gpd_pocket_fan_worker);
156 
157 	/* Note this returns a "weak" reference which we don't need to free */
158 	fan->dts0 = thermal_zone_get_zone_by_name("soc_dts0");
159 	if (IS_ERR(fan->dts0))
160 		return -EPROBE_DEFER;
161 
162 	fan->dts1 = thermal_zone_get_zone_by_name("soc_dts1");
163 	if (IS_ERR(fan->dts1))
164 		return -EPROBE_DEFER;
165 
166 	fan->gpio0 = devm_gpiod_get_index(fan->dev, NULL, 0, GPIOD_ASIS);
167 	if (IS_ERR(fan->gpio0))
168 		return PTR_ERR(fan->gpio0);
169 
170 	fan->gpio1 = devm_gpiod_get_index(fan->dev, NULL, 1, GPIOD_ASIS);
171 	if (IS_ERR(fan->gpio1))
172 		return PTR_ERR(fan->gpio1);
173 
174 	gpd_pocket_fan_force_update(fan);
175 
176 	platform_set_drvdata(pdev, fan);
177 	return 0;
178 }
179 
gpd_pocket_fan_remove(struct platform_device * pdev)180 static int gpd_pocket_fan_remove(struct platform_device *pdev)
181 {
182 	struct gpd_pocket_fan_data *fan = platform_get_drvdata(pdev);
183 
184 	cancel_delayed_work_sync(&fan->work);
185 	return 0;
186 }
187 
188 #ifdef CONFIG_PM_SLEEP
gpd_pocket_fan_suspend(struct device * dev)189 static int gpd_pocket_fan_suspend(struct device *dev)
190 {
191 	struct gpd_pocket_fan_data *fan = dev_get_drvdata(dev);
192 
193 	cancel_delayed_work_sync(&fan->work);
194 	gpd_pocket_fan_set_speed(fan, gpd_pocket_fan_min_speed());
195 	return 0;
196 }
197 
gpd_pocket_fan_resume(struct device * dev)198 static int gpd_pocket_fan_resume(struct device *dev)
199 {
200 	struct gpd_pocket_fan_data *fan = dev_get_drvdata(dev);
201 
202 	gpd_pocket_fan_force_update(fan);
203 	return 0;
204 }
205 #endif
206 static SIMPLE_DEV_PM_OPS(gpd_pocket_fan_pm_ops,
207 			 gpd_pocket_fan_suspend,
208 			 gpd_pocket_fan_resume);
209 
210 static struct acpi_device_id gpd_pocket_fan_acpi_match[] = {
211 	{ "FAN02501" },
212 	{},
213 };
214 MODULE_DEVICE_TABLE(acpi, gpd_pocket_fan_acpi_match);
215 
216 static struct platform_driver gpd_pocket_fan_driver = {
217 	.probe	= gpd_pocket_fan_probe,
218 	.remove	= gpd_pocket_fan_remove,
219 	.driver	= {
220 		.name			= "gpd_pocket_fan",
221 		.acpi_match_table	= gpd_pocket_fan_acpi_match,
222 		.pm			= &gpd_pocket_fan_pm_ops,
223 	 },
224 };
225 
226 module_platform_driver(gpd_pocket_fan_driver);
227 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com");
228 MODULE_DESCRIPTION("GPD pocket fan driver");
229 MODULE_LICENSE("GPL");
230