sh_veu.c 32 KB

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