intel_sdvo.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768
  1. /*
  2. * Copyright 2006 Dave Airlie <airlied@linux.ie>
  3. * Copyright © 2006-2007 Intel Corporation
  4. * Jesse Barnes <jesse.barnes@intel.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. * Eric Anholt <eric@anholt.net>
  27. */
  28. #include <linux/i2c.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include <linux/export.h>
  32. #include <drm/drmP.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. #include "intel_sdvo_regs.h"
  39. #define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
  40. #define SDVO_RGB_MASK (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
  41. #define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
  42. #define SDVO_TV_MASK (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_YPRPB0)
  43. #define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
  44. SDVO_TV_MASK)
  45. #define IS_TV(c) (c->output_flag & SDVO_TV_MASK)
  46. #define IS_TMDS(c) (c->output_flag & SDVO_TMDS_MASK)
  47. #define IS_LVDS(c) (c->output_flag & SDVO_LVDS_MASK)
  48. #define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
  49. #define IS_DIGITAL(c) (c->output_flag & (SDVO_TMDS_MASK | SDVO_LVDS_MASK))
  50. static const char *tv_format_names[] = {
  51. "NTSC_M" , "NTSC_J" , "NTSC_443",
  52. "PAL_B" , "PAL_D" , "PAL_G" ,
  53. "PAL_H" , "PAL_I" , "PAL_M" ,
  54. "PAL_N" , "PAL_NC" , "PAL_60" ,
  55. "SECAM_B" , "SECAM_D" , "SECAM_G" ,
  56. "SECAM_K" , "SECAM_K1", "SECAM_L" ,
  57. "SECAM_60"
  58. };
  59. #define TV_FORMAT_NUM (sizeof(tv_format_names) / sizeof(*tv_format_names))
  60. struct intel_sdvo {
  61. struct intel_encoder base;
  62. struct i2c_adapter *i2c;
  63. u8 slave_addr;
  64. struct i2c_adapter ddc;
  65. /* Register for the SDVO device: SDVOB or SDVOC */
  66. uint32_t sdvo_reg;
  67. /* Active outputs controlled by this SDVO output */
  68. uint16_t controlled_output;
  69. /*
  70. * Capabilities of the SDVO device returned by
  71. * i830_sdvo_get_capabilities()
  72. */
  73. struct intel_sdvo_caps caps;
  74. /* Pixel clock limitations reported by the SDVO device, in kHz */
  75. int pixel_clock_min, pixel_clock_max;
  76. /*
  77. * For multiple function SDVO device,
  78. * this is for current attached outputs.
  79. */
  80. uint16_t attached_output;
  81. /*
  82. * Hotplug activation bits for this device
  83. */
  84. uint16_t hotplug_active;
  85. /**
  86. * This is used to select the color range of RBG outputs in HDMI mode.
  87. * It is only valid when using TMDS encoding and 8 bit per color mode.
  88. */
  89. uint32_t color_range;
  90. /**
  91. * This is set if we're going to treat the device as TV-out.
  92. *
  93. * While we have these nice friendly flags for output types that ought
  94. * to decide this for us, the S-Video output on our HDMI+S-Video card
  95. * shows up as RGB1 (VGA).
  96. */
  97. bool is_tv;
  98. /* On different gens SDVOB is at different places. */
  99. bool is_sdvob;
  100. /* This is for current tv format name */
  101. int tv_format_index;
  102. /**
  103. * This is set if we treat the device as HDMI, instead of DVI.
  104. */
  105. bool is_hdmi;
  106. bool has_hdmi_monitor;
  107. bool has_hdmi_audio;
  108. /**
  109. * This is set if we detect output of sdvo device as LVDS and
  110. * have a valid fixed mode to use with the panel.
  111. */
  112. bool is_lvds;
  113. /**
  114. * This is sdvo fixed pannel mode pointer
  115. */
  116. struct drm_display_mode *sdvo_lvds_fixed_mode;
  117. /* DDC bus used by this SDVO encoder */
  118. uint8_t ddc_bus;
  119. /*
  120. * the sdvo flag gets lost in round trip: dtd->adjusted_mode->dtd
  121. */
  122. uint8_t dtd_sdvo_flags;
  123. };
  124. struct intel_sdvo_connector {
  125. struct intel_connector base;
  126. /* Mark the type of connector */
  127. uint16_t output_flag;
  128. enum hdmi_force_audio force_audio;
  129. /* This contains all current supported TV format */
  130. u8 tv_format_supported[TV_FORMAT_NUM];
  131. int format_supported_num;
  132. struct drm_property *tv_format;
  133. /* add the property for the SDVO-TV */
  134. struct drm_property *left;
  135. struct drm_property *right;
  136. struct drm_property *top;
  137. struct drm_property *bottom;
  138. struct drm_property *hpos;
  139. struct drm_property *vpos;
  140. struct drm_property *contrast;
  141. struct drm_property *saturation;
  142. struct drm_property *hue;
  143. struct drm_property *sharpness;
  144. struct drm_property *flicker_filter;
  145. struct drm_property *flicker_filter_adaptive;
  146. struct drm_property *flicker_filter_2d;
  147. struct drm_property *tv_chroma_filter;
  148. struct drm_property *tv_luma_filter;
  149. struct drm_property *dot_crawl;
  150. /* add the property for the SDVO-TV/LVDS */
  151. struct drm_property *brightness;
  152. /* Add variable to record current setting for the above property */
  153. u32 left_margin, right_margin, top_margin, bottom_margin;
  154. /* this is to get the range of margin.*/
  155. u32 max_hscan, max_vscan;
  156. u32 max_hpos, cur_hpos;
  157. u32 max_vpos, cur_vpos;
  158. u32 cur_brightness, max_brightness;
  159. u32 cur_contrast, max_contrast;
  160. u32 cur_saturation, max_saturation;
  161. u32 cur_hue, max_hue;
  162. u32 cur_sharpness, max_sharpness;
  163. u32 cur_flicker_filter, max_flicker_filter;
  164. u32 cur_flicker_filter_adaptive, max_flicker_filter_adaptive;
  165. u32 cur_flicker_filter_2d, max_flicker_filter_2d;
  166. u32 cur_tv_chroma_filter, max_tv_chroma_filter;
  167. u32 cur_tv_luma_filter, max_tv_luma_filter;
  168. u32 cur_dot_crawl, max_dot_crawl;
  169. };
  170. static struct intel_sdvo *to_intel_sdvo(struct drm_encoder *encoder)
  171. {
  172. return container_of(encoder, struct intel_sdvo, base.base);
  173. }
  174. static struct intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
  175. {
  176. return container_of(intel_attached_encoder(connector),
  177. struct intel_sdvo, base);
  178. }
  179. static struct intel_sdvo_connector *to_intel_sdvo_connector(struct drm_connector *connector)
  180. {
  181. return container_of(to_intel_connector(connector), struct intel_sdvo_connector, base);
  182. }
  183. static bool
  184. intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags);
  185. static bool
  186. intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
  187. struct intel_sdvo_connector *intel_sdvo_connector,
  188. int type);
  189. static bool
  190. intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
  191. struct intel_sdvo_connector *intel_sdvo_connector);
  192. /**
  193. * Writes the SDVOB or SDVOC with the given value, but always writes both
  194. * SDVOB and SDVOC to work around apparent hardware issues (according to
  195. * comments in the BIOS).
  196. */
  197. static void intel_sdvo_write_sdvox(struct intel_sdvo *intel_sdvo, u32 val)
  198. {
  199. struct drm_device *dev = intel_sdvo->base.base.dev;
  200. struct drm_i915_private *dev_priv = dev->dev_private;
  201. u32 bval = val, cval = val;
  202. int i;
  203. if (intel_sdvo->sdvo_reg == PCH_SDVOB) {
  204. I915_WRITE(intel_sdvo->sdvo_reg, val);
  205. I915_READ(intel_sdvo->sdvo_reg);
  206. return;
  207. }
  208. if (intel_sdvo->sdvo_reg == SDVOB) {
  209. cval = I915_READ(SDVOC);
  210. } else {
  211. bval = I915_READ(SDVOB);
  212. }
  213. /*
  214. * Write the registers twice for luck. Sometimes,
  215. * writing them only once doesn't appear to 'stick'.
  216. * The BIOS does this too. Yay, magic
  217. */
  218. for (i = 0; i < 2; i++)
  219. {
  220. I915_WRITE(SDVOB, bval);
  221. I915_READ(SDVOB);
  222. I915_WRITE(SDVOC, cval);
  223. I915_READ(SDVOC);
  224. }
  225. }
  226. static bool intel_sdvo_read_byte(struct intel_sdvo *intel_sdvo, u8 addr, u8 *ch)
  227. {
  228. struct i2c_msg msgs[] = {
  229. {
  230. .addr = intel_sdvo->slave_addr,
  231. .flags = 0,
  232. .len = 1,
  233. .buf = &addr,
  234. },
  235. {
  236. .addr = intel_sdvo->slave_addr,
  237. .flags = I2C_M_RD,
  238. .len = 1,
  239. .buf = ch,
  240. }
  241. };
  242. int ret;
  243. if ((ret = i2c_transfer(intel_sdvo->i2c, msgs, 2)) == 2)
  244. return true;
  245. DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
  246. return false;
  247. }
  248. #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
  249. /** Mapping of command numbers to names, for debug output */
  250. static const struct _sdvo_cmd_name {
  251. u8 cmd;
  252. const char *name;
  253. } sdvo_cmd_names[] = {
  254. SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
  255. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
  256. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
  257. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
  258. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
  259. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
  260. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
  261. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
  262. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
  263. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
  264. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
  265. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
  266. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
  267. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
  268. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
  269. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
  270. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
  271. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  272. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
  273. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  274. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
  275. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
  276. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
  277. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
  278. SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
  279. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
  280. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
  281. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
  282. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
  283. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
  284. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
  285. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
  286. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
  287. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
  288. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
  289. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
  290. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
  291. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
  292. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
  293. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
  294. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
  295. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
  296. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
  297. /* Add the op code for SDVO enhancements */
  298. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
  299. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
  300. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
  301. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
  302. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
  303. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
  304. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
  305. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
  306. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
  307. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
  308. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
  309. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
  310. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
  311. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
  312. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
  313. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
  314. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
  315. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
  316. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
  317. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
  318. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
  319. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
  320. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
  321. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
  322. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
  323. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
  324. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
  325. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
  326. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
  327. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
  328. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
  329. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
  330. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
  331. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
  332. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
  333. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
  334. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
  335. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
  336. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
  337. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
  338. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
  339. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
  340. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
  341. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
  342. /* HDMI op code */
  343. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
  344. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
  345. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
  346. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
  347. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
  348. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
  349. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
  350. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
  351. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
  352. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
  353. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
  354. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
  355. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
  356. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
  357. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
  358. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
  359. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
  360. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
  361. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
  362. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
  363. };
  364. #define SDVO_NAME(svdo) ((svdo)->is_sdvob ? "SDVOB" : "SDVOC")
  365. static void intel_sdvo_debug_write(struct intel_sdvo *intel_sdvo, u8 cmd,
  366. const void *args, int args_len)
  367. {
  368. int i;
  369. DRM_DEBUG_KMS("%s: W: %02X ",
  370. SDVO_NAME(intel_sdvo), cmd);
  371. for (i = 0; i < args_len; i++)
  372. DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
  373. for (; i < 8; i++)
  374. DRM_LOG_KMS(" ");
  375. for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
  376. if (cmd == sdvo_cmd_names[i].cmd) {
  377. DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
  378. break;
  379. }
  380. }
  381. if (i == ARRAY_SIZE(sdvo_cmd_names))
  382. DRM_LOG_KMS("(%02X)", cmd);
  383. DRM_LOG_KMS("\n");
  384. }
  385. static const char *cmd_status_names[] = {
  386. "Power on",
  387. "Success",
  388. "Not supported",
  389. "Invalid arg",
  390. "Pending",
  391. "Target not specified",
  392. "Scaling not supported"
  393. };
  394. static bool intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
  395. const void *args, int args_len)
  396. {
  397. u8 *buf, status;
  398. struct i2c_msg *msgs;
  399. int i, ret = true;
  400. /* Would be simpler to allocate both in one go ? */
  401. buf = (u8 *)kzalloc(args_len * 2 + 2, GFP_KERNEL);
  402. if (!buf)
  403. return false;
  404. msgs = kcalloc(args_len + 3, sizeof(*msgs), GFP_KERNEL);
  405. if (!msgs) {
  406. kfree(buf);
  407. return false;
  408. }
  409. intel_sdvo_debug_write(intel_sdvo, cmd, args, args_len);
  410. for (i = 0; i < args_len; i++) {
  411. msgs[i].addr = intel_sdvo->slave_addr;
  412. msgs[i].flags = 0;
  413. msgs[i].len = 2;
  414. msgs[i].buf = buf + 2 *i;
  415. buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
  416. buf[2*i + 1] = ((u8*)args)[i];
  417. }
  418. msgs[i].addr = intel_sdvo->slave_addr;
  419. msgs[i].flags = 0;
  420. msgs[i].len = 2;
  421. msgs[i].buf = buf + 2*i;
  422. buf[2*i + 0] = SDVO_I2C_OPCODE;
  423. buf[2*i + 1] = cmd;
  424. /* the following two are to read the response */
  425. status = SDVO_I2C_CMD_STATUS;
  426. msgs[i+1].addr = intel_sdvo->slave_addr;
  427. msgs[i+1].flags = 0;
  428. msgs[i+1].len = 1;
  429. msgs[i+1].buf = &status;
  430. msgs[i+2].addr = intel_sdvo->slave_addr;
  431. msgs[i+2].flags = I2C_M_RD;
  432. msgs[i+2].len = 1;
  433. msgs[i+2].buf = &status;
  434. ret = i2c_transfer(intel_sdvo->i2c, msgs, i+3);
  435. if (ret < 0) {
  436. DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
  437. ret = false;
  438. goto out;
  439. }
  440. if (ret != i+3) {
  441. /* failure in I2C transfer */
  442. DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
  443. ret = false;
  444. }
  445. out:
  446. kfree(msgs);
  447. kfree(buf);
  448. return ret;
  449. }
  450. static bool intel_sdvo_read_response(struct intel_sdvo *intel_sdvo,
  451. void *response, int response_len)
  452. {
  453. u8 retry = 5;
  454. u8 status;
  455. int i;
  456. DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(intel_sdvo));
  457. /*
  458. * The documentation states that all commands will be
  459. * processed within 15µs, and that we need only poll
  460. * the status byte a maximum of 3 times in order for the
  461. * command to be complete.
  462. *
  463. * Check 5 times in case the hardware failed to read the docs.
  464. */
  465. if (!intel_sdvo_read_byte(intel_sdvo,
  466. SDVO_I2C_CMD_STATUS,
  467. &status))
  468. goto log_fail;
  469. while (status == SDVO_CMD_STATUS_PENDING && retry--) {
  470. udelay(15);
  471. if (!intel_sdvo_read_byte(intel_sdvo,
  472. SDVO_I2C_CMD_STATUS,
  473. &status))
  474. goto log_fail;
  475. }
  476. if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
  477. DRM_LOG_KMS("(%s)", cmd_status_names[status]);
  478. else
  479. DRM_LOG_KMS("(??? %d)", status);
  480. if (status != SDVO_CMD_STATUS_SUCCESS)
  481. goto log_fail;
  482. /* Read the command response */
  483. for (i = 0; i < response_len; i++) {
  484. if (!intel_sdvo_read_byte(intel_sdvo,
  485. SDVO_I2C_RETURN_0 + i,
  486. &((u8 *)response)[i]))
  487. goto log_fail;
  488. DRM_LOG_KMS(" %02X", ((u8 *)response)[i]);
  489. }
  490. DRM_LOG_KMS("\n");
  491. return true;
  492. log_fail:
  493. DRM_LOG_KMS("... failed\n");
  494. return false;
  495. }
  496. static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
  497. {
  498. if (mode->clock >= 100000)
  499. return 1;
  500. else if (mode->clock >= 50000)
  501. return 2;
  502. else
  503. return 4;
  504. }
  505. static bool intel_sdvo_set_control_bus_switch(struct intel_sdvo *intel_sdvo,
  506. u8 ddc_bus)
  507. {
  508. /* This must be the immediately preceding write before the i2c xfer */
  509. return intel_sdvo_write_cmd(intel_sdvo,
  510. SDVO_CMD_SET_CONTROL_BUS_SWITCH,
  511. &ddc_bus, 1);
  512. }
  513. static bool intel_sdvo_set_value(struct intel_sdvo *intel_sdvo, u8 cmd, const void *data, int len)
  514. {
  515. if (!intel_sdvo_write_cmd(intel_sdvo, cmd, data, len))
  516. return false;
  517. return intel_sdvo_read_response(intel_sdvo, NULL, 0);
  518. }
  519. static bool
  520. intel_sdvo_get_value(struct intel_sdvo *intel_sdvo, u8 cmd, void *value, int len)
  521. {
  522. if (!intel_sdvo_write_cmd(intel_sdvo, cmd, NULL, 0))
  523. return false;
  524. return intel_sdvo_read_response(intel_sdvo, value, len);
  525. }
  526. static bool intel_sdvo_set_target_input(struct intel_sdvo *intel_sdvo)
  527. {
  528. struct intel_sdvo_set_target_input_args targets = {0};
  529. return intel_sdvo_set_value(intel_sdvo,
  530. SDVO_CMD_SET_TARGET_INPUT,
  531. &targets, sizeof(targets));
  532. }
  533. /**
  534. * Return whether each input is trained.
  535. *
  536. * This function is making an assumption about the layout of the response,
  537. * which should be checked against the docs.
  538. */
  539. static bool intel_sdvo_get_trained_inputs(struct intel_sdvo *intel_sdvo, bool *input_1, bool *input_2)
  540. {
  541. struct intel_sdvo_get_trained_inputs_response response;
  542. BUILD_BUG_ON(sizeof(response) != 1);
  543. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
  544. &response, sizeof(response)))
  545. return false;
  546. *input_1 = response.input0_trained;
  547. *input_2 = response.input1_trained;
  548. return true;
  549. }
  550. static bool intel_sdvo_set_active_outputs(struct intel_sdvo *intel_sdvo,
  551. u16 outputs)
  552. {
  553. return intel_sdvo_set_value(intel_sdvo,
  554. SDVO_CMD_SET_ACTIVE_OUTPUTS,
  555. &outputs, sizeof(outputs));
  556. }
  557. static bool intel_sdvo_get_active_outputs(struct intel_sdvo *intel_sdvo,
  558. u16 *outputs)
  559. {
  560. return intel_sdvo_get_value(intel_sdvo,
  561. SDVO_CMD_GET_ACTIVE_OUTPUTS,
  562. outputs, sizeof(*outputs));
  563. }
  564. static bool intel_sdvo_set_encoder_power_state(struct intel_sdvo *intel_sdvo,
  565. int mode)
  566. {
  567. u8 state = SDVO_ENCODER_STATE_ON;
  568. switch (mode) {
  569. case DRM_MODE_DPMS_ON:
  570. state = SDVO_ENCODER_STATE_ON;
  571. break;
  572. case DRM_MODE_DPMS_STANDBY:
  573. state = SDVO_ENCODER_STATE_STANDBY;
  574. break;
  575. case DRM_MODE_DPMS_SUSPEND:
  576. state = SDVO_ENCODER_STATE_SUSPEND;
  577. break;
  578. case DRM_MODE_DPMS_OFF:
  579. state = SDVO_ENCODER_STATE_OFF;
  580. break;
  581. }
  582. return intel_sdvo_set_value(intel_sdvo,
  583. SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
  584. }
  585. static bool intel_sdvo_get_input_pixel_clock_range(struct intel_sdvo *intel_sdvo,
  586. int *clock_min,
  587. int *clock_max)
  588. {
  589. struct intel_sdvo_pixel_clock_range clocks;
  590. BUILD_BUG_ON(sizeof(clocks) != 4);
  591. if (!intel_sdvo_get_value(intel_sdvo,
  592. SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
  593. &clocks, sizeof(clocks)))
  594. return false;
  595. /* Convert the values from units of 10 kHz to kHz. */
  596. *clock_min = clocks.min * 10;
  597. *clock_max = clocks.max * 10;
  598. return true;
  599. }
  600. static bool intel_sdvo_set_target_output(struct intel_sdvo *intel_sdvo,
  601. u16 outputs)
  602. {
  603. return intel_sdvo_set_value(intel_sdvo,
  604. SDVO_CMD_SET_TARGET_OUTPUT,
  605. &outputs, sizeof(outputs));
  606. }
  607. static bool intel_sdvo_set_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
  608. struct intel_sdvo_dtd *dtd)
  609. {
  610. return intel_sdvo_set_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
  611. intel_sdvo_set_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
  612. }
  613. static bool intel_sdvo_set_input_timing(struct intel_sdvo *intel_sdvo,
  614. struct intel_sdvo_dtd *dtd)
  615. {
  616. return intel_sdvo_set_timing(intel_sdvo,
  617. SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
  618. }
  619. static bool intel_sdvo_set_output_timing(struct intel_sdvo *intel_sdvo,
  620. struct intel_sdvo_dtd *dtd)
  621. {
  622. return intel_sdvo_set_timing(intel_sdvo,
  623. SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
  624. }
  625. static bool
  626. intel_sdvo_create_preferred_input_timing(struct intel_sdvo *intel_sdvo,
  627. uint16_t clock,
  628. uint16_t width,
  629. uint16_t height)
  630. {
  631. struct intel_sdvo_preferred_input_timing_args args;
  632. memset(&args, 0, sizeof(args));
  633. args.clock = clock;
  634. args.width = width;
  635. args.height = height;
  636. args.interlace = 0;
  637. if (intel_sdvo->is_lvds &&
  638. (intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
  639. intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
  640. args.scaled = 1;
  641. return intel_sdvo_set_value(intel_sdvo,
  642. SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
  643. &args, sizeof(args));
  644. }
  645. static bool intel_sdvo_get_preferred_input_timing(struct intel_sdvo *intel_sdvo,
  646. struct intel_sdvo_dtd *dtd)
  647. {
  648. BUILD_BUG_ON(sizeof(dtd->part1) != 8);
  649. BUILD_BUG_ON(sizeof(dtd->part2) != 8);
  650. return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
  651. &dtd->part1, sizeof(dtd->part1)) &&
  652. intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
  653. &dtd->part2, sizeof(dtd->part2));
  654. }
  655. static bool intel_sdvo_set_clock_rate_mult(struct intel_sdvo *intel_sdvo, u8 val)
  656. {
  657. return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
  658. }
  659. static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
  660. const struct drm_display_mode *mode)
  661. {
  662. uint16_t width, height;
  663. uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
  664. uint16_t h_sync_offset, v_sync_offset;
  665. int mode_clock;
  666. width = mode->hdisplay;
  667. height = mode->vdisplay;
  668. /* do some mode translations */
  669. h_blank_len = mode->htotal - mode->hdisplay;
  670. h_sync_len = mode->hsync_end - mode->hsync_start;
  671. v_blank_len = mode->vtotal - mode->vdisplay;
  672. v_sync_len = mode->vsync_end - mode->vsync_start;
  673. h_sync_offset = mode->hsync_start - mode->hdisplay;
  674. v_sync_offset = mode->vsync_start - mode->vdisplay;
  675. mode_clock = mode->clock;
  676. mode_clock /= intel_mode_get_pixel_multiplier(mode) ?: 1;
  677. mode_clock /= 10;
  678. dtd->part1.clock = mode_clock;
  679. dtd->part1.h_active = width & 0xff;
  680. dtd->part1.h_blank = h_blank_len & 0xff;
  681. dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
  682. ((h_blank_len >> 8) & 0xf);
  683. dtd->part1.v_active = height & 0xff;
  684. dtd->part1.v_blank = v_blank_len & 0xff;
  685. dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
  686. ((v_blank_len >> 8) & 0xf);
  687. dtd->part2.h_sync_off = h_sync_offset & 0xff;
  688. dtd->part2.h_sync_width = h_sync_len & 0xff;
  689. dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
  690. (v_sync_len & 0xf);
  691. dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
  692. ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
  693. ((v_sync_len & 0x30) >> 4);
  694. dtd->part2.dtd_flags = 0x18;
  695. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  696. dtd->part2.dtd_flags |= DTD_FLAG_INTERLACE;
  697. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  698. dtd->part2.dtd_flags |= DTD_FLAG_HSYNC_POSITIVE;
  699. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  700. dtd->part2.dtd_flags |= DTD_FLAG_VSYNC_POSITIVE;
  701. dtd->part2.sdvo_flags = 0;
  702. dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
  703. dtd->part2.reserved = 0;
  704. }
  705. static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
  706. const struct intel_sdvo_dtd *dtd)
  707. {
  708. mode->hdisplay = dtd->part1.h_active;
  709. mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
  710. mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
  711. mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
  712. mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
  713. mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
  714. mode->htotal = mode->hdisplay + dtd->part1.h_blank;
  715. mode->htotal += (dtd->part1.h_high & 0xf) << 8;
  716. mode->vdisplay = dtd->part1.v_active;
  717. mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
  718. mode->vsync_start = mode->vdisplay;
  719. mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
  720. mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
  721. mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
  722. mode->vsync_end = mode->vsync_start +
  723. (dtd->part2.v_sync_off_width & 0xf);
  724. mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
  725. mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
  726. mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
  727. mode->clock = dtd->part1.clock * 10;
  728. mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
  729. if (dtd->part2.dtd_flags & DTD_FLAG_INTERLACE)
  730. mode->flags |= DRM_MODE_FLAG_INTERLACE;
  731. if (dtd->part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
  732. mode->flags |= DRM_MODE_FLAG_PHSYNC;
  733. if (dtd->part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
  734. mode->flags |= DRM_MODE_FLAG_PVSYNC;
  735. }
  736. static bool intel_sdvo_check_supp_encode(struct intel_sdvo *intel_sdvo)
  737. {
  738. struct intel_sdvo_encode encode;
  739. BUILD_BUG_ON(sizeof(encode) != 2);
  740. return intel_sdvo_get_value(intel_sdvo,
  741. SDVO_CMD_GET_SUPP_ENCODE,
  742. &encode, sizeof(encode));
  743. }
  744. static bool intel_sdvo_set_encode(struct intel_sdvo *intel_sdvo,
  745. uint8_t mode)
  746. {
  747. return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
  748. }
  749. static bool intel_sdvo_set_colorimetry(struct intel_sdvo *intel_sdvo,
  750. uint8_t mode)
  751. {
  752. return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
  753. }
  754. #if 0
  755. static void intel_sdvo_dump_hdmi_buf(struct intel_sdvo *intel_sdvo)
  756. {
  757. int i, j;
  758. uint8_t set_buf_index[2];
  759. uint8_t av_split;
  760. uint8_t buf_size;
  761. uint8_t buf[48];
  762. uint8_t *pos;
  763. intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
  764. for (i = 0; i <= av_split; i++) {
  765. set_buf_index[0] = i; set_buf_index[1] = 0;
  766. intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
  767. set_buf_index, 2);
  768. intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
  769. intel_sdvo_read_response(encoder, &buf_size, 1);
  770. pos = buf;
  771. for (j = 0; j <= buf_size; j += 8) {
  772. intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
  773. NULL, 0);
  774. intel_sdvo_read_response(encoder, pos, 8);
  775. pos += 8;
  776. }
  777. }
  778. }
  779. #endif
  780. static bool intel_sdvo_write_infoframe(struct intel_sdvo *intel_sdvo,
  781. unsigned if_index, uint8_t tx_rate,
  782. uint8_t *data, unsigned length)
  783. {
  784. uint8_t set_buf_index[2] = { if_index, 0 };
  785. uint8_t hbuf_size, tmp[8];
  786. int i;
  787. if (!intel_sdvo_set_value(intel_sdvo,
  788. SDVO_CMD_SET_HBUF_INDEX,
  789. set_buf_index, 2))
  790. return false;
  791. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HBUF_INFO,
  792. &hbuf_size, 1))
  793. return false;
  794. /* Buffer size is 0 based, hooray! */
  795. hbuf_size++;
  796. DRM_DEBUG_KMS("writing sdvo hbuf: %i, hbuf_size %i, hbuf_size: %i\n",
  797. if_index, length, hbuf_size);
  798. for (i = 0; i < hbuf_size; i += 8) {
  799. memset(tmp, 0, 8);
  800. if (i < length)
  801. memcpy(tmp, data + i, min_t(unsigned, 8, length - i));
  802. if (!intel_sdvo_set_value(intel_sdvo,
  803. SDVO_CMD_SET_HBUF_DATA,
  804. tmp, 8))
  805. return false;
  806. }
  807. return intel_sdvo_set_value(intel_sdvo,
  808. SDVO_CMD_SET_HBUF_TXRATE,
  809. &tx_rate, 1);
  810. }
  811. static bool intel_sdvo_set_avi_infoframe(struct intel_sdvo *intel_sdvo)
  812. {
  813. struct dip_infoframe avi_if = {
  814. .type = DIP_TYPE_AVI,
  815. .ver = DIP_VERSION_AVI,
  816. .len = DIP_LEN_AVI,
  817. };
  818. uint8_t sdvo_data[4 + sizeof(avi_if.body.avi)];
  819. intel_dip_infoframe_csum(&avi_if);
  820. /* sdvo spec says that the ecc is handled by the hw, and it looks like
  821. * we must not send the ecc field, either. */
  822. memcpy(sdvo_data, &avi_if, 3);
  823. sdvo_data[3] = avi_if.checksum;
  824. memcpy(&sdvo_data[4], &avi_if.body, sizeof(avi_if.body.avi));
  825. return intel_sdvo_write_infoframe(intel_sdvo, SDVO_HBUF_INDEX_AVI_IF,
  826. SDVO_HBUF_TX_VSYNC,
  827. sdvo_data, sizeof(sdvo_data));
  828. }
  829. static bool intel_sdvo_set_tv_format(struct intel_sdvo *intel_sdvo)
  830. {
  831. struct intel_sdvo_tv_format format;
  832. uint32_t format_map;
  833. format_map = 1 << intel_sdvo->tv_format_index;
  834. memset(&format, 0, sizeof(format));
  835. memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
  836. BUILD_BUG_ON(sizeof(format) != 6);
  837. return intel_sdvo_set_value(intel_sdvo,
  838. SDVO_CMD_SET_TV_FORMAT,
  839. &format, sizeof(format));
  840. }
  841. static bool
  842. intel_sdvo_set_output_timings_from_mode(struct intel_sdvo *intel_sdvo,
  843. const struct drm_display_mode *mode)
  844. {
  845. struct intel_sdvo_dtd output_dtd;
  846. if (!intel_sdvo_set_target_output(intel_sdvo,
  847. intel_sdvo->attached_output))
  848. return false;
  849. intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  850. if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
  851. return false;
  852. return true;
  853. }
  854. /* Asks the sdvo controller for the preferred input mode given the output mode.
  855. * Unfortunately we have to set up the full output mode to do that. */
  856. static bool
  857. intel_sdvo_get_preferred_input_mode(struct intel_sdvo *intel_sdvo,
  858. const struct drm_display_mode *mode,
  859. struct drm_display_mode *adjusted_mode)
  860. {
  861. struct intel_sdvo_dtd input_dtd;
  862. /* Reset the input timing to the screen. Assume always input 0. */
  863. if (!intel_sdvo_set_target_input(intel_sdvo))
  864. return false;
  865. if (!intel_sdvo_create_preferred_input_timing(intel_sdvo,
  866. mode->clock / 10,
  867. mode->hdisplay,
  868. mode->vdisplay))
  869. return false;
  870. if (!intel_sdvo_get_preferred_input_timing(intel_sdvo,
  871. &input_dtd))
  872. return false;
  873. intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
  874. intel_sdvo->dtd_sdvo_flags = input_dtd.part2.sdvo_flags;
  875. return true;
  876. }
  877. static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
  878. const struct drm_display_mode *mode,
  879. struct drm_display_mode *adjusted_mode)
  880. {
  881. struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  882. int multiplier;
  883. /* We need to construct preferred input timings based on our
  884. * output timings. To do that, we have to set the output
  885. * timings, even though this isn't really the right place in
  886. * the sequence to do it. Oh well.
  887. */
  888. if (intel_sdvo->is_tv) {
  889. if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
  890. return false;
  891. (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
  892. mode,
  893. adjusted_mode);
  894. } else if (intel_sdvo->is_lvds) {
  895. if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
  896. intel_sdvo->sdvo_lvds_fixed_mode))
  897. return false;
  898. (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
  899. mode,
  900. adjusted_mode);
  901. }
  902. /* Make the CRTC code factor in the SDVO pixel multiplier. The
  903. * SDVO device will factor out the multiplier during mode_set.
  904. */
  905. multiplier = intel_sdvo_get_pixel_multiplier(adjusted_mode);
  906. intel_mode_set_pixel_multiplier(adjusted_mode, multiplier);
  907. return true;
  908. }
  909. static void intel_sdvo_mode_set(struct drm_encoder *encoder,
  910. struct drm_display_mode *mode,
  911. struct drm_display_mode *adjusted_mode)
  912. {
  913. struct drm_device *dev = encoder->dev;
  914. struct drm_i915_private *dev_priv = dev->dev_private;
  915. struct drm_crtc *crtc = encoder->crtc;
  916. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  917. struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  918. u32 sdvox;
  919. struct intel_sdvo_in_out_map in_out;
  920. struct intel_sdvo_dtd input_dtd, output_dtd;
  921. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  922. int rate;
  923. if (!mode)
  924. return;
  925. /* First, set the input mapping for the first input to our controlled
  926. * output. This is only correct if we're a single-input device, in
  927. * which case the first input is the output from the appropriate SDVO
  928. * channel on the motherboard. In a two-input device, the first input
  929. * will be SDVOB and the second SDVOC.
  930. */
  931. in_out.in0 = intel_sdvo->attached_output;
  932. in_out.in1 = 0;
  933. intel_sdvo_set_value(intel_sdvo,
  934. SDVO_CMD_SET_IN_OUT_MAP,
  935. &in_out, sizeof(in_out));
  936. /* Set the output timings to the screen */
  937. if (!intel_sdvo_set_target_output(intel_sdvo,
  938. intel_sdvo->attached_output))
  939. return;
  940. /* lvds has a special fixed output timing. */
  941. if (intel_sdvo->is_lvds)
  942. intel_sdvo_get_dtd_from_mode(&output_dtd,
  943. intel_sdvo->sdvo_lvds_fixed_mode);
  944. else
  945. intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  946. if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
  947. DRM_INFO("Setting output timings on %s failed\n",
  948. SDVO_NAME(intel_sdvo));
  949. /* Set the input timing to the screen. Assume always input 0. */
  950. if (!intel_sdvo_set_target_input(intel_sdvo))
  951. return;
  952. if (intel_sdvo->has_hdmi_monitor) {
  953. intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
  954. intel_sdvo_set_colorimetry(intel_sdvo,
  955. SDVO_COLORIMETRY_RGB256);
  956. intel_sdvo_set_avi_infoframe(intel_sdvo);
  957. } else
  958. intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_DVI);
  959. if (intel_sdvo->is_tv &&
  960. !intel_sdvo_set_tv_format(intel_sdvo))
  961. return;
  962. /* We have tried to get input timing in mode_fixup, and filled into
  963. * adjusted_mode.
  964. */
  965. intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
  966. if (intel_sdvo->is_tv || intel_sdvo->is_lvds)
  967. input_dtd.part2.sdvo_flags = intel_sdvo->dtd_sdvo_flags;
  968. if (!intel_sdvo_set_input_timing(intel_sdvo, &input_dtd))
  969. DRM_INFO("Setting input timings on %s failed\n",
  970. SDVO_NAME(intel_sdvo));
  971. switch (pixel_multiplier) {
  972. default:
  973. case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
  974. case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
  975. case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
  976. }
  977. if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
  978. return;
  979. /* Set the SDVO control regs. */
  980. if (INTEL_INFO(dev)->gen >= 4) {
  981. /* The real mode polarity is set by the SDVO commands, using
  982. * struct intel_sdvo_dtd. */
  983. sdvox = SDVO_VSYNC_ACTIVE_HIGH | SDVO_HSYNC_ACTIVE_HIGH;
  984. if (intel_sdvo->is_hdmi)
  985. sdvox |= intel_sdvo->color_range;
  986. if (INTEL_INFO(dev)->gen < 5)
  987. sdvox |= SDVO_BORDER_ENABLE;
  988. } else {
  989. sdvox = I915_READ(intel_sdvo->sdvo_reg);
  990. switch (intel_sdvo->sdvo_reg) {
  991. case SDVOB:
  992. sdvox &= SDVOB_PRESERVE_MASK;
  993. break;
  994. case SDVOC:
  995. sdvox &= SDVOC_PRESERVE_MASK;
  996. break;
  997. }
  998. sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
  999. }
  1000. if (INTEL_PCH_TYPE(dev) >= PCH_CPT)
  1001. sdvox |= TRANSCODER_CPT(intel_crtc->pipe);
  1002. else
  1003. sdvox |= TRANSCODER(intel_crtc->pipe);
  1004. if (intel_sdvo->has_hdmi_audio)
  1005. sdvox |= SDVO_AUDIO_ENABLE;
  1006. if (INTEL_INFO(dev)->gen >= 4) {
  1007. /* done in crtc_mode_set as the dpll_md reg must be written early */
  1008. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  1009. /* done in crtc_mode_set as it lives inside the dpll register */
  1010. } else {
  1011. sdvox |= (pixel_multiplier - 1) << SDVO_PORT_MULTIPLY_SHIFT;
  1012. }
  1013. if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL &&
  1014. INTEL_INFO(dev)->gen < 5)
  1015. sdvox |= SDVO_STALL_SELECT;
  1016. intel_sdvo_write_sdvox(intel_sdvo, sdvox);
  1017. }
  1018. static bool intel_sdvo_connector_get_hw_state(struct intel_connector *connector)
  1019. {
  1020. struct intel_sdvo_connector *intel_sdvo_connector =
  1021. to_intel_sdvo_connector(&connector->base);
  1022. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(&connector->base);
  1023. u16 active_outputs;
  1024. intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
  1025. if (active_outputs & intel_sdvo_connector->output_flag)
  1026. return true;
  1027. else
  1028. return false;
  1029. }
  1030. static bool intel_sdvo_get_hw_state(struct intel_encoder *encoder,
  1031. enum pipe *pipe)
  1032. {
  1033. struct drm_device *dev = encoder->base.dev;
  1034. struct drm_i915_private *dev_priv = dev->dev_private;
  1035. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1036. u32 tmp;
  1037. tmp = I915_READ(intel_sdvo->sdvo_reg);
  1038. if (!(tmp & SDVO_ENABLE))
  1039. return false;
  1040. if (HAS_PCH_CPT(dev))
  1041. *pipe = PORT_TO_PIPE_CPT(tmp);
  1042. else
  1043. *pipe = PORT_TO_PIPE(tmp);
  1044. return true;
  1045. }
  1046. static void intel_disable_sdvo(struct intel_encoder *encoder)
  1047. {
  1048. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  1049. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1050. u32 temp;
  1051. intel_sdvo_set_active_outputs(intel_sdvo, 0);
  1052. if (0)
  1053. intel_sdvo_set_encoder_power_state(intel_sdvo,
  1054. DRM_MODE_DPMS_OFF);
  1055. temp = I915_READ(intel_sdvo->sdvo_reg);
  1056. if ((temp & SDVO_ENABLE) != 0) {
  1057. intel_sdvo_write_sdvox(intel_sdvo, temp & ~SDVO_ENABLE);
  1058. }
  1059. }
  1060. static void intel_enable_sdvo(struct intel_encoder *encoder)
  1061. {
  1062. struct drm_device *dev = encoder->base.dev;
  1063. struct drm_i915_private *dev_priv = dev->dev_private;
  1064. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1065. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  1066. u32 temp;
  1067. bool input1, input2;
  1068. int i;
  1069. u8 status;
  1070. temp = I915_READ(intel_sdvo->sdvo_reg);
  1071. if ((temp & SDVO_ENABLE) == 0)
  1072. intel_sdvo_write_sdvox(intel_sdvo, temp | SDVO_ENABLE);
  1073. for (i = 0; i < 2; i++)
  1074. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1075. status = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
  1076. /* Warn if the device reported failure to sync.
  1077. * A lot of SDVO devices fail to notify of sync, but it's
  1078. * a given it the status is a success, we succeeded.
  1079. */
  1080. if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
  1081. DRM_DEBUG_KMS("First %s output reported failure to "
  1082. "sync\n", SDVO_NAME(intel_sdvo));
  1083. }
  1084. if (0)
  1085. intel_sdvo_set_encoder_power_state(intel_sdvo,
  1086. DRM_MODE_DPMS_ON);
  1087. intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
  1088. }
  1089. static void intel_sdvo_dpms(struct drm_connector *connector, int mode)
  1090. {
  1091. struct drm_crtc *crtc;
  1092. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1093. /* dvo supports only 2 dpms states. */
  1094. if (mode != DRM_MODE_DPMS_ON)
  1095. mode = DRM_MODE_DPMS_OFF;
  1096. if (mode == connector->dpms)
  1097. return;
  1098. connector->dpms = mode;
  1099. /* Only need to change hw state when actually enabled */
  1100. crtc = intel_sdvo->base.base.crtc;
  1101. if (!crtc) {
  1102. intel_sdvo->base.connectors_active = false;
  1103. return;
  1104. }
  1105. if (mode != DRM_MODE_DPMS_ON) {
  1106. intel_sdvo_set_active_outputs(intel_sdvo, 0);
  1107. if (0)
  1108. intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
  1109. intel_sdvo->base.connectors_active = false;
  1110. intel_crtc_update_dpms(crtc);
  1111. } else {
  1112. intel_sdvo->base.connectors_active = true;
  1113. intel_crtc_update_dpms(crtc);
  1114. if (0)
  1115. intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
  1116. intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
  1117. }
  1118. intel_modeset_check_state(connector->dev);
  1119. }
  1120. static int intel_sdvo_mode_valid(struct drm_connector *connector,
  1121. struct drm_display_mode *mode)
  1122. {
  1123. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1124. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1125. return MODE_NO_DBLESCAN;
  1126. if (intel_sdvo->pixel_clock_min > mode->clock)
  1127. return MODE_CLOCK_LOW;
  1128. if (intel_sdvo->pixel_clock_max < mode->clock)
  1129. return MODE_CLOCK_HIGH;
  1130. if (intel_sdvo->is_lvds) {
  1131. if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
  1132. return MODE_PANEL;
  1133. if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
  1134. return MODE_PANEL;
  1135. }
  1136. return MODE_OK;
  1137. }
  1138. static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
  1139. {
  1140. BUILD_BUG_ON(sizeof(*caps) != 8);
  1141. if (!intel_sdvo_get_value(intel_sdvo,
  1142. SDVO_CMD_GET_DEVICE_CAPS,
  1143. caps, sizeof(*caps)))
  1144. return false;
  1145. DRM_DEBUG_KMS("SDVO capabilities:\n"
  1146. " vendor_id: %d\n"
  1147. " device_id: %d\n"
  1148. " device_rev_id: %d\n"
  1149. " sdvo_version_major: %d\n"
  1150. " sdvo_version_minor: %d\n"
  1151. " sdvo_inputs_mask: %d\n"
  1152. " smooth_scaling: %d\n"
  1153. " sharp_scaling: %d\n"
  1154. " up_scaling: %d\n"
  1155. " down_scaling: %d\n"
  1156. " stall_support: %d\n"
  1157. " output_flags: %d\n",
  1158. caps->vendor_id,
  1159. caps->device_id,
  1160. caps->device_rev_id,
  1161. caps->sdvo_version_major,
  1162. caps->sdvo_version_minor,
  1163. caps->sdvo_inputs_mask,
  1164. caps->smooth_scaling,
  1165. caps->sharp_scaling,
  1166. caps->up_scaling,
  1167. caps->down_scaling,
  1168. caps->stall_support,
  1169. caps->output_flags);
  1170. return true;
  1171. }
  1172. static uint16_t intel_sdvo_get_hotplug_support(struct intel_sdvo *intel_sdvo)
  1173. {
  1174. struct drm_device *dev = intel_sdvo->base.base.dev;
  1175. uint16_t hotplug;
  1176. /* HW Erratum: SDVO Hotplug is broken on all i945G chips, there's noise
  1177. * on the line. */
  1178. if (IS_I945G(dev) || IS_I945GM(dev))
  1179. return 0;
  1180. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
  1181. &hotplug, sizeof(hotplug)))
  1182. return 0;
  1183. return hotplug;
  1184. }
  1185. static void intel_sdvo_enable_hotplug(struct intel_encoder *encoder)
  1186. {
  1187. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1188. intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG,
  1189. &intel_sdvo->hotplug_active, 2);
  1190. }
  1191. static bool
  1192. intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
  1193. {
  1194. /* Is there more than one type of output? */
  1195. return hweight16(intel_sdvo->caps.output_flags) > 1;
  1196. }
  1197. static struct edid *
  1198. intel_sdvo_get_edid(struct drm_connector *connector)
  1199. {
  1200. struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
  1201. return drm_get_edid(connector, &sdvo->ddc);
  1202. }
  1203. /* Mac mini hack -- use the same DDC as the analog connector */
  1204. static struct edid *
  1205. intel_sdvo_get_analog_edid(struct drm_connector *connector)
  1206. {
  1207. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1208. return drm_get_edid(connector,
  1209. intel_gmbus_get_adapter(dev_priv,
  1210. dev_priv->crt_ddc_pin));
  1211. }
  1212. static enum drm_connector_status
  1213. intel_sdvo_tmds_sink_detect(struct drm_connector *connector)
  1214. {
  1215. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1216. enum drm_connector_status status;
  1217. struct edid *edid;
  1218. edid = intel_sdvo_get_edid(connector);
  1219. if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
  1220. u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
  1221. /*
  1222. * Don't use the 1 as the argument of DDC bus switch to get
  1223. * the EDID. It is used for SDVO SPD ROM.
  1224. */
  1225. for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
  1226. intel_sdvo->ddc_bus = ddc;
  1227. edid = intel_sdvo_get_edid(connector);
  1228. if (edid)
  1229. break;
  1230. }
  1231. /*
  1232. * If we found the EDID on the other bus,
  1233. * assume that is the correct DDC bus.
  1234. */
  1235. if (edid == NULL)
  1236. intel_sdvo->ddc_bus = saved_ddc;
  1237. }
  1238. /*
  1239. * When there is no edid and no monitor is connected with VGA
  1240. * port, try to use the CRT ddc to read the EDID for DVI-connector.
  1241. */
  1242. if (edid == NULL)
  1243. edid = intel_sdvo_get_analog_edid(connector);
  1244. status = connector_status_unknown;
  1245. if (edid != NULL) {
  1246. /* DDC bus is shared, match EDID to connector type */
  1247. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  1248. status = connector_status_connected;
  1249. if (intel_sdvo->is_hdmi) {
  1250. intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
  1251. intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
  1252. }
  1253. } else
  1254. status = connector_status_disconnected;
  1255. kfree(edid);
  1256. }
  1257. if (status == connector_status_connected) {
  1258. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1259. if (intel_sdvo_connector->force_audio != HDMI_AUDIO_AUTO)
  1260. intel_sdvo->has_hdmi_audio = (intel_sdvo_connector->force_audio == HDMI_AUDIO_ON);
  1261. }
  1262. return status;
  1263. }
  1264. static bool
  1265. intel_sdvo_connector_matches_edid(struct intel_sdvo_connector *sdvo,
  1266. struct edid *edid)
  1267. {
  1268. bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
  1269. bool connector_is_digital = !!IS_DIGITAL(sdvo);
  1270. DRM_DEBUG_KMS("connector_is_digital? %d, monitor_is_digital? %d\n",
  1271. connector_is_digital, monitor_is_digital);
  1272. return connector_is_digital == monitor_is_digital;
  1273. }
  1274. static enum drm_connector_status
  1275. intel_sdvo_detect(struct drm_connector *connector, bool force)
  1276. {
  1277. uint16_t response;
  1278. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1279. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1280. enum drm_connector_status ret;
  1281. if (!intel_sdvo_write_cmd(intel_sdvo,
  1282. SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0))
  1283. return connector_status_unknown;
  1284. /* add 30ms delay when the output type might be TV */
  1285. if (intel_sdvo->caps.output_flags & SDVO_TV_MASK)
  1286. msleep(30);
  1287. if (!intel_sdvo_read_response(intel_sdvo, &response, 2))
  1288. return connector_status_unknown;
  1289. DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
  1290. response & 0xff, response >> 8,
  1291. intel_sdvo_connector->output_flag);
  1292. if (response == 0)
  1293. return connector_status_disconnected;
  1294. intel_sdvo->attached_output = response;
  1295. intel_sdvo->has_hdmi_monitor = false;
  1296. intel_sdvo->has_hdmi_audio = false;
  1297. if ((intel_sdvo_connector->output_flag & response) == 0)
  1298. ret = connector_status_disconnected;
  1299. else if (IS_TMDS(intel_sdvo_connector))
  1300. ret = intel_sdvo_tmds_sink_detect(connector);
  1301. else {
  1302. struct edid *edid;
  1303. /* if we have an edid check it matches the connection */
  1304. edid = intel_sdvo_get_edid(connector);
  1305. if (edid == NULL)
  1306. edid = intel_sdvo_get_analog_edid(connector);
  1307. if (edid != NULL) {
  1308. if (intel_sdvo_connector_matches_edid(intel_sdvo_connector,
  1309. edid))
  1310. ret = connector_status_connected;
  1311. else
  1312. ret = connector_status_disconnected;
  1313. kfree(edid);
  1314. } else
  1315. ret = connector_status_connected;
  1316. }
  1317. /* May update encoder flag for like clock for SDVO TV, etc.*/
  1318. if (ret == connector_status_connected) {
  1319. intel_sdvo->is_tv = false;
  1320. intel_sdvo->is_lvds = false;
  1321. intel_sdvo->base.needs_tv_clock = false;
  1322. if (response & SDVO_TV_MASK) {
  1323. intel_sdvo->is_tv = true;
  1324. intel_sdvo->base.needs_tv_clock = true;
  1325. }
  1326. if (response & SDVO_LVDS_MASK)
  1327. intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
  1328. }
  1329. return ret;
  1330. }
  1331. static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
  1332. {
  1333. struct edid *edid;
  1334. /* set the bus switch and get the modes */
  1335. edid = intel_sdvo_get_edid(connector);
  1336. /*
  1337. * Mac mini hack. On this device, the DVI-I connector shares one DDC
  1338. * link between analog and digital outputs. So, if the regular SDVO
  1339. * DDC fails, check to see if the analog output is disconnected, in
  1340. * which case we'll look there for the digital DDC data.
  1341. */
  1342. if (edid == NULL)
  1343. edid = intel_sdvo_get_analog_edid(connector);
  1344. if (edid != NULL) {
  1345. if (intel_sdvo_connector_matches_edid(to_intel_sdvo_connector(connector),
  1346. edid)) {
  1347. drm_mode_connector_update_edid_property(connector, edid);
  1348. drm_add_edid_modes(connector, edid);
  1349. }
  1350. kfree(edid);
  1351. }
  1352. }
  1353. /*
  1354. * Set of SDVO TV modes.
  1355. * Note! This is in reply order (see loop in get_tv_modes).
  1356. * XXX: all 60Hz refresh?
  1357. */
  1358. static const struct drm_display_mode sdvo_tv_modes[] = {
  1359. { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
  1360. 416, 0, 200, 201, 232, 233, 0,
  1361. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1362. { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
  1363. 416, 0, 240, 241, 272, 273, 0,
  1364. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1365. { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
  1366. 496, 0, 300, 301, 332, 333, 0,
  1367. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1368. { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
  1369. 736, 0, 350, 351, 382, 383, 0,
  1370. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1371. { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
  1372. 736, 0, 400, 401, 432, 433, 0,
  1373. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1374. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
  1375. 736, 0, 480, 481, 512, 513, 0,
  1376. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1377. { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
  1378. 800, 0, 480, 481, 512, 513, 0,
  1379. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1380. { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
  1381. 800, 0, 576, 577, 608, 609, 0,
  1382. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1383. { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
  1384. 816, 0, 350, 351, 382, 383, 0,
  1385. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1386. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
  1387. 816, 0, 400, 401, 432, 433, 0,
  1388. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1389. { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
  1390. 816, 0, 480, 481, 512, 513, 0,
  1391. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1392. { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
  1393. 816, 0, 540, 541, 572, 573, 0,
  1394. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1395. { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
  1396. 816, 0, 576, 577, 608, 609, 0,
  1397. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1398. { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
  1399. 864, 0, 576, 577, 608, 609, 0,
  1400. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1401. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
  1402. 896, 0, 600, 601, 632, 633, 0,
  1403. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1404. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
  1405. 928, 0, 624, 625, 656, 657, 0,
  1406. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1407. { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
  1408. 1016, 0, 766, 767, 798, 799, 0,
  1409. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1410. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
  1411. 1120, 0, 768, 769, 800, 801, 0,
  1412. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1413. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
  1414. 1376, 0, 1024, 1025, 1056, 1057, 0,
  1415. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1416. };
  1417. static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
  1418. {
  1419. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1420. struct intel_sdvo_sdtv_resolution_request tv_res;
  1421. uint32_t reply = 0, format_map = 0;
  1422. int i;
  1423. /* Read the list of supported input resolutions for the selected TV
  1424. * format.
  1425. */
  1426. format_map = 1 << intel_sdvo->tv_format_index;
  1427. memcpy(&tv_res, &format_map,
  1428. min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
  1429. if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
  1430. return;
  1431. BUILD_BUG_ON(sizeof(tv_res) != 3);
  1432. if (!intel_sdvo_write_cmd(intel_sdvo,
  1433. SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
  1434. &tv_res, sizeof(tv_res)))
  1435. return;
  1436. if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
  1437. return;
  1438. for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
  1439. if (reply & (1 << i)) {
  1440. struct drm_display_mode *nmode;
  1441. nmode = drm_mode_duplicate(connector->dev,
  1442. &sdvo_tv_modes[i]);
  1443. if (nmode)
  1444. drm_mode_probed_add(connector, nmode);
  1445. }
  1446. }
  1447. static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
  1448. {
  1449. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1450. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1451. struct drm_display_mode *newmode;
  1452. /*
  1453. * Attempt to get the mode list from DDC.
  1454. * Assume that the preferred modes are
  1455. * arranged in priority order.
  1456. */
  1457. intel_ddc_get_modes(connector, intel_sdvo->i2c);
  1458. if (list_empty(&connector->probed_modes) == false)
  1459. goto end;
  1460. /* Fetch modes from VBT */
  1461. if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
  1462. newmode = drm_mode_duplicate(connector->dev,
  1463. dev_priv->sdvo_lvds_vbt_mode);
  1464. if (newmode != NULL) {
  1465. /* Guarantee the mode is preferred */
  1466. newmode->type = (DRM_MODE_TYPE_PREFERRED |
  1467. DRM_MODE_TYPE_DRIVER);
  1468. drm_mode_probed_add(connector, newmode);
  1469. }
  1470. }
  1471. end:
  1472. list_for_each_entry(newmode, &connector->probed_modes, head) {
  1473. if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
  1474. intel_sdvo->sdvo_lvds_fixed_mode =
  1475. drm_mode_duplicate(connector->dev, newmode);
  1476. intel_sdvo->is_lvds = true;
  1477. break;
  1478. }
  1479. }
  1480. }
  1481. static int intel_sdvo_get_modes(struct drm_connector *connector)
  1482. {
  1483. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1484. if (IS_TV(intel_sdvo_connector))
  1485. intel_sdvo_get_tv_modes(connector);
  1486. else if (IS_LVDS(intel_sdvo_connector))
  1487. intel_sdvo_get_lvds_modes(connector);
  1488. else
  1489. intel_sdvo_get_ddc_modes(connector);
  1490. return !list_empty(&connector->probed_modes);
  1491. }
  1492. static void
  1493. intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
  1494. {
  1495. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1496. struct drm_device *dev = connector->dev;
  1497. if (intel_sdvo_connector->left)
  1498. drm_property_destroy(dev, intel_sdvo_connector->left);
  1499. if (intel_sdvo_connector->right)
  1500. drm_property_destroy(dev, intel_sdvo_connector->right);
  1501. if (intel_sdvo_connector->top)
  1502. drm_property_destroy(dev, intel_sdvo_connector->top);
  1503. if (intel_sdvo_connector->bottom)
  1504. drm_property_destroy(dev, intel_sdvo_connector->bottom);
  1505. if (intel_sdvo_connector->hpos)
  1506. drm_property_destroy(dev, intel_sdvo_connector->hpos);
  1507. if (intel_sdvo_connector->vpos)
  1508. drm_property_destroy(dev, intel_sdvo_connector->vpos);
  1509. if (intel_sdvo_connector->saturation)
  1510. drm_property_destroy(dev, intel_sdvo_connector->saturation);
  1511. if (intel_sdvo_connector->contrast)
  1512. drm_property_destroy(dev, intel_sdvo_connector->contrast);
  1513. if (intel_sdvo_connector->hue)
  1514. drm_property_destroy(dev, intel_sdvo_connector->hue);
  1515. if (intel_sdvo_connector->sharpness)
  1516. drm_property_destroy(dev, intel_sdvo_connector->sharpness);
  1517. if (intel_sdvo_connector->flicker_filter)
  1518. drm_property_destroy(dev, intel_sdvo_connector->flicker_filter);
  1519. if (intel_sdvo_connector->flicker_filter_2d)
  1520. drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_2d);
  1521. if (intel_sdvo_connector->flicker_filter_adaptive)
  1522. drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_adaptive);
  1523. if (intel_sdvo_connector->tv_luma_filter)
  1524. drm_property_destroy(dev, intel_sdvo_connector->tv_luma_filter);
  1525. if (intel_sdvo_connector->tv_chroma_filter)
  1526. drm_property_destroy(dev, intel_sdvo_connector->tv_chroma_filter);
  1527. if (intel_sdvo_connector->dot_crawl)
  1528. drm_property_destroy(dev, intel_sdvo_connector->dot_crawl);
  1529. if (intel_sdvo_connector->brightness)
  1530. drm_property_destroy(dev, intel_sdvo_connector->brightness);
  1531. }
  1532. static void intel_sdvo_destroy(struct drm_connector *connector)
  1533. {
  1534. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1535. if (intel_sdvo_connector->tv_format)
  1536. drm_property_destroy(connector->dev,
  1537. intel_sdvo_connector->tv_format);
  1538. intel_sdvo_destroy_enhance_property(connector);
  1539. drm_sysfs_connector_remove(connector);
  1540. drm_connector_cleanup(connector);
  1541. kfree(connector);
  1542. }
  1543. static bool intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
  1544. {
  1545. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1546. struct edid *edid;
  1547. bool has_audio = false;
  1548. if (!intel_sdvo->is_hdmi)
  1549. return false;
  1550. edid = intel_sdvo_get_edid(connector);
  1551. if (edid != NULL && edid->input & DRM_EDID_INPUT_DIGITAL)
  1552. has_audio = drm_detect_monitor_audio(edid);
  1553. kfree(edid);
  1554. return has_audio;
  1555. }
  1556. static int
  1557. intel_sdvo_set_property(struct drm_connector *connector,
  1558. struct drm_property *property,
  1559. uint64_t val)
  1560. {
  1561. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1562. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1563. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1564. uint16_t temp_value;
  1565. uint8_t cmd;
  1566. int ret;
  1567. ret = drm_connector_property_set_value(connector, property, val);
  1568. if (ret)
  1569. return ret;
  1570. if (property == dev_priv->force_audio_property) {
  1571. int i = val;
  1572. bool has_audio;
  1573. if (i == intel_sdvo_connector->force_audio)
  1574. return 0;
  1575. intel_sdvo_connector->force_audio = i;
  1576. if (i == HDMI_AUDIO_AUTO)
  1577. has_audio = intel_sdvo_detect_hdmi_audio(connector);
  1578. else
  1579. has_audio = (i == HDMI_AUDIO_ON);
  1580. if (has_audio == intel_sdvo->has_hdmi_audio)
  1581. return 0;
  1582. intel_sdvo->has_hdmi_audio = has_audio;
  1583. goto done;
  1584. }
  1585. if (property == dev_priv->broadcast_rgb_property) {
  1586. if (val == !!intel_sdvo->color_range)
  1587. return 0;
  1588. intel_sdvo->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
  1589. goto done;
  1590. }
  1591. #define CHECK_PROPERTY(name, NAME) \
  1592. if (intel_sdvo_connector->name == property) { \
  1593. if (intel_sdvo_connector->cur_##name == temp_value) return 0; \
  1594. if (intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
  1595. cmd = SDVO_CMD_SET_##NAME; \
  1596. intel_sdvo_connector->cur_##name = temp_value; \
  1597. goto set_value; \
  1598. }
  1599. if (property == intel_sdvo_connector->tv_format) {
  1600. if (val >= TV_FORMAT_NUM)
  1601. return -EINVAL;
  1602. if (intel_sdvo->tv_format_index ==
  1603. intel_sdvo_connector->tv_format_supported[val])
  1604. return 0;
  1605. intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[val];
  1606. goto done;
  1607. } else if (IS_TV_OR_LVDS(intel_sdvo_connector)) {
  1608. temp_value = val;
  1609. if (intel_sdvo_connector->left == property) {
  1610. drm_connector_property_set_value(connector,
  1611. intel_sdvo_connector->right, val);
  1612. if (intel_sdvo_connector->left_margin == temp_value)
  1613. return 0;
  1614. intel_sdvo_connector->left_margin = temp_value;
  1615. intel_sdvo_connector->right_margin = temp_value;
  1616. temp_value = intel_sdvo_connector->max_hscan -
  1617. intel_sdvo_connector->left_margin;
  1618. cmd = SDVO_CMD_SET_OVERSCAN_H;
  1619. goto set_value;
  1620. } else if (intel_sdvo_connector->right == property) {
  1621. drm_connector_property_set_value(connector,
  1622. intel_sdvo_connector->left, val);
  1623. if (intel_sdvo_connector->right_margin == temp_value)
  1624. return 0;
  1625. intel_sdvo_connector->left_margin = temp_value;
  1626. intel_sdvo_connector->right_margin = temp_value;
  1627. temp_value = intel_sdvo_connector->max_hscan -
  1628. intel_sdvo_connector->left_margin;
  1629. cmd = SDVO_CMD_SET_OVERSCAN_H;
  1630. goto set_value;
  1631. } else if (intel_sdvo_connector->top == property) {
  1632. drm_connector_property_set_value(connector,
  1633. intel_sdvo_connector->bottom, val);
  1634. if (intel_sdvo_connector->top_margin == temp_value)
  1635. return 0;
  1636. intel_sdvo_connector->top_margin = temp_value;
  1637. intel_sdvo_connector->bottom_margin = temp_value;
  1638. temp_value = intel_sdvo_connector->max_vscan -
  1639. intel_sdvo_connector->top_margin;
  1640. cmd = SDVO_CMD_SET_OVERSCAN_V;
  1641. goto set_value;
  1642. } else if (intel_sdvo_connector->bottom == property) {
  1643. drm_connector_property_set_value(connector,
  1644. intel_sdvo_connector->top, val);
  1645. if (intel_sdvo_connector->bottom_margin == temp_value)
  1646. return 0;
  1647. intel_sdvo_connector->top_margin = temp_value;
  1648. intel_sdvo_connector->bottom_margin = temp_value;
  1649. temp_value = intel_sdvo_connector->max_vscan -
  1650. intel_sdvo_connector->top_margin;
  1651. cmd = SDVO_CMD_SET_OVERSCAN_V;
  1652. goto set_value;
  1653. }
  1654. CHECK_PROPERTY(hpos, HPOS)
  1655. CHECK_PROPERTY(vpos, VPOS)
  1656. CHECK_PROPERTY(saturation, SATURATION)
  1657. CHECK_PROPERTY(contrast, CONTRAST)
  1658. CHECK_PROPERTY(hue, HUE)
  1659. CHECK_PROPERTY(brightness, BRIGHTNESS)
  1660. CHECK_PROPERTY(sharpness, SHARPNESS)
  1661. CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
  1662. CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
  1663. CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
  1664. CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
  1665. CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
  1666. CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
  1667. }
  1668. return -EINVAL; /* unknown property */
  1669. set_value:
  1670. if (!intel_sdvo_set_value(intel_sdvo, cmd, &temp_value, 2))
  1671. return -EIO;
  1672. done:
  1673. if (intel_sdvo->base.base.crtc) {
  1674. struct drm_crtc *crtc = intel_sdvo->base.base.crtc;
  1675. intel_set_mode(crtc, &crtc->mode,
  1676. crtc->x, crtc->y, crtc->fb);
  1677. }
  1678. return 0;
  1679. #undef CHECK_PROPERTY
  1680. }
  1681. static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
  1682. .mode_fixup = intel_sdvo_mode_fixup,
  1683. .mode_set = intel_sdvo_mode_set,
  1684. .disable = intel_encoder_noop,
  1685. };
  1686. static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
  1687. .dpms = intel_sdvo_dpms,
  1688. .detect = intel_sdvo_detect,
  1689. .fill_modes = drm_helper_probe_single_connector_modes,
  1690. .set_property = intel_sdvo_set_property,
  1691. .destroy = intel_sdvo_destroy,
  1692. };
  1693. static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
  1694. .get_modes = intel_sdvo_get_modes,
  1695. .mode_valid = intel_sdvo_mode_valid,
  1696. .best_encoder = intel_best_encoder,
  1697. };
  1698. static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
  1699. {
  1700. struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  1701. if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
  1702. drm_mode_destroy(encoder->dev,
  1703. intel_sdvo->sdvo_lvds_fixed_mode);
  1704. i2c_del_adapter(&intel_sdvo->ddc);
  1705. intel_encoder_destroy(encoder);
  1706. }
  1707. static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
  1708. .destroy = intel_sdvo_enc_destroy,
  1709. };
  1710. static void
  1711. intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
  1712. {
  1713. uint16_t mask = 0;
  1714. unsigned int num_bits;
  1715. /* Make a mask of outputs less than or equal to our own priority in the
  1716. * list.
  1717. */
  1718. switch (sdvo->controlled_output) {
  1719. case SDVO_OUTPUT_LVDS1:
  1720. mask |= SDVO_OUTPUT_LVDS1;
  1721. case SDVO_OUTPUT_LVDS0:
  1722. mask |= SDVO_OUTPUT_LVDS0;
  1723. case SDVO_OUTPUT_TMDS1:
  1724. mask |= SDVO_OUTPUT_TMDS1;
  1725. case SDVO_OUTPUT_TMDS0:
  1726. mask |= SDVO_OUTPUT_TMDS0;
  1727. case SDVO_OUTPUT_RGB1:
  1728. mask |= SDVO_OUTPUT_RGB1;
  1729. case SDVO_OUTPUT_RGB0:
  1730. mask |= SDVO_OUTPUT_RGB0;
  1731. break;
  1732. }
  1733. /* Count bits to find what number we are in the priority list. */
  1734. mask &= sdvo->caps.output_flags;
  1735. num_bits = hweight16(mask);
  1736. /* If more than 3 outputs, default to DDC bus 3 for now. */
  1737. if (num_bits > 3)
  1738. num_bits = 3;
  1739. /* Corresponds to SDVO_CONTROL_BUS_DDCx */
  1740. sdvo->ddc_bus = 1 << num_bits;
  1741. }
  1742. /**
  1743. * Choose the appropriate DDC bus for control bus switch command for this
  1744. * SDVO output based on the controlled output.
  1745. *
  1746. * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
  1747. * outputs, then LVDS outputs.
  1748. */
  1749. static void
  1750. intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
  1751. struct intel_sdvo *sdvo, u32 reg)
  1752. {
  1753. struct sdvo_device_mapping *mapping;
  1754. if (sdvo->is_sdvob)
  1755. mapping = &(dev_priv->sdvo_mappings[0]);
  1756. else
  1757. mapping = &(dev_priv->sdvo_mappings[1]);
  1758. if (mapping->initialized)
  1759. sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
  1760. else
  1761. intel_sdvo_guess_ddc_bus(sdvo);
  1762. }
  1763. static void
  1764. intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
  1765. struct intel_sdvo *sdvo, u32 reg)
  1766. {
  1767. struct sdvo_device_mapping *mapping;
  1768. u8 pin;
  1769. if (sdvo->is_sdvob)
  1770. mapping = &dev_priv->sdvo_mappings[0];
  1771. else
  1772. mapping = &dev_priv->sdvo_mappings[1];
  1773. pin = GMBUS_PORT_DPB;
  1774. if (mapping->initialized)
  1775. pin = mapping->i2c_pin;
  1776. if (intel_gmbus_is_port_valid(pin)) {
  1777. sdvo->i2c = intel_gmbus_get_adapter(dev_priv, pin);
  1778. intel_gmbus_set_speed(sdvo->i2c, GMBUS_RATE_1MHZ);
  1779. intel_gmbus_force_bit(sdvo->i2c, true);
  1780. } else {
  1781. sdvo->i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
  1782. }
  1783. }
  1784. static bool
  1785. intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
  1786. {
  1787. return intel_sdvo_check_supp_encode(intel_sdvo);
  1788. }
  1789. static u8
  1790. intel_sdvo_get_slave_addr(struct drm_device *dev, struct intel_sdvo *sdvo)
  1791. {
  1792. struct drm_i915_private *dev_priv = dev->dev_private;
  1793. struct sdvo_device_mapping *my_mapping, *other_mapping;
  1794. if (sdvo->is_sdvob) {
  1795. my_mapping = &dev_priv->sdvo_mappings[0];
  1796. other_mapping = &dev_priv->sdvo_mappings[1];
  1797. } else {
  1798. my_mapping = &dev_priv->sdvo_mappings[1];
  1799. other_mapping = &dev_priv->sdvo_mappings[0];
  1800. }
  1801. /* If the BIOS described our SDVO device, take advantage of it. */
  1802. if (my_mapping->slave_addr)
  1803. return my_mapping->slave_addr;
  1804. /* If the BIOS only described a different SDVO device, use the
  1805. * address that it isn't using.
  1806. */
  1807. if (other_mapping->slave_addr) {
  1808. if (other_mapping->slave_addr == 0x70)
  1809. return 0x72;
  1810. else
  1811. return 0x70;
  1812. }
  1813. /* No SDVO device info is found for another DVO port,
  1814. * so use mapping assumption we had before BIOS parsing.
  1815. */
  1816. if (sdvo->is_sdvob)
  1817. return 0x70;
  1818. else
  1819. return 0x72;
  1820. }
  1821. static void
  1822. intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
  1823. struct intel_sdvo *encoder)
  1824. {
  1825. drm_connector_init(encoder->base.base.dev,
  1826. &connector->base.base,
  1827. &intel_sdvo_connector_funcs,
  1828. connector->base.base.connector_type);
  1829. drm_connector_helper_add(&connector->base.base,
  1830. &intel_sdvo_connector_helper_funcs);
  1831. connector->base.base.interlace_allowed = 1;
  1832. connector->base.base.doublescan_allowed = 0;
  1833. connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
  1834. connector->base.get_hw_state = intel_sdvo_connector_get_hw_state;
  1835. intel_connector_attach_encoder(&connector->base, &encoder->base);
  1836. drm_sysfs_connector_add(&connector->base.base);
  1837. }
  1838. static void
  1839. intel_sdvo_add_hdmi_properties(struct intel_sdvo_connector *connector)
  1840. {
  1841. struct drm_device *dev = connector->base.base.dev;
  1842. intel_attach_force_audio_property(&connector->base.base);
  1843. if (INTEL_INFO(dev)->gen >= 4 && IS_MOBILE(dev))
  1844. intel_attach_broadcast_rgb_property(&connector->base.base);
  1845. }
  1846. static bool
  1847. intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
  1848. {
  1849. struct drm_encoder *encoder = &intel_sdvo->base.base;
  1850. struct drm_connector *connector;
  1851. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  1852. struct intel_connector *intel_connector;
  1853. struct intel_sdvo_connector *intel_sdvo_connector;
  1854. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  1855. if (!intel_sdvo_connector)
  1856. return false;
  1857. if (device == 0) {
  1858. intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
  1859. intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
  1860. } else if (device == 1) {
  1861. intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
  1862. intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
  1863. }
  1864. intel_connector = &intel_sdvo_connector->base;
  1865. connector = &intel_connector->base;
  1866. if (intel_sdvo_get_hotplug_support(intel_sdvo) &
  1867. intel_sdvo_connector->output_flag) {
  1868. connector->polled = DRM_CONNECTOR_POLL_HPD;
  1869. intel_sdvo->hotplug_active |= intel_sdvo_connector->output_flag;
  1870. /* Some SDVO devices have one-shot hotplug interrupts.
  1871. * Ensure that they get re-enabled when an interrupt happens.
  1872. */
  1873. intel_encoder->hot_plug = intel_sdvo_enable_hotplug;
  1874. intel_sdvo_enable_hotplug(intel_encoder);
  1875. } else {
  1876. connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
  1877. }
  1878. encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
  1879. connector->connector_type = DRM_MODE_CONNECTOR_DVID;
  1880. if (intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
  1881. connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
  1882. intel_sdvo->is_hdmi = true;
  1883. }
  1884. intel_sdvo->base.cloneable = true;
  1885. intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  1886. if (intel_sdvo->is_hdmi)
  1887. intel_sdvo_add_hdmi_properties(intel_sdvo_connector);
  1888. return true;
  1889. }
  1890. static bool
  1891. intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
  1892. {
  1893. struct drm_encoder *encoder = &intel_sdvo->base.base;
  1894. struct drm_connector *connector;
  1895. struct intel_connector *intel_connector;
  1896. struct intel_sdvo_connector *intel_sdvo_connector;
  1897. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  1898. if (!intel_sdvo_connector)
  1899. return false;
  1900. intel_connector = &intel_sdvo_connector->base;
  1901. connector = &intel_connector->base;
  1902. encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
  1903. connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
  1904. intel_sdvo->controlled_output |= type;
  1905. intel_sdvo_connector->output_flag = type;
  1906. intel_sdvo->is_tv = true;
  1907. intel_sdvo->base.needs_tv_clock = true;
  1908. intel_sdvo->base.cloneable = false;
  1909. intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  1910. if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
  1911. goto err;
  1912. if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  1913. goto err;
  1914. return true;
  1915. err:
  1916. intel_sdvo_destroy(connector);
  1917. return false;
  1918. }
  1919. static bool
  1920. intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
  1921. {
  1922. struct drm_encoder *encoder = &intel_sdvo->base.base;
  1923. struct drm_connector *connector;
  1924. struct intel_connector *intel_connector;
  1925. struct intel_sdvo_connector *intel_sdvo_connector;
  1926. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  1927. if (!intel_sdvo_connector)
  1928. return false;
  1929. intel_connector = &intel_sdvo_connector->base;
  1930. connector = &intel_connector->base;
  1931. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  1932. encoder->encoder_type = DRM_MODE_ENCODER_DAC;
  1933. connector->connector_type = DRM_MODE_CONNECTOR_VGA;
  1934. if (device == 0) {
  1935. intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
  1936. intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
  1937. } else if (device == 1) {
  1938. intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
  1939. intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
  1940. }
  1941. intel_sdvo->base.cloneable = true;
  1942. intel_sdvo_connector_init(intel_sdvo_connector,
  1943. intel_sdvo);
  1944. return true;
  1945. }
  1946. static bool
  1947. intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
  1948. {
  1949. struct drm_encoder *encoder = &intel_sdvo->base.base;
  1950. struct drm_connector *connector;
  1951. struct intel_connector *intel_connector;
  1952. struct intel_sdvo_connector *intel_sdvo_connector;
  1953. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  1954. if (!intel_sdvo_connector)
  1955. return false;
  1956. intel_connector = &intel_sdvo_connector->base;
  1957. connector = &intel_connector->base;
  1958. encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
  1959. connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
  1960. if (device == 0) {
  1961. intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
  1962. intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
  1963. } else if (device == 1) {
  1964. intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
  1965. intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
  1966. }
  1967. /* SDVO LVDS is not cloneable because the input mode gets adjusted by the encoder */
  1968. intel_sdvo->base.cloneable = false;
  1969. intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  1970. if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  1971. goto err;
  1972. return true;
  1973. err:
  1974. intel_sdvo_destroy(connector);
  1975. return false;
  1976. }
  1977. static bool
  1978. intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
  1979. {
  1980. intel_sdvo->is_tv = false;
  1981. intel_sdvo->base.needs_tv_clock = false;
  1982. intel_sdvo->is_lvds = false;
  1983. /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
  1984. if (flags & SDVO_OUTPUT_TMDS0)
  1985. if (!intel_sdvo_dvi_init(intel_sdvo, 0))
  1986. return false;
  1987. if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
  1988. if (!intel_sdvo_dvi_init(intel_sdvo, 1))
  1989. return false;
  1990. /* TV has no XXX1 function block */
  1991. if (flags & SDVO_OUTPUT_SVID0)
  1992. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
  1993. return false;
  1994. if (flags & SDVO_OUTPUT_CVBS0)
  1995. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
  1996. return false;
  1997. if (flags & SDVO_OUTPUT_YPRPB0)
  1998. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_YPRPB0))
  1999. return false;
  2000. if (flags & SDVO_OUTPUT_RGB0)
  2001. if (!intel_sdvo_analog_init(intel_sdvo, 0))
  2002. return false;
  2003. if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
  2004. if (!intel_sdvo_analog_init(intel_sdvo, 1))
  2005. return false;
  2006. if (flags & SDVO_OUTPUT_LVDS0)
  2007. if (!intel_sdvo_lvds_init(intel_sdvo, 0))
  2008. return false;
  2009. if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
  2010. if (!intel_sdvo_lvds_init(intel_sdvo, 1))
  2011. return false;
  2012. if ((flags & SDVO_OUTPUT_MASK) == 0) {
  2013. unsigned char bytes[2];
  2014. intel_sdvo->controlled_output = 0;
  2015. memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
  2016. DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
  2017. SDVO_NAME(intel_sdvo),
  2018. bytes[0], bytes[1]);
  2019. return false;
  2020. }
  2021. intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  2022. return true;
  2023. }
  2024. static void intel_sdvo_output_cleanup(struct intel_sdvo *intel_sdvo)
  2025. {
  2026. struct drm_device *dev = intel_sdvo->base.base.dev;
  2027. struct drm_connector *connector, *tmp;
  2028. list_for_each_entry_safe(connector, tmp,
  2029. &dev->mode_config.connector_list, head) {
  2030. if (intel_attached_encoder(connector) == &intel_sdvo->base)
  2031. intel_sdvo_destroy(connector);
  2032. }
  2033. }
  2034. static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
  2035. struct intel_sdvo_connector *intel_sdvo_connector,
  2036. int type)
  2037. {
  2038. struct drm_device *dev = intel_sdvo->base.base.dev;
  2039. struct intel_sdvo_tv_format format;
  2040. uint32_t format_map, i;
  2041. if (!intel_sdvo_set_target_output(intel_sdvo, type))
  2042. return false;
  2043. BUILD_BUG_ON(sizeof(format) != 6);
  2044. if (!intel_sdvo_get_value(intel_sdvo,
  2045. SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
  2046. &format, sizeof(format)))
  2047. return false;
  2048. memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
  2049. if (format_map == 0)
  2050. return false;
  2051. intel_sdvo_connector->format_supported_num = 0;
  2052. for (i = 0 ; i < TV_FORMAT_NUM; i++)
  2053. if (format_map & (1 << i))
  2054. intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
  2055. intel_sdvo_connector->tv_format =
  2056. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  2057. "mode", intel_sdvo_connector->format_supported_num);
  2058. if (!intel_sdvo_connector->tv_format)
  2059. return false;
  2060. for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
  2061. drm_property_add_enum(
  2062. intel_sdvo_connector->tv_format, i,
  2063. i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
  2064. intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[0];
  2065. drm_connector_attach_property(&intel_sdvo_connector->base.base,
  2066. intel_sdvo_connector->tv_format, 0);
  2067. return true;
  2068. }
  2069. #define ENHANCEMENT(name, NAME) do { \
  2070. if (enhancements.name) { \
  2071. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
  2072. !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
  2073. return false; \
  2074. intel_sdvo_connector->max_##name = data_value[0]; \
  2075. intel_sdvo_connector->cur_##name = response; \
  2076. intel_sdvo_connector->name = \
  2077. drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
  2078. if (!intel_sdvo_connector->name) return false; \
  2079. drm_connector_attach_property(connector, \
  2080. intel_sdvo_connector->name, \
  2081. intel_sdvo_connector->cur_##name); \
  2082. DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
  2083. data_value[0], data_value[1], response); \
  2084. } \
  2085. } while (0)
  2086. static bool
  2087. intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
  2088. struct intel_sdvo_connector *intel_sdvo_connector,
  2089. struct intel_sdvo_enhancements_reply enhancements)
  2090. {
  2091. struct drm_device *dev = intel_sdvo->base.base.dev;
  2092. struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2093. uint16_t response, data_value[2];
  2094. /* when horizontal overscan is supported, Add the left/right property */
  2095. if (enhancements.overscan_h) {
  2096. if (!intel_sdvo_get_value(intel_sdvo,
  2097. SDVO_CMD_GET_MAX_OVERSCAN_H,
  2098. &data_value, 4))
  2099. return false;
  2100. if (!intel_sdvo_get_value(intel_sdvo,
  2101. SDVO_CMD_GET_OVERSCAN_H,
  2102. &response, 2))
  2103. return false;
  2104. intel_sdvo_connector->max_hscan = data_value[0];
  2105. intel_sdvo_connector->left_margin = data_value[0] - response;
  2106. intel_sdvo_connector->right_margin = intel_sdvo_connector->left_margin;
  2107. intel_sdvo_connector->left =
  2108. drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
  2109. if (!intel_sdvo_connector->left)
  2110. return false;
  2111. drm_connector_attach_property(connector,
  2112. intel_sdvo_connector->left,
  2113. intel_sdvo_connector->left_margin);
  2114. intel_sdvo_connector->right =
  2115. drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
  2116. if (!intel_sdvo_connector->right)
  2117. return false;
  2118. drm_connector_attach_property(connector,
  2119. intel_sdvo_connector->right,
  2120. intel_sdvo_connector->right_margin);
  2121. DRM_DEBUG_KMS("h_overscan: max %d, "
  2122. "default %d, current %d\n",
  2123. data_value[0], data_value[1], response);
  2124. }
  2125. if (enhancements.overscan_v) {
  2126. if (!intel_sdvo_get_value(intel_sdvo,
  2127. SDVO_CMD_GET_MAX_OVERSCAN_V,
  2128. &data_value, 4))
  2129. return false;
  2130. if (!intel_sdvo_get_value(intel_sdvo,
  2131. SDVO_CMD_GET_OVERSCAN_V,
  2132. &response, 2))
  2133. return false;
  2134. intel_sdvo_connector->max_vscan = data_value[0];
  2135. intel_sdvo_connector->top_margin = data_value[0] - response;
  2136. intel_sdvo_connector->bottom_margin = intel_sdvo_connector->top_margin;
  2137. intel_sdvo_connector->top =
  2138. drm_property_create_range(dev, 0,
  2139. "top_margin", 0, data_value[0]);
  2140. if (!intel_sdvo_connector->top)
  2141. return false;
  2142. drm_connector_attach_property(connector,
  2143. intel_sdvo_connector->top,
  2144. intel_sdvo_connector->top_margin);
  2145. intel_sdvo_connector->bottom =
  2146. drm_property_create_range(dev, 0,
  2147. "bottom_margin", 0, data_value[0]);
  2148. if (!intel_sdvo_connector->bottom)
  2149. return false;
  2150. drm_connector_attach_property(connector,
  2151. intel_sdvo_connector->bottom,
  2152. intel_sdvo_connector->bottom_margin);
  2153. DRM_DEBUG_KMS("v_overscan: max %d, "
  2154. "default %d, current %d\n",
  2155. data_value[0], data_value[1], response);
  2156. }
  2157. ENHANCEMENT(hpos, HPOS);
  2158. ENHANCEMENT(vpos, VPOS);
  2159. ENHANCEMENT(saturation, SATURATION);
  2160. ENHANCEMENT(contrast, CONTRAST);
  2161. ENHANCEMENT(hue, HUE);
  2162. ENHANCEMENT(sharpness, SHARPNESS);
  2163. ENHANCEMENT(brightness, BRIGHTNESS);
  2164. ENHANCEMENT(flicker_filter, FLICKER_FILTER);
  2165. ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
  2166. ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
  2167. ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
  2168. ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
  2169. if (enhancements.dot_crawl) {
  2170. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
  2171. return false;
  2172. intel_sdvo_connector->max_dot_crawl = 1;
  2173. intel_sdvo_connector->cur_dot_crawl = response & 0x1;
  2174. intel_sdvo_connector->dot_crawl =
  2175. drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
  2176. if (!intel_sdvo_connector->dot_crawl)
  2177. return false;
  2178. drm_connector_attach_property(connector,
  2179. intel_sdvo_connector->dot_crawl,
  2180. intel_sdvo_connector->cur_dot_crawl);
  2181. DRM_DEBUG_KMS("dot crawl: current %d\n", response);
  2182. }
  2183. return true;
  2184. }
  2185. static bool
  2186. intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
  2187. struct intel_sdvo_connector *intel_sdvo_connector,
  2188. struct intel_sdvo_enhancements_reply enhancements)
  2189. {
  2190. struct drm_device *dev = intel_sdvo->base.base.dev;
  2191. struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2192. uint16_t response, data_value[2];
  2193. ENHANCEMENT(brightness, BRIGHTNESS);
  2194. return true;
  2195. }
  2196. #undef ENHANCEMENT
  2197. static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
  2198. struct intel_sdvo_connector *intel_sdvo_connector)
  2199. {
  2200. union {
  2201. struct intel_sdvo_enhancements_reply reply;
  2202. uint16_t response;
  2203. } enhancements;
  2204. BUILD_BUG_ON(sizeof(enhancements) != 2);
  2205. enhancements.response = 0;
  2206. intel_sdvo_get_value(intel_sdvo,
  2207. SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
  2208. &enhancements, sizeof(enhancements));
  2209. if (enhancements.response == 0) {
  2210. DRM_DEBUG_KMS("No enhancement is supported\n");
  2211. return true;
  2212. }
  2213. if (IS_TV(intel_sdvo_connector))
  2214. return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2215. else if (IS_LVDS(intel_sdvo_connector))
  2216. return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2217. else
  2218. return true;
  2219. }
  2220. static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
  2221. struct i2c_msg *msgs,
  2222. int num)
  2223. {
  2224. struct intel_sdvo *sdvo = adapter->algo_data;
  2225. if (!intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
  2226. return -EIO;
  2227. return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
  2228. }
  2229. static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
  2230. {
  2231. struct intel_sdvo *sdvo = adapter->algo_data;
  2232. return sdvo->i2c->algo->functionality(sdvo->i2c);
  2233. }
  2234. static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
  2235. .master_xfer = intel_sdvo_ddc_proxy_xfer,
  2236. .functionality = intel_sdvo_ddc_proxy_func
  2237. };
  2238. static bool
  2239. intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
  2240. struct drm_device *dev)
  2241. {
  2242. sdvo->ddc.owner = THIS_MODULE;
  2243. sdvo->ddc.class = I2C_CLASS_DDC;
  2244. snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
  2245. sdvo->ddc.dev.parent = &dev->pdev->dev;
  2246. sdvo->ddc.algo_data = sdvo;
  2247. sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
  2248. return i2c_add_adapter(&sdvo->ddc) == 0;
  2249. }
  2250. bool intel_sdvo_init(struct drm_device *dev, uint32_t sdvo_reg, bool is_sdvob)
  2251. {
  2252. struct drm_i915_private *dev_priv = dev->dev_private;
  2253. struct intel_encoder *intel_encoder;
  2254. struct intel_sdvo *intel_sdvo;
  2255. u32 hotplug_mask;
  2256. int i;
  2257. intel_sdvo = kzalloc(sizeof(struct intel_sdvo), GFP_KERNEL);
  2258. if (!intel_sdvo)
  2259. return false;
  2260. intel_sdvo->sdvo_reg = sdvo_reg;
  2261. intel_sdvo->is_sdvob = is_sdvob;
  2262. intel_sdvo->slave_addr = intel_sdvo_get_slave_addr(dev, intel_sdvo) >> 1;
  2263. intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo, sdvo_reg);
  2264. if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev)) {
  2265. kfree(intel_sdvo);
  2266. return false;
  2267. }
  2268. /* encoder type will be decided later */
  2269. intel_encoder = &intel_sdvo->base;
  2270. intel_encoder->type = INTEL_OUTPUT_SDVO;
  2271. drm_encoder_init(dev, &intel_encoder->base, &intel_sdvo_enc_funcs, 0);
  2272. /* Read the regs to test if we can talk to the device */
  2273. for (i = 0; i < 0x40; i++) {
  2274. u8 byte;
  2275. if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
  2276. DRM_DEBUG_KMS("No SDVO device found on %s\n",
  2277. SDVO_NAME(intel_sdvo));
  2278. goto err;
  2279. }
  2280. }
  2281. hotplug_mask = 0;
  2282. if (IS_G4X(dev)) {
  2283. hotplug_mask = intel_sdvo->is_sdvob ?
  2284. SDVOB_HOTPLUG_INT_STATUS_G4X : SDVOC_HOTPLUG_INT_STATUS_G4X;
  2285. } else if (IS_GEN4(dev)) {
  2286. hotplug_mask = intel_sdvo->is_sdvob ?
  2287. SDVOB_HOTPLUG_INT_STATUS_I965 : SDVOC_HOTPLUG_INT_STATUS_I965;
  2288. } else {
  2289. hotplug_mask = intel_sdvo->is_sdvob ?
  2290. SDVOB_HOTPLUG_INT_STATUS_I915 : SDVOC_HOTPLUG_INT_STATUS_I915;
  2291. }
  2292. drm_encoder_helper_add(&intel_encoder->base, &intel_sdvo_helper_funcs);
  2293. intel_encoder->disable = intel_disable_sdvo;
  2294. intel_encoder->enable = intel_enable_sdvo;
  2295. intel_encoder->get_hw_state = intel_sdvo_get_hw_state;
  2296. /* In default case sdvo lvds is false */
  2297. if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
  2298. goto err;
  2299. if (intel_sdvo_output_setup(intel_sdvo,
  2300. intel_sdvo->caps.output_flags) != true) {
  2301. DRM_DEBUG_KMS("SDVO output failed to setup on %s\n",
  2302. SDVO_NAME(intel_sdvo));
  2303. /* Output_setup can leave behind connectors! */
  2304. goto err_output;
  2305. }
  2306. /* Only enable the hotplug irq if we need it, to work around noisy
  2307. * hotplug lines.
  2308. */
  2309. if (intel_sdvo->hotplug_active)
  2310. dev_priv->hotplug_supported_mask |= hotplug_mask;
  2311. intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo, sdvo_reg);
  2312. /* Set the input timing to the screen. Assume always input 0. */
  2313. if (!intel_sdvo_set_target_input(intel_sdvo))
  2314. goto err_output;
  2315. if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
  2316. &intel_sdvo->pixel_clock_min,
  2317. &intel_sdvo->pixel_clock_max))
  2318. goto err_output;
  2319. DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
  2320. "clock range %dMHz - %dMHz, "
  2321. "input 1: %c, input 2: %c, "
  2322. "output 1: %c, output 2: %c\n",
  2323. SDVO_NAME(intel_sdvo),
  2324. intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
  2325. intel_sdvo->caps.device_rev_id,
  2326. intel_sdvo->pixel_clock_min / 1000,
  2327. intel_sdvo->pixel_clock_max / 1000,
  2328. (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
  2329. (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
  2330. /* check currently supported outputs */
  2331. intel_sdvo->caps.output_flags &
  2332. (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
  2333. intel_sdvo->caps.output_flags &
  2334. (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
  2335. return true;
  2336. err_output:
  2337. intel_sdvo_output_cleanup(intel_sdvo);
  2338. err:
  2339. drm_encoder_cleanup(&intel_encoder->base);
  2340. i2c_del_adapter(&intel_sdvo->ddc);
  2341. kfree(intel_sdvo);
  2342. return false;
  2343. }