intel_tv.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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. const struct intel_crtc_state *pipe_config,
  672. const 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(pipe_config->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. const struct intel_crtc_state *old_crtc_state,
  684. const 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(const 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->output_types |= BIT(INTEL_OUTPUT_TVOUT);
  714. pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
  715. }
  716. static bool
  717. intel_tv_compute_config(struct intel_encoder *encoder,
  718. struct intel_crtc_state *pipe_config,
  719. struct drm_connector_state *conn_state)
  720. {
  721. const struct tv_mode *tv_mode = intel_tv_mode_find(conn_state);
  722. if (!tv_mode)
  723. return false;
  724. pipe_config->base.adjusted_mode.crtc_clock = tv_mode->clock;
  725. DRM_DEBUG_KMS("forcing bpc to 8 for TV\n");
  726. pipe_config->pipe_bpp = 8*3;
  727. /* TV has it's own notion of sync and other mode flags, so clear them. */
  728. pipe_config->base.adjusted_mode.flags = 0;
  729. /*
  730. * FIXME: We don't check whether the input mode is actually what we want
  731. * or whether userspace is doing something stupid.
  732. */
  733. return true;
  734. }
  735. static void
  736. set_tv_mode_timings(struct drm_i915_private *dev_priv,
  737. const struct tv_mode *tv_mode,
  738. bool burst_ena)
  739. {
  740. u32 hctl1, hctl2, hctl3;
  741. u32 vctl1, vctl2, vctl3, vctl4, vctl5, vctl6, vctl7;
  742. hctl1 = (tv_mode->hsync_end << TV_HSYNC_END_SHIFT) |
  743. (tv_mode->htotal << TV_HTOTAL_SHIFT);
  744. hctl2 = (tv_mode->hburst_start << 16) |
  745. (tv_mode->hburst_len << TV_HBURST_LEN_SHIFT);
  746. if (burst_ena)
  747. hctl2 |= TV_BURST_ENA;
  748. hctl3 = (tv_mode->hblank_start << TV_HBLANK_START_SHIFT) |
  749. (tv_mode->hblank_end << TV_HBLANK_END_SHIFT);
  750. vctl1 = (tv_mode->nbr_end << TV_NBR_END_SHIFT) |
  751. (tv_mode->vi_end_f1 << TV_VI_END_F1_SHIFT) |
  752. (tv_mode->vi_end_f2 << TV_VI_END_F2_SHIFT);
  753. vctl2 = (tv_mode->vsync_len << TV_VSYNC_LEN_SHIFT) |
  754. (tv_mode->vsync_start_f1 << TV_VSYNC_START_F1_SHIFT) |
  755. (tv_mode->vsync_start_f2 << TV_VSYNC_START_F2_SHIFT);
  756. vctl3 = (tv_mode->veq_len << TV_VEQ_LEN_SHIFT) |
  757. (tv_mode->veq_start_f1 << TV_VEQ_START_F1_SHIFT) |
  758. (tv_mode->veq_start_f2 << TV_VEQ_START_F2_SHIFT);
  759. if (tv_mode->veq_ena)
  760. vctl3 |= TV_EQUAL_ENA;
  761. vctl4 = (tv_mode->vburst_start_f1 << TV_VBURST_START_F1_SHIFT) |
  762. (tv_mode->vburst_end_f1 << TV_VBURST_END_F1_SHIFT);
  763. vctl5 = (tv_mode->vburst_start_f2 << TV_VBURST_START_F2_SHIFT) |
  764. (tv_mode->vburst_end_f2 << TV_VBURST_END_F2_SHIFT);
  765. vctl6 = (tv_mode->vburst_start_f3 << TV_VBURST_START_F3_SHIFT) |
  766. (tv_mode->vburst_end_f3 << TV_VBURST_END_F3_SHIFT);
  767. vctl7 = (tv_mode->vburst_start_f4 << TV_VBURST_START_F4_SHIFT) |
  768. (tv_mode->vburst_end_f4 << TV_VBURST_END_F4_SHIFT);
  769. I915_WRITE(TV_H_CTL_1, hctl1);
  770. I915_WRITE(TV_H_CTL_2, hctl2);
  771. I915_WRITE(TV_H_CTL_3, hctl3);
  772. I915_WRITE(TV_V_CTL_1, vctl1);
  773. I915_WRITE(TV_V_CTL_2, vctl2);
  774. I915_WRITE(TV_V_CTL_3, vctl3);
  775. I915_WRITE(TV_V_CTL_4, vctl4);
  776. I915_WRITE(TV_V_CTL_5, vctl5);
  777. I915_WRITE(TV_V_CTL_6, vctl6);
  778. I915_WRITE(TV_V_CTL_7, vctl7);
  779. }
  780. static void set_color_conversion(struct drm_i915_private *dev_priv,
  781. const struct color_conversion *color_conversion)
  782. {
  783. if (!color_conversion)
  784. return;
  785. I915_WRITE(TV_CSC_Y, (color_conversion->ry << 16) |
  786. color_conversion->gy);
  787. I915_WRITE(TV_CSC_Y2, (color_conversion->by << 16) |
  788. color_conversion->ay);
  789. I915_WRITE(TV_CSC_U, (color_conversion->ru << 16) |
  790. color_conversion->gu);
  791. I915_WRITE(TV_CSC_U2, (color_conversion->bu << 16) |
  792. color_conversion->au);
  793. I915_WRITE(TV_CSC_V, (color_conversion->rv << 16) |
  794. color_conversion->gv);
  795. I915_WRITE(TV_CSC_V2, (color_conversion->bv << 16) |
  796. color_conversion->av);
  797. }
  798. static void intel_tv_pre_enable(struct intel_encoder *encoder,
  799. const struct intel_crtc_state *pipe_config,
  800. const struct drm_connector_state *conn_state)
  801. {
  802. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  803. struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
  804. struct intel_tv *intel_tv = enc_to_tv(encoder);
  805. const struct tv_mode *tv_mode = intel_tv_mode_find(conn_state);
  806. u32 tv_ctl;
  807. u32 scctl1, scctl2, scctl3;
  808. int i, j;
  809. const struct video_levels *video_levels;
  810. const struct color_conversion *color_conversion;
  811. bool burst_ena;
  812. int xpos = 0x0, ypos = 0x0;
  813. unsigned int xsize, ysize;
  814. if (!tv_mode)
  815. return; /* can't happen (mode_prepare prevents this) */
  816. tv_ctl = I915_READ(TV_CTL);
  817. tv_ctl &= TV_CTL_SAVE;
  818. switch (intel_tv->type) {
  819. default:
  820. case DRM_MODE_CONNECTOR_Unknown:
  821. case DRM_MODE_CONNECTOR_Composite:
  822. tv_ctl |= TV_ENC_OUTPUT_COMPOSITE;
  823. video_levels = tv_mode->composite_levels;
  824. color_conversion = tv_mode->composite_color;
  825. burst_ena = tv_mode->burst_ena;
  826. break;
  827. case DRM_MODE_CONNECTOR_Component:
  828. tv_ctl |= TV_ENC_OUTPUT_COMPONENT;
  829. video_levels = &component_levels;
  830. if (tv_mode->burst_ena)
  831. color_conversion = &sdtv_csc_yprpb;
  832. else
  833. color_conversion = &hdtv_csc_yprpb;
  834. burst_ena = false;
  835. break;
  836. case DRM_MODE_CONNECTOR_SVIDEO:
  837. tv_ctl |= TV_ENC_OUTPUT_SVIDEO;
  838. video_levels = tv_mode->svideo_levels;
  839. color_conversion = tv_mode->svideo_color;
  840. burst_ena = tv_mode->burst_ena;
  841. break;
  842. }
  843. if (intel_crtc->pipe == 1)
  844. tv_ctl |= TV_ENC_PIPEB_SELECT;
  845. tv_ctl |= tv_mode->oversample;
  846. if (tv_mode->progressive)
  847. tv_ctl |= TV_PROGRESSIVE;
  848. if (tv_mode->trilevel_sync)
  849. tv_ctl |= TV_TRILEVEL_SYNC;
  850. if (tv_mode->pal_burst)
  851. tv_ctl |= TV_PAL_BURST;
  852. scctl1 = 0;
  853. if (tv_mode->dda1_inc)
  854. scctl1 |= TV_SC_DDA1_EN;
  855. if (tv_mode->dda2_inc)
  856. scctl1 |= TV_SC_DDA2_EN;
  857. if (tv_mode->dda3_inc)
  858. scctl1 |= TV_SC_DDA3_EN;
  859. scctl1 |= tv_mode->sc_reset;
  860. if (video_levels)
  861. scctl1 |= video_levels->burst << TV_BURST_LEVEL_SHIFT;
  862. scctl1 |= tv_mode->dda1_inc << TV_SCDDA1_INC_SHIFT;
  863. scctl2 = tv_mode->dda2_size << TV_SCDDA2_SIZE_SHIFT |
  864. tv_mode->dda2_inc << TV_SCDDA2_INC_SHIFT;
  865. scctl3 = tv_mode->dda3_size << TV_SCDDA3_SIZE_SHIFT |
  866. tv_mode->dda3_inc << TV_SCDDA3_INC_SHIFT;
  867. /* Enable two fixes for the chips that need them. */
  868. if (IS_I915GM(dev_priv))
  869. tv_ctl |= TV_ENC_C0_FIX | TV_ENC_SDP_FIX;
  870. set_tv_mode_timings(dev_priv, tv_mode, burst_ena);
  871. I915_WRITE(TV_SC_CTL_1, scctl1);
  872. I915_WRITE(TV_SC_CTL_2, scctl2);
  873. I915_WRITE(TV_SC_CTL_3, scctl3);
  874. set_color_conversion(dev_priv, color_conversion);
  875. if (INTEL_GEN(dev_priv) >= 4)
  876. I915_WRITE(TV_CLR_KNOBS, 0x00404000);
  877. else
  878. I915_WRITE(TV_CLR_KNOBS, 0x00606000);
  879. if (video_levels)
  880. I915_WRITE(TV_CLR_LEVEL,
  881. ((video_levels->black << TV_BLACK_LEVEL_SHIFT) |
  882. (video_levels->blank << TV_BLANK_LEVEL_SHIFT)));
  883. assert_pipe_disabled(dev_priv, intel_crtc->pipe);
  884. /* Filter ctl must be set before TV_WIN_SIZE */
  885. I915_WRITE(TV_FILTER_CTL_1, TV_AUTO_SCALE);
  886. xsize = tv_mode->hblank_start - tv_mode->hblank_end;
  887. if (tv_mode->progressive)
  888. ysize = tv_mode->nbr_end + 1;
  889. else
  890. ysize = 2*tv_mode->nbr_end + 1;
  891. xpos += conn_state->tv.margins.left;
  892. ypos += conn_state->tv.margins.top;
  893. xsize -= (conn_state->tv.margins.left +
  894. conn_state->tv.margins.right);
  895. ysize -= (conn_state->tv.margins.top +
  896. conn_state->tv.margins.bottom);
  897. I915_WRITE(TV_WIN_POS, (xpos<<16)|ypos);
  898. I915_WRITE(TV_WIN_SIZE, (xsize<<16)|ysize);
  899. j = 0;
  900. for (i = 0; i < 60; i++)
  901. I915_WRITE(TV_H_LUMA(i), tv_mode->filter_table[j++]);
  902. for (i = 0; i < 60; i++)
  903. I915_WRITE(TV_H_CHROMA(i), tv_mode->filter_table[j++]);
  904. for (i = 0; i < 43; i++)
  905. I915_WRITE(TV_V_LUMA(i), tv_mode->filter_table[j++]);
  906. for (i = 0; i < 43; i++)
  907. I915_WRITE(TV_V_CHROMA(i), tv_mode->filter_table[j++]);
  908. I915_WRITE(TV_DAC, I915_READ(TV_DAC) & TV_DAC_SAVE);
  909. I915_WRITE(TV_CTL, tv_ctl);
  910. }
  911. static const struct drm_display_mode reported_modes[] = {
  912. {
  913. .name = "NTSC 480i",
  914. .clock = 107520,
  915. .hdisplay = 1280,
  916. .hsync_start = 1368,
  917. .hsync_end = 1496,
  918. .htotal = 1712,
  919. .vdisplay = 1024,
  920. .vsync_start = 1027,
  921. .vsync_end = 1034,
  922. .vtotal = 1104,
  923. .type = DRM_MODE_TYPE_DRIVER,
  924. },
  925. };
  926. /**
  927. * Detects TV presence by checking for load.
  928. *
  929. * Requires that the current pipe's DPLL is active.
  930. * \return true if TV is connected.
  931. * \return false if TV is disconnected.
  932. */
  933. static int
  934. intel_tv_detect_type(struct intel_tv *intel_tv,
  935. struct drm_connector *connector)
  936. {
  937. struct drm_crtc *crtc = connector->state->crtc;
  938. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  939. struct drm_device *dev = connector->dev;
  940. struct drm_i915_private *dev_priv = to_i915(dev);
  941. u32 tv_ctl, save_tv_ctl;
  942. u32 tv_dac, save_tv_dac;
  943. int type;
  944. /* Disable TV interrupts around load detect or we'll recurse */
  945. if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
  946. spin_lock_irq(&dev_priv->irq_lock);
  947. i915_disable_pipestat(dev_priv, 0,
  948. PIPE_HOTPLUG_INTERRUPT_STATUS |
  949. PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
  950. spin_unlock_irq(&dev_priv->irq_lock);
  951. }
  952. save_tv_dac = tv_dac = I915_READ(TV_DAC);
  953. save_tv_ctl = tv_ctl = I915_READ(TV_CTL);
  954. /* Poll for TV detection */
  955. tv_ctl &= ~(TV_ENC_ENABLE | TV_TEST_MODE_MASK);
  956. tv_ctl |= TV_TEST_MODE_MONITOR_DETECT;
  957. if (intel_crtc->pipe == 1)
  958. tv_ctl |= TV_ENC_PIPEB_SELECT;
  959. else
  960. tv_ctl &= ~TV_ENC_PIPEB_SELECT;
  961. tv_dac &= ~(TVDAC_SENSE_MASK | DAC_A_MASK | DAC_B_MASK | DAC_C_MASK);
  962. tv_dac |= (TVDAC_STATE_CHG_EN |
  963. TVDAC_A_SENSE_CTL |
  964. TVDAC_B_SENSE_CTL |
  965. TVDAC_C_SENSE_CTL |
  966. DAC_CTL_OVERRIDE |
  967. DAC_A_0_7_V |
  968. DAC_B_0_7_V |
  969. DAC_C_0_7_V);
  970. /*
  971. * The TV sense state should be cleared to zero on cantiga platform. Otherwise
  972. * the TV is misdetected. This is hardware requirement.
  973. */
  974. if (IS_GM45(dev_priv))
  975. tv_dac &= ~(TVDAC_STATE_CHG_EN | TVDAC_A_SENSE_CTL |
  976. TVDAC_B_SENSE_CTL | TVDAC_C_SENSE_CTL);
  977. I915_WRITE(TV_CTL, tv_ctl);
  978. I915_WRITE(TV_DAC, tv_dac);
  979. POSTING_READ(TV_DAC);
  980. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  981. type = -1;
  982. tv_dac = I915_READ(TV_DAC);
  983. DRM_DEBUG_KMS("TV detected: %x, %x\n", tv_ctl, tv_dac);
  984. /*
  985. * A B C
  986. * 0 1 1 Composite
  987. * 1 0 X svideo
  988. * 0 0 0 Component
  989. */
  990. if ((tv_dac & TVDAC_SENSE_MASK) == (TVDAC_B_SENSE | TVDAC_C_SENSE)) {
  991. DRM_DEBUG_KMS("Detected Composite TV connection\n");
  992. type = DRM_MODE_CONNECTOR_Composite;
  993. } else if ((tv_dac & (TVDAC_A_SENSE|TVDAC_B_SENSE)) == TVDAC_A_SENSE) {
  994. DRM_DEBUG_KMS("Detected S-Video TV connection\n");
  995. type = DRM_MODE_CONNECTOR_SVIDEO;
  996. } else if ((tv_dac & TVDAC_SENSE_MASK) == 0) {
  997. DRM_DEBUG_KMS("Detected Component TV connection\n");
  998. type = DRM_MODE_CONNECTOR_Component;
  999. } else {
  1000. DRM_DEBUG_KMS("Unrecognised TV connection\n");
  1001. type = -1;
  1002. }
  1003. I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
  1004. I915_WRITE(TV_CTL, save_tv_ctl);
  1005. POSTING_READ(TV_CTL);
  1006. /* For unknown reasons the hw barfs if we don't do this vblank wait. */
  1007. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  1008. /* Restore interrupt config */
  1009. if (connector->polled & DRM_CONNECTOR_POLL_HPD) {
  1010. spin_lock_irq(&dev_priv->irq_lock);
  1011. i915_enable_pipestat(dev_priv, 0,
  1012. PIPE_HOTPLUG_INTERRUPT_STATUS |
  1013. PIPE_HOTPLUG_TV_INTERRUPT_STATUS);
  1014. spin_unlock_irq(&dev_priv->irq_lock);
  1015. }
  1016. return type;
  1017. }
  1018. /*
  1019. * Here we set accurate tv format according to connector type
  1020. * i.e Component TV should not be assigned by NTSC or PAL
  1021. */
  1022. static void intel_tv_find_better_format(struct drm_connector *connector)
  1023. {
  1024. struct intel_tv *intel_tv = intel_attached_tv(connector);
  1025. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  1026. int i;
  1027. if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
  1028. tv_mode->component_only)
  1029. return;
  1030. for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
  1031. tv_mode = tv_modes + i;
  1032. if ((intel_tv->type == DRM_MODE_CONNECTOR_Component) ==
  1033. tv_mode->component_only)
  1034. break;
  1035. }
  1036. connector->state->tv.mode = i;
  1037. }
  1038. /**
  1039. * Detect the TV connection.
  1040. *
  1041. * Currently this always returns CONNECTOR_STATUS_UNKNOWN, as we need to be sure
  1042. * we have a pipe programmed in order to probe the TV.
  1043. */
  1044. static int
  1045. intel_tv_detect(struct drm_connector *connector,
  1046. struct drm_modeset_acquire_ctx *ctx,
  1047. bool force)
  1048. {
  1049. struct drm_display_mode mode;
  1050. struct intel_tv *intel_tv = intel_attached_tv(connector);
  1051. enum drm_connector_status status;
  1052. int type;
  1053. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  1054. connector->base.id, connector->name,
  1055. force);
  1056. mode = reported_modes[0];
  1057. if (force) {
  1058. struct intel_load_detect_pipe tmp;
  1059. int ret;
  1060. ret = intel_get_load_detect_pipe(connector, &mode, &tmp, ctx);
  1061. if (ret < 0)
  1062. return ret;
  1063. if (ret > 0) {
  1064. type = intel_tv_detect_type(intel_tv, connector);
  1065. intel_release_load_detect_pipe(connector, &tmp, ctx);
  1066. status = type < 0 ?
  1067. connector_status_disconnected :
  1068. connector_status_connected;
  1069. } else
  1070. status = connector_status_unknown;
  1071. if (status == connector_status_connected) {
  1072. intel_tv->type = type;
  1073. intel_tv_find_better_format(connector);
  1074. }
  1075. return status;
  1076. } else
  1077. return connector->status;
  1078. }
  1079. static const struct input_res {
  1080. const char *name;
  1081. int w, h;
  1082. } input_res_table[] = {
  1083. {"640x480", 640, 480},
  1084. {"800x600", 800, 600},
  1085. {"1024x768", 1024, 768},
  1086. {"1280x1024", 1280, 1024},
  1087. {"848x480", 848, 480},
  1088. {"1280x720", 1280, 720},
  1089. {"1920x1080", 1920, 1080},
  1090. };
  1091. /*
  1092. * Chose preferred mode according to line number of TV format
  1093. */
  1094. static void
  1095. intel_tv_choose_preferred_modes(const struct tv_mode *tv_mode,
  1096. struct drm_display_mode *mode_ptr)
  1097. {
  1098. if (tv_mode->nbr_end < 480 && mode_ptr->vdisplay == 480)
  1099. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1100. else if (tv_mode->nbr_end > 480) {
  1101. if (tv_mode->progressive == true && tv_mode->nbr_end < 720) {
  1102. if (mode_ptr->vdisplay == 720)
  1103. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1104. } else if (mode_ptr->vdisplay == 1080)
  1105. mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
  1106. }
  1107. }
  1108. /**
  1109. * Stub get_modes function.
  1110. *
  1111. * This should probably return a set of fixed modes, unless we can figure out
  1112. * how to probe modes off of TV connections.
  1113. */
  1114. static int
  1115. intel_tv_get_modes(struct drm_connector *connector)
  1116. {
  1117. struct drm_display_mode *mode_ptr;
  1118. const struct tv_mode *tv_mode = intel_tv_mode_find(connector->state);
  1119. int j, count = 0;
  1120. u64 tmp;
  1121. for (j = 0; j < ARRAY_SIZE(input_res_table);
  1122. j++) {
  1123. const struct input_res *input = &input_res_table[j];
  1124. unsigned int hactive_s = input->w;
  1125. unsigned int vactive_s = input->h;
  1126. if (tv_mode->max_srcw && input->w > tv_mode->max_srcw)
  1127. continue;
  1128. if (input->w > 1024 && (!tv_mode->progressive
  1129. && !tv_mode->component_only))
  1130. continue;
  1131. mode_ptr = drm_mode_create(connector->dev);
  1132. if (!mode_ptr)
  1133. continue;
  1134. strncpy(mode_ptr->name, input->name, DRM_DISPLAY_MODE_LEN);
  1135. mode_ptr->name[DRM_DISPLAY_MODE_LEN - 1] = '\0';
  1136. mode_ptr->hdisplay = hactive_s;
  1137. mode_ptr->hsync_start = hactive_s + 1;
  1138. mode_ptr->hsync_end = hactive_s + 64;
  1139. if (mode_ptr->hsync_end <= mode_ptr->hsync_start)
  1140. mode_ptr->hsync_end = mode_ptr->hsync_start + 1;
  1141. mode_ptr->htotal = hactive_s + 96;
  1142. mode_ptr->vdisplay = vactive_s;
  1143. mode_ptr->vsync_start = vactive_s + 1;
  1144. mode_ptr->vsync_end = vactive_s + 32;
  1145. if (mode_ptr->vsync_end <= mode_ptr->vsync_start)
  1146. mode_ptr->vsync_end = mode_ptr->vsync_start + 1;
  1147. mode_ptr->vtotal = vactive_s + 33;
  1148. tmp = mul_u32_u32(tv_mode->refresh, mode_ptr->vtotal);
  1149. tmp *= mode_ptr->htotal;
  1150. tmp = div_u64(tmp, 1000000);
  1151. mode_ptr->clock = (int) tmp;
  1152. mode_ptr->type = DRM_MODE_TYPE_DRIVER;
  1153. intel_tv_choose_preferred_modes(tv_mode, mode_ptr);
  1154. drm_mode_probed_add(connector, mode_ptr);
  1155. count++;
  1156. }
  1157. return count;
  1158. }
  1159. static void
  1160. intel_tv_destroy(struct drm_connector *connector)
  1161. {
  1162. drm_connector_cleanup(connector);
  1163. kfree(connector);
  1164. }
  1165. static const struct drm_connector_funcs intel_tv_connector_funcs = {
  1166. .late_register = intel_connector_register,
  1167. .early_unregister = intel_connector_unregister,
  1168. .destroy = intel_tv_destroy,
  1169. .fill_modes = drm_helper_probe_single_connector_modes,
  1170. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1171. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1172. };
  1173. static int intel_tv_atomic_check(struct drm_connector *connector,
  1174. struct drm_connector_state *new_state)
  1175. {
  1176. struct drm_crtc_state *new_crtc_state;
  1177. struct drm_connector_state *old_state;
  1178. if (!new_state->crtc)
  1179. return 0;
  1180. old_state = drm_atomic_get_old_connector_state(new_state->state, connector);
  1181. new_crtc_state = drm_atomic_get_new_crtc_state(new_state->state, new_state->crtc);
  1182. if (old_state->tv.mode != new_state->tv.mode ||
  1183. old_state->tv.margins.left != new_state->tv.margins.left ||
  1184. old_state->tv.margins.right != new_state->tv.margins.right ||
  1185. old_state->tv.margins.top != new_state->tv.margins.top ||
  1186. old_state->tv.margins.bottom != new_state->tv.margins.bottom) {
  1187. /* Force a modeset. */
  1188. new_crtc_state->connectors_changed = true;
  1189. }
  1190. return 0;
  1191. }
  1192. static const struct drm_connector_helper_funcs intel_tv_connector_helper_funcs = {
  1193. .detect_ctx = intel_tv_detect,
  1194. .mode_valid = intel_tv_mode_valid,
  1195. .get_modes = intel_tv_get_modes,
  1196. .atomic_check = intel_tv_atomic_check,
  1197. };
  1198. static const struct drm_encoder_funcs intel_tv_enc_funcs = {
  1199. .destroy = intel_encoder_destroy,
  1200. };
  1201. void
  1202. intel_tv_init(struct drm_i915_private *dev_priv)
  1203. {
  1204. struct drm_device *dev = &dev_priv->drm;
  1205. struct drm_connector *connector;
  1206. struct intel_tv *intel_tv;
  1207. struct intel_encoder *intel_encoder;
  1208. struct intel_connector *intel_connector;
  1209. u32 tv_dac_on, tv_dac_off, save_tv_dac;
  1210. const char *tv_format_names[ARRAY_SIZE(tv_modes)];
  1211. int i, initial_mode = 0;
  1212. struct drm_connector_state *state;
  1213. if ((I915_READ(TV_CTL) & TV_FUSE_STATE_MASK) == TV_FUSE_STATE_DISABLED)
  1214. return;
  1215. if (!intel_bios_is_tv_present(dev_priv)) {
  1216. DRM_DEBUG_KMS("Integrated TV is not present.\n");
  1217. return;
  1218. }
  1219. /*
  1220. * Sanity check the TV output by checking to see if the
  1221. * DAC register holds a value
  1222. */
  1223. save_tv_dac = I915_READ(TV_DAC);
  1224. I915_WRITE(TV_DAC, save_tv_dac | TVDAC_STATE_CHG_EN);
  1225. tv_dac_on = I915_READ(TV_DAC);
  1226. I915_WRITE(TV_DAC, save_tv_dac & ~TVDAC_STATE_CHG_EN);
  1227. tv_dac_off = I915_READ(TV_DAC);
  1228. I915_WRITE(TV_DAC, save_tv_dac);
  1229. /*
  1230. * If the register does not hold the state change enable
  1231. * bit, (either as a 0 or a 1), assume it doesn't really
  1232. * exist
  1233. */
  1234. if ((tv_dac_on & TVDAC_STATE_CHG_EN) == 0 ||
  1235. (tv_dac_off & TVDAC_STATE_CHG_EN) != 0)
  1236. return;
  1237. intel_tv = kzalloc(sizeof(*intel_tv), GFP_KERNEL);
  1238. if (!intel_tv) {
  1239. return;
  1240. }
  1241. intel_connector = intel_connector_alloc();
  1242. if (!intel_connector) {
  1243. kfree(intel_tv);
  1244. return;
  1245. }
  1246. intel_encoder = &intel_tv->base;
  1247. connector = &intel_connector->base;
  1248. state = connector->state;
  1249. /* The documentation, for the older chipsets at least, recommend
  1250. * using a polling method rather than hotplug detection for TVs.
  1251. * This is because in order to perform the hotplug detection, the PLLs
  1252. * for the TV must be kept alive increasing power drain and starving
  1253. * bandwidth from other encoders. Notably for instance, it causes
  1254. * pipe underruns on Crestline when this encoder is supposedly idle.
  1255. *
  1256. * More recent chipsets favour HDMI rather than integrated S-Video.
  1257. */
  1258. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  1259. drm_connector_init(dev, connector, &intel_tv_connector_funcs,
  1260. DRM_MODE_CONNECTOR_SVIDEO);
  1261. drm_encoder_init(dev, &intel_encoder->base, &intel_tv_enc_funcs,
  1262. DRM_MODE_ENCODER_TVDAC, "TV");
  1263. intel_encoder->compute_config = intel_tv_compute_config;
  1264. intel_encoder->get_config = intel_tv_get_config;
  1265. intel_encoder->pre_enable = intel_tv_pre_enable;
  1266. intel_encoder->enable = intel_enable_tv;
  1267. intel_encoder->disable = intel_disable_tv;
  1268. intel_encoder->get_hw_state = intel_tv_get_hw_state;
  1269. intel_connector->get_hw_state = intel_connector_get_hw_state;
  1270. intel_connector_attach_encoder(intel_connector, intel_encoder);
  1271. intel_encoder->type = INTEL_OUTPUT_TVOUT;
  1272. intel_encoder->power_domain = POWER_DOMAIN_PORT_OTHER;
  1273. intel_encoder->port = PORT_NONE;
  1274. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  1275. intel_encoder->cloneable = 0;
  1276. intel_encoder->base.possible_crtcs = ((1 << 0) | (1 << 1));
  1277. intel_tv->type = DRM_MODE_CONNECTOR_Unknown;
  1278. /* BIOS margin values */
  1279. state->tv.margins.left = 54;
  1280. state->tv.margins.top = 36;
  1281. state->tv.margins.right = 46;
  1282. state->tv.margins.bottom = 37;
  1283. state->tv.mode = initial_mode;
  1284. drm_connector_helper_add(connector, &intel_tv_connector_helper_funcs);
  1285. connector->interlace_allowed = false;
  1286. connector->doublescan_allowed = false;
  1287. /* Create TV properties then attach current values */
  1288. for (i = 0; i < ARRAY_SIZE(tv_modes); i++)
  1289. tv_format_names[i] = tv_modes[i].name;
  1290. drm_mode_create_tv_properties(dev,
  1291. ARRAY_SIZE(tv_modes),
  1292. tv_format_names);
  1293. drm_object_attach_property(&connector->base, dev->mode_config.tv_mode_property,
  1294. state->tv.mode);
  1295. drm_object_attach_property(&connector->base,
  1296. dev->mode_config.tv_left_margin_property,
  1297. state->tv.margins.left);
  1298. drm_object_attach_property(&connector->base,
  1299. dev->mode_config.tv_top_margin_property,
  1300. state->tv.margins.top);
  1301. drm_object_attach_property(&connector->base,
  1302. dev->mode_config.tv_right_margin_property,
  1303. state->tv.margins.right);
  1304. drm_object_attach_property(&connector->base,
  1305. dev->mode_config.tv_bottom_margin_property,
  1306. state->tv.margins.bottom);
  1307. }