nv40.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. enum nv40_sensor_style { INVALID_STYLE = -1, OLD_STYLE = 0, NEW_STYLE = 1 };
  27. static enum nv40_sensor_style
  28. nv40_sensor_style(struct nvkm_therm *therm)
  29. {
  30. switch (therm->subdev.device->chipset) {
  31. case 0x43:
  32. case 0x44:
  33. case 0x4a:
  34. case 0x47:
  35. return OLD_STYLE;
  36. case 0x46:
  37. case 0x49:
  38. case 0x4b:
  39. case 0x4e:
  40. case 0x4c:
  41. case 0x67:
  42. case 0x68:
  43. case 0x63:
  44. return NEW_STYLE;
  45. default:
  46. return INVALID_STYLE;
  47. }
  48. }
  49. static int
  50. nv40_sensor_setup(struct nvkm_therm *therm)
  51. {
  52. struct nvkm_device *device = therm->subdev.device;
  53. enum nv40_sensor_style style = nv40_sensor_style(therm);
  54. /* enable ADC readout and disable the ALARM threshold */
  55. if (style == NEW_STYLE) {
  56. nvkm_mask(device, 0x15b8, 0x80000000, 0);
  57. nvkm_wr32(device, 0x15b0, 0x80003fff);
  58. mdelay(20); /* wait for the temperature to stabilize */
  59. return nvkm_rd32(device, 0x15b4) & 0x3fff;
  60. } else if (style == OLD_STYLE) {
  61. nvkm_wr32(device, 0x15b0, 0xff);
  62. mdelay(20); /* wait for the temperature to stabilize */
  63. return nvkm_rd32(device, 0x15b4) & 0xff;
  64. } else
  65. return -ENODEV;
  66. }
  67. static int
  68. nv40_temp_get(struct nvkm_therm *therm)
  69. {
  70. struct nvkm_device *device = therm->subdev.device;
  71. struct nvbios_therm_sensor *sensor = &therm->bios_sensor;
  72. enum nv40_sensor_style style = nv40_sensor_style(therm);
  73. int core_temp;
  74. if (style == NEW_STYLE) {
  75. nvkm_wr32(device, 0x15b0, 0x80003fff);
  76. core_temp = nvkm_rd32(device, 0x15b4) & 0x3fff;
  77. } else if (style == OLD_STYLE) {
  78. nvkm_wr32(device, 0x15b0, 0xff);
  79. core_temp = nvkm_rd32(device, 0x15b4) & 0xff;
  80. } else
  81. return -ENODEV;
  82. /* if the slope or the offset is unset, do no use the sensor */
  83. if (!sensor->slope_div || !sensor->slope_mult ||
  84. !sensor->offset_num || !sensor->offset_den)
  85. return -ENODEV;
  86. core_temp = core_temp * sensor->slope_mult / sensor->slope_div;
  87. core_temp = core_temp + sensor->offset_num / sensor->offset_den;
  88. core_temp = core_temp + sensor->offset_constant - 8;
  89. /* reserve negative temperatures for errors */
  90. if (core_temp < 0)
  91. core_temp = 0;
  92. return core_temp;
  93. }
  94. static int
  95. nv40_fan_pwm_ctrl(struct nvkm_therm *therm, int line, bool enable)
  96. {
  97. struct nvkm_subdev *subdev = &therm->subdev;
  98. struct nvkm_device *device = subdev->device;
  99. u32 mask = enable ? 0x80000000 : 0x00000000;
  100. if (line == 2) nvkm_mask(device, 0x0010f0, 0x80000000, mask);
  101. else if (line == 9) nvkm_mask(device, 0x0015f4, 0x80000000, mask);
  102. else {
  103. nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", line);
  104. return -ENODEV;
  105. }
  106. return 0;
  107. }
  108. static int
  109. nv40_fan_pwm_get(struct nvkm_therm *therm, int line, u32 *divs, u32 *duty)
  110. {
  111. struct nvkm_subdev *subdev = &therm->subdev;
  112. struct nvkm_device *device = subdev->device;
  113. if (line == 2) {
  114. u32 reg = nvkm_rd32(device, 0x0010f0);
  115. if (reg & 0x80000000) {
  116. *duty = (reg & 0x7fff0000) >> 16;
  117. *divs = (reg & 0x00007fff);
  118. return 0;
  119. }
  120. } else
  121. if (line == 9) {
  122. u32 reg = nvkm_rd32(device, 0x0015f4);
  123. if (reg & 0x80000000) {
  124. *divs = nvkm_rd32(device, 0x0015f8);
  125. *duty = (reg & 0x7fffffff);
  126. return 0;
  127. }
  128. } else {
  129. nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", line);
  130. return -ENODEV;
  131. }
  132. return -EINVAL;
  133. }
  134. static int
  135. nv40_fan_pwm_set(struct nvkm_therm *therm, int line, u32 divs, u32 duty)
  136. {
  137. struct nvkm_subdev *subdev = &therm->subdev;
  138. struct nvkm_device *device = subdev->device;
  139. if (line == 2) {
  140. nvkm_mask(device, 0x0010f0, 0x7fff7fff, (duty << 16) | divs);
  141. } else
  142. if (line == 9) {
  143. nvkm_wr32(device, 0x0015f8, divs);
  144. nvkm_mask(device, 0x0015f4, 0x7fffffff, duty);
  145. } else {
  146. nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", line);
  147. return -ENODEV;
  148. }
  149. return 0;
  150. }
  151. void
  152. nv40_therm_intr(struct nvkm_therm *therm)
  153. {
  154. struct nvkm_subdev *subdev = &therm->subdev;
  155. struct nvkm_device *device = subdev->device;
  156. uint32_t stat = nvkm_rd32(device, 0x1100);
  157. /* traitement */
  158. /* ack all IRQs */
  159. nvkm_wr32(device, 0x1100, 0x70000);
  160. nvkm_error(subdev, "THERM received an IRQ: stat = %x\n", stat);
  161. }
  162. static void
  163. nv40_therm_init(struct nvkm_therm *therm)
  164. {
  165. nv40_sensor_setup(therm);
  166. }
  167. static const struct nvkm_therm_func
  168. nv40_therm = {
  169. .init = nv40_therm_init,
  170. .intr = nv40_therm_intr,
  171. .pwm_ctrl = nv40_fan_pwm_ctrl,
  172. .pwm_get = nv40_fan_pwm_get,
  173. .pwm_set = nv40_fan_pwm_set,
  174. .temp_get = nv40_temp_get,
  175. .program_alarms = nvkm_therm_program_alarms_polling,
  176. };
  177. int
  178. nv40_therm_new(struct nvkm_device *device, int index,
  179. struct nvkm_therm **ptherm)
  180. {
  181. return nvkm_therm_new_(&nv40_therm, device, index, ptherm);
  182. }