mtk_drm_crtc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <asm/barrier.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_plane_helper.h>
  18. #include <linux/clk.h>
  19. #include <linux/pm_runtime.h>
  20. #include <soc/mediatek/smi.h>
  21. #include "mtk_drm_drv.h"
  22. #include "mtk_drm_crtc.h"
  23. #include "mtk_drm_ddp.h"
  24. #include "mtk_drm_ddp_comp.h"
  25. #include "mtk_drm_gem.h"
  26. #include "mtk_drm_plane.h"
  27. /**
  28. * struct mtk_drm_crtc - MediaTek specific crtc structure.
  29. * @base: crtc object.
  30. * @enabled: records whether crtc_enable succeeded
  31. * @planes: array of 4 drm_plane structures, one for each overlay plane
  32. * @pending_planes: whether any plane has pending changes to be applied
  33. * @config_regs: memory mapped mmsys configuration register space
  34. * @mutex: handle to one of the ten disp_mutex streams
  35. * @ddp_comp_nr: number of components in ddp_comp
  36. * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
  37. */
  38. struct mtk_drm_crtc {
  39. struct drm_crtc base;
  40. bool enabled;
  41. bool pending_needs_vblank;
  42. struct drm_pending_vblank_event *event;
  43. struct drm_plane planes[OVL_LAYER_NR];
  44. bool pending_planes;
  45. void __iomem *config_regs;
  46. struct mtk_disp_mutex *mutex;
  47. unsigned int ddp_comp_nr;
  48. struct mtk_ddp_comp **ddp_comp;
  49. };
  50. struct mtk_crtc_state {
  51. struct drm_crtc_state base;
  52. bool pending_config;
  53. unsigned int pending_width;
  54. unsigned int pending_height;
  55. unsigned int pending_vrefresh;
  56. };
  57. static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
  58. {
  59. return container_of(c, struct mtk_drm_crtc, base);
  60. }
  61. static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
  62. {
  63. return container_of(s, struct mtk_crtc_state, base);
  64. }
  65. static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
  66. {
  67. struct drm_crtc *crtc = &mtk_crtc->base;
  68. unsigned long flags;
  69. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  70. drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
  71. drm_crtc_vblank_put(crtc);
  72. mtk_crtc->event = NULL;
  73. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  74. }
  75. static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
  76. {
  77. drm_crtc_handle_vblank(&mtk_crtc->base);
  78. if (mtk_crtc->pending_needs_vblank) {
  79. mtk_drm_crtc_finish_page_flip(mtk_crtc);
  80. mtk_crtc->pending_needs_vblank = false;
  81. }
  82. }
  83. static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
  84. {
  85. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  86. int i;
  87. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
  88. clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
  89. mtk_disp_mutex_put(mtk_crtc->mutex);
  90. drm_crtc_cleanup(crtc);
  91. }
  92. static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
  93. {
  94. struct mtk_crtc_state *state;
  95. if (crtc->state) {
  96. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  97. state = to_mtk_crtc_state(crtc->state);
  98. memset(state, 0, sizeof(*state));
  99. } else {
  100. state = kzalloc(sizeof(*state), GFP_KERNEL);
  101. if (!state)
  102. return;
  103. crtc->state = &state->base;
  104. }
  105. state->base.crtc = crtc;
  106. }
  107. static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
  108. {
  109. struct mtk_crtc_state *state;
  110. state = kzalloc(sizeof(*state), GFP_KERNEL);
  111. if (!state)
  112. return NULL;
  113. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  114. WARN_ON(state->base.crtc != crtc);
  115. state->base.crtc = crtc;
  116. return &state->base;
  117. }
  118. static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
  119. struct drm_crtc_state *state)
  120. {
  121. __drm_atomic_helper_crtc_destroy_state(state);
  122. kfree(to_mtk_crtc_state(state));
  123. }
  124. static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  125. const struct drm_display_mode *mode,
  126. struct drm_display_mode *adjusted_mode)
  127. {
  128. /* Nothing to do here, but this callback is mandatory. */
  129. return true;
  130. }
  131. static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
  132. {
  133. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  134. state->pending_width = crtc->mode.hdisplay;
  135. state->pending_height = crtc->mode.vdisplay;
  136. state->pending_vrefresh = crtc->mode.vrefresh;
  137. wmb(); /* Make sure the above parameters are set before update */
  138. state->pending_config = true;
  139. }
  140. static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
  141. {
  142. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  143. struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
  144. mtk_ddp_comp_enable_vblank(ovl, &mtk_crtc->base);
  145. return 0;
  146. }
  147. static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
  148. {
  149. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  150. struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
  151. mtk_ddp_comp_disable_vblank(ovl);
  152. }
  153. static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
  154. {
  155. int ret;
  156. int i;
  157. DRM_DEBUG_DRIVER("%s\n", __func__);
  158. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
  159. ret = clk_enable(mtk_crtc->ddp_comp[i]->clk);
  160. if (ret) {
  161. DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
  162. goto err;
  163. }
  164. }
  165. return 0;
  166. err:
  167. while (--i >= 0)
  168. clk_disable(mtk_crtc->ddp_comp[i]->clk);
  169. return ret;
  170. }
  171. static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
  172. {
  173. int i;
  174. DRM_DEBUG_DRIVER("%s\n", __func__);
  175. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
  176. clk_disable(mtk_crtc->ddp_comp[i]->clk);
  177. }
  178. static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
  179. {
  180. struct drm_crtc *crtc = &mtk_crtc->base;
  181. struct drm_connector *connector;
  182. struct drm_encoder *encoder;
  183. struct drm_connector_list_iter conn_iter;
  184. unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
  185. int ret;
  186. int i;
  187. DRM_DEBUG_DRIVER("%s\n", __func__);
  188. if (WARN_ON(!crtc->state))
  189. return -EINVAL;
  190. width = crtc->state->adjusted_mode.hdisplay;
  191. height = crtc->state->adjusted_mode.vdisplay;
  192. vrefresh = crtc->state->adjusted_mode.vrefresh;
  193. drm_for_each_encoder(encoder, crtc->dev) {
  194. if (encoder->crtc != crtc)
  195. continue;
  196. drm_connector_list_iter_begin(crtc->dev, &conn_iter);
  197. drm_for_each_connector_iter(connector, &conn_iter) {
  198. if (connector->encoder != encoder)
  199. continue;
  200. if (connector->display_info.bpc != 0 &&
  201. bpc > connector->display_info.bpc)
  202. bpc = connector->display_info.bpc;
  203. }
  204. drm_connector_list_iter_end(&conn_iter);
  205. }
  206. ret = pm_runtime_get_sync(crtc->dev->dev);
  207. if (ret < 0) {
  208. DRM_ERROR("Failed to enable power domain: %d\n", ret);
  209. return ret;
  210. }
  211. ret = mtk_disp_mutex_prepare(mtk_crtc->mutex);
  212. if (ret < 0) {
  213. DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
  214. goto err_pm_runtime_put;
  215. }
  216. ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
  217. if (ret < 0) {
  218. DRM_ERROR("Failed to enable component clocks: %d\n", ret);
  219. goto err_mutex_unprepare;
  220. }
  221. DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
  222. for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
  223. mtk_ddp_add_comp_to_path(mtk_crtc->config_regs,
  224. mtk_crtc->ddp_comp[i]->id,
  225. mtk_crtc->ddp_comp[i + 1]->id);
  226. mtk_disp_mutex_add_comp(mtk_crtc->mutex,
  227. mtk_crtc->ddp_comp[i]->id);
  228. }
  229. mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
  230. mtk_disp_mutex_enable(mtk_crtc->mutex);
  231. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
  232. struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
  233. mtk_ddp_comp_config(comp, width, height, vrefresh, bpc);
  234. mtk_ddp_comp_start(comp);
  235. }
  236. /* Initially configure all planes */
  237. for (i = 0; i < OVL_LAYER_NR; i++) {
  238. struct drm_plane *plane = &mtk_crtc->planes[i];
  239. struct mtk_plane_state *plane_state;
  240. plane_state = to_mtk_plane_state(plane->state);
  241. mtk_ddp_comp_layer_config(mtk_crtc->ddp_comp[0], i,
  242. plane_state);
  243. }
  244. return 0;
  245. err_mutex_unprepare:
  246. mtk_disp_mutex_unprepare(mtk_crtc->mutex);
  247. err_pm_runtime_put:
  248. pm_runtime_put(crtc->dev->dev);
  249. return ret;
  250. }
  251. static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
  252. {
  253. struct drm_device *drm = mtk_crtc->base.dev;
  254. int i;
  255. DRM_DEBUG_DRIVER("%s\n", __func__);
  256. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
  257. mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
  258. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
  259. mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
  260. mtk_crtc->ddp_comp[i]->id);
  261. mtk_disp_mutex_disable(mtk_crtc->mutex);
  262. for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
  263. mtk_ddp_remove_comp_from_path(mtk_crtc->config_regs,
  264. mtk_crtc->ddp_comp[i]->id,
  265. mtk_crtc->ddp_comp[i + 1]->id);
  266. mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
  267. mtk_crtc->ddp_comp[i]->id);
  268. }
  269. mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
  270. mtk_crtc_ddp_clk_disable(mtk_crtc);
  271. mtk_disp_mutex_unprepare(mtk_crtc->mutex);
  272. pm_runtime_put(drm->dev);
  273. }
  274. static void mtk_crtc_ddp_config(struct drm_crtc *crtc)
  275. {
  276. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  277. struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
  278. struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
  279. unsigned int i;
  280. /*
  281. * TODO: instead of updating the registers here, we should prepare
  282. * working registers in atomic_commit and let the hardware command
  283. * queue update module registers on vblank.
  284. */
  285. if (state->pending_config) {
  286. mtk_ddp_comp_config(ovl, state->pending_width,
  287. state->pending_height,
  288. state->pending_vrefresh, 0);
  289. state->pending_config = false;
  290. }
  291. if (mtk_crtc->pending_planes) {
  292. for (i = 0; i < OVL_LAYER_NR; i++) {
  293. struct drm_plane *plane = &mtk_crtc->planes[i];
  294. struct mtk_plane_state *plane_state;
  295. plane_state = to_mtk_plane_state(plane->state);
  296. if (plane_state->pending.config) {
  297. mtk_ddp_comp_layer_config(ovl, i, plane_state);
  298. plane_state->pending.config = false;
  299. }
  300. }
  301. mtk_crtc->pending_planes = false;
  302. }
  303. }
  304. static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
  305. struct drm_crtc_state *old_state)
  306. {
  307. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  308. struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
  309. int ret;
  310. DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
  311. ret = mtk_smi_larb_get(ovl->larb_dev);
  312. if (ret) {
  313. DRM_ERROR("Failed to get larb: %d\n", ret);
  314. return;
  315. }
  316. ret = mtk_crtc_ddp_hw_init(mtk_crtc);
  317. if (ret) {
  318. mtk_smi_larb_put(ovl->larb_dev);
  319. return;
  320. }
  321. drm_crtc_vblank_on(crtc);
  322. mtk_crtc->enabled = true;
  323. }
  324. static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
  325. struct drm_crtc_state *old_state)
  326. {
  327. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  328. struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
  329. int i;
  330. DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
  331. if (!mtk_crtc->enabled)
  332. return;
  333. /* Set all pending plane state to disabled */
  334. for (i = 0; i < OVL_LAYER_NR; i++) {
  335. struct drm_plane *plane = &mtk_crtc->planes[i];
  336. struct mtk_plane_state *plane_state;
  337. plane_state = to_mtk_plane_state(plane->state);
  338. plane_state->pending.enable = false;
  339. plane_state->pending.config = true;
  340. }
  341. mtk_crtc->pending_planes = true;
  342. /* Wait for planes to be disabled */
  343. drm_crtc_wait_one_vblank(crtc);
  344. drm_crtc_vblank_off(crtc);
  345. mtk_crtc_ddp_hw_fini(mtk_crtc);
  346. mtk_smi_larb_put(ovl->larb_dev);
  347. mtk_crtc->enabled = false;
  348. }
  349. static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
  350. struct drm_crtc_state *old_crtc_state)
  351. {
  352. struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
  353. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  354. if (mtk_crtc->event && state->base.event)
  355. DRM_ERROR("new event while there is still a pending event\n");
  356. if (state->base.event) {
  357. state->base.event->pipe = drm_crtc_index(crtc);
  358. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  359. mtk_crtc->event = state->base.event;
  360. state->base.event = NULL;
  361. }
  362. }
  363. static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
  364. struct drm_crtc_state *old_crtc_state)
  365. {
  366. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  367. struct mtk_drm_private *priv = crtc->dev->dev_private;
  368. unsigned int pending_planes = 0;
  369. int i;
  370. if (mtk_crtc->event)
  371. mtk_crtc->pending_needs_vblank = true;
  372. for (i = 0; i < OVL_LAYER_NR; i++) {
  373. struct drm_plane *plane = &mtk_crtc->planes[i];
  374. struct mtk_plane_state *plane_state;
  375. plane_state = to_mtk_plane_state(plane->state);
  376. if (plane_state->pending.dirty) {
  377. plane_state->pending.config = true;
  378. plane_state->pending.dirty = false;
  379. pending_planes |= BIT(i);
  380. }
  381. }
  382. if (pending_planes)
  383. mtk_crtc->pending_planes = true;
  384. if (crtc->state->color_mgmt_changed)
  385. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
  386. mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
  387. if (priv->data->shadow_register) {
  388. mtk_disp_mutex_acquire(mtk_crtc->mutex);
  389. mtk_crtc_ddp_config(crtc);
  390. mtk_disp_mutex_release(mtk_crtc->mutex);
  391. }
  392. }
  393. static const struct drm_crtc_funcs mtk_crtc_funcs = {
  394. .set_config = drm_atomic_helper_set_config,
  395. .page_flip = drm_atomic_helper_page_flip,
  396. .destroy = mtk_drm_crtc_destroy,
  397. .reset = mtk_drm_crtc_reset,
  398. .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
  399. .atomic_destroy_state = mtk_drm_crtc_destroy_state,
  400. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  401. .enable_vblank = mtk_drm_crtc_enable_vblank,
  402. .disable_vblank = mtk_drm_crtc_disable_vblank,
  403. };
  404. static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
  405. .mode_fixup = mtk_drm_crtc_mode_fixup,
  406. .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
  407. .atomic_begin = mtk_drm_crtc_atomic_begin,
  408. .atomic_flush = mtk_drm_crtc_atomic_flush,
  409. .atomic_enable = mtk_drm_crtc_atomic_enable,
  410. .atomic_disable = mtk_drm_crtc_atomic_disable,
  411. };
  412. static int mtk_drm_crtc_init(struct drm_device *drm,
  413. struct mtk_drm_crtc *mtk_crtc,
  414. struct drm_plane *primary,
  415. struct drm_plane *cursor, unsigned int pipe)
  416. {
  417. int ret;
  418. ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
  419. &mtk_crtc_funcs, NULL);
  420. if (ret)
  421. goto err_cleanup_crtc;
  422. drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
  423. return 0;
  424. err_cleanup_crtc:
  425. drm_crtc_cleanup(&mtk_crtc->base);
  426. return ret;
  427. }
  428. void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *ovl)
  429. {
  430. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  431. struct mtk_drm_private *priv = crtc->dev->dev_private;
  432. if (!priv->data->shadow_register)
  433. mtk_crtc_ddp_config(crtc);
  434. mtk_drm_finish_page_flip(mtk_crtc);
  435. }
  436. int mtk_drm_crtc_create(struct drm_device *drm_dev,
  437. const enum mtk_ddp_comp_id *path, unsigned int path_len)
  438. {
  439. struct mtk_drm_private *priv = drm_dev->dev_private;
  440. struct device *dev = drm_dev->dev;
  441. struct mtk_drm_crtc *mtk_crtc;
  442. enum drm_plane_type type;
  443. unsigned int zpos;
  444. int pipe = priv->num_pipes;
  445. int ret;
  446. int i;
  447. for (i = 0; i < path_len; i++) {
  448. enum mtk_ddp_comp_id comp_id = path[i];
  449. struct device_node *node;
  450. node = priv->comp_node[comp_id];
  451. if (!node) {
  452. dev_info(dev,
  453. "Not creating crtc %d because component %d is disabled or missing\n",
  454. pipe, comp_id);
  455. return 0;
  456. }
  457. }
  458. mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
  459. if (!mtk_crtc)
  460. return -ENOMEM;
  461. mtk_crtc->config_regs = priv->config_regs;
  462. mtk_crtc->ddp_comp_nr = path_len;
  463. mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
  464. sizeof(*mtk_crtc->ddp_comp),
  465. GFP_KERNEL);
  466. if (!mtk_crtc->ddp_comp)
  467. return -ENOMEM;
  468. mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe);
  469. if (IS_ERR(mtk_crtc->mutex)) {
  470. ret = PTR_ERR(mtk_crtc->mutex);
  471. dev_err(dev, "Failed to get mutex: %d\n", ret);
  472. return ret;
  473. }
  474. for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
  475. enum mtk_ddp_comp_id comp_id = path[i];
  476. struct mtk_ddp_comp *comp;
  477. struct device_node *node;
  478. node = priv->comp_node[comp_id];
  479. comp = priv->ddp_comp[comp_id];
  480. if (!comp) {
  481. dev_err(dev, "Component %pOF not initialized\n", node);
  482. ret = -ENODEV;
  483. goto unprepare;
  484. }
  485. ret = clk_prepare(comp->clk);
  486. if (ret) {
  487. dev_err(dev,
  488. "Failed to prepare clock for component %pOF: %d\n",
  489. node, ret);
  490. goto unprepare;
  491. }
  492. mtk_crtc->ddp_comp[i] = comp;
  493. }
  494. for (zpos = 0; zpos < OVL_LAYER_NR; zpos++) {
  495. type = (zpos == 0) ? DRM_PLANE_TYPE_PRIMARY :
  496. (zpos == 1) ? DRM_PLANE_TYPE_CURSOR :
  497. DRM_PLANE_TYPE_OVERLAY;
  498. ret = mtk_plane_init(drm_dev, &mtk_crtc->planes[zpos],
  499. BIT(pipe), type);
  500. if (ret)
  501. goto unprepare;
  502. }
  503. ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, &mtk_crtc->planes[0],
  504. &mtk_crtc->planes[1], pipe);
  505. if (ret < 0)
  506. goto unprepare;
  507. drm_mode_crtc_set_gamma_size(&mtk_crtc->base, MTK_LUT_SIZE);
  508. drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, false, MTK_LUT_SIZE);
  509. priv->num_pipes++;
  510. return 0;
  511. unprepare:
  512. while (--i >= 0)
  513. clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
  514. return ret;
  515. }