omap_connector.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_connector.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include "omap_drv.h"
  23. /*
  24. * connector funcs
  25. */
  26. #define to_omap_connector(x) container_of(x, struct omap_connector, base)
  27. struct omap_connector {
  28. struct drm_connector base;
  29. struct omap_dss_device *dssdev;
  30. struct drm_encoder *encoder;
  31. bool hdmi_mode;
  32. };
  33. bool omap_connector_get_hdmi_mode(struct drm_connector *connector)
  34. {
  35. struct omap_connector *omap_connector = to_omap_connector(connector);
  36. return omap_connector->hdmi_mode;
  37. }
  38. void copy_timings_omap_to_drm(struct drm_display_mode *mode,
  39. struct omap_video_timings *timings)
  40. {
  41. mode->clock = timings->pixelclock / 1000;
  42. mode->hdisplay = timings->x_res;
  43. mode->hsync_start = mode->hdisplay + timings->hfp;
  44. mode->hsync_end = mode->hsync_start + timings->hsw;
  45. mode->htotal = mode->hsync_end + timings->hbp;
  46. mode->vdisplay = timings->y_res;
  47. mode->vsync_start = mode->vdisplay + timings->vfp;
  48. mode->vsync_end = mode->vsync_start + timings->vsw;
  49. mode->vtotal = mode->vsync_end + timings->vbp;
  50. mode->flags = 0;
  51. if (timings->interlace)
  52. mode->flags |= DRM_MODE_FLAG_INTERLACE;
  53. if (timings->hsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
  54. mode->flags |= DRM_MODE_FLAG_PHSYNC;
  55. else
  56. mode->flags |= DRM_MODE_FLAG_NHSYNC;
  57. if (timings->vsync_level == OMAPDSS_SIG_ACTIVE_HIGH)
  58. mode->flags |= DRM_MODE_FLAG_PVSYNC;
  59. else
  60. mode->flags |= DRM_MODE_FLAG_NVSYNC;
  61. }
  62. void copy_timings_drm_to_omap(struct omap_video_timings *timings,
  63. struct drm_display_mode *mode)
  64. {
  65. timings->pixelclock = mode->clock * 1000;
  66. timings->x_res = mode->hdisplay;
  67. timings->hfp = mode->hsync_start - mode->hdisplay;
  68. timings->hsw = mode->hsync_end - mode->hsync_start;
  69. timings->hbp = mode->htotal - mode->hsync_end;
  70. timings->y_res = mode->vdisplay;
  71. timings->vfp = mode->vsync_start - mode->vdisplay;
  72. timings->vsw = mode->vsync_end - mode->vsync_start;
  73. timings->vbp = mode->vtotal - mode->vsync_end;
  74. timings->interlace = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
  75. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  76. timings->hsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
  77. else
  78. timings->hsync_level = OMAPDSS_SIG_ACTIVE_LOW;
  79. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  80. timings->vsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
  81. else
  82. timings->vsync_level = OMAPDSS_SIG_ACTIVE_LOW;
  83. timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
  84. timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
  85. timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE;
  86. }
  87. static enum drm_connector_status omap_connector_detect(
  88. struct drm_connector *connector, bool force)
  89. {
  90. struct omap_connector *omap_connector = to_omap_connector(connector);
  91. struct omap_dss_device *dssdev = omap_connector->dssdev;
  92. struct omap_dss_driver *dssdrv = dssdev->driver;
  93. enum drm_connector_status ret;
  94. if (dssdrv->detect) {
  95. if (dssdrv->detect(dssdev))
  96. ret = connector_status_connected;
  97. else
  98. ret = connector_status_disconnected;
  99. } else if (dssdev->type == OMAP_DISPLAY_TYPE_DPI ||
  100. dssdev->type == OMAP_DISPLAY_TYPE_DBI ||
  101. dssdev->type == OMAP_DISPLAY_TYPE_SDI ||
  102. dssdev->type == OMAP_DISPLAY_TYPE_DSI) {
  103. ret = connector_status_connected;
  104. } else {
  105. ret = connector_status_unknown;
  106. }
  107. VERB("%s: %d (force=%d)", omap_connector->dssdev->name, ret, force);
  108. return ret;
  109. }
  110. static void omap_connector_destroy(struct drm_connector *connector)
  111. {
  112. struct omap_connector *omap_connector = to_omap_connector(connector);
  113. struct omap_dss_device *dssdev = omap_connector->dssdev;
  114. DBG("%s", omap_connector->dssdev->name);
  115. drm_connector_unregister(connector);
  116. drm_connector_cleanup(connector);
  117. kfree(omap_connector);
  118. omap_dss_put_device(dssdev);
  119. }
  120. #define MAX_EDID 512
  121. static int omap_connector_get_modes(struct drm_connector *connector)
  122. {
  123. struct omap_connector *omap_connector = to_omap_connector(connector);
  124. struct omap_dss_device *dssdev = omap_connector->dssdev;
  125. struct omap_dss_driver *dssdrv = dssdev->driver;
  126. struct drm_device *dev = connector->dev;
  127. int n = 0;
  128. DBG("%s", omap_connector->dssdev->name);
  129. /* if display exposes EDID, then we parse that in the normal way to
  130. * build table of supported modes.. otherwise (ie. fixed resolution
  131. * LCD panels) we just return a single mode corresponding to the
  132. * currently configured timings:
  133. */
  134. if (dssdrv->read_edid) {
  135. void *edid = kzalloc(MAX_EDID, GFP_KERNEL);
  136. if ((dssdrv->read_edid(dssdev, edid, MAX_EDID) > 0) &&
  137. drm_edid_is_valid(edid)) {
  138. drm_mode_connector_update_edid_property(
  139. connector, edid);
  140. n = drm_add_edid_modes(connector, edid);
  141. omap_connector->hdmi_mode =
  142. drm_detect_hdmi_monitor(edid);
  143. } else {
  144. drm_mode_connector_update_edid_property(
  145. connector, NULL);
  146. }
  147. kfree(edid);
  148. } else {
  149. struct drm_display_mode *mode = drm_mode_create(dev);
  150. struct omap_video_timings timings = {0};
  151. dssdrv->get_timings(dssdev, &timings);
  152. copy_timings_omap_to_drm(mode, &timings);
  153. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  154. drm_mode_set_name(mode);
  155. drm_mode_probed_add(connector, mode);
  156. n = 1;
  157. }
  158. return n;
  159. }
  160. static int omap_connector_mode_valid(struct drm_connector *connector,
  161. struct drm_display_mode *mode)
  162. {
  163. struct omap_connector *omap_connector = to_omap_connector(connector);
  164. struct omap_dss_device *dssdev = omap_connector->dssdev;
  165. struct omap_dss_driver *dssdrv = dssdev->driver;
  166. struct omap_video_timings timings = {0};
  167. struct drm_device *dev = connector->dev;
  168. struct drm_display_mode *new_mode;
  169. int r, ret = MODE_BAD;
  170. copy_timings_drm_to_omap(&timings, mode);
  171. mode->vrefresh = drm_mode_vrefresh(mode);
  172. /*
  173. * if the panel driver doesn't have a check_timings, it's most likely
  174. * a fixed resolution panel, check if the timings match with the
  175. * panel's timings
  176. */
  177. if (dssdrv->check_timings) {
  178. r = dssdrv->check_timings(dssdev, &timings);
  179. } else {
  180. struct omap_video_timings t = {0};
  181. dssdrv->get_timings(dssdev, &t);
  182. if (memcmp(&timings, &t, sizeof(struct omap_video_timings)))
  183. r = -EINVAL;
  184. else
  185. r = 0;
  186. }
  187. if (!r) {
  188. /* check if vrefresh is still valid */
  189. new_mode = drm_mode_duplicate(dev, mode);
  190. new_mode->clock = timings.pixelclock / 1000;
  191. new_mode->vrefresh = 0;
  192. if (mode->vrefresh == drm_mode_vrefresh(new_mode))
  193. ret = MODE_OK;
  194. drm_mode_destroy(dev, new_mode);
  195. }
  196. DBG("connector: mode %s: "
  197. "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  198. (ret == MODE_OK) ? "valid" : "invalid",
  199. mode->base.id, mode->name, mode->vrefresh, mode->clock,
  200. mode->hdisplay, mode->hsync_start,
  201. mode->hsync_end, mode->htotal,
  202. mode->vdisplay, mode->vsync_start,
  203. mode->vsync_end, mode->vtotal, mode->type, mode->flags);
  204. return ret;
  205. }
  206. struct drm_encoder *omap_connector_attached_encoder(
  207. struct drm_connector *connector)
  208. {
  209. struct omap_connector *omap_connector = to_omap_connector(connector);
  210. return omap_connector->encoder;
  211. }
  212. static const struct drm_connector_funcs omap_connector_funcs = {
  213. .dpms = drm_atomic_helper_connector_dpms,
  214. .reset = drm_atomic_helper_connector_reset,
  215. .detect = omap_connector_detect,
  216. .fill_modes = drm_helper_probe_single_connector_modes,
  217. .destroy = omap_connector_destroy,
  218. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  219. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  220. };
  221. static const struct drm_connector_helper_funcs omap_connector_helper_funcs = {
  222. .get_modes = omap_connector_get_modes,
  223. .mode_valid = omap_connector_mode_valid,
  224. .best_encoder = omap_connector_attached_encoder,
  225. };
  226. /* initialize connector */
  227. struct drm_connector *omap_connector_init(struct drm_device *dev,
  228. int connector_type, struct omap_dss_device *dssdev,
  229. struct drm_encoder *encoder)
  230. {
  231. struct drm_connector *connector = NULL;
  232. struct omap_connector *omap_connector;
  233. DBG("%s", dssdev->name);
  234. omap_dss_get_device(dssdev);
  235. omap_connector = kzalloc(sizeof(struct omap_connector), GFP_KERNEL);
  236. if (!omap_connector)
  237. goto fail;
  238. omap_connector->dssdev = dssdev;
  239. omap_connector->encoder = encoder;
  240. connector = &omap_connector->base;
  241. drm_connector_init(dev, connector, &omap_connector_funcs,
  242. connector_type);
  243. drm_connector_helper_add(connector, &omap_connector_helper_funcs);
  244. #if 0 /* enable when dss2 supports hotplug */
  245. if (dssdev->caps & OMAP_DSS_DISPLAY_CAP_HPD)
  246. connector->polled = 0;
  247. else
  248. #endif
  249. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  250. DRM_CONNECTOR_POLL_DISCONNECT;
  251. connector->interlace_allowed = 1;
  252. connector->doublescan_allowed = 0;
  253. drm_connector_register(connector);
  254. return connector;
  255. fail:
  256. if (connector)
  257. omap_connector_destroy(connector);
  258. return NULL;
  259. }