cdv_intel_hdmi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 gma_encoder *gma_encoder = to_gma_encoder(encoder);
  63. struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
  64. u32 hdmib;
  65. struct drm_crtc *crtc = encoder->crtc;
  66. struct gma_crtc *gma_crtc = to_gma_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 (gma_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 void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
  82. {
  83. struct drm_device *dev = encoder->dev;
  84. struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
  85. struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
  86. u32 hdmib;
  87. hdmib = REG_READ(hdmi_priv->hdmi_reg);
  88. if (mode != DRM_MODE_DPMS_ON)
  89. REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
  90. else
  91. REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
  92. REG_READ(hdmi_priv->hdmi_reg);
  93. }
  94. static void cdv_hdmi_save(struct drm_connector *connector)
  95. {
  96. struct drm_device *dev = connector->dev;
  97. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  98. struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
  99. hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
  100. }
  101. static void cdv_hdmi_restore(struct drm_connector *connector)
  102. {
  103. struct drm_device *dev = connector->dev;
  104. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  105. struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
  106. REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
  107. REG_READ(hdmi_priv->hdmi_reg);
  108. }
  109. static enum drm_connector_status cdv_hdmi_detect(
  110. struct drm_connector *connector, bool force)
  111. {
  112. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  113. struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
  114. struct edid *edid = NULL;
  115. enum drm_connector_status status = connector_status_disconnected;
  116. edid = drm_get_edid(connector, &gma_encoder->i2c_bus->adapter);
  117. hdmi_priv->has_hdmi_sink = false;
  118. hdmi_priv->has_hdmi_audio = false;
  119. if (edid) {
  120. if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  121. status = connector_status_connected;
  122. hdmi_priv->has_hdmi_sink =
  123. drm_detect_hdmi_monitor(edid);
  124. hdmi_priv->has_hdmi_audio =
  125. drm_detect_monitor_audio(edid);
  126. }
  127. kfree(edid);
  128. }
  129. return status;
  130. }
  131. static int cdv_hdmi_set_property(struct drm_connector *connector,
  132. struct drm_property *property,
  133. uint64_t value)
  134. {
  135. struct drm_encoder *encoder = connector->encoder;
  136. if (!strcmp(property->name, "scaling mode") && encoder) {
  137. struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
  138. bool centre;
  139. uint64_t curValue;
  140. if (!crtc)
  141. return -1;
  142. switch (value) {
  143. case DRM_MODE_SCALE_FULLSCREEN:
  144. break;
  145. case DRM_MODE_SCALE_NO_SCALE:
  146. break;
  147. case DRM_MODE_SCALE_ASPECT:
  148. break;
  149. default:
  150. return -1;
  151. }
  152. if (drm_object_property_get_value(&connector->base,
  153. property, &curValue))
  154. return -1;
  155. if (curValue == value)
  156. return 0;
  157. if (drm_object_property_set_value(&connector->base,
  158. property, value))
  159. return -1;
  160. centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
  161. (value == DRM_MODE_SCALE_NO_SCALE);
  162. if (crtc->saved_mode.hdisplay != 0 &&
  163. crtc->saved_mode.vdisplay != 0) {
  164. if (centre) {
  165. if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
  166. encoder->crtc->x, encoder->crtc->y, encoder->crtc->primary->fb))
  167. return -1;
  168. } else {
  169. const struct drm_encoder_helper_funcs *helpers
  170. = encoder->helper_private;
  171. helpers->mode_set(encoder, &crtc->saved_mode,
  172. &crtc->saved_adjusted_mode);
  173. }
  174. }
  175. }
  176. return 0;
  177. }
  178. /*
  179. * Return the list of HDMI DDC modes if available.
  180. */
  181. static int cdv_hdmi_get_modes(struct drm_connector *connector)
  182. {
  183. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  184. struct edid *edid = NULL;
  185. int ret = 0;
  186. edid = drm_get_edid(connector, &gma_encoder->i2c_bus->adapter);
  187. if (edid) {
  188. drm_mode_connector_update_edid_property(connector, edid);
  189. ret = drm_add_edid_modes(connector, edid);
  190. kfree(edid);
  191. }
  192. return ret;
  193. }
  194. static int cdv_hdmi_mode_valid(struct drm_connector *connector,
  195. struct drm_display_mode *mode)
  196. {
  197. if (mode->clock > 165000)
  198. return MODE_CLOCK_HIGH;
  199. if (mode->clock < 20000)
  200. return MODE_CLOCK_HIGH;
  201. /* just in case */
  202. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  203. return MODE_NO_DBLESCAN;
  204. /* just in case */
  205. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  206. return MODE_NO_INTERLACE;
  207. return MODE_OK;
  208. }
  209. static void cdv_hdmi_destroy(struct drm_connector *connector)
  210. {
  211. struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
  212. if (gma_encoder->i2c_bus)
  213. psb_intel_i2c_destroy(gma_encoder->i2c_bus);
  214. drm_connector_unregister(connector);
  215. drm_connector_cleanup(connector);
  216. kfree(connector);
  217. }
  218. static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
  219. .dpms = cdv_hdmi_dpms,
  220. .mode_fixup = gma_encoder_mode_fixup,
  221. .prepare = gma_encoder_prepare,
  222. .mode_set = cdv_hdmi_mode_set,
  223. .commit = gma_encoder_commit,
  224. };
  225. static const struct drm_connector_helper_funcs
  226. cdv_hdmi_connector_helper_funcs = {
  227. .get_modes = cdv_hdmi_get_modes,
  228. .mode_valid = cdv_hdmi_mode_valid,
  229. .best_encoder = gma_best_encoder,
  230. };
  231. static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
  232. .dpms = drm_helper_connector_dpms,
  233. .save = cdv_hdmi_save,
  234. .restore = cdv_hdmi_restore,
  235. .detect = cdv_hdmi_detect,
  236. .fill_modes = drm_helper_probe_single_connector_modes,
  237. .set_property = cdv_hdmi_set_property,
  238. .destroy = cdv_hdmi_destroy,
  239. };
  240. void cdv_hdmi_init(struct drm_device *dev,
  241. struct psb_intel_mode_device *mode_dev, int reg)
  242. {
  243. struct gma_encoder *gma_encoder;
  244. struct gma_connector *gma_connector;
  245. struct drm_connector *connector;
  246. struct drm_encoder *encoder;
  247. struct mid_intel_hdmi_priv *hdmi_priv;
  248. int ddc_bus;
  249. gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
  250. if (!gma_encoder)
  251. return;
  252. gma_connector = kzalloc(sizeof(struct gma_connector),
  253. GFP_KERNEL);
  254. if (!gma_connector)
  255. goto err_connector;
  256. hdmi_priv = kzalloc(sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
  257. if (!hdmi_priv)
  258. goto err_priv;
  259. connector = &gma_connector->base;
  260. connector->polled = DRM_CONNECTOR_POLL_HPD;
  261. encoder = &gma_encoder->base;
  262. drm_connector_init(dev, connector,
  263. &cdv_hdmi_connector_funcs,
  264. DRM_MODE_CONNECTOR_DVID);
  265. drm_encoder_init(dev, encoder, &psb_intel_lvds_enc_funcs,
  266. DRM_MODE_ENCODER_TMDS);
  267. gma_connector_attach_encoder(gma_connector, gma_encoder);
  268. gma_encoder->type = INTEL_OUTPUT_HDMI;
  269. hdmi_priv->hdmi_reg = reg;
  270. hdmi_priv->has_hdmi_sink = false;
  271. gma_encoder->dev_priv = hdmi_priv;
  272. drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
  273. drm_connector_helper_add(connector,
  274. &cdv_hdmi_connector_helper_funcs);
  275. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  276. connector->interlace_allowed = false;
  277. connector->doublescan_allowed = false;
  278. drm_object_attach_property(&connector->base,
  279. dev->mode_config.scaling_mode_property,
  280. DRM_MODE_SCALE_FULLSCREEN);
  281. switch (reg) {
  282. case SDVOB:
  283. ddc_bus = GPIOE;
  284. gma_encoder->ddi_select = DDI0_SELECT;
  285. break;
  286. case SDVOC:
  287. ddc_bus = GPIOD;
  288. gma_encoder->ddi_select = DDI1_SELECT;
  289. break;
  290. default:
  291. DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
  292. goto failed_ddc;
  293. break;
  294. }
  295. gma_encoder->i2c_bus = psb_intel_i2c_create(dev,
  296. ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
  297. if (!gma_encoder->i2c_bus) {
  298. dev_err(dev->dev, "No ddc adapter available!\n");
  299. goto failed_ddc;
  300. }
  301. hdmi_priv->hdmi_i2c_adapter = &(gma_encoder->i2c_bus->adapter);
  302. hdmi_priv->dev = dev;
  303. drm_connector_register(connector);
  304. return;
  305. failed_ddc:
  306. drm_encoder_cleanup(encoder);
  307. drm_connector_cleanup(connector);
  308. err_priv:
  309. kfree(gma_connector);
  310. err_connector:
  311. kfree(gma_encoder);
  312. }