intel_sdvo.c 54 KB

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