omap_crtc.c 20 KB

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