intel_sdvo.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  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/delay.h>
  30. #include "drmP.h"
  31. #include "drm.h"
  32. #include "drm_crtc.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. #include "intel_sdvo_regs.h"
  37. #undef SDVO_DEBUG
  38. #define I915_SDVO "i915_sdvo"
  39. struct intel_sdvo_priv {
  40. struct i2c_adapter *i2c_bus;
  41. u8 slave_addr;
  42. /* Register for the SDVO device: SDVOB or SDVOC */
  43. int output_device;
  44. /* Active outputs controlled by this SDVO output */
  45. uint16_t controlled_output;
  46. /*
  47. * Capabilities of the SDVO device returned by
  48. * i830_sdvo_get_capabilities()
  49. */
  50. struct intel_sdvo_caps caps;
  51. /* Pixel clock limitations reported by the SDVO device, in kHz */
  52. int pixel_clock_min, pixel_clock_max;
  53. /**
  54. * This is set if we're going to treat the device as TV-out.
  55. *
  56. * While we have these nice friendly flags for output types that ought
  57. * to decide this for us, the S-Video output on our HDMI+S-Video card
  58. * shows up as RGB1 (VGA).
  59. */
  60. bool is_tv;
  61. /**
  62. * This is set if we treat the device as HDMI, instead of DVI.
  63. */
  64. bool is_hdmi;
  65. /**
  66. * This is set if we detect output of sdvo device as LVDS.
  67. */
  68. bool is_lvds;
  69. /**
  70. * Returned SDTV resolutions allowed for the current format, if the
  71. * device reported it.
  72. */
  73. struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
  74. /**
  75. * Current selected TV format.
  76. *
  77. * This is stored in the same structure that's passed to the device, for
  78. * convenience.
  79. */
  80. struct intel_sdvo_tv_format tv_format;
  81. /*
  82. * supported encoding mode, used to determine whether HDMI is
  83. * supported
  84. */
  85. struct intel_sdvo_encode encode;
  86. /* DDC bus used by this SDVO output */
  87. uint8_t ddc_bus;
  88. int save_sdvo_mult;
  89. u16 save_active_outputs;
  90. struct intel_sdvo_dtd save_input_dtd_1, save_input_dtd_2;
  91. struct intel_sdvo_dtd save_output_dtd[16];
  92. u32 save_SDVOX;
  93. };
  94. /**
  95. * Writes the SDVOB or SDVOC with the given value, but always writes both
  96. * SDVOB and SDVOC to work around apparent hardware issues (according to
  97. * comments in the BIOS).
  98. */
  99. static void intel_sdvo_write_sdvox(struct intel_output *intel_output, u32 val)
  100. {
  101. struct drm_device *dev = intel_output->base.dev;
  102. struct drm_i915_private *dev_priv = dev->dev_private;
  103. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  104. u32 bval = val, cval = val;
  105. int i;
  106. if (sdvo_priv->output_device == SDVOB) {
  107. cval = I915_READ(SDVOC);
  108. } else {
  109. bval = I915_READ(SDVOB);
  110. }
  111. /*
  112. * Write the registers twice for luck. Sometimes,
  113. * writing them only once doesn't appear to 'stick'.
  114. * The BIOS does this too. Yay, magic
  115. */
  116. for (i = 0; i < 2; i++)
  117. {
  118. I915_WRITE(SDVOB, bval);
  119. I915_READ(SDVOB);
  120. I915_WRITE(SDVOC, cval);
  121. I915_READ(SDVOC);
  122. }
  123. }
  124. static bool intel_sdvo_read_byte(struct intel_output *intel_output, u8 addr,
  125. u8 *ch)
  126. {
  127. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  128. u8 out_buf[2];
  129. u8 buf[2];
  130. int ret;
  131. struct i2c_msg msgs[] = {
  132. {
  133. .addr = sdvo_priv->slave_addr >> 1,
  134. .flags = 0,
  135. .len = 1,
  136. .buf = out_buf,
  137. },
  138. {
  139. .addr = sdvo_priv->slave_addr >> 1,
  140. .flags = I2C_M_RD,
  141. .len = 1,
  142. .buf = buf,
  143. }
  144. };
  145. out_buf[0] = addr;
  146. out_buf[1] = 0;
  147. if ((ret = i2c_transfer(sdvo_priv->i2c_bus, msgs, 2)) == 2)
  148. {
  149. *ch = buf[0];
  150. return true;
  151. }
  152. DRM_DEBUG("i2c transfer returned %d\n", ret);
  153. return false;
  154. }
  155. static bool intel_sdvo_write_byte(struct intel_output *intel_output, int addr,
  156. u8 ch)
  157. {
  158. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  159. u8 out_buf[2];
  160. struct i2c_msg msgs[] = {
  161. {
  162. .addr = sdvo_priv->slave_addr >> 1,
  163. .flags = 0,
  164. .len = 2,
  165. .buf = out_buf,
  166. }
  167. };
  168. out_buf[0] = addr;
  169. out_buf[1] = ch;
  170. if (i2c_transfer(intel_output->i2c_bus, msgs, 1) == 1)
  171. {
  172. return true;
  173. }
  174. return false;
  175. }
  176. #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
  177. /** Mapping of command numbers to names, for debug output */
  178. static const struct _sdvo_cmd_name {
  179. u8 cmd;
  180. char *name;
  181. } sdvo_cmd_names[] = {
  182. SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
  183. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
  184. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
  185. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
  186. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
  187. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
  188. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
  189. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
  190. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
  191. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
  192. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
  193. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
  194. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
  195. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
  196. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
  197. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
  198. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
  199. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  200. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
  201. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  202. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
  203. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
  204. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
  205. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
  206. SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
  207. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
  208. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
  209. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
  210. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
  211. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
  212. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
  213. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
  214. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
  215. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
  216. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
  217. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
  218. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
  219. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
  220. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
  221. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
  222. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
  223. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
  224. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
  225. /* HDMI op code */
  226. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
  227. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
  228. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
  229. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
  230. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
  231. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
  232. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
  233. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
  234. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
  235. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
  236. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
  237. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
  238. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
  239. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
  240. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
  241. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
  242. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
  243. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
  244. SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
  245. SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
  246. };
  247. #define SDVO_NAME(dev_priv) ((dev_priv)->output_device == SDVOB ? "SDVOB" : "SDVOC")
  248. #define SDVO_PRIV(output) ((struct intel_sdvo_priv *) (output)->dev_priv)
  249. #ifdef SDVO_DEBUG
  250. static void intel_sdvo_debug_write(struct intel_output *intel_output, u8 cmd,
  251. void *args, int args_len)
  252. {
  253. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  254. int i;
  255. DRM_DEBUG_KMS(I915_SDVO, "%s: W: %02X ",
  256. SDVO_NAME(sdvo_priv), cmd);
  257. for (i = 0; i < args_len; i++)
  258. DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
  259. for (; i < 8; i++)
  260. DRM_LOG_KMS(" ");
  261. for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
  262. if (cmd == sdvo_cmd_names[i].cmd) {
  263. DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
  264. break;
  265. }
  266. }
  267. if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
  268. DRM_LOG_KMS("(%02X)", cmd);
  269. DRM_LOG_KMS("\n");
  270. }
  271. #else
  272. #define intel_sdvo_debug_write(o, c, a, l)
  273. #endif
  274. static void intel_sdvo_write_cmd(struct intel_output *intel_output, u8 cmd,
  275. void *args, int args_len)
  276. {
  277. int i;
  278. intel_sdvo_debug_write(intel_output, cmd, args, args_len);
  279. for (i = 0; i < args_len; i++) {
  280. intel_sdvo_write_byte(intel_output, SDVO_I2C_ARG_0 - i,
  281. ((u8*)args)[i]);
  282. }
  283. intel_sdvo_write_byte(intel_output, SDVO_I2C_OPCODE, cmd);
  284. }
  285. #ifdef SDVO_DEBUG
  286. static const char *cmd_status_names[] = {
  287. "Power on",
  288. "Success",
  289. "Not supported",
  290. "Invalid arg",
  291. "Pending",
  292. "Target not specified",
  293. "Scaling not supported"
  294. };
  295. static void intel_sdvo_debug_response(struct intel_output *intel_output,
  296. void *response, int response_len,
  297. u8 status)
  298. {
  299. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  300. int i;
  301. DRM_DEBUG_KMS(I915_SDVO, "%s: R: ", SDVO_NAME(sdvo_priv));
  302. for (i = 0; i < response_len; i++)
  303. DRM_LOG_KMS("%02X ", ((u8 *)response)[i]);
  304. for (; i < 8; i++)
  305. DRM_LOG_KMS(" ");
  306. if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
  307. DRM_LOG_KMS("(%s)", cmd_status_names[status]);
  308. else
  309. DRM_LOG_KMS("(??? %d)", status);
  310. DRM_LOG_KMS("\n");
  311. }
  312. #else
  313. #define intel_sdvo_debug_response(o, r, l, s)
  314. #endif
  315. static u8 intel_sdvo_read_response(struct intel_output *intel_output,
  316. void *response, int response_len)
  317. {
  318. int i;
  319. u8 status;
  320. u8 retry = 50;
  321. while (retry--) {
  322. /* Read the command response */
  323. for (i = 0; i < response_len; i++) {
  324. intel_sdvo_read_byte(intel_output,
  325. SDVO_I2C_RETURN_0 + i,
  326. &((u8 *)response)[i]);
  327. }
  328. /* read the return status */
  329. intel_sdvo_read_byte(intel_output, SDVO_I2C_CMD_STATUS,
  330. &status);
  331. intel_sdvo_debug_response(intel_output, response, response_len,
  332. status);
  333. if (status != SDVO_CMD_STATUS_PENDING)
  334. return status;
  335. mdelay(50);
  336. }
  337. return status;
  338. }
  339. static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
  340. {
  341. if (mode->clock >= 100000)
  342. return 1;
  343. else if (mode->clock >= 50000)
  344. return 2;
  345. else
  346. return 4;
  347. }
  348. /**
  349. * Don't check status code from this as it switches the bus back to the
  350. * SDVO chips which defeats the purpose of doing a bus switch in the first
  351. * place.
  352. */
  353. static void intel_sdvo_set_control_bus_switch(struct intel_output *intel_output,
  354. u8 target)
  355. {
  356. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CONTROL_BUS_SWITCH, &target, 1);
  357. }
  358. static bool intel_sdvo_set_target_input(struct intel_output *intel_output, bool target_0, bool target_1)
  359. {
  360. struct intel_sdvo_set_target_input_args targets = {0};
  361. u8 status;
  362. if (target_0 && target_1)
  363. return SDVO_CMD_STATUS_NOTSUPP;
  364. if (target_1)
  365. targets.target_1 = 1;
  366. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_INPUT, &targets,
  367. sizeof(targets));
  368. status = intel_sdvo_read_response(intel_output, NULL, 0);
  369. return (status == SDVO_CMD_STATUS_SUCCESS);
  370. }
  371. /**
  372. * Return whether each input is trained.
  373. *
  374. * This function is making an assumption about the layout of the response,
  375. * which should be checked against the docs.
  376. */
  377. static bool intel_sdvo_get_trained_inputs(struct intel_output *intel_output, bool *input_1, bool *input_2)
  378. {
  379. struct intel_sdvo_get_trained_inputs_response response;
  380. u8 status;
  381. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
  382. status = intel_sdvo_read_response(intel_output, &response, sizeof(response));
  383. if (status != SDVO_CMD_STATUS_SUCCESS)
  384. return false;
  385. *input_1 = response.input0_trained;
  386. *input_2 = response.input1_trained;
  387. return true;
  388. }
  389. static bool intel_sdvo_get_active_outputs(struct intel_output *intel_output,
  390. u16 *outputs)
  391. {
  392. u8 status;
  393. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_OUTPUTS, NULL, 0);
  394. status = intel_sdvo_read_response(intel_output, outputs, sizeof(*outputs));
  395. return (status == SDVO_CMD_STATUS_SUCCESS);
  396. }
  397. static bool intel_sdvo_set_active_outputs(struct intel_output *intel_output,
  398. u16 outputs)
  399. {
  400. u8 status;
  401. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
  402. sizeof(outputs));
  403. status = intel_sdvo_read_response(intel_output, NULL, 0);
  404. return (status == SDVO_CMD_STATUS_SUCCESS);
  405. }
  406. static bool intel_sdvo_set_encoder_power_state(struct intel_output *intel_output,
  407. int mode)
  408. {
  409. u8 status, state = SDVO_ENCODER_STATE_ON;
  410. switch (mode) {
  411. case DRM_MODE_DPMS_ON:
  412. state = SDVO_ENCODER_STATE_ON;
  413. break;
  414. case DRM_MODE_DPMS_STANDBY:
  415. state = SDVO_ENCODER_STATE_STANDBY;
  416. break;
  417. case DRM_MODE_DPMS_SUSPEND:
  418. state = SDVO_ENCODER_STATE_SUSPEND;
  419. break;
  420. case DRM_MODE_DPMS_OFF:
  421. state = SDVO_ENCODER_STATE_OFF;
  422. break;
  423. }
  424. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
  425. sizeof(state));
  426. status = intel_sdvo_read_response(intel_output, NULL, 0);
  427. return (status == SDVO_CMD_STATUS_SUCCESS);
  428. }
  429. static bool intel_sdvo_get_input_pixel_clock_range(struct intel_output *intel_output,
  430. int *clock_min,
  431. int *clock_max)
  432. {
  433. struct intel_sdvo_pixel_clock_range clocks;
  434. u8 status;
  435. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
  436. NULL, 0);
  437. status = intel_sdvo_read_response(intel_output, &clocks, sizeof(clocks));
  438. if (status != SDVO_CMD_STATUS_SUCCESS)
  439. return false;
  440. /* Convert the values from units of 10 kHz to kHz. */
  441. *clock_min = clocks.min * 10;
  442. *clock_max = clocks.max * 10;
  443. return true;
  444. }
  445. static bool intel_sdvo_set_target_output(struct intel_output *intel_output,
  446. u16 outputs)
  447. {
  448. u8 status;
  449. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
  450. sizeof(outputs));
  451. status = intel_sdvo_read_response(intel_output, NULL, 0);
  452. return (status == SDVO_CMD_STATUS_SUCCESS);
  453. }
  454. static bool intel_sdvo_get_timing(struct intel_output *intel_output, u8 cmd,
  455. struct intel_sdvo_dtd *dtd)
  456. {
  457. u8 status;
  458. intel_sdvo_write_cmd(intel_output, cmd, NULL, 0);
  459. status = intel_sdvo_read_response(intel_output, &dtd->part1,
  460. sizeof(dtd->part1));
  461. if (status != SDVO_CMD_STATUS_SUCCESS)
  462. return false;
  463. intel_sdvo_write_cmd(intel_output, cmd + 1, NULL, 0);
  464. status = intel_sdvo_read_response(intel_output, &dtd->part2,
  465. sizeof(dtd->part2));
  466. if (status != SDVO_CMD_STATUS_SUCCESS)
  467. return false;
  468. return true;
  469. }
  470. static bool intel_sdvo_get_input_timing(struct intel_output *intel_output,
  471. struct intel_sdvo_dtd *dtd)
  472. {
  473. return intel_sdvo_get_timing(intel_output,
  474. SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
  475. }
  476. static bool intel_sdvo_get_output_timing(struct intel_output *intel_output,
  477. struct intel_sdvo_dtd *dtd)
  478. {
  479. return intel_sdvo_get_timing(intel_output,
  480. SDVO_CMD_GET_OUTPUT_TIMINGS_PART1, dtd);
  481. }
  482. static bool intel_sdvo_set_timing(struct intel_output *intel_output, u8 cmd,
  483. struct intel_sdvo_dtd *dtd)
  484. {
  485. u8 status;
  486. intel_sdvo_write_cmd(intel_output, cmd, &dtd->part1, sizeof(dtd->part1));
  487. status = intel_sdvo_read_response(intel_output, NULL, 0);
  488. if (status != SDVO_CMD_STATUS_SUCCESS)
  489. return false;
  490. intel_sdvo_write_cmd(intel_output, cmd + 1, &dtd->part2, sizeof(dtd->part2));
  491. status = intel_sdvo_read_response(intel_output, NULL, 0);
  492. if (status != SDVO_CMD_STATUS_SUCCESS)
  493. return false;
  494. return true;
  495. }
  496. static bool intel_sdvo_set_input_timing(struct intel_output *intel_output,
  497. struct intel_sdvo_dtd *dtd)
  498. {
  499. return intel_sdvo_set_timing(intel_output,
  500. SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
  501. }
  502. static bool intel_sdvo_set_output_timing(struct intel_output *intel_output,
  503. struct intel_sdvo_dtd *dtd)
  504. {
  505. return intel_sdvo_set_timing(intel_output,
  506. SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
  507. }
  508. static bool
  509. intel_sdvo_create_preferred_input_timing(struct intel_output *output,
  510. uint16_t clock,
  511. uint16_t width,
  512. uint16_t height)
  513. {
  514. struct intel_sdvo_preferred_input_timing_args args;
  515. uint8_t status;
  516. memset(&args, 0, sizeof(args));
  517. args.clock = clock;
  518. args.width = width;
  519. args.height = height;
  520. args.interlace = 0;
  521. args.scaled = 0;
  522. intel_sdvo_write_cmd(output, SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
  523. &args, sizeof(args));
  524. status = intel_sdvo_read_response(output, NULL, 0);
  525. if (status != SDVO_CMD_STATUS_SUCCESS)
  526. return false;
  527. return true;
  528. }
  529. static bool intel_sdvo_get_preferred_input_timing(struct intel_output *output,
  530. struct intel_sdvo_dtd *dtd)
  531. {
  532. bool status;
  533. intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
  534. NULL, 0);
  535. status = intel_sdvo_read_response(output, &dtd->part1,
  536. sizeof(dtd->part1));
  537. if (status != SDVO_CMD_STATUS_SUCCESS)
  538. return false;
  539. intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
  540. NULL, 0);
  541. status = intel_sdvo_read_response(output, &dtd->part2,
  542. sizeof(dtd->part2));
  543. if (status != SDVO_CMD_STATUS_SUCCESS)
  544. return false;
  545. return false;
  546. }
  547. static int intel_sdvo_get_clock_rate_mult(struct intel_output *intel_output)
  548. {
  549. u8 response, status;
  550. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
  551. status = intel_sdvo_read_response(intel_output, &response, 1);
  552. if (status != SDVO_CMD_STATUS_SUCCESS) {
  553. DRM_DEBUG("Couldn't get SDVO clock rate multiplier\n");
  554. return SDVO_CLOCK_RATE_MULT_1X;
  555. } else {
  556. DRM_DEBUG("Current clock rate multiplier: %d\n", response);
  557. }
  558. return response;
  559. }
  560. static bool intel_sdvo_set_clock_rate_mult(struct intel_output *intel_output, u8 val)
  561. {
  562. u8 status;
  563. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
  564. status = intel_sdvo_read_response(intel_output, NULL, 0);
  565. if (status != SDVO_CMD_STATUS_SUCCESS)
  566. return false;
  567. return true;
  568. }
  569. static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
  570. struct drm_display_mode *mode)
  571. {
  572. uint16_t width, height;
  573. uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
  574. uint16_t h_sync_offset, v_sync_offset;
  575. width = mode->crtc_hdisplay;
  576. height = mode->crtc_vdisplay;
  577. /* do some mode translations */
  578. h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
  579. h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
  580. v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
  581. v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
  582. h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
  583. v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
  584. dtd->part1.clock = mode->clock / 10;
  585. dtd->part1.h_active = width & 0xff;
  586. dtd->part1.h_blank = h_blank_len & 0xff;
  587. dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
  588. ((h_blank_len >> 8) & 0xf);
  589. dtd->part1.v_active = height & 0xff;
  590. dtd->part1.v_blank = v_blank_len & 0xff;
  591. dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
  592. ((v_blank_len >> 8) & 0xf);
  593. dtd->part2.h_sync_off = h_sync_offset & 0xff;
  594. dtd->part2.h_sync_width = h_sync_len & 0xff;
  595. dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
  596. (v_sync_len & 0xf);
  597. dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
  598. ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
  599. ((v_sync_len & 0x30) >> 4);
  600. dtd->part2.dtd_flags = 0x18;
  601. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  602. dtd->part2.dtd_flags |= 0x2;
  603. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  604. dtd->part2.dtd_flags |= 0x4;
  605. dtd->part2.sdvo_flags = 0;
  606. dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
  607. dtd->part2.reserved = 0;
  608. }
  609. static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
  610. struct intel_sdvo_dtd *dtd)
  611. {
  612. mode->hdisplay = dtd->part1.h_active;
  613. mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
  614. mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
  615. mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
  616. mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
  617. mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
  618. mode->htotal = mode->hdisplay + dtd->part1.h_blank;
  619. mode->htotal += (dtd->part1.h_high & 0xf) << 8;
  620. mode->vdisplay = dtd->part1.v_active;
  621. mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
  622. mode->vsync_start = mode->vdisplay;
  623. mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
  624. mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
  625. mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
  626. mode->vsync_end = mode->vsync_start +
  627. (dtd->part2.v_sync_off_width & 0xf);
  628. mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
  629. mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
  630. mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
  631. mode->clock = dtd->part1.clock * 10;
  632. mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
  633. if (dtd->part2.dtd_flags & 0x2)
  634. mode->flags |= DRM_MODE_FLAG_PHSYNC;
  635. if (dtd->part2.dtd_flags & 0x4)
  636. mode->flags |= DRM_MODE_FLAG_PVSYNC;
  637. }
  638. static bool intel_sdvo_get_supp_encode(struct intel_output *output,
  639. struct intel_sdvo_encode *encode)
  640. {
  641. uint8_t status;
  642. intel_sdvo_write_cmd(output, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
  643. status = intel_sdvo_read_response(output, encode, sizeof(*encode));
  644. if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
  645. memset(encode, 0, sizeof(*encode));
  646. return false;
  647. }
  648. return true;
  649. }
  650. static bool intel_sdvo_set_encode(struct intel_output *output, uint8_t mode)
  651. {
  652. uint8_t status;
  653. intel_sdvo_write_cmd(output, SDVO_CMD_SET_ENCODE, &mode, 1);
  654. status = intel_sdvo_read_response(output, NULL, 0);
  655. return (status == SDVO_CMD_STATUS_SUCCESS);
  656. }
  657. static bool intel_sdvo_set_colorimetry(struct intel_output *output,
  658. uint8_t mode)
  659. {
  660. uint8_t status;
  661. intel_sdvo_write_cmd(output, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
  662. status = intel_sdvo_read_response(output, NULL, 0);
  663. return (status == SDVO_CMD_STATUS_SUCCESS);
  664. }
  665. #if 0
  666. static void intel_sdvo_dump_hdmi_buf(struct intel_output *output)
  667. {
  668. int i, j;
  669. uint8_t set_buf_index[2];
  670. uint8_t av_split;
  671. uint8_t buf_size;
  672. uint8_t buf[48];
  673. uint8_t *pos;
  674. intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
  675. intel_sdvo_read_response(output, &av_split, 1);
  676. for (i = 0; i <= av_split; i++) {
  677. set_buf_index[0] = i; set_buf_index[1] = 0;
  678. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX,
  679. set_buf_index, 2);
  680. intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
  681. intel_sdvo_read_response(output, &buf_size, 1);
  682. pos = buf;
  683. for (j = 0; j <= buf_size; j += 8) {
  684. intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_DATA,
  685. NULL, 0);
  686. intel_sdvo_read_response(output, pos, 8);
  687. pos += 8;
  688. }
  689. }
  690. }
  691. #endif
  692. static void intel_sdvo_set_hdmi_buf(struct intel_output *output, int index,
  693. uint8_t *data, int8_t size, uint8_t tx_rate)
  694. {
  695. uint8_t set_buf_index[2];
  696. set_buf_index[0] = index;
  697. set_buf_index[1] = 0;
  698. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX, set_buf_index, 2);
  699. for (; size > 0; size -= 8) {
  700. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_DATA, data, 8);
  701. data += 8;
  702. }
  703. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
  704. }
  705. static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
  706. {
  707. uint8_t csum = 0;
  708. int i;
  709. for (i = 0; i < size; i++)
  710. csum += data[i];
  711. return 0x100 - csum;
  712. }
  713. #define DIP_TYPE_AVI 0x82
  714. #define DIP_VERSION_AVI 0x2
  715. #define DIP_LEN_AVI 13
  716. struct dip_infoframe {
  717. uint8_t type;
  718. uint8_t version;
  719. uint8_t len;
  720. uint8_t checksum;
  721. union {
  722. struct {
  723. /* Packet Byte #1 */
  724. uint8_t S:2;
  725. uint8_t B:2;
  726. uint8_t A:1;
  727. uint8_t Y:2;
  728. uint8_t rsvd1:1;
  729. /* Packet Byte #2 */
  730. uint8_t R:4;
  731. uint8_t M:2;
  732. uint8_t C:2;
  733. /* Packet Byte #3 */
  734. uint8_t SC:2;
  735. uint8_t Q:2;
  736. uint8_t EC:3;
  737. uint8_t ITC:1;
  738. /* Packet Byte #4 */
  739. uint8_t VIC:7;
  740. uint8_t rsvd2:1;
  741. /* Packet Byte #5 */
  742. uint8_t PR:4;
  743. uint8_t rsvd3:4;
  744. /* Packet Byte #6~13 */
  745. uint16_t top_bar_end;
  746. uint16_t bottom_bar_start;
  747. uint16_t left_bar_end;
  748. uint16_t right_bar_start;
  749. } avi;
  750. struct {
  751. /* Packet Byte #1 */
  752. uint8_t channel_count:3;
  753. uint8_t rsvd1:1;
  754. uint8_t coding_type:4;
  755. /* Packet Byte #2 */
  756. uint8_t sample_size:2; /* SS0, SS1 */
  757. uint8_t sample_frequency:3;
  758. uint8_t rsvd2:3;
  759. /* Packet Byte #3 */
  760. uint8_t coding_type_private:5;
  761. uint8_t rsvd3:3;
  762. /* Packet Byte #4 */
  763. uint8_t channel_allocation;
  764. /* Packet Byte #5 */
  765. uint8_t rsvd4:3;
  766. uint8_t level_shift:4;
  767. uint8_t downmix_inhibit:1;
  768. } audio;
  769. uint8_t payload[28];
  770. } __attribute__ ((packed)) u;
  771. } __attribute__((packed));
  772. static void intel_sdvo_set_avi_infoframe(struct intel_output *output,
  773. struct drm_display_mode * mode)
  774. {
  775. struct dip_infoframe avi_if = {
  776. .type = DIP_TYPE_AVI,
  777. .version = DIP_VERSION_AVI,
  778. .len = DIP_LEN_AVI,
  779. };
  780. avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
  781. 4 + avi_if.len);
  782. intel_sdvo_set_hdmi_buf(output, 1, (uint8_t *)&avi_if, 4 + avi_if.len,
  783. SDVO_HBUF_TX_VSYNC);
  784. }
  785. static void intel_sdvo_set_tv_format(struct intel_output *output)
  786. {
  787. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  788. struct intel_sdvo_tv_format *format, unset;
  789. u8 status;
  790. format = &sdvo_priv->tv_format;
  791. memset(&unset, 0, sizeof(unset));
  792. if (memcmp(format, &unset, sizeof(*format))) {
  793. DRM_DEBUG("%s: Choosing default TV format of NTSC-M\n",
  794. SDVO_NAME(sdvo_priv));
  795. format->ntsc_m = 1;
  796. intel_sdvo_write_cmd(output, SDVO_CMD_SET_TV_FORMAT, format,
  797. sizeof(*format));
  798. status = intel_sdvo_read_response(output, NULL, 0);
  799. if (status != SDVO_CMD_STATUS_SUCCESS)
  800. DRM_DEBUG("%s: Failed to set TV format\n",
  801. SDVO_NAME(sdvo_priv));
  802. }
  803. }
  804. static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
  805. struct drm_display_mode *mode,
  806. struct drm_display_mode *adjusted_mode)
  807. {
  808. struct intel_output *output = enc_to_intel_output(encoder);
  809. struct intel_sdvo_priv *dev_priv = output->dev_priv;
  810. if (!dev_priv->is_tv) {
  811. /* Make the CRTC code factor in the SDVO pixel multiplier. The
  812. * SDVO device will be told of the multiplier during mode_set.
  813. */
  814. adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
  815. } else {
  816. struct intel_sdvo_dtd output_dtd;
  817. bool success;
  818. /* We need to construct preferred input timings based on our
  819. * output timings. To do that, we have to set the output
  820. * timings, even though this isn't really the right place in
  821. * the sequence to do it. Oh well.
  822. */
  823. /* Set output timings */
  824. intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  825. intel_sdvo_set_target_output(output,
  826. dev_priv->controlled_output);
  827. intel_sdvo_set_output_timing(output, &output_dtd);
  828. /* Set the input timing to the screen. Assume always input 0. */
  829. intel_sdvo_set_target_input(output, true, false);
  830. success = intel_sdvo_create_preferred_input_timing(output,
  831. mode->clock / 10,
  832. mode->hdisplay,
  833. mode->vdisplay);
  834. if (success) {
  835. struct intel_sdvo_dtd input_dtd;
  836. intel_sdvo_get_preferred_input_timing(output,
  837. &input_dtd);
  838. intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
  839. drm_mode_set_crtcinfo(adjusted_mode, 0);
  840. mode->clock = adjusted_mode->clock;
  841. adjusted_mode->clock *=
  842. intel_sdvo_get_pixel_multiplier(mode);
  843. } else {
  844. return false;
  845. }
  846. }
  847. return true;
  848. }
  849. static void intel_sdvo_mode_set(struct drm_encoder *encoder,
  850. struct drm_display_mode *mode,
  851. struct drm_display_mode *adjusted_mode)
  852. {
  853. struct drm_device *dev = encoder->dev;
  854. struct drm_i915_private *dev_priv = dev->dev_private;
  855. struct drm_crtc *crtc = encoder->crtc;
  856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  857. struct intel_output *output = enc_to_intel_output(encoder);
  858. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  859. u32 sdvox = 0;
  860. int sdvo_pixel_multiply;
  861. struct intel_sdvo_in_out_map in_out;
  862. struct intel_sdvo_dtd input_dtd;
  863. u8 status;
  864. if (!mode)
  865. return;
  866. /* First, set the input mapping for the first input to our controlled
  867. * output. This is only correct if we're a single-input device, in
  868. * which case the first input is the output from the appropriate SDVO
  869. * channel on the motherboard. In a two-input device, the first input
  870. * will be SDVOB and the second SDVOC.
  871. */
  872. in_out.in0 = sdvo_priv->controlled_output;
  873. in_out.in1 = 0;
  874. intel_sdvo_write_cmd(output, SDVO_CMD_SET_IN_OUT_MAP,
  875. &in_out, sizeof(in_out));
  876. status = intel_sdvo_read_response(output, NULL, 0);
  877. if (sdvo_priv->is_hdmi) {
  878. intel_sdvo_set_avi_infoframe(output, mode);
  879. sdvox |= SDVO_AUDIO_ENABLE;
  880. }
  881. /* We have tried to get input timing in mode_fixup, and filled into
  882. adjusted_mode */
  883. if (sdvo_priv->is_tv)
  884. intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
  885. else
  886. intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
  887. /* If it's a TV, we already set the output timing in mode_fixup.
  888. * Otherwise, the output timing is equal to the input timing.
  889. */
  890. if (!sdvo_priv->is_tv) {
  891. /* Set the output timing to the screen */
  892. intel_sdvo_set_target_output(output,
  893. sdvo_priv->controlled_output);
  894. intel_sdvo_set_output_timing(output, &input_dtd);
  895. }
  896. /* Set the input timing to the screen. Assume always input 0. */
  897. intel_sdvo_set_target_input(output, true, false);
  898. if (sdvo_priv->is_tv)
  899. intel_sdvo_set_tv_format(output);
  900. /* We would like to use intel_sdvo_create_preferred_input_timing() to
  901. * provide the device with a timing it can support, if it supports that
  902. * feature. However, presumably we would need to adjust the CRTC to
  903. * output the preferred timing, and we don't support that currently.
  904. */
  905. #if 0
  906. success = intel_sdvo_create_preferred_input_timing(output, clock,
  907. width, height);
  908. if (success) {
  909. struct intel_sdvo_dtd *input_dtd;
  910. intel_sdvo_get_preferred_input_timing(output, &input_dtd);
  911. intel_sdvo_set_input_timing(output, &input_dtd);
  912. }
  913. #else
  914. intel_sdvo_set_input_timing(output, &input_dtd);
  915. #endif
  916. switch (intel_sdvo_get_pixel_multiplier(mode)) {
  917. case 1:
  918. intel_sdvo_set_clock_rate_mult(output,
  919. SDVO_CLOCK_RATE_MULT_1X);
  920. break;
  921. case 2:
  922. intel_sdvo_set_clock_rate_mult(output,
  923. SDVO_CLOCK_RATE_MULT_2X);
  924. break;
  925. case 4:
  926. intel_sdvo_set_clock_rate_mult(output,
  927. SDVO_CLOCK_RATE_MULT_4X);
  928. break;
  929. }
  930. /* Set the SDVO control regs. */
  931. if (IS_I965G(dev)) {
  932. sdvox |= SDVO_BORDER_ENABLE |
  933. SDVO_VSYNC_ACTIVE_HIGH |
  934. SDVO_HSYNC_ACTIVE_HIGH;
  935. } else {
  936. sdvox |= I915_READ(sdvo_priv->output_device);
  937. switch (sdvo_priv->output_device) {
  938. case SDVOB:
  939. sdvox &= SDVOB_PRESERVE_MASK;
  940. break;
  941. case SDVOC:
  942. sdvox &= SDVOC_PRESERVE_MASK;
  943. break;
  944. }
  945. sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
  946. }
  947. if (intel_crtc->pipe == 1)
  948. sdvox |= SDVO_PIPE_B_SELECT;
  949. sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
  950. if (IS_I965G(dev)) {
  951. /* done in crtc_mode_set as the dpll_md reg must be written early */
  952. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  953. /* done in crtc_mode_set as it lives inside the dpll register */
  954. } else {
  955. sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
  956. }
  957. intel_sdvo_write_sdvox(output, sdvox);
  958. }
  959. static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
  960. {
  961. struct drm_device *dev = encoder->dev;
  962. struct drm_i915_private *dev_priv = dev->dev_private;
  963. struct intel_output *intel_output = enc_to_intel_output(encoder);
  964. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  965. u32 temp;
  966. if (mode != DRM_MODE_DPMS_ON) {
  967. intel_sdvo_set_active_outputs(intel_output, 0);
  968. if (0)
  969. intel_sdvo_set_encoder_power_state(intel_output, mode);
  970. if (mode == DRM_MODE_DPMS_OFF) {
  971. temp = I915_READ(sdvo_priv->output_device);
  972. if ((temp & SDVO_ENABLE) != 0) {
  973. intel_sdvo_write_sdvox(intel_output, temp & ~SDVO_ENABLE);
  974. }
  975. }
  976. } else {
  977. bool input1, input2;
  978. int i;
  979. u8 status;
  980. temp = I915_READ(sdvo_priv->output_device);
  981. if ((temp & SDVO_ENABLE) == 0)
  982. intel_sdvo_write_sdvox(intel_output, temp | SDVO_ENABLE);
  983. for (i = 0; i < 2; i++)
  984. intel_wait_for_vblank(dev);
  985. status = intel_sdvo_get_trained_inputs(intel_output, &input1,
  986. &input2);
  987. /* Warn if the device reported failure to sync.
  988. * A lot of SDVO devices fail to notify of sync, but it's
  989. * a given it the status is a success, we succeeded.
  990. */
  991. if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
  992. DRM_DEBUG("First %s output reported failure to sync\n",
  993. SDVO_NAME(sdvo_priv));
  994. }
  995. if (0)
  996. intel_sdvo_set_encoder_power_state(intel_output, mode);
  997. intel_sdvo_set_active_outputs(intel_output, sdvo_priv->controlled_output);
  998. }
  999. return;
  1000. }
  1001. static void intel_sdvo_save(struct drm_connector *connector)
  1002. {
  1003. struct drm_device *dev = connector->dev;
  1004. struct drm_i915_private *dev_priv = dev->dev_private;
  1005. struct intel_output *intel_output = to_intel_output(connector);
  1006. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1007. int o;
  1008. sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_output);
  1009. intel_sdvo_get_active_outputs(intel_output, &sdvo_priv->save_active_outputs);
  1010. if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
  1011. intel_sdvo_set_target_input(intel_output, true, false);
  1012. intel_sdvo_get_input_timing(intel_output,
  1013. &sdvo_priv->save_input_dtd_1);
  1014. }
  1015. if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
  1016. intel_sdvo_set_target_input(intel_output, false, true);
  1017. intel_sdvo_get_input_timing(intel_output,
  1018. &sdvo_priv->save_input_dtd_2);
  1019. }
  1020. for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
  1021. {
  1022. u16 this_output = (1 << o);
  1023. if (sdvo_priv->caps.output_flags & this_output)
  1024. {
  1025. intel_sdvo_set_target_output(intel_output, this_output);
  1026. intel_sdvo_get_output_timing(intel_output,
  1027. &sdvo_priv->save_output_dtd[o]);
  1028. }
  1029. }
  1030. if (sdvo_priv->is_tv) {
  1031. /* XXX: Save TV format/enhancements. */
  1032. }
  1033. sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->output_device);
  1034. }
  1035. static void intel_sdvo_restore(struct drm_connector *connector)
  1036. {
  1037. struct drm_device *dev = connector->dev;
  1038. struct intel_output *intel_output = to_intel_output(connector);
  1039. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1040. int o;
  1041. int i;
  1042. bool input1, input2;
  1043. u8 status;
  1044. intel_sdvo_set_active_outputs(intel_output, 0);
  1045. for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
  1046. {
  1047. u16 this_output = (1 << o);
  1048. if (sdvo_priv->caps.output_flags & this_output) {
  1049. intel_sdvo_set_target_output(intel_output, this_output);
  1050. intel_sdvo_set_output_timing(intel_output, &sdvo_priv->save_output_dtd[o]);
  1051. }
  1052. }
  1053. if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
  1054. intel_sdvo_set_target_input(intel_output, true, false);
  1055. intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_1);
  1056. }
  1057. if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
  1058. intel_sdvo_set_target_input(intel_output, false, true);
  1059. intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_2);
  1060. }
  1061. intel_sdvo_set_clock_rate_mult(intel_output, sdvo_priv->save_sdvo_mult);
  1062. if (sdvo_priv->is_tv) {
  1063. /* XXX: Restore TV format/enhancements. */
  1064. }
  1065. intel_sdvo_write_sdvox(intel_output, sdvo_priv->save_SDVOX);
  1066. if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
  1067. {
  1068. for (i = 0; i < 2; i++)
  1069. intel_wait_for_vblank(dev);
  1070. status = intel_sdvo_get_trained_inputs(intel_output, &input1, &input2);
  1071. if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
  1072. DRM_DEBUG("First %s output reported failure to sync\n",
  1073. SDVO_NAME(sdvo_priv));
  1074. }
  1075. intel_sdvo_set_active_outputs(intel_output, sdvo_priv->save_active_outputs);
  1076. }
  1077. static int intel_sdvo_mode_valid(struct drm_connector *connector,
  1078. struct drm_display_mode *mode)
  1079. {
  1080. struct intel_output *intel_output = to_intel_output(connector);
  1081. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1082. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1083. return MODE_NO_DBLESCAN;
  1084. if (sdvo_priv->pixel_clock_min > mode->clock)
  1085. return MODE_CLOCK_LOW;
  1086. if (sdvo_priv->pixel_clock_max < mode->clock)
  1087. return MODE_CLOCK_HIGH;
  1088. return MODE_OK;
  1089. }
  1090. static bool intel_sdvo_get_capabilities(struct intel_output *intel_output, struct intel_sdvo_caps *caps)
  1091. {
  1092. u8 status;
  1093. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
  1094. status = intel_sdvo_read_response(intel_output, caps, sizeof(*caps));
  1095. if (status != SDVO_CMD_STATUS_SUCCESS)
  1096. return false;
  1097. return true;
  1098. }
  1099. struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
  1100. {
  1101. struct drm_connector *connector = NULL;
  1102. struct intel_output *iout = NULL;
  1103. struct intel_sdvo_priv *sdvo;
  1104. /* find the sdvo connector */
  1105. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1106. iout = to_intel_output(connector);
  1107. if (iout->type != INTEL_OUTPUT_SDVO)
  1108. continue;
  1109. sdvo = iout->dev_priv;
  1110. if (sdvo->output_device == SDVOB && sdvoB)
  1111. return connector;
  1112. if (sdvo->output_device == SDVOC && !sdvoB)
  1113. return connector;
  1114. }
  1115. return NULL;
  1116. }
  1117. int intel_sdvo_supports_hotplug(struct drm_connector *connector)
  1118. {
  1119. u8 response[2];
  1120. u8 status;
  1121. struct intel_output *intel_output;
  1122. DRM_DEBUG("\n");
  1123. if (!connector)
  1124. return 0;
  1125. intel_output = to_intel_output(connector);
  1126. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
  1127. status = intel_sdvo_read_response(intel_output, &response, 2);
  1128. if (response[0] !=0)
  1129. return 1;
  1130. return 0;
  1131. }
  1132. void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
  1133. {
  1134. u8 response[2];
  1135. u8 status;
  1136. struct intel_output *intel_output = to_intel_output(connector);
  1137. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
  1138. intel_sdvo_read_response(intel_output, &response, 2);
  1139. if (on) {
  1140. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
  1141. status = intel_sdvo_read_response(intel_output, &response, 2);
  1142. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
  1143. } else {
  1144. response[0] = 0;
  1145. response[1] = 0;
  1146. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
  1147. }
  1148. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
  1149. intel_sdvo_read_response(intel_output, &response, 2);
  1150. }
  1151. static void
  1152. intel_sdvo_hdmi_sink_detect(struct drm_connector *connector)
  1153. {
  1154. struct intel_output *intel_output = to_intel_output(connector);
  1155. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1156. struct edid *edid = NULL;
  1157. intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
  1158. edid = drm_get_edid(&intel_output->base,
  1159. intel_output->ddc_bus);
  1160. if (edid != NULL) {
  1161. sdvo_priv->is_hdmi = drm_detect_hdmi_monitor(edid);
  1162. kfree(edid);
  1163. intel_output->base.display_info.raw_edid = NULL;
  1164. }
  1165. }
  1166. static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
  1167. {
  1168. u8 response[2];
  1169. u8 status;
  1170. struct intel_output *intel_output = to_intel_output(connector);
  1171. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
  1172. status = intel_sdvo_read_response(intel_output, &response, 2);
  1173. DRM_DEBUG("SDVO response %d %d\n", response[0], response[1]);
  1174. if (status != SDVO_CMD_STATUS_SUCCESS)
  1175. return connector_status_unknown;
  1176. if ((response[0] != 0) || (response[1] != 0)) {
  1177. intel_sdvo_hdmi_sink_detect(connector);
  1178. return connector_status_connected;
  1179. } else
  1180. return connector_status_disconnected;
  1181. }
  1182. static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
  1183. {
  1184. struct intel_output *intel_output = to_intel_output(connector);
  1185. /* set the bus switch and get the modes */
  1186. intel_ddc_get_modes(intel_output);
  1187. #if 0
  1188. struct drm_device *dev = encoder->dev;
  1189. struct drm_i915_private *dev_priv = dev->dev_private;
  1190. /* Mac mini hack. On this device, I get DDC through the analog, which
  1191. * load-detects as disconnected. I fail to DDC through the SDVO DDC,
  1192. * but it does load-detect as connected. So, just steal the DDC bits
  1193. * from analog when we fail at finding it the right way.
  1194. */
  1195. crt = xf86_config->output[0];
  1196. intel_output = crt->driver_private;
  1197. if (intel_output->type == I830_OUTPUT_ANALOG &&
  1198. crt->funcs->detect(crt) == XF86OutputStatusDisconnected) {
  1199. I830I2CInit(pScrn, &intel_output->pDDCBus, GPIOA, "CRTDDC_A");
  1200. edid_mon = xf86OutputGetEDID(crt, intel_output->pDDCBus);
  1201. xf86DestroyI2CBusRec(intel_output->pDDCBus, true, true);
  1202. }
  1203. if (edid_mon) {
  1204. xf86OutputSetEDID(output, edid_mon);
  1205. modes = xf86OutputGetEDIDModes(output);
  1206. }
  1207. #endif
  1208. }
  1209. /**
  1210. * This function checks the current TV format, and chooses a default if
  1211. * it hasn't been set.
  1212. */
  1213. static void
  1214. intel_sdvo_check_tv_format(struct intel_output *output)
  1215. {
  1216. struct intel_sdvo_priv *dev_priv = output->dev_priv;
  1217. struct intel_sdvo_tv_format format;
  1218. uint8_t status;
  1219. intel_sdvo_write_cmd(output, SDVO_CMD_GET_TV_FORMAT, NULL, 0);
  1220. status = intel_sdvo_read_response(output, &format, sizeof(format));
  1221. if (status != SDVO_CMD_STATUS_SUCCESS)
  1222. return;
  1223. memcpy(&dev_priv->tv_format, &format, sizeof(format));
  1224. }
  1225. /*
  1226. * Set of SDVO TV modes.
  1227. * Note! This is in reply order (see loop in get_tv_modes).
  1228. * XXX: all 60Hz refresh?
  1229. */
  1230. struct drm_display_mode sdvo_tv_modes[] = {
  1231. { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
  1232. 416, 0, 200, 201, 232, 233, 0,
  1233. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1234. { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
  1235. 416, 0, 240, 241, 272, 273, 0,
  1236. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1237. { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
  1238. 496, 0, 300, 301, 332, 333, 0,
  1239. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1240. { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
  1241. 736, 0, 350, 351, 382, 383, 0,
  1242. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1243. { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
  1244. 736, 0, 400, 401, 432, 433, 0,
  1245. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1246. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
  1247. 736, 0, 480, 481, 512, 513, 0,
  1248. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1249. { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
  1250. 800, 0, 480, 481, 512, 513, 0,
  1251. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1252. { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
  1253. 800, 0, 576, 577, 608, 609, 0,
  1254. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1255. { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
  1256. 816, 0, 350, 351, 382, 383, 0,
  1257. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1258. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
  1259. 816, 0, 400, 401, 432, 433, 0,
  1260. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1261. { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
  1262. 816, 0, 480, 481, 512, 513, 0,
  1263. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1264. { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
  1265. 816, 0, 540, 541, 572, 573, 0,
  1266. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1267. { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
  1268. 816, 0, 576, 577, 608, 609, 0,
  1269. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1270. { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
  1271. 864, 0, 576, 577, 608, 609, 0,
  1272. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1273. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
  1274. 896, 0, 600, 601, 632, 633, 0,
  1275. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1276. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
  1277. 928, 0, 624, 625, 656, 657, 0,
  1278. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1279. { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
  1280. 1016, 0, 766, 767, 798, 799, 0,
  1281. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1282. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
  1283. 1120, 0, 768, 769, 800, 801, 0,
  1284. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1285. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
  1286. 1376, 0, 1024, 1025, 1056, 1057, 0,
  1287. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1288. };
  1289. static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
  1290. {
  1291. struct intel_output *output = to_intel_output(connector);
  1292. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  1293. struct intel_sdvo_sdtv_resolution_request tv_res;
  1294. uint32_t reply = 0;
  1295. uint8_t status;
  1296. int i = 0;
  1297. intel_sdvo_check_tv_format(output);
  1298. /* Read the list of supported input resolutions for the selected TV
  1299. * format.
  1300. */
  1301. memset(&tv_res, 0, sizeof(tv_res));
  1302. memcpy(&tv_res, &sdvo_priv->tv_format, sizeof(tv_res));
  1303. intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
  1304. &tv_res, sizeof(tv_res));
  1305. status = intel_sdvo_read_response(output, &reply, 3);
  1306. if (status != SDVO_CMD_STATUS_SUCCESS)
  1307. return;
  1308. for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
  1309. if (reply & (1 << i)) {
  1310. struct drm_display_mode *nmode;
  1311. nmode = drm_mode_duplicate(connector->dev,
  1312. &sdvo_tv_modes[i]);
  1313. if (nmode)
  1314. drm_mode_probed_add(connector, nmode);
  1315. }
  1316. }
  1317. static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
  1318. {
  1319. struct intel_output *intel_output = to_intel_output(connector);
  1320. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1321. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1322. /*
  1323. * Attempt to get the mode list from DDC.
  1324. * Assume that the preferred modes are
  1325. * arranged in priority order.
  1326. */
  1327. /* set the bus switch and get the modes */
  1328. intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
  1329. intel_ddc_get_modes(intel_output);
  1330. if (list_empty(&connector->probed_modes) == false)
  1331. return;
  1332. /* Fetch modes from VBT */
  1333. if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
  1334. struct drm_display_mode *newmode;
  1335. newmode = drm_mode_duplicate(connector->dev,
  1336. dev_priv->sdvo_lvds_vbt_mode);
  1337. if (newmode != NULL) {
  1338. /* Guarantee the mode is preferred */
  1339. newmode->type = (DRM_MODE_TYPE_PREFERRED |
  1340. DRM_MODE_TYPE_DRIVER);
  1341. drm_mode_probed_add(connector, newmode);
  1342. }
  1343. }
  1344. }
  1345. static int intel_sdvo_get_modes(struct drm_connector *connector)
  1346. {
  1347. struct intel_output *output = to_intel_output(connector);
  1348. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  1349. if (sdvo_priv->is_tv)
  1350. intel_sdvo_get_tv_modes(connector);
  1351. else if (sdvo_priv->is_lvds == true)
  1352. intel_sdvo_get_lvds_modes(connector);
  1353. else
  1354. intel_sdvo_get_ddc_modes(connector);
  1355. if (list_empty(&connector->probed_modes))
  1356. return 0;
  1357. return 1;
  1358. }
  1359. static void intel_sdvo_destroy(struct drm_connector *connector)
  1360. {
  1361. struct intel_output *intel_output = to_intel_output(connector);
  1362. if (intel_output->i2c_bus)
  1363. intel_i2c_destroy(intel_output->i2c_bus);
  1364. if (intel_output->ddc_bus)
  1365. intel_i2c_destroy(intel_output->ddc_bus);
  1366. drm_sysfs_connector_remove(connector);
  1367. drm_connector_cleanup(connector);
  1368. kfree(intel_output);
  1369. }
  1370. static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
  1371. .dpms = intel_sdvo_dpms,
  1372. .mode_fixup = intel_sdvo_mode_fixup,
  1373. .prepare = intel_encoder_prepare,
  1374. .mode_set = intel_sdvo_mode_set,
  1375. .commit = intel_encoder_commit,
  1376. };
  1377. static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
  1378. .dpms = drm_helper_connector_dpms,
  1379. .save = intel_sdvo_save,
  1380. .restore = intel_sdvo_restore,
  1381. .detect = intel_sdvo_detect,
  1382. .fill_modes = drm_helper_probe_single_connector_modes,
  1383. .destroy = intel_sdvo_destroy,
  1384. };
  1385. static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
  1386. .get_modes = intel_sdvo_get_modes,
  1387. .mode_valid = intel_sdvo_mode_valid,
  1388. .best_encoder = intel_best_encoder,
  1389. };
  1390. static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
  1391. {
  1392. drm_encoder_cleanup(encoder);
  1393. }
  1394. static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
  1395. .destroy = intel_sdvo_enc_destroy,
  1396. };
  1397. /**
  1398. * Choose the appropriate DDC bus for control bus switch command for this
  1399. * SDVO output based on the controlled output.
  1400. *
  1401. * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
  1402. * outputs, then LVDS outputs.
  1403. */
  1404. static void
  1405. intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
  1406. {
  1407. uint16_t mask = 0;
  1408. unsigned int num_bits;
  1409. /* Make a mask of outputs less than or equal to our own priority in the
  1410. * list.
  1411. */
  1412. switch (dev_priv->controlled_output) {
  1413. case SDVO_OUTPUT_LVDS1:
  1414. mask |= SDVO_OUTPUT_LVDS1;
  1415. case SDVO_OUTPUT_LVDS0:
  1416. mask |= SDVO_OUTPUT_LVDS0;
  1417. case SDVO_OUTPUT_TMDS1:
  1418. mask |= SDVO_OUTPUT_TMDS1;
  1419. case SDVO_OUTPUT_TMDS0:
  1420. mask |= SDVO_OUTPUT_TMDS0;
  1421. case SDVO_OUTPUT_RGB1:
  1422. mask |= SDVO_OUTPUT_RGB1;
  1423. case SDVO_OUTPUT_RGB0:
  1424. mask |= SDVO_OUTPUT_RGB0;
  1425. break;
  1426. }
  1427. /* Count bits to find what number we are in the priority list. */
  1428. mask &= dev_priv->caps.output_flags;
  1429. num_bits = hweight16(mask);
  1430. if (num_bits > 3) {
  1431. /* if more than 3 outputs, default to DDC bus 3 for now */
  1432. num_bits = 3;
  1433. }
  1434. /* Corresponds to SDVO_CONTROL_BUS_DDCx */
  1435. dev_priv->ddc_bus = 1 << num_bits;
  1436. }
  1437. static bool
  1438. intel_sdvo_get_digital_encoding_mode(struct intel_output *output)
  1439. {
  1440. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  1441. uint8_t status;
  1442. intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
  1443. intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
  1444. status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
  1445. if (status != SDVO_CMD_STATUS_SUCCESS)
  1446. return false;
  1447. return true;
  1448. }
  1449. static struct intel_output *
  1450. intel_sdvo_chan_to_intel_output(struct intel_i2c_chan *chan)
  1451. {
  1452. struct drm_device *dev = chan->drm_dev;
  1453. struct drm_connector *connector;
  1454. struct intel_output *intel_output = NULL;
  1455. list_for_each_entry(connector,
  1456. &dev->mode_config.connector_list, head) {
  1457. if (to_intel_output(connector)->ddc_bus == &chan->adapter) {
  1458. intel_output = to_intel_output(connector);
  1459. break;
  1460. }
  1461. }
  1462. return intel_output;
  1463. }
  1464. static int intel_sdvo_master_xfer(struct i2c_adapter *i2c_adap,
  1465. struct i2c_msg msgs[], int num)
  1466. {
  1467. struct intel_output *intel_output;
  1468. struct intel_sdvo_priv *sdvo_priv;
  1469. struct i2c_algo_bit_data *algo_data;
  1470. const struct i2c_algorithm *algo;
  1471. algo_data = (struct i2c_algo_bit_data *)i2c_adap->algo_data;
  1472. intel_output =
  1473. intel_sdvo_chan_to_intel_output(
  1474. (struct intel_i2c_chan *)(algo_data->data));
  1475. if (intel_output == NULL)
  1476. return -EINVAL;
  1477. sdvo_priv = intel_output->dev_priv;
  1478. algo = intel_output->i2c_bus->algo;
  1479. intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
  1480. return algo->master_xfer(i2c_adap, msgs, num);
  1481. }
  1482. static struct i2c_algorithm intel_sdvo_i2c_bit_algo = {
  1483. .master_xfer = intel_sdvo_master_xfer,
  1484. };
  1485. static u8
  1486. intel_sdvo_get_slave_addr(struct drm_device *dev, int output_device)
  1487. {
  1488. struct drm_i915_private *dev_priv = dev->dev_private;
  1489. struct sdvo_device_mapping *my_mapping, *other_mapping;
  1490. if (output_device == SDVOB) {
  1491. my_mapping = &dev_priv->sdvo_mappings[0];
  1492. other_mapping = &dev_priv->sdvo_mappings[1];
  1493. } else {
  1494. my_mapping = &dev_priv->sdvo_mappings[1];
  1495. other_mapping = &dev_priv->sdvo_mappings[0];
  1496. }
  1497. /* If the BIOS described our SDVO device, take advantage of it. */
  1498. if (my_mapping->slave_addr)
  1499. return my_mapping->slave_addr;
  1500. /* If the BIOS only described a different SDVO device, use the
  1501. * address that it isn't using.
  1502. */
  1503. if (other_mapping->slave_addr) {
  1504. if (other_mapping->slave_addr == 0x70)
  1505. return 0x72;
  1506. else
  1507. return 0x70;
  1508. }
  1509. /* No SDVO device info is found for another DVO port,
  1510. * so use mapping assumption we had before BIOS parsing.
  1511. */
  1512. if (output_device == SDVOB)
  1513. return 0x70;
  1514. else
  1515. return 0x72;
  1516. }
  1517. bool intel_sdvo_init(struct drm_device *dev, int output_device)
  1518. {
  1519. struct drm_connector *connector;
  1520. struct intel_output *intel_output;
  1521. struct intel_sdvo_priv *sdvo_priv;
  1522. struct i2c_adapter *i2cbus = NULL;
  1523. struct i2c_adapter *ddcbus = NULL;
  1524. int connector_type;
  1525. u8 ch[0x40];
  1526. int i;
  1527. int encoder_type;
  1528. u8 slave_addr;
  1529. intel_output = kcalloc(sizeof(struct intel_output)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
  1530. if (!intel_output) {
  1531. return false;
  1532. }
  1533. sdvo_priv = (struct intel_sdvo_priv *)(intel_output + 1);
  1534. intel_output->type = INTEL_OUTPUT_SDVO;
  1535. /* setup the DDC bus. */
  1536. if (output_device == SDVOB) {
  1537. i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
  1538. slave_addr = 0x38;
  1539. } else {
  1540. i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
  1541. slave_addr = 0x39;
  1542. }
  1543. if (!i2cbus)
  1544. goto err_inteloutput;
  1545. slave_addr = intel_sdvo_get_slave_addr(dev, output_device);
  1546. sdvo_priv->i2c_bus = i2cbus;
  1547. sdvo_priv->slave_addr = slave_addr;
  1548. sdvo_priv->output_device = output_device;
  1549. intel_output->i2c_bus = sdvo_priv->i2c_bus;
  1550. intel_output->dev_priv = sdvo_priv;
  1551. /* Read the regs to test if we can talk to the device */
  1552. for (i = 0; i < 0x40; i++) {
  1553. if (!intel_sdvo_read_byte(intel_output, i, &ch[i])) {
  1554. DRM_DEBUG_KMS(I915_SDVO,
  1555. "No SDVO device found on SDVO%c\n",
  1556. output_device == SDVOB ? 'B' : 'C');
  1557. goto err_i2c;
  1558. }
  1559. }
  1560. /* setup the DDC bus. */
  1561. if (output_device == SDVOB)
  1562. ddcbus = intel_i2c_create(dev, GPIOE, "SDVOB DDC BUS");
  1563. else
  1564. ddcbus = intel_i2c_create(dev, GPIOE, "SDVOC DDC BUS");
  1565. if (ddcbus == NULL)
  1566. goto err_i2c;
  1567. intel_sdvo_i2c_bit_algo.functionality =
  1568. intel_output->i2c_bus->algo->functionality;
  1569. ddcbus->algo = &intel_sdvo_i2c_bit_algo;
  1570. intel_output->ddc_bus = ddcbus;
  1571. /* In defaut case sdvo lvds is false */
  1572. sdvo_priv->is_lvds = false;
  1573. intel_sdvo_get_capabilities(intel_output, &sdvo_priv->caps);
  1574. if (sdvo_priv->caps.output_flags &
  1575. (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
  1576. if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
  1577. sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
  1578. else
  1579. sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
  1580. encoder_type = DRM_MODE_ENCODER_TMDS;
  1581. connector_type = DRM_MODE_CONNECTOR_DVID;
  1582. if (intel_sdvo_get_supp_encode(intel_output,
  1583. &sdvo_priv->encode) &&
  1584. intel_sdvo_get_digital_encoding_mode(intel_output) &&
  1585. sdvo_priv->is_hdmi) {
  1586. /* enable hdmi encoding mode if supported */
  1587. intel_sdvo_set_encode(intel_output, SDVO_ENCODE_HDMI);
  1588. intel_sdvo_set_colorimetry(intel_output,
  1589. SDVO_COLORIMETRY_RGB256);
  1590. connector_type = DRM_MODE_CONNECTOR_HDMIA;
  1591. }
  1592. }
  1593. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_SVID0)
  1594. {
  1595. sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
  1596. encoder_type = DRM_MODE_ENCODER_TVDAC;
  1597. connector_type = DRM_MODE_CONNECTOR_SVIDEO;
  1598. sdvo_priv->is_tv = true;
  1599. intel_output->needs_tv_clock = true;
  1600. }
  1601. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB0)
  1602. {
  1603. sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
  1604. encoder_type = DRM_MODE_ENCODER_DAC;
  1605. connector_type = DRM_MODE_CONNECTOR_VGA;
  1606. }
  1607. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB1)
  1608. {
  1609. sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
  1610. encoder_type = DRM_MODE_ENCODER_DAC;
  1611. connector_type = DRM_MODE_CONNECTOR_VGA;
  1612. }
  1613. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS0)
  1614. {
  1615. sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
  1616. encoder_type = DRM_MODE_ENCODER_LVDS;
  1617. connector_type = DRM_MODE_CONNECTOR_LVDS;
  1618. sdvo_priv->is_lvds = true;
  1619. }
  1620. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS1)
  1621. {
  1622. sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
  1623. encoder_type = DRM_MODE_ENCODER_LVDS;
  1624. connector_type = DRM_MODE_CONNECTOR_LVDS;
  1625. sdvo_priv->is_lvds = true;
  1626. }
  1627. else
  1628. {
  1629. unsigned char bytes[2];
  1630. sdvo_priv->controlled_output = 0;
  1631. memcpy (bytes, &sdvo_priv->caps.output_flags, 2);
  1632. DRM_DEBUG_KMS(I915_SDVO,
  1633. "%s: Unknown SDVO output type (0x%02x%02x)\n",
  1634. SDVO_NAME(sdvo_priv),
  1635. bytes[0], bytes[1]);
  1636. encoder_type = DRM_MODE_ENCODER_NONE;
  1637. connector_type = DRM_MODE_CONNECTOR_Unknown;
  1638. goto err_i2c;
  1639. }
  1640. connector = &intel_output->base;
  1641. drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
  1642. connector_type);
  1643. drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
  1644. connector->interlace_allowed = 0;
  1645. connector->doublescan_allowed = 0;
  1646. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  1647. drm_encoder_init(dev, &intel_output->enc, &intel_sdvo_enc_funcs, encoder_type);
  1648. drm_encoder_helper_add(&intel_output->enc, &intel_sdvo_helper_funcs);
  1649. drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
  1650. drm_sysfs_connector_add(connector);
  1651. intel_sdvo_select_ddc_bus(sdvo_priv);
  1652. /* Set the input timing to the screen. Assume always input 0. */
  1653. intel_sdvo_set_target_input(intel_output, true, false);
  1654. intel_sdvo_get_input_pixel_clock_range(intel_output,
  1655. &sdvo_priv->pixel_clock_min,
  1656. &sdvo_priv->pixel_clock_max);
  1657. DRM_DEBUG_KMS(I915_SDVO, "%s device VID/DID: %02X:%02X.%02X, "
  1658. "clock range %dMHz - %dMHz, "
  1659. "input 1: %c, input 2: %c, "
  1660. "output 1: %c, output 2: %c\n",
  1661. SDVO_NAME(sdvo_priv),
  1662. sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
  1663. sdvo_priv->caps.device_rev_id,
  1664. sdvo_priv->pixel_clock_min / 1000,
  1665. sdvo_priv->pixel_clock_max / 1000,
  1666. (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
  1667. (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
  1668. /* check currently supported outputs */
  1669. sdvo_priv->caps.output_flags &
  1670. (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
  1671. sdvo_priv->caps.output_flags &
  1672. (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
  1673. return true;
  1674. err_i2c:
  1675. if (ddcbus != NULL)
  1676. intel_i2c_destroy(intel_output->ddc_bus);
  1677. intel_i2c_destroy(intel_output->i2c_bus);
  1678. err_inteloutput:
  1679. kfree(intel_output);
  1680. return false;
  1681. }