tvmodesnv17.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright (C) 2009 Francisco Jerez.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include "nouveau_drm.h"
  29. #include "nouveau_encoder.h"
  30. #include "nouveau_crtc.h"
  31. #include "hw.h"
  32. #include "tvnv17.h"
  33. const char * const nv17_tv_norm_names[NUM_TV_NORMS] = {
  34. [TV_NORM_PAL] = "PAL",
  35. [TV_NORM_PAL_M] = "PAL-M",
  36. [TV_NORM_PAL_N] = "PAL-N",
  37. [TV_NORM_PAL_NC] = "PAL-Nc",
  38. [TV_NORM_NTSC_M] = "NTSC-M",
  39. [TV_NORM_NTSC_J] = "NTSC-J",
  40. [TV_NORM_HD480I] = "hd480i",
  41. [TV_NORM_HD480P] = "hd480p",
  42. [TV_NORM_HD576I] = "hd576i",
  43. [TV_NORM_HD576P] = "hd576p",
  44. [TV_NORM_HD720P] = "hd720p",
  45. [TV_NORM_HD1080I] = "hd1080i"
  46. };
  47. /* TV standard specific parameters */
  48. struct nv17_tv_norm_params nv17_tv_norms[NUM_TV_NORMS] = {
  49. [TV_NORM_PAL] = { TV_ENC_MODE, {
  50. .tv_enc_mode = { 720, 576, 50000, {
  51. 0x2a, 0x9, 0x8a, 0xcb, 0x0, 0x0, 0xb, 0x18,
  52. 0x7e, 0x40, 0x8a, 0x35, 0x27, 0x0, 0x34, 0x3,
  53. 0x3e, 0x3, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x9c,
  54. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x3,
  55. 0xd3, 0x4, 0xd4, 0x1, 0x2, 0x0, 0xa, 0x5,
  56. 0x0, 0x1a, 0xff, 0x3, 0x18, 0xf, 0x78, 0x0,
  57. 0x0, 0xb4, 0x0, 0x15, 0x49, 0x10, 0x0, 0x9b,
  58. 0xbd, 0x15, 0x5, 0x15, 0x3e, 0x3, 0x0, 0x0
  59. } } } },
  60. [TV_NORM_PAL_M] = { TV_ENC_MODE, {
  61. .tv_enc_mode = { 720, 480, 59940, {
  62. 0x21, 0xe6, 0xef, 0xe3, 0x0, 0x0, 0xb, 0x18,
  63. 0x7e, 0x44, 0x76, 0x32, 0x25, 0x0, 0x3c, 0x0,
  64. 0x3c, 0x0, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x83,
  65. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x1,
  66. 0xc5, 0x4, 0xc5, 0x1, 0x2, 0x0, 0xa, 0x5,
  67. 0x0, 0x18, 0xff, 0x3, 0x20, 0xf, 0x78, 0x0,
  68. 0x0, 0xb4, 0x0, 0x15, 0x40, 0x10, 0x0, 0x9c,
  69. 0xc8, 0x15, 0x5, 0x15, 0x3c, 0x0, 0x0, 0x0
  70. } } } },
  71. [TV_NORM_PAL_N] = { TV_ENC_MODE, {
  72. .tv_enc_mode = { 720, 576, 50000, {
  73. 0x2a, 0x9, 0x8a, 0xcb, 0x0, 0x0, 0xb, 0x18,
  74. 0x7e, 0x40, 0x8a, 0x32, 0x25, 0x0, 0x3c, 0x0,
  75. 0x3c, 0x0, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x9c,
  76. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x1,
  77. 0xc5, 0x4, 0xc5, 0x1, 0x2, 0x0, 0xa, 0x5,
  78. 0x0, 0x1a, 0xff, 0x3, 0x18, 0xf, 0x78, 0x0,
  79. 0x0, 0xb4, 0x0, 0x15, 0x49, 0x10, 0x0, 0x9b,
  80. 0xbd, 0x15, 0x5, 0x15, 0x3c, 0x0, 0x0, 0x0
  81. } } } },
  82. [TV_NORM_PAL_NC] = { TV_ENC_MODE, {
  83. .tv_enc_mode = { 720, 576, 50000, {
  84. 0x21, 0xf6, 0x94, 0x46, 0x0, 0x0, 0xb, 0x18,
  85. 0x7e, 0x44, 0x8a, 0x35, 0x27, 0x0, 0x34, 0x3,
  86. 0x3e, 0x3, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x9c,
  87. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x3,
  88. 0xd3, 0x4, 0xd4, 0x1, 0x2, 0x0, 0xa, 0x5,
  89. 0x0, 0x1a, 0xff, 0x3, 0x18, 0xf, 0x78, 0x0,
  90. 0x0, 0xb4, 0x0, 0x15, 0x49, 0x10, 0x0, 0x9b,
  91. 0xbd, 0x15, 0x5, 0x15, 0x3e, 0x3, 0x0, 0x0
  92. } } } },
  93. [TV_NORM_NTSC_M] = { TV_ENC_MODE, {
  94. .tv_enc_mode = { 720, 480, 59940, {
  95. 0x21, 0xf0, 0x7c, 0x1f, 0x0, 0x0, 0xb, 0x18,
  96. 0x7e, 0x44, 0x76, 0x48, 0x0, 0x0, 0x3c, 0x0,
  97. 0x3c, 0x0, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x83,
  98. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x1,
  99. 0xc5, 0x4, 0xc5, 0x1, 0x2, 0x0, 0xa, 0x5,
  100. 0x0, 0x16, 0xff, 0x3, 0x20, 0xf, 0x78, 0x0,
  101. 0x0, 0xb4, 0x0, 0x15, 0x4, 0x10, 0x0, 0x9c,
  102. 0xc8, 0x15, 0x5, 0x15, 0x3c, 0x0, 0x0, 0x0
  103. } } } },
  104. [TV_NORM_NTSC_J] = { TV_ENC_MODE, {
  105. .tv_enc_mode = { 720, 480, 59940, {
  106. 0x21, 0xf0, 0x7c, 0x1f, 0x0, 0x0, 0xb, 0x18,
  107. 0x7e, 0x44, 0x76, 0x48, 0x0, 0x0, 0x32, 0x0,
  108. 0x3c, 0x0, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x83,
  109. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x1,
  110. 0xcf, 0x4, 0xcf, 0x1, 0x2, 0x0, 0xa, 0x5,
  111. 0x0, 0x16, 0xff, 0x3, 0x20, 0xf, 0x78, 0x0,
  112. 0x0, 0xb4, 0x0, 0x15, 0x4, 0x10, 0x0, 0xa4,
  113. 0xc8, 0x15, 0x5, 0x15, 0x3c, 0x0, 0x0, 0x0
  114. } } } },
  115. [TV_NORM_HD480I] = { TV_ENC_MODE, {
  116. .tv_enc_mode = { 720, 480, 59940, {
  117. 0x21, 0xf0, 0x7c, 0x1f, 0x0, 0x0, 0xb, 0x18,
  118. 0x7e, 0x44, 0x76, 0x48, 0x0, 0x0, 0x32, 0x0,
  119. 0x3c, 0x0, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x83,
  120. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x1,
  121. 0xcf, 0x4, 0xcf, 0x1, 0x2, 0x0, 0xa, 0x5,
  122. 0x0, 0x16, 0xff, 0x3, 0x20, 0xf, 0x78, 0x0,
  123. 0x0, 0xb4, 0x0, 0x15, 0x4, 0x10, 0x0, 0xa4,
  124. 0xc8, 0x15, 0x5, 0x15, 0x3c, 0x0, 0x0, 0x0
  125. } } } },
  126. [TV_NORM_HD576I] = { TV_ENC_MODE, {
  127. .tv_enc_mode = { 720, 576, 50000, {
  128. 0x2a, 0x9, 0x8a, 0xcb, 0x0, 0x0, 0xb, 0x18,
  129. 0x7e, 0x40, 0x8a, 0x35, 0x27, 0x0, 0x34, 0x3,
  130. 0x3e, 0x3, 0x17, 0x21, 0x1b, 0x1b, 0x24, 0x9c,
  131. 0x1, 0x0, 0xf, 0xf, 0x60, 0x5, 0xd3, 0x3,
  132. 0xd3, 0x4, 0xd4, 0x1, 0x2, 0x0, 0xa, 0x5,
  133. 0x0, 0x1a, 0xff, 0x3, 0x18, 0xf, 0x78, 0x0,
  134. 0x0, 0xb4, 0x0, 0x15, 0x49, 0x10, 0x0, 0x9b,
  135. 0xbd, 0x15, 0x5, 0x15, 0x3e, 0x3, 0x0, 0x0
  136. } } } },
  137. [TV_NORM_HD480P] = { CTV_ENC_MODE, {
  138. .ctv_enc_mode = {
  139. .mode = { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000,
  140. 720, 735, 743, 858, 0, 480, 490, 494, 525, 0,
  141. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  142. .ctv_regs = { 0x3540000, 0x0, 0x0, 0x314,
  143. 0x354003a, 0x40000, 0x6f0344, 0x18100000,
  144. 0x10160004, 0x10060005, 0x1006000c, 0x10060020,
  145. 0x10060021, 0x140e0022, 0x10060202, 0x1802020a,
  146. 0x1810020b, 0x10000fff, 0x10000fff, 0x10000fff,
  147. 0x10000fff, 0x10000fff, 0x10000fff, 0x70,
  148. 0x3ff0000, 0x57, 0x2e001e, 0x258012c,
  149. 0xa0aa04ec, 0x30, 0x80960019, 0x12c0300,
  150. 0x2019, 0x600, 0x32060019, 0x0, 0x0, 0x400
  151. } } } },
  152. [TV_NORM_HD576P] = { CTV_ENC_MODE, {
  153. .ctv_enc_mode = {
  154. .mode = { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000,
  155. 720, 730, 738, 864, 0, 576, 581, 585, 625, 0,
  156. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  157. .ctv_regs = { 0x3540000, 0x0, 0x0, 0x314,
  158. 0x354003a, 0x40000, 0x6f0344, 0x18100000,
  159. 0x10060001, 0x10060009, 0x10060026, 0x10060027,
  160. 0x140e0028, 0x10060268, 0x1810026d, 0x10000fff,
  161. 0x10000fff, 0x10000fff, 0x10000fff, 0x10000fff,
  162. 0x10000fff, 0x10000fff, 0x10000fff, 0x69,
  163. 0x3ff0000, 0x57, 0x2e001e, 0x258012c,
  164. 0xa0aa04ec, 0x30, 0x80960019, 0x12c0300,
  165. 0x2019, 0x600, 0x32060019, 0x0, 0x0, 0x400
  166. } } } },
  167. [TV_NORM_HD720P] = { CTV_ENC_MODE, {
  168. .ctv_enc_mode = {
  169. .mode = { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250,
  170. 1280, 1349, 1357, 1650, 0, 720, 725, 730, 750, 0,
  171. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  172. .ctv_regs = { 0x1260394, 0x0, 0x0, 0x622,
  173. 0x66b0021, 0x6004a, 0x1210626, 0x8170000,
  174. 0x70004, 0x70016, 0x70017, 0x40f0018,
  175. 0x702e8, 0x81702ed, 0xfff, 0xfff,
  176. 0xfff, 0xfff, 0xfff, 0xfff,
  177. 0xfff, 0xfff, 0xfff, 0x0,
  178. 0x2e40001, 0x58, 0x2e001e, 0x258012c,
  179. 0xa0aa04ec, 0x30, 0x810c0039, 0x12c0300,
  180. 0xc0002039, 0x600, 0x32060039, 0x0, 0x0, 0x0
  181. } } } },
  182. [TV_NORM_HD1080I] = { CTV_ENC_MODE, {
  183. .ctv_enc_mode = {
  184. .mode = { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250,
  185. 1920, 1961, 2049, 2200, 0, 1080, 1084, 1088, 1125, 0,
  186. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC
  187. | DRM_MODE_FLAG_INTERLACE) },
  188. .ctv_regs = { 0xac0420, 0x44c0478, 0x4a4, 0x4fc0868,
  189. 0x8940028, 0x60054, 0xe80870, 0xbf70000,
  190. 0xbc70004, 0x70005, 0x70012, 0x70013,
  191. 0x40f0014, 0x70230, 0xbf70232, 0xbf70233,
  192. 0x1c70237, 0x70238, 0x70244, 0x70245,
  193. 0x40f0246, 0x70462, 0x1f70464, 0x0,
  194. 0x2e40001, 0x58, 0x2e001e, 0x258012c,
  195. 0xa0aa04ec, 0x30, 0x815f004c, 0x12c0300,
  196. 0xc000204c, 0x600, 0x3206004c, 0x0, 0x0, 0x0
  197. } } } }
  198. };
  199. /*
  200. * The following is some guesswork on how the TV encoder flicker
  201. * filter/rescaler works:
  202. *
  203. * It seems to use some sort of resampling filter, it is controlled
  204. * through the registers at NV_PTV_HFILTER and NV_PTV_VFILTER, they
  205. * control the horizontal and vertical stage respectively, there is
  206. * also NV_PTV_HFILTER2 the blob fills identically to NV_PTV_HFILTER,
  207. * but they seem to do nothing. A rough guess might be that they could
  208. * be used to independently control the filtering of each interlaced
  209. * field, but I don't know how they are enabled. The whole filtering
  210. * process seems to be disabled with bits 26:27 of PTV_200, but we
  211. * aren't doing that.
  212. *
  213. * The layout of both register sets is the same:
  214. *
  215. * A: [BASE+0x18]...[BASE+0x0] [BASE+0x58]..[BASE+0x40]
  216. * B: [BASE+0x34]...[BASE+0x1c] [BASE+0x74]..[BASE+0x5c]
  217. *
  218. * Each coefficient is stored in bits [31],[15:9] in two's complement
  219. * format. They seem to be some kind of weights used in a low-pass
  220. * filter. Both A and B coefficients are applied to the 14 nearest
  221. * samples on each side (Listed from nearest to furthermost. They
  222. * roughly cover 2 framebuffer pixels on each side). They are
  223. * probably multiplied with some more hardwired weights before being
  224. * used: B-coefficients are applied the same on both sides,
  225. * A-coefficients are inverted before being applied to the opposite
  226. * side.
  227. *
  228. * After all the hassle, I got the following formula by empirical
  229. * means...
  230. */
  231. #define calc_overscan(o) interpolate(0x100, 0xe1, 0xc1, o)
  232. #define id1 (1LL << 8)
  233. #define id2 (1LL << 16)
  234. #define id3 (1LL << 24)
  235. #define id4 (1LL << 32)
  236. #define id5 (1LL << 48)
  237. static struct filter_params{
  238. int64_t k1;
  239. int64_t ki;
  240. int64_t ki2;
  241. int64_t ki3;
  242. int64_t kr;
  243. int64_t kir;
  244. int64_t ki2r;
  245. int64_t ki3r;
  246. int64_t kf;
  247. int64_t kif;
  248. int64_t ki2f;
  249. int64_t ki3f;
  250. int64_t krf;
  251. int64_t kirf;
  252. int64_t ki2rf;
  253. int64_t ki3rf;
  254. } fparams[2][4] = {
  255. /* Horizontal filter parameters */
  256. {
  257. {64.311690 * id5, -39.516924 * id5, 6.586143 * id5, 0.000002 * id5,
  258. 0.051285 * id4, 26.168746 * id4, -4.361449 * id4, -0.000001 * id4,
  259. 9.308169 * id3, 78.180965 * id3, -13.030158 * id3, -0.000001 * id3,
  260. -8.801540 * id1, -46.572890 * id1, 7.762145 * id1, -0.000000 * id1},
  261. {-44.565569 * id5, -68.081246 * id5, 39.812074 * id5, -4.009316 * id5,
  262. 29.832207 * id4, 50.047322 * id4, -25.380017 * id4, 2.546422 * id4,
  263. 104.605622 * id3, 141.908641 * id3, -74.322319 * id3, 7.484316 * id3,
  264. -37.081621 * id1, -90.397510 * id1, 42.784229 * id1, -4.289952 * id1},
  265. {-56.793244 * id5, 31.153584 * id5, -5.192247 * id5, -0.000003 * id5,
  266. 33.541131 * id4, -34.149302 * id4, 5.691537 * id4, 0.000002 * id4,
  267. 87.196610 * id3, -88.995169 * id3, 14.832456 * id3, 0.000012 * id3,
  268. 17.288138 * id1, 71.864786 * id1, -11.977408 * id1, -0.000009 * id1},
  269. {51.787796 * id5, 21.211771 * id5, -18.993730 * id5, 1.853310 * id5,
  270. -41.470726 * id4, -17.775823 * id4, 13.057821 * id4, -1.15823 * id4,
  271. -154.235673 * id3, -44.878641 * id3, 40.656077 * id3, -3.695595 * id3,
  272. 112.201065 * id1, 39.992155 * id1, -25.155714 * id1, 2.113984 * id1},
  273. },
  274. /* Vertical filter parameters */
  275. {
  276. {67.601979 * id5, 0.428319 * id5, -0.071318 * id5, -0.000012 * id5,
  277. -3.402339 * id4, 0.000209 * id4, -0.000092 * id4, 0.000010 * id4,
  278. -9.180996 * id3, 6.111270 * id3, -1.024457 * id3, 0.001043 * id3,
  279. 6.060315 * id1, -0.017425 * id1, 0.007830 * id1, -0.000869 * id1},
  280. {6.755647 * id5, 5.841348 * id5, 1.469734 * id5, -0.149656 * id5,
  281. 8.293120 * id4, -1.192888 * id4, -0.947652 * id4, 0.094507 * id4,
  282. 37.526655 * id3, 10.257875 * id3, -10.823275 * id3, 1.081497 * id3,
  283. -2.361928 * id1, -2.059432 * id1, 1.840671 * id1, -0.168100 * id1},
  284. {-14.780391 * id5, -16.042148 * id5, 2.673692 * id5, -0.000000 * id5,
  285. 39.541978 * id4, 5.680053 * id4, -0.946676 * id4, 0.000000 * id4,
  286. 152.994486 * id3, 12.625439 * id3, -2.119579 * id3, 0.002708 * id3,
  287. -38.125089 * id1, -0.855880 * id1, 0.155359 * id1, -0.002245 * id1},
  288. {-27.476193 * id5, -1.454976 * id5, 1.286557 * id5, 0.025346 * id5,
  289. 20.687300 * id4, 3.014003 * id4, -0.557786 * id4, -0.01311 * id4,
  290. 60.008737 * id3, -0.738273 * id3, 5.408217 * id3, -0.796798 * id3,
  291. -17.296835 * id1, 4.438577 * id1, -2.809420 * id1, 0.385491 * id1},
  292. }
  293. };
  294. static void tv_setup_filter(struct drm_encoder *encoder)
  295. {
  296. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  297. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  298. struct drm_display_mode *mode = &encoder->crtc->mode;
  299. uint32_t (*filters[])[4][7] = {&tv_enc->state.hfilter,
  300. &tv_enc->state.vfilter};
  301. int i, j, k;
  302. int32_t overscan = calc_overscan(tv_enc->overscan);
  303. int64_t flicker = (tv_enc->flicker - 50) * (id3 / 100);
  304. uint64_t rs[] = {mode->hdisplay * id3,
  305. mode->vdisplay * id3};
  306. do_div(rs[0], overscan * tv_norm->tv_enc_mode.hdisplay);
  307. do_div(rs[1], overscan * tv_norm->tv_enc_mode.vdisplay);
  308. for (k = 0; k < 2; k++) {
  309. rs[k] = max((int64_t)rs[k], id2);
  310. for (j = 0; j < 4; j++) {
  311. struct filter_params *p = &fparams[k][j];
  312. for (i = 0; i < 7; i++) {
  313. int64_t c = (p->k1 + p->ki*i + p->ki2*i*i +
  314. p->ki3*i*i*i)
  315. + (p->kr + p->kir*i + p->ki2r*i*i +
  316. p->ki3r*i*i*i) * rs[k]
  317. + (p->kf + p->kif*i + p->ki2f*i*i +
  318. p->ki3f*i*i*i) * flicker
  319. + (p->krf + p->kirf*i + p->ki2rf*i*i +
  320. p->ki3rf*i*i*i) * flicker * rs[k];
  321. (*filters[k])[j][i] = (c + id5/2) >> 39
  322. & (0x1 << 31 | 0x7f << 9);
  323. }
  324. }
  325. }
  326. }
  327. /* Hardware state saving/restoring */
  328. static void tv_save_filter(struct drm_device *dev, uint32_t base,
  329. uint32_t regs[4][7])
  330. {
  331. int i, j;
  332. uint32_t offsets[] = { base, base + 0x1c, base + 0x40, base + 0x5c };
  333. for (i = 0; i < 4; i++) {
  334. for (j = 0; j < 7; j++)
  335. regs[i][j] = nv_read_ptv(dev, offsets[i]+4*j);
  336. }
  337. }
  338. static void tv_load_filter(struct drm_device *dev, uint32_t base,
  339. uint32_t regs[4][7])
  340. {
  341. int i, j;
  342. uint32_t offsets[] = { base, base + 0x1c, base + 0x40, base + 0x5c };
  343. for (i = 0; i < 4; i++) {
  344. for (j = 0; j < 7; j++)
  345. nv_write_ptv(dev, offsets[i]+4*j, regs[i][j]);
  346. }
  347. }
  348. void nv17_tv_state_save(struct drm_device *dev, struct nv17_tv_state *state)
  349. {
  350. int i;
  351. for (i = 0; i < 0x40; i++)
  352. state->tv_enc[i] = nv_read_tv_enc(dev, i);
  353. tv_save_filter(dev, NV_PTV_HFILTER, state->hfilter);
  354. tv_save_filter(dev, NV_PTV_HFILTER2, state->hfilter2);
  355. tv_save_filter(dev, NV_PTV_VFILTER, state->vfilter);
  356. nv_save_ptv(dev, state, 200);
  357. nv_save_ptv(dev, state, 204);
  358. nv_save_ptv(dev, state, 208);
  359. nv_save_ptv(dev, state, 20c);
  360. nv_save_ptv(dev, state, 304);
  361. nv_save_ptv(dev, state, 500);
  362. nv_save_ptv(dev, state, 504);
  363. nv_save_ptv(dev, state, 508);
  364. nv_save_ptv(dev, state, 600);
  365. nv_save_ptv(dev, state, 604);
  366. nv_save_ptv(dev, state, 608);
  367. nv_save_ptv(dev, state, 60c);
  368. nv_save_ptv(dev, state, 610);
  369. nv_save_ptv(dev, state, 614);
  370. }
  371. void nv17_tv_state_load(struct drm_device *dev, struct nv17_tv_state *state)
  372. {
  373. int i;
  374. for (i = 0; i < 0x40; i++)
  375. nv_write_tv_enc(dev, i, state->tv_enc[i]);
  376. tv_load_filter(dev, NV_PTV_HFILTER, state->hfilter);
  377. tv_load_filter(dev, NV_PTV_HFILTER2, state->hfilter2);
  378. tv_load_filter(dev, NV_PTV_VFILTER, state->vfilter);
  379. nv_load_ptv(dev, state, 200);
  380. nv_load_ptv(dev, state, 204);
  381. nv_load_ptv(dev, state, 208);
  382. nv_load_ptv(dev, state, 20c);
  383. nv_load_ptv(dev, state, 304);
  384. nv_load_ptv(dev, state, 500);
  385. nv_load_ptv(dev, state, 504);
  386. nv_load_ptv(dev, state, 508);
  387. nv_load_ptv(dev, state, 600);
  388. nv_load_ptv(dev, state, 604);
  389. nv_load_ptv(dev, state, 608);
  390. nv_load_ptv(dev, state, 60c);
  391. nv_load_ptv(dev, state, 610);
  392. nv_load_ptv(dev, state, 614);
  393. /* This is required for some settings to kick in. */
  394. nv_write_tv_enc(dev, 0x3e, 1);
  395. nv_write_tv_enc(dev, 0x3e, 0);
  396. }
  397. /* Timings similar to the ones the blob sets */
  398. const struct drm_display_mode nv17_tv_modes[] = {
  399. { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 0,
  400. 320, 344, 392, 560, 0, 200, 200, 202, 220, 0,
  401. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC
  402. | DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_CLKDIV2) },
  403. { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 0,
  404. 320, 344, 392, 560, 0, 240, 240, 246, 263, 0,
  405. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC
  406. | DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_CLKDIV2) },
  407. { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 0,
  408. 400, 432, 496, 640, 0, 300, 300, 303, 314, 0,
  409. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC
  410. | DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_CLKDIV2) },
  411. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 0,
  412. 640, 672, 768, 880, 0, 480, 480, 492, 525, 0,
  413. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  414. { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 0,
  415. 720, 752, 872, 960, 0, 480, 480, 493, 525, 0,
  416. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  417. { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 0,
  418. 720, 776, 856, 960, 0, 576, 576, 588, 597, 0,
  419. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  420. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 0,
  421. 800, 840, 920, 1040, 0, 600, 600, 604, 618, 0,
  422. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  423. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 0,
  424. 1024, 1064, 1200, 1344, 0, 768, 768, 777, 806, 0,
  425. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  426. {}
  427. };
  428. void nv17_tv_update_properties(struct drm_encoder *encoder)
  429. {
  430. struct drm_device *dev = encoder->dev;
  431. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  432. struct nv17_tv_state *regs = &tv_enc->state;
  433. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  434. int subconnector = tv_enc->select_subconnector ?
  435. tv_enc->select_subconnector :
  436. tv_enc->subconnector;
  437. switch (subconnector) {
  438. case DRM_MODE_SUBCONNECTOR_Composite:
  439. {
  440. regs->ptv_204 = 0x2;
  441. /* The composite connector may be found on either pin. */
  442. if (tv_enc->pin_mask & 0x4)
  443. regs->ptv_204 |= 0x010000;
  444. else if (tv_enc->pin_mask & 0x2)
  445. regs->ptv_204 |= 0x100000;
  446. else
  447. regs->ptv_204 |= 0x110000;
  448. regs->tv_enc[0x7] = 0x10;
  449. break;
  450. }
  451. case DRM_MODE_SUBCONNECTOR_SVIDEO:
  452. regs->ptv_204 = 0x11012;
  453. regs->tv_enc[0x7] = 0x18;
  454. break;
  455. case DRM_MODE_SUBCONNECTOR_Component:
  456. regs->ptv_204 = 0x111333;
  457. regs->tv_enc[0x7] = 0x14;
  458. break;
  459. case DRM_MODE_SUBCONNECTOR_SCART:
  460. regs->ptv_204 = 0x111012;
  461. regs->tv_enc[0x7] = 0x18;
  462. break;
  463. }
  464. regs->tv_enc[0x20] = interpolate(0, tv_norm->tv_enc_mode.tv_enc[0x20],
  465. 255, tv_enc->saturation);
  466. regs->tv_enc[0x22] = interpolate(0, tv_norm->tv_enc_mode.tv_enc[0x22],
  467. 255, tv_enc->saturation);
  468. regs->tv_enc[0x25] = tv_enc->hue * 255 / 100;
  469. nv_load_ptv(dev, regs, 204);
  470. nv_load_tv_enc(dev, regs, 7);
  471. nv_load_tv_enc(dev, regs, 20);
  472. nv_load_tv_enc(dev, regs, 22);
  473. nv_load_tv_enc(dev, regs, 25);
  474. }
  475. void nv17_tv_update_rescaler(struct drm_encoder *encoder)
  476. {
  477. struct drm_device *dev = encoder->dev;
  478. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  479. struct nv17_tv_state *regs = &tv_enc->state;
  480. regs->ptv_208 = 0x40 | (calc_overscan(tv_enc->overscan) << 8);
  481. tv_setup_filter(encoder);
  482. nv_load_ptv(dev, regs, 208);
  483. tv_load_filter(dev, NV_PTV_HFILTER, regs->hfilter);
  484. tv_load_filter(dev, NV_PTV_HFILTER2, regs->hfilter2);
  485. tv_load_filter(dev, NV_PTV_VFILTER, regs->vfilter);
  486. }
  487. void nv17_ctv_update_rescaler(struct drm_encoder *encoder)
  488. {
  489. struct drm_device *dev = encoder->dev;
  490. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  491. int head = nouveau_crtc(encoder->crtc)->index;
  492. struct nv04_crtc_reg *regs = &nv04_display(dev)->mode_reg.crtc_reg[head];
  493. struct drm_display_mode *crtc_mode = &encoder->crtc->mode;
  494. struct drm_display_mode *output_mode =
  495. &get_tv_norm(encoder)->ctv_enc_mode.mode;
  496. int overscan, hmargin, vmargin, hratio, vratio;
  497. /* The rescaler doesn't do the right thing for interlaced modes. */
  498. if (output_mode->flags & DRM_MODE_FLAG_INTERLACE)
  499. overscan = 100;
  500. else
  501. overscan = tv_enc->overscan;
  502. hmargin = (output_mode->hdisplay - crtc_mode->hdisplay) / 2;
  503. vmargin = (output_mode->vdisplay - crtc_mode->vdisplay) / 2;
  504. hmargin = interpolate(0, min(hmargin, output_mode->hdisplay/20),
  505. hmargin, overscan);
  506. vmargin = interpolate(0, min(vmargin, output_mode->vdisplay/20),
  507. vmargin, overscan);
  508. hratio = crtc_mode->hdisplay * 0x800 /
  509. (output_mode->hdisplay - 2*hmargin);
  510. vratio = crtc_mode->vdisplay * 0x800 /
  511. (output_mode->vdisplay - 2*vmargin) & ~3;
  512. regs->fp_horiz_regs[FP_VALID_START] = hmargin;
  513. regs->fp_horiz_regs[FP_VALID_END] = output_mode->hdisplay - hmargin - 1;
  514. regs->fp_vert_regs[FP_VALID_START] = vmargin;
  515. regs->fp_vert_regs[FP_VALID_END] = output_mode->vdisplay - vmargin - 1;
  516. regs->fp_debug_1 = NV_PRAMDAC_FP_DEBUG_1_YSCALE_TESTMODE_ENABLE |
  517. XLATE(vratio, 0, NV_PRAMDAC_FP_DEBUG_1_YSCALE_VALUE) |
  518. NV_PRAMDAC_FP_DEBUG_1_XSCALE_TESTMODE_ENABLE |
  519. XLATE(hratio, 0, NV_PRAMDAC_FP_DEBUG_1_XSCALE_VALUE);
  520. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HVALID_START,
  521. regs->fp_horiz_regs[FP_VALID_START]);
  522. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HVALID_END,
  523. regs->fp_horiz_regs[FP_VALID_END]);
  524. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_VVALID_START,
  525. regs->fp_vert_regs[FP_VALID_START]);
  526. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_VVALID_END,
  527. regs->fp_vert_regs[FP_VALID_END]);
  528. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_DEBUG_1, regs->fp_debug_1);
  529. }