nouveau_volt.c 5.5 KB

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