zynqmp_disp.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ZynqMP Display Controller Driver
  4. *
  5. * Copyright (C) 2017 - 2020 Xilinx, Inc.
  6. *
  7. * Authors:
  8. * - Hyun Woo Kwon <hyun.kwon@xilinx.com>
  9. * - Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. */
  11. #include <drm/drm_fb_dma_helper.h>
  12. #include <drm/drm_fourcc.h>
  13. #include <drm/drm_framebuffer.h>
  14. #include <drm/drm_plane.h>
  15. #include <linux/clk.h>
  16. #include <linux/dma/xilinx_dpdma.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/media-bus-format.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include "zynqmp_disp.h"
  25. #include "zynqmp_disp_regs.h"
  26. #include "zynqmp_dp.h"
  27. #include "zynqmp_dpsub.h"
  28. /*
  29. * Overview
  30. * --------
  31. *
  32. * The display controller part of ZynqMP DP subsystem, made of the Audio/Video
  33. * Buffer Manager, the Video Rendering Pipeline (blender) and the Audio Mixer.
  34. *
  35. * +------------------------------------------------------------+
  36. * +--------+ | +----------------+ +-----------+ |
  37. * | DPDMA | --->| | --> | Video | Video +-------------+ |
  38. * | 4x vid | | | | | Rendering | -+--> | | | +------+
  39. * | 2x aud | | | Audio/Video | --> | Pipeline | | | DisplayPort |---> | PHY0 |
  40. * +--------+ | | Buffer Manager | +-----------+ | | Source | | +------+
  41. * | | and STC | +-----------+ | | Controller | | +------+
  42. * Live Video --->| | --> | Audio | Audio | |---> | PHY1 |
  43. * | | | | Mixer | --+-> | | | +------+
  44. * Live Audio --->| | --> | | || +-------------+ |
  45. * | +----------------+ +-----------+ || |
  46. * +---------------------------------------||-------------------+
  47. * vv
  48. * Blended Video and
  49. * Mixed Audio to PL
  50. *
  51. * Only non-live input from the DPDMA and output to the DisplayPort Source
  52. * Controller are currently supported. Interface with the programmable logic
  53. * for live streams is not implemented.
  54. *
  55. * The display controller code creates planes for the DPDMA video and graphics
  56. * layers, and a CRTC for the Video Rendering Pipeline.
  57. */
  58. #define ZYNQMP_DISP_AV_BUF_NUM_VID_GFX_BUFFERS 4
  59. #define ZYNQMP_DISP_AV_BUF_NUM_BUFFERS 6
  60. #define ZYNQMP_DISP_MAX_NUM_SUB_PLANES 3
  61. /**
  62. * enum zynqmp_dpsub_layer_mode - Layer mode
  63. * @ZYNQMP_DPSUB_LAYER_NONLIVE: non-live (memory) mode
  64. * @ZYNQMP_DPSUB_LAYER_LIVE: live (stream) mode
  65. */
  66. enum zynqmp_dpsub_layer_mode {
  67. ZYNQMP_DPSUB_LAYER_NONLIVE,
  68. ZYNQMP_DPSUB_LAYER_LIVE,
  69. };
  70. /**
  71. * struct zynqmp_disp_format - Display subsystem format information
  72. * @drm_fmt: DRM format (4CC)
  73. * @bus_fmt: Media bus format
  74. * @buf_fmt: AV buffer format
  75. * @swap: Flag to swap R & B for RGB formats, and U & V for YUV formats
  76. * @sf: Scaling factors for color components
  77. */
  78. struct zynqmp_disp_format {
  79. u32 drm_fmt;
  80. u32 bus_fmt;
  81. u32 buf_fmt;
  82. bool swap;
  83. const u32 *sf;
  84. };
  85. /**
  86. * struct zynqmp_disp_layer_dma - DMA channel for one data plane of a layer
  87. * @chan: DMA channel
  88. * @xt: Interleaved DMA descriptor template
  89. * @sgl: Data chunk for dma_interleaved_template
  90. */
  91. struct zynqmp_disp_layer_dma {
  92. struct dma_chan *chan;
  93. struct dma_interleaved_template xt;
  94. struct data_chunk sgl;
  95. };
  96. /**
  97. * struct zynqmp_disp_layer_info - Static layer information
  98. * @formats: Array of supported formats
  99. * @num_formats: Number of formats in @formats array
  100. * @num_channels: Number of DMA channels
  101. */
  102. struct zynqmp_disp_layer_info {
  103. const struct zynqmp_disp_format *formats;
  104. unsigned int num_formats;
  105. unsigned int num_channels;
  106. };
  107. /**
  108. * struct zynqmp_disp_layer - Display layer
  109. * @id: Layer ID
  110. * @disp: Back pointer to struct zynqmp_disp
  111. * @info: Static layer information
  112. * @dmas: DMA channels
  113. * @disp_fmt: Current format information
  114. * @drm_fmt: Current DRM format information
  115. * @mode: Current operation mode
  116. */
  117. struct zynqmp_disp_layer {
  118. enum zynqmp_dpsub_layer_id id;
  119. struct zynqmp_disp *disp;
  120. const struct zynqmp_disp_layer_info *info;
  121. struct zynqmp_disp_layer_dma dmas[ZYNQMP_DISP_MAX_NUM_SUB_PLANES];
  122. const struct zynqmp_disp_format *disp_fmt;
  123. const struct drm_format_info *drm_fmt;
  124. enum zynqmp_dpsub_layer_mode mode;
  125. };
  126. /**
  127. * struct zynqmp_disp - Display controller
  128. * @dev: Device structure
  129. * @dpsub: Display subsystem
  130. * @blend: Register I/O base address for the blender
  131. * @avbuf: Register I/O base address for the audio/video buffer manager
  132. * @layers: Layers (planes)
  133. */
  134. struct zynqmp_disp {
  135. struct device *dev;
  136. struct zynqmp_dpsub *dpsub;
  137. void __iomem *blend;
  138. void __iomem *avbuf;
  139. struct zynqmp_disp_layer layers[ZYNQMP_DPSUB_NUM_LAYERS];
  140. };
  141. /* -----------------------------------------------------------------------------
  142. * Audio/Video Buffer Manager
  143. */
  144. static const u32 scaling_factors_444[] = {
  145. ZYNQMP_DISP_AV_BUF_4BIT_SF,
  146. ZYNQMP_DISP_AV_BUF_4BIT_SF,
  147. ZYNQMP_DISP_AV_BUF_4BIT_SF,
  148. };
  149. static const u32 scaling_factors_555[] = {
  150. ZYNQMP_DISP_AV_BUF_5BIT_SF,
  151. ZYNQMP_DISP_AV_BUF_5BIT_SF,
  152. ZYNQMP_DISP_AV_BUF_5BIT_SF,
  153. };
  154. static const u32 scaling_factors_565[] = {
  155. ZYNQMP_DISP_AV_BUF_5BIT_SF,
  156. ZYNQMP_DISP_AV_BUF_6BIT_SF,
  157. ZYNQMP_DISP_AV_BUF_5BIT_SF,
  158. };
  159. static const u32 scaling_factors_666[] = {
  160. ZYNQMP_DISP_AV_BUF_6BIT_SF,
  161. ZYNQMP_DISP_AV_BUF_6BIT_SF,
  162. ZYNQMP_DISP_AV_BUF_6BIT_SF,
  163. };
  164. static const u32 scaling_factors_888[] = {
  165. ZYNQMP_DISP_AV_BUF_8BIT_SF,
  166. ZYNQMP_DISP_AV_BUF_8BIT_SF,
  167. ZYNQMP_DISP_AV_BUF_8BIT_SF,
  168. };
  169. static const u32 scaling_factors_101010[] = {
  170. ZYNQMP_DISP_AV_BUF_10BIT_SF,
  171. ZYNQMP_DISP_AV_BUF_10BIT_SF,
  172. ZYNQMP_DISP_AV_BUF_10BIT_SF,
  173. };
  174. /* List of video layer formats */
  175. static const struct zynqmp_disp_format avbuf_vid_fmts[] = {
  176. {
  177. .drm_fmt = DRM_FORMAT_VYUY,
  178. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_VYUY,
  179. .swap = true,
  180. .sf = scaling_factors_888,
  181. }, {
  182. .drm_fmt = DRM_FORMAT_UYVY,
  183. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_VYUY,
  184. .swap = false,
  185. .sf = scaling_factors_888,
  186. }, {
  187. .drm_fmt = DRM_FORMAT_YUYV,
  188. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YUYV,
  189. .swap = false,
  190. .sf = scaling_factors_888,
  191. }, {
  192. .drm_fmt = DRM_FORMAT_YVYU,
  193. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YUYV,
  194. .swap = true,
  195. .sf = scaling_factors_888,
  196. }, {
  197. .drm_fmt = DRM_FORMAT_YUV422,
  198. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16,
  199. .swap = false,
  200. .sf = scaling_factors_888,
  201. }, {
  202. .drm_fmt = DRM_FORMAT_YVU422,
  203. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16,
  204. .swap = true,
  205. .sf = scaling_factors_888,
  206. }, {
  207. .drm_fmt = DRM_FORMAT_YUV444,
  208. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV24,
  209. .swap = false,
  210. .sf = scaling_factors_888,
  211. }, {
  212. .drm_fmt = DRM_FORMAT_YVU444,
  213. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV24,
  214. .swap = true,
  215. .sf = scaling_factors_888,
  216. }, {
  217. .drm_fmt = DRM_FORMAT_NV16,
  218. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16CI,
  219. .swap = false,
  220. .sf = scaling_factors_888,
  221. }, {
  222. .drm_fmt = DRM_FORMAT_NV61,
  223. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16CI,
  224. .swap = true,
  225. .sf = scaling_factors_888,
  226. }, {
  227. .drm_fmt = DRM_FORMAT_BGR888,
  228. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_RGB888,
  229. .swap = false,
  230. .sf = scaling_factors_888,
  231. }, {
  232. .drm_fmt = DRM_FORMAT_RGB888,
  233. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_RGB888,
  234. .swap = true,
  235. .sf = scaling_factors_888,
  236. }, {
  237. .drm_fmt = DRM_FORMAT_XBGR8888,
  238. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_RGBA8880,
  239. .swap = false,
  240. .sf = scaling_factors_888,
  241. }, {
  242. .drm_fmt = DRM_FORMAT_XRGB8888,
  243. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_RGBA8880,
  244. .swap = true,
  245. .sf = scaling_factors_888,
  246. }, {
  247. .drm_fmt = DRM_FORMAT_XBGR2101010,
  248. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_RGB888_10,
  249. .swap = false,
  250. .sf = scaling_factors_101010,
  251. }, {
  252. .drm_fmt = DRM_FORMAT_XRGB2101010,
  253. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_RGB888_10,
  254. .swap = true,
  255. .sf = scaling_factors_101010,
  256. }, {
  257. .drm_fmt = DRM_FORMAT_YUV420,
  258. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16_420,
  259. .swap = false,
  260. .sf = scaling_factors_888,
  261. }, {
  262. .drm_fmt = DRM_FORMAT_YVU420,
  263. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16_420,
  264. .swap = true,
  265. .sf = scaling_factors_888,
  266. }, {
  267. .drm_fmt = DRM_FORMAT_NV12,
  268. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16CI_420,
  269. .swap = false,
  270. .sf = scaling_factors_888,
  271. }, {
  272. .drm_fmt = DRM_FORMAT_NV21,
  273. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_VID_YV16CI_420,
  274. .swap = true,
  275. .sf = scaling_factors_888,
  276. },
  277. };
  278. /* List of graphics layer formats */
  279. static const struct zynqmp_disp_format avbuf_gfx_fmts[] = {
  280. {
  281. .drm_fmt = DRM_FORMAT_ABGR8888,
  282. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGBA8888,
  283. .swap = false,
  284. .sf = scaling_factors_888,
  285. }, {
  286. .drm_fmt = DRM_FORMAT_ARGB8888,
  287. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGBA8888,
  288. .swap = true,
  289. .sf = scaling_factors_888,
  290. }, {
  291. .drm_fmt = DRM_FORMAT_RGBA8888,
  292. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_ABGR8888,
  293. .swap = false,
  294. .sf = scaling_factors_888,
  295. }, {
  296. .drm_fmt = DRM_FORMAT_BGRA8888,
  297. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_ABGR8888,
  298. .swap = true,
  299. .sf = scaling_factors_888,
  300. }, {
  301. .drm_fmt = DRM_FORMAT_BGR888,
  302. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGB888,
  303. .swap = false,
  304. .sf = scaling_factors_888,
  305. }, {
  306. .drm_fmt = DRM_FORMAT_RGB888,
  307. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_BGR888,
  308. .swap = false,
  309. .sf = scaling_factors_888,
  310. }, {
  311. .drm_fmt = DRM_FORMAT_RGBA5551,
  312. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGBA5551,
  313. .swap = false,
  314. .sf = scaling_factors_555,
  315. }, {
  316. .drm_fmt = DRM_FORMAT_BGRA5551,
  317. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGBA5551,
  318. .swap = true,
  319. .sf = scaling_factors_555,
  320. }, {
  321. .drm_fmt = DRM_FORMAT_RGBA4444,
  322. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGBA4444,
  323. .swap = false,
  324. .sf = scaling_factors_444,
  325. }, {
  326. .drm_fmt = DRM_FORMAT_BGRA4444,
  327. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGBA4444,
  328. .swap = true,
  329. .sf = scaling_factors_444,
  330. }, {
  331. .drm_fmt = DRM_FORMAT_RGB565,
  332. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGB565,
  333. .swap = false,
  334. .sf = scaling_factors_565,
  335. }, {
  336. .drm_fmt = DRM_FORMAT_BGR565,
  337. .buf_fmt = ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_RGB565,
  338. .swap = true,
  339. .sf = scaling_factors_565,
  340. },
  341. };
  342. /* List of live video layer formats */
  343. static const struct zynqmp_disp_format avbuf_live_fmts[] = {
  344. {
  345. .drm_fmt = DRM_FORMAT_RGB565,
  346. .bus_fmt = MEDIA_BUS_FMT_RGB666_1X18,
  347. .buf_fmt = ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_BPC_6 |
  348. ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_FMT_RGB,
  349. .sf = scaling_factors_666,
  350. }, {
  351. .drm_fmt = DRM_FORMAT_RGB888,
  352. .bus_fmt = MEDIA_BUS_FMT_RGB888_1X24,
  353. .buf_fmt = ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_BPC_8 |
  354. ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_FMT_RGB,
  355. .sf = scaling_factors_888,
  356. }, {
  357. .drm_fmt = DRM_FORMAT_YUV422,
  358. .bus_fmt = MEDIA_BUS_FMT_UYVY8_1X16,
  359. .buf_fmt = ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_BPC_8 |
  360. ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_FMT_YUV422,
  361. .sf = scaling_factors_888,
  362. }, {
  363. .drm_fmt = DRM_FORMAT_YUV444,
  364. .bus_fmt = MEDIA_BUS_FMT_VUY8_1X24,
  365. .buf_fmt = ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_BPC_8 |
  366. ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_FMT_YUV444,
  367. .sf = scaling_factors_888,
  368. }, {
  369. .drm_fmt = DRM_FORMAT_P210,
  370. .bus_fmt = MEDIA_BUS_FMT_UYVY10_1X20,
  371. .buf_fmt = ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_BPC_10 |
  372. ZYNQMP_DISP_AV_BUF_LIVE_CONFIG_FMT_YUV422,
  373. .sf = scaling_factors_101010,
  374. },
  375. };
  376. static u32 zynqmp_disp_avbuf_read(struct zynqmp_disp *disp, int reg)
  377. {
  378. return readl(disp->avbuf + reg);
  379. }
  380. static void zynqmp_disp_avbuf_write(struct zynqmp_disp *disp, int reg, u32 val)
  381. {
  382. writel(val, disp->avbuf + reg);
  383. }
  384. static bool zynqmp_disp_layer_is_video(const struct zynqmp_disp_layer *layer)
  385. {
  386. return layer->id == ZYNQMP_DPSUB_LAYER_VID;
  387. }
  388. /**
  389. * zynqmp_disp_avbuf_set_format - Set the input format for a layer
  390. * @disp: Display controller
  391. * @layer: The layer
  392. * @fmt: The format information
  393. *
  394. * Set the video buffer manager format for @layer to @fmt.
  395. */
  396. static void zynqmp_disp_avbuf_set_format(struct zynqmp_disp *disp,
  397. struct zynqmp_disp_layer *layer,
  398. const struct zynqmp_disp_format *fmt)
  399. {
  400. unsigned int i;
  401. u32 val, reg;
  402. layer->disp_fmt = fmt;
  403. if (layer->mode == ZYNQMP_DPSUB_LAYER_NONLIVE) {
  404. reg = ZYNQMP_DISP_AV_BUF_FMT;
  405. val = zynqmp_disp_avbuf_read(disp, ZYNQMP_DISP_AV_BUF_FMT);
  406. val &= zynqmp_disp_layer_is_video(layer)
  407. ? ~ZYNQMP_DISP_AV_BUF_FMT_NL_VID_MASK
  408. : ~ZYNQMP_DISP_AV_BUF_FMT_NL_GFX_MASK;
  409. val |= fmt->buf_fmt;
  410. zynqmp_disp_avbuf_write(disp, reg, val);
  411. } else {
  412. reg = zynqmp_disp_layer_is_video(layer)
  413. ? ZYNQMP_DISP_AV_BUF_LIVE_VID_CONFIG
  414. : ZYNQMP_DISP_AV_BUF_LIVE_GFX_CONFIG;
  415. val = fmt->buf_fmt;
  416. zynqmp_disp_avbuf_write(disp, reg, val);
  417. }
  418. for (i = 0; i < ZYNQMP_DISP_AV_BUF_NUM_SF; i++) {
  419. reg = zynqmp_disp_layer_is_video(layer)
  420. ? ZYNQMP_DISP_AV_BUF_VID_COMP_SF(i)
  421. : ZYNQMP_DISP_AV_BUF_GFX_COMP_SF(i);
  422. zynqmp_disp_avbuf_write(disp, reg, fmt->sf[i]);
  423. }
  424. }
  425. /**
  426. * zynqmp_disp_avbuf_set_clocks_sources - Set the clocks sources
  427. * @disp: Display controller
  428. * @video_from_ps: True if the video clock originates from the PS
  429. * @audio_from_ps: True if the audio clock originates from the PS
  430. * @timings_internal: True if video timings are generated internally
  431. *
  432. * Set the source for the video and audio clocks, as well as for the video
  433. * timings. Clocks can originate from the PS or PL, and timings can be
  434. * generated internally or externally.
  435. */
  436. static void
  437. zynqmp_disp_avbuf_set_clocks_sources(struct zynqmp_disp *disp,
  438. bool video_from_ps, bool audio_from_ps,
  439. bool timings_internal)
  440. {
  441. u32 val = 0;
  442. if (video_from_ps)
  443. val |= ZYNQMP_DISP_AV_BUF_CLK_SRC_VID_FROM_PS;
  444. if (audio_from_ps)
  445. val |= ZYNQMP_DISP_AV_BUF_CLK_SRC_AUD_FROM_PS;
  446. if (timings_internal)
  447. val |= ZYNQMP_DISP_AV_BUF_CLK_SRC_VID_INTERNAL_TIMING;
  448. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_CLK_SRC, val);
  449. }
  450. /**
  451. * zynqmp_disp_avbuf_enable_channels - Enable buffer channels
  452. * @disp: Display controller
  453. *
  454. * Enable all (video and audio) buffer channels.
  455. */
  456. static void zynqmp_disp_avbuf_enable_channels(struct zynqmp_disp *disp)
  457. {
  458. unsigned int i;
  459. u32 val;
  460. val = ZYNQMP_DISP_AV_BUF_CHBUF_EN |
  461. (ZYNQMP_DISP_AV_BUF_CHBUF_BURST_LEN_MAX <<
  462. ZYNQMP_DISP_AV_BUF_CHBUF_BURST_LEN_SHIFT);
  463. for (i = 0; i < ZYNQMP_DISP_AV_BUF_NUM_VID_GFX_BUFFERS; i++)
  464. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_CHBUF(i),
  465. val);
  466. val = ZYNQMP_DISP_AV_BUF_CHBUF_EN |
  467. (ZYNQMP_DISP_AV_BUF_CHBUF_BURST_LEN_AUD_MAX <<
  468. ZYNQMP_DISP_AV_BUF_CHBUF_BURST_LEN_SHIFT);
  469. for (; i < ZYNQMP_DISP_AV_BUF_NUM_BUFFERS; i++)
  470. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_CHBUF(i),
  471. val);
  472. }
  473. /**
  474. * zynqmp_disp_avbuf_disable_channels - Disable buffer channels
  475. * @disp: Display controller
  476. *
  477. * Disable all (video and audio) buffer channels.
  478. */
  479. static void zynqmp_disp_avbuf_disable_channels(struct zynqmp_disp *disp)
  480. {
  481. unsigned int i;
  482. for (i = 0; i < ZYNQMP_DISP_AV_BUF_NUM_BUFFERS; i++)
  483. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_CHBUF(i),
  484. ZYNQMP_DISP_AV_BUF_CHBUF_FLUSH);
  485. }
  486. /**
  487. * zynqmp_disp_avbuf_enable_audio - Enable audio
  488. * @disp: Display controller
  489. *
  490. * Enable all audio buffers with a non-live (memory) source.
  491. */
  492. static void zynqmp_disp_avbuf_enable_audio(struct zynqmp_disp *disp)
  493. {
  494. u32 val;
  495. val = zynqmp_disp_avbuf_read(disp, ZYNQMP_DISP_AV_BUF_OUTPUT);
  496. val &= ~ZYNQMP_DISP_AV_BUF_OUTPUT_AUD1_MASK;
  497. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_AUD1_MEM;
  498. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_AUD2_EN;
  499. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_OUTPUT, val);
  500. }
  501. /**
  502. * zynqmp_disp_avbuf_disable_audio - Disable audio
  503. * @disp: Display controller
  504. *
  505. * Disable all audio buffers.
  506. */
  507. static void zynqmp_disp_avbuf_disable_audio(struct zynqmp_disp *disp)
  508. {
  509. u32 val;
  510. val = zynqmp_disp_avbuf_read(disp, ZYNQMP_DISP_AV_BUF_OUTPUT);
  511. val &= ~ZYNQMP_DISP_AV_BUF_OUTPUT_AUD1_MASK;
  512. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_AUD1_DISABLE;
  513. val &= ~ZYNQMP_DISP_AV_BUF_OUTPUT_AUD2_EN;
  514. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_OUTPUT, val);
  515. }
  516. /**
  517. * zynqmp_disp_avbuf_enable_video - Enable a video layer
  518. * @disp: Display controller
  519. * @layer: The layer
  520. *
  521. * Enable the video/graphics buffer for @layer.
  522. */
  523. static void zynqmp_disp_avbuf_enable_video(struct zynqmp_disp *disp,
  524. struct zynqmp_disp_layer *layer)
  525. {
  526. u32 val;
  527. val = zynqmp_disp_avbuf_read(disp, ZYNQMP_DISP_AV_BUF_OUTPUT);
  528. if (zynqmp_disp_layer_is_video(layer)) {
  529. val &= ~ZYNQMP_DISP_AV_BUF_OUTPUT_VID1_MASK;
  530. if (layer->mode == ZYNQMP_DPSUB_LAYER_NONLIVE)
  531. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_VID1_MEM;
  532. else
  533. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_VID1_LIVE;
  534. } else {
  535. val &= ~ZYNQMP_DISP_AV_BUF_OUTPUT_VID2_MASK;
  536. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_VID2_MEM;
  537. if (layer->mode == ZYNQMP_DPSUB_LAYER_NONLIVE)
  538. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_VID2_MEM;
  539. else
  540. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_VID2_LIVE;
  541. }
  542. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_OUTPUT, val);
  543. }
  544. /**
  545. * zynqmp_disp_avbuf_disable_video - Disable a video layer
  546. * @disp: Display controller
  547. * @layer: The layer
  548. *
  549. * Disable the video/graphics buffer for @layer.
  550. */
  551. static void zynqmp_disp_avbuf_disable_video(struct zynqmp_disp *disp,
  552. struct zynqmp_disp_layer *layer)
  553. {
  554. u32 val;
  555. val = zynqmp_disp_avbuf_read(disp, ZYNQMP_DISP_AV_BUF_OUTPUT);
  556. if (zynqmp_disp_layer_is_video(layer)) {
  557. val &= ~ZYNQMP_DISP_AV_BUF_OUTPUT_VID1_MASK;
  558. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_VID1_NONE;
  559. } else {
  560. val &= ~ZYNQMP_DISP_AV_BUF_OUTPUT_VID2_MASK;
  561. val |= ZYNQMP_DISP_AV_BUF_OUTPUT_VID2_DISABLE;
  562. }
  563. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_OUTPUT, val);
  564. }
  565. /**
  566. * zynqmp_disp_avbuf_enable - Enable the video pipe
  567. * @disp: Display controller
  568. *
  569. * De-assert the video pipe reset.
  570. */
  571. static void zynqmp_disp_avbuf_enable(struct zynqmp_disp *disp)
  572. {
  573. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_SRST_REG, 0);
  574. }
  575. /**
  576. * zynqmp_disp_avbuf_disable - Disable the video pipe
  577. * @disp: Display controller
  578. *
  579. * Assert the video pipe reset.
  580. */
  581. static void zynqmp_disp_avbuf_disable(struct zynqmp_disp *disp)
  582. {
  583. zynqmp_disp_avbuf_write(disp, ZYNQMP_DISP_AV_BUF_SRST_REG,
  584. ZYNQMP_DISP_AV_BUF_SRST_REG_VID_RST);
  585. }
  586. /* -----------------------------------------------------------------------------
  587. * Blender (Video Pipeline)
  588. */
  589. static void zynqmp_disp_blend_write(struct zynqmp_disp *disp, int reg, u32 val)
  590. {
  591. writel(val, disp->blend + reg);
  592. }
  593. /*
  594. * Colorspace conversion matrices.
  595. *
  596. * Hardcode RGB <-> YUV conversion to full-range SDTV for now.
  597. */
  598. static const u16 csc_zero_matrix[] = {
  599. 0x0, 0x0, 0x0,
  600. 0x0, 0x0, 0x0,
  601. 0x0, 0x0, 0x0
  602. };
  603. static const u16 csc_identity_matrix[] = {
  604. 0x1000, 0x0, 0x0,
  605. 0x0, 0x1000, 0x0,
  606. 0x0, 0x0, 0x1000
  607. };
  608. static const u32 csc_zero_offsets[] = {
  609. 0, 0, 0
  610. };
  611. static const u16 csc_rgb_to_sdtv_matrix[] = {
  612. 0x4c9, 0x864, 0x1d3,
  613. 0x7d4d, 0x7ab3, 0x800,
  614. 0x800, 0x794d, 0x7eb3
  615. };
  616. static const u32 csc_rgb_to_sdtv_offsets[] = {
  617. 0x0, 0x8000000, 0x8000000
  618. };
  619. static const u16 csc_sdtv_to_rgb_matrix[] = {
  620. 0x1000, 0x166f, 0x0,
  621. 0x1000, 0x7483, 0x7a7f,
  622. 0x1000, 0x0, 0x1c5a
  623. };
  624. static const u32 csc_sdtv_to_rgb_offsets[] = {
  625. 0x0, 0x1800, 0x1800
  626. };
  627. /**
  628. * zynqmp_disp_blend_set_output_format - Set the output format of the blender
  629. * @disp: Display controller
  630. * @format: Output format
  631. *
  632. * Set the output format of the blender to @format.
  633. */
  634. static void zynqmp_disp_blend_set_output_format(struct zynqmp_disp *disp,
  635. enum zynqmp_dpsub_format format)
  636. {
  637. static const unsigned int blend_output_fmts[] = {
  638. [ZYNQMP_DPSUB_FORMAT_RGB] = ZYNQMP_DISP_V_BLEND_OUTPUT_VID_FMT_RGB,
  639. [ZYNQMP_DPSUB_FORMAT_YCRCB444] = ZYNQMP_DISP_V_BLEND_OUTPUT_VID_FMT_YCBCR444,
  640. [ZYNQMP_DPSUB_FORMAT_YCRCB422] = ZYNQMP_DISP_V_BLEND_OUTPUT_VID_FMT_YCBCR422
  641. | ZYNQMP_DISP_V_BLEND_OUTPUT_VID_FMT_EN_DOWNSAMPLE,
  642. [ZYNQMP_DPSUB_FORMAT_YONLY] = ZYNQMP_DISP_V_BLEND_OUTPUT_VID_FMT_YONLY,
  643. };
  644. u32 fmt = blend_output_fmts[format];
  645. const u16 *coeffs;
  646. const u32 *offsets;
  647. unsigned int i;
  648. zynqmp_disp_blend_write(disp, ZYNQMP_DISP_V_BLEND_OUTPUT_VID_FMT, fmt);
  649. if (fmt == ZYNQMP_DISP_V_BLEND_OUTPUT_VID_FMT_RGB) {
  650. coeffs = csc_identity_matrix;
  651. offsets = csc_zero_offsets;
  652. } else {
  653. coeffs = csc_rgb_to_sdtv_matrix;
  654. offsets = csc_rgb_to_sdtv_offsets;
  655. }
  656. for (i = 0; i < ZYNQMP_DISP_V_BLEND_NUM_COEFF; i++)
  657. zynqmp_disp_blend_write(disp,
  658. ZYNQMP_DISP_V_BLEND_RGB2YCBCR_COEFF(i),
  659. coeffs[i]);
  660. for (i = 0; i < ZYNQMP_DISP_V_BLEND_NUM_OFFSET; i++)
  661. zynqmp_disp_blend_write(disp,
  662. ZYNQMP_DISP_V_BLEND_OUTCSC_OFFSET(i),
  663. offsets[i]);
  664. }
  665. /**
  666. * zynqmp_disp_blend_set_bg_color - Set the background color
  667. * @disp: Display controller
  668. * @rcr: Red/Cr color component
  669. * @gy: Green/Y color component
  670. * @bcb: Blue/Cb color component
  671. *
  672. * Set the background color to (@rcr, @gy, @bcb), corresponding to the R, G and
  673. * B or Cr, Y and Cb components respectively depending on the selected output
  674. * format.
  675. */
  676. static void zynqmp_disp_blend_set_bg_color(struct zynqmp_disp *disp,
  677. u32 rcr, u32 gy, u32 bcb)
  678. {
  679. zynqmp_disp_blend_write(disp, ZYNQMP_DISP_V_BLEND_BG_CLR_0, rcr);
  680. zynqmp_disp_blend_write(disp, ZYNQMP_DISP_V_BLEND_BG_CLR_1, gy);
  681. zynqmp_disp_blend_write(disp, ZYNQMP_DISP_V_BLEND_BG_CLR_2, bcb);
  682. }
  683. /**
  684. * zynqmp_disp_blend_set_global_alpha - Configure global alpha blending
  685. * @disp: Display controller
  686. * @enable: True to enable global alpha blending
  687. * @alpha: Global alpha value (ignored if @enabled is false)
  688. */
  689. void zynqmp_disp_blend_set_global_alpha(struct zynqmp_disp *disp,
  690. bool enable, u32 alpha)
  691. {
  692. zynqmp_disp_blend_write(disp, ZYNQMP_DISP_V_BLEND_SET_GLOBAL_ALPHA,
  693. ZYNQMP_DISP_V_BLEND_SET_GLOBAL_ALPHA_VALUE(alpha) |
  694. (enable ? ZYNQMP_DISP_V_BLEND_SET_GLOBAL_ALPHA_EN : 0));
  695. }
  696. /**
  697. * zynqmp_disp_blend_layer_set_csc - Configure colorspace conversion for layer
  698. * @disp: Display controller
  699. * @layer: The layer
  700. * @coeffs: Colorspace conversion matrix
  701. * @offsets: Colorspace conversion offsets
  702. *
  703. * Configure the input colorspace conversion matrix and offsets for the @layer.
  704. * Columns of the matrix are automatically swapped based on the input format to
  705. * handle RGB and YCrCb components permutations.
  706. */
  707. static void zynqmp_disp_blend_layer_set_csc(struct zynqmp_disp *disp,
  708. struct zynqmp_disp_layer *layer,
  709. const u16 *coeffs,
  710. const u32 *offsets)
  711. {
  712. unsigned int swap[3] = { 0, 1, 2 };
  713. unsigned int reg;
  714. unsigned int i;
  715. if (layer->disp_fmt->swap) {
  716. if (layer->drm_fmt->is_yuv) {
  717. /* Swap U and V. */
  718. swap[1] = 2;
  719. swap[2] = 1;
  720. } else {
  721. /* Swap R and B. */
  722. swap[0] = 2;
  723. swap[2] = 0;
  724. }
  725. }
  726. if (zynqmp_disp_layer_is_video(layer))
  727. reg = ZYNQMP_DISP_V_BLEND_IN1CSC_COEFF(0);
  728. else
  729. reg = ZYNQMP_DISP_V_BLEND_IN2CSC_COEFF(0);
  730. for (i = 0; i < ZYNQMP_DISP_V_BLEND_NUM_COEFF; i += 3, reg += 12) {
  731. zynqmp_disp_blend_write(disp, reg + 0, coeffs[i + swap[0]]);
  732. zynqmp_disp_blend_write(disp, reg + 4, coeffs[i + swap[1]]);
  733. zynqmp_disp_blend_write(disp, reg + 8, coeffs[i + swap[2]]);
  734. }
  735. if (zynqmp_disp_layer_is_video(layer))
  736. reg = ZYNQMP_DISP_V_BLEND_IN1CSC_OFFSET(0);
  737. else
  738. reg = ZYNQMP_DISP_V_BLEND_IN2CSC_OFFSET(0);
  739. for (i = 0; i < ZYNQMP_DISP_V_BLEND_NUM_OFFSET; i++)
  740. zynqmp_disp_blend_write(disp, reg + i * 4, offsets[i]);
  741. }
  742. /**
  743. * zynqmp_disp_blend_layer_enable - Enable a layer
  744. * @disp: Display controller
  745. * @layer: The layer
  746. */
  747. static void zynqmp_disp_blend_layer_enable(struct zynqmp_disp *disp,
  748. struct zynqmp_disp_layer *layer)
  749. {
  750. const u16 *coeffs;
  751. const u32 *offsets;
  752. u32 val;
  753. val = (layer->drm_fmt->is_yuv ?
  754. 0 : ZYNQMP_DISP_V_BLEND_LAYER_CONTROL_RGB) |
  755. (layer->drm_fmt->hsub > 1 ?
  756. ZYNQMP_DISP_V_BLEND_LAYER_CONTROL_EN_US : 0);
  757. zynqmp_disp_blend_write(disp,
  758. ZYNQMP_DISP_V_BLEND_LAYER_CONTROL(layer->id),
  759. val);
  760. if (layer->drm_fmt->is_yuv) {
  761. coeffs = csc_sdtv_to_rgb_matrix;
  762. offsets = csc_sdtv_to_rgb_offsets;
  763. } else {
  764. coeffs = csc_identity_matrix;
  765. offsets = csc_zero_offsets;
  766. }
  767. zynqmp_disp_blend_layer_set_csc(disp, layer, coeffs, offsets);
  768. }
  769. /**
  770. * zynqmp_disp_blend_layer_disable - Disable a layer
  771. * @disp: Display controller
  772. * @layer: The layer
  773. */
  774. static void zynqmp_disp_blend_layer_disable(struct zynqmp_disp *disp,
  775. struct zynqmp_disp_layer *layer)
  776. {
  777. zynqmp_disp_blend_write(disp,
  778. ZYNQMP_DISP_V_BLEND_LAYER_CONTROL(layer->id),
  779. 0);
  780. zynqmp_disp_blend_layer_set_csc(disp, layer, csc_zero_matrix,
  781. csc_zero_offsets);
  782. }
  783. /* -----------------------------------------------------------------------------
  784. * ZynqMP Display Layer & DRM Plane
  785. */
  786. /**
  787. * zynqmp_disp_layer_find_format - Find format information for a DRM format
  788. * @layer: The layer
  789. * @drm_fmt: DRM format to search
  790. *
  791. * Search display subsystem format information corresponding to the given DRM
  792. * format @drm_fmt for the @layer, and return a pointer to the format
  793. * descriptor.
  794. *
  795. * Return: A pointer to the format descriptor if found, NULL otherwise
  796. */
  797. static const struct zynqmp_disp_format *
  798. zynqmp_disp_layer_find_format(struct zynqmp_disp_layer *layer,
  799. u32 drm_fmt)
  800. {
  801. unsigned int i;
  802. for (i = 0; i < layer->info->num_formats; i++) {
  803. if (layer->info->formats[i].drm_fmt == drm_fmt)
  804. return &layer->info->formats[i];
  805. }
  806. return NULL;
  807. }
  808. /**
  809. * zynqmp_disp_layer_find_live_format - Find format information for given
  810. * media bus format
  811. * @layer: The layer
  812. * @media_bus_format: Media bus format to search
  813. *
  814. * Search display subsystem format information corresponding to the given media
  815. * bus format @media_bus_format for the @layer, and return a pointer to the
  816. * format descriptor.
  817. *
  818. * Return: A pointer to the format descriptor if found, NULL otherwise
  819. */
  820. static const struct zynqmp_disp_format *
  821. zynqmp_disp_layer_find_live_format(struct zynqmp_disp_layer *layer,
  822. u32 media_bus_format)
  823. {
  824. unsigned int i;
  825. for (i = 0; i < layer->info->num_formats; i++)
  826. if (layer->info->formats[i].bus_fmt == media_bus_format)
  827. return &layer->info->formats[i];
  828. return NULL;
  829. }
  830. /**
  831. * zynqmp_disp_layer_drm_formats - Return the DRM formats supported by the layer
  832. * @layer: The layer
  833. * @num_formats: Pointer to the returned number of formats
  834. *
  835. * NOTE: This function doesn't make sense for live video layers and will
  836. * always return an empty list in such cases. zynqmp_disp_live_layer_formats()
  837. * should be used to query a list of media bus formats supported by the live
  838. * video input layer.
  839. *
  840. * Return: A newly allocated u32 array that stores all the DRM formats
  841. * supported by the layer. The number of formats in the array is returned
  842. * through the num_formats argument.
  843. */
  844. u32 *zynqmp_disp_layer_drm_formats(struct zynqmp_disp_layer *layer,
  845. unsigned int *num_formats)
  846. {
  847. unsigned int i;
  848. u32 *formats;
  849. if (WARN_ON(layer->mode != ZYNQMP_DPSUB_LAYER_NONLIVE)) {
  850. *num_formats = 0;
  851. return NULL;
  852. }
  853. formats = kcalloc(layer->info->num_formats, sizeof(*formats),
  854. GFP_KERNEL);
  855. if (!formats) {
  856. *num_formats = 0;
  857. return NULL;
  858. }
  859. for (i = 0; i < layer->info->num_formats; ++i)
  860. formats[i] = layer->info->formats[i].drm_fmt;
  861. *num_formats = layer->info->num_formats;
  862. return formats;
  863. }
  864. /**
  865. * zynqmp_disp_live_layer_formats - Return the media bus formats supported by
  866. * the live video layer
  867. * @layer: The layer
  868. * @num_formats: Pointer to the returned number of formats
  869. *
  870. * NOTE: This function should be used only for live video input layers.
  871. *
  872. * Return: A newly allocated u32 array of media bus formats supported by the
  873. * layer. The number of formats in the array is returned through the
  874. * @num_formats argument.
  875. */
  876. u32 *zynqmp_disp_live_layer_formats(struct zynqmp_disp_layer *layer,
  877. unsigned int *num_formats)
  878. {
  879. unsigned int i;
  880. u32 *formats;
  881. if (WARN_ON(layer->mode != ZYNQMP_DPSUB_LAYER_LIVE)) {
  882. *num_formats = 0;
  883. return NULL;
  884. }
  885. formats = kcalloc(layer->info->num_formats, sizeof(*formats),
  886. GFP_KERNEL);
  887. if (!formats) {
  888. *num_formats = 0;
  889. return NULL;
  890. }
  891. for (i = 0; i < layer->info->num_formats; ++i)
  892. formats[i] = layer->info->formats[i].bus_fmt;
  893. *num_formats = layer->info->num_formats;
  894. return formats;
  895. }
  896. /**
  897. * zynqmp_disp_layer_enable - Enable a layer
  898. * @layer: The layer
  899. *
  900. * Enable the @layer in the audio/video buffer manager and the blender. DMA
  901. * channels are started separately by zynqmp_disp_layer_update().
  902. */
  903. void zynqmp_disp_layer_enable(struct zynqmp_disp_layer *layer)
  904. {
  905. zynqmp_disp_avbuf_enable_video(layer->disp, layer);
  906. zynqmp_disp_blend_layer_enable(layer->disp, layer);
  907. }
  908. /**
  909. * zynqmp_disp_layer_disable - Disable the layer
  910. * @layer: The layer
  911. *
  912. * Disable the layer by stopping its DMA channels and disabling it in the
  913. * audio/video buffer manager and the blender.
  914. */
  915. void zynqmp_disp_layer_disable(struct zynqmp_disp_layer *layer)
  916. {
  917. unsigned int i;
  918. if (layer->mode == ZYNQMP_DPSUB_LAYER_NONLIVE) {
  919. for (i = 0; i < layer->drm_fmt->num_planes; i++)
  920. dmaengine_terminate_sync(layer->dmas[i].chan);
  921. }
  922. zynqmp_disp_avbuf_disable_video(layer->disp, layer);
  923. zynqmp_disp_blend_layer_disable(layer->disp, layer);
  924. }
  925. /**
  926. * zynqmp_disp_layer_set_format - Set the layer format
  927. * @layer: The layer
  928. * @info: The format info
  929. *
  930. * NOTE: Use zynqmp_disp_layer_set_live_format() to set media bus format for
  931. * live video layers.
  932. *
  933. * Set the format for @layer to @info. The layer must be disabled.
  934. */
  935. void zynqmp_disp_layer_set_format(struct zynqmp_disp_layer *layer,
  936. const struct drm_format_info *info)
  937. {
  938. unsigned int i;
  939. if (WARN_ON(layer->mode != ZYNQMP_DPSUB_LAYER_NONLIVE))
  940. return;
  941. layer->disp_fmt = zynqmp_disp_layer_find_format(layer, info->format);
  942. if (WARN_ON(!layer->disp_fmt))
  943. return;
  944. layer->drm_fmt = info;
  945. zynqmp_disp_avbuf_set_format(layer->disp, layer, layer->disp_fmt);
  946. /*
  947. * Set pconfig for each DMA channel to indicate they're part of a
  948. * video group.
  949. */
  950. for (i = 0; i < info->num_planes; i++) {
  951. struct zynqmp_disp_layer_dma *dma = &layer->dmas[i];
  952. struct xilinx_dpdma_peripheral_config pconfig = {
  953. .video_group = true,
  954. };
  955. struct dma_slave_config config = {
  956. .direction = DMA_MEM_TO_DEV,
  957. .peripheral_config = &pconfig,
  958. .peripheral_size = sizeof(pconfig),
  959. };
  960. dmaengine_slave_config(dma->chan, &config);
  961. }
  962. }
  963. /**
  964. * zynqmp_disp_layer_set_live_format - Set the live video layer format
  965. * @layer: The layer
  966. * @media_bus_format: Media bus format to set
  967. *
  968. * NOTE: This function should not be used to set format for non-live video
  969. * layer. Use zynqmp_disp_layer_set_format() instead.
  970. *
  971. * Set the display format for the live @layer. The layer must be disabled.
  972. */
  973. void zynqmp_disp_layer_set_live_format(struct zynqmp_disp_layer *layer,
  974. u32 media_bus_format)
  975. {
  976. if (WARN_ON(layer->mode != ZYNQMP_DPSUB_LAYER_LIVE))
  977. return;
  978. layer->disp_fmt = zynqmp_disp_layer_find_live_format(layer,
  979. media_bus_format);
  980. if (WARN_ON(!layer->disp_fmt))
  981. return;
  982. zynqmp_disp_avbuf_set_format(layer->disp, layer, layer->disp_fmt);
  983. layer->drm_fmt = drm_format_info(layer->disp_fmt->drm_fmt);
  984. }
  985. /**
  986. * zynqmp_disp_layer_update - Update the layer framebuffer
  987. * @layer: The layer
  988. * @state: The plane state
  989. *
  990. * Update the framebuffer for the layer by issuing a new DMA engine transaction
  991. * for the new framebuffer.
  992. *
  993. * Return: 0 on success, or the DMA descriptor failure error otherwise
  994. */
  995. int zynqmp_disp_layer_update(struct zynqmp_disp_layer *layer,
  996. struct drm_plane_state *state)
  997. {
  998. const struct drm_format_info *info = layer->drm_fmt;
  999. unsigned int i;
  1000. if (layer->mode == ZYNQMP_DPSUB_LAYER_LIVE)
  1001. return 0;
  1002. for (i = 0; i < info->num_planes; i++) {
  1003. unsigned int width = state->crtc_w / (i ? info->hsub : 1);
  1004. unsigned int height = state->crtc_h / (i ? info->vsub : 1);
  1005. struct zynqmp_disp_layer_dma *dma = &layer->dmas[i];
  1006. struct dma_async_tx_descriptor *desc;
  1007. dma_addr_t dma_addr;
  1008. dma_addr = drm_fb_dma_get_gem_addr(state->fb, state, i);
  1009. dma->xt.numf = height;
  1010. dma->sgl.size = width * info->cpp[i];
  1011. dma->sgl.icg = state->fb->pitches[i] - dma->sgl.size;
  1012. dma->xt.src_start = dma_addr;
  1013. dma->xt.frame_size = 1;
  1014. dma->xt.dir = DMA_MEM_TO_DEV;
  1015. dma->xt.src_sgl = true;
  1016. dma->xt.dst_sgl = false;
  1017. desc = dmaengine_prep_interleaved_dma(dma->chan, &dma->xt,
  1018. DMA_CTRL_ACK |
  1019. DMA_PREP_REPEAT |
  1020. DMA_PREP_LOAD_EOT);
  1021. if (!desc) {
  1022. dev_err(layer->disp->dev,
  1023. "failed to prepare DMA descriptor\n");
  1024. return -ENOMEM;
  1025. }
  1026. dmaengine_submit(desc);
  1027. dma_async_issue_pending(dma->chan);
  1028. }
  1029. return 0;
  1030. }
  1031. /**
  1032. * zynqmp_disp_layer_release_dma - Release DMA channels for a layer
  1033. * @disp: Display controller
  1034. * @layer: The layer
  1035. *
  1036. * Release the DMA channels associated with @layer.
  1037. */
  1038. static void zynqmp_disp_layer_release_dma(struct zynqmp_disp *disp,
  1039. struct zynqmp_disp_layer *layer)
  1040. {
  1041. unsigned int i;
  1042. if (!layer->info)
  1043. return;
  1044. for (i = 0; i < layer->info->num_channels; i++) {
  1045. struct zynqmp_disp_layer_dma *dma = &layer->dmas[i];
  1046. if (!dma->chan)
  1047. continue;
  1048. /* Make sure the channel is terminated before release. */
  1049. dmaengine_terminate_sync(dma->chan);
  1050. dma_release_channel(dma->chan);
  1051. }
  1052. }
  1053. /**
  1054. * zynqmp_disp_destroy_layers - Destroy all layers
  1055. * @disp: Display controller
  1056. */
  1057. static void zynqmp_disp_destroy_layers(struct zynqmp_disp *disp)
  1058. {
  1059. unsigned int i;
  1060. for (i = 0; i < ARRAY_SIZE(disp->layers); i++)
  1061. zynqmp_disp_layer_release_dma(disp, &disp->layers[i]);
  1062. }
  1063. /**
  1064. * zynqmp_disp_layer_request_dma - Request DMA channels for a layer
  1065. * @disp: Display controller
  1066. * @layer: The layer
  1067. *
  1068. * Request all DMA engine channels needed by @layer.
  1069. *
  1070. * Return: 0 on success, or the DMA channel request error otherwise
  1071. */
  1072. static int zynqmp_disp_layer_request_dma(struct zynqmp_disp *disp,
  1073. struct zynqmp_disp_layer *layer)
  1074. {
  1075. static const char * const dma_names[] = { "vid", "gfx" };
  1076. unsigned int i;
  1077. int ret;
  1078. for (i = 0; i < layer->info->num_channels; i++) {
  1079. struct zynqmp_disp_layer_dma *dma = &layer->dmas[i];
  1080. char dma_channel_name[16];
  1081. snprintf(dma_channel_name, sizeof(dma_channel_name),
  1082. "%s%u", dma_names[layer->id], i);
  1083. dma->chan = dma_request_chan(disp->dev, dma_channel_name);
  1084. if (IS_ERR(dma->chan)) {
  1085. ret = dev_err_probe(disp->dev, PTR_ERR(dma->chan),
  1086. "failed to request dma channel\n");
  1087. dma->chan = NULL;
  1088. return ret;
  1089. }
  1090. }
  1091. return 0;
  1092. }
  1093. /**
  1094. * zynqmp_disp_create_layers - Create and initialize all layers
  1095. * @disp: Display controller
  1096. *
  1097. * Return: 0 on success, or the DMA channel request error otherwise
  1098. */
  1099. static int zynqmp_disp_create_layers(struct zynqmp_disp *disp)
  1100. {
  1101. static const struct zynqmp_disp_layer_info layer_info[] = {
  1102. [ZYNQMP_DPSUB_LAYER_VID] = {
  1103. .formats = avbuf_vid_fmts,
  1104. .num_formats = ARRAY_SIZE(avbuf_vid_fmts),
  1105. .num_channels = 3,
  1106. },
  1107. [ZYNQMP_DPSUB_LAYER_GFX] = {
  1108. .formats = avbuf_gfx_fmts,
  1109. .num_formats = ARRAY_SIZE(avbuf_gfx_fmts),
  1110. .num_channels = 1,
  1111. },
  1112. };
  1113. static const struct zynqmp_disp_layer_info live_layer_info = {
  1114. .formats = avbuf_live_fmts,
  1115. .num_formats = ARRAY_SIZE(avbuf_live_fmts),
  1116. .num_channels = 0,
  1117. };
  1118. unsigned int i;
  1119. int ret;
  1120. for (i = 0; i < ARRAY_SIZE(disp->layers); i++) {
  1121. struct zynqmp_disp_layer *layer = &disp->layers[i];
  1122. layer->id = i;
  1123. layer->disp = disp;
  1124. /*
  1125. * For now assume dpsub works in either live or non-live mode for both layers.
  1126. * Hybrid mode is not supported yet.
  1127. */
  1128. if (disp->dpsub->dma_enabled) {
  1129. layer->mode = ZYNQMP_DPSUB_LAYER_NONLIVE;
  1130. layer->info = &layer_info[i];
  1131. } else {
  1132. layer->mode = ZYNQMP_DPSUB_LAYER_LIVE;
  1133. layer->info = &live_layer_info;
  1134. }
  1135. ret = zynqmp_disp_layer_request_dma(disp, layer);
  1136. if (ret)
  1137. goto err;
  1138. disp->dpsub->layers[i] = layer;
  1139. }
  1140. return 0;
  1141. err:
  1142. zynqmp_disp_destroy_layers(disp);
  1143. return ret;
  1144. }
  1145. /* -----------------------------------------------------------------------------
  1146. * ZynqMP Display
  1147. */
  1148. /**
  1149. * zynqmp_disp_enable - Enable the display controller
  1150. * @disp: Display controller
  1151. */
  1152. void zynqmp_disp_enable(struct zynqmp_disp *disp)
  1153. {
  1154. zynqmp_disp_blend_set_output_format(disp, ZYNQMP_DPSUB_FORMAT_RGB);
  1155. zynqmp_disp_blend_set_bg_color(disp, 0, 0, 0);
  1156. zynqmp_disp_avbuf_enable(disp);
  1157. /* Choose clock source based on the DT clock handle. */
  1158. zynqmp_disp_avbuf_set_clocks_sources(disp, disp->dpsub->vid_clk_from_ps,
  1159. disp->dpsub->aud_clk_from_ps,
  1160. disp->dpsub->vid_clk_from_ps);
  1161. zynqmp_disp_avbuf_enable_channels(disp);
  1162. zynqmp_disp_avbuf_enable_audio(disp);
  1163. }
  1164. /**
  1165. * zynqmp_disp_disable - Disable the display controller
  1166. * @disp: Display controller
  1167. */
  1168. void zynqmp_disp_disable(struct zynqmp_disp *disp)
  1169. {
  1170. zynqmp_disp_avbuf_disable_audio(disp);
  1171. zynqmp_disp_avbuf_disable_channels(disp);
  1172. zynqmp_disp_avbuf_disable(disp);
  1173. }
  1174. /**
  1175. * zynqmp_disp_setup_clock - Configure the display controller pixel clock rate
  1176. * @disp: Display controller
  1177. * @mode_clock: The pixel clock rate, in Hz
  1178. *
  1179. * Return: 0 on success, or a negative error clock otherwise
  1180. */
  1181. int zynqmp_disp_setup_clock(struct zynqmp_disp *disp,
  1182. unsigned long mode_clock)
  1183. {
  1184. unsigned long rate;
  1185. long diff;
  1186. int ret;
  1187. ret = clk_set_rate(disp->dpsub->vid_clk, mode_clock);
  1188. if (ret) {
  1189. dev_err(disp->dev, "failed to set the video clock\n");
  1190. return ret;
  1191. }
  1192. rate = clk_get_rate(disp->dpsub->vid_clk);
  1193. diff = rate - mode_clock;
  1194. if (abs(diff) > mode_clock / 20)
  1195. dev_info(disp->dev,
  1196. "requested pixel rate: %lu actual rate: %lu\n",
  1197. mode_clock, rate);
  1198. else
  1199. dev_dbg(disp->dev,
  1200. "requested pixel rate: %lu actual rate: %lu\n",
  1201. mode_clock, rate);
  1202. return 0;
  1203. }
  1204. /* -----------------------------------------------------------------------------
  1205. * Initialization & Cleanup
  1206. */
  1207. int zynqmp_disp_probe(struct zynqmp_dpsub *dpsub)
  1208. {
  1209. struct platform_device *pdev = to_platform_device(dpsub->dev);
  1210. struct zynqmp_disp *disp;
  1211. int ret;
  1212. disp = kzalloc_obj(*disp);
  1213. if (!disp)
  1214. return -ENOMEM;
  1215. disp->dev = &pdev->dev;
  1216. disp->dpsub = dpsub;
  1217. disp->blend = devm_platform_ioremap_resource_byname(pdev, "blend");
  1218. if (IS_ERR(disp->blend)) {
  1219. ret = PTR_ERR(disp->blend);
  1220. goto error;
  1221. }
  1222. disp->avbuf = devm_platform_ioremap_resource_byname(pdev, "av_buf");
  1223. if (IS_ERR(disp->avbuf)) {
  1224. ret = PTR_ERR(disp->avbuf);
  1225. goto error;
  1226. }
  1227. ret = zynqmp_disp_create_layers(disp);
  1228. if (ret)
  1229. goto error;
  1230. if (disp->dpsub->dma_enabled) {
  1231. struct zynqmp_disp_layer *layer;
  1232. layer = &disp->layers[ZYNQMP_DPSUB_LAYER_VID];
  1233. dpsub->dma_align = 1 << layer->dmas[0].chan->device->copy_align;
  1234. }
  1235. dpsub->disp = disp;
  1236. return 0;
  1237. error:
  1238. kfree(disp);
  1239. return ret;
  1240. }
  1241. void zynqmp_disp_remove(struct zynqmp_dpsub *dpsub)
  1242. {
  1243. struct zynqmp_disp *disp = dpsub->disp;
  1244. zynqmp_disp_destroy_layers(disp);
  1245. }