cdv_intel_hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright © 2006-2011 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * jim liu <jim.liu@intel.com>
  25. *
  26. * FIXME:
  27. * We should probably make this generic and share it with Medfield
  28. */
  29. #include <drm/drmP.h>
  30. #include <drm/drm.h>
  31. #include <drm/drm_crtc.h>
  32. #include <drm/drm_edid.h>
  33. #include "psb_intel_drv.h"
  34. #include "psb_drv.h"
  35. #include "psb_intel_reg.h"
  36. #include "cdv_device.h"
  37. #include <linux/pm_runtime.h>
  38. /* hdmi control bits */
  39. #define HDMI_NULL_PACKETS_DURING_VSYNC (1 << 9)
  40. #define HDMI_BORDER_ENABLE (1 << 7)
  41. #define HDMI_AUDIO_ENABLE (1 << 6)
  42. #define HDMI_VSYNC_ACTIVE_HIGH (1 << 4)
  43. #define HDMI_HSYNC_ACTIVE_HIGH (1 << 3)
  44. /* hdmi-b control bits */
  45. #define HDMIB_PIPE_B_SELECT (1 << 30)
  46. struct mid_intel_hdmi_priv {
  47. u32 hdmi_reg;
  48. u32 save_HDMIB;
  49. bool has_hdmi_sink;
  50. bool has_hdmi_audio;
  51. /* Should set this when detect hotplug */
  52. bool hdmi_device_connected;
  53. struct mdfld_hdmi_i2c *i2c_bus;
  54. struct i2c_adapter *hdmi_i2c_adapter; /* for control functions */
  55. struct drm_device *dev;
  56. };
  57. static void cdv_hdmi_mode_set(struct drm_encoder *encoder,
  58. struct drm_display_mode *mode,
  59. struct drm_display_mode *adjusted_mode)
  60. {
  61. struct drm_device *dev = encoder->dev;
  62. struct psb_intel_encoder *psb_intel_encoder = to_psb_intel_encoder(encoder);
  63. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  64. u32 hdmib;
  65. struct drm_crtc *crtc = encoder->crtc;
  66. struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
  67. hdmib = (2 << 10);
  68. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  69. hdmib |= HDMI_VSYNC_ACTIVE_HIGH;
  70. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  71. hdmib |= HDMI_HSYNC_ACTIVE_HIGH;
  72. if (intel_crtc->pipe == 1)
  73. hdmib |= HDMIB_PIPE_B_SELECT;
  74. if (hdmi_priv->has_hdmi_audio) {
  75. hdmib |= HDMI_AUDIO_ENABLE;
  76. hdmib |= HDMI_NULL_PACKETS_DURING_VSYNC;
  77. }
  78. REG_WRITE(hdmi_priv->hdmi_reg, hdmib);
  79. REG_READ(hdmi_priv->hdmi_reg);
  80. }
  81. static bool cdv_hdmi_mode_fixup(struct drm_encoder *encoder,
  82. struct drm_display_mode *mode,
  83. struct drm_display_mode *adjusted_mode)
  84. {
  85. return true;
  86. }
  87. static void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
  88. {
  89. struct drm_device *dev = encoder->dev;
  90. struct psb_intel_encoder *psb_intel_encoder =
  91. to_psb_intel_encoder(encoder);
  92. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  93. u32 hdmib;
  94. hdmib = REG_READ(hdmi_priv->hdmi_reg);
  95. if (mode != DRM_MODE_DPMS_ON)
  96. REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
  97. else
  98. REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
  99. REG_READ(hdmi_priv->hdmi_reg);
  100. }
  101. static void cdv_hdmi_save(struct drm_connector *connector)
  102. {
  103. struct drm_device *dev = connector->dev;
  104. struct psb_intel_encoder *psb_intel_encoder =
  105. psb_intel_attached_encoder(connector);
  106. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  107. hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
  108. }
  109. static void cdv_hdmi_restore(struct drm_connector *connector)
  110. {
  111. struct drm_device *dev = connector->dev;
  112. struct psb_intel_encoder *psb_intel_encoder =
  113. psb_intel_attached_encoder(connector);
  114. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  115. REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
  116. REG_READ(hdmi_priv->hdmi_reg);
  117. }
  118. static enum drm_connector_status cdv_hdmi_detect(
  119. struct drm_connector *connector, bool force)
  120. {
  121. struct psb_intel_encoder *psb_intel_encoder =
  122. psb_intel_attached_encoder(connector);
  123. struct psb_intel_connector *psb_intel_connector =
  124. to_psb_intel_connector(connector);
  125. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  126. struct edid *edid = NULL;
  127. enum drm_connector_status status = connector_status_disconnected;
  128. edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
  129. hdmi_priv->has_hdmi_sink = false;
  130. hdmi_priv->has_hdmi_audio = false;
  131. if (edid) {
  132. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  133. status = connector_status_connected;
  134. hdmi_priv->has_hdmi_sink =
  135. drm_detect_hdmi_monitor(edid);
  136. hdmi_priv->has_hdmi_audio =
  137. drm_detect_monitor_audio(edid);
  138. }
  139. psb_intel_connector->base.display_info.raw_edid = NULL;
  140. kfree(edid);
  141. }
  142. return status;
  143. }
  144. static int cdv_hdmi_set_property(struct drm_connector *connector,
  145. struct drm_property *property,
  146. uint64_t value)
  147. {
  148. struct drm_encoder *encoder = connector->encoder;
  149. if (!strcmp(property->name, "scaling mode") && encoder) {
  150. struct psb_intel_crtc *crtc = to_psb_intel_crtc(encoder->crtc);
  151. bool centre;
  152. uint64_t curValue;
  153. if (!crtc)
  154. return -1;
  155. switch (value) {
  156. case DRM_MODE_SCALE_FULLSCREEN:
  157. break;
  158. case DRM_MODE_SCALE_NO_SCALE:
  159. break;
  160. case DRM_MODE_SCALE_ASPECT:
  161. break;
  162. default:
  163. return -1;
  164. }
  165. if (drm_connector_property_get_value(connector,
  166. property, &curValue))
  167. return -1;
  168. if (curValue == value)
  169. return 0;
  170. if (drm_connector_property_set_value(connector,
  171. property, value))
  172. return -1;
  173. centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
  174. (value == DRM_MODE_SCALE_NO_SCALE);
  175. if (crtc->saved_mode.hdisplay != 0 &&
  176. crtc->saved_mode.vdisplay != 0) {
  177. if (centre) {
  178. if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
  179. encoder->crtc->x, encoder->crtc->y, encoder->crtc->fb))
  180. return -1;
  181. } else {
  182. struct drm_encoder_helper_funcs *helpers
  183. = encoder->helper_private;
  184. helpers->mode_set(encoder, &crtc->saved_mode,
  185. &crtc->saved_adjusted_mode);
  186. }
  187. }
  188. }
  189. return 0;
  190. }
  191. /*
  192. * Return the list of HDMI DDC modes if available.
  193. */
  194. static int cdv_hdmi_get_modes(struct drm_connector *connector)
  195. {
  196. struct psb_intel_encoder *psb_intel_encoder =
  197. psb_intel_attached_encoder(connector);
  198. struct edid *edid = NULL;
  199. int ret = 0;
  200. edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
  201. if (edid) {
  202. drm_mode_connector_update_edid_property(connector, edid);
  203. ret = drm_add_edid_modes(connector, edid);
  204. kfree(edid);
  205. }
  206. return ret;
  207. }
  208. static int cdv_hdmi_mode_valid(struct drm_connector *connector,
  209. struct drm_display_mode *mode)
  210. {
  211. struct drm_psb_private *dev_priv = connector->dev->dev_private;
  212. if (mode->clock > 165000)
  213. return MODE_CLOCK_HIGH;
  214. if (mode->clock < 20000)
  215. return MODE_CLOCK_HIGH;
  216. /* just in case */
  217. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  218. return MODE_NO_DBLESCAN;
  219. /* just in case */
  220. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  221. return MODE_NO_INTERLACE;
  222. /* We assume worst case scenario of 32 bpp here, since we don't know */
  223. if ((ALIGN(mode->hdisplay * 4, 64) * mode->vdisplay) >
  224. dev_priv->vram_stolen_size)
  225. return MODE_MEM;
  226. return MODE_OK;
  227. }
  228. static void cdv_hdmi_destroy(struct drm_connector *connector)
  229. {
  230. struct psb_intel_encoder *psb_intel_encoder =
  231. psb_intel_attached_encoder(connector);
  232. if (psb_intel_encoder->i2c_bus)
  233. psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
  234. drm_sysfs_connector_remove(connector);
  235. drm_connector_cleanup(connector);
  236. kfree(connector);
  237. }
  238. static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
  239. .dpms = cdv_hdmi_dpms,
  240. .mode_fixup = cdv_hdmi_mode_fixup,
  241. .prepare = psb_intel_encoder_prepare,
  242. .mode_set = cdv_hdmi_mode_set,
  243. .commit = psb_intel_encoder_commit,
  244. };
  245. static const struct drm_connector_helper_funcs
  246. cdv_hdmi_connector_helper_funcs = {
  247. .get_modes = cdv_hdmi_get_modes,
  248. .mode_valid = cdv_hdmi_mode_valid,
  249. .best_encoder = psb_intel_best_encoder,
  250. };
  251. static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
  252. .dpms = drm_helper_connector_dpms,
  253. .save = cdv_hdmi_save,
  254. .restore = cdv_hdmi_restore,
  255. .detect = cdv_hdmi_detect,
  256. .fill_modes = drm_helper_probe_single_connector_modes,
  257. .set_property = cdv_hdmi_set_property,
  258. .destroy = cdv_hdmi_destroy,
  259. };
  260. void cdv_hdmi_init(struct drm_device *dev,
  261. struct psb_intel_mode_device *mode_dev, int reg)
  262. {
  263. struct psb_intel_encoder *psb_intel_encoder;
  264. struct psb_intel_connector *psb_intel_connector;
  265. struct drm_connector *connector;
  266. struct drm_encoder *encoder;
  267. struct mid_intel_hdmi_priv *hdmi_priv;
  268. int ddc_bus;
  269. psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder),
  270. GFP_KERNEL);
  271. if (!psb_intel_encoder)
  272. return;
  273. psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector),
  274. GFP_KERNEL);
  275. if (!psb_intel_connector)
  276. goto err_connector;
  277. hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
  278. if (!hdmi_priv)
  279. goto err_priv;
  280. connector = &psb_intel_connector->base;
  281. encoder = &psb_intel_encoder->base;
  282. drm_connector_init(dev, connector,
  283. &cdv_hdmi_connector_funcs,
  284. DRM_MODE_CONNECTOR_DVID);
  285. drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
  286. DRM_MODE_ENCODER_TMDS);
  287. psb_intel_connector_attach_encoder(psb_intel_connector,
  288. psb_intel_encoder);
  289. psb_intel_encoder->type = INTEL_OUTPUT_HDMI;
  290. hdmi_priv->hdmi_reg = reg;
  291. hdmi_priv->has_hdmi_sink = false;
  292. psb_intel_encoder->dev_priv = hdmi_priv;
  293. drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
  294. drm_connector_helper_add(connector,
  295. &cdv_hdmi_connector_helper_funcs);
  296. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  297. connector->interlace_allowed = false;
  298. connector->doublescan_allowed = false;
  299. drm_connector_attach_property(connector,
  300. dev->mode_config.scaling_mode_property,
  301. DRM_MODE_SCALE_FULLSCREEN);
  302. switch (reg) {
  303. case SDVOB:
  304. ddc_bus = GPIOE;
  305. break;
  306. case SDVOC:
  307. ddc_bus = GPIOD;
  308. break;
  309. default:
  310. DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
  311. goto failed_ddc;
  312. break;
  313. }
  314. psb_intel_encoder->i2c_bus = psb_intel_i2c_create(dev,
  315. ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
  316. if (!psb_intel_encoder->i2c_bus) {
  317. dev_err(dev->dev, "No ddc adapter available!\n");
  318. goto failed_ddc;
  319. }
  320. hdmi_priv->hdmi_i2c_adapter =
  321. &(psb_intel_encoder->i2c_bus->adapter);
  322. hdmi_priv->dev = dev;
  323. drm_sysfs_connector_add(connector);
  324. return;
  325. failed_ddc:
  326. drm_encoder_cleanup(encoder);
  327. drm_connector_cleanup(connector);
  328. err_priv:
  329. kfree(psb_intel_connector);
  330. err_connector:
  331. kfree(psb_intel_encoder);
  332. }