intel_sdvo.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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. args.clock = clock;
  510. args.width = width;
  511. args.height = height;
  512. intel_sdvo_write_cmd(output, SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
  513. &args, sizeof(args));
  514. status = intel_sdvo_read_response(output, NULL, 0);
  515. if (status != SDVO_CMD_STATUS_SUCCESS)
  516. return false;
  517. return true;
  518. }
  519. static bool intel_sdvo_get_preferred_input_timing(struct intel_output *output,
  520. struct intel_sdvo_dtd *dtd)
  521. {
  522. bool status;
  523. intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
  524. NULL, 0);
  525. status = intel_sdvo_read_response(output, &dtd->part1,
  526. sizeof(dtd->part1));
  527. if (status != SDVO_CMD_STATUS_SUCCESS)
  528. return false;
  529. intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
  530. NULL, 0);
  531. status = intel_sdvo_read_response(output, &dtd->part2,
  532. sizeof(dtd->part2));
  533. if (status != SDVO_CMD_STATUS_SUCCESS)
  534. return false;
  535. return false;
  536. }
  537. static int intel_sdvo_get_clock_rate_mult(struct intel_output *intel_output)
  538. {
  539. u8 response, status;
  540. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
  541. status = intel_sdvo_read_response(intel_output, &response, 1);
  542. if (status != SDVO_CMD_STATUS_SUCCESS) {
  543. DRM_DEBUG("Couldn't get SDVO clock rate multiplier\n");
  544. return SDVO_CLOCK_RATE_MULT_1X;
  545. } else {
  546. DRM_DEBUG("Current clock rate multiplier: %d\n", response);
  547. }
  548. return response;
  549. }
  550. static bool intel_sdvo_set_clock_rate_mult(struct intel_output *intel_output, u8 val)
  551. {
  552. u8 status;
  553. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
  554. status = intel_sdvo_read_response(intel_output, NULL, 0);
  555. if (status != SDVO_CMD_STATUS_SUCCESS)
  556. return false;
  557. return true;
  558. }
  559. static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
  560. struct drm_display_mode *mode)
  561. {
  562. uint16_t width, height;
  563. uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
  564. uint16_t h_sync_offset, v_sync_offset;
  565. width = mode->crtc_hdisplay;
  566. height = mode->crtc_vdisplay;
  567. /* do some mode translations */
  568. h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
  569. h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
  570. v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
  571. v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
  572. h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
  573. v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
  574. dtd->part1.clock = mode->clock / 10;
  575. dtd->part1.h_active = width & 0xff;
  576. dtd->part1.h_blank = h_blank_len & 0xff;
  577. dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
  578. ((h_blank_len >> 8) & 0xf);
  579. dtd->part1.v_active = height & 0xff;
  580. dtd->part1.v_blank = v_blank_len & 0xff;
  581. dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
  582. ((v_blank_len >> 8) & 0xf);
  583. dtd->part2.h_sync_off = h_sync_offset;
  584. dtd->part2.h_sync_width = h_sync_len & 0xff;
  585. dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
  586. (v_sync_len & 0xf);
  587. dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
  588. ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
  589. ((v_sync_len & 0x30) >> 4);
  590. dtd->part2.dtd_flags = 0x18;
  591. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  592. dtd->part2.dtd_flags |= 0x2;
  593. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  594. dtd->part2.dtd_flags |= 0x4;
  595. dtd->part2.sdvo_flags = 0;
  596. dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
  597. dtd->part2.reserved = 0;
  598. }
  599. static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
  600. struct intel_sdvo_dtd *dtd)
  601. {
  602. uint16_t width, height;
  603. uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
  604. uint16_t h_sync_offset, v_sync_offset;
  605. width = mode->crtc_hdisplay;
  606. height = mode->crtc_vdisplay;
  607. /* do some mode translations */
  608. h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
  609. h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
  610. v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
  611. v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
  612. h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
  613. v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
  614. mode->hdisplay = dtd->part1.h_active;
  615. mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
  616. mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
  617. mode->hsync_start += (dtd->part2.sync_off_width_high & 0xa0) << 2;
  618. mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
  619. mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
  620. mode->htotal = mode->hdisplay + dtd->part1.h_blank;
  621. mode->htotal += (dtd->part1.h_high & 0xf) << 8;
  622. mode->vdisplay = dtd->part1.v_active;
  623. mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
  624. mode->vsync_start = mode->vdisplay;
  625. mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
  626. mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0a) << 2;
  627. mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
  628. mode->vsync_end = mode->vsync_start +
  629. (dtd->part2.v_sync_off_width & 0xf);
  630. mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
  631. mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
  632. mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
  633. mode->clock = dtd->part1.clock * 10;
  634. mode->flags &= (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
  635. if (dtd->part2.dtd_flags & 0x2)
  636. mode->flags |= DRM_MODE_FLAG_PHSYNC;
  637. if (dtd->part2.dtd_flags & 0x4)
  638. mode->flags |= DRM_MODE_FLAG_PVSYNC;
  639. }
  640. static bool intel_sdvo_get_supp_encode(struct intel_output *output,
  641. struct intel_sdvo_encode *encode)
  642. {
  643. uint8_t status;
  644. intel_sdvo_write_cmd(output, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
  645. status = intel_sdvo_read_response(output, encode, sizeof(*encode));
  646. if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
  647. memset(encode, 0, sizeof(*encode));
  648. return false;
  649. }
  650. return true;
  651. }
  652. static bool intel_sdvo_set_encode(struct intel_output *output, uint8_t mode)
  653. {
  654. uint8_t status;
  655. intel_sdvo_write_cmd(output, SDVO_CMD_SET_ENCODE, &mode, 1);
  656. status = intel_sdvo_read_response(output, NULL, 0);
  657. return (status == SDVO_CMD_STATUS_SUCCESS);
  658. }
  659. static bool intel_sdvo_set_colorimetry(struct intel_output *output,
  660. uint8_t mode)
  661. {
  662. uint8_t status;
  663. intel_sdvo_write_cmd(output, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
  664. status = intel_sdvo_read_response(output, NULL, 0);
  665. return (status == SDVO_CMD_STATUS_SUCCESS);
  666. }
  667. #if 0
  668. static void intel_sdvo_dump_hdmi_buf(struct intel_output *output)
  669. {
  670. int i, j;
  671. uint8_t set_buf_index[2];
  672. uint8_t av_split;
  673. uint8_t buf_size;
  674. uint8_t buf[48];
  675. uint8_t *pos;
  676. intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
  677. intel_sdvo_read_response(output, &av_split, 1);
  678. for (i = 0; i <= av_split; i++) {
  679. set_buf_index[0] = i; set_buf_index[1] = 0;
  680. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX,
  681. set_buf_index, 2);
  682. intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
  683. intel_sdvo_read_response(output, &buf_size, 1);
  684. pos = buf;
  685. for (j = 0; j <= buf_size; j += 8) {
  686. intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_DATA,
  687. NULL, 0);
  688. intel_sdvo_read_response(output, pos, 8);
  689. pos += 8;
  690. }
  691. }
  692. }
  693. #endif
  694. static void intel_sdvo_set_hdmi_buf(struct intel_output *output, int index,
  695. uint8_t *data, int8_t size, uint8_t tx_rate)
  696. {
  697. uint8_t set_buf_index[2];
  698. set_buf_index[0] = index;
  699. set_buf_index[1] = 0;
  700. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX, set_buf_index, 2);
  701. for (; size > 0; size -= 8) {
  702. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_DATA, data, 8);
  703. data += 8;
  704. }
  705. intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
  706. }
  707. static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
  708. {
  709. uint8_t csum = 0;
  710. int i;
  711. for (i = 0; i < size; i++)
  712. csum += data[i];
  713. return 0x100 - csum;
  714. }
  715. #define DIP_TYPE_AVI 0x82
  716. #define DIP_VERSION_AVI 0x2
  717. #define DIP_LEN_AVI 13
  718. struct dip_infoframe {
  719. uint8_t type;
  720. uint8_t version;
  721. uint8_t len;
  722. uint8_t checksum;
  723. union {
  724. struct {
  725. /* Packet Byte #1 */
  726. uint8_t S:2;
  727. uint8_t B:2;
  728. uint8_t A:1;
  729. uint8_t Y:2;
  730. uint8_t rsvd1:1;
  731. /* Packet Byte #2 */
  732. uint8_t R:4;
  733. uint8_t M:2;
  734. uint8_t C:2;
  735. /* Packet Byte #3 */
  736. uint8_t SC:2;
  737. uint8_t Q:2;
  738. uint8_t EC:3;
  739. uint8_t ITC:1;
  740. /* Packet Byte #4 */
  741. uint8_t VIC:7;
  742. uint8_t rsvd2:1;
  743. /* Packet Byte #5 */
  744. uint8_t PR:4;
  745. uint8_t rsvd3:4;
  746. /* Packet Byte #6~13 */
  747. uint16_t top_bar_end;
  748. uint16_t bottom_bar_start;
  749. uint16_t left_bar_end;
  750. uint16_t right_bar_start;
  751. } avi;
  752. struct {
  753. /* Packet Byte #1 */
  754. uint8_t channel_count:3;
  755. uint8_t rsvd1:1;
  756. uint8_t coding_type:4;
  757. /* Packet Byte #2 */
  758. uint8_t sample_size:2; /* SS0, SS1 */
  759. uint8_t sample_frequency:3;
  760. uint8_t rsvd2:3;
  761. /* Packet Byte #3 */
  762. uint8_t coding_type_private:5;
  763. uint8_t rsvd3:3;
  764. /* Packet Byte #4 */
  765. uint8_t channel_allocation;
  766. /* Packet Byte #5 */
  767. uint8_t rsvd4:3;
  768. uint8_t level_shift:4;
  769. uint8_t downmix_inhibit:1;
  770. } audio;
  771. uint8_t payload[28];
  772. } __attribute__ ((packed)) u;
  773. } __attribute__((packed));
  774. static void intel_sdvo_set_avi_infoframe(struct intel_output *output,
  775. struct drm_display_mode * mode)
  776. {
  777. struct dip_infoframe avi_if = {
  778. .type = DIP_TYPE_AVI,
  779. .version = DIP_VERSION_AVI,
  780. .len = DIP_LEN_AVI,
  781. };
  782. avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
  783. 4 + avi_if.len);
  784. intel_sdvo_set_hdmi_buf(output, 1, (uint8_t *)&avi_if, 4 + avi_if.len,
  785. SDVO_HBUF_TX_VSYNC);
  786. }
  787. static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
  788. struct drm_display_mode *mode,
  789. struct drm_display_mode *adjusted_mode)
  790. {
  791. struct intel_output *output = enc_to_intel_output(encoder);
  792. struct intel_sdvo_priv *dev_priv = output->dev_priv;
  793. if (!dev_priv->is_tv) {
  794. /* Make the CRTC code factor in the SDVO pixel multiplier. The
  795. * SDVO device will be told of the multiplier during mode_set.
  796. */
  797. adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
  798. } else {
  799. struct intel_sdvo_dtd output_dtd;
  800. bool success;
  801. /* We need to construct preferred input timings based on our
  802. * output timings. To do that, we have to set the output
  803. * timings, even though this isn't really the right place in
  804. * the sequence to do it. Oh well.
  805. */
  806. /* Set output timings */
  807. intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  808. intel_sdvo_set_target_output(output,
  809. dev_priv->controlled_output);
  810. intel_sdvo_set_output_timing(output, &output_dtd);
  811. /* Set the input timing to the screen. Assume always input 0. */
  812. intel_sdvo_set_target_input(output, true, false);
  813. success = intel_sdvo_create_preferred_input_timing(output,
  814. mode->clock / 10,
  815. mode->hdisplay,
  816. mode->vdisplay);
  817. if (success) {
  818. struct intel_sdvo_dtd input_dtd;
  819. intel_sdvo_get_preferred_input_timing(output,
  820. &input_dtd);
  821. intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
  822. } else {
  823. return false;
  824. }
  825. }
  826. return true;
  827. }
  828. static void intel_sdvo_mode_set(struct drm_encoder *encoder,
  829. struct drm_display_mode *mode,
  830. struct drm_display_mode *adjusted_mode)
  831. {
  832. struct drm_device *dev = encoder->dev;
  833. struct drm_i915_private *dev_priv = dev->dev_private;
  834. struct drm_crtc *crtc = encoder->crtc;
  835. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  836. struct intel_output *output = enc_to_intel_output(encoder);
  837. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  838. u32 sdvox = 0;
  839. int sdvo_pixel_multiply;
  840. struct intel_sdvo_in_out_map in_out;
  841. struct intel_sdvo_dtd input_dtd;
  842. u8 status;
  843. if (!mode)
  844. return;
  845. /* First, set the input mapping for the first input to our controlled
  846. * output. This is only correct if we're a single-input device, in
  847. * which case the first input is the output from the appropriate SDVO
  848. * channel on the motherboard. In a two-input device, the first input
  849. * will be SDVOB and the second SDVOC.
  850. */
  851. in_out.in0 = sdvo_priv->controlled_output;
  852. in_out.in1 = 0;
  853. intel_sdvo_write_cmd(output, SDVO_CMD_SET_IN_OUT_MAP,
  854. &in_out, sizeof(in_out));
  855. status = intel_sdvo_read_response(output, NULL, 0);
  856. if (sdvo_priv->is_hdmi) {
  857. intel_sdvo_set_avi_infoframe(output, mode);
  858. sdvox |= SDVO_AUDIO_ENABLE;
  859. }
  860. intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
  861. /* If it's a TV, we already set the output timing in mode_fixup.
  862. * Otherwise, the output timing is equal to the input timing.
  863. */
  864. if (!sdvo_priv->is_tv) {
  865. /* Set the output timing to the screen */
  866. intel_sdvo_set_target_output(output,
  867. sdvo_priv->controlled_output);
  868. intel_sdvo_set_output_timing(output, &input_dtd);
  869. }
  870. /* Set the input timing to the screen. Assume always input 0. */
  871. intel_sdvo_set_target_input(output, true, false);
  872. /* We would like to use intel_sdvo_create_preferred_input_timing() to
  873. * provide the device with a timing it can support, if it supports that
  874. * feature. However, presumably we would need to adjust the CRTC to
  875. * output the preferred timing, and we don't support that currently.
  876. */
  877. #if 0
  878. success = intel_sdvo_create_preferred_input_timing(output, clock,
  879. width, height);
  880. if (success) {
  881. struct intel_sdvo_dtd *input_dtd;
  882. intel_sdvo_get_preferred_input_timing(output, &input_dtd);
  883. intel_sdvo_set_input_timing(output, &input_dtd);
  884. }
  885. #else
  886. intel_sdvo_set_input_timing(output, &input_dtd);
  887. #endif
  888. switch (intel_sdvo_get_pixel_multiplier(mode)) {
  889. case 1:
  890. intel_sdvo_set_clock_rate_mult(output,
  891. SDVO_CLOCK_RATE_MULT_1X);
  892. break;
  893. case 2:
  894. intel_sdvo_set_clock_rate_mult(output,
  895. SDVO_CLOCK_RATE_MULT_2X);
  896. break;
  897. case 4:
  898. intel_sdvo_set_clock_rate_mult(output,
  899. SDVO_CLOCK_RATE_MULT_4X);
  900. break;
  901. }
  902. /* Set the SDVO control regs. */
  903. if (IS_I965G(dev)) {
  904. sdvox |= SDVO_BORDER_ENABLE |
  905. SDVO_VSYNC_ACTIVE_HIGH |
  906. SDVO_HSYNC_ACTIVE_HIGH;
  907. } else {
  908. sdvox |= I915_READ(sdvo_priv->output_device);
  909. switch (sdvo_priv->output_device) {
  910. case SDVOB:
  911. sdvox &= SDVOB_PRESERVE_MASK;
  912. break;
  913. case SDVOC:
  914. sdvox &= SDVOC_PRESERVE_MASK;
  915. break;
  916. }
  917. sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
  918. }
  919. if (intel_crtc->pipe == 1)
  920. sdvox |= SDVO_PIPE_B_SELECT;
  921. sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
  922. if (IS_I965G(dev)) {
  923. /* done in crtc_mode_set as the dpll_md reg must be written early */
  924. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  925. /* done in crtc_mode_set as it lives inside the dpll register */
  926. } else {
  927. sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
  928. }
  929. intel_sdvo_write_sdvox(output, sdvox);
  930. }
  931. static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
  932. {
  933. struct drm_device *dev = encoder->dev;
  934. struct drm_i915_private *dev_priv = dev->dev_private;
  935. struct intel_output *intel_output = enc_to_intel_output(encoder);
  936. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  937. u32 temp;
  938. if (mode != DRM_MODE_DPMS_ON) {
  939. intel_sdvo_set_active_outputs(intel_output, 0);
  940. if (0)
  941. intel_sdvo_set_encoder_power_state(intel_output, mode);
  942. if (mode == DRM_MODE_DPMS_OFF) {
  943. temp = I915_READ(sdvo_priv->output_device);
  944. if ((temp & SDVO_ENABLE) != 0) {
  945. intel_sdvo_write_sdvox(intel_output, temp & ~SDVO_ENABLE);
  946. }
  947. }
  948. } else {
  949. bool input1, input2;
  950. int i;
  951. u8 status;
  952. temp = I915_READ(sdvo_priv->output_device);
  953. if ((temp & SDVO_ENABLE) == 0)
  954. intel_sdvo_write_sdvox(intel_output, temp | SDVO_ENABLE);
  955. for (i = 0; i < 2; i++)
  956. intel_wait_for_vblank(dev);
  957. status = intel_sdvo_get_trained_inputs(intel_output, &input1,
  958. &input2);
  959. /* Warn if the device reported failure to sync.
  960. * A lot of SDVO devices fail to notify of sync, but it's
  961. * a given it the status is a success, we succeeded.
  962. */
  963. if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
  964. DRM_DEBUG("First %s output reported failure to sync\n",
  965. SDVO_NAME(sdvo_priv));
  966. }
  967. if (0)
  968. intel_sdvo_set_encoder_power_state(intel_output, mode);
  969. intel_sdvo_set_active_outputs(intel_output, sdvo_priv->controlled_output);
  970. }
  971. return;
  972. }
  973. static void intel_sdvo_save(struct drm_connector *connector)
  974. {
  975. struct drm_device *dev = connector->dev;
  976. struct drm_i915_private *dev_priv = dev->dev_private;
  977. struct intel_output *intel_output = to_intel_output(connector);
  978. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  979. int o;
  980. sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_output);
  981. intel_sdvo_get_active_outputs(intel_output, &sdvo_priv->save_active_outputs);
  982. if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
  983. intel_sdvo_set_target_input(intel_output, true, false);
  984. intel_sdvo_get_input_timing(intel_output,
  985. &sdvo_priv->save_input_dtd_1);
  986. }
  987. if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
  988. intel_sdvo_set_target_input(intel_output, false, true);
  989. intel_sdvo_get_input_timing(intel_output,
  990. &sdvo_priv->save_input_dtd_2);
  991. }
  992. for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
  993. {
  994. u16 this_output = (1 << o);
  995. if (sdvo_priv->caps.output_flags & this_output)
  996. {
  997. intel_sdvo_set_target_output(intel_output, this_output);
  998. intel_sdvo_get_output_timing(intel_output,
  999. &sdvo_priv->save_output_dtd[o]);
  1000. }
  1001. }
  1002. if (sdvo_priv->is_tv) {
  1003. /* XXX: Save TV format/enhancements. */
  1004. }
  1005. sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->output_device);
  1006. }
  1007. static void intel_sdvo_restore(struct drm_connector *connector)
  1008. {
  1009. struct drm_device *dev = connector->dev;
  1010. struct intel_output *intel_output = to_intel_output(connector);
  1011. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1012. int o;
  1013. int i;
  1014. bool input1, input2;
  1015. u8 status;
  1016. intel_sdvo_set_active_outputs(intel_output, 0);
  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. intel_sdvo_set_target_output(intel_output, this_output);
  1022. intel_sdvo_set_output_timing(intel_output, &sdvo_priv->save_output_dtd[o]);
  1023. }
  1024. }
  1025. if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
  1026. intel_sdvo_set_target_input(intel_output, true, false);
  1027. intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_1);
  1028. }
  1029. if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
  1030. intel_sdvo_set_target_input(intel_output, false, true);
  1031. intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_2);
  1032. }
  1033. intel_sdvo_set_clock_rate_mult(intel_output, sdvo_priv->save_sdvo_mult);
  1034. if (sdvo_priv->is_tv) {
  1035. /* XXX: Restore TV format/enhancements. */
  1036. }
  1037. intel_sdvo_write_sdvox(intel_output, sdvo_priv->save_SDVOX);
  1038. if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
  1039. {
  1040. for (i = 0; i < 2; i++)
  1041. intel_wait_for_vblank(dev);
  1042. status = intel_sdvo_get_trained_inputs(intel_output, &input1, &input2);
  1043. if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
  1044. DRM_DEBUG("First %s output reported failure to sync\n",
  1045. SDVO_NAME(sdvo_priv));
  1046. }
  1047. intel_sdvo_set_active_outputs(intel_output, sdvo_priv->save_active_outputs);
  1048. }
  1049. static int intel_sdvo_mode_valid(struct drm_connector *connector,
  1050. struct drm_display_mode *mode)
  1051. {
  1052. struct intel_output *intel_output = to_intel_output(connector);
  1053. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1054. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1055. return MODE_NO_DBLESCAN;
  1056. if (sdvo_priv->pixel_clock_min > mode->clock)
  1057. return MODE_CLOCK_LOW;
  1058. if (sdvo_priv->pixel_clock_max < mode->clock)
  1059. return MODE_CLOCK_HIGH;
  1060. return MODE_OK;
  1061. }
  1062. static bool intel_sdvo_get_capabilities(struct intel_output *intel_output, struct intel_sdvo_caps *caps)
  1063. {
  1064. u8 status;
  1065. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
  1066. status = intel_sdvo_read_response(intel_output, caps, sizeof(*caps));
  1067. if (status != SDVO_CMD_STATUS_SUCCESS)
  1068. return false;
  1069. return true;
  1070. }
  1071. struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
  1072. {
  1073. struct drm_connector *connector = NULL;
  1074. struct intel_output *iout = NULL;
  1075. struct intel_sdvo_priv *sdvo;
  1076. /* find the sdvo connector */
  1077. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1078. iout = to_intel_output(connector);
  1079. if (iout->type != INTEL_OUTPUT_SDVO)
  1080. continue;
  1081. sdvo = iout->dev_priv;
  1082. if (sdvo->output_device == SDVOB && sdvoB)
  1083. return connector;
  1084. if (sdvo->output_device == SDVOC && !sdvoB)
  1085. return connector;
  1086. }
  1087. return NULL;
  1088. }
  1089. int intel_sdvo_supports_hotplug(struct drm_connector *connector)
  1090. {
  1091. u8 response[2];
  1092. u8 status;
  1093. struct intel_output *intel_output;
  1094. DRM_DEBUG("\n");
  1095. if (!connector)
  1096. return 0;
  1097. intel_output = to_intel_output(connector);
  1098. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
  1099. status = intel_sdvo_read_response(intel_output, &response, 2);
  1100. if (response[0] !=0)
  1101. return 1;
  1102. return 0;
  1103. }
  1104. void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
  1105. {
  1106. u8 response[2];
  1107. u8 status;
  1108. struct intel_output *intel_output = to_intel_output(connector);
  1109. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
  1110. intel_sdvo_read_response(intel_output, &response, 2);
  1111. if (on) {
  1112. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
  1113. status = intel_sdvo_read_response(intel_output, &response, 2);
  1114. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
  1115. } else {
  1116. response[0] = 0;
  1117. response[1] = 0;
  1118. intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
  1119. }
  1120. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
  1121. intel_sdvo_read_response(intel_output, &response, 2);
  1122. }
  1123. static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
  1124. {
  1125. u8 response[2];
  1126. u8 status;
  1127. struct intel_output *intel_output = to_intel_output(connector);
  1128. intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
  1129. status = intel_sdvo_read_response(intel_output, &response, 2);
  1130. DRM_DEBUG("SDVO response %d %d\n", response[0], response[1]);
  1131. if (status != SDVO_CMD_STATUS_SUCCESS)
  1132. return connector_status_unknown;
  1133. if ((response[0] != 0) || (response[1] != 0))
  1134. return connector_status_connected;
  1135. else
  1136. return connector_status_disconnected;
  1137. }
  1138. static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
  1139. {
  1140. struct intel_output *intel_output = to_intel_output(connector);
  1141. struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
  1142. /* set the bus switch and get the modes */
  1143. intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
  1144. intel_ddc_get_modes(intel_output);
  1145. #if 0
  1146. struct drm_device *dev = encoder->dev;
  1147. struct drm_i915_private *dev_priv = dev->dev_private;
  1148. /* Mac mini hack. On this device, I get DDC through the analog, which
  1149. * load-detects as disconnected. I fail to DDC through the SDVO DDC,
  1150. * but it does load-detect as connected. So, just steal the DDC bits
  1151. * from analog when we fail at finding it the right way.
  1152. */
  1153. crt = xf86_config->output[0];
  1154. intel_output = crt->driver_private;
  1155. if (intel_output->type == I830_OUTPUT_ANALOG &&
  1156. crt->funcs->detect(crt) == XF86OutputStatusDisconnected) {
  1157. I830I2CInit(pScrn, &intel_output->pDDCBus, GPIOA, "CRTDDC_A");
  1158. edid_mon = xf86OutputGetEDID(crt, intel_output->pDDCBus);
  1159. xf86DestroyI2CBusRec(intel_output->pDDCBus, true, true);
  1160. }
  1161. if (edid_mon) {
  1162. xf86OutputSetEDID(output, edid_mon);
  1163. modes = xf86OutputGetEDIDModes(output);
  1164. }
  1165. #endif
  1166. }
  1167. /**
  1168. * This function checks the current TV format, and chooses a default if
  1169. * it hasn't been set.
  1170. */
  1171. static void
  1172. intel_sdvo_check_tv_format(struct intel_output *output)
  1173. {
  1174. struct intel_sdvo_priv *dev_priv = output->dev_priv;
  1175. struct intel_sdvo_tv_format format, unset;
  1176. uint8_t status;
  1177. intel_sdvo_write_cmd(output, SDVO_CMD_GET_TV_FORMAT, NULL, 0);
  1178. status = intel_sdvo_read_response(output, &format, sizeof(format));
  1179. if (status != SDVO_CMD_STATUS_SUCCESS)
  1180. return;
  1181. memset(&unset, 0, sizeof(unset));
  1182. if (memcmp(&format, &unset, sizeof(format))) {
  1183. DRM_DEBUG("%s: Choosing default TV format of NTSC-M\n",
  1184. SDVO_NAME(dev_priv));
  1185. format.ntsc_m = true;
  1186. intel_sdvo_write_cmd(output, SDVO_CMD_SET_TV_FORMAT, NULL, 0);
  1187. status = intel_sdvo_read_response(output, NULL, 0);
  1188. }
  1189. }
  1190. /*
  1191. * Set of SDVO TV modes.
  1192. * Note! This is in reply order (see loop in get_tv_modes).
  1193. * XXX: all 60Hz refresh?
  1194. */
  1195. struct drm_display_mode sdvo_tv_modes[] = {
  1196. { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815680, 321, 384, 416,
  1197. 200, 0, 232, 201, 233, 4196112, 0,
  1198. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1199. { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814080, 321, 384, 416,
  1200. 240, 0, 272, 241, 273, 4196112, 0,
  1201. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1202. { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910080, 401, 464, 496,
  1203. 300, 0, 332, 301, 333, 4196112, 0,
  1204. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1205. { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913280, 641, 704, 736,
  1206. 350, 0, 382, 351, 383, 4196112, 0,
  1207. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1208. { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121280, 641, 704, 736,
  1209. 400, 0, 432, 401, 433, 4196112, 0,
  1210. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1211. { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121280, 641, 704, 736,
  1212. 400, 0, 432, 401, 433, 4196112, 0,
  1213. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1214. { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624000, 705, 768, 800,
  1215. 480, 0, 512, 481, 513, 4196112, 0,
  1216. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1217. { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232000, 705, 768, 800,
  1218. 576, 0, 608, 577, 609, 4196112, 0,
  1219. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1220. { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751680, 721, 784, 816,
  1221. 350, 0, 382, 351, 383, 4196112, 0,
  1222. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1223. { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199680, 721, 784, 816,
  1224. 400, 0, 432, 401, 433, 4196112, 0,
  1225. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1226. { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116480, 721, 784, 816,
  1227. 480, 0, 512, 481, 513, 4196112, 0,
  1228. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1229. { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054080, 721, 784, 816,
  1230. 540, 0, 572, 541, 573, 4196112, 0,
  1231. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1232. { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816640, 721, 784, 816,
  1233. 576, 0, 608, 577, 609, 4196112, 0,
  1234. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1235. { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570560, 769, 832, 864,
  1236. 576, 0, 608, 577, 609, 4196112, 0,
  1237. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1238. { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030080, 801, 864, 896,
  1239. 600, 0, 632, 601, 633, 4196112, 0,
  1240. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1241. { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581760, 833, 896, 928,
  1242. 624, 0, 656, 625, 657, 4196112, 0,
  1243. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1244. { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707040, 921, 984, 1016,
  1245. 766, 0, 798, 767, 799, 4196112, 0,
  1246. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1247. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827200, 1025, 1088, 1120,
  1248. 768, 0, 800, 769, 801, 4196112, 0,
  1249. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1250. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265920, 1281, 1344, 1376,
  1251. 1024, 0, 1056, 1025, 1057, 4196112, 0,
  1252. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1253. };
  1254. static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
  1255. {
  1256. struct intel_output *output = to_intel_output(connector);
  1257. uint32_t reply = 0;
  1258. uint8_t status;
  1259. int i = 0;
  1260. intel_sdvo_check_tv_format(output);
  1261. /* Read the list of supported input resolutions for the selected TV
  1262. * format.
  1263. */
  1264. intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
  1265. NULL, 0);
  1266. status = intel_sdvo_read_response(output, &reply, 3);
  1267. if (status != SDVO_CMD_STATUS_SUCCESS)
  1268. return;
  1269. for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
  1270. if (reply & (1 << i))
  1271. drm_mode_probed_add(connector, &sdvo_tv_modes[i]);
  1272. }
  1273. static int intel_sdvo_get_modes(struct drm_connector *connector)
  1274. {
  1275. struct intel_output *output = to_intel_output(connector);
  1276. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  1277. if (sdvo_priv->is_tv)
  1278. intel_sdvo_get_tv_modes(connector);
  1279. else
  1280. intel_sdvo_get_ddc_modes(connector);
  1281. if (list_empty(&connector->probed_modes))
  1282. return 0;
  1283. return 1;
  1284. }
  1285. static void intel_sdvo_destroy(struct drm_connector *connector)
  1286. {
  1287. struct intel_output *intel_output = to_intel_output(connector);
  1288. if (intel_output->i2c_bus)
  1289. intel_i2c_destroy(intel_output->i2c_bus);
  1290. drm_sysfs_connector_remove(connector);
  1291. drm_connector_cleanup(connector);
  1292. kfree(intel_output);
  1293. }
  1294. static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
  1295. .dpms = intel_sdvo_dpms,
  1296. .mode_fixup = intel_sdvo_mode_fixup,
  1297. .prepare = intel_encoder_prepare,
  1298. .mode_set = intel_sdvo_mode_set,
  1299. .commit = intel_encoder_commit,
  1300. };
  1301. static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
  1302. .save = intel_sdvo_save,
  1303. .restore = intel_sdvo_restore,
  1304. .detect = intel_sdvo_detect,
  1305. .fill_modes = drm_helper_probe_single_connector_modes,
  1306. .destroy = intel_sdvo_destroy,
  1307. };
  1308. static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
  1309. .get_modes = intel_sdvo_get_modes,
  1310. .mode_valid = intel_sdvo_mode_valid,
  1311. .best_encoder = intel_best_encoder,
  1312. };
  1313. static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
  1314. {
  1315. drm_encoder_cleanup(encoder);
  1316. }
  1317. static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
  1318. .destroy = intel_sdvo_enc_destroy,
  1319. };
  1320. /**
  1321. * Choose the appropriate DDC bus for control bus switch command for this
  1322. * SDVO output based on the controlled output.
  1323. *
  1324. * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
  1325. * outputs, then LVDS outputs.
  1326. */
  1327. static void
  1328. intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
  1329. {
  1330. uint16_t mask = 0;
  1331. unsigned int num_bits;
  1332. /* Make a mask of outputs less than or equal to our own priority in the
  1333. * list.
  1334. */
  1335. switch (dev_priv->controlled_output) {
  1336. case SDVO_OUTPUT_LVDS1:
  1337. mask |= SDVO_OUTPUT_LVDS1;
  1338. case SDVO_OUTPUT_LVDS0:
  1339. mask |= SDVO_OUTPUT_LVDS0;
  1340. case SDVO_OUTPUT_TMDS1:
  1341. mask |= SDVO_OUTPUT_TMDS1;
  1342. case SDVO_OUTPUT_TMDS0:
  1343. mask |= SDVO_OUTPUT_TMDS0;
  1344. case SDVO_OUTPUT_RGB1:
  1345. mask |= SDVO_OUTPUT_RGB1;
  1346. case SDVO_OUTPUT_RGB0:
  1347. mask |= SDVO_OUTPUT_RGB0;
  1348. break;
  1349. }
  1350. /* Count bits to find what number we are in the priority list. */
  1351. mask &= dev_priv->caps.output_flags;
  1352. num_bits = hweight16(mask);
  1353. if (num_bits > 3) {
  1354. /* if more than 3 outputs, default to DDC bus 3 for now */
  1355. num_bits = 3;
  1356. }
  1357. /* Corresponds to SDVO_CONTROL_BUS_DDCx */
  1358. dev_priv->ddc_bus = 1 << num_bits;
  1359. }
  1360. static bool
  1361. intel_sdvo_get_digital_encoding_mode(struct intel_output *output)
  1362. {
  1363. struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
  1364. uint8_t status;
  1365. intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
  1366. intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
  1367. status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
  1368. if (status != SDVO_CMD_STATUS_SUCCESS)
  1369. return false;
  1370. return true;
  1371. }
  1372. bool intel_sdvo_init(struct drm_device *dev, int output_device)
  1373. {
  1374. struct drm_connector *connector;
  1375. struct intel_output *intel_output;
  1376. struct intel_sdvo_priv *sdvo_priv;
  1377. struct intel_i2c_chan *i2cbus = NULL;
  1378. int connector_type;
  1379. u8 ch[0x40];
  1380. int i;
  1381. int encoder_type, output_id;
  1382. intel_output = kcalloc(sizeof(struct intel_output)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
  1383. if (!intel_output) {
  1384. return false;
  1385. }
  1386. connector = &intel_output->base;
  1387. drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
  1388. DRM_MODE_CONNECTOR_Unknown);
  1389. drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
  1390. sdvo_priv = (struct intel_sdvo_priv *)(intel_output + 1);
  1391. intel_output->type = INTEL_OUTPUT_SDVO;
  1392. connector->interlace_allowed = 0;
  1393. connector->doublescan_allowed = 0;
  1394. /* setup the DDC bus. */
  1395. if (output_device == SDVOB)
  1396. i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
  1397. else
  1398. i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
  1399. if (!i2cbus)
  1400. goto err_connector;
  1401. sdvo_priv->i2c_bus = i2cbus;
  1402. if (output_device == SDVOB) {
  1403. output_id = 1;
  1404. sdvo_priv->i2c_bus->slave_addr = 0x38;
  1405. } else {
  1406. output_id = 2;
  1407. sdvo_priv->i2c_bus->slave_addr = 0x39;
  1408. }
  1409. sdvo_priv->output_device = output_device;
  1410. intel_output->i2c_bus = i2cbus;
  1411. intel_output->dev_priv = sdvo_priv;
  1412. /* Read the regs to test if we can talk to the device */
  1413. for (i = 0; i < 0x40; i++) {
  1414. if (!intel_sdvo_read_byte(intel_output, i, &ch[i])) {
  1415. DRM_DEBUG("No SDVO device found on SDVO%c\n",
  1416. output_device == SDVOB ? 'B' : 'C');
  1417. goto err_i2c;
  1418. }
  1419. }
  1420. intel_sdvo_get_capabilities(intel_output, &sdvo_priv->caps);
  1421. if (sdvo_priv->caps.output_flags &
  1422. (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
  1423. if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
  1424. sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
  1425. else
  1426. sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
  1427. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  1428. encoder_type = DRM_MODE_ENCODER_TMDS;
  1429. connector_type = DRM_MODE_CONNECTOR_DVID;
  1430. if (intel_sdvo_get_supp_encode(intel_output,
  1431. &sdvo_priv->encode) &&
  1432. intel_sdvo_get_digital_encoding_mode(intel_output) &&
  1433. sdvo_priv->is_hdmi) {
  1434. /* enable hdmi encoding mode if supported */
  1435. intel_sdvo_set_encode(intel_output, SDVO_ENCODE_HDMI);
  1436. intel_sdvo_set_colorimetry(intel_output,
  1437. SDVO_COLORIMETRY_RGB256);
  1438. connector_type = DRM_MODE_CONNECTOR_HDMIA;
  1439. }
  1440. }
  1441. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_SVID0)
  1442. {
  1443. sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
  1444. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  1445. encoder_type = DRM_MODE_ENCODER_TVDAC;
  1446. connector_type = DRM_MODE_CONNECTOR_SVIDEO;
  1447. sdvo_priv->is_tv = true;
  1448. intel_output->needs_tv_clock = true;
  1449. }
  1450. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB0)
  1451. {
  1452. sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
  1453. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  1454. encoder_type = DRM_MODE_ENCODER_DAC;
  1455. connector_type = DRM_MODE_CONNECTOR_VGA;
  1456. }
  1457. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB1)
  1458. {
  1459. sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
  1460. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  1461. encoder_type = DRM_MODE_ENCODER_DAC;
  1462. connector_type = DRM_MODE_CONNECTOR_VGA;
  1463. }
  1464. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS0)
  1465. {
  1466. sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
  1467. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  1468. encoder_type = DRM_MODE_ENCODER_LVDS;
  1469. connector_type = DRM_MODE_CONNECTOR_LVDS;
  1470. }
  1471. else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS1)
  1472. {
  1473. sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
  1474. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  1475. encoder_type = DRM_MODE_ENCODER_LVDS;
  1476. connector_type = DRM_MODE_CONNECTOR_LVDS;
  1477. }
  1478. else
  1479. {
  1480. unsigned char bytes[2];
  1481. sdvo_priv->controlled_output = 0;
  1482. memcpy (bytes, &sdvo_priv->caps.output_flags, 2);
  1483. DRM_DEBUG("%s: Unknown SDVO output type (0x%02x%02x)\n",
  1484. SDVO_NAME(sdvo_priv),
  1485. bytes[0], bytes[1]);
  1486. encoder_type = DRM_MODE_ENCODER_NONE;
  1487. connector_type = DRM_MODE_CONNECTOR_Unknown;
  1488. goto err_i2c;
  1489. }
  1490. drm_encoder_init(dev, &intel_output->enc, &intel_sdvo_enc_funcs, encoder_type);
  1491. drm_encoder_helper_add(&intel_output->enc, &intel_sdvo_helper_funcs);
  1492. connector->connector_type = connector_type;
  1493. drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
  1494. drm_sysfs_connector_add(connector);
  1495. intel_sdvo_select_ddc_bus(sdvo_priv);
  1496. /* Set the input timing to the screen. Assume always input 0. */
  1497. intel_sdvo_set_target_input(intel_output, true, false);
  1498. intel_sdvo_get_input_pixel_clock_range(intel_output,
  1499. &sdvo_priv->pixel_clock_min,
  1500. &sdvo_priv->pixel_clock_max);
  1501. DRM_DEBUG("%s device VID/DID: %02X:%02X.%02X, "
  1502. "clock range %dMHz - %dMHz, "
  1503. "input 1: %c, input 2: %c, "
  1504. "output 1: %c, output 2: %c\n",
  1505. SDVO_NAME(sdvo_priv),
  1506. sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
  1507. sdvo_priv->caps.device_rev_id,
  1508. sdvo_priv->pixel_clock_min / 1000,
  1509. sdvo_priv->pixel_clock_max / 1000,
  1510. (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
  1511. (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
  1512. /* check currently supported outputs */
  1513. sdvo_priv->caps.output_flags &
  1514. (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
  1515. sdvo_priv->caps.output_flags &
  1516. (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
  1517. intel_output->ddc_bus = i2cbus;
  1518. return true;
  1519. err_i2c:
  1520. intel_i2c_destroy(intel_output->i2c_bus);
  1521. err_connector:
  1522. drm_connector_cleanup(connector);
  1523. kfree(intel_output);
  1524. return false;
  1525. }