intel_dvo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright 2006 Dave Airlie <airlied@linux.ie>
  3. * Copyright © 2006-2007 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Eric Anholt <eric@anholt.net>
  26. */
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include "drmP.h"
  30. #include "drm.h"
  31. #include "drm_crtc.h"
  32. #include "intel_drv.h"
  33. #include "i915_drm.h"
  34. #include "i915_drv.h"
  35. #include "dvo.h"
  36. #define SIL164_ADDR 0x38
  37. #define CH7xxx_ADDR 0x76
  38. #define TFP410_ADDR 0x38
  39. static struct intel_dvo_device intel_dvo_devices[] = {
  40. {
  41. .type = INTEL_DVO_CHIP_TMDS,
  42. .name = "sil164",
  43. .dvo_reg = DVOC,
  44. .slave_addr = SIL164_ADDR,
  45. .dev_ops = &sil164_ops,
  46. },
  47. {
  48. .type = INTEL_DVO_CHIP_TMDS,
  49. .name = "ch7xxx",
  50. .dvo_reg = DVOC,
  51. .slave_addr = CH7xxx_ADDR,
  52. .dev_ops = &ch7xxx_ops,
  53. },
  54. {
  55. .type = INTEL_DVO_CHIP_LVDS,
  56. .name = "ivch",
  57. .dvo_reg = DVOA,
  58. .slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
  59. .dev_ops = &ivch_ops,
  60. },
  61. {
  62. .type = INTEL_DVO_CHIP_TMDS,
  63. .name = "tfp410",
  64. .dvo_reg = DVOC,
  65. .slave_addr = TFP410_ADDR,
  66. .dev_ops = &tfp410_ops,
  67. },
  68. {
  69. .type = INTEL_DVO_CHIP_LVDS,
  70. .name = "ch7017",
  71. .dvo_reg = DVOC,
  72. .slave_addr = 0x75,
  73. .gpio = GPIOE,
  74. .dev_ops = &ch7017_ops,
  75. }
  76. };
  77. static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
  78. {
  79. struct drm_i915_private *dev_priv = encoder->dev->dev_private;
  80. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  81. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  82. u32 dvo_reg = dvo->dvo_reg;
  83. u32 temp = I915_READ(dvo_reg);
  84. if (mode == DRM_MODE_DPMS_ON) {
  85. I915_WRITE(dvo_reg, temp | DVO_ENABLE);
  86. I915_READ(dvo_reg);
  87. dvo->dev_ops->dpms(dvo, mode);
  88. } else {
  89. dvo->dev_ops->dpms(dvo, mode);
  90. I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
  91. I915_READ(dvo_reg);
  92. }
  93. }
  94. static void intel_dvo_save(struct drm_connector *connector)
  95. {
  96. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  97. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  98. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  99. /* Each output should probably just save the registers it touches,
  100. * but for now, use more overkill.
  101. */
  102. dev_priv->saveDVOA = I915_READ(DVOA);
  103. dev_priv->saveDVOB = I915_READ(DVOB);
  104. dev_priv->saveDVOC = I915_READ(DVOC);
  105. dvo->dev_ops->save(dvo);
  106. }
  107. static void intel_dvo_restore(struct drm_connector *connector)
  108. {
  109. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  110. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  111. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  112. dvo->dev_ops->restore(dvo);
  113. I915_WRITE(DVOA, dev_priv->saveDVOA);
  114. I915_WRITE(DVOB, dev_priv->saveDVOB);
  115. I915_WRITE(DVOC, dev_priv->saveDVOC);
  116. }
  117. static int intel_dvo_mode_valid(struct drm_connector *connector,
  118. struct drm_display_mode *mode)
  119. {
  120. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  121. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  122. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  123. return MODE_NO_DBLESCAN;
  124. /* XXX: Validate clock range */
  125. if (dvo->panel_fixed_mode) {
  126. if (mode->hdisplay > dvo->panel_fixed_mode->hdisplay)
  127. return MODE_PANEL;
  128. if (mode->vdisplay > dvo->panel_fixed_mode->vdisplay)
  129. return MODE_PANEL;
  130. }
  131. return dvo->dev_ops->mode_valid(dvo, mode);
  132. }
  133. static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
  134. struct drm_display_mode *mode,
  135. struct drm_display_mode *adjusted_mode)
  136. {
  137. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  138. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  139. /* If we have timings from the BIOS for the panel, put them in
  140. * to the adjusted mode. The CRTC will be set up for this mode,
  141. * with the panel scaling set up to source from the H/VDisplay
  142. * of the original mode.
  143. */
  144. if (dvo->panel_fixed_mode != NULL) {
  145. #define C(x) adjusted_mode->x = dvo->panel_fixed_mode->x
  146. C(hdisplay);
  147. C(hsync_start);
  148. C(hsync_end);
  149. C(htotal);
  150. C(vdisplay);
  151. C(vsync_start);
  152. C(vsync_end);
  153. C(vtotal);
  154. C(clock);
  155. drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
  156. #undef C
  157. }
  158. if (dvo->dev_ops->mode_fixup)
  159. return dvo->dev_ops->mode_fixup(dvo, mode, adjusted_mode);
  160. return true;
  161. }
  162. static void intel_dvo_mode_set(struct drm_encoder *encoder,
  163. struct drm_display_mode *mode,
  164. struct drm_display_mode *adjusted_mode)
  165. {
  166. struct drm_device *dev = encoder->dev;
  167. struct drm_i915_private *dev_priv = dev->dev_private;
  168. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  169. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  170. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  171. int pipe = intel_crtc->pipe;
  172. u32 dvo_val;
  173. u32 dvo_reg = dvo->dvo_reg, dvo_srcdim_reg;
  174. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  175. switch (dvo_reg) {
  176. case DVOA:
  177. default:
  178. dvo_srcdim_reg = DVOA_SRCDIM;
  179. break;
  180. case DVOB:
  181. dvo_srcdim_reg = DVOB_SRCDIM;
  182. break;
  183. case DVOC:
  184. dvo_srcdim_reg = DVOC_SRCDIM;
  185. break;
  186. }
  187. dvo->dev_ops->mode_set(dvo, mode, adjusted_mode);
  188. /* Save the data order, since I don't know what it should be set to. */
  189. dvo_val = I915_READ(dvo_reg) &
  190. (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
  191. dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
  192. DVO_BLANK_ACTIVE_HIGH;
  193. if (pipe == 1)
  194. dvo_val |= DVO_PIPE_B_SELECT;
  195. dvo_val |= DVO_PIPE_STALL;
  196. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  197. dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
  198. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  199. dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
  200. I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
  201. /*I915_WRITE(DVOB_SRCDIM,
  202. (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
  203. (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
  204. I915_WRITE(dvo_srcdim_reg,
  205. (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
  206. (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
  207. /*I915_WRITE(DVOB, dvo_val);*/
  208. I915_WRITE(dvo_reg, dvo_val);
  209. }
  210. /**
  211. * Detect the output connection on our DVO device.
  212. *
  213. * Unimplemented.
  214. */
  215. static enum drm_connector_status intel_dvo_detect(struct drm_connector *connector)
  216. {
  217. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  218. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  219. return dvo->dev_ops->detect(dvo);
  220. }
  221. static int intel_dvo_get_modes(struct drm_connector *connector)
  222. {
  223. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  224. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  225. /* We should probably have an i2c driver get_modes function for those
  226. * devices which will have a fixed set of modes determined by the chip
  227. * (TV-out, for example), but for now with just TMDS and LVDS,
  228. * that's not the case.
  229. */
  230. intel_ddc_get_modes(intel_encoder);
  231. if (!list_empty(&connector->probed_modes))
  232. return 1;
  233. if (dvo->panel_fixed_mode != NULL) {
  234. struct drm_display_mode *mode;
  235. mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode);
  236. if (mode) {
  237. drm_mode_probed_add(connector, mode);
  238. return 1;
  239. }
  240. }
  241. return 0;
  242. }
  243. static void intel_dvo_destroy (struct drm_connector *connector)
  244. {
  245. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  246. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  247. if (dvo) {
  248. if (dvo->dev_ops->destroy)
  249. dvo->dev_ops->destroy(dvo);
  250. if (dvo->panel_fixed_mode)
  251. kfree(dvo->panel_fixed_mode);
  252. /* no need, in i830_dvoices[] now */
  253. //kfree(dvo);
  254. }
  255. if (intel_encoder->i2c_bus)
  256. intel_i2c_destroy(intel_encoder->i2c_bus);
  257. if (intel_encoder->ddc_bus)
  258. intel_i2c_destroy(intel_encoder->ddc_bus);
  259. drm_sysfs_connector_remove(connector);
  260. drm_connector_cleanup(connector);
  261. kfree(intel_encoder);
  262. }
  263. #ifdef RANDR_GET_CRTC_INTERFACE
  264. static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector)
  265. {
  266. struct drm_device *dev = connector->dev;
  267. struct drm_i915_private *dev_priv = dev->dev_private;
  268. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  269. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  270. int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT);
  271. return intel_pipe_to_crtc(pScrn, pipe);
  272. }
  273. #endif
  274. static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
  275. .dpms = intel_dvo_dpms,
  276. .mode_fixup = intel_dvo_mode_fixup,
  277. .prepare = intel_encoder_prepare,
  278. .mode_set = intel_dvo_mode_set,
  279. .commit = intel_encoder_commit,
  280. };
  281. static const struct drm_connector_funcs intel_dvo_connector_funcs = {
  282. .dpms = drm_helper_connector_dpms,
  283. .save = intel_dvo_save,
  284. .restore = intel_dvo_restore,
  285. .detect = intel_dvo_detect,
  286. .destroy = intel_dvo_destroy,
  287. .fill_modes = drm_helper_probe_single_connector_modes,
  288. };
  289. static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
  290. .mode_valid = intel_dvo_mode_valid,
  291. .get_modes = intel_dvo_get_modes,
  292. .best_encoder = intel_best_encoder,
  293. };
  294. static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
  295. {
  296. drm_encoder_cleanup(encoder);
  297. }
  298. static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
  299. .destroy = intel_dvo_enc_destroy,
  300. };
  301. /**
  302. * Attempts to get a fixed panel timing for LVDS (currently only the i830).
  303. *
  304. * Other chips with DVO LVDS will need to extend this to deal with the LVDS
  305. * chip being on DVOB/C and having multiple pipes.
  306. */
  307. static struct drm_display_mode *
  308. intel_dvo_get_current_mode (struct drm_connector *connector)
  309. {
  310. struct drm_device *dev = connector->dev;
  311. struct drm_i915_private *dev_priv = dev->dev_private;
  312. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  313. struct intel_dvo_device *dvo = intel_encoder->dev_priv;
  314. uint32_t dvo_reg = dvo->dvo_reg;
  315. uint32_t dvo_val = I915_READ(dvo_reg);
  316. struct drm_display_mode *mode = NULL;
  317. /* If the DVO port is active, that'll be the LVDS, so we can pull out
  318. * its timings to get how the BIOS set up the panel.
  319. */
  320. if (dvo_val & DVO_ENABLE) {
  321. struct drm_crtc *crtc;
  322. int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
  323. crtc = intel_get_crtc_from_pipe(dev, pipe);
  324. if (crtc) {
  325. mode = intel_crtc_mode_get(dev, crtc);
  326. if (mode) {
  327. mode->type |= DRM_MODE_TYPE_PREFERRED;
  328. if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
  329. mode->flags |= DRM_MODE_FLAG_PHSYNC;
  330. if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
  331. mode->flags |= DRM_MODE_FLAG_PVSYNC;
  332. }
  333. }
  334. }
  335. return mode;
  336. }
  337. void intel_dvo_init(struct drm_device *dev)
  338. {
  339. struct intel_encoder *intel_encoder;
  340. struct intel_dvo_device *dvo;
  341. struct i2c_adapter *i2cbus = NULL;
  342. int ret = 0;
  343. int i;
  344. int encoder_type = DRM_MODE_ENCODER_NONE;
  345. intel_encoder = kzalloc (sizeof(struct intel_encoder), GFP_KERNEL);
  346. if (!intel_encoder)
  347. return;
  348. /* Set up the DDC bus */
  349. intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D");
  350. if (!intel_encoder->ddc_bus)
  351. goto free_intel;
  352. /* Now, try to find a controller */
  353. for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
  354. struct drm_connector *connector = &intel_encoder->base;
  355. int gpio;
  356. dvo = &intel_dvo_devices[i];
  357. /* Allow the I2C driver info to specify the GPIO to be used in
  358. * special cases, but otherwise default to what's defined
  359. * in the spec.
  360. */
  361. if (dvo->gpio != 0)
  362. gpio = dvo->gpio;
  363. else if (dvo->type == INTEL_DVO_CHIP_LVDS)
  364. gpio = GPIOB;
  365. else
  366. gpio = GPIOE;
  367. /* Set up the I2C bus necessary for the chip we're probing.
  368. * It appears that everything is on GPIOE except for panels
  369. * on i830 laptops, which are on GPIOB (DVOA).
  370. */
  371. if (i2cbus != NULL)
  372. intel_i2c_destroy(i2cbus);
  373. if (!(i2cbus = intel_i2c_create(dev, gpio,
  374. gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) {
  375. continue;
  376. }
  377. if (dvo->dev_ops!= NULL)
  378. ret = dvo->dev_ops->init(dvo, i2cbus);
  379. else
  380. ret = false;
  381. if (!ret)
  382. continue;
  383. intel_encoder->type = INTEL_OUTPUT_DVO;
  384. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  385. switch (dvo->type) {
  386. case INTEL_DVO_CHIP_TMDS:
  387. intel_encoder->clone_mask =
  388. (1 << INTEL_DVO_TMDS_CLONE_BIT) |
  389. (1 << INTEL_ANALOG_CLONE_BIT);
  390. drm_connector_init(dev, connector,
  391. &intel_dvo_connector_funcs,
  392. DRM_MODE_CONNECTOR_DVII);
  393. encoder_type = DRM_MODE_ENCODER_TMDS;
  394. break;
  395. case INTEL_DVO_CHIP_LVDS:
  396. intel_encoder->clone_mask =
  397. (1 << INTEL_DVO_LVDS_CLONE_BIT);
  398. drm_connector_init(dev, connector,
  399. &intel_dvo_connector_funcs,
  400. DRM_MODE_CONNECTOR_LVDS);
  401. encoder_type = DRM_MODE_ENCODER_LVDS;
  402. break;
  403. }
  404. drm_connector_helper_add(connector,
  405. &intel_dvo_connector_helper_funcs);
  406. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  407. connector->interlace_allowed = false;
  408. connector->doublescan_allowed = false;
  409. intel_encoder->dev_priv = dvo;
  410. intel_encoder->i2c_bus = i2cbus;
  411. drm_encoder_init(dev, &intel_encoder->enc,
  412. &intel_dvo_enc_funcs, encoder_type);
  413. drm_encoder_helper_add(&intel_encoder->enc,
  414. &intel_dvo_helper_funcs);
  415. drm_mode_connector_attach_encoder(&intel_encoder->base,
  416. &intel_encoder->enc);
  417. if (dvo->type == INTEL_DVO_CHIP_LVDS) {
  418. /* For our LVDS chipsets, we should hopefully be able
  419. * to dig the fixed panel mode out of the BIOS data.
  420. * However, it's in a different format from the BIOS
  421. * data on chipsets with integrated LVDS (stored in AIM
  422. * headers, likely), so for now, just get the current
  423. * mode being output through DVO.
  424. */
  425. dvo->panel_fixed_mode =
  426. intel_dvo_get_current_mode(connector);
  427. dvo->panel_wants_dither = true;
  428. }
  429. drm_sysfs_connector_add(connector);
  430. return;
  431. }
  432. intel_i2c_destroy(intel_encoder->ddc_bus);
  433. /* Didn't find a chip, so tear down. */
  434. if (i2cbus != NULL)
  435. intel_i2c_destroy(i2cbus);
  436. free_intel:
  437. kfree(intel_encoder);
  438. }