kirin_drm_ade.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * Hisilicon Hi6220 SoC ADE(Advanced Display Engine)'s crtc&plane driver
  3. *
  4. * Copyright (c) 2016 Linaro Limited.
  5. * Copyright (c) 2014-2016 Hisilicon Limited.
  6. *
  7. * Author:
  8. * Xinliang Liu <z.liuxinliang@hisilicon.com>
  9. * Xinliang Liu <xinliang.liu@linaro.org>
  10. * Xinwei Kong <kong.kongxinwei@hisilicon.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/clk.h>
  19. #include <video/display_timing.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/regmap.h>
  22. #include <linux/reset.h>
  23. #include <drm/drmP.h>
  24. #include <drm/drm_crtc.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drm_atomic.h>
  27. #include <drm/drm_atomic_helper.h>
  28. #include <drm/drm_plane_helper.h>
  29. #include <drm/drm_gem_cma_helper.h>
  30. #include <drm/drm_fb_cma_helper.h>
  31. #include "kirin_drm_drv.h"
  32. #include "kirin_ade_reg.h"
  33. #define PRIMARY_CH ADE_CH1 /* primary plane */
  34. #define OUT_OVLY ADE_OVLY2 /* output overlay compositor */
  35. #define ADE_DEBUG 1
  36. #define to_ade_crtc(crtc) \
  37. container_of(crtc, struct ade_crtc, base)
  38. #define to_ade_plane(plane) \
  39. container_of(plane, struct ade_plane, base)
  40. struct ade_hw_ctx {
  41. void __iomem *base;
  42. struct regmap *noc_regmap;
  43. struct clk *ade_core_clk;
  44. struct clk *media_noc_clk;
  45. struct clk *ade_pix_clk;
  46. struct reset_control *reset;
  47. bool power_on;
  48. int irq;
  49. };
  50. struct ade_crtc {
  51. struct drm_crtc base;
  52. struct ade_hw_ctx *ctx;
  53. bool enable;
  54. u32 out_format;
  55. };
  56. struct ade_plane {
  57. struct drm_plane base;
  58. void *ctx;
  59. u8 ch; /* channel */
  60. };
  61. struct ade_data {
  62. struct ade_crtc acrtc;
  63. struct ade_plane aplane[ADE_CH_NUM];
  64. struct ade_hw_ctx ctx;
  65. };
  66. /* ade-format info: */
  67. struct ade_format {
  68. u32 pixel_format;
  69. enum ade_fb_format ade_format;
  70. };
  71. static const struct ade_format ade_formats[] = {
  72. /* 16bpp RGB: */
  73. { DRM_FORMAT_RGB565, ADE_RGB_565 },
  74. { DRM_FORMAT_BGR565, ADE_BGR_565 },
  75. /* 24bpp RGB: */
  76. { DRM_FORMAT_RGB888, ADE_RGB_888 },
  77. { DRM_FORMAT_BGR888, ADE_BGR_888 },
  78. /* 32bpp [A]RGB: */
  79. { DRM_FORMAT_XRGB8888, ADE_XRGB_8888 },
  80. { DRM_FORMAT_XBGR8888, ADE_XBGR_8888 },
  81. { DRM_FORMAT_RGBA8888, ADE_RGBA_8888 },
  82. { DRM_FORMAT_BGRA8888, ADE_BGRA_8888 },
  83. { DRM_FORMAT_ARGB8888, ADE_ARGB_8888 },
  84. { DRM_FORMAT_ABGR8888, ADE_ABGR_8888 },
  85. };
  86. static const u32 channel_formats1[] = {
  87. /* channel 1,2,3,4 */
  88. DRM_FORMAT_RGB565, DRM_FORMAT_BGR565, DRM_FORMAT_RGB888,
  89. DRM_FORMAT_BGR888, DRM_FORMAT_XRGB8888, DRM_FORMAT_XBGR8888,
  90. DRM_FORMAT_RGBA8888, DRM_FORMAT_BGRA8888, DRM_FORMAT_ARGB8888,
  91. DRM_FORMAT_ABGR8888
  92. };
  93. u32 ade_get_channel_formats(u8 ch, const u32 **formats)
  94. {
  95. switch (ch) {
  96. case ADE_CH1:
  97. *formats = channel_formats1;
  98. return ARRAY_SIZE(channel_formats1);
  99. default:
  100. DRM_ERROR("no this channel %d\n", ch);
  101. *formats = NULL;
  102. return 0;
  103. }
  104. }
  105. /* convert from fourcc format to ade format */
  106. static u32 ade_get_format(u32 pixel_format)
  107. {
  108. int i;
  109. for (i = 0; i < ARRAY_SIZE(ade_formats); i++)
  110. if (ade_formats[i].pixel_format == pixel_format)
  111. return ade_formats[i].ade_format;
  112. /* not found */
  113. DRM_ERROR("Not found pixel format!!fourcc_format= %d\n",
  114. pixel_format);
  115. return ADE_FORMAT_UNSUPPORT;
  116. }
  117. static void ade_update_reload_bit(void __iomem *base, u32 bit_num, u32 val)
  118. {
  119. u32 bit_ofst, reg_num;
  120. bit_ofst = bit_num % 32;
  121. reg_num = bit_num / 32;
  122. ade_update_bits(base + ADE_RELOAD_DIS(reg_num), bit_ofst,
  123. MASK(1), !!val);
  124. }
  125. static u32 ade_read_reload_bit(void __iomem *base, u32 bit_num)
  126. {
  127. u32 tmp, bit_ofst, reg_num;
  128. bit_ofst = bit_num % 32;
  129. reg_num = bit_num / 32;
  130. tmp = readl(base + ADE_RELOAD_DIS(reg_num));
  131. return !!(BIT(bit_ofst) & tmp);
  132. }
  133. static void ade_init(struct ade_hw_ctx *ctx)
  134. {
  135. void __iomem *base = ctx->base;
  136. /* enable clk gate */
  137. ade_update_bits(base + ADE_CTRL1, AUTO_CLK_GATE_EN_OFST,
  138. AUTO_CLK_GATE_EN, ADE_ENABLE);
  139. /* clear overlay */
  140. writel(0, base + ADE_OVLY1_TRANS_CFG);
  141. writel(0, base + ADE_OVLY_CTL);
  142. writel(0, base + ADE_OVLYX_CTL(OUT_OVLY));
  143. /* clear reset and reload regs */
  144. writel(MASK(32), base + ADE_SOFT_RST_SEL(0));
  145. writel(MASK(32), base + ADE_SOFT_RST_SEL(1));
  146. writel(MASK(32), base + ADE_RELOAD_DIS(0));
  147. writel(MASK(32), base + ADE_RELOAD_DIS(1));
  148. /*
  149. * for video mode, all the ade registers should
  150. * become effective at frame end.
  151. */
  152. ade_update_bits(base + ADE_CTRL, FRM_END_START_OFST,
  153. FRM_END_START_MASK, REG_EFFECTIVE_IN_ADEEN_FRMEND);
  154. }
  155. static bool ade_crtc_mode_fixup(struct drm_crtc *crtc,
  156. const struct drm_display_mode *mode,
  157. struct drm_display_mode *adjusted_mode)
  158. {
  159. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  160. struct ade_hw_ctx *ctx = acrtc->ctx;
  161. adjusted_mode->clock =
  162. clk_round_rate(ctx->ade_pix_clk, mode->clock * 1000) / 1000;
  163. return true;
  164. }
  165. static void ade_set_pix_clk(struct ade_hw_ctx *ctx,
  166. struct drm_display_mode *mode,
  167. struct drm_display_mode *adj_mode)
  168. {
  169. u32 clk_Hz = mode->clock * 1000;
  170. int ret;
  171. /*
  172. * Success should be guaranteed in mode_valid call back,
  173. * so failure shouldn't happen here
  174. */
  175. ret = clk_set_rate(ctx->ade_pix_clk, clk_Hz);
  176. if (ret)
  177. DRM_ERROR("failed to set pixel clk %dHz (%d)\n", clk_Hz, ret);
  178. adj_mode->clock = clk_get_rate(ctx->ade_pix_clk) / 1000;
  179. }
  180. static void ade_ldi_set_mode(struct ade_crtc *acrtc,
  181. struct drm_display_mode *mode,
  182. struct drm_display_mode *adj_mode)
  183. {
  184. struct ade_hw_ctx *ctx = acrtc->ctx;
  185. void __iomem *base = ctx->base;
  186. u32 width = mode->hdisplay;
  187. u32 height = mode->vdisplay;
  188. u32 hfp, hbp, hsw, vfp, vbp, vsw;
  189. u32 plr_flags;
  190. plr_flags = (mode->flags & DRM_MODE_FLAG_NVSYNC) ? FLAG_NVSYNC : 0;
  191. plr_flags |= (mode->flags & DRM_MODE_FLAG_NHSYNC) ? FLAG_NHSYNC : 0;
  192. hfp = mode->hsync_start - mode->hdisplay;
  193. hbp = mode->htotal - mode->hsync_end;
  194. hsw = mode->hsync_end - mode->hsync_start;
  195. vfp = mode->vsync_start - mode->vdisplay;
  196. vbp = mode->vtotal - mode->vsync_end;
  197. vsw = mode->vsync_end - mode->vsync_start;
  198. if (vsw > 15) {
  199. DRM_DEBUG_DRIVER("vsw exceeded 15\n");
  200. vsw = 15;
  201. }
  202. writel((hbp << HBP_OFST) | hfp, base + LDI_HRZ_CTRL0);
  203. /* the configured value is actual value - 1 */
  204. writel(hsw - 1, base + LDI_HRZ_CTRL1);
  205. writel((vbp << VBP_OFST) | vfp, base + LDI_VRT_CTRL0);
  206. /* the configured value is actual value - 1 */
  207. writel(vsw - 1, base + LDI_VRT_CTRL1);
  208. /* the configured value is actual value - 1 */
  209. writel(((height - 1) << VSIZE_OFST) | (width - 1),
  210. base + LDI_DSP_SIZE);
  211. writel(plr_flags, base + LDI_PLR_CTRL);
  212. /* set overlay compositor output size */
  213. writel(((width - 1) << OUTPUT_XSIZE_OFST) | (height - 1),
  214. base + ADE_OVLY_OUTPUT_SIZE(OUT_OVLY));
  215. /* ctran6 setting */
  216. writel(CTRAN_BYPASS_ON, base + ADE_CTRAN_DIS(ADE_CTRAN6));
  217. /* the configured value is actual value - 1 */
  218. writel(width * height - 1, base + ADE_CTRAN_IMAGE_SIZE(ADE_CTRAN6));
  219. ade_update_reload_bit(base, CTRAN_OFST + ADE_CTRAN6, 0);
  220. ade_set_pix_clk(ctx, mode, adj_mode);
  221. DRM_DEBUG_DRIVER("set mode: %dx%d\n", width, height);
  222. }
  223. static int ade_power_up(struct ade_hw_ctx *ctx)
  224. {
  225. int ret;
  226. ret = clk_prepare_enable(ctx->media_noc_clk);
  227. if (ret) {
  228. DRM_ERROR("failed to enable media_noc_clk (%d)\n", ret);
  229. return ret;
  230. }
  231. ret = reset_control_deassert(ctx->reset);
  232. if (ret) {
  233. DRM_ERROR("failed to deassert reset\n");
  234. return ret;
  235. }
  236. ret = clk_prepare_enable(ctx->ade_core_clk);
  237. if (ret) {
  238. DRM_ERROR("failed to enable ade_core_clk (%d)\n", ret);
  239. return ret;
  240. }
  241. ade_init(ctx);
  242. ctx->power_on = true;
  243. return 0;
  244. }
  245. static void ade_power_down(struct ade_hw_ctx *ctx)
  246. {
  247. void __iomem *base = ctx->base;
  248. writel(ADE_DISABLE, base + LDI_CTRL);
  249. /* dsi pixel off */
  250. writel(DSI_PCLK_OFF, base + LDI_HDMI_DSI_GT);
  251. clk_disable_unprepare(ctx->ade_core_clk);
  252. reset_control_assert(ctx->reset);
  253. clk_disable_unprepare(ctx->media_noc_clk);
  254. ctx->power_on = false;
  255. }
  256. static void ade_set_medianoc_qos(struct ade_crtc *acrtc)
  257. {
  258. struct ade_hw_ctx *ctx = acrtc->ctx;
  259. struct regmap *map = ctx->noc_regmap;
  260. regmap_update_bits(map, ADE0_QOSGENERATOR_MODE,
  261. QOSGENERATOR_MODE_MASK, BYPASS_MODE);
  262. regmap_update_bits(map, ADE0_QOSGENERATOR_EXTCONTROL,
  263. SOCKET_QOS_EN, SOCKET_QOS_EN);
  264. regmap_update_bits(map, ADE1_QOSGENERATOR_MODE,
  265. QOSGENERATOR_MODE_MASK, BYPASS_MODE);
  266. regmap_update_bits(map, ADE1_QOSGENERATOR_EXTCONTROL,
  267. SOCKET_QOS_EN, SOCKET_QOS_EN);
  268. }
  269. static int ade_crtc_enable_vblank(struct drm_crtc *crtc)
  270. {
  271. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  272. struct ade_hw_ctx *ctx = acrtc->ctx;
  273. void __iomem *base = ctx->base;
  274. if (!ctx->power_on)
  275. (void)ade_power_up(ctx);
  276. ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
  277. MASK(1), 1);
  278. return 0;
  279. }
  280. static void ade_crtc_disable_vblank(struct drm_crtc *crtc)
  281. {
  282. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  283. struct ade_hw_ctx *ctx = acrtc->ctx;
  284. void __iomem *base = ctx->base;
  285. if (!ctx->power_on) {
  286. DRM_ERROR("power is down! vblank disable fail\n");
  287. return;
  288. }
  289. ade_update_bits(base + LDI_INT_EN, FRAME_END_INT_EN_OFST,
  290. MASK(1), 0);
  291. }
  292. static irqreturn_t ade_irq_handler(int irq, void *data)
  293. {
  294. struct ade_crtc *acrtc = data;
  295. struct ade_hw_ctx *ctx = acrtc->ctx;
  296. struct drm_crtc *crtc = &acrtc->base;
  297. void __iomem *base = ctx->base;
  298. u32 status;
  299. status = readl(base + LDI_MSK_INT);
  300. DRM_DEBUG_VBL("LDI IRQ: status=0x%X\n", status);
  301. /* vblank irq */
  302. if (status & BIT(FRAME_END_INT_EN_OFST)) {
  303. ade_update_bits(base + LDI_INT_CLR, FRAME_END_INT_EN_OFST,
  304. MASK(1), 1);
  305. drm_crtc_handle_vblank(crtc);
  306. }
  307. return IRQ_HANDLED;
  308. }
  309. static void ade_display_enable(struct ade_crtc *acrtc)
  310. {
  311. struct ade_hw_ctx *ctx = acrtc->ctx;
  312. void __iomem *base = ctx->base;
  313. u32 out_fmt = acrtc->out_format;
  314. /* enable output overlay compositor */
  315. writel(ADE_ENABLE, base + ADE_OVLYX_CTL(OUT_OVLY));
  316. ade_update_reload_bit(base, OVLY_OFST + OUT_OVLY, 0);
  317. /* display source setting */
  318. writel(DISP_SRC_OVLY2, base + ADE_DISP_SRC_CFG);
  319. /* enable ade */
  320. writel(ADE_ENABLE, base + ADE_EN);
  321. /* enable ldi */
  322. writel(NORMAL_MODE, base + LDI_WORK_MODE);
  323. writel((out_fmt << BPP_OFST) | DATA_GATE_EN | LDI_EN,
  324. base + LDI_CTRL);
  325. /* dsi pixel on */
  326. writel(DSI_PCLK_ON, base + LDI_HDMI_DSI_GT);
  327. }
  328. #if ADE_DEBUG
  329. static void ade_rdma_dump_regs(void __iomem *base, u32 ch)
  330. {
  331. u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
  332. u32 val;
  333. reg_ctrl = RD_CH_CTRL(ch);
  334. reg_addr = RD_CH_ADDR(ch);
  335. reg_size = RD_CH_SIZE(ch);
  336. reg_stride = RD_CH_STRIDE(ch);
  337. reg_space = RD_CH_SPACE(ch);
  338. reg_en = RD_CH_EN(ch);
  339. val = ade_read_reload_bit(base, RDMA_OFST + ch);
  340. DRM_DEBUG_DRIVER("[rdma%d]: reload(%d)\n", ch + 1, val);
  341. val = readl(base + reg_ctrl);
  342. DRM_DEBUG_DRIVER("[rdma%d]: reg_ctrl(0x%08x)\n", ch + 1, val);
  343. val = readl(base + reg_addr);
  344. DRM_DEBUG_DRIVER("[rdma%d]: reg_addr(0x%08x)\n", ch + 1, val);
  345. val = readl(base + reg_size);
  346. DRM_DEBUG_DRIVER("[rdma%d]: reg_size(0x%08x)\n", ch + 1, val);
  347. val = readl(base + reg_stride);
  348. DRM_DEBUG_DRIVER("[rdma%d]: reg_stride(0x%08x)\n", ch + 1, val);
  349. val = readl(base + reg_space);
  350. DRM_DEBUG_DRIVER("[rdma%d]: reg_space(0x%08x)\n", ch + 1, val);
  351. val = readl(base + reg_en);
  352. DRM_DEBUG_DRIVER("[rdma%d]: reg_en(0x%08x)\n", ch + 1, val);
  353. }
  354. static void ade_clip_dump_regs(void __iomem *base, u32 ch)
  355. {
  356. u32 val;
  357. val = ade_read_reload_bit(base, CLIP_OFST + ch);
  358. DRM_DEBUG_DRIVER("[clip%d]: reload(%d)\n", ch + 1, val);
  359. val = readl(base + ADE_CLIP_DISABLE(ch));
  360. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_disable(0x%08x)\n", ch + 1, val);
  361. val = readl(base + ADE_CLIP_SIZE0(ch));
  362. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size0(0x%08x)\n", ch + 1, val);
  363. val = readl(base + ADE_CLIP_SIZE1(ch));
  364. DRM_DEBUG_DRIVER("[clip%d]: reg_clip_size1(0x%08x)\n", ch + 1, val);
  365. }
  366. static void ade_compositor_routing_dump_regs(void __iomem *base, u32 ch)
  367. {
  368. u8 ovly_ch = 0; /* TODO: Only primary plane now */
  369. u32 val;
  370. val = readl(base + ADE_OVLY_CH_XY0(ovly_ch));
  371. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy0(0x%08x)\n", ovly_ch, val);
  372. val = readl(base + ADE_OVLY_CH_XY1(ovly_ch));
  373. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_xy1(0x%08x)\n", ovly_ch, val);
  374. val = readl(base + ADE_OVLY_CH_CTL(ovly_ch));
  375. DRM_DEBUG_DRIVER("[overlay ch%d]: reg_ch_ctl(0x%08x)\n", ovly_ch, val);
  376. }
  377. static void ade_dump_overlay_compositor_regs(void __iomem *base, u32 comp)
  378. {
  379. u32 val;
  380. val = ade_read_reload_bit(base, OVLY_OFST + comp);
  381. DRM_DEBUG_DRIVER("[overlay%d]: reload(%d)\n", comp + 1, val);
  382. writel(ADE_ENABLE, base + ADE_OVLYX_CTL(comp));
  383. DRM_DEBUG_DRIVER("[overlay%d]: reg_ctl(0x%08x)\n", comp + 1, val);
  384. val = readl(base + ADE_OVLY_CTL);
  385. DRM_DEBUG_DRIVER("ovly_ctl(0x%08x)\n", val);
  386. }
  387. static void ade_dump_regs(void __iomem *base)
  388. {
  389. u32 i;
  390. /* dump channel regs */
  391. for (i = 0; i < ADE_CH_NUM; i++) {
  392. /* dump rdma regs */
  393. ade_rdma_dump_regs(base, i);
  394. /* dump clip regs */
  395. ade_clip_dump_regs(base, i);
  396. /* dump compositor routing regs */
  397. ade_compositor_routing_dump_regs(base, i);
  398. }
  399. /* dump overlay compositor regs */
  400. ade_dump_overlay_compositor_regs(base, OUT_OVLY);
  401. }
  402. #else
  403. static void ade_dump_regs(void __iomem *base) { }
  404. #endif
  405. static void ade_crtc_atomic_enable(struct drm_crtc *crtc,
  406. struct drm_crtc_state *old_state)
  407. {
  408. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  409. struct ade_hw_ctx *ctx = acrtc->ctx;
  410. int ret;
  411. if (acrtc->enable)
  412. return;
  413. if (!ctx->power_on) {
  414. ret = ade_power_up(ctx);
  415. if (ret)
  416. return;
  417. }
  418. ade_set_medianoc_qos(acrtc);
  419. ade_display_enable(acrtc);
  420. ade_dump_regs(ctx->base);
  421. drm_crtc_vblank_on(crtc);
  422. acrtc->enable = true;
  423. }
  424. static void ade_crtc_atomic_disable(struct drm_crtc *crtc,
  425. struct drm_crtc_state *old_state)
  426. {
  427. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  428. struct ade_hw_ctx *ctx = acrtc->ctx;
  429. if (!acrtc->enable)
  430. return;
  431. drm_crtc_vblank_off(crtc);
  432. ade_power_down(ctx);
  433. acrtc->enable = false;
  434. }
  435. static void ade_crtc_mode_set_nofb(struct drm_crtc *crtc)
  436. {
  437. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  438. struct ade_hw_ctx *ctx = acrtc->ctx;
  439. struct drm_display_mode *mode = &crtc->state->mode;
  440. struct drm_display_mode *adj_mode = &crtc->state->adjusted_mode;
  441. if (!ctx->power_on)
  442. (void)ade_power_up(ctx);
  443. ade_ldi_set_mode(acrtc, mode, adj_mode);
  444. }
  445. static void ade_crtc_atomic_begin(struct drm_crtc *crtc,
  446. struct drm_crtc_state *old_state)
  447. {
  448. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  449. struct ade_hw_ctx *ctx = acrtc->ctx;
  450. struct drm_display_mode *mode = &crtc->state->mode;
  451. struct drm_display_mode *adj_mode = &crtc->state->adjusted_mode;
  452. if (!ctx->power_on)
  453. (void)ade_power_up(ctx);
  454. ade_ldi_set_mode(acrtc, mode, adj_mode);
  455. }
  456. static void ade_crtc_atomic_flush(struct drm_crtc *crtc,
  457. struct drm_crtc_state *old_state)
  458. {
  459. struct ade_crtc *acrtc = to_ade_crtc(crtc);
  460. struct ade_hw_ctx *ctx = acrtc->ctx;
  461. struct drm_pending_vblank_event *event = crtc->state->event;
  462. void __iomem *base = ctx->base;
  463. /* only crtc is enabled regs take effect */
  464. if (acrtc->enable) {
  465. ade_dump_regs(base);
  466. /* flush ade registers */
  467. writel(ADE_ENABLE, base + ADE_EN);
  468. }
  469. if (event) {
  470. crtc->state->event = NULL;
  471. spin_lock_irq(&crtc->dev->event_lock);
  472. if (drm_crtc_vblank_get(crtc) == 0)
  473. drm_crtc_arm_vblank_event(crtc, event);
  474. else
  475. drm_crtc_send_vblank_event(crtc, event);
  476. spin_unlock_irq(&crtc->dev->event_lock);
  477. }
  478. }
  479. static const struct drm_crtc_helper_funcs ade_crtc_helper_funcs = {
  480. .mode_fixup = ade_crtc_mode_fixup,
  481. .mode_set_nofb = ade_crtc_mode_set_nofb,
  482. .atomic_begin = ade_crtc_atomic_begin,
  483. .atomic_flush = ade_crtc_atomic_flush,
  484. .atomic_enable = ade_crtc_atomic_enable,
  485. .atomic_disable = ade_crtc_atomic_disable,
  486. };
  487. static const struct drm_crtc_funcs ade_crtc_funcs = {
  488. .destroy = drm_crtc_cleanup,
  489. .set_config = drm_atomic_helper_set_config,
  490. .page_flip = drm_atomic_helper_page_flip,
  491. .reset = drm_atomic_helper_crtc_reset,
  492. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  493. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  494. .enable_vblank = ade_crtc_enable_vblank,
  495. .disable_vblank = ade_crtc_disable_vblank,
  496. };
  497. static int ade_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  498. struct drm_plane *plane)
  499. {
  500. struct device_node *port;
  501. int ret;
  502. /* set crtc port so that
  503. * drm_of_find_possible_crtcs call works
  504. */
  505. port = of_get_child_by_name(dev->dev->of_node, "port");
  506. if (!port) {
  507. DRM_ERROR("no port node found in %pOF\n", dev->dev->of_node);
  508. return -EINVAL;
  509. }
  510. of_node_put(port);
  511. crtc->port = port;
  512. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  513. &ade_crtc_funcs, NULL);
  514. if (ret) {
  515. DRM_ERROR("failed to init crtc.\n");
  516. return ret;
  517. }
  518. drm_crtc_helper_add(crtc, &ade_crtc_helper_funcs);
  519. return 0;
  520. }
  521. static void ade_rdma_set(void __iomem *base, struct drm_framebuffer *fb,
  522. u32 ch, u32 y, u32 in_h, u32 fmt)
  523. {
  524. struct drm_gem_cma_object *obj = drm_fb_cma_get_gem_obj(fb, 0);
  525. struct drm_format_name_buf format_name;
  526. u32 reg_ctrl, reg_addr, reg_size, reg_stride, reg_space, reg_en;
  527. u32 stride = fb->pitches[0];
  528. u32 addr = (u32)obj->paddr + y * stride;
  529. DRM_DEBUG_DRIVER("rdma%d: (y=%d, height=%d), stride=%d, paddr=0x%x\n",
  530. ch + 1, y, in_h, stride, (u32)obj->paddr);
  531. DRM_DEBUG_DRIVER("addr=0x%x, fb:%dx%d, pixel_format=%d(%s)\n",
  532. addr, fb->width, fb->height, fmt,
  533. drm_get_format_name(fb->format->format, &format_name));
  534. /* get reg offset */
  535. reg_ctrl = RD_CH_CTRL(ch);
  536. reg_addr = RD_CH_ADDR(ch);
  537. reg_size = RD_CH_SIZE(ch);
  538. reg_stride = RD_CH_STRIDE(ch);
  539. reg_space = RD_CH_SPACE(ch);
  540. reg_en = RD_CH_EN(ch);
  541. /*
  542. * TODO: set rotation
  543. */
  544. writel((fmt << 16) & 0x1f0000, base + reg_ctrl);
  545. writel(addr, base + reg_addr);
  546. writel((in_h << 16) | stride, base + reg_size);
  547. writel(stride, base + reg_stride);
  548. writel(in_h * stride, base + reg_space);
  549. writel(ADE_ENABLE, base + reg_en);
  550. ade_update_reload_bit(base, RDMA_OFST + ch, 0);
  551. }
  552. static void ade_rdma_disable(void __iomem *base, u32 ch)
  553. {
  554. u32 reg_en;
  555. /* get reg offset */
  556. reg_en = RD_CH_EN(ch);
  557. writel(0, base + reg_en);
  558. ade_update_reload_bit(base, RDMA_OFST + ch, 1);
  559. }
  560. static void ade_clip_set(void __iomem *base, u32 ch, u32 fb_w, u32 x,
  561. u32 in_w, u32 in_h)
  562. {
  563. u32 disable_val;
  564. u32 clip_left;
  565. u32 clip_right;
  566. /*
  567. * clip width, no need to clip height
  568. */
  569. if (fb_w == in_w) { /* bypass */
  570. disable_val = 1;
  571. clip_left = 0;
  572. clip_right = 0;
  573. } else {
  574. disable_val = 0;
  575. clip_left = x;
  576. clip_right = fb_w - (x + in_w) - 1;
  577. }
  578. DRM_DEBUG_DRIVER("clip%d: clip_left=%d, clip_right=%d\n",
  579. ch + 1, clip_left, clip_right);
  580. writel(disable_val, base + ADE_CLIP_DISABLE(ch));
  581. writel((fb_w - 1) << 16 | (in_h - 1), base + ADE_CLIP_SIZE0(ch));
  582. writel(clip_left << 16 | clip_right, base + ADE_CLIP_SIZE1(ch));
  583. ade_update_reload_bit(base, CLIP_OFST + ch, 0);
  584. }
  585. static void ade_clip_disable(void __iomem *base, u32 ch)
  586. {
  587. writel(1, base + ADE_CLIP_DISABLE(ch));
  588. ade_update_reload_bit(base, CLIP_OFST + ch, 1);
  589. }
  590. static bool has_Alpha_channel(int format)
  591. {
  592. switch (format) {
  593. case ADE_ARGB_8888:
  594. case ADE_ABGR_8888:
  595. case ADE_RGBA_8888:
  596. case ADE_BGRA_8888:
  597. return true;
  598. default:
  599. return false;
  600. }
  601. }
  602. static void ade_get_blending_params(u32 fmt, u8 glb_alpha, u8 *alp_mode,
  603. u8 *alp_sel, u8 *under_alp_sel)
  604. {
  605. bool has_alpha = has_Alpha_channel(fmt);
  606. /*
  607. * get alp_mode
  608. */
  609. if (has_alpha && glb_alpha < 255)
  610. *alp_mode = ADE_ALP_PIXEL_AND_GLB;
  611. else if (has_alpha)
  612. *alp_mode = ADE_ALP_PIXEL;
  613. else
  614. *alp_mode = ADE_ALP_GLOBAL;
  615. /*
  616. * get alp sel
  617. */
  618. *alp_sel = ADE_ALP_MUL_COEFF_3; /* 1 */
  619. *under_alp_sel = ADE_ALP_MUL_COEFF_2; /* 0 */
  620. }
  621. static void ade_compositor_routing_set(void __iomem *base, u8 ch,
  622. u32 x0, u32 y0,
  623. u32 in_w, u32 in_h, u32 fmt)
  624. {
  625. u8 ovly_ch = 0; /* TODO: This is the zpos, only one plane now */
  626. u8 glb_alpha = 255;
  627. u32 x1 = x0 + in_w - 1;
  628. u32 y1 = y0 + in_h - 1;
  629. u32 val;
  630. u8 alp_sel;
  631. u8 under_alp_sel;
  632. u8 alp_mode;
  633. ade_get_blending_params(fmt, glb_alpha, &alp_mode, &alp_sel,
  634. &under_alp_sel);
  635. /* overlay routing setting
  636. */
  637. writel(x0 << 16 | y0, base + ADE_OVLY_CH_XY0(ovly_ch));
  638. writel(x1 << 16 | y1, base + ADE_OVLY_CH_XY1(ovly_ch));
  639. val = (ch + 1) << CH_SEL_OFST | BIT(CH_EN_OFST) |
  640. alp_sel << CH_ALP_SEL_OFST |
  641. under_alp_sel << CH_UNDER_ALP_SEL_OFST |
  642. glb_alpha << CH_ALP_GBL_OFST |
  643. alp_mode << CH_ALP_MODE_OFST;
  644. writel(val, base + ADE_OVLY_CH_CTL(ovly_ch));
  645. /* connect this plane/channel to overlay2 compositor */
  646. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  647. CH_OVLY_SEL_MASK, CH_OVLY_SEL_VAL(OUT_OVLY));
  648. }
  649. static void ade_compositor_routing_disable(void __iomem *base, u32 ch)
  650. {
  651. u8 ovly_ch = 0; /* TODO: Only primary plane now */
  652. /* disable this plane/channel */
  653. ade_update_bits(base + ADE_OVLY_CH_CTL(ovly_ch), CH_EN_OFST,
  654. MASK(1), 0);
  655. /* dis-connect this plane/channel of overlay2 compositor */
  656. ade_update_bits(base + ADE_OVLY_CTL, CH_OVLY_SEL_OFST(ovly_ch),
  657. CH_OVLY_SEL_MASK, 0);
  658. }
  659. /*
  660. * Typicaly, a channel looks like: DMA-->clip-->scale-->ctrans-->compositor
  661. */
  662. static void ade_update_channel(struct ade_plane *aplane,
  663. struct drm_framebuffer *fb, int crtc_x,
  664. int crtc_y, unsigned int crtc_w,
  665. unsigned int crtc_h, u32 src_x,
  666. u32 src_y, u32 src_w, u32 src_h)
  667. {
  668. struct ade_hw_ctx *ctx = aplane->ctx;
  669. void __iomem *base = ctx->base;
  670. u32 fmt = ade_get_format(fb->format->format);
  671. u32 ch = aplane->ch;
  672. u32 in_w;
  673. u32 in_h;
  674. DRM_DEBUG_DRIVER("channel%d: src:(%d, %d)-%dx%d, crtc:(%d, %d)-%dx%d",
  675. ch + 1, src_x, src_y, src_w, src_h,
  676. crtc_x, crtc_y, crtc_w, crtc_h);
  677. /* 1) DMA setting */
  678. in_w = src_w;
  679. in_h = src_h;
  680. ade_rdma_set(base, fb, ch, src_y, in_h, fmt);
  681. /* 2) clip setting */
  682. ade_clip_set(base, ch, fb->width, src_x, in_w, in_h);
  683. /* 3) TODO: scale setting for overlay planes */
  684. /* 4) TODO: ctran/csc setting for overlay planes */
  685. /* 5) compositor routing setting */
  686. ade_compositor_routing_set(base, ch, crtc_x, crtc_y, in_w, in_h, fmt);
  687. }
  688. static void ade_disable_channel(struct ade_plane *aplane)
  689. {
  690. struct ade_hw_ctx *ctx = aplane->ctx;
  691. void __iomem *base = ctx->base;
  692. u32 ch = aplane->ch;
  693. DRM_DEBUG_DRIVER("disable channel%d\n", ch + 1);
  694. /* disable read DMA */
  695. ade_rdma_disable(base, ch);
  696. /* disable clip */
  697. ade_clip_disable(base, ch);
  698. /* disable compositor routing */
  699. ade_compositor_routing_disable(base, ch);
  700. }
  701. static int ade_plane_atomic_check(struct drm_plane *plane,
  702. struct drm_plane_state *state)
  703. {
  704. struct drm_framebuffer *fb = state->fb;
  705. struct drm_crtc *crtc = state->crtc;
  706. struct drm_crtc_state *crtc_state;
  707. u32 src_x = state->src_x >> 16;
  708. u32 src_y = state->src_y >> 16;
  709. u32 src_w = state->src_w >> 16;
  710. u32 src_h = state->src_h >> 16;
  711. int crtc_x = state->crtc_x;
  712. int crtc_y = state->crtc_y;
  713. u32 crtc_w = state->crtc_w;
  714. u32 crtc_h = state->crtc_h;
  715. u32 fmt;
  716. if (!crtc || !fb)
  717. return 0;
  718. fmt = ade_get_format(fb->format->format);
  719. if (fmt == ADE_FORMAT_UNSUPPORT)
  720. return -EINVAL;
  721. crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
  722. if (IS_ERR(crtc_state))
  723. return PTR_ERR(crtc_state);
  724. if (src_w != crtc_w || src_h != crtc_h) {
  725. DRM_ERROR("Scale not support!!!\n");
  726. return -EINVAL;
  727. }
  728. if (src_x + src_w > fb->width ||
  729. src_y + src_h > fb->height)
  730. return -EINVAL;
  731. if (crtc_x < 0 || crtc_y < 0)
  732. return -EINVAL;
  733. if (crtc_x + crtc_w > crtc_state->adjusted_mode.hdisplay ||
  734. crtc_y + crtc_h > crtc_state->adjusted_mode.vdisplay)
  735. return -EINVAL;
  736. return 0;
  737. }
  738. static void ade_plane_atomic_update(struct drm_plane *plane,
  739. struct drm_plane_state *old_state)
  740. {
  741. struct drm_plane_state *state = plane->state;
  742. struct ade_plane *aplane = to_ade_plane(plane);
  743. ade_update_channel(aplane, state->fb, state->crtc_x, state->crtc_y,
  744. state->crtc_w, state->crtc_h,
  745. state->src_x >> 16, state->src_y >> 16,
  746. state->src_w >> 16, state->src_h >> 16);
  747. }
  748. static void ade_plane_atomic_disable(struct drm_plane *plane,
  749. struct drm_plane_state *old_state)
  750. {
  751. struct ade_plane *aplane = to_ade_plane(plane);
  752. ade_disable_channel(aplane);
  753. }
  754. static const struct drm_plane_helper_funcs ade_plane_helper_funcs = {
  755. .atomic_check = ade_plane_atomic_check,
  756. .atomic_update = ade_plane_atomic_update,
  757. .atomic_disable = ade_plane_atomic_disable,
  758. };
  759. static struct drm_plane_funcs ade_plane_funcs = {
  760. .update_plane = drm_atomic_helper_update_plane,
  761. .disable_plane = drm_atomic_helper_disable_plane,
  762. .destroy = drm_plane_cleanup,
  763. .reset = drm_atomic_helper_plane_reset,
  764. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  765. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  766. };
  767. static int ade_plane_init(struct drm_device *dev, struct ade_plane *aplane,
  768. enum drm_plane_type type)
  769. {
  770. const u32 *fmts;
  771. u32 fmts_cnt;
  772. int ret = 0;
  773. /* get properties */
  774. fmts_cnt = ade_get_channel_formats(aplane->ch, &fmts);
  775. if (ret)
  776. return ret;
  777. ret = drm_universal_plane_init(dev, &aplane->base, 1, &ade_plane_funcs,
  778. fmts, fmts_cnt, NULL, type, NULL);
  779. if (ret) {
  780. DRM_ERROR("fail to init plane, ch=%d\n", aplane->ch);
  781. return ret;
  782. }
  783. drm_plane_helper_add(&aplane->base, &ade_plane_helper_funcs);
  784. return 0;
  785. }
  786. static int ade_dts_parse(struct platform_device *pdev, struct ade_hw_ctx *ctx)
  787. {
  788. struct resource *res;
  789. struct device *dev = &pdev->dev;
  790. struct device_node *np = pdev->dev.of_node;
  791. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  792. ctx->base = devm_ioremap_resource(dev, res);
  793. if (IS_ERR(ctx->base)) {
  794. DRM_ERROR("failed to remap ade io base\n");
  795. return PTR_ERR(ctx->base);
  796. }
  797. ctx->reset = devm_reset_control_get(dev, NULL);
  798. if (IS_ERR(ctx->reset))
  799. return PTR_ERR(ctx->reset);
  800. ctx->noc_regmap =
  801. syscon_regmap_lookup_by_phandle(np, "hisilicon,noc-syscon");
  802. if (IS_ERR(ctx->noc_regmap)) {
  803. DRM_ERROR("failed to get noc regmap\n");
  804. return PTR_ERR(ctx->noc_regmap);
  805. }
  806. ctx->irq = platform_get_irq(pdev, 0);
  807. if (ctx->irq < 0) {
  808. DRM_ERROR("failed to get irq\n");
  809. return -ENODEV;
  810. }
  811. ctx->ade_core_clk = devm_clk_get(dev, "clk_ade_core");
  812. if (IS_ERR(ctx->ade_core_clk)) {
  813. DRM_ERROR("failed to parse clk ADE_CORE\n");
  814. return PTR_ERR(ctx->ade_core_clk);
  815. }
  816. ctx->media_noc_clk = devm_clk_get(dev, "clk_codec_jpeg");
  817. if (IS_ERR(ctx->media_noc_clk)) {
  818. DRM_ERROR("failed to parse clk CODEC_JPEG\n");
  819. return PTR_ERR(ctx->media_noc_clk);
  820. }
  821. ctx->ade_pix_clk = devm_clk_get(dev, "clk_ade_pix");
  822. if (IS_ERR(ctx->ade_pix_clk)) {
  823. DRM_ERROR("failed to parse clk ADE_PIX\n");
  824. return PTR_ERR(ctx->ade_pix_clk);
  825. }
  826. return 0;
  827. }
  828. static int ade_drm_init(struct platform_device *pdev)
  829. {
  830. struct drm_device *dev = platform_get_drvdata(pdev);
  831. struct ade_data *ade;
  832. struct ade_hw_ctx *ctx;
  833. struct ade_crtc *acrtc;
  834. struct ade_plane *aplane;
  835. enum drm_plane_type type;
  836. int ret;
  837. int i;
  838. ade = devm_kzalloc(dev->dev, sizeof(*ade), GFP_KERNEL);
  839. if (!ade) {
  840. DRM_ERROR("failed to alloc ade_data\n");
  841. return -ENOMEM;
  842. }
  843. platform_set_drvdata(pdev, ade);
  844. ctx = &ade->ctx;
  845. acrtc = &ade->acrtc;
  846. acrtc->ctx = ctx;
  847. acrtc->out_format = LDI_OUT_RGB_888;
  848. ret = ade_dts_parse(pdev, ctx);
  849. if (ret)
  850. return ret;
  851. /*
  852. * plane init
  853. * TODO: Now only support primary plane, overlay planes
  854. * need to do.
  855. */
  856. for (i = 0; i < ADE_CH_NUM; i++) {
  857. aplane = &ade->aplane[i];
  858. aplane->ch = i;
  859. aplane->ctx = ctx;
  860. type = i == PRIMARY_CH ? DRM_PLANE_TYPE_PRIMARY :
  861. DRM_PLANE_TYPE_OVERLAY;
  862. ret = ade_plane_init(dev, aplane, type);
  863. if (ret)
  864. return ret;
  865. }
  866. /* crtc init */
  867. ret = ade_crtc_init(dev, &acrtc->base, &ade->aplane[PRIMARY_CH].base);
  868. if (ret)
  869. return ret;
  870. /* vblank irq init */
  871. ret = devm_request_irq(dev->dev, ctx->irq, ade_irq_handler,
  872. IRQF_SHARED, dev->driver->name, acrtc);
  873. if (ret)
  874. return ret;
  875. return 0;
  876. }
  877. static void ade_drm_cleanup(struct platform_device *pdev)
  878. {
  879. }
  880. const struct kirin_dc_ops ade_dc_ops = {
  881. .init = ade_drm_init,
  882. .cleanup = ade_drm_cleanup
  883. };