omap_crtc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  3. * Author: Rob Clark <rob@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_crtc.h>
  20. #include <drm/drm_crtc_helper.h>
  21. #include <drm/drm_mode.h>
  22. #include <drm/drm_plane_helper.h>
  23. #include <linux/math64.h>
  24. #include "omap_drv.h"
  25. #define to_omap_crtc_state(x) container_of(x, struct omap_crtc_state, base)
  26. struct omap_crtc_state {
  27. /* Must be first. */
  28. struct drm_crtc_state base;
  29. /* Shadow values for legacy userspace support. */
  30. unsigned int rotation;
  31. unsigned int zpos;
  32. };
  33. #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
  34. struct omap_crtc {
  35. struct drm_crtc base;
  36. const char *name;
  37. enum omap_channel channel;
  38. struct videomode vm;
  39. bool ignore_digit_sync_lost;
  40. bool enabled;
  41. bool pending;
  42. wait_queue_head_t pending_wait;
  43. struct drm_pending_vblank_event *event;
  44. };
  45. /* -----------------------------------------------------------------------------
  46. * Helper Functions
  47. */
  48. struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
  49. {
  50. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  51. return &omap_crtc->vm;
  52. }
  53. enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
  54. {
  55. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  56. return omap_crtc->channel;
  57. }
  58. static bool omap_crtc_is_pending(struct drm_crtc *crtc)
  59. {
  60. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  61. unsigned long flags;
  62. bool pending;
  63. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  64. pending = omap_crtc->pending;
  65. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  66. return pending;
  67. }
  68. int omap_crtc_wait_pending(struct drm_crtc *crtc)
  69. {
  70. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  71. /*
  72. * Timeout is set to a "sufficiently" high value, which should cover
  73. * a single frame refresh even on slower displays.
  74. */
  75. return wait_event_timeout(omap_crtc->pending_wait,
  76. !omap_crtc_is_pending(crtc),
  77. msecs_to_jiffies(250));
  78. }
  79. /* -----------------------------------------------------------------------------
  80. * DSS Manager Functions
  81. */
  82. /*
  83. * Manager-ops, callbacks from output when they need to configure
  84. * the upstream part of the video pipe.
  85. *
  86. * Most of these we can ignore until we add support for command-mode
  87. * panels.. for video-mode the crtc-helpers already do an adequate
  88. * job of sequencing the setup of the video pipe in the proper order
  89. */
  90. /* ovl-mgr-id -> crtc */
  91. static struct omap_crtc *omap_crtcs[8];
  92. static struct omap_dss_device *omap_crtc_output[8];
  93. /* we can probably ignore these until we support command-mode panels: */
  94. static int omap_crtc_dss_connect(struct omap_drm_private *priv,
  95. enum omap_channel channel,
  96. struct omap_dss_device *dst)
  97. {
  98. const struct dispc_ops *dispc_ops = priv->dispc_ops;
  99. struct dispc_device *dispc = priv->dispc;
  100. if (omap_crtc_output[channel])
  101. return -EINVAL;
  102. if (!(dispc_ops->mgr_get_supported_outputs(dispc, channel) & dst->id))
  103. return -EINVAL;
  104. omap_crtc_output[channel] = dst;
  105. dst->dispc_channel_connected = true;
  106. return 0;
  107. }
  108. static void omap_crtc_dss_disconnect(struct omap_drm_private *priv,
  109. enum omap_channel channel,
  110. struct omap_dss_device *dst)
  111. {
  112. omap_crtc_output[channel] = NULL;
  113. dst->dispc_channel_connected = false;
  114. }
  115. static void omap_crtc_dss_start_update(struct omap_drm_private *priv,
  116. enum omap_channel channel)
  117. {
  118. }
  119. /* Called only from the encoder enable/disable and suspend/resume handlers. */
  120. static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
  121. {
  122. struct drm_device *dev = crtc->dev;
  123. struct omap_drm_private *priv = dev->dev_private;
  124. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  125. enum omap_channel channel = omap_crtc->channel;
  126. struct omap_irq_wait *wait;
  127. u32 framedone_irq, vsync_irq;
  128. int ret;
  129. if (WARN_ON(omap_crtc->enabled == enable))
  130. return;
  131. if (omap_crtc_output[channel]->output_type == OMAP_DISPLAY_TYPE_HDMI) {
  132. priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
  133. omap_crtc->enabled = enable;
  134. return;
  135. }
  136. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  137. /*
  138. * Digit output produces some sync lost interrupts during the
  139. * first frame when enabling, so we need to ignore those.
  140. */
  141. omap_crtc->ignore_digit_sync_lost = true;
  142. }
  143. framedone_irq = priv->dispc_ops->mgr_get_framedone_irq(priv->dispc,
  144. channel);
  145. vsync_irq = priv->dispc_ops->mgr_get_vsync_irq(priv->dispc, channel);
  146. if (enable) {
  147. wait = omap_irq_wait_init(dev, vsync_irq, 1);
  148. } else {
  149. /*
  150. * When we disable the digit output, we need to wait for
  151. * FRAMEDONE to know that DISPC has finished with the output.
  152. *
  153. * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
  154. * that case we need to use vsync interrupt, and wait for both
  155. * even and odd frames.
  156. */
  157. if (framedone_irq)
  158. wait = omap_irq_wait_init(dev, framedone_irq, 1);
  159. else
  160. wait = omap_irq_wait_init(dev, vsync_irq, 2);
  161. }
  162. priv->dispc_ops->mgr_enable(priv->dispc, channel, enable);
  163. omap_crtc->enabled = enable;
  164. ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
  165. if (ret) {
  166. dev_err(dev->dev, "%s: timeout waiting for %s\n",
  167. omap_crtc->name, enable ? "enable" : "disable");
  168. }
  169. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  170. omap_crtc->ignore_digit_sync_lost = false;
  171. /* make sure the irq handler sees the value above */
  172. mb();
  173. }
  174. }
  175. static int omap_crtc_dss_enable(struct omap_drm_private *priv,
  176. enum omap_channel channel)
  177. {
  178. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  179. priv->dispc_ops->mgr_set_timings(priv->dispc, omap_crtc->channel,
  180. &omap_crtc->vm);
  181. omap_crtc_set_enabled(&omap_crtc->base, true);
  182. return 0;
  183. }
  184. static void omap_crtc_dss_disable(struct omap_drm_private *priv,
  185. enum omap_channel channel)
  186. {
  187. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  188. omap_crtc_set_enabled(&omap_crtc->base, false);
  189. }
  190. static void omap_crtc_dss_set_timings(struct omap_drm_private *priv,
  191. enum omap_channel channel,
  192. const struct videomode *vm)
  193. {
  194. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  195. DBG("%s", omap_crtc->name);
  196. omap_crtc->vm = *vm;
  197. }
  198. static void omap_crtc_dss_set_lcd_config(struct omap_drm_private *priv,
  199. enum omap_channel channel,
  200. const struct dss_lcd_mgr_config *config)
  201. {
  202. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  203. DBG("%s", omap_crtc->name);
  204. priv->dispc_ops->mgr_set_lcd_config(priv->dispc, omap_crtc->channel,
  205. config);
  206. }
  207. static int omap_crtc_dss_register_framedone(
  208. struct omap_drm_private *priv, enum omap_channel channel,
  209. void (*handler)(void *), void *data)
  210. {
  211. return 0;
  212. }
  213. static void omap_crtc_dss_unregister_framedone(
  214. struct omap_drm_private *priv, enum omap_channel channel,
  215. void (*handler)(void *), void *data)
  216. {
  217. }
  218. static const struct dss_mgr_ops mgr_ops = {
  219. .connect = omap_crtc_dss_connect,
  220. .disconnect = omap_crtc_dss_disconnect,
  221. .start_update = omap_crtc_dss_start_update,
  222. .enable = omap_crtc_dss_enable,
  223. .disable = omap_crtc_dss_disable,
  224. .set_timings = omap_crtc_dss_set_timings,
  225. .set_lcd_config = omap_crtc_dss_set_lcd_config,
  226. .register_framedone_handler = omap_crtc_dss_register_framedone,
  227. .unregister_framedone_handler = omap_crtc_dss_unregister_framedone,
  228. };
  229. /* -----------------------------------------------------------------------------
  230. * Setup, Flush and Page Flip
  231. */
  232. void omap_crtc_error_irq(struct drm_crtc *crtc, u32 irqstatus)
  233. {
  234. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  235. if (omap_crtc->ignore_digit_sync_lost) {
  236. irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
  237. if (!irqstatus)
  238. return;
  239. }
  240. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
  241. }
  242. void omap_crtc_vblank_irq(struct drm_crtc *crtc)
  243. {
  244. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  245. struct drm_device *dev = omap_crtc->base.dev;
  246. struct omap_drm_private *priv = dev->dev_private;
  247. bool pending;
  248. spin_lock(&crtc->dev->event_lock);
  249. /*
  250. * If the dispc is busy we're racing the flush operation. Try again on
  251. * the next vblank interrupt.
  252. */
  253. if (priv->dispc_ops->mgr_go_busy(priv->dispc, omap_crtc->channel)) {
  254. spin_unlock(&crtc->dev->event_lock);
  255. return;
  256. }
  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. pending = omap_crtc->pending;
  263. omap_crtc->pending = false;
  264. spin_unlock(&crtc->dev->event_lock);
  265. if (pending)
  266. drm_crtc_vblank_put(crtc);
  267. /* Wake up omap_atomic_complete. */
  268. wake_up(&omap_crtc->pending_wait);
  269. DBG("%s: apply done", omap_crtc->name);
  270. }
  271. static void omap_crtc_write_crtc_properties(struct drm_crtc *crtc)
  272. {
  273. struct omap_drm_private *priv = crtc->dev->dev_private;
  274. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  275. struct omap_overlay_manager_info info;
  276. memset(&info, 0, sizeof(info));
  277. info.default_color = 0x000000;
  278. info.trans_enabled = false;
  279. info.partial_alpha_enabled = false;
  280. info.cpr_enable = false;
  281. priv->dispc_ops->mgr_setup(priv->dispc, omap_crtc->channel, &info);
  282. }
  283. /* -----------------------------------------------------------------------------
  284. * CRTC Functions
  285. */
  286. static void omap_crtc_destroy(struct drm_crtc *crtc)
  287. {
  288. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  289. DBG("%s", omap_crtc->name);
  290. drm_crtc_cleanup(crtc);
  291. kfree(omap_crtc);
  292. }
  293. static void omap_crtc_arm_event(struct drm_crtc *crtc)
  294. {
  295. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  296. WARN_ON(omap_crtc->pending);
  297. omap_crtc->pending = true;
  298. if (crtc->state->event) {
  299. omap_crtc->event = crtc->state->event;
  300. crtc->state->event = NULL;
  301. }
  302. }
  303. static void omap_crtc_atomic_enable(struct drm_crtc *crtc,
  304. struct drm_crtc_state *old_state)
  305. {
  306. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  307. int ret;
  308. DBG("%s", omap_crtc->name);
  309. spin_lock_irq(&crtc->dev->event_lock);
  310. drm_crtc_vblank_on(crtc);
  311. ret = drm_crtc_vblank_get(crtc);
  312. WARN_ON(ret != 0);
  313. omap_crtc_arm_event(crtc);
  314. spin_unlock_irq(&crtc->dev->event_lock);
  315. }
  316. static void omap_crtc_atomic_disable(struct drm_crtc *crtc,
  317. struct drm_crtc_state *old_state)
  318. {
  319. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  320. DBG("%s", omap_crtc->name);
  321. spin_lock_irq(&crtc->dev->event_lock);
  322. if (crtc->state->event) {
  323. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  324. crtc->state->event = NULL;
  325. }
  326. spin_unlock_irq(&crtc->dev->event_lock);
  327. drm_crtc_vblank_off(crtc);
  328. }
  329. static enum drm_mode_status omap_crtc_mode_valid(struct drm_crtc *crtc,
  330. const struct drm_display_mode *mode)
  331. {
  332. struct omap_drm_private *priv = crtc->dev->dev_private;
  333. /* Check for bandwidth limit */
  334. if (priv->max_bandwidth) {
  335. /*
  336. * Estimation for the bandwidth need of a given mode with one
  337. * full screen plane:
  338. * bandwidth = resolution * 32bpp * (pclk / (vtotal * htotal))
  339. * ^^ Refresh rate ^^
  340. *
  341. * The interlaced mode is taken into account by using the
  342. * pixelclock in the calculation.
  343. *
  344. * The equation is rearranged for 64bit arithmetic.
  345. */
  346. uint64_t bandwidth = mode->clock * 1000;
  347. unsigned int bpp = 4;
  348. bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
  349. bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
  350. /*
  351. * Reject modes which would need more bandwidth if used with one
  352. * full resolution plane (most common use case).
  353. */
  354. if (priv->max_bandwidth < bandwidth)
  355. return MODE_BAD;
  356. }
  357. return MODE_OK;
  358. }
  359. static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
  360. {
  361. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  362. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  363. struct omap_drm_private *priv = crtc->dev->dev_private;
  364. const u32 flags_mask = DISPLAY_FLAGS_DE_HIGH | DISPLAY_FLAGS_DE_LOW |
  365. DISPLAY_FLAGS_PIXDATA_POSEDGE | DISPLAY_FLAGS_PIXDATA_NEGEDGE |
  366. DISPLAY_FLAGS_SYNC_POSEDGE | DISPLAY_FLAGS_SYNC_NEGEDGE;
  367. unsigned int i;
  368. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  369. omap_crtc->name, mode->base.id, mode->name,
  370. mode->vrefresh, mode->clock,
  371. mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
  372. mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal,
  373. mode->type, mode->flags);
  374. drm_display_mode_to_videomode(mode, &omap_crtc->vm);
  375. /*
  376. * HACK: This fixes the vm flags.
  377. * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags
  378. * and they get lost when converting back and forth between
  379. * struct drm_display_mode and struct videomode. The hack below
  380. * goes and fetches the missing flags from the panel drivers.
  381. *
  382. * Correct solution would be to use DRM's bus-flags, but that's not
  383. * easily possible before the omapdrm's panel/encoder driver model
  384. * has been changed to the DRM model.
  385. */
  386. for (i = 0; i < priv->num_encoders; ++i) {
  387. struct drm_encoder *encoder = priv->encoders[i];
  388. if (encoder->crtc == crtc) {
  389. struct omap_dss_device *dssdev;
  390. dssdev = omap_encoder_get_dssdev(encoder);
  391. if (dssdev) {
  392. struct videomode vm = {0};
  393. dssdev->driver->get_timings(dssdev, &vm);
  394. omap_crtc->vm.flags |= vm.flags & flags_mask;
  395. }
  396. break;
  397. }
  398. }
  399. }
  400. static int omap_crtc_atomic_check(struct drm_crtc *crtc,
  401. struct drm_crtc_state *state)
  402. {
  403. struct drm_plane_state *pri_state;
  404. if (state->color_mgmt_changed && state->gamma_lut) {
  405. unsigned int length = state->gamma_lut->length /
  406. sizeof(struct drm_color_lut);
  407. if (length < 2)
  408. return -EINVAL;
  409. }
  410. pri_state = drm_atomic_get_new_plane_state(state->state, crtc->primary);
  411. if (pri_state) {
  412. struct omap_crtc_state *omap_crtc_state =
  413. to_omap_crtc_state(state);
  414. /* Mirror new values for zpos and rotation in omap_crtc_state */
  415. omap_crtc_state->zpos = pri_state->zpos;
  416. omap_crtc_state->rotation = pri_state->rotation;
  417. }
  418. return 0;
  419. }
  420. static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
  421. struct drm_crtc_state *old_crtc_state)
  422. {
  423. }
  424. static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
  425. struct drm_crtc_state *old_crtc_state)
  426. {
  427. struct omap_drm_private *priv = crtc->dev->dev_private;
  428. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  429. int ret;
  430. if (crtc->state->color_mgmt_changed) {
  431. struct drm_color_lut *lut = NULL;
  432. unsigned int length = 0;
  433. if (crtc->state->gamma_lut) {
  434. lut = (struct drm_color_lut *)
  435. crtc->state->gamma_lut->data;
  436. length = crtc->state->gamma_lut->length /
  437. sizeof(*lut);
  438. }
  439. priv->dispc_ops->mgr_set_gamma(priv->dispc, omap_crtc->channel,
  440. lut, length);
  441. }
  442. omap_crtc_write_crtc_properties(crtc);
  443. /* Only flush the CRTC if it is currently enabled. */
  444. if (!omap_crtc->enabled)
  445. return;
  446. DBG("%s: GO", omap_crtc->name);
  447. ret = drm_crtc_vblank_get(crtc);
  448. WARN_ON(ret != 0);
  449. spin_lock_irq(&crtc->dev->event_lock);
  450. priv->dispc_ops->mgr_go(priv->dispc, omap_crtc->channel);
  451. omap_crtc_arm_event(crtc);
  452. spin_unlock_irq(&crtc->dev->event_lock);
  453. }
  454. static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
  455. struct drm_crtc_state *state,
  456. struct drm_property *property,
  457. u64 val)
  458. {
  459. struct omap_drm_private *priv = crtc->dev->dev_private;
  460. struct drm_plane_state *plane_state;
  461. /*
  462. * Delegate property set to the primary plane. Get the plane state and
  463. * set the property directly, the shadow copy will be assigned in the
  464. * omap_crtc_atomic_check callback. This way updates to plane state will
  465. * always be mirrored in the crtc state correctly.
  466. */
  467. plane_state = drm_atomic_get_plane_state(state->state, crtc->primary);
  468. if (IS_ERR(plane_state))
  469. return PTR_ERR(plane_state);
  470. if (property == crtc->primary->rotation_property)
  471. plane_state->rotation = val;
  472. else if (property == priv->zorder_prop)
  473. plane_state->zpos = val;
  474. else
  475. return -EINVAL;
  476. return 0;
  477. }
  478. static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
  479. const struct drm_crtc_state *state,
  480. struct drm_property *property,
  481. u64 *val)
  482. {
  483. struct omap_drm_private *priv = crtc->dev->dev_private;
  484. struct omap_crtc_state *omap_state = to_omap_crtc_state(state);
  485. if (property == crtc->primary->rotation_property)
  486. *val = omap_state->rotation;
  487. else if (property == priv->zorder_prop)
  488. *val = omap_state->zpos;
  489. else
  490. return -EINVAL;
  491. return 0;
  492. }
  493. static void omap_crtc_reset(struct drm_crtc *crtc)
  494. {
  495. if (crtc->state)
  496. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  497. kfree(crtc->state);
  498. crtc->state = kzalloc(sizeof(struct omap_crtc_state), GFP_KERNEL);
  499. if (crtc->state)
  500. crtc->state->crtc = crtc;
  501. }
  502. static struct drm_crtc_state *
  503. omap_crtc_duplicate_state(struct drm_crtc *crtc)
  504. {
  505. struct omap_crtc_state *state, *current_state;
  506. if (WARN_ON(!crtc->state))
  507. return NULL;
  508. current_state = to_omap_crtc_state(crtc->state);
  509. state = kmalloc(sizeof(*state), GFP_KERNEL);
  510. if (!state)
  511. return NULL;
  512. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  513. state->zpos = current_state->zpos;
  514. state->rotation = current_state->rotation;
  515. return &state->base;
  516. }
  517. static const struct drm_crtc_funcs omap_crtc_funcs = {
  518. .reset = omap_crtc_reset,
  519. .set_config = drm_atomic_helper_set_config,
  520. .destroy = omap_crtc_destroy,
  521. .page_flip = drm_atomic_helper_page_flip,
  522. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  523. .atomic_duplicate_state = omap_crtc_duplicate_state,
  524. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  525. .atomic_set_property = omap_crtc_atomic_set_property,
  526. .atomic_get_property = omap_crtc_atomic_get_property,
  527. .enable_vblank = omap_irq_enable_vblank,
  528. .disable_vblank = omap_irq_disable_vblank,
  529. };
  530. static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
  531. .mode_set_nofb = omap_crtc_mode_set_nofb,
  532. .atomic_check = omap_crtc_atomic_check,
  533. .atomic_begin = omap_crtc_atomic_begin,
  534. .atomic_flush = omap_crtc_atomic_flush,
  535. .atomic_enable = omap_crtc_atomic_enable,
  536. .atomic_disable = omap_crtc_atomic_disable,
  537. .mode_valid = omap_crtc_mode_valid,
  538. };
  539. /* -----------------------------------------------------------------------------
  540. * Init and Cleanup
  541. */
  542. static const char *channel_names[] = {
  543. [OMAP_DSS_CHANNEL_LCD] = "lcd",
  544. [OMAP_DSS_CHANNEL_DIGIT] = "tv",
  545. [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
  546. [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
  547. };
  548. void omap_crtc_pre_init(struct omap_drm_private *priv)
  549. {
  550. memset(omap_crtcs, 0, sizeof(omap_crtcs));
  551. dss_install_mgr_ops(&mgr_ops, priv);
  552. }
  553. void omap_crtc_pre_uninit(void)
  554. {
  555. dss_uninstall_mgr_ops();
  556. }
  557. /* initialize crtc */
  558. struct drm_crtc *omap_crtc_init(struct drm_device *dev,
  559. struct drm_plane *plane, struct omap_dss_device *dssdev)
  560. {
  561. struct omap_drm_private *priv = dev->dev_private;
  562. struct drm_crtc *crtc = NULL;
  563. struct omap_crtc *omap_crtc;
  564. enum omap_channel channel;
  565. struct omap_dss_device *out;
  566. int ret;
  567. out = omapdss_find_output_from_display(dssdev);
  568. channel = out->dispc_channel;
  569. omap_dss_put_device(out);
  570. DBG("%s", channel_names[channel]);
  571. /* Multiple displays on same channel is not allowed */
  572. if (WARN_ON(omap_crtcs[channel] != NULL))
  573. return ERR_PTR(-EINVAL);
  574. omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
  575. if (!omap_crtc)
  576. return ERR_PTR(-ENOMEM);
  577. crtc = &omap_crtc->base;
  578. init_waitqueue_head(&omap_crtc->pending_wait);
  579. omap_crtc->channel = channel;
  580. omap_crtc->name = channel_names[channel];
  581. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  582. &omap_crtc_funcs, NULL);
  583. if (ret < 0) {
  584. dev_err(dev->dev, "%s(): could not init crtc for: %s\n",
  585. __func__, dssdev->name);
  586. kfree(omap_crtc);
  587. return ERR_PTR(ret);
  588. }
  589. drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
  590. /* The dispc API adapts to what ever size, but the HW supports
  591. * 256 element gamma table for LCDs and 1024 element table for
  592. * OMAP_DSS_CHANNEL_DIGIT. X server assumes 256 element gamma
  593. * tables so lets use that. Size of HW gamma table can be
  594. * extracted with dispc_mgr_gamma_size(). If it returns 0
  595. * gamma table is not supprted.
  596. */
  597. if (priv->dispc_ops->mgr_gamma_size(priv->dispc, channel)) {
  598. unsigned int gamma_lut_size = 256;
  599. drm_crtc_enable_color_mgmt(crtc, 0, false, gamma_lut_size);
  600. drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
  601. }
  602. omap_plane_install_properties(crtc->primary, &crtc->base);
  603. omap_crtcs[channel] = omap_crtc;
  604. return crtc;
  605. }