imx-pxp.c 54 KB

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