imx-pxp.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * i.MX Pixel Pipeline (PXP) mem-to-mem scaler/CSC/rotator driver
  4. *
  5. * Copyright (c) 2018 Pengutronix, Philipp Zabel
  6. *
  7. * based on vim2m
  8. *
  9. * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
  10. * Pawel Osciak, <[email protected]>
  11. * Marek Szyprowski, <[email protected]>
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/platform_device.h>
  24. #include <media/v4l2-mem2mem.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/v4l2-ctrls.h>
  28. #include <media/v4l2-event.h>
  29. #include <media/videobuf2-dma-contig.h>
  30. #include "imx-pxp.h"
  31. static unsigned int debug;
  32. module_param(debug, uint, 0644);
  33. MODULE_PARM_DESC(debug, "activates debug info");
  34. #define MIN_W 8
  35. #define MIN_H 8
  36. #define MAX_W 4096
  37. #define MAX_H 4096
  38. #define ALIGN_W 3 /* 8x8 pixel blocks */
  39. #define ALIGN_H 3
  40. /* Flags that indicate a format can be used for capture/output */
  41. #define MEM2MEM_CAPTURE (1 << 0)
  42. #define MEM2MEM_OUTPUT (1 << 1)
  43. #define MEM2MEM_NAME "pxp"
  44. /* Flags that indicate processing mode */
  45. #define MEM2MEM_HFLIP (1 << 0)
  46. #define MEM2MEM_VFLIP (1 << 1)
  47. #define dprintk(dev, fmt, arg...) \
  48. v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  49. struct pxp_fmt {
  50. u32 fourcc;
  51. int depth;
  52. /* Types the format can be used for */
  53. u32 types;
  54. };
  55. static struct pxp_fmt formats[] = {
  56. {
  57. .fourcc = V4L2_PIX_FMT_XBGR32,
  58. .depth = 32,
  59. /* Both capture and output format */
  60. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  61. }, {
  62. .fourcc = V4L2_PIX_FMT_ABGR32,
  63. .depth = 32,
  64. /* Capture-only format */
  65. .types = MEM2MEM_CAPTURE,
  66. }, {
  67. .fourcc = V4L2_PIX_FMT_BGR24,
  68. .depth = 24,
  69. .types = MEM2MEM_CAPTURE,
  70. }, {
  71. .fourcc = V4L2_PIX_FMT_RGB565,
  72. .depth = 16,
  73. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  74. }, {
  75. .fourcc = V4L2_PIX_FMT_RGB555,
  76. .depth = 16,
  77. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  78. }, {
  79. .fourcc = V4L2_PIX_FMT_RGB444,
  80. .depth = 16,
  81. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  82. }, {
  83. .fourcc = V4L2_PIX_FMT_VUYA32,
  84. .depth = 32,
  85. .types = MEM2MEM_CAPTURE,
  86. }, {
  87. .fourcc = V4L2_PIX_FMT_VUYX32,
  88. .depth = 32,
  89. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  90. }, {
  91. .fourcc = V4L2_PIX_FMT_UYVY,
  92. .depth = 16,
  93. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  94. }, {
  95. .fourcc = V4L2_PIX_FMT_YUYV,
  96. .depth = 16,
  97. /* Output-only format */
  98. .types = MEM2MEM_OUTPUT,
  99. }, {
  100. .fourcc = V4L2_PIX_FMT_VYUY,
  101. .depth = 16,
  102. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  103. }, {
  104. .fourcc = V4L2_PIX_FMT_YVYU,
  105. .depth = 16,
  106. .types = MEM2MEM_OUTPUT,
  107. }, {
  108. .fourcc = V4L2_PIX_FMT_GREY,
  109. .depth = 8,
  110. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  111. }, {
  112. .fourcc = V4L2_PIX_FMT_Y4,
  113. .depth = 4,
  114. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  115. }, {
  116. .fourcc = V4L2_PIX_FMT_NV16,
  117. .depth = 16,
  118. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  119. }, {
  120. .fourcc = V4L2_PIX_FMT_NV12,
  121. .depth = 12,
  122. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  123. }, {
  124. .fourcc = V4L2_PIX_FMT_NV21,
  125. .depth = 12,
  126. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  127. }, {
  128. .fourcc = V4L2_PIX_FMT_NV61,
  129. .depth = 16,
  130. .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
  131. }, {
  132. .fourcc = V4L2_PIX_FMT_YUV422P,
  133. .depth = 16,
  134. .types = MEM2MEM_OUTPUT,
  135. }, {
  136. .fourcc = V4L2_PIX_FMT_YUV420,
  137. .depth = 12,
  138. .types = MEM2MEM_OUTPUT,
  139. },
  140. };
  141. #define NUM_FORMATS ARRAY_SIZE(formats)
  142. /* Per-queue, driver-specific private data */
  143. struct pxp_q_data {
  144. unsigned int width;
  145. unsigned int height;
  146. unsigned int bytesperline;
  147. unsigned int sizeimage;
  148. unsigned int sequence;
  149. struct pxp_fmt *fmt;
  150. enum v4l2_ycbcr_encoding ycbcr_enc;
  151. enum v4l2_quantization quant;
  152. };
  153. enum {
  154. V4L2_M2M_SRC = 0,
  155. V4L2_M2M_DST = 1,
  156. };
  157. static struct pxp_fmt *find_format(struct v4l2_format *f)
  158. {
  159. struct pxp_fmt *fmt;
  160. unsigned int k;
  161. for (k = 0; k < NUM_FORMATS; k++) {
  162. fmt = &formats[k];
  163. if (fmt->fourcc == f->fmt.pix.pixelformat)
  164. break;
  165. }
  166. if (k == NUM_FORMATS)
  167. return NULL;
  168. return &formats[k];
  169. }
  170. struct pxp_dev {
  171. struct v4l2_device v4l2_dev;
  172. struct video_device vfd;
  173. struct clk *clk;
  174. void __iomem *mmio;
  175. atomic_t num_inst;
  176. struct mutex dev_mutex;
  177. spinlock_t irqlock;
  178. struct v4l2_m2m_dev *m2m_dev;
  179. };
  180. struct pxp_ctx {
  181. struct v4l2_fh fh;
  182. struct pxp_dev *dev;
  183. struct v4l2_ctrl_handler hdl;
  184. /* Abort requested by m2m */
  185. int aborting;
  186. /* Processing mode */
  187. int mode;
  188. u8 alpha_component;
  189. u8 rotation;
  190. enum v4l2_colorspace colorspace;
  191. enum v4l2_xfer_func xfer_func;
  192. /* Source and destination queue data */
  193. struct pxp_q_data q_data[2];
  194. };
  195. static inline struct pxp_ctx *file2ctx(struct file *file)
  196. {
  197. return container_of(file->private_data, struct pxp_ctx, fh);
  198. }
  199. static struct pxp_q_data *get_q_data(struct pxp_ctx *ctx,
  200. enum v4l2_buf_type type)
  201. {
  202. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  203. return &ctx->q_data[V4L2_M2M_SRC];
  204. else
  205. return &ctx->q_data[V4L2_M2M_DST];
  206. }
  207. static u32 pxp_v4l2_pix_fmt_to_ps_format(u32 v4l2_pix_fmt)
  208. {
  209. switch (v4l2_pix_fmt) {
  210. case V4L2_PIX_FMT_XBGR32: return BV_PXP_PS_CTRL_FORMAT__RGB888;
  211. case V4L2_PIX_FMT_RGB555: return BV_PXP_PS_CTRL_FORMAT__RGB555;
  212. case V4L2_PIX_FMT_RGB444: return BV_PXP_PS_CTRL_FORMAT__RGB444;
  213. case V4L2_PIX_FMT_RGB565: return BV_PXP_PS_CTRL_FORMAT__RGB565;
  214. case V4L2_PIX_FMT_VUYX32: return BV_PXP_PS_CTRL_FORMAT__YUV1P444;
  215. case V4L2_PIX_FMT_UYVY: return BV_PXP_PS_CTRL_FORMAT__UYVY1P422;
  216. case V4L2_PIX_FMT_YUYV: return BM_PXP_PS_CTRL_WB_SWAP |
  217. BV_PXP_PS_CTRL_FORMAT__UYVY1P422;
  218. case V4L2_PIX_FMT_VYUY: return BV_PXP_PS_CTRL_FORMAT__VYUY1P422;
  219. case V4L2_PIX_FMT_YVYU: return BM_PXP_PS_CTRL_WB_SWAP |
  220. BV_PXP_PS_CTRL_FORMAT__VYUY1P422;
  221. case V4L2_PIX_FMT_GREY: return BV_PXP_PS_CTRL_FORMAT__Y8;
  222. default:
  223. case V4L2_PIX_FMT_Y4: return BV_PXP_PS_CTRL_FORMAT__Y4;
  224. case V4L2_PIX_FMT_NV16: return BV_PXP_PS_CTRL_FORMAT__YUV2P422;
  225. case V4L2_PIX_FMT_NV12: return BV_PXP_PS_CTRL_FORMAT__YUV2P420;
  226. case V4L2_PIX_FMT_NV21: return BV_PXP_PS_CTRL_FORMAT__YVU2P420;
  227. case V4L2_PIX_FMT_NV61: return BV_PXP_PS_CTRL_FORMAT__YVU2P422;
  228. case V4L2_PIX_FMT_YUV422P: return BV_PXP_PS_CTRL_FORMAT__YUV422;
  229. case V4L2_PIX_FMT_YUV420: return BV_PXP_PS_CTRL_FORMAT__YUV420;
  230. }
  231. }
  232. static u32 pxp_v4l2_pix_fmt_to_out_format(u32 v4l2_pix_fmt)
  233. {
  234. switch (v4l2_pix_fmt) {
  235. case V4L2_PIX_FMT_XBGR32: return BV_PXP_OUT_CTRL_FORMAT__RGB888;
  236. case V4L2_PIX_FMT_ABGR32: return BV_PXP_OUT_CTRL_FORMAT__ARGB8888;
  237. case V4L2_PIX_FMT_BGR24: return BV_PXP_OUT_CTRL_FORMAT__RGB888P;
  238. /* Missing V4L2 pixel formats for ARGB1555 and ARGB4444 */
  239. case V4L2_PIX_FMT_RGB555: return BV_PXP_OUT_CTRL_FORMAT__RGB555;
  240. case V4L2_PIX_FMT_RGB444: return BV_PXP_OUT_CTRL_FORMAT__RGB444;
  241. case V4L2_PIX_FMT_RGB565: return BV_PXP_OUT_CTRL_FORMAT__RGB565;
  242. case V4L2_PIX_FMT_VUYA32:
  243. case V4L2_PIX_FMT_VUYX32: return BV_PXP_OUT_CTRL_FORMAT__YUV1P444;
  244. case V4L2_PIX_FMT_UYVY: return BV_PXP_OUT_CTRL_FORMAT__UYVY1P422;
  245. case V4L2_PIX_FMT_VYUY: return BV_PXP_OUT_CTRL_FORMAT__VYUY1P422;
  246. case V4L2_PIX_FMT_GREY: return BV_PXP_OUT_CTRL_FORMAT__Y8;
  247. default:
  248. case V4L2_PIX_FMT_Y4: return BV_PXP_OUT_CTRL_FORMAT__Y4;
  249. case V4L2_PIX_FMT_NV16: return BV_PXP_OUT_CTRL_FORMAT__YUV2P422;
  250. case V4L2_PIX_FMT_NV12: return BV_PXP_OUT_CTRL_FORMAT__YUV2P420;
  251. case V4L2_PIX_FMT_NV61: return BV_PXP_OUT_CTRL_FORMAT__YVU2P422;
  252. case V4L2_PIX_FMT_NV21: return BV_PXP_OUT_CTRL_FORMAT__YVU2P420;
  253. }
  254. }
  255. static bool pxp_v4l2_pix_fmt_is_yuv(u32 v4l2_pix_fmt)
  256. {
  257. switch (v4l2_pix_fmt) {
  258. case V4L2_PIX_FMT_VUYA32:
  259. case V4L2_PIX_FMT_VUYX32:
  260. case V4L2_PIX_FMT_UYVY:
  261. case V4L2_PIX_FMT_YUYV:
  262. case V4L2_PIX_FMT_VYUY:
  263. case V4L2_PIX_FMT_YVYU:
  264. case V4L2_PIX_FMT_NV16:
  265. case V4L2_PIX_FMT_NV12:
  266. case V4L2_PIX_FMT_NV61:
  267. case V4L2_PIX_FMT_NV21:
  268. case V4L2_PIX_FMT_YUV420:
  269. case V4L2_PIX_FMT_YUV422P:
  270. case V4L2_PIX_FMT_GREY:
  271. case V4L2_PIX_FMT_Y4:
  272. return true;
  273. default:
  274. return false;
  275. }
  276. }
  277. static void pxp_setup_csc(struct pxp_ctx *ctx)
  278. {
  279. struct pxp_dev *dev = ctx->dev;
  280. enum v4l2_ycbcr_encoding ycbcr_enc;
  281. enum v4l2_quantization quantization;
  282. if (pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_SRC].fmt->fourcc) &&
  283. !pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_DST].fmt->fourcc)) {
  284. /*
  285. * CSC1 YUV/YCbCr to RGB conversion is implemented as follows:
  286. *
  287. * |R| |C0 0 C1| |Y + Yoffset |
  288. * |G| = |C0 C3 C2| * |Cb + UVoffset|
  289. * |B| |C0 C4 0 | |Cr + UVoffset|
  290. *
  291. * Results are clamped to 0..255.
  292. *
  293. * BT.601 limited range:
  294. *
  295. * |R| |1.1644 0.0000 1.5960| |Y - 16 |
  296. * |G| = |1.1644 -0.3917 -0.8129| * |Cb - 128|
  297. * |B| |1.1644 2.0172 0.0000| |Cr - 128|
  298. */
  299. static const u32 csc1_coef_bt601_lim[3] = {
  300. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  301. BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
  302. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  303. BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
  304. BF_PXP_CSC1_COEF1_C1(0x198) | /* 1.5938 (-0.23 %) */
  305. BF_PXP_CSC1_COEF1_C4(0x204), /* 2.0156 (-0.16 %) */
  306. BF_PXP_CSC1_COEF2_C2(0x730) | /* -0.8125 (+0.04 %) */
  307. BF_PXP_CSC1_COEF2_C3(0x79c), /* -0.3906 (+0.11 %) */
  308. };
  309. /*
  310. * BT.601 full range:
  311. *
  312. * |R| |1.0000 0.0000 1.4020| |Y + 0 |
  313. * |G| = |1.0000 -0.3441 -0.7141| * |Cb - 128|
  314. * |B| |1.0000 1.7720 0.0000| |Cr - 128|
  315. */
  316. static const u32 csc1_coef_bt601_full[3] = {
  317. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  318. BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
  319. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  320. BF_PXP_CSC1_COEF0_Y_OFFSET(0),
  321. BF_PXP_CSC1_COEF1_C1(0x166) | /* 1.3984 (-0.36 %) */
  322. BF_PXP_CSC1_COEF1_C4(0x1c5), /* 1.7695 (-0.25 %) */
  323. BF_PXP_CSC1_COEF2_C2(0x74a) | /* -0.7109 (+0.32 %) */
  324. BF_PXP_CSC1_COEF2_C3(0x7a8), /* -0.3438 (+0.04 %) */
  325. };
  326. /*
  327. * Rec.709 limited range:
  328. *
  329. * |R| |1.1644 0.0000 1.7927| |Y - 16 |
  330. * |G| = |1.1644 -0.2132 -0.5329| * |Cb - 128|
  331. * |B| |1.1644 2.1124 0.0000| |Cr - 128|
  332. */
  333. static const u32 csc1_coef_rec709_lim[3] = {
  334. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  335. BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
  336. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  337. BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
  338. BF_PXP_CSC1_COEF1_C1(0x1ca) | /* 1.7891 (-0.37 %) */
  339. BF_PXP_CSC1_COEF1_C4(0x21c), /* 2.1094 (-0.30 %) */
  340. BF_PXP_CSC1_COEF2_C2(0x778) | /* -0.5312 (+0.16 %) */
  341. BF_PXP_CSC1_COEF2_C3(0x7ca), /* -0.2109 (+0.23 %) */
  342. };
  343. /*
  344. * Rec.709 full range:
  345. *
  346. * |R| |1.0000 0.0000 1.5748| |Y + 0 |
  347. * |G| = |1.0000 -0.1873 -0.4681| * |Cb - 128|
  348. * |B| |1.0000 1.8556 0.0000| |Cr - 128|
  349. */
  350. static const u32 csc1_coef_rec709_full[3] = {
  351. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  352. BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
  353. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  354. BF_PXP_CSC1_COEF0_Y_OFFSET(0),
  355. BF_PXP_CSC1_COEF1_C1(0x193) | /* 1.5742 (-0.06 %) */
  356. BF_PXP_CSC1_COEF1_C4(0x1db), /* 1.8555 (-0.01 %) */
  357. BF_PXP_CSC1_COEF2_C2(0x789) | /* -0.4648 (+0.33 %) */
  358. BF_PXP_CSC1_COEF2_C3(0x7d1), /* -0.1836 (+0.37 %) */
  359. };
  360. /*
  361. * BT.2020 limited range:
  362. *
  363. * |R| |1.1644 0.0000 1.6787| |Y - 16 |
  364. * |G| = |1.1644 -0.1874 -0.6505| * |Cb - 128|
  365. * |B| |1.1644 2.1418 0.0000| |Cr - 128|
  366. */
  367. static const u32 csc1_coef_bt2020_lim[3] = {
  368. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  369. BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
  370. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  371. BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
  372. BF_PXP_CSC1_COEF1_C1(0x1ad) | /* 1.6758 (-0.29 %) */
  373. BF_PXP_CSC1_COEF1_C4(0x224), /* 2.1406 (-0.11 %) */
  374. BF_PXP_CSC1_COEF2_C2(0x75a) | /* -0.6484 (+0.20 %) */
  375. BF_PXP_CSC1_COEF2_C3(0x7d1), /* -0.1836 (+0.38 %) */
  376. };
  377. /*
  378. * BT.2020 full range:
  379. *
  380. * |R| |1.0000 0.0000 1.4746| |Y + 0 |
  381. * |G| = |1.0000 -0.1646 -0.5714| * |Cb - 128|
  382. * |B| |1.0000 1.8814 0.0000| |Cr - 128|
  383. */
  384. static const u32 csc1_coef_bt2020_full[3] = {
  385. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  386. BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
  387. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  388. BF_PXP_CSC1_COEF0_Y_OFFSET(0),
  389. BF_PXP_CSC1_COEF1_C1(0x179) | /* 1.4727 (-0.19 %) */
  390. BF_PXP_CSC1_COEF1_C4(0x1e1), /* 1.8789 (-0.25 %) */
  391. BF_PXP_CSC1_COEF2_C2(0x76e) | /* -0.5703 (+0.11 %) */
  392. BF_PXP_CSC1_COEF2_C3(0x7d6), /* -0.1641 (+0.05 %) */
  393. };
  394. /*
  395. * SMPTE 240m limited range:
  396. *
  397. * |R| |1.1644 0.0000 1.7937| |Y - 16 |
  398. * |G| = |1.1644 -0.2565 -0.5427| * |Cb - 128|
  399. * |B| |1.1644 2.0798 0.0000| |Cr - 128|
  400. */
  401. static const u32 csc1_coef_smpte240m_lim[3] = {
  402. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  403. BF_PXP_CSC1_COEF0_C0(0x12a) | /* 1.1641 (-0.03 %) */
  404. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  405. BF_PXP_CSC1_COEF0_Y_OFFSET(-16),
  406. BF_PXP_CSC1_COEF1_C1(0x1cb) | /* 1.7930 (-0.07 %) */
  407. BF_PXP_CSC1_COEF1_C4(0x214), /* 2.0781 (-0.17 %) */
  408. BF_PXP_CSC1_COEF2_C2(0x776) | /* -0.5391 (+0.36 %) */
  409. BF_PXP_CSC1_COEF2_C3(0x7bf), /* -0.2539 (+0.26 %) */
  410. };
  411. /*
  412. * SMPTE 240m full range:
  413. *
  414. * |R| |1.0000 0.0000 1.5756| |Y + 0 |
  415. * |G| = |1.0000 -0.2253 -0.4767| * |Cb - 128|
  416. * |B| |1.0000 1.8270 0.0000| |Cr - 128|
  417. */
  418. static const u32 csc1_coef_smpte240m_full[3] = {
  419. BM_PXP_CSC1_COEF0_YCBCR_MODE |
  420. BF_PXP_CSC1_COEF0_C0(0x100) | /* 1.0000 (+0.00 %) */
  421. BF_PXP_CSC1_COEF0_UV_OFFSET(-128) |
  422. BF_PXP_CSC1_COEF0_Y_OFFSET(0),
  423. BF_PXP_CSC1_COEF1_C1(0x193) | /* 1.5742 (-0.14 %) */
  424. BF_PXP_CSC1_COEF1_C4(0x1d3), /* 1.8242 (-0.28 %) */
  425. BF_PXP_CSC1_COEF2_C2(0x786) | /* -0.4766 (+0.01 %) */
  426. BF_PXP_CSC1_COEF2_C3(0x7c7), /* -0.2227 (+0.26 %) */
  427. };
  428. const u32 *csc1_coef;
  429. ycbcr_enc = ctx->q_data[V4L2_M2M_SRC].ycbcr_enc;
  430. quantization = ctx->q_data[V4L2_M2M_SRC].quant;
  431. if (ycbcr_enc == V4L2_YCBCR_ENC_601) {
  432. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  433. csc1_coef = csc1_coef_bt601_full;
  434. else
  435. csc1_coef = csc1_coef_bt601_lim;
  436. } else if (ycbcr_enc == V4L2_YCBCR_ENC_709) {
  437. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  438. csc1_coef = csc1_coef_rec709_full;
  439. else
  440. csc1_coef = csc1_coef_rec709_lim;
  441. } else if (ycbcr_enc == V4L2_YCBCR_ENC_BT2020) {
  442. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  443. csc1_coef = csc1_coef_bt2020_full;
  444. else
  445. csc1_coef = csc1_coef_bt2020_lim;
  446. } else {
  447. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  448. csc1_coef = csc1_coef_smpte240m_full;
  449. else
  450. csc1_coef = csc1_coef_smpte240m_lim;
  451. }
  452. writel(csc1_coef[0], dev->mmio + HW_PXP_CSC1_COEF0);
  453. writel(csc1_coef[1], dev->mmio + HW_PXP_CSC1_COEF1);
  454. writel(csc1_coef[2], dev->mmio + HW_PXP_CSC1_COEF2);
  455. } else {
  456. writel(BM_PXP_CSC1_COEF0_BYPASS, dev->mmio + HW_PXP_CSC1_COEF0);
  457. }
  458. if (!pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_SRC].fmt->fourcc) &&
  459. pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_DST].fmt->fourcc)) {
  460. /*
  461. * CSC2 RGB to YUV/YCbCr conversion is implemented as follows:
  462. *
  463. * |Y | |A1 A2 A3| |R| |D1|
  464. * |Cb| = |B1 B2 B3| * |G| + |D2|
  465. * |Cr| |C1 C2 C3| |B| |D3|
  466. *
  467. * Results are clamped to 0..255.
  468. *
  469. * BT.601 limited range:
  470. *
  471. * |Y | | 0.2568 0.5041 0.0979| |R| |16 |
  472. * |Cb| = |-0.1482 -0.2910 0.4392| * |G| + |128|
  473. * |Cr| | 0.4392 0.4392 -0.3678| |B| |128|
  474. */
  475. static const u32 csc2_coef_bt601_lim[6] = {
  476. BF_PXP_CSC2_COEF0_A2(0x081) | /* 0.5039 (-0.02 %) */
  477. BF_PXP_CSC2_COEF0_A1(0x041), /* 0.2539 (-0.29 %) */
  478. BF_PXP_CSC2_COEF1_B1(0x7db) | /* -0.1445 (+0.37 %) */
  479. BF_PXP_CSC2_COEF1_A3(0x019), /* 0.0977 (-0.02 %) */
  480. BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
  481. BF_PXP_CSC2_COEF2_B2(0x7b6), /* -0.2891 (+0.20 %) */
  482. BF_PXP_CSC2_COEF3_C2(0x7a2) | /* -0.3672 (+0.06 %) */
  483. BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
  484. BF_PXP_CSC2_COEF4_D1(16) |
  485. BF_PXP_CSC2_COEF4_C3(0x7ee), /* -0.0703 (+0.11 %) */
  486. BF_PXP_CSC2_COEF5_D3(128) |
  487. BF_PXP_CSC2_COEF5_D2(128),
  488. };
  489. /*
  490. * BT.601 full range:
  491. *
  492. * |Y | | 0.2990 0.5870 0.1140| |R| |0 |
  493. * |Cb| = |-0.1687 -0.3313 0.5000| * |G| + |128|
  494. * |Cr| | 0.5000 0.5000 -0.4187| |B| |128|
  495. */
  496. static const u32 csc2_coef_bt601_full[6] = {
  497. BF_PXP_CSC2_COEF0_A2(0x096) | /* 0.5859 (-0.11 %) */
  498. BF_PXP_CSC2_COEF0_A1(0x04c), /* 0.2969 (-0.21 %) */
  499. BF_PXP_CSC2_COEF1_B1(0x7d5) | /* -0.1680 (+0.07 %) */
  500. BF_PXP_CSC2_COEF1_A3(0x01d), /* 0.1133 (-0.07 %) */
  501. BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
  502. BF_PXP_CSC2_COEF2_B2(0x7ac), /* -0.3281 (+0.32 %) */
  503. BF_PXP_CSC2_COEF3_C2(0x795) | /* -0.4180 (+0.07 %) */
  504. BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
  505. BF_PXP_CSC2_COEF4_D1(0) |
  506. BF_PXP_CSC2_COEF4_C3(0x7ec), /* -0.0781 (+0.32 %) */
  507. BF_PXP_CSC2_COEF5_D3(128) |
  508. BF_PXP_CSC2_COEF5_D2(128),
  509. };
  510. /*
  511. * Rec.709 limited range:
  512. *
  513. * |Y | | 0.1826 0.6142 0.0620| |R| |16 |
  514. * |Cb| = |-0.1007 -0.3385 0.4392| * |G| + |128|
  515. * |Cr| | 0.4392 0.4392 -0.3990| |B| |128|
  516. */
  517. static const u32 csc2_coef_rec709_lim[6] = {
  518. BF_PXP_CSC2_COEF0_A2(0x09d) | /* 0.6133 (-0.09 %) */
  519. BF_PXP_CSC2_COEF0_A1(0x02e), /* 0.1797 (-0.29 %) */
  520. BF_PXP_CSC2_COEF1_B1(0x7e7) | /* -0.0977 (+0.30 %) */
  521. BF_PXP_CSC2_COEF1_A3(0x00f), /* 0.0586 (-0.34 %) */
  522. BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
  523. BF_PXP_CSC2_COEF2_B2(0x7aa), /* -0.3359 (+0.26 %) */
  524. BF_PXP_CSC2_COEF3_C2(0x79a) | /* -0.3984 (+0.05 %) */
  525. BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
  526. BF_PXP_CSC2_COEF4_D1(16) |
  527. BF_PXP_CSC2_COEF4_C3(0x7f6), /* -0.0391 (+0.12 %) */
  528. BF_PXP_CSC2_COEF5_D3(128) |
  529. BF_PXP_CSC2_COEF5_D2(128),
  530. };
  531. /*
  532. * Rec.709 full range:
  533. *
  534. * |Y | | 0.2126 0.7152 0.0722| |R| |0 |
  535. * |Cb| = |-0.1146 -0.3854 0.5000| * |G| + |128|
  536. * |Cr| | 0.5000 0.5000 -0.4542| |B| |128|
  537. */
  538. static const u32 csc2_coef_rec709_full[6] = {
  539. BF_PXP_CSC2_COEF0_A2(0x0b7) | /* 0.7148 (-0.04 %) */
  540. BF_PXP_CSC2_COEF0_A1(0x036), /* 0.2109 (-0.17 %) */
  541. BF_PXP_CSC2_COEF1_B1(0x7e3) | /* -0.1133 (+0.13 %) */
  542. BF_PXP_CSC2_COEF1_A3(0x012), /* 0.0703 (-0.19 %) */
  543. BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
  544. BF_PXP_CSC2_COEF2_B2(0x79e), /* -0.3828 (+0.26 %) */
  545. BF_PXP_CSC2_COEF3_C2(0x78c) | /* -0.4531 (+0.11 %) */
  546. BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
  547. BF_PXP_CSC2_COEF4_D1(0) |
  548. BF_PXP_CSC2_COEF4_C3(0x7f5), /* -0.0430 (+0.28 %) */
  549. BF_PXP_CSC2_COEF5_D3(128) |
  550. BF_PXP_CSC2_COEF5_D2(128),
  551. };
  552. /*
  553. * BT.2020 limited range:
  554. *
  555. * |Y | | 0.2256 0.5823 0.0509| |R| |16 |
  556. * |Cb| = |-0.1226 -0.3166 0.4392| * |G| + |128|
  557. * |Cr| | 0.4392 0.4392 -0.4039| |B| |128|
  558. */
  559. static const u32 csc2_coef_bt2020_lim[6] = {
  560. BF_PXP_CSC2_COEF0_A2(0x095) | /* 0.5820 (-0.03 %) */
  561. BF_PXP_CSC2_COEF0_A1(0x039), /* 0.2227 (-0.30 %) */
  562. BF_PXP_CSC2_COEF1_B1(0x7e1) | /* -0.1211 (+0.15 %) */
  563. BF_PXP_CSC2_COEF1_A3(0x00d), /* 0.0508 (-0.01 %) */
  564. BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
  565. BF_PXP_CSC2_COEF2_B2(0x7af), /* -0.3164 (+0.02 %) */
  566. BF_PXP_CSC2_COEF3_C2(0x799) | /* -0.4023 (+0.16 %) */
  567. BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
  568. BF_PXP_CSC2_COEF4_D1(16) |
  569. BF_PXP_CSC2_COEF4_C3(0x7f7), /* -0.0352 (+0.02 %) */
  570. BF_PXP_CSC2_COEF5_D3(128) |
  571. BF_PXP_CSC2_COEF5_D2(128),
  572. };
  573. /*
  574. * BT.2020 full range:
  575. *
  576. * |Y | | 0.2627 0.6780 0.0593| |R| |0 |
  577. * |Cb| = |-0.1396 -0.3604 0.5000| * |G| + |128|
  578. * |Cr| | 0.5000 0.5000 -0.4598| |B| |128|
  579. */
  580. static const u32 csc2_coef_bt2020_full[6] = {
  581. BF_PXP_CSC2_COEF0_A2(0x0ad) | /* 0.6758 (-0.22 %) */
  582. BF_PXP_CSC2_COEF0_A1(0x043), /* 0.2617 (-0.10 %) */
  583. BF_PXP_CSC2_COEF1_B1(0x7dd) | /* -0.1367 (+0.29 %) */
  584. BF_PXP_CSC2_COEF1_A3(0x00f), /* 0.0586 (-0.07 %) */
  585. BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
  586. BF_PXP_CSC2_COEF2_B2(0x7a4), /* -0.3594 (+0.10 %) */
  587. BF_PXP_CSC2_COEF3_C2(0x78b) | /* -0.4570 (+0.28 %) */
  588. BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
  589. BF_PXP_CSC2_COEF4_D1(0) |
  590. BF_PXP_CSC2_COEF4_C3(0x7f6), /* -0.0391 (+0.11 %) */
  591. BF_PXP_CSC2_COEF5_D3(128) |
  592. BF_PXP_CSC2_COEF5_D2(128),
  593. };
  594. /*
  595. * SMPTE 240m limited range:
  596. *
  597. * |Y | | 0.1821 0.6020 0.0747| |R| |16 |
  598. * |Cb| = |-0.1019 -0.3373 0.4392| * |G| + |128|
  599. * |Cr| | 0.4392 0.4392 -0.3909| |B| |128|
  600. */
  601. static const u32 csc2_coef_smpte240m_lim[6] = {
  602. BF_PXP_CSC2_COEF0_A2(0x09a) | /* 0.6016 (-0.05 %) */
  603. BF_PXP_CSC2_COEF0_A1(0x02e), /* 0.1797 (-0.24 %) */
  604. BF_PXP_CSC2_COEF1_B1(0x7e6) | /* -0.1016 (+0.03 %) */
  605. BF_PXP_CSC2_COEF1_A3(0x013), /* 0.0742 (-0.05 %) */
  606. BF_PXP_CSC2_COEF2_B3(0x070) | /* 0.4375 (-0.17 %) */
  607. BF_PXP_CSC2_COEF2_B2(0x7aa), /* -0.3359 (+0.14 %) */
  608. BF_PXP_CSC2_COEF3_C2(0x79c) | /* -0.3906 (+0.03 %) */
  609. BF_PXP_CSC2_COEF3_C1(0x070), /* 0.4375 (-0.17 %) */
  610. BF_PXP_CSC2_COEF4_D1(16) |
  611. BF_PXP_CSC2_COEF4_C3(0x7f4), /* -0.0469 (+0.14 %) */
  612. BF_PXP_CSC2_COEF5_D3(128) |
  613. BF_PXP_CSC2_COEF5_D2(128),
  614. };
  615. /*
  616. * SMPTE 240m full range:
  617. *
  618. * |Y | | 0.2120 0.7010 0.0870| |R| |0 |
  619. * |Cb| = |-0.1160 -0.3840 0.5000| * |G| + |128|
  620. * |Cr| | 0.5000 0.5000 -0.4450| |B| |128|
  621. */
  622. static const u32 csc2_coef_smpte240m_full[6] = {
  623. BF_PXP_CSC2_COEF0_A2(0x0b3) | /* 0.6992 (-0.18 %) */
  624. BF_PXP_CSC2_COEF0_A1(0x036), /* 0.2109 (-0.11 %) */
  625. BF_PXP_CSC2_COEF1_B1(0x7e3) | /* -0.1133 (+0.27 %) */
  626. BF_PXP_CSC2_COEF1_A3(0x016), /* 0.0859 (-0.11 %) */
  627. BF_PXP_CSC2_COEF2_B3(0x080) | /* 0.5000 (+0.00 %) */
  628. BF_PXP_CSC2_COEF2_B2(0x79e), /* -0.3828 (+0.12 %) */
  629. BF_PXP_CSC2_COEF3_C2(0x78f) | /* -0.4414 (+0.36 %) */
  630. BF_PXP_CSC2_COEF3_C1(0x080), /* 0.5000 (+0.00 %) */
  631. BF_PXP_CSC2_COEF4_D1(0) |
  632. BF_PXP_CSC2_COEF4_C3(0x7f2), /* -0.0547 (+0.03 %) */
  633. BF_PXP_CSC2_COEF5_D3(128) |
  634. BF_PXP_CSC2_COEF5_D2(128),
  635. };
  636. const u32 *csc2_coef;
  637. u32 csc2_ctrl;
  638. ycbcr_enc = ctx->q_data[V4L2_M2M_DST].ycbcr_enc;
  639. quantization = ctx->q_data[V4L2_M2M_DST].quant;
  640. if (ycbcr_enc == V4L2_YCBCR_ENC_601) {
  641. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  642. csc2_coef = csc2_coef_bt601_full;
  643. else
  644. csc2_coef = csc2_coef_bt601_lim;
  645. } else if (ycbcr_enc == V4L2_YCBCR_ENC_709) {
  646. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  647. csc2_coef = csc2_coef_rec709_full;
  648. else
  649. csc2_coef = csc2_coef_rec709_lim;
  650. } else if (ycbcr_enc == V4L2_YCBCR_ENC_BT2020) {
  651. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  652. csc2_coef = csc2_coef_bt2020_full;
  653. else
  654. csc2_coef = csc2_coef_bt2020_lim;
  655. } else {
  656. if (quantization == V4L2_QUANTIZATION_FULL_RANGE)
  657. csc2_coef = csc2_coef_smpte240m_full;
  658. else
  659. csc2_coef = csc2_coef_smpte240m_lim;
  660. }
  661. if (quantization == V4L2_QUANTIZATION_FULL_RANGE) {
  662. csc2_ctrl = BV_PXP_CSC2_CTRL_CSC_MODE__RGB2YUV <<
  663. BP_PXP_CSC2_CTRL_CSC_MODE;
  664. } else {
  665. csc2_ctrl = BV_PXP_CSC2_CTRL_CSC_MODE__RGB2YCbCr <<
  666. BP_PXP_CSC2_CTRL_CSC_MODE;
  667. }
  668. writel(csc2_ctrl, dev->mmio + HW_PXP_CSC2_CTRL);
  669. writel(csc2_coef[0], dev->mmio + HW_PXP_CSC2_COEF0);
  670. writel(csc2_coef[1], dev->mmio + HW_PXP_CSC2_COEF1);
  671. writel(csc2_coef[2], dev->mmio + HW_PXP_CSC2_COEF2);
  672. writel(csc2_coef[3], dev->mmio + HW_PXP_CSC2_COEF3);
  673. writel(csc2_coef[4], dev->mmio + HW_PXP_CSC2_COEF4);
  674. writel(csc2_coef[5], dev->mmio + HW_PXP_CSC2_COEF5);
  675. } else {
  676. writel(BM_PXP_CSC2_CTRL_BYPASS, dev->mmio + HW_PXP_CSC2_CTRL);
  677. }
  678. }
  679. static int pxp_start(struct pxp_ctx *ctx, struct vb2_v4l2_buffer *in_vb,
  680. struct vb2_v4l2_buffer *out_vb)
  681. {
  682. struct pxp_dev *dev = ctx->dev;
  683. struct pxp_q_data *q_data;
  684. u32 src_width, src_height, src_stride, src_fourcc;
  685. u32 dst_width, dst_height, dst_stride, dst_fourcc;
  686. dma_addr_t p_in, p_out;
  687. u32 ctrl, out_ctrl, out_buf, out_buf2, out_pitch, out_lrc, out_ps_ulc;
  688. u32 out_ps_lrc;
  689. u32 ps_ctrl, ps_buf, ps_ubuf, ps_vbuf, ps_pitch, ps_scale, ps_offset;
  690. u32 as_ulc, as_lrc;
  691. u32 y_size;
  692. u32 decx, decy, xscale, yscale;
  693. q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
  694. src_width = ctx->q_data[V4L2_M2M_SRC].width;
  695. dst_width = ctx->q_data[V4L2_M2M_DST].width;
  696. src_height = ctx->q_data[V4L2_M2M_SRC].height;
  697. dst_height = ctx->q_data[V4L2_M2M_DST].height;
  698. src_stride = ctx->q_data[V4L2_M2M_SRC].bytesperline;
  699. dst_stride = ctx->q_data[V4L2_M2M_DST].bytesperline;
  700. src_fourcc = ctx->q_data[V4L2_M2M_SRC].fmt->fourcc;
  701. dst_fourcc = ctx->q_data[V4L2_M2M_DST].fmt->fourcc;
  702. p_in = vb2_dma_contig_plane_dma_addr(&in_vb->vb2_buf, 0);
  703. p_out = vb2_dma_contig_plane_dma_addr(&out_vb->vb2_buf, 0);
  704. if (!p_in || !p_out) {
  705. v4l2_err(&dev->v4l2_dev,
  706. "Acquiring DMA addresses of buffers failed\n");
  707. return -EFAULT;
  708. }
  709. out_vb->sequence =
  710. get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
  711. in_vb->sequence = q_data->sequence++;
  712. out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp;
  713. if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
  714. out_vb->timecode = in_vb->timecode;
  715. out_vb->field = in_vb->field;
  716. out_vb->flags = in_vb->flags &
  717. (V4L2_BUF_FLAG_TIMECODE |
  718. V4L2_BUF_FLAG_KEYFRAME |
  719. V4L2_BUF_FLAG_PFRAME |
  720. V4L2_BUF_FLAG_BFRAME |
  721. V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
  722. /* 8x8 block size */
  723. ctrl = BF_PXP_CTRL_VFLIP0(!!(ctx->mode & MEM2MEM_VFLIP)) |
  724. BF_PXP_CTRL_HFLIP0(!!(ctx->mode & MEM2MEM_HFLIP)) |
  725. BF_PXP_CTRL_ROTATE0(ctx->rotation);
  726. /* Always write alpha value as V4L2_CID_ALPHA_COMPONENT */
  727. out_ctrl = BF_PXP_OUT_CTRL_ALPHA(ctx->alpha_component) |
  728. BF_PXP_OUT_CTRL_ALPHA_OUTPUT(1) |
  729. pxp_v4l2_pix_fmt_to_out_format(dst_fourcc);
  730. out_buf = p_out;
  731. if (ctx->rotation == BV_PXP_CTRL_ROTATE0__ROT_90 ||
  732. ctx->rotation == BV_PXP_CTRL_ROTATE0__ROT_270)
  733. swap(dst_width, dst_height);
  734. switch (dst_fourcc) {
  735. case V4L2_PIX_FMT_NV12:
  736. case V4L2_PIX_FMT_NV21:
  737. case V4L2_PIX_FMT_NV16:
  738. case V4L2_PIX_FMT_NV61:
  739. out_buf2 = out_buf + dst_stride * dst_height;
  740. break;
  741. default:
  742. out_buf2 = 0;
  743. }
  744. out_pitch = BF_PXP_OUT_PITCH_PITCH(dst_stride);
  745. out_lrc = BF_PXP_OUT_LRC_X(dst_width - 1) |
  746. BF_PXP_OUT_LRC_Y(dst_height - 1);
  747. /* PS covers whole output */
  748. out_ps_ulc = BF_PXP_OUT_PS_ULC_X(0) | BF_PXP_OUT_PS_ULC_Y(0);
  749. out_ps_lrc = BF_PXP_OUT_PS_LRC_X(dst_width - 1) |
  750. BF_PXP_OUT_PS_LRC_Y(dst_height - 1);
  751. /* no AS */
  752. as_ulc = BF_PXP_OUT_AS_ULC_X(1) | BF_PXP_OUT_AS_ULC_Y(1);
  753. as_lrc = BF_PXP_OUT_AS_LRC_X(0) | BF_PXP_OUT_AS_LRC_Y(0);
  754. decx = (src_width <= dst_width) ? 0 : ilog2(src_width / dst_width);
  755. decy = (src_height <= dst_height) ? 0 : ilog2(src_height / dst_height);
  756. ps_ctrl = BF_PXP_PS_CTRL_DECX(decx) | BF_PXP_PS_CTRL_DECY(decy) |
  757. pxp_v4l2_pix_fmt_to_ps_format(src_fourcc);
  758. ps_buf = p_in;
  759. y_size = src_stride * src_height;
  760. switch (src_fourcc) {
  761. case V4L2_PIX_FMT_YUV420:
  762. ps_ubuf = ps_buf + y_size;
  763. ps_vbuf = ps_ubuf + y_size / 4;
  764. break;
  765. case V4L2_PIX_FMT_YUV422P:
  766. ps_ubuf = ps_buf + y_size;
  767. ps_vbuf = ps_ubuf + y_size / 2;
  768. break;
  769. case V4L2_PIX_FMT_NV12:
  770. case V4L2_PIX_FMT_NV21:
  771. case V4L2_PIX_FMT_NV16:
  772. case V4L2_PIX_FMT_NV61:
  773. ps_ubuf = ps_buf + y_size;
  774. ps_vbuf = 0;
  775. break;
  776. case V4L2_PIX_FMT_GREY:
  777. case V4L2_PIX_FMT_Y4:
  778. ps_ubuf = 0;
  779. /* In grayscale mode, ps_vbuf contents are reused as CbCr */
  780. ps_vbuf = 0x8080;
  781. break;
  782. default:
  783. ps_ubuf = 0;
  784. ps_vbuf = 0;
  785. break;
  786. }
  787. ps_pitch = BF_PXP_PS_PITCH_PITCH(src_stride);
  788. if (decx) {
  789. xscale = (src_width >> decx) * 0x1000 / dst_width;
  790. } else {
  791. switch (src_fourcc) {
  792. case V4L2_PIX_FMT_UYVY:
  793. case V4L2_PIX_FMT_YUYV:
  794. case V4L2_PIX_FMT_VYUY:
  795. case V4L2_PIX_FMT_YVYU:
  796. case V4L2_PIX_FMT_NV16:
  797. case V4L2_PIX_FMT_NV12:
  798. case V4L2_PIX_FMT_NV21:
  799. case V4L2_PIX_FMT_NV61:
  800. case V4L2_PIX_FMT_YUV422P:
  801. case V4L2_PIX_FMT_YUV420:
  802. /*
  803. * This avoids sampling past the right edge for
  804. * horizontally chroma subsampled formats.
  805. */
  806. xscale = (src_width - 2) * 0x1000 / (dst_width - 1);
  807. break;
  808. default:
  809. xscale = (src_width - 1) * 0x1000 / (dst_width - 1);
  810. break;
  811. }
  812. }
  813. if (decy)
  814. yscale = (src_height >> decy) * 0x1000 / dst_height;
  815. else
  816. yscale = (src_height - 1) * 0x1000 / (dst_height - 1);
  817. ps_scale = BF_PXP_PS_SCALE_YSCALE(yscale) |
  818. BF_PXP_PS_SCALE_XSCALE(xscale);
  819. ps_offset = BF_PXP_PS_OFFSET_YOFFSET(0) | BF_PXP_PS_OFFSET_XOFFSET(0);
  820. writel(ctrl, dev->mmio + HW_PXP_CTRL);
  821. /* skip STAT */
  822. writel(out_ctrl, dev->mmio + HW_PXP_OUT_CTRL);
  823. writel(out_buf, dev->mmio + HW_PXP_OUT_BUF);
  824. writel(out_buf2, dev->mmio + HW_PXP_OUT_BUF2);
  825. writel(out_pitch, dev->mmio + HW_PXP_OUT_PITCH);
  826. writel(out_lrc, dev->mmio + HW_PXP_OUT_LRC);
  827. writel(out_ps_ulc, dev->mmio + HW_PXP_OUT_PS_ULC);
  828. writel(out_ps_lrc, dev->mmio + HW_PXP_OUT_PS_LRC);
  829. writel(as_ulc, dev->mmio + HW_PXP_OUT_AS_ULC);
  830. writel(as_lrc, dev->mmio + HW_PXP_OUT_AS_LRC);
  831. writel(ps_ctrl, dev->mmio + HW_PXP_PS_CTRL);
  832. writel(ps_buf, dev->mmio + HW_PXP_PS_BUF);
  833. writel(ps_ubuf, dev->mmio + HW_PXP_PS_UBUF);
  834. writel(ps_vbuf, dev->mmio + HW_PXP_PS_VBUF);
  835. writel(ps_pitch, dev->mmio + HW_PXP_PS_PITCH);
  836. writel(0x00ffffff, dev->mmio + HW_PXP_PS_BACKGROUND_0);
  837. writel(ps_scale, dev->mmio + HW_PXP_PS_SCALE);
  838. writel(ps_offset, dev->mmio + HW_PXP_PS_OFFSET);
  839. /* disable processed surface color keying */
  840. writel(0x00ffffff, dev->mmio + HW_PXP_PS_CLRKEYLOW_0);
  841. writel(0x00000000, dev->mmio + HW_PXP_PS_CLRKEYHIGH_0);
  842. /* disable alpha surface color keying */
  843. writel(0x00ffffff, dev->mmio + HW_PXP_AS_CLRKEYLOW_0);
  844. writel(0x00000000, dev->mmio + HW_PXP_AS_CLRKEYHIGH_0);
  845. /* setup CSC */
  846. pxp_setup_csc(ctx);
  847. /* bypass LUT */
  848. writel(BM_PXP_LUT_CTRL_BYPASS, dev->mmio + HW_PXP_LUT_CTRL);
  849. writel(BF_PXP_DATA_PATH_CTRL0_MUX15_SEL(0)|
  850. BF_PXP_DATA_PATH_CTRL0_MUX14_SEL(1)|
  851. BF_PXP_DATA_PATH_CTRL0_MUX13_SEL(0)|
  852. BF_PXP_DATA_PATH_CTRL0_MUX12_SEL(0)|
  853. BF_PXP_DATA_PATH_CTRL0_MUX11_SEL(0)|
  854. BF_PXP_DATA_PATH_CTRL0_MUX10_SEL(0)|
  855. BF_PXP_DATA_PATH_CTRL0_MUX9_SEL(1)|
  856. BF_PXP_DATA_PATH_CTRL0_MUX8_SEL(0)|
  857. BF_PXP_DATA_PATH_CTRL0_MUX7_SEL(0)|
  858. BF_PXP_DATA_PATH_CTRL0_MUX6_SEL(0)|
  859. BF_PXP_DATA_PATH_CTRL0_MUX5_SEL(0)|
  860. BF_PXP_DATA_PATH_CTRL0_MUX4_SEL(0)|
  861. BF_PXP_DATA_PATH_CTRL0_MUX3_SEL(0)|
  862. BF_PXP_DATA_PATH_CTRL0_MUX2_SEL(0)|
  863. BF_PXP_DATA_PATH_CTRL0_MUX1_SEL(0)|
  864. BF_PXP_DATA_PATH_CTRL0_MUX0_SEL(0),
  865. dev->mmio + HW_PXP_DATA_PATH_CTRL0);
  866. writel(BF_PXP_DATA_PATH_CTRL1_MUX17_SEL(1) |
  867. BF_PXP_DATA_PATH_CTRL1_MUX16_SEL(1),
  868. dev->mmio + HW_PXP_DATA_PATH_CTRL1);
  869. writel(0xffff, dev->mmio + HW_PXP_IRQ_MASK);
  870. /* ungate, enable PS/AS/OUT and PXP operation */
  871. writel(BM_PXP_CTRL_IRQ_ENABLE, dev->mmio + HW_PXP_CTRL_SET);
  872. writel(BM_PXP_CTRL_ENABLE | BM_PXP_CTRL_ENABLE_CSC2 |
  873. BM_PXP_CTRL_ENABLE_LUT | BM_PXP_CTRL_ENABLE_ROTATE0 |
  874. BM_PXP_CTRL_ENABLE_PS_AS_OUT, dev->mmio + HW_PXP_CTRL_SET);
  875. return 0;
  876. }
  877. static void pxp_job_finish(struct pxp_dev *dev)
  878. {
  879. struct pxp_ctx *curr_ctx;
  880. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  881. unsigned long flags;
  882. curr_ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
  883. if (curr_ctx == NULL) {
  884. pr_err("Instance released before the end of transaction\n");
  885. return;
  886. }
  887. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  888. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  889. spin_lock_irqsave(&dev->irqlock, flags);
  890. v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
  891. v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
  892. spin_unlock_irqrestore(&dev->irqlock, flags);
  893. dprintk(curr_ctx->dev, "Finishing transaction\n");
  894. v4l2_m2m_job_finish(dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  895. }
  896. /*
  897. * mem2mem callbacks
  898. */
  899. static void pxp_device_run(void *priv)
  900. {
  901. struct pxp_ctx *ctx = priv;
  902. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  903. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  904. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  905. pxp_start(ctx, src_buf, dst_buf);
  906. }
  907. static int pxp_job_ready(void *priv)
  908. {
  909. struct pxp_ctx *ctx = priv;
  910. if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < 1 ||
  911. v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < 1) {
  912. dprintk(ctx->dev, "Not enough buffers available\n");
  913. return 0;
  914. }
  915. return 1;
  916. }
  917. static void pxp_job_abort(void *priv)
  918. {
  919. struct pxp_ctx *ctx = priv;
  920. /* Will cancel the transaction in the next interrupt handler */
  921. ctx->aborting = 1;
  922. }
  923. /*
  924. * interrupt handler
  925. */
  926. static irqreturn_t pxp_irq_handler(int irq, void *dev_id)
  927. {
  928. struct pxp_dev *dev = dev_id;
  929. u32 stat;
  930. stat = readl(dev->mmio + HW_PXP_STAT);
  931. if (stat & BM_PXP_STAT_IRQ0) {
  932. /* we expect x = 0, y = height, irq0 = 1 */
  933. if (stat & ~(BM_PXP_STAT_BLOCKX | BM_PXP_STAT_BLOCKY |
  934. BM_PXP_STAT_IRQ0))
  935. dprintk(dev, "%s: stat = 0x%08x\n", __func__, stat);
  936. writel(BM_PXP_STAT_IRQ0, dev->mmio + HW_PXP_STAT_CLR);
  937. pxp_job_finish(dev);
  938. } else {
  939. u32 irq = readl(dev->mmio + HW_PXP_IRQ);
  940. dprintk(dev, "%s: stat = 0x%08x\n", __func__, stat);
  941. dprintk(dev, "%s: irq = 0x%08x\n", __func__, irq);
  942. writel(irq, dev->mmio + HW_PXP_IRQ_CLR);
  943. }
  944. return IRQ_HANDLED;
  945. }
  946. /*
  947. * video ioctls
  948. */
  949. static int pxp_querycap(struct file *file, void *priv,
  950. struct v4l2_capability *cap)
  951. {
  952. strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
  953. strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
  954. snprintf(cap->bus_info, sizeof(cap->bus_info),
  955. "platform:%s", MEM2MEM_NAME);
  956. return 0;
  957. }
  958. static int pxp_enum_fmt(struct v4l2_fmtdesc *f, u32 type)
  959. {
  960. int i, num;
  961. struct pxp_fmt *fmt;
  962. num = 0;
  963. for (i = 0; i < NUM_FORMATS; ++i) {
  964. if (formats[i].types & type) {
  965. /* index-th format of type type found ? */
  966. if (num == f->index)
  967. break;
  968. /*
  969. * Correct type but haven't reached our index yet,
  970. * just increment per-type index
  971. */
  972. ++num;
  973. }
  974. }
  975. if (i < NUM_FORMATS) {
  976. /* Format found */
  977. fmt = &formats[i];
  978. f->pixelformat = fmt->fourcc;
  979. return 0;
  980. }
  981. /* Format not found */
  982. return -EINVAL;
  983. }
  984. static int pxp_enum_fmt_vid_cap(struct file *file, void *priv,
  985. struct v4l2_fmtdesc *f)
  986. {
  987. return pxp_enum_fmt(f, MEM2MEM_CAPTURE);
  988. }
  989. static int pxp_enum_fmt_vid_out(struct file *file, void *priv,
  990. struct v4l2_fmtdesc *f)
  991. {
  992. return pxp_enum_fmt(f, MEM2MEM_OUTPUT);
  993. }
  994. static int pxp_g_fmt(struct pxp_ctx *ctx, struct v4l2_format *f)
  995. {
  996. struct vb2_queue *vq;
  997. struct pxp_q_data *q_data;
  998. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  999. if (!vq)
  1000. return -EINVAL;
  1001. q_data = get_q_data(ctx, f->type);
  1002. f->fmt.pix.width = q_data->width;
  1003. f->fmt.pix.height = q_data->height;
  1004. f->fmt.pix.field = V4L2_FIELD_NONE;
  1005. f->fmt.pix.pixelformat = q_data->fmt->fourcc;
  1006. f->fmt.pix.bytesperline = q_data->bytesperline;
  1007. f->fmt.pix.sizeimage = q_data->sizeimage;
  1008. f->fmt.pix.colorspace = ctx->colorspace;
  1009. f->fmt.pix.xfer_func = ctx->xfer_func;
  1010. f->fmt.pix.ycbcr_enc = q_data->ycbcr_enc;
  1011. f->fmt.pix.quantization = q_data->quant;
  1012. return 0;
  1013. }
  1014. static int pxp_g_fmt_vid_out(struct file *file, void *priv,
  1015. struct v4l2_format *f)
  1016. {
  1017. return pxp_g_fmt(file2ctx(file), f);
  1018. }
  1019. static int pxp_g_fmt_vid_cap(struct file *file, void *priv,
  1020. struct v4l2_format *f)
  1021. {
  1022. return pxp_g_fmt(file2ctx(file), f);
  1023. }
  1024. static inline u32 pxp_bytesperline(struct pxp_fmt *fmt, u32 width)
  1025. {
  1026. switch (fmt->fourcc) {
  1027. case V4L2_PIX_FMT_YUV420:
  1028. case V4L2_PIX_FMT_NV12:
  1029. case V4L2_PIX_FMT_NV21:
  1030. case V4L2_PIX_FMT_YUV422P:
  1031. case V4L2_PIX_FMT_NV16:
  1032. case V4L2_PIX_FMT_NV61:
  1033. return width;
  1034. default:
  1035. return (width * fmt->depth) >> 3;
  1036. }
  1037. }
  1038. static inline u32 pxp_sizeimage(struct pxp_fmt *fmt, u32 width, u32 height)
  1039. {
  1040. return (fmt->depth * width * height) >> 3;
  1041. }
  1042. static int pxp_try_fmt(struct v4l2_format *f, struct pxp_fmt *fmt)
  1043. {
  1044. v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, ALIGN_W,
  1045. &f->fmt.pix.height, MIN_H, MAX_H, ALIGN_H, 0);
  1046. f->fmt.pix.bytesperline = pxp_bytesperline(fmt, f->fmt.pix.width);
  1047. f->fmt.pix.sizeimage = pxp_sizeimage(fmt, f->fmt.pix.width,
  1048. f->fmt.pix.height);
  1049. f->fmt.pix.field = V4L2_FIELD_NONE;
  1050. return 0;
  1051. }
  1052. static void
  1053. pxp_fixup_colorimetry_cap(struct pxp_ctx *ctx, u32 dst_fourcc,
  1054. enum v4l2_ycbcr_encoding *ycbcr_enc,
  1055. enum v4l2_quantization *quantization)
  1056. {
  1057. bool dst_is_yuv = pxp_v4l2_pix_fmt_is_yuv(dst_fourcc);
  1058. if (pxp_v4l2_pix_fmt_is_yuv(ctx->q_data[V4L2_M2M_SRC].fmt->fourcc) ==
  1059. dst_is_yuv) {
  1060. /*
  1061. * There is no support for conversion between different YCbCr
  1062. * encodings or between RGB limited and full range.
  1063. */
  1064. *ycbcr_enc = ctx->q_data[V4L2_M2M_SRC].ycbcr_enc;
  1065. *quantization = ctx->q_data[V4L2_M2M_SRC].quant;
  1066. } else {
  1067. *ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(ctx->colorspace);
  1068. *quantization = V4L2_MAP_QUANTIZATION_DEFAULT(!dst_is_yuv,
  1069. ctx->colorspace,
  1070. *ycbcr_enc);
  1071. }
  1072. }
  1073. static int pxp_try_fmt_vid_cap(struct file *file, void *priv,
  1074. struct v4l2_format *f)
  1075. {
  1076. struct pxp_fmt *fmt;
  1077. struct pxp_ctx *ctx = file2ctx(file);
  1078. fmt = find_format(f);
  1079. if (!fmt) {
  1080. f->fmt.pix.pixelformat = formats[0].fourcc;
  1081. fmt = find_format(f);
  1082. }
  1083. if (!(fmt->types & MEM2MEM_CAPTURE)) {
  1084. v4l2_err(&ctx->dev->v4l2_dev,
  1085. "Fourcc format (0x%08x) invalid.\n",
  1086. f->fmt.pix.pixelformat);
  1087. return -EINVAL;
  1088. }
  1089. f->fmt.pix.colorspace = ctx->colorspace;
  1090. f->fmt.pix.xfer_func = ctx->xfer_func;
  1091. pxp_fixup_colorimetry_cap(ctx, fmt->fourcc,
  1092. &f->fmt.pix.ycbcr_enc,
  1093. &f->fmt.pix.quantization);
  1094. return pxp_try_fmt(f, fmt);
  1095. }
  1096. static int pxp_try_fmt_vid_out(struct file *file, void *priv,
  1097. struct v4l2_format *f)
  1098. {
  1099. struct pxp_fmt *fmt;
  1100. struct pxp_ctx *ctx = file2ctx(file);
  1101. fmt = find_format(f);
  1102. if (!fmt) {
  1103. f->fmt.pix.pixelformat = formats[0].fourcc;
  1104. fmt = find_format(f);
  1105. }
  1106. if (!(fmt->types & MEM2MEM_OUTPUT)) {
  1107. v4l2_err(&ctx->dev->v4l2_dev,
  1108. "Fourcc format (0x%08x) invalid.\n",
  1109. f->fmt.pix.pixelformat);
  1110. return -EINVAL;
  1111. }
  1112. if (!f->fmt.pix.colorspace)
  1113. f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  1114. return pxp_try_fmt(f, fmt);
  1115. }
  1116. static int pxp_s_fmt(struct pxp_ctx *ctx, struct v4l2_format *f)
  1117. {
  1118. struct pxp_q_data *q_data;
  1119. struct vb2_queue *vq;
  1120. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  1121. if (!vq)
  1122. return -EINVAL;
  1123. q_data = get_q_data(ctx, f->type);
  1124. if (!q_data)
  1125. return -EINVAL;
  1126. if (vb2_is_busy(vq)) {
  1127. v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
  1128. return -EBUSY;
  1129. }
  1130. q_data->fmt = find_format(f);
  1131. q_data->width = f->fmt.pix.width;
  1132. q_data->height = f->fmt.pix.height;
  1133. q_data->bytesperline = f->fmt.pix.bytesperline;
  1134. q_data->sizeimage = f->fmt.pix.sizeimage;
  1135. dprintk(ctx->dev,
  1136. "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
  1137. f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
  1138. return 0;
  1139. }
  1140. static int pxp_s_fmt_vid_cap(struct file *file, void *priv,
  1141. struct v4l2_format *f)
  1142. {
  1143. struct pxp_ctx *ctx = file2ctx(file);
  1144. int ret;
  1145. ret = pxp_try_fmt_vid_cap(file, priv, f);
  1146. if (ret)
  1147. return ret;
  1148. ret = pxp_s_fmt(file2ctx(file), f);
  1149. if (ret)
  1150. return ret;
  1151. ctx->q_data[V4L2_M2M_DST].ycbcr_enc = f->fmt.pix.ycbcr_enc;
  1152. ctx->q_data[V4L2_M2M_DST].quant = f->fmt.pix.quantization;
  1153. return 0;
  1154. }
  1155. static int pxp_s_fmt_vid_out(struct file *file, void *priv,
  1156. struct v4l2_format *f)
  1157. {
  1158. struct pxp_ctx *ctx = file2ctx(file);
  1159. int ret;
  1160. ret = pxp_try_fmt_vid_out(file, priv, f);
  1161. if (ret)
  1162. return ret;
  1163. ret = pxp_s_fmt(file2ctx(file), f);
  1164. if (ret)
  1165. return ret;
  1166. ctx->colorspace = f->fmt.pix.colorspace;
  1167. ctx->xfer_func = f->fmt.pix.xfer_func;
  1168. ctx->q_data[V4L2_M2M_SRC].ycbcr_enc = f->fmt.pix.ycbcr_enc;
  1169. ctx->q_data[V4L2_M2M_SRC].quant = f->fmt.pix.quantization;
  1170. pxp_fixup_colorimetry_cap(ctx, ctx->q_data[V4L2_M2M_DST].fmt->fourcc,
  1171. &ctx->q_data[V4L2_M2M_DST].ycbcr_enc,
  1172. &ctx->q_data[V4L2_M2M_DST].quant);
  1173. return 0;
  1174. }
  1175. static u8 pxp_degrees_to_rot_mode(u32 degrees)
  1176. {
  1177. switch (degrees) {
  1178. case 90:
  1179. return BV_PXP_CTRL_ROTATE0__ROT_90;
  1180. case 180:
  1181. return BV_PXP_CTRL_ROTATE0__ROT_180;
  1182. case 270:
  1183. return BV_PXP_CTRL_ROTATE0__ROT_270;
  1184. case 0:
  1185. default:
  1186. return BV_PXP_CTRL_ROTATE0__ROT_0;
  1187. }
  1188. }
  1189. static int pxp_s_ctrl(struct v4l2_ctrl *ctrl)
  1190. {
  1191. struct pxp_ctx *ctx =
  1192. container_of(ctrl->handler, struct pxp_ctx, hdl);
  1193. switch (ctrl->id) {
  1194. case V4L2_CID_HFLIP:
  1195. if (ctrl->val)
  1196. ctx->mode |= MEM2MEM_HFLIP;
  1197. else
  1198. ctx->mode &= ~MEM2MEM_HFLIP;
  1199. break;
  1200. case V4L2_CID_VFLIP:
  1201. if (ctrl->val)
  1202. ctx->mode |= MEM2MEM_VFLIP;
  1203. else
  1204. ctx->mode &= ~MEM2MEM_VFLIP;
  1205. break;
  1206. case V4L2_CID_ROTATE:
  1207. ctx->rotation = pxp_degrees_to_rot_mode(ctrl->val);
  1208. break;
  1209. case V4L2_CID_ALPHA_COMPONENT:
  1210. ctx->alpha_component = ctrl->val;
  1211. break;
  1212. default:
  1213. v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
  1214. return -EINVAL;
  1215. }
  1216. return 0;
  1217. }
  1218. static const struct v4l2_ctrl_ops pxp_ctrl_ops = {
  1219. .s_ctrl = pxp_s_ctrl,
  1220. };
  1221. static const struct v4l2_ioctl_ops pxp_ioctl_ops = {
  1222. .vidioc_querycap = pxp_querycap,
  1223. .vidioc_enum_fmt_vid_cap = pxp_enum_fmt_vid_cap,
  1224. .vidioc_g_fmt_vid_cap = pxp_g_fmt_vid_cap,
  1225. .vidioc_try_fmt_vid_cap = pxp_try_fmt_vid_cap,
  1226. .vidioc_s_fmt_vid_cap = pxp_s_fmt_vid_cap,
  1227. .vidioc_enum_fmt_vid_out = pxp_enum_fmt_vid_out,
  1228. .vidioc_g_fmt_vid_out = pxp_g_fmt_vid_out,
  1229. .vidioc_try_fmt_vid_out = pxp_try_fmt_vid_out,
  1230. .vidioc_s_fmt_vid_out = pxp_s_fmt_vid_out,
  1231. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  1232. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  1233. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  1234. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  1235. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  1236. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  1237. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  1238. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  1239. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  1240. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1241. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1242. };
  1243. /*
  1244. * Queue operations
  1245. */
  1246. static int pxp_queue_setup(struct vb2_queue *vq,
  1247. unsigned int *nbuffers, unsigned int *nplanes,
  1248. unsigned int sizes[], struct device *alloc_devs[])
  1249. {
  1250. struct pxp_ctx *ctx = vb2_get_drv_priv(vq);
  1251. struct pxp_q_data *q_data;
  1252. unsigned int size, count = *nbuffers;
  1253. q_data = get_q_data(ctx, vq->type);
  1254. size = q_data->sizeimage;
  1255. *nbuffers = count;
  1256. if (*nplanes)
  1257. return sizes[0] < size ? -EINVAL : 0;
  1258. *nplanes = 1;
  1259. sizes[0] = size;
  1260. dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
  1261. return 0;
  1262. }
  1263. static int pxp_buf_prepare(struct vb2_buffer *vb)
  1264. {
  1265. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1266. struct pxp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1267. struct pxp_dev *dev = ctx->dev;
  1268. struct pxp_q_data *q_data;
  1269. dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
  1270. q_data = get_q_data(ctx, vb->vb2_queue->type);
  1271. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  1272. if (vbuf->field == V4L2_FIELD_ANY)
  1273. vbuf->field = V4L2_FIELD_NONE;
  1274. if (vbuf->field != V4L2_FIELD_NONE) {
  1275. dprintk(dev, "%s field isn't supported\n", __func__);
  1276. return -EINVAL;
  1277. }
  1278. }
  1279. if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
  1280. dprintk(dev, "%s data will not fit into plane (%lu < %lu)\n",
  1281. __func__, vb2_plane_size(vb, 0),
  1282. (long)q_data->sizeimage);
  1283. return -EINVAL;
  1284. }
  1285. vb2_set_plane_payload(vb, 0, q_data->sizeimage);
  1286. return 0;
  1287. }
  1288. static void pxp_buf_queue(struct vb2_buffer *vb)
  1289. {
  1290. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1291. struct pxp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  1292. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  1293. }
  1294. static int pxp_start_streaming(struct vb2_queue *q, unsigned int count)
  1295. {
  1296. struct pxp_ctx *ctx = vb2_get_drv_priv(q);
  1297. struct pxp_q_data *q_data = get_q_data(ctx, q->type);
  1298. q_data->sequence = 0;
  1299. return 0;
  1300. }
  1301. static void pxp_stop_streaming(struct vb2_queue *q)
  1302. {
  1303. struct pxp_ctx *ctx = vb2_get_drv_priv(q);
  1304. struct vb2_v4l2_buffer *vbuf;
  1305. unsigned long flags;
  1306. for (;;) {
  1307. if (V4L2_TYPE_IS_OUTPUT(q->type))
  1308. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  1309. else
  1310. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  1311. if (vbuf == NULL)
  1312. return;
  1313. spin_lock_irqsave(&ctx->dev->irqlock, flags);
  1314. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  1315. spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
  1316. }
  1317. }
  1318. static const struct vb2_ops pxp_qops = {
  1319. .queue_setup = pxp_queue_setup,
  1320. .buf_prepare = pxp_buf_prepare,
  1321. .buf_queue = pxp_buf_queue,
  1322. .start_streaming = pxp_start_streaming,
  1323. .stop_streaming = pxp_stop_streaming,
  1324. .wait_prepare = vb2_ops_wait_prepare,
  1325. .wait_finish = vb2_ops_wait_finish,
  1326. };
  1327. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1328. struct vb2_queue *dst_vq)
  1329. {
  1330. struct pxp_ctx *ctx = priv;
  1331. int ret;
  1332. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1333. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1334. src_vq->drv_priv = ctx;
  1335. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1336. src_vq->ops = &pxp_qops;
  1337. src_vq->mem_ops = &vb2_dma_contig_memops;
  1338. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1339. src_vq->lock = &ctx->dev->dev_mutex;
  1340. src_vq->dev = ctx->dev->v4l2_dev.dev;
  1341. ret = vb2_queue_init(src_vq);
  1342. if (ret)
  1343. return ret;
  1344. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1345. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  1346. dst_vq->drv_priv = ctx;
  1347. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1348. dst_vq->ops = &pxp_qops;
  1349. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1350. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1351. dst_vq->lock = &ctx->dev->dev_mutex;
  1352. dst_vq->dev = ctx->dev->v4l2_dev.dev;
  1353. return vb2_queue_init(dst_vq);
  1354. }
  1355. /*
  1356. * File operations
  1357. */
  1358. static int pxp_open(struct file *file)
  1359. {
  1360. struct pxp_dev *dev = video_drvdata(file);
  1361. struct pxp_ctx *ctx = NULL;
  1362. struct v4l2_ctrl_handler *hdl;
  1363. int rc = 0;
  1364. if (mutex_lock_interruptible(&dev->dev_mutex))
  1365. return -ERESTARTSYS;
  1366. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1367. if (!ctx) {
  1368. rc = -ENOMEM;
  1369. goto open_unlock;
  1370. }
  1371. v4l2_fh_init(&ctx->fh, video_devdata(file));
  1372. file->private_data = &ctx->fh;
  1373. ctx->dev = dev;
  1374. hdl = &ctx->hdl;
  1375. v4l2_ctrl_handler_init(hdl, 4);
  1376. v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  1377. v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  1378. v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
  1379. v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
  1380. 0, 255, 1, 255);
  1381. if (hdl->error) {
  1382. rc = hdl->error;
  1383. v4l2_ctrl_handler_free(hdl);
  1384. kfree(ctx);
  1385. goto open_unlock;
  1386. }
  1387. ctx->fh.ctrl_handler = hdl;
  1388. v4l2_ctrl_handler_setup(hdl);
  1389. ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
  1390. ctx->q_data[V4L2_M2M_SRC].width = 640;
  1391. ctx->q_data[V4L2_M2M_SRC].height = 480;
  1392. ctx->q_data[V4L2_M2M_SRC].bytesperline =
  1393. pxp_bytesperline(&formats[0], 640);
  1394. ctx->q_data[V4L2_M2M_SRC].sizeimage =
  1395. pxp_sizeimage(&formats[0], 640, 480);
  1396. ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
  1397. ctx->colorspace = V4L2_COLORSPACE_REC709;
  1398. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  1399. if (IS_ERR(ctx->fh.m2m_ctx)) {
  1400. rc = PTR_ERR(ctx->fh.m2m_ctx);
  1401. v4l2_ctrl_handler_free(hdl);
  1402. v4l2_fh_exit(&ctx->fh);
  1403. kfree(ctx);
  1404. goto open_unlock;
  1405. }
  1406. v4l2_fh_add(&ctx->fh);
  1407. atomic_inc(&dev->num_inst);
  1408. dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
  1409. ctx, ctx->fh.m2m_ctx);
  1410. open_unlock:
  1411. mutex_unlock(&dev->dev_mutex);
  1412. return rc;
  1413. }
  1414. static int pxp_release(struct file *file)
  1415. {
  1416. struct pxp_dev *dev = video_drvdata(file);
  1417. struct pxp_ctx *ctx = file2ctx(file);
  1418. dprintk(dev, "Releasing instance %p\n", ctx);
  1419. v4l2_fh_del(&ctx->fh);
  1420. v4l2_fh_exit(&ctx->fh);
  1421. v4l2_ctrl_handler_free(&ctx->hdl);
  1422. mutex_lock(&dev->dev_mutex);
  1423. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1424. mutex_unlock(&dev->dev_mutex);
  1425. kfree(ctx);
  1426. atomic_dec(&dev->num_inst);
  1427. return 0;
  1428. }
  1429. static const struct v4l2_file_operations pxp_fops = {
  1430. .owner = THIS_MODULE,
  1431. .open = pxp_open,
  1432. .release = pxp_release,
  1433. .poll = v4l2_m2m_fop_poll,
  1434. .unlocked_ioctl = video_ioctl2,
  1435. .mmap = v4l2_m2m_fop_mmap,
  1436. };
  1437. static const struct video_device pxp_videodev = {
  1438. .name = MEM2MEM_NAME,
  1439. .vfl_dir = VFL_DIR_M2M,
  1440. .fops = &pxp_fops,
  1441. .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
  1442. .ioctl_ops = &pxp_ioctl_ops,
  1443. .minor = -1,
  1444. .release = video_device_release_empty,
  1445. };
  1446. static const struct v4l2_m2m_ops m2m_ops = {
  1447. .device_run = pxp_device_run,
  1448. .job_ready = pxp_job_ready,
  1449. .job_abort = pxp_job_abort,
  1450. };
  1451. static int pxp_soft_reset(struct pxp_dev *dev)
  1452. {
  1453. int ret;
  1454. u32 val;
  1455. writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_CLR);
  1456. writel(BM_PXP_CTRL_CLKGATE, dev->mmio + HW_PXP_CTRL_CLR);
  1457. writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_SET);
  1458. ret = readl_poll_timeout(dev->mmio + HW_PXP_CTRL, val,
  1459. val & BM_PXP_CTRL_CLKGATE, 0, 100);
  1460. if (ret < 0)
  1461. return ret;
  1462. writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_CLR);
  1463. writel(BM_PXP_CTRL_CLKGATE, dev->mmio + HW_PXP_CTRL_CLR);
  1464. return 0;
  1465. }
  1466. static int pxp_probe(struct platform_device *pdev)
  1467. {
  1468. struct pxp_dev *dev;
  1469. struct video_device *vfd;
  1470. int irq;
  1471. int ret;
  1472. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1473. if (!dev)
  1474. return -ENOMEM;
  1475. dev->clk = devm_clk_get(&pdev->dev, "axi");
  1476. if (IS_ERR(dev->clk)) {
  1477. ret = PTR_ERR(dev->clk);
  1478. dev_err(&pdev->dev, "Failed to get clk: %d\n", ret);
  1479. return ret;
  1480. }
  1481. dev->mmio = devm_platform_ioremap_resource(pdev, 0);
  1482. if (IS_ERR(dev->mmio))
  1483. return PTR_ERR(dev->mmio);
  1484. irq = platform_get_irq(pdev, 0);
  1485. if (irq < 0)
  1486. return irq;
  1487. spin_lock_init(&dev->irqlock);
  1488. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, pxp_irq_handler,
  1489. IRQF_ONESHOT, dev_name(&pdev->dev), dev);
  1490. if (ret < 0) {
  1491. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  1492. return ret;
  1493. }
  1494. ret = clk_prepare_enable(dev->clk);
  1495. if (ret < 0)
  1496. return ret;
  1497. ret = pxp_soft_reset(dev);
  1498. if (ret < 0) {
  1499. dev_err(&pdev->dev, "PXP reset timeout: %d\n", ret);
  1500. goto err_clk;
  1501. }
  1502. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1503. if (ret)
  1504. goto err_clk;
  1505. atomic_set(&dev->num_inst, 0);
  1506. mutex_init(&dev->dev_mutex);
  1507. dev->vfd = pxp_videodev;
  1508. vfd = &dev->vfd;
  1509. vfd->lock = &dev->dev_mutex;
  1510. vfd->v4l2_dev = &dev->v4l2_dev;
  1511. video_set_drvdata(vfd, dev);
  1512. snprintf(vfd->name, sizeof(vfd->name), "%s", pxp_videodev.name);
  1513. v4l2_info(&dev->v4l2_dev,
  1514. "Device registered as /dev/video%d\n", vfd->num);
  1515. platform_set_drvdata(pdev, dev);
  1516. dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
  1517. if (IS_ERR(dev->m2m_dev)) {
  1518. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  1519. ret = PTR_ERR(dev->m2m_dev);
  1520. goto err_v4l2;
  1521. }
  1522. ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
  1523. if (ret) {
  1524. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1525. goto err_m2m;
  1526. }
  1527. return 0;
  1528. err_m2m:
  1529. v4l2_m2m_release(dev->m2m_dev);
  1530. err_v4l2:
  1531. v4l2_device_unregister(&dev->v4l2_dev);
  1532. err_clk:
  1533. clk_disable_unprepare(dev->clk);
  1534. return ret;
  1535. }
  1536. static int pxp_remove(struct platform_device *pdev)
  1537. {
  1538. struct pxp_dev *dev = platform_get_drvdata(pdev);
  1539. writel(BM_PXP_CTRL_CLKGATE, dev->mmio + HW_PXP_CTRL_SET);
  1540. writel(BM_PXP_CTRL_SFTRST, dev->mmio + HW_PXP_CTRL_SET);
  1541. clk_disable_unprepare(dev->clk);
  1542. v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
  1543. video_unregister_device(&dev->vfd);
  1544. v4l2_m2m_release(dev->m2m_dev);
  1545. v4l2_device_unregister(&dev->v4l2_dev);
  1546. return 0;
  1547. }
  1548. static const struct of_device_id pxp_dt_ids[] = {
  1549. { .compatible = "fsl,imx6ull-pxp", .data = NULL },
  1550. { },
  1551. };
  1552. MODULE_DEVICE_TABLE(of, pxp_dt_ids);
  1553. static struct platform_driver pxp_driver = {
  1554. .probe = pxp_probe,
  1555. .remove = pxp_remove,
  1556. .driver = {
  1557. .name = MEM2MEM_NAME,
  1558. .of_match_table = pxp_dt_ids,
  1559. },
  1560. };
  1561. module_platform_driver(pxp_driver);
  1562. MODULE_DESCRIPTION("i.MX PXP mem2mem scaler/CSC/rotator");
  1563. MODULE_AUTHOR("Philipp Zabel <[email protected]>");
  1564. MODULE_LICENSE("GPL");