omap_crtc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_crtc.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.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <drm/drm_crtc.h>
  22. #include <drm/drm_crtc_helper.h>
  23. #include <drm/drm_mode.h>
  24. #include <drm/drm_plane_helper.h>
  25. #include "omap_drv.h"
  26. #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
  27. struct omap_crtc {
  28. struct drm_crtc base;
  29. const char *name;
  30. enum omap_channel channel;
  31. /*
  32. * Temporary: eventually this will go away, but it is needed
  33. * for now to keep the output's happy. (They only need
  34. * mgr->id.) Eventually this will be replaced w/ something
  35. * more common-panel-framework-y
  36. */
  37. struct omap_overlay_manager *mgr;
  38. struct omap_video_timings timings;
  39. struct omap_drm_irq vblank_irq;
  40. struct omap_drm_irq error_irq;
  41. bool ignore_digit_sync_lost;
  42. bool pending;
  43. wait_queue_head_t pending_wait;
  44. };
  45. /* -----------------------------------------------------------------------------
  46. * Helper Functions
  47. */
  48. uint32_t pipe2vbl(struct drm_crtc *crtc)
  49. {
  50. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  51. return dispc_mgr_get_vsync_irq(omap_crtc->channel);
  52. }
  53. struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
  54. {
  55. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  56. return &omap_crtc->timings;
  57. }
  58. enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
  59. {
  60. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  61. return omap_crtc->channel;
  62. }
  63. int omap_crtc_wait_pending(struct drm_crtc *crtc)
  64. {
  65. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  66. /*
  67. * Timeout is set to a "sufficiently" high value, which should cover
  68. * a single frame refresh even on slower displays.
  69. */
  70. return wait_event_timeout(omap_crtc->pending_wait,
  71. !omap_crtc->pending,
  72. msecs_to_jiffies(250));
  73. }
  74. /* -----------------------------------------------------------------------------
  75. * DSS Manager Functions
  76. */
  77. /*
  78. * Manager-ops, callbacks from output when they need to configure
  79. * the upstream part of the video pipe.
  80. *
  81. * Most of these we can ignore until we add support for command-mode
  82. * panels.. for video-mode the crtc-helpers already do an adequate
  83. * job of sequencing the setup of the video pipe in the proper order
  84. */
  85. /* ovl-mgr-id -> crtc */
  86. static struct omap_crtc *omap_crtcs[8];
  87. /* we can probably ignore these until we support command-mode panels: */
  88. static int omap_crtc_dss_connect(struct omap_overlay_manager *mgr,
  89. struct omap_dss_device *dst)
  90. {
  91. if (mgr->output)
  92. return -EINVAL;
  93. if ((mgr->supported_outputs & dst->id) == 0)
  94. return -EINVAL;
  95. dst->manager = mgr;
  96. mgr->output = dst;
  97. return 0;
  98. }
  99. static void omap_crtc_dss_disconnect(struct omap_overlay_manager *mgr,
  100. struct omap_dss_device *dst)
  101. {
  102. mgr->output->manager = NULL;
  103. mgr->output = NULL;
  104. }
  105. static void omap_crtc_dss_start_update(struct omap_overlay_manager *mgr)
  106. {
  107. }
  108. /* Called only from the encoder enable/disable and suspend/resume handlers. */
  109. static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
  110. {
  111. struct drm_device *dev = crtc->dev;
  112. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  113. enum omap_channel channel = omap_crtc->channel;
  114. struct omap_irq_wait *wait;
  115. u32 framedone_irq, vsync_irq;
  116. int ret;
  117. if (omap_crtc->mgr->output->output_type == OMAP_DISPLAY_TYPE_HDMI) {
  118. dispc_mgr_enable(channel, enable);
  119. return;
  120. }
  121. if (dispc_mgr_is_enabled(channel) == enable)
  122. return;
  123. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  124. /*
  125. * Digit output produces some sync lost interrupts during the
  126. * first frame when enabling, so we need to ignore those.
  127. */
  128. omap_crtc->ignore_digit_sync_lost = true;
  129. }
  130. framedone_irq = dispc_mgr_get_framedone_irq(channel);
  131. vsync_irq = dispc_mgr_get_vsync_irq(channel);
  132. if (enable) {
  133. wait = omap_irq_wait_init(dev, vsync_irq, 1);
  134. } else {
  135. /*
  136. * When we disable the digit output, we need to wait for
  137. * FRAMEDONE to know that DISPC has finished with the output.
  138. *
  139. * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
  140. * that case we need to use vsync interrupt, and wait for both
  141. * even and odd frames.
  142. */
  143. if (framedone_irq)
  144. wait = omap_irq_wait_init(dev, framedone_irq, 1);
  145. else
  146. wait = omap_irq_wait_init(dev, vsync_irq, 2);
  147. }
  148. dispc_mgr_enable(channel, enable);
  149. ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
  150. if (ret) {
  151. dev_err(dev->dev, "%s: timeout waiting for %s\n",
  152. omap_crtc->name, enable ? "enable" : "disable");
  153. }
  154. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  155. omap_crtc->ignore_digit_sync_lost = false;
  156. /* make sure the irq handler sees the value above */
  157. mb();
  158. }
  159. }
  160. static int omap_crtc_dss_enable(struct omap_overlay_manager *mgr)
  161. {
  162. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  163. struct omap_overlay_manager_info info;
  164. memset(&info, 0, sizeof(info));
  165. info.default_color = 0x00000000;
  166. info.trans_key = 0x00000000;
  167. info.trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  168. info.trans_enabled = false;
  169. dispc_mgr_setup(omap_crtc->channel, &info);
  170. dispc_mgr_set_timings(omap_crtc->channel,
  171. &omap_crtc->timings);
  172. omap_crtc_set_enabled(&omap_crtc->base, true);
  173. return 0;
  174. }
  175. static void omap_crtc_dss_disable(struct omap_overlay_manager *mgr)
  176. {
  177. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  178. omap_crtc_set_enabled(&omap_crtc->base, false);
  179. }
  180. static void omap_crtc_dss_set_timings(struct omap_overlay_manager *mgr,
  181. const struct omap_video_timings *timings)
  182. {
  183. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  184. DBG("%s", omap_crtc->name);
  185. omap_crtc->timings = *timings;
  186. }
  187. static void omap_crtc_dss_set_lcd_config(struct omap_overlay_manager *mgr,
  188. const struct dss_lcd_mgr_config *config)
  189. {
  190. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  191. DBG("%s", omap_crtc->name);
  192. dispc_mgr_set_lcd_config(omap_crtc->channel, config);
  193. }
  194. static int omap_crtc_dss_register_framedone(
  195. struct omap_overlay_manager *mgr,
  196. void (*handler)(void *), void *data)
  197. {
  198. return 0;
  199. }
  200. static void omap_crtc_dss_unregister_framedone(
  201. struct omap_overlay_manager *mgr,
  202. void (*handler)(void *), void *data)
  203. {
  204. }
  205. static const struct dss_mgr_ops mgr_ops = {
  206. .connect = omap_crtc_dss_connect,
  207. .disconnect = omap_crtc_dss_disconnect,
  208. .start_update = omap_crtc_dss_start_update,
  209. .enable = omap_crtc_dss_enable,
  210. .disable = omap_crtc_dss_disable,
  211. .set_timings = omap_crtc_dss_set_timings,
  212. .set_lcd_config = omap_crtc_dss_set_lcd_config,
  213. .register_framedone_handler = omap_crtc_dss_register_framedone,
  214. .unregister_framedone_handler = omap_crtc_dss_unregister_framedone,
  215. };
  216. /* -----------------------------------------------------------------------------
  217. * Setup, Flush and Page Flip
  218. */
  219. static void omap_crtc_complete_page_flip(struct drm_crtc *crtc)
  220. {
  221. struct drm_pending_vblank_event *event;
  222. struct drm_device *dev = crtc->dev;
  223. unsigned long flags;
  224. event = crtc->state->event;
  225. if (!event)
  226. return;
  227. spin_lock_irqsave(&dev->event_lock, flags);
  228. list_del(&event->base.link);
  229. /*
  230. * Queue the event for delivery if it's still linked to a file
  231. * handle, otherwise just destroy it.
  232. */
  233. if (event->base.file_priv)
  234. drm_crtc_send_vblank_event(crtc, event);
  235. else
  236. event->base.destroy(&event->base);
  237. spin_unlock_irqrestore(&dev->event_lock, flags);
  238. }
  239. static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  240. {
  241. struct omap_crtc *omap_crtc =
  242. container_of(irq, struct omap_crtc, error_irq);
  243. if (omap_crtc->ignore_digit_sync_lost) {
  244. irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
  245. if (!irqstatus)
  246. return;
  247. }
  248. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
  249. }
  250. static void omap_crtc_vblank_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  251. {
  252. struct omap_crtc *omap_crtc =
  253. container_of(irq, struct omap_crtc, vblank_irq);
  254. struct drm_device *dev = omap_crtc->base.dev;
  255. if (dispc_mgr_go_busy(omap_crtc->channel))
  256. return;
  257. DBG("%s: apply done", omap_crtc->name);
  258. __omap_irq_unregister(dev, &omap_crtc->vblank_irq);
  259. rmb();
  260. WARN_ON(!omap_crtc->pending);
  261. omap_crtc->pending = false;
  262. wmb();
  263. /* wake up userspace */
  264. omap_crtc_complete_page_flip(&omap_crtc->base);
  265. /* wake up omap_atomic_complete */
  266. wake_up(&omap_crtc->pending_wait);
  267. }
  268. /* -----------------------------------------------------------------------------
  269. * CRTC Functions
  270. */
  271. static void omap_crtc_destroy(struct drm_crtc *crtc)
  272. {
  273. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  274. DBG("%s", omap_crtc->name);
  275. WARN_ON(omap_crtc->vblank_irq.registered);
  276. omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
  277. drm_crtc_cleanup(crtc);
  278. kfree(omap_crtc);
  279. }
  280. static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
  281. const struct drm_display_mode *mode,
  282. struct drm_display_mode *adjusted_mode)
  283. {
  284. return true;
  285. }
  286. static void omap_crtc_enable(struct drm_crtc *crtc)
  287. {
  288. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  289. DBG("%s", omap_crtc->name);
  290. rmb();
  291. WARN_ON(omap_crtc->pending);
  292. omap_crtc->pending = true;
  293. wmb();
  294. omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
  295. drm_crtc_vblank_on(crtc);
  296. }
  297. static void omap_crtc_disable(struct drm_crtc *crtc)
  298. {
  299. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  300. DBG("%s", omap_crtc->name);
  301. drm_crtc_vblank_off(crtc);
  302. }
  303. static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
  304. {
  305. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  306. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  307. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  308. omap_crtc->name, mode->base.id, mode->name,
  309. mode->vrefresh, mode->clock,
  310. mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
  311. mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal,
  312. mode->type, mode->flags);
  313. copy_timings_drm_to_omap(&omap_crtc->timings, mode);
  314. }
  315. static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
  316. struct drm_crtc_state *old_crtc_state)
  317. {
  318. }
  319. static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
  320. struct drm_crtc_state *old_crtc_state)
  321. {
  322. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  323. WARN_ON(omap_crtc->vblank_irq.registered);
  324. if (dispc_mgr_is_enabled(omap_crtc->channel)) {
  325. DBG("%s: GO", omap_crtc->name);
  326. rmb();
  327. WARN_ON(omap_crtc->pending);
  328. omap_crtc->pending = true;
  329. wmb();
  330. dispc_mgr_go(omap_crtc->channel);
  331. omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
  332. }
  333. }
  334. static bool omap_crtc_is_plane_prop(struct drm_device *dev,
  335. struct drm_property *property)
  336. {
  337. struct omap_drm_private *priv = dev->dev_private;
  338. return property == priv->zorder_prop ||
  339. property == dev->mode_config.rotation_property;
  340. }
  341. static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
  342. struct drm_crtc_state *state,
  343. struct drm_property *property,
  344. uint64_t val)
  345. {
  346. struct drm_device *dev = crtc->dev;
  347. if (omap_crtc_is_plane_prop(dev, property)) {
  348. struct drm_plane_state *plane_state;
  349. struct drm_plane *plane = crtc->primary;
  350. /*
  351. * Delegate property set to the primary plane. Get the plane
  352. * state and set the property directly.
  353. */
  354. plane_state = drm_atomic_get_plane_state(state->state, plane);
  355. if (IS_ERR(plane_state))
  356. return PTR_ERR(plane_state);
  357. return drm_atomic_plane_set_property(plane, plane_state,
  358. property, val);
  359. }
  360. return -EINVAL;
  361. }
  362. static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
  363. const struct drm_crtc_state *state,
  364. struct drm_property *property,
  365. uint64_t *val)
  366. {
  367. struct drm_device *dev = crtc->dev;
  368. if (omap_crtc_is_plane_prop(dev, property)) {
  369. /*
  370. * Delegate property get to the primary plane. The
  371. * drm_atomic_plane_get_property() function isn't exported, but
  372. * can be called through drm_object_property_get_value() as that
  373. * will call drm_atomic_get_property() for atomic drivers.
  374. */
  375. return drm_object_property_get_value(&crtc->primary->base,
  376. property, val);
  377. }
  378. return -EINVAL;
  379. }
  380. static const struct drm_crtc_funcs omap_crtc_funcs = {
  381. .reset = drm_atomic_helper_crtc_reset,
  382. .set_config = drm_atomic_helper_set_config,
  383. .destroy = omap_crtc_destroy,
  384. .page_flip = drm_atomic_helper_page_flip,
  385. .set_property = drm_atomic_helper_crtc_set_property,
  386. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  387. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  388. .atomic_set_property = omap_crtc_atomic_set_property,
  389. .atomic_get_property = omap_crtc_atomic_get_property,
  390. };
  391. static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
  392. .mode_fixup = omap_crtc_mode_fixup,
  393. .mode_set_nofb = omap_crtc_mode_set_nofb,
  394. .disable = omap_crtc_disable,
  395. .enable = omap_crtc_enable,
  396. .atomic_begin = omap_crtc_atomic_begin,
  397. .atomic_flush = omap_crtc_atomic_flush,
  398. };
  399. /* -----------------------------------------------------------------------------
  400. * Init and Cleanup
  401. */
  402. static const char *channel_names[] = {
  403. [OMAP_DSS_CHANNEL_LCD] = "lcd",
  404. [OMAP_DSS_CHANNEL_DIGIT] = "tv",
  405. [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
  406. [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
  407. };
  408. void omap_crtc_pre_init(void)
  409. {
  410. dss_install_mgr_ops(&mgr_ops);
  411. }
  412. void omap_crtc_pre_uninit(void)
  413. {
  414. dss_uninstall_mgr_ops();
  415. }
  416. /* initialize crtc */
  417. struct drm_crtc *omap_crtc_init(struct drm_device *dev,
  418. struct drm_plane *plane, enum omap_channel channel, int id)
  419. {
  420. struct drm_crtc *crtc = NULL;
  421. struct omap_crtc *omap_crtc;
  422. int ret;
  423. DBG("%s", channel_names[channel]);
  424. omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
  425. if (!omap_crtc)
  426. return NULL;
  427. crtc = &omap_crtc->base;
  428. init_waitqueue_head(&omap_crtc->pending_wait);
  429. omap_crtc->channel = channel;
  430. omap_crtc->name = channel_names[channel];
  431. omap_crtc->vblank_irq.irqmask = pipe2vbl(crtc);
  432. omap_crtc->vblank_irq.irq = omap_crtc_vblank_irq;
  433. omap_crtc->error_irq.irqmask =
  434. dispc_mgr_get_sync_lost_irq(channel);
  435. omap_crtc->error_irq.irq = omap_crtc_error_irq;
  436. omap_irq_register(dev, &omap_crtc->error_irq);
  437. /* temporary: */
  438. omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
  439. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  440. &omap_crtc_funcs, NULL);
  441. if (ret < 0) {
  442. kfree(omap_crtc);
  443. return NULL;
  444. }
  445. drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
  446. omap_plane_install_properties(crtc->primary, &crtc->base);
  447. omap_crtcs[channel] = omap_crtc;
  448. return crtc;
  449. }