omap_crtc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 <linux/completion.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_mode.h>
  23. #include <drm/drm_plane_helper.h>
  24. #include "omap_drv.h"
  25. #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
  26. enum omap_page_flip_state {
  27. OMAP_PAGE_FLIP_IDLE,
  28. OMAP_PAGE_FLIP_WAIT,
  29. OMAP_PAGE_FLIP_QUEUED,
  30. OMAP_PAGE_FLIP_CANCELLED,
  31. };
  32. struct omap_crtc {
  33. struct drm_crtc base;
  34. const char *name;
  35. int pipe;
  36. enum omap_channel channel;
  37. struct omap_overlay_manager_info info;
  38. struct drm_encoder *current_encoder;
  39. /*
  40. * Temporary: eventually this will go away, but it is needed
  41. * for now to keep the output's happy. (They only need
  42. * mgr->id.) Eventually this will be replaced w/ something
  43. * more common-panel-framework-y
  44. */
  45. struct omap_overlay_manager *mgr;
  46. struct omap_video_timings timings;
  47. bool enabled;
  48. struct omap_drm_irq vblank_irq;
  49. struct omap_drm_irq error_irq;
  50. /* list of framebuffers to unpin */
  51. struct list_head pending_unpins;
  52. /*
  53. * flip_state flag indicates the current page flap state: IDLE if no
  54. * page queue has been submitted, WAIT when waiting for GEM async
  55. * completion, QUEUED when the page flip has been queued to the hardware
  56. * or CANCELLED when the CRTC is turned off before the flip gets queued
  57. * to the hardware. The flip event, if any, is stored in flip_event, and
  58. * the framebuffer queued for page flip is stored in flip_fb. The
  59. * flip_wait wait queue is used to wait for page flip completion.
  60. *
  61. * The flip_work work queue handles page flip requests without caring
  62. * about what context the GEM async callback is called from. Possibly we
  63. * should just make omap_gem always call the cb from the worker so we
  64. * don't have to care about this.
  65. */
  66. enum omap_page_flip_state flip_state;
  67. struct drm_pending_vblank_event *flip_event;
  68. struct drm_framebuffer *flip_fb;
  69. wait_queue_head_t flip_wait;
  70. struct work_struct flip_work;
  71. struct completion completion;
  72. bool ignore_digit_sync_lost;
  73. };
  74. struct omap_framebuffer_unpin {
  75. struct list_head list;
  76. struct drm_framebuffer *fb;
  77. };
  78. /* -----------------------------------------------------------------------------
  79. * Helper Functions
  80. */
  81. uint32_t pipe2vbl(struct drm_crtc *crtc)
  82. {
  83. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  84. return dispc_mgr_get_vsync_irq(omap_crtc->channel);
  85. }
  86. const struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
  87. {
  88. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  89. return &omap_crtc->timings;
  90. }
  91. enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
  92. {
  93. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  94. return omap_crtc->channel;
  95. }
  96. /* -----------------------------------------------------------------------------
  97. * DSS Manager Functions
  98. */
  99. /*
  100. * Manager-ops, callbacks from output when they need to configure
  101. * the upstream part of the video pipe.
  102. *
  103. * Most of these we can ignore until we add support for command-mode
  104. * panels.. for video-mode the crtc-helpers already do an adequate
  105. * job of sequencing the setup of the video pipe in the proper order
  106. */
  107. /* ovl-mgr-id -> crtc */
  108. static struct omap_crtc *omap_crtcs[8];
  109. /* we can probably ignore these until we support command-mode panels: */
  110. static int omap_crtc_dss_connect(struct omap_overlay_manager *mgr,
  111. struct omap_dss_device *dst)
  112. {
  113. if (mgr->output)
  114. return -EINVAL;
  115. if ((mgr->supported_outputs & dst->id) == 0)
  116. return -EINVAL;
  117. dst->manager = mgr;
  118. mgr->output = dst;
  119. return 0;
  120. }
  121. static void omap_crtc_dss_disconnect(struct omap_overlay_manager *mgr,
  122. struct omap_dss_device *dst)
  123. {
  124. mgr->output->manager = NULL;
  125. mgr->output = NULL;
  126. }
  127. static void omap_crtc_dss_start_update(struct omap_overlay_manager *mgr)
  128. {
  129. }
  130. /* Called only from omap_crtc_setup and suspend/resume handlers. */
  131. static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
  132. {
  133. struct drm_device *dev = crtc->dev;
  134. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  135. enum omap_channel channel = omap_crtc->channel;
  136. struct omap_irq_wait *wait;
  137. u32 framedone_irq, vsync_irq;
  138. int ret;
  139. if (dispc_mgr_is_enabled(channel) == enable)
  140. return;
  141. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  142. /*
  143. * Digit output produces some sync lost interrupts during the
  144. * first frame when enabling, so we need to ignore those.
  145. */
  146. omap_crtc->ignore_digit_sync_lost = true;
  147. }
  148. framedone_irq = dispc_mgr_get_framedone_irq(channel);
  149. vsync_irq = dispc_mgr_get_vsync_irq(channel);
  150. if (enable) {
  151. wait = omap_irq_wait_init(dev, vsync_irq, 1);
  152. } else {
  153. /*
  154. * When we disable the digit output, we need to wait for
  155. * FRAMEDONE to know that DISPC has finished with the output.
  156. *
  157. * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
  158. * that case we need to use vsync interrupt, and wait for both
  159. * even and odd frames.
  160. */
  161. if (framedone_irq)
  162. wait = omap_irq_wait_init(dev, framedone_irq, 1);
  163. else
  164. wait = omap_irq_wait_init(dev, vsync_irq, 2);
  165. }
  166. dispc_mgr_enable(channel, enable);
  167. ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
  168. if (ret) {
  169. dev_err(dev->dev, "%s: timeout waiting for %s\n",
  170. omap_crtc->name, enable ? "enable" : "disable");
  171. }
  172. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  173. omap_crtc->ignore_digit_sync_lost = false;
  174. /* make sure the irq handler sees the value above */
  175. mb();
  176. }
  177. }
  178. static int omap_crtc_dss_enable(struct omap_overlay_manager *mgr)
  179. {
  180. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  181. dispc_mgr_setup(omap_crtc->channel, &omap_crtc->info);
  182. dispc_mgr_set_timings(omap_crtc->channel,
  183. &omap_crtc->timings);
  184. omap_crtc_set_enabled(&omap_crtc->base, true);
  185. return 0;
  186. }
  187. static void omap_crtc_dss_disable(struct omap_overlay_manager *mgr)
  188. {
  189. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  190. omap_crtc_set_enabled(&omap_crtc->base, false);
  191. }
  192. static void omap_crtc_dss_set_timings(struct omap_overlay_manager *mgr,
  193. const struct omap_video_timings *timings)
  194. {
  195. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  196. DBG("%s", omap_crtc->name);
  197. omap_crtc->timings = *timings;
  198. }
  199. static void omap_crtc_dss_set_lcd_config(struct omap_overlay_manager *mgr,
  200. const struct dss_lcd_mgr_config *config)
  201. {
  202. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  203. DBG("%s", omap_crtc->name);
  204. dispc_mgr_set_lcd_config(omap_crtc->channel, config);
  205. }
  206. static int omap_crtc_dss_register_framedone(
  207. struct omap_overlay_manager *mgr,
  208. void (*handler)(void *), void *data)
  209. {
  210. return 0;
  211. }
  212. static void omap_crtc_dss_unregister_framedone(
  213. struct omap_overlay_manager *mgr,
  214. void (*handler)(void *), void *data)
  215. {
  216. }
  217. static const struct dss_mgr_ops mgr_ops = {
  218. .connect = omap_crtc_dss_connect,
  219. .disconnect = omap_crtc_dss_disconnect,
  220. .start_update = omap_crtc_dss_start_update,
  221. .enable = omap_crtc_dss_enable,
  222. .disable = omap_crtc_dss_disable,
  223. .set_timings = omap_crtc_dss_set_timings,
  224. .set_lcd_config = omap_crtc_dss_set_lcd_config,
  225. .register_framedone_handler = omap_crtc_dss_register_framedone,
  226. .unregister_framedone_handler = omap_crtc_dss_unregister_framedone,
  227. };
  228. /* -----------------------------------------------------------------------------
  229. * Setup, Flush and Page Flip
  230. */
  231. void omap_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  232. {
  233. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  234. struct drm_device *dev = crtc->dev;
  235. unsigned long flags;
  236. spin_lock_irqsave(&dev->event_lock, flags);
  237. /* Only complete events queued for our file handle. */
  238. if (omap_crtc->flip_event &&
  239. file == omap_crtc->flip_event->base.file_priv) {
  240. drm_send_vblank_event(dev, omap_crtc->pipe,
  241. omap_crtc->flip_event);
  242. omap_crtc->flip_event = NULL;
  243. }
  244. spin_unlock_irqrestore(&dev->event_lock, flags);
  245. }
  246. /* Must be called with dev->event_lock locked. */
  247. static void omap_crtc_complete_page_flip(struct drm_crtc *crtc,
  248. enum omap_page_flip_state state)
  249. {
  250. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  251. struct drm_device *dev = crtc->dev;
  252. if (omap_crtc->flip_event) {
  253. drm_send_vblank_event(dev, omap_crtc->pipe,
  254. omap_crtc->flip_event);
  255. omap_crtc->flip_event = NULL;
  256. }
  257. omap_crtc->flip_state = state;
  258. if (state == OMAP_PAGE_FLIP_IDLE)
  259. wake_up(&omap_crtc->flip_wait);
  260. }
  261. static bool omap_crtc_page_flip_pending(struct drm_crtc *crtc)
  262. {
  263. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  264. struct drm_device *dev = crtc->dev;
  265. unsigned long flags;
  266. bool pending;
  267. spin_lock_irqsave(&dev->event_lock, flags);
  268. pending = omap_crtc->flip_state != OMAP_PAGE_FLIP_IDLE;
  269. spin_unlock_irqrestore(&dev->event_lock, flags);
  270. return pending;
  271. }
  272. static void omap_crtc_wait_page_flip(struct drm_crtc *crtc)
  273. {
  274. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  275. struct drm_device *dev = crtc->dev;
  276. bool cancelled = false;
  277. unsigned long flags;
  278. /*
  279. * If we're still waiting for the GEM async operation to complete just
  280. * cancel the page flip, as we're holding the CRTC mutex preventing the
  281. * page flip work handler from queueing the page flip.
  282. *
  283. * We can't release the reference to the frame buffer here as the async
  284. * operation doesn't keep its own reference to the buffer. We'll just
  285. * let the page flip work queue handle that.
  286. */
  287. spin_lock_irqsave(&dev->event_lock, flags);
  288. if (omap_crtc->flip_state == OMAP_PAGE_FLIP_WAIT) {
  289. omap_crtc_complete_page_flip(crtc, OMAP_PAGE_FLIP_CANCELLED);
  290. cancelled = true;
  291. }
  292. spin_unlock_irqrestore(&dev->event_lock, flags);
  293. if (cancelled)
  294. return;
  295. if (wait_event_timeout(omap_crtc->flip_wait,
  296. !omap_crtc_page_flip_pending(crtc),
  297. msecs_to_jiffies(50)))
  298. return;
  299. dev_warn(crtc->dev->dev, "page flip timeout!\n");
  300. spin_lock_irqsave(&dev->event_lock, flags);
  301. omap_crtc_complete_page_flip(crtc, OMAP_PAGE_FLIP_IDLE);
  302. spin_unlock_irqrestore(&dev->event_lock, flags);
  303. }
  304. static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  305. {
  306. struct omap_crtc *omap_crtc =
  307. container_of(irq, struct omap_crtc, error_irq);
  308. if (omap_crtc->ignore_digit_sync_lost) {
  309. irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
  310. if (!irqstatus)
  311. return;
  312. }
  313. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
  314. }
  315. static void omap_crtc_vblank_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  316. {
  317. struct omap_crtc *omap_crtc =
  318. container_of(irq, struct omap_crtc, vblank_irq);
  319. struct drm_device *dev = omap_crtc->base.dev;
  320. unsigned long flags;
  321. if (dispc_mgr_go_busy(omap_crtc->channel))
  322. return;
  323. DBG("%s: apply done", omap_crtc->name);
  324. __omap_irq_unregister(dev, &omap_crtc->vblank_irq);
  325. /* wakeup userspace */
  326. spin_lock_irqsave(&dev->event_lock, flags);
  327. omap_crtc_complete_page_flip(&omap_crtc->base, OMAP_PAGE_FLIP_IDLE);
  328. spin_unlock_irqrestore(&dev->event_lock, flags);
  329. complete(&omap_crtc->completion);
  330. }
  331. int omap_crtc_flush(struct drm_crtc *crtc)
  332. {
  333. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  334. struct omap_framebuffer_unpin *fb, *next;
  335. DBG("%s: GO", omap_crtc->name);
  336. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  337. WARN_ON(omap_crtc->vblank_irq.registered);
  338. dispc_runtime_get();
  339. if (dispc_mgr_is_enabled(omap_crtc->channel)) {
  340. dispc_mgr_go(omap_crtc->channel);
  341. omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
  342. WARN_ON(!wait_for_completion_timeout(&omap_crtc->completion,
  343. msecs_to_jiffies(100)));
  344. reinit_completion(&omap_crtc->completion);
  345. }
  346. dispc_runtime_put();
  347. /* Unpin and unreference pending framebuffers. */
  348. list_for_each_entry_safe(fb, next, &omap_crtc->pending_unpins, list) {
  349. omap_framebuffer_unpin(fb->fb);
  350. drm_framebuffer_unreference(fb->fb);
  351. list_del(&fb->list);
  352. kfree(fb);
  353. }
  354. return 0;
  355. }
  356. int omap_crtc_queue_unpin(struct drm_crtc *crtc, struct drm_framebuffer *fb)
  357. {
  358. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  359. struct omap_framebuffer_unpin *unpin;
  360. unpin = kzalloc(sizeof(*unpin), GFP_KERNEL);
  361. if (!unpin)
  362. return -ENOMEM;
  363. unpin->fb = fb;
  364. list_add_tail(&unpin->list, &omap_crtc->pending_unpins);
  365. return 0;
  366. }
  367. static void omap_crtc_setup(struct drm_crtc *crtc)
  368. {
  369. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  370. struct omap_drm_private *priv = crtc->dev->dev_private;
  371. struct drm_encoder *encoder = NULL;
  372. unsigned int i;
  373. DBG("%s: enabled=%d", omap_crtc->name, omap_crtc->enabled);
  374. dispc_runtime_get();
  375. for (i = 0; i < priv->num_encoders; i++) {
  376. if (priv->encoders[i]->crtc == crtc) {
  377. encoder = priv->encoders[i];
  378. break;
  379. }
  380. }
  381. if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
  382. omap_encoder_set_enabled(omap_crtc->current_encoder, false);
  383. omap_crtc->current_encoder = encoder;
  384. if (!omap_crtc->enabled) {
  385. if (encoder)
  386. omap_encoder_set_enabled(encoder, false);
  387. } else {
  388. if (encoder) {
  389. omap_encoder_set_enabled(encoder, false);
  390. omap_encoder_update(encoder, omap_crtc->mgr,
  391. &omap_crtc->timings);
  392. omap_encoder_set_enabled(encoder, true);
  393. }
  394. }
  395. dispc_runtime_put();
  396. }
  397. /* -----------------------------------------------------------------------------
  398. * CRTC Functions
  399. */
  400. static void omap_crtc_destroy(struct drm_crtc *crtc)
  401. {
  402. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  403. DBG("%s", omap_crtc->name);
  404. WARN_ON(omap_crtc->vblank_irq.registered);
  405. omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
  406. drm_crtc_cleanup(crtc);
  407. kfree(omap_crtc);
  408. }
  409. static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
  410. const struct drm_display_mode *mode,
  411. struct drm_display_mode *adjusted_mode)
  412. {
  413. return true;
  414. }
  415. static void omap_crtc_enable(struct drm_crtc *crtc)
  416. {
  417. struct omap_drm_private *priv = crtc->dev->dev_private;
  418. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  419. unsigned int i;
  420. DBG("%s", omap_crtc->name);
  421. if (omap_crtc->enabled)
  422. return;
  423. /* Enable all planes associated with the CRTC. */
  424. for (i = 0; i < priv->num_planes; i++) {
  425. struct drm_plane *plane = priv->planes[i];
  426. if (plane->crtc == crtc)
  427. WARN_ON(omap_plane_set_enable(plane, true));
  428. }
  429. omap_crtc->enabled = true;
  430. omap_crtc_setup(crtc);
  431. omap_crtc_flush(crtc);
  432. dispc_runtime_get();
  433. drm_crtc_vblank_on(crtc);
  434. dispc_runtime_put();
  435. }
  436. static void omap_crtc_disable(struct drm_crtc *crtc)
  437. {
  438. struct omap_drm_private *priv = crtc->dev->dev_private;
  439. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  440. unsigned int i;
  441. DBG("%s", omap_crtc->name);
  442. if (!omap_crtc->enabled)
  443. return;
  444. omap_crtc_wait_page_flip(crtc);
  445. dispc_runtime_get();
  446. drm_crtc_vblank_off(crtc);
  447. dispc_runtime_put();
  448. /* Disable all planes associated with the CRTC. */
  449. for (i = 0; i < priv->num_planes; i++) {
  450. struct drm_plane *plane = priv->planes[i];
  451. if (plane->crtc == crtc)
  452. WARN_ON(omap_plane_set_enable(plane, false));
  453. }
  454. omap_crtc->enabled = false;
  455. omap_crtc_setup(crtc);
  456. omap_crtc_flush(crtc);
  457. }
  458. static int omap_crtc_mode_set(struct drm_crtc *crtc,
  459. struct drm_display_mode *mode,
  460. struct drm_display_mode *adjusted_mode,
  461. int x, int y,
  462. struct drm_framebuffer *old_fb)
  463. {
  464. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  465. mode = adjusted_mode;
  466. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  467. omap_crtc->name, mode->base.id, mode->name,
  468. mode->vrefresh, mode->clock,
  469. mode->hdisplay, mode->hsync_start,
  470. mode->hsync_end, mode->htotal,
  471. mode->vdisplay, mode->vsync_start,
  472. mode->vsync_end, mode->vtotal,
  473. mode->type, mode->flags);
  474. copy_timings_drm_to_omap(&omap_crtc->timings, mode);
  475. /*
  476. * The primary plane CRTC can be reset if the plane is disabled directly
  477. * through the universal plane API. Set it again here.
  478. */
  479. crtc->primary->crtc = crtc;
  480. return omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
  481. 0, 0, mode->hdisplay, mode->vdisplay,
  482. x, y, mode->hdisplay, mode->vdisplay);
  483. }
  484. static void omap_crtc_dpms(struct drm_crtc *crtc, int mode)
  485. {
  486. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  487. bool enable = (mode == DRM_MODE_DPMS_ON);
  488. DBG("%s: %d", omap_crtc->name, mode);
  489. if (enable)
  490. omap_crtc_enable(crtc);
  491. else
  492. omap_crtc_disable(crtc);
  493. }
  494. static void omap_crtc_prepare(struct drm_crtc *crtc)
  495. {
  496. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  497. DBG("%s", omap_crtc->name);
  498. omap_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  499. }
  500. static void omap_crtc_commit(struct drm_crtc *crtc)
  501. {
  502. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  503. DBG("%s", omap_crtc->name);
  504. omap_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  505. }
  506. static int omap_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  507. struct drm_framebuffer *old_fb)
  508. {
  509. struct drm_plane *plane = crtc->primary;
  510. struct drm_display_mode *mode = &crtc->mode;
  511. int ret;
  512. ret = omap_plane_mode_set(plane, crtc, crtc->primary->fb,
  513. 0, 0, mode->hdisplay, mode->vdisplay,
  514. x, y, mode->hdisplay, mode->vdisplay);
  515. if (ret < 0)
  516. return ret;
  517. return omap_crtc_flush(crtc);
  518. }
  519. static void page_flip_worker(struct work_struct *work)
  520. {
  521. struct omap_crtc *omap_crtc =
  522. container_of(work, struct omap_crtc, flip_work);
  523. struct drm_crtc *crtc = &omap_crtc->base;
  524. struct drm_display_mode *mode = &crtc->mode;
  525. struct drm_device *dev = crtc->dev;
  526. struct drm_framebuffer *fb;
  527. struct drm_gem_object *bo;
  528. unsigned long flags;
  529. bool queue_flip;
  530. drm_modeset_lock(&crtc->mutex, NULL);
  531. spin_lock_irqsave(&dev->event_lock, flags);
  532. /*
  533. * The page flip could have been cancelled while waiting for the GEM
  534. * async operation to complete. Don't queue the flip in that case.
  535. */
  536. if (omap_crtc->flip_state == OMAP_PAGE_FLIP_WAIT) {
  537. omap_crtc->flip_state = OMAP_PAGE_FLIP_QUEUED;
  538. queue_flip = true;
  539. } else {
  540. omap_crtc->flip_state = OMAP_PAGE_FLIP_IDLE;
  541. queue_flip = false;
  542. }
  543. fb = omap_crtc->flip_fb;
  544. omap_crtc->flip_fb = NULL;
  545. spin_unlock_irqrestore(&dev->event_lock, flags);
  546. if (queue_flip) {
  547. omap_plane_mode_set(crtc->primary, crtc, fb,
  548. 0, 0, mode->hdisplay, mode->vdisplay,
  549. crtc->x, crtc->y, mode->hdisplay,
  550. mode->vdisplay);
  551. omap_crtc_flush(crtc);
  552. }
  553. drm_modeset_unlock(&crtc->mutex);
  554. bo = omap_framebuffer_bo(fb, 0);
  555. drm_gem_object_unreference_unlocked(bo);
  556. drm_framebuffer_unreference(fb);
  557. }
  558. static void page_flip_cb(void *arg)
  559. {
  560. struct drm_crtc *crtc = arg;
  561. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  562. struct omap_drm_private *priv = crtc->dev->dev_private;
  563. /* avoid assumptions about what ctxt we are called from: */
  564. queue_work(priv->wq, &omap_crtc->flip_work);
  565. }
  566. static int omap_crtc_page_flip(struct drm_crtc *crtc,
  567. struct drm_framebuffer *fb,
  568. struct drm_pending_vblank_event *event,
  569. uint32_t page_flip_flags)
  570. {
  571. struct drm_device *dev = crtc->dev;
  572. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  573. struct drm_plane *primary = crtc->primary;
  574. struct drm_gem_object *bo;
  575. unsigned long flags;
  576. DBG("%d -> %d (event=%p)", primary->fb ? primary->fb->base.id : -1,
  577. fb->base.id, event);
  578. spin_lock_irqsave(&dev->event_lock, flags);
  579. if (omap_crtc->flip_state != OMAP_PAGE_FLIP_IDLE) {
  580. spin_unlock_irqrestore(&dev->event_lock, flags);
  581. dev_err(dev->dev, "already a pending flip\n");
  582. return -EBUSY;
  583. }
  584. /*
  585. * Store a reference to the framebuffer queued for page flip in the CRTC
  586. * private structure. We can't rely on crtc->primary->fb in the page
  587. * flip worker, as a racing CRTC disable (due for instance to an
  588. * explicit framebuffer deletion from userspace) would set that field to
  589. * NULL before the worker gets a change to run.
  590. */
  591. drm_framebuffer_reference(fb);
  592. omap_crtc->flip_fb = fb;
  593. omap_crtc->flip_event = event;
  594. omap_crtc->flip_state = OMAP_PAGE_FLIP_WAIT;
  595. primary->fb = fb;
  596. spin_unlock_irqrestore(&dev->event_lock, flags);
  597. /*
  598. * Hold a reference temporarily until the crtc is updated
  599. * and takes the reference to the bo. This avoids it
  600. * getting freed from under us:
  601. */
  602. bo = omap_framebuffer_bo(fb, 0);
  603. drm_gem_object_reference(bo);
  604. omap_gem_op_async(bo, OMAP_GEM_READ, page_flip_cb, crtc);
  605. return 0;
  606. }
  607. static int omap_crtc_set_property(struct drm_crtc *crtc,
  608. struct drm_property *property, uint64_t val)
  609. {
  610. if (property == crtc->dev->mode_config.rotation_property) {
  611. crtc->invert_dimensions =
  612. !!(val & ((1LL << DRM_ROTATE_90) | (1LL << DRM_ROTATE_270)));
  613. }
  614. return omap_plane_set_property(crtc->primary, property, val);
  615. }
  616. static const struct drm_crtc_funcs omap_crtc_funcs = {
  617. .set_config = drm_crtc_helper_set_config,
  618. .destroy = omap_crtc_destroy,
  619. .page_flip = omap_crtc_page_flip,
  620. .set_property = omap_crtc_set_property,
  621. };
  622. static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
  623. .dpms = omap_crtc_dpms,
  624. .mode_fixup = omap_crtc_mode_fixup,
  625. .mode_set = omap_crtc_mode_set,
  626. .prepare = omap_crtc_prepare,
  627. .commit = omap_crtc_commit,
  628. .mode_set_base = omap_crtc_mode_set_base,
  629. .disable = omap_crtc_disable,
  630. .enable = omap_crtc_enable,
  631. };
  632. /* -----------------------------------------------------------------------------
  633. * Init and Cleanup
  634. */
  635. static const char *channel_names[] = {
  636. [OMAP_DSS_CHANNEL_LCD] = "lcd",
  637. [OMAP_DSS_CHANNEL_DIGIT] = "tv",
  638. [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
  639. [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
  640. };
  641. void omap_crtc_pre_init(void)
  642. {
  643. dss_install_mgr_ops(&mgr_ops);
  644. }
  645. void omap_crtc_pre_uninit(void)
  646. {
  647. dss_uninstall_mgr_ops();
  648. }
  649. /* initialize crtc */
  650. struct drm_crtc *omap_crtc_init(struct drm_device *dev,
  651. struct drm_plane *plane, enum omap_channel channel, int id)
  652. {
  653. struct drm_crtc *crtc = NULL;
  654. struct omap_crtc *omap_crtc;
  655. struct omap_overlay_manager_info *info;
  656. int ret;
  657. DBG("%s", channel_names[channel]);
  658. omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
  659. if (!omap_crtc)
  660. return NULL;
  661. crtc = &omap_crtc->base;
  662. INIT_WORK(&omap_crtc->flip_work, page_flip_worker);
  663. init_waitqueue_head(&omap_crtc->flip_wait);
  664. INIT_LIST_HEAD(&omap_crtc->pending_unpins);
  665. init_completion(&omap_crtc->completion);
  666. omap_crtc->channel = channel;
  667. omap_crtc->name = channel_names[channel];
  668. omap_crtc->pipe = id;
  669. omap_crtc->vblank_irq.irqmask = pipe2vbl(crtc);
  670. omap_crtc->vblank_irq.irq = omap_crtc_vblank_irq;
  671. omap_crtc->error_irq.irqmask =
  672. dispc_mgr_get_sync_lost_irq(channel);
  673. omap_crtc->error_irq.irq = omap_crtc_error_irq;
  674. omap_irq_register(dev, &omap_crtc->error_irq);
  675. /* temporary: */
  676. omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
  677. /* TODO: fix hard-coded setup.. add properties! */
  678. info = &omap_crtc->info;
  679. info->default_color = 0x00000000;
  680. info->trans_key = 0x00000000;
  681. info->trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  682. info->trans_enabled = false;
  683. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  684. &omap_crtc_funcs);
  685. if (ret < 0) {
  686. kfree(omap_crtc);
  687. return NULL;
  688. }
  689. drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
  690. omap_plane_install_properties(crtc->primary, &crtc->base);
  691. omap_crtcs[channel] = omap_crtc;
  692. return crtc;
  693. }