intel_sdvo.c 55 KB

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