dw100.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DW100 Hardware dewarper
  4. *
  5. * Copyright 2022 NXP
  6. * Author: Xavier Roumegue (xavier.roumegue@oss.nxp.com)
  7. *
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/minmax.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-event.h>
  21. #include <media/v4l2-ioctl.h>
  22. #include <media/v4l2-mem2mem.h>
  23. #include <media/videobuf2-dma-contig.h>
  24. #include <uapi/linux/dw100.h>
  25. #include "dw100_regs.h"
  26. #define DRV_NAME "dw100"
  27. #define DW100_MIN_W 176u
  28. #define DW100_MIN_H 144u
  29. #define DW100_MAX_W 4096u
  30. #define DW100_MAX_H 3072u
  31. #define DW100_ALIGN_W 3
  32. #define DW100_ALIGN_H 3
  33. #define DW100_BLOCK_SIZE 16
  34. #define DW100_DEF_W 640u
  35. #define DW100_DEF_H 480u
  36. #define DW100_DEF_LUT_W (DIV_ROUND_UP(DW100_DEF_W, DW100_BLOCK_SIZE) + 1)
  37. #define DW100_DEF_LUT_H (DIV_ROUND_UP(DW100_DEF_H, DW100_BLOCK_SIZE) + 1)
  38. /*
  39. * 16 controls have been reserved for this driver for future extension, but
  40. * let's limit the related driver allocation to the effective number of controls
  41. * in use.
  42. */
  43. #define DW100_MAX_CTRLS 1
  44. #define DW100_CTRL_DEWARPING_MAP 0
  45. enum {
  46. DW100_QUEUE_SRC = 0,
  47. DW100_QUEUE_DST = 1,
  48. };
  49. enum {
  50. DW100_FMT_CAPTURE = BIT(0),
  51. DW100_FMT_OUTPUT = BIT(1),
  52. };
  53. struct dw100_device {
  54. struct platform_device *pdev;
  55. struct v4l2_m2m_dev *m2m_dev;
  56. struct v4l2_device v4l2_dev;
  57. struct video_device vfd;
  58. struct media_device mdev;
  59. /* Video device lock */
  60. struct mutex vfd_mutex;
  61. void __iomem *mmio;
  62. struct clk_bulk_data *clks;
  63. int num_clks;
  64. struct dentry *debugfs_root;
  65. };
  66. struct dw100_q_data {
  67. struct v4l2_pix_format_mplane pix_fmt;
  68. unsigned int sequence;
  69. const struct dw100_fmt *fmt;
  70. struct v4l2_rect crop;
  71. };
  72. struct dw100_ctx {
  73. struct v4l2_fh fh;
  74. struct dw100_device *dw_dev;
  75. struct v4l2_ctrl_handler hdl;
  76. struct v4l2_ctrl *ctrls[DW100_MAX_CTRLS];
  77. /* per context m2m queue lock */
  78. struct mutex vq_mutex;
  79. /* Look Up Table for pixel remapping */
  80. unsigned int *map;
  81. dma_addr_t map_dma;
  82. size_t map_size;
  83. unsigned int map_width;
  84. unsigned int map_height;
  85. bool user_map_is_set;
  86. /* Source and destination queue data */
  87. struct dw100_q_data q_data[2];
  88. };
  89. static const struct v4l2_frmsize_stepwise dw100_frmsize_stepwise = {
  90. .min_width = DW100_MIN_W,
  91. .min_height = DW100_MIN_H,
  92. .max_width = DW100_MAX_W,
  93. .max_height = DW100_MAX_H,
  94. .step_width = 1UL << DW100_ALIGN_W,
  95. .step_height = 1UL << DW100_ALIGN_H,
  96. };
  97. static const struct dw100_fmt {
  98. u32 fourcc;
  99. u32 types;
  100. u32 reg_format;
  101. bool reg_swap_uv;
  102. } formats[] = {
  103. {
  104. .fourcc = V4L2_PIX_FMT_NV16,
  105. .types = DW100_FMT_OUTPUT | DW100_FMT_CAPTURE,
  106. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV422_SP,
  107. .reg_swap_uv = false,
  108. }, {
  109. .fourcc = V4L2_PIX_FMT_NV16M,
  110. .types = DW100_FMT_OUTPUT | DW100_FMT_CAPTURE,
  111. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV422_SP,
  112. .reg_swap_uv = false,
  113. }, {
  114. .fourcc = V4L2_PIX_FMT_NV61,
  115. .types = DW100_FMT_CAPTURE,
  116. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV422_SP,
  117. .reg_swap_uv = true,
  118. }, {
  119. .fourcc = V4L2_PIX_FMT_NV61M,
  120. .types = DW100_FMT_CAPTURE,
  121. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV422_SP,
  122. .reg_swap_uv = true,
  123. }, {
  124. .fourcc = V4L2_PIX_FMT_YUYV,
  125. .types = DW100_FMT_OUTPUT | DW100_FMT_CAPTURE,
  126. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED,
  127. .reg_swap_uv = false,
  128. }, {
  129. .fourcc = V4L2_PIX_FMT_UYVY,
  130. .types = DW100_FMT_OUTPUT | DW100_FMT_CAPTURE,
  131. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED,
  132. .reg_swap_uv = true,
  133. }, {
  134. .fourcc = V4L2_PIX_FMT_NV12,
  135. .types = DW100_FMT_OUTPUT | DW100_FMT_CAPTURE,
  136. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV420_SP,
  137. .reg_swap_uv = false,
  138. }, {
  139. .fourcc = V4L2_PIX_FMT_NV12M,
  140. .types = DW100_FMT_OUTPUT | DW100_FMT_CAPTURE,
  141. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV420_SP,
  142. .reg_swap_uv = false,
  143. }, {
  144. .fourcc = V4L2_PIX_FMT_NV21,
  145. .types = DW100_FMT_CAPTURE,
  146. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV420_SP,
  147. .reg_swap_uv = true,
  148. }, {
  149. .fourcc = V4L2_PIX_FMT_NV21M,
  150. .types = DW100_FMT_CAPTURE,
  151. .reg_format = DW100_DEWARP_CTRL_FORMAT_YUV420_SP,
  152. .reg_swap_uv = true,
  153. },
  154. };
  155. static inline int to_dw100_fmt_type(enum v4l2_buf_type type)
  156. {
  157. if (V4L2_TYPE_IS_OUTPUT(type))
  158. return DW100_FMT_OUTPUT;
  159. else
  160. return DW100_FMT_CAPTURE;
  161. }
  162. static const struct dw100_fmt *dw100_find_pixel_format(u32 pixel_format,
  163. int fmt_type)
  164. {
  165. unsigned int i;
  166. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  167. const struct dw100_fmt *fmt = &formats[i];
  168. if (fmt->fourcc == pixel_format && fmt->types & fmt_type)
  169. return fmt;
  170. }
  171. return NULL;
  172. }
  173. static const struct dw100_fmt *dw100_find_format(struct v4l2_format *f)
  174. {
  175. return dw100_find_pixel_format(f->fmt.pix_mp.pixelformat,
  176. to_dw100_fmt_type(f->type));
  177. }
  178. static inline u32 dw100_read(struct dw100_device *dw_dev, u32 reg)
  179. {
  180. return readl(dw_dev->mmio + reg);
  181. }
  182. static inline void dw100_write(struct dw100_device *dw_dev, u32 reg, u32 val)
  183. {
  184. writel(val, dw_dev->mmio + reg);
  185. }
  186. static inline int dw100_dump_regs(struct seq_file *m)
  187. {
  188. struct dw100_device *dw_dev = m->private;
  189. #define __DECLARE_REG(x) { #x, x }
  190. unsigned int i;
  191. static const struct reg_desc {
  192. const char * const name;
  193. unsigned int addr;
  194. } dw100_regs[] = {
  195. __DECLARE_REG(DW100_DEWARP_ID),
  196. __DECLARE_REG(DW100_DEWARP_CTRL),
  197. __DECLARE_REG(DW100_MAP_LUT_ADDR),
  198. __DECLARE_REG(DW100_MAP_LUT_SIZE),
  199. __DECLARE_REG(DW100_MAP_LUT_ADDR2),
  200. __DECLARE_REG(DW100_MAP_LUT_SIZE2),
  201. __DECLARE_REG(DW100_SRC_IMG_Y_BASE),
  202. __DECLARE_REG(DW100_SRC_IMG_UV_BASE),
  203. __DECLARE_REG(DW100_SRC_IMG_SIZE),
  204. __DECLARE_REG(DW100_SRC_IMG_STRIDE),
  205. __DECLARE_REG(DW100_DST_IMG_Y_BASE),
  206. __DECLARE_REG(DW100_DST_IMG_UV_BASE),
  207. __DECLARE_REG(DW100_DST_IMG_SIZE),
  208. __DECLARE_REG(DW100_DST_IMG_STRIDE),
  209. __DECLARE_REG(DW100_DST_IMG_Y_SIZE1),
  210. __DECLARE_REG(DW100_DST_IMG_UV_SIZE1),
  211. __DECLARE_REG(DW100_SRC_IMG_Y_BASE2),
  212. __DECLARE_REG(DW100_SRC_IMG_UV_BASE2),
  213. __DECLARE_REG(DW100_SRC_IMG_SIZE2),
  214. __DECLARE_REG(DW100_SRC_IMG_STRIDE2),
  215. __DECLARE_REG(DW100_DST_IMG_Y_BASE2),
  216. __DECLARE_REG(DW100_DST_IMG_UV_BASE2),
  217. __DECLARE_REG(DW100_DST_IMG_SIZE2),
  218. __DECLARE_REG(DW100_DST_IMG_STRIDE2),
  219. __DECLARE_REG(DW100_DST_IMG_Y_SIZE2),
  220. __DECLARE_REG(DW100_DST_IMG_UV_SIZE2),
  221. __DECLARE_REG(DW100_SWAP_CONTROL),
  222. __DECLARE_REG(DW100_VERTICAL_SPLIT_LINE),
  223. __DECLARE_REG(DW100_HORIZON_SPLIT_LINE),
  224. __DECLARE_REG(DW100_SCALE_FACTOR),
  225. __DECLARE_REG(DW100_ROI_START),
  226. __DECLARE_REG(DW100_BOUNDARY_PIXEL),
  227. __DECLARE_REG(DW100_INTERRUPT_STATUS),
  228. __DECLARE_REG(DW100_BUS_CTRL),
  229. __DECLARE_REG(DW100_BUS_CTRL1),
  230. __DECLARE_REG(DW100_BUS_TIME_OUT_CYCLE),
  231. };
  232. for (i = 0; i < ARRAY_SIZE(dw100_regs); i++)
  233. seq_printf(m, "%s: %#x\n", dw100_regs[i].name,
  234. dw100_read(dw_dev, dw100_regs[i].addr));
  235. return 0;
  236. }
  237. static inline struct dw100_ctx *dw100_file2ctx(struct file *file)
  238. {
  239. return container_of(file_to_v4l2_fh(file), struct dw100_ctx, fh);
  240. }
  241. static struct dw100_q_data *dw100_get_q_data(struct dw100_ctx *ctx,
  242. enum v4l2_buf_type type)
  243. {
  244. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  245. return &ctx->q_data[DW100_QUEUE_SRC];
  246. else
  247. return &ctx->q_data[DW100_QUEUE_DST];
  248. }
  249. static u32 dw100_get_n_vertices_from_length(u32 length)
  250. {
  251. return DIV_ROUND_UP(length, DW100_BLOCK_SIZE) + 1;
  252. }
  253. static u16 dw100_map_convert_to_uq12_4(u32 a)
  254. {
  255. return (u16)((a & 0xfff) << 4);
  256. }
  257. static u32 dw100_map_format_coordinates(u16 xq, u16 yq)
  258. {
  259. return (u32)((yq << 16) | xq);
  260. }
  261. static u32 *dw100_get_user_map(struct dw100_ctx *ctx)
  262. {
  263. struct v4l2_ctrl *ctrl = ctx->ctrls[DW100_CTRL_DEWARPING_MAP];
  264. return ctrl->p_cur.p_u32;
  265. }
  266. /*
  267. * Create the dewarp map used by the hardware from the V4L2 control values which
  268. * have been initialized with an identity map or set by the application.
  269. */
  270. static int dw100_create_mapping(struct dw100_ctx *ctx)
  271. {
  272. u32 *user_map;
  273. if (ctx->map)
  274. dma_free_coherent(&ctx->dw_dev->pdev->dev, ctx->map_size,
  275. ctx->map, ctx->map_dma);
  276. ctx->map = dma_alloc_coherent(&ctx->dw_dev->pdev->dev, ctx->map_size,
  277. &ctx->map_dma, GFP_KERNEL);
  278. if (!ctx->map)
  279. return -ENOMEM;
  280. user_map = dw100_get_user_map(ctx);
  281. memcpy(ctx->map, user_map, ctx->map_size);
  282. dev_dbg(&ctx->dw_dev->pdev->dev,
  283. "%ux%u %s mapping created (d:%pad-c:%p) for stream %ux%u->%ux%u\n",
  284. ctx->map_width, ctx->map_height,
  285. ctx->user_map_is_set ? "user" : "identity",
  286. &ctx->map_dma, ctx->map,
  287. ctx->q_data[DW100_QUEUE_SRC].pix_fmt.width,
  288. ctx->q_data[DW100_QUEUE_DST].pix_fmt.height,
  289. ctx->q_data[DW100_QUEUE_SRC].pix_fmt.width,
  290. ctx->q_data[DW100_QUEUE_DST].pix_fmt.height);
  291. return 0;
  292. }
  293. static void dw100_destroy_mapping(struct dw100_ctx *ctx)
  294. {
  295. if (ctx->map) {
  296. dma_free_coherent(&ctx->dw_dev->pdev->dev, ctx->map_size,
  297. ctx->map, ctx->map_dma);
  298. ctx->map = NULL;
  299. }
  300. }
  301. static int dw100_s_ctrl(struct v4l2_ctrl *ctrl)
  302. {
  303. struct dw100_ctx *ctx =
  304. container_of(ctrl->handler, struct dw100_ctx, hdl);
  305. switch (ctrl->id) {
  306. case V4L2_CID_DW100_DEWARPING_16x16_VERTEX_MAP:
  307. ctx->user_map_is_set = true;
  308. break;
  309. }
  310. return 0;
  311. }
  312. static const struct v4l2_ctrl_ops dw100_ctrl_ops = {
  313. .s_ctrl = dw100_s_ctrl,
  314. };
  315. /*
  316. * Initialize the dewarping map with an identity mapping.
  317. *
  318. * A 16 pixels cell size grid is mapped on the destination image.
  319. * The last cells width/height might be lesser than 16 if the destination image
  320. * width/height is not divisible by 16. This dewarping grid map specifies the
  321. * source image pixel location (x, y) on each grid intersection point.
  322. * Bilinear interpolation is used to compute inner cell points locations.
  323. *
  324. * The coordinates are saved in UQ12.4 fixed point format.
  325. */
  326. static void dw100_ctrl_dewarping_map_init(const struct v4l2_ctrl *ctrl,
  327. u32 from_idx,
  328. union v4l2_ctrl_ptr ptr)
  329. {
  330. struct dw100_ctx *ctx =
  331. container_of(ctrl->handler, struct dw100_ctx, hdl);
  332. u32 sw, sh, mw, mh, idx;
  333. u16 qx, qy, qdx, qdy, qsh, qsw;
  334. u32 *map = ctrl->p_cur.p_u32;
  335. sw = ctx->q_data[DW100_QUEUE_SRC].pix_fmt.width;
  336. sh = ctx->q_data[DW100_QUEUE_SRC].pix_fmt.height;
  337. mw = ctrl->dims[0];
  338. mh = ctrl->dims[1];
  339. qsw = dw100_map_convert_to_uq12_4(sw);
  340. qsh = dw100_map_convert_to_uq12_4(sh);
  341. qdx = qsw / (mw - 1);
  342. qdy = qsh / (mh - 1);
  343. ctx->map_width = mw;
  344. ctx->map_height = mh;
  345. ctx->map_size = mh * mw * sizeof(u32);
  346. for (idx = from_idx; idx < ctrl->elems; idx++) {
  347. qy = min_t(u32, (idx / mw) * qdy, qsh);
  348. qx = min_t(u32, (idx % mw) * qdx, qsw);
  349. map[idx] = dw100_map_format_coordinates(qx, qy);
  350. }
  351. ctx->user_map_is_set = false;
  352. }
  353. static const struct v4l2_ctrl_type_ops dw100_ctrl_type_ops = {
  354. .init = dw100_ctrl_dewarping_map_init,
  355. .validate = v4l2_ctrl_type_op_validate,
  356. .log = v4l2_ctrl_type_op_log,
  357. .equal = v4l2_ctrl_type_op_equal,
  358. };
  359. static const struct v4l2_ctrl_config controls[] = {
  360. [DW100_CTRL_DEWARPING_MAP] = {
  361. .ops = &dw100_ctrl_ops,
  362. .type_ops = &dw100_ctrl_type_ops,
  363. .id = V4L2_CID_DW100_DEWARPING_16x16_VERTEX_MAP,
  364. .name = "Dewarping Vertex Map",
  365. .type = V4L2_CTRL_TYPE_U32,
  366. .min = 0x00000000,
  367. .max = 0xffffffff,
  368. .step = 1,
  369. .def = 0,
  370. .dims = { DW100_DEF_LUT_W, DW100_DEF_LUT_H },
  371. },
  372. };
  373. static int dw100_queue_setup(struct vb2_queue *vq,
  374. unsigned int *nbuffers, unsigned int *nplanes,
  375. unsigned int sizes[], struct device *alloc_devs[])
  376. {
  377. struct dw100_ctx *ctx = vb2_get_drv_priv(vq);
  378. const struct v4l2_pix_format_mplane *format;
  379. unsigned int i;
  380. format = &dw100_get_q_data(ctx, vq->type)->pix_fmt;
  381. if (*nplanes) {
  382. if (*nplanes != format->num_planes)
  383. return -EINVAL;
  384. for (i = 0; i < *nplanes; ++i) {
  385. if (sizes[i] < format->plane_fmt[i].sizeimage)
  386. return -EINVAL;
  387. }
  388. return 0;
  389. }
  390. *nplanes = format->num_planes;
  391. for (i = 0; i < format->num_planes; ++i)
  392. sizes[i] = format->plane_fmt[i].sizeimage;
  393. return 0;
  394. }
  395. static int dw100_buf_prepare(struct vb2_buffer *vb)
  396. {
  397. unsigned int i;
  398. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  399. struct dw100_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  400. struct dw100_device *dw_dev = ctx->dw_dev;
  401. const struct v4l2_pix_format_mplane *pix_fmt =
  402. &dw100_get_q_data(ctx, vb->vb2_queue->type)->pix_fmt;
  403. if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
  404. if (vbuf->field != V4L2_FIELD_NONE) {
  405. dev_dbg(&dw_dev->pdev->dev, "%x field isn't supported\n",
  406. vbuf->field);
  407. return -EINVAL;
  408. }
  409. }
  410. for (i = 0; i < pix_fmt->num_planes; i++) {
  411. unsigned long size = pix_fmt->plane_fmt[i].sizeimage;
  412. if (vb2_plane_size(vb, i) < size) {
  413. dev_dbg(&dw_dev->pdev->dev,
  414. "User buffer too small (%lu < %lu)\n",
  415. vb2_plane_size(vb, i), size);
  416. return -EINVAL;
  417. }
  418. vb2_set_plane_payload(vb, i, size);
  419. }
  420. return 0;
  421. }
  422. static void dw100_buf_queue(struct vb2_buffer *vb)
  423. {
  424. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  425. struct dw100_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  426. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  427. }
  428. static void dw100_return_all_buffers(struct vb2_queue *q,
  429. enum vb2_buffer_state state)
  430. {
  431. struct dw100_ctx *ctx = vb2_get_drv_priv(q);
  432. struct vb2_v4l2_buffer *vbuf;
  433. for (;;) {
  434. if (V4L2_TYPE_IS_OUTPUT(q->type))
  435. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  436. else
  437. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  438. if (!vbuf)
  439. return;
  440. v4l2_m2m_buf_done(vbuf, state);
  441. }
  442. }
  443. static int dw100_start_streaming(struct vb2_queue *q, unsigned int count)
  444. {
  445. struct dw100_ctx *ctx = vb2_get_drv_priv(q);
  446. struct dw100_q_data *q_data = dw100_get_q_data(ctx, q->type);
  447. int ret;
  448. q_data->sequence = 0;
  449. ret = dw100_create_mapping(ctx);
  450. if (ret)
  451. goto err;
  452. ret = pm_runtime_resume_and_get(&ctx->dw_dev->pdev->dev);
  453. if (ret) {
  454. dw100_destroy_mapping(ctx);
  455. goto err;
  456. }
  457. return 0;
  458. err:
  459. dw100_return_all_buffers(q, VB2_BUF_STATE_QUEUED);
  460. return ret;
  461. }
  462. static void dw100_stop_streaming(struct vb2_queue *q)
  463. {
  464. struct dw100_ctx *ctx = vb2_get_drv_priv(q);
  465. dw100_return_all_buffers(q, VB2_BUF_STATE_ERROR);
  466. pm_runtime_put_sync(&ctx->dw_dev->pdev->dev);
  467. dw100_destroy_mapping(ctx);
  468. }
  469. static const struct vb2_ops dw100_qops = {
  470. .queue_setup = dw100_queue_setup,
  471. .buf_prepare = dw100_buf_prepare,
  472. .buf_queue = dw100_buf_queue,
  473. .start_streaming = dw100_start_streaming,
  474. .stop_streaming = dw100_stop_streaming,
  475. };
  476. static int dw100_m2m_queue_init(void *priv, struct vb2_queue *src_vq,
  477. struct vb2_queue *dst_vq)
  478. {
  479. struct dw100_ctx *ctx = priv;
  480. int ret;
  481. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  482. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  483. src_vq->drv_priv = ctx;
  484. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  485. src_vq->ops = &dw100_qops;
  486. src_vq->mem_ops = &vb2_dma_contig_memops;
  487. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  488. src_vq->lock = &ctx->vq_mutex;
  489. src_vq->dev = ctx->dw_dev->v4l2_dev.dev;
  490. ret = vb2_queue_init(src_vq);
  491. if (ret)
  492. return ret;
  493. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  494. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  495. dst_vq->drv_priv = ctx;
  496. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  497. dst_vq->ops = &dw100_qops;
  498. dst_vq->mem_ops = &vb2_dma_contig_memops;
  499. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  500. dst_vq->lock = &ctx->vq_mutex;
  501. dst_vq->dev = ctx->dw_dev->v4l2_dev.dev;
  502. return vb2_queue_init(dst_vq);
  503. }
  504. static int dw100_open(struct file *file)
  505. {
  506. struct dw100_device *dw_dev = video_drvdata(file);
  507. struct dw100_ctx *ctx;
  508. struct v4l2_ctrl_handler *hdl;
  509. struct v4l2_pix_format_mplane *pix_fmt;
  510. int ret, i;
  511. ctx = kzalloc_obj(*ctx);
  512. if (!ctx)
  513. return -ENOMEM;
  514. mutex_init(&ctx->vq_mutex);
  515. v4l2_fh_init(&ctx->fh, video_devdata(file));
  516. ctx->dw_dev = dw_dev;
  517. ctx->q_data[DW100_QUEUE_SRC].fmt = &formats[0];
  518. pix_fmt = &ctx->q_data[DW100_QUEUE_SRC].pix_fmt;
  519. pix_fmt->field = V4L2_FIELD_NONE;
  520. pix_fmt->colorspace = V4L2_COLORSPACE_REC709;
  521. pix_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix_fmt->colorspace);
  522. pix_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix_fmt->colorspace);
  523. pix_fmt->quantization =
  524. V4L2_MAP_QUANTIZATION_DEFAULT(false, pix_fmt->colorspace,
  525. pix_fmt->ycbcr_enc);
  526. v4l2_fill_pixfmt_mp(pix_fmt, formats[0].fourcc, DW100_DEF_W, DW100_DEF_H);
  527. ctx->q_data[DW100_QUEUE_SRC].crop.top = 0;
  528. ctx->q_data[DW100_QUEUE_SRC].crop.left = 0;
  529. ctx->q_data[DW100_QUEUE_SRC].crop.width = DW100_DEF_W;
  530. ctx->q_data[DW100_QUEUE_SRC].crop.height = DW100_DEF_H;
  531. ctx->q_data[DW100_QUEUE_DST] = ctx->q_data[DW100_QUEUE_SRC];
  532. hdl = &ctx->hdl;
  533. v4l2_ctrl_handler_init(hdl, ARRAY_SIZE(controls));
  534. for (i = 0; i < ARRAY_SIZE(controls); i++) {
  535. ctx->ctrls[i] = v4l2_ctrl_new_custom(hdl, &controls[i], NULL);
  536. if (hdl->error) {
  537. dev_err(&ctx->dw_dev->pdev->dev,
  538. "Adding control (%d) failed\n", i);
  539. ret = hdl->error;
  540. goto err;
  541. }
  542. }
  543. ctx->fh.ctrl_handler = hdl;
  544. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dw_dev->m2m_dev,
  545. ctx, &dw100_m2m_queue_init);
  546. if (IS_ERR(ctx->fh.m2m_ctx)) {
  547. ret = PTR_ERR(ctx->fh.m2m_ctx);
  548. goto err;
  549. }
  550. v4l2_fh_add(&ctx->fh, file);
  551. return 0;
  552. err:
  553. v4l2_ctrl_handler_free(hdl);
  554. v4l2_fh_exit(&ctx->fh);
  555. mutex_destroy(&ctx->vq_mutex);
  556. kfree(ctx);
  557. return ret;
  558. }
  559. static int dw100_release(struct file *file)
  560. {
  561. struct dw100_ctx *ctx = dw100_file2ctx(file);
  562. v4l2_fh_del(&ctx->fh, file);
  563. v4l2_fh_exit(&ctx->fh);
  564. v4l2_ctrl_handler_free(&ctx->hdl);
  565. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  566. mutex_destroy(&ctx->vq_mutex);
  567. kfree(ctx);
  568. return 0;
  569. }
  570. static const struct v4l2_file_operations dw100_fops = {
  571. .owner = THIS_MODULE,
  572. .open = dw100_open,
  573. .release = dw100_release,
  574. .poll = v4l2_m2m_fop_poll,
  575. .unlocked_ioctl = video_ioctl2,
  576. .mmap = v4l2_m2m_fop_mmap,
  577. };
  578. static int dw100_querycap(struct file *file, void *priv,
  579. struct v4l2_capability *cap)
  580. {
  581. strscpy(cap->driver, DRV_NAME, sizeof(cap->driver));
  582. strscpy(cap->card, "DW100 dewarper", sizeof(cap->card));
  583. return 0;
  584. }
  585. static int dw100_enum_fmt_vid(struct file *file, void *priv,
  586. struct v4l2_fmtdesc *f)
  587. {
  588. int i, num = 0;
  589. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  590. if (formats[i].types & to_dw100_fmt_type(f->type)) {
  591. if (num == f->index) {
  592. f->pixelformat = formats[i].fourcc;
  593. return 0;
  594. }
  595. ++num;
  596. }
  597. }
  598. return -EINVAL;
  599. }
  600. static int dw100_enum_framesizes(struct file *file, void *priv,
  601. struct v4l2_frmsizeenum *fsize)
  602. {
  603. const struct dw100_fmt *fmt;
  604. if (fsize->index)
  605. return -EINVAL;
  606. fmt = dw100_find_pixel_format(fsize->pixel_format,
  607. DW100_FMT_OUTPUT | DW100_FMT_CAPTURE);
  608. if (!fmt)
  609. return -EINVAL;
  610. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  611. fsize->stepwise = dw100_frmsize_stepwise;
  612. return 0;
  613. }
  614. static int dw100_g_fmt_vid(struct file *file, void *priv, struct v4l2_format *f)
  615. {
  616. struct dw100_ctx *ctx = dw100_file2ctx(file);
  617. struct dw100_q_data *q_data;
  618. q_data = dw100_get_q_data(ctx, f->type);
  619. f->fmt.pix_mp = q_data->pix_fmt;
  620. return 0;
  621. }
  622. static int dw100_try_fmt(struct file *file, struct v4l2_format *f)
  623. {
  624. struct dw100_ctx *ctx = dw100_file2ctx(file);
  625. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  626. const struct dw100_fmt *fmt;
  627. fmt = dw100_find_format(f);
  628. if (!fmt) {
  629. fmt = &formats[0];
  630. pix->pixelformat = fmt->fourcc;
  631. }
  632. v4l2_apply_frmsize_constraints(&pix->width, &pix->height,
  633. &dw100_frmsize_stepwise);
  634. v4l2_fill_pixfmt_mp(pix, fmt->fourcc, pix->width, pix->height);
  635. pix->field = V4L2_FIELD_NONE;
  636. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  637. if (pix->colorspace == V4L2_COLORSPACE_DEFAULT)
  638. pix->colorspace = V4L2_COLORSPACE_REC709;
  639. if (pix->xfer_func == V4L2_XFER_FUNC_DEFAULT)
  640. pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
  641. if (pix->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
  642. pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
  643. if (pix->quantization == V4L2_QUANTIZATION_DEFAULT)
  644. pix->quantization =
  645. V4L2_MAP_QUANTIZATION_DEFAULT(false,
  646. pix->colorspace,
  647. pix->ycbcr_enc);
  648. } else {
  649. /*
  650. * The DW100 can't perform colorspace conversion, the colorspace
  651. * on the capture queue must be identical to the output queue.
  652. */
  653. const struct dw100_q_data *q_data =
  654. dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  655. pix->colorspace = q_data->pix_fmt.colorspace;
  656. pix->xfer_func = q_data->pix_fmt.xfer_func;
  657. pix->ycbcr_enc = q_data->pix_fmt.ycbcr_enc;
  658. pix->quantization = q_data->pix_fmt.quantization;
  659. }
  660. return 0;
  661. }
  662. static int dw100_s_fmt(struct dw100_ctx *ctx, struct v4l2_format *f)
  663. {
  664. struct dw100_q_data *q_data;
  665. struct vb2_queue *vq;
  666. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  667. q_data = dw100_get_q_data(ctx, f->type);
  668. if (!q_data)
  669. return -EINVAL;
  670. if (vb2_is_busy(vq)) {
  671. dev_dbg(&ctx->dw_dev->pdev->dev, "%s queue busy\n", __func__);
  672. return -EBUSY;
  673. }
  674. q_data->fmt = dw100_find_format(f);
  675. q_data->pix_fmt = f->fmt.pix_mp;
  676. q_data->crop.top = 0;
  677. q_data->crop.left = 0;
  678. q_data->crop.width = f->fmt.pix_mp.width;
  679. q_data->crop.height = f->fmt.pix_mp.height;
  680. /* Propagate buffers encoding */
  681. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  682. struct dw100_q_data *dst_q_data =
  683. dw100_get_q_data(ctx,
  684. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  685. dst_q_data->pix_fmt.colorspace = q_data->pix_fmt.colorspace;
  686. dst_q_data->pix_fmt.ycbcr_enc = q_data->pix_fmt.ycbcr_enc;
  687. dst_q_data->pix_fmt.quantization = q_data->pix_fmt.quantization;
  688. dst_q_data->pix_fmt.xfer_func = q_data->pix_fmt.xfer_func;
  689. }
  690. dev_dbg(&ctx->dw_dev->pdev->dev,
  691. "Setting format for type %u, wxh: %ux%u, fmt: %p4cc\n",
  692. f->type, q_data->pix_fmt.width, q_data->pix_fmt.height,
  693. &q_data->pix_fmt.pixelformat);
  694. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  695. int ret;
  696. u32 dims[V4L2_CTRL_MAX_DIMS] = {};
  697. struct v4l2_ctrl *ctrl = ctx->ctrls[DW100_CTRL_DEWARPING_MAP];
  698. dims[0] = dw100_get_n_vertices_from_length(q_data->pix_fmt.width);
  699. dims[1] = dw100_get_n_vertices_from_length(q_data->pix_fmt.height);
  700. ret = v4l2_ctrl_modify_dimensions(ctrl, dims);
  701. if (ret) {
  702. dev_err(&ctx->dw_dev->pdev->dev,
  703. "Modifying LUT dimensions failed with error %d\n",
  704. ret);
  705. return ret;
  706. }
  707. }
  708. return 0;
  709. }
  710. static int dw100_try_fmt_vid_cap(struct file *file, void *priv,
  711. struct v4l2_format *f)
  712. {
  713. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  714. return -EINVAL;
  715. return dw100_try_fmt(file, f);
  716. }
  717. static int dw100_s_fmt_vid_cap(struct file *file, void *priv,
  718. struct v4l2_format *f)
  719. {
  720. struct dw100_ctx *ctx = dw100_file2ctx(file);
  721. int ret;
  722. ret = dw100_try_fmt_vid_cap(file, priv, f);
  723. if (ret)
  724. return ret;
  725. ret = dw100_s_fmt(ctx, f);
  726. if (ret)
  727. return ret;
  728. return 0;
  729. }
  730. static int dw100_try_fmt_vid_out(struct file *file, void *priv,
  731. struct v4l2_format *f)
  732. {
  733. if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  734. return -EINVAL;
  735. return dw100_try_fmt(file, f);
  736. }
  737. static int dw100_s_fmt_vid_out(struct file *file, void *priv,
  738. struct v4l2_format *f)
  739. {
  740. struct dw100_ctx *ctx = dw100_file2ctx(file);
  741. int ret;
  742. ret = dw100_try_fmt_vid_out(file, priv, f);
  743. if (ret)
  744. return ret;
  745. ret = dw100_s_fmt(ctx, f);
  746. if (ret)
  747. return ret;
  748. return 0;
  749. }
  750. static int dw100_g_selection(struct file *file, void *fh,
  751. struct v4l2_selection *sel)
  752. {
  753. struct dw100_ctx *ctx = dw100_file2ctx(file);
  754. struct dw100_q_data *src_q_data;
  755. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  756. return -EINVAL;
  757. src_q_data = dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  758. switch (sel->target) {
  759. case V4L2_SEL_TGT_CROP_DEFAULT:
  760. case V4L2_SEL_TGT_CROP_BOUNDS:
  761. sel->r.top = 0;
  762. sel->r.left = 0;
  763. sel->r.width = src_q_data->pix_fmt.width;
  764. sel->r.height = src_q_data->pix_fmt.height;
  765. break;
  766. case V4L2_SEL_TGT_CROP:
  767. sel->r.top = src_q_data->crop.top;
  768. sel->r.left = src_q_data->crop.left;
  769. sel->r.width = src_q_data->crop.width;
  770. sel->r.height = src_q_data->crop.height;
  771. break;
  772. default:
  773. return -EINVAL;
  774. }
  775. return 0;
  776. }
  777. static int dw100_s_selection(struct file *file, void *fh,
  778. struct v4l2_selection *sel)
  779. {
  780. struct dw100_ctx *ctx = dw100_file2ctx(file);
  781. struct dw100_q_data *src_q_data;
  782. u32 qscalex, qscaley, qscale;
  783. int x, y, w, h;
  784. unsigned int wframe, hframe;
  785. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  786. return -EINVAL;
  787. src_q_data = dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  788. dev_dbg(&ctx->dw_dev->pdev->dev,
  789. ">>> Buffer Type: %u Target: %u Rect: (%d,%d)/%ux%u\n",
  790. sel->type, sel->target,
  791. sel->r.left, sel->r.top, sel->r.width, sel->r.height);
  792. switch (sel->target) {
  793. case V4L2_SEL_TGT_CROP:
  794. wframe = src_q_data->pix_fmt.width;
  795. hframe = src_q_data->pix_fmt.height;
  796. sel->r.top = clamp_t(int, sel->r.top, 0, hframe - DW100_MIN_H);
  797. sel->r.left = clamp_t(int, sel->r.left, 0, wframe - DW100_MIN_W);
  798. sel->r.height =
  799. clamp(sel->r.height, DW100_MIN_H, hframe - sel->r.top);
  800. sel->r.width =
  801. clamp(sel->r.width, DW100_MIN_W, wframe - sel->r.left);
  802. /* UQ16.16 for float operations */
  803. qscalex = (sel->r.width << 16) / wframe;
  804. qscaley = (sel->r.height << 16) / hframe;
  805. y = sel->r.top;
  806. x = sel->r.left;
  807. if (qscalex == qscaley) {
  808. qscale = qscalex;
  809. } else {
  810. switch (sel->flags) {
  811. case 0:
  812. qscale = (qscalex + qscaley) / 2;
  813. break;
  814. case V4L2_SEL_FLAG_GE:
  815. qscale = max(qscaley, qscalex);
  816. break;
  817. case V4L2_SEL_FLAG_LE:
  818. qscale = min(qscaley, qscalex);
  819. break;
  820. case V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE:
  821. return -ERANGE;
  822. default:
  823. return -EINVAL;
  824. }
  825. }
  826. w = (u32)((((u64)wframe << 16) * qscale) >> 32);
  827. h = (u32)((((u64)hframe << 16) * qscale) >> 32);
  828. x = x + (sel->r.width - w) / 2;
  829. y = y + (sel->r.height - h) / 2;
  830. x = min(wframe - w, (unsigned int)max(0, x));
  831. y = min(hframe - h, (unsigned int)max(0, y));
  832. sel->r.top = y;
  833. sel->r.left = x;
  834. sel->r.width = w;
  835. sel->r.height = h;
  836. src_q_data->crop.top = sel->r.top;
  837. src_q_data->crop.left = sel->r.left;
  838. src_q_data->crop.width = sel->r.width;
  839. src_q_data->crop.height = sel->r.height;
  840. break;
  841. default:
  842. return -EINVAL;
  843. }
  844. dev_dbg(&ctx->dw_dev->pdev->dev,
  845. "<<< Buffer Type: %u Target: %u Rect: (%d,%d)/%ux%u\n",
  846. sel->type, sel->target,
  847. sel->r.left, sel->r.top, sel->r.width, sel->r.height);
  848. return 0;
  849. }
  850. static const struct v4l2_ioctl_ops dw100_ioctl_ops = {
  851. .vidioc_querycap = dw100_querycap,
  852. .vidioc_enum_fmt_vid_cap = dw100_enum_fmt_vid,
  853. .vidioc_enum_framesizes = dw100_enum_framesizes,
  854. .vidioc_g_fmt_vid_cap_mplane = dw100_g_fmt_vid,
  855. .vidioc_try_fmt_vid_cap_mplane = dw100_try_fmt_vid_cap,
  856. .vidioc_s_fmt_vid_cap_mplane = dw100_s_fmt_vid_cap,
  857. .vidioc_enum_fmt_vid_out = dw100_enum_fmt_vid,
  858. .vidioc_g_fmt_vid_out_mplane = dw100_g_fmt_vid,
  859. .vidioc_try_fmt_vid_out_mplane = dw100_try_fmt_vid_out,
  860. .vidioc_s_fmt_vid_out_mplane = dw100_s_fmt_vid_out,
  861. .vidioc_g_selection = dw100_g_selection,
  862. .vidioc_s_selection = dw100_s_selection,
  863. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  864. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  865. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  866. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  867. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  868. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  869. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  870. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  871. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  872. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  873. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  874. };
  875. static void dw100_job_finish(struct dw100_device *dw_dev, bool with_error)
  876. {
  877. struct dw100_ctx *curr_ctx;
  878. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  879. enum vb2_buffer_state buf_state;
  880. curr_ctx = v4l2_m2m_get_curr_priv(dw_dev->m2m_dev);
  881. if (!curr_ctx) {
  882. dev_err(&dw_dev->pdev->dev,
  883. "Instance released before the end of transaction\n");
  884. return;
  885. }
  886. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  887. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  888. if (likely(!with_error))
  889. buf_state = VB2_BUF_STATE_DONE;
  890. else
  891. buf_state = VB2_BUF_STATE_ERROR;
  892. v4l2_m2m_buf_done(src_vb, buf_state);
  893. v4l2_m2m_buf_done(dst_vb, buf_state);
  894. dev_dbg(&dw_dev->pdev->dev, "Finishing transaction with%s error(s)\n",
  895. with_error ? "" : "out");
  896. v4l2_m2m_job_finish(dw_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  897. }
  898. static void dw100_hw_reset(struct dw100_device *dw_dev)
  899. {
  900. u32 val;
  901. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  902. val |= DW100_DEWARP_CTRL_ENABLE;
  903. val |= DW100_DEWARP_CTRL_SOFT_RESET;
  904. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  905. val &= ~DW100_DEWARP_CTRL_SOFT_RESET;
  906. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  907. }
  908. static void _dw100_hw_set_master_bus_enable(struct dw100_device *dw_dev,
  909. unsigned int enable)
  910. {
  911. u32 val;
  912. dev_dbg(&dw_dev->pdev->dev, "%sable master bus\n",
  913. enable ? "En" : "Dis");
  914. val = dw100_read(dw_dev, DW100_BUS_CTRL);
  915. if (enable)
  916. val |= DW100_BUS_CTRL_AXI_MASTER_ENABLE;
  917. else
  918. val &= ~DW100_BUS_CTRL_AXI_MASTER_ENABLE;
  919. dw100_write(dw_dev, DW100_BUS_CTRL, val);
  920. }
  921. static void dw100_hw_master_bus_enable(struct dw100_device *dw_dev)
  922. {
  923. _dw100_hw_set_master_bus_enable(dw_dev, 1);
  924. }
  925. static void dw100_hw_master_bus_disable(struct dw100_device *dw_dev)
  926. {
  927. _dw100_hw_set_master_bus_enable(dw_dev, 0);
  928. }
  929. static void dw100_hw_dewarp_start(struct dw100_device *dw_dev)
  930. {
  931. u32 val;
  932. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  933. dev_dbg(&dw_dev->pdev->dev, "Starting Hardware CTRL:0x%08x\n", val);
  934. dw100_write(dw_dev, DW100_DEWARP_CTRL, val | DW100_DEWARP_CTRL_START);
  935. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  936. }
  937. static void dw100_hw_init_ctrl(struct dw100_device *dw_dev)
  938. {
  939. u32 val;
  940. /*
  941. * Input format YUV422_SP
  942. * Output format YUV422_SP
  943. * No hardware handshake (SW)
  944. * No automatic double src buffering (Single)
  945. * No automatic double dst buffering (Single)
  946. * No Black Line
  947. * Prefetch image pixel traversal
  948. */
  949. val = DW100_DEWARP_CTRL_ENABLE
  950. /* Valid only for auto prefetch mode*/
  951. | DW100_DEWARP_CTRL_PREFETCH_THRESHOLD(32);
  952. /*
  953. * Calculation mode required to support any scaling factor,
  954. * but x4 slower than traversal mode.
  955. *
  956. * DW100_DEWARP_CTRL_PREFETCH_MODE_TRAVERSAL
  957. * DW100_DEWARP_CTRL_PREFETCH_MODE_CALCULATION
  958. * DW100_DEWARP_CTRL_PREFETCH_MODE_AUTO
  959. *
  960. * TODO: Find heuristics requiring calculation mode
  961. */
  962. val |= DW100_DEWARP_CTRL_PREFETCH_MODE_CALCULATION;
  963. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  964. }
  965. static void dw100_hw_set_pixel_boundary(struct dw100_device *dw_dev)
  966. {
  967. u32 val;
  968. val = DW100_BOUNDARY_PIXEL_V(128)
  969. | DW100_BOUNDARY_PIXEL_U(128)
  970. | DW100_BOUNDARY_PIXEL_Y(0);
  971. dw100_write(dw_dev, DW100_BOUNDARY_PIXEL, val);
  972. }
  973. static void dw100_hw_set_scale(struct dw100_device *dw_dev, u8 scale)
  974. {
  975. dev_dbg(&dw_dev->pdev->dev, "Setting scale factor to %u\n", scale);
  976. dw100_write(dw_dev, DW100_SCALE_FACTOR, scale);
  977. }
  978. static void dw100_hw_set_roi(struct dw100_device *dw_dev, u32 x, u32 y)
  979. {
  980. u32 val;
  981. dev_dbg(&dw_dev->pdev->dev, "Setting ROI region to %u.%u\n", x, y);
  982. val = DW100_ROI_START_X(x) | DW100_ROI_START_Y(y);
  983. dw100_write(dw_dev, DW100_ROI_START, val);
  984. }
  985. static void dw100_hw_set_src_crop(struct dw100_device *dw_dev,
  986. const struct dw100_q_data *src_q_data,
  987. const struct dw100_q_data *dst_q_data)
  988. {
  989. const struct v4l2_rect *rect = &src_q_data->crop;
  990. u32 src_scale, qscale, left_scale, top_scale;
  991. /* HW Scale is UQ1.7 encoded */
  992. src_scale = (rect->width << 7) / src_q_data->pix_fmt.width;
  993. dw100_hw_set_scale(dw_dev, src_scale);
  994. qscale = (dst_q_data->pix_fmt.width << 7) / src_q_data->pix_fmt.width;
  995. left_scale = ((rect->left << 7) * qscale) >> 14;
  996. top_scale = ((rect->top << 7) * qscale) >> 14;
  997. dw100_hw_set_roi(dw_dev, left_scale, top_scale);
  998. }
  999. static void dw100_hw_set_source(struct dw100_device *dw_dev,
  1000. const struct dw100_q_data *q_data,
  1001. struct vb2_buffer *buffer)
  1002. {
  1003. u32 width, height, stride, fourcc, val;
  1004. const struct dw100_fmt *fmt = q_data->fmt;
  1005. dma_addr_t addr_y = vb2_dma_contig_plane_dma_addr(buffer, 0);
  1006. dma_addr_t addr_uv;
  1007. width = q_data->pix_fmt.width;
  1008. height = q_data->pix_fmt.height;
  1009. stride = q_data->pix_fmt.plane_fmt[0].bytesperline;
  1010. fourcc = q_data->fmt->fourcc;
  1011. if (q_data->pix_fmt.num_planes == 2)
  1012. addr_uv = vb2_dma_contig_plane_dma_addr(buffer, 1);
  1013. else
  1014. addr_uv = addr_y + (stride * height);
  1015. dev_dbg(&dw_dev->pdev->dev,
  1016. "Set HW source registers for %ux%u - stride %u, pixfmt: %p4cc, dma:%pad\n",
  1017. width, height, stride, &fourcc, &addr_y);
  1018. /* Pixel Format */
  1019. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  1020. val &= ~DW100_DEWARP_CTRL_INPUT_FORMAT_MASK;
  1021. val |= DW100_DEWARP_CTRL_INPUT_FORMAT(fmt->reg_format);
  1022. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  1023. /* Swap */
  1024. val = dw100_read(dw_dev, DW100_SWAP_CONTROL);
  1025. val &= ~DW100_SWAP_CONTROL_SRC_MASK;
  1026. /*
  1027. * Data swapping is performed only on Y plane for source image.
  1028. */
  1029. if (fmt->reg_swap_uv &&
  1030. fmt->reg_format == DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED)
  1031. val |= DW100_SWAP_CONTROL_SRC(DW100_SWAP_CONTROL_Y
  1032. (DW100_SWAP_CONTROL_BYTE));
  1033. dw100_write(dw_dev, DW100_SWAP_CONTROL, val);
  1034. /* Image resolution */
  1035. dw100_write(dw_dev, DW100_SRC_IMG_SIZE,
  1036. DW100_IMG_SIZE_WIDTH(width) | DW100_IMG_SIZE_HEIGHT(height));
  1037. dw100_write(dw_dev, DW100_SRC_IMG_STRIDE, stride);
  1038. /* Buffers */
  1039. dw100_write(dw_dev, DW100_SRC_IMG_Y_BASE, DW100_IMG_Y_BASE(addr_y));
  1040. dw100_write(dw_dev, DW100_SRC_IMG_UV_BASE, DW100_IMG_UV_BASE(addr_uv));
  1041. }
  1042. static void dw100_hw_set_destination(struct dw100_device *dw_dev,
  1043. const struct dw100_q_data *q_data,
  1044. const struct dw100_fmt *ifmt,
  1045. struct vb2_buffer *buffer)
  1046. {
  1047. u32 width, height, stride, fourcc, val, size_y, size_uv;
  1048. const struct dw100_fmt *fmt = q_data->fmt;
  1049. dma_addr_t addr_y, addr_uv;
  1050. width = q_data->pix_fmt.width;
  1051. height = q_data->pix_fmt.height;
  1052. stride = q_data->pix_fmt.plane_fmt[0].bytesperline;
  1053. fourcc = fmt->fourcc;
  1054. addr_y = vb2_dma_contig_plane_dma_addr(buffer, 0);
  1055. size_y = q_data->pix_fmt.plane_fmt[0].sizeimage;
  1056. if (q_data->pix_fmt.num_planes == 2) {
  1057. addr_uv = vb2_dma_contig_plane_dma_addr(buffer, 1);
  1058. size_uv = q_data->pix_fmt.plane_fmt[1].sizeimage;
  1059. } else {
  1060. addr_uv = addr_y + ALIGN(stride * height, 16);
  1061. size_uv = size_y;
  1062. if (fmt->reg_format == DW100_DEWARP_CTRL_FORMAT_YUV420_SP)
  1063. size_uv /= 2;
  1064. }
  1065. dev_dbg(&dw_dev->pdev->dev,
  1066. "Set HW destination registers for %ux%u - stride %u, pixfmt: %p4cc, dma:%pad\n",
  1067. width, height, stride, &fourcc, &addr_y);
  1068. /* Pixel Format */
  1069. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  1070. val &= ~DW100_DEWARP_CTRL_OUTPUT_FORMAT_MASK;
  1071. val |= DW100_DEWARP_CTRL_OUTPUT_FORMAT(fmt->reg_format);
  1072. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  1073. /* Swap */
  1074. val = dw100_read(dw_dev, DW100_SWAP_CONTROL);
  1075. val &= ~DW100_SWAP_CONTROL_DST_MASK;
  1076. /*
  1077. * Avoid to swap twice
  1078. */
  1079. if (fmt->reg_swap_uv ^
  1080. (ifmt->reg_swap_uv && ifmt->reg_format !=
  1081. DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED)) {
  1082. if (fmt->reg_format == DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED)
  1083. val |= DW100_SWAP_CONTROL_DST(DW100_SWAP_CONTROL_Y
  1084. (DW100_SWAP_CONTROL_BYTE));
  1085. else
  1086. val |= DW100_SWAP_CONTROL_DST(DW100_SWAP_CONTROL_UV
  1087. (DW100_SWAP_CONTROL_BYTE));
  1088. }
  1089. dw100_write(dw_dev, DW100_SWAP_CONTROL, val);
  1090. /* Image resolution */
  1091. dw100_write(dw_dev, DW100_DST_IMG_SIZE,
  1092. DW100_IMG_SIZE_WIDTH(width) | DW100_IMG_SIZE_HEIGHT(height));
  1093. dw100_write(dw_dev, DW100_DST_IMG_STRIDE, stride);
  1094. dw100_write(dw_dev, DW100_DST_IMG_Y_BASE, DW100_IMG_Y_BASE(addr_y));
  1095. dw100_write(dw_dev, DW100_DST_IMG_UV_BASE, DW100_IMG_UV_BASE(addr_uv));
  1096. dw100_write(dw_dev, DW100_DST_IMG_Y_SIZE1, DW100_DST_IMG_Y_SIZE(size_y));
  1097. dw100_write(dw_dev, DW100_DST_IMG_UV_SIZE1,
  1098. DW100_DST_IMG_UV_SIZE(size_uv));
  1099. }
  1100. static void dw100_hw_set_mapping(struct dw100_device *dw_dev, dma_addr_t addr,
  1101. u32 width, u32 height)
  1102. {
  1103. dev_dbg(&dw_dev->pdev->dev,
  1104. "Set HW mapping registers for %ux%u addr:%pad",
  1105. width, height, &addr);
  1106. dw100_write(dw_dev, DW100_MAP_LUT_ADDR, DW100_MAP_LUT_ADDR_ADDR(addr));
  1107. dw100_write(dw_dev, DW100_MAP_LUT_SIZE, DW100_MAP_LUT_SIZE_WIDTH(width)
  1108. | DW100_MAP_LUT_SIZE_HEIGHT(height));
  1109. }
  1110. static void dw100_hw_clear_irq(struct dw100_device *dw_dev, unsigned int irq)
  1111. {
  1112. dw100_write(dw_dev, DW100_INTERRUPT_STATUS,
  1113. DW100_INTERRUPT_STATUS_INT_CLEAR(irq));
  1114. }
  1115. static void dw100_hw_enable_irq(struct dw100_device *dw_dev)
  1116. {
  1117. dw100_write(dw_dev, DW100_INTERRUPT_STATUS,
  1118. DW100_INTERRUPT_STATUS_INT_ENABLE_MASK);
  1119. }
  1120. static void dw100_hw_disable_irq(struct dw100_device *dw_dev)
  1121. {
  1122. dw100_write(dw_dev, DW100_INTERRUPT_STATUS, 0);
  1123. }
  1124. static u32 dw_hw_get_pending_irqs(struct dw100_device *dw_dev)
  1125. {
  1126. u32 val;
  1127. val = dw100_read(dw_dev, DW100_INTERRUPT_STATUS);
  1128. return DW100_INTERRUPT_STATUS_INT_STATUS(val);
  1129. }
  1130. static irqreturn_t dw100_irq_handler(int irq, void *dev_id)
  1131. {
  1132. struct dw100_device *dw_dev = dev_id;
  1133. u32 pending_irqs, err_irqs, frame_done_irq;
  1134. bool with_error = true;
  1135. pending_irqs = dw_hw_get_pending_irqs(dw_dev);
  1136. frame_done_irq = pending_irqs & DW100_INTERRUPT_STATUS_INT_FRAME_DONE;
  1137. err_irqs = DW100_INTERRUPT_STATUS_INT_ERR_STATUS(pending_irqs);
  1138. if (frame_done_irq) {
  1139. dev_dbg(&dw_dev->pdev->dev, "Frame done interrupt\n");
  1140. with_error = false;
  1141. err_irqs &= ~DW100_INTERRUPT_STATUS_INT_ERR_STATUS
  1142. (DW100_INTERRUPT_STATUS_INT_ERR_FRAME_DONE);
  1143. }
  1144. if (err_irqs)
  1145. dev_err(&dw_dev->pdev->dev, "Interrupt error: %#x\n", err_irqs);
  1146. dw100_hw_disable_irq(dw_dev);
  1147. dw100_hw_master_bus_disable(dw_dev);
  1148. dw100_hw_clear_irq(dw_dev, pending_irqs |
  1149. DW100_INTERRUPT_STATUS_INT_ERR_TIME_OUT);
  1150. dw100_job_finish(dw_dev, with_error);
  1151. return IRQ_HANDLED;
  1152. }
  1153. static void dw100_start(struct dw100_ctx *ctx, struct vb2_v4l2_buffer *in_vb,
  1154. struct vb2_v4l2_buffer *out_vb)
  1155. {
  1156. struct dw100_device *dw_dev = ctx->dw_dev;
  1157. out_vb->sequence =
  1158. dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)->sequence++;
  1159. in_vb->sequence =
  1160. dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)->sequence++;
  1161. dev_dbg(&ctx->dw_dev->pdev->dev,
  1162. "Starting queues %p->%p, sequence %u->%u\n",
  1163. v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  1164. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE),
  1165. v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  1166. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE),
  1167. in_vb->sequence, out_vb->sequence);
  1168. v4l2_m2m_buf_copy_metadata(in_vb, out_vb);
  1169. /* Now, let's deal with hardware ... */
  1170. dw100_hw_master_bus_disable(dw_dev);
  1171. dw100_hw_init_ctrl(dw_dev);
  1172. dw100_hw_set_pixel_boundary(dw_dev);
  1173. dw100_hw_set_src_crop(dw_dev, &ctx->q_data[DW100_QUEUE_SRC],
  1174. &ctx->q_data[DW100_QUEUE_DST]);
  1175. dw100_hw_set_source(dw_dev, &ctx->q_data[DW100_QUEUE_SRC],
  1176. &in_vb->vb2_buf);
  1177. dw100_hw_set_destination(dw_dev, &ctx->q_data[DW100_QUEUE_DST],
  1178. ctx->q_data[DW100_QUEUE_SRC].fmt,
  1179. &out_vb->vb2_buf);
  1180. dw100_hw_set_mapping(dw_dev, ctx->map_dma,
  1181. ctx->map_width, ctx->map_height);
  1182. dw100_hw_enable_irq(dw_dev);
  1183. dw100_hw_dewarp_start(dw_dev);
  1184. /* Enable Bus */
  1185. dw100_hw_master_bus_enable(dw_dev);
  1186. }
  1187. static void dw100_device_run(void *priv)
  1188. {
  1189. struct dw100_ctx *ctx = priv;
  1190. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1191. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1192. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1193. dw100_start(ctx, src_buf, dst_buf);
  1194. }
  1195. static const struct v4l2_m2m_ops dw100_m2m_ops = {
  1196. .device_run = dw100_device_run,
  1197. };
  1198. static struct video_device *dw100_init_video_device(struct dw100_device *dw_dev)
  1199. {
  1200. struct video_device *vfd = &dw_dev->vfd;
  1201. vfd->vfl_dir = VFL_DIR_M2M;
  1202. vfd->fops = &dw100_fops;
  1203. vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1204. vfd->ioctl_ops = &dw100_ioctl_ops;
  1205. vfd->minor = -1;
  1206. vfd->release = video_device_release_empty;
  1207. vfd->v4l2_dev = &dw_dev->v4l2_dev;
  1208. vfd->lock = &dw_dev->vfd_mutex;
  1209. strscpy(vfd->name, DRV_NAME, sizeof(vfd->name));
  1210. mutex_init(vfd->lock);
  1211. video_set_drvdata(vfd, dw_dev);
  1212. return vfd;
  1213. }
  1214. static int dw100_dump_regs_show(struct seq_file *m, void *private)
  1215. {
  1216. struct dw100_device *dw_dev = m->private;
  1217. int ret;
  1218. ret = pm_runtime_resume_and_get(&dw_dev->pdev->dev);
  1219. if (ret < 0)
  1220. return ret;
  1221. ret = dw100_dump_regs(m);
  1222. pm_runtime_put_sync(&dw_dev->pdev->dev);
  1223. return ret;
  1224. }
  1225. DEFINE_SHOW_ATTRIBUTE(dw100_dump_regs);
  1226. static void dw100_debugfs_init(struct dw100_device *dw_dev)
  1227. {
  1228. dw_dev->debugfs_root =
  1229. debugfs_create_dir(dev_name(&dw_dev->pdev->dev), NULL);
  1230. debugfs_create_file("dump_regs", 0600, dw_dev->debugfs_root, dw_dev,
  1231. &dw100_dump_regs_fops);
  1232. }
  1233. static void dw100_debugfs_exit(struct dw100_device *dw_dev)
  1234. {
  1235. debugfs_remove_recursive(dw_dev->debugfs_root);
  1236. }
  1237. static int dw100_probe(struct platform_device *pdev)
  1238. {
  1239. struct dw100_device *dw_dev;
  1240. struct video_device *vfd;
  1241. int ret, irq;
  1242. dw_dev = devm_kzalloc(&pdev->dev, sizeof(*dw_dev), GFP_KERNEL);
  1243. if (!dw_dev)
  1244. return -ENOMEM;
  1245. dw_dev->pdev = pdev;
  1246. ret = devm_clk_bulk_get_all(&pdev->dev, &dw_dev->clks);
  1247. if (ret < 0) {
  1248. dev_err(&pdev->dev, "Unable to get clocks: %d\n", ret);
  1249. return ret;
  1250. }
  1251. dw_dev->num_clks = ret;
  1252. dw_dev->mmio = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  1253. if (IS_ERR(dw_dev->mmio))
  1254. return PTR_ERR(dw_dev->mmio);
  1255. irq = platform_get_irq(pdev, 0);
  1256. if (irq < 0)
  1257. return irq;
  1258. platform_set_drvdata(pdev, dw_dev);
  1259. pm_runtime_enable(&pdev->dev);
  1260. ret = pm_runtime_resume_and_get(&pdev->dev);
  1261. if (ret < 0) {
  1262. dev_err(&pdev->dev, "Unable to resume the device: %d\n", ret);
  1263. goto err_pm;
  1264. }
  1265. pm_runtime_put_sync(&pdev->dev);
  1266. ret = devm_request_irq(&pdev->dev, irq, dw100_irq_handler, IRQF_ONESHOT,
  1267. dev_name(&pdev->dev), dw_dev);
  1268. if (ret < 0) {
  1269. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  1270. goto err_pm;
  1271. }
  1272. ret = v4l2_device_register(&pdev->dev, &dw_dev->v4l2_dev);
  1273. if (ret)
  1274. goto err_pm;
  1275. vfd = dw100_init_video_device(dw_dev);
  1276. dw_dev->m2m_dev = v4l2_m2m_init(&dw100_m2m_ops);
  1277. if (IS_ERR(dw_dev->m2m_dev)) {
  1278. dev_err(&pdev->dev, "Failed to init mem2mem device\n");
  1279. ret = PTR_ERR(dw_dev->m2m_dev);
  1280. goto err_v4l2;
  1281. }
  1282. dw_dev->mdev.dev = &pdev->dev;
  1283. strscpy(dw_dev->mdev.model, "dw100", sizeof(dw_dev->mdev.model));
  1284. media_device_init(&dw_dev->mdev);
  1285. dw_dev->v4l2_dev.mdev = &dw_dev->mdev;
  1286. ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
  1287. if (ret) {
  1288. dev_err(&pdev->dev, "Failed to register video device\n");
  1289. goto err_m2m;
  1290. }
  1291. ret = v4l2_m2m_register_media_controller(dw_dev->m2m_dev, vfd,
  1292. MEDIA_ENT_F_PROC_VIDEO_SCALER);
  1293. if (ret) {
  1294. dev_err(&pdev->dev, "Failed to init mem2mem media controller\n");
  1295. goto error_v4l2;
  1296. }
  1297. ret = media_device_register(&dw_dev->mdev);
  1298. if (ret) {
  1299. dev_err(&pdev->dev, "Failed to register mem2mem media device\n");
  1300. goto error_m2m_mc;
  1301. }
  1302. dw100_debugfs_init(dw_dev);
  1303. dev_info(&pdev->dev,
  1304. "dw100 v4l2 m2m registered as /dev/video%u\n", vfd->num);
  1305. return 0;
  1306. error_m2m_mc:
  1307. v4l2_m2m_unregister_media_controller(dw_dev->m2m_dev);
  1308. error_v4l2:
  1309. video_unregister_device(vfd);
  1310. err_m2m:
  1311. media_device_cleanup(&dw_dev->mdev);
  1312. v4l2_m2m_release(dw_dev->m2m_dev);
  1313. err_v4l2:
  1314. v4l2_device_unregister(&dw_dev->v4l2_dev);
  1315. err_pm:
  1316. pm_runtime_disable(&pdev->dev);
  1317. return ret;
  1318. }
  1319. static void dw100_remove(struct platform_device *pdev)
  1320. {
  1321. struct dw100_device *dw_dev = platform_get_drvdata(pdev);
  1322. dw100_debugfs_exit(dw_dev);
  1323. pm_runtime_disable(&pdev->dev);
  1324. media_device_unregister(&dw_dev->mdev);
  1325. v4l2_m2m_unregister_media_controller(dw_dev->m2m_dev);
  1326. media_device_cleanup(&dw_dev->mdev);
  1327. video_unregister_device(&dw_dev->vfd);
  1328. mutex_destroy(dw_dev->vfd.lock);
  1329. v4l2_m2m_release(dw_dev->m2m_dev);
  1330. v4l2_device_unregister(&dw_dev->v4l2_dev);
  1331. }
  1332. static int __maybe_unused dw100_runtime_suspend(struct device *dev)
  1333. {
  1334. struct dw100_device *dw_dev = dev_get_drvdata(dev);
  1335. clk_bulk_disable_unprepare(dw_dev->num_clks, dw_dev->clks);
  1336. return 0;
  1337. }
  1338. static int __maybe_unused dw100_runtime_resume(struct device *dev)
  1339. {
  1340. int ret;
  1341. struct dw100_device *dw_dev = dev_get_drvdata(dev);
  1342. ret = clk_bulk_prepare_enable(dw_dev->num_clks, dw_dev->clks);
  1343. if (ret)
  1344. return ret;
  1345. dw100_hw_reset(dw_dev);
  1346. return 0;
  1347. }
  1348. static const struct dev_pm_ops dw100_pm = {
  1349. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1350. pm_runtime_force_resume)
  1351. SET_RUNTIME_PM_OPS(dw100_runtime_suspend,
  1352. dw100_runtime_resume, NULL)
  1353. };
  1354. static const struct of_device_id dw100_dt_ids[] = {
  1355. { .compatible = "nxp,imx8mp-dw100", .data = NULL },
  1356. { },
  1357. };
  1358. MODULE_DEVICE_TABLE(of, dw100_dt_ids);
  1359. static struct platform_driver dw100_driver = {
  1360. .probe = dw100_probe,
  1361. .remove = dw100_remove,
  1362. .driver = {
  1363. .name = DRV_NAME,
  1364. .pm = &dw100_pm,
  1365. .of_match_table = dw100_dt_ids,
  1366. },
  1367. };
  1368. module_platform_driver(dw100_driver);
  1369. MODULE_DESCRIPTION("DW100 Hardware dewarper");
  1370. MODULE_AUTHOR("Xavier Roumegue <Xavier.Roumegue@oss.nxp.com>");
  1371. MODULE_LICENSE("GPL");