intel_sdvo.c 80 KB

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