sh_veu.c 31 KB

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