intel_dvo.c 14 KB

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