intel_dvo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <drm/drmP.h>
  30. #include <drm/drm_crtc.h>
  31. #include "intel_drv.h"
  32. #include <drm/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. #define NS2501_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_TMDS,
  56. .name = "ch7xxx",
  57. .dvo_reg = DVOC,
  58. .slave_addr = 0x75, /* For some ch7010 */
  59. .dev_ops = &ch7xxx_ops,
  60. },
  61. {
  62. .type = INTEL_DVO_CHIP_LVDS,
  63. .name = "ivch",
  64. .dvo_reg = DVOA,
  65. .slave_addr = 0x02, /* Might also be 0x44, 0x84, 0xc4 */
  66. .dev_ops = &ivch_ops,
  67. },
  68. {
  69. .type = INTEL_DVO_CHIP_TMDS,
  70. .name = "tfp410",
  71. .dvo_reg = DVOC,
  72. .slave_addr = TFP410_ADDR,
  73. .dev_ops = &tfp410_ops,
  74. },
  75. {
  76. .type = INTEL_DVO_CHIP_LVDS,
  77. .name = "ch7017",
  78. .dvo_reg = DVOC,
  79. .slave_addr = 0x75,
  80. .gpio = GMBUS_PORT_DPB,
  81. .dev_ops = &ch7017_ops,
  82. },
  83. {
  84. .type = INTEL_DVO_CHIP_TMDS,
  85. .name = "ns2501",
  86. .dvo_reg = DVOC,
  87. .slave_addr = NS2501_ADDR,
  88. .dev_ops = &ns2501_ops,
  89. }
  90. };
  91. struct intel_dvo {
  92. struct intel_encoder base;
  93. struct intel_dvo_device dev;
  94. struct drm_display_mode *panel_fixed_mode;
  95. bool panel_wants_dither;
  96. };
  97. static struct intel_dvo *enc_to_dvo(struct intel_encoder *encoder)
  98. {
  99. return container_of(encoder, struct intel_dvo, base);
  100. }
  101. static struct intel_dvo *intel_attached_dvo(struct drm_connector *connector)
  102. {
  103. return enc_to_dvo(intel_attached_encoder(connector));
  104. }
  105. static bool intel_dvo_connector_get_hw_state(struct intel_connector *connector)
  106. {
  107. struct intel_dvo *intel_dvo = intel_attached_dvo(&connector->base);
  108. return intel_dvo->dev.dev_ops->get_hw_state(&intel_dvo->dev);
  109. }
  110. static bool intel_dvo_get_hw_state(struct intel_encoder *encoder,
  111. enum pipe *pipe)
  112. {
  113. struct drm_device *dev = encoder->base.dev;
  114. struct drm_i915_private *dev_priv = dev->dev_private;
  115. struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
  116. u32 tmp;
  117. tmp = I915_READ(intel_dvo->dev.dvo_reg);
  118. if (!(tmp & DVO_ENABLE))
  119. return false;
  120. *pipe = PORT_TO_PIPE(tmp);
  121. return true;
  122. }
  123. static void intel_dvo_get_config(struct intel_encoder *encoder,
  124. struct intel_crtc_config *pipe_config)
  125. {
  126. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  127. struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
  128. u32 tmp, flags = 0;
  129. tmp = I915_READ(intel_dvo->dev.dvo_reg);
  130. if (tmp & DVO_HSYNC_ACTIVE_HIGH)
  131. flags |= DRM_MODE_FLAG_PHSYNC;
  132. else
  133. flags |= DRM_MODE_FLAG_NHSYNC;
  134. if (tmp & DVO_VSYNC_ACTIVE_HIGH)
  135. flags |= DRM_MODE_FLAG_PVSYNC;
  136. else
  137. flags |= DRM_MODE_FLAG_NVSYNC;
  138. pipe_config->adjusted_mode.flags |= flags;
  139. }
  140. static void intel_disable_dvo(struct intel_encoder *encoder)
  141. {
  142. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  143. struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
  144. u32 dvo_reg = intel_dvo->dev.dvo_reg;
  145. u32 temp = I915_READ(dvo_reg);
  146. intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, false);
  147. I915_WRITE(dvo_reg, temp & ~DVO_ENABLE);
  148. I915_READ(dvo_reg);
  149. }
  150. static void intel_enable_dvo(struct intel_encoder *encoder)
  151. {
  152. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  153. struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
  154. u32 dvo_reg = intel_dvo->dev.dvo_reg;
  155. u32 temp = I915_READ(dvo_reg);
  156. I915_WRITE(dvo_reg, temp | DVO_ENABLE);
  157. I915_READ(dvo_reg);
  158. intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, true);
  159. }
  160. /* Special dpms function to support cloning between dvo/sdvo/crt. */
  161. static void intel_dvo_dpms(struct drm_connector *connector, int mode)
  162. {
  163. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  164. struct drm_crtc *crtc;
  165. /* dvo supports only 2 dpms states. */
  166. if (mode != DRM_MODE_DPMS_ON)
  167. mode = DRM_MODE_DPMS_OFF;
  168. if (mode == connector->dpms)
  169. return;
  170. connector->dpms = mode;
  171. /* Only need to change hw state when actually enabled */
  172. crtc = intel_dvo->base.base.crtc;
  173. if (!crtc) {
  174. intel_dvo->base.connectors_active = false;
  175. return;
  176. }
  177. /* We call connector dpms manually below in case pipe dpms doesn't
  178. * change due to cloning. */
  179. if (mode == DRM_MODE_DPMS_ON) {
  180. intel_dvo->base.connectors_active = true;
  181. intel_crtc_update_dpms(crtc);
  182. intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, true);
  183. } else {
  184. intel_dvo->dev.dev_ops->dpms(&intel_dvo->dev, false);
  185. intel_dvo->base.connectors_active = false;
  186. intel_crtc_update_dpms(crtc);
  187. }
  188. intel_modeset_check_state(connector->dev);
  189. }
  190. static int intel_dvo_mode_valid(struct drm_connector *connector,
  191. struct drm_display_mode *mode)
  192. {
  193. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  194. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  195. return MODE_NO_DBLESCAN;
  196. /* XXX: Validate clock range */
  197. if (intel_dvo->panel_fixed_mode) {
  198. if (mode->hdisplay > intel_dvo->panel_fixed_mode->hdisplay)
  199. return MODE_PANEL;
  200. if (mode->vdisplay > intel_dvo->panel_fixed_mode->vdisplay)
  201. return MODE_PANEL;
  202. }
  203. return intel_dvo->dev.dev_ops->mode_valid(&intel_dvo->dev, mode);
  204. }
  205. static bool intel_dvo_compute_config(struct intel_encoder *encoder,
  206. struct intel_crtc_config *pipe_config)
  207. {
  208. struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
  209. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  210. /* If we have timings from the BIOS for the panel, put them in
  211. * to the adjusted mode. The CRTC will be set up for this mode,
  212. * with the panel scaling set up to source from the H/VDisplay
  213. * of the original mode.
  214. */
  215. if (intel_dvo->panel_fixed_mode != NULL) {
  216. #define C(x) adjusted_mode->x = intel_dvo->panel_fixed_mode->x
  217. C(hdisplay);
  218. C(hsync_start);
  219. C(hsync_end);
  220. C(htotal);
  221. C(vdisplay);
  222. C(vsync_start);
  223. C(vsync_end);
  224. C(vtotal);
  225. C(clock);
  226. #undef C
  227. drm_mode_set_crtcinfo(adjusted_mode, 0);
  228. }
  229. if (intel_dvo->dev.dev_ops->mode_fixup)
  230. return intel_dvo->dev.dev_ops->mode_fixup(&intel_dvo->dev,
  231. &pipe_config->requested_mode,
  232. adjusted_mode);
  233. return true;
  234. }
  235. static void intel_dvo_mode_set(struct intel_encoder *encoder)
  236. {
  237. struct drm_device *dev = encoder->base.dev;
  238. struct drm_i915_private *dev_priv = dev->dev_private;
  239. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  240. struct drm_display_mode *adjusted_mode = &crtc->config.adjusted_mode;
  241. struct intel_dvo *intel_dvo = enc_to_dvo(encoder);
  242. int pipe = crtc->pipe;
  243. u32 dvo_val;
  244. u32 dvo_reg = intel_dvo->dev.dvo_reg, dvo_srcdim_reg;
  245. switch (dvo_reg) {
  246. case DVOA:
  247. default:
  248. dvo_srcdim_reg = DVOA_SRCDIM;
  249. break;
  250. case DVOB:
  251. dvo_srcdim_reg = DVOB_SRCDIM;
  252. break;
  253. case DVOC:
  254. dvo_srcdim_reg = DVOC_SRCDIM;
  255. break;
  256. }
  257. intel_dvo->dev.dev_ops->mode_set(&intel_dvo->dev,
  258. &crtc->config.requested_mode,
  259. adjusted_mode);
  260. /* Save the data order, since I don't know what it should be set to. */
  261. dvo_val = I915_READ(dvo_reg) &
  262. (DVO_PRESERVE_MASK | DVO_DATA_ORDER_GBRG);
  263. dvo_val |= DVO_DATA_ORDER_FP | DVO_BORDER_ENABLE |
  264. DVO_BLANK_ACTIVE_HIGH;
  265. if (pipe == 1)
  266. dvo_val |= DVO_PIPE_B_SELECT;
  267. dvo_val |= DVO_PIPE_STALL;
  268. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  269. dvo_val |= DVO_HSYNC_ACTIVE_HIGH;
  270. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  271. dvo_val |= DVO_VSYNC_ACTIVE_HIGH;
  272. /*I915_WRITE(DVOB_SRCDIM,
  273. (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
  274. (adjusted_mode->VDisplay << DVO_SRCDIM_VERTICAL_SHIFT));*/
  275. I915_WRITE(dvo_srcdim_reg,
  276. (adjusted_mode->hdisplay << DVO_SRCDIM_HORIZONTAL_SHIFT) |
  277. (adjusted_mode->vdisplay << DVO_SRCDIM_VERTICAL_SHIFT));
  278. /*I915_WRITE(DVOB, dvo_val);*/
  279. I915_WRITE(dvo_reg, dvo_val);
  280. }
  281. /**
  282. * Detect the output connection on our DVO device.
  283. *
  284. * Unimplemented.
  285. */
  286. static enum drm_connector_status
  287. intel_dvo_detect(struct drm_connector *connector, bool force)
  288. {
  289. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  290. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  291. connector->base.id, drm_get_connector_name(connector));
  292. return intel_dvo->dev.dev_ops->detect(&intel_dvo->dev);
  293. }
  294. static int intel_dvo_get_modes(struct drm_connector *connector)
  295. {
  296. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  297. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  298. /* We should probably have an i2c driver get_modes function for those
  299. * devices which will have a fixed set of modes determined by the chip
  300. * (TV-out, for example), but for now with just TMDS and LVDS,
  301. * that's not the case.
  302. */
  303. intel_ddc_get_modes(connector,
  304. intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPC));
  305. if (!list_empty(&connector->probed_modes))
  306. return 1;
  307. if (intel_dvo->panel_fixed_mode != NULL) {
  308. struct drm_display_mode *mode;
  309. mode = drm_mode_duplicate(connector->dev, intel_dvo->panel_fixed_mode);
  310. if (mode) {
  311. drm_mode_probed_add(connector, mode);
  312. return 1;
  313. }
  314. }
  315. return 0;
  316. }
  317. static void intel_dvo_destroy(struct drm_connector *connector)
  318. {
  319. drm_sysfs_connector_remove(connector);
  320. drm_connector_cleanup(connector);
  321. kfree(connector);
  322. }
  323. static const struct drm_connector_funcs intel_dvo_connector_funcs = {
  324. .dpms = intel_dvo_dpms,
  325. .detect = intel_dvo_detect,
  326. .destroy = intel_dvo_destroy,
  327. .fill_modes = drm_helper_probe_single_connector_modes,
  328. };
  329. static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = {
  330. .mode_valid = intel_dvo_mode_valid,
  331. .get_modes = intel_dvo_get_modes,
  332. .best_encoder = intel_best_encoder,
  333. };
  334. static void intel_dvo_enc_destroy(struct drm_encoder *encoder)
  335. {
  336. struct intel_dvo *intel_dvo = enc_to_dvo(to_intel_encoder(encoder));
  337. if (intel_dvo->dev.dev_ops->destroy)
  338. intel_dvo->dev.dev_ops->destroy(&intel_dvo->dev);
  339. kfree(intel_dvo->panel_fixed_mode);
  340. intel_encoder_destroy(encoder);
  341. }
  342. static const struct drm_encoder_funcs intel_dvo_enc_funcs = {
  343. .destroy = intel_dvo_enc_destroy,
  344. };
  345. /**
  346. * Attempts to get a fixed panel timing for LVDS (currently only the i830).
  347. *
  348. * Other chips with DVO LVDS will need to extend this to deal with the LVDS
  349. * chip being on DVOB/C and having multiple pipes.
  350. */
  351. static struct drm_display_mode *
  352. intel_dvo_get_current_mode(struct drm_connector *connector)
  353. {
  354. struct drm_device *dev = connector->dev;
  355. struct drm_i915_private *dev_priv = dev->dev_private;
  356. struct intel_dvo *intel_dvo = intel_attached_dvo(connector);
  357. uint32_t dvo_val = I915_READ(intel_dvo->dev.dvo_reg);
  358. struct drm_display_mode *mode = NULL;
  359. /* If the DVO port is active, that'll be the LVDS, so we can pull out
  360. * its timings to get how the BIOS set up the panel.
  361. */
  362. if (dvo_val & DVO_ENABLE) {
  363. struct drm_crtc *crtc;
  364. int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0;
  365. crtc = intel_get_crtc_for_pipe(dev, pipe);
  366. if (crtc) {
  367. mode = intel_crtc_mode_get(dev, crtc);
  368. if (mode) {
  369. mode->type |= DRM_MODE_TYPE_PREFERRED;
  370. if (dvo_val & DVO_HSYNC_ACTIVE_HIGH)
  371. mode->flags |= DRM_MODE_FLAG_PHSYNC;
  372. if (dvo_val & DVO_VSYNC_ACTIVE_HIGH)
  373. mode->flags |= DRM_MODE_FLAG_PVSYNC;
  374. }
  375. }
  376. }
  377. return mode;
  378. }
  379. void intel_dvo_init(struct drm_device *dev)
  380. {
  381. struct drm_i915_private *dev_priv = dev->dev_private;
  382. struct intel_encoder *intel_encoder;
  383. struct intel_dvo *intel_dvo;
  384. struct intel_connector *intel_connector;
  385. int i;
  386. int encoder_type = DRM_MODE_ENCODER_NONE;
  387. intel_dvo = kzalloc(sizeof(struct intel_dvo), GFP_KERNEL);
  388. if (!intel_dvo)
  389. return;
  390. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  391. if (!intel_connector) {
  392. kfree(intel_dvo);
  393. return;
  394. }
  395. intel_encoder = &intel_dvo->base;
  396. drm_encoder_init(dev, &intel_encoder->base,
  397. &intel_dvo_enc_funcs, encoder_type);
  398. intel_encoder->disable = intel_disable_dvo;
  399. intel_encoder->enable = intel_enable_dvo;
  400. intel_encoder->get_hw_state = intel_dvo_get_hw_state;
  401. intel_encoder->get_config = intel_dvo_get_config;
  402. intel_encoder->compute_config = intel_dvo_compute_config;
  403. intel_encoder->mode_set = intel_dvo_mode_set;
  404. intel_connector->get_hw_state = intel_dvo_connector_get_hw_state;
  405. /* Now, try to find a controller */
  406. for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) {
  407. struct drm_connector *connector = &intel_connector->base;
  408. const struct intel_dvo_device *dvo = &intel_dvo_devices[i];
  409. struct i2c_adapter *i2c;
  410. int gpio;
  411. bool dvoinit;
  412. /* Allow the I2C driver info to specify the GPIO to be used in
  413. * special cases, but otherwise default to what's defined
  414. * in the spec.
  415. */
  416. if (intel_gmbus_is_port_valid(dvo->gpio))
  417. gpio = dvo->gpio;
  418. else if (dvo->type == INTEL_DVO_CHIP_LVDS)
  419. gpio = GMBUS_PORT_SSC;
  420. else
  421. gpio = GMBUS_PORT_DPB;
  422. /* Set up the I2C bus necessary for the chip we're probing.
  423. * It appears that everything is on GPIOE except for panels
  424. * on i830 laptops, which are on GPIOB (DVOA).
  425. */
  426. i2c = intel_gmbus_get_adapter(dev_priv, gpio);
  427. intel_dvo->dev = *dvo;
  428. /* GMBUS NAK handling seems to be unstable, hence let the
  429. * transmitter detection run in bit banging mode for now.
  430. */
  431. intel_gmbus_force_bit(i2c, true);
  432. dvoinit = dvo->dev_ops->init(&intel_dvo->dev, i2c);
  433. intel_gmbus_force_bit(i2c, false);
  434. if (!dvoinit)
  435. continue;
  436. intel_encoder->type = INTEL_OUTPUT_DVO;
  437. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  438. switch (dvo->type) {
  439. case INTEL_DVO_CHIP_TMDS:
  440. intel_encoder->cloneable = true;
  441. drm_connector_init(dev, connector,
  442. &intel_dvo_connector_funcs,
  443. DRM_MODE_CONNECTOR_DVII);
  444. encoder_type = DRM_MODE_ENCODER_TMDS;
  445. break;
  446. case INTEL_DVO_CHIP_LVDS:
  447. intel_encoder->cloneable = false;
  448. drm_connector_init(dev, connector,
  449. &intel_dvo_connector_funcs,
  450. DRM_MODE_CONNECTOR_LVDS);
  451. encoder_type = DRM_MODE_ENCODER_LVDS;
  452. break;
  453. }
  454. drm_connector_helper_add(connector,
  455. &intel_dvo_connector_helper_funcs);
  456. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  457. connector->interlace_allowed = false;
  458. connector->doublescan_allowed = false;
  459. intel_connector_attach_encoder(intel_connector, intel_encoder);
  460. if (dvo->type == INTEL_DVO_CHIP_LVDS) {
  461. /* For our LVDS chipsets, we should hopefully be able
  462. * to dig the fixed panel mode out of the BIOS data.
  463. * However, it's in a different format from the BIOS
  464. * data on chipsets with integrated LVDS (stored in AIM
  465. * headers, likely), so for now, just get the current
  466. * mode being output through DVO.
  467. */
  468. intel_dvo->panel_fixed_mode =
  469. intel_dvo_get_current_mode(connector);
  470. intel_dvo->panel_wants_dither = true;
  471. }
  472. drm_sysfs_connector_add(connector);
  473. return;
  474. }
  475. drm_encoder_cleanup(&intel_encoder->base);
  476. kfree(intel_dvo);
  477. kfree(intel_connector);
  478. }