aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLad, Prabhakar <prabhakar.lad@ti.com>2012-06-28 08:28:05 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2012-07-30 18:05:29 -0400
commit60aa38d87d7d50a89e43b77559307a95a42bf07e (patch)
tree46ef62777d3c35df03e4bbeb19ac03d7d417741f
parent764af39aa415c9418f035d2a4bd606d7a7814a68 (diff)
[media] davinci: vpif capture: migrate driver to videobuf2
This patch migrates VPIF capture driver to videobuf2 framework. Signed-off-by: Lad, Prabhakar <prabhakar.lad@ti.com> Signed-off-by: Manjunath Hadli <manjunath.hadli@ti.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
-rw-r--r--drivers/media/video/davinci/Kconfig2
-rw-r--r--drivers/media/video/davinci/vpif_capture.c596
-rw-r--r--drivers/media/video/davinci/vpif_capture.h15
3 files changed, 281 insertions, 332 deletions
diff --git a/drivers/media/video/davinci/Kconfig b/drivers/media/video/davinci/Kconfig
index 9337b5605c9..c45739dd461 100644
--- a/drivers/media/video/davinci/Kconfig
+++ b/drivers/media/video/davinci/Kconfig
@@ -14,7 +14,7 @@ config DISPLAY_DAVINCI_DM646X_EVM
14config CAPTURE_DAVINCI_DM646X_EVM 14config CAPTURE_DAVINCI_DM646X_EVM
15 tristate "DM646x EVM Video Capture" 15 tristate "DM646x EVM Video Capture"
16 depends on VIDEO_DEV && MACH_DAVINCI_DM6467_EVM 16 depends on VIDEO_DEV && MACH_DAVINCI_DM6467_EVM
17 select VIDEOBUF_DMA_CONTIG 17 select VIDEOBUF2_DMA_CONTIG
18 select VIDEO_DAVINCI_VPIF 18 select VIDEO_DAVINCI_VPIF
19 help 19 help
20 Support for DM6467 based capture device. 20 Support for DM6467 based capture device.
diff --git a/drivers/media/video/davinci/vpif_capture.c b/drivers/media/video/davinci/vpif_capture.c
index d126fb6d74b..1d4427aa08a 100644
--- a/drivers/media/video/davinci/vpif_capture.c
+++ b/drivers/media/video/davinci/vpif_capture.c
@@ -80,108 +80,45 @@ static struct vpif_config_params config_params = {
80/* global variables */ 80/* global variables */
81static struct vpif_device vpif_obj = { {NULL} }; 81static struct vpif_device vpif_obj = { {NULL} };
82static struct device *vpif_dev; 82static struct device *vpif_dev;
83 83static void vpif_calculate_offsets(struct channel_obj *ch);
84/** 84static void vpif_config_addr(struct channel_obj *ch, int muxmode);
85 * vpif_uservirt_to_phys : translate user/virtual address to phy address
86 * @virtp: user/virtual address
87 *
88 * This inline function is used to convert user space virtual address to
89 * physical address.
90 */
91static inline u32 vpif_uservirt_to_phys(u32 virtp)
92{
93 unsigned long physp = 0;
94 struct mm_struct *mm = current->mm;
95 struct vm_area_struct *vma;
96
97 vma = find_vma(mm, virtp);
98
99 /* For kernel direct-mapped memory, take the easy way */
100 if (virtp >= PAGE_OFFSET)
101 physp = virt_to_phys((void *)virtp);
102 else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
103 /**
104 * this will catch, kernel-allocated, mmaped-to-usermode
105 * addresses
106 */
107 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
108 else {
109 /* otherwise, use get_user_pages() for general userland pages */
110 int res, nr_pages = 1;
111 struct page *pages;
112
113 down_read(&current->mm->mmap_sem);
114
115 res = get_user_pages(current, current->mm,
116 virtp, nr_pages, 1, 0, &pages, NULL);
117 up_read(&current->mm->mmap_sem);
118
119 if (res == nr_pages)
120 physp = __pa(page_address(&pages[0]) +
121 (virtp & ~PAGE_MASK));
122 else {
123 vpif_err("get_user_pages failed\n");
124 return 0;
125 }
126 }
127 return physp;
128}
129 85
130/** 86/**
131 * buffer_prepare : callback function for buffer prepare 87 * buffer_prepare : callback function for buffer prepare
132 * @q : buffer queue ptr 88 * @vb: ptr to vb2_buffer
133 * @vb: ptr to video buffer
134 * @field: field info
135 * 89 *
136 * This is the callback function for buffer prepare when videobuf_qbuf() 90 * This is the callback function for buffer prepare when vb2_qbuf()
137 * function is called. The buffer is prepared and user space virtual address 91 * function is called. The buffer is prepared and user space virtual address
138 * or user address is converted into physical address 92 * or user address is converted into physical address
139 */ 93 */
140static int vpif_buffer_prepare(struct videobuf_queue *q, 94static int vpif_buffer_prepare(struct vb2_buffer *vb)
141 struct videobuf_buffer *vb,
142 enum v4l2_field field)
143{ 95{
144 /* Get the file handle object and channel object */ 96 /* Get the file handle object and channel object */
145 struct vpif_fh *fh = q->priv_data; 97 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
98 struct vb2_queue *q = vb->vb2_queue;
146 struct channel_obj *ch = fh->channel; 99 struct channel_obj *ch = fh->channel;
147 struct common_obj *common; 100 struct common_obj *common;
148 unsigned long addr; 101 unsigned long addr;
149 102
150
151 vpif_dbg(2, debug, "vpif_buffer_prepare\n"); 103 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
152 104
153 common = &ch->common[VPIF_VIDEO_INDEX]; 105 common = &ch->common[VPIF_VIDEO_INDEX];
154 106
155 /* If buffer is not initialized, initialize it */ 107 if (vb->state != VB2_BUF_STATE_ACTIVE &&
156 if (VIDEOBUF_NEEDS_INIT == vb->state) { 108 vb->state != VB2_BUF_STATE_PREPARED) {
157 vb->width = common->width; 109 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
158 vb->height = common->height; 110 if (vb2_plane_vaddr(vb, 0) &&
159 vb->size = vb->width * vb->height; 111 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
160 vb->field = field;
161 }
162 vb->state = VIDEOBUF_PREPARED;
163 /**
164 * if user pointer memory mechanism is used, get the physical
165 * address of the buffer
166 */
167 if (V4L2_MEMORY_USERPTR == common->memory) {
168 if (0 == vb->baddr) {
169 vpif_dbg(1, debug, "buffer address is 0\n");
170 return -EINVAL;
171
172 }
173 vb->boff = vpif_uservirt_to_phys(vb->baddr);
174 if (!IS_ALIGNED(vb->boff, 8))
175 goto exit; 112 goto exit;
176 } 113 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
177 114
178 addr = vb->boff; 115 if (q->streaming) {
179 if (q->streaming) { 116 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
180 if (!IS_ALIGNED((addr + common->ytop_off), 8) || 117 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
181 !IS_ALIGNED((addr + common->ybtm_off), 8) || 118 !IS_ALIGNED((addr + common->ctop_off), 8) ||
182 !IS_ALIGNED((addr + common->ctop_off), 8) || 119 !IS_ALIGNED((addr + common->cbtm_off), 8))
183 !IS_ALIGNED((addr + common->cbtm_off), 8)) 120 goto exit;
184 goto exit; 121 }
185 } 122 }
186 return 0; 123 return 0;
187exit: 124exit:
@@ -190,66 +127,79 @@ exit:
190} 127}
191 128
192/** 129/**
193 * vpif_buffer_setup : Callback function for buffer setup. 130 * vpif_buffer_queue_setup : Callback function for buffer setup.
194 * @q: buffer queue ptr 131 * @vq: vb2_queue ptr
195 * @count: number of buffers 132 * @fmt: v4l2 format
196 * @size: size of the buffer 133 * @nbuffers: ptr to number of buffers requested by application
134 * @nplanes:: contains number of distinct video planes needed to hold a frame
135 * @sizes[]: contains the size (in bytes) of each plane.
136 * @alloc_ctxs: ptr to allocation context
197 * 137 *
198 * This callback function is called when reqbuf() is called to adjust 138 * This callback function is called when reqbuf() is called to adjust
199 * the buffer count and buffer size 139 * the buffer count and buffer size
200 */ 140 */
201static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count, 141static int vpif_buffer_queue_setup(struct vb2_queue *vq,
202 unsigned int *size) 142 const struct v4l2_format *fmt,
143 unsigned int *nbuffers, unsigned int *nplanes,
144 unsigned int sizes[], void *alloc_ctxs[])
203{ 145{
204 /* Get the file handle object and channel object */ 146 /* Get the file handle object and channel object */
205 struct vpif_fh *fh = q->priv_data; 147 struct vpif_fh *fh = vb2_get_drv_priv(vq);
206 struct channel_obj *ch = fh->channel; 148 struct channel_obj *ch = fh->channel;
207 struct common_obj *common; 149 struct common_obj *common;
150 unsigned long size;
208 151
209 common = &ch->common[VPIF_VIDEO_INDEX]; 152 common = &ch->common[VPIF_VIDEO_INDEX];
210 153
211 vpif_dbg(2, debug, "vpif_buffer_setup\n"); 154 vpif_dbg(2, debug, "vpif_buffer_setup\n");
212 155
213 /* If memory type is not mmap, return */ 156 /* If memory type is not mmap, return */
214 if (V4L2_MEMORY_MMAP != common->memory) 157 if (V4L2_MEMORY_MMAP == common->memory) {
215 return 0; 158 /* Calculate the size of the buffer */
216 159 size = config_params.channel_bufsize[ch->channel_id];
217 /* Calculate the size of the buffer */ 160 /*
218 *size = config_params.channel_bufsize[ch->channel_id]; 161 * Checking if the buffer size exceeds the available buffer
219 162 * ycmux_mode = 0 means 1 channel mode HD and
220 /* 163 * ycmux_mode = 1 means 2 channels mode SD
221 * Checking if the buffer size exceeds the available buffer 164 */
222 * ycmux_mode = 0 means 1 channel mode HD and 165 if (ch->vpifparams.std_info.ycmux_mode == 0) {
223 * ycmux_mode = 1 means 2 channels mode SD 166 if (config_params.video_limit[ch->channel_id])
224 */ 167 while (size * *nbuffers >
225 if (ch->vpifparams.std_info.ycmux_mode == 0) { 168 (config_params.video_limit[0]
226 if (config_params.video_limit[ch->channel_id])
227 while (*size * *count > (config_params.video_limit[0]
228 + config_params.video_limit[1])) 169 + config_params.video_limit[1]))
229 (*count)--; 170 (*nbuffers)--;
230 } else { 171 } else {
231 if (config_params.video_limit[ch->channel_id]) 172 if (config_params.video_limit[ch->channel_id])
232 while (*size * *count > 173 while (size * *nbuffers >
233 config_params.video_limit[ch->channel_id]) 174 config_params.video_limit[ch->channel_id])
234 (*count)--; 175 (*nbuffers)--;
176 }
177
178 } else {
179 size = common->fmt.fmt.pix.sizeimage;
235 } 180 }
236 181
237 if (*count < config_params.min_numbuffers) 182 if (*nbuffers < config_params.min_numbuffers)
238 *count = config_params.min_numbuffers; 183 *nbuffers = config_params.min_numbuffers;
184
185 *nplanes = 1;
186 sizes[0] = size;
187 alloc_ctxs[0] = common->alloc_ctx;
188
239 return 0; 189 return 0;
240} 190}
241 191
242/** 192/**
243 * vpif_buffer_queue : Callback function to add buffer to DMA queue 193 * vpif_buffer_queue : Callback function to add buffer to DMA queue
244 * @q: ptr to videobuf_queue 194 * @vb: ptr to vb2_buffer
245 * @vb: ptr to videobuf_buffer
246 */ 195 */
247static void vpif_buffer_queue(struct videobuf_queue *q, 196static void vpif_buffer_queue(struct vb2_buffer *vb)
248 struct videobuf_buffer *vb)
249{ 197{
250 /* Get the file handle object and channel object */ 198 /* Get the file handle object and channel object */
251 struct vpif_fh *fh = q->priv_data; 199 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
252 struct channel_obj *ch = fh->channel; 200 struct channel_obj *ch = fh->channel;
201 struct vpif_cap_buffer *buf = container_of(vb,
202 struct vpif_cap_buffer, vb);
253 struct common_obj *common; 203 struct common_obj *common;
254 204
255 common = &ch->common[VPIF_VIDEO_INDEX]; 205 common = &ch->common[VPIF_VIDEO_INDEX];
@@ -257,43 +207,189 @@ static void vpif_buffer_queue(struct videobuf_queue *q,
257 vpif_dbg(2, debug, "vpif_buffer_queue\n"); 207 vpif_dbg(2, debug, "vpif_buffer_queue\n");
258 208
259 /* add the buffer to the DMA queue */ 209 /* add the buffer to the DMA queue */
260 list_add_tail(&vb->queue, &common->dma_queue); 210 list_add_tail(&buf->list, &common->dma_queue);
261 /* Change state of the buffer */
262 vb->state = VIDEOBUF_QUEUED;
263} 211}
264 212
265/** 213/**
266 * vpif_buffer_release : Callback function to free buffer 214 * vpif_buf_cleanup : Callback function to free buffer
267 * @q: buffer queue ptr 215 * @vb: ptr to vb2_buffer
268 * @vb: ptr to video buffer
269 * 216 *
270 * This function is called from the videobuf layer to free memory 217 * This function is called from the videobuf2 layer to free memory
271 * allocated to the buffers 218 * allocated to the buffers
272 */ 219 */
273static void vpif_buffer_release(struct videobuf_queue *q, 220static void vpif_buf_cleanup(struct vb2_buffer *vb)
274 struct videobuf_buffer *vb)
275{ 221{
276 /* Get the file handle object and channel object */ 222 /* Get the file handle object and channel object */
277 struct vpif_fh *fh = q->priv_data; 223 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
224 struct vpif_cap_buffer *buf = container_of(vb,
225 struct vpif_cap_buffer, vb);
278 struct channel_obj *ch = fh->channel; 226 struct channel_obj *ch = fh->channel;
279 struct common_obj *common; 227 struct common_obj *common;
228 unsigned long flags;
280 229
281 common = &ch->common[VPIF_VIDEO_INDEX]; 230 common = &ch->common[VPIF_VIDEO_INDEX];
282 231
283 videobuf_dma_contig_free(q, vb); 232 spin_lock_irqsave(&common->irqlock, flags);
284 vb->state = VIDEOBUF_NEEDS_INIT; 233 if (vb->state == VB2_BUF_STATE_ACTIVE)
234 list_del_init(&buf->list);
235 spin_unlock_irqrestore(&common->irqlock, flags);
236
285} 237}
286 238
287static struct videobuf_queue_ops video_qops = { 239static void vpif_wait_prepare(struct vb2_queue *vq)
288 .buf_setup = vpif_buffer_setup, 240{
289 .buf_prepare = vpif_buffer_prepare, 241 struct vpif_fh *fh = vb2_get_drv_priv(vq);
290 .buf_queue = vpif_buffer_queue, 242 struct channel_obj *ch = fh->channel;
291 .buf_release = vpif_buffer_release, 243 struct common_obj *common;
292}; 244
245 common = &ch->common[VPIF_VIDEO_INDEX];
246 mutex_unlock(&common->lock);
247}
248
249static void vpif_wait_finish(struct vb2_queue *vq)
250{
251 struct vpif_fh *fh = vb2_get_drv_priv(vq);
252 struct channel_obj *ch = fh->channel;
253 struct common_obj *common;
254
255 common = &ch->common[VPIF_VIDEO_INDEX];
256 mutex_lock(&common->lock);
257}
258
259static int vpif_buffer_init(struct vb2_buffer *vb)
260{
261 struct vpif_cap_buffer *buf = container_of(vb,
262 struct vpif_cap_buffer, vb);
263
264 INIT_LIST_HEAD(&buf->list);
265
266 return 0;
267}
293 268
294static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = 269static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
295 { {1, 1} }; 270 { {1, 1} };
296 271
272static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
273{
274 struct vpif_capture_config *vpif_config_data =
275 vpif_dev->platform_data;
276 struct vpif_fh *fh = vb2_get_drv_priv(vq);
277 struct channel_obj *ch = fh->channel;
278 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
279 struct vpif_params *vpif = &ch->vpifparams;
280 unsigned long addr = 0;
281 int ret;
282
283 /* If buffer queue is empty, return error */
284 if (list_empty(&common->dma_queue)) {
285 vpif_dbg(1, debug, "buffer queue is empty\n");
286 return -EIO;
287 }
288
289 /* Get the next frame from the buffer queue */
290 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
291 struct vpif_cap_buffer, list);
292 /* Remove buffer from the buffer queue */
293 list_del(&common->cur_frm->list);
294 /* Mark state of the current frame to active */
295 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
296 /* Initialize field_id and started member */
297 ch->field_id = 0;
298 common->started = 1;
299 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
300
301 /* Calculate the offset for Y and C data in the buffer */
302 vpif_calculate_offsets(ch);
303
304 if ((vpif->std_info.frm_fmt &&
305 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
306 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
307 (!vpif->std_info.frm_fmt &&
308 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
309 vpif_dbg(1, debug, "conflict in field format and std format\n");
310 return -EINVAL;
311 }
312
313 /* configure 1 or 2 channel mode */
314 ret = vpif_config_data->setup_input_channel_mode
315 (vpif->std_info.ycmux_mode);
316
317 if (ret < 0) {
318 vpif_dbg(1, debug, "can't set vpif channel mode\n");
319 return ret;
320 }
321
322 /* Call vpif_set_params function to set the parameters and addresses */
323 ret = vpif_set_video_params(vpif, ch->channel_id);
324
325 if (ret < 0) {
326 vpif_dbg(1, debug, "can't set video params\n");
327 return ret;
328 }
329
330 common->started = ret;
331 vpif_config_addr(ch, ret);
332
333 common->set_addr(addr + common->ytop_off,
334 addr + common->ybtm_off,
335 addr + common->ctop_off,
336 addr + common->cbtm_off);
337
338 /**
339 * Set interrupt for both the fields in VPIF Register enable channel in
340 * VPIF register
341 */
342 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
343 channel0_intr_assert();
344 channel0_intr_enable(1);
345 enable_channel0(1);
346 }
347 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
348 (common->started == 2)) {
349 channel1_intr_assert();
350 channel1_intr_enable(1);
351 enable_channel1(1);
352 }
353 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
354
355 return 0;
356}
357
358/* abort streaming and wait for last buffer */
359static int vpif_stop_streaming(struct vb2_queue *vq)
360{
361 struct vpif_fh *fh = vb2_get_drv_priv(vq);
362 struct channel_obj *ch = fh->channel;
363 struct common_obj *common;
364
365 if (!vb2_is_streaming(vq))
366 return 0;
367
368 common = &ch->common[VPIF_VIDEO_INDEX];
369
370 /* release all active buffers */
371 while (!list_empty(&common->dma_queue)) {
372 common->next_frm = list_entry(common->dma_queue.next,
373 struct vpif_cap_buffer, list);
374 list_del(&common->next_frm->list);
375 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
376 }
377
378 return 0;
379}
380
381static struct vb2_ops video_qops = {
382 .queue_setup = vpif_buffer_queue_setup,
383 .wait_prepare = vpif_wait_prepare,
384 .wait_finish = vpif_wait_finish,
385 .buf_init = vpif_buffer_init,
386 .buf_prepare = vpif_buffer_prepare,
387 .start_streaming = vpif_start_streaming,
388 .stop_streaming = vpif_stop_streaming,
389 .buf_cleanup = vpif_buf_cleanup,
390 .buf_queue = vpif_buffer_queue,
391};
392
297/** 393/**
298 * vpif_process_buffer_complete: process a completed buffer 394 * vpif_process_buffer_complete: process a completed buffer
299 * @common: ptr to common channel object 395 * @common: ptr to common channel object
@@ -304,9 +400,9 @@ static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
304 */ 400 */
305static void vpif_process_buffer_complete(struct common_obj *common) 401static void vpif_process_buffer_complete(struct common_obj *common)
306{ 402{
307 do_gettimeofday(&common->cur_frm->ts); 403 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
308 common->cur_frm->state = VIDEOBUF_DONE; 404 vb2_buffer_done(&common->cur_frm->vb,
309 wake_up_interruptible(&common->cur_frm->done); 405 VB2_BUF_STATE_DONE);
310 /* Make curFrm pointing to nextFrm */ 406 /* Make curFrm pointing to nextFrm */
311 common->cur_frm = common->next_frm; 407 common->cur_frm = common->next_frm;
312} 408}
@@ -324,14 +420,11 @@ static void vpif_schedule_next_buffer(struct common_obj *common)
324 unsigned long addr = 0; 420 unsigned long addr = 0;
325 421
326 common->next_frm = list_entry(common->dma_queue.next, 422 common->next_frm = list_entry(common->dma_queue.next,
327 struct videobuf_buffer, queue); 423 struct vpif_cap_buffer, list);
328 /* Remove that buffer from the buffer queue */ 424 /* Remove that buffer from the buffer queue */
329 list_del(&common->next_frm->queue); 425 list_del(&common->next_frm->list);
330 common->next_frm->state = VIDEOBUF_ACTIVE; 426 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
331 if (V4L2_MEMORY_USERPTR == common->memory) 427 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
332 addr = common->next_frm->boff;
333 else
334 addr = videobuf_to_dma_contig(common->next_frm);
335 428
336 /* Set top and bottom field addresses in VPIF registers */ 429 /* Set top and bottom field addresses in VPIF registers */
337 common->set_addr(addr + common->ytop_off, 430 common->set_addr(addr + common->ytop_off,
@@ -505,10 +598,7 @@ static void vpif_calculate_offsets(struct channel_obj *ch)
505 } else 598 } else
506 vid_ch->buf_field = common->fmt.fmt.pix.field; 599 vid_ch->buf_field = common->fmt.fmt.pix.field;
507 600
508 if (V4L2_MEMORY_USERPTR == common->memory) 601 sizeimage = common->fmt.fmt.pix.sizeimage;
509 sizeimage = common->fmt.fmt.pix.sizeimage;
510 else
511 sizeimage = config_params.channel_bufsize[ch->channel_id];
512 602
513 hpitch = common->fmt.fmt.pix.bytesperline; 603 hpitch = common->fmt.fmt.pix.bytesperline;
514 vpitch = sizeimage / (hpitch * 2); 604 vpitch = sizeimage / (hpitch * 2);
@@ -660,10 +750,7 @@ static int vpif_check_format(struct channel_obj *ch,
660 hpitch = vpif_params->std_info.width; 750 hpitch = vpif_params->std_info.width;
661 } 751 }
662 752
663 if (V4L2_MEMORY_USERPTR == common->memory) 753 sizeimage = pixfmt->sizeimage;
664 sizeimage = pixfmt->sizeimage;
665 else
666 sizeimage = config_params.channel_bufsize[ch->channel_id];
667 754
668 vpitch = sizeimage / (hpitch * 2); 755 vpitch = sizeimage / (hpitch * 2);
669 756
@@ -736,7 +823,7 @@ static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
736 823
737 vpif_dbg(2, debug, "vpif_mmap\n"); 824 vpif_dbg(2, debug, "vpif_mmap\n");
738 825
739 return videobuf_mmap_mapper(&common->buffer_queue, vma); 826 return vb2_mmap(&common->buffer_queue, vma);
740} 827}
741 828
742/** 829/**
@@ -753,7 +840,7 @@ static unsigned int vpif_poll(struct file *filep, poll_table * wait)
753 vpif_dbg(2, debug, "vpif_poll\n"); 840 vpif_dbg(2, debug, "vpif_poll\n");
754 841
755 if (common->started) 842 if (common->started)
756 return videobuf_poll_stream(filep, &common->buffer_queue, wait); 843 return vb2_poll(&common->buffer_queue, filep, wait);
757 return 0; 844 return 0;
758} 845}
759 846
@@ -861,8 +948,8 @@ static int vpif_release(struct file *filep)
861 } 948 }
862 common->started = 0; 949 common->started = 0;
863 /* Free buffers allocated */ 950 /* Free buffers allocated */
864 videobuf_queue_cancel(&common->buffer_queue); 951 vb2_queue_release(&common->buffer_queue);
865 videobuf_mmap_free(&common->buffer_queue); 952 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
866 } 953 }
867 954
868 /* Decrement channel usrs counter */ 955 /* Decrement channel usrs counter */
@@ -892,6 +979,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
892 struct channel_obj *ch = fh->channel; 979 struct channel_obj *ch = fh->channel;
893 struct common_obj *common; 980 struct common_obj *common;
894 u8 index = 0; 981 u8 index = 0;
982 struct vb2_queue *q;
895 983
896 vpif_dbg(2, debug, "vpif_reqbufs\n"); 984 vpif_dbg(2, debug, "vpif_reqbufs\n");
897 985
@@ -917,14 +1005,21 @@ static int vpif_reqbufs(struct file *file, void *priv,
917 if (0 != common->io_usrs) 1005 if (0 != common->io_usrs)
918 return -EBUSY; 1006 return -EBUSY;
919 1007
920 /* Initialize videobuf queue as per the buffer type */ 1008 /* Initialize videobuf2 queue as per the buffer type */
921 videobuf_queue_dma_contig_init(&common->buffer_queue, 1009 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
922 &video_qops, vpif_dev, 1010 if (!common->alloc_ctx) {
923 &common->irqlock, 1011 vpif_err("Failed to get the context\n");
924 reqbuf->type, 1012 return -EINVAL;
925 common->fmt.fmt.pix.field, 1013 }
926 sizeof(struct videobuf_buffer), fh, 1014 q = &common->buffer_queue;
927 &common->lock); 1015 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1016 q->io_modes = VB2_MMAP | VB2_USERPTR;
1017 q->drv_priv = fh;
1018 q->ops = &video_qops;
1019 q->mem_ops = &vb2_dma_contig_memops;
1020 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1021
1022 vb2_queue_init(q);
928 1023
929 /* Set io allowed member of file handle to TRUE */ 1024 /* Set io allowed member of file handle to TRUE */
930 fh->io_allowed[index] = 1; 1025 fh->io_allowed[index] = 1;
@@ -935,7 +1030,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
935 INIT_LIST_HEAD(&common->dma_queue); 1030 INIT_LIST_HEAD(&common->dma_queue);
936 1031
937 /* Allocate buffers */ 1032 /* Allocate buffers */
938 return videobuf_reqbufs(&common->buffer_queue, reqbuf); 1033 return vb2_reqbufs(&common->buffer_queue, reqbuf);
939} 1034}
940 1035
941/** 1036/**
@@ -961,7 +1056,7 @@ static int vpif_querybuf(struct file *file, void *priv,
961 return -EINVAL; 1056 return -EINVAL;
962 } 1057 }
963 1058
964 return videobuf_querybuf(&common->buffer_queue, buf); 1059 return vb2_querybuf(&common->buffer_queue, buf);
965} 1060}
966 1061
967/** 1062/**
@@ -977,10 +1072,6 @@ static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
977 struct channel_obj *ch = fh->channel; 1072 struct channel_obj *ch = fh->channel;
978 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1073 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
979 struct v4l2_buffer tbuf = *buf; 1074 struct v4l2_buffer tbuf = *buf;
980 struct videobuf_buffer *buf1;
981 unsigned long addr = 0;
982 unsigned long flags;
983 int ret = 0;
984 1075
985 vpif_dbg(2, debug, "vpif_qbuf\n"); 1076 vpif_dbg(2, debug, "vpif_qbuf\n");
986 1077
@@ -990,76 +1081,11 @@ static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
990 } 1081 }
991 1082
992 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) { 1083 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
993 vpif_err("fh io not allowed \n"); 1084 vpif_err("fh io not allowed\n");
994 return -EACCES; 1085 return -EACCES;
995 } 1086 }
996 1087
997 if (!(list_empty(&common->dma_queue)) || 1088 return vb2_qbuf(&common->buffer_queue, buf);
998 (common->cur_frm != common->next_frm) ||
999 !common->started ||
1000 (common->started && (0 == ch->field_id)))
1001 return videobuf_qbuf(&common->buffer_queue, buf);
1002
1003 /* bufferqueue is empty store buffer address in VPIF registers */
1004 mutex_lock(&common->buffer_queue.vb_lock);
1005 buf1 = common->buffer_queue.bufs[tbuf.index];
1006
1007 if ((buf1->state == VIDEOBUF_QUEUED) ||
1008 (buf1->state == VIDEOBUF_ACTIVE)) {
1009 vpif_err("invalid state\n");
1010 goto qbuf_exit;
1011 }
1012
1013 switch (buf1->memory) {
1014 case V4L2_MEMORY_MMAP:
1015 if (buf1->baddr == 0)
1016 goto qbuf_exit;
1017 break;
1018
1019 case V4L2_MEMORY_USERPTR:
1020 if (tbuf.length < buf1->bsize)
1021 goto qbuf_exit;
1022
1023 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
1024 && (buf1->baddr != tbuf.m.userptr)) {
1025 vpif_buffer_release(&common->buffer_queue, buf1);
1026 buf1->baddr = tbuf.m.userptr;
1027 }
1028 break;
1029
1030 default:
1031 goto qbuf_exit;
1032 }
1033
1034 local_irq_save(flags);
1035 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1036 common->buffer_queue.field);
1037 if (ret < 0) {
1038 local_irq_restore(flags);
1039 goto qbuf_exit;
1040 }
1041
1042 buf1->state = VIDEOBUF_ACTIVE;
1043
1044 if (V4L2_MEMORY_USERPTR == common->memory)
1045 addr = buf1->boff;
1046 else
1047 addr = videobuf_to_dma_contig(buf1);
1048
1049 common->next_frm = buf1;
1050 common->set_addr(addr + common->ytop_off,
1051 addr + common->ybtm_off,
1052 addr + common->ctop_off,
1053 addr + common->cbtm_off);
1054
1055 local_irq_restore(flags);
1056 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1057 mutex_unlock(&common->buffer_queue.vb_lock);
1058 return 0;
1059
1060qbuf_exit:
1061 mutex_unlock(&common->buffer_queue.vb_lock);
1062 return -EINVAL;
1063} 1089}
1064 1090
1065/** 1091/**
@@ -1076,8 +1102,8 @@ static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1076 1102
1077 vpif_dbg(2, debug, "vpif_dqbuf\n"); 1103 vpif_dbg(2, debug, "vpif_dqbuf\n");
1078 1104
1079 return videobuf_dqbuf(&common->buffer_queue, buf, 1105 return vb2_dqbuf(&common->buffer_queue, buf,
1080 file->f_flags & O_NONBLOCK); 1106 (file->f_flags & O_NONBLOCK));
1081} 1107}
1082 1108
1083/** 1109/**
@@ -1090,13 +1116,11 @@ static int vpif_streamon(struct file *file, void *priv,
1090 enum v4l2_buf_type buftype) 1116 enum v4l2_buf_type buftype)
1091{ 1117{
1092 1118
1093 struct vpif_capture_config *config = vpif_dev->platform_data;
1094 struct vpif_fh *fh = priv; 1119 struct vpif_fh *fh = priv;
1095 struct channel_obj *ch = fh->channel; 1120 struct channel_obj *ch = fh->channel;
1096 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX]; 1121 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1097 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id]; 1122 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1098 struct vpif_params *vpif; 1123 struct vpif_params *vpif;
1099 unsigned long addr = 0;
1100 int ret = 0; 1124 int ret = 0;
1101 1125
1102 vpif_dbg(2, debug, "vpif_streamon\n"); 1126 vpif_dbg(2, debug, "vpif_streamon\n");
@@ -1142,95 +1166,13 @@ static int vpif_streamon(struct file *file, void *priv,
1142 return ret; 1166 return ret;
1143 } 1167 }
1144 1168
1145 /* Call videobuf_streamon to start streaming in videobuf */ 1169 /* Call vb2_streamon to start streaming in videobuf2 */
1146 ret = videobuf_streamon(&common->buffer_queue); 1170 ret = vb2_streamon(&common->buffer_queue, buftype);
1147 if (ret) { 1171 if (ret) {
1148 vpif_dbg(1, debug, "videobuf_streamon\n"); 1172 vpif_dbg(1, debug, "vb2_streamon\n");
1149 return ret; 1173 return ret;
1150 } 1174 }
1151 1175
1152 /* If buffer queue is empty, return error */
1153 if (list_empty(&common->dma_queue)) {
1154 vpif_dbg(1, debug, "buffer queue is empty\n");
1155 ret = -EIO;
1156 goto exit;
1157 }
1158
1159 /* Get the next frame from the buffer queue */
1160 common->cur_frm = list_entry(common->dma_queue.next,
1161 struct videobuf_buffer, queue);
1162 common->next_frm = common->cur_frm;
1163
1164 /* Remove buffer from the buffer queue */
1165 list_del(&common->cur_frm->queue);
1166 /* Mark state of the current frame to active */
1167 common->cur_frm->state = VIDEOBUF_ACTIVE;
1168 /* Initialize field_id and started member */
1169 ch->field_id = 0;
1170 common->started = 1;
1171
1172 if (V4L2_MEMORY_USERPTR == common->memory)
1173 addr = common->cur_frm->boff;
1174 else
1175 addr = videobuf_to_dma_contig(common->cur_frm);
1176
1177 /* Calculate the offset for Y and C data in the buffer */
1178 vpif_calculate_offsets(ch);
1179
1180 if ((vpif->std_info.frm_fmt &&
1181 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1182 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1183 (!vpif->std_info.frm_fmt &&
1184 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1185 vpif_dbg(1, debug, "conflict in field format and std format\n");
1186 ret = -EINVAL;
1187 goto exit;
1188 }
1189
1190 /* configure 1 or 2 channel mode */
1191 ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1192
1193 if (ret < 0) {
1194 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1195 goto exit;
1196 }
1197
1198 /* Call vpif_set_params function to set the parameters and addresses */
1199 ret = vpif_set_video_params(vpif, ch->channel_id);
1200
1201 if (ret < 0) {
1202 vpif_dbg(1, debug, "can't set video params\n");
1203 goto exit;
1204 }
1205
1206 common->started = ret;
1207 vpif_config_addr(ch, ret);
1208
1209 common->set_addr(addr + common->ytop_off,
1210 addr + common->ybtm_off,
1211 addr + common->ctop_off,
1212 addr + common->cbtm_off);
1213
1214 /**
1215 * Set interrupt for both the fields in VPIF Register enable channel in
1216 * VPIF register
1217 */
1218 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1219 channel0_intr_assert();
1220 channel0_intr_enable(1);
1221 enable_channel0(1);
1222 }
1223 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1224 (common->started == 2)) {
1225 channel1_intr_assert();
1226 channel1_intr_enable(1);
1227 enable_channel1(1);
1228 }
1229 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1230 return ret;
1231
1232exit:
1233 videobuf_streamoff(&common->buffer_queue);
1234 return ret; 1176 return ret;
1235} 1177}
1236 1178
@@ -1285,7 +1227,7 @@ static int vpif_streamoff(struct file *file, void *priv,
1285 if (ret && (ret != -ENOIOCTLCMD)) 1227 if (ret && (ret != -ENOIOCTLCMD))
1286 vpif_dbg(1, debug, "stream off failed in subdev\n"); 1228 vpif_dbg(1, debug, "stream off failed in subdev\n");
1287 1229
1288 return videobuf_streamoff(&common->buffer_queue); 1230 return vb2_streamoff(&common->buffer_queue, buftype);
1289} 1231}
1290 1232
1291/** 1233/**
diff --git a/drivers/media/video/davinci/vpif_capture.h b/drivers/media/video/davinci/vpif_capture.h
index 8095910f02c..3511510f43e 100644
--- a/drivers/media/video/davinci/vpif_capture.h
+++ b/drivers/media/video/davinci/vpif_capture.h
@@ -26,7 +26,7 @@
26#include <media/v4l2-common.h> 26#include <media/v4l2-common.h>
27#include <media/v4l2-device.h> 27#include <media/v4l2-device.h>
28#include <media/videobuf-core.h> 28#include <media/videobuf-core.h>
29#include <media/videobuf-dma-contig.h> 29#include <media/videobuf2-dma-contig.h>
30#include <media/davinci/vpif_types.h> 30#include <media/davinci/vpif_types.h>
31 31
32#include "vpif.h" 32#include "vpif.h"
@@ -60,11 +60,16 @@ struct video_obj {
60 u32 input_idx; 60 u32 input_idx;
61}; 61};
62 62
63struct vpif_cap_buffer {
64 struct vb2_buffer vb;
65 struct list_head list;
66};
67
63struct common_obj { 68struct common_obj {
64 /* Pointer pointing to current v4l2_buffer */ 69 /* Pointer pointing to current v4l2_buffer */
65 struct videobuf_buffer *cur_frm; 70 struct vpif_cap_buffer *cur_frm;
66 /* Pointer pointing to current v4l2_buffer */ 71 /* Pointer pointing to current v4l2_buffer */
67 struct videobuf_buffer *next_frm; 72 struct vpif_cap_buffer *next_frm;
68 /* 73 /*
69 * This field keeps track of type of buffer exchange mechanism 74 * This field keeps track of type of buffer exchange mechanism
70 * user has selected 75 * user has selected
@@ -73,7 +78,9 @@ struct common_obj {
73 /* Used to store pixel format */ 78 /* Used to store pixel format */
74 struct v4l2_format fmt; 79 struct v4l2_format fmt;
75 /* Buffer queue used in video-buf */ 80 /* Buffer queue used in video-buf */
76 struct videobuf_queue buffer_queue; 81 struct vb2_queue buffer_queue;
82 /* allocator-specific contexts for each plane */
83 struct vb2_alloc_ctx *alloc_ctx;
77 /* Queue of filled frames */ 84 /* Queue of filled frames */
78 struct list_head dma_queue; 85 struct list_head dma_queue;
79 /* Used in video-buf */ 86 /* Used in video-buf */