intel_sdvo.c 87 KB

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