intel_dvo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 const 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 = GMBUS_PORT_DPB,
  74. .dev_ops = &ch7017_ops,
  75. }
  76. };
  77. struct intel_dvo {
  78. struct intel_encoder base;
  79. struct intel_dvo_device dev;
  80. struct drm_display_mode *panel_fixed_mode;
  81. bool panel_wants_dither;
  82. };
  83. static struct intel_dvo *enc_to_intel_dvo(struct drm_encoder *encoder)
  84. {
  85. return container_of(encoder, struct intel_dvo, base.base);
  86. }
  87. static struct intel_dvo *intel_attached_dvo(struct drm_connector *connector)
  88. {
  89. return container_of(intel_attached_encoder(connector),
  90. struct intel_dvo, base);
  91. }
  92. static void intel_dvo_dpms(struct drm_encoder *encoder, int mode)
  93. {
  94. struct drm_i915_private *dev_priv = encoder->dev->dev_private;
  95. struct intel_dvo *intel_dvo = enc_to_intel_dvo(encoder);
  96. u32 dvo_reg = intel_dvo->dev.dvo_reg;
  97. u32 temp = I915_READ(dvo_reg);
  98. if (mode == DRM_MODE_DPMS_ON) {
  99. I915_WRITE(dvo_reg, temp | DVO_ENABLE);
  100. I915_READ(dvo_reg);
  101. intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, mode);
  102. } else {
  103. intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, mode);
  104. I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
  105. I915_READ(dvo_reg);
  106. }
  107. }
  108. static int intel_dvo_mode_valid(struct drm_connector *connector,
  109. struct drm_display_mode *mode)
  110. {
  111. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  112. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  113. return MODE_NO_DBLESCAN;
  114. /* XXX: Validate clock range */
  115. if (intel_dvo->panel_fixed_mode) {
  116. if (mode->hdisplay > intel_dvo->panel_fixed_mode->hdisplay)
  117. return MODE_PANEL;
  118. if (mode->vdisplay > intel_dvo->panel_fixed_mode->vdisplay)
  119. return MODE_PANEL;
  120. }
  121. return intel_dvo->dev.dev_ops->mode_valid(&intel_dvo->dev, mode);
  122. }
  123. static bool intel_dvo_mode_fixup(struct drm_encoder *encoder,
  124. struct drm_display_mode *mode,
  125. struct drm_display_mode *adjusted_mode)
  126. {
  127. struct intel_dvo *intel_dvo = enc_to_intel_dvo(encoder);
  128. /* If we have timings from the BIOS for the panel, put them in
  129. * to the adjusted mode. The CRTC will be set up for this mode,
  130. * with the panel scaling set up to source from the H/VDisplay
  131. * of the original mode.
  132. */
  133. if (intel_dvo->panel_fixed_mode != NULL) {
  134. #define C(x) adjusted_mode->x = intel_dvo->panel_fixed_mode->x
  135. C(hdisplay);
  136. C(hsync_start);
  137. C(hsync_end);
  138. C(htotal);
  139. C(vdisplay);
  140. C(vsync_start);
  141. C(vsync_end);
  142. C(vtotal);
  143. C(clock);
  144. #undef C
  145. }
  146. if (intel_dvo->dev.dev_ops->mode_fixup)
  147. return intel_dvo->dev.dev_ops->mode_fixup(&intel_dvo->dev, mode, adjusted_mode);
  148. return true;
  149. }
  150. static void intel_dvo_mode_set(struct drm_encoder *encoder,
  151. struct drm_display_mode *mode,
  152. struct drm_display_mode *adjusted_mode)
  153. {
  154. struct drm_device *dev = encoder->dev;
  155. struct drm_i915_private *dev_priv = dev->dev_private;
  156. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  157. struct intel_dvo *intel_dvo = enc_to_intel_dvo(encoder);
  158. int pipe = intel_crtc->pipe;
  159. u32 dvo_val;
  160. u32 dvo_reg = intel_dvo->dev.dvo_reg, dvo_srcdim_reg;
  161. int dpll_reg = DPLL(pipe);
  162. switch (dvo_reg) {
  163. case DVOA:
  164. default:
  165. dvo_srcdim_reg = DVOA_SRCDIM;
  166. break;
  167. case DVOB:
  168. dvo_srcdim_reg = DVOB_SRCDIM;
  169. break;
  170. case DVOC:
  171. dvo_srcdim_reg = DVOC_SRCDIM;
  172. break;
  173. }
  174. intel_dvo->dev.dev_ops->mode_set(&intel_dvo->dev, mode, adjusted_mode);
  175. /* Save the data order, since I don't know what it should be set to. */
  176. dvo_val = I915_READ(dvo_reg) &
  177. (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
  178. dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
  179. DVO_BLANK_ACTIVE_HIGH;
  180. if (pipe == 1)
  181. dvo_val |= DVO_PIPE_B_SELECT;
  182. dvo_val |= DVO_PIPE_STALL;
  183. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  184. dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
  185. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  186. dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
  187. I915_WRITE(dpll_reg, I915_READ(dpll_reg) | DPLL_DVO_HIGH_SPEED);
  188. /*I915_WRITE(DVOB_SRCDIM,
  189. (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
  190. (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
  191. I915_WRITE(dvo_srcdim_reg,
  192. (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
  193. (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
  194. /*I915_WRITE(DVOB, dvo_val);*/
  195. I915_WRITE(dvo_reg, dvo_val);
  196. }
  197. /**
  198. * Detect the output connection on our DVO device.
  199. *
  200. * Unimplemented.
  201. */
  202. static enum drm_connector_status
  203. intel_dvo_detect(struct drm_connector *connector, bool force)
  204. {
  205. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  206. return intel_dvo->dev.dev_ops->detect(&intel_dvo->dev);
  207. }
  208. static int intel_dvo_get_modes(struct drm_connector *connector)
  209. {
  210. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  211. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  212. /* We should probably have an i2c driver get_modes function for those
  213. * devices which will have a fixed set of modes determined by the chip
  214. * (TV-out, for example), but for now with just TMDS and LVDS,
  215. * that's not the case.
  216. */
  217. intel_ddc_get_modes(connector,
  218. &dev_priv->gmbus[GMBUS_PORT_DPC].adapter);
  219. if (!list_empty(&connector->probed_modes))
  220. return 1;
  221. if (intel_dvo->panel_fixed_mode != NULL) {
  222. struct drm_display_mode *mode;
  223. mode = drm_mode_duplicate(connector->dev, intel_dvo->panel_fixed_mode);
  224. if (mode) {
  225. drm_mode_probed_add(connector, mode);
  226. return 1;
  227. }
  228. }
  229. return 0;
  230. }
  231. static void intel_dvo_destroy(struct drm_connector *connector)
  232. {
  233. drm_sysfs_connector_remove(connector);
  234. drm_connector_cleanup(connector);
  235. kfree(connector);
  236. }
  237. static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = {
  238. .dpms = intel_dvo_dpms,
  239. .mode_fixup = intel_dvo_mode_fixup,
  240. .prepare = intel_encoder_prepare,
  241. .mode_set = intel_dvo_mode_set,
  242. .commit = intel_encoder_commit,
  243. };
  244. static const struct drm_connector_funcs intel_dvo_connector_funcs = {
  245. .dpms = drm_helper_connector_dpms,
  246. .detect = intel_dvo_detect,
  247. .destroy = intel_dvo_destroy,
  248. .fill_modes = drm_helper_probe_single_connector_modes,
  249. };
  250. static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
  251. .mode_valid = intel_dvo_mode_valid,
  252. .get_modes = intel_dvo_get_modes,
  253. .best_encoder = intel_best_encoder,
  254. };
  255. static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
  256. {
  257. struct intel_dvo *intel_dvo = enc_to_intel_dvo(encoder);
  258. if (intel_dvo->dev.dev_ops->destroy)
  259. intel_dvo->dev.dev_ops->destroy(&intel_dvo->dev);
  260. kfree(intel_dvo->panel_fixed_mode);
  261. intel_encoder_destroy(encoder);
  262. }
  263. static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
  264. .destroy = intel_dvo_enc_destroy,
  265. };
  266. /**
  267. * Attempts to get a fixed panel timing for LVDS (currently only the i830).
  268. *
  269. * Other chips with DVO LVDS will need to extend this to deal with the LVDS
  270. * chip being on DVOB/C and having multiple pipes.
  271. */
  272. static struct drm_display_mode *
  273. intel_dvo_get_current_mode(struct drm_connector *connector)
  274. {
  275. struct drm_device *dev = connector->dev;
  276. struct drm_i915_private *dev_priv = dev->dev_private;
  277. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  278. uint32_t dvo_val = I915_READ(intel_dvo->dev.dvo_reg);
  279. struct drm_display_mode *mode = NULL;
  280. /* If the DVO port is active, that'll be the LVDS, so we can pull out
  281. * its timings to get how the BIOS set up the panel.
  282. */
  283. if (dvo_val & DVO_ENABLE) {
  284. struct drm_crtc *crtc;
  285. int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
  286. crtc = intel_get_crtc_for_pipe(dev, pipe);
  287. if (crtc) {
  288. mode = intel_crtc_mode_get(dev, crtc);
  289. if (mode) {
  290. mode->type |= DRM_MODE_TYPE_PREFERRED;
  291. if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
  292. mode->flags |= DRM_MODE_FLAG_PHSYNC;
  293. if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
  294. mode->flags |= DRM_MODE_FLAG_PVSYNC;
  295. }
  296. }
  297. }
  298. return mode;
  299. }
  300. void intel_dvo_init(struct drm_device *dev)
  301. {
  302. struct drm_i915_private *dev_priv = dev->dev_private;
  303. struct intel_encoder *intel_encoder;
  304. struct intel_dvo *intel_dvo;
  305. struct intel_connector *intel_connector;
  306. int i;
  307. int encoder_type = DRM_MODE_ENCODER_NONE;
  308. intel_dvo = kzalloc(sizeof(struct intel_dvo), GFP_KERNEL);
  309. if (!intel_dvo)
  310. return;
  311. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  312. if (!intel_connector) {
  313. kfree(intel_dvo);
  314. return;
  315. }
  316. intel_encoder = &intel_dvo->base;
  317. drm_encoder_init(dev, &intel_encoder->base,
  318. &intel_dvo_enc_funcs, encoder_type);
  319. /* Now, try to find a controller */
  320. for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
  321. struct drm_connector *connector = &intel_connector->base;
  322. const struct intel_dvo_device *dvo = &intel_dvo_devices[i];
  323. struct i2c_adapter *i2c;
  324. int gpio;
  325. bool dvoinit;
  326. /* Allow the I2C driver info to specify the GPIO to be used in
  327. * special cases, but otherwise default to what's defined
  328. * in the spec.
  329. */
  330. if (dvo->gpio != 0)
  331. gpio = dvo->gpio;
  332. else if (dvo->type == INTEL_DVO_CHIP_LVDS)
  333. gpio = GMBUS_PORT_SSC;
  334. else
  335. gpio = GMBUS_PORT_DPB;
  336. /* Set up the I2C bus necessary for the chip we're probing.
  337. * It appears that everything is on GPIOE except for panels
  338. * on i830 laptops, which are on GPIOB (DVOA).
  339. */
  340. i2c = &dev_priv->gmbus[gpio].adapter;
  341. intel_dvo->dev = *dvo;
  342. /* GMBUS NAK handling seems to be unstable, hence let the
  343. * transmitter detection run in bit banging mode for now.
  344. */
  345. intel_gmbus_force_bit(i2c, true);
  346. dvoinit = dvo->dev_ops->init(&intel_dvo->dev, i2c);
  347. intel_gmbus_force_bit(i2c, false);
  348. if (!dvoinit)
  349. continue;
  350. intel_encoder->type = INTEL_OUTPUT_DVO;
  351. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  352. switch (dvo->type) {
  353. case INTEL_DVO_CHIP_TMDS:
  354. intel_encoder->clone_mask =
  355. (1 << INTEL_DVO_TMDS_CLONE_BIT) |
  356. (1 << INTEL_ANALOG_CLONE_BIT);
  357. drm_connector_init(dev, connector,
  358. &intel_dvo_connector_funcs,
  359. DRM_MODE_CONNECTOR_DVII);
  360. encoder_type = DRM_MODE_ENCODER_TMDS;
  361. break;
  362. case INTEL_DVO_CHIP_LVDS:
  363. intel_encoder->clone_mask =
  364. (1 << INTEL_DVO_LVDS_CLONE_BIT);
  365. drm_connector_init(dev, connector,
  366. &intel_dvo_connector_funcs,
  367. DRM_MODE_CONNECTOR_LVDS);
  368. encoder_type = DRM_MODE_ENCODER_LVDS;
  369. break;
  370. }
  371. drm_connector_helper_add(connector,
  372. &intel_dvo_connector_helper_funcs);
  373. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  374. connector->interlace_allowed = false;
  375. connector->doublescan_allowed = false;
  376. drm_encoder_helper_add(&intel_encoder->base,
  377. &intel_dvo_helper_funcs);
  378. intel_connector_attach_encoder(intel_connector, intel_encoder);
  379. if (dvo->type == INTEL_DVO_CHIP_LVDS) {
  380. /* For our LVDS chipsets, we should hopefully be able
  381. * to dig the fixed panel mode out of the BIOS data.
  382. * However, it's in a different format from the BIOS
  383. * data on chipsets with integrated LVDS (stored in AIM
  384. * headers, likely), so for now, just get the current
  385. * mode being output through DVO.
  386. */
  387. intel_dvo->panel_fixed_mode =
  388. intel_dvo_get_current_mode(connector);
  389. intel_dvo->panel_wants_dither = true;
  390. }
  391. drm_sysfs_connector_add(connector);
  392. return;
  393. }
  394. drm_encoder_cleanup(&intel_encoder->base);
  395. kfree(intel_dvo);
  396. kfree(intel_connector);
  397. }