intel_tv.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. /*
  2. * Copyright © 2006-2008 Intel Corporation
  3. * Jesse Barnes <jesse.barnes@intel.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Eric Anholt <eric@anholt.net>
  26. *
  27. */
  28. /** @file
  29. * Integrated TV-out support for the 915GM and 945GM.
  30. */
  31. #include <drm/drmP.h>
  32. #include <drm/drm_atomic_helper.h>
  33. #include <drm/drm_crtc.h>
  34. #include <drm/drm_edid.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. enum tv_margin {
  39. TV_MARGIN_LEFT, TV_MARGIN_TOP,
  40. TV_MARGIN_RIGHT, TV_MARGIN_BOTTOM
  41. };
  42. /** Private structure for the integrated TV support */
  43. struct intel_tv {
  44. struct intel_encoder base;
  45. int type;
  46. };
  47. struct video_levels {
  48. u16 blank, black;
  49. u8 burst;
  50. };
  51. struct color_conversion {
  52. u16 ry, gy, by, ay;
  53. u16 ru, gu, bu, au;
  54. u16 rv, gv, bv, av;
  55. };
  56. static const u32 filter_table[] = {
  57. 0xB1403000, 0x2E203500, 0x35002E20, 0x3000B140,
  58. 0x35A0B160, 0x2DC02E80, 0xB1403480, 0xB1603000,
  59. 0x2EA03640, 0x34002D80, 0x3000B120, 0x36E0B160,
  60. 0x2D202EF0, 0xB1203380, 0xB1603000, 0x2F303780,
  61. 0x33002CC0, 0x3000B100, 0x3820B160, 0x2C802F50,
  62. 0xB10032A0, 0xB1603000, 0x2F9038C0, 0x32202C20,
  63. 0x3000B0E0, 0x3980B160, 0x2BC02FC0, 0xB0E031C0,
  64. 0xB1603000, 0x2FF03A20, 0x31602B60, 0xB020B0C0,
  65. 0x3AE0B160, 0x2B001810, 0xB0C03120, 0xB140B020,
  66. 0x18283BA0, 0x30C02A80, 0xB020B0A0, 0x3C60B140,
  67. 0x2A201838, 0xB0A03080, 0xB120B020, 0x18383D20,
  68. 0x304029C0, 0xB040B080, 0x3DE0B100, 0x29601848,
  69. 0xB0803000, 0xB100B040, 0x18483EC0, 0xB0402900,
  70. 0xB040B060, 0x3F80B0C0, 0x28801858, 0xB060B080,
  71. 0xB0A0B060, 0x18602820, 0xB0A02820, 0x0000B060,
  72. 0xB1403000, 0x2E203500, 0x35002E20, 0x3000B140,
  73. 0x35A0B160, 0x2DC02E80, 0xB1403480, 0xB1603000,
  74. 0x2EA03640, 0x34002D80, 0x3000B120, 0x36E0B160,
  75. 0x2D202EF0, 0xB1203380, 0xB1603000, 0x2F303780,
  76. 0x33002CC0, 0x3000B100, 0x3820B160, 0x2C802F50,
  77. 0xB10032A0, 0xB1603000, 0x2F9038C0, 0x32202C20,
  78. 0x3000B0E0, 0x3980B160, 0x2BC02FC0, 0xB0E031C0,
  79. 0xB1603000, 0x2FF03A20, 0x31602B60, 0xB020B0C0,
  80. 0x3AE0B160, 0x2B001810, 0xB0C03120, 0xB140B020,
  81. 0x18283BA0, 0x30C02A80, 0xB020B0A0, 0x3C60B140,
  82. 0x2A201838, 0xB0A03080, 0xB120B020, 0x18383D20,
  83. 0x304029C0, 0xB040B080, 0x3DE0B100, 0x29601848,
  84. 0xB0803000, 0xB100B040, 0x18483EC0, 0xB0402900,
  85. 0xB040B060, 0x3F80B0C0, 0x28801858, 0xB060B080,
  86. 0xB0A0B060, 0x18602820, 0xB0A02820, 0x0000B060,
  87. 0x36403000, 0x2D002CC0, 0x30003640, 0x2D0036C0,
  88. 0x35C02CC0, 0x37403000, 0x2C802D40, 0x30003540,
  89. 0x2D8037C0, 0x34C02C40, 0x38403000, 0x2BC02E00,
  90. 0x30003440, 0x2E2038C0, 0x34002B80, 0x39803000,
  91. 0x2B402E40, 0x30003380, 0x2E603A00, 0x33402B00,
  92. 0x3A803040, 0x2A802EA0, 0x30403300, 0x2EC03B40,
  93. 0x32802A40, 0x3C003040, 0x2A002EC0, 0x30803240,
  94. 0x2EC03C80, 0x320029C0, 0x3D403080, 0x29402F00,
  95. 0x308031C0, 0x2F203DC0, 0x31802900, 0x3E8030C0,
  96. 0x28802F40, 0x30C03140, 0x2F203F40, 0x31402840,
  97. 0x28003100, 0x28002F00, 0x00003100, 0x36403000,
  98. 0x2D002CC0, 0x30003640, 0x2D0036C0,
  99. 0x35C02CC0, 0x37403000, 0x2C802D40, 0x30003540,
  100. 0x2D8037C0, 0x34C02C40, 0x38403000, 0x2BC02E00,
  101. 0x30003440, 0x2E2038C0, 0x34002B80, 0x39803000,
  102. 0x2B402E40, 0x30003380, 0x2E603A00, 0x33402B00,
  103. 0x3A803040, 0x2A802EA0, 0x30403300, 0x2EC03B40,
  104. 0x32802A40, 0x3C003040, 0x2A002EC0, 0x30803240,
  105. 0x2EC03C80, 0x320029C0, 0x3D403080, 0x29402F00,
  106. 0x308031C0, 0x2F203DC0, 0x31802900, 0x3E8030C0,
  107. 0x28802F40, 0x30C03140, 0x2F203F40, 0x31402840,
  108. 0x28003100, 0x28002F00, 0x00003100,
  109. };
  110. /*
  111. * Color conversion values have 3 separate fixed point formats:
  112. *
  113. * 10 bit fields (ay, au)
  114. * 1.9 fixed point (b.bbbbbbbbb)
  115. * 11 bit fields (ry, by, ru, gu, gv)
  116. * exp.mantissa (ee.mmmmmmmmm)
  117. * ee = 00 = 10^-1 (0.mmmmmmmmm)
  118. * ee = 01 = 10^-2 (0.0mmmmmmmmm)
  119. * ee = 10 = 10^-3 (0.00mmmmmmmmm)
  120. * ee = 11 = 10^-4 (0.000mmmmmmmmm)
  121. * 12 bit fields (gy, rv, bu)
  122. * exp.mantissa (eee.mmmmmmmmm)
  123. * eee = 000 = 10^-1 (0.mmmmmmmmm)
  124. * eee = 001 = 10^-2 (0.0mmmmmmmmm)
  125. * eee = 010 = 10^-3 (0.00mmmmmmmmm)
  126. * eee = 011 = 10^-4 (0.000mmmmmmmmm)
  127. * eee = 100 = reserved
  128. * eee = 101 = reserved
  129. * eee = 110 = reserved
  130. * eee = 111 = 10^0 (m.mmmmmmmm) (only usable for 1.0 representation)
  131. *
  132. * Saturation and contrast are 8 bits, with their own representation:
  133. * 8 bit field (saturation, contrast)
  134. * exp.mantissa (ee.mmmmmm)
  135. * ee = 00 = 10^-1 (0.mmmmmm)
  136. * ee = 01 = 10^0 (m.mmmmm)
  137. * ee = 10 = 10^1 (mm.mmmm)
  138. * ee = 11 = 10^2 (mmm.mmm)
  139. *
  140. * Simple conversion function:
  141. *
  142. * static u32
  143. * float_to_csc_11(float f)
  144. * {
  145. * u32 exp;
  146. * u32 mant;
  147. * u32 ret;
  148. *
  149. * if (f < 0)
  150. * f = -f;
  151. *
  152. * if (f >= 1) {
  153. * exp = 0x7;
  154. * mant = 1 << 8;
  155. * } else {
  156. * for (exp = 0; exp < 3 && f < 0.5; exp++)
  157. * f *= 2.0;
  158. * mant = (f * (1 << 9) + 0.5);
  159. * if (mant >= (1 << 9))
  160. * mant = (1 << 9) - 1;
  161. * }
  162. * ret = (exp << 9) | mant;
  163. * return ret;
  164. * }
  165. */
  166. /*
  167. * Behold, magic numbers! If we plant them they might grow a big
  168. * s-video cable to the sky... or something.
  169. *
  170. * Pre-converted to appropriate hex value.
  171. */
  172. /*
  173. * PAL & NTSC values for composite & s-video connections
  174. */
  175. static const struct color_conversion ntsc_m_csc_composite = {
  176. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
  177. .ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
  178. .rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
  179. };
  180. static const struct video_levels ntsc_m_levels_composite = {
  181. .blank = 225, .black = 267, .burst = 113,
  182. };
  183. static const struct color_conversion ntsc_m_csc_svideo = {
  184. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
  185. .ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
  186. .rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
  187. };
  188. static const struct video_levels ntsc_m_levels_svideo = {
  189. .blank = 266, .black = 316, .burst = 133,
  190. };
  191. static const struct color_conversion ntsc_j_csc_composite = {
  192. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0119,
  193. .ru = 0x074c, .gu = 0x0546, .bu = 0x05ec, .au = 0x0200,
  194. .rv = 0x035a, .gv = 0x0322, .bv = 0x06e1, .av = 0x0200,
  195. };
  196. static const struct video_levels ntsc_j_levels_composite = {
  197. .blank = 225, .black = 225, .burst = 113,
  198. };
  199. static const struct color_conversion ntsc_j_csc_svideo = {
  200. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x014c,
  201. .ru = 0x0788, .gu = 0x0581, .bu = 0x0322, .au = 0x0200,
  202. .rv = 0x0399, .gv = 0x0356, .bv = 0x070a, .av = 0x0200,
  203. };
  204. static const struct video_levels ntsc_j_levels_svideo = {
  205. .blank = 266, .black = 266, .burst = 133,
  206. };
  207. static const struct color_conversion pal_csc_composite = {
  208. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0113,
  209. .ru = 0x0745, .gu = 0x053f, .bu = 0x05e1, .au = 0x0200,
  210. .rv = 0x0353, .gv = 0x031c, .bv = 0x06dc, .av = 0x0200,
  211. };
  212. static const struct video_levels pal_levels_composite = {
  213. .blank = 237, .black = 237, .burst = 118,
  214. };
  215. static const struct color_conversion pal_csc_svideo = {
  216. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0145,
  217. .ru = 0x0780, .gu = 0x0579, .bu = 0x031c, .au = 0x0200,
  218. .rv = 0x0390, .gv = 0x034f, .bv = 0x0705, .av = 0x0200,
  219. };
  220. static const struct video_levels pal_levels_svideo = {
  221. .blank = 280, .black = 280, .burst = 139,
  222. };
  223. static const struct color_conversion pal_m_csc_composite = {
  224. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
  225. .ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
  226. .rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
  227. };
  228. static const struct video_levels pal_m_levels_composite = {
  229. .blank = 225, .black = 267, .burst = 113,
  230. };
  231. static const struct color_conversion pal_m_csc_svideo = {
  232. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
  233. .ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
  234. .rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
  235. };
  236. static const struct video_levels pal_m_levels_svideo = {
  237. .blank = 266, .black = 316, .burst = 133,
  238. };
  239. static const struct color_conversion pal_n_csc_composite = {
  240. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
  241. .ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
  242. .rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
  243. };
  244. static const struct video_levels pal_n_levels_composite = {
  245. .blank = 225, .black = 267, .burst = 118,
  246. };
  247. static const struct color_conversion pal_n_csc_svideo = {
  248. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
  249. .ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
  250. .rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
  251. };
  252. static const struct video_levels pal_n_levels_svideo = {
  253. .blank = 266, .black = 316, .burst = 139,
  254. };
  255. /*
  256. * Component connections
  257. */
  258. static const struct color_conversion sdtv_csc_yprpb = {
  259. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0145,
  260. .ru = 0x0559, .gu = 0x0353, .bu = 0x0100, .au = 0x0200,
  261. .rv = 0x0100, .gv = 0x03ad, .bv = 0x074d, .av = 0x0200,
  262. };
  263. static const struct color_conversion hdtv_csc_yprpb = {
  264. .ry = 0x05b3, .gy = 0x016e, .by = 0x0728, .ay = 0x0145,
  265. .ru = 0x07d5, .gu = 0x038b, .bu = 0x0100, .au = 0x0200,
  266. .rv = 0x0100, .gv = 0x03d1, .bv = 0x06bc, .av = 0x0200,
  267. };
  268. static const struct video_levels component_levels = {
  269. .blank = 279, .black = 279, .burst = 0,
  270. };
  271. struct tv_mode {
  272. const char *name;
  273. u32 clock;
  274. u16 refresh; /* in millihertz (for precision) */
  275. u32 oversample;
  276. u8 hsync_end;
  277. u16 hblank_start, hblank_end, htotal;
  278. bool progressive : 1, trilevel_sync : 1, component_only : 1;
  279. u8 vsync_start_f1, vsync_start_f2, vsync_len;
  280. bool veq_ena : 1;
  281. u8 veq_start_f1, veq_start_f2, veq_len;
  282. u8 vi_end_f1, vi_end_f2;
  283. u16 nbr_end;
  284. bool burst_ena : 1;
  285. u8 hburst_start, hburst_len;
  286. u8 vburst_start_f1;
  287. u16 vburst_end_f1;
  288. u8 vburst_start_f2;
  289. u16 vburst_end_f2;
  290. u8 vburst_start_f3;
  291. u16 vburst_end_f3;
  292. u8 vburst_start_f4;
  293. u16 vburst_end_f4;
  294. /*
  295. * subcarrier programming
  296. */
  297. u16 dda2_size, dda3_size;
  298. u8 dda1_inc;
  299. u16 dda2_inc, dda3_inc;
  300. u32 sc_reset;
  301. bool pal_burst : 1;
  302. /*
  303. * blank/black levels
  304. */
  305. const struct video_levels *composite_levels, *svideo_levels;
  306. const struct color_conversion *composite_color, *svideo_color;
  307. const u32 *filter_table;
  308. u16 max_srcw;
  309. };
  310. /*
  311. * Sub carrier DDA
  312. *
  313. * I think this works as follows:
  314. *
  315. * subcarrier freq = pixel_clock * (dda1_inc + dda2_inc / dda2_size) / 4096
  316. *
  317. * Presumably, when dda3 is added in, it gets to adjust the dda2_inc value
  318. *
  319. * So,
  320. * dda1_ideal = subcarrier/pixel * 4096
  321. * dda1_inc = floor (dda1_ideal)
  322. * dda2 = dda1_ideal - dda1_inc
  323. *
  324. * then pick a ratio for dda2 that gives the closest approximation. If
  325. * you can't get close enough, you can play with dda3 as well. This
  326. * seems likely to happen when dda2 is small as the jumps would be larger
  327. *
  328. * To invert this,
  329. *
  330. * pixel_clock = subcarrier * 4096 / (dda1_inc + dda2_inc / dda2_size)
  331. *
  332. * The constants below were all computed using a 107.520MHz clock
  333. */
  334. /**
  335. * Register programming values for TV modes.
  336. *
  337. * These values account for -1s required.
  338. */
  339. static const struct tv_mode tv_modes[] = {
  340. {
  341. .name = "NTSC-M",
  342. .clock = 108000,
  343. .refresh = 59940,
  344. .oversample = TV_OVERSAMPLE_8X,
  345. .component_only = 0,
  346. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
  347. .hsync_end = 64, .hblank_end = 124,
  348. .hblank_start = 836, .htotal = 857,
  349. .progressive = false, .trilevel_sync = false,
  350. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  351. .vsync_len = 6,
  352. .veq_ena = true, .veq_start_f1 = 0,
  353. .veq_start_f2 = 1, .veq_len = 18,
  354. .vi_end_f1 = 20, .vi_end_f2 = 21,
  355. .nbr_end = 240,
  356. .burst_ena = true,
  357. .hburst_start = 72, .hburst_len = 34,
  358. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  359. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  360. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  361. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  362. /* desired 3.5800000 actual 3.5800000 clock 107.52 */
  363. .dda1_inc = 135,
  364. .dda2_inc = 20800, .dda2_size = 27456,
  365. .dda3_inc = 0, .dda3_size = 0,
  366. .sc_reset = TV_SC_RESET_EVERY_4,
  367. .pal_burst = false,
  368. .composite_levels = &ntsc_m_levels_composite,
  369. .composite_color = &ntsc_m_csc_composite,
  370. .svideo_levels = &ntsc_m_levels_svideo,
  371. .svideo_color = &ntsc_m_csc_svideo,
  372. .filter_table = filter_table,
  373. },
  374. {
  375. .name = "NTSC-443",
  376. .clock = 108000,
  377. .refresh = 59940,
  378. .oversample = TV_OVERSAMPLE_8X,
  379. .component_only = 0,
  380. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 4.43MHz */
  381. .hsync_end = 64, .hblank_end = 124,
  382. .hblank_start = 836, .htotal = 857,
  383. .progressive = false, .trilevel_sync = false,
  384. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  385. .vsync_len = 6,
  386. .veq_ena = true, .veq_start_f1 = 0,
  387. .veq_start_f2 = 1, .veq_len = 18,
  388. .vi_end_f1 = 20, .vi_end_f2 = 21,
  389. .nbr_end = 240,
  390. .burst_ena = true,
  391. .hburst_start = 72, .hburst_len = 34,
  392. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  393. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  394. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  395. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  396. /* desired 4.4336180 actual 4.4336180 clock 107.52 */
  397. .dda1_inc = 168,
  398. .dda2_inc = 4093, .dda2_size = 27456,
  399. .dda3_inc = 310, .dda3_size = 525,
  400. .sc_reset = TV_SC_RESET_NEVER,
  401. .pal_burst = false,
  402. .composite_levels = &ntsc_m_levels_composite,
  403. .composite_color = &ntsc_m_csc_composite,
  404. .svideo_levels = &ntsc_m_levels_svideo,
  405. .svideo_color = &ntsc_m_csc_svideo,
  406. .filter_table = filter_table,
  407. },
  408. {
  409. .name = "NTSC-J",
  410. .clock = 108000,
  411. .refresh = 59940,
  412. .oversample = TV_OVERSAMPLE_8X,
  413. .component_only = 0,
  414. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
  415. .hsync_end = 64, .hblank_end = 124,
  416. .hblank_start = 836, .htotal = 857,
  417. .progressive = false, .trilevel_sync = false,
  418. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  419. .vsync_len = 6,
  420. .veq_ena = true, .veq_start_f1 = 0,
  421. .veq_start_f2 = 1, .veq_len = 18,
  422. .vi_end_f1 = 20, .vi_end_f2 = 21,
  423. .nbr_end = 240,
  424. .burst_ena = true,
  425. .hburst_start = 72, .hburst_len = 34,
  426. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  427. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  428. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  429. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  430. /* desired 3.5800000 actual 3.5800000 clock 107.52 */
  431. .dda1_inc = 135,
  432. .dda2_inc = 20800, .dda2_size = 27456,
  433. .dda3_inc = 0, .dda3_size = 0,
  434. .sc_reset = TV_SC_RESET_EVERY_4,
  435. .pal_burst = false,
  436. .composite_levels = &ntsc_j_levels_composite,
  437. .composite_color = &ntsc_j_csc_composite,
  438. .svideo_levels = &ntsc_j_levels_svideo,
  439. .svideo_color = &ntsc_j_csc_svideo,
  440. .filter_table = filter_table,
  441. },
  442. {
  443. .name = "PAL-M",
  444. .clock = 108000,
  445. .refresh = 59940,
  446. .oversample = TV_OVERSAMPLE_8X,
  447. .component_only = 0,
  448. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
  449. .hsync_end = 64, .hblank_end = 124,
  450. .hblank_start = 836, .htotal = 857,
  451. .progressive = false, .trilevel_sync = false,
  452. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  453. .vsync_len = 6,
  454. .veq_ena = true, .veq_start_f1 = 0,
  455. .veq_start_f2 = 1, .veq_len = 18,
  456. .vi_end_f1 = 20, .vi_end_f2 = 21,
  457. .nbr_end = 240,
  458. .burst_ena = true,
  459. .hburst_start = 72, .hburst_len = 34,
  460. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  461. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  462. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  463. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  464. /* desired 3.5800000 actual 3.5800000 clock 107.52 */
  465. .dda1_inc = 135,
  466. .dda2_inc = 16704, .dda2_size = 27456,
  467. .dda3_inc = 0, .dda3_size = 0,
  468. .sc_reset = TV_SC_RESET_EVERY_8,
  469. .pal_burst = true,
  470. .composite_levels = &pal_m_levels_composite,
  471. .composite_color = &pal_m_csc_composite,
  472. .svideo_levels = &pal_m_levels_svideo,
  473. .svideo_color = &pal_m_csc_svideo,
  474. .filter_table = filter_table,
  475. },
  476. {
  477. /* 625 Lines, 50 Fields, 15.625KHz line, Sub-Carrier 4.434MHz */
  478. .name = "PAL-N",
  479. .clock = 108000,
  480. .refresh = 50000,
  481. .oversample = TV_OVERSAMPLE_8X,
  482. .component_only = 0,
  483. .hsync_end = 64, .hblank_end = 128,
  484. .hblank_start = 844, .htotal = 863,
  485. .progressive = false, .trilevel_sync = false,
  486. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  487. .vsync_len = 6,
  488. .veq_ena = true, .veq_start_f1 = 0,
  489. .veq_start_f2 = 1, .veq_len = 18,
  490. .vi_end_f1 = 24, .vi_end_f2 = 25,
  491. .nbr_end = 286,
  492. .burst_ena = true,
  493. .hburst_start = 73, .hburst_len = 34,
  494. .vburst_start_f1 = 8, .vburst_end_f1 = 285,
  495. .vburst_start_f2 = 8, .vburst_end_f2 = 286,
  496. .vburst_start_f3 = 9, .vburst_end_f3 = 286,
  497. .vburst_start_f4 = 9, .vburst_end_f4 = 285,
  498. /* desired 4.4336180 actual 4.4336180 clock 107.52 */
  499. .dda1_inc = 135,
  500. .dda2_inc = 23578, .dda2_size = 27648,
  501. .dda3_inc = 134, .dda3_size = 625,
  502. .sc_reset = TV_SC_RESET_EVERY_8,
  503. .pal_burst = true,
  504. .composite_levels = &pal_n_levels_composite,
  505. .composite_color = &pal_n_csc_composite,
  506. .svideo_levels = &pal_n_levels_svideo,
  507. .svideo_color = &pal_n_csc_svideo,
  508. .filter_table = filter_table,
  509. },
  510. {
  511. /* 625 Lines, 50 Fields, 15.625KHz line, Sub-Carrier 4.434MHz */
  512. .name = "PAL",
  513. .clock = 108000,
  514. .refresh = 50000,
  515. .oversample = TV_OVERSAMPLE_8X,
  516. .component_only = 0,
  517. .hsync_end = 64, .hblank_end = 142,
  518. .hblank_start = 844, .htotal = 863,
  519. .progressive = false, .trilevel_sync = false,
  520. .vsync_start_f1 = 5, .vsync_start_f2 = 6,
  521. .vsync_len = 5,
  522. .veq_ena = true, .veq_start_f1 = 0,
  523. .veq_start_f2 = 1, .veq_len = 15,
  524. .vi_end_f1 = 24, .vi_end_f2 = 25,
  525. .nbr_end = 286,
  526. .burst_ena = true,
  527. .hburst_start = 73, .hburst_len = 32,
  528. .vburst_start_f1 = 8, .vburst_end_f1 = 285,
  529. .vburst_start_f2 = 8, .vburst_end_f2 = 286,
  530. .vburst_start_f3 = 9, .vburst_end_f3 = 286,
  531. .vburst_start_f4 = 9, .vburst_end_f4 = 285,
  532. /* desired 4.4336180 actual 4.4336180 clock 107.52 */
  533. .dda1_inc = 168,
  534. .dda2_inc = 4122, .dda2_size = 27648,
  535. .dda3_inc = 67, .dda3_size = 625,
  536. .sc_reset = TV_SC_RESET_EVERY_8,
  537. .pal_burst = true,
  538. .composite_levels = &pal_levels_composite,
  539. .composite_color = &pal_csc_composite,
  540. .svideo_levels = &pal_levels_svideo,
  541. .svideo_color = &pal_csc_svideo,
  542. .filter_table = filter_table,
  543. },
  544. {
  545. .name = "480p",
  546. .clock = 107520,
  547. .refresh = 59940,
  548. .oversample = TV_OVERSAMPLE_4X,
  549. .component_only = 1,
  550. .hsync_end = 64, .hblank_end = 122,
  551. .hblank_start = 842, .htotal = 857,
  552. .progressive = true, .trilevel_sync = false,
  553. .vsync_start_f1 = 12, .vsync_start_f2 = 12,
  554. .vsync_len = 12,
  555. .veq_ena = false,
  556. .vi_end_f1 = 44, .vi_end_f2 = 44,
  557. .nbr_end = 479,
  558. .burst_ena = false,
  559. .filter_table = filter_table,
  560. },
  561. {
  562. .name = "576p",
  563. .clock = 107520,
  564. .refresh = 50000,
  565. .oversample = TV_OVERSAMPLE_4X,
  566. .component_only = 1,
  567. .hsync_end = 64, .hblank_end = 139,
  568. .hblank_start = 859, .htotal = 863,
  569. .progressive = true, .trilevel_sync = false,
  570. .vsync_start_f1 = 10, .vsync_start_f2 = 10,
  571. .vsync_len = 10,
  572. .veq_ena = false,
  573. .vi_end_f1 = 48, .vi_end_f2 = 48,
  574. .nbr_end = 575,
  575. .burst_ena = false,
  576. .filter_table = filter_table,
  577. },
  578. {
  579. .name = "720p@60Hz",
  580. .clock = 148800,
  581. .refresh = 60000,
  582. .oversample = TV_OVERSAMPLE_2X,
  583. .component_only = 1,
  584. .hsync_end = 80, .hblank_end = 300,
  585. .hblank_start = 1580, .htotal = 1649,
  586. .progressive = true, .trilevel_sync = true,
  587. .vsync_start_f1 = 10, .vsync_start_f2 = 10,
  588. .vsync_len = 10,
  589. .veq_ena = false,
  590. .vi_end_f1 = 29, .vi_end_f2 = 29,
  591. .nbr_end = 719,
  592. .burst_ena = false,
  593. .filter_table = filter_table,
  594. },
  595. {
  596. .name = "720p@50Hz",
  597. .clock = 148800,
  598. .refresh = 50000,
  599. .oversample = TV_OVERSAMPLE_2X,
  600. .component_only = 1,
  601. .hsync_end = 80, .hblank_end = 300,
  602. .hblank_start = 1580, .htotal = 1979,
  603. .progressive = true, .trilevel_sync = true,
  604. .vsync_start_f1 = 10, .vsync_start_f2 = 10,
  605. .vsync_len = 10,
  606. .veq_ena = false,
  607. .vi_end_f1 = 29, .vi_end_f2 = 29,
  608. .nbr_end = 719,
  609. .burst_ena = false,
  610. .filter_table = filter_table,
  611. .max_srcw = 800
  612. },
  613. {
  614. .name = "1080i@50Hz",
  615. .clock = 148800,
  616. .refresh = 50000,
  617. .oversample = TV_OVERSAMPLE_2X,
  618. .component_only = 1,
  619. .hsync_end = 88, .hblank_end = 235,
  620. .hblank_start = 2155, .htotal = 2639,
  621. .progressive = false, .trilevel_sync = true,
  622. .vsync_start_f1 = 4, .vsync_start_f2 = 5,
  623. .vsync_len = 10,
  624. .veq_ena = true, .veq_start_f1 = 4,
  625. .veq_start_f2 = 4, .veq_len = 10,
  626. .vi_end_f1 = 21, .vi_end_f2 = 22,
  627. .nbr_end = 539,
  628. .burst_ena = false,
  629. .filter_table = filter_table,
  630. },
  631. {
  632. .name = "1080i@60Hz",
  633. .clock = 148800,
  634. .refresh = 60000,
  635. .oversample = TV_OVERSAMPLE_2X,
  636. .component_only = 1,
  637. .hsync_end = 88, .hblank_end = 235,
  638. .hblank_start = 2155, .htotal = 2199,
  639. .progressive = false, .trilevel_sync = true,
  640. .vsync_start_f1 = 4, .vsync_start_f2 = 5,
  641. .vsync_len = 10,
  642. .veq_ena = true, .veq_start_f1 = 4,
  643. .veq_start_f2 = 4, .veq_len = 10,
  644. .vi_end_f1 = 21, .vi_end_f2 = 22,
  645. .nbr_end = 539,
  646. .burst_ena = false,
  647. .filter_table = filter_table,
  648. },
  649. };
  650. static struct intel_tv *enc_to_tv(struct intel_encoder *encoder)
  651. {
  652. return container_of(encoder, struct intel_tv, base);
  653. }
  654. static struct intel_tv *intel_attached_tv(struct drm_connector *connector)
  655. {
  656. return enc_to_tv(intel_attached_encoder(connector));
  657. }
  658. static bool
  659. intel_tv_get_hw_state(struct intel_encoder *encoder, enum pipe *pipe)
  660. {
  661. struct drm_device *dev = encoder->base.dev;
  662. struct drm_i915_private *dev_priv = to_i915(dev);
  663. u32 tmp = I915_READ(TV_CTL);
  664. if (!(tmp & TV_ENC_ENABLE))
  665. return false;
  666. *pipe = PORT_TO_PIPE(tmp);
  667. return true;
  668. }
  669. static void
  670. intel_enable_tv(struct intel_encoder *encoder,
  671. struct intel_crtc_state *pipe_config,
  672. struct drm_connector_state *conn_state)
  673. {
  674. struct drm_device *dev = encoder->base.dev;
  675. struct drm_i915_private *dev_priv = to_i915(dev);
  676. /* Prevents vblank waits from timing out in intel_tv_detect_type() */
  677. intel_wait_for_vblank(dev_priv,
  678. to_intel_crtc(encoder->base.crtc)->pipe);
  679. I915_WRITE(TV_CTL, I915_READ(TV_CTL) | TV_ENC_ENABLE);
  680. }
  681. static void
  682. intel_disable_tv(struct intel_encoder *encoder,
  683. struct intel_crtc_state *old_crtc_state,
  684. struct drm_connector_state *old_conn_state)
  685. {
  686. struct drm_device *dev = encoder->base.dev;
  687. struct drm_i915_private *dev_priv = to_i915(dev);
  688. I915_WRITE(TV_CTL, I915_READ(TV_CTL) & ~TV_ENC_ENABLE);
  689. }
  690. static const struct tv_mode *intel_tv_mode_find(struct drm_connector_state *conn_state)
  691. {
  692. int format = conn_state->tv.mode;
  693. return &tv_modes[format];
  694. }
  695. static enum drm_mode_status
  696. intel_tv_mode_valid(struct drm_connector *connector,
  697. struct drm_display_mode *mode)
  698. {
  699. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  700. int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
  701. if (mode->clock > max_dotclk)
  702. return MODE_CLOCK_HIGH;
  703. /* Ensure TV refresh is close to desired refresh */
  704. if (tv_mode && abs(tv_mode->refresh - drm_mode_vrefresh(mode) * 1000)
  705. < 1000)
  706. return MODE_OK;
  707. return MODE_CLOCK_RANGE;
  708. }
  709. static void
  710. intel_tv_get_config(struct intel_encoder *encoder,
  711. struct intel_crtc_state *pipe_config)
  712. {
  713. pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
  714. }
  715. static bool
  716. intel_tv_compute_config(struct intel_encoder *encoder,
  717. struct intel_crtc_state *pipe_config,
  718. struct drm_connector_state *conn_state)
  719. {
  720. const struct tv_mode *tv_mode = intel_tv_mode_find(conn_state);
  721. if (!tv_mode)
  722. return false;
  723. pipe_config->base.adjusted_mode.crtc_clock = tv_mode->clock;
  724. DRM_DEBUG_KMS("forcing bpc to 8 for TV\n");
  725. pipe_config->pipe_bpp = 8*3;
  726. /* TV has it's own notion of sync and other mode flags, so clear them. */
  727. pipe_config->base.adjusted_mode.flags = 0;
  728. /*
  729. * FIXME: We don't check whether the input mode is actually what we want
  730. * or whether userspace is doing something stupid.
  731. */
  732. return true;
  733. }
  734. static void
  735. set_tv_mode_timings(struct drm_i915_private *dev_priv,
  736. const struct tv_mode *tv_mode,
  737. bool burst_ena)
  738. {
  739. u32 hctl1, hctl2, hctl3;
  740. u32 vctl1, vctl2, vctl3, vctl4, vctl5, vctl6, vctl7;
  741. hctl1 = (tv_mode->hsync_end << TV_HSYNC_END_SHIFT) |
  742. (tv_mode->htotal << TV_HTOTAL_SHIFT);
  743. hctl2 = (tv_mode->hburst_start << 16) |
  744. (tv_mode->hburst_len << TV_HBURST_LEN_SHIFT);
  745. if (burst_ena)
  746. hctl2 |= TV_BURST_ENA;
  747. hctl3 = (tv_mode->hblank_start << TV_HBLANK_START_SHIFT) |
  748. (tv_mode->hblank_end << TV_HBLANK_END_SHIFT);
  749. vctl1 = (tv_mode->nbr_end << TV_NBR_END_SHIFT) |
  750. (tv_mode->vi_end_f1 << TV_VI_END_F1_SHIFT) |
  751. (tv_mode->vi_end_f2 << TV_VI_END_F2_SHIFT);
  752. vctl2 = (tv_mode->vsync_len << TV_VSYNC_LEN_SHIFT) |
  753. (tv_mode->vsync_start_f1 << TV_VSYNC_START_F1_SHIFT) |
  754. (tv_mode->vsync_start_f2 << TV_VSYNC_START_F2_SHIFT);
  755. vctl3 = (tv_mode->veq_len << TV_VEQ_LEN_SHIFT) |
  756. (tv_mode->veq_start_f1 << TV_VEQ_START_F1_SHIFT) |
  757. (tv_mode->veq_start_f2 << TV_VEQ_START_F2_SHIFT);
  758. if (tv_mode->veq_ena)
  759. vctl3 |= TV_EQUAL_ENA;
  760. vctl4 = (tv_mode->vburst_start_f1 << TV_VBURST_START_F1_SHIFT) |
  761. (tv_mode->vburst_end_f1 << TV_VBURST_END_F1_SHIFT);
  762. vctl5 = (tv_mode->vburst_start_f2 << TV_VBURST_START_F2_SHIFT) |
  763. (tv_mode->vburst_end_f2 << TV_VBURST_END_F2_SHIFT);
  764. vctl6 = (tv_mode->vburst_start_f3 << TV_VBURST_START_F3_SHIFT) |
  765. (tv_mode->vburst_end_f3 << TV_VBURST_END_F3_SHIFT);
  766. vctl7 = (tv_mode->vburst_start_f4 << TV_VBURST_START_F4_SHIFT) |
  767. (tv_mode->vburst_end_f4 << TV_VBURST_END_F4_SHIFT);
  768. I915_WRITE(TV_H_CTL_1, hctl1);
  769. I915_WRITE(TV_H_CTL_2, hctl2);
  770. I915_WRITE(TV_H_CTL_3, hctl3);
  771. I915_WRITE(TV_V_CTL_1, vctl1);
  772. I915_WRITE(TV_V_CTL_2, vctl2);
  773. I915_WRITE(TV_V_CTL_3, vctl3);
  774. I915_WRITE(TV_V_CTL_4, vctl4);
  775. I915_WRITE(TV_V_CTL_5, vctl5);
  776. I915_WRITE(TV_V_CTL_6, vctl6);
  777. I915_WRITE(TV_V_CTL_7, vctl7);
  778. }
  779. static void set_color_conversion(struct drm_i915_private *dev_priv,
  780. const struct color_conversion *color_conversion)
  781. {
  782. if (!color_conversion)
  783. return;
  784. I915_WRITE(TV_CSC_Y, (color_conversion->ry << 16) |
  785. color_conversion->gy);
  786. I915_WRITE(TV_CSC_Y2, (color_conversion->by << 16) |
  787. color_conversion->ay);
  788. I915_WRITE(TV_CSC_U, (color_conversion->ru << 16) |
  789. color_conversion->gu);
  790. I915_WRITE(TV_CSC_U2, (color_conversion->bu << 16) |
  791. color_conversion->au);
  792. I915_WRITE(TV_CSC_V, (color_conversion->rv << 16) |
  793. color_conversion->gv);
  794. I915_WRITE(TV_CSC_V2, (color_conversion->bv << 16) |
  795. color_conversion->av);
  796. }
  797. static void intel_tv_pre_enable(struct intel_encoder *encoder,
  798. struct intel_crtc_state *pipe_config,
  799. struct drm_connector_state *conn_state)
  800. {
  801. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  802. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  803. struct intel_tv *intel_tv = enc_to_tv(encoder);
  804. const struct tv_mode *tv_mode = intel_tv_mode_find(conn_state);
  805. u32 tv_ctl;
  806. u32 scctl1, scctl2, scctl3;
  807. int i, j;
  808. const struct video_levels *video_levels;
  809. const struct color_conversion *color_conversion;
  810. bool burst_ena;
  811. int xpos = 0x0, ypos = 0x0;
  812. unsigned int xsize, ysize;
  813. if (!tv_mode)
  814. return; /* can't happen (mode_prepare prevents this) */
  815. tv_ctl = I915_READ(TV_CTL);
  816. tv_ctl &= TV_CTL_SAVE;
  817. switch (intel_tv->type) {
  818. default:
  819. case DRM_MODE_CONNECTOR_Unknown:
  820. case DRM_MODE_CONNECTOR_Composite:
  821. tv_ctl |= TV_ENC_OUTPUT_COMPOSITE;
  822. video_levels = tv_mode->composite_levels;
  823. color_conversion = tv_mode->composite_color;
  824. burst_ena = tv_mode->burst_ena;
  825. break;
  826. case DRM_MODE_CONNECTOR_Component:
  827. tv_ctl |= TV_ENC_OUTPUT_COMPONENT;
  828. video_levels = &component_levels;
  829. if (tv_mode->burst_ena)
  830. color_conversion = &sdtv_csc_yprpb;
  831. else
  832. color_conversion = &hdtv_csc_yprpb;
  833. burst_ena = false;
  834. break;
  835. case DRM_MODE_CONNECTOR_SVIDEO:
  836. tv_ctl |= TV_ENC_OUTPUT_SVIDEO;
  837. video_levels = tv_mode->svideo_levels;
  838. color_conversion = tv_mode->svideo_color;
  839. burst_ena = tv_mode->burst_ena;
  840. break;
  841. }
  842. if (intel_crtc->pipe == 1)
  843. tv_ctl |= TV_ENC_PIPEB_SELECT;
  844. tv_ctl |= tv_mode->oversample;
  845. if (tv_mode->progressive)
  846. tv_ctl |= TV_PROGRESSIVE;
  847. if (tv_mode->trilevel_sync)
  848. tv_ctl |= TV_TRILEVEL_SYNC;
  849. if (tv_mode->pal_burst)
  850. tv_ctl |= TV_PAL_BURST;
  851. scctl1 = 0;
  852. if (tv_mode->dda1_inc)
  853. scctl1 |= TV_SC_DDA1_EN;
  854. if (tv_mode->dda2_inc)
  855. scctl1 |= TV_SC_DDA2_EN;
  856. if (tv_mode->dda3_inc)
  857. scctl1 |= TV_SC_DDA3_EN;
  858. scctl1 |= tv_mode->sc_reset;
  859. if (video_levels)
  860. scctl1 |= video_levels->burst << TV_BURST_LEVEL_SHIFT;
  861. scctl1 |= tv_mode->dda1_inc << TV_SCDDA1_INC_SHIFT;
  862. scctl2 = tv_mode->dda2_size << TV_SCDDA2_SIZE_SHIFT |
  863. tv_mode->dda2_inc << TV_SCDDA2_INC_SHIFT;
  864. scctl3 = tv_mode->dda3_size << TV_SCDDA3_SIZE_SHIFT |
  865. tv_mode->dda3_inc << TV_SCDDA3_INC_SHIFT;
  866. /* Enable two fixes for the chips that need them. */
  867. if (IS_I915GM(dev_priv))
  868. tv_ctl |= TV_ENC_C0_FIX | TV_ENC_SDP_FIX;
  869. set_tv_mode_timings(dev_priv, tv_mode, burst_ena);
  870. I915_WRITE(TV_SC_CTL_1, scctl1);
  871. I915_WRITE(TV_SC_CTL_2, scctl2);
  872. I915_WRITE(TV_SC_CTL_3, scctl3);
  873. set_color_conversion(dev_priv, color_conversion);
  874. if (INTEL_GEN(dev_priv) >= 4)
  875. I915_WRITE(TV_CLR_KNOBS, 0x00404000);
  876. else
  877. I915_WRITE(TV_CLR_KNOBS, 0x00606000);
  878. if (video_levels)
  879. I915_WRITE(TV_CLR_LEVEL,
  880. ((video_levels->black << TV_BLACK_LEVEL_SHIFT) |
  881. (video_levels->blank << TV_BLANK_LEVEL_SHIFT)));
  882. assert_pipe_disabled(dev_priv, intel_crtc->pipe);
  883. /* Filter ctl must be set before TV_WIN_SIZE */
  884. I915_WRITE(TV_FILTER_CTL_1, TV_AUTO_SCALE);
  885. xsize = tv_mode->hblank_start - tv_mode->hblank_end;
  886. if (tv_mode->progressive)
  887. ysize = tv_mode->nbr_end + 1;
  888. else
  889. ysize = 2*tv_mode->nbr_end + 1;
  890. xpos += conn_state->tv.margins.left;
  891. ypos += conn_state->tv.margins.top;
  892. xsize -= (conn_state->tv.margins.left +
  893. conn_state->tv.margins.right);
  894. ysize -= (conn_state->tv.margins.top +
  895. conn_state->tv.margins.bottom);
  896. I915_WRITE(TV_WIN_POS, (xpos<<16)|ypos);
  897. I915_WRITE(TV_WIN_SIZE, (xsize<<16)|ysize);
  898. j = 0;
  899. for (i = 0; i < 60; i++)
  900. I915_WRITE(TV_H_LUMA(i), tv_mode->filter_table[j++]);
  901. for (i = 0; i < 60; i++)
  902. I915_WRITE(TV_H_CHROMA(i), tv_mode->filter_table[j++]);
  903. for (i = 0; i < 43; i++)
  904. I915_WRITE(TV_V_LUMA(i), tv_mode->filter_table[j++]);
  905. for (i = 0; i < 43; i++)
  906. I915_WRITE(TV_V_CHROMA(i), tv_mode->filter_table[j++]);
  907. I915_WRITE(TV_DAC, I915_READ(TV_DAC) & TV_DAC_SAVE);
  908. I915_WRITE(TV_CTL, tv_ctl);
  909. }
  910. static const struct drm_display_mode reported_modes[] = {
  911. {
  912. .name = "NTSC 480i",
  913. .clock = 107520,
  914. .hdisplay = 1280,
  915. .hsync_start = 1368,
  916. .hsync_end = 1496,
  917. .htotal = 1712,
  918. .vdisplay = 1024,
  919. .vsync_start = 1027,
  920. .vsync_end = 1034,
  921. .vtotal = 1104,
  922. .type = DRM_MODE_TYPE_DRIVER,
  923. },
  924. };
  925. /**
  926. * Detects TV presence by checking for load.
  927. *
  928. * Requires that the current pipe's DPLL is active.
  929. * \return true if TV is connected.
  930. * \return false if TV is disconnected.
  931. */
  932. static int
  933. intel_tv_detect_type(struct intel_tv *intel_tv,
  934. struct drm_connector *connector)
  935. {
  936. struct drm_crtc *crtc = connector->state->crtc;
  937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  938. struct drm_device *dev = connector->dev;
  939. struct drm_i915_private *dev_priv = to_i915(dev);
  940. u32 tv_ctl, save_tv_ctl;
  941. u32 tv_dac, save_tv_dac;
  942. int type;
  943. /* Disable TV interrupts around load detect or we'll recurse */
  944. if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
  945. spin_lock_irq(&dev_priv->irq_lock);
  946. i915_disable_pipestat(dev_priv, 0,
  947. PIPE_HOTPLUG_INTERRUPT_STATUS |
  948. PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
  949. spin_unlock_irq(&dev_priv->irq_lock);
  950. }
  951. save_tv_dac = tv_dac = I915_READ(TV_DAC);
  952. save_tv_ctl = tv_ctl = I915_READ(TV_CTL);
  953. /* Poll for TV detection */
  954. tv_ctl &= ~(TV_ENC_ENABLE | TV_TEST_MODE_MASK);
  955. tv_ctl |= TV_TEST_MODE_MONITOR_DETECT;
  956. if (intel_crtc->pipe == 1)
  957. tv_ctl |= TV_ENC_PIPEB_SELECT;
  958. else
  959. tv_ctl &= ~TV_ENC_PIPEB_SELECT;
  960. tv_dac &= ~(TVDAC_SENSE_MASK | DAC_A_MASK | DAC_B_MASK | DAC_C_MASK);
  961. tv_dac |= (TVDAC_STATE_CHG_EN |
  962. TVDAC_A_SENSE_CTL |
  963. TVDAC_B_SENSE_CTL |
  964. TVDAC_C_SENSE_CTL |
  965. DAC_CTL_OVERRIDE |
  966. DAC_A_0_7_V |
  967. DAC_B_0_7_V |
  968. DAC_C_0_7_V);
  969. /*
  970. * The TV sense state should be cleared to zero on cantiga platform. Otherwise
  971. * the TV is misdetected. This is hardware requirement.
  972. */
  973. if (IS_GM45(dev_priv))
  974. tv_dac &= ~(TVDAC_STATE_CHG_EN | TVDAC_A_SENSE_CTL |
  975. TVDAC_B_SENSE_CTL | TVDAC_C_SENSE_CTL);
  976. I915_WRITE(TV_CTL, tv_ctl);
  977. I915_WRITE(TV_DAC, tv_dac);
  978. POSTING_READ(TV_DAC);
  979. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  980. type = -1;
  981. tv_dac = I915_READ(TV_DAC);
  982. DRM_DEBUG_KMS("TV detected: %x, %x\n", tv_ctl, tv_dac);
  983. /*
  984. * A B C
  985. * 0 1 1 Composite
  986. * 1 0 X svideo
  987. * 0 0 0 Component
  988. */
  989. if ((tv_dac & TVDAC_SENSE_MASK) == (TVDAC_B_SENSE | TVDAC_C_SENSE)) {
  990. DRM_DEBUG_KMS("Detected Composite TV connection\n");
  991. type = DRM_MODE_CONNECTOR_Composite;
  992. } else if ((tv_dac & (TVDAC_A_SENSE|TVDAC_B_SENSE)) == TVDAC_A_SENSE) {
  993. DRM_DEBUG_KMS("Detected S-Video TV connection\n");
  994. type = DRM_MODE_CONNECTOR_SVIDEO;
  995. } else if ((tv_dac & TVDAC_SENSE_MASK) == 0) {
  996. DRM_DEBUG_KMS("Detected Component TV connection\n");
  997. type = DRM_MODE_CONNECTOR_Component;
  998. } else {
  999. DRM_DEBUG_KMS("Unrecognised TV connection\n");
  1000. type = -1;
  1001. }
  1002. I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
  1003. I915_WRITE(TV_CTL, save_tv_ctl);
  1004. POSTING_READ(TV_CTL);
  1005. /* For unknown reasons the hw barfs if we don't do this vblank wait. */
  1006. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  1007. /* Restore interrupt config */
  1008. if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
  1009. spin_lock_irq(&dev_priv->irq_lock);
  1010. i915_enable_pipestat(dev_priv, 0,
  1011. PIPE_HOTPLUG_INTERRUPT_STATUS |
  1012. PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
  1013. spin_unlock_irq(&dev_priv->irq_lock);
  1014. }
  1015. return type;
  1016. }
  1017. /*
  1018. * Here we set accurate tv format according to connector type
  1019. * i.e Component TV should not be assigned by NTSC or PAL
  1020. */
  1021. static void intel_tv_find_better_format(struct drm_connector *connector)
  1022. {
  1023. struct intel_tv *intel_tv = intel_attached_tv(connector);
  1024. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  1025. int i;
  1026. if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
  1027. tv_mode->component_only)
  1028. return;
  1029. for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
  1030. tv_mode = tv_modes + i;
  1031. if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
  1032. tv_mode->component_only)
  1033. break;
  1034. }
  1035. connector->state->tv.mode = i;
  1036. }
  1037. /**
  1038. * Detect the TV connection.
  1039. *
  1040. * Currently this always returns CONNECTOR_STATUS_UNKNOWN, as we need to be sure
  1041. * we have a pipe programmed in order to probe the TV.
  1042. */
  1043. static int
  1044. intel_tv_detect(struct drm_connector *connector,
  1045. struct drm_modeset_acquire_ctx *ctx,
  1046. bool force)
  1047. {
  1048. struct drm_display_mode mode;
  1049. struct intel_tv *intel_tv = intel_attached_tv(connector);
  1050. enum drm_connector_status status;
  1051. int type;
  1052. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  1053. connector->base.id, connector->name,
  1054. force);
  1055. mode = reported_modes[0];
  1056. if (force) {
  1057. struct intel_load_detect_pipe tmp;
  1058. int ret;
  1059. ret = intel_get_load_detect_pipe(connector, &mode, &tmp, ctx);
  1060. if (ret < 0)
  1061. return ret;
  1062. if (ret > 0) {
  1063. type = intel_tv_detect_type(intel_tv, connector);
  1064. intel_release_load_detect_pipe(connector, &tmp, ctx);
  1065. status = type < 0 ?
  1066. connector_status_disconnected :
  1067. connector_status_connected;
  1068. } else
  1069. status = connector_status_unknown;
  1070. if (status == connector_status_connected) {
  1071. intel_tv->type = type;
  1072. intel_tv_find_better_format(connector);
  1073. }
  1074. return status;
  1075. } else
  1076. return connector->status;
  1077. }
  1078. static const struct input_res {
  1079. const char *name;
  1080. int w, h;
  1081. } input_res_table[] = {
  1082. {"640x480", 640, 480},
  1083. {"800x600", 800, 600},
  1084. {"1024x768", 1024, 768},
  1085. {"1280x1024", 1280, 1024},
  1086. {"848x480", 848, 480},
  1087. {"1280x720", 1280, 720},
  1088. {"1920x1080", 1920, 1080},
  1089. };
  1090. /*
  1091. * Chose preferred mode according to line number of TV format
  1092. */
  1093. static void
  1094. intel_tv_choose_preferred_modes(const struct tv_mode *tv_mode,
  1095. struct drm_display_mode *mode_ptr)
  1096. {
  1097. if (tv_mode->nbr_end < 480 && mode_ptr->vdisplay == 480)
  1098. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1099. else if (tv_mode->nbr_end > 480) {
  1100. if (tv_mode->progressive == true && tv_mode->nbr_end < 720) {
  1101. if (mode_ptr->vdisplay == 720)
  1102. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1103. } else if (mode_ptr->vdisplay == 1080)
  1104. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1105. }
  1106. }
  1107. /**
  1108. * Stub get_modes function.
  1109. *
  1110. * This should probably return a set of fixed modes, unless we can figure out
  1111. * how to probe modes off of TV connections.
  1112. */
  1113. static int
  1114. intel_tv_get_modes(struct drm_connector *connector)
  1115. {
  1116. struct drm_display_mode *mode_ptr;
  1117. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  1118. int j, count = 0;
  1119. u64 tmp;
  1120. for (j = 0; j < ARRAY_SIZE(input_res_table);
  1121. j++) {
  1122. const struct input_res *input = &input_res_table[j];
  1123. unsigned int hactive_s = input->w;
  1124. unsigned int vactive_s = input->h;
  1125. if (tv_mode->max_srcw && input->w > tv_mode->max_srcw)
  1126. continue;
  1127. if (input->w > 1024 && (!tv_mode->progressive
  1128. && !tv_mode->component_only))
  1129. continue;
  1130. mode_ptr = drm_mode_create(connector->dev);
  1131. if (!mode_ptr)
  1132. continue;
  1133. strncpy(mode_ptr->name, input->name, DRM_DISPLAY_MODE_LEN);
  1134. mode_ptr->name[DRM_DISPLAY_MODE_LEN - 1] = '\0';
  1135. mode_ptr->hdisplay = hactive_s;
  1136. mode_ptr->hsync_start = hactive_s + 1;
  1137. mode_ptr->hsync_end = hactive_s + 64;
  1138. if (mode_ptr->hsync_end <= mode_ptr->hsync_start)
  1139. mode_ptr->hsync_end = mode_ptr->hsync_start + 1;
  1140. mode_ptr->htotal = hactive_s + 96;
  1141. mode_ptr->vdisplay = vactive_s;
  1142. mode_ptr->vsync_start = vactive_s + 1;
  1143. mode_ptr->vsync_end = vactive_s + 32;
  1144. if (mode_ptr->vsync_end <= mode_ptr->vsync_start)
  1145. mode_ptr->vsync_end = mode_ptr->vsync_start + 1;
  1146. mode_ptr->vtotal = vactive_s + 33;
  1147. tmp = (u64) tv_mode->refresh * mode_ptr->vtotal;
  1148. tmp *= mode_ptr->htotal;
  1149. tmp = div_u64(tmp, 1000000);
  1150. mode_ptr->clock = (int) tmp;
  1151. mode_ptr->type = DRM_MODE_TYPE_DRIVER;
  1152. intel_tv_choose_preferred_modes(tv_mode, mode_ptr);
  1153. drm_mode_probed_add(connector, mode_ptr);
  1154. count++;
  1155. }
  1156. return count;
  1157. }
  1158. static void
  1159. intel_tv_destroy(struct drm_connector *connector)
  1160. {
  1161. drm_connector_cleanup(connector);
  1162. kfree(connector);
  1163. }
  1164. static const struct drm_connector_funcs intel_tv_connector_funcs = {
  1165. .late_register = intel_connector_register,
  1166. .early_unregister = intel_connector_unregister,
  1167. .destroy = intel_tv_destroy,
  1168. .fill_modes = drm_helper_probe_single_connector_modes,
  1169. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1170. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1171. };
  1172. static int intel_tv_atomic_check(struct drm_connector *connector,
  1173. struct drm_connector_state *new_state)
  1174. {
  1175. struct drm_crtc_state *new_crtc_state;
  1176. struct drm_connector_state *old_state;
  1177. if (!new_state->crtc)
  1178. return 0;
  1179. old_state = drm_atomic_get_old_connector_state(new_state->state, connector);
  1180. new_crtc_state = drm_atomic_get_new_crtc_state(new_state->state, new_state->crtc);
  1181. if (old_state->tv.mode != new_state->tv.mode ||
  1182. old_state->tv.margins.left != new_state->tv.margins.left ||
  1183. old_state->tv.margins.right != new_state->tv.margins.right ||
  1184. old_state->tv.margins.top != new_state->tv.margins.top ||
  1185. old_state->tv.margins.bottom != new_state->tv.margins.bottom) {
  1186. /* Force a modeset. */
  1187. new_crtc_state->connectors_changed = true;
  1188. }
  1189. return 0;
  1190. }
  1191. static const struct drm_connector_helper_funcs intel_tv_connector_helper_funcs = {
  1192. .detect_ctx = intel_tv_detect,
  1193. .mode_valid = intel_tv_mode_valid,
  1194. .get_modes = intel_tv_get_modes,
  1195. .atomic_check = intel_tv_atomic_check,
  1196. };
  1197. static const struct drm_encoder_funcs intel_tv_enc_funcs = {
  1198. .destroy = intel_encoder_destroy,
  1199. };
  1200. void
  1201. intel_tv_init(struct drm_i915_private *dev_priv)
  1202. {
  1203. struct drm_device *dev = &dev_priv->drm;
  1204. struct drm_connector *connector;
  1205. struct intel_tv *intel_tv;
  1206. struct intel_encoder *intel_encoder;
  1207. struct intel_connector *intel_connector;
  1208. u32 tv_dac_on, tv_dac_off, save_tv_dac;
  1209. const char *tv_format_names[ARRAY_SIZE(tv_modes)];
  1210. int i, initial_mode = 0;
  1211. struct drm_connector_state *state;
  1212. if ((I915_READ(TV_CTL) & TV_FUSE_STATE_MASK) == TV_FUSE_STATE_DISABLED)
  1213. return;
  1214. if (!intel_bios_is_tv_present(dev_priv)) {
  1215. DRM_DEBUG_KMS("Integrated TV is not present.\n");
  1216. return;
  1217. }
  1218. /*
  1219. * Sanity check the TV output by checking to see if the
  1220. * DAC register holds a value
  1221. */
  1222. save_tv_dac = I915_READ(TV_DAC);
  1223. I915_WRITE(TV_DAC, save_tv_dac | TVDAC_STATE_CHG_EN);
  1224. tv_dac_on = I915_READ(TV_DAC);
  1225. I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
  1226. tv_dac_off = I915_READ(TV_DAC);
  1227. I915_WRITE(TV_DAC, save_tv_dac);
  1228. /*
  1229. * If the register does not hold the state change enable
  1230. * bit, (either as a 0 or a 1), assume it doesn't really
  1231. * exist
  1232. */
  1233. if ((tv_dac_on & TVDAC_STATE_CHG_EN) == 0 ||
  1234. (tv_dac_off & TVDAC_STATE_CHG_EN) != 0)
  1235. return;
  1236. intel_tv = kzalloc(sizeof(*intel_tv), GFP_KERNEL);
  1237. if (!intel_tv) {
  1238. return;
  1239. }
  1240. intel_connector = intel_connector_alloc();
  1241. if (!intel_connector) {
  1242. kfree(intel_tv);
  1243. return;
  1244. }
  1245. intel_encoder = &intel_tv->base;
  1246. connector = &intel_connector->base;
  1247. state = connector->state;
  1248. /* The documentation, for the older chipsets at least, recommend
  1249. * using a polling method rather than hotplug detection for TVs.
  1250. * This is because in order to perform the hotplug detection, the PLLs
  1251. * for the TV must be kept alive increasing power drain and starving
  1252. * bandwidth from other encoders. Notably for instance, it causes
  1253. * pipe underruns on Crestline when this encoder is supposedly idle.
  1254. *
  1255. * More recent chipsets favour HDMI rather than integrated S-Video.
  1256. */
  1257. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  1258. drm_connector_init(dev, connector, &intel_tv_connector_funcs,
  1259. DRM_MODE_CONNECTOR_SVIDEO);
  1260. drm_encoder_init(dev, &intel_encoder->base, &intel_tv_enc_funcs,
  1261. DRM_MODE_ENCODER_TVDAC, "TV");
  1262. intel_encoder->compute_config = intel_tv_compute_config;
  1263. intel_encoder->get_config = intel_tv_get_config;
  1264. intel_encoder->pre_enable = intel_tv_pre_enable;
  1265. intel_encoder->enable = intel_enable_tv;
  1266. intel_encoder->disable = intel_disable_tv;
  1267. intel_encoder->get_hw_state = intel_tv_get_hw_state;
  1268. intel_connector->get_hw_state = intel_connector_get_hw_state;
  1269. intel_connector_attach_encoder(intel_connector, intel_encoder);
  1270. intel_encoder->type = INTEL_OUTPUT_TVOUT;
  1271. intel_encoder->power_domain = POWER_DOMAIN_PORT_OTHER;
  1272. intel_encoder->port = PORT_NONE;
  1273. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  1274. intel_encoder->cloneable = 0;
  1275. intel_encoder->base.possible_crtcs = ((1 << 0) | (1 << 1));
  1276. intel_tv->type = DRM_MODE_CONNECTOR_Unknown;
  1277. /* BIOS margin values */
  1278. state->tv.margins.left = 54;
  1279. state->tv.margins.top = 36;
  1280. state->tv.margins.right = 46;
  1281. state->tv.margins.bottom = 37;
  1282. state->tv.mode = initial_mode;
  1283. drm_connector_helper_add(connector, &intel_tv_connector_helper_funcs);
  1284. connector->interlace_allowed = false;
  1285. connector->doublescan_allowed = false;
  1286. /* Create TV properties then attach current values */
  1287. for (i = 0; i < ARRAY_SIZE(tv_modes); i++)
  1288. tv_format_names[i] = tv_modes[i].name;
  1289. drm_mode_create_tv_properties(dev,
  1290. ARRAY_SIZE(tv_modes),
  1291. tv_format_names);
  1292. drm_object_attach_property(&connector->base, dev->mode_config.tv_mode_property,
  1293. state->tv.mode);
  1294. drm_object_attach_property(&connector->base,
  1295. dev->mode_config.tv_left_margin_property,
  1296. state->tv.margins.left);
  1297. drm_object_attach_property(&connector->base,
  1298. dev->mode_config.tv_top_margin_property,
  1299. state->tv.margins.top);
  1300. drm_object_attach_property(&connector->base,
  1301. dev->mode_config.tv_right_margin_property,
  1302. state->tv.margins.right);
  1303. drm_object_attach_property(&connector->base,
  1304. dev->mode_config.tv_bottom_margin_property,
  1305. state->tv.margins.bottom);
  1306. }