sti_drm_crtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
  4. * Fabien Dessenne <fabien.dessenne@st.com>
  5. * for STMicroelectronics.
  6. * License terms: GNU General Public License (GPL), version 2
  7. */
  8. #include <linux/clk.h>
  9. #include <drm/drmP.h>
  10. #include <drm/drm_crtc_helper.h>
  11. #include "sti_compositor.h"
  12. #include "sti_drm_drv.h"
  13. #include "sti_drm_crtc.h"
  14. #include "sti_vtg.h"
  15. static void sti_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  16. {
  17. DRM_DEBUG_KMS("\n");
  18. }
  19. static void sti_drm_crtc_prepare(struct drm_crtc *crtc)
  20. {
  21. struct sti_mixer *mixer = to_sti_mixer(crtc);
  22. struct device *dev = mixer->dev;
  23. struct sti_compositor *compo = dev_get_drvdata(dev);
  24. compo->enable = true;
  25. /* Prepare and enable the compo IP clock */
  26. if (mixer->id == STI_MIXER_MAIN) {
  27. if (clk_prepare_enable(compo->clk_compo_main))
  28. DRM_INFO("Failed to prepare/enable compo_main clk\n");
  29. } else {
  30. if (clk_prepare_enable(compo->clk_compo_aux))
  31. DRM_INFO("Failed to prepare/enable compo_aux clk\n");
  32. }
  33. }
  34. static void sti_drm_crtc_commit(struct drm_crtc *crtc)
  35. {
  36. struct sti_mixer *mixer = to_sti_mixer(crtc);
  37. struct device *dev = mixer->dev;
  38. struct sti_compositor *compo = dev_get_drvdata(dev);
  39. struct sti_layer *layer;
  40. if ((!mixer || !compo)) {
  41. DRM_ERROR("Can not find mixer or compositor)\n");
  42. return;
  43. }
  44. /* get GDP which is reserved to the CRTC FB */
  45. layer = to_sti_layer(crtc->primary);
  46. if (layer)
  47. sti_layer_commit(layer);
  48. else
  49. DRM_ERROR("Can not find CRTC dedicated plane (GDP0)\n");
  50. /* Enable layer on mixer */
  51. if (sti_mixer_set_layer_status(mixer, layer, true))
  52. DRM_ERROR("Can not enable layer at mixer\n");
  53. }
  54. static bool sti_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  55. const struct drm_display_mode *mode,
  56. struct drm_display_mode *adjusted_mode)
  57. {
  58. /* accept the provided drm_display_mode, do not fix it up */
  59. return true;
  60. }
  61. static int
  62. sti_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  63. struct drm_display_mode *adjusted_mode, int x, int y,
  64. struct drm_framebuffer *old_fb)
  65. {
  66. struct sti_mixer *mixer = to_sti_mixer(crtc);
  67. struct device *dev = mixer->dev;
  68. struct sti_compositor *compo = dev_get_drvdata(dev);
  69. struct sti_layer *layer;
  70. struct clk *clk;
  71. int rate = mode->clock * 1000;
  72. int res;
  73. unsigned int w, h;
  74. DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d mode:%d (%s)\n",
  75. crtc->base.id, sti_mixer_to_str(mixer),
  76. crtc->primary->fb->base.id, mode->base.id, mode->name);
  77. DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
  78. mode->vrefresh, mode->clock,
  79. mode->hdisplay,
  80. mode->hsync_start, mode->hsync_end,
  81. mode->htotal,
  82. mode->vdisplay,
  83. mode->vsync_start, mode->vsync_end,
  84. mode->vtotal, mode->type, mode->flags);
  85. /* Set rate and prepare/enable pixel clock */
  86. if (mixer->id == STI_MIXER_MAIN)
  87. clk = compo->clk_pix_main;
  88. else
  89. clk = compo->clk_pix_aux;
  90. res = clk_set_rate(clk, rate);
  91. if (res < 0) {
  92. DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
  93. return -EINVAL;
  94. }
  95. if (clk_prepare_enable(clk)) {
  96. DRM_ERROR("Failed to prepare/enable pix clk\n");
  97. return -EINVAL;
  98. }
  99. sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
  100. compo->vtg_main : compo->vtg_aux, &crtc->mode);
  101. /* a GDP is reserved to the CRTC FB */
  102. layer = to_sti_layer(crtc->primary);
  103. if (!layer) {
  104. DRM_ERROR("Can not find GDP0)\n");
  105. return -EINVAL;
  106. }
  107. /* copy the mode data adjusted by mode_fixup() into crtc->mode
  108. * so that hardware can be set to proper mode
  109. */
  110. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  111. res = sti_mixer_set_layer_depth(mixer, layer);
  112. if (res) {
  113. DRM_ERROR("Can not set layer depth\n");
  114. return -EINVAL;
  115. }
  116. res = sti_mixer_active_video_area(mixer, &crtc->mode);
  117. if (res) {
  118. DRM_ERROR("Can not set active video area\n");
  119. return -EINVAL;
  120. }
  121. w = crtc->primary->fb->width - x;
  122. h = crtc->primary->fb->height - y;
  123. return sti_layer_prepare(layer, crtc->primary->fb, &crtc->mode,
  124. mixer->id, 0, 0, w, h, x, y, w, h);
  125. }
  126. static int sti_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  127. struct drm_framebuffer *old_fb)
  128. {
  129. struct sti_mixer *mixer = to_sti_mixer(crtc);
  130. struct sti_layer *layer;
  131. unsigned int w, h;
  132. int ret;
  133. DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d (%d,%d)\n",
  134. crtc->base.id, sti_mixer_to_str(mixer),
  135. crtc->primary->fb->base.id, x, y);
  136. /* GDP is reserved to the CRTC FB */
  137. layer = to_sti_layer(crtc->primary);
  138. if (!layer) {
  139. DRM_ERROR("Can not find GDP0)\n");
  140. ret = -EINVAL;
  141. goto out;
  142. }
  143. w = crtc->primary->fb->width - crtc->x;
  144. h = crtc->primary->fb->height - crtc->y;
  145. ret = sti_layer_prepare(layer, crtc->primary->fb, &crtc->mode,
  146. mixer->id, 0, 0, w, h,
  147. crtc->x, crtc->y, w, h);
  148. if (ret) {
  149. DRM_ERROR("Can not prepare layer\n");
  150. goto out;
  151. }
  152. sti_drm_crtc_commit(crtc);
  153. out:
  154. return ret;
  155. }
  156. static void sti_drm_crtc_load_lut(struct drm_crtc *crtc)
  157. {
  158. /* do nothing */
  159. }
  160. static void sti_drm_crtc_disable(struct drm_crtc *crtc)
  161. {
  162. struct sti_mixer *mixer = to_sti_mixer(crtc);
  163. struct device *dev = mixer->dev;
  164. struct sti_compositor *compo = dev_get_drvdata(dev);
  165. struct sti_layer *layer;
  166. if (!compo->enable)
  167. return;
  168. DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
  169. /* Disable Background */
  170. sti_mixer_set_background_status(mixer, false);
  171. /* Disable GDP */
  172. layer = to_sti_layer(crtc->primary);
  173. if (!layer) {
  174. DRM_ERROR("Cannot find GDP0\n");
  175. return;
  176. }
  177. /* Disable layer at mixer level */
  178. if (sti_mixer_set_layer_status(mixer, layer, false))
  179. DRM_ERROR("Can not disable %s layer at mixer\n",
  180. sti_layer_to_str(layer));
  181. /* Wait a while to be sure that a Vsync event is received */
  182. msleep(WAIT_NEXT_VSYNC_MS);
  183. /* Then disable layer itself */
  184. sti_layer_disable(layer);
  185. drm_vblank_off(crtc->dev, mixer->id);
  186. /* Disable pixel clock and compo IP clocks */
  187. if (mixer->id == STI_MIXER_MAIN) {
  188. clk_disable_unprepare(compo->clk_pix_main);
  189. clk_disable_unprepare(compo->clk_compo_main);
  190. } else {
  191. clk_disable_unprepare(compo->clk_pix_aux);
  192. clk_disable_unprepare(compo->clk_compo_aux);
  193. }
  194. compo->enable = false;
  195. }
  196. static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
  197. .dpms = sti_drm_crtc_dpms,
  198. .prepare = sti_drm_crtc_prepare,
  199. .commit = sti_drm_crtc_commit,
  200. .mode_fixup = sti_drm_crtc_mode_fixup,
  201. .mode_set = sti_drm_crtc_mode_set,
  202. .mode_set_base = sti_drm_crtc_mode_set_base,
  203. .load_lut = sti_drm_crtc_load_lut,
  204. .disable = sti_drm_crtc_disable,
  205. };
  206. static int sti_drm_crtc_page_flip(struct drm_crtc *crtc,
  207. struct drm_framebuffer *fb,
  208. struct drm_pending_vblank_event *event,
  209. uint32_t page_flip_flags)
  210. {
  211. struct drm_device *drm_dev = crtc->dev;
  212. struct drm_framebuffer *old_fb;
  213. struct sti_mixer *mixer = to_sti_mixer(crtc);
  214. unsigned long flags;
  215. int ret;
  216. DRM_DEBUG_KMS("fb %d --> fb %d\n",
  217. crtc->primary->fb->base.id, fb->base.id);
  218. mutex_lock(&drm_dev->struct_mutex);
  219. old_fb = crtc->primary->fb;
  220. crtc->primary->fb = fb;
  221. ret = sti_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y, old_fb);
  222. if (ret) {
  223. DRM_ERROR("failed\n");
  224. crtc->primary->fb = old_fb;
  225. goto out;
  226. }
  227. if (event) {
  228. event->pipe = mixer->id;
  229. ret = drm_vblank_get(drm_dev, event->pipe);
  230. if (ret) {
  231. DRM_ERROR("Cannot get vblank\n");
  232. goto out;
  233. }
  234. spin_lock_irqsave(&drm_dev->event_lock, flags);
  235. if (mixer->pending_event) {
  236. drm_vblank_put(drm_dev, event->pipe);
  237. ret = -EBUSY;
  238. } else {
  239. mixer->pending_event = event;
  240. }
  241. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  242. }
  243. out:
  244. mutex_unlock(&drm_dev->struct_mutex);
  245. return ret;
  246. }
  247. static void sti_drm_crtc_destroy(struct drm_crtc *crtc)
  248. {
  249. DRM_DEBUG_KMS("\n");
  250. drm_crtc_cleanup(crtc);
  251. }
  252. static int sti_drm_crtc_set_property(struct drm_crtc *crtc,
  253. struct drm_property *property,
  254. uint64_t val)
  255. {
  256. DRM_DEBUG_KMS("\n");
  257. return 0;
  258. }
  259. int sti_drm_crtc_vblank_cb(struct notifier_block *nb,
  260. unsigned long event, void *data)
  261. {
  262. struct drm_device *drm_dev;
  263. struct sti_compositor *compo =
  264. container_of(nb, struct sti_compositor, vtg_vblank_nb);
  265. int *crtc = data;
  266. unsigned long flags;
  267. struct sti_drm_private *priv;
  268. drm_dev = compo->mixer[*crtc]->drm_crtc.dev;
  269. priv = drm_dev->dev_private;
  270. if ((event != VTG_TOP_FIELD_EVENT) &&
  271. (event != VTG_BOTTOM_FIELD_EVENT)) {
  272. DRM_ERROR("unknown event: %lu\n", event);
  273. return -EINVAL;
  274. }
  275. drm_handle_vblank(drm_dev, *crtc);
  276. spin_lock_irqsave(&drm_dev->event_lock, flags);
  277. if (compo->mixer[*crtc]->pending_event) {
  278. drm_send_vblank_event(drm_dev, -1,
  279. compo->mixer[*crtc]->pending_event);
  280. drm_vblank_put(drm_dev, *crtc);
  281. compo->mixer[*crtc]->pending_event = NULL;
  282. }
  283. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  284. return 0;
  285. }
  286. int sti_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  287. {
  288. struct sti_drm_private *dev_priv = dev->dev_private;
  289. struct sti_compositor *compo = dev_priv->compo;
  290. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
  291. if (sti_vtg_register_client(crtc == STI_MIXER_MAIN ?
  292. compo->vtg_main : compo->vtg_aux,
  293. vtg_vblank_nb, crtc)) {
  294. DRM_ERROR("Cannot register VTG notifier\n");
  295. return -EINVAL;
  296. }
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(sti_drm_crtc_enable_vblank);
  300. void sti_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  301. {
  302. struct sti_drm_private *priv = dev->dev_private;
  303. struct sti_compositor *compo = priv->compo;
  304. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
  305. unsigned long flags;
  306. DRM_DEBUG_DRIVER("\n");
  307. if (sti_vtg_unregister_client(crtc == STI_MIXER_MAIN ?
  308. compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
  309. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  310. /* free the resources of the pending requests */
  311. spin_lock_irqsave(&dev->event_lock, flags);
  312. if (compo->mixer[crtc]->pending_event) {
  313. drm_vblank_put(dev, crtc);
  314. compo->mixer[crtc]->pending_event = NULL;
  315. }
  316. spin_unlock_irqrestore(&dev->event_lock, flags);
  317. }
  318. EXPORT_SYMBOL(sti_drm_crtc_disable_vblank);
  319. static struct drm_crtc_funcs sti_crtc_funcs = {
  320. .set_config = drm_crtc_helper_set_config,
  321. .page_flip = sti_drm_crtc_page_flip,
  322. .destroy = sti_drm_crtc_destroy,
  323. .set_property = sti_drm_crtc_set_property,
  324. };
  325. bool sti_drm_crtc_is_main(struct drm_crtc *crtc)
  326. {
  327. struct sti_mixer *mixer = to_sti_mixer(crtc);
  328. if (mixer->id == STI_MIXER_MAIN)
  329. return true;
  330. return false;
  331. }
  332. int sti_drm_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
  333. struct drm_plane *primary, struct drm_plane *cursor)
  334. {
  335. struct drm_crtc *crtc = &mixer->drm_crtc;
  336. int res;
  337. res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
  338. &sti_crtc_funcs);
  339. if (res) {
  340. DRM_ERROR("Can not initialze CRTC\n");
  341. return -EINVAL;
  342. }
  343. drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
  344. DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
  345. crtc->base.id, sti_mixer_to_str(mixer));
  346. return 0;
  347. }