rockchip_drm_vop.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author:Mark Yao <mark.yao@rock-chips.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drm.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_crtc.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_flip_work.h>
  20. #include <drm/drm_plane_helper.h>
  21. #ifdef CONFIG_DRM_ANALOGIX_DP
  22. #include <drm/bridge/analogix_dp.h>
  23. #endif
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/clk.h>
  28. #include <linux/iopoll.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/component.h>
  33. #include <linux/reset.h>
  34. #include <linux/delay.h>
  35. #include "rockchip_drm_drv.h"
  36. #include "rockchip_drm_gem.h"
  37. #include "rockchip_drm_fb.h"
  38. #include "rockchip_drm_psr.h"
  39. #include "rockchip_drm_vop.h"
  40. #define VOP_WIN_SET(x, win, name, v) \
  41. vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
  42. #define VOP_SCL_SET(x, win, name, v) \
  43. vop_reg_set(vop, &win->phy->scl->name, win->base, ~0, v, #name)
  44. #define VOP_SCL_SET_EXT(x, win, name, v) \
  45. vop_reg_set(vop, &win->phy->scl->ext->name, \
  46. win->base, ~0, v, #name)
  47. #define VOP_INTR_SET_MASK(vop, name, mask, v) \
  48. vop_reg_set(vop, &vop->data->intr->name, 0, mask, v, #name)
  49. #define VOP_REG_SET(vop, group, name, v) \
  50. vop_reg_set(vop, &vop->data->group->name, 0, ~0, v, #name)
  51. #define VOP_INTR_SET_TYPE(vop, name, type, v) \
  52. do { \
  53. int i, reg = 0, mask = 0; \
  54. for (i = 0; i < vop->data->intr->nintrs; i++) { \
  55. if (vop->data->intr->intrs[i] & type) { \
  56. reg |= (v) << i; \
  57. mask |= 1 << i; \
  58. } \
  59. } \
  60. VOP_INTR_SET_MASK(vop, name, mask, reg); \
  61. } while (0)
  62. #define VOP_INTR_GET_TYPE(vop, name, type) \
  63. vop_get_intr_type(vop, &vop->data->intr->name, type)
  64. #define VOP_WIN_GET(x, win, name) \
  65. vop_read_reg(x, win->offset, win->phy->name)
  66. #define VOP_WIN_GET_YRGBADDR(vop, win) \
  67. vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
  68. #define to_vop(x) container_of(x, struct vop, crtc)
  69. #define to_vop_win(x) container_of(x, struct vop_win, base)
  70. enum vop_pending {
  71. VOP_PENDING_FB_UNREF,
  72. };
  73. struct vop_win {
  74. struct drm_plane base;
  75. const struct vop_win_data *data;
  76. struct vop *vop;
  77. };
  78. struct vop {
  79. struct drm_crtc crtc;
  80. struct device *dev;
  81. struct drm_device *drm_dev;
  82. bool is_enabled;
  83. /* mutex vsync_ work */
  84. struct mutex vsync_mutex;
  85. bool vsync_work_pending;
  86. struct completion dsp_hold_completion;
  87. /* protected by dev->event_lock */
  88. struct drm_pending_vblank_event *event;
  89. struct drm_flip_work fb_unref_work;
  90. unsigned long pending;
  91. struct completion line_flag_completion;
  92. const struct vop_data *data;
  93. uint32_t *regsbak;
  94. void __iomem *regs;
  95. /* physical map length of vop register */
  96. uint32_t len;
  97. /* one time only one process allowed to config the register */
  98. spinlock_t reg_lock;
  99. /* lock vop irq reg */
  100. spinlock_t irq_lock;
  101. unsigned int irq;
  102. /* vop AHP clk */
  103. struct clk *hclk;
  104. /* vop dclk */
  105. struct clk *dclk;
  106. /* vop share memory frequency */
  107. struct clk *aclk;
  108. /* vop dclk reset */
  109. struct reset_control *dclk_rst;
  110. struct vop_win win[];
  111. };
  112. static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
  113. {
  114. writel(v, vop->regs + offset);
  115. vop->regsbak[offset >> 2] = v;
  116. }
  117. static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
  118. {
  119. return readl(vop->regs + offset);
  120. }
  121. static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
  122. const struct vop_reg *reg)
  123. {
  124. return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
  125. }
  126. static void vop_reg_set(struct vop *vop, const struct vop_reg *reg,
  127. uint32_t _offset, uint32_t _mask, uint32_t v,
  128. const char *reg_name)
  129. {
  130. int offset, mask, shift;
  131. if (!reg || !reg->mask) {
  132. dev_dbg(vop->dev, "Warning: not support %s\n", reg_name);
  133. return;
  134. }
  135. offset = reg->offset + _offset;
  136. mask = reg->mask & _mask;
  137. shift = reg->shift;
  138. if (reg->write_mask) {
  139. v = ((v << shift) & 0xffff) | (mask << (shift + 16));
  140. } else {
  141. uint32_t cached_val = vop->regsbak[offset >> 2];
  142. v = (cached_val & ~(mask << shift)) | ((v & mask) << shift);
  143. vop->regsbak[offset >> 2] = v;
  144. }
  145. if (reg->relaxed)
  146. writel_relaxed(v, vop->regs + offset);
  147. else
  148. writel(v, vop->regs + offset);
  149. }
  150. static inline uint32_t vop_get_intr_type(struct vop *vop,
  151. const struct vop_reg *reg, int type)
  152. {
  153. uint32_t i, ret = 0;
  154. uint32_t regs = vop_read_reg(vop, 0, reg);
  155. for (i = 0; i < vop->data->intr->nintrs; i++) {
  156. if ((type & vop->data->intr->intrs[i]) && (regs & 1 << i))
  157. ret |= vop->data->intr->intrs[i];
  158. }
  159. return ret;
  160. }
  161. static inline void vop_cfg_done(struct vop *vop)
  162. {
  163. VOP_REG_SET(vop, common, cfg_done, 1);
  164. }
  165. static bool has_rb_swapped(uint32_t format)
  166. {
  167. switch (format) {
  168. case DRM_FORMAT_XBGR8888:
  169. case DRM_FORMAT_ABGR8888:
  170. case DRM_FORMAT_BGR888:
  171. case DRM_FORMAT_BGR565:
  172. return true;
  173. default:
  174. return false;
  175. }
  176. }
  177. static enum vop_data_format vop_convert_format(uint32_t format)
  178. {
  179. switch (format) {
  180. case DRM_FORMAT_XRGB8888:
  181. case DRM_FORMAT_ARGB8888:
  182. case DRM_FORMAT_XBGR8888:
  183. case DRM_FORMAT_ABGR8888:
  184. return VOP_FMT_ARGB8888;
  185. case DRM_FORMAT_RGB888:
  186. case DRM_FORMAT_BGR888:
  187. return VOP_FMT_RGB888;
  188. case DRM_FORMAT_RGB565:
  189. case DRM_FORMAT_BGR565:
  190. return VOP_FMT_RGB565;
  191. case DRM_FORMAT_NV12:
  192. return VOP_FMT_YUV420SP;
  193. case DRM_FORMAT_NV16:
  194. return VOP_FMT_YUV422SP;
  195. case DRM_FORMAT_NV24:
  196. return VOP_FMT_YUV444SP;
  197. default:
  198. DRM_ERROR("unsupported format[%08x]\n", format);
  199. return -EINVAL;
  200. }
  201. }
  202. static bool is_yuv_support(uint32_t format)
  203. {
  204. switch (format) {
  205. case DRM_FORMAT_NV12:
  206. case DRM_FORMAT_NV16:
  207. case DRM_FORMAT_NV24:
  208. return true;
  209. default:
  210. return false;
  211. }
  212. }
  213. static bool is_alpha_support(uint32_t format)
  214. {
  215. switch (format) {
  216. case DRM_FORMAT_ARGB8888:
  217. case DRM_FORMAT_ABGR8888:
  218. return true;
  219. default:
  220. return false;
  221. }
  222. }
  223. static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src,
  224. uint32_t dst, bool is_horizontal,
  225. int vsu_mode, int *vskiplines)
  226. {
  227. uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT;
  228. if (is_horizontal) {
  229. if (mode == SCALE_UP)
  230. val = GET_SCL_FT_BIC(src, dst);
  231. else if (mode == SCALE_DOWN)
  232. val = GET_SCL_FT_BILI_DN(src, dst);
  233. } else {
  234. if (mode == SCALE_UP) {
  235. if (vsu_mode == SCALE_UP_BIL)
  236. val = GET_SCL_FT_BILI_UP(src, dst);
  237. else
  238. val = GET_SCL_FT_BIC(src, dst);
  239. } else if (mode == SCALE_DOWN) {
  240. if (vskiplines) {
  241. *vskiplines = scl_get_vskiplines(src, dst);
  242. val = scl_get_bili_dn_vskip(src, dst,
  243. *vskiplines);
  244. } else {
  245. val = GET_SCL_FT_BILI_DN(src, dst);
  246. }
  247. }
  248. }
  249. return val;
  250. }
  251. static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win,
  252. uint32_t src_w, uint32_t src_h, uint32_t dst_w,
  253. uint32_t dst_h, uint32_t pixel_format)
  254. {
  255. uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode;
  256. uint16_t cbcr_hor_scl_mode = SCALE_NONE;
  257. uint16_t cbcr_ver_scl_mode = SCALE_NONE;
  258. int hsub = drm_format_horz_chroma_subsampling(pixel_format);
  259. int vsub = drm_format_vert_chroma_subsampling(pixel_format);
  260. bool is_yuv = is_yuv_support(pixel_format);
  261. uint16_t cbcr_src_w = src_w / hsub;
  262. uint16_t cbcr_src_h = src_h / vsub;
  263. uint16_t vsu_mode;
  264. uint16_t lb_mode;
  265. uint32_t val;
  266. int vskiplines = 0;
  267. if (dst_w > 3840) {
  268. DRM_DEV_ERROR(vop->dev, "Maximum dst width (3840) exceeded\n");
  269. return;
  270. }
  271. if (!win->phy->scl->ext) {
  272. VOP_SCL_SET(vop, win, scale_yrgb_x,
  273. scl_cal_scale2(src_w, dst_w));
  274. VOP_SCL_SET(vop, win, scale_yrgb_y,
  275. scl_cal_scale2(src_h, dst_h));
  276. if (is_yuv) {
  277. VOP_SCL_SET(vop, win, scale_cbcr_x,
  278. scl_cal_scale2(cbcr_src_w, dst_w));
  279. VOP_SCL_SET(vop, win, scale_cbcr_y,
  280. scl_cal_scale2(cbcr_src_h, dst_h));
  281. }
  282. return;
  283. }
  284. yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
  285. yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
  286. if (is_yuv) {
  287. cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w);
  288. cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h);
  289. if (cbcr_hor_scl_mode == SCALE_DOWN)
  290. lb_mode = scl_vop_cal_lb_mode(dst_w, true);
  291. else
  292. lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true);
  293. } else {
  294. if (yrgb_hor_scl_mode == SCALE_DOWN)
  295. lb_mode = scl_vop_cal_lb_mode(dst_w, false);
  296. else
  297. lb_mode = scl_vop_cal_lb_mode(src_w, false);
  298. }
  299. VOP_SCL_SET_EXT(vop, win, lb_mode, lb_mode);
  300. if (lb_mode == LB_RGB_3840X2) {
  301. if (yrgb_ver_scl_mode != SCALE_NONE) {
  302. DRM_DEV_ERROR(vop->dev, "not allow yrgb ver scale\n");
  303. return;
  304. }
  305. if (cbcr_ver_scl_mode != SCALE_NONE) {
  306. DRM_DEV_ERROR(vop->dev, "not allow cbcr ver scale\n");
  307. return;
  308. }
  309. vsu_mode = SCALE_UP_BIL;
  310. } else if (lb_mode == LB_RGB_2560X4) {
  311. vsu_mode = SCALE_UP_BIL;
  312. } else {
  313. vsu_mode = SCALE_UP_BIC;
  314. }
  315. val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w,
  316. true, 0, NULL);
  317. VOP_SCL_SET(vop, win, scale_yrgb_x, val);
  318. val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h,
  319. false, vsu_mode, &vskiplines);
  320. VOP_SCL_SET(vop, win, scale_yrgb_y, val);
  321. VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt4, vskiplines == 4);
  322. VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt2, vskiplines == 2);
  323. VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode);
  324. VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode);
  325. VOP_SCL_SET_EXT(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL);
  326. VOP_SCL_SET_EXT(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL);
  327. VOP_SCL_SET_EXT(vop, win, yrgb_vsu_mode, vsu_mode);
  328. if (is_yuv) {
  329. val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w,
  330. dst_w, true, 0, NULL);
  331. VOP_SCL_SET(vop, win, scale_cbcr_x, val);
  332. val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h,
  333. dst_h, false, vsu_mode, &vskiplines);
  334. VOP_SCL_SET(vop, win, scale_cbcr_y, val);
  335. VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt4, vskiplines == 4);
  336. VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt2, vskiplines == 2);
  337. VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode);
  338. VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode);
  339. VOP_SCL_SET_EXT(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL);
  340. VOP_SCL_SET_EXT(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL);
  341. VOP_SCL_SET_EXT(vop, win, cbcr_vsu_mode, vsu_mode);
  342. }
  343. }
  344. static void vop_dsp_hold_valid_irq_enable(struct vop *vop)
  345. {
  346. unsigned long flags;
  347. if (WARN_ON(!vop->is_enabled))
  348. return;
  349. spin_lock_irqsave(&vop->irq_lock, flags);
  350. VOP_INTR_SET_TYPE(vop, clear, DSP_HOLD_VALID_INTR, 1);
  351. VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 1);
  352. spin_unlock_irqrestore(&vop->irq_lock, flags);
  353. }
  354. static void vop_dsp_hold_valid_irq_disable(struct vop *vop)
  355. {
  356. unsigned long flags;
  357. if (WARN_ON(!vop->is_enabled))
  358. return;
  359. spin_lock_irqsave(&vop->irq_lock, flags);
  360. VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 0);
  361. spin_unlock_irqrestore(&vop->irq_lock, flags);
  362. }
  363. /*
  364. * (1) each frame starts at the start of the Vsync pulse which is signaled by
  365. * the "FRAME_SYNC" interrupt.
  366. * (2) the active data region of each frame ends at dsp_vact_end
  367. * (3) we should program this same number (dsp_vact_end) into dsp_line_frag_num,
  368. * to get "LINE_FLAG" interrupt at the end of the active on screen data.
  369. *
  370. * VOP_INTR_CTRL0.dsp_line_frag_num = VOP_DSP_VACT_ST_END.dsp_vact_end
  371. * Interrupts
  372. * LINE_FLAG -------------------------------+
  373. * FRAME_SYNC ----+ |
  374. * | |
  375. * v v
  376. * | Vsync | Vbp | Vactive | Vfp |
  377. * ^ ^ ^ ^
  378. * | | | |
  379. * | | | |
  380. * dsp_vs_end ------------+ | | | VOP_DSP_VTOTAL_VS_END
  381. * dsp_vact_start --------------+ | | VOP_DSP_VACT_ST_END
  382. * dsp_vact_end ----------------------------+ | VOP_DSP_VACT_ST_END
  383. * dsp_total -------------------------------------+ VOP_DSP_VTOTAL_VS_END
  384. */
  385. static bool vop_line_flag_irq_is_enabled(struct vop *vop)
  386. {
  387. uint32_t line_flag_irq;
  388. unsigned long flags;
  389. spin_lock_irqsave(&vop->irq_lock, flags);
  390. line_flag_irq = VOP_INTR_GET_TYPE(vop, enable, LINE_FLAG_INTR);
  391. spin_unlock_irqrestore(&vop->irq_lock, flags);
  392. return !!line_flag_irq;
  393. }
  394. static void vop_line_flag_irq_enable(struct vop *vop)
  395. {
  396. unsigned long flags;
  397. if (WARN_ON(!vop->is_enabled))
  398. return;
  399. spin_lock_irqsave(&vop->irq_lock, flags);
  400. VOP_INTR_SET_TYPE(vop, clear, LINE_FLAG_INTR, 1);
  401. VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 1);
  402. spin_unlock_irqrestore(&vop->irq_lock, flags);
  403. }
  404. static void vop_line_flag_irq_disable(struct vop *vop)
  405. {
  406. unsigned long flags;
  407. if (WARN_ON(!vop->is_enabled))
  408. return;
  409. spin_lock_irqsave(&vop->irq_lock, flags);
  410. VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 0);
  411. spin_unlock_irqrestore(&vop->irq_lock, flags);
  412. }
  413. static int vop_enable(struct drm_crtc *crtc)
  414. {
  415. struct vop *vop = to_vop(crtc);
  416. int ret, i;
  417. ret = pm_runtime_get_sync(vop->dev);
  418. if (ret < 0) {
  419. dev_err(vop->dev, "failed to get pm runtime: %d\n", ret);
  420. return ret;
  421. }
  422. ret = clk_enable(vop->hclk);
  423. if (WARN_ON(ret < 0))
  424. goto err_put_pm_runtime;
  425. ret = clk_enable(vop->dclk);
  426. if (WARN_ON(ret < 0))
  427. goto err_disable_hclk;
  428. ret = clk_enable(vop->aclk);
  429. if (WARN_ON(ret < 0))
  430. goto err_disable_dclk;
  431. /*
  432. * Slave iommu shares power, irq and clock with vop. It was associated
  433. * automatically with this master device via common driver code.
  434. * Now that we have enabled the clock we attach it to the shared drm
  435. * mapping.
  436. */
  437. ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
  438. if (ret) {
  439. dev_err(vop->dev, "failed to attach dma mapping, %d\n", ret);
  440. goto err_disable_aclk;
  441. }
  442. memcpy(vop->regs, vop->regsbak, vop->len);
  443. /*
  444. * We need to make sure that all windows are disabled before we
  445. * enable the crtc. Otherwise we might try to scan from a destroyed
  446. * buffer later.
  447. */
  448. for (i = 0; i < vop->data->win_size; i++) {
  449. struct vop_win *vop_win = &vop->win[i];
  450. const struct vop_win_data *win = vop_win->data;
  451. spin_lock(&vop->reg_lock);
  452. VOP_WIN_SET(vop, win, enable, 0);
  453. spin_unlock(&vop->reg_lock);
  454. }
  455. vop_cfg_done(vop);
  456. /*
  457. * At here, vop clock & iommu is enable, R/W vop regs would be safe.
  458. */
  459. vop->is_enabled = true;
  460. spin_lock(&vop->reg_lock);
  461. VOP_REG_SET(vop, common, standby, 1);
  462. spin_unlock(&vop->reg_lock);
  463. enable_irq(vop->irq);
  464. drm_crtc_vblank_on(crtc);
  465. return 0;
  466. err_disable_aclk:
  467. clk_disable(vop->aclk);
  468. err_disable_dclk:
  469. clk_disable(vop->dclk);
  470. err_disable_hclk:
  471. clk_disable(vop->hclk);
  472. err_put_pm_runtime:
  473. pm_runtime_put_sync(vop->dev);
  474. return ret;
  475. }
  476. static void vop_crtc_atomic_disable(struct drm_crtc *crtc,
  477. struct drm_crtc_state *old_state)
  478. {
  479. struct vop *vop = to_vop(crtc);
  480. WARN_ON(vop->event);
  481. rockchip_drm_psr_deactivate(&vop->crtc);
  482. drm_crtc_vblank_off(crtc);
  483. /*
  484. * Vop standby will take effect at end of current frame,
  485. * if dsp hold valid irq happen, it means standby complete.
  486. *
  487. * we must wait standby complete when we want to disable aclk,
  488. * if not, memory bus maybe dead.
  489. */
  490. reinit_completion(&vop->dsp_hold_completion);
  491. vop_dsp_hold_valid_irq_enable(vop);
  492. spin_lock(&vop->reg_lock);
  493. VOP_REG_SET(vop, common, standby, 1);
  494. spin_unlock(&vop->reg_lock);
  495. wait_for_completion(&vop->dsp_hold_completion);
  496. vop_dsp_hold_valid_irq_disable(vop);
  497. disable_irq(vop->irq);
  498. vop->is_enabled = false;
  499. /*
  500. * vop standby complete, so iommu detach is safe.
  501. */
  502. rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
  503. clk_disable(vop->dclk);
  504. clk_disable(vop->aclk);
  505. clk_disable(vop->hclk);
  506. pm_runtime_put(vop->dev);
  507. if (crtc->state->event && !crtc->state->active) {
  508. spin_lock_irq(&crtc->dev->event_lock);
  509. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  510. spin_unlock_irq(&crtc->dev->event_lock);
  511. crtc->state->event = NULL;
  512. }
  513. }
  514. static void vop_plane_destroy(struct drm_plane *plane)
  515. {
  516. drm_plane_cleanup(plane);
  517. }
  518. static int vop_plane_atomic_check(struct drm_plane *plane,
  519. struct drm_plane_state *state)
  520. {
  521. struct drm_crtc *crtc = state->crtc;
  522. struct drm_crtc_state *crtc_state;
  523. struct drm_framebuffer *fb = state->fb;
  524. struct vop_win *vop_win = to_vop_win(plane);
  525. const struct vop_win_data *win = vop_win->data;
  526. int ret;
  527. struct drm_rect clip;
  528. int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
  529. DRM_PLANE_HELPER_NO_SCALING;
  530. int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
  531. DRM_PLANE_HELPER_NO_SCALING;
  532. if (!crtc || !fb)
  533. return 0;
  534. crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
  535. if (WARN_ON(!crtc_state))
  536. return -EINVAL;
  537. clip.x1 = 0;
  538. clip.y1 = 0;
  539. clip.x2 = crtc_state->adjusted_mode.hdisplay;
  540. clip.y2 = crtc_state->adjusted_mode.vdisplay;
  541. ret = drm_plane_helper_check_state(state, &clip,
  542. min_scale, max_scale,
  543. true, true);
  544. if (ret)
  545. return ret;
  546. if (!state->visible)
  547. return 0;
  548. ret = vop_convert_format(fb->format->format);
  549. if (ret < 0)
  550. return ret;
  551. /*
  552. * Src.x1 can be odd when do clip, but yuv plane start point
  553. * need align with 2 pixel.
  554. */
  555. if (is_yuv_support(fb->format->format) && ((state->src.x1 >> 16) % 2)) {
  556. DRM_ERROR("Invalid Source: Yuv format not support odd xpos\n");
  557. return -EINVAL;
  558. }
  559. return 0;
  560. }
  561. static void vop_plane_atomic_disable(struct drm_plane *plane,
  562. struct drm_plane_state *old_state)
  563. {
  564. struct vop_win *vop_win = to_vop_win(plane);
  565. const struct vop_win_data *win = vop_win->data;
  566. struct vop *vop = to_vop(old_state->crtc);
  567. if (!old_state->crtc)
  568. return;
  569. spin_lock(&vop->reg_lock);
  570. VOP_WIN_SET(vop, win, enable, 0);
  571. spin_unlock(&vop->reg_lock);
  572. }
  573. static void vop_plane_atomic_update(struct drm_plane *plane,
  574. struct drm_plane_state *old_state)
  575. {
  576. struct drm_plane_state *state = plane->state;
  577. struct drm_crtc *crtc = state->crtc;
  578. struct vop_win *vop_win = to_vop_win(plane);
  579. const struct vop_win_data *win = vop_win->data;
  580. struct vop *vop = to_vop(state->crtc);
  581. struct drm_framebuffer *fb = state->fb;
  582. unsigned int actual_w, actual_h;
  583. unsigned int dsp_stx, dsp_sty;
  584. uint32_t act_info, dsp_info, dsp_st;
  585. struct drm_rect *src = &state->src;
  586. struct drm_rect *dest = &state->dst;
  587. struct drm_gem_object *obj, *uv_obj;
  588. struct rockchip_gem_object *rk_obj, *rk_uv_obj;
  589. unsigned long offset;
  590. dma_addr_t dma_addr;
  591. uint32_t val;
  592. bool rb_swap;
  593. int format;
  594. /*
  595. * can't update plane when vop is disabled.
  596. */
  597. if (WARN_ON(!crtc))
  598. return;
  599. if (WARN_ON(!vop->is_enabled))
  600. return;
  601. if (!state->visible) {
  602. vop_plane_atomic_disable(plane, old_state);
  603. return;
  604. }
  605. obj = rockchip_fb_get_gem_obj(fb, 0);
  606. rk_obj = to_rockchip_obj(obj);
  607. actual_w = drm_rect_width(src) >> 16;
  608. actual_h = drm_rect_height(src) >> 16;
  609. act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
  610. dsp_info = (drm_rect_height(dest) - 1) << 16;
  611. dsp_info |= (drm_rect_width(dest) - 1) & 0xffff;
  612. dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start;
  613. dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start;
  614. dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff);
  615. offset = (src->x1 >> 16) * fb->format->cpp[0];
  616. offset += (src->y1 >> 16) * fb->pitches[0];
  617. dma_addr = rk_obj->dma_addr + offset + fb->offsets[0];
  618. format = vop_convert_format(fb->format->format);
  619. spin_lock(&vop->reg_lock);
  620. VOP_WIN_SET(vop, win, format, format);
  621. VOP_WIN_SET(vop, win, yrgb_vir, DIV_ROUND_UP(fb->pitches[0], 4));
  622. VOP_WIN_SET(vop, win, yrgb_mst, dma_addr);
  623. if (is_yuv_support(fb->format->format)) {
  624. int hsub = drm_format_horz_chroma_subsampling(fb->format->format);
  625. int vsub = drm_format_vert_chroma_subsampling(fb->format->format);
  626. int bpp = fb->format->cpp[1];
  627. uv_obj = rockchip_fb_get_gem_obj(fb, 1);
  628. rk_uv_obj = to_rockchip_obj(uv_obj);
  629. offset = (src->x1 >> 16) * bpp / hsub;
  630. offset += (src->y1 >> 16) * fb->pitches[1] / vsub;
  631. dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1];
  632. VOP_WIN_SET(vop, win, uv_vir, DIV_ROUND_UP(fb->pitches[1], 4));
  633. VOP_WIN_SET(vop, win, uv_mst, dma_addr);
  634. }
  635. if (win->phy->scl)
  636. scl_vop_cal_scl_fac(vop, win, actual_w, actual_h,
  637. drm_rect_width(dest), drm_rect_height(dest),
  638. fb->format->format);
  639. VOP_WIN_SET(vop, win, act_info, act_info);
  640. VOP_WIN_SET(vop, win, dsp_info, dsp_info);
  641. VOP_WIN_SET(vop, win, dsp_st, dsp_st);
  642. rb_swap = has_rb_swapped(fb->format->format);
  643. VOP_WIN_SET(vop, win, rb_swap, rb_swap);
  644. if (is_alpha_support(fb->format->format)) {
  645. VOP_WIN_SET(vop, win, dst_alpha_ctl,
  646. DST_FACTOR_M0(ALPHA_SRC_INVERSE));
  647. val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
  648. SRC_ALPHA_M0(ALPHA_STRAIGHT) |
  649. SRC_BLEND_M0(ALPHA_PER_PIX) |
  650. SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
  651. SRC_FACTOR_M0(ALPHA_ONE);
  652. VOP_WIN_SET(vop, win, src_alpha_ctl, val);
  653. } else {
  654. VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
  655. }
  656. VOP_WIN_SET(vop, win, enable, 1);
  657. spin_unlock(&vop->reg_lock);
  658. }
  659. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  660. .atomic_check = vop_plane_atomic_check,
  661. .atomic_update = vop_plane_atomic_update,
  662. .atomic_disable = vop_plane_atomic_disable,
  663. };
  664. static const struct drm_plane_funcs vop_plane_funcs = {
  665. .update_plane = drm_atomic_helper_update_plane,
  666. .disable_plane = drm_atomic_helper_disable_plane,
  667. .destroy = vop_plane_destroy,
  668. .reset = drm_atomic_helper_plane_reset,
  669. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  670. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  671. };
  672. static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
  673. {
  674. struct vop *vop = to_vop(crtc);
  675. unsigned long flags;
  676. if (WARN_ON(!vop->is_enabled))
  677. return -EPERM;
  678. spin_lock_irqsave(&vop->irq_lock, flags);
  679. VOP_INTR_SET_TYPE(vop, clear, FS_INTR, 1);
  680. VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 1);
  681. spin_unlock_irqrestore(&vop->irq_lock, flags);
  682. return 0;
  683. }
  684. static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
  685. {
  686. struct vop *vop = to_vop(crtc);
  687. unsigned long flags;
  688. if (WARN_ON(!vop->is_enabled))
  689. return;
  690. spin_lock_irqsave(&vop->irq_lock, flags);
  691. VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 0);
  692. spin_unlock_irqrestore(&vop->irq_lock, flags);
  693. }
  694. static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
  695. const struct drm_display_mode *mode,
  696. struct drm_display_mode *adjusted_mode)
  697. {
  698. struct vop *vop = to_vop(crtc);
  699. adjusted_mode->clock =
  700. clk_round_rate(vop->dclk, mode->clock * 1000) / 1000;
  701. return true;
  702. }
  703. static void vop_crtc_atomic_enable(struct drm_crtc *crtc,
  704. struct drm_crtc_state *old_state)
  705. {
  706. struct vop *vop = to_vop(crtc);
  707. const struct vop_data *vop_data = vop->data;
  708. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc->state);
  709. struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
  710. u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
  711. u16 hdisplay = adjusted_mode->hdisplay;
  712. u16 htotal = adjusted_mode->htotal;
  713. u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
  714. u16 hact_end = hact_st + hdisplay;
  715. u16 vdisplay = adjusted_mode->vdisplay;
  716. u16 vtotal = adjusted_mode->vtotal;
  717. u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
  718. u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
  719. u16 vact_end = vact_st + vdisplay;
  720. uint32_t pin_pol, val;
  721. int ret;
  722. WARN_ON(vop->event);
  723. ret = vop_enable(crtc);
  724. if (ret) {
  725. DRM_DEV_ERROR(vop->dev, "Failed to enable vop (%d)\n", ret);
  726. return;
  727. }
  728. pin_pol = BIT(DCLK_INVERT);
  729. pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC) ?
  730. BIT(HSYNC_POSITIVE) : 0;
  731. pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC) ?
  732. BIT(VSYNC_POSITIVE) : 0;
  733. VOP_REG_SET(vop, output, pin_pol, pin_pol);
  734. switch (s->output_type) {
  735. case DRM_MODE_CONNECTOR_LVDS:
  736. VOP_REG_SET(vop, output, rgb_en, 1);
  737. VOP_REG_SET(vop, output, rgb_pin_pol, pin_pol);
  738. break;
  739. case DRM_MODE_CONNECTOR_eDP:
  740. VOP_REG_SET(vop, output, edp_pin_pol, pin_pol);
  741. VOP_REG_SET(vop, output, edp_en, 1);
  742. break;
  743. case DRM_MODE_CONNECTOR_HDMIA:
  744. VOP_REG_SET(vop, output, hdmi_pin_pol, pin_pol);
  745. VOP_REG_SET(vop, output, hdmi_en, 1);
  746. break;
  747. case DRM_MODE_CONNECTOR_DSI:
  748. VOP_REG_SET(vop, output, mipi_pin_pol, pin_pol);
  749. VOP_REG_SET(vop, output, mipi_en, 1);
  750. break;
  751. case DRM_MODE_CONNECTOR_DisplayPort:
  752. pin_pol &= ~BIT(DCLK_INVERT);
  753. VOP_REG_SET(vop, output, dp_pin_pol, pin_pol);
  754. VOP_REG_SET(vop, output, dp_en, 1);
  755. break;
  756. default:
  757. DRM_DEV_ERROR(vop->dev, "unsupported connector_type [%d]\n",
  758. s->output_type);
  759. }
  760. /*
  761. * if vop is not support RGB10 output, need force RGB10 to RGB888.
  762. */
  763. if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA &&
  764. !(vop_data->feature & VOP_FEATURE_OUTPUT_RGB10))
  765. s->output_mode = ROCKCHIP_OUT_MODE_P888;
  766. VOP_REG_SET(vop, common, out_mode, s->output_mode);
  767. VOP_REG_SET(vop, modeset, htotal_pw, (htotal << 16) | hsync_len);
  768. val = hact_st << 16;
  769. val |= hact_end;
  770. VOP_REG_SET(vop, modeset, hact_st_end, val);
  771. VOP_REG_SET(vop, modeset, hpost_st_end, val);
  772. VOP_REG_SET(vop, modeset, vtotal_pw, (vtotal << 16) | vsync_len);
  773. val = vact_st << 16;
  774. val |= vact_end;
  775. VOP_REG_SET(vop, modeset, vact_st_end, val);
  776. VOP_REG_SET(vop, modeset, vpost_st_end, val);
  777. VOP_REG_SET(vop, intr, line_flag_num[0], vact_end);
  778. clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
  779. VOP_REG_SET(vop, common, standby, 0);
  780. rockchip_drm_psr_activate(&vop->crtc);
  781. }
  782. static bool vop_fs_irq_is_pending(struct vop *vop)
  783. {
  784. return VOP_INTR_GET_TYPE(vop, status, FS_INTR);
  785. }
  786. static void vop_wait_for_irq_handler(struct vop *vop)
  787. {
  788. bool pending;
  789. int ret;
  790. /*
  791. * Spin until frame start interrupt status bit goes low, which means
  792. * that interrupt handler was invoked and cleared it. The timeout of
  793. * 10 msecs is really too long, but it is just a safety measure if
  794. * something goes really wrong. The wait will only happen in the very
  795. * unlikely case of a vblank happening exactly at the same time and
  796. * shouldn't exceed microseconds range.
  797. */
  798. ret = readx_poll_timeout_atomic(vop_fs_irq_is_pending, vop, pending,
  799. !pending, 0, 10 * 1000);
  800. if (ret)
  801. DRM_DEV_ERROR(vop->dev, "VOP vblank IRQ stuck for 10 ms\n");
  802. synchronize_irq(vop->irq);
  803. }
  804. static void vop_crtc_atomic_flush(struct drm_crtc *crtc,
  805. struct drm_crtc_state *old_crtc_state)
  806. {
  807. struct drm_atomic_state *old_state = old_crtc_state->state;
  808. struct drm_plane_state *old_plane_state, *new_plane_state;
  809. struct vop *vop = to_vop(crtc);
  810. struct drm_plane *plane;
  811. int i;
  812. if (WARN_ON(!vop->is_enabled))
  813. return;
  814. spin_lock(&vop->reg_lock);
  815. vop_cfg_done(vop);
  816. spin_unlock(&vop->reg_lock);
  817. /*
  818. * There is a (rather unlikely) possiblity that a vblank interrupt
  819. * fired before we set the cfg_done bit. To avoid spuriously
  820. * signalling flip completion we need to wait for it to finish.
  821. */
  822. vop_wait_for_irq_handler(vop);
  823. spin_lock_irq(&crtc->dev->event_lock);
  824. if (crtc->state->event) {
  825. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  826. WARN_ON(vop->event);
  827. vop->event = crtc->state->event;
  828. crtc->state->event = NULL;
  829. }
  830. spin_unlock_irq(&crtc->dev->event_lock);
  831. for_each_oldnew_plane_in_state(old_state, plane, old_plane_state,
  832. new_plane_state, i) {
  833. if (!old_plane_state->fb)
  834. continue;
  835. if (old_plane_state->fb == new_plane_state->fb)
  836. continue;
  837. drm_framebuffer_get(old_plane_state->fb);
  838. drm_flip_work_queue(&vop->fb_unref_work, old_plane_state->fb);
  839. set_bit(VOP_PENDING_FB_UNREF, &vop->pending);
  840. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  841. }
  842. }
  843. static void vop_crtc_atomic_begin(struct drm_crtc *crtc,
  844. struct drm_crtc_state *old_crtc_state)
  845. {
  846. rockchip_drm_psr_flush(crtc);
  847. }
  848. static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
  849. .mode_fixup = vop_crtc_mode_fixup,
  850. .atomic_flush = vop_crtc_atomic_flush,
  851. .atomic_begin = vop_crtc_atomic_begin,
  852. .atomic_enable = vop_crtc_atomic_enable,
  853. .atomic_disable = vop_crtc_atomic_disable,
  854. };
  855. static void vop_crtc_destroy(struct drm_crtc *crtc)
  856. {
  857. drm_crtc_cleanup(crtc);
  858. }
  859. static void vop_crtc_reset(struct drm_crtc *crtc)
  860. {
  861. if (crtc->state)
  862. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  863. kfree(crtc->state);
  864. crtc->state = kzalloc(sizeof(struct rockchip_crtc_state), GFP_KERNEL);
  865. if (crtc->state)
  866. crtc->state->crtc = crtc;
  867. }
  868. static struct drm_crtc_state *vop_crtc_duplicate_state(struct drm_crtc *crtc)
  869. {
  870. struct rockchip_crtc_state *rockchip_state;
  871. rockchip_state = kzalloc(sizeof(*rockchip_state), GFP_KERNEL);
  872. if (!rockchip_state)
  873. return NULL;
  874. __drm_atomic_helper_crtc_duplicate_state(crtc, &rockchip_state->base);
  875. return &rockchip_state->base;
  876. }
  877. static void vop_crtc_destroy_state(struct drm_crtc *crtc,
  878. struct drm_crtc_state *state)
  879. {
  880. struct rockchip_crtc_state *s = to_rockchip_crtc_state(state);
  881. __drm_atomic_helper_crtc_destroy_state(&s->base);
  882. kfree(s);
  883. }
  884. #ifdef CONFIG_DRM_ANALOGIX_DP
  885. static struct drm_connector *vop_get_edp_connector(struct vop *vop)
  886. {
  887. struct drm_connector *connector;
  888. struct drm_connector_list_iter conn_iter;
  889. drm_connector_list_iter_begin(vop->drm_dev, &conn_iter);
  890. drm_for_each_connector_iter(connector, &conn_iter) {
  891. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
  892. drm_connector_list_iter_end(&conn_iter);
  893. return connector;
  894. }
  895. }
  896. drm_connector_list_iter_end(&conn_iter);
  897. return NULL;
  898. }
  899. static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
  900. const char *source_name, size_t *values_cnt)
  901. {
  902. struct vop *vop = to_vop(crtc);
  903. struct drm_connector *connector;
  904. int ret;
  905. connector = vop_get_edp_connector(vop);
  906. if (!connector)
  907. return -EINVAL;
  908. *values_cnt = 3;
  909. if (source_name && strcmp(source_name, "auto") == 0)
  910. ret = analogix_dp_start_crc(connector);
  911. else if (!source_name)
  912. ret = analogix_dp_stop_crc(connector);
  913. else
  914. ret = -EINVAL;
  915. return ret;
  916. }
  917. #else
  918. static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
  919. const char *source_name, size_t *values_cnt)
  920. {
  921. return -ENODEV;
  922. }
  923. #endif
  924. static const struct drm_crtc_funcs vop_crtc_funcs = {
  925. .set_config = drm_atomic_helper_set_config,
  926. .page_flip = drm_atomic_helper_page_flip,
  927. .destroy = vop_crtc_destroy,
  928. .reset = vop_crtc_reset,
  929. .atomic_duplicate_state = vop_crtc_duplicate_state,
  930. .atomic_destroy_state = vop_crtc_destroy_state,
  931. .enable_vblank = vop_crtc_enable_vblank,
  932. .disable_vblank = vop_crtc_disable_vblank,
  933. .set_crc_source = vop_crtc_set_crc_source,
  934. };
  935. static void vop_fb_unref_worker(struct drm_flip_work *work, void *val)
  936. {
  937. struct vop *vop = container_of(work, struct vop, fb_unref_work);
  938. struct drm_framebuffer *fb = val;
  939. drm_crtc_vblank_put(&vop->crtc);
  940. drm_framebuffer_put(fb);
  941. }
  942. static void vop_handle_vblank(struct vop *vop)
  943. {
  944. struct drm_device *drm = vop->drm_dev;
  945. struct drm_crtc *crtc = &vop->crtc;
  946. unsigned long flags;
  947. spin_lock_irqsave(&drm->event_lock, flags);
  948. if (vop->event) {
  949. drm_crtc_send_vblank_event(crtc, vop->event);
  950. drm_crtc_vblank_put(crtc);
  951. vop->event = NULL;
  952. }
  953. spin_unlock_irqrestore(&drm->event_lock, flags);
  954. if (test_and_clear_bit(VOP_PENDING_FB_UNREF, &vop->pending))
  955. drm_flip_work_commit(&vop->fb_unref_work, system_unbound_wq);
  956. }
  957. static irqreturn_t vop_isr(int irq, void *data)
  958. {
  959. struct vop *vop = data;
  960. struct drm_crtc *crtc = &vop->crtc;
  961. uint32_t active_irqs;
  962. unsigned long flags;
  963. int ret = IRQ_NONE;
  964. /*
  965. * interrupt register has interrupt status, enable and clear bits, we
  966. * must hold irq_lock to avoid a race with enable/disable_vblank().
  967. */
  968. spin_lock_irqsave(&vop->irq_lock, flags);
  969. active_irqs = VOP_INTR_GET_TYPE(vop, status, INTR_MASK);
  970. /* Clear all active interrupt sources */
  971. if (active_irqs)
  972. VOP_INTR_SET_TYPE(vop, clear, active_irqs, 1);
  973. spin_unlock_irqrestore(&vop->irq_lock, flags);
  974. /* This is expected for vop iommu irqs, since the irq is shared */
  975. if (!active_irqs)
  976. return IRQ_NONE;
  977. if (active_irqs & DSP_HOLD_VALID_INTR) {
  978. complete(&vop->dsp_hold_completion);
  979. active_irqs &= ~DSP_HOLD_VALID_INTR;
  980. ret = IRQ_HANDLED;
  981. }
  982. if (active_irqs & LINE_FLAG_INTR) {
  983. complete(&vop->line_flag_completion);
  984. active_irqs &= ~LINE_FLAG_INTR;
  985. ret = IRQ_HANDLED;
  986. }
  987. if (active_irqs & FS_INTR) {
  988. drm_crtc_handle_vblank(crtc);
  989. vop_handle_vblank(vop);
  990. active_irqs &= ~FS_INTR;
  991. ret = IRQ_HANDLED;
  992. }
  993. /* Unhandled irqs are spurious. */
  994. if (active_irqs)
  995. DRM_DEV_ERROR(vop->dev, "Unknown VOP IRQs: %#02x\n",
  996. active_irqs);
  997. return ret;
  998. }
  999. static int vop_create_crtc(struct vop *vop)
  1000. {
  1001. const struct vop_data *vop_data = vop->data;
  1002. struct device *dev = vop->dev;
  1003. struct drm_device *drm_dev = vop->drm_dev;
  1004. struct drm_plane *primary = NULL, *cursor = NULL, *plane, *tmp;
  1005. struct drm_crtc *crtc = &vop->crtc;
  1006. struct device_node *port;
  1007. int ret;
  1008. int i;
  1009. /*
  1010. * Create drm_plane for primary and cursor planes first, since we need
  1011. * to pass them to drm_crtc_init_with_planes, which sets the
  1012. * "possible_crtcs" to the newly initialized crtc.
  1013. */
  1014. for (i = 0; i < vop_data->win_size; i++) {
  1015. struct vop_win *vop_win = &vop->win[i];
  1016. const struct vop_win_data *win_data = vop_win->data;
  1017. if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
  1018. win_data->type != DRM_PLANE_TYPE_CURSOR)
  1019. continue;
  1020. ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
  1021. 0, &vop_plane_funcs,
  1022. win_data->phy->data_formats,
  1023. win_data->phy->nformats,
  1024. NULL, win_data->type, NULL);
  1025. if (ret) {
  1026. DRM_DEV_ERROR(vop->dev, "failed to init plane %d\n",
  1027. ret);
  1028. goto err_cleanup_planes;
  1029. }
  1030. plane = &vop_win->base;
  1031. drm_plane_helper_add(plane, &plane_helper_funcs);
  1032. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  1033. primary = plane;
  1034. else if (plane->type == DRM_PLANE_TYPE_CURSOR)
  1035. cursor = plane;
  1036. }
  1037. ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
  1038. &vop_crtc_funcs, NULL);
  1039. if (ret)
  1040. goto err_cleanup_planes;
  1041. drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
  1042. /*
  1043. * Create drm_planes for overlay windows with possible_crtcs restricted
  1044. * to the newly created crtc.
  1045. */
  1046. for (i = 0; i < vop_data->win_size; i++) {
  1047. struct vop_win *vop_win = &vop->win[i];
  1048. const struct vop_win_data *win_data = vop_win->data;
  1049. unsigned long possible_crtcs = 1 << drm_crtc_index(crtc);
  1050. if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
  1051. continue;
  1052. ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
  1053. possible_crtcs,
  1054. &vop_plane_funcs,
  1055. win_data->phy->data_formats,
  1056. win_data->phy->nformats,
  1057. NULL, win_data->type, NULL);
  1058. if (ret) {
  1059. DRM_DEV_ERROR(vop->dev, "failed to init overlay %d\n",
  1060. ret);
  1061. goto err_cleanup_crtc;
  1062. }
  1063. drm_plane_helper_add(&vop_win->base, &plane_helper_funcs);
  1064. }
  1065. port = of_get_child_by_name(dev->of_node, "port");
  1066. if (!port) {
  1067. DRM_DEV_ERROR(vop->dev, "no port node found in %pOF\n",
  1068. dev->of_node);
  1069. ret = -ENOENT;
  1070. goto err_cleanup_crtc;
  1071. }
  1072. drm_flip_work_init(&vop->fb_unref_work, "fb_unref",
  1073. vop_fb_unref_worker);
  1074. init_completion(&vop->dsp_hold_completion);
  1075. init_completion(&vop->line_flag_completion);
  1076. crtc->port = port;
  1077. return 0;
  1078. err_cleanup_crtc:
  1079. drm_crtc_cleanup(crtc);
  1080. err_cleanup_planes:
  1081. list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
  1082. head)
  1083. drm_plane_cleanup(plane);
  1084. return ret;
  1085. }
  1086. static void vop_destroy_crtc(struct vop *vop)
  1087. {
  1088. struct drm_crtc *crtc = &vop->crtc;
  1089. struct drm_device *drm_dev = vop->drm_dev;
  1090. struct drm_plane *plane, *tmp;
  1091. of_node_put(crtc->port);
  1092. /*
  1093. * We need to cleanup the planes now. Why?
  1094. *
  1095. * The planes are "&vop->win[i].base". That means the memory is
  1096. * all part of the big "struct vop" chunk of memory. That memory
  1097. * was devm allocated and associated with this component. We need to
  1098. * free it ourselves before vop_unbind() finishes.
  1099. */
  1100. list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
  1101. head)
  1102. vop_plane_destroy(plane);
  1103. /*
  1104. * Destroy CRTC after vop_plane_destroy() since vop_disable_plane()
  1105. * references the CRTC.
  1106. */
  1107. drm_crtc_cleanup(crtc);
  1108. drm_flip_work_cleanup(&vop->fb_unref_work);
  1109. }
  1110. static int vop_initial(struct vop *vop)
  1111. {
  1112. const struct vop_data *vop_data = vop->data;
  1113. struct reset_control *ahb_rst;
  1114. int i, ret;
  1115. vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
  1116. if (IS_ERR(vop->hclk)) {
  1117. dev_err(vop->dev, "failed to get hclk source\n");
  1118. return PTR_ERR(vop->hclk);
  1119. }
  1120. vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
  1121. if (IS_ERR(vop->aclk)) {
  1122. dev_err(vop->dev, "failed to get aclk source\n");
  1123. return PTR_ERR(vop->aclk);
  1124. }
  1125. vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
  1126. if (IS_ERR(vop->dclk)) {
  1127. dev_err(vop->dev, "failed to get dclk source\n");
  1128. return PTR_ERR(vop->dclk);
  1129. }
  1130. ret = pm_runtime_get_sync(vop->dev);
  1131. if (ret < 0) {
  1132. dev_err(vop->dev, "failed to get pm runtime: %d\n", ret);
  1133. return ret;
  1134. }
  1135. ret = clk_prepare(vop->dclk);
  1136. if (ret < 0) {
  1137. dev_err(vop->dev, "failed to prepare dclk\n");
  1138. goto err_put_pm_runtime;
  1139. }
  1140. /* Enable both the hclk and aclk to setup the vop */
  1141. ret = clk_prepare_enable(vop->hclk);
  1142. if (ret < 0) {
  1143. dev_err(vop->dev, "failed to prepare/enable hclk\n");
  1144. goto err_unprepare_dclk;
  1145. }
  1146. ret = clk_prepare_enable(vop->aclk);
  1147. if (ret < 0) {
  1148. dev_err(vop->dev, "failed to prepare/enable aclk\n");
  1149. goto err_disable_hclk;
  1150. }
  1151. /*
  1152. * do hclk_reset, reset all vop registers.
  1153. */
  1154. ahb_rst = devm_reset_control_get(vop->dev, "ahb");
  1155. if (IS_ERR(ahb_rst)) {
  1156. dev_err(vop->dev, "failed to get ahb reset\n");
  1157. ret = PTR_ERR(ahb_rst);
  1158. goto err_disable_aclk;
  1159. }
  1160. reset_control_assert(ahb_rst);
  1161. usleep_range(10, 20);
  1162. reset_control_deassert(ahb_rst);
  1163. memcpy(vop->regsbak, vop->regs, vop->len);
  1164. VOP_REG_SET(vop, misc, global_regdone_en, 1);
  1165. VOP_REG_SET(vop, common, dsp_blank, 0);
  1166. for (i = 0; i < vop_data->win_size; i++) {
  1167. const struct vop_win_data *win = &vop_data->win[i];
  1168. int channel = i * 2 + 1;
  1169. VOP_WIN_SET(vop, win, channel, (channel + 1) << 4 | channel);
  1170. VOP_WIN_SET(vop, win, enable, 0);
  1171. VOP_WIN_SET(vop, win, gate, 1);
  1172. }
  1173. vop_cfg_done(vop);
  1174. /*
  1175. * do dclk_reset, let all config take affect.
  1176. */
  1177. vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
  1178. if (IS_ERR(vop->dclk_rst)) {
  1179. dev_err(vop->dev, "failed to get dclk reset\n");
  1180. ret = PTR_ERR(vop->dclk_rst);
  1181. goto err_disable_aclk;
  1182. }
  1183. reset_control_assert(vop->dclk_rst);
  1184. usleep_range(10, 20);
  1185. reset_control_deassert(vop->dclk_rst);
  1186. clk_disable(vop->hclk);
  1187. clk_disable(vop->aclk);
  1188. vop->is_enabled = false;
  1189. pm_runtime_put_sync(vop->dev);
  1190. return 0;
  1191. err_disable_aclk:
  1192. clk_disable_unprepare(vop->aclk);
  1193. err_disable_hclk:
  1194. clk_disable_unprepare(vop->hclk);
  1195. err_unprepare_dclk:
  1196. clk_unprepare(vop->dclk);
  1197. err_put_pm_runtime:
  1198. pm_runtime_put_sync(vop->dev);
  1199. return ret;
  1200. }
  1201. /*
  1202. * Initialize the vop->win array elements.
  1203. */
  1204. static void vop_win_init(struct vop *vop)
  1205. {
  1206. const struct vop_data *vop_data = vop->data;
  1207. unsigned int i;
  1208. for (i = 0; i < vop_data->win_size; i++) {
  1209. struct vop_win *vop_win = &vop->win[i];
  1210. const struct vop_win_data *win_data = &vop_data->win[i];
  1211. vop_win->data = win_data;
  1212. vop_win->vop = vop;
  1213. }
  1214. }
  1215. /**
  1216. * rockchip_drm_wait_vact_end
  1217. * @crtc: CRTC to enable line flag
  1218. * @mstimeout: millisecond for timeout
  1219. *
  1220. * Wait for vact_end line flag irq or timeout.
  1221. *
  1222. * Returns:
  1223. * Zero on success, negative errno on failure.
  1224. */
  1225. int rockchip_drm_wait_vact_end(struct drm_crtc *crtc, unsigned int mstimeout)
  1226. {
  1227. struct vop *vop = to_vop(crtc);
  1228. unsigned long jiffies_left;
  1229. if (!crtc || !vop->is_enabled)
  1230. return -ENODEV;
  1231. if (mstimeout <= 0)
  1232. return -EINVAL;
  1233. if (vop_line_flag_irq_is_enabled(vop))
  1234. return -EBUSY;
  1235. reinit_completion(&vop->line_flag_completion);
  1236. vop_line_flag_irq_enable(vop);
  1237. jiffies_left = wait_for_completion_timeout(&vop->line_flag_completion,
  1238. msecs_to_jiffies(mstimeout));
  1239. vop_line_flag_irq_disable(vop);
  1240. if (jiffies_left == 0) {
  1241. dev_err(vop->dev, "Timeout waiting for IRQ\n");
  1242. return -ETIMEDOUT;
  1243. }
  1244. return 0;
  1245. }
  1246. EXPORT_SYMBOL(rockchip_drm_wait_vact_end);
  1247. static int vop_bind(struct device *dev, struct device *master, void *data)
  1248. {
  1249. struct platform_device *pdev = to_platform_device(dev);
  1250. const struct vop_data *vop_data;
  1251. struct drm_device *drm_dev = data;
  1252. struct vop *vop;
  1253. struct resource *res;
  1254. size_t alloc_size;
  1255. int ret, irq;
  1256. vop_data = of_device_get_match_data(dev);
  1257. if (!vop_data)
  1258. return -ENODEV;
  1259. /* Allocate vop struct and its vop_win array */
  1260. alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size;
  1261. vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
  1262. if (!vop)
  1263. return -ENOMEM;
  1264. vop->dev = dev;
  1265. vop->data = vop_data;
  1266. vop->drm_dev = drm_dev;
  1267. dev_set_drvdata(dev, vop);
  1268. vop_win_init(vop);
  1269. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1270. vop->len = resource_size(res);
  1271. vop->regs = devm_ioremap_resource(dev, res);
  1272. if (IS_ERR(vop->regs))
  1273. return PTR_ERR(vop->regs);
  1274. vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
  1275. if (!vop->regsbak)
  1276. return -ENOMEM;
  1277. irq = platform_get_irq(pdev, 0);
  1278. if (irq < 0) {
  1279. dev_err(dev, "cannot find irq for vop\n");
  1280. return irq;
  1281. }
  1282. vop->irq = (unsigned int)irq;
  1283. spin_lock_init(&vop->reg_lock);
  1284. spin_lock_init(&vop->irq_lock);
  1285. mutex_init(&vop->vsync_mutex);
  1286. ret = devm_request_irq(dev, vop->irq, vop_isr,
  1287. IRQF_SHARED, dev_name(dev), vop);
  1288. if (ret)
  1289. return ret;
  1290. /* IRQ is initially disabled; it gets enabled in power_on */
  1291. disable_irq(vop->irq);
  1292. ret = vop_create_crtc(vop);
  1293. if (ret)
  1294. goto err_enable_irq;
  1295. pm_runtime_enable(&pdev->dev);
  1296. ret = vop_initial(vop);
  1297. if (ret < 0) {
  1298. dev_err(&pdev->dev, "cannot initial vop dev - err %d\n", ret);
  1299. goto err_disable_pm_runtime;
  1300. }
  1301. return 0;
  1302. err_disable_pm_runtime:
  1303. pm_runtime_disable(&pdev->dev);
  1304. vop_destroy_crtc(vop);
  1305. err_enable_irq:
  1306. enable_irq(vop->irq); /* To balance out the disable_irq above */
  1307. return ret;
  1308. }
  1309. static void vop_unbind(struct device *dev, struct device *master, void *data)
  1310. {
  1311. struct vop *vop = dev_get_drvdata(dev);
  1312. pm_runtime_disable(dev);
  1313. vop_destroy_crtc(vop);
  1314. clk_unprepare(vop->aclk);
  1315. clk_unprepare(vop->hclk);
  1316. clk_unprepare(vop->dclk);
  1317. }
  1318. const struct component_ops vop_component_ops = {
  1319. .bind = vop_bind,
  1320. .unbind = vop_unbind,
  1321. };
  1322. EXPORT_SYMBOL_GPL(vop_component_ops);