intel_sdvo.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893
  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 void i9xx_adjust_sdvo_tv_clock(struct intel_crtc_config *pipe_config)
  899. {
  900. unsigned dotclock = pipe_config->adjusted_mode.clock;
  901. struct dpll *clock = &pipe_config->dpll;
  902. /* SDVO TV has fixed PLL values depend on its clock range,
  903. this mirrors vbios setting. */
  904. if (dotclock >= 100000 && dotclock < 140500) {
  905. clock->p1 = 2;
  906. clock->p2 = 10;
  907. clock->n = 3;
  908. clock->m1 = 16;
  909. clock->m2 = 8;
  910. } else if (dotclock >= 140500 && dotclock <= 200000) {
  911. clock->p1 = 1;
  912. clock->p2 = 10;
  913. clock->n = 6;
  914. clock->m1 = 12;
  915. clock->m2 = 8;
  916. } else {
  917. WARN(1, "SDVO TV clock out of range: %i\n", dotclock);
  918. }
  919. pipe_config->clock_set = true;
  920. }
  921. static bool intel_sdvo_compute_config(struct intel_encoder *encoder,
  922. struct intel_crtc_config *pipe_config)
  923. {
  924. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  925. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  926. struct drm_display_mode *mode = &pipe_config->requested_mode;
  927. DRM_DEBUG_KMS("forcing bpc to 8 for SDVO\n");
  928. pipe_config->pipe_bpp = 8*3;
  929. if (HAS_PCH_SPLIT(encoder->base.dev))
  930. pipe_config->has_pch_encoder = true;
  931. /* We need to construct preferred input timings based on our
  932. * output timings. To do that, we have to set the output
  933. * timings, even though this isn't really the right place in
  934. * the sequence to do it. Oh well.
  935. */
  936. if (intel_sdvo->is_tv) {
  937. if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
  938. return false;
  939. (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
  940. mode,
  941. adjusted_mode);
  942. pipe_config->sdvo_tv_clock = true;
  943. } else if (intel_sdvo->is_lvds) {
  944. if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
  945. intel_sdvo->sdvo_lvds_fixed_mode))
  946. return false;
  947. (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
  948. mode,
  949. adjusted_mode);
  950. }
  951. /* Make the CRTC code factor in the SDVO pixel multiplier. The
  952. * SDVO device will factor out the multiplier during mode_set.
  953. */
  954. pipe_config->pixel_multiplier =
  955. intel_sdvo_get_pixel_multiplier(adjusted_mode);
  956. adjusted_mode->clock *= pipe_config->pixel_multiplier;
  957. if (intel_sdvo->color_range_auto) {
  958. /* See CEA-861-E - 5.1 Default Encoding Parameters */
  959. /* FIXME: This bit is only valid when using TMDS encoding and 8
  960. * bit per color mode. */
  961. if (intel_sdvo->has_hdmi_monitor &&
  962. drm_match_cea_mode(adjusted_mode) > 1)
  963. intel_sdvo->color_range = HDMI_COLOR_RANGE_16_235;
  964. else
  965. intel_sdvo->color_range = 0;
  966. }
  967. if (intel_sdvo->color_range)
  968. pipe_config->limited_color_range = true;
  969. /* Clock computation needs to happen after pixel multiplier. */
  970. if (intel_sdvo->is_tv)
  971. i9xx_adjust_sdvo_tv_clock(pipe_config);
  972. return true;
  973. }
  974. static void intel_sdvo_mode_set(struct intel_encoder *intel_encoder)
  975. {
  976. struct drm_device *dev = intel_encoder->base.dev;
  977. struct drm_i915_private *dev_priv = dev->dev_private;
  978. struct drm_crtc *crtc = intel_encoder->base.crtc;
  979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  980. struct drm_display_mode *adjusted_mode =
  981. &intel_crtc->config.adjusted_mode;
  982. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  983. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&intel_encoder->base);
  984. u32 sdvox;
  985. struct intel_sdvo_in_out_map in_out;
  986. struct intel_sdvo_dtd input_dtd, output_dtd;
  987. int rate;
  988. if (!mode)
  989. return;
  990. /* First, set the input mapping for the first input to our controlled
  991. * output. This is only correct if we're a single-input device, in
  992. * which case the first input is the output from the appropriate SDVO
  993. * channel on the motherboard. In a two-input device, the first input
  994. * will be SDVOB and the second SDVOC.
  995. */
  996. in_out.in0 = intel_sdvo->attached_output;
  997. in_out.in1 = 0;
  998. intel_sdvo_set_value(intel_sdvo,
  999. SDVO_CMD_SET_IN_OUT_MAP,
  1000. &in_out, sizeof(in_out));
  1001. /* Set the output timings to the screen */
  1002. if (!intel_sdvo_set_target_output(intel_sdvo,
  1003. intel_sdvo->attached_output))
  1004. return;
  1005. /* lvds has a special fixed output timing. */
  1006. if (intel_sdvo->is_lvds)
  1007. intel_sdvo_get_dtd_from_mode(&output_dtd,
  1008. intel_sdvo->sdvo_lvds_fixed_mode);
  1009. else
  1010. intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  1011. if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
  1012. DRM_INFO("Setting output timings on %s failed\n",
  1013. SDVO_NAME(intel_sdvo));
  1014. /* Set the input timing to the screen. Assume always input 0. */
  1015. if (!intel_sdvo_set_target_input(intel_sdvo))
  1016. return;
  1017. if (intel_sdvo->has_hdmi_monitor) {
  1018. intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
  1019. intel_sdvo_set_colorimetry(intel_sdvo,
  1020. SDVO_COLORIMETRY_RGB256);
  1021. intel_sdvo_set_avi_infoframe(intel_sdvo, adjusted_mode);
  1022. } else
  1023. intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_DVI);
  1024. if (intel_sdvo->is_tv &&
  1025. !intel_sdvo_set_tv_format(intel_sdvo))
  1026. return;
  1027. /* We have tried to get input timing in mode_fixup, and filled into
  1028. * adjusted_mode.
  1029. */
  1030. intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
  1031. if (intel_sdvo->is_tv || intel_sdvo->is_lvds)
  1032. input_dtd.part2.sdvo_flags = intel_sdvo->dtd_sdvo_flags;
  1033. if (!intel_sdvo_set_input_timing(intel_sdvo, &input_dtd))
  1034. DRM_INFO("Setting input timings on %s failed\n",
  1035. SDVO_NAME(intel_sdvo));
  1036. switch (intel_crtc->config.pixel_multiplier) {
  1037. default:
  1038. case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
  1039. case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
  1040. case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
  1041. }
  1042. if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
  1043. return;
  1044. /* Set the SDVO control regs. */
  1045. if (INTEL_INFO(dev)->gen >= 4) {
  1046. /* The real mode polarity is set by the SDVO commands, using
  1047. * struct intel_sdvo_dtd. */
  1048. sdvox = SDVO_VSYNC_ACTIVE_HIGH | SDVO_HSYNC_ACTIVE_HIGH;
  1049. if (!HAS_PCH_SPLIT(dev) && intel_sdvo->is_hdmi)
  1050. sdvox |= intel_sdvo->color_range;
  1051. if (INTEL_INFO(dev)->gen < 5)
  1052. sdvox |= SDVO_BORDER_ENABLE;
  1053. } else {
  1054. sdvox = I915_READ(intel_sdvo->sdvo_reg);
  1055. switch (intel_sdvo->sdvo_reg) {
  1056. case GEN3_SDVOB:
  1057. sdvox &= SDVOB_PRESERVE_MASK;
  1058. break;
  1059. case GEN3_SDVOC:
  1060. sdvox &= SDVOC_PRESERVE_MASK;
  1061. break;
  1062. }
  1063. sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
  1064. }
  1065. if (INTEL_PCH_TYPE(dev) >= PCH_CPT)
  1066. sdvox |= SDVO_PIPE_SEL_CPT(intel_crtc->pipe);
  1067. else
  1068. sdvox |= SDVO_PIPE_SEL(intel_crtc->pipe);
  1069. if (intel_sdvo->has_hdmi_audio)
  1070. sdvox |= SDVO_AUDIO_ENABLE;
  1071. if (INTEL_INFO(dev)->gen >= 4) {
  1072. /* done in crtc_mode_set as the dpll_md reg must be written early */
  1073. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  1074. /* done in crtc_mode_set as it lives inside the dpll register */
  1075. } else {
  1076. sdvox |= (intel_crtc->config.pixel_multiplier - 1)
  1077. << SDVO_PORT_MULTIPLY_SHIFT;
  1078. }
  1079. if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL &&
  1080. INTEL_INFO(dev)->gen < 5)
  1081. sdvox |= SDVO_STALL_SELECT;
  1082. intel_sdvo_write_sdvox(intel_sdvo, sdvox);
  1083. }
  1084. static bool intel_sdvo_connector_get_hw_state(struct intel_connector *connector)
  1085. {
  1086. struct intel_sdvo_connector *intel_sdvo_connector =
  1087. to_intel_sdvo_connector(&connector->base);
  1088. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(&connector->base);
  1089. u16 active_outputs;
  1090. intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
  1091. if (active_outputs & intel_sdvo_connector->output_flag)
  1092. return true;
  1093. else
  1094. return false;
  1095. }
  1096. static bool intel_sdvo_get_hw_state(struct intel_encoder *encoder,
  1097. enum pipe *pipe)
  1098. {
  1099. struct drm_device *dev = encoder->base.dev;
  1100. struct drm_i915_private *dev_priv = dev->dev_private;
  1101. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1102. u16 active_outputs;
  1103. u32 tmp;
  1104. tmp = I915_READ(intel_sdvo->sdvo_reg);
  1105. intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
  1106. if (!(tmp & SDVO_ENABLE) && (active_outputs == 0))
  1107. return false;
  1108. if (HAS_PCH_CPT(dev))
  1109. *pipe = PORT_TO_PIPE_CPT(tmp);
  1110. else
  1111. *pipe = PORT_TO_PIPE(tmp);
  1112. return true;
  1113. }
  1114. static void intel_disable_sdvo(struct intel_encoder *encoder)
  1115. {
  1116. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  1117. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1118. u32 temp;
  1119. intel_sdvo_set_active_outputs(intel_sdvo, 0);
  1120. if (0)
  1121. intel_sdvo_set_encoder_power_state(intel_sdvo,
  1122. DRM_MODE_DPMS_OFF);
  1123. temp = I915_READ(intel_sdvo->sdvo_reg);
  1124. if ((temp & SDVO_ENABLE) != 0) {
  1125. /* HW workaround for IBX, we need to move the port to
  1126. * transcoder A before disabling it. */
  1127. if (HAS_PCH_IBX(encoder->base.dev)) {
  1128. struct drm_crtc *crtc = encoder->base.crtc;
  1129. int pipe = crtc ? to_intel_crtc(crtc)->pipe : -1;
  1130. if (temp & SDVO_PIPE_B_SELECT) {
  1131. temp &= ~SDVO_PIPE_B_SELECT;
  1132. I915_WRITE(intel_sdvo->sdvo_reg, temp);
  1133. POSTING_READ(intel_sdvo->sdvo_reg);
  1134. /* Again we need to write this twice. */
  1135. I915_WRITE(intel_sdvo->sdvo_reg, temp);
  1136. POSTING_READ(intel_sdvo->sdvo_reg);
  1137. /* Transcoder selection bits only update
  1138. * effectively on vblank. */
  1139. if (crtc)
  1140. intel_wait_for_vblank(encoder->base.dev, pipe);
  1141. else
  1142. msleep(50);
  1143. }
  1144. }
  1145. intel_sdvo_write_sdvox(intel_sdvo, temp & ~SDVO_ENABLE);
  1146. }
  1147. }
  1148. static void intel_enable_sdvo(struct intel_encoder *encoder)
  1149. {
  1150. struct drm_device *dev = encoder->base.dev;
  1151. struct drm_i915_private *dev_priv = dev->dev_private;
  1152. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1153. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  1154. u32 temp;
  1155. bool input1, input2;
  1156. int i;
  1157. u8 status;
  1158. temp = I915_READ(intel_sdvo->sdvo_reg);
  1159. if ((temp & SDVO_ENABLE) == 0) {
  1160. /* HW workaround for IBX, we need to move the port
  1161. * to transcoder A before disabling it, so restore it here. */
  1162. if (HAS_PCH_IBX(dev))
  1163. temp |= SDVO_PIPE_SEL(intel_crtc->pipe);
  1164. intel_sdvo_write_sdvox(intel_sdvo, temp | SDVO_ENABLE);
  1165. }
  1166. for (i = 0; i < 2; i++)
  1167. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1168. status = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
  1169. /* Warn if the device reported failure to sync.
  1170. * A lot of SDVO devices fail to notify of sync, but it's
  1171. * a given it the status is a success, we succeeded.
  1172. */
  1173. if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
  1174. DRM_DEBUG_KMS("First %s output reported failure to "
  1175. "sync\n", SDVO_NAME(intel_sdvo));
  1176. }
  1177. if (0)
  1178. intel_sdvo_set_encoder_power_state(intel_sdvo,
  1179. DRM_MODE_DPMS_ON);
  1180. intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
  1181. }
  1182. static void intel_sdvo_dpms(struct drm_connector *connector, int mode)
  1183. {
  1184. struct drm_crtc *crtc;
  1185. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1186. /* dvo supports only 2 dpms states. */
  1187. if (mode != DRM_MODE_DPMS_ON)
  1188. mode = DRM_MODE_DPMS_OFF;
  1189. if (mode == connector->dpms)
  1190. return;
  1191. connector->dpms = mode;
  1192. /* Only need to change hw state when actually enabled */
  1193. crtc = intel_sdvo->base.base.crtc;
  1194. if (!crtc) {
  1195. intel_sdvo->base.connectors_active = false;
  1196. return;
  1197. }
  1198. if (mode != DRM_MODE_DPMS_ON) {
  1199. intel_sdvo_set_active_outputs(intel_sdvo, 0);
  1200. if (0)
  1201. intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
  1202. intel_sdvo->base.connectors_active = false;
  1203. intel_crtc_update_dpms(crtc);
  1204. } else {
  1205. intel_sdvo->base.connectors_active = true;
  1206. intel_crtc_update_dpms(crtc);
  1207. if (0)
  1208. intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
  1209. intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
  1210. }
  1211. intel_modeset_check_state(connector->dev);
  1212. }
  1213. static int intel_sdvo_mode_valid(struct drm_connector *connector,
  1214. struct drm_display_mode *mode)
  1215. {
  1216. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1217. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1218. return MODE_NO_DBLESCAN;
  1219. if (intel_sdvo->pixel_clock_min > mode->clock)
  1220. return MODE_CLOCK_LOW;
  1221. if (intel_sdvo->pixel_clock_max < mode->clock)
  1222. return MODE_CLOCK_HIGH;
  1223. if (intel_sdvo->is_lvds) {
  1224. if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
  1225. return MODE_PANEL;
  1226. if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
  1227. return MODE_PANEL;
  1228. }
  1229. return MODE_OK;
  1230. }
  1231. static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
  1232. {
  1233. BUILD_BUG_ON(sizeof(*caps) != 8);
  1234. if (!intel_sdvo_get_value(intel_sdvo,
  1235. SDVO_CMD_GET_DEVICE_CAPS,
  1236. caps, sizeof(*caps)))
  1237. return false;
  1238. DRM_DEBUG_KMS("SDVO capabilities:\n"
  1239. " vendor_id: %d\n"
  1240. " device_id: %d\n"
  1241. " device_rev_id: %d\n"
  1242. " sdvo_version_major: %d\n"
  1243. " sdvo_version_minor: %d\n"
  1244. " sdvo_inputs_mask: %d\n"
  1245. " smooth_scaling: %d\n"
  1246. " sharp_scaling: %d\n"
  1247. " up_scaling: %d\n"
  1248. " down_scaling: %d\n"
  1249. " stall_support: %d\n"
  1250. " output_flags: %d\n",
  1251. caps->vendor_id,
  1252. caps->device_id,
  1253. caps->device_rev_id,
  1254. caps->sdvo_version_major,
  1255. caps->sdvo_version_minor,
  1256. caps->sdvo_inputs_mask,
  1257. caps->smooth_scaling,
  1258. caps->sharp_scaling,
  1259. caps->up_scaling,
  1260. caps->down_scaling,
  1261. caps->stall_support,
  1262. caps->output_flags);
  1263. return true;
  1264. }
  1265. static uint16_t intel_sdvo_get_hotplug_support(struct intel_sdvo *intel_sdvo)
  1266. {
  1267. struct drm_device *dev = intel_sdvo->base.base.dev;
  1268. uint16_t hotplug;
  1269. /* HW Erratum: SDVO Hotplug is broken on all i945G chips, there's noise
  1270. * on the line. */
  1271. if (IS_I945G(dev) || IS_I945GM(dev))
  1272. return 0;
  1273. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
  1274. &hotplug, sizeof(hotplug)))
  1275. return 0;
  1276. return hotplug;
  1277. }
  1278. static void intel_sdvo_enable_hotplug(struct intel_encoder *encoder)
  1279. {
  1280. struct intel_sdvo *intel_sdvo = to_intel_sdvo(&encoder->base);
  1281. intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG,
  1282. &intel_sdvo->hotplug_active, 2);
  1283. }
  1284. static bool
  1285. intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
  1286. {
  1287. /* Is there more than one type of output? */
  1288. return hweight16(intel_sdvo->caps.output_flags) > 1;
  1289. }
  1290. static struct edid *
  1291. intel_sdvo_get_edid(struct drm_connector *connector)
  1292. {
  1293. struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
  1294. return drm_get_edid(connector, &sdvo->ddc);
  1295. }
  1296. /* Mac mini hack -- use the same DDC as the analog connector */
  1297. static struct edid *
  1298. intel_sdvo_get_analog_edid(struct drm_connector *connector)
  1299. {
  1300. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1301. return drm_get_edid(connector,
  1302. intel_gmbus_get_adapter(dev_priv,
  1303. dev_priv->vbt.crt_ddc_pin));
  1304. }
  1305. static enum drm_connector_status
  1306. intel_sdvo_tmds_sink_detect(struct drm_connector *connector)
  1307. {
  1308. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1309. enum drm_connector_status status;
  1310. struct edid *edid;
  1311. edid = intel_sdvo_get_edid(connector);
  1312. if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
  1313. u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
  1314. /*
  1315. * Don't use the 1 as the argument of DDC bus switch to get
  1316. * the EDID. It is used for SDVO SPD ROM.
  1317. */
  1318. for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
  1319. intel_sdvo->ddc_bus = ddc;
  1320. edid = intel_sdvo_get_edid(connector);
  1321. if (edid)
  1322. break;
  1323. }
  1324. /*
  1325. * If we found the EDID on the other bus,
  1326. * assume that is the correct DDC bus.
  1327. */
  1328. if (edid == NULL)
  1329. intel_sdvo->ddc_bus = saved_ddc;
  1330. }
  1331. /*
  1332. * When there is no edid and no monitor is connected with VGA
  1333. * port, try to use the CRT ddc to read the EDID for DVI-connector.
  1334. */
  1335. if (edid == NULL)
  1336. edid = intel_sdvo_get_analog_edid(connector);
  1337. status = connector_status_unknown;
  1338. if (edid != NULL) {
  1339. /* DDC bus is shared, match EDID to connector type */
  1340. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  1341. status = connector_status_connected;
  1342. if (intel_sdvo->is_hdmi) {
  1343. intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
  1344. intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
  1345. intel_sdvo->rgb_quant_range_selectable =
  1346. drm_rgb_quant_range_selectable(edid);
  1347. }
  1348. } else
  1349. status = connector_status_disconnected;
  1350. kfree(edid);
  1351. }
  1352. if (status == connector_status_connected) {
  1353. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1354. if (intel_sdvo_connector->force_audio != HDMI_AUDIO_AUTO)
  1355. intel_sdvo->has_hdmi_audio = (intel_sdvo_connector->force_audio == HDMI_AUDIO_ON);
  1356. }
  1357. return status;
  1358. }
  1359. static bool
  1360. intel_sdvo_connector_matches_edid(struct intel_sdvo_connector *sdvo,
  1361. struct edid *edid)
  1362. {
  1363. bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
  1364. bool connector_is_digital = !!IS_DIGITAL(sdvo);
  1365. DRM_DEBUG_KMS("connector_is_digital? %d, monitor_is_digital? %d\n",
  1366. connector_is_digital, monitor_is_digital);
  1367. return connector_is_digital == monitor_is_digital;
  1368. }
  1369. static enum drm_connector_status
  1370. intel_sdvo_detect(struct drm_connector *connector, bool force)
  1371. {
  1372. uint16_t response;
  1373. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1374. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1375. enum drm_connector_status ret;
  1376. if (!intel_sdvo_get_value(intel_sdvo,
  1377. SDVO_CMD_GET_ATTACHED_DISPLAYS,
  1378. &response, 2))
  1379. return connector_status_unknown;
  1380. DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
  1381. response & 0xff, response >> 8,
  1382. intel_sdvo_connector->output_flag);
  1383. if (response == 0)
  1384. return connector_status_disconnected;
  1385. intel_sdvo->attached_output = response;
  1386. intel_sdvo->has_hdmi_monitor = false;
  1387. intel_sdvo->has_hdmi_audio = false;
  1388. intel_sdvo->rgb_quant_range_selectable = false;
  1389. if ((intel_sdvo_connector->output_flag & response) == 0)
  1390. ret = connector_status_disconnected;
  1391. else if (IS_TMDS(intel_sdvo_connector))
  1392. ret = intel_sdvo_tmds_sink_detect(connector);
  1393. else {
  1394. struct edid *edid;
  1395. /* if we have an edid check it matches the connection */
  1396. edid = intel_sdvo_get_edid(connector);
  1397. if (edid == NULL)
  1398. edid = intel_sdvo_get_analog_edid(connector);
  1399. if (edid != NULL) {
  1400. if (intel_sdvo_connector_matches_edid(intel_sdvo_connector,
  1401. edid))
  1402. ret = connector_status_connected;
  1403. else
  1404. ret = connector_status_disconnected;
  1405. kfree(edid);
  1406. } else
  1407. ret = connector_status_connected;
  1408. }
  1409. /* May update encoder flag for like clock for SDVO TV, etc.*/
  1410. if (ret == connector_status_connected) {
  1411. intel_sdvo->is_tv = false;
  1412. intel_sdvo->is_lvds = false;
  1413. if (response & SDVO_TV_MASK)
  1414. intel_sdvo->is_tv = true;
  1415. if (response & SDVO_LVDS_MASK)
  1416. intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
  1417. }
  1418. return ret;
  1419. }
  1420. static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
  1421. {
  1422. struct edid *edid;
  1423. /* set the bus switch and get the modes */
  1424. edid = intel_sdvo_get_edid(connector);
  1425. /*
  1426. * Mac mini hack. On this device, the DVI-I connector shares one DDC
  1427. * link between analog and digital outputs. So, if the regular SDVO
  1428. * DDC fails, check to see if the analog output is disconnected, in
  1429. * which case we'll look there for the digital DDC data.
  1430. */
  1431. if (edid == NULL)
  1432. edid = intel_sdvo_get_analog_edid(connector);
  1433. if (edid != NULL) {
  1434. if (intel_sdvo_connector_matches_edid(to_intel_sdvo_connector(connector),
  1435. edid)) {
  1436. drm_mode_connector_update_edid_property(connector, edid);
  1437. drm_add_edid_modes(connector, edid);
  1438. }
  1439. kfree(edid);
  1440. }
  1441. }
  1442. /*
  1443. * Set of SDVO TV modes.
  1444. * Note! This is in reply order (see loop in get_tv_modes).
  1445. * XXX: all 60Hz refresh?
  1446. */
  1447. static const struct drm_display_mode sdvo_tv_modes[] = {
  1448. { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
  1449. 416, 0, 200, 201, 232, 233, 0,
  1450. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1451. { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
  1452. 416, 0, 240, 241, 272, 273, 0,
  1453. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1454. { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
  1455. 496, 0, 300, 301, 332, 333, 0,
  1456. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1457. { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
  1458. 736, 0, 350, 351, 382, 383, 0,
  1459. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1460. { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
  1461. 736, 0, 400, 401, 432, 433, 0,
  1462. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1463. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
  1464. 736, 0, 480, 481, 512, 513, 0,
  1465. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1466. { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
  1467. 800, 0, 480, 481, 512, 513, 0,
  1468. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1469. { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
  1470. 800, 0, 576, 577, 608, 609, 0,
  1471. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1472. { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
  1473. 816, 0, 350, 351, 382, 383, 0,
  1474. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1475. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
  1476. 816, 0, 400, 401, 432, 433, 0,
  1477. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1478. { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
  1479. 816, 0, 480, 481, 512, 513, 0,
  1480. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1481. { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
  1482. 816, 0, 540, 541, 572, 573, 0,
  1483. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1484. { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
  1485. 816, 0, 576, 577, 608, 609, 0,
  1486. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1487. { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
  1488. 864, 0, 576, 577, 608, 609, 0,
  1489. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1490. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
  1491. 896, 0, 600, 601, 632, 633, 0,
  1492. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1493. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
  1494. 928, 0, 624, 625, 656, 657, 0,
  1495. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1496. { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
  1497. 1016, 0, 766, 767, 798, 799, 0,
  1498. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1499. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
  1500. 1120, 0, 768, 769, 800, 801, 0,
  1501. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1502. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
  1503. 1376, 0, 1024, 1025, 1056, 1057, 0,
  1504. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1505. };
  1506. static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
  1507. {
  1508. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1509. struct intel_sdvo_sdtv_resolution_request tv_res;
  1510. uint32_t reply = 0, format_map = 0;
  1511. int i;
  1512. /* Read the list of supported input resolutions for the selected TV
  1513. * format.
  1514. */
  1515. format_map = 1 << intel_sdvo->tv_format_index;
  1516. memcpy(&tv_res, &format_map,
  1517. min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
  1518. if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
  1519. return;
  1520. BUILD_BUG_ON(sizeof(tv_res) != 3);
  1521. if (!intel_sdvo_write_cmd(intel_sdvo,
  1522. SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
  1523. &tv_res, sizeof(tv_res)))
  1524. return;
  1525. if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
  1526. return;
  1527. for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
  1528. if (reply & (1 << i)) {
  1529. struct drm_display_mode *nmode;
  1530. nmode = drm_mode_duplicate(connector->dev,
  1531. &sdvo_tv_modes[i]);
  1532. if (nmode)
  1533. drm_mode_probed_add(connector, nmode);
  1534. }
  1535. }
  1536. static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
  1537. {
  1538. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1539. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1540. struct drm_display_mode *newmode;
  1541. /*
  1542. * Attempt to get the mode list from DDC.
  1543. * Assume that the preferred modes are
  1544. * arranged in priority order.
  1545. */
  1546. intel_ddc_get_modes(connector, intel_sdvo->i2c);
  1547. if (list_empty(&connector->probed_modes) == false)
  1548. goto end;
  1549. /* Fetch modes from VBT */
  1550. if (dev_priv->vbt.sdvo_lvds_vbt_mode != NULL) {
  1551. newmode = drm_mode_duplicate(connector->dev,
  1552. dev_priv->vbt.sdvo_lvds_vbt_mode);
  1553. if (newmode != NULL) {
  1554. /* Guarantee the mode is preferred */
  1555. newmode->type = (DRM_MODE_TYPE_PREFERRED |
  1556. DRM_MODE_TYPE_DRIVER);
  1557. drm_mode_probed_add(connector, newmode);
  1558. }
  1559. }
  1560. end:
  1561. list_for_each_entry(newmode, &connector->probed_modes, head) {
  1562. if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
  1563. intel_sdvo->sdvo_lvds_fixed_mode =
  1564. drm_mode_duplicate(connector->dev, newmode);
  1565. intel_sdvo->is_lvds = true;
  1566. break;
  1567. }
  1568. }
  1569. }
  1570. static int intel_sdvo_get_modes(struct drm_connector *connector)
  1571. {
  1572. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1573. if (IS_TV(intel_sdvo_connector))
  1574. intel_sdvo_get_tv_modes(connector);
  1575. else if (IS_LVDS(intel_sdvo_connector))
  1576. intel_sdvo_get_lvds_modes(connector);
  1577. else
  1578. intel_sdvo_get_ddc_modes(connector);
  1579. return !list_empty(&connector->probed_modes);
  1580. }
  1581. static void
  1582. intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
  1583. {
  1584. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1585. struct drm_device *dev = connector->dev;
  1586. if (intel_sdvo_connector->left)
  1587. drm_property_destroy(dev, intel_sdvo_connector->left);
  1588. if (intel_sdvo_connector->right)
  1589. drm_property_destroy(dev, intel_sdvo_connector->right);
  1590. if (intel_sdvo_connector->top)
  1591. drm_property_destroy(dev, intel_sdvo_connector->top);
  1592. if (intel_sdvo_connector->bottom)
  1593. drm_property_destroy(dev, intel_sdvo_connector->bottom);
  1594. if (intel_sdvo_connector->hpos)
  1595. drm_property_destroy(dev, intel_sdvo_connector->hpos);
  1596. if (intel_sdvo_connector->vpos)
  1597. drm_property_destroy(dev, intel_sdvo_connector->vpos);
  1598. if (intel_sdvo_connector->saturation)
  1599. drm_property_destroy(dev, intel_sdvo_connector->saturation);
  1600. if (intel_sdvo_connector->contrast)
  1601. drm_property_destroy(dev, intel_sdvo_connector->contrast);
  1602. if (intel_sdvo_connector->hue)
  1603. drm_property_destroy(dev, intel_sdvo_connector->hue);
  1604. if (intel_sdvo_connector->sharpness)
  1605. drm_property_destroy(dev, intel_sdvo_connector->sharpness);
  1606. if (intel_sdvo_connector->flicker_filter)
  1607. drm_property_destroy(dev, intel_sdvo_connector->flicker_filter);
  1608. if (intel_sdvo_connector->flicker_filter_2d)
  1609. drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_2d);
  1610. if (intel_sdvo_connector->flicker_filter_adaptive)
  1611. drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_adaptive);
  1612. if (intel_sdvo_connector->tv_luma_filter)
  1613. drm_property_destroy(dev, intel_sdvo_connector->tv_luma_filter);
  1614. if (intel_sdvo_connector->tv_chroma_filter)
  1615. drm_property_destroy(dev, intel_sdvo_connector->tv_chroma_filter);
  1616. if (intel_sdvo_connector->dot_crawl)
  1617. drm_property_destroy(dev, intel_sdvo_connector->dot_crawl);
  1618. if (intel_sdvo_connector->brightness)
  1619. drm_property_destroy(dev, intel_sdvo_connector->brightness);
  1620. }
  1621. static void intel_sdvo_destroy(struct drm_connector *connector)
  1622. {
  1623. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1624. if (intel_sdvo_connector->tv_format)
  1625. drm_property_destroy(connector->dev,
  1626. intel_sdvo_connector->tv_format);
  1627. intel_sdvo_destroy_enhance_property(connector);
  1628. drm_sysfs_connector_remove(connector);
  1629. drm_connector_cleanup(connector);
  1630. kfree(intel_sdvo_connector);
  1631. }
  1632. static bool intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
  1633. {
  1634. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1635. struct edid *edid;
  1636. bool has_audio = false;
  1637. if (!intel_sdvo->is_hdmi)
  1638. return false;
  1639. edid = intel_sdvo_get_edid(connector);
  1640. if (edid != NULL && edid->input & DRM_EDID_INPUT_DIGITAL)
  1641. has_audio = drm_detect_monitor_audio(edid);
  1642. kfree(edid);
  1643. return has_audio;
  1644. }
  1645. static int
  1646. intel_sdvo_set_property(struct drm_connector *connector,
  1647. struct drm_property *property,
  1648. uint64_t val)
  1649. {
  1650. struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1651. struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1652. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1653. uint16_t temp_value;
  1654. uint8_t cmd;
  1655. int ret;
  1656. ret = drm_object_property_set_value(&connector->base, property, val);
  1657. if (ret)
  1658. return ret;
  1659. if (property == dev_priv->force_audio_property) {
  1660. int i = val;
  1661. bool has_audio;
  1662. if (i == intel_sdvo_connector->force_audio)
  1663. return 0;
  1664. intel_sdvo_connector->force_audio = i;
  1665. if (i == HDMI_AUDIO_AUTO)
  1666. has_audio = intel_sdvo_detect_hdmi_audio(connector);
  1667. else
  1668. has_audio = (i == HDMI_AUDIO_ON);
  1669. if (has_audio == intel_sdvo->has_hdmi_audio)
  1670. return 0;
  1671. intel_sdvo->has_hdmi_audio = has_audio;
  1672. goto done;
  1673. }
  1674. if (property == dev_priv->broadcast_rgb_property) {
  1675. bool old_auto = intel_sdvo->color_range_auto;
  1676. uint32_t old_range = intel_sdvo->color_range;
  1677. switch (val) {
  1678. case INTEL_BROADCAST_RGB_AUTO:
  1679. intel_sdvo->color_range_auto = true;
  1680. break;
  1681. case INTEL_BROADCAST_RGB_FULL:
  1682. intel_sdvo->color_range_auto = false;
  1683. intel_sdvo->color_range = 0;
  1684. break;
  1685. case INTEL_BROADCAST_RGB_LIMITED:
  1686. intel_sdvo->color_range_auto = false;
  1687. /* FIXME: this bit is only valid when using TMDS
  1688. * encoding and 8 bit per color mode. */
  1689. intel_sdvo->color_range = HDMI_COLOR_RANGE_16_235;
  1690. break;
  1691. default:
  1692. return -EINVAL;
  1693. }
  1694. if (old_auto == intel_sdvo->color_range_auto &&
  1695. old_range == intel_sdvo->color_range)
  1696. return 0;
  1697. goto done;
  1698. }
  1699. #define CHECK_PROPERTY(name, NAME) \
  1700. if (intel_sdvo_connector->name == property) { \
  1701. if (intel_sdvo_connector->cur_##name == temp_value) return 0; \
  1702. if (intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
  1703. cmd = SDVO_CMD_SET_##NAME; \
  1704. intel_sdvo_connector->cur_##name = temp_value; \
  1705. goto set_value; \
  1706. }
  1707. if (property == intel_sdvo_connector->tv_format) {
  1708. if (val >= TV_FORMAT_NUM)
  1709. return -EINVAL;
  1710. if (intel_sdvo->tv_format_index ==
  1711. intel_sdvo_connector->tv_format_supported[val])
  1712. return 0;
  1713. intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[val];
  1714. goto done;
  1715. } else if (IS_TV_OR_LVDS(intel_sdvo_connector)) {
  1716. temp_value = val;
  1717. if (intel_sdvo_connector->left == property) {
  1718. drm_object_property_set_value(&connector->base,
  1719. intel_sdvo_connector->right, val);
  1720. if (intel_sdvo_connector->left_margin == temp_value)
  1721. return 0;
  1722. intel_sdvo_connector->left_margin = temp_value;
  1723. intel_sdvo_connector->right_margin = temp_value;
  1724. temp_value = intel_sdvo_connector->max_hscan -
  1725. intel_sdvo_connector->left_margin;
  1726. cmd = SDVO_CMD_SET_OVERSCAN_H;
  1727. goto set_value;
  1728. } else if (intel_sdvo_connector->right == property) {
  1729. drm_object_property_set_value(&connector->base,
  1730. intel_sdvo_connector->left, val);
  1731. if (intel_sdvo_connector->right_margin == temp_value)
  1732. return 0;
  1733. intel_sdvo_connector->left_margin = temp_value;
  1734. intel_sdvo_connector->right_margin = temp_value;
  1735. temp_value = intel_sdvo_connector->max_hscan -
  1736. intel_sdvo_connector->left_margin;
  1737. cmd = SDVO_CMD_SET_OVERSCAN_H;
  1738. goto set_value;
  1739. } else if (intel_sdvo_connector->top == property) {
  1740. drm_object_property_set_value(&connector->base,
  1741. intel_sdvo_connector->bottom, val);
  1742. if (intel_sdvo_connector->top_margin == temp_value)
  1743. return 0;
  1744. intel_sdvo_connector->top_margin = temp_value;
  1745. intel_sdvo_connector->bottom_margin = temp_value;
  1746. temp_value = intel_sdvo_connector->max_vscan -
  1747. intel_sdvo_connector->top_margin;
  1748. cmd = SDVO_CMD_SET_OVERSCAN_V;
  1749. goto set_value;
  1750. } else if (intel_sdvo_connector->bottom == property) {
  1751. drm_object_property_set_value(&connector->base,
  1752. intel_sdvo_connector->top, val);
  1753. if (intel_sdvo_connector->bottom_margin == temp_value)
  1754. return 0;
  1755. intel_sdvo_connector->top_margin = temp_value;
  1756. intel_sdvo_connector->bottom_margin = temp_value;
  1757. temp_value = intel_sdvo_connector->max_vscan -
  1758. intel_sdvo_connector->top_margin;
  1759. cmd = SDVO_CMD_SET_OVERSCAN_V;
  1760. goto set_value;
  1761. }
  1762. CHECK_PROPERTY(hpos, HPOS)
  1763. CHECK_PROPERTY(vpos, VPOS)
  1764. CHECK_PROPERTY(saturation, SATURATION)
  1765. CHECK_PROPERTY(contrast, CONTRAST)
  1766. CHECK_PROPERTY(hue, HUE)
  1767. CHECK_PROPERTY(brightness, BRIGHTNESS)
  1768. CHECK_PROPERTY(sharpness, SHARPNESS)
  1769. CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
  1770. CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
  1771. CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
  1772. CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
  1773. CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
  1774. CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
  1775. }
  1776. return -EINVAL; /* unknown property */
  1777. set_value:
  1778. if (!intel_sdvo_set_value(intel_sdvo, cmd, &temp_value, 2))
  1779. return -EIO;
  1780. done:
  1781. if (intel_sdvo->base.base.crtc)
  1782. intel_crtc_restore_mode(intel_sdvo->base.base.crtc);
  1783. return 0;
  1784. #undef CHECK_PROPERTY
  1785. }
  1786. static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
  1787. .dpms = intel_sdvo_dpms,
  1788. .detect = intel_sdvo_detect,
  1789. .fill_modes = drm_helper_probe_single_connector_modes,
  1790. .set_property = intel_sdvo_set_property,
  1791. .destroy = intel_sdvo_destroy,
  1792. };
  1793. static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
  1794. .get_modes = intel_sdvo_get_modes,
  1795. .mode_valid = intel_sdvo_mode_valid,
  1796. .best_encoder = intel_best_encoder,
  1797. };
  1798. static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
  1799. {
  1800. struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  1801. if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
  1802. drm_mode_destroy(encoder->dev,
  1803. intel_sdvo->sdvo_lvds_fixed_mode);
  1804. i2c_del_adapter(&intel_sdvo->ddc);
  1805. intel_encoder_destroy(encoder);
  1806. }
  1807. static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
  1808. .destroy = intel_sdvo_enc_destroy,
  1809. };
  1810. static void
  1811. intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
  1812. {
  1813. uint16_t mask = 0;
  1814. unsigned int num_bits;
  1815. /* Make a mask of outputs less than or equal to our own priority in the
  1816. * list.
  1817. */
  1818. switch (sdvo->controlled_output) {
  1819. case SDVO_OUTPUT_LVDS1:
  1820. mask |= SDVO_OUTPUT_LVDS1;
  1821. case SDVO_OUTPUT_LVDS0:
  1822. mask |= SDVO_OUTPUT_LVDS0;
  1823. case SDVO_OUTPUT_TMDS1:
  1824. mask |= SDVO_OUTPUT_TMDS1;
  1825. case SDVO_OUTPUT_TMDS0:
  1826. mask |= SDVO_OUTPUT_TMDS0;
  1827. case SDVO_OUTPUT_RGB1:
  1828. mask |= SDVO_OUTPUT_RGB1;
  1829. case SDVO_OUTPUT_RGB0:
  1830. mask |= SDVO_OUTPUT_RGB0;
  1831. break;
  1832. }
  1833. /* Count bits to find what number we are in the priority list. */
  1834. mask &= sdvo->caps.output_flags;
  1835. num_bits = hweight16(mask);
  1836. /* If more than 3 outputs, default to DDC bus 3 for now. */
  1837. if (num_bits > 3)
  1838. num_bits = 3;
  1839. /* Corresponds to SDVO_CONTROL_BUS_DDCx */
  1840. sdvo->ddc_bus = 1 << num_bits;
  1841. }
  1842. /**
  1843. * Choose the appropriate DDC bus for control bus switch command for this
  1844. * SDVO output based on the controlled output.
  1845. *
  1846. * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
  1847. * outputs, then LVDS outputs.
  1848. */
  1849. static void
  1850. intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
  1851. struct intel_sdvo *sdvo, u32 reg)
  1852. {
  1853. struct sdvo_device_mapping *mapping;
  1854. if (sdvo->is_sdvob)
  1855. mapping = &(dev_priv->sdvo_mappings[0]);
  1856. else
  1857. mapping = &(dev_priv->sdvo_mappings[1]);
  1858. if (mapping->initialized)
  1859. sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
  1860. else
  1861. intel_sdvo_guess_ddc_bus(sdvo);
  1862. }
  1863. static void
  1864. intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
  1865. struct intel_sdvo *sdvo, u32 reg)
  1866. {
  1867. struct sdvo_device_mapping *mapping;
  1868. u8 pin;
  1869. if (sdvo->is_sdvob)
  1870. mapping = &dev_priv->sdvo_mappings[0];
  1871. else
  1872. mapping = &dev_priv->sdvo_mappings[1];
  1873. if (mapping->initialized && intel_gmbus_is_port_valid(mapping->i2c_pin))
  1874. pin = mapping->i2c_pin;
  1875. else
  1876. pin = GMBUS_PORT_DPB;
  1877. sdvo->i2c = intel_gmbus_get_adapter(dev_priv, pin);
  1878. /* With gmbus we should be able to drive sdvo i2c at 2MHz, but somehow
  1879. * our code totally fails once we start using gmbus. Hence fall back to
  1880. * bit banging for now. */
  1881. intel_gmbus_force_bit(sdvo->i2c, true);
  1882. }
  1883. /* undo any changes intel_sdvo_select_i2c_bus() did to sdvo->i2c */
  1884. static void
  1885. intel_sdvo_unselect_i2c_bus(struct intel_sdvo *sdvo)
  1886. {
  1887. intel_gmbus_force_bit(sdvo->i2c, false);
  1888. }
  1889. static bool
  1890. intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
  1891. {
  1892. return intel_sdvo_check_supp_encode(intel_sdvo);
  1893. }
  1894. static u8
  1895. intel_sdvo_get_slave_addr(struct drm_device *dev, struct intel_sdvo *sdvo)
  1896. {
  1897. struct drm_i915_private *dev_priv = dev->dev_private;
  1898. struct sdvo_device_mapping *my_mapping, *other_mapping;
  1899. if (sdvo->is_sdvob) {
  1900. my_mapping = &dev_priv->sdvo_mappings[0];
  1901. other_mapping = &dev_priv->sdvo_mappings[1];
  1902. } else {
  1903. my_mapping = &dev_priv->sdvo_mappings[1];
  1904. other_mapping = &dev_priv->sdvo_mappings[0];
  1905. }
  1906. /* If the BIOS described our SDVO device, take advantage of it. */
  1907. if (my_mapping->slave_addr)
  1908. return my_mapping->slave_addr;
  1909. /* If the BIOS only described a different SDVO device, use the
  1910. * address that it isn't using.
  1911. */
  1912. if (other_mapping->slave_addr) {
  1913. if (other_mapping->slave_addr == 0x70)
  1914. return 0x72;
  1915. else
  1916. return 0x70;
  1917. }
  1918. /* No SDVO device info is found for another DVO port,
  1919. * so use mapping assumption we had before BIOS parsing.
  1920. */
  1921. if (sdvo->is_sdvob)
  1922. return 0x70;
  1923. else
  1924. return 0x72;
  1925. }
  1926. static void
  1927. intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
  1928. struct intel_sdvo *encoder)
  1929. {
  1930. drm_connector_init(encoder->base.base.dev,
  1931. &connector->base.base,
  1932. &intel_sdvo_connector_funcs,
  1933. connector->base.base.connector_type);
  1934. drm_connector_helper_add(&connector->base.base,
  1935. &intel_sdvo_connector_helper_funcs);
  1936. connector->base.base.interlace_allowed = 1;
  1937. connector->base.base.doublescan_allowed = 0;
  1938. connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
  1939. connector->base.get_hw_state = intel_sdvo_connector_get_hw_state;
  1940. intel_connector_attach_encoder(&connector->base, &encoder->base);
  1941. drm_sysfs_connector_add(&connector->base.base);
  1942. }
  1943. static void
  1944. intel_sdvo_add_hdmi_properties(struct intel_sdvo *intel_sdvo,
  1945. struct intel_sdvo_connector *connector)
  1946. {
  1947. struct drm_device *dev = connector->base.base.dev;
  1948. intel_attach_force_audio_property(&connector->base.base);
  1949. if (INTEL_INFO(dev)->gen >= 4 && IS_MOBILE(dev)) {
  1950. intel_attach_broadcast_rgb_property(&connector->base.base);
  1951. intel_sdvo->color_range_auto = true;
  1952. }
  1953. }
  1954. static bool
  1955. intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
  1956. {
  1957. struct drm_encoder *encoder = &intel_sdvo->base.base;
  1958. struct drm_connector *connector;
  1959. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  1960. struct intel_connector *intel_connector;
  1961. struct intel_sdvo_connector *intel_sdvo_connector;
  1962. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  1963. if (!intel_sdvo_connector)
  1964. return false;
  1965. if (device == 0) {
  1966. intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
  1967. intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
  1968. } else if (device == 1) {
  1969. intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
  1970. intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
  1971. }
  1972. intel_connector = &intel_sdvo_connector->base;
  1973. connector = &intel_connector->base;
  1974. if (intel_sdvo_get_hotplug_support(intel_sdvo) &
  1975. intel_sdvo_connector->output_flag) {
  1976. intel_sdvo->hotplug_active |= intel_sdvo_connector->output_flag;
  1977. /* Some SDVO devices have one-shot hotplug interrupts.
  1978. * Ensure that they get re-enabled when an interrupt happens.
  1979. */
  1980. intel_encoder->hot_plug = intel_sdvo_enable_hotplug;
  1981. intel_sdvo_enable_hotplug(intel_encoder);
  1982. } else {
  1983. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
  1984. }
  1985. encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
  1986. connector->connector_type = DRM_MODE_CONNECTOR_DVID;
  1987. if (intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
  1988. connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
  1989. intel_sdvo->is_hdmi = true;
  1990. }
  1991. intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  1992. if (intel_sdvo->is_hdmi)
  1993. intel_sdvo_add_hdmi_properties(intel_sdvo, intel_sdvo_connector);
  1994. return true;
  1995. }
  1996. static bool
  1997. intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
  1998. {
  1999. struct drm_encoder *encoder = &intel_sdvo->base.base;
  2000. struct drm_connector *connector;
  2001. struct intel_connector *intel_connector;
  2002. struct intel_sdvo_connector *intel_sdvo_connector;
  2003. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  2004. if (!intel_sdvo_connector)
  2005. return false;
  2006. intel_connector = &intel_sdvo_connector->base;
  2007. connector = &intel_connector->base;
  2008. encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
  2009. connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
  2010. intel_sdvo->controlled_output |= type;
  2011. intel_sdvo_connector->output_flag = type;
  2012. intel_sdvo->is_tv = true;
  2013. intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  2014. if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
  2015. goto err;
  2016. if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  2017. goto err;
  2018. return true;
  2019. err:
  2020. intel_sdvo_destroy(connector);
  2021. return false;
  2022. }
  2023. static bool
  2024. intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
  2025. {
  2026. struct drm_encoder *encoder = &intel_sdvo->base.base;
  2027. struct drm_connector *connector;
  2028. struct intel_connector *intel_connector;
  2029. struct intel_sdvo_connector *intel_sdvo_connector;
  2030. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  2031. if (!intel_sdvo_connector)
  2032. return false;
  2033. intel_connector = &intel_sdvo_connector->base;
  2034. connector = &intel_connector->base;
  2035. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  2036. encoder->encoder_type = DRM_MODE_ENCODER_DAC;
  2037. connector->connector_type = DRM_MODE_CONNECTOR_VGA;
  2038. if (device == 0) {
  2039. intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
  2040. intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
  2041. } else if (device == 1) {
  2042. intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
  2043. intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
  2044. }
  2045. intel_sdvo_connector_init(intel_sdvo_connector,
  2046. intel_sdvo);
  2047. return true;
  2048. }
  2049. static bool
  2050. intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
  2051. {
  2052. struct drm_encoder *encoder = &intel_sdvo->base.base;
  2053. struct drm_connector *connector;
  2054. struct intel_connector *intel_connector;
  2055. struct intel_sdvo_connector *intel_sdvo_connector;
  2056. intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  2057. if (!intel_sdvo_connector)
  2058. return false;
  2059. intel_connector = &intel_sdvo_connector->base;
  2060. connector = &intel_connector->base;
  2061. encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
  2062. connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
  2063. if (device == 0) {
  2064. intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
  2065. intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
  2066. } else if (device == 1) {
  2067. intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
  2068. intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
  2069. }
  2070. intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  2071. if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  2072. goto err;
  2073. return true;
  2074. err:
  2075. intel_sdvo_destroy(connector);
  2076. return false;
  2077. }
  2078. static bool
  2079. intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
  2080. {
  2081. intel_sdvo->is_tv = false;
  2082. intel_sdvo->is_lvds = false;
  2083. /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
  2084. if (flags & SDVO_OUTPUT_TMDS0)
  2085. if (!intel_sdvo_dvi_init(intel_sdvo, 0))
  2086. return false;
  2087. if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
  2088. if (!intel_sdvo_dvi_init(intel_sdvo, 1))
  2089. return false;
  2090. /* TV has no XXX1 function block */
  2091. if (flags & SDVO_OUTPUT_SVID0)
  2092. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
  2093. return false;
  2094. if (flags & SDVO_OUTPUT_CVBS0)
  2095. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
  2096. return false;
  2097. if (flags & SDVO_OUTPUT_YPRPB0)
  2098. if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_YPRPB0))
  2099. return false;
  2100. if (flags & SDVO_OUTPUT_RGB0)
  2101. if (!intel_sdvo_analog_init(intel_sdvo, 0))
  2102. return false;
  2103. if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
  2104. if (!intel_sdvo_analog_init(intel_sdvo, 1))
  2105. return false;
  2106. if (flags & SDVO_OUTPUT_LVDS0)
  2107. if (!intel_sdvo_lvds_init(intel_sdvo, 0))
  2108. return false;
  2109. if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
  2110. if (!intel_sdvo_lvds_init(intel_sdvo, 1))
  2111. return false;
  2112. if ((flags & SDVO_OUTPUT_MASK) == 0) {
  2113. unsigned char bytes[2];
  2114. intel_sdvo->controlled_output = 0;
  2115. memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
  2116. DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
  2117. SDVO_NAME(intel_sdvo),
  2118. bytes[0], bytes[1]);
  2119. return false;
  2120. }
  2121. intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  2122. return true;
  2123. }
  2124. static void intel_sdvo_output_cleanup(struct intel_sdvo *intel_sdvo)
  2125. {
  2126. struct drm_device *dev = intel_sdvo->base.base.dev;
  2127. struct drm_connector *connector, *tmp;
  2128. list_for_each_entry_safe(connector, tmp,
  2129. &dev->mode_config.connector_list, head) {
  2130. if (intel_attached_encoder(connector) == &intel_sdvo->base)
  2131. intel_sdvo_destroy(connector);
  2132. }
  2133. }
  2134. static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
  2135. struct intel_sdvo_connector *intel_sdvo_connector,
  2136. int type)
  2137. {
  2138. struct drm_device *dev = intel_sdvo->base.base.dev;
  2139. struct intel_sdvo_tv_format format;
  2140. uint32_t format_map, i;
  2141. if (!intel_sdvo_set_target_output(intel_sdvo, type))
  2142. return false;
  2143. BUILD_BUG_ON(sizeof(format) != 6);
  2144. if (!intel_sdvo_get_value(intel_sdvo,
  2145. SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
  2146. &format, sizeof(format)))
  2147. return false;
  2148. memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
  2149. if (format_map == 0)
  2150. return false;
  2151. intel_sdvo_connector->format_supported_num = 0;
  2152. for (i = 0 ; i < TV_FORMAT_NUM; i++)
  2153. if (format_map & (1 << i))
  2154. intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
  2155. intel_sdvo_connector->tv_format =
  2156. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  2157. "mode", intel_sdvo_connector->format_supported_num);
  2158. if (!intel_sdvo_connector->tv_format)
  2159. return false;
  2160. for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
  2161. drm_property_add_enum(
  2162. intel_sdvo_connector->tv_format, i,
  2163. i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
  2164. intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[0];
  2165. drm_object_attach_property(&intel_sdvo_connector->base.base.base,
  2166. intel_sdvo_connector->tv_format, 0);
  2167. return true;
  2168. }
  2169. #define ENHANCEMENT(name, NAME) do { \
  2170. if (enhancements.name) { \
  2171. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
  2172. !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
  2173. return false; \
  2174. intel_sdvo_connector->max_##name = data_value[0]; \
  2175. intel_sdvo_connector->cur_##name = response; \
  2176. intel_sdvo_connector->name = \
  2177. drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
  2178. if (!intel_sdvo_connector->name) return false; \
  2179. drm_object_attach_property(&connector->base, \
  2180. intel_sdvo_connector->name, \
  2181. intel_sdvo_connector->cur_##name); \
  2182. DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
  2183. data_value[0], data_value[1], response); \
  2184. } \
  2185. } while (0)
  2186. static bool
  2187. intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
  2188. struct intel_sdvo_connector *intel_sdvo_connector,
  2189. struct intel_sdvo_enhancements_reply enhancements)
  2190. {
  2191. struct drm_device *dev = intel_sdvo->base.base.dev;
  2192. struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2193. uint16_t response, data_value[2];
  2194. /* when horizontal overscan is supported, Add the left/right property */
  2195. if (enhancements.overscan_h) {
  2196. if (!intel_sdvo_get_value(intel_sdvo,
  2197. SDVO_CMD_GET_MAX_OVERSCAN_H,
  2198. &data_value, 4))
  2199. return false;
  2200. if (!intel_sdvo_get_value(intel_sdvo,
  2201. SDVO_CMD_GET_OVERSCAN_H,
  2202. &response, 2))
  2203. return false;
  2204. intel_sdvo_connector->max_hscan = data_value[0];
  2205. intel_sdvo_connector->left_margin = data_value[0] - response;
  2206. intel_sdvo_connector->right_margin = intel_sdvo_connector->left_margin;
  2207. intel_sdvo_connector->left =
  2208. drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
  2209. if (!intel_sdvo_connector->left)
  2210. return false;
  2211. drm_object_attach_property(&connector->base,
  2212. intel_sdvo_connector->left,
  2213. intel_sdvo_connector->left_margin);
  2214. intel_sdvo_connector->right =
  2215. drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
  2216. if (!intel_sdvo_connector->right)
  2217. return false;
  2218. drm_object_attach_property(&connector->base,
  2219. intel_sdvo_connector->right,
  2220. intel_sdvo_connector->right_margin);
  2221. DRM_DEBUG_KMS("h_overscan: max %d, "
  2222. "default %d, current %d\n",
  2223. data_value[0], data_value[1], response);
  2224. }
  2225. if (enhancements.overscan_v) {
  2226. if (!intel_sdvo_get_value(intel_sdvo,
  2227. SDVO_CMD_GET_MAX_OVERSCAN_V,
  2228. &data_value, 4))
  2229. return false;
  2230. if (!intel_sdvo_get_value(intel_sdvo,
  2231. SDVO_CMD_GET_OVERSCAN_V,
  2232. &response, 2))
  2233. return false;
  2234. intel_sdvo_connector->max_vscan = data_value[0];
  2235. intel_sdvo_connector->top_margin = data_value[0] - response;
  2236. intel_sdvo_connector->bottom_margin = intel_sdvo_connector->top_margin;
  2237. intel_sdvo_connector->top =
  2238. drm_property_create_range(dev, 0,
  2239. "top_margin", 0, data_value[0]);
  2240. if (!intel_sdvo_connector->top)
  2241. return false;
  2242. drm_object_attach_property(&connector->base,
  2243. intel_sdvo_connector->top,
  2244. intel_sdvo_connector->top_margin);
  2245. intel_sdvo_connector->bottom =
  2246. drm_property_create_range(dev, 0,
  2247. "bottom_margin", 0, data_value[0]);
  2248. if (!intel_sdvo_connector->bottom)
  2249. return false;
  2250. drm_object_attach_property(&connector->base,
  2251. intel_sdvo_connector->bottom,
  2252. intel_sdvo_connector->bottom_margin);
  2253. DRM_DEBUG_KMS("v_overscan: max %d, "
  2254. "default %d, current %d\n",
  2255. data_value[0], data_value[1], response);
  2256. }
  2257. ENHANCEMENT(hpos, HPOS);
  2258. ENHANCEMENT(vpos, VPOS);
  2259. ENHANCEMENT(saturation, SATURATION);
  2260. ENHANCEMENT(contrast, CONTRAST);
  2261. ENHANCEMENT(hue, HUE);
  2262. ENHANCEMENT(sharpness, SHARPNESS);
  2263. ENHANCEMENT(brightness, BRIGHTNESS);
  2264. ENHANCEMENT(flicker_filter, FLICKER_FILTER);
  2265. ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
  2266. ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
  2267. ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
  2268. ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
  2269. if (enhancements.dot_crawl) {
  2270. if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
  2271. return false;
  2272. intel_sdvo_connector->max_dot_crawl = 1;
  2273. intel_sdvo_connector->cur_dot_crawl = response & 0x1;
  2274. intel_sdvo_connector->dot_crawl =
  2275. drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
  2276. if (!intel_sdvo_connector->dot_crawl)
  2277. return false;
  2278. drm_object_attach_property(&connector->base,
  2279. intel_sdvo_connector->dot_crawl,
  2280. intel_sdvo_connector->cur_dot_crawl);
  2281. DRM_DEBUG_KMS("dot crawl: current %d\n", response);
  2282. }
  2283. return true;
  2284. }
  2285. static bool
  2286. intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
  2287. struct intel_sdvo_connector *intel_sdvo_connector,
  2288. struct intel_sdvo_enhancements_reply enhancements)
  2289. {
  2290. struct drm_device *dev = intel_sdvo->base.base.dev;
  2291. struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2292. uint16_t response, data_value[2];
  2293. ENHANCEMENT(brightness, BRIGHTNESS);
  2294. return true;
  2295. }
  2296. #undef ENHANCEMENT
  2297. static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
  2298. struct intel_sdvo_connector *intel_sdvo_connector)
  2299. {
  2300. union {
  2301. struct intel_sdvo_enhancements_reply reply;
  2302. uint16_t response;
  2303. } enhancements;
  2304. BUILD_BUG_ON(sizeof(enhancements) != 2);
  2305. enhancements.response = 0;
  2306. intel_sdvo_get_value(intel_sdvo,
  2307. SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
  2308. &enhancements, sizeof(enhancements));
  2309. if (enhancements.response == 0) {
  2310. DRM_DEBUG_KMS("No enhancement is supported\n");
  2311. return true;
  2312. }
  2313. if (IS_TV(intel_sdvo_connector))
  2314. return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2315. else if (IS_LVDS(intel_sdvo_connector))
  2316. return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2317. else
  2318. return true;
  2319. }
  2320. static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
  2321. struct i2c_msg *msgs,
  2322. int num)
  2323. {
  2324. struct intel_sdvo *sdvo = adapter->algo_data;
  2325. if (!intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
  2326. return -EIO;
  2327. return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
  2328. }
  2329. static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
  2330. {
  2331. struct intel_sdvo *sdvo = adapter->algo_data;
  2332. return sdvo->i2c->algo->functionality(sdvo->i2c);
  2333. }
  2334. static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
  2335. .master_xfer = intel_sdvo_ddc_proxy_xfer,
  2336. .functionality = intel_sdvo_ddc_proxy_func
  2337. };
  2338. static bool
  2339. intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
  2340. struct drm_device *dev)
  2341. {
  2342. sdvo->ddc.owner = THIS_MODULE;
  2343. sdvo->ddc.class = I2C_CLASS_DDC;
  2344. snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
  2345. sdvo->ddc.dev.parent = &dev->pdev->dev;
  2346. sdvo->ddc.algo_data = sdvo;
  2347. sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
  2348. return i2c_add_adapter(&sdvo->ddc) == 0;
  2349. }
  2350. bool intel_sdvo_init(struct drm_device *dev, uint32_t sdvo_reg, bool is_sdvob)
  2351. {
  2352. struct drm_i915_private *dev_priv = dev->dev_private;
  2353. struct intel_encoder *intel_encoder;
  2354. struct intel_sdvo *intel_sdvo;
  2355. u32 hotplug_mask;
  2356. int i;
  2357. intel_sdvo = kzalloc(sizeof(struct intel_sdvo), GFP_KERNEL);
  2358. if (!intel_sdvo)
  2359. return false;
  2360. intel_sdvo->sdvo_reg = sdvo_reg;
  2361. intel_sdvo->is_sdvob = is_sdvob;
  2362. intel_sdvo->slave_addr = intel_sdvo_get_slave_addr(dev, intel_sdvo) >> 1;
  2363. intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo, sdvo_reg);
  2364. if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev))
  2365. goto err_i2c_bus;
  2366. /* encoder type will be decided later */
  2367. intel_encoder = &intel_sdvo->base;
  2368. intel_encoder->type = INTEL_OUTPUT_SDVO;
  2369. drm_encoder_init(dev, &intel_encoder->base, &intel_sdvo_enc_funcs, 0);
  2370. /* Read the regs to test if we can talk to the device */
  2371. for (i = 0; i < 0x40; i++) {
  2372. u8 byte;
  2373. if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
  2374. DRM_DEBUG_KMS("No SDVO device found on %s\n",
  2375. SDVO_NAME(intel_sdvo));
  2376. goto err;
  2377. }
  2378. }
  2379. hotplug_mask = 0;
  2380. if (IS_G4X(dev)) {
  2381. hotplug_mask = intel_sdvo->is_sdvob ?
  2382. SDVOB_HOTPLUG_INT_STATUS_G4X : SDVOC_HOTPLUG_INT_STATUS_G4X;
  2383. } else if (IS_GEN4(dev)) {
  2384. hotplug_mask = intel_sdvo->is_sdvob ?
  2385. SDVOB_HOTPLUG_INT_STATUS_I965 : SDVOC_HOTPLUG_INT_STATUS_I965;
  2386. } else {
  2387. hotplug_mask = intel_sdvo->is_sdvob ?
  2388. SDVOB_HOTPLUG_INT_STATUS_I915 : SDVOC_HOTPLUG_INT_STATUS_I915;
  2389. }
  2390. /* Only enable the hotplug irq if we need it, to work around noisy
  2391. * hotplug lines.
  2392. */
  2393. if (intel_sdvo->hotplug_active)
  2394. intel_encoder->hpd_pin = HPD_SDVO_B ? HPD_SDVO_B : HPD_SDVO_C;
  2395. intel_encoder->compute_config = intel_sdvo_compute_config;
  2396. intel_encoder->disable = intel_disable_sdvo;
  2397. intel_encoder->mode_set = intel_sdvo_mode_set;
  2398. intel_encoder->enable = intel_enable_sdvo;
  2399. intel_encoder->get_hw_state = intel_sdvo_get_hw_state;
  2400. /* In default case sdvo lvds is false */
  2401. if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
  2402. goto err;
  2403. if (intel_sdvo_output_setup(intel_sdvo,
  2404. intel_sdvo->caps.output_flags) != true) {
  2405. DRM_DEBUG_KMS("SDVO output failed to setup on %s\n",
  2406. SDVO_NAME(intel_sdvo));
  2407. /* Output_setup can leave behind connectors! */
  2408. goto err_output;
  2409. }
  2410. /*
  2411. * Cloning SDVO with anything is often impossible, since the SDVO
  2412. * encoder can request a special input timing mode. And even if that's
  2413. * not the case we have evidence that cloning a plain unscaled mode with
  2414. * VGA doesn't really work. Furthermore the cloning flags are way too
  2415. * simplistic anyway to express such constraints, so just give up on
  2416. * cloning for SDVO encoders.
  2417. */
  2418. intel_sdvo->base.cloneable = false;
  2419. intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo, sdvo_reg);
  2420. /* Set the input timing to the screen. Assume always input 0. */
  2421. if (!intel_sdvo_set_target_input(intel_sdvo))
  2422. goto err_output;
  2423. if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
  2424. &intel_sdvo->pixel_clock_min,
  2425. &intel_sdvo->pixel_clock_max))
  2426. goto err_output;
  2427. DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
  2428. "clock range %dMHz - %dMHz, "
  2429. "input 1: %c, input 2: %c, "
  2430. "output 1: %c, output 2: %c\n",
  2431. SDVO_NAME(intel_sdvo),
  2432. intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
  2433. intel_sdvo->caps.device_rev_id,
  2434. intel_sdvo->pixel_clock_min / 1000,
  2435. intel_sdvo->pixel_clock_max / 1000,
  2436. (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
  2437. (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
  2438. /* check currently supported outputs */
  2439. intel_sdvo->caps.output_flags &
  2440. (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
  2441. intel_sdvo->caps.output_flags &
  2442. (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
  2443. return true;
  2444. err_output:
  2445. intel_sdvo_output_cleanup(intel_sdvo);
  2446. err:
  2447. drm_encoder_cleanup(&intel_encoder->base);
  2448. i2c_del_adapter(&intel_sdvo->ddc);
  2449. err_i2c_bus:
  2450. intel_sdvo_unselect_i2c_bus(intel_sdvo);
  2451. kfree(intel_sdvo);
  2452. return false;
  2453. }