omap_crtc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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(x) container_of(x, struct omap_crtc, base)
  27. struct omap_crtc {
  28. struct drm_crtc base;
  29. const char *name;
  30. enum omap_channel channel;
  31. struct videomode vm;
  32. struct omap_drm_irq vblank_irq;
  33. bool ignore_digit_sync_lost;
  34. bool pending;
  35. wait_queue_head_t pending_wait;
  36. };
  37. /* -----------------------------------------------------------------------------
  38. * Helper Functions
  39. */
  40. uint32_t pipe2vbl(struct drm_crtc *crtc)
  41. {
  42. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  43. return dispc_mgr_get_vsync_irq(omap_crtc->channel);
  44. }
  45. struct videomode *omap_crtc_timings(struct drm_crtc *crtc)
  46. {
  47. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  48. return &omap_crtc->vm;
  49. }
  50. enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
  51. {
  52. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  53. return omap_crtc->channel;
  54. }
  55. int omap_crtc_wait_pending(struct drm_crtc *crtc)
  56. {
  57. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  58. /*
  59. * Timeout is set to a "sufficiently" high value, which should cover
  60. * a single frame refresh even on slower displays.
  61. */
  62. return wait_event_timeout(omap_crtc->pending_wait,
  63. !omap_crtc->pending,
  64. msecs_to_jiffies(250));
  65. }
  66. /* -----------------------------------------------------------------------------
  67. * DSS Manager Functions
  68. */
  69. /*
  70. * Manager-ops, callbacks from output when they need to configure
  71. * the upstream part of the video pipe.
  72. *
  73. * Most of these we can ignore until we add support for command-mode
  74. * panels.. for video-mode the crtc-helpers already do an adequate
  75. * job of sequencing the setup of the video pipe in the proper order
  76. */
  77. /* ovl-mgr-id -> crtc */
  78. static struct omap_crtc *omap_crtcs[8];
  79. static struct omap_dss_device *omap_crtc_output[8];
  80. /* we can probably ignore these until we support command-mode panels: */
  81. static int omap_crtc_dss_connect(enum omap_channel channel,
  82. struct omap_dss_device *dst)
  83. {
  84. if (omap_crtc_output[channel])
  85. return -EINVAL;
  86. if ((dispc_mgr_get_supported_outputs(channel) & dst->id) == 0)
  87. return -EINVAL;
  88. omap_crtc_output[channel] = dst;
  89. dst->dispc_channel_connected = true;
  90. return 0;
  91. }
  92. static void omap_crtc_dss_disconnect(enum omap_channel channel,
  93. struct omap_dss_device *dst)
  94. {
  95. omap_crtc_output[channel] = NULL;
  96. dst->dispc_channel_connected = false;
  97. }
  98. static void omap_crtc_dss_start_update(enum omap_channel channel)
  99. {
  100. }
  101. /* Called only from the encoder enable/disable and suspend/resume handlers. */
  102. static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
  103. {
  104. struct drm_device *dev = crtc->dev;
  105. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  106. enum omap_channel channel = omap_crtc->channel;
  107. struct omap_irq_wait *wait;
  108. u32 framedone_irq, vsync_irq;
  109. int ret;
  110. if (omap_crtc_output[channel]->output_type == OMAP_DISPLAY_TYPE_HDMI) {
  111. dispc_mgr_enable(channel, enable);
  112. return;
  113. }
  114. if (dispc_mgr_is_enabled(channel) == enable)
  115. return;
  116. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  117. /*
  118. * Digit output produces some sync lost interrupts during the
  119. * first frame when enabling, so we need to ignore those.
  120. */
  121. omap_crtc->ignore_digit_sync_lost = true;
  122. }
  123. framedone_irq = dispc_mgr_get_framedone_irq(channel);
  124. vsync_irq = dispc_mgr_get_vsync_irq(channel);
  125. if (enable) {
  126. wait = omap_irq_wait_init(dev, vsync_irq, 1);
  127. } else {
  128. /*
  129. * When we disable the digit output, we need to wait for
  130. * FRAMEDONE to know that DISPC has finished with the output.
  131. *
  132. * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
  133. * that case we need to use vsync interrupt, and wait for both
  134. * even and odd frames.
  135. */
  136. if (framedone_irq)
  137. wait = omap_irq_wait_init(dev, framedone_irq, 1);
  138. else
  139. wait = omap_irq_wait_init(dev, vsync_irq, 2);
  140. }
  141. dispc_mgr_enable(channel, enable);
  142. ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
  143. if (ret) {
  144. dev_err(dev->dev, "%s: timeout waiting for %s\n",
  145. omap_crtc->name, enable ? "enable" : "disable");
  146. }
  147. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  148. omap_crtc->ignore_digit_sync_lost = false;
  149. /* make sure the irq handler sees the value above */
  150. mb();
  151. }
  152. }
  153. static int omap_crtc_dss_enable(enum omap_channel channel)
  154. {
  155. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  156. struct omap_overlay_manager_info info;
  157. memset(&info, 0, sizeof(info));
  158. info.default_color = 0x00000000;
  159. info.trans_key = 0x00000000;
  160. info.trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  161. info.trans_enabled = false;
  162. dispc_mgr_setup(omap_crtc->channel, &info);
  163. dispc_mgr_set_timings(omap_crtc->channel,
  164. &omap_crtc->vm);
  165. omap_crtc_set_enabled(&omap_crtc->base, true);
  166. return 0;
  167. }
  168. static void omap_crtc_dss_disable(enum omap_channel channel)
  169. {
  170. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  171. omap_crtc_set_enabled(&omap_crtc->base, false);
  172. }
  173. static void omap_crtc_dss_set_timings(enum omap_channel channel,
  174. const struct videomode *vm)
  175. {
  176. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  177. DBG("%s", omap_crtc->name);
  178. omap_crtc->vm = *vm;
  179. }
  180. static void omap_crtc_dss_set_lcd_config(enum omap_channel channel,
  181. const struct dss_lcd_mgr_config *config)
  182. {
  183. struct omap_crtc *omap_crtc = omap_crtcs[channel];
  184. DBG("%s", omap_crtc->name);
  185. dispc_mgr_set_lcd_config(omap_crtc->channel, config);
  186. }
  187. static int omap_crtc_dss_register_framedone(
  188. enum omap_channel channel,
  189. void (*handler)(void *), void *data)
  190. {
  191. return 0;
  192. }
  193. static void omap_crtc_dss_unregister_framedone(
  194. enum omap_channel channel,
  195. void (*handler)(void *), void *data)
  196. {
  197. }
  198. static const struct dss_mgr_ops mgr_ops = {
  199. .connect = omap_crtc_dss_connect,
  200. .disconnect = omap_crtc_dss_disconnect,
  201. .start_update = omap_crtc_dss_start_update,
  202. .enable = omap_crtc_dss_enable,
  203. .disable = omap_crtc_dss_disable,
  204. .set_timings = omap_crtc_dss_set_timings,
  205. .set_lcd_config = omap_crtc_dss_set_lcd_config,
  206. .register_framedone_handler = omap_crtc_dss_register_framedone,
  207. .unregister_framedone_handler = omap_crtc_dss_unregister_framedone,
  208. };
  209. /* -----------------------------------------------------------------------------
  210. * Setup, Flush and Page Flip
  211. */
  212. static void omap_crtc_complete_page_flip(struct drm_crtc *crtc)
  213. {
  214. struct drm_pending_vblank_event *event;
  215. struct drm_device *dev = crtc->dev;
  216. unsigned long flags;
  217. event = crtc->state->event;
  218. if (!event)
  219. return;
  220. spin_lock_irqsave(&dev->event_lock, flags);
  221. drm_crtc_send_vblank_event(crtc, event);
  222. spin_unlock_irqrestore(&dev->event_lock, flags);
  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. static void omap_crtc_vblank_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  235. {
  236. struct omap_crtc *omap_crtc =
  237. container_of(irq, struct omap_crtc, vblank_irq);
  238. struct drm_device *dev = omap_crtc->base.dev;
  239. if (dispc_mgr_go_busy(omap_crtc->channel))
  240. return;
  241. DBG("%s: apply done", omap_crtc->name);
  242. __omap_irq_unregister(dev, &omap_crtc->vblank_irq);
  243. rmb();
  244. WARN_ON(!omap_crtc->pending);
  245. omap_crtc->pending = false;
  246. wmb();
  247. /* wake up userspace */
  248. omap_crtc_complete_page_flip(&omap_crtc->base);
  249. /* wake up omap_atomic_complete */
  250. wake_up(&omap_crtc->pending_wait);
  251. }
  252. /* -----------------------------------------------------------------------------
  253. * CRTC Functions
  254. */
  255. static void omap_crtc_destroy(struct drm_crtc *crtc)
  256. {
  257. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  258. DBG("%s", omap_crtc->name);
  259. WARN_ON(omap_crtc->vblank_irq.registered);
  260. drm_crtc_cleanup(crtc);
  261. kfree(omap_crtc);
  262. }
  263. static void omap_crtc_enable(struct drm_crtc *crtc)
  264. {
  265. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  266. DBG("%s", omap_crtc->name);
  267. rmb();
  268. WARN_ON(omap_crtc->pending);
  269. omap_crtc->pending = true;
  270. wmb();
  271. omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
  272. drm_crtc_vblank_on(crtc);
  273. }
  274. static void omap_crtc_disable(struct drm_crtc *crtc)
  275. {
  276. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  277. DBG("%s", omap_crtc->name);
  278. drm_crtc_vblank_off(crtc);
  279. }
  280. static void omap_crtc_mode_set_nofb(struct drm_crtc *crtc)
  281. {
  282. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  283. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  284. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  285. omap_crtc->name, mode->base.id, mode->name,
  286. mode->vrefresh, mode->clock,
  287. mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
  288. mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal,
  289. mode->type, mode->flags);
  290. drm_display_mode_to_videomode(mode, &omap_crtc->vm);
  291. omap_crtc->vm.flags |= DISPLAY_FLAGS_DE_HIGH |
  292. DISPLAY_FLAGS_PIXDATA_POSEDGE |
  293. DISPLAY_FLAGS_SYNC_NEGEDGE;
  294. }
  295. static int omap_crtc_atomic_check(struct drm_crtc *crtc,
  296. struct drm_crtc_state *state)
  297. {
  298. if (state->color_mgmt_changed && state->gamma_lut) {
  299. uint length = state->gamma_lut->length /
  300. sizeof(struct drm_color_lut);
  301. if (length < 2)
  302. return -EINVAL;
  303. }
  304. return 0;
  305. }
  306. static void omap_crtc_atomic_begin(struct drm_crtc *crtc,
  307. struct drm_crtc_state *old_crtc_state)
  308. {
  309. }
  310. static void omap_crtc_atomic_flush(struct drm_crtc *crtc,
  311. struct drm_crtc_state *old_crtc_state)
  312. {
  313. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  314. WARN_ON(omap_crtc->vblank_irq.registered);
  315. if (crtc->state->color_mgmt_changed) {
  316. struct drm_color_lut *lut = NULL;
  317. uint length = 0;
  318. if (crtc->state->gamma_lut) {
  319. lut = (struct drm_color_lut *)
  320. crtc->state->gamma_lut->data;
  321. length = crtc->state->gamma_lut->length /
  322. sizeof(*lut);
  323. }
  324. dispc_mgr_set_gamma(omap_crtc->channel, lut, length);
  325. }
  326. if (dispc_mgr_is_enabled(omap_crtc->channel)) {
  327. DBG("%s: GO", omap_crtc->name);
  328. rmb();
  329. WARN_ON(omap_crtc->pending);
  330. omap_crtc->pending = true;
  331. wmb();
  332. dispc_mgr_go(omap_crtc->channel);
  333. omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
  334. }
  335. }
  336. static bool omap_crtc_is_plane_prop(struct drm_crtc *crtc,
  337. struct drm_property *property)
  338. {
  339. struct drm_device *dev = crtc->dev;
  340. struct omap_drm_private *priv = dev->dev_private;
  341. return property == priv->zorder_prop ||
  342. property == crtc->primary->rotation_property;
  343. }
  344. static int omap_crtc_atomic_set_property(struct drm_crtc *crtc,
  345. struct drm_crtc_state *state,
  346. struct drm_property *property,
  347. uint64_t val)
  348. {
  349. if (omap_crtc_is_plane_prop(crtc, property)) {
  350. struct drm_plane_state *plane_state;
  351. struct drm_plane *plane = crtc->primary;
  352. /*
  353. * Delegate property set to the primary plane. Get the plane
  354. * state and set the property directly.
  355. */
  356. plane_state = drm_atomic_get_plane_state(state->state, plane);
  357. if (IS_ERR(plane_state))
  358. return PTR_ERR(plane_state);
  359. return drm_atomic_plane_set_property(plane, plane_state,
  360. property, val);
  361. }
  362. return -EINVAL;
  363. }
  364. static int omap_crtc_atomic_get_property(struct drm_crtc *crtc,
  365. const struct drm_crtc_state *state,
  366. struct drm_property *property,
  367. uint64_t *val)
  368. {
  369. if (omap_crtc_is_plane_prop(crtc, property)) {
  370. /*
  371. * Delegate property get to the primary plane. The
  372. * drm_atomic_plane_get_property() function isn't exported, but
  373. * can be called through drm_object_property_get_value() as that
  374. * will call drm_atomic_get_property() for atomic drivers.
  375. */
  376. return drm_object_property_get_value(&crtc->primary->base,
  377. property, val);
  378. }
  379. return -EINVAL;
  380. }
  381. static const struct drm_crtc_funcs omap_crtc_funcs = {
  382. .reset = drm_atomic_helper_crtc_reset,
  383. .set_config = drm_atomic_helper_set_config,
  384. .destroy = omap_crtc_destroy,
  385. .page_flip = drm_atomic_helper_page_flip,
  386. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  387. .set_property = drm_atomic_helper_crtc_set_property,
  388. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  389. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  390. .atomic_set_property = omap_crtc_atomic_set_property,
  391. .atomic_get_property = omap_crtc_atomic_get_property,
  392. };
  393. static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
  394. .mode_set_nofb = omap_crtc_mode_set_nofb,
  395. .disable = omap_crtc_disable,
  396. .enable = omap_crtc_enable,
  397. .atomic_check = omap_crtc_atomic_check,
  398. .atomic_begin = omap_crtc_atomic_begin,
  399. .atomic_flush = omap_crtc_atomic_flush,
  400. };
  401. /* -----------------------------------------------------------------------------
  402. * Init and Cleanup
  403. */
  404. static const char *channel_names[] = {
  405. [OMAP_DSS_CHANNEL_LCD] = "lcd",
  406. [OMAP_DSS_CHANNEL_DIGIT] = "tv",
  407. [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
  408. [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
  409. };
  410. void omap_crtc_pre_init(void)
  411. {
  412. dss_install_mgr_ops(&mgr_ops);
  413. }
  414. void omap_crtc_pre_uninit(void)
  415. {
  416. dss_uninstall_mgr_ops();
  417. }
  418. /* initialize crtc */
  419. struct drm_crtc *omap_crtc_init(struct drm_device *dev,
  420. struct drm_plane *plane, enum omap_channel channel, int id)
  421. {
  422. struct drm_crtc *crtc = NULL;
  423. struct omap_crtc *omap_crtc;
  424. int ret;
  425. DBG("%s", channel_names[channel]);
  426. omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
  427. if (!omap_crtc)
  428. return NULL;
  429. crtc = &omap_crtc->base;
  430. init_waitqueue_head(&omap_crtc->pending_wait);
  431. omap_crtc->channel = channel;
  432. omap_crtc->name = channel_names[channel];
  433. omap_crtc->vblank_irq.irqmask = pipe2vbl(crtc);
  434. omap_crtc->vblank_irq.irq = omap_crtc_vblank_irq;
  435. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  436. &omap_crtc_funcs, NULL);
  437. if (ret < 0) {
  438. kfree(omap_crtc);
  439. return NULL;
  440. }
  441. drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
  442. /* The dispc API adapts to what ever size, but the HW supports
  443. * 256 element gamma table for LCDs and 1024 element table for
  444. * OMAP_DSS_CHANNEL_DIGIT. X server assumes 256 element gamma
  445. * tables so lets use that. Size of HW gamma table can be
  446. * extracted with dispc_mgr_gamma_size(). If it returns 0
  447. * gamma table is not supprted.
  448. */
  449. if (dispc_mgr_gamma_size(channel)) {
  450. uint gamma_lut_size = 256;
  451. drm_crtc_enable_color_mgmt(crtc, 0, false, gamma_lut_size);
  452. drm_mode_crtc_set_gamma_size(crtc, gamma_lut_size);
  453. }
  454. omap_plane_install_properties(crtc->primary, &crtc->base);
  455. omap_crtcs[channel] = omap_crtc;
  456. return crtc;
  457. }