fanpwm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <core/option.h>
  27. #include <subdev/bios.h>
  28. #include <subdev/bios/fan.h>
  29. #include <subdev/gpio.h>
  30. struct nvkm_fanpwm {
  31. struct nvkm_fan base;
  32. struct dcb_gpio_func func;
  33. };
  34. static int
  35. nvkm_fanpwm_get(struct nvkm_therm *therm)
  36. {
  37. struct nvkm_fanpwm *fan = (void *)therm->fan;
  38. struct nvkm_device *device = therm->subdev.device;
  39. struct nvkm_gpio *gpio = device->gpio;
  40. int card_type = device->card_type;
  41. u32 divs, duty;
  42. int ret;
  43. ret = therm->func->pwm_get(therm, fan->func.line, &divs, &duty);
  44. if (ret == 0 && divs) {
  45. divs = max(divs, duty);
  46. if (card_type <= NV_40 || (fan->func.log[0] & 1))
  47. duty = divs - duty;
  48. return (duty * 100) / divs;
  49. }
  50. return nvkm_gpio_get(gpio, 0, fan->func.func, fan->func.line) * 100;
  51. }
  52. static int
  53. nvkm_fanpwm_set(struct nvkm_therm *therm, int percent)
  54. {
  55. struct nvkm_fanpwm *fan = (void *)therm->fan;
  56. int card_type = therm->subdev.device->card_type;
  57. u32 divs, duty;
  58. int ret;
  59. divs = fan->base.perf.pwm_divisor;
  60. if (fan->base.bios.pwm_freq) {
  61. divs = 1;
  62. if (therm->func->pwm_clock)
  63. divs = therm->func->pwm_clock(therm, fan->func.line);
  64. divs /= fan->base.bios.pwm_freq;
  65. }
  66. duty = ((divs * percent) + 99) / 100;
  67. if (card_type <= NV_40 || (fan->func.log[0] & 1))
  68. duty = divs - duty;
  69. ret = therm->func->pwm_set(therm, fan->func.line, divs, duty);
  70. if (ret == 0)
  71. ret = therm->func->pwm_ctrl(therm, fan->func.line, true);
  72. return ret;
  73. }
  74. int
  75. nvkm_fanpwm_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
  76. {
  77. struct nvkm_device *device = therm->subdev.device;
  78. struct nvkm_bios *bios = device->bios;
  79. struct nvkm_fanpwm *fan;
  80. struct nvbios_therm_fan info = {};
  81. u32 divs, duty;
  82. nvbios_fan_parse(bios, &info);
  83. if (!nvkm_boolopt(device->cfgopt, "NvFanPWM", func->param) ||
  84. !therm->func->pwm_ctrl || info.type == NVBIOS_THERM_FAN_TOGGLE ||
  85. therm->func->pwm_get(therm, func->line, &divs, &duty) == -ENODEV)
  86. return -ENODEV;
  87. fan = kzalloc(sizeof(*fan), GFP_KERNEL);
  88. therm->fan = &fan->base;
  89. if (!fan)
  90. return -ENOMEM;
  91. fan->base.type = "PWM";
  92. fan->base.get = nvkm_fanpwm_get;
  93. fan->base.set = nvkm_fanpwm_set;
  94. fan->func = *func;
  95. return 0;
  96. }