rockchip_drm_vop.c 36 KB

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