sh_veu.c 31 KB

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