sh_veu.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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_JPEG;
  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_lock(void *priv)
  219. {
  220. struct sh_veu_dev *veu = priv;
  221. mutex_lock(&veu->fop_lock);
  222. }
  223. static void sh_veu_unlock(void *priv)
  224. {
  225. struct sh_veu_dev *veu = priv;
  226. mutex_unlock(&veu->fop_lock);
  227. }
  228. static void sh_veu_process(struct sh_veu_dev *veu,
  229. struct vb2_buffer *src_buf,
  230. struct vb2_buffer *dst_buf)
  231. {
  232. dma_addr_t addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
  233. sh_veu_reg_write(veu, VEU_DAYR, addr + veu->vfmt_out.offset_y);
  234. sh_veu_reg_write(veu, VEU_DACR, veu->vfmt_out.offset_c ?
  235. addr + veu->vfmt_out.offset_c : 0);
  236. dev_dbg(veu->dev, "%s(): dst base %lx, y: %x, c: %x\n", __func__,
  237. (unsigned long)addr,
  238. veu->vfmt_out.offset_y, veu->vfmt_out.offset_c);
  239. addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
  240. sh_veu_reg_write(veu, VEU_SAYR, addr + veu->vfmt_in.offset_y);
  241. sh_veu_reg_write(veu, VEU_SACR, veu->vfmt_in.offset_c ?
  242. addr + veu->vfmt_in.offset_c : 0);
  243. dev_dbg(veu->dev, "%s(): src base %lx, y: %x, c: %x\n", __func__,
  244. (unsigned long)addr,
  245. veu->vfmt_in.offset_y, veu->vfmt_in.offset_c);
  246. sh_veu_reg_write(veu, VEU_STR, 1);
  247. sh_veu_reg_write(veu, VEU_EIER, 1); /* enable interrupt in VEU */
  248. }
  249. /**
  250. * sh_veu_device_run() - prepares and starts the device
  251. *
  252. * This will be called by the framework when it decides to schedule a particular
  253. * instance.
  254. */
  255. static void sh_veu_device_run(void *priv)
  256. {
  257. struct sh_veu_dev *veu = priv;
  258. struct vb2_buffer *src_buf, *dst_buf;
  259. src_buf = v4l2_m2m_next_src_buf(veu->m2m_ctx);
  260. dst_buf = v4l2_m2m_next_dst_buf(veu->m2m_ctx);
  261. if (src_buf && dst_buf)
  262. sh_veu_process(veu, src_buf, dst_buf);
  263. }
  264. /* ========== video ioctls ========== */
  265. static bool sh_veu_is_streamer(struct sh_veu_dev *veu, struct sh_veu_file *veu_file,
  266. enum v4l2_buf_type type)
  267. {
  268. return (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  269. veu_file == veu->capture) ||
  270. (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  271. veu_file == veu->output);
  272. }
  273. static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
  274. struct vb2_queue *dst_vq);
  275. /*
  276. * It is not unusual to have video nodes open()ed multiple times. While some
  277. * V4L2 operations are non-intrusive, like querying formats and various
  278. * parameters, others, like setting formats, starting and stopping streaming,
  279. * queuing and dequeuing buffers, directly affect hardware configuration and /
  280. * or execution. This function verifies availability of the requested interface
  281. * and, if available, reserves it for the requesting user.
  282. */
  283. static int sh_veu_stream_init(struct sh_veu_dev *veu, struct sh_veu_file *veu_file,
  284. enum v4l2_buf_type type)
  285. {
  286. struct sh_veu_file **stream;
  287. switch (type) {
  288. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  289. stream = &veu->capture;
  290. break;
  291. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  292. stream = &veu->output;
  293. break;
  294. default:
  295. return -EINVAL;
  296. }
  297. if (*stream == veu_file)
  298. return 0;
  299. if (*stream)
  300. return -EBUSY;
  301. *stream = veu_file;
  302. return 0;
  303. }
  304. static int sh_veu_context_init(struct sh_veu_dev *veu)
  305. {
  306. if (veu->m2m_ctx)
  307. return 0;
  308. veu->m2m_ctx = v4l2_m2m_ctx_init(veu->m2m_dev, veu,
  309. sh_veu_queue_init);
  310. return PTR_ERR_OR_ZERO(veu->m2m_ctx);
  311. }
  312. static int sh_veu_querycap(struct file *file, void *priv,
  313. struct v4l2_capability *cap)
  314. {
  315. strlcpy(cap->driver, "sh-veu", sizeof(cap->driver));
  316. strlcpy(cap->card, "sh-mobile VEU", sizeof(cap->card));
  317. strlcpy(cap->bus_info, "platform:sh-veu", sizeof(cap->bus_info));
  318. cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
  319. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  320. return 0;
  321. }
  322. static int sh_veu_enum_fmt(struct v4l2_fmtdesc *f, const int *fmt, int fmt_num)
  323. {
  324. if (f->index >= fmt_num)
  325. return -EINVAL;
  326. strlcpy(f->description, sh_veu_fmt[fmt[f->index]].name, sizeof(f->description));
  327. f->pixelformat = sh_veu_fmt[fmt[f->index]].fourcc;
  328. return 0;
  329. }
  330. static int sh_veu_enum_fmt_vid_cap(struct file *file, void *priv,
  331. struct v4l2_fmtdesc *f)
  332. {
  333. return sh_veu_enum_fmt(f, sh_veu_fmt_out, ARRAY_SIZE(sh_veu_fmt_out));
  334. }
  335. static int sh_veu_enum_fmt_vid_out(struct file *file, void *priv,
  336. struct v4l2_fmtdesc *f)
  337. {
  338. return sh_veu_enum_fmt(f, sh_veu_fmt_in, ARRAY_SIZE(sh_veu_fmt_in));
  339. }
  340. static struct sh_veu_vfmt *sh_veu_get_vfmt(struct sh_veu_dev *veu,
  341. enum v4l2_buf_type type)
  342. {
  343. switch (type) {
  344. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  345. return &veu->vfmt_out;
  346. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  347. return &veu->vfmt_in;
  348. default:
  349. return NULL;
  350. }
  351. }
  352. static int sh_veu_g_fmt(struct sh_veu_file *veu_file, struct v4l2_format *f)
  353. {
  354. struct v4l2_pix_format *pix = &f->fmt.pix;
  355. struct sh_veu_dev *veu = veu_file->veu_dev;
  356. struct sh_veu_vfmt *vfmt;
  357. vfmt = sh_veu_get_vfmt(veu, f->type);
  358. pix->width = vfmt->frame.width;
  359. pix->height = vfmt->frame.height;
  360. pix->field = V4L2_FIELD_NONE;
  361. pix->pixelformat = vfmt->fmt->fourcc;
  362. pix->colorspace = sh_veu_4cc2cspace(pix->pixelformat);
  363. pix->bytesperline = vfmt->bytesperline;
  364. pix->sizeimage = vfmt->bytesperline * pix->height *
  365. vfmt->fmt->depth / vfmt->fmt->ydepth;
  366. dev_dbg(veu->dev, "%s(): type: %d, size %u @ %ux%u, fmt %x\n", __func__,
  367. f->type, pix->sizeimage, pix->width, pix->height, pix->pixelformat);
  368. return 0;
  369. }
  370. static int sh_veu_g_fmt_vid_out(struct file *file, void *priv,
  371. struct v4l2_format *f)
  372. {
  373. return sh_veu_g_fmt(priv, f);
  374. }
  375. static int sh_veu_g_fmt_vid_cap(struct file *file, void *priv,
  376. struct v4l2_format *f)
  377. {
  378. return sh_veu_g_fmt(priv, f);
  379. }
  380. static int sh_veu_try_fmt(struct v4l2_format *f, const struct sh_veu_format *fmt)
  381. {
  382. struct v4l2_pix_format *pix = &f->fmt.pix;
  383. unsigned int y_bytes_used;
  384. /*
  385. * V4L2 specification suggests, that the driver should correct the
  386. * format struct if any of the dimensions is unsupported
  387. */
  388. switch (pix->field) {
  389. default:
  390. case V4L2_FIELD_ANY:
  391. pix->field = V4L2_FIELD_NONE;
  392. /* fall through: continue handling V4L2_FIELD_NONE */
  393. case V4L2_FIELD_NONE:
  394. break;
  395. }
  396. v4l_bound_align_image(&pix->width, MIN_W, MAX_W, ALIGN_W,
  397. &pix->height, MIN_H, MAX_H, 0, 0);
  398. y_bytes_used = (pix->width * fmt->ydepth) >> 3;
  399. if (pix->bytesperline < y_bytes_used)
  400. pix->bytesperline = y_bytes_used;
  401. pix->sizeimage = pix->height * pix->bytesperline * fmt->depth / fmt->ydepth;
  402. pix->pixelformat = fmt->fourcc;
  403. pix->colorspace = sh_veu_4cc2cspace(pix->pixelformat);
  404. pr_debug("%s(): type: %d, size %u\n", __func__, f->type, pix->sizeimage);
  405. return 0;
  406. }
  407. static const struct sh_veu_format *sh_veu_find_fmt(const struct v4l2_format *f)
  408. {
  409. const int *fmt;
  410. int i, n, dflt;
  411. pr_debug("%s(%d;%d)\n", __func__, f->type, f->fmt.pix.field);
  412. switch (f->type) {
  413. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  414. fmt = sh_veu_fmt_out;
  415. n = ARRAY_SIZE(sh_veu_fmt_out);
  416. dflt = DEFAULT_OUT_FMTIDX;
  417. break;
  418. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  419. default:
  420. fmt = sh_veu_fmt_in;
  421. n = ARRAY_SIZE(sh_veu_fmt_in);
  422. dflt = DEFAULT_IN_FMTIDX;
  423. break;
  424. }
  425. for (i = 0; i < n; i++)
  426. if (sh_veu_fmt[fmt[i]].fourcc == f->fmt.pix.pixelformat)
  427. return &sh_veu_fmt[fmt[i]];
  428. return &sh_veu_fmt[dflt];
  429. }
  430. static int sh_veu_try_fmt_vid_cap(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 int sh_veu_try_fmt_vid_out(struct file *file, void *priv,
  441. struct v4l2_format *f)
  442. {
  443. const struct sh_veu_format *fmt;
  444. fmt = sh_veu_find_fmt(f);
  445. if (!fmt)
  446. /* wrong buffer type */
  447. return -EINVAL;
  448. return sh_veu_try_fmt(f, fmt);
  449. }
  450. static void sh_veu_colour_offset(struct sh_veu_dev *veu, struct sh_veu_vfmt *vfmt)
  451. {
  452. /* dst_left and dst_top validity will be verified in CROP / COMPOSE */
  453. unsigned int left = vfmt->frame.left & ~0x03;
  454. unsigned int top = vfmt->frame.top;
  455. dma_addr_t offset = ((left * veu->vfmt_out.fmt->depth) >> 3) +
  456. top * veu->vfmt_out.bytesperline;
  457. unsigned int y_line;
  458. vfmt->offset_y = offset;
  459. switch (vfmt->fmt->fourcc) {
  460. case V4L2_PIX_FMT_NV12:
  461. case V4L2_PIX_FMT_NV16:
  462. case V4L2_PIX_FMT_NV24:
  463. y_line = ALIGN(vfmt->frame.width, 16);
  464. vfmt->offset_c = offset + y_line * vfmt->frame.height;
  465. break;
  466. case V4L2_PIX_FMT_RGB332:
  467. case V4L2_PIX_FMT_RGB444:
  468. case V4L2_PIX_FMT_RGB565:
  469. case V4L2_PIX_FMT_BGR666:
  470. case V4L2_PIX_FMT_RGB24:
  471. vfmt->offset_c = 0;
  472. break;
  473. default:
  474. BUG();
  475. }
  476. }
  477. static int sh_veu_s_fmt(struct sh_veu_file *veu_file, struct v4l2_format *f)
  478. {
  479. struct v4l2_pix_format *pix = &f->fmt.pix;
  480. struct sh_veu_dev *veu = veu_file->veu_dev;
  481. struct sh_veu_vfmt *vfmt;
  482. struct vb2_queue *vq;
  483. int ret = sh_veu_context_init(veu);
  484. if (ret < 0)
  485. return ret;
  486. vq = v4l2_m2m_get_vq(veu->m2m_ctx, f->type);
  487. if (!vq)
  488. return -EINVAL;
  489. if (vb2_is_busy(vq)) {
  490. v4l2_err(&veu_file->veu_dev->v4l2_dev, "%s queue busy\n", __func__);
  491. return -EBUSY;
  492. }
  493. vfmt = sh_veu_get_vfmt(veu, f->type);
  494. /* called after try_fmt(), hence vfmt != NULL. Implicit BUG_ON() below */
  495. vfmt->fmt = sh_veu_find_fmt(f);
  496. /* vfmt->fmt != NULL following the same argument as above */
  497. vfmt->frame.width = pix->width;
  498. vfmt->frame.height = pix->height;
  499. vfmt->bytesperline = pix->bytesperline;
  500. sh_veu_colour_offset(veu, vfmt);
  501. /*
  502. * We could also verify and require configuration only if any parameters
  503. * actually have changed, but it is unlikely, that the user requests the
  504. * same configuration several times without closing the device.
  505. */
  506. veu_file->cfg_needed = true;
  507. dev_dbg(veu->dev,
  508. "Setting format for type %d, wxh: %dx%d, fmt: %x\n",
  509. f->type, pix->width, pix->height, vfmt->fmt->fourcc);
  510. return 0;
  511. }
  512. static int sh_veu_s_fmt_vid_cap(struct file *file, void *priv,
  513. struct v4l2_format *f)
  514. {
  515. int ret = sh_veu_try_fmt_vid_cap(file, priv, f);
  516. if (ret)
  517. return ret;
  518. return sh_veu_s_fmt(priv, f);
  519. }
  520. static int sh_veu_s_fmt_vid_out(struct file *file, void *priv,
  521. struct v4l2_format *f)
  522. {
  523. int ret = sh_veu_try_fmt_vid_out(file, priv, f);
  524. if (ret)
  525. return ret;
  526. return sh_veu_s_fmt(priv, f);
  527. }
  528. static int sh_veu_reqbufs(struct file *file, void *priv,
  529. struct v4l2_requestbuffers *reqbufs)
  530. {
  531. struct sh_veu_file *veu_file = priv;
  532. struct sh_veu_dev *veu = veu_file->veu_dev;
  533. int ret = sh_veu_context_init(veu);
  534. if (ret < 0)
  535. return ret;
  536. ret = sh_veu_stream_init(veu, veu_file, reqbufs->type);
  537. if (ret < 0)
  538. return ret;
  539. return v4l2_m2m_reqbufs(file, veu->m2m_ctx, reqbufs);
  540. }
  541. static int sh_veu_querybuf(struct file *file, void *priv,
  542. struct v4l2_buffer *buf)
  543. {
  544. struct sh_veu_file *veu_file = priv;
  545. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
  546. return -EBUSY;
  547. return v4l2_m2m_querybuf(file, veu_file->veu_dev->m2m_ctx, buf);
  548. }
  549. static int sh_veu_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  550. {
  551. struct sh_veu_file *veu_file = priv;
  552. dev_dbg(veu_file->veu_dev->dev, "%s(%d)\n", __func__, buf->type);
  553. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
  554. return -EBUSY;
  555. return v4l2_m2m_qbuf(file, veu_file->veu_dev->m2m_ctx, buf);
  556. }
  557. static int sh_veu_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  558. {
  559. struct sh_veu_file *veu_file = priv;
  560. dev_dbg(veu_file->veu_dev->dev, "%s(%d)\n", __func__, buf->type);
  561. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, buf->type))
  562. return -EBUSY;
  563. return v4l2_m2m_dqbuf(file, veu_file->veu_dev->m2m_ctx, buf);
  564. }
  565. static void sh_veu_calc_scale(struct sh_veu_dev *veu,
  566. int size_in, int size_out, int crop_out,
  567. u32 *mant, u32 *frac, u32 *rep)
  568. {
  569. u32 fixpoint;
  570. /* calculate FRAC and MANT */
  571. *rep = *mant = *frac = 0;
  572. if (size_in == size_out) {
  573. if (crop_out != size_out)
  574. *mant = 1; /* needed for cropping */
  575. return;
  576. }
  577. /* VEU2H special upscale */
  578. if (veu->is_2h && size_out > size_in) {
  579. u32 fixpoint = (4096 * size_in) / size_out;
  580. *mant = fixpoint / 4096;
  581. *frac = (fixpoint - (*mant * 4096)) & ~0x07;
  582. switch (*frac) {
  583. case 0x800:
  584. *rep = 1;
  585. break;
  586. case 0x400:
  587. *rep = 3;
  588. break;
  589. case 0x200:
  590. *rep = 7;
  591. break;
  592. }
  593. if (*rep)
  594. return;
  595. }
  596. fixpoint = (4096 * (size_in - 1)) / (size_out + 1);
  597. *mant = fixpoint / 4096;
  598. *frac = fixpoint - (*mant * 4096);
  599. if (*frac & 0x07) {
  600. /*
  601. * FIXME: do we really have to round down twice in the
  602. * up-scaling case?
  603. */
  604. *frac &= ~0x07;
  605. if (size_out > size_in)
  606. *frac -= 8; /* round down if scaling up */
  607. else
  608. *frac += 8; /* round up if scaling down */
  609. }
  610. }
  611. static unsigned long sh_veu_scale_v(struct sh_veu_dev *veu,
  612. int size_in, int size_out, int crop_out)
  613. {
  614. u32 mant, frac, value, rep;
  615. sh_veu_calc_scale(veu, size_in, size_out, crop_out, &mant, &frac, &rep);
  616. /* set scale */
  617. value = (sh_veu_reg_read(veu, VEU_RFCR) & ~0xffff0000) |
  618. (((mant << 12) | frac) << 16);
  619. sh_veu_reg_write(veu, VEU_RFCR, value);
  620. /* set clip */
  621. value = (sh_veu_reg_read(veu, VEU_RFSR) & ~0xffff0000) |
  622. (((rep << 12) | crop_out) << 16);
  623. sh_veu_reg_write(veu, VEU_RFSR, value);
  624. return ALIGN((size_in * crop_out) / size_out, 4);
  625. }
  626. static unsigned long sh_veu_scale_h(struct sh_veu_dev *veu,
  627. int size_in, int size_out, int crop_out)
  628. {
  629. u32 mant, frac, value, rep;
  630. sh_veu_calc_scale(veu, size_in, size_out, crop_out, &mant, &frac, &rep);
  631. /* set scale */
  632. value = (sh_veu_reg_read(veu, VEU_RFCR) & ~0xffff) |
  633. (mant << 12) | frac;
  634. sh_veu_reg_write(veu, VEU_RFCR, value);
  635. /* set clip */
  636. value = (sh_veu_reg_read(veu, VEU_RFSR) & ~0xffff) |
  637. (rep << 12) | crop_out;
  638. sh_veu_reg_write(veu, VEU_RFSR, value);
  639. return ALIGN((size_in * crop_out) / size_out, 4);
  640. }
  641. static void sh_veu_configure(struct sh_veu_dev *veu)
  642. {
  643. u32 src_width, src_stride, src_height;
  644. u32 dst_width, dst_stride, dst_height;
  645. u32 real_w, real_h;
  646. /* reset VEU */
  647. sh_veu_reg_write(veu, VEU_BSRR, 0x100);
  648. src_width = veu->vfmt_in.frame.width;
  649. src_height = veu->vfmt_in.frame.height;
  650. src_stride = ALIGN(veu->vfmt_in.frame.width, 16);
  651. dst_width = real_w = veu->vfmt_out.frame.width;
  652. dst_height = real_h = veu->vfmt_out.frame.height;
  653. /* Datasheet is unclear - whether it's always number of bytes or not */
  654. dst_stride = veu->vfmt_out.bytesperline;
  655. /*
  656. * So far real_w == dst_width && real_h == dst_height, but it wasn't
  657. * necessarily the case in the original vidix driver, so, it may change
  658. * here in the future too.
  659. */
  660. src_width = sh_veu_scale_h(veu, src_width, real_w, dst_width);
  661. src_height = sh_veu_scale_v(veu, src_height, real_h, dst_height);
  662. sh_veu_reg_write(veu, VEU_SWR, src_stride);
  663. sh_veu_reg_write(veu, VEU_SSR, src_width | (src_height << 16));
  664. sh_veu_reg_write(veu, VEU_BSSR, 0); /* not using bundle mode */
  665. sh_veu_reg_write(veu, VEU_EDWR, dst_stride);
  666. sh_veu_reg_write(veu, VEU_DACR, 0); /* unused for RGB */
  667. sh_veu_reg_write(veu, VEU_SWPR, 0x67);
  668. sh_veu_reg_write(veu, VEU_TRCR, (6 << 16) | (0 << 14) | 2 | 4);
  669. if (veu->is_2h) {
  670. sh_veu_reg_write(veu, VEU_MCR00, 0x0cc5);
  671. sh_veu_reg_write(veu, VEU_MCR01, 0x0950);
  672. sh_veu_reg_write(veu, VEU_MCR02, 0x0000);
  673. sh_veu_reg_write(veu, VEU_MCR10, 0x397f);
  674. sh_veu_reg_write(veu, VEU_MCR11, 0x0950);
  675. sh_veu_reg_write(veu, VEU_MCR12, 0x3ccd);
  676. sh_veu_reg_write(veu, VEU_MCR20, 0x0000);
  677. sh_veu_reg_write(veu, VEU_MCR21, 0x0950);
  678. sh_veu_reg_write(veu, VEU_MCR22, 0x1023);
  679. sh_veu_reg_write(veu, VEU_COFFR, 0x00800010);
  680. }
  681. }
  682. static int sh_veu_streamon(struct file *file, void *priv,
  683. enum v4l2_buf_type type)
  684. {
  685. struct sh_veu_file *veu_file = priv;
  686. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, type))
  687. return -EBUSY;
  688. if (veu_file->cfg_needed) {
  689. struct sh_veu_dev *veu = veu_file->veu_dev;
  690. veu_file->cfg_needed = false;
  691. sh_veu_configure(veu_file->veu_dev);
  692. veu->xaction = 0;
  693. veu->aborting = false;
  694. }
  695. return v4l2_m2m_streamon(file, veu_file->veu_dev->m2m_ctx, type);
  696. }
  697. static int sh_veu_streamoff(struct file *file, void *priv,
  698. enum v4l2_buf_type type)
  699. {
  700. struct sh_veu_file *veu_file = priv;
  701. if (!sh_veu_is_streamer(veu_file->veu_dev, veu_file, type))
  702. return -EBUSY;
  703. return v4l2_m2m_streamoff(file, veu_file->veu_dev->m2m_ctx, type);
  704. }
  705. static const struct v4l2_ioctl_ops sh_veu_ioctl_ops = {
  706. .vidioc_querycap = sh_veu_querycap,
  707. .vidioc_enum_fmt_vid_cap = sh_veu_enum_fmt_vid_cap,
  708. .vidioc_g_fmt_vid_cap = sh_veu_g_fmt_vid_cap,
  709. .vidioc_try_fmt_vid_cap = sh_veu_try_fmt_vid_cap,
  710. .vidioc_s_fmt_vid_cap = sh_veu_s_fmt_vid_cap,
  711. .vidioc_enum_fmt_vid_out = sh_veu_enum_fmt_vid_out,
  712. .vidioc_g_fmt_vid_out = sh_veu_g_fmt_vid_out,
  713. .vidioc_try_fmt_vid_out = sh_veu_try_fmt_vid_out,
  714. .vidioc_s_fmt_vid_out = sh_veu_s_fmt_vid_out,
  715. .vidioc_reqbufs = sh_veu_reqbufs,
  716. .vidioc_querybuf = sh_veu_querybuf,
  717. .vidioc_qbuf = sh_veu_qbuf,
  718. .vidioc_dqbuf = sh_veu_dqbuf,
  719. .vidioc_streamon = sh_veu_streamon,
  720. .vidioc_streamoff = sh_veu_streamoff,
  721. };
  722. /* ========== Queue operations ========== */
  723. static int sh_veu_queue_setup(struct vb2_queue *vq,
  724. const struct v4l2_format *f,
  725. unsigned int *nbuffers, unsigned int *nplanes,
  726. unsigned int sizes[], void *alloc_ctxs[])
  727. {
  728. struct sh_veu_dev *veu = vb2_get_drv_priv(vq);
  729. struct sh_veu_vfmt *vfmt;
  730. unsigned int size, count = *nbuffers;
  731. if (f) {
  732. const struct v4l2_pix_format *pix = &f->fmt.pix;
  733. const struct sh_veu_format *fmt = sh_veu_find_fmt(f);
  734. struct v4l2_format ftmp = *f;
  735. if (fmt->fourcc != pix->pixelformat)
  736. return -EINVAL;
  737. sh_veu_try_fmt(&ftmp, fmt);
  738. if (ftmp.fmt.pix.width != pix->width ||
  739. ftmp.fmt.pix.height != pix->height)
  740. return -EINVAL;
  741. size = pix->bytesperline ? pix->bytesperline * pix->height * fmt->depth / fmt->ydepth :
  742. pix->width * pix->height * fmt->depth / fmt->ydepth;
  743. } else {
  744. vfmt = sh_veu_get_vfmt(veu, vq->type);
  745. size = vfmt->bytesperline * vfmt->frame.height * vfmt->fmt->depth / vfmt->fmt->ydepth;
  746. }
  747. if (count < 2)
  748. *nbuffers = count = 2;
  749. if (size * count > VIDEO_MEM_LIMIT) {
  750. count = VIDEO_MEM_LIMIT / size;
  751. *nbuffers = count;
  752. }
  753. *nplanes = 1;
  754. sizes[0] = size;
  755. alloc_ctxs[0] = veu->alloc_ctx;
  756. dev_dbg(veu->dev, "get %d buffer(s) of size %d each.\n", count, size);
  757. return 0;
  758. }
  759. static int sh_veu_buf_prepare(struct vb2_buffer *vb)
  760. {
  761. struct sh_veu_dev *veu = vb2_get_drv_priv(vb->vb2_queue);
  762. struct sh_veu_vfmt *vfmt;
  763. unsigned int sizeimage;
  764. vfmt = sh_veu_get_vfmt(veu, vb->vb2_queue->type);
  765. sizeimage = vfmt->bytesperline * vfmt->frame.height *
  766. vfmt->fmt->depth / vfmt->fmt->ydepth;
  767. if (vb2_plane_size(vb, 0) < sizeimage) {
  768. dev_dbg(veu->dev, "%s data will not fit into plane (%lu < %u)\n",
  769. __func__, vb2_plane_size(vb, 0), sizeimage);
  770. return -EINVAL;
  771. }
  772. vb2_set_plane_payload(vb, 0, sizeimage);
  773. return 0;
  774. }
  775. static void sh_veu_buf_queue(struct vb2_buffer *vb)
  776. {
  777. struct sh_veu_dev *veu = vb2_get_drv_priv(vb->vb2_queue);
  778. dev_dbg(veu->dev, "%s(%d)\n", __func__, vb->v4l2_buf.type);
  779. v4l2_m2m_buf_queue(veu->m2m_ctx, vb);
  780. }
  781. static void sh_veu_wait_prepare(struct vb2_queue *q)
  782. {
  783. sh_veu_unlock(vb2_get_drv_priv(q));
  784. }
  785. static void sh_veu_wait_finish(struct vb2_queue *q)
  786. {
  787. sh_veu_lock(vb2_get_drv_priv(q));
  788. }
  789. static const struct vb2_ops sh_veu_qops = {
  790. .queue_setup = sh_veu_queue_setup,
  791. .buf_prepare = sh_veu_buf_prepare,
  792. .buf_queue = sh_veu_buf_queue,
  793. .wait_prepare = sh_veu_wait_prepare,
  794. .wait_finish = sh_veu_wait_finish,
  795. };
  796. static int sh_veu_queue_init(void *priv, struct vb2_queue *src_vq,
  797. struct vb2_queue *dst_vq)
  798. {
  799. int ret;
  800. memset(src_vq, 0, sizeof(*src_vq));
  801. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  802. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  803. src_vq->drv_priv = priv;
  804. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  805. src_vq->ops = &sh_veu_qops;
  806. src_vq->mem_ops = &vb2_dma_contig_memops;
  807. ret = vb2_queue_init(src_vq);
  808. if (ret < 0)
  809. return ret;
  810. memset(dst_vq, 0, sizeof(*dst_vq));
  811. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  812. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  813. dst_vq->drv_priv = priv;
  814. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  815. dst_vq->ops = &sh_veu_qops;
  816. dst_vq->mem_ops = &vb2_dma_contig_memops;
  817. return vb2_queue_init(dst_vq);
  818. }
  819. /* ========== File operations ========== */
  820. static int sh_veu_open(struct file *file)
  821. {
  822. struct sh_veu_dev *veu = video_drvdata(file);
  823. struct sh_veu_file *veu_file;
  824. veu_file = kzalloc(sizeof(*veu_file), GFP_KERNEL);
  825. if (!veu_file)
  826. return -ENOMEM;
  827. veu_file->veu_dev = veu;
  828. veu_file->cfg_needed = true;
  829. file->private_data = veu_file;
  830. pm_runtime_get_sync(veu->dev);
  831. dev_dbg(veu->dev, "Created instance %p\n", veu_file);
  832. return 0;
  833. }
  834. static int sh_veu_release(struct file *file)
  835. {
  836. struct sh_veu_dev *veu = video_drvdata(file);
  837. struct sh_veu_file *veu_file = file->private_data;
  838. dev_dbg(veu->dev, "Releasing instance %p\n", veu_file);
  839. if (veu_file == veu->capture) {
  840. veu->capture = NULL;
  841. vb2_queue_release(v4l2_m2m_get_vq(veu->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE));
  842. }
  843. if (veu_file == veu->output) {
  844. veu->output = NULL;
  845. vb2_queue_release(v4l2_m2m_get_vq(veu->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT));
  846. }
  847. if (!veu->output && !veu->capture && veu->m2m_ctx) {
  848. v4l2_m2m_ctx_release(veu->m2m_ctx);
  849. veu->m2m_ctx = NULL;
  850. }
  851. pm_runtime_put(veu->dev);
  852. kfree(veu_file);
  853. return 0;
  854. }
  855. static unsigned int sh_veu_poll(struct file *file,
  856. struct poll_table_struct *wait)
  857. {
  858. struct sh_veu_file *veu_file = file->private_data;
  859. return v4l2_m2m_poll(file, veu_file->veu_dev->m2m_ctx, wait);
  860. }
  861. static int sh_veu_mmap(struct file *file, struct vm_area_struct *vma)
  862. {
  863. struct sh_veu_file *veu_file = file->private_data;
  864. return v4l2_m2m_mmap(file, veu_file->veu_dev->m2m_ctx, vma);
  865. }
  866. static const struct v4l2_file_operations sh_veu_fops = {
  867. .owner = THIS_MODULE,
  868. .open = sh_veu_open,
  869. .release = sh_veu_release,
  870. .poll = sh_veu_poll,
  871. .unlocked_ioctl = video_ioctl2,
  872. .mmap = sh_veu_mmap,
  873. };
  874. static const struct video_device sh_veu_videodev = {
  875. .name = "sh-veu",
  876. .fops = &sh_veu_fops,
  877. .ioctl_ops = &sh_veu_ioctl_ops,
  878. .minor = -1,
  879. .release = video_device_release_empty,
  880. .vfl_dir = VFL_DIR_M2M,
  881. };
  882. static const struct v4l2_m2m_ops sh_veu_m2m_ops = {
  883. .device_run = sh_veu_device_run,
  884. .job_abort = sh_veu_job_abort,
  885. };
  886. static irqreturn_t sh_veu_bh(int irq, void *dev_id)
  887. {
  888. struct sh_veu_dev *veu = dev_id;
  889. if (veu->xaction == MEM2MEM_DEF_TRANSLEN || veu->aborting) {
  890. v4l2_m2m_job_finish(veu->m2m_dev, veu->m2m_ctx);
  891. veu->xaction = 0;
  892. } else {
  893. sh_veu_device_run(veu);
  894. }
  895. return IRQ_HANDLED;
  896. }
  897. static irqreturn_t sh_veu_isr(int irq, void *dev_id)
  898. {
  899. struct sh_veu_dev *veu = dev_id;
  900. struct vb2_buffer *dst;
  901. struct vb2_buffer *src;
  902. u32 status = sh_veu_reg_read(veu, VEU_EVTR);
  903. /* bundle read mode not used */
  904. if (!(status & 1))
  905. return IRQ_NONE;
  906. /* disable interrupt in VEU */
  907. sh_veu_reg_write(veu, VEU_EIER, 0);
  908. /* halt operation */
  909. sh_veu_reg_write(veu, VEU_STR, 0);
  910. /* ack int, write 0 to clear bits */
  911. sh_veu_reg_write(veu, VEU_EVTR, status & ~1);
  912. /* conversion completed */
  913. dst = v4l2_m2m_dst_buf_remove(veu->m2m_ctx);
  914. src = v4l2_m2m_src_buf_remove(veu->m2m_ctx);
  915. if (!src || !dst)
  916. return IRQ_NONE;
  917. spin_lock(&veu->lock);
  918. v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
  919. v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
  920. spin_unlock(&veu->lock);
  921. veu->xaction++;
  922. return IRQ_WAKE_THREAD;
  923. }
  924. static int sh_veu_probe(struct platform_device *pdev)
  925. {
  926. struct sh_veu_dev *veu;
  927. struct resource *reg_res;
  928. struct video_device *vdev;
  929. int irq, ret;
  930. reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  931. irq = platform_get_irq(pdev, 0);
  932. if (!reg_res || irq <= 0) {
  933. dev_err(&pdev->dev, "Insufficient VEU platform information.\n");
  934. return -ENODEV;
  935. }
  936. veu = devm_kzalloc(&pdev->dev, sizeof(*veu), GFP_KERNEL);
  937. if (!veu)
  938. return -ENOMEM;
  939. veu->is_2h = resource_size(reg_res) == 0x22c;
  940. veu->base = devm_ioremap_resource(&pdev->dev, reg_res);
  941. if (IS_ERR(veu->base))
  942. return PTR_ERR(veu->base);
  943. ret = devm_request_threaded_irq(&pdev->dev, irq, sh_veu_isr, sh_veu_bh,
  944. 0, "veu", veu);
  945. if (ret < 0)
  946. return ret;
  947. ret = v4l2_device_register(&pdev->dev, &veu->v4l2_dev);
  948. if (ret < 0) {
  949. dev_err(&pdev->dev, "Error registering v4l2 device\n");
  950. return ret;
  951. }
  952. vdev = &veu->vdev;
  953. veu->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  954. if (IS_ERR(veu->alloc_ctx)) {
  955. ret = PTR_ERR(veu->alloc_ctx);
  956. goto einitctx;
  957. }
  958. *vdev = sh_veu_videodev;
  959. spin_lock_init(&veu->lock);
  960. mutex_init(&veu->fop_lock);
  961. vdev->lock = &veu->fop_lock;
  962. video_set_drvdata(vdev, veu);
  963. veu->dev = &pdev->dev;
  964. veu->vfmt_out = DEFAULT_OUT_VFMT;
  965. veu->vfmt_in = DEFAULT_IN_VFMT;
  966. veu->m2m_dev = v4l2_m2m_init(&sh_veu_m2m_ops);
  967. if (IS_ERR(veu->m2m_dev)) {
  968. ret = PTR_ERR(veu->m2m_dev);
  969. v4l2_err(&veu->v4l2_dev, "Failed to init mem2mem device: %d\n", ret);
  970. goto em2minit;
  971. }
  972. pm_runtime_enable(&pdev->dev);
  973. pm_runtime_resume(&pdev->dev);
  974. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  975. pm_runtime_suspend(&pdev->dev);
  976. if (ret < 0)
  977. goto evidreg;
  978. return ret;
  979. evidreg:
  980. pm_runtime_disable(&pdev->dev);
  981. v4l2_m2m_release(veu->m2m_dev);
  982. em2minit:
  983. vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
  984. einitctx:
  985. v4l2_device_unregister(&veu->v4l2_dev);
  986. return ret;
  987. }
  988. static int sh_veu_remove(struct platform_device *pdev)
  989. {
  990. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  991. struct sh_veu_dev *veu = container_of(v4l2_dev,
  992. struct sh_veu_dev, v4l2_dev);
  993. video_unregister_device(&veu->vdev);
  994. pm_runtime_disable(&pdev->dev);
  995. v4l2_m2m_release(veu->m2m_dev);
  996. vb2_dma_contig_cleanup_ctx(veu->alloc_ctx);
  997. v4l2_device_unregister(&veu->v4l2_dev);
  998. return 0;
  999. }
  1000. static struct platform_driver __refdata sh_veu_pdrv = {
  1001. .remove = sh_veu_remove,
  1002. .driver = {
  1003. .name = "sh_veu",
  1004. },
  1005. };
  1006. module_platform_driver_probe(sh_veu_pdrv, sh_veu_probe);
  1007. MODULE_DESCRIPTION("sh-mobile VEU mem2mem driver");
  1008. MODULE_AUTHOR("Guennadi Liakhovetski, <g.liakhovetski@gmx.de>");
  1009. MODULE_LICENSE("GPL v2");