2 * NVIDIA Tegra Video Input Device
4 * Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Bryan Wu <pengw@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/atomic.h>
14 #include <linux/bitmap.h>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/nvhost.h>
18 #include <linux/kthread.h>
19 #include <linux/freezer.h>
20 #include <linux/lcm.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-dev.h>
30 #include <media/v4l2-fh.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/videobuf2-core.h>
33 #include <media/videobuf2-dma-contig.h>
34 #include <media/camera_common.h>
35 #include <media/tegra_camera_platform.h>
38 #include <mach/io_dpd.h>
40 #include "camera/mc_common.h"
42 #include "nvhost_acm.h"
48 extern int _vb2_fop_release(struct file *file, struct mutex *lock);
49 static void tegra_channel_queued_buf_done(struct tegra_channel *chan,
50 enum vb2_buffer_state state);
51 static void tegra_channel_stop_kthreads(struct tegra_channel *chan);
52 static int tegra_channel_set_stream(struct tegra_channel *chan, bool on);
53 static int tegra_channel_mipi_cal(struct tegra_channel *chan, char is_bypass);
55 static void gang_buffer_offsets(struct tegra_channel *chan)
61 for (i = 0; i < chan->total_ports; i++) {
62 switch (chan->gang_mode) {
65 case CAMERA_NO_GANG_MODE:
67 offset = chan->gang_bytesperline;
72 offset = chan->gang_sizeimage;
77 offset = ((offset + TEGRA_SURFACE_ALIGNMENT - 1) &
78 ~(TEGRA_SURFACE_ALIGNMENT - 1));
79 ports = reverse ? (chan->total_ports - 1 - i) : i;
80 chan->buffer_offset[i] = ports * offset;
84 static u32 gang_mode_width(enum camera_gang_mode gang_mode,
87 if ((gang_mode == CAMERA_GANG_L_R) ||
88 (gang_mode == CAMERA_GANG_R_L))
94 static u32 gang_mode_height(enum camera_gang_mode gang_mode,
97 if ((gang_mode == CAMERA_GANG_T_B) ||
98 (gang_mode == CAMERA_GANG_B_T))
104 static void update_gang_mode_params(struct tegra_channel *chan)
106 chan->gang_width = gang_mode_width(chan->gang_mode,
108 chan->gang_height = gang_mode_height(chan->gang_mode,
109 chan->format.height);
110 chan->gang_bytesperline = chan->gang_width *
112 chan->gang_sizeimage = chan->gang_bytesperline *
114 gang_buffer_offsets(chan);
117 static void update_gang_mode(struct tegra_channel *chan)
119 int width = chan->format.width;
120 int height = chan->format.height;
123 * At present only 720p, 1080p and 4k resolutions
124 * are supported and only 4K requires gang mode
125 * Update this code with CID for future extensions
126 * Also, validate width and height of images based
127 * on gang mode and surface stride alignment
129 if ((width > 1920) && (height > 1080)) {
130 chan->gang_mode = chan->gang_mode_default;
131 chan->valid_ports = chan->total_ports;
133 chan->gang_mode = CAMERA_NO_GANG_MODE;
134 chan->valid_ports = 1;
137 update_gang_mode_params(chan);
140 static void tegra_channel_fmt_align(struct tegra_channel *chan,
141 u32 *width, u32 *height, u32 *bytesperline)
143 unsigned int min_width;
144 unsigned int max_width;
145 unsigned int min_bpl;
146 unsigned int max_bpl;
147 unsigned int temp_width;
149 unsigned int temp_bpl;
150 unsigned int bpp = chan->fmtinfo->bpp;
152 /* Init, if un-init */
154 *width = chan->format.width;
156 *height = chan->format.height;
158 *bytesperline = *width * chan->fmtinfo->bpp;
160 /* The transfer alignment requirements are expressed in bytes. Compute
161 * the minimum and maximum values, clamp the requested width and convert
164 align = lcm(chan->width_align, bpp);
165 min_width = roundup(TEGRA_MIN_WIDTH, align);
166 max_width = rounddown(TEGRA_MAX_WIDTH, align);
167 temp_width = roundup(*width * bpp, align);
169 *width = clamp(temp_width, min_width, max_width) / bpp;
170 *height = clamp(*height, TEGRA_MIN_HEIGHT, TEGRA_MAX_HEIGHT);
172 /* Clamp the requested bytes per line value. If the maximum bytes per
173 * line value is zero, the module doesn't support user configurable line
174 * sizes. Override the requested value with the minimum in that case.
176 min_bpl = *width * bpp;
177 max_bpl = rounddown(TEGRA_MAX_WIDTH, chan->stride_align);
178 temp_bpl = roundup(*bytesperline, chan->stride_align);
180 *bytesperline = clamp(temp_bpl, min_bpl, max_bpl);
183 static void tegra_channel_fmts_bitmap_init(struct tegra_channel *chan)
185 int ret, pixel_format_index = 0, init_code = 0;
186 struct v4l2_subdev *subdev = chan->subdev_on_csi;
187 struct v4l2_mbus_framefmt mbus_fmt;
188 struct v4l2_subdev_mbus_code_enum code = {
189 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
192 bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM);
195 * Initialize all the formats available from
196 * the sub-device and extract the corresponding
197 * index from the pre-defined video formats and initialize
198 * the channel default format with the active code
199 * Index zero as the only sub-device is sensor
202 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
205 /* no more formats */
208 pixel_format_index = tegra_core_get_idx_by_code(code.code);
209 if (pixel_format_index >= 0) {
210 bitmap_set(chan->fmts_bitmap, pixel_format_index, 1);
212 init_code = code.code;
219 pixel_format_index = tegra_core_get_idx_by_code(TEGRA_VF_DEF);
220 if (pixel_format_index >= 0) {
221 bitmap_set(chan->fmts_bitmap, pixel_format_index, 1);
222 init_code = TEGRA_VF_DEF;
226 /* Get the format based on active code of the sub-device */
227 ret = v4l2_subdev_call(subdev, video, g_mbus_fmt, &mbus_fmt);
231 chan->fmtinfo = tegra_core_get_format_by_code(mbus_fmt.code);
232 chan->format.pixelformat = chan->fmtinfo->fourcc;
233 chan->format.colorspace = mbus_fmt.colorspace;
234 chan->format.field = mbus_fmt.field;
235 chan->format.width = mbus_fmt.width;
236 chan->format.height = mbus_fmt.height;
237 chan->format.maxframerate = mbus_fmt.maxframerate;
238 chan->format.bytesperline = chan->format.width *
240 tegra_channel_fmt_align(chan,
241 &chan->format.width, &chan->format.height,
242 &chan->format.bytesperline);
243 chan->format.sizeimage = chan->format.bytesperline *
245 if (chan->total_ports > 1)
246 update_gang_mode(chan);
250 * -----------------------------------------------------------------------------
251 * Tegra channel frame setup and capture operations
252 * -----------------------------------------------------------------------------
255 static int tegra_channel_capture_setup(struct tegra_channel *chan)
257 return chan->fops->soc_channel_capture_setup(chan);
260 static int tegra_channel_enable_stream(struct tegra_channel *chan)
265 * enable pad power and perform calibration before arming
266 * single shot for first frame after the HW setup is complete
268 /* enable pad power */
269 tegra_csi_pad_control(chan->vi->csi, chan->port, ENABLE);
270 /* start streaming */
271 if (chan->vi->pg_mode) {
272 for (i = 0; i < chan->valid_ports; i++)
273 tegra_csi_tpg_start_streaming(chan->vi->csi,
275 atomic_set(&chan->is_streaming, ENABLE);
277 ret = tegra_channel_set_stream(chan, true);
281 /* perform calibration as sensor started streaming */
282 tegra_mipi_bias_pad_enable();
283 if (!chan->vi->pg_mode) {
284 mutex_lock(&chan->vi->mipical_lock);
285 tegra_channel_mipi_cal(chan, 0);
286 mutex_unlock(&chan->vi->mipical_lock);
292 static int tegra_channel_error_status(struct tegra_channel *chan)
294 return chan->fops->soc_channel_error_status(chan);
297 static void tegra_channel_init_ring_buffer(struct tegra_channel *chan)
299 chan->released_bufs = 0;
300 chan->num_buffers = 0;
301 chan->save_index = 0;
302 chan->free_index = 0;
303 chan->bfirst_fstart = false;
306 static void free_ring_buffers(struct tegra_channel *chan, int frames)
308 struct vb2_buffer *vb;
311 vb = chan->buffers[chan->free_index];
313 /* release one frame */
314 vb->v4l2_buf.sequence = chan->sequence++;
315 vb->v4l2_buf.field = V4L2_FIELD_NONE;
316 vb2_set_plane_payload(vb, 0, chan->format.sizeimage);
319 * WAR to force buffer state if capture state is not good
320 * WAR - After sync point timeout or error frame capture
321 * the second buffer is intermittently frame of zeros
322 * with no error status or padding.
324 if (chan->capture_state != CAPTURE_GOOD ||
325 chan->released_bufs < 2)
326 chan->buffer_state[chan->free_index] =
329 vb2_buffer_done(vb, chan->buffer_state[chan->free_index++]);
331 if (chan->free_index >= QUEUED_BUFFERS)
332 chan->free_index = 0;
334 chan->released_bufs++;
339 static void add_buffer_to_ring(struct tegra_channel *chan,
340 struct vb2_buffer *vb)
342 /* save the buffer to the ring first */
343 /* Mark buffer state as error before start */
344 chan->buffer_state[chan->save_index] = VB2_BUF_STATE_ERROR;
345 chan->buffers[chan->save_index++] = vb;
346 if (chan->save_index >= QUEUED_BUFFERS)
347 chan->save_index = 0;
351 static void update_state_to_buffer(struct tegra_channel *chan, int state)
353 int save_index = (chan->save_index - PREVIOUS_BUFFER_DEC_INDEX);
355 /* save index decrements by 2 as 3 bufs are added in ring buffer */
357 save_index += QUEUED_BUFFERS;
358 /* update state for the previous buffer */
359 chan->buffer_state[save_index] = state;
361 /* for timeout/error case update the current buffer state as well */
362 if (chan->capture_state != CAPTURE_GOOD)
363 chan->buffer_state[chan->save_index] = state;
366 static void tegra_channel_ring_buffer(struct tegra_channel *chan,
367 struct vb2_buffer *vb,
368 struct timespec *ts, int state)
371 if (!chan->bfirst_fstart)
372 chan->bfirst_fstart = true;
374 update_state_to_buffer(chan, state);
376 /* update time stamp of the buffer */
377 vb->v4l2_buf.timestamp.tv_sec = ts->tv_sec;
378 vb->v4l2_buf.timestamp.tv_usec = ts->tv_nsec / NSEC_PER_USEC;
380 /* Capture state is not GOOD, release all buffers and re-init state */
381 if (chan->capture_state != CAPTURE_GOOD) {
382 free_ring_buffers(chan, chan->num_buffers);
383 tegra_channel_init_ring_buffer(chan);
387 /* release buffer N at N+2 frame start event */
388 if (chan->num_buffers >= (QUEUED_BUFFERS - 1))
389 free_ring_buffers(chan, 1);
392 void tegra_channel_ec_close(struct tegra_mc_vi *vi)
395 struct tegra_channel *chan = &vi->chans[0];
397 /* clear all channles sync point fifo context */
398 for (i = 0; i < vi->num_channels; i++) {
399 chan = &vi->chans[i];
400 memset(&chan->syncpoint_fifo[0], 0, TEGRA_CSI_BLOCKS);
404 static void tegra_channel_ec_init(struct tegra_channel *chan)
407 * error recover initialization sequence
408 * set timeout as 200 ms, use default if fps not available
409 * Time limit allow CSI to capture good frames and drop error frames
410 * Timeout units is jiffies, 1 jiffy = 10ms
411 * TODO: Get frame rate from sub-device and adopt timeout
415 chan->fops->soc_channel_ec_init(chan);
418 static void tegra_channel_ec_recover(struct tegra_channel *chan)
420 int index, valid_ports = chan->valid_ports;
422 /* Disable pad power to start recovery */
423 tegra_csi_pad_control(chan->vi->csi, chan->port, DISABLE);
425 chan->fops->soc_channel_ec_recover(chan);
427 /* Re-init VI and CSI */
428 tegra_channel_capture_setup(chan);
429 for (index = 0; index < valid_ports; index++) {
430 tegra_csi_stop_streaming(chan->vi->csi,
432 tegra_csi_start_streaming(chan->vi->csi,
434 nvhost_syncpt_set_min_eq_max_ext(chan->vi->ndev,
435 chan->syncpt[index]);
439 static int tegra_channel_capture_frame(struct tegra_channel *chan,
440 struct tegra_channel_buffer *buf)
442 struct vb2_buffer *vb = &buf->buf;
444 u32 thresh[TEGRA_CSI_BLOCKS] = { 0 };
446 int state = VB2_BUF_STATE_DONE;
448 /* Init registers related to each frames */
449 chan->fops->soc_channel_capture_frame_init(chan, buf, thresh);
451 if (!chan->bfirst_fstart) {
452 err = tegra_channel_enable_stream(chan);
454 state = VB2_BUF_STATE_ERROR;
455 chan->capture_state = CAPTURE_ERROR;
456 tegra_channel_ring_buffer(chan, vb, &ts, state);
459 /* Enable input stream once the VI registers are configured */
460 chan->fops->soc_channel_capture_frame_enable(chan);
463 /* Arm capture and wait for notifier or syncpoint */
464 err = chan->fops->soc_channel_capture_frame(chan, &ts, thresh);
466 state = VB2_BUF_STATE_ERROR;
467 /* perform error recovery for timeout */
468 tegra_channel_ec_recover(chan);
469 } else if (!chan->vi->pg_mode) {
470 /* Marking error frames and resume capture */
471 /* TODO: TPG has frame height short error always set */
472 err = tegra_channel_error_status(chan);
474 state = VB2_BUF_STATE_ERROR;
475 chan->capture_state = CAPTURE_ERROR;
476 /* do we have to run recover here ?? */
477 /* tegra_channel_ec_recover(chan); */
481 tegra_channel_ring_buffer(chan, vb, &ts, state);
487 struct tegra_channel_buffer *dequeue_buffer(struct tegra_channel *chan)
489 struct tegra_channel_buffer *buf = NULL;
491 spin_lock(&chan->start_lock);
492 if (list_empty(&chan->capture))
495 buf = list_entry(chan->capture.next,
496 struct tegra_channel_buffer, queue);
497 list_del_init(&buf->queue);
499 /* add dequeued buffer to the ring buffer */
500 add_buffer_to_ring(chan, &buf->buf);
502 spin_unlock(&chan->start_lock);
506 static void tegra_channel_capture_done(struct tegra_channel *chan)
510 struct tegra_channel_buffer *buf;
511 int state = VB2_BUF_STATE_DONE;
513 /* dequeue buffer and return if no buffer exists */
514 buf = dequeue_buffer(chan);
518 err = chan->fops->soc_channel_capture_done(chan, buf, &ts);
520 state = VB2_BUF_STATE_ERROR;
521 /* perform error recovery for timeout */
522 tegra_channel_ec_recover(chan);
523 chan->capture_state = CAPTURE_TIMEOUT;
526 /* Mark capture state to IDLE as capture is finished */
527 chan->capture_state = CAPTURE_IDLE;
529 tegra_channel_ring_buffer(chan, &buf->buf, &ts, state);
532 static int tegra_channel_kthread_capture_start(void *data)
534 struct tegra_channel *chan = data;
535 struct tegra_channel_buffer *buf;
544 wait_event_interruptible(chan->start_wait,
545 !list_empty(&chan->capture) ||
546 kthread_should_stop());
548 if (kthread_should_stop()) {
549 complete(&chan->capture_comp);
553 /* source is not streaming if error is non-zero */
554 /* wait till kthread stop and dont DeQ buffers */
558 buf = dequeue_buffer(chan);
562 err = tegra_channel_capture_frame(chan, buf);
568 static void tegra_channel_stop_kthreads(struct tegra_channel *chan)
570 mutex_lock(&chan->stop_kthread_lock);
571 /* Stop the kthread for capture */
572 if (chan->kthread_capture_start) {
573 kthread_stop(chan->kthread_capture_start);
574 wait_for_completion(&chan->capture_comp);
575 chan->kthread_capture_start = NULL;
577 mutex_unlock(&chan->stop_kthread_lock);
581 * -----------------------------------------------------------------------------
582 * videobuf2 queue operations
583 * -----------------------------------------------------------------------------
586 tegra_channel_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
587 unsigned int *nbuffers, unsigned int *nplanes,
588 unsigned int sizes[], void *alloc_ctxs[])
590 struct tegra_channel *chan = vb2_get_drv_priv(vq);
592 /* Make sure the image size is large enough. */
593 if (fmt && fmt->fmt.pix.sizeimage < chan->format.sizeimage)
598 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : chan->format.sizeimage;
599 alloc_ctxs[0] = chan->alloc_ctx;
601 /* Make sure minimum number of buffers are passed */
602 if (*nbuffers < (QUEUED_BUFFERS - 1))
603 *nbuffers = QUEUED_BUFFERS - 1;
608 static int tegra_channel_buffer_prepare(struct vb2_buffer *vb)
610 struct tegra_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
611 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vb);
614 vb2_set_plane_payload(vb, 0, chan->format.sizeimage);
615 buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0);
620 static void tegra_channel_buffer_queue(struct vb2_buffer *vb)
622 struct tegra_channel *chan = vb2_get_drv_priv(vb->vb2_queue);
623 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vb);
625 /* for bypass mode - do nothing */
629 /* Put buffer into the capture queue */
630 spin_lock(&chan->start_lock);
631 list_add_tail(&buf->queue, &chan->capture);
632 spin_unlock(&chan->start_lock);
634 /* Wait up kthread for capture */
635 wake_up_interruptible(&chan->start_wait);
638 /* Return all queued buffers back to videobuf2 */
639 static void tegra_channel_queued_buf_done(struct tegra_channel *chan,
640 enum vb2_buffer_state state)
642 struct tegra_channel_buffer *buf, *nbuf;
643 spinlock_t *lock = &chan->start_lock;
644 struct list_head *q = &chan->capture;
647 list_for_each_entry_safe(buf, nbuf, q, queue) {
648 vb2_buffer_done(&buf->buf, state);
649 list_del(&buf->queue);
654 #if defined(CONFIG_ARCH_TEGRA_21x_SOC)
655 static int tegra_channel_mipi_cal(struct tegra_channel *chan, char is_bypass)
657 unsigned int lanes, cur_lanes, val;
658 unsigned int csi_phya, csi_phyb, csi_phya_mask, csi_phyb_mask;
659 struct tegra_mc_vi *vi = chan->vi;
663 csi_phya = 0x1 << CSI_A_PHY_CIL_ENABLE_SHIFT;
664 csi_phya_mask = 0x3 << CSI_A_PHY_CIL_ENABLE_SHIFT;
665 csi_phyb = 0x1 << CSI_B_PHY_CIL_ENABLE_SHIFT;
666 csi_phyb_mask = 0x3 << CSI_B_PHY_CIL_ENABLE_SHIFT;
667 if (chan->numlanes == 2 && chan->total_ports == 1) {
668 switch (chan->port[0]) {
671 val = (host1x_readl(vi->ndev, CSI_PHY_CIL_COMMAND_0) &
672 (~csi_phya_mask)) | csi_phya;
674 host1x_writel(vi->ndev, CSI_PHY_CIL_COMMAND_0,
679 val = (host1x_readl(vi->ndev, CSI_PHY_CIL_COMMAND_0) &
680 (~csi_phyb_mask)) | csi_phyb;
682 host1x_writel(vi->ndev, CSI_PHY_CIL_COMMAND_0,
687 val = (host1x_readl(vi->ndev, CSI1_PHY_CIL_COMMAND_0) &
688 (~csi_phya_mask)) | csi_phya;
690 host1x_writel(vi->ndev, CSI1_PHY_CIL_COMMAND_0,
695 val = (host1x_readl(vi->ndev, CSI1_PHY_CIL_COMMAND_0) &
696 (~csi_phyb_mask)) | csi_phyb;
698 host1x_writel(vi->ndev, CSI1_PHY_CIL_COMMAND_0,
703 val = (host1x_readl(vi->ndev, CSI2_PHY_CIL_COMMAND_0) &
704 (~csi_phya_mask)) | csi_phya;
706 host1x_writel(vi->ndev, CSI2_PHY_CIL_COMMAND_0,
711 val = (host1x_readl(vi->ndev, CSI2_PHY_CIL_COMMAND_0) &
712 (~csi_phyb_mask)) | csi_phyb;
714 host1x_writel(vi->ndev, CSI2_PHY_CIL_COMMAND_0,
718 dev_err(vi->dev, "csi_port number: %d", chan->port[0]);
721 } else if (chan->numlanes == 4 && chan->total_ports == 1) {
722 switch (chan->port[0]) {
727 host1x_writel(vi->ndev, CSI_PHY_CIL_COMMAND_0,
728 csi_phya | csi_phyb);
734 host1x_writel(vi->ndev, CSI1_PHY_CIL_COMMAND_0,
735 csi_phya | csi_phyb);
741 host1x_writel(vi->ndev, CSI2_PHY_CIL_COMMAND_0,
742 csi_phya | csi_phyb);
745 dev_err(vi->dev, "csi_port number: %d", chan->port[0]);
748 } else if (chan->numlanes == 8) {
750 for (j = 0; j < chan->valid_ports; ++j) {
751 switch (chan->port[j]) {
754 cur_lanes = CSIA|CSIB;
756 host1x_writel(vi->ndev,
757 CSI_PHY_CIL_COMMAND_0,
758 csi_phya | csi_phyb);
762 cur_lanes = CSIC|CSID;
764 host1x_writel(vi->ndev,
765 CSI1_PHY_CIL_COMMAND_0,
766 csi_phya | csi_phyb);
770 cur_lanes = CSIE|CSIF;
772 host1x_writel(vi->ndev,
773 CSI2_PHY_CIL_COMMAND_0,
774 csi_phya | csi_phyb);
777 dev_err(vi->dev, "csi_port number: %d",
785 dev_err(vi->dev, "Selected no CSI lane, cannot do calibration");
788 return tegra_mipi_calibration(lanes);
792 static int tegra_channel_mipi_cal(struct tegra_channel *chan, char is_bypass)
799 * -----------------------------------------------------------------------------
800 * subdevice set/unset operations
801 * -----------------------------------------------------------------------------
803 static int tegra_channel_set_stream(struct tegra_channel *chan, bool on)
807 if (atomic_read(&chan->is_streaming) == on)
810 ret = v4l2_device_call_until_err(chan->video.v4l2_dev,
811 chan->grp_id, video, s_stream, on);
815 atomic_set(&chan->is_streaming, on);
820 static int tegra_channel_set_power(struct tegra_channel *chan, bool on)
822 return v4l2_device_call_until_err(chan->video.v4l2_dev,
823 chan->grp_id, core, s_power, on);
826 static int update_clk(struct tegra_mc_vi *vi)
829 unsigned long max_clk = 0;
831 for (i = 0; i < vi->num_channels; i++) {
832 max_clk = max_clk > vi->chans[i].requested_hz ?
833 max_clk : vi->chans[i].requested_hz;
835 return clk_set_rate(vi->clk, max_clk);
838 static void tegra_channel_update_clknbw(struct tegra_channel *chan, u8 on)
840 /* width * height * fps * KBytes write to memory
841 * WAR: Using fix fps until we have a way to set it
843 chan->requested_kbyteps = (on > 0 ? 1 : -1) * ((chan->format.width
844 * chan->format.height
845 * FRAMERATE * BPP_MEM) / 1000);
846 chan->requested_hz = on > 0 ? chan->format.width * chan->format.height
848 mutex_lock(&chan->vi->bw_update_lock);
849 chan->vi->aggregated_kbyteps += chan->requested_kbyteps;
850 vi_v4l2_update_isobw(chan->vi->aggregated_kbyteps, 0);
851 vi_v4l2_set_la(tegra_vi_get(), 0, 0);
852 update_clk(chan->vi);
853 mutex_unlock(&chan->vi->bw_update_lock);
856 static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count)
858 struct tegra_channel *chan = vb2_get_drv_priv(vq);
859 struct media_pipeline *pipe = chan->video.entity.pipe;
862 tegra_channel_ec_init(chan);
864 if (!chan->vi->pg_mode) {
865 /* Start the pipeline. */
866 ret = media_entity_pipeline_start(&chan->video.entity, pipe);
868 goto error_pipeline_start;
872 ret = tegra_channel_set_stream(chan, true);
874 goto error_set_stream;
875 nvhost_module_enable_clk(chan->vi->dev);
876 tegra_mipi_bias_pad_enable();
877 mutex_lock(&chan->vi->mipical_lock);
878 tegra_channel_mipi_cal(chan, 1);
879 mutex_unlock(&chan->vi->mipical_lock);
880 nvhost_module_disable_clk(chan->vi->dev);
884 chan->capture_state = CAPTURE_IDLE;
885 for (i = 0; i < chan->valid_ports; i++) {
886 tegra_csi_start_streaming(chan->vi->csi, chan->port[i]);
887 /* ensure sync point state is clean */
888 nvhost_syncpt_set_min_eq_max_ext(chan->vi->ndev,
892 /* Note: Program VI registers after TPG, sensors and CSI streaming */
893 ret = tegra_channel_capture_setup(chan);
895 goto error_capture_setup;
898 tegra_channel_init_ring_buffer(chan);
900 /* Update clock and bandwidth based on the format */
901 tegra_channel_update_clknbw(chan, 1);
903 /* Start kthread to capture data to buffer */
904 chan->kthread_capture_start = kthread_run(
905 tegra_channel_kthread_capture_start,
906 chan, chan->video.name);
907 if (IS_ERR(chan->kthread_capture_start)) {
908 dev_err(&chan->video.dev,
909 "failed to run kthread for capture start\n");
910 ret = PTR_ERR(chan->kthread_capture_start);
911 goto error_capture_setup;
917 if (!chan->vi->pg_mode)
918 tegra_channel_set_stream(chan, false);
920 if (!chan->vi->pg_mode)
921 media_entity_pipeline_stop(&chan->video.entity);
922 error_pipeline_start:
923 vq->start_streaming_called = 0;
924 tegra_channel_queued_buf_done(chan, VB2_BUF_STATE_QUEUED);
929 static int tegra_channel_stop_streaming(struct vb2_queue *vq)
931 struct tegra_channel *chan = vb2_get_drv_priv(vq);
932 bool is_streaming = atomic_read(&chan->is_streaming);
935 tegra_channel_stop_kthreads(chan);
936 /* wait for last frame memory write ack */
937 if (is_streaming && chan->capture_state == CAPTURE_GOOD)
938 tegra_channel_capture_done(chan);
939 /* free all the ring buffers */
940 free_ring_buffers(chan, chan->num_buffers);
941 /* dequeue buffers back to app which are in capture queue */
942 tegra_channel_queued_buf_done(chan, VB2_BUF_STATE_ERROR);
944 chan->fops->soc_channel_stop_streaming(chan);
946 tegra_csi_pad_control(chan->vi->csi, chan->port, DISABLE);
949 if (!chan->vi->pg_mode) {
950 tegra_channel_set_stream(chan, false);
951 media_entity_pipeline_stop(&chan->video.entity);
955 tegra_channel_update_clknbw(chan, 0);
957 tegra_mipi_bias_pad_disable();
962 static const struct vb2_ops tegra_channel_queue_qops = {
963 .queue_setup = tegra_channel_queue_setup,
964 .buf_prepare = tegra_channel_buffer_prepare,
965 .buf_queue = tegra_channel_buffer_queue,
966 .wait_prepare = vb2_ops_wait_prepare,
967 .wait_finish = vb2_ops_wait_finish,
968 .start_streaming = tegra_channel_start_streaming,
969 .stop_streaming = tegra_channel_stop_streaming,
972 /* -----------------------------------------------------------------------------
977 tegra_channel_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
979 struct v4l2_fh *vfh = file->private_data;
980 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
982 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
983 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
984 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
986 strlcpy(cap->driver, "tegra-video", sizeof(cap->driver));
987 strlcpy(cap->card, chan->video.name, sizeof(cap->card));
988 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s:%u",
989 dev_name(chan->vi->dev), chan->port[0]);
995 tegra_channel_enum_framesizes(struct file *file, void *fh,
996 struct v4l2_frmsizeenum *sizes)
998 struct v4l2_fh *vfh = file->private_data;
999 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1001 return v4l2_device_call_until_err(chan->video.v4l2_dev,
1002 chan->grp_id, video, enum_framesizes, sizes);
1006 tegra_channel_enum_frameintervals(struct file *file, void *fh,
1007 struct v4l2_frmivalenum *intervals)
1009 struct v4l2_fh *vfh = file->private_data;
1010 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1012 return v4l2_device_call_until_err(chan->video.v4l2_dev,
1013 chan->grp_id, video, enum_frameintervals,
1019 tegra_channel_enum_format(struct file *file, void *fh, struct v4l2_fmtdesc *f)
1021 struct v4l2_fh *vfh = file->private_data;
1022 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1023 unsigned int index = 0, i;
1024 unsigned long *fmts_bitmap = NULL;
1026 if (chan->vi->pg_mode)
1027 fmts_bitmap = chan->vi->tpg_fmts_bitmap;
1029 fmts_bitmap = chan->fmts_bitmap;
1031 if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM))
1034 for (i = 0; i < f->index + 1; i++, index++)
1035 index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index);
1038 f->pixelformat = tegra_core_get_fourcc_by_idx(index);
1039 tegra_core_get_description_by_idx(index, f->description);
1045 tegra_channel_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1047 struct v4l2_fh *vfh = file->private_data;
1048 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1049 struct v4l2_subdev *sd = chan->subdev_on_csi;
1051 if (!v4l2_subdev_has_op(sd, pad, get_edid))
1054 return v4l2_subdev_call(sd, pad, get_edid, edid);
1058 tegra_channel_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1060 struct v4l2_fh *vfh = file->private_data;
1061 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1062 struct v4l2_subdev *sd = chan->subdev_on_csi;
1064 if (!v4l2_subdev_has_op(sd, pad, set_edid))
1067 return v4l2_subdev_call(sd, pad, set_edid, edid);
1071 tegra_channel_s_dv_timings(struct file *file, void *fh,
1072 struct v4l2_dv_timings *timings)
1074 struct v4l2_fh *vfh = file->private_data;
1075 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1076 struct v4l2_bt_timings *bt = &timings->bt;
1079 if (!v4l2_subdev_has_op(chan->subdev_on_csi, video, s_dv_timings))
1082 ret = v4l2_device_call_until_err(chan->video.v4l2_dev,
1083 chan->grp_id, video, s_dv_timings, timings);
1086 chan->format.width = bt->width;
1087 chan->format.height = bt->height;
1088 chan->format.bytesperline = bt->width *
1090 tegra_channel_fmt_align(chan,
1091 &chan->format.width,
1092 &chan->format.height,
1093 &chan->format.bytesperline);
1094 chan->format.sizeimage = chan->format.bytesperline *
1095 chan->format.height;
1098 if (chan->total_ports > 1)
1099 update_gang_mode(chan);
1105 tegra_channel_g_dv_timings(struct file *file, void *fh,
1106 struct v4l2_dv_timings *timings)
1108 struct v4l2_fh *vfh = file->private_data;
1109 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1111 if (!v4l2_subdev_has_op(chan->subdev_on_csi, video, g_dv_timings))
1114 return v4l2_device_call_until_err(chan->video.v4l2_dev,
1115 chan->grp_id, video, g_dv_timings, timings);
1119 tegra_channel_query_dv_timings(struct file *file, void *fh,
1120 struct v4l2_dv_timings *timings)
1122 struct v4l2_fh *vfh = file->private_data;
1123 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1125 if (!v4l2_subdev_has_op(chan->subdev_on_csi, video, query_dv_timings))
1128 return v4l2_device_call_until_err(chan->video.v4l2_dev,
1129 chan->grp_id, video, query_dv_timings, timings);
1133 tegra_channel_enum_dv_timings(struct file *file, void *fh,
1134 struct v4l2_enum_dv_timings *timings)
1136 struct v4l2_fh *vfh = file->private_data;
1137 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1138 struct v4l2_subdev *sd = chan->subdev_on_csi;
1140 if (!v4l2_subdev_has_op(sd, pad, enum_dv_timings))
1143 return v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
1147 tegra_channel_dv_timings_cap(struct file *file, void *fh,
1148 struct v4l2_dv_timings_cap *cap)
1150 struct v4l2_fh *vfh = file->private_data;
1151 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1152 struct v4l2_subdev *sd = chan->subdev_on_csi;
1154 if (!v4l2_subdev_has_op(sd, pad, dv_timings_cap))
1157 return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
1160 static int tegra_channel_s_ctrl(struct v4l2_ctrl *ctrl)
1162 struct tegra_channel *chan = container_of(ctrl->handler,
1163 struct tegra_channel, ctrl_handler);
1166 case V4L2_CID_VI_BYPASS_MODE:
1167 if (switch_ctrl_qmenu[ctrl->val] == SWITCH_ON)
1168 chan->bypass = true;
1169 else if (chan->vi->vi->bypass) {
1170 dev_dbg(&chan->video.dev,
1171 "can't disable bypass mode\n");
1172 dev_dbg(&chan->video.dev,
1173 "because the VI/CSI is in bypass mode\n");
1174 chan->bypass = true;
1176 chan->bypass = false;
1179 dev_err(&chan->video.dev, "%s:Not valid ctrl\n", __func__);
1186 static const struct v4l2_ctrl_ops channel_ctrl_ops = {
1187 .s_ctrl = tegra_channel_s_ctrl,
1191 * By default channel will be in VI mode
1192 * User space can set it to 0 for working in bypass mode
1194 static const struct v4l2_ctrl_config bypass_mode_ctrl = {
1195 .ops = &channel_ctrl_ops,
1196 .id = V4L2_CID_VI_BYPASS_MODE,
1197 .name = "Bypass Mode",
1198 .type = V4L2_CTRL_TYPE_INTEGER_MENU,
1201 .max = ARRAY_SIZE(switch_ctrl_qmenu) - 1,
1202 .menu_skip_mask = 0,
1203 .qmenu_int = switch_ctrl_qmenu,
1206 static int tegra_channel_setup_controls(struct tegra_channel *chan)
1209 struct v4l2_subdev *sd = NULL;
1211 /* Initialize the subdev and controls here at first open */
1212 sd = chan->subdev[num_sd];
1213 while ((sd = chan->subdev[num_sd++]) &&
1214 (num_sd <= chan->num_subdevs)) {
1215 /* Add control handler for the subdevice */
1216 v4l2_ctrl_add_handler(&chan->ctrl_handler,
1217 sd->ctrl_handler, NULL);
1218 if (chan->ctrl_handler.error)
1219 dev_err(chan->vi->dev,
1220 "Failed to add sub-device controls\n");
1223 /* Add the bypass mode ctrl */
1224 v4l2_ctrl_new_custom(&chan->ctrl_handler, &bypass_mode_ctrl, NULL);
1225 if (chan->ctrl_handler.error) {
1226 dev_err(chan->vi->dev,
1227 "Failed to add bypass control\n");
1228 return chan->ctrl_handler.error;
1231 /* setup the controls */
1232 return v4l2_ctrl_handler_setup(&chan->ctrl_handler);
1235 int tegra_channel_init_subdevices(struct tegra_channel *chan)
1237 struct media_entity *entity;
1238 struct media_pad *pad;
1241 int grp_id = chan->port[0] + 1;
1242 struct v4l2_subdev *sd;
1244 /* set_stream of CSI */
1245 entity = &chan->video.entity;
1246 pad = media_entity_remote_source(&chan->pad);
1250 /* the remote source entity */
1251 entity = pad->entity;
1252 sd = media_entity_to_v4l2_subdev(entity);
1253 sd->grp_id = grp_id;
1254 chan->grp_id = grp_id;
1255 chan->subdev[num_sd++] = sd;
1256 /* Each CSI channel has only one pad, thus there
1257 * is only one subdev directly attached to this
1258 * CSI channel. Set this subdev to subdev_on_csi */
1259 chan->subdev_on_csi = sd;
1261 /* Append subdev name to this video dev name*/
1262 snprintf(chan->video.name, sizeof(chan->video.name), "%s, %s",
1263 chan->video.name, sd->name);
1265 index = pad->index - 1;
1266 while (index >= 0) {
1267 pad = &entity->pads[index];
1268 if (!(pad->flags & MEDIA_PAD_FL_SINK))
1271 pad = media_entity_remote_source(pad);
1273 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1276 if (num_sd >= MAX_SUBDEVICES)
1279 entity = pad->entity;
1280 sd = media_entity_to_v4l2_subdev(entity);
1281 sd->grp_id = grp_id;
1282 chan->subdev[num_sd++] = sd;
1284 index = pad->index - 1;
1286 chan->num_subdevs = num_sd;
1288 /* initialize the available formats */
1289 if (chan->num_subdevs)
1290 tegra_channel_fmts_bitmap_init(chan);
1292 return tegra_channel_setup_controls(chan);
1296 __tegra_channel_get_format(struct tegra_channel *chan,
1297 struct v4l2_pix_format *pix)
1299 struct tegra_video_format const *vfmt;
1300 struct v4l2_subdev_format fmt;
1302 struct v4l2_subdev *sd = chan->subdev_on_csi;
1304 memset(&fmt, 0x0, sizeof(fmt));
1306 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
1307 if (ret == -ENOIOCTLCMD)
1310 v4l2_fill_pix_format(pix, &fmt.format);
1311 vfmt = tegra_core_get_format_by_code(fmt.format.code);
1313 pix->pixelformat = vfmt->fourcc;
1314 tegra_channel_fmt_align(chan,
1315 &pix->width, &pix->height, &pix->bytesperline);
1316 pix->sizeimage = pix->height * pix->bytesperline;
1323 tegra_channel_get_format(struct file *file, void *fh,
1324 struct v4l2_format *format)
1326 struct v4l2_fh *vfh = file->private_data;
1327 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1328 struct v4l2_pix_format *pix = &format->fmt.pix;
1330 return __tegra_channel_get_format(chan, pix);
1334 __tegra_channel_try_format(struct tegra_channel *chan,
1335 struct v4l2_pix_format *pix)
1337 const struct tegra_video_format *vfmt;
1338 struct v4l2_subdev_format fmt;
1339 struct v4l2_subdev *sd = chan->subdev_on_csi;
1342 /* Use the channel format if pixformat is not supported */
1343 vfmt = tegra_core_get_format_by_fourcc(pix->pixelformat);
1345 pix->pixelformat = chan->format.pixelformat;
1346 vfmt = tegra_core_get_format_by_fourcc(pix->pixelformat);
1349 tegra_channel_fmt_align(chan,
1350 &pix->width, &pix->height, &pix->bytesperline);
1351 pix->sizeimage = pix->bytesperline * pix->height;
1353 fmt.which = V4L2_SUBDEV_FORMAT_TRY;
1355 v4l2_fill_mbus_format(&fmt.format, pix, vfmt->code);
1357 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &fmt);
1358 if (ret == -ENOIOCTLCMD)
1361 v4l2_fill_pix_format(pix, &fmt.format);
1363 pix->sizeimage = pix->height * pix->bytesperline;
1369 tegra_channel_try_format(struct file *file, void *fh,
1370 struct v4l2_format *format)
1372 struct v4l2_fh *vfh = file->private_data;
1373 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1375 return __tegra_channel_try_format(chan, &format->fmt.pix);
1379 __tegra_channel_set_format(struct tegra_channel *chan,
1380 struct v4l2_pix_format *pix)
1382 const struct tegra_video_format *vfmt;
1383 struct v4l2_subdev_format fmt;
1384 struct v4l2_subdev *sd = chan->subdev_on_csi;
1387 vfmt = tegra_core_get_format_by_fourcc(pix->pixelformat);
1389 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1391 v4l2_fill_mbus_format(&fmt.format, pix, vfmt->code);
1393 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &fmt);
1394 if (ret == -ENOIOCTLCMD)
1397 v4l2_fill_pix_format(pix, &fmt.format);
1400 chan->format = *pix;
1401 chan->fmtinfo = vfmt;
1402 if (chan->total_ports > 1)
1403 update_gang_mode(chan);
1410 tegra_channel_set_format(struct file *file, void *fh,
1411 struct v4l2_format *format)
1413 struct v4l2_fh *vfh = file->private_data;
1414 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1417 /* get the supported format by try_fmt */
1418 ret = __tegra_channel_try_format(chan, &format->fmt.pix);
1422 if (vb2_is_busy(&chan->queue))
1425 return __tegra_channel_set_format(chan, &format->fmt.pix);
1428 static int tegra_channel_s_crop(struct file *file, void *fh,
1429 const struct v4l2_crop *crop)
1431 struct v4l2_fh *vfh = file->private_data;
1432 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1433 struct v4l2_subdev *subdev = chan->subdev_on_csi;
1436 if (!v4l2_subdev_has_op(subdev, video, s_crop))
1439 ret = v4l2_device_call_until_err(chan->video.v4l2_dev,
1440 chan->grp_id, video, s_crop, crop);
1445 static int tegra_channel_g_crop(struct file *file, void *fh,
1446 struct v4l2_crop *crop)
1448 struct v4l2_fh *vfh = file->private_data;
1449 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1450 struct v4l2_subdev *subdev = chan->subdev_on_csi;
1453 if (!v4l2_subdev_has_op(subdev, video, g_crop))
1456 ret = v4l2_device_call_until_err(chan->video.v4l2_dev,
1457 chan->grp_id, video, g_crop, crop);
1462 static int tegra_channel_subscribe_event(struct v4l2_fh *fh,
1463 const struct v4l2_event_subscription *sub)
1465 switch (sub->type) {
1466 case V4L2_EVENT_SOURCE_CHANGE:
1467 return v4l2_event_subscribe(fh, sub, 4, NULL);
1469 return v4l2_ctrl_subscribe_event(fh, sub);
1473 tegra_channel_enum_input(struct file *file, void *fh, struct v4l2_input *inp)
1475 struct v4l2_fh *vfh = file->private_data;
1476 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1477 struct v4l2_subdev *sd_on_csi = chan->subdev_on_csi;
1483 ret = v4l2_device_call_until_err(chan->video.v4l2_dev,
1484 chan->grp_id, video, g_input_status, &inp->status);
1486 if (ret != -ENODEV) {
1487 if (v4l2_subdev_has_op(sd_on_csi, video, s_dv_timings))
1488 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1490 inp->type = V4L2_INPUT_TYPE_CAMERA;
1491 if (inp->capabilities == V4L2_IN_CAP_DV_TIMINGS)
1493 sizeof(inp->name), "HDMI %u",
1497 sizeof(inp->name), "Camera %u",
1506 static int tegra_channel_g_input(struct file *file, void *priv, unsigned int *i)
1512 static int tegra_channel_s_input(struct file *file, void *priv, unsigned int i)
1519 static int tegra_channel_g_parm(struct file *file, void *fh,
1520 struct v4l2_streamparm *parm)
1522 struct v4l2_fh *vfh = file->private_data;
1523 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1524 struct v4l2_subdev *subdev = chan->subdev_on_csi;
1527 ret = v4l2_subdev_call(subdev, video, g_parm, parm);
1528 if (ret == -ENOIOCTLCMD)
1534 static int tegra_channel_s_parm(struct file *file, void *fh,
1535 struct v4l2_streamparm *parm)
1537 struct v4l2_fh *vfh = file->private_data;
1538 struct tegra_channel *chan = to_tegra_channel(vfh->vdev);
1539 struct v4l2_subdev *subdev = chan->subdev_on_csi;
1542 ret = v4l2_subdev_call(subdev, video, s_parm, parm);
1543 if (ret == -ENOIOCTLCMD)
1549 static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = {
1550 .vidioc_querycap = tegra_channel_querycap,
1551 .vidioc_enum_framesizes = tegra_channel_enum_framesizes,
1552 .vidioc_enum_frameintervals = tegra_channel_enum_frameintervals,
1553 .vidioc_enum_fmt_vid_cap = tegra_channel_enum_format,
1554 .vidioc_g_fmt_vid_cap = tegra_channel_get_format,
1555 .vidioc_s_fmt_vid_cap = tegra_channel_set_format,
1556 .vidioc_try_fmt_vid_cap = tegra_channel_try_format,
1557 .vidioc_s_crop = tegra_channel_s_crop,
1558 .vidioc_g_crop = tegra_channel_g_crop,
1559 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1560 .vidioc_querybuf = vb2_ioctl_querybuf,
1561 .vidioc_qbuf = vb2_ioctl_qbuf,
1562 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1563 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1564 .vidioc_expbuf = vb2_ioctl_expbuf,
1565 .vidioc_streamon = vb2_ioctl_streamon,
1566 .vidioc_streamoff = vb2_ioctl_streamoff,
1567 .vidioc_g_edid = tegra_channel_g_edid,
1568 .vidioc_s_edid = tegra_channel_s_edid,
1569 .vidioc_s_dv_timings = tegra_channel_s_dv_timings,
1570 .vidioc_g_dv_timings = tegra_channel_g_dv_timings,
1571 .vidioc_query_dv_timings = tegra_channel_query_dv_timings,
1572 .vidioc_enum_dv_timings = tegra_channel_enum_dv_timings,
1573 .vidioc_dv_timings_cap = tegra_channel_dv_timings_cap,
1574 .vidioc_subscribe_event = tegra_channel_subscribe_event,
1575 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1576 .vidioc_enum_input = tegra_channel_enum_input,
1577 .vidioc_g_input = tegra_channel_g_input,
1578 .vidioc_s_input = tegra_channel_s_input,
1579 .vidioc_g_parm = tegra_channel_g_parm,
1580 .vidioc_s_parm = tegra_channel_s_parm,
1583 static int tegra_channel_open(struct file *fp)
1586 struct video_device *vdev = video_devdata(fp);
1587 struct tegra_channel *chan = video_get_drvdata(vdev);
1588 struct vi *tegra_vi;
1589 struct tegra_mc_vi *vi;
1590 struct tegra_csi_device *csi;
1592 mutex_lock(&chan->video_lock);
1593 ret = v4l2_fh_open(fp);
1594 if (ret || !v4l2_fh_is_singular_file(fp))
1597 if (chan->subdev_on_csi == NULL) {
1606 /* TPG mode and a real sensor is open, return busy */
1607 if (vi->pg_mode && tegra_vi->sensor_opened)
1610 /* None TPG mode and a TPG channel is opened, return busy */
1611 if (!vi->pg_mode && tegra_vi->tpg_opened)
1614 /* The first open then turn on power */
1615 tegra_vi_power_on(vi);
1616 if (atomic_add_return(1, &vi->csi_power_on_refcnt) == 1) {
1617 tegra_csi_power_on(csi);
1619 tegra_vi->tpg_opened = true;
1621 tegra_vi->sensor_opened = true;
1625 (atomic_add_return(1, &chan->power_on_refcnt) == 1)) {
1626 /* power on sensors connected in channel */
1627 tegra_csi_channel_power_on(csi, chan->port);
1628 ret = tegra_channel_set_power(chan, 1);
1633 chan->fh = (struct v4l2_fh *)fp->private_data;
1636 mutex_unlock(&chan->video_lock);
1640 static int tegra_channel_close(struct file *fp)
1643 struct video_device *vdev = video_devdata(fp);
1644 struct tegra_channel *chan = video_get_drvdata(vdev);
1645 struct tegra_mc_vi *vi = chan->vi;
1646 struct vi *tegra_vi = vi->vi;
1647 struct tegra_csi_device *csi = vi->csi;
1650 mutex_lock(&chan->video_lock);
1651 is_singular = v4l2_fh_is_singular_file(fp);
1652 ret = _vb2_fop_release(fp, NULL);
1655 mutex_unlock(&chan->video_lock);
1660 atomic_dec_and_test(&chan->power_on_refcnt)) {
1661 /* power off sensors connected in channel */
1662 tegra_csi_channel_power_off(csi, chan->port);
1663 ret = tegra_channel_set_power(chan, 0);
1665 dev_err(vi->dev, "Failed to power off subdevices\n");
1668 /* The last release then turn off power */
1669 if (atomic_dec_and_test(&vi->csi_power_on_refcnt)) {
1670 tegra_csi_power_off(csi);
1672 tegra_vi->tpg_opened = false;
1674 tegra_vi->sensor_opened = false;
1676 tegra_vi_power_off(vi);
1678 mutex_unlock(&chan->video_lock);
1682 /* -----------------------------------------------------------------------------
1683 * V4L2 file operations
1685 static const struct v4l2_file_operations tegra_channel_fops = {
1686 .owner = THIS_MODULE,
1687 .unlocked_ioctl = video_ioctl2,
1688 .open = tegra_channel_open,
1689 .release = tegra_channel_close,
1690 .read = vb2_fop_read,
1691 .poll = vb2_fop_poll,
1692 .mmap = vb2_fop_mmap,
1695 static void vi_channel_syncpt_init(struct tegra_channel *chan)
1699 for (i = 0; i < chan->total_ports; i++)
1701 nvhost_get_syncpt_client_managed(chan->vi->ndev, "vi");
1704 static void vi_channel_syncpt_free(struct tegra_channel *chan)
1708 for (i = 0; i < chan->total_ports; i++)
1709 nvhost_syncpt_put_ref_ext(chan->vi->ndev, chan->syncpt[i]);
1712 static void tegra_channel_csi_init(struct tegra_mc_vi *vi, unsigned int index)
1716 struct tegra_channel *chan = &vi->chans[index];
1718 chan->gang_mode = CAMERA_NO_GANG_MODE;
1719 chan->total_ports = 0;
1720 memset(&chan->port[0], INVALID_CSI_PORT, TEGRA_CSI_BLOCKS);
1721 memset(&chan->syncpoint_fifo[0], 0, TEGRA_CSI_BLOCKS);
1723 chan->port[0] = index;
1726 tegra_vi_get_port_info(chan, vi->dev->of_node, index);
1728 for (idx = 0; csi_port_is_valid(chan->port[idx]); idx++) {
1729 chan->total_ports++;
1730 numlanes = chan->numlanes - (idx * 4);
1731 numlanes = numlanes > 4 ? 4 : numlanes;
1732 /* maximum of 4 lanes are present per CSI block */
1733 chan->csibase[idx] = vi->iomem +
1734 TEGRA_VI_CSI_BASE(chan->port[idx]);
1735 set_csi_portinfo(vi->csi, chan->port[idx], numlanes);
1737 /* based on gang mode valid ports will be updated - set default to 1 */
1738 chan->valid_ports = chan->total_ports ? 1 : 0;
1741 static int tegra_channel_init(struct tegra_mc_vi *vi, unsigned int index)
1744 struct tegra_channel *chan = &vi->chans[index];
1746 /* VI/CSI is in bypass mode, then channel has to be in bypass */
1748 chan->bypass = true;
1751 chan->fops = vi->vi->data->channel_fops;
1752 tegra_channel_csi_init(vi, index);
1754 chan->width_align = TEGRA_WIDTH_ALIGNMENT;
1755 chan->stride_align = TEGRA_STRIDE_ALIGNMENT;
1756 chan->num_subdevs = 0;
1757 mutex_init(&chan->video_lock);
1758 INIT_LIST_HEAD(&chan->capture);
1759 init_waitqueue_head(&chan->start_wait);
1760 spin_lock_init(&chan->start_lock);
1761 mutex_init(&chan->stop_kthread_lock);
1762 init_completion(&chan->capture_comp);
1763 atomic_set(&chan->is_streaming, DISABLE);
1765 /* Init video format */
1766 chan->fmtinfo = tegra_core_get_format_by_code(TEGRA_VF_DEF);
1767 chan->format.pixelformat = chan->fmtinfo->fourcc;
1768 chan->format.colorspace = V4L2_COLORSPACE_SRGB;
1769 chan->format.field = V4L2_FIELD_NONE;
1770 chan->format.width = TEGRA_DEF_WIDTH;
1771 chan->format.height = TEGRA_DEF_HEIGHT;
1772 chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp;
1773 chan->format.sizeimage = chan->format.bytesperline *
1774 chan->format.height;
1775 chan->buffer_offset[0] = 0;
1777 /* Initialize the media entity... */
1778 chan->pad.flags = MEDIA_PAD_FL_SINK;
1780 ret = media_entity_init(&chan->video.entity, 1, &chan->pad, 0);
1784 /* init control handler */
1785 ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS);
1786 if (chan->ctrl_handler.error) {
1787 dev_err(&chan->video.dev, "failed to init control handler\n");
1788 goto video_register_error;
1791 /* init video node... */
1792 chan->video.fops = &tegra_channel_fops;
1793 chan->video.v4l2_dev = &vi->v4l2_dev;
1794 chan->video.queue = &chan->queue;
1795 snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u",
1796 dev_name(vi->dev), vi->pg_mode ? "tpg" : "output",
1798 chan->video.vfl_type = VFL_TYPE_GRABBER;
1799 chan->video.vfl_dir = VFL_DIR_RX;
1800 chan->video.release = video_device_release_empty;
1801 chan->video.ioctl_ops = &tegra_channel_ioctl_ops;
1802 chan->video.ctrl_handler = &chan->ctrl_handler;
1803 chan->video.lock = &chan->video_lock;
1805 set_bit(V4L2_FL_USE_FH_PRIO, &chan->video.flags);
1807 video_set_drvdata(&chan->video, chan);
1809 vi_channel_syncpt_init(chan);
1811 /* get the buffers queue... */
1812 chan->alloc_ctx = vb2_dma_contig_init_ctx(chan->vi->dev);
1813 if (IS_ERR(chan->alloc_ctx)) {
1814 dev_err(chan->vi->dev, "failed to init vb2 buffer\n");
1816 goto vb2_init_error;
1819 chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1820 chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ | VB2_USERPTR;
1821 chan->queue.lock = &chan->video_lock;
1822 chan->queue.drv_priv = chan;
1823 chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer);
1824 chan->queue.ops = &tegra_channel_queue_qops;
1825 chan->queue.mem_ops = &vb2_dma_contig_memops;
1826 chan->queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
1827 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
1828 ret = vb2_queue_init(&chan->queue);
1830 dev_err(chan->vi->dev, "failed to initialize VB2 queue\n");
1831 goto vb2_queue_error;
1834 ret = video_register_device(&chan->video, VFL_TYPE_GRABBER, -1);
1836 dev_err(&chan->video.dev, "failed to register video device\n");
1837 goto video_register_error;
1842 video_register_error:
1843 vb2_queue_release(&chan->queue);
1845 vb2_dma_contig_cleanup_ctx(chan->alloc_ctx);
1847 media_entity_cleanup(&chan->video.entity);
1851 static int tegra_channel_cleanup(struct tegra_channel *chan)
1853 video_unregister_device(&chan->video);
1855 v4l2_ctrl_handler_free(&chan->ctrl_handler);
1856 vb2_queue_release(&chan->queue);
1857 vb2_dma_contig_cleanup_ctx(chan->alloc_ctx);
1859 vi_channel_syncpt_free(chan);
1860 media_entity_cleanup(&chan->video.entity);
1865 int tegra_vi_channels_init(struct tegra_mc_vi *vi)
1870 for (i = 0; i < vi->num_channels; i++) {
1871 ret = tegra_channel_init(vi, i);
1873 dev_err(vi->dev, "channel %d init failed\n", i);
1879 EXPORT_SYMBOL(tegra_vi_channels_init);
1881 int tegra_vi_channels_cleanup(struct tegra_mc_vi *vi)
1886 for (i = 0; i < vi->num_channels; i++) {
1887 ret = tegra_channel_cleanup(&vi->chans[i]);
1889 dev_err(vi->dev, "channel %d cleanup failed\n", i);
1895 EXPORT_SYMBOL(tegra_vi_channels_cleanup);
1897 int tegra_clean_unlinked_channels(struct tegra_mc_vi *vi)
1902 for (i = 0; i < vi->num_channels; i++) {
1903 struct tegra_channel *chan = &vi->chans[i];
1905 if (chan->num_subdevs)
1908 ret = tegra_channel_cleanup(chan);
1910 dev_err(vi->dev, "channel %d cleanup failed\n", i);
1917 EXPORT_SYMBOL(tegra_clean_unlinked_channels);