omap_crtc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Rob Clark <rob@ti.com>
  5. */
  6. #include <linux/math64.h>
  7. #include <drm/drm_atomic.h>
  8. #include <drm/drm_atomic_helper.h>
  9. #include <drm/drm_crtc.h>
  10. #include <drm/drm_mode.h>
  11. #include <drm/drm_print.h>
  12. #include <drm/drm_vblank.h>
  13. #include "omap_drv.h"
  14. #define to_omap_crtc_state(x) container_of(x, struct omap_crtc_state, base)
  15. struct omap_crtc_state {
  16. /* Must be first. */
  17. struct drm_crtc_state base;
  18. /* Shadow values for legacy userspace support. */
  19. unsigned int rotation;
  20. unsigned int zpos;
  21. bool manually_updated;
  22. };
  23. #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
  24. struct omap_crtc {
  25. struct drm_crtc base;
  26. const char *name;
  27. struct omap_drm_pipeline *pipe;
  28. enum omap_channel channel;
  29. struct videomode vm;
  30. bool ignore_digit_sync_lost;
  31. bool enabled;
  32. bool pending;
  33. wait_queue_head_t pending_wait;
  34. struct drm_pending_vblank_event *event;
  35. struct delayed_work update_work;
  36. void (*framedone_handler)(void *);
  37. void *framedone_handler_data;
  38. };
  39. /* -----------------------------------------------------------------------------
  40. * Helper Functions
  41. */
  42. struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
  43. {
  44. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  45. return &omap_crtc->vm;
  46. }
  47. enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
  48. {
  49. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  50. return omap_crtc->channel;
  51. }
  52. static bool omap_crtc_is_pending(struct drm_crtc *crtc)
  53. {
  54. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  55. unsigned long flags;
  56. bool pending;
  57. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  58. pending = omap_crtc->pending;
  59. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  60. return pending;
  61. }
  62. int omap_crtc_wait_pending(struct drm_crtc *crtc)
  63. {
  64. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  65. /*
  66. * Timeout is set to a "sufficiently" high value, which should cover
  67. * a single frame refresh even on slower displays.
  68. */
  69. return wait_event_timeout(omap_crtc->pending_wait,
  70. !omap_crtc_is_pending(crtc),
  71. msecs_to_jiffies(250));
  72. }
  73. /* -----------------------------------------------------------------------------
  74. * DSS Manager Functions
  75. */
  76. /*
  77. * Manager-ops, callbacks from output when they need to configure
  78. * the upstream part of the video pipe.
  79. */
  80. void omap_crtc_dss_start_update(struct omap_drm_private *priv,
  81. enum omap_channel channel)
  82. {
  83. dispc_mgr_enable(priv->dispc, channel, true);
  84. }
  85. /* Called only from the encoder enable/disable and suspend/resume handlers. */
  86. void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
  87. {
  88. struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
  89. struct drm_device *dev = crtc->dev;
  90. struct omap_drm_private *priv = dev->dev_private;
  91. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  92. enum omap_channel channel = omap_crtc->channel;
  93. struct omap_irq_wait *wait;
  94. u32 framedone_irq, vsync_irq;
  95. int ret;
  96. if (WARN_ON(omap_crtc->enabled == enable))
  97. return;
  98. if (omap_state->manually_updated) {
  99. omap_irq_enable_framedone(crtc, enable);
  100. omap_crtc->enabled = enable;
  101. return;
  102. }
  103. if (omap_crtc->pipe->output->type == OMAP_DISPLAY_TYPE_HDMI) {
  104. dispc_mgr_enable(priv->dispc, channel, enable);
  105. omap_crtc->enabled = enable;
  106. return;
  107. }
  108. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  109. /*
  110. * Digit output produces some sync lost interrupts during the
  111. * first frame when enabling, so we need to ignore those.
  112. */
  113. omap_crtc->ignore_digit_sync_lost = true;
  114. }
  115. framedone_irq = dispc_mgr_get_framedone_irq(priv->dispc,
  116. channel);
  117. vsync_irq = dispc_mgr_get_vsync_irq(priv->dispc, channel);
  118. if (enable) {
  119. wait = omap_irq_wait_init(dev, vsync_irq, 1);
  120. } else {
  121. /*
  122. * When we disable the digit output, we need to wait for
  123. * FRAMEDONE to know that DISPC has finished with the output.
  124. *
  125. * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
  126. * that case we need to use vsync interrupt, and wait for both
  127. * even and odd frames.
  128. */
  129. if (framedone_irq)
  130. wait = omap_irq_wait_init(dev, framedone_irq, 1);
  131. else
  132. wait = omap_irq_wait_init(dev, vsync_irq, 2);
  133. }
  134. dispc_mgr_enable(priv->dispc, channel, enable);
  135. omap_crtc->enabled = enable;
  136. ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
  137. if (ret) {
  138. dev_err(dev->dev, "%s: timeout waiting for %s\n",
  139. omap_crtc->name, enable ? "enable" : "disable");
  140. }
  141. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  142. omap_crtc->ignore_digit_sync_lost = false;
  143. /* make sure the irq handler sees the value above */
  144. mb();
  145. }
  146. }
  147. int omap_crtc_dss_enable(struct omap_drm_private *priv, enum omap_channel channel)
  148. {
  149. struct drm_crtc *crtc = priv->channels[channel]->crtc;
  150. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  151. dispc_mgr_set_timings(priv->dispc, omap_crtc->channel,
  152. &omap_crtc->vm);
  153. omap_crtc_set_enabled(&omap_crtc->base, true);
  154. return 0;
  155. }
  156. void omap_crtc_dss_disable(struct omap_drm_private *priv, enum omap_channel channel)
  157. {
  158. struct drm_crtc *crtc = priv->channels[channel]->crtc;
  159. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  160. omap_crtc_set_enabled(&omap_crtc->base, false);
  161. }
  162. void omap_crtc_dss_set_timings(struct omap_drm_private *priv,
  163. enum omap_channel channel,
  164. const struct videomode *vm)
  165. {
  166. struct drm_crtc *crtc = priv->channels[channel]->crtc;
  167. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  168. DBG("%s", omap_crtc->name);
  169. omap_crtc->vm = *vm;
  170. }
  171. void omap_crtc_dss_set_lcd_config(struct omap_drm_private *priv,
  172. enum omap_channel channel,
  173. const struct dss_lcd_mgr_config *config)
  174. {
  175. struct drm_crtc *crtc = priv->channels[channel]->crtc;
  176. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  177. DBG("%s", omap_crtc->name);
  178. dispc_mgr_set_lcd_config(priv->dispc, omap_crtc->channel,
  179. config);
  180. }
  181. int omap_crtc_dss_register_framedone(
  182. struct omap_drm_private *priv, enum omap_channel channel,
  183. void (*handler)(void *), void *data)
  184. {
  185. struct drm_crtc *crtc = priv->channels[channel]->crtc;
  186. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  187. struct drm_device *dev = omap_crtc->base.dev;
  188. if (omap_crtc->framedone_handler)
  189. return -EBUSY;
  190. dev_dbg(dev->dev, "register framedone %s", omap_crtc->name);
  191. omap_crtc->framedone_handler = handler;
  192. omap_crtc->framedone_handler_data = data;
  193. return 0;
  194. }
  195. void omap_crtc_dss_unregister_framedone(
  196. struct omap_drm_private *priv, enum omap_channel channel,
  197. void (*handler)(void *), void *data)
  198. {
  199. struct drm_crtc *crtc = priv->channels[channel]->crtc;
  200. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  201. struct drm_device *dev = omap_crtc->base.dev;
  202. dev_dbg(dev->dev, "unregister framedone %s", omap_crtc->name);
  203. WARN_ON(omap_crtc->framedone_handler != handler);
  204. WARN_ON(omap_crtc->framedone_handler_data != data);
  205. omap_crtc->framedone_handler = NULL;
  206. omap_crtc->framedone_handler_data = NULL;
  207. }
  208. /* -----------------------------------------------------------------------------
  209. * Setup, Flush and Page Flip
  210. */
  211. void omap_crtc_error_irq(struct drm_crtc *crtc, u32 irqstatus)
  212. {
  213. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  214. if (omap_crtc->ignore_digit_sync_lost) {
  215. irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
  216. if (!irqstatus)
  217. return;
  218. }
  219. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
  220. }
  221. void omap_crtc_vblank_irq(struct drm_crtc *crtc)
  222. {
  223. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  224. struct drm_device *dev = omap_crtc->base.dev;
  225. struct omap_drm_private *priv = dev->dev_private;
  226. bool pending;
  227. spin_lock(&crtc->dev->event_lock);
  228. /*
  229. * If the dispc is busy we're racing the flush operation. Try again on
  230. * the next vblank interrupt.
  231. */
  232. if (dispc_mgr_go_busy(priv->dispc, omap_crtc->channel)) {
  233. spin_unlock(&crtc->dev->event_lock);
  234. return;
  235. }
  236. /* Send the vblank event if one has been requested. */
  237. if (omap_crtc->event) {
  238. drm_crtc_send_vblank_event(crtc, omap_crtc->event);
  239. omap_crtc->event = NULL;
  240. }
  241. pending = omap_crtc->pending;
  242. omap_crtc->pending = false;
  243. spin_unlock(&crtc->dev->event_lock);
  244. if (pending)
  245. drm_crtc_vblank_put(crtc);
  246. /* Wake up omap_atomic_complete. */
  247. wake_up(&omap_crtc->pending_wait);
  248. DBG("%s: apply done", omap_crtc->name);
  249. }
  250. void omap_crtc_framedone_irq(struct drm_crtc *crtc, uint32_t irqstatus)
  251. {
  252. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  253. if (!omap_crtc->framedone_handler)
  254. return;
  255. omap_crtc->framedone_handler(omap_crtc->framedone_handler_data);
  256. spin_lock(&crtc->dev->event_lock);
  257. /* Send the vblank event if one has been requested. */
  258. if (omap_crtc->event) {
  259. drm_crtc_send_vblank_event(crtc, omap_crtc->event);
  260. omap_crtc->event = NULL;
  261. }
  262. omap_crtc->pending = false;
  263. spin_unlock(&crtc->dev->event_lock);
  264. /* Wake up omap_atomic_complete. */
  265. wake_up(&omap_crtc->pending_wait);
  266. }
  267. void omap_crtc_flush(struct drm_crtc *crtc)
  268. {
  269. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  270. struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
  271. if (!omap_state->manually_updated)
  272. return;
  273. if (!delayed_work_pending(&omap_crtc->update_work))
  274. schedule_delayed_work(&omap_crtc->update_work, 0);
  275. }
  276. static void omap_crtc_manual_display_update(struct work_struct *data)
  277. {
  278. struct omap_crtc *omap_crtc =
  279. container_of(data, struct omap_crtc, update_work.work);
  280. struct omap_dss_device *dssdev = omap_crtc->pipe->output;
  281. struct drm_device *dev = omap_crtc->base.dev;
  282. int ret;
  283. if (!dssdev || !dssdev->dsi_ops || !dssdev->dsi_ops->update)
  284. return;
  285. ret = dssdev->dsi_ops->update(dssdev);
  286. if (ret < 0) {
  287. spin_lock_irq(&dev->event_lock);
  288. omap_crtc->pending = false;
  289. spin_unlock_irq(&dev->event_lock);
  290. wake_up(&omap_crtc->pending_wait);
  291. }
  292. }
  293. static s16 omap_crtc_s31_32_to_s2_8(s64 coef)
  294. {
  295. u64 sign_bit = 1ULL << 63;
  296. u64 cbits = (u64)coef;
  297. s16 ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x1ff);
  298. if (cbits & sign_bit)
  299. ret = -ret;
  300. return ret;
  301. }
  302. static void omap_crtc_cpr_coefs_from_ctm(const struct drm_color_ctm *ctm,
  303. struct omap_dss_cpr_coefs *cpr)
  304. {
  305. cpr->rr = omap_crtc_s31_32_to_s2_8(ctm->matrix[0]);
  306. cpr->rg = omap_crtc_s31_32_to_s2_8(ctm->matrix[1]);
  307. cpr->rb = omap_crtc_s31_32_to_s2_8(ctm->matrix[2]);
  308. cpr->gr = omap_crtc_s31_32_to_s2_8(ctm->matrix[3]);
  309. cpr->gg = omap_crtc_s31_32_to_s2_8(ctm->matrix[4]);
  310. cpr->gb = omap_crtc_s31_32_to_s2_8(ctm->matrix[5]);
  311. cpr->br = omap_crtc_s31_32_to_s2_8(ctm->matrix[6]);
  312. cpr->bg = omap_crtc_s31_32_to_s2_8(ctm->matrix[7]);
  313. cpr->bb = omap_crtc_s31_32_to_s2_8(ctm->matrix[8]);
  314. }
  315. static void omap_crtc_write_crtc_properties(struct drm_crtc *crtc)
  316. {
  317. struct omap_drm_private *priv = crtc->dev->dev_private;
  318. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  319. struct omap_overlay_manager_info info;
  320. memset(&info, 0, sizeof(info));
  321. info.default_color = 0x000000;
  322. info.trans_enabled = false;
  323. info.partial_alpha_enabled = false;
  324. if (crtc->state->ctm) {
  325. struct drm_color_ctm *ctm = crtc->state->ctm->data;
  326. info.cpr_enable = true;
  327. omap_crtc_cpr_coefs_from_ctm(ctm, &info.cpr_coefs);
  328. } else {
  329. info.cpr_enable = false;
  330. }
  331. dispc_mgr_setup(priv->dispc, omap_crtc->channel, &info);
  332. }
  333. /* -----------------------------------------------------------------------------
  334. * CRTC Functions
  335. */
  336. static void omap_crtc_destroy(struct drm_crtc *crtc)
  337. {
  338. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  339. DBG("%s", omap_crtc->name);
  340. drm_crtc_cleanup(crtc);
  341. kfree(omap_crtc);
  342. }
  343. static void omap_crtc_arm_event(struct drm_crtc *crtc)
  344. {
  345. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  346. WARN_ON(omap_crtc->pending);
  347. omap_crtc->pending = true;
  348. if (crtc->state->event) {
  349. omap_crtc->event = crtc->state->event;
  350. crtc->state->event = NULL;
  351. }
  352. }
  353. static void omap_crtc_atomic_enable(struct drm_crtc *crtc,
  354. struct drm_atomic_state *state)
  355. {
  356. struct omap_drm_private *priv = crtc->dev->dev_private;
  357. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  358. struct omap_crtc_state *omap_state = to_omap_crtc_state(crtc->state);
  359. int ret;
  360. DBG("%s", omap_crtc->name);
  361. dispc_runtime_get(priv->dispc);
  362. /* manual updated display will not trigger vsync irq */
  363. if (omap_state->manually_updated)
  364. return;
  365. drm_crtc_vblank_on(crtc);
  366. ret = drm_crtc_vblank_get(crtc);
  367. WARN_ON(ret != 0);
  368. spin_lock_irq(&crtc->dev->event_lock);
  369. omap_crtc_arm_event(crtc);
  370. spin_unlock_irq(&crtc->dev->event_lock);
  371. }
  372. static void omap_crtc_atomic_disable(struct drm_crtc *crtc,
  373. struct drm_atomic_state *state)
  374. {
  375. struct omap_drm_private *priv = crtc->dev->dev_private;
  376. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  377. struct drm_device *dev = crtc->dev;
  378. DBG("%s", omap_crtc->name);
  379. spin_lock_irq(&crtc->dev->event_lock);
  380. if (crtc->state->event) {
  381. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  382. crtc->state->event = NULL;
  383. }
  384. spin_unlock_irq(&crtc->dev->event_lock);
  385. cancel_delayed_work(&omap_crtc->update_work);
  386. if (!omap_crtc_wait_pending(crtc))
  387. dev_warn(dev->dev, "manual display update did not finish!");
  388. drm_crtc_vblank_off(crtc);
  389. dispc_runtime_put(priv->dispc);
  390. }
  391. static enum drm_mode_status omap_crtc_mode_valid(struct drm_crtc *crtc,
  392. const struct drm_display_mode *mode)
  393. {
  394. struct omap_drm_private *priv = crtc->dev->dev_private;
  395. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  396. struct videomode vm = {0};
  397. int r;
  398. drm_display_mode_to_videomode(mode, &vm);
  399. /*
  400. * DSI might not call this, since the supplied mode is not a
  401. * valid DISPC mode. DSI will calculate and configure the
  402. * proper DISPC mode later.
  403. */
  404. if (omap_crtc->pipe->output->type != OMAP_DISPLAY_TYPE_DSI) {
  405. r = dispc_mgr_check_timings(priv->dispc,
  406. omap_crtc->channel,
  407. &vm);
  408. if (r)
  409. return r;
  410. }
  411. /* Check for bandwidth limit */
  412. if (priv->max_bandwidth) {
  413. /*
  414. * Estimation for the bandwidth need of a given mode with one
  415. * full screen plane:
  416. * bandwidth = resolution * 32bpp * (pclk / (vtotal * htotal))
  417. * ^^ Refresh rate ^^
  418. *
  419. * The interlaced mode is taken into account by using the
  420. * pixelclock in the calculation.
  421. *
  422. * The equation is rearranged for 64bit arithmetic.
  423. */
  424. uint64_t bandwidth = mode->clock * 1000;
  425. unsigned int bpp = 4;
  426. bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
  427. bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
  428. /*
  429. * Reject modes which would need more bandwidth if used with one
  430. * full resolution plane (most common use case).
  431. */
  432. if (priv->max_bandwidth < bandwidth)
  433. return MODE_BAD;
  434. }
  435. return MODE_OK;
  436. }
  437. static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
  438. {
  439. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  440. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  441. DBG("%s: set mode: " DRM_MODE_FMT,
  442. omap_crtc->name, DRM_MODE_ARG(mode));
  443. drm_display_mode_to_videomode(mode, &omap_crtc->vm);
  444. }
  445. static bool omap_crtc_is_manually_updated(struct drm_crtc *crtc)
  446. {
  447. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  448. struct omap_dss_device *dssdev = omap_crtc->pipe->output;
  449. if (!dssdev || !dssdev->dsi_ops || !dssdev->dsi_ops->is_video_mode)
  450. return false;
  451. if (dssdev->dsi_ops->is_video_mode(dssdev))
  452. return false;
  453. DBG("detected manually updated display!");
  454. return true;
  455. }
  456. static int omap_crtc_atomic_check(struct drm_crtc *crtc,
  457. struct drm_atomic_state *state)
  458. {
  459. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
  460. crtc);
  461. struct drm_plane_state *pri_state;
  462. if (crtc_state->color_mgmt_changed && crtc_state->degamma_lut) {
  463. unsigned int length = crtc_state->degamma_lut->length /
  464. sizeof(struct drm_color_lut);
  465. if (length < 2)
  466. return -EINVAL;
  467. }
  468. pri_state = drm_atomic_get_new_plane_state(state,
  469. crtc->primary);
  470. if (pri_state) {
  471. struct omap_crtc_state *omap_crtc_state =
  472. to_omap_crtc_state(crtc_state);
  473. /* Mirror new values for zpos and rotation in omap_crtc_state */
  474. omap_crtc_state->zpos = pri_state->zpos;
  475. omap_crtc_state->rotation = pri_state->rotation;
  476. /* Check if this CRTC is for a manually updated display */
  477. omap_crtc_state->manually_updated = omap_crtc_is_manually_updated(crtc);
  478. }
  479. return 0;
  480. }
  481. static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
  482. struct drm_atomic_state *state)
  483. {
  484. }
  485. static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
  486. struct drm_atomic_state *state)
  487. {
  488. struct omap_drm_private *priv = crtc->dev->dev_private;
  489. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  490. struct omap_crtc_state *omap_crtc_state = to_omap_crtc_state(crtc->state);
  491. int ret;
  492. if (crtc->state->color_mgmt_changed) {
  493. struct drm_color_lut *lut = NULL;
  494. unsigned int length = 0;
  495. if (crtc->state->degamma_lut) {
  496. lut = (struct drm_color_lut *)
  497. crtc->state->degamma_lut->data;
  498. length = crtc->state->degamma_lut->length /
  499. sizeof(*lut);
  500. }
  501. dispc_mgr_set_gamma(priv->dispc, omap_crtc->channel,
  502. lut, length);
  503. }
  504. omap_crtc_write_crtc_properties(crtc);
  505. /* Only flush the CRTC if it is currently enabled. */
  506. if (!omap_crtc->enabled)
  507. return;
  508. DBG("%s: GO", omap_crtc->name);
  509. if (omap_crtc_state->manually_updated) {
  510. /* send new image for page flips and modeset changes */
  511. spin_lock_irq(&crtc->dev->event_lock);
  512. omap_crtc_flush(crtc);
  513. omap_crtc_arm_event(crtc);
  514. spin_unlock_irq(&crtc->dev->event_lock);
  515. return;
  516. }
  517. ret = drm_crtc_vblank_get(crtc);
  518. WARN_ON(ret != 0);
  519. spin_lock_irq(&crtc->dev->event_lock);
  520. dispc_mgr_go(priv->dispc, omap_crtc->channel);
  521. omap_crtc_arm_event(crtc);
  522. spin_unlock_irq(&crtc->dev->event_lock);
  523. }
  524. static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
  525. struct drm_crtc_state *state,
  526. struct drm_property *property,
  527. u64 val)
  528. {
  529. struct omap_drm_private *priv = crtc->dev->dev_private;
  530. struct drm_plane_state *plane_state;
  531. /*
  532. * Delegate property set to the primary plane. Get the plane state and
  533. * set the property directly, the shadow copy will be assigned in the
  534. * omap_crtc_atomic_check callback. This way updates to plane state will
  535. * always be mirrored in the crtc state correctly.
  536. */
  537. plane_state = drm_atomic_get_plane_state(state->state, crtc->primary);
  538. if (IS_ERR(plane_state))
  539. return PTR_ERR(plane_state);
  540. if (property == crtc->primary->rotation_property)
  541. plane_state->rotation = val;
  542. else if (property == priv->zorder_prop)
  543. plane_state->zpos = val;
  544. else
  545. return -EINVAL;
  546. return 0;
  547. }
  548. static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
  549. const struct drm_crtc_state *state,
  550. struct drm_property *property,
  551. u64 *val)
  552. {
  553. struct omap_drm_private *priv = crtc->dev->dev_private;
  554. struct omap_crtc_state *omap_state = to_omap_crtc_state(state);
  555. if (property == crtc->primary->rotation_property)
  556. *val = omap_state->rotation;
  557. else if (property == priv->zorder_prop)
  558. *val = omap_state->zpos;
  559. else
  560. return -EINVAL;
  561. return 0;
  562. }
  563. static void omap_crtc_reset(struct drm_crtc *crtc)
  564. {
  565. struct omap_crtc_state *state;
  566. if (crtc->state)
  567. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  568. kfree(crtc->state);
  569. state = kzalloc_obj(*state);
  570. if (state)
  571. __drm_atomic_helper_crtc_reset(crtc, &state->base);
  572. }
  573. static struct drm_crtc_state *
  574. omap_crtc_duplicate_state(struct drm_crtc *crtc)
  575. {
  576. struct omap_crtc_state *state, *current_state;
  577. if (WARN_ON(!crtc->state))
  578. return NULL;
  579. current_state = to_omap_crtc_state(crtc->state);
  580. state = kmalloc_obj(*state);
  581. if (!state)
  582. return NULL;
  583. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  584. state->zpos = current_state->zpos;
  585. state->rotation = current_state->rotation;
  586. state->manually_updated = current_state->manually_updated;
  587. return &state->base;
  588. }
  589. static const struct drm_crtc_funcs omap_crtc_funcs = {
  590. .reset = omap_crtc_reset,
  591. .set_config = drm_atomic_helper_set_config,
  592. .destroy = omap_crtc_destroy,
  593. .page_flip = drm_atomic_helper_page_flip,
  594. .atomic_duplicate_state = omap_crtc_duplicate_state,
  595. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  596. .atomic_set_property = omap_crtc_atomic_set_property,
  597. .atomic_get_property = omap_crtc_atomic_get_property,
  598. .enable_vblank = omap_irq_enable_vblank,
  599. .disable_vblank = omap_irq_disable_vblank,
  600. };
  601. static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
  602. .mode_set_nofb = omap_crtc_mode_set_nofb,
  603. .atomic_check = omap_crtc_atomic_check,
  604. .atomic_begin = omap_crtc_atomic_begin,
  605. .atomic_flush = omap_crtc_atomic_flush,
  606. .atomic_enable = omap_crtc_atomic_enable,
  607. .atomic_disable = omap_crtc_atomic_disable,
  608. .mode_valid = omap_crtc_mode_valid,
  609. };
  610. /* -----------------------------------------------------------------------------
  611. * Init and Cleanup
  612. */
  613. static const char *channel_names[] = {
  614. [OMAP_DSS_CHANNEL_LCD] = "lcd",
  615. [OMAP_DSS_CHANNEL_DIGIT] = "tv",
  616. [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
  617. [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
  618. };
  619. /* initialize crtc */
  620. struct drm_crtc *omap_crtc_init(struct drm_device *dev,
  621. struct omap_drm_pipeline *pipe,
  622. struct drm_plane *plane)
  623. {
  624. struct omap_drm_private *priv = dev->dev_private;
  625. struct drm_crtc *crtc = NULL;
  626. struct omap_crtc *omap_crtc;
  627. enum omap_channel channel;
  628. int ret;
  629. channel = pipe->output->dispc_channel;
  630. DBG("%s", channel_names[channel]);
  631. omap_crtc = kzalloc_obj(*omap_crtc);
  632. if (!omap_crtc)
  633. return ERR_PTR(-ENOMEM);
  634. crtc = &omap_crtc->base;
  635. init_waitqueue_head(&omap_crtc->pending_wait);
  636. omap_crtc->pipe = pipe;
  637. omap_crtc->channel = channel;
  638. omap_crtc->name = channel_names[channel];
  639. /*
  640. * We want to refresh manually updated displays from dirty callback,
  641. * which is called quite often (e.g. for each drawn line). This will
  642. * be used to do the display update asynchronously to avoid blocking
  643. * the rendering process and merges multiple dirty calls into one
  644. * update if they arrive very fast. We also call this function for
  645. * atomic display updates (e.g. for page flips), which means we do
  646. * not need extra locking. Atomic updates should be synchronous, but
  647. * need to wait for the framedone interrupt anyways.
  648. */
  649. INIT_DELAYED_WORK(&omap_crtc->update_work,
  650. omap_crtc_manual_display_update);
  651. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  652. &omap_crtc_funcs, NULL);
  653. if (ret < 0) {
  654. dev_err(dev->dev, "%s(): could not init crtc for: %s\n",
  655. __func__, pipe->output->name);
  656. kfree(omap_crtc);
  657. return ERR_PTR(ret);
  658. }
  659. drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
  660. /* The dispc API adapts to what ever size, but the HW supports
  661. * 256 element gamma table for LCDs and 1024 element table for
  662. * OMAP_DSS_CHANNEL_DIGIT. X server assumes 256 element gamma
  663. * tables so lets use that. Size of HW gamma table can be
  664. * extracted with dispc_mgr_gamma_size(). If it returns 0
  665. * gamma table is not supported.
  666. */
  667. if (dispc_mgr_gamma_size(priv->dispc, channel)) {
  668. unsigned int gamma_lut_size = 256;
  669. drm_crtc_enable_color_mgmt(crtc, gamma_lut_size, true, 0);
  670. drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
  671. }
  672. omap_plane_install_properties(crtc->primary, &crtc->base);
  673. return crtc;
  674. }