rcar-v4l2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Renesas R-Car VIN
  4. *
  5. * Copyright (C) 2016 Renesas Electronics Corp.
  6. * Copyright (C) 2011-2013 Renesas Solutions Corp.
  7. * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
  8. * Copyright (C) 2008 Magnus Damm
  9. *
  10. * Based on the soc-camera rcar_vin driver
  11. */
  12. #include <linux/pm_runtime.h>
  13. #include <media/v4l2-event.h>
  14. #include <media/v4l2-ioctl.h>
  15. #include <media/v4l2-mc.h>
  16. #include <media/v4l2-rect.h>
  17. #include "rcar-vin.h"
  18. #define RVIN_DEFAULT_FORMAT V4L2_PIX_FMT_YUYV
  19. #define RVIN_DEFAULT_WIDTH 800
  20. #define RVIN_DEFAULT_HEIGHT 600
  21. #define RVIN_DEFAULT_FIELD V4L2_FIELD_NONE
  22. #define RVIN_DEFAULT_COLORSPACE V4L2_COLORSPACE_SRGB
  23. /* -----------------------------------------------------------------------------
  24. * Format Conversions
  25. */
  26. static const struct rvin_video_format rvin_formats[] = {
  27. {
  28. .fourcc = V4L2_PIX_FMT_NV16,
  29. .bpp = 1,
  30. },
  31. {
  32. .fourcc = V4L2_PIX_FMT_YUYV,
  33. .bpp = 2,
  34. },
  35. {
  36. .fourcc = V4L2_PIX_FMT_UYVY,
  37. .bpp = 2,
  38. },
  39. {
  40. .fourcc = V4L2_PIX_FMT_RGB565,
  41. .bpp = 2,
  42. },
  43. {
  44. .fourcc = V4L2_PIX_FMT_XRGB555,
  45. .bpp = 2,
  46. },
  47. {
  48. .fourcc = V4L2_PIX_FMT_XBGR32,
  49. .bpp = 4,
  50. },
  51. {
  52. .fourcc = V4L2_PIX_FMT_ARGB555,
  53. .bpp = 2,
  54. },
  55. {
  56. .fourcc = V4L2_PIX_FMT_ABGR32,
  57. .bpp = 4,
  58. },
  59. };
  60. const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin,
  61. u32 pixelformat)
  62. {
  63. int i;
  64. if (vin->info->model == RCAR_M1 && pixelformat == V4L2_PIX_FMT_XBGR32)
  65. return NULL;
  66. for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
  67. if (rvin_formats[i].fourcc == pixelformat)
  68. return rvin_formats + i;
  69. return NULL;
  70. }
  71. static u32 rvin_format_bytesperline(struct rvin_dev *vin,
  72. struct v4l2_pix_format *pix)
  73. {
  74. const struct rvin_video_format *fmt;
  75. u32 align;
  76. fmt = rvin_format_from_pixel(vin, pix->pixelformat);
  77. if (WARN_ON(!fmt))
  78. return -EINVAL;
  79. align = pix->pixelformat == V4L2_PIX_FMT_NV16 ? 0x20 : 0x10;
  80. return ALIGN(pix->width, align) * fmt->bpp;
  81. }
  82. static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix)
  83. {
  84. if (pix->pixelformat == V4L2_PIX_FMT_NV16)
  85. return pix->bytesperline * pix->height * 2;
  86. return pix->bytesperline * pix->height;
  87. }
  88. static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix)
  89. {
  90. u32 walign;
  91. if (!rvin_format_from_pixel(vin, pix->pixelformat))
  92. pix->pixelformat = RVIN_DEFAULT_FORMAT;
  93. switch (pix->field) {
  94. case V4L2_FIELD_TOP:
  95. case V4L2_FIELD_BOTTOM:
  96. case V4L2_FIELD_NONE:
  97. case V4L2_FIELD_INTERLACED_TB:
  98. case V4L2_FIELD_INTERLACED_BT:
  99. case V4L2_FIELD_INTERLACED:
  100. break;
  101. case V4L2_FIELD_ALTERNATE:
  102. /*
  103. * Driver does not (yet) support outputting ALTERNATE to a
  104. * userspace. It does support outputting INTERLACED so use
  105. * the VIN hardware to combine the two fields.
  106. */
  107. pix->field = V4L2_FIELD_INTERLACED;
  108. pix->height *= 2;
  109. break;
  110. default:
  111. pix->field = RVIN_DEFAULT_FIELD;
  112. break;
  113. }
  114. /* HW limit width to a multiple of 32 (2^5) for NV16 else 2 (2^1) */
  115. walign = vin->format.pixelformat == V4L2_PIX_FMT_NV16 ? 5 : 1;
  116. /* Limit to VIN capabilities */
  117. v4l_bound_align_image(&pix->width, 2, vin->info->max_width, walign,
  118. &pix->height, 4, vin->info->max_height, 2, 0);
  119. pix->bytesperline = rvin_format_bytesperline(vin, pix);
  120. pix->sizeimage = rvin_format_sizeimage(pix);
  121. vin_dbg(vin, "Format %ux%u bpl: %u size: %u\n",
  122. pix->width, pix->height, pix->bytesperline, pix->sizeimage);
  123. }
  124. /* -----------------------------------------------------------------------------
  125. * V4L2
  126. */
  127. static int rvin_reset_format(struct rvin_dev *vin)
  128. {
  129. struct v4l2_subdev_format fmt = {
  130. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  131. .pad = vin->parallel->source_pad,
  132. };
  133. int ret;
  134. ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt);
  135. if (ret)
  136. return ret;
  137. v4l2_fill_pix_format(&vin->format, &fmt.format);
  138. rvin_format_align(vin, &vin->format);
  139. vin->source.top = 0;
  140. vin->source.left = 0;
  141. vin->source.width = vin->format.width;
  142. vin->source.height = vin->format.height;
  143. vin->crop = vin->source;
  144. vin->compose = vin->source;
  145. return 0;
  146. }
  147. static int rvin_try_format(struct rvin_dev *vin, u32 which,
  148. struct v4l2_pix_format *pix,
  149. struct v4l2_rect *crop, struct v4l2_rect *compose)
  150. {
  151. struct v4l2_subdev *sd = vin_to_source(vin);
  152. struct v4l2_subdev_pad_config *pad_cfg;
  153. struct v4l2_subdev_format format = {
  154. .which = which,
  155. .pad = vin->parallel->source_pad,
  156. };
  157. enum v4l2_field field;
  158. u32 width, height;
  159. int ret;
  160. pad_cfg = v4l2_subdev_alloc_pad_config(sd);
  161. if (pad_cfg == NULL)
  162. return -ENOMEM;
  163. if (!rvin_format_from_pixel(vin, pix->pixelformat))
  164. pix->pixelformat = RVIN_DEFAULT_FORMAT;
  165. v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code);
  166. /* Allow the video device to override field and to scale */
  167. field = pix->field;
  168. width = pix->width;
  169. height = pix->height;
  170. ret = v4l2_subdev_call(sd, pad, set_fmt, pad_cfg, &format);
  171. if (ret < 0 && ret != -ENOIOCTLCMD)
  172. goto done;
  173. ret = 0;
  174. v4l2_fill_pix_format(pix, &format.format);
  175. if (crop) {
  176. crop->top = 0;
  177. crop->left = 0;
  178. crop->width = pix->width;
  179. crop->height = pix->height;
  180. /*
  181. * If source is ALTERNATE the driver will use the VIN hardware
  182. * to INTERLACE it. The crop height then needs to be doubled.
  183. */
  184. if (pix->field == V4L2_FIELD_ALTERNATE)
  185. crop->height *= 2;
  186. }
  187. if (field != V4L2_FIELD_ANY)
  188. pix->field = field;
  189. pix->width = width;
  190. pix->height = height;
  191. rvin_format_align(vin, pix);
  192. if (compose) {
  193. compose->top = 0;
  194. compose->left = 0;
  195. compose->width = pix->width;
  196. compose->height = pix->height;
  197. }
  198. done:
  199. v4l2_subdev_free_pad_config(pad_cfg);
  200. return ret;
  201. }
  202. static int rvin_querycap(struct file *file, void *priv,
  203. struct v4l2_capability *cap)
  204. {
  205. struct rvin_dev *vin = video_drvdata(file);
  206. strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  207. strscpy(cap->card, "R_Car_VIN", sizeof(cap->card));
  208. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  209. dev_name(vin->dev));
  210. return 0;
  211. }
  212. static int rvin_try_fmt_vid_cap(struct file *file, void *priv,
  213. struct v4l2_format *f)
  214. {
  215. struct rvin_dev *vin = video_drvdata(file);
  216. return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL,
  217. NULL);
  218. }
  219. static int rvin_s_fmt_vid_cap(struct file *file, void *priv,
  220. struct v4l2_format *f)
  221. {
  222. struct rvin_dev *vin = video_drvdata(file);
  223. struct v4l2_rect crop, compose;
  224. int ret;
  225. if (vb2_is_busy(&vin->queue))
  226. return -EBUSY;
  227. ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
  228. &crop, &compose);
  229. if (ret)
  230. return ret;
  231. vin->format = f->fmt.pix;
  232. vin->crop = crop;
  233. vin->compose = compose;
  234. vin->source = crop;
  235. return 0;
  236. }
  237. static int rvin_g_fmt_vid_cap(struct file *file, void *priv,
  238. struct v4l2_format *f)
  239. {
  240. struct rvin_dev *vin = video_drvdata(file);
  241. f->fmt.pix = vin->format;
  242. return 0;
  243. }
  244. static int rvin_enum_fmt_vid_cap(struct file *file, void *priv,
  245. struct v4l2_fmtdesc *f)
  246. {
  247. if (f->index >= ARRAY_SIZE(rvin_formats))
  248. return -EINVAL;
  249. f->pixelformat = rvin_formats[f->index].fourcc;
  250. return 0;
  251. }
  252. static int rvin_g_selection(struct file *file, void *fh,
  253. struct v4l2_selection *s)
  254. {
  255. struct rvin_dev *vin = video_drvdata(file);
  256. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  257. return -EINVAL;
  258. switch (s->target) {
  259. case V4L2_SEL_TGT_CROP_BOUNDS:
  260. case V4L2_SEL_TGT_CROP_DEFAULT:
  261. s->r.left = s->r.top = 0;
  262. s->r.width = vin->source.width;
  263. s->r.height = vin->source.height;
  264. break;
  265. case V4L2_SEL_TGT_CROP:
  266. s->r = vin->crop;
  267. break;
  268. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  269. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  270. s->r.left = s->r.top = 0;
  271. s->r.width = vin->format.width;
  272. s->r.height = vin->format.height;
  273. break;
  274. case V4L2_SEL_TGT_COMPOSE:
  275. s->r = vin->compose;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. static int rvin_s_selection(struct file *file, void *fh,
  283. struct v4l2_selection *s)
  284. {
  285. struct rvin_dev *vin = video_drvdata(file);
  286. const struct rvin_video_format *fmt;
  287. struct v4l2_rect r = s->r;
  288. struct v4l2_rect max_rect;
  289. struct v4l2_rect min_rect = {
  290. .width = 6,
  291. .height = 2,
  292. };
  293. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  294. return -EINVAL;
  295. v4l2_rect_set_min_size(&r, &min_rect);
  296. switch (s->target) {
  297. case V4L2_SEL_TGT_CROP:
  298. /* Can't crop outside of source input */
  299. max_rect.top = max_rect.left = 0;
  300. max_rect.width = vin->source.width;
  301. max_rect.height = vin->source.height;
  302. v4l2_rect_map_inside(&r, &max_rect);
  303. v4l_bound_align_image(&r.width, 6, vin->source.width, 0,
  304. &r.height, 2, vin->source.height, 0, 0);
  305. r.top = clamp_t(s32, r.top, 0, vin->source.height - r.height);
  306. r.left = clamp_t(s32, r.left, 0, vin->source.width - r.width);
  307. vin->crop = s->r = r;
  308. vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n",
  309. r.width, r.height, r.left, r.top,
  310. vin->source.width, vin->source.height);
  311. break;
  312. case V4L2_SEL_TGT_COMPOSE:
  313. /* Make sure compose rect fits inside output format */
  314. max_rect.top = max_rect.left = 0;
  315. max_rect.width = vin->format.width;
  316. max_rect.height = vin->format.height;
  317. v4l2_rect_map_inside(&r, &max_rect);
  318. /*
  319. * Composing is done by adding a offset to the buffer address,
  320. * the HW wants this address to be aligned to HW_BUFFER_MASK.
  321. * Make sure the top and left values meets this requirement.
  322. */
  323. while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK)
  324. r.top--;
  325. fmt = rvin_format_from_pixel(vin, vin->format.pixelformat);
  326. while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
  327. r.left--;
  328. vin->compose = s->r = r;
  329. vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n",
  330. r.width, r.height, r.left, r.top,
  331. vin->format.width, vin->format.height);
  332. break;
  333. default:
  334. return -EINVAL;
  335. }
  336. /* HW supports modifying configuration while running */
  337. rvin_crop_scale_comp(vin);
  338. return 0;
  339. }
  340. static int rvin_g_pixelaspect(struct file *file, void *priv,
  341. int type, struct v4l2_fract *f)
  342. {
  343. struct rvin_dev *vin = video_drvdata(file);
  344. struct v4l2_subdev *sd = vin_to_source(vin);
  345. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  346. return -EINVAL;
  347. return v4l2_subdev_call(sd, video, g_pixelaspect, f);
  348. }
  349. static int rvin_enum_input(struct file *file, void *priv,
  350. struct v4l2_input *i)
  351. {
  352. struct rvin_dev *vin = video_drvdata(file);
  353. struct v4l2_subdev *sd = vin_to_source(vin);
  354. int ret;
  355. if (i->index != 0)
  356. return -EINVAL;
  357. ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
  358. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  359. return ret;
  360. i->type = V4L2_INPUT_TYPE_CAMERA;
  361. if (v4l2_subdev_has_op(sd, pad, dv_timings_cap)) {
  362. i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  363. i->std = 0;
  364. } else {
  365. i->capabilities = V4L2_IN_CAP_STD;
  366. i->std = vin->vdev.tvnorms;
  367. }
  368. strscpy(i->name, "Camera", sizeof(i->name));
  369. return 0;
  370. }
  371. static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
  372. {
  373. *i = 0;
  374. return 0;
  375. }
  376. static int rvin_s_input(struct file *file, void *priv, unsigned int i)
  377. {
  378. if (i > 0)
  379. return -EINVAL;
  380. return 0;
  381. }
  382. static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a)
  383. {
  384. struct rvin_dev *vin = video_drvdata(file);
  385. struct v4l2_subdev *sd = vin_to_source(vin);
  386. return v4l2_subdev_call(sd, video, querystd, a);
  387. }
  388. static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a)
  389. {
  390. struct rvin_dev *vin = video_drvdata(file);
  391. int ret;
  392. ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a);
  393. if (ret < 0)
  394. return ret;
  395. vin->std = a;
  396. /* Changing the standard will change the width/height */
  397. return rvin_reset_format(vin);
  398. }
  399. static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a)
  400. {
  401. struct rvin_dev *vin = video_drvdata(file);
  402. if (v4l2_subdev_has_op(vin_to_source(vin), pad, dv_timings_cap))
  403. return -ENOIOCTLCMD;
  404. *a = vin->std;
  405. return 0;
  406. }
  407. static int rvin_subscribe_event(struct v4l2_fh *fh,
  408. const struct v4l2_event_subscription *sub)
  409. {
  410. switch (sub->type) {
  411. case V4L2_EVENT_SOURCE_CHANGE:
  412. return v4l2_event_subscribe(fh, sub, 4, NULL);
  413. }
  414. return v4l2_ctrl_subscribe_event(fh, sub);
  415. }
  416. static int rvin_enum_dv_timings(struct file *file, void *priv_fh,
  417. struct v4l2_enum_dv_timings *timings)
  418. {
  419. struct rvin_dev *vin = video_drvdata(file);
  420. struct v4l2_subdev *sd = vin_to_source(vin);
  421. int ret;
  422. if (timings->pad)
  423. return -EINVAL;
  424. timings->pad = vin->parallel->sink_pad;
  425. ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
  426. timings->pad = 0;
  427. return ret;
  428. }
  429. static int rvin_s_dv_timings(struct file *file, void *priv_fh,
  430. struct v4l2_dv_timings *timings)
  431. {
  432. struct rvin_dev *vin = video_drvdata(file);
  433. struct v4l2_subdev *sd = vin_to_source(vin);
  434. int ret;
  435. ret = v4l2_subdev_call(sd, video, s_dv_timings, timings);
  436. if (ret)
  437. return ret;
  438. /* Changing the timings will change the width/height */
  439. return rvin_reset_format(vin);
  440. }
  441. static int rvin_g_dv_timings(struct file *file, void *priv_fh,
  442. struct v4l2_dv_timings *timings)
  443. {
  444. struct rvin_dev *vin = video_drvdata(file);
  445. struct v4l2_subdev *sd = vin_to_source(vin);
  446. return v4l2_subdev_call(sd, video, g_dv_timings, timings);
  447. }
  448. static int rvin_query_dv_timings(struct file *file, void *priv_fh,
  449. struct v4l2_dv_timings *timings)
  450. {
  451. struct rvin_dev *vin = video_drvdata(file);
  452. struct v4l2_subdev *sd = vin_to_source(vin);
  453. return v4l2_subdev_call(sd, video, query_dv_timings, timings);
  454. }
  455. static int rvin_dv_timings_cap(struct file *file, void *priv_fh,
  456. struct v4l2_dv_timings_cap *cap)
  457. {
  458. struct rvin_dev *vin = video_drvdata(file);
  459. struct v4l2_subdev *sd = vin_to_source(vin);
  460. int ret;
  461. if (cap->pad)
  462. return -EINVAL;
  463. cap->pad = vin->parallel->sink_pad;
  464. ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
  465. cap->pad = 0;
  466. return ret;
  467. }
  468. static int rvin_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
  469. {
  470. struct rvin_dev *vin = video_drvdata(file);
  471. struct v4l2_subdev *sd = vin_to_source(vin);
  472. int ret;
  473. if (edid->pad)
  474. return -EINVAL;
  475. edid->pad = vin->parallel->sink_pad;
  476. ret = v4l2_subdev_call(sd, pad, get_edid, edid);
  477. edid->pad = 0;
  478. return ret;
  479. }
  480. static int rvin_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
  481. {
  482. struct rvin_dev *vin = video_drvdata(file);
  483. struct v4l2_subdev *sd = vin_to_source(vin);
  484. int ret;
  485. if (edid->pad)
  486. return -EINVAL;
  487. edid->pad = vin->parallel->sink_pad;
  488. ret = v4l2_subdev_call(sd, pad, set_edid, edid);
  489. edid->pad = 0;
  490. return ret;
  491. }
  492. static const struct v4l2_ioctl_ops rvin_ioctl_ops = {
  493. .vidioc_querycap = rvin_querycap,
  494. .vidioc_try_fmt_vid_cap = rvin_try_fmt_vid_cap,
  495. .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
  496. .vidioc_s_fmt_vid_cap = rvin_s_fmt_vid_cap,
  497. .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
  498. .vidioc_g_selection = rvin_g_selection,
  499. .vidioc_s_selection = rvin_s_selection,
  500. .vidioc_g_pixelaspect = rvin_g_pixelaspect,
  501. .vidioc_enum_input = rvin_enum_input,
  502. .vidioc_g_input = rvin_g_input,
  503. .vidioc_s_input = rvin_s_input,
  504. .vidioc_dv_timings_cap = rvin_dv_timings_cap,
  505. .vidioc_enum_dv_timings = rvin_enum_dv_timings,
  506. .vidioc_g_dv_timings = rvin_g_dv_timings,
  507. .vidioc_s_dv_timings = rvin_s_dv_timings,
  508. .vidioc_query_dv_timings = rvin_query_dv_timings,
  509. .vidioc_g_edid = rvin_g_edid,
  510. .vidioc_s_edid = rvin_s_edid,
  511. .vidioc_querystd = rvin_querystd,
  512. .vidioc_g_std = rvin_g_std,
  513. .vidioc_s_std = rvin_s_std,
  514. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  515. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  516. .vidioc_querybuf = vb2_ioctl_querybuf,
  517. .vidioc_qbuf = vb2_ioctl_qbuf,
  518. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  519. .vidioc_expbuf = vb2_ioctl_expbuf,
  520. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  521. .vidioc_streamon = vb2_ioctl_streamon,
  522. .vidioc_streamoff = vb2_ioctl_streamoff,
  523. .vidioc_log_status = v4l2_ctrl_log_status,
  524. .vidioc_subscribe_event = rvin_subscribe_event,
  525. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  526. };
  527. /* -----------------------------------------------------------------------------
  528. * V4L2 Media Controller
  529. */
  530. static void rvin_mc_try_format(struct rvin_dev *vin,
  531. struct v4l2_pix_format *pix)
  532. {
  533. /*
  534. * The V4L2 specification clearly documents the colorspace fields
  535. * as being set by drivers for capture devices. Using the values
  536. * supplied by userspace thus wouldn't comply with the API. Until
  537. * the API is updated force fixed values.
  538. */
  539. pix->colorspace = RVIN_DEFAULT_COLORSPACE;
  540. pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
  541. pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
  542. pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
  543. pix->ycbcr_enc);
  544. rvin_format_align(vin, pix);
  545. }
  546. static int rvin_mc_try_fmt_vid_cap(struct file *file, void *priv,
  547. struct v4l2_format *f)
  548. {
  549. struct rvin_dev *vin = video_drvdata(file);
  550. rvin_mc_try_format(vin, &f->fmt.pix);
  551. return 0;
  552. }
  553. static int rvin_mc_s_fmt_vid_cap(struct file *file, void *priv,
  554. struct v4l2_format *f)
  555. {
  556. struct rvin_dev *vin = video_drvdata(file);
  557. if (vb2_is_busy(&vin->queue))
  558. return -EBUSY;
  559. rvin_mc_try_format(vin, &f->fmt.pix);
  560. vin->format = f->fmt.pix;
  561. vin->crop.top = 0;
  562. vin->crop.left = 0;
  563. vin->crop.width = vin->format.width;
  564. vin->crop.height = vin->format.height;
  565. vin->compose = vin->crop;
  566. return 0;
  567. }
  568. static int rvin_mc_enum_input(struct file *file, void *priv,
  569. struct v4l2_input *i)
  570. {
  571. if (i->index != 0)
  572. return -EINVAL;
  573. i->type = V4L2_INPUT_TYPE_CAMERA;
  574. strscpy(i->name, "Camera", sizeof(i->name));
  575. return 0;
  576. }
  577. static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops = {
  578. .vidioc_querycap = rvin_querycap,
  579. .vidioc_try_fmt_vid_cap = rvin_mc_try_fmt_vid_cap,
  580. .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
  581. .vidioc_s_fmt_vid_cap = rvin_mc_s_fmt_vid_cap,
  582. .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
  583. .vidioc_enum_input = rvin_mc_enum_input,
  584. .vidioc_g_input = rvin_g_input,
  585. .vidioc_s_input = rvin_s_input,
  586. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  587. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  588. .vidioc_querybuf = vb2_ioctl_querybuf,
  589. .vidioc_qbuf = vb2_ioctl_qbuf,
  590. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  591. .vidioc_expbuf = vb2_ioctl_expbuf,
  592. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  593. .vidioc_streamon = vb2_ioctl_streamon,
  594. .vidioc_streamoff = vb2_ioctl_streamoff,
  595. .vidioc_log_status = v4l2_ctrl_log_status,
  596. .vidioc_subscribe_event = rvin_subscribe_event,
  597. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  598. };
  599. /* -----------------------------------------------------------------------------
  600. * File Operations
  601. */
  602. static int rvin_power_parallel(struct rvin_dev *vin, bool on)
  603. {
  604. struct v4l2_subdev *sd = vin_to_source(vin);
  605. int power = on ? 1 : 0;
  606. int ret;
  607. ret = v4l2_subdev_call(sd, core, s_power, power);
  608. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  609. return ret;
  610. return 0;
  611. }
  612. static int rvin_open(struct file *file)
  613. {
  614. struct rvin_dev *vin = video_drvdata(file);
  615. int ret;
  616. ret = pm_runtime_get_sync(vin->dev);
  617. if (ret < 0)
  618. return ret;
  619. ret = mutex_lock_interruptible(&vin->lock);
  620. if (ret)
  621. goto err_pm;
  622. file->private_data = vin;
  623. ret = v4l2_fh_open(file);
  624. if (ret)
  625. goto err_unlock;
  626. if (vin->info->use_mc)
  627. ret = v4l2_pipeline_pm_use(&vin->vdev.entity, 1);
  628. else if (v4l2_fh_is_singular_file(file))
  629. ret = rvin_power_parallel(vin, true);
  630. if (ret < 0)
  631. goto err_open;
  632. ret = v4l2_ctrl_handler_setup(&vin->ctrl_handler);
  633. if (ret)
  634. goto err_power;
  635. mutex_unlock(&vin->lock);
  636. return 0;
  637. err_power:
  638. if (vin->info->use_mc)
  639. v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
  640. else if (v4l2_fh_is_singular_file(file))
  641. rvin_power_parallel(vin, false);
  642. err_open:
  643. v4l2_fh_release(file);
  644. err_unlock:
  645. mutex_unlock(&vin->lock);
  646. err_pm:
  647. pm_runtime_put(vin->dev);
  648. return ret;
  649. }
  650. static int rvin_release(struct file *file)
  651. {
  652. struct rvin_dev *vin = video_drvdata(file);
  653. bool fh_singular;
  654. int ret;
  655. mutex_lock(&vin->lock);
  656. /* Save the singular status before we call the clean-up helper */
  657. fh_singular = v4l2_fh_is_singular_file(file);
  658. /* the release helper will cleanup any on-going streaming */
  659. ret = _vb2_fop_release(file, NULL);
  660. if (vin->info->use_mc) {
  661. v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
  662. } else {
  663. if (fh_singular)
  664. rvin_power_parallel(vin, false);
  665. }
  666. mutex_unlock(&vin->lock);
  667. pm_runtime_put(vin->dev);
  668. return ret;
  669. }
  670. static const struct v4l2_file_operations rvin_fops = {
  671. .owner = THIS_MODULE,
  672. .unlocked_ioctl = video_ioctl2,
  673. .open = rvin_open,
  674. .release = rvin_release,
  675. .poll = vb2_fop_poll,
  676. .mmap = vb2_fop_mmap,
  677. .read = vb2_fop_read,
  678. };
  679. void rvin_v4l2_unregister(struct rvin_dev *vin)
  680. {
  681. if (!video_is_registered(&vin->vdev))
  682. return;
  683. v4l2_info(&vin->v4l2_dev, "Removing %s\n",
  684. video_device_node_name(&vin->vdev));
  685. /* Checks internally if vdev have been init or not */
  686. video_unregister_device(&vin->vdev);
  687. }
  688. static void rvin_notify(struct v4l2_subdev *sd,
  689. unsigned int notification, void *arg)
  690. {
  691. struct rvin_dev *vin =
  692. container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev);
  693. switch (notification) {
  694. case V4L2_DEVICE_NOTIFY_EVENT:
  695. v4l2_event_queue(&vin->vdev, arg);
  696. break;
  697. default:
  698. break;
  699. }
  700. }
  701. int rvin_v4l2_register(struct rvin_dev *vin)
  702. {
  703. struct video_device *vdev = &vin->vdev;
  704. int ret;
  705. vin->v4l2_dev.notify = rvin_notify;
  706. /* video node */
  707. vdev->v4l2_dev = &vin->v4l2_dev;
  708. vdev->queue = &vin->queue;
  709. snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id);
  710. vdev->release = video_device_release_empty;
  711. vdev->lock = &vin->lock;
  712. vdev->fops = &rvin_fops;
  713. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  714. V4L2_CAP_READWRITE;
  715. /* Set a default format */
  716. vin->format.pixelformat = RVIN_DEFAULT_FORMAT;
  717. vin->format.width = RVIN_DEFAULT_WIDTH;
  718. vin->format.height = RVIN_DEFAULT_HEIGHT;
  719. vin->format.field = RVIN_DEFAULT_FIELD;
  720. vin->format.colorspace = RVIN_DEFAULT_COLORSPACE;
  721. if (vin->info->use_mc) {
  722. vdev->ioctl_ops = &rvin_mc_ioctl_ops;
  723. } else {
  724. vdev->ioctl_ops = &rvin_ioctl_ops;
  725. rvin_reset_format(vin);
  726. }
  727. rvin_format_align(vin, &vin->format);
  728. ret = video_register_device(&vin->vdev, VFL_TYPE_GRABBER, -1);
  729. if (ret) {
  730. vin_err(vin, "Failed to register video device\n");
  731. return ret;
  732. }
  733. video_set_drvdata(&vin->vdev, vin);
  734. v4l2_info(&vin->v4l2_dev, "Device registered as %s\n",
  735. video_device_node_name(&vin->vdev));
  736. return ret;
  737. }