rcar-v4l2.c 22 KB

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