iccsense.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <subdev/bios.h>
  25. #include <subdev/bios/bit.h>
  26. #include <subdev/bios/extdev.h>
  27. #include <subdev/bios/iccsense.h>
  28. static u32
  29. nvbios_iccsense_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
  30. u8 *len)
  31. {
  32. struct bit_entry bit_P;
  33. u32 iccsense;
  34. if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
  35. bit_P.length < 0x2c)
  36. return 0;
  37. iccsense = nvbios_rd32(bios, bit_P.offset + 0x28);
  38. if (!iccsense)
  39. return 0;
  40. *ver = nvbios_rd08(bios, iccsense + 0);
  41. switch (*ver) {
  42. case 0x10:
  43. case 0x20:
  44. *hdr = nvbios_rd08(bios, iccsense + 1);
  45. *len = nvbios_rd08(bios, iccsense + 2);
  46. *cnt = nvbios_rd08(bios, iccsense + 3);
  47. return iccsense;
  48. default:
  49. break;
  50. }
  51. return 0;
  52. }
  53. int
  54. nvbios_iccsense_parse(struct nvkm_bios *bios, struct nvbios_iccsense *iccsense)
  55. {
  56. struct nvkm_subdev *subdev = &bios->subdev;
  57. u8 ver, hdr, cnt, len, i;
  58. u32 table, entry;
  59. table = nvbios_iccsense_table(bios, &ver, &hdr, &cnt, &len);
  60. if (!table || !cnt)
  61. return -EINVAL;
  62. if (ver != 0x10 && ver != 0x20) {
  63. nvkm_error(subdev, "ICCSENSE version 0x%02x unknown\n", ver);
  64. return -EINVAL;
  65. }
  66. iccsense->nr_entry = cnt;
  67. iccsense->rail = kmalloc(sizeof(struct pwr_rail_t) * cnt, GFP_KERNEL);
  68. if (!iccsense->rail)
  69. return -ENOMEM;
  70. for (i = 0; i < cnt; ++i) {
  71. struct nvbios_extdev_func extdev;
  72. struct pwr_rail_t *rail = &iccsense->rail[i];
  73. u8 res_start = 0;
  74. int r;
  75. entry = table + hdr + i * len;
  76. switch(ver) {
  77. case 0x10:
  78. rail->mode = nvbios_rd08(bios, entry + 0x1);
  79. rail->extdev_id = nvbios_rd08(bios, entry + 0x2);
  80. res_start = 0x3;
  81. break;
  82. case 0x20:
  83. rail->mode = nvbios_rd08(bios, entry);
  84. rail->extdev_id = nvbios_rd08(bios, entry + 0x1);
  85. res_start = 0x5;
  86. break;
  87. };
  88. if (nvbios_extdev_parse(bios, rail->extdev_id, &extdev))
  89. continue;
  90. switch (extdev.type) {
  91. case NVBIOS_EXTDEV_INA209:
  92. case NVBIOS_EXTDEV_INA219:
  93. rail->resistor_count = 1;
  94. break;
  95. case NVBIOS_EXTDEV_INA3221:
  96. rail->resistor_count = 3;
  97. break;
  98. default:
  99. rail->resistor_count = 0;
  100. break;
  101. };
  102. for (r = 0; r < rail->resistor_count; ++r) {
  103. rail->resistors[r].mohm = nvbios_rd08(bios, entry + res_start + r * 2);
  104. rail->resistors[r].enabled = !(nvbios_rd08(bios, entry + res_start + r * 2 + 1) & 0x40);
  105. }
  106. rail->config = nvbios_rd16(bios, entry + res_start + rail->resistor_count * 2);
  107. }
  108. return 0;
  109. }