rockchip_drm_vop.c 45 KB

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