rcar-v4l2.c 24 KB

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