sh_veu.c 32 KB

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