sh_veu.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /*
  2. * sh-mobile VEU mem2mem driver
  3. *
  4. * Copyright (C) 2012 Renesas Electronics Corporation
  5. * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
  6. * Copyright (C) 2008 Magnus Damm
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation
  11. */
  12. #include <linux/err.h>
  13. #include <linux/fs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-dev.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-mem2mem.h>
  27. #include <media/v4l2-image-sizes.h>
  28. #include <media/videobuf2-dma-contig.h>
  29. #define VEU_STR 0x00 /* start register */
  30. #define VEU_SWR 0x10 /* src: line length */
  31. #define VEU_SSR 0x14 /* src: image size */
  32. #define VEU_SAYR 0x18 /* src: y/rgb plane address */
  33. #define VEU_SACR 0x1c /* src: c plane address */
  34. #define VEU_BSSR 0x20 /* bundle mode register */
  35. #define VEU_EDWR 0x30 /* dst: line length */
  36. #define VEU_DAYR 0x34 /* dst: y/rgb plane address */
  37. #define VEU_DACR 0x38 /* dst: c plane address */
  38. #define VEU_TRCR 0x50 /* transform control */
  39. #define VEU_RFCR 0x54 /* resize scale */
  40. #define VEU_RFSR 0x58 /* resize clip */
  41. #define VEU_ENHR 0x5c /* enhance */
  42. #define VEU_FMCR 0x70 /* filter mode */
  43. #define VEU_VTCR 0x74 /* lowpass vertical */
  44. #define VEU_HTCR 0x78 /* lowpass horizontal */
  45. #define VEU_APCR 0x80 /* color match */
  46. #define VEU_ECCR 0x84 /* color replace */
  47. #define VEU_AFXR 0x90 /* fixed mode */
  48. #define VEU_SWPR 0x94 /* swap */
  49. #define VEU_EIER 0xa0 /* interrupt mask */
  50. #define VEU_EVTR 0xa4 /* interrupt event */
  51. #define VEU_STAR 0xb0 /* status */
  52. #define VEU_BSRR 0xb4 /* reset */
  53. #define VEU_MCR00 0x200 /* color conversion matrix coefficient 00 */
  54. #define VEU_MCR01 0x204 /* color conversion matrix coefficient 01 */
  55. #define VEU_MCR02 0x208 /* color conversion matrix coefficient 02 */
  56. #define VEU_MCR10 0x20c /* color conversion matrix coefficient 10 */
  57. #define VEU_MCR11 0x210 /* color conversion matrix coefficient 11 */
  58. #define VEU_MCR12 0x214 /* color conversion matrix coefficient 12 */
  59. #define VEU_MCR20 0x218 /* color conversion matrix coefficient 20 */
  60. #define VEU_MCR21 0x21c /* color conversion matrix coefficient 21 */
  61. #define VEU_MCR22 0x220 /* color conversion matrix coefficient 22 */
  62. #define VEU_COFFR 0x224 /* color conversion offset */
  63. #define VEU_CBR 0x228 /* color conversion clip */
  64. /*
  65. * 4092x4092 max size is the normal case. In some cases it can be reduced to
  66. * 2048x2048, in other cases it can be 4092x8188 or even 8188x8188.
  67. */
  68. #define MAX_W 4092
  69. #define MAX_H 4092
  70. #define MIN_W 8
  71. #define MIN_H 8
  72. #define ALIGN_W 4
  73. /* 3 buffers of 2048 x 1536 - 3 megapixels @ 16bpp */
  74. #define VIDEO_MEM_LIMIT ALIGN(2048 * 1536 * 2 * 3, 1024 * 1024)
  75. #define MEM2MEM_DEF_TRANSLEN 1
  76. struct sh_veu_dev;
  77. struct sh_veu_file {
  78. struct sh_veu_dev *veu_dev;
  79. bool cfg_needed;
  80. };
  81. struct sh_veu_format {
  82. char *name;
  83. u32 fourcc;
  84. unsigned int depth;
  85. unsigned int ydepth;
  86. };
  87. /* video data format */
  88. struct sh_veu_vfmt {
  89. /* Replace with v4l2_rect */
  90. struct v4l2_rect frame;
  91. unsigned int bytesperline;
  92. unsigned int offset_y;
  93. unsigned int offset_c;
  94. const struct sh_veu_format *fmt;
  95. };
  96. struct sh_veu_dev {
  97. struct v4l2_device v4l2_dev;
  98. struct video_device vdev;
  99. struct v4l2_m2m_dev *m2m_dev;
  100. struct device *dev;
  101. struct v4l2_m2m_ctx *m2m_ctx;
  102. struct sh_veu_vfmt vfmt_out;
  103. struct sh_veu_vfmt vfmt_in;
  104. /* Only single user per direction so far */
  105. struct sh_veu_file *capture;
  106. struct sh_veu_file *output;
  107. struct mutex fop_lock;
  108. void __iomem *base;
  109. struct vb2_alloc_ctx *alloc_ctx;
  110. spinlock_t lock;
  111. bool is_2h;
  112. unsigned int xaction;
  113. bool aborting;
  114. };
  115. enum sh_veu_fmt_idx {
  116. SH_VEU_FMT_NV12,
  117. SH_VEU_FMT_NV16,
  118. SH_VEU_FMT_NV24,
  119. SH_VEU_FMT_RGB332,
  120. SH_VEU_FMT_RGB444,
  121. SH_VEU_FMT_RGB565,
  122. SH_VEU_FMT_RGB666,
  123. SH_VEU_FMT_RGB24,
  124. };
  125. #define DEFAULT_IN_WIDTH VGA_WIDTH
  126. #define DEFAULT_IN_HEIGHT VGA_HEIGHT
  127. #define DEFAULT_IN_FMTIDX SH_VEU_FMT_NV12
  128. #define DEFAULT_OUT_WIDTH VGA_WIDTH
  129. #define DEFAULT_OUT_HEIGHT VGA_HEIGHT
  130. #define DEFAULT_OUT_FMTIDX SH_VEU_FMT_RGB565
  131. /*
  132. * Alignment: Y-plane should be 4-byte aligned for NV12 and NV16, and 8-byte
  133. * aligned for NV24.
  134. */
  135. static const struct sh_veu_format sh_veu_fmt[] = {
  136. [SH_VEU_FMT_NV12] = { .ydepth = 8, .depth = 12, .name = "NV12", .fourcc = V4L2_PIX_FMT_NV12 },
  137. [SH_VEU_FMT_NV16] = { .ydepth = 8, .depth = 16, .name = "NV16", .fourcc = V4L2_PIX_FMT_NV16 },
  138. [SH_VEU_FMT_NV24] = { .ydepth = 8, .depth = 24, .name = "NV24", .fourcc = V4L2_PIX_FMT_NV24 },
  139. [SH_VEU_FMT_RGB332] = { .ydepth = 8, .depth = 8, .name = "RGB332", .fourcc = V4L2_PIX_FMT_RGB332 },
  140. [SH_VEU_FMT_RGB444] = { .ydepth = 16, .depth = 16, .name = "RGB444", .fourcc = V4L2_PIX_FMT_RGB444 },
  141. [SH_VEU_FMT_RGB565] = { .ydepth = 16, .depth = 16, .name = "RGB565", .fourcc = V4L2_PIX_FMT_RGB565 },
  142. [SH_VEU_FMT_RGB666] = { .ydepth = 32, .depth = 32, .name = "BGR666", .fourcc = V4L2_PIX_FMT_BGR666 },
  143. [SH_VEU_FMT_RGB24] = { .ydepth = 24, .depth = 24, .name = "RGB24", .fourcc = V4L2_PIX_FMT_RGB24 },
  144. };
  145. #define DEFAULT_IN_VFMT (struct sh_veu_vfmt){ \
  146. .frame = { \
  147. .width = VGA_WIDTH, \
  148. .height = VGA_HEIGHT, \
  149. }, \
  150. .bytesperline = (VGA_WIDTH * sh_veu_fmt[DEFAULT_IN_FMTIDX].ydepth) >> 3, \
  151. .fmt = &sh_veu_fmt[DEFAULT_IN_FMTIDX], \
  152. }
  153. #define DEFAULT_OUT_VFMT (struct sh_veu_vfmt){ \
  154. .frame = { \
  155. .width = VGA_WIDTH, \
  156. .height = VGA_HEIGHT, \
  157. }, \
  158. .bytesperline = (VGA_WIDTH * sh_veu_fmt[DEFAULT_OUT_FMTIDX].ydepth) >> 3, \
  159. .fmt = &sh_veu_fmt[DEFAULT_OUT_FMTIDX], \
  160. }
  161. /*
  162. * TODO: add support for further output formats:
  163. * SH_VEU_FMT_NV12,
  164. * SH_VEU_FMT_NV16,
  165. * SH_VEU_FMT_NV24,
  166. * SH_VEU_FMT_RGB332,
  167. * SH_VEU_FMT_RGB444,
  168. * SH_VEU_FMT_RGB666,
  169. * SH_VEU_FMT_RGB24,
  170. */
  171. static const int sh_veu_fmt_out[] = {
  172. SH_VEU_FMT_RGB565,
  173. };
  174. /*
  175. * TODO: add support for further input formats:
  176. * SH_VEU_FMT_NV16,
  177. * SH_VEU_FMT_NV24,
  178. * SH_VEU_FMT_RGB565,
  179. * SH_VEU_FMT_RGB666,
  180. * SH_VEU_FMT_RGB24,
  181. */
  182. static const int sh_veu_fmt_in[] = {
  183. SH_VEU_FMT_NV12,
  184. };
  185. static enum v4l2_colorspace sh_veu_4cc2cspace(u32 fourcc)
  186. {
  187. switch (fourcc) {
  188. default:
  189. BUG();
  190. case V4L2_PIX_FMT_NV12:
  191. case V4L2_PIX_FMT_NV16:
  192. case V4L2_PIX_FMT_NV24:
  193. return V4L2_COLORSPACE_SMPTE170M;
  194. case V4L2_PIX_FMT_RGB332:
  195. case V4L2_PIX_FMT_RGB444:
  196. case V4L2_PIX_FMT_RGB565:
  197. case V4L2_PIX_FMT_BGR666:
  198. case V4L2_PIX_FMT_RGB24:
  199. return V4L2_COLORSPACE_SRGB;
  200. }
  201. }
  202. static u32 sh_veu_reg_read(struct sh_veu_dev *veu, unsigned int reg)
  203. {
  204. return ioread32(veu->base + reg);
  205. }
  206. static void sh_veu_reg_write(struct sh_veu_dev *veu, unsigned int reg,
  207. u32 value)
  208. {
  209. iowrite32(value, veu->base + reg);
  210. }
  211. /* ========== mem2mem callbacks ========== */
  212. static void sh_veu_job_abort(void *priv)
  213. {
  214. struct sh_veu_dev *veu = priv;
  215. /* Will cancel the transaction in the next interrupt handler */
  216. veu->aborting = true;
  217. }
  218. static void sh_veu_process(struct sh_veu_dev *veu,
  219. struct vb2_buffer *src_buf,
  220. struct vb2_buffer *dst_buf)
  221. {
  222. dma_addr_t addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  223. sh_veu_reg_write(veu, VEU_DAYR, addr + veu->vfmt_out.offset_y);
  224. sh_veu_reg_write(veu, VEU_DACR, veu->vfmt_out.offset_c ?
  225. addr + veu->vfmt_out.offset_c : 0);
  226. dev_dbg(veu->dev, "%s(): dst base %lx, y: %x, c: %x\n", __func__,
  227. (unsigned long)addr,
  228. veu->vfmt_out.offset_y, veu->vfmt_out.offset_c);
  229. addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  230. sh_veu_reg_write(veu, VEU_SAYR, addr + veu->vfmt_in.offset_y);
  231. sh_veu_reg_write(veu, VEU_SACR, veu->vfmt_in.offset_c ?
  232. addr + veu->vfmt_in.offset_c : 0);
  233. dev_dbg(veu->dev, "%s(): src base %lx, y: %x, c: %x\n", __func__,
  234. (unsigned long)addr,
  235. veu->vfmt_in.offset_y, veu->vfmt_in.offset_c);
  236. sh_veu_reg_write(veu, VEU_STR, 1);
  237. sh_veu_reg_write(veu, VEU_EIER, 1); /* enable interrupt in VEU */
  238. }
  239. /**
  240. * sh_veu_device_run() - prepares and starts the device
  241. *
  242. * This will be called by the framework when it decides to schedule a particular
  243. * instance.
  244. */
  245. static void sh_veu_device_run(void *priv)
  246. {
  247. struct sh_veu_dev *veu = priv;
  248. struct vb2_buffer *src_buf, *dst_buf;
  249. src_buf = v4l2_m2m_next_src_buf(veu->m2m_ctx);
  250. dst_buf = v4l2_m2m_next_dst_buf(veu->m2m_ctx);
  251. if (src_buf && dst_buf)
  252. sh_veu_process(veu, src_buf, dst_buf);
  253. }
  254. /* ========== video ioctls ========== */
  255. static bool sh_veu_is_streamer(struct sh_veu_dev *veu, struct sh_veu_file *veu_file,
  256. enum v4l2_buf_type type)
  257. {
  258. return (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  259. veu_file == veu->capture) ||
  260. (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  261. veu_file == veu->output);
  262. }
  263. static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
  264. struct vb2_queue *dst_vq);
  265. /*
  266. * It is not unusual to have video nodes open()ed multiple times. While some
  267. * V4L2 operations are non-intrusive, like querying formats and various
  268. * parameters, others, like setting formats, starting and stopping streaming,
  269. * queuing and dequeuing buffers, directly affect hardware configuration and /
  270. * or execution. This function verifies availability of the requested interface
  271. * and, if available, reserves it for the requesting user.
  272. */
  273. static int sh_veu_stream_init(struct sh_veu_dev *veu, struct sh_veu_file *veu_file,
  274. enum v4l2_buf_type type)
  275. {
  276. struct sh_veu_file **stream;
  277. switch (type) {
  278. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  279. stream = &veu->capture;
  280. break;
  281. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  282. stream = &veu->output;
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. if (*stream == veu_file)
  288. return 0;
  289. if (*stream)
  290. return -EBUSY;
  291. *stream = veu_file;
  292. return 0;
  293. }
  294. static int sh_veu_context_init(struct sh_veu_dev *veu)
  295. {
  296. if (veu->m2m_ctx)
  297. return 0;
  298. veu->m2m_ctx = v4l2_m2m_ctx_init(veu->m2m_dev, veu,
  299. sh_veu_queue_init);
  300. return PTR_ERR_OR_ZERO(veu->m2m_ctx);
  301. }
  302. static int sh_veu_querycap(struct file *file, void *priv,
  303. struct v4l2_capability *cap)
  304. {
  305. strlcpy(cap->driver, "sh-veu", sizeof(cap->driver));
  306. strlcpy(cap->card, "sh-mobile VEU", sizeof(cap->card));
  307. strlcpy(cap->bus_info, "platform:sh-veu", sizeof(cap->bus_info));
  308. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  309. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  310. return 0;
  311. }
  312. static int sh_veu_enum_fmt(struct v4l2_fmtdesc *f, const int *fmt, int fmt_num)
  313. {
  314. if (f->index >= fmt_num)
  315. return -EINVAL;
  316. strlcpy(f->description, sh_veu_fmt[fmt[f->index]].name, sizeof(f->description));
  317. f->pixelformat = sh_veu_fmt[fmt[f->index]].fourcc;
  318. return 0;
  319. }
  320. static int sh_veu_enum_fmt_vid_cap(struct file *file, void *priv,
  321. struct v4l2_fmtdesc *f)
  322. {
  323. return sh_veu_enum_fmt(f, sh_veu_fmt_out, ARRAY_SIZE(sh_veu_fmt_out));
  324. }
  325. static int sh_veu_enum_fmt_vid_out(struct file *file, void *priv,
  326. struct v4l2_fmtdesc *f)
  327. {
  328. return sh_veu_enum_fmt(f, sh_veu_fmt_in, ARRAY_SIZE(sh_veu_fmt_in));
  329. }
  330. static struct sh_veu_vfmt *sh_veu_get_vfmt(struct sh_veu_dev *veu,
  331. enum v4l2_buf_type type)
  332. {
  333. switch (type) {
  334. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  335. return &veu->vfmt_out;
  336. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  337. return &veu->vfmt_in;
  338. default:
  339. return NULL;
  340. }
  341. }
  342. static int sh_veu_g_fmt(struct sh_veu_file *veu_file, struct v4l2_format *f)
  343. {
  344. struct v4l2_pix_format *pix = &f->fmt.pix;
  345. struct sh_veu_dev *veu = veu_file->veu_dev;
  346. struct sh_veu_vfmt *vfmt;
  347. vfmt = sh_veu_get_vfmt(veu, f->type);
  348. pix->width = vfmt->frame.width;
  349. pix->height = vfmt->frame.height;
  350. pix->field = V4L2_FIELD_NONE;
  351. pix->pixelformat = vfmt->fmt->fourcc;
  352. pix->colorspace = sh_veu_4cc2cspace(pix->pixelformat);
  353. pix->bytesperline = vfmt->bytesperline;
  354. pix->sizeimage = vfmt->bytesperline * pix->height *
  355. vfmt->fmt->depth / vfmt->fmt->ydepth;
  356. dev_dbg(veu->dev, "%s(): type: %d, size %u @ %ux%u, fmt %x\n", __func__,
  357. f->type, pix->sizeimage, pix->width, pix->height, pix->pixelformat);
  358. return 0;
  359. }
  360. static int sh_veu_g_fmt_vid_out(struct file *file, void *priv,
  361. struct v4l2_format *f)
  362. {
  363. return sh_veu_g_fmt(priv, f);
  364. }
  365. static int sh_veu_g_fmt_vid_cap(struct file *file, void *priv,
  366. struct v4l2_format *f)
  367. {
  368. return sh_veu_g_fmt(priv, f);
  369. }
  370. static int sh_veu_try_fmt(struct v4l2_format *f, const struct sh_veu_format *fmt)
  371. {
  372. struct v4l2_pix_format *pix = &f->fmt.pix;
  373. unsigned int y_bytes_used;
  374. /*
  375. * V4L2 specification suggests, that the driver should correct the
  376. * format struct if any of the dimensions is unsupported
  377. */
  378. switch (pix->field) {
  379. default:
  380. case V4L2_FIELD_ANY:
  381. pix->field = V4L2_FIELD_NONE;
  382. /* fall through: continue handling V4L2_FIELD_NONE */
  383. case V4L2_FIELD_NONE:
  384. break;
  385. }
  386. v4l_bound_align_image(&pix->width, MIN_W, MAX_W, ALIGN_W,
  387. &pix->height, MIN_H, MAX_H, 0, 0);
  388. y_bytes_used = (pix->width * fmt->ydepth) >> 3;
  389. if (pix->bytesperline < y_bytes_used)
  390. pix->bytesperline = y_bytes_used;
  391. pix->sizeimage = pix->height * pix->bytesperline * fmt->depth / fmt->ydepth;
  392. pix->pixelformat = fmt->fourcc;
  393. pix->colorspace = sh_veu_4cc2cspace(pix->pixelformat);
  394. pr_debug("%s(): type: %d, size %u\n", __func__, f->type, pix->sizeimage);
  395. return 0;
  396. }
  397. static const struct sh_veu_format *sh_veu_find_fmt(const struct v4l2_format *f)
  398. {
  399. const int *fmt;
  400. int i, n, dflt;
  401. pr_debug("%s(%d;%d)\n", __func__, f->type, f->fmt.pix.field);
  402. switch (f->type) {
  403. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  404. fmt = sh_veu_fmt_out;
  405. n = ARRAY_SIZE(sh_veu_fmt_out);
  406. dflt = DEFAULT_OUT_FMTIDX;
  407. break;
  408. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  409. default:
  410. fmt = sh_veu_fmt_in;
  411. n = ARRAY_SIZE(sh_veu_fmt_in);
  412. dflt = DEFAULT_IN_FMTIDX;
  413. break;
  414. }
  415. for (i = 0; i < n; i++)
  416. if (sh_veu_fmt[fmt[i]].fourcc == f->fmt.pix.pixelformat)
  417. return &sh_veu_fmt[fmt[i]];
  418. return &sh_veu_fmt[dflt];
  419. }
  420. static int sh_veu_try_fmt_vid_cap(struct file *file, void *priv,
  421. struct v4l2_format *f)
  422. {
  423. const struct sh_veu_format *fmt;
  424. fmt = sh_veu_find_fmt(f);
  425. if (!fmt)
  426. /* wrong buffer type */
  427. return -EINVAL;
  428. return sh_veu_try_fmt(f, fmt);
  429. }
  430. static int sh_veu_try_fmt_vid_out(struct file *file, void *priv,
  431. struct v4l2_format *f)
  432. {
  433. const struct sh_veu_format *fmt;
  434. fmt = sh_veu_find_fmt(f);
  435. if (!fmt)
  436. /* wrong buffer type */
  437. return -EINVAL;
  438. return sh_veu_try_fmt(f, fmt);
  439. }
  440. static void sh_veu_colour_offset(struct sh_veu_dev *veu, struct sh_veu_vfmt *vfmt)
  441. {
  442. /* dst_left and dst_top validity will be verified in CROP / COMPOSE */
  443. unsigned int left = vfmt->frame.left & ~0x03;
  444. unsigned int top = vfmt->frame.top;
  445. dma_addr_t offset = ((left * veu->vfmt_out.fmt->depth) >> 3) +
  446. top * veu->vfmt_out.bytesperline;
  447. unsigned int y_line;
  448. vfmt->offset_y = offset;
  449. switch (vfmt->fmt->fourcc) {
  450. case V4L2_PIX_FMT_NV12:
  451. case V4L2_PIX_FMT_NV16:
  452. case V4L2_PIX_FMT_NV24:
  453. y_line = ALIGN(vfmt->frame.width, 16);
  454. vfmt->offset_c = offset + y_line * vfmt->frame.height;
  455. break;
  456. case V4L2_PIX_FMT_RGB332:
  457. case V4L2_PIX_FMT_RGB444:
  458. case V4L2_PIX_FMT_RGB565:
  459. case V4L2_PIX_FMT_BGR666:
  460. case V4L2_PIX_FMT_RGB24:
  461. vfmt->offset_c = 0;
  462. break;
  463. default:
  464. BUG();
  465. }
  466. }
  467. static int sh_veu_s_fmt(struct sh_veu_file *veu_file, struct v4l2_format *f)
  468. {
  469. struct v4l2_pix_format *pix = &f->fmt.pix;
  470. struct sh_veu_dev *veu = veu_file->veu_dev;
  471. struct sh_veu_vfmt *vfmt;
  472. struct vb2_queue *vq;
  473. int ret = sh_veu_context_init(veu);
  474. if (ret < 0)
  475. return ret;
  476. vq = v4l2_m2m_get_vq(veu->m2m_ctx, f->type);
  477. if (!vq)
  478. return -EINVAL;
  479. if (vb2_is_busy(vq)) {
  480. v4l2_err(&veu_file->veu_dev->v4l2_dev, "%s queue busy\n", __func__);
  481. return -EBUSY;
  482. }
  483. vfmt = sh_veu_get_vfmt(veu, f->type);
  484. /* called after try_fmt(), hence vfmt != NULL. Implicit BUG_ON() below */
  485. vfmt->fmt = sh_veu_find_fmt(f);
  486. /* vfmt->fmt != NULL following the same argument as above */
  487. vfmt->frame.width = pix->width;
  488. vfmt->frame.height = pix->height;
  489. vfmt->bytesperline = pix->bytesperline;
  490. sh_veu_colour_offset(veu, vfmt);
  491. /*
  492. * We could also verify and require configuration only if any parameters
  493. * actually have changed, but it is unlikely, that the user requests the
  494. * same configuration several times without closing the device.
  495. */
  496. veu_file->cfg_needed = true;
  497. dev_dbg(veu->dev,
  498. "Setting format for type %d, wxh: %dx%d, fmt: %x\n",
  499. f->type, pix->width, pix->height, vfmt->fmt->fourcc);
  500. return 0;
  501. }
  502. static int sh_veu_s_fmt_vid_cap(struct file *file, void *priv,
  503. struct v4l2_format *f)
  504. {
  505. int ret = sh_veu_try_fmt_vid_cap(file, priv, f);
  506. if (ret)
  507. return ret;
  508. return sh_veu_s_fmt(priv, f);
  509. }
  510. static int sh_veu_s_fmt_vid_out(struct file *file, void *priv,
  511. struct v4l2_format *f)
  512. {
  513. int ret = sh_veu_try_fmt_vid_out(file, priv, f);
  514. if (ret)
  515. return ret;
  516. return sh_veu_s_fmt(priv, f);
  517. }
  518. static int sh_veu_reqbufs(struct file *file, void *priv,
  519. struct v4l2_requestbuffers *reqbufs)
  520. {
  521. struct sh_veu_file *veu_file = priv;
  522. struct sh_veu_dev *veu = veu_file->veu_dev;
  523. int ret = sh_veu_context_init(veu);
  524. if (ret < 0)
  525. return ret;
  526. ret = sh_veu_stream_init(veu, veu_file, reqbufs->type);
  527. if (ret < 0)
  528. return ret;
  529. return v4l2_m2m_reqbufs(file, veu->m2m_ctx, reqbufs);
  530. }
  531. static int sh_veu_querybuf(struct file *file, void *priv,
  532. struct v4l2_buffer *buf)
  533. {
  534. struct sh_veu_file *veu_file = priv;
  535. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
  536. return -EBUSY;
  537. return v4l2_m2m_querybuf(file, veu_file->veu_dev->m2m_ctx, buf);
  538. }
  539. static int sh_veu_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  540. {
  541. struct sh_veu_file *veu_file = priv;
  542. dev_dbg(veu_file->veu_dev->dev, "%s(%d)\n", __func__, buf->type);
  543. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
  544. return -EBUSY;
  545. return v4l2_m2m_qbuf(file, veu_file->veu_dev->m2m_ctx, buf);
  546. }
  547. static int sh_veu_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  548. {
  549. struct sh_veu_file *veu_file = priv;
  550. dev_dbg(veu_file->veu_dev->dev, "%s(%d)\n", __func__, buf->type);
  551. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
  552. return -EBUSY;
  553. return v4l2_m2m_dqbuf(file, veu_file->veu_dev->m2m_ctx, buf);
  554. }
  555. static void sh_veu_calc_scale(struct sh_veu_dev *veu,
  556. int size_in, int size_out, int crop_out,
  557. u32 *mant, u32 *frac, u32 *rep)
  558. {
  559. u32 fixpoint;
  560. /* calculate FRAC and MANT */
  561. *rep = *mant = *frac = 0;
  562. if (size_in == size_out) {
  563. if (crop_out != size_out)
  564. *mant = 1; /* needed for cropping */
  565. return;
  566. }
  567. /* VEU2H special upscale */
  568. if (veu->is_2h && size_out > size_in) {
  569. u32 fixpoint = (4096 * size_in) / size_out;
  570. *mant = fixpoint / 4096;
  571. *frac = (fixpoint - (*mant * 4096)) & ~0x07;
  572. switch (*frac) {
  573. case 0x800:
  574. *rep = 1;
  575. break;
  576. case 0x400:
  577. *rep = 3;
  578. break;
  579. case 0x200:
  580. *rep = 7;
  581. break;
  582. }
  583. if (*rep)
  584. return;
  585. }
  586. fixpoint = (4096 * (size_in - 1)) / (size_out + 1);
  587. *mant = fixpoint / 4096;
  588. *frac = fixpoint - (*mant * 4096);
  589. if (*frac & 0x07) {
  590. /*
  591. * FIXME: do we really have to round down twice in the
  592. * up-scaling case?
  593. */
  594. *frac &= ~0x07;
  595. if (size_out > size_in)
  596. *frac -= 8; /* round down if scaling up */
  597. else
  598. *frac += 8; /* round up if scaling down */
  599. }
  600. }
  601. static unsigned long sh_veu_scale_v(struct sh_veu_dev *veu,
  602. int size_in, int size_out, int crop_out)
  603. {
  604. u32 mant, frac, value, rep;
  605. sh_veu_calc_scale(veu, size_in, size_out, crop_out, &mant, &frac, &rep);
  606. /* set scale */
  607. value = (sh_veu_reg_read(veu, VEU_RFCR) & ~0xffff0000) |
  608. (((mant << 12) | frac) << 16);
  609. sh_veu_reg_write(veu, VEU_RFCR, value);
  610. /* set clip */
  611. value = (sh_veu_reg_read(veu, VEU_RFSR) & ~0xffff0000) |
  612. (((rep << 12) | crop_out) << 16);
  613. sh_veu_reg_write(veu, VEU_RFSR, value);
  614. return ALIGN((size_in * crop_out) / size_out, 4);
  615. }
  616. static unsigned long sh_veu_scale_h(struct sh_veu_dev *veu,
  617. int size_in, int size_out, int crop_out)
  618. {
  619. u32 mant, frac, value, rep;
  620. sh_veu_calc_scale(veu, size_in, size_out, crop_out, &mant, &frac, &rep);
  621. /* set scale */
  622. value = (sh_veu_reg_read(veu, VEU_RFCR) & ~0xffff) |
  623. (mant << 12) | frac;
  624. sh_veu_reg_write(veu, VEU_RFCR, value);
  625. /* set clip */
  626. value = (sh_veu_reg_read(veu, VEU_RFSR) & ~0xffff) |
  627. (rep << 12) | crop_out;
  628. sh_veu_reg_write(veu, VEU_RFSR, value);
  629. return ALIGN((size_in * crop_out) / size_out, 4);
  630. }
  631. static void sh_veu_configure(struct sh_veu_dev *veu)
  632. {
  633. u32 src_width, src_stride, src_height;
  634. u32 dst_width, dst_stride, dst_height;
  635. u32 real_w, real_h;
  636. /* reset VEU */
  637. sh_veu_reg_write(veu, VEU_BSRR, 0x100);
  638. src_width = veu->vfmt_in.frame.width;
  639. src_height = veu->vfmt_in.frame.height;
  640. src_stride = ALIGN(veu->vfmt_in.frame.width, 16);
  641. dst_width = real_w = veu->vfmt_out.frame.width;
  642. dst_height = real_h = veu->vfmt_out.frame.height;
  643. /* Datasheet is unclear - whether it's always number of bytes or not */
  644. dst_stride = veu->vfmt_out.bytesperline;
  645. /*
  646. * So far real_w == dst_width && real_h == dst_height, but it wasn't
  647. * necessarily the case in the original vidix driver, so, it may change
  648. * here in the future too.
  649. */
  650. src_width = sh_veu_scale_h(veu, src_width, real_w, dst_width);
  651. src_height = sh_veu_scale_v(veu, src_height, real_h, dst_height);
  652. sh_veu_reg_write(veu, VEU_SWR, src_stride);
  653. sh_veu_reg_write(veu, VEU_SSR, src_width | (src_height << 16));
  654. sh_veu_reg_write(veu, VEU_BSSR, 0); /* not using bundle mode */
  655. sh_veu_reg_write(veu, VEU_EDWR, dst_stride);
  656. sh_veu_reg_write(veu, VEU_DACR, 0); /* unused for RGB */
  657. sh_veu_reg_write(veu, VEU_SWPR, 0x67);
  658. sh_veu_reg_write(veu, VEU_TRCR, (6 << 16) | (0 << 14) | 2 | 4);
  659. if (veu->is_2h) {
  660. sh_veu_reg_write(veu, VEU_MCR00, 0x0cc5);
  661. sh_veu_reg_write(veu, VEU_MCR01, 0x0950);
  662. sh_veu_reg_write(veu, VEU_MCR02, 0x0000);
  663. sh_veu_reg_write(veu, VEU_MCR10, 0x397f);
  664. sh_veu_reg_write(veu, VEU_MCR11, 0x0950);
  665. sh_veu_reg_write(veu, VEU_MCR12, 0x3ccd);
  666. sh_veu_reg_write(veu, VEU_MCR20, 0x0000);
  667. sh_veu_reg_write(veu, VEU_MCR21, 0x0950);
  668. sh_veu_reg_write(veu, VEU_MCR22, 0x1023);
  669. sh_veu_reg_write(veu, VEU_COFFR, 0x00800010);
  670. }
  671. }
  672. static int sh_veu_streamon(struct file *file, void *priv,
  673. enum v4l2_buf_type type)
  674. {
  675. struct sh_veu_file *veu_file = priv;
  676. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, type))
  677. return -EBUSY;
  678. if (veu_file->cfg_needed) {
  679. struct sh_veu_dev *veu = veu_file->veu_dev;
  680. veu_file->cfg_needed = false;
  681. sh_veu_configure(veu_file->veu_dev);
  682. veu->xaction = 0;
  683. veu->aborting = false;
  684. }
  685. return v4l2_m2m_streamon(file, veu_file->veu_dev->m2m_ctx, type);
  686. }
  687. static int sh_veu_streamoff(struct file *file, void *priv,
  688. enum v4l2_buf_type type)
  689. {
  690. struct sh_veu_file *veu_file = priv;
  691. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, type))
  692. return -EBUSY;
  693. return v4l2_m2m_streamoff(file, veu_file->veu_dev->m2m_ctx, type);
  694. }
  695. static const struct v4l2_ioctl_ops sh_veu_ioctl_ops = {
  696. .vidioc_querycap = sh_veu_querycap,
  697. .vidioc_enum_fmt_vid_cap = sh_veu_enum_fmt_vid_cap,
  698. .vidioc_g_fmt_vid_cap = sh_veu_g_fmt_vid_cap,
  699. .vidioc_try_fmt_vid_cap = sh_veu_try_fmt_vid_cap,
  700. .vidioc_s_fmt_vid_cap = sh_veu_s_fmt_vid_cap,
  701. .vidioc_enum_fmt_vid_out = sh_veu_enum_fmt_vid_out,
  702. .vidioc_g_fmt_vid_out = sh_veu_g_fmt_vid_out,
  703. .vidioc_try_fmt_vid_out = sh_veu_try_fmt_vid_out,
  704. .vidioc_s_fmt_vid_out = sh_veu_s_fmt_vid_out,
  705. .vidioc_reqbufs = sh_veu_reqbufs,
  706. .vidioc_querybuf = sh_veu_querybuf,
  707. .vidioc_qbuf = sh_veu_qbuf,
  708. .vidioc_dqbuf = sh_veu_dqbuf,
  709. .vidioc_streamon = sh_veu_streamon,
  710. .vidioc_streamoff = sh_veu_streamoff,
  711. };
  712. /* ========== Queue operations ========== */
  713. static int sh_veu_queue_setup(struct vb2_queue *vq,
  714. const struct v4l2_format *f,
  715. unsigned int *nbuffers, unsigned int *nplanes,
  716. unsigned int sizes[], void *alloc_ctxs[])
  717. {
  718. struct sh_veu_dev *veu = vb2_get_drv_priv(vq);
  719. struct sh_veu_vfmt *vfmt;
  720. unsigned int size, count = *nbuffers;
  721. if (f) {
  722. const struct v4l2_pix_format *pix = &f->fmt.pix;
  723. const struct sh_veu_format *fmt = sh_veu_find_fmt(f);
  724. struct v4l2_format ftmp = *f;
  725. if (fmt->fourcc != pix->pixelformat)
  726. return -EINVAL;
  727. sh_veu_try_fmt(&ftmp, fmt);
  728. if (ftmp.fmt.pix.width != pix->width ||
  729. ftmp.fmt.pix.height != pix->height)
  730. return -EINVAL;
  731. size = pix->bytesperline ? pix->bytesperline * pix->height * fmt->depth / fmt->ydepth :
  732. pix->width * pix->height * fmt->depth / fmt->ydepth;
  733. } else {
  734. vfmt = sh_veu_get_vfmt(veu, vq->type);
  735. size = vfmt->bytesperline * vfmt->frame.height * vfmt->fmt->depth / vfmt->fmt->ydepth;
  736. }
  737. if (count < 2)
  738. *nbuffers = count = 2;
  739. if (size * count > VIDEO_MEM_LIMIT) {
  740. count = VIDEO_MEM_LIMIT / size;
  741. *nbuffers = count;
  742. }
  743. *nplanes = 1;
  744. sizes[0] = size;
  745. alloc_ctxs[0] = veu->alloc_ctx;
  746. dev_dbg(veu->dev, "get %d buffer(s) of size %d each.\n", count, size);
  747. return 0;
  748. }
  749. static int sh_veu_buf_prepare(struct vb2_buffer *vb)
  750. {
  751. struct sh_veu_dev *veu = vb2_get_drv_priv(vb->vb2_queue);
  752. struct sh_veu_vfmt *vfmt;
  753. unsigned int sizeimage;
  754. vfmt = sh_veu_get_vfmt(veu, vb->vb2_queue->type);
  755. sizeimage = vfmt->bytesperline * vfmt->frame.height *
  756. vfmt->fmt->depth / vfmt->fmt->ydepth;
  757. if (vb2_plane_size(vb, 0) < sizeimage) {
  758. dev_dbg(veu->dev, "%s data will not fit into plane (%lu < %u)\n",
  759. __func__, vb2_plane_size(vb, 0), sizeimage);
  760. return -EINVAL;
  761. }
  762. vb2_set_plane_payload(vb, 0, sizeimage);
  763. return 0;
  764. }
  765. static void sh_veu_buf_queue(struct vb2_buffer *vb)
  766. {
  767. struct sh_veu_dev *veu = vb2_get_drv_priv(vb->vb2_queue);
  768. dev_dbg(veu->dev, "%s(%d)\n", __func__, vb->v4l2_buf.type);
  769. v4l2_m2m_buf_queue(veu->m2m_ctx, vb);
  770. }
  771. static const struct vb2_ops sh_veu_qops = {
  772. .queue_setup = sh_veu_queue_setup,
  773. .buf_prepare = sh_veu_buf_prepare,
  774. .buf_queue = sh_veu_buf_queue,
  775. .wait_prepare = vb2_ops_wait_prepare,
  776. .wait_finish = vb2_ops_wait_finish,
  777. };
  778. static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
  779. struct vb2_queue *dst_vq)
  780. {
  781. struct sh_veu_dev *veu = priv;
  782. int ret;
  783. memset(src_vq, 0, sizeof(*src_vq));
  784. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  785. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  786. src_vq->drv_priv = veu;
  787. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  788. src_vq->ops = &sh_veu_qops;
  789. src_vq->mem_ops = &vb2_dma_contig_memops;
  790. src_vq->lock = &veu->fop_lock;
  791. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  792. ret = vb2_queue_init(src_vq);
  793. if (ret < 0)
  794. return ret;
  795. memset(dst_vq, 0, sizeof(*dst_vq));
  796. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  797. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  798. dst_vq->drv_priv = veu;
  799. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  800. dst_vq->ops = &sh_veu_qops;
  801. dst_vq->mem_ops = &vb2_dma_contig_memops;
  802. dst_vq->lock = &veu->fop_lock;
  803. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  804. return vb2_queue_init(dst_vq);
  805. }
  806. /* ========== File operations ========== */
  807. static int sh_veu_open(struct file *file)
  808. {
  809. struct sh_veu_dev *veu = video_drvdata(file);
  810. struct sh_veu_file *veu_file;
  811. veu_file = kzalloc(sizeof(*veu_file), GFP_KERNEL);
  812. if (!veu_file)
  813. return -ENOMEM;
  814. veu_file->veu_dev = veu;
  815. veu_file->cfg_needed = true;
  816. file->private_data = veu_file;
  817. pm_runtime_get_sync(veu->dev);
  818. dev_dbg(veu->dev, "Created instance %p\n", veu_file);
  819. return 0;
  820. }
  821. static int sh_veu_release(struct file *file)
  822. {
  823. struct sh_veu_dev *veu = video_drvdata(file);
  824. struct sh_veu_file *veu_file = file->private_data;
  825. dev_dbg(veu->dev, "Releasing instance %p\n", veu_file);
  826. if (veu_file == veu->capture) {
  827. veu->capture = NULL;
  828. vb2_queue_release(v4l2_m2m_get_vq(veu->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE));
  829. }
  830. if (veu_file == veu->output) {
  831. veu->output = NULL;
  832. vb2_queue_release(v4l2_m2m_get_vq(veu->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT));
  833. }
  834. if (!veu->output && !veu->capture && veu->m2m_ctx) {
  835. v4l2_m2m_ctx_release(veu->m2m_ctx);
  836. veu->m2m_ctx = NULL;
  837. }
  838. pm_runtime_put(veu->dev);
  839. kfree(veu_file);
  840. return 0;
  841. }
  842. static unsigned int sh_veu_poll(struct file *file,
  843. struct poll_table_struct *wait)
  844. {
  845. struct sh_veu_file *veu_file = file->private_data;
  846. return v4l2_m2m_poll(file, veu_file->veu_dev->m2m_ctx, wait);
  847. }
  848. static int sh_veu_mmap(struct file *file, struct vm_area_struct *vma)
  849. {
  850. struct sh_veu_file *veu_file = file->private_data;
  851. return v4l2_m2m_mmap(file, veu_file->veu_dev->m2m_ctx, vma);
  852. }
  853. static const struct v4l2_file_operations sh_veu_fops = {
  854. .owner = THIS_MODULE,
  855. .open = sh_veu_open,
  856. .release = sh_veu_release,
  857. .poll = sh_veu_poll,
  858. .unlocked_ioctl = video_ioctl2,
  859. .mmap = sh_veu_mmap,
  860. };
  861. static const struct video_device sh_veu_videodev = {
  862. .name = "sh-veu",
  863. .fops = &sh_veu_fops,
  864. .ioctl_ops = &sh_veu_ioctl_ops,
  865. .minor = -1,
  866. .release = video_device_release_empty,
  867. .vfl_dir = VFL_DIR_M2M,
  868. };
  869. static const struct v4l2_m2m_ops sh_veu_m2m_ops = {
  870. .device_run = sh_veu_device_run,
  871. .job_abort = sh_veu_job_abort,
  872. };
  873. static irqreturn_t sh_veu_bh(int irq, void *dev_id)
  874. {
  875. struct sh_veu_dev *veu = dev_id;
  876. if (veu->xaction == MEM2MEM_DEF_TRANSLEN || veu->aborting) {
  877. v4l2_m2m_job_finish(veu->m2m_dev, veu->m2m_ctx);
  878. veu->xaction = 0;
  879. } else {
  880. sh_veu_device_run(veu);
  881. }
  882. return IRQ_HANDLED;
  883. }
  884. static irqreturn_t sh_veu_isr(int irq, void *dev_id)
  885. {
  886. struct sh_veu_dev *veu = dev_id;
  887. struct vb2_buffer *dst;
  888. struct vb2_buffer *src;
  889. u32 status = sh_veu_reg_read(veu, VEU_EVTR);
  890. /* bundle read mode not used */
  891. if (!(status & 1))
  892. return IRQ_NONE;
  893. /* disable interrupt in VEU */
  894. sh_veu_reg_write(veu, VEU_EIER, 0);
  895. /* halt operation */
  896. sh_veu_reg_write(veu, VEU_STR, 0);
  897. /* ack int, write 0 to clear bits */
  898. sh_veu_reg_write(veu, VEU_EVTR, status & ~1);
  899. /* conversion completed */
  900. dst = v4l2_m2m_dst_buf_remove(veu->m2m_ctx);
  901. src = v4l2_m2m_src_buf_remove(veu->m2m_ctx);
  902. if (!src || !dst)
  903. return IRQ_NONE;
  904. dst->v4l2_buf.timestamp = src->v4l2_buf.timestamp;
  905. dst->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  906. dst->v4l2_buf.flags |=
  907. src->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  908. dst->v4l2_buf.timecode = src->v4l2_buf.timecode;
  909. spin_lock(&veu->lock);
  910. v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
  911. v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
  912. spin_unlock(&veu->lock);
  913. veu->xaction++;
  914. return IRQ_WAKE_THREAD;
  915. }
  916. static int sh_veu_probe(struct platform_device *pdev)
  917. {
  918. struct sh_veu_dev *veu;
  919. struct resource *reg_res;
  920. struct video_device *vdev;
  921. int irq, ret;
  922. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  923. irq = platform_get_irq(pdev, 0);
  924. if (!reg_res || irq <= 0) {
  925. dev_err(&pdev->dev, "Insufficient VEU platform information.\n");
  926. return -ENODEV;
  927. }
  928. veu = devm_kzalloc(&pdev->dev, sizeof(*veu), GFP_KERNEL);
  929. if (!veu)
  930. return -ENOMEM;
  931. veu->is_2h = resource_size(reg_res) == 0x22c;
  932. veu->base = devm_ioremap_resource(&pdev->dev, reg_res);
  933. if (IS_ERR(veu->base))
  934. return PTR_ERR(veu->base);
  935. ret = devm_request_threaded_irq(&pdev->dev, irq, sh_veu_isr, sh_veu_bh,
  936. 0, "veu", veu);
  937. if (ret < 0)
  938. return ret;
  939. ret = v4l2_device_register(&pdev->dev, &veu->v4l2_dev);
  940. if (ret < 0) {
  941. dev_err(&pdev->dev, "Error registering v4l2 device\n");
  942. return ret;
  943. }
  944. vdev = &veu->vdev;
  945. veu->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  946. if (IS_ERR(veu->alloc_ctx)) {
  947. ret = PTR_ERR(veu->alloc_ctx);
  948. goto einitctx;
  949. }
  950. *vdev = sh_veu_videodev;
  951. vdev->v4l2_dev = &veu->v4l2_dev;
  952. spin_lock_init(&veu->lock);
  953. mutex_init(&veu->fop_lock);
  954. vdev->lock = &veu->fop_lock;
  955. video_set_drvdata(vdev, veu);
  956. veu->dev = &pdev->dev;
  957. veu->vfmt_out = DEFAULT_OUT_VFMT;
  958. veu->vfmt_in = DEFAULT_IN_VFMT;
  959. veu->m2m_dev = v4l2_m2m_init(&sh_veu_m2m_ops);
  960. if (IS_ERR(veu->m2m_dev)) {
  961. ret = PTR_ERR(veu->m2m_dev);
  962. v4l2_err(&veu->v4l2_dev, "Failed to init mem2mem device: %d\n", ret);
  963. goto em2minit;
  964. }
  965. pm_runtime_enable(&pdev->dev);
  966. pm_runtime_resume(&pdev->dev);
  967. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  968. pm_runtime_suspend(&pdev->dev);
  969. if (ret < 0)
  970. goto evidreg;
  971. return ret;
  972. evidreg:
  973. pm_runtime_disable(&pdev->dev);
  974. v4l2_m2m_release(veu->m2m_dev);
  975. em2minit:
  976. vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
  977. einitctx:
  978. v4l2_device_unregister(&veu->v4l2_dev);
  979. return ret;
  980. }
  981. static int sh_veu_remove(struct platform_device *pdev)
  982. {
  983. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  984. struct sh_veu_dev *veu = container_of(v4l2_dev,
  985. struct sh_veu_dev, v4l2_dev);
  986. video_unregister_device(&veu->vdev);
  987. pm_runtime_disable(&pdev->dev);
  988. v4l2_m2m_release(veu->m2m_dev);
  989. vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
  990. v4l2_device_unregister(&veu->v4l2_dev);
  991. return 0;
  992. }
  993. static struct platform_driver __refdata sh_veu_pdrv = {
  994. .remove = sh_veu_remove,
  995. .driver = {
  996. .name = "sh_veu",
  997. },
  998. };
  999. module_platform_driver_probe(sh_veu_pdrv, sh_veu_probe);
  1000. MODULE_DESCRIPTION("sh-mobile VEU mem2mem driver");
  1001. MODULE_AUTHOR("Guennadi Liakhovetski, <g.liakhovetski@gmx.de>");
  1002. MODULE_LICENSE("GPL v2");