omap_crtc.c 19 KB

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