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 <linux/pm_runtime.h>
  37. /* hdmi control bits */
  38. #define HDMI_NULL_PACKETS_DURING_VSYNC (1 << 9)
  39. #define HDMI_BORDER_ENABLE (1 << 7)
  40. #define HDMI_AUDIO_ENABLE (1 << 6)
  41. #define HDMI_VSYNC_ACTIVE_HIGH (1 << 4)
  42. #define HDMI_HSYNC_ACTIVE_HIGH (1 << 3)
  43. /* hdmi-b control bits */
  44. #define HDMIB_PIPE_B_SELECT (1 << 30)
  45. struct mid_intel_hdmi_priv {
  46. u32 hdmi_reg;
  47. u32 save_HDMIB;
  48. bool has_hdmi_sink;
  49. bool has_hdmi_audio;
  50. /* Should set this when detect hotplug */
  51. bool hdmi_device_connected;
  52. struct mdfld_hdmi_i2c *i2c_bus;
  53. struct i2c_adapter *hdmi_i2c_adapter; /* for control functions */
  54. struct drm_device *dev;
  55. };
  56. static void cdv_hdmi_mode_set(struct drm_encoder *encoder,
  57. struct drm_display_mode *mode,
  58. struct drm_display_mode *adjusted_mode)
  59. {
  60. struct drm_device *dev = encoder->dev;
  61. struct psb_intel_encoder *psb_intel_encoder = to_psb_intel_encoder(encoder);
  62. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  63. u32 hdmib;
  64. struct drm_crtc *crtc = encoder->crtc;
  65. struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
  66. hdmib = (2 << 10);
  67. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  68. hdmib |= HDMI_VSYNC_ACTIVE_HIGH;
  69. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  70. hdmib |= HDMI_HSYNC_ACTIVE_HIGH;
  71. if (intel_crtc->pipe == 1)
  72. hdmib |= HDMIB_PIPE_B_SELECT;
  73. if (hdmi_priv->has_hdmi_audio) {
  74. hdmib |= HDMI_AUDIO_ENABLE;
  75. hdmib |= HDMI_NULL_PACKETS_DURING_VSYNC;
  76. }
  77. REG_WRITE(hdmi_priv->hdmi_reg, hdmib);
  78. REG_READ(hdmi_priv->hdmi_reg);
  79. }
  80. static bool cdv_hdmi_mode_fixup(struct drm_encoder *encoder,
  81. struct drm_display_mode *mode,
  82. struct drm_display_mode *adjusted_mode)
  83. {
  84. return true;
  85. }
  86. static void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
  87. {
  88. struct drm_device *dev = encoder->dev;
  89. struct psb_intel_encoder *psb_intel_encoder =
  90. to_psb_intel_encoder(encoder);
  91. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  92. u32 hdmib;
  93. hdmib = REG_READ(hdmi_priv->hdmi_reg);
  94. if (mode != DRM_MODE_DPMS_ON)
  95. REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
  96. else
  97. REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
  98. REG_READ(hdmi_priv->hdmi_reg);
  99. }
  100. static void cdv_hdmi_save(struct drm_connector *connector)
  101. {
  102. struct drm_device *dev = connector->dev;
  103. struct psb_intel_encoder *psb_intel_encoder =
  104. psb_intel_attached_encoder(connector);
  105. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  106. hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
  107. }
  108. static void cdv_hdmi_restore(struct drm_connector *connector)
  109. {
  110. struct drm_device *dev = connector->dev;
  111. struct psb_intel_encoder *psb_intel_encoder =
  112. psb_intel_attached_encoder(connector);
  113. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  114. REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
  115. REG_READ(hdmi_priv->hdmi_reg);
  116. }
  117. static enum drm_connector_status cdv_hdmi_detect(
  118. struct drm_connector *connector, bool force)
  119. {
  120. struct psb_intel_encoder *psb_intel_encoder =
  121. psb_intel_attached_encoder(connector);
  122. struct psb_intel_connector *psb_intel_connector =
  123. to_psb_intel_connector(connector);
  124. struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_encoder->dev_priv;
  125. struct edid *edid = NULL;
  126. enum drm_connector_status status = connector_status_disconnected;
  127. edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
  128. hdmi_priv->has_hdmi_sink = false;
  129. hdmi_priv->has_hdmi_audio = false;
  130. if (edid) {
  131. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  132. status = connector_status_connected;
  133. hdmi_priv->has_hdmi_sink =
  134. drm_detect_hdmi_monitor(edid);
  135. hdmi_priv->has_hdmi_audio =
  136. drm_detect_monitor_audio(edid);
  137. }
  138. psb_intel_connector->base.display_info.raw_edid = NULL;
  139. kfree(edid);
  140. }
  141. return status;
  142. }
  143. static int cdv_hdmi_set_property(struct drm_connector *connector,
  144. struct drm_property *property,
  145. uint64_t value)
  146. {
  147. struct drm_encoder *encoder = connector->encoder;
  148. if (!strcmp(property->name, "scaling mode") && encoder) {
  149. struct psb_intel_crtc *crtc = to_psb_intel_crtc(encoder->crtc);
  150. bool centre;
  151. uint64_t curValue;
  152. if (!crtc)
  153. return -1;
  154. switch (value) {
  155. case DRM_MODE_SCALE_FULLSCREEN:
  156. break;
  157. case DRM_MODE_SCALE_NO_SCALE:
  158. break;
  159. case DRM_MODE_SCALE_ASPECT:
  160. break;
  161. default:
  162. return -1;
  163. }
  164. if (drm_connector_property_get_value(connector,
  165. property, &curValue))
  166. return -1;
  167. if (curValue == value)
  168. return 0;
  169. if (drm_connector_property_set_value(connector,
  170. property, value))
  171. return -1;
  172. centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
  173. (value == DRM_MODE_SCALE_NO_SCALE);
  174. if (crtc->saved_mode.hdisplay != 0 &&
  175. crtc->saved_mode.vdisplay != 0) {
  176. if (centre) {
  177. if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
  178. encoder->crtc->x, encoder->crtc->y, encoder->crtc->fb))
  179. return -1;
  180. } else {
  181. struct drm_encoder_helper_funcs *helpers
  182. = encoder->helper_private;
  183. helpers->mode_set(encoder, &crtc->saved_mode,
  184. &crtc->saved_adjusted_mode);
  185. }
  186. }
  187. }
  188. return 0;
  189. }
  190. /*
  191. * Return the list of HDMI DDC modes if available.
  192. */
  193. static int cdv_hdmi_get_modes(struct drm_connector *connector)
  194. {
  195. struct psb_intel_encoder *psb_intel_encoder =
  196. psb_intel_attached_encoder(connector);
  197. struct edid *edid = NULL;
  198. int ret = 0;
  199. edid = drm_get_edid(connector, &psb_intel_encoder->i2c_bus->adapter);
  200. if (edid) {
  201. drm_mode_connector_update_edid_property(connector, edid);
  202. ret = drm_add_edid_modes(connector, edid);
  203. kfree(edid);
  204. }
  205. return ret;
  206. }
  207. static int cdv_hdmi_mode_valid(struct drm_connector *connector,
  208. struct drm_display_mode *mode)
  209. {
  210. if (mode->clock > 165000)
  211. return MODE_CLOCK_HIGH;
  212. if (mode->clock < 20000)
  213. return MODE_CLOCK_HIGH;
  214. /* just in case */
  215. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  216. return MODE_NO_DBLESCAN;
  217. /* just in case */
  218. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  219. return MODE_NO_INTERLACE;
  220. /*
  221. * FIXME: for now we limit the size to 1680x1050 on CDV, otherwise it
  222. * will go beyond the stolen memory size allocated to the framebuffer
  223. */
  224. if (mode->hdisplay > 1680)
  225. return MODE_PANEL;
  226. if (mode->vdisplay > 1050)
  227. return MODE_PANEL;
  228. return MODE_OK;
  229. }
  230. static void cdv_hdmi_destroy(struct drm_connector *connector)
  231. {
  232. struct psb_intel_encoder *psb_intel_encoder =
  233. psb_intel_attached_encoder(connector);
  234. if (psb_intel_encoder->i2c_bus)
  235. psb_intel_i2c_destroy(psb_intel_encoder->i2c_bus);
  236. drm_sysfs_connector_remove(connector);
  237. drm_connector_cleanup(connector);
  238. kfree(connector);
  239. }
  240. static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
  241. .dpms = cdv_hdmi_dpms,
  242. .mode_fixup = cdv_hdmi_mode_fixup,
  243. .prepare = psb_intel_encoder_prepare,
  244. .mode_set = cdv_hdmi_mode_set,
  245. .commit = psb_intel_encoder_commit,
  246. };
  247. static const struct drm_connector_helper_funcs
  248. cdv_hdmi_connector_helper_funcs = {
  249. .get_modes = cdv_hdmi_get_modes,
  250. .mode_valid = cdv_hdmi_mode_valid,
  251. .best_encoder = psb_intel_best_encoder,
  252. };
  253. static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
  254. .dpms = drm_helper_connector_dpms,
  255. .save = cdv_hdmi_save,
  256. .restore = cdv_hdmi_restore,
  257. .detect = cdv_hdmi_detect,
  258. .fill_modes = drm_helper_probe_single_connector_modes,
  259. .set_property = cdv_hdmi_set_property,
  260. .destroy = cdv_hdmi_destroy,
  261. };
  262. void cdv_hdmi_init(struct drm_device *dev,
  263. struct psb_intel_mode_device *mode_dev, int reg)
  264. {
  265. struct psb_intel_encoder *psb_intel_encoder;
  266. struct psb_intel_connector *psb_intel_connector;
  267. struct drm_connector *connector;
  268. struct drm_encoder *encoder;
  269. struct mid_intel_hdmi_priv *hdmi_priv;
  270. int ddc_bus;
  271. psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder),
  272. GFP_KERNEL);
  273. if (!psb_intel_encoder)
  274. return;
  275. psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector),
  276. GFP_KERNEL);
  277. if (!psb_intel_connector)
  278. goto err_connector;
  279. hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
  280. if (!hdmi_priv)
  281. goto err_priv;
  282. connector = &psb_intel_connector->base;
  283. encoder = &psb_intel_encoder->base;
  284. drm_connector_init(dev, connector,
  285. &cdv_hdmi_connector_funcs,
  286. DRM_MODE_CONNECTOR_DVID);
  287. drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
  288. DRM_MODE_ENCODER_TMDS);
  289. psb_intel_connector_attach_encoder(psb_intel_connector,
  290. psb_intel_encoder);
  291. psb_intel_encoder->type = INTEL_OUTPUT_HDMI;
  292. hdmi_priv->hdmi_reg = reg;
  293. hdmi_priv->has_hdmi_sink = false;
  294. psb_intel_encoder->dev_priv = hdmi_priv;
  295. drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
  296. drm_connector_helper_add(connector,
  297. &cdv_hdmi_connector_helper_funcs);
  298. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  299. connector->interlace_allowed = false;
  300. connector->doublescan_allowed = false;
  301. drm_connector_attach_property(connector,
  302. dev->mode_config.scaling_mode_property,
  303. DRM_MODE_SCALE_FULLSCREEN);
  304. switch (reg) {
  305. case SDVOB:
  306. ddc_bus = GPIOE;
  307. break;
  308. case SDVOC:
  309. ddc_bus = GPIOD;
  310. break;
  311. default:
  312. DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
  313. goto failed_ddc;
  314. break;
  315. }
  316. psb_intel_encoder->i2c_bus = psb_intel_i2c_create(dev,
  317. ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
  318. if (!psb_intel_encoder->i2c_bus) {
  319. dev_err(dev->dev, "No ddc adapter available!\n");
  320. goto failed_ddc;
  321. }
  322. hdmi_priv->hdmi_i2c_adapter =
  323. &(psb_intel_encoder->i2c_bus->adapter);
  324. hdmi_priv->dev = dev;
  325. drm_sysfs_connector_add(connector);
  326. return;
  327. failed_ddc:
  328. drm_encoder_cleanup(encoder);
  329. drm_connector_cleanup(connector);
  330. err_priv:
  331. kfree(psb_intel_connector);
  332. err_connector:
  333. kfree(psb_intel_encoder);
  334. }