mtk_drm_crtc.c 17 KB

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