intel_tv.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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. struct intel_tv {
  43. struct intel_encoder base;
  44. int type;
  45. };
  46. struct video_levels {
  47. u16 blank, black;
  48. u8 burst;
  49. };
  50. struct color_conversion {
  51. u16 ry, gy, by, ay;
  52. u16 ru, gu, bu, au;
  53. u16 rv, gv, bv, av;
  54. };
  55. static const u32 filter_table[] = {
  56. 0xB1403000, 0x2E203500, 0x35002E20, 0x3000B140,
  57. 0x35A0B160, 0x2DC02E80, 0xB1403480, 0xB1603000,
  58. 0x2EA03640, 0x34002D80, 0x3000B120, 0x36E0B160,
  59. 0x2D202EF0, 0xB1203380, 0xB1603000, 0x2F303780,
  60. 0x33002CC0, 0x3000B100, 0x3820B160, 0x2C802F50,
  61. 0xB10032A0, 0xB1603000, 0x2F9038C0, 0x32202C20,
  62. 0x3000B0E0, 0x3980B160, 0x2BC02FC0, 0xB0E031C0,
  63. 0xB1603000, 0x2FF03A20, 0x31602B60, 0xB020B0C0,
  64. 0x3AE0B160, 0x2B001810, 0xB0C03120, 0xB140B020,
  65. 0x18283BA0, 0x30C02A80, 0xB020B0A0, 0x3C60B140,
  66. 0x2A201838, 0xB0A03080, 0xB120B020, 0x18383D20,
  67. 0x304029C0, 0xB040B080, 0x3DE0B100, 0x29601848,
  68. 0xB0803000, 0xB100B040, 0x18483EC0, 0xB0402900,
  69. 0xB040B060, 0x3F80B0C0, 0x28801858, 0xB060B080,
  70. 0xB0A0B060, 0x18602820, 0xB0A02820, 0x0000B060,
  71. 0xB1403000, 0x2E203500, 0x35002E20, 0x3000B140,
  72. 0x35A0B160, 0x2DC02E80, 0xB1403480, 0xB1603000,
  73. 0x2EA03640, 0x34002D80, 0x3000B120, 0x36E0B160,
  74. 0x2D202EF0, 0xB1203380, 0xB1603000, 0x2F303780,
  75. 0x33002CC0, 0x3000B100, 0x3820B160, 0x2C802F50,
  76. 0xB10032A0, 0xB1603000, 0x2F9038C0, 0x32202C20,
  77. 0x3000B0E0, 0x3980B160, 0x2BC02FC0, 0xB0E031C0,
  78. 0xB1603000, 0x2FF03A20, 0x31602B60, 0xB020B0C0,
  79. 0x3AE0B160, 0x2B001810, 0xB0C03120, 0xB140B020,
  80. 0x18283BA0, 0x30C02A80, 0xB020B0A0, 0x3C60B140,
  81. 0x2A201838, 0xB0A03080, 0xB120B020, 0x18383D20,
  82. 0x304029C0, 0xB040B080, 0x3DE0B100, 0x29601848,
  83. 0xB0803000, 0xB100B040, 0x18483EC0, 0xB0402900,
  84. 0xB040B060, 0x3F80B0C0, 0x28801858, 0xB060B080,
  85. 0xB0A0B060, 0x18602820, 0xB0A02820, 0x0000B060,
  86. 0x36403000, 0x2D002CC0, 0x30003640, 0x2D0036C0,
  87. 0x35C02CC0, 0x37403000, 0x2C802D40, 0x30003540,
  88. 0x2D8037C0, 0x34C02C40, 0x38403000, 0x2BC02E00,
  89. 0x30003440, 0x2E2038C0, 0x34002B80, 0x39803000,
  90. 0x2B402E40, 0x30003380, 0x2E603A00, 0x33402B00,
  91. 0x3A803040, 0x2A802EA0, 0x30403300, 0x2EC03B40,
  92. 0x32802A40, 0x3C003040, 0x2A002EC0, 0x30803240,
  93. 0x2EC03C80, 0x320029C0, 0x3D403080, 0x29402F00,
  94. 0x308031C0, 0x2F203DC0, 0x31802900, 0x3E8030C0,
  95. 0x28802F40, 0x30C03140, 0x2F203F40, 0x31402840,
  96. 0x28003100, 0x28002F00, 0x00003100, 0x36403000,
  97. 0x2D002CC0, 0x30003640, 0x2D0036C0,
  98. 0x35C02CC0, 0x37403000, 0x2C802D40, 0x30003540,
  99. 0x2D8037C0, 0x34C02C40, 0x38403000, 0x2BC02E00,
  100. 0x30003440, 0x2E2038C0, 0x34002B80, 0x39803000,
  101. 0x2B402E40, 0x30003380, 0x2E603A00, 0x33402B00,
  102. 0x3A803040, 0x2A802EA0, 0x30403300, 0x2EC03B40,
  103. 0x32802A40, 0x3C003040, 0x2A002EC0, 0x30803240,
  104. 0x2EC03C80, 0x320029C0, 0x3D403080, 0x29402F00,
  105. 0x308031C0, 0x2F203DC0, 0x31802900, 0x3E8030C0,
  106. 0x28802F40, 0x30C03140, 0x2F203F40, 0x31402840,
  107. 0x28003100, 0x28002F00, 0x00003100,
  108. };
  109. /*
  110. * Color conversion values have 3 separate fixed point formats:
  111. *
  112. * 10 bit fields (ay, au)
  113. * 1.9 fixed point (b.bbbbbbbbb)
  114. * 11 bit fields (ry, by, ru, gu, gv)
  115. * exp.mantissa (ee.mmmmmmmmm)
  116. * ee = 00 = 10^-1 (0.mmmmmmmmm)
  117. * ee = 01 = 10^-2 (0.0mmmmmmmmm)
  118. * ee = 10 = 10^-3 (0.00mmmmmmmmm)
  119. * ee = 11 = 10^-4 (0.000mmmmmmmmm)
  120. * 12 bit fields (gy, rv, bu)
  121. * exp.mantissa (eee.mmmmmmmmm)
  122. * eee = 000 = 10^-1 (0.mmmmmmmmm)
  123. * eee = 001 = 10^-2 (0.0mmmmmmmmm)
  124. * eee = 010 = 10^-3 (0.00mmmmmmmmm)
  125. * eee = 011 = 10^-4 (0.000mmmmmmmmm)
  126. * eee = 100 = reserved
  127. * eee = 101 = reserved
  128. * eee = 110 = reserved
  129. * eee = 111 = 10^0 (m.mmmmmmmm) (only usable for 1.0 representation)
  130. *
  131. * Saturation and contrast are 8 bits, with their own representation:
  132. * 8 bit field (saturation, contrast)
  133. * exp.mantissa (ee.mmmmmm)
  134. * ee = 00 = 10^-1 (0.mmmmmm)
  135. * ee = 01 = 10^0 (m.mmmmm)
  136. * ee = 10 = 10^1 (mm.mmmm)
  137. * ee = 11 = 10^2 (mmm.mmm)
  138. *
  139. * Simple conversion function:
  140. *
  141. * static u32
  142. * float_to_csc_11(float f)
  143. * {
  144. * u32 exp;
  145. * u32 mant;
  146. * u32 ret;
  147. *
  148. * if (f < 0)
  149. * f = -f;
  150. *
  151. * if (f >= 1) {
  152. * exp = 0x7;
  153. * mant = 1 << 8;
  154. * } else {
  155. * for (exp = 0; exp < 3 && f < 0.5; exp++)
  156. * f *= 2.0;
  157. * mant = (f * (1 << 9) + 0.5);
  158. * if (mant >= (1 << 9))
  159. * mant = (1 << 9) - 1;
  160. * }
  161. * ret = (exp << 9) | mant;
  162. * return ret;
  163. * }
  164. */
  165. /*
  166. * Behold, magic numbers! If we plant them they might grow a big
  167. * s-video cable to the sky... or something.
  168. *
  169. * Pre-converted to appropriate hex value.
  170. */
  171. /*
  172. * PAL & NTSC values for composite & s-video connections
  173. */
  174. static const struct color_conversion ntsc_m_csc_composite = {
  175. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
  176. .ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
  177. .rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
  178. };
  179. static const struct video_levels ntsc_m_levels_composite = {
  180. .blank = 225, .black = 267, .burst = 113,
  181. };
  182. static const struct color_conversion ntsc_m_csc_svideo = {
  183. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
  184. .ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
  185. .rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
  186. };
  187. static const struct video_levels ntsc_m_levels_svideo = {
  188. .blank = 266, .black = 316, .burst = 133,
  189. };
  190. static const struct color_conversion ntsc_j_csc_composite = {
  191. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0119,
  192. .ru = 0x074c, .gu = 0x0546, .bu = 0x05ec, .au = 0x0200,
  193. .rv = 0x035a, .gv = 0x0322, .bv = 0x06e1, .av = 0x0200,
  194. };
  195. static const struct video_levels ntsc_j_levels_composite = {
  196. .blank = 225, .black = 225, .burst = 113,
  197. };
  198. static const struct color_conversion ntsc_j_csc_svideo = {
  199. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x014c,
  200. .ru = 0x0788, .gu = 0x0581, .bu = 0x0322, .au = 0x0200,
  201. .rv = 0x0399, .gv = 0x0356, .bv = 0x070a, .av = 0x0200,
  202. };
  203. static const struct video_levels ntsc_j_levels_svideo = {
  204. .blank = 266, .black = 266, .burst = 133,
  205. };
  206. static const struct color_conversion pal_csc_composite = {
  207. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0113,
  208. .ru = 0x0745, .gu = 0x053f, .bu = 0x05e1, .au = 0x0200,
  209. .rv = 0x0353, .gv = 0x031c, .bv = 0x06dc, .av = 0x0200,
  210. };
  211. static const struct video_levels pal_levels_composite = {
  212. .blank = 237, .black = 237, .burst = 118,
  213. };
  214. static const struct color_conversion pal_csc_svideo = {
  215. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0145,
  216. .ru = 0x0780, .gu = 0x0579, .bu = 0x031c, .au = 0x0200,
  217. .rv = 0x0390, .gv = 0x034f, .bv = 0x0705, .av = 0x0200,
  218. };
  219. static const struct video_levels pal_levels_svideo = {
  220. .blank = 280, .black = 280, .burst = 139,
  221. };
  222. static const struct color_conversion pal_m_csc_composite = {
  223. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
  224. .ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
  225. .rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
  226. };
  227. static const struct video_levels pal_m_levels_composite = {
  228. .blank = 225, .black = 267, .burst = 113,
  229. };
  230. static const struct color_conversion pal_m_csc_svideo = {
  231. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
  232. .ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
  233. .rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
  234. };
  235. static const struct video_levels pal_m_levels_svideo = {
  236. .blank = 266, .black = 316, .burst = 133,
  237. };
  238. static const struct color_conversion pal_n_csc_composite = {
  239. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0104,
  240. .ru = 0x0733, .gu = 0x052d, .bu = 0x05c7, .au = 0x0200,
  241. .rv = 0x0340, .gv = 0x030c, .bv = 0x06d0, .av = 0x0200,
  242. };
  243. static const struct video_levels pal_n_levels_composite = {
  244. .blank = 225, .black = 267, .burst = 118,
  245. };
  246. static const struct color_conversion pal_n_csc_svideo = {
  247. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0133,
  248. .ru = 0x076a, .gu = 0x0564, .bu = 0x030d, .au = 0x0200,
  249. .rv = 0x037a, .gv = 0x033d, .bv = 0x06f6, .av = 0x0200,
  250. };
  251. static const struct video_levels pal_n_levels_svideo = {
  252. .blank = 266, .black = 316, .burst = 139,
  253. };
  254. /*
  255. * Component connections
  256. */
  257. static const struct color_conversion sdtv_csc_yprpb = {
  258. .ry = 0x0332, .gy = 0x012d, .by = 0x07d3, .ay = 0x0145,
  259. .ru = 0x0559, .gu = 0x0353, .bu = 0x0100, .au = 0x0200,
  260. .rv = 0x0100, .gv = 0x03ad, .bv = 0x074d, .av = 0x0200,
  261. };
  262. static const struct color_conversion hdtv_csc_yprpb = {
  263. .ry = 0x05b3, .gy = 0x016e, .by = 0x0728, .ay = 0x0145,
  264. .ru = 0x07d5, .gu = 0x038b, .bu = 0x0100, .au = 0x0200,
  265. .rv = 0x0100, .gv = 0x03d1, .bv = 0x06bc, .av = 0x0200,
  266. };
  267. static const struct video_levels component_levels = {
  268. .blank = 279, .black = 279, .burst = 0,
  269. };
  270. struct tv_mode {
  271. const char *name;
  272. u32 clock;
  273. u16 refresh; /* in millihertz (for precision) */
  274. u32 oversample;
  275. u8 hsync_end;
  276. u16 hblank_start, hblank_end, htotal;
  277. bool progressive : 1, trilevel_sync : 1, component_only : 1;
  278. u8 vsync_start_f1, vsync_start_f2, vsync_len;
  279. bool veq_ena : 1;
  280. u8 veq_start_f1, veq_start_f2, veq_len;
  281. u8 vi_end_f1, vi_end_f2;
  282. u16 nbr_end;
  283. bool burst_ena : 1;
  284. u8 hburst_start, hburst_len;
  285. u8 vburst_start_f1;
  286. u16 vburst_end_f1;
  287. u8 vburst_start_f2;
  288. u16 vburst_end_f2;
  289. u8 vburst_start_f3;
  290. u16 vburst_end_f3;
  291. u8 vburst_start_f4;
  292. u16 vburst_end_f4;
  293. /*
  294. * subcarrier programming
  295. */
  296. u16 dda2_size, dda3_size;
  297. u8 dda1_inc;
  298. u16 dda2_inc, dda3_inc;
  299. u32 sc_reset;
  300. bool pal_burst : 1;
  301. /*
  302. * blank/black levels
  303. */
  304. const struct video_levels *composite_levels, *svideo_levels;
  305. const struct color_conversion *composite_color, *svideo_color;
  306. const u32 *filter_table;
  307. u16 max_srcw;
  308. };
  309. /*
  310. * Sub carrier DDA
  311. *
  312. * I think this works as follows:
  313. *
  314. * subcarrier freq = pixel_clock * (dda1_inc + dda2_inc / dda2_size) / 4096
  315. *
  316. * Presumably, when dda3 is added in, it gets to adjust the dda2_inc value
  317. *
  318. * So,
  319. * dda1_ideal = subcarrier/pixel * 4096
  320. * dda1_inc = floor (dda1_ideal)
  321. * dda2 = dda1_ideal - dda1_inc
  322. *
  323. * then pick a ratio for dda2 that gives the closest approximation. If
  324. * you can't get close enough, you can play with dda3 as well. This
  325. * seems likely to happen when dda2 is small as the jumps would be larger
  326. *
  327. * To invert this,
  328. *
  329. * pixel_clock = subcarrier * 4096 / (dda1_inc + dda2_inc / dda2_size)
  330. *
  331. * The constants below were all computed using a 107.520MHz clock
  332. */
  333. /*
  334. * Register programming values for TV modes.
  335. *
  336. * These values account for -1s required.
  337. */
  338. static const struct tv_mode tv_modes[] = {
  339. {
  340. .name = "NTSC-M",
  341. .clock = 108000,
  342. .refresh = 59940,
  343. .oversample = TV_OVERSAMPLE_8X,
  344. .component_only = 0,
  345. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
  346. .hsync_end = 64, .hblank_end = 124,
  347. .hblank_start = 836, .htotal = 857,
  348. .progressive = false, .trilevel_sync = false,
  349. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  350. .vsync_len = 6,
  351. .veq_ena = true, .veq_start_f1 = 0,
  352. .veq_start_f2 = 1, .veq_len = 18,
  353. .vi_end_f1 = 20, .vi_end_f2 = 21,
  354. .nbr_end = 240,
  355. .burst_ena = true,
  356. .hburst_start = 72, .hburst_len = 34,
  357. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  358. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  359. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  360. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  361. /* desired 3.5800000 actual 3.5800000 clock 107.52 */
  362. .dda1_inc = 135,
  363. .dda2_inc = 20800, .dda2_size = 27456,
  364. .dda3_inc = 0, .dda3_size = 0,
  365. .sc_reset = TV_SC_RESET_EVERY_4,
  366. .pal_burst = false,
  367. .composite_levels = &ntsc_m_levels_composite,
  368. .composite_color = &ntsc_m_csc_composite,
  369. .svideo_levels = &ntsc_m_levels_svideo,
  370. .svideo_color = &ntsc_m_csc_svideo,
  371. .filter_table = filter_table,
  372. },
  373. {
  374. .name = "NTSC-443",
  375. .clock = 108000,
  376. .refresh = 59940,
  377. .oversample = TV_OVERSAMPLE_8X,
  378. .component_only = 0,
  379. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 4.43MHz */
  380. .hsync_end = 64, .hblank_end = 124,
  381. .hblank_start = 836, .htotal = 857,
  382. .progressive = false, .trilevel_sync = false,
  383. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  384. .vsync_len = 6,
  385. .veq_ena = true, .veq_start_f1 = 0,
  386. .veq_start_f2 = 1, .veq_len = 18,
  387. .vi_end_f1 = 20, .vi_end_f2 = 21,
  388. .nbr_end = 240,
  389. .burst_ena = true,
  390. .hburst_start = 72, .hburst_len = 34,
  391. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  392. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  393. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  394. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  395. /* desired 4.4336180 actual 4.4336180 clock 107.52 */
  396. .dda1_inc = 168,
  397. .dda2_inc = 4093, .dda2_size = 27456,
  398. .dda3_inc = 310, .dda3_size = 525,
  399. .sc_reset = TV_SC_RESET_NEVER,
  400. .pal_burst = false,
  401. .composite_levels = &ntsc_m_levels_composite,
  402. .composite_color = &ntsc_m_csc_composite,
  403. .svideo_levels = &ntsc_m_levels_svideo,
  404. .svideo_color = &ntsc_m_csc_svideo,
  405. .filter_table = filter_table,
  406. },
  407. {
  408. .name = "NTSC-J",
  409. .clock = 108000,
  410. .refresh = 59940,
  411. .oversample = TV_OVERSAMPLE_8X,
  412. .component_only = 0,
  413. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
  414. .hsync_end = 64, .hblank_end = 124,
  415. .hblank_start = 836, .htotal = 857,
  416. .progressive = false, .trilevel_sync = false,
  417. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  418. .vsync_len = 6,
  419. .veq_ena = true, .veq_start_f1 = 0,
  420. .veq_start_f2 = 1, .veq_len = 18,
  421. .vi_end_f1 = 20, .vi_end_f2 = 21,
  422. .nbr_end = 240,
  423. .burst_ena = true,
  424. .hburst_start = 72, .hburst_len = 34,
  425. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  426. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  427. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  428. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  429. /* desired 3.5800000 actual 3.5800000 clock 107.52 */
  430. .dda1_inc = 135,
  431. .dda2_inc = 20800, .dda2_size = 27456,
  432. .dda3_inc = 0, .dda3_size = 0,
  433. .sc_reset = TV_SC_RESET_EVERY_4,
  434. .pal_burst = false,
  435. .composite_levels = &ntsc_j_levels_composite,
  436. .composite_color = &ntsc_j_csc_composite,
  437. .svideo_levels = &ntsc_j_levels_svideo,
  438. .svideo_color = &ntsc_j_csc_svideo,
  439. .filter_table = filter_table,
  440. },
  441. {
  442. .name = "PAL-M",
  443. .clock = 108000,
  444. .refresh = 59940,
  445. .oversample = TV_OVERSAMPLE_8X,
  446. .component_only = 0,
  447. /* 525 Lines, 60 Fields, 15.734KHz line, Sub-Carrier 3.580MHz */
  448. .hsync_end = 64, .hblank_end = 124,
  449. .hblank_start = 836, .htotal = 857,
  450. .progressive = false, .trilevel_sync = false,
  451. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  452. .vsync_len = 6,
  453. .veq_ena = true, .veq_start_f1 = 0,
  454. .veq_start_f2 = 1, .veq_len = 18,
  455. .vi_end_f1 = 20, .vi_end_f2 = 21,
  456. .nbr_end = 240,
  457. .burst_ena = true,
  458. .hburst_start = 72, .hburst_len = 34,
  459. .vburst_start_f1 = 9, .vburst_end_f1 = 240,
  460. .vburst_start_f2 = 10, .vburst_end_f2 = 240,
  461. .vburst_start_f3 = 9, .vburst_end_f3 = 240,
  462. .vburst_start_f4 = 10, .vburst_end_f4 = 240,
  463. /* desired 3.5800000 actual 3.5800000 clock 107.52 */
  464. .dda1_inc = 135,
  465. .dda2_inc = 16704, .dda2_size = 27456,
  466. .dda3_inc = 0, .dda3_size = 0,
  467. .sc_reset = TV_SC_RESET_EVERY_8,
  468. .pal_burst = true,
  469. .composite_levels = &pal_m_levels_composite,
  470. .composite_color = &pal_m_csc_composite,
  471. .svideo_levels = &pal_m_levels_svideo,
  472. .svideo_color = &pal_m_csc_svideo,
  473. .filter_table = filter_table,
  474. },
  475. {
  476. /* 625 Lines, 50 Fields, 15.625KHz line, Sub-Carrier 4.434MHz */
  477. .name = "PAL-N",
  478. .clock = 108000,
  479. .refresh = 50000,
  480. .oversample = TV_OVERSAMPLE_8X,
  481. .component_only = 0,
  482. .hsync_end = 64, .hblank_end = 128,
  483. .hblank_start = 844, .htotal = 863,
  484. .progressive = false, .trilevel_sync = false,
  485. .vsync_start_f1 = 6, .vsync_start_f2 = 7,
  486. .vsync_len = 6,
  487. .veq_ena = true, .veq_start_f1 = 0,
  488. .veq_start_f2 = 1, .veq_len = 18,
  489. .vi_end_f1 = 24, .vi_end_f2 = 25,
  490. .nbr_end = 286,
  491. .burst_ena = true,
  492. .hburst_start = 73, .hburst_len = 34,
  493. .vburst_start_f1 = 8, .vburst_end_f1 = 285,
  494. .vburst_start_f2 = 8, .vburst_end_f2 = 286,
  495. .vburst_start_f3 = 9, .vburst_end_f3 = 286,
  496. .vburst_start_f4 = 9, .vburst_end_f4 = 285,
  497. /* desired 4.4336180 actual 4.4336180 clock 107.52 */
  498. .dda1_inc = 135,
  499. .dda2_inc = 23578, .dda2_size = 27648,
  500. .dda3_inc = 134, .dda3_size = 625,
  501. .sc_reset = TV_SC_RESET_EVERY_8,
  502. .pal_burst = true,
  503. .composite_levels = &pal_n_levels_composite,
  504. .composite_color = &pal_n_csc_composite,
  505. .svideo_levels = &pal_n_levels_svideo,
  506. .svideo_color = &pal_n_csc_svideo,
  507. .filter_table = filter_table,
  508. },
  509. {
  510. /* 625 Lines, 50 Fields, 15.625KHz line, Sub-Carrier 4.434MHz */
  511. .name = "PAL",
  512. .clock = 108000,
  513. .refresh = 50000,
  514. .oversample = TV_OVERSAMPLE_8X,
  515. .component_only = 0,
  516. .hsync_end = 64, .hblank_end = 142,
  517. .hblank_start = 844, .htotal = 863,
  518. .progressive = false, .trilevel_sync = false,
  519. .vsync_start_f1 = 5, .vsync_start_f2 = 6,
  520. .vsync_len = 5,
  521. .veq_ena = true, .veq_start_f1 = 0,
  522. .veq_start_f2 = 1, .veq_len = 15,
  523. .vi_end_f1 = 24, .vi_end_f2 = 25,
  524. .nbr_end = 286,
  525. .burst_ena = true,
  526. .hburst_start = 73, .hburst_len = 32,
  527. .vburst_start_f1 = 8, .vburst_end_f1 = 285,
  528. .vburst_start_f2 = 8, .vburst_end_f2 = 286,
  529. .vburst_start_f3 = 9, .vburst_end_f3 = 286,
  530. .vburst_start_f4 = 9, .vburst_end_f4 = 285,
  531. /* desired 4.4336180 actual 4.4336180 clock 107.52 */
  532. .dda1_inc = 168,
  533. .dda2_inc = 4122, .dda2_size = 27648,
  534. .dda3_inc = 67, .dda3_size = 625,
  535. .sc_reset = TV_SC_RESET_EVERY_8,
  536. .pal_burst = true,
  537. .composite_levels = &pal_levels_composite,
  538. .composite_color = &pal_csc_composite,
  539. .svideo_levels = &pal_levels_svideo,
  540. .svideo_color = &pal_csc_svideo,
  541. .filter_table = filter_table,
  542. },
  543. {
  544. .name = "480p",
  545. .clock = 107520,
  546. .refresh = 59940,
  547. .oversample = TV_OVERSAMPLE_4X,
  548. .component_only = 1,
  549. .hsync_end = 64, .hblank_end = 122,
  550. .hblank_start = 842, .htotal = 857,
  551. .progressive = true, .trilevel_sync = false,
  552. .vsync_start_f1 = 12, .vsync_start_f2 = 12,
  553. .vsync_len = 12,
  554. .veq_ena = false,
  555. .vi_end_f1 = 44, .vi_end_f2 = 44,
  556. .nbr_end = 479,
  557. .burst_ena = false,
  558. .filter_table = filter_table,
  559. },
  560. {
  561. .name = "576p",
  562. .clock = 107520,
  563. .refresh = 50000,
  564. .oversample = TV_OVERSAMPLE_4X,
  565. .component_only = 1,
  566. .hsync_end = 64, .hblank_end = 139,
  567. .hblank_start = 859, .htotal = 863,
  568. .progressive = true, .trilevel_sync = false,
  569. .vsync_start_f1 = 10, .vsync_start_f2 = 10,
  570. .vsync_len = 10,
  571. .veq_ena = false,
  572. .vi_end_f1 = 48, .vi_end_f2 = 48,
  573. .nbr_end = 575,
  574. .burst_ena = false,
  575. .filter_table = filter_table,
  576. },
  577. {
  578. .name = "720p@60Hz",
  579. .clock = 148800,
  580. .refresh = 60000,
  581. .oversample = TV_OVERSAMPLE_2X,
  582. .component_only = 1,
  583. .hsync_end = 80, .hblank_end = 300,
  584. .hblank_start = 1580, .htotal = 1649,
  585. .progressive = true, .trilevel_sync = true,
  586. .vsync_start_f1 = 10, .vsync_start_f2 = 10,
  587. .vsync_len = 10,
  588. .veq_ena = false,
  589. .vi_end_f1 = 29, .vi_end_f2 = 29,
  590. .nbr_end = 719,
  591. .burst_ena = false,
  592. .filter_table = filter_table,
  593. },
  594. {
  595. .name = "720p@50Hz",
  596. .clock = 148800,
  597. .refresh = 50000,
  598. .oversample = TV_OVERSAMPLE_2X,
  599. .component_only = 1,
  600. .hsync_end = 80, .hblank_end = 300,
  601. .hblank_start = 1580, .htotal = 1979,
  602. .progressive = true, .trilevel_sync = true,
  603. .vsync_start_f1 = 10, .vsync_start_f2 = 10,
  604. .vsync_len = 10,
  605. .veq_ena = false,
  606. .vi_end_f1 = 29, .vi_end_f2 = 29,
  607. .nbr_end = 719,
  608. .burst_ena = false,
  609. .filter_table = filter_table,
  610. .max_srcw = 800
  611. },
  612. {
  613. .name = "1080i@50Hz",
  614. .clock = 148800,
  615. .refresh = 50000,
  616. .oversample = TV_OVERSAMPLE_2X,
  617. .component_only = 1,
  618. .hsync_end = 88, .hblank_end = 235,
  619. .hblank_start = 2155, .htotal = 2639,
  620. .progressive = false, .trilevel_sync = true,
  621. .vsync_start_f1 = 4, .vsync_start_f2 = 5,
  622. .vsync_len = 10,
  623. .veq_ena = true, .veq_start_f1 = 4,
  624. .veq_start_f2 = 4, .veq_len = 10,
  625. .vi_end_f1 = 21, .vi_end_f2 = 22,
  626. .nbr_end = 539,
  627. .burst_ena = false,
  628. .filter_table = filter_table,
  629. },
  630. {
  631. .name = "1080i@60Hz",
  632. .clock = 148800,
  633. .refresh = 60000,
  634. .oversample = TV_OVERSAMPLE_2X,
  635. .component_only = 1,
  636. .hsync_end = 88, .hblank_end = 235,
  637. .hblank_start = 2155, .htotal = 2199,
  638. .progressive = false, .trilevel_sync = true,
  639. .vsync_start_f1 = 4, .vsync_start_f2 = 5,
  640. .vsync_len = 10,
  641. .veq_ena = true, .veq_start_f1 = 4,
  642. .veq_start_f2 = 4, .veq_len = 10,
  643. .vi_end_f1 = 21, .vi_end_f2 = 22,
  644. .nbr_end = 539,
  645. .burst_ena = false,
  646. .filter_table = filter_table,
  647. },
  648. };
  649. static struct intel_tv *enc_to_tv(struct intel_encoder *encoder)
  650. {
  651. return container_of(encoder, struct intel_tv, base);
  652. }
  653. static struct intel_tv *intel_attached_tv(struct drm_connector *connector)
  654. {
  655. return enc_to_tv(intel_attached_encoder(connector));
  656. }
  657. static bool
  658. intel_tv_get_hw_state(struct intel_encoder *encoder, enum pipe *pipe)
  659. {
  660. struct drm_device *dev = encoder->base.dev;
  661. struct drm_i915_private *dev_priv = to_i915(dev);
  662. u32 tmp = I915_READ(TV_CTL);
  663. if (!(tmp & TV_ENC_ENABLE))
  664. return false;
  665. *pipe = PORT_TO_PIPE(tmp);
  666. return true;
  667. }
  668. static void
  669. intel_enable_tv(struct intel_encoder *encoder,
  670. const struct intel_crtc_state *pipe_config,
  671. const struct drm_connector_state *conn_state)
  672. {
  673. struct drm_device *dev = encoder->base.dev;
  674. struct drm_i915_private *dev_priv = to_i915(dev);
  675. /* Prevents vblank waits from timing out in intel_tv_detect_type() */
  676. intel_wait_for_vblank(dev_priv,
  677. to_intel_crtc(pipe_config->base.crtc)->pipe);
  678. I915_WRITE(TV_CTL, I915_READ(TV_CTL) | TV_ENC_ENABLE);
  679. }
  680. static void
  681. intel_disable_tv(struct intel_encoder *encoder,
  682. const struct intel_crtc_state *old_crtc_state,
  683. const struct drm_connector_state *old_conn_state)
  684. {
  685. struct drm_device *dev = encoder->base.dev;
  686. struct drm_i915_private *dev_priv = to_i915(dev);
  687. I915_WRITE(TV_CTL, I915_READ(TV_CTL) & ~TV_ENC_ENABLE);
  688. }
  689. static const struct tv_mode *intel_tv_mode_find(const struct drm_connector_state *conn_state)
  690. {
  691. int format = conn_state->tv.mode;
  692. return &tv_modes[format];
  693. }
  694. static enum drm_mode_status
  695. intel_tv_mode_valid(struct drm_connector *connector,
  696. struct drm_display_mode *mode)
  697. {
  698. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  699. int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
  700. if (mode->clock > max_dotclk)
  701. return MODE_CLOCK_HIGH;
  702. /* Ensure TV refresh is close to desired refresh */
  703. if (tv_mode && abs(tv_mode->refresh - drm_mode_vrefresh(mode) * 1000)
  704. < 1000)
  705. return MODE_OK;
  706. return MODE_CLOCK_RANGE;
  707. }
  708. static void
  709. intel_tv_get_config(struct intel_encoder *encoder,
  710. struct intel_crtc_state *pipe_config)
  711. {
  712. pipe_config->output_types |= BIT(INTEL_OUTPUT_TVOUT);
  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. const struct intel_crtc_state *pipe_config,
  799. const 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(pipe_config->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. static int
  926. intel_tv_detect_type(struct intel_tv *intel_tv,
  927. struct drm_connector *connector)
  928. {
  929. struct drm_crtc *crtc = connector->state->crtc;
  930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  931. struct drm_device *dev = connector->dev;
  932. struct drm_i915_private *dev_priv = to_i915(dev);
  933. u32 tv_ctl, save_tv_ctl;
  934. u32 tv_dac, save_tv_dac;
  935. int type;
  936. /* Disable TV interrupts around load detect or we'll recurse */
  937. if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
  938. spin_lock_irq(&dev_priv->irq_lock);
  939. i915_disable_pipestat(dev_priv, 0,
  940. PIPE_HOTPLUG_INTERRUPT_STATUS |
  941. PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
  942. spin_unlock_irq(&dev_priv->irq_lock);
  943. }
  944. save_tv_dac = tv_dac = I915_READ(TV_DAC);
  945. save_tv_ctl = tv_ctl = I915_READ(TV_CTL);
  946. /* Poll for TV detection */
  947. tv_ctl &= ~(TV_ENC_ENABLE | TV_TEST_MODE_MASK);
  948. tv_ctl |= TV_TEST_MODE_MONITOR_DETECT;
  949. if (intel_crtc->pipe == 1)
  950. tv_ctl |= TV_ENC_PIPEB_SELECT;
  951. else
  952. tv_ctl &= ~TV_ENC_PIPEB_SELECT;
  953. tv_dac &= ~(TVDAC_SENSE_MASK | DAC_A_MASK | DAC_B_MASK | DAC_C_MASK);
  954. tv_dac |= (TVDAC_STATE_CHG_EN |
  955. TVDAC_A_SENSE_CTL |
  956. TVDAC_B_SENSE_CTL |
  957. TVDAC_C_SENSE_CTL |
  958. DAC_CTL_OVERRIDE |
  959. DAC_A_0_7_V |
  960. DAC_B_0_7_V |
  961. DAC_C_0_7_V);
  962. /*
  963. * The TV sense state should be cleared to zero on cantiga platform. Otherwise
  964. * the TV is misdetected. This is hardware requirement.
  965. */
  966. if (IS_GM45(dev_priv))
  967. tv_dac &= ~(TVDAC_STATE_CHG_EN | TVDAC_A_SENSE_CTL |
  968. TVDAC_B_SENSE_CTL | TVDAC_C_SENSE_CTL);
  969. I915_WRITE(TV_CTL, tv_ctl);
  970. I915_WRITE(TV_DAC, tv_dac);
  971. POSTING_READ(TV_DAC);
  972. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  973. type = -1;
  974. tv_dac = I915_READ(TV_DAC);
  975. DRM_DEBUG_KMS("TV detected: %x, %x\n", tv_ctl, tv_dac);
  976. /*
  977. * A B C
  978. * 0 1 1 Composite
  979. * 1 0 X svideo
  980. * 0 0 0 Component
  981. */
  982. if ((tv_dac & TVDAC_SENSE_MASK) == (TVDAC_B_SENSE | TVDAC_C_SENSE)) {
  983. DRM_DEBUG_KMS("Detected Composite TV connection\n");
  984. type = DRM_MODE_CONNECTOR_Composite;
  985. } else if ((tv_dac & (TVDAC_A_SENSE|TVDAC_B_SENSE)) == TVDAC_A_SENSE) {
  986. DRM_DEBUG_KMS("Detected S-Video TV connection\n");
  987. type = DRM_MODE_CONNECTOR_SVIDEO;
  988. } else if ((tv_dac & TVDAC_SENSE_MASK) == 0) {
  989. DRM_DEBUG_KMS("Detected Component TV connection\n");
  990. type = DRM_MODE_CONNECTOR_Component;
  991. } else {
  992. DRM_DEBUG_KMS("Unrecognised TV connection\n");
  993. type = -1;
  994. }
  995. I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
  996. I915_WRITE(TV_CTL, save_tv_ctl);
  997. POSTING_READ(TV_CTL);
  998. /* For unknown reasons the hw barfs if we don't do this vblank wait. */
  999. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  1000. /* Restore interrupt config */
  1001. if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
  1002. spin_lock_irq(&dev_priv->irq_lock);
  1003. i915_enable_pipestat(dev_priv, 0,
  1004. PIPE_HOTPLUG_INTERRUPT_STATUS |
  1005. PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
  1006. spin_unlock_irq(&dev_priv->irq_lock);
  1007. }
  1008. return type;
  1009. }
  1010. /*
  1011. * Here we set accurate tv format according to connector type
  1012. * i.e Component TV should not be assigned by NTSC or PAL
  1013. */
  1014. static void intel_tv_find_better_format(struct drm_connector *connector)
  1015. {
  1016. struct intel_tv *intel_tv = intel_attached_tv(connector);
  1017. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  1018. int i;
  1019. if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
  1020. tv_mode->component_only)
  1021. return;
  1022. for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
  1023. tv_mode = tv_modes + i;
  1024. if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
  1025. tv_mode->component_only)
  1026. break;
  1027. }
  1028. connector->state->tv.mode = i;
  1029. }
  1030. static int
  1031. intel_tv_detect(struct drm_connector *connector,
  1032. struct drm_modeset_acquire_ctx *ctx,
  1033. bool force)
  1034. {
  1035. struct drm_display_mode mode;
  1036. struct intel_tv *intel_tv = intel_attached_tv(connector);
  1037. enum drm_connector_status status;
  1038. int type;
  1039. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  1040. connector->base.id, connector->name,
  1041. force);
  1042. mode = reported_modes[0];
  1043. if (force) {
  1044. struct intel_load_detect_pipe tmp;
  1045. int ret;
  1046. ret = intel_get_load_detect_pipe(connector, &mode, &tmp, ctx);
  1047. if (ret < 0)
  1048. return ret;
  1049. if (ret > 0) {
  1050. type = intel_tv_detect_type(intel_tv, connector);
  1051. intel_release_load_detect_pipe(connector, &tmp, ctx);
  1052. status = type < 0 ?
  1053. connector_status_disconnected :
  1054. connector_status_connected;
  1055. } else
  1056. status = connector_status_unknown;
  1057. if (status == connector_status_connected) {
  1058. intel_tv->type = type;
  1059. intel_tv_find_better_format(connector);
  1060. }
  1061. return status;
  1062. } else
  1063. return connector->status;
  1064. }
  1065. static const struct input_res {
  1066. const char *name;
  1067. int w, h;
  1068. } input_res_table[] = {
  1069. {"640x480", 640, 480},
  1070. {"800x600", 800, 600},
  1071. {"1024x768", 1024, 768},
  1072. {"1280x1024", 1280, 1024},
  1073. {"848x480", 848, 480},
  1074. {"1280x720", 1280, 720},
  1075. {"1920x1080", 1920, 1080},
  1076. };
  1077. /*
  1078. * Chose preferred mode according to line number of TV format
  1079. */
  1080. static void
  1081. intel_tv_choose_preferred_modes(const struct tv_mode *tv_mode,
  1082. struct drm_display_mode *mode_ptr)
  1083. {
  1084. if (tv_mode->nbr_end < 480 && mode_ptr->vdisplay == 480)
  1085. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1086. else if (tv_mode->nbr_end > 480) {
  1087. if (tv_mode->progressive == true && tv_mode->nbr_end < 720) {
  1088. if (mode_ptr->vdisplay == 720)
  1089. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1090. } else if (mode_ptr->vdisplay == 1080)
  1091. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1092. }
  1093. }
  1094. static int
  1095. intel_tv_get_modes(struct drm_connector *connector)
  1096. {
  1097. struct drm_display_mode *mode_ptr;
  1098. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  1099. int j, count = 0;
  1100. u64 tmp;
  1101. for (j = 0; j < ARRAY_SIZE(input_res_table);
  1102. j++) {
  1103. const struct input_res *input = &input_res_table[j];
  1104. unsigned int hactive_s = input->w;
  1105. unsigned int vactive_s = input->h;
  1106. if (tv_mode->max_srcw && input->w > tv_mode->max_srcw)
  1107. continue;
  1108. if (input->w > 1024 && (!tv_mode->progressive
  1109. && !tv_mode->component_only))
  1110. continue;
  1111. mode_ptr = drm_mode_create(connector->dev);
  1112. if (!mode_ptr)
  1113. continue;
  1114. strncpy(mode_ptr->name, input->name, DRM_DISPLAY_MODE_LEN);
  1115. mode_ptr->name[DRM_DISPLAY_MODE_LEN - 1] = '\0';
  1116. mode_ptr->hdisplay = hactive_s;
  1117. mode_ptr->hsync_start = hactive_s + 1;
  1118. mode_ptr->hsync_end = hactive_s + 64;
  1119. if (mode_ptr->hsync_end <= mode_ptr->hsync_start)
  1120. mode_ptr->hsync_end = mode_ptr->hsync_start + 1;
  1121. mode_ptr->htotal = hactive_s + 96;
  1122. mode_ptr->vdisplay = vactive_s;
  1123. mode_ptr->vsync_start = vactive_s + 1;
  1124. mode_ptr->vsync_end = vactive_s + 32;
  1125. if (mode_ptr->vsync_end <= mode_ptr->vsync_start)
  1126. mode_ptr->vsync_end = mode_ptr->vsync_start + 1;
  1127. mode_ptr->vtotal = vactive_s + 33;
  1128. tmp = mul_u32_u32(tv_mode->refresh, mode_ptr->vtotal);
  1129. tmp *= mode_ptr->htotal;
  1130. tmp = div_u64(tmp, 1000000);
  1131. mode_ptr->clock = (int) tmp;
  1132. mode_ptr->type = DRM_MODE_TYPE_DRIVER;
  1133. intel_tv_choose_preferred_modes(tv_mode, mode_ptr);
  1134. drm_mode_probed_add(connector, mode_ptr);
  1135. count++;
  1136. }
  1137. return count;
  1138. }
  1139. static void
  1140. intel_tv_destroy(struct drm_connector *connector)
  1141. {
  1142. drm_connector_cleanup(connector);
  1143. kfree(connector);
  1144. }
  1145. static const struct drm_connector_funcs intel_tv_connector_funcs = {
  1146. .late_register = intel_connector_register,
  1147. .early_unregister = intel_connector_unregister,
  1148. .destroy = intel_tv_destroy,
  1149. .fill_modes = drm_helper_probe_single_connector_modes,
  1150. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1151. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1152. };
  1153. static int intel_tv_atomic_check(struct drm_connector *connector,
  1154. struct drm_connector_state *new_state)
  1155. {
  1156. struct drm_crtc_state *new_crtc_state;
  1157. struct drm_connector_state *old_state;
  1158. if (!new_state->crtc)
  1159. return 0;
  1160. old_state = drm_atomic_get_old_connector_state(new_state->state, connector);
  1161. new_crtc_state = drm_atomic_get_new_crtc_state(new_state->state, new_state->crtc);
  1162. if (old_state->tv.mode != new_state->tv.mode ||
  1163. old_state->tv.margins.left != new_state->tv.margins.left ||
  1164. old_state->tv.margins.right != new_state->tv.margins.right ||
  1165. old_state->tv.margins.top != new_state->tv.margins.top ||
  1166. old_state->tv.margins.bottom != new_state->tv.margins.bottom) {
  1167. /* Force a modeset. */
  1168. new_crtc_state->connectors_changed = true;
  1169. }
  1170. return 0;
  1171. }
  1172. static const struct drm_connector_helper_funcs intel_tv_connector_helper_funcs = {
  1173. .detect_ctx = intel_tv_detect,
  1174. .mode_valid = intel_tv_mode_valid,
  1175. .get_modes = intel_tv_get_modes,
  1176. .atomic_check = intel_tv_atomic_check,
  1177. };
  1178. static const struct drm_encoder_funcs intel_tv_enc_funcs = {
  1179. .destroy = intel_encoder_destroy,
  1180. };
  1181. void
  1182. intel_tv_init(struct drm_i915_private *dev_priv)
  1183. {
  1184. struct drm_device *dev = &dev_priv->drm;
  1185. struct drm_connector *connector;
  1186. struct intel_tv *intel_tv;
  1187. struct intel_encoder *intel_encoder;
  1188. struct intel_connector *intel_connector;
  1189. u32 tv_dac_on, tv_dac_off, save_tv_dac;
  1190. const char *tv_format_names[ARRAY_SIZE(tv_modes)];
  1191. int i, initial_mode = 0;
  1192. struct drm_connector_state *state;
  1193. if ((I915_READ(TV_CTL) & TV_FUSE_STATE_MASK) == TV_FUSE_STATE_DISABLED)
  1194. return;
  1195. if (!intel_bios_is_tv_present(dev_priv)) {
  1196. DRM_DEBUG_KMS("Integrated TV is not present.\n");
  1197. return;
  1198. }
  1199. /*
  1200. * Sanity check the TV output by checking to see if the
  1201. * DAC register holds a value
  1202. */
  1203. save_tv_dac = I915_READ(TV_DAC);
  1204. I915_WRITE(TV_DAC, save_tv_dac | TVDAC_STATE_CHG_EN);
  1205. tv_dac_on = I915_READ(TV_DAC);
  1206. I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
  1207. tv_dac_off = I915_READ(TV_DAC);
  1208. I915_WRITE(TV_DAC, save_tv_dac);
  1209. /*
  1210. * If the register does not hold the state change enable
  1211. * bit, (either as a 0 or a 1), assume it doesn't really
  1212. * exist
  1213. */
  1214. if ((tv_dac_on & TVDAC_STATE_CHG_EN) == 0 ||
  1215. (tv_dac_off & TVDAC_STATE_CHG_EN) != 0)
  1216. return;
  1217. intel_tv = kzalloc(sizeof(*intel_tv), GFP_KERNEL);
  1218. if (!intel_tv) {
  1219. return;
  1220. }
  1221. intel_connector = intel_connector_alloc();
  1222. if (!intel_connector) {
  1223. kfree(intel_tv);
  1224. return;
  1225. }
  1226. intel_encoder = &intel_tv->base;
  1227. connector = &intel_connector->base;
  1228. state = connector->state;
  1229. /*
  1230. * The documentation, for the older chipsets at least, recommend
  1231. * using a polling method rather than hotplug detection for TVs.
  1232. * This is because in order to perform the hotplug detection, the PLLs
  1233. * for the TV must be kept alive increasing power drain and starving
  1234. * bandwidth from other encoders. Notably for instance, it causes
  1235. * pipe underruns on Crestline when this encoder is supposedly idle.
  1236. *
  1237. * More recent chipsets favour HDMI rather than integrated S-Video.
  1238. */
  1239. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  1240. drm_connector_init(dev, connector, &intel_tv_connector_funcs,
  1241. DRM_MODE_CONNECTOR_SVIDEO);
  1242. drm_encoder_init(dev, &intel_encoder->base, &intel_tv_enc_funcs,
  1243. DRM_MODE_ENCODER_TVDAC, "TV");
  1244. intel_encoder->compute_config = intel_tv_compute_config;
  1245. intel_encoder->get_config = intel_tv_get_config;
  1246. intel_encoder->pre_enable = intel_tv_pre_enable;
  1247. intel_encoder->enable = intel_enable_tv;
  1248. intel_encoder->disable = intel_disable_tv;
  1249. intel_encoder->get_hw_state = intel_tv_get_hw_state;
  1250. intel_connector->get_hw_state = intel_connector_get_hw_state;
  1251. intel_connector_attach_encoder(intel_connector, intel_encoder);
  1252. intel_encoder->type = INTEL_OUTPUT_TVOUT;
  1253. intel_encoder->power_domain = POWER_DOMAIN_PORT_OTHER;
  1254. intel_encoder->port = PORT_NONE;
  1255. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  1256. intel_encoder->cloneable = 0;
  1257. intel_encoder->base.possible_crtcs = ((1 << 0) | (1 << 1));
  1258. intel_tv->type = DRM_MODE_CONNECTOR_Unknown;
  1259. /* BIOS margin values */
  1260. state->tv.margins.left = 54;
  1261. state->tv.margins.top = 36;
  1262. state->tv.margins.right = 46;
  1263. state->tv.margins.bottom = 37;
  1264. state->tv.mode = initial_mode;
  1265. drm_connector_helper_add(connector, &intel_tv_connector_helper_funcs);
  1266. connector->interlace_allowed = false;
  1267. connector->doublescan_allowed = false;
  1268. /* Create TV properties then attach current values */
  1269. for (i = 0; i < ARRAY_SIZE(tv_modes); i++)
  1270. tv_format_names[i] = tv_modes[i].name;
  1271. drm_mode_create_tv_properties(dev,
  1272. ARRAY_SIZE(tv_modes),
  1273. tv_format_names);
  1274. drm_object_attach_property(&connector->base, dev->mode_config.tv_mode_property,
  1275. state->tv.mode);
  1276. drm_object_attach_property(&connector->base,
  1277. dev->mode_config.tv_left_margin_property,
  1278. state->tv.margins.left);
  1279. drm_object_attach_property(&connector->base,
  1280. dev->mode_config.tv_top_margin_property,
  1281. state->tv.margins.top);
  1282. drm_object_attach_property(&connector->base,
  1283. dev->mode_config.tv_right_margin_property,
  1284. state->tv.margins.right);
  1285. drm_object_attach_property(&connector->base,
  1286. dev->mode_config.tv_bottom_margin_property,
  1287. state->tv.margins.bottom);
  1288. }