intel_tv.c 48 KB

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