omap_crtc.c 14 KB

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