omap_crtc.c 22 KB

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