intel_dvo.c 14 KB

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