intel_sdvo.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224
  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_atomic_helper.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_edid.h>
  36. #include "intel_drv.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "intel_sdvo_regs.h"
  40. #define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
  41. #define SDVO_RGB_MASK (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
  42. #define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
  43. #define SDVO_TV_MASK (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_YPRPB0)
  44. #define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
  45. SDVO_TV_MASK)
  46. #define IS_TV(c) (c->output_flag & SDVO_TV_MASK)
  47. #define IS_TMDS(c) (c->output_flag & SDVO_TMDS_MASK)
  48. #define IS_LVDS(c) (c->output_flag & SDVO_LVDS_MASK)
  49. #define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
  50. #define IS_DIGITAL(c) (c->output_flag & (SDVO_TMDS_MASK | SDVO_LVDS_MASK))
  51. static const char * const tv_format_names[] = {
  52. "NTSC_M" , "NTSC_J" , "NTSC_443",
  53. "PAL_B" , "PAL_D" , "PAL_G" ,
  54. "PAL_H" , "PAL_I" , "PAL_M" ,
  55. "PAL_N" , "PAL_NC" , "PAL_60" ,
  56. "SECAM_B" , "SECAM_D" , "SECAM_G" ,
  57. "SECAM_K" , "SECAM_K1", "SECAM_L" ,
  58. "SECAM_60"
  59. };
  60. #define TV_FORMAT_NUM ARRAY_SIZE(tv_format_names)
  61. struct intel_sdvo {
  62. struct intel_encoder base;
  63. struct i2c_adapter *i2c;
  64. u8 slave_addr;
  65. struct i2c_adapter ddc;
  66. /* Register for the SDVO device: SDVOB or SDVOC */
  67. i915_reg_t sdvo_reg;
  68. /* Active outputs controlled by this SDVO output */
  69. uint16_t controlled_output;
  70. /*
  71. * Capabilities of the SDVO device returned by
  72. * intel_sdvo_get_capabilities()
  73. */
  74. struct intel_sdvo_caps caps;
  75. /* Pixel clock limitations reported by the SDVO device, in kHz */
  76. int pixel_clock_min, pixel_clock_max;
  77. /*
  78. * For multiple function SDVO device,
  79. * this is for current attached outputs.
  80. */
  81. uint16_t attached_output;
  82. /*
  83. * Hotplug activation bits for this device
  84. */
  85. uint16_t hotplug_active;
  86. /**
  87. * This is set if we're going to treat the device as TV-out.
  88. *
  89. * While we have these nice friendly flags for output types that ought
  90. * to decide this for us, the S-Video output on our HDMI+S-Video card
  91. * shows up as RGB1 (VGA).
  92. */
  93. bool is_tv;
  94. enum port port;
  95. /**
  96. * This is set if we treat the device as HDMI, instead of DVI.
  97. */
  98. bool is_hdmi;
  99. bool has_hdmi_monitor;
  100. bool has_hdmi_audio;
  101. bool rgb_quant_range_selectable;
  102. /**
  103. * This is set if we detect output of sdvo device as LVDS and
  104. * have a valid fixed mode to use with the panel.
  105. */
  106. bool is_lvds;
  107. /**
  108. * This is sdvo fixed pannel mode pointer
  109. */
  110. struct drm_display_mode *sdvo_lvds_fixed_mode;
  111. /* DDC bus used by this SDVO encoder */
  112. uint8_t ddc_bus;
  113. /*
  114. * the sdvo flag gets lost in round trip: dtd->adjusted_mode->dtd
  115. */
  116. uint8_t dtd_sdvo_flags;
  117. };
  118. struct intel_sdvo_connector {
  119. struct intel_connector base;
  120. /* Mark the type of connector */
  121. uint16_t output_flag;
  122. /* This contains all current supported TV format */
  123. u8 tv_format_supported[TV_FORMAT_NUM];
  124. int format_supported_num;
  125. struct drm_property *tv_format;
  126. /* add the property for the SDVO-TV */
  127. struct drm_property *left;
  128. struct drm_property *right;
  129. struct drm_property *top;
  130. struct drm_property *bottom;
  131. struct drm_property *hpos;
  132. struct drm_property *vpos;
  133. struct drm_property *contrast;
  134. struct drm_property *saturation;
  135. struct drm_property *hue;
  136. struct drm_property *sharpness;
  137. struct drm_property *flicker_filter;
  138. struct drm_property *flicker_filter_adaptive;
  139. struct drm_property *flicker_filter_2d;
  140. struct drm_property *tv_chroma_filter;
  141. struct drm_property *tv_luma_filter;
  142. struct drm_property *dot_crawl;
  143. /* add the property for the SDVO-TV/LVDS */
  144. struct drm_property *brightness;
  145. /* this is to get the range of margin.*/
  146. u32 max_hscan, max_vscan;
  147. };
  148. struct intel_sdvo_connector_state {
  149. /* base.base: tv.saturation/contrast/hue/brightness */
  150. struct intel_digital_connector_state base;
  151. struct {
  152. unsigned overscan_h, overscan_v, hpos, vpos, sharpness;
  153. unsigned flicker_filter, flicker_filter_2d, flicker_filter_adaptive;
  154. unsigned chroma_filter, luma_filter, dot_crawl;
  155. } tv;
  156. };
  157. static struct intel_sdvo *to_sdvo(struct intel_encoder *encoder)
  158. {
  159. return container_of(encoder, struct intel_sdvo, base);
  160. }
  161. static struct intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
  162. {
  163. return to_sdvo(intel_attached_encoder(connector));
  164. }
  165. static struct intel_sdvo_connector *
  166. to_intel_sdvo_connector(struct drm_connector *connector)
  167. {
  168. return container_of(connector, struct intel_sdvo_connector, base.base);
  169. }
  170. #define to_intel_sdvo_connector_state(conn_state) \
  171. container_of((conn_state), struct intel_sdvo_connector_state, base.base)
  172. static bool
  173. intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags);
  174. static bool
  175. intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
  176. struct intel_sdvo_connector *intel_sdvo_connector,
  177. int type);
  178. static bool
  179. intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
  180. struct intel_sdvo_connector *intel_sdvo_connector);
  181. /*
  182. * Writes the SDVOB or SDVOC with the given value, but always writes both
  183. * SDVOB and SDVOC to work around apparent hardware issues (according to
  184. * comments in the BIOS).
  185. */
  186. static void intel_sdvo_write_sdvox(struct intel_sdvo *intel_sdvo, u32 val)
  187. {
  188. struct drm_device *dev = intel_sdvo->base.base.dev;
  189. struct drm_i915_private *dev_priv = to_i915(dev);
  190. u32 bval = val, cval = val;
  191. int i;
  192. if (HAS_PCH_SPLIT(dev_priv)) {
  193. I915_WRITE(intel_sdvo->sdvo_reg, val);
  194. POSTING_READ(intel_sdvo->sdvo_reg);
  195. /*
  196. * HW workaround, need to write this twice for issue
  197. * that may result in first write getting masked.
  198. */
  199. if (HAS_PCH_IBX(dev_priv)) {
  200. I915_WRITE(intel_sdvo->sdvo_reg, val);
  201. POSTING_READ(intel_sdvo->sdvo_reg);
  202. }
  203. return;
  204. }
  205. if (intel_sdvo->port == PORT_B)
  206. cval = I915_READ(GEN3_SDVOC);
  207. else
  208. bval = I915_READ(GEN3_SDVOB);
  209. /*
  210. * Write the registers twice for luck. Sometimes,
  211. * writing them only once doesn't appear to 'stick'.
  212. * The BIOS does this too. Yay, magic
  213. */
  214. for (i = 0; i < 2; i++) {
  215. I915_WRITE(GEN3_SDVOB, bval);
  216. POSTING_READ(GEN3_SDVOB);
  217. I915_WRITE(GEN3_SDVOC, cval);
  218. POSTING_READ(GEN3_SDVOC);
  219. }
  220. }
  221. static bool intel_sdvo_read_byte(struct intel_sdvo *intel_sdvo, u8 addr, u8 *ch)
  222. {
  223. struct i2c_msg msgs[] = {
  224. {
  225. .addr = intel_sdvo->slave_addr,
  226. .flags = 0,
  227. .len = 1,
  228. .buf = &addr,
  229. },
  230. {
  231. .addr = intel_sdvo->slave_addr,
  232. .flags = I2C_M_RD,
  233. .len = 1,
  234. .buf = ch,
  235. }
  236. };
  237. int ret;
  238. if ((ret = i2c_transfer(intel_sdvo->i2c, msgs, 2)) == 2)
  239. return true;
  240. DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
  241. return false;
  242. }
  243. #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
  244. /** Mapping of command numbers to names, for debug output */
  245. static const struct _sdvo_cmd_name {
  246. u8 cmd;
  247. const char *name;
  248. } __attribute__ ((packed)) sdvo_cmd_names[] = {
  249. SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
  250. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
  251. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
  252. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
  253. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
  254. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
  255. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
  256. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
  257. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
  258. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
  259. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
  260. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
  261. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
  262. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
  263. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
  264. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
  265. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
  266. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  267. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
  268. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  269. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
  270. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
  271. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
  272. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
  273. SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
  274. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
  275. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
  276. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
  277. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
  278. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
  279. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
  280. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
  281. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
  282. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
  283. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
  284. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
  285. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
  286. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
  287. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
  288. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
  289. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
  290. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
  291. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
  292. /* Add the op code for SDVO enhancements */
  293. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
  294. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
  295. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
  296. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
  297. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
  298. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
  299. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
  300. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
  301. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
  302. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
  303. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
  304. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
  305. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
  306. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
  307. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
  308. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
  309. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
  310. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
  311. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
  312. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
  313. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
  314. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
  315. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
  316. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
  317. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
  318. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
  319. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
  320. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
  321. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
  322. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
  323. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
  324. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
  325. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
  326. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
  327. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
  328. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
  329. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
  330. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
  331. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
  332. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
  333. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
  334. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
  335. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
  336. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
  337. /* HDMI op code */
  338. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
  339. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
  340. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
  341. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
  342. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
  343. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
  344. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
  345. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
  346. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
  347. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
  348. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
  349. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
  350. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
  351. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
  352. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
  353. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
  354. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
  355. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
  356. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
  357. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
  358. };
  359. #define SDVO_NAME(svdo) ((svdo)->port == PORT_B ? "SDVOB" : "SDVOC")
  360. static void intel_sdvo_debug_write(struct intel_sdvo *intel_sdvo, u8 cmd,
  361. const void *args, int args_len)
  362. {
  363. int i, pos = 0;
  364. #define BUF_LEN 256
  365. char buffer[BUF_LEN];
  366. #define BUF_PRINT(args...) \
  367. pos += snprintf(buffer + pos, max_t(int, BUF_LEN - pos, 0), args)
  368. for (i = 0; i < args_len; i++) {
  369. BUF_PRINT("%02X ", ((u8 *)args)[i]);
  370. }
  371. for (; i < 8; i++) {
  372. BUF_PRINT(" ");
  373. }
  374. for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
  375. if (cmd == sdvo_cmd_names[i].cmd) {
  376. BUF_PRINT("(%s)", sdvo_cmd_names[i].name);
  377. break;
  378. }
  379. }
  380. if (i == ARRAY_SIZE(sdvo_cmd_names)) {
  381. BUF_PRINT("(%02X)", cmd);
  382. }
  383. BUG_ON(pos >= BUF_LEN - 1);
  384. #undef BUF_PRINT
  385. #undef BUF_LEN
  386. DRM_DEBUG_KMS("%s: W: %02X %s\n", SDVO_NAME(intel_sdvo), cmd, buffer);
  387. }
  388. static const char * const cmd_status_names[] = {
  389. "Power on",
  390. "Success",
  391. "Not supported",
  392. "Invalid arg",
  393. "Pending",
  394. "Target not specified",
  395. "Scaling not supported"
  396. };
  397. static bool __intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
  398. const void *args, int args_len,
  399. bool unlocked)
  400. {
  401. u8 *buf, status;
  402. struct i2c_msg *msgs;
  403. int i, ret = true;
  404. /* Would be simpler to allocate both in one go ? */
  405. buf = kzalloc(args_len * 2 + 2, GFP_KERNEL);
  406. if (!buf)
  407. return false;
  408. msgs = kcalloc(args_len + 3, sizeof(*msgs), GFP_KERNEL);
  409. if (!msgs) {
  410. kfree(buf);
  411. return false;
  412. }
  413. intel_sdvo_debug_write(intel_sdvo, cmd, args, args_len);
  414. for (i = 0; i < args_len; i++) {
  415. msgs[i].addr = intel_sdvo->slave_addr;
  416. msgs[i].flags = 0;
  417. msgs[i].len = 2;
  418. msgs[i].buf = buf + 2 *i;
  419. buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
  420. buf[2*i + 1] = ((u8*)args)[i];
  421. }
  422. msgs[i].addr = intel_sdvo->slave_addr;
  423. msgs[i].flags = 0;
  424. msgs[i].len = 2;
  425. msgs[i].buf = buf + 2*i;
  426. buf[2*i + 0] = SDVO_I2C_OPCODE;
  427. buf[2*i + 1] = cmd;
  428. /* the following two are to read the response */
  429. status = SDVO_I2C_CMD_STATUS;
  430. msgs[i+1].addr = intel_sdvo->slave_addr;
  431. msgs[i+1].flags = 0;
  432. msgs[i+1].len = 1;
  433. msgs[i+1].buf = &status;
  434. msgs[i+2].addr = intel_sdvo->slave_addr;
  435. msgs[i+2].flags = I2C_M_RD;
  436. msgs[i+2].len = 1;
  437. msgs[i+2].buf = &status;
  438. if (unlocked)
  439. ret = i2c_transfer(intel_sdvo->i2c, msgs, i+3);
  440. else
  441. ret = __i2c_transfer(intel_sdvo->i2c, msgs, i+3);
  442. if (ret < 0) {
  443. DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
  444. ret = false;
  445. goto out;
  446. }
  447. if (ret != i+3) {
  448. /* failure in I2C transfer */
  449. DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
  450. ret = false;
  451. }
  452. out:
  453. kfree(msgs);
  454. kfree(buf);
  455. return ret;
  456. }
  457. static bool intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
  458. const void *args, int args_len)
  459. {
  460. return __intel_sdvo_write_cmd(intel_sdvo, cmd, args, args_len, true);
  461. }
  462. static bool intel_sdvo_read_response(struct intel_sdvo *intel_sdvo,
  463. void *response, int response_len)
  464. {
  465. u8 retry = 15; /* 5 quick checks, followed by 10 long checks */
  466. u8 status;
  467. int i, pos = 0;
  468. #define BUF_LEN 256
  469. char buffer[BUF_LEN];
  470. /*
  471. * The documentation states that all commands will be
  472. * processed within 15µs, and that we need only poll
  473. * the status byte a maximum of 3 times in order for the
  474. * command to be complete.
  475. *
  476. * Check 5 times in case the hardware failed to read the docs.
  477. *
  478. * Also beware that the first response by many devices is to
  479. * reply PENDING and stall for time. TVs are notorious for
  480. * requiring longer than specified to complete their replies.
  481. * Originally (in the DDX long ago), the delay was only ever 15ms
  482. * with an additional delay of 30ms applied for TVs added later after
  483. * many experiments. To accommodate both sets of delays, we do a
  484. * sequence of slow checks if the device is falling behind and fails
  485. * to reply within 5*15µs.
  486. */
  487. if (!intel_sdvo_read_byte(intel_sdvo,
  488. SDVO_I2C_CMD_STATUS,
  489. &status))
  490. goto log_fail;
  491. while ((status == SDVO_CMD_STATUS_PENDING ||
  492. status == SDVO_CMD_STATUS_TARGET_NOT_SPECIFIED) && --retry) {
  493. if (retry < 10)
  494. msleep(15);
  495. else
  496. udelay(15);
  497. if (!intel_sdvo_read_byte(intel_sdvo,
  498. SDVO_I2C_CMD_STATUS,
  499. &status))
  500. goto log_fail;
  501. }
  502. #define BUF_PRINT(args...) \
  503. pos += snprintf(buffer + pos, max_t(int, BUF_LEN - pos, 0), args)
  504. if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
  505. BUF_PRINT("(%s)", cmd_status_names[status]);
  506. else
  507. BUF_PRINT("(??? %d)", status);
  508. if (status != SDVO_CMD_STATUS_SUCCESS)
  509. goto log_fail;
  510. /* Read the command response */
  511. for (i = 0; i < response_len; i++) {
  512. if (!intel_sdvo_read_byte(intel_sdvo,
  513. SDVO_I2C_RETURN_0 + i,
  514. &((u8 *)response)[i]))
  515. goto log_fail;
  516. BUF_PRINT(" %02X", ((u8 *)response)[i]);
  517. }
  518. BUG_ON(pos >= BUF_LEN - 1);
  519. #undef BUF_PRINT
  520. #undef BUF_LEN
  521. DRM_DEBUG_KMS("%s: R: %s\n", SDVO_NAME(intel_sdvo), buffer);
  522. return true;
  523. log_fail:
  524. DRM_DEBUG_KMS("%s: R: ... failed\n", SDVO_NAME(intel_sdvo));
  525. return false;
  526. }
  527. static int intel_sdvo_get_pixel_multiplier(const struct drm_display_mode *adjusted_mode)
  528. {
  529. if (adjusted_mode->crtc_clock >= 100000)
  530. return 1;
  531. else if (adjusted_mode->crtc_clock >= 50000)
  532. return 2;
  533. else
  534. return 4;
  535. }
  536. static bool __intel_sdvo_set_control_bus_switch(struct intel_sdvo *intel_sdvo,
  537. u8 ddc_bus)
  538. {
  539. /* This must be the immediately preceding write before the i2c xfer */
  540. return __intel_sdvo_write_cmd(intel_sdvo,
  541. SDVO_CMD_SET_CONTROL_BUS_SWITCH,
  542. &ddc_bus, 1, false);
  543. }
  544. static bool intel_sdvo_set_value(struct intel_sdvo *intel_sdvo, u8 cmd, const void *data, int len)
  545. {
  546. if (!intel_sdvo_write_cmd(intel_sdvo, cmd, data, len))
  547. return false;
  548. return intel_sdvo_read_response(intel_sdvo, NULL, 0);
  549. }
  550. static bool
  551. intel_sdvo_get_value(struct intel_sdvo *intel_sdvo, u8 cmd, void *value, int len)
  552. {
  553. if (!intel_sdvo_write_cmd(intel_sdvo, cmd, NULL, 0))
  554. return false;
  555. return intel_sdvo_read_response(intel_sdvo, value, len);
  556. }
  557. static bool intel_sdvo_set_target_input(struct intel_sdvo *intel_sdvo)
  558. {
  559. struct intel_sdvo_set_target_input_args targets = {0};
  560. return intel_sdvo_set_value(intel_sdvo,
  561. SDVO_CMD_SET_TARGET_INPUT,
  562. &targets, sizeof(targets));
  563. }
  564. /*
  565. * Return whether each input is trained.
  566. *
  567. * This function is making an assumption about the layout of the response,
  568. * which should be checked against the docs.
  569. */
  570. static bool intel_sdvo_get_trained_inputs(struct intel_sdvo *intel_sdvo, bool *input_1, bool *input_2)
  571. {
  572. struct intel_sdvo_get_trained_inputs_response response;
  573. BUILD_BUG_ON(sizeof(response) != 1);
  574. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
  575. &response, sizeof(response)))
  576. return false;
  577. *input_1 = response.input0_trained;
  578. *input_2 = response.input1_trained;
  579. return true;
  580. }
  581. static bool intel_sdvo_set_active_outputs(struct intel_sdvo *intel_sdvo,
  582. u16 outputs)
  583. {
  584. return intel_sdvo_set_value(intel_sdvo,
  585. SDVO_CMD_SET_ACTIVE_OUTPUTS,
  586. &outputs, sizeof(outputs));
  587. }
  588. static bool intel_sdvo_get_active_outputs(struct intel_sdvo *intel_sdvo,
  589. u16 *outputs)
  590. {
  591. return intel_sdvo_get_value(intel_sdvo,
  592. SDVO_CMD_GET_ACTIVE_OUTPUTS,
  593. outputs, sizeof(*outputs));
  594. }
  595. static bool intel_sdvo_set_encoder_power_state(struct intel_sdvo *intel_sdvo,
  596. int mode)
  597. {
  598. u8 state = SDVO_ENCODER_STATE_ON;
  599. switch (mode) {
  600. case DRM_MODE_DPMS_ON:
  601. state = SDVO_ENCODER_STATE_ON;
  602. break;
  603. case DRM_MODE_DPMS_STANDBY:
  604. state = SDVO_ENCODER_STATE_STANDBY;
  605. break;
  606. case DRM_MODE_DPMS_SUSPEND:
  607. state = SDVO_ENCODER_STATE_SUSPEND;
  608. break;
  609. case DRM_MODE_DPMS_OFF:
  610. state = SDVO_ENCODER_STATE_OFF;
  611. break;
  612. }
  613. return intel_sdvo_set_value(intel_sdvo,
  614. SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
  615. }
  616. static bool intel_sdvo_get_input_pixel_clock_range(struct intel_sdvo *intel_sdvo,
  617. int *clock_min,
  618. int *clock_max)
  619. {
  620. struct intel_sdvo_pixel_clock_range clocks;
  621. BUILD_BUG_ON(sizeof(clocks) != 4);
  622. if (!intel_sdvo_get_value(intel_sdvo,
  623. SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
  624. &clocks, sizeof(clocks)))
  625. return false;
  626. /* Convert the values from units of 10 kHz to kHz. */
  627. *clock_min = clocks.min * 10;
  628. *clock_max = clocks.max * 10;
  629. return true;
  630. }
  631. static bool intel_sdvo_set_target_output(struct intel_sdvo *intel_sdvo,
  632. u16 outputs)
  633. {
  634. return intel_sdvo_set_value(intel_sdvo,
  635. SDVO_CMD_SET_TARGET_OUTPUT,
  636. &outputs, sizeof(outputs));
  637. }
  638. static bool intel_sdvo_set_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
  639. struct intel_sdvo_dtd *dtd)
  640. {
  641. return intel_sdvo_set_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
  642. intel_sdvo_set_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
  643. }
  644. static bool intel_sdvo_get_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
  645. struct intel_sdvo_dtd *dtd)
  646. {
  647. return intel_sdvo_get_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
  648. intel_sdvo_get_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
  649. }
  650. static bool intel_sdvo_set_input_timing(struct intel_sdvo *intel_sdvo,
  651. struct intel_sdvo_dtd *dtd)
  652. {
  653. return intel_sdvo_set_timing(intel_sdvo,
  654. SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
  655. }
  656. static bool intel_sdvo_set_output_timing(struct intel_sdvo *intel_sdvo,
  657. struct intel_sdvo_dtd *dtd)
  658. {
  659. return intel_sdvo_set_timing(intel_sdvo,
  660. SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
  661. }
  662. static bool intel_sdvo_get_input_timing(struct intel_sdvo *intel_sdvo,
  663. struct intel_sdvo_dtd *dtd)
  664. {
  665. return intel_sdvo_get_timing(intel_sdvo,
  666. SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
  667. }
  668. static bool
  669. intel_sdvo_create_preferred_input_timing(struct intel_sdvo *intel_sdvo,
  670. uint16_t clock,
  671. uint16_t width,
  672. uint16_t height)
  673. {
  674. struct intel_sdvo_preferred_input_timing_args args;
  675. memset(&args, 0, sizeof(args));
  676. args.clock = clock;
  677. args.width = width;
  678. args.height = height;
  679. args.interlace = 0;
  680. if (intel_sdvo->is_lvds &&
  681. (intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
  682. intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
  683. args.scaled = 1;
  684. return intel_sdvo_set_value(intel_sdvo,
  685. SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
  686. &args, sizeof(args));
  687. }
  688. static bool intel_sdvo_get_preferred_input_timing(struct intel_sdvo *intel_sdvo,
  689. struct intel_sdvo_dtd *dtd)
  690. {
  691. BUILD_BUG_ON(sizeof(dtd->part1) != 8);
  692. BUILD_BUG_ON(sizeof(dtd->part2) != 8);
  693. return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
  694. &dtd->part1, sizeof(dtd->part1)) &&
  695. intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
  696. &dtd->part2, sizeof(dtd->part2));
  697. }
  698. static bool intel_sdvo_set_clock_rate_mult(struct intel_sdvo *intel_sdvo, u8 val)
  699. {
  700. return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
  701. }
  702. static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
  703. const struct drm_display_mode *mode)
  704. {
  705. uint16_t width, height;
  706. uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
  707. uint16_t h_sync_offset, v_sync_offset;
  708. int mode_clock;
  709. memset(dtd, 0, sizeof(*dtd));
  710. width = mode->hdisplay;
  711. height = mode->vdisplay;
  712. /* do some mode translations */
  713. h_blank_len = mode->htotal - mode->hdisplay;
  714. h_sync_len = mode->hsync_end - mode->hsync_start;
  715. v_blank_len = mode->vtotal - mode->vdisplay;
  716. v_sync_len = mode->vsync_end - mode->vsync_start;
  717. h_sync_offset = mode->hsync_start - mode->hdisplay;
  718. v_sync_offset = mode->vsync_start - mode->vdisplay;
  719. mode_clock = mode->clock;
  720. mode_clock /= 10;
  721. dtd->part1.clock = mode_clock;
  722. dtd->part1.h_active = width & 0xff;
  723. dtd->part1.h_blank = h_blank_len & 0xff;
  724. dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
  725. ((h_blank_len >> 8) & 0xf);
  726. dtd->part1.v_active = height & 0xff;
  727. dtd->part1.v_blank = v_blank_len & 0xff;
  728. dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
  729. ((v_blank_len >> 8) & 0xf);
  730. dtd->part2.h_sync_off = h_sync_offset & 0xff;
  731. dtd->part2.h_sync_width = h_sync_len & 0xff;
  732. dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
  733. (v_sync_len & 0xf);
  734. dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
  735. ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
  736. ((v_sync_len & 0x30) >> 4);
  737. dtd->part2.dtd_flags = 0x18;
  738. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  739. dtd->part2.dtd_flags |= DTD_FLAG_INTERLACE;
  740. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  741. dtd->part2.dtd_flags |= DTD_FLAG_HSYNC_POSITIVE;
  742. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  743. dtd->part2.dtd_flags |= DTD_FLAG_VSYNC_POSITIVE;
  744. dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
  745. }
  746. static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode *pmode,
  747. const struct intel_sdvo_dtd *dtd)
  748. {
  749. struct drm_display_mode mode = {};
  750. mode.hdisplay = dtd->part1.h_active;
  751. mode.hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
  752. mode.hsync_start = mode.hdisplay + dtd->part2.h_sync_off;
  753. mode.hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
  754. mode.hsync_end = mode.hsync_start + dtd->part2.h_sync_width;
  755. mode.hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
  756. mode.htotal = mode.hdisplay + dtd->part1.h_blank;
  757. mode.htotal += (dtd->part1.h_high & 0xf) << 8;
  758. mode.vdisplay = dtd->part1.v_active;
  759. mode.vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
  760. mode.vsync_start = mode.vdisplay;
  761. mode.vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
  762. mode.vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
  763. mode.vsync_start += dtd->part2.v_sync_off_high & 0xc0;
  764. mode.vsync_end = mode.vsync_start +
  765. (dtd->part2.v_sync_off_width & 0xf);
  766. mode.vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
  767. mode.vtotal = mode.vdisplay + dtd->part1.v_blank;
  768. mode.vtotal += (dtd->part1.v_high & 0xf) << 8;
  769. mode.clock = dtd->part1.clock * 10;
  770. if (dtd->part2.dtd_flags & DTD_FLAG_INTERLACE)
  771. mode.flags |= DRM_MODE_FLAG_INTERLACE;
  772. if (dtd->part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
  773. mode.flags |= DRM_MODE_FLAG_PHSYNC;
  774. else
  775. mode.flags |= DRM_MODE_FLAG_NHSYNC;
  776. if (dtd->part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
  777. mode.flags |= DRM_MODE_FLAG_PVSYNC;
  778. else
  779. mode.flags |= DRM_MODE_FLAG_NVSYNC;
  780. drm_mode_set_crtcinfo(&mode, 0);
  781. drm_mode_copy(pmode, &mode);
  782. }
  783. static bool intel_sdvo_check_supp_encode(struct intel_sdvo *intel_sdvo)
  784. {
  785. struct intel_sdvo_encode encode;
  786. BUILD_BUG_ON(sizeof(encode) != 2);
  787. return intel_sdvo_get_value(intel_sdvo,
  788. SDVO_CMD_GET_SUPP_ENCODE,
  789. &encode, sizeof(encode));
  790. }
  791. static bool intel_sdvo_set_encode(struct intel_sdvo *intel_sdvo,
  792. uint8_t mode)
  793. {
  794. return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
  795. }
  796. static bool intel_sdvo_set_colorimetry(struct intel_sdvo *intel_sdvo,
  797. uint8_t mode)
  798. {
  799. return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
  800. }
  801. static bool intel_sdvo_set_audio_state(struct intel_sdvo *intel_sdvo,
  802. u8 audio_state)
  803. {
  804. return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_AUDIO_STAT,
  805. &audio_state, 1);
  806. }
  807. #if 0
  808. static void intel_sdvo_dump_hdmi_buf(struct intel_sdvo *intel_sdvo)
  809. {
  810. int i, j;
  811. uint8_t set_buf_index[2];
  812. uint8_t av_split;
  813. uint8_t buf_size;
  814. uint8_t buf[48];
  815. uint8_t *pos;
  816. intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
  817. for (i = 0; i <= av_split; i++) {
  818. set_buf_index[0] = i; set_buf_index[1] = 0;
  819. intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
  820. set_buf_index, 2);
  821. intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
  822. intel_sdvo_read_response(encoder, &buf_size, 1);
  823. pos = buf;
  824. for (j = 0; j <= buf_size; j += 8) {
  825. intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
  826. NULL, 0);
  827. intel_sdvo_read_response(encoder, pos, 8);
  828. pos += 8;
  829. }
  830. }
  831. }
  832. #endif
  833. static bool intel_sdvo_write_infoframe(struct intel_sdvo *intel_sdvo,
  834. unsigned if_index, uint8_t tx_rate,
  835. const uint8_t *data, unsigned length)
  836. {
  837. uint8_t set_buf_index[2] = { if_index, 0 };
  838. uint8_t hbuf_size, tmp[8];
  839. int i;
  840. if (!intel_sdvo_set_value(intel_sdvo,
  841. SDVO_CMD_SET_HBUF_INDEX,
  842. set_buf_index, 2))
  843. return false;
  844. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HBUF_INFO,
  845. &hbuf_size, 1))
  846. return false;
  847. /* Buffer size is 0 based, hooray! */
  848. hbuf_size++;
  849. DRM_DEBUG_KMS("writing sdvo hbuf: %i, hbuf_size %i, hbuf_size: %i\n",
  850. if_index, length, hbuf_size);
  851. for (i = 0; i < hbuf_size; i += 8) {
  852. memset(tmp, 0, 8);
  853. if (i < length)
  854. memcpy(tmp, data + i, min_t(unsigned, 8, length - i));
  855. if (!intel_sdvo_set_value(intel_sdvo,
  856. SDVO_CMD_SET_HBUF_DATA,
  857. tmp, 8))
  858. return false;
  859. }
  860. return intel_sdvo_set_value(intel_sdvo,
  861. SDVO_CMD_SET_HBUF_TXRATE,
  862. &tx_rate, 1);
  863. }
  864. static bool intel_sdvo_set_avi_infoframe(struct intel_sdvo *intel_sdvo,
  865. const struct intel_crtc_state *pipe_config)
  866. {
  867. uint8_t sdvo_data[HDMI_INFOFRAME_SIZE(AVI)];
  868. union hdmi_infoframe frame;
  869. int ret;
  870. ssize_t len;
  871. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
  872. &pipe_config->base.adjusted_mode,
  873. false);
  874. if (ret < 0) {
  875. DRM_ERROR("couldn't fill AVI infoframe\n");
  876. return false;
  877. }
  878. if (intel_sdvo->rgb_quant_range_selectable) {
  879. if (pipe_config->limited_color_range)
  880. frame.avi.quantization_range =
  881. HDMI_QUANTIZATION_RANGE_LIMITED;
  882. else
  883. frame.avi.quantization_range =
  884. HDMI_QUANTIZATION_RANGE_FULL;
  885. }
  886. len = hdmi_infoframe_pack(&frame, sdvo_data, sizeof(sdvo_data));
  887. if (len < 0)
  888. return false;
  889. return intel_sdvo_write_infoframe(intel_sdvo, SDVO_HBUF_INDEX_AVI_IF,
  890. SDVO_HBUF_TX_VSYNC,
  891. sdvo_data, sizeof(sdvo_data));
  892. }
  893. static bool intel_sdvo_set_tv_format(struct intel_sdvo *intel_sdvo,
  894. const struct drm_connector_state *conn_state)
  895. {
  896. struct intel_sdvo_tv_format format;
  897. uint32_t format_map;
  898. format_map = 1 << conn_state->tv.mode;
  899. memset(&format, 0, sizeof(format));
  900. memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
  901. BUILD_BUG_ON(sizeof(format) != 6);
  902. return intel_sdvo_set_value(intel_sdvo,
  903. SDVO_CMD_SET_TV_FORMAT,
  904. &format, sizeof(format));
  905. }
  906. static bool
  907. intel_sdvo_set_output_timings_from_mode(struct intel_sdvo *intel_sdvo,
  908. const struct drm_display_mode *mode)
  909. {
  910. struct intel_sdvo_dtd output_dtd;
  911. if (!intel_sdvo_set_target_output(intel_sdvo,
  912. intel_sdvo->attached_output))
  913. return false;
  914. intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  915. if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
  916. return false;
  917. return true;
  918. }
  919. /*
  920. * Asks the sdvo controller for the preferred input mode given the output mode.
  921. * Unfortunately we have to set up the full output mode to do that.
  922. */
  923. static bool
  924. intel_sdvo_get_preferred_input_mode(struct intel_sdvo *intel_sdvo,
  925. const struct drm_display_mode *mode,
  926. struct drm_display_mode *adjusted_mode)
  927. {
  928. struct intel_sdvo_dtd input_dtd;
  929. /* Reset the input timing to the screen. Assume always input 0. */
  930. if (!intel_sdvo_set_target_input(intel_sdvo))
  931. return false;
  932. if (!intel_sdvo_create_preferred_input_timing(intel_sdvo,
  933. mode->clock / 10,
  934. mode->hdisplay,
  935. mode->vdisplay))
  936. return false;
  937. if (!intel_sdvo_get_preferred_input_timing(intel_sdvo,
  938. &input_dtd))
  939. return false;
  940. intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
  941. intel_sdvo->dtd_sdvo_flags = input_dtd.part2.sdvo_flags;
  942. return true;
  943. }
  944. static void i9xx_adjust_sdvo_tv_clock(struct intel_crtc_state *pipe_config)
  945. {
  946. unsigned dotclock = pipe_config->port_clock;
  947. struct dpll *clock = &pipe_config->dpll;
  948. /*
  949. * SDVO TV has fixed PLL values depend on its clock range,
  950. * this mirrors vbios setting.
  951. */
  952. if (dotclock >= 100000 && dotclock < 140500) {
  953. clock->p1 = 2;
  954. clock->p2 = 10;
  955. clock->n = 3;
  956. clock->m1 = 16;
  957. clock->m2 = 8;
  958. } else if (dotclock >= 140500 && dotclock <= 200000) {
  959. clock->p1 = 1;
  960. clock->p2 = 10;
  961. clock->n = 6;
  962. clock->m1 = 12;
  963. clock->m2 = 8;
  964. } else {
  965. WARN(1, "SDVO TV clock out of range: %i\n", dotclock);
  966. }
  967. pipe_config->clock_set = true;
  968. }
  969. static bool intel_sdvo_compute_config(struct intel_encoder *encoder,
  970. struct intel_crtc_state *pipe_config,
  971. struct drm_connector_state *conn_state)
  972. {
  973. struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
  974. struct intel_sdvo_connector_state *intel_sdvo_state =
  975. to_intel_sdvo_connector_state(conn_state);
  976. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  977. struct drm_display_mode *mode = &pipe_config->base.mode;
  978. DRM_DEBUG_KMS("forcing bpc to 8 for SDVO\n");
  979. pipe_config->pipe_bpp = 8*3;
  980. if (HAS_PCH_SPLIT(to_i915(encoder->base.dev)))
  981. pipe_config->has_pch_encoder = true;
  982. /*
  983. * We need to construct preferred input timings based on our
  984. * output timings. To do that, we have to set the output
  985. * timings, even though this isn't really the right place in
  986. * the sequence to do it. Oh well.
  987. */
  988. if (intel_sdvo->is_tv) {
  989. if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
  990. return false;
  991. (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
  992. mode,
  993. adjusted_mode);
  994. pipe_config->sdvo_tv_clock = true;
  995. } else if (intel_sdvo->is_lvds) {
  996. if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
  997. intel_sdvo->sdvo_lvds_fixed_mode))
  998. return false;
  999. (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
  1000. mode,
  1001. adjusted_mode);
  1002. }
  1003. if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1004. return false;
  1005. /*
  1006. * Make the CRTC code factor in the SDVO pixel multiplier. The
  1007. * SDVO device will factor out the multiplier during mode_set.
  1008. */
  1009. pipe_config->pixel_multiplier =
  1010. intel_sdvo_get_pixel_multiplier(adjusted_mode);
  1011. if (intel_sdvo_state->base.force_audio != HDMI_AUDIO_OFF_DVI)
  1012. pipe_config->has_hdmi_sink = intel_sdvo->has_hdmi_monitor;
  1013. if (intel_sdvo_state->base.force_audio == HDMI_AUDIO_ON ||
  1014. (intel_sdvo_state->base.force_audio == HDMI_AUDIO_AUTO && intel_sdvo->has_hdmi_audio))
  1015. pipe_config->has_audio = true;
  1016. if (intel_sdvo_state->base.broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
  1017. /*
  1018. * See CEA-861-E - 5.1 Default Encoding Parameters
  1019. *
  1020. * FIXME: This bit is only valid when using TMDS encoding and 8
  1021. * bit per color mode.
  1022. */
  1023. if (pipe_config->has_hdmi_sink &&
  1024. drm_match_cea_mode(adjusted_mode) > 1)
  1025. pipe_config->limited_color_range = true;
  1026. } else {
  1027. if (pipe_config->has_hdmi_sink &&
  1028. intel_sdvo_state->base.broadcast_rgb == INTEL_BROADCAST_RGB_LIMITED)
  1029. pipe_config->limited_color_range = true;
  1030. }
  1031. /* Clock computation needs to happen after pixel multiplier. */
  1032. if (intel_sdvo->is_tv)
  1033. i9xx_adjust_sdvo_tv_clock(pipe_config);
  1034. /* Set user selected PAR to incoming mode's member */
  1035. if (intel_sdvo->is_hdmi)
  1036. adjusted_mode->picture_aspect_ratio = conn_state->picture_aspect_ratio;
  1037. return true;
  1038. }
  1039. #define UPDATE_PROPERTY(input, NAME) \
  1040. do { \
  1041. val = input; \
  1042. intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_##NAME, &val, sizeof(val)); \
  1043. } while (0)
  1044. static void intel_sdvo_update_props(struct intel_sdvo *intel_sdvo,
  1045. const struct intel_sdvo_connector_state *sdvo_state)
  1046. {
  1047. const struct drm_connector_state *conn_state = &sdvo_state->base.base;
  1048. struct intel_sdvo_connector *intel_sdvo_conn =
  1049. to_intel_sdvo_connector(conn_state->connector);
  1050. uint16_t val;
  1051. if (intel_sdvo_conn->left)
  1052. UPDATE_PROPERTY(sdvo_state->tv.overscan_h, OVERSCAN_H);
  1053. if (intel_sdvo_conn->top)
  1054. UPDATE_PROPERTY(sdvo_state->tv.overscan_v, OVERSCAN_V);
  1055. if (intel_sdvo_conn->hpos)
  1056. UPDATE_PROPERTY(sdvo_state->tv.hpos, HPOS);
  1057. if (intel_sdvo_conn->vpos)
  1058. UPDATE_PROPERTY(sdvo_state->tv.vpos, VPOS);
  1059. if (intel_sdvo_conn->saturation)
  1060. UPDATE_PROPERTY(conn_state->tv.saturation, SATURATION);
  1061. if (intel_sdvo_conn->contrast)
  1062. UPDATE_PROPERTY(conn_state->tv.contrast, CONTRAST);
  1063. if (intel_sdvo_conn->hue)
  1064. UPDATE_PROPERTY(conn_state->tv.hue, HUE);
  1065. if (intel_sdvo_conn->brightness)
  1066. UPDATE_PROPERTY(conn_state->tv.brightness, BRIGHTNESS);
  1067. if (intel_sdvo_conn->sharpness)
  1068. UPDATE_PROPERTY(sdvo_state->tv.sharpness, SHARPNESS);
  1069. if (intel_sdvo_conn->flicker_filter)
  1070. UPDATE_PROPERTY(sdvo_state->tv.flicker_filter, FLICKER_FILTER);
  1071. if (intel_sdvo_conn->flicker_filter_2d)
  1072. UPDATE_PROPERTY(sdvo_state->tv.flicker_filter_2d, FLICKER_FILTER_2D);
  1073. if (intel_sdvo_conn->flicker_filter_adaptive)
  1074. UPDATE_PROPERTY(sdvo_state->tv.flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
  1075. if (intel_sdvo_conn->tv_chroma_filter)
  1076. UPDATE_PROPERTY(sdvo_state->tv.chroma_filter, TV_CHROMA_FILTER);
  1077. if (intel_sdvo_conn->tv_luma_filter)
  1078. UPDATE_PROPERTY(sdvo_state->tv.luma_filter, TV_LUMA_FILTER);
  1079. if (intel_sdvo_conn->dot_crawl)
  1080. UPDATE_PROPERTY(sdvo_state->tv.dot_crawl, DOT_CRAWL);
  1081. #undef UPDATE_PROPERTY
  1082. }
  1083. static void intel_sdvo_pre_enable(struct intel_encoder *intel_encoder,
  1084. const struct intel_crtc_state *crtc_state,
  1085. const struct drm_connector_state *conn_state)
  1086. {
  1087. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  1088. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1089. const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
  1090. const struct intel_sdvo_connector_state *sdvo_state =
  1091. to_intel_sdvo_connector_state(conn_state);
  1092. const struct drm_display_mode *mode = &crtc_state->base.mode;
  1093. struct intel_sdvo *intel_sdvo = to_sdvo(intel_encoder);
  1094. u32 sdvox;
  1095. struct intel_sdvo_in_out_map in_out;
  1096. struct intel_sdvo_dtd input_dtd, output_dtd;
  1097. int rate;
  1098. intel_sdvo_update_props(intel_sdvo, sdvo_state);
  1099. /*
  1100. * First, set the input mapping for the first input to our controlled
  1101. * output. This is only correct if we're a single-input device, in
  1102. * which case the first input is the output from the appropriate SDVO
  1103. * channel on the motherboard. In a two-input device, the first input
  1104. * will be SDVOB and the second SDVOC.
  1105. */
  1106. in_out.in0 = intel_sdvo->attached_output;
  1107. in_out.in1 = 0;
  1108. intel_sdvo_set_value(intel_sdvo,
  1109. SDVO_CMD_SET_IN_OUT_MAP,
  1110. &in_out, sizeof(in_out));
  1111. /* Set the output timings to the screen */
  1112. if (!intel_sdvo_set_target_output(intel_sdvo,
  1113. intel_sdvo->attached_output))
  1114. return;
  1115. /* lvds has a special fixed output timing. */
  1116. if (intel_sdvo->is_lvds)
  1117. intel_sdvo_get_dtd_from_mode(&output_dtd,
  1118. intel_sdvo->sdvo_lvds_fixed_mode);
  1119. else
  1120. intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  1121. if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
  1122. DRM_INFO("Setting output timings on %s failed\n",
  1123. SDVO_NAME(intel_sdvo));
  1124. /* Set the input timing to the screen. Assume always input 0. */
  1125. if (!intel_sdvo_set_target_input(intel_sdvo))
  1126. return;
  1127. if (crtc_state->has_hdmi_sink) {
  1128. intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
  1129. intel_sdvo_set_colorimetry(intel_sdvo,
  1130. SDVO_COLORIMETRY_RGB256);
  1131. intel_sdvo_set_avi_infoframe(intel_sdvo, crtc_state);
  1132. } else
  1133. intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_DVI);
  1134. if (intel_sdvo->is_tv &&
  1135. !intel_sdvo_set_tv_format(intel_sdvo, conn_state))
  1136. return;
  1137. intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
  1138. if (intel_sdvo->is_tv || intel_sdvo->is_lvds)
  1139. input_dtd.part2.sdvo_flags = intel_sdvo->dtd_sdvo_flags;
  1140. if (!intel_sdvo_set_input_timing(intel_sdvo, &input_dtd))
  1141. DRM_INFO("Setting input timings on %s failed\n",
  1142. SDVO_NAME(intel_sdvo));
  1143. switch (crtc_state->pixel_multiplier) {
  1144. default:
  1145. WARN(1, "unknown pixel multiplier specified\n");
  1146. /* fall through */
  1147. case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
  1148. case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
  1149. case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
  1150. }
  1151. if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
  1152. return;
  1153. /* Set the SDVO control regs. */
  1154. if (INTEL_GEN(dev_priv) >= 4) {
  1155. /* The real mode polarity is set by the SDVO commands, using
  1156. * struct intel_sdvo_dtd. */
  1157. sdvox = SDVO_VSYNC_ACTIVE_HIGH | SDVO_HSYNC_ACTIVE_HIGH;
  1158. if (!HAS_PCH_SPLIT(dev_priv) && crtc_state->limited_color_range)
  1159. sdvox |= HDMI_COLOR_RANGE_16_235;
  1160. if (INTEL_GEN(dev_priv) < 5)
  1161. sdvox |= SDVO_BORDER_ENABLE;
  1162. } else {
  1163. sdvox = I915_READ(intel_sdvo->sdvo_reg);
  1164. if (intel_sdvo->port == PORT_B)
  1165. sdvox &= SDVOB_PRESERVE_MASK;
  1166. else
  1167. sdvox &= SDVOC_PRESERVE_MASK;
  1168. sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
  1169. }
  1170. if (HAS_PCH_CPT(dev_priv))
  1171. sdvox |= SDVO_PIPE_SEL_CPT(crtc->pipe);
  1172. else
  1173. sdvox |= SDVO_PIPE_SEL(crtc->pipe);
  1174. if (INTEL_GEN(dev_priv) >= 4) {
  1175. /* done in crtc_mode_set as the dpll_md reg must be written early */
  1176. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  1177. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  1178. /* done in crtc_mode_set as it lives inside the dpll register */
  1179. } else {
  1180. sdvox |= (crtc_state->pixel_multiplier - 1)
  1181. << SDVO_PORT_MULTIPLY_SHIFT;
  1182. }
  1183. if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL &&
  1184. INTEL_GEN(dev_priv) < 5)
  1185. sdvox |= SDVO_STALL_SELECT;
  1186. intel_sdvo_write_sdvox(intel_sdvo, sdvox);
  1187. }
  1188. static bool intel_sdvo_connector_get_hw_state(struct intel_connector *connector)
  1189. {
  1190. struct intel_sdvo_connector *intel_sdvo_connector =
  1191. to_intel_sdvo_connector(&connector->base);
  1192. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(&connector->base);
  1193. u16 active_outputs = 0;
  1194. intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
  1195. return active_outputs & intel_sdvo_connector->output_flag;
  1196. }
  1197. bool intel_sdvo_port_enabled(struct drm_i915_private *dev_priv,
  1198. i915_reg_t sdvo_reg, enum pipe *pipe)
  1199. {
  1200. u32 val;
  1201. val = I915_READ(sdvo_reg);
  1202. /* asserts want to know the pipe even if the port is disabled */
  1203. if (HAS_PCH_CPT(dev_priv))
  1204. *pipe = (val & SDVO_PIPE_SEL_MASK_CPT) >> SDVO_PIPE_SEL_SHIFT_CPT;
  1205. else if (IS_CHERRYVIEW(dev_priv))
  1206. *pipe = (val & SDVO_PIPE_SEL_MASK_CHV) >> SDVO_PIPE_SEL_SHIFT_CHV;
  1207. else
  1208. *pipe = (val & SDVO_PIPE_SEL_MASK) >> SDVO_PIPE_SEL_SHIFT;
  1209. return val & SDVO_ENABLE;
  1210. }
  1211. static bool intel_sdvo_get_hw_state(struct intel_encoder *encoder,
  1212. enum pipe *pipe)
  1213. {
  1214. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  1215. struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
  1216. u16 active_outputs = 0;
  1217. bool ret;
  1218. intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
  1219. ret = intel_sdvo_port_enabled(dev_priv, intel_sdvo->sdvo_reg, pipe);
  1220. return ret || active_outputs;
  1221. }
  1222. static void intel_sdvo_get_config(struct intel_encoder *encoder,
  1223. struct intel_crtc_state *pipe_config)
  1224. {
  1225. struct drm_device *dev = encoder->base.dev;
  1226. struct drm_i915_private *dev_priv = to_i915(dev);
  1227. struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
  1228. struct intel_sdvo_dtd dtd;
  1229. int encoder_pixel_multiplier = 0;
  1230. int dotclock;
  1231. u32 flags = 0, sdvox;
  1232. u8 val;
  1233. bool ret;
  1234. pipe_config->output_types |= BIT(INTEL_OUTPUT_SDVO);
  1235. sdvox = I915_READ(intel_sdvo->sdvo_reg);
  1236. ret = intel_sdvo_get_input_timing(intel_sdvo, &dtd);
  1237. if (!ret) {
  1238. /*
  1239. * Some sdvo encoders are not spec compliant and don't
  1240. * implement the mandatory get_timings function.
  1241. */
  1242. DRM_DEBUG_DRIVER("failed to retrieve SDVO DTD\n");
  1243. pipe_config->quirks |= PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS;
  1244. } else {
  1245. if (dtd.part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
  1246. flags |= DRM_MODE_FLAG_PHSYNC;
  1247. else
  1248. flags |= DRM_MODE_FLAG_NHSYNC;
  1249. if (dtd.part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
  1250. flags |= DRM_MODE_FLAG_PVSYNC;
  1251. else
  1252. flags |= DRM_MODE_FLAG_NVSYNC;
  1253. }
  1254. pipe_config->base.adjusted_mode.flags |= flags;
  1255. /*
  1256. * pixel multiplier readout is tricky: Only on i915g/gm it is stored in
  1257. * the sdvo port register, on all other platforms it is part of the dpll
  1258. * state. Since the general pipe state readout happens before the
  1259. * encoder->get_config we so already have a valid pixel multplier on all
  1260. * other platfroms.
  1261. */
  1262. if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
  1263. pipe_config->pixel_multiplier =
  1264. ((sdvox & SDVO_PORT_MULTIPLY_MASK)
  1265. >> SDVO_PORT_MULTIPLY_SHIFT) + 1;
  1266. }
  1267. dotclock = pipe_config->port_clock;
  1268. if (pipe_config->pixel_multiplier)
  1269. dotclock /= pipe_config->pixel_multiplier;
  1270. pipe_config->base.adjusted_mode.crtc_clock = dotclock;
  1271. /* Cross check the port pixel multiplier with the sdvo encoder state. */
  1272. if (intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_CLOCK_RATE_MULT,
  1273. &val, 1)) {
  1274. switch (val) {
  1275. case SDVO_CLOCK_RATE_MULT_1X:
  1276. encoder_pixel_multiplier = 1;
  1277. break;
  1278. case SDVO_CLOCK_RATE_MULT_2X:
  1279. encoder_pixel_multiplier = 2;
  1280. break;
  1281. case SDVO_CLOCK_RATE_MULT_4X:
  1282. encoder_pixel_multiplier = 4;
  1283. break;
  1284. }
  1285. }
  1286. if (sdvox & HDMI_COLOR_RANGE_16_235)
  1287. pipe_config->limited_color_range = true;
  1288. if (intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_AUDIO_STAT,
  1289. &val, 1)) {
  1290. u8 mask = SDVO_AUDIO_ELD_VALID | SDVO_AUDIO_PRESENCE_DETECT;
  1291. if ((val & mask) == mask)
  1292. pipe_config->has_audio = true;
  1293. }
  1294. if (intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_ENCODE,
  1295. &val, 1)) {
  1296. if (val == SDVO_ENCODE_HDMI)
  1297. pipe_config->has_hdmi_sink = true;
  1298. }
  1299. WARN(encoder_pixel_multiplier != pipe_config->pixel_multiplier,
  1300. "SDVO pixel multiplier mismatch, port: %i, encoder: %i\n",
  1301. pipe_config->pixel_multiplier, encoder_pixel_multiplier);
  1302. }
  1303. static void intel_sdvo_disable_audio(struct intel_sdvo *intel_sdvo)
  1304. {
  1305. intel_sdvo_set_audio_state(intel_sdvo, 0);
  1306. }
  1307. static void intel_sdvo_enable_audio(struct intel_sdvo *intel_sdvo,
  1308. const struct intel_crtc_state *crtc_state,
  1309. const struct drm_connector_state *conn_state)
  1310. {
  1311. const struct drm_display_mode *adjusted_mode =
  1312. &crtc_state->base.adjusted_mode;
  1313. struct drm_connector *connector = conn_state->connector;
  1314. u8 *eld = connector->eld;
  1315. eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
  1316. intel_sdvo_set_audio_state(intel_sdvo, 0);
  1317. intel_sdvo_write_infoframe(intel_sdvo, SDVO_HBUF_INDEX_ELD,
  1318. SDVO_HBUF_TX_DISABLED,
  1319. eld, drm_eld_size(eld));
  1320. intel_sdvo_set_audio_state(intel_sdvo, SDVO_AUDIO_ELD_VALID |
  1321. SDVO_AUDIO_PRESENCE_DETECT);
  1322. }
  1323. static void intel_disable_sdvo(struct intel_encoder *encoder,
  1324. const struct intel_crtc_state *old_crtc_state,
  1325. const struct drm_connector_state *conn_state)
  1326. {
  1327. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  1328. struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
  1329. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1330. u32 temp;
  1331. if (old_crtc_state->has_audio)
  1332. intel_sdvo_disable_audio(intel_sdvo);
  1333. intel_sdvo_set_active_outputs(intel_sdvo, 0);
  1334. if (0)
  1335. intel_sdvo_set_encoder_power_state(intel_sdvo,
  1336. DRM_MODE_DPMS_OFF);
  1337. temp = I915_READ(intel_sdvo->sdvo_reg);
  1338. temp &= ~SDVO_ENABLE;
  1339. intel_sdvo_write_sdvox(intel_sdvo, temp);
  1340. /*
  1341. * HW workaround for IBX, we need to move the port
  1342. * to transcoder A after disabling it to allow the
  1343. * matching DP port to be enabled on transcoder A.
  1344. */
  1345. if (HAS_PCH_IBX(dev_priv) && crtc->pipe == PIPE_B) {
  1346. /*
  1347. * We get CPU/PCH FIFO underruns on the other pipe when
  1348. * doing the workaround. Sweep them under the rug.
  1349. */
  1350. intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  1351. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  1352. temp &= ~SDVO_PIPE_SEL_MASK;
  1353. temp |= SDVO_ENABLE | SDVO_PIPE_SEL(PIPE_A);
  1354. intel_sdvo_write_sdvox(intel_sdvo, temp);
  1355. temp &= ~SDVO_ENABLE;
  1356. intel_sdvo_write_sdvox(intel_sdvo, temp);
  1357. intel_wait_for_vblank_if_active(dev_priv, PIPE_A);
  1358. intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  1359. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  1360. }
  1361. }
  1362. static void pch_disable_sdvo(struct intel_encoder *encoder,
  1363. const struct intel_crtc_state *old_crtc_state,
  1364. const struct drm_connector_state *old_conn_state)
  1365. {
  1366. }
  1367. static void pch_post_disable_sdvo(struct intel_encoder *encoder,
  1368. const struct intel_crtc_state *old_crtc_state,
  1369. const struct drm_connector_state *old_conn_state)
  1370. {
  1371. intel_disable_sdvo(encoder, old_crtc_state, old_conn_state);
  1372. }
  1373. static void intel_enable_sdvo(struct intel_encoder *encoder,
  1374. const struct intel_crtc_state *pipe_config,
  1375. const struct drm_connector_state *conn_state)
  1376. {
  1377. struct drm_device *dev = encoder->base.dev;
  1378. struct drm_i915_private *dev_priv = to_i915(dev);
  1379. struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
  1380. struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
  1381. u32 temp;
  1382. bool input1, input2;
  1383. int i;
  1384. bool success;
  1385. temp = I915_READ(intel_sdvo->sdvo_reg);
  1386. temp |= SDVO_ENABLE;
  1387. intel_sdvo_write_sdvox(intel_sdvo, temp);
  1388. for (i = 0; i < 2; i++)
  1389. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  1390. success = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
  1391. /*
  1392. * Warn if the device reported failure to sync.
  1393. *
  1394. * A lot of SDVO devices fail to notify of sync, but it's
  1395. * a given it the status is a success, we succeeded.
  1396. */
  1397. if (success && !input1) {
  1398. DRM_DEBUG_KMS("First %s output reported failure to "
  1399. "sync\n", SDVO_NAME(intel_sdvo));
  1400. }
  1401. if (0)
  1402. intel_sdvo_set_encoder_power_state(intel_sdvo,
  1403. DRM_MODE_DPMS_ON);
  1404. intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
  1405. if (pipe_config->has_audio)
  1406. intel_sdvo_enable_audio(intel_sdvo, pipe_config, conn_state);
  1407. }
  1408. static enum drm_mode_status
  1409. intel_sdvo_mode_valid(struct drm_connector *connector,
  1410. struct drm_display_mode *mode)
  1411. {
  1412. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1413. int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
  1414. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1415. return MODE_NO_DBLESCAN;
  1416. if (intel_sdvo->pixel_clock_min > mode->clock)
  1417. return MODE_CLOCK_LOW;
  1418. if (intel_sdvo->pixel_clock_max < mode->clock)
  1419. return MODE_CLOCK_HIGH;
  1420. if (mode->clock > max_dotclk)
  1421. return MODE_CLOCK_HIGH;
  1422. if (intel_sdvo->is_lvds) {
  1423. if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
  1424. return MODE_PANEL;
  1425. if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
  1426. return MODE_PANEL;
  1427. }
  1428. return MODE_OK;
  1429. }
  1430. static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
  1431. {
  1432. BUILD_BUG_ON(sizeof(*caps) != 8);
  1433. if (!intel_sdvo_get_value(intel_sdvo,
  1434. SDVO_CMD_GET_DEVICE_CAPS,
  1435. caps, sizeof(*caps)))
  1436. return false;
  1437. DRM_DEBUG_KMS("SDVO capabilities:\n"
  1438. " vendor_id: %d\n"
  1439. " device_id: %d\n"
  1440. " device_rev_id: %d\n"
  1441. " sdvo_version_major: %d\n"
  1442. " sdvo_version_minor: %d\n"
  1443. " sdvo_inputs_mask: %d\n"
  1444. " smooth_scaling: %d\n"
  1445. " sharp_scaling: %d\n"
  1446. " up_scaling: %d\n"
  1447. " down_scaling: %d\n"
  1448. " stall_support: %d\n"
  1449. " output_flags: %d\n",
  1450. caps->vendor_id,
  1451. caps->device_id,
  1452. caps->device_rev_id,
  1453. caps->sdvo_version_major,
  1454. caps->sdvo_version_minor,
  1455. caps->sdvo_inputs_mask,
  1456. caps->smooth_scaling,
  1457. caps->sharp_scaling,
  1458. caps->up_scaling,
  1459. caps->down_scaling,
  1460. caps->stall_support,
  1461. caps->output_flags);
  1462. return true;
  1463. }
  1464. static uint16_t intel_sdvo_get_hotplug_support(struct intel_sdvo *intel_sdvo)
  1465. {
  1466. struct drm_i915_private *dev_priv = to_i915(intel_sdvo->base.base.dev);
  1467. uint16_t hotplug;
  1468. if (!I915_HAS_HOTPLUG(dev_priv))
  1469. return 0;
  1470. /*
  1471. * HW Erratum: SDVO Hotplug is broken on all i945G chips, there's noise
  1472. * on the line.
  1473. */
  1474. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
  1475. return 0;
  1476. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
  1477. &hotplug, sizeof(hotplug)))
  1478. return 0;
  1479. return hotplug;
  1480. }
  1481. static void intel_sdvo_enable_hotplug(struct intel_encoder *encoder)
  1482. {
  1483. struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
  1484. intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG,
  1485. &intel_sdvo->hotplug_active, 2);
  1486. }
  1487. static bool intel_sdvo_hotplug(struct intel_encoder *encoder,
  1488. struct intel_connector *connector)
  1489. {
  1490. intel_sdvo_enable_hotplug(encoder);
  1491. return intel_encoder_hotplug(encoder, connector);
  1492. }
  1493. static bool
  1494. intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
  1495. {
  1496. /* Is there more than one type of output? */
  1497. return hweight16(intel_sdvo->caps.output_flags) > 1;
  1498. }
  1499. static struct edid *
  1500. intel_sdvo_get_edid(struct drm_connector *connector)
  1501. {
  1502. struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
  1503. return drm_get_edid(connector, &sdvo->ddc);
  1504. }
  1505. /* Mac mini hack -- use the same DDC as the analog connector */
  1506. static struct edid *
  1507. intel_sdvo_get_analog_edid(struct drm_connector *connector)
  1508. {
  1509. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  1510. return drm_get_edid(connector,
  1511. intel_gmbus_get_adapter(dev_priv,
  1512. dev_priv->vbt.crt_ddc_pin));
  1513. }
  1514. static enum drm_connector_status
  1515. intel_sdvo_tmds_sink_detect(struct drm_connector *connector)
  1516. {
  1517. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1518. enum drm_connector_status status;
  1519. struct edid *edid;
  1520. edid = intel_sdvo_get_edid(connector);
  1521. if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
  1522. u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
  1523. /*
  1524. * Don't use the 1 as the argument of DDC bus switch to get
  1525. * the EDID. It is used for SDVO SPD ROM.
  1526. */
  1527. for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
  1528. intel_sdvo->ddc_bus = ddc;
  1529. edid = intel_sdvo_get_edid(connector);
  1530. if (edid)
  1531. break;
  1532. }
  1533. /*
  1534. * If we found the EDID on the other bus,
  1535. * assume that is the correct DDC bus.
  1536. */
  1537. if (edid == NULL)
  1538. intel_sdvo->ddc_bus = saved_ddc;
  1539. }
  1540. /*
  1541. * When there is no edid and no monitor is connected with VGA
  1542. * port, try to use the CRT ddc to read the EDID for DVI-connector.
  1543. */
  1544. if (edid == NULL)
  1545. edid = intel_sdvo_get_analog_edid(connector);
  1546. status = connector_status_unknown;
  1547. if (edid != NULL) {
  1548. /* DDC bus is shared, match EDID to connector type */
  1549. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  1550. status = connector_status_connected;
  1551. if (intel_sdvo->is_hdmi) {
  1552. intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
  1553. intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
  1554. intel_sdvo->rgb_quant_range_selectable =
  1555. drm_rgb_quant_range_selectable(edid);
  1556. }
  1557. } else
  1558. status = connector_status_disconnected;
  1559. kfree(edid);
  1560. }
  1561. return status;
  1562. }
  1563. static bool
  1564. intel_sdvo_connector_matches_edid(struct intel_sdvo_connector *sdvo,
  1565. struct edid *edid)
  1566. {
  1567. bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
  1568. bool connector_is_digital = !!IS_DIGITAL(sdvo);
  1569. DRM_DEBUG_KMS("connector_is_digital? %d, monitor_is_digital? %d\n",
  1570. connector_is_digital, monitor_is_digital);
  1571. return connector_is_digital == monitor_is_digital;
  1572. }
  1573. static enum drm_connector_status
  1574. intel_sdvo_detect(struct drm_connector *connector, bool force)
  1575. {
  1576. uint16_t response;
  1577. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1578. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1579. enum drm_connector_status ret;
  1580. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1581. connector->base.id, connector->name);
  1582. if (!intel_sdvo_get_value(intel_sdvo,
  1583. SDVO_CMD_GET_ATTACHED_DISPLAYS,
  1584. &response, 2))
  1585. return connector_status_unknown;
  1586. DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
  1587. response & 0xff, response >> 8,
  1588. intel_sdvo_connector->output_flag);
  1589. if (response == 0)
  1590. return connector_status_disconnected;
  1591. intel_sdvo->attached_output = response;
  1592. intel_sdvo->has_hdmi_monitor = false;
  1593. intel_sdvo->has_hdmi_audio = false;
  1594. intel_sdvo->rgb_quant_range_selectable = false;
  1595. if ((intel_sdvo_connector->output_flag & response) == 0)
  1596. ret = connector_status_disconnected;
  1597. else if (IS_TMDS(intel_sdvo_connector))
  1598. ret = intel_sdvo_tmds_sink_detect(connector);
  1599. else {
  1600. struct edid *edid;
  1601. /* if we have an edid check it matches the connection */
  1602. edid = intel_sdvo_get_edid(connector);
  1603. if (edid == NULL)
  1604. edid = intel_sdvo_get_analog_edid(connector);
  1605. if (edid != NULL) {
  1606. if (intel_sdvo_connector_matches_edid(intel_sdvo_connector,
  1607. edid))
  1608. ret = connector_status_connected;
  1609. else
  1610. ret = connector_status_disconnected;
  1611. kfree(edid);
  1612. } else
  1613. ret = connector_status_connected;
  1614. }
  1615. /* May update encoder flag for like clock for SDVO TV, etc.*/
  1616. if (ret == connector_status_connected) {
  1617. intel_sdvo->is_tv = false;
  1618. intel_sdvo->is_lvds = false;
  1619. if (response & SDVO_TV_MASK)
  1620. intel_sdvo->is_tv = true;
  1621. if (response & SDVO_LVDS_MASK)
  1622. intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
  1623. }
  1624. return ret;
  1625. }
  1626. static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
  1627. {
  1628. struct edid *edid;
  1629. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1630. connector->base.id, connector->name);
  1631. /* set the bus switch and get the modes */
  1632. edid = intel_sdvo_get_edid(connector);
  1633. /*
  1634. * Mac mini hack. On this device, the DVI-I connector shares one DDC
  1635. * link between analog and digital outputs. So, if the regular SDVO
  1636. * DDC fails, check to see if the analog output is disconnected, in
  1637. * which case we'll look there for the digital DDC data.
  1638. */
  1639. if (edid == NULL)
  1640. edid = intel_sdvo_get_analog_edid(connector);
  1641. if (edid != NULL) {
  1642. if (intel_sdvo_connector_matches_edid(to_intel_sdvo_connector(connector),
  1643. edid)) {
  1644. drm_connector_update_edid_property(connector, edid);
  1645. drm_add_edid_modes(connector, edid);
  1646. }
  1647. kfree(edid);
  1648. }
  1649. }
  1650. /*
  1651. * Set of SDVO TV modes.
  1652. * Note! This is in reply order (see loop in get_tv_modes).
  1653. * XXX: all 60Hz refresh?
  1654. */
  1655. static const struct drm_display_mode sdvo_tv_modes[] = {
  1656. { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
  1657. 416, 0, 200, 201, 232, 233, 0,
  1658. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1659. { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
  1660. 416, 0, 240, 241, 272, 273, 0,
  1661. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1662. { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
  1663. 496, 0, 300, 301, 332, 333, 0,
  1664. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1665. { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
  1666. 736, 0, 350, 351, 382, 383, 0,
  1667. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1668. { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
  1669. 736, 0, 400, 401, 432, 433, 0,
  1670. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1671. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
  1672. 736, 0, 480, 481, 512, 513, 0,
  1673. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1674. { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
  1675. 800, 0, 480, 481, 512, 513, 0,
  1676. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1677. { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
  1678. 800, 0, 576, 577, 608, 609, 0,
  1679. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1680. { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
  1681. 816, 0, 350, 351, 382, 383, 0,
  1682. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1683. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
  1684. 816, 0, 400, 401, 432, 433, 0,
  1685. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1686. { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
  1687. 816, 0, 480, 481, 512, 513, 0,
  1688. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1689. { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
  1690. 816, 0, 540, 541, 572, 573, 0,
  1691. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1692. { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
  1693. 816, 0, 576, 577, 608, 609, 0,
  1694. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1695. { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
  1696. 864, 0, 576, 577, 608, 609, 0,
  1697. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1698. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
  1699. 896, 0, 600, 601, 632, 633, 0,
  1700. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1701. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
  1702. 928, 0, 624, 625, 656, 657, 0,
  1703. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1704. { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
  1705. 1016, 0, 766, 767, 798, 799, 0,
  1706. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1707. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
  1708. 1120, 0, 768, 769, 800, 801, 0,
  1709. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1710. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
  1711. 1376, 0, 1024, 1025, 1056, 1057, 0,
  1712. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1713. };
  1714. static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
  1715. {
  1716. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1717. const struct drm_connector_state *conn_state = connector->state;
  1718. struct intel_sdvo_sdtv_resolution_request tv_res;
  1719. uint32_t reply = 0, format_map = 0;
  1720. int i;
  1721. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1722. connector->base.id, connector->name);
  1723. /*
  1724. * Read the list of supported input resolutions for the selected TV
  1725. * format.
  1726. */
  1727. format_map = 1 << conn_state->tv.mode;
  1728. memcpy(&tv_res, &format_map,
  1729. min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
  1730. if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
  1731. return;
  1732. BUILD_BUG_ON(sizeof(tv_res) != 3);
  1733. if (!intel_sdvo_write_cmd(intel_sdvo,
  1734. SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
  1735. &tv_res, sizeof(tv_res)))
  1736. return;
  1737. if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
  1738. return;
  1739. for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
  1740. if (reply & (1 << i)) {
  1741. struct drm_display_mode *nmode;
  1742. nmode = drm_mode_duplicate(connector->dev,
  1743. &sdvo_tv_modes[i]);
  1744. if (nmode)
  1745. drm_mode_probed_add(connector, nmode);
  1746. }
  1747. }
  1748. static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
  1749. {
  1750. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1751. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  1752. struct drm_display_mode *newmode;
  1753. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1754. connector->base.id, connector->name);
  1755. /*
  1756. * Fetch modes from VBT. For SDVO prefer the VBT mode since some
  1757. * SDVO->LVDS transcoders can't cope with the EDID mode.
  1758. */
  1759. if (dev_priv->vbt.sdvo_lvds_vbt_mode != NULL) {
  1760. newmode = drm_mode_duplicate(connector->dev,
  1761. dev_priv->vbt.sdvo_lvds_vbt_mode);
  1762. if (newmode != NULL) {
  1763. /* Guarantee the mode is preferred */
  1764. newmode->type = (DRM_MODE_TYPE_PREFERRED |
  1765. DRM_MODE_TYPE_DRIVER);
  1766. drm_mode_probed_add(connector, newmode);
  1767. }
  1768. }
  1769. /*
  1770. * Attempt to get the mode list from DDC.
  1771. * Assume that the preferred modes are
  1772. * arranged in priority order.
  1773. */
  1774. intel_ddc_get_modes(connector, &intel_sdvo->ddc);
  1775. list_for_each_entry(newmode, &connector->probed_modes, head) {
  1776. if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
  1777. intel_sdvo->sdvo_lvds_fixed_mode =
  1778. drm_mode_duplicate(connector->dev, newmode);
  1779. intel_sdvo->is_lvds = true;
  1780. break;
  1781. }
  1782. }
  1783. }
  1784. static int intel_sdvo_get_modes(struct drm_connector *connector)
  1785. {
  1786. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1787. if (IS_TV(intel_sdvo_connector))
  1788. intel_sdvo_get_tv_modes(connector);
  1789. else if (IS_LVDS(intel_sdvo_connector))
  1790. intel_sdvo_get_lvds_modes(connector);
  1791. else
  1792. intel_sdvo_get_ddc_modes(connector);
  1793. return !list_empty(&connector->probed_modes);
  1794. }
  1795. static void intel_sdvo_destroy(struct drm_connector *connector)
  1796. {
  1797. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1798. drm_connector_cleanup(connector);
  1799. kfree(intel_sdvo_connector);
  1800. }
  1801. static int
  1802. intel_sdvo_connector_atomic_get_property(struct drm_connector *connector,
  1803. const struct drm_connector_state *state,
  1804. struct drm_property *property,
  1805. uint64_t *val)
  1806. {
  1807. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1808. const struct intel_sdvo_connector_state *sdvo_state = to_intel_sdvo_connector_state((void *)state);
  1809. if (property == intel_sdvo_connector->tv_format) {
  1810. int i;
  1811. for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
  1812. if (state->tv.mode == intel_sdvo_connector->tv_format_supported[i]) {
  1813. *val = i;
  1814. return 0;
  1815. }
  1816. WARN_ON(1);
  1817. *val = 0;
  1818. } else if (property == intel_sdvo_connector->top ||
  1819. property == intel_sdvo_connector->bottom)
  1820. *val = intel_sdvo_connector->max_vscan - sdvo_state->tv.overscan_v;
  1821. else if (property == intel_sdvo_connector->left ||
  1822. property == intel_sdvo_connector->right)
  1823. *val = intel_sdvo_connector->max_hscan - sdvo_state->tv.overscan_h;
  1824. else if (property == intel_sdvo_connector->hpos)
  1825. *val = sdvo_state->tv.hpos;
  1826. else if (property == intel_sdvo_connector->vpos)
  1827. *val = sdvo_state->tv.vpos;
  1828. else if (property == intel_sdvo_connector->saturation)
  1829. *val = state->tv.saturation;
  1830. else if (property == intel_sdvo_connector->contrast)
  1831. *val = state->tv.contrast;
  1832. else if (property == intel_sdvo_connector->hue)
  1833. *val = state->tv.hue;
  1834. else if (property == intel_sdvo_connector->brightness)
  1835. *val = state->tv.brightness;
  1836. else if (property == intel_sdvo_connector->sharpness)
  1837. *val = sdvo_state->tv.sharpness;
  1838. else if (property == intel_sdvo_connector->flicker_filter)
  1839. *val = sdvo_state->tv.flicker_filter;
  1840. else if (property == intel_sdvo_connector->flicker_filter_2d)
  1841. *val = sdvo_state->tv.flicker_filter_2d;
  1842. else if (property == intel_sdvo_connector->flicker_filter_adaptive)
  1843. *val = sdvo_state->tv.flicker_filter_adaptive;
  1844. else if (property == intel_sdvo_connector->tv_chroma_filter)
  1845. *val = sdvo_state->tv.chroma_filter;
  1846. else if (property == intel_sdvo_connector->tv_luma_filter)
  1847. *val = sdvo_state->tv.luma_filter;
  1848. else if (property == intel_sdvo_connector->dot_crawl)
  1849. *val = sdvo_state->tv.dot_crawl;
  1850. else
  1851. return intel_digital_connector_atomic_get_property(connector, state, property, val);
  1852. return 0;
  1853. }
  1854. static int
  1855. intel_sdvo_connector_atomic_set_property(struct drm_connector *connector,
  1856. struct drm_connector_state *state,
  1857. struct drm_property *property,
  1858. uint64_t val)
  1859. {
  1860. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1861. struct intel_sdvo_connector_state *sdvo_state = to_intel_sdvo_connector_state(state);
  1862. if (property == intel_sdvo_connector->tv_format) {
  1863. state->tv.mode = intel_sdvo_connector->tv_format_supported[val];
  1864. if (state->crtc) {
  1865. struct drm_crtc_state *crtc_state =
  1866. drm_atomic_get_new_crtc_state(state->state, state->crtc);
  1867. crtc_state->connectors_changed = true;
  1868. }
  1869. } else if (property == intel_sdvo_connector->top ||
  1870. property == intel_sdvo_connector->bottom)
  1871. /* Cannot set these independent from each other */
  1872. sdvo_state->tv.overscan_v = intel_sdvo_connector->max_vscan - val;
  1873. else if (property == intel_sdvo_connector->left ||
  1874. property == intel_sdvo_connector->right)
  1875. /* Cannot set these independent from each other */
  1876. sdvo_state->tv.overscan_h = intel_sdvo_connector->max_hscan - val;
  1877. else if (property == intel_sdvo_connector->hpos)
  1878. sdvo_state->tv.hpos = val;
  1879. else if (property == intel_sdvo_connector->vpos)
  1880. sdvo_state->tv.vpos = val;
  1881. else if (property == intel_sdvo_connector->saturation)
  1882. state->tv.saturation = val;
  1883. else if (property == intel_sdvo_connector->contrast)
  1884. state->tv.contrast = val;
  1885. else if (property == intel_sdvo_connector->hue)
  1886. state->tv.hue = val;
  1887. else if (property == intel_sdvo_connector->brightness)
  1888. state->tv.brightness = val;
  1889. else if (property == intel_sdvo_connector->sharpness)
  1890. sdvo_state->tv.sharpness = val;
  1891. else if (property == intel_sdvo_connector->flicker_filter)
  1892. sdvo_state->tv.flicker_filter = val;
  1893. else if (property == intel_sdvo_connector->flicker_filter_2d)
  1894. sdvo_state->tv.flicker_filter_2d = val;
  1895. else if (property == intel_sdvo_connector->flicker_filter_adaptive)
  1896. sdvo_state->tv.flicker_filter_adaptive = val;
  1897. else if (property == intel_sdvo_connector->tv_chroma_filter)
  1898. sdvo_state->tv.chroma_filter = val;
  1899. else if (property == intel_sdvo_connector->tv_luma_filter)
  1900. sdvo_state->tv.luma_filter = val;
  1901. else if (property == intel_sdvo_connector->dot_crawl)
  1902. sdvo_state->tv.dot_crawl = val;
  1903. else
  1904. return intel_digital_connector_atomic_set_property(connector, state, property, val);
  1905. return 0;
  1906. }
  1907. static int
  1908. intel_sdvo_connector_register(struct drm_connector *connector)
  1909. {
  1910. struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
  1911. int ret;
  1912. ret = intel_connector_register(connector);
  1913. if (ret)
  1914. return ret;
  1915. return sysfs_create_link(&connector->kdev->kobj,
  1916. &sdvo->ddc.dev.kobj,
  1917. sdvo->ddc.dev.kobj.name);
  1918. }
  1919. static void
  1920. intel_sdvo_connector_unregister(struct drm_connector *connector)
  1921. {
  1922. struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
  1923. sysfs_remove_link(&connector->kdev->kobj,
  1924. sdvo->ddc.dev.kobj.name);
  1925. intel_connector_unregister(connector);
  1926. }
  1927. static struct drm_connector_state *
  1928. intel_sdvo_connector_duplicate_state(struct drm_connector *connector)
  1929. {
  1930. struct intel_sdvo_connector_state *state;
  1931. state = kmemdup(connector->state, sizeof(*state), GFP_KERNEL);
  1932. if (!state)
  1933. return NULL;
  1934. __drm_atomic_helper_connector_duplicate_state(connector, &state->base.base);
  1935. return &state->base.base;
  1936. }
  1937. static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
  1938. .detect = intel_sdvo_detect,
  1939. .fill_modes = drm_helper_probe_single_connector_modes,
  1940. .atomic_get_property = intel_sdvo_connector_atomic_get_property,
  1941. .atomic_set_property = intel_sdvo_connector_atomic_set_property,
  1942. .late_register = intel_sdvo_connector_register,
  1943. .early_unregister = intel_sdvo_connector_unregister,
  1944. .destroy = intel_sdvo_destroy,
  1945. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1946. .atomic_duplicate_state = intel_sdvo_connector_duplicate_state,
  1947. };
  1948. static int intel_sdvo_atomic_check(struct drm_connector *conn,
  1949. struct drm_connector_state *new_conn_state)
  1950. {
  1951. struct drm_atomic_state *state = new_conn_state->state;
  1952. struct drm_connector_state *old_conn_state =
  1953. drm_atomic_get_old_connector_state(state, conn);
  1954. struct intel_sdvo_connector_state *old_state =
  1955. to_intel_sdvo_connector_state(old_conn_state);
  1956. struct intel_sdvo_connector_state *new_state =
  1957. to_intel_sdvo_connector_state(new_conn_state);
  1958. if (new_conn_state->crtc &&
  1959. (memcmp(&old_state->tv, &new_state->tv, sizeof(old_state->tv)) ||
  1960. memcmp(&old_conn_state->tv, &new_conn_state->tv, sizeof(old_conn_state->tv)))) {
  1961. struct drm_crtc_state *crtc_state =
  1962. drm_atomic_get_new_crtc_state(new_conn_state->state,
  1963. new_conn_state->crtc);
  1964. crtc_state->connectors_changed = true;
  1965. }
  1966. return intel_digital_connector_atomic_check(conn, new_conn_state);
  1967. }
  1968. static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
  1969. .get_modes = intel_sdvo_get_modes,
  1970. .mode_valid = intel_sdvo_mode_valid,
  1971. .atomic_check = intel_sdvo_atomic_check,
  1972. };
  1973. static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
  1974. {
  1975. struct intel_sdvo *intel_sdvo = to_sdvo(to_intel_encoder(encoder));
  1976. if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
  1977. drm_mode_destroy(encoder->dev,
  1978. intel_sdvo->sdvo_lvds_fixed_mode);
  1979. i2c_del_adapter(&intel_sdvo->ddc);
  1980. intel_encoder_destroy(encoder);
  1981. }
  1982. static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
  1983. .destroy = intel_sdvo_enc_destroy,
  1984. };
  1985. static void
  1986. intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
  1987. {
  1988. uint16_t mask = 0;
  1989. unsigned int num_bits;
  1990. /*
  1991. * Make a mask of outputs less than or equal to our own priority in the
  1992. * list.
  1993. */
  1994. switch (sdvo->controlled_output) {
  1995. case SDVO_OUTPUT_LVDS1:
  1996. mask |= SDVO_OUTPUT_LVDS1;
  1997. /* fall through */
  1998. case SDVO_OUTPUT_LVDS0:
  1999. mask |= SDVO_OUTPUT_LVDS0;
  2000. /* fall through */
  2001. case SDVO_OUTPUT_TMDS1:
  2002. mask |= SDVO_OUTPUT_TMDS1;
  2003. /* fall through */
  2004. case SDVO_OUTPUT_TMDS0:
  2005. mask |= SDVO_OUTPUT_TMDS0;
  2006. /* fall through */
  2007. case SDVO_OUTPUT_RGB1:
  2008. mask |= SDVO_OUTPUT_RGB1;
  2009. /* fall through */
  2010. case SDVO_OUTPUT_RGB0:
  2011. mask |= SDVO_OUTPUT_RGB0;
  2012. break;
  2013. }
  2014. /* Count bits to find what number we are in the priority list. */
  2015. mask &= sdvo->caps.output_flags;
  2016. num_bits = hweight16(mask);
  2017. /* If more than 3 outputs, default to DDC bus 3 for now. */
  2018. if (num_bits > 3)
  2019. num_bits = 3;
  2020. /* Corresponds to SDVO_CONTROL_BUS_DDCx */
  2021. sdvo->ddc_bus = 1 << num_bits;
  2022. }
  2023. /*
  2024. * Choose the appropriate DDC bus for control bus switch command for this
  2025. * SDVO output based on the controlled output.
  2026. *
  2027. * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
  2028. * outputs, then LVDS outputs.
  2029. */
  2030. static void
  2031. intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
  2032. struct intel_sdvo *sdvo)
  2033. {
  2034. struct sdvo_device_mapping *mapping;
  2035. if (sdvo->port == PORT_B)
  2036. mapping = &dev_priv->vbt.sdvo_mappings[0];
  2037. else
  2038. mapping = &dev_priv->vbt.sdvo_mappings[1];
  2039. if (mapping->initialized)
  2040. sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
  2041. else
  2042. intel_sdvo_guess_ddc_bus(sdvo);
  2043. }
  2044. static void
  2045. intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
  2046. struct intel_sdvo *sdvo)
  2047. {
  2048. struct sdvo_device_mapping *mapping;
  2049. u8 pin;
  2050. if (sdvo->port == PORT_B)
  2051. mapping = &dev_priv->vbt.sdvo_mappings[0];
  2052. else
  2053. mapping = &dev_priv->vbt.sdvo_mappings[1];
  2054. if (mapping->initialized &&
  2055. intel_gmbus_is_valid_pin(dev_priv, mapping->i2c_pin))
  2056. pin = mapping->i2c_pin;
  2057. else
  2058. pin = GMBUS_PIN_DPB;
  2059. sdvo->i2c = intel_gmbus_get_adapter(dev_priv, pin);
  2060. /*
  2061. * With gmbus we should be able to drive sdvo i2c at 2MHz, but somehow
  2062. * our code totally fails once we start using gmbus. Hence fall back to
  2063. * bit banging for now.
  2064. */
  2065. intel_gmbus_force_bit(sdvo->i2c, true);
  2066. }
  2067. /* undo any changes intel_sdvo_select_i2c_bus() did to sdvo->i2c */
  2068. static void
  2069. intel_sdvo_unselect_i2c_bus(struct intel_sdvo *sdvo)
  2070. {
  2071. intel_gmbus_force_bit(sdvo->i2c, false);
  2072. }
  2073. static bool
  2074. intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
  2075. {
  2076. return intel_sdvo_check_supp_encode(intel_sdvo);
  2077. }
  2078. static u8
  2079. intel_sdvo_get_slave_addr(struct drm_i915_private *dev_priv,
  2080. struct intel_sdvo *sdvo)
  2081. {
  2082. struct sdvo_device_mapping *my_mapping, *other_mapping;
  2083. if (sdvo->port == PORT_B) {
  2084. my_mapping = &dev_priv->vbt.sdvo_mappings[0];
  2085. other_mapping = &dev_priv->vbt.sdvo_mappings[1];
  2086. } else {
  2087. my_mapping = &dev_priv->vbt.sdvo_mappings[1];
  2088. other_mapping = &dev_priv->vbt.sdvo_mappings[0];
  2089. }
  2090. /* If the BIOS described our SDVO device, take advantage of it. */
  2091. if (my_mapping->slave_addr)
  2092. return my_mapping->slave_addr;
  2093. /*
  2094. * If the BIOS only described a different SDVO device, use the
  2095. * address that it isn't using.
  2096. */
  2097. if (other_mapping->slave_addr) {
  2098. if (other_mapping->slave_addr == 0x70)
  2099. return 0x72;
  2100. else
  2101. return 0x70;
  2102. }
  2103. /*
  2104. * No SDVO device info is found for another DVO port,
  2105. * so use mapping assumption we had before BIOS parsing.
  2106. */
  2107. if (sdvo->port == PORT_B)
  2108. return 0x70;
  2109. else
  2110. return 0x72;
  2111. }
  2112. static int
  2113. intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
  2114. struct intel_sdvo *encoder)
  2115. {
  2116. struct drm_connector *drm_connector;
  2117. int ret;
  2118. drm_connector = &connector->base.base;
  2119. ret = drm_connector_init(encoder->base.base.dev,
  2120. drm_connector,
  2121. &intel_sdvo_connector_funcs,
  2122. connector->base.base.connector_type);
  2123. if (ret < 0)
  2124. return ret;
  2125. drm_connector_helper_add(drm_connector,
  2126. &intel_sdvo_connector_helper_funcs);
  2127. connector->base.base.interlace_allowed = 1;
  2128. connector->base.base.doublescan_allowed = 0;
  2129. connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
  2130. connector->base.get_hw_state = intel_sdvo_connector_get_hw_state;
  2131. intel_connector_attach_encoder(&connector->base, &encoder->base);
  2132. return 0;
  2133. }
  2134. static void
  2135. intel_sdvo_add_hdmi_properties(struct intel_sdvo *intel_sdvo,
  2136. struct intel_sdvo_connector *connector)
  2137. {
  2138. struct drm_i915_private *dev_priv = to_i915(connector->base.base.dev);
  2139. intel_attach_force_audio_property(&connector->base.base);
  2140. if (INTEL_GEN(dev_priv) >= 4 && IS_MOBILE(dev_priv)) {
  2141. intel_attach_broadcast_rgb_property(&connector->base.base);
  2142. }
  2143. intel_attach_aspect_ratio_property(&connector->base.base);
  2144. connector->base.base.state->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
  2145. }
  2146. static struct intel_sdvo_connector *intel_sdvo_connector_alloc(void)
  2147. {
  2148. struct intel_sdvo_connector *sdvo_connector;
  2149. struct intel_sdvo_connector_state *conn_state;
  2150. sdvo_connector = kzalloc(sizeof(*sdvo_connector), GFP_KERNEL);
  2151. if (!sdvo_connector)
  2152. return NULL;
  2153. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  2154. if (!conn_state) {
  2155. kfree(sdvo_connector);
  2156. return NULL;
  2157. }
  2158. __drm_atomic_helper_connector_reset(&sdvo_connector->base.base,
  2159. &conn_state->base.base);
  2160. return sdvo_connector;
  2161. }
  2162. static bool
  2163. intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
  2164. {
  2165. struct drm_encoder *encoder = &intel_sdvo->base.base;
  2166. struct drm_connector *connector;
  2167. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2168. struct intel_connector *intel_connector;
  2169. struct intel_sdvo_connector *intel_sdvo_connector;
  2170. DRM_DEBUG_KMS("initialising DVI device %d\n", device);
  2171. intel_sdvo_connector = intel_sdvo_connector_alloc();
  2172. if (!intel_sdvo_connector)
  2173. return false;
  2174. if (device == 0) {
  2175. intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
  2176. intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
  2177. } else if (device == 1) {
  2178. intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
  2179. intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
  2180. }
  2181. intel_connector = &intel_sdvo_connector->base;
  2182. connector = &intel_connector->base;
  2183. if (intel_sdvo_get_hotplug_support(intel_sdvo) &
  2184. intel_sdvo_connector->output_flag) {
  2185. intel_sdvo->hotplug_active |= intel_sdvo_connector->output_flag;
  2186. /*
  2187. * Some SDVO devices have one-shot hotplug interrupts.
  2188. * Ensure that they get re-enabled when an interrupt happens.
  2189. */
  2190. intel_encoder->hotplug = intel_sdvo_hotplug;
  2191. intel_sdvo_enable_hotplug(intel_encoder);
  2192. } else {
  2193. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
  2194. }
  2195. encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
  2196. connector->connector_type = DRM_MODE_CONNECTOR_DVID;
  2197. if (intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
  2198. connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
  2199. intel_sdvo->is_hdmi = true;
  2200. }
  2201. if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
  2202. kfree(intel_sdvo_connector);
  2203. return false;
  2204. }
  2205. if (intel_sdvo->is_hdmi)
  2206. intel_sdvo_add_hdmi_properties(intel_sdvo, intel_sdvo_connector);
  2207. return true;
  2208. }
  2209. static bool
  2210. intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
  2211. {
  2212. struct drm_encoder *encoder = &intel_sdvo->base.base;
  2213. struct drm_connector *connector;
  2214. struct intel_connector *intel_connector;
  2215. struct intel_sdvo_connector *intel_sdvo_connector;
  2216. DRM_DEBUG_KMS("initialising TV type %d\n", type);
  2217. intel_sdvo_connector = intel_sdvo_connector_alloc();
  2218. if (!intel_sdvo_connector)
  2219. return false;
  2220. intel_connector = &intel_sdvo_connector->base;
  2221. connector = &intel_connector->base;
  2222. encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
  2223. connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
  2224. intel_sdvo->controlled_output |= type;
  2225. intel_sdvo_connector->output_flag = type;
  2226. intel_sdvo->is_tv = true;
  2227. if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
  2228. kfree(intel_sdvo_connector);
  2229. return false;
  2230. }
  2231. if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
  2232. goto err;
  2233. if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  2234. goto err;
  2235. return true;
  2236. err:
  2237. intel_sdvo_destroy(connector);
  2238. return false;
  2239. }
  2240. static bool
  2241. intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
  2242. {
  2243. struct drm_encoder *encoder = &intel_sdvo->base.base;
  2244. struct drm_connector *connector;
  2245. struct intel_connector *intel_connector;
  2246. struct intel_sdvo_connector *intel_sdvo_connector;
  2247. DRM_DEBUG_KMS("initialising analog device %d\n", device);
  2248. intel_sdvo_connector = intel_sdvo_connector_alloc();
  2249. if (!intel_sdvo_connector)
  2250. return false;
  2251. intel_connector = &intel_sdvo_connector->base;
  2252. connector = &intel_connector->base;
  2253. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  2254. encoder->encoder_type = DRM_MODE_ENCODER_DAC;
  2255. connector->connector_type = DRM_MODE_CONNECTOR_VGA;
  2256. if (device == 0) {
  2257. intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
  2258. intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
  2259. } else if (device == 1) {
  2260. intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
  2261. intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
  2262. }
  2263. if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
  2264. kfree(intel_sdvo_connector);
  2265. return false;
  2266. }
  2267. return true;
  2268. }
  2269. static bool
  2270. intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
  2271. {
  2272. struct drm_encoder *encoder = &intel_sdvo->base.base;
  2273. struct drm_connector *connector;
  2274. struct intel_connector *intel_connector;
  2275. struct intel_sdvo_connector *intel_sdvo_connector;
  2276. DRM_DEBUG_KMS("initialising LVDS device %d\n", device);
  2277. intel_sdvo_connector = intel_sdvo_connector_alloc();
  2278. if (!intel_sdvo_connector)
  2279. return false;
  2280. intel_connector = &intel_sdvo_connector->base;
  2281. connector = &intel_connector->base;
  2282. encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
  2283. connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
  2284. if (device == 0) {
  2285. intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
  2286. intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
  2287. } else if (device == 1) {
  2288. intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
  2289. intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
  2290. }
  2291. if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
  2292. kfree(intel_sdvo_connector);
  2293. return false;
  2294. }
  2295. if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  2296. goto err;
  2297. return true;
  2298. err:
  2299. intel_sdvo_destroy(connector);
  2300. return false;
  2301. }
  2302. static bool
  2303. intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
  2304. {
  2305. intel_sdvo->is_tv = false;
  2306. intel_sdvo->is_lvds = false;
  2307. /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
  2308. if (flags & SDVO_OUTPUT_TMDS0)
  2309. if (!intel_sdvo_dvi_init(intel_sdvo, 0))
  2310. return false;
  2311. if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
  2312. if (!intel_sdvo_dvi_init(intel_sdvo, 1))
  2313. return false;
  2314. /* TV has no XXX1 function block */
  2315. if (flags & SDVO_OUTPUT_SVID0)
  2316. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
  2317. return false;
  2318. if (flags & SDVO_OUTPUT_CVBS0)
  2319. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
  2320. return false;
  2321. if (flags & SDVO_OUTPUT_YPRPB0)
  2322. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_YPRPB0))
  2323. return false;
  2324. if (flags & SDVO_OUTPUT_RGB0)
  2325. if (!intel_sdvo_analog_init(intel_sdvo, 0))
  2326. return false;
  2327. if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
  2328. if (!intel_sdvo_analog_init(intel_sdvo, 1))
  2329. return false;
  2330. if (flags & SDVO_OUTPUT_LVDS0)
  2331. if (!intel_sdvo_lvds_init(intel_sdvo, 0))
  2332. return false;
  2333. if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
  2334. if (!intel_sdvo_lvds_init(intel_sdvo, 1))
  2335. return false;
  2336. if ((flags & SDVO_OUTPUT_MASK) == 0) {
  2337. unsigned char bytes[2];
  2338. intel_sdvo->controlled_output = 0;
  2339. memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
  2340. DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
  2341. SDVO_NAME(intel_sdvo),
  2342. bytes[0], bytes[1]);
  2343. return false;
  2344. }
  2345. intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  2346. return true;
  2347. }
  2348. static void intel_sdvo_output_cleanup(struct intel_sdvo *intel_sdvo)
  2349. {
  2350. struct drm_device *dev = intel_sdvo->base.base.dev;
  2351. struct drm_connector *connector, *tmp;
  2352. list_for_each_entry_safe(connector, tmp,
  2353. &dev->mode_config.connector_list, head) {
  2354. if (intel_attached_encoder(connector) == &intel_sdvo->base) {
  2355. drm_connector_unregister(connector);
  2356. intel_sdvo_destroy(connector);
  2357. }
  2358. }
  2359. }
  2360. static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
  2361. struct intel_sdvo_connector *intel_sdvo_connector,
  2362. int type)
  2363. {
  2364. struct drm_device *dev = intel_sdvo->base.base.dev;
  2365. struct intel_sdvo_tv_format format;
  2366. uint32_t format_map, i;
  2367. if (!intel_sdvo_set_target_output(intel_sdvo, type))
  2368. return false;
  2369. BUILD_BUG_ON(sizeof(format) != 6);
  2370. if (!intel_sdvo_get_value(intel_sdvo,
  2371. SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
  2372. &format, sizeof(format)))
  2373. return false;
  2374. memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
  2375. if (format_map == 0)
  2376. return false;
  2377. intel_sdvo_connector->format_supported_num = 0;
  2378. for (i = 0 ; i < TV_FORMAT_NUM; i++)
  2379. if (format_map & (1 << i))
  2380. intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
  2381. intel_sdvo_connector->tv_format =
  2382. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  2383. "mode", intel_sdvo_connector->format_supported_num);
  2384. if (!intel_sdvo_connector->tv_format)
  2385. return false;
  2386. for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
  2387. drm_property_add_enum(intel_sdvo_connector->tv_format, i,
  2388. tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
  2389. intel_sdvo_connector->base.base.state->tv.mode = intel_sdvo_connector->tv_format_supported[0];
  2390. drm_object_attach_property(&intel_sdvo_connector->base.base.base,
  2391. intel_sdvo_connector->tv_format, 0);
  2392. return true;
  2393. }
  2394. #define _ENHANCEMENT(state_assignment, name, NAME) do { \
  2395. if (enhancements.name) { \
  2396. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
  2397. !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
  2398. return false; \
  2399. intel_sdvo_connector->name = \
  2400. drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
  2401. if (!intel_sdvo_connector->name) return false; \
  2402. state_assignment = response; \
  2403. drm_object_attach_property(&connector->base, \
  2404. intel_sdvo_connector->name, 0); \
  2405. DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
  2406. data_value[0], data_value[1], response); \
  2407. } \
  2408. } while (0)
  2409. #define ENHANCEMENT(state, name, NAME) _ENHANCEMENT((state)->name, name, NAME)
  2410. static bool
  2411. intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
  2412. struct intel_sdvo_connector *intel_sdvo_connector,
  2413. struct intel_sdvo_enhancements_reply enhancements)
  2414. {
  2415. struct drm_device *dev = intel_sdvo->base.base.dev;
  2416. struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2417. struct drm_connector_state *conn_state = connector->state;
  2418. struct intel_sdvo_connector_state *sdvo_state =
  2419. to_intel_sdvo_connector_state(conn_state);
  2420. uint16_t response, data_value[2];
  2421. /* when horizontal overscan is supported, Add the left/right property */
  2422. if (enhancements.overscan_h) {
  2423. if (!intel_sdvo_get_value(intel_sdvo,
  2424. SDVO_CMD_GET_MAX_OVERSCAN_H,
  2425. &data_value, 4))
  2426. return false;
  2427. if (!intel_sdvo_get_value(intel_sdvo,
  2428. SDVO_CMD_GET_OVERSCAN_H,
  2429. &response, 2))
  2430. return false;
  2431. sdvo_state->tv.overscan_h = response;
  2432. intel_sdvo_connector->max_hscan = data_value[0];
  2433. intel_sdvo_connector->left =
  2434. drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
  2435. if (!intel_sdvo_connector->left)
  2436. return false;
  2437. drm_object_attach_property(&connector->base,
  2438. intel_sdvo_connector->left, 0);
  2439. intel_sdvo_connector->right =
  2440. drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
  2441. if (!intel_sdvo_connector->right)
  2442. return false;
  2443. drm_object_attach_property(&connector->base,
  2444. intel_sdvo_connector->right, 0);
  2445. DRM_DEBUG_KMS("h_overscan: max %d, "
  2446. "default %d, current %d\n",
  2447. data_value[0], data_value[1], response);
  2448. }
  2449. if (enhancements.overscan_v) {
  2450. if (!intel_sdvo_get_value(intel_sdvo,
  2451. SDVO_CMD_GET_MAX_OVERSCAN_V,
  2452. &data_value, 4))
  2453. return false;
  2454. if (!intel_sdvo_get_value(intel_sdvo,
  2455. SDVO_CMD_GET_OVERSCAN_V,
  2456. &response, 2))
  2457. return false;
  2458. sdvo_state->tv.overscan_v = response;
  2459. intel_sdvo_connector->max_vscan = data_value[0];
  2460. intel_sdvo_connector->top =
  2461. drm_property_create_range(dev, 0,
  2462. "top_margin", 0, data_value[0]);
  2463. if (!intel_sdvo_connector->top)
  2464. return false;
  2465. drm_object_attach_property(&connector->base,
  2466. intel_sdvo_connector->top, 0);
  2467. intel_sdvo_connector->bottom =
  2468. drm_property_create_range(dev, 0,
  2469. "bottom_margin", 0, data_value[0]);
  2470. if (!intel_sdvo_connector->bottom)
  2471. return false;
  2472. drm_object_attach_property(&connector->base,
  2473. intel_sdvo_connector->bottom, 0);
  2474. DRM_DEBUG_KMS("v_overscan: max %d, "
  2475. "default %d, current %d\n",
  2476. data_value[0], data_value[1], response);
  2477. }
  2478. ENHANCEMENT(&sdvo_state->tv, hpos, HPOS);
  2479. ENHANCEMENT(&sdvo_state->tv, vpos, VPOS);
  2480. ENHANCEMENT(&conn_state->tv, saturation, SATURATION);
  2481. ENHANCEMENT(&conn_state->tv, contrast, CONTRAST);
  2482. ENHANCEMENT(&conn_state->tv, hue, HUE);
  2483. ENHANCEMENT(&conn_state->tv, brightness, BRIGHTNESS);
  2484. ENHANCEMENT(&sdvo_state->tv, sharpness, SHARPNESS);
  2485. ENHANCEMENT(&sdvo_state->tv, flicker_filter, FLICKER_FILTER);
  2486. ENHANCEMENT(&sdvo_state->tv, flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
  2487. ENHANCEMENT(&sdvo_state->tv, flicker_filter_2d, FLICKER_FILTER_2D);
  2488. _ENHANCEMENT(sdvo_state->tv.chroma_filter, tv_chroma_filter, TV_CHROMA_FILTER);
  2489. _ENHANCEMENT(sdvo_state->tv.luma_filter, tv_luma_filter, TV_LUMA_FILTER);
  2490. if (enhancements.dot_crawl) {
  2491. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
  2492. return false;
  2493. sdvo_state->tv.dot_crawl = response & 0x1;
  2494. intel_sdvo_connector->dot_crawl =
  2495. drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
  2496. if (!intel_sdvo_connector->dot_crawl)
  2497. return false;
  2498. drm_object_attach_property(&connector->base,
  2499. intel_sdvo_connector->dot_crawl, 0);
  2500. DRM_DEBUG_KMS("dot crawl: current %d\n", response);
  2501. }
  2502. return true;
  2503. }
  2504. static bool
  2505. intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
  2506. struct intel_sdvo_connector *intel_sdvo_connector,
  2507. struct intel_sdvo_enhancements_reply enhancements)
  2508. {
  2509. struct drm_device *dev = intel_sdvo->base.base.dev;
  2510. struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2511. uint16_t response, data_value[2];
  2512. ENHANCEMENT(&connector->state->tv, brightness, BRIGHTNESS);
  2513. return true;
  2514. }
  2515. #undef ENHANCEMENT
  2516. #undef _ENHANCEMENT
  2517. static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
  2518. struct intel_sdvo_connector *intel_sdvo_connector)
  2519. {
  2520. union {
  2521. struct intel_sdvo_enhancements_reply reply;
  2522. uint16_t response;
  2523. } enhancements;
  2524. BUILD_BUG_ON(sizeof(enhancements) != 2);
  2525. if (!intel_sdvo_get_value(intel_sdvo,
  2526. SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
  2527. &enhancements, sizeof(enhancements)) ||
  2528. enhancements.response == 0) {
  2529. DRM_DEBUG_KMS("No enhancement is supported\n");
  2530. return true;
  2531. }
  2532. if (IS_TV(intel_sdvo_connector))
  2533. return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2534. else if (IS_LVDS(intel_sdvo_connector))
  2535. return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2536. else
  2537. return true;
  2538. }
  2539. static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
  2540. struct i2c_msg *msgs,
  2541. int num)
  2542. {
  2543. struct intel_sdvo *sdvo = adapter->algo_data;
  2544. if (!__intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
  2545. return -EIO;
  2546. return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
  2547. }
  2548. static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
  2549. {
  2550. struct intel_sdvo *sdvo = adapter->algo_data;
  2551. return sdvo->i2c->algo->functionality(sdvo->i2c);
  2552. }
  2553. static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
  2554. .master_xfer = intel_sdvo_ddc_proxy_xfer,
  2555. .functionality = intel_sdvo_ddc_proxy_func
  2556. };
  2557. static void proxy_lock_bus(struct i2c_adapter *adapter,
  2558. unsigned int flags)
  2559. {
  2560. struct intel_sdvo *sdvo = adapter->algo_data;
  2561. sdvo->i2c->lock_ops->lock_bus(sdvo->i2c, flags);
  2562. }
  2563. static int proxy_trylock_bus(struct i2c_adapter *adapter,
  2564. unsigned int flags)
  2565. {
  2566. struct intel_sdvo *sdvo = adapter->algo_data;
  2567. return sdvo->i2c->lock_ops->trylock_bus(sdvo->i2c, flags);
  2568. }
  2569. static void proxy_unlock_bus(struct i2c_adapter *adapter,
  2570. unsigned int flags)
  2571. {
  2572. struct intel_sdvo *sdvo = adapter->algo_data;
  2573. sdvo->i2c->lock_ops->unlock_bus(sdvo->i2c, flags);
  2574. }
  2575. static const struct i2c_lock_operations proxy_lock_ops = {
  2576. .lock_bus = proxy_lock_bus,
  2577. .trylock_bus = proxy_trylock_bus,
  2578. .unlock_bus = proxy_unlock_bus,
  2579. };
  2580. static bool
  2581. intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
  2582. struct drm_i915_private *dev_priv)
  2583. {
  2584. struct pci_dev *pdev = dev_priv->drm.pdev;
  2585. sdvo->ddc.owner = THIS_MODULE;
  2586. sdvo->ddc.class = I2C_CLASS_DDC;
  2587. snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
  2588. sdvo->ddc.dev.parent = &pdev->dev;
  2589. sdvo->ddc.algo_data = sdvo;
  2590. sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
  2591. sdvo->ddc.lock_ops = &proxy_lock_ops;
  2592. return i2c_add_adapter(&sdvo->ddc) == 0;
  2593. }
  2594. static void assert_sdvo_port_valid(const struct drm_i915_private *dev_priv,
  2595. enum port port)
  2596. {
  2597. if (HAS_PCH_SPLIT(dev_priv))
  2598. WARN_ON(port != PORT_B);
  2599. else
  2600. WARN_ON(port != PORT_B && port != PORT_C);
  2601. }
  2602. bool intel_sdvo_init(struct drm_i915_private *dev_priv,
  2603. i915_reg_t sdvo_reg, enum port port)
  2604. {
  2605. struct intel_encoder *intel_encoder;
  2606. struct intel_sdvo *intel_sdvo;
  2607. int i;
  2608. assert_sdvo_port_valid(dev_priv, port);
  2609. intel_sdvo = kzalloc(sizeof(*intel_sdvo), GFP_KERNEL);
  2610. if (!intel_sdvo)
  2611. return false;
  2612. intel_sdvo->sdvo_reg = sdvo_reg;
  2613. intel_sdvo->port = port;
  2614. intel_sdvo->slave_addr =
  2615. intel_sdvo_get_slave_addr(dev_priv, intel_sdvo) >> 1;
  2616. intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo);
  2617. if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev_priv))
  2618. goto err_i2c_bus;
  2619. /* encoder type will be decided later */
  2620. intel_encoder = &intel_sdvo->base;
  2621. intel_encoder->type = INTEL_OUTPUT_SDVO;
  2622. intel_encoder->power_domain = POWER_DOMAIN_PORT_OTHER;
  2623. intel_encoder->port = port;
  2624. drm_encoder_init(&dev_priv->drm, &intel_encoder->base,
  2625. &intel_sdvo_enc_funcs, 0,
  2626. "SDVO %c", port_name(port));
  2627. /* Read the regs to test if we can talk to the device */
  2628. for (i = 0; i < 0x40; i++) {
  2629. u8 byte;
  2630. if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
  2631. DRM_DEBUG_KMS("No SDVO device found on %s\n",
  2632. SDVO_NAME(intel_sdvo));
  2633. goto err;
  2634. }
  2635. }
  2636. intel_encoder->compute_config = intel_sdvo_compute_config;
  2637. if (HAS_PCH_SPLIT(dev_priv)) {
  2638. intel_encoder->disable = pch_disable_sdvo;
  2639. intel_encoder->post_disable = pch_post_disable_sdvo;
  2640. } else {
  2641. intel_encoder->disable = intel_disable_sdvo;
  2642. }
  2643. intel_encoder->pre_enable = intel_sdvo_pre_enable;
  2644. intel_encoder->enable = intel_enable_sdvo;
  2645. intel_encoder->get_hw_state = intel_sdvo_get_hw_state;
  2646. intel_encoder->get_config = intel_sdvo_get_config;
  2647. /* In default case sdvo lvds is false */
  2648. if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
  2649. goto err;
  2650. if (intel_sdvo_output_setup(intel_sdvo,
  2651. intel_sdvo->caps.output_flags) != true) {
  2652. DRM_DEBUG_KMS("SDVO output failed to setup on %s\n",
  2653. SDVO_NAME(intel_sdvo));
  2654. /* Output_setup can leave behind connectors! */
  2655. goto err_output;
  2656. }
  2657. /*
  2658. * Only enable the hotplug irq if we need it, to work around noisy
  2659. * hotplug lines.
  2660. */
  2661. if (intel_sdvo->hotplug_active) {
  2662. if (intel_sdvo->port == PORT_B)
  2663. intel_encoder->hpd_pin = HPD_SDVO_B;
  2664. else
  2665. intel_encoder->hpd_pin = HPD_SDVO_C;
  2666. }
  2667. /*
  2668. * Cloning SDVO with anything is often impossible, since the SDVO
  2669. * encoder can request a special input timing mode. And even if that's
  2670. * not the case we have evidence that cloning a plain unscaled mode with
  2671. * VGA doesn't really work. Furthermore the cloning flags are way too
  2672. * simplistic anyway to express such constraints, so just give up on
  2673. * cloning for SDVO encoders.
  2674. */
  2675. intel_sdvo->base.cloneable = 0;
  2676. intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo);
  2677. /* Set the input timing to the screen. Assume always input 0. */
  2678. if (!intel_sdvo_set_target_input(intel_sdvo))
  2679. goto err_output;
  2680. if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
  2681. &intel_sdvo->pixel_clock_min,
  2682. &intel_sdvo->pixel_clock_max))
  2683. goto err_output;
  2684. DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
  2685. "clock range %dMHz - %dMHz, "
  2686. "input 1: %c, input 2: %c, "
  2687. "output 1: %c, output 2: %c\n",
  2688. SDVO_NAME(intel_sdvo),
  2689. intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
  2690. intel_sdvo->caps.device_rev_id,
  2691. intel_sdvo->pixel_clock_min / 1000,
  2692. intel_sdvo->pixel_clock_max / 1000,
  2693. (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
  2694. (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
  2695. /* check currently supported outputs */
  2696. intel_sdvo->caps.output_flags &
  2697. (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
  2698. intel_sdvo->caps.output_flags &
  2699. (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
  2700. return true;
  2701. err_output:
  2702. intel_sdvo_output_cleanup(intel_sdvo);
  2703. err:
  2704. drm_encoder_cleanup(&intel_encoder->base);
  2705. i2c_del_adapter(&intel_sdvo->ddc);
  2706. err_i2c_bus:
  2707. intel_sdvo_unselect_i2c_bus(intel_sdvo);
  2708. kfree(intel_sdvo);
  2709. return false;
  2710. }