nouveau_volt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright 2010 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. */
  24. #include <drm/drmP.h>
  25. #include "nouveau_drm.h"
  26. #include "nouveau_pm.h"
  27. #include <subdev/bios/gpio.h>
  28. #include <subdev/gpio.h>
  29. static const enum dcb_gpio_func_name vidtag[] = { 0x04, 0x05, 0x06, 0x1a, 0x73 };
  30. static int nr_vidtag = sizeof(vidtag) / sizeof(vidtag[0]);
  31. int
  32. nouveau_voltage_gpio_get(struct drm_device *dev)
  33. {
  34. struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage;
  35. struct nouveau_device *device = nouveau_dev(dev);
  36. struct nouveau_gpio *gpio = nouveau_gpio(device);
  37. u8 vid = 0;
  38. int i;
  39. for (i = 0; i < nr_vidtag; i++) {
  40. if (!(volt->vid_mask & (1 << i)))
  41. continue;
  42. vid |= gpio->get(gpio, 0, vidtag[i], 0xff) << i;
  43. }
  44. return nouveau_volt_lvl_lookup(dev, vid);
  45. }
  46. int
  47. nouveau_voltage_gpio_set(struct drm_device *dev, int voltage)
  48. {
  49. struct nouveau_device *device = nouveau_dev(dev);
  50. struct nouveau_gpio *gpio = nouveau_gpio(device);
  51. struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage;
  52. int vid, i;
  53. vid = nouveau_volt_vid_lookup(dev, voltage);
  54. if (vid < 0)
  55. return vid;
  56. for (i = 0; i < nr_vidtag; i++) {
  57. if (!(volt->vid_mask & (1 << i)))
  58. continue;
  59. gpio->set(gpio, 0, vidtag[i], 0xff, !!(vid & (1 << i)));
  60. }
  61. return 0;
  62. }
  63. int
  64. nouveau_volt_vid_lookup(struct drm_device *dev, int voltage)
  65. {
  66. struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage;
  67. int i;
  68. for (i = 0; i < volt->nr_level; i++) {
  69. if (volt->level[i].voltage == voltage)
  70. return volt->level[i].vid;
  71. }
  72. return -ENOENT;
  73. }
  74. int
  75. nouveau_volt_lvl_lookup(struct drm_device *dev, int vid)
  76. {
  77. struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage;
  78. int i;
  79. for (i = 0; i < volt->nr_level; i++) {
  80. if (volt->level[i].vid == vid)
  81. return volt->level[i].voltage;
  82. }
  83. return -ENOENT;
  84. }
  85. void
  86. nouveau_volt_init(struct drm_device *dev)
  87. {
  88. struct nouveau_drm *drm = nouveau_drm(dev);
  89. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  90. struct nouveau_pm *pm = nouveau_pm(dev);
  91. struct nouveau_pm_voltage *voltage = &pm->voltage;
  92. struct nvbios *bios = &drm->vbios;
  93. struct dcb_gpio_func func;
  94. struct bit_entry P;
  95. u8 *volt = NULL, *entry;
  96. int i, headerlen, recordlen, entries, vidmask, vidshift;
  97. if (bios->type == NVBIOS_BIT) {
  98. if (bit_table(dev, 'P', &P))
  99. return;
  100. if (P.version == 1)
  101. volt = ROMPTR(dev, P.data[16]);
  102. else
  103. if (P.version == 2)
  104. volt = ROMPTR(dev, P.data[12]);
  105. else {
  106. NV_WARN(drm, "unknown volt for BIT P %d\n", P.version);
  107. }
  108. } else {
  109. if (bios->data[bios->offset + 6] < 0x27) {
  110. NV_DEBUG(drm, "BMP version too old for voltage\n");
  111. return;
  112. }
  113. volt = ROMPTR(dev, bios->data[bios->offset + 0x98]);
  114. }
  115. if (!volt) {
  116. NV_DEBUG(drm, "voltage table pointer invalid\n");
  117. return;
  118. }
  119. switch (volt[0]) {
  120. case 0x10:
  121. case 0x11:
  122. case 0x12:
  123. headerlen = 5;
  124. recordlen = volt[1];
  125. entries = volt[2];
  126. vidshift = 0;
  127. vidmask = volt[4];
  128. break;
  129. case 0x20:
  130. headerlen = volt[1];
  131. recordlen = volt[3];
  132. entries = volt[2];
  133. vidshift = 0; /* could be vidshift like 0x30? */
  134. vidmask = volt[5];
  135. break;
  136. case 0x30:
  137. headerlen = volt[1];
  138. recordlen = volt[2];
  139. entries = volt[3];
  140. vidmask = volt[4];
  141. /* no longer certain what volt[5] is, if it's related to
  142. * the vid shift then it's definitely not a function of
  143. * how many bits are set.
  144. *
  145. * after looking at a number of nva3+ vbios images, they
  146. * all seem likely to have a static shift of 2.. lets
  147. * go with that for now until proven otherwise.
  148. */
  149. vidshift = 2;
  150. break;
  151. case 0x40:
  152. headerlen = volt[1];
  153. recordlen = volt[2];
  154. entries = volt[3]; /* not a clue what the entries are for.. */
  155. vidmask = volt[11]; /* guess.. */
  156. vidshift = 0;
  157. break;
  158. default:
  159. NV_WARN(drm, "voltage table 0x%02x unknown\n", volt[0]);
  160. return;
  161. }
  162. /* validate vid mask */
  163. voltage->vid_mask = vidmask;
  164. if (!voltage->vid_mask)
  165. return;
  166. i = 0;
  167. while (vidmask) {
  168. if (i > nr_vidtag) {
  169. NV_DEBUG(drm, "vid bit %d unknown\n", i);
  170. return;
  171. }
  172. if (gpio && gpio->find(gpio, 0, vidtag[i], 0xff, &func)) {
  173. NV_DEBUG(drm, "vid bit %d has no gpio tag\n", i);
  174. return;
  175. }
  176. vidmask >>= 1;
  177. i++;
  178. }
  179. /* parse vbios entries into common format */
  180. voltage->version = volt[0];
  181. if (voltage->version < 0x40) {
  182. voltage->nr_level = entries;
  183. voltage->level =
  184. kcalloc(entries, sizeof(*voltage->level), GFP_KERNEL);
  185. if (!voltage->level)
  186. return;
  187. entry = volt + headerlen;
  188. for (i = 0; i < entries; i++, entry += recordlen) {
  189. voltage->level[i].voltage = entry[0] * 10000;
  190. voltage->level[i].vid = entry[1] >> vidshift;
  191. }
  192. } else {
  193. u32 volt_uv = ROM32(volt[4]);
  194. s16 step_uv = ROM16(volt[8]);
  195. u8 vid;
  196. voltage->nr_level = voltage->vid_mask + 1;
  197. voltage->level = kcalloc(voltage->nr_level,
  198. sizeof(*voltage->level), GFP_KERNEL);
  199. if (!voltage->level)
  200. return;
  201. for (vid = 0; vid <= voltage->vid_mask; vid++) {
  202. voltage->level[vid].voltage = volt_uv;
  203. voltage->level[vid].vid = vid;
  204. volt_uv += step_uv;
  205. }
  206. }
  207. voltage->supported = true;
  208. }
  209. void
  210. nouveau_volt_fini(struct drm_device *dev)
  211. {
  212. struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage;
  213. kfree(volt->level);
  214. }