dw100.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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->private_data, 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. .wait_prepare = vb2_ops_wait_prepare,
  476. .wait_finish = vb2_ops_wait_finish,
  477. };
  478. static int dw100_m2m_queue_init(void *priv, struct vb2_queue *src_vq,
  479. struct vb2_queue *dst_vq)
  480. {
  481. struct dw100_ctx *ctx = priv;
  482. int ret;
  483. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  484. src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  485. src_vq->drv_priv = ctx;
  486. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  487. src_vq->ops = &dw100_qops;
  488. src_vq->mem_ops = &vb2_dma_contig_memops;
  489. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  490. src_vq->lock = &ctx->vq_mutex;
  491. src_vq->dev = ctx->dw_dev->v4l2_dev.dev;
  492. ret = vb2_queue_init(src_vq);
  493. if (ret)
  494. return ret;
  495. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  496. dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  497. dst_vq->drv_priv = ctx;
  498. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  499. dst_vq->ops = &dw100_qops;
  500. dst_vq->mem_ops = &vb2_dma_contig_memops;
  501. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  502. dst_vq->lock = &ctx->vq_mutex;
  503. dst_vq->dev = ctx->dw_dev->v4l2_dev.dev;
  504. return vb2_queue_init(dst_vq);
  505. }
  506. static int dw100_open(struct file *file)
  507. {
  508. struct dw100_device *dw_dev = video_drvdata(file);
  509. struct dw100_ctx *ctx;
  510. struct v4l2_ctrl_handler *hdl;
  511. struct v4l2_pix_format_mplane *pix_fmt;
  512. int ret, i;
  513. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  514. if (!ctx)
  515. return -ENOMEM;
  516. mutex_init(&ctx->vq_mutex);
  517. v4l2_fh_init(&ctx->fh, video_devdata(file));
  518. file->private_data = &ctx->fh;
  519. ctx->dw_dev = dw_dev;
  520. ctx->q_data[DW100_QUEUE_SRC].fmt = &formats[0];
  521. pix_fmt = &ctx->q_data[DW100_QUEUE_SRC].pix_fmt;
  522. pix_fmt->field = V4L2_FIELD_NONE;
  523. pix_fmt->colorspace = V4L2_COLORSPACE_REC709;
  524. pix_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix_fmt->colorspace);
  525. pix_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix_fmt->colorspace);
  526. pix_fmt->quantization =
  527. V4L2_MAP_QUANTIZATION_DEFAULT(false, pix_fmt->colorspace,
  528. pix_fmt->ycbcr_enc);
  529. v4l2_fill_pixfmt_mp(pix_fmt, formats[0].fourcc, DW100_DEF_W, DW100_DEF_H);
  530. ctx->q_data[DW100_QUEUE_SRC].crop.top = 0;
  531. ctx->q_data[DW100_QUEUE_SRC].crop.left = 0;
  532. ctx->q_data[DW100_QUEUE_SRC].crop.width = DW100_DEF_W;
  533. ctx->q_data[DW100_QUEUE_SRC].crop.height = DW100_DEF_H;
  534. ctx->q_data[DW100_QUEUE_DST] = ctx->q_data[DW100_QUEUE_SRC];
  535. hdl = &ctx->hdl;
  536. v4l2_ctrl_handler_init(hdl, ARRAY_SIZE(controls));
  537. for (i = 0; i < ARRAY_SIZE(controls); i++) {
  538. ctx->ctrls[i] = v4l2_ctrl_new_custom(hdl, &controls[i], NULL);
  539. if (hdl->error) {
  540. dev_err(&ctx->dw_dev->pdev->dev,
  541. "Adding control (%d) failed\n", i);
  542. ret = hdl->error;
  543. goto err;
  544. }
  545. }
  546. ctx->fh.ctrl_handler = hdl;
  547. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dw_dev->m2m_dev,
  548. ctx, &dw100_m2m_queue_init);
  549. if (IS_ERR(ctx->fh.m2m_ctx)) {
  550. ret = PTR_ERR(ctx->fh.m2m_ctx);
  551. goto err;
  552. }
  553. v4l2_fh_add(&ctx->fh);
  554. return 0;
  555. err:
  556. v4l2_ctrl_handler_free(hdl);
  557. v4l2_fh_exit(&ctx->fh);
  558. mutex_destroy(&ctx->vq_mutex);
  559. kfree(ctx);
  560. return ret;
  561. }
  562. static int dw100_release(struct file *file)
  563. {
  564. struct dw100_ctx *ctx = dw100_file2ctx(file);
  565. v4l2_fh_del(&ctx->fh);
  566. v4l2_fh_exit(&ctx->fh);
  567. v4l2_ctrl_handler_free(&ctx->hdl);
  568. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  569. mutex_destroy(&ctx->vq_mutex);
  570. kfree(ctx);
  571. return 0;
  572. }
  573. static const struct v4l2_file_operations dw100_fops = {
  574. .owner = THIS_MODULE,
  575. .open = dw100_open,
  576. .release = dw100_release,
  577. .poll = v4l2_m2m_fop_poll,
  578. .unlocked_ioctl = video_ioctl2,
  579. .mmap = v4l2_m2m_fop_mmap,
  580. };
  581. static int dw100_querycap(struct file *file, void *priv,
  582. struct v4l2_capability *cap)
  583. {
  584. strscpy(cap->driver, DRV_NAME, sizeof(cap->driver));
  585. strscpy(cap->card, "DW100 dewarper", sizeof(cap->card));
  586. return 0;
  587. }
  588. static int dw100_enum_fmt_vid(struct file *file, void *priv,
  589. struct v4l2_fmtdesc *f)
  590. {
  591. int i, num = 0;
  592. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  593. if (formats[i].types & to_dw100_fmt_type(f->type)) {
  594. if (num == f->index) {
  595. f->pixelformat = formats[i].fourcc;
  596. return 0;
  597. }
  598. ++num;
  599. }
  600. }
  601. return -EINVAL;
  602. }
  603. static int dw100_enum_framesizes(struct file *file, void *priv,
  604. struct v4l2_frmsizeenum *fsize)
  605. {
  606. const struct dw100_fmt *fmt;
  607. if (fsize->index)
  608. return -EINVAL;
  609. fmt = dw100_find_pixel_format(fsize->pixel_format,
  610. DW100_FMT_OUTPUT | DW100_FMT_CAPTURE);
  611. if (!fmt)
  612. return -EINVAL;
  613. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  614. fsize->stepwise = dw100_frmsize_stepwise;
  615. return 0;
  616. }
  617. static int dw100_g_fmt_vid(struct file *file, void *priv, struct v4l2_format *f)
  618. {
  619. struct dw100_ctx *ctx = dw100_file2ctx(file);
  620. struct vb2_queue *vq;
  621. struct dw100_q_data *q_data;
  622. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  623. if (!vq)
  624. return -EINVAL;
  625. q_data = dw100_get_q_data(ctx, f->type);
  626. f->fmt.pix_mp = q_data->pix_fmt;
  627. return 0;
  628. }
  629. static int dw100_try_fmt(struct file *file, struct v4l2_format *f)
  630. {
  631. struct dw100_ctx *ctx = dw100_file2ctx(file);
  632. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  633. const struct dw100_fmt *fmt;
  634. fmt = dw100_find_format(f);
  635. if (!fmt) {
  636. fmt = &formats[0];
  637. pix->pixelformat = fmt->fourcc;
  638. }
  639. v4l2_apply_frmsize_constraints(&pix->width, &pix->height,
  640. &dw100_frmsize_stepwise);
  641. v4l2_fill_pixfmt_mp(pix, fmt->fourcc, pix->width, pix->height);
  642. pix->field = V4L2_FIELD_NONE;
  643. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  644. if (pix->colorspace == V4L2_COLORSPACE_DEFAULT)
  645. pix->colorspace = V4L2_COLORSPACE_REC709;
  646. if (pix->xfer_func == V4L2_XFER_FUNC_DEFAULT)
  647. pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
  648. if (pix->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT)
  649. pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
  650. if (pix->quantization == V4L2_QUANTIZATION_DEFAULT)
  651. pix->quantization =
  652. V4L2_MAP_QUANTIZATION_DEFAULT(false,
  653. pix->colorspace,
  654. pix->ycbcr_enc);
  655. } else {
  656. /*
  657. * The DW100 can't perform colorspace conversion, the colorspace
  658. * on the capture queue must be identical to the output queue.
  659. */
  660. const struct dw100_q_data *q_data =
  661. dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  662. pix->colorspace = q_data->pix_fmt.colorspace;
  663. pix->xfer_func = q_data->pix_fmt.xfer_func;
  664. pix->ycbcr_enc = q_data->pix_fmt.ycbcr_enc;
  665. pix->quantization = q_data->pix_fmt.quantization;
  666. }
  667. return 0;
  668. }
  669. static int dw100_s_fmt(struct dw100_ctx *ctx, struct v4l2_format *f)
  670. {
  671. struct dw100_q_data *q_data;
  672. struct vb2_queue *vq;
  673. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  674. if (!vq)
  675. return -EINVAL;
  676. q_data = dw100_get_q_data(ctx, f->type);
  677. if (!q_data)
  678. return -EINVAL;
  679. if (vb2_is_busy(vq)) {
  680. dev_dbg(&ctx->dw_dev->pdev->dev, "%s queue busy\n", __func__);
  681. return -EBUSY;
  682. }
  683. q_data->fmt = dw100_find_format(f);
  684. q_data->pix_fmt = f->fmt.pix_mp;
  685. q_data->crop.top = 0;
  686. q_data->crop.left = 0;
  687. q_data->crop.width = f->fmt.pix_mp.width;
  688. q_data->crop.height = f->fmt.pix_mp.height;
  689. /* Propagate buffers encoding */
  690. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  691. struct dw100_q_data *dst_q_data =
  692. dw100_get_q_data(ctx,
  693. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  694. dst_q_data->pix_fmt.colorspace = q_data->pix_fmt.colorspace;
  695. dst_q_data->pix_fmt.ycbcr_enc = q_data->pix_fmt.ycbcr_enc;
  696. dst_q_data->pix_fmt.quantization = q_data->pix_fmt.quantization;
  697. dst_q_data->pix_fmt.xfer_func = q_data->pix_fmt.xfer_func;
  698. }
  699. dev_dbg(&ctx->dw_dev->pdev->dev,
  700. "Setting format for type %u, wxh: %ux%u, fmt: %p4cc\n",
  701. f->type, q_data->pix_fmt.width, q_data->pix_fmt.height,
  702. &q_data->pix_fmt.pixelformat);
  703. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  704. int ret;
  705. u32 dims[V4L2_CTRL_MAX_DIMS] = {};
  706. struct v4l2_ctrl *ctrl = ctx->ctrls[DW100_CTRL_DEWARPING_MAP];
  707. dims[0] = dw100_get_n_vertices_from_length(q_data->pix_fmt.width);
  708. dims[1] = dw100_get_n_vertices_from_length(q_data->pix_fmt.height);
  709. ret = v4l2_ctrl_modify_dimensions(ctrl, dims);
  710. if (ret) {
  711. dev_err(&ctx->dw_dev->pdev->dev,
  712. "Modifying LUT dimensions failed with error %d\n",
  713. ret);
  714. return ret;
  715. }
  716. }
  717. return 0;
  718. }
  719. static int dw100_try_fmt_vid_cap(struct file *file, void *priv,
  720. struct v4l2_format *f)
  721. {
  722. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  723. return -EINVAL;
  724. return dw100_try_fmt(file, f);
  725. }
  726. static int dw100_s_fmt_vid_cap(struct file *file, void *priv,
  727. struct v4l2_format *f)
  728. {
  729. struct dw100_ctx *ctx = dw100_file2ctx(file);
  730. int ret;
  731. ret = dw100_try_fmt_vid_cap(file, priv, f);
  732. if (ret)
  733. return ret;
  734. ret = dw100_s_fmt(ctx, f);
  735. if (ret)
  736. return ret;
  737. return 0;
  738. }
  739. static int dw100_try_fmt_vid_out(struct file *file, void *priv,
  740. struct v4l2_format *f)
  741. {
  742. if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  743. return -EINVAL;
  744. return dw100_try_fmt(file, f);
  745. }
  746. static int dw100_s_fmt_vid_out(struct file *file, void *priv,
  747. struct v4l2_format *f)
  748. {
  749. struct dw100_ctx *ctx = dw100_file2ctx(file);
  750. int ret;
  751. ret = dw100_try_fmt_vid_out(file, priv, f);
  752. if (ret)
  753. return ret;
  754. ret = dw100_s_fmt(ctx, f);
  755. if (ret)
  756. return ret;
  757. return 0;
  758. }
  759. static int dw100_g_selection(struct file *file, void *fh,
  760. struct v4l2_selection *sel)
  761. {
  762. struct dw100_ctx *ctx = dw100_file2ctx(file);
  763. struct dw100_q_data *src_q_data;
  764. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  765. return -EINVAL;
  766. src_q_data = dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  767. switch (sel->target) {
  768. case V4L2_SEL_TGT_CROP_DEFAULT:
  769. case V4L2_SEL_TGT_CROP_BOUNDS:
  770. sel->r.top = 0;
  771. sel->r.left = 0;
  772. sel->r.width = src_q_data->pix_fmt.width;
  773. sel->r.height = src_q_data->pix_fmt.height;
  774. break;
  775. case V4L2_SEL_TGT_CROP:
  776. sel->r.top = src_q_data->crop.top;
  777. sel->r.left = src_q_data->crop.left;
  778. sel->r.width = src_q_data->crop.width;
  779. sel->r.height = src_q_data->crop.height;
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. return 0;
  785. }
  786. static int dw100_s_selection(struct file *file, void *fh,
  787. struct v4l2_selection *sel)
  788. {
  789. struct dw100_ctx *ctx = dw100_file2ctx(file);
  790. struct dw100_q_data *src_q_data;
  791. u32 qscalex, qscaley, qscale;
  792. int x, y, w, h;
  793. unsigned int wframe, hframe;
  794. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  795. return -EINVAL;
  796. src_q_data = dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  797. dev_dbg(&ctx->dw_dev->pdev->dev,
  798. ">>> Buffer Type: %u Target: %u Rect: %ux%u@%d.%d\n",
  799. sel->type, sel->target,
  800. sel->r.width, sel->r.height, sel->r.left, sel->r.top);
  801. switch (sel->target) {
  802. case V4L2_SEL_TGT_CROP:
  803. wframe = src_q_data->pix_fmt.width;
  804. hframe = src_q_data->pix_fmt.height;
  805. sel->r.top = clamp_t(int, sel->r.top, 0, hframe - DW100_MIN_H);
  806. sel->r.left = clamp_t(int, sel->r.left, 0, wframe - DW100_MIN_W);
  807. sel->r.height =
  808. clamp(sel->r.height, DW100_MIN_H, hframe - sel->r.top);
  809. sel->r.width =
  810. clamp(sel->r.width, DW100_MIN_W, wframe - sel->r.left);
  811. /* UQ16.16 for float operations */
  812. qscalex = (sel->r.width << 16) / wframe;
  813. qscaley = (sel->r.height << 16) / hframe;
  814. y = sel->r.top;
  815. x = sel->r.left;
  816. if (qscalex == qscaley) {
  817. qscale = qscalex;
  818. } else {
  819. switch (sel->flags) {
  820. case 0:
  821. qscale = (qscalex + qscaley) / 2;
  822. break;
  823. case V4L2_SEL_FLAG_GE:
  824. qscale = max(qscaley, qscalex);
  825. break;
  826. case V4L2_SEL_FLAG_LE:
  827. qscale = min(qscaley, qscalex);
  828. break;
  829. case V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE:
  830. return -ERANGE;
  831. default:
  832. return -EINVAL;
  833. }
  834. }
  835. w = (u32)((((u64)wframe << 16) * qscale) >> 32);
  836. h = (u32)((((u64)hframe << 16) * qscale) >> 32);
  837. x = x + (sel->r.width - w) / 2;
  838. y = y + (sel->r.height - h) / 2;
  839. x = min(wframe - w, (unsigned int)max(0, x));
  840. y = min(hframe - h, (unsigned int)max(0, y));
  841. sel->r.top = y;
  842. sel->r.left = x;
  843. sel->r.width = w;
  844. sel->r.height = h;
  845. src_q_data->crop.top = sel->r.top;
  846. src_q_data->crop.left = sel->r.left;
  847. src_q_data->crop.width = sel->r.width;
  848. src_q_data->crop.height = sel->r.height;
  849. break;
  850. default:
  851. return -EINVAL;
  852. }
  853. dev_dbg(&ctx->dw_dev->pdev->dev,
  854. "<<< Buffer Type: %u Target: %u Rect: %ux%u@%d.%d\n",
  855. sel->type, sel->target,
  856. sel->r.width, sel->r.height, sel->r.left, sel->r.top);
  857. return 0;
  858. }
  859. static const struct v4l2_ioctl_ops dw100_ioctl_ops = {
  860. .vidioc_querycap = dw100_querycap,
  861. .vidioc_enum_fmt_vid_cap = dw100_enum_fmt_vid,
  862. .vidioc_enum_framesizes = dw100_enum_framesizes,
  863. .vidioc_g_fmt_vid_cap_mplane = dw100_g_fmt_vid,
  864. .vidioc_try_fmt_vid_cap_mplane = dw100_try_fmt_vid_cap,
  865. .vidioc_s_fmt_vid_cap_mplane = dw100_s_fmt_vid_cap,
  866. .vidioc_enum_fmt_vid_out = dw100_enum_fmt_vid,
  867. .vidioc_g_fmt_vid_out_mplane = dw100_g_fmt_vid,
  868. .vidioc_try_fmt_vid_out_mplane = dw100_try_fmt_vid_out,
  869. .vidioc_s_fmt_vid_out_mplane = dw100_s_fmt_vid_out,
  870. .vidioc_g_selection = dw100_g_selection,
  871. .vidioc_s_selection = dw100_s_selection,
  872. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  873. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  874. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  875. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  876. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  877. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  878. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  879. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  880. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  881. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  882. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  883. };
  884. static void dw100_job_finish(struct dw100_device *dw_dev, bool with_error)
  885. {
  886. struct dw100_ctx *curr_ctx;
  887. struct vb2_v4l2_buffer *src_vb, *dst_vb;
  888. enum vb2_buffer_state buf_state;
  889. curr_ctx = v4l2_m2m_get_curr_priv(dw_dev->m2m_dev);
  890. if (!curr_ctx) {
  891. dev_err(&dw_dev->pdev->dev,
  892. "Instance released before the end of transaction\n");
  893. return;
  894. }
  895. src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
  896. dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
  897. if (likely(!with_error))
  898. buf_state = VB2_BUF_STATE_DONE;
  899. else
  900. buf_state = VB2_BUF_STATE_ERROR;
  901. v4l2_m2m_buf_done(src_vb, buf_state);
  902. v4l2_m2m_buf_done(dst_vb, buf_state);
  903. dev_dbg(&dw_dev->pdev->dev, "Finishing transaction with%s error(s)\n",
  904. with_error ? "" : "out");
  905. v4l2_m2m_job_finish(dw_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
  906. }
  907. static void dw100_hw_reset(struct dw100_device *dw_dev)
  908. {
  909. u32 val;
  910. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  911. val |= DW100_DEWARP_CTRL_ENABLE;
  912. val |= DW100_DEWARP_CTRL_SOFT_RESET;
  913. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  914. val &= ~DW100_DEWARP_CTRL_SOFT_RESET;
  915. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  916. }
  917. static void _dw100_hw_set_master_bus_enable(struct dw100_device *dw_dev,
  918. unsigned int enable)
  919. {
  920. u32 val;
  921. dev_dbg(&dw_dev->pdev->dev, "%sable master bus\n",
  922. enable ? "En" : "Dis");
  923. val = dw100_read(dw_dev, DW100_BUS_CTRL);
  924. if (enable)
  925. val |= DW100_BUS_CTRL_AXI_MASTER_ENABLE;
  926. else
  927. val &= ~DW100_BUS_CTRL_AXI_MASTER_ENABLE;
  928. dw100_write(dw_dev, DW100_BUS_CTRL, val);
  929. }
  930. static void dw100_hw_master_bus_enable(struct dw100_device *dw_dev)
  931. {
  932. _dw100_hw_set_master_bus_enable(dw_dev, 1);
  933. }
  934. static void dw100_hw_master_bus_disable(struct dw100_device *dw_dev)
  935. {
  936. _dw100_hw_set_master_bus_enable(dw_dev, 0);
  937. }
  938. static void dw100_hw_dewarp_start(struct dw100_device *dw_dev)
  939. {
  940. u32 val;
  941. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  942. dev_dbg(&dw_dev->pdev->dev, "Starting Hardware CTRL:0x%08x\n", val);
  943. dw100_write(dw_dev, DW100_DEWARP_CTRL, val | DW100_DEWARP_CTRL_START);
  944. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  945. }
  946. static void dw100_hw_init_ctrl(struct dw100_device *dw_dev)
  947. {
  948. u32 val;
  949. /*
  950. * Input format YUV422_SP
  951. * Output format YUV422_SP
  952. * No hardware handshake (SW)
  953. * No automatic double src buffering (Single)
  954. * No automatic double dst buffering (Single)
  955. * No Black Line
  956. * Prefetch image pixel traversal
  957. */
  958. val = DW100_DEWARP_CTRL_ENABLE
  959. /* Valid only for auto prefetch mode*/
  960. | DW100_DEWARP_CTRL_PREFETCH_THRESHOLD(32);
  961. /*
  962. * Calculation mode required to support any scaling factor,
  963. * but x4 slower than traversal mode.
  964. *
  965. * DW100_DEWARP_CTRL_PREFETCH_MODE_TRAVERSAL
  966. * DW100_DEWARP_CTRL_PREFETCH_MODE_CALCULATION
  967. * DW100_DEWARP_CTRL_PREFETCH_MODE_AUTO
  968. *
  969. * TODO: Find heuristics requiring calculation mode
  970. */
  971. val |= DW100_DEWARP_CTRL_PREFETCH_MODE_CALCULATION;
  972. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  973. }
  974. static void dw100_hw_set_pixel_boundary(struct dw100_device *dw_dev)
  975. {
  976. u32 val;
  977. val = DW100_BOUNDARY_PIXEL_V(128)
  978. | DW100_BOUNDARY_PIXEL_U(128)
  979. | DW100_BOUNDARY_PIXEL_Y(0);
  980. dw100_write(dw_dev, DW100_BOUNDARY_PIXEL, val);
  981. }
  982. static void dw100_hw_set_scale(struct dw100_device *dw_dev, u8 scale)
  983. {
  984. dev_dbg(&dw_dev->pdev->dev, "Setting scale factor to %u\n", scale);
  985. dw100_write(dw_dev, DW100_SCALE_FACTOR, scale);
  986. }
  987. static void dw100_hw_set_roi(struct dw100_device *dw_dev, u32 x, u32 y)
  988. {
  989. u32 val;
  990. dev_dbg(&dw_dev->pdev->dev, "Setting ROI region to %u.%u\n", x, y);
  991. val = DW100_ROI_START_X(x) | DW100_ROI_START_Y(y);
  992. dw100_write(dw_dev, DW100_ROI_START, val);
  993. }
  994. static void dw100_hw_set_src_crop(struct dw100_device *dw_dev,
  995. const struct dw100_q_data *src_q_data,
  996. const struct dw100_q_data *dst_q_data)
  997. {
  998. const struct v4l2_rect *rect = &src_q_data->crop;
  999. u32 src_scale, qscale, left_scale, top_scale;
  1000. /* HW Scale is UQ1.7 encoded */
  1001. src_scale = (rect->width << 7) / src_q_data->pix_fmt.width;
  1002. dw100_hw_set_scale(dw_dev, src_scale);
  1003. qscale = (dst_q_data->pix_fmt.width << 7) / src_q_data->pix_fmt.width;
  1004. left_scale = ((rect->left << 7) * qscale) >> 14;
  1005. top_scale = ((rect->top << 7) * qscale) >> 14;
  1006. dw100_hw_set_roi(dw_dev, left_scale, top_scale);
  1007. }
  1008. static void dw100_hw_set_source(struct dw100_device *dw_dev,
  1009. const struct dw100_q_data *q_data,
  1010. struct vb2_buffer *buffer)
  1011. {
  1012. u32 width, height, stride, fourcc, val;
  1013. const struct dw100_fmt *fmt = q_data->fmt;
  1014. dma_addr_t addr_y = vb2_dma_contig_plane_dma_addr(buffer, 0);
  1015. dma_addr_t addr_uv;
  1016. width = q_data->pix_fmt.width;
  1017. height = q_data->pix_fmt.height;
  1018. stride = q_data->pix_fmt.plane_fmt[0].bytesperline;
  1019. fourcc = q_data->fmt->fourcc;
  1020. if (q_data->pix_fmt.num_planes == 2)
  1021. addr_uv = vb2_dma_contig_plane_dma_addr(buffer, 1);
  1022. else
  1023. addr_uv = addr_y + (stride * height);
  1024. dev_dbg(&dw_dev->pdev->dev,
  1025. "Set HW source registers for %ux%u - stride %u, pixfmt: %p4cc, dma:%pad\n",
  1026. width, height, stride, &fourcc, &addr_y);
  1027. /* Pixel Format */
  1028. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  1029. val &= ~DW100_DEWARP_CTRL_INPUT_FORMAT_MASK;
  1030. val |= DW100_DEWARP_CTRL_INPUT_FORMAT(fmt->reg_format);
  1031. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  1032. /* Swap */
  1033. val = dw100_read(dw_dev, DW100_SWAP_CONTROL);
  1034. val &= ~DW100_SWAP_CONTROL_SRC_MASK;
  1035. /*
  1036. * Data swapping is performed only on Y plane for source image.
  1037. */
  1038. if (fmt->reg_swap_uv &&
  1039. fmt->reg_format == DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED)
  1040. val |= DW100_SWAP_CONTROL_SRC(DW100_SWAP_CONTROL_Y
  1041. (DW100_SWAP_CONTROL_BYTE));
  1042. dw100_write(dw_dev, DW100_SWAP_CONTROL, val);
  1043. /* Image resolution */
  1044. dw100_write(dw_dev, DW100_SRC_IMG_SIZE,
  1045. DW100_IMG_SIZE_WIDTH(width) | DW100_IMG_SIZE_HEIGHT(height));
  1046. dw100_write(dw_dev, DW100_SRC_IMG_STRIDE, stride);
  1047. /* Buffers */
  1048. dw100_write(dw_dev, DW100_SRC_IMG_Y_BASE, DW100_IMG_Y_BASE(addr_y));
  1049. dw100_write(dw_dev, DW100_SRC_IMG_UV_BASE, DW100_IMG_UV_BASE(addr_uv));
  1050. }
  1051. static void dw100_hw_set_destination(struct dw100_device *dw_dev,
  1052. const struct dw100_q_data *q_data,
  1053. const struct dw100_fmt *ifmt,
  1054. struct vb2_buffer *buffer)
  1055. {
  1056. u32 width, height, stride, fourcc, val, size_y, size_uv;
  1057. const struct dw100_fmt *fmt = q_data->fmt;
  1058. dma_addr_t addr_y, addr_uv;
  1059. width = q_data->pix_fmt.width;
  1060. height = q_data->pix_fmt.height;
  1061. stride = q_data->pix_fmt.plane_fmt[0].bytesperline;
  1062. fourcc = fmt->fourcc;
  1063. addr_y = vb2_dma_contig_plane_dma_addr(buffer, 0);
  1064. size_y = q_data->pix_fmt.plane_fmt[0].sizeimage;
  1065. if (q_data->pix_fmt.num_planes == 2) {
  1066. addr_uv = vb2_dma_contig_plane_dma_addr(buffer, 1);
  1067. size_uv = q_data->pix_fmt.plane_fmt[1].sizeimage;
  1068. } else {
  1069. addr_uv = addr_y + ALIGN(stride * height, 16);
  1070. size_uv = size_y;
  1071. if (fmt->reg_format == DW100_DEWARP_CTRL_FORMAT_YUV420_SP)
  1072. size_uv /= 2;
  1073. }
  1074. dev_dbg(&dw_dev->pdev->dev,
  1075. "Set HW source registers for %ux%u - stride %u, pixfmt: %p4cc, dma:%pad\n",
  1076. width, height, stride, &fourcc, &addr_y);
  1077. /* Pixel Format */
  1078. val = dw100_read(dw_dev, DW100_DEWARP_CTRL);
  1079. val &= ~DW100_DEWARP_CTRL_OUTPUT_FORMAT_MASK;
  1080. val |= DW100_DEWARP_CTRL_OUTPUT_FORMAT(fmt->reg_format);
  1081. dw100_write(dw_dev, DW100_DEWARP_CTRL, val);
  1082. /* Swap */
  1083. val = dw100_read(dw_dev, DW100_SWAP_CONTROL);
  1084. val &= ~DW100_SWAP_CONTROL_DST_MASK;
  1085. /*
  1086. * Avoid to swap twice
  1087. */
  1088. if (fmt->reg_swap_uv ^
  1089. (ifmt->reg_swap_uv && ifmt->reg_format !=
  1090. DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED)) {
  1091. if (fmt->reg_format == DW100_DEWARP_CTRL_FORMAT_YUV422_PACKED)
  1092. val |= DW100_SWAP_CONTROL_DST(DW100_SWAP_CONTROL_Y
  1093. (DW100_SWAP_CONTROL_BYTE));
  1094. else
  1095. val |= DW100_SWAP_CONTROL_DST(DW100_SWAP_CONTROL_UV
  1096. (DW100_SWAP_CONTROL_BYTE));
  1097. }
  1098. dw100_write(dw_dev, DW100_SWAP_CONTROL, val);
  1099. /* Image resolution */
  1100. dw100_write(dw_dev, DW100_DST_IMG_SIZE,
  1101. DW100_IMG_SIZE_WIDTH(width) | DW100_IMG_SIZE_HEIGHT(height));
  1102. dw100_write(dw_dev, DW100_DST_IMG_STRIDE, stride);
  1103. dw100_write(dw_dev, DW100_DST_IMG_Y_BASE, DW100_IMG_Y_BASE(addr_y));
  1104. dw100_write(dw_dev, DW100_DST_IMG_UV_BASE, DW100_IMG_UV_BASE(addr_uv));
  1105. dw100_write(dw_dev, DW100_DST_IMG_Y_SIZE1, DW100_DST_IMG_Y_SIZE(size_y));
  1106. dw100_write(dw_dev, DW100_DST_IMG_UV_SIZE1,
  1107. DW100_DST_IMG_UV_SIZE(size_uv));
  1108. }
  1109. static void dw100_hw_set_mapping(struct dw100_device *dw_dev, dma_addr_t addr,
  1110. u32 width, u32 height)
  1111. {
  1112. dev_dbg(&dw_dev->pdev->dev,
  1113. "Set HW mapping registers for %ux%u addr:%pad",
  1114. width, height, &addr);
  1115. dw100_write(dw_dev, DW100_MAP_LUT_ADDR, DW100_MAP_LUT_ADDR_ADDR(addr));
  1116. dw100_write(dw_dev, DW100_MAP_LUT_SIZE, DW100_MAP_LUT_SIZE_WIDTH(width)
  1117. | DW100_MAP_LUT_SIZE_HEIGHT(height));
  1118. }
  1119. static void dw100_hw_clear_irq(struct dw100_device *dw_dev, unsigned int irq)
  1120. {
  1121. dw100_write(dw_dev, DW100_INTERRUPT_STATUS,
  1122. DW100_INTERRUPT_STATUS_INT_CLEAR(irq));
  1123. }
  1124. static void dw100_hw_enable_irq(struct dw100_device *dw_dev)
  1125. {
  1126. dw100_write(dw_dev, DW100_INTERRUPT_STATUS,
  1127. DW100_INTERRUPT_STATUS_INT_ENABLE_MASK);
  1128. }
  1129. static void dw100_hw_disable_irq(struct dw100_device *dw_dev)
  1130. {
  1131. dw100_write(dw_dev, DW100_INTERRUPT_STATUS, 0);
  1132. }
  1133. static u32 dw_hw_get_pending_irqs(struct dw100_device *dw_dev)
  1134. {
  1135. u32 val;
  1136. val = dw100_read(dw_dev, DW100_INTERRUPT_STATUS);
  1137. return DW100_INTERRUPT_STATUS_INT_STATUS(val);
  1138. }
  1139. static irqreturn_t dw100_irq_handler(int irq, void *dev_id)
  1140. {
  1141. struct dw100_device *dw_dev = dev_id;
  1142. u32 pending_irqs, err_irqs, frame_done_irq;
  1143. bool with_error = true;
  1144. pending_irqs = dw_hw_get_pending_irqs(dw_dev);
  1145. frame_done_irq = pending_irqs & DW100_INTERRUPT_STATUS_INT_FRAME_DONE;
  1146. err_irqs = DW100_INTERRUPT_STATUS_INT_ERR_STATUS(pending_irqs);
  1147. if (frame_done_irq) {
  1148. dev_dbg(&dw_dev->pdev->dev, "Frame done interrupt\n");
  1149. with_error = false;
  1150. err_irqs &= ~DW100_INTERRUPT_STATUS_INT_ERR_STATUS
  1151. (DW100_INTERRUPT_STATUS_INT_ERR_FRAME_DONE);
  1152. }
  1153. if (err_irqs)
  1154. dev_err(&dw_dev->pdev->dev, "Interrupt error: %#x\n", err_irqs);
  1155. dw100_hw_disable_irq(dw_dev);
  1156. dw100_hw_master_bus_disable(dw_dev);
  1157. dw100_hw_clear_irq(dw_dev, pending_irqs |
  1158. DW100_INTERRUPT_STATUS_INT_ERR_TIME_OUT);
  1159. dw100_job_finish(dw_dev, with_error);
  1160. return IRQ_HANDLED;
  1161. }
  1162. static void dw100_start(struct dw100_ctx *ctx, struct vb2_v4l2_buffer *in_vb,
  1163. struct vb2_v4l2_buffer *out_vb)
  1164. {
  1165. struct dw100_device *dw_dev = ctx->dw_dev;
  1166. out_vb->sequence =
  1167. dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)->sequence++;
  1168. in_vb->sequence =
  1169. dw100_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)->sequence++;
  1170. dev_dbg(&ctx->dw_dev->pdev->dev,
  1171. "Starting queues %p->%p, sequence %u->%u\n",
  1172. v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  1173. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE),
  1174. v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  1175. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE),
  1176. in_vb->sequence, out_vb->sequence);
  1177. v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true);
  1178. /* Now, let's deal with hardware ... */
  1179. dw100_hw_master_bus_disable(dw_dev);
  1180. dw100_hw_init_ctrl(dw_dev);
  1181. dw100_hw_set_pixel_boundary(dw_dev);
  1182. dw100_hw_set_src_crop(dw_dev, &ctx->q_data[DW100_QUEUE_SRC],
  1183. &ctx->q_data[DW100_QUEUE_DST]);
  1184. dw100_hw_set_source(dw_dev, &ctx->q_data[DW100_QUEUE_SRC],
  1185. &in_vb->vb2_buf);
  1186. dw100_hw_set_destination(dw_dev, &ctx->q_data[DW100_QUEUE_DST],
  1187. ctx->q_data[DW100_QUEUE_SRC].fmt,
  1188. &out_vb->vb2_buf);
  1189. dw100_hw_set_mapping(dw_dev, ctx->map_dma,
  1190. ctx->map_width, ctx->map_height);
  1191. dw100_hw_enable_irq(dw_dev);
  1192. dw100_hw_dewarp_start(dw_dev);
  1193. /* Enable Bus */
  1194. dw100_hw_master_bus_enable(dw_dev);
  1195. }
  1196. static void dw100_device_run(void *priv)
  1197. {
  1198. struct dw100_ctx *ctx = priv;
  1199. struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1200. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  1201. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  1202. dw100_start(ctx, src_buf, dst_buf);
  1203. }
  1204. static const struct v4l2_m2m_ops dw100_m2m_ops = {
  1205. .device_run = dw100_device_run,
  1206. };
  1207. static struct video_device *dw100_init_video_device(struct dw100_device *dw_dev)
  1208. {
  1209. struct video_device *vfd = &dw_dev->vfd;
  1210. vfd->vfl_dir = VFL_DIR_M2M;
  1211. vfd->fops = &dw100_fops;
  1212. vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1213. vfd->ioctl_ops = &dw100_ioctl_ops;
  1214. vfd->minor = -1;
  1215. vfd->release = video_device_release_empty;
  1216. vfd->v4l2_dev = &dw_dev->v4l2_dev;
  1217. vfd->lock = &dw_dev->vfd_mutex;
  1218. strscpy(vfd->name, DRV_NAME, sizeof(vfd->name));
  1219. mutex_init(vfd->lock);
  1220. video_set_drvdata(vfd, dw_dev);
  1221. return vfd;
  1222. }
  1223. static int dw100_dump_regs_show(struct seq_file *m, void *private)
  1224. {
  1225. struct dw100_device *dw_dev = m->private;
  1226. int ret;
  1227. ret = pm_runtime_resume_and_get(&dw_dev->pdev->dev);
  1228. if (ret < 0)
  1229. return ret;
  1230. ret = dw100_dump_regs(m);
  1231. pm_runtime_put_sync(&dw_dev->pdev->dev);
  1232. return ret;
  1233. }
  1234. DEFINE_SHOW_ATTRIBUTE(dw100_dump_regs);
  1235. static void dw100_debugfs_init(struct dw100_device *dw_dev)
  1236. {
  1237. dw_dev->debugfs_root =
  1238. debugfs_create_dir(dev_name(&dw_dev->pdev->dev), NULL);
  1239. debugfs_create_file("dump_regs", 0600, dw_dev->debugfs_root, dw_dev,
  1240. &dw100_dump_regs_fops);
  1241. }
  1242. static void dw100_debugfs_exit(struct dw100_device *dw_dev)
  1243. {
  1244. debugfs_remove_recursive(dw_dev->debugfs_root);
  1245. }
  1246. static int dw100_probe(struct platform_device *pdev)
  1247. {
  1248. struct dw100_device *dw_dev;
  1249. struct video_device *vfd;
  1250. int ret, irq;
  1251. dw_dev = devm_kzalloc(&pdev->dev, sizeof(*dw_dev), GFP_KERNEL);
  1252. if (!dw_dev)
  1253. return -ENOMEM;
  1254. dw_dev->pdev = pdev;
  1255. ret = devm_clk_bulk_get_all(&pdev->dev, &dw_dev->clks);
  1256. if (ret < 0) {
  1257. dev_err(&pdev->dev, "Unable to get clocks: %d\n", ret);
  1258. return ret;
  1259. }
  1260. dw_dev->num_clks = ret;
  1261. dw_dev->mmio = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  1262. if (IS_ERR(dw_dev->mmio))
  1263. return PTR_ERR(dw_dev->mmio);
  1264. irq = platform_get_irq(pdev, 0);
  1265. if (irq < 0)
  1266. return irq;
  1267. platform_set_drvdata(pdev, dw_dev);
  1268. pm_runtime_enable(&pdev->dev);
  1269. ret = pm_runtime_resume_and_get(&pdev->dev);
  1270. if (ret < 0) {
  1271. dev_err(&pdev->dev, "Unable to resume the device: %d\n", ret);
  1272. goto err_pm;
  1273. }
  1274. pm_runtime_put_sync(&pdev->dev);
  1275. ret = devm_request_irq(&pdev->dev, irq, dw100_irq_handler, IRQF_ONESHOT,
  1276. dev_name(&pdev->dev), dw_dev);
  1277. if (ret < 0) {
  1278. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  1279. goto err_pm;
  1280. }
  1281. ret = v4l2_device_register(&pdev->dev, &dw_dev->v4l2_dev);
  1282. if (ret)
  1283. goto err_pm;
  1284. vfd = dw100_init_video_device(dw_dev);
  1285. dw_dev->m2m_dev = v4l2_m2m_init(&dw100_m2m_ops);
  1286. if (IS_ERR(dw_dev->m2m_dev)) {
  1287. dev_err(&pdev->dev, "Failed to init mem2mem device\n");
  1288. ret = PTR_ERR(dw_dev->m2m_dev);
  1289. goto err_v4l2;
  1290. }
  1291. dw_dev->mdev.dev = &pdev->dev;
  1292. strscpy(dw_dev->mdev.model, "dw100", sizeof(dw_dev->mdev.model));
  1293. media_device_init(&dw_dev->mdev);
  1294. dw_dev->v4l2_dev.mdev = &dw_dev->mdev;
  1295. ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
  1296. if (ret) {
  1297. dev_err(&pdev->dev, "Failed to register video device\n");
  1298. goto err_m2m;
  1299. }
  1300. ret = v4l2_m2m_register_media_controller(dw_dev->m2m_dev, vfd,
  1301. MEDIA_ENT_F_PROC_VIDEO_SCALER);
  1302. if (ret) {
  1303. dev_err(&pdev->dev, "Failed to init mem2mem media controller\n");
  1304. goto error_v4l2;
  1305. }
  1306. ret = media_device_register(&dw_dev->mdev);
  1307. if (ret) {
  1308. dev_err(&pdev->dev, "Failed to register mem2mem media device\n");
  1309. goto error_m2m_mc;
  1310. }
  1311. dw100_debugfs_init(dw_dev);
  1312. dev_info(&pdev->dev,
  1313. "dw100 v4l2 m2m registered as /dev/video%u\n", vfd->num);
  1314. return 0;
  1315. error_m2m_mc:
  1316. v4l2_m2m_unregister_media_controller(dw_dev->m2m_dev);
  1317. error_v4l2:
  1318. video_unregister_device(vfd);
  1319. err_m2m:
  1320. media_device_cleanup(&dw_dev->mdev);
  1321. v4l2_m2m_release(dw_dev->m2m_dev);
  1322. err_v4l2:
  1323. v4l2_device_unregister(&dw_dev->v4l2_dev);
  1324. err_pm:
  1325. pm_runtime_disable(&pdev->dev);
  1326. return ret;
  1327. }
  1328. static void dw100_remove(struct platform_device *pdev)
  1329. {
  1330. struct dw100_device *dw_dev = platform_get_drvdata(pdev);
  1331. dw100_debugfs_exit(dw_dev);
  1332. pm_runtime_disable(&pdev->dev);
  1333. media_device_unregister(&dw_dev->mdev);
  1334. v4l2_m2m_unregister_media_controller(dw_dev->m2m_dev);
  1335. media_device_cleanup(&dw_dev->mdev);
  1336. video_unregister_device(&dw_dev->vfd);
  1337. mutex_destroy(dw_dev->vfd.lock);
  1338. v4l2_m2m_release(dw_dev->m2m_dev);
  1339. v4l2_device_unregister(&dw_dev->v4l2_dev);
  1340. }
  1341. static int __maybe_unused dw100_runtime_suspend(struct device *dev)
  1342. {
  1343. struct dw100_device *dw_dev = dev_get_drvdata(dev);
  1344. clk_bulk_disable_unprepare(dw_dev->num_clks, dw_dev->clks);
  1345. return 0;
  1346. }
  1347. static int __maybe_unused dw100_runtime_resume(struct device *dev)
  1348. {
  1349. int ret;
  1350. struct dw100_device *dw_dev = dev_get_drvdata(dev);
  1351. ret = clk_bulk_prepare_enable(dw_dev->num_clks, dw_dev->clks);
  1352. if (ret)
  1353. return ret;
  1354. dw100_hw_reset(dw_dev);
  1355. return 0;
  1356. }
  1357. static const struct dev_pm_ops dw100_pm = {
  1358. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1359. pm_runtime_force_resume)
  1360. SET_RUNTIME_PM_OPS(dw100_runtime_suspend,
  1361. dw100_runtime_resume, NULL)
  1362. };
  1363. static const struct of_device_id dw100_dt_ids[] = {
  1364. { .compatible = "nxp,imx8mp-dw100", .data = NULL },
  1365. { },
  1366. };
  1367. MODULE_DEVICE_TABLE(of, dw100_dt_ids);
  1368. static struct platform_driver dw100_driver = {
  1369. .probe = dw100_probe,
  1370. .remove_new = dw100_remove,
  1371. .driver = {
  1372. .name = DRV_NAME,
  1373. .pm = &dw100_pm,
  1374. .of_match_table = dw100_dt_ids,
  1375. },
  1376. };
  1377. module_platform_driver(dw100_driver);
  1378. MODULE_DESCRIPTION("DW100 Hardware dewarper");
  1379. MODULE_AUTHOR("Xavier Roumegue <Xavier.Roumegue@oss.nxp.com>");
  1380. MODULE_LICENSE("GPL");