rockchip_drm_vop.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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_crtc.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_plane_helper.h>
  19. #include <linux/kernel.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/clk.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/component.h>
  26. #include <linux/reset.h>
  27. #include <linux/delay.h>
  28. #include "rockchip_drm_drv.h"
  29. #include "rockchip_drm_gem.h"
  30. #include "rockchip_drm_fb.h"
  31. #include "rockchip_drm_vop.h"
  32. #define VOP_REG(off, _mask, s) \
  33. {.offset = off, \
  34. .mask = _mask, \
  35. .shift = s,}
  36. #define __REG_SET_RELAXED(x, off, mask, shift, v) \
  37. vop_mask_write_relaxed(x, off, (mask) << shift, (v) << shift)
  38. #define __REG_SET_NORMAL(x, off, mask, shift, v) \
  39. vop_mask_write(x, off, (mask) << shift, (v) << shift)
  40. #define REG_SET(x, base, reg, v, mode) \
  41. __REG_SET_##mode(x, base + reg.offset, reg.mask, reg.shift, v)
  42. #define VOP_WIN_SET(x, win, name, v) \
  43. REG_SET(x, win->base, win->phy->name, v, RELAXED)
  44. #define VOP_CTRL_SET(x, name, v) \
  45. REG_SET(x, 0, (x)->data->ctrl->name, v, NORMAL)
  46. #define VOP_WIN_GET(x, win, name) \
  47. vop_read_reg(x, win->base, &win->phy->name)
  48. #define VOP_WIN_GET_YRGBADDR(vop, win) \
  49. vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
  50. #define to_vop(x) container_of(x, struct vop, crtc)
  51. #define to_vop_win(x) container_of(x, struct vop_win, base)
  52. struct vop_win_state {
  53. struct list_head head;
  54. struct drm_framebuffer *fb;
  55. dma_addr_t yrgb_mst;
  56. struct drm_pending_vblank_event *event;
  57. };
  58. struct vop_win {
  59. struct drm_plane base;
  60. const struct vop_win_data *data;
  61. struct vop *vop;
  62. struct list_head pending;
  63. struct vop_win_state *active;
  64. };
  65. struct vop {
  66. struct drm_crtc crtc;
  67. struct device *dev;
  68. struct drm_device *drm_dev;
  69. unsigned int dpms;
  70. int connector_type;
  71. int connector_out_mode;
  72. /* mutex vsync_ work */
  73. struct mutex vsync_mutex;
  74. bool vsync_work_pending;
  75. const struct vop_data *data;
  76. uint32_t *regsbak;
  77. void __iomem *regs;
  78. /* physical map length of vop register */
  79. uint32_t len;
  80. /* one time only one process allowed to config the register */
  81. spinlock_t reg_lock;
  82. /* lock vop irq reg */
  83. spinlock_t irq_lock;
  84. unsigned int irq;
  85. /* vop AHP clk */
  86. struct clk *hclk;
  87. /* vop dclk */
  88. struct clk *dclk;
  89. /* vop share memory frequency */
  90. struct clk *aclk;
  91. /* vop dclk reset */
  92. struct reset_control *dclk_rst;
  93. int pipe;
  94. struct vop_win win[];
  95. };
  96. enum vop_data_format {
  97. VOP_FMT_ARGB8888 = 0,
  98. VOP_FMT_RGB888,
  99. VOP_FMT_RGB565,
  100. VOP_FMT_YUV420SP = 4,
  101. VOP_FMT_YUV422SP,
  102. VOP_FMT_YUV444SP,
  103. };
  104. struct vop_reg_data {
  105. uint32_t offset;
  106. uint32_t value;
  107. };
  108. struct vop_reg {
  109. uint32_t offset;
  110. uint32_t shift;
  111. uint32_t mask;
  112. };
  113. struct vop_ctrl {
  114. struct vop_reg standby;
  115. struct vop_reg data_blank;
  116. struct vop_reg gate_en;
  117. struct vop_reg mmu_en;
  118. struct vop_reg rgb_en;
  119. struct vop_reg edp_en;
  120. struct vop_reg hdmi_en;
  121. struct vop_reg mipi_en;
  122. struct vop_reg out_mode;
  123. struct vop_reg dither_down;
  124. struct vop_reg dither_up;
  125. struct vop_reg pin_pol;
  126. struct vop_reg htotal_pw;
  127. struct vop_reg hact_st_end;
  128. struct vop_reg vtotal_pw;
  129. struct vop_reg vact_st_end;
  130. struct vop_reg hpost_st_end;
  131. struct vop_reg vpost_st_end;
  132. };
  133. struct vop_win_phy {
  134. const uint32_t *data_formats;
  135. uint32_t nformats;
  136. struct vop_reg enable;
  137. struct vop_reg format;
  138. struct vop_reg act_info;
  139. struct vop_reg dsp_info;
  140. struct vop_reg dsp_st;
  141. struct vop_reg yrgb_mst;
  142. struct vop_reg uv_mst;
  143. struct vop_reg yrgb_vir;
  144. struct vop_reg uv_vir;
  145. struct vop_reg dst_alpha_ctl;
  146. struct vop_reg src_alpha_ctl;
  147. };
  148. struct vop_win_data {
  149. uint32_t base;
  150. const struct vop_win_phy *phy;
  151. enum drm_plane_type type;
  152. };
  153. struct vop_data {
  154. const struct vop_reg_data *init_table;
  155. unsigned int table_size;
  156. const struct vop_ctrl *ctrl;
  157. const struct vop_win_data *win;
  158. unsigned int win_size;
  159. };
  160. static const uint32_t formats_01[] = {
  161. DRM_FORMAT_XRGB8888,
  162. DRM_FORMAT_ARGB8888,
  163. DRM_FORMAT_RGB888,
  164. DRM_FORMAT_RGB565,
  165. DRM_FORMAT_NV12,
  166. DRM_FORMAT_NV16,
  167. DRM_FORMAT_NV24,
  168. };
  169. static const uint32_t formats_234[] = {
  170. DRM_FORMAT_XRGB8888,
  171. DRM_FORMAT_ARGB8888,
  172. DRM_FORMAT_RGB888,
  173. DRM_FORMAT_RGB565,
  174. };
  175. static const struct vop_win_phy win01_data = {
  176. .data_formats = formats_01,
  177. .nformats = ARRAY_SIZE(formats_01),
  178. .enable = VOP_REG(WIN0_CTRL0, 0x1, 0),
  179. .format = VOP_REG(WIN0_CTRL0, 0x7, 1),
  180. .act_info = VOP_REG(WIN0_ACT_INFO, 0x1fff1fff, 0),
  181. .dsp_info = VOP_REG(WIN0_DSP_INFO, 0x0fff0fff, 0),
  182. .dsp_st = VOP_REG(WIN0_DSP_ST, 0x1fff1fff, 0),
  183. .yrgb_mst = VOP_REG(WIN0_YRGB_MST, 0xffffffff, 0),
  184. .uv_mst = VOP_REG(WIN0_CBR_MST, 0xffffffff, 0),
  185. .yrgb_vir = VOP_REG(WIN0_VIR, 0x3fff, 0),
  186. .uv_vir = VOP_REG(WIN0_VIR, 0x3fff, 16),
  187. .src_alpha_ctl = VOP_REG(WIN0_SRC_ALPHA_CTRL, 0xff, 0),
  188. .dst_alpha_ctl = VOP_REG(WIN0_DST_ALPHA_CTRL, 0xff, 0),
  189. };
  190. static const struct vop_win_phy win23_data = {
  191. .data_formats = formats_234,
  192. .nformats = ARRAY_SIZE(formats_234),
  193. .enable = VOP_REG(WIN2_CTRL0, 0x1, 0),
  194. .format = VOP_REG(WIN2_CTRL0, 0x7, 1),
  195. .dsp_info = VOP_REG(WIN2_DSP_INFO0, 0x0fff0fff, 0),
  196. .dsp_st = VOP_REG(WIN2_DSP_ST0, 0x1fff1fff, 0),
  197. .yrgb_mst = VOP_REG(WIN2_MST0, 0xffffffff, 0),
  198. .yrgb_vir = VOP_REG(WIN2_VIR0_1, 0x1fff, 0),
  199. .src_alpha_ctl = VOP_REG(WIN2_SRC_ALPHA_CTRL, 0xff, 0),
  200. .dst_alpha_ctl = VOP_REG(WIN2_DST_ALPHA_CTRL, 0xff, 0),
  201. };
  202. static const struct vop_win_phy cursor_data = {
  203. .data_formats = formats_234,
  204. .nformats = ARRAY_SIZE(formats_234),
  205. .enable = VOP_REG(HWC_CTRL0, 0x1, 0),
  206. .format = VOP_REG(HWC_CTRL0, 0x7, 1),
  207. .dsp_st = VOP_REG(HWC_DSP_ST, 0x1fff1fff, 0),
  208. .yrgb_mst = VOP_REG(HWC_MST, 0xffffffff, 0),
  209. };
  210. static const struct vop_ctrl ctrl_data = {
  211. .standby = VOP_REG(SYS_CTRL, 0x1, 22),
  212. .gate_en = VOP_REG(SYS_CTRL, 0x1, 23),
  213. .mmu_en = VOP_REG(SYS_CTRL, 0x1, 20),
  214. .rgb_en = VOP_REG(SYS_CTRL, 0x1, 12),
  215. .hdmi_en = VOP_REG(SYS_CTRL, 0x1, 13),
  216. .edp_en = VOP_REG(SYS_CTRL, 0x1, 14),
  217. .mipi_en = VOP_REG(SYS_CTRL, 0x1, 15),
  218. .dither_down = VOP_REG(DSP_CTRL1, 0xf, 1),
  219. .dither_up = VOP_REG(DSP_CTRL1, 0x1, 6),
  220. .data_blank = VOP_REG(DSP_CTRL0, 0x1, 19),
  221. .out_mode = VOP_REG(DSP_CTRL0, 0xf, 0),
  222. .pin_pol = VOP_REG(DSP_CTRL0, 0xf, 4),
  223. .htotal_pw = VOP_REG(DSP_HTOTAL_HS_END, 0x1fff1fff, 0),
  224. .hact_st_end = VOP_REG(DSP_HACT_ST_END, 0x1fff1fff, 0),
  225. .vtotal_pw = VOP_REG(DSP_VTOTAL_VS_END, 0x1fff1fff, 0),
  226. .vact_st_end = VOP_REG(DSP_VACT_ST_END, 0x1fff1fff, 0),
  227. .hpost_st_end = VOP_REG(POST_DSP_HACT_INFO, 0x1fff1fff, 0),
  228. .vpost_st_end = VOP_REG(POST_DSP_VACT_INFO, 0x1fff1fff, 0),
  229. };
  230. static const struct vop_reg_data vop_init_reg_table[] = {
  231. {SYS_CTRL, 0x00c00000},
  232. {DSP_CTRL0, 0x00000000},
  233. {WIN0_CTRL0, 0x00000080},
  234. {WIN1_CTRL0, 0x00000080},
  235. };
  236. /*
  237. * Note: rk3288 has a dedicated 'cursor' window, however, that window requires
  238. * special support to get alpha blending working. For now, just use overlay
  239. * window 1 for the drm cursor.
  240. */
  241. static const struct vop_win_data rk3288_vop_win_data[] = {
  242. { .base = 0x00, .phy = &win01_data, .type = DRM_PLANE_TYPE_PRIMARY },
  243. { .base = 0x40, .phy = &win01_data, .type = DRM_PLANE_TYPE_CURSOR },
  244. { .base = 0x00, .phy = &win23_data, .type = DRM_PLANE_TYPE_OVERLAY },
  245. { .base = 0x50, .phy = &win23_data, .type = DRM_PLANE_TYPE_OVERLAY },
  246. { .base = 0x00, .phy = &cursor_data, .type = DRM_PLANE_TYPE_OVERLAY },
  247. };
  248. static const struct vop_data rk3288_vop = {
  249. .init_table = vop_init_reg_table,
  250. .table_size = ARRAY_SIZE(vop_init_reg_table),
  251. .ctrl = &ctrl_data,
  252. .win = rk3288_vop_win_data,
  253. .win_size = ARRAY_SIZE(rk3288_vop_win_data),
  254. };
  255. static const struct of_device_id vop_driver_dt_match[] = {
  256. { .compatible = "rockchip,rk3288-vop",
  257. .data = &rk3288_vop },
  258. {},
  259. };
  260. static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
  261. {
  262. writel(v, vop->regs + offset);
  263. vop->regsbak[offset >> 2] = v;
  264. }
  265. static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
  266. {
  267. return readl(vop->regs + offset);
  268. }
  269. static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
  270. const struct vop_reg *reg)
  271. {
  272. return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
  273. }
  274. static inline void vop_cfg_done(struct vop *vop)
  275. {
  276. writel(0x01, vop->regs + REG_CFG_DONE);
  277. }
  278. static inline void vop_mask_write(struct vop *vop, uint32_t offset,
  279. uint32_t mask, uint32_t v)
  280. {
  281. if (mask) {
  282. uint32_t cached_val = vop->regsbak[offset >> 2];
  283. cached_val = (cached_val & ~mask) | v;
  284. writel(cached_val, vop->regs + offset);
  285. vop->regsbak[offset >> 2] = cached_val;
  286. }
  287. }
  288. static inline void vop_mask_write_relaxed(struct vop *vop, uint32_t offset,
  289. uint32_t mask, uint32_t v)
  290. {
  291. if (mask) {
  292. uint32_t cached_val = vop->regsbak[offset >> 2];
  293. cached_val = (cached_val & ~mask) | v;
  294. writel_relaxed(cached_val, vop->regs + offset);
  295. vop->regsbak[offset >> 2] = cached_val;
  296. }
  297. }
  298. static enum vop_data_format vop_convert_format(uint32_t format)
  299. {
  300. switch (format) {
  301. case DRM_FORMAT_XRGB8888:
  302. case DRM_FORMAT_ARGB8888:
  303. return VOP_FMT_ARGB8888;
  304. case DRM_FORMAT_RGB888:
  305. return VOP_FMT_RGB888;
  306. case DRM_FORMAT_RGB565:
  307. return VOP_FMT_RGB565;
  308. case DRM_FORMAT_NV12:
  309. return VOP_FMT_YUV420SP;
  310. case DRM_FORMAT_NV16:
  311. return VOP_FMT_YUV422SP;
  312. case DRM_FORMAT_NV24:
  313. return VOP_FMT_YUV444SP;
  314. default:
  315. DRM_ERROR("unsupport format[%08x]\n", format);
  316. return -EINVAL;
  317. }
  318. }
  319. static bool is_alpha_support(uint32_t format)
  320. {
  321. switch (format) {
  322. case DRM_FORMAT_ARGB8888:
  323. return true;
  324. default:
  325. return false;
  326. }
  327. }
  328. static void vop_enable(struct drm_crtc *crtc)
  329. {
  330. struct vop *vop = to_vop(crtc);
  331. int ret;
  332. ret = clk_enable(vop->hclk);
  333. if (ret < 0) {
  334. dev_err(vop->dev, "failed to enable hclk - %d\n", ret);
  335. return;
  336. }
  337. ret = clk_enable(vop->dclk);
  338. if (ret < 0) {
  339. dev_err(vop->dev, "failed to enable dclk - %d\n", ret);
  340. goto err_disable_hclk;
  341. }
  342. ret = clk_enable(vop->aclk);
  343. if (ret < 0) {
  344. dev_err(vop->dev, "failed to enable aclk - %d\n", ret);
  345. goto err_disable_dclk;
  346. }
  347. /*
  348. * Slave iommu shares power, irq and clock with vop. It was associated
  349. * automatically with this master device via common driver code.
  350. * Now that we have enabled the clock we attach it to the shared drm
  351. * mapping.
  352. */
  353. ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
  354. if (ret) {
  355. dev_err(vop->dev, "failed to attach dma mapping, %d\n", ret);
  356. goto err_disable_aclk;
  357. }
  358. spin_lock(&vop->reg_lock);
  359. VOP_CTRL_SET(vop, standby, 0);
  360. spin_unlock(&vop->reg_lock);
  361. enable_irq(vop->irq);
  362. drm_vblank_on(vop->drm_dev, vop->pipe);
  363. return;
  364. err_disable_aclk:
  365. clk_disable(vop->aclk);
  366. err_disable_dclk:
  367. clk_disable(vop->dclk);
  368. err_disable_hclk:
  369. clk_disable(vop->hclk);
  370. }
  371. static void vop_disable(struct drm_crtc *crtc)
  372. {
  373. struct vop *vop = to_vop(crtc);
  374. drm_vblank_off(crtc->dev, vop->pipe);
  375. disable_irq(vop->irq);
  376. /*
  377. * TODO: Since standby doesn't take effect until the next vblank,
  378. * when we turn off dclk below, the vop is probably still active.
  379. */
  380. spin_lock(&vop->reg_lock);
  381. VOP_CTRL_SET(vop, standby, 1);
  382. spin_unlock(&vop->reg_lock);
  383. /*
  384. * disable dclk to stop frame scan, so we can safely detach iommu,
  385. */
  386. clk_disable(vop->dclk);
  387. rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
  388. clk_disable(vop->aclk);
  389. clk_disable(vop->hclk);
  390. }
  391. /*
  392. * Caller must hold vsync_mutex.
  393. */
  394. static struct drm_framebuffer *vop_win_last_pending_fb(struct vop_win *vop_win)
  395. {
  396. struct vop_win_state *last;
  397. struct vop_win_state *active = vop_win->active;
  398. if (list_empty(&vop_win->pending))
  399. return active ? active->fb : NULL;
  400. last = list_last_entry(&vop_win->pending, struct vop_win_state, head);
  401. return last ? last->fb : NULL;
  402. }
  403. /*
  404. * Caller must hold vsync_mutex.
  405. */
  406. static int vop_win_queue_fb(struct vop_win *vop_win,
  407. struct drm_framebuffer *fb, dma_addr_t yrgb_mst,
  408. struct drm_pending_vblank_event *event)
  409. {
  410. struct vop_win_state *state;
  411. state = kzalloc(sizeof(*state), GFP_KERNEL);
  412. if (!state)
  413. return -ENOMEM;
  414. state->fb = fb;
  415. state->yrgb_mst = yrgb_mst;
  416. state->event = event;
  417. list_add_tail(&state->head, &vop_win->pending);
  418. return 0;
  419. }
  420. static int vop_update_plane_event(struct drm_plane *plane,
  421. struct drm_crtc *crtc,
  422. struct drm_framebuffer *fb, int crtc_x,
  423. int crtc_y, unsigned int crtc_w,
  424. unsigned int crtc_h, uint32_t src_x,
  425. uint32_t src_y, uint32_t src_w,
  426. uint32_t src_h,
  427. struct drm_pending_vblank_event *event)
  428. {
  429. struct vop_win *vop_win = to_vop_win(plane);
  430. const struct vop_win_data *win = vop_win->data;
  431. struct vop *vop = to_vop(crtc);
  432. struct drm_gem_object *obj;
  433. struct rockchip_gem_object *rk_obj;
  434. unsigned long offset;
  435. unsigned int actual_w;
  436. unsigned int actual_h;
  437. unsigned int dsp_stx;
  438. unsigned int dsp_sty;
  439. unsigned int y_vir_stride;
  440. dma_addr_t yrgb_mst;
  441. enum vop_data_format format;
  442. uint32_t val;
  443. bool is_alpha;
  444. bool visible;
  445. int ret;
  446. struct drm_rect dest = {
  447. .x1 = crtc_x,
  448. .y1 = crtc_y,
  449. .x2 = crtc_x + crtc_w,
  450. .y2 = crtc_y + crtc_h,
  451. };
  452. struct drm_rect src = {
  453. /* 16.16 fixed point */
  454. .x1 = src_x,
  455. .y1 = src_y,
  456. .x2 = src_x + src_w,
  457. .y2 = src_y + src_h,
  458. };
  459. const struct drm_rect clip = {
  460. .x2 = crtc->mode.hdisplay,
  461. .y2 = crtc->mode.vdisplay,
  462. };
  463. bool can_position = plane->type != DRM_PLANE_TYPE_PRIMARY;
  464. ret = drm_plane_helper_check_update(plane, crtc, fb,
  465. &src, &dest, &clip,
  466. DRM_PLANE_HELPER_NO_SCALING,
  467. DRM_PLANE_HELPER_NO_SCALING,
  468. can_position, false, &visible);
  469. if (ret)
  470. return ret;
  471. if (!visible)
  472. return 0;
  473. is_alpha = is_alpha_support(fb->pixel_format);
  474. format = vop_convert_format(fb->pixel_format);
  475. if (format < 0)
  476. return format;
  477. obj = rockchip_fb_get_gem_obj(fb, 0);
  478. if (!obj) {
  479. DRM_ERROR("fail to get rockchip gem object from framebuffer\n");
  480. return -EINVAL;
  481. }
  482. rk_obj = to_rockchip_obj(obj);
  483. actual_w = (src.x2 - src.x1) >> 16;
  484. actual_h = (src.y2 - src.y1) >> 16;
  485. crtc_x = max(0, crtc_x);
  486. crtc_y = max(0, crtc_y);
  487. dsp_stx = crtc_x + crtc->mode.htotal - crtc->mode.hsync_start;
  488. dsp_sty = crtc_y + crtc->mode.vtotal - crtc->mode.vsync_start;
  489. offset = (src.x1 >> 16) * (fb->bits_per_pixel >> 3);
  490. offset += (src.y1 >> 16) * fb->pitches[0];
  491. yrgb_mst = rk_obj->dma_addr + offset;
  492. y_vir_stride = fb->pitches[0] / (fb->bits_per_pixel >> 3);
  493. /*
  494. * If this plane update changes the plane's framebuffer, (or more
  495. * precisely, if this update has a different framebuffer than the last
  496. * update), enqueue it so we can track when it completes.
  497. *
  498. * Only when we discover that this update has completed, can we
  499. * unreference any previous framebuffers.
  500. */
  501. mutex_lock(&vop->vsync_mutex);
  502. if (fb != vop_win_last_pending_fb(vop_win)) {
  503. ret = drm_vblank_get(plane->dev, vop->pipe);
  504. if (ret) {
  505. DRM_ERROR("failed to get vblank, %d\n", ret);
  506. mutex_unlock(&vop->vsync_mutex);
  507. return ret;
  508. }
  509. drm_framebuffer_reference(fb);
  510. ret = vop_win_queue_fb(vop_win, fb, yrgb_mst, event);
  511. if (ret) {
  512. drm_vblank_put(plane->dev, vop->pipe);
  513. mutex_unlock(&vop->vsync_mutex);
  514. return ret;
  515. }
  516. vop->vsync_work_pending = true;
  517. }
  518. mutex_unlock(&vop->vsync_mutex);
  519. spin_lock(&vop->reg_lock);
  520. VOP_WIN_SET(vop, win, format, format);
  521. VOP_WIN_SET(vop, win, yrgb_vir, y_vir_stride);
  522. VOP_WIN_SET(vop, win, yrgb_mst, yrgb_mst);
  523. val = (actual_h - 1) << 16;
  524. val |= (actual_w - 1) & 0xffff;
  525. VOP_WIN_SET(vop, win, act_info, val);
  526. VOP_WIN_SET(vop, win, dsp_info, val);
  527. val = (dsp_sty - 1) << 16;
  528. val |= (dsp_stx - 1) & 0xffff;
  529. VOP_WIN_SET(vop, win, dsp_st, val);
  530. if (is_alpha) {
  531. VOP_WIN_SET(vop, win, dst_alpha_ctl,
  532. DST_FACTOR_M0(ALPHA_SRC_INVERSE));
  533. val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
  534. SRC_ALPHA_M0(ALPHA_STRAIGHT) |
  535. SRC_BLEND_M0(ALPHA_PER_PIX) |
  536. SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
  537. SRC_FACTOR_M0(ALPHA_ONE);
  538. VOP_WIN_SET(vop, win, src_alpha_ctl, val);
  539. } else {
  540. VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
  541. }
  542. VOP_WIN_SET(vop, win, enable, 1);
  543. vop_cfg_done(vop);
  544. spin_unlock(&vop->reg_lock);
  545. return 0;
  546. }
  547. static int vop_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  548. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  549. unsigned int crtc_w, unsigned int crtc_h,
  550. uint32_t src_x, uint32_t src_y, uint32_t src_w,
  551. uint32_t src_h)
  552. {
  553. return vop_update_plane_event(plane, crtc, fb, crtc_x, crtc_y, crtc_w,
  554. crtc_h, src_x, src_y, src_w, src_h,
  555. NULL);
  556. }
  557. static int vop_update_primary_plane(struct drm_crtc *crtc,
  558. struct drm_pending_vblank_event *event)
  559. {
  560. unsigned int crtc_w, crtc_h;
  561. crtc_w = crtc->primary->fb->width - crtc->x;
  562. crtc_h = crtc->primary->fb->height - crtc->y;
  563. return vop_update_plane_event(crtc->primary, crtc, crtc->primary->fb,
  564. 0, 0, crtc_w, crtc_h, crtc->x << 16,
  565. crtc->y << 16, crtc_w << 16,
  566. crtc_h << 16, event);
  567. }
  568. static int vop_disable_plane(struct drm_plane *plane)
  569. {
  570. struct vop_win *vop_win = to_vop_win(plane);
  571. const struct vop_win_data *win = vop_win->data;
  572. struct vop *vop;
  573. int ret;
  574. if (!plane->crtc)
  575. return 0;
  576. vop = to_vop(plane->crtc);
  577. ret = drm_vblank_get(plane->dev, vop->pipe);
  578. if (ret) {
  579. DRM_ERROR("failed to get vblank, %d\n", ret);
  580. return ret;
  581. }
  582. mutex_lock(&vop->vsync_mutex);
  583. ret = vop_win_queue_fb(vop_win, NULL, 0, NULL);
  584. if (ret) {
  585. drm_vblank_put(plane->dev, vop->pipe);
  586. mutex_unlock(&vop->vsync_mutex);
  587. return ret;
  588. }
  589. vop->vsync_work_pending = true;
  590. mutex_unlock(&vop->vsync_mutex);
  591. spin_lock(&vop->reg_lock);
  592. VOP_WIN_SET(vop, win, enable, 0);
  593. vop_cfg_done(vop);
  594. spin_unlock(&vop->reg_lock);
  595. return 0;
  596. }
  597. static void vop_plane_destroy(struct drm_plane *plane)
  598. {
  599. vop_disable_plane(plane);
  600. drm_plane_cleanup(plane);
  601. }
  602. static const struct drm_plane_funcs vop_plane_funcs = {
  603. .update_plane = vop_update_plane,
  604. .disable_plane = vop_disable_plane,
  605. .destroy = vop_plane_destroy,
  606. };
  607. int rockchip_drm_crtc_mode_config(struct drm_crtc *crtc,
  608. int connector_type,
  609. int out_mode)
  610. {
  611. struct vop *vop = to_vop(crtc);
  612. vop->connector_type = connector_type;
  613. vop->connector_out_mode = out_mode;
  614. return 0;
  615. }
  616. static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
  617. {
  618. struct vop *vop = to_vop(crtc);
  619. unsigned long flags;
  620. if (vop->dpms != DRM_MODE_DPMS_ON)
  621. return -EPERM;
  622. spin_lock_irqsave(&vop->irq_lock, flags);
  623. vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(1));
  624. spin_unlock_irqrestore(&vop->irq_lock, flags);
  625. return 0;
  626. }
  627. static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
  628. {
  629. struct vop *vop = to_vop(crtc);
  630. unsigned long flags;
  631. if (vop->dpms != DRM_MODE_DPMS_ON)
  632. return;
  633. spin_lock_irqsave(&vop->irq_lock, flags);
  634. vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(0));
  635. spin_unlock_irqrestore(&vop->irq_lock, flags);
  636. }
  637. static const struct rockchip_crtc_funcs private_crtc_funcs = {
  638. .enable_vblank = vop_crtc_enable_vblank,
  639. .disable_vblank = vop_crtc_disable_vblank,
  640. };
  641. static void vop_crtc_dpms(struct drm_crtc *crtc, int mode)
  642. {
  643. struct vop *vop = to_vop(crtc);
  644. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  645. if (vop->dpms == mode) {
  646. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  647. return;
  648. }
  649. switch (mode) {
  650. case DRM_MODE_DPMS_ON:
  651. vop_enable(crtc);
  652. break;
  653. case DRM_MODE_DPMS_STANDBY:
  654. case DRM_MODE_DPMS_SUSPEND:
  655. case DRM_MODE_DPMS_OFF:
  656. vop_disable(crtc);
  657. break;
  658. default:
  659. DRM_DEBUG_KMS("unspecified mode %d\n", mode);
  660. break;
  661. }
  662. vop->dpms = mode;
  663. }
  664. static void vop_crtc_prepare(struct drm_crtc *crtc)
  665. {
  666. vop_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  667. }
  668. static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
  669. const struct drm_display_mode *mode,
  670. struct drm_display_mode *adjusted_mode)
  671. {
  672. if (adjusted_mode->htotal == 0 || adjusted_mode->vtotal == 0)
  673. return false;
  674. return true;
  675. }
  676. static int vop_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  677. struct drm_framebuffer *old_fb)
  678. {
  679. int ret;
  680. crtc->x = x;
  681. crtc->y = y;
  682. ret = vop_update_primary_plane(crtc, NULL);
  683. if (ret < 0) {
  684. DRM_ERROR("fail to update plane\n");
  685. return ret;
  686. }
  687. return 0;
  688. }
  689. static int vop_crtc_mode_set(struct drm_crtc *crtc,
  690. struct drm_display_mode *mode,
  691. struct drm_display_mode *adjusted_mode,
  692. int x, int y, struct drm_framebuffer *fb)
  693. {
  694. struct vop *vop = to_vop(crtc);
  695. u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
  696. u16 hdisplay = adjusted_mode->hdisplay;
  697. u16 htotal = adjusted_mode->htotal;
  698. u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
  699. u16 hact_end = hact_st + hdisplay;
  700. u16 vdisplay = adjusted_mode->vdisplay;
  701. u16 vtotal = adjusted_mode->vtotal;
  702. u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
  703. u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
  704. u16 vact_end = vact_st + vdisplay;
  705. int ret;
  706. uint32_t val;
  707. /*
  708. * disable dclk to stop frame scan, so that we can safe config mode and
  709. * enable iommu.
  710. */
  711. clk_disable(vop->dclk);
  712. switch (vop->connector_type) {
  713. case DRM_MODE_CONNECTOR_LVDS:
  714. VOP_CTRL_SET(vop, rgb_en, 1);
  715. break;
  716. case DRM_MODE_CONNECTOR_eDP:
  717. VOP_CTRL_SET(vop, edp_en, 1);
  718. break;
  719. case DRM_MODE_CONNECTOR_HDMIA:
  720. VOP_CTRL_SET(vop, hdmi_en, 1);
  721. break;
  722. default:
  723. DRM_ERROR("unsupport connector_type[%d]\n",
  724. vop->connector_type);
  725. return -EINVAL;
  726. };
  727. VOP_CTRL_SET(vop, out_mode, vop->connector_out_mode);
  728. val = 0x8;
  729. val |= (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) ? 1 : 0;
  730. val |= (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC) ? (1 << 1) : 0;
  731. VOP_CTRL_SET(vop, pin_pol, val);
  732. VOP_CTRL_SET(vop, htotal_pw, (htotal << 16) | hsync_len);
  733. val = hact_st << 16;
  734. val |= hact_end;
  735. VOP_CTRL_SET(vop, hact_st_end, val);
  736. VOP_CTRL_SET(vop, hpost_st_end, val);
  737. VOP_CTRL_SET(vop, vtotal_pw, (vtotal << 16) | vsync_len);
  738. val = vact_st << 16;
  739. val |= vact_end;
  740. VOP_CTRL_SET(vop, vact_st_end, val);
  741. VOP_CTRL_SET(vop, vpost_st_end, val);
  742. ret = vop_crtc_mode_set_base(crtc, x, y, fb);
  743. if (ret)
  744. return ret;
  745. /*
  746. * reset dclk, take all mode config affect, so the clk would run in
  747. * correct frame.
  748. */
  749. reset_control_assert(vop->dclk_rst);
  750. usleep_range(10, 20);
  751. reset_control_deassert(vop->dclk_rst);
  752. clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
  753. ret = clk_enable(vop->dclk);
  754. if (ret < 0) {
  755. dev_err(vop->dev, "failed to enable dclk - %d\n", ret);
  756. return ret;
  757. }
  758. return 0;
  759. }
  760. static void vop_crtc_commit(struct drm_crtc *crtc)
  761. {
  762. }
  763. static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
  764. .dpms = vop_crtc_dpms,
  765. .prepare = vop_crtc_prepare,
  766. .mode_fixup = vop_crtc_mode_fixup,
  767. .mode_set = vop_crtc_mode_set,
  768. .mode_set_base = vop_crtc_mode_set_base,
  769. .commit = vop_crtc_commit,
  770. };
  771. static int vop_crtc_page_flip(struct drm_crtc *crtc,
  772. struct drm_framebuffer *fb,
  773. struct drm_pending_vblank_event *event,
  774. uint32_t page_flip_flags)
  775. {
  776. struct vop *vop = to_vop(crtc);
  777. struct drm_framebuffer *old_fb = crtc->primary->fb;
  778. int ret;
  779. /* when the page flip is requested, crtc's dpms should be on */
  780. if (vop->dpms > DRM_MODE_DPMS_ON) {
  781. DRM_DEBUG("failed page flip request at dpms[%d].\n", vop->dpms);
  782. return 0;
  783. }
  784. crtc->primary->fb = fb;
  785. ret = vop_update_primary_plane(crtc, event);
  786. if (ret)
  787. crtc->primary->fb = old_fb;
  788. return ret;
  789. }
  790. static void vop_win_state_complete(struct vop_win *vop_win,
  791. struct vop_win_state *state)
  792. {
  793. struct vop *vop = vop_win->vop;
  794. struct drm_crtc *crtc = &vop->crtc;
  795. struct drm_device *drm = crtc->dev;
  796. unsigned long flags;
  797. if (state->event) {
  798. spin_lock_irqsave(&drm->event_lock, flags);
  799. drm_send_vblank_event(drm, -1, state->event);
  800. spin_unlock_irqrestore(&drm->event_lock, flags);
  801. }
  802. list_del(&state->head);
  803. drm_vblank_put(crtc->dev, vop->pipe);
  804. }
  805. static void vop_crtc_destroy(struct drm_crtc *crtc)
  806. {
  807. drm_crtc_cleanup(crtc);
  808. }
  809. static const struct drm_crtc_funcs vop_crtc_funcs = {
  810. .set_config = drm_crtc_helper_set_config,
  811. .page_flip = vop_crtc_page_flip,
  812. .destroy = vop_crtc_destroy,
  813. };
  814. static bool vop_win_state_is_active(struct vop_win *vop_win,
  815. struct vop_win_state *state)
  816. {
  817. bool active = false;
  818. if (state->fb) {
  819. dma_addr_t yrgb_mst;
  820. /* check yrgb_mst to tell if pending_fb is now front */
  821. yrgb_mst = VOP_WIN_GET_YRGBADDR(vop_win->vop, vop_win->data);
  822. active = (yrgb_mst == state->yrgb_mst);
  823. } else {
  824. bool enabled;
  825. /* if enable bit is clear, plane is now disabled */
  826. enabled = VOP_WIN_GET(vop_win->vop, vop_win->data, enable);
  827. active = (enabled == 0);
  828. }
  829. return active;
  830. }
  831. static void vop_win_state_destroy(struct vop_win_state *state)
  832. {
  833. struct drm_framebuffer *fb = state->fb;
  834. if (fb)
  835. drm_framebuffer_unreference(fb);
  836. kfree(state);
  837. }
  838. static void vop_win_update_state(struct vop_win *vop_win)
  839. {
  840. struct vop_win_state *state, *n, *new_active = NULL;
  841. /* Check if any pending states are now active */
  842. list_for_each_entry(state, &vop_win->pending, head)
  843. if (vop_win_state_is_active(vop_win, state)) {
  844. new_active = state;
  845. break;
  846. }
  847. if (!new_active)
  848. return;
  849. /*
  850. * Destroy any 'skipped' pending states - states that were queued
  851. * before the newly active state.
  852. */
  853. list_for_each_entry_safe(state, n, &vop_win->pending, head) {
  854. if (state == new_active)
  855. break;
  856. vop_win_state_complete(vop_win, state);
  857. vop_win_state_destroy(state);
  858. }
  859. vop_win_state_complete(vop_win, new_active);
  860. if (vop_win->active)
  861. vop_win_state_destroy(vop_win->active);
  862. vop_win->active = new_active;
  863. }
  864. static bool vop_win_has_pending_state(struct vop_win *vop_win)
  865. {
  866. return !list_empty(&vop_win->pending);
  867. }
  868. static irqreturn_t vop_isr_thread(int irq, void *data)
  869. {
  870. struct vop *vop = data;
  871. const struct vop_data *vop_data = vop->data;
  872. unsigned int i;
  873. mutex_lock(&vop->vsync_mutex);
  874. if (!vop->vsync_work_pending)
  875. goto done;
  876. vop->vsync_work_pending = false;
  877. for (i = 0; i < vop_data->win_size; i++) {
  878. struct vop_win *vop_win = &vop->win[i];
  879. vop_win_update_state(vop_win);
  880. if (vop_win_has_pending_state(vop_win))
  881. vop->vsync_work_pending = true;
  882. }
  883. done:
  884. mutex_unlock(&vop->vsync_mutex);
  885. return IRQ_HANDLED;
  886. }
  887. static irqreturn_t vop_isr(int irq, void *data)
  888. {
  889. struct vop *vop = data;
  890. uint32_t intr0_reg, active_irqs;
  891. unsigned long flags;
  892. /*
  893. * INTR_CTRL0 register has interrupt status, enable and clear bits, we
  894. * must hold irq_lock to avoid a race with enable/disable_vblank().
  895. */
  896. spin_lock_irqsave(&vop->irq_lock, flags);
  897. intr0_reg = vop_readl(vop, INTR_CTRL0);
  898. active_irqs = intr0_reg & INTR_MASK;
  899. /* Clear all active interrupt sources */
  900. if (active_irqs)
  901. vop_writel(vop, INTR_CTRL0,
  902. intr0_reg | (active_irqs << INTR_CLR_SHIFT));
  903. spin_unlock_irqrestore(&vop->irq_lock, flags);
  904. /* This is expected for vop iommu irqs, since the irq is shared */
  905. if (!active_irqs)
  906. return IRQ_NONE;
  907. /* Only Frame Start Interrupt is enabled; other irqs are spurious. */
  908. if (!(active_irqs & FS_INTR)) {
  909. DRM_ERROR("Unknown VOP IRQs: %#02x\n", active_irqs);
  910. return IRQ_NONE;
  911. }
  912. drm_handle_vblank(vop->drm_dev, vop->pipe);
  913. return (vop->vsync_work_pending) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
  914. }
  915. static int vop_create_crtc(struct vop *vop)
  916. {
  917. const struct vop_data *vop_data = vop->data;
  918. struct device *dev = vop->dev;
  919. struct drm_device *drm_dev = vop->drm_dev;
  920. struct drm_plane *primary = NULL, *cursor = NULL, *plane;
  921. struct drm_crtc *crtc = &vop->crtc;
  922. struct device_node *port;
  923. int ret;
  924. int i;
  925. /*
  926. * Create drm_plane for primary and cursor planes first, since we need
  927. * to pass them to drm_crtc_init_with_planes, which sets the
  928. * "possible_crtcs" to the newly initialized crtc.
  929. */
  930. for (i = 0; i < vop_data->win_size; i++) {
  931. struct vop_win *vop_win = &vop->win[i];
  932. const struct vop_win_data *win_data = vop_win->data;
  933. if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
  934. win_data->type != DRM_PLANE_TYPE_CURSOR)
  935. continue;
  936. ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
  937. 0, &vop_plane_funcs,
  938. win_data->phy->data_formats,
  939. win_data->phy->nformats,
  940. win_data->type);
  941. if (ret) {
  942. DRM_ERROR("failed to initialize plane\n");
  943. goto err_cleanup_planes;
  944. }
  945. plane = &vop_win->base;
  946. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  947. primary = plane;
  948. else if (plane->type == DRM_PLANE_TYPE_CURSOR)
  949. cursor = plane;
  950. }
  951. ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
  952. &vop_crtc_funcs);
  953. if (ret)
  954. return ret;
  955. drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
  956. /*
  957. * Create drm_planes for overlay windows with possible_crtcs restricted
  958. * to the newly created crtc.
  959. */
  960. for (i = 0; i < vop_data->win_size; i++) {
  961. struct vop_win *vop_win = &vop->win[i];
  962. const struct vop_win_data *win_data = vop_win->data;
  963. unsigned long possible_crtcs = 1 << drm_crtc_index(crtc);
  964. if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
  965. continue;
  966. ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
  967. possible_crtcs,
  968. &vop_plane_funcs,
  969. win_data->phy->data_formats,
  970. win_data->phy->nformats,
  971. win_data->type);
  972. if (ret) {
  973. DRM_ERROR("failed to initialize overlay plane\n");
  974. goto err_cleanup_crtc;
  975. }
  976. }
  977. port = of_get_child_by_name(dev->of_node, "port");
  978. if (!port) {
  979. DRM_ERROR("no port node found in %s\n",
  980. dev->of_node->full_name);
  981. goto err_cleanup_crtc;
  982. }
  983. crtc->port = port;
  984. vop->pipe = drm_crtc_index(crtc);
  985. rockchip_register_crtc_funcs(drm_dev, &private_crtc_funcs, vop->pipe);
  986. return 0;
  987. err_cleanup_crtc:
  988. drm_crtc_cleanup(crtc);
  989. err_cleanup_planes:
  990. list_for_each_entry(plane, &drm_dev->mode_config.plane_list, head)
  991. drm_plane_cleanup(plane);
  992. return ret;
  993. }
  994. static void vop_destroy_crtc(struct vop *vop)
  995. {
  996. struct drm_crtc *crtc = &vop->crtc;
  997. rockchip_unregister_crtc_funcs(vop->drm_dev, vop->pipe);
  998. of_node_put(crtc->port);
  999. drm_crtc_cleanup(crtc);
  1000. }
  1001. static int vop_initial(struct vop *vop)
  1002. {
  1003. const struct vop_data *vop_data = vop->data;
  1004. const struct vop_reg_data *init_table = vop_data->init_table;
  1005. struct reset_control *ahb_rst;
  1006. int i, ret;
  1007. vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
  1008. if (IS_ERR(vop->hclk)) {
  1009. dev_err(vop->dev, "failed to get hclk source\n");
  1010. return PTR_ERR(vop->hclk);
  1011. }
  1012. vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
  1013. if (IS_ERR(vop->aclk)) {
  1014. dev_err(vop->dev, "failed to get aclk source\n");
  1015. return PTR_ERR(vop->aclk);
  1016. }
  1017. vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
  1018. if (IS_ERR(vop->dclk)) {
  1019. dev_err(vop->dev, "failed to get dclk source\n");
  1020. return PTR_ERR(vop->dclk);
  1021. }
  1022. ret = clk_prepare(vop->hclk);
  1023. if (ret < 0) {
  1024. dev_err(vop->dev, "failed to prepare hclk\n");
  1025. return ret;
  1026. }
  1027. ret = clk_prepare(vop->dclk);
  1028. if (ret < 0) {
  1029. dev_err(vop->dev, "failed to prepare dclk\n");
  1030. goto err_unprepare_hclk;
  1031. }
  1032. ret = clk_prepare(vop->aclk);
  1033. if (ret < 0) {
  1034. dev_err(vop->dev, "failed to prepare aclk\n");
  1035. goto err_unprepare_dclk;
  1036. }
  1037. /*
  1038. * enable hclk, so that we can config vop register.
  1039. */
  1040. ret = clk_enable(vop->hclk);
  1041. if (ret < 0) {
  1042. dev_err(vop->dev, "failed to prepare aclk\n");
  1043. goto err_unprepare_aclk;
  1044. }
  1045. /*
  1046. * do hclk_reset, reset all vop registers.
  1047. */
  1048. ahb_rst = devm_reset_control_get(vop->dev, "ahb");
  1049. if (IS_ERR(ahb_rst)) {
  1050. dev_err(vop->dev, "failed to get ahb reset\n");
  1051. ret = PTR_ERR(ahb_rst);
  1052. goto err_disable_hclk;
  1053. }
  1054. reset_control_assert(ahb_rst);
  1055. usleep_range(10, 20);
  1056. reset_control_deassert(ahb_rst);
  1057. memcpy(vop->regsbak, vop->regs, vop->len);
  1058. for (i = 0; i < vop_data->table_size; i++)
  1059. vop_writel(vop, init_table[i].offset, init_table[i].value);
  1060. for (i = 0; i < vop_data->win_size; i++) {
  1061. const struct vop_win_data *win = &vop_data->win[i];
  1062. VOP_WIN_SET(vop, win, enable, 0);
  1063. }
  1064. vop_cfg_done(vop);
  1065. /*
  1066. * do dclk_reset, let all config take affect.
  1067. */
  1068. vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
  1069. if (IS_ERR(vop->dclk_rst)) {
  1070. dev_err(vop->dev, "failed to get dclk reset\n");
  1071. ret = PTR_ERR(vop->dclk_rst);
  1072. goto err_unprepare_aclk;
  1073. }
  1074. reset_control_assert(vop->dclk_rst);
  1075. usleep_range(10, 20);
  1076. reset_control_deassert(vop->dclk_rst);
  1077. clk_disable(vop->hclk);
  1078. vop->dpms = DRM_MODE_DPMS_OFF;
  1079. return 0;
  1080. err_disable_hclk:
  1081. clk_disable(vop->hclk);
  1082. err_unprepare_aclk:
  1083. clk_unprepare(vop->aclk);
  1084. err_unprepare_dclk:
  1085. clk_unprepare(vop->dclk);
  1086. err_unprepare_hclk:
  1087. clk_unprepare(vop->hclk);
  1088. return ret;
  1089. }
  1090. /*
  1091. * Initialize the vop->win array elements.
  1092. */
  1093. static void vop_win_init(struct vop *vop)
  1094. {
  1095. const struct vop_data *vop_data = vop->data;
  1096. unsigned int i;
  1097. for (i = 0; i < vop_data->win_size; i++) {
  1098. struct vop_win *vop_win = &vop->win[i];
  1099. const struct vop_win_data *win_data = &vop_data->win[i];
  1100. vop_win->data = win_data;
  1101. vop_win->vop = vop;
  1102. INIT_LIST_HEAD(&vop_win->pending);
  1103. }
  1104. }
  1105. static int vop_bind(struct device *dev, struct device *master, void *data)
  1106. {
  1107. struct platform_device *pdev = to_platform_device(dev);
  1108. const struct of_device_id *of_id;
  1109. const struct vop_data *vop_data;
  1110. struct drm_device *drm_dev = data;
  1111. struct vop *vop;
  1112. struct resource *res;
  1113. size_t alloc_size;
  1114. int ret;
  1115. of_id = of_match_device(vop_driver_dt_match, dev);
  1116. vop_data = of_id->data;
  1117. if (!vop_data)
  1118. return -ENODEV;
  1119. /* Allocate vop struct and its vop_win array */
  1120. alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size;
  1121. vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
  1122. if (!vop)
  1123. return -ENOMEM;
  1124. vop->dev = dev;
  1125. vop->data = vop_data;
  1126. vop->drm_dev = drm_dev;
  1127. dev_set_drvdata(dev, vop);
  1128. vop_win_init(vop);
  1129. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1130. vop->len = resource_size(res);
  1131. vop->regs = devm_ioremap_resource(dev, res);
  1132. if (IS_ERR(vop->regs))
  1133. return PTR_ERR(vop->regs);
  1134. vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
  1135. if (!vop->regsbak)
  1136. return -ENOMEM;
  1137. ret = vop_initial(vop);
  1138. if (ret < 0) {
  1139. dev_err(&pdev->dev, "cannot initial vop dev - err %d\n", ret);
  1140. return ret;
  1141. }
  1142. vop->irq = platform_get_irq(pdev, 0);
  1143. if (vop->irq < 0) {
  1144. dev_err(dev, "cannot find irq for vop\n");
  1145. return vop->irq;
  1146. }
  1147. spin_lock_init(&vop->reg_lock);
  1148. spin_lock_init(&vop->irq_lock);
  1149. mutex_init(&vop->vsync_mutex);
  1150. ret = devm_request_threaded_irq(dev, vop->irq, vop_isr, vop_isr_thread,
  1151. IRQF_SHARED, dev_name(dev), vop);
  1152. if (ret)
  1153. return ret;
  1154. /* IRQ is initially disabled; it gets enabled in power_on */
  1155. disable_irq(vop->irq);
  1156. ret = vop_create_crtc(vop);
  1157. if (ret)
  1158. return ret;
  1159. pm_runtime_enable(&pdev->dev);
  1160. return 0;
  1161. }
  1162. static void vop_unbind(struct device *dev, struct device *master, void *data)
  1163. {
  1164. struct vop *vop = dev_get_drvdata(dev);
  1165. pm_runtime_disable(dev);
  1166. vop_destroy_crtc(vop);
  1167. }
  1168. static const struct component_ops vop_component_ops = {
  1169. .bind = vop_bind,
  1170. .unbind = vop_unbind,
  1171. };
  1172. static int vop_probe(struct platform_device *pdev)
  1173. {
  1174. struct device *dev = &pdev->dev;
  1175. if (!dev->of_node) {
  1176. dev_err(dev, "can't find vop devices\n");
  1177. return -ENODEV;
  1178. }
  1179. return component_add(dev, &vop_component_ops);
  1180. }
  1181. static int vop_remove(struct platform_device *pdev)
  1182. {
  1183. component_del(&pdev->dev, &vop_component_ops);
  1184. return 0;
  1185. }
  1186. struct platform_driver vop_platform_driver = {
  1187. .probe = vop_probe,
  1188. .remove = vop_remove,
  1189. .driver = {
  1190. .name = "rockchip-vop",
  1191. .owner = THIS_MODULE,
  1192. .of_match_table = of_match_ptr(vop_driver_dt_match),
  1193. },
  1194. };
  1195. module_platform_driver(vop_platform_driver);
  1196. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  1197. MODULE_DESCRIPTION("ROCKCHIP VOP Driver");
  1198. MODULE_LICENSE("GPL v2");