nv50.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. static int
  27. pwm_info(struct nvkm_therm *therm, int *line, int *ctrl, int *indx)
  28. {
  29. struct nvkm_subdev *subdev = &therm->subdev;
  30. if (*line == 0x04) {
  31. *ctrl = 0x00e100;
  32. *line = 4;
  33. *indx = 0;
  34. } else
  35. if (*line == 0x09) {
  36. *ctrl = 0x00e100;
  37. *line = 9;
  38. *indx = 1;
  39. } else
  40. if (*line == 0x10) {
  41. *ctrl = 0x00e28c;
  42. *line = 0;
  43. *indx = 0;
  44. } else {
  45. nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", *line);
  46. return -ENODEV;
  47. }
  48. return 0;
  49. }
  50. int
  51. nv50_fan_pwm_ctrl(struct nvkm_therm *therm, int line, bool enable)
  52. {
  53. struct nvkm_device *device = therm->subdev.device;
  54. u32 data = enable ? 0x00000001 : 0x00000000;
  55. int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
  56. if (ret == 0)
  57. nvkm_mask(device, ctrl, 0x00010001 << line, data << line);
  58. return ret;
  59. }
  60. int
  61. nv50_fan_pwm_get(struct nvkm_therm *therm, int line, u32 *divs, u32 *duty)
  62. {
  63. struct nvkm_device *device = therm->subdev.device;
  64. int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
  65. if (ret)
  66. return ret;
  67. if (nvkm_rd32(device, ctrl) & (1 << line)) {
  68. *divs = nvkm_rd32(device, 0x00e114 + (id * 8));
  69. *duty = nvkm_rd32(device, 0x00e118 + (id * 8));
  70. return 0;
  71. }
  72. return -EINVAL;
  73. }
  74. int
  75. nv50_fan_pwm_set(struct nvkm_therm *therm, int line, u32 divs, u32 duty)
  76. {
  77. struct nvkm_device *device = therm->subdev.device;
  78. int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
  79. if (ret)
  80. return ret;
  81. nvkm_wr32(device, 0x00e114 + (id * 8), divs);
  82. nvkm_wr32(device, 0x00e118 + (id * 8), duty | 0x80000000);
  83. return 0;
  84. }
  85. int
  86. nv50_fan_pwm_clock(struct nvkm_therm *therm, int line)
  87. {
  88. struct nvkm_device *device = therm->subdev.device;
  89. int pwm_clock;
  90. /* determine the PWM source clock */
  91. if (device->chipset > 0x50 && device->chipset < 0x94) {
  92. u8 pwm_div = nvkm_rd32(device, 0x410c);
  93. if (nvkm_rd32(device, 0xc040) & 0x800000) {
  94. /* Use the HOST clock (100 MHz)
  95. * Where does this constant(2.4) comes from? */
  96. pwm_clock = (100000000 >> pwm_div) * 10 / 24;
  97. } else {
  98. /* Where does this constant(20) comes from? */
  99. pwm_clock = (device->crystal * 1000) >> pwm_div;
  100. pwm_clock /= 20;
  101. }
  102. } else {
  103. pwm_clock = (device->crystal * 1000) / 20;
  104. }
  105. return pwm_clock;
  106. }
  107. static void
  108. nv50_sensor_setup(struct nvkm_therm *therm)
  109. {
  110. struct nvkm_device *device = therm->subdev.device;
  111. nvkm_mask(device, 0x20010, 0x40000000, 0x0);
  112. mdelay(20); /* wait for the temperature to stabilize */
  113. }
  114. static int
  115. nv50_temp_get(struct nvkm_therm *therm)
  116. {
  117. struct nvkm_device *device = therm->subdev.device;
  118. struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
  119. int core_temp;
  120. core_temp = nvkm_rd32(device, 0x20014) & 0x3fff;
  121. /* if the slope or the offset is unset, do no use the sensor */
  122. if (!sensor->slope_div || !sensor->slope_mult ||
  123. !sensor->offset_num || !sensor->offset_den)
  124. return -ENODEV;
  125. core_temp = core_temp * sensor->slope_mult / sensor->slope_div;
  126. core_temp = core_temp + sensor->offset_num / sensor->offset_den;
  127. core_temp = core_temp + sensor->offset_constant - 8;
  128. /* reserve negative temperatures for errors */
  129. if (core_temp < 0)
  130. core_temp = 0;
  131. return core_temp;
  132. }
  133. static void
  134. nv50_therm_init(struct nvkm_therm *therm)
  135. {
  136. nv50_sensor_setup(therm);
  137. }
  138. static const struct nvkm_therm_func
  139. nv50_therm = {
  140. .init = nv50_therm_init,
  141. .intr = nv40_therm_intr,
  142. .pwm_ctrl = nv50_fan_pwm_ctrl,
  143. .pwm_get = nv50_fan_pwm_get,
  144. .pwm_set = nv50_fan_pwm_set,
  145. .pwm_clock = nv50_fan_pwm_clock,
  146. .temp_get = nv50_temp_get,
  147. .program_alarms = nvkm_therm_program_alarms_polling,
  148. };
  149. int
  150. nv50_therm_new(struct nvkm_device *device, int index,
  151. struct nvkm_therm **ptherm)
  152. {
  153. return nvkm_therm_new_(&nv50_therm, device, index, ptherm);
  154. }