fan.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. * Martin Peres
  24. */
  25. #include "priv.h"
  26. #include <subdev/bios/fan.h>
  27. #include <subdev/gpio.h>
  28. #include <subdev/timer.h>
  29. static int
  30. nvkm_fan_update(struct nvkm_fan *fan, bool immediate, int target)
  31. {
  32. struct nvkm_therm *therm = fan->parent;
  33. struct nvkm_subdev *subdev = &therm->subdev;
  34. struct nvkm_timer *tmr = subdev->device->timer;
  35. unsigned long flags;
  36. int ret = 0;
  37. int duty;
  38. /* update target fan speed, restricting to allowed range */
  39. spin_lock_irqsave(&fan->lock, flags);
  40. if (target < 0)
  41. target = fan->percent;
  42. target = max_t(u8, target, fan->bios.min_duty);
  43. target = min_t(u8, target, fan->bios.max_duty);
  44. if (fan->percent != target) {
  45. nvkm_debug(subdev, "FAN target: %d\n", target);
  46. fan->percent = target;
  47. }
  48. /* check that we're not already at the target duty cycle */
  49. duty = fan->get(therm);
  50. if (duty == target) {
  51. spin_unlock_irqrestore(&fan->lock, flags);
  52. return 0;
  53. }
  54. /* smooth out the fanspeed increase/decrease */
  55. if (!immediate && duty >= 0) {
  56. /* the constant "3" is a rough approximation taken from
  57. * nvidia's behaviour.
  58. * it is meant to bump the fan speed more incrementally
  59. */
  60. if (duty < target)
  61. duty = min(duty + 3, target);
  62. else if (duty > target)
  63. duty = max(duty - 3, target);
  64. } else {
  65. duty = target;
  66. }
  67. nvkm_debug(subdev, "FAN update: %d\n", duty);
  68. ret = fan->set(therm, duty);
  69. if (ret) {
  70. spin_unlock_irqrestore(&fan->lock, flags);
  71. return ret;
  72. }
  73. /* fan speed updated, drop the fan lock before grabbing the
  74. * alarm-scheduling lock and risking a deadlock
  75. */
  76. spin_unlock_irqrestore(&fan->lock, flags);
  77. /* schedule next fan update, if not at target speed already */
  78. if (list_empty(&fan->alarm.head) && target != duty) {
  79. u16 bump_period = fan->bios.bump_period;
  80. u16 slow_down_period = fan->bios.slow_down_period;
  81. u64 delay;
  82. if (duty > target)
  83. delay = slow_down_period;
  84. else if (duty == target)
  85. delay = min(bump_period, slow_down_period) ;
  86. else
  87. delay = bump_period;
  88. nvkm_timer_alarm(tmr, delay * 1000 * 1000, &fan->alarm);
  89. }
  90. return ret;
  91. }
  92. static void
  93. nvkm_fan_alarm(struct nvkm_alarm *alarm)
  94. {
  95. struct nvkm_fan *fan = container_of(alarm, struct nvkm_fan, alarm);
  96. nvkm_fan_update(fan, false, -1);
  97. }
  98. int
  99. nvkm_therm_fan_get(struct nvkm_therm *therm)
  100. {
  101. return therm->fan->get(therm);
  102. }
  103. int
  104. nvkm_therm_fan_set(struct nvkm_therm *therm, bool immediate, int percent)
  105. {
  106. return nvkm_fan_update(therm->fan, immediate, percent);
  107. }
  108. int
  109. nvkm_therm_fan_sense(struct nvkm_therm *therm)
  110. {
  111. struct nvkm_device *device = therm->subdev.device;
  112. struct nvkm_timer *tmr = device->timer;
  113. struct nvkm_gpio *gpio = device->gpio;
  114. u32 cycles, cur, prev;
  115. u64 start, end, tach;
  116. if (therm->func->fan_sense)
  117. return therm->func->fan_sense(therm);
  118. if (therm->fan->tach.func == DCB_GPIO_UNUSED)
  119. return -ENODEV;
  120. /* Time a complete rotation and extrapolate to RPM:
  121. * When the fan spins, it changes the value of GPIO FAN_SENSE.
  122. * We get 4 changes (0 -> 1 -> 0 -> 1) per complete rotation.
  123. */
  124. start = nvkm_timer_read(tmr);
  125. prev = nvkm_gpio_get(gpio, 0, therm->fan->tach.func,
  126. therm->fan->tach.line);
  127. cycles = 0;
  128. do {
  129. usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
  130. cur = nvkm_gpio_get(gpio, 0, therm->fan->tach.func,
  131. therm->fan->tach.line);
  132. if (prev != cur) {
  133. if (!start)
  134. start = nvkm_timer_read(tmr);
  135. cycles++;
  136. prev = cur;
  137. }
  138. } while (cycles < 5 && nvkm_timer_read(tmr) - start < 250000000);
  139. end = nvkm_timer_read(tmr);
  140. if (cycles == 5) {
  141. tach = (u64)60000000000ULL;
  142. do_div(tach, (end - start));
  143. return tach;
  144. } else
  145. return 0;
  146. }
  147. int
  148. nvkm_therm_fan_user_get(struct nvkm_therm *therm)
  149. {
  150. return nvkm_therm_fan_get(therm);
  151. }
  152. int
  153. nvkm_therm_fan_user_set(struct nvkm_therm *therm, int percent)
  154. {
  155. if (therm->mode != NVKM_THERM_CTRL_MANUAL)
  156. return -EINVAL;
  157. return nvkm_therm_fan_set(therm, true, percent);
  158. }
  159. static void
  160. nvkm_therm_fan_set_defaults(struct nvkm_therm *therm)
  161. {
  162. therm->fan->bios.pwm_freq = 0;
  163. therm->fan->bios.min_duty = 0;
  164. therm->fan->bios.max_duty = 100;
  165. therm->fan->bios.bump_period = 500;
  166. therm->fan->bios.slow_down_period = 2000;
  167. therm->fan->bios.linear_min_temp = 40;
  168. therm->fan->bios.linear_max_temp = 85;
  169. }
  170. static void
  171. nvkm_therm_fan_safety_checks(struct nvkm_therm *therm)
  172. {
  173. if (therm->fan->bios.min_duty > 100)
  174. therm->fan->bios.min_duty = 100;
  175. if (therm->fan->bios.max_duty > 100)
  176. therm->fan->bios.max_duty = 100;
  177. if (therm->fan->bios.min_duty > therm->fan->bios.max_duty)
  178. therm->fan->bios.min_duty = therm->fan->bios.max_duty;
  179. }
  180. int
  181. nvkm_therm_fan_init(struct nvkm_therm *therm)
  182. {
  183. return 0;
  184. }
  185. int
  186. nvkm_therm_fan_fini(struct nvkm_therm *therm, bool suspend)
  187. {
  188. struct nvkm_timer *tmr = therm->subdev.device->timer;
  189. if (suspend)
  190. nvkm_timer_alarm_cancel(tmr, &therm->fan->alarm);
  191. return 0;
  192. }
  193. int
  194. nvkm_therm_fan_ctor(struct nvkm_therm *therm)
  195. {
  196. struct nvkm_subdev *subdev = &therm->subdev;
  197. struct nvkm_device *device = subdev->device;
  198. struct nvkm_gpio *gpio = device->gpio;
  199. struct nvkm_bios *bios = device->bios;
  200. struct dcb_gpio_func func;
  201. int ret;
  202. /* attempt to locate a drivable fan, and determine control method */
  203. ret = nvkm_gpio_find(gpio, 0, DCB_GPIO_FAN, 0xff, &func);
  204. if (ret == 0) {
  205. /* FIXME: is this really the place to perform such checks ? */
  206. if (func.line != 16 && func.log[0] & DCB_GPIO_LOG_DIR_IN) {
  207. nvkm_debug(subdev, "GPIO_FAN is in input mode\n");
  208. ret = -EINVAL;
  209. } else {
  210. ret = nvkm_fanpwm_create(therm, &func);
  211. if (ret != 0)
  212. ret = nvkm_fantog_create(therm, &func);
  213. }
  214. }
  215. /* no controllable fan found, create a dummy fan module */
  216. if (ret != 0) {
  217. ret = nvkm_fannil_create(therm);
  218. if (ret)
  219. return ret;
  220. }
  221. nvkm_debug(subdev, "FAN control: %s\n", therm->fan->type);
  222. /* read the current speed, it is useful when resuming */
  223. therm->fan->percent = nvkm_therm_fan_get(therm);
  224. /* attempt to detect a tachometer connection */
  225. ret = nvkm_gpio_find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff,
  226. &therm->fan->tach);
  227. if (ret)
  228. therm->fan->tach.func = DCB_GPIO_UNUSED;
  229. /* initialise fan bump/slow update handling */
  230. therm->fan->parent = therm;
  231. nvkm_alarm_init(&therm->fan->alarm, nvkm_fan_alarm);
  232. spin_lock_init(&therm->fan->lock);
  233. /* other random init... */
  234. nvkm_therm_fan_set_defaults(therm);
  235. nvbios_perf_fan_parse(bios, &therm->fan->perf);
  236. if (!nvbios_fan_parse(bios, &therm->fan->bios)) {
  237. nvkm_debug(subdev, "parsing the fan table failed\n");
  238. if (nvbios_therm_fan_parse(bios, &therm->fan->bios))
  239. nvkm_error(subdev, "parsing both fan tables failed\n");
  240. }
  241. nvkm_therm_fan_safety_checks(therm);
  242. return 0;
  243. }