gpd-pocket-fan.c 5.7 KB

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