rcar-v4l2.c 24 KB

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