Linux Audio

Check our new training course

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