gk104.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2015 Martin Peres
  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: Martin Peres
  23. */
  24. #include "priv.h"
  25. #include <subdev/volt.h>
  26. #include <subdev/gpio.h>
  27. #include <subdev/bios.h>
  28. #include <subdev/bios/volt.h>
  29. #include <subdev/fuse.h>
  30. #define gk104_volt(p) container_of((p), struct gk104_volt, base)
  31. struct gk104_volt {
  32. struct nvkm_volt base;
  33. struct nvbios_volt bios;
  34. };
  35. static int
  36. gk104_volt_get(struct nvkm_volt *base)
  37. {
  38. struct nvbios_volt *bios = &gk104_volt(base)->bios;
  39. struct nvkm_device *device = base->subdev.device;
  40. u32 div, duty;
  41. div = nvkm_rd32(device, 0x20340);
  42. duty = nvkm_rd32(device, 0x20344);
  43. return bios->base + bios->pwm_range * duty / div;
  44. }
  45. static int
  46. gk104_volt_set(struct nvkm_volt *base, u32 uv)
  47. {
  48. struct nvbios_volt *bios = &gk104_volt(base)->bios;
  49. struct nvkm_device *device = base->subdev.device;
  50. u32 div, duty;
  51. /* the blob uses this crystal frequency, let's use it too. */
  52. div = 27648000 / bios->pwm_freq;
  53. duty = DIV_ROUND_UP((uv - bios->base) * div, bios->pwm_range);
  54. nvkm_wr32(device, 0x20340, div);
  55. nvkm_wr32(device, 0x20344, 0x80000000 | duty);
  56. return 0;
  57. }
  58. static int
  59. gk104_volt_speedo_read(struct nvkm_volt *volt)
  60. {
  61. struct nvkm_device *device = volt->subdev.device;
  62. struct nvkm_fuse *fuse = device->fuse;
  63. int ret;
  64. if (!fuse)
  65. return -EINVAL;
  66. nvkm_wr32(device, 0x122634, 0x0);
  67. ret = nvkm_fuse_read(fuse, 0x3a8);
  68. nvkm_wr32(device, 0x122634, 0x41);
  69. return ret;
  70. }
  71. static const struct nvkm_volt_func
  72. gk104_volt_pwm = {
  73. .oneinit = gf100_volt_oneinit,
  74. .volt_get = gk104_volt_get,
  75. .volt_set = gk104_volt_set,
  76. .speedo_read = gk104_volt_speedo_read,
  77. }, gk104_volt_gpio = {
  78. .oneinit = gf100_volt_oneinit,
  79. .vid_get = nvkm_voltgpio_get,
  80. .vid_set = nvkm_voltgpio_set,
  81. .speedo_read = gk104_volt_speedo_read,
  82. };
  83. int
  84. gk104_volt_new(struct nvkm_device *device, int index, struct nvkm_volt **pvolt)
  85. {
  86. const struct nvkm_volt_func *volt_func = &gk104_volt_gpio;
  87. struct dcb_gpio_func gpio;
  88. struct nvbios_volt bios;
  89. struct gk104_volt *volt;
  90. u8 ver, hdr, cnt, len;
  91. const char *mode;
  92. if (!nvbios_volt_parse(device->bios, &ver, &hdr, &cnt, &len, &bios))
  93. return 0;
  94. if (!nvkm_gpio_find(device->gpio, 0, DCB_GPIO_VID_PWM, 0xff, &gpio) &&
  95. bios.type == NVBIOS_VOLT_PWM) {
  96. volt_func = &gk104_volt_pwm;
  97. }
  98. if (!(volt = kzalloc(sizeof(*volt), GFP_KERNEL)))
  99. return -ENOMEM;
  100. nvkm_volt_ctor(volt_func, device, index, &volt->base);
  101. *pvolt = &volt->base;
  102. volt->bios = bios;
  103. /* now that we have a subdev, we can show an error if we found through
  104. * the voltage table that we were supposed to use the PWN mode but we
  105. * did not find the right GPIO for it.
  106. */
  107. if (bios.type == NVBIOS_VOLT_PWM && volt_func != &gk104_volt_pwm) {
  108. nvkm_error(&volt->base.subdev,
  109. "Type mismatch between the voltage table type and "
  110. "the GPIO table. Fallback to GPIO mode.\n");
  111. }
  112. if (volt_func == &gk104_volt_gpio) {
  113. nvkm_voltgpio_init(&volt->base);
  114. mode = "GPIO";
  115. } else
  116. mode = "PWM";
  117. nvkm_debug(&volt->base.subdev, "Using %s mode\n", mode);
  118. return 0;
  119. }