gpd-pocket-fan.c 5.8 KB

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