intel_sdvo.c 82 KB

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